by KGB » Wed Mar 12, 2014 10:30 pm
SnotlinG,
What makes A* so great is that once you visit a node (square) you never visit it again. The idea is that each time you check a new node (square) it's supposed to be the cheapest path to get to that particular square. So there is no need to check again to see if there is a better way because by definition the first time you visit you have the cheapest path.
For impossible terrain you should not add it to the search path (land units on mountains or sea units on land).
For example consider the following. Stack wants to move from X to Y. Cost of the nodes (A-F plus Y) in brackets. Any node can reach any node above/below it.
X-A(1)-B(1)-C(5)-Y(2)
\D(3)-E(2)-F(2)/
Time1: From X, add (A-1) and (D-3) to the path. Pick the cheapest one (A) from (A,D)
Time2: From A (total cost 1), add (B-2 which is A+B cost), (E-3 which is A+E cost). Pick cheapest one (B) from (B,D,E)
Time3: From B (total cost 2), add (C-7 which is B+C cost), (F-4) which is B+F cost). Pick cheapest one (D) from (D,E,F,C)
Time4: From D (total cost 3) there is nothing new to add (already visited everywhere D can reach so its not in the optimal path). Pick cheapest one (E) from (E,F,C)
Time5: From E (total cost 3) there is nothing to add (already visited everywhere E can reach so its not in the optimal path). Pick cheapest one (F) from (F,C)
Time 6: From F (total cost 4), add (Y-6 which is F+Y cost). Pick cheapest one (Y) from Y,C)
Time 7: Stop, we reached destination. Total cost 6. We never even visited C nor computed paths from many other ways because we didn't need to.
If you don't put impossible squares in the search path (which you shouldn't) then for impossible paths, you will eventually run out of nodes in your path to select from. If that happens and you aren't at Y, then you have an impossible path and can tell the user.
KGB
P.S. I looked quickly at the code. Some of the functions you call, I don't see but I can guess at.
Definitely this call:
adjsquares = getadjacentsquares(node);
Should not return any impossible squares for the stack to move on. It should also not return any square you have already visited prior to this (or you need another function/array that tracks what you visited and ignore already visited squares).
This call
returnarray = move.getcostfor(adjsquares[k], node, pathfindinggroupmovecosttable, mover);
I assume returns the cost for moving to that square. But the full value should be the move cost for the square PLUS the sum of all the path required to get here. That's how you ensure you get the cheapest cost to reach any square along the movement path.
The rest of the tentative logic I won't pretend to understand since I know it involves trying other paths. Which you won't need to do if you track the full cost for each square rather than just the cost to move into it. You also won't need the lowest cost = 10000 stuff either.
And your openset should always be sorted cheapest to most expensive so you start with the cheapest node. Just sort once after you all all the new nodes (squares) to the openset.