[Lua] Event on_pre_entity_damaged

Things that we aren't going to implement
timboo07up
Inserter
Inserter
Posts: 23
Joined: Wed Nov 07, 2018 8:23 pm
Contact:

[Lua] Event on_pre_entity_damaged

Post 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.
Rseding91
Factorio Staff
Factorio Staff
Posts: 14348
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: [Lua] Event on_pre_entity_damaged

Post 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.
If you want to get ahold of me I'm almost always on Discord.
timboo07up
Inserter
Inserter
Posts: 23
Joined: Wed Nov 07, 2018 8:23 pm
Contact:

Re: [Lua] Event on_pre_entity_damaged

Post 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.
Bilka
Factorio Staff
Factorio Staff
Posts: 3310
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: [Lua] Event on_pre_entity_damaged

Post by Bilka »

The on entity damaged event contains the initial and final damage amount, what exactly do you need the old health for?
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.
Multimodcrafter
Manual Inserter
Manual Inserter
Posts: 1
Joined: Wed May 08, 2019 10:51 am
Contact:

Re: [Lua] Event on_pre_entity_damaged

Post 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.
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5207
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: [Lua] Event on_pre_entity_damaged

Post 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
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
Post Reply

Return to “Won't implement”