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