diff --git a/tdkpin-rs/CHANGELOG.md b/tdkpin-rs/CHANGELOG.md index 8683ed0..5ec96c8 100644 --- a/tdkpin-rs/CHANGELOG.md +++ b/tdkpin-rs/CHANGELOG.md @@ -10,6 +10,10 @@ and this project adheres to ### Fixed +- Replace the cooldown/float-meter nudge approximation with the original + Left-Shift, keypad-3, and Space formulas using scalar 15, the shared Borland + random stream, 32-bit velocity wrapping, and the decaying 16-bit `+25` tilt + counter with its randomized `30..39` threshold. - Replace the Xorshift gameplay RNG with the original Borland linear stream and unbiased `Random(n)` high-word mapping. Launcher variation, effect selection, claw terminals, and magnetic-field impulses now share the recovered sequence. diff --git a/tdkpin-rs/README.md b/tdkpin-rs/README.md index e0497db..4372094 100644 --- a/tdkpin-rs/README.md +++ b/tdkpin-rs/README.md @@ -57,7 +57,7 @@ validation runs. | Charge launcher | Hold Down arrow, release to launch | - | | Left flipper | Left Ctrl | `A` or Left arrow | | Right flipper | Keypad Enter | Right Ctrl, `D`, or Right arrow | -| Nudge | Space, either Shift, keypad `3` | - | +| Nudge | Space, Left Shift, keypad `3` | Right Shift aliases keypad `3` | | Help | F1 | Enter/Escape returns | | Settings | F2 | - | | High scores | F3 | - | diff --git a/tdkpin-rs/RECONSTRUCTION.md b/tdkpin-rs/RECONSTRUCTION.md index b97a685..a3a683a 100644 --- a/tdkpin-rs/RECONSTRUCTION.md +++ b/tdkpin-rs/RECONSTRUCTION.md @@ -23,7 +23,7 @@ implementation. | Audio | Exact samples and recovered dispatch | All 16 mono PCM WAV resources are embedded unchanged. Playback is emitted at the reconstructed call sites, including multi-sound bank completions and nudge-then-tilt ordering. WAV 2022 is loaded by the original generic resource loop but has no playback call and is therefore never played by Rust. | | Help and languages | Exact | Original resource images 1001-1005 are displayed directly. | | 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. The upward edge transfer matches three live probes and the downstroke matches its live position-preserving `(3519,6302)` transfer; both are mirrored on the right. Object 174 is omitted because the original overwrites it with the live ball every frame. | -| Ball launcher | Recovered | The initial 32-bit fixed-point coordinates decode to `(325, 413)` in the right shooter lane. The port reproduces the initial `-375` millipixel Down event, 650 ms repeat delay, 40 ms repeats, release impulse, and randomized clamp below the original `-3800` maximum. This replaces the former guessed 330-430 px/s shot. | +| Ball launcher and nudge input | Recovered | The initial 32-bit fixed-point coordinates decode to `(325, 413)` in the right shooter lane. The port reproduces the initial `-375` millipixel Down event, 650 ms repeat delay, 40 ms repeats, release impulse, and randomized clamp below the original `-3800` maximum. 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 gameplay behavior | Production movement uses the original 10 ms millipixel substep, `+15` vertical acceleration, `3800` speed bound, point-path type-2 intersection, one-sided line response, swept type-1 circle response, and swept non-physical sensor contacts. It evaluates all records and applies the earliest contact along the substep. The original Borland seed update and high-word `Random(n)` mapping drive launcher variation, effects, claw terminals, and the recovered randomized magnetic-field impulse. Live probes cover ordinary rails, ordinary circles, a kicked bumper, lock holes, magnetic fields, all claw exits, and both flipper directions. Rust represents the original per-ball contact words as path-entry/inside latches; this is a source-structure difference rather than a missing collision route. | | 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. 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. | | 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. Score mutation uses the original 32-bit wrapping behavior, and each add operation can advance at most one media threshold. | diff --git a/tdkpin-rs/src/app.rs b/tdkpin-rs/src/app.rs index ec767a0..71f4c3f 100644 --- a/tdkpin-rs/src/app.rs +++ b/tdkpin-rs/src/app.rs @@ -1,6 +1,6 @@ use crate::{ assets::Assets, - game::{Controls, Event, Game}, + game::{Controls, Event, Game, Nudge}, persistence::{HighScore, Language, Persistence, SavedData, insert_high_score}, table::BUMPERS, }; @@ -174,21 +174,13 @@ impl App { || is_key_down(KeyCode::D) || is_key_down(KeyCode::Right); let nudge = if is_key_pressed(KeyCode::LeftShift) { - -1.0 + Nudge::Left } else if is_key_pressed(KeyCode::RightShift) || is_key_pressed(KeyCode::Kp3) { - 1.0 + Nudge::Right } else if is_key_pressed(KeyCode::Space) { - if self - .game - .as_ref() - .is_some_and(|game| game.ball.position.x < WIDTH / 4.0) - { - 1.0 - } else { - -1.0 - } + Nudge::Center } else { - 0.0 + Nudge::None }; let controls = Controls { left_flipper, diff --git a/tdkpin-rs/src/game.rs b/tdkpin-rs/src/game.rs index 955cc40..8fe942c 100644 --- a/tdkpin-rs/src/game.rs +++ b/tdkpin-rs/src/game.rs @@ -19,7 +19,6 @@ const LEFT_FLIPPER_RAISED_TIP: Vec2 = Vec2::new(133.0, 377.0); const RIGHT_FLIPPER_PIVOT: Vec2 = Vec2::new(210.0, 397.0); const RIGHT_FLIPPER_REST_TIP: Vec2 = Vec2::new(179.0, 419.0); const RIGHT_FLIPPER_RAISED_TIP: Vec2 = Vec2::new(181.0, 376.0); -const TILT_THRESHOLD: f32 = 1.15; const LAUNCHER_POSITION: Vec2 = Vec2::new(325.0, 413.0); const LAUNCHER_FRAME_THRESHOLDS: [f32; 10] = [0.05, 0.15, 0.25, 0.35, 0.45, 0.55, 0.65, 0.75, 0.85, 0.95]; @@ -41,7 +40,16 @@ pub struct Controls { pub left_flipper: bool, pub right_flipper: bool, pub launch_down: bool, - pub nudge: f32, + pub nudge: Nudge, +} + +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub enum Nudge { + #[default] + None, + Left, + Right, + Center, } #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -229,8 +237,8 @@ pub struct Game { pub last_collision_id: Option, accumulator: f32, bumper_cooldown: f32, - nudge_cooldown: f32, - nudge_meter: f32, + tilt_counter: u16, + tilt_counter_accumulator: f32, launcher_was_down: bool, launcher_hold_seconds: f32, launcher_next_repeat: f32, @@ -270,8 +278,8 @@ impl Game { last_collision_id: None, accumulator: 0.0, bumper_cooldown: 0.0, - nudge_cooldown: 0.0, - nudge_meter: 0.0, + tilt_counter: 0, + tilt_counter_accumulator: 0.0, launcher_was_down: false, launcher_hold_seconds: 0.0, launcher_next_repeat: LAUNCHER_REPEAT_DELAY_SECONDS, @@ -339,6 +347,7 @@ impl Game { } self.last_collision_id = None; 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; @@ -376,24 +385,8 @@ impl Game { events.push(Event::Sound(2002)); } } - if !self.tilted - && !self.ball.in_launcher - && controls.nudge.abs() > 0.1 - && self.nudge_cooldown <= 0.0 - { - self.ball.velocity.x += controls.nudge * 55.0; - self.nudge_meter += controls.nudge.abs() * 0.34; - self.nudge_cooldown = 0.22; - self.nudge_shake = 0.18; - events.push(Event::Sound(2019)); - if self.nudge_meter >= TILT_THRESHOLD { - self.tilted = true; - self.bonus = 0; - events.push(Event::Tilt); - events.push(Event::Sound(2020)); - } else { - events.push(Event::Nudge); - } + if !self.tilted && controls.nudge != Nudge::None { + self.apply_nudge(controls.nudge, &mut events); } self.accumulator = (self.accumulator + frame_time.min(0.05)).min(0.1); @@ -405,19 +398,79 @@ impl Game { events } + fn advance_tilt_counter(&mut self, elapsed: f32) { + self.tilt_counter_accumulator += elapsed; + while self.tilt_counter_accumulator >= self.claw_frame_seconds { + self.tilt_counter_accumulator -= self.claw_frame_seconds; + if self.tilt_counter != 0 { + self.tilt_counter -= 1; + } + } + } + + #[allow(clippy::cast_possible_truncation)] + fn apply_nudge(&mut self, nudge: Nudge, events: &mut Vec) { + const SCALAR: i32 = 15; + events.push(Event::Sound(2019)); + let mut velocity = MilliVec::from_velocity_per_second(self.ball.velocity); + match nudge { + Nudge::Left | Nudge::Right => { + let amount = (50 - i32::from(self.random.below(20))) * SCALAR; + let signed = if nudge == Nudge::Left { -amount } else { amount }; + velocity.x = velocity.x.wrapping_add(signed); + } + Nudge::Center => { + let horizontal = + (f64::from(SCALAR) * (self.random.unit_interval() * 20.0 - 10.0)).round() + as i32; + let vertical = -(60 - i32::from(self.random.below(20))) * SCALAR; + velocity.x = velocity.x.wrapping_add(horizontal); + velocity.y = velocity.y.wrapping_add(vertical); + } + Nudge::None => return, + } + self.ball.velocity = velocity.to_velocity_per_second(); + + if let Some(secondary) = &mut self.secondary_ball { + let mut slot_velocity = MilliVec::from_velocity_per_second(secondary.velocity); + match nudge { + Nudge::Left | Nudge::Right => { + let amount = if nudge == Nudge::Left { -600 } else { 600 }; + slot_velocity.x = slot_velocity.x.wrapping_add(amount); + } + Nudge::Center => { + let horizontal = (i32::from(self.random.below(21)) - 10) * SCALAR; + let vertical = -(i32::from(self.random.below(100)) + 50) * SCALAR; + slot_velocity.x = slot_velocity.x.wrapping_add(horizontal); + slot_velocity.y = slot_velocity.y.wrapping_add(vertical); + } + Nudge::None => {} + } + secondary.velocity = slot_velocity.to_velocity_per_second(); + } + + self.nudge_shake = 0.18; + self.tilt_counter = self.tilt_counter.wrapping_add(25); + let threshold = self.random.below(10) + 30; + if threshold < self.tilt_counter { + self.tilted = true; + self.flippers = Flippers::default(); + self.pending_flipper_edges.fill(0); + events.push(Event::Tilt); + events.push(Event::Sound(2020)); + } else { + events.push(Event::Nudge); + } + } + #[allow(clippy::too_many_lines)] fn fixed_update(&mut self, dt: f32, events: &mut Vec) { self.bumper_cooldown = (self.bumper_cooldown - dt).max(0.0); - self.nudge_cooldown = (self.nudge_cooldown - dt).max(0.0); self.wheel_animation = (self.wheel_animation - dt).max(0.0); self.nudge_shake = (self.nudge_shake - dt).max(0.0); for flash in &mut self.bumper_flash { *flash = (*flash - dt).max(0.0); } - if !self.tilted { - self.nudge_meter = (self.nudge_meter - dt * 0.34).max(0.0); - } - self.update_claw(dt, events); if self.claw.ball_suspended { self.pending_flipper_edges.fill(0); @@ -1109,7 +1162,8 @@ impl Game { self.top_targets.fill(false); self.wheel_holes.fill(false); self.tilted = false; - self.nudge_meter = 0.0; + self.tilt_counter = 0; + self.tilt_counter_accumulator = 0.0; self.bumper_flash.fill(0.0); self.wheel_animation = 0.0; self.claw = Claw::default(); @@ -1515,22 +1569,21 @@ mod tests { #[test] fn tilt_latches_and_disables_flippers_and_scoring() { - let mut game = Game::new(1); + let mut game = Game::new_with_seed(1, 7); assert!(launch_ball(&mut game, 1).contains(&Event::Launch)); game.bonus = 4_000; - for _ in 0..4 { - game.nudge_cooldown = 0.0; + for _ in 0..2 { game.update( - 1.0 / 60.0, + 0.0, 3, Controls { - nudge: 1.0, + nudge: Nudge::Right, ..Controls::default() }, ); } assert!(game.tilted); - assert_eq!(game.bonus, 0); + assert_eq!(game.bonus, 4_000); game.flippers.left_raised = true; game.flippers.right_raised = true; @@ -1713,10 +1766,10 @@ mod tests { #[test] fn nudge_and_tilt_emit_the_original_ordered_sound_sequence() { - let mut game = Game::new(1); + let mut game = Game::new_with_seed(1, 7); game.ball.in_launcher = false; let controls = Controls { - nudge: 1.0, + nudge: Nudge::Right, ..Controls::default() }; @@ -1724,15 +1777,43 @@ mod tests { game.update(0.0, 3, controls), [Event::Sound(2019), Event::Nudge] ); + assert_eq!( + MilliVec::from_velocity_per_second(game.ball.velocity), + MilliVec { x: 690, y: 0 } + ); + assert_eq!(game.tilt_counter, 25); - game.nudge_cooldown = 0.0; - game.nudge_meter = TILT_THRESHOLD - 0.01; + game.tilt_counter = 15; assert_eq!( game.update(0.0, 3, controls), [Event::Sound(2019), Event::Tilt, Event::Sound(2020)] ); } + #[test] + fn center_nudge_and_tilt_decay_match_the_detail_timer() { + let mut game = Game::new_with_seed(1, 7); + let events = game.update( + 0.0, + 3, + Controls { + nudge: Nudge::Center, + ..Controls::default() + }, + ); + assert_eq!(events, [Event::Sound(2019), Event::Nudge]); + assert_eq!( + MilliVec::from_velocity_per_second(game.ball.velocity), + MilliVec { x: -84, y: -660 } + ); + assert_eq!(game.tilt_counter, 25); + + game.update(0.029, 3, Controls::default()); + assert_eq!(game.tilt_counter, 25); + game.update(0.001_1, 3, Controls::default()); + assert_eq!(game.tilt_counter, 24); + } + #[test] fn claw_sprite_rects_follow_the_recovered_four_row_sheet() { let source = |frame, bank| { @@ -1997,24 +2078,22 @@ mod tests { } #[test] - fn waiting_ball_cannot_be_tilted() { - let mut game = Game::new(1); + fn waiting_ball_uses_the_same_nudge_tilt_counter() { + let mut game = Game::new_with_seed(1, 7); - for _ in 0..12 { - game.nudge_cooldown = 0.0; + for attempt in 0..2 { let events = game.update( - 1.0 / 60.0, + 0.0, 3, Controls { - nudge: 1.0, + nudge: Nudge::Right, ..Controls::default() }, ); - assert!(!events.contains(&Event::Nudge)); - assert!(!events.contains(&Event::Tilt)); + assert_eq!(events.contains(&Event::Tilt), attempt == 1); } - assert!(!game.tilted); - assert!(launch_ball(&mut game, 1).contains(&Event::Launch)); + assert!(game.tilted); + assert!(game.ball.in_launcher); } }