Add a completion helper for MapIds (#3833)

* Add a completion helper for MapIds

* oop
This commit is contained in:
metalgearsloth
2023-03-10 09:03:42 +11:00
committed by GitHub
parent 8a827b37e6
commit abecc554f4
2 changed files with 75 additions and 30 deletions

View File

@@ -2,7 +2,10 @@
using System.Linq;
using JetBrains.Annotations;
using Robust.Shared.ContentPack;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Map.Components;
using Robust.Shared.Players;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
@@ -100,4 +103,11 @@ public static class CompletionHelper
var playerOptions = players.Sessions.Select(p => new CompletionOption(p.Name));
return sorted ? playerOptions.OrderBy(o => o.Value) : playerOptions;
}
public static IEnumerable<CompletionOption> MapIds(IEntityManager? entManager = null)
{
IoCManager.Resolve(ref entManager);
return entManager.EntityQuery<MapComponent>(true).Select(o => new CompletionOption(o.WorldMap.ToString()));
}
}

View File

@@ -6,6 +6,7 @@ using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Map;
using Robust.Shared.Map.Enumerators;
using Robust.Shared.Map.Events;
using Robust.Shared.Maths;
@@ -78,7 +79,7 @@ namespace Robust.Shared.Map.Components
Chunks.Remove(origin);
if (Chunks.Count == 0)
_entMan.EventBus.RaiseLocalEvent(base.Owner, new EmptyGridEvent { GridId = base.Owner }, true);
_entMan.EventBus.RaiseLocalEvent(Owner, new EmptyGridEvent { GridId = Owner }, true);
}
/// <summary>
@@ -181,7 +182,7 @@ namespace Robust.Shared.Map.Components
if (!Chunks.TryGetValue(chunkIndices, out var output))
{
// Chunk doesn't exist, return a tileRef to an empty (space) tile.
return new TileRef(base.Owner, tileCoordinates.X, tileCoordinates.Y, default);
return new TileRef(Owner, tileCoordinates.X, tileCoordinates.Y, default);
}
var chunkTileIndices = output.GridTileToChunkTile(tileCoordinates);
@@ -204,7 +205,7 @@ namespace Robust.Shared.Map.Components
throw new ArgumentOutOfRangeException(nameof(yIndex), "Tile indices out of bounds.");
var indices = mapChunk.ChunkTileToGridTile(new Vector2i(xIndex, yIndex));
return new TileRef(base.Owner, indices, mapChunk.GetTile(xIndex, yIndex));
return new TileRef(Owner, indices, mapChunk.GetTile(xIndex, yIndex));
}
/// <inheritdoc />
@@ -223,7 +224,7 @@ namespace Robust.Shared.Map.Components
continue;
var (gridX, gridY) = new Vector2i(x, y) + chunk.Indices * ChunkSize;
yield return new TileRef(base.Owner, gridX, gridY, tile);
yield return new TileRef(Owner, gridX, gridY, tile);
}
}
}
@@ -232,7 +233,7 @@ namespace Robust.Shared.Map.Components
/// <inheritdoc />
public GridTileEnumerator GetAllTilesEnumerator(bool ignoreEmpty = true)
{
return new GridTileEnumerator(base.Owner, Chunks.GetEnumerator(), ChunkSize, ignoreEmpty);
return new GridTileEnumerator(Owner, Chunks.GetEnumerator(), ChunkSize, ignoreEmpty);
}
/// <inheritdoc />
@@ -286,7 +287,7 @@ namespace Robust.Shared.Map.Components
public IEnumerable<TileRef> GetTilesIntersecting(Box2Rotated worldArea, bool ignoreEmpty = true,
Predicate<TileRef>? predicate = null)
{
var matrix = _entMan.GetComponent<TransformComponent>(base.Owner).InvWorldMatrix;
var matrix = _entMan.GetComponent<TransformComponent>(Owner).InvWorldMatrix;
var localArea = matrix.TransformBox(worldArea);
foreach (var tile in GetLocalTilesIntersecting(localArea, ignoreEmpty, predicate))
@@ -299,7 +300,7 @@ namespace Robust.Shared.Map.Components
public IEnumerable<TileRef> GetTilesIntersecting(Box2 worldArea, bool ignoreEmpty = true,
Predicate<TileRef>? predicate = null)
{
var matrix = _entMan.GetComponent<TransformComponent>(base.Owner).InvWorldMatrix;
var matrix = _entMan.GetComponent<TransformComponent>(Owner).InvWorldMatrix;
var localArea = matrix.TransformBox(worldArea);
foreach (var tile in GetLocalTilesIntersecting(localArea, ignoreEmpty, predicate))
@@ -336,7 +337,7 @@ namespace Robust.Shared.Map.Components
}
else if (!ignoreEmpty)
{
var tile = new TileRef(base.Owner, x, y, new Tile());
var tile = new TileRef(Owner, x, y, new Tile());
if (predicate == null || predicate(tile))
yield return tile;
@@ -351,9 +352,9 @@ namespace Robust.Shared.Map.Components
{
var aabb = new Box2(worldArea.Position.X - worldArea.Radius, worldArea.Position.Y - worldArea.Radius,
worldArea.Position.X + worldArea.Radius, worldArea.Position.Y + worldArea.Radius);
var circleGridPos = new EntityCoordinates(base.Owner, WorldToLocal(worldArea.Position));
var circleGridPos = new EntityCoordinates(Owner, WorldToLocal(worldArea.Position));
foreach (var tile in GetTilesIntersecting(aabb, ignoreEmpty))
foreach (var tile in GetTilesIntersecting(aabb, ignoreEmpty, predicate))
{
var local = GridTileToLocal(tile.GridIndices);
@@ -364,14 +365,48 @@ namespace Robust.Shared.Map.Components
if (distance <= worldArea.Radius)
{
if (predicate == null || predicate(tile))
{
yield return tile;
}
yield return tile;
}
}
}
private bool TryGetTile(Vector2i indices, bool ignoreEmpty, [NotNullWhen(true)] out TileRef? tileRef, Predicate<TileRef>? predicate = null)
{
// Similar to TryGetTileRef but for the tiles intersecting iterators.
var gridChunk = GridTileToChunkIndices(indices);
if (Chunks.TryGetValue(gridChunk, out var chunk))
{
var chunkTile = chunk.GridTileToChunkTile(indices);
var tile = GetTileRef(chunk, (ushort)chunkTile.X, (ushort)chunkTile.Y);
if (ignoreEmpty && tile.Tile.IsEmpty)
{
tileRef = null;
return false;
}
if (predicate == null || predicate(tile))
{
tileRef = tile;
return true;
}
}
else if (!ignoreEmpty)
{
var tile = new TileRef(Owner, indices.X, indices.Y, Tile.Empty);
if (predicate == null || predicate(tile))
{
tileRef = tile;
return true;
}
}
tileRef = null;
return false;
}
#endregion TileAccess
#region ChunkAccess
@@ -422,7 +457,7 @@ namespace Robust.Shared.Map.Components
/// <inheritdoc />
internal ChunkEnumerator GetMapChunks(Box2 worldAABB)
{
var localAABB = _entMan.GetComponent<TransformComponent>(base.Owner).InvWorldMatrix
var localAABB = _entMan.GetComponent<TransformComponent>(Owner).InvWorldMatrix
.TransformBox(worldAABB);
return new ChunkEnumerator(Chunks, localAABB, ChunkSize);
}
@@ -430,7 +465,7 @@ namespace Robust.Shared.Map.Components
/// <inheritdoc />
internal ChunkEnumerator GetMapChunks(Box2Rotated worldArea)
{
var matrix = _entMan.GetComponent<TransformComponent>(base.Owner).InvWorldMatrix;
var matrix = _entMan.GetComponent<TransformComponent>(Owner).InvWorldMatrix;
var localArea = matrix.TransformBox(worldArea);
return new ChunkEnumerator(Chunks, localArea, ChunkSize);
}
@@ -533,7 +568,7 @@ namespace Robust.Shared.Map.Components
public Vector2i TileIndicesFor(EntityCoordinates coords)
{
#if DEBUG
var mapId = _entMan.GetComponent<TransformComponent>(base.Owner).MapID;
var mapId = _entMan.GetComponent<TransformComponent>(Owner).MapID;
DebugTools.Assert(mapId == coords.GetMapId(_entMan));
#endif
@@ -544,7 +579,7 @@ namespace Robust.Shared.Map.Components
public Vector2i TileIndicesFor(MapCoordinates worldPos)
{
#if DEBUG
var mapId = _entMan.GetComponent<TransformComponent>(base.Owner).MapID;
var mapId = _entMan.GetComponent<TransformComponent>(Owner).MapID;
DebugTools.Assert(mapId == worldPos.MapId);
#endif
@@ -672,21 +707,21 @@ namespace Robust.Shared.Map.Components
/// <inheritdoc />
public Vector2 WorldToLocal(Vector2 posWorld)
{
var matrix = _entMan.GetComponent<TransformComponent>(base.Owner).InvWorldMatrix;
var matrix = _entMan.GetComponent<TransformComponent>(Owner).InvWorldMatrix;
return matrix.Transform(posWorld);
}
/// <inheritdoc />
public EntityCoordinates MapToGrid(MapCoordinates posWorld)
{
var mapId = _entMan.GetComponent<TransformComponent>(base.Owner).MapID;
var mapId = _entMan.GetComponent<TransformComponent>(Owner).MapID;
if (posWorld.MapId != mapId)
throw new ArgumentException(
$"Grid {base.Owner} is on map {mapId}, but coords are on map {posWorld.MapId}.",
$"Grid {Owner} is on map {mapId}, but coords are on map {posWorld.MapId}.",
nameof(posWorld));
if (!_mapManager.TryGetGrid(base.Owner, out var grid))
if (!_mapManager.TryGetGrid(Owner, out var grid))
{
return new EntityCoordinates(_mapManager.GetMapEntityId(posWorld.MapId), (posWorld.X, posWorld.Y));
}
@@ -697,7 +732,7 @@ namespace Robust.Shared.Map.Components
/// <inheritdoc />
public Vector2 LocalToWorld(Vector2 posLocal)
{
var matrix = _entMan.GetComponent<TransformComponent>(base.Owner).WorldMatrix;
var matrix = _entMan.GetComponent<TransformComponent>(Owner).WorldMatrix;
return matrix.Transform(posLocal);
}
@@ -719,7 +754,7 @@ namespace Robust.Shared.Map.Components
public Vector2i CoordinatesToTile(MapCoordinates coords)
{
#if DEBUG
var mapId = _entMan.GetComponent<TransformComponent>(base.Owner).MapID;
var mapId = _entMan.GetComponent<TransformComponent>(Owner).MapID;
DebugTools.Assert(mapId == coords.MapId);
#endif
@@ -734,7 +769,7 @@ namespace Robust.Shared.Map.Components
public Vector2i CoordinatesToTile(EntityCoordinates coords)
{
#if DEBUG
var mapId = _entMan.GetComponent<TransformComponent>(base.Owner).MapID;
var mapId = _entMan.GetComponent<TransformComponent>(Owner).MapID;
DebugTools.Assert(mapId == coords.GetMapId(_entMan));
#endif
var local = LocalToGrid(coords);
@@ -756,7 +791,7 @@ namespace Robust.Shared.Map.Components
public Vector2 LocalToGrid(EntityCoordinates position)
{
return position.EntityId == base.Owner
return position.EntityId == Owner
? position.Position
: WorldToLocal(position.ToMapPos(_entMan));
}
@@ -791,14 +826,14 @@ namespace Robust.Shared.Map.Components
{
var locX = gridTile.X * TileSize + (TileSize / 2f);
var locY = gridTile.Y * TileSize + (TileSize / 2f);
var xform = _entMan.GetComponent<TransformComponent>(base.Owner);
var xform = _entMan.GetComponent<TransformComponent>(Owner);
return xform.WorldMatrix.Transform(new Vector2(locX, locY));
}
public MapCoordinates GridTileToWorld(Vector2i gridTile)
{
var parentMapId = _entMan.GetComponent<TransformComponent>(base.Owner).MapID;
var parentMapId = _entMan.GetComponent<TransformComponent>(Owner).MapID;
return new(GridTileToWorldPos(gridTile), parentMapId);
}
@@ -838,7 +873,7 @@ namespace Robust.Shared.Map.Components
internal Box2 CalcWorldAABB(MapChunk mapChunk)
{
var (position, rotation) =
_entMan.GetComponent<TransformComponent>(base.Owner).GetWorldPositionRotation();
_entMan.GetComponent<TransformComponent>(Owner).GetWorldPositionRotation();
var chunkPosition = mapChunk.Indices;
var tileScale = TileSize;
@@ -867,7 +902,7 @@ namespace Robust.Shared.Map.Components
// ParentMapId is not able to be accessed on unbound grids, so we can't even call this function for unbound grids.
if (!_mapManager.SuppressOnTileChanged)
{
var newTileRef = new TileRef(base.Owner, gridTile, newTile);
var newTileRef = new TileRef(Owner, gridTile, newTile);
_mapManager.RaiseOnTileChanged(newTileRef, oldTile);
}