Start working on rule compiler

This commit is contained in:
Jarrod Doyle 2024-01-31 14:54:12 +00:00
parent 0a17d1d7d3
commit c80d5b48c2
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
1 changed files with 48 additions and 40 deletions

View File

@ -2,6 +2,37 @@ use bevy::{ecs::system::Resource, utils::HashMap};
use super::Element; use super::Element;
struct RuleConfig {
element_groups: Vec<Vec<Element>>,
rules: Vec<([usize; 4], [usize; 4])>,
}
impl RuleConfig {
fn compile(&self) -> HashMap<u32, u32> {
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)] #[derive(Resource)]
pub struct FallingSandRules { pub struct FallingSandRules {
rules: HashMap<u32, u32>, rules: HashMap<u32, u32>,
@ -10,46 +41,23 @@ pub struct FallingSandRules {
impl Default for FallingSandRules { impl Default for FallingSandRules {
fn default() -> Self { fn default() -> Self {
// Pre-computed air-sand rules // Pre-computed air-sand rules
Self { let rule_config = RuleConfig {
rules: HashMap::from([ element_groups: vec![vec![Element::Air], vec![Element::Sand]],
gen_rule( rules: vec![
(Element::Air, Element::Sand, Element::Air, Element::Air), ([0, 1, 0, 0], [0, 0, 0, 1]),
(Element::Air, Element::Air, Element::Air, Element::Sand), ([0, 1, 0, 1], [0, 0, 1, 1]),
), ([0, 1, 1, 0], [0, 0, 1, 1]),
gen_rule( ([1, 0, 0, 0], [0, 0, 1, 0]),
(Element::Air, Element::Sand, Element::Air, Element::Sand), ([1, 0, 0, 1], [0, 0, 1, 1]),
(Element::Air, Element::Air, Element::Sand, Element::Sand), ([1, 0, 1, 0], [0, 0, 1, 1]),
), ([1, 1, 0, 0], [0, 0, 1, 1]),
gen_rule( ([1, 1, 0, 1], [0, 1, 1, 1]),
(Element::Air, Element::Sand, Element::Sand, Element::Air), ([1, 1, 1, 0], [1, 0, 1, 1]),
(Element::Air, Element::Air, Element::Sand, Element::Sand), ],
), };
gen_rule( let rules = rule_config.compile();
(Element::Sand, Element::Air, Element::Air, Element::Air),
(Element::Air, Element::Air, Element::Sand, Element::Air), Self { rules }
),
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),
),
]),
}
} }
} }