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

@@ -32,12 +32,12 @@ $(B)/cJSON.o: cJSON/cJSON.c cJSON/cJSON.h | $(B)
$(B)/stb.o: src/stb.c third_party/stb/stb_image_write.h | $(B)
$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
VSMV_OBJ = vsmv cs pb json maps save img dump lcg noise shape worldgen settings verify cJSON stb
VSMV_OBJ = vsmv cs pb json maps save img dump lcg noise shape worldgen fastnoise rivers settings verify cJSON stb
$(B)/vsmv: $(VSMV_OBJ:%=$(B)/%.o)
$(CC) $^ -o $@ -lsqlite3
VIEWER_OBJ = viewer tiles settings worldgen shape noise lcg maps save pb json cs nk cJSON stb
VIEWER_OBJ = viewer tiles settings worldgen fastnoise rivers shape noise lcg maps save pb json cs nk cJSON stb
$(B)/vs-map-viewer: $(VIEWER_OBJ:%=$(B)/%.o)
$(CC) $^ -o $@ $(SDL_LIBS) -lsqlite3

View File

@@ -372,17 +372,41 @@ nothing here uses.
Deferred: the river config section moves to phase 4, where there is
something to configure.
** 4. Rivers layout
- Port =fastnoise= (OpenSimplex2 2D FBm) and =rivers=:
- zone ocean flags;
- distance to ocean;
- river tree, with the exact RNG call order;
- chunk segment sets;
- =SampleRiver=.
- =vsmv verify-rivers= compares per-column =riverDistance= against the
saved moddata in every stored map chunk.
- Viewer: river layer (segments plus the =riverDistance= field), computed
per plate in the background.
** 4. Rivers layout (done)
=src/rivers.sex= and =src/fastnoise.sex= port Rivers 5.0.3's layout
and its per-column sampler. =vsmv verify= now also recomputes
=riverDistance= for every stored map chunk:
- *6,558,720 / 6,558,720* columns match where Rivers stored the
moddata.
- *5,009 / 5,009* chunks without it rightly have no river within 300
blocks.
How the port works:
- *Ocean test.* The ocean test per zone is one blurred ocean pixel at
the zone centre's chunk: 25 raw samples instead of the mod's whole
region map per zone. =ocean-layer.blurred-at= computes it.
- *Ocean distance.* An expanding-ring search returns exactly what the
mod's pruned BFS finds, the Euclidean distance to the nearest ocean
zone.
- *Generation.* A FIFO queue and an LCG with the mod's call order.
=FindHighestZone='s stable sort is kept, ties to the first neighbour
in x-then-z order.
- *NaN directions.* .NET 10 converts NaN to int 0, saturating, and
=saturating-int= mirrors it. A NaN direction arises when the uphill
target is the start zone.
- *Speed.* A plate takes about 0.08 s.
- *Chunk lookups.* Segment lookups per chunk go through a 64 x 64 grid
over the plate.
Viewer:
- *Rivers layer.* Water, valley distance and Rivers' ocean zones. The
sample density follows the zoom: 64, 32 or 16 samples per region
side.
- *Plate cache.* Each worker keeps its last four plates. With one,
workers rebuilt plates constantly and the overview took 19 s; with
four, 0.7 s.
- *Hover info.* River distance and bank depth.
- *World panel.* A rivers config path; empty means the mod's defaults.
** 5. Terrain height
- Port =NewGenTerra='s per-column path:

View File

@@ -3,6 +3,12 @@
Language bugs and friction met while building vs-map-viewer, against
sexc at ~/dev/sex. Newest first. Unless said otherwise, sexc dfafe8b.
* =-INFINITY= is one symbol
=(var best double -INFINITY)= emits =_INFINITY=: the reader takes
the whole token as a symbol and the leading =-= is mangled like any
other. =(- INFINITY)= works. Negative literals like =-1= are fine, so
it's only symbolic constants, but the mangled name reads like a typo.
* A cast as the object of =.= loses its parentheses
=(. (cast pa (* const wanted)) distance)= emits
=(const wanted *)pa->distance=, which C reads as a cast of

145
src/fastnoise.sex Normal file
View File

@@ -0,0 +1,145 @@
;;; FastNoiseLite, the part Rivers uses: 2D OpenSimplex2 with FBm,
;;; built with FNLfloat = double. Coordinates stay double, noise values
;;; are float, as in the C#. Rivers seeds these with constants, so the
;;; river noise is the same in every world.
(include math.h)
(define PRIME-X 501125321)
(define PRIME-Y 1136930381)
;; 0x27d4eb2d
(define HASH-MUL 668265261)
;;; Generated from Rivers' FastNoise.cs Gradients2D; do not edit.
(var gradients [float 256]
#(0.130526192220052 0.99144486137381 0.38268343236509 0.923879532511287 0.608761429008721 0.793353340291235 0.793353340291235 0.608761429008721
0.923879532511287 0.38268343236509 0.99144486137381 0.130526192220051 0.99144486137381 -0.130526192220051 0.923879532511287 -0.38268343236509
0.793353340291235 -0.60876142900872 0.608761429008721 -0.793353340291235 0.38268343236509 -0.923879532511287 0.130526192220052 -0.99144486137381
-0.130526192220052 -0.99144486137381 -0.38268343236509 -0.923879532511287 -0.608761429008721 -0.793353340291235 -0.793353340291235 -0.608761429008721
-0.923879532511287 -0.38268343236509 -0.99144486137381 -0.130526192220052 -0.99144486137381 0.130526192220051 -0.923879532511287 0.38268343236509
-0.793353340291235 0.608761429008721 -0.608761429008721 0.793353340291235 -0.38268343236509 0.923879532511287 -0.130526192220052 0.99144486137381
0.130526192220052 0.99144486137381 0.38268343236509 0.923879532511287 0.608761429008721 0.793353340291235 0.793353340291235 0.608761429008721
0.923879532511287 0.38268343236509 0.99144486137381 0.130526192220051 0.99144486137381 -0.130526192220051 0.923879532511287 -0.38268343236509
0.793353340291235 -0.60876142900872 0.608761429008721 -0.793353340291235 0.38268343236509 -0.923879532511287 0.130526192220052 -0.99144486137381
-0.130526192220052 -0.99144486137381 -0.38268343236509 -0.923879532511287 -0.608761429008721 -0.793353340291235 -0.793353340291235 -0.608761429008721
-0.923879532511287 -0.38268343236509 -0.99144486137381 -0.130526192220052 -0.99144486137381 0.130526192220051 -0.923879532511287 0.38268343236509
-0.793353340291235 0.608761429008721 -0.608761429008721 0.793353340291235 -0.38268343236509 0.923879532511287 -0.130526192220052 0.99144486137381
0.130526192220052 0.99144486137381 0.38268343236509 0.923879532511287 0.608761429008721 0.793353340291235 0.793353340291235 0.608761429008721
0.923879532511287 0.38268343236509 0.99144486137381 0.130526192220051 0.99144486137381 -0.130526192220051 0.923879532511287 -0.38268343236509
0.793353340291235 -0.60876142900872 0.608761429008721 -0.793353340291235 0.38268343236509 -0.923879532511287 0.130526192220052 -0.99144486137381
-0.130526192220052 -0.99144486137381 -0.38268343236509 -0.923879532511287 -0.608761429008721 -0.793353340291235 -0.793353340291235 -0.608761429008721
-0.923879532511287 -0.38268343236509 -0.99144486137381 -0.130526192220052 -0.99144486137381 0.130526192220051 -0.923879532511287 0.38268343236509
-0.793353340291235 0.608761429008721 -0.608761429008721 0.793353340291235 -0.38268343236509 0.923879532511287 -0.130526192220052 0.99144486137381
0.130526192220052 0.99144486137381 0.38268343236509 0.923879532511287 0.608761429008721 0.793353340291235 0.793353340291235 0.608761429008721
0.923879532511287 0.38268343236509 0.99144486137381 0.130526192220051 0.99144486137381 -0.130526192220051 0.923879532511287 -0.38268343236509
0.793353340291235 -0.60876142900872 0.608761429008721 -0.793353340291235 0.38268343236509 -0.923879532511287 0.130526192220052 -0.99144486137381
-0.130526192220052 -0.99144486137381 -0.38268343236509 -0.923879532511287 -0.608761429008721 -0.793353340291235 -0.793353340291235 -0.608761429008721
-0.923879532511287 -0.38268343236509 -0.99144486137381 -0.130526192220052 -0.99144486137381 0.130526192220051 -0.923879532511287 0.38268343236509
-0.793353340291235 0.608761429008721 -0.608761429008721 0.793353340291235 -0.38268343236509 0.923879532511287 -0.130526192220052 0.99144486137381
0.130526192220052 0.99144486137381 0.38268343236509 0.923879532511287 0.608761429008721 0.793353340291235 0.793353340291235 0.608761429008721
0.923879532511287 0.38268343236509 0.99144486137381 0.130526192220051 0.99144486137381 -0.130526192220051 0.923879532511287 -0.38268343236509
0.793353340291235 -0.60876142900872 0.608761429008721 -0.793353340291235 0.38268343236509 -0.923879532511287 0.130526192220052 -0.99144486137381
-0.130526192220052 -0.99144486137381 -0.38268343236509 -0.923879532511287 -0.608761429008721 -0.793353340291235 -0.793353340291235 -0.608761429008721
-0.923879532511287 -0.38268343236509 -0.99144486137381 -0.130526192220052 -0.99144486137381 0.130526192220051 -0.923879532511287 0.38268343236509
-0.793353340291235 0.608761429008721 -0.608761429008721 0.793353340291235 -0.38268343236509 0.923879532511287 -0.130526192220052 0.99144486137381
0.38268343236509 0.923879532511287 0.923879532511287 0.38268343236509 0.923879532511287 -0.38268343236509 0.38268343236509 -0.923879532511287
-0.38268343236509 -0.923879532511287 -0.923879532511287 -0.38268343236509 -0.923879532511287 0.38268343236509 -0.38268343236509 0.923879532511287))
(pub record fast-noise
((seed int) (frequency float) (octaves int) (gain float) (lacunarity float)
(weighted-strength float) (bounding float)))
(fn fast-floor ((f double)) int
(return (if (>= f 0) (cast f int) (- (cast f int) 1))))
(fn hash ((seed int) (x int) (y int)) int
(return (* (^ seed x y) HASH-MUL)))
(fn grad-coord ((seed int) (x int) (y int) (xd float) (yd float)) float
(var h int (hash seed x y))
(^= h (>> h 15))
(&= h 254)
(return (+ (* xd [gradients h]) (* yd [gradients (c-bit-or h 1)]))))
;;; SingleSimplex: the 2D OpenSimplex2 kernel, after the skew.
(fn single-simplex ((seed int) (x double) (y double)) float
(var sqrt3 float 1.7320508075688772935274463415059)
(var g2 float (/ (- (cast 3 float) sqrt3) (cast 6 float)))
(var i int (fast-floor x))
(var j int (fast-floor y))
(var xi float (cast (- x i) float))
(var yi float (cast (- y j) float))
(var t float (* (+ xi yi) g2))
(var x0 float (- xi t))
(var y0 float (- yi t))
(*= i PRIME-X)
(*= j PRIME-Y)
(var one float 1)
(var two float 2)
(var a float (- (cast 0.5 float) (* x0 x0) (* y0 y0)))
(var n0 float 0)
(if (> a 0)
(= n0 (* (* a a) (* a a) (grad-coord seed i j x0 y0))))
(var c1 float (* (* (* two (- one (* two g2))) (- (/ one g2) two)) t))
(var c2 float (* (* (- two) (- one (* two g2))) (- one (* two g2))))
(var c float (+ c1 (+ c2 a)))
(var n2 float 0)
(if (> c 0)
(do
(var x2 float (+ x0 (- (* two g2) one)))
(var y2 float (+ y0 (- (* two g2) one)))
(= n2 (* (* c c) (* c c) (grad-coord seed (+ i PRIME-X) (+ j PRIME-Y) x2 y2)))))
(var n1 float 0)
(if (> y0 x0)
(do
(var x1 float (+ x0 g2))
(var y1 float (+ y0 (- g2 one)))
(var b float (- (cast 0.5 float) (* x1 x1) (* y1 y1)))
(if (> b 0)
(= n1 (* (* b b) (* b b) (grad-coord seed i (+ j PRIME-Y) x1 y1)))))
(do
(var x1 float (+ x0 (- g2 one)))
(var y1 float (+ y0 g2))
(var b float (- (cast 0.5 float) (* x1 x1) (* y1 y1)))
(if (> b 0)
(= n1 (* (* b b) (* b b) (grad-coord seed (+ i PRIME-X) j x1 y1))))))
(return (* (+ n0 n1 n2) (cast 99.83685446303647 float))))
(ops fast-noise
;; Rivers' Noise(seed, frequency, octaves, gain, lacunarity): FBm with
;; weighted strength -0.2.
(pub fn make ((seed int) (frequency float) (octaves int) (gain float) (lacunarity float))
fast-noise
(var n fast-noise #(fast-noise : seed frequency octaves gain lacunarity (cast -0.2 float) 0))
(var g float (if (< gain 0) (- gain) gain))
(var amp float g)
(var fractal float 1)
(for (var i int 1) (< i octaves) (++ i)
(+= fractal amp)
(*= amp g))
(= (. n bounding) (/ (cast 1 float) fractal))
(return n))
(pub fn get ((self (* const fast-noise)) (x double) (y double)) float
(*= x (. self frequency))
(*= y (. self frequency))
(var sqrt3 double 1.7320508075688772935274463415059)
(var f2 double (* 0.5 (- sqrt3 1)))
(var t double (* (+ x y) f2))
(+= x t)
(+= y t)
(var seed int (. self seed))
(var sum float 0)
(var amp float (. self bounding))
(for (var i int 0) (< i (. self octaves)) (++ i)
(var noise float (single-simplex seed x y))
(++ seed)
(+= sum (* noise amp))
(var m float (+ noise 1))
(if (> m 2) (= m 2))
(var b float (* m (cast 0.5 float)))
(*= amp (+ (cast 1 float) (* (. self weighted-strength) (- b (cast 1 float)))))
(*= x (. self lacunarity))
(*= y (. self lacunarity))
(*= amp (. self gain)))
(return sum)))

809
src/rivers.sex Normal file
View File

@@ -0,0 +1,809 @@
;;; The Rivers mod (5.0.3) river layout, and sampling it per column.
;;;
;;; Rivers lays out a whole "plate" of 128 x 128 zones of 256 blocks
;;; at once: which zones are ocean, how far each land zone is from the
;;; ocean, and then rivers grown uphill from the coast, node by node,
;;; from one LCGRandom(0) positioned at the plate. The world seed only
;;; enters through the ocean map, sampled without forced land.
;;;
;;; Positions are plate-local doubles, as in the mod. The order of
;;; every RNG call is the mod's: it is what the layout depends on.
(include stdlib.h string.h math.h)
(import maps)
(import lcg)
(import noise)
(import shape)
(import json)
(import worldgen)
(import fastnoise)
(pub record river-config
((min-fork-angle int) (fork-variation int) (normal-angle int)
(min-size float) (max-size float) (min-nodes int) (max-nodes int) (river-growth float)
(downhill-error int) (min-length int) (length-variation int) (zone-size int)
(zones-in-region int) (river-spawn-chance float) (river-split-chance float)
(lake-chance float) (segments-in-river int) (segment-offset double) (base-depth double)
(river-depth double) (river-padding-blocks int) (river-octaves int)
(river-frequency float) (river-lacunarity float) (river-gain float)
(river-distortion-strength int) (max-valley-width double) (ocean-threshold float)
(valley-strength-max float) (valley-strength-min float) (noise-expansion float)
(height-boost int) (top-factor float)))
(pub fn river-config/defaults () river-config
(var c river-config)
(memset (& c) 0 (sizeof c))
(= (. c min-fork-angle) 10)
(= (. c fork-variation) 35)
(= (. c normal-angle) 20)
(= (. c valley-strength-max) 1)
(= (. c valley-strength-min) (cast 0.4 float))
(= (. c noise-expansion) (cast 1.5 float))
(= (. c river-padding-blocks) 128)
(= (. c min-size) 14)
(= (. c max-size) 50)
(= (. c min-nodes) 8)
(= (. c max-nodes) 20)
(= (. c river-growth) 3)
(= (. c downhill-error) 1)
(= (. c min-length) 150)
(= (. c length-variation) 200)
(= (. c zone-size) 256)
(= (. c zones-in-region) 128)
(= (. c river-spawn-chance) (cast 0.2 float))
(= (. c river-split-chance) (cast 0.35 float))
(= (. c lake-chance) (cast 0.1 float))
(= (. c segments-in-river) 3)
(= (. c segment-offset) 40)
(= (. c base-depth) 0.1)
(= (. c river-depth) 0.022)
(= (. c height-boost) 8)
(= (. c top-factor) 1)
(= (. c river-octaves) 3)
(= (. c river-frequency) (cast 0.0075 float))
(= (. c river-lacunarity) 3)
(= (. c river-gain) (cast 0.3 float))
(= (. c river-distortion-strength) 10)
(= (. c max-valley-width) 150)
(= (. c ocean-threshold) 30)
(return c))
(fn json-get ((o (* cJSON)) (key (* const char)) (out (* double))) bool
(var v (* cJSON) (cJSON-GetObjectItem o key))
(if (! (cJSON-IsNumber v))
(return false))
(= (* out) (cJSON-GetNumberValue v))
(return true))
;;; ModConfig/rivers.json over the defaults.
(pub fn river-config/load ((path (* const char)) (c (* river-config))) bool
(var json (* cJSON) (json-load path))
(if (! json)
(return false))
(var v double)
(if (json-get json "minForkAngle" (& v)) (= (. c min-fork-angle) (cast v int)))
(if (json-get json "forkVariation" (& v)) (= (. c fork-variation) (cast v int)))
(if (json-get json "normalAngle" (& v)) (= (. c normal-angle) (cast v int)))
(if (json-get json "valleyStrengthMax" (& v)) (= (. c valley-strength-max) (cast v float)))
(if (json-get json "valleyStrengthMin" (& v)) (= (. c valley-strength-min) (cast v float)))
(if (json-get json "noiseExpansion" (& v)) (= (. c noise-expansion) (cast v float)))
(if (json-get json "riverPaddingBlocks" (& v)) (= (. c river-padding-blocks) (cast v int)))
(if (json-get json "minSize" (& v)) (= (. c min-size) (cast v float)))
(if (json-get json "maxSize" (& v)) (= (. c max-size) (cast v float)))
(if (json-get json "minNodes" (& v)) (= (. c min-nodes) (cast v int)))
(if (json-get json "maxNodes" (& v)) (= (. c max-nodes) (cast v int)))
(if (json-get json "riverGrowth" (& v)) (= (. c river-growth) (cast v float)))
(if (json-get json "downhillError" (& v)) (= (. c downhill-error) (cast v int)))
(if (json-get json "minLength" (& v)) (= (. c min-length) (cast v int)))
(if (json-get json "lengthVariation" (& v)) (= (. c length-variation) (cast v int)))
(if (json-get json "zoneSize" (& v)) (= (. c zone-size) (cast v int)))
(if (json-get json "zonesInRegion" (& v)) (= (. c zones-in-region) (cast v int)))
(if (json-get json "riverSpawnChance" (& v)) (= (. c river-spawn-chance) (cast v float)))
(if (json-get json "riverSplitChance" (& v)) (= (. c river-split-chance) (cast v float)))
(if (json-get json "lakeChance" (& v)) (= (. c lake-chance) (cast v float)))
(if (json-get json "segmentsInRiver" (& v)) (= (. c segments-in-river) (cast v int)))
(if (json-get json "segmentOffset" (& v)) (= (. c segment-offset) v))
(if (json-get json "baseDepth" (& v)) (= (. c base-depth) v))
(if (json-get json "riverDepth" (& v)) (= (. c river-depth) v))
(if (json-get json "heightBoost" (& v)) (= (. c height-boost) (cast v int)))
(if (json-get json "topFactor" (& v)) (= (. c top-factor) (cast v float)))
(if (json-get json "riverOctaves" (& v)) (= (. c river-octaves) (cast v int)))
(if (json-get json "riverFrequency" (& v)) (= (. c river-frequency) (cast v float)))
(if (json-get json "riverLacunarity" (& v)) (= (. c river-lacunarity) (cast v float)))
(if (json-get json "riverGain" (& v)) (= (. c river-gain) (cast v float)))
(if (json-get json "riverDistortionStrength" (& v))
(= (. c river-distortion-strength) (cast v int)))
(if (json-get json "maxValleyWidth" (& v)) (= (. c max-valley-width) v))
(if (json-get json "oceanThreshold" (& v)) (= (. c ocean-threshold) (cast v float)))
(cJSON-Delete json)
(return true))
;;; Geometry -----------------------------------------------------------
(pub record vec2 ((x double) (y double)))
(fn v2 ((x double) (y double)) vec2 (return #(vec2 : x y)))
(fn v-add ((a vec2) (b vec2)) vec2 (return (v2 (+ (. a x) (. b x)) (+ (. a y) (. b y)))))
(fn v-sub ((a vec2) (b vec2)) vec2 (return (v2 (- (. a x) (. b x)) (- (. a y) (. b y)))))
(fn v-mul ((a vec2) (f double)) vec2 (return (v2 (* (. a x) f) (* (. a y) f))))
(fn v-eq ((a vec2) (b vec2)) bool (return (&& (== (. a x) (. b x)) (== (. a y) (. b y)))))
;;; OpenTK's Normalized: scaled by the reciprocal of the length, so a
;;; zero vector comes out NaN.
(fn v-normalized ((a vec2)) vec2
(var scale double (/ 1.0 (sqrt (+ (* (. a x) (. a x)) (* (. a y) (. a y))))))
(return (v2 (* (. a x) scale) (* (. a y) scale))))
(fn degrees-to-normal ((degrees double)) vec2
(var radians double (* degrees (/ M-PI 180)))
(return (v2 (cos radians) (sin radians))))
(fn normal-to-degrees ((n vec2)) double
(return (* (atan2 (. n y) (. n x)) (/ 180 M-PI))))
;;; RiverMath.GetProjection: how far along START..END POINT projects,
;;; clamped to 0..1.
(fn projection ((p vec2) (start vec2) (end vec2)) float
(var dx double (- (. end x) (. start x)))
(var dy double (- (. end y) (. start y)))
(var v double (+ (* (- (. p x) (. start x)) dx) (* (- (. p y) (. start y)) dy)))
(/= v (+ (* dx dx) (* dy dy)))
(return (cast (if (< v 0) 0 (if (> v 1) 1 v)) float)))
(fn distance-to-line ((p vec2) (s vec2) (e vec2)) double
(if (<= (+ (* (- (. s x) (. e x)) (- (. p x) (. e x))) (* (- (. s y) (. e y)) (- (. p y) (. e y)))) 0)
(return (sqrt (+ (* (- (. p x) (. e x)) (- (. p x) (. e x)))
(* (- (. p y) (. e y)) (- (. p y) (. e y)))))))
(if (<= (+ (* (- (. e x) (. s x)) (- (. p x) (. s x))) (* (- (. e y) (. s y)) (- (. p y) (. s y)))) 0)
(return (sqrt (+ (* (- (. p x) (. s x)) (- (. p x) (. s x)))
(* (- (. p y) (. s y)) (- (. p y) (. s y)))))))
(var num double (fabs (+ (- (* (- (. e y) (. s y)) (. p x)) (* (- (. e x) (. s x)) (. p y)))
(- (* (. e x) (. s y)) (* (. e y) (. s x))))))
(return (/ num (sqrt (+ (* (- (. s y) (. e y)) (- (. s y) (. e y)))
(* (- (. s x) (. e x)) (- (. s x) (. e x))))))))
(fn lines-intersect ((sa vec2) (ea vec2) (sb vec2) (eb vec2)) bool
(var c1 bool (> (* (- (. eb y) (. sa y)) (- (. sb x) (. sa x)))
(* (- (. sb y) (. sa y)) (- (. eb x) (. sa x)))))
(var c2 bool (> (* (- (. eb y) (. ea y)) (- (. sb x) (. ea x)))
(* (- (. sb y) (. ea y)) (- (. eb x) (. ea x)))))
(var c3 bool (> (* (- (. sb y) (. sa y)) (- (. ea x) (. sa x)))
(* (- (. ea y) (. sa y)) (- (. sb x) (. sa x)))))
(var c4 bool (> (* (- (. eb y) (. sa y)) (- (. ea x) (. sa x)))
(* (- (. ea y) (. sa y)) (- (. eb x) (. sa x)))))
(return (&& (!= c1 c2) (!= c3 c4))))
(pub record envelope ((min-x double) (min-y double) (max-x double) (max-y double)))
(fn intersects ((a (* const envelope)) (b (* const envelope))) bool
(return (&& (<= (. a min-x) (. b max-x)) (<= (. a min-y) (. b max-y))
(>= (. a max-x) (. b min-x)) (>= (. a max-y) (. b min-y)))))
;;; .NET 10 converts out-of-range doubles to int by saturating, and NaN
;;; to 0. A zero-length river direction relies on it.
(fn saturating-int ((v double)) int
(if (!= v v) (return 0))
(if (<= v -2147483648.0) (return INT32_MIN))
(if (>= v 2147483647.0) (return INT32_MAX))
(return (cast v int)))
;;; The layout --------------------------------------------------------
(pub record river-node
((start vec2) (end vec2) (parent int) (river int) (end-size float) (start-size float)
(is-end bool) (first-segment int) (segment-count int) (is-lake bool) (bounds envelope)))
(pub record river-segment
((start vec2) (end vec2) (mid vec2) (node int) (parent int)
(first-child int) (last-child int) (next-sibling int) (parent-invalid bool)
(bounds envelope)))
(pub record river ((start vec2) (first-node int) (alive bool)))
;;; Nodes of one river, in order: `river-nodes' threads them.
(pub record request ((angle double) (start vec2) (stage int) (parent int) (river int) (error int)))
(pub define PLATE-GRID 64)
(pub record river-plate
((config river-config) (px int) (pz int)
(ocean (* bool)) (ocean-distance (* double))
(nodes (* river-node)) (node-count int) (node-cap int)
(node-next (* int))
(segments (* river-segment)) (segment-count int) (segment-cap int)
(rivers (* river)) (river-count int) (river-cap int) (river-last (* int))
(queue (* request)) (queue-head int) (queue-tail int) (queue-cap int)
(rng lcg)
;; A 64 x 64 grid over the plate, each cell listing the segments
;; whose sampling bounds touch it.
(grid-start (* int)) (grid-items (* int))))
(fn zone-index ((p (* const river-plate)) (x int) (z int)) int
(return (+ (* z (. p config zones-in-region)) x)))
(fn zone-center ((p (* const river-plate)) (x int) (z int)) vec2
(var size int (. p config zone-size))
(return (v2 (+ (* x size) (/ size 2)) (+ (* z size) (/ size 2)))))
(fn zone-at ((p (* const river-plate)) (x double) (z double)) int
(var n int (. p config zones-in-region))
(var zx int (saturating-int (fmin (fmax (/ x (. p config zone-size)) 0) (- n 1))))
(var zz int (saturating-int (fmin (fmax (/ z (. p config zone-size)) 0) (- n 1))))
;; Math.Clamp keeps NaN, which then converts to 0.
(if (!= x x) (= zx 0))
(if (!= z z) (= zz 0))
(return (zone-index p zx zz)))
(fn plate-size ((p (* const river-plate))) int
(return (* (. p config zone-size) (. p config zones-in-region))))
(fn within ((p (* const river-plate)) (v vec2) (padding double)) bool
(var size double (plate-size p))
(return (&& (>= (. v x) padding) (<= (. v x) (- size padding))
(>= (. v y) padding) (<= (. v y) (- size padding)))))
;;; SetZoneOceanicity: the blurred ocean map, without forced land, at
;;; the chunk of the zone's centre, scaled by world height.
(fn zone-is-ocean ((p (* const river-plate)) (oceans (* ocean-layer)) (size-y int) (x int) (z int)) bool
(var size int (. p config zone-size))
(var world-x int (+ (* (. p px) (plate-size p)) (* x size) (/ size 2)))
(var world-z int (+ (* (. p pz) (plate-size p)) (* z size) (/ size 2)))
(var value int (.blurred-at oceans (/ world-x 32) (/ world-z 32)))
(var factor float (* (/ size-y 256) (cast 0.33333 float)))
(return (> (cast (* (cast value float) factor) double) (. p config ocean-threshold))))
;;; The distance from each land zone's centre to the nearest ocean
;;; zone's, capped at two plates. The mod finds it with a pruned BFS
;;; that reaches exactly the nearest ocean; rings around the zone find
;;; the same one without visiting every zone.
(fn ocean-distances ((p (* river-plate))) void
(var n int (. p config zones-in-region))
(var cap double (* 2 (plate-size p)))
(for (var x int 0) (< x n) (++ x)
(for (var z int 0) (< z n) (++ z)
(var i int (zone-index p x z))
(if [(. p ocean) i]
(do (= [(. p ocean-distance) i] -1) (continue)))
(var best i64 INT64_MAX)
(for (var r int 1) (< r n) (++ r)
;; Nothing in ring r is closer than r zones.
(if (<= (* (cast r i64) r) best)
(for (var dx int (- r)) (<= dx r) (++ dx)
(for (var dz int (- r)) (<= dz r) (++ dz)
(if (&& (!= (abs dx) r) (!= (abs dz) r))
(continue))
(var ox int (+ x dx))
(var oz int (+ z dz))
(if (|| (< ox 0) (>= ox n) (< oz 0) (>= oz n))
(continue))
(if [(. p ocean) (zone-index p ox oz)]
(do
(var d2 i64 (+ (* (cast dx i64) dx) (* (cast dz i64) dz)))
(if (< d2 best) (= best d2))))))
(break)))
;; Zone centres are whole multiples of the zone size apart, so
;; this is exactly the mod's sqrt(pow(dx, 2) + pow(dz, 2)).
(var size double (. p config zone-size))
(var d double (if (== best INT64_MAX) INFINITY (sqrt (* (cast best double) size size))))
(= [(. p ocean-distance) i] (fmin d cap)))))
;;; Growing storage -----------------------------------------------------
(fn grow-array ((data (* void)) (cap (* int)) (need int) (item size-t)) (* void)
(if (< need (* cap))
(return data))
(var new-cap int (if (> (* cap) 0) (* 2 (* cap)) 64))
(while (<= new-cap need) (*= new-cap 2))
(= (* cap) new-cap)
(return (realloc data (* (cast new-cap size-t) item))))
(fn add-segment ((p (* river-plate)) (start vec2) (end vec2) (node int)) int
(= (. p segments) (grow-array (. p segments) (& (. p segment-cap)) (. p segment-count)
(sizeof river-segment)))
(var s (* river-segment) (& [(. p segments) (. p segment-count)]))
(memset s 0 (sizeof river-segment))
(= (. s start) start)
(= (. s end) end)
;; startPos + ((endPos - startPos) / 2), as OpenTK divides
(= (. s mid) (v-add start (v2 (/ (- (. end x) (. start x)) 2) (/ (- (. end y) (. start y)) 2))))
(= (. s node) node)
(= (. s parent) -1)
(= (. s first-child) -1)
(= (. s last-child) -1)
(= (. s next-sibling) -1)
(++ (. p segment-count))
(return (- (. p segment-count) 1)))
(fn add-child ((p (* river-plate)) (parent int) (child int)) void
(var ps (* river-segment) (& [(. p segments) parent]))
(if (< (. ps last-child) 0)
(= (. ps first-child) child)
(= (. [(. p segments) (. ps last-child)] next-sibling) child))
(= (. ps last-child) child)
(= (. [(. p segments) child] parent) parent))
(fn node-envelope ((p (* const river-plate)) (start vec2) (end vec2)) envelope
(var pad double (. p config river-padding-blocks))
(return #(envelope : (- (fmin (. start x) (. end x)) pad) (- (fmin (. start y) (. end y)) pad)
(+ (fmax (. start x) (. end x)) pad) (+ (fmax (. start y) (. end y)) pad))))
;;; new RiverNode(...) without custom segments: three segments, the
;;; inner joints pushed sideways by up to segmentOffset at random.
(fn add-node ((p (* river-plate)) (start vec2) (end vec2) (river int) (parent int)) int
(= (. p nodes) (grow-array (. p nodes) (& (. p node-cap)) (. p node-count) (sizeof river-node)))
(var cap int (. p node-cap))
(= (. p node-next) (realloc (. p node-next) (* (cast cap size-t) (sizeof int))))
(var index int (. p node-count))
(++ (. p node-count))
(var n (* river-node) (& [(. p nodes) index]))
(memset n 0 (sizeof river-node))
(= (. n start) start)
(= (. n end) end)
(= (. n parent) parent)
(= (. n river) river)
(= (. n end-size) 0)
(= (. n start-size) 1)
(= (. n is-end) true)
(= (. n bounds) (node-envelope p start end))
(= [(. p node-next) index] -1)
(var count int (. p config segments-in-river))
(var dir vec2 (v-normalized (v-sub end start)))
(var side vec2 (v2 (- (. dir y)) (. dir x)))
(= (. n first-segment) (. p segment-count))
(= (. n segment-count) count)
(var previous-end vec2 start)
(for (var i int 0) (< i count) (++ i)
(var r double (.next-double (& (. p rng))))
(var offset double (+ (- (. p config segment-offset)) (* (* r (. p config segment-offset)) 2)))
(var seg-end vec2 end)
(if (!= i (- count 1))
(do
(var t double (/ (cast (+ i 1) double) count))
(= seg-end (v2 (+ (. start x) (* (- (. end x) (. start x)) t))
(+ (. start y) (* (- (. end y) (. start y)) t))))
(+= (. seg-end x) (* offset (. side x)))
(+= (. seg-end y) (* offset (. side y)))))
(add-segment p previous-end seg-end index)
(= previous-end seg-end))
;; ConnectSegments
(var first int (. n first-segment))
(for (var i int 0) (< i count) (++ i)
(if (> i 0)
(add-child p (+ first i -1) (+ first i))
(>= parent 0)
(add-child p (+ (. [(. p nodes) parent] first-segment)
(. [(. p nodes) parent] segment-count) -1)
first)))
;; ValidateSegments
(for (var i int 0) (< i count) (++ i)
(var s (* river-segment) (& [(. p segments) (+ first i)]))
(if (== i 0)
(do
(if (< parent 0)
(do (= (. s parent-invalid) true) (continue)))
(var pn (* river-node) (& [(. p nodes) parent]))
(var last (* river-segment)
(& [(. p segments) (+ (. pn first-segment) (. p config segments-in-river) -1)]))
(var pr float (projection (. s start) (. s mid) (. last mid)))
(if (|| (< pr (cast 0.2 float)) (> pr (cast 0.8 float)))
(= (. s parent-invalid) true))
(continue)))
(var pr float (projection (. s start) (. s mid) (. [(. p segments) (+ first i -1)] mid)))
(if (|| (< pr (cast 0.2 float)) (> pr (cast 0.8 float)))
(= (. s parent-invalid) true)))
(return index))
(fn river-append-node ((p (* river-plate)) (river int) (node int)) void
(var last int [(. p river-last) river])
(if (< last 0)
(= (. [(. p rivers) river] first-node) node)
(= [(. p node-next) last] node))
(= [(. p river-last) river] node))
(fn enqueue ((p (* river-plate)) (r request)) void
(= (. p queue) (grow-array (. p queue) (& (. p queue-cap)) (. p queue-tail) (sizeof request)))
(= [(. p queue) (. p queue-tail)] r)
(++ (. p queue-tail)))
;;; RiverRegion.GenerateRiver: one node of a river, or false where the
;;; mod refuses it. The RNG draws are the mod's, in its order.
(fn generate-river ((p (* river-plate)) (angle double) (start vec2) (stage int) (parent int)
(river int) (error int)) bool
(var c (* river-config) (& (. p config)))
(if (> stage (. c max-nodes))
(return false))
(var normal vec2 (degrees-to-normal angle))
(var length int (+ (. c min-length) (.next-int (& (. p rng)) (. c length-variation))))
(var end vec2 (v-add start (v-mul normal length)))
(var intersecting bool false)
(var probe vec2 (v-add (v-mul (v-sub end start) 0.5) end))
(for (var n int (. [(. p rivers) river] first-node)) (>= n 0) (= n [(. p node-next) n])
(var other (* river-node) (& [(. p nodes) n]))
(if (|| (v-eq start (. other end)) (v-eq start (. other start)))
(continue))
(if (lines-intersect start probe (. other start) (. other end))
(do (= intersecting true) (break))))
(var padding double (+ (+ (. c segment-offset) (. c max-valley-width)) (. c max-size)))
(if (! (&& (within p start padding) (within p end padding)))
(= intersecting true))
(var start-dist double [(. p ocean-distance) (zone-at p (. start x) (. start y))])
(var end-zone int (zone-at p (. end x) (. end y)))
(var end-dist double [(. p ocean-distance) end-zone])
(if (> start-dist end-dist)
(do
(if (== error 0) (= intersecting true))
(-- error)))
(if (&& [(. p ocean) end-zone] (> stage 2))
(= intersecting true))
(if intersecting
(return false))
(var bounds envelope (node-envelope p start end))
(for (var n int 0) (< n (. p node-count)) (++ n)
(var other (* river-node) (& [(. p nodes) n]))
(if (&& (! (. other is-lake)) (intersects (& (. other bounds)) (& bounds))
(!= (. other river) river))
(return false)))
(var node int (add-node p start end river parent))
(river-append-node p river node)
(if (>= parent 0)
(= (. [(. p nodes) parent] is-end) false))
(var split float (.next-float (& (. p rng))))
(if (&& (< split (. c river-split-chance)) (>= parent 0))
(do
(var a1 double (+ angle (+ (. c min-fork-angle) (.next-int (& (. p rng)) (. c fork-variation)))))
(var a2 double (- angle (+ (. c min-fork-angle) (.next-int (& (. p rng)) (. c fork-variation)))))
(enqueue p #(request : a1 end (+ stage 1) node river error))
(enqueue p #(request : a2 end (+ stage 1) node river error)))
(do
(var sign int 0)
(while (== sign 0)
(= sign (+ -1 (.next-int (& (. p rng)) 3))))
(var a1 double (- angle (* (.next-int (& (. p rng)) (. c normal-angle)) sign)))
(enqueue p #(request : a1 end (+ stage 1) node river error))))
(return true))
;;; FindHighestZone: up to HOPS steps to the neighbour (or self) with
;;; the greatest ocean distance, ties to the first in x, then z order.
(fn find-highest ((p (* const river-plate)) (x int) (z int) (hops int)) int
(var n int (. p config zones-in-region))
(var best-x int x)
(var best-z int z)
(var best double (- INFINITY))
(var found bool false)
(for (var dx int -1) (<= dx 1) (++ dx)
(for (var dz int -1) (<= dz 1) (++ dz)
(var ox int (+ x dx))
(var oz int (+ z dz))
(if (|| (< ox 0) (> ox (- n 1)) (< oz 0) (> oz (- n 1)))
(continue))
(var d double [(. p ocean-distance) (zone-index p ox oz)])
(if (|| (! found) (> d best))
(do (= best d) (= best-x ox) (= best-z oz) (= found true)))))
(if (|| (&& (== best-x x) (== best-z z)) (== hops 0))
(return (zone-index p x z)))
(return (find-highest p best-x best-z (- hops 1))))
(fn assign-size ((p (* river-plate)) (node int) (end-size float)) void
(var c (* river-config) (& (. p config)))
(while (>= node 0)
(var n (* river-node) (& [(. p nodes) node]))
(if (> (. n start-size) end-size)
(return))
(= (. n end-size) end-size)
(= (. n start-size) (+ end-size (. c river-growth)))
(= (. n start-size) (fmaxf (. n start-size) (. c min-size)))
(= (. n start-size) (fminf (. n start-size) (. c max-size)))
(= end-size (. n start-size))
(= node (. n parent))))
(fn add-lake ((p (* river-plate)) (river int) (parent int) (angle double)) void
(var c (* river-config) (& (. p config)))
(var pn (* river-node) (& [(. p nodes) parent]))
(= (. pn end-size) (/ (. c min-size) 2))
(var size int (+ (.next-int (& (. p rng)) (- 75 50)) 50))
(var end vec2 (v-add (. pn end) (v-mul (degrees-to-normal angle) 100)))
(var padding double (+ (. c max-valley-width) size))
(if (! (&& (within p (. pn end) padding) (within p end padding)))
(return))
(var start vec2 (. pn end))
(var parent-start-size float (. pn end-size))
(var last-segment int (+ (. pn first-segment) (. c segments-in-river) -1))
(= (. p nodes) (grow-array (. p nodes) (& (. p node-cap)) (. p node-count) (sizeof river-node)))
(= (. p node-next) (realloc (. p node-next) (* (cast (. p node-cap) size-t) (sizeof int))))
(var index int (. p node-count))
(++ (. p node-count))
(var n (* river-node) (& [(. p nodes) index]))
(memset n 0 (sizeof river-node))
(= (. n start) start)
(= (. n end) end)
(= (. n parent) -1)
(= (. n river) river)
(= (. n start-size) parent-start-size)
(= (. n end-size) size)
(= (. n is-end) true)
(= (. n is-lake) true)
(= (. n bounds) (node-envelope p start end))
(= [(. p node-next) index] -1)
(var s int (add-segment p start end index))
(= (. [(. p nodes) index] first-segment) s)
(= (. [(. p nodes) index] segment-count) 1)
(add-child p last-segment s)
(= (. [(. p segments) s] parent-invalid) true)
(= (. [(. p segments) last-segment] parent-invalid) false)
(river-append-node p river index))
(fn river-node-count ((p (* const river-plate)) (river int)) int
(var count int 0)
(for (var n int (. [(. p rivers) river] first-node)) (>= n 0) (= n [(. p node-next) n])
(++ count))
(return count))
;;; RiverSegment.InitializeBounds: the segment, its parent's and
;;; children's midpoints, widened by the valley and the river.
(fn segment-bounds ((p (* river-plate)) (i int)) void
(var s (* river-segment) (& [(. p segments) i]))
(var lo vec2 (v2 (fmin (. s start x) (. s end x)) (fmin (. s start y) (. s end y))))
(var hi vec2 (v2 (fmax (. s start x) (. s end x)) (fmax (. s start y) (. s end y))))
(if (>= (. s parent) 0)
(do
(var m vec2 (. [(. p segments) (. s parent)] mid))
(= lo (v2 (fmin (. lo x) (. m x)) (fmin (. lo y) (. m y))))
(= hi (v2 (fmax (. hi x) (. m x)) (fmax (. hi y) (. m y))))))
(for (var c int (. s first-child)) (>= c 0) (= c (. [(. p segments) c] next-sibling))
(var m vec2 (. [(. p segments) c] mid))
(= lo (v2 (fmin (. lo x) (. m x)) (fmin (. lo y) (. m y))))
(= hi (v2 (fmax (. hi x) (. m x)) (fmax (. hi y) (. m y)))))
(var n (* river-node) (& [(. p nodes) (. s node)]))
(var size float (fmaxf (. n start-size) (. n end-size)))
(var pad double (+ (. p config max-valley-width) size))
(= (. s bounds) #(envelope : (- (. lo x) pad) (- (. lo y) pad) (+ (. hi x) pad) (+ (. hi y) pad))))
(fn grid-cell ((p (* const river-plate)) (v double)) int
(var cell double (/ (cast (plate-size p) double) PLATE-GRID))
(var i int (saturating-int (floor (/ v cell))))
(return (if (< i 0) 0 (if (>= i PLATE-GRID) (- PLATE-GRID 1) i))))
(fn build-grid ((p (* river-plate)) (live (* bool))) void
(var cells int (* PLATE-GRID PLATE-GRID))
(= (. p grid-start) (calloc (+ cells 1) (sizeof int)))
(for (var pass int 0) (< pass 2) (++ pass)
(var fill (* int) (calloc cells (sizeof int)))
(for (var i int 0) (< i (. p segment-count)) (++ i)
(if (! [live i])
(continue))
(var b (* envelope) (& (. [(. p segments) i] bounds)))
(for (var gx int (grid-cell p (. b min-x))) (<= gx (grid-cell p (. b max-x))) (++ gx)
(for (var gz int (grid-cell p (. b min-y))) (<= gz (grid-cell p (. b max-y))) (++ gz)
(var cell int (+ (* gz PLATE-GRID) gx))
(if (== pass 0)
(++ [(. p grid-start) (+ cell 1)])
(do
(= [(. p grid-items) (+ [(. p grid-start) cell] [fill cell])] i)
(++ [fill cell]))))))
(if (== pass 0)
(do
(for (var c int 0) (< c cells) (++ c)
(+= [(. p grid-start) (+ c 1)] [(. p grid-start) c]))
(= (. p grid-items) (malloc (* (+ [(. p grid-start) cells] 1) (sizeof int))))))
(free fill)))
;;; new RiverRegion(plateX, plateZ): the whole plate's rivers.
(pub fn river-plate/make ((config river-config) (oceans (* ocean-layer)) (size-y int)
(px int) (pz int)) (* river-plate)
(var p (* river-plate) (calloc 1 (sizeof river-plate)))
(= (. p config) config)
(= (. p px) px)
(= (. p pz) pz)
(= (. p rng) (lcg/make 0))
(.init-position (& (. p rng)) px pz)
(var n int (. config zones-in-region))
(= (. p ocean) (calloc (* n n) (sizeof bool)))
(= (. p ocean-distance) (calloc (* n n) (sizeof double)))
(for (var x int 0) (< x n) (++ x)
(for (var z int 0) (< z n) (++ z)
(= [(. p ocean) (zone-index p x z)] (zone-is-ocean p oceans size-y x z))))
(ocean-distances p)
;; Seed rivers on coastal ocean zones, x outer and z inner.
(for (var x int 0) (< x n) (++ x)
(for (var z int 0) (< z n) (++ z)
(if (! [(. p ocean) (zone-index p x z)])
(continue))
(var coastal bool false)
(for (var dx int -1) (< dx 2) (++ dx)
(for (var dz int -1) (< dz 2) (++ dz)
(var cx int (if (< (+ x dx) 0) 0 (if (> (+ x dx) (- n 1)) (- n 1) (+ x dx))))
(var cz int (if (< (+ z dz) 0) 0 (if (> (+ z dz) (- n 1)) (- n 1) (+ z dz))))
(if (! [(. p ocean) (zone-index p cx cz)])
(= coastal true))))
(if (! coastal)
(continue))
(if (! (< (.next-float (& (. p rng))) (. config river-spawn-chance)))
(continue))
(var center vec2 (zone-center p x z))
(= (. p rivers) (grow-array (. p rivers) (& (. p river-cap)) (. p river-count) (sizeof river)))
(= (. p river-last) (realloc (. p river-last) (* (cast (. p river-cap) size-t) (sizeof int))))
(var r int (. p river-count))
(= [(. p rivers) r] #(river : center -1 false))
(= [(. p river-last) r] -1)
(++ (. p river-count))
(var target int (find-highest p x z 8))
(var tc vec2 (zone-center p (% target n) (/ target n)))
(var angle double (normal-to-degrees (v-normalized (v-sub tc center))))
(if (generate-river p angle center 0 -1 r (. config downhill-error))
(= (. [(. p rivers) r] alive) true)
(-- (. p river-count)))))
(while (< (. p queue-head) (. p queue-tail))
(var q request [(. p queue) (. p queue-head)])
(++ (. p queue-head))
(generate-river p (. q angle) (. q start) (. q stage) (. q parent) (. q river) (. q error)))
;; Sizes and lakes, then drop rivers with too few nodes.
(for (var r int 0) (< r (. p river-count)) (++ r)
(if (< (river-node-count p r) (. config min-nodes))
(do (= (. [(. p rivers) r] alive) false) (continue)))
(for (var nd int (. [(. p rivers) r] first-node)) (>= nd 0) (= nd [(. p node-next) nd])
(if (. [(. p nodes) nd] is-end)
(assign-size p nd 1)))
(var ends (* int) (malloc (* (+ (river-node-count p r) 1) (sizeof int))))
(var end-count int 0)
(for (var nd int (. [(. p rivers) r] first-node)) (>= nd 0) (= nd [(. p node-next) nd])
(if (. [(. p nodes) nd] is-end)
(do (= [ends end-count] nd) (++ end-count))))
(for (var i int 0) (< i end-count) (++ i)
(if (< (.next-float (& (. p rng))) (. config lake-chance))
(do
(var en (* river-node) (& [(. p nodes) [ends i]]))
(add-lake p r [ends i] (normal-to-degrees (v-normalized (v-sub (. en end) (. en start))))))))
(free ends))
(var live (* bool) (calloc (+ (. p segment-count) 1) (sizeof bool)))
(for (var r int 0) (< r (. p river-count)) (++ r)
(if (! (. [(. p rivers) r] alive))
(continue))
(for (var nd int (. [(. p rivers) r] first-node)) (>= nd 0) (= nd [(. p node-next) nd])
(var node (* river-node) (& [(. p nodes) nd]))
(for (var i int 0) (< i (. node segment-count)) (++ i)
(segment-bounds p (+ (. node first-segment) i))
(= [live (+ (. node first-segment) i)] true))))
(build-grid p live)
(free live)
(return p))
(pub fn river-plate/free ((p (* river-plate))) void
(free (. p ocean))
(free (. p ocean-distance))
(free (. p nodes))
(free (. p node-next))
(free (. p segments))
(free (. p rivers))
(free (. p river-last))
(free (. p queue))
(free (. p grid-start))
(free (. p grid-items))
(free p))
;;; Sampling ------------------------------------------------------------
(pub record river-sample ((distance double) (bank double)))
(pub record river-sampler
((distort-x fast-noise) (distort-z fast-noise) (strength int)
(river-depth double) (base-depth double)))
(pub fn river-sampler/make ((c (* const river-config)) (size-y int)) river-sampler
(var multi double (/ (cast 256 float) size-y))
(var s river-sampler)
(= (. s distort-x) (fast-noise/make 0 (. c river-frequency) (. c river-octaves) (. c river-gain)
(. c river-lacunarity)))
(= (. s distort-z) (fast-noise/make 2 (. c river-frequency) (. c river-octaves) (. c river-gain)
(. c river-lacunarity)))
(= (. s strength) (. c river-distortion-strength))
(= (. s river-depth) (* (. c river-depth) multi))
(= (. s base-depth) (* (. c base-depth) multi))
(return s))
;;; The segments whose sampling bounds touch chunk (cx, cz), which is
;;; the set the mod samples a chunk's columns against. Returns the
;;; count, filling OUT (which needs room for every segment).
(pub fn river-plate/chunk-segments ((p (* const river-plate)) (cx int) (cz int) (out (* int))) int
(var size double (plate-size p))
(var q envelope)
(= (. q min-x) (- (* cx 32.0) (* (. p px) size)))
(= (. q min-y) (- (* cz 32.0) (* (. p pz) size)))
(= (. q max-x) (+ (. q min-x) 32))
(= (. q max-y) (+ (. q min-y) 32))
(var n int 0)
(for (var gx int (grid-cell p (. q min-x))) (<= gx (grid-cell p (. q max-x))) (++ gx)
(for (var gz int (grid-cell p (. q min-y))) (<= gz (grid-cell p (. q max-y))) (++ gz)
(var cell int (+ (* gz PLATE-GRID) gx))
(for (var k int [(. p grid-start) cell]) (< k [(. p grid-start) (+ cell 1)]) (++ k)
(var i int [(. p grid-items) k])
(if (intersects (& (. [(. p segments) i] bounds)) (& q))
(do (= [out n] i) (++ n))))))
(return n))
(fn sample-line ((s (* const river-sampler)) (sample (* river-sample)) (p vec2)
(start vec2) (end vec2) (size float)) void
(var distance double (distance-to-line p start end))
(if (<= distance size)
(do
(= (. sample distance) 0)
(var lerp double (/ (- distance size) (- 0 size)))
(= lerp (sqrt (- 1 (pow (- 1 lerp) 2))))
(var depth double (* (fmax (* (sqrt size) (. s river-depth)) (. s base-depth)) lerp))
(= (. sample bank) (fmax depth (. sample bank)))
(return)))
(-= distance size)
(if (< distance 0) (= distance 0))
(= (. sample distance) (fmin distance (. sample distance))))
(fn sample-connector ((rs (* const river-sampler)) (p (* const river-plate)) (seg int) (conn int)
(proj float) (sample (* river-sample)) (point vec2) (size float)) void
(var s (* const river-segment) (& [(. p segments) seg]))
(var c (* const river-segment) (& [(. p segments) conn]))
(var mid-proj float (projection point (. s mid) (. c mid)))
(var ls vec2)
(var le vec2)
(if (> proj 0.5)
(do
(if (. c parent-invalid) (= mid-proj 0))
(var cn (* const river-node) (& [(. p nodes) (. c node)]))
(var sn (* const river-node) (& [(. p nodes) (. s node)]))
(if (< (. cn start-size) (. sn end-size))
(= size (+ (. cn start-size) (* (- (. cn end-size) (. cn start-size)) (cast 0.5 float)))))
(= ls (v2 (+ (. s mid x) (* (- (. c start x) (. s mid x)) mid-proj))
(+ (. s mid y) (* (- (. c start y) (. s mid y)) mid-proj))))
(= le (v2 (+ (. s end x) (* (- (. c mid x) (. s end x)) mid-proj))
(+ (. s end y) (* (- (. c mid y) (. s end y)) mid-proj)))))
(do
(if (. s parent-invalid) (= mid-proj 0))
(= ls (v2 (+ (. s start x) (* (- (. c mid x) (. s start x)) mid-proj))
(+ (. s start y) (* (- (. c mid y) (. s start y)) mid-proj))))
(= le (v2 (+ (. s mid x) (* (- (. c end x) (. s mid x)) mid-proj))
(+ (. s mid y) (* (- (. c end y) (. s mid y)) mid-proj))))))
(sample-line rs sample point ls le size))
;;; RiverGenerator.SampleRiver at world block (x, z), against the
;;; segments of its chunk.
(pub fn river-sampler/sample ((rs (* const river-sampler)) (p (* const river-plate))
(segments (* const int)) (count int) (x int) (z int)) river-sample
(var size double (plate-size p))
(var lx double (- x (* (. p px) size)))
(var lz double (- z (* (. p pz) size)))
(var dx double (.get (& (. rs distort-x)) lx lz))
(var dz double (.get (& (. rs distort-z)) lx lz))
(var point vec2 (v2 (+ lx (* dx (. rs strength))) (+ lz (* dz (. rs strength)))))
(var sample river-sample #(river-sample : 5000 0))
(for (var k int 0) (< k count) (++ k)
(var seg int [segments k])
(var s (* const river-segment) (& [(. p segments) seg]))
(var n (* const river-node) (& [(. p nodes) (. s node)]))
(var river-proj float (projection point (. n start) (. n end)))
(var size float (+ (. n start-size) (* (- (. n end-size) (. n start-size)) river-proj)))
(var seg-proj float (projection point (. s start) (. s end)))
(if (> seg-proj 0.5)
(if (< (. s first-child) 0)
(sample-line rs (& sample) point (. s start) (. s end) size)
(for (var c int (. s first-child)) (>= c 0) (= c (. [(. p segments) c] next-sibling))
(sample-connector rs p seg c seg-proj (& sample) point size)))
(if (< (. s parent) 0)
(sample-line rs (& sample) point (. s start) (. s end) size)
(sample-connector rs p seg (. s parent) seg-proj (& sample) point size))))
(return sample))
;;; Whether the zone holding plate-local block (x, z) is ocean to
;;; Rivers.
(pub fn river-plate/ocean-at ((p (* const river-plate)) (x int) (z int)) bool
(var size double (plate-size p))
(return [(. p ocean) (zone-at p (- x (* (. p px) size)) (- z (* (. p pz) size)))]))
(pub fn river-plate/chunks ((c (* const river-config))) int
(return (/ (* (. c zone-size) (. c zones-in-region)) 32)))

View File

@@ -12,15 +12,22 @@
;;; generator, and results made under an older one are dropped.
(pub include pthread.h SDL3/SDL.h)
(include stdio.h stdlib.h string.h)
(include stdio.h stdlib.h string.h math.h)
(import json)
(import maps)
(import lcg)
(import noise)
(import shape)
(import worldgen)
(import fastnoise)
(import rivers)
(pub define MAX-TILES 16384)
;;; Not a worldgen map: Rivers' distance to the nearest river.
(pub define LAYER-RIVERS 6)
;;; 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.
(define MAX-WORKERS 32)
(define TILE-FREE 0)
@@ -40,8 +47,8 @@
(slots [tile MAX_TILES]) (index cell-map)
(queue [int MAX_TILES]) (queue-len int) (queue-pos int)
(free-slots [int MAX_TILES]) (free-count int)
(settings world-settings) (assets-dir [char 1024]) (generation int)
(workers [pthread-t 32]) (worker-count int) (quit bool) (frame u64)))
(settings world-settings) (rivers river-config) (assets-dir [char 1024]) (generation int)
(workers [pthread-t 32]) (worker-count int) (quit bool) (frame u64) (waiting int)))
(fn key-hash ((k tile-key)) u64
;; region x and z fit 21 bits each; map and stratum the rest
@@ -108,12 +115,86 @@
(= (* size) inner)
(return px))
;;; What a worker keeps between river tiles, for one generation: the
;;; last few plates it built (a wide view spans several, and its tiles
;;; come in any order), and the sampler.
(define CACHED-PLATES 4)
(record river-cache
((plates [* river-plate 4]) (used [u64 4]) (clock u64) (sampler river-sampler)
(segments (* int)) (segment-cap int)))
(fn release-river-cache ((c (* river-cache))) void
(for (var i int 0) (< i CACHED-PLATES) (++ i)
(if [(. c plates) i] (river-plate/free [(. c plates) i])))
(free (. c segments))
(memset c 0 (sizeof river-cache)))
(fn cached-plate ((c (* river-cache)) (g (* worldgen)) (config (* const river-config))
(px int) (pz int)) (* river-plate)
(++ (. c clock))
(var oldest int 0)
(for (var i int 0) (< i CACHED-PLATES) (++ i)
(var p (* river-plate) [(. c plates) i])
(if (&& p (== (. p px) px) (== (. p pz) pz))
(do (= [(. c used) i] (. c clock)) (return p)))
(if (< [(. c used) i] [(. c used) oldest])
(= oldest i)))
(if [(. c plates) oldest]
(river-plate/free [(. c plates) oldest]))
(var p (* river-plate) (river-plate/make (* config) (& (. g oceans)) (. g settings size-y) px pz))
(= [(. c plates) oldest] p)
(= [(. c used) oldest] (. c clock))
(if (> (+ (. p segment-count) 1) (. c segment-cap))
(do
(= (. c segment-cap) (+ (. p segment-count) 1))
(= (. c segments) (realloc (. c segments) (* (. c segment-cap) (sizeof int))))))
(return p))
(fn river-colour ((distance int) (valley int) (ocean bool)) u32
(if ocean (return 1784427))
(if (== distance 0) (return 3043288))
(if (< distance valley) (return (lerp-colour 8369128 5143098 (/ (* distance 255) valley))))
(return 4022062))
(fn render-rivers ((g (* worldgen)) (cache (* river-cache)) (config (* const river-config))
(k tile-key) (size (* int))) (* u32)
(var res int (. k stratum))
(var step int (/ 512 res))
(var px (* u32) (malloc (* res res (sizeof u32))))
(var per-plate int (river-plate/chunks config))
(var valley int (cast (. config max-valley-width) int))
;; chunk by chunk, so each chunk's segment list is found once
(for (var cz int 0) (< cz 16) (++ cz)
(for (var cx int 0) (< cx 16) (++ cx)
(var chunk-x int (+ (* (. k rx) 16) cx))
(var chunk-z int (+ (* (. k rz) 16) cz))
(var plate-x int (/ chunk-x per-plate))
(var plate-z int (/ chunk-z per-plate))
(var p (* river-plate) (cached-plate cache g config plate-x plate-z))
(var count int (river-plate/chunk-segments p chunk-x chunk-z (. cache segments)))
(for (var dz int 0) (< dz (/ 32 step)) (++ dz)
(for (var dx int 0) (< dx (/ 32 step)) (++ dx)
(var x int (+ (* chunk-x 32) (* dx step) (/ step 2)))
(var z int (+ (* chunk-z 32) (* dz step) (/ step 2)))
(var sample river-sample (river-sampler/sample (& (. cache sampler)) p (. cache segments)
count x z))
(var d int (cast (fmin (. sample distance) 65535) int))
(var ox int (+ (* cx (/ 32 step)) dx))
(var oz int (+ (* cz (/ 32 step)) dz))
(= [px (+ (* oz res) ox)]
(c-bit-or 4278190080 (river-colour d valley (river-plate/ocean-at p x z))))))))
(= (* size) res)
(return px))
;;; Workers -------------------------------------------------------
(fn worker-main ((arg (* void))) (* void)
(var t (* tiles) arg)
(var g (* worldgen) NULL)
(var g-generation int -1)
(var cache river-cache)
(memset (& cache) 0 (sizeof cache))
(pthread-mutex-lock (& (. t lock)))
(while (! (. t quit))
(if (>= (. t queue-pos) (. t queue-len))
@@ -129,15 +210,22 @@
(var key tile-key (. tl key))
(var generation int (. t generation))
(var settings world-settings (. t settings))
(var rivers river-config (. t rivers))
(pthread-mutex-unlock (& (. t lock)))
(if (!= g-generation generation)
(do
(if g (worldgen/free g))
(release-river-cache (& cache))
(= g (worldgen/make settings (. t assets-dir)))
(= (. cache sampler) (river-sampler/make (& rivers) (. settings size-y)))
(= g-generation generation)))
(var size int 0)
(var px (* u32) (if g (render g key (& size)) NULL))
(var px (* u32) NULL)
(if (&& g (== (. key map) LAYER-RIVERS))
(= px (render-rivers g (& cache) (& rivers) key (& size)))
g
(= px (render g key (& size))))
(pthread-mutex-lock (& (. t lock)))
(if (&& px (== generation (. t generation)) (== (. tl state) TILE-WORKING))
@@ -151,10 +239,12 @@
(= (. tl state) TILE-QUEUED)))))
(pthread-mutex-unlock (& (. t lock)))
(if g (worldgen/free g))
(release-river-cache (& cache))
(return NULL))
(ops tiles
(pub fn start ((settings world-settings) (assets-dir (* const char)) (workers int)) (* tiles)
(pub fn start ((settings world-settings) (rivers river-config) (assets-dir (* const char))
(workers int)) (* tiles)
(var t (* tiles) (calloc 1 (sizeof tiles)))
(pthread-mutex-init (& (. t lock)) NULL)
(pthread-cond-init (& (. t wake)) NULL)
@@ -163,6 +253,7 @@
(= [(. t free-slots) i] (- MAX-TILES 1 i)))
(= (. t free-count) MAX-TILES)
(= (. t settings) settings)
(= (. t rivers) rivers)
(snprintf (. t assets-dir) (sizeof (. t assets-dir)) "%s" assets-dir)
(if (> workers MAX-WORKERS) (= workers MAX-WORKERS))
(if (< workers 1) (= workers 1))
@@ -188,9 +279,10 @@
(= (. self free-count) MAX-TILES))
;; New settings: everything cached was made with the old ones.
(pub fn set-settings ((self (* tiles)) (settings world-settings)) void
(pub fn set-settings ((self (* tiles)) (settings world-settings) (rivers river-config)) void
(pthread-mutex-lock (& (. self lock)))
(= (. self settings) settings)
(= (. self rivers) rivers)
(++ (. self generation))
(.clear self)
(pthread-mutex-unlock (& (. self lock))))
@@ -223,7 +315,8 @@
(pthread-mutex-lock (& (. self lock)))
(++ (. self frame))
(= (. self queue-len) 0)
(= (. self queue-pos) 0))
(= (. self queue-pos) 0)
(= (. self waiting) 0))
;; The texture for KEY if it is ready, else NULL and the tile is
;; queued behind those requested earlier this frame.
@@ -251,15 +344,17 @@
(= (. tl state) TILE-DRAWN)))
(if (== (. tl state) TILE-DRAWN)
(return (. tl texture)))
(++ (. self waiting))
(if (&& (== (. tl state) TILE-QUEUED) (< (. self queue-len) MAX-TILES))
(do
(= [(. self queue) (. self queue-len)] slot)
(++ (. self queue-len))))
(return NULL))
;; Hands this frame's queue to the workers. Returns its length.
;; Hands this frame's queue to the workers. Returns how many tiles
;; requested this frame are not ready yet, queued or being made.
(pub fn end-frame ((self (* tiles))) int
(var pending int (. self queue-len))
(var pending int (. self waiting))
(pthread-cond-broadcast (& (. self wake)))
(pthread-mutex-unlock (& (. self lock)))
(return pending))

View File

@@ -2,7 +2,7 @@
;;; region over everything the save has. A port is right when every
;;; map matches to the pixel, padding included.
(include stdio.h stdlib.h string.h)
(include stdio.h stdlib.h string.h time.h)
(import maps)
(import lcg)
(import noise)
@@ -10,6 +10,8 @@
(import shape)
(import save)
(import worldgen)
(import fastnoise)
(import rivers)
(import img)
(define MAP-KINDS 6)
@@ -108,3 +110,66 @@
(.write (& [diffs k]) path)
(.release (& [diffs k])))
(return failed))
;;; Recomputes Rivers' per-column riverDistance for every map chunk the
;;; save stored and compares it with the moddata Rivers saved: exactly
;;; where it saved some, and "nothing within 300 blocks" where it saved
;;; none.
(pub fn verify-rivers ((s (* save)) (g (* worldgen)) (config river-config) (dir (* const char))) int
(var e extent (save/extent s "mapchunk"))
(var w int (* (+ (- (. e x1) (. e x0)) 1) 32))
(var h int (* (+ (- (. e z1) (. e z0)) 1) 32))
(var diff image (image/make w h))
(var sampler river-sampler (river-sampler/make (& config) (. g settings size-y)))
(var plate (* river-plate) NULL)
(var chunks-per-plate int (/ (* (. config zone-size) (. config zones-in-region)) 32))
(var segments (* int) NULL)
(var stored-columns i64 0)
(var stored-matched i64 0)
(var unstored-chunks int 0)
(var unstored-wrong int 0)
(var m chunk-maps)
(for (var cz int (. e z0)) (<= cz (. e z1)) (++ cz)
(for (var cx int (. e x0)) (<= cx (. e x1)) (++ cx)
(if (! (save/chunk s cx cz (& m)))
(continue))
(var px int (/ cx chunks-per-plate))
(var pz int (/ cz chunks-per-plate))
(if (|| (! plate) (!= (. plate px) px) (!= (. plate pz) pz))
(do
(if plate (river-plate/free plate))
(var t0 clock-t (clock))
(= plate (river-plate/make config (& (. g oceans)) (. g settings size-y) px pz))
(printf "river plate %d,%d: %d nodes, %d segments, %.2f s\n" px pz (. plate node-count)
(. plate segment-count) (/ (cast (- (clock) t0) double) CLOCKS-PER-SEC))
(= segments (realloc segments (* (+ (. plate segment-count) 1) (sizeof int))))))
(var count int (river-plate/chunk-segments plate cx cz segments))
(var in-range bool false)
(for (var i int 0) (< i 1024) (++ i)
(var bx int (+ (* cx 32) (% i 32)))
(var bz int (+ (* cz 32) (/ i 32)))
(var sample river-sample (river-sampler/sample (& sampler) plate segments count bx bz))
(var d int (cast (cast (. sample distance) u16) int))
(if (<= d 300) (= in-range true))
(var ok bool true)
(if (. m has-river-distance)
(do
(++ stored-columns)
(= ok (== d [(. m river-distance) i]))
(if ok (++ stored-matched))))
(var shade int (if (> d 255) 0 (- 255 d)))
(.set (& diff) (+ (* (- cx (. e x0)) 32) (% i 32)) (+ (* (- cz (. e z0)) 32) (/ i 32))
(if ok (grey shade) 16719904)))
(if (! (. m has-river-distance))
(do
(++ unstored-chunks)
(if in-range (++ unstored-wrong))))))
(printf "rivers %lld/%lld columns with stored riverDistance match; %d/%d chunks without it rightly have no river within 300\n"
stored-matched stored-columns (- unstored-chunks unstored-wrong) unstored-chunks)
(var path [char 1024])
(snprintf path (sizeof path) "%s/diff-rivers.png" dir)
(.write (& diff) path)
(.release (& diff))
(if plate (river-plate/free plate))
(free segments)
(return (if (&& (== stored-matched stored-columns) (== unstored-wrong 0)) 0 1)))

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))

View File

@@ -13,6 +13,8 @@
(import save)
(import dump)
(import worldgen)
(import fastnoise)
(import rivers)
(import settings)
(import verify)
@@ -86,7 +88,8 @@
(if (! g)
(return 1))
(var s (* save) (save/open save-path))
(var failed int (+ (verify-stories s g) (verify-regions s g out)))
(var failed int (+ (verify-stories s g) (verify-regions s g out)
(verify-rivers s g (river-config/defaults) out)))
(save/close s)
(return failed))

View File

@@ -455,7 +455,24 @@
(if (&& (== v 255) forced-land (.has (& (. self require-land)) nx nz))
(= v 0))
(= [out (+ (* z size-x) x)] v)))
(box-blur out size-x size-z 5)))
(box-blur out size-x size-z 5))
;; One pixel of the blurred map, without forced land: what Rivers
;; samples per zone. Away from a window's edge every 5 x 5 box is
;; full, so this matches a pixel of `gen' for any window holding it.
(pub fn blurred-at ((self (* ocean-layer)) (px int) (pz int)) int
(var sum int 0)
(for (var dz int -2) (<= dz 2) (++ dz)
(var row int 0)
(for (var dx int -2) (<= dx 2) (++ dx)
(var nx int (+ px dx))
(var nz int (+ pz dz))
(var ox int (cast (* (. self wobble) (.noise-2d (. self wobble-x) nx nz)) int))
(var oz int (cast (* (. self wobble) (.noise-2d (. self wobble-z) nx nz)) int))
(+= row (.index-at self (+ nx ox) (+ nz oz))))
;; values are 0 or 255, only in the low byte
(+= sum (/ row 5)))
(return (/ sum 5))))
;;; GenMaps.ForceRandomLandArea: a natural blob of forced land covering
;;; RADIUS blocks around (x, z), in ocean map pixels.