Files
Lucy c6029af30e Refactor GLOB.hostile_machines to sort by Z-level (#94757)
## About The Pull Request

this refactors `GLOB.hostile_machines` into
`GLOB.hostile_machines_by_z`, which is a per-z list, and mobs targeting
hostile machines (mechas, turrets) will only check those on the same
Z-level.

## Why It's Good For The Game

better performance

## Changelog
🆑
refactor: Refactored how mobs target turrets and mechs, which should
hopefully be more performant.
/🆑
2026-01-06 21:21:29 +01:00

53 lines
1.4 KiB
Plaintext

/// AIs will attack this as a potential target if they see it
/datum/element/hostile_machine
element_flags = ELEMENT_DETACH_ON_HOST_DESTROY
/datum/element/hostile_machine/Attach(datum/target)
. = ..()
if (!isatom(target))
return ELEMENT_INCOMPATIBLE
#ifdef UNIT_TESTS
if(!GLOB.target_interested_atoms[target.type])
stack_trace("Tried to make a hostile machine without updating ai targeting to include it, they must be synced")
#endif
if(ismovable(target))
RegisterSignal(target, COMSIG_MOVABLE_Z_CHANGED, PROC_REF(on_z_change))
add_to_z(target)
/datum/element/hostile_machine/Detach(datum/source)
UnregisterSignal(source, COMSIG_MOVABLE_Z_CHANGED)
remove_from_z(source)
return ..()
/datum/element/hostile_machine/proc/on_z_change(atom/movable/source, turf/old_turf, turf/new_turf, same_z_layer)
SIGNAL_HANDLER
if(same_z_layer)
return
remove_from_z(source, old_turf.z)
add_to_z(source, new_turf.z)
/datum/element/hostile_machine/proc/add_to_z(atom/target, z)
if(isnull(z))
var/turf/target_turf = get_turf(target)
z = target_turf?.z
if(!z)
return
if(!GLOB.hostile_machines_by_z[z])
GLOB.hostile_machines_by_z[z] = list()
GLOB.hostile_machines_by_z[z] |= target
/datum/element/hostile_machine/proc/remove_from_z(atom/target, z)
if(isnull(z))
var/turf/target_turf = get_turf(target)
z = target_turf?.z
if(!z)
return
GLOB.hostile_machines_by_z[z] -= target
if(!length(GLOB.hostile_machines_by_z[z]))
GLOB.hostile_machines_by_z -= z