read the save's chunk blocks, and check the rock below the surface

A chunk's solid blocks are a palette of block ids, raw or zstd, and a
zstd frame of bit planes; a layer of a lone 0 is all air. With them,
vsmv verify compares the rock of every stored block below the surface
with the strata laid against our own heights, which predate the dry
seabed raise: 99.91% of 1.38 billion blocks match. Lava basalt near the
bottom, the granite of cave walls and structures, and columns whose
height differs explain all but 0.0011%, nearly all of that in one area
at the explored edge whose layer boundaries are not explained yet.

What the save has instead of stratum rock is tallied too, as the
ground truth the deposits phase can be checked against.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-10 11:59:26 +03:00
parent 53950a465c
commit 2c16f8caef
6 changed files with 335 additions and 17 deletions

View File

@@ -7,7 +7,7 @@
;;; data itself, matched against the map sizes in TerraGenConfig.
(pub include sqlite3.h)
(include stdio.h stdlib.h string.h)
(include stdio.h stdlib.h string.h zstd.h)
(import pb)
(import maps)
@@ -15,6 +15,7 @@
(pub define REGION-SIZE 512)
(pub define CHUNK-SIZE 32)
(pub define CHUNK-AREA 1024)
(pub define CHUNK-VOLUME 32768)
(pub define MAX-STORIES 32)
(pub record chunk-maps
@@ -443,6 +444,92 @@
(sqlite3-finalize stmt)
(return found))
;;; Chunks ------------------------------------------------------------
;;; A zstd frame, whole, into a fresh buffer; NULL if it isn't one.
(fn unzstd ((p (* const u8)) (len size-t) (out-len (* size-t))) (* u8)
(var size u64 (ZSTD-getFrameContentSize p len))
(if (>= size ZSTD-CONTENTSIZE-ERROR)
(return NULL))
(var out (* u8) (malloc (+ size 1)))
(var got size-t (ZSTD-decompress out size p len))
(if (ZSTD-isError got)
(do (free out) (return NULL)))
(= (* out-len) got)
(return out))
;;; A chunk's block layer: an int32, then the palette of block ids,
;;; raw in -n bytes if it is negative and zstd in n if not; then a zstd
;;; frame of bit planes, 4096 bytes each, plane k holding bit k of each
;;; block's palette index, block i at bit i % 8 of byte i / 8. A layer
;;; of 0 alone holds no blocks: all air.
(fn read-blocks ((p (* const u8)) (len size-t) (out (* i32))) bool
(if (< len 4)
(return false))
(var n i32 (cast (c-bit-or [p 0] (<< [p 1] 8) (<< [p 2] 16) (<< (cast [p 3] u32) 24)) i32))
(if (&& (== n 0) (== len 4))
(do
(memset out 0 (* CHUNK-VOLUME (sizeof i32)))
(return true)))
(var palette (* u8) NULL)
(var palette-len size-t 0)
(var rest (* const u8) NULL)
(if (< n 0)
(do
(= palette-len (cast (- n) size-t))
(if (> (+ 4 palette-len) len) (return false))
(= palette (malloc palette-len))
(memcpy palette (+ p 4) palette-len)
(= rest (+ p 4 palette-len)))
(do
(if (> (+ 4 (cast n size-t)) len) (return false))
(= palette (unzstd (+ p 4) (cast n size-t) (& palette-len)))
(= rest (+ p 4 n))))
(var planes-len size-t 0)
(var planes (* u8) (if palette (unzstd rest (cast (- (+ p len) rest) size-t) (& planes-len)) NULL))
(var count int (cast (/ palette-len 4) int))
(var bits int (cast (/ planes-len 4096) int))
(var ok bool (&& palette planes (> count 0)))
(if ok
(do
(var index (* i32) (calloc CHUNK-VOLUME (sizeof i32)))
(for (var k int 0) (< k bits) (++ k)
(var plane (* const u8) (+ planes (* k 4096)))
(for (var i int 0) (< i CHUNK-VOLUME) (++ i)
(if (& (>> [plane (/ i 8)] (% i 8)) 1)
(c-bit-or= [index i] (<< 1 k)))))
(for (var i int 0) (< i CHUNK-VOLUME) (++ i)
(var k int [index i])
(var id i32 -1)
(if (< k count)
(memcpy (& id) (+ palette (* k 4)) 4))
(= [out i] id))
(free index)))
(free palette)
(free planes)
(return ok))
;;; The blocks of stored chunk (cx, cy, cz), by (y * 32 + z) * 32 + x,
;;; as block ids (see save/block-code). False when the save has no such
;;; chunk or its blocks don't read.
(pub fn save/chunk-blocks ((s (* const save)) (cx i32) (cy i32) (cz i32) (out (* i32))) bool
(var stmt (* sqlite3-stmt) NULL)
(var key i64 (c-bit-or (position cx cz) (<< (cast cy i64) 54)))
(var r pb (query-blob s "select data from chunk where position = ?1" key (& stmt)))
(var ok bool false)
(if (! (. r err))
(do
(var wire int)
(var f u32)
(while (> (= f (.field (& r) (& wire))) 0)
(if (== f 1)
(do
(var blocks pb (.bytes (& r)))
(= ok (read-blocks (. blocks p) (.len blocks) out)))
(.skip (& r) wire)))))
(sqlite3-finalize stmt)
(return ok))
;;; The stored extent of a table, in its own units (regions or chunks):
;;; inclusive bounds and the row count.
(pub record extent ((x0 i32) (z0 i32) (x1 i32) (z1 i32) (count i64)))

View File

@@ -165,6 +165,178 @@
(.release (& diff))
(return (if (|| (!= (. t matched) (. t compared)) (> unknown 0)) 1 0)))
;;; What a block of the save is, for the strata check.
(define BLOCK-OTHER -1)
;;; a rock- block whose rock is not a stratum, e.g. a deposit's halite
(define BLOCK-OTHER-ROCK -2)
;;; Kinds of blocks the save has where the strata put rock, by the code
;;; up to its first hyphen, e.g. "ore" or "crackedrock".
(define MAX-KINDS 64)
(record kind-count ((name [char 24]) (count i64)))
(fn count-kind ((kinds (* kind-count)) (n (* int)) (code (* const char))) void
(var len int (cast (strcspn code "-") int))
(for (var i int 0) (< i (* n)) (++ i)
(if (&& (== (cast (strlen (. [kinds i] name)) int) len)
(== (strncmp (. [kinds i] name) code len) 0))
(do (++ (. [kinds i] count)) (return))))
(if (>= (* n) MAX-KINDS)
(return))
(snprintf (. [kinds (* n)] name) 24 "%.*s" len code)
(= (. [kinds (* n)] count) 1)
(++ (* n)))
(fn by-count ((pa (* const void)) (pb (* const void))) int
(var a (* const kind-count) pa)
(var b (* const kind-count) pb)
(return (- (< (. a count) (. b count)) (> (. a count) (. b count)))))
;;; Lava in caves sits at y 11 and below and turns the rock around it
;;; to basalt, a few blocks further up.
(define LAVA-BASALT-TOP 18)
;;; Compares the rock below the surface with the save's blocks, chunk
;;; by chunk: every block from y = 1 up to the lower of our height and
;;; the stored one. The strata are laid against our height, as the game
;;; laid them against its height before the dry seabed raise.
;;;
;;; Where the save has a stratum rock, it should be ours. Three things
;;; written after the strata explain where it isn't: the column's height
;;; differs from ours, so its strata sit differently; basalt around cave
;;; lava near the bottom; and the default rock, which cave walls and
;;; underground structures are built of. Where the save has anything
;;; else, ore, caves, deposits, it is counted by kind.
(pub fn verify-strata ((s (* save)) (g (* worldgen)) (config river-config) (rocks (* strata))
(dir (* const char))) int
(var t (* terra) (terra/make g config))
(var heights [u16 1024])
(var basalt int -1)
(for (var r int 0) (< r (. rocks rock-count)) (++ r)
(if (== (strcmp [(. rocks rock-codes) r] "rock-basalt") 0)
(= basalt r)))
(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))
;; block id -> strata rock index, or a BLOCK- kind
(var rock-of (* int) (malloc (* (+ (. s block-count) 1) (sizeof int))))
(for (var id int 0) (< id (. s block-count)) (++ id)
(var code (* const char) (save/block-code s id))
(= [rock-of id] BLOCK-OTHER)
(if (== (strncmp code "rock-" 5) 0)
(do
(= [rock-of id] BLOCK-OTHER-ROCK)
(for (var r int 0) (< r (. rocks rock-count)) (++ r)
(if (== (strcmp code [(. rocks rock-codes) r]) 0)
(= [rock-of id] r))))))
(var size-y int (. s size-y))
(var columns (* u8) (malloc (* CHUNK-AREA (+ size-y 1))))
(var blocks (* i32) (malloc (* CHUNK-VOLUME (sizeof i32))))
;; ours x theirs, for strata rocks that differ
(var confusion (* i64) (calloc (* MAX-ROCKS MAX-ROCKS) (sizeof i64)))
(var kinds [kind-count MAX-KINDS])
(var kind-n int 0)
(var compared i64 0)
(var matched i64 0)
(var other-rock i64 0)
(var other i64 0)
(var chunks i64 0)
(var unreadable i64 0)
(var other-height i64 0)
(var lava i64 0)
(var default-rock 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))
(var top int 0)
(terra/chunk-heights t cx cz 1 heights)
(for (var i int 0) (< i CHUNK-AREA) (++ i)
(var hi int [heights i])
(if (> hi top) (= top hi))
(strata/column rocks cx cz (% i 32) (/ i 32) hi (+ columns (* i (+ size-y 1)))))
(var bad [bool 1024])
(memset bad 0 (sizeof bad))
(for (var cy int 0) (&& (<= (* cy 32) top) (< (* cy 32) size-y)) (++ cy)
(if (! (save/chunk-blocks s cx cy cz blocks))
(do
(++ unreadable)
(continue)))
(++ chunks)
(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 [(. m terrain-height) col]) (> y [heights col]))
(continue))
(var id i32 [blocks i])
(var theirs int (if (&& (>= id 0) (< id (. s block-count))) [rock-of id] BLOCK-OTHER))
(var ours int [columns (+ (* col (+ size-y 1)) y)])
(if (>= theirs 0)
(do
(++ compared)
(if (== theirs ours)
(++ matched)
(!= [heights col] [(. m terrain-height) col])
(++ other-height)
(&& (== theirs basalt) (<= y LAVA-BASALT-TOP))
(++ lava)
(== theirs (. rocks granite))
(++ default-rock)
(do
(= [bad col] true)
(if (< ours MAX-ROCKS)
(++ [confusion (+ (* ours MAX-ROCKS) theirs)])))))
(== theirs BLOCK-OTHER-ROCK)
(do
(++ other-rock)
(count-kind kinds (& kind-n) (save/block-code s id)))
(do
(++ other)
(count-kind kinds (& kind-n) (if (>= id 0) (save/block-code s id) "?"))))))
(for (var i int 0) (< i CHUNK-AREA) (++ i)
(var colour u32 (if [bad i] 0xFF2020 (grey 96)))
(.set (& diff) (+ (* (- cx (. e x0)) 32) (% i 32)) (+ (* (- cz (. e z0)) 32) (/ i 32))
colour))))
(var unexplained i64 (- compared matched other-height lava default-rock))
(printf "strata %lld/%lld rock blocks below the surface match (%.4f%%), %lld chunks, %.1f s\n"
matched compared (if (> compared 0) (/ (* 100.0 matched) compared) 0.0) chunks
(/ (cast (- (clock) t0) double) CLOCKS-PER-SEC))
(printf " explained: %lld where the height differs, %lld lava basalt, %lld default rock; %lld others (%.4f%%):\n"
other-height lava default-rock unexplained
(if (> compared 0) (/ (* 100.0 unexplained) compared) 0.0))
(if (> unreadable 0)
(printf " %lld chunks missing or unreadable\n" unreadable))
;; the commonest confusions
(for (var shown int 0) (< shown 8) (++ shown)
(var best int -1)
(for (var k int 0) (< k (* MAX-ROCKS MAX-ROCKS)) (++ k)
(if (&& (> [confusion k] 0) (|| (< best 0) (> [confusion k] [confusion best])))
(= best k)))
(if (< best 0)
(break))
(printf " ours %-16s save %-16s %lld\n" [(. rocks rock-codes) (/ best MAX-ROCKS)]
[(. rocks rock-codes) (% best MAX-ROCKS)] [confusion best])
(= [confusion best] 0))
(qsort kinds kind-n (sizeof kind-count) by-count)
(printf " where the save has no stratum rock: %lld blocks, %lld of them other rock;" (+ other other-rock)
other-rock)
(for (var k int 0) (&& (< k kind-n) (< k 12)) (++ k)
(printf " %s %lld" (. [kinds k] name) (. [kinds k] count)))
(printf "\n")
(var path [char 1024])
(snprintf path (sizeof path) "%s/diff-strata.png" dir)
(.write (& diff) path)
(.release (& diff))
(free columns)
(free blocks)
(free confusion)
(free rock-of)
(terra/free t)
(return (if (< (* unexplained 10000) compared) 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.

View File

@@ -88,6 +88,7 @@
(var failed int (+ (verify-stories s g) (verify-regions s g out)
(verify-ores s minerals out)
(verify-readings s g (river-config/defaults) rocks minerals)
(verify-strata s g (river-config/defaults) rocks out)
(verify-rivers s g (river-config/defaults) out)
(verify-heights s g (river-config/defaults) rocks out)))
(save/close s)