diff --git a/original/MECHANICS_PROGRESS.md b/original/MECHANICS_PROGRESS.md index 1b149cc..f2b07d2 100644 --- a/original/MECHANICS_PROGRESS.md +++ b/original/MECHANICS_PROGRESS.md @@ -143,3 +143,10 @@ from the record geometry. Three live left-edge probes at `(115,390)`, `(133.397,360)`, and `(138.182,355)` with raw velocities `(1932,-3523)`, `(2079,-4658)`, and `(2217,-5889)`. The Rust upward stroke fits those states; the full raw calculation and return-stroke transfer remain pending. + +The type-4 rule dispatcher at `1000:b476` shows that records 150-152 are three +independent 500-point sensors. Each sets its own ten-callback timer and enables +the associated magnetic gate; there is no recovered 1,500-per-target plus +5,000 completion rule. Records 140-147 carry their exact 1,000 through 3,000 +scores and use the same enter-once contact bit, which clears after the ball +leaves their radius. diff --git a/tdkpin-rs/RECONSTRUCTION.md b/tdkpin-rs/RECONSTRUCTION.md index fbf04fe..a644e69 100644 --- a/tdkpin-rs/RECONSTRUCTION.md +++ b/tdkpin-rs/RECONSTRUCTION.md @@ -24,8 +24,8 @@ implementation. | 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 left-flipper probes and is mirrored on the right; the complete return-edge arithmetic remains pending. 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. | -| Physics arithmetic | Partly recovered | Production movement uses the original 10 ms millipixel substep, `+15` vertical acceleration, `3800` speed bound, point-path type-2 intersection, one-sided line response, and swept type-1 circle response. Live probes cover ordinary rails, ordinary circles, a kicked bumper, and upward flipper transfer. Type-3/4 triggers, closest-contact selection, persistent contact bookkeeping, and return-edge flipper transfer remain pending. | -| Rules | Partly recovered | Player count, controls, wheel holes, magnetic saves, four-position ball lock, target banks, increasing bumper value, nine-part TDK diamond, permanent double scoring for a completed diamond, KByte media progression, and media extra balls follow the original help and code paths. The claw state machine and release table are readable and terminal 18 has live differential evidence; terminals 1, 6, and 7 still need equivalent live coverage. | +| Physics arithmetic | Partly recovered | 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. Live probes cover ordinary rails, ordinary circles, a kicked bumper, and upward flipper transfer. Closest-contact selection, persistent physical-contact bookkeeping, type-3 lock holes, and return-edge flipper transfer remain pending. | +| Rules | Partly recovered | Player count, controls, increasing bumper value, nine-part TDK diamond, permanent double scoring, KByte media progression, and media extra balls follow original help/code paths. Claw contact and all initially active type-4 targets now use recovered records. The top three targets score 500 each and independently enable a magnetic gate for ten original timer callbacks, replacing the inferred three-target bonus. Wheel holes, ball lock, several target-bank completions, and magnetic save routing still require full rule restoration. The claw state machine and release table are readable and terminal 18 has live differential evidence; terminals 1, 6, and 7 still need equivalent live coverage. | | Numeric scoring | Partly inferred | Visible 2000-6000 target values and recovered registration values are preserved. Some bumper, bank-completion, robot, wheel, lock, and media thresholds are best-evidence reconstructions because the decompiler did not recover meaningful names or a clean rule table. | | High scores | Compatible import | The original 276-byte table is decoded as ten `IWIK`-XOR-obfuscated little-endian scores plus ten 22-byte names, sorted, then migrated to portable JSON. | | 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. | diff --git a/tdkpin-rs/src/game.rs b/tdkpin-rs/src/game.rs index b662848..2bc711c 100644 --- a/tdkpin-rs/src/game.rs +++ b/tdkpin-rs/src/game.rs @@ -2,9 +2,9 @@ use crate::{ geometry::{Segment, closest_point}, original_physics::{ GRAVITY_MILLI_PER_STEP, MAXIMUM_SPEED_MILLI_PER_STEP, MilliVec, STEP_SECONDS, - collide_with_circle, collide_with_line, + collide_with_circle, collide_with_line, path_intersects_circle, }, - table::{BUMPERS, PASSIVE_CIRCLES, WALLS}, + table::{BUMPERS, PASSIVE_CIRCLES, TARGET_SENSORS, WALLS}, }; use macroquad::prelude::{Rect, Vec2, vec2}; @@ -214,6 +214,7 @@ pub struct Game { player_entry: PlayerEntry, claw_rng_state: u32, pending_flipper_kicks: [bool; 2], + trigger_contacts: [bool; 176], } impl Game { @@ -253,6 +254,7 @@ impl Game { player_entry: PlayerEntry::Open, claw_rng_state: seed.max(1), pending_flipper_kicks: [false; 2], + trigger_contacts: [false; 176], } } @@ -401,6 +403,7 @@ impl Game { let mut velocity = MilliVec::from_velocity_per_second(self.ball.velocity); velocity.y += GRAVITY_MILLI_PER_STEP; velocity.clamp_speed(MAXIMUM_SPEED_MILLI_PER_STEP); + let movement_velocity = velocity; let mut position = old_position.add(velocity); let mut hit_wall = None; let mut hit_circle = None; @@ -487,6 +490,11 @@ impl Game { events.push(Event::Bumper); } + self.check_sensor_objects(old_position, movement_velocity, events); + if self.claw.ball_suspended { + return; + } + if !self.tilted { self.check_targets(events); self.check_media(events); @@ -505,35 +513,11 @@ impl Game { } fn check_targets(&mut self, events: &mut Vec) { - if !self.claw.active - && self.ball.position.distance_squared(CLAW_TRIGGER_CENTER) - <= CLAW_TRIGGER_RADIUS.powi(2) - { - let terminal_frame = self.next_claw_terminal_frame(); - self.begin_claw_capture(terminal_frame, events); - return; - } if self.target_cooldown > 0.0 { return; } let position = self.ball.position; - let top = [vec2(205.0, 47.0), vec2(234.0, 50.0), vec2(262.0, 53.0)]; - for (index, center) in top.into_iter().enumerate() { - if position.distance_squared(center) < 11.0_f32.powi(2) && !self.top_targets[index] { - self.top_targets[index] = true; - self.add_score(1_500); - self.ball.velocity.y = self.ball.velocity.y.abs() + 65.0; - self.target_cooldown = 0.12; - events.push(Event::Target); - } - } - if self.top_targets.iter().all(|target| *target) { - self.top_targets.fill(false); - self.magnets = 12.0; - self.add_score(5_000); - } - let side = [180.0, 195.0, 210.0, 225.0, 240.0]; for (index, y) in side.into_iter().enumerate() { if position.distance_squared(vec2(286.0, y)) < 9.0_f32.powi(2) @@ -623,6 +607,53 @@ impl Game { } } + fn check_sensor_objects( + &mut self, + old_position: MilliVec, + movement_velocity: MilliVec, + events: &mut Vec, + ) { + if !self.claw.active + && path_intersects_circle( + old_position, + movement_velocity, + CLAW_TRIGGER_CENTER, + CLAW_TRIGGER_RADIUS, + ) + { + let terminal_frame = self.next_claw_terminal_frame(); + self.begin_claw_capture(terminal_frame, events); + return; + } + + let current_position = MilliVec::from_position(self.ball.position); + for sensor in TARGET_SENSORS { + let touched = path_intersects_circle( + old_position, + movement_velocity, + sensor.center, + sensor.radius, + ); + let contact_index = usize::from(sensor.id); + let entered = touched && !self.trigger_contacts[contact_index]; + self.trigger_contacts[contact_index] = path_intersects_circle( + current_position, + MilliVec::default(), + sensor.center, + sensor.radius, + ); + if !entered || self.tilted { + continue; + } + self.add_score(sensor.score); + events.push(Event::Target); + if (150..=152).contains(&sensor.id) { + self.top_targets[usize::from(sensor.id - 150)] = true; + self.magnets = self.magnets.max(0.3); + } + } + } + fn next_claw_terminal_frame(&mut self) -> u8 { let value = self.next_random_value(); CLAW_TERMINAL_FRAMES[value as usize % CLAW_TERMINAL_FRAMES.len()] @@ -763,6 +794,7 @@ impl Game { self.bumper_flash.fill(0.0); self.wheel_animation = 0.0; self.claw = Claw::default(); + self.trigger_contacts.fill(false); self.nudge_shake = 0.0; self.launcher_charge = 0.0; self.launcher_was_down = false; @@ -1300,17 +1332,63 @@ mod tests { let mut game = Game::new(1); game.ball.in_launcher = false; game.ball.position = CLAW_TRIGGER_CENTER + vec2(CLAW_TRIGGER_RADIUS + 0.1, 0.0); - game.check_targets(&mut Vec::new()); + game.check_sensor_objects( + MilliVec::from_position(game.ball.position), + MilliVec::default(), + &mut Vec::new(), + ); assert!(!game.claw.active); game.ball.position = CLAW_TRIGGER_CENTER; let mut events = Vec::new(); - game.check_targets(&mut events); + game.check_sensor_objects( + MilliVec::from_position(game.ball.position), + MilliVec::default(), + &mut events, + ); assert!(game.claw.active); assert_eq!(events, [Event::ClawCapture]); assert!(CLAW_TERMINAL_FRAMES.contains(&game.claw.target_frame)); } + #[test] + fn top_sensor_uses_the_original_score_and_contact_latch() { + let mut game = Game::new(1); + game.ball.in_launcher = false; + game.ball.position = vec2(205.0, 55.0); + let mut events = Vec::new(); + + game.check_sensor_objects( + MilliVec::from_position(vec2(205.0, 60.0)), + MilliVec { x: 0, y: -5_000 }, + &mut events, + ); + assert_eq!(game.player().score, 500); + assert_eq!(events, [Event::Target]); + assert!((game.magnets - 0.3).abs() < f32::EPSILON); + + game.check_sensor_objects( + MilliVec::from_position(game.ball.position), + MilliVec::default(), + &mut events, + ); + assert_eq!(game.player().score, 500, "contact must score only once"); + + game.ball.position = vec2(205.0, 70.0); + game.check_sensor_objects( + MilliVec::from_position(game.ball.position), + MilliVec::default(), + &mut events, + ); + game.ball.position = vec2(205.0, 55.0); + game.check_sensor_objects( + MilliVec::from_position(vec2(205.0, 60.0)), + MilliVec { x: 0, y: -5_000 }, + &mut events, + ); + assert_eq!(game.player().score, 1_000); + } + #[test] fn claw_release_table_decodes_the_original_thousandth_pixel_coordinates() { for (frame, expected) in [ diff --git a/tdkpin-rs/src/original_physics.rs b/tdkpin-rs/src/original_physics.rs index d42dfa4..7c1f3f4 100644 --- a/tdkpin-rs/src/original_physics.rs +++ b/tdkpin-rs/src/original_physics.rs @@ -204,6 +204,45 @@ pub fn collide_with_circle( true } +/// Test a non-physical circle record against the complete ball-center path. +pub fn path_intersects_circle( + old_position: MilliVec, + velocity: MilliVec, + center: Vec2, + radius: f32, +) -> bool { + let center = MilliVec::from_position(center); + let offset = subtract(old_position, center); + let radius_milli = (radius * 1_000.0).round() as i32; + let radius_squared = i64::from(radius_milli).pow(2); + let old_distance_squared = i64::from(offset.x).pow(2) + i64::from(offset.y).pow(2); + if old_distance_squared <= radius_squared { + return true; + } + let next = offset.add(velocity); + let next_distance_squared = i64::from(next.x).pow(2) + i64::from(next.y).pow(2); + if next_distance_squared <= radius_squared { + return true; + } + + let vx = f64::from(velocity.x); + let vy = f64::from(velocity.y); + let offset_x = f64::from(offset.x); + let offset_y = f64::from(offset.y); + let quadratic_a = vx * vx + vy * vy; + if quadratic_a == 0.0 { + return false; + } + let quadratic_b = 2.0 * (offset_x * vx + offset_y * vy); + let quadratic_c = old_distance_squared as f64 - f64::from(radius_milli).powi(2); + let discriminant = quadratic_b * quadratic_b - 4.0 * quadratic_a * quadratic_c; + if discriminant < 0.0 { + return false; + } + let progress = (-quadratic_b - discriminant.sqrt()) / (2.0 * quadratic_a); + 0.0 < progress && progress <= 1.0 +} + #[cfg(test)] mod tests { use super::*; diff --git a/tdkpin-rs/src/table.rs b/tdkpin-rs/src/table.rs index a33dc3b..5dc59a5 100644 --- a/tdkpin-rs/src/table.rs +++ b/tdkpin-rs/src/table.rs @@ -59,6 +59,25 @@ pub struct StaticCircle { pub normal_kick: f32, } +#[derive(Clone, Copy, Debug)] +pub struct TargetSensor { + pub id: u8, + pub center: Vec2, + pub radius: f32, + pub score: u32, +} + +impl TargetSensor { + const fn new(id: u8, center: Vec2, radius: f32, score: u32) -> Self { + Self { + id, + center, + radius, + score, + } + } +} + impl StaticCircle { const fn new(id: u8, center: Vec2, contact_radius: f32) -> Self { let (normal_rebound, tangent_coupling, normal_kick) = match id { @@ -242,6 +261,22 @@ pub const BUMPERS: [StaticCircle; 3] = [ StaticCircle::new(53, Vec2::new(219.0, 165.0), 23.0), ]; +/// Initially active type-4 circle records. Objects 149, 153, and 154 are +/// mechanism-controlled and begin disabled in the original table. +pub const TARGET_SENSORS: [TargetSensor; 11] = [ + TargetSensor::new(140, Vec2::new(24.0, 182.0), 12.0, 1_000), + TargetSensor::new(141, Vec2::new(18.0, 162.0), 12.0, 1_000), + TargetSensor::new(142, Vec2::new(17.0, 144.0), 12.0, 1_500), + TargetSensor::new(143, Vec2::new(17.0, 125.0), 12.0, 1_500), + TargetSensor::new(144, Vec2::new(17.0, 106.0), 12.0, 2_000), + TargetSensor::new(145, Vec2::new(17.0, 87.0), 12.0, 2_000), + TargetSensor::new(146, Vec2::new(17.0, 67.0), 12.0, 2_500), + TargetSensor::new(147, Vec2::new(17.0, 48.0), 12.0, 3_000), + TargetSensor::new(150, Vec2::new(205.0, 47.0), 11.0, 500), + TargetSensor::new(151, Vec2::new(233.0, 46.0), 11.0, 500), + TargetSensor::new(152, Vec2::new(264.0, 46.0), 11.0, 500), +]; + #[cfg(test)] mod tests { use super::*;