lumberjack robot now manages its axe and corresponding damage, #1869

This commit is contained in:
SpaceToad 2014-06-09 17:40:56 +02:00
parent 0d978677de
commit 1605a36c03
12 changed files with 271 additions and 51 deletions

View file

@ -0,0 +1,24 @@
/**
* 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.robots;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.transport.TileGenericPipe;
public class DockingStation {
public TileGenericPipe pipe;
public ForgeDirection side;
public DockingStation(TileGenericPipe iPipe, ForgeDirection iSide) {
pipe = iPipe;
side = iSide;
}
}

View file

@ -0,0 +1,83 @@
/**
* 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.robots;
import java.util.Collection;
import java.util.HashMap;
import net.minecraftforge.common.util.ForgeDirection;
public class DockingStationRegistry {
private static HashMap<StationIndex, DockingStation> stations = new HashMap<StationIndex, DockingStation>();
private static class StationIndex {
public int x, y, z;
public ForgeDirection side;
public StationIndex(int ix, int iy, int iz, ForgeDirection iSide) {
// TODO: should probably consider dimension id here too
x = ix;
y = iy;
z = iz;
side = iSide;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof StationIndex) {
StationIndex d = (StationIndex) obj;
return d.x == x && d.y == y && d.z == z && d.side == side;
}
return super.equals(obj);
}
@Override
public int hashCode() {
return ((x * 37 + y) * 37 + z * 37) + side.ordinal();
}
@Override
public String toString() {
return "{" + x + ", " + y + ", " + z + ", " + side + "}";
}
}
public static DockingStation getStation(int x, int y, int z, ForgeDirection side) {
StationIndex index = new StationIndex(x, y, z, side);
if (stations.containsKey(index)) {
return stations.get(index);
} else {
return null;
}
}
public static Collection<DockingStation> getStations() {
return stations.values();
}
public static void registerStation(DockingStation station) {
stations.put(toIndex(station), station);
}
public static void removeStation(DockingStation station) {
StationIndex index = toIndex(station);
if (stations.containsKey(index)) {
stations.remove(index);
}
}
private static StationIndex toIndex(DockingStation station) {
return new StationIndex(station.pipe.xCoord, station.pipe.yCoord, station.pipe.zCoord, station.side);
}
}

View file

@ -35,7 +35,8 @@ import buildcraft.core.network.RPC;
import buildcraft.core.network.RPCHandler;
import buildcraft.core.network.RPCMessageInfo;
import buildcraft.core.network.RPCSide;
import buildcraft.transport.TileGenericPipe;
import buildcraft.robots.DockingStation;
import buildcraft.robots.DockingStationRegistry;
public class EntityRobot extends EntityLiving implements
IEntityAdditionalSpawnData, IInventory {
@ -55,7 +56,8 @@ public class EntityRobot extends EntityLiving implements
public LaserData laser = new LaserData ();
public IRobotTask currentTask;
public DockingStation dockingStation = new DockingStation();
public DockingStation currentDockingStation;
public DockingStation mainDockingStation;
public boolean isDocked = false;
public IRedstoneBoardRobot board;
@ -75,11 +77,6 @@ public class EntityRobot extends EntityLiving implements
private String boardID;
private ResourceLocation texture;
public class DockingStation {
public int x, y, z;
public ForgeDirection side;
}
public EntityRobot(World world, IRedstoneBoardRobot iBoard) {
this(world);
@ -326,10 +323,12 @@ public class EntityRobot extends EntityLiving implements
public void writeEntityToNBT(NBTTagCompound nbt) {
super.writeEntityToNBT(nbt);
nbt.setInteger("dockX", dockingStation.x);
nbt.setInteger("dockY", dockingStation.y);
nbt.setInteger("dockZ", dockingStation.z);
nbt.setInteger("dockSide", dockingStation.side.ordinal());
if (mainDockingStation != null) {
nbt.setInteger("dockX", mainDockingStation.pipe.xCoord);
nbt.setInteger("dockY", mainDockingStation.pipe.yCoord);
nbt.setInteger("dockZ", mainDockingStation.pipe.zCoord);
nbt.setInteger("dockSide", mainDockingStation.side.ordinal());
}
if (currentAI != null) {
nbt.setString("ai", currentAI.getClass().getCanonicalName());
@ -352,10 +351,10 @@ public class EntityRobot extends EntityLiving implements
public void readEntityFromNBT(NBTTagCompound nbt) {
super.readEntityFromNBT(nbt);
dockingStation.x = nbt.getInteger("dockX");
dockingStation.y = nbt.getInteger("dockY");
dockingStation.z = nbt.getInteger("dockZ");
dockingStation.side = ForgeDirection.values () [nbt.getInteger("dockSide")];
if (nbt.hasKey("dockX")) {
mainDockingStation = DockingStationRegistry.getStation(nbt.getInteger("dockX"), nbt.getInteger("dockY"),
nbt.getInteger("dockZ"), ForgeDirection.values()[nbt.getInteger("dockSide")]);
}
/*
* if (nbt.hasKey("ai")) { try { nextAI = (RobotAIBase)
@ -372,11 +371,8 @@ public class EntityRobot extends EntityLiving implements
setDead();
}
public void setDockingStation (TileGenericPipe tile, ForgeDirection side) {
dockingStation.x = tile.xCoord;
dockingStation.y = tile.yCoord;
dockingStation.z = tile.zCoord;
dockingStation.side = side;
public void setMainDockingStation(DockingStation station) {
mainDockingStation = station;
}
@Override

View file

@ -24,6 +24,9 @@ public class RobotAIBase extends EntityAIBase {
}
public void setDestination(EntityRobot robot, float x, float y, float z) {
robot.isDocked = false;
robot.currentDockingStation = null;
destX = x;
destY = y;
destZ = z;

View file

@ -8,10 +8,16 @@
*/
package buildcraft.core.robots;
import buildcraft.robots.DockingStation;
public class RobotAIDocked extends RobotAIBase {
public RobotAIDocked(EntityRobot iRobot) {
private DockingStation station;
public RobotAIDocked(EntityRobot iRobot, DockingStation iStation) {
super(iRobot);
station = iStation;
}
@Override
@ -22,8 +28,9 @@ public class RobotAIDocked extends RobotAIBase {
robot.motionX = 0;
robot.motionY = 0;
robot.motionZ = 0;
robot.posX = robot.dockingStation.x + 0.5F + robot.dockingStation.side.offsetX * 0.5F;
robot.posY = robot.dockingStation.y + 0.5F + robot.dockingStation.side.offsetY * 0.5F;
robot.posZ = robot.dockingStation.z + 0.5F + robot.dockingStation.side.offsetZ * 0.5F;
robot.posX = station.pipe.xCoord + 0.5F + station.side.offsetX * 0.5F;
robot.posY = station.pipe.yCoord + 0.5F + station.side.offsetY * 0.5F;
robot.posZ = station.pipe.zCoord + 0.5F + station.side.offsetZ * 0.5F;
robot.currentDockingStation = station;
}
}

View file

@ -0,0 +1,27 @@
/**
* 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 buildcraft.robots.DockingStation;
public class RobotAIGoToDock extends RobotAIComposite {
public RobotAIGoToDock(EntityRobot iRobot, DockingStation station) {
super(iRobot,
new RobotAIMoveTo(iRobot,
station.pipe.xCoord + 0.5F + station.side.offsetX * 1.5F,
station.pipe.yCoord + 0.5F + station.side.offsetY * 1.5F,
station.pipe.zCoord + 0.5F + station.side.offsetZ * 1.5F),
new RobotAIDirectMoveTo(iRobot,
station.pipe.xCoord + 0.5F + station.side.offsetX * 0.5F,
station.pipe.yCoord + 0.5F + station.side.offsetY * 0.5F,
station.pipe.zCoord + 0.5F + station.side.offsetZ * 0.5F),
new RobotAIDocked(iRobot, station));
}
}

View file

@ -8,18 +8,9 @@
*/
package buildcraft.core.robots;
public class RobotAIReturnToDock extends RobotAIComposite {
public class RobotAIReturnToDock extends RobotAIGoToDock {
public RobotAIReturnToDock(EntityRobot iRobot) {
super(iRobot,
new RobotAIMoveTo(iRobot,
iRobot.dockingStation.x + 0.5F + iRobot.dockingStation.side.offsetX * 1.5F,
iRobot.dockingStation.y + 0.5F + iRobot.dockingStation.side.offsetY * 1.5F,
iRobot.dockingStation.z + 0.5F + iRobot.dockingStation.side.offsetZ * 1.5F),
new RobotAIDirectMoveTo(iRobot,
iRobot.dockingStation.x + 0.5F + iRobot.dockingStation.side.offsetX * 0.5F,
iRobot.dockingStation.y + 0.5F + iRobot.dockingStation.side.offsetY * 0.5F,
iRobot.dockingStation.z + 0.5F + iRobot.dockingStation.side.offsetZ * 0.5F),
new RobotAIDocked(iRobot));
super(iRobot, iRobot.mainDockingStation);
}
}

View file

@ -6,28 +6,37 @@ import net.minecraft.block.Block;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.ForgeHooks;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.boards.IRedstoneBoardRobot;
import buildcraft.api.boards.RedstoneBoardNBT;
import buildcraft.api.boards.RedstoneBoardRegistry;
import buildcraft.api.boards.RedstoneBoardRobotNBT;
import buildcraft.core.BlockIndex;
import buildcraft.core.inventory.ITransactor;
import buildcraft.core.inventory.Transactor;
import buildcraft.core.inventory.filters.ArrayStackFilter;
import buildcraft.core.robots.EntityRobot;
import buildcraft.core.robots.RobotAIGoToDock;
import buildcraft.core.robots.RobotAIMoveTo;
import buildcraft.core.utils.BlockUtil;
import buildcraft.core.utils.IPathFound;
import buildcraft.core.utils.PathFinding;
import buildcraft.robots.DockingStation;
import buildcraft.robots.DockingStationRegistry;
public class BoardRobotLumberjack implements IRedstoneBoardRobot<EntityRobot> {
private static enum Stages {
LOOK_FOR_WOOD, GO_TO_WOOD, CUT_WOOD
LOOK_FOR_AXE, GO_TO_AXE_INVENTORY, LOOK_FOR_WOOD, GO_TO_WOOD, CUT_WOOD
};
private NBTTagCompound data;
@ -35,7 +44,8 @@ public class BoardRobotLumberjack implements IRedstoneBoardRobot<EntityRobot> {
private int range;
private boolean initialized = false;
private PathFinding woodScanner = null;
private Stages stage = Stages.LOOK_FOR_WOOD;
private DockingStation axeDocking = null;
private Stages stage = Stages.LOOK_FOR_AXE;
private BlockIndex woodToChop;
private float blockDamage;
@ -53,12 +63,60 @@ public class BoardRobotLumberjack implements IRedstoneBoardRobot<EntityRobot> {
if (!initialized) {
range = data.getInteger("range");
robot.setItemInUse(new ItemStack(Items.wooden_axe));
initialized = true;
}
if (stage == Stages.LOOK_FOR_WOOD) {
if (stage == Stages.LOOK_FOR_AXE) {
for (DockingStation d : DockingStationRegistry.getStations()) {
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
TileEntity nearbyTile = robot.worldObj.getTileEntity(d.pipe.xCoord + dir.offsetX, d.pipe.yCoord
+ dir.offsetY, d.pipe.zCoord
+ dir.offsetZ);
if (nearbyTile != null && nearbyTile instanceof IInventory) {
ArrayStackFilter filter = new ArrayStackFilter(new ItemStack(Items.wooden_axe));
ITransactor trans = Transactor.getTransactorFor(nearbyTile);
if (trans.remove(filter, dir.getOpposite(), false) != null) {
axeDocking = d;
robot.setMainAI(new RobotAIGoToDock(robot, axeDocking));
stage = Stages.GO_TO_AXE_INVENTORY;
return;
}
}
}
}
} else if (stage == Stages.GO_TO_AXE_INVENTORY) {
if (robot.currentDockingStation != null && robot.currentDockingStation.equals(axeDocking)) {
ItemStack axeFound = null;
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
TileEntity nearbyTile = robot.worldObj.getTileEntity(axeDocking.pipe.xCoord + dir.offsetX,
axeDocking.pipe.yCoord
+ dir.offsetY, axeDocking.pipe.zCoord + dir.offsetZ);
if (nearbyTile != null && nearbyTile instanceof IInventory) {
ArrayStackFilter filter = new ArrayStackFilter(new ItemStack(Items.wooden_axe));
ITransactor trans = Transactor.getTransactorFor(nearbyTile);
axeFound = trans.remove(filter, dir.getOpposite(), true);
if (axeFound != null) {
break;
}
}
}
reset();
if (axeFound == null) {
stage = Stages.LOOK_FOR_AXE;
} else {
robot.setItemInUse(axeFound);
stage = Stages.LOOK_FOR_WOOD;
}
}
} else if (stage == Stages.LOOK_FOR_WOOD) {
if (woodScanner == null) {
woodScanner = new PathFinding(robot.worldObj, new BlockIndex(robot), new IPathFound() {
@Override
@ -117,9 +175,16 @@ public class BoardRobotLumberjack implements IRedstoneBoardRobot<EntityRobot> {
BlockUtil.breakBlock((WorldServer) robot.worldObj, woodToChop.x, woodToChop.y, woodToChop.z, 6000);
robot.worldObj.setBlockToAir(woodToChop.x, woodToChop.y, woodToChop.z);
stage = Stages.LOOK_FOR_WOOD;
woodToChop = null;
woodScanner = null;
robot.setItemActive(false);
robot.itemInUse.getItem().onBlockDestroyed(robot.itemInUse, robot.worldObj, block, woodToChop.x,
woodToChop.y, woodToChop.z, robot);
if (robot.itemInUse.getItemDamage() >= robot.itemInUse.getMaxDamage()) {
robot.setItemInUse(null);
stage = Stages.LOOK_FOR_AXE;
}
reset();
} else {
robot.worldObj.destroyBlockInWorldPartially(robot.getEntityId(), woodToChop.x,
woodToChop.y, woodToChop.z, (int) (blockDamage * 10.0F) - 1);
@ -127,6 +192,12 @@ public class BoardRobotLumberjack implements IRedstoneBoardRobot<EntityRobot> {
}
}
private void reset() {
axeDocking = null;
woodToChop = null;
woodScanner = null;
}
@Override
public RedstoneBoardRobotNBT getNBTHandler() {
return BoardRobotLumberjackNBT.instance;

View file

@ -106,22 +106,25 @@ public class BoardRobotPicker implements IRedstoneBoardRobot<EntityRobot> {
} else {
if (robot.isDocked) {
TileGenericPipe pipe = (TileGenericPipe) robot.worldObj
.getTileEntity(robot.dockingStation.x, robot.dockingStation.y,
robot.dockingStation.z);
.getTileEntity(robot.currentDockingStation.pipe.xCoord, robot.currentDockingStation.pipe.yCoord,
robot.currentDockingStation.pipe.zCoord);
if (pipe != null && pipe.pipe.transport instanceof PipeTransportItems) {
if (unloadTracker.markTimeIfDelay(robot.worldObj)) {
for (int i = 0; i < robot.getSizeInventory(); ++i) {
if (robot.getStackInSlot(i) != null) {
float cx = robot.dockingStation.x + 0.5F + 0.2F * robot.dockingStation.side.offsetX;
float cy = robot.dockingStation.y + 0.5F + 0.2F * robot.dockingStation.side.offsetY;
float cz = robot.dockingStation.z + 0.5F + 0.2F * robot.dockingStation.side.offsetZ;
float cx = robot.currentDockingStation.pipe.xCoord + 0.5F + 0.2F
* robot.currentDockingStation.side.offsetX;
float cy = robot.currentDockingStation.pipe.yCoord + 0.5F + 0.2F
* robot.currentDockingStation.side.offsetY;
float cz = robot.currentDockingStation.pipe.zCoord + 0.5F + 0.2F
* robot.currentDockingStation.side.offsetZ;
TravelingItem item = TravelingItem.make(cx, cy,
cz, robot.getStackInSlot(i));
((PipeTransportItems) pipe.pipe.transport)
.injectItem(item, robot.dockingStation.side.getOpposite());
.injectItem(item, robot.currentDockingStation.side.getOpposite());
robot.setInventorySlotContents(i, null);

View file

@ -69,6 +69,10 @@ public class PathFinding {
nextIteration = startNode;
}
public void iterate() {
iterate(PATH_ITERATIONS);
}
public void iterate(int itNumber) {
if (nextIteration == null) {
return;

View file

@ -59,6 +59,7 @@ import buildcraft.core.robots.EntityRobot;
import buildcraft.core.robots.RobotAIDocked;
import buildcraft.core.utils.MatrixTranformations;
import buildcraft.core.utils.Utils;
import buildcraft.robots.DockingStationRegistry;
import buildcraft.transport.gates.GateDefinition;
import buildcraft.transport.gates.GateFactory;
import buildcraft.transport.gates.ItemGate;
@ -795,9 +796,8 @@ public class BlockGenericPipe extends BlockBuildCraft {
float pz = z + 0.5F + rayTraceResult.sideHit.offsetZ * 0.5F;
robot.setPosition(px, py, pz);
robot.setDockingStation(pipe.container,
rayTraceResult.sideHit);
robot.setMainAI(new RobotAIDocked(robot));
robot.setMainDockingStation(DockingStationRegistry.getStation(x, y, z, rayTraceResult.sideHit));
robot.setMainAI(new RobotAIDocked(robot, robot.mainDockingStation));
world.spawnEntityInWorld(robot);
if (!player.capabilities.isCreativeMode) {

View file

@ -65,6 +65,8 @@ import buildcraft.core.network.IGuiReturnHandler;
import buildcraft.core.network.ISyncedTile;
import buildcraft.core.network.PacketTileState;
import buildcraft.core.utils.Utils;
import buildcraft.robots.DockingStation;
import buildcraft.robots.DockingStationRegistry;
import buildcraft.transport.gates.GateDefinition;
import buildcraft.transport.gates.GateFactory;
@ -233,6 +235,12 @@ public class TileGenericPipe extends TileEntity implements IPowerReceptor, IFlui
}
sideProperties.readFromNBT(nbt);
for (int i = 0; i < 6; ++i) {
if (sideProperties.robotStations[i]) {
DockingStationRegistry.registerStation(new DockingStation(this, ForgeDirection.VALID_DIRECTIONS[i]));
}
}
}
@Override
@ -955,6 +963,9 @@ public class TileGenericPipe extends TileEntity implements IPowerReceptor, IFlui
worldObj.notifyBlockChange(this.xCoord, this.yCoord, this.zCoord, getBlock());
scheduleNeighborChange(); //To force recalculation of connections
scheduleRenderUpdate();
DockingStationRegistry.registerStation(new DockingStation(this, forgeDirection));
return true;
}