migrated robot picker to the new path resolver, #1732

This commit is contained in:
SpaceToad 2014-05-25 16:28:20 +02:00
parent 45db58473e
commit 79ddf23192
7 changed files with 97 additions and 9 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) < 10) {
if (getDistance(target.posX, target.posY, target.posZ) < 1) {
setLaserDestination((float) target.posX, (float) target.posY, (float) target.posZ);
showLaser();
pickTracker = new SafeTimeTracker (200);
@ -145,7 +145,7 @@ public class EntityRobotPicker extends EntityRobot implements IInventory {
target = item;
targettedItems.add(e.getEntityId());
setMainAI(new RobotAIMoveAround(this, (float) e.posX,
setMainAI(new RobotAIMoveTo(this, (float) e.posX,
(float) e.posY, (float) e.posZ));
pickTime = -1;
@ -247,5 +247,7 @@ public class EntityRobotPicker extends EntityRobot implements IInventory {
for (int i = 0; i < inv.length; ++i) {
inv [i] = ItemStack.loadItemStackFromNBT(nbt.getCompoundTag("inv[" + i + "]"));
}
setDead();
}
}

View file

@ -16,6 +16,8 @@ public class RobotAIDocked extends RobotAIBase {
@Override
public void updateTask() {
super.updateTask();
robot.motionX = 0;
robot.motionY = 0;
robot.motionZ = 0;

View file

@ -34,6 +34,8 @@ public class RobotAIMoveAround extends RobotAIBase {
@Override
public void updateTask() {
super.updateTask();
double distance = robot.getDistance(destX, destY, destZ);
if (distance >= prevDistance) {

View file

@ -0,0 +1,79 @@
/**
* 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;
import net.minecraft.nbt.NBTTagCompound;
import buildcraft.core.BlockIndex;
import buildcraft.core.utils.PathFinding;
public class RobotAIMoveTo extends RobotAIBase {
private static int PATH_ITERATIONS = 1000;
private PathFinding pathSearch;
private LinkedList<BlockIndex> path;
private double prevDistance = Double.MAX_VALUE;
public RobotAIMoveTo(EntityRobot robot) {
super(robot);
}
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);
}
@Override
public void updateTask() {
super.updateTask();
if (path != null) {
double distance = robot.getDistance(destX, destY, destZ);
if (distance > prevDistance) {
setNextInPath();
} else {
prevDistance = robot.getDistance(destX, destY, destZ);
}
} else if (!pathSearch.isDone()) {
pathSearch.iterate(PATH_ITERATIONS);
} else {
path = pathSearch.getResult();
setNextInPath();
}
}
private void setNextInPath() {
if (path.size() > 0) {
BlockIndex next = path.removeFirst();
setDestination(robot, next.x + 0.5F, next.y + 0.5F, next.z + 0.5F);
prevDistance = Double.MAX_VALUE;
}
}
@Override
public void writeToNBT(NBTTagCompound nbt) {
super.writeToNBT(nbt);
}
@Override
public void readFromNBT(NBTTagCompound nbt) {
super.readFromNBT(nbt);
}
}

View file

@ -22,6 +22,8 @@ public class RobotAIReturnToDock extends RobotAIBase {
@Override
public void updateTask() {
super.updateTask();
if (phase == 0) {
float x = robot.dockingStation.x + 0.5F + robot.dockingStation.side.offsetX * 1.5F;
float y = robot.dockingStation.y + 0.5F + robot.dockingStation.side.offsetY * 1.5F;

View file

@ -11,6 +11,7 @@ package buildcraft.core.utils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import net.minecraft.world.IBlockAccess;
@ -33,7 +34,7 @@ public class PathFinding {
private Node nextIteration;
private ArrayList<BlockIndex> result;
private LinkedList<BlockIndex> result;
public PathFinding(IBlockAccess iWorld, BlockIndex iStart, BlockIndex iEnd) {
world = iWorld;
@ -57,10 +58,10 @@ public class PathFinding {
for (int i = 0; i < itNumber; ++i) {
if (nextIteration.index.equals(end)) {
result = new ArrayList<BlockIndex>();
result = new LinkedList<BlockIndex>();
while (nextIteration != null) {
result.add(nextIteration.index);
result.addFirst(nextIteration.index);
nextIteration = nextIteration.parent;
}
@ -75,11 +76,11 @@ public class PathFinding {
return nextIteration == null;
}
public ArrayList<BlockIndex> getResult() {
public LinkedList<BlockIndex> getResult() {
if (result != null) {
return result;
} else {
return new ArrayList<BlockIndex>();
return new LinkedList<BlockIndex>();
}
}

View file

@ -1,6 +1,6 @@
package buildcraft.tests;
import java.util.ArrayList;
import java.util.LinkedList;
import net.minecraft.init.Blocks;
import net.minecraft.tileentity.TileEntity;
@ -31,7 +31,7 @@ public class TileTestPathfinding extends TileEntity {
p.iterate(10000);
ArrayList<BlockIndex> r = p.getResult();
LinkedList<BlockIndex> r = p.getResult();
for (BlockIndex b : r) {
worldObj.setBlock(b.x, b.y, b.z, Blocks.sponge);