Page 1 of 1

[Lua] Event on_pre_entity_damaged

Posted: Wed Apr 17, 2019 8:09 pm
by timboo07up
TL;DR;

Add an lua event before the entity damage is done, so it can be altered.

What:
On the moment an entity receives damage the on_entity_damaged is fired. This is after the damage is applied so no way to alter/modify it during this event. Also because there is no way to receive the health of the entity before the damage is applied it is hard to calculate the actual damage done or alter the amount of damage when it hits zero. Since the health property doesn't allow values below 0.

So if we can have an event that is fired before the damage is applied and we have the possibility to alter the damage amount (optional), we can alter the health or damage properly.

For example during the night you get a 50% bonus damage increase. Currently this is impossible because we cannot alter the actual damage done/received.
The name could be something like on_pre_entity_damaged, but that depends on your naming standards

One of the problems with the on_entity_damaged event is that original_damage_amount and final_damage_amount is always the same when having no research/armour. i would expect this to differ depending on the resistances and/or settings value. This might also be a bug.
p.s. this is al tested with throwing grenades on oneself

Edit: We also noticed these values are 0 as long as you have an active/charged shield. Is it possible to fire this event before the shield damage is done?

Why:

So mods can have more freedom in the amount of damage given/received on certain times or create temporary damage (de)buffs.

Re: [Lua] Event on_pre_entity_damaged

Posted: Wed Apr 17, 2019 11:17 pm
by Rseding91
No.

Such an event would fire all the time needlessly wasting CPU time when it would go unused 99%+ of the time. Additionally Lua events don't support modifying values passed to them in the way you describe.

Re: [Lua] Event on_pre_entity_damaged

Posted: Sun Apr 21, 2019 12:20 pm
by timboo07up
Ah was hoping that wasn't an issue. Than how about allowing a value below zero on the health or the health before the event started as extra argument in the event? so we can calculate the actual damage or buff after the fact?
I understand it might give other implications. But just asking so we might get a proper calculation of the actual/calculated damage.

Re: [Lua] Event on_pre_entity_damaged

Posted: Mon Apr 22, 2019 7:56 pm
by Bilka
The on entity damaged event contains the initial and final damage amount, what exactly do you need the old health for?

Re: [Lua] Event on_pre_entity_damaged

Posted: Wed May 08, 2019 11:15 am
by Multimodcrafter
I too would be grateful for the old health passed to the event. The problem with the on_entity_damaged event ist the following.

Consider an item that protects me from all damage done to me. I would implement an event handler which looks something like this:

Code: Select all

if entity is player and player has protection item in inventory then set entity health = health + final_damage_amount
Now let's say the player is in combat and his health is getting low - maybe down to 10HP - so he crafts one of those protection items. Then he gets hit by an explosion which deals 100 damage. The expected result would be that the player's health stays at 10HP. However, internally the following happens:
1. Factorio deals the damage to the player

Code: Select all

health = 10 - 100 = -90
which is transferred to 0HP since the health can't be negative. It then calls the on_entity_damaged event with 100 for the final_damage_amount parameter. So our event handler would calculate

Code: Select all

health = 0 + 100 = 100
and therefore doesn't just protect the player from the damage but rather heal him up by 90HP.

Now if we had the old health, our handler could just say

Code: Select all

if entitty is player and player has protection item in inventory then set entity health = old health
Another fix for this would be to change the value from final_damage_amount to the actual ammount of damage being dealt to the entity so the condition

Code: Select all

health + final_damage_amount == old_health
always holds.

Re: [Lua] Event on_pre_entity_damaged

Posted: Wed May 08, 2019 2:50 pm
by eradicator
I remember reading a similar request quite some time ago...
I agree that one could argue that if an entity only has 100HP left, you can not deal 200 damage to it. Maybe adding "overkill_damage" or "wasted_damage" as an optional parameter to on_entity_damaged would work?

Code: Select all

overkill_damage = (final_damage > current_hp) and (final_damage - current_hp) or nil