Skip to content
This repository has been archived by the owner on Jul 27, 2021. It is now read-only.

[MIRROR] [PORT] Datumized AI + implemented for monkeys from TG #604

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion beestation.dme
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "code\__DEFINES\_tick.dm"
#include "code\__DEFINES\access.dm"
#include "code\__DEFINES\admin.dm"
#include "code\__DEFINES\ai.dm"
#include "code\__DEFINES\antagonists.dm"
#include "code\__DEFINES\areas.dm"
#include "code\__DEFINES\atmospherics.dm"
Expand Down Expand Up @@ -309,6 +310,11 @@
#include "code\controllers\subsystem\vis_overlays.dm"
#include "code\controllers\subsystem\vote.dm"
#include "code\controllers\subsystem\weather.dm"
<<<<<<< HEAD
=======
#include "code\controllers\subsystem\processing\ai_controllers.dm"
#include "code\controllers\subsystem\processing\clock_component.dm"
>>>>>>> 23504ea087 ([PORT] Datumized AI + implemented for monkeys from TG (#4670))
#include "code\controllers\subsystem\processing\fastprocess.dm"
#include "code\controllers\subsystem\processing\fields.dm"
#include "code\controllers\subsystem\processing\fluids.dm"
Expand Down Expand Up @@ -364,6 +370,11 @@
#include "code\datums\world_topic.dm"
#include "code\datums\actions\beam_rifle.dm"
#include "code\datums\actions\ninja.dm"
#include "code\datums\ai\_ai_behaviour.dm"
#include "code\datums\ai\_ai_controller.dm"
#include "code\datums\ai\generic_actions.dm"
#include "code\datums\ai\monkey\monkey_behaviours.dm"
#include "code\datums\ai\monkey\monkey_controller.dm"
#include "code\datums\announcers\_announcer.dm"
#include "code\datums\announcers\default_announcer.dm"
#include "code\datums\announcers\intern_announcer.dm"
Expand Down Expand Up @@ -2356,7 +2367,6 @@
#include "code\modules\mob\living\carbon\human\species_types\vampire.dm"
#include "code\modules\mob\living\carbon\human\species_types\zombies.dm"
#include "code\modules\mob\living\carbon\human\verbs\give.dm"
#include "code\modules\mob\living\carbon\monkey\combat.dm"
#include "code\modules\mob\living\carbon\monkey\death.dm"
#include "code\modules\mob\living\carbon\monkey\inventory.dm"
#include "code\modules\mob\living\carbon\monkey\life.dm"
Expand Down
2 changes: 1 addition & 1 deletion code/__DEFINES/DNA.dm
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
#define TR_KEEPSE (1<<5) // changelings shouldn't edit the DNA's SE when turning into a monkey
#define TR_DEFAULTMSG (1<<6)
#define TR_KEEPORGANS (1<<8)

#define TR_KEEPAI (1<<9)

#define CLONER_FRESH_CLONE "fresh"
#define CLONER_MATURE_CLONE "mature"
Expand Down
34 changes: 34 additions & 0 deletions code/__DEFINES/ai.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#define GET_AI_BEHAVIOR(behavior_type) SSai_controllers.ai_behaviors[behavior_type]
#define HAS_AI_CONTROLLER_TYPE(thing, type) istype(thing?.ai_controller, type)

#define AI_STATUS_ON 1
#define AI_STATUS_OFF 2


///Monkey checks
#define SHOULD_RESIST(source) (source.on_fire || source.buckled || source.restrained() || (source.pulledby && source.pulledby.grab_state > GRAB_PASSIVE))

///Max pathing attempts before auto-fail
#define MAX_PATHING_ATTEMPTS 30

///Flags for ai_behavior new()
#define AI_CONTROLLER_INCOMPATIBLE (1<<0)

///Does this task require movement from the AI before it can be performed?
#define AI_BEHAVIOR_REQUIRE_MOVEMENT (1<<0)
///Does this task let you perform the action while you move closer? (Things like moving and shooting)
#define AI_BEHAVIOR_MOVE_AND_PERFORM (1<<1)


///Monkey AI controller blackboard keys

#define BB_MONKEY_AGRESSIVE "BB_monkey_agressive"
#define BB_MONKEY_BEST_FORCE_FOUND "BB_monkey_bestforcefound"
#define BB_MONKEY_ENEMIES "BB_monkey_enemies"
#define BB_MONKEY_BLACKLISTITEMS "BB_monkey_blacklistitems"
#define BB_MONKEY_PICKUPTARGET "BB_monkey_pickuptarget"
#define BB_MONKEY_PICKPOCKETING "BB_monkey_pickpocketing"
#define BB_MONKEY_CURRENT_ATTACK_TARGET "BB_monkey_current_attack_target"
#define BB_MONKEY_TARGET_DISPOSAL "BB_monkey_target_disposal"
#define BB_MONKEY_DISPOSING "BB_monkey_disposing"
#define BB_MONKEY_RECRUIT_COOLDOWN "BB_monkey_recruit_cooldown"
15 changes: 15 additions & 0 deletions code/__DEFINES/dcs/signals.dm
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@
#define COMSIG_ATOM_INTERCEPT_TELEPORT "intercept_teleport" //! called when teleporting into a protected turf: (channel, turf/origin)
#define COMPONENT_BLOCK_TELEPORT 1
/////////////////
/* Attack signals. They should share the returned flags, to standardize the attack chain. */
/// tool_act -> pre_attack -> target.attackby (item.attack) -> afterattack
///Ends the attack chain. If sent early might cause posterior attacks not to happen.
#define COMPONENT_CANCEL_ATTACK_CHAIN (1<<0)
///Skips the specific attack step, continuing for the next one to happen.
#define COMPONENT_SKIP_ATTACK (1<<1)

#define COMSIG_ATOM_ATTACK_GHOST "atom_attack_ghost" //! from base of atom/attack_ghost(): (mob/dead/observer/ghost)
#define COMSIG_ATOM_ATTACK_HAND "atom_attack_hand" //! from base of atom/attack_hand(): (mob/user)
#define COMSIG_ATOM_ATTACK_PAW "atom_attack_paw" //! from base of atom/attack_paw(): (mob/user)
Expand All @@ -106,6 +113,10 @@

#define COMSIG_ATOM_AFTER_SUCCESSFUL_INITIALIZE "atom_init_success"

///from base of atom/hitby(atom/movable/AM, skipcatch, hitpush, blocked, datum/thrownthing/throwingdatum)
#define COMSIG_ATOM_HITBY "atom_hitby"


/////////////////

#define COMSIG_AREA_POWER_CHANGE "area_power_change" //! from base of area/proc/power_change(): ()
Expand Down Expand Up @@ -162,6 +173,7 @@

// /mob signals
#define COMSIG_MOB_LOGIN "mob_login"
#define COMSIG_MOB_LOGOUT "mob_logout" ///from base of /mob/Logout(): ()
#define COMSIG_MOB_DEATH "mob_death" //! from base of mob/death(): (gibbed)
#define COMSIG_MOB_STATCHANGE "mob_statchange" //from base of mob/set_stat(): (new_stat)
#define COMSIG_MOB_CLICKON "mob_clickon" //! from base of mob/clickon(): (atom/A, params)
Expand Down Expand Up @@ -203,6 +215,8 @@
#define COMSIG_PROCESS_BORGCHARGER_OCCUPANT "living_charge" //! sent from borg recharge stations: (amount, repairs)
#define COMSIG_BORG_SAFE_DECONSTRUCT "borg_safe_decon" //sent from borg mobs to itself, for tools to catch an upcoming destroy() due to safe decon (rather than detonation)
#define COMSIG_MOB_CLIENT_LOGIN "comsig_mob_client_login"
#define COMSIG_LIVING_TRY_SYRINGE "living_try_syringe" ///From post-can inject check of syringe after attack (mob/user)
#define COMSIG_LIVING_START_PULL "living_start_pull" ///called on /living when someone starts pulling (atom/movable/pulled, state, force)

//ALL OF THESE DO NOT TAKE INTO ACCOUNT WHETHER AMOUNT IS 0 OR LOWER AND ARE SENT REGARDLESS!
#define COMSIG_LIVING_STATUS_STUN "living_stun" //! from base of mob/living/Stun() (amount, update, ignore)
Expand All @@ -220,6 +234,7 @@
#define COMSIG_CARBON_LOSE_ORGAN "carbon_lose_organ" //from /item/organ/proc/Remove() (/obj/item/organ/)
#define COMSIG_CARBON_EMBED_RIP "item_embed_start_rip" // defined twice, in carbon and human's topics, fired when interacting with a valid embedded_object to pull it out (mob/living/carbon/target, /obj/item, /obj/item/bodypart/L)
#define COMSIG_CARBON_EMBED_REMOVAL "item_embed_remove_safe" // called when removing a given item from a mob, from mob/living/carbon/remove_embedded_object(mob/living/carbon/target, /obj/item)
#define COMSIG_CARBON_CUFF_ATTEMPTED "carbon_attempt_cuff" ///Called when someone attempts to cuff a carbon

// /mob/living/simple_animal/hostile signals
#define COMSIG_HOSTILE_ATTACKINGTARGET "hostile_attackingtarget"
Expand Down
4 changes: 4 additions & 0 deletions code/__DEFINES/inventory.dm
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@
#define NECK (1<<11)
#define FULL_BODY (~0)

//defines for the index of hands
#define LEFT_HANDS 1
#define RIGHT_HANDS 2

//flags for female outfits: How much the game can safely "take off" the uniform without it looking weird
#define NO_FEMALE_UNIFORM 0
#define FEMALE_UNIFORM_FULL 1
Expand Down
64 changes: 35 additions & 29 deletions code/__DEFINES/monkeys.dm
Original file line number Diff line number Diff line change
@@ -1,37 +1,43 @@
//Monkey defines, placed here so they can be read by other things!

//Mode defines
#define MONKEY_IDLE 0 //! idle
#define MONKEY_HUNT 1 //! found target, hunting
#define MONKEY_FLEE 2 //! free from enemies
#define MONKEY_DISPOSE 3 //! dump body in disposals

#define MONKEY_FLEE_HEALTH 50 //! below this health value the monkey starts to flee from enemies
#define MONKEY_ENEMY_VISION 9 //! how close an enemy must be to trigger aggression
#define MONKEY_FLEE_VISION 4 //! how close an enemy must be before it triggers flee
#define MONKEY_ITEM_SNATCH_DELAY 25 //! How long does it take the item to be taken from a mobs hand
#define MONKEY_CUFF_RETALIATION_PROB 20 //! Probability monkey will aggro when cuffed
#define MONKEY_SYRINGE_RETALIATION_PROB 20 //! Probability monkey will aggro when syringed
/// below this health value the monkey starts to flee from enemies
#define MONKEY_FLEE_HEALTH 50
/// how close an enemy must be to trigger aggression
#define MONKEY_ENEMY_VISION 9
/// how close an enemy must be before it triggers flee
#define MONKEY_FLEE_VISION 4
/// How long does it take the item to be taken from a mobs hand
#define MONKEY_ITEM_SNATCH_DELAY 25
/// Probability monkey will aggro when cuffed
#define MONKEY_CUFF_RETALIATION_PROB 20
/// Probability monkey will aggro when syringed
#define MONKEY_SYRINGE_RETALIATION_PROB 20

// Probability per Life tick that the monkey will:
#define MONKEY_RESIST_PROB 50 //! resist out of restraints
//! when the monkey is idle
#define MONKEY_PULL_AGGRO_PROB 5 //! aggro against the mob pulling it
#define MONKEY_SHENANIGAN_PROB 5 //! chance of getting into mischief, i.e. finding/stealing items
//! when the monkey is hunting
#define MONKEY_ATTACK_DISARM_PROB 50 //! disarm an armed attacker
#define MONKEY_WEAPON_PROB 20 //! if not currently getting an item, search for a weapon around it
#define MONKEY_RECRUIT_PROB 25 //! recruit a monkey near it
#define MONKEY_SWITCH_TARGET_PROB 25 //! switch targets if it sees another enemy

#define MONKEY_RETALIATE_HARM_PROB 95 //! probability for the monkey to aggro when attacked with harm intent
#define MONKEY_RETALIATE_DISARM_PROB 20 //! probability for the monkey to aggro when attacked with disarm intent
/// probability that monkey resist out of restraints
#define MONKEY_RESIST_PROB 50
/// probability that monkey aggro against the mob pulling it
#define MONKEY_PULL_AGGRO_PROB 5
/// probability that monkey will get into mischief, i.e. finding/stealing items
#define MONKEY_SHENANIGAN_PROB 20
/// probability that monkey will disarm an armed attacker
#define MONKEY_ATTACK_DISARM_PROB 50
/// probability that monkey will get recruited when friend is attacked
#define MONKEY_RECRUIT_PROB 25


#define MONKEY_HATRED_AMOUNT 4 //! amount of aggro to add to an enemy when they attack user
#define MONKEY_HATRED_REDUCTION_PROB 25 //! probability of reducing aggro by one when the monkey attacks
/// probability for the monkey to aggro when attacked with harm intent
#define MONKEY_RETALIATE_HARM_PROB 95
/// probability for the monkey to aggro when attacked with disarm intent
#define MONKEY_RETALIATE_DISARM_PROB 20

// how many Life ticks the monkey will fail to:
#define MONKEY_HUNT_FRUSTRATION_LIMIT 8 //! Chase after an enemy before giving up
#define MONKEY_DISPOSE_FRUSTRATION_LIMIT 16 //! Dispose of a body before giving up
/// amount of aggro to add to an enemy when they attack user
#define MONKEY_HATRED_AMOUNT 4
/// amount of aggro to add to an enemy when a monkey is recruited
#define MONKEY_RECRUIT_HATED_AMOUNT 2
/// probability of reducing aggro by one when the monkey attacks
#define MONKEY_HATRED_REDUCTION_PROB 20

#define MONKEY_AGGRESSIVE_MVM_PROB 0 //! If you mass edit monkies to be aggressive. there is a small chance of in-fighting
///Monkey recruit cooldown
#define MONKEY_RECRUIT_COOLDOWN 1 MINUTES
3 changes: 3 additions & 0 deletions code/__DEFINES/subsystems.dm
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@
#define INIT_ORDER_EVENTS 70
#define INIT_ORDER_JOBS 65
#define INIT_ORDER_QUIRKS 60
#define INIT_ORDER_AI_MOVEMENT 56 //We need the movement setup
#define INIT_ORDER_AI_CONTROLLERS 55 //So the controller can get the ref
#define INIT_ORDER_TICKER 55
#define INIT_ORDER_MAPPING 50
#define INIT_ORDER_NETWORKS 45
Expand Down Expand Up @@ -152,6 +154,7 @@
#define FIRE_PRIORITY_WET_FLOORS 20
#define FIRE_PRIORITY_AIR 20
#define FIRE_PRIORITY_NPC 20
#define FIRE_PRIORITY_NPC_MOVEMENT 21
#define FIRE_PRIORITY_PROCESS 25
#define FIRE_PRIORITY_THROWING 25
#define FIRE_PRIORITY_SPACEDRIFT 30
Expand Down
1 change: 1 addition & 0 deletions code/__DEFINES/vv.dm
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
#define VV_HK_TRIGGER_EMP "empulse"
#define VV_HK_TRIGGER_EXPLOSION "explode"
#define VV_HK_AUTO_RENAME "auto_rename"
#define VV_HK_ADD_AI "add_ai"

// /obj
#define VV_HK_OSAY "osay"
Expand Down
8 changes: 8 additions & 0 deletions code/_globalvars/lists/mobs.dm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ GLOBAL_LIST_EMPTY(deadmins) //all ckeys who have used the de-admin verb.
GLOBAL_LIST_EMPTY(directory) //all ckeys with associated client
GLOBAL_LIST_EMPTY(stealthminID) //reference list with IDs that store ckeys, for stealthmins


GLOBAL_LIST_INIT(dangerous_turfs, typecacheof(list(
/turf/open/lava,
/turf/open/chasm,
/turf/open/space,
/turf/open/openspace)))


//Since it didn't really belong in any other category, I'm putting this here
//This is for procs to replace all the goddamn 'in world's that are chilling around the code

Expand Down
21 changes: 21 additions & 0 deletions code/controllers/subsystem/processing/ai_controllers.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/// The subsystem used to tick [/datum/ai_controllers] instances. Handling the re-checking of plans.
PROCESSING_SUBSYSTEM_DEF(ai_controllers)
name = "AI behavior"
flags = SS_POST_FIRE_TIMING|SS_BACKGROUND
priority = FIRE_PRIORITY_NPC
runlevels = RUNLEVEL_GAME | RUNLEVEL_POSTGAME
init_order = INIT_ORDER_AI_CONTROLLERS
wait = 8 //Uses the value of CLICK_CD_MELEE because that seemed like a nice standard for the speed of AI behavior

///an assoc list of all ai_behaviors by type, to
var/list/ai_behaviors

/datum/controller/subsystem/processing/ai_controllers/Initialize(timeofday)
SetupAIBehaviors()
return ..()

/datum/controller/subsystem/processing/ai_controllers/proc/SetupAIBehaviors()
ai_behaviors = list()
for(var/i in subtypesof(/datum/ai_behavior))
var/datum/ai_behavior/ai_behavior = new i
ai_behaviors[i] = ai_behavior
21 changes: 21 additions & 0 deletions code/datums/ai/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# AI controllers

## Introduction

Our AI controller system is an attempt at making it possible to create modularized AI that stores its behavior in datums, while keeping state and decision making in a controller. This allows a more versatile way of creating AI that doesn't rely on OOP as much, and doesn't clutter up the Life() code in Mobs.

## AI Controllers

A datum that can be added to any atom in the game. Similarly to components, they might only support a given subtype (e.g. /mob/living), but the idea is that theoretically, you could apply a specific AI controller to a big a group of different types as possible and it would still work.

These datums handle both the normal movement of mobs, but also their decision making, deciding which actions they will take based on the checks you put into their SelectBehaviors proc.

If behaviors are selected, and the AI is in range, it will try to perform them. It runs all the behaviors it currently has in parallel; allowing for it to for example screech at someone while trying to attack them. Aslong as it has behaviors running, it will not try to generate new plans, making it not waste CPU when it already has an active goal.

They also hold data for any of the actions they might need to use, such as cooldowns, whether or not they're currently fighting, etcetera this is stored in the blackboard, more information on that below.

### Blackboard
The blackboard is an associated list keyed with strings and with values of whatever you want. These store information the mob has such as "Am I attacking someone", "Do I have a weapon". By using an associated list like this, no data needs to be stored on the actions themselves, and you could make actions that work on multiple ai controllers if you so pleased by making the key to use a variable.

## AI Behavior
AI behaviors are the actions an AI can take. These can range from "Do an emote" to "Attack this target until he is dead". They are singletons and should contain nothing but static data. Any dynamic data should be stored in the blackboard, to allow different controllers to use the same behaviors.
17 changes: 17 additions & 0 deletions code/datums/ai/_ai_behaviour.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
///Abstract class for an action an AI can take, can range from movement to grabbing a nearby weapon.
/datum/ai_behavior
///What distance you need to be from the target to perform the action
var/required_distance = 1
///Flags for extra behavior
var/behavior_flags = NONE

///Called by the AI controller when this action is performed
/datum/ai_behavior/proc/perform(delta_time, datum/ai_controller/controller)
return

///Called when the action is finished.
/datum/ai_behavior/proc/finish_action(datum/ai_controller/controller, succeeded)
controller.current_behaviors.Remove(src)
if(behavior_flags & AI_BEHAVIOR_REQUIRE_MOVEMENT) //If this was a movement task, reset our movement target.
controller.current_movement_target = null
return
Loading