Access to original health/current lifetime

Place to ask discuss and request the modding support of Factorio. Don't request mods here.
Post Reply
Pi-C
Smart Inserter
Smart Inserter
Posts: 1652
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Access to original health/current lifetime

Post 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?
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

Rseding91
Factorio Staff
Factorio Staff
Posts: 13209
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Access to original health/current lifetime

Post 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?
If you want to get ahold of me I'm almost always on Discord.

Pi-C
Smart Inserter
Smart Inserter
Posts: 1652
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Access to original health/current lifetime

Post 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. :-)
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

User avatar
Oktokolo
Filter Inserter
Filter Inserter
Posts: 883
Joined: Wed Jul 12, 2017 5:45 pm
Contact:

Re: Access to original health/current lifetime

Post 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.

Pi-C
Smart Inserter
Smart Inserter
Posts: 1652
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Access to original health/current lifetime

Post 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.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

smcpeak
Burner Inserter
Burner Inserter
Posts: 9
Joined: Tue Dec 05, 2017 7:50 am
Contact:

Re: Access to original health/current lifetime

Post 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.

Pi-C
Smart Inserter
Smart Inserter
Posts: 1652
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Access to original health/current lifetime

Post 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.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5150
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: Access to original health/current lifetime

Post 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 pm
The 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

User avatar
Optera
Smart Inserter
Smart Inserter
Posts: 2916
Joined: Sat Jun 11, 2016 6:41 am
Contact:

Re: Access to original health/current lifetime

Post 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

smcpeak
Burner Inserter
Burner Inserter
Posts: 9
Joined: Tue Dec 05, 2017 7:50 am
Contact:

Re: Access to original health/current lifetime

Post 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.

Post Reply

Return to “Modding interface requests”