From f0ba85bbe77959573ca3c436638b74d3ca9c738f Mon Sep 17 00:00:00 2001 From: Jared Dunbar Date: Sun, 18 Dec 2022 21:12:49 -0500 Subject: [PATCH 1/7] Adds electric minecarts and overhead lines (WIP) --- src/main/java/mods/eln/Eln.java | 33 +++++ .../entity/carts/EntityElectricMinecart.kt | 106 ++++++++++++++++ .../mods/eln/item/ElectricMinecartItem.kt | 41 ++++++ .../mods/eln/transparentnode/OverheadLines.kt | 120 ++++++++++++++++++ src/main/resources/assets/eln/lang/en_US.lang | 1 + .../model/OverheadGantry/OverheadGantry.mtl | 13 ++ .../model/OverheadGantry/OverheadGantry.obj | 54 ++++++++ .../model/OverheadGantry/OverheadGantry.png | Bin 0 -> 7037 bytes .../eln/textures/blocks/overheadlines.png | Bin 0 -> 638 bytes 9 files changed, 368 insertions(+) create mode 100644 src/main/java/mods/eln/entity/carts/EntityElectricMinecart.kt create mode 100644 src/main/java/mods/eln/item/ElectricMinecartItem.kt create mode 100644 src/main/java/mods/eln/transparentnode/OverheadLines.kt create mode 100644 src/main/resources/assets/eln/model/OverheadGantry/OverheadGantry.mtl create mode 100644 src/main/resources/assets/eln/model/OverheadGantry/OverheadGantry.obj create mode 100644 src/main/resources/assets/eln/model/OverheadGantry/OverheadGantry.png create mode 100644 src/main/resources/assets/eln/textures/blocks/overheadlines.png diff --git a/src/main/java/mods/eln/Eln.java b/src/main/java/mods/eln/Eln.java index 2e54e76a4..c6ddc6014 100644 --- a/src/main/java/mods/eln/Eln.java +++ b/src/main/java/mods/eln/Eln.java @@ -19,6 +19,7 @@ import mods.eln.client.SoundLoader; import mods.eln.entity.ReplicatorEntity; import mods.eln.entity.ReplicatorPopProcess; +import mods.eln.entity.carts.EntityElectricMinecart; import mods.eln.eventhandlers.ElnFMLEventsHandler; import mods.eln.eventhandlers.ElnForgeEventsHandler; import mods.eln.generic.*; @@ -688,6 +689,7 @@ public void preInit(FMLPreInitializationEvent event) { //registerFloodlight(68); registerFestive(69); registerFab(70); + registerOverheadWires(71); //ITEM REGISTRATION @@ -719,6 +721,7 @@ public void preInit(FMLPreInitializationEvent event) { registerFuelBurnerItem(124); registerPortableNaN(); // 125 registerBasicItems(126); + registerElectricMinecartItems(127); OreDictionary.registerOre("blockAluminum", arcClayBlock); OreDictionary.registerOre("blockSteel", arcMetalBlock); @@ -773,6 +776,16 @@ private void registerFab(int id) { }*/ } + private void registerOverheadWires(int id) { + int subId; + { + subId = 0; + OverheadLinesDescriptor desc = new OverheadLinesDescriptor("Overhead Lines", + obj.getObj("OverheadGantry")); + transparentNodeItem.addDescriptor(subId + (id << 6), desc); + } + } + private void registerGridDevices(int id) { int subId; { @@ -906,6 +919,7 @@ public void load(FMLInitializationEvent event) { // registerReplicator(); + registerElectricMinecart(); // recipeEnergyConverter(); @@ -5752,6 +5766,17 @@ private static void registerBasicItems(int id) { } } + private void registerElectricMinecartItems(int id) { + int subId; + String name; + { + subId = 0; + name = TR_NAME(Type.NONE, "Electric Minecart"); + ElectricMinecartItem minecartItem = new ElectricMinecartItem(name); + sharedItem.addElement(subId + (id << 6), minecartItem); + } + } + public DataLogsPrintDescriptor dataLogsPrintDescriptor; private void recipeGround() { @@ -8521,6 +8546,14 @@ private void registerReplicator() { } + private int electricMinecartId = -1; + + private void registerElectricMinecart() { + electricMinecartId = EntityRegistry.findGlobalUniqueEntityId(); + Utils.println("Electric Minecrart registered at " + electricMinecartId); + EntityRegistry.registerGlobalEntityID(EntityElectricMinecart.class, TR_NAME(Type.ENTITY, "ElectricMinecart"), electricMinecartId); + } + // Registers WIP items. private void registerWipItems() { } diff --git a/src/main/java/mods/eln/entity/carts/EntityElectricMinecart.kt b/src/main/java/mods/eln/entity/carts/EntityElectricMinecart.kt new file mode 100644 index 000000000..355cff13a --- /dev/null +++ b/src/main/java/mods/eln/entity/carts/EntityElectricMinecart.kt @@ -0,0 +1,106 @@ +package mods.eln.entity.carts + +import mods.eln.Eln +import mods.eln.misc.Coordinate +import mods.eln.node.NodeManager +import mods.eln.sim.mna.component.Resistor +import mods.eln.sim.mna.misc.MnaConst +import mods.eln.transparentnode.OverheadLinesElement +import net.minecraft.block.Block +import net.minecraft.entity.item.EntityMinecart +import net.minecraft.init.Blocks +import net.minecraft.util.DamageSource +import net.minecraft.world.World + +class EntityElectricMinecart(world: World, x: Double, y: Double, z: Double): EntityMinecart(world, x, y, z) { + + private var lastOverheadWires: OverheadLinesElement? = null + private var lastRunState = false + + private val locomotiveResistance = 500.0 + val resistor = Resistor() + + init { + resistor.r = locomotiveResistance + } + + override fun func_145821_a( + blockX: Int, + blockY: Int, + blockZ: Int, + speed: Double, + drag: Double, + block: Block?, + direction: Int + ) { + super.func_145821_a(blockX, blockY, blockZ, speed + 1, drag, block, direction) + val overheadWires = getOverheadWires(Coordinate(posX.toInt(), posY.toInt(), posZ.toInt(), worldObj)) ?: return + // TODO: Configure isRunning to act as the cart needs more battery power in an internal buffer + // Allow the cart to move when there is power in the buffer + // Control the cart ... somehow. Just turn it on full bore? lol + takeOverheadPower(overheadWires, true) + } + + private fun attachResistor(overheadWires: OverheadLinesElement) { + if (overheadWires != lastOverheadWires) + detachResistor() + lastOverheadWires = overheadWires + resistor.connectTo(overheadWires.electricalLoad, null) + overheadWires.reconnect() + overheadWires.needPublish() + } + + private fun detachResistor() { + if (resistor.subSystem != null) + resistor.subSystem.removeComponent(resistor) + resistor.dirty() + updateLastWire() + lastOverheadWires = null + } + + private fun updateLastWire() { + lastOverheadWires?.reconnect() + lastOverheadWires?.needPublish() + } + + override fun killMinecart(p_94095_1_: DamageSource?) { + super.killMinecart(p_94095_1_) + detachResistor() + } + + private fun takeOverheadPower(overheadWires: OverheadLinesElement, isRunning: Boolean) { + Eln.logger.info("Voltage over the cart is ${overheadWires.electricalLoad.u}") + + if (isRunning != lastRunState) { + resistor.r = if (isRunning) locomotiveResistance else MnaConst.highImpedance + resistor.dirty() + } + + attachResistor(overheadWires) + } + + private fun getOverheadWires(coordinate: Coordinate): OverheadLinesElement? { + // Pass coordinate of tracks and check vertically the next 3-4 blocks + val originalY = coordinate.y + while (coordinate.y <= (originalY + 4)) { + coordinate.y + val node = NodeManager.instance!!.getTransparentNodeFromCoordinate(coordinate) + if (node is OverheadLinesElement) { + Eln.logger.info("Cart is under an overhead wire") + return node + } + coordinate.y++ + } + Eln.logger.info("Cart is not under an overhead wire") + detachResistor() + return null + } + + override fun getMinecartType(): Int { + return 2 + } + + override fun func_145817_o(): Block? { + return Blocks.iron_block + } +} diff --git a/src/main/java/mods/eln/item/ElectricMinecartItem.kt b/src/main/java/mods/eln/item/ElectricMinecartItem.kt new file mode 100644 index 000000000..0409ad7f8 --- /dev/null +++ b/src/main/java/mods/eln/item/ElectricMinecartItem.kt @@ -0,0 +1,41 @@ +package mods.eln.item + +import mods.eln.entity.carts.EntityElectricMinecart +import mods.eln.generic.GenericItemUsingDamageDescriptor +import net.minecraft.block.BlockRailBase +import net.minecraft.entity.player.EntityPlayer +import net.minecraft.item.ItemStack +import net.minecraft.world.World + +class ElectricMinecartItem(name: String) : GenericItemUsingDamageDescriptor(name) { + override fun onItemUse( + stack: ItemStack?, + player: EntityPlayer?, + world: World?, + x: Int, + y: Int, + z: Int, + side: Int, + vx: Float, + vy: Float, + vz: Float + ): Boolean { + if (world == null || stack == null) return false + return if (BlockRailBase.func_150051_a(world.getBlock(x, y, z))) { + if (!world.isRemote) { + val minecart = EntityElectricMinecart( + world, + (x.toFloat() + 0.5f).toDouble(), + (y.toFloat() + 0.5f).toDouble(), + (z.toFloat() + 0.5f).toDouble() + ) + if (stack.hasDisplayName()) { + minecart.setMinecartName(stack.displayName) + } + world.spawnEntityInWorld(minecart) + } + --stack.stackSize + true + } else false + } +} diff --git a/src/main/java/mods/eln/transparentnode/OverheadLines.kt b/src/main/java/mods/eln/transparentnode/OverheadLines.kt new file mode 100644 index 000000000..e35b33691 --- /dev/null +++ b/src/main/java/mods/eln/transparentnode/OverheadLines.kt @@ -0,0 +1,120 @@ +package mods.eln.transparentnode + +import mods.eln.Eln +import mods.eln.cable.CableRenderDescriptor +import mods.eln.i18n.I18N +import mods.eln.misc.Coordinate +import mods.eln.misc.Direction +import mods.eln.misc.LRDU +import mods.eln.misc.LRDUMask +import mods.eln.misc.Obj3D +import mods.eln.misc.Utils.getBlock +import mods.eln.misc.Utils.plotPower +import mods.eln.misc.Utils.plotVolt +import mods.eln.node.NodeBase +import mods.eln.node.transparent.* +import mods.eln.sim.ElectricalLoad +import mods.eln.sim.nbt.NbtElectricalLoad +import java.io.DataInputStream +import java.io.DataOutputStream +import java.io.IOException + +class OverheadLinesDescriptor(name: String, private val obj3D: Obj3D?): TransparentNodeDescriptor(name, OverheadLinesElement::class.java, + OverheadLinesRender::class.java +) { + override fun mustHaveFloor() = false + override fun mustHaveWall() = false + override fun mustHaveWallFrontInverse() = false + override fun mustHaveCeiling() = false + + val centenaryCableRender = CableRenderDescriptor("eln", "sprites/cable.png", + 0.95f, 0.95f) + + fun drawBase() { + obj3D?.draw("OverheadGantry") + } +} + +class OverheadLinesElement(node: TransparentNode?, + transparentNodeDescriptor: TransparentNodeDescriptor +): TransparentNodeElement(node, transparentNodeDescriptor) { + + val electricalLoad = NbtElectricalLoad("electricalLoad") + + override fun initialize() { + Eln.applySmallRs(electricalLoad) + connect() + } + + override fun getConnectionMask(side: Direction, lrdu: LRDU): Int { + return NodeBase.maskElectricalPower + } + + override fun getElectricalLoad(side: Direction, lrdu: LRDU): ElectricalLoad { + return electricalLoad + } + + override fun networkSerialize(stream: DataOutputStream) { + super.networkSerialize(stream) + try { + node!!.lrduCubeMask.getTranslate(front.down()).serialize(stream) + } catch (e: IOException) { + e.printStackTrace() + } + } + + override fun getWaila(): Map { + val info: MutableMap = HashMap() + info[I18N.tr("Voltage")] = plotVolt("", electricalLoad.u) + if (Eln.wailaEasyMode) { + info[I18N.tr("Power")] = plotPower("", electricalLoad.i * electricalLoad.u) + } + return info + } +} + +class OverheadLinesRender(tileEntity: TransparentNodeEntity, transparentNodeDescriptor: TransparentNodeDescriptor): + TransparentNodeElementRender(tileEntity, transparentNodeDescriptor) { + + val desc = transparentNodeDescriptor as OverheadLinesDescriptor + private val eConn = LRDUMask() + + private val boundedSides = mutableListOf() + + init { + val x = tileEntity.xCoord + val y = tileEntity.yCoord + val z = tileEntity.zCoord + boundedSides.add(Coordinate(x + 1, y, z, 0)) + boundedSides.add(Coordinate(x -1, y, z, 0)) + boundedSides.add(Coordinate(x, y + 1, z, 0)) + boundedSides.add(Coordinate(x, y - 1, z, 0)) + boundedSides.add(Coordinate(x, y, z + 1, 0)) + } + + private fun hasBlockAnySideNotBottom(): Boolean { + return boundedSides.any { + getBlock(tileEntity.worldObj, it.x.toDouble(), it.y.toDouble(),it.z.toDouble()).isOpaqueCube + } + } + + override fun draw() { + val locFront = front + if (locFront != null) { + drawCable(locFront.down(), desc.centenaryCableRender, eConn, null, true) + } + + if (hasBlockAnySideNotBottom()) { + desc.drawBase() + } + } + + override fun networkUnserialize(stream: DataInputStream) { + super.networkUnserialize(stream) + try { + eConn.deserialize(stream) + } catch (e: IOException) { + e.printStackTrace() + } + } +} diff --git a/src/main/resources/assets/eln/lang/en_US.lang b/src/main/resources/assets/eln/lang/en_US.lang index 6cdcb8318..c8cbf5406 100644 --- a/src/main/resources/assets/eln/lang/en_US.lang +++ b/src/main/resources/assets/eln/lang/en_US.lang @@ -1159,3 +1159,4 @@ Fabricator.name=Fabricator Multicolor_LED_vuMeter.name=Multicolor LED vuMeter Grid_Switch.name=Grid Switch Direct_Utility_Pole.name=Direct Utility Pole +Overhead_Lines.name=Overhead Lines diff --git a/src/main/resources/assets/eln/model/OverheadGantry/OverheadGantry.mtl b/src/main/resources/assets/eln/model/OverheadGantry/OverheadGantry.mtl new file mode 100644 index 000000000..59a84a38e --- /dev/null +++ b/src/main/resources/assets/eln/model/OverheadGantry/OverheadGantry.mtl @@ -0,0 +1,13 @@ +# Blender MTL File: 'TestCube.blend' +# Material Count: 1 + +newmtl OverheadGantry +Ns 96.078431 +Ka 1.000000 1.000000 1.000000 +Kd 0.640000 0.640000 0.640000 +Ks 0.500000 0.500000 0.500000 +Ke 0.000000 0.000000 0.000000 +Ni 1.000000 +d 1.000000 +illum 2 +map_Kd OverheadGantry.png diff --git a/src/main/resources/assets/eln/model/OverheadGantry/OverheadGantry.obj b/src/main/resources/assets/eln/model/OverheadGantry/OverheadGantry.obj new file mode 100644 index 000000000..de4c175c9 --- /dev/null +++ b/src/main/resources/assets/eln/model/OverheadGantry/OverheadGantry.obj @@ -0,0 +1,54 @@ +# Blender v2.78 (sub 0) OBJ File: 'TestCube.blend' +# www.blender.org +mtllib OverheadGantry.mtl +o OverheadGantry +v 0.500000 -0.500000 -0.500000 +v 0.500000 -0.500000 0.500000 +v -0.500000 -0.500000 0.500000 +v -0.500000 -0.500000 -0.500000 +v 0.500000 0.500000 -0.500000 +v 0.500000 0.500000 0.500000 +v -0.500000 0.500000 0.500000 +v -0.500000 0.500000 -0.500000 +vt 1.0000 0.0000 +vt 0.6667 0.3333 +vt 0.6667 0.0000 +vt 0.6667 0.6667 +vt 0.3333 0.3333 +vt 0.6667 0.3333 +vt 0.3333 0.3333 +vt 0.6667 0.0000 +vt 0.6667 0.3333 +vt 0.0000 0.0000 +vt 0.3333 0.3333 +vt 0.0000 0.3333 +vt 0.3333 1.0000 +vt 0.0000 0.6667 +vt 0.3333 0.6667 +vt 0.3333 0.3333 +vt 0.0000 0.3333 +vt 1.0000 0.3333 +vt 0.3333 0.6667 +vt 0.3333 0.0000 +vt 0.3333 0.0000 +vt 0.0000 1.0000 +vn 0.0000 -1.0000 0.0000 +vn 0.0000 1.0000 -0.0000 +vn 1.0000 -0.0000 0.0000 +vn -0.0000 -0.0000 1.0000 +vn -1.0000 -0.0000 -0.0000 +vn 0.0000 0.0000 -1.0000 +usemtl OverheadGantry +s off +f 2/1/1 4/2/1 1/3/1 +f 8/4/2 6/5/2 5/6/2 +f 5/7/3 2/8/3 1/9/3 +f 6/10/4 3/11/4 2/12/4 +f 3/13/5 8/14/5 4/15/5 +f 1/16/6 8/14/6 5/17/6 +f 2/1/1 3/18/1 4/2/1 +f 8/4/2 7/19/2 6/5/2 +f 5/7/3 6/20/3 2/8/3 +f 6/10/4 7/21/4 3/11/4 +f 3/13/5 7/22/5 8/14/5 +f 1/16/6 4/15/6 8/14/6 diff --git a/src/main/resources/assets/eln/model/OverheadGantry/OverheadGantry.png b/src/main/resources/assets/eln/model/OverheadGantry/OverheadGantry.png new file mode 100644 index 0000000000000000000000000000000000000000..606e2b0403003b29d6570a7f0ffa951dcdecd549 GIT binary patch literal 7037 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 Ybj=qDAC0m Date: Thu, 22 Dec 2022 00:45:07 -0500 Subject: [PATCH 2/7] Progress on the carts. What a mess. --- src/main/java/mods/eln/Eln.java | 11 +- .../entity/carts/EntityElectricMinecart.kt | 98 +++++++--------- .../railroad/GenericRailroadPowerElement.kt | 13 +++ .../{ => railroad}/OverheadLines.kt | 21 +++- .../railroad/UnderTrackPower.kt | 110 ++++++++++++++++++ src/main/resources/assets/eln/lang/en_US.lang | 1 + 6 files changed, 192 insertions(+), 62 deletions(-) create mode 100644 src/main/java/mods/eln/transparentnode/railroad/GenericRailroadPowerElement.kt rename src/main/java/mods/eln/transparentnode/{ => railroad}/OverheadLines.kt (83%) create mode 100644 src/main/java/mods/eln/transparentnode/railroad/UnderTrackPower.kt diff --git a/src/main/java/mods/eln/Eln.java b/src/main/java/mods/eln/Eln.java index c6ddc6014..3d5746d33 100644 --- a/src/main/java/mods/eln/Eln.java +++ b/src/main/java/mods/eln/Eln.java @@ -62,9 +62,6 @@ import mods.eln.sim.ThermalLoadInitializerByPowerDrop; import mods.eln.sim.mna.component.Resistor; import mods.eln.sim.nbt.NbtElectricalLoad; -import mods.eln.simplenode.DeviceProbeBlock; -import mods.eln.simplenode.DeviceProbeEntity; -import mods.eln.simplenode.DeviceProbeNode; import mods.eln.simplenode.computerprobe.ComputerProbeBlock; import mods.eln.simplenode.computerprobe.ComputerProbeEntity; import mods.eln.simplenode.computerprobe.ComputerProbeNode; @@ -145,6 +142,8 @@ import mods.eln.transparentnode.heatfurnace.HeatFurnaceDescriptor; import mods.eln.transparentnode.powercapacitor.PowerCapacitorDescriptor; import mods.eln.transparentnode.powerinductor.PowerInductorDescriptor; +import mods.eln.transparentnode.railroad.OverheadLinesDescriptor; +import mods.eln.transparentnode.railroad.UnderTrackPowerDescriptor; import mods.eln.transparentnode.solarpanel.SolarPanelDescriptor; import mods.eln.transparentnode.teleporter.TeleporterDescriptor; import mods.eln.transparentnode.teleporter.TeleporterElement; @@ -784,6 +783,12 @@ private void registerOverheadWires(int id) { obj.getObj("OverheadGantry")); transparentNodeItem.addDescriptor(subId + (id << 6), desc); } + { + subId = 1; + UnderTrackPowerDescriptor desc = new UnderTrackPowerDescriptor("Under Track Power", + obj.getObj("OverheadGantry")); + transparentNodeItem.addDescriptor(subId + (id << 6), desc); + } } private void registerGridDevices(int id) { diff --git a/src/main/java/mods/eln/entity/carts/EntityElectricMinecart.kt b/src/main/java/mods/eln/entity/carts/EntityElectricMinecart.kt index 355cff13a..593d36d18 100644 --- a/src/main/java/mods/eln/entity/carts/EntityElectricMinecart.kt +++ b/src/main/java/mods/eln/entity/carts/EntityElectricMinecart.kt @@ -1,28 +1,19 @@ package mods.eln.entity.carts -import mods.eln.Eln import mods.eln.misc.Coordinate import mods.eln.node.NodeManager -import mods.eln.sim.mna.component.Resistor -import mods.eln.sim.mna.misc.MnaConst -import mods.eln.transparentnode.OverheadLinesElement +import mods.eln.transparentnode.railroad.GenericRailroadPowerElement +import mods.eln.transparentnode.railroad.OverheadLinesElement +import mods.eln.transparentnode.railroad.UnderTrackPowerElement import net.minecraft.block.Block import net.minecraft.entity.item.EntityMinecart import net.minecraft.init.Blocks -import net.minecraft.util.DamageSource import net.minecraft.world.World class EntityElectricMinecart(world: World, x: Double, y: Double, z: Double): EntityMinecart(world, x, y, z) { - private var lastOverheadWires: OverheadLinesElement? = null - private var lastRunState = false - + private var lastPowerElement: GenericRailroadPowerElement? = null private val locomotiveResistance = 500.0 - val resistor = Resistor() - - init { - resistor.r = locomotiveResistance - } override fun func_145821_a( blockX: Int, @@ -34,50 +25,37 @@ class EntityElectricMinecart(world: World, x: Double, y: Double, z: Double): Ent direction: Int ) { super.func_145821_a(blockX, blockY, blockZ, speed + 1, drag, block, direction) - val overheadWires = getOverheadWires(Coordinate(posX.toInt(), posY.toInt(), posZ.toInt(), worldObj)) ?: return - // TODO: Configure isRunning to act as the cart needs more battery power in an internal buffer - // Allow the cart to move when there is power in the buffer - // Control the cart ... somehow. Just turn it on full bore? lol - takeOverheadPower(overheadWires, true) - } - - private fun attachResistor(overheadWires: OverheadLinesElement) { - if (overheadWires != lastOverheadWires) - detachResistor() - lastOverheadWires = overheadWires - resistor.connectTo(overheadWires.electricalLoad, null) - overheadWires.reconnect() - overheadWires.needPublish() - } - - private fun detachResistor() { - if (resistor.subSystem != null) - resistor.subSystem.removeComponent(resistor) - resistor.dirty() - updateLastWire() - lastOverheadWires = null - } - - private fun updateLastWire() { - lastOverheadWires?.reconnect() - lastOverheadWires?.needPublish() - } - override fun killMinecart(p_94095_1_: DamageSource?) { - super.killMinecart(p_94095_1_) - detachResistor() + val cartCoordinate = Coordinate(posX.toInt(), posY.toInt(), posZ.toInt(), worldObj) + val overheadWires = getOverheadWires(cartCoordinate) + val underTrackWires = getUnderTrackWires(cartCoordinate) + + /** + * Minecarts don't trigger an event in this class when they are removed in creative mode + * This means we need to use the actual element of the Overhead Lines or Under Track Power classes to handle + * the resistors and that no MNA elements can be owned by this class. + * + * The Replicator does this by registering a process that will remove the resistor from the simulator thread + * periodically and registers that apart from the entity (ish) although it probably isn't tested well and + * theoretically a creative mode removed replicator could be buggy or something + * + * Anyhow. Best way to implement this is to ask for the nearest wires (prefer the under track ones) and then + * pull power from that and save that. Might be able to make some generics here but I think that there + * may need to be differing code for sending the under power track rendering as opposed to the over track ones. + * This is because I want to render a wire _above_ the block if there's a track there. Presumably by rendering + * a simple quad over the top of the rails by a small distance and detecting the rail rendering for the rail + * above the device to make sure that we 'follow' the track with the custom renderer. + * + * This will have to be raw OpenGL code, similar to how the CableRender class works. It's probably not going + * to be super fun but it does help that the code will be 2D quads (rendered in 3D space/orientation). + * + * Some various textures may be required but I'm hoping I can sorta follow the cable render by rendering a quad + * in each direction and possibly also a center quad that is always rendered - and making it render diagonally + * in 3D space if the rail is going up or something. Turns should also be detectable. Might need to do something + * for mods that add tracks or have a default of just rendering the center square (2x2 pixels in MC) as wire. + */ } - private fun takeOverheadPower(overheadWires: OverheadLinesElement, isRunning: Boolean) { - Eln.logger.info("Voltage over the cart is ${overheadWires.electricalLoad.u}") - - if (isRunning != lastRunState) { - resistor.r = if (isRunning) locomotiveResistance else MnaConst.highImpedance - resistor.dirty() - } - - attachResistor(overheadWires) - } private fun getOverheadWires(coordinate: Coordinate): OverheadLinesElement? { // Pass coordinate of tracks and check vertically the next 3-4 blocks @@ -86,13 +64,19 @@ class EntityElectricMinecart(world: World, x: Double, y: Double, z: Double): Ent coordinate.y val node = NodeManager.instance!!.getTransparentNodeFromCoordinate(coordinate) if (node is OverheadLinesElement) { - Eln.logger.info("Cart is under an overhead wire") return node } coordinate.y++ } - Eln.logger.info("Cart is not under an overhead wire") - detachResistor() + return null + } + + private fun getUnderTrackWires(coordinate: Coordinate): UnderTrackPowerElement? { + coordinate.y -= 1 // check the block below the cart + val node = NodeManager.instance!!.getTransparentNodeFromCoordinate(coordinate) + if (node is UnderTrackPowerElement) { + return node + } return null } diff --git a/src/main/java/mods/eln/transparentnode/railroad/GenericRailroadPowerElement.kt b/src/main/java/mods/eln/transparentnode/railroad/GenericRailroadPowerElement.kt new file mode 100644 index 000000000..0ddfafcd8 --- /dev/null +++ b/src/main/java/mods/eln/transparentnode/railroad/GenericRailroadPowerElement.kt @@ -0,0 +1,13 @@ +package mods.eln.transparentnode.railroad + +import mods.eln.entity.carts.EntityElectricMinecart +import mods.eln.node.transparent.TransparentNode +import mods.eln.node.transparent.TransparentNodeDescriptor +import mods.eln.node.transparent.TransparentNodeElement + +abstract class GenericRailroadPowerElement( + node: TransparentNode?, transparentNodeDescriptor: TransparentNodeDescriptor +): TransparentNodeElement(node, transparentNodeDescriptor) { + open fun registerCart(cart: EntityElectricMinecart) {} + open fun deregisterCart(cart: EntityElectricMinecart) {} +} diff --git a/src/main/java/mods/eln/transparentnode/OverheadLines.kt b/src/main/java/mods/eln/transparentnode/railroad/OverheadLines.kt similarity index 83% rename from src/main/java/mods/eln/transparentnode/OverheadLines.kt rename to src/main/java/mods/eln/transparentnode/railroad/OverheadLines.kt index e35b33691..7ca916dff 100644 --- a/src/main/java/mods/eln/transparentnode/OverheadLines.kt +++ b/src/main/java/mods/eln/transparentnode/railroad/OverheadLines.kt @@ -1,4 +1,4 @@ -package mods.eln.transparentnode +package mods.eln.transparentnode.railroad import mods.eln.Eln import mods.eln.cable.CableRenderDescriptor @@ -37,12 +37,14 @@ class OverheadLinesDescriptor(name: String, private val obj3D: Obj3D?): Transpar class OverheadLinesElement(node: TransparentNode?, transparentNodeDescriptor: TransparentNodeDescriptor -): TransparentNodeElement(node, transparentNodeDescriptor) { +): GenericRailroadPowerElement(node, transparentNodeDescriptor) { val electricalLoad = NbtElectricalLoad("electricalLoad") override fun initialize() { Eln.applySmallRs(electricalLoad) + electricalLoad.setCanBeSimplifiedByLine(true) + electricalLoadList.add(electricalLoad) connect() } @@ -69,6 +71,21 @@ class OverheadLinesElement(node: TransparentNode?, if (Eln.wailaEasyMode) { info[I18N.tr("Power")] = plotPower("", electricalLoad.i * electricalLoad.u) } + val ss = electricalLoad.subSystem + if (ss != null) { + val subSystemSize = electricalLoad.subSystem.component.size + var textColor = "" + textColor = if (subSystemSize <= 8) { + "§a" + } else if (subSystemSize <= 15) { + "§6" + } else { + "§c" + } + info[I18N.tr("Subsystem Matrix Size")] = textColor + subSystemSize + } else { + info[I18N.tr("Subsystem Matrix Size")] = "§cnull SubSystem" + } return info } } diff --git a/src/main/java/mods/eln/transparentnode/railroad/UnderTrackPower.kt b/src/main/java/mods/eln/transparentnode/railroad/UnderTrackPower.kt new file mode 100644 index 000000000..14da9faed --- /dev/null +++ b/src/main/java/mods/eln/transparentnode/railroad/UnderTrackPower.kt @@ -0,0 +1,110 @@ +package mods.eln.transparentnode.railroad + +import mods.eln.Eln +import mods.eln.cable.CableRenderDescriptor +import mods.eln.i18n.I18N +import mods.eln.misc.* +import mods.eln.node.NodeBase +import mods.eln.node.transparent.* +import mods.eln.sim.ElectricalLoad +import mods.eln.sim.nbt.NbtElectricalLoad +import java.io.DataInputStream +import java.io.DataOutputStream +import java.io.IOException + +class UnderTrackPowerDescriptor(name: String, private val obj3D: Obj3D?): TransparentNodeDescriptor(name, UnderTrackPowerElement::class.java, + UnderTrackPowerRender::class.java +) { + override fun checkCanPlace(coord: Coordinate?, front: Direction): String? { + // TODO: Require support from at least one side, except top + return null + } + + val thirdRailRender = CableRenderDescriptor("eln", "sprites/cable.png", + 0.95f, 0.95f) + + fun drawBase() { + obj3D?.draw("OverheadGantry") + } +} + +class UnderTrackPowerElement(node: TransparentNode?, + transparentNodeDescriptor: TransparentNodeDescriptor +): GenericRailroadPowerElement(node, transparentNodeDescriptor) { + + val electricalLoad = NbtElectricalLoad("electricalLoad") + + override fun initialize() { + Eln.applySmallRs(electricalLoad) + electricalLoad.setCanBeSimplifiedByLine(true) + electricalLoadList.add(electricalLoad) + connect() + } + + override fun getConnectionMask(side: Direction, lrdu: LRDU): Int { + return NodeBase.maskElectricalPower + } + + override fun getElectricalLoad(side: Direction, lrdu: LRDU): ElectricalLoad { + return electricalLoad + } + + override fun networkSerialize(stream: DataOutputStream) { + super.networkSerialize(stream) + try { + node!!.lrduCubeMask.getTranslate(front.down()).serialize(stream) + } catch (e: IOException) { + e.printStackTrace() + } + } + + override fun getWaila(): Map { + val info: MutableMap = HashMap() + info[I18N.tr("Voltage")] = Utils.plotVolt("", electricalLoad.u) + if (Eln.wailaEasyMode) { + info[I18N.tr("Power")] = Utils.plotPower("", electricalLoad.i * electricalLoad.u) + } + val ss = electricalLoad.subSystem + if (ss != null) { + val subSystemSize = electricalLoad.subSystem.component.size + var textColor = "" + textColor = if (subSystemSize <= 8) { + "§a" + } else if (subSystemSize <= 15) { + "§6" + } else { + "§c" + } + info[I18N.tr("Subsystem Matrix Size")] = textColor + subSystemSize + } else { + info[I18N.tr("Subsystem Matrix Size")] = "§cnull SubSystem" + } + return info + } +} + +class UnderTrackPowerRender(tileEntity: TransparentNodeEntity, transparentNodeDescriptor: TransparentNodeDescriptor): + TransparentNodeElementRender(tileEntity, transparentNodeDescriptor) { + + val desc = transparentNodeDescriptor as UnderTrackPowerDescriptor + private val eConn = LRDUMask() + + init { + val x = tileEntity.xCoord + val y = tileEntity.yCoord + val z = tileEntity.zCoord + } + + override fun draw() { + desc.drawBase() + } + + override fun networkUnserialize(stream: DataInputStream) { + super.networkUnserialize(stream) + try { + eConn.deserialize(stream) + } catch (e: IOException) { + e.printStackTrace() + } + } +} diff --git a/src/main/resources/assets/eln/lang/en_US.lang b/src/main/resources/assets/eln/lang/en_US.lang index c8cbf5406..3699bb451 100644 --- a/src/main/resources/assets/eln/lang/en_US.lang +++ b/src/main/resources/assets/eln/lang/en_US.lang @@ -1160,3 +1160,4 @@ Multicolor_LED_vuMeter.name=Multicolor LED vuMeter Grid_Switch.name=Grid Switch Direct_Utility_Pole.name=Direct Utility Pole Overhead_Lines.name=Overhead Lines +Under_Track_Power.name=Under Track Power From 073a42bcbceaa8775506d94e5d20a6f4b4092f8d Mon Sep 17 00:00:00 2001 From: Jared Dunbar Date: Fri, 23 Dec 2022 02:14:00 -0500 Subject: [PATCH 3/7] The carts move. Electrical power kinda works but the MNA matrix isn't built correctly yet. Cart physics work. --- src/main/java/mods/eln/Eln.java | 2 + .../entity/carts/EntityElectricMinecart.kt | 126 +++++++++++++----- .../railroad/GenericRailroadPowerElement.kt | 13 -- .../transparentnode/railroad/OverheadLines.kt | 41 +++++- .../railroad/PoweredMinecartSimulationData.kt | 17 +++ .../PoweredMinecartSimulationSingleton.kt | 65 +++++++++ .../railroad/RailroadPowerInterface.kt | 17 +++ .../railroad/RailroadResistorSlowProcess.kt | 16 +++ .../railroad/UnderTrackPower.kt | 41 +++++- 9 files changed, 289 insertions(+), 49 deletions(-) delete mode 100644 src/main/java/mods/eln/transparentnode/railroad/GenericRailroadPowerElement.kt create mode 100644 src/main/java/mods/eln/transparentnode/railroad/PoweredMinecartSimulationData.kt create mode 100644 src/main/java/mods/eln/transparentnode/railroad/PoweredMinecartSimulationSingleton.kt create mode 100644 src/main/java/mods/eln/transparentnode/railroad/RailroadPowerInterface.kt create mode 100644 src/main/java/mods/eln/transparentnode/railroad/RailroadResistorSlowProcess.kt diff --git a/src/main/java/mods/eln/Eln.java b/src/main/java/mods/eln/Eln.java index 3d5746d33..467a51406 100644 --- a/src/main/java/mods/eln/Eln.java +++ b/src/main/java/mods/eln/Eln.java @@ -783,12 +783,14 @@ private void registerOverheadWires(int id) { obj.getObj("OverheadGantry")); transparentNodeItem.addDescriptor(subId + (id << 6), desc); } + /* { subId = 1; UnderTrackPowerDescriptor desc = new UnderTrackPowerDescriptor("Under Track Power", obj.getObj("OverheadGantry")); transparentNodeItem.addDescriptor(subId + (id << 6), desc); } + */ } private void registerGridDevices(int id) { diff --git a/src/main/java/mods/eln/entity/carts/EntityElectricMinecart.kt b/src/main/java/mods/eln/entity/carts/EntityElectricMinecart.kt index 593d36d18..403eb9489 100644 --- a/src/main/java/mods/eln/entity/carts/EntityElectricMinecart.kt +++ b/src/main/java/mods/eln/entity/carts/EntityElectricMinecart.kt @@ -1,19 +1,80 @@ package mods.eln.entity.carts +import mods.eln.Eln import mods.eln.misc.Coordinate +import mods.eln.misc.Utils import mods.eln.node.NodeManager -import mods.eln.transparentnode.railroad.GenericRailroadPowerElement +import mods.eln.sim.mna.misc.MnaConst import mods.eln.transparentnode.railroad.OverheadLinesElement +import mods.eln.transparentnode.railroad.PoweredMinecartSimulationSingleton +import mods.eln.transparentnode.railroad.RailroadPowerInterface import mods.eln.transparentnode.railroad.UnderTrackPowerElement import net.minecraft.block.Block import net.minecraft.entity.item.EntityMinecart +import net.minecraft.entity.player.EntityPlayer import net.minecraft.init.Blocks +import net.minecraft.server.MinecraftServer import net.minecraft.world.World +import kotlin.math.abs +import kotlin.math.sign class EntityElectricMinecart(world: World, x: Double, y: Double, z: Double): EntityMinecart(world, x, y, z) { - private var lastPowerElement: GenericRailroadPowerElement? = null - private val locomotiveResistance = 500.0 + private var lastPowerElement: RailroadPowerInterface? = null + private val locomotiveMaximumResistance = 200.0 + var energyBufferTargetJoules = 10_000.0 + var energyBufferJoules = 0.0 + + var count = 0 + + override fun onUpdate() { + super.onUpdate() + val cartCoordinate = Coordinate(posX.toInt(), posY.toInt(), posZ.toInt(), worldObj) + val overheadWires = getOverheadWires(cartCoordinate) + val underTrackWires = getUnderTrackWires(cartCoordinate) + + when (val oldPowerElement = lastPowerElement) { + overheadWires, underTrackWires, null -> { + // references existing overhead wires or under track wires, or nothing + } + else -> { + // references old overhead wires or under track wires that are not the current ones + energyBufferJoules += PoweredMinecartSimulationSingleton.cartCollectEnergy(this) + Eln.logger.info("Deregister cart from $oldPowerElement") + oldPowerElement.deregisterCart(this) + } + } + + val currentElement: RailroadPowerInterface? = underTrackWires ?: overheadWires + + if (currentElement != null) { + if (currentElement != lastPowerElement) { + Eln.logger.info("Register cart to $currentElement") + currentElement.registerCart(this) + } + + if (energyBufferJoules < energyBufferTargetJoules) { + PoweredMinecartSimulationSingleton.powerCart(this, locomotiveMaximumResistance, 0.1) + } else { + PoweredMinecartSimulationSingleton.powerCart(this, MnaConst.highImpedance, 1.0) + } + energyBufferJoules += PoweredMinecartSimulationSingleton.cartCollectEnergy(this) + } + + if (count > 20) count = 0 + if (count == 0) { + MinecraftServer.getServer().entityWorld.playerEntities.forEach {it -> + val player = it as EntityPlayer + Utils.addChatMessage(player, "Cart Energy: ${Utils.plotEnergy(energyBufferJoules)}") + } + } + count++ + + lastPowerElement = currentElement + } + + var pushX: Double = 0.0 + var pushZ: Double = 0.0 override fun func_145821_a( blockX: Int, @@ -25,37 +86,34 @@ class EntityElectricMinecart(world: World, x: Double, y: Double, z: Double): Ent direction: Int ) { super.func_145821_a(blockX, blockY, blockZ, speed + 1, drag, block, direction) + if (energyBufferJoules > 0) { + val maxEnergy = 40.0 + var energyAvailable = maxEnergy + if (energyBufferJoules < maxEnergy) { + energyAvailable = energyBufferJoules + } - val cartCoordinate = Coordinate(posX.toInt(), posY.toInt(), posZ.toInt(), worldObj) - val overheadWires = getOverheadWires(cartCoordinate) - val underTrackWires = getUnderTrackWires(cartCoordinate) + val startingThreshold = 0.0005 - /** - * Minecarts don't trigger an event in this class when they are removed in creative mode - * This means we need to use the actual element of the Overhead Lines or Under Track Power classes to handle - * the resistors and that no MNA elements can be owned by this class. - * - * The Replicator does this by registering a process that will remove the resistor from the simulator thread - * periodically and registers that apart from the entity (ish) although it probably isn't tested well and - * theoretically a creative mode removed replicator could be buggy or something - * - * Anyhow. Best way to implement this is to ask for the nearest wires (prefer the under track ones) and then - * pull power from that and save that. Might be able to make some generics here but I think that there - * may need to be differing code for sending the under power track rendering as opposed to the over track ones. - * This is because I want to render a wire _above_ the block if there's a track there. Presumably by rendering - * a simple quad over the top of the rails by a small distance and detecting the rail rendering for the rail - * above the device to make sure that we 'follow' the track with the custom renderer. - * - * This will have to be raw OpenGL code, similar to how the CableRender class works. It's probably not going - * to be super fun but it does help that the code will be 2D quads (rendered in 3D space/orientation). - * - * Some various textures may be required but I'm hoping I can sorta follow the cable render by rendering a quad - * in each direction and possibly also a center quad that is always rendered - and making it render diagonally - * in 3D space if the rail is going up or something. Turns should also be detectable. Might need to do something - * for mods that add tracks or have a default of just rendering the center square (2x2 pixels in MC) as wire. - */ - } + if (abs(motionX) >= startingThreshold || abs(motionZ) >= startingThreshold) { + if (abs(motionX) < 0.5 && abs(motionZ) < 0.5) { + pushX = motionX.sign * 0.05 * (energyAvailable / maxEnergy) + pushZ = motionZ.sign * 0.05 * (energyAvailable / maxEnergy) + energyBufferJoules -= energyAvailable + } + } + } + + Eln.logger.info("Push: ($pushX, $pushZ)") + + motionX += pushX + motionZ += pushZ + + Eln.logger.info("Speed: ($motionX, $motionZ)") + pushX = 0.0 + pushZ = 0.0 + } private fun getOverheadWires(coordinate: Coordinate): OverheadLinesElement? { // Pass coordinate of tracks and check vertically the next 3-4 blocks @@ -81,10 +139,14 @@ class EntityElectricMinecart(world: World, x: Double, y: Double, z: Double): Ent } override fun getMinecartType(): Int { - return 2 + return -1 } override fun func_145817_o(): Block? { return Blocks.iron_block } + + override fun func_145820_n(): Block? { + return Blocks.iron_block + } } diff --git a/src/main/java/mods/eln/transparentnode/railroad/GenericRailroadPowerElement.kt b/src/main/java/mods/eln/transparentnode/railroad/GenericRailroadPowerElement.kt deleted file mode 100644 index 0ddfafcd8..000000000 --- a/src/main/java/mods/eln/transparentnode/railroad/GenericRailroadPowerElement.kt +++ /dev/null @@ -1,13 +0,0 @@ -package mods.eln.transparentnode.railroad - -import mods.eln.entity.carts.EntityElectricMinecart -import mods.eln.node.transparent.TransparentNode -import mods.eln.node.transparent.TransparentNodeDescriptor -import mods.eln.node.transparent.TransparentNodeElement - -abstract class GenericRailroadPowerElement( - node: TransparentNode?, transparentNodeDescriptor: TransparentNodeDescriptor -): TransparentNodeElement(node, transparentNodeDescriptor) { - open fun registerCart(cart: EntityElectricMinecart) {} - open fun deregisterCart(cart: EntityElectricMinecart) {} -} diff --git a/src/main/java/mods/eln/transparentnode/railroad/OverheadLines.kt b/src/main/java/mods/eln/transparentnode/railroad/OverheadLines.kt index 7ca916dff..d6bc2a023 100644 --- a/src/main/java/mods/eln/transparentnode/railroad/OverheadLines.kt +++ b/src/main/java/mods/eln/transparentnode/railroad/OverheadLines.kt @@ -2,6 +2,7 @@ package mods.eln.transparentnode.railroad import mods.eln.Eln import mods.eln.cable.CableRenderDescriptor +import mods.eln.entity.carts.EntityElectricMinecart import mods.eln.i18n.I18N import mods.eln.misc.Coordinate import mods.eln.misc.Direction @@ -14,7 +15,10 @@ import mods.eln.misc.Utils.plotVolt import mods.eln.node.NodeBase import mods.eln.node.transparent.* import mods.eln.sim.ElectricalLoad +import mods.eln.sim.mna.component.Resistor +import mods.eln.sim.mna.misc.MnaConst import mods.eln.sim.nbt.NbtElectricalLoad +import mods.eln.transparentnode.railroad.PoweredMinecartSimulationSingleton.poweredMinecartSimulationData import java.io.DataInputStream import java.io.DataOutputStream import java.io.IOException @@ -37,14 +41,17 @@ class OverheadLinesDescriptor(name: String, private val obj3D: Obj3D?): Transpar class OverheadLinesElement(node: TransparentNode?, transparentNodeDescriptor: TransparentNodeDescriptor -): GenericRailroadPowerElement(node, transparentNodeDescriptor) { +): TransparentNodeElement(node, transparentNodeDescriptor), RailroadPowerInterface { val electricalLoad = NbtElectricalLoad("electricalLoad") - override fun initialize() { + init { Eln.applySmallRs(electricalLoad) electricalLoad.setCanBeSimplifiedByLine(true) electricalLoadList.add(electricalLoad) + } + + override fun initialize() { connect() } @@ -88,6 +95,36 @@ class OverheadLinesElement(node: TransparentNode?, } return info } + + override fun registerCart(cart: EntityElectricMinecart) { + if (cart !in poweredMinecartSimulationData.map { it.minecart }) { + val resistor = Resistor() + resistor.connectTo(electricalLoad, null) + resistor.r = MnaConst.highImpedance + electricalComponentList.add(resistor) + val rrsp = RailroadResistorSlowProcess(this, cart, 0.05) + Eln.simulator.addSlowProcess(rrsp) + poweredMinecartSimulationData.add(PoweredMinecartSimulationData(cart, resistor, rrsp, this)) + this.electricalLoad.subSystem.invalidate() + this.needPublish() + } + } + + override fun deregisterCart(cart: EntityElectricMinecart) { + val search = poweredMinecartSimulationData.filter { it.minecart == cart } + if (search.isNotEmpty()) { + search.forEach { + if (it.owningElement == this) { + electricalComponentList.remove(it.resistor) + it.resistor.breakConnection() + this.electricalLoad.subSystem.invalidate() + Eln.simulator.removeSlowProcess(it.slowProcess) + poweredMinecartSimulationData.remove(it) + this.needPublish() + } + } + } + } } class OverheadLinesRender(tileEntity: TransparentNodeEntity, transparentNodeDescriptor: TransparentNodeDescriptor): diff --git a/src/main/java/mods/eln/transparentnode/railroad/PoweredMinecartSimulationData.kt b/src/main/java/mods/eln/transparentnode/railroad/PoweredMinecartSimulationData.kt new file mode 100644 index 000000000..d5662535e --- /dev/null +++ b/src/main/java/mods/eln/transparentnode/railroad/PoweredMinecartSimulationData.kt @@ -0,0 +1,17 @@ +package mods.eln.transparentnode.railroad + +import mods.eln.entity.carts.EntityElectricMinecart +import mods.eln.sim.mna.component.Resistor + +/** + * @param minecart the electrical minecart entity instance + * @param resistor the resistor instance used by this minecart + * @param slowProcess the IProcess slow process for processing the resistor removal after the time specified. + * @param owningElement the element this minecart was most recently connected to + */ +data class PoweredMinecartSimulationData( + val minecart: EntityElectricMinecart, + var resistor: Resistor, + var slowProcess: RailroadResistorSlowProcess, + val owningElement: RailroadPowerInterface + ) diff --git a/src/main/java/mods/eln/transparentnode/railroad/PoweredMinecartSimulationSingleton.kt b/src/main/java/mods/eln/transparentnode/railroad/PoweredMinecartSimulationSingleton.kt new file mode 100644 index 000000000..b1c101ff8 --- /dev/null +++ b/src/main/java/mods/eln/transparentnode/railroad/PoweredMinecartSimulationSingleton.kt @@ -0,0 +1,65 @@ +package mods.eln.transparentnode.railroad + +import mods.eln.Eln +import mods.eln.entity.carts.EntityElectricMinecart +import mods.eln.node.transparent.TransparentNodeElement +import mods.eln.sim.IProcess + +object PoweredMinecartSimulationSingleton { + val poweredMinecartSimulationData: MutableList = mutableListOf() + val minecartEnergyCache: MutableMap = mutableMapOf() + + init { + val energyMover = IProcess { time -> + poweredMinecartSimulationData.forEach { + moveEnergy(it.minecart, it.resistor.p * time) + } + } + Eln.simulator.addSlowPostProcess(energyMover) + } + + /** + * powerCart - set the power demands of the cart + * @param cart electric minecart entity instance + * @param resistance the resistance of the motor + * @param time time in seconds to request this power for the cart + */ + fun powerCart(cart: EntityElectricMinecart, resistance: Double, time: Double) { + val search = poweredMinecartSimulationData.filter { it.minecart == cart } + if (search.isEmpty()) return + val cartData = search.first() + if (cartData.resistor.r != resistance) + if (cartData.owningElement is TransparentNodeElement) + cartData.owningElement.needPublish() + cartData.resistor.r = resistance + cartData.slowProcess.timeLeft = time + } + + /** + * cartCollectEnergy - collect the joules from the wire for the cart + * @param cart electric minecart entity instance + * @return the joules of energy from the cable + */ + fun cartCollectEnergy(cart: EntityElectricMinecart): Double { + return if (cart in minecartEnergyCache) { + val currentEnergy = minecartEnergyCache[cart]!! + minecartEnergyCache.remove(cart) + currentEnergy + } else { + 0.0 + } + } + + /** + * moveEnergy - move energy to the cart's energy cache for collection by the entity + * @param cart electric minecart entity instance + * @param joules the quantity of energy in joules + */ + private fun moveEnergy(cart: EntityElectricMinecart, joules: Double) { + minecartEnergyCache[cart] = if (cart in minecartEnergyCache) { + minecartEnergyCache[cart]!! + joules + } else { + joules + } + } +} diff --git a/src/main/java/mods/eln/transparentnode/railroad/RailroadPowerInterface.kt b/src/main/java/mods/eln/transparentnode/railroad/RailroadPowerInterface.kt new file mode 100644 index 000000000..afc8b2626 --- /dev/null +++ b/src/main/java/mods/eln/transparentnode/railroad/RailroadPowerInterface.kt @@ -0,0 +1,17 @@ +package mods.eln.transparentnode.railroad + +import mods.eln.entity.carts.EntityElectricMinecart + +interface RailroadPowerInterface { + /** + * registerCart - register a cart against a power element + * @param cart electric minecart entity instance + */ + fun registerCart(cart: EntityElectricMinecart) + + /** + * deregisterCart - de-register a cart against a power element + * @param cart electric minecart entity instance + */ + fun deregisterCart(cart: EntityElectricMinecart) +} diff --git a/src/main/java/mods/eln/transparentnode/railroad/RailroadResistorSlowProcess.kt b/src/main/java/mods/eln/transparentnode/railroad/RailroadResistorSlowProcess.kt new file mode 100644 index 000000000..16d8da02f --- /dev/null +++ b/src/main/java/mods/eln/transparentnode/railroad/RailroadResistorSlowProcess.kt @@ -0,0 +1,16 @@ +package mods.eln.transparentnode.railroad + +import mods.eln.Eln +import mods.eln.entity.carts.EntityElectricMinecart +import mods.eln.sim.IProcess + +class RailroadResistorSlowProcess(val rpi: RailroadPowerInterface, val cart: EntityElectricMinecart, var timeLeft: Double): IProcess { + + override fun process(time: Double) { + if (timeLeft - time < 0.0) { + Eln.logger.warn("Automatically unregistered minecart after timeout") + rpi.deregisterCart(cart) + } + timeLeft -= time + } +} diff --git a/src/main/java/mods/eln/transparentnode/railroad/UnderTrackPower.kt b/src/main/java/mods/eln/transparentnode/railroad/UnderTrackPower.kt index 14da9faed..4772f6c71 100644 --- a/src/main/java/mods/eln/transparentnode/railroad/UnderTrackPower.kt +++ b/src/main/java/mods/eln/transparentnode/railroad/UnderTrackPower.kt @@ -2,11 +2,15 @@ package mods.eln.transparentnode.railroad import mods.eln.Eln import mods.eln.cable.CableRenderDescriptor +import mods.eln.entity.carts.EntityElectricMinecart import mods.eln.i18n.I18N import mods.eln.misc.* import mods.eln.node.NodeBase import mods.eln.node.transparent.* import mods.eln.sim.ElectricalLoad +import mods.eln.sim.IProcess +import mods.eln.sim.mna.component.Resistor +import mods.eln.sim.mna.misc.MnaConst import mods.eln.sim.nbt.NbtElectricalLoad import java.io.DataInputStream import java.io.DataOutputStream @@ -30,14 +34,17 @@ class UnderTrackPowerDescriptor(name: String, private val obj3D: Obj3D?): Transp class UnderTrackPowerElement(node: TransparentNode?, transparentNodeDescriptor: TransparentNodeDescriptor -): GenericRailroadPowerElement(node, transparentNodeDescriptor) { +): TransparentNodeElement(node, transparentNodeDescriptor), RailroadPowerInterface { val electricalLoad = NbtElectricalLoad("electricalLoad") - override fun initialize() { + init { Eln.applySmallRs(electricalLoad) electricalLoad.setCanBeSimplifiedByLine(true) electricalLoadList.add(electricalLoad) + } + + override fun initialize() { connect() } @@ -81,6 +88,36 @@ class UnderTrackPowerElement(node: TransparentNode?, } return info } + + override fun registerCart(cart: EntityElectricMinecart) { + if (cart !in PoweredMinecartSimulationSingleton.poweredMinecartSimulationData.map { it.minecart }) { + val resistor = Resistor() + resistor.connectTo(electricalLoad, null) + resistor.r = MnaConst.highImpedance + electricalComponentList.add(resistor) + val rrsp = RailroadResistorSlowProcess(this, cart, 0.05) + Eln.simulator.addSlowProcess(rrsp) + PoweredMinecartSimulationSingleton.poweredMinecartSimulationData.add(PoweredMinecartSimulationData(cart, resistor, rrsp, this)) + this.electricalLoad.subSystem.invalidate() + this.needPublish() + } + } + + override fun deregisterCart(cart: EntityElectricMinecart) { + val search = PoweredMinecartSimulationSingleton.poweredMinecartSimulationData.filter { it.minecart == cart } + if (search.isNotEmpty()) { + search.forEach { + if (it.owningElement == this) { + electricalComponentList.remove(it.resistor) + it.resistor.breakConnection() + this.electricalLoad.subSystem.invalidate() + Eln.simulator.removeSlowProcess(it.slowProcess) + PoweredMinecartSimulationSingleton.poweredMinecartSimulationData.remove(it) + this.needPublish() + } + } + } + } } class UnderTrackPowerRender(tileEntity: TransparentNodeEntity, transparentNodeDescriptor: TransparentNodeDescriptor): From b9d3b0ce2397dded8219ff4e8152f885618af652 Mon Sep 17 00:00:00 2001 From: Jared Dunbar Date: Fri, 6 Jan 2023 00:31:28 -0500 Subject: [PATCH 4/7] Progress on electric minecarts. Electrical code is done. --- .../entity/carts/EntityElectricMinecart.kt | 4 +- .../transparentnode/railroad/OverheadLines.kt | 45 ++++++++++++++----- .../railroad/PoweredMinecartSimulationData.kt | 4 ++ .../PoweredMinecartSimulationSingleton.kt | 3 ++ .../railroad/UnderTrackPower.kt | 30 +++++++++---- 5 files changed, 63 insertions(+), 23 deletions(-) diff --git a/src/main/java/mods/eln/entity/carts/EntityElectricMinecart.kt b/src/main/java/mods/eln/entity/carts/EntityElectricMinecart.kt index 403eb9489..d7ef123fc 100644 --- a/src/main/java/mods/eln/entity/carts/EntityElectricMinecart.kt +++ b/src/main/java/mods/eln/entity/carts/EntityElectricMinecart.kt @@ -104,12 +104,12 @@ class EntityElectricMinecart(world: World, x: Double, y: Double, z: Double): Ent } } - Eln.logger.info("Push: ($pushX, $pushZ)") + //Eln.logger.info("Push: ($pushX, $pushZ)") motionX += pushX motionZ += pushZ - Eln.logger.info("Speed: ($motionX, $motionZ)") + //Eln.logger.info("Speed: ($motionX, $motionZ)") pushX = 0.0 pushZ = 0.0 diff --git a/src/main/java/mods/eln/transparentnode/railroad/OverheadLines.kt b/src/main/java/mods/eln/transparentnode/railroad/OverheadLines.kt index d6bc2a023..e84abf67b 100644 --- a/src/main/java/mods/eln/transparentnode/railroad/OverheadLines.kt +++ b/src/main/java/mods/eln/transparentnode/railroad/OverheadLines.kt @@ -14,6 +14,7 @@ import mods.eln.misc.Utils.plotPower import mods.eln.misc.Utils.plotVolt import mods.eln.node.NodeBase import mods.eln.node.transparent.* +import mods.eln.sim.ElectricalConnection import mods.eln.sim.ElectricalLoad import mods.eln.sim.mna.component.Resistor import mods.eln.sim.mna.misc.MnaConst @@ -22,6 +23,7 @@ import mods.eln.transparentnode.railroad.PoweredMinecartSimulationSingleton.powe import java.io.DataInputStream import java.io.DataOutputStream import java.io.IOException +import java.util.NoSuchElementException class OverheadLinesDescriptor(name: String, private val obj3D: Obj3D?): TransparentNodeDescriptor(name, OverheadLinesElement::class.java, OverheadLinesRender::class.java @@ -55,12 +57,22 @@ class OverheadLinesElement(node: TransparentNode?, connect() } + private val actualSides = listOf(Direction.XN, Direction.ZN, Direction.XP, Direction.ZP) + override fun getConnectionMask(side: Direction, lrdu: LRDU): Int { - return NodeBase.maskElectricalPower + return if (side in actualSides && (lrdu == LRDU.Down)) { + NodeBase.maskElectricalPower + } else { + 0 + } } - override fun getElectricalLoad(side: Direction, lrdu: LRDU): ElectricalLoad { - return electricalLoad + override fun getElectricalLoad(side: Direction, lrdu: LRDU): ElectricalLoad? { + return if (side in actualSides && (lrdu == LRDU.Down)) { + electricalLoad + } else { + null + } } override fun networkSerialize(stream: DataOutputStream) { @@ -81,8 +93,7 @@ class OverheadLinesElement(node: TransparentNode?, val ss = electricalLoad.subSystem if (ss != null) { val subSystemSize = electricalLoad.subSystem.component.size - var textColor = "" - textColor = if (subSystemSize <= 8) { + val textColor: String = if (subSystemSize <= 8) { "§a" } else if (subSystemSize <= 15) { "§6" @@ -96,16 +107,24 @@ class OverheadLinesElement(node: TransparentNode?, return info } + class MinecartResistor: Resistor() + override fun registerCart(cart: EntityElectricMinecart) { if (cart !in poweredMinecartSimulationData.map { it.minecart }) { - val resistor = Resistor() - resistor.connectTo(electricalLoad, null) + val resistor = MinecartResistor() + val resistorLoad = ElectricalLoad() + resistorLoad.setAsPrivate() + val connection = ElectricalConnection(electricalLoad, resistorLoad) + resistor.connectTo(resistorLoad, null) resistor.r = MnaConst.highImpedance - electricalComponentList.add(resistor) + resistorLoad.rs = MnaConst.noImpedance + connection.r = MnaConst.noImpedance + Eln.simulator.addElectricalLoad(resistorLoad) + Eln.simulator.addElectricalComponent(connection) + Eln.simulator.addElectricalComponent(resistor) val rrsp = RailroadResistorSlowProcess(this, cart, 0.05) Eln.simulator.addSlowProcess(rrsp) - poweredMinecartSimulationData.add(PoweredMinecartSimulationData(cart, resistor, rrsp, this)) - this.electricalLoad.subSystem.invalidate() + poweredMinecartSimulationData.add(PoweredMinecartSimulationData(cart, resistor, resistorLoad, connection, rrsp, this)) this.needPublish() } } @@ -115,9 +134,11 @@ class OverheadLinesElement(node: TransparentNode?, if (search.isNotEmpty()) { search.forEach { if (it.owningElement == this) { - electricalComponentList.remove(it.resistor) + Eln.simulator.removeElectricalComponent(it.resistor) + Eln.simulator.removeElectricalComponent(it.electricalConnection) + Eln.simulator.removeElectricalLoad(it.resistorElectricalLoad) it.resistor.breakConnection() - this.electricalLoad.subSystem.invalidate() + it.electricalConnection.breakConnection() Eln.simulator.removeSlowProcess(it.slowProcess) poweredMinecartSimulationData.remove(it) this.needPublish() diff --git a/src/main/java/mods/eln/transparentnode/railroad/PoweredMinecartSimulationData.kt b/src/main/java/mods/eln/transparentnode/railroad/PoweredMinecartSimulationData.kt index d5662535e..1567f3473 100644 --- a/src/main/java/mods/eln/transparentnode/railroad/PoweredMinecartSimulationData.kt +++ b/src/main/java/mods/eln/transparentnode/railroad/PoweredMinecartSimulationData.kt @@ -1,6 +1,8 @@ package mods.eln.transparentnode.railroad import mods.eln.entity.carts.EntityElectricMinecart +import mods.eln.sim.ElectricalConnection +import mods.eln.sim.ElectricalLoad import mods.eln.sim.mna.component.Resistor /** @@ -12,6 +14,8 @@ import mods.eln.sim.mna.component.Resistor data class PoweredMinecartSimulationData( val minecart: EntityElectricMinecart, var resistor: Resistor, + var resistorElectricalLoad: ElectricalLoad, + var electricalConnection: ElectricalConnection, var slowProcess: RailroadResistorSlowProcess, val owningElement: RailroadPowerInterface ) diff --git a/src/main/java/mods/eln/transparentnode/railroad/PoweredMinecartSimulationSingleton.kt b/src/main/java/mods/eln/transparentnode/railroad/PoweredMinecartSimulationSingleton.kt index b1c101ff8..b58d13054 100644 --- a/src/main/java/mods/eln/transparentnode/railroad/PoweredMinecartSimulationSingleton.kt +++ b/src/main/java/mods/eln/transparentnode/railroad/PoweredMinecartSimulationSingleton.kt @@ -4,6 +4,7 @@ import mods.eln.Eln import mods.eln.entity.carts.EntityElectricMinecart import mods.eln.node.transparent.TransparentNodeElement import mods.eln.sim.IProcess +import mods.eln.sim.mna.misc.MnaConst object PoweredMinecartSimulationSingleton { val poweredMinecartSimulationData: MutableList = mutableListOf() @@ -33,6 +34,8 @@ object PoweredMinecartSimulationSingleton { cartData.owningElement.needPublish() cartData.resistor.r = resistance cartData.slowProcess.timeLeft = time + + Eln.logger.info(cartData.resistor.subSystem) } /** diff --git a/src/main/java/mods/eln/transparentnode/railroad/UnderTrackPower.kt b/src/main/java/mods/eln/transparentnode/railroad/UnderTrackPower.kt index 4772f6c71..cbc56da37 100644 --- a/src/main/java/mods/eln/transparentnode/railroad/UnderTrackPower.kt +++ b/src/main/java/mods/eln/transparentnode/railroad/UnderTrackPower.kt @@ -7,8 +7,9 @@ import mods.eln.i18n.I18N import mods.eln.misc.* import mods.eln.node.NodeBase import mods.eln.node.transparent.* +import mods.eln.sim.ElectricalConnection import mods.eln.sim.ElectricalLoad -import mods.eln.sim.IProcess +import mods.eln.transparentnode.railroad.PoweredMinecartSimulationSingleton.poweredMinecartSimulationData import mods.eln.sim.mna.component.Resistor import mods.eln.sim.mna.misc.MnaConst import mods.eln.sim.nbt.NbtElectricalLoad @@ -74,8 +75,7 @@ class UnderTrackPowerElement(node: TransparentNode?, val ss = electricalLoad.subSystem if (ss != null) { val subSystemSize = electricalLoad.subSystem.component.size - var textColor = "" - textColor = if (subSystemSize <= 8) { + val textColor: String = if (subSystemSize <= 8) { "§a" } else if (subSystemSize <= 15) { "§6" @@ -89,30 +89,42 @@ class UnderTrackPowerElement(node: TransparentNode?, return info } + class MinecartResistor: Resistor() + override fun registerCart(cart: EntityElectricMinecart) { - if (cart !in PoweredMinecartSimulationSingleton.poweredMinecartSimulationData.map { it.minecart }) { - val resistor = Resistor() - resistor.connectTo(electricalLoad, null) + if (cart !in poweredMinecartSimulationData.map { it.minecart }) { + val resistor = MinecartResistor() + val resistorLoad = ElectricalLoad() + resistorLoad.setAsPrivate() + val connection = ElectricalConnection(electricalLoad, resistorLoad) + resistor.connectTo(resistorLoad, null) resistor.r = MnaConst.highImpedance + resistorLoad.rs = MnaConst.noImpedance + connection.r = MnaConst.noImpedance + electricalLoadList.add(resistorLoad) + electricalComponentList.add(connection) electricalComponentList.add(resistor) val rrsp = RailroadResistorSlowProcess(this, cart, 0.05) Eln.simulator.addSlowProcess(rrsp) - PoweredMinecartSimulationSingleton.poweredMinecartSimulationData.add(PoweredMinecartSimulationData(cart, resistor, rrsp, this)) + poweredMinecartSimulationData.add(PoweredMinecartSimulationData(cart, resistor, resistorLoad, connection, rrsp, this)) this.electricalLoad.subSystem.invalidate() this.needPublish() } } override fun deregisterCart(cart: EntityElectricMinecart) { - val search = PoweredMinecartSimulationSingleton.poweredMinecartSimulationData.filter { it.minecart == cart } + val search = poweredMinecartSimulationData.filter { it.minecart == cart } if (search.isNotEmpty()) { search.forEach { if (it.owningElement == this) { electricalComponentList.remove(it.resistor) + electricalComponentList.remove(it.electricalConnection) + electricalLoadList.remove(it.resistorElectricalLoad) it.resistor.breakConnection() + it.electricalConnection.breakConnection() this.electricalLoad.subSystem.invalidate() Eln.simulator.removeSlowProcess(it.slowProcess) - PoweredMinecartSimulationSingleton.poweredMinecartSimulationData.remove(it) + poweredMinecartSimulationData.remove(it) this.needPublish() } } From 728791f1c43824ce85955e44e98e6483511378e8 Mon Sep 17 00:00:00 2001 From: Jared Dunbar Date: Sat, 7 Jan 2023 13:04:12 -0500 Subject: [PATCH 5/7] Minor changes --- .../java/mods/eln/entity/carts/EntityElectricMinecart.kt | 6 ++++-- .../mods/eln/transparentnode/railroad/OverheadLines.kt | 1 - .../railroad/PoweredMinecartSimulationSingleton.kt | 9 ++++++--- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/main/java/mods/eln/entity/carts/EntityElectricMinecart.kt b/src/main/java/mods/eln/entity/carts/EntityElectricMinecart.kt index d7ef123fc..47f91a07a 100644 --- a/src/main/java/mods/eln/entity/carts/EntityElectricMinecart.kt +++ b/src/main/java/mods/eln/entity/carts/EntityElectricMinecart.kt @@ -15,6 +15,8 @@ import net.minecraft.entity.player.EntityPlayer import net.minecraft.init.Blocks import net.minecraft.server.MinecraftServer import net.minecraft.world.World +import net.minecraftforge.common.MinecraftForge +import net.minecraftforge.event.entity.minecart.MinecartInteractEvent import kotlin.math.abs import kotlin.math.sign @@ -116,9 +118,9 @@ class EntityElectricMinecart(world: World, x: Double, y: Double, z: Double): Ent } private fun getOverheadWires(coordinate: Coordinate): OverheadLinesElement? { - // Pass coordinate of tracks and check vertically the next 3-4 blocks + // Pass coordinate of tracks and check vertically the next 3 blocks (4 up looks visually weird) val originalY = coordinate.y - while (coordinate.y <= (originalY + 4)) { + while (coordinate.y <= (originalY + 3)) { coordinate.y val node = NodeManager.instance!!.getTransparentNodeFromCoordinate(coordinate) if (node is OverheadLinesElement) { diff --git a/src/main/java/mods/eln/transparentnode/railroad/OverheadLines.kt b/src/main/java/mods/eln/transparentnode/railroad/OverheadLines.kt index e84abf67b..e39cb2944 100644 --- a/src/main/java/mods/eln/transparentnode/railroad/OverheadLines.kt +++ b/src/main/java/mods/eln/transparentnode/railroad/OverheadLines.kt @@ -23,7 +23,6 @@ import mods.eln.transparentnode.railroad.PoweredMinecartSimulationSingleton.powe import java.io.DataInputStream import java.io.DataOutputStream import java.io.IOException -import java.util.NoSuchElementException class OverheadLinesDescriptor(name: String, private val obj3D: Obj3D?): TransparentNodeDescriptor(name, OverheadLinesElement::class.java, OverheadLinesRender::class.java diff --git a/src/main/java/mods/eln/transparentnode/railroad/PoweredMinecartSimulationSingleton.kt b/src/main/java/mods/eln/transparentnode/railroad/PoweredMinecartSimulationSingleton.kt index b58d13054..0cc8393ca 100644 --- a/src/main/java/mods/eln/transparentnode/railroad/PoweredMinecartSimulationSingleton.kt +++ b/src/main/java/mods/eln/transparentnode/railroad/PoweredMinecartSimulationSingleton.kt @@ -32,10 +32,13 @@ object PoweredMinecartSimulationSingleton { if (cartData.resistor.r != resistance) if (cartData.owningElement is TransparentNodeElement) cartData.owningElement.needPublish() - cartData.resistor.r = resistance + // Don't draw power from the overhead lines if the voltage is too low + if (cartData.resistorElectricalLoad.u < 700.0) { + cartData.resistor.r = MnaConst.highImpedance + } else { + cartData.resistor.r = resistance + } cartData.slowProcess.timeLeft = time - - Eln.logger.info(cartData.resistor.subSystem) } /** From 60cf268504031684f0cbebc4370bb9431df4099e Mon Sep 17 00:00:00 2001 From: Jared Dunbar Date: Sun, 11 Feb 2024 19:16:40 -0500 Subject: [PATCH 6/7] Local changes of unknown nature --- src/main/java/mods/eln/Eln.java | 8 ++++---- .../mods/eln/{item => railroad}/ElectricMinecartItem.kt | 3 +-- .../{entity/carts => railroad}/EntityElectricMinecart.kt | 8 +------- .../eln/{transparentnode => }/railroad/OverheadLines.kt | 5 ++--- .../railroad/PoweredMinecartSimulationData.kt | 3 +-- .../railroad/PoweredMinecartSimulationSingleton.kt | 3 +-- .../railroad/RailroadPowerInterface.kt | 4 +--- .../railroad/RailroadResistorSlowProcess.kt | 3 +-- .../eln/{transparentnode => }/railroad/UnderTrackPower.kt | 5 ++--- 9 files changed, 14 insertions(+), 28 deletions(-) rename src/main/java/mods/eln/{item => railroad}/ElectricMinecartItem.kt (94%) rename src/main/java/mods/eln/{entity/carts => railroad}/EntityElectricMinecart.kt (92%) rename src/main/java/mods/eln/{transparentnode => }/railroad/OverheadLines.kt (97%) rename src/main/java/mods/eln/{transparentnode => }/railroad/PoweredMinecartSimulationData.kt (88%) rename src/main/java/mods/eln/{transparentnode => }/railroad/PoweredMinecartSimulationSingleton.kt (96%) rename src/main/java/mods/eln/{transparentnode => }/railroad/RailroadPowerInterface.kt (81%) rename src/main/java/mods/eln/{transparentnode => }/railroad/RailroadResistorSlowProcess.kt (81%) rename src/main/java/mods/eln/{transparentnode => }/railroad/UnderTrackPower.kt (96%) diff --git a/src/main/java/mods/eln/Eln.java b/src/main/java/mods/eln/Eln.java index 467a51406..e2e6ba953 100644 --- a/src/main/java/mods/eln/Eln.java +++ b/src/main/java/mods/eln/Eln.java @@ -19,7 +19,6 @@ import mods.eln.client.SoundLoader; import mods.eln.entity.ReplicatorEntity; import mods.eln.entity.ReplicatorPopProcess; -import mods.eln.entity.carts.EntityElectricMinecart; import mods.eln.eventhandlers.ElnFMLEventsHandler; import mods.eln.eventhandlers.ElnForgeEventsHandler; import mods.eln.generic.*; @@ -54,6 +53,8 @@ import mods.eln.ore.OreDescriptor; import mods.eln.ore.OreItem; import mods.eln.packets.*; +import mods.eln.railroad.ElectricMinecartItem; +import mods.eln.railroad.EntityElectricMinecart; import mods.eln.server.*; import mods.eln.server.console.ElnConsoleCommands; import mods.eln.signalinductor.SignalInductorDescriptor; @@ -142,8 +143,7 @@ import mods.eln.transparentnode.heatfurnace.HeatFurnaceDescriptor; import mods.eln.transparentnode.powercapacitor.PowerCapacitorDescriptor; import mods.eln.transparentnode.powerinductor.PowerInductorDescriptor; -import mods.eln.transparentnode.railroad.OverheadLinesDescriptor; -import mods.eln.transparentnode.railroad.UnderTrackPowerDescriptor; +import mods.eln.railroad.OverheadLinesDescriptor; import mods.eln.transparentnode.solarpanel.SolarPanelDescriptor; import mods.eln.transparentnode.teleporter.TeleporterDescriptor; import mods.eln.transparentnode.teleporter.TeleporterElement; @@ -4711,7 +4711,7 @@ private void registerTransparentNodeMisc(int id) { g.addRectangle(-4, -1, 2, 2, 0, 0); g.addElement(0, 1, 0); //g.addElement(0, 2, 0); - g.addElement(-1, 0, 0, ghostBlock, ghostBlock.tFloor); + g.addElement(-1, 0, 0, ghostBlock, GhostBlock.tFloor); /* g.addElement(1, 0, 0,ghostBlock,ghostBlock.tLadder); g.addElement(1, 1, 0,ghostBlock,ghostBlock.tLadder); g.addElement(1, 2, 0,ghostBlock,ghostBlock.tLadder);*/ diff --git a/src/main/java/mods/eln/item/ElectricMinecartItem.kt b/src/main/java/mods/eln/railroad/ElectricMinecartItem.kt similarity index 94% rename from src/main/java/mods/eln/item/ElectricMinecartItem.kt rename to src/main/java/mods/eln/railroad/ElectricMinecartItem.kt index 0409ad7f8..209ba5619 100644 --- a/src/main/java/mods/eln/item/ElectricMinecartItem.kt +++ b/src/main/java/mods/eln/railroad/ElectricMinecartItem.kt @@ -1,6 +1,5 @@ -package mods.eln.item +package mods.eln.railroad -import mods.eln.entity.carts.EntityElectricMinecart import mods.eln.generic.GenericItemUsingDamageDescriptor import net.minecraft.block.BlockRailBase import net.minecraft.entity.player.EntityPlayer diff --git a/src/main/java/mods/eln/entity/carts/EntityElectricMinecart.kt b/src/main/java/mods/eln/railroad/EntityElectricMinecart.kt similarity index 92% rename from src/main/java/mods/eln/entity/carts/EntityElectricMinecart.kt rename to src/main/java/mods/eln/railroad/EntityElectricMinecart.kt index 47f91a07a..9c10af164 100644 --- a/src/main/java/mods/eln/entity/carts/EntityElectricMinecart.kt +++ b/src/main/java/mods/eln/railroad/EntityElectricMinecart.kt @@ -1,22 +1,16 @@ -package mods.eln.entity.carts +package mods.eln.railroad import mods.eln.Eln import mods.eln.misc.Coordinate import mods.eln.misc.Utils import mods.eln.node.NodeManager import mods.eln.sim.mna.misc.MnaConst -import mods.eln.transparentnode.railroad.OverheadLinesElement -import mods.eln.transparentnode.railroad.PoweredMinecartSimulationSingleton -import mods.eln.transparentnode.railroad.RailroadPowerInterface -import mods.eln.transparentnode.railroad.UnderTrackPowerElement import net.minecraft.block.Block import net.minecraft.entity.item.EntityMinecart import net.minecraft.entity.player.EntityPlayer import net.minecraft.init.Blocks import net.minecraft.server.MinecraftServer import net.minecraft.world.World -import net.minecraftforge.common.MinecraftForge -import net.minecraftforge.event.entity.minecart.MinecartInteractEvent import kotlin.math.abs import kotlin.math.sign diff --git a/src/main/java/mods/eln/transparentnode/railroad/OverheadLines.kt b/src/main/java/mods/eln/railroad/OverheadLines.kt similarity index 97% rename from src/main/java/mods/eln/transparentnode/railroad/OverheadLines.kt rename to src/main/java/mods/eln/railroad/OverheadLines.kt index e39cb2944..b7911fc26 100644 --- a/src/main/java/mods/eln/transparentnode/railroad/OverheadLines.kt +++ b/src/main/java/mods/eln/railroad/OverheadLines.kt @@ -1,8 +1,7 @@ -package mods.eln.transparentnode.railroad +package mods.eln.railroad import mods.eln.Eln import mods.eln.cable.CableRenderDescriptor -import mods.eln.entity.carts.EntityElectricMinecart import mods.eln.i18n.I18N import mods.eln.misc.Coordinate import mods.eln.misc.Direction @@ -19,7 +18,7 @@ import mods.eln.sim.ElectricalLoad import mods.eln.sim.mna.component.Resistor import mods.eln.sim.mna.misc.MnaConst import mods.eln.sim.nbt.NbtElectricalLoad -import mods.eln.transparentnode.railroad.PoweredMinecartSimulationSingleton.poweredMinecartSimulationData +import mods.eln.railroad.PoweredMinecartSimulationSingleton.poweredMinecartSimulationData import java.io.DataInputStream import java.io.DataOutputStream import java.io.IOException diff --git a/src/main/java/mods/eln/transparentnode/railroad/PoweredMinecartSimulationData.kt b/src/main/java/mods/eln/railroad/PoweredMinecartSimulationData.kt similarity index 88% rename from src/main/java/mods/eln/transparentnode/railroad/PoweredMinecartSimulationData.kt rename to src/main/java/mods/eln/railroad/PoweredMinecartSimulationData.kt index 1567f3473..a87f32ec5 100644 --- a/src/main/java/mods/eln/transparentnode/railroad/PoweredMinecartSimulationData.kt +++ b/src/main/java/mods/eln/railroad/PoweredMinecartSimulationData.kt @@ -1,6 +1,5 @@ -package mods.eln.transparentnode.railroad +package mods.eln.railroad -import mods.eln.entity.carts.EntityElectricMinecart import mods.eln.sim.ElectricalConnection import mods.eln.sim.ElectricalLoad import mods.eln.sim.mna.component.Resistor diff --git a/src/main/java/mods/eln/transparentnode/railroad/PoweredMinecartSimulationSingleton.kt b/src/main/java/mods/eln/railroad/PoweredMinecartSimulationSingleton.kt similarity index 96% rename from src/main/java/mods/eln/transparentnode/railroad/PoweredMinecartSimulationSingleton.kt rename to src/main/java/mods/eln/railroad/PoweredMinecartSimulationSingleton.kt index 0cc8393ca..4f736600c 100644 --- a/src/main/java/mods/eln/transparentnode/railroad/PoweredMinecartSimulationSingleton.kt +++ b/src/main/java/mods/eln/railroad/PoweredMinecartSimulationSingleton.kt @@ -1,7 +1,6 @@ -package mods.eln.transparentnode.railroad +package mods.eln.railroad import mods.eln.Eln -import mods.eln.entity.carts.EntityElectricMinecart import mods.eln.node.transparent.TransparentNodeElement import mods.eln.sim.IProcess import mods.eln.sim.mna.misc.MnaConst diff --git a/src/main/java/mods/eln/transparentnode/railroad/RailroadPowerInterface.kt b/src/main/java/mods/eln/railroad/RailroadPowerInterface.kt similarity index 81% rename from src/main/java/mods/eln/transparentnode/railroad/RailroadPowerInterface.kt rename to src/main/java/mods/eln/railroad/RailroadPowerInterface.kt index afc8b2626..3c614ae53 100644 --- a/src/main/java/mods/eln/transparentnode/railroad/RailroadPowerInterface.kt +++ b/src/main/java/mods/eln/railroad/RailroadPowerInterface.kt @@ -1,6 +1,4 @@ -package mods.eln.transparentnode.railroad - -import mods.eln.entity.carts.EntityElectricMinecart +package mods.eln.railroad interface RailroadPowerInterface { /** diff --git a/src/main/java/mods/eln/transparentnode/railroad/RailroadResistorSlowProcess.kt b/src/main/java/mods/eln/railroad/RailroadResistorSlowProcess.kt similarity index 81% rename from src/main/java/mods/eln/transparentnode/railroad/RailroadResistorSlowProcess.kt rename to src/main/java/mods/eln/railroad/RailroadResistorSlowProcess.kt index 16d8da02f..24ded1517 100644 --- a/src/main/java/mods/eln/transparentnode/railroad/RailroadResistorSlowProcess.kt +++ b/src/main/java/mods/eln/railroad/RailroadResistorSlowProcess.kt @@ -1,7 +1,6 @@ -package mods.eln.transparentnode.railroad +package mods.eln.railroad import mods.eln.Eln -import mods.eln.entity.carts.EntityElectricMinecart import mods.eln.sim.IProcess class RailroadResistorSlowProcess(val rpi: RailroadPowerInterface, val cart: EntityElectricMinecart, var timeLeft: Double): IProcess { diff --git a/src/main/java/mods/eln/transparentnode/railroad/UnderTrackPower.kt b/src/main/java/mods/eln/railroad/UnderTrackPower.kt similarity index 96% rename from src/main/java/mods/eln/transparentnode/railroad/UnderTrackPower.kt rename to src/main/java/mods/eln/railroad/UnderTrackPower.kt index cbc56da37..4959eec5b 100644 --- a/src/main/java/mods/eln/transparentnode/railroad/UnderTrackPower.kt +++ b/src/main/java/mods/eln/railroad/UnderTrackPower.kt @@ -1,15 +1,14 @@ -package mods.eln.transparentnode.railroad +package mods.eln.railroad import mods.eln.Eln import mods.eln.cable.CableRenderDescriptor -import mods.eln.entity.carts.EntityElectricMinecart import mods.eln.i18n.I18N import mods.eln.misc.* import mods.eln.node.NodeBase import mods.eln.node.transparent.* import mods.eln.sim.ElectricalConnection import mods.eln.sim.ElectricalLoad -import mods.eln.transparentnode.railroad.PoweredMinecartSimulationSingleton.poweredMinecartSimulationData +import mods.eln.railroad.PoweredMinecartSimulationSingleton.poweredMinecartSimulationData import mods.eln.sim.mna.component.Resistor import mods.eln.sim.mna.misc.MnaConst import mods.eln.sim.nbt.NbtElectricalLoad From e954a8a057ab8f0fd9a1adad336b5a6dbde3894d Mon Sep 17 00:00:00 2001 From: Jared Dunbar Date: Sun, 11 Feb 2024 20:11:40 -0500 Subject: [PATCH 7/7] Less spiky power utilization --- src/main/java/mods/eln/railroad/EntityElectricMinecart.kt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/mods/eln/railroad/EntityElectricMinecart.kt b/src/main/java/mods/eln/railroad/EntityElectricMinecart.kt index 9c10af164..9b1f9ea0d 100644 --- a/src/main/java/mods/eln/railroad/EntityElectricMinecart.kt +++ b/src/main/java/mods/eln/railroad/EntityElectricMinecart.kt @@ -50,9 +50,10 @@ class EntityElectricMinecart(world: World, x: Double, y: Double, z: Double): Ent } if (energyBufferJoules < energyBufferTargetJoules) { - PoweredMinecartSimulationSingleton.powerCart(this, locomotiveMaximumResistance, 0.1) + val chargeRateInv = energyBufferTargetJoules / (abs(energyBufferTargetJoules - energyBufferJoules) * 2) + PoweredMinecartSimulationSingleton.powerCart(this, chargeRateInv * locomotiveMaximumResistance, 0.1) } else { - PoweredMinecartSimulationSingleton.powerCart(this, MnaConst.highImpedance, 1.0) + PoweredMinecartSimulationSingleton.powerCart(this, MnaConst.highImpedance, 0.1) } energyBufferJoules += PoweredMinecartSimulationSingleton.cartCollectEnergy(this) }