lay rock strata and show them as an overlay

The surface rock matches the save's TopRockIdMap in all but 14 of
11.7 million columns. Rock tiles keep rock indices and take their
colours from a palette on upload, so highlighting one rock, to find
where it lies, recolours what is drawn instead of regenerating it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-08 19:53:26 +03:00
parent c4c7ab9968
commit 4666188b21
8 changed files with 620 additions and 47 deletions

View File

@@ -22,12 +22,20 @@
(import fastnoise)
(import rivers)
(import terra)
(import strata)
(pub define MAX-TILES 16384)
;;; Not worldgen maps: Rivers' distance to the nearest river, and the
;;; terrain height.
;;; Not worldgen maps: Rivers' distance to the nearest river, the
;;; terrain height, and the rock at the surface, at a depth or at a Y.
(pub define LAYER-RIVERS 6)
(pub define LAYER-HEIGHT 7)
(pub define LAYER-ROCK 8)
(pub define ROCK-AT-SURFACE 0)
(pub define ROCK-AT-DEPTH 1)
(pub define ROCK-AT-Y 2)
;;; Rock tile values besides rock indices.
(pub define VALUE-WATER 253)
(pub define VALUE-AIR 254)
;;; 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.
@@ -39,10 +47,14 @@
(define TILE-READY 3)
(define TILE-DRAWN 4)
(pub record tile-key ((map int) (stratum int) (rx int) (rz int)))
;;; STRATUM is the stratum for MAP-STRATA, the samples per region side
;;; for the sampled layers; PARAM is the rock layer's mode * 1024 + value.
(pub record tile-key ((map int) (stratum int) (rx int) (rz int) (param int)))
;;; Rock tiles keep their VALUES, rock indices, and are coloured from
;;; the palette on upload, so a new palette only re-uploads them.
(pub record tile
((key tile-key) (state int) (generation int) (size int) (pixels (* u32))
((key tile-key) (state int) (generation int) (size int) (pixels (* u32)) (values (* u8))
(texture (* SDL-Texture)) (last-used u64)))
(pub record tiles
@@ -51,13 +63,17 @@
(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)
(workers [pthread-t 32]) (worker-count int) (quit bool) (frame u64) (waiting int)))
(workers [pthread-t 32]) (worker-count int) (quit bool) (frame u64) (waiting int)
(palette [u32 256])))
(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))))
;; one-to-one: region x and z 16 bits each, map 4, stratum or
;; resolution 10, param 18
(return (c-bit-or (cast (& (. k rx) 65535) u64)
(<< (cast (& (. k rz) 65535) u64) 16)
(<< (cast (& (. k map) 15) u64) 32)
(<< (cast (& (. k stratum) 1023) u64) 36)
(<< (cast (& (. k param) 262143) u64) 46))))
;;; The tile index is a cell-map keyed by (x, z): the hash is split
;;; over the two halves.
@@ -236,6 +252,43 @@
(= (* size) res)
(return px))
;;; The rock tile: per sample, the rock at the surface, at a depth
;;; below it, or at an absolute Y (water or air above the surface).
(fn render-rocks ((tr (* terra)) (rocks (* strata)) (k tile-key) (size (* int))) (* u8)
(var res int (. k stratum))
(var step int (/ 512 res))
(var mode int (/ (. k param) 1024))
(var value int (% (. k param) 1024))
(var out (* u8) (malloc (* res res)))
(var heights [u16 1024])
(var column (* u8) (malloc (+ (. tr size-y) 1)))
(var per-chunk int (/ 32 step))
(if (< per-chunk 1) (= per-chunk 1))
(for (var cz int 0) (< cz 16) (++ cz)
(for (var cx int 0) (< cx 16) (++ cx)
(if (|| (!= (% (* cx 32) step) 0) (!= (% (* cz 32) step) 0))
(continue))
(var chunk-x int (+ (* (. k rx) 16) cx))
(var chunk-z int (+ (* (. k rz) 16) cz))
(terra/chunk-heights tr chunk-x chunk-z (if (> step 32) 32 step) heights)
(for (var dz int 0) (< dz per-chunk) (++ dz)
(for (var dx int 0) (< dx per-chunk) (++ dx)
(var lx int (* dx step))
(var lz int (* dz step))
(var h int [heights (+ (* lz 32) lx)])
(strata/column rocks chunk-x chunk-z lx lz h column)
(var y int h)
(if (== mode ROCK-AT-DEPTH) (= y (- h value))
(== mode ROCK-AT-Y) (= y value))
(var v u8 VALUE-AIR)
(if (< y 0) (= v ROCK-MANTLE)
(<= y h) (= v [column y])
(< y (. tr sea-level)) (= v VALUE-WATER))
(= [out (+ (* (+ (/ (* cz 32) step) dz) res) (/ (* cx 32) step) dx)] v)))))
(free column)
(= (* size) res)
(return out))
;;; Workers -------------------------------------------------------
(fn worker-main ((arg (* void))) (* void)
@@ -245,6 +298,7 @@
(var cache river-cache)
(memset (& cache) 0 (sizeof cache))
(var tr (* terra) NULL)
(var rocks (* strata) NULL)
(pthread-mutex-lock (& (. t lock)))
(while (! (. t quit))
(if (>= (. t queue-pos) (. t queue-len))
@@ -267,32 +321,41 @@
(do
(if tr (terra/free tr))
(= tr NULL)
(if rocks (strata/free rocks))
(= rocks NULL)
(if g (worldgen/free g))
(release-river-cache (& cache))
(= g (worldgen/make settings (. t assets-dir)))
(if g (= tr (terra/make g rivers)))
(if g (= rocks (strata/make g (. t assets-dir))))
(= (. cache sampler) (river-sampler/make (& rivers) (. settings size-y)))
(= g-generation generation)))
(var size int 0)
(var px (* u32) NULL)
(var values (* u8) NULL)
(if (&& g (== (. key map) LAYER-RIVERS))
(= px (render-rivers g (& cache) (& rivers) key (& size)))
(&& tr (== (. key map) LAYER-HEIGHT))
(= px (render-heights tr key (& size)))
(&& tr rocks (== (. key map) LAYER-ROCK))
(= values (render-rocks tr rocks key (& size)))
g
(= px (render g key (& size))))
(pthread-mutex-lock (& (. t lock)))
(if (&& px (== generation (. t generation)) (== (. tl state) TILE-WORKING))
(if (&& (|| px values) (== generation (. t generation)) (== (. tl state) TILE-WORKING))
(do
(= (. tl pixels) px)
(= (. tl values) values)
(= (. tl size) size)
(= (. tl state) TILE-READY))
(do
(free px)
(free values)
(if (&& (== generation (. t generation)) (== (. tl state) TILE-WORKING))
(= (. tl state) TILE-QUEUED)))))
(pthread-mutex-unlock (& (. t lock)))
(if rocks (strata/free rocks))
(if tr (terra/free tr))
(if g (worldgen/free g))
(release-river-cache (& cache))
@@ -325,6 +388,7 @@
(if (. tl texture)
(SDL-DestroyTexture (. tl texture)))
(free (. tl pixels))
(free (. tl values))
(memset tl 0 (sizeof tile)))
(.release (& (. self index)))
(= (. self index) (cell-map/make))
@@ -364,6 +428,7 @@
(if (. tl texture)
(SDL-DestroyTexture (. tl texture)))
(free (. tl pixels))
(free (. tl values))
(memset tl 0 (sizeof tile))
(return best))
@@ -391,6 +456,12 @@
(= (. tl last-used) (. self frame))
(if (== (. tl state) TILE-READY)
(do
(if (. tl values)
(do
(var n int (* (. tl size) (. tl size)))
(= (. tl pixels) (malloc (* n (sizeof u32))))
(for (var i int 0) (< i n) (++ i)
(= [(. tl pixels) i] [(. self palette) [(. tl values) i]]))))
(= (. tl texture) (SDL-CreateTexture renderer SDL-PIXELFORMAT-ARGB8888
SDL-TEXTUREACCESS-STATIC (. tl size) (. tl size)))
(SDL-SetTextureScaleMode (. tl texture) SDL-SCALEMODE-NEAREST)
@@ -407,6 +478,20 @@
(++ (. self queue-len))))
(return NULL))
;; A new palette for value tiles: drawn ones are coloured again from
;; the values they kept. Main thread only.
(pub fn set-palette ((self (* tiles)) (palette (* const u32))) void
(pthread-mutex-lock (& (. self lock)))
(memcpy (. self palette) palette (sizeof (. self palette)))
(for (var i int 0) (< i MAX-TILES) (++ i)
(var tl (* tile) (& [(. self slots) i]))
(if (&& (. tl values) (== (. tl state) TILE-DRAWN))
(do
(SDL-DestroyTexture (. tl texture))
(= (. tl texture) NULL)
(= (. tl state) TILE-READY))))
(pthread-mutex-unlock (& (. self lock))))
;; 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