From 07719c3d957bf6bf1d8b11e3c805d996ee25204a Mon Sep 17 00:00:00 2001 From: Johannes Lohrer Date: Thu, 10 Oct 2013 22:44:11 +0200 Subject: [PATCH 1/4] some changes to power --- li/cil/oc/api/network/PoweredNode.scala | 48 ++++---- .../common/tileentity/PowerDistributor.scala | 42 +++---- li/cil/oc/common/tileentity/PowerSupply.scala | 105 +++++++++--------- 3 files changed, 98 insertions(+), 97 deletions(-) diff --git a/li/cil/oc/api/network/PoweredNode.scala b/li/cil/oc/api/network/PoweredNode.scala index ef923d33a..4b56aba7a 100644 --- a/li/cil/oc/api/network/PoweredNode.scala +++ b/li/cil/oc/api/network/PoweredNode.scala @@ -1,12 +1,12 @@ package li.cil.oc.api.network import li.cil.oc.common.tileentity.PowerDistributor -import scala.collection.mutable.ArrayBuffer +import scala.collection.mutable trait PoweredNode extends Node { - var arrayBuffer = ArrayBuffer[PowerDistributor]() - var demand = 2 + var powerDistributors = mutable.Set[PowerDistributor]() + override def receive(message: Message): Option[Array[Any]] = { message.name match { @@ -14,9 +14,9 @@ trait PoweredNode extends Node { message.source match { case distributor: PowerDistributor => { println("connect") - if (arrayBuffer.filter(p => p == distributor).isEmpty) { - arrayBuffer += distributor - distributor.connectNode(this, getDemand, getPriority) + if (powerDistributors.contains(istributor)) { + powerDistributors += distributor + distributor.connectNode(this, _demand, _priority) } } case _ => @@ -26,8 +26,8 @@ trait PoweredNode extends Node { message.source match { case distributor: PowerDistributor => { println("connect") - if (arrayBuffer.contains(distributor)) { - arrayBuffer -= distributor + if (powerDistributors.contains(distributor)) { + powerDistributors -= distributor distributor.disconnectNode(this) } } @@ -39,24 +39,34 @@ trait PoweredNode extends Node { super.receive(message) } - def removeBuffer(distributor: PowerDistributor) = { - distributor.disconnectNode(this) - arrayBuffer -= distributor - } override protected def onDisconnect() { - - arrayBuffer.foreach(e => { + super.onDisconnect() + powerDistributors.foreach(e => { e.disconnectNode(this) }) - super.onDisconnect() + } - def getDemand: Int = 2 + private var _demand = 0 + + def demand = _demand + + def demand_=(value: Int) = { + + powerDistributors.foreach(e => e.updateDemand(this, value)) + _demand = value + } + + private var _priority = 0 + + def priority = _priority + + + def main: PowerDistributor = { + powerDistributors.filter(p => p.isActive).foreach(f => return f) - def getPriority: Int = 1 - def updateDemand(demand: Int) { - arrayBuffer.foreach(e => e.updateDemand(this, getDemand)) } } + diff --git a/li/cil/oc/common/tileentity/PowerDistributor.scala b/li/cil/oc/common/tileentity/PowerDistributor.scala index 47be0d0a6..7dc2eaebb 100644 --- a/li/cil/oc/common/tileentity/PowerDistributor.scala +++ b/li/cil/oc/common/tileentity/PowerDistributor.scala @@ -1,15 +1,16 @@ package li.cil.oc.common.tileentity import li.cil.oc.api.network.{PoweredNode, Message, Visibility} -import scala.collection.mutable.ArrayBuffer +import scala.collection.mutable class PowerDistributor extends Rotatable with PoweredNode { var isActive = true - var energyStorageList = ArrayBuffer[EnergyStorage]() + var energyStorageList = mutable.Set[EnergyStorage]() var energyDemand = 0 - demand = 1 + var storedEnergy = 0 + var MAXENERGY = 2000 override def name = "powerdistributor" @@ -17,13 +18,13 @@ class PowerDistributor extends Rotatable with PoweredNode { override def receive(message: Message): Option[Array[Any]] = { - - message.name match { + if (message.source != this) + {message.name match { case "network.connect" => { message.source match { case distributor: PowerDistributor => //if other powerDistributor connected and is active set inactive - if (message.source != this && distributor.isActive) { + if (distributor.isActive) { isActive = false println("demand now (disabled) " + 0) @@ -36,7 +37,7 @@ class PowerDistributor extends Rotatable with PoweredNode { case distributor: PowerDistributor => //received request from other distributor that is newly connected... set it to inactive - if (isActive && message.source != this) { + if (isActive ) { distributor.isActive = false } case _ => @@ -46,7 +47,7 @@ class PowerDistributor extends Rotatable with PoweredNode { message.source match { case distributor: PowerDistributor => println("distri disc recieved") - if (distributor.isActive && distributor != this) { + if (distributor.isActive ) { isActive = true network.foreach(_.sendToVisible(this, "power.find")) @@ -58,6 +59,7 @@ class PowerDistributor extends Rotatable with PoweredNode { } case _ => // Ignore. } + } super.receive(message) } @@ -78,7 +80,7 @@ class PowerDistributor extends Rotatable with PoweredNode { * @param demand */ def updateDemand(node: PoweredNode, demand: Int) { - energyStorageList.filter(n => n.node == node).foreach(n => { + energyStorageList.fi(n => n.node == node).foreach(n => { energyDemand -= n.amount energyDemand += demand n.amount = demand @@ -109,22 +111,7 @@ class PowerDistributor extends Rotatable with PoweredNode { super.onConnect() } - override protected def onDisconnect() { - println("disc distri other " + arrayBuffer.length) - super.onDisconnect() - energyStorageList.clone().foreach(e => { - e.node.removeBuffer(this) - if (energyStorageList.contains(e)) { - - energyStorageList -= e - energyDemand -= e.amount - } - - }) - if (isActive) - println("demand now (close) " + energyDemand) - } override def updateEntity() { super.updateEntity() @@ -133,7 +120,12 @@ class PowerDistributor extends Rotatable with PoweredNode { } //TODO remove energy } - + def getDemand = { + MAXENERGY-storedEnergy max 0 + } + def addEnergy(amount:Int){ + storedEnergy+=amount + } class EnergyStorage(var node: PoweredNode, var amount: Int, var priority: Int) { } diff --git a/li/cil/oc/common/tileentity/PowerSupply.scala b/li/cil/oc/common/tileentity/PowerSupply.scala index e30554a40..e896cfcca 100644 --- a/li/cil/oc/common/tileentity/PowerSupply.scala +++ b/li/cil/oc/common/tileentity/PowerSupply.scala @@ -1,7 +1,7 @@ package li.cil.oc.common.tileentity import net.minecraft.tileentity.TileEntity -import li.cil.oc.api.network.{PoweredNode, Visibility, Node} +import li.cil.oc.api.network.{PoweredNode, Visibility} import net.minecraftforge.common.{ForgeDirection, MinecraftForge} import ic2.api.energy.event.{EnergyTileLoadEvent, EnergyTileUnloadEvent} import cpw.mods.fml.common.FMLCommonHandler @@ -19,46 +19,48 @@ import universalelectricity.core.electricity.ElectricityPack * Time: 20:37 * To change this template use File | Settings | File Templates. */ -class PowerSupply extends Rotatable with PoweredNode with IEnergySink with IPowerReceptor with IElectrical{ +class PowerSupply extends Rotatable with PoweredNode with IEnergySink with IPowerReceptor with IElectrical { var addedToEnet = false - var powerHandler:PowerHandler = null + var powerHandler: PowerHandler = null + override def name = "powersupply" override def visibility = Visibility.Network - override def onChunkUnload(){ + override def onChunkUnload() { super.onChunkUnload() - onUnload() + onUnload() } - def onUnload(){ - if(addedToEnet){ + + def onUnload() { + if (addedToEnet) { MinecraftForge.EVENT_BUS.post(new EnergyTileUnloadEvent(this)) addedToEnet = false } } - override def updateEntity(){ - super.updateEntity() - if(!addedToEnet) { - onLoaded() - } - if(!FMLCommonHandler.instance.getEffectiveSide.isClient) - { - storedEnergy+=getPowerProvider().useEnergy(1,(MAXENERGY-storedEnergy).toFloat/5.0f,true)*5; - - } + override def updateEntity() { + super.updateEntity() + if (!addedToEnet) { + onLoaded() } + if (!FMLCommonHandler.instance.getEffectiveSide.isClient) { + main.addEnergy(getPowerProvider().useEnergy(1, main.getDemand.toFloat / 5.0f, true) * 5) + + } + } override def readFromNBT(nbt: NBTTagCompound) = { super.readFromNBT(nbt) getPowerProvider().readFromNBT(nbt) storedEnergy = nbt.getDouble("storedEnergy") } + override def writeToNBT(nbt: NBTTagCompound) = { super.writeToNBT(nbt) getPowerProvider().writeToNBT(nbt) - nbt.setDouble("storedEnergy",storedEnergy) + nbt.setDouble("storedEnergy", storedEnergy) } @@ -73,9 +75,8 @@ class PowerSupply extends Rotatable with PoweredNode with IEnergySink with IPowe } } - var storedEnergy = 0.0; - var lastInjectedEnergy =0.0; - var MAXENERGY = 1000; + + var lastInjectedEnergy = 0.0 //IC2 stuff /** * Determine how much energy the sink accepts. @@ -86,10 +87,10 @@ class PowerSupply extends Rotatable with PoweredNode with IEnergySink with IPowe * * @return max accepted input in eu */ - override def demandedEnergyUnits: Double={ - val needed = MAXENERGY-storedEnergy - if(needed>lastInjectedEnergy||needed>MAXENERGY/2) - return needed/2 + override def demandedEnergyUnits: Double = { + val needed = main.getDemand + if (needed > lastInjectedEnergy || needed > main.MAXENERGY / 2) + return needed / 2 0 } @@ -103,10 +104,9 @@ class PowerSupply extends Rotatable with PoweredNode with IEnergySink with IPowe * @param amount energy to be transferred * @return Energy not consumed (leftover) */ - override def injectEnergyUnits(directionFrom: ForgeDirection, amount: Double): Double ={ - lastInjectedEnergy = amount*2; - storedEnergy+=amount*2; - 0 + override def injectEnergyUnits(directionFrom: ForgeDirection, amount: Double): Double = { + lastInjectedEnergy = amount * 2 + main.addEnergy(amount*2) } /** @@ -119,7 +119,7 @@ class PowerSupply extends Rotatable with PoweredNode with IEnergySink with IPowe * * @return max safe input in eu */ - override def getMaxSafeInput: Int =Integer.MAX_VALUE + override def getMaxSafeInput: Int = Integer.MAX_VALUE /** * Determine if this acceptor can accept current from an adjacent emitter in a direction. @@ -146,21 +146,21 @@ class PowerSupply extends Rotatable with PoweredNode with IEnergySink with IPowe * @param side * @return */ - def getPowerReceiver(side: ForgeDirection): PowerHandler#PowerReceiver={ + def getPowerReceiver(side: ForgeDirection): PowerHandler#PowerReceiver = { - return getPowerProvider().getPowerReceiver + getPowerProvider().getPowerReceiver } - def getPowerProvider():PowerHandler= - { - if (powerHandler == null) - { + + def getPowerProvider(): PowerHandler = { + if (powerHandler == null) { powerHandler = new PowerHandler(this, PowerHandler.Type.STORAGE); if (powerHandler != null) { powerHandler.configure(1.0F, 320.0F, 800.0F, 640.0F); } } - return powerHandler; + powerHandler; } + /** * Call back from the PowerHandler that is called when the stored power exceeds the activation * power. @@ -169,11 +169,11 @@ class PowerSupply extends Rotatable with PoweredNode with IEnergySink with IPowe * * @param workProvider */ - def doWork(workProvider: PowerHandler){ + def doWork(workProvider: PowerHandler) { } - def getWorld: World=worldObj + def getWorld: World = worldObj /** * UE************************* @@ -188,15 +188,14 @@ class PowerSupply extends Rotatable with PoweredNode with IEnergySink with IPowe * @param doReceive If false, the charge will only be simulated. * @return Amount of energy that was accepted by the block. */ - def receiveElectricity(from: ForgeDirection, receive: ElectricityPack, doReceive: Boolean): Float={ - if (receive == null) return 0.0F; + def receiveElectricity(from: ForgeDirection, receive: ElectricityPack, doReceive: Boolean): Float = { + if (receive == null) return 0.0F - if (doReceive) - { - val energy = receive.getWatts() / 0.2F; - storedEnergy += energy; + if (doReceive) { + val energy = receive.getWatts() / 0.2F + main.addEnergy(energy) } - return receive.getWatts(); + receive.getWatts() } /** @@ -208,28 +207,28 @@ class PowerSupply extends Rotatable with PoweredNode with IEnergySink with IPowe * @param doProvide If false, the charge will only be simulated. * @return Amount of energy that was given out by the block. */ - def provideElectricity(from: ForgeDirection, request: ElectricityPack, doProvide: Boolean): ElectricityPack =null + def provideElectricity(from: ForgeDirection, request: ElectricityPack, doProvide: Boolean): ElectricityPack = null /** * @return How much energy does this TileEntity want? */ - def getRequest(direction: ForgeDirection): Float ={ - val diff = Math.floor((MAXENERGY - storedEnergy) * 0.2F) - return diff.toFloat max 0 + def getRequest(direction: ForgeDirection): Float = { + val diff = Math.floor(main.getDemand * 0.2F) + diff.toFloat max 0 } /** * @return How much energy does this TileEntity want to provide? */ - def getProvide(direction: ForgeDirection): Float = 0.0F + def getProvide(direction: ForgeDirection): Float = 0.0F /** * Gets the voltage of this TileEntity. * * @return The amount of volts. E.g 120v or 240v */ - def getVoltage: Float =120.0F + def getVoltage: Float = 120.0F - def canConnect(direction:ForgeDirection):Boolean = true + def canConnect(direction: ForgeDirection): Boolean = true } From 1ca4b92e31d56090ba52ec7c61ad3fc58bf13914 Mon Sep 17 00:00:00 2001 From: Johannes Lohrer Date: Sat, 12 Oct 2013 10:19:35 +0200 Subject: [PATCH 2/4] some changes (upload for pull) --- li/cil/oc/api/network/PoweredNode.scala | 6 ++++-- li/cil/oc/common/tileentity/PowerDistributor.scala | 5 +++-- li/cil/oc/common/tileentity/PowerSupply.scala | 13 +++++++------ 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/li/cil/oc/api/network/PoweredNode.scala b/li/cil/oc/api/network/PoweredNode.scala index 4b56aba7a..4a2f05500 100644 --- a/li/cil/oc/api/network/PoweredNode.scala +++ b/li/cil/oc/api/network/PoweredNode.scala @@ -2,6 +2,7 @@ package li.cil.oc.api.network import li.cil.oc.common.tileentity.PowerDistributor import scala.collection.mutable +import li.cil.oc.api.network.Node trait PoweredNode extends Node { @@ -14,7 +15,7 @@ trait PoweredNode extends Node { message.source match { case distributor: PowerDistributor => { println("connect") - if (powerDistributors.contains(istributor)) { + if (powerDistributors.contains(distributor)) { powerDistributors += distributor distributor.connectNode(this, _demand, _priority) } @@ -64,7 +65,8 @@ trait PoweredNode extends Node { def main: PowerDistributor = { - powerDistributors.filter(p => p.isActive).foreach(f => return f) + null + //powerDistributors.filter(p => p.isActive).foreach(f => return f) } diff --git a/li/cil/oc/common/tileentity/PowerDistributor.scala b/li/cil/oc/common/tileentity/PowerDistributor.scala index 7dc2eaebb..ce782a6ae 100644 --- a/li/cil/oc/common/tileentity/PowerDistributor.scala +++ b/li/cil/oc/common/tileentity/PowerDistributor.scala @@ -2,6 +2,7 @@ package li.cil.oc.common.tileentity import li.cil.oc.api.network.{PoweredNode, Message, Visibility} import scala.collection.mutable +import li.cil.oc.common.tileentity.Rotatable class PowerDistributor extends Rotatable with PoweredNode { @@ -38,7 +39,7 @@ class PowerDistributor extends Rotatable with PoweredNode { //received request from other distributor that is newly connected... set it to inactive if (isActive ) { - distributor.isActive = false + return Result } case _ => } @@ -80,7 +81,7 @@ class PowerDistributor extends Rotatable with PoweredNode { * @param demand */ def updateDemand(node: PoweredNode, demand: Int) { - energyStorageList.fi(n => n.node == node).foreach(n => { + energyStorageList.filter(n => n.node == node).foreach(n => { energyDemand -= n.amount energyDemand += demand n.amount = demand diff --git a/li/cil/oc/common/tileentity/PowerSupply.scala b/li/cil/oc/common/tileentity/PowerSupply.scala index e896cfcca..4d05c8ffa 100644 --- a/li/cil/oc/common/tileentity/PowerSupply.scala +++ b/li/cil/oc/common/tileentity/PowerSupply.scala @@ -46,7 +46,7 @@ class PowerSupply extends Rotatable with PoweredNode with IEnergySink with IPowe onLoaded() } if (!FMLCommonHandler.instance.getEffectiveSide.isClient) { - main.addEnergy(getPowerProvider().useEnergy(1, main.getDemand.toFloat / 5.0f, true) * 5) + main.addEnergy((getPowerProvider().useEnergy(1, main.getDemand.toFloat / 5.0f, true) * 5).toInt) } } @@ -54,13 +54,13 @@ class PowerSupply extends Rotatable with PoweredNode with IEnergySink with IPowe override def readFromNBT(nbt: NBTTagCompound) = { super.readFromNBT(nbt) getPowerProvider().readFromNBT(nbt) - storedEnergy = nbt.getDouble("storedEnergy") + } override def writeToNBT(nbt: NBTTagCompound) = { super.writeToNBT(nbt) getPowerProvider().writeToNBT(nbt) - nbt.setDouble("storedEnergy", storedEnergy) + } @@ -105,8 +105,9 @@ class PowerSupply extends Rotatable with PoweredNode with IEnergySink with IPowe * @return Energy not consumed (leftover) */ override def injectEnergyUnits(directionFrom: ForgeDirection, amount: Double): Double = { - lastInjectedEnergy = amount * 2 - main.addEnergy(amount*2) + lastInjectedEnergy = amount * 2.0 + main.addEnergy((amount*2.0).toInt) + 0 } /** @@ -193,7 +194,7 @@ class PowerSupply extends Rotatable with PoweredNode with IEnergySink with IPowe if (doReceive) { val energy = receive.getWatts() / 0.2F - main.addEnergy(energy) + main.addEnergy(energy.toInt) } receive.getWatts() } From 1c8927f45e1ae40346bd27cc6d14e399299673e6 Mon Sep 17 00:00:00 2001 From: Johannes Lohrer Date: Sun, 13 Oct 2013 16:25:44 +0200 Subject: [PATCH 3/4] made traits for all possibilities --- li/cil/oc/api/network/Producer.scala | 30 ++++ li/cil/oc/api/network/Provider.scala | 145 ++++++++++++++++++ .../{PoweredNode.scala => Receiver.scala} | 21 +-- .../tileentity/ComponentInventory.scala | 4 +- .../common/tileentity/PowerDistributor.scala | 124 +-------------- li/cil/oc/common/tileentity/PowerSupply.scala | 28 ++-- li/cil/oc/common/tileentity/Screen.scala | 4 +- 7 files changed, 211 insertions(+), 145 deletions(-) create mode 100644 li/cil/oc/api/network/Producer.scala create mode 100644 li/cil/oc/api/network/Provider.scala rename li/cil/oc/api/network/{PoweredNode.scala => Receiver.scala} (78%) diff --git a/li/cil/oc/api/network/Producer.scala b/li/cil/oc/api/network/Producer.scala new file mode 100644 index 000000000..857f57149 --- /dev/null +++ b/li/cil/oc/api/network/Producer.scala @@ -0,0 +1,30 @@ +package li.cil.oc.api.network + +trait Producer extends Receiver { + demand = 0 + + def powerDemand:Double= { + if (main != null) { + main.getDemand + } + else { + 0.0 + } + } + + def maxEnergy:Double= { + if (main != null) { + main.MAXENERGY + } + else { + 0.0 + } + + } + + def addEnergy(amount: Double) { + if (main != null) { + main.addEnergy(amount) + } + } +} diff --git a/li/cil/oc/api/network/Provider.scala b/li/cil/oc/api/network/Provider.scala new file mode 100644 index 000000000..314cdc2a8 --- /dev/null +++ b/li/cil/oc/api/network/Provider.scala @@ -0,0 +1,145 @@ +package li.cil.oc.api.network + +import scala.collection.mutable + + +trait Provider extends Node{ + + var isActive = false + var energyDemand =0.0 + var storedEnergy =0.0 + var MAXENERGY :Double + var energyStorageList = mutable.Set[EnergyStorage]() + override def receive(message: Message): Option[Array[Any]] = { + if (message.source != this) { + message.name match { + case "system.connect" => { + message.source match { + case distributor: Provider => + //if other powerDistributor connected and is active set inactive + if (distributor.isActive) { + isActive = false + + println("demand now (disabled) " + 0) + } + case _ => + } + } + case "power.find" => { + message.source match { + case distributor: Provider => + if (isActive) { + message.cancel() + return result(this) + } + case _ => + } + } + case "system.disconnect" => { + message.source match { + case distributor: Provider => + println("distri disc recieved") + if (distributor.isActive) { + + searchMain() + + } + case _ => + } + + } + case _ => // Ignore. + } + } + super.receive(message) + + } + + def connectNode(node: Receiver, amount: Int, priority: Int) { + if (energyStorageList.filter(x => x.node == node).isEmpty) { + energyStorageList += new EnergyStorage(node, amount, priority) + energyDemand += amount + } + + if (isActive) + println("demand now (connect)" + energyDemand) + } + + /** + * Updates the demand of the node to the given value + * @param node + * @param demand + */ + def updateDemand(node: Receiver, demand: Int) { + energyStorageList.filter(n => n.node == node).foreach(n => { + energyDemand -= n.amount + energyDemand += demand + n.amount = demand + }) + if (isActive) + println("demand now (update)" + energyDemand) + } + + def disconnectNode(node: Receiver) { + energyStorageList.clone().foreach(e => { + if (e == null || node == null) { + println("something null") + + } + else if (e.node == node) { + energyStorageList -= e + energyDemand -= e.amount + } + + }) + if (isActive) + println("demand now (disc) " + energyDemand) + } + + override protected def onConnect() { + //check if other distributors already are in the network + searchMain() + super.onConnect() + } + + + def update() { + //super.updateEntity() + if (isActive) { + if(storedEnergy>energyDemand){ + storedEnergy-=energyDemand + println("energy level now "+storedEnergy) + } + } + //TODO remove energy + } + + def getDemand = { + MAXENERGY - storedEnergy max 0.0 + } + + def addEnergy(amount: Double) { + storedEnergy += amount + + + } + + def searchMain() { + network.foreach(_.sendToVisible(this, "power.find") match { + case Some(Array(powerDistributor: Provider)) => { + println("found other distri") + isActive = false + } + case _ => { + println("no other") + isActive = true + + println("demand now (new main) " + energyDemand) + } + }) + } + + class EnergyStorage(var node: Receiver, var amount: Int) { + + } +} diff --git a/li/cil/oc/api/network/PoweredNode.scala b/li/cil/oc/api/network/Receiver.scala similarity index 78% rename from li/cil/oc/api/network/PoweredNode.scala rename to li/cil/oc/api/network/Receiver.scala index 77afda1be..81ca3f8dc 100644 --- a/li/cil/oc/api/network/PoweredNode.scala +++ b/li/cil/oc/api/network/Receiver.scala @@ -1,11 +1,11 @@ package li.cil.oc.api.network import li.cil.oc.common.tileentity.PowerDistributor +import li.cil.oc.api.network.{Visibility, Node, Message} import scala.collection.mutable -import li.cil.oc.api.network.Node -trait PoweredNode extends Node { +trait Receiver extends Node { var powerDistributors = mutable.Set[PowerDistributor]() @@ -15,9 +15,9 @@ trait PoweredNode extends Node { message.source match { case distributor: PowerDistributor => { println("connect") - if (powerDistributors.contains(distributor)) { + if (!powerDistributors.contains(distributor)) { powerDistributors += distributor - distributor.connectNode(this, _demand, _priority) + distributor.connectNode(this, _demand) } } case _ => @@ -59,16 +59,17 @@ trait PoweredNode extends Node { _demand = value } - private var _priority = 0 - - def priority = _priority - def main: PowerDistributor = { - null - //powerDistributors.filter(p => p.isActive).foreach(f => return f) + powerDistributors.find(p => p.isActive) match { + case Some(p:PowerDistributor) => p + case _=> null + } } + + def onPowerAvailable() + def onPowerLoss() } diff --git a/li/cil/oc/common/tileentity/ComponentInventory.scala b/li/cil/oc/common/tileentity/ComponentInventory.scala index f95957cf3..d83b27a45 100644 --- a/li/cil/oc/common/tileentity/ComponentInventory.scala +++ b/li/cil/oc/common/tileentity/ComponentInventory.scala @@ -2,7 +2,7 @@ package li.cil.oc.common.tileentity import li.cil.oc.Items import li.cil.oc.api.driver.Slot -import li.cil.oc.api.network.{PoweredNode, Node} +import li.cil.oc.api.network.{Receiver, Node} import li.cil.oc.common.item import li.cil.oc.server.component import li.cil.oc.server.driver.Registry @@ -12,7 +12,7 @@ import net.minecraft.nbt.NBTTagCompound import net.minecraft.nbt.NBTTagList import net.minecraft.world.World -trait ComponentInventory extends IInventory with PoweredNode { +trait ComponentInventory extends IInventory with Receiver { protected val inventory = new Array[ItemStack](inventorySize) protected val itemComponents = Array.fill[Option[Node]](inventorySize)(None) diff --git a/li/cil/oc/common/tileentity/PowerDistributor.scala b/li/cil/oc/common/tileentity/PowerDistributor.scala index 000f8283b..197a9e3fd 100644 --- a/li/cil/oc/common/tileentity/PowerDistributor.scala +++ b/li/cil/oc/common/tileentity/PowerDistributor.scala @@ -1,134 +1,18 @@ package li.cil.oc.common.tileentity -import li.cil.oc.api.network.{PoweredNode, Message, Visibility} +import li.cil.oc.api.network._ import scala.collection.mutable -import li.cil.oc.common.tileentity.Rotatable + +class PowerDistributor extends Rotatable with Provider { -class PowerDistributor extends Rotatable with PoweredNode { - - var isActive = true - var energyStorageList = mutable.Set[EnergyStorage]() - var energyDemand = 0 - var storedEnergy = 0 - var MAXENERGY = 2000 + MAXENERGY = 2000.0 override val name = "powerdistributor" override val visibility = Visibility.Network - override def receive(message: Message): Option[Array[Any]] = { - if (message.source != this) - {message.name match { - case "system.connect" => { - message.source match { - case distributor: PowerDistributor => - //if other powerDistributor connected and is active set inactive - if (distributor.isActive) { - isActive = false - println("demand now (disabled) " + 0) - } - case _ => - } - } - case "power.find" => { - message.source match { - case distributor: PowerDistributor => - //received request from other distributor that is newly connected... set it to inactive - - if (isActive ) { - return Result - } - case _ => - } - } - case "system.disconnect" => { - message.source match { - case distributor: PowerDistributor => - println("distri disc recieved") - if (distributor.isActive ) { - isActive = true - network.foreach(_.sendToVisible(this, "power.find")) - - println("demand now (new main) " + energyDemand) - } - case _ => - } - - } - case _ => // Ignore. - } - } - super.receive(message) - - } - - def connectNode(node: PoweredNode, amount: Int, priority: Int) { - if (energyStorageList.filter(x => x.node == node).isEmpty) { - energyStorageList += new EnergyStorage(node, amount, priority) - energyDemand += amount - } - - if (isActive) - println("demand now (connect)" + energyDemand) - } - - /** - * Updates the demand of the node to the given value - * @param node - * @param demand - */ - def updateDemand(node: PoweredNode, demand: Int) { - energyStorageList.filter(n => n.node == node).foreach(n => { - energyDemand -= n.amount - energyDemand += demand - n.amount = demand - }) - if (isActive) - println("demand now (update)" + energyDemand) - } - - def disconnectNode(node: PoweredNode) { - energyStorageList.clone().foreach(e => { - if (e == null || node == null) { - println("something null") - - } - else if (e.node == node) { - energyStorageList -= e - energyDemand -= e.amount - } - - }) - if (isActive) - println("demand now (disc) " + energyDemand) - } - - override protected def onConnect() { - //check if other distributors already are in the network - network.foreach(_.sendToVisible(this, "power.find")) - super.onConnect() - } - - - - override def updateEntity() { - super.updateEntity() - if (isActive) { - - } - //TODO remove energy - } - def getDemand = { - MAXENERGY-storedEnergy max 0 - } - def addEnergy(amount:Int){ - storedEnergy+=amount - } - class EnergyStorage(var node: PoweredNode, var amount: Int, var priority: Int) { - - } } diff --git a/li/cil/oc/common/tileentity/PowerSupply.scala b/li/cil/oc/common/tileentity/PowerSupply.scala index 2f958e5dd..db6004935 100644 --- a/li/cil/oc/common/tileentity/PowerSupply.scala +++ b/li/cil/oc/common/tileentity/PowerSupply.scala @@ -1,7 +1,8 @@ package li.cil.oc.common.tileentity import net.minecraft.tileentity.TileEntity -import li.cil.oc.api.network.{PoweredNode, Visibility} +import li.cil.oc.api.network._ + import net.minecraftforge.common.{ForgeDirection, MinecraftForge} import ic2.api.energy.event.{EnergyTileLoadEvent, EnergyTileUnloadEvent} import cpw.mods.fml.common.FMLCommonHandler @@ -19,7 +20,7 @@ import universalelectricity.core.electricity.ElectricityPack * Time: 20:37 * To change this template use File | Settings | File Templates. */ -class PowerSupply extends Rotatable with PoweredNode with IEnergySink with IPowerReceptor with IElectrical { +class PowerSupply extends Rotatable with Producer with IEnergySink with IPowerReceptor with IElectrical { var addedToEnet = false var powerHandler: PowerHandler = null @@ -46,7 +47,8 @@ class PowerSupply extends Rotatable with PoweredNode with IEnergySink with IPowe onLoaded() } if (!FMLCommonHandler.instance.getEffectiveSide.isClient) { - main.addEnergy((getPowerProvider().useEnergy(1, main.getDemand.toFloat / 5.0f, true) * 5).toInt) + + addEnergy((getPowerProvider().useEnergy(1, powerDemand.toFloat / 5.0f, true) * 5).toDouble) } } @@ -88,10 +90,14 @@ class PowerSupply extends Rotatable with PoweredNode with IEnergySink with IPowe * @return max accepted input in eu */ override def demandedEnergyUnits: Double = { - val needed = main.getDemand - if (needed > lastInjectedEnergy || needed > main.MAXENERGY / 2) + + val needed = powerDemand + if (needed > lastInjectedEnergy || needed > (maxEnergy / 2.0)) { + println("demand " + (needed / 2)) return needed / 2 - 0 + } + 0.0 + } /** @@ -106,7 +112,7 @@ class PowerSupply extends Rotatable with PoweredNode with IEnergySink with IPowe */ override def injectEnergyUnits(directionFrom: ForgeDirection, amount: Double): Double = { lastInjectedEnergy = amount * 2.0 - main.addEnergy((amount*2.0).toInt) + addEnergy(amount * 2.0) 0 } @@ -193,10 +199,10 @@ class PowerSupply extends Rotatable with PoweredNode with IEnergySink with IPowe if (receive == null) return 0.0F if (doReceive) { - val energy = receive.getWatts() / 0.2F - main.addEnergy(energy.toInt) + val energy = receive.getWatts / 0.2F + addEnergy(energy.toDouble) } - receive.getWatts() + receive.getWatts } /** @@ -214,7 +220,7 @@ class PowerSupply extends Rotatable with PoweredNode with IEnergySink with IPowe * @return How much energy does this TileEntity want? */ def getRequest(direction: ForgeDirection): Float = { - val diff = Math.floor(main.getDemand * 0.2F) + val diff = Math.floor(powerDemand * 0.2F) diff.toFloat max 0 } diff --git a/li/cil/oc/common/tileentity/Screen.scala b/li/cil/oc/common/tileentity/Screen.scala index f48b9bb86..2e0c774b6 100644 --- a/li/cil/oc/common/tileentity/Screen.scala +++ b/li/cil/oc/common/tileentity/Screen.scala @@ -1,13 +1,13 @@ package li.cil.oc.common.tileentity -import li.cil.oc.api.network.PoweredNode +import li.cil.oc.api.network.Receiver import li.cil.oc.client.gui import li.cil.oc.client.{PacketSender => ClientPacketSender} import li.cil.oc.common.component.ScreenEnvironment import li.cil.oc.server.{PacketSender => ServerPacketSender} import net.minecraft.nbt.NBTTagCompound -class Screen extends Rotatable with ScreenEnvironment with PoweredNode { +class Screen extends Rotatable with ScreenEnvironment with Receiver { var guiScreen: Option[gui.Screen] = None /** Read and reset to false from the tile entity renderer. */ From 56cc2c3dfde729727b76a3879d6c24eb016dc23a Mon Sep 17 00:00:00 2001 From: Johannes Lohrer Date: Sun, 13 Oct 2013 19:13:04 +0200 Subject: [PATCH 4/4] updated energy distribution but not with final values --- li/cil/oc/api/network/Provider.scala | 81 ++++++++++++---- li/cil/oc/api/network/Receiver.scala | 94 ++++++++++++++++--- li/cil/oc/common/tileentity/Computer.scala | 2 +- .../common/tileentity/PowerDistributor.scala | 16 +++- li/cil/oc/common/tileentity/PowerSupply.scala | 2 +- 5 files changed, 158 insertions(+), 37 deletions(-) diff --git a/li/cil/oc/api/network/Provider.scala b/li/cil/oc/api/network/Provider.scala index 314cdc2a8..93745dcd6 100644 --- a/li/cil/oc/api/network/Provider.scala +++ b/li/cil/oc/api/network/Provider.scala @@ -1,15 +1,33 @@ package li.cil.oc.api.network import scala.collection.mutable +import net.minecraft.nbt.NBTTagCompound trait Provider extends Node{ var isActive = false + var updateNodes = false var energyDemand =0.0 var storedEnergy =0.0 - var MAXENERGY :Double + var MAXENERGY :Double =2000.0 + + var energyStorageList = mutable.Set[EnergyStorage]() + + override def load(nbt: NBTTagCompound) = { + super.load(nbt) + storedEnergy = nbt.getDouble("storedEnergy") + + } + + override def save(nbt: NBTTagCompound) = { + super.save(nbt) + nbt.setDouble("storedEnergy",storedEnergy) + + + } + override def receive(message: Message): Option[Array[Any]] = { if (message.source != this) { message.name match { @@ -19,7 +37,6 @@ trait Provider extends Node{ //if other powerDistributor connected and is active set inactive if (distributor.isActive) { isActive = false - println("demand now (disabled) " + 0) } case _ => @@ -40,9 +57,7 @@ trait Provider extends Node{ case distributor: Provider => println("distri disc recieved") if (distributor.isActive) { - searchMain() - } case _ => } @@ -55,10 +70,16 @@ trait Provider extends Node{ } - def connectNode(node: Receiver, amount: Int, priority: Int) { - if (energyStorageList.filter(x => x.node == node).isEmpty) { - energyStorageList += new EnergyStorage(node, amount, priority) + /** + * Connect a reciever to the provider + * @param receiver + * @param amount + */ + def connectNode(receiver: Receiver, amount: Double) { + if (energyStorageList.filter(x => x.node == receiver).isEmpty) { + energyStorageList += new EnergyStorage(receiver, amount) energyDemand += amount + updateNodes = true } if (isActive) @@ -67,11 +88,11 @@ trait Provider extends Node{ /** * Updates the demand of the node to the given value - * @param node + * @param receiver * @param demand */ - def updateDemand(node: Receiver, demand: Int) { - energyStorageList.filter(n => n.node == node).foreach(n => { + def updateDemand(receiver: Receiver, demand: Double) { + energyStorageList.filter(n => n.node == receiver).foreach(n => { energyDemand -= n.amount energyDemand += demand n.amount = demand @@ -80,15 +101,20 @@ trait Provider extends Node{ println("demand now (update)" + energyDemand) } - def disconnectNode(node: Receiver) { + def disconnectNode(receiver: Receiver) { energyStorageList.clone().foreach(e => { - if (e == null || node == null) { + if (e == null || receiver == null) { println("something null") } - else if (e.node == node) { + else if (e.node == receiver) { energyStorageList -= e energyDemand -= e.amount + if(isActive) + { + receiver.unConnect() + updateNodes = true + } } }) @@ -102,16 +128,33 @@ trait Provider extends Node{ super.onConnect() } - - def update() { - //super.updateEntity() + private var hasEnergy = false + override def update() { + super.update() + //check if is main if (isActive) { + //if enough energy is available to supply all recievers if(storedEnergy>energyDemand){ storedEnergy-=energyDemand + if(!hasEnergy) + updateNodes = true + hasEnergy = true println("energy level now "+storedEnergy) } + else{ + if(hasEnergy) + updateNodes = true + hasEnergy = false + } + //if nodes must be updated send message to them + if(updateNodes){ + if(hasEnergy) + energyStorageList.foreach(storage=>storage.node.connect()) + else + energyStorageList.foreach(storage=>storage.node.unConnect()) + updateNodes = false + } } - //TODO remove energy } def getDemand = { @@ -133,13 +176,13 @@ trait Provider extends Node{ case _ => { println("no other") isActive = true - + updateNodes = true println("demand now (new main) " + energyDemand) } }) } - class EnergyStorage(var node: Receiver, var amount: Int) { + class EnergyStorage(var node: Receiver, var amount: Double) { } } diff --git a/li/cil/oc/api/network/Receiver.scala b/li/cil/oc/api/network/Receiver.scala index 81ca3f8dc..79240b2c7 100644 --- a/li/cil/oc/api/network/Receiver.scala +++ b/li/cil/oc/api/network/Receiver.scala @@ -1,13 +1,22 @@ package li.cil.oc.api.network import li.cil.oc.common.tileentity.PowerDistributor -import li.cil.oc.api.network.{Visibility, Node, Message} import scala.collection.mutable +import net.minecraft.nbt.NBTTagCompound trait Receiver extends Node { var powerDistributors = mutable.Set[PowerDistributor]() + private var _demand = 2.0 + + def demand = _demand + + def demand_=(value: Double) = { + + powerDistributors.foreach(e => e.updateDemand(this, value)) + _demand = value + } override def receive(message: Message): Option[Array[Any]] = { message.name match { @@ -49,27 +58,84 @@ trait Receiver extends Node { } - private var _demand = 0 - - def demand = _demand - - def demand_=(value: Int) = { - - powerDistributors.foreach(e => e.updateDemand(this, value)) - _demand = value - } def main: PowerDistributor = { powerDistributors.find(p => p.isActive) match { - case Some(p:PowerDistributor) => p - case _=> null + case Some(p: PowerDistributor) => p + case _ => null } } + override def load(nbt: NBTTagCompound) = { + super.load(nbt) + buffer = nbt.getDouble("buffer") + } - def onPowerAvailable() - def onPowerLoss() + override def save(nbt: NBTTagCompound) = { + super.save(nbt) + nbt.setDouble("buffer",buffer) + } + + private var buffer = 0.0 + private val maxBuffer = 100.0 + private var hasEnergy = false + private var isConnected = false + + override def update() { + super.update() + //if has enough energy to operate + if (isConnected) { + //increase buffer + if (maxBuffer > buffer + 1) + buffer += 1 + //notify if energy wasn't available before + if (!hasEnergy) { + hasEnergy = true + onPowerAvailable() + } + } + else { + //continue running until we are out of energy + if (buffer - demand < 0) { + if (hasEnergy) { + hasEnergy = false + onPowerLoss() + } + } else { + buffer -= demand + } + } + } + + /** + * called from the producer when he has enough energy to operate + */ + final def connect() { + isConnected = true + } + + /** + * Called from the producer when there is not enough energy to operate + */ + final def unConnect() { + isConnected = false + } + + /** + * Called when the receiver has enough power to operate. + */ + def onPowerAvailable() { + println("received energy") + } + + /** + * Called when the receiver has no power to operate. This can happen at a later time + * then unConnect was called, because of the internal capacity + */ + def onPowerLoss() { + println("no more energy") + } } diff --git a/li/cil/oc/common/tileentity/Computer.scala b/li/cil/oc/common/tileentity/Computer.scala index 9510a2cba..3443b3bbb 100644 --- a/li/cil/oc/common/tileentity/Computer.scala +++ b/li/cil/oc/common/tileentity/Computer.scala @@ -60,7 +60,7 @@ class Computer(isClient: Boolean) extends Rotatable with component.Computer.Envi override def updateEntity() = if (!worldObj.isRemote) { computer.update() - + update() if (hasChanged.get) worldObj.markTileEntityChunkModified(xCoord, yCoord, zCoord, this) if (isRunning != computer.isRunning) diff --git a/li/cil/oc/common/tileentity/PowerDistributor.scala b/li/cil/oc/common/tileentity/PowerDistributor.scala index 197a9e3fd..54b2eafdb 100644 --- a/li/cil/oc/common/tileentity/PowerDistributor.scala +++ b/li/cil/oc/common/tileentity/PowerDistributor.scala @@ -2,17 +2,29 @@ package li.cil.oc.common.tileentity import li.cil.oc.api.network._ import scala.collection.mutable +import net.minecraft.nbt.NBTTagCompound class PowerDistributor extends Rotatable with Provider { - MAXENERGY = 2000.0 + //MAXENERGY = 2000.0.toDouble override val name = "powerdistributor" override val visibility = Visibility.Network + override def updateEntity(){ + super.updateEntity() + update() + } + override def readFromNBT(nbt: NBTTagCompound) = { + super.readFromNBT(nbt) + load(nbt) + } - + override def writeToNBT(nbt: NBTTagCompound) = { + super.writeToNBT(nbt) + save(nbt) + } } diff --git a/li/cil/oc/common/tileentity/PowerSupply.scala b/li/cil/oc/common/tileentity/PowerSupply.scala index db6004935..01339829e 100644 --- a/li/cil/oc/common/tileentity/PowerSupply.scala +++ b/li/cil/oc/common/tileentity/PowerSupply.scala @@ -43,6 +43,7 @@ class PowerSupply extends Rotatable with Producer with IEnergySink with IPowerRe override def updateEntity() { super.updateEntity() + update() if (!addedToEnet) { onLoaded() } @@ -93,7 +94,6 @@ class PowerSupply extends Rotatable with Producer with IEnergySink with IPowerRe val needed = powerDemand if (needed > lastInjectedEnergy || needed > (maxEnergy / 2.0)) { - println("demand " + (needed / 2)) return needed / 2 } 0.0