diff --git a/src/falling_sand/rules.rs b/src/falling_sand/rules.rs index 94625c8..d7febc6 100644 --- a/src/falling_sand/rules.rs +++ b/src/falling_sand/rules.rs @@ -2,6 +2,37 @@ use bevy::{ecs::system::Resource, utils::HashMap}; use super::Element; +struct RuleConfig { + element_groups: Vec>, + rules: Vec<([usize; 4], [usize; 4])>, +} + +impl RuleConfig { + fn compile(&self) -> HashMap { + let mut rules = HashMap::new(); + for i in 0..self.rules.len() { + let rule = self.rules[i]; + + let i0 = &self.element_groups[rule.0[0]]; + let i1 = &self.element_groups[rule.0[1]]; + let i2 = &self.element_groups[rule.0[2]]; + let i3 = &self.element_groups[rule.0[3]]; + + let o0 = &self.element_groups[rule.1[0]]; + let o1 = &self.element_groups[rule.1[1]]; + let o2 = &self.element_groups[rule.1[2]]; + let o3 = &self.element_groups[rule.1[3]]; + + // !HACK: Assume element groups are length 1 for now! + let input = to_rule_state((i0[0], i1[0], i2[0], i3[0])); + let output = to_rule_state((o0[0], o1[0], o2[0], o3[0])); + rules.insert(input, output); + } + + rules + } +} + #[derive(Resource)] pub struct FallingSandRules { rules: HashMap, @@ -10,46 +41,23 @@ pub struct FallingSandRules { impl Default for FallingSandRules { fn default() -> Self { // Pre-computed air-sand rules - Self { - rules: HashMap::from([ - gen_rule( - (Element::Air, Element::Sand, Element::Air, Element::Air), - (Element::Air, Element::Air, Element::Air, Element::Sand), - ), - gen_rule( - (Element::Air, Element::Sand, Element::Air, Element::Sand), - (Element::Air, Element::Air, Element::Sand, Element::Sand), - ), - gen_rule( - (Element::Air, Element::Sand, Element::Sand, Element::Air), - (Element::Air, Element::Air, Element::Sand, Element::Sand), - ), - gen_rule( - (Element::Sand, Element::Air, Element::Air, Element::Air), - (Element::Air, Element::Air, Element::Sand, Element::Air), - ), - gen_rule( - (Element::Sand, Element::Air, Element::Air, Element::Sand), - (Element::Air, Element::Air, Element::Sand, Element::Sand), - ), - gen_rule( - (Element::Sand, Element::Air, Element::Sand, Element::Air), - (Element::Air, Element::Air, Element::Sand, Element::Sand), - ), - gen_rule( - (Element::Sand, Element::Sand, Element::Air, Element::Air), - (Element::Air, Element::Air, Element::Sand, Element::Sand), - ), - gen_rule( - (Element::Sand, Element::Sand, Element::Air, Element::Sand), - (Element::Air, Element::Sand, Element::Sand, Element::Sand), - ), - gen_rule( - (Element::Sand, Element::Sand, Element::Sand, Element::Air), - (Element::Sand, Element::Air, Element::Sand, Element::Sand), - ), - ]), - } + let rule_config = RuleConfig { + element_groups: vec![vec![Element::Air], vec![Element::Sand]], + rules: vec![ + ([0, 1, 0, 0], [0, 0, 0, 1]), + ([0, 1, 0, 1], [0, 0, 1, 1]), + ([0, 1, 1, 0], [0, 0, 1, 1]), + ([1, 0, 0, 0], [0, 0, 1, 0]), + ([1, 0, 0, 1], [0, 0, 1, 1]), + ([1, 0, 1, 0], [0, 0, 1, 1]), + ([1, 1, 0, 0], [0, 0, 1, 1]), + ([1, 1, 0, 1], [0, 1, 1, 1]), + ([1, 1, 1, 0], [1, 0, 1, 1]), + ], + }; + let rules = rule_config.compile(); + + Self { rules } } }