From 72a952fbdda9653f9e7bd8f09db1eaa755e2fea0 Mon Sep 17 00:00:00 2001 From: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Date: Sun, 13 Nov 2022 15:37:01 +1100 Subject: [PATCH] ECS grid methods and map partials (#3441) --- .../Components/Map/MapGridComponent.cs | 53 +----------- .../Systems/SharedMapSystem.Grid.cs | 71 ++++++++++++++++ .../Systems/SharedMapSystem.Map.cs | 51 ++++++++++++ .../GameObjects/Systems/SharedMapSystem.cs | 80 +------------------ Robust.Shared/Map/IMapManagerInternal.cs | 1 - .../Map/MapManager.GridCollection.cs | 14 +--- .../Shared/GameObjects/EntityState_Tests.cs | 2 +- 7 files changed, 131 insertions(+), 141 deletions(-) create mode 100644 Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.cs create mode 100644 Robust.Shared/GameObjects/Systems/SharedMapSystem.Map.cs diff --git a/Robust.Shared/GameObjects/Components/Map/MapGridComponent.cs b/Robust.Shared/GameObjects/Components/Map/MapGridComponent.cs index cf1fa3272..8efef0e9c 100644 --- a/Robust.Shared/GameObjects/Components/Map/MapGridComponent.cs +++ b/Robust.Shared/GameObjects/Components/Map/MapGridComponent.cs @@ -4,7 +4,6 @@ using Robust.Shared.GameStates; using Robust.Shared.IoC; using Robust.Shared.Map; using Robust.Shared.Maths; -using Robust.Shared.Physics; using Robust.Shared.Serialization; using Robust.Shared.Serialization.Manager.Attributes; using Robust.Shared.Utility; @@ -16,8 +15,8 @@ namespace Robust.Shared.GameObjects [NetworkedComponent] public sealed class MapGridComponent : Component { - [Dependency] private readonly IMapManagerInternal _mapManager = default!; [Dependency] private readonly IEntityManager _entMan = default!; + [Dependency] private readonly IMapManagerInternal _mapManager = default!; // This field is used for deserialization internally in the map loader. // If you want to remove this, you would have to restructure the map save file. @@ -29,7 +28,7 @@ namespace Robust.Shared.GameObjects private IMapGrid? _mapGrid; [DataField("chunkSize")] - private ushort _chunkSize = 16; + internal ushort ChunkSize = 16; [ViewVariables] public IMapGrid Grid @@ -38,43 +37,6 @@ namespace Robust.Shared.GameObjects private set => _mapGrid = value; } - protected override void Initialize() - { - base.Initialize(); - var xform = _entMan.GetComponent(Owner); - var mapId = xform.MapID; - - if (_mapManager.HasMapEntity(mapId)) - { - xform.AttachParent(_mapManager.GetMapEntityIdOrThrow(mapId)); - } - } - - protected override void OnRemove() - { - if (_mapGrid != null) - _mapManager.TrueGridDelete((MapGrid)_mapGrid); - - base.OnRemove(); - } - - /// - public override ComponentState GetComponentState() - { - return new MapGridComponentState(Owner, _chunkSize); - } - - /// - public override void HandleComponentState(ComponentState? curState, ComponentState? nextState) - { - base.HandleComponentState(curState, nextState); - - if (curState is not MapGridComponentState state) - return; - - _chunkSize = state.ChunkSize; - } - internal MapGrid AllocMapGrid(ushort chunkSize, ushort tileSize) { DebugTools.Assert(LifeStage == ComponentLifeStage.Added); @@ -142,15 +104,9 @@ namespace Robust.Shared.GameObjects /// /// Serialized state of a . /// -#pragma warning disable CS0618 [Serializable, NetSerializable] internal sealed class MapGridComponentState : ComponentState { - /// - /// Index of the grid this component is linked to. - /// - public EntityUid GridIndex { get; } - /// /// The size of the chunks in the map grid. /// @@ -159,13 +115,10 @@ namespace Robust.Shared.GameObjects /// /// Constructs a new instance of . /// - /// Index of the grid this component is linked to. /// The size of the chunks in the map grid. - public MapGridComponentState(EntityUid gridIndex, ushort chunkSize) + public MapGridComponentState(ushort chunkSize) { - GridIndex = gridIndex; ChunkSize = chunkSize; } } -#pragma warning restore CS0618 } diff --git a/Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.cs b/Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.cs new file mode 100644 index 000000000..aadb6a83e --- /dev/null +++ b/Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.cs @@ -0,0 +1,71 @@ +using Robust.Shared.GameStates; +using Robust.Shared.Map; + +namespace Robust.Shared.GameObjects; + +public abstract partial class SharedMapSystem +{ + private void InitializeGrid() + { + SubscribeLocalEvent(OnGridGetState); + SubscribeLocalEvent(OnGridHandleState); + SubscribeLocalEvent(OnGridAdd); + SubscribeLocalEvent(OnGridInit); + SubscribeLocalEvent(OnGridStartup); + SubscribeLocalEvent(OnGridRemove); + } + + private void OnGridHandleState(EntityUid uid, MapGridComponent component, ref ComponentHandleState args) + { + if (args.Current is not MapGridComponentState state) + return; + + component.ChunkSize = state.ChunkSize; + } + + private void OnGridGetState(EntityUid uid, MapGridComponent component, ref ComponentGetState args) + { + args.State = new MapGridComponentState(component.ChunkSize); + } + + private void OnGridAdd(EntityUid uid, MapGridComponent component, ComponentAdd args) + { + // GridID is not set yet so we don't include it. + var msg = new GridAddEvent(uid); + RaiseLocalEvent(uid, msg, true); + } + + private void OnGridInit(EntityUid uid, MapGridComponent component, ComponentInit args) + { + var xformQuery = GetEntityQuery(); + var xform = xformQuery.GetComponent(uid); + var mapId = xform.MapID; + + if (MapManager.HasMapEntity(mapId)) + { + _transform.SetParent(xform, MapManager.GetMapEntityIdOrThrow(mapId), xformQuery); + } + + var msg = new GridInitializeEvent(uid); + RaiseLocalEvent(uid, msg, true); + } + + private void OnGridStartup(EntityUid uid, MapGridComponent component, ComponentStartup args) + { + var msg = new GridStartupEvent(uid); + RaiseLocalEvent(uid, msg, true); + } + + private void OnGridRemove(EntityUid uid, MapGridComponent component, ComponentShutdown args) + { + RaiseLocalEvent(uid, new GridRemovalEvent(uid), true); + + if (uid == EntityUid.Invalid) + return; + + if (!MapManager.GridExists(uid)) + return; + + MapManager.DeleteGrid(uid); + } +} diff --git a/Robust.Shared/GameObjects/Systems/SharedMapSystem.Map.cs b/Robust.Shared/GameObjects/Systems/SharedMapSystem.Map.cs new file mode 100644 index 000000000..9aa61fa78 --- /dev/null +++ b/Robust.Shared/GameObjects/Systems/SharedMapSystem.Map.cs @@ -0,0 +1,51 @@ +using Robust.Shared.GameStates; +using Robust.Shared.Map; + +namespace Robust.Shared.GameObjects; + +public abstract partial class SharedMapSystem +{ + private void InitializeMap() + { + SubscribeLocalEvent(OnMapAdd); + SubscribeLocalEvent(OnMapInit); + SubscribeLocalEvent(OnMapRemoved); + SubscribeLocalEvent(OnMapHandleState); + SubscribeLocalEvent(OnMapGetState); + } + + private void OnMapHandleState(EntityUid uid, MapComponent component, ref ComponentHandleState args) + { + if (args.Current is not MapComponentState state) + return; + + component.WorldMap = state.MapId; + component.LightingEnabled = state.LightingEnabled; + var xformQuery = GetEntityQuery(); + + xformQuery.GetComponent(uid).ChangeMapId(state.MapId, xformQuery); + } + + private void OnMapGetState(EntityUid uid, MapComponent component, ref ComponentGetState args) + { + args.State = new MapComponentState(component.WorldMap, component.LightingEnabled); + } + + protected abstract void OnMapAdd(EntityUid uid, MapComponent component, ComponentAdd args); + + private void OnMapInit(EntityUid uid, MapComponent component, ComponentInit args) + { + var msg = new MapChangedEvent(component.WorldMap, true); + RaiseLocalEvent(uid, msg, true); + } + + private void OnMapRemoved(EntityUid uid, MapComponent component, ComponentShutdown args) + { + var iMap = (IMapManagerInternal)MapManager; + + iMap.TrueDeleteMap(component.WorldMap); + + var msg = new MapChangedEvent(component.WorldMap, false); + RaiseLocalEvent(uid, msg, true); + } +} diff --git a/Robust.Shared/GameObjects/Systems/SharedMapSystem.cs b/Robust.Shared/GameObjects/Systems/SharedMapSystem.cs index c3712fe96..8096989b7 100644 --- a/Robust.Shared/GameObjects/Systems/SharedMapSystem.cs +++ b/Robust.Shared/GameObjects/Systems/SharedMapSystem.cs @@ -12,92 +12,18 @@ namespace Robust.Shared.GameObjects public abstract partial class SharedMapSystem : EntitySystem { [Dependency] protected readonly IMapManager MapManager = default!; + [Dependency] private readonly SharedTransformSystem _transform = default!; public override void Initialize() { base.Initialize(); - SubscribeLocalEvent(OnMapAdd); - SubscribeLocalEvent(OnMapInit); - SubscribeLocalEvent(OnMapRemoved); - SubscribeLocalEvent(OnMapHandleState); - SubscribeLocalEvent(OnMapGetState); - - SubscribeLocalEvent(OnGridAdd); - SubscribeLocalEvent(OnGridInit); - SubscribeLocalEvent(OnGridStartup); - SubscribeLocalEvent(OnGridRemove); + InitializeMap(); + InitializeGrid(); SubscribeLocalEvent(OnMapLightGetState); SubscribeLocalEvent(OnMapLightHandleState); } - - private void OnMapHandleState(EntityUid uid, MapComponent component, ref ComponentHandleState args) - { - if (args.Current is not MapComponentState state) - return; - - component.WorldMap = state.MapId; - component.LightingEnabled = state.LightingEnabled; - var xformQuery = GetEntityQuery(); - - xformQuery.GetComponent(uid).ChangeMapId(state.MapId, xformQuery); - } - - private void OnMapGetState(EntityUid uid, MapComponent component, ref ComponentGetState args) - { - args.State = new MapComponentState(component.WorldMap, component.LightingEnabled); - } - - protected abstract void OnMapAdd(EntityUid uid, MapComponent component, ComponentAdd args); - - private void OnMapInit(EntityUid uid, MapComponent component, ComponentInit args) - { - var msg = new MapChangedEvent(component.WorldMap, true); - RaiseLocalEvent(uid, msg, true); - } - - private void OnMapRemoved(EntityUid uid, MapComponent component, ComponentShutdown args) - { - var iMap = (IMapManagerInternal)MapManager; - - iMap.TrueDeleteMap(component.WorldMap); - - var msg = new MapChangedEvent(component.WorldMap, false); - RaiseLocalEvent(uid, msg, true); - } - - private void OnGridAdd(EntityUid uid, MapGridComponent component, ComponentAdd args) - { - // GridID is not set yet so we don't include it. - var msg = new GridAddEvent(uid); - RaiseLocalEvent(uid, msg, true); - } - - private void OnGridInit(EntityUid uid, MapGridComponent component, ComponentInit args) - { - var msg = new GridInitializeEvent(uid); - RaiseLocalEvent(uid, msg, true); - } - - private void OnGridStartup(EntityUid uid, MapGridComponent component, ComponentStartup args) - { - var msg = new GridStartupEvent(uid); - RaiseLocalEvent(uid, msg, true); - } - - private void OnGridRemove(EntityUid uid, MapGridComponent component, ComponentShutdown args) - { - RaiseLocalEvent(uid, new GridRemovalEvent(uid), true); - - if (uid == EntityUid.Invalid) - return; - - if (!MapManager.GridExists(uid)) - return; - - MapManager.DeleteGrid(uid); - } } /// diff --git a/Robust.Shared/Map/IMapManagerInternal.cs b/Robust.Shared/Map/IMapManagerInternal.cs index f962fc61a..c1745f0f6 100644 --- a/Robust.Shared/Map/IMapManagerInternal.cs +++ b/Robust.Shared/Map/IMapManagerInternal.cs @@ -44,7 +44,6 @@ namespace Robust.Shared.Map /// The old tile that got replaced. void RaiseOnTileChanged(TileRef tileRef, Tile oldTile); - void TrueGridDelete(MapGrid grid); void TrueDeleteMap(MapId mapId); void OnGridAllocated(MapGridComponent gridComponent, MapGrid mapGrid); void OnGridBoundsChange(EntityUid uid, MapGrid grid); diff --git a/Robust.Shared/Map/MapManager.GridCollection.cs b/Robust.Shared/Map/MapManager.GridCollection.cs index 1496fc7a8..971cce0a1 100644 --- a/Robust.Shared/Map/MapManager.GridCollection.cs +++ b/Robust.Shared/Map/MapManager.GridCollection.cs @@ -151,6 +151,8 @@ internal partial class MapManager public virtual void DeleteGrid(EntityUid euid) { + _grids.Remove(euid); + #if DEBUG DebugTools.Assert(_dbgGuardRunning); #endif @@ -182,18 +184,6 @@ internal partial class MapManager EntityManager.DeleteEntity(entityId); } - public void TrueGridDelete(MapGrid grid) - { - grid.Deleting = true; - var xform = EntityManager.GetComponent(grid.GridEntityId); - - var mapId = xform.MapID; - - _grids.Remove(grid.GridEntityId); - - Logger.DebugS("map", $"Deleted grid {grid.GridEntityId}"); - } - /// public event EventHandler? TileChanged; diff --git a/Robust.UnitTesting/Shared/GameObjects/EntityState_Tests.cs b/Robust.UnitTesting/Shared/GameObjects/EntityState_Tests.cs index de1de065a..a5661a39b 100644 --- a/Robust.UnitTesting/Shared/GameObjects/EntityState_Tests.cs +++ b/Robust.UnitTesting/Shared/GameObjects/EntityState_Tests.cs @@ -62,7 +62,7 @@ namespace Robust.UnitTesting.Shared.GameObjects new EntityUid(512), new [] { - new ComponentChange(0, new MapGridComponentState(new EntityUid(0), 16), default) + new ComponentChange(0, new MapGridComponentState(16), default) }, default); serializer.Serialize(stream, payload);