From fabc580df953da9b8f6b9e73fd1bedf44a1afcbe Mon Sep 17 00:00:00 2001 From: collinlunn <60152240+collinlunn@users.noreply.github.com> Date: Wed, 17 Feb 2021 06:51:30 -0700 Subject: [PATCH] ApcNet updating fix (#3078) * GridPowerComponent * ApcNet Powered update bugfix * PowerTest fix * Add GridPower to Saltern * test fix * Update canceling cleanup * code cleanup * nullable & code cleanup for test * undo power test nullable * Replaces GridPowerSystem with ApcNetSystem * build fix * Update Content.Server/GameObjects/EntitySystems/ApcNetSystem.cs Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> --- Content.IntegrationTests/Tests/PowerTest.cs | 7 +-- .../NodeGroups/ApcNetNodeGroup.cs | 52 ++++++++++++++++++- .../NodeContainer/NodeGroups/INodeGroup.cs | 8 +++ .../GameObjects/EntitySystems/ApcNetSystem.cs | 39 ++++++++++++++ .../EntitySystems/Power/PowerApcSystem.cs | 9 ---- 5 files changed, 101 insertions(+), 14 deletions(-) create mode 100644 Content.Server/GameObjects/EntitySystems/ApcNetSystem.cs diff --git a/Content.IntegrationTests/Tests/PowerTest.cs b/Content.IntegrationTests/Tests/PowerTest.cs index 51db85c585..c2fe594c5c 100644 --- a/Content.IntegrationTests/Tests/PowerTest.cs +++ b/Content.IntegrationTests/Tests/PowerTest.cs @@ -1,4 +1,4 @@ -using System.Threading.Tasks; +using System.Threading.Tasks; using Content.Server.GameObjects.Components; using Content.Server.GameObjects.Components.Power; using Content.Server.GameObjects.Components.Power.ApcNetComponents; @@ -225,8 +225,9 @@ namespace Content.IntegrationTests.Tests { var mapMan = IoCManager.Resolve(); var entityMan = IoCManager.Resolve(); - mapMan.CreateMap(new MapId(1)); - var grid = mapMan.CreateGrid(new MapId(1)); + var mapId = new MapId(1); + mapMan.CreateMap(mapId); + var grid = mapMan.CreateGrid(mapId); var apcEnt = entityMan.SpawnEntity("ApcDummy", grid.ToCoordinates(0, 0)); var apcExtensionEnt = entityMan.SpawnEntity("ApcExtensionCableDummy", grid.ToCoordinates(0, 1)); diff --git a/Content.Server/GameObjects/Components/NodeContainer/NodeGroups/ApcNetNodeGroup.cs b/Content.Server/GameObjects/Components/NodeContainer/NodeGroups/ApcNetNodeGroup.cs index d8ef14d799..4da747c4a1 100644 --- a/Content.Server/GameObjects/Components/NodeContainer/NodeGroups/ApcNetNodeGroup.cs +++ b/Content.Server/GameObjects/Components/NodeContainer/NodeGroups/ApcNetNodeGroup.cs @@ -1,8 +1,12 @@ #nullable enable using System.Collections.Generic; using System.Linq; +using Content.Server.GameObjects.Components.NodeContainer.Nodes; using Content.Server.GameObjects.Components.Power; using Content.Server.GameObjects.Components.Power.ApcNetComponents; +using Content.Server.GameObjects.EntitySystems; +using Robust.Shared.GameObjects; +using Robust.Shared.Map; using Robust.Shared.Utility; using Robust.Shared.ViewVariables; @@ -23,6 +27,8 @@ namespace Content.Server.GameObjects.Components.NodeContainer.NodeGroups void UpdatePowerProviderReceivers(PowerProviderComponent provider, int oldLoad, int newLoad); void Update(float frameTime); + + GridId? GridId { get; } } [NodeGroup(NodeGroupID.Apc)] @@ -44,10 +50,52 @@ namespace Content.Server.GameObjects.Components.NodeContainer.NodeGroups [ViewVariables] private int TotalPowerReceiverLoad { get => _totalPowerReceiverLoad; set => SetTotalPowerReceiverLoad(value); } + + GridId? IApcNet.GridId => GridId; + private int _totalPowerReceiverLoad = 0; public static readonly IApcNet NullNet = new NullApcNet(); + public override void Initialize(Node sourceNode) + { + base.Initialize(sourceNode); + + EntitySystem.Get().AddApcNet(this); + } + + protected override void AfterRemake(IEnumerable newGroups) + { + base.AfterRemake(newGroups); + + foreach (var group in newGroups) + { + if (group is not ApcNetNodeGroup apcNet) + continue; + + apcNet.Powered = Powered; + } + + StopUpdates(); + } + + protected override void OnGivingNodesForCombine(INodeGroup newGroup) + { + base.OnGivingNodesForCombine(newGroup); + + if (newGroup is ApcNetNodeGroup apcNet) + { + apcNet.Powered = Powered; + } + + StopUpdates(); + } + + private void StopUpdates() + { + EntitySystem.Get().RemoveApcNet(this); + } + #region IApcNet Methods protected override void SetNetConnectorNet(BaseApcNetComponent netConnectorComponent) @@ -57,7 +105,7 @@ namespace Content.Server.GameObjects.Components.NodeContainer.NodeGroups public void AddApc(ApcComponent apc) { - if (!apc.Owner.TryGetComponent(out var battery)) + if (!apc.Owner.TryGetComponent(out BatteryComponent? battery)) { return; } @@ -162,7 +210,7 @@ namespace Content.Server.GameObjects.Components.NodeContainer.NodeGroups /// It is important that this returns false, so s with a have no power. /// public bool Powered => false; - + public GridId? GridId => default; public void AddApc(ApcComponent apc) { } public void AddPowerProvider(PowerProviderComponent provider) { } public void RemoveApc(ApcComponent apc) { } diff --git a/Content.Server/GameObjects/Components/NodeContainer/NodeGroups/INodeGroup.cs b/Content.Server/GameObjects/Components/NodeContainer/NodeGroups/INodeGroup.cs index a7b4e87099..615b6eb4e4 100644 --- a/Content.Server/GameObjects/Components/NodeContainer/NodeGroups/INodeGroup.cs +++ b/Content.Server/GameObjects/Components/NodeContainer/NodeGroups/INodeGroup.cs @@ -36,6 +36,12 @@ namespace Content.Server.GameObjects.Components.NodeContainer.NodeGroups [ViewVariables] public int NodeCount => Nodes.Count; + /// + /// Debug variable to indicate that this NodeGroup should not be being used by anything. + /// + [ViewVariables] + public bool Removed { get; private set; } = false; + public static readonly INodeGroup NullGroup = new NullNodeGroup(); protected GridId GridId { get; private set;} @@ -70,6 +76,7 @@ namespace Content.Server.GameObjects.Components.NodeContainer.NodeGroups { node.NodeGroup = newGroup; } + Removed = true; } /// @@ -92,6 +99,7 @@ namespace Content.Server.GameObjects.Components.NodeContainer.NodeGroups } } AfterRemake(newGroups); + Removed = true; } protected virtual void OnAddNode(Node node) { } diff --git a/Content.Server/GameObjects/EntitySystems/ApcNetSystem.cs b/Content.Server/GameObjects/EntitySystems/ApcNetSystem.cs new file mode 100644 index 0000000000..fe21ca6193 --- /dev/null +++ b/Content.Server/GameObjects/EntitySystems/ApcNetSystem.cs @@ -0,0 +1,39 @@ +#nullable enable +using Content.Server.GameObjects.Components.NodeContainer.NodeGroups; +using JetBrains.Annotations; +using Robust.Server.Timing; +using Robust.Shared.GameObjects; +using Robust.Shared.IoC; +using Robust.Shared.Map; +using System.Collections.Generic; + +namespace Content.Server.GameObjects.EntitySystems +{ + [UsedImplicitly] + internal sealed class ApcNetSystem : EntitySystem + { + [Dependency] private readonly IPauseManager _pauseManager = default!; + + private HashSet _apcNets = new(); + + public override void Update(float frameTime) + { + foreach (var apcNet in _apcNets) + { + var gridId = apcNet.GridId; + if (gridId != null && !_pauseManager.IsGridPaused(gridId.Value)) + apcNet.Update(frameTime); + } + } + + public void AddApcNet(ApcNetNodeGroup apcNet) + { + _apcNets.Add(apcNet); + } + + public void RemoveApcNet(ApcNetNodeGroup apcNet) + { + _apcNets.Remove(apcNet); + } + } +} diff --git a/Content.Server/GameObjects/EntitySystems/Power/PowerApcSystem.cs b/Content.Server/GameObjects/EntitySystems/Power/PowerApcSystem.cs index b977d0e80a..f880f79e88 100644 --- a/Content.Server/GameObjects/EntitySystems/Power/PowerApcSystem.cs +++ b/Content.Server/GameObjects/EntitySystems/Power/PowerApcSystem.cs @@ -1,6 +1,4 @@ #nullable enable -using System.Collections.Generic; -using Content.Server.GameObjects.Components.NodeContainer.NodeGroups; using Content.Server.GameObjects.Components.Power.ApcNetComponents; using JetBrains.Annotations; using Robust.Shared.GameObjects; @@ -12,17 +10,10 @@ namespace Content.Server.GameObjects.EntitySystems { public override void Update(float frameTime) { - var uniqueApcNets = new HashSet(); //could be improved by maintaining set instead of getting collection every frame foreach (var apc in ComponentManager.EntityQuery(false)) { - uniqueApcNets.Add(apc.Net); apc.Update(); } - - foreach (var apcNet in uniqueApcNets) - { - apcNet.Update(frameTime); - } } } }