;;; Reads a Vintage Story save (.vcdbs): the world's settings, and the ;;; maps worldgen stored per map region and per map chunk. This is the ;;; ground truth the generator is checked against. ;;; ;;; The save is SQLite with protobuf blobs. The message classes live ;;; in the closed engine, so the field numbers below were read off the ;;; data itself, matched against the map sizes in TerraGenConfig. (pub include sqlite3.h) (include stdio.h stdlib.h string.h zstd.h) (import pb) (import maps) ;;; RegionSize is the closed engine's; Rivers hardcodes the same 512. (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 ((terrain-height [u16 CHUNK-AREA]) (rain-height [u16 CHUNK-AREA]) (top-rock [i32 CHUNK-AREA]) (river-distance [u16 CHUNK-AREA]) (has-river-distance bool))) (pub record story-location ((code [char 64]) (x i32) (y i32) (z i32))) ;;; One mineral of a prospecting pick reading a player took: where, ;;; and the reading's TotalFactor and PartsPerThousand. (pub record pick-reading ((x int) (y int) (z int) (code [char 32]) (total double) (ppt double))) (pub record save ((db (* sqlite3)) (seed i32) (size-x i32) (size-y i32) (size-z i32) (config-keys (* * char)) (config-values (* * char)) (config-count int) (block-codes (* * char)) (block-count int) (stories [story-location MAX-STORIES]) (story-count int) ;; every player's stored pick readings (pick-readings (* pick-reading)) (pick-reading-count int))) ;;; Map regions and map chunks share one key: x in the low 27 bits, ;;; z above them. (fn position ((x i32) (z i32)) i64 (return (c-bit-or (cast x i64) (<< (cast z i64) 27)))) (fn strndup-pb ((r pb)) (* char) (return (strndup (cast (. r p) (* const char)) (.len r)))) ;;; World config ---------------------------------------------------- (fn add-config ((s (* save)) (key (* char)) (value (* char))) void (var n int (. s config-count)) (= (. s config-keys) (realloc (. s config-keys) (* (+ n 1) (sizeof key)))) (= (. s config-values) (realloc (. s config-values) (* (+ n 1) (sizeof value)))) (= [(. s config-keys) n] key) (= [(. s config-values) n] value) (= (. s config-count) (+ n 1))) (fn read-le ((r (* pb)) (n int)) u64 (var v u64 0) (for (var i int 0) (< i n) (++ i) (if (! (.more r)) (do (= (. r err) true) (return 0))) (c-bit-or= v (<< (cast (* (. r p)) u64) (* 8 i))) (++ (. r p))) (return v)) ;;; .NET BinaryWriter strings carry the same LEB128 length as protobuf. (fn read-dotnet-string ((r (* pb))) (* char) (return (strndup-pb (.bytes r)))) ;;; A TreeAttribute in its binary form: (type byte, key, value)* 0. ;;; Values are kept as the text the world config would show them as. (fn read-tree-attribute ((s (* save)) (r pb)) void (while (.more r) (var type int (cast (read-le (& r) 1) int)) (if (== type 0) (return)) (var key (* char) (read-dotnet-string (& r))) (var value [char 64]) (switch type (case 1 (snprintf value 64 "%d" (cast (read-le (& r) 4) i32)) (break)) (case 2 (snprintf value 64 "%lld" (cast (read-le (& r) 8) i64)) (break)) (case 3 (var bits u64 (read-le (& r) 8)) (var d double) (memcpy (& d) (& bits) 8) (snprintf value 64 "%.17g" d) (break)) (case 4 (var bits u32 (cast (read-le (& r) 4) u32)) (var f float) (memcpy (& f) (& bits) 4) (snprintf value 64 "%.9g" (cast f double)) (break)) (case 5 (add-config s key (read-dotnet-string (& r))) (continue)) (case 9 (snprintf value 64 "%s" (if (read-le (& r) 1) "true" "false")) (break)) (default (fprintf stderr "world config: attribute type %d of `%s' unsupported, stopping\n" type key) (free key) (return))) (add-config s key (strdup value)))) (pub fn save/config ((s (* const save)) (key (* const char))) (* const char) (for (var i int 0) (< i (. s config-count)) (++ i) (if (== (strcmp [(. s config-keys) i] key) 0) (return [(. s config-values) i]))) (return NULL)) ;;; Mod data entries ------------------------------------------------- ;;; Dictionary BlockIDs: entries {1: id, 2: code}. (fn read-block-ids ((s (* save)) (r pb)) void (var scan pb r) (var max-id int -1) (var wire int) (while (> (.field (& scan) (& wire)) 0) (var entry pb (.bytes (& scan))) (while (> (.field (& entry) (& wire)) 0) (if (== wire WIRE-VARINT) (do (var id int (cast (.varint (& entry)) int)) (if (> id max-id) (= max-id id))) (.skip (& entry) wire)))) (= (. s block-count) (+ max-id 1)) (= (. s block-codes) (calloc (+ max-id 1) (sizeof (* char)))) (while (> (.field (& r) (& wire)) 0) (var entry pb (.bytes (& r))) (var id int -1) (var code (* char) NULL) (var f u32) (while (> (= f (.field (& entry) (& wire))) 0) (if (== f 1) (= id (cast (.varint (& entry)) int)) (== f 2) (= code (strndup-pb (.bytes (& entry)))) (.skip (& entry) wire))) (if (>= id 0) (= [(. s block-codes) id] code) (free code)))) ;;; Dictionary: entries {1: key, ;;; 2: {1: code, 2: centre {1: x, 2: y, 3: z}, ...}}. (fn read-stories ((s (* save)) (r pb)) void (var wire int) (while (&& (< (. s story-count) MAX-STORIES) (> (.field (& r) (& wire)) 0)) (var entry pb (.bytes (& r))) (var loc (* story-location) (& [(. s stories) (. s story-count)])) (var f u32) (while (> (= f (.field (& entry) (& wire))) 0) (if (!= f 2) (do (.skip (& entry) wire) (continue))) (var body pb (.bytes (& entry))) (var g u32) (while (> (= g (.field (& body) (& wire))) 0) (if (== g 1) (do (var code pb (.bytes (& body))) (snprintf (. loc code) 64 "%.*s" (cast (.len code) int) (. code p))) (== g 2) (do (var centre pb (.bytes (& body))) (var h u32) (while (> (= h (.field (& centre) (& wire))) 0) (if (== h 1) (= (. loc x) (.int32 (& centre))) (== h 2) (= (. loc y) (.int32 (& centre))) (== h 3) (= (. loc z) (.int32 (& centre))) (.skip (& centre) wire)))) (.skip (& body) wire)))) (++ (. s story-count)))) (fn read-double ((r (* pb))) double (var bits u64 (read-le r 8)) (var d double) (memcpy (& d) (& bits) 8) (return d)) (fn add-pick-reading ((s (* save)) (reading pick-reading)) void (var n int (. s pick-reading-count)) (= (. s pick-readings) (realloc (. s pick-readings) (* (+ n 1) (sizeof pick-reading)))) (= [(. s pick-readings) n] reading) (= (. s pick-reading-count) (+ n 1))) ;;; oreMapMarkers-PLAYER: List, entries {1: position ;;; {1: x, 2: y, 3: z}, 2: {1: code, 2: {2: total, 3: ppt}}*}. (fn read-pick-readings ((s (* save)) (r pb)) void (var wire int) (while (> (.field (& r) (& wire)) 0) (var body pb (.bytes (& r))) (var at pick-reading) (memset (& at) 0 (sizeof at)) (var f u32) (while (> (= f (.field (& body) (& wire))) 0) (if (== f 1) (do (var position pb (.bytes (& body))) (var g u32) (while (> (= g (.field (& position) (& wire))) 0) (if (== g 1) (= (. at x) (cast (read-double (& position)) int)) (== g 2) (= (. at y) (cast (read-double (& position)) int)) (== g 3) (= (. at z) (cast (read-double (& position)) int)) (.skip (& position) wire)))) (== f 2) (do (var entry pb (.bytes (& body))) (var reading pick-reading at) (var g u32) (while (> (= g (.field (& entry) (& wire))) 0) (if (== g 1) (do (var code pb (.bytes (& entry))) (snprintf (. reading code) 32 "%.*s" (cast (.len code) int) (. code p))) (== g 2) (do (var value pb (.bytes (& entry))) (var h u32) (while (> (= h (.field (& value) (& wire))) 0) (if (== h 2) (= (. reading total) (read-double (& value))) (== h 3) (= (. reading ppt) (read-double (& value))) (.skip (& value) wire)))) (.skip (& entry) wire))) (add-pick-reading s reading)) (.skip (& body) wire))))) ;;; Game data -------------------------------------------------------- (fn read-gamedata ((s (* save)) (r pb)) void (var wire int) (var f u32) (while (> (= f (.field (& r) (& wire))) 0) (if (== f 1) (= (. s size-x) (.int32 (& r))) (== f 2) (= (. s size-y) (.int32 (& r))) (== f 3) (= (. s size-z) (.int32 (& r))) (== f 7) (= (. s seed) (.int32 (& r))) (== f 31) (read-tree-attribute s (.bytes (& r))) (== f 11) (do (var entry pb (.bytes (& r))) (var key pb #(pb : NULL NULL false)) (var value pb #(pb : NULL NULL false)) (var g u32) (while (> (= g (.field (& entry) (& wire))) 0) (if (== g 1) (= key (.bytes (& entry))) (== g 2) (= value (.bytes (& entry))) (.skip (& entry) wire))) (if (.equals key "BlockIDs") (read-block-ids s value) (.equals key "storystructurelocations") (read-stories s value) (&& (. key p) (>= (.len key) 14) (== (strncmp (cast (. key p) (* const char)) "oreMapMarkers-" 14) 0)) (read-pick-readings s value))) (.skip (& r) wire)))) (fn query-blob ((s (* const save)) (sql (* const char)) (key i64) (out (* * sqlite3-stmt))) pb (var stmt (* sqlite3-stmt) NULL) (sqlite3-prepare-v2 (. s db) sql -1 (& stmt) NULL) (sqlite3-bind-int64 stmt 1 key) (= (* out) stmt) (if (!= (sqlite3-step stmt) SQLITE-ROW) (return #(pb : NULL NULL true))) (return (pb/of (sqlite3-column-blob stmt 0) (cast (sqlite3-column-bytes stmt 0) size-t)))) (pub fn save/open ((path (* const char))) (* save) (var s (* save) (calloc 1 (sizeof save))) (if (!= (sqlite3-open-v2 path (& (. s db)) SQLITE-OPEN-READONLY NULL) SQLITE-OK) (do (fprintf stderr "%s: %s\n" path (sqlite3-errmsg (. s db))) (sqlite3-close (. s db)) (free s) (return NULL))) (var stmt (* sqlite3-stmt) NULL) (var r pb (query-blob s "select data from gamedata limit ?1" 1 (& stmt))) (if (. r err) (do (fprintf stderr "%s: no game data\n" path) (sqlite3-finalize stmt) (sqlite3-close (. s db)) (free s) (return NULL))) (read-gamedata s r) (sqlite3-finalize stmt) (return s)) (pub fn save/block-code ((s (* const save)) (id int)) (* const char) (if (&& (>= id 0) (< id (. s block-count)) [(. s block-codes) id]) (return [(. s block-codes) id])) (return "?")) (pub fn save/close ((s (* save))) void (for (var i int 0) (< i (. s config-count)) (++ i) (free [(. s config-keys) i]) (free [(. s config-values) i])) (free (. s config-keys)) (free (. s config-values)) (for (var i int 0) (< i (. s block-count)) (++ i) (free [(. s block-codes) i])) (free (. s block-codes)) (free (. s pick-readings)) (sqlite3-close (. s db)) (free s)) ;;; Map regions ------------------------------------------------------ ;;; IntDataMap2D: {1: packed data, 2: size, 3: pad-tl, 4: pad-br}. (fn read-int-map ((r pb) (m (* int-map))) void (var wire int) (var f u32) (= (* m) #(int-map : NULL 0 0 0)) (while (> (= f (.field (& r) (& wire))) 0) (if (== f 1) (do (var packed pb (.bytes (& r))) (var count pb packed) (var n size-t 0) (while (.more count) (.varint (& count)) (++ n)) (= (. m data) (malloc (* (+ n 1) (sizeof i32)))) (for (var i size-t 0) (< i n) (++ i) (= [(. m data) i] (.int32 (& packed))))) (== f 2) (= (. m size) (.int32 (& r))) (== f 3) (= (. m pad-tl) (.int32 (& r))) (== f 4) (= (. m pad-br) (.int32 (& r))) (.skip (& r) wire)))) ;;; ServerMapRegion, the fields we use: 1 landform, 3 climate, ;;; 4 geologic province, 12 rock strata (one per stratum), 17 upheaval, ;;; 18 ocean. (fn read-region ((r pb) (m (* region-maps))) void (memset m 0 (sizeof region-maps)) (var wire int) (var f u32) (while (> (= f (.field (& r) (& wire))) 0) (if (== f 1) (read-int-map (.bytes (& r)) (& (. m landform))) (== f 3) (read-int-map (.bytes (& r)) (& (. m climate))) (== f 4) (read-int-map (.bytes (& r)) (& (. m geoprov))) (== f 17) (read-int-map (.bytes (& r)) (& (. m upheaval))) (== f 18) (read-int-map (.bytes (& r)) (& (. m ocean))) (&& (== f 12) (< (. m strata-count) MAX-STRATA)) (do (read-int-map (.bytes (& r)) (& [(. m strata) (. m strata-count)])) (++ (. m strata-count))) (.skip (& r) wire)))) (pub fn save/region ((s (* const save)) (rx i32) (rz i32) (out (* region-maps))) bool (var stmt (* sqlite3-stmt) NULL) (var r pb (query-blob s "select data from mapregion where position = ?1" (position rx rz) (& stmt))) (var found bool (! (. r err))) (if found (read-region r out)) (sqlite3-finalize stmt) (return found)) ;;; ServerMapRegion field 8, OreMaps: one {1: code, 2: IntDataMap2D} ;;; entry per mineral. Fills up to MAX of CODES (each strdup'd) and ;;; MAPS, and returns how many; -1 when the save has no such region. (pub fn save/ore-maps ((s (* const save)) (rx i32) (rz i32) (codes (* * char)) (maps (* int-map)) (max int)) int (var stmt (* sqlite3-stmt) NULL) (var r pb (query-blob s "select data from mapregion where position = ?1" (position rx rz) (& stmt))) (if (. r err) (do (sqlite3-finalize stmt) (return -1))) (var n int 0) (var wire int) (var f u32) (while (> (= f (.field (& r) (& wire))) 0) (if (|| (!= f 8) (>= n max)) (do (.skip (& r) wire) (continue))) (var entry pb (.bytes (& r))) (= [codes n] NULL) (= [maps n] #(int-map : NULL 0 0 0)) (var g u32) (while (> (= g (.field (& entry) (& wire))) 0) (if (== g 1) (= [codes n] (strndup-pb (.bytes (& entry)))) (== g 2) (read-int-map (.bytes (& entry)) (& [maps n])) (.skip (& entry) wire))) (if [codes n] (++ n) (free (. [maps n] data)))) (sqlite3-finalize stmt) (return n)) ;;; Map chunks ------------------------------------------------------- ;;; A repeated ushort field arrives one value per field, or packed. (fn read-u16s ((r (* pb)) (wire int) (out (* u16)) (n (* int))) void (if (== wire WIRE-BYTES) (do (var packed pb (.bytes r)) (while (&& (.more packed) (< (* n) CHUNK-AREA)) (= [out (* n)] (cast (.varint (& packed)) u16)) (++ (* n)))) (do (var v u16 (cast (.varint r) u16)) (if (< (* n) CHUNK-AREA) (do (= [out (* n)] v) (++ (* n))))))) ;;; Rivers stores riverDistance as a serialized ushort[]: {1: value}*. (fn read-river-distance ((r pb) (m (* chunk-maps))) void (var wire int) (var n int 0) (while (> (.field (& r) (& wire)) 0) (read-u16s (& r) wire (. m river-distance) (& n))) (= (. m has-river-distance) (== n CHUNK-AREA))) ;;; ServerMapChunk, the fields we use: 1 mod data, 3 rain height, ;;; 7 worldgen terrain height, 13 top rock id. (fn read-chunk ((r pb) (m (* chunk-maps))) void (memset m 0 (sizeof chunk-maps)) (var wire int) (var f u32) (var rain int 0) (var terrain int 0) (var rock int 0) (while (> (= f (.field (& r) (& wire))) 0) (if (== f 3) (read-u16s (& r) wire (. m rain-height) (& rain)) (== f 7) (read-u16s (& r) wire (. m terrain-height) (& terrain)) (&& (== f 13) (< rock CHUNK-AREA)) (do (= [(. m top-rock) rock] (.int32 (& r))) (++ rock)) (== f 1) (do (var entry pb (.bytes (& r))) (var key pb #(pb : NULL NULL false)) (var g u32) (while (> (= g (.field (& entry) (& wire))) 0) (if (== g 1) (= key (.bytes (& entry))) (&& (== g 2) (.equals key "riverDistance")) (read-river-distance (.bytes (& entry)) m) (.skip (& entry) wire)))) (.skip (& r) wire)))) (pub fn save/chunk ((s (* const save)) (cx i32) (cz i32) (out (* chunk-maps))) bool (var stmt (* sqlite3-stmt) NULL) (var r pb (query-blob s "select data from mapchunk where position = ?1" (position cx cz) (& stmt))) (var found bool (! (. r err))) (if found (read-chunk r out)) (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))) (pub fn save/extent ((s (* const save)) (table (* const char))) extent (var sql [char 160]) (snprintf sql (sizeof sql) "select min(position & 134217727), min(position >> 27), max(position & 134217727), max(position >> 27), count(*) from %s" table) (var stmt (* sqlite3-stmt) NULL) (var e extent #(extent : 0 0 -1 -1 0)) (sqlite3-prepare-v2 (. s db) sql -1 (& stmt) NULL) (if (== (sqlite3-step stmt) SQLITE-ROW) (= e #(extent : (sqlite3-column-int stmt 0) (sqlite3-column-int stmt 1) (sqlite3-column-int stmt 2) (sqlite3-column-int stmt 3) (sqlite3-column-int64 stmt 4)))) (sqlite3-finalize stmt) (return e))