196 lines
12 KiB
C#
196 lines
12 KiB
C#
using GamePort.Game;
|
|
using System.Text.Json;
|
|
|
|
var checks = new (string Name, Action Check)[]
|
|
{
|
|
("acceleration is per callback and does not pull an overspeed value down", Acceleration),
|
|
("support loss follows acceleration, including both vertical keys", SupportOrder),
|
|
("coast threshold is strict and loss is per callback", Coasting),
|
|
("braking depends on support and clamps both signs", Braking),
|
|
("Up wins published flags after simultaneous Down braking", BothDirections),
|
|
("left precedence, bank limits and prior keyboard release", Bank),
|
|
("vertical rates publish acceleration after compensating gravity", VerticalCommands),
|
|
("strict neutral velocity band and symmetric existing velocity cap", VerticalBand),
|
|
("height clamp retains velocity and allows Down acceleration", HeightClamp),
|
|
("low scalar retains acceleration and skips height clamp", LowSpeed),
|
|
("fourteen copied-original input-prefix numeric references", OriginalReferences),
|
|
("fourteen copied-original vertical-suffix numeric references", OriginalVerticalReferences),
|
|
("31 copied-original transform/motion/maneuver/facing/camera cases", MotionReferences.Run),
|
|
("state, tuning and timestep admission", Invalid),
|
|
};
|
|
foreach (var (name, check) in checks) { check(); Console.WriteLine($"PASS {name}"); }
|
|
Console.WriteLine($"Rocket flight checks: {checks.Length} passed. Recovered arithmetic; not live/original gameplay verification.");
|
|
|
|
static RocketFlightTuning Tuning() => new(400, 1, 1400, 3900, 650, -650, 650);
|
|
static PlayerInput Input(bool up = false, bool down = false, bool left = false, bool right = false)
|
|
=> new(up, down, left, right, false);
|
|
static RocketSteering Step(float speed = 0, PlayerInput input = default, bool supported = false,
|
|
float dt = .1f, float bank = 0, bool keyboardBank = false)
|
|
=> RocketFlight.StepKeyboardInput(new(speed, bank, keyboardBank), input, supported, dt, Tuning());
|
|
|
|
static void Acceleration()
|
|
{
|
|
Near(Step(input: Input(up:true), dt:.01f).Speed,400,"short callback");
|
|
Near(Step(input: Input(up:true), dt:.5f).Speed,400,"long callback");
|
|
Near(Step(1300,Input(up:true)).Speed,1400,"cap crossing");
|
|
Near(Step(1500,Input(up:true)).Speed,1500,"retained overspeed");
|
|
}
|
|
static void SupportOrder()
|
|
{
|
|
Near(Step(input:Input(up:true),supported:true).Speed,350,"accelerate then support loss");
|
|
Near(Step(25,supported:true).Speed,0,"support loss clamps");
|
|
Near(Step(100,Input(up:true,down:true),supported:true).Speed,449.9f,"accel, support, then Down");
|
|
Near(Step(-20,supported:true).Speed,0,"negative supported scalar");
|
|
}
|
|
static void Coasting()
|
|
{
|
|
var threshold=1400*.9090909361839294f;
|
|
Near(Step(threshold).Speed,threshold,"equal threshold");
|
|
Near(Step(1400,dt:.01f).Speed,1399.5f,"short callback coast");
|
|
Near(Step(1400,dt:.5f).Speed,1399.5f,"long callback coast");
|
|
Near(Step(1000).Speed,1000,"low coast retains scalar");
|
|
}
|
|
static void Braking()
|
|
{
|
|
Near(Step(100,Input(down:true),dt:.25f).Speed,99.9f,"air brake");
|
|
Near(Step(500,Input(down:true),supported:true,dt:.25f).Speed,374.75f,"support brake");
|
|
Near(Step(.05f,Input(down:true)).Speed,0,"positive crossing");
|
|
Near(Step(-.5f,Input(down:true)).Speed,0,"negative crossing");
|
|
Near(Step(-3,Input(down:true)).Speed,-2,"negative per-call recovery");
|
|
Near(Step(1400,Input(down:true)).Speed,1399.4f,"coast precedes brake");
|
|
}
|
|
static void BothDirections()
|
|
{
|
|
var both=Step(0,Input(up:true,down:true));
|
|
Near(both.Speed,399.9f,"Down brakes newly accelerated speed");
|
|
Check(both.Up&&!both.Down,"Up wins flags");
|
|
Check(Step(input:Input(down:true)).Down,"Down alone");
|
|
Check(!Step().Up&&!Step().Down,"fresh flags clear old requests");
|
|
}
|
|
static void Bank()
|
|
{
|
|
Near(Step(input:Input(left:true,right:true)).Bank,16,"left precedence");
|
|
Near(Step(input:Input(right:true)).Bank,-16,"right sign");
|
|
Near(Step(input:Input(left:true),bank:44).Bank,45,"positive cap");
|
|
Near(Step(input:Input(right:true),bank:-44).Bank,-45,"negative cap");
|
|
Near(Step(bank:20,keyboardBank:true).Bank,7,"release positive");
|
|
Near(Step(bank:-20,keyboardBank:true).Bank,-7,"release negative");
|
|
Near(Step(bank:5,keyboardBank:true).Bank,0,"release crossing");
|
|
Near(Step(bank:20).Bank,20,"non-keyboard bank retained");
|
|
Check(Step(input:Input(left:true)).KeyboardBank,"keyboard origin set");
|
|
}
|
|
static RocketVerticalStep Vertical(float height=1000,float velocity=0,float command=0,
|
|
float speed=500,bool up=false,bool down=false)
|
|
=> RocketFlight.StepVertical(new(height,velocity,command),new(speed,0,Up:up,Down:down),Tuning(),-800);
|
|
static void VerticalCommands()
|
|
{
|
|
var up=Vertical(up:true);
|
|
Near(up.State.CommandedAcceleration,650,"up command");
|
|
Near(up.BodyAcceleration,1450,"up compensates gravity");
|
|
Near(up.State.Velocity,0,"up does not assign velocity");
|
|
Near(Vertical(down:true).BodyAcceleration,150,"down compensates gravity");
|
|
Near(Vertical().BodyAcceleration,800,"neutral cancels gravity");
|
|
Near(Vertical(up:true,down:true).State.CommandedAcceleration,650,"below ceiling Up precedence");
|
|
Near(Vertical(speed:100,up:true).BodyAcceleration,1450,"inclusive speed threshold");
|
|
}
|
|
static void VerticalBand()
|
|
{
|
|
Near(Vertical(velocity:59.9f).State.Velocity,0,"positive snap");
|
|
Near(Vertical(velocity:-59.9f).State.Velocity,0,"negative snap");
|
|
Near(Vertical(velocity:60).State.CommandedAcceleration,-160,"positive boundary");
|
|
Near(Vertical(velocity:-60).State.CommandedAcceleration,160,"negative boundary");
|
|
Near(Vertical(velocity:700,up:true).State.Velocity,650,"upper cap");
|
|
Near(Vertical(velocity:-700,down:true).State.Velocity,-650,"lower cap");
|
|
Near(Vertical(velocity:700).State.CommandedAcceleration,-160,"neutral decision precedes cap");
|
|
}
|
|
static void HeightClamp()
|
|
{
|
|
var ceiling=Vertical(height:4000,velocity:123,up:true);
|
|
Near(ceiling.State.Height,3900,"ceiling root clamp");
|
|
Near(ceiling.State.Velocity,123,"height clamp preserves velocity");
|
|
Near(ceiling.State.CommandedAcceleration,0,"ceiling rejects Up acceleration");
|
|
Near(Vertical(height:3900,down:true).State.CommandedAcceleration,-650,"Down at equal ceiling");
|
|
Near(Vertical(height:3900,up:true,down:true).State.CommandedAcceleration,-650,"ceiling Down priority");
|
|
}
|
|
static void LowSpeed()
|
|
{
|
|
var low=Vertical(height:5000,velocity:700,command:-321,speed:99,up:true);
|
|
Near(low.State.Height,5000,"below100 does not clamp height");
|
|
Near(low.State.CommandedAcceleration,-321,"retained65C");
|
|
Near(low.BodyAcceleration,479,"retained command still compensates gravity");
|
|
Near(low.State.Velocity,650,"low speed still caps velocity");
|
|
}
|
|
static void OriginalReferences()
|
|
{
|
|
using var doc=JsonDocument.Parse(File.ReadAllText(Path.Combine(AppContext.BaseDirectory,"original-input-reference.json")));
|
|
var root=doc.RootElement;
|
|
Check(root.GetProperty("source_sha256").GetString()=="f5281076e1a2492f2d61f87aae3f4544b6d0f535ebe33f9fbb6f78710fe1aea8","original identity");
|
|
var tuning=root.GetProperty("tuning");
|
|
var parameters=Tuning() with{Acceleration=tuning.GetProperty("acceleration").GetSingle(),
|
|
Deceleration=tuning.GetProperty("deceleration").GetSingle(),MaximumSpeed=tuning.GetProperty("maximum_speed").GetSingle()};
|
|
Check(root.GetProperty("cases").GetArrayLength()==14,"reference count");
|
|
foreach(var sample in root.GetProperty("cases").EnumerateArray())
|
|
{
|
|
var name=sample.GetProperty("name").GetString();
|
|
var input=sample.GetProperty("input");var expected=sample.GetProperty("observed");
|
|
var keys=Input(input.GetProperty("up").GetBoolean(),input.GetProperty("down").GetBoolean(),
|
|
input.GetProperty("left").GetBoolean(),input.GetProperty("right").GetBoolean());
|
|
var state=new RocketSteering(input.GetProperty("speed").GetSingle(),input.GetProperty("bank").GetSingle(),
|
|
input.GetProperty("keyboard").GetBoolean());
|
|
var actual=RocketFlight.StepKeyboardInput(state,keys,input.GetProperty("supported").GetBoolean(),input.GetProperty("dt").GetSingle(),parameters);
|
|
Near(actual.Speed,expected.GetProperty("speed").GetSingle(),$"{name} scalar");
|
|
Near(actual.Bank,expected.GetProperty("bank").GetSingle(),$"{name} bank");
|
|
Check(actual.Up==expected.GetProperty("up").GetBoolean()&&actual.Down==expected.GetProperty("down").GetBoolean(),$"{name} vertical flags");
|
|
Check(actual.KeyboardBank==expected.GetProperty("keyboard").GetBoolean(),$"{name} bank origin");
|
|
//The source also observed destination Euler. It belongs to the still
|
|
//separate facing/rotation contract and is deliberately not compared here.
|
|
}
|
|
}
|
|
static void OriginalVerticalReferences()
|
|
{
|
|
using var doc=JsonDocument.Parse(File.ReadAllText(Path.Combine(AppContext.BaseDirectory,"original-vertical-reference.json")));
|
|
var root=doc.RootElement;
|
|
Check(root.GetProperty("source_sha256").GetString()=="f5281076e1a2492f2d61f87aae3f4544b6d0f535ebe33f9fbb6f78710fe1aea8","original vertical identity");
|
|
var tuning=root.GetProperty("tuning");
|
|
var parameters=Tuning() with{MaximumHeight=tuning.GetProperty("maximum_height").GetSingle(),
|
|
UpRate=tuning.GetProperty("up_rate").GetSingle(),DownRate=tuning.GetProperty("down_rate").GetSingle(),
|
|
MaximumVerticalSpeed=tuning.GetProperty("maximum_vertical_speed").GetSingle()};
|
|
Check(root.GetProperty("cases").GetArrayLength()==14,"vertical reference count");
|
|
foreach(var sample in root.GetProperty("cases").EnumerateArray())
|
|
{
|
|
var name=sample.GetProperty("name").GetString();
|
|
var input=sample.GetProperty("input");var expected=sample.GetProperty("observed");
|
|
var state=new RocketVerticalState(input.GetProperty("height").GetSingle(),input.GetProperty("velocity").GetSingle(),input.GetProperty("command").GetSingle());
|
|
var steering=new RocketSteering(input.GetProperty("speed").GetSingle(),0,Up:input.GetProperty("up").GetBoolean(),Down:input.GetProperty("down").GetBoolean());
|
|
var actual=RocketFlight.StepVertical(state,steering,parameters,input.GetProperty("gravity").GetSingle());
|
|
Near(actual.State.Height,expected.GetProperty("height").GetSingle(),$"{name} height");
|
|
Near(actual.State.Velocity,expected.GetProperty("velocity").GetSingle(),$"{name} velocity");
|
|
Near(actual.State.CommandedAcceleration,expected.GetProperty("command").GetSingle(),$"{name} retained65C");
|
|
Near(actual.BodyAcceleration,expected.GetProperty("bodyacceleration").GetSingle(),$"{name} published acceleration");
|
|
//X/Z were preserved by the original suffix. This scalar component
|
|
//returns only Y and cannot overwrite the caller's X/Z vectors.
|
|
}
|
|
}
|
|
static void Invalid()
|
|
{
|
|
foreach(var dt in new[]{0f,-.1f,1,float.NaN,float.PositiveInfinity})
|
|
Reject(()=>Step(dt:dt));
|
|
Reject(()=>Step(float.NaN)); Reject(()=>Step(bank:float.PositiveInfinity));
|
|
Reject(()=>RocketFlight.StepKeyboardInput(default,default,false,.1f,Tuning() with{MaximumSpeed=0}));
|
|
Reject(()=>RocketFlight.StepKeyboardInput(default,default,false,.1f,Tuning() with{Deceleration=-1}));
|
|
Reject(()=>Vertical(height:float.NaN)); Reject(()=>Vertical(velocity:float.PositiveInfinity));
|
|
Reject(()=>Vertical(command:float.NaN)); Reject(()=>Vertical(speed:float.NaN));
|
|
Reject(()=>RocketFlight.StepVertical(default,default,Tuning(),float.NaN));
|
|
}
|
|
static void Near(float value,float expected,string message)
|
|
{
|
|
if(!float.IsFinite(value)||!float.IsFinite(expected)||MathF.Abs(value-expected)>.0002f)
|
|
throw new Exception($"{message}: {value} != {expected}");
|
|
}
|
|
static void Check(bool condition,string message) { if(!condition)throw new Exception(message); }
|
|
static void Reject(Action action)
|
|
{
|
|
try { action(); } catch(ArgumentOutOfRangeException) { return; }
|
|
throw new Exception("Expected invalid input rejection");
|
|
}
|