mirror of
https://github.com/Pumpkin-MC/Pumpkin.git
synced 2026-08-31 08:22:33 +00:00
revert: 31a56879
This commit is contained in:
@@ -1,395 +1,459 @@
|
||||
use heck::ToShoutySnakeCase;
|
||||
use proc_macro2::TokenStream;
|
||||
use quote::{format_ident, quote};
|
||||
use quote::{ToTokens, format_ident, quote};
|
||||
use serde::Deserialize;
|
||||
use std::{collections::BTreeMap, fs};
|
||||
|
||||
// ── Deserialized input types (unchanged from original) ────────────────────────
|
||||
|
||||
/// Deserialized block reference used in chunk generation settings (e.g., default block or fluid).
|
||||
#[derive(Deserialize)]
|
||||
pub struct BlockStateCodecStruct {
|
||||
/// Block registry name including the `minecraft:` namespace prefix.
|
||||
#[serde(rename = "Name")]
|
||||
pub name: String,
|
||||
/// Optional block state properties (e.g., `{"facing": "north"}`).
|
||||
#[serde(rename = "Properties")]
|
||||
pub properties: Option<BTreeMap<String, String>>,
|
||||
}
|
||||
|
||||
/// Deserialized chunk generation settings for a dimension, sourced from `chunk_gen_settings.json`.
|
||||
#[derive(Deserialize)]
|
||||
pub struct GenerationSettingsStruct {
|
||||
/// Whether aquifer (underground water pocket) generation is enabled.
|
||||
#[serde(default)]
|
||||
pub aquifers_enabled: bool,
|
||||
/// Whether ore-vein generation is enabled.
|
||||
#[serde(default)]
|
||||
pub ore_veins_enabled: bool,
|
||||
/// Whether to use the legacy random number source for this dimension.
|
||||
#[serde(default)]
|
||||
pub legacy_random_source: bool,
|
||||
/// Y-level treated as sea level for this dimension.
|
||||
pub sea_level: i32,
|
||||
/// Default fluid block (usually water or lava) placed by the aquifer generator.
|
||||
pub default_fluid: BlockStateCodecStruct,
|
||||
/// Default solid block used to fill the terrain.
|
||||
pub default_block: BlockStateCodecStruct,
|
||||
/// Noise shape parameters controlling vertical and horizontal cell sizes.
|
||||
#[serde(rename = "noise")]
|
||||
pub shape: GenerationShapeConfigStruct,
|
||||
/// Hierarchical surface material rule determining which block is placed at each surface point.
|
||||
pub surface_rule: MaterialRuleStruct,
|
||||
}
|
||||
|
||||
/// Deserialized noise-shape configuration controlling terrain cell dimensions.
|
||||
#[derive(Deserialize)]
|
||||
pub struct GenerationShapeConfigStruct {
|
||||
/// Minimum Y level for terrain generation.
|
||||
pub min_y: i8,
|
||||
/// Total vertical span of the generation region in blocks.
|
||||
pub height: u16,
|
||||
/// Log₂ of the horizontal cell block count (cell width = `1 << size_horizontal`).
|
||||
pub size_horizontal: u8,
|
||||
/// Log₂ of the vertical cell block count (cell height = `1 << size_vertical`).
|
||||
pub size_vertical: u8,
|
||||
}
|
||||
|
||||
/// Deserialized surface material rule that determines which block to place at a given surface point.
|
||||
#[derive(Deserialize)]
|
||||
#[serde(tag = "type")]
|
||||
pub enum MaterialRuleStruct {
|
||||
/// Place a specific block state unconditionally.
|
||||
#[serde(rename = "minecraft:block")]
|
||||
Block { result_state: BlockStateCodecStruct },
|
||||
Block {
|
||||
/// The block state to place.
|
||||
result_state: BlockStateCodecStruct,
|
||||
},
|
||||
/// Evaluate each rule in order, stopping at the first match.
|
||||
#[serde(rename = "minecraft:sequence")]
|
||||
Sequence { sequence: Vec<Self> },
|
||||
Sequence {
|
||||
/// The ordered list of child rules.
|
||||
sequence: Vec<Self>,
|
||||
},
|
||||
/// Apply `then_run` only when `if_true` evaluates to true.
|
||||
#[serde(rename = "minecraft:condition")]
|
||||
Condition {
|
||||
/// The condition that must be satisfied.
|
||||
if_true: MaterialConditionStruct,
|
||||
/// The rule to apply when the condition is met.
|
||||
then_run: Box<Self>,
|
||||
},
|
||||
/// Special Badlands terrain coloring rule.
|
||||
#[serde(rename = "minecraft:bandlands")]
|
||||
Badlands,
|
||||
}
|
||||
|
||||
/// Deserialized surface material condition that gates a material rule.
|
||||
#[derive(Deserialize)]
|
||||
#[serde(tag = "type")]
|
||||
pub enum MaterialConditionStruct {
|
||||
/// True when the current position is in one of the listed biomes.
|
||||
#[serde(rename = "minecraft:biome")]
|
||||
Biome { biome_is: Vec<String> },
|
||||
Biome {
|
||||
/// List of biome resource locations to match against.
|
||||
biome_is: Vec<String>,
|
||||
},
|
||||
/// True when a named noise value is within the given range.
|
||||
#[serde(rename = "minecraft:noise_threshold")]
|
||||
NoiseThreshold {
|
||||
/// Resource location of the noise parameter.
|
||||
noise: String,
|
||||
/// Minimum threshold (inclusive).
|
||||
min_threshold: f64,
|
||||
/// Maximum threshold (inclusive).
|
||||
max_threshold: f64,
|
||||
},
|
||||
/// True below a Y offset and false above another, with a linear gradient in between.
|
||||
#[serde(rename = "minecraft:vertical_gradient")]
|
||||
VerticalGradient {
|
||||
/// Name of the random source used for this gradient.
|
||||
random_name: String,
|
||||
/// Y offset below which the condition is always true.
|
||||
true_at_and_below: YOffsetStruct,
|
||||
/// Y offset above which the condition is always false.
|
||||
false_at_and_above: YOffsetStruct,
|
||||
},
|
||||
/// True when the position is above a given Y anchor.
|
||||
#[serde(rename = "minecraft:y_above")]
|
||||
YAbove {
|
||||
/// The Y offset anchor to compare against.
|
||||
anchor: YOffsetStruct,
|
||||
/// Multiplier applied to surface depth when computing the threshold.
|
||||
surface_depth_multiplier: i32,
|
||||
/// Whether to add stone depth to the comparison value.
|
||||
add_stone_depth: bool,
|
||||
},
|
||||
/// True when the position is above a water surface within a certain offset.
|
||||
#[serde(rename = "minecraft:water")]
|
||||
Water {
|
||||
/// Y offset relative to the water surface.
|
||||
offset: i32,
|
||||
/// Multiplier applied to surface depth.
|
||||
surface_depth_multiplier: i32,
|
||||
/// Whether to add stone depth to the comparison value.
|
||||
add_stone_depth: bool,
|
||||
},
|
||||
/// True when the biome temperature is cold (below freezing).
|
||||
#[serde(rename = "minecraft:temperature")]
|
||||
Temperature,
|
||||
/// True when the terrain is steep (high slope).
|
||||
#[serde(rename = "minecraft:steep")]
|
||||
Steep,
|
||||
/// Inverts the inner condition.
|
||||
#[serde(rename = "minecraft:not")]
|
||||
Not { invert: Box<Self> },
|
||||
Not {
|
||||
/// The condition to invert.
|
||||
invert: Box<Self>,
|
||||
},
|
||||
/// True when there is a hole (cave opening) at the position.
|
||||
#[serde(rename = "minecraft:hole")]
|
||||
Hole,
|
||||
/// True when the position is above the preliminary surface estimate.
|
||||
#[serde(rename = "minecraft:above_preliminary_surface")]
|
||||
AbovePreliminarySurface,
|
||||
/// True based on the depth of stone/ceiling relative to the surface.
|
||||
#[serde(rename = "minecraft:stone_depth")]
|
||||
StoneDepth {
|
||||
/// Y offset added to the depth value.
|
||||
offset: i32,
|
||||
/// Whether to include surface depth in the calculation.
|
||||
add_surface_depth: bool,
|
||||
/// Additional secondary depth range.
|
||||
secondary_depth_range: i32,
|
||||
/// Surface type to measure from: `"ceiling"` or `"floor"`.
|
||||
surface_type: String,
|
||||
},
|
||||
}
|
||||
|
||||
/// Deserialized Y offset that can be expressed relative to different reference points.
|
||||
#[derive(Deserialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum YOffsetStruct {
|
||||
Absolute { absolute: i16 },
|
||||
AboveBottom { above_bottom: i8 },
|
||||
BelowTop { below_top: i8 },
|
||||
/// An absolute Y coordinate.
|
||||
Absolute {
|
||||
/// The absolute Y level.
|
||||
absolute: i16,
|
||||
},
|
||||
/// A Y level measured upward from the dimension's minimum Y.
|
||||
AboveBottom {
|
||||
/// Number of blocks above the bottom of the dimension.
|
||||
above_bottom: i8,
|
||||
},
|
||||
/// A Y level measured downward from the dimension's maximum Y.
|
||||
BelowTop {
|
||||
/// Number of blocks below the top of the dimension.
|
||||
below_top: i8,
|
||||
},
|
||||
}
|
||||
|
||||
// ── Flat bytecode compiler ────────────────────────────────────────────────────
|
||||
// --- ToTokens Implementations ---
|
||||
|
||||
/// Compiles a `MaterialRuleStruct` tree into a flat Vec of `TokenStream`s,
|
||||
/// each representing one `SurfaceInstruction` literal.
|
||||
///
|
||||
/// Layout rules:
|
||||
/// - `Sequence` → children in order, no instruction of its own
|
||||
/// - `Condition` → `Test*` instruction (with a `skip` field) followed by the
|
||||
/// body instructions; skip jumps past the body on failure
|
||||
/// - `Block` → `PlaceBlock` terminal
|
||||
/// - `Badlands` → `PlaceBadlands` terminal
|
||||
struct Compiler {
|
||||
out: Vec<TokenStream>,
|
||||
}
|
||||
|
||||
impl Compiler {
|
||||
fn new() -> Self {
|
||||
Self { out: Vec::new() }
|
||||
}
|
||||
|
||||
/// Appends instructions for `rule` and returns the number of instructions emitted.
|
||||
fn compile_rule(&mut self, rule: &MaterialRuleStruct) -> usize {
|
||||
let before = self.out.len();
|
||||
match rule {
|
||||
MaterialRuleStruct::Block { result_state } => {
|
||||
let ts = block_state_tokens(result_state);
|
||||
self.out
|
||||
.push(quote!(SurfaceInstruction::PlaceBlock { state: #ts }));
|
||||
}
|
||||
MaterialRuleStruct::Badlands => {
|
||||
self.out.push(quote!(SurfaceInstruction::PlaceBadlands));
|
||||
}
|
||||
MaterialRuleStruct::Sequence { sequence } => {
|
||||
// Sequences have no instruction of their own; children are inline
|
||||
for child in sequence {
|
||||
self.compile_rule(child);
|
||||
}
|
||||
}
|
||||
MaterialRuleStruct::Condition { if_true, then_run } => {
|
||||
// Reserve a slot for the Test instruction; we need body size first
|
||||
let test_slot = self.out.len();
|
||||
self.out.push(TokenStream::new()); // placeholder
|
||||
|
||||
let body_len = self.compile_rule(then_run);
|
||||
let skip = body_len as u16;
|
||||
|
||||
// Patch placeholder with the real instruction now that skip is known
|
||||
self.out[test_slot] = compile_condition(if_true, skip);
|
||||
}
|
||||
}
|
||||
self.out.len() - before
|
||||
impl ToTokens for BlockStateCodecStruct {
|
||||
/// Emits a `BlockBlueprint` literal, stripping the `minecraft:` namespace prefix from the block name.
|
||||
fn to_tokens(&self, tokens: &mut TokenStream) {
|
||||
let name = &self.name.strip_prefix("minecraft:").unwrap_or(&self.name);
|
||||
let name_stripped = name.strip_prefix("minecraft:").unwrap_or(name);
|
||||
let block_ident =
|
||||
quote::format_ident!("{}", name_stripped.to_uppercase().replace([':', '-'], "_"));
|
||||
// TODO: use props
|
||||
tokens.extend(quote! {
|
||||
crate::Block::#block_ident.default_state
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// Emits a `SurfaceInstruction::Test*` token stream for the given condition.
|
||||
/// `skip` is the number of instructions to jump past on failure.
|
||||
fn compile_condition(cond: &MaterialConditionStruct, skip: u16) -> TokenStream {
|
||||
match cond {
|
||||
MaterialConditionStruct::Biome { biome_is } => {
|
||||
let refs: Vec<TokenStream> = biome_is
|
||||
.iter()
|
||||
.map(|b| {
|
||||
let ident = format_ident!(
|
||||
"{}",
|
||||
b.strip_prefix("minecraft:").unwrap_or(b).to_uppercase()
|
||||
);
|
||||
quote!(&crate::biome::Biome::#ident)
|
||||
})
|
||||
.collect();
|
||||
quote!(SurfaceInstruction::TestBiome {
|
||||
biome_is: &[#(#refs),*],
|
||||
skip: #skip,
|
||||
})
|
||||
}
|
||||
impl ToTokens for GenerationSettingsStruct {
|
||||
/// Emits a `GenerationSettings` struct literal with all fields populated from the deserialized data.
|
||||
fn to_tokens(&self, tokens: &mut TokenStream) {
|
||||
let aquifers = self.aquifers_enabled;
|
||||
let ores = self.ore_veins_enabled;
|
||||
let legacy = self.legacy_random_source;
|
||||
let sea_level = self.sea_level;
|
||||
let fluid = &self.default_fluid;
|
||||
let block = &self.default_block;
|
||||
let shape = &self.shape;
|
||||
let rule = &self.surface_rule;
|
||||
|
||||
MaterialConditionStruct::NoiseThreshold {
|
||||
noise,
|
||||
min_threshold,
|
||||
max_threshold,
|
||||
} => {
|
||||
let noise_id = format_ident!(
|
||||
"{}",
|
||||
noise
|
||||
.strip_prefix("minecraft:")
|
||||
.unwrap()
|
||||
.to_shouty_snake_case()
|
||||
);
|
||||
// f64::MAX (or near it) means "no upper bound" — emit the cheaper variant
|
||||
if *max_threshold >= f64::MAX / 2.0 {
|
||||
quote!(SurfaceInstruction::TestNoiseAbove {
|
||||
noise: DoublePerlinNoiseParameters::#noise_id,
|
||||
min: #min_threshold,
|
||||
skip: #skip,
|
||||
})
|
||||
} else {
|
||||
quote!(SurfaceInstruction::TestNoiseRange {
|
||||
noise: DoublePerlinNoiseParameters::#noise_id,
|
||||
min: #min_threshold,
|
||||
max: #max_threshold,
|
||||
skip: #skip,
|
||||
})
|
||||
tokens.extend(quote!(
|
||||
GenerationSettings {
|
||||
aquifers_enabled: #aquifers,
|
||||
ore_veins_enabled: #ores,
|
||||
legacy_random_source: #legacy,
|
||||
sea_level: #sea_level,
|
||||
default_fluid: #fluid,
|
||||
shape: #shape,
|
||||
surface_rule: #rule,
|
||||
default_block: #block,
|
||||
}
|
||||
}
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
MaterialConditionStruct::VerticalGradient {
|
||||
random_name,
|
||||
true_at_and_below,
|
||||
false_at_and_above,
|
||||
} => {
|
||||
let bytes = md5::compute(random_name.as_bytes());
|
||||
let lo = u64::from_be_bytes(bytes[0..8].try_into().expect("md5 slice"));
|
||||
let hi = u64::from_be_bytes(bytes[8..16].try_into().expect("md5 slice"));
|
||||
let below = y_offset_tokens(true_at_and_below);
|
||||
let above = y_offset_tokens(false_at_and_above);
|
||||
quote!(SurfaceInstruction::TestVerticalGradient {
|
||||
random_lo: #lo,
|
||||
random_hi: #hi,
|
||||
true_at_and_below: #below,
|
||||
false_at_and_above: #above,
|
||||
skip: #skip,
|
||||
})
|
||||
}
|
||||
impl ToTokens for GenerationShapeConfigStruct {
|
||||
/// Emits a `GenerationShapeConfig` struct literal with all noise-shape dimensions.
|
||||
fn to_tokens(&self, tokens: &mut TokenStream) {
|
||||
let min_y = self.min_y;
|
||||
let height = self.height;
|
||||
let hor = self.size_horizontal;
|
||||
let ver = self.size_vertical;
|
||||
tokens.extend(quote!(
|
||||
GenerationShapeConfig { min_y: #min_y, height: #height, size_horizontal: #hor, size_vertical: #ver }
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
MaterialConditionStruct::YAbove {
|
||||
anchor,
|
||||
surface_depth_multiplier,
|
||||
add_stone_depth,
|
||||
} => {
|
||||
let anchor_ts = y_offset_tokens(anchor);
|
||||
quote!(SurfaceInstruction::TestYAbove {
|
||||
anchor: #anchor_ts,
|
||||
surface_depth_multiplier: #surface_depth_multiplier,
|
||||
add_stone_depth: #add_stone_depth,
|
||||
skip: #skip,
|
||||
})
|
||||
}
|
||||
|
||||
MaterialConditionStruct::Water {
|
||||
offset,
|
||||
surface_depth_multiplier,
|
||||
add_stone_depth,
|
||||
} => {
|
||||
quote!(SurfaceInstruction::TestWater {
|
||||
offset: #offset,
|
||||
surface_depth_multiplier: #surface_depth_multiplier,
|
||||
add_stone_depth: #add_stone_depth,
|
||||
skip: #skip,
|
||||
})
|
||||
}
|
||||
|
||||
MaterialConditionStruct::StoneDepth {
|
||||
offset,
|
||||
add_surface_depth,
|
||||
secondary_depth_range,
|
||||
surface_type,
|
||||
} => {
|
||||
let st = match surface_type.as_str() {
|
||||
"ceiling" => quote!(VerticalSurfaceType::Ceiling),
|
||||
_ => quote!(VerticalSurfaceType::Floor),
|
||||
};
|
||||
quote!(SurfaceInstruction::TestStoneDepth {
|
||||
offset: #offset,
|
||||
add_surface_depth: #add_surface_depth,
|
||||
secondary_depth_range: #secondary_depth_range,
|
||||
surface_type: #st,
|
||||
skip: #skip,
|
||||
})
|
||||
}
|
||||
|
||||
MaterialConditionStruct::Not { invert } => {
|
||||
// Compile the inner condition with skip=0; the Not wrapper flips the result
|
||||
let inner = compile_condition(invert, 0);
|
||||
quote!(SurfaceInstruction::TestNot {
|
||||
inner: &#inner,
|
||||
skip: #skip,
|
||||
})
|
||||
}
|
||||
|
||||
MaterialConditionStruct::AbovePreliminarySurface => {
|
||||
quote!(SurfaceInstruction::TestAbovePreliminarySurface { skip: #skip })
|
||||
}
|
||||
MaterialConditionStruct::Hole => {
|
||||
quote!(SurfaceInstruction::TestHole { skip: #skip })
|
||||
}
|
||||
MaterialConditionStruct::Steep => {
|
||||
quote!(SurfaceInstruction::TestSteep { skip: #skip })
|
||||
}
|
||||
MaterialConditionStruct::Temperature => {
|
||||
quote!(SurfaceInstruction::TestTemperature { skip: #skip })
|
||||
impl ToTokens for YOffsetStruct {
|
||||
/// Emits a `YOffset` enum variant literal corresponding to the deserialized offset kind.
|
||||
fn to_tokens(&self, tokens: &mut TokenStream) {
|
||||
match self {
|
||||
Self::Absolute { absolute } => {
|
||||
tokens.extend(quote!(YOffset::Absolute(pumpkin_util::y_offset::Absolute { absolute: #absolute })));
|
||||
}
|
||||
Self::AboveBottom { above_bottom } => {
|
||||
tokens.extend(quote!(YOffset::AboveBottom(pumpkin_util::y_offset::AboveBottom { above_bottom: #above_bottom })));
|
||||
}
|
||||
Self::BelowTop { below_top } => {
|
||||
tokens.extend(quote!(YOffset::BelowTop(pumpkin_util::y_offset::BelowTop { below_top: #below_top })));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ── Small token helpers ───────────────────────────────────────────────────────
|
||||
impl ToTokens for MaterialConditionStruct {
|
||||
/// Emits a `MaterialCondition` enum variant literal for each surface condition kind.
|
||||
fn to_tokens(&self, tokens: &mut TokenStream) {
|
||||
match self {
|
||||
Self::Biome { biome_is } => {
|
||||
let biomes = biome_is
|
||||
.iter()
|
||||
.map(|b| b.strip_prefix("minecraft:").unwrap_or(b).to_uppercase());
|
||||
let biome_refs: Vec<TokenStream> = biomes
|
||||
.map(|b| {
|
||||
let ident = format_ident!("{}", b);
|
||||
quote!(&crate::biome::Biome::#ident)
|
||||
})
|
||||
.collect();
|
||||
|
||||
fn block_state_tokens(b: &BlockStateCodecStruct) -> TokenStream {
|
||||
let name = b.name.strip_prefix("minecraft:").unwrap_or(&b.name);
|
||||
let ident = format_ident!("{}", name.to_uppercase().replace([':', '-'], "_"));
|
||||
quote!(crate::Block::#ident.default_state)
|
||||
}
|
||||
tokens.extend(quote!(
|
||||
MaterialCondition::Biome(BiomeMaterialCondition {
|
||||
biome_is: &[#(#biome_refs),*],
|
||||
})
|
||||
));
|
||||
}
|
||||
Self::NoiseThreshold {
|
||||
noise,
|
||||
min_threshold,
|
||||
max_threshold,
|
||||
} => {
|
||||
let noise_id = quote::format_ident!(
|
||||
"{}",
|
||||
noise
|
||||
.strip_prefix("minecraft:")
|
||||
.unwrap()
|
||||
.to_shouty_snake_case()
|
||||
);
|
||||
|
||||
fn y_offset_tokens(y: &YOffsetStruct) -> TokenStream {
|
||||
match y {
|
||||
YOffsetStruct::Absolute { absolute } => {
|
||||
quote!(YOffset::Absolute(pumpkin_util::y_offset::Absolute { absolute: #absolute }))
|
||||
}
|
||||
YOffsetStruct::AboveBottom { above_bottom } => {
|
||||
quote!(YOffset::AboveBottom(pumpkin_util::y_offset::AboveBottom { above_bottom: #above_bottom }))
|
||||
}
|
||||
YOffsetStruct::BelowTop { below_top } => {
|
||||
quote!(YOffset::BelowTop(pumpkin_util::y_offset::BelowTop { below_top: #below_top }))
|
||||
tokens.extend(quote!(
|
||||
MaterialCondition::NoiseThreshold(NoiseThresholdMaterialCondition {
|
||||
noise: DoublePerlinNoiseParameters::#noise_id,
|
||||
min_threshold: #min_threshold,
|
||||
max_threshold: #max_threshold,
|
||||
})
|
||||
));
|
||||
}
|
||||
Self::VerticalGradient {
|
||||
random_name,
|
||||
true_at_and_below,
|
||||
false_at_and_above,
|
||||
} => {
|
||||
// Pre calc for speed :D
|
||||
let bytes = md5::compute(random_name.as_bytes());
|
||||
let lo = u64::from_be_bytes(bytes[0..8].try_into().expect("incorrect length"));
|
||||
let hi = u64::from_be_bytes(bytes[8..16].try_into().expect("incorrect length"));
|
||||
tokens.extend(quote!(
|
||||
MaterialCondition::VerticalGradient(VerticalGradientMaterialCondition {
|
||||
random_lo: #lo,
|
||||
random_hi: #hi,
|
||||
true_at_and_below: #true_at_and_below,
|
||||
false_at_and_above: #false_at_and_above,
|
||||
})
|
||||
));
|
||||
}
|
||||
Self::YAbove {
|
||||
anchor,
|
||||
surface_depth_multiplier,
|
||||
add_stone_depth,
|
||||
} => {
|
||||
tokens.extend(quote!(
|
||||
MaterialCondition::YAbove(AboveYMaterialCondition {
|
||||
anchor: #anchor,
|
||||
surface_depth_multiplier: #surface_depth_multiplier,
|
||||
add_stone_depth: #add_stone_depth,
|
||||
})
|
||||
));
|
||||
}
|
||||
Self::Water {
|
||||
offset,
|
||||
surface_depth_multiplier,
|
||||
add_stone_depth,
|
||||
} => {
|
||||
tokens.extend(quote!(
|
||||
MaterialCondition::Water(WaterMaterialCondition {
|
||||
offset: #offset,
|
||||
surface_depth_multiplier: #surface_depth_multiplier,
|
||||
add_stone_depth: #add_stone_depth,
|
||||
})
|
||||
));
|
||||
}
|
||||
Self::Temperature => {
|
||||
tokens.extend(quote!(MaterialCondition::Temperature));
|
||||
}
|
||||
Self::Steep => {
|
||||
tokens.extend(quote!(MaterialCondition::Steep));
|
||||
}
|
||||
Self::Not { invert } => {
|
||||
tokens.extend(quote!(
|
||||
MaterialCondition::Not(NotMaterialCondition {
|
||||
invert: &#invert,
|
||||
})
|
||||
));
|
||||
}
|
||||
Self::Hole => {
|
||||
tokens.extend(quote!(MaterialCondition::Hole(HoleMaterialCondition)));
|
||||
}
|
||||
Self::AbovePreliminarySurface => {
|
||||
tokens.extend(quote!(MaterialCondition::AbovePreliminarySurface(
|
||||
SurfaceMaterialCondition
|
||||
)));
|
||||
}
|
||||
Self::StoneDepth {
|
||||
offset,
|
||||
add_surface_depth,
|
||||
secondary_depth_range,
|
||||
surface_type,
|
||||
} => {
|
||||
let surface_type_token = match surface_type.as_str() {
|
||||
"ceiling" => quote!(
|
||||
pumpkin_util::math::vertical_surface_type::VerticalSurfaceType::Ceiling
|
||||
),
|
||||
"floor" => quote!(
|
||||
pumpkin_util::math::vertical_surface_type::VerticalSurfaceType::Floor
|
||||
),
|
||||
_ => quote!(panic!("Unknown surface type")),
|
||||
};
|
||||
|
||||
tokens.extend(quote!(
|
||||
MaterialCondition::StoneDepth(StoneDepthMaterialCondition {
|
||||
offset: #offset,
|
||||
add_surface_depth: #add_surface_depth,
|
||||
secondary_depth_range: #secondary_depth_range,
|
||||
surface_type: #surface_type_token,
|
||||
})
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn shape_tokens(s: &GenerationShapeConfigStruct) -> TokenStream {
|
||||
let min_y = s.min_y;
|
||||
let height = s.height;
|
||||
let hor = s.size_horizontal;
|
||||
let ver = s.size_vertical;
|
||||
quote!(GenerationShapeConfig {
|
||||
min_y: #min_y,
|
||||
height: #height,
|
||||
size_horizontal: #hor,
|
||||
size_vertical: #ver,
|
||||
})
|
||||
impl ToTokens for MaterialRuleStruct {
|
||||
/// Emits a `MaterialRule` enum variant literal for each surface placement rule kind.
|
||||
fn to_tokens(&self, tokens: &mut TokenStream) {
|
||||
match self {
|
||||
Self::Block { result_state } => {
|
||||
tokens.extend(quote!(
|
||||
MaterialRule::Block(BlockMaterialRule {
|
||||
result_state: #result_state
|
||||
})
|
||||
));
|
||||
}
|
||||
Self::Sequence { sequence } => {
|
||||
tokens.extend(quote!(
|
||||
MaterialRule::Sequence(SequenceMaterialRule {
|
||||
sequence: &[#(#sequence),*]
|
||||
})
|
||||
));
|
||||
}
|
||||
Self::Condition { if_true, then_run } => {
|
||||
tokens.extend(quote!(
|
||||
MaterialRule::Condition(ConditionMaterialRule {
|
||||
if_true: #if_true,
|
||||
then_run: &#then_run
|
||||
})
|
||||
));
|
||||
}
|
||||
Self::Badlands => {
|
||||
tokens.extend(quote!(MaterialRule::Badlands(BadLandsMaterialRule)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn settings_tokens(s: &GenerationSettingsStruct) -> TokenStream {
|
||||
let aquifers = s.aquifers_enabled;
|
||||
let ores = s.ore_veins_enabled;
|
||||
let legacy = s.legacy_random_source;
|
||||
let sea_level = s.sea_level;
|
||||
let fluid = block_state_tokens(&s.default_fluid);
|
||||
let block = block_state_tokens(&s.default_block);
|
||||
let shape = shape_tokens(&s.shape);
|
||||
|
||||
let mut compiler = Compiler::new();
|
||||
compiler.compile_rule(&s.surface_rule);
|
||||
let instructions = &compiler.out;
|
||||
|
||||
quote!(GenerationSettings {
|
||||
aquifers_enabled: #aquifers,
|
||||
ore_veins_enabled: #ores,
|
||||
legacy_random_source: #legacy,
|
||||
sea_level: #sea_level,
|
||||
default_fluid: #fluid,
|
||||
default_block: #block,
|
||||
shape: #shape,
|
||||
surface_rule: CompiledSurfaceRule {
|
||||
instructions: &[#(#instructions),*],
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// ── Entry point ───────────────────────────────────────────────────────────────
|
||||
|
||||
/// Reads `chunk_gen_settings.json` and emits the complete chunk generation settings `TokenStream`.
|
||||
pub fn build() -> TokenStream {
|
||||
let json: BTreeMap<String, GenerationSettingsStruct> =
|
||||
serde_json::from_str(&fs::read_to_string("../assets/chunk_gen_settings.json").unwrap())
|
||||
.expect("Failed to parse chunk_gen_settings.json");
|
||||
.expect("Failed to parse settings.json");
|
||||
|
||||
let const_defs: TokenStream = json
|
||||
.iter()
|
||||
.map(|(name, settings)| {
|
||||
let const_name = format_ident!("{}", name.to_uppercase());
|
||||
let body = settings_tokens(settings);
|
||||
quote!(pub const #const_name: GenerationSettings = #body;)
|
||||
})
|
||||
.collect();
|
||||
let mut const_defs = TokenStream::new();
|
||||
|
||||
for (name, settings) in &json {
|
||||
let upper_name = name.to_uppercase();
|
||||
let const_name = format_ident!("{}", upper_name);
|
||||
|
||||
const_defs.extend(quote!(
|
||||
pub const #const_name: GenerationSettings = #settings;
|
||||
));
|
||||
}
|
||||
|
||||
quote!(
|
||||
use crate::dimension::Dimension;
|
||||
use crate::chunk::DoublePerlinNoiseParameters;
|
||||
use crate::BlockState;
|
||||
use pumpkin_util::y_offset::YOffset;
|
||||
use pumpkin_util::math::vertical_surface_type::VerticalSurfaceType;
|
||||
use crate::biome::Biome;
|
||||
|
||||
// ── Core settings struct ──────────────────────────────────────────────
|
||||
use std::{cell::RefCell, num::NonZeroUsize};
|
||||
use pumpkin_util::random::RandomDeriver;
|
||||
use pumpkin_util::y_offset::YOffset;
|
||||
use crate::biome::Biome;
|
||||
use pumpkin_util::y_offset::Absolute;
|
||||
|
||||
pub struct GenerationSettings {
|
||||
pub aquifers_enabled: bool,
|
||||
@@ -397,10 +461,9 @@ pub fn build() -> TokenStream {
|
||||
pub legacy_random_source: bool,
|
||||
pub sea_level: i32,
|
||||
pub default_fluid: &'static BlockState,
|
||||
pub default_block: &'static BlockState,
|
||||
pub shape: GenerationShapeConfig,
|
||||
/// Flat compiled surface rule — no recursion at runtime.
|
||||
pub surface_rule: CompiledSurfaceRule,
|
||||
pub surface_rule: MaterialRule,
|
||||
pub default_block: &'static BlockState,
|
||||
}
|
||||
|
||||
pub struct GenerationShapeConfig {
|
||||
@@ -411,9 +474,12 @@ pub fn build() -> TokenStream {
|
||||
}
|
||||
|
||||
impl GenerationShapeConfig {
|
||||
#[inline] #[must_use]
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub const fn vertical_cell_block_count(&self) -> u8 { self.size_vertical << 2 }
|
||||
#[inline] #[must_use]
|
||||
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub const fn horizontal_cell_block_count(&self) -> u8 { self.size_horizontal << 2 }
|
||||
|
||||
#[must_use]
|
||||
@@ -438,148 +504,107 @@ pub fn build() -> TokenStream {
|
||||
} else {
|
||||
new_top + new_min.unsigned_abs() as u16
|
||||
};
|
||||
Self { min_y: new_min, height: new_height,
|
||||
size_horizontal: self.size_horizontal, size_vertical: self.size_vertical }
|
||||
}
|
||||
}
|
||||
|
||||
// ── Flat bytecode types ───────────────────────────────────────────────
|
||||
|
||||
/// A compiled, flat representation of a surface rule tree.
|
||||
/// Evaluated by a simple index-advancing loop — no recursion.
|
||||
pub struct CompiledSurfaceRule {
|
||||
pub instructions: &'static [SurfaceInstruction],
|
||||
}
|
||||
|
||||
/// One instruction in the flat surface rule bytecode.
|
||||
///
|
||||
/// Test instructions carry a `skip: u16` field. On failure the evaluator
|
||||
/// advances the program counter by `skip + 1` (past the body). On success
|
||||
/// it advances by 1 (into the body). Terminals end evaluation immediately.
|
||||
pub enum SurfaceInstruction {
|
||||
// ── Terminals ────────────────────────────────────────────────────
|
||||
PlaceBlock { state: &'static BlockState },
|
||||
PlaceBadlands,
|
||||
|
||||
// ── Conditions ───────────────────────────────────────────────────
|
||||
TestBiome {
|
||||
biome_is: &'static [&'static Biome],
|
||||
skip: u16,
|
||||
},
|
||||
/// Noise >= min (the f64::MAX upper-bound fast path)
|
||||
TestNoiseAbove {
|
||||
noise: DoublePerlinNoiseParameters,
|
||||
min: f64,
|
||||
skip: u16,
|
||||
},
|
||||
/// min <= noise <= max
|
||||
TestNoiseRange {
|
||||
noise: DoublePerlinNoiseParameters,
|
||||
min: f64,
|
||||
max: f64,
|
||||
skip: u16,
|
||||
},
|
||||
TestVerticalGradient {
|
||||
random_lo: u64,
|
||||
random_hi: u64,
|
||||
true_at_and_below: YOffset,
|
||||
false_at_and_above: YOffset,
|
||||
skip: u16,
|
||||
},
|
||||
TestYAbove {
|
||||
anchor: YOffset,
|
||||
surface_depth_multiplier: i32,
|
||||
add_stone_depth: bool,
|
||||
skip: u16,
|
||||
},
|
||||
TestWater {
|
||||
offset: i32,
|
||||
surface_depth_multiplier: i32,
|
||||
add_stone_depth: bool,
|
||||
skip: u16,
|
||||
},
|
||||
TestStoneDepth {
|
||||
offset: i32,
|
||||
add_surface_depth: bool,
|
||||
secondary_depth_range: i32,
|
||||
surface_type: VerticalSurfaceType,
|
||||
skip: u16,
|
||||
},
|
||||
TestAbovePreliminarySurface { skip: u16 },
|
||||
TestHole { skip: u16 },
|
||||
TestSteep { skip: u16 },
|
||||
TestTemperature { skip: u16 },
|
||||
/// Inverts a single inner condition.
|
||||
/// The inner condition is stored inline — it must not itself contain
|
||||
/// a body (i.e. it always has inner skip = 0).
|
||||
TestNot {
|
||||
inner: &'static SurfaceInstruction,
|
||||
skip: u16,
|
||||
},
|
||||
}
|
||||
|
||||
// ── Per-column noise cache ────────────────────────────────────────────
|
||||
|
||||
/// Caches noise samples for one (x, z) column.
|
||||
/// Call `invalidate(x, z)` once per column; `get` then returns the cached
|
||||
/// value on subsequent calls for the same noise parameter.
|
||||
pub struct ColumnNoiseCache {
|
||||
values: [f64; DoublePerlinNoiseParameters::COUNT],
|
||||
valid: [bool; DoublePerlinNoiseParameters::COUNT],
|
||||
col_x: i32,
|
||||
col_z: i32,
|
||||
}
|
||||
|
||||
impl ColumnNoiseCache {
|
||||
pub const fn new() -> Self {
|
||||
Self {
|
||||
values: [0.0; DoublePerlinNoiseParameters::COUNT],
|
||||
valid: [false; DoublePerlinNoiseParameters::COUNT],
|
||||
col_x: i32::MIN,
|
||||
col_z: i32::MIN,
|
||||
min_y: new_min,
|
||||
height: new_height,
|
||||
size_horizontal: self.size_horizontal,
|
||||
size_vertical: self.size_vertical,
|
||||
}
|
||||
}
|
||||
|
||||
/// Must be called at the start of each new (x, z) column.
|
||||
#[inline]
|
||||
pub fn invalidate(&mut self, x: i32, z: i32) {
|
||||
if self.col_x != x || self.col_z != z {
|
||||
self.valid = [false; DoublePerlinNoiseParameters::COUNT];
|
||||
self.col_x = x;
|
||||
self.col_z = z;
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the cached noise value, sampling it on first access.
|
||||
#[inline]
|
||||
pub fn get(
|
||||
&mut self,
|
||||
noise: &DoublePerlinNoiseParameters,
|
||||
sample: impl FnOnce() -> f64,
|
||||
) -> f64 {
|
||||
let idx = noise.id;
|
||||
if !self.valid[idx] {
|
||||
self.values[idx] = sample();
|
||||
self.valid[idx] = true;
|
||||
}
|
||||
self.values[idx]
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ColumnNoiseCache {
|
||||
fn default() -> Self { Self::new() }
|
||||
pub struct BlockMaterialRule {
|
||||
pub result_state: &'static BlockState,
|
||||
}
|
||||
|
||||
// ── Generated constants ───────────────────────────────────────────────
|
||||
pub struct SequenceMaterialRule {
|
||||
pub sequence: &'static [MaterialRule],
|
||||
}
|
||||
|
||||
pub struct ConditionMaterialRule {
|
||||
pub if_true: MaterialCondition,
|
||||
pub then_run: &'static MaterialRule,
|
||||
}
|
||||
|
||||
pub struct BadLandsMaterialRule;
|
||||
|
||||
pub enum MaterialRule {
|
||||
Block(BlockMaterialRule),
|
||||
Sequence(SequenceMaterialRule),
|
||||
Condition(ConditionMaterialRule),
|
||||
Badlands(BadLandsMaterialRule),
|
||||
}
|
||||
|
||||
|
||||
pub struct BiomeMaterialCondition {
|
||||
pub biome_is: &'static [&'static Biome],
|
||||
}
|
||||
|
||||
pub struct NoiseThresholdMaterialCondition {
|
||||
pub noise: DoublePerlinNoiseParameters,
|
||||
pub min_threshold: f64,
|
||||
pub max_threshold: f64,
|
||||
}
|
||||
|
||||
pub struct VerticalGradientMaterialCondition {
|
||||
pub random_lo: u64,
|
||||
pub random_hi: u64,
|
||||
pub true_at_and_below: YOffset,
|
||||
pub false_at_and_above: YOffset,
|
||||
}
|
||||
|
||||
pub struct AboveYMaterialCondition {
|
||||
pub anchor: YOffset,
|
||||
pub surface_depth_multiplier: i32,
|
||||
pub add_stone_depth: bool,
|
||||
}
|
||||
|
||||
pub struct WaterMaterialCondition {
|
||||
pub offset: i32,
|
||||
pub surface_depth_multiplier: i32,
|
||||
pub add_stone_depth: bool,
|
||||
}
|
||||
|
||||
pub struct HoleMaterialCondition;
|
||||
|
||||
pub struct NotMaterialCondition {
|
||||
pub invert: &'static MaterialCondition,
|
||||
}
|
||||
|
||||
pub struct SurfaceMaterialCondition;
|
||||
|
||||
pub struct StoneDepthMaterialCondition {
|
||||
pub offset: i32,
|
||||
pub add_surface_depth: bool,
|
||||
pub secondary_depth_range: i32,
|
||||
pub surface_type: pumpkin_util::math::vertical_surface_type::VerticalSurfaceType,
|
||||
}
|
||||
|
||||
pub enum MaterialCondition {
|
||||
Biome(BiomeMaterialCondition),
|
||||
NoiseThreshold(NoiseThresholdMaterialCondition),
|
||||
VerticalGradient(VerticalGradientMaterialCondition),
|
||||
YAbove(AboveYMaterialCondition),
|
||||
Water(WaterMaterialCondition),
|
||||
Temperature,
|
||||
Steep,
|
||||
Not(NotMaterialCondition),
|
||||
Hole(HoleMaterialCondition),
|
||||
AbovePreliminarySurface(SurfaceMaterialCondition),
|
||||
StoneDepth(StoneDepthMaterialCondition),
|
||||
}
|
||||
|
||||
impl GenerationSettings {
|
||||
#const_defs
|
||||
|
||||
pub fn from_dimension(dimension: &Dimension) -> &'static Self {
|
||||
match dimension {
|
||||
d if d == &Dimension::OVERWORLD => &Self::OVERWORLD,
|
||||
d if d == &Dimension::THE_NETHER => &Self::NETHER,
|
||||
_ => &Self::END,
|
||||
if dimension == &Dimension::OVERWORLD {
|
||||
&Self::OVERWORLD
|
||||
} else if dimension == &Dimension::THE_NETHER {
|
||||
&Self::NETHER
|
||||
} else {
|
||||
&Self::END
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -607,7 +607,7 @@ pub fn value_to_configured_feature(v: &Value) -> TokenStream {
|
||||
"minecraft:kelp" => {
|
||||
quote! { ConfiguredFeature::Kelp(crate::generation::feature::features::kelp::KelpFeature {}) }
|
||||
}
|
||||
|
||||
|
||||
// All TODO/empty features
|
||||
"minecraft:fossil" => {
|
||||
quote! { ConfiguredFeature::Fossil(crate::generation::feature::features::fossil::FossilFeature {}) }
|
||||
@@ -1033,9 +1033,8 @@ fn value_to_trunk_placer(v: &Value) -> TokenStream {
|
||||
}
|
||||
"minecraft:upwards_branching_trunk_placer" => {
|
||||
let extra_branch_steps = value_to_int_provider(&v["extra_branch_steps"]);
|
||||
let place_branch_per_log_probability = v["place_branch_per_log_probability"]
|
||||
.as_f64()
|
||||
.unwrap_or(0.0) as f32;
|
||||
let place_branch_per_log_probability =
|
||||
v["place_branch_per_log_probability"].as_f64().unwrap_or(0.0) as f32;
|
||||
let extra_branch_length = value_to_int_provider(&v["extra_branch_length"]);
|
||||
let can_grow_through = value_to_block_list(&v["can_grow_through"]);
|
||||
quote! {
|
||||
@@ -1053,8 +1052,7 @@ fn value_to_trunk_placer(v: &Value) -> TokenStream {
|
||||
let branch_start_offset_v = &v["branch_start_offset_from_top"];
|
||||
let min = branch_start_offset_v["min_inclusive"].as_i64().unwrap_or(0) as i32;
|
||||
let max = branch_start_offset_v["max_inclusive"].as_i64().unwrap_or(0) as i32;
|
||||
let branch_end_offset_from_top =
|
||||
value_to_int_provider(&v["branch_end_offset_from_top"]);
|
||||
let branch_end_offset_from_top = value_to_int_provider(&v["branch_end_offset_from_top"]);
|
||||
quote! {
|
||||
TrunkType::Cherry(CherryTrunkPlacer {
|
||||
branch_count: #branch_count,
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -48,7 +48,7 @@ use crate::generation::structure::structures::{
|
||||
StructureGeneratorContext, StructureInstance, create_chunk_random,
|
||||
};
|
||||
use crate::generation::structure::try_generate_structure;
|
||||
use crate::generation::surface::evaluate_surface_rule;
|
||||
use crate::generation::surface::rule::try_apply_material_rule;
|
||||
use crate::{
|
||||
BlockStateId,
|
||||
block::RawBlockState,
|
||||
@@ -879,7 +879,7 @@ impl ProtoChunk {
|
||||
context.block_pos_y,
|
||||
context.block_pos_z,
|
||||
);
|
||||
let new_state = evaluate_surface_rule(
|
||||
let new_state = try_apply_material_rule(
|
||||
&settings.surface_rule,
|
||||
self,
|
||||
&mut context,
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
use pumpkin_data::{
|
||||
BlockState,
|
||||
chunk::{Biome, DoublePerlinNoiseParameters},
|
||||
chunk_gen_settings::{ColumnNoiseCache, CompiledSurfaceRule, SurfaceInstruction},
|
||||
chunk::Biome,
|
||||
chunk_gen_settings::{
|
||||
AboveYMaterialCondition, MaterialCondition, NoiseThresholdMaterialCondition,
|
||||
NotMaterialCondition, StoneDepthMaterialCondition, VerticalGradientMaterialCondition,
|
||||
WaterMaterialCondition,
|
||||
},
|
||||
};
|
||||
use pumpkin_util::{
|
||||
math::{lerp2, vertical_surface_type::VerticalSurfaceType},
|
||||
random::{RandomImpl, xoroshiro128::XoroshiroSplitter},
|
||||
y_offset::YOffset,
|
||||
};
|
||||
|
||||
use terrain::SurfaceTerrainBuilder;
|
||||
@@ -24,6 +26,7 @@ use super::{
|
||||
},
|
||||
};
|
||||
|
||||
pub mod rule;
|
||||
pub mod terrain;
|
||||
|
||||
pub struct MaterialRuleContext<'a> {
|
||||
@@ -49,7 +52,6 @@ pub struct MaterialRuleContext<'a> {
|
||||
pub stone_depth_above: i32,
|
||||
pub terrain_builder: &'a SurfaceTerrainBuilder,
|
||||
pub sea_level: i32,
|
||||
pub noise_cache: ColumnNoiseCache,
|
||||
}
|
||||
|
||||
impl<'a> MaterialRuleContext<'a> {
|
||||
@@ -86,7 +88,6 @@ impl<'a> MaterialRuleContext<'a> {
|
||||
stone_depth_below: 0,
|
||||
stone_depth_above: 0,
|
||||
sea_level,
|
||||
noise_cache: ColumnNoiseCache::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -134,233 +135,110 @@ impl<'a> MaterialRuleContext<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn evaluate_surface_rule(
|
||||
rule: &CompiledSurfaceRule,
|
||||
#[expect(clippy::similar_names)]
|
||||
pub fn test_condition(
|
||||
condition: &MaterialCondition,
|
||||
chunk: &mut ProtoChunk,
|
||||
ctx: &mut MaterialRuleContext,
|
||||
sampler: &mut SurfaceHeightEstimateSampler,
|
||||
) -> Option<&'static BlockState> {
|
||||
let instrs = &rule.instructions;
|
||||
let mut pc = 0usize;
|
||||
let x = ctx.block_pos_x;
|
||||
let z = ctx.block_pos_z;
|
||||
let deriver = ctx.random_deriver;
|
||||
while pc < instrs.len() {
|
||||
match &instrs[pc] {
|
||||
SurfaceInstruction::PlaceBlock { state } => return Some(state),
|
||||
|
||||
SurfaceInstruction::PlaceBadlands => {
|
||||
return Some(get_badlands_block(ctx));
|
||||
}
|
||||
|
||||
SurfaceInstruction::TestBiome { biome_is, skip } => {
|
||||
if !biome_is.iter().any(|b| std::ptr::eq(*b, ctx.biome)) {
|
||||
pc += *skip as usize;
|
||||
}
|
||||
}
|
||||
|
||||
SurfaceInstruction::TestNoiseAbove { noise, min, skip } => {
|
||||
let v = ctx
|
||||
.noise_cache
|
||||
.get(noise, || sample_noise(noise, deriver, x, z));
|
||||
if v < *min {
|
||||
pc += *skip as usize;
|
||||
}
|
||||
}
|
||||
|
||||
SurfaceInstruction::TestNoiseRange {
|
||||
noise,
|
||||
min,
|
||||
max,
|
||||
skip,
|
||||
} => {
|
||||
let v = ctx
|
||||
.noise_cache
|
||||
.get(noise, || sample_noise(noise, deriver, x, z));
|
||||
if v < *min || v > *max {
|
||||
pc += *skip as usize;
|
||||
}
|
||||
}
|
||||
|
||||
SurfaceInstruction::TestVerticalGradient {
|
||||
random_lo,
|
||||
random_hi,
|
||||
true_at_and_below,
|
||||
false_at_and_above,
|
||||
skip,
|
||||
} => {
|
||||
if !test_vertical_gradient(
|
||||
ctx,
|
||||
*random_lo,
|
||||
*random_hi,
|
||||
true_at_and_below,
|
||||
false_at_and_above,
|
||||
) {
|
||||
pc += *skip as usize;
|
||||
}
|
||||
}
|
||||
|
||||
SurfaceInstruction::TestYAbove {
|
||||
anchor,
|
||||
surface_depth_multiplier,
|
||||
add_stone_depth,
|
||||
skip,
|
||||
} => {
|
||||
if !test_y_above(ctx, anchor, *surface_depth_multiplier, *add_stone_depth) {
|
||||
pc += *skip as usize;
|
||||
}
|
||||
}
|
||||
|
||||
SurfaceInstruction::TestWater {
|
||||
offset,
|
||||
surface_depth_multiplier,
|
||||
add_stone_depth,
|
||||
skip,
|
||||
} => {
|
||||
if !test_water(ctx, *offset, *surface_depth_multiplier, *add_stone_depth) {
|
||||
pc += *skip as usize;
|
||||
}
|
||||
}
|
||||
|
||||
SurfaceInstruction::TestStoneDepth {
|
||||
offset,
|
||||
add_surface_depth,
|
||||
secondary_depth_range,
|
||||
surface_type,
|
||||
skip,
|
||||
} => {
|
||||
if !test_stone_depth(
|
||||
ctx,
|
||||
*offset,
|
||||
*add_surface_depth,
|
||||
*secondary_depth_range,
|
||||
surface_type,
|
||||
) {
|
||||
pc += *skip as usize;
|
||||
}
|
||||
}
|
||||
|
||||
SurfaceInstruction::TestAbovePreliminarySurface { skip } => {
|
||||
if !test_above_preliminary_surface(ctx, sampler) {
|
||||
pc += *skip as usize;
|
||||
}
|
||||
}
|
||||
|
||||
SurfaceInstruction::TestHole { skip } => {
|
||||
if !test_hole(ctx) {
|
||||
pc += *skip as usize;
|
||||
}
|
||||
}
|
||||
|
||||
SurfaceInstruction::TestSteep { skip } => {
|
||||
if !test_steep(ctx, chunk) {
|
||||
pc += *skip as usize;
|
||||
}
|
||||
}
|
||||
|
||||
SurfaceInstruction::TestTemperature { skip } => {
|
||||
if !test_temperature(ctx) {
|
||||
pc += *skip as usize;
|
||||
}
|
||||
}
|
||||
|
||||
SurfaceInstruction::TestNot { inner, skip } => {
|
||||
let passed = eval_single_instruction(inner, chunk, ctx, sampler);
|
||||
if passed {
|
||||
pc += *skip as usize;
|
||||
}
|
||||
}
|
||||
}
|
||||
pc += 1;
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn eval_single_instruction(
|
||||
instr: &SurfaceInstruction,
|
||||
chunk: &mut ProtoChunk,
|
||||
ctx: &mut MaterialRuleContext,
|
||||
sampler: &mut SurfaceHeightEstimateSampler,
|
||||
context: &mut MaterialRuleContext,
|
||||
surface_height_estimate_sampler: &mut SurfaceHeightEstimateSampler,
|
||||
) -> bool {
|
||||
let x = ctx.block_pos_x;
|
||||
let z = ctx.block_pos_z;
|
||||
let deriver = ctx.random_deriver;
|
||||
match instr {
|
||||
SurfaceInstruction::TestBiome { biome_is, .. } => {
|
||||
biome_is.iter().any(|b| std::ptr::eq(*b, ctx.biome))
|
||||
match condition {
|
||||
MaterialCondition::Biome(biome) => BiomeMaterialCondition::test(biome.biome_is, context),
|
||||
MaterialCondition::NoiseThreshold(noise_threshold) => {
|
||||
test_noise_threshold(noise_threshold, context)
|
||||
}
|
||||
SurfaceInstruction::TestNoiseAbove { noise, min, .. } => {
|
||||
ctx.noise_cache
|
||||
.get(noise, || sample_noise(noise, deriver, x, z))
|
||||
>= *min
|
||||
MaterialCondition::VerticalGradient(vertical_gradient) => {
|
||||
test_vertical_gradient(vertical_gradient, context)
|
||||
}
|
||||
SurfaceInstruction::TestNoiseRange {
|
||||
noise, min, max, ..
|
||||
} => {
|
||||
let v = ctx
|
||||
.noise_cache
|
||||
.get(noise, || sample_noise(noise, deriver, x, z));
|
||||
v >= *min && v <= *max
|
||||
MaterialCondition::YAbove(above_y) => test_above_y_material(above_y, context),
|
||||
MaterialCondition::Water(water) => test_water_material(water, context),
|
||||
MaterialCondition::Temperature => {
|
||||
let temperature = context.biome.weather.compute_temperature(
|
||||
context.block_pos_x as f64,
|
||||
context.block_pos_y,
|
||||
context.block_pos_z as f64,
|
||||
context.sea_level,
|
||||
);
|
||||
temperature < 0.15f32
|
||||
}
|
||||
SurfaceInstruction::TestHole { .. } => test_hole(ctx),
|
||||
SurfaceInstruction::TestSteep { .. } => test_steep(ctx, chunk),
|
||||
SurfaceInstruction::TestTemperature { .. } => test_temperature(ctx),
|
||||
SurfaceInstruction::TestYAbove {
|
||||
anchor,
|
||||
surface_depth_multiplier,
|
||||
add_stone_depth,
|
||||
..
|
||||
} => test_y_above(ctx, anchor, *surface_depth_multiplier, *add_stone_depth),
|
||||
SurfaceInstruction::TestWater {
|
||||
offset,
|
||||
surface_depth_multiplier,
|
||||
add_stone_depth,
|
||||
..
|
||||
} => test_water(ctx, *offset, *surface_depth_multiplier, *add_stone_depth),
|
||||
SurfaceInstruction::TestAbovePreliminarySurface { .. } => {
|
||||
test_above_preliminary_surface(ctx, sampler)
|
||||
MaterialCondition::Steep => {
|
||||
let local_x = context.block_pos_x & 15;
|
||||
let local_z = context.block_pos_z & 15;
|
||||
|
||||
let local_z_sub = 0.max(local_z - 1);
|
||||
let local_z_add = 15.min(local_z + 1);
|
||||
|
||||
let sub_height = chunk.top_block_height_exclusive(local_x, local_z_sub);
|
||||
let add_height = chunk.top_block_height_exclusive(local_x, local_z_add);
|
||||
|
||||
if add_height >= sub_height + 4 {
|
||||
true
|
||||
} else {
|
||||
let local_x_sub = 0.max(local_x - 1);
|
||||
let local_x_add = 15.min(local_x + 1);
|
||||
|
||||
let sub_height = chunk.top_block_height_exclusive(local_x_sub, local_z);
|
||||
let add_height = chunk.top_block_height_exclusive(local_x_add, local_z);
|
||||
|
||||
sub_height >= add_height + 4
|
||||
}
|
||||
}
|
||||
_ => false,
|
||||
MaterialCondition::Not(not) => {
|
||||
test_not_material(not, chunk, context, surface_height_estimate_sampler)
|
||||
}
|
||||
MaterialCondition::Hole(_hole) => HoleMaterialCondition::test(context),
|
||||
MaterialCondition::AbovePreliminarySurface(_above) => {
|
||||
SurfaceMaterialCondition::test(context, surface_height_estimate_sampler)
|
||||
}
|
||||
MaterialCondition::StoneDepth(stone_depth) => test_stone_depth(stone_depth, context),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get_badlands_block(context: &MaterialRuleContext) -> &'static BlockState {
|
||||
context.terrain_builder.get_terracotta_block(
|
||||
context.block_pos_x,
|
||||
context.block_pos_y,
|
||||
context.block_pos_z,
|
||||
)
|
||||
pub struct HoleMaterialCondition;
|
||||
|
||||
impl HoleMaterialCondition {
|
||||
pub const fn test(context: &MaterialRuleContext) -> bool {
|
||||
context.run_depth <= 0
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub const fn test_hole(context: &MaterialRuleContext) -> bool {
|
||||
context.run_depth <= 0
|
||||
}
|
||||
|
||||
pub const fn test_y_above(
|
||||
pub const fn test_above_y_material(
|
||||
condition: &AboveYMaterialCondition,
|
||||
context: &MaterialRuleContext,
|
||||
anchor: &YOffset,
|
||||
surface_depth_multiplier: i32,
|
||||
add_stone_depth: bool,
|
||||
) -> bool {
|
||||
context.block_pos_y
|
||||
+ if add_stone_depth {
|
||||
+ if condition.add_stone_depth {
|
||||
context.stone_depth_above
|
||||
} else {
|
||||
0
|
||||
}
|
||||
>= anchor.get_y(context.min_y as i16, context.height)
|
||||
+ context.run_depth * surface_depth_multiplier
|
||||
>= condition.anchor.get_y(context.min_y as i16, context.height)
|
||||
+ context.run_depth * condition.surface_depth_multiplier
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn test_above_preliminary_surface(
|
||||
pub fn test_not_material(
|
||||
condition: &NotMaterialCondition,
|
||||
chunk: &mut ProtoChunk,
|
||||
context: &mut MaterialRuleContext,
|
||||
surface_height_estimate_sampler: &mut SurfaceHeightEstimateSampler,
|
||||
) -> bool {
|
||||
context.block_pos_y >= estimate_surface_height(context, surface_height_estimate_sampler)
|
||||
!test_condition(
|
||||
condition.invert,
|
||||
chunk,
|
||||
context,
|
||||
surface_height_estimate_sampler,
|
||||
)
|
||||
}
|
||||
|
||||
pub struct SurfaceMaterialCondition;
|
||||
|
||||
impl SurfaceMaterialCondition {
|
||||
pub fn test(
|
||||
context: &mut MaterialRuleContext,
|
||||
surface_height_estimate_sampler: &mut SurfaceHeightEstimateSampler,
|
||||
) -> bool {
|
||||
context.block_pos_y >= estimate_surface_height(context, surface_height_estimate_sampler)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn estimate_surface_height(
|
||||
@@ -405,33 +283,41 @@ pub fn estimate_surface_height(
|
||||
context.surface_min_y
|
||||
}
|
||||
|
||||
pub fn sample_noise(
|
||||
parameters: &DoublePerlinNoiseParameters,
|
||||
random_deriver: &XoroshiroSplitter,
|
||||
x: i32,
|
||||
z: i32,
|
||||
) -> f64 {
|
||||
let sampler = DoublePerlinNoiseBuilder::get_noise_sampler_for_id(random_deriver, parameters);
|
||||
sampler.sample(x as f64, 0.0, z as f64)
|
||||
pub struct BiomeMaterialCondition;
|
||||
|
||||
impl BiomeMaterialCondition {
|
||||
pub fn test(biome_is: &[&'static Biome], context: &MaterialRuleContext) -> bool {
|
||||
biome_is.contains(&context.biome)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn test_noise_threshold(
|
||||
condition: &NoiseThresholdMaterialCondition,
|
||||
context: &mut MaterialRuleContext,
|
||||
) -> bool {
|
||||
// TODO: we want to cache these
|
||||
let sampler = DoublePerlinNoiseBuilder::get_noise_sampler_for_id(
|
||||
context.random_deriver,
|
||||
&condition.noise,
|
||||
);
|
||||
let value = sampler.sample(context.block_pos_x as f64, 0.0, context.block_pos_z as f64);
|
||||
value >= condition.min_threshold && value <= condition.max_threshold
|
||||
}
|
||||
|
||||
pub fn test_stone_depth(
|
||||
condition: &StoneDepthMaterialCondition,
|
||||
context: &mut MaterialRuleContext,
|
||||
offset: i32,
|
||||
add_surface_depth: bool,
|
||||
secondary_depth_range: i32,
|
||||
surface_type: &VerticalSurfaceType,
|
||||
) -> bool {
|
||||
let stone_depth = match surface_type {
|
||||
let stone_depth = match &condition.surface_type {
|
||||
VerticalSurfaceType::Ceiling => context.stone_depth_below,
|
||||
VerticalSurfaceType::Floor => context.stone_depth_above,
|
||||
};
|
||||
let depth = if add_surface_depth {
|
||||
let depth = if condition.add_surface_depth {
|
||||
context.run_depth
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let depth_range = if secondary_depth_range == 0 {
|
||||
let depth_range = if condition.secondary_depth_range == 0 {
|
||||
0
|
||||
} else {
|
||||
pumpkin_util::math::map(
|
||||
@@ -439,37 +325,40 @@ pub fn test_stone_depth(
|
||||
-1.0,
|
||||
1.0,
|
||||
0.0,
|
||||
secondary_depth_range as f64,
|
||||
condition.secondary_depth_range as f64,
|
||||
) as i32
|
||||
};
|
||||
stone_depth <= 1 + offset + depth + depth_range
|
||||
stone_depth <= 1 + condition.offset + depth + depth_range
|
||||
}
|
||||
|
||||
pub const fn test_water(
|
||||
pub const fn test_water_material(
|
||||
condition: &WaterMaterialCondition,
|
||||
context: &MaterialRuleContext,
|
||||
offset: i32,
|
||||
surface_depth_multiplier: i32,
|
||||
add_stone_depth: bool,
|
||||
) -> bool {
|
||||
context.fluid_height == i32::MIN
|
||||
|| context.block_pos_y
|
||||
+ (if add_stone_depth {
|
||||
+ (if condition.add_stone_depth {
|
||||
context.stone_depth_above
|
||||
} else {
|
||||
0
|
||||
})
|
||||
>= context.fluid_height + offset + context.run_depth * surface_depth_multiplier
|
||||
>= context.fluid_height
|
||||
+ condition.offset
|
||||
+ context.run_depth * condition.surface_depth_multiplier
|
||||
}
|
||||
|
||||
// random_deriver: ThreadLocal<RefCell<LruCache<usize, RandomDeriver>>>,
|
||||
|
||||
pub fn test_vertical_gradient(
|
||||
condition: &VerticalGradientMaterialCondition,
|
||||
context: &MaterialRuleContext,
|
||||
random_lo: u64,
|
||||
random_hi: u64,
|
||||
true_at_and_below: &YOffset,
|
||||
false_at_and_above: &YOffset,
|
||||
) -> bool {
|
||||
let true_at = true_at_and_below.get_y(context.min_y as i16, context.height);
|
||||
let false_at = false_at_and_above.get_y(context.min_y as i16, context.height);
|
||||
let true_at = condition
|
||||
.true_at_and_below
|
||||
.get_y(context.min_y as i16, context.height);
|
||||
let false_at = condition
|
||||
.false_at_and_above
|
||||
.get_y(context.min_y as i16, context.height);
|
||||
|
||||
let block_y = context.block_pos_y;
|
||||
if block_y <= true_at {
|
||||
@@ -480,42 +369,9 @@ pub fn test_vertical_gradient(
|
||||
}
|
||||
let splitter = context
|
||||
.random_deriver
|
||||
.from_lo_and_hi(random_lo, random_hi)
|
||||
.from_lo_and_hi(condition.random_lo, condition.random_hi)
|
||||
.next_splitter();
|
||||
let mapped = pumpkin_util::math::map(block_y as f32, true_at as f32, false_at as f32, 1.0, 0.0);
|
||||
let mut random = splitter.split_pos(context.block_pos_x, block_y, context.block_pos_z);
|
||||
random.next_f32() < mapped
|
||||
}
|
||||
|
||||
pub fn test_steep(context: &MaterialRuleContext, chunk: &ProtoChunk) -> bool {
|
||||
let local_x = context.block_pos_x & 15;
|
||||
let local_z = context.block_pos_z & 15;
|
||||
|
||||
let local_z_sub = 0.max(local_z - 1);
|
||||
let local_z_add = 15.min(local_z + 1);
|
||||
|
||||
let sub_height = chunk.top_block_height_exclusive(local_x, local_z_sub);
|
||||
let add_height = chunk.top_block_height_exclusive(local_x, local_z_add);
|
||||
|
||||
if add_height >= sub_height + 4 {
|
||||
true
|
||||
} else {
|
||||
let local_x_sub = 0.max(local_x - 1);
|
||||
let local_x_add = 15.min(local_x + 1);
|
||||
|
||||
let sub_height = chunk.top_block_height_exclusive(local_x_sub, local_z);
|
||||
let add_height = chunk.top_block_height_exclusive(local_x_add, local_z);
|
||||
|
||||
sub_height >= add_height + 4
|
||||
}
|
||||
}
|
||||
|
||||
pub fn test_temperature(context: &MaterialRuleContext) -> bool {
|
||||
let temperature = context.biome.weather.compute_temperature(
|
||||
context.block_pos_x as f64,
|
||||
context.block_pos_y,
|
||||
context.block_pos_z as f64,
|
||||
context.sea_level,
|
||||
);
|
||||
temperature < 0.15f32
|
||||
}
|
||||
|
||||
89
pumpkin-world/src/generation/surface/rule.rs
Normal file
89
pumpkin-world/src/generation/surface/rule.rs
Normal file
@@ -0,0 +1,89 @@
|
||||
use pumpkin_data::{
|
||||
BlockState,
|
||||
chunk_gen_settings::{ConditionMaterialRule, MaterialRule, SequenceMaterialRule},
|
||||
};
|
||||
|
||||
use super::MaterialRuleContext;
|
||||
use crate::{
|
||||
ProtoChunk,
|
||||
generation::{
|
||||
noise::router::surface_height_sampler::SurfaceHeightEstimateSampler,
|
||||
surface::test_condition,
|
||||
},
|
||||
};
|
||||
|
||||
pub fn try_apply_material_rule(
|
||||
rule: &MaterialRule,
|
||||
chunk: &mut ProtoChunk,
|
||||
context: &mut MaterialRuleContext,
|
||||
surface_height_estimate_sampler: &mut SurfaceHeightEstimateSampler,
|
||||
) -> Option<&'static BlockState> {
|
||||
match rule {
|
||||
MaterialRule::Badlands(_badlands) => Some(BadLandsMaterialRule::try_apply(context)),
|
||||
MaterialRule::Block(block) => Some(BlockMaterialRule::try_apply(block.result_state)),
|
||||
MaterialRule::Sequence(sequence) => {
|
||||
try_apply_sequence(sequence, chunk, context, surface_height_estimate_sampler)
|
||||
}
|
||||
MaterialRule::Condition(condition) => {
|
||||
try_apply_condition(condition, chunk, context, surface_height_estimate_sampler)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct BadLandsMaterialRule;
|
||||
|
||||
impl BadLandsMaterialRule {
|
||||
pub fn try_apply(context: &mut MaterialRuleContext) -> &'static BlockState {
|
||||
context.terrain_builder.get_terracotta_block(
|
||||
context.block_pos_x,
|
||||
context.block_pos_y,
|
||||
context.block_pos_z,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct BlockMaterialRule;
|
||||
|
||||
impl BlockMaterialRule {
|
||||
pub const fn try_apply(state: &'static BlockState) -> &'static BlockState {
|
||||
state
|
||||
}
|
||||
}
|
||||
|
||||
pub fn try_apply_sequence(
|
||||
rule: &SequenceMaterialRule,
|
||||
chunk: &mut ProtoChunk,
|
||||
context: &mut MaterialRuleContext,
|
||||
surface_height_estimate_sampler: &mut SurfaceHeightEstimateSampler,
|
||||
) -> Option<&'static BlockState> {
|
||||
for seq in rule.sequence {
|
||||
if let Some(state) =
|
||||
try_apply_material_rule(seq, chunk, context, surface_height_estimate_sampler)
|
||||
{
|
||||
return Some(state);
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn try_apply_condition(
|
||||
rule: &ConditionMaterialRule,
|
||||
chunk: &mut ProtoChunk,
|
||||
context: &mut MaterialRuleContext,
|
||||
surface_height_estimate_sampler: &mut SurfaceHeightEstimateSampler,
|
||||
) -> Option<&'static BlockState> {
|
||||
if test_condition(
|
||||
&rule.if_true,
|
||||
chunk,
|
||||
context,
|
||||
surface_height_estimate_sampler,
|
||||
) {
|
||||
return try_apply_material_rule(
|
||||
rule.then_run,
|
||||
chunk,
|
||||
context,
|
||||
surface_height_estimate_sampler,
|
||||
);
|
||||
}
|
||||
None
|
||||
}
|
||||
Reference in New Issue
Block a user