rework the viewer around base layers, overlays and side panels
One base layer is drawn opaque and any number of overlays over it, so rocks can be looked for on top of the terrain rather than instead of it. Settings and rock finding live in fixed panels beside the map, not in windows over it. What is under the cursor is worked out on a thread of its own, since computing it per frame stalled the window. Find nearest searches regions outward from the view's centre and stops once no nearer region is left; probes leave pins. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2
Makefile
2
Makefile
@@ -37,7 +37,7 @@ VSMV_OBJ = vsmv cs pb json maps save img dump lcg noise shape worldgen fastnoise
|
|||||||
$(B)/vsmv: $(VSMV_OBJ:%=$(B)/%.o)
|
$(B)/vsmv: $(VSMV_OBJ:%=$(B)/%.o)
|
||||||
$(CC) $^ -o $@ -lsqlite3
|
$(CC) $^ -o $@ -lsqlite3
|
||||||
|
|
||||||
VIEWER_OBJ = viewer tiles settings worldgen fastnoise rivers terra strata shape noise lcg maps save pb json cs nk cJSON stb
|
VIEWER_OBJ = viewer tiles search hover settings worldgen fastnoise rivers terra strata shape noise lcg maps save pb json cs nk cJSON stb
|
||||||
|
|
||||||
$(B)/vs-map-viewer: $(VIEWER_OBJ:%=$(B)/%.o)
|
$(B)/vs-map-viewer: $(VIEWER_OBJ:%=$(B)/%.o)
|
||||||
$(CC) $^ -o $@ $(SDL_LIBS) -lsqlite3
|
$(CC) $^ -o $@ $(SDL_LIBS) -lsqlite3
|
||||||
|
|||||||
80
Plan.org
80
Plan.org
@@ -485,6 +485,86 @@ Viewer:
|
|||||||
- *Tile keys.* Now injective bit fields. The old =map * 64 + stratum=
|
- *Tile keys.* Now injective bit fields. The old =map * 64 + stratum=
|
||||||
packing could alias once resolutions passed 64.
|
packing could alias once resolutions passed 64.
|
||||||
|
|
||||||
|
** Interface pass (after phase 6)
|
||||||
|
- *Multi-select.* The legend highlights any number of rocks; Show all
|
||||||
|
clears it.
|
||||||
|
- *Coverage.* Each legend entry shows its share of the visible map,
|
||||||
|
counted from the drawn rock tiles' values.
|
||||||
|
- *Find nearest* (=src/search.sex=).
|
||||||
|
- Order: regions nearest the view centre first.
|
||||||
|
- Sampling: every 16 blocks, on cores - 1 threads, each with its own
|
||||||
|
generator.
|
||||||
|
- Stopping: once the next region can't hold anything nearer.
|
||||||
|
- Refinement: block by block in the 3 x 3 chunks around the hit.
|
||||||
|
- Range: 24 regions, about 12,000 blocks.
|
||||||
|
- Result: the view jumps there, and the column is pinned and probed.
|
||||||
|
- Caveat: a patch smaller than the 16-block sampling can be missed.
|
||||||
|
- *Pins.* Every probe drops a numbered pin; clicking a pin probes it
|
||||||
|
again. The Probe window has Go to and Clear pins.
|
||||||
|
- *Info panel.* Wider, one fact per line.
|
||||||
|
- *Font.* A system monospace font with Cyrillic: Menlo, or DejaVu Sans
|
||||||
|
Mono on Linux. Nuklear's built-in font is ASCII only, so a Cyrillic
|
||||||
|
seed read from =serverconfig.json= showed as =??????=.
|
||||||
|
- *Layout.* Panels are sized from the window at start; Probe sits left
|
||||||
|
of the Layer panel.
|
||||||
|
- *Command line.* =--find= and repeated =--highlight= for screenshot
|
||||||
|
tests.
|
||||||
|
|
||||||
|
** Layers and Rocks window
|
||||||
|
- *Base.* One base layer, opaque: Ocean, Rivers, or Terrain height (the
|
||||||
|
default).
|
||||||
|
- *Overlays.* Any number, drawn over the base with one opacity slider
|
||||||
|
(default 50%): Landform, Climate, Geologic province, Upheaval, Rock
|
||||||
|
strata thickness (with its stratum picker), Rock.
|
||||||
|
- *Draw and queue order.* Layer by layer, base first; each layer's
|
||||||
|
tiles nearest the centre first.
|
||||||
|
- *Rocks window.* Always shown: mode, depth or Y, the legend with
|
||||||
|
highlights, Find nearest. Coverage percentages appear while the Rock
|
||||||
|
overlay is on.
|
||||||
|
- *Rock overlay transparency.* Water, air, and every rock not
|
||||||
|
highlighted are transparent, so the base shows through.
|
||||||
|
- *Ticks.* Checkboxes and options get an accent; Nuklear's default
|
||||||
|
draws a ticked box darker, as if empty.
|
||||||
|
- *Tile cache.* Holds 65,536 tiles, since every region now has several.
|
||||||
|
- *Command line.* =--base=, =--overlay= (repeatable) and =--opacity=
|
||||||
|
replace =--layer=.
|
||||||
|
|
||||||
|
** Side panels
|
||||||
|
- *Layout.* Two opaque panels, pinned to the window's edges every frame:
|
||||||
|
- left, 310 points: World, Info and Probe;
|
||||||
|
- right, 300 points: Layers and Rocks.
|
||||||
|
Each section is a collapsible Nuklear tree tab.
|
||||||
|
- *Map viewport.* The map is the rectangle between the panels. All
|
||||||
|
world/window conversions, drawing (clipped), zoom-at-cursor, the
|
||||||
|
visible tile range and the coverage counts use it, so the panels hide
|
||||||
|
nothing and coverage counts only what is on screen.
|
||||||
|
- *Default window.* 1600 x 960.
|
||||||
|
- *Story locations checkbox.* Toggles the spawn and story markers; off
|
||||||
|
by default.
|
||||||
|
- *Rock under cursor checkbox.* A label beside the cursor naming the
|
||||||
|
rock there in the Rocks mode (surface, depth or Y), or water, air or
|
||||||
|
mantle.
|
||||||
|
|
||||||
|
** Hover worker
|
||||||
|
- *The lag.* Info and the cursor label used to compute on the UI
|
||||||
|
thread: a whole chunk of heights (about 11 ms) per new chunk, region
|
||||||
|
maps per new region, and a river plate (80 ms) per new plate. Moving
|
||||||
|
the cursor stalled frames, and the label lagged behind the cursor.
|
||||||
|
- *The worker* (=src/hover.sex=). A thread with its own generators
|
||||||
|
answers "what is at this block". The answer covers:
|
||||||
|
- height;
|
||||||
|
- the rock in the Rocks mode, and the surface rock;
|
||||||
|
- landform, province, climate, ocean, upheaval;
|
||||||
|
- river distance and bank depth.
|
||||||
|
- *Newest only.* The viewer posts the cursor's block every frame. The
|
||||||
|
worker takes the newest and drops stale requests; the viewer shows
|
||||||
|
its newest answer.
|
||||||
|
- *The label* moves with the cursor every frame, whatever the answer's
|
||||||
|
age. While the worker owes an answer, the viewer keeps drawing.
|
||||||
|
- *Single columns.* =terra/column-height= computes one column (about
|
||||||
|
11 us). Probes on the main thread use it too.
|
||||||
|
- *Test flag.* =--mouse X Y= places the cursor for screenshot tests.
|
||||||
|
|
||||||
** 7. Performance + polish
|
** 7. Performance + polish
|
||||||
- Level of detail:
|
- Level of detail:
|
||||||
- zoomed out: one column per N blocks;
|
- zoomed out: one column per N blocks;
|
||||||
|
|||||||
207
src/hover.sex
Normal file
207
src/hover.sex
Normal file
@@ -0,0 +1,207 @@
|
|||||||
|
;;; What lies at the block under the cursor, worked out on a thread of
|
||||||
|
;;; its own so the window never waits for it.
|
||||||
|
;;;
|
||||||
|
;;; The viewer posts the block it is over every frame; the worker always
|
||||||
|
;;; takes the newest one and drops any it had no time for. The viewer
|
||||||
|
;;; shows the newest result, which may be a frame or two behind the
|
||||||
|
;;; cursor. Moving to a new region or river plate costs the worker
|
||||||
|
;;; milliseconds, which used to be the window's.
|
||||||
|
|
||||||
|
(pub include pthread.h)
|
||||||
|
(include stdio.h stdlib.h string.h math.h)
|
||||||
|
(import maps)
|
||||||
|
(import lcg)
|
||||||
|
(import noise)
|
||||||
|
(import shape)
|
||||||
|
(import json)
|
||||||
|
(import worldgen)
|
||||||
|
(import fastnoise)
|
||||||
|
(import rivers)
|
||||||
|
(import terra)
|
||||||
|
(import strata)
|
||||||
|
|
||||||
|
(pub record hover-query ((x int) (z int) (mode int) (value int)))
|
||||||
|
|
||||||
|
(pub record hover-result
|
||||||
|
((valid bool) (query hover-query)
|
||||||
|
(height int) (sea-level int)
|
||||||
|
;; rock index at the query's surface, depth or Y; -1 when the block
|
||||||
|
;; there is water or air, -2 below the mantle
|
||||||
|
(rock int) (rock-y int) (surface-rock int)
|
||||||
|
(landform int) (province int) (climate int) (ocean int) (upheaval int)
|
||||||
|
(river-distance double) (bank double)))
|
||||||
|
|
||||||
|
(pub record hover-worker
|
||||||
|
((lock pthread-mutex-t) (wake pthread-cond-t) (thread pthread-t) (quit bool)
|
||||||
|
(settings world-settings) (rivers river-config) (assets-dir [char 1024]) (generation int)
|
||||||
|
(query hover-query) (pending bool) (working bool) (result hover-result)))
|
||||||
|
|
||||||
|
(fn sample-map ((m (* const int-map)) (x int) (z int)) int
|
||||||
|
(var inner int (- (. m size) (. m pad-tl) (. m pad-br)))
|
||||||
|
(var px int (+ (. m pad-tl) (/ (* (% x 512) inner) 512)))
|
||||||
|
(var pz int (+ (. m pad-tl) (/ (* (% z 512) inner) 512)))
|
||||||
|
(return [(. m data) (+ (* pz (. m size)) px)]))
|
||||||
|
|
||||||
|
;;; The worker's generators and the region and plate it has at hand.
|
||||||
|
(record kit
|
||||||
|
((g (* worldgen)) (tr (* terra)) (rocks (* strata)) (generation int)
|
||||||
|
(rx int) (rz int) (maps [int-map 5]) (has-region bool)
|
||||||
|
(plate (* river-plate)) (segments (* int)) (sampler river-sampler)
|
||||||
|
(column (* u8))))
|
||||||
|
|
||||||
|
(var region-kinds [int 5] #(0 2 1 4 3))
|
||||||
|
|
||||||
|
(fn release-kit ((k (* kit))) void
|
||||||
|
(if (. k has-region)
|
||||||
|
(for (var i int 0) (< i 5) (++ i)
|
||||||
|
(free (. [(. k maps) i] data))))
|
||||||
|
(if (. k plate) (river-plate/free (. k plate)))
|
||||||
|
(if (. k rocks) (strata/free (. k rocks)))
|
||||||
|
(if (. k tr) (terra/free (. k tr)))
|
||||||
|
(if (. k g) (worldgen/free (. k g)))
|
||||||
|
(free (. k segments))
|
||||||
|
(free (. k column))
|
||||||
|
(memset k 0 (sizeof kit)))
|
||||||
|
|
||||||
|
(fn compute ((w (* hover-worker)) (k (* kit)) (q hover-query) (r (* hover-result))) void
|
||||||
|
(memset r 0 (sizeof hover-result))
|
||||||
|
(= (. r query) q)
|
||||||
|
(var x int (. q x))
|
||||||
|
(var z int (. q z))
|
||||||
|
(var rx int (/ x 512))
|
||||||
|
(var rz int (/ z 512))
|
||||||
|
(if (|| (! (. k has-region)) (!= (. k rx) rx) (!= (. k rz) rz))
|
||||||
|
(do
|
||||||
|
(if (. k has-region)
|
||||||
|
(for (var i int 0) (< i 5) (++ i)
|
||||||
|
(free (. [(. k maps) i] data))))
|
||||||
|
(for (var i int 0) (< i 5) (++ i)
|
||||||
|
(worldgen/map (. k g) [region-kinds i] 0 rx rz (& [(. k maps) i])))
|
||||||
|
(= (. k rx) rx)
|
||||||
|
(= (. k rz) rz)
|
||||||
|
(= (. k has-region) true)))
|
||||||
|
(= (. r landform) (sample-map (& [(. k maps) 0]) x z))
|
||||||
|
(= (. r province) (sample-map (& [(. k maps) 1]) x z))
|
||||||
|
(= (. r climate) (sample-map (& [(. k maps) 2]) x z))
|
||||||
|
(= (. r ocean) (sample-map (& [(. k maps) 3]) x z))
|
||||||
|
(= (. r upheaval) (sample-map (& [(. k maps) 4]) x z))
|
||||||
|
|
||||||
|
(var per-plate int (river-plate/chunks (& (. w rivers))))
|
||||||
|
(var p (* river-plate) (. k plate))
|
||||||
|
(if (|| (! p) (!= (. p px) (/ (/ x 32) per-plate)) (!= (. p pz) (/ (/ z 32) per-plate)))
|
||||||
|
(do
|
||||||
|
(if p (river-plate/free p))
|
||||||
|
(= p (river-plate/make (. w rivers) (& (. (. k g) oceans)) (. w settings size-y)
|
||||||
|
(/ (/ x 32) per-plate) (/ (/ z 32) per-plate)))
|
||||||
|
(= (. k plate) p)
|
||||||
|
(= (. k segments) (realloc (. k segments) (* (+ (. p segment-count) 1) (sizeof int))))))
|
||||||
|
(var count int (river-plate/chunk-segments p (/ x 32) (/ z 32) (. k segments)))
|
||||||
|
(var sample river-sample (river-sampler/sample (& (. k sampler)) p (. k segments) count x z))
|
||||||
|
(= (. r river-distance) (. sample distance))
|
||||||
|
(= (. r bank) (. sample bank))
|
||||||
|
|
||||||
|
(var h int (terra/column-height (. k tr) x z))
|
||||||
|
(= (. r height) h)
|
||||||
|
(= (. r sea-level) (. (. k tr) sea-level))
|
||||||
|
(strata/column (. k rocks) (/ x 32) (/ z 32) (% x 32) (% z 32) h (. k column))
|
||||||
|
(= (. r surface-rock) [(. k column) h])
|
||||||
|
(var y int h)
|
||||||
|
(if (== (. q mode) 1) (= y (- h (. q value)))
|
||||||
|
(== (. q mode) 2) (= y (. q value)))
|
||||||
|
(= (. r rock-y) y)
|
||||||
|
(= (. r rock) -1)
|
||||||
|
(if (< y 1) (= (. r rock) -2)
|
||||||
|
(<= y h) (= (. r rock) [(. k column) y]))
|
||||||
|
(= (. r valid) true))
|
||||||
|
|
||||||
|
(fn worker-main ((arg (* void))) (* void)
|
||||||
|
(var w (* hover-worker) arg)
|
||||||
|
(var k kit)
|
||||||
|
(memset (& k) 0 (sizeof k))
|
||||||
|
(= (. k generation) -1)
|
||||||
|
(pthread-mutex-lock (& (. w lock)))
|
||||||
|
(while (! (. w quit))
|
||||||
|
(if (! (. w pending))
|
||||||
|
(do
|
||||||
|
(pthread-cond-wait (& (. w wake)) (& (. w lock)))
|
||||||
|
(continue)))
|
||||||
|
(var q hover-query (. w query))
|
||||||
|
(= (. w pending) false)
|
||||||
|
(= (. w working) true)
|
||||||
|
(var generation int (. w generation))
|
||||||
|
(var settings world-settings (. w settings))
|
||||||
|
(pthread-mutex-unlock (& (. w lock)))
|
||||||
|
|
||||||
|
(if (!= (. k generation) generation)
|
||||||
|
(do
|
||||||
|
(release-kit (& k))
|
||||||
|
(= (. k g) (worldgen/make settings (. w assets-dir)))
|
||||||
|
(if (. k g)
|
||||||
|
(do
|
||||||
|
(= (. k tr) (terra/make (. k g) (. w rivers)))
|
||||||
|
(= (. k rocks) (strata/make (. k g) (. w assets-dir)))
|
||||||
|
(= (. k sampler) (river-sampler/make (& (. w rivers)) (. settings size-y)))
|
||||||
|
(= (. k column) (malloc (+ (. settings size-y) 1)))))
|
||||||
|
(= (. k generation) generation)))
|
||||||
|
(var r hover-result)
|
||||||
|
(memset (& r) 0 (sizeof r))
|
||||||
|
(if (&& (. k g) (. k tr) (. k rocks))
|
||||||
|
(compute w (& k) q (& r)))
|
||||||
|
|
||||||
|
(pthread-mutex-lock (& (. w lock)))
|
||||||
|
(= (. w working) false)
|
||||||
|
(if (== generation (. w generation))
|
||||||
|
(= (. w result) r)))
|
||||||
|
(pthread-mutex-unlock (& (. w lock)))
|
||||||
|
(release-kit (& k))
|
||||||
|
(return NULL))
|
||||||
|
|
||||||
|
(pub fn hover-worker/start ((assets-dir (* const char))) (* hover-worker)
|
||||||
|
(var w (* hover-worker) (calloc 1 (sizeof hover-worker)))
|
||||||
|
(pthread-mutex-init (& (. w lock)) NULL)
|
||||||
|
(pthread-cond-init (& (. w wake)) NULL)
|
||||||
|
(snprintf (. w assets-dir) (sizeof (. w assets-dir)) "%s" assets-dir)
|
||||||
|
(pthread-create (& (. w thread)) NULL worker-main w)
|
||||||
|
(return w))
|
||||||
|
|
||||||
|
;;; New world settings: what was worked out no longer holds.
|
||||||
|
(pub fn hover-worker/set-world ((w (* hover-worker)) (settings world-settings) (rivers river-config)) void
|
||||||
|
(pthread-mutex-lock (& (. w lock)))
|
||||||
|
(= (. w settings) settings)
|
||||||
|
(= (. w rivers) rivers)
|
||||||
|
(++ (. w generation))
|
||||||
|
(= (. w result valid) false)
|
||||||
|
(pthread-mutex-unlock (& (. w lock))))
|
||||||
|
|
||||||
|
;;; Asks about Q, unless that is what was asked last.
|
||||||
|
(pub fn hover-worker/ask ((w (* hover-worker)) (q hover-query)) void
|
||||||
|
(pthread-mutex-lock (& (. w lock)))
|
||||||
|
(if (|| (!= (. w query x) (. q x)) (!= (. w query z) (. q z)) (!= (. w query mode) (. q mode))
|
||||||
|
(!= (. w query value) (. q value)) (! (. w result valid)))
|
||||||
|
(do
|
||||||
|
(= (. w query) q)
|
||||||
|
(= (. w pending) true)
|
||||||
|
(pthread-cond-signal (& (. w wake)))))
|
||||||
|
(pthread-mutex-unlock (& (. w lock))))
|
||||||
|
|
||||||
|
;;; The newest result, valid or not.
|
||||||
|
(pub fn hover-worker/result ((w (* hover-worker))) hover-result
|
||||||
|
(pthread-mutex-lock (& (. w lock)))
|
||||||
|
(var r hover-result (. w result))
|
||||||
|
(pthread-mutex-unlock (& (. w lock)))
|
||||||
|
(return r))
|
||||||
|
|
||||||
|
;;; Whether the worker owes an answer, for the viewer to keep drawing.
|
||||||
|
(pub fn hover-worker/busy ((w (* hover-worker))) bool
|
||||||
|
(pthread-mutex-lock (& (. w lock)))
|
||||||
|
(var busy bool (|| (. w pending) (. w working)))
|
||||||
|
(pthread-mutex-unlock (& (. w lock)))
|
||||||
|
(return busy))
|
||||||
|
|
||||||
|
(pub fn hover-worker/stop ((w (* hover-worker))) void
|
||||||
|
(pthread-mutex-lock (& (. w lock)))
|
||||||
|
(= (. w quit) true)
|
||||||
|
(pthread-cond-signal (& (. w wake)))
|
||||||
|
(pthread-mutex-unlock (& (. w lock)))
|
||||||
|
(pthread-join (. w thread) NULL)
|
||||||
|
(free w))
|
||||||
220
src/search.sex
Normal file
220
src/search.sex
Normal file
@@ -0,0 +1,220 @@
|
|||||||
|
;;; Finds the block nearest a point where one of a set of rocks lies, at
|
||||||
|
;;; the surface, at a depth below it or at a Y.
|
||||||
|
;;;
|
||||||
|
;;; Regions are taken in order of their distance from the point and
|
||||||
|
;;; sampled every 16 blocks by several threads at once, each with its
|
||||||
|
;;; own generator. A region can only hold a nearer hit than the best so
|
||||||
|
;;; far while its nearest edge is nearer, so the search stops as soon as
|
||||||
|
;;; the next region's isn't. The 16 block hit is then refined to the
|
||||||
|
;;; nearest block in the chunks around it.
|
||||||
|
|
||||||
|
(pub include pthread.h)
|
||||||
|
(include stdio.h stdlib.h string.h math.h)
|
||||||
|
(import maps)
|
||||||
|
(import lcg)
|
||||||
|
(import noise)
|
||||||
|
(import shape)
|
||||||
|
(import json)
|
||||||
|
(import worldgen)
|
||||||
|
(import fastnoise)
|
||||||
|
(import rivers)
|
||||||
|
(import terra)
|
||||||
|
(import strata)
|
||||||
|
|
||||||
|
(pub define SEARCH-RADIUS-REGIONS 24)
|
||||||
|
(define SEARCH-STEP 16)
|
||||||
|
(define MAX-SEARCH-THREADS 16)
|
||||||
|
|
||||||
|
(pub record search
|
||||||
|
((lock pthread-mutex-t) (threads [pthread-t 16]) (thread-count int) (running bool)
|
||||||
|
(cancel bool)
|
||||||
|
;; the question
|
||||||
|
(settings world-settings) (rivers river-config) (assets-dir [char 1024])
|
||||||
|
(mode int) (value int) (mask u32) (x double) (z double)
|
||||||
|
;; regions nearest first, and progress through them
|
||||||
|
(regions (* int)) (region-count int) (next int) (done-count int) (active int)
|
||||||
|
;; the answer
|
||||||
|
(found bool) (best-distance double) (best-x int) (best-z int)))
|
||||||
|
|
||||||
|
(record region-order ((rx int) (rz int) (distance double)))
|
||||||
|
|
||||||
|
(fn by-distance ((pa (* const void)) (pb (* const void))) int
|
||||||
|
(var a (* const region-order) pa)
|
||||||
|
(var b (* const region-order) pb)
|
||||||
|
(return (- (> (. a distance) (. b distance)) (< (. a distance) (. b distance)))))
|
||||||
|
|
||||||
|
(fn region-distance ((s (* const search)) (rx int) (rz int)) double
|
||||||
|
(var x0 double (* rx 512.0))
|
||||||
|
(var z0 double (* rz 512.0))
|
||||||
|
(var dx double (fmax 0 (fmax (- x0 (. s x)) (- (. s x) (+ x0 512)))))
|
||||||
|
(var dz double (fmax 0 (fmax (- z0 (. s z)) (- (. s z) (+ z0 512)))))
|
||||||
|
(return (sqrt (+ (* dx dx) (* dz dz)))))
|
||||||
|
|
||||||
|
;;; Rock index Y of the column holds in the searched mode, or -1 when
|
||||||
|
;;; the column has no block there (air, water, below the mantle).
|
||||||
|
(fn rock-at ((column (* const u8)) (height int) (mode int) (value int)) int
|
||||||
|
(var y int height)
|
||||||
|
(if (== mode 1) (= y (- height value))
|
||||||
|
(== mode 2) (= y value))
|
||||||
|
(if (|| (< y 1) (> y height))
|
||||||
|
(return -1))
|
||||||
|
(return [column y]))
|
||||||
|
|
||||||
|
(fn matches ((s (* const search)) (rock int)) bool
|
||||||
|
(return (&& (>= rock 0) (< rock 32) (!= (& (. s mask) (<< (cast 1 u32) rock)) 0))))
|
||||||
|
|
||||||
|
(fn offer ((s (* search)) (x int) (z int)) void
|
||||||
|
(var dx double (- (+ x 0.5) (. s x)))
|
||||||
|
(var dz double (- (+ z 0.5) (. s z)))
|
||||||
|
(var d double (sqrt (+ (* dx dx) (* dz dz))))
|
||||||
|
(pthread-mutex-lock (& (. s lock)))
|
||||||
|
(if (|| (! (. s found)) (< d (. s best-distance)))
|
||||||
|
(do
|
||||||
|
(= (. s found) true)
|
||||||
|
(= (. s best-distance) d)
|
||||||
|
(= (. s best-x) x)
|
||||||
|
(= (. s best-z) z)))
|
||||||
|
(pthread-mutex-unlock (& (. s lock))))
|
||||||
|
|
||||||
|
;;; Every STEP-th column of chunk (cx, cz) against the mask.
|
||||||
|
(fn scan-chunk ((s (* search)) (tr (* terra)) (rocks (* strata)) (cx int) (cz int) (step int)
|
||||||
|
(column (* u8))) void
|
||||||
|
(var heights [u16 1024])
|
||||||
|
(terra/chunk-heights tr cx cz step heights)
|
||||||
|
(for (var lz int 0) (< lz 32) (+= lz step)
|
||||||
|
(for (var lx int 0) (< lx 32) (+= lx step)
|
||||||
|
(var h int [heights (+ (* lz 32) lx)])
|
||||||
|
(strata/column rocks cx cz lx lz h column)
|
||||||
|
(if (matches s (rock-at column h (. s mode) (. s value)))
|
||||||
|
(offer s (+ (* cx 32) lx) (+ (* cz 32) lz))))))
|
||||||
|
|
||||||
|
(fn search-thread ((arg (* void))) (* void)
|
||||||
|
(var s (* search) arg)
|
||||||
|
(var g (* worldgen) (worldgen/make (. s settings) (. s assets-dir)))
|
||||||
|
(var tr (* terra) (if g (terra/make g (. s rivers)) NULL))
|
||||||
|
(var rocks (* strata) (if g (strata/make g (. s assets-dir)) NULL))
|
||||||
|
(var column (* u8) (malloc (+ (. s settings size-y) 1)))
|
||||||
|
(while (&& tr rocks)
|
||||||
|
(pthread-mutex-lock (& (. s lock)))
|
||||||
|
(var i int (. s next))
|
||||||
|
(var stop bool (|| (. s cancel) (>= i (. s region-count))))
|
||||||
|
(if (&& (! stop) (. s found))
|
||||||
|
(= stop (> (region-distance s [(. s regions) (* i 2)] [(. s regions) (+ (* i 2) 1)])
|
||||||
|
(. s best-distance))))
|
||||||
|
(if (! stop)
|
||||||
|
(++ (. s next)))
|
||||||
|
(pthread-mutex-unlock (& (. s lock)))
|
||||||
|
(if stop
|
||||||
|
(break))
|
||||||
|
(var rx int [(. s regions) (* i 2)])
|
||||||
|
(var rz int [(. s regions) (+ (* i 2) 1)])
|
||||||
|
(for (var cz int 0) (< cz 16) (++ cz)
|
||||||
|
(for (var cx int 0) (< cx 16) (++ cx)
|
||||||
|
(scan-chunk s tr rocks (+ (* rx 16) cx) (+ (* rz 16) cz) SEARCH-STEP column)))
|
||||||
|
(pthread-mutex-lock (& (. s lock)))
|
||||||
|
(++ (. s done-count))
|
||||||
|
(pthread-mutex-unlock (& (. s lock))))
|
||||||
|
|
||||||
|
;; The last thread out refines the hit: every block of the chunks
|
||||||
|
;; around it, which holds the true nearest block unless a 16 block
|
||||||
|
;; sample missed a smaller patch.
|
||||||
|
(pthread-mutex-lock (& (. s lock)))
|
||||||
|
(-- (. s active))
|
||||||
|
(var last bool (&& (== (. s active) 0) (. s found) (! (. s cancel))))
|
||||||
|
(var bx int (. s best-x))
|
||||||
|
(var bz int (. s best-z))
|
||||||
|
(pthread-mutex-unlock (& (. s lock)))
|
||||||
|
(if (&& last tr rocks)
|
||||||
|
(for (var dz int -1) (<= dz 1) (++ dz)
|
||||||
|
(for (var dx int -1) (<= dx 1) (++ dx)
|
||||||
|
(var cx int (+ (/ bx 32) dx))
|
||||||
|
(var cz int (+ (/ bz 32) dz))
|
||||||
|
(if (&& (>= cx 0) (>= cz 0) (< (* cx 32) (. s settings size-x)) (< (* cz 32) (. s settings size-z)))
|
||||||
|
(scan-chunk s tr rocks cx cz 1 column)))))
|
||||||
|
(free column)
|
||||||
|
(if rocks (strata/free rocks))
|
||||||
|
(if tr (terra/free tr))
|
||||||
|
(if g (worldgen/free g))
|
||||||
|
(return NULL))
|
||||||
|
|
||||||
|
(pub fn search/make () (* search)
|
||||||
|
(var s (* search) (calloc 1 (sizeof search)))
|
||||||
|
(pthread-mutex-init (& (. s lock)) NULL)
|
||||||
|
(return s))
|
||||||
|
|
||||||
|
;;; Waits for the running search, if any, to give up.
|
||||||
|
(pub fn search/stop ((s (* search))) void
|
||||||
|
(if (! (. s running))
|
||||||
|
(return))
|
||||||
|
(pthread-mutex-lock (& (. s lock)))
|
||||||
|
(= (. s cancel) true)
|
||||||
|
(pthread-mutex-unlock (& (. s lock)))
|
||||||
|
(for (var i int 0) (< i (. s thread-count)) (++ i)
|
||||||
|
(pthread-join [(. s threads) i] NULL))
|
||||||
|
(= (. s running) false)
|
||||||
|
(free (. s regions))
|
||||||
|
(= (. s regions) NULL))
|
||||||
|
|
||||||
|
;;; Starts looking for MASK's rocks nearest (x, z); see search/poll.
|
||||||
|
(pub fn search/start ((s (* search)) (settings world-settings) (rivers river-config)
|
||||||
|
(assets-dir (* const char)) (mode int) (value int) (mask u32)
|
||||||
|
(x double) (z double) (threads int)) void
|
||||||
|
(search/stop s)
|
||||||
|
(= (. s settings) settings)
|
||||||
|
(= (. s rivers) rivers)
|
||||||
|
(snprintf (. s assets-dir) (sizeof (. s assets-dir)) "%s" assets-dir)
|
||||||
|
(= (. s mode) mode)
|
||||||
|
(= (. s value) value)
|
||||||
|
(= (. s mask) mask)
|
||||||
|
(= (. s x) x)
|
||||||
|
(= (. s z) z)
|
||||||
|
(= (. s cancel) false)
|
||||||
|
(= (. s found) false)
|
||||||
|
(= (. s next) 0)
|
||||||
|
(= (. s done-count) 0)
|
||||||
|
|
||||||
|
(var crx int (cast (floor (/ x 512)) int))
|
||||||
|
(var crz int (cast (floor (/ z 512)) int))
|
||||||
|
(var r int SEARCH-RADIUS-REGIONS)
|
||||||
|
(var max-rx int (- (/ (. settings size-x) 512) 1))
|
||||||
|
(var max-rz int (- (/ (. settings size-z) 512) 1))
|
||||||
|
(var order (* region-order) (malloc (* (+ (* 2 r) 1) (+ (* 2 r) 1) (sizeof region-order))))
|
||||||
|
(var n int 0)
|
||||||
|
(for (var rz int (- crz r)) (<= rz (+ crz r)) (++ rz)
|
||||||
|
(for (var rx int (- crx r)) (<= rx (+ crx r)) (++ rx)
|
||||||
|
(if (|| (< rx 0) (< rz 0) (> rx max-rx) (> rz max-rz))
|
||||||
|
(continue))
|
||||||
|
(= [order n] #(region-order : rx rz (region-distance s rx rz)))
|
||||||
|
(++ n)))
|
||||||
|
(qsort order n (sizeof region-order) by-distance)
|
||||||
|
(= (. s regions) (malloc (* (+ n 1) 2 (sizeof int))))
|
||||||
|
(for (var i int 0) (< i n) (++ i)
|
||||||
|
(= [(. s regions) (* i 2)] (. [order i] rx))
|
||||||
|
(= [(. s regions) (+ (* i 2) 1)] (. [order i] rz)))
|
||||||
|
(free order)
|
||||||
|
(= (. s region-count) n)
|
||||||
|
|
||||||
|
(if (> threads MAX-SEARCH-THREADS) (= threads MAX-SEARCH-THREADS))
|
||||||
|
(if (< threads 1) (= threads 1))
|
||||||
|
(= (. s thread-count) threads)
|
||||||
|
(= (. s active) threads)
|
||||||
|
(= (. s running) true)
|
||||||
|
(for (var i int 0) (< i threads) (++ i)
|
||||||
|
(pthread-create (& [(. s threads) i]) NULL search-thread s)))
|
||||||
|
|
||||||
|
;;; Whether the search has finished; once it has, the threads are
|
||||||
|
;;; joined and FOUND, BEST-X, BEST-Z hold the answer.
|
||||||
|
(pub fn search/poll ((s (* search))) bool
|
||||||
|
(if (! (. s running))
|
||||||
|
(return true))
|
||||||
|
(pthread-mutex-lock (& (. s lock)))
|
||||||
|
(var finished bool (== (. s active) 0))
|
||||||
|
(pthread-mutex-unlock (& (. s lock)))
|
||||||
|
(if finished
|
||||||
|
(do
|
||||||
|
(for (var i int 0) (< i (. s thread-count)) (++ i)
|
||||||
|
(pthread-join [(. s threads) i] NULL))
|
||||||
|
(= (. s running) false)
|
||||||
|
(free (. s regions))
|
||||||
|
(= (. s regions) NULL)))
|
||||||
|
(return finished))
|
||||||
@@ -647,10 +647,11 @@
|
|||||||
(= [out 0] (* dx force))
|
(= [out 0] (* dx force))
|
||||||
(= [out 1] (* dz force)))
|
(= [out 1] (* dz force)))
|
||||||
|
|
||||||
;;; Heights of the columns of chunk (cx, cz) whose local x and z are
|
;;; A height is the top solid block, 0 if there is none.
|
||||||
;;; multiples of STEP, into HEIGHTS[lz * 32 + lx]; the rest are left
|
;;; Heights of the columns of chunk (cx, cz) from local (lx0, lz0) to
|
||||||
;;; alone. A height is the top solid block, 0 if there is none.
|
;;; (lx1, lz1) inclusive, every STEP-th, into HEIGHTS[lz * 32 + lx].
|
||||||
(pub fn terra/chunk-heights ((t (* terra)) (cx int) (cz int) (step int) (heights (* u16))) void
|
(fn heights-in ((t (* terra)) (cx int) (cz int) (lx0 int) (lz0 int) (lx1 int) (lz1 int)
|
||||||
|
(step int) (heights (* u16))) void
|
||||||
(use-region t (/ cx 16) (/ cz 16))
|
(use-region t (/ cx 16) (/ cz 16))
|
||||||
(var per-plate int (river-plate/chunks (& (. t config))))
|
(var per-plate int (river-plate/chunks (& (. t config))))
|
||||||
(var plate (* river-plate) (use-plate t (/ cx per-plate) (/ cz per-plate)))
|
(var plate (* river-plate) (use-plate t (/ cx per-plate) (/ cz per-plate)))
|
||||||
@@ -686,8 +687,8 @@
|
|||||||
(var ths [double 16])
|
(var ths [double 16])
|
||||||
(var c (* river-config) (& (. t config)))
|
(var c (* river-config) (& (. t config)))
|
||||||
|
|
||||||
(for (var lz int 0) (< lz 32) (+= lz step)
|
(for (var lz int lz0) (<= lz lz1) (+= lz step)
|
||||||
(for (var lx int 0) (< lx 32) (+= lx step)
|
(for (var lx int lx0) (<= lx lx1) (+= lx step)
|
||||||
(var wx int (+ (* cx 32) lx))
|
(var wx int (+ (* cx 32) lx))
|
||||||
(var wz int (+ (* cz 32) lz))
|
(var wz int (+ (* cz 32) lz))
|
||||||
(var sample river-sample (river-sampler/sample (& (. t sampler)) plate (. t segments)
|
(var sample river-sample (river-sampler/sample (& (. t sampler)) plate (. t segments)
|
||||||
@@ -785,3 +786,18 @@
|
|||||||
(if solid (= height y)))
|
(if solid (= height y)))
|
||||||
(= [heights (+ (* lz 32) lx)] (cast height u16))))
|
(= [heights (+ (* lz 32) lx)] (cast height u16))))
|
||||||
(free w))
|
(free w))
|
||||||
|
|
||||||
|
;;; Heights of the columns of chunk (cx, cz) whose local x and z are
|
||||||
|
;;; multiples of STEP, into HEIGHTS[lz * 32 + lx]; the rest are left
|
||||||
|
;;; alone.
|
||||||
|
(pub fn terra/chunk-heights ((t (* terra)) (cx int) (cz int) (step int) (heights (* u16))) void
|
||||||
|
(heights-in t cx cz 0 0 31 31 step heights))
|
||||||
|
|
||||||
|
;;; The height of the one column at block (x, z): 1/1024th of a chunk's
|
||||||
|
;;; work, for a cursor or a probe.
|
||||||
|
(pub fn terra/column-height ((t (* terra)) (x int) (z int)) int
|
||||||
|
(var heights [u16 1024])
|
||||||
|
(var lx int (% x 32))
|
||||||
|
(var lz int (% z 32))
|
||||||
|
(heights-in t (/ x 32) (/ z 32) lx lz lx lz 1 heights)
|
||||||
|
(return [heights (+ (* lz 32) lx)]))
|
||||||
|
|||||||
@@ -24,7 +24,7 @@
|
|||||||
(import terra)
|
(import terra)
|
||||||
(import strata)
|
(import strata)
|
||||||
|
|
||||||
(pub define MAX-TILES 16384)
|
(pub define MAX-TILES 65536)
|
||||||
;;; Not worldgen maps: Rivers' distance to the nearest river, the
|
;;; 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.
|
;;; terrain height, and the rock at the surface, at a depth or at a Y.
|
||||||
(pub define LAYER-RIVERS 6)
|
(pub define LAYER-RIVERS 6)
|
||||||
@@ -439,9 +439,11 @@
|
|||||||
(= (. self queue-pos) 0)
|
(= (. self queue-pos) 0)
|
||||||
(= (. self waiting) 0))
|
(= (. self waiting) 0))
|
||||||
|
|
||||||
;; The texture for KEY if it is ready, else NULL and the tile is
|
;; The texture for KEY if it is ready, and VALUES and SIZE if it is a
|
||||||
;; queued behind those requested earlier this frame.
|
;; value tile (both may be NULL); else NULL and the tile is queued
|
||||||
(pub fn request ((self (* tiles)) (renderer (* SDL-Renderer)) (k tile-key)) (* SDL-Texture)
|
;; behind those requested earlier this frame.
|
||||||
|
(pub fn request ((self (* tiles)) (renderer (* SDL-Renderer)) (k tile-key)
|
||||||
|
(values (* * u8)) (size (* int))) (* SDL-Texture)
|
||||||
(var slot int (index-get self k))
|
(var slot int (index-get self k))
|
||||||
(if (< slot 0)
|
(if (< slot 0)
|
||||||
(do
|
(do
|
||||||
@@ -470,7 +472,11 @@
|
|||||||
(= (. tl pixels) NULL)
|
(= (. tl pixels) NULL)
|
||||||
(= (. tl state) TILE-DRAWN)))
|
(= (. tl state) TILE-DRAWN)))
|
||||||
(if (== (. tl state) TILE-DRAWN)
|
(if (== (. tl state) TILE-DRAWN)
|
||||||
(return (. tl texture)))
|
(do
|
||||||
|
;; a value tile's values, for whoever wants to count them
|
||||||
|
(if values (= (* values) (. tl values)))
|
||||||
|
(if size (= (* size) (. tl size)))
|
||||||
|
(return (. tl texture))))
|
||||||
(++ (. self waiting))
|
(++ (. self waiting))
|
||||||
(if (&& (== (. tl state) TILE-QUEUED) (< (. self queue-len) MAX-TILES))
|
(if (&& (== (. tl state) TILE-QUEUED) (< (. self queue-len) MAX-TILES))
|
||||||
(do
|
(do
|
||||||
|
|||||||
759
src/viewer.sex
759
src/viewer.sex
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user