Added a limit to block scanning. For #1873

This commit is contained in:
SpaceToad 2014-06-23 08:32:33 +02:00
parent afc9ce619e
commit 7c9993568c
2 changed files with 27 additions and 2 deletions

View file

@ -33,7 +33,7 @@ public class AIRobotSearchBlock extends AIRobot {
@Override
public void start() {
blockScanner = new PathFinding(robot.worldObj, new BlockIndex(robot), pathFound);
blockScanner = new PathFinding(robot.worldObj, new BlockIndex(robot), pathFound, 64);
blockScannerJob = new PathFindingJob(blockScanner);
blockScannerJob.start();
}

View file

@ -31,6 +31,8 @@ public class PathFinding {
private BlockIndex start;
private BlockIndex end;
private IPathFound pathFound;
private float maxDistance = -1;
private float sqrMaxDistance = -1;
private HashMap<BlockIndex, Node> openList = new HashMap<BlockIndex, PathFinding.Node>();
private HashMap<BlockIndex, Node> closedList = new HashMap<BlockIndex, PathFinding.Node>();
@ -56,7 +58,7 @@ public class PathFinding {
nextIteration = startNode;
}
public PathFinding(World iWorld, BlockIndex iStart, IPathFound iPathFound) {
public PathFinding(World iWorld, BlockIndex iStart, IPathFound iPathFound, float iMaxDistance) {
world = iWorld;
start = iStart;
pathFound = iPathFound;
@ -69,6 +71,8 @@ public class PathFinding {
startNode.index = iStart;
openList.put(start, startNode);
nextIteration = startNode;
maxDistance = iMaxDistance;
sqrMaxDistance = maxDistance * maxDistance;
}
public void iterate() {
@ -338,6 +342,27 @@ public class PathFinding {
resultMoves[2][2][2] = 0;
}
if (maxDistance > 0) {
for (int dx = -1; dx <= +1; ++dx) {
for (int dy = -1; dy <= +1; ++dy) {
for (int dz = -1; dz <= +1; ++dz) {
int x = from.index.x + dx;
int y = from.index.y + dy;
int z = from.index.z + dz;
float distX = x - start.x;
float distY = y - start.y;
float distZ = z - start.z;
float sqrDist = distX * distX + distY * distY + distZ * distZ;
if (sqrDist > sqrMaxDistance) {
resultMoves[dx + 1][dy + 1][dz + 1] = 0;
}
}
}
}
}
return resultMoves;
}