Files
Bubberstation/code/datums/proximity_monitor/proximity_monitor.dm
LemonInTheDark 37aad4720d Reworks targeting behavior to fall back onto proximity monitors. Refactors ai cooldowns a bit (#82640)
## About The Pull Request

Nother bit ripped out of #79498
[Implements a get_cooldown() proc to get around dumb manual overrides
and empower me to optimize the findtarget
logic](7047d294dd)

[Adds modify_cooldown, uses it to optimize find_potential_targets
further](4ebc8cedce)

No sense running the behavior if we're just waiting on its output, so
let's run it once a minute just in case, then push an update instantly
if we find something

[Optimizes connect_range and
promxity_monitors](bcf7d7c5b3)

We know what turfs exist before and after a move
We can use this information to prevent trying to update turfs we don't
care about.

This is important because post these changes mobs with fields will be
moving a lot more, so it's gotta be cheap

[Implements a special kind of field to handle ai
targeting](80b63b3445)

If we run targeting and don't like, find anything, we should setup a
field that listens for things coming near us and then handle those
things as we find them.

This incurs a slight startup cost but saves so much time on the churn of
constant costs

Note:
We should also work to figure out a way to avoid waking ais if none is
near them/they aren't doing anything interesting

We don't need to do that immediately this acts as somewhat of a stopgap
(and would be good regardless) but it is worth keeping in mind)

## IMPORTANT

I am unsure whether this is worth it anymore since #82539 was merged. As
I say it was done as a stopgap because ais didn't know how to idle.
If not I'll rip er out and we'll keep the other
refactoring/optimizations.

## Why It's Good For The Game

Cleaner basic ai code, maybe? faster basic ai code, for sure faster
proximity monitors (significantly)
2024-04-16 17:37:46 -06:00

90 lines
3.4 KiB
Plaintext

/datum/proximity_monitor
///The atom we are tracking
var/atom/host
///The atom that will receive HasProximity calls.
var/atom/hasprox_receiver
///The range of the proximity monitor. Things moving wihin it will trigger HasProximity calls.
var/current_range
///If we don't check turfs in range if the host's loc isn't a turf
var/ignore_if_not_on_turf
///The signals of the connect range component, needed to monitor the turfs in range.
var/static/list/loc_connections = list(
COMSIG_ATOM_ENTERED = PROC_REF(on_entered),
COMSIG_ATOM_EXITED = PROC_REF(on_uncrossed),
COMSIG_ATOM_AFTER_SUCCESSFUL_INITIALIZED_ON = PROC_REF(on_initialized),
)
/datum/proximity_monitor/New(atom/_host, range, _ignore_if_not_on_turf = TRUE)
ignore_if_not_on_turf = _ignore_if_not_on_turf
current_range = range
set_host(_host)
/datum/proximity_monitor/proc/set_host(atom/new_host, atom/new_receiver)
if(new_host == host)
return
if(host) //No need to delete the connect range and containers comps. They'll be updated with the new tracked host.
UnregisterSignal(host, list(COMSIG_MOVABLE_MOVED, COMSIG_QDELETING))
if(hasprox_receiver)
UnregisterSignal(hasprox_receiver, COMSIG_QDELETING)
if(new_receiver)
hasprox_receiver = new_receiver
if(new_receiver != new_host)
RegisterSignal(new_receiver, COMSIG_QDELETING, PROC_REF(on_host_or_receiver_del))
else if(hasprox_receiver == host) //Default case
hasprox_receiver = new_host
host = new_host
RegisterSignal(new_host, COMSIG_QDELETING, PROC_REF(on_host_or_receiver_del))
var/static/list/containers_connections = list(COMSIG_MOVABLE_MOVED = PROC_REF(on_moved), COMSIG_MOVABLE_Z_CHANGED = PROC_REF(on_z_change))
AddComponent(/datum/component/connect_containers, host, containers_connections)
RegisterSignal(host, COMSIG_MOVABLE_MOVED, PROC_REF(on_moved))
RegisterSignal(host, COMSIG_MOVABLE_Z_CHANGED, PROC_REF(on_z_change))
set_range(current_range, TRUE)
/datum/proximity_monitor/proc/on_host_or_receiver_del(datum/source)
SIGNAL_HANDLER
qdel(src)
/datum/proximity_monitor/Destroy()
host = null
hasprox_receiver = null
return ..()
/datum/proximity_monitor/proc/set_range(range, force_rebuild = FALSE)
if(!force_rebuild && range == current_range)
return FALSE
. = TRUE
current_range = range
//If the connect_range component exists already, this will just update its range. No errors or duplicates.
AddComponent(/datum/component/connect_range, host, loc_connections, range, !ignore_if_not_on_turf)
/datum/proximity_monitor/proc/on_moved(atom/movable/source, atom/old_loc)
SIGNAL_HANDLER
if(source == host)
hasprox_receiver?.HasProximity(host)
/datum/proximity_monitor/proc/on_z_change()
SIGNAL_HANDLER
return
/datum/proximity_monitor/proc/set_ignore_if_not_on_turf(does_ignore = TRUE)
if(ignore_if_not_on_turf == does_ignore)
return
ignore_if_not_on_turf = does_ignore
//Update the ignore_if_not_on_turf
AddComponent(/datum/component/connect_range, host, loc_connections, current_range, ignore_if_not_on_turf)
/datum/proximity_monitor/proc/on_uncrossed()
SIGNAL_HANDLER
return //Used by the advanced subtype for effect fields.
/datum/proximity_monitor/proc/on_entered(atom/source, atom/movable/arrived, turf/old_loc)
SIGNAL_HANDLER
if(source != host)
hasprox_receiver?.HasProximity(arrived)
/datum/proximity_monitor/proc/on_initialized(turf/location, atom/created, init_flags)
SIGNAL_HANDLER
if(location != host)
hasprox_receiver?.HasProximity(created)