Add efficient method for damaging (or killing) LuaEntities from the lua API

Place to ask discuss and request the modding support of Factorio. Don't request mods here.
BicycleEater
Fast Inserter
Fast Inserter
Posts: 153
Joined: Sun Jul 26, 2020 4:05 pm
Contact:

Add efficient method for damaging (or killing) LuaEntities from the lua API

Post by BicycleEater »

TL;DR
Adding a method to the LuaEntity lua API, which can do damage to (or just kill) the entity, without spawning particles/nice graphical effects, for performance reasons.

What ?
The current die(),and damage() methods in LuaEntity trigger all the particles and effects associated with the death of that entity. The destroy() method is not intended for standard damage effects, it doesn't create ghosts or leave corpses, and doesn't behave uniformly (destroy the character, and you end up in god mode).
I really want an in-between, something which applies normal damage calculations and long-term effects (such as corpses/script triggers), without the short-term overhead of the normal method (e.g. particles). Something like damage_without_particles().
An alternative would be some kind of more aggressive option for particles in the game settings, which means they don't get created at all.
Why ?
I have a mod which adds really huge nuclear weapons with realistic damage calculations, and it runs reasonably well, but the damage scripts slow the game to a crawl, because they kill so many entities, resulting in huge numbers of particles - most of them from trees and enemies. These are spawned miles away from the player, and can't possibly be seen or have any effect on gameplay - most of them are on bits of the map which haven't been charted yet. There are so many of these particles that they can out-of-memory crash the game, and the whole computer (this happened on a computer with 16GB of RAM).

To solve this, I made it so that the script calls destroy() on any tree it is going to kill, and spawns a corpse manually. This reduced the performance impact significantly, so the cause of the slowdown is definitely either particles, or something similar.

The current particle options only allow the player to stop the rendering of particles - they are still spawned in, and have CPU and memory costs associated.
This would also allow other wide-area scripts to run faster, when killing large numbers of entities.
The relevant mod is https://mods.factorio.com/mod/True-Nukes.
FoxLBA
Burner Inserter
Burner Inserter
Posts: 11
Joined: Mon May 01, 2017 6:45 pm
Contact:

Re: Add efficient method for damaging (or killing) LuaEntities from the lua API

Post by FoxLBA »

The ideal option would be to add an option that limits the number of new particle generators appearing in the tick (if the particles somehow affect the gamestate, then add a hard limit or limit on map option).

An example of comparing the methods "entity.die()" and "entity.destroy()".

A couple more examples where the same problem arises:
Krastorio 2: Anti Biter Virus - End game OP weapon. 1 capsule destroys 40% of enemies on the map and reduces the evolution factor.
Space Exploration: Plague rocket - Expensive ammunition that wipe out all life from on a planet. It has strong side effects.
BicycleEater
Fast Inserter
Fast Inserter
Posts: 153
Joined: Sun Jul 26, 2020 4:05 pm
Contact:

Re: Add efficient method for damaging (or killing) LuaEntities from the lua API

Post by BicycleEater »

Another factor is that the game simulates the particles even if they aren't rendered, and I've seen True-Nukes produce particle updates in excess of 20ms/tick for several minutes straight - and that is with substantial anti-particle measures in place (in fact a huge amount of T-N is anti-particle code), using destroy on all trees and biters.
At least half of the performance impact of True-Nukes is nothing but particles which I can't do anything about.
User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5284
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: Add efficient method for damaging (or killing) LuaEntities from the lua API

Post by Klonan »

The biters don't spawn particles if they are damaged by 'fire' damage type, if the script uses this type to do the damage it will not spawn the particles.

Mods can also add other damage types to the `damage_type_filters` in the particle trigger effects to also filter with those ones.
BicycleEater
Fast Inserter
Fast Inserter
Posts: 153
Joined: Sun Jul 26, 2020 4:05 pm
Contact:

Re: Add efficient method for damaging (or killing) LuaEntities from the lua API

Post by BicycleEater »

That sounds great, but it doesn't appear in the documentation for TriggerEffect (https://wiki.factorio.com/Types/TriggerEffect), so I'm not sure what TriggerEffect types this works for...
The hit-effects.lua file seems to use it for "create-entity" type effects. I'm also curious why that file uses "create-entity" type effects instead of "create-explosion" type effects.
Please could you clarify?

Also, from some brief experiments, the filter only works for damage, not death - is there any particular reason why it couldn't be implemented for death as well?
Post Reply

Return to “Modding interface requests”