Factorised station scan code.
Fixed world property analysis.
This commit is contained in:
parent
6f8466a8f4
commit
afbfe26fa0
9 changed files with 222 additions and 83 deletions
|
@ -69,7 +69,6 @@ public class AIRobot {
|
|||
public final void abortDelegateAI() {
|
||||
if (delegateAI != null) {
|
||||
delegateAI.terminate();
|
||||
delegateAI = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
71
common/buildcraft/core/robots/AIRobotLookForStation.java
Executable file
71
common/buildcraft/core/robots/AIRobotLookForStation.java
Executable file
|
@ -0,0 +1,71 @@
|
|||
/**
|
||||
* Copyright (c) 2011-2014, 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.core.robots;
|
||||
|
||||
import buildcraft.api.robots.AIRobot;
|
||||
import buildcraft.api.robots.DockingStationRegistry;
|
||||
import buildcraft.api.robots.EntityRobotBase;
|
||||
import buildcraft.api.robots.IDockingStation;
|
||||
|
||||
public class AIRobotLookForStation extends AIRobot {
|
||||
|
||||
private DockingStation axeDocking = null;
|
||||
private IStationFilter filter;
|
||||
|
||||
public AIRobotLookForStation(EntityRobotBase iRobot, IStationFilter iFilter) {
|
||||
super(iRobot);
|
||||
|
||||
filter = iFilter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
if (robot.getDockingStation() != null
|
||||
&& filter.matches((DockingStation) robot.getDockingStation())) {
|
||||
terminate();
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: replace FetchItemStack and Recharge with this implementation
|
||||
|
||||
double potentialStationDistance = Float.MAX_VALUE;
|
||||
DockingStation potentialStation = null;
|
||||
|
||||
for (IDockingStation d : DockingStationRegistry.getStations()) {
|
||||
DockingStation station = (DockingStation) d;
|
||||
|
||||
if (station.reserved() != null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (filter.matches(station)) {
|
||||
double dx = robot.posX - d.x();
|
||||
double dy = robot.posY - d.y();
|
||||
double dz = robot.posZ - d.z();
|
||||
double distance = dx * dx + dy + dy + dz * dz;
|
||||
|
||||
if (potentialStation == null || distance < potentialStationDistance) {
|
||||
potentialStation = station;
|
||||
potentialStationDistance = distance;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (potentialStation != null) {
|
||||
startDelegateAI(new AIRobotGoToDock(robot, potentialStation));
|
||||
} else {
|
||||
terminate();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delegateAIEnded(AIRobot ai) {
|
||||
terminate();
|
||||
}
|
||||
}
|
|
@ -9,9 +9,7 @@
|
|||
package buildcraft.core.robots;
|
||||
|
||||
import buildcraft.api.robots.AIRobot;
|
||||
import buildcraft.api.robots.DockingStationRegistry;
|
||||
import buildcraft.api.robots.EntityRobotBase;
|
||||
import buildcraft.api.robots.IDockingStation;
|
||||
import buildcraft.api.transport.IPipeTile.PipeType;
|
||||
import buildcraft.transport.PipeTransportPower;
|
||||
|
||||
|
@ -23,33 +21,34 @@ public class AIRobotRecharge extends AIRobot {
|
|||
super(iRobot);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
startDelegateAI(new AIRobotLookForStation(robot, new IStationFilter() {
|
||||
@Override
|
||||
public boolean matches(DockingStation station) {
|
||||
return station.pipe.getPipeType() == PipeType.POWER;
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
PipeTransportPower powerProvider = (PipeTransportPower) ((DockingStation) robot.getDockingStation()).pipe.pipe.transport;
|
||||
|
||||
powerProvider.requestEnergy(robot.getDockingStation().side(), 100);
|
||||
robot.setEnergy(robot.getEnergy()
|
||||
+ powerProvider.consumePower(robot.getDockingStation().side(), 100));
|
||||
|
||||
if (robot.getEnergy() >= EntityRobotBase.MAX_ENERGY) {
|
||||
terminate();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delegateAIEnded(AIRobot ai) {
|
||||
if (robot.getDockingStation() == null
|
||||
|| ((DockingStation) robot.getDockingStation()).pipe.getPipeType() != PipeType.POWER) {
|
||||
|
||||
for (IDockingStation d : DockingStationRegistry.getStations()) {
|
||||
DockingStation station = (DockingStation) d;
|
||||
|
||||
if (station.reserved() != null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (station.pipe.getPipeType() == PipeType.POWER) {
|
||||
startDelegateAI(new AIRobotGoToDock(robot, station));
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
PipeTransportPower powerProvider = (PipeTransportPower) ((DockingStation) robot.getDockingStation()).pipe.pipe.transport;
|
||||
|
||||
powerProvider.requestEnergy(robot.getDockingStation().side(), 100);
|
||||
robot.setEnergy(robot.getEnergy()
|
||||
+ powerProvider.consumePower(robot.getDockingStation().side(), 100));
|
||||
|
||||
if (robot.getEnergy() >= EntityRobotBase.MAX_ENERGY) {
|
||||
terminate();
|
||||
}
|
||||
|| !(((DockingStation) robot.getDockingStation()).pipe.pipe.transport instanceof PipeTransportPower)) {
|
||||
terminate ();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
14
common/buildcraft/core/robots/IStationFilter.java
Executable file
14
common/buildcraft/core/robots/IStationFilter.java
Executable file
|
@ -0,0 +1,14 @@
|
|||
/**
|
||||
* Copyright (c) 2011-2014, 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.core.robots;
|
||||
|
||||
public interface IStationFilter {
|
||||
|
||||
boolean matches(DockingStation station);
|
||||
}
|
|
@ -15,14 +15,13 @@ import net.minecraft.tileentity.TileEntity;
|
|||
import net.minecraftforge.common.util.ForgeDirection;
|
||||
|
||||
import buildcraft.api.robots.AIRobot;
|
||||
import buildcraft.api.robots.DockingStationRegistry;
|
||||
import buildcraft.api.robots.EntityRobotBase;
|
||||
import buildcraft.api.robots.IDockingStation;
|
||||
import buildcraft.core.inventory.ITransactor;
|
||||
import buildcraft.core.inventory.Transactor;
|
||||
import buildcraft.core.inventory.filters.IStackFilter;
|
||||
import buildcraft.core.robots.AIRobotGoToDock;
|
||||
import buildcraft.core.robots.AIRobotLookForStation;
|
||||
import buildcraft.core.robots.DockingStation;
|
||||
import buildcraft.core.robots.IStationFilter;
|
||||
|
||||
public class AIRobotFetchItemStack extends AIRobot {
|
||||
|
||||
|
@ -37,29 +36,7 @@ public class AIRobotFetchItemStack extends AIRobot {
|
|||
|
||||
@Override
|
||||
public void update() {
|
||||
for (IDockingStation d : DockingStationRegistry.getStations()) {
|
||||
DockingStation station = (DockingStation) d;
|
||||
|
||||
if (station.reserved() != null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
|
||||
TileEntity nearbyTile = robot.worldObj.getTileEntity(d.x() + dir.offsetX, d.y()
|
||||
+ dir.offsetY, d.z()
|
||||
+ dir.offsetZ);
|
||||
|
||||
if (nearbyTile != null && nearbyTile instanceof IInventory) {
|
||||
ITransactor trans = Transactor.getTransactorFor(nearbyTile);
|
||||
|
||||
if (trans.remove(filter, dir.getOpposite(), false) != null) {
|
||||
stationToDock = station;
|
||||
startDelegateAI(new AIRobotGoToDock(robot, stationToDock));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
startDelegateAI(new AIRobotLookForStation(robot, new StationFilter()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -87,4 +64,28 @@ public class AIRobotFetchItemStack extends AIRobot {
|
|||
terminate();
|
||||
}
|
||||
}
|
||||
|
||||
private class StationFilter implements IStationFilter {
|
||||
|
||||
@Override
|
||||
public boolean matches(DockingStation station) {
|
||||
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
|
||||
TileEntity nearbyTile = robot.worldObj.getTileEntity(station.x() + dir.offsetX, station.y()
|
||||
+ dir.offsetY, station.z()
|
||||
+ dir.offsetZ);
|
||||
|
||||
if (nearbyTile != null && nearbyTile instanceof IInventory) {
|
||||
ITransactor trans = Transactor.getTransactorFor(nearbyTile);
|
||||
|
||||
if (trans.remove(filter, dir.getOpposite(), false) != null) {
|
||||
stationToDock = station;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,8 +11,12 @@ package buildcraft.core.robots.boards;
|
|||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import net.minecraft.inventory.IInventory;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
|
||||
import net.minecraftforge.common.util.ForgeDirection;
|
||||
|
||||
import buildcraft.api.boards.IBoardParameter;
|
||||
import buildcraft.api.boards.IBoardParameterStack;
|
||||
|
@ -23,13 +27,13 @@ import buildcraft.api.boards.RedstoneBoardRobotNBT;
|
|||
import buildcraft.api.core.SafeTimeTracker;
|
||||
import buildcraft.api.robots.AIRobot;
|
||||
import buildcraft.api.robots.EntityRobotBase;
|
||||
import buildcraft.core.inventory.ITransactor;
|
||||
import buildcraft.core.inventory.Transactor;
|
||||
import buildcraft.core.inventory.filters.ArrayStackFilter;
|
||||
import buildcraft.core.inventory.filters.IStackFilter;
|
||||
import buildcraft.core.robots.AIRobotGoToDock;
|
||||
import buildcraft.core.robots.AIRobotLookForStation;
|
||||
import buildcraft.core.robots.DockingStation;
|
||||
import buildcraft.transport.PipeTransportItems;
|
||||
import buildcraft.transport.TileGenericPipe;
|
||||
import buildcraft.transport.TravelingItem;
|
||||
import buildcraft.core.robots.IStationFilter;
|
||||
|
||||
public class BoardRobotPicker extends RedstoneBoardRobot {
|
||||
|
||||
|
@ -81,39 +85,36 @@ public class BoardRobotPicker extends RedstoneBoardRobot {
|
|||
// if we could get an item, let's try to get another one
|
||||
startDelegateAI(new AIRobotFetchItem(robot, range, stackFilter));
|
||||
} else {
|
||||
// otherwise, let's return to base
|
||||
startDelegateAI(new AIRobotGoToDock(robot, (DockingStation) robot.getLinkedStation()));
|
||||
// otherwise, let's deliver items
|
||||
startDelegateAI(new AIRobotLookForStation(robot, new StationInventory()));
|
||||
}
|
||||
} else if (ai instanceof AIRobotGoToDock) {
|
||||
emptyContainerInStation();
|
||||
} else if (ai instanceof AIRobotLookForStation) {
|
||||
emptyContainerInInventory();
|
||||
}
|
||||
}
|
||||
|
||||
private void emptyContainerInStation() {
|
||||
private void emptyContainerInInventory() {
|
||||
DockingStation station = (DockingStation) robot.getDockingStation();
|
||||
|
||||
if (station == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
TileGenericPipe pipe = (TileGenericPipe) robot.worldObj
|
||||
.getTileEntity(station.pipe.xCoord, station.pipe.yCoord, station.pipe.zCoord);
|
||||
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
|
||||
TileEntity nearbyTile = robot.worldObj.getTileEntity(station.x() + dir.offsetX, station.y()
|
||||
+ dir.offsetY, station.z()
|
||||
+ dir.offsetZ);
|
||||
|
||||
if (pipe != null && pipe.pipe.transport instanceof PipeTransportItems) {
|
||||
for (int i = 0; i < robot.getSizeInventory(); ++i) {
|
||||
if (robot.getStackInSlot(i) != null) {
|
||||
float cx = station.pipe.xCoord + 0.5F + 0.2F * station.side.offsetX;
|
||||
float cy = station.pipe.yCoord + 0.5F + 0.2F * station.side.offsetY;
|
||||
float cz = station.pipe.zCoord + 0.5F + 0.2F * station.side.offsetZ;
|
||||
if (nearbyTile != null && nearbyTile instanceof IInventory) {
|
||||
ITransactor trans = Transactor.getTransactorFor(nearbyTile);
|
||||
|
||||
TravelingItem item = TravelingItem.make(cx, cy,
|
||||
cz, robot.getStackInSlot(i));
|
||||
for (int i = 0; i < robot.getSizeInventory(); ++i) {
|
||||
ItemStack stackToAdd = robot.getStackInSlot(i);
|
||||
|
||||
((PipeTransportItems) pipe.pipe.transport).injectItem(item, station.side.getOpposite());
|
||||
|
||||
robot.setInventorySlotContents(i, null);
|
||||
|
||||
break;
|
||||
if (stackToAdd != null) {
|
||||
ItemStack added = trans.add(stackToAdd, dir, true);
|
||||
robot.decrStackSize(i, added.stackSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -123,4 +124,31 @@ public class BoardRobotPicker extends RedstoneBoardRobot {
|
|||
public RedstoneBoardRobotNBT getNBTHandler() {
|
||||
return BoardRobotPickerNBT.instance;
|
||||
}
|
||||
|
||||
private class StationInventory implements IStationFilter {
|
||||
@Override
|
||||
public boolean matches(DockingStation station) {
|
||||
for (ForgeDirection dir : ForgeDirection.VALID_DIRECTIONS) {
|
||||
TileEntity nearbyTile = robot.worldObj.getTileEntity(station.x() + dir.offsetX, station.y()
|
||||
+ dir.offsetY, station.z()
|
||||
+ dir.offsetZ);
|
||||
|
||||
if (nearbyTile != null && nearbyTile instanceof IInventory) {
|
||||
ITransactor trans = Transactor.getTransactorFor(nearbyTile);
|
||||
|
||||
for (int i = 0; i < robot.getSizeInventory(); ++i) {
|
||||
ItemStack stackToAdd = robot.getStackInSlot(i);
|
||||
|
||||
if (stackToAdd != null && trans.add(stackToAdd, dir, false) != null) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,7 +24,20 @@ public class WorldPropertyIsLeave extends WorldProperty {
|
|||
|
||||
@Override
|
||||
public boolean get(IBlockAccess blockAccess, Block block, int meta, int x, int y, int z) {
|
||||
return block != null
|
||||
&& OreDictionary.getOreID(new ItemStack(block, 1, OreDictionary.WILDCARD_VALUE)) == leavesId;
|
||||
if (block == null) {
|
||||
return false;
|
||||
} else {
|
||||
ItemStack stack = new ItemStack(block);
|
||||
|
||||
if (stack.getItem() != null) {
|
||||
for (int id : OreDictionary.getOreIDs(stack)) {
|
||||
if (id == leavesId) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,6 +24,20 @@ public class WorldPropertyIsWood extends WorldProperty {
|
|||
|
||||
@Override
|
||||
public boolean get(IBlockAccess blockAccess, Block block, int meta, int x, int y, int z) {
|
||||
return block != null && OreDictionary.getOreID(new ItemStack(block, 1, OreDictionary.WILDCARD_VALUE)) == woodId;
|
||||
if (block == null) {
|
||||
return false;
|
||||
} else {
|
||||
ItemStack stack = new ItemStack(block);
|
||||
|
||||
if (stack.getItem() != null) {
|
||||
for (int id : OreDictionary.getOreIDs(stack)) {
|
||||
if (id == woodId) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1179,9 +1179,9 @@ public class BlockGenericPipe extends BlockBuildCraft {
|
|||
skippedFirstIconRegister = true;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
BuildCraftTransport.instance.wireIconProvider.registerIcons(iconRegister);
|
||||
|
||||
|
||||
for (Item i : pipes.keySet()) {
|
||||
Pipe<?> dummyPipe = createPipe(i);
|
||||
if (dummyPipe != null) {
|
||||
|
@ -1317,7 +1317,7 @@ public class BlockGenericPipe extends BlockBuildCraft {
|
|||
if (facadeRenderColor != -1) {
|
||||
return facadeRenderColor;
|
||||
}
|
||||
|
||||
|
||||
return super.colorMultiplier(world, x, y, z);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue