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. */