mirror of
https://github.com/Bubberstation/Bubberstation.git
synced 2025-12-10 09:42:29 +00:00
* Implements JPS (Jump Point Search) Pathfinding (#56780) So a month or so ago I wanted to make it so dogs in my dog AI PR could path through doors if they had access, and was told I'd need to improve our pathfinding efficiency if I wanted to use full pathfinding for them. Thus, enter JPS, a pathfinding algorithm that allows for massive timesavings in systems with uniform cost grids like ours. This code is still fairly rough and needs polishing, but it's fully functional and already shows massive savings over traditional A*! I plan for this to replace A* as our default pathing method, but I'll leave the A* code in place in case someone ever needs it for whatever reason, like if a specific case needs variable cost pathing. Note that this allows for diagonal pathing instead of the cardinal pathing our A* uses right now, and the current version of the code costs the same to move diagonally as it does to move laterally, which may change later. There's also a lot of dummy/test code in right now in general, but you should still be able to test it out for yourself by spawning a bot like a medibot and using your PDA to summon it. Preliminary Profile Results A preliminary profile is available here. Using one medibot by itself on Metastation, I generated a list of 500 random blob spawn points around the station, gave the medibot all access, then let each algorithm tackle the list. The old A* algorithm took a total of 86 seconds to complete the list and processed 978065 nodes, while JPS took a total of 46 seconds and processed only 100062 nodes, for a 47% decrease in total time and an almost 90% decrease in nodes processed! Why It's Good For The Game Significantly cheaper pathing, which will very much come in handy for the AI datums I'm looking to dig into, what's not to like? * Implements JPS (Jump Point Search) Pathfinding Co-authored-by: Ryll Ryll <3589655+Ryll-Ryll@users.noreply.github.com>
81 lines
1.7 KiB
Plaintext
81 lines
1.7 KiB
Plaintext
//////////////////////
|
|
//datum/heap object
|
|
//////////////////////
|
|
|
|
/datum/heap
|
|
var/list/L
|
|
var/cmp
|
|
|
|
/datum/heap/New(compare)
|
|
L = new()
|
|
cmp = compare
|
|
|
|
/datum/heap/Destroy(force, ...)
|
|
for(var/i in L) // because this is before the list helpers are loaded
|
|
qdel(i)
|
|
L = null
|
|
return ..()
|
|
|
|
/datum/heap/proc/is_empty()
|
|
return !length(L)
|
|
|
|
//insert and place at its position a new node in the heap
|
|
/datum/heap/proc/insert(atom/A)
|
|
|
|
L.Add(A)
|
|
swim(length(L))
|
|
|
|
//removes and returns the first element of the heap
|
|
//(i.e the max or the min dependant on the comparison function)
|
|
/datum/heap/proc/pop()
|
|
if(!length(L))
|
|
return 0
|
|
. = L[1]
|
|
|
|
L[1] = L[length(L)]
|
|
L.Cut(length(L))
|
|
if(length(L))
|
|
sink(1)
|
|
|
|
//Get a node up to its right position in the heap
|
|
/datum/heap/proc/swim(index)
|
|
var/parent = round(index * 0.5)
|
|
|
|
while(parent > 0 && (call(cmp)(L[index],L[parent]) > 0))
|
|
L.Swap(index,parent)
|
|
index = parent
|
|
parent = round(index * 0.5)
|
|
|
|
//Get a node down to its right position in the heap
|
|
/datum/heap/proc/sink(index)
|
|
var/g_child = get_greater_child(index)
|
|
|
|
while(g_child > 0 && (call(cmp)(L[index],L[g_child]) < 0))
|
|
L.Swap(index,g_child)
|
|
index = g_child
|
|
g_child = get_greater_child(index)
|
|
|
|
//Returns the greater (relative to the comparison proc) of a node children
|
|
//or 0 if there's no child
|
|
/datum/heap/proc/get_greater_child(index)
|
|
if(index * 2 > length(L))
|
|
return 0
|
|
|
|
if(index * 2 + 1 > length(L))
|
|
return index * 2
|
|
|
|
if(call(cmp)(L[index * 2],L[index * 2 + 1]) < 0)
|
|
return index * 2 + 1
|
|
else
|
|
return index * 2
|
|
|
|
//Replaces a given node so it verify the heap condition
|
|
/datum/heap/proc/resort(atom/A)
|
|
var/index = L.Find(A)
|
|
|
|
swim(index)
|
|
sink(index)
|
|
|
|
/datum/heap/proc/List()
|
|
. = L.Copy()
|