using System.Numerics; using GamePort.Assets; namespace GamePort.Game; public readonly record struct PlayerInput(bool Forward, bool Backward, bool Left, bool Right, bool Jump); public enum PlayerPose { Idle, Run, Backpedal, Jump, Fall, Fly } /// /// No-gadget walking checkpoint. Recovered input/arithmetic is described in /// movement-checkpoint.md. Capsule, floor-derived pivot and 60Hz are port policies; /// original collision, intro, object interactions and animation timing remain open. /// public sealed class LabSimulation { public const float BodyRadius = 24; // Provisional, not a recovered collider dimension. private readonly SceneCollision collision; private readonly Vector3 sourceStart; private readonly float startYaw, modelMinimumY, rootHeight, startFloor; private float targetYaw, extraAirAcceleration, shadowDistance; private JetpackBurstState jumpState; private bool forceFall; public Func? JetpackInventoryState { get; set; } public string LevelCode { get; set; } = "LV1B"; public int GadgetMode { get; set; } = -1; public float Energy { get; set; } = 20; public float InteractionRadius { get; private set; } = 60; public bool JetpackEffectVisible { get; private set; } public float JetpackRemaining => jumpState.EffectRemaining; public int Bursts { get; private set; } public float BodyHeight { get; } public Vector3 Position { get; private set; } // Feet, not original actor root. public Vector3 Velocity { get; private set; } public float Yaw { get; private set; } public float Speed { get; private set; } public bool Grounded { get; private set; } public PlayerPose Pose => Grounded ? Speed < -1 ? PlayerPose.Backpedal : Speed > 1 ? PlayerPose.Run : PlayerPose.Idle : jumpState.JumpState == 3 && jumpState.EffectRemaining > 0 ? PlayerPose.Fly : !forceFall && Velocity.Y > 0 ? PlayerPose.Jump : PlayerPose.Fall; public int Steps { get; private set; } public int Jumps { get; private set; } public int Landings { get; private set; } public int WallHits { get; private set; } public float Travel { get; private set; } public float MaximumRise { get; private set; } public Func? DynamicRootResolver { get; set; } public Vector3 ActorRoot => Position + Vector3.UnitY * rootHeight; public Vector3 CameraEye { get; private set; } public Vector3 CameraTarget => ActorRoot + new Vector3(0, 99, 0); public Matrix4x4 Model => Matrix4x4.CreateRotationY(-Yaw * MathF.PI / 180) * Matrix4x4.CreateTranslation(Position - new Vector3(0, modelMinimumY, 0)); public LabSimulation(SceneCollision collision, Mesh standingPose, Vector3 start, float yaw) { this.collision = collision; sourceStart = start; startYaw = yaw; var bounds = standingPose.Bounds(); modelMinimumY = bounds.Min.Y; BodyHeight = bounds.Max.Y - bounds.Min.Y; var floor = collision.GroundHeight(start, 2500) ?? throw new InvalidDataException("No support below GAM startpoint"); startFloor = floor; // Normal Jimmy PostLoad4236A3..BA and support47BAE1 place the actor // root at floor+60*1.265. Serialized spawnY is settled by physics. // Feet collision and sole-based visual placement remain provisional. rootHeight = OriginalBodySupport.JimmyRadius * OriginalBodySupport.JimmyFactor; Reset(); } public void Reset() { var floor = collision.GroundHeight(sourceStart, 2500)!.Value; Position = new(sourceStart.X, floor + SceneCollision.Skin, sourceStart.Z); Velocity = Vector3.Zero; Yaw = targetYaw = startYaw; Speed = extraAirAcceleration = shadowDistance = 0; jumpState = default; forceFall = JetpackEffectVisible = false; InteractionRadius = 60; Bursts = 0; Grounded = true; Steps = Jumps = Landings = WallHits = 0; Travel = MaximumRise = 0; CameraEye = DesiredEye(); } // EXE 0x47F940 truncates, then degree lookup masks to the 16384-entry table. // Table generated with modern trig; x87 bit-identical table recovery is pending. public static (float Sin, float Cos) Trig(float degrees) { var index = (int)(degrees * 45.5111122f) & 0x3fff; var radians = index * (Math.Tau / 16384); return ((float)Math.Sin(radians), (float)Math.Cos(radians)); } public void Step(PlayerInput input) { const float dt = FixedStepClock.Step; var wasGrounded = Grounded; float lateral = 0; if (Grounded) { InteractionRadius = 60; // Jimmy's ordinary grounded update42469B. float yawDelta = 0; if (input.Left) { yawDelta += 20; lateral = -1000; Speed = RecoveredMotion.AddSpeed(Speed); } else if (input.Right) { yawDelta -= 20; lateral = 1000; Speed = RecoveredMotion.AddSpeed(Speed); } if (input.Forward) { yawDelta *= 0.5f; Speed = RecoveredMotion.AddSpeed(Speed); } if (yawDelta != 0) targetYaw = Yaw + yawDelta; if (input.Backward) Speed = MathF.Max(Speed - 52.5f, -300); else if (!input.Forward) Speed = RecoveredMotion.ReleaseSpeed(Speed); } var yawError = MathF.IEEERemainder(targetYaw - Yaw, 360); Yaw = (Yaw + yawError * dt * 3.7f) % 360; var (sin, cos) = Trig(Yaw); var velocity = new Vector3(-sin * Speed, Velocity.Y, cos * Speed); if (Grounded) velocity += new Vector3(cos, 0, sin) * (lateral * dt); if (collision.GroundHeight(ActorRoot, 2500) is { } shadowFloor) shadowDistance = MathF.Abs(ActorRoot.Y - shadowFloor - 13); var air = JetpackBurst.Step(jumpState, new(Grounded, input.Jump, velocity.Y, shadowDistance, JetpackInventoryState?.Invoke() ?? 0, Energy, LevelCode, GadgetMode, dt, extraAirAcceleration)); jumpState = air.State; extraAirAcceleration = air.ExtraAccelerationY; var impulse = new Vector3(0, air.ImpulseY, 0); if (air.ImpulseY != 0) { Grounded = false; Jumps++; } if (air.VelocityYOverride is { } vertical) velocity.Y = vertical; if (air.RadiusOverride is { } radius) InteractionRadius = radius; if (air.Pose == JetpackPose.Fall) forceFall = true; else if (air.Pose is JetpackPose.Jump or JetpackPose.Fly) forceFall = false; if (air.StartGadget) Bursts++; var displacement = RecoveredMotion.Integrate(ref velocity, ref impulse, Vector3.UnitY * extraAirAcceleration, RecoveredMotion.Gravity, dt); var before = Position; var result = collision.Move(Position, displacement, BodyRadius, BodyHeight); Position = result.Position; if (DynamicRootResolver != null) { var root = ActorRoot; var resolved = DynamicRootResolver(before + Vector3.UnitY * rootHeight, root, InteractionRadius); if (resolved != root) { // Original47A5E0 re-clips against static geometry before storing // the new root. Here both kernels remain explicitly provisional. result = collision.Move(Position, resolved - root, BodyRadius, BodyHeight); Position = result.Position; } } Grounded = result.Grounded && velocity.Y <= 0; if (Grounded && velocity.Y < 0 || result.HitCeiling && velocity.Y > 0) velocity.Y = 0; if (Grounded) { extraAirAcceleration = 0; forceFall = false; } Velocity = velocity; // Explicit port phase policy: controller, collision, then effect update. // Original global object iteration order has not yet been matched. var effect = JetpackBurst.StepEffect(jumpState, Grounded, dt, Energy, LevelCode); jumpState = effect.State; Energy = effect.Energy; JetpackEffectVisible = effect.Visible; if (!wasGrounded && Grounded) Landings++; if (result.HitWall) WallHits++; Travel += Vector2.Distance(new(before.X, before.Z), new(Position.X, Position.Z)); MaximumRise = Math.Max(MaximumRise, Position.Y - startFloor); Steps++; var desired = DesiredEye(); var delta = (desired - CameraEye) * new Vector3(1.6f, 1.2f, 1.6f) * dt; CameraEye = collision.ClipCamera(CameraTarget, CameraEye + delta); } private Vector3 DesiredEye() { var (sin, cos) = Trig(Yaw); return collision.ClipCamera(CameraTarget, ActorRoot + new Vector3(sin * 350, 180, -cos * 350)); } public static PlayerInput SmokeInput(int step) => new(step < 120, false, false, step >= 120 && step < 180, step >= 72 && step < 84); public bool SmokePassed => Steps >= 240 && Travel > 50 && MaximumRise > 20 && Jumps > 0 && Landings > 0 && Grounded; public string Report => $"Walk checkpoint: steps={Steps}, travel={Travel:F2}, jumps={Jumps}, landings={Landings}, max rise={MaximumRise:F2}, wall hits={WallHits}, grounded={Grounded}, feet={Position}."; }