Skip to main content

steel_worldgen/density/
mod.rs

1//! Density function types and transpiler for world generation.
2//!
3//! Density functions form a tree structure parsed from JSON at build time.
4//! The transpiler compiles these trees into native Rust code — runtime evaluation
5//! is done by the transpiled output, not by interpreting this tree.
6//!
7//! # Key Types
8//!
9//! - [`DensityFunction`] - The density function enum with all operation types
10//! - [`NoiseRouter`] - Collection of all density functions for world generation
11//! - [`CubicSpline`] - Cubic spline interpolation for smooth terrain transitions
12//! - [`RarityValueMapper`] - Used at runtime by transpiled cave generation code
13//! - [`DimensionNoises`] - Trait for dimension-specific noise generators
14//! - [`NoiseSettings`] - Trait for dimension-specific settings from datapack
15
16pub mod spline_eval;
17pub mod traits;
18
19pub use traits::{ColumnCache, DimensionNoises, NoiseSettings};
20
21/// Parameters for creating a noise generator.
22#[derive(Debug, Clone)]
23pub struct NoiseParameters {
24    /// The first octave level.
25    pub first_octave: i32,
26    /// Amplitude multipliers for each octave.
27    pub amplitudes: Vec<f64>,
28}
29
30impl NoiseParameters {
31    /// Create new noise parameters.
32    #[must_use]
33    pub const fn new(first_octave: i32, amplitudes: Vec<f64>) -> Self {
34        Self {
35            first_octave,
36            amplitudes,
37        }
38    }
39}
40
41/// Rarity value mapper for cave generation.
42///
43/// Used at runtime by transpiled `WeirdScaledSampler` code.
44#[derive(Debug, Clone, Copy, PartialEq, Eq)]
45pub enum RarityValueMapper {
46    /// Mapper type `"type_1"` for tunnels.
47    Tunnels,
48    /// Mapper type `"type_2"` for caves.
49    Caves,
50}
51
52impl RarityValueMapper {
53    /// Get the scaling factor for this mapper based on rarity value.
54    ///
55    /// From vanilla `NoiseRouterData.QuantizedSpaghettiRarity`.
56    #[must_use]
57    pub fn get_values(self, rarity: f64) -> f64 {
58        match self {
59            Self::Tunnels => {
60                if rarity < -0.5 {
61                    0.75
62                } else if rarity < 0.0 {
63                    1.0
64                } else if rarity < 0.5 {
65                    1.5
66                } else {
67                    2.0
68                }
69            }
70            Self::Caves => {
71                if rarity < -0.75 {
72                    0.5
73                } else if rarity < -0.5 {
74                    0.75
75                } else if rarity < 0.5 {
76                    1.0
77                } else if rarity < 0.75 {
78                    2.0
79                } else {
80                    3.0
81                }
82            }
83        }
84    }
85}