mirror of
https://github.com/Bubberstation/Bubberstation.git
synced 2026-08-22 12:41:09 +01:00
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:
committed by
The Sharkenning
co-authored by
Iamgoofball
SmArtKar
Ghom
Ben10Omintrix
SyncIt21
parent
ad0d6a3e7d
commit
df7832aa43
@@ -179,7 +179,6 @@
|
||||
#include "embedding.dm"
|
||||
#include "emoting.dm"
|
||||
#include "emp_flashlight.dm"
|
||||
#include "ensure_subtree_operational_datum.dm"
|
||||
#include "ethereal_revival.dm"
|
||||
#include "explosion_action.dm"
|
||||
#include "firedoor_regions.dm"
|
||||
|
||||
@@ -1,64 +0,0 @@
|
||||
/// The subtree that requires the operational datum.
|
||||
#define REQUIRED_SUBTREE "required_subtree"
|
||||
/// The list of typepaths of applicable operational datums that would satisfy the requirement.
|
||||
#define REQUIRED_OPERATIONAL_DATUMS "required_operational_datums"
|
||||
|
||||
/// Unit Test that ensure that if we add a specific planning subtree to a basic mob's planning tree, that we also have the operational datum needed for it (component/element).
|
||||
/// This can be extended to other "mandatory" operational datums for certain subtrees to work.
|
||||
/datum/unit_test/ensure_subtree_operational_datum
|
||||
/// Associated list of mobs that we need to test this on. Key is the typepath of the mob, value is a list of the planning subtree and the operational datums that are required for it.
|
||||
var/list/testable_mobs = list()
|
||||
|
||||
/datum/unit_test/ensure_subtree_operational_datum/Run()
|
||||
gather_testable_mobs()
|
||||
test_applicable_mobs()
|
||||
|
||||
/// First, look for all mobs that have a planning subtree that requires an element, then add it to the list for stuff to test afterwards. Done like this to not have one mumbo proc that's hard to read.
|
||||
/datum/unit_test/ensure_subtree_operational_datum/proc/gather_testable_mobs()
|
||||
for(var/mob/living/basic/checkable_mob as anything in subtypesof(/mob/living/basic))
|
||||
var/datum/ai_controller/testable_controller = initial(checkable_mob.ai_controller)
|
||||
if(isnull(testable_controller))
|
||||
continue
|
||||
|
||||
// we can't do inital() memes on lists so it's allocation time
|
||||
testable_controller = allocate(testable_controller)
|
||||
var/list/ai_planning_subtrees = testable_controller.planning_subtrees // list of instantiated datums. easy money
|
||||
if(!length(ai_planning_subtrees))
|
||||
continue
|
||||
|
||||
for(var/datum/ai_planning_subtree/testable_subtree as anything in ai_planning_subtrees)
|
||||
var/list/necessary_datums = testable_subtree.operational_datums
|
||||
if(isnull(necessary_datums))
|
||||
continue
|
||||
|
||||
testable_mobs[checkable_mob] = list(
|
||||
REQUIRED_OPERATIONAL_DATUMS = necessary_datums,
|
||||
REQUIRED_SUBTREE = testable_subtree.type,
|
||||
)
|
||||
|
||||
/// Then, test the mobs that we've found
|
||||
/datum/unit_test/ensure_subtree_operational_datum/proc/test_applicable_mobs()
|
||||
for(var/mob/living/basic/checkable_mob as anything in testable_mobs)
|
||||
var/list/checkable_mob_data = testable_mobs[checkable_mob]
|
||||
checkable_mob = allocate(checkable_mob)
|
||||
|
||||
var/datum/ai_planning_subtree/test_subtree = checkable_mob_data[REQUIRED_SUBTREE]
|
||||
var/list/trait_sources = GET_TRAIT_SOURCES(checkable_mob, TRAIT_SUBTREE_REQUIRED_OPERATIONAL_DATUM)
|
||||
if(!length(trait_sources)) // yes yes we could use `COUNT_TRAIT_SOURCES` but why invoke the same macro twice
|
||||
TEST_FAIL("The mob [checkable_mob] ([checkable_mob.type]) does not have ANY instances of TRAIT_SUBTREE_REQUIRED_OPERATIONAL_DATUM, but has a planning subtree ([test_subtree]) that requires it!")
|
||||
continue
|
||||
|
||||
var/has_element = FALSE
|
||||
var/list/testable_operational_datums = checkable_mob_data[REQUIRED_OPERATIONAL_DATUMS]
|
||||
for(var/iterable in trait_sources)
|
||||
if(iterable in testable_operational_datums)
|
||||
has_element = TRUE
|
||||
break
|
||||
|
||||
if(!has_element)
|
||||
var/list/message_list = list("The mob [checkable_mob] ([checkable_mob.type]) has a planning subtree ([test_subtree]) that requires a component/element, but does not have any!")
|
||||
message_list += "Needs one of the following to satisfy the requirement: ([testable_operational_datums.Join(", ")])"
|
||||
TEST_FAIL(message_list.Join(" "))
|
||||
|
||||
#undef REQUIRED_SUBTREE
|
||||
#undef REQUIRED_OPERATIONAL_DATUMS
|
||||
@@ -20,21 +20,19 @@
|
||||
|
||||
// Ai controlling processes expect a seconds_per_tick, supply a real-fake dt
|
||||
var/fake_dt = SSai_controllers.wait * 0.1
|
||||
// Set AI - AIs by default are off in z-levels with no client, we have to force it on.
|
||||
// Force this fker to be on
|
||||
biter.ai_controller.ai_traits |= RUN_WHILE_UNWATCHED
|
||||
biter.ai_controller.set_ai_status(AI_STATUS_ON)
|
||||
biter.ai_controller.can_idle = FALSE
|
||||
// Select behavior - this will queue finding the cable
|
||||
biter.ai_controller.SelectBehaviors(fake_dt)
|
||||
// Process behavior - this will execute the "locate the cable" behavior
|
||||
biter.ai_controller.process(fake_dt)
|
||||
// Check that the cable was found
|
||||
TEST_ASSERT(biter.ai_controller.blackboard[BB_LOW_PRIORITY_HUNTING_TARGET] == wire, "Mouse, after executing find, did not set the cable as a target.")
|
||||
// Select behavior - this will queue hunting
|
||||
biter.ai_controller.SelectBehaviors(fake_dt)
|
||||
// Process behavior - this will execute the hunt for the cable and cause a bite (as we're in the min range)
|
||||
biter.ai_controller.process(fake_dt)
|
||||
// Check that the cable was removed, as it was hunted correctly
|
||||
TEST_ASSERT_NULL(biter.ai_controller.blackboard[BB_LOW_PRIORITY_HUNTING_TARGET], "Mouse, after executing hunt, did not clear their target blackboard.")
|
||||
|
||||
// Mouse eating is chance-based, so we set the hunting target directly, yes, this messed with the test from what it was before, but I'm not sure how to do it better :(
|
||||
biter.ai_controller.set_blackboard_key(BB_LOW_PRIORITY_HUNTING_TARGET, wire)
|
||||
|
||||
// Tick the tree until the hunt branch moves onto and bites the cable. Can we do this better in unit tests?? idk..
|
||||
for(var/i in 1 to 5)
|
||||
if(QDELETED(biter))
|
||||
break
|
||||
biter.ai_controller.SelectBehaviors(fake_dt)
|
||||
biter.ai_controller.process(fake_dt)
|
||||
|
||||
// Now check that the bite went through - remember we qdel mice on death
|
||||
TEST_ASSERT(QDELETED(biter), "Mouse, did not die after biting a powered cable.")
|
||||
@@ -47,12 +45,3 @@
|
||||
/// Dummy mouse that is guaranteed to die when biting shocked cables.
|
||||
/mob/living/basic/mouse/cable_lover
|
||||
cable_zap_prob = 100
|
||||
ai_controller = /datum/ai_controller/basic_controller/mouse/guaranteed_to_bite
|
||||
|
||||
/// Dummy mouse's ai controller that is guaranteed to find and bite a cable beneath it
|
||||
/datum/ai_controller/basic_controller/mouse/guaranteed_to_bite
|
||||
planning_subtrees = list(/datum/ai_planning_subtree/find_and_hunt_target/look_for_cables/guaranteed)
|
||||
|
||||
/// Cable hunting subtree that's guarantee to hunt its target.
|
||||
/datum/ai_planning_subtree/find_and_hunt_target/look_for_cables/guaranteed
|
||||
hunt_chance = 100
|
||||
|
||||
Reference in New Issue
Block a user