From cc08eb883ba095be10faec3a503731139673677a Mon Sep 17 00:00:00 2001 From: TemporalOroboros Date: Tue, 19 May 2026 00:44:03 -0700 Subject: [PATCH 01/13] Move MapManager queries to SharedMapSystem Moves all variants of FindGridsIntersecting/TryFindGridAt to SharedMapSystem Hollows out the MapManager methods and converts them into relays to SharedMapSystem --- .../Systems/SharedMapSystem.Lookup.cs | 452 ++++++++++++++++++ .../GameObjects/Systems/SharedMapSystem.cs | 3 + Robust.Shared/Map/IMapManager.cs | 21 + Robust.Shared/Map/MapManager.Queries.cs | 262 ++-------- 4 files changed, 522 insertions(+), 216 deletions(-) create mode 100644 Robust.Shared/GameObjects/Systems/SharedMapSystem.Lookup.cs diff --git a/Robust.Shared/GameObjects/Systems/SharedMapSystem.Lookup.cs b/Robust.Shared/GameObjects/Systems/SharedMapSystem.Lookup.cs new file mode 100644 index 00000000000..00af546bdfd --- /dev/null +++ b/Robust.Shared/GameObjects/Systems/SharedMapSystem.Lookup.cs @@ -0,0 +1,452 @@ +using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; +using System.Numerics; +using Robust.Shared.Map; +using Robust.Shared.Map.Components; +using Robust.Shared.Map.Enumerators; +using Robust.Shared.Maths; +using Robust.Shared.Physics; +using Robust.Shared.Physics.Collision.Shapes; +using Robust.Shared.Physics.Shapes; +using Transform = Robust.Shared.Physics.Transform; + +namespace Robust.Shared.GameObjects; + +public abstract partial class SharedMapSystem +{ + #region TryFindGridAt + + public bool TryFindGridAt(EntityUid mapEnt, Vector2 worldPos, out EntityUid uid, [NotNullWhen(true)] out MapGridComponent? grid) + { + var rangeVec = new Vector2(0.2f, 0.2f); + + // Need to enlarge the AABB by at least the grid shrinkage size. + var aabb = new Box2(worldPos - rangeVec, worldPos + rangeVec); + + uid = EntityUid.Invalid; + grid = null; + var state = (uid, grid, worldPos, this, _transform); + + FindGridsIntersecting(mapEnt, aabb, ref state, static (EntityUid iUid, MapGridComponent iGrid, ref ( + EntityUid uid, + MapGridComponent? grid, + Vector2 worldPos, + SharedMapSystem mapSystem, + SharedTransformSystem xformSystem) tuple) => + { + // Turn the worldPos into a localPos and work out the relevant chunk we need to check + // This is much faster than iterating over every chunk individually. + // (though now we need some extra calcs up front). + + // Doesn't use WorldBounds because it's just an AABB. + var matrix = tuple.xformSystem.GetInvWorldMatrix(iUid); + var localPos = Vector2.Transform(tuple.worldPos, matrix); + + // NOTE: + // If you change this to use fixtures instead (i.e. if you want half-tiles) then you need to make sure + // you account for the fact that fixtures are shrunk slightly! + var chunkIndices = SharedMapSystem.GetChunkIndices(localPos, iGrid.ChunkSize); + + if (!iGrid.Chunks.TryGetValue(chunkIndices, out var chunk)) + return true; + + var chunkRelative = SharedMapSystem.GetChunkRelative(localPos, iGrid.ChunkSize); + var chunkTile = chunk.GetTile(chunkRelative); + + if (chunkTile.IsEmpty) + return true; + + tuple.uid = iUid; + tuple.grid = iGrid; + return false; + }, approx: true, includeMap: false); + + if (state.grid == null && _gridQuery.TryGetComponent(mapEnt, out var mapGrid)) + { + uid = mapEnt; + grid = mapGrid; + return true; + } + + uid = state.uid; + grid = state.grid; + return grid != null; + } + + public bool TryFindGridAt(MapId mapId, Vector2 worldPos, out EntityUid uid, [NotNullWhen(true)] out MapGridComponent? grid) + { + if (TryGetMap(mapId, out var map)) + return TryFindGridAt(map.Value, worldPos, out uid, out grid); + + uid = default; + grid = null; + return false; + } + + public bool TryFindGridAt(MapCoordinates mapCoordinates, out EntityUid uid, [NotNullWhen(true)] out MapGridComponent? grid) + { + return TryFindGridAt(mapCoordinates.MapId, mapCoordinates.Position, out uid, out grid); + } + + #endregion + + #region MapId + + public void FindGridsIntersecting( + MapId mapId, + T shape, + Transform transform, + ref List> grids, + bool approx = IMapManager.Approximate, + bool includeMap = IMapManager.IncludeMap) where T : IPhysShape + { + if (TryGetMap(mapId, out var mapEnt)) + FindGridsIntersecting(mapEnt.Value, shape, transform, ref grids, approx: approx, includeMap: includeMap); + } + + public void FindGridsIntersecting( + MapId mapId, + T shape, + Transform transform, + GridCallback callback, + bool approx = IMapManager.Approximate, + bool includeMap = IMapManager.IncludeMap) where T : IPhysShape + { + if (TryGetMap(mapId, out var mapEnt)) + FindGridsIntersecting(mapEnt.Value, shape, transform, callback, approx: approx, includeMap: includeMap); + } + + public void FindGridsIntersecting( + MapId mapId, + Box2 worldAABB, + GridCallback callback, + bool approx = IMapManager.Approximate, + bool includeMap = IMapManager.IncludeMap) + { + if (TryGetMap(mapId, out var mapEnt)) + FindGridsIntersecting(mapEnt.Value, worldAABB, callback, approx: approx, includeMap: includeMap); + } + + public void FindGridsIntersecting( + MapId mapId, + Box2 worldAABB, + ref TState state, + GridCallback callback, + bool approx = IMapManager.Approximate, + bool includeMap = IMapManager.IncludeMap) + { + if (TryGetMap(mapId, out var map)) + FindGridsIntersecting(map.Value, worldAABB, ref state, callback, approx: approx, includeMap: includeMap); + } + + public void FindGridsIntersecting( + MapId mapId, + Box2 worldAABB, + ref List> grids, + bool approx = IMapManager.Approximate, + bool includeMap = IMapManager.IncludeMap) + { + if (TryGetMap(mapId, out var map)) + FindGridsIntersecting(map.Value, worldAABB, ref grids, approx: approx, includeMap: includeMap); + } + + public void FindGridsIntersecting( + MapId mapId, + Box2Rotated worldBounds, + GridCallback callback, + bool approx = IMapManager.Approximate, + bool includeMap = IMapManager.IncludeMap) + { + if (TryGetMap(mapId, out var mapEnt)) + FindGridsIntersecting(mapEnt.Value, worldBounds, callback, approx: approx, includeMap: includeMap); + } + + public void FindGridsIntersecting( + MapId mapId, + Box2Rotated worldBounds, + ref TState state, + GridCallback callback, + bool approx = IMapManager.Approximate, + bool includeMap = IMapManager.IncludeMap) + { + if (TryGetMap(mapId, out var mapEnt)) + FindGridsIntersecting(mapEnt.Value, worldBounds, ref state, callback, approx: approx, includeMap: includeMap); + } + + public void FindGridsIntersecting( + MapId mapId, + Box2Rotated worldBounds, + ref List> grids, + bool approx = IMapManager.Approximate, + bool includeMap = IMapManager.IncludeMap) + { + if (TryGetMap(mapId, out var mapEnt)) + FindGridsIntersecting(mapEnt.Value, worldBounds, ref grids, approx: approx, includeMap: includeMap); + } + + #endregion + + #region EntityUid + + public void FindGridsIntersecting( + EntityUid mapEnt, + TShape shape, + Transform transform, + GridCallback callback, + bool approx = false, + bool includeMap = true) where TShape : IPhysShape + { + FindGridsIntersecting(mapEnt, shape, shape.ComputeAABB(transform, 0), transform, callback, approx: approx, includeMap: includeMap); + } + + public void FindGridsIntersecting( + EntityUid mapEnt, + TShape shape, + Transform transform, + ref TState state, + GridCallback callback, + bool approx = false, + bool includeMap = true) where TShape : IPhysShape + { + FindGridsIntersecting(mapEnt, shape, shape.ComputeAABB(transform, 0), transform, ref state, callback, approx: approx, includeMap: includeMap); + } + + public void FindGridsIntersecting( + EntityUid mapEnt, + List shapes, + Transform transform, + ref List> entities, + bool approx = false, + bool includeMap = true) + { + foreach (var shape in shapes) + { + FindGridsIntersecting(mapEnt, shape, transform, ref entities, approx: approx, includeMap: includeMap); + } + } + + public void FindGridsIntersecting( + EntityUid mapEnt, + TShape shape, + Transform transform, + ref List> grids, + bool approx = false, + bool includeMap = true) where TShape : IPhysShape + { + FindGridsIntersecting(mapEnt, shape, shape.ComputeAABB(transform, 0), transform, ref grids, approx: approx, includeMap: includeMap); + } + + public void FindGridsIntersecting( + EntityUid mapEnt, + Box2 worldAABB, + GridCallback callback, + bool approx = false, + bool includeMap = true) + { + var shape = new SlimPolygon(worldAABB); + FindGridsIntersecting(mapEnt, shape, worldAABB, Robust.Shared.Physics.Transform.Empty, callback, approx: approx, includeMap: includeMap); + } + + public void FindGridsIntersecting( + EntityUid mapEnt, + Box2 worldAABB, + ref TState state, + GridCallback callback, + bool approx = false, + bool includeMap = true) + { + var shape = new SlimPolygon(worldAABB); + FindGridsIntersecting(mapEnt, shape, worldAABB, Robust.Shared.Physics.Transform.Empty, ref state, callback, approx: approx, includeMap: includeMap); + } + + public void FindGridsIntersecting( + EntityUid mapEnt, + Box2 worldAABB, + ref List> grids, + bool approx = false, + bool includeMap = true) + { + var shape = new SlimPolygon(worldAABB); + FindGridsIntersecting(mapEnt, shape, worldAABB, Robust.Shared.Physics.Transform.Empty, ref grids, approx: approx, includeMap: includeMap); + } + + public void FindGridsIntersecting( + EntityUid mapEnt, + Box2Rotated worldBounds, + GridCallback callback, + bool approx = false, + bool includeMap = true) + { + var shape = new SlimPolygon(worldBounds); + FindGridsIntersecting(mapEnt, shape, Robust.Shared.Physics.Transform.Empty, callback, approx: approx, includeMap: includeMap); + } + + public void FindGridsIntersecting( + EntityUid mapEnt, + Box2Rotated worldBounds, + ref TState state, + GridCallback callback, + bool approx = false, + bool includeMap = true) + { + var shape = new SlimPolygon(worldBounds); + FindGridsIntersecting(mapEnt, shape, Robust.Shared.Physics.Transform.Empty, ref state, callback, approx: approx, includeMap: includeMap); + } + + public void FindGridsIntersecting( + EntityUid mapEnt, + Box2Rotated worldBounds, + ref List> grids, + bool approx = false, + bool includeMap = true) + { + var shape = new SlimPolygon(worldBounds); + FindGridsIntersecting(mapEnt, shape, Robust.Shared.Physics.Transform.Empty, ref grids, approx: approx, includeMap: includeMap); + } + + #endregion + + public IEnumerable> GetAllGrids(MapId mapId) + { + throw new System.NotImplementedException(); + } + + public IEnumerable GetAllMapGrids(MapId mapId) + { + throw new System.NotImplementedException(); + } + + public void FindGridsIntersecting( + EntityUid mapEnt, + TShape shape, + Box2 worldAABB, + Transform transform, + ref List> grids, + bool approx, + bool includeMap) where TShape : IPhysShape + { + var state = grids; + FindGridsIntersecting(mapEnt, shape, worldAABB, transform, ref state, + static (EntityUid uid, MapGridComponent grid, ref List> state) => + { + state.Add((uid, grid)); + return true; + }, + approx, includeMap + ); + } + + private void FindGridsIntersecting( + EntityUid mapEnt, + TShape shape, + Box2 worldAABB, + Transform transform, + GridCallback callback, + bool approx, + bool includeMap) where TShape : IPhysShape + { + var state = callback; + FindGridsIntersecting(mapEnt, shape, worldAABB, transform, ref state, + static (EntityUid uid, MapGridComponent grid, ref GridCallback state) => state.Invoke(uid, grid), + approx, includeMap + ); + } + + private void FindGridsIntersecting( + EntityUid mapEnt, + TShape shape, + Box2 worldAABB, + Transform transform, + ref TState state, + GridCallback callback, + bool approx, + bool includeMap) where TShape : IPhysShape + { + if (!_gridTreeQuery.TryGetComponent(mapEnt, out var gridTree)) + return; + + if (includeMap && _gridQuery.TryGetComponent(mapEnt, out var mapGrid)) + { + callback(mapEnt, mapGrid, ref state); + } + + var gridState = new GridQueryState( + callback, + state, + worldAABB, + shape, + transform, + gridTree.Tree, + this, + _transform, + approx); + + gridTree.Tree.Query(ref gridState, static (ref GridQueryState state, DynamicTree.Proxy proxy) => + { + // Even for approximate we'll check if any chunks roughly overlap. + var data = state.Tree.GetUserData(proxy); + var gridInvMatrix = state.TransformSystem.GetInvWorldMatrix(data.Uid); + var localAABB = gridInvMatrix.TransformBox(state.WorldAABB); + + var overlappingChunks = state.MapSystem.GetLocalMapChunks(data.Uid, data.Grid, localAABB); + + if (state.Approximate) + { + if (!overlappingChunks.MoveNext(out _)) + return true; + } + else if (!state.MapSystem.IsIntersecting(overlappingChunks, state.Shape, state.Transform, (data.Uid, data.Fixtures))) + { + return true; + } + + var callbackState = state.State; + var result = state.Callback(data.Uid, data.Grid, ref callbackState); + state.State = callbackState; + + return result; + }, worldAABB); + + // By-ref things + state = gridState.State; + } + + private bool IsIntersecting( + ChunkEnumerator enumerator, + TShape shape, + Transform shapeTransform, + Entity grid) where TShape : IPhysShape + { + var gridTransform = _physics.GetPhysicsTransform(grid); + + while (enumerator.MoveNext(out var chunk)) + { + foreach (var id in chunk.Fixtures) + { + var fixture = grid.Comp.Fixtures[id]; + + for (var j = 0; j < fixture.Shape.ChildCount; j++) + { + if (_manifolds.TestOverlap(shape, 0, fixture.Shape, j, shapeTransform, gridTransform)) + { + return true; + } + } + } + } + + return false; + } + + private record struct GridQueryState( + GridCallback Callback, + TState State, + Box2 WorldAABB, + TShape Shape, + Transform Transform, + B2DynamicTree<(EntityUid Uid, FixturesComponent Fixtures, MapGridComponent Grid)> Tree, + SharedMapSystem MapSystem, + SharedTransformSystem TransformSystem, + bool Approximate + ) where TShape : IPhysShape; +} diff --git a/Robust.Shared/GameObjects/Systems/SharedMapSystem.cs b/Robust.Shared/GameObjects/Systems/SharedMapSystem.cs index 2f99eb9c7ae..16e07f3d50f 100644 --- a/Robust.Shared/GameObjects/Systems/SharedMapSystem.cs +++ b/Robust.Shared/GameObjects/Systems/SharedMapSystem.cs @@ -8,6 +8,7 @@ using Robust.Shared.Maths; using Robust.Shared.Network; using Robust.Shared.Physics; +using Robust.Shared.Physics.Collision; using Robust.Shared.Physics.Systems; using Robust.Shared.Timing; using Robust.Shared.Utility; @@ -22,6 +23,7 @@ public abstract partial class SharedMapSystem : EntitySystem [Dependency] private ITileDefinitionManager _tileMan = default!; [Dependency] private IGameTiming _timing = default!; [Dependency] protected IMapManager MapManager = default!; + [Dependency] private IManifoldManager _manifolds = default!; [Dependency] private IMapManagerInternal _mapInternal = default!; [Dependency] private INetManager _netManager = default!; [Dependency] private FixtureSystem _fixtures = default!; @@ -34,6 +36,7 @@ public abstract partial class SharedMapSystem : EntitySystem private EntityQuery _gridQuery; private EntityQuery _metaQuery; private EntityQuery _xformQuery; + [Dependency] EntityQuery _gridTreeQuery; internal Dictionary Maps { get; } = new(); diff --git a/Robust.Shared/Map/IMapManager.cs b/Robust.Shared/Map/IMapManager.cs index d0aaea4a5a4..8855d2366fd 100644 --- a/Robust.Shared/Map/IMapManager.cs +++ b/Robust.Shared/Map/IMapManager.cs @@ -82,29 +82,37 @@ public interface IMapManager #region MapId + [Obsolete("Use MapSystem")] public void FindGridsIntersecting(MapId mapId, T shape, Transform transform, ref List> grids, bool approx = Approximate, bool includeMap = IncludeMap) where T : IPhysShape; + [Obsolete("Use MapSystem")] public void FindGridsIntersecting(MapId mapId, T shape, Transform transform, GridCallback callback, bool approx = Approximate, bool includeMap = IncludeMap) where T : IPhysShape; + [Obsolete("Use MapSystem")] public void FindGridsIntersecting(MapId mapId, Box2 worldAABB, GridCallback callback, bool approx = Approximate, bool includeMap = IncludeMap); + [Obsolete("Use MapSystem")] public void FindGridsIntersecting(MapId mapId, Box2 worldAABB, ref TState state, GridCallback callback, bool approx = Approximate, bool includeMap = IncludeMap); + [Obsolete("Use MapSystem")] public void FindGridsIntersecting(MapId mapId, Box2 worldAABB, ref List> grids, bool approx = Approximate, bool includeMap = IncludeMap); + [Obsolete("Use MapSystem")] public void FindGridsIntersecting(MapId mapId, Box2Rotated worldBounds, GridCallback callback, bool approx = Approximate, bool includeMap = IncludeMap); + [Obsolete("Use MapSystem")] public void FindGridsIntersecting(MapId mapId, Box2Rotated worldBounds, ref TState state, GridCallback callback, bool approx = Approximate, bool includeMap = IncludeMap); + [Obsolete("Use MapSystem")] public void FindGridsIntersecting(MapId mapId, Box2Rotated worldBounds, ref List> grids, bool approx = Approximate, bool includeMap = IncludeMap); @@ -112,38 +120,48 @@ public void FindGridsIntersecting(MapId mapId, Box2Rotated worldBounds, ref List #region MapEnt + [Obsolete("Use MapSystem")] public void FindGridsIntersecting(EntityUid mapEnt, T shape, Transform transform, GridCallback callback, bool approx = Approximate, bool includeMap = IncludeMap) where T : IPhysShape; + [Obsolete("Use MapSystem")] public void FindGridsIntersecting(EntityUid mapEnt, T shape, Transform transform, ref TState state, GridCallback callback, bool approx = Approximate, bool includeMap = IncludeMap) where T : IPhysShape; /// /// Returns true if any grids overlap the specified shapes. /// + [Obsolete("Use MapSystem")] public void FindGridsIntersecting(EntityUid mapEnt, List shapes, Transform transform, ref List> entities, bool approx = Approximate, bool includeMap = IncludeMap); + [Obsolete("Use MapSystem")] public void FindGridsIntersecting(EntityUid mapEnt, T shape, Transform transform, ref List> grids, bool approx = Approximate, bool includeMap = IncludeMap) where T : IPhysShape; + [Obsolete("Use MapSystem")] public void FindGridsIntersecting(EntityUid mapEnt, Box2 worldAABB, GridCallback callback, bool approx = Approximate, bool includeMap = IncludeMap); + [Obsolete("Use MapSystem")] public void FindGridsIntersecting(EntityUid mapEnt, Box2 worldAABB, ref TState state, GridCallback callback, bool approx = Approximate, bool includeMap = IncludeMap); + [Obsolete("Use MapSystem")] public void FindGridsIntersecting(EntityUid mapEnt, Box2 worldAABB, ref List> grids, bool approx = Approximate, bool includeMap = IncludeMap); + [Obsolete("Use MapSystem")] public void FindGridsIntersecting(EntityUid mapEnt, Box2Rotated worldBounds, GridCallback callback, bool approx = Approximate, bool includeMap = IncludeMap); + [Obsolete("Use MapSystem")] public void FindGridsIntersecting(EntityUid mapEnt, Box2Rotated worldBounds, ref TState state, GridCallback callback, bool approx = Approximate, bool includeMap = IncludeMap); + [Obsolete("Use MapSystem")] public void FindGridsIntersecting(EntityUid mapEnt, Box2Rotated worldBounds, ref List> grids, bool approx = Approximate, bool includeMap = IncludeMap); @@ -152,6 +170,7 @@ public void FindGridsIntersecting(EntityUid mapEnt, Box2Rotated worldBounds, #region TryFindGridAt + [Obsolete("Use MapSystem")] public bool TryFindGridAt( EntityUid mapEnt, Vector2 worldPos, @@ -161,12 +180,14 @@ public bool TryFindGridAt( /// /// Attempts to find the map grid under the map location. /// + [Obsolete("Use MapSystem")] public bool TryFindGridAt(MapId mapId, Vector2 worldPos, out EntityUid uid, [NotNullWhen(true)] out MapGridComponent? grid); /// /// Attempts to find the map grid under the map location. /// + [Obsolete("Use MapSystem")] public bool TryFindGridAt(MapCoordinates mapCoordinates, out EntityUid uid, [NotNullWhen(true)] out MapGridComponent? grid); diff --git a/Robust.Shared/Map/MapManager.Queries.cs b/Robust.Shared/Map/MapManager.Queries.cs index f37a545525f..0799927e63f 100644 --- a/Robust.Shared/Map/MapManager.Queries.cs +++ b/Robust.Shared/Map/MapManager.Queries.cs @@ -4,102 +4,74 @@ using System.Numerics; using Robust.Shared.GameObjects; using Robust.Shared.Map.Components; -using Robust.Shared.Map.Enumerators; using Robust.Shared.Maths; using Robust.Shared.Physics; using Robust.Shared.Physics.Collision.Shapes; -using Robust.Shared.Physics.Shapes; namespace Robust.Shared.Map; internal partial class MapManager { - private bool IsIntersecting( - ChunkEnumerator enumerator, - T shape, - Transform shapeTransform, - Entity grid) where T : IPhysShape - { - var gridTransform = _physics.GetPhysicsTransform(grid); - - while (enumerator.MoveNext(out var chunk)) - { - foreach (var id in chunk.Fixtures) - { - var fixture = grid.Comp.Fixtures[id]; - - for (var j = 0; j < fixture.Shape.ChildCount; j++) - { - if (_manifolds.TestOverlap(shape, 0, fixture.Shape, j, shapeTransform, gridTransform)) - { - return true; - } - } - } - } - - return false; - } - - #region MapId + #region MapId [Obsolete] + [Obsolete("use SharedMapSystem")] public void FindGridsIntersecting(MapId mapId, T shape, Transform transform, ref List> grids, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) where T : IPhysShape { - if (_mapSystem.TryGetMap(mapId, out var mapEnt)) - FindGridsIntersecting(mapEnt.Value, shape, transform, ref grids, approx: approx, includeMap: includeMap); + _mapSystem.FindGridsIntersecting(mapId, shape, transform, ref grids, approx: approx, includeMap: includeMap); } + [Obsolete("use SharedMapSystem")] public void FindGridsIntersecting(MapId mapId, T shape, Transform transform, GridCallback callback, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) where T : IPhysShape { - if (_mapSystem.TryGetMap(mapId, out var mapEnt)) - FindGridsIntersecting(mapEnt.Value, shape, transform, callback, approx: approx, includeMap: includeMap); + _mapSystem.FindGridsIntersecting(mapId, shape, transform, callback, approx: approx, includeMap: includeMap); } + [Obsolete("use SharedMapSystem")] public void FindGridsIntersecting(MapId mapId, Box2 worldAABB, GridCallback callback, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) { - if (_mapSystem.TryGetMap(mapId, out var mapEnt)) - FindGridsIntersecting(mapEnt.Value, worldAABB, callback, approx: approx, includeMap: includeMap); + _mapSystem.FindGridsIntersecting(mapId, worldAABB, callback, approx: approx, includeMap: includeMap); } + [Obsolete("use SharedMapSystem")] public void FindGridsIntersecting(MapId mapId, Box2 worldAABB, ref TState state, GridCallback callback, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) { - if (_mapSystem.TryGetMap(mapId, out var map)) - FindGridsIntersecting(map.Value, worldAABB, ref state, callback, approx: approx, includeMap: includeMap); + _mapSystem.FindGridsIntersecting(mapId, worldAABB, ref state, callback, approx: approx, includeMap: includeMap); } + [Obsolete("use SharedMapSystem")] public void FindGridsIntersecting(MapId mapId, Box2 worldAABB, ref List> grids, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) { - if (_mapSystem.TryGetMap(mapId, out var map)) - FindGridsIntersecting(map.Value, worldAABB, ref grids, approx: approx, includeMap: includeMap); + _mapSystem.FindGridsIntersecting(mapId, worldAABB, ref grids, approx: approx, includeMap: includeMap); } + [Obsolete("use SharedMapSystem")] public void FindGridsIntersecting(MapId mapId, Box2Rotated worldBounds, GridCallback callback, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) { - if (_mapSystem.TryGetMap(mapId, out var mapEnt)) - FindGridsIntersecting(mapEnt.Value, worldBounds, callback, approx: approx, includeMap: includeMap); + _mapSystem.FindGridsIntersecting(mapId, worldBounds, callback, approx: approx, includeMap: includeMap); } + [Obsolete("use SharedMapSystem")] public void FindGridsIntersecting(MapId mapId, Box2Rotated worldBounds, ref TState state, GridCallback callback, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) { - if (_mapSystem.TryGetMap(mapId, out var mapEnt)) - FindGridsIntersecting(mapEnt.Value, worldBounds, ref state, callback, approx: approx, includeMap: includeMap); + _mapSystem.FindGridsIntersecting(mapId, worldBounds, ref state, callback, approx: approx, includeMap: includeMap); } + [Obsolete("use SharedMapSystem")] public void FindGridsIntersecting(MapId mapId, Box2Rotated worldBounds, ref List> grids, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) { - if (_mapSystem.TryGetMap(mapId, out var mapEnt)) - FindGridsIntersecting(mapEnt.Value, worldBounds, ref grids, approx: approx, includeMap: includeMap); + _mapSystem.FindGridsIntersecting(mapId, worldBounds, ref grids, approx: approx, includeMap: includeMap); } #endregion - #region MapEnt + #region MapEnt [Obsolete] + [Obsolete("use SharedMapSystem")] public void FindGridsIntersecting( EntityUid mapEnt, T shape, @@ -108,19 +80,10 @@ public void FindGridsIntersecting( bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) where T : IPhysShape { - FindGridsIntersecting(mapEnt, shape, shape.ComputeAABB(transform, 0), transform, callback, approx: approx, includeMap: includeMap); - } - - private void FindGridsIntersecting(EntityUid mapEnt, T shape, Box2 worldAABB, Transform transform, GridCallback callback, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) where T : IPhysShape - { - // This is here so we don't double up on code. - var state = callback; - - FindGridsIntersecting(mapEnt, shape, worldAABB, transform, ref state, - static (EntityUid uid, MapGridComponent grid, ref GridCallback state) => state.Invoke(uid, grid), - approx: approx, includeMap: includeMap); + _mapSystem.FindGridsIntersecting(mapEnt, shape, transform, callback, approx: approx, includeMap: includeMap); } + [Obsolete("use SharedMapSystem")] public void FindGridsIntersecting( EntityUid mapEnt, T shape, @@ -130,236 +93,103 @@ public void FindGridsIntersecting( bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) where T : IPhysShape { - FindGridsIntersecting(mapEnt, shape, shape.ComputeAABB(transform, 0), transform, ref state, callback, approx: approx, includeMap: includeMap); - } - - private void FindGridsIntersecting( - EntityUid mapEnt, - T shape, - Box2 worldAABB, - Transform transform, - ref TState state, - GridCallback callback, - bool approx = IMapManager.Approximate, - bool includeMap = IMapManager.IncludeMap) where T : IPhysShape - { - if (!_gridTreeQuery.TryGetComponent(mapEnt, out var gridTree)) - return; - - if (includeMap && _gridQuery.TryGetComponent(mapEnt, out var mapGrid)) - { - callback(mapEnt, mapGrid, ref state); - } - - var gridState = new GridQueryState( - callback, - state, - worldAABB, - shape, - transform, - gridTree.Tree, - _mapSystem, - this, - _transformSystem, - approx); - - gridTree.Tree.Query(ref gridState, static (ref GridQueryState state, DynamicTree.Proxy proxy) => - { - // Even for approximate we'll check if any chunks roughly overlap. - var data = state.Tree.GetUserData(proxy); - var gridInvMatrix = state.TransformSystem.GetInvWorldMatrix(data.Uid); - var localAABB = gridInvMatrix.TransformBox(state.WorldAABB); - - var overlappingChunks = state.MapSystem.GetLocalMapChunks(data.Uid, data.Grid, localAABB); - - if (state.Approximate) - { - if (!overlappingChunks.MoveNext(out _)) - return true; - } - else if (!state.MapManager.IsIntersecting(overlappingChunks, state.Shape, state.Transform, (data.Uid, data.Fixtures))) - { - return true; - } - - var callbackState = state.State; - var result = state.Callback(data.Uid, data.Grid, ref callbackState); - state.State = callbackState; - - return result; - }, worldAABB); - - // By-ref things - state = gridState.State; + _mapSystem.FindGridsIntersecting(mapEnt, shape, transform, ref state, callback, approx: approx, includeMap: includeMap); } /// /// Returns true if any grids overlap the specified shapes. /// + [Obsolete("use SharedMapSystem")] public void FindGridsIntersecting(EntityUid mapEnt, List shapes, Transform transform, ref List> entities, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) { - foreach (var shape in shapes) - { - FindGridsIntersecting(mapEnt, shape, shape.ComputeAABB(transform, 0), transform, ref entities, approx: approx, includeMap: includeMap); - } + _mapSystem.FindGridsIntersecting(mapEnt, shapes, transform, ref entities, approx: approx, includeMap: includeMap); } + [Obsolete("use SharedMapSystem")] public void FindGridsIntersecting(EntityUid mapEnt, T shape, Transform transform, ref List> grids, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) where T : IPhysShape { - FindGridsIntersecting(mapEnt, shape, shape.ComputeAABB(transform, 0), transform, ref grids, approx: approx, includeMap: includeMap); + _mapSystem.FindGridsIntersecting(mapEnt, shape, transform, ref grids, approx: approx, includeMap: includeMap); } + [Obsolete("use SharedMapSystem")] public void FindGridsIntersecting(EntityUid mapEnt, T shape, Box2 worldAABB, Transform transform, ref List> grids, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) where T : IPhysShape { - var state = grids; - - FindGridsIntersecting(mapEnt, shape, worldAABB, transform, ref state, - static (EntityUid uid, MapGridComponent grid, ref List> list) => - { - list.Add((uid, grid)); - return true; - }, approx: approx, includeMap: includeMap); + _mapSystem.FindGridsIntersecting(mapEnt, shape, worldAABB, transform, ref grids, approx: approx, includeMap: includeMap); } + [Obsolete("use SharedMapSystem")] public void FindGridsIntersecting(EntityUid mapEnt, Box2 worldAABB, GridCallback callback, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) { - var polygon = new SlimPolygon(worldAABB); - FindGridsIntersecting(mapEnt, polygon, worldAABB, Transform.Empty, callback, approx: approx, includeMap: includeMap); + _mapSystem.FindGridsIntersecting(mapEnt, worldAABB, callback, approx: approx, includeMap: includeMap); } + [Obsolete("use SharedMapSystem")] public void FindGridsIntersecting(EntityUid mapEnt, Box2 worldAABB, ref TState state, GridCallback callback, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) { - var polygon = new SlimPolygon(worldAABB); - FindGridsIntersecting(mapEnt, polygon, worldAABB, Transform.Empty, ref state, callback, approx: approx, includeMap: includeMap); + _mapSystem.FindGridsIntersecting(mapEnt, worldAABB, ref state, callback, approx: approx, includeMap: includeMap); } + [Obsolete("use SharedMapSystem")] public void FindGridsIntersecting(EntityUid mapEnt, Box2 worldAABB, ref List> grids, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) { - var polygon = new SlimPolygon(worldAABB); - FindGridsIntersecting(mapEnt, polygon, worldAABB, Transform.Empty, ref grids, approx: approx, includeMap: includeMap); + _mapSystem.FindGridsIntersecting(mapEnt, worldAABB, ref grids, approx: approx, includeMap: includeMap); } + [Obsolete("use SharedMapSystem")] public void FindGridsIntersecting(EntityUid mapEnt, Box2Rotated worldBounds, GridCallback callback, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) { - var polygon = new SlimPolygon(worldBounds); - FindGridsIntersecting(mapEnt, polygon, worldBounds.CalcBoundingBox(), Transform.Empty, callback, approx: approx, includeMap: includeMap); + _mapSystem.FindGridsIntersecting(mapEnt, worldBounds, callback, approx: approx, includeMap: includeMap); } + [Obsolete("use SharedMapSystem")] public void FindGridsIntersecting(EntityUid mapEnt, Box2Rotated worldBounds, ref TState state, GridCallback callback, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) { - var polygon = new SlimPolygon(worldBounds); - FindGridsIntersecting(mapEnt, polygon, worldBounds.CalcBoundingBox(), Transform.Empty, ref state, callback, approx: approx, includeMap: includeMap); + _mapSystem.FindGridsIntersecting(mapEnt, worldBounds, ref state, callback, approx: approx, includeMap: includeMap); } + [Obsolete("use SharedMapSystem")] public void FindGridsIntersecting(EntityUid mapEnt, Box2Rotated worldBounds, ref List> grids, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) { - var polygon = new SlimPolygon(worldBounds); - FindGridsIntersecting(mapEnt, polygon, worldBounds.CalcBoundingBox(), Transform.Empty, ref grids, approx: approx, includeMap: includeMap); + _mapSystem.FindGridsIntersecting(mapEnt, worldBounds, ref grids, approx: approx, includeMap: includeMap); } #endregion #region TryFindGridAt + [Obsolete("use SharedMapSystem")] public bool TryFindGridAt( EntityUid mapEnt, Vector2 worldPos, out EntityUid uid, [NotNullWhen(true)] out MapGridComponent? grid) { - var rangeVec = new Vector2(0.2f, 0.2f); - - // Need to enlarge the AABB by at least the grid shrinkage size. - var aabb = new Box2(worldPos - rangeVec, worldPos + rangeVec); - - uid = EntityUid.Invalid; - grid = null; - var state = (uid, grid, worldPos, _mapSystem, _transformSystem); - - FindGridsIntersecting(mapEnt, aabb, ref state, static (EntityUid iUid, MapGridComponent iGrid, ref ( - EntityUid uid, - MapGridComponent? grid, - Vector2 worldPos, - SharedMapSystem mapSystem, - SharedTransformSystem xformSystem) tuple) => - { - // Turn the worldPos into a localPos and work out the relevant chunk we need to check - // This is much faster than iterating over every chunk individually. - // (though now we need some extra calcs up front). - - // Doesn't use WorldBounds because it's just an AABB. - var matrix = tuple.xformSystem.GetInvWorldMatrix(iUid); - var localPos = Vector2.Transform(tuple.worldPos, matrix); - - // NOTE: - // If you change this to use fixtures instead (i.e. if you want half-tiles) then you need to make sure - // you account for the fact that fixtures are shrunk slightly! - var chunkIndices = SharedMapSystem.GetChunkIndices(localPos, iGrid.ChunkSize); - - if (!iGrid.Chunks.TryGetValue(chunkIndices, out var chunk)) - return true; - - var chunkRelative = SharedMapSystem.GetChunkRelative(localPos, iGrid.ChunkSize); - var chunkTile = chunk.GetTile(chunkRelative); - - if (chunkTile.IsEmpty) - return true; - - tuple.uid = iUid; - tuple.grid = iGrid; - return false; - }, approx: true, includeMap: false); - - if (state.grid == null && _gridQuery.TryGetComponent(mapEnt, out var mapGrid)) - { - uid = mapEnt; - grid = mapGrid; - return true; - } - - uid = state.uid; - grid = state.grid; - return grid != null; + return _mapSystem.TryFindGridAt(mapEnt, worldPos, out uid, out grid); } /// /// Attempts to find the map grid under the map location. /// + [Obsolete("use SharedMapSystem")] public bool TryFindGridAt(MapId mapId, Vector2 worldPos, out EntityUid uid, [NotNullWhen(true)] out MapGridComponent? grid) { - if (_mapSystem.TryGetMap(mapId, out var map)) - return TryFindGridAt(map.Value, worldPos, out uid, out grid); - - uid = default; - grid = null; - return false; + return _mapSystem.TryFindGridAt(mapId, worldPos, out uid, out grid); } /// /// Attempts to find the map grid under the map location. /// + [Obsolete("use SharedMapSystem")] public bool TryFindGridAt(MapCoordinates mapCoordinates, out EntityUid uid, [NotNullWhen(true)] out MapGridComponent? grid) { - return TryFindGridAt(mapCoordinates.MapId, mapCoordinates.Position, out uid, out grid); + return _mapSystem.TryFindGridAt(mapCoordinates, out uid, out grid); } #endregion - - private record struct GridQueryState( - GridCallback Callback, - TState State, - Box2 WorldAABB, - T Shape, - Transform Transform, - B2DynamicTree<(EntityUid Uid, FixturesComponent Fixtures, MapGridComponent Grid)> Tree, - SharedMapSystem MapSystem, - MapManager MapManager, - SharedTransformSystem TransformSystem, - bool Approximate); } From e1b090f115c1456cdefa940760a26c9a148a69a9 Mon Sep 17 00:00:00 2001 From: TemporalOroboros Date: Tue, 19 May 2026 01:44:15 -0700 Subject: [PATCH 02/13] Move CreateGrid to SharedMapSystem Moves the functionality for CreateGrid and its variants to SharedMapSystem Hollows out the MapManager methods and converts them into relays for the SharedMapSystem methods Obsoletes them too Also moves over the GetAllMapGrids and GetAllGrids methods --- .../Systems/SharedMapSystem.Grid.cs | 40 ++++++++++++ .../Systems/SharedMapSystem.Lookup.cs | 16 ++++- Robust.Shared/Map/IMapManager.cs | 7 ++ .../Map/MapManager.GridCollection.cs | 64 +++++-------------- 4 files changed, 76 insertions(+), 51 deletions(-) diff --git a/Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.cs b/Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.cs index 6c21c94534d..f388a747cf0 100644 --- a/Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.cs +++ b/Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.cs @@ -21,6 +21,46 @@ namespace Robust.Shared.GameObjects; public abstract partial class SharedMapSystem { + #region CreateGrid + + public Entity CreateGridEntity(MapId mapId, GridCreateOptions? options = null) + { + return CreateGridEntity(GetMap(mapId), options); + } + + public Entity CreateGridEntity(EntityUid mapEnt, GridCreateOptions? options = null) + { + options ??= GridCreateOptions.Default; + return CreateGridInternal(mapEnt, options.Value); + } + + protected Entity CreateGridInternal(EntityUid mapEnt, GridCreateOptions options) + { + var gridEnt = EntityManager.CreateEntityUninitialized(null); + + var grid = EnsureComp(gridEnt); + grid.ChunkSize = options.ChunkSize; + + Log.Debug("Binding new grid {gridEnt}"); + + //TODO: This is a hack to get TransformComponent.MapId working before entity states + //are applied. After they are applied the parent may be different, but the MapId will + //be the same. This causes TransformComponent.ParentUid of a grid to be unsafe to + //use in transform states anytime before the state parent is properly set. + _transform.SetParent(gridEnt, mapEnt); + + var meta = _metaQuery.GetComponent(gridEnt); + EntityManager.System().SetEntityName(gridEnt, $"grid", meta); + EntityManager.InitializeComponents(gridEnt, meta); + EntityManager.StartComponents(gridEnt); + // Note that this does not actually map-initialize the grid entity, even if the map its being spawn on has already been initialized. + // I don't know whether that is intentional or not. + + return (gridEnt, grid); + } + + #endregion + #region Chunk helpers [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/Robust.Shared/GameObjects/Systems/SharedMapSystem.Lookup.cs b/Robust.Shared/GameObjects/Systems/SharedMapSystem.Lookup.cs index 00af546bdfd..1c38c60aee0 100644 --- a/Robust.Shared/GameObjects/Systems/SharedMapSystem.Lookup.cs +++ b/Robust.Shared/GameObjects/Systems/SharedMapSystem.Lookup.cs @@ -308,12 +308,24 @@ public void FindGridsIntersecting( public IEnumerable> GetAllGrids(MapId mapId) { - throw new System.NotImplementedException(); + var query = AllEntityQuery(); + while (query.MoveNext(out var uid, out var grid, out var xform)) + { + if (xform.MapID != mapId) + continue; + + yield return (uid, grid); + } } public IEnumerable GetAllMapGrids(MapId mapId) { - throw new System.NotImplementedException(); + var query = AllEntityQuery(); + while (query.MoveNext(out var grid, out var xform)) + { + if (xform.MapID == mapId) + yield return grid; + } } public void FindGridsIntersecting( diff --git a/Robust.Shared/Map/IMapManager.cs b/Robust.Shared/Map/IMapManager.cs index 8855d2366fd..d294b2d8d03 100644 --- a/Robust.Shared/Map/IMapManager.cs +++ b/Robust.Shared/Map/IMapManager.cs @@ -70,14 +70,21 @@ public interface IMapManager void DeleteMap(MapId mapId); // ReSharper disable once MethodOverloadWithOptionalParameter + [Obsolete("Use MapSystem.CreateGridEntity(...).Comp")] MapGridComponent CreateGrid(MapId currentMapId, ushort chunkSize = 16); + [Obsolete("Use MapSystem.CreateGridEntity(...).Comp")] MapGridComponent CreateGrid(MapId currentMapId, in GridCreateOptions options); + [Obsolete("Use MapSystem.CreateGridEntity(...).Comp")] MapGridComponent CreateGrid(MapId currentMapId); + [Obsolete("Use MapSystem")] Entity CreateGridEntity(MapId currentMapId, GridCreateOptions? options = null); + [Obsolete("Use MapSystem")] Entity CreateGridEntity(EntityUid map, GridCreateOptions? options = null); + [Obsolete("Use MapSystem")] IEnumerable GetAllMapGrids(MapId mapId); + [Obsolete("Use MapSystem")] IEnumerable> GetAllGrids(MapId mapId); #region MapId diff --git a/Robust.Shared/Map/MapManager.GridCollection.cs b/Robust.Shared/Map/MapManager.GridCollection.cs index cf23be33c1c..96f89e7547b 100644 --- a/Robust.Shared/Map/MapManager.GridCollection.cs +++ b/Robust.Shared/Map/MapManager.GridCollection.cs @@ -1,42 +1,42 @@ using System; using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; using Robust.Shared.GameObjects; using Robust.Shared.Map.Components; using Robust.Shared.Maths; using Robust.Shared.Utility; -// All the obsolete warnings about GridId are probably useless here. -#pragma warning disable CS0618 - namespace Robust.Shared.Map; internal partial class MapManager { // ReSharper disable once MethodOverloadWithOptionalParameter + [Obsolete("use SharedMapSystem.CreateGridEntity(...).Comp")] public MapGridComponent CreateGrid(MapId currentMapId, ushort chunkSize = 16) { - return CreateGrid(GetMapEntityIdOrThrow(currentMapId), chunkSize, default); + return CreateGridEntity(currentMapId, options: GridCreateOptions.Default with { ChunkSize = chunkSize }).Comp; } + [Obsolete("use SharedMapSystem.CreateGridEntity(...).Comp")] public MapGridComponent CreateGrid(MapId currentMapId, in GridCreateOptions options) { - return CreateGrid(GetMapEntityIdOrThrow(currentMapId), options.ChunkSize, default); + return CreateGridEntity(currentMapId, options: options).Comp; } + [Obsolete("use SharedMapSystem.CreateGridEntity(...).Comp")] public MapGridComponent CreateGrid(MapId currentMapId) { - return CreateGrid(currentMapId, GridCreateOptions.Default); + return CreateGridEntity(currentMapId, options: GridCreateOptions.Default).Comp; } + [Obsolete("use SharedMapSystem.CreateGridEntity")] public Entity CreateGridEntity(MapId currentMapId, GridCreateOptions? options = null) { - return CreateGridEntity(GetMapEntityIdOrThrow(currentMapId), options); + return _mapSystem.CreateGridEntity(currentMapId, options: options); } + [Obsolete("use SharedMapSystem.CreateGridEntity")] public Entity CreateGridEntity(EntityUid map, GridCreateOptions? options = null) { - options ??= GridCreateOptions.Default; - return CreateGrid(map, options.Value.ChunkSize, default); + return _mapSystem.CreateGridEntity(map, options: options); } [Obsolete("Use HasComponent(uid)")] @@ -45,28 +45,19 @@ public bool IsGrid(EntityUid uid) return EntityManager.HasComponent(uid); } + [Obsolete("use SharedMapSystem.GetAllMapGrids")] public IEnumerable GetAllMapGrids(MapId mapId) { - var query = EntityManager.AllEntityQueryEnumerator(); - while (query.MoveNext(out var grid, out var xform)) - { - if (xform.MapID == mapId) - yield return grid; - } + return _mapSystem.GetAllMapGrids(mapId); } + [Obsolete("use SharedMapSystem.GetAllGrids")] public IEnumerable> GetAllGrids(MapId mapId) { - var query = EntityManager.AllEntityQueryEnumerator(); - while (query.MoveNext(out var uid, out var grid, out var xform)) - { - if (xform.MapID != mapId) - continue; - - yield return (uid, grid); - } + return _mapSystem.GetAllGrids(mapId); } + [Obsolete("just delete the grid entity")] public virtual void DeleteGrid(EntityUid euid) { // Possible the grid was already deleted / is invalid @@ -104,29 +95,4 @@ void IMapManagerInternal.RaiseOnTileChanged(Entity entity, Til var ev = new TileChangedEvent(entity, tileRef, oldTile, chunk); EntityManager.EventBus.RaiseLocalEvent(entity.Owner, ref ev, true); } - - protected Entity CreateGrid(EntityUid map, ushort chunkSize, EntityUid forcedGridEuid) - { - var gridEnt = EntityManager.CreateEntityUninitialized(null, forcedGridEuid); - - var grid = EntityManager.AddComponent(gridEnt); - grid.ChunkSize = chunkSize; - - _sawmill.Debug($"Binding new grid {gridEnt}"); - - //TODO: This is a hack to get TransformComponent.MapId working before entity states - //are applied. After they are applied the parent may be different, but the MapId will - //be the same. This causes TransformComponent.ParentUid of a grid to be unsafe to - //use in transform states anytime before the state parent is properly set. - EntityManager.GetComponent(gridEnt).AttachParent(map); - - var meta = EntityManager.GetComponent(gridEnt); - EntityManager.System().SetEntityName(gridEnt, $"grid", meta); - EntityManager.InitializeComponents(gridEnt, meta); - EntityManager.StartComponents(gridEnt); - // Note that this does not actually map-initialize the grid entity, even if the map its being spawn on has already been initialized. - // I don't know whether that is intentional or not. - - return (gridEnt, grid); - } } From 9220fac16911c1fab55a963bbfd6ccad9ed20da4 Mon Sep 17 00:00:00 2001 From: TemporalOroboros Date: Tue, 19 May 2026 02:02:51 -0700 Subject: [PATCH 03/13] Move RaiseOnTileChanged to SharedMapSystem Also moves the SuppressOnTileChanged flag member to SharedMapSystem Hollows out and obsoletes the MapManager versions --- .../Systems/SharedMapSystem.Grid.cs | 19 ++++++++++++++----- .../GameObjects/Systems/SharedMapSystem.cs | 8 ++++++++ Robust.Shared/Map/IMapManager.cs | 1 + Robust.Shared/Map/IMapManagerInternal.cs | 2 ++ .../Map/MapManager.GridCollection.cs | 14 ++++++++------ 5 files changed, 33 insertions(+), 11 deletions(-) diff --git a/Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.cs b/Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.cs index f388a747cf0..48b45568dbf 100644 --- a/Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.cs +++ b/Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.cs @@ -362,7 +362,7 @@ private void ApplyChunkData( var gridIndices = deletedChunk.ChunkTileToGridTile((x, y)); var newTileRef = new TileRef(uid, gridIndices, Tile.Empty); - _mapInternal.RaiseOnTileChanged(gridEnt, newTileRef, oldTile, index); + RaiseOnTileChanged(gridEnt, newTileRef, oldTile, index); } } @@ -886,7 +886,7 @@ public void SetTiles(EntityUid uid, MapGridComponent grid, List<(Vector2i GridIn // Suppress sending out events for each tile changed // We're going to send them all out together at the end - MapManager.SuppressOnTileChanged = true; + SuppressOnTileChanged = true; foreach (var (gridIndices, tile) in tiles) { @@ -923,7 +923,7 @@ public void SetTiles(EntityUid uid, MapGridComponent grid, List<(Vector2i GridIn RegenerateCollision(uid, grid, modified); // Back to normal - MapManager.SuppressOnTileChanged = false; + SuppressOnTileChanged = false; } public TilesEnumerator GetLocalTilesEnumerator(EntityUid uid, MapGridComponent grid, Box2 aabb, @@ -1671,10 +1671,10 @@ private void OnTileModified(EntityUid uid, MapGridComponent grid, MapChunk mapCh // The map serializer currently sets tiles of unbound grids as part of the deserialization process // It properly sets SuppressOnTileChanged so that the event isn't spammed for every tile on the grid. // ParentMapId is not able to be accessed on unbound grids, so we can't even call this function for unbound grids. - if (!MapManager.SuppressOnTileChanged) + if (!SuppressOnTileChanged) { var newTileRef = new TileRef(uid, gridTile, newTile); - _mapInternal.RaiseOnTileChanged((uid, grid), newTileRef, oldTile, mapChunk.Indices); + RaiseOnTileChanged((uid, grid), newTileRef, oldTile, mapChunk.Indices); } if (shapeChanged && !mapChunk.SuppressCollisionRegeneration) @@ -1683,6 +1683,15 @@ private void OnTileModified(EntityUid uid, MapGridComponent grid, MapChunk mapCh } } + internal void RaiseOnTileChanged(Entity entity, TileRef tileRef, Tile oldTile, Vector2i chunk) + { + if (SuppressOnTileChanged) + return; + + var ev = new TileChangedEvent(entity, tileRef, oldTile, chunk); + EntityManager.EventBus.RaiseLocalEvent(entity.Owner, ref ev, true); + } + /// /// Iterates the local tiles of the specified data. /// diff --git a/Robust.Shared/GameObjects/Systems/SharedMapSystem.cs b/Robust.Shared/GameObjects/Systems/SharedMapSystem.cs index 16e07f3d50f..94486e8077e 100644 --- a/Robust.Shared/GameObjects/Systems/SharedMapSystem.cs +++ b/Robust.Shared/GameObjects/Systems/SharedMapSystem.cs @@ -40,6 +40,14 @@ public abstract partial class SharedMapSystem : EntitySystem internal Dictionary Maps { get; } = new(); + /// + /// If set, this prevents the from being raised when modifying grids. + /// + /// + /// Useful if you want to create a new grid, delete an existing grid, or bulk-modify tiles and don't want to spam ten billion individual tile-changed events. + /// + internal bool SuppressOnTileChanged { get; set; } + /// /// This hashset is used to try prevent MapId re-use. This is mainly for auto-assigned map ids. /// Loading a map with a specific id (e.g., the various mapping commands) may still result in an id being diff --git a/Robust.Shared/Map/IMapManager.cs b/Robust.Shared/Map/IMapManager.cs index d294b2d8d03..c3cd767a8e2 100644 --- a/Robust.Shared/Map/IMapManager.cs +++ b/Robust.Shared/Map/IMapManager.cs @@ -28,6 +28,7 @@ public interface IMapManager /// Should the OnTileChanged event be suppressed? This is useful for initially loading the map /// so that you don't spam an event for each of the million station tiles. /// + [Obsolete("use SharedMapSystem")] bool SuppressOnTileChanged { get; set; } /// diff --git a/Robust.Shared/Map/IMapManagerInternal.cs b/Robust.Shared/Map/IMapManagerInternal.cs index e6a5b5b3637..bbc3debc343 100644 --- a/Robust.Shared/Map/IMapManagerInternal.cs +++ b/Robust.Shared/Map/IMapManagerInternal.cs @@ -1,3 +1,4 @@ +using System; using Robust.Shared.GameObjects; using Robust.Shared.Map.Components; using Robust.Shared.Maths; @@ -12,6 +13,7 @@ internal interface IMapManagerInternal : IMapManager /// /// A reference to the new tile. /// The old tile that got replaced. + [Obsolete("use SharedMapSystem")] void RaiseOnTileChanged(Entity entity, TileRef tileRef, Tile oldTile, Vector2i chunk); } } diff --git a/Robust.Shared/Map/MapManager.GridCollection.cs b/Robust.Shared/Map/MapManager.GridCollection.cs index 96f89e7547b..a1034c9f3e4 100644 --- a/Robust.Shared/Map/MapManager.GridCollection.cs +++ b/Robust.Shared/Map/MapManager.GridCollection.cs @@ -80,19 +80,21 @@ public virtual void DeleteGrid(EntityUid euid) } /// - public bool SuppressOnTileChanged { get; set; } + [Obsolete("use SharedMapSystem.SuppressOnTileChanged")] + public bool SuppressOnTileChanged + { + get => _mapSystem.SuppressOnTileChanged; + set { _mapSystem.SuppressOnTileChanged = value; } + } /// /// Raises the OnTileChanged event. /// /// A reference to the new tile. /// The old tile that got replaced. + [Obsolete("use SharedMapSystem.RaiseOnTileChanged")] void IMapManagerInternal.RaiseOnTileChanged(Entity entity, TileRef tileRef, Tile oldTile, Vector2i chunk) { - if (SuppressOnTileChanged) - return; - - var ev = new TileChangedEvent(entity, tileRef, oldTile, chunk); - EntityManager.EventBus.RaiseLocalEvent(entity.Owner, ref ev, true); + _mapSystem.RaiseOnTileChanged(entity, tileRef, oldTile, chunk); } } From 541975bcb79a14591d9ee0a794a9adbddcbb9589 Mon Sep 17 00:00:00 2001 From: TemporalOroboros Date: Tue, 19 May 2026 02:03:22 -0700 Subject: [PATCH 04/13] Move default value constants to SharedMapSystem --- .../Systems/SharedMapSystem.Lookup.cs | 83 +++++++++++-------- 1 file changed, 47 insertions(+), 36 deletions(-) diff --git a/Robust.Shared/GameObjects/Systems/SharedMapSystem.Lookup.cs b/Robust.Shared/GameObjects/Systems/SharedMapSystem.Lookup.cs index 1c38c60aee0..39ed8e8109c 100644 --- a/Robust.Shared/GameObjects/Systems/SharedMapSystem.Lookup.cs +++ b/Robust.Shared/GameObjects/Systems/SharedMapSystem.Lookup.cs @@ -14,6 +14,17 @@ namespace Robust.Shared.GameObjects; public abstract partial class SharedMapSystem { + + /// + /// Whether and its extended family should only approximately check for intersection by default. + /// + public const bool Approximate = false; + + /// + /// Whether and its extended family should also check the map itself by default. + /// + public const bool IncludeMap = true; + #region TryFindGridAt public bool TryFindGridAt(EntityUid mapEnt, Vector2 worldPos, out EntityUid uid, [NotNullWhen(true)] out MapGridComponent? grid) @@ -97,8 +108,8 @@ public void FindGridsIntersecting( T shape, Transform transform, ref List> grids, - bool approx = IMapManager.Approximate, - bool includeMap = IMapManager.IncludeMap) where T : IPhysShape + bool approx = Approximate, + bool includeMap = IncludeMap) where T : IPhysShape { if (TryGetMap(mapId, out var mapEnt)) FindGridsIntersecting(mapEnt.Value, shape, transform, ref grids, approx: approx, includeMap: includeMap); @@ -109,8 +120,8 @@ public void FindGridsIntersecting( T shape, Transform transform, GridCallback callback, - bool approx = IMapManager.Approximate, - bool includeMap = IMapManager.IncludeMap) where T : IPhysShape + bool approx = Approximate, + bool includeMap = IncludeMap) where T : IPhysShape { if (TryGetMap(mapId, out var mapEnt)) FindGridsIntersecting(mapEnt.Value, shape, transform, callback, approx: approx, includeMap: includeMap); @@ -120,8 +131,8 @@ public void FindGridsIntersecting( MapId mapId, Box2 worldAABB, GridCallback callback, - bool approx = IMapManager.Approximate, - bool includeMap = IMapManager.IncludeMap) + bool approx = Approximate, + bool includeMap = IncludeMap) { if (TryGetMap(mapId, out var mapEnt)) FindGridsIntersecting(mapEnt.Value, worldAABB, callback, approx: approx, includeMap: includeMap); @@ -132,8 +143,8 @@ public void FindGridsIntersecting( Box2 worldAABB, ref TState state, GridCallback callback, - bool approx = IMapManager.Approximate, - bool includeMap = IMapManager.IncludeMap) + bool approx = Approximate, + bool includeMap = IncludeMap) { if (TryGetMap(mapId, out var map)) FindGridsIntersecting(map.Value, worldAABB, ref state, callback, approx: approx, includeMap: includeMap); @@ -143,8 +154,8 @@ public void FindGridsIntersecting( MapId mapId, Box2 worldAABB, ref List> grids, - bool approx = IMapManager.Approximate, - bool includeMap = IMapManager.IncludeMap) + bool approx = Approximate, + bool includeMap = IncludeMap) { if (TryGetMap(mapId, out var map)) FindGridsIntersecting(map.Value, worldAABB, ref grids, approx: approx, includeMap: includeMap); @@ -154,8 +165,8 @@ public void FindGridsIntersecting( MapId mapId, Box2Rotated worldBounds, GridCallback callback, - bool approx = IMapManager.Approximate, - bool includeMap = IMapManager.IncludeMap) + bool approx = Approximate, + bool includeMap = IncludeMap) { if (TryGetMap(mapId, out var mapEnt)) FindGridsIntersecting(mapEnt.Value, worldBounds, callback, approx: approx, includeMap: includeMap); @@ -166,8 +177,8 @@ public void FindGridsIntersecting( Box2Rotated worldBounds, ref TState state, GridCallback callback, - bool approx = IMapManager.Approximate, - bool includeMap = IMapManager.IncludeMap) + bool approx = Approximate, + bool includeMap = IncludeMap) { if (TryGetMap(mapId, out var mapEnt)) FindGridsIntersecting(mapEnt.Value, worldBounds, ref state, callback, approx: approx, includeMap: includeMap); @@ -177,8 +188,8 @@ public void FindGridsIntersecting( MapId mapId, Box2Rotated worldBounds, ref List> grids, - bool approx = IMapManager.Approximate, - bool includeMap = IMapManager.IncludeMap) + bool approx = Approximate, + bool includeMap = IncludeMap) { if (TryGetMap(mapId, out var mapEnt)) FindGridsIntersecting(mapEnt.Value, worldBounds, ref grids, approx: approx, includeMap: includeMap); @@ -193,8 +204,8 @@ public void FindGridsIntersecting( TShape shape, Transform transform, GridCallback callback, - bool approx = false, - bool includeMap = true) where TShape : IPhysShape + bool approx = Approximate, + bool includeMap = IncludeMap) where TShape : IPhysShape { FindGridsIntersecting(mapEnt, shape, shape.ComputeAABB(transform, 0), transform, callback, approx: approx, includeMap: includeMap); } @@ -205,8 +216,8 @@ public void FindGridsIntersecting( Transform transform, ref TState state, GridCallback callback, - bool approx = false, - bool includeMap = true) where TShape : IPhysShape + bool approx = Approximate, + bool includeMap = IncludeMap) where TShape : IPhysShape { FindGridsIntersecting(mapEnt, shape, shape.ComputeAABB(transform, 0), transform, ref state, callback, approx: approx, includeMap: includeMap); } @@ -216,8 +227,8 @@ public void FindGridsIntersecting( List shapes, Transform transform, ref List> entities, - bool approx = false, - bool includeMap = true) + bool approx = Approximate, + bool includeMap = IncludeMap) { foreach (var shape in shapes) { @@ -230,8 +241,8 @@ public void FindGridsIntersecting( TShape shape, Transform transform, ref List> grids, - bool approx = false, - bool includeMap = true) where TShape : IPhysShape + bool approx = Approximate, + bool includeMap = IncludeMap) where TShape : IPhysShape { FindGridsIntersecting(mapEnt, shape, shape.ComputeAABB(transform, 0), transform, ref grids, approx: approx, includeMap: includeMap); } @@ -240,8 +251,8 @@ public void FindGridsIntersecting( EntityUid mapEnt, Box2 worldAABB, GridCallback callback, - bool approx = false, - bool includeMap = true) + bool approx = Approximate, + bool includeMap = IncludeMap) { var shape = new SlimPolygon(worldAABB); FindGridsIntersecting(mapEnt, shape, worldAABB, Robust.Shared.Physics.Transform.Empty, callback, approx: approx, includeMap: includeMap); @@ -252,8 +263,8 @@ public void FindGridsIntersecting( Box2 worldAABB, ref TState state, GridCallback callback, - bool approx = false, - bool includeMap = true) + bool approx = Approximate, + bool includeMap = IncludeMap) { var shape = new SlimPolygon(worldAABB); FindGridsIntersecting(mapEnt, shape, worldAABB, Robust.Shared.Physics.Transform.Empty, ref state, callback, approx: approx, includeMap: includeMap); @@ -263,8 +274,8 @@ public void FindGridsIntersecting( EntityUid mapEnt, Box2 worldAABB, ref List> grids, - bool approx = false, - bool includeMap = true) + bool approx = Approximate, + bool includeMap = IncludeMap) { var shape = new SlimPolygon(worldAABB); FindGridsIntersecting(mapEnt, shape, worldAABB, Robust.Shared.Physics.Transform.Empty, ref grids, approx: approx, includeMap: includeMap); @@ -274,8 +285,8 @@ public void FindGridsIntersecting( EntityUid mapEnt, Box2Rotated worldBounds, GridCallback callback, - bool approx = false, - bool includeMap = true) + bool approx = Approximate, + bool includeMap = IncludeMap) { var shape = new SlimPolygon(worldBounds); FindGridsIntersecting(mapEnt, shape, Robust.Shared.Physics.Transform.Empty, callback, approx: approx, includeMap: includeMap); @@ -286,8 +297,8 @@ public void FindGridsIntersecting( Box2Rotated worldBounds, ref TState state, GridCallback callback, - bool approx = false, - bool includeMap = true) + bool approx = Approximate, + bool includeMap = IncludeMap) { var shape = new SlimPolygon(worldBounds); FindGridsIntersecting(mapEnt, shape, Robust.Shared.Physics.Transform.Empty, ref state, callback, approx: approx, includeMap: includeMap); @@ -297,8 +308,8 @@ public void FindGridsIntersecting( EntityUid mapEnt, Box2Rotated worldBounds, ref List> grids, - bool approx = false, - bool includeMap = true) + bool approx = Approximate, + bool includeMap = IncludeMap) { var shape = new SlimPolygon(worldBounds); FindGridsIntersecting(mapEnt, shape, Robust.Shared.Physics.Transform.Empty, ref grids, approx: approx, includeMap: includeMap); From 41bbaa9eaaa8be80499449907bbf768d3026ee67 Mon Sep 17 00:00:00 2001 From: TemporalOroboros Date: Tue, 19 May 2026 02:22:30 -0700 Subject: [PATCH 05/13] Converts map pausing events into LocalizedEntityCommands --- Resources/Locale/en-US/commands.ftl | 9 +++ .../Map/Commands/MapPausingCommands.cs | 81 +++++++++++++++++++ Robust.Shared/Map/MapManager.Pause.cs | 66 --------------- Robust.Shared/Map/MapManager.cs | 13 --- 4 files changed, 90 insertions(+), 79 deletions(-) create mode 100644 Robust.Shared/Map/Commands/MapPausingCommands.cs diff --git a/Resources/Locale/en-US/commands.ftl b/Resources/Locale/en-US/commands.ftl index 4d4e3264618..fcafc42eedf 100644 --- a/Resources/Locale/en-US/commands.ftl +++ b/Resources/Locale/en-US/commands.ftl @@ -306,6 +306,15 @@ cmd-addmap-help = Usage: {$command} [pre-init] cmd-rmmap-desc = Removes a map from the world. You cannot remove nullspace. cmd-rmmap-help = Usage: {$command} +cmd-pausemap-desc = Pauses a map, pausing all simulation processing on it. +cmd-pausemap-help = Usage: pausemap + +cmd-unpausemap-desc = Unpauses a map, resuming all simulation processing on it. +cmd-unpausemap-help = Usage: unpausemap + +cmd-querymappaused-desc = Check whether a map is paused or not. +cmd-querymappaused-help = Usage: querymappaused + cmd-savegrid-desc = Serializes a grid to disk. cmd-savegrid-help = Usage: {$command} diff --git a/Robust.Shared/Map/Commands/MapPausingCommands.cs b/Robust.Shared/Map/Commands/MapPausingCommands.cs new file mode 100644 index 00000000000..6f2a2a80538 --- /dev/null +++ b/Robust.Shared/Map/Commands/MapPausingCommands.cs @@ -0,0 +1,81 @@ +using System.Globalization; +using Robust.Shared.Console; +using Robust.Shared.GameObjects; +using Robust.Shared.IoC; + +namespace Robust.Shared.Map.Commands; + +public sealed partial class PauseMapCommand : LocalizedEntityCommands +{ + [Dependency] SharedMapSystem _mapSystem = default!; + public override string Command => "pausemap"; + + public override void Execute(IConsoleShell shell, string argStr, string[] args) + { + if (args.Length != 1) + { + shell.WriteError("Need to supply a valid MapId"); + return; + } + + var mapId = new MapId(int.Parse(args[0], CultureInfo.InvariantCulture)); + + if (!_mapSystem.MapExists(mapId)) + { + shell.WriteError("That map does not exist."); + return; + } + + _mapSystem.SetPaused(mapId, true); + } +} + +public sealed partial class UnpauseMapCommand : LocalizedEntityCommands +{ + [Dependency] SharedMapSystem _mapSystem = default!; + public override string Command => "unpausemap"; + + public override void Execute(IConsoleShell shell, string argStr, string[] args) + { + if (args.Length != 1) + { + shell.WriteError("Need to supply a valid MapId"); + return; + } + + var mapId = new MapId(int.Parse(args[0], CultureInfo.InvariantCulture)); + + if (!_mapSystem.MapExists(mapId)) + { + shell.WriteError("That map does not exist."); + return; + } + + _mapSystem.SetPaused(mapId, false); + } +} + +public sealed partial class QueryMapPausedCommand : LocalizedEntityCommands +{ + [Dependency] SharedMapSystem _mapSystem = default!; + public override string Command => "querymappaused"; + + public override void Execute(IConsoleShell shell, string argStr, string[] args) + { + if (args.Length != 1) + { + shell.WriteError("Need to supply a valid MapId"); + return; + } + + var mapId = new MapId(int.Parse(args[0], CultureInfo.InvariantCulture)); + + if (!_mapSystem.MapExists(mapId)) + { + shell.WriteError("That map does not exist."); + return; + } + + shell.WriteLine(_mapSystem.IsPaused(mapId).ToString()); + } +} diff --git a/Robust.Shared/Map/MapManager.Pause.cs b/Robust.Shared/Map/MapManager.Pause.cs index d4fb8f58976..49a76ec5eaf 100644 --- a/Robust.Shared/Map/MapManager.Pause.cs +++ b/Robust.Shared/Map/MapManager.Pause.cs @@ -36,71 +36,5 @@ public bool IsMapPaused(EntityUid uid) { return _mapSystem.IsPaused(uid); } - - /// - /// Initializes the map pausing system. - /// - private void InitializeMapPausing() - { - _conhost.RegisterCommand("pausemap", - "Pauses a map, pausing all simulation processing on it.", - "pausemap ", - (shell, _, args) => - { - if (args.Length != 1) - { - shell.WriteError("Need to supply a valid MapId"); - return; - } - - var mapId = new MapId(int.Parse(args[0], CultureInfo.InvariantCulture)); - - if (!MapExists(mapId)) - { - shell.WriteError("That map does not exist."); - return; - } - - SetMapPaused(mapId, true); - }); - - _conhost.RegisterCommand("querymappaused", - "Check whether a map is paused or not.", - "querymappaused ", - (shell, _, args) => - { - var mapId = new MapId(int.Parse(args[0], CultureInfo.InvariantCulture)); - - if (!MapExists(mapId)) - { - shell.WriteError("That map does not exist."); - return; - } - - shell.WriteLine(_mapSystem.IsPaused(mapId).ToString()); - }); - - _conhost.RegisterCommand("unpausemap", - "unpauses a map, resuming all simulation processing on it.", - "Usage: unpausemap ", - (shell, _, args) => - { - if (args.Length != 1) - { - shell.WriteLine("Need to supply a valid MapId"); - return; - } - - var mapId = new MapId(int.Parse(args[0], CultureInfo.InvariantCulture)); - - if (!MapExists(mapId)) - { - shell.WriteLine("That map does not exist."); - return; - } - - SetMapPaused(mapId, false); - }); - } } } diff --git a/Robust.Shared/Map/MapManager.cs b/Robust.Shared/Map/MapManager.cs index c5e853c8b63..9b4f1b753b0 100644 --- a/Robust.Shared/Map/MapManager.cs +++ b/Robust.Shared/Map/MapManager.cs @@ -13,35 +13,22 @@ namespace Robust.Shared.Map; [Virtual] internal partial class MapManager : IMapManagerInternal, IEntityEventSubscriber { - [Dependency] public IGameTiming GameTiming = default!; [Dependency] public IEntityManager EntityManager = default!; - [Dependency] private IManifoldManager _manifolds = default!; [Dependency] private ILogManager _logManager = default!; - [Dependency] private IConsoleHost _conhost = default!; private ISawmill _sawmill = default!; private SharedMapSystem _mapSystem = default!; - private SharedPhysicsSystem _physics = default!; - private SharedTransformSystem _transformSystem = default!; - - private EntityQuery _gridTreeQuery; - private EntityQuery _gridQuery; /// public void Initialize() { - _gridTreeQuery = EntityManager.GetEntityQuery(); - _gridQuery = EntityManager.GetEntityQuery(); - InitializeMapPausing(); _sawmill = _logManager.GetSawmill("system.map"); } /// public void Startup() { - _physics = EntityManager.System(); - _transformSystem = EntityManager.System(); _mapSystem = EntityManager.System(); _sawmill.Debug("Starting..."); From bd890ffe3fee20f17573906c4c70ba6c5fc4a14c Mon Sep 17 00:00:00 2001 From: TemporalOroboros Date: Tue, 19 May 2026 02:30:59 -0700 Subject: [PATCH 06/13] Move MapManager related delegates/structs to SharedMapSystem Moves the GridCreationOptions struct to the same namespace as SharedMapSystem and converts it into a record struct Move the GridCallback delegates to the same namespace as SharedMapSystem --- .../Systems/SharedMapSystem.Grid.cs | 5 +++++ .../Systems/SharedMapSystem.Lookup.cs | 3 +++ Robust.Shared/Map/IMapManager.cs | 20 ++----------------- 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.cs b/Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.cs index 48b45568dbf..3511f0bea14 100644 --- a/Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.cs +++ b/Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.cs @@ -1787,3 +1787,8 @@ public bool MoveNext(out TileRef tile) } } } + +public record struct GridCreateOptions(ushort ChunkSize) +{ + public readonly static GridCreateOptions Default = new(ChunkSize: 16); +} diff --git a/Robust.Shared/GameObjects/Systems/SharedMapSystem.Lookup.cs b/Robust.Shared/GameObjects/Systems/SharedMapSystem.Lookup.cs index 39ed8e8109c..d00cb55ea07 100644 --- a/Robust.Shared/GameObjects/Systems/SharedMapSystem.Lookup.cs +++ b/Robust.Shared/GameObjects/Systems/SharedMapSystem.Lookup.cs @@ -473,3 +473,6 @@ private record struct GridQueryState( bool Approximate ) where TShape : IPhysShape; } + +public delegate bool GridCallback(EntityUid gridUid, MapGridComponent gridComp); +public delegate bool GridCallback(EntityUid gridUid, MapGridComponent gridComp, ref TState state); diff --git a/Robust.Shared/Map/IMapManager.cs b/Robust.Shared/Map/IMapManager.cs index c3cd767a8e2..fad26a165af 100644 --- a/Robust.Shared/Map/IMapManager.cs +++ b/Robust.Shared/Map/IMapManager.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Numerics; -using JetBrains.Annotations; using Robust.Shared.GameObjects; using Robust.Shared.Map.Components; using Robust.Shared.Maths; @@ -11,18 +10,14 @@ namespace Robust.Shared.Map { - public delegate bool GridCallback(EntityUid uid, MapGridComponent grid); - - public delegate bool GridCallback(EntityUid uid, MapGridComponent grid, ref TState state); - /// /// This manages all the grids and maps in the world. Largely superseded by . /// [NotContentImplementable] public interface IMapManager { - public const bool Approximate = false; - public const bool IncludeMap = true; + public const bool Approximate = SharedMapSystem.Approximate; + public const bool IncludeMap = SharedMapSystem.IncludeMap; /// /// Should the OnTileChanged event be suppressed? This is useful for initially loading the map @@ -260,16 +255,5 @@ public IEnumerable FindGridsIntersecting(MapId mapId, Box2Rota [Obsolete("Use MapSystem")] bool IsMapInitialized(MapId mapId); - - } - - public struct GridCreateOptions - { - public static readonly GridCreateOptions Default = new() - { - ChunkSize = 16 - }; - - public ushort ChunkSize; } } From cc10ab40773886e0bf351a98badc030a125a89d5 Mon Sep 17 00:00:00 2001 From: TemporalOroboros Date: Tue, 19 May 2026 02:42:46 -0700 Subject: [PATCH 07/13] Move CullDeletionHistory to SharedMapSystem Well, that was less painful than I thought it would be --- Robust.Server/GameStates/PvsSystem.Chunks.cs | 2 +- Robust.Server/GameStates/PvsSystem.cs | 4 ++-- .../GameObjects/Systems/SharedMapSystem.Grid.cs | 11 +++++++++++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/Robust.Server/GameStates/PvsSystem.Chunks.cs b/Robust.Server/GameStates/PvsSystem.Chunks.cs index 99466b1f6b7..a270c7410b7 100644 --- a/Robust.Server/GameStates/PvsSystem.Chunks.cs +++ b/Robust.Server/GameStates/PvsSystem.Chunks.cs @@ -133,7 +133,7 @@ private void GetVisibleChunks(Entity eye, _grids.Clear(); var rangeVec = new Vector2(range, range); var box = new Box2(viewPos - rangeVec, viewPos + rangeVec); - _mapManager.FindGridsIntersecting(map, box, ref _grids, approx: true, includeMap: false); + _maps.FindGridsIntersecting(map, box, ref _grids, approx: true, includeMap: false); foreach (var (grid, _) in _grids) { diff --git a/Robust.Server/GameStates/PvsSystem.cs b/Robust.Server/GameStates/PvsSystem.cs index 2095023f220..02615e9ea73 100644 --- a/Robust.Server/GameStates/PvsSystem.cs +++ b/Robust.Server/GameStates/PvsSystem.cs @@ -28,7 +28,6 @@ namespace Robust.Server.GameStates; internal sealed partial class PvsSystem : EntitySystem { [Dependency] private IConfigurationManager _configManager = default!; - [Dependency] private INetworkedMapManager _mapManager = default!; [Dependency] private IServerEntityNetworkManager _netEntMan = default!; [Dependency] private IPlayerManager _playerManager = default!; [Dependency] private IParallelManager _parallelManager = default!; @@ -40,6 +39,7 @@ internal sealed partial class PvsSystem : EntitySystem [Dependency] private IParallelManagerInternal _parallelMgr = default!; [Dependency] private PvsOverrideSystem _pvsOverride = default!; [Dependency] private IServerReplayRecordingManager _replay = default!; + [Dependency] private SharedMapSystem _maps = default!; // TODO make this a cvar. Make it in terms of seconds and tie it to tick rate? // Main issue is that I CBF figuring out the logic for handling it changing mid-game. @@ -288,7 +288,7 @@ private void CullDeletionHistory(GameTick oldestAck) { using var _ = Histogram.WithLabels("Cull History").NewTimer(); CullDeletionHistoryUntil(oldestAck); - _mapManager.CullDeletionHistory(oldestAck); + _maps.CullDeletionHistory(oldestAck); } private void GetEntityStates(PvsSession session) diff --git a/Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.cs b/Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.cs index 3511f0bea14..6de54c1b019 100644 --- a/Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.cs +++ b/Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.cs @@ -524,6 +524,17 @@ private void GetFullState(EntityUid uid, MapGridComponent component, ref Compone #endif } + public void CullDeletionHistory(GameTick upToTick) + { + var query = AllEntityQuery(); + + while (query.MoveNext(out var grid)) + { + var chunks = grid.ChunkDeletionHistory; + chunks.RemoveAll(t => t.tick < upToTick); + } + } + private void OnGridAdd(EntityUid uid, MapGridComponent component, ComponentAdd args) { var msg = new GridAddEvent(uid); From 7b0e32055216febe4a83ab37fb17227ee3b5cd65 Mon Sep 17 00:00:00 2001 From: TemporalOroboros Date: Tue, 19 May 2026 02:50:06 -0700 Subject: [PATCH 08/13] Actually obsolete the NetworkedMapManager method --- Robust.Shared/Map/IMapManagerInternal.cs | 1 + .../Map/MapManager.GridCollection.cs | 14 +++--- Robust.Shared/Map/MapManager.MapCollection.cs | 16 +++---- Robust.Shared/Map/MapManager.Pause.cs | 12 ++--- Robust.Shared/Map/MapManager.Queries.cs | 44 +++++++++---------- Robust.Shared/Map/MapManager.cs | 8 +--- Robust.Shared/Map/NetworkedMapManager.cs | 14 +++--- 7 files changed, 52 insertions(+), 57 deletions(-) diff --git a/Robust.Shared/Map/IMapManagerInternal.cs b/Robust.Shared/Map/IMapManagerInternal.cs index bbc3debc343..c2b19a20f21 100644 --- a/Robust.Shared/Map/IMapManagerInternal.cs +++ b/Robust.Shared/Map/IMapManagerInternal.cs @@ -6,6 +6,7 @@ namespace Robust.Shared.Map { /// + [Obsolete] internal interface IMapManagerInternal : IMapManager { /// diff --git a/Robust.Shared/Map/MapManager.GridCollection.cs b/Robust.Shared/Map/MapManager.GridCollection.cs index a1034c9f3e4..75502418251 100644 --- a/Robust.Shared/Map/MapManager.GridCollection.cs +++ b/Robust.Shared/Map/MapManager.GridCollection.cs @@ -30,13 +30,13 @@ public MapGridComponent CreateGrid(MapId currentMapId) [Obsolete("use SharedMapSystem.CreateGridEntity")] public Entity CreateGridEntity(MapId currentMapId, GridCreateOptions? options = null) { - return _mapSystem.CreateGridEntity(currentMapId, options: options); + return MapSystem.CreateGridEntity(currentMapId, options: options); } [Obsolete("use SharedMapSystem.CreateGridEntity")] public Entity CreateGridEntity(EntityUid map, GridCreateOptions? options = null) { - return _mapSystem.CreateGridEntity(map, options: options); + return MapSystem.CreateGridEntity(map, options: options); } [Obsolete("Use HasComponent(uid)")] @@ -48,13 +48,13 @@ public bool IsGrid(EntityUid uid) [Obsolete("use SharedMapSystem.GetAllMapGrids")] public IEnumerable GetAllMapGrids(MapId mapId) { - return _mapSystem.GetAllMapGrids(mapId); + return MapSystem.GetAllMapGrids(mapId); } [Obsolete("use SharedMapSystem.GetAllGrids")] public IEnumerable> GetAllGrids(MapId mapId) { - return _mapSystem.GetAllGrids(mapId); + return MapSystem.GetAllGrids(mapId); } [Obsolete("just delete the grid entity")] @@ -83,8 +83,8 @@ public virtual void DeleteGrid(EntityUid euid) [Obsolete("use SharedMapSystem.SuppressOnTileChanged")] public bool SuppressOnTileChanged { - get => _mapSystem.SuppressOnTileChanged; - set { _mapSystem.SuppressOnTileChanged = value; } + get => MapSystem.SuppressOnTileChanged; + set { MapSystem.SuppressOnTileChanged = value; } } /// @@ -95,6 +95,6 @@ public bool SuppressOnTileChanged [Obsolete("use SharedMapSystem.RaiseOnTileChanged")] void IMapManagerInternal.RaiseOnTileChanged(Entity entity, TileRef tileRef, Tile oldTile, Vector2i chunk) { - _mapSystem.RaiseOnTileChanged(entity, tileRef, oldTile, chunk); + MapSystem.RaiseOnTileChanged(entity, tileRef, oldTile, chunk); } } diff --git a/Robust.Shared/Map/MapManager.MapCollection.cs b/Robust.Shared/Map/MapManager.MapCollection.cs index e8a5f44e181..3810652f9a4 100644 --- a/Robust.Shared/Map/MapManager.MapCollection.cs +++ b/Robust.Shared/Map/MapManager.MapCollection.cs @@ -30,7 +30,7 @@ internal partial class MapManager /// public virtual void DeleteMap(MapId mapId) { - _mapSystem.DeleteMap(mapId); + MapSystem.DeleteMap(mapId); } /// @@ -38,24 +38,24 @@ public MapId CreateMap(MapId? mapId = null) { if (mapId != null) { - _mapSystem.CreateMap(mapId.Value); + MapSystem.CreateMap(mapId.Value); return mapId.Value; } - _mapSystem.CreateMap(out var map); + MapSystem.CreateMap(out var map); return map; } /// public bool MapExists([NotNullWhen(true)] MapId? mapId) { - return _mapSystem.MapExists(mapId); + return MapSystem.MapExists(mapId); } /// public EntityUid GetMapEntityId(MapId mapId) { - return _mapSystem.GetMapOrInvalid(mapId); + return MapSystem.GetMapOrInvalid(mapId); } /// @@ -63,18 +63,18 @@ public EntityUid GetMapEntityId(MapId mapId) /// public EntityUid GetMapEntityIdOrThrow(MapId mapId) { - return _mapSystem.GetMap(mapId); + return MapSystem.GetMap(mapId); } public bool TryGetMap([NotNullWhen(true)] MapId? mapId, [NotNullWhen(true)] out EntityUid? uid) { - return _mapSystem.TryGetMap(mapId, out uid); + return MapSystem.TryGetMap(mapId, out uid); } /// public IEnumerable GetAllMapIds() { - return _mapSystem.GetAllMapIds(); + return MapSystem.GetAllMapIds(); } /// diff --git a/Robust.Shared/Map/MapManager.Pause.cs b/Robust.Shared/Map/MapManager.Pause.cs index 49a76ec5eaf..35415537c4a 100644 --- a/Robust.Shared/Map/MapManager.Pause.cs +++ b/Robust.Shared/Map/MapManager.Pause.cs @@ -7,34 +7,34 @@ internal partial class MapManager { public void SetMapPaused(MapId mapId, bool paused) { - _mapSystem.SetPaused(mapId, paused); + MapSystem.SetPaused(mapId, paused); } public void SetMapPaused(EntityUid uid, bool paused) { - _mapSystem.SetPaused(uid, paused); + MapSystem.SetPaused(uid, paused); } public void DoMapInitialize(MapId mapId) { - _mapSystem.InitializeMap(mapId); + MapSystem.InitializeMap(mapId); } public bool IsMapInitialized(MapId mapId) { - return _mapSystem.IsInitialized(mapId); + return MapSystem.IsInitialized(mapId); } /// public bool IsMapPaused(MapId mapId) { - return _mapSystem.IsPaused(mapId); + return MapSystem.IsPaused(mapId); } /// public bool IsMapPaused(EntityUid uid) { - return _mapSystem.IsPaused(uid); + return MapSystem.IsPaused(uid); } } } diff --git a/Robust.Shared/Map/MapManager.Queries.cs b/Robust.Shared/Map/MapManager.Queries.cs index 0799927e63f..8916fa76d84 100644 --- a/Robust.Shared/Map/MapManager.Queries.cs +++ b/Robust.Shared/Map/MapManager.Queries.cs @@ -18,53 +18,53 @@ internal partial class MapManager public void FindGridsIntersecting(MapId mapId, T shape, Transform transform, ref List> grids, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) where T : IPhysShape { - _mapSystem.FindGridsIntersecting(mapId, shape, transform, ref grids, approx: approx, includeMap: includeMap); + MapSystem.FindGridsIntersecting(mapId, shape, transform, ref grids, approx: approx, includeMap: includeMap); } [Obsolete("use SharedMapSystem")] public void FindGridsIntersecting(MapId mapId, T shape, Transform transform, GridCallback callback, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) where T : IPhysShape { - _mapSystem.FindGridsIntersecting(mapId, shape, transform, callback, approx: approx, includeMap: includeMap); + MapSystem.FindGridsIntersecting(mapId, shape, transform, callback, approx: approx, includeMap: includeMap); } [Obsolete("use SharedMapSystem")] public void FindGridsIntersecting(MapId mapId, Box2 worldAABB, GridCallback callback, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) { - _mapSystem.FindGridsIntersecting(mapId, worldAABB, callback, approx: approx, includeMap: includeMap); + MapSystem.FindGridsIntersecting(mapId, worldAABB, callback, approx: approx, includeMap: includeMap); } [Obsolete("use SharedMapSystem")] public void FindGridsIntersecting(MapId mapId, Box2 worldAABB, ref TState state, GridCallback callback, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) { - _mapSystem.FindGridsIntersecting(mapId, worldAABB, ref state, callback, approx: approx, includeMap: includeMap); + MapSystem.FindGridsIntersecting(mapId, worldAABB, ref state, callback, approx: approx, includeMap: includeMap); } [Obsolete("use SharedMapSystem")] public void FindGridsIntersecting(MapId mapId, Box2 worldAABB, ref List> grids, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) { - _mapSystem.FindGridsIntersecting(mapId, worldAABB, ref grids, approx: approx, includeMap: includeMap); + MapSystem.FindGridsIntersecting(mapId, worldAABB, ref grids, approx: approx, includeMap: includeMap); } [Obsolete("use SharedMapSystem")] public void FindGridsIntersecting(MapId mapId, Box2Rotated worldBounds, GridCallback callback, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) { - _mapSystem.FindGridsIntersecting(mapId, worldBounds, callback, approx: approx, includeMap: includeMap); + MapSystem.FindGridsIntersecting(mapId, worldBounds, callback, approx: approx, includeMap: includeMap); } [Obsolete("use SharedMapSystem")] public void FindGridsIntersecting(MapId mapId, Box2Rotated worldBounds, ref TState state, GridCallback callback, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) { - _mapSystem.FindGridsIntersecting(mapId, worldBounds, ref state, callback, approx: approx, includeMap: includeMap); + MapSystem.FindGridsIntersecting(mapId, worldBounds, ref state, callback, approx: approx, includeMap: includeMap); } [Obsolete("use SharedMapSystem")] public void FindGridsIntersecting(MapId mapId, Box2Rotated worldBounds, ref List> grids, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) { - _mapSystem.FindGridsIntersecting(mapId, worldBounds, ref grids, approx: approx, includeMap: includeMap); + MapSystem.FindGridsIntersecting(mapId, worldBounds, ref grids, approx: approx, includeMap: includeMap); } #endregion @@ -80,7 +80,7 @@ public void FindGridsIntersecting( bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) where T : IPhysShape { - _mapSystem.FindGridsIntersecting(mapEnt, shape, transform, callback, approx: approx, includeMap: includeMap); + MapSystem.FindGridsIntersecting(mapEnt, shape, transform, callback, approx: approx, includeMap: includeMap); } [Obsolete("use SharedMapSystem")] @@ -93,7 +93,7 @@ public void FindGridsIntersecting( bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) where T : IPhysShape { - _mapSystem.FindGridsIntersecting(mapEnt, shape, transform, ref state, callback, approx: approx, includeMap: includeMap); + MapSystem.FindGridsIntersecting(mapEnt, shape, transform, ref state, callback, approx: approx, includeMap: includeMap); } /// @@ -102,61 +102,61 @@ public void FindGridsIntersecting( [Obsolete("use SharedMapSystem")] public void FindGridsIntersecting(EntityUid mapEnt, List shapes, Transform transform, ref List> entities, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) { - _mapSystem.FindGridsIntersecting(mapEnt, shapes, transform, ref entities, approx: approx, includeMap: includeMap); + MapSystem.FindGridsIntersecting(mapEnt, shapes, transform, ref entities, approx: approx, includeMap: includeMap); } [Obsolete("use SharedMapSystem")] public void FindGridsIntersecting(EntityUid mapEnt, T shape, Transform transform, ref List> grids, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) where T : IPhysShape { - _mapSystem.FindGridsIntersecting(mapEnt, shape, transform, ref grids, approx: approx, includeMap: includeMap); + MapSystem.FindGridsIntersecting(mapEnt, shape, transform, ref grids, approx: approx, includeMap: includeMap); } [Obsolete("use SharedMapSystem")] public void FindGridsIntersecting(EntityUid mapEnt, T shape, Box2 worldAABB, Transform transform, ref List> grids, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) where T : IPhysShape { - _mapSystem.FindGridsIntersecting(mapEnt, shape, worldAABB, transform, ref grids, approx: approx, includeMap: includeMap); + MapSystem.FindGridsIntersecting(mapEnt, shape, worldAABB, transform, ref grids, approx: approx, includeMap: includeMap); } [Obsolete("use SharedMapSystem")] public void FindGridsIntersecting(EntityUid mapEnt, Box2 worldAABB, GridCallback callback, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) { - _mapSystem.FindGridsIntersecting(mapEnt, worldAABB, callback, approx: approx, includeMap: includeMap); + MapSystem.FindGridsIntersecting(mapEnt, worldAABB, callback, approx: approx, includeMap: includeMap); } [Obsolete("use SharedMapSystem")] public void FindGridsIntersecting(EntityUid mapEnt, Box2 worldAABB, ref TState state, GridCallback callback, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) { - _mapSystem.FindGridsIntersecting(mapEnt, worldAABB, ref state, callback, approx: approx, includeMap: includeMap); + MapSystem.FindGridsIntersecting(mapEnt, worldAABB, ref state, callback, approx: approx, includeMap: includeMap); } [Obsolete("use SharedMapSystem")] public void FindGridsIntersecting(EntityUid mapEnt, Box2 worldAABB, ref List> grids, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) { - _mapSystem.FindGridsIntersecting(mapEnt, worldAABB, ref grids, approx: approx, includeMap: includeMap); + MapSystem.FindGridsIntersecting(mapEnt, worldAABB, ref grids, approx: approx, includeMap: includeMap); } [Obsolete("use SharedMapSystem")] public void FindGridsIntersecting(EntityUid mapEnt, Box2Rotated worldBounds, GridCallback callback, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) { - _mapSystem.FindGridsIntersecting(mapEnt, worldBounds, callback, approx: approx, includeMap: includeMap); + MapSystem.FindGridsIntersecting(mapEnt, worldBounds, callback, approx: approx, includeMap: includeMap); } [Obsolete("use SharedMapSystem")] public void FindGridsIntersecting(EntityUid mapEnt, Box2Rotated worldBounds, ref TState state, GridCallback callback, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) { - _mapSystem.FindGridsIntersecting(mapEnt, worldBounds, ref state, callback, approx: approx, includeMap: includeMap); + MapSystem.FindGridsIntersecting(mapEnt, worldBounds, ref state, callback, approx: approx, includeMap: includeMap); } [Obsolete("use SharedMapSystem")] public void FindGridsIntersecting(EntityUid mapEnt, Box2Rotated worldBounds, ref List> grids, bool approx = IMapManager.Approximate, bool includeMap = IMapManager.IncludeMap) { - _mapSystem.FindGridsIntersecting(mapEnt, worldBounds, ref grids, approx: approx, includeMap: includeMap); + MapSystem.FindGridsIntersecting(mapEnt, worldBounds, ref grids, approx: approx, includeMap: includeMap); } #endregion @@ -170,7 +170,7 @@ public bool TryFindGridAt( out EntityUid uid, [NotNullWhen(true)] out MapGridComponent? grid) { - return _mapSystem.TryFindGridAt(mapEnt, worldPos, out uid, out grid); + return MapSystem.TryFindGridAt(mapEnt, worldPos, out uid, out grid); } /// @@ -179,7 +179,7 @@ public bool TryFindGridAt( [Obsolete("use SharedMapSystem")] public bool TryFindGridAt(MapId mapId, Vector2 worldPos, out EntityUid uid, [NotNullWhen(true)] out MapGridComponent? grid) { - return _mapSystem.TryFindGridAt(mapId, worldPos, out uid, out grid); + return MapSystem.TryFindGridAt(mapId, worldPos, out uid, out grid); } /// @@ -188,7 +188,7 @@ public bool TryFindGridAt(MapId mapId, Vector2 worldPos, out EntityUid uid, [Not [Obsolete("use SharedMapSystem")] public bool TryFindGridAt(MapCoordinates mapCoordinates, out EntityUid uid, [NotNullWhen(true)] out MapGridComponent? grid) { - return _mapSystem.TryFindGridAt(mapCoordinates, out uid, out grid); + return MapSystem.TryFindGridAt(mapCoordinates, out uid, out grid); } #endregion diff --git a/Robust.Shared/Map/MapManager.cs b/Robust.Shared/Map/MapManager.cs index 9b4f1b753b0..3bfd5f3e3ce 100644 --- a/Robust.Shared/Map/MapManager.cs +++ b/Robust.Shared/Map/MapManager.cs @@ -1,11 +1,7 @@ -using Robust.Shared.Console; using Robust.Shared.GameObjects; using Robust.Shared.IoC; using Robust.Shared.Log; using Robust.Shared.Map.Components; -using Robust.Shared.Physics.Collision; -using Robust.Shared.Physics.Systems; -using Robust.Shared.Timing; namespace Robust.Shared.Map; @@ -18,7 +14,7 @@ internal partial class MapManager : IMapManagerInternal, IEntityEventSubscriber private ISawmill _sawmill = default!; - private SharedMapSystem _mapSystem = default!; + protected SharedMapSystem MapSystem = default!; /// public void Initialize() @@ -29,7 +25,7 @@ public void Initialize() /// public void Startup() { - _mapSystem = EntityManager.System(); + MapSystem = EntityManager.System(); _sawmill.Debug("Starting..."); } diff --git a/Robust.Shared/Map/NetworkedMapManager.cs b/Robust.Shared/Map/NetworkedMapManager.cs index 683916bc46d..fdfbfadf781 100644 --- a/Robust.Shared/Map/NetworkedMapManager.cs +++ b/Robust.Shared/Map/NetworkedMapManager.cs @@ -1,23 +1,21 @@ -using Robust.Shared.Map.Components; +using System; using Robust.Shared.Timing; namespace Robust.Shared.Map; +[Obsolete] internal interface INetworkedMapManager : IMapManagerInternal { + [Obsolete] void CullDeletionHistory(GameTick upToTick); } +[Obsolete] internal sealed class NetworkedMapManager : MapManager, INetworkedMapManager { + [Obsolete] public void CullDeletionHistory(GameTick upToTick) { - var query = EntityManager.AllEntityQueryEnumerator(); - - while (query.MoveNext(out var grid)) - { - var chunks = grid.ChunkDeletionHistory; - chunks.RemoveAll(t => t.tick < upToTick); - } + MapSystem.CullDeletionHistory(upToTick); } } From d8f4a284981874f00c6ff078886b1960bb412103 Mon Sep 17 00:00:00 2001 From: TemporalOroboros Date: Tue, 19 May 2026 21:42:53 -0700 Subject: [PATCH 09/13] Rename file --- ...{SharedMapSystem.Lookup.cs => SharedMapSystem.Grid.Queries.cs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Robust.Shared/GameObjects/Systems/{SharedMapSystem.Lookup.cs => SharedMapSystem.Grid.Queries.cs} (100%) diff --git a/Robust.Shared/GameObjects/Systems/SharedMapSystem.Lookup.cs b/Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.Queries.cs similarity index 100% rename from Robust.Shared/GameObjects/Systems/SharedMapSystem.Lookup.cs rename to Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.Queries.cs From 7f9eb72be5cdd39a88e972845d54c1b6f76f13c5 Mon Sep 17 00:00:00 2001 From: TemporalOroboros Date: Tue, 19 May 2026 22:56:49 -0700 Subject: [PATCH 10/13] Doc comments for new SharedMapSystem methods --- .../Systems/SharedMapSystem.Grid.Queries.cs | 173 ++++++++++++++++-- .../Systems/SharedMapSystem.Grid.cs | 13 ++ 2 files changed, 167 insertions(+), 19 deletions(-) diff --git a/Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.Queries.cs b/Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.Queries.cs index d00cb55ea07..61f2aa75139 100644 --- a/Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.Queries.cs +++ b/Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.Queries.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Numerics; @@ -27,6 +28,15 @@ public abstract partial class SharedMapSystem #region TryFindGridAt + /// + /// Attempts to find a grid which overlaps with a given position on a given map. + /// If the map is itself a grid and there is no other grid overlapping with the given position this will return the map itself as such a grid. + /// + /// The uid of the map to search for a valid grid. + /// The exact position within and relative to the map to search for a valid grid. + /// Returns the uid of the grid found, if any. + /// Returns the component of the grid found, if any. + /// True if a grid overlapping with the given position within the given map was found, or false otherwise. public bool TryFindGridAt(EntityUid mapEnt, Vector2 worldPos, out EntityUid uid, [NotNullWhen(true)] out MapGridComponent? grid) { var rangeVec = new Vector2(0.2f, 0.2f); @@ -56,12 +66,12 @@ public bool TryFindGridAt(EntityUid mapEnt, Vector2 worldPos, out EntityUid uid, // NOTE: // If you change this to use fixtures instead (i.e. if you want half-tiles) then you need to make sure // you account for the fact that fixtures are shrunk slightly! - var chunkIndices = SharedMapSystem.GetChunkIndices(localPos, iGrid.ChunkSize); + var chunkIndices = GetChunkIndices(localPos, iGrid.ChunkSize); if (!iGrid.Chunks.TryGetValue(chunkIndices, out var chunk)) return true; - var chunkRelative = SharedMapSystem.GetChunkRelative(localPos, iGrid.ChunkSize); + var chunkRelative = GetChunkRelative(localPos, iGrid.ChunkSize); var chunkTile = chunk.GetTile(chunkRelative); if (chunkTile.IsEmpty) @@ -84,6 +94,8 @@ public bool TryFindGridAt(EntityUid mapEnt, Vector2 worldPos, out EntityUid uid, return grid != null; } + /// + /// The id of the map to search for a valid grid. public bool TryFindGridAt(MapId mapId, Vector2 worldPos, out EntityUid uid, [NotNullWhen(true)] out MapGridComponent? grid) { if (TryGetMap(mapId, out var map)) @@ -94,6 +106,8 @@ public bool TryFindGridAt(MapId mapId, Vector2 worldPos, out EntityUid uid, [Not return false; } + /// + /// The map position to search for a valid grid. public bool TryFindGridAt(MapCoordinates mapCoordinates, out EntityUid uid, [NotNullWhen(true)] out MapGridComponent? grid) { return TryFindGridAt(mapCoordinates.MapId, mapCoordinates.Position, out uid, out grid); @@ -103,30 +117,62 @@ public bool TryFindGridAt(MapCoordinates mapCoordinates, out EntityUid uid, [Not #region MapId - public void FindGridsIntersecting( + /// + /// Adds every grid on the specified map which intersects the given region to the provided collection. + /// + /// The shape of the region to check. + /// The transform, relative to the map, of the region to check. + public void FindGridsIntersecting( MapId mapId, - T shape, + TShape shape, Transform transform, ref List> grids, bool approx = Approximate, - bool includeMap = IncludeMap) where T : IPhysShape + bool includeMap = IncludeMap) where TShape : IPhysShape { if (TryGetMap(mapId, out var mapEnt)) FindGridsIntersecting(mapEnt.Value, shape, transform, ref grids, approx: approx, includeMap: includeMap); } - public void FindGridsIntersecting( + /// + /// Invokes the provided callback on every grid on the specified map which intersect the given region. + /// + /// The shape of the region to check. + /// The transform, relative to the map, of the region to check. + public void FindGridsIntersecting( MapId mapId, - T shape, + TShape shape, Transform transform, GridCallback callback, bool approx = Approximate, - bool includeMap = IncludeMap) where T : IPhysShape + bool includeMap = IncludeMap) where TShape : IPhysShape { if (TryGetMap(mapId, out var mapEnt)) FindGridsIntersecting(mapEnt.Value, shape, transform, callback, approx: approx, includeMap: includeMap); } + /// + /// Invokes the provided callback on every grid on the specified map which intersect the given region. + /// Allows providing some additional to pass to the callback when it is invoked. + /// + /// The shape of the region to check. + /// The transform, relative to the map, of the region to check. + public void FindGridsIntersecting( + MapId mapId, + TShape shape, + Transform transform, + ref TState state, + GridCallback callback, + bool approx = Approximate, + bool includeMap = IncludeMap) where TShape : IPhysShape + { + if (TryGetMap(mapId, out var mapEnt)) + FindGridsIntersecting(mapEnt.Value, shape, transform, ref state, callback, approx: approx, includeMap: includeMap); + } + + /// + /// Invokes the provided callback on every grid on the specified map which intersect the given region. + /// public void FindGridsIntersecting( MapId mapId, Box2 worldAABB, @@ -138,6 +184,10 @@ public void FindGridsIntersecting( FindGridsIntersecting(mapEnt.Value, worldAABB, callback, approx: approx, includeMap: includeMap); } + /// + /// Invokes the provided callback on every grid on the specified map which intersect the given region. + /// Allows providing some additional to pass to the callback when it is invoked. + /// public void FindGridsIntersecting( MapId mapId, Box2 worldAABB, @@ -150,6 +200,9 @@ public void FindGridsIntersecting( FindGridsIntersecting(map.Value, worldAABB, ref state, callback, approx: approx, includeMap: includeMap); } + /// + /// Adds every grid on the specified map which intersects the given region to the provided collection. + /// public void FindGridsIntersecting( MapId mapId, Box2 worldAABB, @@ -161,6 +214,9 @@ public void FindGridsIntersecting( FindGridsIntersecting(map.Value, worldAABB, ref grids, approx: approx, includeMap: includeMap); } + /// + /// Invokes the provided callback on every grid on the specified map which intersect the given region. + /// public void FindGridsIntersecting( MapId mapId, Box2Rotated worldBounds, @@ -172,6 +228,10 @@ public void FindGridsIntersecting( FindGridsIntersecting(mapEnt.Value, worldBounds, callback, approx: approx, includeMap: includeMap); } + /// + /// Invokes the provided callback on every grid on the specified map which intersect the given region. + /// Allows providing some additional to pass to the callback when it is invoked. + /// public void FindGridsIntersecting( MapId mapId, Box2Rotated worldBounds, @@ -184,6 +244,9 @@ public void FindGridsIntersecting( FindGridsIntersecting(mapEnt.Value, worldBounds, ref state, callback, approx: approx, includeMap: includeMap); } + /// + /// Adds every grid on the specified map which intersects the given region to the provided collection. + /// public void FindGridsIntersecting( MapId mapId, Box2Rotated worldBounds, @@ -199,6 +262,11 @@ public void FindGridsIntersecting( #region EntityUid + /// + /// Invokes the provided callback on every grid on the specified map which intersect the given region. + /// + /// The shape of the region to check. + /// The transform, relative to the map, of the region to check. public void FindGridsIntersecting( EntityUid mapEnt, TShape shape, @@ -210,6 +278,12 @@ public void FindGridsIntersecting( FindGridsIntersecting(mapEnt, shape, shape.ComputeAABB(transform, 0), transform, callback, approx: approx, includeMap: includeMap); } + /// + /// Invokes the provided callback on every grid on the specified map which intersect the given region. + /// Allows providing some additional to pass to the callback when it is invoked. + /// + /// The shape of the region to check. + /// The transform, relative to the map, of the region to check. public void FindGridsIntersecting( EntityUid mapEnt, TShape shape, @@ -222,6 +296,27 @@ public void FindGridsIntersecting( FindGridsIntersecting(mapEnt, shape, shape.ComputeAABB(transform, 0), transform, ref state, callback, approx: approx, includeMap: includeMap); } + /// + /// Adds every grid on the specified map which intersects the given region to the provided list. + /// + /// The shape of the region to check. + /// The transform, relative to the map, of the region to check. + public void FindGridsIntersecting( + EntityUid mapEnt, + TShape shape, + Transform transform, + ref List> grids, + bool approx = Approximate, + bool includeMap = IncludeMap) where TShape : IPhysShape + { + FindGridsIntersecting(mapEnt, shape, shape.ComputeAABB(transform, 0), transform, ref grids, approx: approx, includeMap: includeMap); + } + + /// + /// Adds every grid on the specified map which intersects the given regions to the provided collection. + /// + /// A set of regions to check. + /// The transform, relative to the map, of the regions to check. public void FindGridsIntersecting( EntityUid mapEnt, List shapes, @@ -236,17 +331,9 @@ public void FindGridsIntersecting( } } - public void FindGridsIntersecting( - EntityUid mapEnt, - TShape shape, - Transform transform, - ref List> grids, - bool approx = Approximate, - bool includeMap = IncludeMap) where TShape : IPhysShape - { - FindGridsIntersecting(mapEnt, shape, shape.ComputeAABB(transform, 0), transform, ref grids, approx: approx, includeMap: includeMap); - } - + /// + /// Invokes the provided callback on every grid on the specified map which intersect the given region. + /// public void FindGridsIntersecting( EntityUid mapEnt, Box2 worldAABB, @@ -258,6 +345,10 @@ public void FindGridsIntersecting( FindGridsIntersecting(mapEnt, shape, worldAABB, Robust.Shared.Physics.Transform.Empty, callback, approx: approx, includeMap: includeMap); } + /// + /// Invokes the provided callback on every grid on the specified map which intersect the given region. + /// Allows providing some additional to pass to the callback when it is invoked. + /// public void FindGridsIntersecting( EntityUid mapEnt, Box2 worldAABB, @@ -270,6 +361,9 @@ public void FindGridsIntersecting( FindGridsIntersecting(mapEnt, shape, worldAABB, Robust.Shared.Physics.Transform.Empty, ref state, callback, approx: approx, includeMap: includeMap); } + /// + /// Adds every grid on the specified map which intersects the given regions to the provided list. + /// public void FindGridsIntersecting( EntityUid mapEnt, Box2 worldAABB, @@ -281,6 +375,9 @@ public void FindGridsIntersecting( FindGridsIntersecting(mapEnt, shape, worldAABB, Robust.Shared.Physics.Transform.Empty, ref grids, approx: approx, includeMap: includeMap); } + /// + /// Invokes the provided callback on every grid on the specified map which intersect the given region. + /// public void FindGridsIntersecting( EntityUid mapEnt, Box2Rotated worldBounds, @@ -292,6 +389,10 @@ public void FindGridsIntersecting( FindGridsIntersecting(mapEnt, shape, Robust.Shared.Physics.Transform.Empty, callback, approx: approx, includeMap: includeMap); } + /// + /// Invokes the provided callback on every grid on the specified map which intersect the given region. + /// Allows providing some additional to pass to the callback when it is invoked. + /// public void FindGridsIntersecting( EntityUid mapEnt, Box2Rotated worldBounds, @@ -304,6 +405,9 @@ public void FindGridsIntersecting( FindGridsIntersecting(mapEnt, shape, Robust.Shared.Physics.Transform.Empty, ref state, callback, approx: approx, includeMap: includeMap); } + /// + /// Adds every grid on the specified map which intersects the given regions to the provided list. + /// public void FindGridsIntersecting( EntityUid mapEnt, Box2Rotated worldBounds, @@ -317,6 +421,9 @@ public void FindGridsIntersecting( #endregion + /// + /// Enumerates all of the grids located on a given map. + /// public IEnumerable> GetAllGrids(MapId mapId) { var query = AllEntityQuery(); @@ -329,6 +436,11 @@ public IEnumerable> GetAllGrids(MapId mapId) } } + /// + /// This version only provides the component without the uid and should not be used. + /// + /// + [Obsolete("use GetAllGrids instead")] public IEnumerable GetAllMapGrids(MapId mapId) { var query = AllEntityQuery(); @@ -339,6 +451,13 @@ public IEnumerable GetAllMapGrids(MapId mapId) } } + /// + /// Adds every grid on the specified map which intersects the given regions to the provided list. + /// + /// The shape of the region to check. + /// The world-local axis aligned bounding box of the region to check. + /// The transform, relative to the map, of the region to check. + [Access(typeof(IMapManager), Other = AccessPermissions.None)] public void FindGridsIntersecting( EntityUid mapEnt, TShape shape, @@ -359,6 +478,12 @@ public void FindGridsIntersecting( ); } + /// + /// Invokes the provided callback on every grid on the specified map which intersect the given region. + /// + /// The shape of the region to check. + /// The world-local axis aligned bounding box of the region to check. + /// The transform, relative to the map, of the region to check. private void FindGridsIntersecting( EntityUid mapEnt, TShape shape, @@ -375,6 +500,13 @@ private void FindGridsIntersecting( ); } + /// + /// Invokes the provided callback on every grid on the specified map which intersect the given region. + /// Allows providing some additional to pass to the callback when it is invoked. + /// + /// The shape of the region to check. + /// The world-local axis aligned bounding box of the region to check. + /// The transform, relative to the map, of the region to check. private void FindGridsIntersecting( EntityUid mapEnt, TShape shape, @@ -434,6 +566,9 @@ private void FindGridsIntersecting( state = gridState.State; } + /// + /// Tests whether any of a collection of grid chunks intersect with a given region. + /// private bool IsIntersecting( ChunkEnumerator enumerator, TShape shape, diff --git a/Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.cs b/Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.cs index 6de54c1b019..1af4b61baa7 100644 --- a/Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.cs +++ b/Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.cs @@ -23,11 +23,17 @@ public abstract partial class SharedMapSystem { #region CreateGrid + /// + /// Creates a new grid entity on a given map. + /// public Entity CreateGridEntity(MapId mapId, GridCreateOptions? options = null) { return CreateGridEntity(GetMap(mapId), options); } + /// + /// Creates a new grid entity on a given map. + /// public Entity CreateGridEntity(EntityUid mapEnt, GridCreateOptions? options = null) { options ??= GridCreateOptions.Default; @@ -524,6 +530,9 @@ private void GetFullState(EntityUid uid, MapGridComponent component, ref Compone #endif } + /// + /// Prunes tracked grid chunk deletions older than some given game tick. + /// public void CullDeletionHistory(GameTick upToTick) { var query = AllEntityQuery(); @@ -1799,6 +1808,10 @@ public bool MoveNext(out TileRef tile) } } +/// +/// Additional parameters used when creating a new grid entity. +/// +/// The number of tiles long/wide the grids chunks should be. public record struct GridCreateOptions(ushort ChunkSize) { public readonly static GridCreateOptions Default = new(ChunkSize: 16); From e4c6c7a072feaab58fc604c325a9859a83610195 Mon Sep 17 00:00:00 2001 From: TemporalOroboros Date: Tue, 19 May 2026 22:59:03 -0700 Subject: [PATCH 11/13] Doc comment --- Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.cs b/Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.cs index 1af4b61baa7..2e4c328a8e7 100644 --- a/Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.cs +++ b/Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.cs @@ -1703,6 +1703,9 @@ private void OnTileModified(EntityUid uid, MapGridComponent grid, MapChunk mapCh } } + /// + /// Raises on the provided grid unless is set. + /// internal void RaiseOnTileChanged(Entity entity, TileRef tileRef, Tile oldTile, Vector2i chunk) { if (SuppressOnTileChanged) From 0663a9ba6b9143945bdbe4926207d708cbd17025 Mon Sep 17 00:00:00 2001 From: TemporalOroboros Date: Tue, 19 May 2026 23:01:15 -0700 Subject: [PATCH 12/13] Doc comments --- Robust.Shared/Map/Commands/MapPausingCommands.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Robust.Shared/Map/Commands/MapPausingCommands.cs b/Robust.Shared/Map/Commands/MapPausingCommands.cs index 6f2a2a80538..2b74ac3c15a 100644 --- a/Robust.Shared/Map/Commands/MapPausingCommands.cs +++ b/Robust.Shared/Map/Commands/MapPausingCommands.cs @@ -5,6 +5,9 @@ namespace Robust.Shared.Map.Commands; +/// +/// Pauses a given map, halting all entity processing on it. +/// public sealed partial class PauseMapCommand : LocalizedEntityCommands { [Dependency] SharedMapSystem _mapSystem = default!; @@ -30,6 +33,9 @@ public override void Execute(IConsoleShell shell, string argStr, string[] args) } } +/// +/// Unpauses a given map, resuming all entity processing on it. +/// public sealed partial class UnpauseMapCommand : LocalizedEntityCommands { [Dependency] SharedMapSystem _mapSystem = default!; @@ -55,6 +61,9 @@ public override void Execute(IConsoleShell shell, string argStr, string[] args) } } +/// +/// Checks whether a given map is currently paused. +/// public sealed partial class QueryMapPausedCommand : LocalizedEntityCommands { [Dependency] SharedMapSystem _mapSystem = default!; From 633ddcf4f6ea65facfbc839dacab142bd51cd051 Mon Sep 17 00:00:00 2001 From: TemporalOroboros Date: Tue, 19 May 2026 23:06:50 -0700 Subject: [PATCH 13/13] Fix access --- .../GameObjects/Systems/SharedMapSystem.Grid.Queries.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.Queries.cs b/Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.Queries.cs index 61f2aa75139..ae3165b0543 100644 --- a/Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.Queries.cs +++ b/Robust.Shared/GameObjects/Systems/SharedMapSystem.Grid.Queries.cs @@ -457,7 +457,7 @@ public IEnumerable GetAllMapGrids(MapId mapId) /// The shape of the region to check. /// The world-local axis aligned bounding box of the region to check. /// The transform, relative to the map, of the region to check. - [Access(typeof(IMapManager), Other = AccessPermissions.None)] + [Access(typeof(MapManager), Other = AccessPermissions.None)] public void FindGridsIntersecting( EntityUid mapEnt, TShape shape,