Replace every usage of GridCoordinates with EntityCoordinates (#2021)

* Update RobustToolbox

* Transition direct type usages

* More updates

* Fix invalid use of to map

* Update RobustToolbox

* Fix dropping items

* Rename name usages of "GridCoordinates" to "EntityCoordinates"

* Revert "Update RobustToolbox"

This reverts commit 9f334a17c5908ded0043a63158bb671e4aa3f346.

* Revert "Update RobustToolbox"

This reverts commit 3a9c8cfa3606fa501aa84407796d2ad920853a09.

# Conflicts:
#	RobustToolbox

* Fix cursed IMapGrid method usage.

* GridTileLookupTest now uses EntityCoordinates

Co-authored-by: Víctor Aguilera Puerto <6766154+Zumorica@users.noreply.github.com>
Co-authored-by: Víctor Aguilera Puerto <zddm@outlook.es>
This commit is contained in:
DrSmugleaf
2020-09-06 16:11:53 +02:00
committed by GitHub
parent 72d2318ea7
commit 48b61f6bcc
196 changed files with 780 additions and 676 deletions

View File

@@ -38,6 +38,7 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Pathfinding.Accessible
*/
[Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly IEntityManager _entityManager = default!;
private PathfindingSystem _pathfindingSystem;
@@ -160,7 +161,7 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Pathfinding.Accessible
/// <returns></returns>
public bool CanAccess(IEntity entity, IEntity target, float range = 0.0f)
{
var targetTile = _mapManager.GetGrid(target.Transform.GridID).GetTileRef(target.Transform.GridPosition);
var targetTile = _mapManager.GetGrid(target.Transform.GridID).GetTileRef(target.Transform.Coordinates);
var targetNode = _pathfindingSystem.GetNode(targetTile);
var collisionMask = 0;
@@ -198,7 +199,7 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Pathfinding.Accessible
return false;
}
var entityTile = _mapManager.GetGrid(entity.Transform.GridID).GetTileRef(entity.Transform.GridPosition);
var entityTile = _mapManager.GetGrid(entity.Transform.GridID).GetTileRef(entity.Transform.Coordinates);
var entityNode = _pathfindingSystem.GetNode(entityTile);
var entityRegion = GetRegion(entityNode);
var targetRegion = GetRegion(targetNode);
@@ -408,7 +409,7 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Pathfinding.Accessible
/// <returns></returns>
public PathfindingRegion GetRegion(IEntity entity)
{
var entityTile = _mapManager.GetGrid(entity.Transform.GridID).GetTileRef(entity.Transform.GridPosition);
var entityTile = _mapManager.GetGrid(entity.Transform.GridID).GetTileRef(entity.Transform.Coordinates);
var entityNode = _pathfindingSystem.GetNode(entityTile);
return GetRegion(entityNode);
}
@@ -632,7 +633,7 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Pathfinding.Accessible
{
return;
}
if (!_regions.ContainsKey(chunk.GridId))
{
_regions.Add(chunk.GridId, new Dictionary<PathfindingChunk, HashSet<PathfindingRegion>>());
@@ -714,7 +715,7 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Pathfinding.Accessible
foreach (var node in region.Nodes)
{
var nodeVector = grid.GridTileToLocal(node.TileRef.GridIndices).ToMapPos(_mapManager);
var nodeVector = grid.GridTileToLocal(node.TileRef.GridIndices).ToMapPos(_entityManager);
debugRegionNodes.Add(nodeVector);
}
@@ -743,7 +744,7 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Pathfinding.Accessible
foreach (var node in region.Nodes)
{
var nodeVector = grid.GridTileToLocal(node.TileRef.GridIndices).ToMapPos(_mapManager);
var nodeVector = grid.GridTileToLocal(node.TileRef.GridIndices).ToMapPos(_entityManager);
debugResult[_runningCacheIdx].Add(nodeVector);
}

View File

@@ -30,6 +30,7 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Pathfinding
public class PathfindingSystem : EntitySystem
{
[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>>();
@@ -180,7 +181,7 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Pathfinding
/// <returns></returns>
public PathfindingNode GetNode(IEntity entity)
{
var tile = _mapManager.GetGrid(entity.Transform.GridID).GetTileRef(entity.Transform.GridPosition);
var tile = _mapManager.GetGrid(entity.Transform.GridID).GetTileRef(entity.Transform.Coordinates);
return GetNode(tile);
}
@@ -280,7 +281,7 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Pathfinding
}
var grid = _mapManager.GetGrid(entity.Transform.GridID);
var tileRef = grid.GetTileRef(entity.Transform.GridPosition);
var tileRef = grid.GetTileRef(entity.Transform.Coordinates);
var chunk = GetChunk(tileRef);
var node = chunk.GetNode(tileRef);
@@ -337,7 +338,7 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Pathfinding
// The pathfinding graph is tile-based so first we'll check if they're on a different tile and if we need to update.
// If you get entities bigger than 1 tile wide you'll need some other system so god help you.
var newTile = _mapManager.GetGrid(moveEvent.NewPosition.GridID).GetTileRef(moveEvent.NewPosition);
var newTile = _mapManager.GetGrid(moveEvent.NewPosition.GetGridId(_entityManager)).GetTileRef(moveEvent.NewPosition);
if (oldNode == null || oldNode.TileRef == newTile)
{
@@ -359,9 +360,10 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Pathfinding
// TODO: Need to rethink the pathfinder utils (traversable etc.). Maybe just chuck them all in PathfindingSystem
// Otherwise you get the steerer using this and the pathfinders using a different traversable.
// Also look at increasing tile cost the more physics entities are on it
public bool CanTraverse(IEntity entity, GridCoordinates grid)
public bool CanTraverse(IEntity entity, EntityCoordinates coordinates)
{
var tile = _mapManager.GetGrid(grid.GridID).GetTileRef(grid);
var gridId = coordinates.GetGridId(_entityManager);
var tile = _mapManager.GetGrid(gridId).GetTileRef(coordinates);
var node = GetNode(tile);
return CanTraverse(entity, node);
}

View File

@@ -35,16 +35,16 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Pathfinding
foreach (var tile in routeDebug.Route)
{
var tileGrid = mapManager.GetGrid(tile.GridIndex).GridTileToLocal(tile.GridIndices);
route.Add(mapManager.GetGrid(tile.GridIndex).LocalToWorld(tileGrid).Position);
route.Add(tileGrid.ToMapPos(EntityManager));
}
var cameFrom = new Dictionary<Vector2, Vector2>();
foreach (var (from, to) in routeDebug.CameFrom)
{
var tileOneGrid = mapManager.GetGrid(from.GridIndex).GridTileToLocal(from.GridIndices);
var tileOneWorld = mapManager.GetGrid(from.GridIndex).LocalToWorld(tileOneGrid).Position;
var tileOneWorld = tileOneGrid.ToMapPos(EntityManager);
var tileTwoGrid = mapManager.GetGrid(to.GridIndex).GridTileToLocal(to.GridIndices);
var tileTwoWorld = mapManager.GetGrid(to.GridIndex).LocalToWorld(tileTwoGrid).Position;
var tileTwoWorld = tileTwoGrid.ToMapPos(EntityManager);
cameFrom.Add(tileOneWorld, tileTwoWorld);
}
@@ -52,7 +52,7 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Pathfinding
foreach (var (tile, score) in routeDebug.GScores)
{
var tileGrid = mapManager.GetGrid(tile.GridIndex).GridTileToLocal(tile.GridIndices);
gScores.Add(mapManager.GetGrid(tile.GridIndex).LocalToWorld(tileGrid).Position, score);
gScores.Add(tileGrid.ToMapPos(EntityManager), score);
}
var systemMessage = new SharedAiDebug.AStarRouteMessage(
@@ -73,14 +73,14 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Pathfinding
foreach (var tile in routeDebug.Route)
{
var tileGrid = mapManager.GetGrid(tile.GridIndex).GridTileToLocal(tile.GridIndices);
route.Add(mapManager.GetGrid(tile.GridIndex).LocalToWorld(tileGrid).Position);
route.Add(tileGrid.ToMapPos(EntityManager));
}
var jumpNodes = new List<Vector2>();
foreach (var tile in routeDebug.JumpNodes)
{
var tileGrid = mapManager.GetGrid(tile.GridIndex).GridTileToLocal(tile.GridIndices);
jumpNodes.Add(mapManager.GetGrid(tile.GridIndex).LocalToWorld(tileGrid).Position);
jumpNodes.Add(tileGrid.ToMapPos(EntityManager));
}
var systemMessage = new SharedAiDebug.JpsRouteMessage(

View File

@@ -25,8 +25,9 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Steering
public sealed class AiSteeringSystem : EntitySystem
{
// http://www.red3d.com/cwr/papers/1999/gdc99steer.html for a steering overview
[Dependency] private IMapManager _mapManager = default!;
[Dependency] private IPauseManager _pauseManager = default!;
[Dependency] private readonly IMapManager _mapManager = default!;
[Dependency] private readonly IPauseManager _pauseManager = default!;
[Dependency] private readonly IEntityManager _entityManager = default!;
private PathfindingSystem _pathfindingSystem;
@@ -57,7 +58,7 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Steering
private int _listIndex;
// Cache nextGrid
private readonly Dictionary<IEntity, GridCoordinates> _nextGrid = new Dictionary<IEntity, GridCoordinates>();
private readonly Dictionary<IEntity, EntityCoordinates> _nextGrid = new Dictionary<IEntity, EntityCoordinates>();
/// <summary>
/// Current live paths for AI
@@ -78,11 +79,11 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Steering
/// <summary>
/// Get a fixed position for the target entity; if they move then re-path
/// </summary>
private readonly Dictionary<IEntity, GridCoordinates> _entityTargetPosition = new Dictionary<IEntity, GridCoordinates>();
private readonly Dictionary<IEntity, EntityCoordinates> _entityTargetPosition = new Dictionary<IEntity, EntityCoordinates>();
// Anti-Stuck
// Given the collision avoidance can lead to twitching need to store a reference position and check if we've been near this too long
private readonly Dictionary<IEntity, GridCoordinates> _stuckPositions = new Dictionary<IEntity, GridCoordinates>();
private readonly Dictionary<IEntity, EntityCoordinates> _stuckPositions = new Dictionary<IEntity, EntityCoordinates>();
public override void Initialize()
{
@@ -268,7 +269,7 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Steering
// Validation
// Check if we can even arrive -> Currently only samegrid movement supported
if (entity.Transform.GridID != steeringRequest.TargetGrid.GridID)
if (entity.Transform.GridID != steeringRequest.TargetGrid.GetGridId(_entityManager))
{
controller.VelocityDir = Vector2.Zero;
return SteeringStatus.NoPath;
@@ -413,7 +414,7 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Steering
var cancelToken = new CancellationTokenSource();
var gridManager = _mapManager.GetGrid(entity.Transform.GridID);
var startTile = gridManager.GetTileRef(entity.Transform.GridPosition);
var startTile = gridManager.GetTileRef(entity.Transform.Coordinates);
var endTile = gridManager.GetTileRef(steeringRequest.TargetGrid);
var collisionMask = 0;
if (entity.TryGetComponent(out ICollidableComponent collidableComponent))
@@ -443,7 +444,7 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Steering
{
_pathfindingRequests.Remove(entity);
var entityTile = _mapManager.GetGrid(entity.Transform.GridID).GetTileRef(entity.Transform.GridPosition);
var entityTile = _mapManager.GetGrid(entity.Transform.GridID).GetTileRef(entity.Transform.Coordinates);
var tile = path.Dequeue();
var closestDistance = PathfindingHelpers.OctileDistance(entityTile, tile);
@@ -465,12 +466,12 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Steering
}
/// <summary>
/// Get the next tile as GridCoordinates
/// Get the next tile as EntityCoordinates
/// </summary>
/// <param name="entity"></param>
/// <param name="steeringRequest"></param>
/// <returns></returns>
private GridCoordinates? NextGrid(IEntity entity, IAiSteeringRequest steeringRequest)
private EntityCoordinates? NextGrid(IEntity entity, IAiSteeringRequest steeringRequest)
{
// Remove the cached grid
if (!_paths.ContainsKey(entity) && _nextGrid.ContainsKey(entity))
@@ -481,7 +482,7 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Steering
// If no tiles left just move towards the target (if we're close)
if (!_paths.ContainsKey(entity) || _paths[entity].Count == 0)
{
if ((steeringRequest.TargetGrid.Position - entity.Transform.GridPosition.Position).Length <= 2.0f)
if ((steeringRequest.TargetGrid.Position - entity.Transform.Coordinates.Position).Length <= 2.0f)
{
return steeringRequest.TargetGrid;
}
@@ -491,7 +492,7 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Steering
}
if (!_nextGrid.TryGetValue(entity, out var nextGrid) ||
(nextGrid.Position - entity.Transform.GridPosition.Position).Length <= TileTolerance)
(nextGrid.Position - entity.Transform.Coordinates.Position).Length <= TileTolerance)
{
UpdateGridCache(entity);
nextGrid = _nextGrid[entity];
@@ -502,7 +503,7 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Steering
}
/// <summary>
/// Rather than converting TileRef to GridCoordinates over and over we'll just cache it
/// Rather than converting TileRef to EntityCoordinates over and over we'll just cache it
/// </summary>
/// <param name="entity"></param>
/// <param name="dequeue"></param>
@@ -515,19 +516,19 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Steering
}
/// <summary>
/// Check if we've been near our last GridCoordinates too long and try to fix it
/// Check if we've been near our last EntityCoordinates too long and try to fix it
/// </summary>
/// <param name="entity"></param>
private void HandleStuck(IEntity entity)
{
if (!_stuckPositions.TryGetValue(entity, out var stuckPosition))
{
_stuckPositions[entity] = entity.Transform.GridPosition;
_stuckPositions[entity] = entity.Transform.Coordinates;
_stuckCounter[entity] = 0;
return;
}
if ((entity.Transform.GridPosition.Position - stuckPosition.Position).Length <= 1.0f)
if ((entity.Transform.Coordinates.Position - stuckPosition.Position).Length <= 1.0f)
{
_stuckCounter.TryGetValue(entity, out var stuckCount);
_stuckCounter[entity] = stuckCount + 1;
@@ -535,7 +536,7 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Steering
else
{
// No longer stuck
_stuckPositions[entity] = entity.Transform.GridPosition;
_stuckPositions[entity] = entity.Transform.Coordinates;
_stuckCounter[entity] = 0;
return;
}
@@ -558,11 +559,13 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Steering
/// <param name="entity"></param>
/// <param name="grid"></param>
/// <returns></returns>
private Vector2 Seek(IEntity entity, GridCoordinates grid)
private Vector2 Seek(IEntity entity, EntityCoordinates grid)
{
// is-even much
var entityPos = entity.Transform.GridPosition;
return entityPos == grid ? Vector2.Zero : (grid.Position - entityPos.Position).Normalized;
var entityPos = entity.Transform.Coordinates;
return entityPos == grid
? Vector2.Zero
: (grid.Position - entityPos.Position).Normalized;
}
/// <summary>
@@ -572,9 +575,9 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Steering
/// <param name="grid"></param>
/// <param name="slowingDistance"></param>
/// <returns></returns>
private Vector2 Arrival(IEntity entity, GridCoordinates grid, float slowingDistance = 1.0f)
private Vector2 Arrival(IEntity entity, EntityCoordinates grid, float slowingDistance = 1.0f)
{
var entityPos = entity.Transform.GridPosition;
var entityPos = entity.Transform.Coordinates;
DebugTools.Assert(slowingDistance > 0.0f);
if (entityPos == grid)
{
@@ -593,8 +596,8 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Steering
/// <returns></returns>
private Vector2 Pursuit(IEntity entity, IEntity target)
{
var entityPos = entity.Transform.GridPosition;
var targetPos = target.Transform.GridPosition;
var entityPos = entity.Transform.Coordinates;
var targetPos = target.Transform.Coordinates;
if (entityPos == targetPos)
{
return Vector2.Zero;
@@ -630,7 +633,7 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Steering
var avoidanceVector = Vector2.Zero;
var checkTiles = new HashSet<TileRef>();
var avoidTiles = new HashSet<TileRef>();
var entityGridCoords = entity.Transform.GridPosition;
var entityGridCoords = entity.Transform.Coordinates;
var grid = _mapManager.GetGrid(entity.Transform.GridID);
var currentTile = grid.GetTileRef(entityGridCoords);
var halfwayTile = grid.GetTileRef(entityGridCoords.Offset(direction / 2));
@@ -665,7 +668,7 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Steering
continue;
}
var centerGrid = physicsEntity.Transform.GridPosition;
var centerGrid = physicsEntity.Transform.Coordinates;
// Check how close we are to center of tile and get the inverse; if we're closer this is stronger
var additionalVector = (centerGrid.Position - entityGridCoords.Position);
var distance = additionalVector.Length;

View File

@@ -7,16 +7,16 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Steering
{
public SteeringStatus Status { get; set; } = SteeringStatus.Pending;
public MapCoordinates TargetMap => _target.Transform.MapPosition;
public GridCoordinates TargetGrid => _target.Transform.GridPosition;
public EntityCoordinates TargetGrid => _target.Transform.Coordinates;
public IEntity Target => _target;
private IEntity _target;
/// <inheritdoc />
public float ArrivalDistance { get; }
/// <inheritdoc />
public float PathfindingProximity { get; }
/// <summary>
/// How far the target can move before we re-path
/// </summary>
@@ -24,12 +24,12 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Steering
/// <inheritdoc />
public bool RequiresInRangeUnobstructed { get; }
/// <summary>
/// To avoid spamming InRangeUnobstructed we'll apply a cd to it.
/// </summary>
public float TimeUntilInteractionCheck { get; set; }
public EntityTargetSteeringRequest(IEntity target, float arrivalDistance, float pathfindingProximity = 0.5f, bool requiresInRangeUnobstructed = false)
{
_target = target;
@@ -38,4 +38,4 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Steering
RequiresInRangeUnobstructed = requiresInRangeUnobstructed;
}
}
}
}

View File

@@ -1,3 +1,4 @@
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.Interfaces.Map;
using Robust.Shared.IoC;
using Robust.Shared.Map;
@@ -8,7 +9,7 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Steering
{
public SteeringStatus Status { get; set; } = SteeringStatus.Pending;
public MapCoordinates TargetMap { get; }
public GridCoordinates TargetGrid { get; }
public EntityCoordinates TargetGrid { get; }
/// <inheritdoc />
public float ArrivalDistance { get; }
/// <inheritdoc />
@@ -19,15 +20,15 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Steering
public float TimeUntilInteractionCheck { get; set; } = 0.0f;
public GridTargetSteeringRequest(GridCoordinates targetGrid, float arrivalDistance, float pathfindingProximity = 0.5f, bool requiresInRangeUnobstructed = false)
public GridTargetSteeringRequest(EntityCoordinates targetGrid, float arrivalDistance, float pathfindingProximity = 0.5f, bool requiresInRangeUnobstructed = false)
{
// Get it once up front so we the manager doesn't have to continuously get it
var mapManager = IoCManager.Resolve<IMapManager>();
TargetMap = targetGrid.ToMap(mapManager);
var entityManager = IoCManager.Resolve<IEntityManager>();
TargetMap = targetGrid.ToMap(entityManager);
TargetGrid = targetGrid;
ArrivalDistance = arrivalDistance;
PathfindingProximity = pathfindingProximity;
RequiresInRangeUnobstructed = requiresInRangeUnobstructed;
}
}
}
}

View File

@@ -6,25 +6,25 @@ namespace Content.Server.GameObjects.EntitySystems.AI.Steering
{
SteeringStatus Status { get; set; }
MapCoordinates TargetMap { get; }
GridCoordinates TargetGrid { get; }
EntityCoordinates TargetGrid { get; }
/// <summary>
/// How close we have to get before we've arrived
/// </summary>
float ArrivalDistance { get; }
/// <summary>
/// How close the pathfinder needs to get. Typically you want this set lower than ArrivalDistance
/// </summary>
float PathfindingProximity { get; }
/// <summary>
/// If we need LOS on the entity first before interaction
/// </summary>
bool RequiresInRangeUnobstructed { get; }
/// <summary>
/// To avoid spamming InRangeUnobstructed we'll apply a cd to it.
/// </summary>
public float TimeUntilInteractionCheck { get; set; }
}
}
}