From 61a3562451cfd965334441a8c8113ad5a90ec0ab Mon Sep 17 00:00:00 2001 From: Mervill Date: Thu, 15 Aug 2024 21:55:20 -0700 Subject: [PATCH] Replace obsolete map functions in IconSmoothSystem (#30958) * first part * second part --- .../IconSmoothing/IconSmoothSystem.cs | 126 ++++++++++-------- 1 file changed, 74 insertions(+), 52 deletions(-) diff --git a/Content.Client/IconSmoothing/IconSmoothSystem.cs b/Content.Client/IconSmoothing/IconSmoothSystem.cs index 11ca75bc82..2654f1ab57 100644 --- a/Content.Client/IconSmoothing/IconSmoothSystem.cs +++ b/Content.Client/IconSmoothing/IconSmoothSystem.cs @@ -16,6 +16,8 @@ namespace Content.Client.IconSmoothing [UsedImplicitly] public sealed partial class IconSmoothSystem : EntitySystem { + [Dependency] private readonly SharedMapSystem _mapSystem = default!; + private readonly Queue _dirtyEntities = new(); private readonly Queue _anchorChangedEntities = new(); @@ -46,7 +48,7 @@ namespace Content.Client.IconSmoothing if (xform.Anchored) { component.LastPosition = TryComp(xform.GridUid, out var grid) - ? (xform.GridUid.Value, grid.TileIndicesFor(xform.Coordinates)) + ? (xform.GridUid.Value, _mapSystem.TileIndicesFor(xform.GridUid.Value, grid, xform.Coordinates)) : (null, new Vector2i(0, 0)); DirtyNeighbours(uid, component); @@ -151,9 +153,12 @@ namespace Content.Client.IconSmoothing Vector2i pos; + EntityUid entityUid; + if (transform.Anchored && TryComp(transform.GridUid, out var grid)) { - pos = grid.CoordinatesToTile(transform.Coordinates); + entityUid = transform.GridUid.Value; + pos = _mapSystem.CoordinatesToTile(transform.GridUid.Value, grid, transform.Coordinates); } else { @@ -164,21 +169,22 @@ namespace Content.Client.IconSmoothing if (!TryComp(gridId, out grid)) return; + entityUid = gridId; pos = oldPos; } // Yes, we updates ALL smoothing entities surrounding us even if they would never smooth with us. - DirtyEntities(grid.GetAnchoredEntitiesEnumerator(pos + new Vector2i(1, 0))); - DirtyEntities(grid.GetAnchoredEntitiesEnumerator(pos + new Vector2i(-1, 0))); - DirtyEntities(grid.GetAnchoredEntitiesEnumerator(pos + new Vector2i(0, 1))); - DirtyEntities(grid.GetAnchoredEntitiesEnumerator(pos + new Vector2i(0, -1))); + DirtyEntities(_mapSystem.GetAnchoredEntitiesEnumerator(entityUid, grid, pos + new Vector2i(1, 0))); + DirtyEntities(_mapSystem.GetAnchoredEntitiesEnumerator(entityUid, grid, pos + new Vector2i(-1, 0))); + DirtyEntities(_mapSystem.GetAnchoredEntitiesEnumerator(entityUid, grid, pos + new Vector2i(0, 1))); + DirtyEntities(_mapSystem.GetAnchoredEntitiesEnumerator(entityUid, grid, pos + new Vector2i(0, -1))); if (comp.Mode is IconSmoothingMode.Corners or IconSmoothingMode.NoSprite or IconSmoothingMode.Diagonal) { - DirtyEntities(grid.GetAnchoredEntitiesEnumerator(pos + new Vector2i(1, 1))); - DirtyEntities(grid.GetAnchoredEntitiesEnumerator(pos + new Vector2i(-1, -1))); - DirtyEntities(grid.GetAnchoredEntitiesEnumerator(pos + new Vector2i(-1, 1))); - DirtyEntities(grid.GetAnchoredEntitiesEnumerator(pos + new Vector2i(1, -1))); + DirtyEntities(_mapSystem.GetAnchoredEntitiesEnumerator(entityUid, grid, pos + new Vector2i(1, 1))); + DirtyEntities(_mapSystem.GetAnchoredEntitiesEnumerator(entityUid, grid, pos + new Vector2i(-1, -1))); + DirtyEntities(_mapSystem.GetAnchoredEntitiesEnumerator(entityUid, grid, pos + new Vector2i(-1, 1))); + DirtyEntities(_mapSystem.GetAnchoredEntitiesEnumerator(entityUid, grid, pos + new Vector2i(1, -1))); } } @@ -206,7 +212,7 @@ namespace Content.Client.IconSmoothing IconSmoothComponent? smooth = null) { TransformComponent? xform; - MapGridComponent? grid = null; + Entity? gridEntity = null; // The generation check prevents updating an entity multiple times per tick. // As it stands now, it's totally possible for something to get queued twice. @@ -223,17 +229,20 @@ namespace Content.Client.IconSmoothing { var directions = DirectionFlag.None; - if (TryComp(xform.GridUid, out grid)) + if (TryComp(xform.GridUid, out MapGridComponent? grid)) { - var pos = grid.TileIndicesFor(xform.Coordinates); + var gridUid = xform.GridUid.Value; + var pos = _mapSystem.TileIndicesFor(gridUid, grid, xform.Coordinates); - if (MatchingEntity(smooth, grid.GetAnchoredEntitiesEnumerator(pos.Offset(Direction.North)), smoothQuery)) + gridEntity = (gridUid, grid); + + if (MatchingEntity(smooth, _mapSystem.GetAnchoredEntitiesEnumerator(gridUid, grid, pos.Offset(Direction.North)), smoothQuery)) directions |= DirectionFlag.North; - if (MatchingEntity(smooth, grid.GetAnchoredEntitiesEnumerator(pos.Offset(Direction.South)), smoothQuery)) + if (MatchingEntity(smooth, _mapSystem.GetAnchoredEntitiesEnumerator(gridUid, grid, pos.Offset(Direction.South)), smoothQuery)) directions |= DirectionFlag.South; - if (MatchingEntity(smooth, grid.GetAnchoredEntitiesEnumerator(pos.Offset(Direction.East)), smoothQuery)) + if (MatchingEntity(smooth, _mapSystem.GetAnchoredEntitiesEnumerator(gridUid, grid, pos.Offset(Direction.East)), smoothQuery)) directions |= DirectionFlag.East; - if (MatchingEntity(smooth, grid.GetAnchoredEntitiesEnumerator(pos.Offset(Direction.West)), smoothQuery)) + if (MatchingEntity(smooth, _mapSystem.GetAnchoredEntitiesEnumerator(gridUid, grid, pos.Offset(Direction.West)), smoothQuery)) directions |= DirectionFlag.West; } @@ -257,7 +266,11 @@ namespace Content.Client.IconSmoothing if (xform.Anchored) { - if (!TryComp(xform.GridUid, out grid)) + if (TryComp(xform.GridUid, out MapGridComponent? grid)) + { + gridEntity = (xform.GridUid.Value, grid); + } + else { Log.Error($"Failed to calculate IconSmoothComponent sprite in {uid} because grid {xform.GridUid} was missing."); return; @@ -267,28 +280,31 @@ namespace Content.Client.IconSmoothing switch (smooth.Mode) { case IconSmoothingMode.Corners: - CalculateNewSpriteCorners(grid, smooth, spriteEnt, xform, smoothQuery); + CalculateNewSpriteCorners(gridEntity, smooth, spriteEnt, xform, smoothQuery); break; case IconSmoothingMode.CardinalFlags: - CalculateNewSpriteCardinal(grid, smooth, spriteEnt, xform, smoothQuery); + CalculateNewSpriteCardinal(gridEntity, smooth, spriteEnt, xform, smoothQuery); break; case IconSmoothingMode.Diagonal: - CalculateNewSpriteDiagonal(grid, smooth, spriteEnt, xform, smoothQuery); + CalculateNewSpriteDiagonal(gridEntity, smooth, spriteEnt, xform, smoothQuery); break; default: throw new ArgumentOutOfRangeException(); } } - private void CalculateNewSpriteDiagonal(MapGridComponent? grid, IconSmoothComponent smooth, + private void CalculateNewSpriteDiagonal(Entity? gridEntity, IconSmoothComponent smooth, Entity sprite, TransformComponent xform, EntityQuery smoothQuery) { - if (grid == null) + if (gridEntity == null) { sprite.Comp.LayerSetState(0, $"{smooth.StateBase}0"); return; } + var gridUid = gridEntity.Value.Owner; + var grid = gridEntity.Value.Comp; + var neighbors = new Vector2[] { new(1, 0), @@ -296,14 +312,14 @@ namespace Content.Client.IconSmoothing new(0, -1), }; - var pos = grid.TileIndicesFor(xform.Coordinates); + var pos = _mapSystem.TileIndicesFor(gridUid, grid, xform.Coordinates); var rotation = xform.LocalRotation; var matching = true; for (var i = 0; i < neighbors.Length; i++) { - var neighbor = (Vector2i) rotation.RotateVec(neighbors[i]); - matching = matching && MatchingEntity(smooth, grid.GetAnchoredEntitiesEnumerator(pos + neighbor), smoothQuery); + var neighbor = (Vector2i)rotation.RotateVec(neighbors[i]); + matching = matching && MatchingEntity(smooth, _mapSystem.GetAnchoredEntitiesEnumerator(gridUid, grid, pos + neighbor), smoothQuery); } if (matching) @@ -316,27 +332,30 @@ namespace Content.Client.IconSmoothing } } - private void CalculateNewSpriteCardinal(MapGridComponent? grid, IconSmoothComponent smooth, Entity sprite, TransformComponent xform, EntityQuery smoothQuery) + private void CalculateNewSpriteCardinal(Entity? gridEntity, IconSmoothComponent smooth, Entity sprite, TransformComponent xform, EntityQuery smoothQuery) { var dirs = CardinalConnectDirs.None; - if (grid == null) + if (gridEntity == null) { - sprite.Comp.LayerSetState(0, $"{smooth.StateBase}{(int) dirs}"); + sprite.Comp.LayerSetState(0, $"{smooth.StateBase}{(int)dirs}"); return; } - var pos = grid.TileIndicesFor(xform.Coordinates); - if (MatchingEntity(smooth, grid.GetAnchoredEntitiesEnumerator(pos.Offset(Direction.North)), smoothQuery)) + var gridUid = gridEntity.Value.Owner; + var grid = gridEntity.Value.Comp; + + var pos = _mapSystem.TileIndicesFor(gridUid, grid, xform.Coordinates); + if (MatchingEntity(smooth, _mapSystem.GetAnchoredEntitiesEnumerator(gridUid, grid, pos.Offset(Direction.North)), smoothQuery)) dirs |= CardinalConnectDirs.North; - if (MatchingEntity(smooth, grid.GetAnchoredEntitiesEnumerator(pos.Offset(Direction.South)), smoothQuery)) + if (MatchingEntity(smooth, _mapSystem.GetAnchoredEntitiesEnumerator(gridUid, grid, pos.Offset(Direction.South)), smoothQuery)) dirs |= CardinalConnectDirs.South; - if (MatchingEntity(smooth, grid.GetAnchoredEntitiesEnumerator(pos.Offset(Direction.East)), smoothQuery)) + if (MatchingEntity(smooth, _mapSystem.GetAnchoredEntitiesEnumerator(gridUid, grid, pos.Offset(Direction.East)), smoothQuery)) dirs |= CardinalConnectDirs.East; - if (MatchingEntity(smooth, grid.GetAnchoredEntitiesEnumerator(pos.Offset(Direction.West)), smoothQuery)) + if (MatchingEntity(smooth, _mapSystem.GetAnchoredEntitiesEnumerator(gridUid, grid, pos.Offset(Direction.West)), smoothQuery)) dirs |= CardinalConnectDirs.West; - sprite.Comp.LayerSetState(0, $"{smooth.StateBase}{(int) dirs}"); + sprite.Comp.LayerSetState(0, $"{smooth.StateBase}{(int)dirs}"); var directions = DirectionFlag.None; @@ -367,11 +386,11 @@ namespace Content.Client.IconSmoothing return false; } - private void CalculateNewSpriteCorners(MapGridComponent? grid, IconSmoothComponent smooth, Entity spriteEnt, TransformComponent xform, EntityQuery smoothQuery) + private void CalculateNewSpriteCorners(Entity? gridEntity, IconSmoothComponent smooth, Entity spriteEnt, TransformComponent xform, EntityQuery smoothQuery) { - var (cornerNE, cornerNW, cornerSW, cornerSE) = grid == null + var (cornerNE, cornerNW, cornerSW, cornerSE) = gridEntity == null ? (CornerFill.None, CornerFill.None, CornerFill.None, CornerFill.None) - : CalculateCornerFill(grid, smooth, xform, smoothQuery); + : CalculateCornerFill(gridEntity.Value, smooth, xform, smoothQuery); // TODO figure out a better way to set multiple sprite layers. // This will currently re-calculate the sprite bounding box 4 times. @@ -379,10 +398,10 @@ namespace Content.Client.IconSmoothing // At the very least each event currently only queues a sprite for updating. // Oh god sprite component is a mess. var sprite = spriteEnt.Comp; - sprite.LayerSetState(CornerLayers.NE, $"{smooth.StateBase}{(int) cornerNE}"); - sprite.LayerSetState(CornerLayers.SE, $"{smooth.StateBase}{(int) cornerSE}"); - sprite.LayerSetState(CornerLayers.SW, $"{smooth.StateBase}{(int) cornerSW}"); - sprite.LayerSetState(CornerLayers.NW, $"{smooth.StateBase}{(int) cornerNW}"); + sprite.LayerSetState(CornerLayers.NE, $"{smooth.StateBase}{(int)cornerNE}"); + sprite.LayerSetState(CornerLayers.SE, $"{smooth.StateBase}{(int)cornerSE}"); + sprite.LayerSetState(CornerLayers.SW, $"{smooth.StateBase}{(int)cornerSW}"); + sprite.LayerSetState(CornerLayers.NW, $"{smooth.StateBase}{(int)cornerNW}"); var directions = DirectionFlag.None; @@ -401,17 +420,20 @@ namespace Content.Client.IconSmoothing CalculateEdge(spriteEnt, directions, sprite); } - private (CornerFill ne, CornerFill nw, CornerFill sw, CornerFill se) CalculateCornerFill(MapGridComponent grid, IconSmoothComponent smooth, TransformComponent xform, EntityQuery smoothQuery) + private (CornerFill ne, CornerFill nw, CornerFill sw, CornerFill se) CalculateCornerFill(Entity gridEntity, IconSmoothComponent smooth, TransformComponent xform, EntityQuery smoothQuery) { - var pos = grid.TileIndicesFor(xform.Coordinates); - var n = MatchingEntity(smooth, grid.GetAnchoredEntitiesEnumerator(pos.Offset(Direction.North)), smoothQuery); - var ne = MatchingEntity(smooth, grid.GetAnchoredEntitiesEnumerator(pos.Offset(Direction.NorthEast)), smoothQuery); - var e = MatchingEntity(smooth, grid.GetAnchoredEntitiesEnumerator(pos.Offset(Direction.East)), smoothQuery); - var se = MatchingEntity(smooth, grid.GetAnchoredEntitiesEnumerator(pos.Offset(Direction.SouthEast)), smoothQuery); - var s = MatchingEntity(smooth, grid.GetAnchoredEntitiesEnumerator(pos.Offset(Direction.South)), smoothQuery); - var sw = MatchingEntity(smooth, grid.GetAnchoredEntitiesEnumerator(pos.Offset(Direction.SouthWest)), smoothQuery); - var w = MatchingEntity(smooth, grid.GetAnchoredEntitiesEnumerator(pos.Offset(Direction.West)), smoothQuery); - var nw = MatchingEntity(smooth, grid.GetAnchoredEntitiesEnumerator(pos.Offset(Direction.NorthWest)), smoothQuery); + var gridUid = gridEntity.Owner; + var grid = gridEntity.Comp; + + var pos = _mapSystem.TileIndicesFor(gridUid, grid, xform.Coordinates); + var n = MatchingEntity(smooth, _mapSystem.GetAnchoredEntitiesEnumerator(gridUid, grid, pos.Offset(Direction.North)), smoothQuery); + var ne = MatchingEntity(smooth, _mapSystem.GetAnchoredEntitiesEnumerator(gridUid, grid, pos.Offset(Direction.NorthEast)), smoothQuery); + var e = MatchingEntity(smooth, _mapSystem.GetAnchoredEntitiesEnumerator(gridUid, grid, pos.Offset(Direction.East)), smoothQuery); + var se = MatchingEntity(smooth, _mapSystem.GetAnchoredEntitiesEnumerator(gridUid, grid, pos.Offset(Direction.SouthEast)), smoothQuery); + var s = MatchingEntity(smooth, _mapSystem.GetAnchoredEntitiesEnumerator(gridUid, grid, pos.Offset(Direction.South)), smoothQuery); + var sw = MatchingEntity(smooth, _mapSystem.GetAnchoredEntitiesEnumerator(gridUid, grid, pos.Offset(Direction.SouthWest)), smoothQuery); + var w = MatchingEntity(smooth, _mapSystem.GetAnchoredEntitiesEnumerator(gridUid, grid, pos.Offset(Direction.West)), smoothQuery); + var nw = MatchingEntity(smooth, _mapSystem.GetAnchoredEntitiesEnumerator(gridUid, grid, pos.Offset(Direction.NorthWest)), smoothQuery); // ReSharper disable InconsistentNaming var cornerNE = CornerFill.None;