Files
VS-map-previewer/src/ores.sex
alex-eg 28f0e8acac pub-import the modules a unit's interface holds by value
sexc now checks that importers can see every type a pub form holds,
and asks for pub where the type comes from another import.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 18:05:14 +03:00

545 lines
22 KiB
Scheme

;;; Ore deposits as the prospecting pick's density mode sees them: per
;;; mineral, the region ore map GenDeposits makes, and the reading the
;;; pick gives for a column.
;;;
;;; A reading is the ore map's value there times the share of the
;;; column that is the mineral's host rock within the Y range its
;;; deposits are placed in (DiscDepositGenerator.GetPropickReading).
;;; It says how likely ore is near, not where the ore blocks are.
;;;
;;; Two things the game folds into a reading are left out:
;;; - The game draws a deposit's Y range from 100 random samples each
;;; time, from an RNG whose state depends on what ran before. The
;;; limits of the distribution are taken instead, so a range can be
;;; a block or two wider than the game's.
;;; - The pick's column includes the deposits generated in its own
;;; chunk. Those are not generated here, so a column is all host rock
;;; where the game may already have put ore in some of it. And a
;;; child deposit (silver, gold, and peridot, whose reading comes from
;;; the peridot inside olivine) is hosted by its parent's ore, so it
;;; can't be read at all without the deposits.
(include stdio.h stdlib.h string.h math.h dirent.h)
;; TODO sex#58: import only the modules used here once an import brings
;; the types its interface needs; until then, every dependency, in
;; dependency order.
(import cs)
(import maps)
(pub import lcg)
(import noise)
(import shape)
(import json)
(pub import worldgen)
(import fastnoise)
(import rivers)
(import terra)
(import strata)
(pub define MAX-MINERALS 48)
(define MAX-DEPOSIT-FILES 64)
;;; TerraGenConfig: ore map pixels per region side, and the scales of
;;; the ore noise and its wobble.
(define ORE-MAP-SIZE 32)
(define ORE-SUB-SCALE 16)
(define ORE-WOBBLE-SCALE 110)
;;; The reading's categories, as the pick words them: none, miniscule
;;; amounts, then very poor up to ultra high.
(pub define READING-CATEGORIES 8)
;;; Where a deposit generator puts its deposits, for the reading's Y
;;; range.
(define PLACE-NONE 0)
(define PLACE-FOLLOW-SURFACE 1)
(define PLACE-FOLLOW-SEALEVEL 2)
(define PLACE-FOLLOW-SURFACE-BELOW 3)
;;; NatFloat, with the distributions the deposits use.
(define DIST-UNIFORM 0)
(define DIST-GAUSSIAN 1)
(define DIST-INVEXP 2)
(define DIST-STRONGINVEXP 3)
(define DIST-TRIANGLE 4)
(define DIST-NARROWGAUSSIAN 5)
(define DIST-STRONGERINVEXP 6)
;; TODO sex#57: make this private again once a pub record can use a
;; private type.
(pub record natfloat ((dist int) (avg float) (var float)))
;;; A mineral is one code with an ore map. Its map is made with the
;;; parameters of the first deposit that asks for it; its reading comes
;;; from the last deposit with that code, as in ProPickWorkSpace.
(pub record mineral
((code [char 32])
;; the ore map: NoiseOre, MapLayerOre and MapLayerPerlinWobble
(noise lcg) (wobble-x (* simplex-noise)) (wobble-z (* simplex-noise))
(scale float) (contrast float) (sub float)
;; the reading
(readable bool) (place int) (y natfloat) (abs-avg double) (bearing u32)))
;; TODO sex#50: write MAX-MINERALS once constants in array types are
;; kebab-converted.
(pub record ores
((minerals [mineral MAX_MINERALS]) (count int) (granite int) (sea-level int)))
;;; The reading at one column: TotalFactor and PartsPerThousand.
(pub record mineral-reading ((total double) (ppt double)))
;;; NatFloat ---------------------------------------------------------
(var dist-names [(* const char) 7]
#("uniform" "gaussian" "invexp" "stronginvexp" "triangle" "narrowgaussian" "strongerinvexp"))
(fn read-natfloat ((o (* cJSON))) natfloat
(var n natfloat #(natfloat : DIST-UNIFORM 0 0))
(if (! o)
(return n))
(var dist (* const char) (cJSON-GetStringValue (cJSON-GetObjectItem o "dist")))
(for (var i int 0) (&& dist (< i 7)) (++ i)
(if (== (strcasecmp dist [dist-names i]) 0)
(= (. n dist) i)))
(= (. n avg) (cast (cJSON-GetNumberValue (cJSON-GetObjectItem o "avg")) float))
(= (. n var) (cast (cJSON-GetNumberValue (cJSON-GetObjectItem o "var")) float))
(if (isnan (. n avg)) (= (. n avg) 0))
(if (isnan (. n var)) (= (. n var) 0))
(return n))
;;; NatFloat.nextFloat(1, rand).
(fn natfloat-next ((n (* const natfloat)) (r (* lcg))) float
(var half float (cast 0.5 float))
(var two float (cast 2 float))
(var rnd float 0)
(switch (. n dist)
(case DIST-GAUSSIAN
(do
(var a float (.next-float r))
(var b float (.next-float r))
(var c float (.next-float r))
(= rnd (- (/ (+ a b c) (cast 3 float)) half)))
(return (+ (. n avg) (* rnd two (. n var)))))
(case DIST-NARROWGAUSSIAN
(do
(var sum float 0)
(for (var i int 0) (< i 6) (++ i)
(+= sum (.next-float r)))
(= rnd (- (/ sum (cast 6 float)) half)))
(return (+ (. n avg) (* rnd two (. n var)))))
(case DIST-INVEXP
(do
(var a float (.next-float r))
(var b float (.next-float r))
(= rnd (* a b)))
(return (+ (. n avg) (* rnd (. n var)))))
(case DIST-STRONGINVEXP
(do
(var a float (.next-float r))
(var b float (.next-float r))
(var c float (.next-float r))
(= rnd (* a b c)))
(return (+ (. n avg) (* rnd (. n var)))))
(case DIST-STRONGERINVEXP
(do
(var a float (.next-float r))
(var b float (.next-float r))
(var c float (.next-float r))
(var d float (.next-float r))
(= rnd (* a b c d)))
(return (+ (. n avg) (* rnd (. n var)))))
(case DIST-TRIANGLE
(do
(var a float (.next-float r))
(var b float (.next-float r))
(= rnd (- (/ (+ a b) two) half)))
(return (+ (. n avg) (* rnd two (. n var)))))
(default
(= rnd (- (.next-float r) half))
(return (+ (. n avg) (* rnd two (. n var)))))))
;;; The least and greatest values the distribution gives.
(fn natfloat-min ((n (* const natfloat))) float
(if (|| (== (. n dist) DIST-INVEXP) (== (. n dist) DIST-STRONGINVEXP)
(== (. n dist) DIST-STRONGERINVEXP))
(return (fminf (. n avg) (+ (. n avg) (. n var)))))
(return (fminf (- (. n avg) (. n var)) (+ (. n avg) (. n var)))))
(fn natfloat-max ((n (* const natfloat))) float
(if (|| (== (. n dist) DIST-INVEXP) (== (. n dist) DIST-STRONGINVEXP)
(== (. n dist) DIST-STRONGERINVEXP))
(return (fmaxf (. n avg) (+ (. n avg) (. n var)))))
(return (fmaxf (- (. n avg) (. n var)) (+ (. n avg) (. n var)))))
;;; Deposits ---------------------------------------------------------
(fn find-mineral ((o (* ores)) (code (* const char))) int
(for (var i int 0) (< i (. o count)) (++ i)
(if (== (strcmp (. [(. o minerals) i] code) code) 0)
(return i)))
(return -1))
(fn json-float ((o (* cJSON)) (key (* const char)) (fallback float)) float
(var v (* cJSON) (cJSON-GetObjectItem o key))
(if (cJSON-IsNumber v)
(return (cast (cJSON-GetNumberValue v) float)))
(return fallback))
;;; GenDeposits.getOrCreateMapLayer: the map for CODE, with the
;;; parameters of the deposit that asks first. Seeds are C# ints, so
;;; their sums wrap.
(fn add-map ((o (* ores)) (seed i32) (code (* const char)) (params (* cJSON))) void
(if (|| (>= (find-mineral o code) 0) (>= (. o count) MAX-MINERALS))
(return))
(var m (* mineral) (& [(. o minerals) (. o count)]))
(memset m 0 (sizeof mineral))
(snprintf (. m code) (sizeof (. m code)) "%s" code)
(var hash u32 (cast (nonrandomized-string-hash code) u32))
(var noise-seed i32 (cast (+ (cast seed u32) hash) i32))
(var map-seed i32 (cast (+ (cast seed u32) hash 1) i32))
(= (. m noise) (lcg/make noise-seed))
(var wobble-seed i64 (+ (cast map-seed i64) 2))
(var frequency float (/ (cast 1 float) (cast ORE-WOBBLE-SCALE float)))
(= (. m wobble-x) (simplex-noise/octaves 5 frequency (cast 0.85 float) wobble-seed))
(= (. m wobble-z) (simplex-noise/octaves 5 frequency (cast 0.85 float) (+ wobble-seed 1231296)))
(= (. m scale) (* (cast ORE-SUB-SCALE float) (json-float params "oreMapScale" 1)))
(= (. m contrast) (json-float params "oreMapContrast" 1))
(= (. m sub) (json-float params "oreMapSub" 0))
(++ (. o count)))
;;; Whether rock CODE is one INBLOCK's code and allowedVariants take.
(fn in-block-matches ((inblock (* cJSON)) (code (* const char))) bool
(var pattern (* const char) (cJSON-GetStringValue (cJSON-GetObjectItem inblock "code")))
(if (! pattern)
(return false))
(var star (* const char) (strchr pattern #\*))
(if (! star)
(return (== (strcmp pattern code) 0)))
(var prefix size-t (cast (- star pattern) size-t))
(if (!= (strncmp pattern code prefix) 0)
(return false))
(var allowed (* cJSON) (cJSON-GetObjectItem inblock "allowedVariants"))
(if (! allowed)
(return true))
(for (var v (* cJSON) (. allowed child)) v (= v (. v next))
(var s (* const char) (cJSON-GetStringValue v))
(if (&& s (== (strcmp s (+ code prefix)) 0))
(return true)))
(return false))
;;; DiscDepositGenerator.GetAbsAvgQuantity with a fresh LCGRandom of
;;; the world seed, as Init calls it.
(fn abs-avg-quantity ((seed i32) (radius (* const natfloat)) (thickness (* const natfloat))
(tries float)) double
(var r lcg (lcg/make seed))
(var rs float 0)
(var ts float 0)
(for (var j int 0) (< j 100) (++ j)
(+= rs (natfloat-next radius (& r)))
(+= ts (natfloat-next thickness (& r))))
(/= rs (cast 100 float))
(/= ts (cast 100 float))
(var pi float (cast M-PI float))
(return (* ts rs rs pi tries)))
;;; The reading for CODE now comes from deposit V; a child deposit,
;;; hosted by its parent's ore, can't be read.
(fn set-reading ((o (* ores)) (v (* cJSON)) (child bool) (seed i32) (rocks (* const strata))) void
(var i int (find-mineral o (cJSON-GetStringValue (cJSON-GetObjectItem v "code"))))
(if (< i 0)
(return))
(var m (* mineral) (& [(. o minerals) i]))
(= (. m readable) false)
(= (. m place) PLACE-NONE)
(var gen (* const char) (cJSON-GetStringValue (cJSON-GetObjectItem v "generator")))
(var attributes (* cJSON) (cJSON-GetObjectItem v "attributes"))
(if (|| child (! gen) (! attributes))
(return))
(if (== (strcmp gen "disc-followsurface") 0)
(do
(= (. m place) PLACE-FOLLOW-SURFACE)
(= (. m y) (read-natfloat (cJSON-GetObjectItem attributes "yPosRel"))))
(== (strcmp gen "disc-followsealevel") 0)
(do
(= (. m place) PLACE-FOLLOW-SEALEVEL)
(= (. m y) (read-natfloat (cJSON-GetObjectItem attributes "yPosRel")))
;; FollowSealevelDiscGenerator.Init
(*= (. m y avg) (cast (. o sea-level) float))
(*= (. m y var) (cast (. o sea-level) float)))
(== (strcmp gen "disc-followsurfacebelow") 0)
(do
(= (. m place) PLACE-FOLLOW-SURFACE-BELOW)
(= (. m y) (read-natfloat (cJSON-GetObjectItem attributes "depth"))))
(return))
(var radius natfloat (read-natfloat (cJSON-GetObjectItem attributes "radius")))
(if (! (cJSON-GetObjectItem attributes "radius"))
(= radius #(natfloat : DIST-UNIFORM 10 0)))
(var thickness natfloat (read-natfloat (cJSON-GetObjectItem attributes "thickness")))
(= (. m abs-avg) (abs-avg-quantity seed (& radius) (& thickness) (json-float v "triesPerChunk" 0)))
(var inblock (* cJSON) (cJSON-GetObjectItem attributes "inblock"))
(= (. m bearing) 0)
(for (var r int 0) (&& inblock (< r (. rocks rock-count)) (< r 32)) (++ r)
(if (in-block-matches inblock [(. rocks rock-codes) r])
(c-bit-or= (. m bearing) (<< (cast 1 u32) r))))
(= (. m readable) true))
(fn by-name ((pa (* const void)) (pb (* const void))) int
(var a (* * char) (cast pa (* * char)))
(var b (* * char) (cast pb (* * char)))
(return (strcmp (* a) (* b))))
;;; The JSON files under DIR, relative to ROOT, into PATHS.
(fn list-files ((root (* const char)) (dir (* const char)) (paths (* * char)) (n (* int))) void
(var full [char 1024])
(snprintf full (sizeof full) "%s/%s" root dir)
(var d (* DIR) (opendir full))
(if (! d)
(return))
(var ent (* struct dirent) NULL)
(while (&& (= ent (readdir d)) (< (* n) MAX-DEPOSIT-FILES))
(var name (* const char) (. ent d-name))
(if (== [name 0] #\.)
(continue))
(var rel [char 1024])
(if (* dir)
(snprintf rel (sizeof rel) "%s/%s" dir name)
(snprintf rel (sizeof rel) "%s" name))
(var len size-t (strlen name))
(if (== (. ent d-type) DT-DIR)
(list-files root rel paths n)
(&& (> len 5) (== (strcmp (+ name (- len 5)) ".json") 0))
(do
(= [paths (* n)] (strdup rel))
(++ (* n)))))
(closedir d))
;;; GenDeposits.initAssets and initWorldGen, and ProPickWorkSpace's
;;; depositsByCode: deposits/*.json in path order, each file's
;;; variants in order, children after their parent.
(pub fn ores/make ((settings world-settings) (assets-dir (* const char)) (rocks (* const strata)))
(* ores)
(var o (* ores) (calloc 1 (sizeof ores)))
(= (. o granite) (. rocks granite))
(= (. o sea-level) (cast (* 0.4313725490196078 (. settings size-y)) int))
(var root [char 1024])
(snprintf root (sizeof root) "%s/survival/worldgen/deposits" assets-dir)
(var paths [* char MAX_DEPOSIT_FILES])
(var n int 0)
(list-files root "" paths (& n))
(qsort paths n (sizeof [paths 0]) by-name)
;; TODO sex#60: size with MAX-DEPOSIT-FILES once arrays of a header
;; type take a symbolic size.
(var files [* cJSON 64])
(for (var f int 0) (< f n) (++ f)
(var path [char 1024])
(snprintf path (sizeof path) "%s/%s" root [paths f])
(= [files f] (json-load path))
(free [paths f]))
(var seed i32 (. settings seed))
;; the maps, first come first served
(for (var f int 0) (< f n) (++ f)
(for (var v (* cJSON) (if [files f] (. [files f] child) NULL)) v (= v (. v next))
(if (cJSON-IsTrue (cJSON-GetObjectItem v "withOreMap"))
(add-map o seed (cJSON-GetStringValue (cJSON-GetObjectItem v "code")) v))
(var children (* cJSON) (cJSON-GetObjectItem v "childDeposits"))
(for (var c (* cJSON) (if children (. children child) NULL)) c (= c (. c next))
;; a child's map takes its parent's parameters
(if (cJSON-IsTrue (cJSON-GetObjectItem c "withOreMap"))
(add-map o seed (cJSON-GetStringValue (cJSON-GetObjectItem c "code")) v)))))
;; the readings, last come last served
(for (var f int 0) (< f n) (++ f)
(for (var v (* cJSON) (if [files f] (. [files f] child) NULL)) v (= v (. v next))
(if (cJSON-IsTrue (cJSON-GetObjectItem v "withOreMap"))
(set-reading o v false seed rocks))
(var children (* cJSON) (cJSON-GetObjectItem v "childDeposits"))
(for (var c (* cJSON) (if children (. children child) NULL)) c (= c (. c next))
(if (cJSON-IsTrue (cJSON-GetObjectItem c "withOreMap"))
(set-reading o c true seed rocks)))))
(for (var f int 0) (< f n) (++ f)
(if [files f] (cJSON-Delete [files f])))
(return o))
(pub fn ores/free ((o (* ores))) void
(for (var i int 0) (< i (. o count)) (++ i)
(free (. [(. o minerals) i] wobble-x))
(free (. [(. o minerals) i] wobble-z)))
(free o))
(pub fn ores/find ((o (* const ores)) (code (* const char))) int
(for (var i int 0) (< i (. o count)) (++ i)
(if (== (strcmp (. [(. o minerals) i] code) code) 0)
(return i)))
(return -1))
;;; Ore maps ---------------------------------------------------------
(fn smooth-step ((x float)) float
(return (* x x (- (cast 3 float) (* (cast 2 float) x)))))
(fn lerp-byte ((t float) (a int) (b int)) int
(return (cast (cast (+ (* (- (cast 1 float) t) (cast a float)) (* t (cast b float))) int) u8)))
;;; NoiseOre.GetRandomOre: density in the low byte, hyper-rich at 256,
;;; quality at 1024 and 2048.
(fn random-ore ((r (* lcg))) int
(var rnd int (.next-int r 1024))
(var quality int 0)
(if (> (.next-int r 2) 0)
(= quality 1)
(if (> (.next-int r 50) 15) (= quality 0) (= quality 2)))
(= quality (<< quality 10))
(if (< rnd 1) (return (c-bit-or quality 256 255)))
(if (< rnd 30) (return (c-bit-or quality 255)))
(if (< rnd 105) (return (c-bit-or quality (+ 75 (.next-int r 100)))))
(if (< rnd 190) (return (c-bit-or quality (+ 20 (.next-int r 20)))))
(return 0))
(fn max4 ((a int) (b int) (c int) (d int) (mask int)) int
(var m int (& a mask))
(if (> (& b mask) m) (= m (& b mask)))
(if (> (& c mask) m) (= m (& c mask)))
(if (> (& d mask) m) (= m (& d mask)))
(return m))
;;; NoiseOre.GetLerpedOreValueAt.
(fn lerped-ore ((x double) (z double) (cache (* const int)) (w int) (contrast float)
(sub float)) int
(var xi int (cast x int))
(var zi int (cast z int))
(var lt int [cache (+ (* zi w) xi)])
(var rt int [cache (+ (* zi w) xi 1)])
(var lb int [cache (+ (* (+ zi 1) w) xi)])
(var rb int [cache (+ (* (+ zi 1) w) xi 1)])
(var fx float (smooth-step (cast (- x xi) float)))
(var fz float (smooth-step (cast (- z zi) float)))
(var top int (lerp-byte fx (& lt 255) (& rt 255)))
(var bottom int (lerp-byte fx (& lb 255) (& rb 255)))
(var v float (cast (lerp-byte fz top bottom) float))
(var c float (- (+ (* (- v (cast 128 float)) contrast) (cast 128 float)) sub))
(if (< c (cast 0 float)) (= c 0))
(if (> c (cast 255 float)) (= c 255))
(var val int (cast (cast c int) u8))
(if (== val 0)
(return 0))
;; TODO sex#47: write the masks (0xFF0000, 0xFF00) in hex once there
;; are hex literals.
(return (c-bit-or val (max4 lt rt lb rb 16711680) (max4 lt rt lb rb 65280))))
;;; MapLayerOre.GenLayer.
(fn gen-unwobbled ((m (* const mineral)) (x0 int) (z0 int) (size-x int) (size-z int)
(out (* int))) void
(var noise lcg (. m noise))
(var scale float (. m scale))
(var cache-w int (+ (cast (ceil (cast (/ (cast size-x float) scale) double)) int) 1))
(var cache-h int (+ (cast (ceil (cast (/ (cast size-z float) scale) double)) int) 1))
(var cx int (cast (/ (cast x0 float) scale) int))
(var cz int (cast (/ (cast z0 float) scale) int))
(var cache (* int) (malloc (* cache-w cache-h (sizeof int))))
(for (var x int 0) (< x cache-w) (++ x)
(for (var z int 0) (< z cache-h) (++ z)
(.init-position (& noise) (+ cx x) (+ cz z))
(= [cache (+ (* z cache-w) x)] (random-ore (& noise)))))
(for (var x int 0) (< x size-x) (++ x)
(for (var z int 0) (< z size-z) (++ z)
(= [out (+ (* z size-x) x)]
(lerped-ore (/ x (cast scale double)) (/ z (cast scale double)) cache cache-w
(. m contrast) (. m sub)))))
(free cache))
;;; MapLayerPerlinWobble over MapLayerOre, for region (rx, rz): the map
;;; DepositVariant.OnMapRegionGen stores, 33 pixels a side with one of
;;; padding at the bottom right.
(pub fn ores/map ((o (* const ores)) (i int) (rx int) (rz int) (out (* int-map))) void
(var m (* const mineral) (& [(. o minerals) i]))
(var size int (+ ORE-MAP-SIZE 1))
(var x0 int (* rx ORE-MAP-SIZE))
(var z0 int (* rz ORE-MAP-SIZE))
(var intensity float (* (cast ORE-WOBBLE-SCALE float) (cast 0.15 float)))
(var margin int (cast (ceil (cast intensity double)) int))
(var pw int (+ size (* 2 margin)))
(var in (* int) (malloc (* pw pw (sizeof int))))
(gen-unwobbled m (- x0 margin) (- z0 margin) pw pw in)
(= (. out size) size)
(= (. out pad-tl) 0)
(= (. out pad-br) 1)
(= (. out data) (malloc (* size size (sizeof i32))))
(for (var y int 0) (< y size) (++ y)
(for (var x int 0) (< x size) (++ x)
(var nx double (+ x0 x margin))
(var nz double (+ z0 y margin))
(var ox int (cast (* intensity (.noise-2d (. m wobble-x) nx nz)) int))
(var oz int (cast (* intensity (.noise-2d (. m wobble-z) nx nz)) int))
(var sx int (% (+ (% (+ x ox (/ margin 2)) pw) pw) pw))
(var sz int (% (+ (% (+ y oz (/ margin 2)) pw) pw) pw))
(= [(. out data) (+ (* y size) x)] [in (+ (* sz pw) sx)])))
(free in))
;;; Readings ---------------------------------------------------------
;;; IntDataMap2D.GetUnpaddedColorLerped, the low byte only.
(fn ore-factor ((map (* const int-map)) (x int) (z int)) double
(var inner int (- (. map size) (. map pad-tl) (. map pad-br)))
(var px float (* (/ (cast (% x 512) float) (cast 512 float)) (cast inner float)))
(var pz float (* (/ (cast (% z 512) float) (cast 512 float)) (cast inner float)))
(var ix int (cast px int))
(var iz int (cast pz int))
(var fx float (- px (cast ix float)))
(var fz float (- pz (cast iz float)))
(var i int (+ (* (+ iz (. map pad-tl)) (. map size)) ix (. map pad-tl)))
(var lt float (cast (& [(. map data) i] 255) float))
(var rt float (cast (& [(. map data) (+ i 1)] 255) float))
(var lb float (cast (& [(. map data) (+ i (. map size))] 255) float))
(var rb float (cast (& [(. map data) (+ i (. map size) 1)] 255) float))
(var top float (+ lt (* (- rt lt) fx)))
(var bottom float (+ lb (* (- rb lb) fx)))
(var v int (& (cast (+ top (* (- bottom top) fz)) int) 255))
(return (/ v 255.0)))
;;; The pick's reading of mineral I at column (x, z): MAP is the
;;; mineral's ore map of the column's region, COLUMN the column's
;;; strata up to its surface at HEIGHT.
(pub fn ores/reading ((o (* const ores)) (i int) (map (* const int-map)) (x int) (z int)
(column (* const u8)) (height int)) mineral-reading
(var r mineral-reading #(mineral-reading : 0 0))
(var m (* const mineral) (& [(. o minerals) i]))
(if (|| (! (. m readable)) (< height 1))
(return r))
;; GetYMinMax
(var miny double 0)
(var maxy double 0)
(if (== (. m place) PLACE-FOLLOW-SURFACE)
(do
(= miny (* (natfloat-min (& (. m y))) (cast height float)))
(= maxy height))
(== (. m place) PLACE-FOLLOW-SEALEVEL)
(do
(= miny (natfloat-min (& (. m y))))
(= maxy (natfloat-max (& (. m y)))))
(do
(= miny (- height (natfloat-max (& (. m y)))))
(= maxy height)))
;; oreBearingBlockQuantityRelative over the pick's column, y below
;; the surface; it has the default rock at y = 0, not the mantle
(var q int 0)
(for (var y int 0) (< y height) (++ y)
(if (|| (< y miny) (> y maxy))
(continue))
(var rock int (if (== y 0) (. o granite) [column y]))
(if (&& (< rock 32) (!= (& (. m bearing) (<< (cast 1 u32) rock)) 0))
(++ q)))
(var rock-factor double (/ (cast q double) height))
(= (. r total) (* (ore-factor map x z) rock-factor))
(var blocks int (* height 1024))
(= (. r ppt) (* (/ (* (. r total) (. m abs-avg)) blocks) 1000))
(return r))
;;; The category the pick words a reading in: 0 nothing, 1 miniscule
;;; amounts, 2 very poor to 7 ultra high.
(pub fn ores/category ((total double)) int
(if (<= total 0.002)
(return 0))
(if (<= total 0.025)
(return 1))
(var k double (* total (cast 7.5 float)))
(if (> k 5) (= k 5))
(return (+ 2 (cast k int))))