Provide support for locating incomplete paths.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@2936 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Michael Bayne
2004-01-12 05:52:05 +00:00
parent ca89a3c122
commit 7568ae1223
@@ -1,5 +1,5 @@
// //
// $Id: AStarPathUtil.java,v 1.2 2003/05/16 00:49:44 mdb Exp $ // $Id: AStarPathUtil.java,v 1.3 2004/01/12 05:52:05 mdb Exp $
package com.threerings.miso.util; package com.threerings.miso.util;
@@ -58,11 +58,15 @@ public class AStarPathUtil
* @param ay the starting y-position in tile coordinates. * @param ay the starting y-position in tile coordinates.
* @param bx the ending x-position in tile coordinates. * @param bx the ending x-position in tile coordinates.
* @param by the ending y-position in tile coordinates. * @param by the ending y-position in tile coordinates.
* @param partial if true, a partial path will be returned that gets
* us as close as we can to the goal in the event that a complete path
* cannot be located.
* *
* @return the list of points in the path. * @return the list of points in the path.
*/ */
public static List getPath (TraversalPred tpred, Object trav, public static List getPath (TraversalPred tpred, Object trav,
int longest, int ax, int ay, int bx, int by) int longest, int ax, int ay, int bx, int by,
boolean partial)
{ {
Info info = new Info(tpred, trav, longest, bx, by); Info info = new Info(tpred, trav, longest, bx, by);
@@ -76,6 +80,10 @@ public class AStarPathUtil
info.open.add(s); info.open.add(s);
_considered = 1; _considered = 1;
// track the best path
float bestdist = Float.MAX_VALUE;
Node bestpath = null;
// while there are more nodes on the open list // while there are more nodes on the open list
while (info.open.size() > 0) { while (info.open.size() > 0) {
@@ -87,7 +95,13 @@ public class AStarPathUtil
if (n.x == bx && n.y == by) { if (n.x == bx && n.y == by) {
// construct and return the acceptable path // construct and return the acceptable path
return getNodePath(n); return getNodePath(n);
} } else if (partial) {
float pathdist = MathUtil.distance(n.x, n.y, bx, by);
if (pathdist < bestdist) {
bestdist = pathdist;
bestpath = n;
}
}
// consider each successor of the node // consider each successor of the node
considerStep(info, n, n.x - 1, n.y - 1, DIAGONAL_COST); considerStep(info, n, n.x - 1, n.y - 1, DIAGONAL_COST);
@@ -103,6 +117,11 @@ public class AStarPathUtil
info.closed.add(n); info.closed.add(n);
} }
// return the best path we could find if we were asked to do so
if (bestpath != null) {
return getNodePath(bestpath);
}
// no path found // no path found
return null; return null;
} }