diff --git a/gradle.properties b/gradle.properties index f3eba408e..da17ede91 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ MAJORVERSION = 1 MINORVERSION = 16 -REVISION = 15 +REVISION = 16 GROUP = 'net.electricalage.eln' ARCHIVE_NAME = 'ElectricalAge-jrddunbr' diff --git a/src/main/java/mods/eln/Eln.java b/src/main/java/mods/eln/Eln.java index 99b99cf63..3aa297e93 100644 --- a/src/main/java/mods/eln/Eln.java +++ b/src/main/java/mods/eln/Eln.java @@ -63,9 +63,6 @@ import mods.eln.simplenode.computerprobe.ComputerProbeNode; import mods.eln.simplenode.energyconverter.EnergyConverterElnToOtherBlock; import mods.eln.simplenode.energyconverter.EnergyConverterElnToOtherDescriptor; -import mods.eln.simplenode.energyconverter.EnergyConverterElnToOtherDescriptor.ElnDescriptor; -import mods.eln.simplenode.energyconverter.EnergyConverterElnToOtherDescriptor.Ic2Descriptor; -import mods.eln.simplenode.energyconverter.EnergyConverterElnToOtherDescriptor.OcDescriptor; import mods.eln.simplenode.energyconverter.EnergyConverterElnToOtherEntity; import mods.eln.simplenode.energyconverter.EnergyConverterElnToOtherNode; import mods.eln.simplenode.test.TestBlock; @@ -269,13 +266,10 @@ public class Eln { public GraphiteDescriptor GraphiteDescriptor; public ElectricalCableDescriptor creativeCableDescriptor; - /*public ElectricalCableDescriptor T2TransmissionCableDescriptor; - public ElectricalCableDescriptor T1TransmissionCableDescriptor;*/ public ElectricalCableDescriptor veryHighVoltageCableDescriptor; public ElectricalCableDescriptor highVoltageCableDescriptor; public ElectricalCableDescriptor signalCableDescriptor; public ElectricalCableDescriptor lowVoltageCableDescriptor; - public ElectricalCableDescriptor batteryCableDescriptor; public ElectricalCableDescriptor meduimVoltageCableDescriptor; public ElectricalCableDescriptor signalBusCableDescriptor; @@ -593,7 +587,7 @@ public void preInit(FMLPreInitializationEvent event) { SixNode.sixNodeCacheList.add(new SixNodeCacheStd()); registerTestBlock(); - registerEnergyConverter(); + //registerEnergyConverter(); registerComputer(); registerArmor(); @@ -839,7 +833,7 @@ public void load(FMLInitializationEvent event) { registerReplicator(); // - recipeEnergyConverter(); + //recipeEnergyConverter(); recipeComputerProbe(); recipeArmor(); @@ -946,9 +940,9 @@ public void load(FMLInitializationEvent event) { Utils.println("Electrical age init done"); } - private EnergyConverterElnToOtherBlock elnToOtherBlockLvu; - private EnergyConverterElnToOtherBlock elnToOtherBlockMvu; - private EnergyConverterElnToOtherBlock elnToOtherBlockHvu; + private EnergyConverterElnToOtherBlock elnToOtherBlockConverter; + + public Double ELN_CONVERTER_MAX_POWER = 120_000.0; private void registerEnergyConverter() { if (ElnToOtherEnergyConverterEnable) { @@ -958,37 +952,12 @@ private void registerEnergyConverter() { NodeManager.registerUuid(EnergyConverterElnToOtherNode.getNodeUuidStatic(), EnergyConverterElnToOtherNode.class); { - String blockName = TR_NAME(Type.TILE, "eln.EnergyConverterElnToOtherLVUBlock"); - ElnDescriptor elnDesc = new ElnDescriptor(LVU, LVP()); - Ic2Descriptor ic2Desc = new Ic2Descriptor(32, 1); - OcDescriptor ocDesc = new OcDescriptor(ic2Desc.outMax * Other.getElnToOcConversionRatio() / Other.getElnToIc2ConversionRatio()); - EnergyConverterElnToOtherDescriptor desc = - new EnergyConverterElnToOtherDescriptor("EnergyConverterElnToOtherLVU", elnDesc, ic2Desc, ocDesc); - elnToOtherBlockLvu = new EnergyConverterElnToOtherBlock(desc); - elnToOtherBlockLvu.setCreativeTab(creativeTab).setBlockName(blockName); - GameRegistry.registerBlock(elnToOtherBlockLvu, SimpleNodeItem.class, blockName); - } - { - String blockName = TR_NAME(Type.TILE, "eln.EnergyConverterElnToOtherMVUBlock"); - ElnDescriptor elnDesc = new ElnDescriptor(MVU, MVP()); - Ic2Descriptor ic2Desc = new Ic2Descriptor(128, 2); - OcDescriptor ocDesc = new OcDescriptor(ic2Desc.outMax * Other.getElnToOcConversionRatio() / Other.getElnToIc2ConversionRatio()); + String blockName = TR_NAME(Type.TILE, "eln.EnergyConverter"); EnergyConverterElnToOtherDescriptor desc = - new EnergyConverterElnToOtherDescriptor("EnergyConverterElnToOtherMVU", elnDesc, ic2Desc, ocDesc); - elnToOtherBlockMvu = new EnergyConverterElnToOtherBlock(desc); - elnToOtherBlockMvu.setCreativeTab(creativeTab).setBlockName(blockName); - GameRegistry.registerBlock(elnToOtherBlockMvu, SimpleNodeItem.class, blockName); - } - { - String blockName = TR_NAME(Type.TILE, "eln.EnergyConverterElnToOtherHVUBlock"); - ElnDescriptor elnDesc = new ElnDescriptor(HVU, HVP()); - Ic2Descriptor ic2Desc = new Ic2Descriptor(512, 3); - OcDescriptor ocDesc = new OcDescriptor(ic2Desc.outMax * Other.getElnToOcConversionRatio() / Other.getElnToIc2ConversionRatio()); - EnergyConverterElnToOtherDescriptor desc = - new EnergyConverterElnToOtherDescriptor("EnergyConverterElnToOtherHVU", elnDesc, ic2Desc, ocDesc); - elnToOtherBlockHvu = new EnergyConverterElnToOtherBlock(desc); - elnToOtherBlockHvu.setCreativeTab(creativeTab).setBlockName(blockName); - GameRegistry.registerBlock(elnToOtherBlockHvu, SimpleNodeItem.class, blockName); + new EnergyConverterElnToOtherDescriptor("EnergyConverterElnToOtherLVU", ELN_CONVERTER_MAX_POWER); + elnToOtherBlockConverter = new EnergyConverterElnToOtherBlock(desc); + elnToOtherBlockConverter.setCreativeTab(creativeTab).setBlockName(blockName); + //GameRegistry.registerBlock(elnToOtherBlockConverter, SimpleNodeItem.class, blockName); } } } @@ -1309,7 +1278,6 @@ LVU, LVP() / 4, 0.2 / 20,// electricalNominalVoltage, cableHeatingTime, cableThermalConductionTao// thermalNominalHeatTime, // thermalConductivityTao ); - batteryCableDescriptor = desc; } @@ -1498,17 +1466,13 @@ private void registerThermalCable(int id) { public FunctionTable batteryVoltageFunctionTable; private void registerBattery(int id) { - int subId, completId; + int subId; String name; double heatTIme = 30; double[] voltageFunctionTable = {0.000, 0.9, 1.0, 1.025, 1.04, 1.05, 2.0}; FunctionTable voltageFunction = new FunctionTable(voltageFunctionTable, 6.0 / 5); - double[] condoVoltageFunctionTable = {0.000, 0.89, 0.90, 0.905, 0.91, 1.1, - 1.5}; - FunctionTable condoVoltageFunction = new FunctionTable(condoVoltageFunctionTable, - 6.0 / 5); Utils.printFunction(voltageFunction, -0.2, 1.2, 0.1); @@ -1516,28 +1480,25 @@ private void registerBattery(int id) { double stdU = LVU; double stdP = LVP() / 4; double stdEfficiency = 1.0 - 2.0 / 50.0; - double condoEfficiency = 1.0 - 2.0 / 50.0; batteryVoltageFunctionTable = voltageFunction; { subId = 0; name = TR_NAME(Type.NONE, "Cost Oriented Battery"); - BatteryDescriptor desc = new BatteryDescriptor(name, "BatteryBig", batteryCableDescriptor, - 0.5, //what % of charge it starts out with - true, true, //is rechargable?, Uses Life Mechanic? + BatteryDescriptor desc = new BatteryDescriptor(name, "BatteryBig", + 0.5, + true, true, voltageFunction, - stdU, //battery nominal voltage - stdP * 1.2, //how much power it can handle at max, - 0.00, //precentage of its total output to self-discharge. Should probably be 0 - stdP, //no idea + stdU, + stdP * 1.2, + 0.0, + stdP, stdDischargeTime * batteryCapacityFactor, stdEfficiency, stdBatteryHalfLife, - - heatTIme, 60, -100, // thermalHeatTime, thermalWarmLimit, // thermalCoolLimit, - "Cheap battery" // name, description) + heatTIme, 60, -100 ); desc.setRenderSpec("lowcost"); - desc.setCurrentDrop(desc.electricalU * 1.2, desc.electricalStdP * 1.0); + desc.setCurrentDrop(desc.getElectricalU() * 1.2, desc.getElectricalStdP() * 1.0); transparentNodeItem.addDescriptor(subId + (id << 6), desc); } { @@ -1545,19 +1506,14 @@ private void registerBattery(int id) { name = TR_NAME(Type.NONE, "Capacity Oriented Battery"); BatteryDescriptor desc = new BatteryDescriptor(name, - "BatteryBig", batteryCableDescriptor, 0.5, true, true, voltageFunction, - stdU / 4, stdP / 2 * 1.2, 0.000, // electricalU, - // electricalPMax,electricalDischargeRate - stdP / 2, stdDischargeTime * 8 * batteryCapacityFactor, stdEfficiency, stdBatteryHalfLife, // electricalStdP, - // electricalStdDischargeTime, - // electricalStdEfficiency, - // electricalStdHalfLife, - heatTIme, 60, -100, // thermalHeatTime, thermalWarmLimit, - // thermalCoolLimit, - "the battery" // name, description) + "BatteryBig", 0.5, true, true, voltageFunction, + stdU / 4, stdP / 2 * 1.2, 0.000, + stdP / 2, + stdDischargeTime * 8 * batteryCapacityFactor, stdEfficiency, stdBatteryHalfLife, + heatTIme, 60, -100 ); desc.setRenderSpec("capacity"); - desc.setCurrentDrop(desc.electricalU * 1.2, desc.electricalStdP * 1.0); + desc.setCurrentDrop(desc.getElectricalU() * 1.2, desc.getElectricalStdP() * 1.0); transparentNodeItem.addDescriptor(subId + (id << 6), desc); } { @@ -1565,19 +1521,13 @@ private void registerBattery(int id) { name = TR_NAME(Type.NONE, "Voltage Oriented Battery"); BatteryDescriptor desc = new BatteryDescriptor(name, - "BatteryBig", meduimVoltageCableDescriptor, 0.5, true, true, voltageFunction, stdU * 4, - stdP * 1.2, 0.000, // electricalU, - // electricalPMax,electricalDischargeRate - stdP, stdDischargeTime * batteryCapacityFactor, stdEfficiency, stdBatteryHalfLife, // electricalStdP, - // electricalStdDischargeTime, - // electricalStdEfficiency, - // electricalStdHalfLife, - heatTIme, 60, -100, // thermalHeatTime, thermalWarmLimit, - // thermalCoolLimit, - "the battery" // name, description) + "BatteryBig", 0.5, true, true, voltageFunction, stdU * 4, + stdP * 1.2, 0.000, + stdP, stdDischargeTime * batteryCapacityFactor, stdEfficiency, stdBatteryHalfLife, + heatTIme, 60, -100 ); desc.setRenderSpec("highvoltage"); - desc.setCurrentDrop(desc.electricalU * 1.2, desc.electricalStdP * 1.0); + desc.setCurrentDrop(desc.getElectricalU() * 1.2, desc.getElectricalStdP() * 1.0); transparentNodeItem.addDescriptor(subId + (id << 6), desc); } @@ -1586,19 +1536,13 @@ private void registerBattery(int id) { name = TR_NAME(Type.NONE, "Current Oriented Battery"); BatteryDescriptor desc = new BatteryDescriptor(name, - "BatteryBig", batteryCableDescriptor, 0.5, true, true, voltageFunction, stdU, - stdP * 1.2 * 4, 0.000, // electricalU, - // electricalPMax,electricalDischargeRate - stdP * 4, stdDischargeTime / 6 * batteryCapacityFactor, stdEfficiency, stdBatteryHalfLife, // electricalStdP, - // electricalStdDischargeTime, - // electricalStdEfficiency, - // electricalStdHalfLife, - heatTIme, 60, -100, // thermalHeatTime, thermalWarmLimit, - // thermalCoolLimit, - "the battery" // name, description) + "BatteryBig", 0.5, true, true, voltageFunction, stdU, + stdP * 1.2 * 4, 0.000, + stdP * 4, stdDischargeTime / 6 * batteryCapacityFactor, stdEfficiency, stdBatteryHalfLife, + heatTIme, 60, -100 ); desc.setRenderSpec("current"); - desc.setCurrentDrop(desc.electricalU * 1.2, desc.electricalStdP * 1.0); + desc.setCurrentDrop(desc.getElectricalU() * 1.2, desc.getElectricalStdP() * 1.0); transparentNodeItem.addDescriptor(subId + (id << 6), desc); } { @@ -1606,19 +1550,13 @@ private void registerBattery(int id) { name = TR_NAME(Type.NONE, "Life Oriented Battery"); BatteryDescriptor desc = new BatteryDescriptor(name, - "BatteryBig", batteryCableDescriptor, 0.5, true, false, voltageFunction, stdU, - stdP * 1.2, 0.000, // electricalU, - // electricalPMax,electricalDischargeRate - stdP, stdDischargeTime * batteryCapacityFactor, stdEfficiency, stdBatteryHalfLife * 8, // electricalStdP, - // electricalStdDischargeTime, - // electricalStdEfficiency, - // electricalStdHalfLife, - heatTIme, 60, -100, // thermalHeatTime, thermalWarmLimit, - // thermalCoolLimit, - "the battery" // name, description) + "BatteryBig", 0.5, true, false, voltageFunction, stdU, + stdP * 1.2, 0.000, + stdP, stdDischargeTime * batteryCapacityFactor, stdEfficiency, stdBatteryHalfLife * 8, + heatTIme, 60, -100 ); desc.setRenderSpec("life"); - desc.setCurrentDrop(desc.electricalU * 1.2, desc.electricalStdP * 1.0); + desc.setCurrentDrop(desc.getElectricalU() * 1.2, desc.getElectricalStdP() * 1.0); transparentNodeItem.addDescriptor(subId + (id << 6), desc); } @@ -1627,16 +1565,10 @@ private void registerBattery(int id) { name = TR_NAME(Type.NONE, "Single-use Battery"); BatteryDescriptor desc = new BatteryDescriptor(name, - "BatteryBig", batteryCableDescriptor, 1.0, false, false, voltageFunction, stdU, - stdP * 1.2 * 2, 0.000, // electricalU, - // electricalPMax,electricalDischargeRate - stdP * 2, stdDischargeTime / 4 * batteryCapacityFactor, stdEfficiency, stdBatteryHalfLife * 8, // electricalStdP, - // electricalStdDischargeTime, - // electricalStdEfficiency, - // electricalStdHalfLife, - heatTIme, 60, -100, // thermalHeatTime, thermalWarmLimit, - // thermalCoolLimit, - "the battery" // name, description) + "BatteryBig", 1.0, false, false, voltageFunction, stdU, + stdP * 1.2 * 2, 0.000, + stdP * 2, stdDischargeTime / 4 * batteryCapacityFactor, stdEfficiency, stdBatteryHalfLife * 8, + heatTIme, 60, -100 ); desc.setRenderSpec("coal"); transparentNodeItem.addDescriptor(subId + (id << 6), desc); @@ -1646,64 +1578,15 @@ private void registerBattery(int id) { name = TR_NAME(Type.NONE, "Experimental Battery"); BatteryDescriptor desc = new BatteryDescriptor(name, - "BatteryBig", batteryCableDescriptor, 0.5, true, false, voltageFunction, stdU * 2, - stdP * 1.2 * 8, 0.025, // electricalU, - // electricalPMax,electricalDischargeRate - stdP * 8, stdDischargeTime / 4 * batteryCapacityFactor, stdEfficiency, stdBatteryHalfLife * 8, // electricalStdP, - // electricalStdDischargeTime, - // electricalStdEfficiency, - // electricalStdHalfLife, - heatTIme, 60, -100, // thermalHeatTime, thermalWarmLimit, - // thermalCoolLimit, - "You were unable to fix the power leaking problem, though." // name, description) + "BatteryBig", 0.5, true, false, voltageFunction, stdU * 2, + stdP * 1.2 * 8, 0.025, + stdP * 8, stdDischargeTime / 4 * batteryCapacityFactor, stdEfficiency, stdBatteryHalfLife * 8, + heatTIme, 60, -100 ); desc.setRenderSpec("highvoltage"); - desc.setCurrentDrop(desc.electricalU * 1.2, desc.electricalStdP * 1.0); + desc.setCurrentDrop(desc.getElectricalU() * 1.2, desc.getElectricalStdP() * 1.0); transparentNodeItem.addDescriptor(subId + (id << 6), desc); } - /*{ - subId = 32; - name = TR_NAME(Type.NONE, "50V Condensator"); - - BatteryDescriptor desc = new BatteryDescriptor(name, - "condo200", batteryCableDescriptor, 0.0, true, false, - condoVoltageFunction, - stdU, stdP * 1.2 * 8, 0.005, // electricalU,// - // electricalPMax,electricalDischargeRate - stdP * 8, 4, condoEfficiency, stdBatteryHalfLife, // electricalStdP, - // electricalStdDischargeTime, - // electricalStdEfficiency, - // electricalStdHalfLife, - heatTIme, 60, -100, // thermalHeatTime, thermalWarmLimit, - // thermalCoolLimit, - "Obselete, must be deleted" // name, description) - ); - desc.setCurrentDrop(desc.electricalU * 1.2, desc.electricalStdP * 2.0); - desc.setDefaultIcon("empty-texture"); - transparentNodeItem.addWithoutRegistry(subId + (id << 6), desc); - } - - { - subId = 36; - name = TR_NAME(I18N.Type.NONE, "200V Condensator"); - - BatteryDescriptor desc = new BatteryDescriptor(name, - "condo200", highVoltageCableDescriptor, 0.0, true, false, - condoVoltageFunction, - MVU, MVP() * 1.5, 0.005, // electricalU,// - // electricalPMax,electricalDischargeRate - MVP(), 4, condoEfficiency, stdBatteryHalfLife, // electricalStdP, - // electricalStdDischargeTime, - // electricalStdEfficiency, - // electricalStdHalfLife, - heatTIme, 60, -100, // thermalHeatTime, thermalWarmLimit, - // thermalCoolLimit, - "the battery" // name, description) - ); - desc.setCurrentDrop(desc.electricalU * 1.2, desc.electricalStdP * 2.0); - desc.setDefaultIcon("empty-texture"); - transparentNodeItem.addWithoutRegistry(subId + (id << 6), desc); - } */ } private void registerGround(int id) { @@ -2958,7 +2841,7 @@ private void registerTransformer(int id) { LegacyDcDcDescriptor desc = new LegacyDcDcDescriptor(name, obj.getObj("transformator"), obj.getObj("feromagneticcorea"), obj.getObj("transformatorCase"), 0.5f); - transparentNodeItem.addDescriptor(subId + (id << 6), desc); + transparentNodeItem.addWithoutRegistry(subId + (id << 6), desc); } { subId = 1; @@ -8316,25 +8199,7 @@ private void recipeEggIncubator() { private void recipeEnergyConverter() { if (ElnToOtherEnergyConverterEnable) { - addRecipe(new ItemStack(elnToOtherBlockLvu), - "III", - "cCR", - "III", - 'C', dictCheapChip, - 'c', findItemStack("Low Voltage Cable"), - 'I', findItemStack("Iron Cable"), - 'R', "ingotCopper"); - - addRecipe(new ItemStack(elnToOtherBlockMvu), - "III", - "cCR", - "III", - 'C', dictCheapChip, - 'c', findItemStack("Medium Voltage Cable"), - 'I', findItemStack("Iron Cable"), - 'R', dictTungstenIngot); - - addRecipe(new ItemStack(elnToOtherBlockHvu), + addRecipe(new ItemStack(elnToOtherBlockConverter), "III", "cCR", "III", @@ -8342,7 +8207,6 @@ private void recipeEnergyConverter() { 'c', findItemStack("High Voltage Cable"), 'I', findItemStack("Iron Cable"), 'R', new ItemStack(Items.gold_ingot)); - } } diff --git a/src/main/java/mods/eln/fluid/ElementSidedFluidHandler.kt b/src/main/java/mods/eln/fluid/ElementSidedFluidHandler.kt index a57a2cd6b..2f12e59c2 100644 --- a/src/main/java/mods/eln/fluid/ElementSidedFluidHandler.kt +++ b/src/main/java/mods/eln/fluid/ElementSidedFluidHandler.kt @@ -154,17 +154,17 @@ open class ElementSidedFluidHandler: IFluidHandler, INBTTReady { tank.readFromNBT(nbt, "${str}tank$idx") tankList.add(tank) } - println("numTanks: $numTanks") - println("tankList $tankList") + //println("numTanks: $numTanks") + //println("tankList $tankList") tanks.clear() ForgeDirection.VALID_DIRECTIONS.forEach { val tankRef = nbt.getInteger("${str}${it.name}tankRef") if (tankRef != -1 && numTanks != 0) { - println("$it: $tankRef") + //println("$it: $tankRef") tanks[it] = tankList[tankRef] } } - println("tanks: $tanks") + //println("tanks: $tanks") } override fun writeToNBT(nbt: NBTTagCompound, str: String) { diff --git a/src/main/java/mods/eln/misc/ThermalParameters.kt b/src/main/java/mods/eln/misc/ThermalParameters.kt new file mode 100644 index 000000000..41d318d3f --- /dev/null +++ b/src/main/java/mods/eln/misc/ThermalParameters.kt @@ -0,0 +1,9 @@ +package mods.eln.misc + +data class ThermalParameters(var thermalHeatTime: Double, var thermalMaxTemp: Double?, var thermalMinTemp: Double?) { + companion object { + fun battery(): ThermalParameters { + return ThermalParameters(60.0, 100.0, -40.0) + } + } +} diff --git a/src/main/java/mods/eln/node/NodeEntityClientSender.java b/src/main/java/mods/eln/node/NodeEntityClientSender.java index 6bdb35911..281d7a268 100644 --- a/src/main/java/mods/eln/node/NodeEntityClientSender.java +++ b/src/main/java/mods/eln/node/NodeEntityClientSender.java @@ -129,4 +129,22 @@ public void clientSendInt(Byte id, int str) { } } + + public void clientSendDouble(Byte id, double num) { + try { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + DataOutputStream stream = new DataOutputStream(bos); + + preparePacketForServer(stream); + + stream.writeByte(id); + stream.writeDouble(num); + + sendPacketToServer(bos); + } catch (IOException e) { + + e.printStackTrace(); + } + + } } diff --git a/src/main/java/mods/eln/sim/BatteryProcess.java b/src/main/java/mods/eln/sim/BatteryProcess.java deleted file mode 100644 index 468abb045..000000000 --- a/src/main/java/mods/eln/sim/BatteryProcess.java +++ /dev/null @@ -1,132 +0,0 @@ -package mods.eln.sim; - -import mods.eln.misc.FunctionTable; -import mods.eln.sim.mna.component.VoltageSource; -import mods.eln.sim.mna.state.VoltageState; - -public class BatteryProcess implements IProcess { - - VoltageState positiveLoad, negativeLoad; - public FunctionTable voltageFunction; - public double Q = 0, QNominal = 0; - public double uNominal = 0; - public double life = 1.0; - private ThermalLoad thermalLoad = null; - //public double efficiency = 1.0; - - public VoltageSource voltageSource; - - public boolean isRechargeable = true; - public double IMax = 20; - - public BatteryProcess(VoltageState positiveLoad, VoltageState negativeLoad, FunctionTable voltageFunction, double IMax, VoltageSource voltageSource) { - this.positiveLoad = positiveLoad; - this.negativeLoad = negativeLoad; - this.voltageFunction = voltageFunction; - - this.IMax = IMax; - this.voltageSource = voltageSource; - } - - public BatteryProcess(VoltageState positiveLoad, VoltageState negativeLoad, FunctionTable voltageFunction, double IMax, VoltageSource voltageSource, ThermalLoad thermalLoad) { - this.positiveLoad = positiveLoad; - this.negativeLoad = negativeLoad; - this.voltageFunction = voltageFunction; - this.thermalLoad = thermalLoad; - this.IMax = IMax; - this.voltageSource = voltageSource; - } - - @Override - public void process(double time) { - double lastQ = Q; - double wasteQ = 0; - Q = Math.max(Q - voltageSource.getCurrent() * time / QNominal, 0); - if (Q > lastQ && !isRechargeable) { - System.out.println("Battery is recharging when it shouldn't!"); - wasteQ = Q - lastQ; - Q = lastQ; - } - double voltage = computeVoltage(); - voltageSource.setU(voltage); - if (wasteQ > 0 && thermalLoad != null) { - thermalLoad.movePowerTo(Math.abs(voltageSource.getCurrent() * voltage)); - } - } - - double computeVoltage() { - double voltage = voltageFunction.getValue(Q / life); - return Math.max(0, voltage * uNominal); - } - - public double getQRatio() { - return Q; - } - - public void changeLife(double newLife) { - if (newLife < life) { - Q *= newLife / life; - } - life = newLife; - } - - public double getCharge() { - return Q / life; - } - - public void setCharge(double charge) { - Q = life * charge; - } - - public double getEnergy() { - int stepNbr = 50; - double chargeStep = getCharge() / stepNbr; - double chargeIntegrator = 0; - double energy = 0; - double QperStep = QNominal * life * chargeStep; - - for (int step = 0; step < stepNbr; step++) { - double voltage = voltageFunction.getValue(chargeIntegrator) * uNominal; - energy += voltage * QperStep; - chargeIntegrator += chargeStep; - } - - return energy; - - /*double probeU = (positiveLoad.Uc - negativeLoad.Uc); - double q = 0, dq = 0.00001; - double e = 0; - double u; - while ((u = voltageFunction.getValue(q) * uNominal) < probeU) { - e += u * dq * QNominal; - q += dq; - - //if (e > 1) break; - } - return e * life; */ - } - - public double getEnergyMax() { - int stepNbr = 50; - double chargeStep = 1.0 / stepNbr; - double chargeIntegrator = 0; - double energy = 0; - double QperStep = QNominal * life * 1.0 / stepNbr; - - for (int step = 0; step < stepNbr; step++) { - double voltage = voltageFunction.getValue(chargeIntegrator) * uNominal; - energy += voltage * QperStep; - chargeIntegrator += chargeStep; - } - - return energy; - } - - public double getU() { - return computeVoltage(); - } - - public double getDischargeCurrent() { - return voltageSource.getI(); - } -} diff --git a/src/main/java/mods/eln/sim/BatteryProcess.kt b/src/main/java/mods/eln/sim/BatteryProcess.kt new file mode 100644 index 000000000..5ba350ac1 --- /dev/null +++ b/src/main/java/mods/eln/sim/BatteryProcess.kt @@ -0,0 +1,89 @@ +package mods.eln.sim + +import mods.eln.misc.FunctionTable +import mods.eln.sim.mna.component.VoltageSource +import mods.eln.sim.mna.state.VoltageState + +open class BatteryProcess( + var positiveLoad: VoltageState?, + var negativeLoad: VoltageState?, + var voltageFunction: FunctionTable, + @JvmField var IMax: Double, + var voltageSource: VoltageSource, + private var thermalLoad: ThermalLoad +) : IProcess { + + @JvmField + var Q = 0.0 + var QNominal = 0.0 + var uNominal = 0.0 + @JvmField + var life = 1.0 + var isRechargeable = true + + override fun process(time: Double) { + val lastQ = Q + var wasteQ = 0.0 + Q = Math.max(Q - voltageSource.current * time / QNominal, 0.0) + if (Q > lastQ && !isRechargeable) { + println("Battery is recharging when it shouldn't!") + wasteQ = Q - lastQ + Q = lastQ + } + val voltage = computeVoltage() + voltageSource.u = voltage + if (wasteQ > 0) { + thermalLoad.movePowerTo(Math.abs(voltageSource.current * voltage)) + } + } + + fun computeVoltage(): Double { + val voltage = voltageFunction.getValue(Q / life) + return Math.max(0.0, voltage * uNominal) + } + + fun changeLife(newLife: Double) { + if (newLife < life) { + Q *= newLife / life + } + life = newLife + } + + var charge: Double + get() = Q / life + set(charge) { + Q = life * charge + } + val energy: Double + get() { + val stepNbr = 50 + val chargeStep = charge / stepNbr + var chargeIntegrator = 0.0 + var energy = 0.0 + val QperStep = QNominal * life * chargeStep + for (step in 0 until stepNbr) { + val voltage = voltageFunction.getValue(chargeIntegrator) * uNominal + energy += voltage * QperStep + chargeIntegrator += chargeStep + } + return energy + } + val energyMax: Double + get() { + val stepNbr = 50 + val chargeStep = 1.0 / stepNbr + var chargeIntegrator = 0.0 + var energy = 0.0 + val QperStep = QNominal * life * 1.0 / stepNbr + for (step in 0 until stepNbr) { + val voltage = voltageFunction.getValue(chargeIntegrator) * uNominal + energy += voltage * QperStep + chargeIntegrator += chargeStep + } + return energy + } + val u: Double + get() = computeVoltage() + val dischargeCurrent: Double + get() = voltageSource.i +} diff --git a/src/main/java/mods/eln/sim/BatterySlowProcess.java b/src/main/java/mods/eln/sim/BatterySlowProcess.java deleted file mode 100644 index 3cf348079..000000000 --- a/src/main/java/mods/eln/sim/BatterySlowProcess.java +++ /dev/null @@ -1,44 +0,0 @@ -package mods.eln.sim; - -import mods.eln.server.SaveConfig; - -public abstract class BatterySlowProcess implements IProcess { - - BatteryProcess batteryProcess; - - ThermalLoad thermalLoad; - - public double lifeNominalCurrent, lifeNominalLost; - - public BatterySlowProcess(BatteryProcess batteryProcess, ThermalLoad thermalLoad) { - this.batteryProcess = batteryProcess; - this.thermalLoad = thermalLoad; - } - - @Override - public void process(double time) { - double U = batteryProcess.getU(); - if (U < -0.1 * batteryProcess.uNominal) { - destroy(); - return; - } - if (U > getUMax()) { - destroy(); - return; - } - if (SaveConfig.instance.batteryAging) { - double newLife = batteryProcess.life; - double normalisedCurrent = Math.abs(batteryProcess.getDischargeCurrent()) / lifeNominalCurrent; - newLife -= normalisedCurrent * normalisedCurrent * lifeNominalLost * time; - - if (newLife < 0.1) newLife = 0.1; - batteryProcess.changeLife(newLife); - } - } - - public double getUMax() { - return 1.3 * batteryProcess.uNominal; - } - - public abstract void destroy(); -} diff --git a/src/main/java/mods/eln/sim/BatterySlowProcess.kt b/src/main/java/mods/eln/sim/BatterySlowProcess.kt new file mode 100644 index 000000000..f31bcdcfd --- /dev/null +++ b/src/main/java/mods/eln/sim/BatterySlowProcess.kt @@ -0,0 +1,33 @@ +package mods.eln.sim + +import mods.eln.server.SaveConfig + +abstract class BatterySlowProcess(var batteryProcess: BatteryProcess, var thermalLoad: ThermalLoad) : IProcess { + + var lifeNominalCurrent = 0.0 + var lifeNominalLost = 0.0 + + override fun process(time: Double) { + val U = batteryProcess.u + if (U < -0.1 * batteryProcess.uNominal) { + destroy() + return + } + if (U > uMax) { + destroy() + return + } + if (SaveConfig.instance.batteryAging) { + var newLife = batteryProcess.life + val normalisedCurrent = Math.abs(batteryProcess.dischargeCurrent) / lifeNominalCurrent + newLife -= normalisedCurrent * normalisedCurrent * lifeNominalLost * time + if (newLife < 0.1) newLife = 0.1 + batteryProcess.changeLife(newLife) + } + } + + val uMax: Double + get() = 1.3 * batteryProcess.uNominal + + abstract fun destroy() +} diff --git a/src/main/java/mods/eln/sim/mna/component/Resistor.java b/src/main/java/mods/eln/sim/mna/component/Resistor.java index 67e3ff217..75ff41ee0 100644 --- a/src/main/java/mods/eln/sim/mna/component/Resistor.java +++ b/src/main/java/mods/eln/sim/mna/component/Resistor.java @@ -1,5 +1,6 @@ package mods.eln.sim.mna.component; +import mods.eln.misc.Utils; import mods.eln.sim.mna.SubSystem; import mods.eln.sim.mna.misc.MnaConst; import mods.eln.sim.mna.state.State; @@ -47,6 +48,10 @@ public double getU() { } public Resistor setR(double r) { + if (Double.isNaN(r) || Double.isInfinite(r)) { + Utils.println("Error! Resistor cannot be set to " + r ); + return this; + } if (this.r != r) { this.r = r; this.rInv = 1 / r; diff --git a/src/main/java/mods/eln/sim/nbt/NbtBatteryProcess.java b/src/main/java/mods/eln/sim/nbt/NbtBatteryProcess.java deleted file mode 100644 index 1297b9b4f..000000000 --- a/src/main/java/mods/eln/sim/nbt/NbtBatteryProcess.java +++ /dev/null @@ -1,38 +0,0 @@ -package mods.eln.sim.nbt; - -import mods.eln.misc.FunctionTable; -import mods.eln.misc.INBTTReady; -import mods.eln.sim.BatteryProcess; -import mods.eln.sim.ThermalLoad; -import mods.eln.sim.mna.component.VoltageSource; -import mods.eln.sim.mna.state.VoltageState; -import net.minecraft.nbt.NBTTagCompound; - -public class NbtBatteryProcess extends BatteryProcess implements INBTTReady { - - public NbtBatteryProcess(VoltageState positiveLoad, VoltageState negativeLoad, FunctionTable voltageFunction, double IMax, VoltageSource voltageSource) { - super(positiveLoad, negativeLoad, voltageFunction, IMax, voltageSource); - } - - public NbtBatteryProcess(VoltageState positiveLoad, VoltageState negativeLoad, FunctionTable voltageFunction, double IMax, VoltageSource voltageSource, ThermalLoad thermalLoad) { - super(positiveLoad, negativeLoad, voltageFunction, IMax, voltageSource, thermalLoad); - } - - @Override - public void readFromNBT(NBTTagCompound nbttagcompound, String str) { - Q = nbttagcompound.getDouble(str + "NBP" + "Q"); - if (Double.isNaN(Q)) Q = 0; - life = nbttagcompound.getDouble(str + "NBP" + "life"); - if (Double.isNaN(life)) life = 1; - } - - @Override - public void writeToNBT(NBTTagCompound nbttagcompound, String str) { - nbttagcompound.setDouble(str + "NBP" + "Q", Q); - nbttagcompound.setDouble(str + "NBP" + "life", life); - } - - public void setIMax(double iMax) { - this.IMax = iMax; - } -} diff --git a/src/main/java/mods/eln/sim/nbt/NbtBatteryProcess.kt b/src/main/java/mods/eln/sim/nbt/NbtBatteryProcess.kt new file mode 100644 index 000000000..4d2edb63f --- /dev/null +++ b/src/main/java/mods/eln/sim/nbt/NbtBatteryProcess.kt @@ -0,0 +1,31 @@ +package mods.eln.sim.nbt + +import mods.eln.misc.FunctionTable +import mods.eln.misc.INBTTReady +import mods.eln.sim.BatteryProcess +import mods.eln.sim.ThermalLoad +import mods.eln.sim.mna.component.VoltageSource +import mods.eln.sim.mna.state.VoltageState +import net.minecraft.nbt.NBTTagCompound + +class NbtBatteryProcess( + positiveLoad: VoltageState?, + negativeLoad: VoltageState?, + voltageFunction: FunctionTable, + IMax: Double, + voltageSource: VoltageSource, + thermalLoad: ThermalLoad +) : BatteryProcess(positiveLoad, negativeLoad, voltageFunction, IMax, voltageSource, thermalLoad), INBTTReady { + + override fun readFromNBT(nbttagcompound: NBTTagCompound, str: String) { + Q = nbttagcompound.getDouble(str + "NBP" + "Q") + if (!Q.isFinite()) Q = 0.0 + life = nbttagcompound.getDouble(str + "NBP" + "life") + if (!life.isFinite()) life = 1.0 + } + + override fun writeToNBT(nbttagcompound: NBTTagCompound, str: String) { + nbttagcompound.setDouble(str + "NBP" + "Q", Q) + nbttagcompound.setDouble(str + "NBP" + "life", life) + } +} diff --git a/src/main/java/mods/eln/sim/nbt/NbtBatterySlowProcess.java b/src/main/java/mods/eln/sim/nbt/NbtBatterySlowProcess.java deleted file mode 100644 index 0d3276b3d..000000000 --- a/src/main/java/mods/eln/sim/nbt/NbtBatterySlowProcess.java +++ /dev/null @@ -1,22 +0,0 @@ -package mods.eln.sim.nbt; - -import mods.eln.node.NodeBase; -import mods.eln.sim.BatteryProcess; -import mods.eln.sim.BatterySlowProcess; -import mods.eln.sim.ThermalLoad; - -public class NbtBatterySlowProcess extends BatterySlowProcess { - - NodeBase node; - float explosionRadius = 2; - - public NbtBatterySlowProcess(NodeBase node, BatteryProcess batteryProcess, ThermalLoad thermalLoad) { - super(batteryProcess, thermalLoad); - this.node = node; - } - - @Override - public void destroy() { - node.physicalSelfDestruction(explosionRadius); - } -} diff --git a/src/main/java/mods/eln/sim/nbt/NbtBatterySlowProcess.kt b/src/main/java/mods/eln/sim/nbt/NbtBatterySlowProcess.kt new file mode 100644 index 000000000..90a7fd01d --- /dev/null +++ b/src/main/java/mods/eln/sim/nbt/NbtBatterySlowProcess.kt @@ -0,0 +1,19 @@ +package mods.eln.sim.nbt + +import mods.eln.node.NodeBase +import mods.eln.sim.BatteryProcess +import mods.eln.sim.BatterySlowProcess +import mods.eln.sim.ThermalLoad + +class NbtBatterySlowProcess( + var node: NodeBase, + batteryProcess: BatteryProcess, + thermalLoad: ThermalLoad +) : BatterySlowProcess(batteryProcess, thermalLoad) { + + var explosionRadius = 2f + + override fun destroy() { + node.physicalSelfDestruction(explosionRadius) + } +} diff --git a/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherBlock.java b/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherBlock.java deleted file mode 100644 index 22ef8bcbf..000000000 --- a/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherBlock.java +++ /dev/null @@ -1,70 +0,0 @@ -package mods.eln.simplenode.energyconverter; - -import cpw.mods.fml.relauncher.Side; -import cpw.mods.fml.relauncher.SideOnly; -import mods.eln.misc.Direction; -import mods.eln.node.simple.SimpleNode; -import mods.eln.node.simple.SimpleNodeBlock; -import mods.eln.node.simple.SimpleNodeEntity; -import net.minecraft.block.material.Material; -import net.minecraft.client.renderer.texture.IIconRegister; -import net.minecraft.tileentity.TileEntity; -import net.minecraft.util.IIcon; -import net.minecraft.world.IBlockAccess; -import net.minecraft.world.World; - -public class EnergyConverterElnToOtherBlock extends SimpleNodeBlock { - - private EnergyConverterElnToOtherDescriptor descriptor; - - private IIcon elnIcon, eln2Icon; - private IIcon sideIcon; - - public EnergyConverterElnToOtherBlock(EnergyConverterElnToOtherDescriptor descriptor) { - super(Material.packedIce); - this.descriptor = descriptor; - setDescriptor(descriptor); - } - - @Override - public TileEntity createNewTileEntity(World var1, int var2) { - return new EnergyConverterElnToOtherEntity(); - } - - @Override - protected SimpleNode newNode() { - return new EnergyConverterElnToOtherNode(); - } - - @Override - @SideOnly(Side.CLIENT) - public IIcon getIcon(IBlockAccess w, int x, int y, int z, int side) { - SimpleNodeEntity e = (SimpleNodeEntity) w.getTileEntity(x, y, z); - Direction s = Direction.fromIntMinecraftSide(side); - if (e == null) return sideIcon; - if (e.front == null) return sideIcon; - if (e.front == s) return getElnIcon(side); - if (e.front.back() == s) return blockIcon; - return sideIcon; - } - - public IIcon getIcon(int side, int meta) { - Direction s = Direction.fromIntMinecraftSide(side); - if (s == Direction.XP) return getElnIcon(side); - if (s == Direction.XN) return blockIcon; - return sideIcon; - } - - IIcon getElnIcon(int side) { - if (side == 2 || side == 5) return eln2Icon; - return elnIcon; - } - - @SideOnly(Side.CLIENT) - public void registerBlockIcons(IIconRegister register) { - this.blockIcon = register.registerIcon("eln:elntoic2lvu_ic2"); - this.elnIcon = register.registerIcon("eln:elntoic2lvu_eln"); - this.eln2Icon = register.registerIcon("eln:elntoic2lvu_eln2"); - this.sideIcon = register.registerIcon("eln:elntoic2lvu_side"); - } -} diff --git a/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherBlock.kt b/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherBlock.kt new file mode 100644 index 000000000..cd7af3ad5 --- /dev/null +++ b/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherBlock.kt @@ -0,0 +1,49 @@ +package mods.eln.simplenode.energyconverter + +import cpw.mods.fml.relauncher.Side +import cpw.mods.fml.relauncher.SideOnly +import mods.eln.misc.Direction +import mods.eln.node.simple.SimpleNode +import mods.eln.node.simple.SimpleNodeBlock +import mods.eln.node.simple.SimpleNodeEntity +import net.minecraft.block.material.Material +import net.minecraft.client.renderer.texture.IIconRegister +import net.minecraft.tileentity.TileEntity +import net.minecraft.util.IIcon +import net.minecraft.world.IBlockAccess +import net.minecraft.world.World + +class EnergyConverterElnToOtherBlock(private val descriptor: EnergyConverterElnToOtherDescriptor) : SimpleNodeBlock(Material.packedIce) { + + private var sideIcon: IIcon? = null + + override fun createNewTileEntity(var1: World, var2: Int): TileEntity { + return EnergyConverterElnToOtherEntity() + } + + override fun newNode(): SimpleNode { + return EnergyConverterElnToOtherNode() + } + + @SideOnly(Side.CLIENT) + override fun getIcon(w: IBlockAccess, x: Int, y: Int, z: Int, side: Int): IIcon { + return sideIcon!! + } + + override fun getIcon(side: Int, meta: Int): IIcon { + return sideIcon!! + } + + fun getElnIcon(side: Int): IIcon? { + return sideIcon + } + + @SideOnly(Side.CLIENT) + override fun registerBlockIcons(register: IIconRegister) { + sideIcon = register.registerIcon("eln:elntoic2lvu_side") + } + + init { + setDescriptor(descriptor) + } +} diff --git a/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherDescriptor.java b/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherDescriptor.java deleted file mode 100644 index 930a1a1de..000000000 --- a/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherDescriptor.java +++ /dev/null @@ -1,53 +0,0 @@ -package mods.eln.simplenode.energyconverter; - -import mods.eln.misc.DescriptorBase; - -public class EnergyConverterElnToOtherDescriptor extends DescriptorBase { - - public ElnDescriptor eln; - public Ic2Descriptor ic2; - public OcDescriptor oc; - - public EnergyConverterElnToOtherDescriptor(String key, ElnDescriptor eln, Ic2Descriptor ic2, OcDescriptor oc) { - super(key); - this.eln = eln; - this.ic2 = ic2; - this.oc = oc; - } - - void applyTo(EnergyConverterElnToOtherNode node) { - node.inStdVoltage = eln.nominalU; - node.inPowerMax = eln.maxP; - node.energyBufferMax = eln.maxP * 2; - /*node.otherOutMax = otherOutMax; - node.energyBufferMax = otherOutMax / node.conversionRatio * 20;*/ - } - - public static class ElnDescriptor { - public double nominalU; - public double maxP; - - public ElnDescriptor(double nominalU, double maxP) { - this.nominalU = nominalU; - this.maxP = maxP; - } - } - - public static class Ic2Descriptor { - public double outMax; - public int tier; - - public Ic2Descriptor(double outMax, int tier) { - this.outMax = outMax; - this.tier = tier; - } - } - - public static class OcDescriptor { - public double outMax; - - public OcDescriptor(double outMax) { - this.outMax = outMax; - } - } -} diff --git a/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherDescriptor.kt b/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherDescriptor.kt new file mode 100644 index 000000000..bb01819eb --- /dev/null +++ b/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherDescriptor.kt @@ -0,0 +1,14 @@ +package mods.eln.simplenode.energyconverter + +import mods.eln.misc.DescriptorBase + +class EnergyConverterElnToOtherDescriptor( + key: String, + var maxPower: Double +) : DescriptorBase(key) { + + fun applyTo(node: EnergyConverterElnToOtherNode) { + node.energyBufferMax = maxPower * 2 + node.inPowerMax = maxPower + } +} diff --git a/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherEntity.java b/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherEntity.java deleted file mode 100644 index c33bc0694..000000000 --- a/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherEntity.java +++ /dev/null @@ -1,266 +0,0 @@ -package mods.eln.simplenode.energyconverter; - -import cofh.api.energy.IEnergyHandler; -import cpw.mods.fml.common.Optional; -import cpw.mods.fml.relauncher.Side; -import cpw.mods.fml.relauncher.SideOnly; -import ic2.api.energy.tile.IEnergySource; -import li.cil.oc.api.network.Environment; -import li.cil.oc.api.network.Message; -import li.cil.oc.api.network.Node; -import mods.eln.Other; -import mods.eln.misc.Direction; -import mods.eln.node.simple.SimpleNode; -import mods.eln.node.simple.SimpleNodeEntity; -import net.minecraft.client.gui.GuiScreen; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.tileentity.TileEntity; -import net.minecraftforge.common.util.ForgeDirection; - -import java.io.DataInputStream; -import java.io.IOException; - -@Optional.InterfaceList({ - @Optional.Interface(iface = "ic2.api.energy.tile.IEnergySource", modid = Other.modIdIc2), - @Optional.Interface(iface = "cofh.api.energy.IEnergyHandler", modid = Other.modIdTe), - @Optional.Interface(iface = "li.cil.oc.api.network.Environment", modid = Other.modIdOc)}) -public class EnergyConverterElnToOtherEntity extends SimpleNodeEntity implements - IEnergySource, Environment, IEnergyHandler /* ,SidedEnvironment, ISidedBatteryProvider, IPowerEmitter, IPipeConnection */ { - - float inPowerFactor; - boolean hasChanges = false; - public float inPowerMax; - - EnergyConverterElnToOtherFireWallOc oc; - - protected boolean addedToEnet; - - public EnergyConverterElnToOtherEntity() { - if (Other.ocLoaded) - getOc().constructor(); - } - - @Override - public boolean onBlockActivated(EntityPlayer entityPlayer, Direction side, float vx, float vy, float vz) { - return super.onBlockActivated(entityPlayer, side, vx, vy, vz); - } - - @Override - @SideOnly(Side.CLIENT) - public GuiScreen newGuiDraw(Direction side, EntityPlayer player) { - return new EnergyConverterElnToOtherGui(player, this); - } - - @Override - public void serverPublishUnserialize(DataInputStream stream) { - super.serverPublishUnserialize(stream); - try { - inPowerFactor = stream.readFloat(); - inPowerMax = stream.readFloat(); - - hasChanges = true; - } catch (IOException e) { - e.printStackTrace(); - } - } - - @Override - public String getNodeUuid() { - return EnergyConverterElnToOtherNode.getNodeUuidStatic(); - } - - // ********************IC2******************** - - @Optional.Method(modid = Other.modIdIc2) - @Override - public boolean emitsEnergyTo(TileEntity receiver, ForgeDirection direction) { - if (worldObj.isRemote) - return false; - SimpleNode n = getNode(); - if (n == null) - return false; - return n.getFront().back() == Direction.from(direction); - } - - @Optional.Method(modid = Other.modIdIc2) - @Override - public double getOfferedEnergy() { - if (worldObj.isRemote) - return 0; - if (getNode() == null) - return 0; - EnergyConverterElnToOtherNode node = (EnergyConverterElnToOtherNode) getNode(); - double pMax = node.getOtherModOutMax(node.descriptor.ic2.outMax, - Other.getElnToIc2ConversionRatio()); - return pMax; - } - - @Optional.Method(modid = Other.modIdIc2) - @Override - public void drawEnergy(double amount) { - if (worldObj.isRemote) - return; - if (getNode() == null) - return; - - EnergyConverterElnToOtherNode node = (EnergyConverterElnToOtherNode) getNode(); - node.drawEnergy(amount, Other.getElnToIc2ConversionRatio()); - } - - @Optional.Method(modid = Other.modIdIc2) - // @Override - public int getSourceTier() { - EnergyConverterElnToOtherNode node = (EnergyConverterElnToOtherNode) getNode(); - if (node == null) return 0; - return node.descriptor.ic2.tier; - } - - // ***************** OC ********************** - - @Optional.Method(modid = Other.modIdOc) - EnergyConverterElnToOtherFireWallOc getOc() { - if (oc == null) - oc = new EnergyConverterElnToOtherFireWallOc(this); - return oc; - } - - @Override - @Optional.Method(modid = Other.modIdOc) - public Node node() { - return getOc().node; - } - - @Override - @Optional.Method(modid = Other.modIdOc) - public void onConnect(Node node) { - } - - @Override - @Optional.Method(modid = Other.modIdOc) - public void onDisconnect(Node node) { - } - - @Override - @Optional.Method(modid = Other.modIdOc) - public void onMessage(Message message) { - } - - /* - * @Override - * - * @Optional.Method(modid = Other.modIdOc) public Node - * sidedNode(ForgeDirection side) { if(worldObj.isRemote){ if(front.back() - * == Direction.from(side)) return node(); return null; }else{ - * if(getNode().getFront().back() == Direction.from(side)) return node(); - * return null; } } - * - * @Override - * - * @SideOnly(Side.CLIENT) - * - * @Optional.Method(modid = Other.modIdOc) public boolean - * canConnect(ForgeDirection side) { if(front == null) return false; - * if(front.back() == Direction.from(side)) return true; return false; } - */ - - // *************** RF ************** - @Override - @Optional.Method(modid = Other.modIdTe) - public boolean canConnectEnergy(ForgeDirection from) { - // Utils.println("*****canConnectEnergy*****"); - // return true; - if (worldObj.isRemote) - return false; - if (getNode() == null) - return false; - SimpleNode n = getNode(); - return n.getFront().back() == Direction.from(from); - } - - @Override - @Optional.Method(modid = Other.modIdTe) - public int receiveEnergy(ForgeDirection from, int maxReceive, boolean simulate) { - // Utils.println("*****receiveEnergy*****"); - return 0; - } - - @Override - @Optional.Method(modid = Other.modIdTe) - public int extractEnergy(ForgeDirection from, int maxExtract, boolean simulate) { - // Utils.println("*****extractEnergy*****"); - if (worldObj.isRemote) - return 0; - if (getNode() == null) - return 0; - EnergyConverterElnToOtherNode node = (EnergyConverterElnToOtherNode) getNode(); - int extract = Math.max(0, Math.min(maxExtract, (int) node.getOtherModEnergyBuffer(Other.getElnToTeConversionRatio()))); - if (!simulate) - node.drawEnergy(extract, Other.getElnToTeConversionRatio()); - - return extract; - } - - @Override - @Optional.Method(modid = Other.modIdTe) - public int getEnergyStored(ForgeDirection from) { - // Utils.println("*****getEnergyStored*****"); - return 0; - } - - @Override - @Optional.Method(modid = Other.modIdTe) - public int getMaxEnergyStored(ForgeDirection from) { - // Utils.println("*****getMaxEnergyStored*****"); - return 0; - } - - // ***************** Bridges **************** - - @Override - public void updateEntity() { - super.updateEntity(); - if (Other.ic2Loaded) - EnergyConverterElnToOtherFireWallIc2.updateEntity(this); - if (Other.ocLoaded) - getOc().updateEntity(); - if (Other.teLoaded) - EnergyConverterElnToOtherFireWallRf.updateEntity(this); - } - - public void onLoaded() { - if (Other.ic2Loaded) - EnergyConverterElnToOtherFireWallIc2.onLoaded(this); - } - - @Override - public void invalidate() { - super.invalidate(); - if (Other.ic2Loaded) - EnergyConverterElnToOtherFireWallIc2.invalidate(this); - if (Other.ocLoaded) - getOc().invalidate(); - } - - @Override - public void onChunkUnload() { - super.onChunkUnload(); - if (Other.ic2Loaded) - EnergyConverterElnToOtherFireWallIc2.onChunkUnload(this); - if (Other.ocLoaded) - getOc().onChunkUnload(); - } - - @Override - public void readFromNBT(NBTTagCompound nbt) { - super.readFromNBT(nbt); - if (Other.ocLoaded) - getOc().readFromNBT(nbt); - } - - public void writeToNBT(NBTTagCompound nbt) { - super.writeToNBT(nbt); - if (Other.ocLoaded) - getOc().writeToNBT(nbt); - } -} diff --git a/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherEntity.kt b/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherEntity.kt new file mode 100644 index 000000000..1a0fc9e40 --- /dev/null +++ b/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherEntity.kt @@ -0,0 +1,203 @@ +package mods.eln.simplenode.energyconverter + +import cofh.api.energy.IEnergyHandler +import cpw.mods.fml.common.Optional +import cpw.mods.fml.common.Optional.InterfaceList +import cpw.mods.fml.relauncher.Side +import cpw.mods.fml.relauncher.SideOnly +import ic2.api.energy.tile.IEnergySource +import li.cil.oc.api.network.Environment +import li.cil.oc.api.network.Message +import li.cil.oc.api.network.Node +import mods.eln.Other +import mods.eln.misc.Direction +import mods.eln.node.simple.SimpleNodeEntity +import net.minecraft.client.gui.GuiScreen +import net.minecraft.entity.player.EntityPlayer +import net.minecraft.nbt.NBTTagCompound +import net.minecraft.tileentity.TileEntity +import net.minecraftforge.common.util.ForgeDirection +import java.io.DataInputStream +import java.io.IOException +import kotlin.math.min + +@InterfaceList(Optional.Interface(iface = "ic2.api.energy.tile.IEnergySource", modid = Other.modIdIc2), Optional.Interface(iface = "cofh.api.energy.IEnergyHandler", modid = Other.modIdTe), Optional.Interface(iface = "li.cil.oc.api.network.Environment", modid = Other.modIdOc)) +class EnergyConverterElnToOtherEntity : SimpleNodeEntity(), IEnergySource, Environment, IEnergyHandler { + @JvmField + var selectorPower = 0.0 + @JvmField + var hasChanges = false + var ocEnergy: EnergyConverterElnToOtherFireWallOc? = null + @JvmField + var addedToEnet = false + + @SideOnly(Side.CLIENT) + override fun newGuiDraw(side: Direction, player: EntityPlayer): GuiScreen { + return EnergyConverterElnToOtherGui(this) + } + + override fun serverPublishUnserialize(stream: DataInputStream) { + super.serverPublishUnserialize(stream) + try { + selectorPower = stream.readDouble() + hasChanges = true + } catch (e: IOException) { + e.printStackTrace() + } + } + + override fun getNodeUuid(): String { + return EnergyConverterElnToOtherNode.nodeUuidStatic + } + + // ********************IC2******************** + @Optional.Method(modid = Other.modIdIc2) + override fun emitsEnergyTo(receiver: TileEntity, direction: ForgeDirection): Boolean { + if (worldObj.isRemote) return false + node ?: return false + return true + } + + @Optional.Method(modid = Other.modIdIc2) + override fun getOfferedEnergy(): Double { + if (worldObj.isRemote) return 0.0 + if (node == null) return 0.0 + val node = node as EnergyConverterElnToOtherNode + //val offered = node.getOtherModEnergyBuffer(Other.ElnToIc2ConversionRatio) + val offered = node.getOtherModOutMax(node.energyBuffer, Other.getElnToIc2ConversionRatio()) + println("offering: $offered of ${node.energyBuffer}") + return offered + } + + @Optional.Method(modid = Other.modIdIc2) + override fun drawEnergy(amount: Double) { + if (worldObj.isRemote) return + if (node == null) return + val node = node as EnergyConverterElnToOtherNode + val draw = node.drawEnergy(amount, Other.getElnToIc2ConversionRatio()) + println("drawing $draw") + } + + @Optional.Method(modid = Other.modIdIc2) + override fun getSourceTier(): Int { + val node = node as EnergyConverterElnToOtherNode + return 5 + } + + // ***************** OC ********************** + @Optional.Method(modid = Other.modIdOc) + fun getOc(): EnergyConverterElnToOtherFireWallOc { + if (ocEnergy == null) ocEnergy = EnergyConverterElnToOtherFireWallOc(this) + return ocEnergy!! + } + + @Optional.Method(modid = Other.modIdOc) + override fun node(): Node { + return getOc().node!! + } + + @Optional.Method(modid = Other.modIdOc) + override fun onConnect(node: Node) { + } + + @Optional.Method(modid = Other.modIdOc) + override fun onDisconnect(node: Node) { + } + + @Optional.Method(modid = Other.modIdOc) + override fun onMessage(message: Message) { + } + + /* + * @Override + * + * @Optional.Method(modid = Other.modIdOc) public Node + * sidedNode(ForgeDirection side) { if(worldObj.isRemote){ if(front.back() + * == Direction.from(side)) return node(); return null; }else{ + * if(getNode().getFront().back() == Direction.from(side)) return node(); + * return null; } } + * + * @Override + * + * @SideOnly(Side.CLIENT) + * + * @Optional.Method(modid = Other.modIdOc) public boolean + * canConnect(ForgeDirection side) { if(front == null) return false; + * if(front.back() == Direction.from(side)) return true; return false; } + */ + // *************** RF ************** + @Optional.Method(modid = Other.modIdTe) + override fun canConnectEnergy(from: ForgeDirection): Boolean { + // Utils.println("*****canConnectEnergy*****"); + if (worldObj.isRemote) return false + if (node == null) return false + return true + } + + @Optional.Method(modid = Other.modIdTe) + override fun receiveEnergy(from: ForgeDirection, maxReceive: Int, simulate: Boolean): Int { + // Utils.println("*****receiveEnergy*****"); + return 0 + } + + @Optional.Method(modid = Other.modIdTe) + override fun extractEnergy(from: ForgeDirection, maxExtract: Int, simulate: Boolean): Int { + // Utils.println("*****extractEnergy*****"); + if (worldObj.isRemote) return 0 + if (node == null) return 0 + val node = node as EnergyConverterElnToOtherNode + val extract = Math.max(0, Math.min(maxExtract, node.getOtherModEnergyBuffer(Other.getElnToTeConversionRatio()).toInt())) + if (!simulate) node.drawEnergy(extract.toDouble(), Other.getElnToTeConversionRatio()) + return extract + } + + @Optional.Method(modid = Other.modIdTe) + override fun getEnergyStored(from: ForgeDirection): Int { + // Utils.println("*****getEnergyStored*****"); + return 0 + } + + @Optional.Method(modid = Other.modIdTe) + override fun getMaxEnergyStored(from: ForgeDirection): Int { + // Utils.println("*****getMaxEnergyStored*****"); + return 0 + } + + // ***************** Bridges **************** + override fun updateEntity() { + super.updateEntity() + if (Other.ic2Loaded) EnergyConverterElnToOtherFireWallIc2.updateEntity(this) + if (Other.ocLoaded) getOc().updateEntity() + if (Other.teLoaded) EnergyConverterElnToOtherFireWallRf.updateEntity(this) + } + + fun onLoaded() { + if (Other.ic2Loaded) EnergyConverterElnToOtherFireWallIc2.onLoaded(this) + } + + override fun invalidate() { + super.invalidate() + if (Other.ic2Loaded) EnergyConverterElnToOtherFireWallIc2.invalidate(this) + if (Other.ocLoaded) getOc().invalidate() + } + + override fun onChunkUnload() { + super.onChunkUnload() + if (Other.ic2Loaded) EnergyConverterElnToOtherFireWallIc2.onChunkUnload(this) + if (Other.ocLoaded) getOc().onChunkUnload() + } + + override fun readFromNBT(nbt: NBTTagCompound) { + super.readFromNBT(nbt) + if (Other.ocLoaded) getOc().readFromNBT(nbt) + } + + override fun writeToNBT(nbt: NBTTagCompound) { + super.writeToNBT(nbt) + if (Other.ocLoaded) getOc().writeToNBT(nbt) + } + + init { + if (Other.ocLoaded) getOc().constructor() + } +} diff --git a/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherFireWallIc2.java b/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherFireWallIc2.java deleted file mode 100644 index 476bc5c1d..000000000 --- a/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherFireWallIc2.java +++ /dev/null @@ -1,53 +0,0 @@ -package mods.eln.simplenode.energyconverter; - -import cpw.mods.fml.common.FMLCommonHandler; -import ic2.api.energy.event.EnergyTileLoadEvent; -import ic2.api.energy.event.EnergyTileUnloadEvent; -import ic2.api.info.Info; -import net.minecraftforge.common.MinecraftForge; - -public class EnergyConverterElnToOtherFireWallIc2 { - - /** - * Forward for the base TileEntity's updateEntity(), used for creating the energy net link. - * Either updateEntity or onLoaded have to be used. - */ - public static void updateEntity(EnergyConverterElnToOtherEntity e) { - if (!e.addedToEnet) onLoaded(e); - } - - /** - * Notification that the base TileEntity finished loading, for advanced uses. - * Either updateEntity or onLoaded have to be used. - */ - public static void onLoaded(EnergyConverterElnToOtherEntity e) { - if (!e.addedToEnet && - !FMLCommonHandler.instance().getEffectiveSide().isClient() && - Info.isIc2Available()) { - - MinecraftForge.EVENT_BUS.post(new EnergyTileLoadEvent(e)); - - e.addedToEnet = true; - } - } - - /** - * Forward for the base TileEntity's invalidate(), used for destroying the energy net link. - * Both invalidate and onChunkUnload have to be used. - */ - public static void invalidate(EnergyConverterElnToOtherEntity e) { - e.onChunkUnload(); - } - - /** - * Forward for the base TileEntity's onChunkUnload(), used for destroying the energy net link. - * Both invalidate and onChunkUnload have to be used. - */ - public static void onChunkUnload(EnergyConverterElnToOtherEntity e) { - if (e.addedToEnet && Info.isIc2Available()) { - MinecraftForge.EVENT_BUS.post(new EnergyTileUnloadEvent(e)); - - e.addedToEnet = false; - } - } -} diff --git a/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherFireWallIc2.kt b/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherFireWallIc2.kt new file mode 100644 index 000000000..00d22e376 --- /dev/null +++ b/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherFireWallIc2.kt @@ -0,0 +1,49 @@ +package mods.eln.simplenode.energyconverter + +import cpw.mods.fml.common.FMLCommonHandler +import ic2.api.energy.event.EnergyTileLoadEvent +import ic2.api.energy.event.EnergyTileUnloadEvent +import ic2.api.info.Info +import net.minecraftforge.common.MinecraftForge + +object EnergyConverterElnToOtherFireWallIc2 { + /** + * Forward for the base TileEntity's updateEntity(), used for creating the energy net link. + * Either updateEntity or onLoaded have to be used. + */ + fun updateEntity(e: EnergyConverterElnToOtherEntity) { + if (!e.addedToEnet) onLoaded(e) + } + + /** + * Notification that the base TileEntity finished loading, for advanced uses. + * Either updateEntity or onLoaded have to be used. + */ + fun onLoaded(e: EnergyConverterElnToOtherEntity) { + if (!e.addedToEnet && + !FMLCommonHandler.instance().effectiveSide.isClient && + Info.isIc2Available()) { + MinecraftForge.EVENT_BUS.post(EnergyTileLoadEvent(e)) + e.addedToEnet = true + } + } + + /** + * Forward for the base TileEntity's invalidate(), used for destroying the energy net link. + * Both invalidate and onChunkUnload have to be used. + */ + fun invalidate(e: EnergyConverterElnToOtherEntity) { + e.onChunkUnload() + } + + /** + * Forward for the base TileEntity's onChunkUnload(), used for destroying the energy net link. + * Both invalidate and onChunkUnload have to be used. + */ + fun onChunkUnload(e: EnergyConverterElnToOtherEntity) { + if (e.addedToEnet && Info.isIc2Available()) { + MinecraftForge.EVENT_BUS.post(EnergyTileUnloadEvent(e)) + e.addedToEnet = false + } + } +} diff --git a/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherFireWallOc.java b/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherFireWallOc.java deleted file mode 100644 index 1b2f7e5bd..000000000 --- a/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherFireWallOc.java +++ /dev/null @@ -1,88 +0,0 @@ -package mods.eln.simplenode.energyconverter; - -import li.cil.oc.api.Network; -import li.cil.oc.api.network.Connector; -import li.cil.oc.api.network.Node; -import li.cil.oc.api.network.Visibility; -import mods.eln.Other; -import mods.eln.misc.Utils; -import net.minecraft.nbt.NBTTagCompound; - -public class EnergyConverterElnToOtherFireWallOc { - - EnergyConverterElnToOtherEntity e; - Node node; - - protected boolean addedToNetwork = false; - - public EnergyConverterElnToOtherFireWallOc(EnergyConverterElnToOtherEntity e) { - this.e = e; - } - - public void updateEntity() { - // On the first update, try to add our node to nearby networks. We do - // this in the update logic, not in validate() because we need to access - // neighboring tile entities, which isn't possible in validate(). - // We could alternatively check node != null && node.network() == null, - // but this has somewhat better performance, and makes it clearer. - if (e.getWorldObj().isRemote) return; - if (!addedToNetwork) { - addedToNetwork = true; - Network.joinOrCreateNetwork(e); - } else { - if (node != null) { - if (e.getNode() == null) return; - Connector c = ((Connector) node); - EnergyConverterElnToOtherNode node = (EnergyConverterElnToOtherNode) e.getNode(); - double eMax = node.getOtherModEnergyBuffer(Other.getElnToOcConversionRatio()); - eMax = Math.min(Math.min(eMax, c.globalBufferSize() - c.globalBuffer()), node.descriptor.oc.outMax); - if (c.tryChangeBuffer(eMax)) { - node.drawEnergy(eMax, Other.getElnToOcConversionRatio()); - } - } - } - } - - public void onChunkUnload() { - // Make sure to remove the node from its network when its environment, - // meaning this tile entity, gets unloaded. - if (e.getWorldObj().isRemote) return; - if (node != null) node.remove(); - } - - public void invalidate() { - // Make sure to remove the node from its network when its environment, - // meaning this tile entity, gets unloaded. - if (e.getWorldObj().isRemote) return; - if (node != null) node.remove(); - } - - // ----------------------------------------------------------------------- // - public void readFromNBT(final NBTTagCompound nbt) { - // The host check may be superfluous for you. It's just there to allow - // some special cases, where getNode() returns some node managed by - // some other instance (for example when you have multiple internal - // nodes in this tile entity). - if (node != null && node.host() == this) { - // This restores the node's address, which is required for networks - // to continue working without interruption across loads. If the - // node is a power connector this is also required to restore the - // internal energy buffer of the node. - node.load(nbt.getCompoundTag("oc:node")); - } - } - - public void writeToNBT(final NBTTagCompound nbt) { - // See readFromNBT() regarding host check. - if (node != null && node.host() == this) { - final NBTTagCompound nodeNbt = new NBTTagCompound(); - node.save(nodeNbt); - Utils.newNbtTagCompund(nodeNbt, "oc:node"); - } - } - - public void constructor() { - node = li.cil.oc.api.Network.newNode(e, Visibility.None).withConnector().create(); - Utils.println("******** C " + node); - } -} diff --git a/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherFireWallOc.kt b/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherFireWallOc.kt new file mode 100644 index 000000000..79f34fc7e --- /dev/null +++ b/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherFireWallOc.kt @@ -0,0 +1,83 @@ +package mods.eln.simplenode.energyconverter + +import li.cil.oc.api.Network +import li.cil.oc.api.network.Connector +import li.cil.oc.api.network.Node +import li.cil.oc.api.network.Visibility +import mods.eln.Other +import mods.eln.misc.Utils +import net.minecraft.nbt.NBTTagCompound + +class EnergyConverterElnToOtherFireWallOc(var e: EnergyConverterElnToOtherEntity) { + + var node: Node? = null + private var addedToNetwork = false + + fun updateEntity() { + // On the first update, try to add our node to nearby networks. We do + // this in the update logic, not in validate() because we need to access + // neighboring tile entities, which isn't possible in validate(). + // We could alternatively check node != null && node.network() == null, + // but this has somewhat better performance, and makes it clearer. + if (e.worldObj.isRemote) return + if (!addedToNetwork) { + addedToNetwork = true + Network.joinOrCreateNetwork(e) + } else { + if (node != null) { + if (e.node == null) return + val c = node as Connector + val node = e.node as EnergyConverterElnToOtherNode + var eMax = node.getOtherModEnergyBuffer(Other.getElnToOcConversionRatio()) + eMax = Math.min(Math.min(eMax, c.globalBufferSize() - c.globalBuffer()), node.descriptor!!.maxPower) + if (c.tryChangeBuffer(eMax)) { + node.drawEnergy(eMax, Other.getElnToOcConversionRatio()) + } + } + } + } + + fun onChunkUnload() { + // Make sure to remove the node from its network when its environment, + // meaning this tile entity, gets unloaded. + if (e.worldObj.isRemote) return + if (node != null) node!!.remove() + } + + fun invalidate() { + // Make sure to remove the node from its network when its environment, + // meaning this tile entity, gets unloaded. + if (e.worldObj.isRemote) return + if (node != null) node!!.remove() + } + + // ----------------------------------------------------------------------- // + fun readFromNBT(nbt: NBTTagCompound) { + // Node check removed because it was invalid code? Doesn't seem important here - jrddunbr + // The host check may be superfluous for you. It's just there to allow + // some special cases, where getNode() returns some node managed by + // some other instance (for example when you have multiple internal + // nodes in this tile entity). + if (node != null) { + // This restores the node's address, which is required for networks + // to continue working without interruption across loads. If the + // node is a power connector this is also required to restore the + // internal energy buffer of the node. + node!!.load(nbt.getCompoundTag("oc:node")) + } + } + + fun writeToNBT(nbt: NBTTagCompound?) { + // See readFromNBT() regarding host check. + if (node != null) { + val nodeNbt = NBTTagCompound() + node!!.save(nodeNbt) + Utils.newNbtTagCompund(nodeNbt, "oc:node") + } + } + + fun constructor() { + node = Network.newNode(e, Visibility.None).withConnector().create() + Utils.println("******** C $node") + } +} diff --git a/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherFireWallRf.java b/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherFireWallRf.java deleted file mode 100644 index fe01f578f..000000000 --- a/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherFireWallRf.java +++ /dev/null @@ -1,21 +0,0 @@ -package mods.eln.simplenode.energyconverter; - -import cofh.api.energy.IEnergyHandler; -import mods.eln.Other; -import net.minecraft.tileentity.TileEntity; - -public class EnergyConverterElnToOtherFireWallRf { - public static void updateEntity(EnergyConverterElnToOtherEntity e) { - if (e.getWorldObj().isRemote) return; - if (e.getNode() == null) return; - - EnergyConverterElnToOtherNode node = (EnergyConverterElnToOtherNode) e.getNode(); - TileEntity tileEntity = node.getFront().getInverse().applyToTileEntity(e); - - if (tileEntity == null || !(tileEntity instanceof IEnergyHandler)) return; - IEnergyHandler energyHandler = (IEnergyHandler) tileEntity; - - double pMax = node.getOtherModEnergyBuffer(Other.getElnToTeConversionRatio()); - node.drawEnergy(energyHandler.receiveEnergy(node.getFront().toForge(), (int) pMax, false), Other.getElnToTeConversionRatio()); - } -} diff --git a/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherFireWallRf.kt b/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherFireWallRf.kt new file mode 100644 index 000000000..d9fd52ccc --- /dev/null +++ b/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherFireWallRf.kt @@ -0,0 +1,27 @@ +package mods.eln.simplenode.energyconverter + +import cofh.api.energy.IEnergyHandler +import mods.eln.Other +import mods.eln.misc.Direction +import mods.eln.node.simple.SimpleNode + +object EnergyConverterElnToOtherFireWallRf { + + fun updateEntity(e: EnergyConverterElnToOtherEntity) { + if (e.worldObj.isRemote) return + if (e.node == null) return + val node = e.node as EnergyConverterElnToOtherNode + + val energySinkList: List> = Direction.all + .mapNotNull { Pair(it.applyToTileEntity(e), it) } + .filter{ it.first is IEnergyHandler } + .map { Pair(it.first as IEnergyHandler, it.second) } + if (energySinkList.isEmpty()) return + val pMax = node.getOtherModEnergyBuffer(Other.getElnToTeConversionRatio()) + + val energyUsed = energySinkList.map { + it.first.receiveEnergy(it.second.toForge(), pMax.toInt() / energySinkList.size, false).toDouble() + }.sum() + node.drawEnergy(energyUsed, Other.getElnToTeConversionRatio()) + } +} diff --git a/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherGui.java b/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherGui.java deleted file mode 100644 index e06581b39..000000000 --- a/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherGui.java +++ /dev/null @@ -1,56 +0,0 @@ -package mods.eln.simplenode.energyconverter; - -import mods.eln.gui.GuiHelper; -import mods.eln.gui.GuiScreenEln; -import mods.eln.gui.GuiVerticalTrackBar; -import mods.eln.gui.IGuiObject; -import net.minecraft.entity.player.EntityPlayer; - -import static mods.eln.i18n.I18N.tr; - -public class EnergyConverterElnToOtherGui extends GuiScreenEln { - - EnergyConverterElnToOtherEntity render; - GuiVerticalTrackBar voltage; - - public EnergyConverterElnToOtherGui(EntityPlayer player, EnergyConverterElnToOtherEntity render) { - this.render = render; - } - - @Override - public void initGui() { - super.initGui(); - - voltage = newGuiVerticalTrackBar(6, 6 + 2, 20, 50); - voltage.setStepIdMax((int) 100); - voltage.setEnable(true); - voltage.setRange(0f, 1f); - - syncVoltage(); - } - - public void syncVoltage() { - voltage.setValue(render.inPowerFactor); - render.hasChanges = false; - } - - @Override - public void guiObjectEvent(IGuiObject object) { - super.guiObjectEvent(object); - if (object == voltage) { - render.sender.clientSendFloat(EnergyConverterElnToOtherNode.setInPowerFactor, voltage.getValue()); - } - } - - @Override - protected void preDraw(float f, int x, int y) { - super.preDraw(f, x, y); - if (render.hasChanges) syncVoltage(); - voltage.setComment(0, tr("Input power is limited to %1$W", (int) (voltage.getValue() * render.inPowerMax))); - } - - @Override - protected GuiHelper newHelper() { - return new GuiHelper(this, 12 + 20, 12 + 50 + 4); - } -} diff --git a/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherGui.kt b/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherGui.kt new file mode 100644 index 000000000..317355271 --- /dev/null +++ b/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherGui.kt @@ -0,0 +1,111 @@ +package mods.eln.simplenode.energyconverter + +import mods.eln.Eln +import mods.eln.gui.GuiButtonEln +import mods.eln.gui.GuiHelper +import mods.eln.gui.GuiScreenEln +import mods.eln.gui.GuiTextFieldEln +import mods.eln.gui.GuiVerticalTrackBar +import mods.eln.gui.IGuiObject +import mods.eln.i18n.I18N +import java.lang.Math.pow +import java.text.DecimalFormat +import kotlin.math.log +import kotlin.math.max +import kotlin.math.pow +import kotlin.math.roundToInt + +class EnergyConverterElnToOtherGui(var render: EnergyConverterElnToOtherEntity) : GuiScreenEln() { + + var powerSelector: GuiVerticalTrackBar? = null + //var ic2tier1: GuiButtonEln? = null + //var ic2tier2: GuiButtonEln? = null + //var ic2tier3: GuiButtonEln? = null + //var ic2tier4: GuiButtonEln? = null + //var ic2tier5: GuiButtonEln? = null + var powerEntry: GuiTextFieldEln? = null + + val maxPower = Eln.instance.ELN_CONVERTER_MAX_POWER + + //var ic2tierList: List? = null + + override fun initGui() { + super.initGui() + powerSelector = newGuiVerticalTrackBar(6, 6 + 2, 20, 100) + powerSelector!!.setStepIdMax(100) + powerSelector!!.setEnable(true) + powerSelector!!.setRange(0f, 1f) + //ic2tier1 = newGuiButton(35, 6, 40,"IC2 T1") + //ic2tier2 = newGuiButton(35, 28, 40,"IC2 T2") + //ic2tier3 = newGuiButton(35, 50, 40,"IC2 T3") + //ic2tier4 = newGuiButton(35, 72, 40,"IC2 T4") + //ic2tier5 = newGuiButton(35, 94, 40, "IC2 T5") + //ic2tierList = listOfNotNull(ic2tier1, ic2tier2, ic2tier3, ic2tier4, ic2tier5) + //ic2tier1!!.enabled = true + //ic2tier1!!.setComment(0, "Set output to 32 EU/t max") + //ic2tier2!!.enabled = true + //ic2tier2!!.setComment(0, "Set output to 128 EU/t max") + //ic2tier3!!.enabled = true + //ic2tier3!!.setComment(0, "Set output to 512 EU/t max") + //ic2tier4!!.enabled = true + //ic2tier4!!.setComment(0, "Set output to 2048 EU/t max") + //ic2tier5!!.enabled = true + //ic2tier5!!.setComment(0, "Set output to 8192 EU/t max") + powerEntry = newGuiTextField(6, 112, 55) + powerEntry!!.enabled + powerEntry!!.setComment(listOf("Select the maximum export power").toTypedArray()) + syncVoltage() + } + + fun syncVoltage() { + powerSelector!!.value = selectedPowerToSlider(render.selectorPower) + powerEntry!!.text = render.selectorPower.toInt().toString() + render.hasChanges = false + } + + fun selectedPowerToSlider(selectedPower: Double): Float { + return log(selectedPower / 100 + 1, (maxPower / 100) + 1).toFloat() + //return log(selectedPower + 1, Eln.instance.ELN_CONVERTER_MAX_POWER + 1).toFloat() + //return log(selectedPower, Eln.instance.ELN_CONVERTER_MAX_POWER).toFloat() + } + + fun sliderToSelectedPower(slider: Float): Double { + return 100 * (pow((maxPower / 100) + 1, slider.toDouble()) - 1) + //return pow(Eln.instance.ELN_CONVERTER_MAX_POWER + 1, slider.toDouble()) - 1 + //return Eln.instance.ELN_CONVERTER_MAX_POWER.pow(slider.toDouble()) + } + + override fun guiObjectEvent(guiObject: IGuiObject) { + super.guiObjectEvent(guiObject) + when { + guiObject === powerSelector -> { + render.selectorPower = sliderToSelectedPower(powerSelector!!.value) + render.sender.clientSendDouble(NetworkType.SET_POWER.id, render.selectorPower) + } + guiObject === powerEntry -> { + render.selectorPower = powerEntry!!.text.toDoubleOrNull()?: render.selectorPower + render.sender.clientSendDouble(NetworkType.SET_POWER.id, render.selectorPower) + } + //guiObject === ic2tier1 -> + // render.sender.clientSendInt(NetworkType.SET_IC2_TIER.id, 1) + //guiObject === ic2tier2 -> + // render.sender.clientSendInt(NetworkType.SET_IC2_TIER.id, 2) + //guiObject === ic2tier3 -> + // render.sender.clientSendInt(NetworkType.SET_IC2_TIER.id, 3) + //guiObject === ic2tier4 -> + // render.sender.clientSendInt(NetworkType.SET_IC2_TIER.id, 4) + //guiObject === ic2tier5 -> + // render.sender.clientSendInt(NetworkType.SET_IC2_TIER.id, 5) + } + } + + override fun preDraw(f: Float, x: Int, y: Int) { + super.preDraw(f, x, y) + if (render.hasChanges) syncVoltage() + powerSelector!!.setComment(0, I18N.tr("Input power is limited to %1\$W", sliderToSelectedPower(powerSelector!!.value).toInt())) + } + + override fun newHelper(): GuiHelper { + return GuiHelper(this, 67, 140) + } +} diff --git a/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherNode.java b/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherNode.java deleted file mode 100644 index 41a6c836b..000000000 --- a/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherNode.java +++ /dev/null @@ -1,171 +0,0 @@ -package mods.eln.simplenode.energyconverter; - -import mods.eln.Eln; -import mods.eln.misc.Direction; -import mods.eln.misc.LRDU; -import mods.eln.node.simple.SimpleNode; -import mods.eln.sim.ElectricalLoad; -import mods.eln.sim.IProcess; -import mods.eln.sim.ThermalLoad; -import mods.eln.sim.nbt.NbtElectricalLoad; -import mods.eln.sim.nbt.NbtResistor; -import mods.eln.sim.process.destruct.VoltageStateWatchDog; -import mods.eln.sim.process.destruct.WorldExplosion; -import net.minecraft.entity.player.EntityPlayerMP; -import net.minecraft.nbt.NBTTagCompound; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; - -public class EnergyConverterElnToOtherNode extends SimpleNode { - - EnergyConverterElnToOtherDescriptor descriptor; - - NbtElectricalLoad load = new NbtElectricalLoad("load"); - NbtResistor powerInResistor = new NbtResistor("powerInResistor", load, null); - ElectricalProcess electricalProcess = new ElectricalProcess(); - VoltageStateWatchDog watchdog = new VoltageStateWatchDog(); - - public double energyBuffer = 0; - public double energyBufferMax; - public double inStdVoltage; - public double inPowerMax; - //public double otherOutMax = 32; - - public double inPowerFactor = 0.5; - - public static final byte setInPowerFactor = 1; - - @Override - protected void setDescriptorKey(String key) { - super.setDescriptorKey(key); - descriptor = (EnergyConverterElnToOtherDescriptor) getDescriptor(); - } - - @Override - public int getSideConnectionMask(Direction directionA, LRDU lrduA) { - if (directionA == getFront()) return maskElectricalPower; - return 0; - } - - @Override - public ThermalLoad getThermalLoad(Direction directionA, LRDU lrduA, int mask) { - return null; - } - - @Override - public ElectricalLoad getElectricalLoad(Direction directionB, LRDU lrduB, int mask) { - return load; - } - - @Override - public void initialize() { - electricalLoadList.add(load); - electricalComponentList.add(powerInResistor); - electricalProcessList.add(electricalProcess); - slowProcessList.add(watchdog); - - Eln.applySmallRs(load); - - load.setAsPrivate(); - - descriptor.applyTo(this); - - WorldExplosion exp = new WorldExplosion(this).machineExplosion(); - watchdog.set(load).setUNominal(inStdVoltage).set(exp); - - connect(); - } - - class ElectricalProcess implements IProcess { - double timeout = 0; - - @Override - public void process(double time) { - energyBuffer += powerInResistor.getP() * time; - timeout -= time; - if (timeout < 0) { - timeout = 0.05; - double energyMiss = energyBufferMax - energyBuffer; - if (energyMiss <= 0) { - powerInResistor.highImpedance(); - } else { - double factor = Math.min(1, energyMiss / energyBufferMax * 2); - if (factor < 0.005) factor = 0; - double inP = factor * inPowerMax * inPowerFactor; - powerInResistor.setR(inStdVoltage * inStdVoltage / inP); - } - } - } - } - - public double getOtherModEnergyBuffer(double conversionRatio) { - return energyBuffer * conversionRatio; - } - - public void drawEnergy(double otherModEnergy, double conversionRatio) { - energyBuffer -= otherModEnergy / conversionRatio; - } - - public double getOtherModOutMax(double otherOutMax, double conversionRatio) { - return Math.min(getOtherModEnergyBuffer(conversionRatio), otherOutMax); - } - - @Override - public void writeToNBT(NBTTagCompound nbt) { - super.writeToNBT(nbt); - nbt.setDouble("energyBuffer", energyBuffer); - nbt.setDouble("inPowerFactor", inPowerFactor); - } - - @Override - public void readFromNBT(NBTTagCompound nbt) { - super.readFromNBT(nbt); - energyBuffer = nbt.getDouble("energyBuffer"); - inPowerFactor = nbt.getDouble("inPowerFactor"); - } - - @Override - public boolean hasGui(Direction side) { - return true; - } - - @Override - public void publishSerialize(DataOutputStream stream) { - super.publishSerialize(stream); - - try { - stream.writeFloat((float) inPowerFactor); - stream.writeFloat((float) inPowerMax); - } catch (IOException e) { - e.printStackTrace(); - } - } - - @Override - public void networkUnserialize(DataInputStream stream, EntityPlayerMP player) { - try { - switch (stream.readByte()) { - case setInPowerFactor: - inPowerFactor = stream.readFloat(); - needPublish(); - break; - - default: - break; - } - } catch (IOException e) { - e.printStackTrace(); - } - } - - @Override - public String getNodeUuid() { - return getNodeUuidStatic(); - } - - public static String getNodeUuidStatic() { - return "ElnToOther"; - } -} diff --git a/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherNode.kt b/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherNode.kt new file mode 100644 index 000000000..7e7d2488a --- /dev/null +++ b/src/main/java/mods/eln/simplenode/energyconverter/EnergyConverterElnToOtherNode.kt @@ -0,0 +1,171 @@ +package mods.eln.simplenode.energyconverter + +import mods.eln.Eln +import mods.eln.Other +import mods.eln.misc.Direction +import mods.eln.misc.LRDU +import mods.eln.node.simple.SimpleNode +import mods.eln.sim.ElectricalLoad +import mods.eln.sim.IProcess +import mods.eln.sim.ThermalLoad +import mods.eln.sim.mna.misc.MnaConst +import mods.eln.sim.nbt.NbtElectricalLoad +import mods.eln.sim.nbt.NbtResistor +import net.minecraft.client.renderer.texture.ITickable +import net.minecraft.entity.player.EntityPlayerMP +import net.minecraft.nbt.NBTTagCompound +import java.io.DataInputStream +import java.io.DataOutputStream +import java.io.IOException +import kotlin.math.max + +class EnergyConverterElnToOtherNode : SimpleNode() { + var descriptor: EnergyConverterElnToOtherDescriptor? = null + var load = NbtElectricalLoad("load") + var powerInResistor = NbtResistor("powerInResistor", load, null) + var electricalProcess = ElectricalProcess() + var energyBuffer = 0.0 + var energyBufferMax = 0.0 + var inStdVoltage = 0.0 + var inPowerMax = 0.0 + var selectedPower = 0.0 + var ic2tier = 1 + + init { + powerInResistor.r = MnaConst.highImpedance + } + + override fun setDescriptorKey(key: String) { + super.setDescriptorKey(key) + descriptor = getDescriptor() as EnergyConverterElnToOtherDescriptor + } + + override fun getSideConnectionMask(directionA: Direction, lrduA: LRDU): Int { + return maskElectricalPower + } + + override fun getThermalLoad(directionA: Direction, lrduA: LRDU, mask: Int): ThermalLoad? { + return null + } + + override fun getElectricalLoad(directionB: Direction, lrduB: LRDU, mask: Int): ElectricalLoad { + return load + } + + override fun initialize() { + electricalLoadList.add(load) + electricalComponentList.add(powerInResistor) + electricalProcessList.add(electricalProcess) + Eln.applySmallRs(load) + load.setAsPrivate() + descriptor!!.applyTo(this) + connect() + } + + inner class ElectricalProcess : IProcess { + var timeout = 0.0 + override fun process(time: Double) { + var power = powerInResistor.p + if (!power.isFinite()) power = 0.0 + energyBuffer += power * time + timeout -= time + if (timeout < 0) { + timeout = 0.05 + val energyMiss = energyBufferMax - energyBuffer + if (energyMiss <= 0) { + powerInResistor.highImpedance() + } else { + val inP = selectedPower + if (inP <= 0.0) { + powerInResistor.r = MnaConst.highImpedance + } else { + powerInResistor.r = max(Eln.getSmallRs(), load.u * load.u / inP) + } + } + } + println("Energy buffer: $energyBuffer/$energyBufferMax") + } + } + + fun getOtherModEnergyBuffer(conversionRatio: Double): Double { + return energyBuffer * conversionRatio + } + + fun drawEnergy(otherModEnergy: Double, conversionRatio: Double): Double { + val drawEnergy = otherModEnergy / conversionRatio + energyBuffer -= drawEnergy + return drawEnergy + } + + fun getOtherModOutMax(otherOutMax: Double, conversionRatio: Double): Double { + return Math.min(getOtherModEnergyBuffer(conversionRatio), otherOutMax) + } + + override fun writeToNBT(nbt: NBTTagCompound) { + super.writeToNBT(nbt) + nbt.setDouble("energyBuffer", energyBuffer) + nbt.setDouble("selectedPower", selectedPower) + } + + override fun readFromNBT(nbt: NBTTagCompound) { + super.readFromNBT(nbt) + energyBuffer = nbt.getDouble("energyBuffer") + selectedPower = nbt.getDouble("selectedPower") + } + + override fun hasGui(side: Direction): Boolean { + return true + } + + override fun publishSerialize(stream: DataOutputStream) { + super.publishSerialize(stream) + try { + stream.writeDouble(selectedPower) + } catch (e: IOException) { + e.printStackTrace() + } + } + + override fun networkUnserialize(stream: DataInputStream, player: EntityPlayerMP) { + try { + when (stream.readByte()) { + NetworkType.SET_POWER.id -> { + selectedPower = stream.readDouble() + needPublish() + } + NetworkType.SET_IC2_TIER.id -> { + val tier = stream.readInt() + if (tier in 1..5) { + val tierLimit = when(tier) { + 1 -> 32 + 2 -> 128 + 3 -> 512 + 4 -> 2048 + 5 -> 8192 + else -> 32 + } + selectedPower = tierLimit * (1 / Other.getElnToIc2ConversionRatio()) + needPublish() + } + } + else -> {} + } + } catch (e: IOException) { + e.printStackTrace() + } + } + + override fun getNodeUuid(): String { + return nodeUuidStatic + } + + companion object { + @JvmStatic + val nodeUuidStatic = "ElnToOther" + } +} + +enum class NetworkType(val id: Byte) { + SET_POWER(1), + SET_IC2_TIER(2) +} diff --git a/src/main/java/mods/eln/transparentnode/battery/BatteryContainer.java b/src/main/java/mods/eln/transparentnode/battery/BatteryContainer.java deleted file mode 100644 index 1e7a038fa..000000000 --- a/src/main/java/mods/eln/transparentnode/battery/BatteryContainer.java +++ /dev/null @@ -1,41 +0,0 @@ -package mods.eln.transparentnode.battery; - -import mods.eln.generic.GenericItemUsingDamageSlot; -import mods.eln.gui.ISlotSkin.SlotSkin; -import mods.eln.item.OverHeatingProtectionDescriptor; -import mods.eln.item.OverVoltageProtectionDescriptor; -import mods.eln.misc.BasicContainer; -import mods.eln.node.INodeContainer; -import mods.eln.node.NodeBase; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.inventory.IInventory; -import net.minecraft.inventory.Slot; - -import static mods.eln.i18n.I18N.tr; - -public class BatteryContainer extends BasicContainer implements INodeContainer { - - NodeBase node; - - public BatteryContainer(NodeBase node, EntityPlayer player, IInventory inventory) { - super(player, inventory, new Slot[]{ - new GenericItemUsingDamageSlot(inventory, 0, 130, 40, 1, - OverVoltageProtectionDescriptor.class, SlotSkin.medium, - new String[]{tr("Overvoltage protection")}), - new GenericItemUsingDamageSlot(inventory, 1, 130, 60, 1, - OverHeatingProtectionDescriptor.class, SlotSkin.medium, - new String[]{tr("Overheating protection")}), - }); - this.node = node; - } - - @Override - public NodeBase getNode() { - return node; - } - - @Override - public int getRefreshRateDivider() { - return 1; - } -} diff --git a/src/main/java/mods/eln/transparentnode/battery/BatteryDescriptor.java b/src/main/java/mods/eln/transparentnode/battery/BatteryDescriptor.java deleted file mode 100644 index 90214f4ba..000000000 --- a/src/main/java/mods/eln/transparentnode/battery/BatteryDescriptor.java +++ /dev/null @@ -1,290 +0,0 @@ -package mods.eln.transparentnode.battery; - -import mods.eln.Eln; -import mods.eln.misc.FunctionTable; -import mods.eln.misc.Obj3D; -import mods.eln.misc.Obj3D.Obj3DPart; -import mods.eln.misc.Utils; -import mods.eln.misc.VoltageLevelColor; -import mods.eln.node.transparent.TransparentNodeDescriptor; -import mods.eln.sim.*; -import mods.eln.sim.mna.component.Resistor; -import mods.eln.sixnode.electricalcable.ElectricalCableDescriptor; -import mods.eln.wiki.Data; -import net.minecraft.entity.item.EntityItem; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.item.Item; -import net.minecraft.item.ItemStack; -import net.minecraft.nbt.NBTTagCompound; - -import java.util.List; - -import static mods.eln.i18n.I18N.tr; - -public class BatteryDescriptor extends TransparentNodeDescriptor { - - public double electricalU, electricalDischargeRate; - public double electricalStdP, electricalStdDischargeTime, electricalStdHalfLife, electricalStdEfficiency, electricalPMax; - - public double electricalStdEnergy, electricalStdI; - - public double thermalHeatTime, thermalWarmLimit, thermalCoolLimit; - - public double electricalQ, electricalRs, electricalRp; - public double thermalC, thermalRp, thermalPMax; - public double lifeNominalCurrent, lifeNominalLost; - public double startCharge; - public boolean isRechargable; - String description = "todo battery"; - - FunctionTable UfCharge; - String modelName; - Obj3DPart modelPart; - public double IMax; - public boolean lifeEnable; - private ElectricalCableDescriptor cable; - - Obj3D obj; - - Obj3DPart main, plugPlus, plusMinus, battery; - - int renderType; - private String renderSpec; - - public static final BatteryDescriptor[] list = new BatteryDescriptor[8]; - - public double currentDropVoltage = 1000000, currentDropFactor = 0; - - public void draw(boolean plus, boolean minus) { - switch (renderType) { - case 0: - if (modelPart == null) return; - modelPart.draw(); - break; - case 1: - if (main != null) main.draw(); - if (plugPlus != null && plus) plugPlus.draw(); - if (plusMinus != null && minus) plusMinus.draw(); - //if(cables != null) cables.draw(); - if (battery != null) battery.draw(); - break; - } - } - - public BatteryDescriptor(String name, String modelName, - ElectricalCableDescriptor cable, - double startCharge, boolean isRechargable, boolean lifeEnable, - FunctionTable UfCharge, - double electricalU, double electricalPMax, double electricalDischargeRate, - double electricalStdP, double electricalStdDischargeTime, double electricalStdEfficiency, double electricalStdHalfLife, - double thermalHeatTime, double thermalWarmLimit, double thermalCoolLimit, - String description) { - super(name, BatteryElement.class, BatteryRender.class); - this.electricalU = electricalU; - this.electricalDischargeRate = electricalDischargeRate; - this.electricalStdEfficiency = electricalStdEfficiency; - this.electricalStdP = electricalStdP; - this.electricalStdHalfLife = electricalStdHalfLife; - this.electricalStdDischargeTime = electricalStdDischargeTime; - this.startCharge = startCharge; - this.isRechargable = isRechargable; - this.lifeEnable = lifeEnable; - this.cable = cable; - - this.thermalHeatTime = thermalHeatTime; - this.thermalWarmLimit = thermalWarmLimit; - this.thermalCoolLimit = thermalCoolLimit; - this.electricalPMax = electricalPMax; - - this.UfCharge = UfCharge; - this.description = description; - - electricalStdI = electricalStdP / electricalU; - electricalStdEnergy = electricalStdDischargeTime * electricalStdP; - - electricalQ = electricalStdP * electricalStdDischargeTime / electricalU; - electricalQ = 1; - double energy = getEnergy(1.0, 1.0); - electricalQ *= electricalStdEnergy / energy; - electricalRs = electricalStdP * (1 - electricalStdEfficiency) / electricalStdI / electricalStdI / 2; - //electricalRs = cable.electricalRs; - electricalRp = Math.min(electricalU * electricalU / electricalStdP / electricalDischargeRate, 1000000000.0); - - lifeNominalCurrent = electricalStdP / electricalU; - lifeNominalLost = 0.5 / electricalStdHalfLife; - - thermalPMax = electricalPMax / electricalU * electricalPMax / electricalU * electricalRs * 2; - thermalC = Math.pow(electricalPMax / electricalU, 2) * electricalRs * thermalHeatTime / thermalWarmLimit; - thermalRp = thermalWarmLimit / thermalPMax; - - IMax = electricalStdI * 3; - - obj = Eln.obj.getObj(modelName); - - if (obj != null) { - if (obj.getString("type").equals("A")) - renderType = 0; - if (obj.getString("type").equals("B")) - renderType = 1; - - switch (renderType) { - case 0: - modelPart = obj.getPart("Battery"); - case 1: - break; - } - } - - voltageLevelColor = VoltageLevelColor.fromVoltage(electricalU); - } - - @Override - public void setParent(Item item, int damage) { - super.setParent(item, damage); - Data.addEnergy(newItemStack()); - } - - public void applyTo(Resistor resistor) { - resistor.setR(electricalRp); - } - - public void applyTo(BatteryProcess battery) { - battery.uNominal = electricalU; - battery.QNominal = electricalQ; - battery.voltageFunction = UfCharge; - battery.isRechargeable = isRechargable; - //battery.efficiency = electricalStdEfficiency; - - // Convert old battery absolute charge in Coulomb to to fraction of battery capacity if the capacity is - // very small and the output voltage is more than a quarter of the nominal voltage. - if (battery.Q > 1.5 && battery.getU() > (battery.uNominal / 4)) { - battery.Q /= electricalQ; - } - } - - public void applyTo(ElectricalLoad load, Simulator simulator) { - load.setRs(electricalRs); - } - - public void applyTo(ThermalLoad load) { - load.Rp = thermalRp; - load.C = thermalC; - //load.setRsByTao(2); - } - - public void applyTo(BatterySlowProcess process) { - process.lifeNominalCurrent = lifeNominalCurrent; - process.lifeNominalLost = lifeNominalLost; - } - - public static BatteryDescriptor getDescriptorFrom(ItemStack itemStack) { - return list[itemStack.getItemDamage() & 0x7]; - } - - @Override - public NBTTagCompound getDefaultNBT() { - NBTTagCompound nbt = new NBTTagCompound(); - - nbt.setDouble("charge", startCharge); - nbt.setDouble("life", 1.0); - return nbt; - } - - @Override - public void addInformation(ItemStack itemStack, EntityPlayer entityPlayer, List list, boolean par4) { - super.addInformation(itemStack, entityPlayer, list, par4); - list.add(Utils.plotVolt(tr("Nominal voltage: "), electricalU)); - list.add(Utils.plotPower(tr("Nominal power: "), electricalStdP)); - list.add(Utils.plotEnergy(tr("Energy capacity: "), electricalStdDischargeTime * electricalStdP)); - list.add(Utils.plotOhm(tr("Internal resistance: "), electricalRs * 2)); - list.add(""); - list.add(Utils.plotPercent(tr("Actual charge: "), getChargeInTag(itemStack))); - - if (lifeEnable) - list.add(Utils.plotPercent(tr("Life: "), getLifeInTag(itemStack))); - } - - @Override - public String getName(ItemStack stack) { - return super.getName(stack) + Utils.plotPercent(tr(" charged at "), getChargeInTag(stack)); - } - - double getChargeInTag(ItemStack stack) { - if (!stack.hasTagCompound()) - stack.setTagCompound(getDefaultNBT()); - return stack.getTagCompound().getDouble("charge"); - } - - double getLifeInTag(ItemStack stack) { - if (!stack.hasTagCompound()) - stack.setTagCompound(getDefaultNBT()); - return stack.getTagCompound().getDouble("life"); - } - - public double getEnergy(double charge, double life) { - int stepNbr = 50; - double chargeStep = charge / stepNbr; - double chargeIntegrator = 0; - double energy = 0; - double QperStep = electricalQ * life * charge / stepNbr; - - for (int step = 0; step < stepNbr; step++) { - double voltage = UfCharge.getValue(chargeIntegrator) * electricalU; - energy += voltage * QperStep; - chargeIntegrator += chargeStep; - } - return energy; - } - - @Override - public boolean handleRenderType(ItemStack item, ItemRenderType type) { - return true; - } - - @Override - public boolean shouldUseRenderHelper(ItemRenderType type, ItemStack item, ItemRendererHelper helper) { - return type != ItemRenderType.INVENTORY; - } - - @Override - public void renderItem(ItemRenderType type, ItemStack item, Object... data) { - if (type == ItemRenderType.INVENTORY) { - super.renderItem(type, item, data); - } else { - draw(true, true); - } - } - - @Override - public boolean onEntityItemUpdate(EntityItem entityItem) { - if (entityItem.isBurning()) { - entityItem.worldObj.createExplosion(entityItem, entityItem.posX, entityItem.posY, entityItem.posZ, 2, true); - entityItem.extinguish(); - entityItem.setDead(); - } - return false; - } - - public void setRenderSpec(String renderSpec) { - this.renderSpec = renderSpec; - - if (obj != null) { - switch (renderType) { - case 0: - - case 1: - main = obj.getPart("main"); - plugPlus = obj.getPart("plugPlus"); - plusMinus = obj.getPart("plugMinus"); - // cables = obj.getPart("cables"); - battery = obj.getPart("battery_" + renderSpec); - break; - } - } - } - - public void setCurrentDrop(double currentDropVoltage, double currentDropFactor) { - this.currentDropFactor = currentDropFactor; - this.currentDropVoltage = currentDropVoltage; - } -} diff --git a/src/main/java/mods/eln/transparentnode/battery/BatteryDescriptor.kt b/src/main/java/mods/eln/transparentnode/battery/BatteryDescriptor.kt new file mode 100644 index 000000000..aba0b3c31 --- /dev/null +++ b/src/main/java/mods/eln/transparentnode/battery/BatteryDescriptor.kt @@ -0,0 +1,240 @@ +package mods.eln.transparentnode.battery + +import mods.eln.Eln +import mods.eln.i18n.I18N +import mods.eln.misc.FunctionTable +import mods.eln.misc.Obj3D +import mods.eln.misc.Obj3D.Obj3DPart +import mods.eln.misc.Utils +import mods.eln.misc.VoltageLevelColor +import mods.eln.node.transparent.TransparentNodeDescriptor +import mods.eln.sim.BatteryProcess +import mods.eln.sim.BatterySlowProcess +import mods.eln.sim.ElectricalLoad +import mods.eln.sim.Simulator +import mods.eln.sim.ThermalLoad +import mods.eln.sim.mna.component.Resistor +import mods.eln.wiki.Data +import net.minecraft.entity.item.EntityItem +import net.minecraft.entity.player.EntityPlayer +import net.minecraft.item.Item +import net.minecraft.item.ItemStack +import net.minecraft.nbt.NBTTagCompound +import net.minecraftforge.client.IItemRenderer.ItemRenderType +import net.minecraftforge.client.IItemRenderer.ItemRendererHelper + +class BatteryDescriptor( + name: String, + modelName: String, + var startCharge: Double, + var isRechargable: Boolean, + var lifeEnable: Boolean, + var UfCharge: FunctionTable, + var electricalU: Double, + var electricalPMax: Double, + var electricalDischargeRate: Double, + var electricalStdP: Double, + var electricalStdDischargeTime: Double, + var electricalStdEfficiency: Double, + var electricalStdHalfLife: Double, + var thermalHeatTime: Double, + var thermalWarmLimit: Double, + var thermalCoolLimit: Double +) : TransparentNodeDescriptor(name, BatteryElement::class.java, BatteryRender::class.java) { + @JvmField + var electricalStdEnergy: Double + var electricalStdI: Double + var electricalQ: Double + @JvmField + var electricalRs: Double + @JvmField + var electricalRp: Double + var thermalC: Double + var thermalRp: Double + var thermalPMax: Double + var lifeNominalCurrent: Double + var lifeNominalLost: Double + var modelName: String? = null + var modelPart: Obj3DPart? = null + @JvmField + var IMax: Double + var obj: Obj3D? + var main: Obj3DPart? = null + var plugPlus: Obj3DPart? = null + var plusMinus: Obj3DPart? = null + var battery: Obj3DPart? = null + var renderType = 0 + private var renderSpec: String? = null + @JvmField + var currentDropVoltage = 1000000.0 + @JvmField + var currentDropFactor = 0.0 + fun draw(plus: Boolean, minus: Boolean) { + when (renderType) { + 0 -> { + if (modelPart == null) return + modelPart!!.draw() + } + 1 -> { + if (main != null) main!!.draw() + if (plugPlus != null && plus) plugPlus!!.draw() + if (plusMinus != null && minus) plusMinus!!.draw() + if (battery != null) battery!!.draw() + } + } + } + + override fun setParent(item: Item, damage: Int) { + super.setParent(item, damage) + Data.addEnergy(newItemStack()) + } + + fun applyTo(resistor: Resistor) { + resistor.r = electricalRp + } + + fun applyTo(battery: BatteryProcess) { + battery.uNominal = electricalU + battery.QNominal = electricalQ + battery.voltageFunction = UfCharge + battery.isRechargeable = isRechargable + + // Convert old battery absolute charge in Coulomb to to fraction of battery capacity if the capacity is + // very small and the output voltage is more than a quarter of the nominal voltage. + if (battery.Q > 1.5 && battery.u > battery.uNominal / 4) { + battery.Q /= electricalQ + } + } + + fun applyTo(load: ElectricalLoad, simulator: Simulator?) { + load.rs = electricalRs + } + + fun applyTo(load: ThermalLoad) { + load.Rp = thermalRp + load.C = thermalC + } + + fun applyTo(process: BatterySlowProcess) { + process.lifeNominalCurrent = lifeNominalCurrent + process.lifeNominalLost = lifeNominalLost + } + + override fun getDefaultNBT(): NBTTagCompound { + val nbt = NBTTagCompound() + nbt.setDouble("charge", startCharge) + nbt.setDouble("life", 1.0) + return nbt + } + + override fun addInformation(itemStack: ItemStack, entityPlayer: EntityPlayer?, list: MutableList, par4: Boolean) { + super.addInformation(itemStack, entityPlayer, list, par4) + list.add(Utils.plotVolt(I18N.tr("Nominal voltage: "), electricalU)) + list.add(Utils.plotPower(I18N.tr("Nominal power: "), electricalStdP)) + list.add(Utils.plotEnergy(I18N.tr("Energy capacity: "), electricalStdDischargeTime * electricalStdP)) + list.add(Utils.plotOhm(I18N.tr("Internal resistance: "), electricalRs * 2)) + list.add("") + list.add(Utils.plotPercent(I18N.tr("Actual charge: "), getChargeInTag(itemStack))) + if (lifeEnable) list.add(Utils.plotPercent(I18N.tr("Life: "), getLifeInTag(itemStack))) + } + + override fun getName(stack: ItemStack): String { + return super.getName(stack) + Utils.plotPercent(I18N.tr(" charged at "), getChargeInTag(stack)) + } + + fun getChargeInTag(stack: ItemStack): Double { + if (!stack.hasTagCompound()) stack.tagCompound = defaultNBT + return stack.tagCompound.getDouble("charge") + } + + fun getLifeInTag(stack: ItemStack): Double { + if (!stack.hasTagCompound()) stack.tagCompound = defaultNBT + return stack.tagCompound.getDouble("life") + } + + fun getEnergy(charge: Double, life: Double): Double { + val stepNbr = 50 + val chargeStep = charge / stepNbr + var chargeIntegrator = 0.0 + var energy = 0.0 + val QperStep = electricalQ * life * charge / stepNbr + for (step in 0 until stepNbr) { + val voltage = UfCharge.getValue(chargeIntegrator) * electricalU + energy += voltage * QperStep + chargeIntegrator += chargeStep + } + return energy + } + + override fun handleRenderType(item: ItemStack, type: ItemRenderType): Boolean { + return true + } + + override fun shouldUseRenderHelper(type: ItemRenderType, item: ItemStack, helper: ItemRendererHelper): Boolean { + return type != ItemRenderType.INVENTORY + } + + override fun renderItem(type: ItemRenderType, item: ItemStack, vararg data: Any) { + if (type == ItemRenderType.INVENTORY) { + super.renderItem(type, item, *data) + } else { + draw(true, true) + } + } + + override fun onEntityItemUpdate(entityItem: EntityItem): Boolean { + if (entityItem.isBurning) { + entityItem.worldObj.createExplosion(entityItem, entityItem.posX, entityItem.posY, entityItem.posZ, 2f, true) + entityItem.extinguish() + entityItem.setDead() + } + return false + } + + fun setRenderSpec(renderSpec: String) { + this.renderSpec = renderSpec + if (obj != null) { + when (renderType) { + 0, 1 -> { + main = obj!!.getPart("main") + plugPlus = obj!!.getPart("plugPlus") + plusMinus = obj!!.getPart("plugMinus") + battery = obj!!.getPart("battery_$renderSpec") + } + } + } + } + + fun setCurrentDrop(currentDropVoltage: Double, currentDropFactor: Double) { + this.currentDropFactor = currentDropFactor + this.currentDropVoltage = currentDropVoltage + } + + init { + electricalStdI = electricalStdP / electricalU + electricalStdEnergy = electricalStdDischargeTime * electricalStdP + electricalQ = electricalStdP * electricalStdDischargeTime / electricalU + electricalQ = 1.0 + val energy = getEnergy(1.0, 1.0) + electricalQ *= electricalStdEnergy / energy + electricalRs = electricalStdP * (1 - electricalStdEfficiency) / electricalStdI / electricalStdI / 2 + electricalRp = Math.min(electricalU * electricalU / electricalStdP / electricalDischargeRate, 1000000000.0) + lifeNominalCurrent = electricalStdP / electricalU + lifeNominalLost = 0.5 / electricalStdHalfLife + thermalPMax = electricalPMax / electricalU * electricalPMax / electricalU * electricalRs * 2 + thermalC = Math.pow(electricalPMax / electricalU, 2.0) * electricalRs * thermalHeatTime / thermalWarmLimit + thermalRp = thermalWarmLimit / thermalPMax + IMax = electricalStdI * 3 + obj = Eln.obj.getObj(modelName) + if (obj != null) { + if (obj!!.getString("type") == "A") renderType = 0 + if (obj!!.getString("type") == "B") renderType = 1 + when (renderType) { + 0 -> modelPart = obj!!.getPart("Battery") + 1 -> { + } + } + } + voltageLevelColor = VoltageLevelColor.fromVoltage(electricalU) + } +} diff --git a/src/main/java/mods/eln/transparentnode/battery/BatteryElement.java b/src/main/java/mods/eln/transparentnode/battery/BatteryElement.java deleted file mode 100644 index 7b6e0fdbf..000000000 --- a/src/main/java/mods/eln/transparentnode/battery/BatteryElement.java +++ /dev/null @@ -1,292 +0,0 @@ -package mods.eln.transparentnode.battery; - -import mods.eln.Eln; -import mods.eln.i18n.I18N; -import mods.eln.item.OverHeatingProtectionDescriptor; -import mods.eln.item.OverVoltageProtectionDescriptor; -import mods.eln.misc.Direction; -import mods.eln.misc.LRDU; -import mods.eln.misc.Utils; -import mods.eln.node.AutoAcceptInventoryProxy; -import mods.eln.node.transparent.TransparentNode; -import mods.eln.node.transparent.TransparentNodeDescriptor; -import mods.eln.node.transparent.TransparentNodeElement; -import mods.eln.node.transparent.TransparentNodeElementInventory; -import mods.eln.sim.ElectricalLoad; -import mods.eln.sim.NodeVoltageState; -import mods.eln.sim.ThermalLoad; -import mods.eln.sim.mna.component.Resistor; -import mods.eln.sim.mna.component.ResistorSwitch; -import mods.eln.sim.mna.component.VoltageSource; -import mods.eln.sim.nbt.NbtBatteryProcess; -import mods.eln.sim.nbt.NbtBatterySlowProcess; -import mods.eln.sim.nbt.NbtElectricalLoad; -import mods.eln.sim.nbt.NbtThermalLoad; -import mods.eln.sim.process.destruct.ThermalLoadWatchDog; -import mods.eln.sim.process.destruct.WorldExplosion; -import mods.eln.sim.process.heater.ElectricalLoadHeatThermalLoad; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.inventory.Container; -import net.minecraft.inventory.IInventory; -import net.minecraft.nbt.NBTTagCompound; - -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; - -public class BatteryElement extends TransparentNodeElement { - - public NbtElectricalLoad cutLoad = new NbtElectricalLoad("cutLoad"); - public NodeVoltageState positiveLoad = new NodeVoltageState("positiveLoad"); - public NbtElectricalLoad negativeLoad = new NbtElectricalLoad("negativeLoad"); - public VoltageSource voltageSource = new VoltageSource("volSrc", positiveLoad, negativeLoad); - - public NbtThermalLoad thermalLoad = new NbtThermalLoad("thermalLoad"); - public ElectricalLoadHeatThermalLoad negativeETProcess = new ElectricalLoadHeatThermalLoad(negativeLoad, thermalLoad); - public ThermalLoadWatchDog thermalWatchdog = new ThermalLoadWatchDog(); - - public NbtBatteryProcess batteryProcess = new NbtBatteryProcess(positiveLoad, negativeLoad, null, 0, voltageSource, thermalLoad); - - public Resistor dischargeResistor = new Resistor(positiveLoad, negativeLoad); - public ResistorSwitch cutSwitch = new ResistorSwitch("cutSwitch", cutLoad, positiveLoad); - - public BatteryInventoryProcess inventoryProcess = new BatteryInventoryProcess(this); - - double syncronizedPositiveUc, syncronizedNegativeUc, syncronizedCurrent, syncronizedTc; - - NbtBatterySlowProcess batterySlowProcess = new NbtBatterySlowProcess(node, batteryProcess, thermalLoad); - - AutoAcceptInventoryProxy inventory = - (new AutoAcceptInventoryProxy(new TransparentNodeElementInventory(2, 64, this))) - .acceptIfEmpty(0, OverVoltageProtectionDescriptor.class) - .acceptIfEmpty(1, OverHeatingProtectionDescriptor.class); - - boolean fromNBT = false; - - public BatteryDescriptor descriptor; - - //static int UUIDCounter = 0; - //int UUID = 0; - boolean fromItemStack = false; - double fromItemStack_charge; - double fromItemStack_life; - - public BatteryElement(TransparentNode transparentNode, TransparentNodeDescriptor descriptor) { - super(transparentNode, descriptor); - this.descriptor = (BatteryDescriptor) descriptor; - - electricalLoadList.add(cutLoad); - electricalLoadList.add(positiveLoad); - electricalLoadList.add(negativeLoad); - - electricalComponentList.add(new Resistor(positiveLoad, null)); - electricalComponentList.add(new Resistor(negativeLoad, null)); - //electricalComponentList.add(new Resistor(cutLoad, null).setR(1000)); - // electricalComponentList.add(new Resistor(positiveLoad, null).setR(1000)); - //electricalComponentList.add(new Resistor(negativeLoad, null).setR(1000)); - electricalComponentList.add(dischargeResistor); - electricalComponentList.add(voltageSource); - electricalComponentList.add(cutSwitch); - thermalLoadList.add(thermalLoad); - electricalProcessList.add(batteryProcess); - thermalFastProcessList.add(negativeETProcess); - - slowProcessList.add(batterySlowProcess); - slowProcessList.add(inventoryProcess); - - grounded = false; - batteryProcess.setIMax(this.descriptor.IMax); - - slowProcessList.add(thermalWatchdog); - - thermalWatchdog - .set(thermalLoad) - .setTMax(this.descriptor.thermalWarmLimit) - .set(new WorldExplosion(this).machineExplosion()); - } - - @Override - public IInventory getInventory() { - return inventory.getInventory(); - } - - public boolean hasOverVoltageProtection() { - return getInventory().getStackInSlot(0) != null; - } - - public boolean hasOverHeatingProtection() { - return getInventory().getStackInSlot(1) != null; - } - - @Override - public ElectricalLoad getElectricalLoad(Direction side, LRDU lrdu) { - if (lrdu != LRDU.Down) return null; - if (side == front.left()) return cutLoad; - if (side == front.right() && !grounded) return negativeLoad; - return null; - } - - @Override - public ThermalLoad getThermalLoad(Direction side, LRDU lrdu) { - if (lrdu != LRDU.Down) return null; - /* if(side == front.left()) return thermalLoad; - if(side == front.right() && ! grounded) return thermalLoad;*/ - return null; - } - - - - @Override - public int getConnectionMask(Direction side, LRDU lrdu) { - if (lrdu != LRDU.Down) return 0; - if (side == front.left()) return node.maskElectricalPower; - if (side == front.right() && !grounded) return node.maskElectricalPower; - return 0; - } - - @Override - public String multiMeterString(Direction side) { - String str = ""; - str += Utils.plotVolt("Ubat:", batteryProcess.getU()); - str += Utils.plotAmpere("I:", batteryProcess.getDischargeCurrent()); - str += Utils.plotPercent("Charge:", batteryProcess.getCharge()); - // batteryProcess.life is a percentage from 1.0 to 0.0. - str += Utils.plotPercent("Life:", batteryProcess.life); - return str; - } - - @Override - public String thermoMeterString(Direction side) { - return Utils.plotCelsius("Tbat:", thermalLoad.Tc); - } - - @Override - public void networkSerialize(DataOutputStream stream) { - super.networkSerialize(stream); - try { - double U = batteryProcess.getU();//(positiveLoad.Uc - negativeLoad.Uc); - stream.writeFloat((float) (U * batteryProcess.getDischargeCurrent())); - stream.writeFloat((float) batteryProcess.getEnergy()); - stream.writeShort((short) (batteryProcess.life * 1000)); - - node.lrduCubeMask.getTranslate(Direction.YN).serialize(stream); - } catch (IOException e) { - e.printStackTrace(); - } - } - - @Override - public void initialize() { - initPhysicalValue(); - connect(); - } - - public void initPhysicalValue() { - descriptor.applyTo(batteryProcess); - descriptor.applyTo(thermalLoad); - descriptor.applyTo(dischargeResistor); - descriptor.applyTo(batterySlowProcess); - cutSwitch.setR(descriptor.electricalRs / 2); - cutLoad.setRs(descriptor.electricalRs / 2); - negativeLoad.setRs(descriptor.electricalRs); - if (fromItemStack) { - batteryProcess.life = fromItemStack_life; - batteryProcess.setCharge(fromItemStack_charge); - fromItemStack = false; - } - } - - @Override - public void inventoryChange(IInventory inventory) { - // initPhysicalValue(); - } - - @Override - public boolean onBlockActivated(EntityPlayer entityPlayer, Direction side, float vx, float vy, float vz) { - return inventory.take(entityPlayer.getCurrentEquippedItem()); - } - - @Override - public void writeToNBT(NBTTagCompound nbt) { - super.writeToNBT(nbt); - //inventory.writeToNBT(nbt, str + "inv"); - } - - @Override - public void readFromNBT(NBTTagCompound nbt) { - super.readFromNBT(nbt); - fromNBT = true; - //inventory.readFromNBT(nbt, str + "inv"); - } - - @Override - public boolean hasGui() { - return true; - } - - @Override - public Container newContainer(Direction side, EntityPlayer player) { - return new BatteryContainer(this.node, player, getInventory()); - } - - @Override - public void onGroundedChangedByClient() { - super.onGroundedChangedByClient(); - - disconnect(); - initPhysicalValue(); - reconnect(); - } - - @Override - public void readItemStackNBT(NBTTagCompound nbt) { - super.readItemStackNBT(nbt); - - if (nbt == null) nbt = descriptor.getDefaultNBT(); - fromItemStack_charge = nbt.getDouble("charge"); - fromItemStack_life = nbt.getDouble("life"); - - fromItemStack = true; - } - - @Override - public NBTTagCompound getItemStackNBT() { - NBTTagCompound nbt = new NBTTagCompound(); - nbt.setDouble("charge", batteryProcess.getCharge()); - nbt.setDouble("life", batteryProcess.life); - return nbt; - } - - /* - public static NBTTagCompound newItemStackNBT() { - NBTTagCompound nbt = new NBTTagCompound("itemStackNBT"); - nbt.setDouble("charge", 0.5); - nbt.setDouble("life", 1.0); - return nbt; - }*/ - - @Override - public Map getWaila() { - Map info = new HashMap(); - info.put(I18N.tr("Charge"), Utils.plotPercent("", batteryProcess.getCharge())); - info.put(I18N.tr("Energy"), Utils.plotEnergy("", batteryProcess.getEnergy())); - info.put(I18N.tr("Life"), Utils.plotPercent("", batteryProcess.life)); - if (Eln.wailaEasyMode) { - info.put(I18N.tr("Voltage"), Utils.plotVolt("", batteryProcess.getU())); - info.put(I18N.tr("Current"), Utils.plotAmpere("", batteryProcess.getDischargeCurrent())); - info.put(I18N.tr("Temperature"), Utils.plotCelsius("", thermalLoad.Tc)); - } - int subSystemSize = positiveLoad.getSubSystem().component.size(); - String textColor = ""; - if (subSystemSize <= 8) { - textColor = "§a"; - } else if (subSystemSize <= 15) { - textColor = "§6"; - } else { - textColor = "§c"; - } - info.put(I18N.tr("Subsystem Matrix Size"), textColor + subSystemSize); - return info; - } -} diff --git a/src/main/java/mods/eln/transparentnode/battery/BatteryElement.kt b/src/main/java/mods/eln/transparentnode/battery/BatteryElement.kt new file mode 100644 index 000000000..bf8737934 --- /dev/null +++ b/src/main/java/mods/eln/transparentnode/battery/BatteryElement.kt @@ -0,0 +1,179 @@ +package mods.eln.transparentnode.battery + +import mods.eln.Eln +import mods.eln.i18n.I18N +import mods.eln.misc.Direction +import mods.eln.misc.LRDU +import mods.eln.misc.Utils +import mods.eln.node.NodeBase +import mods.eln.node.NodePeriodicPublishProcess +import mods.eln.node.transparent.TransparentNode +import mods.eln.node.transparent.TransparentNodeDescriptor +import mods.eln.node.transparent.TransparentNodeElement +import mods.eln.sim.ElectricalLoad +import mods.eln.sim.ThermalLoad +import mods.eln.sim.mna.component.Resistor +import mods.eln.sim.mna.component.VoltageSource +import mods.eln.sim.mna.misc.MnaConst +import mods.eln.sim.nbt.NbtBatteryProcess +import mods.eln.sim.nbt.NbtBatterySlowProcess +import mods.eln.sim.nbt.NbtElectricalLoad +import mods.eln.sim.nbt.NbtThermalLoad +import mods.eln.sim.process.destruct.ThermalLoadWatchDog +import mods.eln.sim.process.destruct.WorldExplosion +import mods.eln.sim.process.heater.ElectricalLoadHeatThermalLoad +import net.minecraft.entity.player.EntityPlayer +import net.minecraft.nbt.NBTTagCompound +import java.io.DataOutputStream +import java.io.IOException +import java.util.* + +class BatteryElement(transparentNode: TransparentNode?, descriptor: TransparentNodeDescriptor) : TransparentNodeElement(transparentNode, descriptor) { + var descriptor: BatteryDescriptor = descriptor as BatteryDescriptor + var positiveLoad = NbtElectricalLoad("positiveLoad") + var negativeLoad = NbtElectricalLoad("negativeLoad") + var voltageSource = VoltageSource("volSrc", positiveLoad, negativeLoad) + var thermalLoad = NbtThermalLoad("thermalLoad") + var negativeETProcess = ElectricalLoadHeatThermalLoad(negativeLoad, thermalLoad) + var thermalWatchdog = ThermalLoadWatchDog() + var batteryProcess = NbtBatteryProcess(positiveLoad, negativeLoad, this.descriptor.UfCharge, 0.0, voltageSource, thermalLoad) + var dischargeResistor = Resistor(positiveLoad, negativeLoad) + var batterySlowProcess = NbtBatterySlowProcess(node, batteryProcess, thermalLoad) + var fromItemStack = false + var fromItemstackCharge = 0.0 + var fromItemstackLife = 0.0 + override fun getElectricalLoad(side: Direction, lrdu: LRDU): ElectricalLoad? { + if (lrdu != LRDU.Down) return null + if (side == front.left()) return positiveLoad + return if (side == front.right()) negativeLoad else null + } + + override fun getThermalLoad(side: Direction, lrdu: LRDU): ThermalLoad? { + if (lrdu != LRDU.Down) return null + if (side == front.left()) return thermalLoad + return if (side == front.right()) thermalLoad else null + } + + override fun getConnectionMask(side: Direction, lrdu: LRDU): Int { + if (lrdu != LRDU.Down) return 0 + if (side == front.left()) return NodeBase.maskElectricalPower + return if (side == front.right()) NodeBase.maskElectricalPower else 0 + } + + override fun multiMeterString(side: Direction): String { + var str = "" + str += Utils.plotVolt("Ubat:", batteryProcess.u) + str += Utils.plotAmpere("I:", batteryProcess.dischargeCurrent) + str += Utils.plotPercent("Charge:", batteryProcess.charge) + // batteryProcess.life is a percentage from 1.0 to 0.0. + str += Utils.plotPercent("Life:", batteryProcess.life) + return str + } + + override fun thermoMeterString(side: Direction): String { + return Utils.plotCelsius("Tbat:", thermalLoad.Tc) + } + + override fun networkSerialize(stream: DataOutputStream) { + super.networkSerialize(stream) + try { + stream.writeFloat((batteryProcess.u * batteryProcess.dischargeCurrent).toFloat()) + stream.writeFloat(batteryProcess.energy.toFloat()) + stream.writeShort((batteryProcess.life * 1000).toInt()) + node.lrduCubeMask.getTranslate(Direction.YN).serialize(stream) + } catch (e: IOException) { + e.printStackTrace() + } + } + + override fun initialize() { + descriptor.applyTo(batteryProcess) + descriptor.applyTo(thermalLoad) + descriptor.applyTo(dischargeResistor) + descriptor.applyTo(batterySlowProcess) + positiveLoad.rs = descriptor.electricalRs + negativeLoad.rs = descriptor.electricalRs + dischargeResistor.r = MnaConst.highImpedance + if (fromItemStack) { + println("Loading from item stack") + batteryProcess.life = fromItemstackLife + batteryProcess.charge = fromItemstackCharge + fromItemStack = false + } + connect() + } + + override fun reconnect() { + disconnect() + connect() + } + + override fun onBlockActivated(entityPlayer: EntityPlayer, side: Direction, vx: Float, vy: Float, vz: Float): Boolean { + return false + } + + override fun hasGui(): Boolean { + return true + } + + override fun readItemStackNBT(nbt: NBTTagCompound?) { + super.readItemStackNBT(nbt) + fromItemstackCharge = nbt?.getDouble("charge")?: 0.5 + fromItemstackLife = nbt?.getDouble("life")?: 1.0 + fromItemStack = true + } + + override fun getItemStackNBT(): NBTTagCompound { + val nbt = NBTTagCompound() + nbt.setDouble("charge", batteryProcess.charge) + nbt.setDouble("life", batteryProcess.life) + return nbt + } + + override fun getWaila(): Map { + val info: MutableMap = HashMap() + info[I18N.tr("Charge")] = Utils.plotPercent("", batteryProcess.charge) + info[I18N.tr("Energy")] = Utils.plotEnergy("", batteryProcess.energy) + info[I18N.tr("Life")] = Utils.plotPercent("", batteryProcess.life) + if (Eln.wailaEasyMode) { + info[I18N.tr("Voltage")] = Utils.plotVolt("", batteryProcess.u) + info[I18N.tr("Current")] = Utils.plotAmpere("", batteryProcess.dischargeCurrent) + info[I18N.tr("Temperature")] = Utils.plotCelsius("", thermalLoad.Tc) + } + val subSystemSize = positiveLoad.subSystem.component.size + var textColor = "" + textColor = when { + subSystemSize <= 8 -> { + "§a" + } + subSystemSize <= 15 -> { + "§6" + } + else -> { + "§c" + } + } + info[I18N.tr("Subsystem Matrix Size")] = textColor + subSystemSize + return info + } + + init { + electricalLoadList.add(positiveLoad) + electricalLoadList.add(negativeLoad) + electricalComponentList.add(Resistor(positiveLoad, null)) + electricalComponentList.add(Resistor(negativeLoad, null)) + electricalComponentList.add(dischargeResistor) + electricalComponentList.add(voltageSource) + thermalLoadList.add(thermalLoad) + electricalProcessList.add(batteryProcess) + thermalFastProcessList.add(negativeETProcess) + slowProcessList.add(batterySlowProcess) + slowProcessList.add(NodePeriodicPublishProcess(transparentNode, 1.0, 0.0)) + batteryProcess.IMax = this.descriptor.IMax + slowProcessList.add(thermalWatchdog) + thermalWatchdog + .set(thermalLoad) + .setTMax(this.descriptor.thermalWarmLimit) + .set(WorldExplosion(this).machineExplosion()) + } +} diff --git a/src/main/java/mods/eln/transparentnode/battery/BatteryGuiDraw.java b/src/main/java/mods/eln/transparentnode/battery/BatteryGuiDraw.java deleted file mode 100644 index ee711e5ff..000000000 --- a/src/main/java/mods/eln/transparentnode/battery/BatteryGuiDraw.java +++ /dev/null @@ -1,104 +0,0 @@ -package mods.eln.transparentnode.battery; - -import mods.eln.gui.*; -import mods.eln.misc.Utils; -import mods.eln.node.transparent.TransparentNodeElementInventory; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.inventory.IInventory; - -import static mods.eln.i18n.I18N.tr; - -public class BatteryGuiDraw extends GuiContainerEln { - - private TransparentNodeElementInventory inventory; - BatteryRender render; - GuiButtonEln buttonGrounded; - GuiVerticalProgressBar energyBar; - - public BatteryGuiDraw(EntityPlayer player, IInventory inventory, BatteryRender render) { - super(new BatteryContainer(null, player, inventory)); - this.inventory = (TransparentNodeElementInventory) inventory; - this.render = render; - } - - public void initGui() { - super.initGui(); - - buttonGrounded = newGuiButton(8, 45, 100, ""); - buttonGrounded.visible = false; - energyBar = newGuiVerticalProgressBar(167 - 16, 8, 16, 69); - energyBar.setColor(0.2f, 0.5f, 0.8f); - } - - @Override - public void guiObjectEvent(IGuiObject object) { - super.guiObjectEvent(object); - if (buttonGrounded == object) { - render.clientSetGrounded(!render.grounded); - } - } - - @Override - protected void drawGuiContainerForegroundLayer(int param1, int param2) { - super.drawGuiContainerForegroundLayer(param1, param2); - } - - @Override - protected void preDraw(float f, int x, int y) { - super.preDraw(f, x, y); - buttonGrounded.displayString = tr("Grounded: " + render.grounded); - energyBar.setValue((float) (render.energy / (render.descriptor.electricalStdEnergy * render.life))); - energyBar.setComment(0, tr("Energy: %1$", Utils.plotPercent("", energyBar.getValue()).replace(" ", ""))); - - } - - @Override - protected void postDraw(float f, int x, int y) { - super.postDraw(f, x, y); - String str1, str2 = ""; - - double p = render.power; - double energyMiss = render.descriptor.electricalStdEnergy * render.life - render.energy; - - if (Math.abs(p) < 5) { - str1 = tr("No charge"); - } else if (p > 0) { - str1 = tr("Discharge"); - str2 = Utils.plotTime("", render.energy / p); - } else if (energyMiss > 0) { - str1 = tr("Charge"); - str2 = Utils.plotTime("", -energyMiss / p); - } else { - str1 = tr("Charged"); - } - - int xDelta = 70; - if (render.descriptor.lifeEnable) { - drawString(8, 8, tr("Life:")); - drawString(xDelta, 8, Utils.plotPercent("", render.life)); - } - drawString(8, 17, tr("Energy:")); - drawString(xDelta, 17, - Utils.plotValue(render.energy, "J/") + Utils.plotValue(render.descriptor.electricalStdEnergy * render.life, "J")); - - if (render.power >= 0) - drawString(8, 26, tr("Power out:")); - else - drawString(8, 26, tr("Power in:")); - drawString(xDelta, 26, Utils.plotValue(Math.abs(render.power), "W/") + Utils.plotValue(render.descriptor.electricalStdP, "W")); - - drawString(8, 35, str1); - drawString(xDelta, 35, str2); - - // drawString(8, 44, "Thermal protection"); - } - /* - list.add("Nominal voltage : " + (int)(electricalU) + "V"); - list.add("Nominal power : " + (int)(electricalStdP) + "W"); - */ - - @Override - protected GuiHelperContainer newHelper() { - return new HelperStdContainer(this); - } -} diff --git a/src/main/java/mods/eln/transparentnode/battery/BatteryGuiDraw.kt b/src/main/java/mods/eln/transparentnode/battery/BatteryGuiDraw.kt new file mode 100644 index 000000000..53886d474 --- /dev/null +++ b/src/main/java/mods/eln/transparentnode/battery/BatteryGuiDraw.kt @@ -0,0 +1,70 @@ +package mods.eln.transparentnode.battery + +import mods.eln.gui.GuiHelperContainer +import mods.eln.gui.GuiScreenEln +import mods.eln.gui.GuiVerticalProgressBar +import mods.eln.gui.HelperStdContainer +import mods.eln.gui.IGuiObject +import mods.eln.i18n.I18N +import mods.eln.misc.Utils + +class BatteryGuiDraw(var render: BatteryRender) : GuiScreenEln() { + var energyBar: GuiVerticalProgressBar? = null + + override fun initGui() { + super.initGui() + super.helper.ySize = 50 + energyBar = newGuiVerticalProgressBar(167 - 16, 8, 16, 35) + energyBar!!.setColor(0.2f, 0.5f, 0.8f) + } + + override fun guiObjectEvent(`object`: IGuiObject) { + super.guiObjectEvent(`object`) + } + + override fun preDraw(f: Float, x: Int, y: Int) { + super.preDraw(f, x, y) + energyBar!!.setValue((render.energy / (render.descriptor.electricalStdEnergy * render.life)).toFloat()) + energyBar!!.setComment(0, I18N.tr("Energy: %1$", Utils.plotPercent("", energyBar!!.value).replace(" ", ""))) + } + + override fun postDraw(f: Float, x: Int, y: Int) { + super.postDraw(f, x, y) + val str1: String + var str2: String? = "" + val p = render.power.toDouble() + val energyMiss = render.descriptor.electricalStdEnergy * render.life - render.energy + when { + Math.abs(p) < 5 -> { + str1 = I18N.tr("No charge") + } + p > 0 -> { + str1 = I18N.tr("Discharge") + str2 = Utils.plotTime("", render.energy / p) + } + energyMiss > 0 -> { + str1 = I18N.tr("Charge") + str2 = Utils.plotTime("", -energyMiss / p) + } + else -> { + str1 = I18N.tr("Charged") + } + } + val xDelta = 70 + if (render.descriptor.lifeEnable) { + drawString(8, 8, I18N.tr("Life:")) + drawString(xDelta, 8, Utils.plotPercent("", render.life.toDouble())) + } + drawString(8, 17, I18N.tr("Energy:")) + drawString(xDelta, 17, + Utils.plotValue(render.energy.toDouble(), "J/") + Utils.plotValue(render.descriptor.electricalStdEnergy * render.life, "J")) + if (render.power >= 0) drawString(8, 26, I18N.tr("Power out:")) else drawString(8, 26, I18N.tr("Power in:")) + drawString(xDelta, 26, Utils.plotValue(Math.abs(render.power).toDouble(), "W/") + Utils.plotValue(render.descriptor.electricalStdP, "W")) + drawString(8, 35, str1) + drawString(xDelta, 35, str2) + } + + override fun newHelper(): GuiHelperContainer { + return HelperStdContainer(this) + } +} diff --git a/src/main/java/mods/eln/transparentnode/battery/BatteryInventoryProcess.java b/src/main/java/mods/eln/transparentnode/battery/BatteryInventoryProcess.java deleted file mode 100644 index d0aa257af..000000000 --- a/src/main/java/mods/eln/transparentnode/battery/BatteryInventoryProcess.java +++ /dev/null @@ -1,57 +0,0 @@ -package mods.eln.transparentnode.battery; - -import mods.eln.sim.IProcess; - -public class BatteryInventoryProcess implements IProcess { - - BatteryElement battery; - - boolean thermalCut = false; - - public BatteryInventoryProcess(BatteryElement battery) { - this.battery = battery; - } - - @Override - public void process(double time) { - if (!battery.descriptor.lifeEnable) - battery.batteryProcess.life = 1.0; - - boolean cut = false; - if (battery.hasOverHeatingProtection()) { - if (battery.thermalLoad.Tc * 1.1 > battery.descriptor.thermalHeatTime) { - thermalCut = true; - } - if (battery.thermalLoad.Tc * 1.15 < battery.descriptor.thermalHeatTime) { - thermalCut = false; - } - } else { - thermalCut = false; - } - - if (thermalCut) cut = true; - - if (battery.hasOverVoltageProtection()) { - if (battery.batteryProcess.getU() * 1.1 > battery.batterySlowProcess.getUMax()) { - if (battery.batteryProcess.getU() < battery.cutLoad.getU() - battery.negativeLoad.getU()) { - cut = true; - } - } - if (battery.batteryProcess.getU() < battery.batteryProcess.uNominal * 0.001) { - if (battery.batteryProcess.getU() > battery.cutLoad.getU() - battery.negativeLoad.getU()) { - cut = true; - } - } - } - battery.cutSwitch.setState(!cut); - - double U = battery.positiveLoad.getU() - battery.negativeLoad.getU(); - - double currentDropPower = (U - battery.descriptor.currentDropVoltage) / battery.descriptor.electricalU * battery.descriptor.currentDropFactor; - if (currentDropPower > 0.1) { - battery.dischargeResistor.setR(1 / (1 / battery.descriptor.electricalRp + 1 / ((U * U) / currentDropPower))); - } else { - battery.dischargeResistor.setR(battery.descriptor.electricalRp); - } - } -} diff --git a/src/main/java/mods/eln/transparentnode/battery/BatteryRender.java b/src/main/java/mods/eln/transparentnode/battery/BatteryRender.java deleted file mode 100644 index b6379129e..000000000 --- a/src/main/java/mods/eln/transparentnode/battery/BatteryRender.java +++ /dev/null @@ -1,74 +0,0 @@ -package mods.eln.transparentnode.battery; - -import mods.eln.misc.Direction; -import mods.eln.misc.LRDUMask; -import mods.eln.node.transparent.TransparentNodeDescriptor; -import mods.eln.node.transparent.TransparentNodeElementInventory; -import mods.eln.node.transparent.TransparentNodeElementRender; -import mods.eln.node.transparent.TransparentNodeEntity; -import net.minecraft.client.gui.GuiScreen; -import net.minecraft.entity.player.EntityPlayer; - -import java.io.DataInputStream; -import java.io.IOException; - -public class BatteryRender extends TransparentNodeElementRender { - - //public double voltagePositive = 0, voltageNegative, current = 0, temperature = 0; - public float energy, life; - - public BatteryDescriptor descriptor; - - TransparentNodeElementInventory inventory = new TransparentNodeElementInventory(2, 64, this); - - boolean plus, minus; - - LRDUMask lrdu = new LRDUMask(); - - float power; - - public BatteryRender(TransparentNodeEntity tileEntity, TransparentNodeDescriptor descriptor) { - super(tileEntity, descriptor); - this.descriptor = (BatteryDescriptor) descriptor; - } - - @Override - public void draw() { - front.glRotateXnRef(); - descriptor.draw(plus, minus); - } - - /* - @Override - public GuiContainer newGuiDraw(Direction side, EntityPlayer player) { - return new TransformatorGuiDraw(player, inventory, this); - }*/ - - @Override - public void networkUnserialize(DataInputStream stream) { - super.networkUnserialize(stream); - try { - /*voltagePositive = stream.readShort() / Node.networkSerializeUFactor; - voltageNegative = stream.readShort() / Node.networkSerializeUFactor; - current = stream.readShort() / Node.networkSerializeIFactor; - temperature = stream.readShort() / Node.networkSerializeTFactor; - */ - power = stream.readFloat(); - energy = stream.readFloat(); - life = stream.readShort() / 1000.0f; - - lrdu.deserialize(stream); - - plus = true; - minus = true; - //plus = lrdu.get(lrdu) - } catch (IOException e) { - e.printStackTrace(); - } - } - - @Override - public GuiScreen newGuiDraw(Direction side, EntityPlayer player) { - return new BatteryGuiDraw(player, inventory, this); - } -} diff --git a/src/main/java/mods/eln/transparentnode/battery/BatteryRender.kt b/src/main/java/mods/eln/transparentnode/battery/BatteryRender.kt new file mode 100644 index 000000000..8b5a85ec0 --- /dev/null +++ b/src/main/java/mods/eln/transparentnode/battery/BatteryRender.kt @@ -0,0 +1,44 @@ +package mods.eln.transparentnode.battery + +import mods.eln.misc.Direction +import mods.eln.misc.LRDUMask +import mods.eln.node.transparent.TransparentNodeDescriptor +import mods.eln.node.transparent.TransparentNodeElementRender +import mods.eln.node.transparent.TransparentNodeEntity +import net.minecraft.client.gui.GuiScreen +import net.minecraft.entity.player.EntityPlayer +import java.io.DataInputStream +import java.io.IOException + +class BatteryRender(tileEntity: TransparentNodeEntity?, descriptor: TransparentNodeDescriptor) : TransparentNodeElementRender(tileEntity, descriptor) { + var energy = 0f + var life = 0f + var descriptor: BatteryDescriptor = descriptor as BatteryDescriptor + var plus = false + var minus = false + var lrdu = LRDUMask() + var power = 0f + + override fun draw() { + front.glRotateXnRef() + descriptor.draw(plus, minus) + } + + override fun networkUnserialize(stream: DataInputStream) { + super.networkUnserialize(stream) + try { + power = stream.readFloat() + energy = stream.readFloat() + life = stream.readShort() / 1000.0f + lrdu.deserialize(stream) + plus = true + minus = true + } catch (e: IOException) { + e.printStackTrace() + } + } + + override fun newGuiDraw(side: Direction, player: EntityPlayer): GuiScreen { + return BatteryGuiDraw(this) + } +}