proc
find_path(turf/start, turf/goal)
var/list/frontier = list(start)
var/list/came_from = list()
came_from[start] = null
while(frontier.len)
var/turf/current = frontier[frontier.len]
frontier -= current
for(var/turf/next in range(1, current))
if(!(next in came_from))
frontier += next
came_from[next] = current
var/turf/current = goal
var/list/path = list(current)
while(current != start)
current = came_from[current]
path.Insert(1, current)
return path
Problem description:
I'm going through this great article about pathfinding and recreating each step in DM. The code above implements the Breadth First Search closely following the example code. However, the result it gives has me completely baffled. Take a look at this gif showing what it does with what should be a simple straight line (green being the start, blue the goal, yellow the absurd path):
Can anyone explain why something like this can happen? Thanks!
var/turf/current = frontier[frontier.len]
A breadth-first search would be different. I suggest this modification.
I didn't go over the logic in full, but I think those changes ought to fix it. I made use of associative lists to improve speed.
Of course for best results you'd really want to use something like A*, where you're using a heuristic to determine which node is most worth exploring.