From 1bd39d326aacae55fed0df47acb8fd2afdd68a3a Mon Sep 17 00:00:00 2001 From: SpaceToad Date: Fri, 15 Aug 2014 10:55:28 +0200 Subject: [PATCH] implemented delivery robot, for #1984 --- .../assets/buildcraft/lang/en_US.lang | 3 +- common/buildcraft/BuildCraftSilicon.java | 16 +- .../robots/AIRobotSearchStackRequest.java | 161 ++++++++++++++++++ .../core/robots/boards/BoardRobotCrafter.java | 100 +---------- .../robots/boards/BoardRobotDelivery.java | 99 +++++++++++ .../robots/boards/BoardRobotDeliveryNBT.java | 64 +++++++ 6 files changed, 342 insertions(+), 101 deletions(-) create mode 100755 common/buildcraft/core/robots/AIRobotSearchStackRequest.java create mode 100755 common/buildcraft/core/robots/boards/BoardRobotDelivery.java create mode 100755 common/buildcraft/core/robots/boards/BoardRobotDeliveryNBT.java diff --git a/buildcraft_resources/assets/buildcraft/lang/en_US.lang b/buildcraft_resources/assets/buildcraft/lang/en_US.lang index 2cd4dbb1..fe514294 100755 --- a/buildcraft_resources/assets/buildcraft/lang/en_US.lang +++ b/buildcraft_resources/assets/buildcraft/lang/en_US.lang @@ -14,8 +14,7 @@ buildcraft.boardRobotShovelman=Shovelman buildcraft.boardRobotButcher=Butcher buildcraft.boardRobotBuilder=Builder buildcraft.boardRobotCrafter=Crafter -buildcraft.boardDetail.parameters=Parameters -buildcraft.boardDetail.range=Range +buildcraft.boardRobotDelivery=Delivery chat.pipe.power.iron.mode=Switched to %d MJ/t limit chat.pipe.power.energyConverter=Energy conversion: %s diff --git a/common/buildcraft/BuildCraftSilicon.java b/common/buildcraft/BuildCraftSilicon.java index f7980b9e..a80385ab 100644 --- a/common/buildcraft/BuildCraftSilicon.java +++ b/common/buildcraft/BuildCraftSilicon.java @@ -52,6 +52,7 @@ import buildcraft.core.robots.boards.BoardRobotBuilderNBT; import buildcraft.core.robots.boards.BoardRobotButcherNBT; import buildcraft.core.robots.boards.BoardRobotCarrierNBT; import buildcraft.core.robots.boards.BoardRobotCrafterNBT; +import buildcraft.core.robots.boards.BoardRobotDeliveryNBT; import buildcraft.core.robots.boards.BoardRobotFarmerNBT; import buildcraft.core.robots.boards.BoardRobotHarvesterNBT; import buildcraft.core.robots.boards.BoardRobotKnightNBT; @@ -217,6 +218,7 @@ public class BuildCraftSilicon extends BuildCraftMod { RedstoneBoardRegistry.instance.registerBoardClass(BoardRobotButcherNBT.instance, 5); RedstoneBoardRegistry.instance.registerBoardClass(BoardRobotShovelmanNBT.instance, 5); RedstoneBoardRegistry.instance.registerBoardClass(BoardRobotCrafterNBT.instance, 5); + RedstoneBoardRegistry.instance.registerBoardClass(BoardRobotDeliveryNBT.instance, 5); RedstoneBoardRegistry.instance.registerBoardClass(BoardRobotKnightNBT.instance, 1); RedstoneBoardRegistry.instance.registerBoardClass(BoardRobotBomberNBT.instance, 1); RedstoneBoardRegistry.instance.registerBoardClass(BoardRobotBuilderNBT.instance, 0.5F); @@ -227,18 +229,22 @@ public class BuildCraftSilicon extends BuildCraftMod { @Mod.EventHandler public void init(FMLInitializationEvent evt) { - channels = NetworkRegistry.INSTANCE.newChannel + channels = NetworkRegistry.INSTANCE + .newChannel (DefaultProps.NET_CHANNEL_NAME + "-SILICON", new BuildCraftChannelHandler(), new PacketHandlerSilicon()); NetworkRegistry.INSTANCE.registerGuiHandler(instance, new GuiHandler()); CoreProxy.proxy.registerTileEntity(TileLaser.class, "net.minecraft.src.buildcraft.factory.TileLaser"); - CoreProxy.proxy.registerTileEntity(TileAssemblyTable.class, "net.minecraft.src.buildcraft.factory.TileAssemblyTable"); - CoreProxy.proxy.registerTileEntity(TileAdvancedCraftingTable.class, "net.minecraft.src.buildcraft.factory.TileAssemblyAdvancedWorkbench"); - CoreProxy.proxy.registerTileEntity(TileIntegrationTable.class, "net.minecraft.src.buildcraft.factory.TileIntegrationTable"); + CoreProxy.proxy.registerTileEntity(TileAssemblyTable.class, + "net.minecraft.src.buildcraft.factory.TileAssemblyTable"); + CoreProxy.proxy.registerTileEntity(TileAdvancedCraftingTable.class, + "net.minecraft.src.buildcraft.factory.TileAssemblyAdvancedWorkbench"); + CoreProxy.proxy.registerTileEntity(TileIntegrationTable.class, + "net.minecraft.src.buildcraft.factory.TileIntegrationTable"); CoreProxy.proxy.registerTileEntity(TileZonePlan.class, "net.minecraft.src.buildcraft.commander.TileZonePlan"); CoreProxy.proxy.registerTileEntity(TileRequester.class, "net.minecraft.src.buildcraft.commander.TileRequester"); - SchematicRegistry.registerSchematicBlock(laserBlock, SchematicRotateMeta.class, new int[]{2, 5, 3, 4}, true); + SchematicRegistry.registerSchematicBlock(laserBlock, SchematicRotateMeta.class, new int[] {2, 5, 3, 4}, true); if (BuildCraftCore.loadDefaultRecipes) { loadRecipes(); diff --git a/common/buildcraft/core/robots/AIRobotSearchStackRequest.java b/common/buildcraft/core/robots/AIRobotSearchStackRequest.java new file mode 100755 index 00000000..93f29ee1 --- /dev/null +++ b/common/buildcraft/core/robots/AIRobotSearchStackRequest.java @@ -0,0 +1,161 @@ +/** + * 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.Collection; + +import net.minecraft.item.ItemStack; +import net.minecraft.tileentity.TileEntity; + +import net.minecraftforge.common.util.ForgeDirection; + +import buildcraft.api.gates.ActionParameterItemStack; +import buildcraft.api.gates.IStatementParameter; +import buildcraft.api.robots.AIRobot; +import buildcraft.api.robots.EntityRobotBase; +import buildcraft.api.robots.IRequestProvider; +import buildcraft.api.robots.StackRequest; +import buildcraft.core.inventory.StackHelper; +import buildcraft.silicon.statements.ActionStationRequestItems; +import buildcraft.silicon.statements.ActionStationRequestItemsMachine; +import buildcraft.transport.Pipe; +import buildcraft.transport.gates.ActionIterator; +import buildcraft.transport.gates.ActionSlot; + +public class AIRobotSearchStackRequest extends AIRobot { + + public StackRequest request = null; + + private Collection blackList; + + public AIRobotSearchStackRequest(EntityRobotBase iRobot) { + super(iRobot); + } + + public AIRobotSearchStackRequest(EntityRobotBase iRobot, Collection iBlackList) { + super(iRobot); + + blackList = iBlackList; + } + + @Override + public void start() { + startDelegateAI(new AIRobotSearchStation(robot, new StationProviderFilter(), robot.getZoneToWork())); + } + + @Override + public void delegateAIEnded(AIRobot ai) { + if (ai instanceof AIRobotSearchStation) { + if (!ai.success()) { + terminate(); + } else { + request = getOrderFromRequestingAction(((AIRobotSearchStation) ai).targetStation); + + if (request == null) { + request = getOrderFromRequestingStation(((AIRobotSearchStation) ai).targetStation, true); + } + + terminate(); + } + } + } + + @Override + public boolean success() { + return request != null; + } + + private boolean isBlacklisted(ItemStack stack) { + for (ItemStack black : blackList) { + if (StackHelper.isMatchingItem(stack, black)) { + return true; + } + } + + return false; + } + + private StackRequest getOrderFromRequestingStation(DockingStation station, boolean take) { + boolean actionFound = false; + + Pipe pipe = station.getPipe().pipe; + + for (ActionSlot s : new ActionIterator(pipe)) { + if (s.action instanceof ActionStationRequestItemsMachine) { + actionFound = true; + } + } + + if (!actionFound) { + return null; + } + + for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) { + TileEntity nearbyTile = robot.worldObj.getTileEntity(station.x() + dir.offsetX, station.y() + + dir.offsetY, station.z() + + dir.offsetZ); + + if (nearbyTile instanceof IRequestProvider) { + IRequestProvider provider = (IRequestProvider) nearbyTile; + + for (int i = 0; i < provider.getNumberOfRequests(); ++i) { + StackRequest requestFound = provider.getAvailableRequest(i); + + if (requestFound != null && !isBlacklisted(requestFound.stack)) { + requestFound.station = station; + + if (take) { + if (provider.takeRequest(i, robot)) { + return requestFound; + } + } else { + return requestFound; + } + } + } + } + } + + return null; + } + + private StackRequest getOrderFromRequestingAction(DockingStation station) { + boolean actionFound = false; + + Pipe pipe = station.getPipe().pipe; + + for (ActionSlot s : new ActionIterator(pipe)) { + if (s.action instanceof ActionStationRequestItems) { + for (IStatementParameter p : s.parameters) { + ActionParameterItemStack param = (ActionParameterItemStack) p; + + if (param != null && !isBlacklisted(param.getItemStackToDraw())) { + StackRequest req = new StackRequest(); + req.station = station; + req.stack = param.getItemStackToDraw(); + + return req; + } + } + } + } + + return null; + } + + private class StationProviderFilter implements IStationFilter { + + @Override + public boolean matches(DockingStation station) { + return getOrderFromRequestingAction(station) != null + || getOrderFromRequestingStation(station, false) != null; + } + } + +} diff --git a/common/buildcraft/core/robots/boards/BoardRobotCrafter.java b/common/buildcraft/core/robots/boards/BoardRobotCrafter.java index 2668f999..9337b813 100755 --- a/common/buildcraft/core/robots/boards/BoardRobotCrafter.java +++ b/common/buildcraft/core/robots/boards/BoardRobotCrafter.java @@ -18,9 +18,7 @@ import net.minecraft.item.crafting.FurnaceRecipes; import net.minecraft.item.crafting.IRecipe; import net.minecraft.item.crafting.ShapedRecipes; import net.minecraft.item.crafting.ShapelessRecipes; -import net.minecraft.tileentity.TileEntity; -import net.minecraftforge.common.util.ForgeDirection; import net.minecraftforge.oredict.ShapedOreRecipe; import net.minecraftforge.oredict.ShapelessOreRecipe; @@ -28,13 +26,11 @@ import buildcraft.api.boards.RedstoneBoardRobot; import buildcraft.api.boards.RedstoneBoardRobotNBT; import buildcraft.api.gates.ActionParameterItemStack; import buildcraft.api.gates.IActionParameter; -import buildcraft.api.gates.IStatementParameter; import buildcraft.api.recipes.CraftingResult; import buildcraft.api.recipes.IFlexibleRecipe; import buildcraft.api.robots.AIRobot; import buildcraft.api.robots.EntityRobotBase; import buildcraft.api.robots.IDockingStation; -import buildcraft.api.robots.IRequestProvider; import buildcraft.api.robots.StackRequest; import buildcraft.core.inventory.StackHelper; import buildcraft.core.recipes.AssemblyRecipeManager; @@ -44,15 +40,12 @@ import buildcraft.core.robots.AIRobotCraftGeneric; import buildcraft.core.robots.AIRobotCraftWorkbench; import buildcraft.core.robots.AIRobotDeliverRequested; import buildcraft.core.robots.AIRobotGotoSleep; +import buildcraft.core.robots.AIRobotGotoStationAndUnload; import buildcraft.core.robots.AIRobotGotoStationToUnload; -import buildcraft.core.robots.AIRobotSearchStation; +import buildcraft.core.robots.AIRobotSearchStackRequest; import buildcraft.core.robots.AIRobotUnload; import buildcraft.core.robots.DockingStation; -import buildcraft.core.robots.IStationFilter; import buildcraft.silicon.statements.ActionRobotCraft; -import buildcraft.silicon.statements.ActionStationRequestItems; -import buildcraft.silicon.statements.ActionStationRequestItemsMachine; -import buildcraft.transport.Pipe; import buildcraft.transport.gates.ActionIterator; import buildcraft.transport.gates.ActionSlot; @@ -80,7 +73,7 @@ public class BoardRobotCrafter extends RedstoneBoardRobot { // TODO: We should call load or drop, in order to clean items even // if no destination is to be found - startDelegateAI(new AIRobotGotoStationToUnload(robot, robot.getZoneToWork())); + startDelegateAI(new AIRobotGotoStationAndUnload(robot, robot.getZoneToWork())); return; } @@ -91,7 +84,7 @@ public class BoardRobotCrafter extends RedstoneBoardRobot { } if (order == null) { - startDelegateAI(new AIRobotSearchStation(robot, new StationProviderFilter(), robot.getZoneToWork())); + startDelegateAI(new AIRobotSearchStackRequest(robot, craftingBlacklist)); return; } @@ -141,16 +134,12 @@ public class BoardRobotCrafter extends RedstoneBoardRobot { } else { startDelegateAI(new AIRobotGotoSleep(robot)); } - } else if (ai instanceof AIRobotSearchStation) { + } else if (ai instanceof AIRobotSearchStackRequest) { if (!ai.success()) { craftingBlacklist.clear(); startDelegateAI(new AIRobotGotoSleep(robot)); } else { - currentRequest = getOrderFromRequestingAction(((AIRobotSearchStation) ai).targetStation); - - if (currentRequest == null) { - currentRequest = getOrderFromRequestingStation(((AIRobotSearchStation) ai).targetStation, true); - } + currentRequest = ((AIRobotSearchStackRequest) ai).request; if (!currentRequest.station.take(robot)) { currentRequest = null; @@ -237,81 +226,4 @@ public class BoardRobotCrafter extends RedstoneBoardRobot { return null; } - - private StackRequest getOrderFromRequestingStation(DockingStation station, boolean take) { - boolean actionFound = false; - - Pipe pipe = station.getPipe().pipe; - - for (ActionSlot s : new ActionIterator(pipe)) { - if (s.action instanceof ActionStationRequestItemsMachine) { - actionFound = true; - } - } - - if (!actionFound) { - return null; - } - - for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) { - TileEntity nearbyTile = robot.worldObj.getTileEntity(station.x() + dir.offsetX, station.y() - + dir.offsetY, station.z() - + dir.offsetZ); - - if (nearbyTile instanceof IRequestProvider) { - IRequestProvider provider = (IRequestProvider) nearbyTile; - - for (int i = 0; i < provider.getNumberOfRequests(); ++i) { - StackRequest request = provider.getAvailableRequest(i); - - if (request != null && !isBlacklisted(request.stack)) { - request.station = station; - - if (take) { - if (provider.takeRequest(i, robot)) { - return request; - } - } else { - return request; - } - } - } - } - } - - return null; - } - - private StackRequest getOrderFromRequestingAction(DockingStation station) { - boolean actionFound = false; - - Pipe pipe = station.getPipe().pipe; - - for (ActionSlot s : new ActionIterator(pipe)) { - if (s.action instanceof ActionStationRequestItems) { - for (IStatementParameter p : s.parameters) { - ActionParameterItemStack param = (ActionParameterItemStack) p; - - if (param != null && !isBlacklisted(param.getItemStackToDraw())) { - StackRequest req = new StackRequest(); - req.station = station; - req.stack = param.getItemStackToDraw(); - - return req; - } - } - } - } - - return null; - } - - private class StationProviderFilter implements IStationFilter { - - @Override - public boolean matches(DockingStation station) { - return getOrderFromRequestingAction(station) != null - || getOrderFromRequestingStation(station, false) != null; - } - } } diff --git a/common/buildcraft/core/robots/boards/BoardRobotDelivery.java b/common/buildcraft/core/robots/boards/BoardRobotDelivery.java new file mode 100755 index 00000000..6e8b9417 --- /dev/null +++ b/common/buildcraft/core/robots/boards/BoardRobotDelivery.java @@ -0,0 +1,99 @@ +/** + * 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.boards; + +import java.util.ArrayList; + +import net.minecraft.item.ItemStack; + +import buildcraft.api.boards.RedstoneBoardRobot; +import buildcraft.api.boards.RedstoneBoardRobotNBT; +import buildcraft.api.robots.AIRobot; +import buildcraft.api.robots.EntityRobotBase; +import buildcraft.api.robots.StackRequest; +import buildcraft.core.inventory.StackHelper; +import buildcraft.core.inventory.filters.IStackFilter; +import buildcraft.core.robots.AIRobotDeliverRequested; +import buildcraft.core.robots.AIRobotGotoSleep; +import buildcraft.core.robots.AIRobotGotoStationAndLoad; +import buildcraft.core.robots.AIRobotGotoStationAndUnload; +import buildcraft.core.robots.AIRobotSearchStackRequest; + +public class BoardRobotDelivery extends RedstoneBoardRobot { + + private ArrayList deliveryBlacklist = new ArrayList(); + + private StackRequest currentRequest = null; + + public BoardRobotDelivery(EntityRobotBase iRobot) { + super(iRobot); + } + + @Override + public RedstoneBoardRobotNBT getNBTHandler() { + return BoardRobotDeliveryNBT.instance; + } + + @Override + public void update() { + if (robot.containsItems()) { + // Always makes sure that when starting a craft, the inventory is + // clean. + + // TODO: We should call load or drop, in order to clean items even + // if no destination is to be found + startDelegateAI(new AIRobotGotoStationAndUnload(robot, robot.getZoneToWork())); + return; + } + + if (currentRequest == null) { + startDelegateAI(new AIRobotSearchStackRequest(robot, deliveryBlacklist)); + } else { + startDelegateAI(new AIRobotGotoStationAndLoad(robot, new ReqFilter(), robot.getZoneToWork())); + } + } + + @Override + public void delegateAIEnded(AIRobot ai) { + if (ai instanceof AIRobotSearchStackRequest) { + if (!ai.success()) { + deliveryBlacklist.clear(); + startDelegateAI(new AIRobotGotoSleep(robot)); + } else { + currentRequest = ((AIRobotSearchStackRequest) ai).request; + + if (!currentRequest.station.take(robot)) { + currentRequest = null; + } + } + } else if (ai instanceof AIRobotGotoStationAndLoad) { + if (!ai.success()) { + deliveryBlacklist.add(currentRequest.stack); + robot.releaseResources(); + currentRequest = null; + } else { + startDelegateAI(new AIRobotDeliverRequested(robot, currentRequest)); + } + } else if (ai instanceof AIRobotDeliverRequested) { + robot.releaseResources(); + } + } + + private class ReqFilter implements IStackFilter { + + @Override + public boolean matches(ItemStack stack) { + if (currentRequest == null) { + return false; + } else { + return StackHelper.isMatchingItem(stack, currentRequest.stack); + } + } + } +} diff --git a/common/buildcraft/core/robots/boards/BoardRobotDeliveryNBT.java b/common/buildcraft/core/robots/boards/BoardRobotDeliveryNBT.java new file mode 100755 index 00000000..44101d67 --- /dev/null +++ b/common/buildcraft/core/robots/boards/BoardRobotDeliveryNBT.java @@ -0,0 +1,64 @@ +/** + * 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.boards; + +import java.util.List; + +import net.minecraft.client.renderer.texture.IIconRegister; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.util.IIcon; +import net.minecraft.util.ResourceLocation; + +import buildcraft.api.boards.RedstoneBoardRobot; +import buildcraft.api.boards.RedstoneBoardRobotNBT; +import buildcraft.api.robots.EntityRobotBase; +import buildcraft.core.DefaultProps; +import buildcraft.core.utils.StringUtils; + +public final class BoardRobotDeliveryNBT extends RedstoneBoardRobotNBT { + + public static BoardRobotDeliveryNBT instance = new BoardRobotDeliveryNBT(); + + private static final ResourceLocation TEXTURE = new ResourceLocation("buildcraft", + DefaultProps.TEXTURE_PATH_ENTITIES + "/robot_delivery.png"); + + private IIcon icon; + + @Override + public RedstoneBoardRobot create(NBTTagCompound nbt, EntityRobotBase robot) { + return new BoardRobotDelivery(robot); + } + + @Override + public ResourceLocation getRobotTexture() { + return TEXTURE; + } + + @Override + public String getID() { + return "buildcraft:boardRobotDelivery"; + } + + @Override + public void addInformation(ItemStack stack, EntityPlayer player, List list, boolean advanced) { + list.add(StringUtils.localize("buildcraft.boardRobotDelivery")); + } + + @Override + public void registerIcons(IIconRegister iconRegister) { + icon = iconRegister.registerIcon("buildcraft:board_green"); + } + + @Override + public IIcon getIcon(NBTTagCompound nbt) { + return icon; + } +}