Faster Power (#596)

uses SnapGridComponent to select nearby wires instead of entity list (only marginally faster if entity queries are using DynamicTree)

TheoreticalLoad double dipping property setter, reduced to one update

minor refactoring

Co-authored-by: Tyler Young <tyler.young@impromptu.ninja>
This commit is contained in:
Tyler Young
2020-02-06 08:50:52 -05:00
committed by GitHub
parent fcccae5df0
commit ef751f68dc
4 changed files with 49 additions and 40 deletions

View File

@@ -4,6 +4,7 @@ using Content.Server.GameObjects.Components.Stack;
using Content.Server.GameObjects.EntitySystems;
using Robust.Server.Interfaces.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Components.Transform;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.GameObjects.Components;
using Robust.Shared.IoC;
@@ -51,20 +52,22 @@ namespace Content.Server.GameObjects.Components.Power
/// </summary>
public void SpreadPowernet()
{
var _emanager = IoCManager.Resolve<IServerEntityManager>();
var position = Owner.GetComponent<ITransformComponent>().WorldPosition;
var wires = _emanager.GetEntitiesInRange(Owner, 1.1f) //arbitrarily low, just scrape things //wip
.Where(x => x.HasComponent<PowerTransferComponent>());
var entMgr = IoCManager.Resolve<IServerEntityManager>();
var sgc = Owner.GetComponent<SnapGridComponent>();
var wires = sgc.GetCardinalNeighborCells()
.SelectMany(x => x.GetLocal()).Distinct()
.Select(x => x.TryGetComponent<PowerTransferComponent>(out var c) ? c : null)
.Where(x => x != null).Distinct()
.ToArray();
//we have no parent so lets find a partner we can join his powernet
if (Parent == null || Regenerating)
{
foreach (var wire in wires)
{
var ptc = wire.GetComponent<PowerTransferComponent>();
if (ptc.CanConnectTo())
if (wire.CanConnectTo())
{
ConnectToPowernet(ptc.Parent);
ConnectToPowernet(wire.Parent);
break;
}
}
@@ -72,15 +75,14 @@ namespace Content.Server.GameObjects.Components.Power
//we couldn't find a partner so none must have spread yet, lets make our own powernet to spread
if (Parent == null || Regenerating)
{
var powernew = new Powernet();
ConnectToPowernet(powernew);
ConnectToPowernet(new Powernet());
}
}
//Find nodes intersecting us and if not already assigned to a powernet assign them to us
var nodes = _emanager.GetEntitiesIntersecting(Owner)
.Where(x => x.HasComponent<PowerNodeComponent>())
.Select(x => x.GetComponent<PowerNodeComponent>());
var nodes = entMgr.GetEntitiesIntersecting(Owner)
.Select(x => x.TryGetComponent(out PowerNodeComponent pnc) ? pnc : null)
.Where(x => x != null);
foreach (var node in nodes)
{
@@ -97,15 +99,14 @@ namespace Content.Server.GameObjects.Components.Power
//spread powernet to nearby wires which haven't got one yet, and tell them to spread as well
foreach (var wire in wires)
{
var ptc = wire.GetComponent<PowerTransferComponent>();
if (ptc.Parent == null || Regenerating)
if (wire.Parent == null || Regenerating)
{
ptc.ConnectToPowernet(Parent);
ptc.SpreadPowernet();
wire.ConnectToPowernet(Parent);
wire.SpreadPowernet();
}
else if (ptc.Parent != Parent && !ptc.Parent.Dirty)
else if (wire.Parent != Parent && !wire.Parent.Dirty)
{
Parent.MergePowernets(ptc.Parent);
Parent.MergePowernets(wire.Parent);
}
}
}