Files
Bubberstation/code/datums/ai/generic/find_and_set.dm
CabinetOnFire af8f69da13 Adds "Event Logging", or EVLogging, A new debug system that allows us to track individual datums and log events on a timeline (#96035)
## About The Pull Request

This Pull Request adds a new logging system that uses a timeline to
track and visualize important events for specific datums.

This is done via a new window in which you can select a datum for
tracking, which adds it to the timeline. If this datum implements the
EVLOGGING macros, it can track important events onto this timeline. As
an example, we can log whenever an AI is deciding to make a new path, if
it decides to generate a new decisionmaking plan, it finishes an action,
or it decides to target someone/something.

We can select these events to see more information, and optionally get a
snapshot of important variables at the time this event was logged (like
the blackboard and current plan for AI controllers).

You can also filter out specific events / track info, which is done via
categories. Each event / piece of track info is given a category and if
you disable a category all events / track info in that category is
hidden. This lets you filter out things you might not care about.

<img width="2346" height="1209" alt="image"
src="https://github.com/user-attachments/assets/0763077c-e349-4c7c-b017-23d29e1d089b"
/>

_whoever thinks we didnt need advanced cleanbot logging is a noob_


In the video below I showcase how this works;
https://file.house/7nsOiqdvmSTxlsk3fs-e8g==.mp4
A cleanbot is roaming the halls, I turn on the event logger, click the
"pick target" button and click on the datum I'd like to track (the
cleanbot). This results in the cleanbot now tracking its events. I spawn
some dirt and the cleanbot decides to clean it, and I go through the
events; You can see theres different events being listed, such as when
the cleanbot starts targetting the dirt, when it cleans plan, when it
makes it JPS path and every time it moves over it.


The macros I've currently implemented are as follows:

**EVLOG_TEXT(DATUM, CATEGORY, INFO)**
Only adds text to the event logger window, no world-visuals

EVLOG_LOCATION(DATUM, CATEGORY, INFO, TURF) 
Adds text to the event logger and adds an image to where that turf is.

EVLOG_TURFS(DATUM, CATEGORY, INFO, TURFS)
Adds text to the event logger and adds an image to each turf in the
TURFS list

EVLOG_LINES(DATUM, CATEGORY, INFO, TURF_A, TURF_B)
Adds text to the event logger and adds a line from turf_a to turf_B

EVLOG_PATH(DATUM, CATEGORY, INFO, TURFS)
Adds text to the event logger and visualizes a path from A to B (same
way as the pathfinding debugger, of which I moved the visualization
before to SSPathfinder)

In terms of performance, the logger is a singleton, and events are ONLY
logged if
1. The logger is running
2. The datum has the DF_EVLOGGING flag.

This means most of the time, logging an event is a single var lookup
(Since the runner is off by default). The DF_EVLOGGING flag is off by
default as well and has to be enabled by the event logger, or set
temporarily by a dev in code.

This system can easily be extended with more event types / visualization
types as well. (I'm thinking of datumizing the ones I have now)

The TGUI is still a bit of a mess, I would love some pointers because
I'm not really good at react so I just kind of hit it with a hammer
until it did what I wanted 😎

Also, all of this is based on VisLogging from Unreal Engine, so it will
have some likeness https://unreal-garden.com/tutorials/visual-logger/

## Why It's Good For The Game

This system allows us to debug more complex systems (like basic AI) in
an understandable and clear way. While the implementation cases are not
super common right now, extending this system could make debugging these
systems much more comprehensible, and hopefully lets more developers
help us with improving these systems. (plus, we LOVE timelines)

## Changelog

🆑 CabinetOnFire
refactor: Implements "Event Logging" an improved way for programmers to
debug specific datums.
/🆑

---------

Co-authored-by: Lucy <lucy@absolucy.moe>
2026-05-13 12:37:56 +00:00

224 lines
8.8 KiB
Plaintext

/**find and set
* Finds an item near themselves, sets a blackboard key as it. Very useful for ais that need to use machines or something.
* if you want to do something more complicated than find a single atom, change the search_tactic() proc
* cool tip: search_tactic() can set lists
*/
/datum/ai_behavior/find_and_set
action_cooldown = 2 SECONDS
/datum/ai_behavior/find_and_set/perform(seconds_per_tick, datum/ai_controller/controller, set_key, locate_path, search_range)
if (controller.blackboard_key_exists(set_key))
return AI_BEHAVIOR_DELAY | AI_BEHAVIOR_SUCCEEDED
if(QDELETED(controller.pawn))
return AI_BEHAVIOR_DELAY | AI_BEHAVIOR_SUCCEEDED
var/find_this_thing = search_tactic(controller, locate_path, search_range)
if(isnull(find_this_thing))
return AI_BEHAVIOR_DELAY | AI_BEHAVIOR_FAILED
EVLOG_MAPTEXT(controller, EVLOG_CATEGORY_AI_TARGETING, "[controller.pawn] has selected [find_this_thing] as a target for blackboard key [set_key]! Behavior: [src]", get_turf(find_this_thing), "Target: [find_this_thing]")
EVLOG_LINES(controller, EVLOG_CATEGORY_AI_TARGETING, "Line to target", get_turf(controller.pawn), get_turf(find_this_thing))
controller.set_blackboard_key(set_key, find_this_thing)
return AI_BEHAVIOR_DELAY | AI_BEHAVIOR_SUCCEEDED
/datum/ai_behavior/find_and_set/proc/search_tactic(datum/ai_controller/controller, locate_path, search_range = 3)
return locate(locate_path) in oview(search_range, controller.pawn)
/**
* Variant of find and set that fails if the living pawn doesn't hold something
*/
/datum/ai_behavior/find_and_set/pawn_must_hold_item
/datum/ai_behavior/find_and_set/pawn_must_hold_item/search_tactic(datum/ai_controller/controller)
var/mob/living/living_pawn = controller.pawn
if(!living_pawn.get_num_held_items())
return //we want to fail the search if we don't have something held
return ..()
/**
* Variant of find and set that also requires the item to be edible. checks hands too
*/
/datum/ai_behavior/find_and_set/food_or_drink
var/force_find_drinks = FALSE
/datum/ai_behavior/find_and_set/food_or_drink/search_tactic(datum/ai_controller/controller, locate_path, search_range = SEARCH_TACTIC_DEFAULT_RANGE)
var/mob/living/living_pawn = controller.pawn
var/find_drinks = force_find_drinks || controller.blackboard[BB_IGNORE_DRINKS] || FALSE
for(var/atom/held_candidate in living_pawn.held_items)
if(is_food_or_drink(controller, held_candidate, find_drinks))
return held_candidate
for(var/atom/local_candidate in oview(search_range, controller.pawn))
if(is_food_or_drink(controller, local_candidate, find_drinks) && istype(local_candidate, locate_path))
return local_candidate
return null
/datum/ai_behavior/find_and_set/food_or_drink/proc/is_food_or_drink(datum/ai_controller/controller, obj/item/thing, find_drinks = FALSE)
return is_food(thing) || (find_drinks && is_drink(thing))
/datum/ai_behavior/find_and_set/food_or_drink/proc/is_food(obj/item/thing)
if(IS_EDIBLE(thing))
return TRUE
if(istype(thing, /obj/item/reagent_containers/cup/bowl))
return thing.reagents.total_volume > 0
return FALSE
/datum/ai_behavior/find_and_set/food_or_drink/proc/is_drink(obj/item/thing)
if(istype(thing, /obj/item/reagent_containers/cup/glass))
return thing.reagents.total_volume > 0
return FALSE
/datum/ai_behavior/find_and_set/food_or_drink/to_eat
/datum/ai_behavior/find_and_set/food_or_drink/to_serve
force_find_drinks = TRUE
/**
* Variant of find and set that only checks in hands, search range should be excluded for this
*/
/datum/ai_behavior/find_and_set/in_hands
/datum/ai_behavior/find_and_set/in_hands/search_tactic(datum/ai_controller/controller, locate_path)
var/mob/living/living_pawn = controller.pawn
return locate(locate_path) in living_pawn.held_items
/datum/ai_behavior/find_and_set/in_hands/given_list
/datum/ai_behavior/find_and_set/in_hands/given_list/search_tactic(datum/ai_controller/controller, locate_paths)
var/list/found = typecache_filter_list(controller.pawn, locate_paths)
if(length(found))
return pick(found)
/**
* Variant of find and set that takes a list of things to find.
*/
/datum/ai_behavior/find_and_set/in_list
/datum/ai_behavior/find_and_set/in_list/search_tactic(datum/ai_controller/controller, locate_paths, search_range = SEARCH_TACTIC_DEFAULT_RANGE)
var/list/found = typecache_filter_list(oview(search_range, controller.pawn), locate_paths)
if(length(found))
return pick(found)
/// Like find_and_set/in_list, but we return the turf location of the item instead of the item itself.
/datum/ai_behavior/find_and_set/in_list/turf_location
/datum/ai_behavior/find_and_set/in_list/turf_location/search_tactic(datum/ai_controller/controller, locate_paths, search_range)
. = ..()
if(isnull(.))
return null
return get_turf(.)
/**
* Variant of find and set which returns an object which can be animated with a staff of change
*/
/datum/ai_behavior/find_and_set/animatable
/datum/ai_behavior/find_and_set/animatable/search_tactic(datum/ai_controller/controller, locate_path, search_range = SEARCH_TACTIC_DEFAULT_RANGE)
var/mob/living/living_pawn = controller.pawn
var/list/nearby_items = list()
for (var/obj/new_friend in oview(search_range, controller.pawn))
if (!isitem(new_friend) && !isstructure(new_friend))
continue
if (is_type_in_list(new_friend, GLOB.animatable_blacklist))
continue
if (living_pawn.see_invisible < new_friend.invisibility)
continue
nearby_items += new_friend
if(nearby_items.len)
return pick(nearby_items)
/**
* Variant of find and set which returns the nearest wall which isn't invulnerable
*/
/datum/ai_behavior/find_and_set/nearest_wall
/datum/ai_behavior/find_and_set/nearest_wall/search_tactic(datum/ai_controller/controller, locate_path, search_range = SEARCH_TACTIC_DEFAULT_RANGE)
var/mob/living/living_pawn = controller.pawn
var/list/nearby_walls = list()
for (var/turf/closed/new_wall in oview(search_range, controller.pawn))
if (isindestructiblewall(new_wall))
continue
nearby_walls += new_wall
if(nearby_walls.len)
return get_closest_atom(/turf/closed/, nearby_walls, living_pawn)
/**
* Variant of find and set which returns corpses who share your faction
*/
/datum/ai_behavior/find_and_set/friendly_corpses
/datum/ai_behavior/find_and_set/friendly_corpses/search_tactic(datum/ai_controller/controller, locate_path, search_range = SEARCH_TACTIC_DEFAULT_RANGE)
var/mob/living/living_pawn = controller.pawn
var/list/nearby_bodies = list()
for (var/mob/living/dead_pal in oview(search_range, controller.pawn))
if (!isturf(dead_pal.loc))
continue
if (!dead_pal.stat || dead_pal.health > 0)
continue
if (living_pawn.see_invisible < dead_pal.invisibility)
continue
if (!living_pawn.faction_check_atom(dead_pal))
continue
nearby_bodies += dead_pal
if (nearby_bodies.len)
return pick(nearby_bodies)
/**
* A variant that looks for a human who is not dead or incapacitated, and has a mind
*/
/datum/ai_behavior/find_and_set/conscious_person
/datum/ai_behavior/find_and_set/conscious_person/search_tactic(datum/ai_controller/controller, locate_path, search_range = SEARCH_TACTIC_DEFAULT_RANGE)
var/list/customers = list()
for(var/mob/living/carbon/human/target in oview(search_range, controller.pawn))
if(IS_DEAD_OR_INCAP(target) || !target.mind)
continue
customers += target
if(customers.len)
return pick(customers)
return null
/datum/ai_behavior/find_and_set/nearby_friends
action_cooldown = 2 SECONDS
/datum/ai_behavior/find_and_set/nearby_friends/search_tactic(datum/ai_controller/controller, locate_path, search_range = SEARCH_TACTIC_DEFAULT_RANGE)
var/atom/friend = locate(/mob/living/carbon/human) in oview(search_range, controller.pawn)
if(isnull(friend))
return null
var/mob/living/living_pawn = controller.pawn
var/potential_friend = living_pawn.has_ally(friend) ? friend : null
return potential_friend
/datum/ai_behavior/find_and_set/in_list/turf_types
/datum/ai_behavior/find_and_set/in_list/turf_types/search_tactic(datum/ai_controller/controller, locate_paths, search_range = SEARCH_TACTIC_DEFAULT_RANGE)
var/list/found = RANGE_TURFS(search_range, controller.pawn)
shuffle_inplace(found)
for(var/turf/possible_turf as anything in found)
if(!is_type_in_typecache(possible_turf, locate_paths))
continue
if(can_see(controller.pawn, possible_turf, search_range))
return possible_turf
return null
/datum/ai_behavior/find_and_set/in_list/closest_turf
/datum/ai_behavior/find_and_set/in_list/closest_turf/search_tactic(datum/ai_controller/controller, locate_paths, search_range = SEARCH_TACTIC_DEFAULT_RANGE)
var/list/found = RANGE_TURFS(search_range, controller.pawn)
for(var/turf/possible_turf as anything in found)
if(!is_type_in_typecache(possible_turf, locate_paths) || !can_see(controller.pawn, possible_turf, search_range))
found -= possible_turf
return (length(found)) ? get_closest_atom(/turf, found, controller.pawn) : null