Files
df7832aa43 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>
2026-08-15 10:33:57 -06:00

264 lines
10 KiB
Plaintext

/mob/living/basic/bot/secbot
name = "\improper Securitron"
desc = "A little security robot. He looks less than thrilled."
icon = 'icons/mob/silicon/aibots.dmi'
icon_state = "secbot"
base_icon_state = "secbot"
light_color = "#f56275"
light_power = 0.8
gender = MALE
density = FALSE
anchored = FALSE
health = 25
maxHealth = 25
damage_coeff = list(BRUTE = 0.5, BURN = 0.7, TOX = 0, STAMINA = 0, OXY = 0)
pass_flags = PASSMOB | PASSFLAPS
combat_mode = TRUE
can_buckle_to = FALSE
req_one_access = list(ACCESS_SECURITY)
radio_key = /obj/item/encryptionkey/secbot //AI Priv + Security
radio_channel = RADIO_CHANNEL_SECURITY //Security channel
bot_type = SEC_BOT
bot_mode_flags = ~BOT_MODE_CAN_BE_SAPIENT
data_hud_type = TRAIT_SECURITY_HUD
hackables = "target identification systems"
path_image_color = COLOR_RED
possessed_message = "You are a securitron! Guard the station to the best of your ability!"
additional_access = /datum/id_trim/job/detective
custom_materials = list(/datum/material/iron = SHEET_MATERIAL_AMOUNT * 6.2, /datum/material/glass = SMALL_MATERIAL_AMOUNT * 3.2)
ai_controller = /datum/ai_controller/basic_controller/bot/secbot
///Whether this secbot is considered 'commissioned' and given the trait on Initialize.
var/commissioned = FALSE
///The type of baton this Secbot will use
var/baton_type = /obj/item/melee/baton/security
///The weapon (from baton_type) that will be used to make arrests.
var/obj/item/weapon
///The threat level of the BOT, will arrest anyone at threatlevel 4 or above
var/threatlevel = 0
///Flags SecBOTs use on what to check on targets when arresting, and whether they should announce it to security/handcuff their target
/// Look at the security_mode_flags bitfield for more information on what's togglable here.
var/security_mode_flags = SECBOT_DECLARE_ARRESTS | SECBOT_CHECK_RECORDS | SECBOT_HANDCUFF_TARGET
/// On arrest, charges the violator this much.
/// If they don't have that much in their account, they will get beaten instead
var/price_arrest = 0
/// Charged each time the violator is stunned on detain
var/price_detain = 0
///The department the secbot will deposit collected money into
var/payment_department = ACCOUNT_SEC
///what sound we play when stunning
var/stun_sound = 'sound/items/weapons/egloves.ogg'
///The type of cuffs we use on criminals after making arrests
var/cuff_type = /obj/item/restraints/handcuffs/cable/zipties/used
/mob/living/basic/bot/secbot/Initialize(mapload)
. = ..()
weapon = new baton_type(src)
update_appearance(UPDATE_ICON)
if(commissioned)
ADD_TRAIT(src, TRAIT_COMMISSIONED, INNATE_TRAIT)
var/static/list/loc_connections = list(
COMSIG_ATOM_ENTERED = PROC_REF(on_entered),
)
AddElement(/datum/element/connect_loc, loc_connections)
AddComponent(/datum/component/security_vision, judgement_criteria = NONE, update_judgement_criteria = CALLBACK(src, PROC_REF(judgement_criteria)))
add_arrest_component()
/mob/living/basic/bot/secbot/Destroy()
QDEL_NULL(weapon)
return ..()
/mob/living/basic/bot/secbot/update_icon_state()
if(mode == BOT_HUNT)
icon_state = "[base_icon_state]-c"
return ..()
/mob/living/basic/bot/secbot/turn_off()
..()
update_bot_mode(new_mode = BOT_IDLE)
/mob/living/basic/bot/secbot/on_saboteur(datum/source, disrupt_duration)
. = ..()
if(!(security_mode_flags & SECBOT_SABOTEUR_AFFECTED))
security_mode_flags |= SECBOT_SABOTEUR_AFFECTED
addtimer(CALLBACK(src, PROC_REF(remove_saboteur_effect)), disrupt_duration)
return TRUE
/mob/living/basic/bot/secbot/proc/remove_saboteur_effect()
security_mode_flags &= ~SECBOT_SABOTEUR_AFFECTED
/mob/living/basic/bot/secbot/electrocute_act(shock_damage, source, siemens_coeff = 1, flags = NONE)//shocks only make him angry
if(speed >= initial(speed) + 3)
return
speed += 3
addtimer(VARSET_CALLBACK(src, speed, speed - 3), 6 SECONDS)
playsound(src, 'sound/machines/defib/defib_zap.ogg', 50)
visible_message(span_warning("[src] shakes and speeds up!"))
/mob/living/basic/bot/secbot/Exited(atom/movable/gone, direction)
. = ..()
if(gone == weapon)
weapon = null
update_appearance()
// Variables sent to TGUI
/mob/living/basic/bot/secbot/ui_data(mob/user)
var/list/data = ..()
if(!(bot_access_flags & BOT_COVER_LOCKED) || HAS_SILICON_ACCESS(user))
data["custom_controls"]["check_id"] = security_mode_flags & SECBOT_CHECK_IDS
data["custom_controls"]["check_weapons"] = security_mode_flags & SECBOT_CHECK_WEAPONS
data["custom_controls"]["check_warrants"] = security_mode_flags & SECBOT_CHECK_RECORDS
data["custom_controls"]["handcuff_targets"] = security_mode_flags & SECBOT_HANDCUFF_TARGET
data["custom_controls"]["arrest_alert"] = security_mode_flags & SECBOT_DECLARE_ARRESTS
return data
// Actions received from TGUI
/mob/living/basic/bot/secbot/ui_act(action, list/params, datum/tgui/ui, datum/ui_state/state)
. = ..()
var/mob/user = ui.user
if(. || (bot_access_flags & BOT_COVER_LOCKED && !HAS_SILICON_ACCESS(user)))
return
switch(action)
if("check_id")
security_mode_flags ^= SECBOT_CHECK_IDS
return TRUE
if("check_weapons")
security_mode_flags ^= SECBOT_CHECK_WEAPONS
return TRUE
if("check_warrants")
security_mode_flags ^= SECBOT_CHECK_RECORDS
return TRUE
if("handcuff_targets")
security_mode_flags ^= SECBOT_HANDCUFF_TARGET
return TRUE
if("arrest_alert")
security_mode_flags ^= SECBOT_DECLARE_ARRESTS
return TRUE
/mob/living/basic/bot/secbot/attack_hand(mob/living/carbon/human/user, list/modifiers)
// Turns an oversight into a feature. Beepsky will now announce when pacifists taunt him over sec comms.
if(HAS_TRAIT(user, TRAIT_PACIFISM))
user.visible_message(span_notice("[user] taunts [src], daring [p_them()] to give chase!"), \
span_notice("You taunt [src], daring [p_them()] to chase you!"), span_hear("You hear someone shout a daring taunt!"), DEFAULT_MESSAGE_RANGE, user)
speak("Taunted by pacifist scumbag [RUNECHAT_BOLD("[user]")] in [get_area(src)].", radio_channel)
// Interrupt the attack chain. We've already handled this scenario for pacifists.
return
return ..()
/mob/living/basic/bot/secbot/proc/retrieve_emag_message()
audible_message(span_danger("[src] buzzes oddly!"))
/mob/living/basic/bot/secbot/emag_act(mob/user, obj/item/card/emag/emag_card)
. = ..()
if(!(bot_access_flags & BOT_COVER_EMAGGED))
return
if(user)
balloon_alert(user, "target assessment circuits shorted")
retrieve_emag_message()
security_mode_flags &= ~SECBOT_DECLARE_ARRESTS
update_appearance()
return TRUE
/mob/living/basic/bot/secbot/proc/post_arrest(mob/living/carbon/current_target)
playsound(src, SFX_LAW, 50, FALSE)
/mob/living/basic/bot/secbot/proc/post_stun(mob/living/carbon/current_target, harm = FALSE)
flick("[base_icon_state]-c", src)
var/threat = 5 || ai_controller.blackboard[BB_CURRENT_CRIMINAL_ASSESSMENT]
if(security_mode_flags & SECBOT_DECLARE_ARRESTS)
var/area/location = get_area(src)
speak("[security_mode_flags & SECBOT_HANDCUFF_TARGET ? "Arresting" : "Detaining"] level [threat] scumbag [RUNECHAT_BOLD("[current_target]")] in [location].", radio_channel)
payment_check(current_target)
update_bot_mode(new_mode = BOT_PREP_ARREST)
/mob/living/basic/bot/secbot/explode()
var/atom/drop_location = drop_location()
retrieve_secbot_drops(drop_location)
new /obj/effect/decal/cleanable/blood/oil(loc)
return ..()
/mob/living/basic/bot/secbot/proc/retrieve_secbot_drops(atom/drop_location)
var/obj/item/bot_assembly/secbot/secbot_assembly = new(drop_location)
secbot_assembly.build_step = ASSEMBLY_FIRST_STEP
secbot_assembly.add_overlay("hs_hole")
secbot_assembly.created_name = name
new /obj/item/assembly/prox_sensor(drop_location)
drop_part(weapon, drop_location)
/mob/living/basic/bot/secbot/proc/on_entered(datum/source, atom/movable/to_be_tripped)
SIGNAL_HANDLER
var/mob/living/possible_target = ai_controller.blackboard[BB_CURRENT_TARGET]
if(!has_gravity() || !ismob(to_be_tripped) || !possible_target)
return
var/mob/living/carbon/tripped_mob = to_be_tripped
if(istype(tripped_mob) && !in_range(src, possible_target))
knockOver(tripped_mob)
/// Returns true if the current target is unable to pay to be detained/arrested
/mob/living/basic/bot/secbot/proc/payment_check(mob/living/carbon/human/human_target)
var/fair_market_price = (security_mode_flags & SECBOT_HANDCUFF_TARGET) ? price_arrest : price_detain
if(fair_market_price <= 0)
return FALSE
if(!ishuman(human_target))
return FALSE
var/obj/item/card/id/target_id = human_target.get_idcard()
if(!target_id)
say("Unable to pay fine: No ID card found.")
return TRUE
var/datum/bank_account/insurance = target_id.registered_account
if(!insurance)
say("Unable to pay fine: No bank account found.")
return TRUE
if(!insurance.adjust_money(-fair_market_price, "Securitron fine"))
say("Unable to pay fine: Not enough funds in account.")
return TRUE
SSeconomy.get_dep_account(payment_department)?.adjust_money(fair_market_price)
say("Fine paid: Thank you for your compliance. Your account been charged [fair_market_price] [MONEY_NAME].")
return FALSE
/mob/living/basic/bot/secbot/generate_speak_list()
var/static/list/secbot_lines = list(
BEEPSKY_VOICED_CRIMINAL_DETECTED = 'sound/mobs/non-humanoids/beepsky/criminal.ogg',
BEEPSKY_VOICED_FREEZE = 'sound/mobs/non-humanoids/beepsky/freeze.ogg',
BEEPSKY_VOICED_JUSTICE = 'sound/mobs/non-humanoids/beepsky/justice.ogg',
BEEPSKY_VOICED_YOUR_MOVE = 'sound/mobs/non-humanoids/beepsky/creep.ogg',
BEEPSKY_VOICED_I_AM_THE_LAW = 'sound/mobs/non-humanoids/beepsky/iamthelaw.ogg',
BEEPSKY_VOICED_SECURE_DAY = 'sound/mobs/non-humanoids/beepsky/secureday.ogg',
)
return secbot_lines
/mob/living/basic/bot/secbot/proc/judgement_criteria()
var/final = FALSE
if(bot_access_flags & BOT_COVER_EMAGGED)
final |= JUDGE_EMAGGED
if(security_mode_flags & SECBOT_CHECK_IDS)
final |= JUDGE_IDCHECK
if(security_mode_flags & SECBOT_CHECK_RECORDS)
final |= JUDGE_RECORDCHECK
if(security_mode_flags & SECBOT_CHECK_WEAPONS)
final |= JUDGE_WEAPONCHECK
if(security_mode_flags & SECBOT_SABOTEUR_AFFECTED)
final |= JUDGE_CHILLOUT
return final
/mob/living/basic/bot/secbot/proc/add_arrest_component()
AddComponent(/datum/component/stun_n_cuff,\
stun_sound = stun_sound,\
post_stun_callback = CALLBACK(src, PROC_REF(post_stun)),\
post_arrest_callback = CALLBACK(src, PROC_REF(post_arrest)),\
handcuff_type = cuff_type,\
)