Compare commits

...

3 Commits

Author SHA1 Message Date
df0b8d815a note the silent misparses of sex#59 found in this project
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 21:02:38 +03:00
c14c154303 fix four conditional operands sexc emitted without parentheses, and mark every Sex workaround with a TODO
A conditional as an operand loses its parentheses (sex#59): box-blur
allocated a quarter of its buffer, deep water got the shallow colour,
and verify's delta histogram wrote out of bounds into the height
classes. Plan.org quoted the corrupted count.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 21:01:59 +03:00
39b0e74f2b add a readme with usage and an example screenshot
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 20:50:35 +03:00
25 changed files with 350 additions and 5 deletions

View File

@@ -16,10 +16,14 @@ all: $(B)/vsmv $(B)/vs-map-viewer
# sexc writes no dependency files, and an import pastes the imported
# module's public forms into the importer, so any Sex source may change
# any Sex object.
# TODO sex#40: depend on what each unit imports once sexc writes
# dependency files.
SEX_SRC := $(wildcard src/*.sex)
# Statement-level #line directives can land inside a ?: and break the
# C (see Sex-field-notes.org), so they are kept to toplevel forms.
# TODO sex#54: drop --line-directives=toplevel once #line no longer
# lands inside a ?:.
$(B)/%.o: src/%.sex $(SEX_SRC) | $(B)
$(SEXC) --line-directives=toplevel $< -c -o $@ -- $(CFLAGS) $(SEX_CFLAGS) $(CPPFLAGS)

View File

@@ -424,7 +424,7 @@ lets carving reach y = 1.
Against the 11,687,936 stored columns:
- *Exact:* 11,481,361 (98.2%).
- *Stored higher, under water:* 141,978. GenBlockLayers raises dry
- *Stored higher, under water:* 126,543. GenBlockLayers raises dry
seabeds after the strata.
- *Stored lower, on land:* 77,222. GenCaves lowers the height where a
cave opens the surface.

176
Readme.org Normal file
View File

@@ -0,0 +1,176 @@
* vs-map-viewer
A map previewer for Vintage Story worlds. Give it a seed and world
settings, and it generates the world the way the game would and shows
it as a zoomable top-down map: terrain, rivers, oceans, climate,
landforms, geologic provinces, and rock strata at any depth. It is
mostly for finding rock: where chalk, bauxite or limestone lie, at the
surface or 20 blocks down, and which one is nearest.
[[file:doc/screenshot.png]]
It covers Vintage Story 1.22.7 with the Rivers 5.0.3 mod. The
generator is a port of the game's own, written in [[https://git.kotobank.ch/alex-eg/sex][Sex]]. It is checked
against a real save, and matches it:
| What | Matches |
|-------------------------------------------+-----------------------------------------|
| Region maps: landform, climate, province, ocean, upheaval, strata | every pixel of 118 regions |
| Story structure locations | all 6 |
| Rivers' distance to the nearest river | 100% of 6.5 million columns |
| Terrain height | 98.2% of 11.7 million columns |
| Rock at the surface | all but 14 of 11.7 million columns |
All but 0.02% of the other heights come from two passes that run after
the strata are laid: caves that open the surface, and the raise of dry
seabeds. Neither changes which rock is where.
=Plan.org= has the details.
** Building
You need:
- the Sex compiler, expected at =~/dev/sex/sexc= (or set =SEXC=);
- a C compiler;
- SDL3 (=brew install sdl3=);
- SQLite3;
- a checkout of [[https://github.com/DaveGamble/cJSON][cJSON]] in =./cJSON=.
Nuklear and stb_image_write are vendored in =third_party/=.
#+begin_src sh
make
#+end_src
This builds two programs into =build/=: =vs-map-viewer=, the viewer,
and =vsmv=, the command-line tool.
The game's assets are not part of the repository. Copy the =assets=
directory of a Vintage Story 1.22.7 install to =./assets=, or point
=--assets= at it. The viewer
reads =assets/survival/worldgen/*.json=.
** Running
#+begin_src sh
build/vs-map-viewer [WORLD]
#+end_src
WORLD is where the seed and settings come from:
- a save, =something.vcdbs=: the world as it was created, including its
real size;
- or a =serverconfig.json=: the seed as typed and the world config.
Without WORLD it opens =./serverconfig.json=. It starts at spawn, in the
middle of the map.
Options:
| Option | Does |
|-------------------------------------+---------------------------------------------------------|
| =--assets DIR= | where the game's =assets= are; default =./assets= |
| =--zoom Z= | blocks per screen point, 1/16 to 32; default 4 |
| =--base LAYER= | the base layer: =ocean=, =rivers= or =height=; default =height= |
| =--overlay NAME= | turn on an overlay: =landform=, =climate=, =province=, =upheaval=, =strata=, =rock=; repeatable |
| =--opacity F= | overlay opacity, 0 to 1; default 0.5 |
| =--rock-depth N= / =--rock-y Y= | rocks at N blocks under the surface, or at height Y |
| =--highlight ROCK= | highlight a rock, e.g. =chalk=; repeatable |
| =--find= | find the nearest highlighted rock on start |
| =--probe X Z= | probe the column at block X, Z on start |
| =--screenshot FILE.png= | wait until everything is drawn, save the window, quit |
| =--mouse X Y= | place the cursor, for screenshots |
The screenshot above came from:
#+begin_src sh
build/vs-map-viewer --zoom 14 --overlay rock --opacity 0.6 \
--probe 1027600 13800 --mouse 1000 380 \
--screenshot doc/screenshot.png "ktt9s village story-new.vcdbs"
#+end_src
** Using the viewer
The map fills the middle of the window, between two panels.
*** The map
- Drag with the left mouse button to pan.
- Scroll to zoom, around the cursor.
- Click (without dragging) to probe that column: its rock strata appear
in the Probe section, and a numbered pin marks the place. Click a pin
to probe it again.
- With /Rock under cursor/ on, a label by the cursor names the rock
under it, as the Rocks section is set (at the surface, at a depth, or
at a height).
The further out you zoom, the coarser the terrain is sampled: every
block when zoomed in close, one in 32 when far out. Only what is on screen is computed;
when nothing is left to compute, the viewer sleeps.
*** Left panel
- *World*: where the world comes from, and its settings.
- Type a save or =serverconfig.json= path and press /Load/.
- Leave /Rivers config/ empty for the mod's defaults, or load a
=ModConfig/rivers.json=.
- Change any setting (seed, sizes, landcover, scales, climate...)
and press /Apply/ to generate that world instead. The seed can be
any text, as typed into the game.
- /Go to spawn/ goes back to the centre of the map.
- *Info*: what is under the cursor:
- coordinates, absolute and from the map centre, and the region;
- landform and geologic province;
- climate: temperature at sea level, rain, geologic activity;
- ocean and upheaval values;
- distance to the nearest river;
- terrain height and the rock on top.
Below it, how many tiles are still being generated.
- *Probe*: the last column probed: its surface height and its strata,
top down, with the Y range and thickness of each.
- /Go to/ centres it.
- /Clear pins/ removes the pins.
- /Close/ hides it.
*** Right panel
- *Layers*:
- *Base*: one of Ocean, Rivers or Terrain height. Drawn opaque.
- *Overlays*: any of Landform, Climate, Geologic province, Upheaval,
Rock strata thickness (pick the stratum below it) and Rock. Drawn
over the base at the /Overlay opacity/.
- /Story locations/ marks spawn and the story structures.
- /Rock under cursor/ turns the cursor label on and off.
- *Rocks*: finding rock.
- Choose where to look: at the /Surface/, at a /Depth below
surface/, or /At Y/, and set the depth or Y.
- The legend lists every rock with its share of the visible map,
while the Rock overlay is on.
- Click rocks in the legend to highlight them. With any highlighted,
the Rock overlay shows only those, and the base shows through
elsewhere. /Show all/ clears the highlight.
- /Find nearest/ looks for the highlighted rocks around the centre of
the view, up to about 12,000 blocks out, jumps to the nearest one
and probes it. It samples every 16 blocks before it refines, so a
patch smaller than that can be missed. /Cancel/ stops it.
** vsmv
The command-line side, mostly for checking the generator against a
save:
| Command | Does |
|----------------------------------+----------------------------------------------------------|
| =vsmv info SAVE.vcdbs= | the save's seed, size, world config and story locations |
| =vsmv seed TEXT...= | the numeric seed the game makes of TEXT |
| =vsmv assets DIR= | checks that the worldgen assets load |
| =vsmv dump SAVE.vcdbs OUT= | PNGs of the maps the save stored |
| =vsmv verify SAVE.vcdbs DIR OUT= | regenerates everything the save stored and compares it; diff images go to OUT |
=verify= takes a few minutes, most of it on the terrain heights of
every stored chunk.
** Limits
- Only the /realistic/ world climate is supported, not /patchy/.
- Strata below the surface use the same code as the surface rock,
which is checked, but they are not checked against the save's blocks.
- Rivers' settings are not stored in a save. Without a =rivers.json=,
the mod's defaults are assumed.
- Other mods that change worldgen are not modelled.
** Notes
- =Plan.org= is the design and how each part was checked.
- =Sex-field-notes.org= lists the problems the Sex compiler showed
while this was built. They are filed as issues #40 to #63 on its
tracker.

View File

@@ -42,6 +42,24 @@ Clang only warns (-Wparentheses). This one computes wrong values
silently, so it is the most dangerous entry here. Bound to a variable
first, it's right.
Clang warns only when the operator ends up in the condition, as above.
When the conditional comes first, the rest of the operand moves into
the else branch, and that is valid C nothing warns about. A sweep of
this project found four such sites that had compiled cleanly with
-Wall -Wparentheses:
- =(malloc (* (if (> w h) w h) (sizeof u32)))= emitted
=malloc((w > h) ? w : h * sizeof(u32))=: when =w > h=, a buffer a
quarter of the size, so a heap overflow on every wide box blur.
- =(+ (if (< d -4) -4 (if (> d 4) 4 d)) 4)= gave an index of -4, an
out-of-bounds write that corrupted a neighbouring array of counts
and with it a figure in our own docs.
- =(* (if (> depth 40) 40 depth) 255)= gave water deeper than 40 blocks
the shallow colour.
- =(cast (if ...) float)= cast only the condition; harmless there.
Only the generated C showed them. The fix is the same: bind to a
variable first.
* An import doesn't bring the types its interface needs
Module =save= takes a =(* region-maps)= from module =maps=. Importing
=save= pastes its prototypes but not =region-maps=, so every importer

BIN
doc/screenshot.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 MiB

View File

@@ -10,6 +10,8 @@
(var p (* const u8) (* s))
(var c u32 [p 0])
(var n int 0)
;; TODO sex#47: write the UTF-8 masks in hex once there are hex
;; literals.
(if (< c 128) (= n 0)
(== (& c 224) 192) (do (= n 1) (&= c 31))
(== (& c 240) 224) (do (= n 2) (&= c 15))
@@ -37,6 +39,8 @@
(= [units n] c)
(+= n 1))
(do
;; TODO sex#47: write the surrogate constants in hex once
;; there are hex literals.
(-= c 65536)
(= [units n] (+ 55296 (>> c 10)))
(= [units (+ n 1)] (+ 56320 (& c 1023)))
@@ -67,6 +71,8 @@
(if (> len 0)
(= h1 (hash-step h1 (c-bit-or [units i] (<< (cast [units (+ i 1)] u32) 16)))))
(free units)
;; TODO sex#47: write the multiplier (0x5D588B65) in hex once there
;; are hex literals.
(return (cast (+ (cast h1 u32) (* (cast h2 u32) (cast 1566083941 u32))) i32)))
;;; The world seed from the seed text in world settings. A number is

View File

@@ -9,12 +9,15 @@
(fn landform-colour ((v i32)) u32 (return (palette (cast v u32))))
(fn province-colour ((v i32)) u32 (return (palette (+ (cast v u32) 100))))
;; TODO sex#47: write the mask in hex once there are hex literals.
(fn climate-colour ((v i32)) u32 (return (& (cast v u32) 16777215)))
(fn byte-colour ((v i32)) u32 (return (grey v)))
;;; The inner square of one region's map, placed at (ox, oz) and
;;; stretched to CELL pixels a side, so maps of different resolutions
;;; line up pixel for pixel across mosaics.
;; TODO sex#53: name the colour function type with a typedef once that
;; no longer crashes sexc.
(fn paint-map ((im (* image)) (m (* const int-map)) (ox int) (oz int) (cell int)
(colour (fn ((i32)) u32))) void
(var inner int (- (. m size) (. m pad-tl) (. m pad-br)))
@@ -48,6 +51,8 @@
(var province image (image/make w h))
(var ocean image (image/make w h))
(var upheaval image (image/make w h))
;; TODO sex#50: write MAX-STRATA once constants in array types are
;; kebab-converted.
(var strata [image MAX_STRATA])
(var strata-count int 0)
@@ -126,7 +131,11 @@
(if (. m has-river-distance)
(do
(var d int [(. m river-distance) i])
;; TODO sex#54: pass the colour as an (if ...) argument to
;; .set once #line no longer splits it.
(var c u32 (grey (- 255 d)))
;; TODO sex#47: write the colours in hex once there are
;; hex literals.
(if (== d 0) (= c 4219071))
(.set (& river) x y c))
(.set (& river) x y 2105376)))))

View File

@@ -9,6 +9,9 @@
(pub include pthread.h)
(include stdio.h stdlib.h string.h math.h)
;; TODO sex#58: import only the modules used here once an import brings
;; the types its interface needs; until then, every dependency, in
;; dependency order.
(import maps)
(import lcg)
(import noise)

View File

@@ -27,6 +27,8 @@
;;; A distinct, stable colour per small integer: landform and province
;;; indices, block ids.
(pub fn palette ((i u32)) u32
;; TODO sex#47: write the multiplier (0x9E3779B1) and masks in hex
;; once there are hex literals.
(var h u32 (* (+ i 1) (cast 2654435761 u32)))
(return (c-bit-or (<< (+ 48 (& (>> h 24) 191)) 16)
(<< (+ 48 (& (>> h 16) 191)) 8)

View File

@@ -6,6 +6,7 @@
(pub include "cJSON.h")
(include stdio.h stdlib.h string.h ctype.h)
;; TODO sex#49: rename to ident-start? once ? is mangled in names.
(fn ident-start-char ((c char)) bool
(return (|| (isalpha (cast c u8)) (== c #\_) (== c #\$))))

View File

@@ -10,6 +10,8 @@
;;; (size - pad-tl - pad-br) square.
(pub record int-map ((data (* i32)) (size i32) (pad-tl i32) (pad-br i32)))
;; TODO sex#50: write MAX-STRATA once constants in array types are
;; kebab-converted.
(pub record region-maps
((landform int-map) (climate int-map) (geoprov int-map) (upheaval int-map)
(ocean int-map) (strata [int-map MAX_STRATA]) (strata-count int)))

View File

@@ -9,6 +9,8 @@
;;; The C# folds these at compile time; written as divisions here they
;;; would be unparenthesized macros and round differently in context.
;; TODO sex#55: write the divisions, e.g. (/ -1.0 6.0), once define
;; bodies are translated and parenthesized.
(define STRETCH-2D -0.211324865405187)
(define SQUISH-2D 0.366025403784439)
(define STRETCH-3D -0.16666666666666666)
@@ -248,6 +250,8 @@
;;; NormalizedSimplexNoise. Octave i samples at frequencies[i] and is
;;; weighted so the sum stays in a known range before tanh.
;; TODO sex#50: write MAX-OCTAVES once constants in array types are
;; kebab-converted.
(pub record simplex-noise
((count int) (frequencies [double MAX_OCTAVES]) (amplitudes [double MAX_OCTAVES])
(scaled-2d [double MAX_OCTAVES]) (scaled-3d [double MAX_OCTAVES])
@@ -276,6 +280,8 @@
;; PERSISTENCE^i.
(pub fn octaves ((count int) (base-frequency double) (persistence double) (seed i64))
(* simplex-noise)
;; TODO sex#50: write MAX-OCTAVES once constants in array types are
;; kebab-converted.
(var frequencies [double MAX_OCTAVES])
(var amplitudes [double MAX_OCTAVES])
(for (var i int 0) (< i count) (++ i)
@@ -301,6 +307,8 @@
;;; SimplexNoise: plain octave sums, no normalising and no tanh. The
;;; rock strata thickness maps and the strata distortion use it.
;; TODO sex#50: write MAX-OCTAVES once constants in array types are
;; kebab-converted.
(pub record simplex-sum
((count int) (frequencies [double MAX_OCTAVES]) (amplitudes [double MAX_OCTAVES])
(octaves [simplex-octave MAX_OCTAVES])))

View File

@@ -8,6 +8,8 @@
(pub include stddef.h)
(include string.h)
;; TODO sex#46: make these a (pub enum wire ...) once explicit enum
;; values are kebab-converted.
(pub define WIRE-VARINT 0)
(pub define WIRE-FIXED64 1)
(pub define WIRE-BYTES 2)
@@ -15,6 +17,8 @@
(pub record pb ((p (* const u8)) (end (* const u8)) (err bool)))
;; TODO sex#44: the methods are ordered so each calls only those above
;; it; order them by topic once a method can call one below.
(ops pb
(pub fn of ((data (* const void)) (len size-t)) pb
(var p (* const u8) data)
@@ -37,6 +41,8 @@
(break))
(var b u8 (* (. self p)))
(++ (. self p))
;; TODO sex#47: write the varint masks in hex once there are hex
;; literals.
(c-bit-or= v (<< (cast (& b 127) u64) shift))
(if (< b 128)
(return v)))
@@ -60,6 +66,8 @@
;; string, or a packed array.
(pub fn bytes ((self (* pb))) pb
(var len u64 (.varint self))
;; TODO sex#45: use (. self p) in the literals once member access
;; inside a compound literal follows the pointer.
(var p (* const u8) (. self p))
(if (> len (.len self))
(do

View File

@@ -10,6 +10,9 @@
;;; every RNG call is the mod's: it is what the layout depends on.
(include stdlib.h string.h math.h)
;; TODO sex#58: import only the modules used here once an import brings
;; the types its interface needs; until then, every dependency, in
;; dependency order.
(import maps)
(import lcg)
(import noise)
@@ -148,7 +151,11 @@
(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)))
;; TODO sex#59: clamp inside the cast once a conditional operand
;; keeps its parentheses.
(if (< v 0) (= v 0))
(if (> v 1) (= v 1))
(return (cast 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)
@@ -474,6 +481,7 @@
(var n int (. p config zones-in-region))
(var best-x int x)
(var best-z int z)
;; TODO sex#63: write -INFINITY once it reads as a negation.
(var best double (- INFINITY))
(var found bool false)
(for (var dx int -1) (<= dx 1) (++ dx)

View File

@@ -17,6 +17,8 @@
(pub define CHUNK-AREA 1024)
(pub define MAX-STORIES 32)
;; TODO sex#50: write CHUNK-AREA once constants in array types are
;; kebab-converted.
(pub record chunk-maps
((terrain-height [u16 CHUNK_AREA]) (rain-height [u16 CHUNK_AREA])
(top-rock [i32 CHUNK_AREA]) (river-distance [u16 CHUNK_AREA])
@@ -28,6 +30,8 @@
((db (* sqlite3)) (seed i32) (size-x i32) (size-y i32) (size-z i32)
(config-keys (* * char)) (config-values (* * char)) (config-count int)
(block-codes (* * char)) (block-count int)
;; TODO sex#50: write MAX-STORIES once constants in array types are
;; kebab-converted.
(stories [story-location MAX_STORIES]) (story-count int)))
;;; Map regions and map chunks share one key: x in the low 27 bits,
@@ -73,6 +77,8 @@
(switch type
(case 1 (snprintf value 64 "%d" (cast (read-le (& r) 4) i32)) (break))
(case 2 (snprintf value 64 "%lld" (cast (read-le (& r) 8) i64)) (break))
;; TODO sex#51: drop the (do ...) around case bodies once each
;; case gets its own scope.
(case 3
(do
(var bits u64 (read-le (& r) 8))
@@ -120,6 +126,8 @@
(if (> id max-id) (= max-id id)))
(.skip (& entry) wire))))
(= (. s block-count) (+ max-id 1))
;; TODO sex#52: write (sizeof (* char)) once sizeof takes a pointer
;; type.
(= (. s block-codes) (calloc (+ max-id 1) (sizeof [(. s block-codes) 0])))
(while (> (.field (& r) (& wire)) 0)
(var entry pb (.bytes (& r)))

View File

@@ -10,6 +10,9 @@
(pub include pthread.h)
(include stdio.h stdlib.h string.h math.h)
;; TODO sex#58: import only the modules used here once an import brings
;; the types its interface needs; until then, every dependency, in
;; dependency order.
(import maps)
(import lcg)
(import noise)
@@ -25,6 +28,8 @@
(define SEARCH-STEP 16)
(define MAX-SEARCH-THREADS 16)
;; TODO sex#60: size threads with MAX-SEARCH-THREADS once arrays of a
;; header type take a symbolic size.
(pub record search
((lock pthread-mutex-t) (threads [pthread-t 16]) (thread-count int) (running bool)
(cancel bool)
@@ -39,6 +44,8 @@
(record region-order ((rx int) (rz int) (distance double)))
(fn by-distance ((pa (* const void)) (pb (* const void))) int
;; TODO sex#61: cast inside the field access once a cast as the object
;; of . keeps its parentheses.
(var a (* const region-order) pa)
(var b (* const region-order) pb)
(return (- (> (. a distance) (. b distance)) (< (. a distance) (. b distance)))))

View File

@@ -3,6 +3,9 @@
;;; has the seed as typed and the world config as strings.
(include stdio.h stdlib.h string.h)
;; TODO sex#58: import only the modules used here once an import brings
;; the types its interface needs; until then, every dependency, in
;; dependency order.
(import cs)
(import json)
(import maps)

View File

@@ -100,6 +100,8 @@
;;; NaturalShape -----------------------------------------------------
;; TODO sex#57: make this private again once a pub record can use a
;; private type.
(pub record shape-cell ((x int) (z int) (open [bool 4]) (live bool)))
(pub record natural-shape
@@ -238,6 +240,8 @@
(for (var i int 0) (< i (. in cap)) (++ i)
(if (== [(. in state) i] 1)
(.put set (+ x0 (cast (>> [(. in keys) i] 32) i32))
;; TODO sex#47: write the mask in hex once there are hex
;; literals.
(+ z0 (cast (& [(. in keys) i] 4294967295) i32)) 1))))
(pub fn release ((self (* natural-shape))) void

View File

@@ -10,6 +10,9 @@
;;; if it were rock, as the game's prospecting assumes too.
(include stdio.h stdlib.h string.h math.h)
;; TODO sex#58: import only the modules used here once an import brings
;; the types its interface needs; until then, every dependency, in
;; dependency order.
(import maps)
(import lcg)
(import noise)

View File

@@ -10,6 +10,9 @@
;;; certainly air above.
(include stdio.h stdlib.h string.h math.h)
;; TODO sex#58: import only the modules used here once an import brings
;; the types its interface needs; until then, every dependency, in
;; dependency order.
(import maps)
(import lcg)
(import noise)
@@ -21,6 +24,8 @@
;;; NewSimplexNoiseLayer --------------------------------------------
;; TODO sex#47: write these in hex, as the C# does, once there are hex
;; literals.
(define PRIME-X 5910200641878280303)
(define PRIME-Y 6452764530575939509)
(define PRIME-Z 6614699811220273867)
@@ -140,6 +145,8 @@
(var xf1 float (- (* (cast (- -2 (* xn 4)) float) x1) one))
(var yf1 float (- (* (cast (- -2 (* yn 4)) float) y1) one))
(var zf1 float (- (* (cast (- -2 (* zn 4)) float) z1) one))
;; TODO sex#62: cast (c-bit-or xn 1) to float directly once a cast of
;; a bitwise expression keeps its parentheses.
(var xsi int (c-bit-or xn 1))
(var ysi int (c-bit-or yn 1))
(var zsi int (c-bit-or zn 1))
@@ -278,6 +285,7 @@
;;; *tanh* curve. GenTerra turns its threshold into a noise value with
;;; this one, though the column bounds use the new noise's own curve.
(fn tanh-curve-inverse ((v double)) double
;; TODO sex#63: write -INFINITY once it reads as a negation.
(if (<= v 0.0) (return (- INFINITY)))
(if (>= v 1.0) (return INFINITY))
(return (* 0.5 (log (/ v (- 1.0 v))))))
@@ -672,6 +680,8 @@
(var oct-a [double 64])
(var oct-t [double 64])
(for (var k int 0) (< k 4) (++ k)
;; TODO sex#59: put the (if ...)s in the sums below once a
;; conditional operand keeps its parentheses.
(var ox float (if (& k 1) pixel 0))
(var oz float (if (& k 2) pixel 0))
(landform-weights/at (& (. t weights)) (+ base-x ox) (+ base-z oz) w n)

View File

@@ -13,6 +13,9 @@
(pub include pthread.h SDL3/SDL.h)
(include stdio.h stdlib.h string.h math.h)
;; TODO sex#58: import only the modules used here once an import brings
;; the types its interface needs; until then, every dependency, in
;; dependency order.
(import json)
(import maps)
(import lcg)
@@ -59,16 +62,22 @@
(pub record tiles
((lock pthread-mutex-t) (wake pthread-cond-t)
;; TODO sex#50: write MAX-TILES once constants in array types are
;; kebab-converted.
(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) (rivers river-config) (assets-dir [char 1024]) (generation int)
;; TODO sex#60: size with MAX-WORKERS once arrays of a header type
;; take a symbolic size.
(workers [pthread-t 32]) (worker-count int) (quit bool) (frame u64) (waiting int)
(palette [u32 256])))
(fn key-hash ((k tile-key)) u64
;; one-to-one: region x and z 16 bits each, map 4, stratum or
;; resolution 10, param 18
;; TODO sex#47: write the field masks in hex once there are hex
;; literals.
(return (c-bit-or (cast (& (. k rx) 65535) u64)
(<< (cast (& (. k rz) 65535) u64) 16)
(<< (cast (& (. k map) 15) u64) 32)
@@ -77,6 +86,8 @@
;;; The tile index is a cell-map keyed by (x, z): the hash is split
;;; over the two halves.
;; TODO sex#47: write the low-half masks below in hex once there are hex
;; literals.
(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))))
@@ -93,6 +104,8 @@
;;; A distinct, stable colour per landform index.
(fn landform-colour ((i u32)) u32
;; TODO sex#47: write the multiplier (0x9E3779B1) and masks in hex
;; once there are hex literals.
(var h u32 (* (+ i 1) (cast 2654435761 u32)))
(return (c-bit-or (<< (+ 48 (& (>> h 24) 191)) 16)
(<< (+ 48 (& (>> h 16) 191)) 8)
@@ -107,6 +120,8 @@
(return r))
(fn colour-of ((g (* const worldgen)) (map int) (v int)) u32
;; TODO sex#47: write the colours and masks in hex once there are hex
;; literals.
(switch map
;; landforms.json gives most landforms the same green, so these
;; are coloured by index instead
@@ -129,6 +144,8 @@
(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))])
;; TODO sex#47: write the opaque alpha in hex once there are hex
;; literals.
(= [px (+ (* z inner) x)] (c-bit-or 4278190080 (colour-of g (. k map) v)))))
(free (. m data))
(= (* size) inner)
@@ -170,6 +187,7 @@
(= (. c segments) (realloc (. c segments) (* (. c segment-cap) (sizeof int))))))
(return p))
;; TODO sex#47: write the colours in hex once there are hex literals.
(fn river-colour ((distance int) (valley int) (ocean bool)) u32
(if ocean (return 1784427))
(if (== distance 0) (return 3043288))
@@ -202,15 +220,22 @@
(var ox int (+ (* cx (/ 32 step)) dx))
(var oz int (+ (* cz (/ 32 step)) dz))
(= [px (+ (* oz res) ox)]
;; TODO sex#47: write the opaque alpha in hex once there
;; are hex literals.
(c-bit-or 4278190080 (river-colour d valley (river-plate/ocean-at p x z))))))))
(= (* size) res)
(return px))
;;; Terrain height, shaded: water by depth under sea level, land by
;;; height, lit from the north-west.
;; TODO sex#47: write the colours in hex once there are hex literals.
(fn height-colour ((h int) (west int) (north int) (sea int) (size-y int)) u32
;; TODO sex#59: clamp the depth inline once a conditional operand
;; keeps its parentheses.
(var depth int (- sea h))
(if (> depth 40) (= depth 40))
(if (< h sea)
(return (lerp-colour 6400472 1186659 (/ (* (if (> (- sea h) 40) 40 (- sea h)) 255) 40))))
(return (lerp-colour 6400472 1186659 (/ (* depth 255) 40))))
(var t int (/ (* (- h sea) 255) (- size-y sea)))
(var base u32 (if (< t 32) (lerp-colour 5605944 8299066 (* t 8))
(if (< t 96) (lerp-colour 8299066 9922899 (/ (* (- t 32) 255) 64))
@@ -247,6 +272,8 @@
(var west int (if (> x 0) [hs (+ (* z res) x -1)] h))
(var north int (if (> z 0) [hs (+ (* (- z 1) res) x)] h))
(= [px (+ (* z res) x)]
;; TODO sex#47: write the opaque alpha in hex once there are
;; hex literals.
(c-bit-or 4278190080 (height-colour h west north (. tr sea-level) (. tr size-y))))))
(free hs)
(= (* size) res)

View File

@@ -3,6 +3,9 @@
;;; map matches to the pixel, padding included.
(include stdio.h stdlib.h string.h time.h)
;; TODO sex#58: import only the modules used here once an import brings
;; the types its interface needs; until then, every dependency, in
;; dependency order.
(import maps)
(import lcg)
(import noise)
@@ -215,12 +218,19 @@
(if (== d 0) (++ matched))
(if (!= d 0)
(do
;; TODO sex#59: sum two (if ...)s once a conditional
;; operand keeps its parentheses.
(var cls int 0)
(if (< d 0) (+= cls 2))
(if (>= got (. t sea-level)) (+= cls 1))
(++ [by-class cls])))
;; -4..+4, the ends counting everything beyond
(var bucket int (+ (if (< d -4) -4 (if (> d 4) 4 d)) 4))
;; TODO sex#59: clamp inline once a conditional operand keeps its
;; parentheses.
(var clamped int d)
(if (< clamped -4) (= clamped -4))
(if (> clamped 4) (= clamped 4))
(var bucket int (+ clamped 4))
(++ [by-delta bucket])
(var colour u32 (grey (/ (* got 255) (. g settings size-y))))
(if (> d 0) (= colour 16719904))

View File

@@ -11,6 +11,9 @@
;;; quits, which is how the viewer is checked without looking at it.
(include stdio.h stdlib.h string.h math.h unistd.h "nk.h" "stb_image_write.h")
;; TODO sex#58: import only the modules used here once an import brings
;; the types its interface needs; until then, every dependency, in
;; dependency order.
(import cs)
(import json)
(import maps)
@@ -57,6 +60,7 @@
#("rock-granite" "rock-andesite" "rock-basalt" "rock-peridotite" "rock-kimberlite"
"rock-claystone" "rock-sandstone" "rock-shale" "rock-conglomerate" "rock-limestone"
"rock-chert" "rock-chalk" "rock-bauxite" "rock-slate" "rock-phyllite"))
;; TODO sex#47: write the colours in hex once there are hex literals.
(var rock-colour-values [u32 15]
;; Sex has no hex literals: B49A8C 6E6A66 3A3A3D 4F6B3A 5A6E7A C08A5A D8C38A
;; 5E6470 9A7B5C C9C7B8 8A5A48 F0F0E8 B5532E 4A5568 7D8A7A
@@ -188,6 +192,8 @@
(for (var k int 0) (< k 15) (++ k)
(if (== (strcmp code [rock-colour-codes k]) 0)
(return [rock-colour-values k])))
;; TODO sex#47: write the multiplier and mask in hex once there are
;; hex literals.
(var hash u32 (* (cast (+ i 41) u32) (cast 2654435761 u32)))
(return (& hash 16777215)))
@@ -196,6 +202,8 @@
(fn update-palette ((a (* app))) void
(var p [u32 256])
(for (var i int 0) (< i 256) (++ i)
;; TODO sex#47: write the palette colours and alpha in hex once
;; there are hex literals.
(= [p i] 4280295456))
;; the base layer shows through water, air and the rocks not looked for
(= [p VALUE-WATER] 0)
@@ -696,6 +704,8 @@
(do
(var value int (. a rock-value))
(if (== (. a rock-mode) ROCK-AT-SURFACE) (= value 0))
;; TODO sex#45: use (. a rock-mode) in the literal once member
;; access inside a compound literal follows the pointer.
(var mode int (. a rock-mode))
(hover-worker/ask (. a hover-worker) #(hover-query : bx bz mode value))))
(= (. a hover) (hover-worker/result (. a hover-worker))))

View File

@@ -4,6 +4,9 @@
(include stdio.h string.h)
;; An import brings in a module's own public forms but not the types
;; they mention, so dependencies come first.
;; TODO sex#58: import only the modules used here once an import brings
;; the types its interface needs; until then, every dependency, in
;; dependency order.
(import cs)
(import json)
(import maps)

View File

@@ -80,6 +80,8 @@
;;; NoiseGeoProvince plus MapLayerGeoProvince: a weighted random
;;; province per 64x64 cell of province-map pixels, sampled through a
;;; wobble so the cells don't show.
;; TODO sex#50: write MAX-PROVINCES once constants in array types are
;; kebab-converted.
(pub record province-layer
((rng lcg) (weights [int MAX_PROVINCES]) (count int) (weight-sum int)
(codes [* char MAX_PROVINCES]) (colours [u32 MAX_PROVINCES])
@@ -165,7 +167,10 @@
(= [map (+ start (* i stride))] (cast [tmp i] i32))))
(fn box-blur ((map (* i32)) (w int) (h int) (range int)) void
(var tmp (* u32) (malloc (* (if (> w h) w h) (sizeof u32))))
;; TODO sex#59: inline the longer side once a conditional operand
;; keeps its parentheses.
(var longest int (if (> w h) w h))
(var tmp (* u32) (malloc (* longest (sizeof u32))))
(for (var y int 0) (< y h) (++ y)
(blur-line map (* y w) 1 w range tmp))
(for (var x int 0) (< x w) (++ x)
@@ -385,6 +390,8 @@
(= (. l octave-threshold-count) 11)))
(= (. l first-mutation) -1))
;; TODO sex#50: write MAX-LANDFORMS once constants in array types are
;; kebab-converted.
(pub record landform-table ((all [landform MAX_LANDFORMS]) (count int) (variant-count int)))
(pub fn terrain-octave-count ((size-y int)) int
@@ -445,6 +452,8 @@
(return (|| (!= (- rain clamped-rain) 0) (!= (- t clamped-temp) 0))))
;;; MapLayerLandforms over NoiseLandforms.
;; TODO sex#50: write MAX-LANDFORMS once constants in array types are
;; kebab-converted.
(pub record landform-layer
((rng lcg) (table (* const landform-table)) (climate climate-noise) (scale float)
(weights [double MAX_LANDFORMS])
@@ -715,6 +724,8 @@
;;; MapLayerCustomPerlin per stratum of rockstrata.json: how thick
;;; that stratum may be, in blocks, per 16 blocks of region.
;; TODO sex#50: write MAX-STRATA once constants in array types are
;; kebab-converted.
(pub record strata-layer
((count int) (noises [* simplex-sum MAX_STRATA]) (thresholds [* double MAX_STRATA])
(codes [* char MAX_STRATA])))
@@ -733,6 +744,8 @@
(memset (& l) 0 (sizeof l))
(var variants (* cJSON) (cJSON-GetObjectItem rockstrata "variants"))
(for (var v (* cJSON) (. variants child)) (&& v (< (. l count) MAX-STRATA)) (= v (. v next))
;; TODO sex#50: write MAX-OCTAVES once constants in array types
;; are kebab-converted.
(var amplitudes [double MAX_OCTAVES])
(var frequencies [double MAX_OCTAVES])
(var th (* double) (calloc MAX-OCTAVES (sizeof double)))
@@ -814,6 +827,8 @@
(= base-z (. parent z))
(= dir-x (. parent dir-x)))
(continue))
;; TODO sex#59: put the (if ...) in the product below once a
;; conditional operand keeps its parentheses.
(var dir-z int (if (> (.next-float (& rng)) 0.5) -1 1))
(var dist-x int (+ min-x (.next-int (& rng) (- (+ max-x 1) min-x))))
(var dist-z int (+ min-z (.next-int (& rng) (- (+ max-z 1) min-z))))