carve caves as GenCaves does, and check them block by block

A cave's course depends only on the seed and the chunk it starts in,
so each source chunk's tunnels are traced once, in its own
coordinates, as the ellipsoids they carve. A chunk is carved as the
game carves it: its eleven by eleven sources in order, each ellipsoid
skipped where the terrain's water or earlier lava is near, lava and
basalt below y 12 by geologic activity, the height map lowered where a
cave opens. The terrain now keeps whether each block is solid, which
makes the water exact; story locations keep caves away as theirs do.

vsmv verify compares every stored block below our surface: 96.1% of
the 18.5 million carved blocks agree, and of the rest all but 0.28%
are what later passes put in caves or made of air themselves, the
vugs and the block layers above all. Carving lifts the columns
matching the stored height by 71,379.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-10 15:34:00 +03:00
parent 2c16f8caef
commit aeb6d3be22
10 changed files with 930 additions and 11 deletions

View File

@@ -11,6 +11,7 @@
(import terra)
(import strata)
(import ores)
(import caves)
(import img)
(define MAP-KINDS 6)
@@ -337,6 +338,179 @@
(terra/free t)
(return (if (< (* unexplained 10000) compared) 0 1)))
;;; The save's block, as the cave check sees it. Obsidian and anything
;;; else of basalt, ore or cracked rock, is what the deposits made of it.
(define SAVED-AIR 0)
(define SAVED-BASALT 1)
(define SAVED-ROCK 2)
(define SAVED-OTHER 3)
(define SAVED-FROM-BASALT 4)
(define SAVED-KINDS 5)
;;; Ours: the CAVE- states, and one more for the gaps the terrain itself
;;; leaves below its surface.
(define TERRAIN-AIR 4)
(define OUR-KINDS 5)
(var cave-state-names [(* const char) 5] #("rock" "air" "lava" "basalt" "terrain air"))
(var saved-names [(* const char) 5] #("air" "basalt" "rock" "other" "deposit in basalt"))
(define MAX-STRUCTURES 8192)
;;; Whether block (x, y, z) is inside one of N structure boxes.
(fn in-structure ((boxes (* const i32)) (n int) (x int) (y int) (z int)) bool
(for (var i int 0) (< i n) (++ i)
(var b (* const i32) (+ boxes (* i 6)))
(if (&& (>= x [b 0]) (<= x [b 3]) (>= y [b 1]) (<= y [b 4]) (>= z [b 2]) (<= z [b 5]))
(return true)))
(return false))
;;; Carves every stored chunk column and compares its blocks below our
;;; terrain height with the save's: where we carve air or lava the save
;;; should have air (lava is a fluid, so its solid block is air), where
;;; we put basalt it should have basalt, and where we leave rock it
;;; should not have air. Later passes put things in caves too, so air
;;; next to "other" is not wrong by itself. Also compares the terrain
;;; heights after carving with the stored ones.
(pub fn verify-caves ((s (* save)) (g (* worldgen)) (config river-config) (dir (* const char))) int
(var e extent (save/extent s "mapchunk"))
(var w int (* (+ (- (. e x1) (. e x0)) 1) 32))
(var h int (* (+ (- (. e z1) (. e z0)) 1) 32))
(var diff image (image/make w h))
(var t0 clock-t (clock))
(var t (* terra) (terra/make g config))
(var c (* caves) (caves/make g))
(var size-y int (. s size-y))
(var saved (* u8) (malloc (+ (. s block-count) 1)))
(for (var id int 0) (< id (. s block-count)) (++ id)
(var code (* const char) (save/block-code s id))
(= [saved id] SAVED-OTHER)
(var len size-t (strlen code))
(if (== id 0) (= [saved id] SAVED-AIR)
(== (strcmp code "rock-basalt") 0) (= [saved id] SAVED-BASALT)
(|| (== (strcmp code "rock-obsidian") 0)
(&& (> len 7) (== (strcmp (+ code (- len 7)) "-basalt") 0)))
(= [saved id] SAVED-FROM-BASALT)
(|| (== (strncmp code "rock-" 5) 0) (== (strncmp code "crackedrock-" 12) 0)
(== (strncmp code "ore-" 4) 0))
(= [saved id] SAVED-ROCK)))
(var states (* u8) (malloc (* 1024 size-y)))
(var blocks (* i32) (malloc (* CHUNK-VOLUME (sizeof i32))))
(var tally [i64 25])
(var words int (terra/solid-words t))
(var solid (* u32) (malloc (* 1024 words (sizeof u32))))
(memset tally 0 (sizeof tally))
;; the structure boxes of the regions around the column
(var boxes (* i32) (malloc (* MAX-STRUCTURES 6 (sizeof i32))))
(var box-count int 0)
(var boxes-rx int -1)
(var boxes-rz int -1)
(var missed-in i64 0)
(var missed-out i64 0)
(var over-in i64 0)
(var over-out i64 0)
(var missed-surface i64 0)
(var heights [u16 1024])
(var before [int 1024])
(var after [int 1024])
(var columns i64 0)
(var height-before i64 0)
(var height-after i64 0)
(var m chunk-maps)
(for (var cz int (. e z0)) (<= cz (. e z1)) (++ cz)
(for (var cx int (. e x0)) (<= cx (. e x1)) (++ cx)
(if (! (save/chunk s cx cz (& m)))
(continue))
(terra/chunk-solidity t cx cz heights solid)
;; the region's structures; boxes reach into neighbours only a
;; little, so the region itself is enough for a tally
(if (|| (!= boxes-rx (/ cx 16)) (!= boxes-rz (/ cz 16)))
(do
(= boxes-rx (/ cx 16))
(= boxes-rz (/ cz 16))
(= box-count (save/structure-boxes s boxes-rx boxes-rz boxes MAX-STRUCTURES))))
(var top int 0)
(for (var i int 0) (< i 1024) (++ i)
(= [before i] [heights i])
(= [after i] [heights i])
(if (> [heights i] top) (= top [heights i])))
(caves/carve-column c cx cz after solid words states)
(for (var i int 0) (< i 1024) (++ i)
(++ columns)
(if (== [before i] [(. m terrain-height) i]) (++ height-before))
(if (== [after i] [(. m terrain-height) i]) (++ height-after)))
(var over [bool 1024])
(var missed [bool 1024])
(memset over 0 (sizeof over))
(memset missed 0 (sizeof missed))
(for (var cy int 0) (&& (<= (* cy 32) top) (< (* cy 32) size-y)) (++ cy)
(if (! (save/chunk-blocks s cx cy cz blocks))
(continue))
(for (var i int 0) (< i CHUNK-VOLUME) (++ i)
(var y int (+ (* cy 32) (/ i 1024)))
(var col int (% i 1024))
(if (|| (< y 1) (> y [before col]))
(continue))
(var id i32 [blocks i])
(var theirs int (if (&& (>= id 0) (< id (. s block-count))) [saved id] SAVED-OTHER))
(var ours int [states (+ (* y 1024) col)])
(if (&& (== ours CAVE-UNTOUCHED)
(== (& (>> [solid (+ (* col words) (/ y 32))] (% y 32)) 1) 0))
(= ours TERRAIN-AIR))
(++ [tally (+ (* ours SAVED-KINDS) theirs)])
(var bx int (+ (* cx 32) (% col 32)))
(var bz int (+ (* cz 32) (/ col 32)))
(if (&& (== ours CAVE-AIR) (== theirs SAVED-ROCK))
(if (in-structure boxes box-count bx y bz)
(++ over-in)
(do (++ over-out) (= [over col] true))))
;; air the save has and we don't: structures, the save's own
;; surface block (the block layers take some away, glaciers
;; above all) and anything above it, or ours to explain
(if (&& (== ours CAVE-UNTOUCHED) (== theirs SAVED-AIR))
(if (in-structure boxes box-count bx y bz)
(++ missed-in)
(>= y [(. m terrain-height) col])
(++ missed-surface)
(do (++ missed-out) (= [missed col] true))))))
(for (var i int 0) (< i 1024) (++ i)
(var colour u32 (grey 96))
(if [missed i] (= colour 0x2020FF))
(if [over i] (= colour 0xFF2020))
(.set (& diff) (+ (* (- cx (. e x0)) 32) (% i 32)) (+ (* (- cz (. e z0)) 32) (/ i 32)) colour))))
(printf "caves ours by the save's, below our terrain height, %.1f s:\n"
(/ (cast (- (clock) t0) double) CLOCKS-PER-SEC))
(for (var o int 0) (< o OUR-KINDS) (++ o)
(printf " %-11s" [cave-state-names o])
(for (var k int 0) (< k SAVED-KINDS) (++ k)
(printf " %s %lld" [saved-names k] [tally (+ (* o SAVED-KINDS) k)]))
(printf "\n"))
(var carved i64 (+ [tally (+ (* CAVE-AIR SAVED-KINDS) SAVED-AIR)]
[tally (+ (* CAVE-LAVA SAVED-KINDS) SAVED-AIR)]
[tally (+ (* CAVE-BASALT SAVED-KINDS) SAVED-BASALT)]
[tally (+ (* CAVE-BASALT SAVED-KINDS) SAVED-FROM-BASALT)]))
(var ours-carved i64 0)
(for (var k int 0) (< k SAVED-KINDS) (++ k)
(+= ours-carved (+ [tally (+ (* CAVE-AIR SAVED-KINDS) k)] [tally (+ (* CAVE-LAVA SAVED-KINDS) k)]
[tally (+ (* CAVE-BASALT SAVED-KINDS) k)])))
(printf " %lld of our %lld carved blocks agree (%.2f%%)\n" carved ours-carved
(if (> ours-carved 0) (/ (* 100.0 carved) ours-carved) 0.0))
(printf " air the save has where we leave rock: %lld in generated structures, %lld at or above the save's surface, %lld otherwise\n"
missed-in missed-surface missed-out)
(printf " rock the save has where we carve air: %lld in generated structures, %lld otherwise\n"
over-in over-out)
(printf " terrain height: %lld/%lld columns before carving, %lld after\n" height-before columns
height-after)
(var path [char 1024])
(snprintf path (sizeof path) "%s/diff-caves.png" dir)
(.write (& diff) path)
(.release (& diff))
(free states)
(free blocks)
(free saved)
(free boxes)
(free solid)
(caves/free c)
(terra/free t)
(return (if (< (* (+ missed-out over-out) 200) ours-carved) 0 1)))
;;; Recomputes the prospecting pick readings players took in the save
;;; and compares each mineral's TotalFactor: per reading, every
;;; mineral the save has or this reads above nothing.