You don't calculate distances with flood fill. You just add 1 as you radiate out from the target. It assumes only horizontal and vertical movement though. To make it accurate it would have to add 1.4 diagonally. This makes things more complicated because the list wont remain in order just radiating out. You would want to avoid sorting but I think if you work out the right way to do it you could keep it in order without having to sort. I've been working on it. Here is my pseudo code. I'm using horizontal/vertical distance as 10 and diagonal as 14 because I assume using ints is better than using floats.
I don't think there is anything there that is to complex so I'm hoping it wont take long to do the whole map. Then instead of using the Manhattan distance for the heuristics ie. abs(startNode.x-endNode.x) + abs(startNode.z-endNode.z) you can use the value in the closed list cl[endNode.x, endNode.y].
On another note I think I found in the vanilla pathfinding where it uses the Manhattan distance.
Technically speaking the Manhattan distance assumes only vertical and horizontal movement. It should really be something like
x = abs(startNode.x-endNode.x)
y = abs(startNode.y-endNode.y)
max = Max(x, y)
min = Min (x, y)
heuristic = max - min + (min * 1.4)
It probably could be done more elegantly than that but I wonder what difference just this one change would make.
Code Select
Starting with an open list of nodes, let's call it ol, with x,y and dist value
And closed list that is an array for easy reference cl[x,y]=dist
Assume ol has function add (int x, int y, int distance)
ol.add (endNode.x, endNode.y, 0)
while ol. count != 0
curDist = ol.getFirstItem.dist
' Do horizontal and vertical neighbors with dist values from curDist to curDist + 4
for each node in ol do
if node.dist <= curDist + 4 then
Get horizontal and vertical neighbours that are not on closed list or blocked
Add them to open list adding dist as curDist + 10
else
break
end if
end for
' Do diagonal neighbours for curDist only. Remove from open list as you do them.
while ol.getFirstItem.dist = curDist
Get diagonal neighbors that are not on closed list or blocked
Add them to open list adding dist as curDist + 14
remove first node from open list and add to closed list
end while
end while I don't think there is anything there that is to complex so I'm hoping it wont take long to do the whole map. Then instead of using the Manhattan distance for the heuristics ie. abs(startNode.x-endNode.x) + abs(startNode.z-endNode.z) you can use the value in the closed list cl[endNode.x, endNode.y].
On another note I think I found in the vanilla pathfinding where it uses the Manhattan distance.
Code Select
this.h = this.heuristicStrength * (Mathf.Abs((int)this.neighX - this.destinationX) + Mathf.Abs((int)this.neighZ - this.destinationZ));Technically speaking the Manhattan distance assumes only vertical and horizontal movement. It should really be something like
x = abs(startNode.x-endNode.x)
y = abs(startNode.y-endNode.y)
max = Max(x, y)
min = Min (x, y)
heuristic = max - min + (min * 1.4)
It probably could be done more elegantly than that but I wonder what difference just this one change would make.