read the save, the seed and the worldgen assets

The engine's save classes are closed, so the protobuf field numbers
were read off the data and matched to the map sizes TerraGenConfig
gives. vsmv dump renders what the save stored, as the reference the
generator will be diffed against.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-08 17:56:14 +03:00
parent d72ae437ae
commit 90b5d50fa5
12 changed files with 2807 additions and 51 deletions

390
src/save.sex Normal file
View File

@@ -0,0 +1,390 @@
;;; 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)
(import pb)
;;; 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 MAX-STRATA 32)
(pub define MAX-STORIES 32)
;;; IntDataMap2D: a square map whose border pixels only feed lerps
;;; and blurs. The region's own pixels are the inner
;;; (size - pad-tl - pad-br) square.
(pub record int-map ((data (* i32)) (size i32) (pad-tl i32) (pad-br i32)))
(pub record region-maps
((landform int-map) (climate int-map) (geoprov int-map) (upheaval int-map)
(ocean int-map) (strata [int-map MAX_STRATA]) (strata-count int)))
(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)))
(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)))
;;; 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
(do
(var bits u64 (read-le (& r) 8))
(var d double)
(memcpy (& d) (& bits) 8)
(snprintf value 64 "%.17g" d))
(break))
(case 4
(do
(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<int, string> 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 [(. s block-codes) 0])))
(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<string, StoryStructureLocation>: 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))))
;;; 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)))
(.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))
(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 region-maps/free ((m (* region-maps))) void
(free (. m landform data))
(free (. m climate data))
(free (. m geoprov data))
(free (. m upheaval data))
(free (. m ocean data))
(for (var i int 0) (< i (. m strata-count)) (++ i)
(free (. [(. m strata) i] data))))
(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))
;;; 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))
;;; 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))