diff --git a/crates/horizon-world/src/worldgen/material_condition.rs b/crates/horizon-world/src/worldgen/material_condition.rs index 30eb26ec8..6433b09a4 100644 --- a/crates/horizon-world/src/worldgen/material_condition.rs +++ b/crates/horizon-world/src/worldgen/material_condition.rs @@ -300,6 +300,14 @@ enum Kind { Not(Box>), } impl Compiled { + fn constant_result(&self) -> Option { + match &self.kind { + Kind::Constant(value) => Some(*value), + Kind::Not(child) => child.constant_result().map(|value| !value), + _ => None, + } + } + fn compile( condition: MaterialCondition, root: &R::Positional, diff --git a/crates/horizon-world/src/worldgen/material_condition/rule.rs b/crates/horizon-world/src/worldgen/material_condition/rule.rs index 9dfc2ff1e..cf4fc8547 100644 --- a/crates/horizon-world/src/worldgen/material_condition/rule.rs +++ b/crates/horizon-world/src/worldgen/material_condition/rule.rs @@ -70,6 +70,7 @@ impl From for MaterialRuleError { } pub(super) enum CompiledRule { + Empty, Block(BlockStateId), OreVein { blocks: [BlockStateId; 3], @@ -127,7 +128,12 @@ impl CompiledRule { // whether the predicate will match any subsequent position. let condition = Compiled::compile(if_true, root, context, depth + 1)?; let rule = Box::new(Self::compile(*then_run, root, context, depth + 1, nodes)?); - Self::Condition { condition, rule } + // Compile both sides first: binding and errors must retain their order. + match condition.constant_result() { + Some(false) => Self::Empty, + Some(true) => *rule, + None => Self::Condition { condition, rule }, + } } MaterialRule::Sequence(mut children) => { if children.len() == 1 { @@ -135,9 +141,16 @@ impl CompiledRule { } let mut result = Vec::with_capacity(children.len()); for child in children { - result.push(Self::compile(child, root, context, depth + 1, nodes)?); + let child = Self::compile(child, root, context, depth + 1, nodes)?; + if !matches!(child, Self::Empty) { + result.push(child); + } + } + match result.len() { + 0 => Self::Empty, + 1 => result.pop().unwrap(), + _ => Self::Sequence(result), } - Self::Sequence(result) } }) } @@ -147,6 +160,7 @@ impl CompiledRule { context: &mut Context, ) -> Result, MaterialRuleError> { match self { + Self::Empty => Ok(None), Self::Block(block) => Ok(Some(*block)), Self::OreVein { blocks, diff --git a/crates/horizon-world/tests/material_rule_specialization.rs b/crates/horizon-world/tests/material_rule_specialization.rs new file mode 100644 index 000000000..e953e908e --- /dev/null +++ b/crates/horizon-world/tests/material_rule_specialization.rs @@ -0,0 +1,172 @@ +use horizon_core::{ + density::{ConstantSampler, DensitySampler}, + position::BlockPos, + random::{RandomSource, XoroshiroRandom}, +}; +use horizon_world::{ + registry::BlockStateId, + worldgen::{ + DecodeError, + anchor::WorldGenerationContext, + material_condition::{ + MaterialCondition, MaterialConditions, MaterialRule, MaterialRuleError, + MaterialSurfaceQueries, + }, + }, +}; +use std::{collections::BTreeSet, sync::Arc}; + +#[derive(Default)] +struct Queries { + bindings: Vec, + temperatures: usize, + biomes: usize, + fail_binding: bool, + fail_temperature: bool, +} +impl MaterialSurfaceQueries for Queries { + fn possible_biomes(&self) -> Option> { + Some(BTreeSet::from(["minecraft:plains".into()])) + } + fn surface_depth(&mut self, _: i32, _: i32) -> i32 { + 1 + } + fn surface_secondary(&mut self, _: i32, _: i32) -> f64 { + 0. + } + fn bind_density( + &mut self, + _: Arc, + eager: bool, + ) -> Result { + self.bindings.push(eager); + if self.fail_binding { + return Err(DecodeError::Field("test binding failure")); + } + Ok(self.bindings.len() - 1) + } + fn biome(&mut self, _: BlockPos) -> Result>, MaterialRuleError> { + self.biomes += 1; + Ok(Some(Arc::from("minecraft:plains"))) + } + fn cold_enough_to_snow( + &mut self, + _: &str, + _: BlockPos, + _: i32, + ) -> Result { + self.temperatures += 1; + if self.fail_temperature { + Err(MaterialRuleError::BiomeQueryFailed) + } else { + Ok(true) + } + } +} +fn worker(surface: Queries) -> MaterialConditions { + MaterialConditions::new( + WorldGenerationContext::new(-64, 384, 63), + XoroshiroRandom::new(7).fork_positional(), + surface, + ) +} +fn condition(if_true: MaterialCondition, then_run: MaterialRule) -> MaterialRule { + MaterialRule::Condition { + if_true, + then_run: Box::new(then_run), + } +} +fn biome(name: &str) -> MaterialCondition { + MaterialCondition::Biome(vec![name.into()]) +} +fn ore() -> MaterialRule { + MaterialRule::OreVein { + ore: BlockStateId(1), + raw_ore: BlockStateId(2), + filler: BlockStateId(3), + raw_ore_chance: 0.02, + density: Arc::new(ConstantSampler(1.)), + richness: Arc::new(ConstantSampler(1.)), + filler_gap: Arc::new(ConstantSampler(-1.)), + } +} +#[test] +fn constant_branches_keep_binding_order_and_short_circuit_queries() { + let mut w = worker(Queries::default()); + let rule = w + .add_rule(MaterialRule::Sequence(vec![ + condition(biome("minecraft:desert"), ore()), + condition( + MaterialCondition::Not(Box::new(biome("minecraft:desert"))), + MaterialRule::Block(BlockStateId(9)), + ), + ore(), + ])) + .unwrap(); + w.update_xz(4, 5, 0, 0); + w.update_y(1, 1, i32::MIN, 64); + for _ in 0..2 { + assert_eq!( + w.try_apply(rule, BlockPos::new(4, 64, 5)), + Ok(Some(BlockStateId(9))) + ); + } + let q = w.into_surface(); + assert_eq!(q.bindings, [true, true, false, true, true, false]); + assert_eq!(q.biomes, 0); + assert_eq!(q.temperatures, 0); +} +#[test] +fn unreachable_children_still_fail_compilation_without_adding_a_rule() { + for preceding_block in [false, true] { + let mut w = worker(Queries { + fail_binding: true, + ..Queries::default() + }); + let dead = condition(biome("minecraft:desert"), ore()); + let rule = if preceding_block { + MaterialRule::Sequence(vec![MaterialRule::Block(BlockStateId(9)), dead]) + } else { + dead + }; + assert!(w.add_rule(rule).is_err()); + assert_eq!( + w.try_apply(0, BlockPos::new(0, 0, 0)), + Err(MaterialRuleError::UnknownRule) + ); + assert_eq!(w.into_surface().bindings, [true]); + } +} +#[test] +fn dynamic_predicate_on_empty_child_retains_each_temperature_call_and_error() { + for fail in [false, true] { + let mut w = worker(Queries { + fail_temperature: fail, + ..Queries::default() + }); + let rule = w + .add_rule(condition( + MaterialCondition::Temperature, + condition( + biome("minecraft:desert"), + MaterialRule::Block(BlockStateId(9)), + ), + )) + .unwrap(); + w.update_xz(0, 0, 0, 0); + w.update_y(1, 1, i32::MIN, 64); + for _ in 0..2 { + assert_eq!( + w.try_apply(rule, BlockPos::new(0, 64, 0)), + if fail { + Err(MaterialRuleError::BiomeQueryFailed) + } else { + Ok(None) + } + ); + } + let q = w.into_surface(); + assert_eq!(q.temperatures, 2); + assert_eq!(q.biomes, 1); + } +}