progress with robot AI and fixed pathfinding, #1732

This commit is contained in:
SpaceToad 2014-05-25 17:08:53 +02:00
parent 79ddf23192
commit 5060da651f
6 changed files with 93 additions and 26 deletions

View file

@ -71,7 +71,7 @@ public class EntityRobotPicker extends EntityRobot implements IInventory {
hideLaser();
scan ();
} else if (pickTime == -1) {
if (getDistance(target.posX, target.posY, target.posZ) < 1) {
if (currentAI.isDone()) {
setLaserDestination((float) target.posX, (float) target.posY, (float) target.posZ);
showLaser();
pickTracker = new SafeTimeTracker (200);

View file

@ -65,4 +65,8 @@ public class RobotAIBase extends EntityAIBase {
public boolean shouldExecute() {
return true;
}
public boolean isDone() {
return false;
}
}

View file

@ -0,0 +1,49 @@
/**
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.core.robots;
import java.util.LinkedList;
public class RobotAIComposite extends RobotAIBase {
LinkedList<RobotAIBase> cpts = new LinkedList<RobotAIBase>();
public RobotAIComposite(EntityRobot iRobot, RobotAIBase... icpts) {
super(iRobot);
for (RobotAIBase ai : icpts) {
cpts.add(ai);
}
}
@Override
public void updateTask() {
if (cpts.size() > 0) {
if (cpts.getFirst().isDone()) {
cpts.removeFirst();
} else {
cpts.getFirst().updateTask();
}
}
}
@Override
public boolean shouldExecute() {
if (cpts.size() > 0) {
return cpts.getFirst().shouldExecute();
} else {
return true;
}
}
@Override
public boolean isDone() {
return cpts.size() == 0;
}
}

View file

@ -22,35 +22,45 @@ public class RobotAIMoveTo extends RobotAIBase {
private PathFinding pathSearch;
private LinkedList<BlockIndex> path;
private double prevDistance = Double.MAX_VALUE;
public RobotAIMoveTo(EntityRobot robot) {
super(robot);
}
private float dx, dy, dz;
public RobotAIMoveTo (EntityRobot robot, float x, float y, float z) {
super(robot);
pathSearch = new PathFinding
(robot.worldObj,
new BlockIndex((int) Math.floor(robot.posX), (int) Math.floor(robot.posY),
(int) Math.floor(robot.posZ)),
new BlockIndex((int) Math.floor(x), (int) Math.floor(y), (int) Math.floor(z)));
pathSearch.iterate(PATH_ITERATIONS);
dx = x;
dy = y;
dz = z;
}
@Override
public void updateTask() {
super.updateTask();
if (path != null) {
if (pathSearch == null) {
pathSearch = new PathFinding
(robot.worldObj,
new BlockIndex((int) Math.floor(robot.posX), (int) Math.floor(robot.posY),
(int) Math.floor(robot.posZ)),
new BlockIndex((int) Math.floor(dx), (int) Math.floor(dy), (int) Math.floor(dz)));
pathSearch.iterate(PATH_ITERATIONS);
} else if (path != null) {
double distance = robot.getDistance(destX, destY, destZ);
if (distance > prevDistance) {
if (path.size() > 0) {
path.removeFirst();
}
setNextInPath();
} else {
prevDistance = robot.getDistance(destX, destY, destZ);
}
if (path.size() == 0) {
robot.motionX = 0;
robot.motionY = 0;
robot.motionZ = 0;
}
} else if (!pathSearch.isDone()) {
pathSearch.iterate(PATH_ITERATIONS);
} else {
@ -61,7 +71,7 @@ public class RobotAIMoveTo extends RobotAIBase {
private void setNextInPath() {
if (path.size() > 0) {
BlockIndex next = path.removeFirst();
BlockIndex next = path.getFirst();
setDestination(robot, next.x + 0.5F, next.y + 0.5F, next.z + 0.5F);
prevDistance = Double.MAX_VALUE;
}
@ -76,4 +86,9 @@ public class RobotAIMoveTo extends RobotAIBase {
public void readFromNBT(NBTTagCompound nbt) {
super.readFromNBT(nbt);
}
@Override
public boolean isDone() {
return path != null && path.size() == 0;
}
}

View file

@ -8,19 +8,22 @@
*/
package buildcraft.core.robots;
import net.minecraft.nbt.NBTTagCompound;
public class RobotAIReturnToDock extends RobotAIBase {
public class RobotAIReturnToDock extends RobotAIComposite {
double prevDistance = Double.MAX_VALUE;
int phase = 0;
public RobotAIReturnToDock(EntityRobot iRobot) {
super(iRobot);
super(iRobot,
new RobotAIMoveTo(iRobot,
iRobot.dockingStation.x + iRobot.dockingStation.side.offsetX * 2,
iRobot.dockingStation.y + 0.5F + iRobot.dockingStation.side.offsetY * 1.5F,
iRobot.dockingStation.z + 0.5F + iRobot.dockingStation.side.offsetZ * 1.5F));
}
@Override
/*@Override
public void updateTask() {
super.updateTask();
@ -77,5 +80,5 @@ public class RobotAIReturnToDock extends RobotAIBase {
super.readFromNBT(nbt);
phase = nbt.getInteger("phase");
}
}*/
}

View file

@ -129,13 +129,9 @@ public class PathFinding {
}
}
Node bestMatch = findSmallerWeight(nodes);
nodes.addAll(openList.values());
if (bestMatch == null) {
bestMatch = findSmallerWeight(openList.values());
}
return bestMatch;
return findSmallerWeight(nodes);
}
private Node findSmallerWeight(Collection<Node> collection) {