FireControl.RoundEnd states a round's end; DrawAhead draws rounds as pure statics
RoundEnd(shot, known) is the one statement of when a ballistic round's drawn flight ends: arrival for a hit, burst or unpublished outcome, the frozen range (never short of arrival) for a miss. RoundAt takes the known outcome and clamps at RoundEnd. DrawAhead gains Round, RoundStop, RoundOver (pure, no state) so the blend, stop and done rules are headless-testable. Seven new tests (ten cases). Unity build: Projectile.cs still calls the old RoundAt, so this commit does not compile in Unity (the headless test project does not include Projectile). The next commit moves Projectile onto the statics. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
e56f9c8be5
commit
60aacfdc35
3 changed files with 313 additions and 26 deletions
|
|
@ -5,8 +5,9 @@
|
|||
using CultMath;
|
||||
using static CultMath.math;
|
||||
|
||||
// Where a presenter draws the latest sim state when the frame falls Lead sim seconds after the last step. Pure reads
|
||||
// of the simulation for presenters (never called by ServerShared): the sim owns every pose and no presenter stores
|
||||
// Where a presenter draws the latest sim state when the frame falls Lead sim seconds after the last step: ships by
|
||||
// their last step's velocity, ballistic rounds on FireControl's RoundAt and RoundEnd. Pure reads of the simulation for
|
||||
// presenters (never called by ServerShared, no state of their own): the sim owns every pose and no presenter stores
|
||||
// one, a previous state or a velocity of its own.
|
||||
public static class DrawAhead
|
||||
{
|
||||
|
|
@ -37,4 +38,24 @@ public static class DrawAhead
|
|||
t.w * q.z + t.x * q.y - t.y * q.x + t.z * q.w,
|
||||
t.w * q.w - t.x * q.x - t.y * q.y - t.z * q.z));
|
||||
}
|
||||
|
||||
// A ballistic round at a sim time (the clock's Zone.Time + Lead): on FireControl's line for what the sim has published
|
||||
// of its outcome, with the barrel's offset from that line decaying to nothing over blendTime sim seconds from FireTime.
|
||||
public static float3 Round(in PendingShot shot, ShotResult? known, float3 barrel, float blendTime, float time) =>
|
||||
Lifted(shot, FireControl.RoundAt(shot, known, time), barrel, blendTime, time);
|
||||
|
||||
// Where a round that hit or burst stops: the outcome's burst point, else where it arrives on its line.
|
||||
public static float3 RoundStop(in PendingShot shot, ShotOutcome outcome, float3 barrel, float blendTime) =>
|
||||
Lifted(shot, outcome.HasBurstPoint ? outcome.BurstPoint : FireControl.RoundAt(shot, outcome.Result, shot.ArrivalTime),
|
||||
barrel, blendTime, shot.ArrivalTime);
|
||||
|
||||
// Whether the drawn flight is over by time: only a published miss ends by the clock. A hit or burst ends on ShotResolved.
|
||||
public static bool RoundOver(in PendingShot shot, ShotResult? known, float time) =>
|
||||
known == ShotResult.Miss && time >= FireControl.RoundEnd(shot, ShotResult.Miss);
|
||||
|
||||
private static float3 Lifted(in PendingShot shot, float2 planar, float3 barrel, float blendTime, float time)
|
||||
{
|
||||
var blend = blendTime > 0 ? saturate(1 - (time - shot.FireTime) / blendTime) : 0f;
|
||||
return float3(planar.x, shot.FireOrigin.y, planar.y) + (barrel - shot.FireOrigin) * blend;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -514,13 +514,23 @@ public static class FireControl
|
|||
return shot.Target == null ? 1f : PDeviation(deviation, shot.Tracking);
|
||||
}
|
||||
|
||||
// The sim time a ballistic round's drawn flight ends, for what is known of its outcome: the one statement of a round's
|
||||
// end. A round that hit or burst (or whose outcome is not yet published) ends at its ArrivalTime. A round the sim has
|
||||
// published as a Miss flies on to the weapon's frozen range and never ends short of its own arrival (a direct round's
|
||||
// FireRange is measured to any target, in range or not). Pure: it reads the shot alone, and the simulation's own
|
||||
// arithmetic (Step, Commit, Apply) does not call it. Presenters end rounds here, through DrawAhead.
|
||||
public static float RoundEnd(in PendingShot shot, ShotResult? known) =>
|
||||
known == ShotResult.Miss
|
||||
? max(shot.ArrivalTime, shot.FireTime + (shot.Speed > .01f ? shot.MaxRange / shot.Speed : 0f))
|
||||
: shot.ArrivalTime;
|
||||
|
||||
// Where a ballistic round is at a sim time: the one statement of the round's line, from its frozen origin along its
|
||||
// frozen direction at its frozen speed, held at the MaxRange point once it has flown that far. Pure: it reads the
|
||||
// shot alone, and the simulation's own arithmetic (Step, Commit, Apply) does not call it. Presenters draw rounds here.
|
||||
public static float2 RoundAt(in PendingShot shot, float time)
|
||||
// frozen direction at its frozen speed, held at RoundEnd once it has flown that far. With no outcome known the round is
|
||||
// drawn no further than its arrival point and waits there for the sim to publish one. Pure, like RoundEnd.
|
||||
public static float2 RoundAt(in PendingShot shot, ShotResult? known, float time)
|
||||
{
|
||||
if (shot.Speed <= .01f) return shot.FireOrigin.xz;
|
||||
var flown = clamp(time - shot.FireTime, 0f, shot.MaxRange / shot.Speed);
|
||||
var flown = clamp(time - shot.FireTime, 0f, RoundEnd(shot, known) - shot.FireTime);
|
||||
return shot.FireOrigin.xz + shot.TravelDirection * (shot.Speed * flown);
|
||||
}
|
||||
|
||||
|
|
@ -865,6 +875,8 @@ public static class FireControl
|
|||
// A contact burst short of max range arrives when the round has flown that far, not at the frozen
|
||||
// max-range time (CommitBurst). Never later than the frozen time.
|
||||
if (shot.Outcome.Result == ShotResult.Burst) shot.ArrivalTime = min(shot.ArrivalTime, now + shot.Outcome.ArrivalIn);
|
||||
// Written back before ShotCommitted fires: a presenter re-reading the record there (Zone.TryGetShot) sees
|
||||
// the committed ArrivalTime a contact burst has just shortened.
|
||||
shots[i] = shot;
|
||||
zone.ShotCommitted.OnNext(shot.Outcome);
|
||||
}
|
||||
|
|
@ -2013,8 +2025,8 @@ public struct PendingShot
|
|||
public float FireTime;
|
||||
// The weapon's Velocity at Fire: frozen with the rest, so RoundAt states the same line whatever happens to the stat later.
|
||||
public float Speed;
|
||||
// The weapon's Range at Fire, likewise frozen: how far the round can fly, which RoundAt clamps at. FireRange above is the
|
||||
// distance to the engaged target (0 for a round with none), not how far a round that misses flies on.
|
||||
// The weapon's Range at Fire, likewise frozen: how far a round that misses flies on, read only by RoundEnd. FireRange above
|
||||
// is the distance to the engaged target (0 for a round with none), not how far a round that misses flies on.
|
||||
public float MaxRange;
|
||||
public float CommitTime;
|
||||
public float ArrivalTime;
|
||||
|
|
|
|||
|
|
@ -9,9 +9,11 @@ using float2 = CultMath.float2;
|
|||
using float3 = CultMath.float3;
|
||||
|
||||
// Ballistic flight: FireControl.RoundAt is the one statement of where a fired round is at a sim time, from the
|
||||
// PendingShot's frozen origin, direction, speed and fire time. Unity draws rounds there and integrates nothing, so
|
||||
// these tests pin the line itself against first principles (the shooter's position, the aim, the authored velocity)
|
||||
// rather than against the code that computes it. Shares the Cut 12.4 engagement fixture.
|
||||
// PendingShot's frozen origin, direction, speed and fire time, and FireControl.RoundEnd the one statement of when its
|
||||
// drawn flight ends for what the sim has published of its outcome. Unity draws rounds through DrawAhead's round statics
|
||||
// and integrates nothing, so these tests pin the line, the end and the drawing against first principles (the shooter's
|
||||
// position, the aim, the authored velocity and range) rather than against the code that computes them. Shares the
|
||||
// Cut 12.4 engagement fixture.
|
||||
public sealed partial class FireControlCut124Tests
|
||||
{
|
||||
private const float BallisticSpeed = 20f;
|
||||
|
|
@ -38,24 +40,28 @@ public sealed partial class FireControlCut124Tests
|
|||
// The shooter moves on after the round left: the line is the one frozen at Fire, not one drawn from where it is now.
|
||||
e.Shooter.Position += float3(7, 0, 3);
|
||||
|
||||
var start = FireControl.RoundAt(burst, burst.FireTime);
|
||||
var start = FireControl.RoundAt(burst, null, burst.FireTime);
|
||||
Assert.Equal(origin.x, start.x, 3);
|
||||
Assert.Equal(origin.z, start.y, 3);
|
||||
|
||||
var second = FireControl.RoundAt(burst, burst.FireTime + 1f);
|
||||
var second = FireControl.RoundAt(burst, null, burst.FireTime + 1f);
|
||||
Assert.Equal(origin.x + aim.x * BallisticSpeed, second.x, 3);
|
||||
Assert.Equal(origin.z + aim.y * BallisticSpeed, second.y, 3);
|
||||
|
||||
var arrival = FireControl.RoundAt(burst, burst.ArrivalTime);
|
||||
Assert.Equal(burst.BurstPosition.x, arrival.x, 3);
|
||||
Assert.Equal(burst.BurstPosition.z, arrival.y, 3);
|
||||
foreach (ShotResult? known in new ShotResult?[] { null, ShotResult.Burst })
|
||||
foreach (var later in new[] { 0f, 1f, 50f })
|
||||
{
|
||||
var arrival = FireControl.RoundAt(burst, known, burst.ArrivalTime + later);
|
||||
Assert.Equal(burst.BurstPosition.x, arrival.x, 3);
|
||||
Assert.Equal(burst.BurstPosition.z, arrival.y, 3);
|
||||
}
|
||||
|
||||
// Past the weapon's range it is held at the range point, however late it is asked.
|
||||
var held = FireControl.RoundAt(burst, burst.FireTime + NoLockRange / BallisticSpeed + 50f);
|
||||
var held = FireControl.RoundAt(burst, ShotResult.Miss, burst.FireTime + NoLockRange / BallisticSpeed + 50f);
|
||||
Assert.Equal(origin.x + aim.x * NoLockRange, held.x, 3);
|
||||
Assert.Equal(origin.z + aim.y * NoLockRange, held.y, 3);
|
||||
// Before it was fired it is still at the origin, not behind it.
|
||||
var before = FireControl.RoundAt(burst, burst.FireTime - 5f);
|
||||
var before = FireControl.RoundAt(burst, null, burst.FireTime - 5f);
|
||||
Assert.Equal(origin.x, before.x, 3);
|
||||
Assert.Equal(origin.z, before.y, 3);
|
||||
|
||||
|
|
@ -64,11 +70,16 @@ public sealed partial class FireControlCut124Tests
|
|||
var dOrigin = d.Shooter.Position;
|
||||
FireControl.Fire(d.Weapon, d.WeaponItem, d.Shooter);
|
||||
var direct = SafeAssert.OnlyShot(d.Zone);
|
||||
var hit = FireControl.RoundAt(direct, direct.ArrivalTime);
|
||||
Assert.Equal(d.Target.Position.x, hit.x, 2);
|
||||
Assert.Equal(d.Target.Position.z, hit.y, 2);
|
||||
Assert.Equal(dOrigin.z + 100f / 2f, FireControl.RoundAt(direct, direct.FireTime + 2.5f).y, 2);
|
||||
var heldDirect = FireControl.RoundAt(direct, direct.FireTime + 1000f / BallisticSpeed + 99f);
|
||||
foreach (ShotResult? known in new ShotResult?[] { null, ShotResult.Hit })
|
||||
// A round not known to miss waits at its arrival point however late it is asked.
|
||||
foreach (var at in new[] { direct.ArrivalTime, direct.FireTime + 99f })
|
||||
{
|
||||
var hit = FireControl.RoundAt(direct, known, at);
|
||||
Assert.Equal(d.Target.Position.x, hit.x, 2);
|
||||
Assert.Equal(d.Target.Position.z, hit.y, 2);
|
||||
}
|
||||
Assert.Equal(dOrigin.z + 100f / 2f, FireControl.RoundAt(direct, null, direct.FireTime + 2.5f).y, 2);
|
||||
var heldDirect = FireControl.RoundAt(direct, ShotResult.Miss, direct.FireTime + 1000f / BallisticSpeed + 99f);
|
||||
Assert.Equal(dOrigin.z + 1000f, heldDirect.y, 2);
|
||||
}
|
||||
|
||||
|
|
@ -82,7 +93,7 @@ public sealed partial class FireControlCut124Tests
|
|||
FireControl.Fire(e.Weapon, e.WeaponItem, e.Shooter);
|
||||
var shot = SafeAssert.OnlyShot(e.Zone);
|
||||
|
||||
var late = FireControl.RoundAt(shot, shot.FireTime + 5000f);
|
||||
var late = FireControl.RoundAt(shot, ShotResult.Miss, shot.FireTime + 5000f);
|
||||
Assert.Equal(origin.x, late.x, 3);
|
||||
Assert.Equal(origin.z, late.y, 3);
|
||||
}
|
||||
|
|
@ -102,7 +113,7 @@ public sealed partial class FireControlCut124Tests
|
|||
var after = SafeAssert.OnlyShot(e.Zone);
|
||||
Assert.Equal(BallisticSpeed, after.Speed, 4);
|
||||
var aim = normalize(float2(2, 1));
|
||||
var at = FireControl.RoundAt(after, after.FireTime + 1f);
|
||||
var at = FireControl.RoundAt(after, null, after.FireTime + 1f);
|
||||
Assert.Equal(origin.x + aim.x * BallisticSpeed, at.x, 3);
|
||||
Assert.Equal(origin.z + aim.y * BallisticSpeed, at.y, 3);
|
||||
}
|
||||
|
|
@ -117,8 +128,18 @@ public sealed partial class FireControlCut124Tests
|
|||
var positions = entities.Select(x => x.Position).ToList();
|
||||
var velocities = entities.Select(x => x.Velocity).ToList();
|
||||
|
||||
var barrel = shot.FireOrigin + float3(1, .5f, -2);
|
||||
for (var i = 0; i < 25; i++)
|
||||
FireControl.RoundAt(shot, shot.FireTime + i * .37f);
|
||||
{
|
||||
var at = shot.FireTime + i * .37f;
|
||||
foreach (ShotResult? known in new ShotResult?[] { null, ShotResult.Miss })
|
||||
{
|
||||
FireControl.RoundAt(shot, known, at);
|
||||
FireControl.RoundEnd(shot, known);
|
||||
DrawAhead.Round(shot, known, barrel, .1f, at);
|
||||
DrawAhead.RoundOver(shot, known, at);
|
||||
}
|
||||
}
|
||||
|
||||
Assert.Equal(time, e.Zone.Time);
|
||||
Assert.True(e.Zone.PendingShots.Count == 1 && e.Zone.PendingShots[0].Equals(shot), "the pending shot changed");
|
||||
|
|
@ -126,4 +147,237 @@ public sealed partial class FireControlCut124Tests
|
|||
Assert.True(entities.Select(x => x.Position).SequenceEqual(positions), "an entity moved");
|
||||
Assert.True(entities.Select(x => x.Velocity).SequenceEqual(velocities), "an entity's velocity changed");
|
||||
}
|
||||
|
||||
// The weapon's Range is frozen with the rest: a round that misses flies on to the range it left with, whatever the stat
|
||||
// becomes after.
|
||||
[Fact]
|
||||
public void RangeIsFrozenAtFire()
|
||||
{
|
||||
var e = FireBurstRound(out var origin);
|
||||
var shot = SafeAssert.OnlyShot(e.Zone);
|
||||
Assert.Equal(NoLockRange, shot.MaxRange, 4);
|
||||
var endBefore = FireControl.RoundEnd(shot, ShotResult.Miss);
|
||||
|
||||
// The stat changes after the round left, and the weapon re-reads it.
|
||||
((InstantWeaponData) e.Weapon.WeaponData).Range = Constant(NoLockRange * 5f);
|
||||
e.Weapon.Execute(0f);
|
||||
Assert.Equal(NoLockRange * 5f, e.Weapon.Range, 4);
|
||||
|
||||
var after = SafeAssert.OnlyShot(e.Zone);
|
||||
Assert.Equal(NoLockRange, after.MaxRange, 4);
|
||||
Assert.Equal(endBefore, FireControl.RoundEnd(after, ShotResult.Miss), 4);
|
||||
var aim = normalize(float2(2, 1));
|
||||
var held = FireControl.RoundAt(after, ShotResult.Miss, after.FireTime + 1000f);
|
||||
Assert.Equal(origin.x + aim.x * NoLockRange, held.x, 3);
|
||||
Assert.Equal(origin.z + aim.y * NoLockRange, held.y, 3);
|
||||
}
|
||||
|
||||
// A direct round at a stationary target `targetRange` ahead (+z) of the shooter, weapon Range `range`, at `speed`;
|
||||
// or, with atNothing, the same weapon fired along +z at no target.
|
||||
private Engagement FireDirectRound(float speed, float range, float targetRange = 100f, bool atNothing = false)
|
||||
{
|
||||
var e = Build(TestSettings(), SolidShape(5, 4), velocity: speed, weaponRange: range, targetRange: targetRange);
|
||||
if (atNothing)
|
||||
{
|
||||
Aim(e, float2(0, 1));
|
||||
e.Shooter.SetTarget(TargetRef.None);
|
||||
}
|
||||
FireControl.Fire(e.Weapon, e.WeaponItem, e.Shooter);
|
||||
return e;
|
||||
}
|
||||
|
||||
// A hit, a burst or an outcome not yet known ends at the round's arrival; a miss flies on to the weapon's range and
|
||||
// never ends short of its own arrival; only a published miss is over by the clock.
|
||||
[Fact]
|
||||
public void RoundEndAndRoundOverAreTheOutcomesEnd()
|
||||
{
|
||||
foreach (var speed in new[] { 20f, 200f })
|
||||
{
|
||||
var d = FireDirectRound(speed, 1000f);
|
||||
var direct = SafeAssert.OnlyShot(d.Zone);
|
||||
Assert.Equal(direct.FireTime + 100f / speed, direct.ArrivalTime, 4);
|
||||
Assert.Equal(direct.FireTime + 100f / speed, FireControl.RoundEnd(direct, null), 4);
|
||||
Assert.Equal(direct.FireTime + 100f / speed, FireControl.RoundEnd(direct, ShotResult.Hit), 4);
|
||||
Assert.Equal(direct.FireTime + 100f / speed, FireControl.RoundEnd(direct, ShotResult.Burst), 4);
|
||||
Assert.Equal(direct.FireTime + 1000f / speed, FireControl.RoundEnd(direct, ShotResult.Miss), 4);
|
||||
|
||||
foreach (ShotResult? known in new ShotResult?[] { null, ShotResult.Hit, ShotResult.Burst })
|
||||
Assert.False(DrawAhead.RoundOver(direct, known, direct.FireTime + 10000f));
|
||||
var missEnd = direct.FireTime + 1000f / speed;
|
||||
Assert.False(DrawAhead.RoundOver(direct, ShotResult.Miss, missEnd - .01f));
|
||||
Assert.True(DrawAhead.RoundOver(direct, ShotResult.Miss, missEnd));
|
||||
|
||||
// A target beyond the weapon's range: a miss never ends short of its own arrival.
|
||||
var far = FireDirectRound(speed, 100f, targetRange: 150f);
|
||||
var beyond = SafeAssert.OnlyShot(far.Zone);
|
||||
Assert.Equal(beyond.FireTime + 150f / speed, beyond.ArrivalTime, 4);
|
||||
Assert.Equal(beyond.FireTime + 150f / speed, FireControl.RoundEnd(beyond, ShotResult.Miss), 4);
|
||||
|
||||
// A round fired at nothing arrives where it left, and a miss flies on to the range.
|
||||
var none = FireDirectRound(speed, 100f, atNothing: true);
|
||||
var nothing = SafeAssert.OnlyShot(none.Zone);
|
||||
Assert.Equal(nothing.FireTime, nothing.ArrivalTime, 4);
|
||||
Assert.Equal(nothing.FireTime, FireControl.RoundEnd(nothing, null), 4);
|
||||
Assert.Equal(nothing.FireTime + 100f / speed, FireControl.RoundEnd(nothing, ShotResult.Miss), 4);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// The frame falls Lead (< one step) after the last step, so a hit or burst is drawn at Zone.Time + Lead; it must stay
|
||||
// on the arrival point until the sim resolves it, and only a published miss flies on.
|
||||
[Theory]
|
||||
[InlineData(20f)]
|
||||
[InlineData(200f)]
|
||||
public void ARoundIsDrawnNoFurtherThanItsArrivalUntilItMisses(float speed)
|
||||
{
|
||||
var e = FireDirectRound(speed, 1000f);
|
||||
var shot = SafeAssert.OnlyShot(e.Zone);
|
||||
var origin = shot.FireOrigin;
|
||||
const float step = 1f / 30f;
|
||||
|
||||
foreach (var lead in new[] { 0f, step / 2f, .999f * step })
|
||||
{
|
||||
var time = shot.ArrivalTime + lead;
|
||||
foreach (ShotResult? known in new ShotResult?[] { null, ShotResult.Hit, ShotResult.Burst })
|
||||
{
|
||||
var drawn = DrawAhead.Round(shot, known, shot.FireOrigin, 0f, time);
|
||||
Assert.Equal(origin.x, drawn.x, 3);
|
||||
Assert.Equal(origin.z + 100f, drawn.z, 3);
|
||||
}
|
||||
var missed = DrawAhead.Round(shot, ShotResult.Miss, shot.FireOrigin, 0f, time);
|
||||
Assert.Equal(origin.x, missed.x, 3);
|
||||
Assert.Equal(origin.z + 100f + lead * speed, missed.z, 3);
|
||||
}
|
||||
|
||||
for (var time = shot.FireTime; time <= shot.ArrivalTime + 1f; time += 1f / 144f)
|
||||
{
|
||||
var drawn = DrawAhead.Round(shot, null, shot.FireOrigin, 0f, time);
|
||||
Assert.True(drawn.z - origin.z <= 100f + 1e-3f, "the round was drawn past its arrival point");
|
||||
}
|
||||
}
|
||||
|
||||
// A no-lock contact round stopped by a hull short of max range has its arrival shortened at the commit; the
|
||||
// simulation writes the record back before ShotCommitted fires, so a presenter re-reading it there draws the stop
|
||||
// at the committed arrival, not the fire-time copy's.
|
||||
[Fact]
|
||||
public void TheCommittedRecordCarriesTheCommittedArrival()
|
||||
{
|
||||
var e = Build(TestSettings(), SolidShape(5, 4), velocity: 20f, fuse: WeaponFuse.Contact, blastRadius: 4f, damage: 100f,
|
||||
weaponRange: NoLockRange);
|
||||
e.Shooter.SetTarget(TargetRef.None);
|
||||
Aim(e, float2(0, 1));
|
||||
AddShip(e, PointAlong(e, float2(0, 1), 25f), 1f, 300);
|
||||
ShotOutcome outcome = null;
|
||||
PendingShot record = default;
|
||||
var found = false;
|
||||
using var c = e.Zone.ShotCommitted.Subscribe(o =>
|
||||
{
|
||||
outcome = o;
|
||||
found = e.Zone.TryGetShot(o.ShotId, out record);
|
||||
});
|
||||
|
||||
FireControl.Fire(e.Weapon, e.WeaponItem, e.Shooter);
|
||||
var fired = SafeAssert.OnlyShot(e.Zone);
|
||||
Assert.False(fired.Committed);
|
||||
e.Zone.Update(.01f);
|
||||
|
||||
Assert.True(found, "the committed record was not readable when ShotCommitted fired");
|
||||
Assert.True(record.Committed);
|
||||
Assert.Equal(fired.FireTime + 3f, fired.ArrivalTime, 2);
|
||||
Assert.Equal(fired.FireTime + 1.05f, record.ArrivalTime, 2);
|
||||
Assert.True(record.ArrivalTime < fired.ArrivalTime);
|
||||
|
||||
var stop = DrawAhead.RoundStop(record, outcome, record.FireOrigin, 0f);
|
||||
var expected = outcome.HasBurstPoint
|
||||
? outcome.BurstPoint
|
||||
: record.FireOrigin.xz + record.TravelDirection * (20f * (record.ArrivalTime - record.FireTime));
|
||||
Assert.Equal(expected.x, stop.x, 3);
|
||||
Assert.Equal(expected.y, stop.z, 3);
|
||||
foreach (var later in new[] { 0f, .5f, 5f })
|
||||
{
|
||||
var drawn = DrawAhead.Round(record, ShotResult.Burst, record.FireOrigin, 0f, record.ArrivalTime + later);
|
||||
Assert.Equal(expected.x, drawn.x, 3);
|
||||
Assert.Equal(expected.y, drawn.z, 3);
|
||||
}
|
||||
}
|
||||
|
||||
// The barrel is only where the round is first seen: its offset from the sim line decays linearly to nothing over
|
||||
// blendTime sim seconds from FireTime.
|
||||
[Theory]
|
||||
[InlineData(.1f)]
|
||||
[InlineData(.25f)]
|
||||
public void TheBarrelBlendDecaysOnSimTime(float blend)
|
||||
{
|
||||
var e = FireBurstRound(out var origin);
|
||||
var shot = SafeAssert.OnlyShot(e.Zone);
|
||||
var aim = normalize(float2(2, 1));
|
||||
var offset = float3(1, .5f, -2);
|
||||
var barrel = shot.FireOrigin + offset;
|
||||
float3 Line(float dt) => float3(origin.x + aim.x * BallisticSpeed * dt, origin.y, origin.z + aim.y * BallisticSpeed * dt);
|
||||
|
||||
void Close(float3 want, float3 got)
|
||||
{
|
||||
Assert.Equal(want.x, got.x, 3);
|
||||
Assert.Equal(want.y, got.y, 3);
|
||||
Assert.Equal(want.z, got.z, 3);
|
||||
}
|
||||
|
||||
Close(barrel, DrawAhead.Round(shot, null, barrel, blend, shot.FireTime));
|
||||
Close(Line(blend / 2f) + offset * .5f, DrawAhead.Round(shot, null, barrel, blend, shot.FireTime + blend / 2f));
|
||||
Close(Line(blend), DrawAhead.Round(shot, null, barrel, blend, shot.FireTime + blend));
|
||||
Close(Line(2f * blend), DrawAhead.Round(shot, null, barrel, blend, shot.FireTime + 2f * blend));
|
||||
Close(Line(0f), DrawAhead.Round(shot, null, barrel, 0f, shot.FireTime));
|
||||
}
|
||||
|
||||
// Placement depends on sim time alone: any history of frames walked to a checkpoint draws what a fresh call draws, which
|
||||
// is the frozen line at the frozen speed.
|
||||
[Theory]
|
||||
[InlineData(20f)]
|
||||
[InlineData(200f)]
|
||||
public void ARoundIsAFunctionOfSimTime(float speed)
|
||||
{
|
||||
var e = FireDirectRound(speed, 1000f);
|
||||
var shot = SafeAssert.OnlyShot(e.Zone);
|
||||
var checkpoints = new[] { .05f, .3f, 1f, 3f };
|
||||
var histories = new[] { new[] { 1f / 144f }, new[] { 1f / 30f }, new[] { 1f / 144f, 1f / 30f, 1f / 90f } };
|
||||
|
||||
foreach (var frames in histories)
|
||||
{
|
||||
var elapsed = 0f;
|
||||
var frame = 0;
|
||||
foreach (var checkpoint in checkpoints)
|
||||
{
|
||||
while (elapsed + 1e-4f < checkpoint)
|
||||
{
|
||||
elapsed = min(checkpoint, elapsed + frames[frame++ % frames.Length]);
|
||||
DrawAhead.Round(shot, ShotResult.Miss, shot.FireOrigin, .1f, shot.FireTime + elapsed);
|
||||
}
|
||||
var drawn = DrawAhead.Round(shot, ShotResult.Miss, shot.FireOrigin, .1f, shot.FireTime + checkpoint);
|
||||
var fresh = DrawAhead.Round(shot, ShotResult.Miss, shot.FireOrigin, .1f, shot.FireTime + checkpoint);
|
||||
Assert.Equal(fresh.z, drawn.z, 3);
|
||||
Assert.Equal(shot.FireOrigin.z + speed * min(checkpoint, 1000f / speed), drawn.z, 3);
|
||||
Assert.Equal(shot.FireOrigin.x, drawn.x, 3);
|
||||
Assert.Equal(shot.FireOrigin.y, drawn.y, 3);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Today's sim geometry: the round flies the line frozen at Fire and stops at the arrival point on it, whatever the
|
||||
// target does afterwards. (Where a leading round's arrival should lie is the sim's question, never Projectile's.)
|
||||
[Fact]
|
||||
public void AHitOnAMovingTargetStopsOnItsFrozenLine()
|
||||
{
|
||||
var e = Build(TestSettings(), SolidShape(5, 4), velocity: BallisticSpeed, weaponRange: 1000f);
|
||||
e.Target.Velocity = float2(30, 0);
|
||||
FireControl.Fire(e.Weapon, e.WeaponItem, e.Shooter);
|
||||
var shot = SafeAssert.OnlyShot(e.Zone);
|
||||
Assert.True(shot.TravelDirection.x > 0f, "the round did not lead the target");
|
||||
|
||||
e.Target.Position += float3(40, 0, 0);
|
||||
var stop = DrawAhead.RoundStop(shot, new ShotOutcome { ShotId = shot.ShotId, Result = ShotResult.Hit }, shot.FireOrigin, 0f);
|
||||
var line = shot.FireOrigin.xz + shot.TravelDirection * (BallisticSpeed * (shot.ArrivalTime - shot.FireTime));
|
||||
Assert.Equal(line.x, stop.x, 3);
|
||||
Assert.Equal(shot.FireOrigin.y, stop.y, 3);
|
||||
Assert.Equal(line.y, stop.z, 3);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue