NPCs can open doors (#7909)
This commit is contained in:
@@ -172,11 +172,12 @@ namespace Content.Server.AI.Pathfinding.Accessible
|
||||
/// <returns></returns>
|
||||
public bool CanAccess(EntityUid entity, EntityUid target, float range = 0.0f)
|
||||
{
|
||||
var xform = EntityManager.GetComponent<TransformComponent>(target);
|
||||
// TODO: Handle this gracefully instead of just failing.
|
||||
if (!EntityManager.GetComponent<TransformComponent>(target).GridID.IsValid())
|
||||
if (!xform.GridID.IsValid())
|
||||
return false;
|
||||
|
||||
var targetTile = _mapManager.GetGrid(EntityManager.GetComponent<TransformComponent>(target).GridID).GetTileRef(EntityManager.GetComponent<TransformComponent>(target).Coordinates);
|
||||
var targetTile = _mapManager.GetGrid(xform.GridID).GetTileRef(xform.Coordinates);
|
||||
var targetNode = _pathfindingSystem.GetNode(targetTile);
|
||||
|
||||
var collisionMask = 0;
|
||||
@@ -209,12 +210,12 @@ namespace Content.Server.AI.Pathfinding.Accessible
|
||||
|
||||
public bool CanAccess(EntityUid entity, PathfindingNode targetNode)
|
||||
{
|
||||
if (EntityManager.GetComponent<TransformComponent>(entity).GridID != targetNode.TileRef.GridIndex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
var xform = EntityManager.GetComponent<TransformComponent>(entity);
|
||||
|
||||
var entityTile = _mapManager.GetGrid(EntityManager.GetComponent<TransformComponent>(entity).GridID).GetTileRef(EntityManager.GetComponent<TransformComponent>(entity).Coordinates);
|
||||
if (xform.GridID != targetNode.TileRef.GridIndex)
|
||||
return false;
|
||||
|
||||
var entityTile = _mapManager.GetGrid(xform.GridID).GetTileRef(xform.Coordinates);
|
||||
var entityNode = _pathfindingSystem.GetNode(entityTile);
|
||||
var entityRegion = GetRegion(entityNode);
|
||||
var targetRegion = GetRegion(targetNode);
|
||||
@@ -424,12 +425,14 @@ namespace Content.Server.AI.Pathfinding.Accessible
|
||||
/// <returns></returns>
|
||||
public PathfindingRegion? GetRegion(EntityUid entity)
|
||||
{
|
||||
if (!EntityManager.GetComponent<TransformComponent>(entity).GridID.IsValid())
|
||||
var xform = EntityManager.GetComponent<TransformComponent>(entity);
|
||||
|
||||
if (!xform.GridID.IsValid())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var entityTile = _mapManager.GetGrid(EntityManager.GetComponent<TransformComponent>(entity).GridID).GetTileRef(EntityManager.GetComponent<TransformComponent>(entity).Coordinates);
|
||||
var entityTile = _mapManager.GetGrid(xform.GridID).GetTileRef(xform.Coordinates);
|
||||
var entityNode = _pathfindingSystem.GetNode(entityTile);
|
||||
return GetRegion(entityNode);
|
||||
}
|
||||
|
||||
@@ -30,7 +30,6 @@ namespace Content.Server.AI.Pathfinding
|
||||
public sealed class PathfindingSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IMapManager _mapManager = default!;
|
||||
[Dependency] private readonly IEntityManager _entityManager = default!;
|
||||
[Dependency] private readonly AccessReaderSystem _accessReader = default!;
|
||||
|
||||
public IReadOnlyDictionary<GridId, Dictionary<Vector2i, PathfindingChunk>> Graph => _graph;
|
||||
@@ -207,12 +206,11 @@ namespace Content.Server.AI.Pathfinding
|
||||
SubscribeLocalEvent<MoveEvent>(QueueMoveEvent);
|
||||
SubscribeLocalEvent<AccessReaderChangeMessage>(QueueAccessChangeMessage);
|
||||
SubscribeLocalEvent<GridRemovalEvent>(HandleGridRemoval);
|
||||
SubscribeLocalEvent<GridModifiedEvent>(QueueGridChange);
|
||||
SubscribeLocalEvent<TileChangedEvent>(QueueTileChange);
|
||||
|
||||
// Handle all the base grid changes
|
||||
// Anything that affects traversal (i.e. collision layer) is handled separately.
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void HandleTileUpdate(TileRef tile)
|
||||
@@ -231,14 +229,6 @@ namespace Content.Server.AI.Pathfinding
|
||||
}
|
||||
}
|
||||
|
||||
private void QueueGridChange(GridModifiedEvent ev)
|
||||
{
|
||||
foreach (var (position, _) in ev.Modified)
|
||||
{
|
||||
_tileUpdateQueue.Enqueue(ev.Grid.GetTileRef(position));
|
||||
}
|
||||
}
|
||||
|
||||
private void QueueTileChange(TileChangedEvent ev)
|
||||
{
|
||||
_tileUpdateQueue.Enqueue(ev.NewTile);
|
||||
@@ -264,8 +254,9 @@ namespace Content.Server.AI.Pathfinding
|
||||
return;
|
||||
}
|
||||
|
||||
var grid = _mapManager.GetGrid(EntityManager.GetComponent<TransformComponent>(entity).GridID);
|
||||
var tileRef = grid.GetTileRef(EntityManager.GetComponent<TransformComponent>(entity).Coordinates);
|
||||
var xform = EntityManager.GetComponent<TransformComponent>(entity);
|
||||
var grid = _mapManager.GetGrid(xform.GridID);
|
||||
var tileRef = grid.GetTileRef(xform.Coordinates);
|
||||
|
||||
var chunk = GetChunk(tileRef);
|
||||
var node = chunk.GetNode(tileRef);
|
||||
@@ -306,9 +297,10 @@ namespace Content.Server.AI.Pathfinding
|
||||
}
|
||||
|
||||
// Memory leak protection until grid parenting confirmed fix / you REALLY need the performance
|
||||
var gridBounds = _mapManager.GetGrid(EntityManager.GetComponent<TransformComponent>(moveEvent.Sender).GridID).WorldBounds;
|
||||
var xform = EntityManager.GetComponent<TransformComponent>(moveEvent.Sender);
|
||||
var gridBounds = _mapManager.GetGrid(xform.GridID).WorldBounds;
|
||||
|
||||
if (!gridBounds.Contains(EntityManager.GetComponent<TransformComponent>(moveEvent.Sender).WorldPosition))
|
||||
if (!gridBounds.Contains(xform.WorldPosition))
|
||||
{
|
||||
HandleEntityRemove(moveEvent.Sender);
|
||||
return;
|
||||
@@ -321,7 +313,7 @@ namespace Content.Server.AI.Pathfinding
|
||||
return;
|
||||
}
|
||||
|
||||
var newGridId = moveEvent.NewPosition.GetGridId(_entityManager);
|
||||
var newGridId = moveEvent.NewPosition.GetGridId(EntityManager);
|
||||
if (newGridId == GridId.Invalid)
|
||||
{
|
||||
HandleEntityRemove(moveEvent.Sender);
|
||||
|
||||
@@ -5,7 +5,9 @@ using Content.Server.AI.Components;
|
||||
using Content.Server.AI.Pathfinding;
|
||||
using Content.Server.AI.Pathfinding.Pathfinders;
|
||||
using Content.Server.CPUJob.JobQueues;
|
||||
using Content.Server.Doors.Components;
|
||||
using Content.Shared.Access.Systems;
|
||||
using Content.Shared.Doors.Components;
|
||||
using Content.Shared.Interaction;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Physics;
|
||||
@@ -385,6 +387,20 @@ namespace Content.Server.AI.Steering
|
||||
{
|
||||
movementVector += CollisionAvoidance(entity, movementVector, ignoredCollision);
|
||||
}
|
||||
|
||||
// TODO: Jesus this code is shit, slork is a cute dork, but the pathfinder should annotate this.
|
||||
if (_mapManager.TryGetGrid(nextGrid.Value.EntityId, out var grid))
|
||||
{
|
||||
foreach (var ent in grid.GetAnchoredEntities(nextGrid.Value))
|
||||
{
|
||||
if (HasComp<DoorComponent>(ent))
|
||||
{
|
||||
_interactionSystem.InteractHand(entity, ent);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Group behaviors would also go here e.g. separation, cohesion, alignment
|
||||
|
||||
// Move towards it
|
||||
@@ -472,10 +488,12 @@ namespace Content.Server.AI.Steering
|
||||
_nextGrid.Remove(entity);
|
||||
}
|
||||
|
||||
var xform = EntityManager.GetComponent<TransformComponent>(entity);
|
||||
|
||||
// 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 - EntityManager.GetComponent<TransformComponent>(entity).Coordinates.Position).Length <= 2.0f)
|
||||
if ((steeringRequest.TargetGrid.Position - xform.Coordinates.Position).Length <= 2.0f)
|
||||
{
|
||||
return steeringRequest.TargetGrid;
|
||||
}
|
||||
@@ -485,7 +503,7 @@ namespace Content.Server.AI.Steering
|
||||
}
|
||||
|
||||
if (!_nextGrid.TryGetValue(entity, out var nextGrid) ||
|
||||
(nextGrid.Position - EntityManager.GetComponent<TransformComponent>(entity).Coordinates.Position).Length <= TileTolerance)
|
||||
(nextGrid.Position - xform.Coordinates.Position).Length <= TileTolerance)
|
||||
{
|
||||
UpdateGridCache(entity);
|
||||
nextGrid = _nextGrid[entity];
|
||||
|
||||
Reference in New Issue
Block a user