Optimize find_potential_targets self cost (#80602)

![image](https://github.com/tgstation/tgstation/assets/35135081/84ae20b6-5f44-4a69-bda3-0df1435dea5c)

`find_potential_targets/perform` currently has a pretty bad self cost in
part due to it running a second "loop over everything in range" check to
find turrets and mechs. This doesn't drop it down by as much as I'd like
because it still needs `hearers`, it still shows up pretty high, but
this at least cuts out some unnecessary work.

Best case is likely to minimize work AIs need to do when there are no
players on their z-level, as there are a lot of calls from Lavaland.
This commit is contained in:
Mothblocks
2023-12-28 14:14:49 +01:00
committed by GitHub
parent a1b9c10924
commit 404d2cb36c
5 changed files with 25 additions and 5 deletions
@@ -1,11 +1,12 @@
/// List of objects that AIs will treat as targets
GLOBAL_LIST_EMPTY_TYPED(hostile_machines, /atom)
/datum/ai_behavior/find_potential_targets
action_cooldown = 2 SECONDS
/// How far can we see stuff?
var/vision_range = 9
/// Blackboard key for aggro range, uses vision range if not specified
var/aggro_range_key = BB_AGGRO_RANGE
/// Static typecache list of potentially dangerous objs
var/static/list/hostile_machines = typecacheof(list(/obj/machinery/porta_turret, /obj/vehicle/sealed/mecha))
/datum/ai_behavior/find_potential_targets/perform(seconds_per_tick, datum/ai_controller/controller, target_key, targeting_strategy_key, hiding_location_key)
. = ..()
@@ -26,9 +27,9 @@
var/list/potential_targets = hearers(aggro_range, get_turf(controller.pawn)) - living_mob //Remove self, so we don't suicide
for(var/HM in typecache_filter_list(range(aggro_range, living_mob), hostile_machines)) //Can we see any hostile machines?
if(can_see(living_mob, HM, aggro_range))
potential_targets += HM
for (var/atom/hostile_machine as anything in GLOB.hostile_machines)
if (can_see(living_mob, hostile_machine, aggro_range))
potential_targets += hostile_machine
if(!potential_targets.len)
finish_action(controller, succeeded = FALSE)