Files
Bubberstation/code/controllers/subsystem/ai_controllers.dm
Watermelon914 6c017cf1e1 Refactors subsystems to use dependency-ordering to determine init order. Subsystems can now declare their own dependencies. (#90268)
## About The Pull Request
As the title says.
`init_order` is no more, subsystems ordering now depends on their
declared dependencies.
Subsystems can now declare which other subsystems need to init before
them using a list and the subsystem's typepath
I.e.
```dm
dependencies = list(
    /datum/controller/subsystem/atoms,
    /datum/controller/subsystem/mapping
)
```
The reverse can also be done, if a subsystem must initialize after your
own:
```dm
dependents = list(
    /datum/controller/subsystem/atoms
)
```
Cyclical dependencies are not allowed and will throw an error on
initialization if one is found.
There's also a debug tool to visualize the dependency graph, although
it's a bit basic:

![image](https://github.com/user-attachments/assets/80c854d9-c2a5-4f2f-92db-a031e9a8e257)

Subsystem load ordering can still be controlled using `init_stage`, some
subsystems use this in cases where they must initialize first or last
regardless of dependencies. An error will be thrown if a subsystem has
an `init_stage` before one of their dependencies.

## Why It's Good For The Game
Makes dealing with subsystem dependencies easier, and reduces the chance
of making a dependency error when needing to shift around subsystem
inits.

## Changelog
🆑
refactor: Refactored subsystem initialization
/🆑
2025-04-03 17:04:30 -04:00

63 lines
2.3 KiB
Plaintext

/// The subsystem used to tick [/datum/ai_controllers] instances. Handling the re-checking of plans.
SUBSYSTEM_DEF(ai_controllers)
name = "AI Controller Ticker"
flags = SS_POST_FIRE_TIMING|SS_BACKGROUND
priority = FIRE_PRIORITY_NPC
dependencies = list(
/datum/controller/subsystem/movement/ai_movement
)
wait = 0.5 SECONDS //Plan every half second if required, not great not terrible.
runlevels = RUNLEVEL_GAME | RUNLEVEL_POSTGAME
///type of status we are interested in running
var/planning_status = AI_STATUS_ON
/// The average tick cost of all active AI, calculated on fire.
var/our_cost
/// The tick cost of all currently processed AI, being summed together
var/summing_cost
/datum/controller/subsystem/ai_controllers/Initialize()
setup_subtrees()
return SS_INIT_SUCCESS
/datum/controller/subsystem/ai_controllers/stat_entry(msg)
var/list/planning_list = GLOB.ai_controllers_by_status[planning_status]
msg = "Planning AIs:[length(planning_list)]/[round(our_cost,1)]%"
return ..()
/datum/controller/subsystem/ai_controllers/fire(resumed)
if(!resumed)
summing_cost = 0
var/timer = TICK_USAGE_REAL
for(var/datum/ai_controller/ai_controller as anything in GLOB.ai_controllers_by_status[planning_status])
if(!ai_controller.able_to_plan)
continue
ai_controller.SelectBehaviors(wait * 0.1)
if(!length(ai_controller.current_behaviors)) //Still no plan
ai_controller.planning_failed()
if(MC_TICK_CHECK)
break
summing_cost += TICK_DELTA_TO_MS(TICK_USAGE_REAL - timer)
if(MC_TICK_CHECK)
return
our_cost = MC_AVERAGE(our_cost, summing_cost)
///Creates all instances of ai_subtrees and assigns them to the ai_subtrees list.
/datum/controller/subsystem/ai_controllers/proc/setup_subtrees()
if(length(GLOB.ai_subtrees))
return
for(var/subtree_type in subtypesof(/datum/ai_planning_subtree))
var/datum/ai_planning_subtree/subtree = new subtree_type
GLOB.ai_subtrees[subtree_type] = subtree
///Called when the max Z level was changed, updating our coverage.
/datum/controller/subsystem/ai_controllers/proc/on_max_z_changed()
if(!length(GLOB.ai_controllers_by_zlevel))
GLOB.ai_controllers_by_zlevel = new /list(world.maxz,0)
while (GLOB.ai_controllers_by_zlevel.len < world.maxz)
GLOB.ai_controllers_by_zlevel.len++
GLOB.ai_controllers_by_zlevel[GLOB.ai_controllers_by_zlevel.len] = list()