From 720b0cb86a2fc25fd8df8427bff2fc254e439eae Mon Sep 17 00:00:00 2001 From: brad-c Date: Tue, 3 Sep 2013 17:07:16 +0800 Subject: [PATCH] Changed algorithm for energy distribution with capacitor banks. updated version to 17. --- build.xml | 2 +- common/crazypants/enderio/EnderIO.java | 2 +- .../conduit/power/NetworkPowerManager.java | 269 ++++++++++++++---- .../machine/AbstractMachineEntity.java | 4 +- .../machine/power/TileCapacitorBank.java | 16 +- .../enderio/textures/blocks/capacitorBank.png | Bin 683 -> 467 bytes resources/mcmod.info | 2 +- 7 files changed, 238 insertions(+), 57 deletions(-) diff --git a/build.xml b/build.xml index 8c533342..e16787d4 100644 --- a/build.xml +++ b/build.xml @@ -19,7 +19,7 @@ - + diff --git a/common/crazypants/enderio/EnderIO.java b/common/crazypants/enderio/EnderIO.java index 5d4fa7ce..a086d9b4 100644 --- a/common/crazypants/enderio/EnderIO.java +++ b/common/crazypants/enderio/EnderIO.java @@ -50,7 +50,7 @@ import crazypants.enderio.material.ItemIndustrialBinder; import crazypants.enderio.material.ItemYetaWrench; import crazypants.enderio.material.MaterialRecipes; -@Mod(name = "EnderIO", modid = "EnderIO", version = "0.1.15", dependencies = "required-after:Forge@[9.10.0.800,)") +@Mod(name = "EnderIO", modid = "EnderIO", version = "0.1.17", dependencies = "required-after:Forge@[9.10.0.800,)") @NetworkMod(clientSideRequired = true, serverSideRequired = true, channels = { "EnderIO" }, packetHandler = PacketHandler.class) public class EnderIO { diff --git a/common/crazypants/enderio/conduit/power/NetworkPowerManager.java b/common/crazypants/enderio/conduit/power/NetworkPowerManager.java index 048859d2..b079108e 100644 --- a/common/crazypants/enderio/conduit/power/NetworkPowerManager.java +++ b/common/crazypants/enderio/conduit/power/NetworkPowerManager.java @@ -9,10 +9,12 @@ import java.util.Set; import net.minecraft.world.World; import net.minecraftforge.common.ForgeDirection; +import buildcraft.api.power.IPowerReceptor; import buildcraft.api.power.PowerHandler; import buildcraft.api.power.PowerHandler.PowerReceiver; import buildcraft.api.power.PowerHandler.Type; import crazypants.enderio.conduit.power.PowerConduitNetwork.ReceptorEntry; +import crazypants.enderio.machine.power.TileCapacitorBank; import crazypants.enderio.power.IInternalPowerReceptor; import crazypants.enderio.power.PowerHandlerUtil; import crazypants.util.BlockCoord; @@ -45,37 +47,37 @@ public class NetworkPowerManager { public void applyRecievedPower() { - //Update our energy stored based on what's in our conduits - updateNetorkStorage(); - checkReserves(); + // Update our energy stored based on what's in our conduits + updateNetorkStorage(); + checkReserves(); updateActiveState(); - if (energyStored <= 0 || receptors.isEmpty()) { - return; - } + CapBankSupply capSupply = new CapBankSupply(); int appliedCount = 0; int numReceptors = receptors.size(); - storageReceptors.clear(); - while (energyStored > 0 && appliedCount < numReceptors) { + float available = energyStored + capSupply.canExtract - reserved; + float wasAvailable = available; + + if (available <= 0 || (receptors.isEmpty() && storageReceptors.isEmpty())) { + return; + } + + while (available > 0 && appliedCount < numReceptors) { if (!receptors.isEmpty() && !receptorIterator.hasNext()) { receptorIterator = receptors.listIterator(); } ReceptorEntry r = receptorIterator.next(); - float reservedForEntry = removeReservedEnergy(r); PowerReceiver pp = r.powerReceptor.getPowerReceiver(r.direction); if (pp != null) { - if (pp.getType() == Type.STORAGE) { - // only apply energy to these guys if there is any left over - storageReceptors.add(r); - } else if (pp.getType() != Type.ENGINE) { + if (pp.getType() != Type.ENGINE) { float used = 0; - float nonReservedPower = energyStored - reserved; - float available = nonReservedPower + reservedForEntry; - float canOffer = Math.min(r.emmiter.getCapacitor().getMaxEnergyExtracted(), available); + float reservedForEntry = removeReservedEnergy(r); + float canOffer = available + reservedForEntry; + canOffer = Math.min(r.emmiter.getCapacitor().getMaxEnergyExtracted(), canOffer); float requested = pp.powerRequest(); // If it is possible to supply the minimum amount of energy @@ -87,15 +89,15 @@ public class NetworkPowerManager { } else if (r.powerReceptor instanceof IInternalPowerReceptor) { used = PowerHandlerUtil.transmitInternal((IInternalPowerReceptor) r.powerReceptor, pp, canOffer, Type.PIPE, r.direction); } else { - float offer = Math.min(requested, canOffer); - used = pp.receiveEnergy(Type.PIPE, offer, r.direction); + float offer = Math.min(requested, canOffer); + used = pp.receiveEnergy(Type.PIPE, offer, r.direction); } } - energyStored -= used; + available -= used; - } - if (energyStored <= 0) { + } + if (available <= 0) { break; } @@ -103,27 +105,29 @@ public class NetworkPowerManager { appliedCount++; } - //send any energy left over to the storage once we are more than 90% full internallly - if (energyStored > 0 && !storageReceptors.isEmpty() && (energyStored / maxEnergyStored > 0.9)) { - for (ReceptorEntry r : storageReceptors) { - PowerReceiver pp = r.powerReceptor.getPowerReceiver(r.direction); - if (pp != null) { - float used = 0; - float available = energyStored - reserved; - float canOffer = Math.min(r.emmiter.getCapacitor().getMaxEnergyExtracted(), available); - float requested = pp.powerRequest(); - if (r.powerReceptor instanceof IInternalPowerReceptor) { - used = PowerHandlerUtil.transmitInternal((IInternalPowerReceptor) r.powerReceptor, pp, canOffer, Type.PIPE, r.direction); - } else { - used = pp.receiveEnergy(Type.PIPE, Math.min(requested, canOffer), r.direction); - } - energyStored -= used; - } - if (energyStored <= 0) { - break; - } - } + float used = wasAvailable - available; + // use all the capacator storage first + energyStored -= used; + + float capBankChange = 0; + if (energyStored < 0) { + // not enough so get the rest from the capacitor bank + capBankChange = energyStored; + energyStored = 0; + } else if (energyStored > 0) { + // push as much as we can back to the cap banks + capBankChange = Math.min(energyStored, capSupply.canFill); + energyStored -= capBankChange; } + + if (capBankChange < 0) { + capSupply.remove(Math.abs(capBankChange)); + } else if (capBankChange > 0) { + capSupply.add(capBankChange); + } + + capSupply.balance(); + distributeStorageToConduits(); } @@ -169,31 +173,31 @@ public class NetworkPowerManager { } private void distributeStorageToConduits() { - if(maxEnergyStored <= 0 || energyStored <= 0) { + if (maxEnergyStored <= 0 || energyStored <= 0) { for (IPowerConduit con : network.getConduits()) { con.getPowerHandler().setEnergy(0); } return; } - if(energyStored > maxEnergyStored) { + if (energyStored > maxEnergyStored) { energyStored = maxEnergyStored; } - + float filledRatio = energyStored / maxEnergyStored; float energyLeft = energyStored; float given = 0; for (IPowerConduit con : network.getConduits()) { //NB: use ceil to ensure we dont through away any energy due to rounding errors - float give = (float)Math.ceil(con.getCapacitor().getMaxEnergyStored() * filledRatio); - give = Math.min(give, con.getCapacitor().getMaxEnergyStored()); - give = Math.min(give, energyLeft); + float give = (float) Math.ceil(con.getCapacitor().getMaxEnergyStored() * filledRatio); + give = Math.min(give, con.getCapacitor().getMaxEnergyStored()); + give = Math.min(give, energyLeft); con.getPowerHandler().setEnergy(give); given += give; energyLeft -= give; - if(energyLeft <= 0) { + if (energyLeft <= 0) { return; } - } + } } boolean isActive() { @@ -216,7 +220,14 @@ public class NetworkPowerManager { public void receptorsChanged() { receptors.clear(); - receptors.addAll(network.getPowerReceptors()); + storageReceptors.clear(); + for (ReceptorEntry rec : network.getPowerReceptors()) { + if (rec.powerReceptor instanceof TileCapacitorBank) { + storageReceptors.add(rec); + } else { + receptors.add(rec); + } + } receptorIterator = receptors.listIterator(); } @@ -237,4 +248,162 @@ public class NetworkPowerManager { } + private float minAbs(float amount, float limit) { + if (amount < 0) { + return Math.max(amount, -limit); + } else { + return Math.min(amount, limit); + } + } + + private class CapBankSupply { + + float canExtract; + float canFill; + float filledRatio; + float stored = 0; + float maxCap = 0; + + List enteries; + + CapBankSupply() { + init(); + } + + void init() { + canExtract = 0; + canFill = 0; + stored = 0; + maxCap = 0; + enteries = new ArrayList(); + for (ReceptorEntry rec : storageReceptors) { + TileCapacitorBank cb = (TileCapacitorBank) rec.powerReceptor; + + stored += cb.getEnergyStored(); + maxCap += cb.getMaxEnergyStored(); + + float canGet = 0; + if (cb.isOutputEnabled()) { + canGet = Math.min(cb.getEnergyStored(), cb.getMaxIO()); + canGet = Math.min(canGet, rec.emmiter.getCapacitor().getMaxEnergyExtracted()); + canExtract += canGet; + } + float canFill = 0; + if(cb.isInputEnabled()) { + canFill = Math.min(cb.getMaxEnergyStored() - cb.getEnergyStored(), cb.getMaxIO()); + this.canFill += canFill; + } + enteries.add(new CapBankSupplyEntry(cb, canGet, canFill)); + + } + + filledRatio = 0; + if (maxCap > 0) { + filledRatio = stored / maxCap; + } + } + + void balance() { + if (enteries.size() < 2) { + return; + } + init(); + int canRemove = 0; + int canAdd = 0; + for (CapBankSupplyEntry entry : enteries) { + entry.calcToBalance(filledRatio); + if (entry.toBalance < 0) { + canRemove += -entry.toBalance; + } else { + canAdd += entry.toBalance; + } + } + + float toalTransferAmount = Math.min(canAdd, canRemove); + + for (int i = 0; i < enteries.size() && toalTransferAmount > 0; i++) { + CapBankSupplyEntry from = enteries.get(i); + float amount = from.toBalance; + amount = minAbs(amount, toalTransferAmount); + from.capBank.addEnergy(amount); + toalTransferAmount -= Math.abs(amount); + float toTranfser = Math.abs(amount); + + for (int j = i + 1; j < enteries.size() && toTranfser > 0; j++) { + CapBankSupplyEntry to = enteries.get(j); + if (Math.signum(amount) != Math.signum(to.toBalance)) { + float toAmount = Math.min(toTranfser, Math.abs(to.toBalance)); + to.capBank.addEnergy(toAmount * Math.signum(to.toBalance)); + toTranfser -= toAmount; + } + } + + } + + } + + void remove(float amount) { + if (canExtract <= 0 || amount <= 0) { + return; + } + float ratio = amount / canExtract; + + for (CapBankSupplyEntry entry : enteries) { + double use = Math.ceil(ratio * entry.canExtract); + use = Math.min(use, amount); + use = Math.min(use, entry.canExtract); + entry.capBank.addEnergy(-(float) use); + amount -= use; + if (amount == 0) { + return; + } + } + } + + void add(float amount) { + if (canFill <= 0 || amount <= 0) { + return; + } + float ratio = amount / canFill; + + for (CapBankSupplyEntry entry : enteries) { + double add = (int) Math.ceil(ratio * entry.canFill); + add = Math.min(add, entry.canFill); + add = Math.min(add, amount); + entry.capBank.addEnergy((float) add); + amount -= add; + if (amount == 0) { + return; + } + } + } + + } + + private static class CapBankSupplyEntry { + + final TileCapacitorBank capBank; + final float canExtract; + final float canFill; + float toBalance; + + private CapBankSupplyEntry(TileCapacitorBank capBank, float available, float canFill) { + this.capBank = capBank; + this.canExtract = available; + this.canFill = canFill; + } + + void calcToBalance(float targetRatio) { + float targetAmount = capBank.getMaxEnergyStored() * targetRatio; + toBalance = targetAmount - capBank.getEnergyStored(); + if (toBalance < 0) { + toBalance = Math.max(toBalance, -canExtract); + } else { + toBalance = Math.min(toBalance, canFill); + } + + } + + } + } diff --git a/common/crazypants/enderio/machine/AbstractMachineEntity.java b/common/crazypants/enderio/machine/AbstractMachineEntity.java index d1d7d810..cdf3f4a4 100644 --- a/common/crazypants/enderio/machine/AbstractMachineEntity.java +++ b/common/crazypants/enderio/machine/AbstractMachineEntity.java @@ -200,9 +200,7 @@ public abstract class AbstractMachineEntity extends TileEntity implements IInven requiresClientSync |= processTasks(redstoneCheckPassed); - // Update if our power has changed by more than 1% - requiresClientSync |= Math.abs(lastSyncPowerStored - powerHandler.getEnergyStored()) > powerHandler.getMaxEnergyStored() / 100; - requiresClientSync |= lastSyncPowerStored != powerHandler.getMaxEnergyStored() && powerHandler.getEnergyStored() == powerHandler.getMaxEnergyStored(); + requiresClientSync |= lastSyncPowerStored != powerHandler.getEnergyStored() && worldObj.getTotalWorldTime() % 16 == 0; if (requiresClientSync) { lastSyncPowerStored = powerHandler.getEnergyStored(); diff --git a/common/crazypants/enderio/machine/power/TileCapacitorBank.java b/common/crazypants/enderio/machine/power/TileCapacitorBank.java index 8c4ee4ad..0bbcc7e3 100644 --- a/common/crazypants/enderio/machine/power/TileCapacitorBank.java +++ b/common/crazypants/enderio/machine/power/TileCapacitorBank.java @@ -15,6 +15,8 @@ import buildcraft.api.power.PowerHandler.PowerReceiver; import buildcraft.api.power.PowerHandler.Type; import crazypants.enderio.ModObject; import crazypants.enderio.PacketHandler; +import crazypants.enderio.conduit.IConduitBundle; +import crazypants.enderio.conduit.TileConduitBundle; import crazypants.enderio.machine.RedstoneControlMode; import crazypants.enderio.power.BasicCapacitor; import crazypants.enderio.power.IInternalPowerReceptor; @@ -129,6 +131,14 @@ public class TileCapacitorBank extends TileEntity implements IInternalPowerRecep } + public boolean isOutputEnabled() { + return getController().outputEnabled; + } + + public boolean isInputEnabled() { + return getController().inputEnabled; + } + private boolean transmitEnergy() { if (powerHandler.getEnergyStored() <= 0) { @@ -154,7 +164,11 @@ public class TileCapacitorBank extends TileEntity implements IInternalPowerRecep if (receptor.receptor instanceof IInternalPowerReceptor) { // System.out.println("TileEntityStirlingGenerator.transmitEnergy: Sending " // + canTransmit + " to internal."); - used = PowerHandlerUtil.transmitInternal((IInternalPowerReceptor) receptor.receptor, pp, canTransmit, Type.STORAGE, receptor.fromDir); + if(! (receptor.receptor instanceof IConduitBundle) ) { //power conduits manage the exchange between them an the cap bank + used = PowerHandlerUtil.transmitInternal((IInternalPowerReceptor) receptor.receptor, pp, canTransmit, Type.STORAGE, receptor.fromDir); + } else { + used = 0; + } } else { // System.out.println("TileEntityStirlingGenerator.transmitEnergy: Sending " // + canTransmit + " to EXTERNAL. Receptor is: " + receptor.receptor); diff --git a/resources/assets/enderio/textures/blocks/capacitorBank.png b/resources/assets/enderio/textures/blocks/capacitorBank.png index 956e911aec20c42472aa44fc43c5f36209b3a844..4d505554f9cfe8bb1770f6055286edbe9c4471ce 100644 GIT binary patch delta 392 zcmV;30eAkZ1=9nNBoYa5NLh0L01FZT01FZU(%pXikyA8(FfjCTp#T5@Oi4sRR5(vP zQVEWOFbw1|Tbcm>Xk)Tk;?Vy8DLXR`NEUL4=ZNhomE(B@^2L1JKZ~6>E>|+Y{}c^5 z0m%*&t@T{1Z|14E@Sdy>%)6lmK|n4oy}-?)nPd%Lu1A5mJiGnvok6E47kzfV*3 zOWtMS7JLpG5NPt1{uyg~2 z!37O4=TQZ+AO*BVcZeWU!U~F9n&ARLfD}61<2u|&@M>3Q_l~}AdQ2#gi9HQV!x$W5 zCOqmTeXU=>CU~V{8SRg@{EQ;UD^Wy7JURv^G8w&Vh*`?LP`gKq;Ef~4tyUv~1b#PX ztKa;q9nlI&fm|G11m|6RwNFfxrFJ|t_^?e8%z?PVAv}yLNn@9YR)~HeP6}h+xqL%} mFm`%aSC0Q7ha0tZP%K~y+TC6bGdqc99b^9y}Jc(;}2(QXR_0!bhV zX`wUS|No)AVI>P8@U@SxO~SehSx*~5TN$-7ZWV$uaVVRhY!5fKyOxOktv=k_pl(Uj zh}_O6`#!thvwNNFlIuAgrbM|!PU#x4u<2m4aSB;SVy+6ke;n;kF}-rTWO_;UBOP2e zx>O;iOt}otVb1iEwu&U(cy@nJ?p?T7reE2JACGK=W=fUK2BbRwMLz)&aR_>IdcIHY z_h?^*T~IZVE>SjB27*FA3p3~Xk(>EA{lwki1>o`7AOXhUF_4N5pvp&NL$fm5CFwPR zGlib>@d40Sf0|h6L~4wFAIXFj;Yosr$wjbd@E3s@gweXAhuHO5H%Fb2v* z7<7i8`|pZuJ}+1{1b*LKq{3v%Qj8&O@;`b*TS1u$e`~(^xOf4waT?%>z}AmGNkFg` z<_f=jMMTwYKEBEJ>k6cZOHYCfsl8DUh$-qZSRYnhP&Q%7!?N9eUdjGi#vL+DDjm)y zR}Vl#1Jfv%LEQ&652_}tTU_Msawj-L)B|@s`YCY>*nro-=#>X~l<{E2-d`(%MypGL w)&e%;D0mSRRqqWPNB`aBw)KGvuY!{O1AKvtDLPxSGynhq07*qoM6N<$f@_%(z5oCK diff --git a/resources/mcmod.info b/resources/mcmod.info index aa063b37..79f8723f 100644 --- a/resources/mcmod.info +++ b/resources/mcmod.info @@ -3,7 +3,7 @@ "modid": "EnderIO", "name": "Ender IO", "description": "Compact conduits, painted things and access to your stuff through an eye of ender.", - "version": "0.1.15", + "version": "0.1.17", "mcversion": "1.6.2", "url": "http://www.minecraftforum.net/topic/1937619-162152-ender-io-how-many-pipes-in-one-block-updated-198/", "updateUrl": "",