Fix thruster postmapinit (#15623)

This commit is contained in:
metalgearsloth
2023-04-21 20:04:20 +10:00
committed by GitHub
parent e6590f437e
commit 844c56c016
7 changed files with 638 additions and 598 deletions

View File

@@ -100,7 +100,7 @@ namespace Content.Server.Construction.Completions
if (entityManager.TryGetComponent(machine, out MachineComponent? machineComp)) if (entityManager.TryGetComponent(machine, out MachineComponent? machineComp))
{ {
constructionSystem.RefreshParts(machineComp); constructionSystem.RefreshParts(machine, machineComp);
} }
var entChangeEv = new ConstructionChangeEntityEvent(machine, uid); var entChangeEv = new ConstructionChangeEntityEvent(machine, uid);

View File

@@ -5,6 +5,7 @@ using Content.Shared.Construction.Components;
using Content.Shared.Construction.Prototypes; using Content.Shared.Construction.Prototypes;
using Content.Shared.Verbs; using Content.Shared.Verbs;
using Robust.Shared.Containers; using Robust.Shared.Containers;
using Robust.Shared.Map.Components;
using Robust.Shared.Utility; using Robust.Shared.Utility;
namespace Content.Server.Construction; namespace Content.Server.Construction;
@@ -28,8 +29,8 @@ public sealed partial class ConstructionSystem
private void OnMachineMapInit(EntityUid uid, MachineComponent component, MapInitEvent args) private void OnMachineMapInit(EntityUid uid, MachineComponent component, MapInitEvent args)
{ {
CreateBoardAndStockParts(component); CreateBoardAndStockParts(uid, component);
RefreshParts(component); RefreshParts(uid, component);
} }
private void OnMachineExaminableVerb(EntityUid uid, MachineComponent component, GetVerbsEvent<ExamineVerb> args) private void OnMachineExaminableVerb(EntityUid uid, MachineComponent component, GetVerbsEvent<ExamineVerb> args)
@@ -99,21 +100,21 @@ public sealed partial class ConstructionSystem
return output; return output;
} }
public void RefreshParts(MachineComponent component) public void RefreshParts(EntityUid uid, MachineComponent component)
{ {
var parts = GetAllParts(component); var parts = GetAllParts(component);
EntityManager.EventBus.RaiseLocalEvent(component.Owner, new RefreshPartsEvent EntityManager.EventBus.RaiseLocalEvent(uid, new RefreshPartsEvent
{ {
Parts = parts, Parts = parts,
PartRatings = GetPartsRatings(parts), PartRatings = GetPartsRatings(parts),
}, true); }, true);
} }
public void CreateBoardAndStockParts(MachineComponent component) private void CreateBoardAndStockParts(EntityUid uid, MachineComponent component)
{ {
// Entity might not be initialized yet. // Entity might not be initialized yet.
var boardContainer = _container.EnsureContainer<Container>(component.Owner, MachineFrameComponent.BoardContainerName); var boardContainer = _container.EnsureContainer<Container>(uid, MachineFrameComponent.BoardContainerName);
var partContainer = _container.EnsureContainer<Container>(component.Owner, MachineFrameComponent.PartContainerName); var partContainer = _container.EnsureContainer<Container>(uid, MachineFrameComponent.PartContainerName);
if (string.IsNullOrEmpty(component.BoardPrototype)) if (string.IsNullOrEmpty(component.BoardPrototype))
return; return;
@@ -122,11 +123,11 @@ public sealed partial class ConstructionSystem
if (boardContainer.ContainedEntities.Count > 0) if (boardContainer.ContainedEntities.Count > 0)
return; return;
var board = EntityManager.SpawnEntity(component.BoardPrototype, Transform(component.Owner).Coordinates); var board = EntityManager.SpawnEntity(component.BoardPrototype, Transform(uid).Coordinates);
if (!component.BoardContainer.Insert(board)) if (!component.BoardContainer.Insert(board))
{ {
throw new Exception($"Couldn't insert board with prototype {component.BoardPrototype} to machine with prototype {MetaData(component.Owner).EntityPrototype?.ID ?? "N/A"}!"); throw new Exception($"Couldn't insert board with prototype {component.BoardPrototype} to machine with prototype {MetaData(uid).EntityPrototype?.ID ?? "N/A"}!");
} }
if (!TryComp<MachineBoardComponent?>(board, out var machineBoard)) if (!TryComp<MachineBoardComponent?>(board, out var machineBoard))
@@ -134,7 +135,7 @@ public sealed partial class ConstructionSystem
throw new Exception($"Entity with prototype {component.BoardPrototype} doesn't have a {nameof(MachineBoardComponent)}!"); throw new Exception($"Entity with prototype {component.BoardPrototype} doesn't have a {nameof(MachineBoardComponent)}!");
} }
var xform = Transform(component.Owner); var xform = Transform(uid);
foreach (var (part, amount) in machineBoard.Requirements) foreach (var (part, amount) in machineBoard.Requirements)
{ {
var partProto = _prototypeManager.Index<MachinePartPrototype>(part); var partProto = _prototypeManager.Index<MachinePartPrototype>(part);
@@ -149,20 +150,20 @@ public sealed partial class ConstructionSystem
foreach (var (stackType, amount) in machineBoard.MaterialRequirements) foreach (var (stackType, amount) in machineBoard.MaterialRequirements)
{ {
var stack = _stackSystem.Spawn(amount, stackType, Transform(component.Owner).Coordinates); var stack = _stackSystem.Spawn(amount, stackType, Transform(uid).Coordinates);
if (!partContainer.Insert(stack)) if (!partContainer.Insert(stack))
throw new Exception($"Couldn't insert machine material of type {stackType} to machine with prototype {MetaData(component.Owner).EntityPrototype?.ID ?? "N/A"}"); throw new Exception($"Couldn't insert machine material of type {stackType} to machine with prototype {MetaData(uid).EntityPrototype?.ID ?? "N/A"}");
} }
foreach (var (compName, info) in machineBoard.ComponentRequirements) foreach (var (compName, info) in machineBoard.ComponentRequirements)
{ {
for (var i = 0; i < info.Amount; i++) for (var i = 0; i < info.Amount; i++)
{ {
var c = EntityManager.SpawnEntity(info.DefaultPrototype, Transform(component.Owner).Coordinates); var c = EntityManager.SpawnEntity(info.DefaultPrototype, Transform(uid).Coordinates);
if(!partContainer.Insert(c)) if(!partContainer.Insert(c))
throw new Exception($"Couldn't insert machine component part with default prototype '{compName}' to machine with prototype {MetaData(component.Owner).EntityPrototype?.ID ?? "N/A"}"); throw new Exception($"Couldn't insert machine component part with default prototype '{compName}' to machine with prototype {MetaData(uid).EntityPrototype?.ID ?? "N/A"}");
} }
} }
@@ -170,10 +171,10 @@ public sealed partial class ConstructionSystem
{ {
for (var i = 0; i < info.Amount; i++) for (var i = 0; i < info.Amount; i++)
{ {
var c = EntityManager.SpawnEntity(info.DefaultPrototype, Transform(component.Owner).Coordinates); var c = EntityManager.SpawnEntity(info.DefaultPrototype, Transform(uid).Coordinates);
if(!partContainer.Insert(c)) if(!partContainer.Insert(c))
throw new Exception($"Couldn't insert machine component part with default prototype '{tagName}' to machine with prototype {MetaData(component.Owner).EntityPrototype?.ID ?? "N/A"}"); throw new Exception($"Couldn't insert machine component part with default prototype '{tagName}' to machine with prototype {MetaData(uid).EntityPrototype?.ID ?? "N/A"}");
} }
} }
} }

View File

@@ -83,7 +83,7 @@ public sealed class PartExchangerSystem : EntitySystem
storage.Storage.Insert(unused.Owner); storage.Storage.Insert(unused.Owner);
_storage.Insert(uid, unused.Owner, null, false); _storage.Insert(uid, unused.Owner, null, false);
} }
_construction.RefreshParts(machine); _construction.RefreshParts(args.Args.Target.Value, machine);
args.Handled = true; args.Handled = true;
} }

View File

@@ -15,12 +15,12 @@ namespace Content.Server.Shuttles.Components
/// <summary> /// <summary>
/// The thrusters contributing to each direction for impulse. /// The thrusters contributing to each direction for impulse.
/// </summary> /// </summary>
public readonly List<ThrusterComponent>[] LinearThrusters = new List<ThrusterComponent>[4]; public readonly List<EntityUid>[] LinearThrusters = new List<EntityUid>[4];
/// <summary> /// <summary>
/// The thrusters contributing to the angular impulse of the shuttle. /// The thrusters contributing to the angular impulse of the shuttle.
/// </summary> /// </summary>
public readonly List<ThrusterComponent> AngularThrusters = new(); public readonly List<EntityUid> AngularThrusters = new();
[ViewVariables] [ViewVariables]
public float AngularThrust = 0f; public float AngularThrust = 0f;

View File

@@ -1,6 +1,7 @@
using Content.Server.Shuttles.Systems; using Content.Server.Shuttles.Systems;
using Content.Shared.Construction.Prototypes; using Content.Shared.Construction.Prototypes;
using Content.Shared.Damage; using Content.Shared.Damage;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype; using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
namespace Content.Server.Shuttles.Components namespace Content.Server.Shuttles.Components
@@ -42,7 +43,8 @@ namespace Content.Server.Shuttles.Components
/// </summary> /// </summary>
public bool IsOn; public bool IsOn;
[ViewVariables(VVAccess.ReadWrite)] // Need to serialize this because RefreshParts isn't called on Init and this will break post-mapinit maps!
[ViewVariables(VVAccess.ReadWrite), DataField("thrust")]
public float Thrust; public float Thrust;
[DataField("baseThrust"), ViewVariables(VVAccess.ReadWrite)] [DataField("baseThrust"), ViewVariables(VVAccess.ReadWrite)]
@@ -73,6 +75,12 @@ namespace Content.Server.Shuttles.Components
public bool Firing = false; public bool Firing = false;
/// <summary>
/// Next time we tick damage for anyone colliding.
/// </summary>
[ViewVariables(VVAccess.ReadWrite), DataField("nextFire", customTypeSerializer:typeof(TimeOffsetSerializer))]
public TimeSpan NextFire;
[DataField("machinePartThrust", customTypeSerializer: typeof(PrototypeIdSerializer<MachinePartPrototype>))] [DataField("machinePartThrust", customTypeSerializer: typeof(PrototypeIdSerializer<MachinePartPrototype>))]
public string MachinePartThrust = "Laser"; public string MachinePartThrust = "Laser";

View File

@@ -12,8 +12,8 @@ using Robust.Shared.Physics.Components;
using Robust.Shared.Physics.Systems; using Robust.Shared.Physics.Systems;
using Robust.Shared.Random; using Robust.Shared.Random;
namespace Content.Server.Shuttles.Systems namespace Content.Server.Shuttles.Systems;
{
[UsedImplicitly] [UsedImplicitly]
public sealed partial class ShuttleSystem : SharedShuttleSystem public sealed partial class ShuttleSystem : SharedShuttleSystem
{ {
@@ -75,7 +75,7 @@ namespace Content.Server.Shuttles.Systems
// Easier than doing it in the comp and they don't have constructors. // Easier than doing it in the comp and they don't have constructors.
for (var i = 0; i < component.LinearThrusters.Length; i++) for (var i = 0; i < component.LinearThrusters.Length; i++)
{ {
component.LinearThrusters[i] = new List<ThrusterComponent>(); component.LinearThrusters[i] = new List<EntityUid>();
} }
} }
@@ -163,7 +163,8 @@ namespace Content.Server.Shuttles.Systems
private void OnShuttleShutdown(EntityUid uid, ShuttleComponent component, ComponentShutdown args) private void OnShuttleShutdown(EntityUid uid, ShuttleComponent component, ComponentShutdown args)
{ {
// None of the below is necessary for any cleanup if we're just deleting. // None of the below is necessary for any cleanup if we're just deleting.
if (EntityManager.GetComponent<MetaDataComponent>(uid).EntityLifeStage >= EntityLifeStage.Terminating) return; if (EntityManager.GetComponent<MetaDataComponent>(uid).EntityLifeStage >= EntityLifeStage.Terminating)
return;
if (!EntityManager.TryGetComponent(uid, out PhysicsComponent? physicsComponent)) if (!EntityManager.TryGetComponent(uid, out PhysicsComponent? physicsComponent))
{ {
@@ -173,4 +174,3 @@ namespace Content.Server.Shuttles.Systems
Disable(uid, physicsComponent); Disable(uid, physicsComponent);
} }
} }
}

View File

@@ -17,12 +17,14 @@ using Robust.Shared.Physics.Collision.Shapes;
using Robust.Shared.Physics.Components; using Robust.Shared.Physics.Components;
using Robust.Shared.Physics.Events; using Robust.Shared.Physics.Events;
using Robust.Shared.Physics.Systems; using Robust.Shared.Physics.Systems;
using Robust.Shared.Timing;
using Robust.Shared.Utility; using Robust.Shared.Utility;
namespace Content.Server.Shuttles.Systems namespace Content.Server.Shuttles.Systems;
{
public sealed class ThrusterSystem : EntitySystem public sealed class ThrusterSystem : EntitySystem
{ {
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly IMapManager _mapManager = default!; [Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private readonly ITileDefinitionManager _tileDefManager = default!; [Dependency] private readonly ITileDefinitionManager _tileDefManager = default!;
[Dependency] private readonly AmbientSoundSystem _ambient = default!; [Dependency] private readonly AmbientSoundSystem _ambient = default!;
@@ -35,18 +37,13 @@ namespace Content.Server.Shuttles.Systems
public const string BurnFixture = "thruster-burn"; public const string BurnFixture = "thruster-burn";
private readonly HashSet<ThrusterComponent> _activeThrusters = new();
// Used for accumulating burn if someone touches a firing thruster.
private float _accumulator;
public override void Initialize() public override void Initialize()
{ {
base.Initialize(); base.Initialize();
SubscribeLocalEvent<ThrusterComponent, ActivateInWorldEvent>(OnActivateThruster); SubscribeLocalEvent<ThrusterComponent, ActivateInWorldEvent>(OnActivateThruster);
SubscribeLocalEvent<ThrusterComponent, ComponentInit>(OnThrusterInit); SubscribeLocalEvent<ThrusterComponent, ComponentInit>(OnThrusterInit);
SubscribeLocalEvent<ThrusterComponent, ComponentShutdown>(OnThrusterShutdown); SubscribeLocalEvent<ThrusterComponent, ComponentShutdown>(OnThrusterShutdown);
SubscribeLocalEvent<ThrusterComponent, MapInitEvent>(OnThrusterMapInit);
SubscribeLocalEvent<ThrusterComponent, PowerChangedEvent>(OnPowerChange); SubscribeLocalEvent<ThrusterComponent, PowerChangedEvent>(OnPowerChange);
SubscribeLocalEvent<ThrusterComponent, AnchorStateChangedEvent>(OnAnchorChange); SubscribeLocalEvent<ThrusterComponent, AnchorStateChangedEvent>(OnAnchorChange);
SubscribeLocalEvent<ThrusterComponent, ReAnchorEvent>(OnThrusterReAnchor); SubscribeLocalEvent<ThrusterComponent, ReAnchorEvent>(OnThrusterReAnchor);
@@ -95,7 +92,8 @@ namespace Content.Server.Shuttles.Systems
private void OnShuttleTileChange(EntityUid uid, ShuttleComponent component, ref TileChangedEvent args) private void OnShuttleTileChange(EntityUid uid, ShuttleComponent component, ref TileChangedEvent args)
{ {
// If the old tile was space but the new one isn't then disable all adjacent thrusters // If the old tile was space but the new one isn't then disable all adjacent thrusters
if (args.NewTile.IsSpace(_tileDefManager) || !args.OldTile.IsSpace(_tileDefManager)) return; if (args.NewTile.IsSpace(_tileDefManager) || !args.OldTile.IsSpace(_tileDefManager))
return;
var tilePos = args.NewTile.GridIndices; var tilePos = args.NewTile.GridIndices;
var grid = _mapManager.GetGrid(uid); var grid = _mapManager.GetGrid(uid);
@@ -106,20 +104,23 @@ namespace Content.Server.Shuttles.Systems
{ {
for (var y = -1; y <= 1; y++) for (var y = -1; y <= 1; y++)
{ {
if (x != 0 && y != 0) continue; if (x != 0 && y != 0)
continue;
var checkPos = tilePos + new Vector2i(x, y); var checkPos = tilePos + new Vector2i(x, y);
var enumerator = grid.GetAnchoredEntitiesEnumerator(checkPos); var enumerator = grid.GetAnchoredEntitiesEnumerator(checkPos);
while (enumerator.MoveNext(out var ent)) while (enumerator.MoveNext(out var ent))
{ {
if (!thrusterQuery.TryGetComponent(ent.Value, out var thruster) || !thruster.RequireSpace) continue; if (!thrusterQuery.TryGetComponent(ent.Value, out var thruster) || !thruster.RequireSpace)
continue;
// Work out if the thruster is facing this direction // Work out if the thruster is facing this direction
var xform = xformQuery.GetComponent(ent.Value); var xform = xformQuery.GetComponent(ent.Value);
var direction = xform.LocalRotation.ToWorldVec(); var direction = xform.LocalRotation.ToWorldVec();
if (new Vector2i((int) direction.X, (int) direction.Y) != new Vector2i(x, y)) continue; if (new Vector2i((int) direction.X, (int) direction.Y) != new Vector2i(x, y))
continue;
DisableThruster(ent.Value, thruster, xform.GridUid); DisableThruster(ent.Value, thruster, xform.GridUid);
} }
@@ -142,8 +143,7 @@ namespace Content.Server.Shuttles.Systems
if (!component.Enabled || if (!component.Enabled ||
component.Type != ThrusterType.Linear || component.Type != ThrusterType.Linear ||
!EntityManager.TryGetComponent(uid, out TransformComponent? xform) || !EntityManager.TryGetComponent(uid, out TransformComponent? xform) ||
!_mapManager.TryGetGrid(xform.GridUid, out var grid) || !EntityManager.TryGetComponent(xform.GridUid, out ShuttleComponent? shuttleComponent))
!EntityManager.TryGetComponent(grid.Owner, out ShuttleComponent? shuttleComponent))
{ {
return; return;
} }
@@ -151,7 +151,8 @@ namespace Content.Server.Shuttles.Systems
var canEnable = CanEnable(uid, component); var canEnable = CanEnable(uid, component);
// If it's not on then don't enable it inadvertantly (given we don't have an old rotation) // If it's not on then don't enable it inadvertantly (given we don't have an old rotation)
if (!canEnable && !component.IsOn) return; if (!canEnable && !component.IsOn)
return;
// Enable it if it was turned off but new tile is valid // Enable it if it was turned off but new tile is valid
if (!component.IsOn && canEnable) if (!component.IsOn && canEnable)
@@ -171,12 +172,12 @@ namespace Content.Server.Shuttles.Systems
var direction = (int) args.NewRotation.GetCardinalDir() / 2; var direction = (int) args.NewRotation.GetCardinalDir() / 2;
shuttleComponent.LinearThrust[oldDirection] -= component.Thrust; shuttleComponent.LinearThrust[oldDirection] -= component.Thrust;
DebugTools.Assert(shuttleComponent.LinearThrusters[oldDirection].Contains(component)); DebugTools.Assert(shuttleComponent.LinearThrusters[oldDirection].Contains(uid));
shuttleComponent.LinearThrusters[oldDirection].Remove(component); shuttleComponent.LinearThrusters[oldDirection].Remove(uid);
shuttleComponent.LinearThrust[direction] += component.Thrust; shuttleComponent.LinearThrust[direction] += component.Thrust;
DebugTools.Assert(!shuttleComponent.LinearThrusters[direction].Contains(component)); DebugTools.Assert(!shuttleComponent.LinearThrusters[direction].Contains(uid));
shuttleComponent.LinearThrusters[direction].Add(component); shuttleComponent.LinearThrusters[direction].Add(uid);
} }
private void OnAnchorChange(EntityUid uid, ThrusterComponent component, ref AnchorStateChangedEvent args) private void OnAnchorChange(EntityUid uid, ThrusterComponent component, ref AnchorStateChangedEvent args)
@@ -199,6 +200,12 @@ namespace Content.Server.Shuttles.Systems
EnableThruster(uid, component); EnableThruster(uid, component);
} }
private void OnThrusterMapInit(EntityUid uid, ThrusterComponent component, MapInitEvent args)
{
if (component.NextFire < _timing.CurTime)
component.NextFire = _timing.CurTime;
}
private void OnThrusterInit(EntityUid uid, ThrusterComponent component, ComponentInit args) private void OnThrusterInit(EntityUid uid, ThrusterComponent component, ComponentInit args)
{ {
_ambient.SetAmbience(uid, false); _ambient.SetAmbience(uid, false);
@@ -237,12 +244,15 @@ namespace Content.Server.Shuttles.Systems
public void EnableThruster(EntityUid uid, ThrusterComponent component, TransformComponent? xform = null) public void EnableThruster(EntityUid uid, ThrusterComponent component, TransformComponent? xform = null)
{ {
if (component.IsOn || if (component.IsOn ||
!Resolve(uid, ref xform) || !Resolve(uid, ref xform))
!_mapManager.TryGetGrid(xform.GridUid, out var grid)) return; {
return;
}
component.IsOn = true; component.IsOn = true;
if (!EntityManager.TryGetComponent(grid.Owner, out ShuttleComponent? shuttleComponent)) return; if (!EntityManager.TryGetComponent(xform.GridUid, out ShuttleComponent? shuttleComponent))
return;
// Logger.DebugS("thruster", $"Enabled thruster {uid}"); // Logger.DebugS("thruster", $"Enabled thruster {uid}");
@@ -252,8 +262,8 @@ namespace Content.Server.Shuttles.Systems
var direction = (int) xform.LocalRotation.GetCardinalDir() / 2; var direction = (int) xform.LocalRotation.GetCardinalDir() / 2;
shuttleComponent.LinearThrust[direction] += component.Thrust; shuttleComponent.LinearThrust[direction] += component.Thrust;
DebugTools.Assert(!shuttleComponent.LinearThrusters[direction].Contains(component)); DebugTools.Assert(!shuttleComponent.LinearThrusters[direction].Contains(uid));
shuttleComponent.LinearThrusters[direction].Add(component); shuttleComponent.LinearThrusters[direction].Add(uid);
// Don't just add / remove the fixture whenever the thruster fires because perf // Don't just add / remove the fixture whenever the thruster fires because perf
if (EntityManager.TryGetComponent(uid, out PhysicsComponent? physicsComponent) && if (EntityManager.TryGetComponent(uid, out PhysicsComponent? physicsComponent) &&
@@ -261,14 +271,14 @@ namespace Content.Server.Shuttles.Systems
{ {
var shape = new PolygonShape(); var shape = new PolygonShape();
shape.SetVertices(component.BurnPoly); shape.SetVertices(component.BurnPoly);
_fixtureSystem.TryCreateFixture(uid, shape, BurnFixture, hard: false, collisionLayer: (int) CollisionGroup.FullTileMask); _fixtureSystem.TryCreateFixture(uid, shape, BurnFixture, hard: false, collisionLayer: (int) CollisionGroup.FullTileMask, body: physicsComponent);
} }
break; break;
case ThrusterType.Angular: case ThrusterType.Angular:
shuttleComponent.AngularThrust += component.Thrust; shuttleComponent.AngularThrust += component.Thrust;
DebugTools.Assert(!shuttleComponent.AngularThrusters.Contains(component)); DebugTools.Assert(!shuttleComponent.AngularThrusters.Contains(uid));
shuttleComponent.AngularThrusters.Add(component); shuttleComponent.AngularThrusters.Add(uid);
break; break;
default: default:
throw new ArgumentOutOfRangeException(); throw new ArgumentOutOfRangeException();
@@ -299,12 +309,15 @@ namespace Content.Server.Shuttles.Systems
public void DisableThruster(EntityUid uid, ThrusterComponent component, EntityUid? gridId, TransformComponent? xform = null, Angle? angle = null) public void DisableThruster(EntityUid uid, ThrusterComponent component, EntityUid? gridId, TransformComponent? xform = null, Angle? angle = null)
{ {
if (!component.IsOn || if (!component.IsOn ||
!Resolve(uid, ref xform) || !Resolve(uid, ref xform))
!_mapManager.TryGetGrid(gridId, out var grid)) return; {
return;
}
component.IsOn = false; component.IsOn = false;
if (!EntityManager.TryGetComponent(grid.Owner, out ShuttleComponent? shuttleComponent)) return; if (!EntityManager.TryGetComponent(gridId, out ShuttleComponent? shuttleComponent))
return;
// Logger.DebugS("thruster", $"Disabled thruster {uid}"); // Logger.DebugS("thruster", $"Disabled thruster {uid}");
@@ -315,13 +328,13 @@ namespace Content.Server.Shuttles.Systems
var direction = (int) angle.Value.GetCardinalDir() / 2; var direction = (int) angle.Value.GetCardinalDir() / 2;
shuttleComponent.LinearThrust[direction] -= component.Thrust; shuttleComponent.LinearThrust[direction] -= component.Thrust;
DebugTools.Assert(shuttleComponent.LinearThrusters[direction].Contains(component)); DebugTools.Assert(shuttleComponent.LinearThrusters[direction].Contains(uid));
shuttleComponent.LinearThrusters[direction].Remove(component); shuttleComponent.LinearThrusters[direction].Remove(uid);
break; break;
case ThrusterType.Angular: case ThrusterType.Angular:
shuttleComponent.AngularThrust -= component.Thrust; shuttleComponent.AngularThrust -= component.Thrust;
DebugTools.Assert(shuttleComponent.AngularThrusters.Contains(component)); DebugTools.Assert(shuttleComponent.AngularThrusters.Contains(uid));
shuttleComponent.AngularThrusters.Remove(component); shuttleComponent.AngularThrusters.Remove(uid);
break; break;
default: default:
throw new ArgumentOutOfRangeException(); throw new ArgumentOutOfRangeException();
@@ -344,14 +357,16 @@ namespace Content.Server.Shuttles.Systems
_fixtureSystem.DestroyFixture(uid, BurnFixture, body: physicsComponent); _fixtureSystem.DestroyFixture(uid, BurnFixture, body: physicsComponent);
} }
_activeThrusters.Remove(component);
component.Colliding.Clear(); component.Colliding.Clear();
} }
public bool CanEnable(EntityUid uid, ThrusterComponent component) public bool CanEnable(EntityUid uid, ThrusterComponent component)
{ {
if (!component.Enabled) return false; if (!component.Enabled)
if (component.LifeStage > ComponentLifeStage.Running) return false; return false;
if (component.LifeStage > ComponentLifeStage.Running)
return false;
var xform = Transform(uid); var xform = Transform(uid);
@@ -383,19 +398,15 @@ namespace Content.Server.Shuttles.Systems
{ {
base.Update(frameTime); base.Update(frameTime);
_accumulator += frameTime; var query = EntityQueryEnumerator<ThrusterComponent>();
var curTime = _timing.CurTime;
if (_accumulator < 1) return; while (query.MoveNext(out var comp))
_accumulator -= 1;
foreach (var comp in _activeThrusters.ToArray())
{ {
MetaDataComponent? metaData = null; if (!comp.Firing || comp.Colliding.Count == 0 || comp.Damage == null || comp.NextFire < curTime)
continue;
if (!comp.Firing || comp.Damage == null || Paused(comp.Owner, metaData) || Deleted(comp.Owner, metaData)) continue; comp.NextFire += TimeSpan.FromSeconds(1);
DebugTools.Assert(comp.Colliding.Count > 0);
foreach (var uid in comp.Colliding.ToArray()) foreach (var uid in comp.Colliding.ToArray())
{ {
@@ -406,22 +417,18 @@ namespace Content.Server.Shuttles.Systems
private void OnStartCollide(EntityUid uid, ThrusterComponent component, ref StartCollideEvent args) private void OnStartCollide(EntityUid uid, ThrusterComponent component, ref StartCollideEvent args)
{ {
if (args.OurFixture.ID != BurnFixture) return; if (args.OurFixture.ID != BurnFixture)
return;
_activeThrusters.Add(component); component.Colliding.Add(args.OtherEntity);
component.Colliding.Add((args.OtherFixture.Body).Owner);
} }
private void OnEndCollide(EntityUid uid, ThrusterComponent component, ref EndCollideEvent args) private void OnEndCollide(EntityUid uid, ThrusterComponent component, ref EndCollideEvent args)
{ {
if (args.OurFixture.ID != BurnFixture) return; if (args.OurFixture.ID != BurnFixture)
return;
component.Colliding.Remove((args.OtherFixture.Body).Owner); component.Colliding.Remove(args.OtherEntity);
if (component.Colliding.Count == 0)
{
_activeThrusters.Remove(component);
}
} }
/// <summary> /// <summary>
@@ -429,16 +436,23 @@ namespace Content.Server.Shuttles.Systems
/// </summary> /// </summary>
public void EnableLinearThrustDirection(ShuttleComponent component, DirectionFlag direction) public void EnableLinearThrustDirection(ShuttleComponent component, DirectionFlag direction)
{ {
if ((component.ThrustDirections & direction) != 0x0) return; if ((component.ThrustDirections & direction) != 0x0)
return;
component.ThrustDirections |= direction; component.ThrustDirections |= direction;
var index = GetFlagIndex(direction); var index = GetFlagIndex(direction);
var appearanceQuery = GetEntityQuery<AppearanceComponent>();
var thrusterQuery = GetEntityQuery<ThrusterComponent>();
foreach (var comp in component.LinearThrusters[index]) foreach (var uid in component.LinearThrusters[index])
{ {
if (!thrusterQuery.TryGetComponent(uid, out var comp))
continue;
comp.Firing = true; comp.Firing = true;
_appearance.SetData(comp.Owner, ThrusterVisualState.Thrusting, true); appearanceQuery.TryGetComponent(uid, out var appearance);
_appearance.SetData(uid, ThrusterVisualState.Thrusting, true, appearance);
} }
} }
@@ -447,16 +461,23 @@ namespace Content.Server.Shuttles.Systems
/// </summary> /// </summary>
public void DisableLinearThrustDirection(ShuttleComponent component, DirectionFlag direction) public void DisableLinearThrustDirection(ShuttleComponent component, DirectionFlag direction)
{ {
if ((component.ThrustDirections & direction) == 0x0) return; if ((component.ThrustDirections & direction) == 0x0)
return;
component.ThrustDirections &= ~direction; component.ThrustDirections &= ~direction;
var index = GetFlagIndex(direction); var index = GetFlagIndex(direction);
var appearanceQuery = GetEntityQuery<AppearanceComponent>();
var thrusterQuery = GetEntityQuery<ThrusterComponent>();
foreach (var comp in component.LinearThrusters[index]) foreach (var uid in component.LinearThrusters[index])
{ {
if (!thrusterQuery.TryGetComponent(uid, out var comp))
continue;
appearanceQuery.TryGetComponent(uid, out var appearance);
comp.Firing = false; comp.Firing = false;
_appearance.SetData(comp.Owner, ThrusterVisualState.Thrusting, false); _appearance.SetData(uid, ThrusterVisualState.Thrusting, false, appearance);
} }
} }
@@ -472,20 +493,31 @@ namespace Content.Server.Shuttles.Systems
public void SetAngularThrust(ShuttleComponent component, bool on) public void SetAngularThrust(ShuttleComponent component, bool on)
{ {
var appearanceQuery = GetEntityQuery<AppearanceComponent>();
var thrusterQuery = GetEntityQuery<ThrusterComponent>();
if (on) if (on)
{ {
foreach (var comp in component.AngularThrusters) foreach (var uid in component.AngularThrusters)
{ {
if (!thrusterQuery.TryGetComponent(uid, out var comp))
continue;
appearanceQuery.TryGetComponent(uid, out var appearance);
comp.Firing = true; comp.Firing = true;
_appearance.SetData(comp.Owner, ThrusterVisualState.Thrusting, true); _appearance.SetData(uid, ThrusterVisualState.Thrusting, true, appearance);
} }
} }
else else
{ {
foreach (var comp in component.AngularThrusters) foreach (var uid in component.AngularThrusters)
{ {
if (!thrusterQuery.TryGetComponent(uid, out var comp))
continue;
appearanceQuery.TryGetComponent(uid, out var appearance);
comp.Firing = false; comp.Firing = false;
_appearance.SetData(comp.Owner, ThrusterVisualState.Thrusting, false); _appearance.SetData(uid, ThrusterVisualState.Thrusting, false, appearance);
} }
} }
} }
@@ -509,4 +541,3 @@ namespace Content.Server.Shuttles.Systems
return (int) Math.Log2((int) flag); return (int) Math.Log2((int) flag);
} }
} }
}