From b597bffc0e9d052a9fd0fb1068b8f43c65e2c0fc Mon Sep 17 00:00:00 2001 From: nat Date: Tue, 3 Dec 2024 11:07:24 -0800 Subject: [PATCH] feat: implement span interface --- src/forestry.rs | 3 ++- src/main.rs | 3 ++- src/state.rs | 19 ++++++++++++++++++- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/forestry.rs b/src/forestry.rs index f00d359..1f3f629 100644 --- a/src/forestry.rs +++ b/src/forestry.rs @@ -66,7 +66,8 @@ pub fn op_plant(instruction: &u16, tree: &mut Tree, next_tree_id: &mut u16, tree trunk: Vec::with_capacity((new_stack_size / 2) as usize), asleep: false, resume_from: 0, - position: ((tree.position.0 + relative_x) % land.width, (tree.position.1 + relative_y) % land.height) + position: ((tree.position.0 + relative_x) % land.width, (tree.position.1 + relative_y) % land.height), + span: [[0u16; 8]; 8], // TODO: should take from current state of land! }); *next_tree_id += 1; diff --git a/src/main.rs b/src/main.rs index 776b7a4..7ff9be5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -103,7 +103,8 @@ fn parse_rom(path: &String) -> io::Result<(state::Land, Vec, HashMa asleep: false, resume_from: 0, stack_size: original_stack_size, - position: (seed_cell_x % land.width, seed_cell_y % land.height) + position: (seed_cell_x % land.width, seed_cell_y % land.height), + span: [[0u16; 8]; 8], }); Ok((land, rules, trees)) diff --git a/src/state.rs b/src/state.rs index 5330042..1e918cf 100644 --- a/src/state.rs +++ b/src/state.rs @@ -42,7 +42,12 @@ pub struct Tree { pub asleep: bool, pub resume_from: u16, pub stack_size: usize, - pub position: (u16, u16) + pub position: (u16, u16), + pub span: [[u16; 8]; 8], +} + +fn derelativize_span_coordinates(x: i16, y: i16) -> (usize, usize) { + return ((x + 4) as usize, (y + 4) as usize); } impl Tree { @@ -91,4 +96,16 @@ impl Tree { self.pull_trunk() } } + + pub fn sow(&mut self, relative_x: i16, relative_y: i16, value: u16) { + let (x, y) = derelativize_span_coordinates(relative_x, relative_y); + self.span[x][y] = value; + } + + pub fn reap(&mut self, relative_x: i16, relative_y: i16) -> u16 { + let (x, y) = derelativize_span_coordinates(relative_x, relative_y); + let value = self.span[x][y]; + self.sow(relative_x, relative_y, 0); + return value; + } }