lay out rivers as the Rivers mod does

Every stored riverDistance column matches. Plates are built from 25
ocean samples per zone rather than a region map per zone, and the
ocean distance from a ring search that finds what the mod's BFS does,
so a plate costs 0.08 s; workers cache a few, as a wide view spans
several.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-08 18:40:53 +03:00
parent 5ad62524f1
commit 517d279c8d
10 changed files with 1257 additions and 38 deletions

View File

@@ -15,6 +15,8 @@
(import noise)
(import shape)
(import worldgen)
(import fastnoise)
(import rivers)
(import settings)
(import tiles)
@@ -27,17 +29,20 @@
(define MIN-ZOOM 0.0625)
(define MAX-ZOOM 32.0)
(var layer-names [(* const char) 6]
#("Landform" "Climate" "Geologic province" "Upheaval" "Ocean" "Rock strata thickness"))
(var layer-names [(* const char) 7]
#("Landform" "Climate" "Geologic province" "Upheaval" "Ocean" "Rock strata thickness"
"Rivers"))
(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)))
(record hover ((valid bool) (rx int) (rz int) (maps region-maps)
(plate (* river-plate)) (segments (* int))))
(record app
((window (* SDL-Window)) (renderer (* SDL-Renderer)) (ctx (* struct nk-context))
(assets-dir [char 1024]) (world-path [char 1024])
(assets-dir [char 1024]) (world-path [char 1024]) (rivers-path [char 1024])
(rivers river-config) (sampler river-sampler)
(source world-source) (edit world-source) (g (* worldgen)) (tiles (* tiles))
(center-x double) (center-z double) (zoom double) (layer int) (stratum int)
(mouse-x float) (mouse-y float) (dragging bool) (ui-hovered bool)
@@ -82,7 +87,10 @@
(fn release-hover ((a (* app))) void
(if (. a hover valid)
(region-maps/free (& (. a hover maps))))
(= (. a hover valid) false))
(= (. a hover valid) false)
(if (. a hover plate)
(river-plate/free (. a hover plate)))
(= (. a hover plate) NULL))
;;; Makes SOURCE the world shown: a fresh generator for the panels,
;;; and every tile regenerated.
@@ -101,10 +109,21 @@
(release-hover a)
(if (>= (. a stratum) (. g strata count))
(= (. a stratum) 0))
(.set-settings (. a tiles) (. source settings))
(= (. a sampler) (river-sampler/make (& (. a rivers)) (. source settings size-y)))
(.set-settings (. a tiles) (. source settings) (. a rivers))
(snprintf (. a status) (sizeof (. a status)) "seed %d" (. source settings seed))
(return true))
;;; ModConfig/rivers.json, or the mod's defaults for an empty path.
(fn load-rivers ((a (* app))) void
(var config river-config (river-config/defaults))
(if (&& (* (. a rivers-path)) (! (river-config/load (. a rivers-path) (& config))))
(do
(snprintf (. a status) (sizeof (. a status)) "cannot load %s" (. a rivers-path))
(return)))
(= (. a rivers) config)
(apply a (. a source)))
(fn load-world ((a (* app))) void
(var source world-source)
(if (world-source/load (. a world-path) (& source))
@@ -124,6 +143,11 @@
(var db double (. wb distance))
(return (- (> da db) (< da db))))
;;; River samples per region side: about one per point on screen,
;;; at most one per 8 blocks.
(fn river-resolution ((zoom double)) int
(return (if (<= zoom 8) 64 (if (<= zoom 16) 32 16))))
;;; Requests the visible tiles, nearest the centre first, and draws
;;; those that are ready.
(fn draw-map ((a (* app))) void
@@ -154,7 +178,7 @@
(for (var i int 0) (< i n) (++ i)
(var k tile-key)
(= (. k map) (. a layer))
(= (. k stratum) (. a stratum))
(= (. k stratum) (if (== (. a layer) LAYER-RIVERS) (river-resolution (. a zoom)) (. a stratum)))
(= (. k rx) (. [list i] rx))
(= (. k rz) (. [list i] rz))
(var tex (* SDL-Texture) (.request (. a tiles) (. a renderer) k))
@@ -201,7 +225,7 @@
(fn draw-settings ((a (* app))) void
(var ctx (* struct nk-context) (. a ctx))
(var e (* world-settings) (& (. a edit settings)))
(if (nk-begin ctx "World" (nk-rect 10 10 290 520)
(if (nk-begin ctx "World" (nk-rect 10 10 290 580)
(| NK-WINDOW-BORDER NK-WINDOW-MOVABLE NK-WINDOW-TITLE NK-WINDOW-MINIMIZABLE
NK-WINDOW-SCALABLE))
(do
@@ -215,6 +239,16 @@
(sizeof (. a world-path)) nk-filter-default)
(if (nk-button-label ctx "Load")
(load-world a))
(nk-layout-row-dynamic ctx 22 1)
(nk-label ctx "Rivers config (empty: defaults)" NK-TEXT-LEFT)
(nk-layout-row-template-begin ctx 24)
(nk-layout-row-template-push-dynamic ctx)
(nk-layout-row-template-push-static ctx 50)
(nk-layout-row-template-end ctx)
(nk-edit-string-zero-terminated ctx NK-EDIT-FIELD (. a rivers-path)
(sizeof (. a rivers-path)) nk-filter-default)
(if (nk-button-label ctx "Load")
(load-rivers a))
(nk-layout-row-dynamic ctx 8 1)
(nk-spacing ctx 1)
@@ -263,11 +297,11 @@
(var w int)
(var h int)
(window-size a (& w) (& h))
(if (nk-begin ctx "Layer" (nk-rect (- w 250) 10 240 220)
(if (nk-begin ctx "Layer" (nk-rect (- w 250) 10 240 240)
(| 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 6) (++ i)
(for (var i int 0) (< i 7) (++ i)
(if (nk-option-label ctx [layer-names i] (== (. a layer) i))
(= (. a layer) i)))
(if (== (. a layer) MAP-STRATA)
@@ -286,6 +320,23 @@
(var pz int (+ (. m pad-tl) (/ (* (- bz (* rz REGION)) inner) REGION)))
(return [(. m data) (+ (* pz (. m size)) px)]))
;;; 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))))
(var cx int (/ x 32))
(var cz int (/ z 32))
(var p (* river-plate) (. a hover plate))
(if (|| (! p) (!= (. p px) (/ cx per-plate)) (!= (. p pz) (/ cz per-plate)))
(do
(if p (river-plate/free p))
(= p (river-plate/make (. a rivers) (& (. a g oceans)) (. a source settings size-y)
(/ cx per-plate) (/ cz per-plate)))
(= (. a hover plate) p)
(= (. a hover segments) (realloc (. a hover segments)
(* (+ (. p segment-count) 1) (sizeof int))))))
(var count int (river-plate/chunk-segments p cx cz (. a hover segments)))
(return (river-sampler/sample (& (. a sampler)) p (. a hover segments) count x z)))
(fn draw-info ((a (* app))) void
(var ctx (* struct nk-context) (. a ctx))
(var w int)
@@ -304,7 +355,7 @@
(= (. a hover valid) true)
(= (. a hover rx) rx)
(= (. a hover rz) rz)))
(if (nk-begin ctx "Info" (nk-rect 10 (- h 230) 290 220)
(if (nk-begin ctx "Info" (nk-rect 10 (- h 250) 290 240)
(| NK-WINDOW-BORDER NK-WINDOW-MOVABLE NK-WINDOW-TITLE NK-WINDOW-MINIMIZABLE))
(do
(nk-layout-row-dynamic ctx 18 1)
@@ -324,7 +375,10 @@
(scaled-temperature (& (>> c 16) 255)) (& (>> c 8) 255) (& c 255))
(nk-labelf ctx NK-TEXT-LEFT "ocean %d upheaval %d"
(sample (& (. m ocean)) rx rz bx bz)
(sample (& (. m upheaval)) rx rz bx bz))))
(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))))
(nk-labelf ctx NK-TEXT-LEFT "%d tiles pending" (. a pending))
(nk-label ctx (. a status) NK-TEXT-LEFT)))
(nk-end ctx))
@@ -417,7 +471,8 @@
(= (. source settings) (world-settings/defaults))
(snprintf (. source seed-text) (sizeof (. source seed-text)) "0")))
(var workers int (- (SDL-GetNumLogicalCPUCores) 1))
(= (. a tiles) (tiles/start (. source settings) (. a assets-dir) workers))
(= (. a rivers) (river-config/defaults))
(= (. a tiles) (tiles/start (. source settings) (. a rivers) (. a assets-dir) workers))
(if (<= (. a zoom) 0)
(= (. a zoom) 4))
(if (! (apply a source))