From ec7bd3523cc2f088ad12de088df0f056a2ac8df2 Mon Sep 17 00:00:00 2001 From: Regalis Date: Fri, 5 Feb 2016 22:10:51 +0200 Subject: [PATCH] Separate recharge and power_out connections in batteries (todo: configure recharge connections as power connections) --- .../Content/Items/Electricity/poweritems.xml | 1 + .../Items/Components/Power/PowerContainer.cs | 85 ++++++++++++++----- .../Items/Components/Power/PowerTransfer.cs | 14 ++- 3 files changed, 78 insertions(+), 22 deletions(-) diff --git a/Subsurface/Content/Items/Electricity/poweritems.xml b/Subsurface/Content/Items/Electricity/poweritems.xml index 847a159f6..658ea4a5f 100644 --- a/Subsurface/Content/Items/Electricity/poweritems.xml +++ b/Subsurface/Content/Items/Electricity/poweritems.xml @@ -55,6 +55,7 @@ + diff --git a/Subsurface/Source/Items/Components/Power/PowerContainer.cs b/Subsurface/Source/Items/Components/Power/PowerContainer.cs index 858f51e74..7c9d3d0b0 100644 --- a/Subsurface/Source/Items/Components/Power/PowerContainer.cs +++ b/Subsurface/Source/Items/Components/Power/PowerContainer.cs @@ -12,6 +12,8 @@ namespace Barotrauma.Items.Components float charge; + float rechargeVoltage, outputVoltage; + //how fast the battery can be recharged float maxRechargeSpeed; @@ -21,6 +23,12 @@ namespace Barotrauma.Items.Components float maxOutput; + public float CurrPowerOutput + { + get; + private set; + } + [Editable, HasDefaultValue(10.0f, true)] public float MaxOutPut { @@ -112,6 +120,7 @@ namespace Barotrauma.Items.Components foreach (Connection c in item.Connections) { + if (c.Name.Contains("recharge")) continue; foreach (Connection c2 in c.Recipients) { PowerTransfer pt = c2.Item.GetComponent(); @@ -122,32 +131,37 @@ namespace Barotrauma.Items.Components } - float gridRate = voltage; + //float gridRate = voltage; - if (gridRate>minVoltage) - { - ApplyStatusEffects(ActionType.OnActive, deltaTime, null); - } + //if (gridRate>minVoltage) + //{ + // ApplyStatusEffects(ActionType.OnActive, deltaTime, null); + //} //recharge - if (gridRate >= chargeRate) + //if (gridRate >= chargeRate) + //{ + if (charge >= capacity) { - if (charge >= capacity) - { - currPowerConsumption = 0.0f; - charge = capacity; - return; - } + rechargeVoltage = 0.0f; + charge = capacity; - currPowerConsumption = MathHelper.Lerp(currPowerConsumption, rechargeSpeed, 0.05f); - Charge += currPowerConsumption*voltage / 3600.0f; + CurrPowerConsumption = 0.0f; } + else + { + currPowerConsumption = MathHelper.Lerp(currPowerConsumption, rechargeSpeed, 0.05f); + Charge += currPowerConsumption * rechargeVoltage / 3600.0f; + } + + //} + //provide power to the grid - else if (gridLoad > 0.0f) + if (gridLoad > 0.0f) { if (charge <= 0.0f) { - currPowerConsumption = 0.0f; + CurrPowerOutput = 0.0f; charge = 0.0f; return; } @@ -157,21 +171,34 @@ namespace Barotrauma.Items.Components // -maxOutput * chargeRate, // 0.1f); - currPowerConsumption = MathHelper.Lerp( - currPowerConsumption, - -Math.Min(maxOutput * chargeRate, gridLoad - (gridLoad * voltage)), + if (outputVoltage < 1.0f) + { + CurrPowerOutput = MathHelper.Lerp( + CurrPowerOutput, Math.Min(maxOutput * chargeRate, gridLoad), 0.05f); + } + else + { + CurrPowerOutput = MathHelper.Lerp(CurrPowerOutput, 0.0f, 0.05f); + } + + CurrPowerOutput = MathHelper.Lerp( + CurrPowerOutput, + Math.Min(maxOutput * chargeRate, gridLoad - (gridLoad * outputVoltage)), 0.05f); + + //powerConsumption = MathHelper.Lerp( // powerConsumption, // -Math.Min(maxOutput * chargeRate, gridLoad - (power)), // 0.1f); //powerConsumption = Math.Min(powerConsumption, 0.0f); - charge -= -currPowerConsumption / chargeRate / 3600.0f; + charge -= CurrPowerOutput / 3600.0f; } - voltage = 0.0f; + rechargeVoltage = 0.0f; + outputVoltage = 0.0f; } public override bool AIOperate(float deltaTime, Character character, AIObjectiveOperateItem objective) @@ -181,6 +208,22 @@ namespace Barotrauma.Items.Components return true; } + public override void ReceiveSignal(string signal, Connection connection, Item sender, float power) + { + if (!connection.IsPower) return; + + if (connection.Name.Contains("recharge")) + { + rechargeVoltage = power; + } + else + { + outputVoltage = power; + } + //if (currPowerConsumption == 0.0f) voltage = 0.0f; + //if (connection.IsPower) voltage = power; + } + public override void Draw(SpriteBatch spriteBatch, bool editing) { base.Draw(spriteBatch); diff --git a/Subsurface/Source/Items/Components/Power/PowerTransfer.cs b/Subsurface/Source/Items/Components/Power/PowerTransfer.cs index 151bd8aae..d10bc6fc9 100644 --- a/Subsurface/Source/Items/Components/Power/PowerTransfer.cs +++ b/Subsurface/Source/Items/Components/Power/PowerTransfer.cs @@ -113,7 +113,7 @@ namespace Barotrauma.Items.Components if (!c.IsPower) continue; foreach (Connection recipient in c.Recipients) { - if (recipient == null || !recipient.IsPower) continue; + if (recipient == null || !c.IsPower) continue; Item it = recipient.Item; if (it == null) continue; @@ -124,11 +124,23 @@ namespace Barotrauma.Items.Components if (powered == null) continue; PowerTransfer powerTransfer = powered as PowerTransfer; + PowerContainer powerContainer = powered as PowerContainer; if (powerTransfer != null) { if (powerTransfer.updateTimer>0) continue; powerTransfer.CheckJunctions(deltaTime); } + else if (powerContainer != null) + { + if (recipient.Name.Contains("recharge")) + { + fullLoad += powerContainer.CurrPowerConsumption; + } + else + { + fullPower += powerContainer.CurrPowerOutput; + } + } else { connectedList.Add(powered);