mirror of
https://github.com/Bubberstation/Bubberstation.git
synced 2026-08-24 05:30:05 +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
@@ -0,0 +1,64 @@
|
||||
{
|
||||
"dm_type": "/datum/ai_controller/vending_machine",
|
||||
"type": "selector",
|
||||
"children": [
|
||||
{
|
||||
"type": "decorator",
|
||||
"decorator": "/datum/bt_node/decorator/vending_is_tilted",
|
||||
"child": {
|
||||
"type": "decorator",
|
||||
"decorator": "/datum/bt_node/decorator/key_off_cooldown",
|
||||
"vars": {
|
||||
"cooldown_key": "BB_VENDING_UNTILT_COOLDOWN"
|
||||
},
|
||||
"child": {
|
||||
"type": "leaf",
|
||||
"behavior": "/datum/bt_node/ai_behavior/vendor_rise_up"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "decorator",
|
||||
"decorator": "/datum/bt_node/decorator/vending_is_tilted",
|
||||
"vars": {
|
||||
"invert": true
|
||||
},
|
||||
"child": {
|
||||
"type": "decorator",
|
||||
"decorator": "/datum/bt_node/decorator/key_off_cooldown",
|
||||
"vars": {
|
||||
"cooldown_key": "BB_VENDING_TILT_COOLDOWN"
|
||||
},
|
||||
"child": {
|
||||
"type": "sequence",
|
||||
"children": [
|
||||
{
|
||||
"type": "leaf",
|
||||
"behavior": "/datum/bt_node/ai_behavior/find_vendor_target",
|
||||
"vars": {
|
||||
"target_key": "BB_VENDING_CURRENT_TARGET",
|
||||
"vision_range": 7
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "leaf",
|
||||
"behavior": "/datum/bt_node/ai_behavior/move_to_target",
|
||||
"vars": {
|
||||
"target_key": "BB_VENDING_CURRENT_TARGET",
|
||||
"required_dist": 1,
|
||||
"finish_on_arrival": true
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "leaf",
|
||||
"behavior": "/datum/bt_node/ai_behavior/vendor_crush",
|
||||
"vars": {
|
||||
"target_key": "BB_VENDING_CURRENT_TARGET"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -1,28 +1,51 @@
|
||||
/datum/ai_behavior/vendor_crush
|
||||
behavior_flags = AI_BEHAVIOR_REQUIRE_MOVEMENT
|
||||
///Time before machine can untilt itself after tilting
|
||||
var/untilt_cooldown = 1 SECONDS
|
||||
///Time to telegraph and tilt over
|
||||
/// Returns TRUE if the vending machine pawn is currently tilted.
|
||||
/datum/bt_node/decorator/vending_is_tilted/check_condition(datum/ai_controller/controller)
|
||||
var/obj/machinery/vending/vendor_pawn = controller.pawn
|
||||
return vendor_pawn.tilted
|
||||
|
||||
/// Searches nearby tiles for a valid living target and sets the given BB key. Sets tilt cooldown and fails if none found.
|
||||
/datum/bt_node/ai_behavior/find_vendor_target
|
||||
var/target_key
|
||||
var/vision_range
|
||||
/// Cooldown applied to BB_VENDING_TILT_COOLDOWN when no valid target is in range
|
||||
var/search_cooldown = 2 SECONDS
|
||||
|
||||
/datum/bt_node/ai_behavior/find_vendor_target/perform(seconds_per_tick, datum/ai_controller/controller)
|
||||
for(var/mob/living/living_target in oview(vision_range, controller.pawn))
|
||||
if(living_target.stat || living_target.incorporeal_move)
|
||||
continue
|
||||
controller.set_blackboard_key(target_key, living_target)
|
||||
return AI_BEHAVIOR_DELAY | AI_BEHAVIOR_SUCCEEDED
|
||||
controller.clear_blackboard_key(target_key)
|
||||
controller.set_blackboard_key(BB_VENDING_TILT_COOLDOWN, world.time + search_cooldown)
|
||||
return AI_BEHAVIOR_DELAY | AI_BEHAVIOR_FAILED
|
||||
|
||||
/// Telegraphs and tilts onto the target. Returns success once the machine is tilted.
|
||||
/datum/bt_node/ai_behavior/vendor_crush
|
||||
var/target_key
|
||||
/// Time to telegraph before tilting
|
||||
var/time_to_tilt = 0.8 SECONDS
|
||||
/// Time before machine can untilt after a tilt attempt
|
||||
var/untilt_cooldown = 1 SECONDS
|
||||
|
||||
/datum/ai_behavior/vendor_crush/setup(datum/ai_controller/controller, target_key)
|
||||
. = ..()
|
||||
set_movement_target(controller, controller.blackboard[target_key])
|
||||
|
||||
/datum/ai_behavior/vendor_crush/perform(seconds_per_tick, datum/ai_controller/controller)
|
||||
/datum/bt_node/ai_behavior/vendor_crush/perform(seconds_per_tick, datum/ai_controller/controller)
|
||||
var/obj/machinery/vending/vendor_pawn = controller.pawn
|
||||
if(vendor_pawn.tilted)
|
||||
return AI_BEHAVIOR_DELAY | AI_BEHAVIOR_SUCCEEDED
|
||||
if(controller.blackboard[BB_VENDING_BUSY_TILTING])
|
||||
return AI_BEHAVIOR_DELAY
|
||||
|
||||
controller.ai_movement.stop_moving_towards(controller)
|
||||
controller.set_blackboard_key(BB_VENDING_BUSY_TILTING, TRUE)
|
||||
var/turf/target_turf = get_turf(controller.blackboard[BB_VENDING_CURRENT_TARGET])
|
||||
var/turf/target_turf = get_turf(controller.blackboard[target_key])
|
||||
new /obj/effect/temp_visual/telegraphing/vending_machine_tilt(target_turf)
|
||||
addtimer(CALLBACK(src, PROC_REF(tiltonmob), controller, target_turf), time_to_tilt)
|
||||
return AI_BEHAVIOR_DELAY
|
||||
|
||||
/datum/ai_behavior/vendor_crush/proc/tiltonmob(datum/ai_controller/controller, turf/target_turf)
|
||||
/datum/bt_node/ai_behavior/vendor_crush/proc/tiltonmob(datum/ai_controller/controller, turf/target_turf)
|
||||
if(QDELETED(controller) || QDELETED(controller.pawn))
|
||||
return
|
||||
var/obj/machinery/vending/vendor_pawn = controller.pawn
|
||||
if(vendor_pawn.tilt(target_turf, 0) & SUCCESSFULLY_CRUSHED_MOB) //We hit something
|
||||
if(vendor_pawn.tilt(target_turf, 0) & SUCCESSFULLY_CRUSHED_MOB)
|
||||
vendor_pawn.say(pick("Supersize this!", "Eat my shiny metal ass!", "Want to consume some of my products?", "SMASH!", "Don't you love these smashing prices!"))
|
||||
controller.set_blackboard_key(BB_VENDING_LAST_HIT_SUCCESSFUL, TRUE)
|
||||
else
|
||||
@@ -30,21 +53,22 @@
|
||||
flick(vendor_pawn.icon_deny, vendor_pawn)
|
||||
vendor_pawn.say(pick("Get back here!", "Don't you want my well priced love?"))
|
||||
controller.set_blackboard_key(BB_VENDING_LAST_HIT_SUCCESSFUL, FALSE)
|
||||
finish_action(controller, TRUE)
|
||||
controller.set_blackboard_key(BB_VENDING_UNTILT_COOLDOWN, world.time + untilt_cooldown)
|
||||
controller.set_blackboard_key(BB_VENDING_BUSY_TILTING, FALSE)
|
||||
|
||||
/datum/ai_behavior/vendor_crush/finish_action(datum/ai_controller/controller, succeeded)
|
||||
/datum/bt_node/ai_behavior/vendor_crush/finish_action(datum/ai_controller/controller, succeeded)
|
||||
. = ..()
|
||||
controller.set_blackboard_key(BB_VENDING_BUSY_TILTING, FALSE)
|
||||
controller.set_blackboard_key(BB_VENDING_UNTILT_COOLDOWN, world.time + untilt_cooldown)
|
||||
|
||||
/datum/ai_behavior/vendor_rise_up //what a gamer
|
||||
///Time before machine can tilt again after untilting if last hit was a success
|
||||
var/succes_tilt_cooldown = 5 SECONDS
|
||||
/// Untilts the machine. Sets a tilt cooldown if the previous hit was successful.
|
||||
/datum/bt_node/ai_behavior/vendor_rise_up
|
||||
/// Time before machine can tilt again after untilting if the last hit landed
|
||||
var/success_tilt_cooldown = 5 SECONDS
|
||||
|
||||
/datum/ai_behavior/vendor_rise_up/perform(seconds_per_tick, datum/ai_controller/controller)
|
||||
/datum/bt_node/ai_behavior/vendor_rise_up/perform(seconds_per_tick, datum/ai_controller/controller)
|
||||
var/obj/machinery/vending/vendor_pawn = controller.pawn
|
||||
vendor_pawn.visible_message(span_warning("[vendor_pawn] untilts itself!"))
|
||||
if(controller.blackboard[BB_VENDING_LAST_HIT_SUCCESSFUL])
|
||||
controller.set_blackboard_key(BB_VENDING_TILT_COOLDOWN, world.time + succes_tilt_cooldown)
|
||||
controller.set_blackboard_key(BB_VENDING_TILT_COOLDOWN, world.time + success_tilt_cooldown)
|
||||
vendor_pawn.untilt()
|
||||
return AI_BEHAVIOR_DELAY | AI_BEHAVIOR_SUCCEEDED
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
///AI controller for vending machine gone rogue, Don't try using this on anything else, it wont work.
|
||||
/datum/ai_controller/vending_machine
|
||||
movement_delay = 0.4 SECONDS
|
||||
behavior_tree_json = "code/datums/ai/objects/vending_machines/vending_machine.bt.json"
|
||||
blackboard = list(
|
||||
BB_VENDING_CURRENT_TARGET = null,
|
||||
BB_VENDING_TILT_COOLDOWN = 0,
|
||||
@@ -10,10 +11,6 @@
|
||||
)
|
||||
/// If TRUE stops mobs from buying things from active machines
|
||||
var/block_usage = FALSE
|
||||
/// Range to search for mobs to crunch
|
||||
var/vision_range = 7
|
||||
/// Seconds between attempts to find a new mob to crunch
|
||||
var/search_for_enemy_cooldown = 2 SECONDS
|
||||
|
||||
/datum/ai_controller/vending_machine/TryPossessPawn(atom/new_pawn)
|
||||
if(!istype(new_pawn, /obj/machinery/vending))
|
||||
@@ -35,26 +32,6 @@
|
||||
UnregisterSignal(vendor_pawn, COMSIG_VENDING_UI_INTERACT)
|
||||
return ..() //Run parent at end
|
||||
|
||||
/datum/ai_controller/vending_machine/SelectBehaviors(seconds_per_tick)
|
||||
current_behaviors = list()
|
||||
var/obj/machinery/vending/vendor_pawn = pawn
|
||||
|
||||
if(vendor_pawn.tilted) //We're tilted, try to untilt
|
||||
if(blackboard[BB_VENDING_UNTILT_COOLDOWN] > world.time)
|
||||
return
|
||||
queue_behavior(/datum/ai_behavior/vendor_rise_up)
|
||||
return
|
||||
else //Not tilted, try to find target to tilt onto.
|
||||
if(blackboard[BB_VENDING_TILT_COOLDOWN] > world.time)
|
||||
return
|
||||
for(var/mob/living/living_target in oview(vision_range, pawn))
|
||||
if(living_target.stat || living_target.incorporeal_move) //They're already fucked up or incorporeal
|
||||
continue
|
||||
set_blackboard_key(BB_VENDING_CURRENT_TARGET, living_target)
|
||||
queue_behavior(/datum/ai_behavior/vendor_crush, BB_VENDING_CURRENT_TARGET)
|
||||
return
|
||||
set_blackboard_key(BB_VENDING_TILT_COOLDOWN, world.time + search_for_enemy_cooldown)
|
||||
|
||||
/datum/ai_controller/vending_machine/proc/deny_vending_interact(obj/machinery/vending/vending_machine, mob/user, datum/tgui/ui)
|
||||
SIGNAL_HANDLER
|
||||
if(!block_usage)
|
||||
|
||||
Reference in New Issue
Block a user