unbanished_core/noise.rs
1//! The engine's Perlin noise, which the terrain generator uses.
2//!
3//! It is value noise: a hash gives a value for each integer point, the values are smoothed over the
4//! 3 × 3 neighbours and joined with cosine interpolation, and several octaves are added. Every
5//! float operation is single precision and in the original's order, and `cosf` is the C runtime's
6//! ([`crate::crt::cosf`]), so the values match the original to the last bit.
7//!
8//! ```
9//! use unbanished_core::noise::PerlinNoise;
10//!
11//! let noise = PerlinNoise::new(0.5, 4.0, 17, 31);
12//! let value = noise.generate(12.5, 40.25);
13//! assert!(value.is_finite());
14//! ```
15//!
16//! Original: `Utility::PerlinNoise`, Runtime `0x18010ad80` to `0x18010b520`.
17
18#[derive(Debug, Clone, Copy, PartialEq)]
19/// A noise field: its persistence, octaves and two seeds.
20///
21/// Measured: `tools/emu/perlin.py` (4,000 values, the temperature's settings and others, bit
22/// for bit).
23pub struct PerlinNoise {
24 /// How much each octave adds: octave `i` is scaled by `persistence` to the power `i`.
25 pub persistence: f32,
26 /// The number of octaves. The loop runs while `(float)i < octaves`.
27 pub octaves: f32,
28 /// The first seed of the hash.
29 pub seed_x: i32,
30 /// The second seed of the hash.
31 pub seed_y: i32,
32}
33
34const PI: f32 = std::f32::consts::PI;
35
36fn cosf(x: f32) -> f32 {
37 crate::crt::cosf(x)
38}
39
40/// `powf` for the exponents the generator uses (small non-negative integers).
41fn powf(base: f32, exp: f32) -> f32 {
42 (base as f64).powf(exp as f64) as f32
43}
44
45impl PerlinNoise {
46 /// A noise field with these settings.
47 ///
48 /// Original: `PerlinNoise(float, float, int, int)`, Runtime `0x18010b500`.
49 pub fn new(persistence: f32, octaves: f32, seed_x: i32, seed_y: i32) -> Self {
50 PerlinNoise {
51 persistence,
52 octaves,
53 seed_x,
54 seed_y,
55 }
56 }
57
58 #[inline]
59 fn hash(&self, x: i32, y: i32) -> u32 {
60 (self.seed_y.wrapping_add(y) as u32)
61 .wrapping_mul(0x39)
62 .wrapping_add(self.seed_x as u32)
63 .wrapping_add(x as u32)
64 }
65
66 #[inline]
67 fn value(n: u32) -> f32 {
68 let n = n ^ n.wrapping_mul(0x2000);
69 let m = n
70 .wrapping_mul(n)
71 .wrapping_mul(0x3d73)
72 .wrapping_add(0xc0ae5)
73 .wrapping_mul(n)
74 .wrapping_add(0xd208dd0d)
75 & 0x7fff_ffff;
76 1.0 - m as f32 * 9.313_226e-10
77 }
78
79 /// The hashed value at an integer point, from -1 to 1.
80 ///
81 /// Original: `Random(int, int)`, Runtime `0x18010ad80`.
82 pub fn random(&self, x: i32, y: i32) -> f32 {
83 Self::value(self.hash(x, y))
84 }
85
86 /// The value at an integer point, smoothed with its 8 neighbours: corners / 16 + edges / 8 +
87 /// centre / 4, added in the original's order.
88 ///
89 /// Original: `SmoothNoise(int, int)`, Runtime `0x18010ae50`.
90 pub fn smooth_noise(&self, x: i32, y: i32) -> f32 {
91 let c = self.hash(x, y);
92 let v = |d: i32| Self::value(c.wrapping_add(d as u32));
93 let corners = v(-0x38) + v(-0x3a) + v(0x38) + v(0x3a);
94 let sides = v(1) + v(-1) + v(-0x39) + v(0x39);
95 corners * 0.0625 + sides * 0.125 + v(0) * 0.25
96 }
97
98 /// The noise at a point: every octave, interpolated and added.
99 ///
100 /// Original: `Generate(float, float)`, Runtime `0x18010b240`.
101 pub fn generate(&self, x: f32, y: f32) -> f32 {
102 let mut total = 0.0f32;
103 let mut i = 0i32;
104 while (i as f32) < self.octaves {
105 let freq = powf(2.0, i as f32);
106 let (fx, fy) = (freq * x, freq * y);
107 let (ix, iy) = (fx as i32, fy as i32);
108 let a = (1.0 - cosf((fx - ix as f32) * PI)) * 0.5;
109 let b = (1.0 - cosf((fy - iy as f32) * PI)) * 0.5;
110 let top = self.smooth_noise(ix, iy) * (1.0 - a) + self.smooth_noise(ix + 1, iy) * a;
111 let bottom =
112 self.smooth_noise(ix, iy + 1) * (1.0 - a) + self.smooth_noise(ix + 1, iy + 1) * a;
113 let v = top * (1.0 - b) + bottom * b;
114 total += v * powf(self.persistence, i as f32);
115 i += 1;
116 }
117 total
118 }
119}
120
121#[cfg(test)]
122mod golden {
123 //! Values produced by the original Runtime DLL under emulation (tools/emu/check_random.py).
124 use super::*;
125
126 #[test]
127 fn matches_original_engine() {
128 let n = PerlinNoise::new(0.5, 5.0, 12345, -6789);
129 for (x, y, expected) in [
130 (0.0f32, 0.0f32, 0.6994447112083435f32),
131 (1.25, 7.5, -0.048602547496557236),
132 (100.75, 33.1, 0.27891477942466736),
133 (511.5, 255.25, 0.1838667392730713),
134 ] {
135 assert_eq!(n.generate(x, y).to_bits(), expected.to_bits(), "({x}, {y})");
136 }
137 }
138}