]> git.smokeofanarchy.ru Git - space-station-14.git/commitdiff
Gravity well improvements (#35027)
authormetalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
Mon, 10 Feb 2025 05:18:24 +0000 (16:18 +1100)
committerGitHub <noreply@github.com>
Mon, 10 Feb 2025 05:18:24 +0000 (06:18 +0100)
* Gravity well improvements

- Fixed persistence
- Removed dummy fields
- Performance

* Update Content.Server/Singularity/Components/GravityWellComponent.cs

---------

Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>
Content.Server/Singularity/Components/GravityWellComponent.cs
Content.Server/Singularity/EntitySystems/GravityWellSystem.cs

index 58a314fa8bb4fe5966daf57851be32f0da725491..fb419d88316a9e19580ea05e78dc177dd8a65636 100644 (file)
@@ -7,22 +7,20 @@ namespace Content.Server.Singularity.Components;
 /// The server-side version of <see cref="SharedGravityWellComponent"/>.
 /// Primarily managed by <see cref="GravityWellSystem"/>.
 /// </summary>
-[RegisterComponent]
+[RegisterComponent, AutoGenerateComponentPause]
 public sealed partial class GravityWellComponent : Component
 {
     /// <summary>
     /// The maximum range at which the gravity well can push/pull entities.
     /// </summary>
-    [DataField("maxRange")]
-    [ViewVariables(VVAccess.ReadWrite)]
+    [DataField]
     public float MaxRange;
 
     /// <summary>
     /// The minimum range at which the gravity well can push/pull entities.
     /// This is effectively hardfloored at <see cref="GravityWellSystem.MinGravPulseRange"/>.
     /// </summary>
-    [DataField("minRange")]
-    [ViewVariables(VVAccess.ReadWrite)]
+    [DataField]
     public float MinRange = 0f;
 
     /// <summary>
@@ -30,8 +28,7 @@ public sealed partial class GravityWellComponent : Component
     /// Negative values accelerate entities away from the gravity well.
     /// Actual acceleration scales with the inverse of the distance to the singularity.
     /// </summary>
-    [DataField("baseRadialAcceleration")]
-    [ViewVariables(VVAccess.ReadWrite)]
+    [DataField]
     public float BaseRadialAcceleration = 0.0f;
 
     /// <summary>
@@ -39,8 +36,7 @@ public sealed partial class GravityWellComponent : Component
     /// Positive tangential acceleration is counter-clockwise.
     /// Actual acceleration scales with the inverse of the distance to the singularity.
     /// </summary>
-    [DataField("baseTangentialAcceleration")]
-    [ViewVariables(VVAccess.ReadWrite)]
+    [DataField]
     public float BaseTangentialAcceleration = 0.0f;
 
     #region Update Timing
@@ -56,16 +52,14 @@ public sealed partial class GravityWellComponent : Component
     /// <summary>
     /// The next time at which this gravity well should pulse.
     /// </summary>
-    [ViewVariables(VVAccess.ReadOnly)]
-    [Access(typeof(GravityWellSystem))]
+    [DataField, Access(typeof(GravityWellSystem)), AutoPausedField]
     public TimeSpan NextPulseTime { get; internal set; } = default!;
 
     /// <summary>
     /// The last time this gravity well pulsed.
     /// </summary>
     [ViewVariables(VVAccess.ReadOnly)]
-    [Access(typeof(GravityWellSystem))]
-    public TimeSpan LastPulseTime { get; internal set; } = default!;
+    public TimeSpan LastPulseTime => NextPulseTime - TargetPulsePeriod;
 
     #endregion Update Timing
 }
index 7dcf3ef1ae5a109d56504c2ca0376d54077dc308..6f2137b0d0a38d9444b3a8e6d5e6c0636bc684f8 100644 (file)
@@ -39,6 +39,8 @@ public sealed class GravityWellSystem : SharedGravityWellSystem
     private EntityQuery<MapGridComponent> _gridQuery;
     private EntityQuery<PhysicsComponent> _physicsQuery;
 
+    private HashSet<EntityUid> _entSet = new();
+
     public override void Initialize()
     {
         base.Initialize();
@@ -46,12 +48,17 @@ public sealed class GravityWellSystem : SharedGravityWellSystem
         _mapQuery = GetEntityQuery<MapComponent>();
         _gridQuery = GetEntityQuery<MapGridComponent>();
         _physicsQuery = GetEntityQuery<PhysicsComponent>();
-        SubscribeLocalEvent<GravityWellComponent, ComponentStartup>(OnGravityWellStartup);
+        SubscribeLocalEvent<GravityWellComponent, MapInitEvent>(OnGravityWellMapInit);
 
         var vvHandle = _vvManager.GetTypeHandler<GravityWellComponent>();
         vvHandle.AddPath(nameof(GravityWellComponent.TargetPulsePeriod), (_, comp) => comp.TargetPulsePeriod, SetPulsePeriod);
     }
 
+    private void OnGravityWellMapInit(Entity<GravityWellComponent> ent, ref MapInitEvent args)
+    {
+        ent.Comp.NextPulseTime = _timing.CurTime + ent.Comp.TargetPulsePeriod;
+    }
+
     public override void Shutdown()
     {
         var vvHandle = _vvManager.GetTypeHandler<GravityWellComponent>();
@@ -73,6 +80,7 @@ public sealed class GravityWellSystem : SharedGravityWellSystem
         while (query.MoveNext(out var uid, out var gravWell, out var xform))
         {
             var curTime = _timing.CurTime;
+
             if (gravWell.NextPulseTime <= curTime)
                 Update(uid, curTime - gravWell.LastPulseTime, gravWell, xform);
         }
@@ -103,8 +111,7 @@ public sealed class GravityWellSystem : SharedGravityWellSystem
         if(!Resolve(uid, ref gravWell))
             return;
 
-        gravWell.LastPulseTime = _timing.CurTime;
-        gravWell.NextPulseTime = gravWell.LastPulseTime + gravWell.TargetPulsePeriod;
+        gravWell.NextPulseTime += gravWell.TargetPulsePeriod;
         if (gravWell.MaxRange < 0.0f || !Resolve(uid, ref xform))
             return;
 
@@ -195,15 +202,18 @@ public sealed class GravityWellSystem : SharedGravityWellSystem
         if (mapPos == MapCoordinates.Nullspace)
             return; // No gravpulses in nullspace please.
 
+        _entSet.Clear();
         var epicenter = mapPos.Position;
         var minRange2 = MathF.Max(minRange * minRange, MinGravPulseRange); // Cache square value for speed. Also apply a sane minimum value to the minimum value so that div/0s don't happen.
-        var bodyQuery = GetEntityQuery<PhysicsComponent>();
-        var xformQuery = GetEntityQuery<TransformComponent>();
+        _lookup.GetEntitiesInRange(mapPos.MapId,
+            epicenter,
+            maxRange,
+            _entSet,
+            flags: LookupFlags.Dynamic | LookupFlags.Sundries);
 
-        foreach(var entity in _lookup.GetEntitiesInRange(mapPos.MapId, epicenter, maxRange, flags: LookupFlags.Dynamic | LookupFlags.Sundries))
+        foreach (var entity in _entSet)
         {
-            if (!bodyQuery.TryGetComponent(entity, out var physics)
-                || physics.BodyType == BodyType.Static)
+            if (!_physicsQuery.TryGetComponent(entity, out var physics))
             {
                 continue;
             }
@@ -214,7 +224,7 @@ public sealed class GravityWellSystem : SharedGravityWellSystem
             if(!CanGravPulseAffect(entity))
                 continue;
 
-            var displacement = epicenter - _transform.GetWorldPosition(entity, xformQuery);
+            var displacement = epicenter - _transform.GetWorldPosition(entity);
             var distance2 = displacement.LengthSquared();
             if (distance2 < minRange2)
                 continue;
@@ -263,20 +273,4 @@ public sealed class GravityWellSystem : SharedGravityWellSystem
     }
 
     #endregion Getters/Setters
-
-    #region Event Handlers
-
-    /// <summary>
-    /// Resets the pulse timings of the gravity well when the components starts up.
-    /// </summary>
-    /// <param name="uid">The uid of the gravity well to start up.</param>
-    /// <param name="comp">The state of the gravity well to start up.</param>
-    /// <param name="args">The startup prompt arguments.</param>
-    public void OnGravityWellStartup(EntityUid uid, GravityWellComponent comp, ComponentStartup args)
-    {
-        comp.LastPulseTime = _timing.CurTime;
-        comp.NextPulseTime = comp.LastPulseTime + comp.TargetPulsePeriod;
-    }
-
-    #endregion Event Handlers
 }