Optimise spreadersystem (#13329)

* Optimise spreadersystem

It makes entity spawning slower.

* Remove redundant airtightchangedevent

* oop
This commit is contained in:
metalgearsloth
2023-01-11 19:18:26 +11:00
committed by GitHub
parent fdc0c86fcf
commit 8900409956
3 changed files with 31 additions and 22 deletions

View File

@@ -29,8 +29,9 @@ namespace Content.Server.Atmos.EntitySystems
if (airtight.FixAirBlockedDirectionInitialize)
{
var moveEvent = new MoveEvent(airtight.Owner, default, default, Angle.Zero, xform.LocalRotation, xform, false);
OnAirtightRotated(uid, airtight, ref moveEvent);
var moveEvent = new MoveEvent(uid, default, default, Angle.Zero, xform.LocalRotation, xform, false);
if (AirtightRotate(uid, airtight, ref moveEvent))
return;
}
UpdatePosition(airtight);
@@ -46,7 +47,7 @@ namespace Content.Server.Atmos.EntitySystems
if (MetaData(grid.Owner).EntityLifeStage > EntityLifeStage.MapInitialized) return;
}
SetAirblocked(airtight, false, xform);
SetAirblocked(uid, airtight, false, xform);
}
private void OnAirtightPositionChanged(EntityUid uid, AirtightComponent airtight, ref AnchorStateChangedEvent args)
@@ -77,16 +78,23 @@ namespace Content.Server.Atmos.EntitySystems
}
private void OnAirtightRotated(EntityUid uid, AirtightComponent airtight, ref MoveEvent ev)
{
AirtightRotate(uid, airtight, ref ev);
}
private bool AirtightRotate(EntityUid uid, AirtightComponent airtight, ref MoveEvent ev)
{
if (!airtight.RotateAirBlocked || airtight.InitialAirBlockedDirection == (int)AtmosDirection.Invalid)
return;
return false;
airtight.CurrentAirBlockedDirection = (int) Rotate((AtmosDirection)airtight.InitialAirBlockedDirection, ev.NewRotation);
UpdatePosition(airtight, ev.Component);
RaiseLocalEvent(uid, new AirtightChanged(airtight), true);
var airtightEv = new AirtightChanged(uid, airtight);
RaiseLocalEvent(uid, ref airtightEv);
return true;
}
public void SetAirblocked(AirtightComponent airtight, bool airblocked, TransformComponent? xform = null)
public void SetAirblocked(EntityUid uid, AirtightComponent airtight, bool airblocked, TransformComponent? xform = null)
{
if (airtight.AirBlocked == airblocked)
return;
@@ -95,7 +103,8 @@ namespace Content.Server.Atmos.EntitySystems
airtight.AirBlocked = airblocked;
UpdatePosition(airtight, xform);
RaiseLocalEvent(airtight.Owner, new AirtightChanged(airtight), true);
var airtightEv = new AirtightChanged(uid, airtight);
RaiseLocalEvent(uid, ref airtightEv);
}
public void UpdatePosition(AirtightComponent airtight, TransformComponent? xform = null)
@@ -143,13 +152,6 @@ namespace Content.Server.Atmos.EntitySystems
}
}
public sealed class AirtightChanged : EntityEventArgs
{
public AirtightComponent Airtight;
public AirtightChanged(AirtightComponent airtight)
{
Airtight = airtight;
}
}
[ByRefEvent]
public readonly record struct AirtightChanged(EntityUid Entity, AirtightComponent Airtight);
}

View File

@@ -67,7 +67,7 @@ public sealed class DoorSystem : SharedDoorSystem
return;
if (door.ChangeAirtight && TryComp(uid, out AirtightComponent? airtight))
_airtightSystem.SetAirblocked(airtight, collidable);
_airtightSystem.SetAirblocked(uid, airtight, collidable);
// Pathfinding / AI stuff.
RaiseLocalEvent(new AccessReaderChangeEvent(uid, collidable));

View File

@@ -3,6 +3,7 @@ using Content.Server.Atmos.Components;
using Content.Server.Atmos.EntitySystems;
using Content.Shared.Atmos;
using Robust.Shared.Map;
using Robust.Shared.Map.Components;
using Robust.Shared.Random;
namespace Content.Server.Kudzu;
@@ -28,9 +29,9 @@ public sealed class SpreaderSystem : EntitySystem
SubscribeLocalEvent<AirtightChanged>(OnAirtightChanged);
}
private void OnAirtightChanged(AirtightChanged e)
private void OnAirtightChanged(ref AirtightChanged ev)
{
UpdateNearbySpreaders((e.Airtight).Owner, e.Airtight);
UpdateNearbySpreaders(ev.Entity, ev.Airtight);
}
private void SpreaderAddHandler(EntityUid uid, SpreaderComponent component, ComponentAdd args)
@@ -46,15 +47,21 @@ public sealed class SpreaderSystem : EntitySystem
if (!_mapManager.TryGetGrid(transform.GridUid, out var grid)) return;
var spreaderQuery = GetEntityQuery<SpreaderComponent>();
var tile = grid.TileIndicesFor(transform.Coordinates);
for (var i = 0; i < Atmospherics.Directions; i++)
{
var direction = (AtmosDirection) (1 << i);
if (!comp.AirBlockedDirection.IsFlagSet(direction)) continue;
foreach (var ent in grid.GetInDir(transform.Coordinates, direction.ToDirection()))
var directionEnumerator =
grid.GetAnchoredEntitiesEnumerator(SharedMapSystem.GetDirection(tile, direction.ToDirection()));
while (directionEnumerator.MoveNext(out var ent))
{
if (EntityManager.TryGetComponent<SpreaderComponent>(ent, out var s) && s.Enabled)
_edgeGrowths.Add(ent);
if (spreaderQuery.TryGetComponent(ent, out var s) && s.Enabled)
_edgeGrowths.Add(ent.Value);
}
}
}