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:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user