refactor common robot ai code

This commit is contained in:
Hea3veN 2015-04-03 12:08:00 -03:00
parent d76f164f0d
commit 5ad693bea8
11 changed files with 276 additions and 269 deletions

View file

@ -87,10 +87,10 @@ import buildcraft.robotics.ai.AIRobotLoadFluids;
import buildcraft.robotics.ai.AIRobotMain; import buildcraft.robotics.ai.AIRobotMain;
import buildcraft.robotics.ai.AIRobotPumpBlock; import buildcraft.robotics.ai.AIRobotPumpBlock;
import buildcraft.robotics.ai.AIRobotRecharge; import buildcraft.robotics.ai.AIRobotRecharge;
import buildcraft.robotics.ai.AIRobotSearchAndGotoBlock;
import buildcraft.robotics.ai.AIRobotSearchAndGotoStation; import buildcraft.robotics.ai.AIRobotSearchAndGotoStation;
import buildcraft.robotics.ai.AIRobotSearchBlock; import buildcraft.robotics.ai.AIRobotSearchBlock;
import buildcraft.robotics.ai.AIRobotSearchEntity; import buildcraft.robotics.ai.AIRobotSearchEntity;
import buildcraft.robotics.ai.AIRobotSearchRandomBlock;
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;
@ -326,10 +326,10 @@ public class BuildCraftRobotics extends BuildCraftMod {
RobotManager.registerAIRobot(AIRobotLoadFluids.class, "aiRobotLoadFluids", "buildcraft.core.robots.AIRobotLoadFluids"); RobotManager.registerAIRobot(AIRobotLoadFluids.class, "aiRobotLoadFluids", "buildcraft.core.robots.AIRobotLoadFluids");
RobotManager.registerAIRobot(AIRobotPumpBlock.class, "aiRobotPumpBlock", "buildcraft.core.robots.AIRobotPumpBlock"); RobotManager.registerAIRobot(AIRobotPumpBlock.class, "aiRobotPumpBlock", "buildcraft.core.robots.AIRobotPumpBlock");
RobotManager.registerAIRobot(AIRobotRecharge.class, "aiRobotRecharge", "buildcraft.core.robots.AIRobotRecharge"); RobotManager.registerAIRobot(AIRobotRecharge.class, "aiRobotRecharge", "buildcraft.core.robots.AIRobotRecharge");
RobotManager.registerAIRobot(AIRobotSearchAndGotoBlock.class, "aiRobotSearchAndGoToBlock");
RobotManager.registerAIRobot(AIRobotSearchAndGotoStation.class, "aiRobotSearchAndGotoStation", "buildcraft.core.robots.AIRobotSearchAndGotoStation"); RobotManager.registerAIRobot(AIRobotSearchAndGotoStation.class, "aiRobotSearchAndGotoStation", "buildcraft.core.robots.AIRobotSearchAndGotoStation");
RobotManager.registerAIRobot(AIRobotSearchBlock.class, "aiRobotSearchBlock", "buildcraft.core.robots.AIRobotSearchBlock"); RobotManager.registerAIRobot(AIRobotSearchBlock.class, "aiRobotSearchBlock", "buildcraft.core.robots.AIRobotSearchBlock");
RobotManager.registerAIRobot(AIRobotSearchEntity.class, "aiRobotSearchEntity", "buildcraft.core.robots.AIRobotSearchEntity"); RobotManager.registerAIRobot(AIRobotSearchEntity.class, "aiRobotSearchEntity", "buildcraft.core.robots.AIRobotSearchEntity");
RobotManager.registerAIRobot(AIRobotSearchRandomBlock.class, "aiRobotSearchRandomBlock", "buildcraft.core.robots.AIRobotSearchRandomBlock");
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");

View file

@ -2,7 +2,6 @@ package buildcraft.robotics;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.util.ForgeDirection; import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.robots.DockingStation; import buildcraft.api.robots.DockingStation;
import buildcraft.api.robots.IDockingStationProvider; import buildcraft.api.robots.IDockingStationProvider;

View file

@ -0,0 +1,112 @@
/**
* 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 net.minecraft.nbt.NBTTagCompound;
import buildcraft.api.core.BlockIndex;
import buildcraft.api.robots.AIRobot;
import buildcraft.api.robots.EntityRobotBase;
import buildcraft.api.robots.ResourceIdBlock;
import buildcraft.core.lib.utils.IBlockFilter;
public class AIRobotSearchAndGotoBlock extends AIRobot {
protected BlockIndex blockFound;
private IBlockFilter filter;
private boolean random;
public AIRobotSearchAndGotoBlock(EntityRobotBase iRobot) {
super(iRobot);
blockFound = null;
random = false;
filter = null;
}
public AIRobotSearchAndGotoBlock(EntityRobotBase iRobot, boolean iRandom, IBlockFilter iPathFound) {
this(iRobot);
random = iRandom;
filter = iPathFound;
}
@Override
public void start() {
startDelegateAI(new AIRobotSearchBlock(robot, random, filter));
}
@Override
public void update() {
}
@Override
public void delegateAIEnded(AIRobot ai) {
if (ai instanceof AIRobotSearchBlock) {
if (ai.success()) {
AIRobotSearchBlock searchAI = (AIRobotSearchBlock) ai;
if (searchAI.takeResource()) {
blockFound = searchAI.blockFound;
startDelegateAI(new AIRobotGotoBlock(robot, searchAI.path));
} else {
terminate();
}
} else {
terminate();
}
} else if (ai instanceof AIRobotGotoBlock) {
if (!ai.success()) {
releaseBlockFound();
}
terminate();
}
}
@Override
public boolean success() {
return blockFound != null;
}
private void releaseBlockFound() {
if (blockFound != null) {
robot.getRegistry().release(new ResourceIdBlock(blockFound));
blockFound = null;
}
}
public BlockIndex getBlockFound() {
return blockFound;
}
@Override
public boolean canLoadFromNBT() {
return true;
}
@Override
public void writeSelfToNBT(NBTTagCompound nbt) {
super.writeSelfToNBT(nbt);
if (blockFound != null) {
NBTTagCompound sub = new NBTTagCompound();
blockFound.writeTo(sub);
nbt.setTag("indexStored", sub);
}
}
@Override
public void loadSelfFromNBT(NBTTagCompound nbt) {
super.loadSelfFromNBT(nbt);
if (nbt.hasKey("indexStored")) {
blockFound = new BlockIndex (nbt.getCompoundTag("indexStored"));
}
}
}

131
common/buildcraft/robotics/ai/AIRobotSearchBlock.java Executable file → Normal file
View file

@ -1,21 +1,128 @@
/**
* 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; package buildcraft.robotics.ai;
import java.util.Iterator;
import java.util.LinkedList;
import net.minecraft.nbt.NBTTagCompound;
import buildcraft.api.core.BlockIndex;
import buildcraft.api.core.IZone;
import buildcraft.api.robots.AIRobot;
import buildcraft.api.robots.EntityRobotBase; import buildcraft.api.robots.EntityRobotBase;
import buildcraft.api.robots.ResourceIdBlock;
import buildcraft.core.lib.utils.BlockScannerExpanding; import buildcraft.core.lib.utils.BlockScannerExpanding;
import buildcraft.core.lib.utils.BlockScannerRandom;
import buildcraft.core.lib.utils.BlockScannerZoneRandom;
import buildcraft.core.lib.utils.IBlockFilter; import buildcraft.core.lib.utils.IBlockFilter;
import buildcraft.core.lib.utils.IterableAlgorithmRunner;
import buildcraft.core.lib.utils.PathFindingSearch;
public class AIRobotSearchBlock extends AIRobotSearchBlockBase { public class AIRobotSearchBlock extends AIRobot {
public AIRobotSearchBlock(EntityRobotBase iRobot, IBlockFilter iPathFound) { public BlockIndex blockFound;
super(iRobot, iPathFound, new BlockScannerExpanding().iterator()); public LinkedList<BlockIndex> path;
private PathFindingSearch blockScanner = null;
private IterableAlgorithmRunner blockScannerJob;
private IBlockFilter pathFound;
private Iterator<BlockIndex> blockIter;
public AIRobotSearchBlock(EntityRobotBase iRobot, boolean random, IBlockFilter iPathFound) {
super(iRobot);
pathFound = iPathFound;
if (!random) {
blockIter = new BlockScannerExpanding().iterator();
} else {
IZone zone = iRobot.getZoneToWork();
if (zone != null) {
BlockIndex pos = new BlockIndex(iRobot);
blockIter = new BlockScannerZoneRandom(pos.x, pos.y, pos.z, iRobot.worldObj.rand, zone)
.iterator();
} else {
blockIter = new BlockScannerRandom(iRobot.worldObj.rand, 64).iterator();
}
}
blockFound = null;
path = null;
} }
} @Override
public void start() {
blockScanner = new PathFindingSearch(robot.worldObj, new BlockIndex(
robot), blockIter, pathFound, 96, robot
.getZoneToWork());
blockScannerJob = new IterableAlgorithmRunner(blockScanner);
blockScannerJob.start();
}
@Override
public void update() {
if (blockScannerJob == null) {
// This is probably due to a load from NBT. Abort the ai in
// that case, since there's no filter to analyze either.
abort();
return;
}
if (blockScannerJob.isDone()) {
path = blockScanner.getResult();
if (path != null && path.size() > 0) {
blockFound = path.removeLast();
} else {
path = null;
}
terminate();
}
}
@Override
public void end() {
if (blockScannerJob != null) {
blockScannerJob.terminate();
}
}
@Override
public boolean success() {
return blockFound != null;
}
@Override
public void writeSelfToNBT(NBTTagCompound nbt) {
super.writeSelfToNBT(nbt);
if (blockFound != null) {
NBTTagCompound sub = new NBTTagCompound();
blockFound.writeTo(sub);
nbt.setTag("blockFound", sub);
}
}
@Override
public void loadSelfFromNBT(NBTTagCompound nbt) {
super.loadSelfFromNBT(nbt);
if (nbt.hasKey("blockFound")) {
blockFound = new BlockIndex(nbt.getCompoundTag("blockFound"));
}
}
public boolean takeResource() {
boolean taken = false;
if (robot.getRegistry().take(new ResourceIdBlock(blockFound), robot)) {
taken = true;
}
unreserve();
return taken;
}
public void unreserve() {
blockScanner.unreserve(blockFound);
}
@Override
public int getEnergyCost() {
return 2;
}
}

View file

@ -1,113 +0,0 @@
package buildcraft.robotics.ai;
import java.util.Iterator;
import java.util.LinkedList;
import net.minecraft.nbt.NBTTagCompound;
import buildcraft.api.core.BlockIndex;
import buildcraft.api.robots.AIRobot;
import buildcraft.api.robots.EntityRobotBase;
import buildcraft.api.robots.ResourceIdBlock;
import buildcraft.core.lib.utils.IBlockFilter;
import buildcraft.core.lib.utils.IterableAlgorithmRunner;
import buildcraft.core.lib.utils.PathFindingSearch;
public class AIRobotSearchBlockBase extends AIRobot {
public BlockIndex blockFound;
public LinkedList<BlockIndex> path;
private PathFindingSearch blockScanner = null;
private IterableAlgorithmRunner blockScannerJob;
private IBlockFilter pathFound;
private Iterator<BlockIndex> blockIter;
public AIRobotSearchBlockBase(EntityRobotBase iRobot, IBlockFilter iPathFound, Iterator<BlockIndex> iBlockIter) {
super(iRobot);
pathFound = iPathFound;
blockIter = iBlockIter;
blockFound = null;
path = null;
}
@Override
public void start() {
blockScanner = new PathFindingSearch(robot.worldObj, new BlockIndex(
robot), blockIter, pathFound, 96, robot
.getZoneToWork());
blockScannerJob = new IterableAlgorithmRunner(blockScanner);
blockScannerJob.start();
}
@Override
public void update() {
if (blockScannerJob == null) {
// This is probably due to a load from NBT. Abort the ai in
// that case, since there's no filter to analyze either.
abort();
return;
}
if (blockScannerJob.isDone()) {
path = blockScanner.getResult();
if (path != null && path.size() > 0) {
blockFound = path.removeLast();
} else {
path = null;
}
terminate();
}
}
@Override
public void end() {
if (blockScannerJob != null) {
blockScannerJob.terminate();
}
}
@Override
public boolean success() {
return blockFound != null;
}
@Override
public void writeSelfToNBT(NBTTagCompound nbt) {
super.writeSelfToNBT(nbt);
if (blockFound != null) {
NBTTagCompound sub = new NBTTagCompound();
blockFound.writeTo(sub);
nbt.setTag("blockFound", sub);
}
}
@Override
public void loadSelfFromNBT(NBTTagCompound nbt) {
super.loadSelfFromNBT(nbt);
if (nbt.hasKey("blockFound")) {
blockFound = new BlockIndex(nbt.getCompoundTag("blockFound"));
}
}
public boolean takeResource() {
boolean taken = false;
if (robot.getRegistry().take(new ResourceIdBlock(blockFound), robot)) {
taken = true;
}
unreserve();
return taken;
}
public void unreserve() {
blockScanner.unreserve(blockFound);
}
@Override
public int getEnergyCost() {
return 2;
}
}

View file

@ -1,35 +0,0 @@
/**
* 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.Iterator;
import buildcraft.api.core.BlockIndex;
import buildcraft.api.core.IZone;
import buildcraft.api.robots.EntityRobotBase;
import buildcraft.core.lib.utils.BlockScannerRandom;
import buildcraft.core.lib.utils.BlockScannerZoneRandom;
import buildcraft.core.lib.utils.IBlockFilter;
public class AIRobotSearchRandomBlock extends AIRobotSearchBlockBase {
public AIRobotSearchRandomBlock(EntityRobotBase iRobot, IBlockFilter iPathFound) {
super(iRobot, iPathFound, getBlockIterator(iRobot));
}
private static Iterator<BlockIndex> getBlockIterator(EntityRobotBase iRobot) {
IZone zone = iRobot.getZoneToWork();
if (zone != null) {
BlockIndex pos = new BlockIndex(iRobot);
return new BlockScannerZoneRandom(pos.x, pos.y, pos.z, iRobot.worldObj.rand, zone).iterator();
} else {
return new BlockScannerRandom(iRobot.worldObj.rand, 64).iterator();
}
}
}

View file

@ -23,9 +23,8 @@ import buildcraft.api.robots.ResourceIdBlock;
import buildcraft.core.lib.inventory.filters.IStackFilter; import buildcraft.core.lib.inventory.filters.IStackFilter;
import buildcraft.core.lib.utils.IBlockFilter; import buildcraft.core.lib.utils.IBlockFilter;
import buildcraft.robotics.ai.AIRobotFetchAndEquipItemStack; import buildcraft.robotics.ai.AIRobotFetchAndEquipItemStack;
import buildcraft.robotics.ai.AIRobotGotoBlock;
import buildcraft.robotics.ai.AIRobotGotoSleep; import buildcraft.robotics.ai.AIRobotGotoSleep;
import buildcraft.robotics.ai.AIRobotSearchBlock; import buildcraft.robotics.ai.AIRobotSearchAndGotoBlock;
import buildcraft.robotics.ai.AIRobotUseToolOnBlock; import buildcraft.robotics.ai.AIRobotUseToolOnBlock;
public class BoardRobotFarmer extends RedstoneBoardRobot { public class BoardRobotFarmer extends RedstoneBoardRobot {
@ -52,7 +51,7 @@ public class BoardRobotFarmer extends RedstoneBoardRobot {
} }
})); }));
} else { } else {
startDelegateAI(new AIRobotSearchBlock(robot, new IBlockFilter() { startDelegateAI(new AIRobotSearchAndGotoBlock(robot, false, new IBlockFilter() {
@Override @Override
public boolean matches(World world, int x, int y, int z) { public boolean matches(World world, int x, int y, int z) {
return isDirt.get(world, x, y, z) return isDirt.get(world, x, y, z)
@ -65,21 +64,13 @@ public class BoardRobotFarmer extends RedstoneBoardRobot {
@Override @Override
public void delegateAIEnded(AIRobot ai) { public void delegateAIEnded(AIRobot ai) {
if (ai instanceof AIRobotSearchBlock) { if (ai instanceof AIRobotSearchAndGotoBlock) {
if (!ai.success()) { if (ai.success()) {
startDelegateAI(new AIRobotGotoSleep(robot)); blockFound = ((AIRobotSearchAndGotoBlock) ai).getBlockFound();
startDelegateAI(new AIRobotUseToolOnBlock(robot, blockFound));
} else { } else {
releaseBlockFound(); startDelegateAI(new AIRobotGotoSleep(robot));
AIRobotSearchBlock searchAI = (AIRobotSearchBlock) ai;
if (searchAI.takeResource()) {
blockFound = searchAI.blockFound;
startDelegateAI(new AIRobotGotoBlock(robot, searchAI.path));
} else {
startDelegateAI(new AIRobotGotoSleep(robot));
}
} }
} else if (ai instanceof AIRobotGotoBlock) {
startDelegateAI(new AIRobotUseToolOnBlock(robot, blockFound));
} else if (ai instanceof AIRobotFetchAndEquipItemStack) { } else if (ai instanceof AIRobotFetchAndEquipItemStack) {
if (robot.getHeldItem() == null) { if (robot.getHeldItem() == null) {
startDelegateAI(new AIRobotGotoSleep(robot)); startDelegateAI(new AIRobotGotoSleep(robot));

View file

@ -27,15 +27,14 @@ import buildcraft.core.lib.inventory.filters.IStackFilter;
import buildcraft.core.lib.utils.IBlockFilter; import buildcraft.core.lib.utils.IBlockFilter;
import buildcraft.robotics.ai.AIRobotBreak; import buildcraft.robotics.ai.AIRobotBreak;
import buildcraft.robotics.ai.AIRobotFetchAndEquipItemStack; import buildcraft.robotics.ai.AIRobotFetchAndEquipItemStack;
import buildcraft.robotics.ai.AIRobotGotoBlock;
import buildcraft.robotics.ai.AIRobotGotoSleep; import buildcraft.robotics.ai.AIRobotGotoSleep;
import buildcraft.robotics.ai.AIRobotSearchBlock; import buildcraft.robotics.ai.AIRobotSearchAndGotoBlock;
import buildcraft.robotics.statements.ActionRobotFilter; import buildcraft.robotics.statements.ActionRobotFilter;
import buildcraft.transport.gates.ActionIterator; import buildcraft.transport.gates.ActionIterator;
public abstract class BoardRobotGenericBreakBlock extends RedstoneBoardRobot { public abstract class BoardRobotGenericBreakBlock extends RedstoneBoardRobot {
protected BlockIndex indexStored; protected BlockIndex blockFound;
private ArrayList<Block> blockFilter = new ArrayList<Block>(); private ArrayList<Block> blockFilter = new ArrayList<Block>();
private ArrayList<Integer> metaFilter = new ArrayList<Integer>(); private ArrayList<Integer> metaFilter = new ArrayList<Integer>();
@ -53,17 +52,6 @@ public abstract class BoardRobotGenericBreakBlock extends RedstoneBoardRobot {
*/ */
public abstract boolean isExpectedBlock(World world, int x, int y, int z); public abstract boolean isExpectedBlock(World world, int x, int y, int z);
@Override
public final void preempt(AIRobot ai) {
if (ai instanceof AIRobotSearchBlock) {
BlockIndex index = ((AIRobotSearchBlock) ai).blockFound;
if (robot.getRegistry().isTaken(new ResourceIdBlock(index))) {
abortDelegateAI();
}
}
}
@Override @Override
public final void update() { public final void update() {
if (!isExpectedTool(null) && robot.getHeldItem() == null) { if (!isExpectedTool(null) && robot.getHeldItem() == null) {
@ -76,7 +64,7 @@ public abstract class BoardRobotGenericBreakBlock extends RedstoneBoardRobot {
} else { } else {
updateFilter(); updateFilter();
startDelegateAI(new AIRobotSearchBlock(robot, new IBlockFilter() { startDelegateAI(new AIRobotSearchAndGotoBlock(robot, false, new IBlockFilter() {
@Override @Override
public boolean matches(World world, int x, int y, int z) { public boolean matches(World world, int x, int y, int z) {
if (isExpectedBlock(world, x, y, z) && !robot.getRegistry().isTaken(new ResourceIdBlock(x, y, z))) { if (isExpectedBlock(world, x, y, z) && !robot.getRegistry().isTaken(new ResourceIdBlock(x, y, z))) {
@ -91,34 +79,26 @@ public abstract class BoardRobotGenericBreakBlock extends RedstoneBoardRobot {
@Override @Override
public void delegateAIEnded(AIRobot ai) { public void delegateAIEnded(AIRobot ai) {
if (ai instanceof AIRobotSearchBlock) { if (ai instanceof AIRobotSearchAndGotoBlock) {
if (!ai.success()) { if (ai.success()) {
startDelegateAI(new AIRobotGotoSleep(robot)); blockFound = ((AIRobotSearchAndGotoBlock) ai).getBlockFound();
startDelegateAI(getBlockBreakAI());
} else { } else {
releaseBlockFound(); startDelegateAI(new AIRobotGotoSleep(robot));
AIRobotSearchBlock searchAI = (AIRobotSearchBlock) ai;
if (searchAI.takeResource()) {
indexStored = searchAI.blockFound;
startDelegateAI(new AIRobotGotoBlock(robot, searchAI.path));
} else {
startDelegateAI(new AIRobotGotoSleep(robot));
}
} }
} else if (ai instanceof AIRobotGotoBlock) {
startDelegateAI(getBlockBreakAI());
} else if (ai.getClass().isInstance(getBlockBreakAI())) { } else if (ai.getClass().isInstance(getBlockBreakAI())) {
releaseBlockFound(); releaseBlockFound();
} }
} }
protected AIRobot getBlockBreakAI() { protected AIRobot getBlockBreakAI() {
return new AIRobotBreak(robot, indexStored); return new AIRobotBreak(robot, blockFound);
} }
private void releaseBlockFound() { private void releaseBlockFound() {
if (indexStored != null) { if (blockFound != null) {
robot.getRegistry().release(new ResourceIdBlock(indexStored)); robot.getRegistry().release(new ResourceIdBlock(blockFound));
indexStored = null; blockFound = null;
} }
} }
@ -175,9 +155,9 @@ public abstract class BoardRobotGenericBreakBlock extends RedstoneBoardRobot {
public void writeSelfToNBT(NBTTagCompound nbt) { public void writeSelfToNBT(NBTTagCompound nbt) {
super.writeSelfToNBT(nbt); super.writeSelfToNBT(nbt);
if (indexStored != null) { if (blockFound != null) {
NBTTagCompound sub = new NBTTagCompound(); NBTTagCompound sub = new NBTTagCompound();
indexStored.writeTo(sub); blockFound.writeTo(sub);
nbt.setTag("indexStored", sub); nbt.setTag("indexStored", sub);
} }
} }
@ -187,7 +167,7 @@ public abstract class BoardRobotGenericBreakBlock extends RedstoneBoardRobot {
super.loadSelfFromNBT(nbt); super.loadSelfFromNBT(nbt);
if (nbt.hasKey("indexStored")) { if (nbt.hasKey("indexStored")) {
indexStored = new BlockIndex (nbt.getCompoundTag("indexStored")); blockFound = new BlockIndex (nbt.getCompoundTag("indexStored"));
} }
} }
} }

View file

@ -31,9 +31,8 @@ import buildcraft.core.lib.inventory.filters.CompositeFilter;
import buildcraft.core.lib.inventory.filters.IStackFilter; import buildcraft.core.lib.inventory.filters.IStackFilter;
import buildcraft.core.lib.utils.IBlockFilter; import buildcraft.core.lib.utils.IBlockFilter;
import buildcraft.robotics.ai.AIRobotFetchAndEquipItemStack; import buildcraft.robotics.ai.AIRobotFetchAndEquipItemStack;
import buildcraft.robotics.ai.AIRobotGotoBlock;
import buildcraft.robotics.ai.AIRobotGotoSleep; import buildcraft.robotics.ai.AIRobotGotoSleep;
import buildcraft.robotics.ai.AIRobotSearchRandomBlock; import buildcraft.robotics.ai.AIRobotSearchAndGotoBlock;
import buildcraft.robotics.ai.AIRobotUseToolOnBlock; import buildcraft.robotics.ai.AIRobotUseToolOnBlock;
import buildcraft.robotics.statements.ActionRobotFilter; import buildcraft.robotics.statements.ActionRobotFilter;
@ -110,30 +109,19 @@ public class BoardRobotPlanter extends RedstoneBoardRobot {
} }
}; };
} }
startDelegateAI(new AIRobotSearchRandomBlock(robot, blockFilter)); startDelegateAI(new AIRobotSearchAndGotoBlock(robot, true, blockFilter));
} }
} }
@Override @Override
public void delegateAIEnded(AIRobot ai) { public void delegateAIEnded(AIRobot ai) {
if (ai instanceof AIRobotSearchRandomBlock) { if (ai instanceof AIRobotSearchAndGotoBlock) {
if (!ai.success()) { if (ai.success()) {
startDelegateAI(new AIRobotGotoSleep(robot)); blockFound = ((AIRobotSearchAndGotoBlock) ai).getBlockFound();
startDelegateAI(new AIRobotUseToolOnBlock(robot, blockFound));
} else { } else {
releaseBlockFound(); startDelegateAI(new AIRobotGotoSleep(robot));
AIRobotSearchRandomBlock searchAI = (AIRobotSearchRandomBlock) ai;
if (searchAI.takeResource()) {
blockFound = searchAI.blockFound;
if (searchAI.path.size() > 1) {
searchAI.path.removeLast();
}
startDelegateAI(new AIRobotGotoBlock(robot, searchAI.path));
} else {
startDelegateAI(new AIRobotGotoSleep(robot));
}
} }
} else if (ai instanceof AIRobotGotoBlock) {
startDelegateAI(new AIRobotUseToolOnBlock(robot, blockFound));
} else if (ai instanceof AIRobotUseToolOnBlock) { } else if (ai instanceof AIRobotUseToolOnBlock) {
releaseBlockFound(); releaseBlockFound();
} else if (ai instanceof AIRobotFetchAndEquipItemStack) { } else if (ai instanceof AIRobotFetchAndEquipItemStack) {

View file

@ -30,11 +30,10 @@ import buildcraft.api.statements.IStatementParameter;
import buildcraft.api.statements.StatementParameterItemStack; import buildcraft.api.statements.StatementParameterItemStack;
import buildcraft.api.statements.StatementSlot; import buildcraft.api.statements.StatementSlot;
import buildcraft.core.lib.utils.IBlockFilter; import buildcraft.core.lib.utils.IBlockFilter;
import buildcraft.robotics.ai.AIRobotGotoBlock;
import buildcraft.robotics.ai.AIRobotGotoSleep; import buildcraft.robotics.ai.AIRobotGotoSleep;
import buildcraft.robotics.ai.AIRobotGotoStationAndUnloadFluids; import buildcraft.robotics.ai.AIRobotGotoStationAndUnloadFluids;
import buildcraft.robotics.ai.AIRobotPumpBlock; import buildcraft.robotics.ai.AIRobotPumpBlock;
import buildcraft.robotics.ai.AIRobotSearchBlock; import buildcraft.robotics.ai.AIRobotSearchAndGotoBlock;
import buildcraft.robotics.statements.ActionRobotFilter; import buildcraft.robotics.statements.ActionRobotFilter;
import buildcraft.transport.gates.ActionIterator; import buildcraft.transport.gates.ActionIterator;
@ -62,7 +61,7 @@ public class BoardRobotPump extends RedstoneBoardRobot {
} else { } else {
updateFilter(); updateFilter();
startDelegateAI(new AIRobotSearchBlock(robot, new IBlockFilter() { startDelegateAI(new AIRobotSearchAndGotoBlock(robot, false, new IBlockFilter() {
@Override @Override
public boolean matches(World world, int x, int y, int z) { public boolean matches(World world, int x, int y, int z) {
@ -79,24 +78,12 @@ public class BoardRobotPump extends RedstoneBoardRobot {
@Override @Override
public void delegateAIEnded(AIRobot ai) { public void delegateAIEnded(AIRobot ai) {
if (ai instanceof AIRobotSearchBlock) { if (ai instanceof AIRobotSearchAndGotoBlock) {
if (!ai.success()) { if (ai.success()) {
startDelegateAI(new AIRobotGotoSleep(robot)); blockFound = ((AIRobotSearchAndGotoBlock) ai).getBlockFound();
} else {
releaseBlockFound();
AIRobotSearchBlock searchAI = (AIRobotSearchBlock) ai;
if (searchAI.takeResource()) {
blockFound = searchAI.blockFound;
startDelegateAI(new AIRobotGotoBlock(robot, searchAI.path));
} else {
startDelegateAI(new AIRobotGotoSleep(robot));
}
}
} else if (ai instanceof AIRobotGotoBlock) {
if (!ai.success()) {
startDelegateAI(new AIRobotGotoSleep(robot));
} else {
startDelegateAI(new AIRobotPumpBlock(robot, blockFound)); startDelegateAI(new AIRobotPumpBlock(robot, blockFound));
} else {
startDelegateAI(new AIRobotGotoSleep(robot));
} }
} else if (ai instanceof AIRobotGotoStationAndUnloadFluids) { } else if (ai instanceof AIRobotGotoStationAndUnloadFluids) {
releaseBlockFound(); releaseBlockFound();

View file

@ -20,9 +20,8 @@ import buildcraft.api.robots.ResourceIdBlock;
import buildcraft.core.lib.inventory.filters.IStackFilter; import buildcraft.core.lib.inventory.filters.IStackFilter;
import buildcraft.core.lib.utils.IBlockFilter; import buildcraft.core.lib.utils.IBlockFilter;
import buildcraft.robotics.ai.AIRobotFetchAndEquipItemStack; import buildcraft.robotics.ai.AIRobotFetchAndEquipItemStack;
import buildcraft.robotics.ai.AIRobotGotoBlock;
import buildcraft.robotics.ai.AIRobotGotoSleep; import buildcraft.robotics.ai.AIRobotGotoSleep;
import buildcraft.robotics.ai.AIRobotSearchRandomBlock; import buildcraft.robotics.ai.AIRobotSearchAndGotoBlock;
import buildcraft.robotics.ai.AIRobotStripesHandler; import buildcraft.robotics.ai.AIRobotStripesHandler;
public class BoardRobotStripes extends RedstoneBoardRobot { public class BoardRobotStripes extends RedstoneBoardRobot {
@ -48,7 +47,7 @@ public class BoardRobotStripes extends RedstoneBoardRobot {
} }
})); }));
} else { } else {
startDelegateAI(new AIRobotSearchRandomBlock(robot, new IBlockFilter() { startDelegateAI(new AIRobotSearchAndGotoBlock(robot, true, new IBlockFilter() {
@Override @Override
public boolean matches(World world, int x, int y, int z) { public boolean matches(World world, int x, int y, int z) {
return world.getBlock(x, y, z).isAir(world, x, y, z) return world.getBlock(x, y, z).isAir(world, x, y, z)
@ -60,21 +59,13 @@ public class BoardRobotStripes extends RedstoneBoardRobot {
@Override @Override
public void delegateAIEnded(AIRobot ai) { public void delegateAIEnded(AIRobot ai) {
if (ai instanceof AIRobotSearchRandomBlock) { if (ai instanceof AIRobotSearchAndGotoBlock) {
if (!ai.success()) { if (ai.success()) {
startDelegateAI(new AIRobotGotoSleep(robot)); blockFound = ((AIRobotSearchAndGotoBlock) ai).getBlockFound();
startDelegateAI(new AIRobotStripesHandler(robot, blockFound));
} else { } else {
releaseBlockFound(); startDelegateAI(new AIRobotGotoSleep(robot));
AIRobotSearchRandomBlock searchAI = (AIRobotSearchRandomBlock) ai;
if (searchAI.takeResource()) {
blockFound = searchAI.blockFound;
startDelegateAI(new AIRobotGotoBlock(robot, searchAI.path));
} else {
startDelegateAI(new AIRobotGotoSleep(robot));
}
} }
} else if (ai instanceof AIRobotGotoBlock) {
startDelegateAI(new AIRobotStripesHandler(robot, blockFound));
} else if (ai instanceof AIRobotFetchAndEquipItemStack) { } else if (ai instanceof AIRobotFetchAndEquipItemStack) {
if (robot.getHeldItem() == null) { if (robot.getHeldItem() == null) {
startDelegateAI(new AIRobotGotoSleep(robot)); startDelegateAI(new AIRobotGotoSleep(robot));