support multiple stages for each level, resetting the machine between

This commit is contained in:
Crispy 2024-12-18 21:31:05 +01:00
parent e6437ae9cf
commit 43130b665d
17 changed files with 220 additions and 126 deletions

View file

@ -4,6 +4,8 @@
logic mostly like https://git.crispypin.cc/CrispyPin/marble logic mostly like https://git.crispypin.cc/CrispyPin/marble
## todo ## todo
- show level info in editor
- comments
- accessibility - accessibility
- background colour setting - background colour setting
- hotkeys for everything - hotkeys for everything
@ -11,9 +13,7 @@ logic mostly like https://git.crispypin.cc/CrispyPin/marble
- make direct power (comparator -> machine) work, (needs storing power direction in machine tiles) - make direct power (comparator -> machine) work, (needs storing power direction in machine tiles)
- cut selections, copy to system clipboard - cut selections, copy to system clipboard
- timestamps in solutions and blueprints - timestamps in solutions and blueprints
- multiple input/output sets
- tooltips - tooltips
- show level info in editor
- lock tile types for early levels to make it less overwhelming - lock tile types for early levels to make it less overwhelming
- display tool variant more clearly (it's not obvious there are more states) - display tool variant more clearly (it's not obvious there are more states)
- option to use 8-bit marbles? - option to use 8-bit marbles?
@ -60,9 +60,11 @@ logic mostly like https://git.crispypin.cc/CrispyPin/marble
"sortorder": 5, "sortorder": 5,
"name": "Zeroes", "name": "Zeroes",
"description": "learn how to output data", "description": "learn how to output data",
"init_board": null, "init_board": "<optional, serialized board>",
"inputs": [], "stages": [{
"outputs": [0, 0, 0, 0, 0, 0, 0, 0], "input": [],
"output": [0, 0, 0, 0, 0, 0, 0, 0],
}]
} }
``` ```
### solution ### solution

View file

@ -4,6 +4,12 @@
"name": "Zero", "name": "Zero",
"description": "learn how to output data", "description": "learn how to output data",
"init_board": "\n o \n\n I\n\n", "init_board": "\n o \n\n I\n\n",
"inputs": [], "stages": [
"outputs": [0] {
"input": [],
"output": [
0
]
}
]
} }

View file

@ -3,7 +3,8 @@
"sort_order": 12, "sort_order": 12,
"name": "Digits", "name": "Digits",
"description": "place digits and use number keys to assign them values", "description": "place digits and use number keys to assign them values",
"init_board": null, "stages": [{
"inputs": [], "input": [],
"outputs": [4, 8, 16] "output": [4, 8, 16]
}]
} }

View file

@ -4,6 +4,8 @@
"name": "Loop", "name": "Loop",
"description": "repeated output", "description": "repeated output",
"init_board": "\n \n o\n\n\n\n ^ \n\n", "init_board": "\n \n o\n\n\n\n ^ \n\n",
"inputs": [], "stages": [{
"outputs": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] "input": [],
"output": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
}]
} }

View file

@ -3,7 +3,8 @@
"sort_order": 14, "sort_order": 14,
"name": "Copy Cat", "name": "Copy Cat",
"description": "read input and output the same thing", "description": "read input and output the same thing",
"init_board": null, "stages": [{
"inputs": "Hello, world!", "input": "Hello, world!",
"outputs": "Hello, world!" "output": "Hello, world!"
}]
} }

View file

@ -3,7 +3,8 @@
"sort_order": 15, "sort_order": 15,
"name": "Odd Cat", "name": "Odd Cat",
"description": "copy only the odd numbers from the input", "description": "copy only the odd numbers from the input",
"init_board": null, "stages": [{
"inputs": [112, 92, 51, 79, 112, 96, 84, 59, 195, 208, 137, 196, 68, 204, 82, 148, 251, 56, 105, 38, 63, 204, 240, 220, 180, 54, 211, 17, 82, 17, 181, 43], "input": [112, 92, 51, 79, 112, 96, 84, 59, 195, 208, 137, 196, 68, 204, 82, 148, 251, 56, 105, 38, 63, 204, 240, 220, 180, 54, 211, 17, 82, 17, 181, 43],
"outputs": [51, 79, 59, 195, 137, 251, 105, 63, 211, 17, 17, 181, 43] "output": [51, 79, 59, 195, 137, 251, 105, 63, 211, 17, 17, 181, 43]
}]
} }

View file

@ -3,7 +3,8 @@
"sort_order": 23, "sort_order": 23,
"name": "Fives", "name": "Fives",
"description": "count how many fives are in the input", "description": "count how many fives are in the input",
"init_board": null, "stages": [{
"inputs": [182, 236, 71, 5, 5, 242, 29, 99, 19, 230, 217, 5, 67, 5, 223, 224, 70, 243, 3, 74, 242, 5, 171, 31, 96, 5, 169, 70, 5, 163, 72, 5, 172, 148, 5, 208, 28, 220, 17, 184, 172, 238, 5, 105, 119, 5, 106, 100, 73, 53, 42, 221, 155, 5, 74, 100, 161, 36, 16, 239, 193, 164, 64, 162, 222, 155, 107, 14, 45, 52, 159, 31, 199, 124, 129, 0], "input": [182, 236, 71, 5, 5, 242, 29, 99, 19, 230, 217, 5, 67, 5, 223, 224, 70, 243, 3, 74, 242, 5, 171, 31, 96, 5, 169, 70, 5, 163, 72, 5, 172, 148, 5, 208, 28, 220, 17, 184, 172, 238, 5, 105, 119, 5, 106, 100, 73, 53, 42, 221, 155, 5, 74, 100, 161, 36, 16, 239, 193, 164, 64, 162, 222, 155, 107, 14, 45, 52, 159, 31, 199, 124, 129, 0],
"outputs": [12] "output": [12]
}]
} }

View file

@ -3,7 +3,8 @@
"sort_order": 22, "sort_order": 22,
"name": "Length", "name": "Length",
"description": "count how many numbers are in the input, until the first zero", "description": "count how many numbers are in the input, until the first zero",
"init_board": null, "stages": [{
"inputs": [182, 236, 71, 5, 5, 242, 29, 99, 19, 230, 217, 5, 67, 5, 223, 224, 70, 243, 3, 74, 242, 5, 171, 31, 96, 5, 169, 70, 5, 163, 72, 5, 172, 148, 5, 208, 28, 220, 17, 184, 172, 0], "input": [182, 236, 71, 5, 5, 242, 29, 99, 19, 230, 217, 5, 67, 5, 223, 224, 70, 243, 3, 74, 242, 5, 171, 31, 96, 5, 169, 70, 5, 163, 72, 5, 172, 148, 5, 208, 28, 220, 17, 184, 172, 0],
"outputs": [41] "output": [41]
}]
} }

View file

@ -3,7 +3,8 @@
"sort_order": 21, "sort_order": 21,
"name": "Null Separation", "name": "Null Separation",
"description": "output everything after the first zero in the input data", "description": "output everything after the first zero in the input data",
"init_board": null, "stages": [{
"inputs": "9834726\u0000Hello, worlg!", "input": "9834726\u0000Hello, worlg!",
"outputs": "Hello, worlg!" "output": "Hello, worlg!"
}]
} }

View file

@ -3,7 +3,8 @@
"sort_order": 24, "sort_order": 24,
"name": "Reverse", "name": "Reverse",
"description": "read input until zero and output the same thing in reverse", "description": "read input until zero and output the same thing in reverse",
"init_board": null, "stages": [{
"inputs": "tnropmi yrev\u0000", "input": "tnropmi yrev\u0000",
"outputs": "very impornt" "output": "very impornt"
}]
} }

View file

@ -3,7 +3,8 @@
"sort_order": 35, "sort_order": 35,
"name": "Lowercase", "name": "Lowercase",
"description": "Convert text to lowercase", "description": "Convert text to lowercase",
"init_board": null, "stages": [{
"inputs": "I CraVeD tHE strEnGTH AND CerTAinTy oF STeeL", "input": "I CraVeD tHE strEnGTH AND CerTAinTy oF STeeL",
"outputs": "i craved the strength and certainty of steel" "output": "i craved the strength and certainty of steel"
}]
} }

View file

@ -3,7 +3,8 @@
"sort_order": 31, "sort_order": 31,
"name": "Numbers", "name": "Numbers",
"description": "Convert input numbers to text, separated by spaces (32)\n'0' = 48, '1' = 49, '2' = 50, and so on", "description": "Convert input numbers to text, separated by spaces (32)\n'0' = 48, '1' = 49, '2' = 50, and so on",
"init_board": null, "stages": [{
"inputs": [85, 114, 32, 103, 97, 121, 58, 51], "input": [85, 114, 32, 103, 97, 121, 58, 51],
"outputs": "85 114 32 103 97 121 58 51" "output": "85 114 32 103 97 121 58 51"
}]
} }

View file

@ -3,7 +3,8 @@
"sort_order": 33, "sort_order": 33,
"name": "Numbers 2", "name": "Numbers 2",
"description": "Convert input numbers from text, separated by spaces (32)\n'0' = 48, '1' = 49, '2' = 50, and so on", "description": "Convert input numbers from text, separated by spaces (32)\n'0' = 48, '1' = 49, '2' = 50, and so on",
"init_board": null, "stages": [{
"inputs": "85 114 32 103 97 121 58 51", "input": "85 114 32 103 97 121 58 51",
"outputs": [85, 114, 32, 103, 97, 121, 58, 51] "output": [85, 114, 32, 103, 97, 121, 58, 51]
}]
} }

View file

@ -2,9 +2,5 @@
"id": "sandbox", "id": "sandbox",
"sort_order": 100000, "sort_order": 100000,
"name": "Sandbox", "name": "Sandbox",
"description": "make whatever you want here", "description": "make whatever you want here"
"is_sandbox": true,
"init_board": null,
"inputs": "",
"outputs": []
} }

View file

@ -54,24 +54,29 @@ pub struct Editor {
tool_mirror: MirrorType, tool_mirror: MirrorType,
tool_wire: WireType, tool_wire: WireType,
input_text_selected: bool, input_text_selected: bool,
stage: Option<usize>,
new_blueprint_name: String, new_blueprint_name: String,
blueprint_name_selected: bool, blueprint_name_selected: bool,
sim_speed: u8, sim_speed: u8,
time_since_step: f32, time_since_step: f32,
exit_state: ExitState, exit_state: ExitState,
exit_menu: bool, exit_menu: bool,
total_steps: usize,
popup: EndPopup, popup: EndPopup,
score: Option<Score>, score: Option<Score>,
blueprints: Vec<Blueprint>, blueprints: Vec<Blueprint>,
selected_blueprint: usize, selected_blueprint: usize,
blueprint_scroll: usize, blueprint_scroll: usize,
step_time: u128,
max_step_time: u128,
start_time: Instant,
pasting_board: Option<Board>, pasting_board: Option<Board>,
/// draw grid, directions and values of marbles
draw_overlay: bool, draw_overlay: bool,
undo_history: Vec<Action>, undo_history: Vec<Action>,
undo_index: usize, undo_index: usize,
// debug/profiling
step_time: u128,
max_step_time: u128,
start_time: Instant,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -124,16 +129,28 @@ pub enum ExitState {
impl Editor { impl Editor {
pub fn new(solution: Solution, level: Level) -> Self { pub fn new(solution: Solution, level: Level) -> Self {
let mut output_as_text = true;
let mut input_as_text = true;
let mut stage = None;
let mut machine = Machine::new_empty();
if let Some(i) = level.stages().first() {
stage = Some(0);
output_as_text = i.output().is_text();
input_as_text = i.input().is_text();
machine.set_input(i.input().as_bytes().to_owned());
}
Self { Self {
source_board: Board::parse(&solution.board), source_board: Board::parse(&solution.board),
machine: Machine::new_empty(level.inputs().to_owned(), 1), machine,
sim_state: SimState::Editing, sim_state: SimState::Editing,
view_offset: Vector2::zero(), view_offset: Vector2::zero(),
zoom: 1., zoom: 1.,
active_tool: Tool::None, active_tool: Tool::None,
output_as_text: level.output_is_text(), output_as_text,
input_as_text: level.input_is_text(), input_as_text,
input_text_selected: false, input_text_selected: false,
stage,
new_blueprint_name: String::new(), new_blueprint_name: String::new(),
blueprint_name_selected: false, blueprint_name_selected: false,
sim_speed: 3, sim_speed: 3,
@ -148,6 +165,7 @@ impl Editor {
exit_menu: false, exit_menu: false,
popup: EndPopup::None, popup: EndPopup::None,
score: solution.score, score: solution.score,
total_steps: 0,
blueprints: get_blueprints(), blueprints: get_blueprints(),
selected_blueprint: usize::MAX, selected_blueprint: usize::MAX,
blueprint_scroll: 0, blueprint_scroll: 0,
@ -244,17 +262,29 @@ impl Editor {
pos * TILE_TEXTURE_SIZE * self.zoom + self.view_offset pos * TILE_TEXTURE_SIZE * self.zoom + self.view_offset
} }
fn start_sim(&mut self) { fn init_sim(&mut self) {
self.max_step_time = 0; self.max_step_time = 0;
self.total_steps = 0;
self.start_time = Instant::now(); self.start_time = Instant::now();
if !self.level.is_sandbox() {
self.stage = Some(0);
}
self.reset_machine();
}
fn reset_machine(&mut self) {
self.machine.reset(); self.machine.reset();
self.machine.set_board(self.source_board.clone()); self.machine.set_board(self.source_board.clone());
if let Some(i) = self.stage {
let bytes = self.level.stages()[i].input().as_bytes();
self.machine.set_input(bytes.to_owned());
}
} }
fn step_pressed(&mut self) { fn step_pressed(&mut self) {
match self.sim_state { match self.sim_state {
SimState::Editing => { SimState::Editing => {
self.start_sim(); self.init_sim();
// self.step(); // self.step();
} }
SimState::Running => (), SimState::Running => (),
@ -266,22 +296,31 @@ impl Editor {
fn step(&mut self) { fn step(&mut self) {
self.machine.step(); self.machine.step();
if self.popup != EndPopup::None { if let Some(i) = self.stage {
self.popup = EndPopup::Dismissed; if self.popup != EndPopup::None {
} self.popup = EndPopup::Dismissed;
if !self.level.outputs().is_empty() && self.popup == EndPopup::None { }
if self.level.outputs() == self.machine.output() { let stage = &self.level.stages()[i];
self.popup = EndPopup::Success; if self.popup == EndPopup::None {
println!("completed in {:?}", self.start_time.elapsed()); if stage.output().as_bytes() == self.machine.output() {
self.exit_state = ExitState::Save; if i < self.level.stages().len() {
self.sim_state = SimState::Stepping; self.stage = Some(i + 1);
self.score = Some(Score { self.total_steps += self.machine.step_count();
cycles: self.machine.step_count(), self.reset_machine();
tiles: self.source_board.count_tiles(), } else {
}); self.popup = EndPopup::Success;
} else if !self.level.outputs().starts_with(self.machine.output()) { println!("completed in {:?}", self.start_time.elapsed());
self.popup = EndPopup::Failure; self.exit_state = ExitState::Save;
self.sim_state = SimState::Stepping; self.sim_state = SimState::Stepping;
self.score = Some(Score {
cycles: self.machine.step_count(),
tiles: self.source_board.count_tiles(),
});
}
} else if !stage.output().as_bytes().starts_with(self.machine.output()) {
self.popup = EndPopup::Failure;
self.sim_state = SimState::Stepping;
}
} }
} }
} }
@ -439,7 +478,7 @@ impl Editor {
if rl.is_key_pressed(KeyboardKey::KEY_ENTER) { if rl.is_key_pressed(KeyboardKey::KEY_ENTER) {
match self.sim_state { match self.sim_state {
SimState::Editing => { SimState::Editing => {
self.start_sim(); self.init_sim();
self.sim_state = SimState::Running; self.sim_state = SimState::Running;
} }
SimState::Running => { SimState::Running => {
@ -654,32 +693,33 @@ impl Editor {
d.draw_text("save", 90, 10, 20, Color::WHITE); d.draw_text("save", 90, 10, 20, Color::WHITE);
} }
if simple_button(d, 150, 4, 32, 32) { if self.sim_state == SimState::Editing {
self.undo() if simple_button(d, 150, 4, 32, 32) {
} self.undo()
let undo_icon = if self.undo_index > 0 { }
"undo" let undo_icon = if self.undo_index > 0 {
} else { "undo"
"undo_disabled" } else {
}; "undo_disabled"
draw_scaled_texture(d, textures.get(undo_icon), 150, 4, 2.); };
draw_scaled_texture(d, textures.get(undo_icon), 150, 4, 2.);
if simple_button(d, 186, 4, 32, 32) { if simple_button(d, 186, 4, 32, 32) {
self.redo() self.redo()
}
let redo_icon = if self.undo_index < self.undo_history.len() {
"redo"
} else {
"redo_disabled"
};
draw_scaled_texture(d, textures.get(redo_icon), 186, 4, 2.);
} }
let redo_icon = if self.undo_index < self.undo_history.len() {
"redo"
} else {
"redo_disabled"
};
draw_scaled_texture(d, textures.get(redo_icon), 186, 4, 2.);
simple_toggle_button(d, &mut self.draw_overlay, 223, 4, 32, 32, 4); simple_toggle_button(d, &mut self.draw_overlay, 223, 4, 32, 32, 4);
match self.sim_state { match self.sim_state {
SimState::Editing => { SimState::Editing => {
if simple_button(d, 260, 4, 32, 32) { if simple_button(d, 260, 4, 32, 32) {
self.start_sim(); self.init_sim();
self.sim_state = SimState::Running; self.sim_state = SimState::Running;
} }
draw_scaled_texture(d, textures.get("play"), 260, 4, 2.); draw_scaled_texture(d, textures.get("play"), 260, 4, 2.);
@ -716,9 +756,20 @@ impl Editor {
slider(d, &mut self.sim_speed, 0, MAX_SPEED_POWER, 368, 24, 48, 12); slider(d, &mut self.sim_speed, 0, MAX_SPEED_POWER, 368, 24, 48, 12);
draw_usize(d, textures, self.machine.step_count(), 420, 4, 9, 2); draw_usize(d, textures, self.machine.step_count(), 420, 4, 9, 2);
if self.stage > Some(0) {
draw_usize(
d,
textures,
self.total_steps + self.machine.step_count(),
420,
44,
9,
2,
);
}
draw_usize(d, textures, self.step_time as usize, 540, 42, 9, 1); draw_usize(d, textures, self.step_time as usize, 260, 42, 9, 1);
draw_usize(d, textures, self.max_step_time as usize, 540, 60, 9, 1); draw_usize(d, textures, self.max_step_time as usize, 260, 60, 9, 1);
d.draw_text("input:", 603, 8, 10, Color::WHITE); d.draw_text("input:", 603, 8, 10, Color::WHITE);
if simple_button(d, 600, 20, 35, 15) { if simple_button(d, 600, 20, 35, 15) {
@ -936,32 +987,35 @@ impl Editor {
for (box_index, index) in (output_start..output_end).enumerate() { for (box_index, index) in (output_start..output_end).enumerate() {
let x = output_x + output_cell_width * box_index as i32; let x = output_x + output_cell_width * box_index as i32;
let expected_byte = self.level.outputs().get(index); let (mut top_color, mut bottom_color) = (BG_LIGHT, BG_MEDIUM);
let real_byte = self.machine.output().get(index); let real_byte = self.machine.output().get(index);
let (top_color, bottom_color) = if let Some(stage_index) = self.stage {
let stage = &self.level.stages()[stage_index];
let expected_byte = stage.output().as_bytes().get(index);
if let (Some(&real_byte), Some(&expected_byte)) = (real_byte, expected_byte) { if let (Some(&real_byte), Some(&expected_byte)) = (real_byte, expected_byte) {
if expected_byte == real_byte { (top_color, bottom_color) = if expected_byte == real_byte {
(Color::GREEN, Color::DARKGREEN) (Color::GREEN, Color::DARKGREEN)
} else { } else {
(Color::RED, Color::DARKRED) (Color::RED, Color::DARKRED)
} };
} else { }
(BG_LIGHT, BG_MEDIUM) d.draw_rectangle(x, y, output_cell_width - 5, 30, top_color);
}; if let Some(&expected_byte) = expected_byte {
let top_text = if self.output_as_text
d.draw_rectangle(x, y, output_cell_width - 5, 30, top_color); && (expected_byte.is_ascii_graphic() || expected_byte.is_ascii_whitespace())
d.draw_rectangle(x, y + 35, output_cell_width - 5, 30, bottom_color); {
if let Some(&expected_byte) = expected_byte { format!("{:?}", expected_byte as char)
let top_text = if self.output_as_text } else {
&& (expected_byte.is_ascii_graphic() || expected_byte.is_ascii_whitespace()) format!("{expected_byte}")
{ };
format!("{:?}", expected_byte as char) d.draw_text(&top_text, x + 2, y + 5, 20, Color::WHITE);
} else { }
format!("{expected_byte}")
};
d.draw_text(&top_text, x + 2, y + 5, 20, Color::WHITE);
} }
d.draw_rectangle(x, y + 35, output_cell_width - 5, 30, bottom_color);
if let Some(&real_byte) = real_byte { if let Some(&real_byte) = real_byte {
let bottom_text = if self.output_as_text let bottom_text = if self.output_as_text
&& (real_byte.is_ascii_graphic() || real_byte.is_ascii_whitespace()) && (real_byte.is_ascii_graphic() || real_byte.is_ascii_whitespace())

View file

@ -7,10 +7,16 @@ pub struct Level {
name: String, name: String,
description: String, description: String,
#[serde(default)] #[serde(default)]
is_sandbox: bool,
init_board: Option<String>, init_board: Option<String>,
inputs: IOData, /// no stages means sandbox
outputs: IOData, #[serde(default)]
stages: Vec<Stage>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct Stage {
input: IOData,
output: IOData,
} }
#[derive(Debug, Clone, Deserialize)] #[derive(Debug, Clone, Deserialize)]
@ -27,6 +33,10 @@ impl IOData {
IOData::Text(t) => t.as_bytes(), IOData::Text(t) => t.as_bytes(),
} }
} }
pub fn is_text(&self) -> bool {
matches!(self, IOData::Text(_))
}
} }
impl Level { impl Level {
@ -47,26 +57,40 @@ impl Level {
} }
pub fn is_sandbox(&self) -> bool { pub fn is_sandbox(&self) -> bool {
self.is_sandbox self.stages.is_empty()
} }
pub fn init_board(&self) -> Option<String> { pub fn init_board(&self) -> Option<String> {
self.init_board.clone() self.init_board.clone()
} }
pub fn inputs(&self) -> &[u8] { pub fn stages(&self) -> &[Stage] {
self.inputs.as_bytes() &self.stages
} }
pub fn outputs(&self) -> &[u8] { // pub fn inputs(&self) -> &[u8] {
self.outputs.as_bytes() // self.inputs.as_bytes()
// }
// pub fn outputs(&self) -> &[u8] {
// self.outputs.as_bytes()
// }
// pub fn input_is_text(&self) -> bool {
// matches!(self.inputs, IOData::Text(_))
// }
// pub fn output_is_text(&self) -> bool {
// matches!(self.outputs, IOData::Text(_))
// }
}
impl Stage {
pub fn input(&self) -> &IOData {
&self.input
} }
pub fn input_is_text(&self) -> bool { pub fn output(&self) -> &IOData {
matches!(self.inputs, IOData::Text(_)) &self.output
}
pub fn output_is_text(&self) -> bool {
matches!(self.outputs, IOData::Text(_))
} }
} }

View file

@ -21,12 +21,12 @@ pub struct Machine {
} }
impl Machine { impl Machine {
pub fn new_empty(input: Vec<u8>, width: usize) -> Self { pub fn new_empty() -> Self {
Self { Self {
board: Board::new_empty(width, width), board: Board::new_empty(5, 5),
marbles: Vec::new(), marbles: Vec::new(),
powered: Vec::new(), powered: Vec::new(),
input, input: Vec::new(),
input_index: 0, input_index: 0,
output: Vec::new(), output: Vec::new(),
steps: 0, steps: 0,