make sure all robot AI properly handle errors in delegate AIs

This commit is contained in:
Hea3veN 2015-04-05 17:41:37 -03:00
parent 542add1ff7
commit 63472e04f2
31 changed files with 120 additions and 160 deletions

View file

@ -17,8 +17,11 @@ public class AIRobot {
private AIRobot delegateAI;
private AIRobot parentAI;
private boolean success;
public AIRobot(EntityRobotBase iRobot) {
robot = iRobot;
success = true;
}
public void start() {
@ -65,7 +68,11 @@ public class AIRobot {
}
public boolean success() {
return true;
return success;
}
protected void setSuccess(boolean iSuccess) {
success = iSuccess;
}
public int getEnergyCost() {

View file

@ -16,7 +16,7 @@ import buildcraft.robotics.EntityRobot;
public class AIRobotAttack extends AIRobot {
public Entity target;
private Entity target;
private int delay = 10;
@ -75,12 +75,9 @@ public class AIRobotAttack extends AIRobot {
@Override
public void delegateAIEnded(AIRobot ai) {
if (ai instanceof AIRobotGotoBlock) {
AIRobotGotoBlock aiGoto = (AIRobotGotoBlock) ai;
if (aiGoto.unreachable) {
if (!ai.success()) {
robot.unreachableEntityDetected(target);
}
terminate();
}
}

View file

@ -23,7 +23,7 @@ import buildcraft.core.proxy.CoreProxy;
public class AIRobotBreak extends AIRobot {
public BlockIndex blockToBreak;
private BlockIndex blockToBreak;
private float blockDamage = 0;
private Block block;
@ -90,6 +90,8 @@ public class AIRobotBreak extends AIRobot {
robot.setItemInUse(null);
}
}
} else {
setSuccess(false);
}
terminate();

View file

@ -28,7 +28,13 @@ public class AIRobotDisposeItems extends AIRobot {
@Override
public void delegateAIEnded(AIRobot ai) {
if (ai instanceof AIRobotGotoStationAndUnload) {
if (!ai.success()) {
if (ai.success()) {
if (robot.containsItems()) {
startDelegateAI(new AIRobotGotoStationAndUnload(robot, robot.getZoneToWork()));
} else {
terminate();
}
} else {
for (IInvSlot slot : InventoryIterator.getIterable(robot)) {
if (slot.getStackInSlot() != null) {
final EntityItem entity = new EntityItem(
@ -43,10 +49,8 @@ public class AIRobotDisposeItems extends AIRobot {
slot.setStackInSlot(null);
}
}
} else if (robot.containsItems()) {
startDelegateAI(new AIRobotGotoStationAndUnload(robot, robot.getZoneToWork()));
terminate();
}
}
}
}

View file

@ -42,14 +42,24 @@ public class AIRobotFetchAndEquipItemStack extends AIRobot {
@Override
public void delegateAIEnded(AIRobot ai) {
if (filter == null) {
// filter can't be retreived, usually because of a load operation.
// Force a hard abort, preventing parent AI to continue normal
// sequence of actions and possibly re-starting this.
abort();
return;
if (ai instanceof AIRobotGotoStationToLoad) {
if (filter == null) {
// filter can't be retreived, usually because of a load operation.
// Force a hard abort, preventing parent AI to continue normal
// sequence of actions and possibly re-starting this.
abort();
return;
}
if (ai.success()) {
equipItemStack();
} else {
setSuccess(false);
}
terminate();
}
}
private void equipItemStack() {
if (robot.getDockingStation() != null) {
DockingStation station = (DockingStation) robot.getDockingStation();
@ -73,10 +83,9 @@ public class AIRobotFetchAndEquipItemStack extends AIRobot {
if (itemFound != null) {
robot.setItemInUse(itemFound);
terminate();
} else {
setSuccess(false);
}
}
terminate();
}
}

View file

@ -20,8 +20,7 @@ import buildcraft.robotics.boards.BoardRobotPicker;
public class AIRobotFetchItem extends AIRobot {
public EntityItem target;
public boolean itemPickupCancelled = false;
private EntityItem target;
private float maxRange;
private IStackFilter stackFilter;
@ -43,7 +42,6 @@ public class AIRobotFetchItem extends AIRobot {
@Override
public void preempt(AIRobot ai) {
if (target != null && target.isDead) {
itemPickupCancelled = true;
terminate();
}
}
@ -78,16 +76,14 @@ public class AIRobotFetchItem extends AIRobot {
// This would happen after a load. As we reached the item
// location already, just consider that the item is not there
// anymore and allow user to try to find another one.
itemPickupCancelled = true;
setSuccess(false);
terminate();
return;
}
if (((AIRobotGotoBlock) ai).unreachable) {
if (!ai.success()) {
robot.unreachableEntityDetected(target);
itemPickupCancelled = true;
terminate();
return;
}
}
}
@ -149,6 +145,7 @@ public class AIRobotFetchItem extends AIRobot {
}
} else {
// No item was found, terminate this AI
setSuccess(false);
terminate();
}
}

View file

@ -14,7 +14,7 @@ import buildcraft.api.robots.EntityRobotBase;
public class AIRobotGoAndLinkToDock extends AIRobot {
public DockingStation station;
private DockingStation station;
public AIRobotGoAndLinkToDock(EntityRobotBase iRobot) {
super(iRobot);
@ -37,6 +37,7 @@ public class AIRobotGoAndLinkToDock extends AIRobot {
station.y() + station.side().offsetY * 2,
station.z() + station.side().offsetZ * 2));
} else {
setSuccess(false);
terminate();
}
}
@ -45,23 +46,19 @@ public class AIRobotGoAndLinkToDock extends AIRobot {
@Override
public void delegateAIEnded(AIRobot ai) {
if (ai instanceof AIRobotGotoBlock) {
if (ai.success()) {
startDelegateAI(new AIRobotStraightMoveTo(robot,
station.x() + 0.5F + station.side().offsetX * 0.5F,
station.y() + 0.5F + station.side().offsetY * 0.5F,
station.z() + 0.5F + station.side().offsetZ * 0.5F));
} else {
robot.dock(station);
station = null;
} else {
terminate();
}
} else if (ai instanceof AIRobotStraightMoveTo) {
if (ai.success()) {
robot.dock(station);
}
terminate();
}
}
@Override
public void end() {
// If there's still a station targeted, it was not reached. The AI has
// probably been interrupted. Cancel reservation.
if (station != null) {
station.release(robot);
}
}
}

View file

@ -19,8 +19,6 @@ import buildcraft.core.lib.utils.PathFinding;
public class AIRobotGotoBlock extends AIRobotGoto {
public boolean unreachable = false;
private PathFinding pathSearch;
private IterableAlgorithmRunner pathSearchJob;
private LinkedList<BlockIndex> path;
@ -88,7 +86,7 @@ public class AIRobotGotoBlock extends AIRobotGoto {
path = pathSearch.getResult();
if (path.size() == 0) {
unreachable = true;
setSuccess(false);
terminate();
return;
}
@ -109,7 +107,6 @@ public class AIRobotGotoBlock extends AIRobotGoto {
robot.posY = lastBlockInPath.y + 0.5F;
robot.posZ = lastBlockInPath.z + 0.5F;
}
terminate();
}
}

View file

@ -19,7 +19,6 @@ public class AIRobotGotoStation extends AIRobot {
private BlockIndex stationIndex;
private ForgeDirection stationSide;
private boolean docked = false;
public AIRobotGotoStation(EntityRobotBase iRobot) {
super(iRobot);
@ -30,6 +29,7 @@ public class AIRobotGotoStation extends AIRobot {
stationIndex = station.index();
stationSide = station.side();
setSuccess(false);
}
@Override
@ -66,17 +66,12 @@ public class AIRobotGotoStation extends AIRobot {
stationIndex.y + 0.5F + stationSide.offsetY * 0.5F,
stationIndex.z + 0.5F + stationSide.offsetZ * 0.5F));
} else {
docked = true;
setSuccess(true);
robot.dock(station);
terminate();
}
}
@Override
public boolean success() {
return docked;
}
@Override
public boolean canLoadFromNBT() {
return true;

View file

@ -15,7 +15,6 @@ import buildcraft.core.lib.inventory.filters.IStackFilter;
public class AIRobotGotoStationAndLoad extends AIRobot {
private boolean found = false;
private IStackFilter filter;
private IZone zone;
@ -39,16 +38,11 @@ public class AIRobotGotoStationAndLoad extends AIRobot {
public void delegateAIEnded(AIRobot ai) {
if (ai instanceof AIRobotGotoStationToLoad) {
if (ai.success()) {
found = true;
startDelegateAI(new AIRobotLoad(robot, filter, 1));
} else {
setSuccess(false);
terminate();
}
}
}
@Override
public boolean success() {
return found;
}
}

View file

@ -15,7 +15,6 @@ import buildcraft.core.lib.inventory.filters.IFluidFilter;
public class AIRobotGotoStationAndLoadFluids extends AIRobot {
private boolean found = false;
private IZone zone;
private IFluidFilter filter;
@ -39,16 +38,11 @@ public class AIRobotGotoStationAndLoadFluids extends AIRobot {
public void delegateAIEnded(AIRobot ai) {
if (ai instanceof AIRobotGotoStationToLoadFluids) {
if (ai.success()) {
found = true;
startDelegateAI(new AIRobotLoadFluids(robot, filter));
} else {
setSuccess(false);
terminate();
}
}
}
@Override
public boolean success() {
return found;
}
}

View file

@ -15,7 +15,6 @@ import buildcraft.api.robots.EntityRobotBase;
public class AIRobotGotoStationAndUnload extends AIRobot {
private boolean found = false;
private IZone zone;
private DockingStation station;
@ -48,23 +47,18 @@ public class AIRobotGotoStationAndUnload extends AIRobot {
public void delegateAIEnded(AIRobot ai) {
if (ai instanceof AIRobotGotoStationToUnload) {
if (ai.success()) {
found = true;
startDelegateAI(new AIRobotUnload(robot));
} else {
setSuccess(false);
terminate();
}
} else if (ai instanceof AIRobotGotoStation) {
if (ai.success()) {
found = true;
startDelegateAI(new AIRobotUnload(robot));
} else {
setSuccess(false);
terminate();
}
}
}
@Override
public boolean success() {
return found;
}
}

View file

@ -14,7 +14,6 @@ import buildcraft.api.robots.EntityRobotBase;
public class AIRobotGotoStationAndUnloadFluids extends AIRobot {
private boolean found = false;
private IZone zone;
public AIRobotGotoStationAndUnloadFluids(EntityRobotBase iRobot) {
@ -36,16 +35,11 @@ public class AIRobotGotoStationAndUnloadFluids extends AIRobot {
public void delegateAIEnded(AIRobot ai) {
if (ai instanceof AIRobotGotoStationToUnloadFluids) {
if (ai.success()) {
found = true;
startDelegateAI(new AIRobotUnloadFluids(robot));
} else {
setSuccess(false);
terminate();
}
}
}
@Override
public boolean success() {
return found;
}
}

View file

@ -24,7 +24,6 @@ import buildcraft.robotics.statements.ActionStationProvideItems;
public class AIRobotGotoStationToLoad extends AIRobot {
private boolean found = false;
private IStackFilter filter;
private IZone zone;
@ -47,17 +46,11 @@ public class AIRobotGotoStationToLoad extends AIRobot {
@Override
public void delegateAIEnded(AIRobot ai) {
if (ai instanceof AIRobotSearchAndGotoStation) {
found = ((AIRobotSearchAndGotoStation) ai).targetStation != null;
setSuccess(ai.success());
terminate();
}
}
@Override
public boolean success() {
return found;
}
private class StationFilter implements IStationFilter {
@Override

View file

@ -23,7 +23,6 @@ import buildcraft.robotics.statements.ActionStationProvideFluids;
public class AIRobotGotoStationToLoadFluids extends AIRobot {
private boolean found = false;
private IZone zone;
private IFluidFilter filter;
@ -46,17 +45,11 @@ public class AIRobotGotoStationToLoadFluids extends AIRobot {
@Override
public void delegateAIEnded(AIRobot ai) {
if (ai instanceof AIRobotSearchAndGotoStation) {
found = ai.success();
setSuccess(ai.success());
terminate();
}
}
@Override
public boolean success() {
return found;
}
private class StationFilter implements IStationFilter {
@Override

View file

@ -24,7 +24,6 @@ import buildcraft.transport.gates.ActionIterator;
public class AIRobotGotoStationToUnload extends AIRobot {
private boolean found = false;
private IZone zone;
public AIRobotGotoStationToUnload(EntityRobotBase iRobot) {
@ -46,17 +45,11 @@ public class AIRobotGotoStationToUnload extends AIRobot {
@Override
public void delegateAIEnded(AIRobot ai) {
if (ai instanceof AIRobotSearchAndGotoStation) {
found = ((AIRobotSearchAndGotoStation) ai).targetStation != null;
setSuccess(ai.success());
terminate();
}
}
@Override
public boolean success() {
return found;
}
private class StationInventory implements IStationFilter {
@Override
public boolean matches(DockingStation station) {

View file

@ -23,7 +23,6 @@ import buildcraft.transport.PipeTransportFluids;
public class AIRobotGotoStationToUnloadFluids extends AIRobot {
private boolean found = false;
private IZone zone;
public AIRobotGotoStationToUnloadFluids(EntityRobotBase iRobot) {
@ -44,17 +43,11 @@ public class AIRobotGotoStationToUnloadFluids extends AIRobot {
@Override
public void delegateAIEnded(AIRobot ai) {
if (ai instanceof AIRobotSearchAndGotoStation) {
found = ((AIRobotSearchAndGotoStation) ai).targetStation != null;
setSuccess(ai.success());
terminate();
}
}
@Override
public boolean success() {
return found;
}
private class StationFilter implements IStationFilter {
@Override

View file

@ -19,9 +19,9 @@ import buildcraft.api.robots.EntityRobotBase;
public class AIRobotPumpBlock extends AIRobot {
public BlockIndex blockToPump;
public long waited = 0;
int pumped = 0;
private BlockIndex blockToPump;
private long waited = 0;
private int pumped = 0;
public AIRobotPumpBlock(EntityRobotBase iRobot) {
super(iRobot);

View file

@ -50,8 +50,8 @@ public class AIRobotRecharge extends AIRobot {
@Override
public void delegateAIEnded(AIRobot ai) {
if (ai instanceof AIRobotSearchAndGotoStation) {
if (robot.getDockingStation() == null
|| ((DockingStation) robot.getDockingStation()).getPipe().getPipeType() != IPipeTile.PipeType.POWER) {
if (!ai.success()) {
setSuccess(false);
terminate();
}
}

View file

@ -10,13 +10,11 @@ package buildcraft.robotics.ai;
import buildcraft.api.core.IZone;
import buildcraft.api.robots.AIRobot;
import buildcraft.api.robots.DockingStation;
import buildcraft.api.robots.EntityRobotBase;
import buildcraft.robotics.IStationFilter;
public class AIRobotSearchAndGotoStation extends AIRobot {
public DockingStation targetStation;
private IStationFilter filter;
private IZone zone;
@ -40,14 +38,14 @@ public class AIRobotSearchAndGotoStation extends AIRobot {
public void delegateAIEnded(AIRobot ai) {
if (ai instanceof AIRobotSearchStation) {
if (ai.success()) {
targetStation = ((AIRobotSearchStation) ai).targetStation;
startDelegateAI(new AIRobotGotoStation(robot, targetStation));
startDelegateAI(new AIRobotGotoStation(robot, ((AIRobotSearchStation) ai).targetStation));
} else {
setSuccess(false);
terminate();
}
} else if (ai instanceof AIRobotGotoStation) {
setSuccess(ai.success());
terminate();
}
}
@Override
public boolean success() {
return targetStation != null;
}
}

View file

@ -71,6 +71,11 @@ public class AIRobotSearchEntity extends AIRobot {
terminate();
}
@Override
public boolean success() {
return target != null;
}
@Override
public int getEnergyCost() {
return 2;

View file

@ -77,6 +77,11 @@ public class AIRobotSearchRandomGroundBlock extends AIRobot {
}
}
@Override
public boolean success() {
return blockFound != null;
}
@Override
public int getEnergyCost() {
return 2;

View file

@ -85,7 +85,7 @@ public class AIRobotSearchStation extends AIRobot {
}
@Override
public boolean success () {
public boolean success() {
return targetStation != null;
}
}

View file

@ -23,7 +23,6 @@ import buildcraft.transport.gates.ActionIterator;
public class AIRobotUnload extends AIRobot {
private int waitedCycles = 0;
private boolean delivered = false;
public AIRobotUnload(EntityRobotBase iRobot) {
super(iRobot);
@ -46,6 +45,7 @@ public class AIRobotUnload extends AIRobot {
DockingStation station = (DockingStation) robot.getDockingStation();
if (station == null) {
setSuccess(false);
return false;
}
@ -60,9 +60,6 @@ public class AIRobotUnload extends AIRobot {
if (s.statement instanceof ActionStationInputItems) {
if (((ActionStationInputItems) s.statement)
.insert(station, (EntityRobot) robot, s, robotSlot, true)) {
delivered = true;
return true;
}
}
@ -76,9 +73,4 @@ public class AIRobotUnload extends AIRobot {
public int getEnergyCost() {
return 10;
}
@Override
public boolean success() {
return delivered;
}
}

View file

@ -8,6 +8,7 @@
*/
package buildcraft.robotics.ai;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.util.ForgeDirection;
@ -54,8 +55,12 @@ public class AIRobotUseToolOnBlock extends AIRobot {
robot.setItemInUse(null);
}
stack.getItem().onItemUse(stack, CoreProxy.proxy.getBuildCraftPlayer((WorldServer) robot.worldObj).get(),
robot.worldObj, useToBlock.x, useToBlock.y, useToBlock.z, ForgeDirection.UP.ordinal(), 0, 0, 0);
EntityPlayer player = CoreProxy.proxy.getBuildCraftPlayer((WorldServer) robot.worldObj)
.get();
if (!stack.getItem().onItemUse(stack, player, robot.worldObj, useToBlock.x,
useToBlock.y, useToBlock.z, ForgeDirection.UP.ordinal(), 0, 0, 0)) {
setSuccess(false);
}
terminate();
}

View file

@ -69,21 +69,27 @@ public class BoardRobotBomber extends RedstoneBoardRobot {
if (ai instanceof AIRobotGotoStationToLoad) {
startDelegateAI(new AIRobotLoad(robot, TNT_FILTER));
} else if (ai instanceof AIRobotSearchRandomGroundBlock) {
AIRobotSearchRandomGroundBlock aiFind = (AIRobotSearchRandomGroundBlock) ai;
if (ai.success()) {
AIRobotSearchRandomGroundBlock aiFind = (AIRobotSearchRandomGroundBlock) ai;
startDelegateAI(new AIRobotGotoBlock(robot, aiFind.blockFound.x, aiFind.blockFound.y + flyingHeight,
aiFind.blockFound.z));
startDelegateAI(new AIRobotGotoBlock(robot, aiFind.blockFound.x,
aiFind.blockFound.y + flyingHeight,
aiFind.blockFound.z));
}
} else if (ai instanceof AIRobotGotoBlock) {
ITransactor t = Transactor.getTransactorFor(robot);
ItemStack stack = t.remove(TNT_FILTER, ForgeDirection.UNKNOWN, true);
if (ai.success()) {
ITransactor t = Transactor.getTransactorFor(robot);
ItemStack stack = t.remove(TNT_FILTER, ForgeDirection.UNKNOWN, true);
if (stack != null && stack.stackSize > 0) {
EntityTNTPrimed tnt = new EntityTNTPrimed(robot.worldObj, robot.posX + 0.25, robot.posY - 1,
robot.posZ + 0.25,
robot);
tnt.fuse = 37;
robot.worldObj.spawnEntityInWorld(tnt);
robot.worldObj.playSoundAtEntity(tnt, "game.tnt.primed", 1.0F, 1.0F);
if (stack != null && stack.stackSize > 0) {
EntityTNTPrimed tnt = new EntityTNTPrimed(robot.worldObj, robot.posX + 0.25,
robot.posY - 1,
robot.posZ + 0.25,
robot);
tnt.fuse = 37;
robot.worldObj.spawnEntityInWorld(tnt);
robot.worldObj.playSoundAtEntity(tnt, "game.tnt.primed", 1.0F, 1.0F);
}
}
}
}

View file

@ -73,6 +73,7 @@ public class BoardRobotBuilder extends RedstoneBoardRobot {
if (markerToBuild == null || !markerToBuild.needsToBuild()) {
markerToBuild = null;
// TODO: review what is this, maybe shutdown the robot
startDelegateAI(new AIRobot(robot));
return;
}

View file

@ -56,13 +56,11 @@ public class BoardRobotButcher extends RedstoneBoardRobot {
@Override
public void delegateAIEnded(AIRobot ai) {
if (ai instanceof AIRobotFetchAndEquipItemStack) {
if (robot.getHeldItem() == null) {
if (!ai.success()) {
startDelegateAI(new AIRobotGotoSleep(robot));
}
} else if (ai instanceof AIRobotSearchEntity) {
AIRobotSearchEntity mobAI = (AIRobotSearchEntity) ai;
if (mobAI.target != null) {
if (ai.success()) {
startDelegateAI(new AIRobotAttack(robot, ((AIRobotSearchEntity) ai).target));
} else {
startDelegateAI(new AIRobotGotoSleep(robot));

View file

@ -87,6 +87,7 @@ public abstract class BoardRobotGenericBreakBlock extends RedstoneBoardRobot {
startDelegateAI(new AIRobotGotoSleep(robot));
}
} else if (ai.getClass().isInstance(getBlockBreakAI())) {
// TODO: if !ai.success() -> can't break block, blacklist it
releaseBlockFound();
}
}

View file

@ -61,9 +61,7 @@ public class BoardRobotKnight extends RedstoneBoardRobot {
startDelegateAI(new AIRobotGotoSleep(robot));
}
} else if (ai instanceof AIRobotSearchEntity) {
AIRobotSearchEntity mobAI = (AIRobotSearchEntity) ai;
if (mobAI.target != null) {
if (ai.success()) {
startDelegateAI(new AIRobotAttack(robot, ((AIRobotSearchEntity) ai).target));
} else {
startDelegateAI(new AIRobotGotoSleep(robot));

View file

@ -38,9 +38,7 @@ public class BoardRobotPicker extends RedstoneBoardRobot {
@Override
public void delegateAIEnded(AIRobot ai) {
if (ai instanceof AIRobotFetchItem) {
AIRobotFetchItem fetching = (AIRobotFetchItem) ai;
if (fetching.itemPickupCancelled || fetching.target != null) {
if (ai.success()) {
// if we find an item - that may have been cancelled.
// let's try to get another one
startDelegateAI(new AIRobotFetchItem(robot, 250, ActionRobotFilter.getGateFilter(robot