Files
Bubberstation/code/controllers/subsystem/pathfinder.dm
SkyratBot 83e3915015 [MIRROR] Makes some improvements to how AI can use JPS with movement loops [MDB IGNORE] (#19151)
* Makes some improvements to how AI can use JPS with movement loops (#72685)

## About The Pull Request

This PR makes some changes to how JPS is used in movement loops, as it
was causing a variety of issues:

- Fixed some code where JPS would fail because the path is still being
made. Instead, the movement loop will now wait.
- Reduced the subsystem wait for the pathfinder subsystem from 2 seconds
to 0.1 seconds. @ LemonInTheDark told me that this is better, I'll update
this with a better explanation once I squeeze it out of him :D
- Allows you to provide an initial path to the movement loop, in case
you pre-calculated one while making a plan.

## Why It's Good For The Game

Makes working with JPS a bit easier when making AI.

---------

Co-authored-by: Capybara <Capybara@ CapybaraMailingServices.com>
Co-authored-by: Jeremiah <42397676+jlsnow301@ users.noreply.github.com>
Co-authored-by: LemonInTheDark <58055496+LemonInTheDark@ users.noreply.github.com>

* Makes some improvements to how AI can use JPS with movement loops

---------

Co-authored-by: CapybaraExtravagante <110635252+CapybaraExtravagante@users.noreply.github.com>
Co-authored-by: Capybara <Capybara@ CapybaraMailingServices.com>
Co-authored-by: Jeremiah <42397676+jlsnow301@ users.noreply.github.com>
Co-authored-by: LemonInTheDark <58055496+LemonInTheDark@ users.noreply.github.com>
2023-02-08 02:04:31 +00:00

48 lines
1.8 KiB
Plaintext

/// Queues and manages JPS pathfinding steps
SUBSYSTEM_DEF(pathfinder)
name = "Pathfinder"
init_order = INIT_ORDER_PATH
priority = FIRE_PRIORITY_PATHFINDING
wait = 0.5
/// List of pathfind datums we are currently trying to process
var/list/datum/pathfind/active_pathing = list()
/// List of pathfind datums being ACTIVELY processed. exists to make subsystem stats readable
var/list/datum/pathfind/currentrun = list()
var/static/space_type_cache
/datum/controller/subsystem/pathfinder/Initialize()
space_type_cache = typecacheof(/turf/open/space)
return SS_INIT_SUCCESS
/datum/controller/subsystem/pathfinder/stat_entry(msg)
msg = "P:[length(active_pathing)]"
return ..()
// This is another one of those subsystems (hey lighting) in which one "Run" means fully processing a queue
// We'll use a copy for this just to be nice to people reading the mc panel
/datum/controller/subsystem/pathfinder/fire(resumed)
if(!resumed)
src.currentrun = active_pathing.Copy()
// Dies of sonic speed from caching datum var reads
var/list/currentrun = src.currentrun
while(length(currentrun))
var/datum/pathfind/path = currentrun[length(currentrun)]
if(!path.search_step()) // Something's wrong
path.early_exit()
currentrun.len--
continue
if(MC_TICK_CHECK)
return
path.finished()
// Next please
currentrun.len--
/// Initiates a pathfind. Returns true if we're good, FALSE if something's failed
/datum/controller/subsystem/pathfinder/proc/pathfind(atom/movable/caller, atom/end, max_distance = 30, mintargetdist, id=null, simulated_only = TRUE, turf/exclude, skip_first=TRUE, diagonal_safety=TRUE, datum/callback/on_finish)
var/datum/pathfind/path = new(caller, end, id, max_distance, mintargetdist, simulated_only, exclude, skip_first, diagonal_safety, on_finish)
if(path.start())
active_pathing += path
return TRUE
return FALSE