An attempt to improve PowerHandler cpu usage

This commit is contained in:
CovertJaguar 2013-08-06 21:50:42 -07:00
parent fc3e3d19cb
commit e85d99b047
3 changed files with 50 additions and 14 deletions

View file

@ -0,0 +1,32 @@
/*
* 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.SafeTimeTracker;
import buildcraft.api.core.TickLimiter;
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 SafeTimeTracker doWorkTracker = new SafeTimeTracker();
private final SafeTimeTracker sourcesTracker = new SafeTimeTracker();
private final SafeTimeTracker perditionTracker = new SafeTimeTracker();
private final TickLimiter doWorkLimiter = new TickLimiter();
private final TickLimiter sourcesLimiter = new TickLimiter();
private final TickLimiter perditionLimiter = new TickLimiter();
public final int[] powerSources = new int[6];
public final IPowerReceptor receptor;
private PerditionCalculator perdition;
@ -180,32 +180,33 @@ 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 (perditionTracker.markTimeIfDelay(1) && energyStored > 0) {
float newEnergy = perdition.applyPerdition(this, energyStored, perditionTracker.durationOfLastDelay());
if (newEnergy == 0 || newEnergy < energyStored)
if (energyStored > 0 && perditionLimiter.canTick()) {
float newEnergy = perdition.applyPerdition(this, energyStored, perditionLimiter.timeSinceLastTick());
if (newEnergy < energyStored || newEnergy == 0)
energyStored = newEnergy;
else
energyStored = DEFUALT_PERDITION.applyPerdition(this, energyStored, perditionTracker.durationOfLastDelay());
energyStored = DEFUALT_PERDITION.applyPerdition(this, energyStored, perditionLimiter.timeSinceLastTick());
validateEnergy();
}
}
private void applyWork() {
if (energyStored >= activationEnergy)
if (doWorkTracker.markTimeIfDelay(1))
receptor.doWork(this);
if (energyStored >= activationEnergy && doWorkLimiter.canTick())
receptor.doWork(this);
}
private void updateSources(ForgeDirection source) {
if (sourcesTracker.markTimeIfDelay(1))
if (sourcesLimiter.canTick())
for (int i = 0; i < 6; ++i) {
powerSources[i] -= sourcesTracker.durationOfLastDelay();
powerSources[i] -= sourcesLimiter.timeSinceLastTick();
if (powerSources[i] < 0)
powerSources[i] = 0;
}

View file

@ -1,6 +1,7 @@
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;
@ -10,7 +11,9 @@ public class TickHandlerTimeTracker implements ITickHandler {
@Override
public void tickStart(EnumSet<TickType> type, Object... tickData) {
SafeTimeTracker.worldTime = ((World) tickData[0]).getWorldTime();
long worldTime = ((World) tickData[0]).getWorldTime();
SafeTimeTracker.worldTime = worldTime;
TickLimiter.worldTime = worldTime;
}
@Override