Merge branch '6.5.x' of github.com:BuildCraft/BuildCraft into 6.5.x

This commit is contained in:
asiekierka 2015-04-25 12:40:03 +02:00
commit 32e4b77b26
5 changed files with 86 additions and 41 deletions

View file

@ -190,7 +190,7 @@ public class TileConstructionMarker extends TileBuildCraft implements IBuildingI
} }
public boolean needsToBuild() { public boolean needsToBuild() {
return bluePrintBuilder != null && !bluePrintBuilder.isDone(this); return !isInvalid() && bluePrintBuilder != null && !bluePrintBuilder.isDone(this);
} }
public BptContext getContext () { public BptContext getContext () {

View file

@ -255,6 +255,7 @@ public class BptBuilderBlueprint extends BptBuilderBase {
public BuildingSlot reserveNextBlock(World world) { public BuildingSlot reserveNextBlock(World world) {
if (buildList.size() != 0) { if (buildList.size() != 0) {
BuildingSlot slot = internalGetNextBlock(world, null); BuildingSlot slot = internalGetNextBlock(world, null);
checkDone();
if (slot != null) { if (slot != null) {
slot.reserved = true; slot.reserved = true;

View file

@ -243,7 +243,9 @@ public class PathFinding implements IIterableAlgorithm {
int y = from.index.y + dy; int y = from.index.y + dy;
int z = from.index.z + dz; int z = from.index.z + dz;
if (endReached(x, y, z)) { if (y < 0) {
resultMoves[dx + 1][dy + 1][dz + 1] = 0;
} else if (endReached(x, y, z)) {
resultMoves[dx + 1][dy + 1][dz + 1] = 2; resultMoves[dx + 1][dy + 1][dz + 1] = 2;
} else if (!BuildCraftAPI.isSoftBlock(world, x, y, z)) { } else if (!BuildCraftAPI.isSoftBlock(world, x, y, z)) {
resultMoves[dx + 1][dy + 1][dz + 1] = 0; resultMoves[dx + 1][dy + 1][dz + 1] = 0;

View file

@ -19,6 +19,7 @@ import buildcraft.builders.TileConstructionMarker;
import buildcraft.core.builders.BuildingItem; import buildcraft.core.builders.BuildingItem;
import buildcraft.core.builders.BuildingSlot; import buildcraft.core.builders.BuildingSlot;
import buildcraft.core.lib.inventory.filters.ArrayStackFilter; import buildcraft.core.lib.inventory.filters.ArrayStackFilter;
import buildcraft.robotics.ai.AIRobotDisposeItems;
import buildcraft.robotics.ai.AIRobotGotoBlock; import buildcraft.robotics.ai.AIRobotGotoBlock;
import buildcraft.robotics.ai.AIRobotGotoSleep; import buildcraft.robotics.ai.AIRobotGotoSleep;
import buildcraft.robotics.ai.AIRobotGotoStationAndLoad; import buildcraft.robotics.ai.AIRobotGotoStationAndLoad;
@ -26,6 +27,8 @@ import buildcraft.robotics.ai.AIRobotRecharge;
public class BoardRobotBuilder extends RedstoneBoardRobot { public class BoardRobotBuilder extends RedstoneBoardRobot {
private static final int MAX_RANGE_SQ = 3 * 64 * 64;
private TileConstructionMarker markerToBuild; private TileConstructionMarker markerToBuild;
private BuildingSlot currentBuildingSlot; private BuildingSlot currentBuildingSlot;
private LinkedList<ItemStack> requirementsToLookFor; private LinkedList<ItemStack> requirementsToLookFor;
@ -48,69 +51,76 @@ public class BoardRobotBuilder extends RedstoneBoardRobot {
} }
if (markerToBuild == null) { if (markerToBuild == null) {
double minDistance = Double.MAX_VALUE; markerToBuild = findClosestMarker();
for (TileConstructionMarker marker : TileConstructionMarker.currentMarkers) {
if (marker.getWorldObj() == robot.worldObj && marker.needsToBuild()) {
double dx = robot.posX - marker.xCoord;
double dy = robot.posY - marker.yCoord;
double dz = robot.posZ - marker.zCoord;
double distance = dx * dx + dy * dy + dz * dz;
if (distance < minDistance) {
markerToBuild = marker;
minDistance = distance;
}
}
}
if (markerToBuild == null) { if (markerToBuild == null) {
startDelegateAI(new AIRobotGotoSleep(robot)); if (robot.containsItems()) {
startDelegateAI(new AIRobotDisposeItems(robot));
} else {
startDelegateAI(new AIRobotGotoSleep(robot));
}
return; return;
} }
} }
if (markerToBuild == null || !markerToBuild.needsToBuild()) { if (!markerToBuild.needsToBuild()) {
markerToBuild = null; markerToBuild = null;
// TODO: review what is this, maybe shutdown the robot currentBuildingSlot = null;
startDelegateAI(new AIRobot(robot));
return; return;
} }
if (currentBuildingSlot == null) { if (currentBuildingSlot == null) {
currentBuildingSlot = markerToBuild.bluePrintBuilder.reserveNextSlot(robot.worldObj); currentBuildingSlot = markerToBuild.bluePrintBuilder.reserveNextSlot(robot.worldObj);
if (currentBuildingSlot != null) { if (currentBuildingSlot == null) {
// The above may return null even if not done, if it's scanning // No slots available yet
// for available blocks. launchingDelay = 40;
requirementsToLookFor = currentBuildingSlot.getRequirements(markerToBuild.getContext()); return;
} }
// TODO: what if there's more requirements that this robot can
// handle e.g. not enough free spots? If there's more than X slots
// found that can't be built, go to sleep.
} }
if (requirementsToLookFor != null && requirementsToLookFor.size() > 0) { if (requirementsToLookFor == null) {
if (robot.containsItems()) {
startDelegateAI(new AIRobotDisposeItems(robot));
}
requirementsToLookFor = currentBuildingSlot.getRequirements(markerToBuild
.getContext());
if (requirementsToLookFor == null) {
launchingDelay = 40;
return;
}
if (requirementsToLookFor.size() > 4) {
currentBuildingSlot.built = true;
currentBuildingSlot = null;
requirementsToLookFor = null;
}
}
if (requirementsToLookFor.size() > 0) {
startDelegateAI(new AIRobotGotoStationAndLoad(robot, new ArrayStackFilter( startDelegateAI(new AIRobotGotoStationAndLoad(robot, new ArrayStackFilter(
requirementsToLookFor.getFirst()), requirementsToLookFor.getFirst().stackSize)); requirementsToLookFor.getFirst()), requirementsToLookFor.getFirst().stackSize));
return;
} }
if (currentBuildingSlot != null && requirementsToLookFor != null && requirementsToLookFor.size() == 0) { if (requirementsToLookFor.size() == 0) {
if (currentBuildingSlot.stackConsumed == null) { if (currentBuildingSlot.stackConsumed == null) {
// Once all the element are in, if not already, use them to // Once all the element are in, if not already, use them to
// prepare the slot. // prepare the slot.
markerToBuild.bluePrintBuilder.useRequirements(robot, currentBuildingSlot); markerToBuild.bluePrintBuilder.useRequirements(robot, currentBuildingSlot);
} }
if (robot.getEnergy() - currentBuildingSlot.getEnergyRequirement() < EntityRobotBase.SAFETY_ENERGY) { if (!hasEnoughEnergy()) {
startDelegateAI(new AIRobotRecharge(robot)); startDelegateAI(new AIRobotRecharge(robot));
} else { } else {
startDelegateAI(new AIRobotGotoBlock(robot, startDelegateAI(new AIRobotGotoBlock(robot,
(int) currentBuildingSlot.getDestination().x, (int) currentBuildingSlot.getDestination().x,
(int) currentBuildingSlot.getDestination().y, (int) currentBuildingSlot.getDestination().y,
(int) currentBuildingSlot.getDestination().z, (int) currentBuildingSlot.getDestination().z,
8)); 8));
} }
// TODO: take into account cases where the robot can't reach the // TODO: take into account cases where the robot can't reach the
// destination - go to work on another block // destination - go to work on another block
@ -131,16 +141,17 @@ public class BoardRobotBuilder extends RedstoneBoardRobot {
return; return;
} }
if (robot.getEnergy() - currentBuildingSlot.getEnergyRequirement() < EntityRobotBase.SAFETY_ENERGY) { if (!hasEnoughEnergy()) {
startDelegateAI(new AIRobotRecharge(robot)); startDelegateAI(new AIRobotRecharge(robot));
return; return;
} }
robot.getBattery().extractEnergy(currentBuildingSlot.getEnergyRequirement(), false); robot.getBattery().extractEnergy(currentBuildingSlot.getEnergyRequirement(), false);
launchingDelay = currentBuildingSlot.getStacksToDisplay().size() * BuildingItem.ITEMS_SPACE; launchingDelay = currentBuildingSlot.getStacksToDisplay().size()
markerToBuild.bluePrintBuilder.buildSlot * BuildingItem.ITEMS_SPACE;
(robot.worldObj, markerToBuild, currentBuildingSlot, markerToBuild.bluePrintBuilder.buildSlot(robot.worldObj, markerToBuild,
robot.posX + 0.125F, robot.posY + 0.125F, robot.posZ + 0.125F); currentBuildingSlot, robot.posX + 0.125F, robot.posY + 0.125F,
robot.posZ + 0.125F);
currentBuildingSlot = null; currentBuildingSlot = null;
requirementsToLookFor = null; requirementsToLookFor = null;
} }
@ -159,4 +170,34 @@ public class BoardRobotBuilder extends RedstoneBoardRobot {
launchingDelay = nbt.getInteger("launchingDelay"); launchingDelay = nbt.getInteger("launchingDelay");
} }
private TileConstructionMarker findClosestMarker() {
double minDistance = Double.MAX_VALUE;
TileConstructionMarker minMarker = null;
for (TileConstructionMarker marker : TileConstructionMarker.currentMarkers) {
if (marker.getWorldObj() == robot.worldObj && marker.needsToBuild()) {
double dx = robot.posX - marker.xCoord;
double dy = robot.posY - marker.yCoord;
double dz = robot.posZ - marker.zCoord;
double distance = dx * dx + dy * dy + dz * dz;
if (distance < minDistance) {
minMarker = marker;
minDistance = distance;
}
}
}
if (minMarker != null && minDistance < MAX_RANGE_SQ) {
return minMarker;
} else {
return null;
}
}
private boolean hasEnoughEnergy() {
return robot.getEnergy() - currentBuildingSlot.getEnergyRequirement() > EntityRobotBase.SAFETY_ENERGY;
}
} }

View file

@ -12,6 +12,7 @@ import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.init.Blocks; import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.ItemBlock; import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemReed; import net.minecraft.item.ItemReed;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
@ -154,7 +155,7 @@ public class BoardRobotPlanter extends RedstoneBoardRobot {
private static class ReedFilter implements IStackFilter { private static class ReedFilter implements IStackFilter {
@Override @Override
public boolean matches(ItemStack stack) { public boolean matches(ItemStack stack) {
return stack.getItem() instanceof ItemReed; return stack.getItem() == Items.reeds;
} }
} }