;;; Diffs generated region maps against what a save stored, region by ;;; region over everything the save has. A port is right when every ;;; map matches to the pixel, padding included. (include stdio.h stdlib.h string.h math.h time.h) (import maps) (import shape) (import save) (import worldgen) (pub import rivers) (import terra) (import strata) (import ores) (import caves) (import img) (define MAP-KINDS 6) ;;; How far below the computed reading the game's may be; see ;;; verify-readings. (define READING-SLACK 0.85) (var map-names [(* const char) 6] #("landform" "climate" "province" "upheaval" "ocean" "strata")) (record tally ((compared i64) (matched i64) (regions int) (size-mismatch int))) (fn map-of ((m (* region-maps)) (kind int) (stratum int)) (* int-map) (switch kind (case 0 (return (& (. m landform)))) (case 1 (return (& (. m climate)))) (case 2 (return (& (. m geoprov)))) (case 3 (return (& (. m upheaval)))) (case 4 (return (& (. m ocean)))) (default (return (& [(. m strata) stratum]))))) ;;; Compares one map; marks the region's cell in DIFF (CELL pixels a ;;; side at OX, OZ): grey where the inner pixel matches, red where not. (fn compare ((want (* const int-map)) (got (* const int-map)) (t (* tally)) (diff (* image)) (ox int) (oz int) (cell int)) void (if (|| (! (. got data)) (! (. want data))) (return)) (++ (. t regions)) (if (!= (. want size) (. got size)) (do (++ (. t size-mismatch)) (return))) (var n int (* (. want size) (. want size))) (for (var i int 0) (< i n) (++ i) (++ (. t compared)) (if (== [(. want data) i] [(. got data) i]) (++ (. t matched)))) (var inner int (- (. want size) (. want pad-tl) (. want pad-br))) (for (var y int 0) (< y cell) (++ y) (for (var x int 0) (< x cell) (++ x) (var i int (+ (* (+ (. want pad-tl) (/ (* y inner) cell)) (. want size)) (. want pad-tl) (/ (* x inner) cell))) (var ok bool (== [(. want data) i] [(. got data) i])) (.set diff (+ ox x) (+ oz y) (if ok (grey 96) 16719904))))) (pub fn verify-stories ((s (* save)) (g (* worldgen))) int (var failed int 0) (for (var i int 0) (< i (. g story-count)) (++ i) (var mine (* story) (& [(. g stories) i])) (var theirs (* story-location) NULL) (for (var j int 0) (< j (. s story-count)) (++ j) (if (== (strcmp (. [(. s stories) j] code) (. mine code)) 0) (= theirs (& [(. s stories) j])))) (var ok bool (&& theirs (== (. theirs x) (. mine x)) (== (. theirs z) (. mine z)))) (if (! ok) (++ failed)) (printf "story %-18s %8d %6d %s\n" (. mine code) (. mine x) (. mine z) (if ok "ok" "MISMATCH"))) (return failed)) (pub fn verify-regions ((s (* save)) (g (* worldgen)) (dir (* const char))) int (var e extent (save/extent s "mapregion")) (var cell int 32) (var w int (* (+ (- (. e x1) (. e x0)) 1) cell)) (var h int (* (+ (- (. e z1) (. e z0)) 1) cell)) (var tallies [tally 6]) (memset tallies 0 (sizeof tallies)) (var diffs [image 6]) (for (var k int 0) (< k MAP-KINDS) (++ k) (= [diffs k] (image/make w h))) (var want region-maps) (var got region-maps) (for (var rz int (. e z0)) (<= rz (. e z1)) (++ rz) (for (var rx int (. e x0)) (<= rx (. e x1)) (++ rx) (if (! (save/region s rx rz (& want))) (continue)) (worldgen/region g rx rz (& got)) (var ox int (* (- rx (. e x0)) cell)) (var oz int (* (- rz (. e z0)) cell)) (for (var k int 0) (< k 5) (++ k) (compare (map-of (& want) k 0) (map-of (& got) k 0) (& [tallies k]) (& [diffs k]) ox oz cell)) (for (var i int 0) (< i (. got strata-count)) (++ i) (compare (map-of (& want) 5 i) (map-of (& got) 5 i) (& [tallies 5]) (& [diffs 5]) ox oz cell)) (region-maps/free (& want)) (region-maps/free (& got)))) (var failed int 0) (for (var k int 0) (< k MAP-KINDS) (++ k) (var t (* tally) (& [tallies k])) (if (== (. t regions) 0) (do (.release (& [diffs k])) (continue))) (var percent double (/ (* 100.0 (. t matched)) (. t compared))) (printf "%-9s %4d maps %10lld/%-10lld px %8.4f%%%s\n" [map-names k] (. t regions) (. t matched) (. t compared) percent (if (. t size-mismatch) " (size mismatch)" "")) (if (!= (. t matched) (. t compared)) (++ failed)) (var path [char 1024]) (snprintf path (sizeof path) "%s/diff-%s.png" dir [map-names k]) (.write (& [diffs k]) path) (.release (& [diffs k]))) (return failed)) ;;; Regenerates the ore map of every mineral in every region the save ;;; stored and compares them; one diff image for all minerals, a region ;;; red where any of its ore maps differs. (pub fn verify-ores ((s (* save)) (o (* ores)) (dir (* const char))) int (var e extent (save/extent s "mapregion")) (var cell int 32) (var diff image (image/make (* (+ (- (. e x1) (. e x0)) 1) cell) (* (+ (- (. e z1) (. e z0)) 1) cell))) (var codes [* char MAX-MINERALS]) (var want [int-map MAX-MINERALS]) (var t tally) (memset (& t) 0 (sizeof t)) (var unknown int 0) (for (var rz int (. e z0)) (<= rz (. e z1)) (++ rz) (for (var rx int (. e x0)) (<= rx (. e x1)) (++ rx) (var n int (save/ore-maps s rx rz codes want MAX-MINERALS)) (var ox int (* (- rx (. e x0)) cell)) (var oz int (* (- rz (. e z0)) cell)) (var ok bool true) (for (var i int 0) (< i n) (++ i) (var k int (ores/find o [codes i])) (if (< k 0) (++ unknown) (do (var got int-map) (ores/map o k rx rz (& got)) (++ (. t regions)) (if (!= (. got size) (. [want i] size)) (do (++ (. t size-mismatch)) (= ok false)) (for (var p int 0) (< p (* (. got size) (. got size))) (++ p) (++ (. t compared)) (if (== [(. got data) p] [(. [want i] data) p]) (++ (. t matched)) (= ok false)))) (free (. got data)))) (free [codes i]) (free (. [want i] data))) (if (> n 0) (for (var y int 0) (< y cell) (++ y) (for (var x int 0) (< x cell) (++ x) (.set (& diff) (+ ox x) (+ oz y) (if ok (grey 96) 16719904))))))) (var percent double (if (> (. t compared) 0) (/ (* 100.0 (. t matched)) (. t compared)) 0)) (printf "%-9s %4d maps %10lld/%-10lld px %8.4f%%%s\n" "ore" (. t regions) (. t matched) (. t compared) percent (if (. t size-mismatch) " (size mismatch)" "")) (if (> unknown 0) (printf "ore %d stored maps of minerals the deposits don't define\n" unknown)) (var path [char 1024]) (snprintf path (sizeof path) "%s/diff-ore.png" dir) (.write (& diff) path) (.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))) ;;; 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. ;;; ;;; They can't match exactly. The pick's column already has the ore of ;;; the deposits generated in its chunk, which no longer counts as host ;;; rock, and the game's Y range is drawn from samples where this takes ;;; the distribution's limits. Both only ever make the game's reading ;;; lower, so a reading passes when the game's is at most this much ;;; below it, and never above. (pub fn verify-readings ((s (* save)) (g (* worldgen)) (config river-config) (rocks (* strata)) (o (* ores))) int (if (== (. s pick-reading-count) 0) (do (printf "readings none stored\n") (return 0))) (var t (* terra) (terra/make g config)) (var column [u8 1024]) (var compared int 0) (var passed int 0) (var same-category int 0) (var unreadable int 0) (var worst double 1) (var i int 0) (while (< i (. s pick-reading-count)) ;; the minerals of one reading are stored one after another (var at (* pick-reading) (& [(. s pick-readings) i])) (var end int i) (while (&& (< end (. s pick-reading-count)) (== (. [(. s pick-readings) end] x) (. at x)) (== (. [(. s pick-readings) end] z) (. at z))) (++ end)) (var x int (. at x)) (var z int (. at z)) (var h int (terra/column-height t x z)) (strata/column rocks (/ x 32) (/ z 32) (% x 32) (% z 32) h column) (printf "reading %d, %d: stored height %d, computed %d\n" x z (. at y) h) (for (var k int 0) (< k (. o count)) (++ k) (var m (* mineral) (& [(. o minerals) k])) (var stored (* pick-reading) NULL) (for (var j int i) (< j end) (++ j) (if (== (strcmp (. [(. s pick-readings) j] code) (. m code)) 0) (= stored (& [(. s pick-readings) j])))) (var map int-map) (ores/map o k (/ x 512) (/ z 512) (& map)) (var r mineral-reading (ores/reading o k (& map) x z column h)) (free (. map data)) (var want double (if stored (. stored total) 0)) (if (&& (! stored) (== (ores/category (. r total)) 0)) (continue)) (if (! (. m readable)) (do (++ unreadable) (printf " %-14s %9.6f (not read here)\n" (. m code) want) (continue))) (++ compared) ;; the game's reading over this one's (var ratio double (if (> (. r total) 0) (/ want (. r total)) 0)) (var ok bool (&& (<= want (+ (. r total) 1e-9)) (>= ratio READING-SLACK))) (var category bool (== (ores/category want) (ores/category (. r total)))) (if ok (++ passed)) (if category (++ same-category)) (if (< ratio worst) (= worst ratio)) (printf " %-14s %9.6f %9.6f %5.3f %s\n" (. m code) want (. r total) ratio (if ok "ok" "MISMATCH"))) (= i end)) (terra/free t) (printf "readings %d of %d no higher than computed and within %.0f%% of it (worst %.3f); %d of %d in the same category; %d child deposits not read\n" passed compared (* 100 (- 1 READING-SLACK)) worst same-category compared unreadable) (return (if (== passed compared) 0 1))) ;;; Recomputes Rivers' per-column riverDistance for every map chunk the ;;; save stored and compares it with the moddata Rivers saved: exactly ;;; where it saved some, and "nothing within 300 blocks" where it saved ;;; none. (pub fn verify-rivers ((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 sampler river-sampler (river-sampler/make (& config) (. g settings size-y))) (var plate (* river-plate) NULL) (var chunks-per-plate int (/ (* (. config zone-size) (. config zones-in-region)) 32)) (var segments (* int) NULL) (var stored-columns i64 0) (var stored-matched i64 0) (var unstored-chunks int 0) (var unstored-wrong int 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 px int (/ cx chunks-per-plate)) (var pz int (/ cz chunks-per-plate)) (if (|| (! plate) (!= (. plate px) px) (!= (. plate pz) pz)) (do (if plate (river-plate/free plate)) (var t0 clock-t (clock)) (= plate (river-plate/make config (& (. g oceans)) (. g settings size-y) px pz)) (printf "river plate %d,%d: %d nodes, %d segments, %.2f s\n" px pz (. plate node-count) (. plate segment-count) (/ (cast (- (clock) t0) double) CLOCKS-PER-SEC)) (= segments (realloc segments (* (+ (. plate segment-count) 1) (sizeof int)))))) (var count int (river-plate/chunk-segments plate cx cz segments)) (var in-range bool false) (for (var i int 0) (< i 1024) (++ i) (var bx int (+ (* cx 32) (% i 32))) (var bz int (+ (* cz 32) (/ i 32))) (var sample river-sample (river-sampler/sample (& sampler) plate segments count bx bz)) (var d int (cast (cast (. sample distance) u16) int)) (if (<= d 300) (= in-range true)) (var ok bool true) (if (. m has-river-distance) (do (++ stored-columns) (= ok (== d [(. m river-distance) i])) (if ok (++ stored-matched)))) (var shade int (if (> d 255) 0 (- 255 d))) (.set (& diff) (+ (* (- cx (. e x0)) 32) (% i 32)) (+ (* (- cz (. e z0)) 32) (/ i 32)) (if ok (grey shade) 16719904))) (if (! (. m has-river-distance)) (do (++ unstored-chunks) (if in-range (++ unstored-wrong)))))) (printf "rivers %lld/%lld columns with stored riverDistance match; %d/%d chunks without it rightly have no river within 300\n" stored-matched stored-columns (- unstored-chunks unstored-wrong) unstored-chunks) (var path [char 1024]) (snprintf path (sizeof path) "%s/diff-rivers.png" dir) (.write (& diff) path) (.release (& diff)) (if plate (river-plate/free plate)) (free segments) (return (if (&& (== stored-matched stored-columns) (== unstored-wrong 0)) 0 1))) ;;; Recomputes every stored map chunk's terrain height and compares it ;;; with WorldGenTerrainHeightMap. Mismatches are counted by how far off ;;; they are, which tells a missing step (a raise of a few blocks) from ;;; noise that is wrong. (pub fn verify-heights ((s (* save)) (g (* worldgen)) (config river-config) (rocks (* strata)) (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 t (* terra) (terra/make g config)) (var heights [u16 1024]) (var columns i64 0) (var matched i64 0) (var by-delta [i64 9]) (memset by-delta 0 (sizeof by-delta)) ;; stored higher / lower, computed top below / at or above sea level (var by-class [i64 4]) (memset by-class 0 (sizeof by-class)) (var t0 clock-t (clock)) (var rock-columns i64 0) (var rock-matched i64 0) (var no-rock i64 0) (var rock-diff image (image/make w h)) (var column [u8 1024]) (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-heights t cx cz 1 heights) (for (var i int 0) (< i 1024) (++ i) (var want int [(. m terrain-height) i]) (var got int [heights i]) (++ columns) (var d int (- want got)) (if (== d 0) (++ matched)) (if (!= d 0) (do (++ [by-class (+ (if (< d 0) 2 0) (if (>= got (. t sea-level)) 1 0))]))) ;; -4..+4, the ends counting everything beyond (var bucket int (+ (if (< d -4) -4 (if (> d 4) 4 d)) 4)) (++ [by-delta bucket]) (var colour u32 (grey (/ (* got 255) (. g settings size-y)))) (if (> d 0) (= colour 16719904)) (if (< d 0) (= colour 2105599)) (.set (& diff) (+ (* (- cx (. e x0)) 32) (% i 32)) (+ (* (- cz (. e z0)) 32) (/ i 32)) colour) ;; The top rock the game recorded is at the top solid block after ;; caves; the dry seabed raise only stacks more of it. (var top (* const char) (save/block-code s [(. m top-rock) i])) (if (|| (! (strncmp top "rock-" 5)) (! (strncmp top "rock" 4))) (do (strata/column rocks cx cz (% i 32) (/ i 32) got column) (var y int (if (< want got) want got)) (var ours int [column y]) (var name (* const char) (if (== ours ROCK-MANTLE) "mantle" [(. rocks rock-codes) ours])) (++ rock-columns) (var same bool (== (strcmp name top) 0)) (if same (++ rock-matched)) (var rc u32 16719904) (if same (= rc (palette (cast ours u32)))) (.set (& rock-diff) (+ (* (- cx (. e x0)) 32) (% i 32)) (+ (* (- cz (. e z0)) 32) (/ i 32)) rc)) (++ no-rock))))) (printf "heights %lld/%lld columns match, %.1f s\n" matched columns (/ (cast (- (clock) t0) double) CLOCKS-PER-SEC)) (printf " stored - computed: <=-4 %lld, -3 %lld, -2 %lld, -1 %lld, +1 %lld, +2 %lld, +3 %lld, >=+4 %lld\n" [by-delta 0] [by-delta 1] [by-delta 2] [by-delta 3] [by-delta 5] [by-delta 6] [by-delta 7] [by-delta 8]) (printf " stored higher: %lld under water, %lld on land; stored lower: %lld under water, %lld on land\n" [by-class 0] [by-class 1] [by-class 2] [by-class 3]) (printf "top rock %lld/%lld columns match (%.4f%%); %lld columns have no rock on top\n" rock-matched rock-columns (/ (* 100.0 rock-matched) rock-columns) no-rock) (var path [char 1024]) (snprintf path (sizeof path) "%s/diff-toprock.png" dir) (.write (& rock-diff) path) (.release (& rock-diff)) (snprintf path (sizeof path) "%s/diff-heights.png" dir) (.write (& diff) path) (.release (& diff)) (terra/free t) ;; Two later passes change the stored height after strata are laid: ;; GenBlockLayers raises dry seabeds, GenCaves lowers where a cave ;; opens the surface. Anything else is ours. (var unexplained i64 (+ [by-class 1] [by-class 2])) (printf " explained by the dry seabed raise and caves: all but %lld (%.3f%%)\n" unexplained (/ (* 100.0 unexplained) columns)) ;; a handful of columns carry structures or boulders placed later (if (> (* (- rock-columns rock-matched) 100000) rock-columns) (return 1)) (return (if (< (* unexplained 1000) columns) 0 1)))