2024-10-04 21:20:53 +02:00
|
|
|
use raylib::prelude::*;
|
2024-10-03 22:59:49 +02:00
|
|
|
|
2024-10-04 22:10:00 +02:00
|
|
|
pub mod board;
|
|
|
|
pub mod tile;
|
2024-10-03 22:59:49 +02:00
|
|
|
use board::{Board, Pos};
|
|
|
|
use tile::*;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Machine {
|
|
|
|
board: Board,
|
|
|
|
marbles: Vec<Pos>,
|
|
|
|
|
|
|
|
input: Vec<u8>,
|
|
|
|
input_index: usize,
|
|
|
|
output: Vec<u8>,
|
|
|
|
steps: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Machine {
|
|
|
|
pub fn new_empty(width: usize) -> Self {
|
|
|
|
Self {
|
|
|
|
board: Board::new_empty(width, width),
|
|
|
|
marbles: Vec::new(),
|
|
|
|
input: Vec::new(),
|
|
|
|
input_index: 0,
|
|
|
|
output: Vec::new(),
|
|
|
|
steps: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-04 22:10:00 +02:00
|
|
|
pub fn reset(&mut self) {
|
|
|
|
self.steps = 0;
|
|
|
|
self.input_index = 0;
|
|
|
|
self.output.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_board(&mut self, board: Board) {
|
|
|
|
self.marbles.clear();
|
|
|
|
for y in 0..board.height() {
|
|
|
|
for x in 0..board.width() {
|
|
|
|
if let Some(Tile::Marble { value: _, dir: _ }) = board.get((x, y).into()) {
|
|
|
|
self.marbles.push((x, y).into());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.board = board;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn board(&self) -> &Board {
|
|
|
|
&self.board
|
|
|
|
}
|
|
|
|
|
2024-10-04 21:20:53 +02:00
|
|
|
pub fn output(&self) -> &[u8] {
|
|
|
|
&self.output
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn input(&self) -> &[u8] {
|
|
|
|
&self.input
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_input(&mut self, bytes: Vec<u8>) {
|
2024-10-05 15:57:20 +02:00
|
|
|
self.input_index = self.input_index.min(bytes.len());
|
2024-10-04 21:20:53 +02:00
|
|
|
self.input = bytes;
|
|
|
|
}
|
|
|
|
|
2024-10-04 22:10:00 +02:00
|
|
|
pub fn draw_marble_values(&self, d: &mut RaylibDrawHandle, offset: Vector2, zoom: i32) {
|
2024-10-04 21:20:53 +02:00
|
|
|
let tile_size = 16 << zoom;
|
2024-10-03 22:59:49 +02:00
|
|
|
for x in 0..self.board.width() {
|
|
|
|
for y in 0..self.board.height() {
|
|
|
|
if let Some(tile) = self.board.get((x, y).into()) {
|
|
|
|
let px = x as i32 * tile_size + offset.x as i32 + tile_size / 2;
|
|
|
|
let py = y as i32 * tile_size + offset.y as i32 + tile_size / 2;
|
2024-10-05 20:48:05 +02:00
|
|
|
if let Tile::Marble { value, dir } = tile {
|
|
|
|
let fontsize = (zoom + 1) * 10;
|
2024-10-04 22:10:00 +02:00
|
|
|
d.draw_text(
|
|
|
|
&format!("{value}"),
|
|
|
|
px - tile_size / 2 + 2,
|
2024-10-04 22:35:15 +02:00
|
|
|
py - tile_size / 2 + 2,
|
2024-10-05 20:48:05 +02:00
|
|
|
fontsize,
|
2024-10-04 22:10:00 +02:00
|
|
|
Color::MAGENTA,
|
2024-10-05 20:48:05 +02:00
|
|
|
);
|
|
|
|
d.draw_text(
|
|
|
|
match dir {
|
|
|
|
Direction::Up => "^",
|
|
|
|
Direction::Down => "v",
|
|
|
|
Direction::Left => "<",
|
|
|
|
Direction::Right => ">",
|
|
|
|
},
|
|
|
|
px - tile_size / 2 + 2,
|
|
|
|
py - tile_size / 2 + fontsize,
|
|
|
|
fontsize,
|
|
|
|
Color::MAGENTA,
|
|
|
|
);
|
2024-10-04 22:10:00 +02:00
|
|
|
}
|
2024-10-03 22:59:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn step(&mut self) {
|
|
|
|
self.steps += 1;
|
|
|
|
// reset wires
|
|
|
|
for y in 0..self.board.height() {
|
|
|
|
for x in 0..self.board.width() {
|
2024-10-05 20:22:18 +02:00
|
|
|
if let Tile::Powerable(_, state) = self.board.get_mut_unchecked((x, y).into()) {
|
2024-10-04 01:21:52 +02:00
|
|
|
*state = false;
|
2024-10-03 22:59:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let mut to_remove = Vec::new();
|
|
|
|
let mut triggers = Vec::new();
|
|
|
|
for i in 0..self.marbles.len() {
|
|
|
|
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 mut new_tile = None;
|
2024-10-05 20:22:18 +02:00
|
|
|
let target = self.board.get_mut_unchecked(next_pos);
|
2024-10-03 22:59:49 +02:00
|
|
|
match target {
|
|
|
|
Tile::Blank => {
|
|
|
|
*target = tile;
|
|
|
|
self.marbles[i] = next_pos;
|
|
|
|
new_tile = Some(Tile::Blank);
|
|
|
|
}
|
|
|
|
Tile::Digit(d) => {
|
2024-10-05 20:48:05 +02:00
|
|
|
let new_val = value.wrapping_mul(10).wrapping_add(*d as MarbleValue);
|
2024-10-03 22:59:49 +02:00
|
|
|
*target = Tile::Marble {
|
|
|
|
value: new_val,
|
|
|
|
dir,
|
|
|
|
};
|
|
|
|
self.marbles[i] = next_pos;
|
|
|
|
new_tile = Some(Tile::Blank);
|
|
|
|
}
|
2024-10-03 23:34:16 +02:00
|
|
|
Tile::Powerable(PTile::Bag, _) => {
|
2024-10-03 22:59:49 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2024-10-04 01:21:52 +02:00
|
|
|
Tile::Powerable(PTile::Trigger, state) => {
|
2024-10-03 22:59:49 +02:00
|
|
|
*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);
|
2024-10-05 20:22:18 +02:00
|
|
|
let far_target = self.board.get_mut_unchecked(far_pos);
|
2024-10-03 22:59:49 +02:00
|
|
|
if let Tile::Blank = far_target {
|
|
|
|
*far_target = Tile::Marble {
|
|
|
|
value,
|
|
|
|
dir: new_dir,
|
|
|
|
};
|
|
|
|
self.marbles[i] = far_pos;
|
|
|
|
new_tile = Some(Tile::Blank);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(t) = new_tile {
|
2024-10-05 20:22:18 +02:00
|
|
|
*self.board.get_mut_unchecked(marble_pos) = t;
|
2024-10-03 22:59:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let mut offset = 0;
|
|
|
|
for i in to_remove {
|
|
|
|
self.marbles.remove(i - offset);
|
|
|
|
offset += 1;
|
|
|
|
}
|
|
|
|
for pos in triggers {
|
|
|
|
for dir in Direction::ALL {
|
|
|
|
self.propagate_power(dir, dir.step(pos));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn propagate_power(&mut self, dir: Direction, pos: Pos) {
|
|
|
|
if !self.board.in_bounds(pos) {
|
|
|
|
return;
|
|
|
|
}
|
2024-10-05 20:22:18 +02:00
|
|
|
let tile = self.board.get_mut_unchecked(pos);
|
2024-10-03 22:59:49 +02:00
|
|
|
let front_pos = dir.step(pos);
|
2024-10-03 23:34:16 +02:00
|
|
|
if let Tile::Powerable(tile, state) = tile {
|
2024-10-04 01:21:52 +02:00
|
|
|
if let PTile::Trigger = tile {
|
|
|
|
return;
|
|
|
|
}
|
2024-10-03 23:34:16 +02:00
|
|
|
if *state {
|
|
|
|
return;
|
2024-10-03 22:59:49 +02:00
|
|
|
}
|
2024-10-03 23:34:16 +02:00
|
|
|
*state = true;
|
|
|
|
match tile {
|
|
|
|
PTile::Wire(wiretype) => {
|
|
|
|
let dirs = wiretype.directions();
|
|
|
|
for d in dirs {
|
|
|
|
self.propagate_power(*d, d.step(pos));
|
|
|
|
}
|
2024-10-03 22:59:49 +02:00
|
|
|
}
|
2024-10-03 23:34:16 +02:00
|
|
|
PTile::Output => {
|
|
|
|
let sample = self.board.get_or_blank(front_pos);
|
|
|
|
if let Tile::Marble { value, dir: _ } = sample {
|
|
|
|
self.output.push(value as u8);
|
|
|
|
}
|
2024-10-03 22:59:49 +02:00
|
|
|
}
|
2024-10-03 23:34:16 +02:00
|
|
|
PTile::Bag => {
|
|
|
|
if let Some(Tile::Blank) = self.board.get(front_pos) {
|
2024-10-05 20:22:18 +02:00
|
|
|
*self.board.get_mut_unchecked(front_pos) = Tile::Marble { value: 0, dir };
|
2024-10-03 23:34:16 +02:00
|
|
|
self.marbles.push(front_pos);
|
|
|
|
}
|
2024-10-03 22:59:49 +02:00
|
|
|
}
|
2024-10-03 23:34:16 +02:00
|
|
|
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;
|
2024-10-05 20:22:18 +02:00
|
|
|
*self.board.get_mut_unchecked(front_pos) = Tile::Marble { value, dir };
|
2024-10-03 23:34:16 +02:00
|
|
|
self.marbles.push(front_pos);
|
|
|
|
self.input_index += 1;
|
2024-10-03 22:59:49 +02:00
|
|
|
}
|
2024-10-03 23:34:16 +02:00
|
|
|
}
|
|
|
|
PTile::Math(op) => {
|
|
|
|
let op = *op;
|
|
|
|
let pos_a = dir.left().step(pos);
|
|
|
|
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()
|
|
|
|
{
|
2024-10-05 20:48:05 +02:00
|
|
|
let value = match op {
|
2024-10-03 23:34:16 +02:00
|
|
|
MathOp::Add => val_a.wrapping_add(val_b),
|
|
|
|
MathOp::Sub => val_a.wrapping_sub(val_b),
|
|
|
|
MathOp::Mul => val_a.wrapping_mul(val_b),
|
|
|
|
MathOp::Div => val_a.checked_div(val_b).unwrap_or_default(),
|
|
|
|
MathOp::Rem => val_a.checked_rem(val_b).unwrap_or_default(),
|
2024-10-03 22:59:49 +02:00
|
|
|
};
|
2024-10-03 23:34:16 +02:00
|
|
|
// println!("{op:?} a:{val_a} b:{val_b}");
|
2024-10-05 20:48:05 +02:00
|
|
|
*self.board.get_mut_unchecked(front_pos) = Tile::Marble { value, dir };
|
2024-10-03 23:34:16 +02:00
|
|
|
self.marbles.push(front_pos);
|
2024-10-03 22:59:49 +02:00
|
|
|
}
|
2024-10-03 23:34:16 +02:00
|
|
|
}
|
|
|
|
PTile::Flipper => {
|
2024-10-05 20:22:18 +02:00
|
|
|
let m = self.board.get_mut_unchecked(front_pos);
|
2024-10-03 23:34:16 +02:00
|
|
|
match m {
|
|
|
|
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 = match *mirror {
|
|
|
|
MirrorType::Forward => MirrorType::Back,
|
|
|
|
MirrorType::Back => MirrorType::Forward,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
Tile::Arrow(dir) => {
|
|
|
|
*dir = dir.opposite();
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
PTile::Gate(gate) => {
|
|
|
|
let gate = *gate;
|
|
|
|
let pos_a = dir.left().step(pos);
|
|
|
|
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();
|
2024-10-03 22:59:49 +02:00
|
|
|
|
2024-10-03 23:34:16 +02:00
|
|
|
let result = match gate {
|
|
|
|
GateType::LessThan => val_a < val_b,
|
|
|
|
GateType::GreaterThan => val_a > val_b,
|
|
|
|
GateType::Equal => val_a == val_b,
|
|
|
|
GateType::NotEqual => val_a != val_b,
|
|
|
|
};
|
|
|
|
if result {
|
|
|
|
self.propagate_power(dir, dir.step(pos));
|
|
|
|
}
|
2024-10-03 22:59:49 +02:00
|
|
|
}
|
2024-10-04 01:21:52 +02:00
|
|
|
PTile::Trigger => (),
|
2024-10-03 22:59:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|