More NPC steering tweaks (#14119)

This commit is contained in:
metalgearsloth
2023-02-15 16:54:06 +11:00
committed by GitHub
parent 781ec8e9eb
commit 99dfe5694a
5 changed files with 49 additions and 12 deletions

View File

@@ -36,6 +36,15 @@ public sealed class NPCSteeringComponent : Component
#endregion #endregion
/// <summary>
/// Next time we can change our steering direction.
/// </summary>
public TimeSpan NextSteer = TimeSpan.Zero;
public Vector2 LastSteerDirection = Vector2.Zero;
public const int SteeringFrequency = 10;
/// <summary> /// <summary>
/// Have we currently requested a path. /// Have we currently requested a path.
/// </summary> /// </summary>

View File

@@ -32,10 +32,10 @@ public sealed partial class NPCCombatSystem
if (cdRemaining < TimeSpan.FromSeconds(1f / weapon.AttackRate) * 0.5f) if (cdRemaining < TimeSpan.FromSeconds(1f / weapon.AttackRate) * 0.5f)
return; return;
if (!_physics.TryGetNearestPoints(uid, component.Target, out _, out var pointB)) if (!_physics.TryGetNearestPoints(uid, component.Target, out var pointA, out var pointB))
return; return;
var idealDistance = weapon.Range * 1.25f; var idealDistance = weapon.Range * 1.5f;
var obstacleDirection = pointB - args.WorldPosition; var obstacleDirection = pointB - args.WorldPosition;
var obstacleDistance = obstacleDirection.Length; var obstacleDistance = obstacleDirection.Length;

View File

@@ -320,7 +320,7 @@ public sealed partial class NPCSteeringSystem
EntityQuery<PhysicsComponent> bodyQuery, EntityQuery<PhysicsComponent> bodyQuery,
EntityQuery<TransformComponent> xformQuery) EntityQuery<TransformComponent> xformQuery)
{ {
var detectionRadius = MathF.Max(1.5f, agentRadius + moveSpeed / 4f); var detectionRadius = MathF.Max(1.5f, agentRadius);
foreach (var ent in _lookup.GetEntitiesInRange(uid, detectionRadius, LookupFlags.Static)) foreach (var ent in _lookup.GetEntitiesInRange(uid, detectionRadius, LookupFlags.Static))
{ {
@@ -338,12 +338,24 @@ public sealed partial class NPCSteeringSystem
if (!_physics.TryGetNearestPoints(uid, ent, out var pointA, out var pointB, xform, xformQuery.GetComponent(ent))) if (!_physics.TryGetNearestPoints(uid, ent, out var pointA, out var pointB, xform, xformQuery.GetComponent(ent)))
continue; continue;
var obstacleDirection = pointB - worldPos; var obstacleDirection = pointB - pointA;
var obstableDistance = obstacleDirection.Length; var obstableDistance = obstacleDirection.Length;
if (obstableDistance > detectionRadius || obstableDistance == 0f) if (obstableDistance > detectionRadius)
continue; continue;
// Fallback to worldpos if we're colliding.
if (obstableDistance == 0f)
{
obstacleDirection = pointB - worldPos;
obstableDistance = obstacleDirection.Length;
if (obstableDistance == 0f)
continue;
obstableDistance = agentRadius;
}
dangerPoints.Add(pointB); dangerPoints.Add(pointB);
obstacleDirection = offsetRot.RotateVec(obstacleDirection); obstacleDirection = offsetRot.RotateVec(obstacleDirection);
var norm = obstacleDirection.Normalized; var norm = obstacleDirection.Normalized;

View File

@@ -206,16 +206,19 @@ namespace Content.Server.NPC.Systems
var npcs = EntityQuery<ActiveNPCComponent, NPCSteeringComponent, InputMoverComponent, TransformComponent>() var npcs = EntityQuery<ActiveNPCComponent, NPCSteeringComponent, InputMoverComponent, TransformComponent>()
.ToArray(); .ToArray();
// Dependency issues across threads.
var options = new ParallelOptions var options = new ParallelOptions
{ {
MaxDegreeOfParallelism = _parallel.ParallelProcessCount, MaxDegreeOfParallelism = 1,
}; };
var curTime = _timing.CurTime;
Parallel.For(0, npcs.Length, options, i => Parallel.For(0, npcs.Length, options, i =>
{ {
var (_, steering, mover, xform) = npcs[i]; var (_, steering, mover, xform) = npcs[i];
Steer(steering, mover, xform, modifierQuery, bodyQuery, xformQuery, frameTime); Steer(steering, mover, xform, modifierQuery, bodyQuery, xformQuery, frameTime, curTime);
}); });
@@ -262,7 +265,8 @@ namespace Content.Server.NPC.Systems
EntityQuery<MovementSpeedModifierComponent> modifierQuery, EntityQuery<MovementSpeedModifierComponent> modifierQuery,
EntityQuery<PhysicsComponent> bodyQuery, EntityQuery<PhysicsComponent> bodyQuery,
EntityQuery<TransformComponent> xformQuery, EntityQuery<TransformComponent> xformQuery,
float frameTime) float frameTime,
TimeSpan curTime)
{ {
if (Deleted(steering.Coordinates.EntityId)) if (Deleted(steering.Coordinates.EntityId))
{ {
@@ -355,6 +359,18 @@ namespace Content.Server.NPC.Systems
resultDirection = new Angle(desiredDirection * InterestRadians).ToVec(); resultDirection = new Angle(desiredDirection * InterestRadians).ToVec();
} }
// Don't steer too frequently to avoid twitchiness.
// This should also implicitly solve tie situations.
// I think doing this after all the ops above is best?
// Originally I had it way above but sometimes mobs would overshoot their tile targets.
if (steering.NextSteer > curTime)
{
SetDirection(mover, steering, steering.LastSteerDirection, false);
return;
}
steering.NextSteer = curTime + TimeSpan.FromSeconds(1f / NPCSteeringComponent.SteeringFrequency);
steering.LastSteerDirection = resultDirection;
DebugTools.Assert(!float.IsNaN(resultDirection.X)); DebugTools.Assert(!float.IsNaN(resultDirection.X));
SetDirection(mover, steering, resultDirection, false); SetDirection(mover, steering, resultDirection, false);
} }

View File

@@ -18,7 +18,7 @@
fixtures: fixtures:
- shape: - shape:
!type:PhysShapeAabb !type:PhysShapeAabb
bounds: "-0.49,-0.49,0.49,-0.45" bounds: "-0.49,-0.49,0.49,-0.25"
density: 1000 density: 1000
mask: mask:
- TableMask - TableMask
@@ -70,7 +70,7 @@
fixtures: fixtures:
- shape: - shape:
!type:PhysShapeAabb !type:PhysShapeAabb
bounds: "-0.49,-0.49,0.49,-0.45" bounds: "-0.49,-0.49,0.49,-0.25"
density: 1000 density: 1000
mask: mask:
- TableMask - TableMask
@@ -78,7 +78,7 @@
- TableLayer - TableLayer
- shape: - shape:
!type:PhysShapeAabb !type:PhysShapeAabb
bounds: "0.49,0.49,0.45,-0.49" bounds: "0.49,0.49,0.25,-0.49"
density: 1000 density: 1000
mask: mask:
- TableMask - TableMask
@@ -130,7 +130,7 @@
fixtures: fixtures:
- shape: - shape:
!type:PhysShapeAabb !type:PhysShapeAabb
bounds: "-0.49,0.49,-0.45,0.45" bounds: "-0.49,0.49,-0.25,0.25"
density: 1000 density: 1000
mask: mask:
- TableMask - TableMask