Files
VS-map-previewer/src/terra.sex
alex-eg c14c154303 fix four conditional operands sexc emitted without parentheses, and mark every Sex workaround with a TODO
A conditional as an operand loses its parentheses (sex#59): box-blur
allocated a quarter of its buffer, deep water got the shallow colour,
and verify's delta histogram wrote out of bounds into the height
classes. Plan.org quoted the corrupted count.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 21:01:59 +03:00

814 lines
35 KiB
Scheme

;;; Terrain height, as Rivers' NewGenTerra computes it: vanilla GenTerra
;;; plus the river valley blend and the riverbed carving. Only the
;;; column's height is kept, the highest solid block, which is what
;;; worldgen stores as WorldGenTerrainHeightMap and what the rock
;;; strata are laid from.
;;;
;;; Per column: landform weights lerped from the region's landform
;;; map, a Y threshold profile from them, distortion and upheaval, and
;;; a 3D noise against the threshold at every Y until the column is
;;; certainly air above.
(include stdio.h stdlib.h string.h math.h)
;; TODO sex#58: import only the modules used here once an import brings
;; the types its interface needs; until then, every dependency, in
;; dependency order.
(import maps)
(import lcg)
(import noise)
(import shape)
(import json)
(import worldgen)
(import fastnoise)
(import rivers)
;;; NewSimplexNoiseLayer --------------------------------------------
;; TODO sex#47: write these in hex, as the C# does, once there are hex
;; literals.
(define PRIME-X 5910200641878280303)
(define PRIME-Y 6452764530575939509)
(define PRIME-Z 6614699811220273867)
(define HASH-MULTIPLIER 6026932503003350773)
(define SEED-FLIP -5968755714895566377)
(define NORMALIZER-3D 0.2781926117527186)
(define MAX-Y-SLOPE 5.0)
;;; Generated from vsapi NewSimplexNoiseLayer.cs grad3; do not edit.
(var grad3 [float 192]
#(2.22474487139 2.22474487139 -1.0 0.0
2.22474487139 2.22474487139 1.0 0.0
3.0862664687972017 1.1721513422464978 0.0 0.0
1.1721513422464978 3.0862664687972017 0.0 0.0
-2.22474487139 2.22474487139 -1.0 0.0
-2.22474487139 2.22474487139 1.0 0.0
-1.1721513422464978 3.0862664687972017 0.0 0.0
-3.0862664687972017 1.1721513422464978 0.0 0.0
-1.0 -2.22474487139 -2.22474487139 0.0
1.0 -2.22474487139 -2.22474487139 0.0
0.0 -3.0862664687972017 -1.1721513422464978 0.0
0.0 -1.1721513422464978 -3.0862664687972017 0.0
-1.0 -2.22474487139 2.22474487139 0.0
1.0 -2.22474487139 2.22474487139 0.0
0.0 -1.1721513422464978 3.0862664687972017 0.0
0.0 -3.0862664687972017 1.1721513422464978 0.0
-2.22474487139 -2.22474487139 -1.0 0.0
-2.22474487139 -2.22474487139 1.0 0.0
-3.0862664687972017 -1.1721513422464978 0.0 0.0
-1.1721513422464978 -3.0862664687972017 0.0 0.0
-2.22474487139 -1.0 -2.22474487139 0.0
-2.22474487139 1.0 -2.22474487139 0.0
-1.1721513422464978 0.0 -3.0862664687972017 0.0
-3.0862664687972017 0.0 -1.1721513422464978 0.0
-2.22474487139 -1.0 2.22474487139 0.0
-2.22474487139 1.0 2.22474487139 0.0
-3.0862664687972017 0.0 1.1721513422464978 0.0
-1.1721513422464978 0.0 3.0862664687972017 0.0
-1.0 2.22474487139 -2.22474487139 0.0
1.0 2.22474487139 -2.22474487139 0.0
0.0 1.1721513422464978 -3.0862664687972017 0.0
0.0 3.0862664687972017 -1.1721513422464978 0.0
-1.0 2.22474487139 2.22474487139 0.0
1.0 2.22474487139 2.22474487139 0.0
0.0 3.0862664687972017 1.1721513422464978 0.0
0.0 1.1721513422464978 3.0862664687972017 0.0
2.22474487139 -2.22474487139 -1.0 0.0
2.22474487139 -2.22474487139 1.0 0.0
1.1721513422464978 -3.0862664687972017 0.0 0.0
3.0862664687972017 -1.1721513422464978 0.0 0.0
2.22474487139 -1.0 -2.22474487139 0.0
2.22474487139 1.0 -2.22474487139 0.0
3.0862664687972017 0.0 -1.1721513422464978 0.0
1.1721513422464978 0.0 -3.0862664687972017 0.0
2.22474487139 -1.0 2.22474487139 0.0
2.22474487139 1.0 2.22474487139 0.0
1.1721513422464978 0.0 3.0862664687972017 0.0
3.0862664687972017 0.0 1.1721513422464978 0.0))
(var gradients [float 1024])
(fn init-gradients () void
(for (var i int 0) (< i 1024) (++ i)
(= [gradients i] (cast (/ (cast [grad3 (% i 192)] double) NORMALIZER-3D) float))))
(fn grad ((hash u64) (dx float) (dy float) (dz float)) float
(*= hash (cast HASH-MULTIPLIER u64))
(^= hash (cast (>> (cast hash i64) 58) u64))
(var gi int (& (cast hash int) 1020))
(return (+ (+ (* [gradients gi] dx) (* [gradients (c-bit-or gi 1)] dy))
(* [gradients (c-bit-or gi 2)] dz))))
(fn hash4 ((seed u64) (x u64) (y u64) (z u64)) u64
(return (^ seed x y z)))
(fn mask ((m int)) u64
(return (cast (cast m i64) u64)))
(fn noise3-base ((seed i64) (xr double) (yr double) (zr double)) float
(var xrb int (cast (floor xr) int))
(var yrb int (cast (floor yr) int))
(var zrb int (cast (floor zr) int))
(var xi float (cast (- xr xrb) float))
(var yi float (cast (- yr yrb) float))
(var zi float (cast (- zr zrb) float))
(var px u64 (cast PRIME-X u64))
(var py u64 (cast PRIME-Y u64))
(var pz u64 (cast PRIME-Z u64))
(var xrbp u64 (* (mask xrb) px))
(var yrbp u64 (* (mask yrb) py))
(var zrbp u64 (* (mask zrb) pz))
(var s1 u64 (cast seed u64))
(var s2 u64 (^ s1 (cast SEED-FLIP u64)))
(var xn int (cast (- (cast -0.5 float) xi) int))
(var yn int (cast (- (cast -0.5 float) yi) int))
(var zn int (cast (- (cast -0.5 float) zi) int))
(var x0 float (+ xi xn))
(var y0 float (+ yi yn))
(var z0 float (+ zi zn))
(var a0 float (- (cast 0.75 float) (* x0 x0) (* y0 y0) (* z0 z0)))
(var value float (* (* (* a0 a0) (* a0 a0))
(grad (hash4 s1 (+ xrbp (& (mask xn) px)) (+ yrbp (& (mask yn) py))
(+ zrbp (& (mask zn) pz)))
x0 y0 z0)))
(var x1 float (- xi (cast 0.5 float)))
(var y1 float (- yi (cast 0.5 float)))
(var z1 float (- zi (cast 0.5 float)))
(var a1 float (- (cast 0.75 float) (* x1 x1) (* y1 y1) (* z1 z1)))
(+= value (* (* (* a1 a1) (* a1 a1))
(grad (hash4 s2 (+ xrbp px) (+ yrbp py) (+ zrbp pz)) x1 y1 z1)))
;; The masks are 0 or -1; shifts of them become multiplications, as a
;; negative left shift is undefined in C.
(var xf0 float (* (cast (* (c-bit-or xn 1) 2) float) x1))
(var yf0 float (* (cast (* (c-bit-or yn 1) 2) float) y1))
(var zf0 float (* (cast (* (c-bit-or zn 1) 2) float) z1))
(var one float 1)
(var xf1 float (- (* (cast (- -2 (* xn 4)) float) x1) one))
(var yf1 float (- (* (cast (- -2 (* yn 4)) float) y1) one))
(var zf1 float (- (* (cast (- -2 (* zn 4)) float) z1) one))
;; TODO sex#62: cast (c-bit-or xn 1) to float directly once a cast of
;; a bitwise expression keeps its parentheses.
(var xsi int (c-bit-or xn 1))
(var ysi int (c-bit-or yn 1))
(var zsi int (c-bit-or zn 1))
(var xs float xsi)
(var ys float ysi)
(var zs float zsi)
(var skip5 bool false)
(var a2 float (+ xf0 a0))
(if (> a2 0)
(+= value (* (* (* a2 a2) (* a2 a2))
(grad (hash4 s1 (+ xrbp (& (~ (mask xn)) px)) (+ yrbp (& (mask yn) py))
(+ zrbp (& (mask zn) pz)))
(- x0 xs) y0 z0)))
(do
(var a3 float (+ (+ yf0 zf0) a0))
(if (> a3 0)
(+= value (* (* (* a3 a3) (* a3 a3))
(grad (hash4 s1 (+ xrbp (& (mask xn) px)) (+ yrbp (& (~ (mask yn)) py))
(+ zrbp (& (~ (mask zn)) pz)))
x0 (- y0 ys) (- z0 zs)))))
(var a4 float (+ xf1 a1))
(if (> a4 0)
(do
(+= value (* (* (* a4 a4) (* a4 a4))
(grad (hash4 s2 (+ xrbp (& (mask xn) (* px 2))) (+ yrbp py) (+ zrbp pz))
(+ xs x1) y1 z1)))
(= skip5 true)))))
(var skip9 bool false)
(var a6 float (+ yf0 a0))
(if (> a6 0)
(+= value (* (* (* a6 a6) (* a6 a6))
(grad (hash4 s1 (+ xrbp (& (mask xn) px)) (+ yrbp (& (~ (mask yn)) py))
(+ zrbp (& (mask zn) pz)))
x0 (- y0 ys) z0)))
(do
(var a7 float (+ (+ xf0 zf0) a0))
(if (> a7 0)
(+= value (* (* (* a7 a7) (* a7 a7))
(grad (hash4 s1 (+ xrbp (& (~ (mask xn)) px)) (+ yrbp (& (mask yn) py))
(+ zrbp (& (~ (mask zn)) pz)))
(- x0 xs) y0 (- z0 zs)))))
(var a8 float (+ yf1 a1))
(if (> a8 0)
(do
(+= value (* (* (* a8 a8) (* a8 a8))
(grad (hash4 s2 (+ xrbp px) (+ yrbp (& (mask yn) (<< py 1))) (+ zrbp pz))
x1 (+ ys y1) z1)))
(= skip9 true)))))
(var skip-d bool false)
(var aa float (+ zf0 a0))
(if (> aa 0)
(+= value (* (* (* aa aa) (* aa aa))
(grad (hash4 s1 (+ xrbp (& (mask xn) px)) (+ yrbp (& (mask yn) py))
(+ zrbp (& (~ (mask zn)) pz)))
x0 y0 (- z0 zs))))
(do
(var ab float (+ (+ xf0 yf0) a0))
(if (> ab 0)
(+= value (* (* (* ab ab) (* ab ab))
(grad (hash4 s1 (+ xrbp (& (~ (mask xn)) px)) (+ yrbp (& (~ (mask yn)) py))
(+ zrbp (& (mask zn) pz)))
(- x0 xs) (- y0 ys) z0))))
(var ac float (+ zf1 a1))
(if (> ac 0)
(do
(+= value (* (* (* ac ac) (* ac ac))
(grad (hash4 s2 (+ xrbp px) (+ yrbp py) (+ zrbp (& (mask zn) (<< pz 1))))
x1 y1 (+ zs z1))))
(= skip-d true)))))
(if (! skip5)
(do
(var a5 float (+ (+ yf1 zf1) a1))
(if (> a5 0)
(+= value (* (* (* a5 a5) (* a5 a5))
(grad (hash4 s2 (+ xrbp px) (+ yrbp (& (mask yn) (<< py 1)))
(+ zrbp (& (mask zn) (<< pz 1))))
x1 (+ ys y1) (+ zs z1)))))))
(if (! skip9)
(do
(var a9 float (+ (+ xf1 zf1) a1))
(if (> a9 0)
(+= value (* (* (* a9 a9) (* a9 a9))
(grad (hash4 s2 (+ xrbp (& (mask xn) (* px 2))) (+ yrbp py)
(+ zrbp (& (mask zn) (<< pz 1))))
(+ xs x1) y1 (+ zs z1)))))))
(if (! skip-d)
(do
(var ad float (+ (+ xf1 yf1) a1))
(if (> ad 0)
(+= value (* (* (* ad ad) (* ad ad))
(grad (hash4 s2 (+ xrbp (& (mask xn) (<< px 1)))
(+ yrbp (& (mask yn) (<< py 1))) (+ zrbp pz))
(+ xs x1) (+ ys y1) z1))))))
(return value))
(fn evaluate-improved-xz ((seed i64) (x double) (y double) (z double)) float
(var root3over3 double 0.577350269189626)
(var xz double (+ x z))
(var s2 double (* xz -0.211324865405187))
(var yy double (* y root3over3))
(var xr double (+ (+ x s2) yy))
(var zr double (+ (+ z s2) yy))
(var yr double (+ (* xz (- root3over3)) yy))
(return (noise3-base seed xr yr zr)))
;;; .NET's Math.Max and Math.Min: NaN if either side is. C's fmax and
;;; fmin return the other side instead, and NoiseSign's first pass
;;; relies on the NaN.
(fn net-max ((a double) (b double)) double
(if (|| (!= a a) (!= b b)) (return NAN))
(return (if (> a b) a b)))
(fn net-min ((a double) (b double)) double
(if (|| (!= a a) (!= b b)) (return NAN))
(return (if (< a b) a b)))
(fn smooth-max ((a double) (b double) (k double)) double
(var falloff double (/ (net-max (- k (fabs (- a b))) 0.0) k))
(return (+ (net-max a b) (* (* (* falloff falloff) k) 0.25))))
(fn smooth-min ((a double) (b double) (k double)) double
(var falloff double (/ (net-max (- k (fabs (- a b))) 0.0) k))
(return (- (net-min a b) (* (* (* falloff falloff) k) 0.25))))
(fn thresholding ((value double) (threshold double) (k double)) double
(return (+ (smooth-max 0 (- value threshold) k) (smooth-min 0 (+ value threshold) k))))
(fn value-curve ((v double)) double
(return (+ (* (/ v (sqrt (+ 1 (* v v)))) 0.5) 0.5)))
;;; NormalizedSimplexNoise.NoiseValueCurveInverse: the inverse of the
;;; *tanh* curve. GenTerra turns its threshold into a noise value with
;;; this one, though the column bounds use the new noise's own curve.
(fn tanh-curve-inverse ((v double)) double
;; TODO sex#63: write -INFINITY once it reads as a negation.
(if (<= v 0.0) (return (- INFINITY)))
(if (>= v 1.0) (return INFINITY))
(return (* 0.5 (log (/ v (- 1.0 v))))))
(define MAX-TERRAIN-OCTAVES 16)
;;; NewNormalizedSimplexFractalNoise.ColumnNoise: one column's octaves,
;;; strongest first, each with the bound on what the rest can add.
(record octave-entry
((seed i64) (x double) (frequency-y double) (z double) (amplitude double) (threshold double)
(smoothing double) (stop-bound double) (past-value double) (past-y double)))
(record column-noise ((entries [octave-entry 16]) (count int) (bound-min double) (bound-max double)))
(pub record terrain-noise
((count int) (frequencies [double 16]) (seeds [i64 16])))
(fn value-multiplier () double (return (* 1.2 MAX-VALUE-3D)))
(fn threshold-rescale () double (return (/ 1.0 MAX-VALUE-3D)))
(fn column-noise/make ((n (* const terrain-noise)) (relative-y double) (amps (* const double))
(thresholds (* const double)) (x double) (z double) (c (* column-noise))) void
(var vm double (value-multiplier))
(var max-values [double 16])
(var order [int 16])
(var used int 0)
(var bound double 0)
(for (var i int (- (. n count) 1)) (>= i 0) (-- i)
(= [max-values i] (* (fmax 0 (- (fabs [amps i]) [thresholds i])) vm))
(+= bound [max-values i])
(if (== [max-values i] 0)
(continue))
(= [order used] i)
(for (var j int (- used 1)) (>= j 0) (-- j)
(if (> [max-values [order (+ j 1)]] [max-values [order j]])
(do
(var t int [order j])
(= [order j] [order (+ j 1)])
(= [order (+ j 1)] t))))
(++ used))
(= (. c bound-min) (value-curve (- bound)))
(= (. c bound-max) (value-curve bound))
(= (. c count) used)
(var vm-tr double (* vm (threshold-rescale)))
(var sum double 0)
(for (var j int (- used 1)) (>= j 0) (-- j)
(var i int [order j])
(+= sum [max-values i])
(var f double [(. n frequencies) i])
(var e (* octave-entry) (& [(. c entries) j]))
(= (. e seed) [(. n seeds) i])
(= (. e x) (* x f))
(= (. e z) (* z f))
(= (. e frequency-y) (* f relative-y))
(= (. e amplitude) (* [amps i] vm))
(= (. e threshold) (* [thresholds i] vm-tr))
(= (. e smoothing) (* (* [amps i] f) 3.5))
(= (. e stop-bound) sum)
(= (. e past-value) 0)
(= (. e past-y) NAN)))
;;; ColumnNoise.NoiseSign: the sign of noise plus thresholder at Y,
;;; leaving out octaves once the rest cannot change it. The first pass
;;; bounds each octave from its value at the last Y sampled, which can
;;; settle the sign without evaluating anything.
(fn column-noise/sign ((c (* column-noise)) (y double) (thresholder double)) double
(var value double thresholder)
(var lo double thresholder)
(var hi double thresholder)
(for (var j int 0) (< j (. c count)) (++ j)
(if (&& (! (<= hi 0)) (! (>= lo 0)))
(break))
(var e (* octave-entry) (& [(. c entries) j]))
(if (>= lo (. e stop-bound)) (return lo))
(if (<= hi (- (. e stop-bound))) (return hi))
(var eval-y double (* y (. e frequency-y)))
(var dy double (fabs (- (. e past-y) eval-y)))
(+= lo (thresholding (* (net-max -1 (- (. e past-value) (* dy MAX-Y-SLOPE))) (. e amplitude))
(. e threshold) (. e smoothing)))
(+= hi (thresholding (* (net-min 1 (+ (. e past-value) (* dy MAX-Y-SLOPE))) (. e amplitude))
(. e threshold) (. e smoothing))))
(for (var j int 0) (< j (. c count)) (++ j)
(var e (* octave-entry) (& [(. c entries) j]))
(if (|| (>= value (. e stop-bound)) (<= value (- (. e stop-bound))))
(break))
(var eval-y double (* y (. e frequency-y)))
(var v double (evaluate-improved-xz (. e seed) (. e x) eval-y (. e z)))
(= (. e past-value) v)
(= (. e past-y) eval-y)
(+= value (thresholding (* v (. e amplitude)) (. e threshold) (. e smoothing))))
(return value))
;;; LerpedWeightedIndex2DMap: each pixel's indices (landforms, or
;;; geologic provinces) box-blurred over a radius, as (index, weight)
;;; lists. Radius 3 at most, so a pixel has at most 49 entries.
(define LANDFORM-BLUR 3)
(pub record landform-weights
((size int) (pad int) (counts (* u8)) (indices (* u8)) (weights (* float))))
(pub fn landform-weights/make ((m (* const int-map)) (radius int)) landform-weights
(var w landform-weights)
(var n int (. m size))
(= (. w size) n)
(= (. w pad) (. m pad-tl))
(= (. w counts) (calloc (* n n) 1))
(= (. w indices) (malloc (* n n 49)))
(= (. w weights) (malloc (* n n 49 (sizeof float))))
(for (var x int 0) (< x n) (++ x)
(for (var z int 0) (< z n) (++ z)
(var min-x int (if (< (- x radius) 0) 0 (- x radius)))
(var min-z int (if (< (- z radius) 0) 0 (- z radius)))
(var max-x int (if (> (+ x radius) (- n 1)) (- n 1) (+ x radius)))
(var max-z int (if (> (+ z radius) (- n 1)) (- n 1) (+ z radius)))
(var frac float (/ (cast 1 float) (cast (* (+ (- max-x min-x) 1) (+ (- max-z min-z) 1)) float)))
(var cell int (+ (* z n) x))
(var base int (* cell 49))
(var count int 0)
(for (var bx int min-x) (<= bx max-x) (++ bx)
(for (var bz int min-z) (<= bz max-z) (++ bz)
(var index int [(. m data) (+ (* bz n) bx)])
(var k int 0)
(while (&& (< k count) (!= [(. w indices) (+ base k)] index))
(++ k))
(if (== k count)
(do
(= [(. w indices) (+ base k)] (cast index u8))
(= [(. w weights) (+ base k)] frac)
(++ count))
(= [(. w weights) (+ base k)] (+ frac [(. w weights) (+ base k)])))))
(= [(. w counts) cell] (cast count u8))))
(return w))
(pub fn landform-weights/release ((w (* landform-weights))) void
(free (. w counts))
(free (. w indices))
(free (. w weights)))
(fn half-bilerp ((w (* const landform-weights)) (left int) (right int) (lerp float)
(out (* float)) (overall float)) void
(for (var k int 0) (< k [(. w counts) left]) (++ k)
(var i int [(. w indices) (+ (* left 49) k)])
(+= [out i] (* (* (- (cast 1 float) lerp) [(. w weights) (+ (* left 49) k)]) overall)))
(for (var k int 0) (< k [(. w counts) right]) (++ k)
(var i int [(. w indices) (+ (* right 49) k)])
(+= [out i] (* (* lerp [(. w weights) (+ (* right 49) k)]) overall))))
;;; WeightsAt: the landform weights at landform-map position (x, z)
;;; (inner pixels), into OUT of LENGTH entries.
(pub fn landform-weights/at ((w (* const landform-weights)) (x float) (z float) (out (* float))
(length int)) void
(for (var i int 0) (< i length) (++ i)
(= [out i] 0))
(var pad int (. w pad))
(var half float 0.5)
(var xl int (+ (cast (floor (cast (- x half) double)) int) pad))
(var zl int (+ (cast (floor (cast (- z half) double)) int) pad))
(var fx float (- x (+ (cast (- xl pad) float) half)))
(var fz float (- z (+ (cast (- zl pad) float) half)))
(var n int (. w size))
(half-bilerp w (+ (* zl n) xl) (+ (* zl n) xl 1) fx out (- (cast 1 float) fz))
(half-bilerp w (+ (* (+ zl 1) n) xl) (+ (* (+ zl 1) n) xl 1) fx out fz))
;;; The terrain generator ------------------------------------------------
(pub record terra
((g (* worldgen)) (config river-config) (sampler river-sampler)
(size-y int) (sea-level int) (octaves int) (noise-scale float)
(noise terrain-noise) (distort-x (* simplex-sum)) (distort-z (* simplex-sum))
(geo-upheaval (* simplex-noise)) (valley fast-noise) (river landform) (river-index int)
(landform-count int) (y-thresholds [* float 129])
;; the region last asked for
(rx int) (rz int) (has-region bool) (landform-map int-map) (ocean-map int-map)
(upheaval-map int-map) (weights landform-weights)
;; the river plates last asked for
(plates [* river-plate 4]) (plate-used [u64 4]) (plate-clock u64) (segments (* int))
(segment-cap int)))
(var gradients-ready bool false)
(fn scaled ((v double) (scale float)) double
(return (/ v scale)))
;;; Rivers' river landform: almost flat, a few blocks above sea level,
;;; with the sixth octave only.
(fn river-landform ((size-y int) (octaves int)) landform
(var l landform)
(memset (& l) 0 (sizeof l))
(snprintf (. l code) 64 "riverlandform")
(= (. l octave-count) 9)
(= [(. l octaves) 5] 1)
(= (. l octave-threshold-count) 9)
(var modifier float (/ (cast 256 float) (cast size-y float)))
(var sea float (cast 0.4313725490196078 float))
(var block float (* (/ sea (cast 110 float)) modifier))
(= (. l y-key-count) 4)
(= [(. l y-keys) 0] sea)
(= [(. l y-keys) 1] (+ sea (* block (cast 4 float))))
(= [(. l y-keys) 2] (+ sea (* block (cast 9 float))))
(= [(. l y-keys) 3] (+ sea (* block (cast 15 float))))
(= (. l y-key-threshold-count) 4)
(= [(. l y-key-thresholds) 0] 1)
(= [(. l y-key-thresholds) 1] (cast 0.5 float))
(= [(. l y-key-thresholds) 2] (cast 0.25 float))
(= [(. l y-key-thresholds) 3] 0)
(landform/init-terrain (& l) octaves size-y)
(return l))
(pub fn terra/make ((g (* worldgen)) (config river-config)) (* terra)
(if (! gradients-ready)
(do (init-gradients) (= gradients-ready true)))
(var t (* terra) (calloc 1 (sizeof terra)))
(var s (* world-settings) (& (. g settings)))
(= (. t g) g)
(= (. t config) config)
(= (. t size-y) (. s size-y))
(= (. t sampler) (river-sampler/make (& config) (. s size-y)))
(= (. t sea-level) (cast (* 0.4313725490196078 (. s size-y)) int))
(= (. t octaves) (terrain-octave-count (. s size-y)))
(var scale float (/ (cast (. s size-y) float) (cast 256 float)))
(if (< scale (cast 1 float)) (= scale 1))
(= (. t noise-scale) scale)
(var seed i64 (. s seed))
(var base double (/ (* 0.0005 0.6123724356957945) scale))
(= (. t noise count) (. t octaves))
(for (var i int 0) (< i (. t octaves)) (++ i)
(= [(. t noise frequencies) i] (* (pow 2 i) base))
(= [(. t noise seeds) i] (cast (+ (* (cast seed u64) 65599) (cast i u64)) i64)))
(var damps [double 4] #(55 40 30 10))
(var dfreqs [double 4])
(= [dfreqs 0] (scaled (/ 1 5.0) scale))
(= [dfreqs 1] (scaled (/ 1 2.50) scale))
(= [dfreqs 2] (scaled (/ 1 1.250) scale))
(= [dfreqs 3] (scaled (/ 1 0.65) scale))
(= (. t distort-x) (simplex-sum/make damps dfreqs 4 (+ (. s seed) 9876 0)))
(= (. t distort-z) (simplex-sum/make damps dfreqs 4 (+ (. s seed) 9876 2)))
(var gamps [double 6] #(55 40 30 15 7 4))
(var gfreqs [double 6])
(= [gfreqs 0] (scaled (/ 1.0 5.5) scale))
(= [gfreqs 1] (scaled (/ 1.1 2.75) scale))
(= [gfreqs 2] (scaled (/ 1.2 1.375) scale))
(= [gfreqs 3] (scaled (/ 1.2 0.715) scale))
(= [gfreqs 4] (scaled (/ 1.2 0.45) scale))
(= [gfreqs 5] (scaled (/ 1.2 0.25) scale))
(= (. t geo-upheaval) (simplex-noise/make gamps gfreqs 6 (+ (. s seed) 9876 1)))
(= (. t valley) (fast-noise/make 0 (cast 0.0008 float) 2 (cast 0.5 float) 2))
(= (. t river) (river-landform (. s size-y) (. t octaves)))
(= (. t landform-count) (. g landform-table count))
(= (. t river-index) (. t landform-count))
(for (var i int 0) (< i (. t landform-count)) (++ i)
(= [(. t y-thresholds) i] (. [(. g landform-table all) i] y-thresholds)))
(= [(. t y-thresholds) (. t river-index)] (. t river y-thresholds))
(return t))
(fn release-region ((t (* terra))) void
(if (! (. t has-region))
(return))
(free (. t landform-map data))
(free (. t ocean-map data))
(free (. t upheaval-map data))
(landform-weights/release (& (. t weights)))
(= (. t has-region) false))
(pub fn terra/free ((t (* terra))) void
(release-region t)
(for (var i int 0) (< i 4) (++ i)
(if [(. t plates) i] (river-plate/free [(. t plates) i])))
(free (. t segments))
(free (. t river y-thresholds))
(free (. t distort-x))
(free (. t distort-z))
(free (. t geo-upheaval))
(free t))
(fn use-region ((t (* terra)) (rx int) (rz int)) void
(if (&& (. t has-region) (== (. t rx) rx) (== (. t rz) rz))
(return))
(release-region t)
(worldgen/map (. t g) MAP-LANDFORM 0 rx rz (& (. t landform-map)))
(worldgen/map (. t g) MAP-OCEAN 0 rx rz (& (. t ocean-map)))
(worldgen/map (. t g) MAP-UPHEAVAL 0 rx rz (& (. t upheaval-map)))
(= (. t weights) (landform-weights/make (& (. t landform-map)) LANDFORM-BLUR))
(= (. t rx) rx)
(= (. t rz) rz)
(= (. t has-region) true))
(fn use-plate ((t (* terra)) (px int) (pz int)) (* river-plate)
(++ (. t plate-clock))
(var oldest int 0)
(for (var i int 0) (< i 4) (++ i)
(var p (* river-plate) [(. t plates) i])
(if (&& p (== (. p px) px) (== (. p pz) pz))
(do (= [(. t plate-used) i] (. t plate-clock)) (return p)))
(if (< [(. t plate-used) i] [(. t plate-used) oldest])
(= oldest i)))
(if [(. t plates) oldest]
(river-plate/free [(. t plates) oldest]))
(var p (* river-plate) (river-plate/make (. t config) (& (. t g oceans)) (. t size-y) px pz))
(= [(. t plates) oldest] p)
(= [(. t plate-used) oldest] (. t plate-clock))
(if (> (+ (. p segment-count) 1) (. t segment-cap))
(do
(= (. t segment-cap) (+ (. p segment-count) 1))
(= (. t segments) (realloc (. t segments) (* (. t segment-cap) (sizeof int))))))
(return p))
;;; IntMapExt.GetValues: a region map's four pixels around chunk
;;; (cx, cz), as UL UR BL BR.
(fn chunk-corners ((m (* const int-map)) (cx int) (cz int) (out (* int))) void
(var lx int (% cx 16))
(var lz int (% cz 16))
(var inner int (- (. m size) (. m pad-tl) (. m pad-br)))
(var factor float (/ (cast inner float) (cast 16 float)))
(var x0 int (cast (* lx factor) int))
(var z0 int (cast (* lz factor) int))
(var x1 int (cast (+ (* lx factor) factor) int))
(var z1 int (cast (+ (* lz factor) factor) int))
(var p int (. m pad-tl))
(= [out 0] [(. m data) (+ (* (+ z0 p) (. m size)) x0 p)])
(= [out 1] [(. m data) (+ (* (+ z0 p) (. m size)) x1 p)])
(= [out 2] [(. m data) (+ (* (+ z1 p) (. m size)) x0 p)])
(= [out 3] [(. m data) (+ (* (+ z1 p) (. m size)) x1 p)]))
;;; IntMapData.LerpForChunk.
(fn lerp-for-chunk ((c (* const int)) (lx int) (lz int)) float
(var delta float (/ (cast 1 float) (cast 32 float)))
(var x float (* (cast lx float) delta))
(var z float (* (cast lz float) delta))
(var top float (+ (cast [c 0] float) (* (- (cast [c 1] float) (cast [c 0] float)) x)))
(var bot float (+ (cast [c 2] float) (* (- (cast [c 3] float) (cast [c 2] float)) x)))
(return (+ top (* (- bot top) z))))
(fn interpolated-octaves ((t (* const terra)) (weights (* const float)) (amps (* double))
(thresholds (* double))) void
(for (var o int 0) (< o (. t octaves)) (++ o)
(var a double 0)
(var th double 0)
(for (var i int 0) (< i (. t landform-count)) (++ i)
(var w float [weights i])
(if (== w 0)
(continue))
(var l (* const landform) (& [(. (. t g) landform-table all) i]))
(+= a (* [(. l octaves) o] w))
(+= th (* [(. l octave-thresholds) o] w)))
(= [amps o] a)
(= [thresholds o] th)))
(fn bilerp ((a double) (b double) (c double) (d double) (x double) (z double)) double
(var top double (+ a (* (- b a) x)))
(var bot double (+ c (* (- d c) x)))
(return (+ top (* (- bot top) z))))
(fn isotropic ((dx double) (dz double) (threshold double) (maximum double) (out (* double))) void
(var mag2 double (+ (* dx dx) (* dz dz)))
(var th2 double (* threshold threshold))
(if (<= mag2 th2)
(do (= [out 0] 0) (= [out 1] 0) (return)))
(var base double (/ (- mag2 th2) mag2))
(var max2 double (* maximum maximum))
(var slide double (* base (/ max2 (- max2 th2))))
(*= slide slide)
(var force double (* slide (/ (- maximum threshold) maximum)))
(= [out 0] (* dx force))
(= [out 1] (* dz force)))
;;; A height is the top solid block, 0 if there is none.
;;; Heights of the columns of chunk (cx, cz) from local (lx0, lz0) to
;;; (lx1, lz1) inclusive, every STEP-th, into HEIGHTS[lz * 32 + lx].
(fn heights-in ((t (* terra)) (cx int) (cz int) (lx0 int) (lz0 int) (lx1 int) (lz1 int)
(step int) (heights (* u16))) void
(use-region t (/ cx 16) (/ cz 16))
(var per-plate int (river-plate/chunks (& (. t config))))
(var plate (* river-plate) (use-plate t (/ cx per-plate) (/ cz per-plate)))
(var seg-count int (river-plate/chunk-segments plate cx cz (. t segments)))
(var ocean [int 4])
(var upheaval [int 4])
(chunk-corners (& (. t ocean-map)) cx cz ocean)
(chunk-corners (& (. t upheaval-map)) cx cz upheaval)
(var oceanicity-fac float (* (cast (/ (. t size-y) 256) float) (cast 0.33333 float)))
(var n int (+ (. t landform-count) 1))
(var w (* float) (calloc n (sizeof float)))
(var lf-inner int (- (. t landform-map size) (* 2 (. t landform-map pad-tl))))
(var pixel float (cast (/ lf-inner 16) float))
(var base-x float (* (cast (% cx 16) float) pixel))
(var base-z float (* (cast (% cz 16) float) pixel))
(var oct-a [double 64])
(var oct-t [double 64])
(for (var k int 0) (< k 4) (++ k)
;; TODO sex#59: put the (if ...)s in the sums below once a
;; conditional operand keeps its parentheses.
(var ox float (if (& k 1) pixel 0))
(var oz float (if (& k 2) pixel 0))
(landform-weights/at (& (. t weights)) (+ base-x ox) (+ base-z oz) w n)
(interpolated-octaves t w (+ oct-a (* k 16)) (+ oct-t (* k 16))))
(var delta float (/ (cast 1 float) (cast 32 float)))
(var block-step float (* pixel delta))
(var max-distortion double (* (+ 55 40 30 10) 1.4142135623730951))
(var size-y int (. t size-y))
(var top-y int (- size-y 2))
(var taper int (cast (* (cast size-y float) (cast 0.9 float)) int))
(var amps [double 16])
(var ths [double 16])
(var c (* river-config) (& (. t config)))
(for (var lz int lz0) (<= lz lz1) (+= lz step)
(for (var lx int lx0) (<= lx lx1) (+= lx step)
(var wx int (+ (* cx 32) lx))
(var wz int (+ (* cz 32) lz))
(var sample river-sample (river-sampler/sample (& (. t sampler)) plate (. t segments)
seg-count wx wz))
(var river-lerp float 1)
(if (< (. sample distance) (. c max-valley-width))
(do
(var valley double (.get (& (. t valley)) wx wz))
(= valley (* valley (. c noise-expansion)))
(if (< valley -1) (= valley -1))
(if (> valley 1) (= valley 1))
(+= valley 1)
(/= valley 2)
(var to-min double (- (cast 1 float) (. c valley-strength-min)))
(var to-max double (- (cast 1 float) (. c valley-strength-max)))
(= valley (+ (* (/ (- valley 0) (- 1 0)) (- to-max to-min)) to-min))
(if (< valley 0.02) (= valley 0.02))
(if (< valley 1)
(do
(var inv double (/ (- (. sample distance) 0) (- (. c max-valley-width) 0)))
(= river-lerp (cast (+ valley (* (- 1 valley) inv)) float))
(= river-lerp (powf river-lerp 2))))))
(landform-weights/at (& (. t weights)) (+ base-x (* (cast lx float) block-step))
(+ base-z (* (cast lz float) block-step)) w n)
(var fx double (* (cast lx float) delta))
(var fz double (* (cast lz float) delta))
(if (< river-lerp (cast 1 float))
(do
(var blend float (- (cast 1 float) river-lerp))
(for (var i int 0) (< i n) (++ i)
(*= [w i] river-lerp))
(+= [w (. t river-index)] blend)
(for (var o int 0) (< o (. t octaves)) (++ o)
(var na double (bilerp [oct-a o] [oct-a (+ 16 o)] [oct-a (+ 32 o)] [oct-a (+ 48 o)] fx fz))
(var nt double (bilerp [oct-t o] [oct-t (+ 16 o)] [oct-t (+ 32 o)] [oct-t (+ 48 o)] fx fz))
(= [amps o] (+ na (* (- [(. t river octaves) o] na) blend)))
(= [ths o] (+ nt (* (- [(. t river octave-thresholds) o] nt) blend)))))
(for (var o int 0) (< o (. t octaves)) (++ o)
(= [amps o] (bilerp [oct-a o] [oct-a (+ 16 o)] [oct-a (+ 32 o)] [oct-a (+ 48 o)] fx fz))
(= [ths o] (bilerp [oct-t o] [oct-t (+ 16 o)] [oct-t (+ 32 o)] [oct-t (+ 48 o)] fx fz))))
(var dist-x double)
(var dist-z double)
(fair-warp (. t distort-x) (. t distort-z) (/ wx 400.0) (/ wz 400.0) (& dist-x) (& dist-z))
(var dt [double 2])
(isotropic (* dist-x 4) (* dist-z 4) 40 (* 4 max-distortion) dt)
(var dg [double 2])
(isotropic (* dist-x 10) (* dist-z 10) 10 (* 10 max-distortion) dg)
(var upheaval-strength float (* (lerp-for-chunk upheaval lx lz) river-lerp))
(var oceanicity float (* (lerp-for-chunk ocean lx lz) oceanicity-fac))
(var geo float (* (cast (.noise-2d (. t geo-upheaval) (/ (+ wx [dg 0]) 400)
(/ (+ wz [dg 1]) 400)) float)
(cast 0.9 float)))
(var multiplier float (fminf 0 (- (cast 0.5 float) geo)))
(var dist-y float (+ oceanicity (* upheaval-strength multiplier)))
(var col column-noise)
(column-noise/make (& (. t noise)) 0.25 amps ths (+ wx [dt 0]) (+ wz [dt 1]) (& col))
(var slide float (- dist-y (cast (cast (floor dist-y) int) float)))
(var bank-blocks int (cast (* (. sample bank) (- size-y (. t sea-level))) int))
(var baseline int (+ (. t sea-level) (. c height-boost)))
(var carved bool (<= (. sample distance) 0))
(var height int 0)
(for (var y int 1) (<= y top-y) (++ y)
(var yb int (cast (floor (+ (cast y float) dist-y)) int))
(if (< yb 0) (= yb 0))
(if (> yb top-y) (= yb top-y))
(var threshold double 0)
(for (var i int 0) (< i n) (++ i)
(var wi float [w i])
(if (== wi 0)
(continue))
(var th (* float) [(. t y-thresholds) i])
(var lerped float (+ [th yb] (* (- [th (+ yb 1)] [th yb]) slide)))
(+= threshold (* wi lerped)))
(if (&& (> y taper) (< dist-y -2))
(do
(var up double (- dist-y))
(if (< up (- y size-y)) (= up (- y size-y)))
(if (> up y) (= up y))
(+= threshold (/ (* (- y taper) up) (* 40 255.0)))))
(var solid bool false)
(if (<= threshold (. col bound-min))
(= solid true)
(! (< threshold (. col bound-max)))
(break)
(= solid (> (column-noise/sign (& col) y (- (tanh-curve-inverse threshold))) 0)))
(if (&& solid carved
(! (|| (<= y (- baseline bank-blocks))
(>= (cast y float) (+ baseline (* bank-blocks (. c top-factor)))))))
(= solid false))
(if solid (= height y)))
(= [heights (+ (* lz 32) lx)] (cast height u16))))
(free w))
;;; Heights of the columns of chunk (cx, cz) whose local x and z are
;;; multiples of STEP, into HEIGHTS[lz * 32 + lx]; the rest are left
;;; alone.
(pub fn terra/chunk-heights ((t (* terra)) (cx int) (cz int) (step int) (heights (* u16))) void
(heights-in t cx cz 0 0 31 31 step heights))
;;; The height of the one column at block (x, z): 1/1024th of a chunk's
;;; work, for a cursor or a probe.
(pub fn terra/column-height ((t (* terra)) (x int) (z int)) int
(var heights [u16 1024])
(var lx int (% x 32))
(var lz int (% z 32))
(heights-in t (/ x 32) (/ z 32) lx lz lx lz 1 heights)
(return [heights (+ (* lz 32) lx)]))