Skip to content

Commit

Permalink
Debris optimize (Take 2) (#1384)
Browse files Browse the repository at this point in the history
* Attempt to fix

* Update LocalityLoaderSystem.cs

* Update base_debris.yml

* Update base_debris.yml

* Update DebrisFeaturePlacerSystem.cs

* Fix

* Update LocalityLoaderSystem.cs

* Update LocalityLoaderSystem.cs

* Update GCQueuePrototype.cs

* Fix

* Fixed

* Update LocalityLoaderSystem.cs

* Update LocalityLoaderSystem.cs

* Update LocalityLoaderSystem.cs

* Update LocalityLoaderSystem.cs

* Update LocalityLoaderSystem.cs

* Update LocalityLoaderSystem.cs

* Update LocalityLoaderSystem.cs

* Update LocalityLoaderSystem.cs
  • Loading branch information
dvir001 authored May 24, 2024
1 parent b2645ab commit 09d780c
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Linq;
using System.Linq;
using System.Numerics;
using Content.Server.Worldgen.Components;
using Content.Server.Worldgen.Components.Debris;
Expand All @@ -10,6 +10,7 @@
using Robust.Shared.Map.Components;
using Robust.Shared.Random;
using Robust.Shared.Utility;
using Content.Server._NF.Worldgen.Components.Debris; // Frontier

namespace Content.Server.Worldgen.Systems.Debris;

Expand Down Expand Up @@ -227,6 +228,8 @@ private void OnChunkLoaded(EntityUid uid, DebrisFeaturePlacerControllerComponent
var owned = EnsureComp<OwnedDebrisComponent>(ent);
owned.OwningController = uid;
owned.LastKey = point;

EnsureComp<SpaceDebrisComponent>(ent); // Frontier
}

if (failures > 0)
Expand Down
30 changes: 1 addition & 29 deletions Content.Server/Worldgen/Systems/GC/GCQueueSystem.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using System.Linq;
using System.Linq;
using Content.Server.Worldgen.Components.GC;
using Content.Server.Worldgen.Prototypes;
using Content.Shared.CCVar;
using JetBrains.Annotations;
using Robust.Shared.Configuration;
using Robust.Shared.Player;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Shared.Timing;
Expand All @@ -19,7 +18,6 @@ public sealed class GCQueueSystem : EntitySystem
[Dependency] private readonly IConfigurationManager _cfg = default!;
[Dependency] private readonly IPrototypeManager _proto = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly SharedTransformSystem _transform = default!;

[ViewVariables] private TimeSpan _maximumProcessTime = TimeSpan.Zero;

Expand Down Expand Up @@ -72,32 +70,6 @@ public override void Update(float frameTime)
/// <param name="e">Entity to GC.</param>
public void TryGCEntity(EntityUid e)
{
if (!EntityManager.TryGetComponent<TransformComponent>(e, out var transform))
{
Log.Error("Entity was missing transform component");
return;
}

if (transform.GridUid == null)
{
Log.Error("Entity has no associated grid?");
return;
}

foreach (var player in Filter.Empty().AddInGrid(transform.GridUid.Value, EntityManager).Recipients)
{
if (player.AttachedEntity.HasValue)
{
var playerEntityUid = player.AttachedEntity.Value;
if (HasComp<GCAbleObjectComponent>(playerEntityUid))
{
// Mobs are NEVER immune (even if they're from a different grid, they shouldn't be here)
continue;
}
_transform.SetParent(playerEntityUid, transform.ParentUid);
}
}

if (!TryComp<GCAbleObjectComponent>(e, out var comp))
{
QueueDel(e); // not our problem :)
Expand Down
60 changes: 58 additions & 2 deletions Content.Server/Worldgen/Systems/LocalityLoaderSystem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
using Content.Server.Worldgen.Components;
using Content.Server.Worldgen.Components;
using Robust.Server.GameObjects;
using Content.Server._NF.Worldgen.Components.Debris; // Frontier
using Content.Shared.Humanoid; // Frontier
using Content.Shared.Mobs.Components; // Frontier
using System.Numerics; // Frontier
using Robust.Shared.Map; // Frontier
using Content.Server._NF.Salvage; // Frontier

using EntityPosition = (Robust.Shared.GameObjects.EntityUid Entity, Robust.Shared.Map.EntityCoordinates Coordinates); // Frontier

namespace Content.Server.Worldgen.Systems;

Expand All @@ -10,6 +18,18 @@ public sealed class LocalityLoaderSystem : BaseWorldSystem
{
[Dependency] private readonly TransformSystem _xformSys = default!;

// Frontier
private List<(Entity<TransformComponent> Entity, EntityUid MapUid, Vector2 LocalPosition)> _detachEnts = new(); // Frontier
private EntityQuery<SpaceDebrisComponent> _debrisQuery;
private readonly List<(EntityUid Debris, List<EntityPosition> Entity)> _terminatingDebris = [];

public override void Initialize()
{
_debrisQuery = GetEntityQuery<SpaceDebrisComponent>();
SubscribeLocalEvent<SpaceDebrisComponent, EntityTerminatingEvent>(OnDebrisDespawn);
}
// Frontier

/// <inheritdoc />
public override void Update(float frameTime)
{
Expand Down Expand Up @@ -54,10 +74,46 @@ public override void Update(float frameTime)
}
}
}

// Frontier
private void OnDebrisDespawn(EntityUid entity, SpaceDebrisComponent component, EntityTerminatingEvent e)
{
if (entity != null)
{
// Handle mobrestrictions getting deleted
var query = AllEntityQuery<SalvageMobRestrictionsNFComponent>();

while (query.MoveNext(out var salvUid, out var salvMob))
{
if (entity == salvMob.LinkedGridEntity)
{
QueueDel(salvUid);
}
}

var mobQuery = AllEntityQuery<HumanoidAppearanceComponent, MobStateComponent, TransformComponent>();
_detachEnts.Clear();

while (mobQuery.MoveNext(out var mobUid, out _, out _, out var xform))
{
if (xform.GridUid == null || entity != xform.GridUid.Value || xform.MapUid == null)
continue;

// Can't parent directly to map as it runs grid traversal.
_detachEnts.Add(((mobUid, xform), xform.MapUid.Value, _xformSys.GetWorldPosition(xform)));
_xformSys.DetachParentToNull(mobUid, xform);
}

foreach (var detachEnt in _detachEnts)
{
_xformSys.SetCoordinates(detachEnt.Entity.Owner, new EntityCoordinates(detachEnt.MapUid, detachEnt.LocalPosition));
}
}
}
// Frontier
}

/// <summary>
/// A directed fired on a loadable entity when a local loader enters it's vicinity.
/// </summary>
public record struct LocalStructureLoadedEvent;

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Robust.Shared.GameStates;

namespace Content.Server._NF.Worldgen.Components.Debris;

[RegisterComponent, NetworkedComponent]
public sealed partial class SpaceDebrisComponent : Component
{
/// <summary>
/// TODO: Add this so we can track all the debris active entities.
/// </summary>
[DataField]
public List<EntityUid>? ActiveEntities;
}
2 changes: 2 additions & 0 deletions Resources/Prototypes/Entities/World/Debris/base_debris.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
components:
- type: OwnedDebris
- type: LocalityLoader
# - type: TimedDespawn # Frontier
# lifetime: 600 # Frontier
5 changes: 3 additions & 2 deletions Resources/Prototypes/GC/world.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
- type: gcQueue
id: SpaceDebris
depth: 512 # So there's a decent bit of time before roids unload.
minDepthToProcess: 256
depth: 256 # Frontier
minDepthToProcess: 128 # Frontier
trySkipQueue: true # Frontier - Attempt immediate removal if possible

0 comments on commit 09d780c

Please sign in to comment.