diff --git a/Makefile b/Makefile index 33b05c8..50df5a3 100644 --- a/Makefile +++ b/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) $(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) $(CC) $^ -o $@ $(SDL_LIBS) -lsqlite3 diff --git a/Plan.org b/Plan.org index fe0b2be..50a9c47 100644 --- a/Plan.org +++ b/Plan.org @@ -485,6 +485,86 @@ Viewer: - *Tile keys.* Now injective bit fields. The old =map * 64 + stratum= 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 - Level of detail: - zoomed out: one column per N blocks; diff --git a/src/hover.sex b/src/hover.sex new file mode 100644 index 0000000..d440de7 --- /dev/null +++ b/src/hover.sex @@ -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)) diff --git a/src/search.sex b/src/search.sex new file mode 100644 index 0000000..84b16a4 --- /dev/null +++ b/src/search.sex @@ -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)) diff --git a/src/terra.sex b/src/terra.sex index 93a5c7b..24d5257 100644 --- a/src/terra.sex +++ b/src/terra.sex @@ -647,10 +647,11 @@ (= [out 0] (* dx force)) (= [out 1] (* dz force))) -;;; 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. A height is the top solid block, 0 if there is none. -(pub fn terra/chunk-heights ((t (* terra)) (cx int) (cz int) (step int) (heights (* u16))) void +;;; A height is the top solid block, 0 if there is none. +;;; Heights of the columns of chunk (cx, cz) from local (lx0, lz0) to +;;; (lx1, lz1) inclusive, every STEP-th, into HEIGHTS[lz * 32 + lx]. +(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)) (var per-plate int (river-plate/chunks (& (. t config)))) (var plate (* river-plate) (use-plate t (/ cx per-plate) (/ cz per-plate))) @@ -686,8 +687,8 @@ (var ths [double 16]) (var c (* river-config) (& (. t config))) - (for (var lz int 0) (< lz 32) (+= lz step) - (for (var lx int 0) (< lx 32) (+= lx step) + (for (var lz int lz0) (<= lz lz1) (+= lz step) + (for (var lx int lx0) (<= lx lx1) (+= lx step) (var wx int (+ (* cx 32) lx)) (var wz int (+ (* cz 32) lz)) (var sample river-sample (river-sampler/sample (& (. t sampler)) plate (. t segments) @@ -785,3 +786,18 @@ (if solid (= height y))) (= [heights (+ (* lz 32) lx)] (cast height u16)))) (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)])) diff --git a/src/tiles.sex b/src/tiles.sex index 7828f80..1d4af07 100644 --- a/src/tiles.sex +++ b/src/tiles.sex @@ -24,7 +24,7 @@ (import terra) (import strata) -(pub define MAX-TILES 16384) +(pub define MAX-TILES 65536) ;;; 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) @@ -439,9 +439,11 @@ (= (. 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. - (pub fn request ((self (* tiles)) (renderer (* SDL-Renderer)) (k tile-key)) (* SDL-Texture) + ;; The texture for KEY if it is ready, and VALUES and SIZE if it is a + ;; value tile (both may be NULL); else NULL and the tile is queued + ;; 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)) (if (< slot 0) (do @@ -470,7 +472,11 @@ (= (. tl pixels) NULL) (= (. 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)) (if (&& (== (. tl state) TILE-QUEUED) (< (. self queue-len) MAX-TILES)) (do diff --git a/src/viewer.sex b/src/viewer.sex index 109a41b..26f1784 100644 --- a/src/viewer.sex +++ b/src/viewer.sex @@ -1,14 +1,16 @@ ;;; The map viewer window: SDL3 for the window, events and rendering, ;;; Nuklear for the panels drawn over the map, tiles for the map. ;;; -;;; vs-map-viewer [--assets DIR] [--zoom BLOCKS-PER-POINT] [--layer N] -;;; [--rock-depth N | --rock-y Y] [--highlight ROCK] [--probe X Z] +;;; vs-map-viewer [--assets DIR] [--zoom BLOCKS-PER-POINT] +;;; [--base ocean|rivers|height] [--overlay NAME]... [--opacity F] +;;; [--rock-depth N | --rock-y Y] [--highlight ROCK]... [--find] +;;; [--probe X Z] [--mouse X Y] ;;; [--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 stdio.h stdlib.h string.h math.h "nk.h" "stb_image_write.h") +(include stdio.h stdlib.h string.h math.h unistd.h "nk.h" "stb_image_write.h") (import cs) (import json) (import maps) @@ -20,11 +22,16 @@ (import rivers) (import terra) (import strata) +(import search) +(import hover) (import settings) (import tiles) -(define WINDOW-WIDTH 1280) -(define WINDOW-HEIGHT 800) +(define WINDOW-WIDTH 1600) +(define WINDOW-HEIGHT 960) +;; the panels' widths, in window points +(define LEFT-PANEL 310) +(define RIGHT-PANEL 300) (define FONT-SIZE 13.0) (define REGION 512) ;; Blocks per window point, either way. Zoomed out further, a window @@ -32,9 +39,17 @@ (define MIN-ZOOM 0.0625) (define MAX-ZOOM 32.0) -(var layer-names [(* const char) 9] - #("Landform" "Climate" "Geologic province" "Upheaval" "Ocean" "Rock strata thickness" - "Rivers" "Terrain height" "Rock")) +;;; One base layer is drawn opaque; any overlays go over it with the +;;; overlay opacity. Command line names first, then panel labels. +(var base-layers [int 3] #(4 6 7)) +(var base-names [(* const char) 3] #("ocean" "rivers" "height")) +(var base-labels [(* const char) 3] #("Ocean" "Rivers" "Terrain height")) +(var overlay-layers [int 6] #(0 1 2 3 5 8)) +(var overlay-names [(* const char) 6] #("landform" "climate" "province" "upheaval" "strata" "rock")) +;;; The Rock overlay's bit in `overlays'. +(define ROCK-OVERLAY-BIT 32) +(var overlay-labels [(* const char) 6] + #("Landform" "Climate" "Geologic province" "Upheaval" "Rock strata thickness" "Rock")) (var rock-mode-names [(* const char) 3] #("Surface" "Depth below surface" "At Y")) ;;; Rock colours, roughly as they look in game; others get a stable one. @@ -48,57 +63,113 @@ #(11836044 7236198 3815997 5204794 5926522 12618330 14205834 6186096 10124124 13223864 9067080 15790312 11883310 4871528 8227450)) (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. ;;; The strata of the column clicked last: top down, as (rock, top y, ;;; bottom y) runs. (record probe ((valid bool) (x int) (z int) (height int) (count int) (rocks [int 64]) (tops [int 64]) (bottoms [int 64]))) -(record hover ((valid bool) (rx int) (rz int) (maps region-maps) - (plate (* river-plate)) (segments (* int)) - (terra (* terra)) (cx int) (cz int) (has-chunk bool) (heights [u16 1024]))) - (record app ((window (* SDL-Window)) (renderer (* SDL-Renderer)) (ctx (* struct nk-context)) (assets-dir [char 1024]) (world-path [char 1024]) (rivers-path [char 1024]) - (rivers river-config) (sampler river-sampler) + (rivers river-config) (source world-source) (edit world-source) (g (* worldgen)) (tiles (* tiles)) - (center-x double) (center-z double) (zoom double) (layer int) (stratum int) + (center-x double) (center-z double) (zoom double) (base int) (overlays u32) + (opacity float) (stratum int) (mouse-x float) (mouse-y float) (dragging bool) (dragged bool) (ui-hovered bool) - (rocks (* strata)) (rock-mode int) (rock-value int) (highlight int) (probe probe) - (column [u8 1100]) (highlight-code (* const char)) (probe-x int) (probe-z int) - (hover hover) (pending int) (status [char 256]) (screenshot (* const char)))) + (rocks (* strata)) (rock-mode int) (rock-value int) (highlight u32) (probe probe) + (column [u8 1100]) (highlight-codes [* const char 32]) (highlight-code-count int) + (probe-x int) (probe-z int) + ;; share of the visible map per rock value, counted while drawing + (coverage [i64 256]) (coverage-total i64) + (pins [int 64]) (pin-count int) + (search (* search)) (searching bool) (search-status [char 160]) (find-requested bool) + (show-stories bool) (show-cursor-rock bool) + ;; what is under the cursor, from the hover worker; and a terrain + ;; generator of the main thread's own, for probes + (hover-worker (* hover-worker)) (hover hover-result) (probe-terra (* terra)) + (pending int) (status [char 256]) (screenshot (* const char)))) (fn fail ((what (* const char))) int (SDL-Log "%s: %s" what (SDL-GetError)) (return 1)) -;;; Bakes Nuklear's default font at the display scale, then shrinks the -;;; reported height back, so text is crisp on a Retina display and still -;;; laid out in window units. +;;; Monospace fonts with Cyrillic, tried in order: seeds are often typed +;;; in it, and Nuklear's built-in font only has ASCII. +(var font-files [(* const char) 4] + #("/System/Library/Fonts/Menlo.ttc" + "/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf" + "/usr/share/fonts/TTF/DejaVuSansMono.ttf" + "/usr/share/fonts/dejavu/DejaVuSansMono.ttf")) + +;;; Bakes the font at the display scale, then shrinks the reported +;;; height back, so text is crisp on a Retina display and still laid +;;; out in window units. (fn setup-font ((ctx (* struct nk-context)) (scale float)) void (var config (struct nk-font-config) (nk-font-config 0)) + (= (. config range) (nk-font-cyrillic-glyph-ranges)) (var atlas (* struct nk-font-atlas) (nk-sdl-font-stash-begin ctx)) - (var font (* struct nk-font) - (nk-font-atlas-add-default atlas (* FONT-SIZE scale) (& config))) + (var font (* struct nk-font) NULL) + (for (var i int 0) (&& (< i 4) (! font)) (++ i) + (if (== (access [font-files i] R-OK) 0) + (= font (nk-font-atlas-add-from-file atlas [font-files i] (* FONT-SIZE scale) (& config))))) + (if (! font) + (do + (= config (nk-font-config 0)) + (= font (nk-font-atlas-add-default atlas (* FONT-SIZE scale) (& config))))) (nk-sdl-font-stash-end ctx) (/= (. font handle height) scale) (nk-style-set-font ctx (& (. font handle)))) +;;; Nuklear's default marks a ticked checkbox or option with a dark +;;; square inside a grey one, which reads as empty; ticked ones get an +;;; accent instead. +(fn setup-style ((ctx (* struct nk-context))) void + (var accent (struct nk-style-item) (nk-style-item-color (nk-rgb 235 185 70))) + (var style (* struct nk-style) (& (. ctx style))) + (= (. style checkbox cursor-normal) accent) + (= (. style checkbox cursor-hover) accent) + (= (. style option cursor-normal) accent) + (= (. style option cursor-hover) accent)) + ;;; 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))))) +;;; The map is drawn between the side panels; these convert between +;;; window points and world blocks in that viewport. +(fn map-rect ((a (* app))) SDL-FRect + (var w int) + (var h int) + (window-size a (& w) (& h)) + (var mw float (cast (- w LEFT-PANEL RIGHT-PANEL) float)) + (if (< mw 1) (= mw 1)) + (return #(SDL-FRect : LEFT-PANEL 0 mw (cast h float)))) -(fn to-world-z ((a (* app)) (sy float) (h int)) double - (return (+ (. a center-z) (* (- sy (/ h 2.0)) (. a zoom))))) +(fn map-centre-x ((a (* app))) double + (var r SDL-FRect (map-rect a)) + (return (+ (. r x) (/ (. r w) 2.0)))) -(fn to-screen ((a (* app)) (world double) (center double) (extent int)) float - (return (cast (+ (/ (- world center) (. a zoom)) (/ extent 2.0)) float))) +(fn map-centre-y ((a (* app))) double + (var r SDL-FRect (map-rect a)) + (return (+ (. r y) (/ (. r h) 2.0)))) + +(fn to-world-x ((a (* app)) (sx float)) double + (return (+ (. a center-x) (* (- sx (map-centre-x a)) (. a zoom))))) + +(fn to-world-z ((a (* app)) (sy float)) double + (return (+ (. a center-z) (* (- sy (map-centre-y a)) (. a zoom))))) + +(fn to-screen-x ((a (* app)) (world double)) float + (return (cast (+ (/ (- world (. a center-x)) (. a zoom)) (map-centre-x a)) float))) + +(fn to-screen-z ((a (* app)) (world double)) float + (return (cast (+ (/ (- world (. a center-z)) (. a zoom)) (map-centre-y a)) float))) + +;;; Whether window point (x, y) is on the map, not on a panel. +(fn on-map ((a (* app)) (x float) (y float)) bool + (var r SDL-FRect (map-rect a)) + (return (&& (>= x (. r x)) (< x (+ (. r x) (. r w))) (>= y (. r y)) (< y (+ (. r y) (. r h)))))) (fn go-to-spawn ((a (* app))) void (= (. a center-x) (/ (. a source settings size-x) 2.0)) @@ -106,17 +177,10 @@ ;;; Applying settings ---------------------------------------------- -(fn release-hover ((a (* app))) void - (if (. a hover valid) - (region-maps/free (& (. a hover maps)))) - (= (. a hover valid) false) - (if (. a hover plate) - (river-plate/free (. a hover plate))) - (= (. a hover plate) NULL) - (if (. a hover terra) - (terra/free (. a hover terra))) - (= (. a hover terra) NULL) - (= (. a hover has-chunk) false)) +(fn release-probe-terra ((a (* app))) void + (if (. a probe-terra) + (terra/free (. a probe-terra))) + (= (. a probe-terra) NULL)) ;;; The colour of rock index I. (fn rock-colour ((a (* app)) (i int)) u32 @@ -127,26 +191,21 @@ (var hash u32 (* (cast (+ i 41) u32) (cast 2654435761 u32))) (return (& hash 16777215))) -;;; The rock layer's palette: every rock in its colour, or only the -;;; highlighted one with the rest greyed out. +;;; The rock overlay's palette: every rock in its colour, or only the +;;; highlighted ones, the rest transparent. (fn update-palette ((a (* app))) void (var p [u32 256]) (for (var i int 0) (< i 256) (++ i) (= [p i] 4280295456)) - (= [p VALUE-WATER] 4280171404) - (= [p VALUE-AIR] 4280295456) - (= [p ROCK-MANTLE] 4278190080) + ;; the base layer shows through water, air and the rocks not looked for + (= [p VALUE-WATER] 0) + (= [p VALUE-AIR] 0) + (= [p ROCK-MANTLE] 0) (for (var i int 0) (< i (. a rocks rock-count)) (++ i) (var c u32 (rock-colour a i)) - (if (&& (>= (. a highlight) 0) (!= i (. a highlight))) - (do - ;; grey, a third of the brightness - (var r u32 (& (>> c 16) 255)) - (var g u32 (& (>> c 8) 255)) - (var b u32 (& c 255)) - (var grey u32 (/ (+ r g b) 9)) - (= c (* grey 65793)))) - (= [p i] (c-bit-or 4278190080 c))) + (= [p i] (c-bit-or 4278190080 c)) + (if (&& (!= (. a highlight) 0) (== (& (. a highlight) (<< (cast 1 u32) i)) 0)) + (= [p i] 0))) (.set-palette (. a tiles) p)) ;;; Makes SOURCE the world shown: a fresh generator for the panels, @@ -162,19 +221,20 @@ (if (. a rocks) (strata/free (. a rocks))) (= (. a rocks) rocks) - (if (>= (. a highlight) (. rocks rock-count)) - (= (. a highlight) -1)) + (&= (. a highlight) (- (<< (cast 1 u32) (. rocks rock-count)) 1)) (= (. a probe valid) false) + (search/stop (. a search)) + (= (. a searching) false) (if (. a g) (worldgen/free (. a g))) (= (. a g) g) (= (. a source) source) (= (. a edit) source) - (release-hover a) + (release-probe-terra a) (if (>= (. a stratum) (. g strata count)) (= (. a stratum) 0)) - (= (. a sampler) (river-sampler/make (& (. a rivers)) (. source settings size-y))) (.set-settings (. a tiles) (. source settings) (. a rivers)) + (hover-worker/set-world (. a hover-worker) (. source settings) (. a rivers)) (update-palette a) (snprintf (. a status) (sizeof (. a status)) "seed %d" (. source settings seed)) (return true)) @@ -223,6 +283,39 @@ (if (< zoom 16) (return 32)) (return 16)) +;;; The tile of LAYER for region (rx, rz) at the current zoom and +;;; settings. +(fn layer-key ((a (* app)) (layer int) (rx int) (rz int)) tile-key + (var k tile-key) + (= (. k map) layer) + (= (. k stratum) (. a stratum)) + (= (. k param) 0) + (= (. k rx) rx) + (= (. k rz) rz) + (if (== layer LAYER-RIVERS) (= (. k stratum) (river-resolution (. a zoom))) + (== layer LAYER-HEIGHT) (= (. k stratum) (height-resolution (. a zoom))) + (== layer LAYER-ROCK) + (do + (= (. k stratum) (height-resolution (. a zoom))) + (var value int (. a rock-value)) + (if (== (. a rock-mode) ROCK-AT-SURFACE) (= value 0)) + (= (. k param) (+ (* (. a rock-mode) 1024) value)))) + (return k)) + +;;; Adds a value tile's samples that fall inside the visible world +;;; rectangle (x0, z0)-(x1, z1) to the coverage counts. +(fn count-visible ((a (* app)) (values (* const u8)) (size int) (rx int) (rz int) + (x0 double) (z0 double) (x1 double) (z1 double)) void + (var step double (/ (cast REGION double) size)) + (var ix0 int (cast (fmax 0 (floor (/ (- x0 (* rx REGION)) step))) int)) + (var iz0 int (cast (fmax 0 (floor (/ (- z0 (* rz REGION)) step))) int)) + (var ix1 int (cast (fmin (- size 1) (floor (/ (- x1 (* rx REGION)) step))) int)) + (var iz1 int (cast (fmin (- size 1) (floor (/ (- z1 (* rz REGION)) step))) int)) + (for (var z int iz0) (<= z iz1) (++ z) + (for (var x int ix0) (<= x ix1) (++ x) + (++ [(. a coverage) [values (+ (* z size) x)]]) + (++ (. a coverage-total))))) + ;;; Requests the visible tiles, nearest the centre first, and draws ;;; those that are ready. (fn draw-map ((a (* app))) void @@ -230,10 +323,11 @@ (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 view SDL-FRect (map-rect a)) + (var x0 double (to-world-x a (. view x))) + (var z0 double (to-world-z a (. view y))) + (var x1 double (to-world-x a (+ (. view x) (. view w)))) + (var z1 double (to-world-z a (+ (. view y) (. view 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)) @@ -249,31 +343,37 @@ (++ n))) (qsort list n (sizeof wanted) by-distance) + (memset (. a coverage) 0 (sizeof (. a coverage))) + (= (. a coverage-total) 0) (.begin-frame (. a tiles)) - (for (var i int 0) (< i n) (++ i) - (var k tile-key) - (= (. k map) (. a layer)) - (= (. k stratum) (. a stratum)) - (if (== (. a layer) LAYER-RIVERS) (= (. k stratum) (river-resolution (. a zoom)))) - (if (== (. a layer) LAYER-HEIGHT) (= (. k stratum) (height-resolution (. a zoom)))) - (= (. k param) 0) - (if (== (. a layer) LAYER-ROCK) - (do - (= (. k stratum) (height-resolution (. a zoom))) - (var value int (. a rock-value)) - (if (== (. a rock-mode) ROCK-AT-SURFACE) (= value 0)) - (= (. k param) (+ (* (. a rock-mode) 1024) value)))) - (= (. 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))) + ;; layer by layer, each nearest the centre first: the base fills in + ;; before the overlays start + (var layers [int 8]) + (var layer-count int 0) + (= [layers 0] (. a base)) + (= layer-count 1) + (for (var o int 0) (< o 6) (++ o) + (if (!= (& (. a overlays) (<< (cast 1 u32) o)) 0) + (do (= [layers layer-count] [overlay-layers o]) (++ layer-count)))) + (var alpha u8 (cast (* (. a opacity) 255) u8)) + (for (var l int 0) (< l layer-count) (++ l) + (for (var i int 0) (< i n) (++ i) + (var k tile-key (layer-key a [layers l] (. [list i] rx) (. [list i] rz))) + (var values (* u8) NULL) + (var size int 0) + (var tex (* SDL-Texture) (.request (. a tiles) (. a renderer) k (& values) (& size))) + (if (! tex) + (continue)) + (if (&& values (== [layers l] LAYER-ROCK)) + (count-visible a values size (. k rx) (. k rz) x0 z0 x1 z1)) + (SDL-SetTextureBlendMode tex SDL-BLENDMODE-BLEND) + (SDL-SetTextureAlphaMod tex (if (== l 0) 255 alpha)) + (var sx0 float (roundf (to-screen-x a (* (. k rx) REGION)))) + (var sz0 float (roundf (to-screen-z a (* (. k rz) REGION)))) + (var sx1 float (roundf (to-screen-x a (* (+ (. k rx) 1) REGION)))) + (var sz1 float (roundf (to-screen-z a (* (+ (. k rz) 1) REGION)))) + (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)) @@ -285,7 +385,9 @@ (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 stories int (. g story-count)) + (if (! (. a show-stories)) (= stories -1)) + (for (var i int -1) (< i stories) (++ 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") @@ -294,11 +396,28 @@ (= 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 sx float (to-screen-x a x)) + (var sy float (to-screen-z a z)) (var box SDL-FRect #(SDL-FRect : (- sx 3) (- sy 3) 7 7)) (SDL-RenderRect r (& box)) - (SDL-RenderDebugText r (+ sx 6) (- sy 4) name))) + (SDL-RenderDebugText r (+ sx 6) (- sy 4) name)) + ;; pins: a filled diamond, the last probed in yellow, numbered + (for (var i int 0) (< i (. a pin-count)) (++ i) + (var px int [(. a pins) (* i 2)]) + (var pz int [(. a pins) (+ (* i 2) 1)]) + (var sx float (to-screen-x a (+ px 0.5))) + (var sy float (to-screen-z a (+ pz 0.5))) + (var current bool (&& (. a probe valid) (== (. a probe x) px) (== (. a probe z) pz))) + (if current + (SDL-SetRenderDrawColor r 255 220 40 255) + (SDL-SetRenderDrawColor r 255 80 60 255)) + (for (var d int 0) (<= d 5) (++ d) + (SDL-RenderLine r (- sx (- 5 d)) (+ sy d) (+ sx (- 5 d)) (+ sy d)) + (SDL-RenderLine r (- sx (- 5 d)) (- sy d) (+ sx (- 5 d)) (- sy d))) + (var label [char 8]) + (snprintf label (sizeof label) "%d" (+ i 1)) + (SDL-SetRenderDrawColor r 255 255 255 255) + (SDL-RenderDebugText r (+ sx 8) (- sy 4) label))) ;;; Panels --------------------------------------------------------- @@ -309,9 +428,7 @@ (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 580) - (| NK-WINDOW-BORDER NK-WINDOW-MOVABLE NK-WINDOW-TITLE NK-WINDOW-MINIMIZABLE - NK-WINDOW-SCALABLE)) + (if (nk-tree-push ctx NK-TREE-TAB "World" NK-MAXIMIZED) (do (nk-layout-row-dynamic ctx 22 1) (nk-label ctx "World or server config file" NK-TEXT-LEFT) @@ -373,15 +490,14 @@ (= (. 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)) + (go-to-spawn a)) + (nk-tree-pop ctx)))) -;;; The rock layer's mode, its depth or Y, and the legend: a rock clicked -;;; is highlighted, clicked again shows all. +;;; The rock layer's mode, its depth or Y, and the legend: clicking rocks +;;; highlights them, each with its share of the view; Find nearest +;;; looks for the highlighted rocks around the view's centre. (fn draw-rock-controls ((a (* app))) void (var ctx (* struct nk-context) (. a ctx)) - (nk-layout-row-dynamic ctx 8 1) - (nk-spacing ctx 1) (nk-layout-row-dynamic ctx 20 1) (for (var i int 0) (< i 3) (++ i) (if (nk-option-label ctx [rock-mode-names i] (== (. a rock-mode) i)) @@ -401,63 +517,120 @@ (var c u32 (rock-colour a i)) (nk-button-color ctx (nk-rgb (cast (& (>> c 16) 255) int) (cast (& (>> c 8) 255) int) (cast (& c 255) int))) - (var name (* const char) (+ [(. rocks rock-codes) i] 5)) - (var selected nk-bool (== (. a highlight) i)) - (if (nk-selectable-label ctx name NK-TEXT-LEFT (& selected)) + (var label [char 64]) + (if (&& (> (. a coverage-total) 0) (!= (& (. a overlays) ROCK-OVERLAY-BIT) 0)) + (snprintf label (sizeof label) "%-13s %5.1f%%" (+ [(. rocks rock-codes) i] 5) + (/ (* 100.0 [(. a coverage) i]) (. a coverage-total))) + (snprintf label (sizeof label) "%s" (+ [(. rocks rock-codes) i] 5))) + (var bit u32 (<< (cast 1 u32) i)) + (var selected nk-bool (!= (& (. a highlight) bit) 0)) + (if (nk-selectable-label ctx label NK-TEXT-LEFT (& selected)) (do - (= (. a highlight) (if selected i -1)) + (^= (. a highlight) bit) (update-palette a)))) (nk-layout-row-dynamic ctx 18 1) - (nk-label ctx "Click a rock to show only it" NK-TEXT-LEFT)) + (if (== (. a highlight) 0) + (nk-label ctx "Click rocks to show only them" NK-TEXT-LEFT) + (do + (nk-layout-row-dynamic ctx 24 2) + (if (nk-button-label ctx "Show all") + (do (= (. a highlight) 0) (update-palette a))) + (if (. a searching) + (if (nk-button-label ctx "Cancel") + (do + (search/stop (. a search)) + (= (. a searching) false) + (snprintf (. a search-status) (sizeof (. a search-status)) "cancelled"))) + (if (nk-button-label ctx "Find nearest") + (= (. a find-requested) true))))) + (if (* (. a search-status)) + (do + (nk-layout-row-dynamic ctx 34 1) + (nk-label-wrap ctx (. a search-status))))) (fn draw-layers ((a (* app))) void (var ctx (* struct nk-context) (. a ctx)) + (if (nk-tree-push ctx NK-TREE-TAB "Layers" NK-MAXIMIZED) + (do + (nk-layout-row-dynamic ctx 18 1) + (nk-label ctx "Base" NK-TEXT-LEFT) + (nk-layout-row-dynamic ctx 20 1) + (for (var i int 0) (< i 3) (++ i) + (if (nk-option-label ctx [base-labels i] (== (. a base) [base-layers i])) + (= (. a base) [base-layers i]))) + (nk-layout-row-dynamic ctx 6 1) + (nk-spacing ctx 1) + (nk-layout-row-dynamic ctx 18 1) + (nk-label ctx "Overlays" NK-TEXT-LEFT) + (nk-layout-row-dynamic ctx 20 1) + (for (var i int 0) (< i 6) (++ i) + (var bit u32 (<< (cast 1 u32) i)) + (var on nk-bool (!= (& (. a overlays) bit) 0)) + (if (nk-checkbox-label ctx [overlay-labels i] (& on)) + (^= (. a overlays) bit)) + ;; the strata overlay needs its stratum + (if (&& on (== [overlay-layers i] 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-layout-row-dynamic ctx 20 1)))) + (nk-layout-row-dynamic ctx 22 1) + (nk-property-float ctx "Overlay opacity" 0 (& (. a opacity)) 1 0.05 0.005) + (nk-layout-row-dynamic ctx 6 1) + (nk-spacing ctx 1) + (nk-layout-row-dynamic ctx 20 1) + (var stories nk-bool (. a show-stories)) + (nk-checkbox-label ctx "Story locations" (& stories)) + (= (. a show-stories) stories) + (var cursor nk-bool (. a show-cursor-rock)) + (nk-checkbox-label ctx "Rock under cursor" (& cursor)) + (= (. a show-cursor-rock) cursor) + (nk-tree-pop ctx)))) + +;;; Rock finding: always there, whether or not the Rock overlay is on. +(fn draw-rocks ((a (* app))) void + (var ctx (* struct nk-context) (. a ctx)) + (if (nk-tree-push ctx NK-TREE-TAB "Rocks" NK-MAXIMIZED) + (do + (draw-rock-controls a) + (nk-tree-pop ctx)))) + +;;; A pin per probed column, so a probe can be found again. The oldest +;;; goes when there are 32. +(fn add-pin ((a (* app)) (x int) (z int)) void + (for (var i int 0) (< i (. a pin-count)) (++ i) + (if (&& (== [(. a pins) (* i 2)] x) (== [(. a pins) (+ (* i 2) 1)] z)) + (return))) + (if (== (. a pin-count) 32) + (do + (memmove (. a pins) (+ (. a pins) 2) (* 62 (sizeof int))) + (-- (. a pin-count)))) + (= [(. a pins) (* (. a pin-count) 2)] x) + (= [(. a pins) (+ (* (. a pin-count) 2) 1)] z) + (++ (. a pin-count))) + +;;; The pin within 6 points of window point (sx, sy), or -1. +(fn pin-at ((a (* app)) (sx float) (sy float)) int (var w int) (var h int) (window-size a (& w) (& h)) - (if (nk-begin ctx "Layer" (nk-rect (- w 250) 10 240 (if (== (. a layer) LAYER-ROCK) 640 280)) - (| 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 9) (++ i) - (if (nk-option-label ctx [layer-names i] (== (. a layer) i)) - (= (. a layer) i))) - (if (== (. a layer) LAYER-ROCK) - (draw-rock-controls a)) - (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)])) - -;;; The terrain height at block (x, z), its chunk computed once. -(fn hover-height ((a (* app)) (x int) (z int)) int - (if (! (. a hover terra)) - (= (. a hover terra) (terra/make (. a g) (. a rivers)))) - (var cx int (/ x 32)) - (var cz int (/ z 32)) - (if (|| (! (. a hover has-chunk)) (!= (. a hover cx) cx) (!= (. a hover cz) cz)) - (do - (terra/chunk-heights (. a hover terra) cx cz 1 (. a hover heights)) - (= (. a hover cx) cx) - (= (. a hover cz) cz) - (= (. a hover has-chunk) true))) - (return [(. a hover heights) (+ (* (% z 32) 32) (% x 32))])) + (for (var i int (- (. a pin-count) 1)) (>= i 0) (-- i) + (var px float (to-screen-x a (+ [(. a pins) (* i 2)] 0.5))) + (var pz float (to-screen-z a (+ [(. a pins) (+ (* i 2) 1)] 0.5))) + (if (&& (< (fabsf (- px sx)) 6) (< (fabsf (- pz sy)) 6)) + (return i))) + (return -1)) ;;; Probes the column at block (x, z): its height and strata. (fn probe-at ((a (* app)) (x int) (z int)) void - (var h int (hover-height a x z)) + (if (! (. a probe-terra)) + (= (. a probe-terra) (terra/make (. a g) (. a rivers)))) + (var h int (terra/column-height (. a probe-terra) x z)) (strata/column (. a rocks) (/ x 32) (/ z 32) (% x 32) (% z 32) h (. a column)) + (add-pin a x z) (var p (* probe) (& (. a probe))) (= (. p valid) true) (= (. p x) x) @@ -480,92 +653,168 @@ (if (! (. p valid)) (return)) (var ctx (* struct nk-context) (. a ctx)) - (var w int) - (var h int) - (window-size a (& w) (& h)) - (if (nk-begin ctx "Probe" (nk-rect (- w 330) (- h 330) 320 320) - (| NK-WINDOW-BORDER NK-WINDOW-MOVABLE NK-WINDOW-TITLE NK-WINDOW-SCALABLE - NK-WINDOW-CLOSABLE)) + (if (nk-tree-push ctx NK-TREE-TAB "Probe" NK-MAXIMIZED) (do (nk-layout-row-dynamic ctx 18 1) (var s (* world-settings) (& (. a source settings))) - (nk-labelf ctx NK-TEXT-LEFT "x %d z %d (from centre %d, %d)" (. p x) (. p z) - (- (. p x) (/ (. s size-x) 2)) (- (. p z) (/ (. s size-z) 2))) + (nk-labelf ctx NK-TEXT-LEFT "x %d z %d" (. p x) (. p z)) + (nk-labelf ctx NK-TEXT-LEFT "from centre %d, %d" (- (. p x) (/ (. s size-x) 2)) + (- (. p z) (/ (. s size-z) 2))) (nk-labelf ctx NK-TEXT-LEFT "surface at y %d" (. p height)) + (nk-layout-row-dynamic ctx 22 3) + (if (nk-button-label ctx "Go to") + (do + (= (. a center-x) (+ (. p x) 0.5)) + (= (. a center-z) (+ (. p z) 0.5)))) + (if (nk-button-label ctx "Clear pins") + (= (. a pin-count) 0)) + (if (nk-button-label ctx "Close") + (= (. p valid) false)) + (nk-layout-row-dynamic ctx 18 1) (for (var i int 0) (< i (. p count)) (++ i) (var r int [(. p rocks) i]) (var name (* const char) (if (== r ROCK-MANTLE) "mantle" (+ [(. a rocks rock-codes) r] 5))) (var thick int (+ (- [(. p tops) i] [(. p bottoms) i]) 1)) - (nk-labelf ctx NK-TEXT-LEFT "%3d-%-3d %-14s %3d" [(. p tops) i] [(. p bottoms) i] name thick))) - (= (. p valid) false)) - (nk-end ctx)) + (nk-labelf ctx NK-TEXT-LEFT "%3d-%-3d %-14s %3d" [(. p tops) i] [(. p bottoms) i] name thick)) + (nk-tree-pop ctx)))) -;;; Rivers' sample at block (x, z), building the plate it needs. -(fn hover-river ((a (* app)) (x int) (z int)) river-sample - (var per-plate int (river-plate/chunks (& (. a rivers)))) - (var cx int (/ x 32)) - (var cz int (/ z 32)) - (var p (* river-plate) (. a hover plate)) - (if (|| (! p) (!= (. p px) (/ cx per-plate)) (!= (. p pz) (/ cz per-plate))) +;;; The block under the cursor; false when the cursor is not on the +;;; world's part of the map. +(fn cursor-block ((a (* app)) (bx (* int)) (bz (* int))) bool + (var s (* world-settings) (& (. a source settings))) + (= (* bx) (cast (floor (to-world-x a (. a mouse-x))) int)) + (= (* bz) (cast (floor (to-world-z a (. a mouse-y))) int)) + (return (&& (on-map a (. a mouse-x) (. a mouse-y)) (>= (* bx) 0) (>= (* bz) 0) + (< (* bx) (. s size-x)) (< (* bz) (. s size-z))))) + +;;; Asks the hover worker about the block under the cursor and picks up +;;; its newest answer. +(fn update-hover ((a (* app))) void + (var bx int) + (var bz int) + (if (cursor-block a (& bx) (& bz)) (do - (if p (river-plate/free p)) - (= p (river-plate/make (. a rivers) (& (. a g oceans)) (. a source settings size-y) - (/ cx per-plate) (/ cz per-plate))) - (= (. a hover plate) p) - (= (. a hover segments) (realloc (. a hover segments) - (* (+ (. p segment-count) 1) (sizeof int)))))) - (var count int (river-plate/chunk-segments p cx cz (. a hover segments))) - (return (river-sampler/sample (& (. a sampler)) p (. a hover segments) count x z))) + (var value int (. a rock-value)) + (if (== (. a rock-mode) ROCK-AT-SURFACE) (= value 0)) + (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)))) + +(fn rock-name ((a (* app)) (rock int)) (* const char) + (if (|| (< rock 0) (>= rock (. a rocks rock-count))) + (return "?")) + (return (+ [(. a rocks rock-codes) rock] 5))) (fn draw-info ((a (* app))) void + (var ctx (* struct nk-context) (. a ctx)) + (var s (* world-settings) (& (. a source settings))) + (var bx int) + (var bz int) + (var inside bool (cursor-block a (& bx) (& bz))) + (var r (* hover-result) (& (. a hover))) + (if (nk-tree-push ctx NK-TREE-TAB "Info" NK-MAXIMIZED) + (do + (nk-layout-row-dynamic ctx 18 1) + (nk-labelf ctx NK-TEXT-LEFT "x %d z %d" bx bz) + (nk-labelf ctx NK-TEXT-LEFT "from centre %d, %d" (- bx (/ (. s size-x) 2)) (- bz (/ (. s size-z) 2))) + (nk-labelf ctx NK-TEXT-LEFT "region %d, %d zoom %.3g blocks/pt" (/ bx REGION) (/ bz REGION) + (. a zoom)) + (if (&& inside (. r valid)) + (do + (var g (* worldgen) (. a g)) + (nk-labelf ctx NK-TEXT-LEFT "landform %s" (. [(. g landform-table all) (. r landform)] code)) + (nk-labelf ctx NK-TEXT-LEFT "province %s" [(. g provinces codes) (. r province)]) + (var c int (. r climate)) + (nk-labelf ctx NK-TEXT-LEFT "climate %d C at sea level" (scaled-temperature (& (>> c 16) 255))) + (nk-labelf ctx NK-TEXT-LEFT " rain %d, geologic activity %d" (& (>> c 8) 255) (& c 255)) + (nk-labelf ctx NK-TEXT-LEFT "ocean %d upheaval %d" (. r ocean) (. r upheaval)) + (nk-labelf ctx NK-TEXT-LEFT "river distance %.0f, bank depth %.3f" + (. r river-distance) (. r bank)) + (nk-labelf ctx NK-TEXT-LEFT "terrain height %d (sea level %d)" (. r height) (. r sea-level)) + (nk-labelf ctx NK-TEXT-LEFT "surface rock %s" (rock-name a (. r surface-rock))))) + (nk-labelf ctx NK-TEXT-LEFT "%d tiles pending" (. a pending)) + (nk-label ctx (. a status) NK-TEXT-LEFT) + (nk-tree-pop ctx)))) + +;;; Find nearest: starts the search the legend asked for, and when one +;;; finishes, jumps to what it found and probes it there. +(fn update-search ((a (* app))) void + (if (. a find-requested) + (do + (= (. a find-requested) false) + (var value int (. a rock-value)) + (if (== (. a rock-mode) ROCK-AT-SURFACE) (= value 0)) + (search/start (. a search) (. a source settings) (. a rivers) (. a assets-dir) + (. a rock-mode) value (. a highlight) (. a center-x) (. a center-z) + (- (SDL-GetNumLogicalCPUCores) 1)) + (= (. a searching) true))) + (if (! (. a searching)) + (return)) + (var s (* search) (. a search)) + (if (! (search/poll s)) + (do + (snprintf (. a search-status) (sizeof (. a search-status)) "searching, %d of %d regions" + (. s done-count) (. s region-count)) + (return))) + (= (. a searching) false) + (if (. s found) + (do + (snprintf (. a search-status) (sizeof (. a search-status)) "nearest at %d, %d: %.0f blocks away" + (. s best-x) (. s best-z) (. s best-distance)) + (= (. a center-x) (+ (. s best-x) 0.5)) + (= (. a center-z) (+ (. s best-z) 0.5)) + (probe-at a (. s best-x) (. s best-z))) + (snprintf (. a search-status) (sizeof (. a search-status)) "none within %d blocks" + (* SEARCH-RADIUS-REGIONS REGION)))) + +;;; The two side panels, pinned to the window's edges: settings, what +;;; is under the cursor and the probe on the left, layers and rocks on +;;; the right. They are opaque, and the map is drawn between them. +(fn draw-panels ((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))) + (var left (struct nk-rect) (nk-rect 0 0 LEFT-PANEL (cast h float))) + (var right (struct nk-rect) (nk-rect (cast (- w RIGHT-PANEL) float) 0 RIGHT-PANEL (cast h float))) + (nk-window-set-bounds ctx "left" left) + (if (nk-begin ctx "left" left NK-WINDOW-BORDER) (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 270) 290 260) - (| NK-WINDOW-BORDER NK-WINDOW-MOVABLE NK-WINDOW-TITLE NK-WINDOW-MINIMIZABLE)) + (draw-settings a) + (draw-info a) + (draw-probe a))) + (nk-end ctx) + (nk-window-set-bounds ctx "right" right) + (if (nk-begin ctx "right" right NK-WINDOW-BORDER) + (do + (draw-layers a) + (draw-rocks a))) + (nk-end ctx)) + +;;; A label by the cursor naming the rock under it, as the Rocks +;;; window's mode has it: at the surface, at a depth or at a Y. It moves +;;; with the cursor every frame; its text is the hover worker's newest, +;;; which can trail the cursor by a frame or two. +(fn draw-cursor-rock ((a (* app))) void + (var bx int) + (var bz int) + (if (|| (! (. a show-cursor-rock)) (. a dragging) (! (cursor-block a (& bx) (& bz)))) + (return)) + (var r (* hover-result) (& (. a hover))) + (var text [char 64]) + (if (! (. r valid)) (snprintf text (sizeof text) "...") + (== (. r rock) -2) (snprintf text (sizeof text) "mantle") + (>= (. r rock) 0) (snprintf text (sizeof text) "%s, y %d" (rock-name a (. r rock)) (. r rock-y)) + (< (. r rock-y) (. r sea-level)) (snprintf text (sizeof text) "water, y %d" (. r rock-y)) + (snprintf text (sizeof text) "air, y %d" (. r rock-y))) + ;; beside the cursor, not under it, so it never takes the map's input + (var ctx (* struct nk-context) (. a ctx)) + (var bounds (struct nk-rect) (nk-rect (+ (. a mouse-x) 16) (+ (. a mouse-y) 16) 190 30)) + (nk-window-set-bounds ctx "cursor" bounds) + (if (nk-begin ctx "cursor" bounds (| NK-WINDOW-BORDER NK-WINDOW-NO-SCROLLBAR NK-WINDOW-NO-INPUT)) (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)) - (var r river-sample (hover-river a bx bz)) - (nk-labelf ctx NK-TEXT-LEFT "river distance %.0f, bank depth %.3f" - (. r distance) (. r bank)) - (var height int (hover-height a bx bz)) - (strata/column (. a rocks) (/ bx 32) (/ bz 32) (% bx 32) (% bz 32) height (. a column)) - (nk-labelf ctx NK-TEXT-LEFT "terrain height %d (sea level %d), %s" - height (. a hover terra sea-level) - (+ [(. a rocks rock-codes) [(. a column) height]] 5)))) - (nk-labelf ctx NK-TEXT-LEFT "%d tiles pending" (. a pending)) - (nk-label ctx (. a status) NK-TEXT-LEFT))) + (nk-label ctx text NK-TEXT-LEFT))) (nk-end ctx)) ;;; Events --------------------------------------------------------- @@ -575,11 +824,11 @@ (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)) + (var wx double (to-world-x a (. a mouse-x))) + (var wz double (to-world-z a (. a mouse-y))) (= (. 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))))) + (= (. a center-x) (- wx (* (- (. a mouse-x) (map-centre-x a)) (. a zoom)))) + (= (. a center-z) (- wz (* (- (. a mouse-y) (map-centre-y a)) (. a zoom))))) (fn handle-event ((a (* app)) (event (* SDL-Event))) bool (var type u32 (. event type)) @@ -608,9 +857,12 @@ (var w int) (var h int) (window-size a (& w) (& h)) - (var bx int (cast (floor (to-world-x a (. event button x) w)) int)) - (var bz int (cast (floor (to-world-z a (. event button y) h)) int)) - (if (&& (>= bx 0) (>= bz 0) (< bx (. a source settings size-x)) (< bz (. a source settings size-z))) + (var bx int (cast (floor (to-world-x a (. event button x))) int)) + (var bz int (cast (floor (to-world-z a (. event button y))) int)) + (var pin int (pin-at a (. event button x) (. event button y))) + (if (>= pin 0) + (probe-at a [(. a pins) (* pin 2)] [(. a pins) (+ (* pin 2) 1)]) + (&& (>= bx 0) (>= bz 0) (< bx (. a source settings size-x)) (< bz (. a source settings size-z))) (probe-at a bx bz)))) (= (. a dragging) false)) (&& (== type SDL-EVENT-MOUSE-WHEEL) (! (. a ui-hovered))) @@ -635,7 +887,11 @@ (pub fn main ((argc int) (argv [* char])) int (var a (* app) (calloc 1 (sizeof app))) - (= (. a highlight) -1) + (= (. a search) (search/make)) + (= (. a base) LAYER-HEIGHT) + (= (. a opacity) 0.5) + (= (. a show-stories) false) + (= (. a show-cursor-rock) true) (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) @@ -646,19 +902,43 @@ (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] "--opacity") 0) more) + (do (++ i) (= (. a opacity) (cast (atof [argv i]) float))) + (&& (== (strcmp [argv i] "--base") 0) more) + (do + (++ i) + (for (var b int 0) (< b 3) (++ b) + (if (== (strcmp [argv i] [base-names b]) 0) + (= (. a base) [base-layers b])))) + (&& (== (strcmp [argv i] "--overlay") 0) more) + (do + (++ i) + (for (var o int 0) (< o 6) (++ o) + (if (== (strcmp [argv i] [overlay-names o]) 0) + (c-bit-or= (. a overlays) (<< (cast 1 u32) o))))) (&& (== (strcmp [argv i] "--rock-depth") 0) more) (do (++ i) (= (. a rock-mode) ROCK-AT-DEPTH) (= (. a rock-value) (atoi [argv i]))) (&& (== (strcmp [argv i] "--rock-y") 0) more) (do (++ i) (= (. a rock-mode) ROCK-AT-Y) (= (. a rock-value) (atoi [argv i]))) + (&& (== (strcmp [argv i] "--mouse") 0) (< (+ i 2) argc)) + (do + (= (. a mouse-x) (cast (atof [argv (+ i 1)]) float)) + (= (. a mouse-y) (cast (atof [argv (+ i 2)]) float)) + (+= i 2)) (&& (== (strcmp [argv i] "--probe") 0) (< (+ i 2) argc)) (do (= (. a probe-x) (atoi [argv (+ i 1)])) (= (. a probe-z) (atoi [argv (+ i 2)])) (+= i 2)) + (== (strcmp [argv i] "--find") 0) + (= (. a find-requested) true) (&& (== (strcmp [argv i] "--highlight") 0) more) - (do (++ i) (= (. a highlight-code) [argv i])) + (do + (++ i) + (if (< (. a highlight-code-count) 32) + (do + (= [(. a highlight-codes) (. a highlight-code-count)] [argv i]) + (++ (. a highlight-code-count))))) (&& (== (strcmp [argv i] "--screenshot") 0) more) (do (++ i) (= (. a screenshot) [argv i])) (snprintf (. a world-path) (sizeof (. a world-path)) "%s" [argv i]))) @@ -674,6 +954,7 @@ (SDL-SetRenderScale (. a renderer) scale scale) (= (. a ctx) (nk-sdl-init (. a window) (. a renderer) (nk-sdl-allocator))) (setup-font (. a ctx) scale) + (setup-style (. a ctx)) (noise-init) (var source world-source) @@ -684,6 +965,7 @@ (var workers int (- (SDL-GetNumLogicalCPUCores) 1)) (= (. a rivers) (river-config/defaults)) (= (. a tiles) (tiles/start (. source settings) (. a rivers) (. a assets-dir) workers)) + (= (. a hover-worker) (hover-worker/start (. a assets-dir))) (if (<= (. a zoom) 0) (= (. a zoom) 4)) (if (! (apply a source)) @@ -693,17 +975,17 @@ (go-to-spawn a) (if (|| (. a probe-x) (. a probe-z)) (probe-at a (. a probe-x) (. a probe-z))) - (if (. a highlight-code) - (do - (for (var i int 0) (< i (. a rocks rock-count)) (++ i) - (if (== (strcmp (+ [(. a rocks rock-codes) i] 5) (. a highlight-code)) 0) - (= (. a highlight) i))) - (update-palette a))) + (for (var k int 0) (< k (. a highlight-code-count)) (++ k) + (for (var i int 0) (< i (. a rocks rock-count)) (++ i) + (if (== (strcmp (+ [(. a rocks rock-codes) i] 5) [(. a highlight-codes) k]) 0) + (c-bit-or= (. a highlight) (<< (cast 1 u32) i))))) + (update-palette a) (var running bool true) (while running ;; Nothing left to generate: sleep until something happens. - (if (&& (== (. a pending) 0) (! (. a dragging)) (! (. a screenshot))) + (if (&& (== (. a pending) 0) (! (. a dragging)) (! (. a screenshot)) (! (. a searching)) + (! (hover-worker/busy (. a hover-worker)))) (SDL-WaitEventTimeout NULL 500)) (var event SDL-Event) (nk-input-begin (. a ctx)) @@ -713,26 +995,33 @@ (nk-sdl-update-TextInput (. a ctx)) (nk-input-end (. a ctx)) - (draw-settings a) - (draw-layers a) - (draw-info a) - (draw-probe a) + (update-search a) + (update-hover a) + (draw-panels a) + (draw-cursor-rock a) (= (. a ui-hovered) (|| (nk-window-is-any-hovered (. a ctx)) (nk-item-is-any-active (. a ctx)))) (SDL-SetRenderDrawColor (. a renderer) 24 32 40 255) (SDL-RenderClear (. a renderer)) + (var view SDL-FRect (map-rect a)) + (var clip SDL-Rect #(SDL-Rect : (cast (. view x) int) (cast (. view y) int) + (cast (. view w) int) (cast (. view h) int))) + (SDL-SetRenderClipRect (. a renderer) (& clip)) (draw-map a) (draw-markers a) + (SDL-SetRenderClipRect (. a renderer) NULL) (nk-sdl-render (. a ctx) NK-ANTI-ALIASING-ON) - (if (&& (. a screenshot) (== (. a pending) 0)) + (if (&& (. a screenshot) (== (. a pending) 0) (! (. a searching)) (! (. a find-requested))) (do (if (! (save-screenshot a)) (fprintf stderr "%s: %s\n" (. a screenshot) (SDL-GetError))) (= running false))) (SDL-RenderPresent (. a renderer))) + (search/stop (. a search)) (.stop (. a tiles)) - (release-hover a) + (release-probe-terra a) + (hover-worker/stop (. a hover-worker)) (strata/free (. a rocks)) (worldgen/free (. a g)) (nk-sdl-shutdown (. a ctx))