206 lines
9.3 KiB
C#
206 lines
9.3 KiB
C#
namespace GamePort.Game;
|
|
|
|
public readonly record struct JetpackBurstState(
|
|
int JumpState = 0, float HoldSeconds = 0, float EffectRemaining = 0);
|
|
|
|
public readonly record struct JetpackBurstInput(
|
|
bool Grounded, bool JumpHeld, float VelocityY, float ShadowDistance,
|
|
int InventoryState, float Energy, string LevelCode, int GadgetMode,
|
|
float DeltaSeconds, float CurrentExtraAccelerationY,
|
|
bool EffectAvailable = true, float GravityY = -800);
|
|
|
|
public enum JetpackPose { None, Jump, Fall, Fly }
|
|
|
|
public readonly record struct JetpackBurstResult(
|
|
JetpackBurstState State, float ImpulseY, float? VelocityYOverride,
|
|
float ExtraAccelerationY, JetpackPose Pose, float? RadiusOverride,
|
|
bool StartGadget, bool StopGadget, int? MovementModeOverride);
|
|
|
|
public readonly record struct JetpackEffectResult(
|
|
JetpackBurstState State, float Energy, bool Visible);
|
|
|
|
/// <summary>
|
|
/// The jump/jetpack branches of EXE437F90 and effect timer of EXE421FB0.
|
|
/// Controller and effect phases are separate. The normal route registry places
|
|
/// Jimmy before his retained fire; other full-scene phases remain incomplete. Horizontal
|
|
/// movement, shadow queries, visuals and sound callbacks belong to the caller.
|
|
/// </summary>
|
|
public static class JetpackBurst
|
|
{
|
|
public const float Duration = 8;
|
|
public const float HoldThreshold = .4f;
|
|
public const float JumpImpulse = 550;
|
|
public const float BurstInitialVelocityY = 10;
|
|
public const float BurstRadius = 100;
|
|
public const float LandingRadius = 60;
|
|
public const float EnergyPerSecond = .5f;
|
|
|
|
/// <summary>
|
|
/// Includes the outer update's ordinary grounded/released reset (437B9E).
|
|
/// CurrentExtraAccelerationY is required because the expired-effect branch
|
|
/// returns before writing acceleration. Results describe this invocation;
|
|
/// a newly started/stopped burst changes acceleration on the next call.
|
|
/// </summary>
|
|
public static JetpackBurstResult Step(JetpackBurstState state, JetpackBurstInput input)
|
|
{
|
|
ValidateState(state);
|
|
ValidateSeconds(input.DeltaSeconds);
|
|
RequireFinite(input.VelocityY);
|
|
RequireFinite(input.ShadowDistance);
|
|
RequireFinite(input.Energy);
|
|
RequireFinite(input.CurrentExtraAccelerationY);
|
|
RequireFinite(input.GravityY);
|
|
ArgumentNullException.ThrowIfNull(input.LevelCode);
|
|
var extra = input.CurrentExtraAccelerationY;
|
|
JetpackBurstResult Result(float impulse = 0, float? velocity = null,
|
|
JetpackPose pose = JetpackPose.None, float? radius = null,
|
|
bool start = false, bool stop = false, int? mode = null) =>
|
|
new(state, impulse, velocity, extra, pose, radius, start, stop, mode);
|
|
|
|
if (input.Grounded && !input.JumpHeld)
|
|
{
|
|
// This path runs ground apply instead of air apply; it does not
|
|
// itself clear the effect timer or restore the radius.
|
|
state = state with { JumpState = 0 };
|
|
return Result(mode: 0);
|
|
}
|
|
|
|
if (state.JumpState < 3)
|
|
extra = (float)((double)input.GravityY * 1.33f - input.GravityY);
|
|
else
|
|
{
|
|
if (input.EffectAvailable && state.EffectRemaining <= 0 && state.JumpState is 3 or 4)
|
|
{
|
|
state = state with { JumpState = 0, EffectRemaining = 0 };
|
|
return Result(pose: JetpackPose.Fall, stop: true, mode: 1);
|
|
}
|
|
// The near-floor constants are doubles in the original; the far
|
|
// branch uses float .1. Preserve those distinct constant widths.
|
|
extra = input.ShadowDistance < 500
|
|
? (float)(-input.GravityY - (input.VelocityY < -1 ? .3 : .1) * input.GravityY)
|
|
: .1f * input.GravityY - input.GravityY;
|
|
}
|
|
|
|
if (!input.JumpHeld)
|
|
{
|
|
switch (state.JumpState)
|
|
{
|
|
case 1:
|
|
state = state with { JumpState = 2 };
|
|
break;
|
|
case 3:
|
|
state = state with { JumpState = 4,
|
|
EffectRemaining = input.EffectAvailable ? 0 : state.EffectRemaining };
|
|
return Result(pose: JetpackPose.Fall, stop: true, mode: 1);
|
|
case 4:
|
|
// Table4387C4 entry3 jumps directly43879A. Only state5's
|
|
// entry checks grounded at43878C (outer reset above too).
|
|
state = state with { JumpState = 0 };
|
|
break;
|
|
}
|
|
return Result();
|
|
}
|
|
|
|
switch (state.JumpState)
|
|
{
|
|
case 0:
|
|
if (!input.Grounded) return Result(pose: JetpackPose.Fall, stop: true);
|
|
state = state with { JumpState = 1, HoldSeconds = 0,
|
|
EffectRemaining = input.EffectAvailable ? 0 : state.EffectRemaining };
|
|
return Result(impulse: JumpImpulse, pose: JetpackPose.Jump);
|
|
case 1:
|
|
//438407 storesfloat but keeps the unrounded sum on x87 for
|
|
//43840D's comparison. Different sums can store the samefloat.
|
|
double hold=(double)state.HoldSeconds+input.DeltaSeconds;
|
|
state = state with { HoldSeconds = (float)hold };
|
|
if (hold <= HoldThreshold) return Result();
|
|
break;
|
|
case 2:
|
|
break;
|
|
case 3:
|
|
if (!input.Grounded) return Result();
|
|
state = state with { JumpState = 4,
|
|
EffectRemaining = input.EffectAvailable ? 0 : state.EffectRemaining };
|
|
return Result(pose: JetpackPose.Fall, radius: LandingRadius, stop: true, mode: 1);
|
|
case 5:
|
|
if (input.Grounded) state = state with { JumpState = 0 };
|
|
return Result();
|
|
default:
|
|
return Result();
|
|
}
|
|
|
|
// Both the >.4-second and released-then-held branches enter state3
|
|
// even when no gadget is available. The next expired-effect guard
|
|
// then returns to state0, preserving the ballistic acceleration.
|
|
state = state with { JumpState = 3 };
|
|
if (!CanStart(input.LevelCode, input.GadgetMode, input.InventoryState, input.Energy)) return Result();
|
|
if (input.EffectAvailable) state = state with { EffectRemaining = Duration };
|
|
return Result(velocity: BurstInitialVelocityY, pose: JetpackPose.Fly,
|
|
radius: input.EffectAvailable ? BurstRadius : null, start: true);
|
|
}
|
|
|
|
public static bool CanStart(string levelCode, int gadgetMode, int inventoryState, float energy)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(levelCode);
|
|
RequireFinite(energy);
|
|
if (levelCode == "VR04") return true;
|
|
return levelCode is not ("LEV6" or "LV4B" or "LV6A" or "LEV3" or "LV3A" or "LV3B" or "LV3C" or "LV3D")
|
|
&& gadgetMode is not (2 or 6 or 7) && energy > 0 && inventoryState is not (0 or 4);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effect421FB0 resets grounded remaining time to0, subtracts dt (including
|
|
/// when inactive), then drains only if the stored result is positive.
|
|
/// Owner/effect absence leaves timer and energy unchanged. Energy reaching
|
|
/// zero does not cancel an already running burst. Visibility is this phase's
|
|
/// request; the controller phase does not itself show/hide the effect.
|
|
/// </summary>
|
|
public static JetpackEffectResult StepEffect(JetpackBurstState state, bool grounded,
|
|
float deltaSeconds, float energy, string levelCode,
|
|
bool ownerAvailable = true, bool effectAvailable = true)
|
|
{
|
|
ValidateState(state);
|
|
ValidateSeconds(deltaSeconds);
|
|
RequireFinite(energy);
|
|
ArgumentNullException.ThrowIfNull(levelCode);
|
|
if (!ownerAvailable || !effectAvailable) return new(state, energy, false);
|
|
var remaining = (grounded ? 0 : state.EffectRemaining) - deltaSeconds;
|
|
state = state with { EffectRemaining = remaining };
|
|
if (remaining <= 0) return new(state, energy, false);
|
|
return new(state, AdjustEnergy(energy, -deltaSeconds * EnergyPerSecond, levelCode), true);
|
|
}
|
|
|
|
/// <summary>EXE42A920 numeric VR01..VR08 immunity and [0,100] clamp. SFX are external.</summary>
|
|
public static float AdjustEnergy(float energy, float delta, string levelCode)
|
|
{
|
|
RequireFinite(energy);
|
|
RequireFinite(delta);
|
|
ArgumentNullException.ThrowIfNull(levelCode);
|
|
// The source compares a big-endian FourCC numerically, not by prefix.
|
|
if (levelCode.Length == 4)
|
|
{
|
|
var code = ((uint)levelCode[0] << 24) | ((uint)levelCode[1] << 16)
|
|
| ((uint)levelCode[2] << 8) | levelCode[3];
|
|
if (code is >= 0x56523031 and <= 0x56523038) return energy;
|
|
}
|
|
return Math.Clamp(energy + delta, 0, 100);
|
|
}
|
|
|
|
private static void ValidateState(JetpackBurstState state)
|
|
{
|
|
if (state.JumpState is < 0 or > 5) throw new ArgumentOutOfRangeException(nameof(state));
|
|
RequireFinite(state.HoldSeconds);
|
|
RequireFinite(state.EffectRemaining);
|
|
}
|
|
|
|
private static void ValidateSeconds(float seconds)
|
|
{
|
|
if (!float.IsFinite(seconds) || seconds < 0) throw new ArgumentOutOfRangeException(nameof(seconds));
|
|
}
|
|
|
|
private static void RequireFinite(float value)
|
|
{
|
|
if (!float.IsFinite(value)) throw new ArgumentOutOfRangeException(nameof(value));
|
|
}
|
|
}
|