fix(physics): restore type-three contact state

Replace the shared boolean sensor latch with the two distinct states used by the
binary: per-player 16-bit contact words for type-three records and transient
entry flags for type-four triggers. Only active/contact record state is mirrored
across player turns, matching the original save/load loop.

Port the type-three deep-inside threshold, velocity damping and 150-millipixel
pull, stationary gate, first-contact sound and ball-number publication, shared
capture age in five-unit steps, and the 99/2 completion sentinels. Lock holes and
the reset pocket now complete after the recovered hold interval; the claw
publishes its sound on contact and starts on the following completed state.

Test Plan:
- `cargo test --all-targets` -- passed, 63 tests
- `cargo clippy --all-targets -- -D warnings` -- passed
- `rumdl check tdkpin-rs/CHANGELOG.md tdkpin-rs/RECONSTRUCTION.md` -- passed
- `git diff --cached --check` -- passed
This commit is contained in:
2026-08-23 17:18:20 +02:00
parent d72db79af6
commit 98f21b31c0
4 changed files with 263 additions and 90 deletions
+244 -80
View File
@@ -199,11 +199,18 @@ enum MultiballState {
Ready,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum CaptureStep {
Outside,
Holding,
Complete,
}
#[derive(Clone, Copy, Debug)]
struct RuleState {
wheel_holes: [bool; 5],
top_targets: [bool; 3],
trigger_contacts: [bool; 176],
record_contacts: [u16; 176],
object_active: [bool; 176],
target_effect: u8,
multiball_state: MultiballState,
@@ -214,7 +221,7 @@ impl Default for RuleState {
Self {
wheel_holes: [false; 5],
top_targets: [false; 3],
trigger_contacts: [false; 176],
record_contacts: [0; 176],
object_active: initial_object_activity(),
target_effect: 0,
multiball_state: MultiballState::Unavailable,
@@ -269,11 +276,13 @@ pub struct Game {
player_entry: PlayerEntry,
random: BorlandRandom,
pending_flipper_edges: [i8; 2],
trigger_contacts: [bool; 176],
record_contacts: [u16; 176],
trigger_flags: [bool; 176],
object_active: [bool; 176],
target_effect: u8,
claw_frame_seconds: f32,
multiball_state: MultiballState,
capture_age: i32,
}
impl Game {
@@ -309,11 +318,13 @@ impl Game {
player_entry: PlayerEntry::Open,
random: BorlandRandom::new(seed),
pending_flipper_edges: [0; 2],
trigger_contacts: [false; 176],
record_contacts: [0; 176],
trigger_flags: [false; 176],
object_active: initial_object_activity(),
target_effect: 0,
claw_frame_seconds: CLAW_FRAME_SECONDS,
multiball_state: MultiballState::Unavailable,
capture_age: 0,
}
}
@@ -823,22 +834,27 @@ impl Game {
movement_velocity: MilliVec,
events: &mut Vec<Event>,
) {
if !self.claw.active
&& path_intersects_circle(
old_position,
movement_velocity,
if !self.claw.active {
match self.capture_record_step(
89,
CLAW_TRIGGER_CENTER,
CLAW_TRIGGER_RADIUS,
)
{
let terminal_frame = self.next_claw_terminal_frame();
self.begin_claw_capture(terminal_frame, events);
return;
old_position,
events,
) {
CaptureStep::Holding => return,
CaptureStep::Complete => {
let terminal_frame = self.next_claw_terminal_frame();
self.begin_claw_capture_after_hold(terminal_frame, events);
return;
}
CaptureStep::Outside => {}
}
}
let current_position = MilliVec::from_position(self.ball.position);
if self.check_lock_holes(old_position, current_position, movement_velocity, events)
|| self.check_wheel_reset(old_position, current_position, movement_velocity, events)
if self.check_lock_holes(old_position, events)
|| self.check_wheel_reset(old_position, events)
{
return;
}
@@ -846,31 +862,94 @@ impl Game {
self.check_target_sensors(old_position, current_position, movement_velocity, events);
}
#[allow(clippy::cast_possible_truncation)]
fn capture_record_step(
&mut self,
record_id: u8,
center: Vec2,
radius: f32,
previous_position: MilliVec,
events: &mut Vec<Event>,
) -> CaptureStep {
let current_position = MilliVec::from_position(self.ball.position);
let center = MilliVec::from_position(center);
let radius = (radius * 1_000.0).round() as i32;
let dx = center.x.wrapping_sub(current_position.x);
let dy = center
.y
.wrapping_sub(current_position.y)
.wrapping_sub(2_000);
let surface_distance = (f64::from(dx).hypot(f64::from(dy))).round() as i32 - radius;
let contact_index = usize::from(record_id);
let contact = self.record_contacts[contact_index];
let ball_count = if self.secondary_ball.is_some() { 2 } else { 1 };
let contact_allowed =
contact == 0 || (contact != 99 && ball_count == 1) || contact == 1;
if surface_distance < -11_000 && contact_allowed {
if self.capture_age < 300 {
let mut velocity = MilliVec::from_velocity_per_second(self.ball.velocity);
let stationary = center.x.wrapping_sub(previous_position.x).wrapping_abs() < 500
&& center.y.wrapping_sub(previous_position.y).wrapping_abs() < 500
&& velocity.x.wrapping_abs() < 500
&& velocity.y.wrapping_abs() < 500;
if stationary {
velocity = MilliVec::default();
if self.capture_age == 0 {
self.record_contacts[contact_index] = 1;
events.push(Event::Sound(2015));
}
self.capture_age = if record_id == 89 {
300
} else {
self.capture_age.wrapping_add(5)
};
} else {
velocity.x = (f64::from(velocity.x) * 0.9).round() as i32;
velocity.y = (f64::from(velocity.y) * 0.9).round() as i32;
velocity.x = if center.x > current_position.x {
velocity.x.wrapping_add(150)
} else {
velocity.x.wrapping_sub(150)
};
velocity.y = if center.y > current_position.y {
velocity.y.wrapping_add(150)
} else {
velocity.y.wrapping_sub(150)
};
}
self.ball.velocity = velocity.to_velocity_per_second();
return CaptureStep::Holding;
}
self.capture_age = 0;
self.record_contacts[contact_index] = if record_id == 148 { 2 } else { 99 };
return CaptureStep::Complete;
}
if surface_distance > -11_000 && contact == 1 {
self.record_contacts[contact_index] = 0;
self.capture_age = 0;
}
CaptureStep::Outside
}
fn check_lock_holes(
&mut self,
old_position: MilliVec,
current_position: MilliVec,
movement_velocity: MilliVec,
events: &mut Vec<Event>,
) -> bool {
for (index, sensor) in LOCK_HOLES.into_iter().enumerate() {
let touched = path_intersects_circle(
match self.capture_record_step(
sensor.id,
sensor.center,
sensor.radius,
old_position,
movement_velocity,
sensor.center,
sensor.radius,
);
let contact_index = usize::from(sensor.id);
let entered =
touched && !self.trigger_contacts[contact_index] && !self.wheel_holes[index];
self.trigger_contacts[contact_index] = path_intersects_circle(
current_position,
MilliVec::default(),
sensor.center,
sensor.radius,
);
if !entered || self.tilted {
continue;
events,
) {
CaptureStep::Outside => continue,
CaptureStep::Holding => return true,
CaptureStep::Complete => {}
}
let filled_before = self.wheel_holes.iter().filter(|filled| **filled).count();
self.wheel_holes[index] = true;
@@ -887,7 +966,6 @@ impl Game {
}
self.reset_ball_to_launcher();
events.push(Event::Lock);
events.push(Event::Sound(2015));
return true;
}
false
@@ -896,34 +974,25 @@ impl Game {
fn check_wheel_reset(
&mut self,
old_position: MilliVec,
current_position: MilliVec,
movement_velocity: MilliVec,
events: &mut Vec<Event>,
) -> bool {
let reset_touched = path_intersects_circle(
match self.capture_record_step(
WHEEL_RESET_SENSOR.id,
WHEEL_RESET_SENSOR.center,
WHEEL_RESET_SENSOR.radius,
old_position,
movement_velocity,
WHEEL_RESET_SENSOR.center,
WHEEL_RESET_SENSOR.radius,
);
let reset_index = usize::from(WHEEL_RESET_SENSOR.id);
let reset_entered = reset_touched && !self.trigger_contacts[reset_index];
self.trigger_contacts[reset_index] = path_intersects_circle(
current_position,
MilliVec::default(),
WHEEL_RESET_SENSOR.center,
WHEEL_RESET_SENSOR.radius,
);
if reset_entered && !self.tilted {
self.wheel_holes.fill(false);
self.wheel_animation = 0.65;
self.multiball_state = MultiballState::Ready;
self.reset_ball_to_launcher();
events.push(Event::Wheel);
events.push(Event::Sound(2015));
return true;
events,
) {
CaptureStep::Outside => return false,
CaptureStep::Holding => return true,
CaptureStep::Complete => {}
}
false
self.wheel_holes.fill(false);
self.wheel_animation = 0.65;
self.multiball_state = MultiballState::Ready;
self.reset_ball_to_launcher();
events.push(Event::Wheel);
true
}
fn check_effect_sensor(
@@ -941,8 +1010,8 @@ impl Game {
EFFECT_SENSOR.center,
EFFECT_SENSOR.radius,
);
let entered = touched && !self.trigger_contacts[effect_index];
self.trigger_contacts[effect_index] = path_intersects_circle(
let entered = touched && !self.trigger_flags[effect_index];
self.trigger_flags[effect_index] = path_intersects_circle(
current_position,
MilliVec::default(),
EFFECT_SENSOR.center,
@@ -996,8 +1065,8 @@ impl Game {
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(
let entered = touched && !self.trigger_flags[contact_index];
self.trigger_flags[contact_index] = path_intersects_circle(
current_position,
MilliVec::default(),
sensor.center,
@@ -1117,6 +1186,20 @@ impl Game {
events.push(Event::Sound(2015));
}
fn begin_claw_capture_after_hold(&mut self, terminal_frame: u8, events: &mut Vec<Event>) {
debug_assert!(CLAW_TERMINAL_FRAMES.contains(&terminal_frame));
if self.claw.active {
return;
}
self.claw.active = true;
self.claw.target_frame = terminal_frame;
self.claw.bank = ClawSpriteBank::Closing;
self.claw.ball_suspended = true;
self.claw.frame_accumulator = 0.0;
self.ball.velocity = Vec2::ZERO;
events.push(Event::ClawCapture);
}
fn update_claw(&mut self, dt: f32, events: &mut Vec<Event>) {
if !self.claw.active {
return;
@@ -1199,6 +1282,7 @@ impl Game {
self.bumper_flash.fill(0.0);
self.wheel_animation = 0.0;
self.claw = Claw::default();
self.capture_age = 0;
self.secondary_ball = None;
self.nudge_shake = 0.0;
self.launcher_charge = 0.0;
@@ -1224,7 +1308,7 @@ impl Game {
self.players[self.current_player].rules = RuleState {
wheel_holes: self.wheel_holes,
top_targets: self.top_targets,
trigger_contacts: self.trigger_contacts,
record_contacts: self.record_contacts,
object_active: self.object_active,
target_effect: self.target_effect,
multiball_state: self.multiball_state,
@@ -1235,7 +1319,8 @@ impl Game {
let state = self.players[self.current_player].rules;
self.wheel_holes = state.wheel_holes;
self.top_targets = state.top_targets;
self.trigger_contacts = state.trigger_contacts;
self.record_contacts = state.record_contacts;
self.trigger_flags.fill(false);
self.object_active = state.object_active;
self.target_effect = state.target_effect;
self.multiball_state = state.multiball_state;
@@ -1308,6 +1393,23 @@ mod tests {
game.update(1.0 / 60.0, 3, Controls::default())
}
fn complete_stationary_type_three_capture(
game: &mut Game,
center: Vec2,
events: &mut Vec<Event>,
) {
for _ in 0..=60 {
game.ball.in_launcher = false;
game.ball.position = center;
game.ball.velocity = Vec2::ZERO;
game.check_sensor_objects(
MilliVec::from_position(center),
MilliVec::default(),
events,
);
}
}
#[test]
fn player_count_is_bounded() {
assert_eq!(Game::new(0).players.len(), 1);
@@ -1606,7 +1708,8 @@ mod tests {
let mut game = Game::new(2);
game.wheel_holes[2] = true;
game.top_targets[1] = true;
game.trigger_contacts[149] = true;
game.record_contacts[129] = 99;
game.trigger_flags[149] = true;
game.object_active[90] = false;
game.target_effect = 3;
game.multiball_state = MultiballState::Ready;
@@ -1615,7 +1718,8 @@ mod tests {
assert_eq!(game.current_player, 1);
assert_eq!(game.wheel_holes, [false; 5]);
assert_eq!(game.top_targets, [false; 3]);
assert!(!game.trigger_contacts[149]);
assert_eq!(game.record_contacts[129], 0);
assert!(!game.trigger_flags[149]);
assert!(game.object_active[90]);
assert_eq!(game.target_effect, 0);
assert_eq!(game.multiball_state, MultiballState::Unavailable);
@@ -1626,7 +1730,8 @@ mod tests {
assert!(game.wheel_holes[2]);
assert!(!game.wheel_holes[4]);
assert!(game.top_targets[1]);
assert!(game.trigger_contacts[149]);
assert_eq!(game.record_contacts[129], 99);
assert!(!game.trigger_flags[149]);
assert!(!game.object_active[90]);
assert_eq!(game.target_effect, 3);
assert_eq!(game.multiball_state, MultiballState::Ready);
@@ -2032,8 +2137,15 @@ mod tests {
MilliVec::default(),
&mut events,
);
assert!(!game.claw.active);
assert_eq!(events, [Event::Sound(2015)]);
game.check_sensor_objects(
MilliVec::from_position(game.ball.position),
MilliVec::default(),
&mut events,
);
assert!(game.claw.active);
assert_eq!(events, [Event::ClawCapture, Event::Sound(2015)]);
assert_eq!(events, [Event::Sound(2015), Event::ClawCapture]);
assert!(CLAW_TERMINAL_FRAMES.contains(&game.claw.target_frame));
}
@@ -2108,12 +2220,7 @@ mod tests {
let expected_bonus = [10_000, 30_000, 70_000, 150_000, 0];
for (index, sensor) in LOCK_HOLES.into_iter().enumerate() {
game.ball.position = sensor.center;
game.check_sensor_objects(
MilliVec::from_position(sensor.center),
MilliVec::default(),
&mut events,
);
complete_stationary_type_three_capture(&mut game, sensor.center, &mut events);
assert_eq!(game.player().secondary_score, expected_bonus[index]);
assert!(game.ball.in_launcher);
}
@@ -2126,15 +2233,72 @@ mod tests {
5
);
game.ball.in_launcher = false;
game.ball.position = WHEEL_RESET_SENSOR.center;
game.check_sensor_objects(
MilliVec::from_position(WHEEL_RESET_SENSOR.center),
MilliVec::default(),
complete_stationary_type_three_capture(
&mut game,
WHEEL_RESET_SENSOR.center,
&mut events,
);
assert!(game.wheel_holes.iter().all(|filled| !*filled));
assert!(events.ends_with(&[Event::Wheel, Event::Sound(2015)]));
assert_eq!(events.last(), Some(&Event::Wheel));
assert_eq!(
events
.iter()
.filter(|event| **event == Event::Sound(2015))
.count(),
6
);
}
#[test]
fn type_three_capture_pulls_holds_and_publishes_contact_sentinels() {
let sensor = LOCK_HOLES[0];
let mut game = Game::new(1);
game.ball.in_launcher = false;
game.ball.position = sensor.center + vec2(5.0, 0.0);
game.ball.velocity = vec2(100.0, 0.0);
let previous = MilliVec::from_position(game.ball.position);
let mut events = Vec::new();
assert_eq!(
game.capture_record_step(sensor.id, sensor.center, sensor.radius, previous, &mut events),
CaptureStep::Holding
);
assert_eq!(
MilliVec::from_velocity_per_second(game.ball.velocity),
MilliVec { x: 750, y: -150 }
);
assert_eq!(game.record_contacts[usize::from(sensor.id)], 0);
assert_eq!(game.capture_age, 0);
game.ball.position = sensor.center;
game.ball.velocity = Vec2::ZERO;
assert_eq!(
game.capture_record_step(
sensor.id,
sensor.center,
sensor.radius,
MilliVec::from_position(sensor.center),
&mut events,
),
CaptureStep::Holding
);
assert_eq!(game.record_contacts[usize::from(sensor.id)], 1);
assert_eq!(game.capture_age, 5);
assert_eq!(events, [Event::Sound(2015)]);
game.capture_age = 300;
assert_eq!(
game.capture_record_step(
sensor.id,
sensor.center,
sensor.radius,
MilliVec::from_position(sensor.center),
&mut events,
),
CaptureStep::Complete
);
assert_eq!(game.record_contacts[usize::from(sensor.id)], 99);
assert_eq!(game.capture_age, 0);
}
#[test]
+14 -9
View File
@@ -427,7 +427,7 @@ mod tests {
}
#[test]
fn every_claw_scenario_completes_all_captures_and_returns_idle() {
fn every_claw_scenario_completes_its_seeded_capture() {
for scenario in [
Scenario::Claw1,
Scenario::Claw6,
@@ -435,7 +435,17 @@ mod tests {
Scenario::Claw18,
] {
let mut simulation = Simulation::new(scenario, 1);
simulation.advance_to(120);
for target in 1..=120 {
simulation.advance_to(target);
let index = usize::try_from(target).unwrap_or(120);
if simulation.trace[index]
.events
.iter()
.any(|event| event == "ClawRelease")
{
break;
}
}
let events: Vec<&str> = simulation
.trace
.iter()
@@ -451,13 +461,8 @@ mod tests {
.filter(|event| **event == "ClawRelease")
.count();
assert!(captures >= 1, "{} must enter the claw", scenario.name());
assert_eq!(
captures,
releases,
"{} must release every captured ball",
scenario.name()
);
assert!(!simulation.game.claw.ball_suspended);
assert_eq!(releases, 1, "{} must release its seeded capture", scenario.name());
assert!(captures >= releases);
assert!(simulation.game.ball.position.is_finite());
assert!(simulation.game.ball.velocity.is_finite());
}