mirror of
https://github.com/Bubberstation/Bubberstation.git
synced 2026-08-23 21:18:37 +01:00
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>
327 lines
12 KiB
Plaintext
327 lines
12 KiB
Plaintext
// None of these are really complex enough to merit their own file
|
|
|
|
/**
|
|
* # Pet Command: Idle
|
|
* Tells a pet to resume its idle behaviour, usually staying put where you leave it
|
|
*/
|
|
/datum/pet_command/idle
|
|
command_name = "Stay"
|
|
command_desc = "Command your pet to stay idle in this location."
|
|
radial_icon_state = "halt"
|
|
speech_commands = list("sit", "stay", "stop")
|
|
command_feedback = "sits"
|
|
|
|
/datum/pet_command/idle/execute_action(datum/ai_controller/controller)
|
|
controller.set_behavior_tree_override(SUBPLAN_ID_PET_COMMAND, /datum/bt_node/subtree/pet_command/stay)
|
|
|
|
/datum/pet_command/idle/retrieve_command_text(atom/living_pet, atom/target)
|
|
return "signals [living_pet] to stay idle!"
|
|
|
|
/**
|
|
* # Pet Command: Stop
|
|
* Tells a pet to exit command mode and resume its normal behaviour
|
|
*/
|
|
/datum/pet_command/free
|
|
command_name = "Loose"
|
|
command_desc = "Allow your pet to resume its natural behaviours."
|
|
radial_icon_state = "free"
|
|
speech_commands = list("free", "loose")
|
|
command_feedback = "relaxes"
|
|
|
|
/datum/pet_command/free/execute_action(datum/ai_controller/controller)
|
|
controller.set_behavior_tree_override(SUBPLAN_ID_PET_COMMAND, null)
|
|
controller.clear_blackboard_key(BB_ACTIVE_PET_COMMAND)
|
|
|
|
/datum/pet_command/free/retrieve_command_text(atom/living_pet, atom/target)
|
|
return "signals [living_pet] to go free!"
|
|
|
|
/**
|
|
* # Pet Command: Follow
|
|
* Tells a pet to follow you until you tell it to do something else
|
|
*/
|
|
/datum/pet_command/follow
|
|
command_name = "Follow"
|
|
command_desc = "Command your pet to accompany you."
|
|
radial_icon_state = "follow"
|
|
speech_commands = list("heel", "follow")
|
|
callout_type = /datum/callout_option/move
|
|
///should we activate immediately if we're doing nothing else and gain a friend?
|
|
var/activate_on_befriend = FALSE
|
|
/// BT subtree installed by execute_action; override to use a mob-specific follow tree.
|
|
var/follow_subtree = /datum/bt_node/subtree/pet_command/follow
|
|
|
|
/datum/pet_command/follow/set_command_active(mob/living/parent, mob/living/commander)
|
|
. = ..()
|
|
set_command_target(parent, commander)
|
|
|
|
/datum/pet_command/follow/retrieve_command_text(atom/living_pet, atom/target)
|
|
return "signals [living_pet] to follow!"
|
|
|
|
/datum/pet_command/follow/execute_action(datum/ai_controller/controller)
|
|
controller.set_behavior_tree_override(SUBPLAN_ID_PET_COMMAND, follow_subtree)
|
|
|
|
/datum/pet_command/follow/add_new_friend(mob/living/tamer)
|
|
. = ..()
|
|
var/mob/living/parent = weak_parent.resolve()
|
|
if (!parent)
|
|
return
|
|
if (activate_on_befriend && !parent.ai_controller.blackboard_key_exists(BB_ACTIVE_PET_COMMAND))
|
|
try_activate_command(tamer)
|
|
|
|
/// Like follow but start active
|
|
/datum/pet_command/follow/start_active
|
|
activate_on_befriend = TRUE
|
|
|
|
/**
|
|
* # Pet Command: Play Dead
|
|
* Pretend to be dead for a random period of time
|
|
*/
|
|
/datum/pet_command/play_dead
|
|
command_name = "Play Dead"
|
|
command_desc = "Play a macabre trick."
|
|
radial_icon_state = "play_dead"
|
|
speech_commands = list("play dead") // Don't get too creative here, people talk about dying pretty often
|
|
|
|
/datum/pet_command/play_dead/execute_action(datum/ai_controller/controller)
|
|
controller.set_behavior_tree_override(SUBPLAN_ID_PET_COMMAND, /datum/bt_node/subtree/pet_command/play_dead)
|
|
|
|
/datum/pet_command/play_dead/retrieve_command_text(atom/living_pet, atom/target)
|
|
return "signals [living_pet] to play dead!"
|
|
|
|
/**
|
|
* # Pet Command: Good Boy
|
|
* React if complimented
|
|
*/
|
|
/datum/pet_command/good_boy
|
|
command_name = "Good Boy"
|
|
command_desc = "Give your pet a compliment."
|
|
hidden = TRUE
|
|
|
|
/datum/pet_command/good_boy/New(mob/living/parent)
|
|
. = ..()
|
|
speech_commands += "good [parent.name]"
|
|
switch (parent.gender)
|
|
if (MALE)
|
|
speech_commands += "good boy"
|
|
return
|
|
if (FEMALE)
|
|
speech_commands += "good girl"
|
|
return
|
|
// If we get past this point someone has finally added a non-binary dog
|
|
|
|
/datum/pet_command/good_boy/execute_action(datum/ai_controller/controller)
|
|
controller.set_behavior_tree_override(SUBPLAN_ID_PET_COMMAND, null)
|
|
controller.clear_blackboard_key(BB_ACTIVE_PET_COMMAND)
|
|
var/mob/living/parent = weak_parent.resolve()
|
|
if (!parent)
|
|
return
|
|
new /obj/effect/temp_visual/heart(parent.loc)
|
|
parent.emote("spin")
|
|
|
|
/**
|
|
* # Pet Command: Use ability
|
|
* Use an an ability that does not require any targets
|
|
*/
|
|
/datum/pet_command/untargeted_ability
|
|
///untargeted ability we will use
|
|
var/ability_key
|
|
|
|
/datum/pet_command/untargeted_ability/execute_action(datum/ai_controller/controller)
|
|
var/datum/action/cooldown/ability = controller.blackboard[ability_key]
|
|
if(!ability?.IsAvailable())
|
|
return
|
|
controller.set_blackboard_key(BB_PET_ACTIVE_ABILITY, ability)
|
|
controller.set_behavior_tree_override(SUBPLAN_ID_PET_COMMAND, /datum/bt_node/subtree/pet_command/untargeted_ability)
|
|
controller.clear_blackboard_key(BB_ACTIVE_PET_COMMAND)
|
|
|
|
/datum/pet_command/untargeted_ability/retrieve_command_text(atom/living_pet, atom/target)
|
|
return "signals [living_pet] to use an ability!"
|
|
|
|
/**
|
|
* # Pet Command: Attack
|
|
* Tells a pet to chase and bite the next thing you point at
|
|
*/
|
|
/datum/pet_command/attack
|
|
command_name = "Attack"
|
|
command_desc = "Command your pet to attack things that you point out to it."
|
|
radial_icon_state = "attack"
|
|
requires_pointing = TRUE
|
|
callout_type = /datum/callout_option/attack
|
|
speech_commands = list("attack", "sic", "kill")
|
|
command_feedback = "growl"
|
|
pointed_reaction = "and growls"
|
|
/// Balloon alert to display if providing an invalid target
|
|
var/refuse_reaction = "shakes head"
|
|
var/datum/bt_node/subtree/attack_subtree = /datum/bt_node/subtree/pet_command/attack
|
|
|
|
// Refuse to target things we can't target, chiefly other friends
|
|
/datum/pet_command/attack/set_command_target(mob/living/parent, atom/target)
|
|
if (!target)
|
|
return FALSE
|
|
var/mob/living/living_parent = parent
|
|
if (!living_parent.ai_controller)
|
|
return FALSE
|
|
var/datum/targeting_strategy/targeter = GET_TARGETING_STRATEGY(living_parent.ai_controller.blackboard[targeting_strategy_key])
|
|
if (!targeter)
|
|
return FALSE
|
|
if (!targeter.is_valid_target(living_parent, target))
|
|
refuse_target(parent, target)
|
|
return FALSE
|
|
return ..()
|
|
|
|
/datum/pet_command/attack/retrieve_command_text(atom/living_pet, atom/target)
|
|
return isnull(target) ? null : "signals [living_pet] to attack [target]!"
|
|
|
|
/// Display feedback about not targeting something
|
|
/datum/pet_command/attack/proc/refuse_target(mob/living/parent, atom/target)
|
|
var/mob/living/living_parent = parent
|
|
living_parent.balloon_alert_to_viewers("[refuse_reaction]")
|
|
living_parent.visible_message(span_notice("[living_parent] refuses to attack [target]."))
|
|
|
|
/datum/pet_command/attack/execute_action(datum/ai_controller/controller)
|
|
controller.set_behavior_tree_override(SUBPLAN_ID_PET_COMMAND, attack_subtree)
|
|
|
|
/**
|
|
* # Breed command. breed with a partner!
|
|
*/
|
|
/datum/pet_command/breed
|
|
command_name = "Breed"
|
|
command_desc = "Command your pet to attempt to breed with a partner."
|
|
requires_pointing = TRUE
|
|
radial_icon_state = "breed"
|
|
speech_commands = list("breed", "consummate")
|
|
|
|
/datum/pet_command/breed/set_command_target(mob/living/parent, atom/target)
|
|
if(isnull(target) || !isliving(target))
|
|
return FALSE
|
|
if(!HAS_TRAIT(parent, TRAIT_MOB_BREEDER) || !HAS_TRAIT(target, TRAIT_MOB_BREEDER))
|
|
return FALSE
|
|
if(isnull(parent.ai_controller))
|
|
return FALSE
|
|
if(!parent.ai_controller.blackboard[BB_BREED_READY] || isnull(parent.ai_controller.blackboard[BB_BABIES_PARTNER_TYPES]))
|
|
return FALSE
|
|
var/mob/living/living_target = target
|
|
if(!living_target.ai_controller?.blackboard[BB_BREED_READY])
|
|
return FALSE
|
|
return ..()
|
|
|
|
/datum/pet_command/breed/execute_action(datum/ai_controller/controller)
|
|
controller.set_behavior_tree_override(SUBPLAN_ID_PET_COMMAND, /datum/bt_node/subtree/pet_command/breed)
|
|
|
|
/datum/pet_command/breed/retrieve_command_text(atom/living_pet, atom/target)
|
|
return isnull(target) ? null : "signals [living_pet] to breed with [target]!"
|
|
|
|
/**
|
|
* # Pet Command: Targetted Ability
|
|
* Tells a pet to use some kind of ability on the next thing you point at
|
|
*/
|
|
/datum/pet_command/use_ability
|
|
command_name = "Use ability"
|
|
command_desc = "Command your pet to use one of its special skills on something that you point out to it."
|
|
radial_icon = 'icons/mob/actions/actions_spells.dmi'
|
|
radial_icon_state = "projectile"
|
|
requires_pointing = TRUE
|
|
speech_commands = list("shoot", "blast", "cast")
|
|
command_feedback = "growl"
|
|
pointed_reaction = "and growls"
|
|
/// Blackboard key where a reference to some kind of mob ability is stored
|
|
var/pet_ability_key
|
|
|
|
/datum/pet_command/use_ability/execute_action(datum/ai_controller/controller)
|
|
if (!pet_ability_key)
|
|
return
|
|
var/datum/action/cooldown/using_action = controller.blackboard[pet_ability_key]
|
|
if (QDELETED(using_action))
|
|
return
|
|
// Store the action datum in a fixed key so the targeted_ability subtree can find it
|
|
controller.override_blackboard_key(BB_TARGETED_ACTION, using_action)
|
|
controller.set_behavior_tree_override(SUBPLAN_ID_PET_COMMAND, /datum/bt_node/subtree/pet_command/targeted_ability)
|
|
|
|
/datum/pet_command/use_ability/retrieve_command_text(atom/living_pet, atom/target)
|
|
return isnull(target) ? null : "signals [living_pet] to use an ability on [target]!"
|
|
|
|
/datum/pet_command/protect_owner
|
|
command_name = "Protect owner"
|
|
command_desc = "Your pet will run to your aid."
|
|
hidden = TRUE
|
|
callout_type = /datum/callout_option/guard
|
|
///the range our owner needs to be in for us to protect him
|
|
var/protect_range = 9
|
|
///message cooldown to prevent too many people from telling you not to commit suicide
|
|
COOLDOWN_DECLARE(self_harm_message_cooldown)
|
|
var/datum/bt_node/subtree/protect_owner_subtree = /datum/bt_node/subtree/pet_command/protect_owner
|
|
|
|
/datum/pet_command/protect_owner/add_new_friend(mob/living/tamer)
|
|
RegisterSignal(tamer, COMSIG_ATOM_WAS_ATTACKED, PROC_REF(set_attacking_target))
|
|
if(!HAS_TRAIT(tamer, TRAIT_RELAYING_ATTACKER))
|
|
tamer.AddElement(/datum/element/relay_attackers)
|
|
|
|
/datum/pet_command/protect_owner/remove_friend(mob/living/unfriended)
|
|
UnregisterSignal(unfriended, COMSIG_ATOM_WAS_ATTACKED)
|
|
|
|
/datum/pet_command/protect_owner/execute_action(datum/ai_controller/controller)
|
|
controller.set_behavior_tree_override(SUBPLAN_ID_PET_COMMAND, protect_owner_subtree)
|
|
|
|
/datum/pet_command/protect_owner/set_command_active(mob/living/parent, mob/living/victim)
|
|
. = ..()
|
|
set_command_target(parent, victim)
|
|
|
|
/datum/pet_command/protect_owner/valid_callout_target(mob/living/speaker, datum/callout_option/callout, atom/target)
|
|
return target == speaker || get_dist(speaker, target) <= 1
|
|
|
|
/datum/pet_command/protect_owner/proc/set_attacking_target(atom/source, mob/living/attacker)
|
|
SIGNAL_HANDLER
|
|
|
|
var/mob/living/basic/owner = weak_parent.resolve()
|
|
if(isnull(owner))
|
|
return
|
|
if(source == attacker)
|
|
var/list/interventions = owner.ai_controller?.blackboard[BB_OWNER_SELF_HARM_RESPONSES] || list()
|
|
if (length(interventions) && COOLDOWN_FINISHED(src, self_harm_message_cooldown) && prob(30))
|
|
COOLDOWN_START(src, self_harm_message_cooldown, 5 SECONDS)
|
|
var/chosen_statement = pick(interventions)
|
|
INVOKE_ASYNC(owner, TYPE_PROC_REF(/atom/movable, say), chosen_statement)
|
|
return
|
|
var/mob/living/current_target = owner.ai_controller?.blackboard[BB_CURRENT_PET_TARGET]
|
|
if(attacker == current_target) //we are already dealing with this target
|
|
return
|
|
if(isliving(attacker) && can_see(owner, attacker, protect_range))
|
|
set_command_active(owner, attacker)
|
|
|
|
/**
|
|
* # Fish command: command the mob to fish at the next fishing spot you point at.
|
|
*/
|
|
/datum/pet_command/fish
|
|
command_name = "Fish"
|
|
command_desc = "Command your pet to try fishing at a nearby fishing spot."
|
|
requires_pointing = TRUE
|
|
radial_icon_state = "fish"
|
|
speech_commands = list("fish")
|
|
|
|
/datum/pet_command/fish/execute_action(datum/ai_controller/controller)
|
|
if(!controller.blackboard_key_exists(BB_CURRENT_PET_TARGET))
|
|
controller.set_behavior_tree_override(SUBPLAN_ID_PET_COMMAND, /datum/bt_node/subtree/pet_command/stay)
|
|
return
|
|
controller.set_behavior_tree_override(SUBPLAN_ID_PET_COMMAND, /datum/bt_node/subtree/pet_command/fish)
|
|
|
|
/datum/pet_command/fish/retrieve_command_text(atom/living_pet, atom/target)
|
|
return "signals [living_pet] to go fish!"
|
|
|
|
/datum/pet_command/move
|
|
command_name = "Move"
|
|
command_desc = "Command your pet to move to a location!"
|
|
requires_pointing = TRUE
|
|
radial_icon_state = "move"
|
|
speech_commands = list("move", "walk")
|
|
|
|
/datum/pet_command/move/set_command_target(mob/living/parent, atom/target)
|
|
if(isnull(target) || !can_see(parent, target, 9))
|
|
return FALSE
|
|
return ..()
|
|
|
|
/datum/pet_command/move/execute_action(datum/ai_controller/controller)
|
|
controller.set_behavior_tree_override(SUBPLAN_ID_PET_COMMAND, /datum/bt_node/subtree/pet_command/move_to)
|
|
|
|
/datum/pet_command/move/retrieve_command_text(atom/living_pet, atom/target)
|
|
return "signals [living_pet] to move!"
|