Stop BatterySystem from iterating over every battery just to recharge micro-reactors (#5393)

This commit is contained in:
20kdc
2021-11-18 19:02:17 +00:00
committed by GitHub
parent f8b6cfca6c
commit 856e570c7b
5 changed files with 30 additions and 15 deletions

View File

@@ -72,6 +72,7 @@ namespace Content.Client.Entry
"Mop", "Mop",
"Bucket", "Bucket",
"CableVis", "CableVis",
"BatterySelfRecharger",
"Puddle", "Puddle",
"Stomach", "Stomach",
"CanSpill", "CanSpill",

View File

@@ -34,10 +34,6 @@ namespace Content.Server.Power.Components
/// </summary> /// </summary>
[ViewVariables] public bool IsFullyCharged => MathHelper.CloseToPercent(CurrentCharge, MaxCharge); [ViewVariables] public bool IsFullyCharged => MathHelper.CloseToPercent(CurrentCharge, MaxCharge);
[ViewVariables(VVAccess.ReadWrite)] [DataField("autoRecharge")] public bool AutoRecharge { get; set; }
[ViewVariables(VVAccess.ReadWrite)] [DataField("autoRechargeRate")] public float AutoRechargeRate { get; set; }
/// <summary> /// <summary>
/// If sufficient charge is avaiable on the battery, use it. Otherwise, don't. /// If sufficient charge is avaiable on the battery, use it. Otherwise, don't.
/// </summary> /// </summary>
@@ -89,12 +85,5 @@ namespace Content.Server.Power.Components
_currentCharge = MathHelper.Clamp(newChargeAmount, 0, MaxCharge); _currentCharge = MathHelper.Clamp(newChargeAmount, 0, MaxCharge);
OnChargeChanged(); OnChargeChanged();
} }
public void OnUpdate(float frameTime)
{
if (!AutoRecharge) return;
if (IsFullyCharged) return;
CurrentCharge += AutoRechargeRate * frameTime;
}
} }
} }

View File

@@ -0,0 +1,21 @@
using System;
using Robust.Shared.GameObjects;
using Robust.Shared.Maths;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Content.Server.Power.Components
{
/// <summary>
/// Self-recharging battery.
/// </summary>
[RegisterComponent]
public class BatterySelfRechargerComponent : Component
{
public override string Name => "BatterySelfRecharger";
[ViewVariables(VVAccess.ReadWrite)] [DataField("autoRecharge")] public bool AutoRecharge { get; set; }
[ViewVariables(VVAccess.ReadWrite)] [DataField("autoRechargeRate")] public float AutoRechargeRate { get; set; }
}
}

View File

@@ -17,7 +17,7 @@ namespace Content.Server.Power.EntitySystems
private void PreSync(NetworkBatteryPreSync ev) private void PreSync(NetworkBatteryPreSync ev)
{ {
foreach (var (bat, netBat) in EntityManager.EntityQuery<BatteryComponent, PowerNetworkBatteryComponent>()) foreach (var (netBat, bat) in EntityManager.EntityQuery<PowerNetworkBatteryComponent, BatteryComponent>())
{ {
netBat.NetworkBattery.Capacity = bat.MaxCharge; netBat.NetworkBattery.Capacity = bat.MaxCharge;
netBat.NetworkBattery.CurrentStorage = bat.CurrentCharge; netBat.NetworkBattery.CurrentStorage = bat.CurrentCharge;
@@ -26,7 +26,7 @@ namespace Content.Server.Power.EntitySystems
private void PostSync(NetworkBatteryPostSync ev) private void PostSync(NetworkBatteryPostSync ev)
{ {
foreach (var (bat, netBat) in EntityManager.EntityQuery<BatteryComponent, PowerNetworkBatteryComponent>()) foreach (var (netBat, bat) in EntityManager.EntityQuery<PowerNetworkBatteryComponent, BatteryComponent>())
{ {
bat.CurrentCharge = netBat.NetworkBattery.CurrentStorage; bat.CurrentCharge = netBat.NetworkBattery.CurrentStorage;
} }
@@ -34,9 +34,11 @@ namespace Content.Server.Power.EntitySystems
public override void Update(float frameTime) public override void Update(float frameTime)
{ {
foreach (var comp in EntityManager.EntityQuery<BatteryComponent>()) foreach (var (comp, batt) in EntityManager.EntityQuery<BatterySelfRechargerComponent, BatteryComponent>())
{ {
comp.OnUpdate(frameTime); if (!comp.AutoRecharge) continue;
if (batt.IsFullyCharged) continue;
batt.CurrentCharge += comp.AutoRechargeRate * frameTime;
} }
} }
} }

View File

@@ -145,6 +145,7 @@
- type: PowerCell - type: PowerCell
maxCharge: 50 maxCharge: 50
startingCharge: 50 startingCharge: 50
- type: BatterySelfRecharger
autoRecharge: true autoRecharge: true
autoRechargeRate: 0.16667 #takes about 5 minutes to charge itself back to full autoRechargeRate: 0.16667 #takes about 5 minutes to charge itself back to full
- type: Appearance - type: Appearance
@@ -165,6 +166,7 @@
- type: PowerCell - type: PowerCell
maxCharge: 600 #lights drain 3/s but recharge of 2 makes this 1/s. Therefore 600 is 10 minutes of light. maxCharge: 600 #lights drain 3/s but recharge of 2 makes this 1/s. Therefore 600 is 10 minutes of light.
startingCharge: 600 startingCharge: 600
- type: BatterySelfRecharger
autoRecharge: true autoRecharge: true
autoRechargeRate: 2 #recharge of 2 makes total drain 1w / s so max charge is 1:1 with time. Time to fully charge should be 5 minutes. Having recharge gives light an extended flicker period which gives you some warning to return to light area. autoRechargeRate: 2 #recharge of 2 makes total drain 1w / s so max charge is 1:1 with time. Time to fully charge should be 5 minutes. Having recharge gives light an extended flicker period which gives you some warning to return to light area.
- type: Appearance - type: Appearance