Page 1 of 1

Access to original health/current lifetime

Posted: Fri Mar 13, 2020 12:57 pm
by Pi-C
I realize something similar has already been asked before, and has been rejected because the requested event would be triggered too often. Perhaps it could get implemented with a small modification to an existing event, instead of an entirely new event?

The problem is that it's currently not possible to get the health of an entity before damage was applied. If you want to prevent an entity from damage by friendly fire, the naive way of doing it is to add final_damage_amount back to entity.health in on_entity_damaged. This won't work as expected because at the moment, entity.health can not be smaller than 0:
health :: float [Read-Write]

Health of the entity. Setting health to less than 0 will set health to 0, entities with 0 health can not be attacked. Setting health to higher than max health will set health to max health.
It would be great if something like "entity.health_before_damage" or "entity.original_health" could be added to on_entity_damaged. Alternatively, it would help if health could be less than 0 in general -- which would imply changing
entities with 0 health can not be attacked
to
entities with health < 0 can not be attacked
I'd also like to have something similar for entities that have lifetime/time_to_live instead of health. My particular use case for this is the issue with invisible fires. I want to create a dummy if a fire is detected. This dummy should be removed again when the fire has expired. Fire detection runs in regular intervals, so it may happen that a fire has been created just after a search for fire is completed. On the next check, part of the fire's lifetime will already have passed, but currently there is no way to know on which tick the fire has been created or how much of its lifetime is left (so if my dummy's TTL is set to fire.maximum_lifetime, it will be removed only some time after the fire has expired). All the prototype provides are these properties:

Code: Select all

initial_lifetime	::	uint32 (optional)
lifetime_increase_by	::	uint32 (optional)
lifetime_increase_cooldown	::	uint32 (optional)
maximum_lifetime	::	uint32 (optional)
Having an additional property like "lifetime" or "remaining_lifetime" would help a lot! Is there any chance we could get access to current health/lifetime of an entity, please?

Re: Access to original health/current lifetime

Posted: Sat Mar 14, 2020 4:29 pm
by Rseding91
This seems like an A-B problem; you don't actually care about the event or what the health was before or any of that. You just want a way to make an entity immune to friendly fire?

Re: Access to original health/current lifetime

Posted: Sat Mar 14, 2020 4:58 pm
by Pi-C
Rseding91 wrote: Sat Mar 14, 2020 4:29 pm This seems like an A-B problem; you don't actually care about the event or what the health was before or any of that. You just want a way to make an entity immune to friendly fire?
Basically that, yes: immunity without additional healing. The current way to make an entity immune from friendly fire is to restore what has been taken away, but that may restore more health than the entity had before because health can't be lower than 0. If there is a better way to achieve that, please enlighten me -- I really haven't come up with any other plan yet. :-)

Re: Access to original health/current lifetime

Posted: Sat Mar 14, 2020 6:02 pm
by Oktokolo
Pi-C wrote: Sat Mar 14, 2020 4:58 pm
Rseding91 wrote: Sat Mar 14, 2020 4:29 pm This seems like an A-B problem; you don't actually care about the event or what the health was before or any of that. You just want a way to make an entity immune to friendly fire?
Basically that, yes: immunity without additional healing. The current way to make an entity immune from friendly fire is to restore what has been taken away, but that may restore more health than the entity had before because health can't be lower than 0. If there is a better way to achieve that, please enlighten me -- I really haven't come up with any other plan yet. :-)
Setting LuaForce.friendly_fire to false might work.

Re: Access to original health/current lifetime

Posted: Sat Mar 14, 2020 6:21 pm
by Pi-C
Oktokolo wrote: Sat Mar 14, 2020 6:02 pm Setting LuaForce.friendly_fire to false might work.
But that is per force, not per entity, right? I want to disable friendly fire from some turrets (or some damage types) only: my water turrets shouldn't be able to do additional damage to entities of the own force if they extinguish fire. There's also the Smart Cannon Shells mod where this would be relevant: If you're using vanilla explosive cannon shells, you can (and should) be in danger of destroying your own stuff, but if you upgrade to the more expensive smart variety, you should be safe from it. Setting "LuaForce.friendly_fire = false" looks like it would prevent friendly fire completely, not selectively.

Re: Access to original health/current lifetime

Posted: Sat Mar 14, 2020 11:11 pm
by smcpeak
In the specific case of Smart Cannon Shells, they already have projectile.force_condition = "not-friend" set, but that does not affect explosive splash damage.

Re: Access to original health/current lifetime

Posted: Mon Mar 16, 2020 11:04 pm
by Pi-C
Pi-C wrote: Fri Mar 13, 2020 12:57 pm Having an additional property like "lifetime" or "remaining_lifetime" would help a lot! Is there any chance we could get access to current health/lifetime of an entity, please?
This would probably also help: Knowing when an entity has been created would allow me to determine the age of an entity by using

Code: Select all

if game.tick - entity.creation_tick >= entity.lifetime then … end
and act on it. This seems to be necessary in the case of fire entities because (as Honktown explained to me) they exist for entity.maximum_lifetime + entity.burnt_patch_lifetime, but are only visible during entity.maximum_lifetime.

Re: Access to original health/current lifetime

Posted: Tue Mar 17, 2020 9:45 am
by Klonan
smcpeak wrote: Sat Mar 14, 2020 11:11 pm In the specific case of Smart Cannon Shells, they already have projectile.force_condition = "not-friend" set, but that does not affect explosive splash damage.
You can also set the force condition on the damage trigger, so that the damage is only applied on 'not-friend' or whomever
Pi-C wrote: Sat Mar 14, 2020 4:58 pmThe current way to make an entity immune from friendly fire is to restore what has been taken away
Or you add a force condition to the trigger effects:
https://wiki.factorio.com/Types/TriggerItem#force

Re: Access to original health/current lifetime

Posted: Tue Mar 17, 2020 12:01 pm
by Optera
Klonan wrote: Tue Mar 17, 2020 9:45 am Or you add a force condition to the trigger effects:
https://wiki.factorio.com/Types/TriggerItem#force
This sounds like it was created to make a bug spray mod. :D

Re: Access to original health/current lifetime

Posted: Wed Mar 18, 2020 3:19 pm
by smcpeak
Klonan wrote: Tue Mar 17, 2020 9:45 am You can also set the force condition on the damage trigger, so that the damage is only applied on 'not-friend' or whomever
Thanks! I was able to find the right place to do that, so the issue for Smart Cannon Shells is resolved as of 0.2.1 of that mod.