diff --git a/Assets/Scripts/ServerShared/Behaviors/ConstantWeapon.cs b/Assets/Scripts/ServerShared/Behaviors/ConstantWeapon.cs index 8a93c2a82..14d6b4820 100644 --- a/Assets/Scripts/ServerShared/Behaviors/ConstantWeapon.cs +++ b/Assets/Scripts/ServerShared/Behaviors/ConstantWeapon.cs @@ -91,7 +91,7 @@ public class ConstantWeapon : Weapon, IProgressBehavior, IEventBehavior, IPowerC // Cut 5 (docs/fire-control-cut.md, 5.3, Soul finding 9): StanceAllowsFire alone let a side-mounted beam // fire forward -- ArcAllowsFire joins it here, matching InstantWeapon.cs's player arc gate (Q2: manual and // programmatic are one truth, and a continuous weapon is no exception). - public float PowerRequest(float dt) => _firing && StanceAllowsFire && ArcAllowsFire && FuseAllowsFire ? EvaluateNominalPower(_data.Energy) * dt : 0f; + public float PowerRequest(float dt) => _firing && StanceAllowsFire && ArcAllowsFire ? EvaluateNominalPower(_data.Energy) * dt : 0f; // Cut 5 (docs/stats-and-power-cut.md ยง1.3, PowerTiers.cs): Low -- offense, same as InstantWeapon. public int DefaultPowerTier => PowerTiers.Low; @@ -99,7 +99,7 @@ public class ConstantWeapon : Weapon, IProgressBehavior, IEventBehavior, IPowerC public override bool Execute(float dt) { base.Execute(dt); - if (_firing && !(StanceAllowsFire && ArcAllowsFire && FuseAllowsFire)) + if (_firing && !(StanceAllowsFire && ArcAllowsFire)) { // Safed: shooter has a target and hasn't declared hostility toward it, or the target no longer // bears (5.3: a beam obeys its arc exactly as InstantWeapon's trigger does). @@ -179,7 +179,7 @@ public class ConstantWeapon : Weapon, IProgressBehavior, IEventBehavior, IPowerC // argument included, when OnBeamShot has no subscriber (true today, R9's "no design uses it // yet"), so the roll would silently never happen. Evaluate it into a local first. var shotId = FireControl.Fire(this, Item, Entity, Damage * interval); - OnBeamShot?.Invoke(shotId); + if (shotId != 0) OnBeamShot?.Invoke(shotId); } } return true; diff --git a/Assets/Scripts/ServerShared/Behaviors/InstantWeapon.cs b/Assets/Scripts/ServerShared/Behaviors/InstantWeapon.cs index 1cf398539..6de087ba0 100644 --- a/Assets/Scripts/ServerShared/Behaviors/InstantWeapon.cs +++ b/Assets/Scripts/ServerShared/Behaviors/InstantWeapon.cs @@ -103,8 +103,6 @@ public class InstantWeapon : Weapon, IProgressBehavior, IEventBehavior, IPowerCo // through -- a player's action-bar Activate() reaches this exactly the same way Combat.cs's and // TurretController.cs's Activate() calls do. if (!ArcAllowsFire) return; - // A fused weapon whose Range no longer clears its blast would burst on its shooter. - if (!FuseAllowsFire) return; // If 1 ammo is consumed per burst, perform ammo and energy consumption here // UseAmmo returns false when triggering reload; cancel firing if that is the case @@ -248,6 +246,9 @@ public class InstantWeapon : Weapon, IProgressBehavior, IEventBehavior, IPowerCo // Unity EntityInstance wiring, docs/headless-playground-cut.md), so a shot silently never fired. // Evaluate it into a local first. var shotId = FireControl.Fire(this, Item, Entity); + // A fused round whose arming distance Range cannot reach is refused (FireControl.Fire returns 0): + // nothing was fired, so nothing sounds, wears or heats. + if (shotId == 0) continue; OnFire?.Invoke(shotId); if(!firedThisFrame) { diff --git a/Assets/Scripts/ServerShared/Behaviors/Weapon.cs b/Assets/Scripts/ServerShared/Behaviors/Weapon.cs index 57340f8d9..47f2acf11 100644 --- a/Assets/Scripts/ServerShared/Behaviors/Weapon.cs +++ b/Assets/Scripts/ServerShared/Behaviors/Weapon.cs @@ -110,9 +110,6 @@ public abstract class Weapon : Behavior, IActivatedBehavior // FireControl owns the gate, including its one exemption: a fused weapon fires out of arc. public bool ArcAllowsFire => FireControl.ArcPermitsFire(this, Entity); - // A fused weapon whose current Range does not exceed its blast stays safed (FireControl.FuseCanArm). - public bool FuseAllowsFire => FireControl.FuseCanArm(this); - public Weapon(WeaponData data, EquippedItem item) : base(data, item) { _data = data; diff --git a/Assets/Scripts/ServerShared/FireControl.cs b/Assets/Scripts/ServerShared/FireControl.cs index 51fab98af..adf1721e0 100644 --- a/Assets/Scripts/ServerShared/FireControl.cs +++ b/Assets/Scripts/ServerShared/FireControl.cs @@ -79,10 +79,15 @@ public static class FireControl return FuseOf(weapon.Item, out _) != null || InArc(weapon.Item, target.Position - shooter.Position); } - // A fused weapon whose Range, as it stands now (power supply and modifiers included), does not exceed its - // blast would burst on its own shooter at max range, so its trigger stays safed. Judged on the effective - // Range rather than the catalog's authored floor, which Evaluate can scale below. - public static bool FuseCanArm(Weapon weapon) => FuseOf(weapon.Item, out var blastRadius) == null || weapon.Range > blastRadius; + // Operator ruling 2026-09-30 (arming distance): a round never bursts closer than its blast radius to where + // it was fired, and the disc must clear the shooter's own hull as it stood at fire. So the burst is at least + // BlastRadius beyond the far edge of the shooter's hull along the flight line. Close bursts are pushed out + // along the aim to this distance; a pilot who flies into their own distant burst takes the damage. + private static float ArmingDistance(Entity shooter, float2 direction, float blastRadius) + { + HullAhead(shooter, shooter.Position.xz, direction, out _, out var exit); + return exit + blastRadius; + } // Operator ruling 2026-09-30: a shot with nothing to bear on flies along where its shooter is aiming // (Entity.LookDirection -- the player's mouse or a turret's tracking), restricted by the mount's arc. The @@ -225,8 +230,10 @@ public static class FireControl // gate -- Fire reads it through this same overload for flightTime, and flightTime must reflect the // real distance even when a gate (not visible, out of arc) already prices the shot at PFire 0. The // gates below still short-circuit the rest of the shooter-side probability in the same order as before. + // Planar (R7: the simulation is 2D): the range gate, the burst reach and the range clamp all measure the + // same distance, so a target's height never moves one of them without the others. var toTarget = target.Position - source.Position; - range = length(toTarget); + range = length(toTarget.xz); if (!source.VisibleEntities.Contains(target)) return 0f; if (range < weapon.MinRange || range > weapon.Range) return 0f; @@ -316,7 +323,7 @@ public static class FireControl if (target == null) return diagnostic; var toTarget = target.Position - source.Position; - diagnostic.Range = length(toTarget); + diagnostic.Range = length(toTarget.xz); diagnostic.Visible = source.VisibleEntities.Contains(target); diagnostic.InRange = diagnostic.Range >= weapon.MinRange && diagnostic.Range <= weapon.Range; diagnostic.Locked = !(weapon is LockWeapon lockWeapon) || lockWeapon.IsLocked; @@ -443,10 +450,36 @@ public static class FireControl // Range, along that same clamped aim: the arc is hoped to land the blast near enough to splash. // Either way the round flies at the weapon's Velocity like a targeted shot flies to its intercept. A shot // without a fuse keeps its target as before. + // Arming distance (operator ruling 2026-09-30): a round never bursts closer than its blast radius past the + // shooter's hull, so a burst that would land nearer -- a target close in, or out of arc and inside the + // radius -- is pushed out along the aim to that distance, and the round carries no target. A round whose + // arming distance exceeds the weapon's Range cannot be fired at all: it is refused here, per round, where + // Range is read after Execute refreshed it (a burst's later rounds and a power-starved Range included). var engaged = fuse != null && !(designated && inArc) ? null : target; var origin = source.Position; var travelDirection = engaged != null ? TravelDirection(weapon, source, engaged) : AimDirection(item, source); + var arming = 0f; + var burstPosition = engaged != null ? engaged.Position : origin; + if (fuse != null && engaged != null) + { + // Nothing bursts beyond max range: an intercept a receding target leads the round past Range is + // pulled back along its own line to Range. + var intercept = PredictedIntercept(weapon, source, engaged); + var interceptReach = length((intercept - origin).xz); + arming = ArmingDistance(source, travelDirection, blastRadius); + if (min(interceptReach, weapon.Range) < arming) engaged = null; + else burstPosition = interceptReach > weapon.Range ? origin + (intercept - origin) * (weapon.Range / interceptReach) : intercept; + } + if (fuse != null && engaged == null) + { + travelDirection = AimDirection(item, source); + arming = ArmingDistance(source, travelDirection, blastRadius); + var reach = max(designated ? fireRange : weapon.Range, arming); + burstPosition = origin + float3(travelDirection.x, 0, travelDirection.y) * reach; + } + if (arming > weapon.Range) return 0; + var targetVelocity = float3.zero; var targetPosition = origin; var flightTime = 0f; @@ -456,25 +489,14 @@ public static class FireControl targetPosition = engaged.Position; flightTime = weapon.Velocity > .01f ? fireRange / weapon.Velocity : 0f; } - - var commitHorizon = source.ItemManager.GameplaySettings.CommitHorizon; - - var burstPosition = targetPosition; - if (fuse != null && engaged != null) - { - // Nothing bursts beyond max range: an intercept a receding target leads the round past Range is - // pulled back along its own line to Range. - var intercept = PredictedIntercept(weapon, source, engaged); - var reach = length((intercept - origin).xz); - burstPosition = reach > weapon.Range ? origin + (intercept - origin) * (weapon.Range / reach) : intercept; - } else if (fuse != null) { - var reach = designated ? fireRange : weapon.Range; - burstPosition = origin + float3(travelDirection.x, 0, travelDirection.y) * reach; - flightTime = weapon.Velocity > .01f ? reach / weapon.Velocity : 0f; + var flown = length((burstPosition - origin).xz); + flightTime = weapon.Velocity > .01f ? flown / weapon.Velocity : 0f; } + var commitHorizon = source.ItemManager.GameplaySettings.CommitHorizon; + var shot = new PendingShot { ShotId = zone.NextShotId(), @@ -504,6 +526,7 @@ public static class FireControl BurstPosition = burstPosition, Fuse = fuse, BlastRadius = blastRadius, + ArmingDistance = arming, Committed = false }; @@ -696,7 +719,11 @@ public static class FireControl var proximity = shot.Fuse == WeaponFuse.Proximity; var outcome = MakeOutcome(shot, hit ? ShotResult.Hit : proximity ? ShotResult.Burst : ShotResult.Miss, shielded, shieldBroken, cell, bearing, lateral, now); - if (proximity) outcome.BurstPoint = shot.BurstPosition.xz; + if (proximity) + { + outcome.BurstPoint = shot.BurstPosition.xz; + outcome.HasBurstPoint = true; + } return outcome; } @@ -708,8 +735,10 @@ public static class FireControl private static ShotOutcome CommitBurst(Zone zone, PendingShot shot, float now) { var outcome = MakeOutcome(shot, ShotResult.Burst, false, false, int2.zero, float2.zero, 0f, now); + outcome.HasBurstPoint = true; var reach = length(shot.BurstPosition.xz - shot.FireOrigin.xz); var travelled = shot.Fuse == WeaponFuse.Contact ? ContactDistance(zone, shot, reach) : reach; + travelled = min(reach, max(travelled, shot.ArmingDistance)); if (travelled < reach) { outcome.BurstPoint = shot.FireOrigin.xz + shot.TravelDirection * travelled; @@ -730,16 +759,18 @@ public static class FireControl foreach (var entity in zone.Entities) { if (entity == shot.Source) continue; - nearest = min(nearest, HullEntryDistance(entity, shot.FireOrigin.xz, shot.TravelDirection)); + if (HullAhead(entity, shot.FireOrigin.xz, shot.TravelDirection, out var entry, out _)) nearest = min(nearest, entry); } return nearest; } - // World distance from `origin` along the unit planar `direction` to the first metal cell of `entity`'s hull, - // or +infinity when the line meets none ahead of it. Read through Lane's own cell admission (CellsOnLine), in - // the entity's schematic frame: the line's lateral offset is its own, and cells wholly behind the origin - // are skipped. An origin already inside the hull is a distance of zero. - private static float HullEntryDistance(Entity entity, float2 origin, float2 direction) + // Where the line from `origin` along the unit planar `direction` meets `entity`'s hull, in world distance + // from `origin`: `entry` to the first metal cell ahead (+infinity when the line meets none, and negative when the + // origin is already inside the hull: the arming distance floors every caller's use of it) and `exit` to the far edge of the last one (zero when none). Read + // through Lane's own cell admission (CellsOnLine), in the entity's schematic frame: the line's lateral offset + // is its own, and cells wholly behind the origin are skipped. Contact fuses read `entry`; a round's arming + // distance reads `exit` of its own shooter. + private static bool HullAhead(Entity entity, float2 origin, float2 direction, out float entry, out float exit) { var hullData = entity.ItemManager.GetData(entity.Hull) as HullData; var cellSize = entity.ItemManager.GameplaySettings.SchematicCellSize; @@ -748,14 +779,19 @@ public static class FireControl var lateral = dot(start, Lateral(bearing)); var along = dot(start, bearing); + entry = float.PositiveInfinity; + exit = 0f; var buffer = ArrayPool.Shared.Rent(hullData.Shape.Coordinates.Length); try { var count = CellsOnLine(hullData, bearing, lateral, buffer); for (var i = 0; i < count; i++) - if (buffer[i].Exit > along) - return max(buffer[i].Entry - along, 0f) * cellSize; - return float.PositiveInfinity; + { + if (!(buffer[i].Exit > along)) continue; + if (float.IsPositiveInfinity(entry)) entry = (buffer[i].Entry - along) * cellSize; + exit = max(exit, (buffer[i].Exit - along) * cellSize); + } + return !float.IsPositiveInfinity(entry); } finally { @@ -1677,6 +1713,9 @@ public struct PendingShot public float3 BurstPosition; public WeaponFuse? Fuse; public float BlastRadius; + // How far along its flight line a fused round may first burst: BlastRadius past the far edge of the + // shooter's hull, frozen at Fire (a round outlives its shooter's pose, too). + public float ArmingDistance; // Frozen with the rest of the shooter's decision: where the round left from. A round outlives its shooter, so // nothing at resolution reads the shooter's position. @@ -1785,9 +1824,10 @@ public sealed class ShotOutcome // (Burst, at BurstPoint). Hit is derived from it, not stored beside it. public ShotResult Result; public bool Hit => Result == ShotResult.Hit; - // World planar point a proximity or no-target round detonates at, frozen by the commit. Meaningless for any - // other round. + // World planar point a proximity or no-target round detonates at, frozen by the commit; HasBurstPoint says + // whether this round has one (a contact or delayed round detonates where it hits, not at a frozen point). public float2 BurstPoint; + public bool HasBurstPoint; public bool Shielded; // Cut 5, 5.2 (Soul finding 4): frozen alongside the rest of the outcome (R4) -- a shield present, active, // and unable to CanTakeHit this shot is decided broken right here, so Apply performs Break() rather than diff --git a/tests/Aetheria.Shared.Tests/FireAuthorityTests.cs b/tests/Aetheria.Shared.Tests/FireAuthorityTests.cs index 3d3568b93..921961b15 100644 --- a/tests/Aetheria.Shared.Tests/FireAuthorityTests.cs +++ b/tests/Aetheria.Shared.Tests/FireAuthorityTests.cs @@ -331,7 +331,7 @@ public sealed class FireAuthorityTests : IDisposable e.Zone.Update(10f); // well past arrival Assert.Equal(before, e.Target.Hull.Durability); - Assert.Empty(e.Zone.PendingShots); + SafeAssert.NoShots(e.Zone); } // R4: ShotCommitted fires exactly CommitHorizon before ShotResolved, once per shot. Mutation: publish only @@ -420,7 +420,7 @@ public sealed class FireAuthorityTests : IDisposable e.Zone.Update(.016f); // one ordinary tick Assert.Equal(new[] { "committed", "resolved" }, order); - Assert.Empty(e.Zone.PendingShots); + SafeAssert.NoShots(e.Zone); } // R10/Q6: a shot's parameters freeze when the trigger is pulled. Changing the weapon's authored Damage diff --git a/tests/Aetheria.Shared.Tests/FireControlCut11Tests.cs b/tests/Aetheria.Shared.Tests/FireControlCut11Tests.cs index 2a65fda64..ab2966025 100644 --- a/tests/Aetheria.Shared.Tests/FireControlCut11Tests.cs +++ b/tests/Aetheria.Shared.Tests/FireControlCut11Tests.cs @@ -305,19 +305,19 @@ public sealed class FireControlCut11Tests : IDisposable var shotId = FireControl.Fire(e.Weapon, e.WeaponItem, e.Shooter); e.Zone.Update(1f); - Assert.Empty(committed); + SafeAssert.Empty(committed); e.Zone.Entities.Remove(e.Target); e.Zone.Update(1f); - foreach (var outcome in new[] { Assert.Single(committed), Assert.Single(resolved) }) + foreach (var outcome in new[] { SafeAssert.Only(committed), SafeAssert.Only(resolved) }) { Assert.Equal(shotId, outcome.ShotId); Assert.False(outcome.Hit); Assert.False(outcome.Shielded, "a shot at nothing did not hit a shield"); Assert.False(outcome.ShieldBroken, "a shot at nothing did not break a shield"); } - Assert.Empty(e.Zone.PendingShots); + SafeAssert.NoShots(e.Zone); } // ---- A targeting system's Tracking is what forgives a target that moves off its predicted line. ---- diff --git a/tests/Aetheria.Shared.Tests/FireControlCut124Tests.cs b/tests/Aetheria.Shared.Tests/FireControlCut124Tests.cs index 3c8f38db3..f6b91f97e 100644 --- a/tests/Aetheria.Shared.Tests/FireControlCut124Tests.cs +++ b/tests/Aetheria.Shared.Tests/FireControlCut124Tests.cs @@ -543,10 +543,10 @@ public sealed class FireControlCut124Tests : IDisposable FireControl.Fire(e.Weapon, e.WeaponItem, e.Shooter); e.Zone.Update(.01f); - Assert.Empty(e.Zone.PendingShots); + SafeAssert.NoShots(e.Zone); Assert.Equal(1, resolved); - Assert.DoesNotContain(near, e.Zone.Entities); // fixture: the blast really was lethal to a ship mid-pass - Assert.Contains(far, e.Zone.Entities); + SafeAssert.NotIn(e.Zone, near); // fixture: the blast really was lethal to a ship mid-pass + SafeAssert.In(e.Zone, far); Assert.True(targetDamage > 0f, "fixture: the target must be inside the blast"); var afterFirstTick = targetDamage; @@ -588,7 +588,7 @@ public sealed class FireControlCut124Tests : IDisposable Assert.Equal(new[] { 1, 1, 1 }, hullHits); Assert.Equal(new[] { 1, 1, 1 }, deaths); foreach (var s in ships) Assert.False(e.Zone.Entities.Contains(s), "a killed ship must have left the zone"); - Assert.Empty(e.Zone.PendingShots); + SafeAssert.NoShots(e.Zone); Assert.Equal(1, resolved); } @@ -1149,7 +1149,7 @@ public sealed class FireControlCut124Tests : IDisposable Assert.NotNull(outcome); Assert.False(outcome.Hit); // fixture: a guaranteed miss Assert.Equal(ShotResult.Miss, outcome.Result); - Assert.Empty(e.Zone.PendingShots); + SafeAssert.NoShots(e.Zone); Assert.Equal(0, events); Assert.Equal(before, e.Target.Hull.Durability); } @@ -1189,7 +1189,7 @@ public sealed class FireControlCut124Tests : IDisposable e.Zone.Update(.01f); Assert.Equal(0f, shooterHit); - Assert.Empty(e.Zone.PendingShots); + SafeAssert.NoShots(e.Zone); Assert.False(e.Zone.Entities.Contains(onLine), "the blast must reach the burst point (fixture: 1 durability, so any damage kills it)"); Assert.True(e.Zone.Entities.Contains(offLine), "the blast must not reach 42 units off the aim line"); } @@ -1205,7 +1205,7 @@ public sealed class FireControlCut124Tests : IDisposable FireControl.Fire(e.Weapon, e.WeaponItem, e.Shooter); - var shot = Assert.Single(e.Zone.PendingShots); + var shot = SafeAssert.OnlyShot(e.Zone); Assert.Equal(NoLockRange / 20f, shot.ArrivalTime - shot.FireTime, 4); var burst = AimLine(e); Assert.Equal(burst.x, shot.BurstPosition.x, 3); @@ -1224,7 +1224,7 @@ public sealed class FireControlCut124Tests : IDisposable using var i = e.Target.ItemDamage.Subscribe(x => damage += x.damage); FireControl.Fire(e.Weapon, e.WeaponItem, e.Shooter); - var shot = Assert.Single(e.Zone.PendingShots); + var shot = SafeAssert.OnlyShot(e.Zone); Assert.Equal(e.Target.Position.x, shot.BurstPosition.x, 3); Assert.Equal(e.Target.Position.z, shot.BurstPosition.z, 3); e.Zone.Update(.01f); @@ -1316,7 +1316,7 @@ public sealed class FireControlCut124Tests : IDisposable var targetBefore = e.Target.Hull.Durability; FireControl.Fire(e.Weapon, e.WeaponItem, e.Shooter); - var shot = Assert.Single(e.Zone.PendingShots); + var shot = SafeAssert.OnlyShot(e.Zone); Assert.True(shot.Target == null, "an invalid target must not ride with the round"); // not Assert.Null: a failure would format the entity graph and overflow the stack Assert.Equal(burst.x, shot.BurstPosition.x, 3); Assert.Equal(burst.z, shot.BurstPosition.z, 3); @@ -1339,7 +1339,7 @@ public sealed class FireControlCut124Tests : IDisposable FireControl.Fire(e.Weapon, e.WeaponItem, e.Shooter); - var shot = Assert.Single(e.Zone.PendingShots); + var shot = SafeAssert.OnlyShot(e.Zone); Assert.True(shot.Target == e.Target, "a locked, visible, in-range, in-arc target rides with the round"); Assert.Equal(e.Target.Position.z, shot.BurstPosition.z, 3); } @@ -1371,7 +1371,7 @@ public sealed class FireControlCut124Tests : IDisposable var targetBefore = e.Target.Hull.Durability; FireControl.Fire(e.Weapon, e.WeaponItem, e.Shooter); - var shot = Assert.Single(e.Zone.PendingShots); + var shot = SafeAssert.OnlyShot(e.Zone); Assert.True(shot.Target == null, "a target the weapon cannot bear on must not ride with the round"); var burst = PointAlong(e, edge, 40f); Assert.Equal(burst.x, shot.BurstPosition.x, 3); @@ -1408,18 +1408,140 @@ public sealed class FireControlCut124Tests : IDisposable Assert.Equal(fires ? 1 : 0, committed); } - // A fused weapon safes itself while its effective Range does not exceed its blast, because a round bursting at - // max range would land on its own shooter. The gate reads the Range the weapon has NOW: the third row is - // Soul's probe (authored Range 100 clears a blast of 30, but at a power supply of 0.1 Range is 10), and the - // first two pin the boundary itself (equal refuses). Kills: reading the authored figure instead of the - // runtime Range; `>=` for `>`; dropping the gate from InstantWeapon.Trigger. + // ==== Operator ruling 2026-09-30, batch 4: the arming distance ==== + // + // A round never bursts closer than its blast radius past the shooter's own hull. The fixture's shooter hull is + // a 5x5 block of 2-unit cells centred on the shooter, so its far edge is 5 units out along the axis and + // 5 * sqrt(2) along a diagonal; with a blast of 30 the arming distance is 35 (or 30 + 7.07). + + private const float ShooterHullReach = 5f; + + private static float Planar(float3 from, float3 to) => length((to - from).xz); + + // A target inside the arming distance is not detonated on: the round flies along the aim and bursts at the + // arming distance, carrying no target, and the shooter that stood beside it is untouched (the probes measured + // 7.07 of self-damage for a target at 5, before this). The out-of-arc row is a target 10 away behind the + // mount, inside the blast radius, with the mount flipped so the arc is closed. Kills: no arming distance; a + // burst that still rides the target; an arming distance measured from the shooter's centre instead of the far + // edge of its hull (the burst would land 5 units short and the shooter would still be inside the disc). [Theory] - [InlineData(30f, 30f, false, false)] - [InlineData(31f, 30f, false, true)] - [InlineData(100f, 30f, true, false)] - public void AFusedWeaponWhoseRangeDoesNotExceedItsBlastStaysSafed(float range, float blast, bool starvedOfPower, bool fires) + [InlineData(5f, false)] + [InlineData(10f, true)] + public void ACloseBurstIsPushedOutAlongTheAimToTheArmingDistance(float targetRange, bool outOfArc) { - var e = Build(TestSettings(), SolidShape(5, 4), velocity: 0, fuse: WeaponFuse.Proximity, blastRadius: blast, damage: 100f, + var e = Build(TestSettings(), SolidShape(5, 4), velocity: 0, fuse: WeaponFuse.Proximity, blastRadius: 30f, damage: 100f, + weaponRange: 60f, targetRange: targetRange); + var aim = float2(0, 1); + if (outOfArc) + { + e.Shooter.Direction = float2(0, -1); + aim = float2(0, -1); + } + Aim(e, aim); + var burst = PointAlong(e, aim, 30f + ShooterHullReach); + var hurt = 0f; + using var a = e.Shooter.ArmorDamage.Subscribe(x => hurt += x.damage); + using var h = e.Shooter.HullDamage.Subscribe(x => hurt += x); + using var i = e.Shooter.ItemDamage.Subscribe(x => hurt += x.damage); + + FireControl.Fire(e.Weapon, e.WeaponItem, e.Shooter); + var shot = SafeAssert.OnlyShot(e.Zone); + Assert.True(shot.Target == null, "a burst pushed out to the arming distance carries no target"); + Assert.Equal(burst.x, shot.BurstPosition.x, 2); + Assert.Equal(burst.z, shot.BurstPosition.z, 2); + Assert.Equal(35f, shot.ArmingDistance, 2); + e.Zone.Update(.01f); + + Assert.Equal(0f, hurt); + } + + // The far edge is read along the flight line, not along an axis: aimed down a diagonal the 5x5 hull reaches + // 5 * sqrt(2) = 7.07 units, so the burst sits at 30 + 7.07. A close in-arc target makes the arming distance + // the burst reach. Kills: an extent read along the mount or the hull's own axes. + [Fact] + public void TheArmingDistanceIsMeasuredAlongTheFlightLine() + { + var e = Build(TestSettings(), SolidShape(5, 4), velocity: 0, fuse: WeaponFuse.Proximity, blastRadius: 30f, damage: 100f, + weaponRange: 60f, targetRange: 5f); + Aim(e, normalize(float2(1, 1))); + + FireControl.Fire(e.Weapon, e.WeaponItem, e.Shooter); + + var shot = SafeAssert.OnlyShot(e.Zone); + Assert.Equal(30f + ShooterHullReach * sqrt(2f), Planar(e.Shooter.Position, shot.BurstPosition), 1); + } + + // A target beyond the arming distance is not pushed anywhere: the round still rides it and bursts at its + // intercept. Kills: demoting every fused round to the aim. + [Fact] + public void ATargetBeyondTheArmingDistanceStillRidesWithTheRound() + { + var e = Build(TestSettings(), SolidShape(5, 4), velocity: 0, fuse: WeaponFuse.Proximity, blastRadius: 30f, damage: 100f, + weaponRange: 60f, targetRange: 40f); + Aim(e, float2(0, 1)); + + FireControl.Fire(e.Weapon, e.WeaponItem, e.Shooter); + + var shot = SafeAssert.OnlyShot(e.Zone); + Assert.True(shot.Target == e.Target, "a target the disc clears the shooter to reach is still the round's target"); + Assert.Equal(40f, Planar(e.Shooter.Position, shot.BurstPosition), 2); + } + + // A contact round stops on the first hull it crosses, but never nearer than its arming distance: a blocker + // whose near face is 6 units out (centred 10) is passed, and the round bursts at 5 + 4 = 9. Kills: a contact + // point taken as the hull face regardless of arming. + [Fact] + public void AContactRoundNeverStopsNearerThanItsArmingDistance() + { + var e = Build(TestSettings(), SolidShape(5, 4), velocity: 0, fuse: WeaponFuse.Contact, blastRadius: 4f, damage: 100f, weaponRange: NoLockRange); + e.Shooter.Target.Value = null; + Aim(e, float2(0, 1)); + AddShip(e, PointAlong(e, float2(0, 1), 10f), 1f, 300); + ShotOutcome outcome = null; + using var c = e.Zone.ShotCommitted.Subscribe(o => outcome = o); + + FireControl.Fire(e.Weapon, e.WeaponItem, e.Shooter); + e.Zone.Update(.01f); + + Assert.NotNull(outcome); + Assert.Equal(e.Shooter.Position.z + ShooterHullReach + 4f, outcome.BurstPoint.y, 2); + } + + // A round fired from inside another hull is already in metal: its contact point is its arming distance. The + // host is a column with a 30-unit gap after the cell the muzzle sits in (cells 0-2 and 18-21, the muzzle in + // cell 2), so a contact search that skips the cell the origin is inside finds the far block instead of the + // muzzle's own. Kills: reading a cell's Entry for its Exit (Soul's M7). + [Fact] + public void AContactRoundFiredFromInsideAnotherHullBurstsAtTheArmingDistance() + { + var column = new Shape(1, 22); + foreach (var y in new[] { 0, 1, 2, 18, 19, 20, 21 }) column[new int2(0, y)] = true; + var e = Build(TestSettings(), column, velocity: 20f, fuse: WeaponFuse.Contact, blastRadius: 4f, damage: 100f, weaponRange: NoLockRange); + e.Shooter.Target.Value = null; + Aim(e, float2(0, 1)); + var offset = e.Target.ToWorldPoint(float2(0, 2)) - e.Target.Position.xz; + e.Target.Position = float3(e.Shooter.Position.x - offset.x, 0, e.Shooter.Position.z - offset.y); + ShotOutcome committed = null; + using var c = e.Zone.ShotCommitted.Subscribe(o => committed = o); + + FireControl.Fire(e.Weapon, e.WeaponItem, e.Shooter); + e.Zone.Update(.01f); + + Assert.NotNull(committed); + Assert.Equal(e.Shooter.Position.z + ShooterHullReach + 4f, committed.BurstPoint.y, 2); + } + + // A fused round is refused, per round and at the moment Fire reads Range, when its arming distance exceeds + // that Range: 34.5 against 35 refuses, 35.01 fires, and a power-starved Range (authored 100, forced to about + // 10 the way Soul's probe did after the trigger has passed) refuses although the authored figure clears + // it. Fire returns 0 and queues nothing. Kills: a gate that reads the authored Range; `>=` for `>`. + [Theory] + [InlineData(34.5f, false, false)] + [InlineData(35.01f, false, true)] + [InlineData(100f, true, false)] + public void AFusedRoundWhoseArmingDistanceExceedsRangeIsRefusedByFire(float range, bool starvedOfPower, bool fires) + { + var e = Build(TestSettings(), SolidShape(5, 4), velocity: 0, fuse: WeaponFuse.Proximity, blastRadius: 30f, damage: 100f, weaponRange: range, rangeTerm: starvedOfPower ? StatSource.PowerSupply : (StatSource?) null); e.Shooter.Target.Value = null; Aim(e, float2(0, 1)); @@ -1428,18 +1550,75 @@ public sealed class FireControlCut124Tests : IDisposable typeof(EquippedItem).GetProperty("PowerSupply").GetSetMethod(true).Invoke(e.WeaponItem, new object[] { .1f }); e.Shooter.Resolver.InvalidateSource(e.WeaponItem, StatSource.PowerSupply); e.Weapon.GetType().GetMethod("UpdateStats", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(e.Weapon, null); - Assert.True(e.Weapon.Range < blast, "fixture: the runtime Range must have fallen below the blast"); + Assert.True(e.Weapon.Range < 35f, "fixture: the runtime Range must have fallen below the arming distance"); } + + var shotId = FireControl.Fire(e.Weapon, e.WeaponItem, e.Shooter); + + Assert.Equal(fires, shotId != 0); + Assert.Equal(fires ? 1 : 0, e.Zone.PendingShots.Count); + } + + // The same refusal through the weapon a player uses: a refused round sounds nothing (OnFire never runs) and + // queues nothing, and the same weapon with Range to spare fires once. Kills: Execute still announcing a + // refused round (Fire's 0 handed to the presentation as a shot id). + [Theory] + [InlineData(34.5f, 0)] + [InlineData(40f, 1)] + public void ARefusedRoundIsNotAnnounced(float range, int fired) + { + var e = Build(TestSettings(), SolidShape(5, 4), velocity: 0, fuse: WeaponFuse.Proximity, blastRadius: 30f, damage: 100f, weaponRange: range); + e.Shooter.Target.Value = null; + Aim(e, float2(0, 1)); + var announced = 0; var committed = 0; + e.Weapon.OnFire += _ => announced++; using var c = e.Zone.ShotCommitted.Subscribe(_ => committed++); - Assert.Equal(fires, e.Weapon.FuseAllowsFire); e.Weapon.Activate(); - // The starved row must not run a full Update first: the power bus would restore the supply we forced. - // Activate queues the burst inside Trigger, so one short tick is enough for a commit if it fires. for (var i = 0; i < 5; i++) e.Zone.Update(.01f); - Assert.Equal(fires ? 1 : 0, committed); + Assert.Equal(fired, announced); + Assert.Equal(fired, committed); + } + + // Planar, everywhere the gate, the clamp and the burst meet (R7). A target 50 units above the plane at a + // planar range of 40 is 64 units away in three dimensions: inside a Range of 60 by the plane, outside it by + // the straight line. It is designated, rides with the round, and the HUD's diagnostic agrees. Kills: a 3D + // length in PFire's range or Inspect's. + [Fact] + public void ATargetsHeightDoesNotMoveTheRangeGate() + { + var e = Build(TestSettings(), SolidShape(5, 4), velocity: 0, fuse: WeaponFuse.Proximity, blastRadius: 4f, damage: 100f, + weaponRange: 60f, targetRange: 40f); + e.Target.Position += float3(0, 50, 0); + Aim(e, float2(0, 1)); + + FireControl.Fire(e.Weapon, e.WeaponItem, e.Shooter); + + var shot = SafeAssert.OnlyShot(e.Zone); + Assert.True(shot.Target == e.Target, "the target is inside Range in the plane, so it is valid data"); + Assert.Equal(40f, Planar(e.Shooter.Position, shot.BurstPosition), 2); + var diagnostic = FireControl.Inspect(e.Weapon, e.Shooter, e.Target); + Assert.True(diagnostic.InRange, "the HUD's range gate is planar too"); + Assert.Equal(40f, diagnostic.Range, 2); + } + + // The F4 clamp is planar as well: the target's height does not change how far the burst is pulled back. + [Fact] + public void TheRangeClampIgnoresTheTargetsHeight() + { + var e = Build(TestSettings(), SolidShape(5, 4), velocity: 20f, fuse: WeaponFuse.Proximity, blastRadius: 4f, damage: 100f, + weaponRange: 100f, targetRange: 90f); + e.Target.Position += float3(0, 200, 0); + e.Target.Velocity = float2(6f, 8f); + var intercept = FireControl.PredictedIntercept(e.Weapon, e.Shooter, e.Target); + Assert.True(Planar(e.Shooter.Position, intercept) > 100f, "fixture: the intercept must lie beyond Range in the plane"); + + FireControl.Fire(e.Weapon, e.WeaponItem, e.Shooter); + + var shot = SafeAssert.OnlyShot(e.Zone); + Assert.Equal(100f, Planar(e.Shooter.Position, shot.BurstPosition), 2); } // Nothing bursts beyond max range. A target receding faster than the round would be led past Range by the @@ -1459,7 +1638,7 @@ public sealed class FireControlCut124Tests : IDisposable FireControl.Fire(e.Weapon, e.WeaponItem, e.Shooter); - var shot = Assert.Single(e.Zone.PendingShots); + var shot = SafeAssert.OnlyShot(e.Zone); Assert.Equal(origin.x + toIntercept.x * 100f, shot.BurstPosition.x, 2); Assert.Equal(origin.z + toIntercept.y * 100f, shot.BurstPosition.z, 2); } @@ -1509,6 +1688,7 @@ public sealed class FireControlCut124Tests : IDisposable Assert.True(results.Any(o => o.Result == ShotResult.Hit), "the stationary, fully-accurate target must be hit at least once"); Assert.True(results.All(o => abs(o.BurstPoint.x - e.Target.Position.x) < 1e-3f && abs(o.BurstPoint.y - e.Target.Position.z) < 1e-3f), "a hit detonates at the intercept too, not at the origin"); + Assert.True(results.All(o => o.HasBurstPoint), "a proximity outcome names its burst point, hit or not: presentation retargets to it"); Assert.True(damage > 0f, "the blasts must have landed"); } @@ -1527,6 +1707,7 @@ public sealed class FireControlCut124Tests : IDisposable Assert.NotNull(resolved); Assert.Equal(ShotResult.Miss, resolved.Result); + Assert.False(resolved.HasBurstPoint, "a contact or delayed round has no frozen burst point"); Assert.True(Alive(e, bystander), "a missed contact or delayed round detonates nowhere"); } @@ -1540,7 +1721,7 @@ public sealed class FireControlCut124Tests : IDisposable var e = Build(TestSettings(), SolidShape(5, 4), velocity: 20f, fuse: WeaponFuse.Proximity, blastRadius: 4f, damage: 100f); FireControl.Fire(e.Weapon, e.WeaponItem, e.Shooter); - var shot = Assert.Single(e.Zone.PendingShots); + var shot = SafeAssert.OnlyShot(e.Zone); Assert.Equal(shot.FireTime + 4.5f, shot.CommitTime, 3); e.Zone.Update(.01f); Assert.False(e.Zone.PendingShots[0].Committed, "a targeted round reads the target's facing late"); @@ -1625,7 +1806,7 @@ public sealed class FireControlCut124Tests : IDisposable Assert.True(Alive(e, blocker)); e.Zone.Update(1.1f); - Assert.Empty(e.Zone.PendingShots); + SafeAssert.NoShots(e.Zone); Assert.False(Alive(e, blocker)); } @@ -1650,7 +1831,7 @@ public sealed class FireControlCut124Tests : IDisposable e.Zone.Entities.Remove(e.Shooter); e.Zone.Update(4f); - Assert.Empty(e.Zone.PendingShots); + SafeAssert.NoShots(e.Zone); Assert.False(Alive(e, witness), "the round must still burst at max range"); } @@ -1668,7 +1849,7 @@ public sealed class FireControlCut124Tests : IDisposable e.Zone.Entities.Remove(e.Shooter); e.Zone.Update(6f); - Assert.Empty(e.Zone.PendingShots); + SafeAssert.NoShots(e.Zone); Assert.Equal(5.65884256f, damage, 3); } @@ -1690,7 +1871,7 @@ public sealed class FireControlCut124Tests : IDisposable e.Zone.Entities.Remove(e.Shooter); e.Zone.Update(6f); - Assert.Empty(e.Zone.PendingShots); + SafeAssert.NoShots(e.Zone); Assert.Equal(12, resolved.Count); Assert.True(resolved.Any(o => o.Hit), "at least one round must land on the stationary target"); Assert.Equal(resolved.Count(o => o.Hit), struck); @@ -1714,6 +1895,7 @@ public sealed class FireControlCut124Tests : IDisposable Assert.NotNull(committed); Assert.Equal(ShotResult.Burst, committed.Result); + Assert.True(committed.HasBurstPoint); Assert.False(committed.Hit); var burst = PointAlong(e, normalize(float2(2, 1)), NoLockRange); Assert.Equal(burst.x, committed.BurstPoint.x, 3); @@ -1816,7 +1998,7 @@ public sealed class FireControlCut124Tests : IDisposable blocker.Position = PointAlong(e, float2(0, 1), 25f); e.Zone.Update(4f); - Assert.Empty(e.Zone.PendingShots); + SafeAssert.NoShots(e.Zone); Assert.True(Alive(e, blocker), "the round flew through the hull that arrived after Fire and burst at max range"); } @@ -1865,27 +2047,6 @@ public sealed class FireControlCut124Tests : IDisposable Assert.Equal(origin.z + (withBlocker ? 21f : 60f), committed.BurstPoint.y, 2); } - // A round fired from inside another hull bursts at the muzzle: the origin is already in metal, a distance of - // zero. Kills: reading a cell's Entry for its Exit (Soul's M7: a cell the origin sits in is skipped and the - // round flies on); dropping the clamp at zero (Soul's M8: the negative distance puts the burst behind the - // muzzle). - [Fact] - public void AContactRoundFiredFromInsideAnotherHullBurstsAtTheMuzzle() - { - var e = Build(TestSettings(), SolidShape(5, 4), velocity: 20f, fuse: WeaponFuse.Contact, blastRadius: 4f, damage: 100f, weaponRange: 60f); - e.Shooter.Target.Value = null; - Aim(e, float2(0, 1)); - AddShip(e, e.Shooter.Position + float3(0.5f, 0, 0.5f), 1f, 300); - ShotOutcome committed = null; - using var c = e.Zone.ShotCommitted.Subscribe(o => committed = o); - - FireControl.Fire(e.Weapon, e.WeaponItem, e.Shooter); - e.Zone.Update(.01f); - - Assert.Equal(e.Shooter.Position.x, committed.BurstPoint.x, 2); - Assert.Equal(e.Shooter.Position.z, committed.BurstPoint.y, 2); - } - // A hull wholly behind the muzzle is not on the round's way. Kills: admitting cells whose exit lies behind // the origin (the round would burst at the hull behind it, at distance zero). [Fact] @@ -1941,10 +2102,10 @@ public sealed class FireControlCut124Tests : IDisposable e.Zone.Update(6f); foreach (var s in subscriptions) s.Dispose(); - Assert.Empty(e.Zone.PendingShots); + SafeAssert.NoShots(e.Zone); Assert.True(bystanders.All(b => Alive(e, b)), "no bystander may take blast damage"); Assert.Equal(0f, damage); - Assert.Equal(ShotResult.Miss, Assert.Single(resolved).Result); + Assert.Equal(ShotResult.Miss, SafeAssert.Only(resolved).Result); } // A no-lock round outlives a shooter destroyed outright in flight, for every fuse: Death removes the shooter @@ -1968,7 +2129,7 @@ public sealed class FireControlCut124Tests : IDisposable Assert.False(Alive(e, e.Shooter)); Assert.False(Alive(e, witness), "the round must still burst at max range"); - Assert.Equal(ShotResult.Burst, Assert.Single(resolved).Result); + Assert.Equal(ShotResult.Burst, SafeAssert.Only(resolved).Result); } // ToWorldPoint and ToSchematicPoint are inverses, and ToWorldPoint puts one cell to starboard of the centre @@ -2116,7 +2277,7 @@ public sealed class FireControlCut124Tests : IDisposable Assert.Equal(ShotResult.Hit, outcome.Result); Assert.True(armorEvents > 0); var shot = e.Zone.PendingShots.Count == 0 ? null : (PendingShot?) e.Zone.PendingShots[0]; - Assert.Null(shot); // resolved and removed -- confirms it went through Apply's null-fuse path to completion + Assert.True(!shot.HasValue, "the shot must be resolved and removed"); // resolved and removed -- confirms it went through Apply's null-fuse path to completion } // ARadiusWithoutAFuseIsADirectHit (Soul F1): the symmetric inert half -- BlastRadius > 0 but the catalog diff --git a/tests/Aetheria.Shared.Tests/FireControlCut5Tests.cs b/tests/Aetheria.Shared.Tests/FireControlCut5Tests.cs index 525a24ce6..e1366721e 100644 --- a/tests/Aetheria.Shared.Tests/FireControlCut5Tests.cs +++ b/tests/Aetheria.Shared.Tests/FireControlCut5Tests.cs @@ -621,12 +621,12 @@ public sealed class FireControlCut5Tests : IDisposable var e = Build(TestSettings(), damage: 10000, velocity: 0, accuracy: 1, resolution: 1, spread: 0, hullDurability: 5, armor: 0); // one hit is lethal - Assert.Contains(e.Target, e.Zone.Entities); + SafeAssert.In(e.Zone, e.Target); FireControl.Fire(e.Weapon, e.WeaponItem, e.Shooter); e.Zone.Update(.1f); Assert.True(e.Target.Hull.Durability < .01f); - Assert.DoesNotContain(e.Target, e.Zone.Entities); + SafeAssert.NotIn(e.Zone, e.Target); } // ---- 5.7: delete the two carried-and-unread fields (Soul finding 12). ---- diff --git a/tests/Aetheria.Shared.Tests/FireControlCut8Tests.cs b/tests/Aetheria.Shared.Tests/FireControlCut8Tests.cs index 81256f3ec..96fb785d1 100644 --- a/tests/Aetheria.Shared.Tests/FireControlCut8Tests.cs +++ b/tests/Aetheria.Shared.Tests/FireControlCut8Tests.cs @@ -293,10 +293,10 @@ public sealed class FireControlCut8Tests : IDisposable e.Zone.Update(.1f); Assert.True(e.Target.Hull.Durability < .01f); - Assert.DoesNotContain(e.Target, e.Zone.Entities); + SafeAssert.NotIn(e.Zone, e.Target); Assert.False(e.Target.Active); Assert.Empty(e.Target.EntityInfoGathered); - Assert.Empty(e.Target.VisibleEntities); + Assert.True(e.Target.VisibleEntities.Count == 0, "the target's visible set must be empty"); } // ---- Ordering risk (8.1's own callout): EntityInstance's loot-drop subscription (EntityInstance.cs:292-325) diff --git a/tests/Aetheria.Shared.Tests/SafeAssert.cs b/tests/Aetheria.Shared.Tests/SafeAssert.cs new file mode 100644 index 000000000..2ab52d6bd --- /dev/null +++ b/tests/Aetheria.Shared.Tests/SafeAssert.cs @@ -0,0 +1,37 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +using System.Collections.Generic; +using Xunit; + +// Assertions over collections of entities and shots. xunit formats a collection's contents into its failure +// message, and an Entity, a PendingShot or a ShotOutcome drags the whole zone graph in with it: the formatter +// recurses until the test host dies of a stack overflow, which a mutation tool scores as a kill of every test +// that was running. A failure here must be an ordinary red naming the test, so these compare counts and +// identities and never hand xunit an entity. +internal static class SafeAssert +{ + public static void NoShots(Zone zone) => + Assert.True(zone.PendingShots.Count == 0, $"{zone.PendingShots.Count} shot(s) still pending"); + + public static PendingShot OnlyShot(Zone zone) + { + Assert.True(zone.PendingShots.Count == 1, $"expected exactly one pending shot, found {zone.PendingShots.Count}"); + return zone.PendingShots[0]; + } + + public static T Only(List list) + { + Assert.True(list.Count == 1, $"expected exactly one {typeof(T).Name}, found {list.Count}"); + return list[0]; + } + + public static void Empty(List list) => Assert.True(list.Count == 0, $"expected no {typeof(T).Name}, found {list.Count}"); + + public static void In(Zone zone, Entity entity, string why = "entity should be in the zone") => + Assert.True(zone.Entities.Contains(entity), why); + + public static void NotIn(Zone zone, Entity entity, string why = "entity should have left the zone") => + Assert.False(zone.Entities.Contains(entity), why); +}