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>
This commit is contained in:
collinlunn
2021-02-17 06:51:30 -07:00
committed by GitHub
parent 55d65889ae
commit fabc580df9
5 changed files with 101 additions and 14 deletions

View File

@@ -1,4 +1,4 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using Content.Server.GameObjects.Components; using Content.Server.GameObjects.Components;
using Content.Server.GameObjects.Components.Power; using Content.Server.GameObjects.Components.Power;
using Content.Server.GameObjects.Components.Power.ApcNetComponents; using Content.Server.GameObjects.Components.Power.ApcNetComponents;
@@ -225,8 +225,9 @@ namespace Content.IntegrationTests.Tests
{ {
var mapMan = IoCManager.Resolve<IMapManager>(); var mapMan = IoCManager.Resolve<IMapManager>();
var entityMan = IoCManager.Resolve<IEntityManager>(); var entityMan = IoCManager.Resolve<IEntityManager>();
mapMan.CreateMap(new MapId(1)); var mapId = new MapId(1);
var grid = mapMan.CreateGrid(new MapId(1)); mapMan.CreateMap(mapId);
var grid = mapMan.CreateGrid(mapId);
var apcEnt = entityMan.SpawnEntity("ApcDummy", grid.ToCoordinates(0, 0)); var apcEnt = entityMan.SpawnEntity("ApcDummy", grid.ToCoordinates(0, 0));
var apcExtensionEnt = entityMan.SpawnEntity("ApcExtensionCableDummy", grid.ToCoordinates(0, 1)); var apcExtensionEnt = entityMan.SpawnEntity("ApcExtensionCableDummy", grid.ToCoordinates(0, 1));

View File

@@ -1,8 +1,12 @@
#nullable enable #nullable enable
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Content.Server.GameObjects.Components.NodeContainer.Nodes;
using Content.Server.GameObjects.Components.Power; using Content.Server.GameObjects.Components.Power;
using Content.Server.GameObjects.Components.Power.ApcNetComponents; 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.Utility;
using Robust.Shared.ViewVariables; using Robust.Shared.ViewVariables;
@@ -23,6 +27,8 @@ namespace Content.Server.GameObjects.Components.NodeContainer.NodeGroups
void UpdatePowerProviderReceivers(PowerProviderComponent provider, int oldLoad, int newLoad); void UpdatePowerProviderReceivers(PowerProviderComponent provider, int oldLoad, int newLoad);
void Update(float frameTime); void Update(float frameTime);
GridId? GridId { get; }
} }
[NodeGroup(NodeGroupID.Apc)] [NodeGroup(NodeGroupID.Apc)]
@@ -44,10 +50,52 @@ namespace Content.Server.GameObjects.Components.NodeContainer.NodeGroups
[ViewVariables] [ViewVariables]
private int TotalPowerReceiverLoad { get => _totalPowerReceiverLoad; set => SetTotalPowerReceiverLoad(value); } private int TotalPowerReceiverLoad { get => _totalPowerReceiverLoad; set => SetTotalPowerReceiverLoad(value); }
GridId? IApcNet.GridId => GridId;
private int _totalPowerReceiverLoad = 0; private int _totalPowerReceiverLoad = 0;
public static readonly IApcNet NullNet = new NullApcNet(); public static readonly IApcNet NullNet = new NullApcNet();
public override void Initialize(Node sourceNode)
{
base.Initialize(sourceNode);
EntitySystem.Get<ApcNetSystem>().AddApcNet(this);
}
protected override void AfterRemake(IEnumerable<INodeGroup> 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<ApcNetSystem>().RemoveApcNet(this);
}
#region IApcNet Methods #region IApcNet Methods
protected override void SetNetConnectorNet(BaseApcNetComponent netConnectorComponent) protected override void SetNetConnectorNet(BaseApcNetComponent netConnectorComponent)
@@ -57,7 +105,7 @@ namespace Content.Server.GameObjects.Components.NodeContainer.NodeGroups
public void AddApc(ApcComponent apc) public void AddApc(ApcComponent apc)
{ {
if (!apc.Owner.TryGetComponent<BatteryComponent>(out var battery)) if (!apc.Owner.TryGetComponent(out BatteryComponent? battery))
{ {
return; return;
} }
@@ -162,7 +210,7 @@ namespace Content.Server.GameObjects.Components.NodeContainer.NodeGroups
/// It is important that this returns false, so <see cref="PowerProviderComponent"/>s with a <see cref="NullApcNet"/> have no power. /// It is important that this returns false, so <see cref="PowerProviderComponent"/>s with a <see cref="NullApcNet"/> have no power.
/// </summary> /// </summary>
public bool Powered => false; public bool Powered => false;
public GridId? GridId => default;
public void AddApc(ApcComponent apc) { } public void AddApc(ApcComponent apc) { }
public void AddPowerProvider(PowerProviderComponent provider) { } public void AddPowerProvider(PowerProviderComponent provider) { }
public void RemoveApc(ApcComponent apc) { } public void RemoveApc(ApcComponent apc) { }

View File

@@ -36,6 +36,12 @@ namespace Content.Server.GameObjects.Components.NodeContainer.NodeGroups
[ViewVariables] [ViewVariables]
public int NodeCount => Nodes.Count; public int NodeCount => Nodes.Count;
/// <summary>
/// Debug variable to indicate that this NodeGroup should not be being used by anything.
/// </summary>
[ViewVariables]
public bool Removed { get; private set; } = false;
public static readonly INodeGroup NullGroup = new NullNodeGroup(); public static readonly INodeGroup NullGroup = new NullNodeGroup();
protected GridId GridId { get; private set;} protected GridId GridId { get; private set;}
@@ -70,6 +76,7 @@ namespace Content.Server.GameObjects.Components.NodeContainer.NodeGroups
{ {
node.NodeGroup = newGroup; node.NodeGroup = newGroup;
} }
Removed = true;
} }
/// <summary> /// <summary>
@@ -92,6 +99,7 @@ namespace Content.Server.GameObjects.Components.NodeContainer.NodeGroups
} }
} }
AfterRemake(newGroups); AfterRemake(newGroups);
Removed = true;
} }
protected virtual void OnAddNode(Node node) { } protected virtual void OnAddNode(Node node) { }

View File

@@ -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<IApcNet> _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);
}
}
}

View File

@@ -1,6 +1,4 @@
#nullable enable #nullable enable
using System.Collections.Generic;
using Content.Server.GameObjects.Components.NodeContainer.NodeGroups;
using Content.Server.GameObjects.Components.Power.ApcNetComponents; using Content.Server.GameObjects.Components.Power.ApcNetComponents;
using JetBrains.Annotations; using JetBrains.Annotations;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
@@ -12,17 +10,10 @@ namespace Content.Server.GameObjects.EntitySystems
{ {
public override void Update(float frameTime) public override void Update(float frameTime)
{ {
var uniqueApcNets = new HashSet<IApcNet>(); //could be improved by maintaining set instead of getting collection every frame
foreach (var apc in ComponentManager.EntityQuery<ApcComponent>(false)) foreach (var apc in ComponentManager.EntityQuery<ApcComponent>(false))
{ {
uniqueApcNets.Add(apc.Net);
apc.Update(); apc.Update();
} }
foreach (var apcNet in uniqueApcNets)
{
apcNet.Update(frameTime);
}
} }
} }
} }