Various fixes in robot behavior.

This commit is contained in:
SpaceToad 2014-07-06 13:22:58 +02:00
parent 6de8ad01ea
commit 40c882de25
4 changed files with 59 additions and 8 deletions

View file

@ -30,6 +30,9 @@ public abstract class RedstoneBoardRobot extends AIRobot implements IRedstoneBoa
}
// TODO: we should put the three calls below into one object making sure
// that blocks are released before being assigned again, and which has a
// finalize () method to free potential block upon garbage collection.
public static boolean isFreeBlock(BlockIndex index) {
synchronized (reservedBlocks) {
return !reservedBlocks.contains(index);

View file

@ -19,6 +19,7 @@ import buildcraft.api.core.BlockIndex;
import buildcraft.api.core.BuildCraftAPI;
import buildcraft.api.robots.AIRobot;
import buildcraft.api.robots.EntityRobotBase;
import buildcraft.core.TickHandlerCoreClient;
import buildcraft.core.inventory.filters.IStackFilter;
import buildcraft.core.robots.AIRobotFetchAndEquipItemStack;
import buildcraft.core.robots.AIRobotGotoBlock;
@ -53,7 +54,9 @@ public class BoardRobotFarmer extends RedstoneBoardRobot {
startDelegateAI(new AIRobotSearchBlock(robot, new IBlockFilter() {
@Override
public boolean matches(World world, int x, int y, int z) {
return BuildCraftAPI.isDirtProperty.get(world, x, y, z);
return BuildCraftAPI.isDirtProperty.get(world, x, y, z)
&& RedstoneBoardRobot.isFreeBlock(new BlockIndex(x, y, z))
&& isAirAbove(world, x, y, z);
}
}));
}
@ -64,11 +67,15 @@ public class BoardRobotFarmer extends RedstoneBoardRobot {
if (ai instanceof AIRobotSearchBlock) {
AIRobotSearchBlock searchAI = (AIRobotSearchBlock) ai;
if (searchAI.blockFound == null) {
startDelegateAI(new AIRobotGotoSleep(robot));
} else {
if (searchAI.blockFound != null && RedstoneBoardRobot.reserveBlock(searchAI.blockFound)) {
if (blockFound != null) {
RedstoneBoardRobot.releaseBlock(blockFound);
}
blockFound = searchAI.blockFound;
startDelegateAI(new AIRobotGotoBlock(robot, searchAI.path));
} else {
startDelegateAI(new AIRobotGotoSleep(robot));
}
} else if (ai instanceof AIRobotGotoBlock) {
AIRobotGotoBlock gotoBlock = (AIRobotGotoBlock) ai;
@ -78,6 +85,16 @@ public class BoardRobotFarmer extends RedstoneBoardRobot {
if (robot.getHeldItem() == null) {
startDelegateAI(new AIRobotGotoSleep(robot));
}
} else if (ai instanceof AIRobotUseToolOnBlock) {
RedstoneBoardRobot.releaseBlock(blockFound);
blockFound = null;
}
}
@Override
public void end() {
if (blockFound != null) {
RedstoneBoardRobot.releaseBlock(blockFound);
}
}
@ -98,6 +115,23 @@ public class BoardRobotFarmer extends RedstoneBoardRobot {
if (nbt.hasKey("blockFound")) {
blockFound = new BlockIndex(nbt.getCompoundTag("blockFound"));
if (!RedstoneBoardRobot.reserveBlock(blockFound)) {
blockFound = null;
}
}
}
private boolean isAirAbove(World world, int x, int y, int z) {
synchronized (TickHandlerCoreClient.startSynchronousComputation) {
try {
TickHandlerCoreClient.startSynchronousComputation.wait();
return world.isAirBlock(x, y + 1, z);
} catch (InterruptedException e) {
e.printStackTrace();
return false;
}
}
}
}

View file

@ -111,6 +111,10 @@ public abstract class BoardRobotGenericBreakBlock extends RedstoneBoardRobot {
@Override
public void delegateAIEnded(AIRobot ai) {
if (ai instanceof AIRobotSearchBlock) {
if (indexStored != null) {
RedstoneBoardRobot.releaseBlock(indexStored);
}
indexStored = ((AIRobotSearchBlock) ai).blockFound;
if (indexStored == null) {

View file

@ -84,7 +84,9 @@ public class BoardRobotPlanter extends RedstoneBoardRobot {
startDelegateAI(new AIRobotSearchBlock(robot, new IBlockFilter() {
@Override
public boolean matches(World world, int x, int y, int z) {
return BuildCraftAPI.isFarmlandProperty.get(world, x, y, z) && isAirAbove(world, x, y, z);
return BuildCraftAPI.isFarmlandProperty.get(world, x, y, z)
&& RedstoneBoardRobot.isFreeBlock(new BlockIndex(x, y, z))
&& isAirAbove(world, x, y, z);
}
}));
} else {
@ -113,11 +115,15 @@ public class BoardRobotPlanter extends RedstoneBoardRobot {
} else if (ai instanceof AIRobotSearchBlock) {
AIRobotSearchBlock gotoBlock = (AIRobotSearchBlock) ai;
if (gotoBlock.blockFound == null) {
startDelegateAI(new AIRobotGotoSleep(robot));
} else {
if (gotoBlock.blockFound != null && RedstoneBoardRobot.reserveBlock(gotoBlock.blockFound)) {
if (blockFound != null) {
RedstoneBoardRobot.releaseBlock(blockFound);
}
blockFound = gotoBlock.blockFound;
startDelegateAI(new AIRobotGotoBlock(robot, gotoBlock.path));
} else {
startDelegateAI(new AIRobotGotoSleep(robot));
}
} else if (ai instanceof AIRobotGotoBlock) {
startDelegateAI(new AIRobotUseToolOnBlock(robot, blockFound));
@ -165,6 +171,10 @@ public class BoardRobotPlanter extends RedstoneBoardRobot {
if (nbt.hasKey("blockFound")) {
blockFound = new BlockIndex(nbt.getCompoundTag("blockFound"));
if (!RedstoneBoardRobot.reserveBlock(blockFound)) {
blockFound = null;
}
}
}
}