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.
/🆑
This commit is contained in:
Lucy
2026-01-06 21:21:29 +01:00
committed by GitHub
parent 90dcd6e963
commit c6029af30e
2 changed files with 41 additions and 6 deletions
@@ -1,5 +1,5 @@
/// List of objects that AIs will treat as targets
GLOBAL_LIST_EMPTY_TYPED(hostile_machines, /atom)
GLOBAL_ALIST_EMPTY(hostile_machines_by_z)
/// Static typecache list of things we are interested in
/// Consider this a union of the for loop and the hearers call from below
/// Must be kept up to date with the contents of hostile_machines
@@ -39,9 +39,11 @@ GLOBAL_LIST_INIT(target_interested_atoms, typecacheof(list(/mob, /obj/machinery/
var/list/potential_targets = hearers(aggro_range, get_turf(controller.pawn)) - living_mob //Remove self, so we don't suicide
for (var/atom/hostile_machine as anything in GLOB.hostile_machines)
if (can_see(living_mob, hostile_machine, aggro_range))
potential_targets += hostile_machine
var/turf/mob_turf = get_turf(living_mob)
if(mob_turf?.z)
for (var/atom/hostile_machine as anything in GLOB.hostile_machines_by_z[mob_turf.z])
if (can_see(living_mob, hostile_machine, aggro_range))
potential_targets += hostile_machine
if(!potential_targets.len)
failed_to_find_anyone(controller, target_key, targeting_strategy_key, hiding_location_key)