Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/electric minecarts #322

Draft
wants to merge 7 commits into
base: 1.22.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 44 additions & 4 deletions src/main/java/mods/eln/Eln.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,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;
Expand All @@ -61,9 +63,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;
Expand Down Expand Up @@ -144,6 +143,7 @@
import mods.eln.transparentnode.heatfurnace.HeatFurnaceDescriptor;
import mods.eln.transparentnode.powercapacitor.PowerCapacitorDescriptor;
import mods.eln.transparentnode.powerinductor.PowerInductorDescriptor;
import mods.eln.railroad.OverheadLinesDescriptor;
import mods.eln.transparentnode.solarpanel.SolarPanelDescriptor;
import mods.eln.transparentnode.teleporter.TeleporterDescriptor;
import mods.eln.transparentnode.teleporter.TeleporterElement;
Expand Down Expand Up @@ -688,6 +688,7 @@ public void preInit(FMLPreInitializationEvent event) {
//registerFloodlight(68);
registerFestive(69);
registerFab(70);
registerOverheadWires(71);


//ITEM REGISTRATION
Expand Down Expand Up @@ -719,6 +720,7 @@ public void preInit(FMLPreInitializationEvent event) {
registerFuelBurnerItem(124);
registerPortableNaN(); // 125
registerBasicItems(126);
registerElectricMinecartItems(127);

OreDictionary.registerOre("blockAluminum", arcClayBlock);
OreDictionary.registerOre("blockSteel", arcMetalBlock);
Expand Down Expand Up @@ -773,6 +775,24 @@ 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);
}
/*
{
subId = 1;
UnderTrackPowerDescriptor desc = new UnderTrackPowerDescriptor("Under Track Power",
obj.getObj("OverheadGantry"));
transparentNodeItem.addDescriptor(subId + (id << 6), desc);
}
*/
}

private void registerGridDevices(int id) {
int subId;
{
Expand Down Expand Up @@ -906,6 +926,7 @@ public void load(FMLInitializationEvent event) {

//
registerReplicator();
registerElectricMinecart();
//

recipeEnergyConverter();
Expand Down Expand Up @@ -4690,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);*/
Expand Down Expand Up @@ -5752,6 +5773,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() {
Expand Down Expand Up @@ -8521,6 +8553,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() {
}
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/mods/eln/railroad/ElectricMinecartItem.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package mods.eln.railroad

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
}
}
149 changes: 149 additions & 0 deletions src/main/java/mods/eln/railroad/EntityElectricMinecart.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
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 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: 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) {
val chargeRateInv = energyBufferTargetJoules / (abs(energyBufferTargetJoules - energyBufferJoules) * 2)
PoweredMinecartSimulationSingleton.powerCart(this, chargeRateInv * locomotiveMaximumResistance, 0.1)
} else {
PoweredMinecartSimulationSingleton.powerCart(this, MnaConst.highImpedance, 0.1)
}
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,
blockY: Int,
blockZ: Int,
speed: Double,
drag: Double,
block: Block?,
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 startingThreshold = 0.0005

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 blocks (4 up looks visually weird)
val originalY = coordinate.y
while (coordinate.y <= (originalY + 3)) {
coordinate.y
val node = NodeManager.instance!!.getTransparentNodeFromCoordinate(coordinate)
if (node is OverheadLinesElement) {
return node
}
coordinate.y++
}
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
}

override fun getMinecartType(): Int {
return -1
}

override fun func_145817_o(): Block? {
return Blocks.iron_block
}

override fun func_145820_n(): Block? {
return Blocks.iron_block
}
}
Loading