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;
}