gpt-4o-2024-08-06· OpenAI
WildChat-4.8M (AI2)
When two different enemies and allies meet, once they attack their relected damage…
- User
When two different enemies and allies meet, once they attack their relected damage bounces infinitely. Can you make this not happen?
if e and h.spiked_armor > 0 and e.soldier and e.soldier.target_id then local t = store.entities[e.soldier.target_id] if t and t.health and not t.health.dead and not t.health.spiked_armor then local sad = E:create_entity("damage") sad.damage_type = DAMAGE_TRUE sad.value = math.ceil(h.spiked_armor * d.value) sad.source_id = e.id sad.target_id = t.id table.insert(store.damage_queue, sad) end end if e and h.spiked_armor > 0 and e.enemy and d.target_id then -- Reflect damage back to the attacker local attacker = store.entities[d.target_id] if attacker and attacker.health and not attacker.health.dead then local reflected_damage = math.ceil(h.spiked_armor * d.value) -- Create a new damage entity for the reflected damage local reflected = E:create_entity("damage") reflected.damage_type = DAMAGE_TRUE reflected.value = reflected_damage reflected.source_id = e.id -- the enemy reflecting the damage reflected.target_id = e.target_id -- the original attacker -- Optionally, you can add more properties if needed table.insert(store.damage_queue, reflected) end end - Assistant
To prevent infinite bouncing of reflected damage between the enemies and allies, you can implement a tracking mechanism or limit the number of reflections. Here's one approach:
- Track a reflection count or a flag for each damage entity to prevent it from being reflected back and forth indefinitely.
- Add a condition to check the reflection count or flag before executing additional reflections.
Here’s how you might implement this logic:
-- Assuming DAMAGE_REFLECT_LIMIT is the maximum number of reflections allowed local DAMAGE_REFLECT_LIMIT = 1 -- Helper function to reflect damage local function reflect_damage(source, target, original_damage_value, multiplier) local reflected_damage = math.ceil(multiplier * original_damage_value) -- Create a new damage entity for the reflected damage local reflected = E:create_entity("damage") reflected.damage_type = DAMAGE_TRUE reflected.value = reflected_damage reflected.source_id = source.id reflected.target_id = target.id -- Add a reflection count to limit further reflections reflected.reflection_count = (source.reflection_count or 0) + 1 -- Optionally, add more properties if needed table.insert(store.damage_queue, reflected) end if e and h.spiked_armor > 0 and e.soldier and e.soldier.target_id then local t = store.entities[e.soldier.target_id] if t and t.health and not t.health.dead and not t.health.spiked_armor then reflect_damage(e, t, d.value, h.spiked_armor) end end if e and h.spiked_armor > 0 and e.enemy and d.target_id then local attacker = store.entities[d.target_id] if attacker and attacker.health and not attacker.health.dead then -- Check if the reflected damage can be further reflected if (d.reflection_count or 0) < DAMAGE_REFLECT_LIMIT then reflect_damage(e, attacker, d.value, h.spiked_armor) end end endKey Modifications:
- Reflection Count: Added a
reflection_countproperty to the damage entities to track how many times the damage has been reflected. - Limit Reflections: Introduced a condition that only allows reflections up to
DAMAGE_REFLECT_LIMITtimes, effectively preventing infinite bouncing.
This approach gives you control over how much damage reflection occurs, avoiding infinite loops in your damage handling code. Adjust
DAMAGE_REFLECT_LIMITas needed for your game's balance and mechanics.