make all powerable tiles also store a powered state that can be rendered
This commit is contained in:
parent
bf46a3e7c3
commit
12bd19a7f3
3 changed files with 152 additions and 144 deletions
|
@ -6,10 +6,10 @@ use raylib::prelude::*;
|
||||||
mod marble_engine;
|
mod marble_engine;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let (mut rl, thread) = raylib::init().resizable().title("Hello, World").build();
|
let (mut rl, thread) = raylib::init().resizable().title("good window title").build();
|
||||||
rl.set_target_fps(60);
|
rl.set_target_fps(60);
|
||||||
|
|
||||||
let board = parse(&read_to_string("boards/flow.mbl").unwrap());
|
let board = parse(&read_to_string("boards/adder.mbl").unwrap());
|
||||||
let mut pos_offset = Vector2::zero();
|
let mut pos_offset = Vector2::zero();
|
||||||
let mut machine = marble_engine::Machine::new(board, "Vec::new()".bytes().collect());
|
let mut machine = marble_engine::Machine::new(board, "Vec::new()".bytes().collect());
|
||||||
|
|
||||||
|
@ -28,7 +28,6 @@ fn main() {
|
||||||
d.clear_background(Color::new(64, 64, 64, 255));
|
d.clear_background(Color::new(64, 64, 64, 255));
|
||||||
|
|
||||||
machine.draw(&mut d, pos_offset);
|
machine.draw(&mut d, pos_offset);
|
||||||
d.draw_text("Hello, world!", 12, 12, 20, Color::WHITE);
|
|
||||||
|
|
||||||
d.draw_fps(2, 2);
|
d.draw_fps(2, 2);
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,7 +67,7 @@ impl Machine {
|
||||||
for y in 0..self.board.height() {
|
for y in 0..self.board.height() {
|
||||||
for x in 0..self.board.width() {
|
for x in 0..self.board.width() {
|
||||||
match self.board.get_mut((x, y).into()) {
|
match self.board.get_mut((x, y).into()) {
|
||||||
Tile::Wire(_, state) => *state = false,
|
Tile::Powerable(_, state) => *state = false,
|
||||||
Tile::Trigger(state) => *state = false,
|
Tile::Trigger(state) => *state = false,
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
|
@ -103,7 +103,7 @@ impl Machine {
|
||||||
self.marbles[i] = next_pos;
|
self.marbles[i] = next_pos;
|
||||||
new_tile = Some(Tile::Blank);
|
new_tile = Some(Tile::Blank);
|
||||||
}
|
}
|
||||||
Tile::Bag => {
|
Tile::Powerable(PTile::Bag, _) => {
|
||||||
to_remove.push(i);
|
to_remove.push(i);
|
||||||
new_tile = Some(Tile::Blank);
|
new_tile = Some(Tile::Blank);
|
||||||
}
|
}
|
||||||
|
@ -190,107 +190,102 @@ impl Machine {
|
||||||
}
|
}
|
||||||
let tile = self.board.get_mut(pos);
|
let tile = self.board.get_mut(pos);
|
||||||
let front_pos = dir.step(pos);
|
let front_pos = dir.step(pos);
|
||||||
match tile {
|
if let Tile::Powerable(tile, state) = tile {
|
||||||
Tile::Wire(wiretype, state) => {
|
if *state {
|
||||||
if *state {
|
return;
|
||||||
return;
|
|
||||||
}
|
|
||||||
*state = true;
|
|
||||||
let dirs = wiretype.directions();
|
|
||||||
for d in dirs {
|
|
||||||
self.propagate_power(*d, d.step(pos));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Tile::Print => {
|
*state = true;
|
||||||
let sample = self.board.get_or_blank(front_pos);
|
match tile {
|
||||||
if let Tile::Marble { value, dir: _ } = sample {
|
PTile::Wire(wiretype) => {
|
||||||
self.output.push(value as u8);
|
let dirs = wiretype.directions();
|
||||||
|
for d in dirs {
|
||||||
|
self.propagate_power(*d, d.step(pos));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
PTile::Output => {
|
||||||
Tile::Bag => {
|
let sample = self.board.get_or_blank(front_pos);
|
||||||
if let Some(Tile::Blank) = self.board.get(front_pos) {
|
if let Tile::Marble { value, dir: _ } = sample {
|
||||||
*self.board.get_mut(front_pos) = Tile::Marble { value: 0, dir };
|
self.output.push(value as u8);
|
||||||
self.marbles.push(front_pos);
|
}
|
||||||
}
|
}
|
||||||
}
|
PTile::Bag => {
|
||||||
Tile::Input => {
|
if let Some(Tile::Blank) = self.board.get(front_pos) {
|
||||||
if self.input_index < self.input.len()
|
*self.board.get_mut(front_pos) = Tile::Marble { value: 0, dir };
|
||||||
&& self.board.get_or_blank(front_pos).is_blank()
|
self.marbles.push(front_pos);
|
||||||
{
|
}
|
||||||
let value = self.input[self.input_index] as MarbleValue;
|
|
||||||
*self.board.get_mut(front_pos) = Tile::Marble { value, dir };
|
|
||||||
self.marbles.push(front_pos);
|
|
||||||
self.input_index += 1;
|
|
||||||
}
|
}
|
||||||
}
|
PTile::Input => {
|
||||||
Tile::Math(op) => {
|
if self.input_index < self.input.len()
|
||||||
let op = *op;
|
&& self.board.get_or_blank(front_pos).is_blank()
|
||||||
let pos_a = dir.left().step(pos);
|
{
|
||||||
let pos_b = dir.right().step(pos);
|
let value = self.input[self.input_index] as MarbleValue;
|
||||||
let val_a = self.board.get_or_blank(pos_a).read_value();
|
*self.board.get_mut(front_pos) = Tile::Marble { value, dir };
|
||||||
let val_b = self.board.get_or_blank(pos_b).read_value();
|
self.marbles.push(front_pos);
|
||||||
if (!self.board.get_or_blank(pos_a).is_blank()
|
self.input_index += 1;
|
||||||
|| !self.board.get_or_blank(pos_b).is_blank())
|
}
|
||||||
&& self.board.get_or_blank(front_pos).is_blank()
|
}
|
||||||
{
|
PTile::Math(op) => {
|
||||||
let result = match op {
|
let op = *op;
|
||||||
MathOp::Add => val_a.wrapping_add(val_b),
|
let pos_a = dir.left().step(pos);
|
||||||
MathOp::Sub => val_a.wrapping_sub(val_b),
|
let pos_b = dir.right().step(pos);
|
||||||
MathOp::Mul => val_a.wrapping_mul(val_b),
|
let val_a = self.board.get_or_blank(pos_a).read_value();
|
||||||
MathOp::Div => val_a.checked_div(val_b).unwrap_or_default(),
|
let val_b = self.board.get_or_blank(pos_b).read_value();
|
||||||
MathOp::Rem => val_a.checked_rem(val_b).unwrap_or_default(),
|
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()
|
||||||
|
{
|
||||||
|
let result = 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}");
|
||||||
|
*self.board.get_mut(front_pos) = Tile::Marble { value: result, dir };
|
||||||
|
self.marbles.push(front_pos);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
PTile::Flipper => {
|
||||||
|
let m = self.board.get_mut(front_pos);
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
};
|
};
|
||||||
// println!("{op:?} a:{val_a} b:{val_b}");
|
|
||||||
*self.board.get_mut(front_pos) = Tile::Marble { value: result, dir };
|
|
||||||
self.marbles.push(front_pos);
|
|
||||||
}
|
}
|
||||||
}
|
PTile::Gate(gate) => {
|
||||||
Tile::Flip => {
|
let gate = *gate;
|
||||||
let m = self.board.get_mut(front_pos);
|
let pos_a = dir.left().step(pos);
|
||||||
match m {
|
let pos_b = dir.right().step(pos);
|
||||||
Tile::Wire(wire_type, _) => {
|
let val_a = self.board.get_or_blank(pos_a).read_value();
|
||||||
*wire_type = match *wire_type {
|
let val_b = self.board.get_or_blank(pos_b).read_value();
|
||||||
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();
|
|
||||||
}
|
|
||||||
_ => (),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
Tile::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 {
|
let result = match gate {
|
||||||
GateType::LessThan => val_a < val_b,
|
GateType::LessThan => val_a < val_b,
|
||||||
GateType::GreaterThan => val_a > val_b,
|
GateType::GreaterThan => val_a > val_b,
|
||||||
GateType::Equal => val_a == val_b,
|
GateType::Equal => val_a == val_b,
|
||||||
GateType::NotEqual => val_a != val_b,
|
GateType::NotEqual => val_a != val_b,
|
||||||
};
|
};
|
||||||
if result {
|
if result {
|
||||||
self.propagate_power(dir, dir.step(pos));
|
self.propagate_power(dir, dir.step(pos));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Tile::Marble { value: _, dir: _ }
|
|
||||||
| Tile::Trigger(_)
|
|
||||||
| Tile::Digit(_)
|
|
||||||
| Tile::Mirror(_)
|
|
||||||
| Tile::Arrow(_)
|
|
||||||
| Tile::Comment(_)
|
|
||||||
| Tile::Blank => (),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -322,30 +317,30 @@ pub fn parse(source: &str) -> Board {
|
||||||
dir: Direction::Down,
|
dir: Direction::Down,
|
||||||
},
|
},
|
||||||
'*' => Tile::Trigger(false),
|
'*' => Tile::Trigger(false),
|
||||||
'-' => Tile::Wire(WireType::Horizontal, false),
|
'-' => Tile::Powerable(PTile::Wire(WireType::Horizontal), false),
|
||||||
'|' => Tile::Wire(WireType::Vertical, false),
|
'|' => Tile::Powerable(PTile::Wire(WireType::Vertical), false),
|
||||||
'+' => Tile::Wire(WireType::Cross, false),
|
'+' => Tile::Powerable(PTile::Wire(WireType::Cross), false),
|
||||||
'/' => Tile::Mirror(MirrorType::Forward),
|
'/' => Tile::Mirror(MirrorType::Forward),
|
||||||
'\\' => Tile::Mirror(MirrorType::Back),
|
'\\' => Tile::Mirror(MirrorType::Back),
|
||||||
'^' => Tile::Arrow(Direction::Up),
|
'^' => Tile::Arrow(Direction::Up),
|
||||||
'v' => Tile::Arrow(Direction::Down),
|
'v' => Tile::Arrow(Direction::Down),
|
||||||
'<' => Tile::Arrow(Direction::Left),
|
'<' => Tile::Arrow(Direction::Left),
|
||||||
'>' => Tile::Arrow(Direction::Right),
|
'>' => Tile::Arrow(Direction::Right),
|
||||||
'=' => Tile::Gate(GateType::Equal),
|
'=' => Tile::Powerable(PTile::Gate(GateType::Equal), false),
|
||||||
'!' => Tile::Gate(GateType::NotEqual),
|
'!' => Tile::Powerable(PTile::Gate(GateType::NotEqual), false),
|
||||||
'L' => Tile::Gate(GateType::LessThan),
|
'L' => Tile::Powerable(PTile::Gate(GateType::LessThan), false),
|
||||||
'G' => Tile::Gate(GateType::GreaterThan),
|
'G' => Tile::Powerable(PTile::Gate(GateType::GreaterThan), false),
|
||||||
'P' => Tile::Print,
|
'P' => Tile::Powerable(PTile::Output, false),
|
||||||
'I' => Tile::Input,
|
'I' => Tile::Powerable(PTile::Input, false),
|
||||||
'F' => Tile::Flip,
|
'F' => Tile::Powerable(PTile::Flipper, false),
|
||||||
'A' => Tile::Math(MathOp::Add),
|
'A' => Tile::Powerable(PTile::Math(MathOp::Add), false),
|
||||||
'S' => Tile::Math(MathOp::Sub),
|
'S' => Tile::Powerable(PTile::Math(MathOp::Sub), false),
|
||||||
'M' => Tile::Math(MathOp::Mul),
|
'M' => Tile::Powerable(PTile::Math(MathOp::Mul), false),
|
||||||
'D' => Tile::Math(MathOp::Div),
|
'D' => Tile::Powerable(PTile::Math(MathOp::Div), false),
|
||||||
'R' => Tile::Math(MathOp::Rem),
|
'R' => Tile::Powerable(PTile::Math(MathOp::Rem), false),
|
||||||
'B' => Tile::Bag,
|
'B' => Tile::Powerable(PTile::Bag, false),
|
||||||
d @ '0'..='9' => Tile::Digit(d as u8),
|
d @ '0'..='9' => Tile::Digit(d as u8),
|
||||||
'#' => Tile::Comment(b'#'),
|
'#' => Tile::Block,
|
||||||
' ' => Tile::Blank,
|
' ' => Tile::Blank,
|
||||||
'(' => {
|
'(' => {
|
||||||
in_comment = true;
|
in_comment = true;
|
||||||
|
|
|
@ -13,22 +13,28 @@ pub type MarbleValue = u32;
|
||||||
pub enum Tile {
|
pub enum Tile {
|
||||||
#[default]
|
#[default]
|
||||||
Blank,
|
Blank,
|
||||||
|
Block,
|
||||||
Comment(u8),
|
Comment(u8),
|
||||||
Bag,
|
|
||||||
Marble {
|
Marble {
|
||||||
value: MarbleValue,
|
value: MarbleValue,
|
||||||
dir: Direction,
|
dir: Direction,
|
||||||
},
|
},
|
||||||
Trigger(bool),
|
Trigger(bool),
|
||||||
Digit(u8),
|
Digit(u8),
|
||||||
Wire(WireType, bool),
|
|
||||||
Gate(GateType),
|
|
||||||
Print,
|
|
||||||
Input,
|
|
||||||
Flip,
|
|
||||||
Math(MathOp),
|
|
||||||
Mirror(MirrorType),
|
Mirror(MirrorType),
|
||||||
Arrow(Direction),
|
Arrow(Direction),
|
||||||
|
Powerable(PTile, bool),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub enum PTile {
|
||||||
|
Wire(WireType),
|
||||||
|
Gate(GateType),
|
||||||
|
Math(MathOp),
|
||||||
|
Bag,
|
||||||
|
Flipper,
|
||||||
|
Input,
|
||||||
|
Output,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
@ -89,16 +95,17 @@ impl Tile {
|
||||||
let right = x + size / 2 - 1;
|
let right = x + size / 2 - 1;
|
||||||
match self {
|
match self {
|
||||||
Tile::Blank => (),
|
Tile::Blank => (),
|
||||||
|
Tile::Block => d.draw_rectangle(x - size / 2, y - size / 2, size, size, Color::DIMGRAY),
|
||||||
Tile::Comment(c) => {
|
Tile::Comment(c) => {
|
||||||
d.draw_text(&format!("{}", *c as char), x - 10, y - 10, 20, Color::RED)
|
d.draw_rectangle(x-size/2, y-size/2, size, size, Color::DIMGRAY);
|
||||||
|
d.draw_text(&format!("{}", *c as char), x - 10, y - 10, 20, Color::WHITE);
|
||||||
}
|
}
|
||||||
Tile::Bag => d.draw_circle_lines(x, y, size as f32 * 0.4, Color::CYAN),
|
|
||||||
Tile::Marble { value, dir } => {
|
Tile::Marble { value, dir } => {
|
||||||
d.draw_circle(x, y, size as f32 * 0.35, Color::new(15, 15, 15, 255));
|
d.draw_circle(x, y, size as f32 * 0.35, Color::new(15, 15, 15, 255));
|
||||||
d.draw_text(
|
d.draw_text(
|
||||||
&format!("{value}"),
|
&format!("{value}"),
|
||||||
x - size / 2,
|
x - size / 2 + 2,
|
||||||
y - size / 2,
|
y - size / 2 + 2,
|
||||||
20,
|
20,
|
||||||
Color::MAGENTA,
|
Color::MAGENTA,
|
||||||
);
|
);
|
||||||
|
@ -110,24 +117,6 @@ impl Tile {
|
||||||
Tile::Digit(n) => {
|
Tile::Digit(n) => {
|
||||||
d.draw_text(&String::from(*n as char), x - 10, y - 10, 20, Color::ORANGE)
|
d.draw_text(&String::from(*n as char), x - 10, y - 10, 20, Color::ORANGE)
|
||||||
}
|
}
|
||||||
Tile::Wire(wire, state) => {
|
|
||||||
let color = if *state { Color::RED } else { Color::WHITE };
|
|
||||||
let vertical = !matches!(wire, WireType::Horizontal);
|
|
||||||
let horizontal = !matches!(wire, WireType::Vertical);
|
|
||||||
if vertical {
|
|
||||||
// d.draw_line(x, up, x, down, color);
|
|
||||||
d.draw_rectangle(x - size / 8, y - size / 2, size / 4, size, color)
|
|
||||||
}
|
|
||||||
if horizontal {
|
|
||||||
// d.draw_line(left, y, right, y, color);
|
|
||||||
d.draw_rectangle(x - size / 2, y - size / 8, size, size / 4, color)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Tile::Gate(_) => todo!(),
|
|
||||||
// Tile::Print => todo!(),
|
|
||||||
// Tile::Input => todo!(),
|
|
||||||
// Tile::Flip => todo!(),
|
|
||||||
// Tile::Math(_) => todo!(),
|
|
||||||
Tile::Mirror(mirror) => {
|
Tile::Mirror(mirror) => {
|
||||||
let height = size as f32 * 1.25;
|
let height = size as f32 * 1.25;
|
||||||
let width = (size / 4) as f32;
|
let width = (size / 4) as f32;
|
||||||
|
@ -156,7 +145,32 @@ impl Tile {
|
||||||
};
|
};
|
||||||
d.draw_triangle(v1, v2, v3, Color::CYAN);
|
d.draw_triangle(v1, v2, v3, Color::CYAN);
|
||||||
}
|
}
|
||||||
_ => d.draw_rectangle(x - size / 2, y - size / 2, size, size, Color::YELLOW),
|
Tile::Powerable(tile, state) => {
|
||||||
|
let color = if *state { Color::RED } else { Color::LIGHTGRAY };
|
||||||
|
match tile {
|
||||||
|
PTile::Bag => {
|
||||||
|
d.draw_circle(x, y, size as f32 * 0.4, color);
|
||||||
|
d.draw_circle(x, y, size as f32 * 0.2, Color::GRAY);
|
||||||
|
}
|
||||||
|
PTile::Wire(wire) => {
|
||||||
|
let vertical = !matches!(wire, WireType::Horizontal);
|
||||||
|
let horizontal = !matches!(wire, WireType::Vertical);
|
||||||
|
if vertical {
|
||||||
|
d.draw_rectangle(x - size / 8, y - size / 2, size / 4, size, color)
|
||||||
|
}
|
||||||
|
if horizontal {
|
||||||
|
d.draw_rectangle(x - size / 2, y - size / 8, size, size / 4, color)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// PTile::Gate(_) => todo!(),
|
||||||
|
// PTile::Math(_) => todo!(),
|
||||||
|
// PTile::Print => todo!(),
|
||||||
|
// PTile::Input => todo!(),
|
||||||
|
// PTile::Flip => todo!(),
|
||||||
|
_ => d.draw_rectangle(x - size / 2, y - size / 2, size, size, color),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue