Specialize constant material rule branches after binding
This commit is contained in:
@@ -300,6 +300,14 @@ enum Kind<R: RandomSource> {
|
||||
Not(Box<Compiled<R>>),
|
||||
}
|
||||
impl<R: RandomSource> Compiled<R> {
|
||||
fn constant_result(&self) -> Option<bool> {
|
||||
match &self.kind {
|
||||
Kind::Constant(value) => Some(*value),
|
||||
Kind::Not(child) => child.constant_result().map(|value| !value),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn compile<S: MaterialSurfaceQueries>(
|
||||
condition: MaterialCondition,
|
||||
root: &R::Positional,
|
||||
|
||||
@@ -70,6 +70,7 @@ impl From<horizon_core::density::SampleError> for MaterialRuleError {
|
||||
}
|
||||
|
||||
pub(super) enum CompiledRule<R: RandomSource> {
|
||||
Empty,
|
||||
Block(BlockStateId),
|
||||
OreVein {
|
||||
blocks: [BlockStateId; 3],
|
||||
@@ -127,7 +128,12 @@ impl<R: RandomSource> CompiledRule<R> {
|
||||
// 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<R: RandomSource> CompiledRule<R> {
|
||||
}
|
||||
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<R: RandomSource> CompiledRule<R> {
|
||||
context: &mut Context<S>,
|
||||
) -> Result<Option<BlockStateId>, MaterialRuleError> {
|
||||
match self {
|
||||
Self::Empty => Ok(None),
|
||||
Self::Block(block) => Ok(Some(*block)),
|
||||
Self::OreVein {
|
||||
blocks,
|
||||
|
||||
@@ -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<bool>,
|
||||
temperatures: usize,
|
||||
biomes: usize,
|
||||
fail_binding: bool,
|
||||
fail_temperature: bool,
|
||||
}
|
||||
impl MaterialSurfaceQueries for Queries {
|
||||
fn possible_biomes(&self) -> Option<BTreeSet<String>> {
|
||||
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<dyn DensitySampler>,
|
||||
eager: bool,
|
||||
) -> Result<usize, DecodeError> {
|
||||
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<Option<Arc<str>>, MaterialRuleError> {
|
||||
self.biomes += 1;
|
||||
Ok(Some(Arc::from("minecraft:plains")))
|
||||
}
|
||||
fn cold_enough_to_snow(
|
||||
&mut self,
|
||||
_: &str,
|
||||
_: BlockPos,
|
||||
_: i32,
|
||||
) -> Result<bool, MaterialRuleError> {
|
||||
self.temperatures += 1;
|
||||
if self.fail_temperature {
|
||||
Err(MaterialRuleError::BiomeQueryFailed)
|
||||
} else {
|
||||
Ok(true)
|
||||
}
|
||||
}
|
||||
}
|
||||
fn worker(surface: Queries) -> MaterialConditions<XoroshiroRandom, Queries> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user