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

@@ -32,13 +32,15 @@ $(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 verify cJSON stb
VSMV_OBJ = vsmv cs pb json maps save img dump lcg noise shape worldgen settings verify cJSON stb
$(B)/vsmv: $(VSMV_OBJ:%=$(B)/%.o)
$(CC) $^ -o $@ -lsqlite3
$(B)/vs-map-viewer: $(B)/viewer.o $(B)/nk.o
$(CC) $^ -o $@ $(SDL_LIBS)
VIEWER_OBJ = viewer tiles settings worldgen shape noise lcg maps save pb json cs nk cJSON stb
$(B)/vs-map-viewer: $(VIEWER_OBJ:%=$(B)/%.o)
$(CC) $^ -o $@ $(SDL_LIBS) -lsqlite3
$(B):
mkdir -p $@

View File

@@ -343,19 +343,34 @@ Not ported yet: the =patchy= world climate (=worldClimate= other than
=realistic=), and the forest, shrub, flower and beach maps, which
nothing here uses.
** 3. Minimal viewer
- Nuklear panels, a pannable and zoomable tile map, and a
layer switcher for the region maps.
- Settings panel:
- seed, world size, worldHeight;
- landcover, oceanscale, landformScale, upheavelCommonness,
geologicActivity;
- worldClimate, polarEquatorDistance, globalTemperature/Precipitation,
startingClimate.
- River config section, with load from =rivers.json=.
- Load settings from =serverconfig.json= or a =.vcdbs=.
- Hover readout: coordinates, province, landform, climate.
- Tiles exist only for the view.
** 3. Minimal viewer (done)
=vs-map-viewer [--assets DIR] [--zoom Z] [--layer N] [--screenshot F]
[WORLD]=, where WORLD is a =.vcdbs= or a =serverconfig.json=.
- *Map.* Pan by dragging, zoom with the wheel around the cursor, from
1/16 to 32 blocks per point. At 32, the window shows the full
25,600-block length of the test world.
- *Layers.* Landform, climate, province, upheaval, ocean, and rock
strata thickness with a stratum picker. Spawn and the story
locations are marked.
- *World panel.* Every setting worldgen reads, with Apply, and Load
for a save or a server config.
- *Info panel.* Block and region under the cursor, with landform,
province, climate, ocean and upheaval there.
- *Tiles* (=tiles.sex=). A tile is one region of one map. They are
generated by =cores - 1= worker threads, each with its own worldgen.
The queue is rebuilt every frame from the visible tiles, nearest the
centre first. Up to 16,384 tiles are cached and evicted least
recently drawn first.
- *Idle.* With nothing pending, the loop sleeps on events (about 2%
CPU).
- *Speed.* A full window at maximum zoom-out renders in 1.1 s for
landforms and under 0.5 s for the other layers.
- *Screenshots.* =--screenshot= saves the window once no tile is
pending, and quits. That is how the viewer gets checked without
looking at it.
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=:

View File

@@ -3,6 +3,19 @@
Language bugs and friction met while building vs-map-viewer, against
sexc at ~/dev/sex. Newest first. Unless said otherwise, sexc dfafe8b.
* 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
=pa->distance= on a =void *=. Like the conditional entry below, the
writer doesn't parenthesize a lower-precedence operand. Bound to a
typed local first, it works.
* Arrays of a header type with a symbolic size misparse
=(workers [pthread-t MAX_WORKERS])= emits =pthread_t MAX_WORKERS
workers[];=, reading =MAX_WORKERS= as a second type word. =[u16
CHUNK_AREA]= works, presumably because =u16= is known to Sex and
=pthread_t= only to C. A literal size, =[pthread-t 32]=, works.
* A conditional expression as an operand loses its parentheses
#+begin_src scheme
(/ (* 100.0 matched) (if (> compared 0) compared 1))

View File

@@ -1,3 +1,10 @@
#include <stdio.h>
#define NK_IMPLEMENTATION
#define NK_SDL3_RENDERER_IMPLEMENTATION
#include "nk.h"
char *vsmv_dtoa(char *s, double n)
{
snprintf(s, NK_MAX_NUMBER_BUFFER, "%g", n);
return s;
}

View File

@@ -17,6 +17,10 @@
#define NK_INCLUDE_COMMAND_USERDATA
#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
/* Nuklear's own dtoa truncates, so a property holding 0.7f reads 0.69. */
char *vsmv_dtoa(char *s, double n);
#define NK_DTOA vsmv_dtoa
#include "nuklear.h"
#include "nuklear_sdl3_renderer.h"

72
src/settings.sex Normal file
View File

@@ -0,0 +1,72 @@
;;; Where world settings come from: a save, which knows the seed as a
;;; number and the world size as generated, or a server config, which
;;; has the seed as typed and the world config as strings.
(include stdio.h stdlib.h string.h)
(import cs)
(import json)
(import maps)
(import lcg)
(import noise)
(import shape)
(import save)
(import worldgen)
;;; Settings plus the seed as the user would type it.
(pub record world-source ((settings world-settings) (seed-text [char 128])))
(fn ends-with ((s (* const char)) (suffix (* const char))) bool
(var n size-t (strlen s))
(var m size-t (strlen suffix))
(return (&& (>= n m) (== (strcmp (+ s (- n m)) suffix) 0))))
(fn from-save ((path (* const char)) (out (* world-source))) bool
(var s (* save) (save/open path))
(if (! s)
(return false))
(for (var i int 0) (< i (. s config-count)) (++ i)
(.set (& (. out settings)) [(. s config-keys) i] [(. s config-values) i]))
;; The sizes the world was generated with, over what the config says.
(= (. out settings seed) (. s seed))
(= (. out settings size-x) (. s size-x))
(= (. out settings size-y) (. s size-y))
(= (. out settings size-z) (. s size-z))
(snprintf (. out seed-text) (sizeof (. out seed-text)) "%d" (. s seed))
(save/close s)
(return true))
;;; serverconfig.json: WorldConfig.Seed and WorldConfig.WorldConfiguration.
(fn from-server-config ((path (* const char)) (out (* world-source))) bool
(var json (* cJSON) (json-load path))
(if (! json)
(return false))
(var world (* cJSON) (cJSON-GetObjectItem json "WorldConfig"))
(var seed (* const char) (cJSON-GetStringValue (cJSON-GetObjectItem world "Seed")))
(if (! seed)
(do
(fprintf stderr "%s: no WorldConfig.Seed\n" path)
(cJSON-Delete json)
(return false)))
(snprintf (. out seed-text) (sizeof (. out seed-text)) "%s" seed)
(= (. out settings seed) (world-seed seed))
(var map-size-y (* cJSON) (cJSON-GetObjectItem world "MapSizeY"))
(if (cJSON-IsNumber map-size-y)
(= (. out settings size-y) (cast (cJSON-GetNumberValue map-size-y) int)))
(var config (* cJSON) (cJSON-GetObjectItem world "WorldConfiguration"))
(for (var item (* cJSON) (if config (. config child) NULL)) item (= item (. item next))
(var text [char 64])
(if (cJSON-IsString item) (snprintf text 64 "%s" (cJSON-GetStringValue item))
(cJSON-IsNumber item) (snprintf text 64 "%.17g" (cJSON-GetNumberValue item))
(cJSON-IsBool item) (snprintf text 64 "%s" (if (cJSON-IsTrue item) "true" "false"))
(continue))
(.set (& (. out settings)) (. item string) text))
(cJSON-Delete json)
(return true))
;;; A .vcdbs save or a serverconfig.json, by extension.
(pub fn world-source/load ((path (* const char)) (out (* world-source))) bool
(= (. out settings) (world-settings/defaults))
(snprintf (. out seed-text) (sizeof (. out seed-text)) "0")
(if (ends-with path ".vcdbs")
(return (from-save path out)))
(return (from-server-config path out)))

278
src/tiles.sex Normal file
View File

@@ -0,0 +1,278 @@
;;; Map tiles for the viewer: one map region of one map, coloured,
;;; generated by a pool of worker threads and drawn as SDL textures.
;;;
;;; Only what the view asks for is generated. Each frame the viewer
;;; says which tiles it can see, nearest the centre first; that list
;;; replaces the work queue, so panning away drops work nobody needs.
;;; The cache keeps recently drawn tiles and evicts the least recently
;;; drawn when full.
;;;
;;; Every worker owns its own worldgen, because the layers keep RNG
;;; state. A settings change bumps `generation'; workers rebuild their
;;; generator, and results made under an older one are dropped.
(pub include pthread.h SDL3/SDL.h)
(include stdio.h stdlib.h string.h)
(import json)
(import maps)
(import lcg)
(import noise)
(import shape)
(import worldgen)
(pub define MAX-TILES 16384)
(define MAX-WORKERS 32)
(define TILE-FREE 0)
(define TILE-QUEUED 1)
(define TILE-WORKING 2)
(define TILE-READY 3)
(define TILE-DRAWN 4)
(pub record tile-key ((map int) (stratum int) (rx int) (rz int)))
(pub record tile
((key tile-key) (state int) (generation int) (size int) (pixels (* u32))
(texture (* SDL-Texture)) (last-used u64)))
(pub record tiles
((lock pthread-mutex-t) (wake pthread-cond-t)
(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)))
(fn key-hash ((k tile-key)) u64
;; region x and z fit 21 bits each; map and stratum the rest
(return (c-bit-or (cast (& (. k rx) 2097151) u64)
(<< (cast (& (. k rz) 2097151) u64) 21)
(<< (cast (+ (* (. k map) 64) (. k stratum)) u64) 42))))
;;; The tile index is a cell-map keyed by (x, z): the hash is split
;;; over the two halves.
(fn index-get ((t (* tiles)) (k tile-key)) int
(var h u64 (key-hash k))
(return (.get (& (. t index)) (cast (>> h 32) int) (cast (& h 4294967295) int))))
(fn index-put ((t (* tiles)) (k tile-key) (slot int)) void
(var h u64 (key-hash k))
(.put (& (. t index)) (cast (>> h 32) int) (cast (& h 4294967295) int) slot))
(fn index-remove ((t (* tiles)) (k tile-key)) void
(var h u64 (key-hash k))
(.remove (& (. t index)) (cast (>> h 32) int) (cast (& h 4294967295) int)))
;;; Colouring -----------------------------------------------------
;;; A distinct, stable colour per landform index.
(fn landform-colour ((i u32)) u32
(var h u32 (* (+ i 1) (cast 2654435761 u32)))
(return (c-bit-or (<< (+ 48 (& (>> h 24) 191)) 16)
(<< (+ 48 (& (>> h 16) 191)) 8)
(+ 48 (& (>> h 8) 191)))))
(fn lerp-colour ((a u32) (b u32) (t int)) u32
(var r u32 0)
(for (var shift int 0) (< shift 24) (+= shift 8)
(var ca int (& (>> a shift) 255))
(var cb int (& (>> b shift) 255))
(c-bit-or= r (<< (cast (+ ca (/ (* (- cb ca) t) 255)) u32) shift)))
(return r))
(fn colour-of ((g (* const worldgen)) (map int) (v int)) u32
(switch map
;; landforms.json gives most landforms the same green, so these
;; are coloured by index instead
(case MAP-LANDFORM (return (landform-colour (cast v u32))))
(case MAP-PROVINCE
(return (if (&& (>= v 0) (< v (. g provinces count))) [(. g provinces colours) v] 0)))
(case MAP-CLIMATE (return (& (cast v u32) 16777215)))
(case MAP-OCEAN (return (lerp-colour 6326847 1919884 v)))
(default
(if (< v 0) (= v 0))
(if (> v 255) (= v 255))
(return (* (cast v u32) 65793)))))
;;; The tile's inner pixels, coloured, as ARGB.
(fn render ((g (* worldgen)) (k tile-key) (size (* int))) (* u32)
(var m int-map)
(worldgen/map g (. k map) (. k stratum) (. k rx) (. k rz) (& m))
(var inner int (- (. m size) (. m pad-tl) (. m pad-br)))
(var px (* u32) (malloc (* inner inner (sizeof u32))))
(for (var z int 0) (< z inner) (++ z)
(for (var x int 0) (< x inner) (++ x)
(var v int [(. m data) (+ (* (+ z (. m pad-tl)) (. m size)) x (. m pad-tl))])
(= [px (+ (* z inner) x)] (c-bit-or 4278190080 (colour-of g (. k map) v)))))
(free (. m data))
(= (* size) inner)
(return px))
;;; Workers -------------------------------------------------------
(fn worker-main ((arg (* void))) (* void)
(var t (* tiles) arg)
(var g (* worldgen) NULL)
(var g-generation int -1)
(pthread-mutex-lock (& (. t lock)))
(while (! (. t quit))
(if (>= (. t queue-pos) (. t queue-len))
(do
(pthread-cond-wait (& (. t wake)) (& (. t lock)))
(continue)))
(var slot int [(. t queue) (. t queue-pos)])
(++ (. t queue-pos))
(var tl (* tile) (& [(. t slots) slot]))
(if (!= (. tl state) TILE-QUEUED)
(continue))
(= (. tl state) TILE-WORKING)
(var key tile-key (. tl key))
(var generation int (. t generation))
(var settings world-settings (. t settings))
(pthread-mutex-unlock (& (. t lock)))
(if (!= g-generation generation)
(do
(if g (worldgen/free g))
(= g (worldgen/make settings (. t assets-dir)))
(= g-generation generation)))
(var size int 0)
(var px (* u32) (if g (render g key (& size)) NULL))
(pthread-mutex-lock (& (. t lock)))
(if (&& px (== generation (. t generation)) (== (. tl state) TILE-WORKING))
(do
(= (. tl pixels) px)
(= (. tl size) size)
(= (. tl state) TILE-READY))
(do
(free px)
(if (&& (== generation (. t generation)) (== (. tl state) TILE-WORKING))
(= (. tl state) TILE-QUEUED)))))
(pthread-mutex-unlock (& (. t lock)))
(if g (worldgen/free g))
(return NULL))
(ops tiles
(pub fn start ((settings world-settings) (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)
(= (. t index) (cell-map/make))
(for (var i int 0) (< i MAX-TILES) (++ i)
(= [(. t free-slots) i] (- MAX-TILES 1 i)))
(= (. t free-count) MAX-TILES)
(= (. t settings) settings)
(snprintf (. t assets-dir) (sizeof (. t assets-dir)) "%s" assets-dir)
(if (> workers MAX-WORKERS) (= workers MAX-WORKERS))
(if (< workers 1) (= workers 1))
(= (. t worker-count) workers)
(for (var i int 0) (< i workers) (++ i)
(pthread-create (& [(. t workers) i]) NULL worker-main t))
(return t))
;; Empties the cache. Call with the lock held, from the main thread.
(fn clear ((self (* tiles))) void
(for (var i int 0) (< i MAX-TILES) (++ i)
(var tl (* tile) (& [(. self slots) i]))
(if (. tl texture)
(SDL-DestroyTexture (. tl texture)))
(free (. tl pixels))
(memset tl 0 (sizeof tile)))
(.release (& (. self index)))
(= (. self index) (cell-map/make))
(= (. self queue-len) 0)
(= (. self queue-pos) 0)
(for (var i int 0) (< i MAX-TILES) (++ i)
(= [(. self free-slots) i] (- MAX-TILES 1 i)))
(= (. 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
(pthread-mutex-lock (& (. self lock)))
(= (. self settings) settings)
(++ (. self generation))
(.clear self)
(pthread-mutex-unlock (& (. self lock))))
;; A free slot, or the least recently drawn finished one.
(fn take-slot ((self (* tiles))) int
(if (> (. self free-count) 0)
(do
(-- (. self free-count))
(return [(. self free-slots) (. self free-count)])))
(var best int -1)
(var best-used u64 UINT64_MAX)
(for (var i int 0) (< i MAX-TILES) (++ i)
(var tl (* tile) (& [(. self slots) i]))
(if (&& (|| (== (. tl state) TILE-READY) (== (. tl state) TILE-DRAWN)
(== (. tl state) TILE-QUEUED))
(< (. tl last-used) best-used) (< (. tl last-used) (. self frame)))
(do (= best i) (= best-used (. tl last-used)))))
(if (< best 0)
(return -1))
(var tl (* tile) (& [(. self slots) best]))
(index-remove self (. tl key))
(if (. tl texture)
(SDL-DestroyTexture (. tl texture)))
(free (. tl pixels))
(memset tl 0 (sizeof tile))
(return best))
(pub fn begin-frame ((self (* tiles))) void
(pthread-mutex-lock (& (. self lock)))
(++ (. self frame))
(= (. self queue-len) 0)
(= (. self queue-pos) 0))
;; The texture for KEY if it is ready, else NULL and the tile is
;; queued behind those requested earlier this frame.
(pub fn request ((self (* tiles)) (renderer (* SDL-Renderer)) (k tile-key)) (* SDL-Texture)
(var slot int (index-get self k))
(if (< slot 0)
(do
(= slot (.take-slot self))
(if (< slot 0)
(return NULL))
(var tl (* tile) (& [(. self slots) slot]))
(= (. tl key) k)
(= (. tl state) TILE-QUEUED)
(index-put self k slot)))
(var tl (* tile) (& [(. self slots) slot]))
(= (. tl last-used) (. self frame))
(if (== (. tl state) TILE-READY)
(do
(= (. tl texture) (SDL-CreateTexture renderer SDL-PIXELFORMAT-ARGB8888
SDL-TEXTUREACCESS-STATIC (. tl size) (. tl size)))
(SDL-SetTextureScaleMode (. tl texture) SDL-SCALEMODE-NEAREST)
(SDL-UpdateTexture (. tl texture) NULL (. tl pixels) (* (. tl size) 4))
(free (. tl pixels))
(= (. tl pixels) NULL)
(= (. tl state) TILE-DRAWN)))
(if (== (. tl state) TILE-DRAWN)
(return (. tl texture)))
(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.
(pub fn end-frame ((self (* tiles))) int
(var pending int (. self queue-len))
(pthread-cond-broadcast (& (. self wake)))
(pthread-mutex-unlock (& (. self lock)))
(return pending))
(pub fn stop ((self (* tiles))) void
(pthread-mutex-lock (& (. self lock)))
(= (. self quit) true)
(pthread-cond-broadcast (& (. self wake)))
(pthread-mutex-unlock (& (. self lock)))
(for (var i int 0) (< i (. self worker-count)) (++ i)
(pthread-join [(. self workers) i] NULL))
(pthread-mutex-lock (& (. self lock)))
(.clear self)
(pthread-mutex-unlock (& (. self lock)))
(.release (& (. self index)))
(free self)))

View File

@@ -1,11 +1,47 @@
;;; The map viewer window: SDL3 for the window, events and rendering,
;;; Nuklear for the panels drawn over the map.
;;; Nuklear for the panels drawn over the map, tiles for the map.
;;;
;;; vs-map-viewer [--assets DIR] [--zoom BLOCKS-PER-POINT] [--layer N]
;;; [--screenshot FILE.png] [WORLD.vcdbs | serverconfig.json]
;;;
;;; --screenshot draws until no tile is pending, saves the window and
;;; quits, which is how the viewer is checked without looking at it.
(include "nk.h")
(include stdio.h stdlib.h string.h math.h "nk.h" "stb_image_write.h")
(import cs)
(import json)
(import maps)
(import lcg)
(import noise)
(import shape)
(import worldgen)
(import settings)
(import tiles)
(define WINDOW-WIDTH 1280)
(define WINDOW-HEIGHT 800)
(define FONT-SIZE 13.0)
(define REGION 512)
;; Blocks per window point, either way. Zoomed out further, a window
;; shows more regions than the tile cache holds.
(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 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 app
((window (* SDL-Window)) (renderer (* SDL-Renderer)) (ctx (* struct nk-context))
(assets-dir [char 1024]) (world-path [char 1024])
(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)
(hover hover) (pending int) (status [char 256]) (screenshot (* const char))))
(fn fail ((what (* const char))) int
(SDL-Log "%s: %s" what (SDL-GetError))
@@ -23,60 +59,408 @@
(/= (. font handle height) scale)
(nk-style-set-font ctx (& (. font handle))))
(fn draw-ui ((ctx (* struct nk-context)) (frame-ms double)) void
(if (nk-begin ctx "vs-map-viewer" (nk-rect 10 10 260 120)
(| NK-WINDOW-BORDER NK-WINDOW-MOVABLE NK-WINDOW-TITLE
NK-WINDOW-MINIMIZABLE))
;;; World ↔ window -------------------------------------------------
(fn window-size ((a (* app)) (w (* int)) (h (* int))) void
(SDL-GetWindowSize (. a window) w h))
(fn to-world-x ((a (* app)) (sx float) (w int)) double
(return (+ (. a center-x) (* (- sx (/ w 2.0)) (. a zoom)))))
(fn to-world-z ((a (* app)) (sy float) (h int)) double
(return (+ (. a center-z) (* (- sy (/ h 2.0)) (. a zoom)))))
(fn to-screen ((a (* app)) (world double) (center double) (extent int)) float
(return (cast (+ (/ (- world center) (. a zoom)) (/ extent 2.0)) float)))
(fn go-to-spawn ((a (* app))) void
(= (. a center-x) (/ (. a source settings size-x) 2.0))
(= (. a center-z) (/ (. a source settings size-z) 2.0)))
;;; Applying settings ----------------------------------------------
(fn release-hover ((a (* app))) void
(if (. a hover valid)
(region-maps/free (& (. a hover maps))))
(= (. a hover valid) false))
;;; Makes SOURCE the world shown: a fresh generator for the panels,
;;; and every tile regenerated.
(fn apply ((a (* app)) (source world-source)) bool
(var g (* worldgen) (worldgen/make (. source settings) (. a assets-dir)))
(if (! g)
(do
(nk-layout-row-dynamic ctx 20 1)
(nk-label ctx "Vintage Story 1.22.7 + Rivers 5.0.3" NK-TEXT-LEFT)
(nk-labelf ctx NK-TEXT-LEFT "frame %.1f ms" frame-ms)))
(snprintf (. a status) (sizeof (. a status)) "cannot load worldgen assets from %s"
(. a assets-dir))
(return false)))
(if (. a g)
(worldgen/free (. a g)))
(= (. a g) g)
(= (. a source) source)
(= (. a edit) source)
(release-hover a)
(if (>= (. a stratum) (. g strata count))
(= (. a stratum) 0))
(.set-settings (. a tiles) (. source settings))
(snprintf (. a status) (sizeof (. a status)) "seed %d" (. source settings seed))
(return true))
(fn load-world ((a (* app))) void
(var source world-source)
(if (world-source/load (. a world-path) (& source))
(do
(apply a source)
(go-to-spawn a))
(snprintf (. a status) (sizeof (. a status)) "cannot load %s" (. a world-path))))
;;; Map ------------------------------------------------------------
(record wanted ((rx int) (rz int) (distance double)))
(fn by-distance ((pa (* const void)) (pb (* const void))) int
(var wa (* const wanted) pa)
(var wb (* const wanted) pb)
(var da double (. wa distance))
(var db double (. wb distance))
(return (- (> da db) (< da db))))
;;; Requests the visible tiles, nearest the centre first, and draws
;;; those that are ready.
(fn draw-map ((a (* app))) void
(var w int)
(var h int)
(window-size a (& w) (& h))
(var s (* world-settings) (& (. a source settings)))
(var x0 double (to-world-x a 0 w))
(var z0 double (to-world-z a 0 h))
(var x1 double (to-world-x a w w))
(var z1 double (to-world-z a h h))
(var rx0 int (cast (fmax 0 (floor (/ x0 REGION))) int))
(var rz0 int (cast (fmax 0 (floor (/ z0 REGION))) int))
(var rx1 int (cast (fmin (- (/ (. s size-x) REGION) 1) (floor (/ x1 REGION))) int))
(var rz1 int (cast (fmin (- (/ (. s size-z) REGION) 1) (floor (/ z1 REGION))) int))
(var count int (* (fmax 0 (+ (- rx1 rx0) 1)) (fmax 0 (+ (- rz1 rz0) 1))))
(var list (* wanted) (malloc (* (+ count 1) (sizeof wanted))))
(var n int 0)
(for (var rz int rz0) (<= rz rz1) (++ rz)
(for (var rx int rx0) (<= rx rx1) (++ rx)
(var dx double (- (* (+ rx 0.5) REGION) (. a center-x)))
(var dz double (- (* (+ rz 0.5) REGION) (. a center-z)))
(= [list n] #(wanted : rx rz (+ (* dx dx) (* dz dz))))
(++ n)))
(qsort list n (sizeof wanted) by-distance)
(.begin-frame (. a tiles))
(for (var i int 0) (< i n) (++ i)
(var k tile-key)
(= (. k map) (. a layer))
(= (. k stratum) (. a stratum))
(= (. k rx) (. [list i] rx))
(= (. k rz) (. [list i] rz))
(var tex (* SDL-Texture) (.request (. a tiles) (. a renderer) k))
(if (! tex)
(continue))
(var sx0 float (roundf (to-screen a (* (. k rx) REGION) (. a center-x) w)))
(var sz0 float (roundf (to-screen a (* (. k rz) REGION) (. a center-z) h)))
(var sx1 float (roundf (to-screen a (* (+ (. k rx) 1) REGION) (. a center-x) w)))
(var sz1 float (roundf (to-screen a (* (+ (. k rz) 1) REGION) (. a center-z) h)))
(var dst SDL-FRect #(SDL-FRect : sx0 sz0 (- sx1 sx0) (- sz1 sz0)))
(SDL-RenderTexture (. a renderer) tex NULL (& dst)))
(= (. a pending) (.end-frame (. a tiles)))
(free list))
;;; Spawn and the story locations, as small squares with their names.
(fn draw-markers ((a (* app))) void
(var w int)
(var h int)
(window-size a (& w) (& h))
(var r (* SDL-Renderer) (. a renderer))
(var g (* worldgen) (. a g))
(SDL-SetRenderDrawColor r 255 255 255 255)
(for (var i int -1) (< i (. g story-count)) (++ i)
(var x double (/ (. a source settings size-x) 2.0))
(var z double (/ (. a source settings size-z) 2.0))
(var name (* const char) "spawn")
(if (>= i 0)
(do
(= x (. [(. g stories) i] x))
(= z (. [(. g stories) i] z))
(= name (. [(. g stories) i] code))))
(var sx float (to-screen a x (. a center-x) w))
(var sy float (to-screen a z (. a center-z) h))
(var box SDL-FRect #(SDL-FRect : (- sx 3) (- sy 3) 7 7))
(SDL-RenderRect r (& box))
(SDL-RenderDebugText r (+ sx 6) (- sy 4) name)))
;;; Panels ---------------------------------------------------------
(fn property-float ((ctx (* struct nk-context)) (name (* const char)) (v (* float))
(lo float) (hi float) (step float)) void
(nk-property-float ctx name lo v hi step (/ step 4)))
(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)
(| NK-WINDOW-BORDER NK-WINDOW-MOVABLE NK-WINDOW-TITLE NK-WINDOW-MINIMIZABLE
NK-WINDOW-SCALABLE))
(do
(nk-layout-row-dynamic ctx 22 1)
(nk-label ctx "World or server config file" 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 world-path)
(sizeof (. a world-path)) nk-filter-default)
(if (nk-button-label ctx "Load")
(load-world a))
(nk-layout-row-dynamic ctx 8 1)
(nk-spacing ctx 1)
(nk-layout-row-template-begin ctx 24)
(nk-layout-row-template-push-static ctx 40)
(nk-layout-row-template-push-dynamic ctx)
(nk-layout-row-template-end ctx)
(nk-label ctx "Seed" NK-TEXT-LEFT)
(nk-edit-string-zero-terminated ctx NK-EDIT-FIELD (. a edit seed-text)
(sizeof (. a edit seed-text)) nk-filter-default)
(nk-layout-row-dynamic ctx 22 1)
(nk-property-int ctx "World width" 512 (& (. e size-x)) 8192000 512 512)
(nk-property-int ctx "World length" 512 (& (. e size-z)) 8192000 512 512)
(nk-property-int ctx "World height" 256 (& (. e size-y)) 1024 64 64)
(property-float ctx "Landcover" (& (. e landcover)) 0 1 0.05)
(property-float ctx "Ocean scale" (& (. e oceanscale)) 0.1 10 0.1)
(property-float ctx "Landform scale" (& (. e landform-scale)) 0.1 4 0.1)
(property-float ctx "Upheaval commonness" (& (. e upheaval-commonness)) 0 1 0.05)
(property-float ctx "Geologic activity" (& (. e geologic-activity)) 0.01 1 0.05)
(nk-property-int ctx "Polar-equator distance" 1000 (& (. e polar-equator-distance))
1000000 1000 100)
(property-float ctx "Global temperature" (& (. e global-temperature)) 0 5 0.1)
(property-float ctx "Global precipitation" (& (. e global-precipitation)) 0 5 0.1)
(property-float ctx "Story distance scale" (& (. e story-dist-scaling)) 0.05 2 0.05)
(var current int 0)
(for (var i int 0) (< i 5) (++ i)
(if (== (strcmp (. e starting-climate) [climate-names i]) 0)
(= current i)))
(nk-layout-row-dynamic ctx 22 2)
(nk-label ctx "Starting climate" NK-TEXT-LEFT)
(var chosen int (nk-combo ctx climate-names 5 current 20 (nk-vec2 140 120)))
(snprintf (. e starting-climate) 16 "%s" [climate-names chosen])
(nk-layout-row-dynamic ctx 26 2)
(if (nk-button-label ctx "Apply")
(do
(var source world-source (. a edit))
(= (. source settings seed) (world-seed (. source seed-text)))
(apply a source)))
(if (nk-button-label ctx "Go to spawn")
(go-to-spawn a))))
(nk-end ctx))
(fn draw-layers ((a (* app))) void
(var ctx (* struct nk-context) (. a ctx))
(var w int)
(var h int)
(window-size a (& w) (& h))
(if (nk-begin ctx "Layer" (nk-rect (- w 250) 10 240 220)
(| 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)
(if (nk-option-label ctx [layer-names i] (== (. a layer) i))
(= (. a layer) i)))
(if (== (. a layer) MAP-STRATA)
(do
(var g (* worldgen) (. a g))
(nk-layout-row-dynamic ctx 22 1)
(= (. a stratum)
(nk-combo ctx (cast (. g strata codes) (* * const char)) (. g strata count)
(. a stratum) 20 (nk-vec2 200 300)))))))
(nk-end ctx))
;;; A map's value at a block of its region.
(fn sample ((m (* const int-map)) (rx int) (rz int) (bx int) (bz int)) int
(var inner int (- (. m size) (. m pad-tl) (. m pad-br)))
(var px int (+ (. m pad-tl) (/ (* (- bx (* rx REGION)) inner) REGION)))
(var pz int (+ (. m pad-tl) (/ (* (- bz (* rz REGION)) inner) REGION)))
(return [(. m data) (+ (* pz (. m size)) px)]))
(fn draw-info ((a (* app))) void
(var ctx (* struct nk-context) (. a ctx))
(var w int)
(var h int)
(window-size a (& w) (& h))
(var s (* world-settings) (& (. a source settings)))
(var bx int (cast (floor (to-world-x a (. a mouse-x) w)) int))
(var bz int (cast (floor (to-world-z a (. a mouse-y) h)) int))
(var inside bool (&& (>= bx 0) (>= bz 0) (< bx (. s size-x)) (< bz (. s size-z))))
(var rx int (/ bx REGION))
(var rz int (/ bz REGION))
(if (&& inside (|| (! (. a hover valid)) (!= (. a hover rx) rx) (!= (. a hover rz) rz)))
(do
(release-hover a)
(worldgen/region (. a g) rx rz (& (. a hover maps)))
(= (. a hover valid) true)
(= (. a hover rx) rx)
(= (. a hover rz) rz)))
(if (nk-begin ctx "Info" (nk-rect 10 (- h 230) 290 220)
(| NK-WINDOW-BORDER NK-WINDOW-MOVABLE NK-WINDOW-TITLE NK-WINDOW-MINIMIZABLE))
(do
(nk-layout-row-dynamic ctx 18 1)
(nk-labelf ctx NK-TEXT-LEFT "x %d z %d (from centre %d, %d)" bx bz
(- bx (/ (. s size-x) 2)) (- bz (/ (. s size-z) 2)))
(nk-labelf ctx NK-TEXT-LEFT "region %d, %d zoom %.3g blocks/pt" rx rz (. a zoom))
(if (&& inside (. a hover valid))
(do
(var g (* worldgen) (. a g))
(var m (* region-maps) (& (. a hover maps)))
(var lf int (sample (& (. m landform)) rx rz bx bz))
(nk-labelf ctx NK-TEXT-LEFT "landform %s" (. [(. g landform-table all) lf] code))
(var pv int (sample (& (. m geoprov)) rx rz bx bz))
(nk-labelf ctx NK-TEXT-LEFT "province %s" [(. g provinces codes) pv])
(var c int (sample (& (. m climate)) rx rz bx bz))
(nk-labelf ctx NK-TEXT-LEFT "climate %d C at sea level, rain %d, activity %d"
(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))))
(nk-labelf ctx NK-TEXT-LEFT "%d tiles pending" (. a pending))
(nk-label ctx (. a status) NK-TEXT-LEFT)))
(nk-end ctx))
;;; Events ---------------------------------------------------------
;;; Zooms by FACTOR keeping the block under the cursor where it is.
(fn zoom-at ((a (* app)) (factor double)) void
(var w int)
(var h int)
(window-size a (& w) (& h))
(var wx double (to-world-x a (. a mouse-x) w))
(var wz double (to-world-z a (. a mouse-y) h))
(= (. a zoom) (fmin MAX-ZOOM (fmax MIN-ZOOM (* (. a zoom) factor))))
(= (. a center-x) (- wx (* (- (. a mouse-x) (/ w 2.0)) (. a zoom))))
(= (. a center-z) (- wz (* (- (. a mouse-y) (/ h 2.0)) (. a zoom)))))
(fn handle-event ((a (* app)) (event (* SDL-Event))) bool
(var type u32 (. event type))
(if (== type SDL-EVENT-QUIT)
(return false))
(SDL-ConvertEventToRenderCoordinates (. a renderer) event)
(if (== type SDL-EVENT-MOUSE-MOTION)
(do
(if (. a dragging)
(do
(-= (. a center-x) (* (. event motion xrel) (. a zoom)))
(-= (. a center-z) (* (. event motion yrel) (. a zoom)))))
(= (. a mouse-x) (. event motion x))
(= (. a mouse-y) (. event motion y)))
(&& (== type SDL-EVENT-MOUSE-BUTTON-DOWN) (! (. a ui-hovered))
(== (. event button button) SDL-BUTTON-LEFT))
(= (. a dragging) true)
(== type SDL-EVENT-MOUSE-BUTTON-UP)
(= (. a dragging) false)
(&& (== type SDL-EVENT-MOUSE-WHEEL) (! (. a ui-hovered)))
(zoom-at a (pow 1.25 (- (. event wheel y)))))
(nk-sdl-handle-event (. a ctx) event)
(return true))
(fn save-screenshot ((a (* app))) bool
(var shot (* SDL-Surface) (SDL-RenderReadPixels (. a renderer) NULL))
(if (! shot)
(return false))
(var rgba (* SDL-Surface) (SDL-ConvertSurface shot SDL-PIXELFORMAT-ABGR8888))
(SDL-DestroySurface shot)
(if (! rgba)
(return false))
(var ok bool (!= (stbi-write-png (. a screenshot) (. rgba w) (. rgba h) 4 (. rgba pixels)
(. rgba pitch)) 0))
(SDL-DestroySurface rgba)
(return ok))
;;; Main -----------------------------------------------------------
(pub fn main ((argc int) (argv [* char])) int
(var a (* app) (calloc 1 (sizeof app)))
(snprintf (. a assets-dir) (sizeof (. a assets-dir)) "assets")
(snprintf (. a world-path) (sizeof (. a world-path)) "serverconfig.json")
(for (var i int 1) (< i argc) (++ i)
(var more bool (< (+ i 1) argc))
(if (&& (== (strcmp [argv i] "--assets") 0) more)
(do
(++ i)
(snprintf (. a assets-dir) (sizeof (. a assets-dir)) "%s" [argv i]))
(&& (== (strcmp [argv i] "--zoom") 0) more)
(do (++ i) (= (. a zoom) (atof [argv i])))
(&& (== (strcmp [argv i] "--layer") 0) more)
(do (++ i) (= (. a layer) (atoi [argv i])))
(&& (== (strcmp [argv i] "--screenshot") 0) more)
(do (++ i) (= (. a screenshot) [argv i]))
(snprintf (. a world-path) (sizeof (. a world-path)) "%s" [argv i])))
(if (! (SDL-Init SDL-INIT-VIDEO))
(return (fail "SDL_Init")))
(var window (* SDL-Window) NULL)
(var renderer (* SDL-Renderer) NULL)
(if (! (SDL-CreateWindowAndRenderer "vs-map-viewer" WINDOW-WIDTH WINDOW-HEIGHT
(| SDL-WINDOW-RESIZABLE
SDL-WINDOW-HIGH-PIXEL-DENSITY)
(& window) (& renderer)))
(| SDL-WINDOW-RESIZABLE SDL-WINDOW-HIGH-PIXEL-DENSITY)
(& (. a window)) (& (. a renderer))))
(return (fail "SDL_CreateWindowAndRenderer")))
(SDL-SetRenderVSync renderer 1)
(SDL-SetRenderVSync (. a renderer) 1)
(var scale float (SDL-GetWindowDisplayScale (. a window)))
(SDL-SetRenderScale (. a renderer) scale scale)
(= (. a ctx) (nk-sdl-init (. a window) (. a renderer) (nk-sdl-allocator)))
(setup-font (. a ctx) scale)
(var scale float (SDL-GetWindowDisplayScale window))
(SDL-SetRenderScale renderer scale scale)
(var ctx (* struct nk-context) (nk-sdl-init window renderer (nk-sdl-allocator)))
(setup-font ctx scale)
(noise-init)
(var source world-source)
(if (! (world-source/load (. a world-path) (& source)))
(do
(= (. 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))
(if (<= (. a zoom) 0)
(= (. a zoom) 4))
(if (! (apply a source))
(do
(fprintf stderr "%s\n" (. a status))
(return 1)))
(go-to-spawn a)
(var running bool true)
(var last-ns u64 (SDL-GetTicksNS))
(while running
(var now-ns u64 (SDL-GetTicksNS))
(var frame-ms double (/ (cast (- now-ns last-ns) double) 1e6))
(= last-ns now-ns)
;; Nothing left to generate: sleep until something happens.
(if (&& (== (. a pending) 0) (! (. a dragging)) (! (. a screenshot)))
(SDL-WaitEventTimeout NULL 500))
(var event SDL-Event)
(nk-input-begin ctx)
(nk-input-begin (. a ctx))
(while (SDL-PollEvent (& event))
(if (== (. event type) SDL-EVENT-QUIT)
(= running false))
(SDL-ConvertEventToRenderCoordinates renderer (& event))
(nk-sdl-handle-event ctx (& event)))
(nk-sdl-update-TextInput ctx)
(nk-input-end ctx)
(if (! (handle-event a (& event)))
(= running false)))
(nk-sdl-update-TextInput (. a ctx))
(nk-input-end (. a ctx))
(draw-ui ctx frame-ms)
(draw-settings a)
(draw-layers a)
(draw-info a)
(= (. a ui-hovered) (|| (nk-window-is-any-hovered (. a ctx)) (nk-item-is-any-active (. a ctx))))
(SDL-SetRenderDrawColor renderer 24 32 40 255)
(SDL-RenderClear renderer)
(nk-sdl-render ctx NK-ANTI-ALIASING-ON)
(SDL-RenderPresent renderer))
(SDL-SetRenderDrawColor (. a renderer) 24 32 40 255)
(SDL-RenderClear (. a renderer))
(draw-map a)
(draw-markers a)
(nk-sdl-render (. a ctx) NK-ANTI-ALIASING-ON)
(if (&& (. a screenshot) (== (. a pending) 0))
(do
(if (! (save-screenshot a))
(fprintf stderr "%s: %s\n" (. a screenshot) (SDL-GetError)))
(= running false)))
(SDL-RenderPresent (. a renderer)))
(nk-sdl-shutdown ctx)
(SDL-DestroyRenderer renderer)
(SDL-DestroyWindow window)
(.stop (. a tiles))
(release-hover a)
(worldgen/free (. a g))
(nk-sdl-shutdown (. a ctx))
(SDL-DestroyRenderer (. a renderer))
(SDL-DestroyWindow (. a window))
(SDL-Quit)
(return 0))

View File

@@ -13,6 +13,7 @@
(import save)
(import dump)
(import worldgen)
(import settings)
(import verify)
(var worldgen-assets [(* const char) 6]
@@ -73,22 +74,18 @@
(cJSON-Delete j))
(return bad))
;;; Generates every region the save stored, with the save's own world
;;; settings, and diffs the maps.
;;; Generates every region the save stored, with the save's own world
;;; settings, and diffs the maps.
(fn verify ((save-path (* const char)) (assets-dir (* const char)) (out (* const char))) int
(var s (* save) (save/open save-path))
(if (! s)
(var source world-source)
(if (! (world-source/load save-path (& source)))
(return 1))
(var settings world-settings (world-settings/defaults))
(= (. settings seed) (. s seed))
(= (. settings size-x) (. s size-x))
(= (. settings size-y) (. s size-y))
(= (. settings size-z) (. s size-z))
(for (var i int 0) (< i (. s config-count)) (++ i)
(.set (& settings) [(. s config-keys) i] [(. s config-values) i]))
(var g (* worldgen) (worldgen/make settings assets-dir))
(var g (* worldgen) (worldgen/make (. source settings) assets-dir))
(if (! g)
(return 1))
(var s (* save) (save/open save-path))
(var failed int (+ (verify-stories s g) (verify-regions s g out)))
(save/close s)
(return failed))

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