mirror of
https://github.com/GTNewHorizons/EnderIO.git
synced 2026-11-19 19:59:58 +00:00
More power network cleanup
This commit is contained in:
1 parent
d4f1109a52
commit
272eace6d7
2 files changed
+46
-105
No files matched your search
@@ -8,6 +8,7 @@ import java.util.ListIterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import net.minecraft.util.MathHelper;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.common.util.ForgeDirection;
|
||||
import cpw.mods.fml.common.gameevent.TickEvent.ServerTickEvent;
|
||||
@@ -43,8 +44,6 @@ public class NetworkPowerManager {
|
||||
private boolean lastActiveValue = false;
|
||||
private int ticksWithNoPower = 0;
|
||||
|
||||
private final Map<BlockCoord, StarveBuffer> starveBuffers = new HashMap<BlockCoord, NetworkPowerManager.StarveBuffer>();
|
||||
|
||||
private final Map<IPowerConduit, PowerTracker> powerTrackers = new HashMap<IPowerConduit, PowerTracker>();
|
||||
|
||||
private PowerTracker networkPowerTracker = new PowerTracker();
|
||||
@@ -133,7 +132,6 @@ public class NetworkPowerManager {
|
||||
updateNetorkStorage();
|
||||
networkPowerTracker.tickStart(energyStored);
|
||||
|
||||
checkReserves();
|
||||
updateActiveState();
|
||||
|
||||
capSupply.init();
|
||||
@@ -144,9 +142,6 @@ public class NetworkPowerManager {
|
||||
int wasAvailable = available;
|
||||
|
||||
if(available <= 0 || (receptors.isEmpty() && storageReceptors.isEmpty())) {
|
||||
if(available > 0) {
|
||||
distributeStorageToConduits();
|
||||
}
|
||||
trackerEndTick();
|
||||
networkPowerTracker.tickEnd(energyStored);
|
||||
return;
|
||||
@@ -157,37 +152,12 @@ public class NetworkPowerManager {
|
||||
if(!receptors.isEmpty() && !receptorIterator.hasNext()) {
|
||||
receptorIterator = receptors.listIterator();
|
||||
}
|
||||
|
||||
ReceptorEntry r = receptorIterator.next();
|
||||
IPowerInterface pp = r.powerInterface;
|
||||
if(pp != null) {
|
||||
|
||||
int used = 0;
|
||||
if(pp.getClass() == PowerInterfaceRF.class) {
|
||||
|
||||
int canOffer = Math.min(r.emmiter.getMaxEnergyExtracted(r.direction), available);
|
||||
used = pp.recieveEnergy(r.direction.getOpposite(), canOffer);
|
||||
trackerSend(r.emmiter, used, false);
|
||||
|
||||
} else {
|
||||
|
||||
int reservedForEntry = removeReservedEnergy(r);
|
||||
available += reservedForEntry;
|
||||
int canOffer = Math.min(r.emmiter.getMaxEnergyExtracted(r.direction), available);
|
||||
int requested = pp.getPowerRequest(r.direction.getOpposite());
|
||||
|
||||
// If it is possible to supply the minimum amount of energy
|
||||
if(pp.getMinEnergyReceived(r.direction) <= r.emmiter.getMaxEnergyExtracted(r.direction) && requested > 0) {
|
||||
// Buffer energy if we can't meet it now
|
||||
if(pp.getMinEnergyReceived(r.direction) > canOffer) {
|
||||
reserveEnergy(r, canOffer);
|
||||
used += canOffer;
|
||||
} else {
|
||||
used = pp.recieveEnergy(r.direction.getOpposite(), canOffer);
|
||||
trackerSend(r.emmiter, used, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
if(pp != null) {
|
||||
int canOffer = Math.min(r.emmiter.getMaxEnergyExtracted(r.direction), available);
|
||||
int used = pp.recieveEnergy(r.direction.getOpposite(), canOffer);
|
||||
trackerSend(r.emmiter, used, false);
|
||||
available -= used;
|
||||
if(available <= 0) {
|
||||
break;
|
||||
@@ -301,28 +271,6 @@ public class NetworkPowerManager {
|
||||
}
|
||||
}
|
||||
|
||||
private int removeReservedEnergy(ReceptorEntry r) {
|
||||
StarveBuffer starveBuf = starveBuffers.remove(r.coord);
|
||||
if(starveBuf == null) {
|
||||
return 0;
|
||||
}
|
||||
int result = starveBuf.stored;
|
||||
reserved -= result;
|
||||
return result;
|
||||
}
|
||||
|
||||
private void reserveEnergy(ReceptorEntry r, int amount) {
|
||||
starveBuffers.put(r.coord, new StarveBuffer(amount));
|
||||
reserved += amount;
|
||||
}
|
||||
|
||||
private void checkReserves() {
|
||||
if(reserved > maxEnergyStored * 0.9) {
|
||||
starveBuffers.clear();
|
||||
reserved = 0;
|
||||
}
|
||||
}
|
||||
|
||||
private void distributeStorageToConduits() {
|
||||
if(maxEnergyStored <= 0 || energyStored <= 0) {
|
||||
for (IPowerConduit con : network.getConduits()) {
|
||||
@@ -330,20 +278,18 @@ public class NetworkPowerManager {
|
||||
}
|
||||
return;
|
||||
}
|
||||
if(energyStored > maxEnergyStored) {
|
||||
energyStored = maxEnergyStored;
|
||||
}
|
||||
energyStored = MathHelper.clamp_int(energyStored, 0, maxEnergyStored);
|
||||
|
||||
float filledRatio = (float)energyStored / maxEnergyStored;
|
||||
float filledRatio = (float) energyStored / maxEnergyStored;
|
||||
int energyLeft = energyStored;
|
||||
int given = 0;
|
||||
for (IPowerConduit con : network.getConduits()) {
|
||||
if(energyLeft >= 0) {
|
||||
if(energyLeft > 0) {
|
||||
// NB: use ceil to ensure we dont through away any energy due to
|
||||
// rounding
|
||||
// errors
|
||||
int give = (int)Math.ceil(con.getCapacitor().getMaxEnergyStored() * filledRatio);
|
||||
give = Math.min(give, con.getCapacitor().getMaxEnergyStored());
|
||||
int give = (int) Math.ceil(con.getMaxEnergyStored() * filledRatio);
|
||||
give = Math.min(give, con.getMaxEnergyStored());
|
||||
give = Math.min(give, energyLeft);
|
||||
con.setEnergyStored(give);
|
||||
given += give;
|
||||
@@ -362,14 +308,11 @@ public class NetworkPowerManager {
|
||||
maxEnergyStored = 0;
|
||||
energyStored = 0;
|
||||
for (IPowerConduit con : network.getConduits()) {
|
||||
maxEnergyStored += con.getCapacitor().getMaxEnergyStored();
|
||||
maxEnergyStored += con.getMaxEnergyStored();
|
||||
con.onTick();
|
||||
energyStored += con.getEnergyStored();
|
||||
}
|
||||
|
||||
if(energyStored > maxEnergyStored) {
|
||||
energyStored = maxEnergyStored;
|
||||
}
|
||||
energyStored = MathHelper.clamp_int(energyStored, 0, maxEnergyStored);
|
||||
}
|
||||
|
||||
public void receptorsChanged() {
|
||||
@@ -449,16 +392,15 @@ public class NetworkPowerManager {
|
||||
|
||||
for (ReceptorEntry rec : storageReceptors) {
|
||||
TileCapacitorBank cb = (TileCapacitorBank) rec.powerInterface.getDelegate();
|
||||
|
||||
|
||||
boolean processed = capBanks.contains(cb.getController());
|
||||
|
||||
if(!processed) {
|
||||
stored += cb.getEnergyStored();
|
||||
maxCap += cb.getMaxEnergyStored();
|
||||
capBanks.add(cb.getController());
|
||||
capBanks.add(cb.getController());
|
||||
}
|
||||
|
||||
|
||||
if(rec.emmiter.getConectionMode(rec.direction) == ConnectionMode.IN_OUT) {
|
||||
toBalance += cb.getEnergyStored();
|
||||
maxToBalance += cb.getMaxEnergyStored();
|
||||
@@ -482,7 +424,7 @@ public class NetworkPowerManager {
|
||||
|
||||
filledRatio = 0;
|
||||
if(maxToBalance > 0) {
|
||||
filledRatio = (float)toBalance / maxToBalance;
|
||||
filledRatio = (float) toBalance / maxToBalance;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -520,7 +462,7 @@ public class NetworkPowerManager {
|
||||
CapBankSupplyEntry to = enteries.get(j);
|
||||
if(Math.signum(amount) != Math.signum(to.toBalance)) {
|
||||
int toAmount = Math.min(toTranfser, Math.abs(to.toBalance));
|
||||
to.capBank.addEnergy(toAmount * (int)Math.signum(to.toBalance));
|
||||
to.capBank.addEnergy(toAmount * (int) Math.signum(to.toBalance));
|
||||
toTranfser -= toAmount;
|
||||
}
|
||||
}
|
||||
@@ -533,10 +475,10 @@ public class NetworkPowerManager {
|
||||
if(canExtract <= 0 || amount <= 0) {
|
||||
return;
|
||||
}
|
||||
double ratio = (double)amount / canExtract;
|
||||
double ratio = (double) amount / canExtract;
|
||||
|
||||
for (CapBankSupplyEntry entry : enteries) {
|
||||
int use = (int)Math.ceil(ratio * entry.canExtract);
|
||||
int use = (int) Math.ceil(ratio * entry.canExtract);
|
||||
use = Math.min(use, amount);
|
||||
use = Math.min(use, entry.canExtract);
|
||||
entry.capBank.addEnergy(-use);
|
||||
@@ -552,7 +494,7 @@ public class NetworkPowerManager {
|
||||
if(canFill <= 0 || amount <= 0) {
|
||||
return;
|
||||
}
|
||||
double ratio = (double)amount / canFill;
|
||||
double ratio = (double) amount / canFill;
|
||||
|
||||
for (CapBankSupplyEntry entry : enteries) {
|
||||
int add = (int) Math.ceil(ratio * entry.canFill);
|
||||
@@ -587,7 +529,7 @@ public class NetworkPowerManager {
|
||||
}
|
||||
|
||||
void calcToBalance(float targetRatio) {
|
||||
int targetAmount = (int)Math.floor(capBank.getMaxEnergyStored() * targetRatio);
|
||||
int targetAmount = (int) Math.floor(capBank.getMaxEnergyStored() * targetRatio);
|
||||
toBalance = targetAmount - capBank.getEnergyStored();
|
||||
if(toBalance < 0) {
|
||||
toBalance = Math.max(toBalance, -canExtract);
|
||||
|
||||
@@ -14,6 +14,7 @@ import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.MathHelper;
|
||||
import net.minecraftforge.common.util.ForgeDirection;
|
||||
import cofh.api.energy.IEnergyContainerItem;
|
||||
import crazypants.enderio.EnderIO;
|
||||
@@ -52,8 +53,6 @@ public class TileCapacitorBank extends TileEntityEio implements IInternalPowerRe
|
||||
|
||||
private int maxOutput;
|
||||
|
||||
//private int mjBuf = 0;
|
||||
|
||||
private boolean multiblockDirty = false;
|
||||
|
||||
private RedstoneControlMode inputControlMode;
|
||||
@@ -236,11 +235,11 @@ public class TileCapacitorBank extends TileEntityEio implements IInternalPowerRe
|
||||
}
|
||||
|
||||
if(isCreative) {
|
||||
storedEnergyRF = getMaxEnergyStored() / 2;
|
||||
setEnergyStored(getMaxEnergyStored() / 2);
|
||||
}
|
||||
|
||||
if(lastSyncPowerStored != storedEnergyRF && worldObj.getTotalWorldTime() % 10 == 0) {
|
||||
lastSyncPowerStored = storedEnergyRF;
|
||||
if(lastSyncPowerStored != getEnergyStored() && worldObj.getTotalWorldTime() % 10 == 0) {
|
||||
lastSyncPowerStored = getEnergyStored();
|
||||
PacketHandler.sendToAllAround(new PacketPowerStorage(this), this, 64);
|
||||
}
|
||||
|
||||
@@ -260,7 +259,7 @@ public class TileCapacitorBank extends TileEntityEio implements IInternalPowerRe
|
||||
|
||||
public boolean chargeItems(ItemStack[] items) {
|
||||
boolean chargedItem = false;
|
||||
int available = Math.min(maxIO, storedEnergyRF);
|
||||
int available = Math.min(maxIO, getEnergyStored());
|
||||
for (ItemStack item : items) {
|
||||
if(item != null && available > 0) {
|
||||
int used = 0;
|
||||
@@ -276,7 +275,7 @@ public class TileCapacitorBank extends TileEntityEio implements IInternalPowerRe
|
||||
|
||||
}
|
||||
if(used > 0) {
|
||||
storedEnergyRF = storedEnergyRF - used;
|
||||
setEnergyStored(getEnergyStored() - used);
|
||||
chargedItem = true;
|
||||
available -= used;
|
||||
}
|
||||
@@ -312,10 +311,10 @@ public class TileCapacitorBank extends TileEntityEio implements IInternalPowerRe
|
||||
|
||||
private boolean transmitEnergy() {
|
||||
|
||||
if(storedEnergyRF <= 0) {
|
||||
if(getEnergyStored() <= 0) {
|
||||
return false;
|
||||
}
|
||||
int canTransmit = Math.min(storedEnergyRF, maxOutput);
|
||||
int canTransmit = Math.min(getEnergyStored(), maxOutput);
|
||||
int transmitted = 0;
|
||||
|
||||
if(!masterReceptors.isEmpty() && !receptorIterator.hasNext()) {
|
||||
@@ -359,7 +358,7 @@ public class TileCapacitorBank extends TileEntityEio implements IInternalPowerRe
|
||||
}
|
||||
appliedCount++;
|
||||
}
|
||||
storedEnergyRF = storedEnergyRF - transmitted;
|
||||
setEnergyStored(getEnergyStored() - transmitted);
|
||||
|
||||
return transmitted > 0;
|
||||
|
||||
@@ -521,11 +520,11 @@ public class TileCapacitorBank extends TileEntityEio implements IInternalPowerRe
|
||||
}
|
||||
|
||||
public int doGetEnergyStored(ForgeDirection from) {
|
||||
return storedEnergyRF;
|
||||
return doGetEnergyStored();
|
||||
}
|
||||
|
||||
public int doGetMaxEnergyStored(ForgeDirection from) {
|
||||
return maxStoredEnergy;
|
||||
return doGetMaxEnergyStored();
|
||||
}
|
||||
|
||||
// end rf power
|
||||
@@ -601,26 +600,25 @@ public class TileCapacitorBank extends TileEntityEio implements IInternalPowerRe
|
||||
return maxStoredEnergy;
|
||||
}
|
||||
|
||||
int doGetEnergyStoredScaled(int scale) {
|
||||
// NB: called on the client so can't use the power provider
|
||||
return (int)VecmathUtil.clamp(Math.round(scale * (storedEnergyRF / maxStoredEnergy)), 0, scale);
|
||||
int doGetEnergyStoredScaled(int scale) {
|
||||
return (int)VecmathUtil.clamp(Math.round(scale * doGetEnergyStoredRatio()), 0, scale);
|
||||
}
|
||||
|
||||
int doGetEnergyStored() {
|
||||
return storedEnergyRF;
|
||||
}
|
||||
|
||||
void doSetEnergyStored(int stored) {
|
||||
storedEnergyRF = stored;
|
||||
void doSetEnergyStored(int stored) {
|
||||
storedEnergyRF = MathHelper.clamp_int(stored, 0, getMaxEnergyStored());
|
||||
}
|
||||
|
||||
|
||||
double doGetEnergyStoredRatio() {
|
||||
return (double)storedEnergyRF / maxStoredEnergy;
|
||||
return (double)doGetEnergyStored() / maxStoredEnergy;
|
||||
}
|
||||
|
||||
void doAddEnergy(int add) {
|
||||
storedEnergyRF = Math.max(0, Math.min(maxStoredEnergy, storedEnergyRF + add));
|
||||
doSetEnergyStored(doGetEnergyStored() + add);
|
||||
}
|
||||
|
||||
void doSetMaxInput(int in) {
|
||||
@@ -754,11 +752,11 @@ public class TileCapacitorBank extends TileEntityEio implements IInternalPowerRe
|
||||
cb.maxIO = BASE_CAP.getMaxEnergyExtracted();
|
||||
cb.maxInput = Math.min(cb.maxInput, cb.maxIO);
|
||||
cb.maxOutput = Math.min(cb.maxOutput, cb.maxIO);
|
||||
cb.storedEnergyRF = powerPerBlock;
|
||||
cb.doSetEnergyStored(powerPerBlock);
|
||||
cb.multiblockDirty = true;
|
||||
}
|
||||
}
|
||||
storedEnergyRF += remaining;
|
||||
doAddEnergy(remaining);
|
||||
|
||||
}
|
||||
multiblock = mb;
|
||||
@@ -772,7 +770,7 @@ public class TileCapacitorBank extends TileEntityEio implements IInternalPowerRe
|
||||
for (BlockCoord bc : multiblock) {
|
||||
TileCapacitorBank cb = getCapBank(bc);
|
||||
if(cb != null) {
|
||||
totalStored += cb.storedEnergyRF;
|
||||
totalStored += cb.doGetEnergyStored();
|
||||
}
|
||||
ItemStack[] inv = cb.inventory;
|
||||
for (int i = 0; i < inv.length; i++) {
|
||||
@@ -783,7 +781,7 @@ public class TileCapacitorBank extends TileEntityEio implements IInternalPowerRe
|
||||
}
|
||||
cb.multiblockDirty = false;
|
||||
}
|
||||
storedEnergyRF = totalStored;
|
||||
doSetEnergyStored(totalStored);
|
||||
maxStoredEnergy = totalCap;
|
||||
maxIO = totalIO;
|
||||
maxInput = maxInput < 0 ? maxIO : Math.min(maxInput, maxIO);
|
||||
@@ -964,10 +962,11 @@ public class TileCapacitorBank extends TileEntityEio implements IInternalPowerRe
|
||||
|
||||
double oldEnergy = storedEnergyRF;
|
||||
if(nbtRoot.hasKey("storedEnergyD")) {
|
||||
storedEnergyRF = (int)nbtRoot.getDouble("storedEnergyD") * 10;
|
||||
} else {
|
||||
storedEnergyRF = nbtRoot.getInteger("storedEnergyRF");
|
||||
}
|
||||
nbtRoot.setInteger("storedEnergyRF", (int)(nbtRoot.getDouble("storedEnergyD") * 10));
|
||||
}
|
||||
|
||||
doSetEnergyStored(nbtRoot.getInteger("storedEnergyRF"));
|
||||
|
||||
maxStoredEnergy = nbtRoot.getInteger("maxStoredEnergy");
|
||||
|
||||
double newEnergy = storedEnergyRF;
|
||||
|
||||
Reference in new issue
Block a user