diff --git a/tdkpin-rs/CHANGELOG.md b/tdkpin-rs/CHANGELOG.md index e3900d6..1caf819 100644 --- a/tdkpin-rs/CHANGELOG.md +++ b/tdkpin-rs/CHANGELOG.md @@ -10,6 +10,11 @@ and this project adheres to ### Fixed +- Batch physics and flipper publication on the original 50/40/30/20/10-ms + detail callbacks with exactly 5/4/3/2/1 internal 10-ms substeps, instead of + exposing every substep on host render frames. +- Separate flipper key flags from rendered/collision position so press/release + edges and WAVE 2021 occur only when the detail timer processes them. - Add a bit-exact Borland Real48 core and route speed clamps, type-1 circles, type-2 segments, moving flippers, capture pulls, trigger randomization, and magnetic impulses through its original rounding behavior. diff --git a/tdkpin-rs/README.md b/tdkpin-rs/README.md index 4372094..4b25640 100644 --- a/tdkpin-rs/README.md +++ b/tdkpin-rs/README.md @@ -64,9 +64,9 @@ validation runs. | Sound | F12 | - | The five original speed choices remain available in settings. Physics now uses -the original invariant 100 Hz millipixel substep, and claw animation follows the -recovered 50/40/30/20/10 ms timer choices. Setting-specific repaint batching -remains a presentation-only reconstruction boundary. +the original invariant 100 Hz millipixel substep, batched as 5/4/3/2/1 steps on +the recovered 50/40/30/20/10 ms callbacks. Claw animation, flipper edges, and +visible state publication follow the same selected callback cadence. The help screen is the original artwork in English, German, French, Italian, or Spanish. As instructed on that screen, double-clicking its upper-left exit diff --git a/tdkpin-rs/RECONSTRUCTION.md b/tdkpin-rs/RECONSTRUCTION.md index 6c1dfeb..3244403 100644 --- a/tdkpin-rs/RECONSTRUCTION.md +++ b/tdkpin-rs/RECONSTRUCTION.md @@ -25,7 +25,7 @@ implementation. | Playfield collision layout | Recovered | All 109 active type-2 line objects and 40 static active type-1 circles are transcribed from the original 175-object registration table. The registration routine converts its sideways inputs with `screen = (y, x - 20)` and accumulates explicitly relative objects. Type-2 records retain every recovered Real48 normal/tangent response pair and registered one-sided orientation. Type-1 records retain their swept-circle radius, radial rebound, tangent coupling, and bumper kick. Each flipper uses its exact two line records plus moving tip circle in both positions. Moving-flipper contact ports `1000:7ed9` rather than fitting live samples: delta-specific pivots/edges, integer cross gates, radial/penetration calculations, response-record gain, and position/velocity publication are tested against all four C harness directions and the raised release geometry. Object 174 is overwritten with the live first ball and Rust handles its ball-to-ball role directly. | | Ball launcher and nudge input | Recovered | The initial 32-bit fixed-point coordinates decode to `(325, 413)` in the right shooter lane. Each Down keydown subtracts `15*50 = 750` millipixels, release subtracts another `15*100 = 1500`, and the result follows the recovered randomized `-3800` lower and `-2280` weak upper clamp branches. The ten decoration frames use the same strict 750-millipixel thresholds. Left Shift and keypad 3 apply their directional `(50-Random(20))*15` impulses; Space uses the recovered Real48 horizontal factor and `(60-Random(20))*15` vertical impulse. Each nudge adds 25 to the wrapping 16-bit tilt counter, compares it with `30+Random(10)`, and the detail timer decrements a nonzero counter once per callback. | | Physics arithmetic | Recovered Real48 core | Production movement uses the original 10 ms millipixel substep, `+15` vertical acceleration, Real48 `3800/speed` clamp, type-2 distance/cross gates, type-1 midpoint normal, surface-distance candidate ordering, persistent Real48 spin, and the common impulse response. Dynamic records 174/175 transfer normal impulse to the other ball before applying `normal_velocity-1000` to the moving ball. The original Borland seed update, high-word `Random(n)`, and normalized Real48 random register result drive launcher variation, effects, claw terminals, magnetic fields, and trigger response. Type-4 triggers retain a separate transient entry flag; type-3 captures retain per-player 16-bit contact words, deep-inside pull/hold progression to age 300, first-contact sound, and `99`/`2` completion sentinels. Zero-spin C harnesses and retained-spin Wine probes are tested separately rather than conflated. | -| Rules | Recovered gameplay paths | Player count, controls, the five three-line bumper-value groups, four three-line TDK-diamond groups, five doubling-value lock holes, wheel-reset target, seven-way effect selector/consumer including multiball, permanent double scoring, and four exact media/extra-ball thresholds follow original help/code paths, globals, and object flags. The ninth diamond pays the original 24,464 completion value; the following completed bank enables double scoring, and later completions add 100,000 to the per-player secondary score. Turn changes mirror the original save/load of all 175 collision record states: wheel/top targets, active/contact slots, selected effect, and multiball readiness remain attached to their player. Claw contact and all initially active type-4 targets use recovered records. The top three targets score 500 each and independently enable the left, center, or right magnetic field record; each field pulls the ball upward until it exits and then deactivates. The claw state machine and release table have live differential coverage for all four random terminals. Remaining timing uncertainty is presentation batching at non-default detail settings, not gameplay routing. | +| Rules | Recovered gameplay paths | Player count, controls, the five three-line bumper-value groups, four three-line TDK-diamond groups, five doubling-value lock holes, wheel-reset target, seven-way effect selector/consumer including multiball, permanent double scoring, and four exact media/extra-ball thresholds follow original help/code paths, globals, and object flags. The ninth diamond pays the original 24,464 completion value; the following completed bank enables double scoring, and later completions add 100,000 to the per-player secondary score. Turn changes mirror the original save/load of all 175 collision record states: wheel/top targets, active/contact slots, selected effect, and multiball readiness remain attached to their player. Claw contact and all initially active type-4 targets use recovered records. The top three targets score 500 each and independently enable the left, center, or right magnetic field record; each field pulls the ball upward until it exits and then deactivates. The claw state machine and release table have live differential coverage for all four random terminals. | | Numeric scoring | Recovered gameplay values | Static scores come from the initialized 175-object ledger. Dynamic bumper progression, target-bank completion, diamond awards, 10k-160k lock bonuses, 310k transfer, six effect values, multiball mode, and all four media thresholds are transcribed from `1000:b476`, `1000:c4e1`, `1000:bc36`, and live state probes. Lock and effect awards share the original per-player secondary score and display multiplier; the fifth hole transfers and clears it, increments the multiplier, and grants the recovered ball award. Score mutation uses the original 32-bit wrapping behavior, and each add operation can advance at most one media threshold. | | High scores | Recovered visible flow; portable storage | The original 276-byte table is decoded as ten `IWIK`-XOR-obfuscated little-endian scores plus ten 22-byte names. Each player is checked immediately when their own last ball is lost; qualifying scores use the original signed-high/unsigned-low comparison and a `TDK Pinball Player`-prefilled name screen before the table is shown and play resumes. Persisted updates use portable JSON rather than rewriting the Win16 file. | | Configuration | Behaviorally compatible | Sound, language, and five detail levels are retained. Storage moves from a local Win16 INI file to the platform user-data directory. | @@ -71,9 +71,10 @@ decoded, build-ready subset; it does not replace that evidence archive. operates both flippers from ball position. The two-minute acceptance run covers repeated launches, both flippers, targets, bumpers, lock holes, a claw capture/release pair, and drains while checking every state for finite values. -- Semantic boundary: collision, random, spin, and fixed-point state now use the - reconstructed Borland arithmetic and rounding model. Win16 GDI execution and - non-default repaint batching remain platform-level presentation differences. +- Semantic boundary: collision, random, spin, fixed-point state, detail callback + batching, and flipper publication now use the reconstructed Borland/Win16 + timing model. Native texture drawing replaces Win16 GDI calls while preserving + the logical 640x460 framebuffer and original resource pixels. ## Architecture diff --git a/tdkpin-rs/src/game.rs b/tdkpin-rs/src/game.rs index c50c41f..413f56f 100644 --- a/tdkpin-rs/src/game.rs +++ b/tdkpin-rs/src/game.rs @@ -29,6 +29,7 @@ const CLAW_TRIGGER_RADIUS: f32 = 19.0; // deterministic on modern machines. const CLAW_FRAME_SECONDS: f32 = 0.030; const DETAIL_TIMER_SECONDS: [f32; 5] = [0.050, 0.040, 0.030, 0.020, 0.010]; +const DETAIL_SUBSTEPS: [u8; 5] = [5, 4, 3, 2, 1]; const CLAW_TERMINAL_FRAMES: [u8; 4] = [1, 6, 7, 18]; const ORIGINAL_BALL_SPEED_PER_SECOND: f32 = 380.0; @@ -262,6 +263,7 @@ pub struct Game { pub top_targets: [bool; 3], pub tilted: bool, pub flippers: Flippers, + flipper_inputs: Flippers, pub claw: Claw, pub bumper_flash: [f32; 3], pub wheel_animation: f32, @@ -304,6 +306,7 @@ impl Game { top_targets: [false; 3], tilted: false, flippers: Flippers::default(), + flipper_inputs: Flippers::default(), claw: Claw::default(), bumper_flash: [0.0; 3], wheel_animation: 0.0, @@ -404,19 +407,8 @@ impl Game { self.claw_frame_seconds = DETAIL_TIMER_SECONDS[usize::from(detail.clamp(1, 5) - 1)]; self.advance_tilt_counter(frame_time.min(0.05)); let mut events = Vec::new(); - let old_flippers = self.flippers; - self.flippers.left_raised = controls.left_flipper && !self.tilted; - self.flippers.right_raised = controls.right_flipper && !self.tilted; - if self.flippers.left_raised != old_flippers.left_raised { - events.push(Event::FlipperMove); - events.push(Event::Sound(2021)); - self.pending_flipper_edges[0] = if self.flippers.left_raised { -1 } else { 1 }; - } - if self.flippers.right_raised != old_flippers.right_raised { - events.push(Event::FlipperMove); - events.push(Event::Sound(2021)); - self.pending_flipper_edges[1] = if self.flippers.right_raised { -1 } else { 1 }; - } + self.flipper_inputs.left_raised = controls.left_flipper && !self.tilted; + self.flipper_inputs.right_raised = controls.right_flipper && !self.tilted; if self.ball.in_launcher && !self.tilted { if controls.launch_down { @@ -449,14 +441,53 @@ impl Game { } self.accumulator = (self.accumulator + frame_time.min(0.05)).min(0.1); - while self.accumulator >= STEP_SECONDS { - self.fixed_update(STEP_SECONDS, &mut events); - self.advance_secondary_ball(&mut events); - self.accumulator -= STEP_SECONDS; + let detail_index = usize::from(detail.clamp(1, 5) - 1); + let timer_interval = DETAIL_TIMER_SECONDS[detail_index]; + while self.accumulator >= timer_interval { + self.timer_tick(timer_interval, DETAIL_SUBSTEPS[detail_index], &mut events); + self.accumulator -= timer_interval; } events } + fn timer_tick(&mut self, elapsed: f32, substeps: u8, events: &mut Vec) { + self.update_claw(elapsed, events); + if !self.claw.ball_suspended { + for _ in 0..substeps { + self.fixed_update(STEP_SECONDS, events); + self.advance_secondary_ball(events); + if self.finished || self.claw.ball_suspended { + break; + } + } + } + + self.pending_flipper_edges[0] = match ( + self.flipper_inputs.left_raised, + self.flippers.left_raised, + ) { + (true, false) => -1, + (false, true) => 1, + _ => 0, + }; + self.pending_flipper_edges[1] = match ( + self.flipper_inputs.right_raised, + self.flippers.right_raised, + ) { + (true, false) => -1, + (false, true) => 1, + _ => 0, + }; + self.flippers = self.flipper_inputs; + for edge in self.pending_flipper_edges { + if edge != 0 { + events.push(Event::FlipperMove); + events.push(Event::Sound(2021)); + } + } + self.apply_flipper_kicks(); + } + fn advance_tilt_counter(&mut self, elapsed: f32) { self.tilt_counter_accumulator += elapsed; while self.tilt_counter_accumulator >= self.claw_frame_seconds { @@ -518,7 +549,7 @@ impl Game { let threshold = self.random.below(10) + 30; if threshold < self.tilt_counter { self.tilted = true; - self.flippers = Flippers::default(); + self.flipper_inputs = Flippers::default(); self.pending_flipper_edges.fill(0); events.push(Event::Tilt); events.push(Event::Sound(2020)); @@ -535,12 +566,6 @@ impl Game { for flash in &mut self.bumper_flash { *flash = (*flash - dt).max(0.0); } - self.update_claw(dt, events); - if self.claw.ball_suspended { - self.pending_flipper_edges.fill(0); - return; - } - if self.ball.in_launcher { self.ball.position = LAUNCHER_POSITION; self.ball.velocity = Vec2::ZERO; @@ -637,8 +662,6 @@ impl Game { return; } - self.apply_flipper_kicks(); - if self.ball.position.y > 470.0 { // Only malformed/out-of-table states reach this guard; the real // drain is collision object 2 at y=455. @@ -1332,6 +1355,9 @@ impl Game { self.bumper_flash.fill(0.0); self.wheel_animation = 0.0; self.claw = Claw::default(); + self.flipper_inputs = Flippers::default(); + self.flippers = Flippers::default(); + self.pending_flipper_edges.fill(0); self.capture_age = 0; self.secondary_ball = None; self.nudge_shake = 0.0; @@ -1497,6 +1523,33 @@ mod tests { assert!(game.ball.in_launcher); } + #[test] + fn detail_timer_batches_the_original_number_of_substeps() { + for (detail, interval, substeps) in [ + (1, 0.050, 5_i32), + (2, 0.040, 4), + (3, 0.030, 3), + (4, 0.020, 2), + (5, 0.010, 1), + ] { + let mut game = Game::new(1); + game.ball.in_launcher = false; + game.ball.position = vec2(200.0, 250.0); + game.ball.velocity = Vec2::ZERO; + game.object_active.fill(false); + + game.update(interval - 0.001, detail, Controls::default()); + assert_eq!(game.ball.position, vec2(200.0, 250.0)); + assert_eq!(game.ball.velocity, Vec2::ZERO); + + game.update(0.001_1, detail, Controls::default()); + let velocity = MilliVec::from_velocity_per_second(game.ball.velocity); + let position = MilliVec::from_position(game.ball.position); + assert_eq!(velocity.y, 15 * substeps); + assert_eq!(position.y, 250_000 + 15 * substeps * (substeps + 1) / 2); + } + } + #[test] fn launcher_charges_while_held_and_fires_on_release() { let mut game = Game::new(1); @@ -1890,7 +1943,7 @@ mod tests { ); let events = game.update( - 1.0 / 60.0, + 0.030, 3, Controls { left_flipper: true, @@ -1944,7 +1997,8 @@ mod tests { ..Controls::default() }; - let press_events = game.update(1.0 / 60.0, 3, raised); + assert!(game.update(0.029, 3, raised).is_empty()); + let press_events = game.update(0.001_1, 3, raised); assert_eq!( press_events, [ @@ -1955,9 +2009,9 @@ mod tests { ], "each original move_flipper call starts WAVE 2021" ); - assert!(game.update(1.0 / 60.0, 3, raised).is_empty()); + assert!(game.update(0.030, 3, raised).is_empty()); - let release_events = game.update(1.0 / 60.0, 3, Controls::default()); + let release_events = game.update(0.030, 3, Controls::default()); assert_eq!( release_events, [ @@ -1998,15 +2052,13 @@ mod tests { fn returning_flipper_uses_the_raised_record_geometry() { let mut game = Game::new(1); game.flippers.left_raised = true; - - let events = game.update(0.0, 3, Controls::default()); - - assert_eq!(events, [Event::FlipperMove, Event::Sound(2021)]); - assert_eq!(game.pending_flipper_edges, [1, 0]); game.ball.in_launcher = false; game.ball.position = vec2(110.0, 410.0); game.ball.velocity = vec2(100.0, 200.0); - game.apply_flipper_kicks(); + let mut events = Vec::new(); + game.timer_tick(0.030, 0, &mut events); + + assert_eq!(events, [Event::FlipperMove, Event::Sound(2021)]); let raw_velocity = MilliVec::from_velocity_per_second(game.ball.velocity); assert_eq!(raw_velocity, MilliVec { x: 765, y: 3_997 }); assert_eq!(game.ball.position, vec2(110.380, 411.984)); @@ -2130,7 +2182,7 @@ mod tests { assert!(game.claw.ball_suspended); assert_eq!(game.ball.velocity, Vec2::ZERO); - game.fixed_update(CLAW_FRAME_SECONDS / 3.0, &mut events); + game.timer_tick(CLAW_FRAME_SECONDS / 3.0, 0, &mut events); assert_eq!(game.ball.position, held_position); assert_eq!(game.claw.frame, 10); @@ -2397,7 +2449,7 @@ mod tests { game.ball.position = vec2(157.0, 454.0); game.ball.velocity = vec2(0.0, 200.0); - game.update(1.0 / 60.0, 3, Controls::default()); + game.update(0.030, 3, Controls::default()); assert!(!game.tilted); assert!(game.ball.in_launcher); diff --git a/tdkpin-rs/src/simulation.rs b/tdkpin-rs/src/simulation.rs index d8fdcfb..3eeb391 100644 --- a/tdkpin-rs/src/simulation.rs +++ b/tdkpin-rs/src/simulation.rs @@ -416,10 +416,19 @@ mod tests { #[test] fn flipper_scenario_records_both_movement_edges() { let mut simulation = Simulation::new(Scenario::Flippers, 1); - simulation.advance_to(46); + simulation.advance_to(50); - assert_eq!(simulation.trace[16].events, ["FlipperMove", "Sound(2021)"]); - assert_eq!(simulation.trace[46].events, ["FlipperMove", "Sound(2021)"]); + let edge_steps = simulation + .trace + .iter() + .filter(|snapshot| snapshot.events.iter().any(|event| event == "FlipperMove")) + .collect::>(); + assert_eq!(edge_steps.len(), 2); + assert!( + edge_steps + .iter() + .all(|snapshot| snapshot.events == ["FlipperMove", "Sound(2021)"]) + ); assert!(!simulation.game.flippers.left_raised); }