From 1605a36c0341c1a5eadb61122196026c933559d3 Mon Sep 17 00:00:00 2001 From: SpaceToad Date: Mon, 9 Jun 2014 17:40:56 +0200 Subject: [PATCH] lumberjack robot now manages its axe and corresponding damage, #1869 --- api/buildcraft/robots/DockingStation.java | 24 ++++++ .../robots/DockingStationRegistry.java | 83 ++++++++++++++++++ .../buildcraft/core/robots/EntityRobot.java | 36 ++++---- .../buildcraft/core/robots/RobotAIBase.java | 3 + .../buildcraft/core/robots/RobotAIDocked.java | 15 +++- .../core/robots/RobotAIGoToDock.java | 27 ++++++ .../core/robots/RobotAIReturnToDock.java | 13 +-- .../robots/boards/BoardRobotLumberjack.java | 85 +++++++++++++++++-- .../core/robots/boards/BoardRobotPicker.java | 15 ++-- common/buildcraft/core/utils/PathFinding.java | 4 + .../transport/BlockGenericPipe.java | 6 +- .../buildcraft/transport/TileGenericPipe.java | 11 +++ 12 files changed, 271 insertions(+), 51 deletions(-) create mode 100755 api/buildcraft/robots/DockingStation.java create mode 100755 api/buildcraft/robots/DockingStationRegistry.java create mode 100755 common/buildcraft/core/robots/RobotAIGoToDock.java diff --git a/api/buildcraft/robots/DockingStation.java b/api/buildcraft/robots/DockingStation.java new file mode 100755 index 00000000..2264026a --- /dev/null +++ b/api/buildcraft/robots/DockingStation.java @@ -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; + } +} + diff --git a/api/buildcraft/robots/DockingStationRegistry.java b/api/buildcraft/robots/DockingStationRegistry.java new file mode 100755 index 00000000..99c06deb --- /dev/null +++ b/api/buildcraft/robots/DockingStationRegistry.java @@ -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 stations = new HashMap(); + + 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 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); + } +} diff --git a/common/buildcraft/core/robots/EntityRobot.java b/common/buildcraft/core/robots/EntityRobot.java index 76698283..859f14e8 100755 --- a/common/buildcraft/core/robots/EntityRobot.java +++ b/common/buildcraft/core/robots/EntityRobot.java @@ -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 diff --git a/common/buildcraft/core/robots/RobotAIBase.java b/common/buildcraft/core/robots/RobotAIBase.java index c35b29a6..1a43e85b 100755 --- a/common/buildcraft/core/robots/RobotAIBase.java +++ b/common/buildcraft/core/robots/RobotAIBase.java @@ -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; diff --git a/common/buildcraft/core/robots/RobotAIDocked.java b/common/buildcraft/core/robots/RobotAIDocked.java index 08aed8f9..4528cfb0 100755 --- a/common/buildcraft/core/robots/RobotAIDocked.java +++ b/common/buildcraft/core/robots/RobotAIDocked.java @@ -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; } } diff --git a/common/buildcraft/core/robots/RobotAIGoToDock.java b/common/buildcraft/core/robots/RobotAIGoToDock.java new file mode 100755 index 00000000..bd81502f --- /dev/null +++ b/common/buildcraft/core/robots/RobotAIGoToDock.java @@ -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)); + } +} diff --git a/common/buildcraft/core/robots/RobotAIReturnToDock.java b/common/buildcraft/core/robots/RobotAIReturnToDock.java index 0d75460f..5eff8bb2 100755 --- a/common/buildcraft/core/robots/RobotAIReturnToDock.java +++ b/common/buildcraft/core/robots/RobotAIReturnToDock.java @@ -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); } } diff --git a/common/buildcraft/core/robots/boards/BoardRobotLumberjack.java b/common/buildcraft/core/robots/boards/BoardRobotLumberjack.java index 639258cb..80e03afe 100755 --- a/common/buildcraft/core/robots/boards/BoardRobotLumberjack.java +++ b/common/buildcraft/core/robots/boards/BoardRobotLumberjack.java @@ -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 { 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 { 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 { 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 { 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 { } } + private void reset() { + axeDocking = null; + woodToChop = null; + woodScanner = null; + } + @Override public RedstoneBoardRobotNBT getNBTHandler() { return BoardRobotLumberjackNBT.instance; diff --git a/common/buildcraft/core/robots/boards/BoardRobotPicker.java b/common/buildcraft/core/robots/boards/BoardRobotPicker.java index c631c84f..68b37755 100755 --- a/common/buildcraft/core/robots/boards/BoardRobotPicker.java +++ b/common/buildcraft/core/robots/boards/BoardRobotPicker.java @@ -106,22 +106,25 @@ public class BoardRobotPicker implements IRedstoneBoardRobot { } 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); diff --git a/common/buildcraft/core/utils/PathFinding.java b/common/buildcraft/core/utils/PathFinding.java index eb7f2f9b..d1833b09 100755 --- a/common/buildcraft/core/utils/PathFinding.java +++ b/common/buildcraft/core/utils/PathFinding.java @@ -69,6 +69,10 @@ public class PathFinding { nextIteration = startNode; } + public void iterate() { + iterate(PATH_ITERATIONS); + } + public void iterate(int itNumber) { if (nextIteration == null) { return; diff --git a/common/buildcraft/transport/BlockGenericPipe.java b/common/buildcraft/transport/BlockGenericPipe.java index 1a41b6cf..d2f38b46 100644 --- a/common/buildcraft/transport/BlockGenericPipe.java +++ b/common/buildcraft/transport/BlockGenericPipe.java @@ -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) { diff --git a/common/buildcraft/transport/TileGenericPipe.java b/common/buildcraft/transport/TileGenericPipe.java index 63793f19..c16b65ae 100644 --- a/common/buildcraft/transport/TileGenericPipe.java +++ b/common/buildcraft/transport/TileGenericPipe.java @@ -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; }