Fixed lasers looking for tables on a regular basis, fix #1400

Attempt at improving SafeTimeTracker interface and implement
automatic randomization. If interface is confirmed, other places
in the code will need to be updated to the interface.
This commit is contained in:
SpaceToad 2014-01-05 17:47:48 +01:00
parent c4cbdcc584
commit 69079dab94
2 changed files with 51 additions and 19 deletions

View file

@ -13,10 +13,45 @@ public class SafeTimeTracker {
private long lastMark = Long.MIN_VALUE; private long lastMark = Long.MIN_VALUE;
private long duration = -1; private long duration = -1;
private long randomRange = 0;
private long lastRandomDelay = 0;
private long internalDelay = 1;
/**
* @deprecated should use constructors with parameters instead
*/
public SafeTimeTracker () {
}
public SafeTimeTracker (long delay) {
internalDelay = delay;
}
/**
* In many situations, it is a bad idea to have all objects of the same
* kind to be waiting for the exact same amount of time, as that can lead
* to some situation where they're all synchronized and got to work all
* at the same time. When created with a random range, the mark that is set
* when reaching the expect delay will be added with a random number
* between [0, range[, meaning that the event will take between 0 and range
* more tick to run.
*/
public SafeTimeTracker (long delay, long random) {
internalDelay = delay;
randomRange = random;
}
public boolean markTimeIfDelay(World world) {
return markTimeIfDelay(world, internalDelay);
}
/** /**
* Return true if a given delay has passed since last time marked was called * Return true if a given delay has passed since last time marked was called
* successfully. * successfully.
*
* @deprecated should use the constructor with a delay instead, and call
* this function without a parameter
*/ */
public boolean markTimeIfDelay(World world, long delay) { public boolean markTimeIfDelay(World world, long delay) {
if (world == null) if (world == null)
@ -27,13 +62,15 @@ public class SafeTimeTracker {
if (currentTime < lastMark) { if (currentTime < lastMark) {
lastMark = currentTime; lastMark = currentTime;
return false; return false;
} else if (lastMark + delay <= currentTime) { } else if (lastMark + delay + lastRandomDelay <= currentTime) {
duration = currentTime - lastMark; duration = currentTime - lastMark;
lastMark = currentTime; lastMark = currentTime;
return true; lastRandomDelay = (int) (Math.random() * randomRange);
} else
return false;
return true;
} else {
return false;
}
} }
public long durationOfLastDelay() { public long durationOfLastDelay() {

View file

@ -32,14 +32,11 @@ public class TileLaser extends TileBuildCraft implements IPowerReceptor, IAction
private static final float LASER_OFFSET = 2.0F / 16.0F; private static final float LASER_OFFSET = 2.0F / 16.0F;
private EntityEnergyLaser laser = null; private EntityEnergyLaser laser = null;
private final SafeTimeTracker laserTickTracker = new SafeTimeTracker(); private final SafeTimeTracker laserTickTracker = new SafeTimeTracker(10);
private final SafeTimeTracker searchTracker = new SafeTimeTracker(); private final SafeTimeTracker searchTracker = new SafeTimeTracker(100, 100);
private final SafeTimeTracker networkTracker = new SafeTimeTracker(); private final SafeTimeTracker networkTracker = new SafeTimeTracker(3);
private ILaserTarget laserTarget; private ILaserTarget laserTarget;
protected PowerHandler powerHandler; protected PowerHandler powerHandler;
private int nextNetworkUpdate = 3;
private int nextLaserUpdate = 10;
private int nextLaserSearch = 100;
private ActionMachineControl.Mode lastMode = ActionMachineControl.Mode.Unknown; private ActionMachineControl.Mode lastMode = ActionMachineControl.Mode.Unknown;
private static final PowerHandler.PerditionCalculator PERDITION = new PowerHandler.PerditionCalculator(0.5F); private static final PowerHandler.PerditionCalculator PERDITION = new PowerHandler.PerditionCalculator(0.5F);
@ -66,8 +63,7 @@ public class TileLaser extends TileBuildCraft implements IPowerReceptor, IAction
return; return;
} }
// Check for available tables if none is linked to this laser. // Check for any available tables at a regular basis
if (!isValidTable())
if (canFindTable()) { if (canFindTable()) {
findTable(); findTable();
} }
@ -117,11 +113,11 @@ public class TileLaser extends TileBuildCraft implements IPowerReceptor, IAction
} }
protected boolean canFindTable() { protected boolean canFindTable() {
return searchTracker.markTimeIfDelay(worldObj, nextLaserSearch); return searchTracker.markTimeIfDelay(worldObj);
} }
protected boolean canUpdateLaser() { protected boolean canUpdateLaser() {
return laserTickTracker.markTimeIfDelay(worldObj, nextLaserUpdate); return laserTickTracker.markTimeIfDelay(worldObj);
} }
protected boolean isValidTable() { protected boolean isValidTable() {
@ -235,7 +231,6 @@ public class TileLaser extends TileBuildCraft implements IPowerReceptor, IAction
} }
protected void removeLaser() { protected void removeLaser() {
if (laser != null) { if (laser != null) {
laser.setDead(); laser.setDead();
laser = null; laser = null;
@ -253,7 +248,7 @@ public class TileLaser extends TileBuildCraft implements IPowerReceptor, IAction
@Override @Override
public void sendNetworkUpdate() { public void sendNetworkUpdate() {
if (networkTracker.markTimeIfDelay(worldObj, nextNetworkUpdate)) { if (networkTracker.markTimeIfDelay(worldObj)) {
super.sendNetworkUpdate(); super.sendNetworkUpdate();
} }
} }