diff --git a/Makefile b/Makefile index dfacf03..ad7d3bf 100644 --- a/Makefile +++ b/Makefile @@ -4,10 +4,11 @@ SEXC ?= $(HOME)/dev/sex/sexc CFLAGS ?= -O2 -g CFLAGS += -std=c11 -Wall -ffp-contract=off -fwrapv -CPPFLAGS += -Isrc -Ithird_party/nuklear -Ithird_party/stb -IcJSON $(shell pkg-config --cflags sdl3) +CPPFLAGS += -Isrc -Ithird_party/nuklear -Ithird_party/stb -IcJSON $(shell pkg-config --cflags sdl3 libzstd) # Sex writes C with only the parentheses precedence needs. SEX_CFLAGS = -Wno-logical-op-parentheses -Wno-bitwise-op-parentheses -Wno-shift-op-parentheses SDL_LIBS := $(shell pkg-config --libs sdl3) +ZSTD_LIBS := $(shell pkg-config --libs libzstd) B = build @@ -33,12 +34,12 @@ $(B)/stb.o: src/stb.c third_party/stb/stb_image_write.h | $(B) VSMV_OBJ = vsmv cs pb json maps save img dump lcg noise shape worldgen fastnoise rivers terra strata ores settings verify cJSON stb $(B)/vsmv: $(VSMV_OBJ:%=$(B)/%.o) - $(CC) $^ -o $@ -lsqlite3 + $(CC) $^ -o $@ -lsqlite3 $(ZSTD_LIBS) VIEWER_OBJ = viewer tiles search hover settings worldgen fastnoise rivers terra strata ores shape noise lcg maps save pb json cs nk cJSON stb $(B)/vs-map-viewer: $(VIEWER_OBJ:%=$(B)/%.o) - $(CC) $^ -o $@ $(SDL_LIBS) -lsqlite3 + $(CC) $^ -o $@ $(SDL_LIBS) -lsqlite3 $(ZSTD_LIBS) $(B): mkdir -p $@ diff --git a/Plan.org b/Plan.org index 95d42b4..0a8c9f8 100644 --- a/Plan.org +++ b/Plan.org @@ -292,6 +292,25 @@ What the save turned out to be: | 7 | =WorldGenTerrainHeightMap= | | 13 | =TopRockIdMap= (block ids) | +- *=chunk=*: one 32 x 32 x 32 chunk per row, keyed by + =x | z << 27 | y << 54= in chunks. All of this save's chunks are + from 1.22.7 (field 12). + + | Field | Contents | + |-------+----------| + | 1 | solid blocks, see below | + | 8 | block entities | + | 9 | mod data, e.g. ="flowVectors"= | + | 12 | game version that generated it | + + The block layer is an int32 n, then the palette of block ids: raw in + -n bytes if n is negative, zstd-compressed in n bytes if not. Then + one zstd frame of bit planes, 4096 bytes each, plane k holding bit k + of every block's palette index, block =(y * 32 + z) * 32 + x= at bit + i % 8 of byte i / 8. A layer that is just n = 0 holds no blocks: all + air. 53,855 of the 114,140 chunks are like that. Water is not in this + layer; fields 2 and 3 are the likely fluid and light layers, not read + yet. - *Assets.* The six worldgen JSONs (and all 910 asset JSONs) parse after a relaxing pass: comments, unquoted keys and trailing commas. There are 48 landforms, 6 provinces and 16 strata. @@ -469,9 +488,8 @@ Validation: - *Result.* 11,675,804 / 11,675,818 columns match, 99.9999%. The 14 others likely carry structures placed later. A further 12,118 columns have no rock on top. -- *Deeper layers* go through the same code but aren't checked against - chunk blocks. Decoding those blobs is the remaining step, if it is - ever needed. +- *Deeper layers.* Checked against the chunk blocks since; see + "Chunk blocks". Viewer: - *Rock layer.* Modes: surface, depth below the surface, or absolute @@ -654,7 +672,41 @@ Viewer: - Go-to-coordinates, PNG export of the current view. - Later: gravel-beach layer; deposits, see below. -** Next: deposits (planned, after the Sex fixes) +** Chunk blocks (done) +=save/chunk-blocks= decodes a stored chunk's solid blocks (format under +the =chunk= table above), with zstd from the system. + +=vsmv verify= now compares the rock of every stored block from y = 1 +up to the lower of our height and the stored one. The strata are laid +against our height, since the game laid them before the dry seabed +raise changed the stored height. Laying them against the stored height +put a whole raised area (around z 7440-7900) one stratum off. + +Result: 1,375,064,177 of 1,376,283,630 stratum rock blocks match +(99.9114%). The rest: +- 26,042 in columns whose height differs from ours; +- 1,075,584 basalt at y 18 or below: deep cave lava (y 11 and below) + turns the rock around it to basalt; +- 103,165 granite, the default rock, in pockets with air, + =stalagsection-granite=, =sand-granite= and =cobblestone-granite=. + Cave walls and underground structures write it after the strata. + Some of it later turned to =ore-quartz-granite=, so it predates the + deposits. +- 14,662 others (0.0011%), nearly all in one area near the explored + edge (x 1024100-1024700, z 7340-7630). There the layer boundaries sit + a few blocks off ours: peridotite, slate and sandstone, all ordinary + strata. Not explained yet. The province map cache is keyed so that + regions can't collide (=regionMapSize= is 8000 here), so that isn't + it. The check passes under 0.01% unexplained. + +Where the save has no stratum rock (247 million blocks), it tallies what +is there instead: crackedrock 123 million, ore 66 million, soil 20 +million, air 18 million, glacier ice 9 million, gravel, sand, +saltpeter, and 373 thousand other rock (halite, marble, obsidian, +travertine). That is what the deposits, caves and block layers wrote, +and what the deposits phase can be checked against. + +** Next: deposits (planned) Placing the deposits themselves, not just the pick's reading of them. Soil deposits come first: they sit at the surface, so they don't need the 3D rock column, and the steps up to the disc are the first half of @@ -688,15 +740,16 @@ showing them means placing them. gravel. Fertility and grass cover come from =GenBlockLayers= and =blocklayers.json=, which are not ported. Without them, patches show where the game puts them on sand, gravel or rich soil. -- *Checking.* The save stores surface blocks only in chunk blobs, which - aren't decoded yet. Until they are, walk to a few patches in game. +- *Checking.* Against the chunk blocks: clay, peat and compost soil + where the save has them, at the top of the column. *Exact ore deposits.* The same replay, then the disc, sea-level, anywhere and child generators placing into the strata column. That places real ore blocks and makes the pick readings exact: the pick's column holds the ore of its own chunk's deposits. It also reads silver and gold (children of quartz) and peridot (a child of olivine), whose -host is their parent's ore. Checking needs the chunk blocks decoded. +host is their parent's ore. Checking: against the save's ore blocks, +which the strata check already counts (66 million). * Risks diff --git a/Readme.org b/Readme.org index 55fa535..5a0b787 100644 --- a/Readme.org +++ b/Readme.org @@ -21,12 +21,16 @@ against a real save, and matches it: | Rivers' distance to the nearest river | 100% of 6.5 million columns | | Terrain height | 98.2% of 11.7 million columns | | Rock at the surface | all but 14 of 11.7 million columns | +| Rock below the surface | 99.91% of 1.38 billion blocks; see below | | Ore maps, 31 minerals | every pixel of 118 regions | | Prospecting pick readings | 38 of 42 in the game's category; see Limits | All but 0.02% of the other heights come from two passes that run after the strata are laid: caves that open the surface, and the raise of dry -seabeds. Neither changes which rock is where. +seabeds. Neither changes which rock is where. Below the surface, all +but 0.0011% of the other rock comes from later passes too: lava in deep +caves turns the rock around it to basalt, and cave walls and +underground structures are built of granite. =Plan.org= has the details. ** Building @@ -35,6 +39,7 @@ You need: - a C compiler; - SDL3 (=brew install sdl3=); - SQLite3; +- zstd (=brew install zstd=); - a checkout of [[https://github.com/DaveGamble/cJSON][cJSON]] in =./cJSON=. Nuklear and stb_image_write are vendored in =third_party/=. @@ -186,14 +191,13 @@ save: | =vsmv dump SAVE.vcdbs OUT= | PNGs of the maps the save stored | | =vsmv verify SAVE.vcdbs DIR OUT= | regenerates everything the save stored and compares it; diff images go to OUT | -=verify= takes a few minutes, most of it on the terrain heights of -every stored chunk. It also compares the ore maps, and the prospecting -pick readings players took in the save, if there are any. +=verify= takes about five minutes, most of it on the terrain heights of +every stored chunk. It also compares the ore maps, the prospecting +pick readings players took in the save, if there are any, and the rock +of every stored block below the surface. ** Limits - Only the /realistic/ world climate is supported, not /patchy/. -- Strata below the surface use the same code as the surface rock, - which is checked, but they are not checked against the save's blocks. - Rivers' settings are not stored in a save. Without a =rivers.json=, the mod's defaults are assumed. - Other mods that change worldgen are not modelled. diff --git a/src/save.sex b/src/save.sex index 5c9c071..80d8057 100644 --- a/src/save.sex +++ b/src/save.sex @@ -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))) diff --git a/src/verify.sex b/src/verify.sex index f3cfbc4..8cb6382 100644 --- a/src/verify.sex +++ b/src/verify.sex @@ -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. diff --git a/src/vsmv.sex b/src/vsmv.sex index 243021b..ef42ce9 100644 --- a/src/vsmv.sex +++ b/src/vsmv.sex @@ -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)