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

@@ -17,6 +17,7 @@
(import worldgen)
(import fastnoise)
(import rivers)
(import terra)
(import settings)
(import tiles)
@@ -29,15 +30,16 @@
(define MIN-ZOOM 0.0625)
(define MAX-ZOOM 32.0)
(var layer-names [(* const char) 7]
(var layer-names [(* const char) 8]
#("Landform" "Climate" "Geologic province" "Upheaval" "Ocean" "Rock strata thickness"
"Rivers"))
"Rivers" "Terrain height"))
(var climate-names [(* const char) 5] #("temperate" "hot" "warm" "cool" "icy"))
;;; What the region under the cursor holds, generated on the main
;;; thread when the cursor moves to another region.
(record hover ((valid bool) (rx int) (rz int) (maps region-maps)
(plate (* river-plate)) (segments (* int))))
(plate (* river-plate)) (segments (* int))
(terra (* terra)) (cx int) (cz int) (has-chunk bool) (heights [u16 1024])))
(record app
((window (* SDL-Window)) (renderer (* SDL-Renderer)) (ctx (* struct nk-context))
@@ -90,7 +92,11 @@
(= (. a hover valid) false)
(if (. a hover plate)
(river-plate/free (. a hover plate)))
(= (. a hover plate) NULL))
(= (. a hover plate) NULL)
(if (. a hover terra)
(terra/free (. a hover terra)))
(= (. a hover terra) NULL)
(= (. a hover has-chunk) false))
;;; Makes SOURCE the world shown: a fresh generator for the panels,
;;; and every tile regenerated.
@@ -148,6 +154,16 @@
(fn river-resolution ((zoom double)) int
(return (if (<= zoom 8) 64 (if (<= zoom 16) 32 16))))
;;; Height samples per region side: one per block close up, down to
;;; one per chunk for an overview, where a column costs ~11 us.
(fn height-resolution ((zoom double)) int
(if (< zoom 1) (return 512))
(if (< zoom 2) (return 256))
(if (< zoom 4) (return 128))
(if (< zoom 8) (return 64))
(if (< zoom 16) (return 32))
(return 16))
;;; Requests the visible tiles, nearest the centre first, and draws
;;; those that are ready.
(fn draw-map ((a (* app))) void
@@ -178,7 +194,9 @@
(for (var i int 0) (< i n) (++ i)
(var k tile-key)
(= (. k map) (. a layer))
(= (. k stratum) (if (== (. a layer) LAYER-RIVERS) (river-resolution (. a zoom)) (. a stratum)))
(= (. k stratum) (. a stratum))
(if (== (. a layer) LAYER-RIVERS) (= (. k stratum) (river-resolution (. a zoom))))
(if (== (. a layer) LAYER-HEIGHT) (= (. k stratum) (height-resolution (. a zoom))))
(= (. k rx) (. [list i] rx))
(= (. k rz) (. [list i] rz))
(var tex (* SDL-Texture) (.request (. a tiles) (. a renderer) k))
@@ -297,11 +315,11 @@
(var w int)
(var h int)
(window-size a (& w) (& h))
(if (nk-begin ctx "Layer" (nk-rect (- w 250) 10 240 240)
(if (nk-begin ctx "Layer" (nk-rect (- w 250) 10 240 260)
(| NK-WINDOW-BORDER NK-WINDOW-MOVABLE NK-WINDOW-TITLE NK-WINDOW-MINIMIZABLE))
(do
(nk-layout-row-dynamic ctx 20 1)
(for (var i int 0) (< i 7) (++ i)
(for (var i int 0) (< i 8) (++ i)
(if (nk-option-label ctx [layer-names i] (== (. a layer) i))
(= (. a layer) i)))
(if (== (. a layer) MAP-STRATA)
@@ -320,6 +338,20 @@
(var pz int (+ (. m pad-tl) (/ (* (- bz (* rz REGION)) inner) REGION)))
(return [(. m data) (+ (* pz (. m size)) px)]))
;;; The terrain height at block (x, z), its chunk computed once.
(fn hover-height ((a (* app)) (x int) (z int)) int
(if (! (. a hover terra))
(= (. a hover terra) (terra/make (. a g) (. a rivers))))
(var cx int (/ x 32))
(var cz int (/ z 32))
(if (|| (! (. a hover has-chunk)) (!= (. a hover cx) cx) (!= (. a hover cz) cz))
(do
(terra/chunk-heights (. a hover terra) cx cz 1 (. a hover heights))
(= (. a hover cx) cx)
(= (. a hover cz) cz)
(= (. a hover has-chunk) true)))
(return [(. a hover heights) (+ (* (% z 32) 32) (% x 32))]))
;;; Rivers' sample at block (x, z), building the plate it needs.
(fn hover-river ((a (* app)) (x int) (z int)) river-sample
(var per-plate int (river-plate/chunks (& (. a rivers))))
@@ -355,7 +387,7 @@
(= (. a hover valid) true)
(= (. a hover rx) rx)
(= (. a hover rz) rz)))
(if (nk-begin ctx "Info" (nk-rect 10 (- h 250) 290 240)
(if (nk-begin ctx "Info" (nk-rect 10 (- h 270) 290 260)
(| NK-WINDOW-BORDER NK-WINDOW-MOVABLE NK-WINDOW-TITLE NK-WINDOW-MINIMIZABLE))
(do
(nk-layout-row-dynamic ctx 18 1)
@@ -378,7 +410,9 @@
(sample (& (. m upheaval)) rx rz bx bz))
(var r river-sample (hover-river a bx bz))
(nk-labelf ctx NK-TEXT-LEFT "river distance %.0f, bank depth %.3f"
(. r distance) (. r bank))))
(. r distance) (. r bank))
(nk-labelf ctx NK-TEXT-LEFT "terrain height %d (sea level %d)"
(hover-height a bx bz) (. a hover terra sea-level))))
(nk-labelf ctx NK-TEXT-LEFT "%d tiles pending" (. a pending))
(nk-label ctx (. a status) NK-TEXT-LEFT)))
(nk-end ctx))