Files
Bubberstation/code/__DEFINES/basic_mobs.dm
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

136 lines
5.3 KiB
Plaintext

///Basic mob flags
/// Stamina threshold to not experience stamina crit
#define BASIC_MOB_NO_STAMCRIT 0
/// Max stamina should be equal to max health
#define BASIC_MOB_STAMINA_MATCH_HEALTH -1
/// Delete mob upon death
#define DEL_ON_DEATH (1<<0)
/// Rotate mob 180 degrees while it is dead
#define FLIP_ON_DEATH (1<<1)
/// Mob remains dense while dead
#define REMAIN_DENSE_WHILE_DEAD (1<<2)
/// Mob can be set on fire
#define FLAMMABLE_MOB (1<<3)
/// Mob never takes damage from unarmed attacks
#define IMMUNE_TO_FISTS (1<<4)
/// Mob is immune to getting wet
#define IMMUNE_TO_GETTING_WET (1<<5)
/// Disables the function of attacking random body zones
#define PRECISE_ATTACK_ZONES (1<<6)
/// People would be sad to see this mob die
#define SENDS_DEATH_MOODLETS (1<<7)
/// Temporary trait applied when an attack forecast animation has completed
#define TRAIT_BASIC_ATTACK_FORECAST "trait_basic_attack_forecast"
#define INTERACTION_BASIC_ATTACK_FORCEAST "interaction_basic_attack_forecast"
/// Above this speed we stop gliding because it looks silly
#define END_GLIDE_SPEED 10
///hunger cooldown for basic mobs
#define EAT_FOOD_COOLDOWN 45 SECONDS
///mook attack status flags
#define MOOK_ATTACK_NEUTRAL 0
#define MOOK_ATTACK_WARMUP 1
#define MOOK_ATTACK_ACTIVE 2
#define MOOK_ATTACK_STRIKE 3
/// Keeps track of how many gutlunches are born
GLOBAL_VAR_INIT(gutlunch_count, 0)
/// Pet customization settings saved for every client
GLOBAL_LIST_EMPTY(customized_pets)
// Raptor defines
/// Raptor chick, tiny and very frail
#define RAPTOR_BABY "baby"
/// Raptor youngling, cannot be ridden and has reduced stats, but can hunt by itself just fine
#define RAPTOR_YOUNG "young"
/// Fully grown adult raptor
#define RAPTOR_ADULT "adult"
/// Innate raptor offsets
#define RAPTOR_INNATE_SOURCE "raptor_innate"
// Growth values
/// How much does meal complexity affect our growth?
#define RAPTOR_MEAL_COMPLEXITY_GROWTH_FACTOR 5
/// Base value for raptor growth from meat
#define RAPTOR_GROWTH_BASE_MEAT 15
/// Base value for raptor growth from ash flora
#define RAPTOR_GROWTH_BASE_PLANT 8
/// How much growth progress raptors need to accumulate to fully grow into an adult
#define RAPTOR_GROWTH_REQUIRED 100
/// Minimum random growth value a baby raptor can gain per second
#define RAPTOR_BABY_GROWTH_LOWER 0.8
/// Maximum random growth value a baby raptor can gain per second
#define RAPTOR_BABY_GROWTH_UPPER 1.2
/// How long it takes for a raptor egg to grow up, in seconds
#define RAPTOR_EGG_GROWTH_PROGRESS 100
/// How much happiness percentage affects our growth speed
#define RAPTOR_GROWTH_HAPPINESS_MULTIPLIER 0.0075 // Full happiness increases growth rate by 75%
/// Damage boost per happiness percent
#define RAPTOR_HAPPINESS_DAMAGE_BOOST 0.05
// Raptor inheritance stats
/// Maximum amount of traits a raptor can inherit
#define RAPTOR_TRAIT_INHERIT_AMOUNT 2
/// Minimum modifier to base attack values a raptor can get
#define RAPTOR_INHERIT_MIN_ATTACK -5
/// Maximum modifier to base attack values a raptor can get
#define RAPTOR_INHERIT_MAX_ATTACK 5
/// Minimum modifier to the base health value a raptor can get
#define RAPTOR_INHERIT_MIN_HEALTH -30
/// Maximum modifier to the base health value a raptor can get
#define RAPTOR_INHERIT_MAX_HEALTH 30
/// Minimum modifier to base speed values a raptor can get
#define RAPTOR_INHERIT_MIN_SPEED -0.66
/// Maximum modifier to base speed values a raptor can get
#define RAPTOR_INHERIT_MAX_SPEED 0.66
/// Minimum modifier a raptor can get to their modifiers, such as ability effect and growth speed
#define RAPTOR_INHERIT_MIN_MODIFIER -0.25
/// Maximum modifier a raptor can get to their modifiers, such as ability effect and growth speed
#define RAPTOR_INHERIT_MAX_MODIFIER 0.25
/// Genetic drift for raptors, aka min/max value from the cap that stats can receive when breeding
#define RAPTOR_GENETIC_DRIFT 0.2
/// This mob suffers depression
#define BB_BASIC_DEPRESSED "basic_depressed"
/// This mob will care for its young
#define BB_RAPTOR_MOTHERLY "raptor_motherly"
/// This mob will be playful around their owners
#define BB_RAPTOR_PLAYFUL "raptor_playful"
/// This mob will flee combat when it feels threatened
#define BB_RAPTOR_COWARD "raptor_coward"
/// Our raptor baby target we will take care of
#define BB_RAPTOR_BABY "raptor_baby"
/// The raptor we will heal up
#define BB_INJURED_RAPTOR "injured_raptor"
/// The cooldown for next time we eat
#define BB_RAPTOR_EAT_COOLDOWN "raptor_eat_cooldown"
/// Our trough target
#define BB_RAPTOR_TROUGH_TARGET "raptor_trough_target"
/// HP level at which we'll flee from attackers
#define BB_RAPTOR_FLEE_THRESHOLD "raptor_flee_threshold"
#define MAX_RAPTOR_POP 64
///Return value for [/mob/living/basic/proc/early_melee_attack]. Using this value will make the attack continue as normal.
#define BASIC_MOB_CONTINUE_ATTACK_CHAIN 0
///Return value for [/mob/living/basic/proc/early_melee_attack]. Using this value will make the attack end, but not set a cooldown. This is the default.
#define BASIC_MOB_END_ATTACK_CHAIN 1
///Return value for [/mob/living/basic/proc/early_melee_attack]. Using this value will make the attack end, and sets a cooldown. Useful if you add behavior to early_melee_attack
#define BASIC_MOB_END_ATTACK_CHAIN_COOLDOWN 2
///Delay between trying to update target selection
#define BASIC_MOB_FIND_TARGET_RATE 1 SECONDS
///Time between idle behavior execution
#define IDLE_BEHAVIOR_RATE 1.5 SECONDS