Skip to content

Commit

Permalink
Generalized latency property, for #1873
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaceToad committed Jun 22, 2014
1 parent 4428b44 commit eeb637d
Show file tree
Hide file tree
Showing 18 changed files with 52 additions and 31 deletions.
4 changes: 2 additions & 2 deletions api/buildcraft/api/boards/RedstoneBoardRobot.java
Expand Up @@ -18,8 +18,8 @@ public abstract class RedstoneBoardRobot extends AIRobot implements IRedstoneBoa

public static HashSet<BlockIndex> reservedBlocks = new HashSet<BlockIndex>();

public RedstoneBoardRobot(EntityRobotBase iRobot) {
super(iRobot);
public RedstoneBoardRobot(EntityRobotBase iRobot, int updateLatency) {
super(iRobot, 0, updateLatency);
}

@Override
Expand Down
13 changes: 13 additions & 0 deletions api/buildcraft/api/gates/ActionState.java
@@ -0,0 +1,13 @@
/**
* 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.api.gates;

public class ActionState {

}
18 changes: 11 additions & 7 deletions api/buildcraft/api/robots/AIRobot.java
@@ -1,21 +1,23 @@

package buildcraft.api.robots;

import buildcraft.api.core.SafeTimeTracker;

public class AIRobot {
public EntityRobotBase robot;

private AIRobot delegateAI;
private AIRobot parentAI;
private double energyCost;
private SafeTimeTracker updateTracker;

public AIRobot(EntityRobotBase iRobot, double iEnergyCost) {
public AIRobot(EntityRobotBase iRobot, double iEnergyCost, int updateLatency) {
robot = iRobot;
energyCost = iEnergyCost;
}

public AIRobot(EntityRobotBase iRobot) {
robot = iRobot;
energyCost = 0;
if (updateLatency > 1) {
updateTracker = new SafeTimeTracker(updateLatency, updateLatency / 5);
}
}

public void start() {
Expand Down Expand Up @@ -54,8 +56,10 @@ public final void cycle() {
if (delegateAI != null) {
delegateAI.cycle();
} else {
robot.setEnergy(robot.getEnergy() - energyCost);
update();
if (updateTracker == null || updateTracker.markTimeIfDelay(robot.worldObj)) {
robot.setEnergy(robot.getEnergy() - energyCost);
update();
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion common/buildcraft/core/robots/AIRobotGoAndLinkToDock.java
Expand Up @@ -16,7 +16,7 @@ public class AIRobotGoAndLinkToDock extends AIRobot {
public DockingStation station;

public AIRobotGoAndLinkToDock(EntityRobotBase iRobot, DockingStation iStation) {
super(iRobot);
super(iRobot, 0, 1);

station = iStation;
}
Expand Down
2 changes: 1 addition & 1 deletion common/buildcraft/core/robots/AIRobotGoToDock.java
Expand Up @@ -16,7 +16,7 @@ public class AIRobotGoToDock extends AIRobot {
private DockingStation station;

public AIRobotGoToDock(EntityRobotBase iRobot, DockingStation iStation) {
super(iRobot);
super(iRobot, 0, 1);

station = iStation;
}
Expand Down
5 changes: 3 additions & 2 deletions common/buildcraft/core/robots/AIRobotLookForStation.java
Expand Up @@ -15,11 +15,11 @@

public class AIRobotLookForStation extends AIRobot {

private DockingStation axeDocking = null;
public DockingStation targetStation;
private IStationFilter filter;

public AIRobotLookForStation(EntityRobotBase iRobot, IStationFilter iFilter) {
super(iRobot);
super(iRobot, 0, 1);

filter = iFilter;
}
Expand Down Expand Up @@ -56,6 +56,7 @@ public void start() {
}

if (potentialStation != null) {
targetStation = potentialStation;
startDelegateAI(new AIRobotGoToDock(robot, potentialStation));
} else {
terminate();
Expand Down
2 changes: 1 addition & 1 deletion common/buildcraft/core/robots/AIRobotMain.java
Expand Up @@ -16,7 +16,7 @@ public class AIRobotMain extends AIRobot {
private AIRobot overridingAI;

public AIRobotMain(EntityRobotBase iRobot) {
super(iRobot);
super(iRobot, 0, 1);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion common/buildcraft/core/robots/AIRobotMove.java
Expand Up @@ -17,7 +17,7 @@ public abstract class AIRobotMove extends AIRobot {
protected double dirX, dirY, dirZ;

public AIRobotMove(EntityRobotBase iRobot) {
super(iRobot, 1);
super(iRobot, 1, 1);
}

protected void setDestination(EntityRobotBase robot, float x, float y, float z) {
Expand Down
2 changes: 1 addition & 1 deletion common/buildcraft/core/robots/AIRobotRecharge.java
Expand Up @@ -18,7 +18,7 @@ public class AIRobotRecharge extends AIRobot {
private DockingStation axeDocking = null;

public AIRobotRecharge(EntityRobotBase iRobot) {
super(iRobot);
super(iRobot, 0, 1);
}

@Override
Expand Down
Expand Up @@ -32,7 +32,7 @@ public class AIRobotBreakWithTool extends AIRobot {
private float speed;

public AIRobotBreakWithTool(EntityRobotBase iRobot, BlockIndex iBlockToBreak) {
super(iRobot, 2);
super(iRobot, 2, 1);

blockToBreak = iBlockToBreak;
}
Expand Down
2 changes: 1 addition & 1 deletion common/buildcraft/core/robots/boards/AIRobotFetchItem.java
Expand Up @@ -28,7 +28,7 @@ public class AIRobotFetchItem extends AIRobot {
private int pickTime = -1;

public AIRobotFetchItem(EntityRobotBase iRobot, float iMaxRange, IStackFilter iStackFilter) {
super(iRobot);
super(iRobot, 0, 1);

maxRange = iMaxRange;
stackFilter = iStackFilter;
Expand Down
Expand Up @@ -30,7 +30,7 @@ public class AIRobotFetchItemStack extends AIRobot {
private IStackFilter filter;

public AIRobotFetchItemStack(EntityRobotBase iRobot, IStackFilter iFilter) {
super(iRobot);
super(iRobot, 0, 1);

filter = iFilter;
}
Expand Down
Expand Up @@ -31,7 +31,7 @@ public class AIRobotGoToRandomDirt extends AIRobot {
private PathFindingJob pathFindingJob;

public AIRobotGoToRandomDirt(EntityRobotBase iRobot, int iRange) {
super(iRobot, 2);
super(iRobot, 2, 1);

range = iRange;
}
Expand Down
Expand Up @@ -22,7 +22,7 @@ public class AIRobotPlantSaple extends AIRobot {
private int plantCycles = 0;

public AIRobotPlantSaple(EntityRobotBase iRobot, BlockIndex index) {
super(iRobot, 2);
super(iRobot, 2, 1);

toPlant = index;
}
Expand Down
Expand Up @@ -26,7 +26,7 @@ public class AIRobotSearchBlock extends AIRobot {
private IPathFound pathFound;

public AIRobotSearchBlock(EntityRobotBase iRobot, IPathFound iPathFound) {
super(iRobot, 2);
super(iRobot, 2, 1);

pathFound = iPathFound;
}
Expand Down
Expand Up @@ -21,7 +21,7 @@
public abstract class BoardRobotGenericBreakBlock extends RedstoneBoardRobot {

public BoardRobotGenericBreakBlock(EntityRobotBase iRobot) {
super(iRobot);
super(iRobot, 1);
}

public abstract boolean isExpectedTool(ItemStack stack);
Expand Down
17 changes: 10 additions & 7 deletions common/buildcraft/core/robots/boards/BoardRobotPicker.java
Expand Up @@ -24,7 +24,6 @@
import buildcraft.api.boards.RedstoneBoardRegistry;
import buildcraft.api.boards.RedstoneBoardRobot;
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;
Expand All @@ -42,16 +41,14 @@ public class BoardRobotPicker extends RedstoneBoardRobot {
// TODO: Clean this when world unloaded
public static Set<Integer> targettedItems = new HashSet<Integer>();

private SafeTimeTracker scanTracker = new SafeTimeTracker(40, 10);

private NBTTagCompound data;
private RedstoneBoardNBT<?> board;
private IBoardParameter[] params;
private int range;
private IStackFilter stackFilter;

public BoardRobotPicker(EntityRobotBase robot, NBTTagCompound nbt) {
super(robot);
super(robot, 40);
data = nbt;

board = RedstoneBoardRegistry.instance.getRedstoneBoard(nbt);
Expand All @@ -75,9 +72,7 @@ public BoardRobotPicker(EntityRobotBase robot, NBTTagCompound nbt) {

@Override
public void update() {
if (scanTracker.markTimeIfDelay(robot.worldObj)) {
startDelegateAI(new AIRobotFetchItem(robot, range, stackFilter));
}
startDelegateAI(new AIRobotFetchItem(robot, range, stackFilter));
}

@Override
Expand Down Expand Up @@ -108,6 +103,10 @@ private void emptyContainerInInventory() {
boolean found = false;
ItemStack stackToAdd = robot.getStackInSlot(i);

if (stackToAdd == null) {
continue;
}

for (Object s : pipe.getActionStates()) {
if (s instanceof StateStationRequestItems) {
if (((StateStationRequestItems) s).matches(new ArrayStackFilter(stackToAdd))) {
Expand Down Expand Up @@ -154,6 +153,10 @@ public boolean matches(DockingStation station) {
boolean found = false;
ItemStack stackToAdd = robot.getStackInSlot(i);

if (stackToAdd == null) {
continue;
}

for (Object s : pipe.getActionStates()) {
if (s instanceof StateStationRequestItems) {
if (((StateStationRequestItems) s).matches(new ArrayStackFilter(stackToAdd))) {
Expand Down
Expand Up @@ -17,7 +17,7 @@
public class BoardRobotPlanter extends RedstoneBoardRobot {

public BoardRobotPlanter(EntityRobotBase iRobot) {
super(iRobot);
super(iRobot, 1);
}

@Override
Expand Down

0 comments on commit eeb637d

Please sign in to comment.