show the caves on the map, and find the nearest entrance

A Caves overlay colours each column by how far below the surface its
topmost cave starts, and marks where a cave opens at the surface; its
legend gives each band's share of the view. Find nearest entrance
carves the chunks it scans and looks at every column. The cursor label
and the probe list a column's caves, air or lava, top down.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-10 15:34:26 +03:00
parent aeb6d3be22
commit 82774445af
7 changed files with 312 additions and 37 deletions

View File

@@ -15,9 +15,14 @@
(import terra)
(import strata)
(pub import ores)
(import caves)
;;; MINERAL is the mineral whose reading is wanted, or -1.
(pub record hover-query ((x int) (z int) (mode int) (value int) (mineral int)))
;;; The most cave runs a column's answer lists.
(pub define MAX-CAVE-RUNS 4)
;;; MINERAL is the mineral whose reading is wanted, or -1; CAVES whether
;;; the caves are.
(pub record hover-query ((x int) (z int) (mode int) (value int) (mineral int) (caves bool)))
(pub record hover-result
((valid bool) (query hover-query)
@@ -30,7 +35,10 @@
(landform int) (province int) (climate int) (ocean int) (upheaval int)
(river-distance double) (bank double)
;; the pick's reading of the query's mineral, and its ore map's value
(mineral mineral-reading) (mineral-map double)))
(mineral mineral-reading) (mineral-map double)
;; the column's caves, top down, when asked for
(cave-entrance bool) (cave-runs int) (cave-tops [int MAX-CAVE-RUNS])
(cave-bottoms [int MAX-CAVE-RUNS]) (cave-lava [bool MAX-CAVE-RUNS])))
(pub record hover-worker
((lock pthread-mutex-t) (wake pthread-cond-t) (thread pthread-t) (quit bool)
@@ -50,7 +58,11 @@
;; the ore map last asked for: its mineral and region
(ore-map int-map) (ore-mineral int) (ore-rx int) (ore-rz int)
(plate (* river-plate)) (segments (* int)) (sampler river-sampler)
(column (* u8))))
(column (* u8))
;; the chunk last carved for caves: its heights before and after, and
;; its blocks' states
(caves (* caves)) (cave-cx int) (cave-cz int) (has-cave-chunk bool) (cave-states (* u8))
(cave-before [int 1024]) (cave-after [int 1024]) (cave-solid (* u32))))
(var region-kinds [int 5] #(0 2 1 4 3))
@@ -61,6 +73,9 @@
(if (. k plate) (river-plate/free (. k plate)))
(free (. k ore-map data))
(if (. k minerals) (ores/free (. k minerals)))
(if (. k caves) (caves/free (. k caves)))
(free (. k cave-states))
(free (. k cave-solid))
(if (. k rocks) (strata/free (. k rocks)))
(if (. k tr) (terra/free (. k tr)))
(if (. k g) (worldgen/free (. k g)))
@@ -110,6 +125,27 @@
(= (. 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])
(if (&& (. q caves) (. k caves))
(do
(var cx int (/ x 32))
(var cz int (/ z 32))
(if (|| (! (. k has-cave-chunk)) (!= (. k cave-cx) cx) (!= (. k cave-cz) cz))
(do
(var heights [u16 1024])
(terra/chunk-solidity (. k tr) cx cz heights (. k cave-solid))
(for (var i int 0) (< i 1024) (++ i)
(= [(. k cave-before) i] [heights i])
(= [(. k cave-after) i] [heights i]))
(caves/carve-column (. k caves) cx cz (. k cave-after) (. k cave-solid)
(terra/solid-words (. k tr)) (. k cave-states))
(= (. k cave-cx) cx)
(= (. k cave-cz) cz)
(= (. k has-cave-chunk) true)))
(var i int (+ (* (% z 32) 32) (% x 32)))
(= (. r cave-entrance) (< [(. k cave-after) i] [(. k cave-before) i]))
(= (. r cave-runs) (caves/runs (. k caves) (. k cave-states) (% x 32) (% z 32)
[(. k cave-before) i] (. r cave-tops) (. r cave-bottoms)
(. r cave-lava) MAX-CAVE-RUNS))))
(if (&& (. k minerals) (>= (. q mineral) 0) (< (. q mineral) (. (. k minerals) count)))
(do
(if (|| (! (. k ore-map data)) (!= (. k ore-mineral) (. q mineral)) (!= (. k ore-rx) rx)
@@ -169,6 +205,9 @@
(= (. k tr) (terra/make (. k g) (. w rivers)))
(= (. k rocks) (strata/make (. k g) (. w assets-dir)))
(= (. k minerals) (ores/make settings (. w assets-dir) (. k rocks)))
(= (. k caves) (caves/make (. k g)))
(= (. k cave-states) (malloc (* 1024 (. settings size-y))))
(= (. k cave-solid) (malloc (* 1024 (terra/solid-words (. k tr)) (sizeof u32))))
(= (. k sampler) (river-sampler/make (& (. w rivers)) (. settings size-y)))
(= (. k column) (malloc (+ (. settings size-y) 1)))))
(= (. k generation) generation)))
@@ -207,6 +246,7 @@
(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 query mineral) (. q mineral))
(!= (. w query caves) (. q caves))
(! (. w result valid)))
(do
(= (. w query) q)

View File

@@ -1,6 +1,7 @@
;;; Finds the block nearest a point where one of a set of rocks lies, at
;;; the surface, at a depth below it, at a Y or at any depth; or where
;;; the prospecting pick reads a mineral at least so high.
;;; the surface, at a depth below it, at a Y or at any depth; where the
;;; prospecting pick reads a mineral at least so high; or where a cave
;;; opens at the surface.
;;;
;;; Regions are taken in order of their distance from the point and
;;; sampled every 16 blocks by several threads at once, each with its
@@ -17,11 +18,14 @@
(import terra)
(import strata)
(import ores)
(import caves)
(pub define SEARCH-RADIUS-REGIONS 24)
;;; The mode that looks for a mineral: the value is the mineral, the
;;; mask the least reading category.
(pub define SEARCH-MINERAL 4)
;;; The mode that looks for a cave entrance.
(pub define SEARCH-CAVE-ENTRANCE 5)
(define SEARCH-STEP 16)
(pub define MAX-SEARCH-THREADS 16)
@@ -44,10 +48,11 @@
(return (- (> a b) (< a b))))
;;; A thread's generators, and the searched mineral's ore map of the
;;; region it last scanned in.
;;; region it last scanned in; for caves, a chunk's terrain and states.
(record scanner
((tr (* terra)) (rocks (* strata)) (minerals (* ores)) (column (* u8))
(ore-map int-map) (ore-rx int) (ore-rz int)))
(ore-map int-map) (ore-rx int) (ore-rz int)
(caves (* caves)) (solid (* u32)) (states (* u8))))
(fn region-distance ((s (* const search)) (rx int) (rz int)) double
(var x0 double (* rx 512.0))
@@ -112,6 +117,20 @@
;;; Every STEP-th column of chunk (cx, cz) against the mask.
(fn scan-chunk ((s (* search)) (sc (* scanner)) (cx int) (cz int) (step int)) void
(var heights [u16 1024])
;; caves are carved a chunk at a time, so every column is looked at:
;; an entrance can be a block wide
(if (== (. s mode) SEARCH-CAVE-ENTRANCE)
(do
(var carved [int 1024])
(terra/chunk-solidity (. sc tr) cx cz heights (. sc solid))
(for (var i int 0) (< i 1024) (++ i)
(= [carved i] [heights i]))
(caves/carve-column (. sc caves) cx cz carved (. sc solid) (terra/solid-words (. sc tr))
(. sc states))
(for (var i int 0) (< i 1024) (++ i)
(if (< [carved i] [heights i])
(offer s (+ (* cx 32) (% i 32)) (+ (* cz 32) (/ i 32)))))
(return)))
(terra/chunk-heights (. sc tr) cx cz step heights)
(for (var lz int 0) (< lz 32) (+= lz step)
(for (var lx int 0) (< lx 32) (+= lx step)
@@ -132,7 +151,13 @@
(if (&& (. sc rocks) (== (. s mode) SEARCH-MINERAL))
(= (. sc minerals) (ores/make (. s settings) (. s assets-dir) (. sc rocks))))
(= (. sc column) (malloc (+ (. s settings size-y) 1)))
(var ready bool (&& (. sc tr) (. sc rocks) (|| (!= (. s mode) SEARCH-MINERAL) (. sc minerals))))
(if (&& g (. sc tr) (== (. s mode) SEARCH-CAVE-ENTRANCE))
(do
(= (. sc caves) (caves/make g))
(= (. sc solid) (malloc (* 1024 (terra/solid-words (. sc tr)) (sizeof u32))))
(= (. sc states) (malloc (* 1024 (. s settings size-y))))))
(var ready bool (&& (. sc tr) (. sc rocks) (|| (!= (. s mode) SEARCH-MINERAL) (. sc minerals))
(|| (!= (. s mode) SEARCH-CAVE-ENTRANCE) (. sc caves))))
(while ready
(pthread-mutex-lock (& (. s lock)))
(var i int (. s next))
@@ -172,6 +197,9 @@
(scan-chunk s (& sc) cx cz 1)))))
(free (. sc column))
(free (. sc ore-map data))
(if (. sc caves) (caves/free (. sc caves)))
(free (. sc solid))
(free (. sc states))
(if (. sc minerals) (ores/free (. sc minerals)))
(if (. sc rocks) (strata/free (. sc rocks)))
(if (. sc tr) (terra/free (. sc tr)))

View File

@@ -21,6 +21,7 @@
(import terra)
(import strata)
(import ores)
(import caves)
(pub define MAX-TILES 65536)
;;; Not worldgen maps: Rivers' distance to the nearest river, the
@@ -31,6 +32,7 @@
(pub define LAYER-HEIGHT 7)
(pub define LAYER-ROCK 8)
(pub define LAYER-MINERAL 9)
(pub define LAYER-CAVES 10)
(pub define ROCK-AT-SURFACE 0)
(pub define ROCK-AT-DEPTH 1)
(pub define ROCK-AT-Y 2)
@@ -38,6 +40,8 @@
(pub define ROCK-ANY-DEPTH 3)
;;; Mineral tile values: this plus the reading's category.
(pub define MINERAL-VALUE-BASE 64)
;;; Cave tile values: this plus the column's cave category.
(pub define CAVE-VALUE-BASE 80)
;;; Rock tile values besides rock indices.
(pub define VALUE-WATER 253)
(pub define VALUE-AIR 254)
@@ -343,6 +347,44 @@
(= (* size) res)
(return out))
;;; The cave tile: per sample, CAVE-VALUE-BASE plus its column's cave
;;; category. Caves are carved chunk by chunk, and a chunk needs every
;;; block of its terrain, so this costs a whole chunk per sample when
;;; zoomed out.
(fn render-caves ((tr (* terra)) (c (* caves)) (k tile-key) (size (* int))) (* u8)
(var res int (. k stratum))
(var step int (/ 512 res))
(var out (* u8) (malloc (* res res)))
(var heights [u16 1024])
(var carved [int 1024])
(var words int (terra/solid-words tr))
(var solid (* u32) (malloc (* 1024 words (sizeof u32))))
(var states (* u8) (malloc (* 1024 (. tr size-y))))
(var per-chunk int (/ 32 step))
(if (< per-chunk 1) (= per-chunk 1))
(for (var cz int 0) (< cz 16) (++ cz)
(for (var cx int 0) (< cx 16) (++ cx)
(if (|| (!= (% (* cx 32) step) 0) (!= (% (* cz 32) step) 0))
(continue))
(var chunk-x int (+ (* (. k rx) 16) cx))
(var chunk-z int (+ (* (. k rz) 16) cz))
(terra/chunk-solidity tr chunk-x chunk-z heights solid)
(for (var i int 0) (< i 1024) (++ i)
(= [carved i] [heights i]))
(caves/carve-column c chunk-x chunk-z carved solid words states)
(for (var dz int 0) (< dz per-chunk) (++ dz)
(for (var dx int 0) (< dx per-chunk) (++ dx)
(var lx int (* dx step))
(var lz int (* dz step))
(var i int (+ (* lz 32) lx))
(var cat int (caves/category c states lx lz [heights i] [carved i]))
(= [out (+ (* (+ (/ (* cz 32) step) dz) res) (/ (* cx 32) step) dx)]
(cast (+ CAVE-VALUE-BASE cat) u8))))))
(free solid)
(free states)
(= (* size) res)
(return out))
;;; Workers -------------------------------------------------------
(fn worker-main ((arg (* void))) (* void)
@@ -354,6 +396,7 @@
(var tr (* terra) NULL)
(var rocks (* strata) NULL)
(var minerals (* ores) NULL)
(var cave-gen (* caves) NULL)
(pthread-mutex-lock (& (. t lock)))
(while (! (. t quit))
(if (>= (. t queue-pos) (. t queue-len))
@@ -378,6 +421,8 @@
(= tr NULL)
(if minerals (ores/free minerals))
(= minerals NULL)
(if cave-gen (caves/free cave-gen))
(= cave-gen NULL)
(if rocks (strata/free rocks))
(= rocks NULL)
(if g (worldgen/free g))
@@ -386,6 +431,7 @@
(if g (= tr (terra/make g rivers)))
(if g (= rocks (strata/make g (. t assets-dir))))
(if rocks (= minerals (ores/make settings (. t assets-dir) rocks)))
(if g (= cave-gen (caves/make g)))
(= (. cache sampler) (river-sampler/make (& rivers) (. settings size-y)))
(= g-generation generation)))
(var size int 0)
@@ -399,6 +445,8 @@
(= values (render-rocks tr rocks key (& size)))
(&& tr rocks minerals (== (. key map) LAYER-MINERAL))
(= values (render-minerals tr rocks minerals key (& size)))
(&& tr cave-gen (== (. key map) LAYER-CAVES))
(= values (render-caves tr cave-gen key (& size)))
g
(= px (render g key (& size))))
@@ -416,6 +464,7 @@
(= (. tl state) TILE-QUEUED)))))
(pthread-mutex-unlock (& (. t lock)))
(if minerals (ores/free minerals))
(if cave-gen (caves/free cave-gen))
(if rocks (strata/free rocks))
(if tr (terra/free tr))
(if g (worldgen/free g))

View File

@@ -4,7 +4,7 @@
;;; vs-map-viewer [--assets DIR] [--zoom BLOCKS-PER-POINT]
;;; [--base ocean|rivers|height] [--overlay NAME]... [--opacity F]
;;; [--rock-depth N | --rock-y Y | --rock-any] [--highlight ROCK]... [--find]
;;; [--mineral CODE] [--min-reading NAME] [--find-mineral]
;;; [--mineral CODE] [--min-reading NAME] [--find-mineral] [--find-cave]
;;; [--probe X Z] [--mouse X Y]
;;; [--screenshot FILE.png] [WORLD.vcdbs | serverconfig.json]
;;;
@@ -20,6 +20,7 @@
(import terra)
(import strata)
(import ores)
(import caves)
(import search)
(import hover)
(import settings)
@@ -42,16 +43,17 @@
(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 7] #(0 1 2 3 5 8 9))
(var overlay-names [(* const char) 7]
#("landform" "climate" "province" "upheaval" "strata" "rock" "mineral"))
(var overlay-layers [int 8] #(0 1 2 3 5 8 9 10))
(var overlay-names [(* const char) 8]
#("landform" "climate" "province" "upheaval" "strata" "rock" "mineral" "caves"))
;;; The Rock and Minerals overlays' bits in `overlays'.
(define ROCK-OVERLAY-BIT 32)
(define MINERAL-OVERLAY-BIT 64)
(define OVERLAY-COUNT 7)
(var overlay-labels [(* const char) 7]
(define CAVES-OVERLAY-BIT 128)
(define OVERLAY-COUNT 8)
(var overlay-labels [(* const char) 8]
#("Landform" "Climate" "Geologic province" "Upheaval" "Rock strata thickness" "Rock"
"Minerals"))
"Minerals" "Caves"))
(var rock-mode-names [(* const char) 4] #("Surface" "Depth below surface" "At Y" "Any depth"))
;;; Rock colours, roughly as they look in game; others get a stable one.
@@ -93,6 +95,20 @@
;;; A mineral's name in the panel's list.
(record item-label ((text [char 40])))
;;; The cave categories, caves/category's numbering, and their colours
;;; on the Caves overlay: entrances stand out, then the deeper the
;;; topmost cave, the darker.
(var cave-names [(* const char) 7]
#("none" "entrance" "10 deep or less" "11-25 deep" "26-50 deep" "51-100 deep" "deeper"))
(var cave-colours [u32 7]
#(0x000000 0x00E5FF 0xFFD54F 0xFF9800 0xE64A19 0x8E24AA 0x303F9F))
;;; What Find nearest is looking for, for which panel shows its status.
(define FIND-ROCK 0)
(define FIND-MINERAL 1)
(define FIND-CAVE 2)
(define MAX-PROBE-CAVES 16)
;;; The strata of the column clicked last: top down, as (rock, top y,
;;; bottom y) runs; the pick's readings there, highest first; and the
;;; ore maps of the minerals the pick can't read.
@@ -100,7 +116,9 @@
(rocks [int 64]) (tops [int 64]) (bottoms [int 64])
(reading-count int) (reading-minerals [int MAX-MINERALS])
(readings [mineral-reading MAX-MINERALS])
(map-count int) (map-minerals [int MAX-MINERALS]) (map-factors [double MAX-MINERALS])))
(map-count int) (map-minerals [int MAX-MINERALS]) (map-factors [double MAX-MINERALS])
(cave-entrance bool) (cave-count int) (cave-tops [int MAX-PROBE-CAVES])
(cave-bottoms [int MAX-PROBE-CAVES]) (cave-lava [bool MAX-PROBE-CAVES])))
(record app
((window (* SDL-Window)) (renderer (* SDL-Renderer)) (ctx (* struct nk-context))
@@ -124,7 +142,11 @@
(mineral-items [* const char MAX-MINERALS]) (mineral-ids [int MAX-MINERALS])
(mineral-item-count int)
(mineral-coverage [i64 256]) (mineral-coverage-total i64)
(min-reading int) (find-mineral-requested bool) (searching-mineral bool)
(min-reading int) (find-mineral-requested bool)
;; caves: their share of the view per category, the probe's carver
(cave-coverage [i64 256]) (cave-coverage-total i64) (find-cave-requested bool)
(probe-caves (* caves))
(search-kind int)
(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
@@ -222,7 +244,11 @@
(fn release-probe-terra ((a (* app))) void
(if (. a probe-terra)
(terra/free (. a probe-terra)))
(= (. a probe-terra) NULL))
(= (. a probe-terra) NULL)
;; it carves with the generator of the world being let go
(if (. a probe-caves)
(caves/free (. a probe-caves)))
(= (. a probe-caves) NULL))
;;; The colour of rock index I.
(fn rock-colour ((a (* app)) (i int)) u32
@@ -252,6 +278,9 @@
(= [p MINERAL-VALUE-BASE] 0)
(for (var c int 1) (< c READING-CATEGORIES) (++ c)
(= [p (+ MINERAL-VALUE-BASE c)] (c-bit-or 0xFF000000 [reading-colours c])))
(= [p CAVE-VALUE-BASE] 0)
(for (var c int 1) (< c CAVE-CATEGORIES) (++ c)
(= [p (+ CAVE-VALUE-BASE c)] (c-bit-or 0xFF000000 [cave-colours c])))
(.set-palette (. a tiles) p))
;;; The highlighted rock when it is the only one, else -1.
@@ -396,7 +425,9 @@
(== layer LAYER-MINERAL)
(do
(= (. k stratum) (height-resolution (. a zoom)))
(= (. k param) (. a mineral))))
(= (. k param) (. a mineral)))
(== layer LAYER-CAVES)
(= (. k stratum) (height-resolution (. a zoom))))
(return k))
;;; Adds a value tile's samples that fall inside the visible world
@@ -444,10 +475,12 @@
(= (. a coverage-total) 0)
(memset (. a mineral-coverage) 0 (sizeof (. a mineral-coverage)))
(= (. a mineral-coverage-total) 0)
(memset (. a cave-coverage) 0 (sizeof (. a cave-coverage)))
(= (. a cave-coverage-total) 0)
(.begin-frame (. a tiles))
;; layer by layer, each nearest the centre first: the base fills in
;; before the overlays start
(var layers [int 9])
(var layers [int 10])
(var layer-count int 0)
(= [layers 0] (. a base))
(= layer-count 1)
@@ -470,6 +503,9 @@
x0 z0 x1 z1)
(&& values (== [layers l] LAYER-MINERAL))
(count-visible (. a mineral-coverage) (& (. a mineral-coverage-total)) values size
(. k rx) (. k rz) x0 z0 x1 z1)
(&& values (== [layers l] LAYER-CAVES))
(count-visible (. a cave-coverage) (& (. a cave-coverage-total)) values size
(. k rx) (. k rz) x0 z0 x1 z1))
(SDL-SetTextureBlendMode tex SDL-BLENDMODE-BLEND)
(SDL-SetTextureAlphaMod tex (if (== l 0) 255 alpha))
@@ -650,7 +686,7 @@
(nk-layout-row-dynamic ctx 24 2)
(if (nk-button-label ctx "Show all")
(do (= (. a highlight) 0) (update-palette a)))
(if (&& (. a searching) (! (. a searching-mineral)))
(if (&& (. a searching) (== (. a search-kind) FIND-ROCK))
(if (nk-button-label ctx "Cancel")
(do
(search/stop (. a search))
@@ -658,7 +694,7 @@
(snprintf (. a search-status) (sizeof (. a search-status)) "cancelled")))
(if (nk-button-label ctx "Find nearest")
(= (. a find-requested) true)))))
(if (&& (* (. a search-status)) (! (. a searching-mineral)))
(if (&& (* (. a search-status)) (== (. a search-kind) FIND-ROCK))
(do
(nk-layout-row-dynamic ctx 34 1)
(nk-label-wrap ctx (. a search-status)))))
@@ -780,7 +816,7 @@
(+ 1 (nk-combo ctx (+ names 1) (- READING-CATEGORIES 1) (- (. a min-reading) 1) 20
(nk-vec2 140 200))))
(nk-layout-row-dynamic ctx 24 1)
(if (&& (. a searching) (. a searching-mineral))
(if (&& (. a searching) (== (. a search-kind) FIND-MINERAL))
(if (nk-button-label ctx "Cancel")
(do
(search/stop (. a search))
@@ -788,11 +824,51 @@
(snprintf (. a search-status) (sizeof (. a search-status)) "cancelled")))
(if (nk-button-label ctx "Find nearest")
(= (. a find-mineral-requested) true)))
(if (&& (* (. a search-status)) (. a searching-mineral))
(if (&& (* (. a search-status)) (== (. a search-kind) FIND-MINERAL))
(do
(nk-layout-row-dynamic ctx 34 1)
(nk-label-wrap ctx (. a search-status)))))
;;; The legend of the Caves overlay, with each category's share of the
;;; view, and Find nearest for a cave entrance.
(fn draw-cave-controls ((a (* app))) void
(var ctx (* struct nk-context) (. a ctx))
(var shown bool (&& (> (. a cave-coverage-total) 0) (!= (& (. a overlays) CAVES-OVERLAY-BIT) 0)))
(nk-layout-row-dynamic ctx 18 1)
(nk-label ctx "The topmost cave below the surface" NK-TEXT-LEFT)
(for (var c int 1) (< c CAVE-CATEGORIES) (++ c)
(nk-layout-row-template-begin ctx 18)
(nk-layout-row-template-push-static ctx 18)
(nk-layout-row-template-push-dynamic ctx)
(nk-layout-row-template-end ctx)
(var colour u32 [cave-colours c])
(nk-button-color ctx (nk-rgb (cast (& (>> colour 16) 255) int) (cast (& (>> colour 8) 255) int)
(cast (& colour 255) int)))
(if shown
(nk-labelf ctx NK-TEXT-LEFT "%-16s %5.1f%%" [cave-names c]
(/ (* 100.0 [(. a cave-coverage) (+ CAVE-VALUE-BASE c)]) (. a cave-coverage-total)))
(nk-label ctx [cave-names c] NK-TEXT-LEFT)))
(nk-layout-row-dynamic ctx 24 1)
(if (&& (. a searching) (== (. a search-kind) FIND-CAVE))
(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 entrance")
(= (. a find-cave-requested) true)))
(if (&& (* (. a search-status)) (== (. a search-kind) FIND-CAVE))
(do
(nk-layout-row-dynamic ctx 34 1)
(nk-label-wrap ctx (. a search-status)))))
(fn draw-caves ((a (* app))) void
(var ctx (* struct nk-context) (. a ctx))
(if (nk-tree-push ctx NK-TREE-TAB "Caves" NK-MAXIMIZED)
(do
(draw-cave-controls a)
(nk-tree-pop ctx))))
(fn draw-minerals ((a (* app))) void
(var ctx (* struct nk-context) (. a ctx))
(if (nk-tree-push ctx NK-TREE-TAB "Minerals" NK-MAXIMIZED)
@@ -849,6 +925,24 @@
(= [(. p tops) (. p count)] y)
(= [(. p bottoms) (. p count)] y)
(++ (. p count)))))
;; the caves through the column
(if (! (. a probe-caves))
(= (. a probe-caves) (caves/make (. a g))))
(var words int (terra/solid-words (. a probe-terra)))
(var solid (* u32) (malloc (* 1024 words (sizeof u32))))
(var states (* u8) (malloc (* 1024 (. a source settings size-y))))
(var heights [u16 1024])
(var carved [int 1024])
(terra/chunk-solidity (. a probe-terra) (/ x 32) (/ z 32) heights solid)
(for (var i int 0) (< i 1024) (++ i)
(= [carved i] [heights i]))
(caves/carve-column (. a probe-caves) (/ x 32) (/ z 32) carved solid words states)
(var ci int (+ (* (% z 32) 32) (% x 32)))
(= (. p cave-entrance) (< [carved ci] [heights ci]))
(= (. p cave-count) (caves/runs (. a probe-caves) states (% x 32) (% z 32) [heights ci]
(. p cave-tops) (. p cave-bottoms) (. p cave-lava) MAX-PROBE-CAVES))
(free solid)
(free states)
;; what the pick reads here: every mineral it can, highest first
(var o (* ores) (. a minerals))
(= (. p reading-count) 0)
@@ -943,6 +1037,17 @@
(* 100 [(. p map-factors) i]))))
(nk-layout-row-dynamic ctx 34 1)
(nk-label-wrap ctx maps)))
(nk-layout-row-dynamic ctx 6 1)
(nk-spacing ctx 1)
(nk-layout-row-dynamic ctx 18 1)
(nk-label ctx (if (. p cave-entrance) "Caves: an entrance here" "Caves") NK-TEXT-LEFT)
(if (== (. p cave-count) 0)
(nk-label ctx "none" NK-TEXT-LEFT))
(for (var i int 0) (< i (. p cave-count)) (++ i)
(var top int [(. p cave-tops) i])
(var bottom int [(. p cave-bottoms) i])
(nk-labelf ctx NK-TEXT-LEFT "%3d-%-3d %-14s %3d" top bottom
(if [(. p cave-lava) i] "lava" "air") (+ (- top bottom) 1)))
(nk-tree-pop ctx))))
;;; The block under the cursor; false when the cursor is not on the
@@ -963,8 +1068,9 @@
(do
(var value int (rock-query-value a))
(var mineral int (if (!= (& (. a overlays) MINERAL-OVERLAY-BIT) 0) (. a mineral) -1))
(var caves bool (!= (& (. a overlays) CAVES-OVERLAY-BIT) 0))
(hover-worker/ask (. a hover-worker)
#(hover-query : bx bz (. a rock-mode) value mineral))))
#(hover-query : bx bz (. a rock-mode) value mineral caves))))
(= (. a hover) (hover-worker/result (. a hover-worker))))
(fn rock-name ((a (* app)) (rock int)) (* const char)
@@ -1019,7 +1125,7 @@
(. a rock-mode) value (. a highlight) (. a center-x) (. a center-z)
(- (SDL-GetNumLogicalCPUCores) 1))
(= (. a searching) true)
(= (. a searching-mineral) false)))
(= (. a search-kind) FIND-ROCK)))
(if (. a find-mineral-requested)
(do
(= (. a find-mineral-requested) false)
@@ -1029,7 +1135,15 @@
SEARCH-MINERAL (. a mineral) (cast (. a min-reading) u32)
(. a center-x) (. a center-z) (- (SDL-GetNumLogicalCPUCores) 1))
(= (. a searching) true)
(= (. a searching-mineral) true)))))
(= (. a search-kind) FIND-MINERAL)))))
(if (. a find-cave-requested)
(do
(= (. a find-cave-requested) false)
(search/start (. a search) (. a source settings) (. a rivers) (. a assets-dir)
SEARCH-CAVE-ENTRANCE 0 0 (. a center-x) (. a center-z)
(- (SDL-GetNumLogicalCPUCores) 1))
(= (. a searching) true)
(= (. a search-kind) FIND-CAVE)))
(if (! (. a searching))
(return))
(var s (* search) (. a search))
@@ -1071,7 +1185,8 @@
(do
(draw-layers a)
(draw-rocks a)
(draw-minerals a)))
(draw-minerals a)
(draw-caves a)))
(nk-end ctx))
;;; A label by the cursor naming the rock under it, as the Rocks
@@ -1102,7 +1217,22 @@
(= [reading 0] 0)
(if (&& (. r valid) (>= (. r query mineral) 0))
(mineral-at-cursor a r reading (sizeof reading)))
(var height float (if [reading 0] 50 30))
;; and the caves through the column, top down
(var caves [char 96])
(= [caves 0] 0)
(if (&& (. r valid) (. r query caves))
(if (. r cave-entrance)
(snprintf caves (sizeof caves) "cave entrance")
(== (. r cave-runs) 0)
(snprintf caves (sizeof caves) "no caves")
(do
(var used int (snprintf caves (sizeof caves) "caves y"))
(for (var i int 0) (&& (< i (. r cave-runs)) (< i 3) (< used 80)) (++ i)
(+= used (snprintf (+ caves used) (- (sizeof caves) used) "%s %d-%d%s" (if (> i 0) "," "")
[(. r cave-tops) i] [(. r cave-bottoms) i]
(if [(. r cave-lava) i] " lava" "")))))))
(var lines int (+ 1 (if [reading 0] 1 0) (if [caves 0] 1 0)))
(var height float (+ 12 (* 18 lines)))
;; 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) 260 height))
@@ -1112,7 +1242,9 @@
(nk-layout-row-dynamic ctx 18 1)
(nk-label ctx text NK-TEXT-LEFT)
(if [reading 0]
(nk-label ctx reading NK-TEXT-LEFT))))
(nk-label ctx reading NK-TEXT-LEFT))
(if [caves 0]
(nk-label ctx caves NK-TEXT-LEFT))))
(nk-end ctx))
;;; Events ---------------------------------------------------------
@@ -1239,6 +1371,8 @@
(do (++ i) (= (. a min-reading) (reading-named [argv i])))
(== (strcmp [argv i] "--find-mineral") 0)
(= (. a find-mineral-requested) true)
(== (strcmp [argv i] "--find-cave") 0)
(= (. a find-cave-requested) true)
(&& (== (strcmp [argv i] "--highlight") 0) more)
(do
(++ i)
@@ -1320,7 +1454,7 @@
(SDL-SetRenderClipRect (. a renderer) NULL)
(nk-sdl-render (. a ctx) NK-ANTI-ALIASING-ON)
(if (&& (. a screenshot) (== (. a pending) 0) (! (. a searching)) (! (. a find-requested))
(! (. a find-mineral-requested)))
(! (. a find-mineral-requested)) (! (. a find-cave-requested)))
(do
(if (! (save-screenshot a))
(fprintf stderr "%s: %s\n" (. a screenshot) (SDL-GetError)))