Gravity well improvements (#35027)

* 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>
This commit is contained in:
metalgearsloth
2025-02-10 16:18:24 +11:00
committed by GitHub
parent 0bb1dfc582
commit a269850a73
2 changed files with 26 additions and 38 deletions

View 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
}