From ebbb6397e634c5e6bb5e50cb3ecc1d04948b989a Mon Sep 17 00:00:00 2001 From: brad-c Date: Sat, 31 Aug 2013 22:53:54 +0800 Subject: [PATCH] Wootly woota. Conduits and storage finally behaving themselves. Updated version to next release. --- build.xml | 2 +- common/crazypants/enderio/CommonProxy.java | 16 ++++ .../crazypants/enderio/DebugTickHandler.java | 53 ++++++++++++ common/crazypants/enderio/EnderIO.java | 2 +- .../enderio/conduit/liquid/LiquidConduit.java | 2 +- .../conduit/power/ItemPowerConduit.java | 3 +- .../conduit/power/NetworkPowerManager.java | 58 +++++++++---- .../enderio/conduit/power/PowerConduit.java | 8 +- .../TileEntityStirlingGenerator.java | 13 +++ .../machine/power/TileCapacitorBank.java | 56 ++++++------ .../machine/solar/TileEntitySolarPanel.java | 2 +- common/crazypants/util/DebugGuiTPS.java | 85 +++++++++++++++++++ resources/mcmod.info | 2 +- 13 files changed, 249 insertions(+), 53 deletions(-) create mode 100644 common/crazypants/enderio/DebugTickHandler.java create mode 100644 common/crazypants/util/DebugGuiTPS.java diff --git a/build.xml b/build.xml index f7c442e4..b7b3fbb5 100644 --- a/build.xml +++ b/build.xml @@ -19,7 +19,7 @@ - + diff --git a/common/crazypants/enderio/CommonProxy.java b/common/crazypants/enderio/CommonProxy.java index 7c87ca71..7c7eb95b 100644 --- a/common/crazypants/enderio/CommonProxy.java +++ b/common/crazypants/enderio/CommonProxy.java @@ -1,16 +1,27 @@ package crazypants.enderio; +import java.text.DecimalFormat; +import java.util.Timer; +import java.util.TimerTask; + import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.server.MinecraftServer; import net.minecraft.world.World; +import net.minecraftforge.common.DimensionManager; import cpw.mods.fml.common.registry.TickRegistry; import cpw.mods.fml.relauncher.Side; import crazypants.enderio.conduit.IConduit; import crazypants.enderio.conduit.render.ConduitRenderer; +import crazypants.util.DebugGuiTPS; public class CommonProxy { private final ServerTickHandler serverTickHandler = new ServerTickHandler(); + private static final DecimalFormat FORMAT = new DecimalFormat("########0.000"); + + private boolean showTpdGUI = false; + public World getClientWorld() { return null; } @@ -21,6 +32,11 @@ public class CommonProxy { public void load() { TickRegistry.registerTickHandler(serverTickHandler, Side.SERVER); + + if (showTpdGUI) { + DebugGuiTPS.showTpsGUI(); + } + } public ConduitRenderer getRendererForConduit(IConduit conduit) { diff --git a/common/crazypants/enderio/DebugTickHandler.java b/common/crazypants/enderio/DebugTickHandler.java new file mode 100644 index 00000000..824d40c1 --- /dev/null +++ b/common/crazypants/enderio/DebugTickHandler.java @@ -0,0 +1,53 @@ +package crazypants.enderio; + +import java.util.EnumSet; +import java.util.LinkedList; + +import cpw.mods.fml.common.ITickHandler; +import cpw.mods.fml.common.TickType; + +public class DebugTickHandler implements ITickHandler { + + long tickStart = -1; + + LinkedList serverTicks; + + @Override + public void tickStart(EnumSet type, Object... tickData) { + tickStart = System.nanoTime(); + if(type.contains(TickType.SERVER)) { + + } + + } + + @Override + public void tickEnd(EnumSet type, Object... tickData) { + long tickDuration = System.nanoTime() - tickStart; + } + + @Override + public EnumSet ticks() { + return EnumSet.of(TickType.SERVER, TickType.WORLD); + } + + @Override + public String getLabel() { + // TODO Auto-generated method stub + return null; + } + + private static class Entry { + + long atTimeMilli; + long tickDurationNano; + + Entry(long atTimeMilli, long tickDurationNano) { + this.atTimeMilli = atTimeMilli; + this.tickDurationNano = tickDurationNano; + } + + + } + +} diff --git a/common/crazypants/enderio/EnderIO.java b/common/crazypants/enderio/EnderIO.java index 5a2dd69c..fa6d4dfe 100644 --- a/common/crazypants/enderio/EnderIO.java +++ b/common/crazypants/enderio/EnderIO.java @@ -49,7 +49,7 @@ import crazypants.enderio.material.ItemIndustrialBinder; import crazypants.enderio.material.ItemYetaWrench; import crazypants.enderio.material.MaterialRecipes; -@Mod(name = "EnderIO", modid = "EnderIO", version = "0.1.13", dependencies = "required-after:Forge@[9.10.0.800,)") +@Mod(name = "EnderIO", modid = "EnderIO", version = "0.1.14", 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/liquid/LiquidConduit.java b/common/crazypants/enderio/conduit/liquid/LiquidConduit.java index ae2505ef..3a4a9351 100644 --- a/common/crazypants/enderio/conduit/liquid/LiquidConduit.java +++ b/common/crazypants/enderio/conduit/liquid/LiquidConduit.java @@ -336,7 +336,7 @@ public class LiquidConduit extends AbstractConduit implements ILiquidConduit { } private int pushLiquid(ForgeDirection from, int amount, boolean doPush, int token) { - if (token == currentPushToken || amount <= 0 || tank.getFluid() == null) { + if (token == currentPushToken || amount <= 0 || tank.getFluid() == null || tank.getFluid().getFluid() == null) { return 0; } currentPushToken = token; diff --git a/common/crazypants/enderio/conduit/power/ItemPowerConduit.java b/common/crazypants/enderio/conduit/power/ItemPowerConduit.java index 04660e66..12e90ef6 100644 --- a/common/crazypants/enderio/conduit/power/ItemPowerConduit.java +++ b/common/crazypants/enderio/conduit/power/ItemPowerConduit.java @@ -46,8 +46,7 @@ public class ItemPowerConduit extends AbstractItemConduit { public void addInformation(ItemStack itemStack, EntityPlayer par2EntityPlayer, List list, boolean par4) { super.addInformation(itemStack, par2EntityPlayer, list, par4); ICapacitor cap = PowerConduit.CAPACITORS[itemStack.getItemDamage()]; - list.add(String.format("I/O %d MJ/t", cap.getMaxEnergyExtracted())); - list.add(String.format("Storage %d MJ", cap.getMaxEnergyStored())); + list.add(String.format("Max Ouput %d MJ/t", cap.getMaxEnergyExtracted())); } } diff --git a/common/crazypants/enderio/conduit/power/NetworkPowerManager.java b/common/crazypants/enderio/conduit/power/NetworkPowerManager.java index ee52d81b..6f10e200 100644 --- a/common/crazypants/enderio/conduit/power/NetworkPowerManager.java +++ b/common/crazypants/enderio/conduit/power/NetworkPowerManager.java @@ -29,9 +29,10 @@ public class NetworkPowerManager { private int inactiveTicks = 100; private final List receptors = new ArrayList(); - //private final List storageReceptors = new ArrayList(); private ListIterator receptorIterator = receptors.listIterator(); + private final List storageReceptors = new ArrayList(); + private boolean lastActiveValue = false; private int ticksWithNoPower = 0; @@ -58,9 +59,9 @@ public class NetworkPowerManager { public void applyRecievedPower() { - updateStorage(); + validateStorage(); - float extracted = extractRecievedEnergy(); + float extracted = extractRecievedEnergy(); addEnergy(extracted); checkReserves(); float quantity = energyStored; @@ -88,7 +89,8 @@ public class NetworkPowerManager { int appliedCount = 0; int numReceptors = receptors.size(); - int numEngines = 0; + storageReceptors.clear(); + while (quantity > 0 && appliedCount < numReceptors) { if (!receptors.isEmpty() && !receptorIterator.hasNext()) { @@ -100,7 +102,10 @@ public class NetworkPowerManager { PowerReceiver pp = r.powerReceptor.getPowerReceiver(r.direction); if (pp != null) { - if (pp.getType() != Type.ENGINE) { + 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) { float used = 0; float nonReservedPower = quantity - reserved; @@ -117,29 +122,47 @@ public class NetworkPowerManager { } else 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); + float offer = Math.min(requested, canOffer); + used = pp.receiveEnergy(Type.PIPE, offer, r.direction); } } quantity -= used; - } else if (pp.getType() == Type.ENGINE && !(r.powerReceptor instanceof IInternalPowerReceptor)) { - numEngines++; - } - + } if (quantity <= 0) { break; } } - appliedCount++; } - float leaveForBlowingUpEngines = numEngines * 8; - if (leaveForBlowingUpEngines > maxEnergyStored) { - leaveForBlowingUpEngines = maxEnergyStored; + + //send any energy left over to the storage + if (quantity > 0 && !storageReceptors.isEmpty()) { + for (ReceptorEntry r : storageReceptors) { + PowerReceiver pp = r.powerReceptor.getPowerReceiver(r.direction); + if (pp != null) { + float used = 0; + float available = quantity - reserved; + //NB: We are using the max energy the the conduit can receive here rather than the amount they can output + //as we always want to be able to give back as much as we received if we haven't used it or we will + //leak energy + float canOffer = Math.min(r.emmiter.getCapacitor().getMaxEnergyReceived(), 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); + } + quantity -= used; + } + if (quantity <= 0) { + break; + } + } } - energyStored = Math.min(quantity, maxEnergyStored - leaveForBlowingUpEngines); + energyStored = quantity; updateConduitStorage(); } @@ -166,6 +189,7 @@ public class NetworkPowerManager { } private void updateConduitStorage() { + //TODO: Even this out or we end up with some blocked up conduits!! float energyLeft = energyStored; for (IPowerConduit con : network.getConduits()) { float give = con.getCapacitor().getMaxEnergyStored(); @@ -181,7 +205,7 @@ public class NetworkPowerManager { return energyStored > 0; } - private void updateStorage() { + private void validateStorage() { maxEnergyStored = 0; energyStored = 0; for (IPowerConduit con : network.getConduits()) { @@ -206,7 +230,7 @@ public class NetworkPowerManager { return extracted; } - public void receptorsChanged() { + public void receptorsChanged() { receptors.clear(); receptors.addAll(network.getPowerReceptors()); receptorIterator = receptors.listIterator(); diff --git a/common/crazypants/enderio/conduit/power/PowerConduit.java b/common/crazypants/enderio/conduit/power/PowerConduit.java index a4883eda..b48180c4 100644 --- a/common/crazypants/enderio/conduit/power/PowerConduit.java +++ b/common/crazypants/enderio/conduit/power/PowerConduit.java @@ -31,11 +31,11 @@ import crazypants.vecmath.Vector3d; public class PowerConduit extends AbstractConduit implements IPowerConduit { static final Map ICONS = new HashMap(); - + static final ICapacitor[] CAPACITORS = new BasicCapacitor[] { - new BasicCapacitor(0, 64, 128, 0, 0, 0, 256), - new BasicCapacitor(0, 128, 256, 0, 0, 0, 1024), - new BasicCapacitor(0, 1024, 1024, 0, 0, 0, 2048) + new BasicCapacitor(250, 1500, 128), + new BasicCapacitor(350, 3000, 512), + new BasicCapacitor(500, 5000, 2048) }; static final String[] POSTFIX = new String[] { "", "Enhanced", "Ender" }; diff --git a/common/crazypants/enderio/machine/generator/TileEntityStirlingGenerator.java b/common/crazypants/enderio/machine/generator/TileEntityStirlingGenerator.java index 00d97a18..949ae536 100644 --- a/common/crazypants/enderio/machine/generator/TileEntityStirlingGenerator.java +++ b/common/crazypants/enderio/machine/generator/TileEntityStirlingGenerator.java @@ -16,6 +16,8 @@ import buildcraft.api.power.PowerHandler.PowerReceiver; import buildcraft.api.power.PowerHandler.Type; import crazypants.enderio.ModObject; import crazypants.enderio.machine.AbstractMachineEntity; +import crazypants.enderio.power.BasicCapacitor; +import crazypants.enderio.power.Capacitors; import crazypants.enderio.power.IInternalPowerReceptor; import crazypants.enderio.power.PowerHandlerUtil; import crazypants.util.BlockCoord; @@ -34,6 +36,17 @@ public class TileEntityStirlingGenerator extends AbstractMachineEntity implement public TileEntityStirlingGenerator() { super(1, Type.ENGINE); + configurePowerHandler(); + } + + @Override + public void setCapacitor(Capacitors capacitorType) { + super.setCapacitor(capacitorType); + configurePowerHandler(); + } + + void configurePowerHandler() { + powerHandler.configure(0, 0, 0, capacitorType.capacitor.getMaxEnergyStored()); } @Override diff --git a/common/crazypants/enderio/machine/power/TileCapacitorBank.java b/common/crazypants/enderio/machine/power/TileCapacitorBank.java index e52c8e05..8c4ee4ad 100644 --- a/common/crazypants/enderio/machine/power/TileCapacitorBank.java +++ b/common/crazypants/enderio/machine/power/TileCapacitorBank.java @@ -41,10 +41,10 @@ public class TileCapacitorBank extends TileEntity implements IInternalPowerRecep private RedstoneControlMode inputControlMode; - private RedstoneControlMode outputControlMode; - + private RedstoneControlMode outputControlMode; + private boolean outputEnabled; - + private boolean inputEnabled; private final List receptors = new ArrayList(); @@ -85,23 +85,29 @@ public class TileCapacitorBank extends TileEntity implements IInternalPowerRecep // do the required tick to keep BC API happy float stored = powerHandler.getEnergyStored(); powerHandler.update(); + + // do a dummy recieve of power to force the updating of what is an isn't a + // power source as we rely on this + // to make sure we dont both send and recieve to the same source + powerHandler.getPowerReceiver().receiveEnergy(Type.STORAGE, 1, null); + powerHandler.setEnergy(stored); boolean requiresClientSync = false; boolean hasSignal = isRecievingRedstoneSignal(); - if(inputControlMode == RedstoneControlMode.IGNORE) { + if (inputControlMode == RedstoneControlMode.IGNORE) { inputEnabled = true; } else { - inputEnabled = (inputControlMode == RedstoneControlMode.ON && hasSignal) || (inputControlMode == RedstoneControlMode.OFF && !hasSignal); + inputEnabled = (inputControlMode == RedstoneControlMode.ON && hasSignal) || (inputControlMode == RedstoneControlMode.OFF && !hasSignal); } - if(outputControlMode == RedstoneControlMode.IGNORE) { + if (outputControlMode == RedstoneControlMode.IGNORE) { outputEnabled = true; } else { - outputEnabled = (outputControlMode == RedstoneControlMode.ON && hasSignal) || (outputControlMode == RedstoneControlMode.OFF && !hasSignal); + outputEnabled = (outputControlMode == RedstoneControlMode.ON && hasSignal) || (outputControlMode == RedstoneControlMode.OFF && !hasSignal); } - if(outputEnabled) { + if (outputEnabled) { transmitEnergy(); } @@ -112,7 +118,7 @@ public class TileCapacitorBank extends TileEntity implements IInternalPowerRecep if (requiresClientSync) { lastSyncPowerStored = storedEnergy; - + // this will cause 'getPacketDescription()' to be called and its result // will be sent to the PacketHandler on the other end of // client/server connection @@ -143,7 +149,7 @@ public class TileCapacitorBank extends TileEntity implements IInternalPowerRecep Receptor receptor = receptorIterator.next(); PowerReceiver pp = receptor.receptor.getPowerReceiver(receptor.fromDir); - if (pp != null && pp.getMinEnergyReceived() <= canTransmit && pp.getType() != Type.ENGINE) { + if (pp != null && pp.getMinEnergyReceived() <= canTransmit && pp.getType() != Type.ENGINE && !powerHandler.isPowerSource(receptor.fromDir)) { float used; if (receptor.receptor instanceof IInternalPowerReceptor) { // System.out.println("TileEntityStirlingGenerator.transmitEnergy: Sending " @@ -254,41 +260,41 @@ public class TileCapacitorBank extends TileEntity implements IInternalPowerRecep } return false; } - + public RedstoneControlMode getInputControlMode() { return inputControlMode; } public void setInputControlMode(RedstoneControlMode inputControlMode) { - if(!isMultiblock()) { + if (!isMultiblock()) { this.inputControlMode = inputControlMode; } else { - for(BlockCoord bc : multiblock) { + for (BlockCoord bc : multiblock) { TileCapacitorBank cp = getCapBank(bc); - if(cp != null) { + if (cp != null) { cp.inputControlMode = inputControlMode; } } - } + } } - + public RedstoneControlMode getOutputControlMode() { return outputControlMode; } public void setOutputControlMode(RedstoneControlMode outputControlMode) { - if(!isMultiblock()) { + if (!isMultiblock()) { this.outputControlMode = outputControlMode; } else { - for(BlockCoord bc : multiblock) { + for (BlockCoord bc : multiblock) { TileCapacitorBank cp = getCapBank(bc); - if(cp != null) { + if (cp != null) { cp.outputControlMode = outputControlMode; } } } } - + // ------------ Multiblock implementations @@ -301,15 +307,15 @@ public class TileCapacitorBank extends TileEntity implements IInternalPowerRecep } PowerHandler doGetPowerHandler() { - if(inputEnabled) { + if (inputEnabled) { return powerHandler; - } + } return getDisabledPowerHandler(); } private PowerHandler getDisabledPowerHandler() { - if(disabledPowerHandler == null) { - disabledPowerHandler = PowerHandlerUtil.createHandler(new BasicCapacitor(0,0), this, Type.STORAGE); + if (disabledPowerHandler == null) { + disabledPowerHandler = PowerHandlerUtil.createHandler(new BasicCapacitor(0, 0), this, Type.STORAGE); } return disabledPowerHandler; } @@ -349,7 +355,7 @@ public class TileCapacitorBank extends TileEntity implements IInternalPowerRecep private void updatePowerHandler() { powerHandler = PowerHandlerUtil.createHandler(new BasicCapacitor(maxIO, maxStoredEnergy), this, Type.STORAGE); - if(storedEnergy > maxStoredEnergy) { + if (storedEnergy > maxStoredEnergy) { storedEnergy = maxStoredEnergy; } powerHandler.setEnergy(storedEnergy); diff --git a/common/crazypants/enderio/machine/solar/TileEntitySolarPanel.java b/common/crazypants/enderio/machine/solar/TileEntitySolarPanel.java index 4d994f6d..734e3983 100644 --- a/common/crazypants/enderio/machine/solar/TileEntitySolarPanel.java +++ b/common/crazypants/enderio/machine/solar/TileEntitySolarPanel.java @@ -31,7 +31,7 @@ public class TileEntitySolarPanel extends TileEntity implements IInternalPowerRe private float energyPerTick = 1; public TileEntitySolarPanel() { - capacitor = new BasicCapacitor(); + capacitor = new BasicCapacitor(0,10,10000); powerHandler = PowerHandlerUtil.createHandler(capacitor, this, Type.ENGINE); } diff --git a/common/crazypants/util/DebugGuiTPS.java b/common/crazypants/util/DebugGuiTPS.java new file mode 100644 index 00000000..8cc362c1 --- /dev/null +++ b/common/crazypants/util/DebugGuiTPS.java @@ -0,0 +1,85 @@ +package crazypants.util; + +import java.awt.BorderLayout; +import java.awt.FlowLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.text.DecimalFormat; + +import javax.swing.Box; +import javax.swing.BoxLayout; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.Timer; + +import net.minecraft.server.MinecraftServer; +import net.minecraftforge.common.DimensionManager; + +public class DebugGuiTPS extends JFrame { + + public static void showTpsGUI() { + DebugGuiTPS g = new DebugGuiTPS(); + g.setVisible(true); + } + + private static final DecimalFormat FORMAT = new DecimalFormat("########0.000"); + + + private JPanel contentPane = new JPanel(); + + private DebugGuiTPS() { + Timer timer = new Timer(2000, new ActionListener() { + + @Override + public void actionPerformed(ActionEvent arg0) { + updateTps(); + } + }); + timer.setRepeats(true); + timer.start(); + + + + contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); + + setTitle("TPS"); + setLayout(new BorderLayout()); + add(contentPane, BorderLayout.CENTER); + setSize(200, 120); + } + + private void updateTps() { + + contentPane.removeAll(); + + MinecraftServer server = MinecraftServer.getServer(); + if(server == null) { + return; + } + + String s = "Avg tick: " + FORMAT.format(average(server.tickTimeArray) * 1.0E-6D) + " ms"; + JLabel l = new JLabel(s); + contentPane.add(l); + + if (server.worldServers != null) { + for (Integer id : DimensionManager.getIDs()) { + s = "Lvl " + id + " tick: " + FORMAT.format(average(server.worldTickTimes.get(id)) * 1.0E-6D) + " ms"; + l = new JLabel(s); + contentPane.add(l); + } + } + revalidate(); + repaint(); + + } + + private double average(long[] values) { + long i = 0L; + for (int j = 0; j < values.length; ++j) { + i += values[j]; + } + return (double) i / (double) values.length; + } + +} diff --git a/resources/mcmod.info b/resources/mcmod.info index 3c4a29bd..e3ad756e 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.13", + "version": "0.1.14", "mcversion": "1.6.2", "url": "http://www.minecraftforum.net/topic/1937619-162152-ender-io-how-many-pipes-in-one-block-updated-198/", "updateUrl": "",