Pin the agent steering writes and the helm rules Soul found unpinned

Holding heading after a state stops piloting (SteeringTests, and a Minion that
loses its target), Combat leading a moving target, MoveTo's Aim and Turn in the
low-delta regime, the behind-the-hull threshold, the Turn clamp, and the hold
branch clearing a stale MovementDirection.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-10-07 16:23:56 +02:00
commit b0d7ccb4f8
2 changed files with 159 additions and 1 deletions

View file

@ -176,7 +176,7 @@ public sealed class FireControlTests : IDisposable
}
// A shooter with one in-range, in-arc gun, a detected target at `targetPosition`, and a Minion driving the shooter.
private (Ship shooter, Ship target, Zone zone) BuildCombatEncounter(float3 targetPosition)
private (Ship shooter, Ship target, Zone zone) BuildCombatEncounter(float3 targetPosition, float projectileSpeed = 0f)
{
var (items, shooter, weapons) = BuildShipWithHardpoints((ItemRotation.None, 0f));
var weaponData = (InstantWeaponData) weapons[0].Data.Behaviors.Single(b => b is InstantWeaponData);
@ -184,6 +184,7 @@ public sealed class FireControlTests : IDisposable
weaponData.Cooldown = new PerformanceStat { Min = 1, Max = 1 };
weaponData.Range = new PerformanceStat { Min = 1000, Max = 1000 };
weaponData.MinRange = new PerformanceStat { Min = 0, Max = 0 };
weaponData.Velocity = new PerformanceStat { Min = projectileSpeed, Max = projectileSpeed };
weaponData.DamageCurve = new BezierCurve { Keys = new[] { float4(0, 1, 0, 0), float4(1, 1, 0, 0) } };
var zone = shooter.Zone;
@ -254,4 +255,45 @@ public sealed class FireControlTests : IDisposable
Assert.Equal(Steering.Toward(shooter, shooter.Aim.xz), shooter.Turn);
Assert.True(abs(shooter.Turn) > .5f);
}
// Against a target that moves, Combat aims at where the round will meet it (FireControl.PredictedIntercept),
// not where the target is: the aim leads, and the hull's Turn follows the intercept bearing.
[Fact]
public void CombatLeadsAMovingTarget()
{
var (shooter, target, zone) = BuildCombatEncounter(float3(0, 0, 100), projectileSpeed: 20f);
zone.Update(1f); // transitions the Minion into CombatState and evaluates weapon stats
// Agents update before entities, so Combat reads exactly this state on the next tick.
target.Position = float3(0, 0, 100);
target.Velocity = float2(4, 0);
var weapon = shooter.Weapons.Single();
var intercept = normalize((FireControl.PredictedIntercept(weapon, shooter, target) - shooter.Position).xz);
var bearing = normalize(target.Position.xz - shooter.Position.xz);
Assert.True(dot(intercept, bearing) < cos(radians(5f)), "the fixture really leads the target");
zone.Update(1f);
var aim = normalize(shooter.Aim.xz);
Assert.True(dot(aim, intercept) > .9999f, $"aim {shooter.Aim}");
Assert.Equal(Steering.Toward(shooter, intercept), shooter.Turn);
Assert.True(shooter.Turn > 0f);
}
// A Minion whose target goes away returns to its root state, which pilots nothing: the hull must stop
// turning rather than keep the rate Combat last commanded.
[Fact]
public void AMinionThatLosesItsTargetStopsTurning()
{
var (shooter, _, zone) = BuildCombatEncounter(float3(10, 0, 10));
zone.Update(1f);
zone.Update(1f);
Assert.True(abs(shooter.Turn) > .5f, "Combat commanded a turn");
shooter.SetTarget(TargetRef.None);
zone.Update(1f); // Combat has no target and the Minion transitions to its root state
zone.Update(1f);
Assert.Equal(0f, shooter.Turn);
}
}

View file

@ -195,6 +195,7 @@ public sealed class SteeringTests
Assert.Equal(float2(0, 0), ship.MovementDirection);
ship.Turn = .37f;
ship.MovementDirection = float2(1, 1); // a stale command the hold branch must clear
agent.Accelerate(float2(1, 0), false); // exactly the thrust threshold: not above it
Assert.Equal(float2.zero, ship.MovementDirection);
Assert.Equal(.37f, ship.Turn);
@ -218,4 +219,119 @@ public sealed class SteeringTests
Assert.Equal(.37f, ship.Turn);
Assert.Equal(float2.zero, ship.MovementDirection);
}
// Behind the hull is "dot < 0": 100 degrees off the nose is already behind, so it turns at full demand, and
// 80 degrees is not, so it is the shaped lateral component.
[Fact]
public void TheBehindRuleBeginsPastNinetyDegrees()
{
using var cache = RestoredHullsTests.OpenCatalog();
var ship = RestoredHullsTests.BuildThrustedShip(cache, Hull);
Assert.Equal(1f, Steering.Toward(ship, Heading(ship, 100f)));
Assert.Equal(-1f, Steering.Toward(ship, Heading(ship, -100f)));
Assert.Equal(1f, Steering.Toward(ship, Heading(ship, 135f)));
Assert.Equal(sqrt(sin(radians(80f))), Steering.Toward(ship, Heading(ship, 80f)), 4);
Assert.Equal(-sqrt(sin(radians(80f))), Steering.Toward(ship, Heading(ship, -80f)), 4);
}
// Ship.Update clamps the command to -1..1: a Turn of 3 rotates the hull exactly as a Turn of 1 does.
[Fact]
public void TurnIsClampedToOne()
{
using var cache = RestoredHullsTests.OpenCatalog();
var one = RestoredHullsTests.BuildThrustedShip(cache, Hull);
var three = RestoredHullsTests.BuildThrustedShip(cache, Hull);
var minusOne = RestoredHullsTests.BuildThrustedShip(cache, Hull);
var minusThree = RestoredHullsTests.BuildThrustedShip(cache, Hull);
one.Turn = 1;
three.Turn = 3;
minusOne.Turn = -1;
minusThree.Turn = -3;
for (var i = 0; i < 10; i++)
{
one.Update(1f / 60f);
three.Update(1f / 60f);
minusOne.Update(1f / 60f);
minusThree.Update(1f / 60f);
}
Assert.True(Angle(one.Direction, Heading(one, 0f)) > radians(.05f), "the reference turn is real");
Assert.True(Angle(one.Direction, three.Direction) < radians(.001f), $"{Angle(one.Direction, three.Direction)}");
Assert.True(Angle(minusOne.Direction, minusThree.Direction) < radians(.001f));
}
// A state that writes a constant turn, standing for any piloting state.
private sealed class SpinState : BaseState
{
public SpinState(Agent agent) : base(agent) { }
public override void Update(float delta) => _agent.Ship.Turn = .5f;
}
// Turn is a rate, so a state that stops piloting must not leave the last state's rate behind: the agent's
// tick writes Turn every tick, zero when its state demands none, and the hull then holds its heading.
[Fact]
public void AnAgentWhoseStateStopsPilotingHoldsItsHeading()
{
using var cache = RestoredHullsTests.OpenCatalog();
var ship = RestoredHullsTests.BuildThrustedShip(cache, Hull);
var agent = new Agent(ship);
var start = ship.Direction;
agent.Transition(new SpinState(agent));
for (var i = 0; i < 10; i++)
{
agent.Update(1f / 60f);
ship.Update(1f / 60f);
}
Assert.True(Angle(start, ship.Direction) > radians(.05f), "the piloting state turns the hull");
Assert.Equal(.5f, ship.Turn);
agent.Transition(new BaseState(agent));
agent.Update(1f / 60f);
ship.Update(1f / 60f);
var held = ship.Direction;
for (var i = 0; i < 60; i++)
{
agent.Update(1f / 60f);
ship.Update(1f / 60f);
}
Assert.Equal(0f, ship.Turn);
Assert.True(Angle(held, ship.Direction) < radians(.01f), $"{Angle(held, ship.Direction)}");
}
private sealed class MoveToPointState : MoveToState
{
public float2 Point;
public MoveToPointState(Agent agent) : base(agent) { }
protected override float2 TargetPosition => Point;
}
// MoveTo aims at its target and commands the hull toward it. The ship already flies the wanted velocity, so
// Accelerate (which leaves Turn to the caller below its thresholds) writes none of it: what is measured is
// MoveTo's own write, to either side.
[Theory]
[InlineData(90f)]
[InlineData(-45f)]
public void MoveToAimsAtTheTargetAndTurnsTowardIt(float degrees)
{
using var cache = RestoredHullsTests.OpenCatalog();
var ship = RestoredHullsTests.BuildThrustedShip(cache, Hull);
var agent = new Agent(ship);
var state = new MoveToPointState(agent);
var dir = Heading(ship, degrees);
state.Point = dir * 500f;
ship.Velocity = dir * agent.TopSpeed; // deltaV is zero: Accelerate holds and leaves Turn alone
agent.Transition(state);
agent.Update(1f / 60f);
Assert.True(dot(normalize(ship.Aim.xz), dir) > .9999f, $"aim {ship.Aim}");
Assert.True(abs(ship.Aim.y) < 1e-6f);
Assert.Equal(Steering.Toward(ship, dir), ship.Turn);
Assert.True(abs(ship.Turn) > .5f);
Assert.Equal(degrees > 0 ? 1f : -1f, sign(ship.Turn));
}
}