Replace MapIndices with Vector2i (#2228)

* Replace MapIndices with Vector2i

* Update da submodule

* AA EE II OO U U

Co-authored-by: Pieter-Jan Briers <pieterjan.briers+git@gmail.com>
This commit is contained in:
DrSmugleaf
2020-10-11 15:21:21 +02:00
committed by GitHub
parent 5127824716
commit 753ca81865
35 changed files with 224 additions and 211 deletions

View File

@@ -26,15 +26,15 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Pathfinding
public TimeSpan LastUpdate { get; private set; }
public GridId GridId { get; }
public MapIndices Indices => _indices;
private readonly MapIndices _indices;
public Vector2i Indices => _indices;
private readonly Vector2i _indices;
// Nodes per chunk row
public static int ChunkSize => 8;
public PathfindingNode[,] Nodes => _nodes;
private PathfindingNode[,] _nodes = new PathfindingNode[ChunkSize,ChunkSize];
public PathfindingChunk(GridId gridId, MapIndices indices)
public PathfindingChunk(GridId gridId, Vector2i indices)
{
GridId = gridId;
_indices = indices;
@@ -46,7 +46,7 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Pathfinding
{
for (var y = 0; y < ChunkSize; y++)
{
var tileRef = mapGrid.GetTileRef(new MapIndices(x + _indices.X, y + _indices.Y));
var tileRef = mapGrid.GetTileRef(new Vector2i(x + _indices.X, y + _indices.Y));
CreateNode(tileRef);
}
}
@@ -75,7 +75,7 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Pathfinding
{
if (x == 0 && y == 0) continue;
var (neighborX, neighborY) = (_indices.X + ChunkSize * x, _indices.Y + ChunkSize * y);
if (chunkGrid.TryGetValue(new MapIndices(neighborX, neighborY), out var neighbor))
if (chunkGrid.TryGetValue(new Vector2i(neighborX, neighborY), out var neighbor))
{
yield return neighbor;
}
@@ -83,10 +83,10 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Pathfinding
}
}
public bool InBounds(MapIndices mapIndices)
public bool InBounds(Vector2i Vector2i)
{
if (mapIndices.X < _indices.X || mapIndices.Y < _indices.Y) return false;
if (mapIndices.X >= _indices.X + ChunkSize || mapIndices.Y >= _indices.Y + ChunkSize) return false;
if (Vector2i.X < _indices.X || Vector2i.Y < _indices.Y) return false;
if (Vector2i.X >= _indices.X + ChunkSize || Vector2i.Y >= _indices.Y + ChunkSize) return false;
return true;
}

View File

@@ -199,7 +199,7 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Pathfinding
}
intermediate = pathfindingSystem.GetNode(grid.GetTileRef(
new MapIndices(intermediate.TileRef.X + xOffset, intermediate.TileRef.Y + yOffset)));
new Vector2i(intermediate.TileRef.X + xOffset, intermediate.TileRef.Y + yOffset)));
if (intermediate.TileRef != current.TileRef)
{

View File

@@ -69,7 +69,7 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Pathfinding
for (var y = -1; y <= 1; y++)
{
if (x == 0 && y == 0) continue;
var indices = new MapIndices(TileRef.X + x, TileRef.Y + y);
var indices = new Vector2i(TileRef.X + x, TileRef.Y + y);
if (ParentChunk.InBounds(indices))
{
var (relativeX, relativeY) = (indices.X - ParentChunk.Indices.X,
@@ -100,7 +100,7 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Pathfinding
{
var chunkXOffset = TileRef.X - ParentChunk.Indices.X;
var chunkYOffset = TileRef.Y - ParentChunk.Indices.Y;
MapIndices neighborMapIndices;
Vector2i neighborVector2i;
switch (direction)
{
@@ -110,13 +110,13 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Pathfinding
return ParentChunk.Nodes[chunkXOffset + 1, chunkYOffset];
}
neighborMapIndices = new MapIndices(TileRef.X + 1, TileRef.Y);
neighborVector2i = new Vector2i(TileRef.X + 1, TileRef.Y);
foreach (var neighbor in ParentChunk.GetNeighbors())
{
if (neighbor.InBounds(neighborMapIndices))
if (neighbor.InBounds(neighborVector2i))
{
return neighbor.Nodes[neighborMapIndices.X - neighbor.Indices.X,
neighborMapIndices.Y - neighbor.Indices.Y];
return neighbor.Nodes[neighborVector2i.X - neighbor.Indices.X,
neighborVector2i.Y - neighbor.Indices.Y];
}
}
@@ -127,13 +127,13 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Pathfinding
return ParentChunk.Nodes[chunkXOffset + 1, chunkYOffset + 1];
}
neighborMapIndices = new MapIndices(TileRef.X + 1, TileRef.Y + 1);
neighborVector2i = new Vector2i(TileRef.X + 1, TileRef.Y + 1);
foreach (var neighbor in ParentChunk.GetNeighbors())
{
if (neighbor.InBounds(neighborMapIndices))
if (neighbor.InBounds(neighborVector2i))
{
return neighbor.Nodes[neighborMapIndices.X - neighbor.Indices.X,
neighborMapIndices.Y - neighbor.Indices.Y];
return neighbor.Nodes[neighborVector2i.X - neighbor.Indices.X,
neighborVector2i.Y - neighbor.Indices.Y];
}
}
@@ -144,13 +144,13 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Pathfinding
return ParentChunk.Nodes[chunkXOffset, chunkYOffset + 1];
}
neighborMapIndices = new MapIndices(TileRef.X, TileRef.Y + 1);
neighborVector2i = new Vector2i(TileRef.X, TileRef.Y + 1);
foreach (var neighbor in ParentChunk.GetNeighbors())
{
if (neighbor.InBounds(neighborMapIndices))
if (neighbor.InBounds(neighborVector2i))
{
return neighbor.Nodes[neighborMapIndices.X - neighbor.Indices.X,
neighborMapIndices.Y - neighbor.Indices.Y];
return neighbor.Nodes[neighborVector2i.X - neighbor.Indices.X,
neighborVector2i.Y - neighbor.Indices.Y];
}
}
@@ -161,13 +161,13 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Pathfinding
return ParentChunk.Nodes[chunkXOffset - 1, chunkYOffset + 1];
}
neighborMapIndices = new MapIndices(TileRef.X - 1, TileRef.Y + 1);
neighborVector2i = new Vector2i(TileRef.X - 1, TileRef.Y + 1);
foreach (var neighbor in ParentChunk.GetNeighbors())
{
if (neighbor.InBounds(neighborMapIndices))
if (neighbor.InBounds(neighborVector2i))
{
return neighbor.Nodes[neighborMapIndices.X - neighbor.Indices.X,
neighborMapIndices.Y - neighbor.Indices.Y];
return neighbor.Nodes[neighborVector2i.X - neighbor.Indices.X,
neighborVector2i.Y - neighbor.Indices.Y];
}
}
@@ -178,13 +178,13 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Pathfinding
return ParentChunk.Nodes[chunkXOffset - 1, chunkYOffset];
}
neighborMapIndices = new MapIndices(TileRef.X - 1, TileRef.Y);
neighborVector2i = new Vector2i(TileRef.X - 1, TileRef.Y);
foreach (var neighbor in ParentChunk.GetNeighbors())
{
if (neighbor.InBounds(neighborMapIndices))
if (neighbor.InBounds(neighborVector2i))
{
return neighbor.Nodes[neighborMapIndices.X - neighbor.Indices.X,
neighborMapIndices.Y - neighbor.Indices.Y];
return neighbor.Nodes[neighborVector2i.X - neighbor.Indices.X,
neighborVector2i.Y - neighbor.Indices.Y];
}
}
@@ -195,13 +195,13 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Pathfinding
return ParentChunk.Nodes[chunkXOffset - 1, chunkYOffset - 1];
}
neighborMapIndices = new MapIndices(TileRef.X - 1, TileRef.Y - 1);
neighborVector2i = new Vector2i(TileRef.X - 1, TileRef.Y - 1);
foreach (var neighbor in ParentChunk.GetNeighbors())
{
if (neighbor.InBounds(neighborMapIndices))
if (neighbor.InBounds(neighborVector2i))
{
return neighbor.Nodes[neighborMapIndices.X - neighbor.Indices.X,
neighborMapIndices.Y - neighbor.Indices.Y];
return neighbor.Nodes[neighborVector2i.X - neighbor.Indices.X,
neighborVector2i.Y - neighbor.Indices.Y];
}
}
@@ -212,13 +212,13 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Pathfinding
return ParentChunk.Nodes[chunkXOffset, chunkYOffset - 1];
}
neighborMapIndices = new MapIndices(TileRef.X, TileRef.Y - 1);
neighborVector2i = new Vector2i(TileRef.X, TileRef.Y - 1);
foreach (var neighbor in ParentChunk.GetNeighbors())
{
if (neighbor.InBounds(neighborMapIndices))
if (neighbor.InBounds(neighborVector2i))
{
return neighbor.Nodes[neighborMapIndices.X - neighbor.Indices.X,
neighborMapIndices.Y - neighbor.Indices.Y];
return neighbor.Nodes[neighborVector2i.X - neighbor.Indices.X,
neighborVector2i.Y - neighbor.Indices.Y];
}
}
@@ -229,13 +229,13 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Pathfinding
return ParentChunk.Nodes[chunkXOffset + 1, chunkYOffset - 1];
}
neighborMapIndices = new MapIndices(TileRef.X + 1, TileRef.Y - 1);
neighborVector2i = new Vector2i(TileRef.X + 1, TileRef.Y - 1);
foreach (var neighbor in ParentChunk.GetNeighbors())
{
if (neighbor.InBounds(neighborMapIndices))
if (neighbor.InBounds(neighborVector2i))
{
return neighbor.Nodes[neighborMapIndices.X - neighbor.Indices.X,
neighborMapIndices.Y - neighbor.Indices.Y];
return neighbor.Nodes[neighborVector2i.X - neighbor.Indices.X,
neighborVector2i.Y - neighbor.Indices.Y];
}
}

View File

@@ -13,6 +13,7 @@ using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Map;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Utility;
namespace Content.Server.GameObjects.EntitySystems.AI.Pathfinding
@@ -32,8 +33,8 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Pathfinding
[Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private readonly IEntityManager _entityManager = default!;
public IReadOnlyDictionary<GridId, Dictionary<MapIndices, PathfindingChunk>> Graph => _graph;
private readonly Dictionary<GridId, Dictionary<MapIndices, PathfindingChunk>> _graph = new Dictionary<GridId, Dictionary<MapIndices, PathfindingChunk>>();
public IReadOnlyDictionary<GridId, Dictionary<Vector2i, PathfindingChunk>> Graph => _graph;
private readonly Dictionary<GridId, Dictionary<Vector2i, PathfindingChunk>> _graph = new Dictionary<GridId, Dictionary<Vector2i, PathfindingChunk>>();
private readonly PathfindingJobQueue _pathfindingQueue = new PathfindingJobQueue();
@@ -144,28 +145,28 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Pathfinding
{
var chunkX = (int) (Math.Floor((float) tile.X / PathfindingChunk.ChunkSize) * PathfindingChunk.ChunkSize);
var chunkY = (int) (Math.Floor((float) tile.Y / PathfindingChunk.ChunkSize) * PathfindingChunk.ChunkSize);
var mapIndices = new MapIndices(chunkX, chunkY);
var Vector2i = new Vector2i(chunkX, chunkY);
if (_graph.TryGetValue(tile.GridIndex, out var chunks))
{
if (!chunks.ContainsKey(mapIndices))
if (!chunks.ContainsKey(Vector2i))
{
CreateChunk(tile.GridIndex, mapIndices);
CreateChunk(tile.GridIndex, Vector2i);
}
return chunks[mapIndices];
return chunks[Vector2i];
}
var newChunk = CreateChunk(tile.GridIndex, mapIndices);
var newChunk = CreateChunk(tile.GridIndex, Vector2i);
return newChunk;
}
private PathfindingChunk CreateChunk(GridId gridId, MapIndices indices)
private PathfindingChunk CreateChunk(GridId gridId, Vector2i indices)
{
var newChunk = new PathfindingChunk(gridId, indices);
if (!_graph.ContainsKey(gridId))
{
_graph.Add(gridId, new Dictionary<MapIndices, PathfindingChunk>());
_graph.Add(gridId, new Dictionary<Vector2i, PathfindingChunk>());
}
_graph[gridId].Add(indices, newChunk);

View File

@@ -603,10 +603,10 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Steering
return Vector2.Zero;
}
if (target.TryGetComponent(out ICollidableComponent physicsComponent))
if (target.TryGetComponent(out ICollidableComponent collidable))
{
var targetDistance = (targetPos.Position - entityPos.Position);
targetPos = targetPos.Offset(physicsComponent.LinearVelocity * targetDistance);
targetPos = targetPos.Offset(collidable.LinearVelocity * targetDistance);
}
return (targetPos.Position - entityPos.Position).Normalized;
@@ -662,8 +662,8 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Steering
// if we're moving in the same direction then ignore
// So if 2 entities are moving towards each other and both detect a collision they'll both move in the same direction
// i.e. towards the right
if (physicsEntity.TryGetComponent(out ICollidableComponent physicsComponent) &&
Vector2.Dot(physicsComponent.LinearVelocity, direction) > 0)
if (physicsEntity.TryGetComponent(out ICollidableComponent collidable) &&
Vector2.Dot(collidable.LinearVelocity, direction) > 0)
{
continue;
}

View File

@@ -120,7 +120,7 @@ namespace Content.Server.GameObjects.EntitySystems.Atmos
if (!gridEnt.TryGetComponent<GridAtmosphereComponent>(out var gam)) continue;
var entityTile = grid.GetTileRef(entity.Transform.Coordinates).GridIndices;
var baseTile = new MapIndices(entityTile.X - (LocalViewRange / 2), entityTile.Y - (LocalViewRange / 2));
var baseTile = new Vector2i(entityTile.X - (LocalViewRange / 2), entityTile.Y - (LocalViewRange / 2));
var debugOverlayContent = new AtmosDebugOverlayData[LocalViewRange * LocalViewRange];
var index = 0;
@@ -128,8 +128,8 @@ namespace Content.Server.GameObjects.EntitySystems.Atmos
{
for (var x = 0; x < LocalViewRange; x++)
{
var mapIndices = new MapIndices(baseTile.X + x, baseTile.Y + y);
debugOverlayContent[index++] = ConvertTileToData(gam.GetTile(mapIndices));
var Vector2i = new Vector2i(baseTile.X + x, baseTile.Y + y);
debugOverlayContent[index++] = ConvertTileToData(gam.GetTile(Vector2i));
}
}

View File

@@ -31,7 +31,7 @@ namespace Content.Server.GameObjects.EntitySystems.Atmos
/// <summary>
/// The tiles that have had their atmos data updated since last tick
/// </summary>
private Dictionary<GridId, HashSet<MapIndices>> _invalidTiles = new Dictionary<GridId, HashSet<MapIndices>>();
private Dictionary<GridId, HashSet<Vector2i>> _invalidTiles = new Dictionary<GridId, HashSet<Vector2i>>();
private Dictionary<IPlayerSession, PlayerGasOverlay> _knownPlayerChunks =
new Dictionary<IPlayerSession, PlayerGasOverlay>();
@@ -39,8 +39,8 @@ namespace Content.Server.GameObjects.EntitySystems.Atmos
/// <summary>
/// Gas data stored in chunks to make PVS / bubbling easier.
/// </summary>
private Dictionary<GridId, Dictionary<MapIndices, GasOverlayChunk>> _overlay =
new Dictionary<GridId, Dictionary<MapIndices, GasOverlayChunk>>();
private Dictionary<GridId, Dictionary<Vector2i, GasOverlayChunk>> _overlay =
new Dictionary<GridId, Dictionary<Vector2i, GasOverlayChunk>>();
/// <summary>
/// How far away do we update gas overlays (minimum; due to chunking further away tiles may also be updated).
@@ -75,22 +75,22 @@ namespace Content.Server.GameObjects.EntitySystems.Atmos
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Invalidate(GridId gridIndex, MapIndices indices)
public void Invalidate(GridId gridIndex, Vector2i indices)
{
if (!_invalidTiles.TryGetValue(gridIndex, out var existing))
{
existing = new HashSet<MapIndices>();
existing = new HashSet<Vector2i>();
_invalidTiles[gridIndex] = existing;
}
existing.Add(indices);
}
private GasOverlayChunk GetOrCreateChunk(GridId gridIndex, MapIndices indices)
private GasOverlayChunk GetOrCreateChunk(GridId gridIndex, Vector2i indices)
{
if (!_overlay.TryGetValue(gridIndex, out var chunks))
{
chunks = new Dictionary<MapIndices, GasOverlayChunk>();
chunks = new Dictionary<Vector2i, GasOverlayChunk>();
_overlay[gridIndex] = chunks;
}
@@ -150,7 +150,7 @@ namespace Content.Server.GameObjects.EntitySystems.Atmos
/// <param name="indices"></param>
/// <param name="overlayData"></param>
/// <returns>true if updated</returns>
private bool TryRefreshTile(GridAtmosphereComponent gam, GasOverlayData oldTile, MapIndices indices, out GasOverlayData overlayData)
private bool TryRefreshTile(GridAtmosphereComponent gam, GasOverlayData oldTile, Vector2i indices, out GasOverlayData overlayData)
{
var tile = gam.GetTile(indices);
@@ -214,7 +214,7 @@ namespace Content.Server.GameObjects.EntitySystems.Atmos
{
for (var y = -maxYDiff; y <= maxYDiff; y++)
{
var chunkIndices = GetGasChunkIndices(new MapIndices(entityTile.X + x * ChunkSize, entityTile.Y + y * ChunkSize));
var chunkIndices = GetGasChunkIndices(new Vector2i(entityTile.X + x * ChunkSize, entityTile.Y + y * ChunkSize));
if (!chunks.TryGetValue(chunkIndices, out var chunk)) continue;
@@ -261,7 +261,7 @@ namespace Content.Server.GameObjects.EntitySystems.Atmos
AccumulatedFrameTime -= _updateCooldown;
var gridAtmosComponents = new Dictionary<GridId, GridAtmosphereComponent>();
var updatedTiles = new Dictionary<GasOverlayChunk, HashSet<MapIndices>>();
var updatedTiles = new Dictionary<GasOverlayChunk, HashSet<Vector2i>>();
// So up to this point we've been caching the updated tiles for multiple ticks.
// Now we'll go through and check whether the update actually matters for the overlay or not,
@@ -295,7 +295,7 @@ namespace Content.Server.GameObjects.EntitySystems.Atmos
if (!updatedTiles.TryGetValue(chunk, out var tiles))
{
tiles = new HashSet<MapIndices>();
tiles = new HashSet<Vector2i>();
updatedTiles[chunk] = tiles;
}
@@ -355,7 +355,7 @@ namespace Content.Server.GameObjects.EntitySystems.Atmos
overlay.RemoveChunk(chunk);
}
var clientInvalids = new Dictionary<GridId, List<(MapIndices, GasOverlayData)>>();
var clientInvalids = new Dictionary<GridId, List<(Vector2i, GasOverlayData)>>();
// Check for any dirty chunks in range and bundle the data to send to the client.
foreach (var chunk in chunksInRange)
@@ -364,7 +364,7 @@ namespace Content.Server.GameObjects.EntitySystems.Atmos
if (!clientInvalids.TryGetValue(chunk.GridIndices, out var existingData))
{
existingData = new List<(MapIndices, GasOverlayData)>();
existingData = new List<(Vector2i, GasOverlayData)>();
clientInvalids[chunk.GridIndices] = existingData;
}
@@ -382,13 +382,13 @@ namespace Content.Server.GameObjects.EntitySystems.Atmos
}
private sealed class PlayerGasOverlay
{
private readonly Dictionary<GridId, Dictionary<MapIndices, GasOverlayChunk>> _data =
new Dictionary<GridId, Dictionary<MapIndices, GasOverlayChunk>>();
private readonly Dictionary<GridId, Dictionary<Vector2i, GasOverlayChunk>> _data =
new Dictionary<GridId, Dictionary<Vector2i, GasOverlayChunk>>();
private readonly Dictionary<GasOverlayChunk, GameTick> _lastSent =
new Dictionary<GasOverlayChunk, GameTick>();
public GasOverlayMessage UpdateClient(GridId grid, List<(MapIndices, GasOverlayData)> data)
public GasOverlayMessage UpdateClient(GridId grid, List<(Vector2i, GasOverlayData)> data)
{
return new GasOverlayMessage(grid, data);
}
@@ -418,7 +418,7 @@ namespace Content.Server.GameObjects.EntitySystems.Atmos
{
if (!_data.TryGetValue(chunk.GridIndices, out var chunks))
{
chunks = new Dictionary<MapIndices, GasOverlayChunk>();
chunks = new Dictionary<Vector2i, GasOverlayChunk>();
_data[chunk.GridIndices] = chunks;
}
@@ -441,9 +441,9 @@ namespace Content.Server.GameObjects.EntitySystems.Atmos
return;
}
if (chunks.ContainsKey(chunk.MapIndices))
if (chunks.ContainsKey(chunk.Vector2i))
{
chunks.Remove(chunk.MapIndices);
chunks.Remove(chunk.Vector2i);
}
}
@@ -457,7 +457,7 @@ namespace Content.Server.GameObjects.EntitySystems.Atmos
// Chunk data should already be up to date.
// Only send relevant tiles to client.
var tileData = new List<(MapIndices, GasOverlayData)>();
var tileData = new List<(Vector2i, GasOverlayData)>();
for (var x = 0; x < ChunkSize; x++)
{
@@ -470,7 +470,7 @@ namespace Content.Server.GameObjects.EntitySystems.Atmos
continue;
}
var indices = new MapIndices(chunk.MapIndices.X + x, chunk.MapIndices.Y + y);
var indices = new Vector2i(chunk.Vector2i.X + x, chunk.Vector2i.Y + y);
tileData.Add((indices, data));
}
}