Replaced our NPC AI with Behavior Trees. (#96628)

This PR replaces our current NPC AI with a [behavior tree
system](https://en.wikipedia.org/wiki/Behavior_tree_(artificial_intelligence,_robotics_and_control)).
Behavior trees are a common way of creating AI in which you place nodes
in a tree structure to define what actions an AI should take.

AI controllers defined a list of /datum/ai_planning_subtree types in
behavior_nodes. Each subtree was a self-contained unit that could call
queue_behavior() to fire off /datum/ai_behavior actions. The controller
iterated subtrees in order, each one deciding independently whether to
queue something and deciding whether the next subtree would run.

This has a few issues:
1. There's no real structure; you are just defining a list of things to
try in order.
2. There was a loooot of subtrees that were basically the same as
another but with some slight modification
3. It was hard to understand.

Controllers now define a single json file describing a tree of nodes.
The tree is composed of structural composites:

Sequence - do A, then B, then C (and so on)
Selector - try A, if it fails try B, then C (and so on)
Parallel - run A and B simultaneously, with configurable failure/success
policies and or looping behavior
Subplan - loop a child continiously

Along that we also have "Decorators". These are nodes that basically
check a condition (E.g.; do we have a combat target). These decorators
can be used to gate behavior and are re-useable across behavior trees.
They also have a concept known as "Observers". Which lets them cancel
lower priority behavior in case their condition changes (Which we check
whenever a signal fires that fits that specific decorator). This makes
the AI much more responsive to change in environment.

For behaviors, we still use the ai_behavior datums. These are the actual
behaviors such as "Move to X", "Attack X". The only major change is that
these can no longer sleep() since they now run in the ai_controller.

Lastly, we now also have subtrees, except now they are essentially
pieces of behavior tree that can be re-used, or even overriden at
runtime or as a variable. Allowing for making modular AI made out of
several smaller trees.

You can set variables on these nodes directly via the extension (see
below), which should reduce the need to make subtypes of behaviors by a
lot. All of these vars are saved on the JSON and will be applied at
runtime.

If you are using subtrees, you can also assign "bindings" to these
variables, which will allow instances of the subtree to override those
variables.

Since a tree structure with variables becomes hard to parse in a JSON,
I've made a VSCode extension to edit these JSONs:

https://marketplace.visualstudio.com/items?itemName=BehaviorTreeG.behaviortreeg
https://github.com/CabinetOnFire/BehaviorTreeG

<img width="1795" height="1268" alt="image"
src="https://github.com/user-attachments/assets/56aa2f0b-3cf9-449f-bca4-8281fca82db6"
/>

This extension allows you to edit the behavior tree JSONs, and browse
through all the behaviors/decorators/subtrees we have

If you'd like more info on how to build these AI check out the
learn_ai.md. I will also make a tutorial to go over more depth on what
the system offers because I kind of suck at doing technical write-ups.

Targetting has been changed to. I've made a new acquire_targets behavior
that takes a target_source (what am I targetting) and
targetting_strategy (what does the candidate need to fulfill to be
considered a target). This allows us to make composites targetting
combinations to reduce the amount of specific find_and_set esque
behaviors we had before. Not everything is ported to this system but
that would be a longer term goal.

I've added a new build_bt script that converts all the behavior tree
JSONs into compiled versions. Why is this needed? Because I wanted to
keep using defines in behavior trees, so we need a way to convert this
into literal values before we send it to DM. This script runs on compile
and should also run in CI (If I didn't fuck that up!). This saves to a
new build/ folder.

I've ported every single AI in the game to this system (except raptors,
Kobsa is working on those so should be in soon!), so I do expect some
bugs to come out of this. But I also fixed some issues that have
probably been in the game for a long time such as:
- Fixed penguins being unable to fish
- Fixed bileworms not being able to devour people
- Fixes goldgrubs not grubbing gold (they could not mine!)
- Lizards actually eat food they find

Either way, I'd reccomend a long TM on this.

1. (Hopefully) a better development experience for making AI
2. Less copy-paste for behaviors, we should be able to re-use more
pieces to make behavior
3. Behavior trees is a more common pattern in making AI, so it should be
easier to find resources to find out how to do things.

🆑 CabinetOnFire, Iamgoofball, SmartKar, Ben10omintrix
refactor: Replaces our AI system with behavior trees, porting all
datum/ai to it
/🆑

I will add this PR with more details down the line. I think I got the
big picture but its a big PR, so sorry if I missed something important.

---------

Co-authored-by: Iamgoofball <iamgoofball@gmail.com>
Co-authored-by: SmArtKar <44720187+SmArtKar@users.noreply.github.com>
Co-authored-by: Ghom <42542238+Ghommie@users.noreply.github.com>
Co-authored-by: Ben10Omintrix <138636438+Ben10Omintrix@users.noreply.github.com>
Co-authored-by: SyncIt21 <110812394+SyncIt21@users.noreply.github.com>
This commit is contained in:
CabinetOnFire
2026-08-15 10:33:57 -06:00
committed by The Sharkenning
co-authored by Iamgoofball SmArtKar Ghom Ben10Omintrix SyncIt21
parent ad0d6a3e7d
commit df7832aa43
1272 changed files with 37929 additions and 13503 deletions
+129 -27
View File
@@ -1,35 +1,87 @@
/// How many of the most expensive controllers to track per pass for the MC stat entry
#define AI_STAT_EXPENSIVE_TRACKED 5
/// The subsystem used to tick [/datum/ai_controllers] instances. Handling the re-checking of plans.
SUBSYSTEM_DEF(ai_controllers)
name = "AI Controller Ticker"
ss_flags = SS_POST_FIRE_TIMING|SS_BACKGROUND
ss_flags = SS_POST_FIRE_TIMING
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.
wait = 0.25 SECONDS //Plan every 1/4th second if required. In theory your AI should not be planning this much, but its useful because we want planning to be responsive when a previous plan ends.
runlevels = RUNLEVEL_GAME | RUNLEVEL_POSTGAME
var/list/currentrun = list()
///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
/// CPU cost accumulated by the in-progress pass, summed across fires.
var/summing_cost
/// world.time at which the in-progress pass started.
var/pass_started
/// How many controllers the in-progress pass started with.
var/pass_size
/// Average wall-clock duration for a full pass on controllers
var/average_pass_time
/// Longest gap any single controller went between two ticks.
var/longest_tick_gap
/// Running longest tick gap of the in-progress pass.
var/summing_tick_gap
/// Display strings for the most expensive controllers of the last completed pass, most expensive first.
var/list/most_expensive = list()
/// Worst SelectBehaviors cost seen this round.
var/worst_controller_cost = 0
/// Display string for the controller responsible for worst_controller_cost.
var/worst_controller_name
/// Running top-cost candidates of the in-progress pass. Assoc list of controller -> SelectBehaviors cost in ms, has a capped amount of entries
var/list/summing_expensive = list()
/// Cheapest cost in summing_expensive once it's full; a controller must beat this to enter the list.
var/summing_expensive_cutoff = 0
/// List of all targeting_strategy singletons, key is the typepath while assigned value is a newly created instance of the typepath. See setup_targeting_strats()
var/list/targeting_strategies
/// List of all target_priority_strategy singletons, key is the typepath while assigned value is a newly created instance of the typepath. See setup_target_priority_strats()
var/list/target_priority_strategies
/// List of all target_source singletons, key is the typepath while assigned value is a newly created instance of the typepath. See setup_target_sources()
var/list/target_sources
///AI controllers, sorted by their status
var/list/ai_controllers_by_status = list(
AI_STATUS_ON = list(),
AI_STATUS_ON_LOW = list(),
AI_STATUS_OFF = list(),
)
///AI controllers, sorted by their z level
var/list/ai_controllers_by_zlevel = list()
/datum/controller/subsystem/ai_controllers/Recover()
if(islist(SSai_controllers.ai_controllers_by_status))
ai_controllers_by_status = SSai_controllers.ai_controllers_by_status
if(islist(SSai_controllers.ai_controllers_by_zlevel))
ai_controllers_by_zlevel = SSai_controllers.ai_controllers_by_zlevel
/datum/controller/subsystem/ai_controllers/Initialize()
setup_subtrees()
setup_targeting_strats()
setup_target_priority_strats()
setup_target_sources()
return SS_INIT_SUCCESS
/datum/controller/subsystem/ai_controllers/stat_entry(msg)
var/list/planning_list = GLOB.ai_controllers_by_status[planning_status]
msg = "\n Planning AIs:[length(planning_list)]/[round(our_cost,1)]%"
msg = "\n Active:[length(SSai_controllers.ai_controllers_by_status[planning_status])]|Off:[length(SSai_controllers.ai_controllers_by_status[AI_STATUS_OFF])]"
msg += "\n Pass:[pass_size - length(currentrun)]/[pass_size]|AvgPass:[round(average_pass_time * 0.1, 0.1)]s|WorstGap:[round(longest_tick_gap * 0.1, 0.1)]s"
if(length(most_expensive))
msg += "\n Top: [most_expensive.Join(" | ")]"
if(worst_controller_name)
msg += "\n Slowest bozo of the round: [worst_controller_name]"
return ..()
/datum/controller/subsystem/ai_controllers/fire(resumed)
if(!resumed)
var/list/planning_list = GLOB.ai_controllers_by_status[planning_status]
var/list/planning_list = SSai_controllers.ai_controllers_by_status[planning_status]
currentrun = planning_list.Copy()
summing_cost = 0
summing_tick_gap = 0
summing_expensive = list()
summing_expensive_cutoff = 0
pass_started = world.time
pass_size = length(currentrun)
//cache for sanic speed (lists are references anyways)
var/list/current_run = src.currentrun
@@ -37,12 +89,35 @@ SUBSYSTEM_DEF(ai_controllers)
while(length(current_run))
var/datum/ai_controller/ai_controller = current_run[length(current_run)]
current_run.len--
if(!ai_controller.able_to_plan)
continue
ai_controller.SelectBehaviors(wait * 0.1)
// Pass the real time since this controller last ticked, so SPT_PROB rolls and
// time accumulators stay time-correct even when a pass takes several seconds.
var/seconds_per_tick = wait * 0.1
if(ai_controller.last_bt_tick)
var/tick_gap = world.time - ai_controller.last_bt_tick
summing_tick_gap = max(summing_tick_gap, tick_gap)
seconds_per_tick = tick_gap * 0.1
ai_controller.last_bt_tick = world.time
var/controller_timer = TICK_USAGE_REAL
ai_controller.SelectBehaviors(seconds_per_tick)
if(!length(ai_controller.current_behaviors)) //Still no plan
ai_controller.planning_failed()
///Lets check if this is an expensive controller
var/tick_cost = TICK_DELTA_TO_MS(TICK_USAGE_REAL - controller_timer)
if(tick_cost > worst_controller_cost)
worst_controller_cost = tick_cost
worst_controller_name = "[ai_controller.pawn || ai_controller] [round(tick_cost, 0.01)]ms"
if(tick_cost > summing_expensive_cutoff)
summing_expensive[ai_controller] = tick_cost
if(length(summing_expensive) > AI_STAT_EXPENSIVE_TRACKED)
var/cheapest_cost = INFINITY
var/datum/ai_controller/cheapest
for(var/datum/ai_controller/candidate as anything in summing_expensive)
if(summing_expensive[candidate] < cheapest_cost)
cheapest_cost = summing_expensive[candidate]
cheapest = candidate
summing_expensive -= cheapest
summing_expensive_cutoff = INFINITY
for(var/datum/ai_controller/candidate as anything in summing_expensive)
summing_expensive_cutoff = min(summing_expensive_cutoff, summing_expensive[candidate])
if(MC_TICK_CHECK)
break
@@ -51,20 +126,47 @@ SUBSYSTEM_DEF(ai_controllers)
if(MC_TICK_CHECK)
return
our_cost = MC_AVERAGE(our_cost, summing_cost)
average_pass_time = MC_AVERAGE(average_pass_time, world.time - pass_started)
longest_tick_gap = summing_tick_gap
///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
// Publish the pass's most expensive controllers as display strings, sorted most expensive first.
// Only a handful of entries, so a selection sort is fine.
var/list/expensive_entries = list()
while(length(summing_expensive))
var/costliest_cost = 0
var/datum/ai_controller/costliest
for(var/datum/ai_controller/candidate as anything in summing_expensive)
if(summing_expensive[candidate] >= costliest_cost)
costliest_cost = summing_expensive[candidate]
costliest = candidate
summing_expensive -= costliest
expensive_entries += "[costliest.pawn || costliest] [round(costliest_cost, 0.01)]ms"
most_expensive = expensive_entries
///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()
if(!length(ai_controllers_by_zlevel))
ai_controllers_by_zlevel = new /list(world.maxz,0)
while (ai_controllers_by_zlevel.len < world.maxz)
ai_controllers_by_zlevel.len++
ai_controllers_by_zlevel[ai_controllers_by_zlevel.len] = list()
/datum/controller/subsystem/ai_controllers/proc/setup_targeting_strats()
targeting_strategies = list()
for(var/target_type in subtypesof(/datum/targeting_strategy))
var/datum/targeting_strategy/target_start = new target_type
targeting_strategies[target_type] = target_start
/datum/controller/subsystem/ai_controllers/proc/setup_target_priority_strats()
target_priority_strategies = list()
for(var/target_type in subtypesof(/datum/target_priority_strategy))
var/datum/target_priority_strategy/target_start = new target_type
target_priority_strategies[target_type] = target_start
/datum/controller/subsystem/ai_controllers/proc/setup_target_sources()
target_sources = list()
for(var/source_type in subtypesof(/datum/target_source))
var/datum/target_source/source = new source_type
target_sources[source_type] = source
#undef AI_STAT_EXPENSIVE_TRACKED
@@ -0,0 +1,6 @@
/// Plans background controllers that are active when unwatched but not critical
AI_CONTROLLER_SUBSYSTEM_DEF(low_priority_ai_controllers)
name = "AI Controller Ticker (Low)"
ss_flags = parent_type::ss_flags | SS_BACKGROUND | SS_NO_INIT
planning_status = AI_STATUS_ON_LOW
priority = FIRE_PRIORITY_NPC_LOW
@@ -1,10 +0,0 @@
AI_CONTROLLER_SUBSYSTEM_DEF(ai_idle_controllers)
name = "AI Idle Controllers"
ss_flags = SS_POST_FIRE_TIMING | SS_BACKGROUND
priority = FIRE_PRIORITY_IDLE_NPC
dependencies = list(
/datum/controller/subsystem/ai_controllers,
)
wait = 5 SECONDS
runlevels = RUNLEVEL_GAME
planning_status = AI_STATUS_IDLE
@@ -129,12 +129,11 @@
owner?.processing_move_loop_flags = flags
var/result = move() //Result is an enum value. Enums defined in __DEFINES/movement.dm
if(result)
EVLOG_PATH(moving, EVLOG_CATEGORY_MOVELOOPS, "Moved using [src]", list(old_loc, moving.loc)) //You might think, this runs a lot; but if not logging, it only does a lookup on the event logger.
if(moving)
var/direction = get_dir(old_loc, moving.loc)
SEND_SIGNAL(moving, COMSIG_MOVABLE_MOVED_FROM_LOOP, src, old_dir, direction)
if(result)
EVLOG_PATH(moving, EVLOG_CATEGORY_MOVELOOPS, "Moved using [src]", list(old_loc, moving.loc)) //You might think, this runs a lot; but if not logging, it only does a lookup on the event logger.
owner?.processing_move_loop_flags = NONE
SEND_SIGNAL(src, COMSIG_MOVELOOP_POSTPROCESS, result, delay * visual_delay)
@@ -438,7 +437,8 @@
/datum/move_loop/has_target/jps/proc/on_finish_pathing(list/path)
movement_path = path
is_pathing = FALSE
EVLOG_PATH(moving, EVLOG_CATEGORY_JPS, "Planned AI path", movement_path)
if(moving)
EVLOG_PATH(moving, EVLOG_CATEGORY_JPS, "Planned AI path", movement_path)
SEND_SIGNAL(src, COMSIG_MOVELOOP_JPS_FINISHED_PATHING, path)
/datum/move_loop/has_target/jps/move()
@@ -1,40 +0,0 @@
/// The subsystem used to tick [/datum/ai_behavior] instances. Handling the individual actions an AI can take like punching someone in the fucking NUTS
PROCESSING_SUBSYSTEM_DEF(ai_behaviors)
name = "AI Behavior Ticker"
ss_flags = SS_POST_FIRE_TIMING|SS_BACKGROUND
priority = FIRE_PRIORITY_NPC_ACTIONS
runlevels = RUNLEVEL_GAME | RUNLEVEL_POSTGAME
dependencies = list(
/datum/controller/subsystem/movement/ai_movement,
)
wait = 1
/// List of all ai_behavior singletons, key is the typepath while assigned value is a newly created instance of the typepath. See setup_ai_behaviors()
var/list/ai_behaviors
/// List of all targeting_strategy singletons, key is the typepath while assigned value is a newly created instance of the typepath. See setup_targeting_strats()
var/list/targeting_strategies
/// List of all target_priority_strategy singletons, key is the typepath while assigned value is a newly created instance of the typepath. See setup_target_priority_strats()
var/list/target_priority_strategies
/datum/controller/subsystem/processing/ai_behaviors/Initialize()
setup_ai_behaviors()
setup_targeting_strats()
setup_target_priority_strats()
return SS_INIT_SUCCESS
/datum/controller/subsystem/processing/ai_behaviors/proc/setup_ai_behaviors()
ai_behaviors = list()
for(var/behavior_type in subtypesof(/datum/ai_behavior))
var/datum/ai_behavior/ai_behavior = new behavior_type
ai_behaviors[behavior_type] = ai_behavior
/datum/controller/subsystem/processing/ai_behaviors/proc/setup_targeting_strats()
targeting_strategies = list()
for(var/target_type in subtypesof(/datum/targeting_strategy))
var/datum/targeting_strategy/target_start = new target_type
targeting_strategies[target_type] = target_start
/datum/controller/subsystem/processing/ai_behaviors/proc/setup_target_priority_strats()
target_priority_strategies = list()
for(var/target_type in subtypesof(/datum/target_priority_strategy))
var/datum/target_priority_strategy/target_start = new target_type
target_priority_strategies[target_type] = target_start
@@ -1,19 +0,0 @@
PROCESSING_SUBSYSTEM_DEF(idle_ai_behaviors)
name = "AI Idle Behaviors"
ss_flags = SS_BACKGROUND
wait = 1.5 SECONDS
priority = FIRE_PRIORITY_IDLE_NPC
dependencies = list(
/datum/controller/subsystem/ai_controllers,
)
///List of all the idle ai behaviors
var/list/idle_behaviors = list()
/datum/controller/subsystem/processing/idle_ai_behaviors/Initialize()
setup_idle_behaviors()
return SS_INIT_SUCCESS
/datum/controller/subsystem/processing/idle_ai_behaviors/proc/setup_idle_behaviors()
for(var/behavior_type in subtypesof(/datum/idle_behavior))
var/datum/idle_behavior/behavior = new behavior_type
idle_behaviors[behavior_type] = behavior
@@ -1,4 +0,0 @@
UNPLANNED_CONTROLLER_SUBSYSTEM_DEF(idle_unplanned_controllers)
name = "Unplanned AI Idle Controllers"
wait = 2.5 SECONDS
target_status = AI_STATUS_IDLE
@@ -1,39 +0,0 @@
GLOBAL_LIST_EMPTY(unplanned_controller_subsystems)
/// Handles making mobs perform lightweight "idle" behaviors such as wandering around when they have nothing planned
SUBSYSTEM_DEF(unplanned_controllers)
name = "Unplanned AI Controllers"
ss_flags = SS_POST_FIRE_TIMING|SS_BACKGROUND
priority = FIRE_PRIORITY_UNPLANNED_NPC
dependencies = list(
/datum/controller/subsystem/movement/ai_movement,
)
wait = 0.25 SECONDS
runlevels = RUNLEVEL_GAME | RUNLEVEL_POSTGAME
///what ai status are we interested in
var/target_status = AI_STATUS_ON
var/list/current_run = list()
/datum/controller/subsystem/unplanned_controllers/Initialize()
..()
GLOB.unplanned_controller_subsystems += src
return SS_INIT_SUCCESS
/datum/controller/subsystem/unplanned_controllers/Destroy()
GLOB.unplanned_controller_subsystems -= src
return ..()
/datum/controller/subsystem/unplanned_controllers/stat_entry(msg)
msg = "\n Planning AIs:[length(GLOB.unplanned_controllers[target_status])]"
return ..()
/datum/controller/subsystem/unplanned_controllers/fire(resumed)
if(!resumed)
src.current_run = GLOB.unplanned_controllers[target_status].Copy()
var/list/current_run = src.current_run // cache for sonic speed
while(length(current_run))
var/datum/ai_controller/unplanned = current_run[current_run.len]
current_run.len--
if(!QDELETED(unplanned))
unplanned.idle_behavior.perform_idle_behavior(wait * 0.1, unplanned)
if (MC_TICK_CHECK)
return