lay rock strata and show them as an overlay
The surface rock matches the save's TopRockIdMap in all but 14 of 11.7 million columns. Rock tiles keep rock indices and take their colours from a palette on upload, so highlighting one rock, to find where it lies, recolours what is drawn instead of regenerating it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
241
src/strata.sex
Normal file
241
src/strata.sex
Normal file
@@ -0,0 +1,241 @@
|
||||
;;; Rock strata of a column, as GenRockStrataNew lays them: given the
|
||||
;;; column's surface height, which rock each block from y = 1 up to the
|
||||
;;; surface is. Everything starts as granite; strata are laid bottom-up
|
||||
;;; or top-down in rockstrata.json order, each as thick as its map
|
||||
;;; allows and its rock group's budget in the local geologic provinces
|
||||
;;; leaves.
|
||||
;;;
|
||||
;;; The result does not know where the column is air or water below the
|
||||
;;; surface (overhangs, riverbeds): it is what the block there would be
|
||||
;;; if it were rock, as the game's prospecting assumes too.
|
||||
|
||||
(include stdio.h stdlib.h string.h math.h)
|
||||
(import maps)
|
||||
(import lcg)
|
||||
(import noise)
|
||||
(import shape)
|
||||
(import json)
|
||||
(import worldgen)
|
||||
(import fastnoise)
|
||||
(import rivers)
|
||||
(import terra)
|
||||
|
||||
(pub define MAX-ROCKS 32)
|
||||
;;; A column entry below any rock: the mantle at y = 0.
|
||||
(pub define ROCK-MANTLE 255)
|
||||
(define PROVINCE-BLUR 2)
|
||||
|
||||
(pub record strata
|
||||
((variant-count int) (variant-rock [int 32]) (variant-group [int 32])
|
||||
(variant-top-down [bool 32])
|
||||
;; distinct rock codes; `granite' is the default rock's index
|
||||
(rock-codes [* char 32]) (rock-count int) (granite int)
|
||||
(province-count int) (province-max [float 128])
|
||||
(distort-x (* simplex-sum)) (distort-z (* simplex-sum)) (sea-level int)
|
||||
(g (* worldgen))
|
||||
;; the region last asked for
|
||||
(rx int) (rz int) (has-region bool) (province-map int-map) (provinces landform-weights)
|
||||
(maps [int-map 32])))
|
||||
|
||||
(var group-names [(* const char) 4] #("Sedimentary" "Metamorphic" "Igneous" "Volcanic"))
|
||||
|
||||
(fn rock-index ((s (* strata)) (code (* const char))) int
|
||||
(for (var i int 0) (< i (. s rock-count)) (++ i)
|
||||
(if (== (strcmp [(. s rock-codes) i] code) 0)
|
||||
(return i)))
|
||||
(= [(. s rock-codes) (. s rock-count)] (strdup code))
|
||||
(++ (. s rock-count))
|
||||
(return (- (. s rock-count) 1)))
|
||||
|
||||
(pub fn strata/make ((g (* worldgen)) (assets-dir (* const char))) (* strata)
|
||||
(var path [char 1024])
|
||||
(snprintf path (sizeof path) "%s/survival/worldgen/rockstrata.json" assets-dir)
|
||||
(var rockstrata (* cJSON) (json-load path))
|
||||
(snprintf path (sizeof path) "%s/survival/worldgen/geologicprovinces.json" assets-dir)
|
||||
(var provinces (* cJSON) (json-load path))
|
||||
(snprintf path (sizeof path) "%s/survival/worldgen/global.json" assets-dir)
|
||||
(var global (* cJSON) (json-load path))
|
||||
(if (|| (! rockstrata) (! provinces))
|
||||
(return NULL))
|
||||
(var s (* strata) (calloc 1 (sizeof strata)))
|
||||
(= (. s g) g)
|
||||
(var default-rock (* const char) "rock-granite")
|
||||
(if (&& global (cJSON-GetStringValue (cJSON-GetObjectItem global "defaultRockCode")))
|
||||
(= default-rock (cJSON-GetStringValue (cJSON-GetObjectItem global "defaultRockCode"))))
|
||||
(= (. s granite) (rock-index s default-rock))
|
||||
|
||||
(var variants (* cJSON) (cJSON-GetObjectItem rockstrata "variants"))
|
||||
(for (var v (* cJSON) (. variants child)) (&& v (< (. s variant-count) MAX-STRATA)) (= v (. v next))
|
||||
(var i int (. s variant-count))
|
||||
(= [(. s variant-rock) i]
|
||||
(rock-index s (cJSON-GetStringValue (cJSON-GetObjectItem v "blockcode"))))
|
||||
(var group (* const char) (cJSON-GetStringValue (cJSON-GetObjectItem v "rockGroup")))
|
||||
(= [(. s variant-group) i] 0)
|
||||
(for (var k int 0) (< k 4) (++ k)
|
||||
(if (&& group (== (strcasecmp group [group-names k]) 0))
|
||||
(= [(. s variant-group) i] k)))
|
||||
(var dir (* const char) (cJSON-GetStringValue (cJSON-GetObjectItem v "genDir")))
|
||||
(= [(. s variant-top-down) i] (&& dir (== (strcasecmp dir "TopDown") 0)))
|
||||
(++ (. s variant-count)))
|
||||
|
||||
;; GeologicProvinceVariant.init: a group the province leaves out has
|
||||
;; a scaled maximum of 0, not the class default's 99.
|
||||
(var mul float (/ (cast (. g settings size-y) float) (cast 256 float)))
|
||||
(var pv (* cJSON) (cJSON-GetObjectItem provinces "variants"))
|
||||
(for (var v (* cJSON) (. pv child)) (&& v (< (. s province-count) 32)) (= v (. v next))
|
||||
(var groups (* cJSON) (cJSON-GetObjectItem v "rockstrata"))
|
||||
(for (var k int 0) (< k 4) (++ k)
|
||||
(var entry (* cJSON) (cJSON-GetObjectItemCaseSensitive groups [group-names k]))
|
||||
(var scaled float 0)
|
||||
(if entry
|
||||
(do
|
||||
(var mt (* cJSON) (cJSON-GetObjectItem entry "maxThickness"))
|
||||
(var max float (if (cJSON-IsNumber mt) (cast (cJSON-GetNumberValue mt) float) 99))
|
||||
(= scaled (* mul max))))
|
||||
(= [(. s province-max) (+ (* (. s province-count) 4) k)] scaled))
|
||||
(++ (. s province-count)))
|
||||
|
||||
(= (. s sea-level) (cast (* 0.4313725490196078 (. g settings size-y)) int))
|
||||
(var amps [double 4] #(14 9 6 3))
|
||||
(var freqs [double 4])
|
||||
(= [freqs 0] (/ 1 100.0))
|
||||
(= [freqs 1] (/ 1 50.0))
|
||||
(= [freqs 2] (/ 1 25.0))
|
||||
(= [freqs 3] (/ 1 12.5))
|
||||
;; seeded from the sea level, not the world seed
|
||||
(= (. s distort-x) (simplex-sum/make amps freqs 4 (+ (. s sea-level) 9876)))
|
||||
(= (. s distort-z) (simplex-sum/make amps freqs 4 (+ (. s sea-level) 9877)))
|
||||
(cJSON-Delete rockstrata)
|
||||
(cJSON-Delete provinces)
|
||||
(if global (cJSON-Delete global))
|
||||
(return s))
|
||||
|
||||
(fn release-region ((s (* strata))) void
|
||||
(if (! (. s has-region))
|
||||
(return))
|
||||
(free (. s province-map data))
|
||||
(landform-weights/release (& (. s provinces)))
|
||||
(for (var i int 0) (< i (. s variant-count)) (++ i)
|
||||
(free (. [(. s maps) i] data)))
|
||||
(= (. s has-region) false))
|
||||
|
||||
(pub fn strata/free ((s (* strata))) void
|
||||
(release-region s)
|
||||
(for (var i int 0) (< i (. s rock-count)) (++ i)
|
||||
(free [(. s rock-codes) i]))
|
||||
(free (. s distort-x))
|
||||
(free (. s distort-z))
|
||||
(free s))
|
||||
|
||||
(fn use-region ((s (* strata)) (rx int) (rz int)) void
|
||||
(if (&& (. s has-region) (== (. s rx) rx) (== (. s rz) rz))
|
||||
(return))
|
||||
(release-region s)
|
||||
(worldgen/map (. s g) MAP-PROVINCE 0 rx rz (& (. s province-map)))
|
||||
(= (. s provinces) (landform-weights/make (& (. s province-map)) PROVINCE-BLUR))
|
||||
(for (var i int 0) (< i (. s variant-count)) (++ i)
|
||||
(worldgen/map (. s g) MAP-STRATA i rx rz (& [(. s maps) i])))
|
||||
(= (. s rx) rx)
|
||||
(= (. s rz) rz)
|
||||
(= (. s has-region) true))
|
||||
|
||||
;;; IntDataMap2D.GetIntLerpedCorrectly.
|
||||
(fn lerped-correctly ((m (* const int-map)) (x float) (z float)) float
|
||||
(var half float 0.5)
|
||||
(var xl int (cast (floor (cast (- x half) double)) int))
|
||||
(var zl int (cast (floor (cast (- z half) double)) int))
|
||||
(var fx float (- x (+ (cast xl float) half)))
|
||||
(var fz float (- z (+ (cast zl float) half)))
|
||||
(var i int (+ (* (+ zl (. m pad-tl)) (. m size)) xl (. m pad-tl)))
|
||||
(var tl float (cast [(. m data) i] float))
|
||||
(var tr float (cast [(. m data) (+ i 1)] float))
|
||||
(var bl float (cast [(. m data) (+ i (. m size))] float))
|
||||
(var br float (cast [(. m data) (+ i (. m size) 1)] float))
|
||||
(var top float (+ tl (* (- tr tl) fx)))
|
||||
(var bot float (+ bl (* (- br bl) fx)))
|
||||
(return (+ top (* (- bot top) fz))))
|
||||
|
||||
;;; GenRockStrataNew.genBlockColumn for block column (cx * 32 + lx,
|
||||
;;; cz * 32 + lz) with surface at SURFACE: OUT[y] is the rock index of
|
||||
;;; y = 0..surface, ROCK-MANTLE at 0.
|
||||
(pub fn strata/column ((s (* strata)) (cx int) (cz int) (lx int) (lz int) (surface int)
|
||||
(out (* u8))) void
|
||||
(use-region s (/ cx 16) (/ cz 16))
|
||||
(var rdx int (% cx 16))
|
||||
(var rdz int (% cz 16))
|
||||
(var inv float (/ (cast 1 float) (cast 64 float)))
|
||||
(var in-region-x float (* (* (cast rdx float) inv) (cast 32 float)))
|
||||
(var in-region-z float (* (* (cast rdz float) inv) (cast 32 float)))
|
||||
|
||||
(var weights [float 32])
|
||||
(landform-weights/at (& (. s provinces)) (+ in-region-x (* (cast lx float) inv))
|
||||
(+ in-region-z (* (cast lz float) inv)) weights (. s province-count))
|
||||
(var max [float 4] #(0 0 0 0))
|
||||
(var current [int 4] #(0 0 0 0))
|
||||
(for (var i int 0) (< i (. s province-count)) (++ i)
|
||||
(var w float [weights i])
|
||||
(if (== w 0)
|
||||
(continue))
|
||||
(for (var k int 0) (< k 4) (++ k)
|
||||
(+= [max k] (* [(. s province-max) (+ (* i 4) k)] w))))
|
||||
|
||||
(var wx int (+ (* cx 32) lx))
|
||||
(var wz int (+ (* cz 32) lz))
|
||||
(var dx float (cast (.noise-2d (. s distort-x) wx wz) float))
|
||||
(var dz float (cast (.noise-2d (. s distort-z) wx wz) float))
|
||||
(var distort float (/ (+ dx dz) (cast 30 float)))
|
||||
(if (< distort (cast 0.9 float)) (= distort (cast 0.9 float)))
|
||||
(if (> distort (cast 1.1 float)) (= distort (cast 1.1 float)))
|
||||
|
||||
(= [out 0] ROCK-MANTLE)
|
||||
(for (var y int 1) (<= y surface) (++ y)
|
||||
(= [out y] (cast (. s granite) u8)))
|
||||
|
||||
(var lower int 1)
|
||||
(var upper int surface)
|
||||
(var id int -1)
|
||||
(var group int 0)
|
||||
(var thickness float 0)
|
||||
(var rock int (. s granite))
|
||||
(var top-down bool false)
|
||||
(while (<= lower upper)
|
||||
(-= thickness 1)
|
||||
(if (<= thickness (cast 0 float))
|
||||
(do
|
||||
(++ id)
|
||||
(if (>= id (. s variant-count))
|
||||
(break))
|
||||
(var m (* int-map) (& [(. s maps) id]))
|
||||
(var step float (/ (cast (- (. m size) (. m pad-tl) (. m pad-br)) float) (cast 16 float)))
|
||||
(= group [(. s variant-group) id])
|
||||
(= rock [(. s variant-rock) id])
|
||||
(= top-down [(. s variant-top-down) id])
|
||||
(var allowed float (- (* [max group] distort) (cast [current group] float)))
|
||||
(var nx float (+ (* (cast rdx float) step)
|
||||
(/ (* step (cast (+ (cast lx float) dx) float)) (cast 32 float))))
|
||||
(var nz float (+ (* (cast rdz float) step)
|
||||
(/ (* step (cast (+ (cast lz float) dz) float)) (cast 32 float))))
|
||||
(if (< nx (cast -1.499 float)) (= nx (cast -1.499 float)))
|
||||
(if (< nz (cast -1.499 float)) (= nz (cast -1.499 float)))
|
||||
(= thickness (fminf allowed (lerped-correctly m nx nz)))
|
||||
(if (== group 0)
|
||||
(do
|
||||
(var above int (- upper (. s sea-level)))
|
||||
(if (< above 0) (= above 0))
|
||||
(-= thickness (* (cast above float) (cast 0.5 float)))))
|
||||
(if (< thickness (cast 2 float))
|
||||
(do (= thickness -1) (continue)))
|
||||
(if (== rock (. s granite))
|
||||
(do
|
||||
(var t int (cast thickness int))
|
||||
(+= [current group] t)
|
||||
(if top-down (-= upper t) (+= lower t))
|
||||
(continue)))))
|
||||
(++ [current group])
|
||||
(if top-down
|
||||
(do
|
||||
(if (== [out upper] (. s granite)) (= [out upper] (cast rock u8)))
|
||||
(-- upper))
|
||||
(do
|
||||
(if (== [out lower] (. s granite)) (= [out lower] (cast rock u8)))
|
||||
(++ lower)))))
|
||||
Reference in New Issue
Block a user