diff --git a/src/forestry.rs b/src/forestry.rs index 1f3f629..e242473 100644 --- a/src/forestry.rs +++ b/src/forestry.rs @@ -1,21 +1,21 @@ -use std::collections::{VecDeque, HashMap}; +use std::collections::VecDeque; use crate::state::{Land, Rule, Tree}; -fn extract_forestry_format(instruction: &u16) -> (u16, u16, u16) { +fn extract_forestry_format(instruction: &u16) -> (u16, i16, i16) { let stack_code = (instruction & 0b00000_1_0000000000) >> 10; let relative_x = (instruction & 0b000000_11111_00000) >> 5; let relative_y = instruction & 0b000000_00000_11111; let relative_x = if relative_x >> 9 == 0 { - relative_x + relative_x as i16 } else { - relative_x | 0b1111111111100000 + (relative_x | 0b1111111111100000) as i16 }; let relative_y = if relative_y >> 4 == 0 { - relative_y + relative_y as i16 } else { - relative_y | 0b1111111111100000 + (relative_y | 0b1111111111100000) as i16 }; return ( @@ -25,18 +25,17 @@ fn extract_forestry_format(instruction: &u16) -> (u16, u16, u16) { ); } -pub fn op_sow(instruction: &u16, tree: &mut Tree, land: &mut Land) { +pub fn op_sow(instruction: &u16, tree: &mut Tree) { let (stack_code, relative_x, relative_y) = extract_forestry_format(instruction); let value = tree.pull_by_stack_code(stack_code); - - land.sow(tree.position.0 + relative_x, tree.position.1 + relative_y, value); + tree.sow(relative_x, relative_y, value); } -pub fn op_reap(instruction: &u16, tree: &mut Tree, land: &mut Land) { +pub fn op_reap(instruction: &u16, tree: &mut Tree) { let (stack_code, relative_x, relative_y) = extract_forestry_format(instruction); - let value = land.reap(tree.position.0 + relative_x, tree.position.1 + relative_y); + let value = tree.reap(relative_x, relative_y); if stack_code == 0 { tree.push_root(value); @@ -66,7 +65,7 @@ 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: land.derelativize(tree.position, relative_x, relative_y), span: [[0u16; 8]; 8], // TODO: should take from current state of land! }); @@ -76,7 +75,7 @@ pub fn op_plant(instruction: &u16, tree: &mut Tree, next_tree_id: &mut u16, tree pub fn op_replant(instruction: &u16, tree: &mut Tree, land: &Land) { let (_, relative_x, relative_y) = extract_forestry_format(instruction); - tree.position = land.derelativize(tree, relative_x, relative_y); + tree.position = land.derelativize(tree.position, relative_x, relative_y); } pub fn op_define(instruction: &u16, tree: &mut Tree, rules: &Vec, rules_to_define: &mut Vec) { @@ -84,7 +83,7 @@ pub fn op_define(instruction: &u16, tree: &mut Tree, rules: &Vec, rules_to let instruction_count = tree.pull_by_stack_code(stack_code); let mut new_rule = VecDeque::with_capacity(instruction_count as usize); - for i in 0..instruction_count { + for _ in 0..instruction_count { new_rule.push_back(tree.pull_by_stack_code(stack_code)); } @@ -97,6 +96,7 @@ pub fn op_define(instruction: &u16, tree: &mut Tree, rules: &Vec, rules_to tree.push_by_stack_code(stack_code, (rules.len() + rules_to_define.len() - 2) as u16) } +/* pub fn op_wait(instruction: &u16, tree: &mut Tree, land: &mut Land, instruction_pointer: &u16) { let (_, relative_x, relative_y) = extract_forestry_format(instruction); let (abs_x, abs_y) = land.derelativize(tree, relative_x, relative_y); @@ -138,3 +138,4 @@ pub fn op_signal(instruction: &u16, tree: &mut Tree, trees: &mut HashMap