robots now shutdown in stead of dropping as items

This commit is contained in:
Hea3veN 2015-04-04 09:29:53 -03:00
parent 7a80a80fad
commit 9ee5947397
8 changed files with 163 additions and 60 deletions

View file

@ -26,6 +26,7 @@ public abstract class EntityRobotBase extends EntityLiving implements IInventory
public static final int MAX_ENERGY = 100000; public static final int MAX_ENERGY = 100000;
public static final int SAFETY_ENERGY = MAX_ENERGY / 5; 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 static final long NULL_ROBOT_ID = Long.MAX_VALUE;
public EntityRobotBase(World par1World) { public EntityRobotBase(World par1World) {

View file

@ -94,6 +94,7 @@ import buildcraft.robotics.ai.AIRobotSearchEntity;
import buildcraft.robotics.ai.AIRobotSearchRandomGroundBlock; import buildcraft.robotics.ai.AIRobotSearchRandomGroundBlock;
import buildcraft.robotics.ai.AIRobotSearchStackRequest; import buildcraft.robotics.ai.AIRobotSearchStackRequest;
import buildcraft.robotics.ai.AIRobotSearchStation; import buildcraft.robotics.ai.AIRobotSearchStation;
import buildcraft.robotics.ai.AIRobotShutdown;
import buildcraft.robotics.ai.AIRobotSleep; import buildcraft.robotics.ai.AIRobotSleep;
import buildcraft.robotics.ai.AIRobotStraightMoveTo; import buildcraft.robotics.ai.AIRobotStraightMoveTo;
import buildcraft.robotics.ai.AIRobotUnload; 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(AIRobotSearchRandomGroundBlock.class, "aiRobotSearchRandomGroundBlock", "buildcraft.core.robots.AIRobotSearchRandomGroundBlock");
RobotManager.registerAIRobot(AIRobotSearchStackRequest.class, "aiRobotSearchStackRequest", "buildcraft.core.robots.AIRobotSearchStackRequest"); RobotManager.registerAIRobot(AIRobotSearchStackRequest.class, "aiRobotSearchStackRequest", "buildcraft.core.robots.AIRobotSearchStackRequest");
RobotManager.registerAIRobot(AIRobotSearchStation.class, "aiRobotSearchStation", "buildcraft.core.robots.AIRobotSearchStation"); 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(AIRobotSleep.class, "aiRobotSleep", "buildcraft.core.robots.AIRobotSleep");
RobotManager.registerAIRobot(AIRobotStraightMoveTo.class, "aiRobotStraightMoveTo", "buildcraft.core.robots.AIRobotStraightMoveTo"); RobotManager.registerAIRobot(AIRobotStraightMoveTo.class, "aiRobotStraightMoveTo", "buildcraft.core.robots.AIRobotStraightMoveTo");
RobotManager.registerAIRobot(AIRobotUnload.class, "aiRobotUnload", "buildcraft.core.robots.AIRobotUnload"); RobotManager.registerAIRobot(AIRobotUnload.class, "aiRobotUnload", "buildcraft.core.robots.AIRobotUnload");

View file

@ -8,6 +8,7 @@
*/ */
package buildcraft.robotics; package buildcraft.robotics;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.WeakHashMap; 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.NBTUtils;
import buildcraft.core.lib.utils.NetworkUtils; import buildcraft.core.lib.utils.NetworkUtils;
import buildcraft.robotics.ai.AIRobotMain; import buildcraft.robotics.ai.AIRobotMain;
import buildcraft.robotics.ai.AIRobotShutdown;
import buildcraft.robotics.ai.AIRobotSleep; import buildcraft.robotics.ai.AIRobotSleep;
import buildcraft.robotics.statements.ActionRobotWorkInArea; import buildcraft.robotics.statements.ActionRobotWorkInArea;
import buildcraft.transport.Pipe; import buildcraft.transport.Pipe;
@ -110,7 +112,7 @@ public class EntityRobot extends EntityRobotBase implements
private boolean firstUpdateDone = false; private boolean firstUpdateDone = false;
private boolean isAsleepClient = false; private boolean isActiveClient = false;
private long robotId = EntityRobotBase.NULL_ROBOT_ID; private long robotId = EntityRobotBase.NULL_ROBOT_ID;
@ -188,7 +190,7 @@ public class EntityRobot extends EntityRobotBase implements
itemAngle1 = dataWatcher.getWatchableObjectFloat(17); itemAngle1 = dataWatcher.getWatchableObjectFloat(17);
itemAngle2 = dataWatcher.getWatchableObjectFloat(18); itemAngle2 = dataWatcher.getWatchableObjectFloat(18);
energySpendPerCycle = dataWatcher.getWatchableObjectInt(19); energySpendPerCycle = dataWatcher.getWatchableObjectInt(19);
isAsleepClient = dataWatcher.getWatchableObjectByte(20) == 1; isActiveClient = dataWatcher.getWatchableObjectByte(20) == 1;
battery.setEnergy(dataWatcher.getWatchableObjectInt(21)); battery.setEnergy(dataWatcher.getWatchableObjectInt(21));
} }
@ -201,8 +203,12 @@ public class EntityRobot extends EntityRobotBase implements
dataWatcher.updateObject(18, Float.valueOf(itemAngle2)); dataWatcher.updateObject(18, Float.valueOf(itemAngle2));
} }
public boolean isAsleep() { public boolean isActive() {
return worldObj.isRemote ? isAsleepClient : mainAI.getActiveAI() instanceof AIRobotSleep; if (worldObj.isRemote) {
return isActiveClient;
} else {
return mainAI.getActiveAI() instanceof AIRobotSleep || mainAI.getActiveAI() instanceof AIRobotShutdown;
}
} }
protected void init() { protected void init() {
@ -260,7 +266,7 @@ public class EntityRobot extends EntityRobotBase implements
if (!worldObj.isRemote) { if (!worldObj.isRemote) {
// The client-side sleep indicator should also display if the robot is charging. // 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. // 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()); dataWatcher.updateObject(21, getEnergy());
if (needsUpdate) { if (needsUpdate) {
@ -285,32 +291,29 @@ public class EntityRobot extends EntityRobotBase implements
if (!worldObj.isRemote) { if (!worldObj.isRemote) {
if (linkedDockingStation == null) { if (linkedDockingStation == null) {
linkedDockingStation = RobotManager.registryProvider.getRegistry(worldObj).getStation( if (linkedDockingStationIndex != null) {
linkedDockingStationIndex.x, linkedDockingStation = getRegistry().getStation(linkedDockingStationIndex.x,
linkedDockingStationIndex.y, linkedDockingStationIndex.y, linkedDockingStationIndex.z,
linkedDockingStationIndex.z,
linkedDockingStationSide); linkedDockingStationSide);
}
if (linkedDockingStation == null if (linkedDockingStation == null
|| linkedDockingStation.robotTaking() != this) { || linkedDockingStation.robotTaking() != this) {
// Error at load time, the expected linked stations is not if (!(mainAI.getDelegateAI() instanceof AIRobotShutdown)) {
// properly set, kill this robot. mainAI.startDelegateAI(new AIRobotShutdown(this));
}
setDead();
return;
} }
} }
if (currentDockingStationIndex != null && currentDockingStation == null) { if (currentDockingStationIndex != null && currentDockingStation == null) {
currentDockingStation = (DockingStation) currentDockingStation = getRegistry().getStation(
RobotManager.registryProvider.getRegistry(worldObj).getStation(
currentDockingStationIndex.x, currentDockingStationIndex.x,
currentDockingStationIndex.y, currentDockingStationIndex.y,
currentDockingStationIndex.z, currentDockingStationIndex.z,
currentDockingStationSide); currentDockingStationSide);
} }
if (linkedDockingStation != null && if (linkedDockingStation == null ||
((Pipe) linkedDockingStation.getPipe().getPipe()).isInitialized()) { ((Pipe) linkedDockingStation.getPipe().getPipe()).isInitialized()) {
this.worldObj.theProfiler.startSection("bcRobotAIMainCycle"); this.worldObj.theProfiler.startSection("bcRobotAIMainCycle");
mainAI.cycle(); mainAI.cycle();
@ -320,10 +323,6 @@ public class EntityRobot extends EntityRobotBase implements
energySpendPerCycle = mainAI.getActiveAI().getEnergyCost(); energySpendPerCycle = mainAI.getActiveAI().getEnergyCost();
dataWatcher.updateObject(19, energySpendPerCycle); 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) { public void writeEntityToNBT(NBTTagCompound nbt) {
super.writeEntityToNBT(nbt); super.writeEntityToNBT(nbt);
if (linkedDockingStation != null) {
NBTTagCompound linkedStationNBT = new NBTTagCompound(); NBTTagCompound linkedStationNBT = new NBTTagCompound();
NBTTagCompound linkedStationIndexNBT = new NBTTagCompound(); NBTTagCompound linkedStationIndexNBT = new NBTTagCompound();
linkedDockingStationIndex.writeTo(linkedStationIndexNBT); linkedDockingStationIndex.writeTo(linkedStationIndexNBT);
linkedStationNBT.setTag("index", linkedStationIndexNBT); linkedStationNBT.setTag("index", linkedStationIndexNBT);
linkedStationNBT.setByte("side", (byte) linkedDockingStationSide.ordinal()); linkedStationNBT.setByte("side", (byte) linkedDockingStationSide.ordinal());
nbt.setTag("linkedStation", linkedStationNBT); nbt.setTag("linkedStation", linkedStationNBT);
}
if (currentDockingStationIndex != null) { if (currentDockingStationIndex != null) {
NBTTagCompound currentStationNBT = new NBTTagCompound(); NBTTagCompound currentStationNBT = new NBTTagCompound();
@ -526,9 +527,11 @@ public class EntityRobot extends EntityRobotBase implements
public void readEntityFromNBT(NBTTagCompound nbt) { public void readEntityFromNBT(NBTTagCompound nbt) {
super.readEntityFromNBT(nbt); super.readEntityFromNBT(nbt);
if (nbt.hasKey("linkedStation")) {
NBTTagCompound linkedStationNBT = nbt.getCompoundTag("linkedStation"); NBTTagCompound linkedStationNBT = nbt.getCompoundTag("linkedStation");
linkedDockingStationIndex = new BlockIndex(linkedStationNBT.getCompoundTag("index")); linkedDockingStationIndex = new BlockIndex(linkedStationNBT.getCompoundTag("index"));
linkedDockingStationSide = ForgeDirection.values()[linkedStationNBT.getByte("side")]; linkedDockingStationSide = ForgeDirection.values()[linkedStationNBT.getByte("side")];
}
if (nbt.hasKey("currentStation")) { if (nbt.hasKey("currentStation")) {
NBTTagCompound currentStationNBT = nbt.getCompoundTag("currentStation"); NBTTagCompound currentStationNBT = nbt.getCompoundTag("currentStation");
@ -602,6 +605,8 @@ public class EntityRobot extends EntityRobotBase implements
currentDockingStationIndex = null; currentDockingStationIndex = null;
currentDockingStationSide = null; currentDockingStationSide = null;
mainAI.abortDelegateAI();
} }
} }
@ -611,16 +616,20 @@ public class EntityRobot extends EntityRobotBase implements
} }
@Override @Override
public void setMainStation(DockingStation iStation) { public void setMainStation(DockingStation station) {
DockingStation station = iStation;
if (linkedDockingStation != null && linkedDockingStation != station) { if (linkedDockingStation != null && linkedDockingStation != station) {
linkedDockingStation.unsafeRelease(this); linkedDockingStation.unsafeRelease(this);
} }
linkedDockingStation = station; linkedDockingStation = station;
if (station != null) {
linkedDockingStationIndex = linkedDockingStation.index(); linkedDockingStationIndex = linkedDockingStation.index();
linkedDockingStationSide = linkedDockingStation.side(); linkedDockingStationSide = linkedDockingStation.side();
} else {
linkedDockingStationIndex = null;
linkedDockingStationSide = ForgeDirection.UNKNOWN;
}
} }
@Override @Override
@ -960,30 +969,40 @@ public class EntityRobot extends EntityRobotBase implements
return unreachableEntities.containsKey(entity); return unreachableEntities.containsKey(entity);
} }
@Override private List<ItemStack> getDrops() {
public void setDead() { List<ItemStack> drops = new ArrayList<ItemStack>();
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 (!worldObj.isRemote && !isDead) {
if (mainAI != null) { if (mainAI != null) {
mainAI.abort(); mainAI.abort();
} }
List<ItemStack> drops = getDrops();
ItemStack robotStack = new ItemStack (BuildCraftRobotics.robotItem); for (ItemStack stack : drops) {
NBTUtils.getItemData(robotStack).setTag("board", originalBoardNBT); entityDropItem(stack, 0);
NBTUtils.getItemData(robotStack).setInteger("energy", battery.getEnergyStored());
entityDropItem(robotStack, 0);
if (itemInUse != null) {
entityDropItem(itemInUse, 0);
}
for (ItemStack element : inv) {
if (element != null) {
entityDropItem(element, 0);
} }
} }
getRegistry().killRobot(this); getRegistry().killRobot(this);
} }
super.setDead(); @Override
public void setDead() {
// prevent super.setDead() from getting called
} }
@Override @Override

View file

@ -22,6 +22,7 @@ import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.common.util.Constants; import net.minecraftforge.common.util.Constants;
import net.minecraftforge.common.util.ForgeDirection; import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.event.world.ChunkEvent; import net.minecraftforge.event.world.ChunkEvent;
import buildcraft.api.core.BCLog;
import buildcraft.api.robots.DockingStation; import buildcraft.api.robots.DockingStation;
import buildcraft.api.robots.EntityRobotBase; import buildcraft.api.robots.EntityRobotBase;
import buildcraft.api.robots.IRobotRegistry; import buildcraft.api.robots.IRobotRegistry;
@ -60,6 +61,9 @@ public class RobotRegistry extends WorldSavedData implements IRobotRegistry {
if (robot.getRobotId() == EntityRobotBase.NULL_ROBOT_ID) { if (robot.getRobotId() == EntityRobotBase.NULL_ROBOT_ID) {
((EntityRobot) robot).setUniqueRobotId(getNextRobotId()); ((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); robotsLoaded.put(robot.getRobotId(), (EntityRobot) robot);
} }
@ -258,7 +262,11 @@ public class RobotRegistry extends WorldSavedData implements IRobotRegistry {
if (stations.containsKey(index)) { if (stations.containsKey(index)) {
if (station.robotTaking() != null) { 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) { } else if (station.robotIdTaking() != EntityRobotBase.NULL_ROBOT_ID) {
stationsTakenByRobot.get(station.robotIdTaking()).remove(index); stationsTakenByRobot.get(station.robotIdTaking()).remove(index);
} }

View file

@ -26,14 +26,18 @@ public class AIRobotMain extends AIRobot {
@Override @Override
public void preempt(AIRobot ai) { public void preempt(AIRobot ai) {
if (!(ai instanceof AIRobotRecharge)) { if (robot.getEnergy() <= EntityRobotBase.SHUTDOWN_ENERGY) {
if (robot.getEnergy() < EntityRobotBase.SAFETY_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)); startDelegateAI(new AIRobotRecharge(robot));
}
} else if (overridingAI != null && ai != overridingAI) { } else if (overridingAI != null && ai != overridingAI) {
startDelegateAI(overridingAI); startDelegateAI(overridingAI);
} }
} }
}
@Override @Override
public void update() { public void update() {

View file

@ -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;
}
}

View file

@ -149,7 +149,7 @@ public class RenderRobot extends Render implements IItemRenderer {
if (robot.getTexture() != null) { if (robot.getTexture() != null) {
renderManager.renderEngine.bindTexture(robot.getTexture()); renderManager.renderEngine.bindTexture(robot.getTexture());
float storagePercent = (float) robot.getBattery().getEnergyStored() / (float) robot.getBattery().getMaxEnergyStored(); 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(); GL11.glPopMatrix();

View file

@ -43,7 +43,7 @@ public class TriggerRobotSleep extends BCStatement implements ITriggerInternal {
if (station.robotTaking() != null) { if (station.robotTaking() != null) {
EntityRobot robot = (EntityRobot) station.robotTaking(); EntityRobot robot = (EntityRobot) station.robotTaking();
if (robot.isAsleep()) { if (robot.isActive()) {
return true; return true;
} }
} }