diff --git a/Makefile b/Makefile index ad7d3bf..c44ed31 100644 --- a/Makefile +++ b/Makefile @@ -31,7 +31,7 @@ $(B)/cJSON.o: cJSON/cJSON.c cJSON/cJSON.h | $(B) $(B)/stb.o: src/stb.c third_party/stb/stb_image_write.h | $(B) $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@ -VSMV_OBJ = vsmv cs pb json maps save img dump lcg noise shape worldgen fastnoise rivers terra strata ores settings verify cJSON stb +VSMV_OBJ = vsmv cs pb json maps save img dump lcg noise shape worldgen fastnoise rivers terra strata ores caves settings verify cJSON stb $(B)/vsmv: $(VSMV_OBJ:%=$(B)/%.o) $(CC) $^ -o $@ -lsqlite3 $(ZSTD_LIBS) diff --git a/Plan.org b/Plan.org index 0a8c9f8..2bfbff9 100644 --- a/Plan.org +++ b/Plan.org @@ -706,6 +706,60 @@ 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. +** Caves (done) +=src/caves.sex= ports =GenCaves= (terrain pass, after the strata). +- *Starts.* =chunkRand= (world seed), positioned at the chunk, starts + a cave with chance 0.37: a position, size and shape from its draws, + then =caveRand= reseeded from it. Tunnels run 108 to 144 steps, so + they reach 5 chunks; they branch (horizontally, and at level 0 into + vertical shafts that end in a fan of tunnels), widen, flatten, go + tall, and near y 12 open lava caverns. =FastSin=/=FastCos= are the + game's 4096-entry tables. +- *Traced once.* A tunnel's course depends only on the seed and its + chunk. It is traced once per source chunk, as the ordered list of + =SetBlocks= ellipsoids, in the source chunk's coordinates. The game + works in the target chunk's coordinates; the double sums are exact + at that size, so the two differ by whole chunks only. World + coordinates would round. +- *Carved per chunk.* A target chunk replays its 11 x 11 sources in + =GenPartial='s order (x outer, z inner): + - Each ellipsoid that reaches the chunk is skipped if any liquid is + in it or one size around it: the water the terrain pass left in + every open block below sea level, or lava an earlier ellipsoid put. + - Below y 12 a carved block becomes basalt above + =geologic activity * 16 / 128= and lava at or below it. At y 11, + =basaltNoise= over 0.65 makes basalt. + - A carve at the column's height lowers it, which later carving + reads through =CaveHeightDistort=. + - Story locations skip caves for chunks whose centre lies in their + "caves" radius, or in the village's footprint (195 x 225; the + story schematics aren't in the assets read here). +- *Terrain solidity.* =terra/chunk-solidity= keeps every block's solid + or open from the height scan, so the water check is exact, overhang + pockets below sea level included. +- *Not ported.* Hot springs, which a later pass puts where caves + flagged them. + +Validation (=vsmv verify=, every stored chunk, blocks below our +terrain height): +- 17,794,389 of our 18,521,333 carved blocks agree with the save + (96.08%): air or lava where the save has air (lava is a fluid, so its + solid block is air), basalt where it has basalt, or basalt a deposit + later made obsidian, ore or cracked rock. Of the rest, 667,823 hold + what later passes put in caves (stalactites, gravel, ice). +- Air the save has where we leave rock: 656,515 inside generated + structures (vugs above all), 292,212 at or above the save's surface + (the block layers take away top blocks, glaciers especially), + 26,749 otherwise. +- Rock the save has where we carve air: 28,562 inside structures, + 25,388 otherwise. The 52,137 unexplained are 0.28% of what we carve; + the check fails over 0.5%. +- The terrain's own pockets: 334,438 air blocks, 99.4% matching. +- Terrain height after carving: 11,552,740 of 11,687,936 columns match + the stored height, against 11,481,361 before. The caves make up most + of the "stored lower on land" columns of the height check. + + ** 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 diff --git a/Readme.org b/Readme.org index 5a0b787..4e279f7 100644 --- a/Readme.org +++ b/Readme.org @@ -22,6 +22,7 @@ against a real save, and matches it: | 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 | +| Caves | 96.1% of 18.5 million carved 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 | @@ -30,7 +31,9 @@ the strata are laid: caves that open the surface, and the raise of dry 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. +underground structures are built of granite. Where the caves differ, +it is nearly all what later passes put in them, or air the structures +and the block layers make; 0.28% is not explained. =Plan.org= has the details. ** Building @@ -191,16 +194,20 @@ 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 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. +=verify= takes about eight minutes, most of it on the terrain heights +of every stored chunk and on the caves. It also compares the ore maps, +the prospecting pick readings players took in the save, if there are +any, and every stored block below the surface: its rock, and whether +it is a cave. ** Limits - Only the /realistic/ world climate is supported, not /patchy/. - 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. +- Caves leave out what later passes put in them: stalactites, gravel, + hot springs, and the structures some of them hold. Of the blocks the + save has where caves are carved, 3.6% are such things. - Prospecting readings come out a little higher than the game's: in the readings checked, the game's was up to 12.5% lower, which can move one across a category edge. The game's column already holds the ore of the diff --git a/src/caves.sex b/src/caves.sex new file mode 100644 index 0000000..4ca629c --- /dev/null +++ b/src/caves.sex @@ -0,0 +1,584 @@ +;;; Caves as GenCaves carves them, during the terrain pass right after +;;; the strata. +;;; +;;; A chunk starts a cave with chance 0.37: a tunnel that wanders, +;;; widens and branches for up to about 140 steps, so it reaches up to +;;; five chunks away. Each step carves an ellipsoid. Its course depends +;;; only on the seed and the chunk, so it is traced once per source +;;; chunk, in that chunk's own coordinates, as the list of ellipsoids +;;; it carves. +;;; +;;; Carving them is per target chunk, as the game does it: the eleven +;;; by eleven source chunks around it in order, each ellipsoid skipped +;;; when there is liquid in or around it, water from the terrain or +;;; lava an earlier one left. Below y 12 a carved block becomes lava or +;;; basalt, by the region's geologic activity. Where a cave reaches the +;;; surface it lowers the height map, which later carving reads. +;;; +;;; Positions are kept relative to the source chunk. The game keeps them +;;; relative to the target chunk; at that size both sums are exact, so +;;; the two differ by whole chunks only, where world coordinates would +;;; round. + +(include stdlib.h string.h math.h) +(pub import maps) +(import lcg) +(import noise) +(pub import worldgen) + +;;; How many chunks a cave reaches, and the states of a carved column. +(pub define CAVE-RANGE 5) +(pub define CAVE-UNTOUCHED 0) +(pub define CAVE-AIR 1) +(pub define CAVE-LAVA 2) +(pub define CAVE-BASALT 3) + +(define CHUNK 32) +(pub define SIN-COUNT 4096) +(pub define TRACE-SLOTS 4096) + +;;; One SetBlocks call: the ellipsoid's centre in its source chunk's +;;; coordinates, and its radii. +(record carve ((x double) (y double) (z double) (hr float) (vr float))) + +;;; The carves of one source chunk's cave, in order. +(pub record trace ((sx int) (sz int) (valid bool) (carves (* carve)) (count int) (cap int))) + +(pub record caves + ((g (* worldgen)) (seed i32) (size-y int) (sea-level int) + (height-noise (* simplex-noise)) (basalt-noise (* simplex-noise)) + ;; GameMath.FastSin and FastCos + (sin [float SIN-COUNT]) (cos [float SIN-COUNT]) (rad-to-index float) + (traces [trace TRACE-SLOTS]) + ;; the climate map of the region last asked about + (climate int-map) (climate-rx int) (climate-rz int) + ;; the trace being made, and its random numbers + (cave-rand lcg) (current (* trace)))) + +(define PI-F 3.14159274) + +(pub fn caves/make ((g (* worldgen))) (* caves) + (var c (* caves) (calloc 1 (sizeof caves))) + (= (. c g) g) + (= (. c seed) (. g settings seed)) + (= (. c size-y) (. g settings size-y)) + (= (. c sea-level) (cast (* 0.4313725490196078 (. g settings size-y)) int)) + ;; Seed + 12, as a C# int + (var seed i32 (cast (+ (cast (. c seed) u32) 12) i32)) + (= (. c height-noise) (simplex-noise/octaves 3 (/ (cast 1 float) (cast 20 float)) (cast 0.9 float) seed)) + (= (. c basalt-noise) (simplex-noise/octaves 2 (/ (cast 1 float) (cast 3.5 float)) (cast 0.9 float) seed)) + (var rad-full float (cast (* M-PI 2.0) float)) + (= (. c rad-to-index) (/ (cast SIN-COUNT float) rad-full)) + (for (var i int 0) (< i SIN-COUNT) (++ i) + (var a float (* (/ (+ (cast i float) (cast 0.5 float)) (cast SIN-COUNT float)) rad-full)) + (= [(. c sin) i] (cast (sin (cast a double)) float)) + (= [(. c cos) i] (cast (cos (cast a double)) float))) + (return c)) + +(pub fn caves/free ((c (* caves))) void + (for (var i int 0) (< i TRACE-SLOTS) (++ i) + (free (. [(. c traces) i] carves))) + (free (. c climate data)) + (free (. c height-noise)) + (free (. c basalt-noise)) + (free c)) + +(fn fast-sin ((c (* const caves)) (rad float)) float + (return [(. c sin) (& (cast (* rad (. c rad-to-index)) int) (- SIN-COUNT 1))])) + +(fn fast-cos ((c (* const caves)) (rad float)) float + (return [(. c cos) (& (cast (* rad (. c rad-to-index)) int) (- SIN-COUNT 1))])) + +(fn clampf ((v float) (lo float) (hi float)) float + (if (< v lo) (return lo)) + (if (> v hi) (return hi)) + (return v)) + +(fn clampd ((v double) (lo double) (hi double)) double + (if (< v lo) (return lo)) + (if (> v hi) (return hi)) + (return v)) + +;;; Tracing ---------------------------------------------------------- + +(fn add-carve ((c (* caves)) (x double) (y double) (z double) (hr float) (vr float)) void + (var t (* trace) (. c current)) + (if (>= (. t count) (. t cap)) + (do + (= (. t cap) (if (> (. t cap) 0) (* (. t cap) 2) 256)) + (= (. t carves) (realloc (. t carves) (* (. t cap) (sizeof carve)))))) + (= [(. t carves) (. t count)] #(carve : x y z hr vr)) + (++ (. t count))) + +;; declared ahead: a tunnel branches into shafts, a shaft into tunnels +(fn carve-tunnel ((c (* caves)) (pos-x double) (pos-y double) (pos-z double) (hor-angle float) + (vert-angle float) (horizontal-size float) (vertical-size float) + (current-iteration int) (max-iterations int) (branch-level int) + (extra-branchy bool) (curviness float) (large-near-lava bool)) void) + +;;; CarveShaft: a vertical branch that ends in a few horizontal ones. +(fn carve-shaft ((c (* caves)) (pos-x double) (pos-y double) (pos-z double) (hor-angle float) + (vert-angle float) (horizontal-size float) (vertical-size float) + (cave-current-iteration int) (max-iterations int) (branch-level int)) void + (var r (* lcg) (& (. c cave-rand))) + (var vert-angle-change float 0) + (var current-iteration int 0) + (while (< current-iteration max-iterations) + (++ current-iteration) + (var rel-pos float (/ (cast current-iteration float) (cast max-iterations float))) + (var hor-radius float (* horizontal-size (- (cast 1 float) (* rel-pos (cast 0.33 float))))) + (var vert-radius float (* hor-radius vertical-size)) + (var advance-hor float (fast-cos c vert-angle)) + (var advance-ver float (fast-sin c vert-angle)) + (if (< vert-radius (cast 1 float)) + (*= vert-angle (cast 0.1 float))) + (+= pos-x (* (fast-cos c hor-angle) advance-hor)) + (+= pos-y (clampf advance-ver (- vert-radius) vert-radius)) + (+= pos-z (* (fast-sin c hor-angle) advance-hor)) + (+= vert-angle (* (cast 0.1 float) vert-angle-change)) + (var a float (.next-float r)) + (var b float (.next-float r)) + (var d float (.next-float r)) + (= vert-angle-change (+ (* (cast 0.9 float) vert-angle-change) + (/ (* (- a b) d) (cast 3 float)))) + ;; the horizontal branches at the bottom + (if (< (- max-iterations current-iteration) 10) + (do + (var num int (+ 3 (.next-int r 4))) + (for (var i int 0) (< i num) (++ i) + (var h float (* (.next-float r) (* (cast 2 float) (cast PI-F float)))) + (var v float (* (- (.next-float r) (cast 0.5 float)) (cast 0.25 float))) + (carve-tunnel c pos-x pos-y pos-z h v (+ horizontal-size (cast 1 float)) vertical-size + cave-current-iteration max-iterations 1 false (cast 0.1 float) false)) + (return))) + (if (&& (== (.next-int r 5) 0) (>= hor-radius (cast 2 float))) + (continue)) + (add-carve c pos-x pos-y pos-z hor-radius vert-radius))) + +;;; CarveTunnel. +(fn carve-tunnel ((c (* caves)) (pos-x double) (pos-y double) (pos-z double) (hor-angle float) + (vert-angle float) (horizontal-size float) (vertical-size float) + (current-iteration int) (max-iterations int) (branch-level int) + (extra-branchy bool) (curviness float) (large-near-lava bool)) void + (var r (* lcg) (& (. c cave-rand))) + (var sea int (. c sea-level)) + (var pi float (cast PI-F float)) + (var hor-angle-change float 0) + (var vert-angle-change float 0) + (var hor-radius-gain float 0) + (var hor-radius-loss float 0) + (var hor-radius-gain-accum float 0) + (var hor-radius-loss-accum float 0) + (var ver-height-gain float 0) + (var ver-height-loss float 0) + (var ver-height-gain-accum float 0) + (var ver-height-loss-accum float 0) + (var size-change-speed-accum float (cast 0.15 float)) + (var size-change-speed-gain float 0) + (var branch-rand int (* (+ branch-level 1) (if extra-branchy 12 25))) + (while (< current-iteration max-iterations) + (++ current-iteration) + (var rel-pos float (/ (cast current-iteration float) (cast max-iterations float))) + (var s float (fast-sin c (* rel-pos pi))) + (var hor-radius float (+ (+ (cast 1.5 float) (* s horizontal-size)) hor-radius-gain-accum)) + (= hor-radius (fminf hor-radius (fmaxf (cast 1 float) (- hor-radius hor-radius-loss-accum)))) + (var vert-radius float (+ (+ (cast 1.5 float) + (* s (+ vertical-size (/ hor-radius-loss-accum (cast 4 float))))) + ver-height-gain-accum)) + (= vert-radius (fminf vert-radius (fmaxf (cast 0.6 float) (- vert-radius ver-height-loss-accum)))) + (var advance-hor float (fast-cos c vert-angle)) + (var advance-ver float (fast-sin c vert-angle)) + ;; caves get bigger near y 12 + (if large-near-lava + (do + (var near float (- (cast 1 float) (/ (cast (fabs (- pos-y 12.0)) float) (cast 10 float)))) + (var factor float (+ (cast 1 float) (fmaxf (cast 0 float) near))) + (*= hor-radius factor) + (*= vert-radius factor))) + (if (< vert-radius (cast 1 float)) + (*= vert-angle (cast 0.1 float))) + (+= pos-x (* (fast-cos c hor-angle) advance-hor)) + (+= pos-y (clampf advance-ver (- vert-radius) vert-radius)) + (+= pos-z (* (fast-sin c hor-angle) advance-hor)) + (*= vert-angle (cast 0.8 float)) + + (var rrnd int (.next-int r 800000)) + (if (== (/ rrnd 10000) 0) + (do + (var a float (.next-float r)) + (var b float (.next-float r)) + (= size-change-speed-gain (/ (* a b) (cast 2 float))))) + ;; one draw picks among the rare changes: the game subtracts each + ;; one's share in turn, which is these running totals + (var rnd int (% rrnd 10000)) + (if (<= rnd 30) + (= hor-angle (* (.next-float r) (* (cast 2 float) pi))) + (<= rnd 106) + (+= hor-angle (- (* (.next-float r) pi) (/ pi (cast 2 float)))) + (<= rnd 166) + (do + (var a float (.next-float r)) + (var b float (.next-float r)) + (= hor-radius-gain (* (* a b) (cast 3.5 float)))) + (<= rnd 226) + (do + (var a float (.next-float r)) + (var b float (.next-float r)) + (= hor-radius-loss (* (* a b) (cast 10 float)))) + (<= rnd 276) + (if (< pos-y (- sea 10)) + (do + (var a float (.next-float r)) + (var b float (.next-float r)) + (= ver-height-loss (* (* a b) (cast 12 float))) + (var e float (.next-float r)) + (var f float (.next-float r)) + (= hor-radius-gain (fmaxf hor-radius-gain (* (* e f) (cast 3 float)))))) + (<= rnd 285) + (if (< pos-y (- sea 20)) + (do + (var a float (.next-float r)) + (var b float (.next-float r)) + (= hor-radius-gain (+ (cast 1 float) (* (* a b) (cast 5 float)))))) + (<= rnd 294) + (do + (var a float (.next-float r)) + (var b float (.next-float r)) + (= ver-height-gain (+ (cast 2 float) (* (* a b) (cast 7 float))))) + (<= rnd 394) + (if (< pos-y 19) + (do + (var a float (.next-float r)) + (var b float (.next-float r)) + (= ver-height-gain (+ (cast 2 float) (* (* a b) (cast 5 float)))) + (var e float (.next-float r)) + (var f float (.next-float r)) + (= hor-radius-gain (+ (cast 4 float) (* (* e f) (cast 9 float))))))) + ;; GenHotSprings, a later pass, would put a hot spring here when + ;; -5 < y < 16, the radii pass 4 and 2 and geologic activity 128 + + (= size-change-speed-accum (fmaxf (cast 0.1 float) (+ size-change-speed-accum + (* size-change-speed-gain (cast 0.05 float))))) + (-= size-change-speed-gain (cast 0.02 float)) + (= hor-radius-gain-accum (fmaxf (cast 0 float) (+ hor-radius-gain-accum (* hor-radius-gain size-change-speed-accum)))) + (-= hor-radius-gain (cast 0.45 float)) + (= hor-radius-loss-accum (fmaxf (cast 0 float) (+ hor-radius-loss-accum (* hor-radius-loss size-change-speed-accum)))) + (-= hor-radius-loss (cast 0.4 float)) + (= ver-height-gain-accum (fmaxf (cast 0 float) (+ ver-height-gain-accum (* ver-height-gain size-change-speed-accum)))) + (-= ver-height-gain (cast 0.45 float)) + (= ver-height-loss-accum (fmaxf (cast 0 float) (+ ver-height-loss-accum (* ver-height-loss size-change-speed-accum)))) + (-= ver-height-loss (cast 0.4 float)) + + (+= hor-angle (* curviness hor-angle-change)) + (+= vert-angle (* curviness vert-angle-change)) + (var m1 float (.next-float-minus-to-plus-one r)) + (var f1 float (.next-float r)) + (= vert-angle-change (+ (* (cast 0.9 float) vert-angle-change) (* (* m1 f1) (cast 3 float)))) + (var m2 float (.next-float-minus-to-plus-one r)) + (var f2 float (.next-float r)) + (= hor-angle-change (+ (* (cast 0.9 float) hor-angle-change) (* m2 f2))) + (if (== (% rrnd 140) 0) + (*= hor-angle-change (* (.next-float r) (cast 6 float)))) + + ;; a horizontal branch, less likely above sea level + (var depth int (- (cast pos-y int) (- sea 20))) + (var brand int (+ branch-rand (* 2 (if (> depth 0) depth 0)))) + (if (&& (< branch-level 3) (|| (> vert-radius (cast 1 float)) (> hor-radius (cast 1 float))) + (== (.next-int r brand) 0)) + (do + (var a float (.next-float r)) + (var b float (.next-float r)) + (var h float (+ (+ hor-angle (- (+ a b) (cast 1 float))) pi)) + (var e float (.next-float r)) + (var f float (.next-float r)) + (var v float (+ vert-angle (* (- e (cast 0.5 float)) (- f (cast 0.5 float))))) + (var k float (.next-float r)) + (var shorter int (cast (* (* (cast k double) 0.5) max-iterations) int)) + (carve-tunnel c pos-x (+ pos-y (/ ver-height-gain-accum (cast 2 float))) pos-z h v + horizontal-size (+ vertical-size ver-height-gain-accum) current-iteration + (- max-iterations shorter) (+ branch-level 1) false (cast 0.1 float) false))) + ;; a vertical one + (if (&& (< branch-level 1) (> hor-radius (cast 3 float)) (> pos-y 60) (== (.next-int r 60) 0)) + (do + (var a float (.next-float r)) + (var b float (.next-float r)) + (var h float (+ (+ hor-angle (- (+ a b) (cast 1 float))) pi)) + (var e float (.next-float r)) + (var v float (+ (- (- (/ pi (cast 2 float))) (cast 0.1 float)) (* (cast 0.2 float) e))) + (var k float (.next-float r)) + (var shorter int (cast (* (* (cast k double) 0.5) max-iterations) int)) + (var l float (.next-float r)) + (var longer int (cast (* (/ pos-y 5) (cast (+ (cast 0.5 float) (* (cast 0.5 float) l)) double)) int)) + (carve-shaft c pos-x (+ pos-y (/ ver-height-gain-accum (cast 2 float))) pos-z h v + (fminf (cast 3.5 float) (- hor-radius (cast 1 float))) + (+ vertical-size ver-height-gain-accum) current-iteration + (+ (- max-iterations shorter) longer) branch-level) + (++ branch-level))) + + (if (&& (>= hor-radius (cast 2 float)) (== (% rrnd 5) 0)) + (continue)) + (add-carve c pos-x (+ pos-y (/ ver-height-gain-accum (cast 2 float))) pos-z hor-radius + (+ vert-radius ver-height-gain-accum)))) + +;;; GeneratePartial's cave for source chunk (sx, sz), into T. +(fn trace-chunk ((c (* caves)) (t (* trace)) (sx int) (sz int)) void + (= (. t sx) sx) + (= (. t sz) sz) + (= (. t count) 0) + (= (. t valid) true) + (= (. c current) t) + (var r lcg (lcg/make (. c seed))) + (.init-position (& r) sx sz) + (if (! (< (.next-int (& r) 100) (* 0.37 100))) + (return)) + (var rnd int (.next-int (& r) (* CHUNK CHUNK (- (. c size-y) 20)))) + (var pos-x int (% rnd CHUNK)) + (/= rnd CHUNK) + (var pos-z int (% rnd CHUNK)) + (/= rnd CHUNK) + (var pos-y int (+ rnd 8)) + (var pi float (cast PI-F float)) + (var hor-angle float (* (.next-float (& r)) (* (cast 2 float) pi))) + (var vert-angle float (* (- (.next-float (& r)) (cast 0.5 float)) (cast 0.25 float))) + (var a float (.next-float (& r))) + (var b float (.next-float (& r))) + (var horizontal-size float (+ (* a (cast 2 float)) b)) + (var vertical-size float (+ (cast 0.75 float) (* (.next-float (& r)) (cast 0.4 float)))) + (= rnd (.next-int (& r) (* 100 50 1000 100))) + (if (< (% rnd 100) 4) + (do + (var e float (.next-float (& r))) + (var f float (.next-float (& r))) + (var g float (.next-float (& r))) + (= horizontal-size (+ (+ (* e (cast 2 float)) f) g)) + (= vertical-size (+ (cast 0.25 float) (* (.next-float (& r)) (cast 0.2 float))))) + (== (% rnd 100) 4) + (do + (= horizontal-size (+ (cast 0.75 float) (.next-float (& r)))) + (var e float (.next-float (& r))) + (var f float (.next-float (& r))) + (= vertical-size (+ (* e (cast 2 float)) f)))) + (/= rnd 100) + (var extra-branchy bool (&& (< pos-y (/ (. c sea-level) 2)) (== (% rnd 50) 0))) + (/= rnd 50) + (var rnd1000 int (% rnd 1000)) + (/= rnd 1000) + (var large-near-lava bool (< (% rnd1000 10) 3)) + (var curviness float (cast 0.1 float)) + (if (== rnd 0) (= curviness (cast 0.035 float)) + (< rnd1000 30) (= curviness (cast 0.5 float))) + (var max-iterations int (- (* CAVE-RANGE CHUNK) (/ CHUNK 2))) + (-= max-iterations (.next-int (& r) (/ max-iterations 4))) + (.reseed (& (. c cave-rand)) (.next-int (& r) 10000000)) + (.init-position (& (. c cave-rand)) sx sz) + (carve-tunnel c pos-x pos-y pos-z hor-angle vert-angle horizontal-size vertical-size 0 + max-iterations 0 extra-branchy curviness large-near-lava)) + +;;; Source chunk (sx, sz)'s trace, traced once and kept. +(fn trace-of ((c (* caves)) (sx int) (sz int)) (* trace) + (var h u32 (+ (* (cast sx u32) 73856093) (* (cast sz u32) 19349663))) + (var t (* trace) (& [(. c traces) (% h TRACE-SLOTS)])) + (if (! (&& (. t valid) (== (. t sx) sx) (== (. t sz) sz))) + (trace-chunk c t sx sz)) + (return t)) + +;;; Carving ---------------------------------------------------------- + +;;; getGeologicActivity: the climate map's low byte at the chunk. +(fn geologic-activity ((c (* caves)) (x int) (z int)) int + (var rx int (/ x 512)) + (var rz int (/ z 512)) + (if (|| (! (. c climate data)) (!= (. c climate-rx) rx) (!= (. c climate-rz) rz)) + (do + (free (. c climate data)) + (worldgen/map (. c g) MAP-CLIMATE 0 rx rz (& (. c climate))) + (= (. c climate-rx) rx) + (= (. c climate-rz) rz))) + (var m (* int-map) (& (. c climate))) + (var lx int (% (/ x CHUNK) 16)) + (var lz int (% (/ z CHUNK) 16)) + (return (& [(. m data) (+ (* (+ lz (. m pad-tl)) (. m size)) lx (. m pad-tl))] 255))) + +;;; A target chunk column while it is carved. +(record column + ((tx int) (tz int) (states (* u8)) (heights (* int)) (water-top (* int)) (distort [u8 1024]) + (solid (* const u32)) (words int))) + +;;; The terrain pass put water in every block below sea level that it +;;; left open; with the terrain's solidity at hand that is exact, else +;;; it is the column above its surface. +(fn liquid-at ((c (* const caves)) (k (* const column)) (lx int) (y int) (lz int)) bool + (var i int (+ (* lz CHUNK) lx)) + (if (< y (. c sea-level)) + (if (. k solid) + (if (== (& (>> [(. k solid) (+ (* i (. k words)) (/ y 32))] (% y 32)) 1) 0) + (return true)) + (> y [(. k water-top) i]) + (return true))) + (return (== [(. k states) (+ (* (+ (* y CHUNK) lz) CHUNK) lx)] CAVE-LAVA))) + +;;; SetBlocks, at centre (x, y, z) in the target chunk's coordinates. +(fn set-blocks ((c (* caves)) (k (* column)) (hor-radius float) (vert-radius float) + (x double) (y double) (z double)) void + (var top int (- (. c size-y) 1)) + ;; one size more, to see whether it would run into liquid + (+= hor-radius (cast 1 float)) + (+= vert-radius (cast 2 float)) + (var min-x int (cast (clampd (- x hor-radius) 0 (- CHUNK 1)) int)) + (var max-x int (cast (clampd (+ (+ x hor-radius) 1) 0 (- CHUNK 1)) int)) + (var min-y int (cast (clampd (- y (* vert-radius (cast 0.7 float))) 1 top) int)) + (var max-y int (cast (clampd (+ (+ y vert-radius) 1) 1 top) int)) + (var min-z int (cast (clampd (- z hor-radius) 0 (- CHUNK 1)) int)) + (var max-z int (cast (clampd (+ (+ z hor-radius) 1) 0 (- CHUNK 1)) int)) + (var h-sq double (* hor-radius hor-radius)) + (var v-sq double (* vert-radius vert-radius)) + (var distort-strength double (clampd (/ vert-radius 4.0) 0 0.1)) + (for (var lx int min-x) (<= lx max-x) (++ lx) + (var xd double (/ (* (- lx x) (- lx x)) h-sq)) + (for (var lz int min-z) (<= lz max-z) (++ lz) + (var zd double (/ (* (- lz z) (- lz z)) h-sq)) + (var height-rnd double (* (- [(. k distort) (+ (* lz CHUNK) lx)] 127) distort-strength)) + (for (var ly int min-y) (<= ly (+ max-y 10)) (++ ly) + (var dy double (- ly y)) + (var off double 0) + (if (> dy 0) (= off (* height-rnd height-rnd))) + (var yd double (/ (* dy dy) (+ v-sq off))) + (if (|| (> (+ xd yd zd) 1.0) (> ly top)) + (continue)) + (if (liquid-at c k lx ly lz) + (return))))) + + (-= hor-radius (cast 1 float)) + (-= vert-radius (cast 2 float)) + (= min-x (cast (clampd (- x hor-radius) 0 (- CHUNK 1)) int)) + (= max-x (cast (clampd (+ (+ x hor-radius) 1) 0 (- CHUNK 1)) int)) + (= min-z (cast (clampd (- z hor-radius) 0 (- CHUNK 1)) int)) + (= max-z (cast (clampd (+ (+ z hor-radius) 1) 0 (- CHUNK 1)) int)) + (= min-y (cast (clampd (- y (* vert-radius (cast 0.7 float))) 1 top) int)) + (= max-y (cast (clampd (+ (+ y vert-radius) 1) 1 top) int)) + (= h-sq (* hor-radius hor-radius)) + (= v-sq (* vert-radius vert-radius)) + (var geo int (geologic-activity c (+ (* (. k tx) CHUNK) (cast x int)) (+ (* (. k tz) CHUNK) (cast z int)))) + (var lava-top int (/ (* geo 16) 128)) + (for (var lx int min-x) (<= lx max-x) (++ lx) + (var xd double (/ (* (- lx x) (- lx x)) h-sq)) + (for (var lz int min-z) (<= lz max-z) (++ lz) + (var zd double (/ (* (- lz z) (- lz z)) h-sq)) + (var i int (+ (* lz CHUNK) lx)) + (var height-rnd double (* (- [(. k distort) i] 127) distort-strength)) + (var surface int [(. k heights) i]) + (for (var ly int (+ max-y 10)) (>= ly min-y) (-- ly) + (var dy double (- ly y)) + (var off double 0) + (if (> dy 0) + (do + (var near double (/ (abs (- ly surface)) 10.0)) + (= off (* height-rnd height-rnd (fmin 1 near))))) + (var yd double (/ (* dy dy) (+ v-sq off))) + (if (|| (> ly top) (> (+ xd yd zd) 1.0)) + (continue)) + (if (== [(. k heights) i] ly) + (= [(. k heights) i] (- ly 1))) + (var b (* u8) (& [(. k states) (+ (* (+ (* ly CHUNK) lz) CHUNK) lx)])) + (if (== ly 11) + (if (> (.noise-2d (. c basalt-noise) (+ (* (. k tx) CHUNK) lx) (+ (* (. k tz) CHUNK) lz)) 0.65) + (do + (= (* b) CAVE-BASALT) + (if (< [(. k heights) i] 11) (= [(. k heights) i] 11))) + (> ly lava-top) (= (* b) CAVE-BASALT) + (= (* b) CAVE-LAVA)) + (< ly 12) + (if (> ly lava-top) (= (* b) CAVE-BASALT) (= (* b) CAVE-LAVA)) + (= (* b) CAVE-AIR)))))) + +;;; Carves chunk column (tx, tz): STATES, 32 x 32 x size-y by +;;; (y * 32 + z) * 32 + x, gets each block's CAVE- state; HEIGHTS, the +;;; terrain heights by z * 32 + x, are lowered where caves reach the +;;; surface. SOLID, terra/chunk-solidity's bits with WORDS per column, +;;; tells where the terrain left water; without it, NULL, water is +;;; taken to be the columns above their surface. +(pub fn caves/carve-column ((c (* caves)) (tx int) (tz int) (heights (* int)) (solid (* const u32)) + (words int) (states (* u8))) void + (memset states 0 (* CHUNK CHUNK (. c size-y))) + (if (worldgen/caves-blocked (. c g) (+ (* tx CHUNK) 16) (+ (* tz CHUNK) 16)) + (return)) + (var k column) + (= (. k tx) tx) + (= (. k tz) tz) + (= (. k states) states) + (= (. k heights) heights) + (= (. k solid) solid) + (= (. k words) words) + ;; the terrain pass left water in every column below sea level above + ;; its surface + (var water-top [int 1024]) + (memcpy water-top heights (sizeof water-top)) + (= (. k water-top) water-top) + ;; GenCaves.OnMapChunkGen: CaveHeightDistort + (for (var lz int 0) (< lz CHUNK) (++ lz) + (for (var lx int 0) (< lx CHUNK) (++ lx) + (var v double (- (.noise-2d (. c height-noise) (+ (* tx CHUNK) lx) (+ (* tz CHUNK) lz)) 0.5)) + (if (> v 0) + (= v (fmax 0 (- v 0.07))) + (= v (fmin 0 (+ v 0.07)))) + (= [(. k distort) (+ (* lz CHUNK) lx)] (cast (cast (+ (* 128 v) 127) int) u8)))) + (for (var dx int (- CAVE-RANGE)) (<= dx CAVE-RANGE) (++ dx) + (for (var dz int (- CAVE-RANGE)) (<= dz CAVE-RANGE) (++ dz) + (var t (* trace) (trace-of c (+ tx dx) (+ tz dz))) + (for (var i int 0) (< i (. t count)) (++ i) + (var cv (* carve) (& [(. t carves) i])) + (var x double (+ (. cv x) (* dx CHUNK))) + (var z double (+ (. cv z) (* dz CHUNK))) + ;; only what reaches this chunk + (var reach float (* (. cv hr) (cast 2 float))) + (if (|| (<= x (- reach)) (>= x (+ CHUNK reach)) (<= z (- reach)) (>= z (+ CHUNK reach))) + (continue)) + (set-blocks c (& k) (. cv hr) (. cv vr) x (. cv y) z))))) + +;;; Columns ---------------------------------------------------------- + +;;; How a column's caves show on the map: none; an entrance, where a +;;; cave reached the surface; or how deep below the surface the topmost +;;; one starts. +(pub define CAVE-NONE 0) +(pub define CAVE-ENTRANCE 1) +(pub define CAVE-CATEGORIES 7) +(var depth-bands [int 4] #(10 25 50 100)) + +;;; The category of column (lx, lz) of a carved chunk column: SURFACE is +;;; its terrain height before carving, CARVED after. +(pub fn caves/category ((c (* const caves)) (states (* const u8)) (lx int) (lz int) (surface int) + (carved int)) int + (if (< carved surface) + (return CAVE-ENTRANCE)) + (for (var y int surface) (>= y 1) (-- y) + (var s u8 [states (+ (* (+ (* y CHUNK) lz) CHUNK) lx)]) + (if (|| (== s CAVE-AIR) (== s CAVE-LAVA)) + (do + (var depth int (- surface y)) + (for (var b int 0) (< b 4) (++ b) + (if (<= depth [depth-bands b]) + (return (+ 2 b)))) + (return 6)))) + (return CAVE-NONE)) + +;;; A column's caves as runs of air or lava, top down: run i is from +;;; TOPS[i] down to BOTTOMS[i], lava when LAVA[i]. Returns how many, up +;;; to MAX. +(pub fn caves/runs ((c (* const caves)) (states (* const u8)) (lx int) (lz int) (surface int) + (tops (* int)) (bottoms (* int)) (lava (* bool)) (max int)) int + (var n int 0) + (var prev u8 CAVE-UNTOUCHED) + (for (var y int surface) (>= y 1) (-- y) + (var s u8 [states (+ (* (+ (* y CHUNK) lz) CHUNK) lx)]) + (if (== s CAVE-BASALT) (= s CAVE-UNTOUCHED)) + (if (&& (!= s CAVE-UNTOUCHED) (== s prev) (> n 0)) + (= [bottoms (- n 1)] y) + (&& (!= s CAVE-UNTOUCHED) (< n max)) + (do + (= [tops n] y) + (= [bottoms n] y) + (= [lava n] (== s CAVE-LAVA)) + (++ n))) + (= prev s)) + (return n)) diff --git a/src/lcg.sex b/src/lcg.sex index 0860c43..1584d7f 100644 --- a/src/lcg.sex +++ b/src/lcg.sex @@ -62,6 +62,12 @@ (pub fn next-float ((self (* lcg))) float (return (/ (cast (.next-bits self) float) (cast INT32_MAX float)))) + ;; NextFloatMinusToPlusOne: -1 to 1, most likely near 0, the + ;; difference of two numbers below 46341 that one draw holds. + (pub fn next-float-minus-to-plus-one ((self (* lcg))) float + (var r int (.next-bits self)) + (return (/ (cast (- (% r 46341) (/ r 46341)) float) (cast 46340 float)))) + (pub fn next-double ((self (* lcg))) double (var r u32 (cast (>> (cast (. self current) i64) 24) u32)) (.advance self) diff --git a/src/save.sex b/src/save.sex index 80d8057..592af20 100644 --- a/src/save.sex +++ b/src/save.sex @@ -383,6 +383,38 @@ (sqlite3-finalize stmt) (return n)) +;;; ServerMapRegion field 11, GeneratedStructures: one {1: code, 3: box +;;; {1: x1, 2: y1, 3: z1, 4: x2, 5: y2, 6: z2}} per structure the +;;; region's chunks placed. Fills up to MAX boxes, six ints each, and +;;; returns how many. +(pub fn save/structure-boxes ((s (* const save)) (rx i32) (rz i32) (boxes (* i32)) (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))) + (var n int 0) + (var wire int) + (var f u32) + (while (&& (! (. r err)) (> (= f (.field (& r) (& wire))) 0)) + (if (|| (!= f 11) (>= n max)) + (do (.skip (& r) wire) (continue))) + (var entry pb (.bytes (& r))) + (var g u32) + (while (> (= g (.field (& entry) (& wire))) 0) + (if (== g 3) + (do + (var box pb (.bytes (& entry))) + (var b (* i32) (+ boxes (* n 6))) + (memset b 0 (* 6 (sizeof i32))) + (var k u32) + (while (> (= k (.field (& box) (& wire))) 0) + (if (&& (>= k 1) (<= k 6)) + (= [b (- k 1)] (.int32 (& box))) + (.skip (& box) wire))) + (++ n)) + (.skip (& entry) wire)))) + (sqlite3-finalize stmt) + (return n)) + ;;; Map chunks ------------------------------------------------------- ;;; A repeated ushort field arrives one value per field, or packed. diff --git a/src/terra.sex b/src/terra.sex index e1c9e7d..682850e 100644 --- a/src/terra.sex +++ b/src/terra.sex @@ -645,8 +645,11 @@ ;;; A height is the top solid block, 0 if there is none. ;;; Heights of the columns of chunk (cx, cz) from local (lx0, lz0) to ;;; (lx1, lz1) inclusive, every STEP-th, into HEIGHTS[lz * 32 + lx]. +;;; SOLID-BITS, unless NULL, gets every column's solid blocks below the top +;;; too, overhangs and pockets showing as gaps: bit y % 32 of word +;;; (lz * 32 + lx) * WORDS + y / 32, set where solid; it starts zeroed. (fn heights-in ((t (* terra)) (cx int) (cz int) (lx0 int) (lz0 int) (lx1 int) (lz1 int) - (step int) (heights (* u16))) void + (step int) (heights (* u16)) (solid-bits (* u32)) (words int)) void (use-region t (/ cx 16) (/ cz 16)) (var per-plate int (river-plate/chunks (& (. t config)))) (var plate (* river-plate) (use-plate t (/ cx per-plate) (/ cz per-plate))) @@ -778,7 +781,11 @@ (! (|| (<= y (- baseline bank-blocks)) (>= (cast y float) (+ baseline (* bank-blocks (. c top-factor))))))) (= solid false)) - (if solid (= height y))) + (if solid + (do + (= height y) + (if solid-bits + (c-bit-or= [solid-bits (+ (* (+ (* lz 32) lx) words) (/ y 32))] (<< (cast 1 u32) (% y 32))))))) (= [heights (+ (* lz 32) lx)] (cast height u16)))) (free w)) @@ -786,7 +793,19 @@ ;;; multiples of STEP, into HEIGHTS[lz * 32 + lx]; the rest are left ;;; alone. (pub fn terra/chunk-heights ((t (* terra)) (cx int) (cz int) (step int) (heights (* u16))) void - (heights-in t cx cz 0 0 31 31 step heights)) + (heights-in t cx cz 0 0 31 31 step heights NULL 0)) + +;;; Words per column of terra/chunk-solidity's bits. +(pub fn terra/solid-words ((t (* const terra))) int + (return (/ (+ (. t size-y) 31) 32))) + +;;; Every block of chunk (cx, cz): its heights as terra/chunk-heights, +;;; and SOLID, 1024 * terra/solid-words words, bit y % 32 of word +;;; (lz * 32 + lx) * words + y / 32 set where the terrain is solid. +(pub fn terra/chunk-solidity ((t (* terra)) (cx int) (cz int) (heights (* u16)) (solid (* u32))) void + (var words int (terra/solid-words t)) + (memset solid 0 (* 1024 words (sizeof u32))) + (heights-in t cx cz 0 0 31 31 1 heights solid words)) ;;; The height of the one column at block (x, z): 1/1024th of a chunk's ;;; work, for a cursor or a probe. @@ -794,5 +813,5 @@ (var heights [u16 1024]) (var lx int (% x 32)) (var lz int (% z 32)) - (heights-in t (/ x 32) (/ z 32) lx lz lx lz 1 heights) + (heights-in t (/ x 32) (/ z 32) lx lz lx lz 1 heights NULL 0) (return [heights (+ (* lz 32) lx)])) diff --git a/src/verify.sex b/src/verify.sex index 8cb6382..dfd5a31 100644 --- a/src/verify.sex +++ b/src/verify.sex @@ -11,6 +11,7 @@ (import terra) (import strata) (import ores) +(import caves) (import img) (define MAP-KINDS 6) @@ -337,6 +338,179 @@ (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. diff --git a/src/vsmv.sex b/src/vsmv.sex index ef42ce9..cfb896d 100644 --- a/src/vsmv.sex +++ b/src/vsmv.sex @@ -89,6 +89,7 @@ (verify-ores s minerals out) (verify-readings s g (river-config/defaults) rocks minerals) (verify-strata s g (river-config/defaults) rocks out) + (verify-caves s g (river-config/defaults) out) (verify-rivers s g (river-config/defaults) out) (verify-heights s g (river-config/defaults) rocks out))) (save/close s) diff --git a/src/worldgen.sex b/src/worldgen.sex index 31632ce..4fdb55f 100644 --- a/src/worldgen.sex +++ b/src/worldgen.sex @@ -767,7 +767,18 @@ ;;; forces on the region maps around it. (pub record story ((code [char 64]) (x int) (z int) (dir-x int) (landform-radius int) - (landform int) (forces-climate bool) (climate int))) + (landform int) (forces-climate bool) (climate int) + ;; skipGenerationCategories' "caves": no caves within this radius, or + ;; in the schematic's footprint when it is 0; -1 when caves are not + ;; skipped + (cave-skip int) (size-x int) (size-z int))) + +;;; The footprints of the story schematics that skip caves by footprint +;;; rather than by radius. The schematics are not among the assets this +;;; reads, so their sizes are written here, as the 1.22.7 save records +;;; the village's. +(record story-size ((code (* const char)) (x int) (z int))) +(var story-sizes [story-size 1] #(#(story-size : "village" 195 225))) (fn json-int ((o (* cJSON)) (key (* const char)) (fallback int)) int (return (cast (json-number o key fallback) int))) @@ -826,6 +837,13 @@ (= (. st z) (+ base-z (* dist-z dir-z))) (= (. st dir-x) dir-x) (= (. st landform-radius) (json-int c "landformRadius" 0)) + (var skip (* cJSON) (cJSON-GetObjectItem (cJSON-GetObjectItem c "skipGenerationCategories") "caves")) + (= (. st cave-skip) (if (cJSON-IsNumber skip) (cast (cJSON-GetNumberValue skip) int) -1)) + (for (var k int 0) (< k 1) (++ k) + (if (== (strcmp (. [story-sizes k] code) (. st code)) 0) + (do + (= (. st size-x) (. [story-sizes k] x)) + (= (. st size-z) (. [story-sizes k] z))))) (var landform (* const char) (cJSON-GetStringValue (cJSON-GetObjectItem c "requireLandform"))) (= (. st landform) (if landform (landform-table/find landforms landform) -1)) (var force-temp (* cJSON) (cJSON-GetObjectItem c "forceTemperature")) @@ -1086,6 +1104,30 @@ (for (var i int 0) (< i (. g strata count)) (++ i) (worldgen/map g MAP-STRATA i rx rz (& [(. out strata) i])))) +;;; GenStoryStructures.GetBlockingStructureAt for the "caves" category: +;;; whether a story location keeps caves out of block column (x, z). +(pub fn worldgen/caves-blocked ((g (* const worldgen)) (x int) (z int)) bool + (for (var i int 0) (< i (. g story-count)) (++ i) + (var st (* const story) (& [(. g stories) i])) + (var r int (. st cave-skip)) + (if (< r 0) + (continue)) + (if (== r 0) + (do + ;; the schematic's cuboid, X2 = X1 + SizeX + (var x1 int (- (. st x) (/ (. st size-x) 2))) + (var z1 int (- (. st z) (/ (. st size-z) 2))) + (if (&& (>= x x1) (<= x (+ x1 (. st size-x))) (>= z z1) (<= z (+ z1 (. st size-z)))) + (return true)) + (continue))) + ;; BlockPos.HorDistanceSqTo: in doubles, returned as a float + (var dx double (- x (. st x))) + (var dz double (- z (. st z))) + (var d float (cast (+ (* dx dx) (* dz dz)) float)) + (if (< d (cast (* r r) float)) + (return true))) + (return false)) + (pub fn worldgen/free ((g (* worldgen))) void (for (var i int 0) (< i (. g landform-table count)) (++ i) (free (. [(. g landform-table all) i] y-thresholds)))