Recover Rocketship flight components against original-code probes
This commit is contained in:
1 parent
c6eb1861cd
commit
a274670fe8
23 files changed
+2462
-2
No files matched your search
@@ -0,0 +1,110 @@
|
||||
namespace GamePort.Game;
|
||||
|
||||
public readonly record struct RocketFlightTuning(float Acceleration, float Deceleration,
|
||||
float MaximumSpeed, float MaximumHeight, float UpRate, float DownRate, float MaximumVerticalSpeed);
|
||||
|
||||
public readonly record struct RocketSteering(float Speed, float Bank, bool KeyboardBank = false,
|
||||
bool Up = false, bool Down = false);
|
||||
|
||||
public readonly record struct RocketVerticalState(float Height, float Velocity, float CommandedAcceleration);
|
||||
public readonly record struct RocketVerticalStep(RocketVerticalState State, float BodyAcceleration);
|
||||
|
||||
/// <summary>
|
||||
/// Bounded ordinary keyboard branch of C3DFlyingObject41A6C0. Input updates
|
||||
/// retained scalars/flags after41A140's application in the ordinary update. Mouse,
|
||||
/// analog, boost/roll, camera and body integration are separate contracts.
|
||||
/// See gameplay/rocketship.md; this component does not enable a live vehicle.
|
||||
/// </summary>
|
||||
public static class RocketFlight
|
||||
{
|
||||
public static RocketSteering StepKeyboardInput(RocketSteering state, PlayerInput input,
|
||||
bool supported, float seconds, RocketFlightTuning tuning)
|
||||
{
|
||||
Validate(tuning, seconds);
|
||||
if (!float.IsFinite(state.Speed) || !float.IsFinite(state.Bank))
|
||||
throw new ArgumentOutOfRangeException(nameof(state));
|
||||
|
||||
var bank = state.Bank;
|
||||
var keyboardBank = state.KeyboardBank;
|
||||
if (input.Left) { bank += 160 * seconds; keyboardBank = true; }
|
||||
else if (input.Right) { bank -= 160 * seconds; keyboardBank = true; }
|
||||
else if (keyboardBank)
|
||||
bank = bank < 0 ? MathF.Min(0, bank + 130 * seconds) : MathF.Max(0, bank - 130 * seconds);
|
||||
|
||||
var speed = state.Speed;
|
||||
//41A8C2: acceleration is per callback, unlike the later500*dt support
|
||||
//loss. Do not convert it into a conventional acceleration-per-second.
|
||||
if (input.Forward)
|
||||
{
|
||||
if (speed < tuning.MaximumSpeed) speed = MathF.Min(tuning.MaximumSpeed, speed + tuning.Acceleration);
|
||||
}
|
||||
else if (speed > tuning.MaximumSpeed * .9090909361839294f)
|
||||
speed -= tuning.Deceleration * .5f;
|
||||
|
||||
if (supported) speed = MathF.Max(0, speed - 500 * seconds);
|
||||
|
||||
//41A9CA: Down still brakes when Up is also held; Up only wins the
|
||||
//published vertical flag after this scalar update.
|
||||
if (input.Backward)
|
||||
{
|
||||
if (speed > 0) speed = MathF.Max(0, speed - tuning.Deceleration * (supported ? seconds : .1f));
|
||||
else if (speed < 0) speed = MathF.Min(0, speed + tuning.Deceleration);
|
||||
}
|
||||
return new(speed, Math.Clamp(bank, -45, 45), keyboardBank,
|
||||
input.Forward, input.Backward && !input.Forward);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///41A47F..41A6A8 vertical publication only. World/body integration precedes
|
||||
/// this callback; UpRate/DownRate are requested acceleration, not velocity.
|
||||
/// The below100 branch retains65C and does not clamp height. The height
|
||||
/// clamp changes root Y, not the current velocity. Caller retains X/Z.
|
||||
/// </summary>
|
||||
public static RocketVerticalStep StepVertical(RocketVerticalState state,
|
||||
RocketSteering steering, RocketFlightTuning tuning, float gravity)
|
||||
{
|
||||
ValidateTuning(tuning);
|
||||
if (!float.IsFinite(state.Height) || !float.IsFinite(state.Velocity)
|
||||
|| !float.IsFinite(state.CommandedAcceleration) || !float.IsFinite(steering.Speed)
|
||||
|| !float.IsFinite(gravity)) throw new ArgumentOutOfRangeException(nameof(state));
|
||||
|
||||
var height = state.Height;
|
||||
var velocity = state.Velocity;
|
||||
var command = state.CommandedAcceleration;
|
||||
if (steering.Speed >= 100)
|
||||
{
|
||||
if (height >= tuning.MaximumHeight)
|
||||
{
|
||||
height = tuning.MaximumHeight;
|
||||
command = steering.Down ? tuning.DownRate : 0;
|
||||
}
|
||||
else if (steering.Up) command = tuning.UpRate;
|
||||
else if (steering.Down) command = tuning.DownRate;
|
||||
else
|
||||
{
|
||||
command = velocity > 0 ? -160 : 160;
|
||||
if (velocity > -60 && velocity < 60) { velocity = 0; command = 0; }
|
||||
}
|
||||
}
|
||||
velocity = Math.Clamp(velocity, -tuning.MaximumVerticalSpeed, tuning.MaximumVerticalSpeed);
|
||||
return new(new(height, velocity, command), command - gravity);
|
||||
}
|
||||
|
||||
private static void Validate(RocketFlightTuning tuning, float seconds)
|
||||
{
|
||||
if (!float.IsFinite(seconds) || seconds <= 0 || seconds >= 1)
|
||||
throw new ArgumentOutOfRangeException(nameof(seconds));
|
||||
ValidateTuning(tuning);
|
||||
}
|
||||
|
||||
private static void ValidateTuning(RocketFlightTuning tuning)
|
||||
{
|
||||
if (!float.IsFinite(tuning.Acceleration) || tuning.Acceleration < 0
|
||||
|| !float.IsFinite(tuning.Deceleration) || tuning.Deceleration < 0
|
||||
|| !float.IsFinite(tuning.MaximumSpeed) || tuning.MaximumSpeed <= 0
|
||||
|| !float.IsFinite(tuning.MaximumHeight) || !float.IsFinite(tuning.UpRate)
|
||||
|| !float.IsFinite(tuning.DownRate) || !float.IsFinite(tuning.MaximumVerticalSpeed)
|
||||
|| tuning.MaximumVerticalSpeed < 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(tuning));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user