[MIRROR] Adds a subsystem for ai movement (#3613)

* Adds a subsystem for ai movement (#57111)

* done

* straight walk

* movement

* yep

* removes unused macro

* done

* Update ai_movement.dm

* Adds a subsystem for ai movement

Co-authored-by: Qustinnus <Floydje123@hotmail.com>
This commit is contained in:
SkyratBot
2021-02-23 21:46:00 +00:00
committed by GitHub
co-authored by Qustinnus
parent 6616887bc1
commit c2215e3905
15 changed files with 119 additions and 34 deletions
+1
View File
@@ -17,4 +17,5 @@
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
controller.ai_movement.stop_moving_towards(controller)
return
+27 -28
View File
@@ -26,8 +26,15 @@ have ways of interacting with a specific atom and control it. They posses a blac
var/continue_processing_when_client = FALSE
///distance to give up on target
var/max_target_distance = 14
///Reference to the movement datum we use. Is a type on initialize but becomes a ref afterwards.
var/datum/ai_movement/ai_movement = /datum/ai_movement/dumb
///Cooldown until next movement
COOLDOWN_DECLARE(movement_cooldown)
///Delay between movements. This is on the controller so we can keep the movement datum singleton
var/movement_delay = 0.1 SECONDS
/datum/ai_controller/New(atom/new_pawn)
ai_movement = SSai_movement.movement_types[ai_movement]
PossessPawn(new_pawn)
/datum/ai_controller/Destroy(force, ...)
@@ -81,8 +88,7 @@ have ways of interacting with a specific atom and control it. They posses a blac
/// Generates a plan and see if our existing one is still valid.
/datum/ai_controller/process(delta_time)
if(!able_to_run())
var/atom/movable/movable_pawn = pawn
walk(movable_pawn, 0) //stop moving
walk(pawn, 0) //stop moving
return //this should remove them from processing in the future through event-based stuff.
if(!current_behaviors?.len)
SelectBehaviors(delta_time)
@@ -90,40 +96,33 @@ have ways of interacting with a specific atom and control it. They posses a blac
PerformIdleBehavior(delta_time) //Do some stupid shit while we have nothing to do
return
var/want_to_move = FALSE
if(current_movement_target && get_dist(pawn, current_movement_target) > max_target_distance) //The distance is out of range
CancelActions()
return
for(var/i in current_behaviors)
var/datum/ai_behavior/current_behavior = i
if(behavior_cooldowns[current_behavior] > world.time) //Still on cooldown
continue
if(current_behavior.behavior_flags & AI_BEHAVIOR_REQUIRE_MOVEMENT && current_movement_target && current_behavior.required_distance < get_dist(pawn, current_movement_target)) //Move closer
want_to_move = TRUE
if(current_behavior.behavior_flags & AI_BEHAVIOR_MOVE_AND_PERFORM) //Move and perform the action
if(current_behavior.behavior_flags & AI_BEHAVIOR_REQUIRE_MOVEMENT && current_movement_target) //Might need to move closer
if(current_behavior.required_distance >= get_dist(pawn, current_movement_target)) ///Are we close enough to engage?
if(ai_movement.moving_controllers[src] == current_movement_target) //We are close enough, if we're moving stop.else
ai_movement.stop_moving_towards(src)
current_behavior.perform(delta_time, src)
else //Perform the action
return
else if(ai_movement.moving_controllers[src] != current_movement_target) //We're too far, if we're not already moving start doing it.
ai_movement.start_moving_towards(src, current_movement_target) //Then start moving
if(current_behavior.behavior_flags & AI_BEHAVIOR_MOVE_AND_PERFORM) //If we can move and perform then do so.
current_behavior.perform(delta_time, src)
return
else //No movement required
current_behavior.perform(delta_time, src)
if(want_to_move)
MoveTo(delta_time) //Need to add some code to check if we can perform the actions now without too much overhead
///Move somewhere using dumb movement (byond base)
/datum/ai_controller/proc/MoveTo(delta_time)
var/current_loc = get_turf(pawn)
var/atom/movable/movable_pawn = pawn
var/turf/target_turf = get_step_towards(movable_pawn, current_movement_target)
if(!is_type_in_typecache(target_turf, GLOB.dangerous_turfs))
movable_pawn.Move(target_turf, get_dir(current_loc, target_turf))
if(get_dist(movable_pawn, current_movement_target) > max_target_distance)
CancelActions()
pathing_attempts = 0
if(current_loc == get_turf(movable_pawn))
if(++pathing_attempts >= MAX_PATHING_ATTEMPTS)
CancelActions()
pathing_attempts = 0
return
///Perform some dumb idle behavior.
@@ -1,5 +1,6 @@
/datum/ai_controller/haunted
movement_delay = 0.4 SECONDS
blackboard = list(BB_TO_HAUNT_LIST = list(),
BB_HAUNT_TARGET,
BB_HAUNTED_THROW_ATTEMPT_COUNT)
@@ -14,9 +15,6 @@
UnregisterSignal(pawn, COMSIG_ITEM_EQUIPPED)
return ..() //Run parent at end
/datum/ai_controller/haunted/able_to_run()
return TRUE
/datum/ai_controller/haunted/SelectBehaviors(delta_time)
current_behaviors = list()
var/obj/item/item_pawn = pawn
+2 -1
View File
@@ -31,6 +31,7 @@
// Strong weapon
else if(target.force > best_force)
living_pawn.drop_all_held_items()
living_pawn.put_in_hands(target)
controller.blackboard[BB_MONKEY_BEST_FORCE_FOUND] = target.force
finish_action(controller, TRUE)
@@ -77,7 +78,7 @@
var/mob/living/living_pawn = controller.pawn
victim.visible_message("<span class='warning'>[living_pawn] starts trying to take [target] from [controller.current_movement_target]!</span>", "<span class='danger'>[living_pawn] tries to take [target]!</span>")
victim.visible_message("<span class='warning'>[living_pawn] starts trying to take [target] from [victim]!</span>", "<span class='danger'>[living_pawn] tries to take [target]!</span>")
controller.blackboard[BB_MONKEY_PICKPOCKETING] = TRUE
+10 -2
View File
@@ -5,6 +5,7 @@ have ways of interacting with a specific mob and control it.
///OOK OOK OOK
/datum/ai_controller/monkey
movement_delay = 0.4 SECONDS
blackboard = list(BB_MONKEY_AGRESSIVE = FALSE,\
BB_MONKEY_BEST_FORCE_FOUND = 0,\
BB_MONKEY_ENEMIES = list(),\
@@ -27,6 +28,7 @@ have ways of interacting with a specific mob and control it.
/datum/ai_controller/monkey/TryPossessPawn(atom/new_pawn)
if(!isliving(new_pawn))
return AI_CONTROLLER_INCOMPATIBLE
var/mob/living/living_pawn = new_pawn
RegisterSignal(new_pawn, COMSIG_PARENT_ATTACKBY, .proc/on_attackby)
RegisterSignal(new_pawn, COMSIG_ATOM_ATTACK_HAND, .proc/on_attack_hand)
RegisterSignal(new_pawn, COMSIG_ATOM_ATTACK_PAW, .proc/on_attack_paw)
@@ -37,11 +39,13 @@ have ways of interacting with a specific mob and control it.
RegisterSignal(new_pawn, COMSIG_LIVING_TRY_SYRINGE, .proc/on_try_syringe)
RegisterSignal(new_pawn, COMSIG_ATOM_HULK_ATTACK, .proc/on_attack_hulk)
RegisterSignal(new_pawn, COMSIG_CARBON_CUFF_ATTEMPTED, .proc/on_attempt_cuff)
RegisterSignal(new_pawn, COMSIG_MOB_MOVESPEED_UPDATED, .proc/update_movespeed)
movement_delay = living_pawn.cached_multiplicative_slowdown
return ..() //Run parent at end
/datum/ai_controller/monkey/UnpossessPawn(destroy)
UnregisterSignal(pawn, list(COMSIG_PARENT_ATTACKBY, COMSIG_ATOM_ATTACK_HAND, COMSIG_ATOM_ATTACK_PAW, COMSIG_ATOM_BULLET_ACT, COMSIG_ATOM_HITBY, COMSIG_MOVABLE_CROSSED, COMSIG_LIVING_START_PULL,\
COMSIG_LIVING_TRY_SYRINGE, COMSIG_ATOM_HULK_ATTACK, COMSIG_CARBON_CUFF_ATTEMPTED))
COMSIG_LIVING_TRY_SYRINGE, COMSIG_ATOM_HULK_ATTACK, COMSIG_CARBON_CUFF_ATTEMPTED, COMSIG_MOB_MOVESPEED_UPDATED))
return ..() //Run parent at end
/datum/ai_controller/monkey/able_to_run()
@@ -116,7 +120,7 @@ have ways of interacting with a specific mob and control it.
for(var/obj/item/i in oview(2, living_pawn))
if(!istype(i))
continue
if(HAS_TRAIT(i, TRAIT_NEEDS_TWO_HANDS) || blackboard[BB_MONKEY_BLACKLISTITEMS][i] || i.force > blackboard[BB_MONKEY_BEST_FORCE_FOUND])
if(HAS_TRAIT(i, TRAIT_NEEDS_TWO_HANDS) || blackboard[BB_MONKEY_BLACKLISTITEMS][i] || i.force < blackboard[BB_MONKEY_BEST_FORCE_FOUND])
continue
W = i
break
@@ -216,3 +220,7 @@ have ways of interacting with a specific mob and control it.
// chance of monkey retaliation
if(prob(MONKEY_CUFF_RETALIATION_PROB))
retaliate(user)
/datum/ai_controller/monkey/proc/update_movespeed(mob/living/pawn)
SIGNAL_HANDLER
movement_delay = pawn.cached_multiplicative_slowdown
+18
View File
@@ -0,0 +1,18 @@
///This datum is an abstract class that can be overriden for different types of movement
/datum/ai_movement
///Assoc list ist of controllers that are currently moving as key, and what they are moving to as value
var/list/moving_controllers = list()
/datum/ai_movement/proc/start_moving_towards(datum/ai_controller/controller, atom/current_movement_target)
controller.pathing_attempts = 0
if(!moving_controllers.len)
START_PROCESSING(SSai_movement, src)
moving_controllers[controller] = current_movement_target
/datum/ai_movement/proc/stop_moving_towards(datum/ai_controller/controller)
controller.pathing_attempts = 0
moving_controllers -= controller
if(!moving_controllers.len)
STOP_PROCESSING(SSai_movement, src)
@@ -0,0 +1,27 @@
///The most braindead type of movement, bee-line to the target with no concern of whats infront of us.
/datum/ai_movement/dumb
///Put your movement behavior in here!
/datum/ai_movement/dumb/process(delta_time)
for(var/datum/ai_controller/controller as anything in moving_controllers)
if(!COOLDOWN_FINISHED(controller, movement_cooldown))
continue
COOLDOWN_START(controller, movement_cooldown, controller.movement_delay)
var/atom/movable/movable_pawn = controller.pawn
if(!isturf(movable_pawn.loc)) //No moving if not on a turf
continue
var/current_loc = get_turf(movable_pawn)
var/turf/target_turf = get_step_towards(movable_pawn, controller.current_movement_target)
if(!is_type_in_typecache(target_turf, GLOB.dangerous_turfs))
movable_pawn.Move(target_turf, get_dir(current_loc, target_turf))
if(current_loc == get_turf(movable_pawn)) //Did we even move after trying to move?
controller.pathing_attempts++
if(controller.pathing_attempts >= MAX_PATHING_ATTEMPTS)
controller.CancelActions()
@@ -10,6 +10,7 @@
if(controller.blackboard[BB_VENDING_BUSY_TILTING])
return
controller.ai_movement.stop_moving_towards(controller)
controller.blackboard[BB_VENDING_BUSY_TILTING] = TRUE
var/turf/target_turf = get_turf(controller.blackboard[BB_VENDING_CURRENT_TARGET])
new /obj/effect/temp_visual/telegraphing/vending_machine_tilt(target_turf)
@@ -1,5 +1,6 @@
///AI controller for vending machine gone rogue, Don't try using this on anything else, it wont work.
/datum/ai_controller/vending_machine
movement_delay = 0.4 SECONDS
blackboard = list(BB_VENDING_CURRENT_TARGET = null,
BB_VENDING_TILT_COOLDOWN = 0,
BB_VENDING_UNTILT_COOLDOWN = 0,