chore(performance): flat compiled surface rules, no recursion at runtime

Decreased the surface function from taking 18% to 7%
This commit is contained in:
Alexander Medvedev 2026-04-20 22:31:32 +02:00
parent cddb7e3f6a
commit 31a5687937
14 changed files with 6792 additions and 844 deletions

View File

@ -1,459 +1,395 @@
use heck::ToShoutySnakeCase;
use proc_macro2::TokenStream;
use quote::{ToTokens, format_ident, quote};
use quote::{format_ident, quote};
use serde::Deserialize;
use std::{collections::BTreeMap, fs};
/// Deserialized block reference used in chunk generation settings (e.g., default block or fluid).
// ── Deserialized input types (unchanged from original) ────────────────────────
#[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 {
/// The block state to place.
result_state: BlockStateCodecStruct,
},
/// Evaluate each rule in order, stopping at the first match.
Block { result_state: BlockStateCodecStruct },
#[serde(rename = "minecraft:sequence")]
Sequence {
/// The ordered list of child rules.
sequence: Vec<Self>,
},
/// Apply `then_run` only when `if_true` evaluates to true.
Sequence { sequence: Vec<Self> },
#[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 {
/// List of biome resource locations to match against.
biome_is: Vec<String>,
},
/// True when a named noise value is within the given range.
Biome { biome_is: Vec<String> },
#[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 {
/// The condition to invert.
invert: Box<Self>,
},
/// True when there is a hole (cave opening) at the position.
Not { invert: Box<Self> },
#[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 {
/// 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,
},
Absolute { absolute: i16 },
AboveBottom { above_bottom: i8 },
BelowTop { below_top: i8 },
}
// --- ToTokens Implementations ---
// ── Flat bytecode compiler ────────────────────────────────────────────────────
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
});
/// 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 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;
/// 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,
})
}
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::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,
})
}
));
}
}
}
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::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 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 })));
}
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 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();
// ── Small token helpers ───────────────────────────────────────────────────────
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 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::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 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 }))
}
}
}
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 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,
})
}
/// Reads `chunk_gen_settings.json` and emits the complete chunk generation settings `TokenStream`.
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 ───────────────────────────────────────────────────────────────
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 settings.json");
.expect("Failed to parse chunk_gen_settings.json");
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;
));
}
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();
quote!(
use crate::dimension::Dimension;
use crate::chunk::DoublePerlinNoiseParameters;
use crate::BlockState;
use std::{cell::RefCell, num::NonZeroUsize};
use pumpkin_util::random::RandomDeriver;
use pumpkin_util::y_offset::YOffset;
use pumpkin_util::math::vertical_surface_type::VerticalSurfaceType;
use crate::biome::Biome;
use pumpkin_util::y_offset::Absolute;
// ── Core settings struct ──────────────────────────────────────────────
pub struct GenerationSettings {
pub aquifers_enabled: bool,
@ -461,9 +397,10 @@ pub fn build() -> TokenStream {
pub legacy_random_source: bool,
pub sea_level: i32,
pub default_fluid: &'static BlockState,
pub shape: GenerationShapeConfig,
pub surface_rule: MaterialRule,
pub default_block: &'static BlockState,
pub shape: GenerationShapeConfig,
/// Flat compiled surface rule — no recursion at runtime.
pub surface_rule: CompiledSurfaceRule,
}
pub struct GenerationShapeConfig {
@ -474,12 +411,9 @@ 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]
@ -504,107 +438,148 @@ 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,
}
Self { min_y: new_min, height: new_height,
size_horizontal: self.size_horizontal, size_vertical: self.size_vertical }
}
}
pub struct BlockMaterialRule {
pub result_state: &'static BlockState,
// ── 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],
}
pub struct SequenceMaterialRule {
pub sequence: &'static [MaterialRule],
/// 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,
},
}
pub struct ConditionMaterialRule {
pub if_true: MaterialCondition,
pub then_run: &'static MaterialRule,
// ── 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,
}
pub struct BadLandsMaterialRule;
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,
}
}
pub enum MaterialRule {
Block(BlockMaterialRule),
Sequence(SequenceMaterialRule),
Condition(ConditionMaterialRule),
Badlands(BadLandsMaterialRule),
/// 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]
}
}
pub struct BiomeMaterialCondition {
pub biome_is: &'static [&'static Biome],
impl Default for ColumnNoiseCache {
fn default() -> Self { Self::new() }
}
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),
}
// ── Generated constants ───────────────────────────────────────────────
impl GenerationSettings {
#const_defs
pub fn from_dimension(dimension: &Dimension) -> &'static Self {
if dimension == &Dimension::OVERWORLD {
&Self::OVERWORLD
} else if dimension == &Dimension::THE_NETHER {
&Self::NETHER
} else {
&Self::END
match dimension {
d if d == &Dimension::OVERWORLD => &Self::OVERWORLD,
d if d == &Dimension::THE_NETHER => &Self::NETHER,
_ => &Self::END,
}
}
}

View File

@ -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 {}) }
@ -1019,8 +1019,9 @@ 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! {
@ -1038,7 +1039,8 @@ 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,

View File

@ -12,41 +12,53 @@ pub fn build() -> TokenStream {
let mut variants = TokenStream::new();
let mut match_variants = TokenStream::new();
for (raw_name, parameter) in &json {
for (i, (raw_name, parameter)) in json.iter().enumerate() {
let name = raw_name
.strip_prefix("minecraft:")
.unwrap()
.replace("/", "_");
let simple_id = name.clone();
let name = format_ident!("{}", name.to_uppercase());
let name_ident = format_ident!("{}", name.to_uppercase());
let first_octave = parameter.first_octave;
let amplitudes = &parameter.amplitudes;
variants.extend([quote! {
pub const #name: DoublePerlinNoiseParameters = DoublePerlinNoiseParameters::new(#first_octave, &[#(#amplitudes),*], #raw_name);
pub const #name_ident: DoublePerlinNoiseParameters = DoublePerlinNoiseParameters::new(
#i,
#first_octave,
&[#(#amplitudes),*],
#raw_name
);
}]);
match_variants.extend([quote! {
#simple_id => &Self::#name,
#simple_id => &Self::#name_ident,
}]);
}
let count = json.len();
quote! {
pub struct DoublePerlinNoiseParameters {
pub id: usize,
pub first_octave: i32,
pub amplitudes: &'static [f64],
id: &'static str,
name: &'static str,
}
impl DoublePerlinNoiseParameters {
pub const fn new(first_octave: i32, amplitudes: &'static [f64], id: &'static str) -> Self {
pub const COUNT: usize = #count;
pub const fn new(id: usize, first_octave: i32, amplitudes: &'static [f64], name: &'static str) -> Self {
Self {
id,
first_octave,
amplitudes,
id
name
}
}
pub const fn id(&self) -> &'static str {
self.id
pub const fn name(&self) -> &'static str {
self.name
}
pub fn id_to_parameters(id: &str) -> Option<&DoublePerlinNoiseParameters> {
@ -55,6 +67,7 @@ pub fn build() -> TokenStream {
_ => return None,
})
}
#variants
}

File diff suppressed because one or more lines are too long

View File

@ -1,19 +1,27 @@
/* This file is generated. Do not edit manually. */
pub struct DoublePerlinNoiseParameters {
pub id: usize,
pub first_octave: i32,
pub amplitudes: &'static [f64],
id: &'static str,
name: &'static str,
}
impl DoublePerlinNoiseParameters {
pub const fn new(first_octave: i32, amplitudes: &'static [f64], id: &'static str) -> Self {
pub const COUNT: usize = 62usize;
pub const fn new(
id: usize,
first_octave: i32,
amplitudes: &'static [f64],
name: &'static str,
) -> Self {
Self {
id,
first_octave,
amplitudes,
id,
name,
}
}
pub const fn id(&self) -> &'static str {
self.id
pub const fn name(&self) -> &'static str {
self.name
}
pub fn id_to_parameters(id: &str) -> Option<&DoublePerlinNoiseParameters> {
Some(match id {
@ -83,62 +91,91 @@ impl DoublePerlinNoiseParameters {
})
}
pub const AQUIFER_BARRIER: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-3i32, &[1f64], "minecraft:aquifer_barrier");
DoublePerlinNoiseParameters::new(0usize, -3i32, &[1f64], "minecraft:aquifer_barrier");
pub const AQUIFER_FLUID_LEVEL_FLOODEDNESS: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(
1usize,
-7i32,
&[1f64],
"minecraft:aquifer_fluid_level_floodedness",
);
pub const AQUIFER_FLUID_LEVEL_SPREAD: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-5i32, &[1f64], "minecraft:aquifer_fluid_level_spread");
DoublePerlinNoiseParameters::new(
2usize,
-5i32,
&[1f64],
"minecraft:aquifer_fluid_level_spread",
);
pub const AQUIFER_LAVA: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-1i32, &[1f64], "minecraft:aquifer_lava");
DoublePerlinNoiseParameters::new(3usize, -1i32, &[1f64], "minecraft:aquifer_lava");
pub const BADLANDS_PILLAR: DoublePerlinNoiseParameters = DoublePerlinNoiseParameters::new(
4usize,
-2i32,
&[1f64, 1f64, 1f64, 1f64],
"minecraft:badlands_pillar",
);
pub const BADLANDS_PILLAR_ROOF: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-8i32, &[1f64], "minecraft:badlands_pillar_roof");
pub const BADLANDS_SURFACE: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-6i32, &[1f64, 1f64, 1f64], "minecraft:badlands_surface");
pub const CALCITE: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-9i32, &[1f64, 1f64, 1f64, 1f64], "minecraft:calcite");
DoublePerlinNoiseParameters::new(5usize, -8i32, &[1f64], "minecraft:badlands_pillar_roof");
pub const BADLANDS_SURFACE: DoublePerlinNoiseParameters = DoublePerlinNoiseParameters::new(
6usize,
-6i32,
&[1f64, 1f64, 1f64],
"minecraft:badlands_surface",
);
pub const CALCITE: DoublePerlinNoiseParameters = DoublePerlinNoiseParameters::new(
7usize,
-9i32,
&[1f64, 1f64, 1f64, 1f64],
"minecraft:calcite",
);
pub const CAVE_CHEESE: DoublePerlinNoiseParameters = DoublePerlinNoiseParameters::new(
8usize,
-8i32,
&[0.5f64, 1f64, 2f64, 1f64, 2f64, 1f64, 0f64, 2f64, 0f64],
"minecraft:cave_cheese",
);
pub const CAVE_ENTRANCE: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-7i32, &[0.4f64, 0.5f64, 1f64], "minecraft:cave_entrance");
pub const CAVE_ENTRANCE: DoublePerlinNoiseParameters = DoublePerlinNoiseParameters::new(
9usize,
-7i32,
&[0.4f64, 0.5f64, 1f64],
"minecraft:cave_entrance",
);
pub const CAVE_LAYER: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-8i32, &[1f64], "minecraft:cave_layer");
DoublePerlinNoiseParameters::new(10usize, -8i32, &[1f64], "minecraft:cave_layer");
pub const CLAY_BANDS_OFFSET: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-8i32, &[1f64], "minecraft:clay_bands_offset");
DoublePerlinNoiseParameters::new(11usize, -8i32, &[1f64], "minecraft:clay_bands_offset");
pub const CONTINENTALNESS: DoublePerlinNoiseParameters = DoublePerlinNoiseParameters::new(
12usize,
-9i32,
&[1f64, 1f64, 2f64, 2f64, 2f64, 1f64, 1f64, 1f64, 1f64],
"minecraft:continentalness",
);
pub const CONTINENTALNESS_LARGE: DoublePerlinNoiseParameters = DoublePerlinNoiseParameters::new(
13usize,
-11i32,
&[1f64, 1f64, 2f64, 2f64, 2f64, 1f64, 1f64, 1f64, 1f64],
"minecraft:continentalness_large",
);
pub const EROSION: DoublePerlinNoiseParameters = DoublePerlinNoiseParameters::new(
14usize,
-9i32,
&[1f64, 1f64, 0f64, 1f64, 1f64],
"minecraft:erosion",
);
pub const EROSION_LARGE: DoublePerlinNoiseParameters = DoublePerlinNoiseParameters::new(
15usize,
-11i32,
&[1f64, 1f64, 0f64, 1f64, 1f64],
"minecraft:erosion_large",
);
pub const GRAVEL: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-8i32, &[1f64, 1f64, 1f64, 1f64], "minecraft:gravel");
pub const GRAVEL: DoublePerlinNoiseParameters = DoublePerlinNoiseParameters::new(
16usize,
-8i32,
&[1f64, 1f64, 1f64, 1f64],
"minecraft:gravel",
);
pub const GRAVEL_LAYER: DoublePerlinNoiseParameters = DoublePerlinNoiseParameters::new(
17usize,
-8i32,
&[
1f64,
@ -153,18 +190,28 @@ impl DoublePerlinNoiseParameters {
],
"minecraft:gravel_layer",
);
pub const ICE: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-4i32, &[1f64, 1f64, 1f64, 1f64], "minecraft:ice");
pub const ICE: DoublePerlinNoiseParameters = DoublePerlinNoiseParameters::new(
18usize,
-4i32,
&[1f64, 1f64, 1f64, 1f64],
"minecraft:ice",
);
pub const ICEBERG_PILLAR: DoublePerlinNoiseParameters = DoublePerlinNoiseParameters::new(
19usize,
-6i32,
&[1f64, 1f64, 1f64, 1f64],
"minecraft:iceberg_pillar",
);
pub const ICEBERG_PILLAR_ROOF: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-3i32, &[1f64], "minecraft:iceberg_pillar_roof");
pub const ICEBERG_SURFACE: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-6i32, &[1f64, 1f64, 1f64], "minecraft:iceberg_surface");
DoublePerlinNoiseParameters::new(20usize, -3i32, &[1f64], "minecraft:iceberg_pillar_roof");
pub const ICEBERG_SURFACE: DoublePerlinNoiseParameters = DoublePerlinNoiseParameters::new(
21usize,
-6i32,
&[1f64, 1f64, 1f64],
"minecraft:iceberg_surface",
);
pub const JAGGED: DoublePerlinNoiseParameters = DoublePerlinNoiseParameters::new(
22usize,
-16i32,
&[
1f64, 1f64, 1f64, 1f64, 1f64, 1f64, 1f64, 1f64, 1f64, 1f64, 1f64, 1f64, 1f64, 1f64,
@ -172,61 +219,90 @@ impl DoublePerlinNoiseParameters {
],
"minecraft:jagged",
);
pub const NETHER_TEMPERATURE: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-7i32, &[1f64, 1f64], "minecraft:nether/temperature");
pub const NETHER_VEGETATION: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-7i32, &[1f64, 1f64], "minecraft:nether/vegetation");
pub const NETHER_STATE_SELECTOR: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-4i32, &[1f64], "minecraft:nether_state_selector");
pub const NETHER_TEMPERATURE: DoublePerlinNoiseParameters = DoublePerlinNoiseParameters::new(
23usize,
-7i32,
&[1f64, 1f64],
"minecraft:nether/temperature",
);
pub const NETHER_VEGETATION: DoublePerlinNoiseParameters = DoublePerlinNoiseParameters::new(
24usize,
-7i32,
&[1f64, 1f64],
"minecraft:nether/vegetation",
);
pub const NETHER_STATE_SELECTOR: DoublePerlinNoiseParameters = DoublePerlinNoiseParameters::new(
25usize,
-4i32,
&[1f64],
"minecraft:nether_state_selector",
);
pub const NETHER_WART: DoublePerlinNoiseParameters = DoublePerlinNoiseParameters::new(
26usize,
-3i32,
&[1f64, 0f64, 0f64, 0.9f64],
"minecraft:nether_wart",
);
pub const NETHERRACK: DoublePerlinNoiseParameters = DoublePerlinNoiseParameters::new(
27usize,
-3i32,
&[1f64, 0f64, 0f64, 0.35f64],
"minecraft:netherrack",
);
pub const NOODLE: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-8i32, &[1f64], "minecraft:noodle");
DoublePerlinNoiseParameters::new(28usize, -8i32, &[1f64], "minecraft:noodle");
pub const NOODLE_RIDGE_A: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-7i32, &[1f64], "minecraft:noodle_ridge_a");
DoublePerlinNoiseParameters::new(29usize, -7i32, &[1f64], "minecraft:noodle_ridge_a");
pub const NOODLE_RIDGE_B: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-7i32, &[1f64], "minecraft:noodle_ridge_b");
DoublePerlinNoiseParameters::new(30usize, -7i32, &[1f64], "minecraft:noodle_ridge_b");
pub const NOODLE_THICKNESS: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-8i32, &[1f64], "minecraft:noodle_thickness");
pub const OFFSET: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-3i32, &[1f64, 1f64, 1f64, 0f64], "minecraft:offset");
DoublePerlinNoiseParameters::new(31usize, -8i32, &[1f64], "minecraft:noodle_thickness");
pub const OFFSET: DoublePerlinNoiseParameters = DoublePerlinNoiseParameters::new(
32usize,
-3i32,
&[1f64, 1f64, 1f64, 0f64],
"minecraft:offset",
);
pub const ORE_GAP: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-5i32, &[1f64], "minecraft:ore_gap");
DoublePerlinNoiseParameters::new(33usize, -5i32, &[1f64], "minecraft:ore_gap");
pub const ORE_VEIN_A: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-7i32, &[1f64], "minecraft:ore_vein_a");
DoublePerlinNoiseParameters::new(34usize, -7i32, &[1f64], "minecraft:ore_vein_a");
pub const ORE_VEIN_B: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-7i32, &[1f64], "minecraft:ore_vein_b");
DoublePerlinNoiseParameters::new(35usize, -7i32, &[1f64], "minecraft:ore_vein_b");
pub const ORE_VEININESS: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-8i32, &[1f64], "minecraft:ore_veininess");
pub const PACKED_ICE: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-7i32, &[1f64, 1f64, 1f64, 1f64], "minecraft:packed_ice");
DoublePerlinNoiseParameters::new(36usize, -8i32, &[1f64], "minecraft:ore_veininess");
pub const PACKED_ICE: DoublePerlinNoiseParameters = DoublePerlinNoiseParameters::new(
37usize,
-7i32,
&[1f64, 1f64, 1f64, 1f64],
"minecraft:packed_ice",
);
pub const PATCH: DoublePerlinNoiseParameters = DoublePerlinNoiseParameters::new(
38usize,
-5i32,
&[1f64, 0f64, 0f64, 0f64, 0f64, 0.013333333333333334f64],
"minecraft:patch",
);
pub const PILLAR: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-7i32, &[1f64, 1f64], "minecraft:pillar");
DoublePerlinNoiseParameters::new(39usize, -7i32, &[1f64, 1f64], "minecraft:pillar");
pub const PILLAR_RARENESS: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-8i32, &[1f64], "minecraft:pillar_rareness");
DoublePerlinNoiseParameters::new(40usize, -8i32, &[1f64], "minecraft:pillar_rareness");
pub const PILLAR_THICKNESS: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-8i32, &[1f64], "minecraft:pillar_thickness");
pub const POWDER_SNOW: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-6i32, &[1f64, 1f64, 1f64, 1f64], "minecraft:powder_snow");
DoublePerlinNoiseParameters::new(41usize, -8i32, &[1f64], "minecraft:pillar_thickness");
pub const POWDER_SNOW: DoublePerlinNoiseParameters = DoublePerlinNoiseParameters::new(
42usize,
-6i32,
&[1f64, 1f64, 1f64, 1f64],
"minecraft:powder_snow",
);
pub const RIDGE: DoublePerlinNoiseParameters = DoublePerlinNoiseParameters::new(
43usize,
-7i32,
&[1f64, 2f64, 1f64, 0f64, 0f64, 0f64],
"minecraft:ridge",
);
pub const SOUL_SAND_LAYER: DoublePerlinNoiseParameters = DoublePerlinNoiseParameters::new(
44usize,
-8i32,
&[
1f64,
@ -242,50 +318,80 @@ impl DoublePerlinNoiseParameters {
"minecraft:soul_sand_layer",
);
pub const SPAGHETTI_2D: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-7i32, &[1f64], "minecraft:spaghetti_2d");
DoublePerlinNoiseParameters::new(45usize, -7i32, &[1f64], "minecraft:spaghetti_2d");
pub const SPAGHETTI_2D_ELEVATION: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-8i32, &[1f64], "minecraft:spaghetti_2d_elevation");
DoublePerlinNoiseParameters::new(
46usize,
-8i32,
&[1f64],
"minecraft:spaghetti_2d_elevation",
);
pub const SPAGHETTI_2D_MODULATOR: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-11i32, &[1f64], "minecraft:spaghetti_2d_modulator");
DoublePerlinNoiseParameters::new(
47usize,
-11i32,
&[1f64],
"minecraft:spaghetti_2d_modulator",
);
pub const SPAGHETTI_2D_THICKNESS: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-11i32, &[1f64], "minecraft:spaghetti_2d_thickness");
DoublePerlinNoiseParameters::new(
48usize,
-11i32,
&[1f64],
"minecraft:spaghetti_2d_thickness",
);
pub const SPAGHETTI_3D_1: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-7i32, &[1f64], "minecraft:spaghetti_3d_1");
DoublePerlinNoiseParameters::new(49usize, -7i32, &[1f64], "minecraft:spaghetti_3d_1");
pub const SPAGHETTI_3D_2: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-7i32, &[1f64], "minecraft:spaghetti_3d_2");
DoublePerlinNoiseParameters::new(50usize, -7i32, &[1f64], "minecraft:spaghetti_3d_2");
pub const SPAGHETTI_3D_RARITY: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-11i32, &[1f64], "minecraft:spaghetti_3d_rarity");
DoublePerlinNoiseParameters::new(51usize, -11i32, &[1f64], "minecraft:spaghetti_3d_rarity");
pub const SPAGHETTI_3D_THICKNESS: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-8i32, &[1f64], "minecraft:spaghetti_3d_thickness");
DoublePerlinNoiseParameters::new(
52usize,
-8i32,
&[1f64],
"minecraft:spaghetti_3d_thickness",
);
pub const SPAGHETTI_ROUGHNESS: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-5i32, &[1f64], "minecraft:spaghetti_roughness");
DoublePerlinNoiseParameters::new(53usize, -5i32, &[1f64], "minecraft:spaghetti_roughness");
pub const SPAGHETTI_ROUGHNESS_MODULATOR: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-8i32, &[1f64], "minecraft:spaghetti_roughness_modulator");
DoublePerlinNoiseParameters::new(
54usize,
-8i32,
&[1f64],
"minecraft:spaghetti_roughness_modulator",
);
pub const SURFACE: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-6i32, &[1f64, 1f64, 1f64], "minecraft:surface");
DoublePerlinNoiseParameters::new(55usize, -6i32, &[1f64, 1f64, 1f64], "minecraft:surface");
pub const SURFACE_SECONDARY: DoublePerlinNoiseParameters = DoublePerlinNoiseParameters::new(
56usize,
-6i32,
&[1f64, 1f64, 0f64, 1f64],
"minecraft:surface_secondary",
);
pub const SURFACE_SWAMP: DoublePerlinNoiseParameters =
DoublePerlinNoiseParameters::new(-2i32, &[1f64], "minecraft:surface_swamp");
DoublePerlinNoiseParameters::new(57usize, -2i32, &[1f64], "minecraft:surface_swamp");
pub const TEMPERATURE: DoublePerlinNoiseParameters = DoublePerlinNoiseParameters::new(
58usize,
-10i32,
&[1.5f64, 0f64, 1f64, 0f64, 0f64, 0f64],
"minecraft:temperature",
);
pub const TEMPERATURE_LARGE: DoublePerlinNoiseParameters = DoublePerlinNoiseParameters::new(
59usize,
-12i32,
&[1.5f64, 0f64, 1f64, 0f64, 0f64, 0f64],
"minecraft:temperature_large",
);
pub const VEGETATION: DoublePerlinNoiseParameters = DoublePerlinNoiseParameters::new(
60usize,
-8i32,
&[1f64, 1f64, 0f64, 0f64, 0f64, 0f64],
"minecraft:vegetation",
);
pub const VEGETATION_LARGE: DoublePerlinNoiseParameters = DoublePerlinNoiseParameters::new(
61usize,
-10i32,
&[1f64, 1f64, 0f64, 0f64, 0f64, 0f64],
"minecraft:vegetation_large",

View File

@ -596,7 +596,7 @@ fn is_vec(ty: &Type) -> bool {
.path
.segments
.iter()
.last()
.next_back()
.is_some_and(|segment| segment.ident == "Vec")
} else {
false

View File

@ -80,7 +80,7 @@ mod double_perlin_noise_sampler_test {
assert_eq!(rand.next_i32(), -1302745855);
let mut rand_gen = RandomGenerator::Legacy(rand);
let params = DoublePerlinNoiseParameters::new(0, &[4f64], "");
let params = DoublePerlinNoiseParameters::new(0, 0, &[4f64], "");
let sampler = DoublePerlinNoiseSampler::from_params(&mut rand_gen, &params, true);
let values = [
@ -178,7 +178,7 @@ mod double_perlin_noise_sampler_test {
let mut rand_gen = RandomGenerator::Xoroshiro(rand);
let params = DoublePerlinNoiseParameters::new(1, &[2f64, 4f64], "");
let params = DoublePerlinNoiseParameters::new(0, 1, &[2f64, 4f64], "");
let sampler = DoublePerlinNoiseSampler::from_params(&mut rand_gen, &params, false);

View File

@ -72,7 +72,7 @@ impl DoublePerlinNoiseBuilder {
base_random_deriver: &XoroshiroSplitter,
parameters: &DoublePerlinNoiseParameters,
) -> DoublePerlinNoiseSampler {
let mut random = base_random_deriver.split_string(parameters.id());
let mut random = base_random_deriver.split_string(parameters.name());
DoublePerlinNoiseSampler::from_params(&mut random, parameters, false)
}
}
@ -245,7 +245,7 @@ impl ProtoNoiseRouters {
// NETHER_TEMPERATURE and NETHER_VEGETATION always use NormalNoise.createLegacyNetherBiome with
// LegacyRandomSource(seed + 0) and LegacyRandomSource(seed + 1)
// respectively, regardless of useLegacyRandomSource.
let sampler = match data.noise_id.id() {
let sampler = match data.noise_id.name() {
"minecraft:nether/temperature" => {
let mut legacy_rand =
LegacyRand::from_seed(random_config.seed.wrapping_add(0));

View File

@ -53,7 +53,7 @@ use crate::generation::structure::structures::{
StructureGeneratorContext, StructureInstance, create_chunk_random,
};
use crate::generation::structure::try_generate_structure;
use crate::generation::surface::rule::try_apply_material_rule;
use crate::generation::surface::evaluate_surface_rule;
use crate::{
BlockStateId, ProtoNoiseRouters,
block::RawBlockState,
@ -916,7 +916,7 @@ impl ProtoChunk {
context.block_pos_y,
context.block_pos_z,
);
let new_state = try_apply_material_rule(
let new_state = evaluate_surface_rule(
&settings.surface_rule,
self,
&mut context,

View File

@ -410,8 +410,6 @@ impl NetherFortressPiece {
}
}
// ---- opening helpers --------------------------------------------------
#[allow(clippy::too_many_arguments)]
pub fn fill_forward_opening(
&self,

View File

@ -1,14 +1,12 @@
use pumpkin_data::{
chunk::Biome,
chunk_gen_settings::{
AboveYMaterialCondition, MaterialCondition, NoiseThresholdMaterialCondition,
NotMaterialCondition, StoneDepthMaterialCondition, VerticalGradientMaterialCondition,
WaterMaterialCondition,
},
BlockState,
chunk::{Biome, DoublePerlinNoiseParameters},
chunk_gen_settings::{ColumnNoiseCache, CompiledSurfaceRule, SurfaceInstruction},
};
use pumpkin_util::{
math::{lerp2, vertical_surface_type::VerticalSurfaceType},
random::{RandomImpl, xoroshiro128::XoroshiroSplitter},
y_offset::YOffset,
};
use terrain::SurfaceTerrainBuilder;
@ -26,7 +24,6 @@ use super::{
},
};
pub mod rule;
pub mod terrain;
pub struct MaterialRuleContext<'a> {
@ -52,6 +49,7 @@ 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> {
@ -88,6 +86,7 @@ impl<'a> MaterialRuleContext<'a> {
stone_depth_below: 0,
stone_depth_above: 0,
sea_level,
noise_cache: ColumnNoiseCache::new(),
}
}
@ -135,110 +134,233 @@ impl<'a> MaterialRuleContext<'a> {
}
}
#[expect(clippy::similar_names)]
pub fn test_condition(
condition: &MaterialCondition,
pub fn evaluate_surface_rule(
rule: &CompiledSurfaceRule,
chunk: &mut ProtoChunk,
context: &mut MaterialRuleContext,
surface_height_estimate_sampler: &mut SurfaceHeightEstimateSampler,
) -> bool {
match condition {
MaterialCondition::Biome(biome) => BiomeMaterialCondition::test(biome.biome_is, context),
MaterialCondition::NoiseThreshold(noise_threshold) => {
test_noise_threshold(noise_threshold, context)
}
MaterialCondition::VerticalGradient(vertical_gradient) => {
test_vertical_gradient(vertical_gradient, context)
}
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
}
MaterialCondition::Steep => {
let local_x = context.block_pos_x & 15;
let local_z = context.block_pos_z & 15;
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),
let local_z_sub = 0.max(local_z - 1);
let local_z_add = 15.min(local_z + 1);
SurfaceInstruction::PlaceBadlands => {
return Some(get_badlands_block(ctx));
}
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);
SurfaceInstruction::TestBiome { biome_is, skip } => {
if !biome_is.iter().any(|b| std::ptr::eq(*b, ctx.biome)) {
pc += *skip as usize;
}
}
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);
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;
}
}
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);
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;
}
}
sub_height >= add_height + 4
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;
}
}
}
MaterialCondition::Not(not) => {
test_not_material(not, chunk, context, surface_height_estimate_sampler)
pc += 1;
}
None
}
#[inline]
fn eval_single_instruction(
instr: &SurfaceInstruction,
chunk: &mut ProtoChunk,
ctx: &mut MaterialRuleContext,
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))
}
MaterialCondition::Hole(_hole) => HoleMaterialCondition::test(context),
MaterialCondition::AbovePreliminarySurface(_above) => {
SurfaceMaterialCondition::test(context, surface_height_estimate_sampler)
SurfaceInstruction::TestNoiseAbove { noise, min, .. } => {
ctx.noise_cache
.get(noise, || sample_noise(noise, deriver, x, z))
>= *min
}
MaterialCondition::StoneDepth(stone_depth) => test_stone_depth(stone_depth, context),
SurfaceInstruction::TestNoiseRange {
noise, min, max, ..
} => {
let v = ctx
.noise_cache
.get(noise, || sample_noise(noise, deriver, x, z));
v >= *min && v <= *max
}
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)
}
_ => false,
}
}
pub struct HoleMaterialCondition;
impl HoleMaterialCondition {
pub const fn test(context: &MaterialRuleContext) -> bool {
context.run_depth <= 0
}
#[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 const fn test_above_y_material(
condition: &AboveYMaterialCondition,
#[inline]
pub const fn test_hole(context: &MaterialRuleContext) -> bool {
context.run_depth <= 0
}
pub const fn test_y_above(
context: &MaterialRuleContext,
anchor: &YOffset,
surface_depth_multiplier: i32,
add_stone_depth: bool,
) -> bool {
context.block_pos_y
+ if condition.add_stone_depth {
+ if add_stone_depth {
context.stone_depth_above
} else {
0
}
>= condition.anchor.get_y(context.min_y as i16, context.height)
+ context.run_depth * condition.surface_depth_multiplier
>= anchor.get_y(context.min_y as i16, context.height)
+ context.run_depth * surface_depth_multiplier
}
pub fn test_not_material(
condition: &NotMaterialCondition,
chunk: &mut ProtoChunk,
#[inline]
pub fn test_above_preliminary_surface(
context: &mut MaterialRuleContext,
surface_height_estimate_sampler: &mut SurfaceHeightEstimateSampler,
) -> bool {
!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)
}
context.block_pos_y >= estimate_surface_height(context, surface_height_estimate_sampler)
}
pub fn estimate_surface_height(
@ -283,41 +405,33 @@ pub fn estimate_surface_height(
context.surface_min_y
}
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 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 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 &condition.surface_type {
let stone_depth = match surface_type {
VerticalSurfaceType::Ceiling => context.stone_depth_below,
VerticalSurfaceType::Floor => context.stone_depth_above,
};
let depth = if condition.add_surface_depth {
let depth = if add_surface_depth {
context.run_depth
} else {
0
};
let depth_range = if condition.secondary_depth_range == 0 {
let depth_range = if secondary_depth_range == 0 {
0
} else {
pumpkin_util::math::map(
@ -325,40 +439,37 @@ pub fn test_stone_depth(
-1.0,
1.0,
0.0,
condition.secondary_depth_range as f64,
secondary_depth_range as f64,
) as i32
};
stone_depth <= 1 + condition.offset + depth + depth_range
stone_depth <= 1 + offset + depth + depth_range
}
pub const fn test_water_material(
condition: &WaterMaterialCondition,
pub const fn test_water(
context: &MaterialRuleContext,
offset: i32,
surface_depth_multiplier: i32,
add_stone_depth: bool,
) -> bool {
context.fluid_height == i32::MIN
|| context.block_pos_y
+ (if condition.add_stone_depth {
+ (if add_stone_depth {
context.stone_depth_above
} else {
0
})
>= context.fluid_height
+ condition.offset
+ context.run_depth * condition.surface_depth_multiplier
>= context.fluid_height + offset + context.run_depth * 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 = 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 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 block_y = context.block_pos_y;
if block_y <= true_at {
@ -369,9 +480,42 @@ pub fn test_vertical_gradient(
}
let splitter = context
.random_deriver
.from_lo_and_hi(condition.random_lo, condition.random_hi)
.from_lo_and_hi(random_lo, 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
}

View File

@ -1,89 +0,0 @@
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
}

View File

@ -107,10 +107,6 @@ const MAX_PREVIOUS_MESSAGES: u8 = 20; // Vanilla: 20
pub const DATA_VERSION: i32 = 4790; // 26.1.2
enum BatchState {
Count(u8),
}
struct HeapNode(i32, Vector2<i32>, SyncChunk);
impl Eq for HeapNode {}
@ -141,7 +137,7 @@ pub struct ChunkManager {
chunk_sent: HashMap<Vector2<i32>, Weak<ChunkData>>,
chunk_queue: BinaryHeap<HeapNode>,
entity_chunk_queue: VecDeque<(Vector2<i32>, SyncEntityChunk)>,
batches_sent_since_ack: BatchState,
batches_sent_since_ack: u8,
last_chunk_batch_sent_at: Instant,
/// The current world for chunk loading. Updated on dimension change.
world: Arc<World>,
@ -165,7 +161,7 @@ impl ChunkManager {
chunk_sent: HashMap::new(),
chunk_queue: BinaryHeap::new(),
entity_chunk_queue: VecDeque::new(),
batches_sent_since_ack: BatchState::Count(0),
batches_sent_since_ack: 0,
last_chunk_batch_sent_at: Instant::now(),
world,
}
@ -186,9 +182,7 @@ impl ChunkManager {
#[must_use]
const fn ack_window_open(&self) -> bool {
match self.batches_sent_since_ack {
BatchState::Count(count) => count < Self::NOTCHIAN_BATCHES_WITHOUT_ACK_UNTIL_PAUSE,
}
self.batches_sent_since_ack < Self::NOTCHIAN_BATCHES_WITHOUT_ACK_UNTIL_PAUSE
}
#[must_use]
@ -198,28 +192,19 @@ impl ChunkManager {
}
pub fn pull_new_chunks(&mut self) {
let mut new_nodes = Vec::new();
while let Ok((pos, chunk)) = self.chunk_listener.try_recv() {
let dst = (pos.x - self.center.x)
.abs()
.max((pos.y - self.center.y).abs());
let dst = Self::chebyshev(pos, self.center);
if dst > i32::from(self.view_distance) {
continue;
}
if self.should_enqueue_chunk(pos, &chunk) {
new_nodes.push(HeapNode(dst, pos, chunk));
self.chunk_queue.push(HeapNode(dst, pos, chunk));
}
}
}
if !new_nodes.is_empty() {
if self.chunk_queue.is_empty() {
self.chunk_queue = BinaryHeap::from(new_nodes);
} else {
for node in new_nodes {
self.chunk_queue.push(node);
}
}
}
fn chebyshev(a: Vector2<i32>, b: Vector2<i32>) -> i32 {
(a.x - b.x).abs().max((a.y - b.y).abs())
}
pub fn update_center_and_view_distance(
@ -259,14 +244,15 @@ impl ChunkManager {
&& !unloading_chunks.contains(pos)
});
let mut tasks: Vec<_> = self.chunk_queue.drain().collect();
tasks.retain(|node| {
let dst = (node.1.x - center.x).abs().max((node.1.y - center.y).abs());
dst <= view_distance_i32 && !unloading_chunks.contains(&node.1)
});
for node in &mut tasks {
node.0 = (node.1.x - center.x).abs().max((node.1.y - center.y).abs());
}
let mut tasks: Vec<_> = self
.chunk_queue
.drain()
.filter_map(|node| {
let dst = Self::chebyshev(node.1, center);
(dst <= view_distance_i32 && !unloading_chunks.contains(&node.1))
.then(|| HeapNode(dst, node.1, node.2))
})
.collect();
for pos in loading_chunks {
if !self.chunk_sent.contains_key(pos)
@ -297,7 +283,7 @@ impl ChunkManager {
self.chunk_sent.clear();
self.chunk_queue.clear();
self.entity_chunk_queue.clear();
self.batches_sent_since_ack = BatchState::Count(0);
self.batches_sent_since_ack = 0;
self.last_chunk_batch_sent_at = Instant::now();
}
@ -314,12 +300,12 @@ impl ChunkManager {
self.chunk_queue.clear();
self.world = new_world;
// Reset batch state so chunks can be sent immediately in the new dimension
self.batches_sent_since_ack = BatchState::Count(0);
self.batches_sent_since_ack = 0;
self.last_chunk_batch_sent_at = Instant::now();
}
pub const fn handle_acknowledge(&mut self, chunks_per_tick: f32) {
self.batches_sent_since_ack = BatchState::Count(0);
self.batches_sent_since_ack = 0;
self.chunks_per_tick = chunks_per_tick.ceil() as usize;
}
@ -344,17 +330,12 @@ impl ChunkManager {
}
pub fn next_chunk(&mut self) -> Box<[SyncChunk]> {
let mut chunk_size = self.chunk_queue.len().min(self.chunks_per_tick.max(1));
let mut chunks = Vec::<Arc<ChunkData>>::with_capacity(chunk_size);
while chunk_size > 0 {
let take = self.chunk_queue.len().min(self.chunks_per_tick.max(1));
let mut chunks = Vec::with_capacity(take);
for _ in 0..take {
chunks.push(self.chunk_queue.pop().unwrap().2);
chunk_size -= 1;
}
match &mut self.batches_sent_since_ack {
BatchState::Count(count) => {
*count = count.saturating_add(1);
}
}
self.batches_sent_since_ack = self.batches_sent_since_ack.saturating_add(1);
self.last_chunk_batch_sent_at = Instant::now();
chunks.into_boxed_slice()
@ -372,11 +353,7 @@ impl ChunkManager {
.map(|(_, chunk)| chunk)
.collect();
match &mut self.batches_sent_since_ack {
BatchState::Count(count) => {
*count = count.saturating_add(1);
}
}
self.batches_sent_since_ack = self.batches_sent_since_ack.saturating_add(1);
self.last_chunk_batch_sent_at = Instant::now();
chunks

View File

@ -386,13 +386,6 @@ impl World {
.await;
}
pub fn get_difficulty(&self, difficulty: Difficulty) {
let current_info = self.level_info.load();
let mut new_info = (**current_info).clone();
new_info.difficulty = difficulty;
self.level_info.store(Arc::new(new_info));
}
pub fn set_difficulty(&self, difficulty: Difficulty) {
let current_info = self.level_info.load();
let mut new_info = (**current_info).clone();
@ -821,7 +814,11 @@ impl World {
Vector3<i32>,
Vec<(BlockPos, BlockStateId)>,
> = HashMap::new();
for (position, block_state_id) in self.unsent_block_changes.lock().await.drain() {
let changes = {
let mut guard = self.unsent_block_changes.lock().await;
std::mem::take(&mut *guard)
};
for (position, block_state_id) in changes {
let chunk_section = chunk_section_from_pos(&position);
block_state_updates_by_chunk_section
.entry(chunk_section)