From 9ee59473971501be7c3ed231939eba548f72d7e9 Mon Sep 17 00:00:00 2001 From: Hea3veN Date: Sat, 4 Apr 2015 09:29:53 -0300 Subject: [PATCH] robots now shutdown in stead of dropping as items --- .../api/robots/EntityRobotBase.java | 1 + common/buildcraft/BuildCraftRobotics.java | 2 + common/buildcraft/robotics/EntityRobot.java | 123 ++++++++++-------- common/buildcraft/robotics/RobotRegistry.java | 10 +- .../buildcraft/robotics/ai/AIRobotMain.java | 14 +- .../robotics/ai/AIRobotShutdown.java | 69 ++++++++++ .../robotics/render/RenderRobot.java | 2 +- .../statements/TriggerRobotSleep.java | 2 +- 8 files changed, 163 insertions(+), 60 deletions(-) create mode 100644 common/buildcraft/robotics/ai/AIRobotShutdown.java diff --git a/api/buildcraft/api/robots/EntityRobotBase.java b/api/buildcraft/api/robots/EntityRobotBase.java index 22d1f888..35ecc2af 100755 --- a/api/buildcraft/api/robots/EntityRobotBase.java +++ b/api/buildcraft/api/robots/EntityRobotBase.java @@ -26,6 +26,7 @@ public abstract class EntityRobotBase extends EntityLiving implements IInventory public static final int MAX_ENERGY = 100000; public static final int SAFETY_ENERGY = MAX_ENERGY / 5; + public static final int SHUTDOWN_ENERGY = 0; public static final long NULL_ROBOT_ID = Long.MAX_VALUE; public EntityRobotBase(World par1World) { diff --git a/common/buildcraft/BuildCraftRobotics.java b/common/buildcraft/BuildCraftRobotics.java index a80434eb..4bcae334 100644 --- a/common/buildcraft/BuildCraftRobotics.java +++ b/common/buildcraft/BuildCraftRobotics.java @@ -94,6 +94,7 @@ import buildcraft.robotics.ai.AIRobotSearchEntity; import buildcraft.robotics.ai.AIRobotSearchRandomGroundBlock; import buildcraft.robotics.ai.AIRobotSearchStackRequest; import buildcraft.robotics.ai.AIRobotSearchStation; +import buildcraft.robotics.ai.AIRobotShutdown; import buildcraft.robotics.ai.AIRobotSleep; import buildcraft.robotics.ai.AIRobotStraightMoveTo; import buildcraft.robotics.ai.AIRobotUnload; @@ -333,6 +334,7 @@ public class BuildCraftRobotics extends BuildCraftMod { RobotManager.registerAIRobot(AIRobotSearchRandomGroundBlock.class, "aiRobotSearchRandomGroundBlock", "buildcraft.core.robots.AIRobotSearchRandomGroundBlock"); RobotManager.registerAIRobot(AIRobotSearchStackRequest.class, "aiRobotSearchStackRequest", "buildcraft.core.robots.AIRobotSearchStackRequest"); RobotManager.registerAIRobot(AIRobotSearchStation.class, "aiRobotSearchStation", "buildcraft.core.robots.AIRobotSearchStation"); + RobotManager.registerAIRobot(AIRobotShutdown.class, "aiRobotShutdown"); RobotManager.registerAIRobot(AIRobotSleep.class, "aiRobotSleep", "buildcraft.core.robots.AIRobotSleep"); RobotManager.registerAIRobot(AIRobotStraightMoveTo.class, "aiRobotStraightMoveTo", "buildcraft.core.robots.AIRobotStraightMoveTo"); RobotManager.registerAIRobot(AIRobotUnload.class, "aiRobotUnload", "buildcraft.core.robots.AIRobotUnload"); diff --git a/common/buildcraft/robotics/EntityRobot.java b/common/buildcraft/robotics/EntityRobot.java index 57511ba6..ed7055f0 100644 --- a/common/buildcraft/robotics/EntityRobot.java +++ b/common/buildcraft/robotics/EntityRobot.java @@ -8,6 +8,7 @@ */ package buildcraft.robotics; +import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.WeakHashMap; @@ -62,6 +63,7 @@ import buildcraft.core.lib.network.command.PacketCommand; import buildcraft.core.lib.utils.NBTUtils; import buildcraft.core.lib.utils.NetworkUtils; import buildcraft.robotics.ai.AIRobotMain; +import buildcraft.robotics.ai.AIRobotShutdown; import buildcraft.robotics.ai.AIRobotSleep; import buildcraft.robotics.statements.ActionRobotWorkInArea; import buildcraft.transport.Pipe; @@ -110,7 +112,7 @@ public class EntityRobot extends EntityRobotBase implements private boolean firstUpdateDone = false; - private boolean isAsleepClient = false; + private boolean isActiveClient = false; private long robotId = EntityRobotBase.NULL_ROBOT_ID; @@ -188,7 +190,7 @@ public class EntityRobot extends EntityRobotBase implements itemAngle1 = dataWatcher.getWatchableObjectFloat(17); itemAngle2 = dataWatcher.getWatchableObjectFloat(18); energySpendPerCycle = dataWatcher.getWatchableObjectInt(19); - isAsleepClient = dataWatcher.getWatchableObjectByte(20) == 1; + isActiveClient = dataWatcher.getWatchableObjectByte(20) == 1; battery.setEnergy(dataWatcher.getWatchableObjectInt(21)); } @@ -201,8 +203,12 @@ public class EntityRobot extends EntityRobotBase implements dataWatcher.updateObject(18, Float.valueOf(itemAngle2)); } - public boolean isAsleep() { - return worldObj.isRemote ? isAsleepClient : mainAI.getActiveAI() instanceof AIRobotSleep; + public boolean isActive() { + if (worldObj.isRemote) { + return isActiveClient; + } else { + return mainAI.getActiveAI() instanceof AIRobotSleep || mainAI.getActiveAI() instanceof AIRobotShutdown; + } } protected void init() { @@ -260,7 +266,7 @@ public class EntityRobot extends EntityRobotBase implements if (!worldObj.isRemote) { // The client-side sleep indicator should also display if the robot is charging. // To not break gates and other things checking for sleep, this is done here. - dataWatcher.updateObject(20, Byte.valueOf((byte) ((isAsleep() && ticksCharging == 0) ? 1 : 0))); + dataWatcher.updateObject(20, Byte.valueOf((byte) ((isActive() && ticksCharging == 0) ? 1 : 0))); dataWatcher.updateObject(21, getEnergy()); if (needsUpdate) { @@ -285,32 +291,29 @@ public class EntityRobot extends EntityRobotBase implements if (!worldObj.isRemote) { if (linkedDockingStation == null) { - linkedDockingStation = RobotManager.registryProvider.getRegistry(worldObj).getStation( - linkedDockingStationIndex.x, - linkedDockingStationIndex.y, - linkedDockingStationIndex.z, - linkedDockingStationSide); + if (linkedDockingStationIndex != null) { + linkedDockingStation = getRegistry().getStation(linkedDockingStationIndex.x, + linkedDockingStationIndex.y, linkedDockingStationIndex.z, + linkedDockingStationSide); + } if (linkedDockingStation == null || linkedDockingStation.robotTaking() != this) { - // Error at load time, the expected linked stations is not - // properly set, kill this robot. - - setDead(); - return; + if (!(mainAI.getDelegateAI() instanceof AIRobotShutdown)) { + mainAI.startDelegateAI(new AIRobotShutdown(this)); + } } } if (currentDockingStationIndex != null && currentDockingStation == null) { - currentDockingStation = (DockingStation) - RobotManager.registryProvider.getRegistry(worldObj).getStation( + currentDockingStation = getRegistry().getStation( currentDockingStationIndex.x, currentDockingStationIndex.y, currentDockingStationIndex.z, currentDockingStationSide); } - if (linkedDockingStation != null && + if (linkedDockingStation == null || ((Pipe) linkedDockingStation.getPipe().getPipe()).isInitialized()) { this.worldObj.theProfiler.startSection("bcRobotAIMainCycle"); mainAI.cycle(); @@ -320,10 +323,6 @@ public class EntityRobot extends EntityRobotBase implements energySpendPerCycle = mainAI.getActiveAI().getEnergyCost(); dataWatcher.updateObject(19, energySpendPerCycle); } - - if (this.battery.getEnergyStored() <= 0 && !linkedToChargeStation()) { - setDead(); - } } } @@ -460,12 +459,14 @@ public class EntityRobot extends EntityRobotBase implements public void writeEntityToNBT(NBTTagCompound nbt) { super.writeEntityToNBT(nbt); - NBTTagCompound linkedStationNBT = new NBTTagCompound(); - NBTTagCompound linkedStationIndexNBT = new NBTTagCompound(); - linkedDockingStationIndex.writeTo(linkedStationIndexNBT); - linkedStationNBT.setTag("index", linkedStationIndexNBT); - linkedStationNBT.setByte("side", (byte) linkedDockingStationSide.ordinal()); - nbt.setTag("linkedStation", linkedStationNBT); + if (linkedDockingStation != null) { + NBTTagCompound linkedStationNBT = new NBTTagCompound(); + NBTTagCompound linkedStationIndexNBT = new NBTTagCompound(); + linkedDockingStationIndex.writeTo(linkedStationIndexNBT); + linkedStationNBT.setTag("index", linkedStationIndexNBT); + linkedStationNBT.setByte("side", (byte) linkedDockingStationSide.ordinal()); + nbt.setTag("linkedStation", linkedStationNBT); + } if (currentDockingStationIndex != null) { NBTTagCompound currentStationNBT = new NBTTagCompound(); @@ -526,9 +527,11 @@ public class EntityRobot extends EntityRobotBase implements public void readEntityFromNBT(NBTTagCompound nbt) { super.readEntityFromNBT(nbt); - NBTTagCompound linkedStationNBT = nbt.getCompoundTag("linkedStation"); - linkedDockingStationIndex = new BlockIndex(linkedStationNBT.getCompoundTag("index")); - linkedDockingStationSide = ForgeDirection.values()[linkedStationNBT.getByte("side")]; + if (nbt.hasKey("linkedStation")) { + NBTTagCompound linkedStationNBT = nbt.getCompoundTag("linkedStation"); + linkedDockingStationIndex = new BlockIndex(linkedStationNBT.getCompoundTag("index")); + linkedDockingStationSide = ForgeDirection.values()[linkedStationNBT.getByte("side")]; + } if (nbt.hasKey("currentStation")) { NBTTagCompound currentStationNBT = nbt.getCompoundTag("currentStation"); @@ -602,6 +605,8 @@ public class EntityRobot extends EntityRobotBase implements currentDockingStationIndex = null; currentDockingStationSide = null; + + mainAI.abortDelegateAI(); } } @@ -611,16 +616,20 @@ public class EntityRobot extends EntityRobotBase implements } @Override - public void setMainStation(DockingStation iStation) { - DockingStation station = iStation; + public void setMainStation(DockingStation station) { if (linkedDockingStation != null && linkedDockingStation != station) { linkedDockingStation.unsafeRelease(this); } linkedDockingStation = station; - linkedDockingStationIndex = linkedDockingStation.index(); - linkedDockingStationSide = linkedDockingStation.side(); + if (station != null) { + linkedDockingStationIndex = linkedDockingStation.index(); + linkedDockingStationSide = linkedDockingStation.side(); + } else { + linkedDockingStationIndex = null; + linkedDockingStationSide = ForgeDirection.UNKNOWN; + } } @Override @@ -960,30 +969,40 @@ public class EntityRobot extends EntityRobotBase implements return unreachableEntities.containsKey(entity); } - @Override - public void setDead() { + private List getDrops() { + List drops = new ArrayList(); + ItemStack robotStack = new ItemStack(BuildCraftRobotics.robotItem); + NBTUtils.getItemData(robotStack).setTag("board", originalBoardNBT); + NBTUtils.getItemData(robotStack).setInteger("energy", battery.getEnergyStored()); + drops.add(robotStack); + if (itemInUse != null) { + drops.add(itemInUse); + } + for (ItemStack element : inv) { + if (element != null) { + drops.add(element); + } + } + return drops; + } + + private void convertToItems() { if (!worldObj.isRemote && !isDead) { if (mainAI != null) { mainAI.abort(); } - - ItemStack robotStack = new ItemStack (BuildCraftRobotics.robotItem); - NBTUtils.getItemData(robotStack).setTag("board", originalBoardNBT); - NBTUtils.getItemData(robotStack).setInteger("energy", battery.getEnergyStored()); - entityDropItem(robotStack, 0); - if (itemInUse != null) { - entityDropItem(itemInUse, 0); + List drops = getDrops(); + for (ItemStack stack : drops) { + entityDropItem(stack, 0); } - for (ItemStack element : inv) { - if (element != null) { - entityDropItem(element, 0); - } - } - - getRegistry().killRobot(this); } - super.setDead(); + getRegistry().killRobot(this); + } + + @Override + public void setDead() { + // prevent super.setDead() from getting called } @Override diff --git a/common/buildcraft/robotics/RobotRegistry.java b/common/buildcraft/robotics/RobotRegistry.java index c21d0e20..b41a9d56 100755 --- a/common/buildcraft/robotics/RobotRegistry.java +++ b/common/buildcraft/robotics/RobotRegistry.java @@ -22,6 +22,7 @@ import cpw.mods.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.common.util.Constants; import net.minecraftforge.common.util.ForgeDirection; import net.minecraftforge.event.world.ChunkEvent; +import buildcraft.api.core.BCLog; import buildcraft.api.robots.DockingStation; import buildcraft.api.robots.EntityRobotBase; import buildcraft.api.robots.IRobotRegistry; @@ -60,6 +61,9 @@ public class RobotRegistry extends WorldSavedData implements IRobotRegistry { if (robot.getRobotId() == EntityRobotBase.NULL_ROBOT_ID) { ((EntityRobot) robot).setUniqueRobotId(getNextRobotId()); } + if (robotsLoaded.containsKey(robot.getRobotId())) { + BCLog.logger.warn("Robot with id %d was not unregistered properly", robot.getRobotId()); + } robotsLoaded.put(robot.getRobotId(), (EntityRobot) robot); } @@ -258,7 +262,11 @@ public class RobotRegistry extends WorldSavedData implements IRobotRegistry { if (stations.containsKey(index)) { if (station.robotTaking() != null) { - station.robotTaking().setDead(); + if (!station.isMainStation()) { + station.robotTaking().undock(); + } else { + station.robotTaking().setMainStation(null); + } } else if (station.robotIdTaking() != EntityRobotBase.NULL_ROBOT_ID) { stationsTakenByRobot.get(station.robotIdTaking()).remove(index); } diff --git a/common/buildcraft/robotics/ai/AIRobotMain.java b/common/buildcraft/robotics/ai/AIRobotMain.java index 15737699..85641cc4 100755 --- a/common/buildcraft/robotics/ai/AIRobotMain.java +++ b/common/buildcraft/robotics/ai/AIRobotMain.java @@ -26,12 +26,16 @@ public class AIRobotMain extends AIRobot { @Override public void preempt(AIRobot ai) { - if (!(ai instanceof AIRobotRecharge)) { - if (robot.getEnergy() < EntityRobotBase.SAFETY_ENERGY) { - startDelegateAI(new AIRobotRecharge(robot)); - } else if (overridingAI != null && ai != overridingAI) { - startDelegateAI(overridingAI); + if (robot.getEnergy() <= EntityRobotBase.SHUTDOWN_ENERGY) { + if (!(ai instanceof AIRobotShutdown)) { + startDelegateAI(new AIRobotShutdown(robot)); } + } else if (robot.getEnergy() < EntityRobotBase.SAFETY_ENERGY) { + if (!(ai instanceof AIRobotRecharge) && !(ai instanceof AIRobotShutdown)) { + startDelegateAI(new AIRobotRecharge(robot)); + } + } else if (overridingAI != null && ai != overridingAI) { + startDelegateAI(overridingAI); } } diff --git a/common/buildcraft/robotics/ai/AIRobotShutdown.java b/common/buildcraft/robotics/ai/AIRobotShutdown.java new file mode 100644 index 00000000..4d283a29 --- /dev/null +++ b/common/buildcraft/robotics/ai/AIRobotShutdown.java @@ -0,0 +1,69 @@ +/** + * Copyright (c) 2011-2015, 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.robotics.ai; + +import java.util.List; +import net.minecraft.util.AxisAlignedBB; +import buildcraft.api.robots.AIRobot; +import buildcraft.api.robots.EntityRobotBase; + +public class AIRobotShutdown extends AIRobot { + + private int skip; + private double motionX; + private double motionZ; + + public AIRobotShutdown(EntityRobotBase iRobot) { + super(iRobot); + skip = 0; + motionX = robot.motionX; + motionZ = robot.motionZ; + } + + @Override + public void start() { + robot.undock(); + robot.motionX = motionX; + robot.motionY = -0.075f; + robot.motionZ = motionZ; + } + + @Override + public void update() { + if (skip == 0) { + List boxes = robot.worldObj.getCollidingBoundingBoxes(robot, + getRobotBox().addCoord(robot.motionX, -0.075f, robot.motionZ)); + if (boxes.size() == 0) { + robot.motionY = -0.075f; + } else { + robot.motionY = 0f; + if (robot.motionX != 0 || robot.motionZ != 0) { + robot.motionX = 0f; + robot.motionZ = 0f; + skip = 0; + } else { + skip = 20; + } + } + } else { + skip--; + } + + } + + private AxisAlignedBB getRobotBox() { + return AxisAlignedBB.getBoundingBox(robot.posX - 0.25d, robot.posY - 0.25d, + robot.posZ - 0.25d, robot.posX + 0.25d, robot.posY + 0.25d, robot.posZ + 0.25d); + } + + @Override + public int getEnergyCost() { + return 0; + } +} diff --git a/common/buildcraft/robotics/render/RenderRobot.java b/common/buildcraft/robotics/render/RenderRobot.java index cb2535ab..6ca27f12 100644 --- a/common/buildcraft/robotics/render/RenderRobot.java +++ b/common/buildcraft/robotics/render/RenderRobot.java @@ -149,7 +149,7 @@ public class RenderRobot extends Render implements IItemRenderer { if (robot.getTexture() != null) { renderManager.renderEngine.bindTexture(robot.getTexture()); float storagePercent = (float) robot.getBattery().getEnergyStored() / (float) robot.getBattery().getMaxEnergyStored(); - doRenderRobot(1F / 16F, renderManager.renderEngine, storagePercent, robot.isAsleep()); + doRenderRobot(1F / 16F, renderManager.renderEngine, storagePercent, robot.isActive()); } GL11.glPopMatrix(); diff --git a/common/buildcraft/robotics/statements/TriggerRobotSleep.java b/common/buildcraft/robotics/statements/TriggerRobotSleep.java index f2dce1b0..e3a49921 100755 --- a/common/buildcraft/robotics/statements/TriggerRobotSleep.java +++ b/common/buildcraft/robotics/statements/TriggerRobotSleep.java @@ -43,7 +43,7 @@ public class TriggerRobotSleep extends BCStatement implements ITriggerInternal { if (station.robotTaking() != null) { EntityRobot robot = (EntityRobot) station.robotTaking(); - if (robot.isAsleep()) { + if (robot.isActive()) { return true; } }