cleanup, fix crashes for some board-edge interactions

This commit is contained in:
Crispy 2024-10-05 23:16:24 +02:00
parent 682dff48f9
commit 0738e658db
3 changed files with 109 additions and 106 deletions

View file

@ -60,6 +60,7 @@ fn main() {
rl.set_window_min_size(640, 480);
rl.set_mouse_cursor(MouseCursor::MOUSE_CURSOR_CROSSHAIR);
rl.set_exit_key(None);
rl.set_trace_log(TraceLogLevel::LOG_WARNING);
let mut textures = Textures::default();
textures.load_dir("assets", &mut rl, &thread);

View file

@ -102,7 +102,7 @@ impl Machine {
// reset wires
for y in 0..self.board.height() {
for x in 0..self.board.width() {
if let Tile::Powerable(_, state) = self.board.get_mut_unchecked((x, y).into()) {
if let Some(Tile::Powerable(_, state)) = self.board.get_mut((x, y).into()) {
*state = false;
}
}
@ -113,95 +113,96 @@ impl Machine {
let marble_pos = self.marbles[i];
let tile = self.board.get_or_blank(marble_pos);
if let Tile::Marble { value, dir } = tile {
let next_pos = dir.step(marble_pos);
if !self.board.in_bounds(next_pos) {
continue;
let Tile::Marble { value, dir } = tile else {
continue;
};
let next_pos = dir.step(marble_pos);
if !self.board.in_bounds(next_pos) {
continue;
}
let mut new_tile = None;
let Some(target) = self.board.get_mut(next_pos) else {
continue;
};
match target {
Tile::Blank => {
*target = tile;
self.marbles[i] = next_pos;
new_tile = Some(Tile::Blank);
}
let mut new_tile = None;
let target = self.board.get_mut_unchecked(next_pos);
match target {
Tile::Blank => {
Tile::Digit(d) => {
let new_val = value.wrapping_mul(10).wrapping_add(*d as MarbleValue);
*target = Tile::Marble {
value: new_val,
dir,
};
self.marbles[i] = next_pos;
new_tile = Some(Tile::Blank);
}
Tile::Powerable(PTile::Bag, _) => {
to_remove.push(i);
new_tile = Some(Tile::Blank);
}
Tile::Marble {
value: _other_value,
dir: other_dir,
} => {
// bounce off other marbles
if *other_dir != dir {
new_tile = Some(Tile::Marble {
value,
dir: dir.opposite(),
});
*other_dir = dir;
}
}
Tile::Powerable(PTile::Trigger, state) => {
// triggers activate even if the marble can't move past it
// making it stay on permanently (or until the marble is knocked away)
*state = true;
triggers.push(next_pos);
let far_pos = dir.step(next_pos);
if let Some(target) = self.board.get_blank_mut(far_pos) {
*target = tile;
self.marbles[i] = next_pos;
self.marbles[i] = far_pos;
new_tile = Some(Tile::Blank);
}
Tile::Digit(d) => {
let new_val = value.wrapping_mul(10).wrapping_add(*d as MarbleValue);
}
Tile::Arrow(arrow_dir) => {
let far_pos = arrow_dir.step(next_pos);
let arrow_dir = *arrow_dir;
if let Some(target) = self.board.get_blank_mut(far_pos) {
self.marbles[i] = far_pos;
*target = Tile::Marble {
value: new_val,
dir,
value,
dir: arrow_dir,
};
self.marbles[i] = next_pos;
new_tile = Some(Tile::Blank);
} else if far_pos == marble_pos {
// always bounce on reverse arrow
new_tile = Some(Tile::Marble {
value,
dir: dir.opposite(),
});
}
Tile::Powerable(PTile::Bag, _) => {
to_remove.push(i);
new_tile = Some(Tile::Blank);
}
Tile::Marble {
value: _other_value,
dir: other_dir,
} => {
// bounce off other marbles
if *other_dir != dir {
new_tile = Some(Tile::Marble {
value,
dir: dir.opposite(),
});
*other_dir = dir;
}
}
Tile::Powerable(PTile::Trigger, state) => {
*state = true;
triggers.push(next_pos);
let far_pos = dir.step(next_pos);
if let Some(Tile::Blank) = self.board.get(far_pos) {
self.board.set(far_pos, Tile::Marble { value, dir });
self.marbles[i] = far_pos;
new_tile = Some(Tile::Blank);
}
}
Tile::Arrow(arrow_dir) => {
let far_pos = arrow_dir.step(next_pos);
let arrow_dir = *arrow_dir;
if let Some(Tile::Blank) = self.board.get(far_pos) {
self.marbles[i] = far_pos;
self.board.set(
far_pos,
Tile::Marble {
value,
dir: arrow_dir,
},
);
new_tile = Some(Tile::Blank);
} else if far_pos == marble_pos {
// bounce on reverse arrow
new_tile = Some(Tile::Marble {
value,
dir: dir.opposite(),
});
}
}
Tile::Mirror(mirror) => {
let new_dir = mirror.new_dir(dir);
let far_pos = new_dir.step(next_pos);
let far_target = self.board.get_mut_unchecked(far_pos);
if let Tile::Blank = far_target {
*far_target = Tile::Marble {
value,
dir: new_dir,
};
self.marbles[i] = far_pos;
new_tile = Some(Tile::Blank);
}
}
_ => (),
}
Tile::Mirror(mirror) => {
let new_dir = mirror.new_dir(dir);
let far_pos = new_dir.step(next_pos);
if let Some(far_target) = self.board.get_blank_mut(far_pos) {
*far_target = Tile::Marble {
value,
dir: new_dir,
};
self.marbles[i] = far_pos;
new_tile = Some(Tile::Blank);
}
}
_ => (),
}
if let Some(t) = new_tile {
*self.board.get_mut_unchecked(marble_pos) = t;
}
if let Some(t) = new_tile {
*self.board.get_mut(marble_pos).unwrap() = t;
}
}
let mut offset = 0;
@ -217,10 +218,9 @@ impl Machine {
}
fn propagate_power(&mut self, dir: Direction, pos: Pos) {
if !self.board.in_bounds(pos) {
let Some(tile) = self.board.get_mut(pos) else {
return;
}
let tile = self.board.get_mut_unchecked(pos);
};
let front_pos = dir.step(pos);
if let Tile::Powerable(tile, state) = tile {
if let PTile::Trigger = tile {
@ -244,19 +244,19 @@ impl Machine {
}
}
PTile::Bag => {
if let Some(Tile::Blank) = self.board.get(front_pos) {
*self.board.get_mut_unchecked(front_pos) = Tile::Marble { value: 0, dir };
if let Some(front) = self.board.get_blank_mut(front_pos) {
*front = Tile::Marble { value: 0, dir };
self.marbles.push(front_pos);
}
}
PTile::Input => {
if self.input_index < self.input.len()
&& self.board.get_or_blank(front_pos).is_blank()
{
let value = self.input[self.input_index] as MarbleValue;
*self.board.get_mut_unchecked(front_pos) = Tile::Marble { value, dir };
self.marbles.push(front_pos);
self.input_index += 1;
if let Some(front) = self.board.get_blank_mut(front_pos) {
if self.input_index < self.input.len() {
let value = self.input[self.input_index] as MarbleValue;
self.input_index += 1;
*front = Tile::Marble { value, dir };
self.marbles.push(front_pos);
}
}
}
PTile::Math(op) => {
@ -265,9 +265,8 @@ impl Machine {
let pos_b = dir.right().step(pos);
let val_a = self.board.get_or_blank(pos_a).read_value();
let val_b = self.board.get_or_blank(pos_b).read_value();
if (!self.board.get_or_blank(pos_a).is_blank()
|| !self.board.get_or_blank(pos_b).is_blank())
&& self.board.get_or_blank(front_pos).is_blank()
if !self.board.get_or_blank(pos_a).is_blank()
|| !self.board.get_or_blank(pos_b).is_blank()
{
let value = match op {
MathOp::Add => val_a.wrapping_add(val_b),
@ -277,13 +276,17 @@ impl Machine {
MathOp::Rem => val_a.checked_rem(val_b).unwrap_or_default(),
};
// println!("{op:?} a:{val_a} b:{val_b}");
*self.board.get_mut_unchecked(front_pos) = Tile::Marble { value, dir };
self.marbles.push(front_pos);
if let Some(front) = self.board.get_blank_mut(front_pos) {
*front = Tile::Marble { value, dir };
self.marbles.push(front_pos);
}
}
}
PTile::Flipper => {
let m = self.board.get_mut_unchecked(front_pos);
match m {
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,

View file

@ -108,15 +108,14 @@ impl Board {
}
}
pub fn get_mut_unchecked(&mut self, p: Pos) -> &mut Tile {
pub fn get_blank_mut(&mut self, p: Pos) -> Option<&mut Tile> {
if self.in_bounds(p) {
&mut self.rows[p.y as usize][p.x as usize]
} else {
panic!(
"position {p:?} out of bounds, size is {}x{}",
self.width, self.height
);
let tile = &mut self.rows[p.y as usize][p.x as usize];
if tile == &Tile::Blank {
return Some(tile);
}
}
None
}
pub fn set(&mut self, p: Pos, tile: Tile) {