439 lines
11 KiB
Rust
439 lines
11 KiB
Rust
use raylib::prelude::*;
|
|
|
|
pub mod board;
|
|
pub mod pos;
|
|
pub mod tile;
|
|
use board::Board;
|
|
use pos::*;
|
|
use tile::*;
|
|
|
|
use crate::{draw_usize_small, Textures};
|
|
|
|
#[derive(Debug)]
|
|
pub struct Machine {
|
|
board: Board,
|
|
marbles: Vec<Pos>,
|
|
powered: Vec<Pos>,
|
|
flipper_events: Vec<Pos>,
|
|
|
|
input: Vec<u8>,
|
|
input_index: usize,
|
|
output: Vec<u8>,
|
|
steps: usize,
|
|
}
|
|
|
|
impl Machine {
|
|
pub fn new_empty(input: Vec<u8>, width: usize) -> Self {
|
|
Self {
|
|
board: Board::new_empty(width, width),
|
|
marbles: Vec::new(),
|
|
powered: Vec::new(),
|
|
flipper_events: Vec::new(),
|
|
input,
|
|
input_index: 0,
|
|
output: Vec::new(),
|
|
steps: 0,
|
|
}
|
|
}
|
|
|
|
pub fn reset(&mut self) {
|
|
self.steps = 0;
|
|
self.input_index = 0;
|
|
self.output.clear();
|
|
self.powered.clear();
|
|
}
|
|
|
|
pub fn set_board(&mut self, board: Board) {
|
|
self.marbles = board.get_marbles();
|
|
self.powered.clear();
|
|
self.board = board;
|
|
}
|
|
|
|
pub fn board(&self) -> &Board {
|
|
&self.board
|
|
}
|
|
|
|
pub fn output(&self) -> &[u8] {
|
|
&self.output
|
|
}
|
|
|
|
pub fn input(&self) -> &[u8] {
|
|
&self.input
|
|
}
|
|
|
|
pub fn input_index(&self) -> usize {
|
|
self.input_index
|
|
}
|
|
|
|
pub fn step_count(&self) -> usize {
|
|
self.steps
|
|
}
|
|
|
|
pub fn set_input(&mut self, bytes: Vec<u8>) {
|
|
self.input_index = self.input_index.min(bytes.len());
|
|
self.input = bytes;
|
|
}
|
|
|
|
pub fn draw_marble_values(
|
|
&self,
|
|
d: &mut RaylibDrawHandle,
|
|
textures: &Textures,
|
|
offset: Vector2,
|
|
zoom: i32,
|
|
) {
|
|
let tile_size = 16 << zoom;
|
|
let scale = (1 << zoom) as f32;
|
|
for marble in &self.marbles {
|
|
let x = marble.x;
|
|
let y = marble.y;
|
|
if let Some(tile) = self.board.get(*marble) {
|
|
let px = x as i32 * tile_size + offset.x as i32;
|
|
let py = y as i32 * tile_size + offset.y as i32;
|
|
if let Tile::Marble { value, dir } = tile {
|
|
let texture = textures.get(dir.arrow_texture_name());
|
|
let pos = Vector2::new(px as f32, py as f32);
|
|
let faded_white = Color::new(255, 255, 255, 100);
|
|
d.draw_texture_ex(texture, pos, 0., scale, faded_white);
|
|
draw_usize_small(d, textures, value as usize, px, py, scale);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn step(&mut self) {
|
|
self.steps += 1;
|
|
// reset wires
|
|
for &p in &self.powered {
|
|
let Some(Tile::Powerable(_, state)) = self.board.get_mut(p) else {
|
|
unreachable!()
|
|
};
|
|
*state = false;
|
|
}
|
|
self.powered.clear();
|
|
|
|
if self.marbles.is_empty() {
|
|
return;
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
enum DirInfluence {
|
|
None,
|
|
One(Direction),
|
|
Multiple,
|
|
}
|
|
|
|
// find all direct bounces
|
|
let mut will_reverse_direction = vec![false; self.marbles.len()];
|
|
// todo store in tile to remove search through self.marbles
|
|
let mut influenced_direction = vec![DirInfluence::None; self.marbles.len()];
|
|
|
|
for (i, &pos) in self.marbles.iter().enumerate() {
|
|
let Some(Tile::Marble { value: _, dir }) = self.board.get(pos) else {
|
|
unreachable!()
|
|
};
|
|
let front_pos = dir.step(pos);
|
|
let Some(front_tile) = self.board.get(front_pos) else {
|
|
continue;
|
|
};
|
|
match front_tile {
|
|
Tile::Marble {
|
|
value: _,
|
|
dir: other_dir,
|
|
} => {
|
|
if other_dir != dir {
|
|
// this marble is facing another marble, and will therefore definitely bounce
|
|
will_reverse_direction[i] = true;
|
|
// the other marble will bounce too, either
|
|
let other_index =
|
|
self.marbles.iter().position(|m| *m == front_pos).unwrap();
|
|
let influence = &mut influenced_direction[other_index];
|
|
*influence = match *influence {
|
|
DirInfluence::None => DirInfluence::One(dir),
|
|
DirInfluence::One(_) => DirInfluence::Multiple,
|
|
DirInfluence::Multiple => DirInfluence::Multiple,
|
|
}
|
|
}
|
|
}
|
|
Tile::Arrow(arrow_dir) => {
|
|
if arrow_dir == dir.opposite() {
|
|
// bounce on a reverse facing arrow
|
|
will_reverse_direction[i] = true;
|
|
}
|
|
}
|
|
_ => (),
|
|
}
|
|
}
|
|
// apply all direct bounces
|
|
for (i, &pos) in self.marbles.iter().enumerate() {
|
|
let Some(Tile::Marble { value: _, dir }) = self.board.get_mut(pos) else {
|
|
unreachable!()
|
|
};
|
|
if will_reverse_direction[i] {
|
|
*dir = dir.opposite();
|
|
} else {
|
|
if let DirInfluence::One(new_dir) = influenced_direction[i] {
|
|
*dir = new_dir;
|
|
}
|
|
}
|
|
}
|
|
|
|
let mut claim_positions = Vec::new();
|
|
// mark claims to figure out what spaces can be moved to
|
|
for &pos in &self.marbles {
|
|
let Some(Tile::Marble { value: _, dir }) = self.board.get(pos) else {
|
|
unreachable!()
|
|
};
|
|
let front_pos = dir.step(pos);
|
|
let Some(front_tile) = self.board.get_mut(front_pos) else {
|
|
continue;
|
|
};
|
|
if let Tile::Open(_type, claim) = front_tile {
|
|
*claim = match claim {
|
|
MarbleTarget::Free => {
|
|
claim_positions.push(front_pos);
|
|
MarbleTarget::Claimed
|
|
}
|
|
MarbleTarget::ClaimedIndirect => MarbleTarget::Claimed,
|
|
MarbleTarget::BlockedIndirect => MarbleTarget::Claimed,
|
|
MarbleTarget::Claimed => MarbleTarget::Blocked,
|
|
MarbleTarget::Blocked => MarbleTarget::Blocked,
|
|
};
|
|
} else {
|
|
let target_pos;
|
|
match front_tile {
|
|
Tile::Arrow(d) => {
|
|
target_pos = d.step(front_pos);
|
|
}
|
|
Tile::Mirror(m) => {
|
|
target_pos = m.new_dir(dir).step(front_pos);
|
|
}
|
|
Tile::Powerable(PTile::Trigger, _) => {
|
|
target_pos = dir.step(front_pos);
|
|
}
|
|
_ => continue,
|
|
}
|
|
let Some(target_tile) = self.board.get_mut(target_pos) else {
|
|
continue;
|
|
};
|
|
if let Tile::Open(_type, claim) = target_tile {
|
|
*claim = match claim {
|
|
MarbleTarget::Free => {
|
|
claim_positions.push(front_pos);
|
|
MarbleTarget::ClaimedIndirect
|
|
}
|
|
MarbleTarget::ClaimedIndirect => MarbleTarget::BlockedIndirect,
|
|
MarbleTarget::BlockedIndirect => MarbleTarget::BlockedIndirect,
|
|
MarbleTarget::Claimed => MarbleTarget::Claimed,
|
|
MarbleTarget::Blocked => MarbleTarget::Blocked,
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
let mut triggers_activated = Vec::new();
|
|
let mut removed_marbles = Vec::new();
|
|
// move marbles
|
|
for (i, pos) in self.marbles.iter_mut().enumerate() {
|
|
let Some(Tile::Marble { value, dir }) = self.board.get(*pos) else {
|
|
unreachable!()
|
|
};
|
|
let front_pos = dir.step(*pos);
|
|
let Some(front_tile) = self.board.get_mut(front_pos) else {
|
|
continue;
|
|
};
|
|
|
|
let mut move_to = |tile, target_pos, dir, board: &mut Board| {
|
|
let value = match tile {
|
|
OpenTile::Blank => value,
|
|
OpenTile::Digit(n) => value.wrapping_mul(10).wrapping_add(n as MarbleValue),
|
|
};
|
|
board.set(*pos, Tile::default());
|
|
board.set(target_pos, Tile::Marble { value, dir });
|
|
*pos = target_pos;
|
|
};
|
|
|
|
if let Tile::Open(space_type, claim_state) = front_tile {
|
|
if *claim_state == MarbleTarget::Claimed {
|
|
move_to(*space_type, front_pos, dir, &mut self.board);
|
|
} else if *claim_state != MarbleTarget::Free {
|
|
// (Free means a marble was just here but moved earlier this tick)
|
|
// bounce on failed direct movement
|
|
self.board.set(
|
|
*pos,
|
|
Tile::Marble {
|
|
value,
|
|
dir: dir.opposite(),
|
|
},
|
|
);
|
|
}
|
|
} else {
|
|
let target_pos;
|
|
let mut is_trigger = false;
|
|
let mut new_dir = dir;
|
|
match front_tile {
|
|
Tile::Arrow(d) => {
|
|
target_pos = d.step(front_pos);
|
|
new_dir = *d;
|
|
}
|
|
Tile::Mirror(m) => {
|
|
new_dir = m.new_dir(dir);
|
|
target_pos = new_dir.step(front_pos);
|
|
}
|
|
Tile::Powerable(PTile::Trigger, _) => {
|
|
is_trigger = true;
|
|
target_pos = dir.step(front_pos);
|
|
}
|
|
Tile::Powerable(PTile::Bag, _) => {
|
|
removed_marbles.push(i);
|
|
continue;
|
|
}
|
|
Tile::Powerable(PTile::IO, _) => {
|
|
removed_marbles.push(i);
|
|
self.output.push(value as u8);
|
|
continue;
|
|
}
|
|
_ => continue,
|
|
}
|
|
let Some(target_tile) = self.board.get_mut(target_pos) else {
|
|
continue;
|
|
};
|
|
if let Tile::Open(space_type, MarbleTarget::ClaimedIndirect) = target_tile {
|
|
move_to(*space_type, target_pos, new_dir, &mut self.board);
|
|
if is_trigger {
|
|
triggers_activated.push(front_pos);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
for pos in claim_positions {
|
|
if let Some(Tile::Open(_, claim_state)) = self.board.get_mut(pos) {
|
|
*claim_state = MarbleTarget::Free;
|
|
}
|
|
}
|
|
|
|
// remove marbles
|
|
for &i in removed_marbles.iter().rev() {
|
|
self.board.set(self.marbles[i], Tile::default());
|
|
self.marbles.swap_remove(i);
|
|
}
|
|
|
|
// process triggers
|
|
self.flipper_events.clear();
|
|
for pos in triggers_activated {
|
|
let Some(Tile::Powerable(PTile::Trigger, state)) = self.board.get_mut(pos) else {
|
|
unreachable!()
|
|
};
|
|
self.powered.push(pos);
|
|
*state = true;
|
|
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) {
|
|
let Some(tile) = self.board.get_mut(pos) else {
|
|
return;
|
|
};
|
|
let front_pos = dir.step(pos);
|
|
if let Tile::Powerable(tile, state) = tile {
|
|
if let PTile::Trigger = tile {
|
|
return;
|
|
}
|
|
let was_powered = *state;
|
|
*state = true;
|
|
self.powered.push(pos);
|
|
match tile {
|
|
PTile::Wire(wiretype) => {
|
|
if was_powered {
|
|
return;
|
|
}
|
|
let dirs = wiretype.directions();
|
|
for d in dirs {
|
|
self.propagate_power(*d, d.step(pos));
|
|
}
|
|
}
|
|
PTile::Bag => {
|
|
if let Some(front) = self.board.get_blank_mut(front_pos) {
|
|
*front = Tile::Marble { value: 0, dir };
|
|
self.marbles.push(front_pos);
|
|
}
|
|
}
|
|
PTile::IO => {
|
|
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) => {
|
|
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()
|
|
{
|
|
let value = match op {
|
|
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(),
|
|
};
|
|
// println!("{op:?} a:{val_a} b:{val_b}");
|
|
if let Some(front) = self.board.get_blank_mut(front_pos) {
|
|
*front = Tile::Marble { value, dir };
|
|
self.marbles.push(front_pos);
|
|
}
|
|
}
|
|
}
|
|
PTile::Flipper => {
|
|
self.flipper_events.push(front_pos);
|
|
}
|
|
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();
|
|
|
|
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));
|
|
}
|
|
}
|
|
PTile::Trigger => (),
|
|
}
|
|
}
|
|
}
|
|
}
|