From 78027aaaa063038413daf6a1555f30a29f67a74f Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Sat, 7 Dec 2024 23:19:32 +0100 Subject: [PATCH] fix update order dependent behavior of wires when flipped --- src/marble_engine.rs | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/src/marble_engine.rs b/src/marble_engine.rs index e4c997c..bb897fe 100644 --- a/src/marble_engine.rs +++ b/src/marble_engine.rs @@ -17,6 +17,7 @@ pub struct Machine { marbles: Vec, powered: Vec, events: Vec, + flipper_events: Vec, input: Vec, input_index: usize, @@ -45,6 +46,7 @@ impl Machine { marbles: Vec::new(), powered: Vec::new(), events: Vec::new(), + flipper_events: Vec::new(), input, input_index: 0, output: Vec::new(), @@ -290,11 +292,31 @@ impl Machine { } // process triggers + self.flipper_events.clear(); for pos in triggers_activated { for dir in Direction::ALL { self.propagate_power(dir, dir.step(pos)); } } + + // process flipped tiles + for &p in &self.flipper_events { + let Some(target) = self.board.get_mut(p) else { + continue; + }; + match target { + Tile::Powerable(PTile::Wire(wire_type), _) => { + *wire_type = match *wire_type { + WireType::Vertical => WireType::Horizontal, + WireType::Horizontal => WireType::Vertical, + WireType::Cross => WireType::Cross, + } + } + Tile::Mirror(mirror) => mirror.flip(), + Tile::Arrow(dir) => *dir = dir.opposite(), + _ => (), + }; + } } fn propagate_power(&mut self, dir: Direction, pos: Pos) { @@ -359,21 +381,7 @@ impl Machine { } } PTile::Flipper => { - let Some(target) = self.board.get_mut(front_pos) else { - return; - }; - match target { - Tile::Powerable(PTile::Wire(wire_type), _) => { - *wire_type = match *wire_type { - WireType::Vertical => WireType::Horizontal, - WireType::Horizontal => WireType::Vertical, - WireType::Cross => WireType::Cross, - } - } - Tile::Mirror(mirror) => mirror.flip(), - Tile::Arrow(dir) => *dir = dir.opposite(), - _ => (), - }; + self.flipper_events.push(front_pos); } PTile::Gate(gate) => { let gate = *gate;