feat: use the span of a tree when sowing and reaping

This commit is contained in:
Nat 2024-12-04 14:39:31 -08:00
parent b597bffc0e
commit dee8a15a7a
1 changed files with 15 additions and 14 deletions

View File

@ -1,21 +1,21 @@
use std::collections::{VecDeque, HashMap}; use std::collections::VecDeque;
use crate::state::{Land, Rule, Tree}; 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 stack_code = (instruction & 0b00000_1_0000000000) >> 10;
let relative_x = (instruction & 0b000000_11111_00000) >> 5; let relative_x = (instruction & 0b000000_11111_00000) >> 5;
let relative_y = instruction & 0b000000_00000_11111; let relative_y = instruction & 0b000000_00000_11111;
let relative_x = if relative_x >> 9 == 0 { let relative_x = if relative_x >> 9 == 0 {
relative_x relative_x as i16
} else { } else {
relative_x | 0b1111111111100000 (relative_x | 0b1111111111100000) as i16
}; };
let relative_y = if relative_y >> 4 == 0 { let relative_y = if relative_y >> 4 == 0 {
relative_y relative_y as i16
} else { } else {
relative_y | 0b1111111111100000 (relative_y | 0b1111111111100000) as i16
}; };
return ( 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 (stack_code, relative_x, relative_y) = extract_forestry_format(instruction);
let value = tree.pull_by_stack_code(stack_code); let value = tree.pull_by_stack_code(stack_code);
tree.sow(relative_x, relative_y, value);
land.sow(tree.position.0 + relative_x, tree.position.1 + 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 (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 { if stack_code == 0 {
tree.push_root(value); 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), trunk: Vec::with_capacity((new_stack_size / 2) as usize),
asleep: false, asleep: false,
resume_from: 0, 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! 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) { pub fn op_replant(instruction: &u16, tree: &mut Tree, land: &Land) {
let (_, relative_x, relative_y) = extract_forestry_format(instruction); 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<Rule>, rules_to_define: &mut Vec<Rule>) { pub fn op_define(instruction: &u16, tree: &mut Tree, rules: &Vec<Rule>, rules_to_define: &mut Vec<Rule>) {
@ -84,7 +83,7 @@ pub fn op_define(instruction: &u16, tree: &mut Tree, rules: &Vec<Rule>, rules_to
let instruction_count = tree.pull_by_stack_code(stack_code); let instruction_count = tree.pull_by_stack_code(stack_code);
let mut new_rule = VecDeque::with_capacity(instruction_count as usize); 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)); 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<Rule>, rules_to
tree.push_by_stack_code(stack_code, (rules.len() + rules_to_define.len() - 2) as u16) 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) { 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 (_, relative_x, relative_y) = extract_forestry_format(instruction);
let (abs_x, abs_y) = land.derelativize(tree, relative_x, relative_y); 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<u16, Tr
tree.resume_from = 0; tree.resume_from = 0;
} }
} }
*/