Aetheria/tests/Aetheria.Shared.Tests/MiningCut4bTests.cs
2026-11-12 21:26:52 +00:00

337 lines
16 KiB
C#

/* 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;
using System.Collections.Generic;
using System.Linq;
using CultMath;
using UniRx;
using Xunit;
using static CultMath.math;
using float2 = CultMath.float2;
using float3 = CultMath.float3;
// Mining Cut 4b (docs/mining-cut-refresh.md, Q12 = A): a shot at a rock goes through FireControl's one fire path
// and wears it; the rock is a disc. The fixture is MiningCut3Tests' belt scene: a shooter at the origin, one rock
// straight ahead (+z, phase .25) at `Distance`, a gun and a targeting system fitted on the shooter.
public sealed class MiningCut4bTests : IDisposable
{
private const float Distance = 100f;
private readonly MiningCut3Tests _fixtures = new MiningCut3Tests();
public void Dispose() => _fixtures.Dispose();
private sealed class Rig
{
public MiningCut3Tests.Scene Scene;
public Zone Zone => Scene.Zone;
public Ship Shooter;
public EquippedItem GunItem;
public Weapon Gun;
public ChunkId Rock;
public List<ShotOutcome> Resolved = new List<ShotOutcome>();
}
private static InstantWeaponData Behavior(bool launcher, float damage, float velocity, float spread, float effective = float.PositiveInfinity)
{
InstantWeaponData data = launcher ? new LockWeaponData
{
LockSpeed = MiningCut3Tests.Constant(1000), SensorImpact = MiningCut3Tests.Constant(1), LockAngle = MiningCut3Tests.Constant(180),
DirectionImpact = MiningCut3Tests.Constant(1), Decay = MiningCut3Tests.Constant(1)
} : new InstantWeaponData();
data.Damage = MiningCut3Tests.Constant(damage);
data.Range = MiningCut3Tests.Constant(500f);
data.MinRange = MiningCut3Tests.Constant(0);
data.EffectiveRange = MiningCut3Tests.Constant(effective);
data.DamageAtMaxRange = 0f;
data.Velocity = MiningCut3Tests.Constant(velocity);
data.Spread = MiningCut3Tests.Constant(spread);
data.DamageSpread = MiningCut3Tests.Constant(0);
data.Penetration = MiningCut3Tests.Constant(0);
data.Count = MiningCut3Tests.Constant(1);
data.BurstTime = MiningCut3Tests.Constant(0);
data.Cooldown = MiningCut3Tests.Constant(1000);
return data;
}
// `precision` 1 gives sigma 1 cell; accuracy 1 and a resolution that asks little of the info make PSensor 1.
private Rig Build(bool sensor = true, bool launcher = false, float damage = 10f, float velocity = 0f, float spread = 0f,
float precision = 1f, float effective = float.PositiveInfinity, WeaponFuse? fuse = null, float blastRadius = 0f,
float rockSize = 0f, float crossSection = 20f, bool target = true)
{
var kind = MiningCut3Tests.Kind("Asteroid", crossSection);
var scene = _fixtures.BuildScene(new[] { kind }, MiningCut3Tests.Belt(kind, MiningCut3Tests.Rock(Distance, .25f, rockSize)));
var gear = MiningCut3Tests.Gear("Targeting", new TargetingSystemData
{
Accuracy = MiningCut3Tests.Constant(1f), Resolution = MiningCut3Tests.Constant(1000f),
Precision = MiningCut3Tests.Constant(precision), Tracking = MiningCut3Tests.Constant(1000000f)
});
scene.Cache.Upsert(gear);
var gun = new WeaponItemData
{
Name = "Gun", Hardpoint = HardpointType.Sensors, Shape = new Shape(), Durability = 1,
MinimumTemperature = -1000, MaximumTemperature = 1000, OptimalTemperature = 0, PlateauWidth = 2000,
Fuse = fuse, BlastRadius = blastRadius,
Behaviors = { Behavior(launcher, damage, velocity, spread, effective) }
};
scene.Cache.Upsert(gun);
var shape = new Shape(5, 5);
foreach (var cell in shape.AllCoordinates) shape[cell] = true;
var hull = new HullData
{
Name = "Gunship", HullType = HullType.Ship, Shape = shape, Durability = 10, Mass = 1000,
Hardpoints = { new HardpointData { Type = HardpointType.Sensors, Position = new int2(0, 0), Shape = new Shape() } }
};
scene.Cache.Upsert(hull);
var shooter = new Ship(scene.Items, scene.Zone, MiningCut3Tests.Mint(scene, hull), new EntitySettings());
if (sensor) Assert.True(shooter.TryEquip(MiningCut3Tests.Mint(scene, scene.Eye)), "eye");
Assert.True(shooter.TryEquip(MiningCut3Tests.Mint(scene, gear)), "targeting");
var minted = MiningCut3Tests.Mint(scene, gun);
Assert.True(shooter.TryEquip(minted, new int2(0, 0)), "gun");
scene.Zone.Admit(shooter, piloted: false);
shooter.Position = float3.zero;
shooter.Direction = float2(0, 1);
shooter.Aim = float3(0, 0, 1);
shooter.Update(.05f); // the fitted sensor becomes active on its first tick; zone time does not advance
shooter.Position = float3.zero;
shooter.Velocity = float2.zero;
var item = shooter.Equipment.Single(x => x.EquippableItem == minted);
var rig = new Rig
{
Scene = scene, Shooter = shooter, GunItem = item, Gun = (Weapon) item.Behaviors.Single(b => b is Weapon),
Rock = new ChunkId(scene.Belts[0], 0)
};
scene.Zone.ShotResolved.Subscribe(outcome => rig.Resolved.Add(outcome));
if (target && sensor) Assert.True(shooter.SetTarget(rig.Rock), "target");
return rig;
}
private static float Phi(double z) => (float) (.5 * (1 + erf((float) (z / Math.Sqrt(2)))));
// The rock's radius in cells is the one the zone reports, sigma the targeting system's (floored at half a cell).
private static float Disc(Rig rig)
{
var sigma = max(1f / FireControl.Precision(rig.Shooter), .5f);
var radius = rig.Zone.ChunkCells(rig.Rock);
return Phi(radius / sigma) - Phi(-radius / sigma);
}
// PSensor as the rule states it, for a gear that wants only the detection threshold plus a thousandth of the rest.
private static float Sensor(Rig rig, float info)
{
var th = rig.Scene.Items.GameplaySettings.TargetDetectionInfoThreshold;
return saturate(unlerp(th, th + (1f - th) / 1000f, info));
}
// ==== one fire path ====
// Accuracy, PSensor, PSpread, Sigma and PMount price a rock exactly as a ship at equal range, precision and info:
// a two-cell ship (span 2, centre-aimed) and a rock one cell in radius (span 2) are the same disc to the gun, and
// an unauthored gun follows anything, so the mount is 1 for both.
[Fact]
public void ARockShotUsesTheSameFactors()
{
var rig = Build(spread: 4f, precision: 1f, crossSection: 1f);
var s = rig.Scene;
Assert.Equal(1f, rig.Zone.ChunkCells(rig.Rock), 4);
var info = rig.Shooter.ChunkInfo(rig.Rock);
Assert.True(info > .11f && info < 1f, $"the rock's info {info} should be visible and unsaturated");
var hull = new HullData { Name = "Pair", HullType = HullType.Ship, Shape = new Shape(2, 2), Durability = 10, Mass = 1000 };
foreach (var cell in hull.Shape.AllCoordinates) hull.Shape[cell] = true;
s.Cache.Upsert(hull);
var at = rig.Zone.ChunkPose(rig.Rock.Field, rig.Rock.Index).xy;
var ship = new Ship(s.Items, rig.Zone, MiningCut3Tests.Mint(s, hull), new EntitySettings());
rig.Zone.Admit(ship, piloted: false);
ship.Position = float3(at.x, 0, at.y);
ship.Direction = float2(0, 1);
rig.Shooter.EntityInfoGathered[ship] = info;
rig.Shooter.VisibleEntities.Add(ship);
var rock = FireControl.HitProbability(rig.Gun, rig.Shooter, rig.Rock);
var vessel = FireControl.HitProbability(rig.Gun, rig.Shooter, ship);
Assert.InRange(rock, .05f, .99f);
Assert.Equal(vessel, rock, 4);
}
[Fact]
public void AnUnseenRockCannotBeFiredOn()
{
var blind = Build(sensor: false);
Assert.False(FireControl.Designated(blind.Gun, blind.Shooter, blind.Rock, out var range));
Assert.Equal(Distance, range, 2);
Assert.Equal(0f, FireControl.HitProbability(blind.Gun, blind.Shooter, blind.Rock));
Assert.False(FireControl.Solve(blind.Gun, blind.GunItem, blind.Shooter, blind.Rock).Designated);
var sighted = Build();
Assert.True(FireControl.Designated(sighted.Gun, sighted.Shooter, sighted.Rock, out _));
Assert.True(FireControl.HitProbability(sighted.Gun, sighted.Shooter, sighted.Rock) > 0f);
}
// Q12 = A: a launcher (a LockWeapon) is never designated on a rock, locked or not.
[Fact]
public void LaunchersCannotFireOnARock()
{
var launcher = Build(launcher: true);
Assert.False(FireControl.Designated(launcher.Gun, launcher.Shooter, launcher.Rock, out _));
Assert.Equal(0f, FireControl.HitProbability(launcher.Gun, launcher.Shooter, launcher.Rock));
var plain = Build();
Assert.True(FireControl.Designated(plain.Gun, plain.Shooter, plain.Rock, out _));
}
// POnHull is the closed form Phi(R/sigma) - Phi(-R/sigma): a zero-spread gun with a full sensor reading is priced by the
// disc alone.
[Fact]
public void DiscMassIsTheClosedForm()
{
var rig = Build(spread: 0f, precision: 1f, rockSize: .5f);
var info = rig.Shooter.ChunkInfo(rig.Rock);
var expected = Sensor(rig, info) * Disc(rig);
Assert.InRange(Disc(rig), .5f, .99f);
Assert.Equal(expected, FireControl.HitProbability(rig.Gun, rig.Shooter, rig.Rock), 4);
}
// Commit's price at fire time is the forecast: for a rock still at the instant it is fired at, one price.
[Fact]
public void TheForecastIsTheCommitPriceForARock()
{
var rig = Build(spread: 4f, precision: 1f, velocity: 50f);
var forecast = FireControl.HitProbability(rig.Gun, rig.Shooter, rig.Rock);
var id = FireControl.Fire(rig.Gun, rig.GunItem, rig.Shooter);
Assert.True(rig.Zone.TryGetShot(id, out var shot));
Assert.Equal(rig.Rock, shot.Chunk);
Assert.Null(shot.Target);
Assert.Equal(forecast, FireControl.CommitProbability(shot, shot.FireTime, out _), 4);
}
// ==== resolution ====
private static void Run(Rig rig, float seconds)
{
for (var elapsed = 0f; elapsed < seconds; elapsed += .01f) rig.Zone.Update(.01f);
}
// A rock hit has no lane and no shield. Hits wear the rock by the shot's Damage after the falloff, and each
// publishes one absorb on the zone's ChunkCapabilities at the rock's surface facing the shot.
[Fact]
public void DirectHitsWearTheRock()
{
var rig = Build(damage: 3f, rockSize: .5f, precision: 1000f, effective: 50f);
var absorbs = new List<AbsorbEvent>();
var surface = new List<float>();
rig.Zone.ChunkCapabilities.Absorb.Subscribe(e =>
{
absorbs.Add(e);
var centre = rig.Zone.ChunkPose(rig.Rock.Field, rig.Rock.Index).xy;
surface.Add(length(float2(e.Position.x, e.Position.z) - centre) - rig.Zone.ChunkRadius(rig.Rock));
});
var before = rig.Zone.ChunkRadius(rig.Rock);
var factor = WeaponData.RangeFactor(0f, 50f, 500f, 0f, Distance);
Assert.InRange(factor, .01f, .99f);
for (var shot = 0; shot < 12; shot++)
{
FireControl.Fire(rig.Gun, rig.GunItem, rig.Shooter);
Run(rig, .02f);
}
var hits = rig.Resolved.Count(o => o.Hit);
Assert.InRange(hits, 1, 12);
Assert.Equal(hits, absorbs.Count);
Assert.All(rig.Resolved.Where(o => o.Hit), o => { Assert.Equal(0, o.Cell.x); Assert.Equal(0, o.Cell.y); Assert.False(o.Shielded); });
Assert.All(absorbs, e => Assert.Equal(3f * factor, e.Magnitude, 2));
Assert.All(surface, d => Assert.True(abs(d) < 1e-3f, $"absorb was {d} off the rock's surface"));
Assert.All(absorbs, e => Assert.Equal(AbsorbLayer.Hull, e.Layer));
var hp = MiningCut3Tests.PlanetSettings().AsteroidHitpoints.Evaluate(.5f);
var expected = MiningCut3Tests.PlanetSettings().AsteroidSize.Evaluate((hp - hits * 3f * factor) / hp * .5f);
Assert.Equal(expected, rig.Zone.ChunkRadius(rig.Rock), 3);
Assert.True(rig.Zone.ChunkRadius(rig.Rock) < before);
}
// A contact round that hits a rock detonates at its centre as it arrives, through Detonate: a bystander beside it
// takes the blast from the rock's centre, not from the muzzle or from a lane.
[Fact]
public void AContactRoundAtARockDetonatesAtItsCentre()
{
var rig = Build(damage: 10f, fuse: WeaponFuse.Contact, blastRadius: 6f, precision: 1000f, rockSize: .5f);
var s = rig.Scene;
var at = rig.Zone.ChunkPose(rig.Rock.Field, rig.Rock.Index).xy;
var bystander = new Ship(s.Items, rig.Zone, MiningCut3Tests.Mint(s, s.Hull), new EntitySettings());
rig.Zone.Admit(bystander, piloted: false);
bystander.Position = float3(at.x + 3f, 0, at.y);
var burstAt = new List<float>();
bystander.Capabilities.Absorb.Subscribe(e =>
{
var centre = rig.Zone.ChunkPose(rig.Rock.Field, rig.Rock.Index).xy;
burstAt.Add(length(float2(e.Position.x, e.Position.z) - centre));
});
for (var shot = 0; shot < 12; shot++)
{
FireControl.Fire(rig.Gun, rig.GunItem, rig.Shooter);
Run(rig, .02f);
}
Assert.InRange(rig.Resolved.Count(o => o.Hit), 1, 12);
Assert.NotEmpty(burstAt);
Assert.All(burstAt, d => Assert.True(d < 1e-2f, $"blast was {d} from the rock's centre"));
}
// A rock that no longer exists when its round arrives, broken in flight, resolves as a miss, like a target that
// left the zone: nothing is worn and nothing absorbs.
[Fact]
public void ARockThatBreaksMidFlightIsAMiss()
{
var rig = Build(velocity: 50f, precision: 1000f);
var absorbs = 0;
rig.Zone.ChunkCapabilities.Absorb.Subscribe(_ => absorbs++);
FireControl.Fire(rig.Gun, rig.GunItem, rig.Shooter);
Assert.Empty(rig.Resolved);
Assert.True(rig.Zone.Wear(rig.Rock, 1e6f));
Run(rig, 2.5f);
var outcome = Assert.Single(rig.Resolved);
Assert.Equal(ShotResult.Miss, outcome.Result);
Assert.Equal(0, absorbs);
Assert.False(rig.Zone.ChunkExists(rig.Rock));
}
// The shot's own dice: two zones built alike roll the same sequence of outcomes for the same shot ids.
[Fact]
public void RockDiceAreTheShotsOwn()
{
List<ShotResult> Roll()
{
var rig = Build(precision: 1f);
for (var shot = 0; shot < 20; shot++)
{
FireControl.Fire(rig.Gun, rig.GunItem, rig.Shooter);
Run(rig, .02f);
}
return rig.Resolved.Select(o => o.Result).ToList();
}
var first = Roll();
var second = Roll();
Assert.Equal(20, first.Count);
Assert.Contains(ShotResult.Hit, first);
Assert.Contains(ShotResult.Miss, first);
Assert.Equal(first, second);
}
// A rock's velocity is the pose function's derivative, derived on every read.
[Fact]
public void ARockMovesAtItsPoseVelocity()
{
var rig = Build();
var velocity = rig.Zone.ChunkVelocity(rig.Rock);
var ahead = rig.Zone.ChunkPose(rig.Rock.Field, rig.Rock.Index, rig.Zone.Time + .5).xy;
var now = rig.Zone.ChunkPose(rig.Rock.Field, rig.Rock.Index).xy;
Assert.True(length(velocity) > 1f);
Assert.True(length(velocity * .5f - (ahead - now)) < .05f * length(ahead - now));
}
}