De-aggro'ing hostile units for safe teleport

Place to get help with not working mods / modding interface.
Post Reply
User avatar
Deadlock989
Smart Inserter
Smart Inserter
Posts: 2528
Joined: Fri Nov 06, 2015 7:41 pm

De-aggro'ing hostile units for safe teleport

Post by Deadlock989 »

If a player character is teleported a great distance while being attacked by biters, massive lag spikes ensue as a crowd of biters tries to path to their target which may be a million tiles away now.

What is the best way of telling nearby biters to "lose" their target and carry on with any other normal business?
Last edited by Deadlock989 on Thu Oct 29, 2020 7:09 pm, edited 1 time in total.
Image

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

Re: De-aggro unit commands?

Post by Klonan »

If the biters think the target is going to die, they will stop attacking it

So you could spawn a projectile, which does 999999 damage, and target the player.
This will internally set the 'damage to be taken', so the biters will see that the character is going to die, and stop attacking it.

Then after a few ticks, or whenever you teleport the character again, just delete the projectile, and the damage to be taken will be cleared.

User avatar
Deadlock989
Smart Inserter
Smart Inserter
Posts: 2528
Joined: Fri Nov 06, 2015 7:41 pm

Re: De-aggro unit commands?

Post by Deadlock989 »

Should the projectile be riding in an invisible car? :shock:

I had some success with this:

Code: Select all

local biters = player.surface.find_entities_filtered{position = teleporter.position, radius = 50, type = "unit"}
for _,biter in pairs(biters) do
	biter.set_command {
		type = defines.command.wander,
		ticks_to_wait = 30,
		distraction = defines.distraction.by_enemy
	}
end
It mainly works because the player is made indestructible just before teleport so the biters don't try to re-target them. It doesn't catch everything though. The player could be running from a biter nest patch a long way off that was aggro'ed and the biters are still pathing their way to them but beyond 50 tiles. I don't know what range biters are guaranteed to lose interest in a player but it's a bigger and bigger search radius and more dead UPS. But if you don't catch them all, just one biter trying to path a million tiles through uncharted territory can produce some nasty lag. One of my tests on a large nest's worth all at once destabilised my PC so badly I had to manually power it off.

To be honest though I don't know what I'm doing with unit commands. I see there are compound commands that might include an attack sub-command. I also vaguely understand that commands can work on unit groups rather than units. I don't want to destabilise some biter group and have them behave erratically forever.
Image

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: De-aggro unit commands?

Post by eradicator »

Klonan wrote: ↑
Wed Oct 28, 2020 5:45 pm
So you could spawn a projectile, which does 999999 damage, and target the player.
Am i correct in assuming that neither the distance not the speed of the projectile matter for that? If yes that has other interesting applications...
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.

User avatar
Deadlock989
Smart Inserter
Smart Inserter
Posts: 2528
Joined: Fri Nov 06, 2015 7:41 pm

Re: De-aggro unit commands?

Post by Deadlock989 »

eradicator wrote: ↑
Wed Oct 28, 2020 11:35 pm
Klonan wrote: ↑
Wed Oct 28, 2020 5:45 pm
So you could spawn a projectile, which does 999999 damage, and target the player.
Am i correct in assuming that neither the distance not the speed of the projectile matter for that? If yes that has other interesting applications...
I tried it, couldn't get it work. I didn't try very hard tbf.
Image

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: De-aggro unit commands?

Post by eradicator »

Deadlock989 wrote: ↑
Wed Oct 28, 2020 11:36 pm
eradicator wrote: ↑
Wed Oct 28, 2020 11:35 pm
Klonan wrote: ↑
Wed Oct 28, 2020 5:45 pm
So you could spawn a projectile, which does 999999 damage, and target the player.
Am i correct in assuming that neither the distance not the speed of the projectile matter for that? If yes that has other interesting applications...
I tried it, couldn't get it work. I didn't try very hard tbf.
No luck either. Tried all sorts of projectiles including homing ones like "laser" and "blue-laser" but the biters don't care. Also most projectiles ignore "speed" given with create_entity it seems.
What does utterly confuse them is if i have 0 health:

Code: Select all

/c x = game.player.character script.on_event(defines.events.on_tick,function()x.health=0 end)
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.

User avatar
Deadlock989
Smart Inserter
Smart Inserter
Posts: 2528
Joined: Fri Nov 06, 2015 7:41 pm

Re: De-aggro unit commands?

Post by Deadlock989 »

eradicator wrote: ↑
Thu Oct 29, 2020 12:34 am
What does utterly confuse them is if i have 0 health:
Thanks, that works (given that the character is set to non-destructible). My only small annoyance with it (seems there is always a price for these hacks) is that because the character is detached, you get three small grey health indicator bars drawn beneath them during the cutscene (like they are a badly damaged wall or something). Trying to find a way to hide it. The character needs to be in that state for at least a second or so before the teleport so that all the units have time to select new targets.

I'm on the verge of reporting this as a bug, it doesn't seem right that I can hard crash Factorio and even destabilise Windows with runaway memory usage by teleporting a character under attack.
Image

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: De-aggro unit commands?

Post by eradicator »

Deadlock989 wrote: ↑
Thu Oct 29, 2020 10:51 am
I'm on the verge of reporting this as a bug, it doesn't seem right that I can hard crash Factorio and even destabilise Windows with runaway memory usage by teleporting a character under attack.
True that. I never used my teleporters while under fire i guess - at least i never noticed any such thing.

Personally i'd prefer if there was a non-hacky way to just force the character LuaEntity out of the biters target list manually - for other usecases too. @Klonan said there's already a mechanism to make them ignore entities "that are going to die", so i wonder if requesting something like LuaEntity.permanently_ignored_by_biters(bool) or LuaEntity.remove_from_current_biter_targets() would be realistic.
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.

User avatar
Deadlock989
Smart Inserter
Smart Inserter
Posts: 2528
Joined: Fri Nov 06, 2015 7:41 pm

Re: De-aggro unit commands?

Post by Deadlock989 »

eradicator wrote: ↑
Thu Oct 29, 2020 3:27 pm
True that. I never used my teleporters while under fire i guess - at least i never noticed any such thing.

Personally i'd prefer if there was a non-hacky way to just force the character LuaEntity out of the biters target list manually - for other usecases too. @Klonan said there's already a mechanism to make them ignore entities "that are going to die", so i wonder if requesting something like LuaEntity.permanently_ignored_by_biters(bool) or LuaEntity.remove_from_current_biter_targets() would be realistic.
I guess that would just be one more thing for every attacking biter to check every single tick. Since vanilla never, ever teleports, there's no worries about pathfinding horror when the worst that can happen is a player with 8 exoskeletons steps half a tile out of range in a tick. Part of my horrible issue is the need to do this, so the distance involved is about as large as it can possibly be, which explains the memory death spiral (20 biters trying to pathfind 1000km through uncharted territory). So a simple visible = false flag for rendering would tick a ton of my boxes and probably kill off this worst case. But in fact in a megabase it's not unreasonable to expect a player to set up two teleporter bases a few km apart, so even if I could just hide the character, biters still might have to path-find much further than they normally would. And because it's a random convergence of events, none of the path-caching optimisations will ever apply.

The code snippet above works as well without having to tinker with health - additionally refinement is to check for units that are actually on the enemy force and maybe setting distraction to "anything" - but it seems to render the affected biters oblivious to the player if they then walk back to that spot. They will continue to attack anything else.
Image

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: De-aggro unit commands?

Post by eradicator »

Deadlock989 wrote: ↑
Thu Oct 29, 2020 3:51 pm
eradicator wrote: ↑
Thu Oct 29, 2020 3:27 pm
LuaEntity.remove_from_current_biter_targets()
I guess that would just be one more thing for every attacking biter to check every single tick.
What i meant there was something that just works once (and doesn't have the range problem that your snippet has). Thus reducing the problem to preventing the re-targeting of the entity, which should be much easier.

Does teleporting to another surface de-aggro? In which case it might be feasible to teleport to your "limbo" surface and back (possibly with one tick delay).
Deadlock989 wrote: ↑
Thu Oct 29, 2020 3:51 pm
visible = false flag for rendering
Looking at the character prototye, maybe you could define an animation for an armor that is completely transparent? Which would reduce the problem to temporary swapping of the armor slot. Though if you start manipulating the character prototype you enter the realm where you have to decide wheather to support charcter skin mods or not.
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.

User avatar
Deadlock989
Smart Inserter
Smart Inserter
Posts: 2528
Joined: Fri Nov 06, 2015 7:41 pm

Re: De-aggro unit commands?

Post by Deadlock989 »

eradicator wrote: ↑
Thu Oct 29, 2020 5:00 pm
Does teleporting to another surface de-aggro? In which case it might be feasible to teleport to your "limbo" surface and back (possibly with one tick delay).
See the other thread - this is a character, not a player, so it can't be transported cross-surface. Characters are disconnected from / not controlled by (but still associated with) the player during a cutscene - putting a disconnected character into a vehicle breaks even the association. Why you can only teleport a character when it is attached to a player remains a mystery but cross-surface teleports are players (with or without a character attached) and vehicles and nothing else.

I did look at swapping out armour for a new armour with a blank animation but then you're juggling armours and equipment grids and inventory bonuses and god knows what. It's next on my list of things to try if I decide I really don't like the half-blind messing with unit commands. The health = 0 thing works perfectly functionality-wise, it just has that unfortunate visual glitch (again because it's a disconnected character, not a player).
Image

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: De-aggro unit commands?

Post by eradicator »

Deadlock989 wrote: ↑
Thu Oct 29, 2020 5:22 pm
eradicator wrote: ↑
Thu Oct 29, 2020 5:00 pm
Does teleporting to another surface de-aggro? In which case it might be feasible to teleport to your "limbo" surface and back (possibly with one tick delay).
See the other thread - this is a character, not a player, so it can't be transported cross-surface.
I know that the character is not attached *during* the cutscene. But as far as i understood you so far you need to de-aggro *before* the cutscene starts. At which point the player is still attached.

I just tested this and

Code: Select all

/sudo x = here me.teleport(here,'test') me.teleport(here,'nauvis')
This instantly de-aggros all biters.
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.

User avatar
Deadlock989
Smart Inserter
Smart Inserter
Posts: 2528
Joined: Fri Nov 06, 2015 7:41 pm

Re: De-aggro unit commands?

Post by Deadlock989 »

eradicator wrote: ↑
Thu Oct 29, 2020 5:53 pm
I just tested this and

Code: Select all

/sudo x = here me.teleport(here,'test') me.teleport(here,'nauvis')
This instantly de-aggros all biters.
Yep, that works well. Back and forth in the same tick = no visual clue that you just spent a millisecond in a dungeon dimension and all the biters immediately lose interest. Tested in multiplayer as well and it looks visually fine.

Nice find, thanks.

Image
Image

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: De-aggro unit commands?

Post by eradicator »

Glad i could help.
Deadlock989 wrote: ↑
Thu Oct 29, 2020 6:47 pm
no visual clue that you just spent a millisecond in a dungeon dimension
Yea, because rendering only happens once per tick for ... obvious reasons :lol: .

Awesome animation. Looking forward to that release :).
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 β€œModding help”