#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>
/// Have we currently requested a path.
/// </summary>
if (cdRemaining < TimeSpan.FromSeconds(1f / weapon.AttackRate) * 0.5f)
return;
- if (!_physics.TryGetNearestPoints(uid, component.Target, out _, out var pointB))
+ if (!_physics.TryGetNearestPoints(uid, component.Target, out var pointA, out var pointB))
return;
- var idealDistance = weapon.Range * 1.25f;
+ var idealDistance = weapon.Range * 1.5f;
var obstacleDirection = pointB - args.WorldPosition;
var obstacleDistance = obstacleDirection.Length;
EntityQuery<PhysicsComponent> bodyQuery,
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))
{
if (!_physics.TryGetNearestPoints(uid, ent, out var pointA, out var pointB, xform, xformQuery.GetComponent(ent)))
continue;
- var obstacleDirection = pointB - worldPos;
+ var obstacleDirection = pointB - pointA;
var obstableDistance = obstacleDirection.Length;
- if (obstableDistance > detectionRadius || obstableDistance == 0f)
+ if (obstableDistance > detectionRadius)
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);
obstacleDirection = offsetRot.RotateVec(obstacleDirection);
var norm = obstacleDirection.Normalized;
var npcs = EntityQuery<ActiveNPCComponent, NPCSteeringComponent, InputMoverComponent, TransformComponent>()
.ToArray();
+
+ // Dependency issues across threads.
var options = new ParallelOptions
{
- MaxDegreeOfParallelism = _parallel.ParallelProcessCount,
+ MaxDegreeOfParallelism = 1,
};
+ var curTime = _timing.CurTime;
Parallel.For(0, npcs.Length, options, i =>
{
var (_, steering, mover, xform) = npcs[i];
- Steer(steering, mover, xform, modifierQuery, bodyQuery, xformQuery, frameTime);
+ Steer(steering, mover, xform, modifierQuery, bodyQuery, xformQuery, frameTime, curTime);
});
EntityQuery<MovementSpeedModifierComponent> modifierQuery,
EntityQuery<PhysicsComponent> bodyQuery,
EntityQuery<TransformComponent> xformQuery,
- float frameTime)
+ float frameTime,
+ TimeSpan curTime)
{
if (Deleted(steering.Coordinates.EntityId))
{
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));
SetDirection(mover, steering, resultDirection, false);
}
fixtures:
- shape:
!type:PhysShapeAabb
- bounds: "-0.49,-0.49,0.49,-0.45"
+ bounds: "-0.49,-0.49,0.49,-0.25"
density: 1000
mask:
- TableMask
fixtures:
- shape:
!type:PhysShapeAabb
- bounds: "-0.49,-0.49,0.49,-0.45"
+ bounds: "-0.49,-0.49,0.49,-0.25"
density: 1000
mask:
- TableMask
- TableLayer
- shape:
!type:PhysShapeAabb
- bounds: "0.49,0.49,0.45,-0.49"
+ bounds: "0.49,0.49,0.25,-0.49"
density: 1000
mask:
- TableMask
fixtures:
- shape:
!type:PhysShapeAabb
- bounds: "-0.49,0.49,-0.45,0.45"
+ bounds: "-0.49,0.49,-0.25,0.25"
density: 1000
mask:
- TableMask