show the generated region maps in the viewer

Tiles are single maps of single regions, generated on worker threads
that each own a worldgen, since the layers keep RNG state. Only the
visible tiles are queued, nearest first, so the viewer never computes
more than the window shows and sleeps once it has drawn it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-08 18:24:59 +03:00
parent 63fd30a189
commit 5ad62524f1
10 changed files with 939 additions and 116 deletions

View File

@@ -61,7 +61,19 @@
(== (strcmp key "startingClimate") 0)
(snprintf (. self starting-climate) 16 "%s" value)
(== (strcmp key "storyStructuresDistScaling") 0)
(= (. self story-dist-scaling) (strtof value NULL)))))
(= (. self story-dist-scaling) (strtof value NULL))
(== (strcmp key "worldWidth") 0) (= (. self size-x) (atoi value))
(== (strcmp key "worldLength") 0) (= (. self size-z) (atoi value))
(== (strcmp key "worldHeight") 0) (= (. self size-y) (atoi value)))))
;;; "#79E02E" -> 0x79E02E
(pub fn hex-colour ((o (* cJSON))) u32
(var hex (* const char) (cJSON-GetStringValue (cJSON-GetObjectItem o "hexcolor")))
(if (! hex)
(return 8421504))
(if (== [hex 0] #\#)
(++ hex))
(return (cast (strtoul hex NULL 16) u32)))
;;; Geologic provinces ----------------------------------------------
@@ -70,6 +82,7 @@
;;; wobble so the cells don't show.
(pub record province-layer
((rng lcg) (weights [int MAX_PROVINCES]) (count int) (weight-sum int)
(codes [* char MAX_PROVINCES]) (colours [u32 MAX_PROVINCES])
(wobble-x (* simplex-noise)) (wobble-z (* simplex-noise))))
(define GEOPROV-MAP-SCALE 64)
@@ -83,6 +96,9 @@
(for (var v (* cJSON) (. variants child)) (&& v (< (. p count) MAX-PROVINCES)) (= v (. v next))
(var w int (cast (cJSON-GetNumberValue (cJSON-GetObjectItem v "weight")) int))
(= [(. p weights) (. p count)] w)
(= [(. p codes) (. p count)]
(strdup (cJSON-GetStringValue (cJSON-GetObjectItem v "code"))))
(= [(. p colours) (. p count)] (hex-colour v))
(+= (. p weight-sum) w)
(++ (. p count)))
(var wscale float (* 1.5 GEOPROV-MAP-SCALE))
@@ -176,7 +192,7 @@
;;; Climate.GetScaledAdjustedTemperature at sea level: the packed byte
;;; back to degrees.
(fn scaled-temperature ((unscaled int)) int
(pub fn scaled-temperature ((unscaled int)) int
(var t float (/ (- unscaled (/ (cast 0 float) (cast 1.5 float))) temperature-scale-conversion))
(return (clamp-int (- (cast t int) 20) -20 40)))
@@ -259,7 +275,8 @@
;;; variants as listed, then every variant's mutations in turn.
(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)))
(min-rain int) (max-rain int) (chance float) (first-mutation int) (mutation-count int)
(colour u32)))
(fn json-number ((o (* cJSON)) (key (* const char)) (fallback double)) double
(var v (* cJSON) (cJSON-GetObjectItem o key))
@@ -277,6 +294,7 @@
(= (. l min-rain) (cast (json-number o "minRain" 0) int))
(= (. l max-rain) (cast (json-number o "maxRain" 255) int))
(= (. l chance) (cast (json-number o "chance" 0) float))
(= (. l colour) (hex-colour o))
(= (. l first-mutation) -1))
(pub record landform-table ((all [landform MAX_LANDFORMS]) (count int) (variant-count int)))
@@ -569,7 +587,8 @@
;;; MapLayerCustomPerlin per stratum of rockstrata.json: how thick
;;; that stratum may be, in blocks, per 16 blocks of region.
(pub record strata-layer
((count int) (noises [* simplex-sum MAX_STRATA]) (thresholds [* double MAX_STRATA])))
((count int) (noises [* simplex-sum MAX_STRATA]) (thresholds [* double MAX_STRATA])
(codes [* char MAX_STRATA])))
(fn json-doubles ((o (* cJSON)) (key (* const char)) (out (* double)) (max int)) int
(var arr (* cJSON) (cJSON-GetObjectItem o key))
@@ -598,6 +617,8 @@
(var seed i64 (+ (cast (+ world-seed 23423 (. l count)) i64) 12321))
(= [(. l noises) (. l count)] (simplex-sum/make amplitudes frequencies n seed))
(= [(. l thresholds) (. l count)] th)
(= [(. l codes) (. l count)]
(strdup (cJSON-GetStringValue (cJSON-GetObjectItem v "blockcode"))))
(++ (. l count)))
(return l))
@@ -876,52 +897,82 @@
(= (. m data) (malloc (* (. m size) (. m size) (sizeof i32)))))
;;; GenMaps.OnMapRegionGen, for the maps generated so far.
(pub fn worldgen/region ((g (* worldgen)) (rx int) (rz int) (out (* region-maps))) void
(memset out 0 (sizeof region-maps))
(var n int (/ REGION-SIZE GEOPROV-MAP-SCALE))
(var pad int 3)
(alloc-map (& (. out geoprov)) n pad)
(.gen (& (. g provinces)) (- (* rx n) pad) (- (* rz n) pad) (+ n (* 2 pad)) (+ n (* 2 pad))
(. out geoprov data))
;;; The maps of a region, by kind. Their sizes and paddings are
;;; GenMaps.OnMapRegionGen's.
(pub define MAP-LANDFORM 0)
(pub define MAP-CLIMATE 1)
(pub define MAP-PROVINCE 2)
(pub define MAP-UPHEAVAL 3)
(pub define MAP-OCEAN 4)
(pub define MAP-STRATA 5)
(= n (/ REGION-SIZE LANDFORM-MAP-SCALE))
(= pad 4)
(alloc-map (& (. out landform)) n pad)
(.gen (& (. g landforms)) (- (* rx n) pad) (- (* rz n) pad) (+ n (* 2 pad)) (+ n (* 2 pad))
(. out landform data))
(= n (/ REGION-SIZE OCEAN-MAP-SCALE))
(= pad 5)
(alloc-map (& (. out ocean)) n pad)
(.gen (& (. g oceans)) (- (* rx n) pad) (- (* rz n) pad) (+ n (* 2 pad)) (+ n (* 2 pad)) true
(. out ocean data))
(= n (/ REGION-SIZE 32))
(= pad 2)
(alloc-map (& (. out climate)) n pad)
(.gen (& (. g climate)) (- (* rx n) pad) (- (* rz n) pad) (+ n (* 2 pad)) (+ n (* 2 pad))
(. out climate data))
(= pad 3)
(alloc-map (& (. out upheaval)) n pad)
(.gen (& (. g upheaval)) (- (* rx n) pad) (- (* rz n) pad) (+ n (* 2 pad)) (+ n (* 2 pad))
(. out upheaval data))
(= n (/ REGION-SIZE 16))
(= pad 2)
(= (. out strata-count) (. g strata count))
(for (var i int 0) (< i (. g strata count)) (++ i)
(alloc-map (& [(. out strata) i]) n pad)
(.gen (& (. g strata)) i (- (* rx n) pad) (- (* rz n) pad) (+ n (* 2 pad)) (+ n (* 2 pad))
(. [(. out strata) i] data)))
;; GenMaps' forcing, in its order: per story landform then upheaval,
;; then climate for every story that forces one.
;;; One map of region (rx, rz), with whatever story structures force
;;; on it. STRATUM picks the rock stratum for MAP-STRATA.
(pub fn worldgen/map ((g (* worldgen)) (kind int) (stratum int) (rx int) (rz int)
(out (* int-map))) void
(var n int 0)
(var pad int 0)
(switch kind
(case MAP-PROVINCE (do (= n (/ REGION-SIZE GEOPROV-MAP-SCALE)) (= pad 3)) (break))
(case MAP-LANDFORM (do (= n (/ REGION-SIZE LANDFORM-MAP-SCALE)) (= pad 4)) (break))
(case MAP-OCEAN (do (= n (/ REGION-SIZE OCEAN-MAP-SCALE)) (= pad 5)) (break))
(case MAP-CLIMATE (do (= n (/ REGION-SIZE 32)) (= pad 2)) (break))
(case MAP-UPHEAVAL (do (= n (/ REGION-SIZE 32)) (= pad 3)) (break))
(default (do (= n (/ REGION-SIZE 16)) (= pad 2))))
(alloc-map out n pad)
(var x0 int (- (* rx n) pad))
(var z0 int (- (* rz n) pad))
(var size int (+ n (* 2 pad)))
(switch kind
(case MAP-PROVINCE (.gen (& (. g provinces)) x0 z0 size size (. out data)) (break))
(case MAP-LANDFORM (.gen (& (. g landforms)) x0 z0 size size (. out data)) (break))
(case MAP-OCEAN (.gen (& (. g oceans)) x0 z0 size size true (. out data)) (break))
(case MAP-CLIMATE (.gen (& (. g climate)) x0 z0 size size (. out data)) (break))
(case MAP-UPHEAVAL (.gen (& (. g upheaval)) x0 z0 size size (. out data)) (break))
(default (.gen (& (. g strata)) stratum x0 z0 size size (. out data))))
;; GenMaps' forcing. Within one region it runs per story, landform
;; then upheaval, then climate; the maps don't depend on each other,
;; so per map the story order is all that is left of it.
(for (var i int 0) (< i (. g story-count)) (++ i)
(var st (* story) (& [(. g stories) i]))
(if (>= (. st landform) 0)
(do
(force-landform (& (. out landform)) rx rz st (. g story-wobble-x) (. g story-wobble-z))
(force-no-upheaval (& (. out upheaval)) rx rz st))))
(for (var i int 0) (< i (. g story-count)) (++ i)
(if (. [(. g stories) i] forces-climate)
(force-climate (& (. out climate)) rx rz (. out landform pad-tl) (& [(. g stories) i])))))
(if (&& (== kind MAP-LANDFORM) (>= (. st landform) 0))
(force-landform out rx rz st (. g story-wobble-x) (. g story-wobble-z))
(&& (== kind MAP-UPHEAVAL) (>= (. st landform) 0))
(force-no-upheaval out rx rz st)
(&& (== kind MAP-CLIMATE) (. st forces-climate))
;; The landform map's padding: GenMaps passes the variable it
;; last set.
(force-climate out rx rz 4 st))))
;;; GenMaps.OnMapRegionGen: every map of the region.
(pub fn worldgen/region ((g (* worldgen)) (rx int) (rz int) (out (* region-maps))) void
(memset out 0 (sizeof region-maps))
(worldgen/map g MAP-PROVINCE 0 rx rz (& (. out geoprov)))
(worldgen/map g MAP-LANDFORM 0 rx rz (& (. out landform)))
(worldgen/map g MAP-OCEAN 0 rx rz (& (. out ocean)))
(worldgen/map g MAP-CLIMATE 0 rx rz (& (. out climate)))
(worldgen/map g MAP-UPHEAVAL 0 rx rz (& (. out upheaval)))
(= (. out strata-count) (. g strata count))
(for (var i int 0) (< i (. g strata count)) (++ i)
(worldgen/map g MAP-STRATA i rx rz (& [(. out strata) i]))))
(pub fn worldgen/free ((g (* worldgen))) void
(free (. g provinces wobble-x))
(free (. g provinces wobble-z))
(for (var i int 0) (< i (. g provinces count)) (++ i)
(free [(. g provinces codes) i]))
(free (. g landforms wobble-x))
(free (. g landforms wobble-z))
(free (. g oceans wobble-x))
(free (. g oceans wobble-z))
(.release (& (. g oceans require-land)))
(free (. g upheaval noise))
(free (. g climate wobble-x))
(free (. g climate wobble-z))
(for (var i int 0) (< i (. g strata count)) (++ i)
(free [(. g strata noises) i])
(free [(. g strata thresholds) i])
(free [(. g strata codes) i]))
(free (. g story-wobble-x))
(free (. g story-wobble-z))
(free g))