Revert "An attempt to improve PowerHandler cpu usage"

This reverts commit e85d99b047.
This commit is contained in:
CovertJaguar 2013-08-06 22:48:07 -07:00
parent e85d99b047
commit 9472dea734
3 changed files with 14 additions and 50 deletions

View file

@ -1,32 +0,0 @@
/*
* Copyright (c) SpaceToad, 2011-2012
* 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.core;
/**
*
* @author CovertJaguar <http://www.railcraft.info/>
*/
public class TickLimiter {
public static long worldTime;
private long lastTick = worldTime;
private long timeSinceLastTick = -1;
public boolean canTick() {
if (lastTick == worldTime)
return false;
timeSinceLastTick = worldTime - lastTick;
lastTick = worldTime;
return true;
}
public long timeSinceLastTick() {
return timeSinceLastTick > 0 ? timeSinceLastTick : 0;
}
}

View file

@ -7,7 +7,7 @@
*/
package buildcraft.api.power;
import buildcraft.api.core.TickLimiter;
import buildcraft.api.core.SafeTimeTracker;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.ForgeDirection;
@ -77,9 +77,9 @@ public final class PowerHandler {
private float maxEnergyStored;
private float activationEnergy;
private float energyStored = 0;
private final TickLimiter doWorkLimiter = new TickLimiter();
private final TickLimiter sourcesLimiter = new TickLimiter();
private final TickLimiter perditionLimiter = new TickLimiter();
private final SafeTimeTracker doWorkTracker = new SafeTimeTracker();
private final SafeTimeTracker sourcesTracker = new SafeTimeTracker();
private final SafeTimeTracker perditionTracker = new SafeTimeTracker();
public final int[] powerSources = new int[6];
public final IPowerReceptor receptor;
private PerditionCalculator perdition;
@ -180,33 +180,32 @@ public final class PowerHandler {
* design around this though if you are aware of the limitations.
*/
public void update() {
if (receptor.getWorld().isRemote)
return;
applyPerdition();
applyWork();
validateEnergy();
}
private void applyPerdition() {
if (energyStored > 0 && perditionLimiter.canTick()) {
float newEnergy = perdition.applyPerdition(this, energyStored, perditionLimiter.timeSinceLastTick());
if (newEnergy < energyStored || newEnergy == 0)
if (perditionTracker.markTimeIfDelay(1) && energyStored > 0) {
float newEnergy = perdition.applyPerdition(this, energyStored, perditionTracker.durationOfLastDelay());
if (newEnergy == 0 || newEnergy < energyStored)
energyStored = newEnergy;
else
energyStored = DEFUALT_PERDITION.applyPerdition(this, energyStored, perditionLimiter.timeSinceLastTick());
energyStored = DEFUALT_PERDITION.applyPerdition(this, energyStored, perditionTracker.durationOfLastDelay());
validateEnergy();
}
}
private void applyWork() {
if (energyStored >= activationEnergy && doWorkLimiter.canTick())
if (energyStored >= activationEnergy)
if (doWorkTracker.markTimeIfDelay(1))
receptor.doWork(this);
}
private void updateSources(ForgeDirection source) {
if (sourcesLimiter.canTick())
if (sourcesTracker.markTimeIfDelay(1))
for (int i = 0; i < 6; ++i) {
powerSources[i] -= sourcesLimiter.timeSinceLastTick();
powerSources[i] -= sourcesTracker.durationOfLastDelay();
if (powerSources[i] < 0)
powerSources[i] = 0;
}

View file

@ -1,7 +1,6 @@
package buildcraft.core;
import buildcraft.api.core.SafeTimeTracker;
import buildcraft.api.core.TickLimiter;
import cpw.mods.fml.common.ITickHandler;
import cpw.mods.fml.common.TickType;
import java.util.EnumSet;
@ -11,9 +10,7 @@ public class TickHandlerTimeTracker implements ITickHandler {
@Override
public void tickStart(EnumSet<TickType> type, Object... tickData) {
long worldTime = ((World) tickData[0]).getWorldTime();
SafeTimeTracker.worldTime = worldTime;
TickLimiter.worldTime = worldTime;
SafeTimeTracker.worldTime = ((World) tickData[0]).getWorldTime();
}
@Override