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

@@ -276,12 +276,95 @@
(pub record landform
((code [char 64]) (weight double) (use-climate bool) (min-temp float) (max-temp float)
(min-rain int) (max-rain int) (chance float) (first-mutation int) (mutation-count int)
(colour u32)))
(colour u32)
;; terrain shape
(octaves [double 16]) (octave-count int) (octave-thresholds [double 16])
(octave-threshold-count int) (y-keys [float 32]) (y-key-thresholds [float 32])
(y-key-count int) (y-key-threshold-count int) (y-thresholds (* float))))
(fn json-number ((o (* cJSON)) (key (* const char)) (fallback double)) double
(var v (* cJSON) (cJSON-GetObjectItem o key))
(return (if (cJSON-IsNumber v) (cJSON-GetNumberValue v) fallback)))
(fn json-doubles-into ((o (* cJSON)) (key (* const char)) (out (* double)) (max int)) int
(var arr (* cJSON) (cJSON-GetObjectItem o key))
(if (! (cJSON-IsArray arr))
(return -1))
(var n int 0)
(for (var v (* cJSON) (. arr child)) (&& v (< n max)) (= v (. v next))
(= [out n] (cJSON-GetNumberValue v))
(++ n))
(return n))
(fn json-floats-into ((o (* cJSON)) (key (* const char)) (out (* float)) (max int)) int
(var tmp [double 32])
(var n int (json-doubles-into o key tmp max))
(for (var i int 0) (< i n) (++ i)
(= [out i] (cast [tmp i] float)))
(return n))
;;; The terrain arrays of a landform or mutation; -1 counts mean the
;;; JSON left them out. TerrainOctaveThresholds defaults to 11 zeros.
(fn read-terrain ((o (* cJSON)) (l (* landform))) void
(= (. l octave-count) (json-doubles-into o "terrainOctaves" (. l octaves) 16))
(= (. l octave-threshold-count) (json-doubles-into o "terrainOctaveThresholds"
(. l octave-thresholds) 16))
(= (. l y-key-count) (json-floats-into o "terrainYKeyPositions" (. l y-keys) 32))
(= (. l y-key-threshold-count) (json-floats-into o "terrainYKeyThresholds"
(. l y-key-thresholds) 32)))
;;; LandformVariant.Init: octaves extended to the world height's count,
;;; and the Y thresholds lerped between the key positions, inverted.
(pub fn landform/init-terrain ((l (* landform)) (octaves int) (size-y int)) void
(var m int (- octaves (. l octave-count)))
(if (> m 0)
(do
(var last double [(. l octaves) (- (. l octave-count) 1)])
(var ext [double 16])
(var add-sum double 0)
(for (var i int 0) (< i m) (++ i)
(var v double (pow 0.8 (+ i 1)))
(= [ext i] (* last v))
(+= add-sum v))
(var prev-sum double 0)
(for (var i int 0) (< i (. l octave-count)) (++ i)
(+= prev-sum [(. l octaves) i]))
(for (var i int 0) (< i (. l octave-count)) (++ i)
(*= [(. l octaves) i] (/ (+ prev-sum add-sum) prev-sum)))
(for (var i int 0) (< i m) (++ i)
(= [(. l octaves) (+ (. l octave-count) i)] [ext i]))
(= (. l octave-count) octaves)))
(var n int (- octaves (. l octave-threshold-count)))
(if (> n 0)
(do
(var last double [(. l octave-thresholds) (- (. l octave-threshold-count) 1)])
(for (var i int 0) (< i n) (++ i)
(= [(. l octave-thresholds) (+ (. l octave-threshold-count) i)] last))
(= (. l octave-threshold-count) octaves)))
(= (. l y-thresholds) (malloc (* size-y (sizeof float))))
(var cur float 1)
(var cur-y float 0)
(var pos int -1)
(var size float (cast size-y float))
(for (var y int 0) (< y size-y) (++ y)
(if (>= (+ pos 1) (. l y-key-threshold-count))
(do (= [(. l y-thresholds) y] 1) (continue)))
(if (>= (cast y float) (* [(. l y-keys) (+ pos 1)] size))
(do
(= cur [(. l y-key-thresholds) (+ pos 1)])
(= cur-y (* [(. l y-keys) (+ pos 1)] size))
(++ pos)))
(var next float 0)
(var next-y float size)
(if (< (+ pos 1) (. l y-key-threshold-count))
(do
(= next [(. l y-key-thresholds) (+ pos 1)])
(= next-y (* [(. l y-keys) (+ pos 1)] size))))
(var range float (- next-y cur-y))
(var distance float (/ (- (cast y float) cur-y) range))
(= [(. l y-thresholds) y] (- (cast 1 float) (+ cur (* (- next cur) distance))))))
(fn read-landform ((o (* cJSON)) (l (* landform))) void
(memset l 0 (sizeof landform))
(var code (* cJSON) (cJSON-GetObjectItem o "code"))
@@ -295,24 +378,53 @@
(= (. l max-rain) (cast (json-number o "maxRain" 255) int))
(= (. l chance) (cast (json-number o "chance" 0) float))
(= (. l colour) (hex-colour o))
(read-terrain o l)
(if (< (. l octave-threshold-count) 0)
(do
(memset (. l octave-thresholds) 0 (sizeof (. l octave-thresholds)))
(= (. l octave-threshold-count) 11)))
(= (. l first-mutation) -1))
(pub record landform-table ((all [landform MAX_LANDFORMS]) (count int) (variant-count int)))
(pub fn landform-table/load ((json (* cJSON)) (t (* landform-table))) void
(pub fn terrain-octave-count ((size-y int)) int
(return (+ 9 (/ (- size-y 256) 128))))
(pub fn landform-table/load ((json (* cJSON)) (t (* landform-table)) (size-y int)) void
(memset t 0 (sizeof landform-table))
(var variants (* cJSON) (cJSON-GetObjectItem json "variants"))
(for (var v (* cJSON) (. variants child)) v (= v (. v next))
(read-landform v (& [(. t all) (. t count)]))
(++ (. t count)))
(= (. t variant-count) (. t count))
(var octaves int (terrain-octave-count size-y))
(for (var k int 0) (< k (. t count)) (++ k)
(landform/init-terrain (& [(. t all) k]) octaves size-y))
(var i int 0)
(for (var v (* cJSON) (. variants child)) v (= v (. v next))
(var mutations (* cJSON) (cJSON-GetObjectItem v "mutations"))
(var parent (* landform) (& [(. t all) i]))
(= (. parent first-mutation) (. t count))
(for (var m (* cJSON) (if mutations (. mutations child) NULL)) m (= m (. m next))
(read-landform m (& [(. t all) (. t count)]))
(var mut (* landform) (& [(. t all) (. t count)]))
(read-landform m mut)
;; Missing arrays are the parent's, as already extended; but not
;; the octave thresholds, whose C# field has an initializer and so
;; is never missing.
(if (< (. mut octave-count) 0)
(do
(memcpy (. mut octaves) (. parent octaves) (sizeof (. mut octaves)))
(= (. mut octave-count) (. parent octave-count))))
(if (< (. mut y-key-count) 0)
(do
(memcpy (. mut y-keys) (. parent y-keys) (sizeof (. mut y-keys)))
(= (. mut y-key-count) (. parent y-key-count))))
(if (< (. mut y-key-threshold-count) 0)
(do
(memcpy (. mut y-key-thresholds) (. parent y-key-thresholds)
(sizeof (. mut y-key-thresholds)))
(= (. mut y-key-threshold-count) (. parent y-key-threshold-count))))
(landform/init-terrain mut octaves size-y)
(++ (. t count))
(++ (. parent mutation-count)))
(++ i)))
@@ -871,7 +983,7 @@
(= (. g settings) settings)
(var seed i64 (. settings seed))
(= (. g provinces) (province-layer/make (+ seed 3 5) provinces))
(landform-table/load landforms (& (. g landform-table)))
(landform-table/load landforms (& (. g landform-table)) (. settings size-y))
(var climate climate-noise (climate-noise/make seed (& (. g settings))))
(= (. g landforms) (landform-layer/make (+ seed 4 12) (& (. g landform-table)) climate
(& (. g settings))))
@@ -974,6 +1086,8 @@
(worldgen/map g MAP-STRATA i rx rz (& [(. out strata) i]))))
(pub fn worldgen/free ((g (* worldgen))) void
(for (var i int 0) (< i (. g landform-table count)) (++ i)
(free (. [(. g landform-table all) i] y-thresholds)))
(free (. g provinces wobble-x))
(free (. g provinces wobble-z))
(for (var i int 0) (< i (. g provinces count)) (++ i)