mirror of
https://github.com/Bubberstation/Bubberstation.git
synced 2026-08-23 05:00:55 +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>
261 lines
11 KiB
Plaintext
261 lines
11 KiB
Plaintext
/datum/ai_controller/basic_controller/minebot
|
|
behavior_tree_json = "code/modules/mob/living/basic/minebots/minebot.bt.json"
|
|
blackboard = list(
|
|
BB_TARGETING_STRATEGY = /datum/targeting_strategy/basic,
|
|
BB_PET_TARGETING_STRATEGY = /datum/targeting_strategy/basic/not_friends,
|
|
BB_BASIC_MOB_FLEE_DISTANCE = 3,
|
|
BB_MINIMUM_SHOOTING_DISTANCE = 3,
|
|
BB_MINEBOT_PLANT_MINES = TRUE,
|
|
BB_MINEBOT_REPAIR_DRONE = TRUE,
|
|
BB_MINEBOT_AUTO_DEFEND = TRUE,
|
|
BB_BLACKLIST_MINERAL_TURFS = list(/turf/closed/mineral/gibtonite),
|
|
BB_AUTOMATED_MINING = FALSE,
|
|
BB_OWNER_SELF_HARM_RESPONSES = list(
|
|
"Please stop hurting yourself.",
|
|
"There is no need to do that.",
|
|
"Your actions are illogical.",
|
|
"Please make better choices.",
|
|
"Remember, you have beaten your worst days before."
|
|
)
|
|
)
|
|
|
|
ai_movement = /datum/ai_movement/basic_avoidance
|
|
|
|
/datum/bt_node/subtree/minebot_combat
|
|
behavior_tree_json = "code/modules/mob/living/basic/minebots/minebot_combat.bt.json"
|
|
|
|
/datum/bt_node/subtree/minebot_mining
|
|
behavior_tree_json = "code/modules/mob/living/basic/minebots/minebot_mining.bt.json"
|
|
|
|
/// Mineral wall finder that skips turfs in the blacklist and the previously unreachable wall.
|
|
/datum/bt_node/ai_behavior/find_mineral_wall/minebot
|
|
|
|
/datum/bt_node/ai_behavior/find_mineral_wall/minebot/check_if_mineable(datum/ai_controller/controller, turf/target_wall)
|
|
var/list/forbidden = controller.blackboard[BB_BLACKLIST_MINERAL_TURFS]
|
|
var/turf/previous_unreachable = controller.blackboard[BB_PREVIOUS_UNREACHABLE_WALL]
|
|
if(is_type_in_list(target_wall, forbidden) || target_wall == previous_unreachable)
|
|
return FALSE
|
|
controller.clear_blackboard_key(BB_PREVIOUS_UNREACHABLE_WALL)
|
|
return ..()
|
|
|
|
/// Mines a mineral turf at range using RangedAttack rather than ai_interact.
|
|
/datum/bt_node/ai_behavior/minebot_mine_turf
|
|
time_between_perform = 3 SECONDS
|
|
var/target_key = BB_TARGET_MINERAL_TURF
|
|
|
|
/datum/bt_node/ai_behavior/minebot_mine_turf/perform(seconds_per_tick, datum/ai_controller/controller)
|
|
var/mob/living/basic/living_pawn = controller.pawn
|
|
var/turf/target = controller.blackboard[target_key]
|
|
if(QDELETED(target))
|
|
return AI_BEHAVIOR_DELAY | AI_BEHAVIOR_FAILED
|
|
if(check_obstacles_in_path(controller, target))
|
|
return AI_BEHAVIOR_DELAY | AI_BEHAVIOR_FAILED
|
|
if(!living_pawn.combat_mode)
|
|
living_pawn.set_combat_mode(TRUE)
|
|
living_pawn.RangedAttack(target)
|
|
return AI_BEHAVIOR_DELAY | AI_BEHAVIOR_SUCCEEDED
|
|
|
|
/datum/bt_node/ai_behavior/minebot_mine_turf/proc/check_obstacles_in_path(datum/ai_controller/controller, turf/target)
|
|
var/mob/living/source = controller.pawn
|
|
var/list/turfs_in_path = get_line(source, target) - target
|
|
for(var/turf/turf in turfs_in_path)
|
|
if(turf.is_blocked_turf(ignore_atoms = list(source)))
|
|
controller.set_blackboard_key(BB_PREVIOUS_UNREACHABLE_WALL, target)
|
|
return TRUE
|
|
return FALSE
|
|
|
|
/datum/bt_node/ai_behavior/minebot_mine_turf/finish_action(datum/ai_controller/controller, succeeded)
|
|
. = ..()
|
|
controller.clear_blackboard_key(target_key)
|
|
|
|
/// Sends a radio SOS message for a dead or unconscious miner. Clears the target key on finish.
|
|
/datum/bt_node/ai_behavior/send_sos_message
|
|
time_between_perform = 2 MINUTES
|
|
var/target_key = BB_NEARBY_DEAD_MINER
|
|
|
|
/datum/bt_node/ai_behavior/send_sos_message/perform(seconds_per_tick, datum/ai_controller/controller)
|
|
var/mob/living/carbon/target = controller.blackboard[target_key]
|
|
var/mob/living/living_pawn = controller.pawn
|
|
if(QDELETED(target) || is_station_level(target.z))
|
|
return AI_BEHAVIOR_DELAY | AI_BEHAVIOR_FAILED
|
|
var/turf/target_turf = get_turf(target)
|
|
var/obj/item/implant/radio/radio_implant = locate(/obj/item/implant/radio) in living_pawn.contents
|
|
if(!radio_implant)
|
|
return AI_BEHAVIOR_DELAY | AI_BEHAVIOR_FAILED
|
|
var/message = "ALERT, [target] in need of help at coordinates: [target_turf.x], [target_turf.y], [target_turf.z]!"
|
|
radio_implant.radio.talk_into(living_pawn, message, RADIO_CHANNEL_SUPPLY)
|
|
return AI_BEHAVIOR_DELAY | AI_BEHAVIOR_SUCCEEDED
|
|
|
|
/datum/bt_node/ai_behavior/send_sos_message/finish_action(datum/ai_controller/controller, succeeded)
|
|
. = ..()
|
|
controller.clear_blackboard_key(target_key)
|
|
|
|
/// Moves adjacent to an allied node drone and repairs it if its health is below the threshold.
|
|
/// Fails if the drone is healthy, not allied, or repair is disabled.
|
|
/datum/bt_node/ai_behavior/repair_drone
|
|
var/target_key = BB_DRONE_DEFEND
|
|
var/repair_threshold = 0.75
|
|
|
|
/datum/bt_node/ai_behavior/repair_drone/perform(seconds_per_tick, datum/ai_controller/controller)
|
|
var/mob/living/target = controller.blackboard[target_key]
|
|
if(QDELETED(target))
|
|
return AI_BEHAVIOR_DELAY | AI_BEHAVIOR_FAILED
|
|
if(!controller.blackboard[BB_MINEBOT_REPAIR_DRONE])
|
|
return AI_BEHAVIOR_DELAY | AI_BEHAVIOR_FAILED
|
|
var/mob/living/living_pawn = controller.pawn
|
|
if(!living_pawn.has_ally(target))
|
|
return AI_BEHAVIOR_DELAY | AI_BEHAVIOR_FAILED
|
|
if(target.health >= target.maxHealth * repair_threshold)
|
|
return AI_BEHAVIOR_DELAY | AI_BEHAVIOR_FAILED
|
|
INVOKE_ASYNC(living_pawn, TYPE_PROC_REF(/atom/movable, say), "REPAIRING [target]!")
|
|
INVOKE_ASYNC(controller, TYPE_PROC_REF(/datum/ai_controller, ai_interact), target)
|
|
return AI_BEHAVIOR_DELAY | AI_BEHAVIOR_SUCCEEDED
|
|
|
|
/// Turns off combat mode then interacts with a nearby ore to collect it. Clears the target key on finish.
|
|
/datum/bt_node/ai_behavior/collect_ore/minebot
|
|
var/target_key = BB_ORE_TARGET
|
|
|
|
/datum/bt_node/ai_behavior/collect_ore/minebot/perform(seconds_per_tick, datum/ai_controller/controller)
|
|
var/obj/item/stack/ore/target = controller.blackboard[target_key]
|
|
if(QDELETED(target))
|
|
return AI_BEHAVIOR_DELAY | AI_BEHAVIOR_FAILED
|
|
var/mob/living/living_pawn = controller.pawn
|
|
if(living_pawn.combat_mode)
|
|
living_pawn.set_combat_mode(FALSE)
|
|
INVOKE_ASYNC(controller, TYPE_PROC_REF(/datum/ai_controller, ai_interact), target)
|
|
return AI_BEHAVIOR_DELAY | AI_BEHAVIOR_SUCCEEDED
|
|
|
|
/datum/bt_node/ai_behavior/collect_ore/minebot/finish_action(datum/ai_controller/controller, succeeded)
|
|
. = ..()
|
|
controller.clear_blackboard_key(target_key)
|
|
|
|
/// befriend_target variant that fails immediately if the target is already an ally used to gate the drone-defend block.
|
|
/datum/bt_node/ai_behavior/befriend_target/check_ally
|
|
|
|
/datum/bt_node/ai_behavior/befriend_target/check_ally/perform(seconds_per_tick, datum/ai_controller/controller)
|
|
var/mob/living/living_pawn = controller.pawn
|
|
var/mob/living/living_target = controller.blackboard[target_key]
|
|
if(QDELETED(living_target) || living_pawn.has_ally(living_target))
|
|
return AI_BEHAVIOR_DELAY | AI_BEHAVIOR_FAILED
|
|
return ..()
|
|
|
|
/// BT-native ranged attack for the minebot. Avoids friendly fire.
|
|
/datum/bt_node/ai_behavior/basic_ranged_attack/minebot
|
|
avoid_friendly_fire = TRUE
|
|
|
|
/// Accepts humans with TRAIT_ROCK_STONER (miners).
|
|
/datum/targeting_strategy/rock_stoner
|
|
|
|
/datum/targeting_strategy/rock_stoner/is_valid_target(mob/living/living_mob, atom/target, vision_range, datum/ai_controller/controller = null)
|
|
. = ..()
|
|
if(!.)
|
|
return FALSE
|
|
if(!istype(target, /mob/living/carbon/human))
|
|
return FALSE
|
|
return HAS_TRAIT(target, TRAIT_ROCK_STONER)
|
|
|
|
/// Accepts unconscious or dead humans that have a mind (i.e., real players/NPCs that need SOS).
|
|
/datum/targeting_strategy/unconscious_human
|
|
|
|
/datum/targeting_strategy/unconscious_human/is_valid_target(mob/living/living_mob, atom/target, vision_range, datum/ai_controller/controller = null)
|
|
. = ..()
|
|
if(!.)
|
|
return FALSE
|
|
if(!istype(target, /mob/living/carbon/human))
|
|
return FALSE
|
|
var/mob/living/carbon/human/human_target = target
|
|
return human_target.stat >= UNCONSCIOUS && human_target.mind
|
|
|
|
///pet commands
|
|
/datum/pet_command/free/minebot
|
|
|
|
/datum/pet_command/free/minebot/execute_action(datum/ai_controller/controller)
|
|
controller.set_blackboard_key(BB_AUTOMATED_MINING, FALSE)
|
|
return ..()
|
|
|
|
/datum/pet_command/automate_mining
|
|
command_name = "Automate mining"
|
|
command_desc = "Make your minebot automatically mine!"
|
|
radial_icon_state = "mine"
|
|
speech_commands = list("mine")
|
|
callout_type = /datum/callout_option/mine
|
|
|
|
/datum/pet_command/automate_mining/valid_callout_target(mob/living/speaker, datum/callout_option/callout, atom/target)
|
|
return ismineralturf(target)
|
|
|
|
/datum/pet_command/automate_mining/retrieve_command_text(atom/living_pet, atom/target)
|
|
return "signals [living_pet] to start mining!"
|
|
|
|
/datum/pet_command/automate_mining/execute_action(datum/ai_controller/controller)
|
|
controller.set_blackboard_key(BB_AUTOMATED_MINING, TRUE)
|
|
controller.clear_blackboard_key(BB_ACTIVE_PET_COMMAND)
|
|
|
|
/datum/pet_command/minebot_ability
|
|
command_name = "Minebot ability"
|
|
command_desc = "Make your minebot use one of its abilities."
|
|
radial_icon = 'icons/mob/actions/actions_mecha.dmi'
|
|
///the ability we will use
|
|
var/ability_key
|
|
|
|
/datum/pet_command/minebot_ability/execute_action(datum/ai_controller/controller)
|
|
var/datum/action/cooldown/ability = controller.blackboard[ability_key]
|
|
if(!ability?.IsAvailable())
|
|
return
|
|
INVOKE_ASYNC(ability, TYPE_PROC_REF(/datum/action, Trigger))
|
|
controller.clear_blackboard_key(BB_ACTIVE_PET_COMMAND)
|
|
|
|
/datum/pet_command/minebot_ability/light
|
|
command_name = "Toggle lights"
|
|
command_desc = "Make your minebot toggle its lights."
|
|
speech_commands = list("light")
|
|
radial_icon_state = "mech_lights_off"
|
|
ability_key = BB_MINEBOT_LIGHT_ABILITY
|
|
|
|
/datum/pet_command/minebot_ability/light/retrieve_command_text(atom/living_pet, atom/target)
|
|
return "signals [living_pet] to toggle its lights!"
|
|
|
|
/datum/pet_command/minebot_ability/dump
|
|
command_name = "Dump ore"
|
|
command_desc = "Make your minebot dump all its ore!"
|
|
speech_commands = list("dump", "ore")
|
|
radial_icon_state = "mech_eject"
|
|
ability_key = BB_MINEBOT_DUMP_ABILITY
|
|
|
|
/datum/pet_command/minebot_ability/dump/retrieve_command_text(atom/living_pet, atom/target)
|
|
return "signals [living_pet] to dump its ore!"
|
|
|
|
/datum/pet_command/minebot_ability/dump/execute_action(datum/ai_controller/controller)
|
|
controller.set_blackboard_key(BB_AUTOMATED_MINING, FALSE) //else bro will just pick it up
|
|
return ..()
|
|
|
|
/datum/pet_command/attack/minebot
|
|
attack_subtree = /datum/bt_node/subtree/pet_command/attack/minebot
|
|
|
|
/datum/pet_command/attack/minebot/execute_action(datum/ai_controller/controller)
|
|
controller.set_blackboard_key(BB_AUTOMATED_MINING, FALSE)
|
|
var/mob/living/living_pawn = controller.pawn
|
|
if(!living_pawn.combat_mode)
|
|
living_pawn.set_combat_mode(TRUE)
|
|
return ..()
|
|
|
|
/datum/pet_command/idle/minebot
|
|
|
|
/datum/pet_command/idle/minebot/execute_action(datum/ai_controller/controller)
|
|
controller.set_blackboard_key(BB_AUTOMATED_MINING, FALSE)
|
|
return ..()
|
|
|
|
/datum/pet_command/protect_owner/minebot
|
|
|
|
/datum/pet_command/protect_owner/minebot/set_command_target(mob/living/parent, atom/target)
|
|
if(!parent.ai_controller.blackboard[BB_MINEBOT_AUTO_DEFEND])
|
|
return FALSE
|
|
if(!parent.ai_controller.blackboard_key_exists(BB_CURRENT_TARGET) && !QDELETED(target)) //we are already dealing with something,
|
|
parent.ai_controller.set_blackboard_key(BB_CURRENT_TARGET, target)
|
|
return TRUE
|
|
|
|
/datum/pet_command/protect_owner/minebot/execute_action(datum/ai_controller/controller)
|
|
if(controller.blackboard[BB_MINEBOT_AUTO_DEFEND])
|
|
var/mob/living/living_pawn = controller.pawn
|
|
living_pawn.set_combat_mode(TRUE)
|
|
controller.clear_blackboard_key(BB_ACTIVE_PET_COMMAND)
|