generate terrain height as Rivers' GenTerra does

98.2% of stored columns match exactly; the rest is the dry seabed
raise and surface caves, both later passes that the strata never see.
GenTerra thresholds the new terrain noise through the old tanh curve's
inverse, and matching that quirk is what moved it from 87%.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-08 19:10:54 +03:00
parent 517d279c8d
commit c4c7ab9968
10 changed files with 1248 additions and 33 deletions

View File

@@ -21,10 +21,13 @@
(import worldgen)
(import fastnoise)
(import rivers)
(import terra)
(pub define MAX-TILES 16384)
;;; Not a worldgen map: Rivers' distance to the nearest river.
;;; Not worldgen maps: Rivers' distance to the nearest river, and the
;;; terrain height.
(pub define LAYER-RIVERS 6)
(pub define LAYER-HEIGHT 7)
;;; River tiles are sampled at a resolution the viewer picks by zoom,
;;; in the key's `stratum': a column costs too much to sample them all
;;; for an overview.
@@ -187,6 +190,52 @@
(= (* size) res)
(return px))
;;; Terrain height, shaded: water by depth under sea level, land by
;;; height, lit from the north-west.
(fn height-colour ((h int) (west int) (north int) (sea int) (size-y int)) u32
(if (< h sea)
(return (lerp-colour 6400472 1186659 (/ (* (if (> (- sea h) 40) 40 (- sea h)) 255) 40))))
(var t int (/ (* (- h sea) 255) (- size-y sea)))
(var base u32 (if (< t 32) (lerp-colour 5605944 8299066 (* t 8))
(if (< t 96) (lerp-colour 8299066 9922899 (/ (* (- t 32) 255) 64))
(lerp-colour 9922899 15263976 (/ (* (- t 96) 255) 159)))))
(var slope int (+ (- h west) (- h north)))
(var light int (+ 128 (* slope 24)))
(if (< light 0) (= light 0))
(if (> light 255) (= light 255))
(if (> light 128)
(return (lerp-colour base 16777215 (/ (* (- light 128) 160) 127))))
(return (lerp-colour 0 base (+ 96 (/ (* light 159) 128)))))
(fn render-heights ((tr (* terra)) (k tile-key) (size (* int))) (* u32)
(var res int (. k stratum))
(var step int (/ 512 res))
(var hs (* int) (malloc (* res res (sizeof int))))
(var chunk [u16 1024])
(var per-chunk int (/ 32 step))
(if (< per-chunk 1) (= per-chunk 1))
(for (var cz int 0) (< cz 16) (++ cz)
(for (var cx int 0) (< cx 16) (++ cx)
(if (|| (!= (% (* cx 32) step) 0) (!= (% (* cz 32) step) 0))
(continue))
(terra/chunk-heights tr (+ (* (. k rx) 16) cx) (+ (* (. k rz) 16) cz) (if (> step 32) 32 step)
chunk)
(for (var dz int 0) (< dz per-chunk) (++ dz)
(for (var dx int 0) (< dx per-chunk) (++ dx)
(= [hs (+ (* (+ (/ (* cz 32) step) dz) res) (/ (* cx 32) step) dx)]
[chunk (+ (* dz step 32) (* dx step))])))))
(var px (* u32) (malloc (* res res (sizeof u32))))
(for (var z int 0) (< z res) (++ z)
(for (var x int 0) (< x res) (++ x)
(var h int [hs (+ (* z res) x)])
(var west int (if (> x 0) [hs (+ (* z res) x -1)] h))
(var north int (if (> z 0) [hs (+ (* (- z 1) res) x)] h))
(= [px (+ (* z res) x)]
(c-bit-or 4278190080 (height-colour h west north (. tr sea-level) (. tr size-y))))))
(free hs)
(= (* size) res)
(return px))
;;; Workers -------------------------------------------------------
(fn worker-main ((arg (* void))) (* void)
@@ -195,6 +244,7 @@
(var g-generation int -1)
(var cache river-cache)
(memset (& cache) 0 (sizeof cache))
(var tr (* terra) NULL)
(pthread-mutex-lock (& (. t lock)))
(while (! (. t quit))
(if (>= (. t queue-pos) (. t queue-len))
@@ -215,15 +265,20 @@
(if (!= g-generation generation)
(do
(if tr (terra/free tr))
(= tr NULL)
(if g (worldgen/free g))
(release-river-cache (& cache))
(= g (worldgen/make settings (. t assets-dir)))
(if g (= tr (terra/make g rivers)))
(= (. cache sampler) (river-sampler/make (& rivers) (. settings size-y)))
(= g-generation generation)))
(var size int 0)
(var px (* u32) NULL)
(if (&& g (== (. key map) LAYER-RIVERS))
(= px (render-rivers g (& cache) (& rivers) key (& size)))
(&& tr (== (. key map) LAYER-HEIGHT))
(= px (render-heights tr key (& size)))
g
(= px (render g key (& size))))
@@ -238,6 +293,7 @@
(if (&& (== generation (. t generation)) (== (. tl state) TILE-WORKING))
(= (. tl state) TILE-QUEUED)))))
(pthread-mutex-unlock (& (. t lock)))
(if tr (terra/free tr))
(if g (worldgen/free g))
(release-river-cache (& cache))
(return NULL))