Add NPC stuck detection (#14410)

This commit is contained in:
metalgearsloth
2023-03-05 16:13:09 +11:00
committed by GitHub
parent bb4d5064ad
commit 28dbbbb734
3 changed files with 127 additions and 70 deletions

View File

@@ -45,7 +45,8 @@ public sealed partial class NPCSteeringSystem
float moveSpeed,
float[] interest,
EntityQuery<PhysicsComponent> bodyQuery,
float frameTime)
float frameTime,
ref bool forceSteer)
{
var ourCoordinates = xform.Coordinates;
var destinationCoordinates = steering.Coordinates;
@@ -72,6 +73,7 @@ public sealed partial class NPCSteeringSystem
// Try to get the next node temporarily.
targetCoordinates = GetTargetCoordinates(steering);
needsPath = true;
ResetStuck(steering, ourCoordinates);
}
}
@@ -84,14 +86,22 @@ public sealed partial class NPCSteeringSystem
// If it's a pathfinding node it might be different to the destination.
arrivalDistance = steering.Range;
}
// If next node is a free tile then get within its bounds.
// This is to avoid popping it too early
else if (steering.CurrentPath.TryPeek(out var node) && node.Data.IsFreeSpace)
{
arrivalDistance = MathF.Min(node.Box.Width, node.Box.Height) - 0.01f;
}
// Try getting into blocked range I guess?
// TODO: Consider melee range or the likes.
else
{
arrivalDistance = SharedInteractionSystem.InteractionRange - 0.65f;
}
// Check if mapids match.
var targetMap = targetCoordinates.ToMap(EntityManager);
var ourMap = ourCoordinates.ToMap(EntityManager);
var targetMap = targetCoordinates.ToMap(EntityManager, _transform);
var ourMap = ourCoordinates.ToMap(EntityManager, _transform);
if (targetMap.MapId != ourMap.MapId)
{
@@ -107,6 +117,8 @@ public sealed partial class NPCSteeringSystem
// Node needs some kind of special handling like access or smashing.
if (steering.CurrentPath.TryPeek(out var node) && !node.Data.IsFreeSpace)
{
// Ignore stuck while handling obstacles.
ResetStuck(steering, ourCoordinates);
SteeringObstacleStatus status;
// Breaking behaviours and the likes.
@@ -125,44 +137,68 @@ public sealed partial class NPCSteeringSystem
steering.Status = SteeringStatus.NoPath;
return false;
case SteeringObstacleStatus.Continuing:
CheckPath(steering, xform, needsPath, distance);
CheckPath(uid, steering, xform, needsPath, distance);
return true;
default:
throw new ArgumentOutOfRangeException();
}
}
// Otherwise it's probably regular pathing so just keep going a bit more to get to tile centre
if (direction.Length <= TileTolerance)
// Distance should already be handled above.
// It was just a node, not the target, so grab the next destination (either the target or next node).
if (steering.CurrentPath.Count > 0)
{
// It was just a node, not the target, so grab the next destination (either the target or next node).
if (steering.CurrentPath.Count > 0)
forceSteer = true;
steering.CurrentPath.Dequeue();
// Alright just adjust slightly and grab the next node so we don't stop moving for a tick.
// TODO: If it's the last node just grab the target instead.
targetCoordinates = GetTargetCoordinates(steering);
targetMap = targetCoordinates.ToMap(EntityManager, _transform);
// Can't make it again.
if (ourMap.MapId != targetMap.MapId)
{
steering.CurrentPath.Dequeue();
// Alright just adjust slightly and grab the next node so we don't stop moving for a tick.
// TODO: If it's the last node just grab the target instead.
targetCoordinates = GetTargetCoordinates(steering);
targetMap = targetCoordinates.ToMap(EntityManager);
// Can't make it again.
if (ourMap.MapId != targetMap.MapId)
{
SetDirection(mover, steering, Vector2.Zero);
steering.Status = SteeringStatus.NoPath;
return false;
}
// Gonna resume now business as usual
direction = targetMap.Position - ourMap.Position;
}
else
{
// This probably shouldn't happen as we check above but eh.
SetDirection(mover, steering, Vector2.Zero);
steering.Status = SteeringStatus.NoPath;
return false;
}
// Gonna resume now business as usual
direction = targetMap.Position - ourMap.Position;
ResetStuck(steering, ourCoordinates);
}
else
{
// This probably shouldn't happen as we check above but eh.
steering.Status = SteeringStatus.NoPath;
return false;
}
}
// Stuck detection
// Check if we have moved further than the movespeed * stuck time.
else if (ourCoordinates.TryDistance(EntityManager, steering.LastStuckCoordinates, out var stuckDistance) &&
stuckDistance < NPCSteeringComponent.StuckDistance)
{
var stuckTime = _timing.CurTime - steering.LastStuckTime;
// Either 1 second or how long it takes to move the stuck distance + buffer if we're REALLY slow.
var maxStuckTime = Math.Max(1, NPCSteeringComponent.StuckDistance / moveSpeed * 1.2f);
if (stuckTime.TotalSeconds > maxStuckTime)
{
// TODO: Blacklist nodes (pathfinder factor wehn)
// TODO: This should be a warning but
// A) NPCs get stuck on non-anchored static bodies still (e.g. closets)
// B) NPCs still try to move in locked containers (e.g. cow, hamster)
// and I don't want to spam grafana even harder than it gets spammed rn.
_sawmill.Debug($"NPC {ToPrettyString(uid)} found stuck at {ourCoordinates}");
steering.Status = SteeringStatus.NoPath;
return false;
}
}
else
{
ResetStuck(steering, ourCoordinates);
}
// Do we have no more nodes to follow OR has the target moved sufficiently? If so then re-path.
@@ -172,7 +208,7 @@ public sealed partial class NPCSteeringSystem
}
// TODO: Probably need partial planning support i.e. patch from the last node to where the target moved to.
CheckPath(steering, xform, needsPath, distance);
CheckPath(uid, steering, xform, needsPath, distance);
// If we don't have a path yet then do nothing; this is to avoid stutter-stepping if it turns out there's no path
// available but we assume there was.
@@ -198,17 +234,22 @@ public sealed partial class NPCSteeringSystem
// Prefer our current direction
if (weight > 0f && body.LinearVelocity.LengthSquared > 0f)
{
const float SameDirectionWeight = 0.1f;
const float sameDirectionWeight = 0.1f;
norm = body.LinearVelocity.Normalized;
ApplySeek(interest, norm, SameDirectionWeight);
ApplySeek(interest, norm, sameDirectionWeight);
}
return true;
}
private void ResetStuck(NPCSteeringComponent component, EntityCoordinates ourCoordinates)
{
component.LastStuckCoordinates = ourCoordinates;
component.LastStuckTime = _timing.CurTime;
}
private void CheckPath(NPCSteeringComponent steering, TransformComponent xform, bool needsPath, float targetDistance)
private void CheckPath(EntityUid uid, NPCSteeringComponent steering, TransformComponent xform, bool needsPath, float targetDistance)
{
if (!_pathfinding)
{
@@ -233,7 +274,7 @@ public sealed partial class NPCSteeringSystem
// Request the new path.
if (needsPath)
{
RequestPath(steering, xform, targetDistance);
RequestPath(uid, steering, xform, targetDistance);
}
}
@@ -253,7 +294,7 @@ public sealed partial class NPCSteeringSystem
if (!node.Data.IsFreeSpace)
break;
var nodeMap = node.Coordinates.ToMap(EntityManager);
var nodeMap = node.Coordinates.ToMap(EntityManager, _transform);
// If any nodes are 'behind us' relative to the target we'll prune them.
// This isn't perfect but should fix most cases of stutter stepping.
@@ -311,7 +352,6 @@ public sealed partial class NPCSteeringSystem
Angle offsetRot,
Vector2 worldPos,
float agentRadius,
float moveSpeed,
int layer,
int mask,
TransformComponent xform,
@@ -414,7 +454,7 @@ public sealed partial class NPCSteeringSystem
var xformB = xformQuery.GetComponent(ent);
if (!_physics.TryGetNearestPoints(uid, ent, out var pointA, out var pointB, xform, xformB))
if (!_physics.TryGetNearestPoints(uid, ent, out _, out var pointB, xform, xformB))
{
continue;
}