Compare commits

...

18 Commits

Author SHA1 Message Date
ce445ce27a depend on the modules each Sex unit reads
sexc writes the rule now (sex#40), so a module's change rebuilds the
units that read it rather than every Sex object.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 18:24:03 +03:00
1b6ac779a8 import only the modules each unit uses
An import brings the types and constants its interface needs now
(sex#58): a module pub-imports what its pub forms hold, and sexc
refuses one that does not. Every unit had imported its whole
dependency chain in order to make up for it.

worldgen also pub-imports json, whose cJSON.h its pub fns take
pointers into; a type only C knows is not something sexc can check.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 18:22:41 +03:00
b8030b86bc make shape-cell private again
A pub record may point to a private one now (sex#57); its importers
are handed the declaration. natfloat stays pub: a mineral holds one by
value, so importers need its size.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 18:19:00 +03:00
f82d2499c6 name the identifier predicates with a ?
sexc spells ? in a C name now (sex#49).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 18:18:20 +03:00
10ec6236fd order pb's methods by topic, and read rock-mode in the literal
A method can call one written below it now (sex#44), so pb's read as
an API: making one, asking about it, reading it, then the private
helpers. A path inside a compound literal follows the pointer
(sex#45). bytes keeps its local: the window starts where self stood
before it moves.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 18:17:55 +03:00
fee9a1c28e make the wire types an enum
Explicit enum values are converted now (sex#46).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 18:16:56 +03:00
c9daae367f write the noise constants the C# divides as divisions
A define's body is walked and parenthesised now (sex#55). The four
written as divisions fold to exactly the doubles they replace.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 18:16:06 +03:00
3ff6dcaa74 drop the blocks around case bodies, and size block codes by type
A case body that declares is its own block now (sex#51), and sizeof
takes a pointer type (sex#52).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 18:12:51 +03:00
7cb49f83cd name the colour function type
A typedef of a function pointer type compiles now (sex#53).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 18:12:04 +03:00
e0901f6c8a pass the river colour as a conditional, with #line on every statement
A #line no longer lands inside a ?: (sex#54), so the build goes back
to statement-level directives, which is what lands a debugger on the
right line.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 18:11:37 +03:00
e4ab3e875f put conditional operands back where they are used
A conditional expression as an operand keeps its parentheses now
(sex#59), so the clamps and offsets are written inline again. The
story's z direction stays a local: it is drawn before the distances,
and inlining it would reorder the draws.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 18:11:17 +03:00
d40455d824 read the compared distances through the casts
A cast as the object of `.' keeps its parentheses now (sex#61), so
the comparator takes the two distances straight from its arguments.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 18:10:24 +03:00
b74c445b25 cast the odd lattice coordinates to float directly
A cast of a bitwise expression keeps its parentheses now (sex#62), so
the int locals that held them are gone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 18:10:00 +03:00
d2721b55f7 write -INFINITY
sexc reads a dash before a name as negation now (sex#63).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 18:09:34 +03:00
74200559b8 write masks, colours and hash constants in hex
sexc reads hex literals now (sex#47). The OpenSimplex constants are
spelled as the C# spells them, and the colours as the comments that
stood in for them did.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 18:09:08 +03:00
9bc1167782 size the thread and file arrays by their constants
An array of a header type takes a symbolic size now (sex#60). The
records holding them are pub, so their constants are too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 18:07:07 +03:00
d299ba05ac size arrays by the constants' own names
sexc converts an array bound's name now (sex#50), so MAX_STRATA and
the like go back to being written as defined.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 18:06:24 +03:00
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
22 changed files with 207 additions and 432 deletions

View File

@@ -13,19 +13,13 @@ B = build
all: $(B)/vsmv $(B)/vs-map-viewer
# sexc writes no dependency files, and an import pastes the imported
# module's public forms into the importer, so any Sex source may change
# any Sex object.
# TODO sex#40: depend on what each unit imports once sexc writes
# dependency files.
SEX_SRC := $(wildcard src/*.sex)
# An import pastes the imported module's public forms into the
# importer, so a Sex object depends on every module it reads; sexc
# names them in a .d beside the object.
$(B)/%.o: src/%.sex | $(B)
$(SEXC) $< -c -o $@ --depfile=$(@:.o=.d) -- $(CFLAGS) $(SEX_CFLAGS) $(CPPFLAGS)
# Statement-level #line directives can land inside a ?: and break the
# C (see Sex-field-notes.org), so they are kept to toplevel forms.
# TODO sex#54: drop --line-directives=toplevel once #line no longer
# lands inside a ?:.
$(B)/%.o: src/%.sex $(SEX_SRC) | $(B)
$(SEXC) --line-directives=toplevel $< -c -o $@ -- $(CFLAGS) $(SEX_CFLAGS) $(CPPFLAGS)
-include $(wildcard $(B)/*.d)
$(B)/nk.o: src/nk.c src/nk.h | $(B)
$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@

View File

@@ -10,17 +10,15 @@
(var p (* const u8) (* s))
(var c u32 [p 0])
(var n int 0)
;; TODO sex#47: write the UTF-8 masks in hex once there are hex
;; literals.
(if (< c 128) (= n 0)
(== (& c 224) 192) (do (= n 1) (&= c 31))
(== (& c 240) 224) (do (= n 2) (&= c 15))
(== (& c 248) 240) (do (= n 3) (&= c 7))
(do (= (* s) (+ p 1)) (return 65533)))
(if (< c 0x80) (= n 0)
(== (& c 0xE0) 0xC0) (do (= n 1) (&= c 0x1F))
(== (& c 0xF0) 0xE0) (do (= n 2) (&= c 0x0F))
(== (& c 0xF8) 0xF0) (do (= n 3) (&= c 0x07))
(do (= (* s) (+ p 1)) (return 0xFFFD)))
(for (var i int 1) (<= i n) (++ i)
(if (!= (& [p i] 192) 128)
(do (= (* s) (+ p i)) (return 65533)))
(= c (c-bit-or (<< c 6) (& [p i] 63))))
(if (!= (& [p i] 0xC0) 0x80)
(do (= (* s) (+ p i)) (return 0xFFFD)))
(= c (c-bit-or (<< c 6) (& [p i] 0x3F))))
(= (* s) (+ p n 1))
(return c))
@@ -34,16 +32,14 @@
(var p (* const u8) (cast text (* const u8)))
(while (* p)
(var c u32 (utf8-next (& p)))
(if (< c 65536)
(if (< c 0x10000)
(do
(= [units n] c)
(+= n 1))
(do
;; TODO sex#47: write the surrogate constants in hex once
;; there are hex literals.
(-= c 65536)
(= [units n] (+ 55296 (>> c 10)))
(= [units (+ n 1)] (+ 56320 (& c 1023)))
(-= c 0x10000)
(= [units n] (+ 0xD800 (>> c 10)))
(= [units (+ n 1)] (+ 0xDC00 (& c 0x3FF)))
(+= n 2))))
(= (* out) units)
(return n))
@@ -71,9 +67,7 @@
(if (> len 0)
(= h1 (hash-step h1 (c-bit-or [units i] (<< (cast [units (+ i 1)] u32) 16)))))
(free units)
;; TODO sex#47: write the multiplier (0x5D588B65) in hex once there
;; are hex literals.
(return (cast (+ (cast h1 u32) (* (cast h2 u32) (cast 1566083941 u32))) i32)))
(return (cast (+ (cast h1 u32) (* (cast h2 u32) (cast 0x5D588B65 u32))) i32)))
;;; The world seed from the seed text in world settings. A number is
;;; taken as it is, anything else is hashed.
@@ -109,6 +103,4 @@
(if (> len 0)
(= h2 (^ (+ (rotl5 h2) h2) (c-bit-or [units i] (<< (cast [units (+ i 1)] u32) 16)))))
(free units)
;; TODO sex#47: write the multiplier (0x5D588B65) in hex once there
;; are hex literals.
(return (cast (+ h1 (* h2 (cast 1566083941 u32))) i32)))
(return (cast (+ h1 (* h2 (cast 0x5D588B65 u32))) i32)))

View File

@@ -9,17 +9,16 @@
(fn landform-colour ((v i32)) u32 (return (palette (cast v u32))))
(fn province-colour ((v i32)) u32 (return (palette (+ (cast v u32) 100))))
;; TODO sex#47: write the mask in hex once there are hex literals.
(fn climate-colour ((v i32)) u32 (return (& (cast v u32) 16777215)))
(fn climate-colour ((v i32)) u32 (return (& (cast v u32) 0xFFFFFF)))
(fn byte-colour ((v i32)) u32 (return (grey v)))
(typedef colour-fn (fn ((i32)) u32))
;;; The inner square of one region's map, placed at (ox, oz) and
;;; stretched to CELL pixels a side, so maps of different resolutions
;;; line up pixel for pixel across mosaics.
;; TODO sex#53: name the colour function type with a typedef once that
;; no longer crashes sexc.
(fn paint-map ((im (* image)) (m (* const int-map)) (ox int) (oz int) (cell int)
(colour (fn ((i32)) u32))) void
(colour colour-fn)) void
(var inner int (- (. m size) (. m pad-tl) (. m pad-br)))
(if (<= inner 0)
(return))
@@ -51,9 +50,7 @@
(var province image (image/make w h))
(var ocean image (image/make w h))
(var upheaval image (image/make w h))
;; TODO sex#50: write MAX-STRATA once constants in array types are
;; kebab-converted.
(var strata [image MAX_STRATA])
(var strata [image MAX-STRATA])
(var strata-count int 0)
(var m region-maps)
@@ -131,14 +128,8 @@
(if (. m has-river-distance)
(do
(var d int [(. m river-distance) i])
;; TODO sex#54: pass the colour as an (if ...) argument to
;; .set once #line no longer splits it.
(var c u32 (grey (- 255 d)))
;; TODO sex#47: write the colours in hex once there are
;; hex literals.
(if (== d 0) (= c 4219071))
(.set (& river) x y c))
(.set (& river) x y 2105376)))))
(.set (& river) x y (if (== d 0) 0x4060BF (grey (- 255 d)))))
(.set (& river) x y 0x202020)))))
(write-image (& height) dir "height")
(write-image (& rock) dir "toprock")

View File

@@ -9,20 +9,12 @@
(pub include pthread.h)
(include stdio.h stdlib.h string.h math.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 maps)
(import lcg)
(import noise)
(import shape)
(import json)
(import worldgen)
(import fastnoise)
(import rivers)
(pub import worldgen)
(pub import rivers)
(import terra)
(import strata)
(import ores)
(pub import ores)
;;; MINERAL is the mineral whose reading is wanted, or -1.
(pub record hover-query ((x int) (z int) (mode int) (value int) (mineral int)))

View File

@@ -27,12 +27,10 @@
;;; A distinct, stable colour per small integer: landform and province
;;; indices, block ids.
(pub fn palette ((i u32)) u32
;; TODO sex#47: write the multiplier (0x9E3779B1) and masks in hex
;; once there are hex literals.
(var h u32 (* (+ i 1) (cast 2654435761 u32)))
(return (c-bit-or (<< (+ 48 (& (>> h 24) 191)) 16)
(<< (+ 48 (& (>> h 16) 191)) 8)
(+ 48 (& (>> h 8) 191)))))
(var h u32 (* (+ i 1) (cast 0x9E3779B1 u32)))
(return (c-bit-or (<< (+ 48 (& (>> h 24) 0xBF)) 16)
(<< (+ 48 (& (>> h 16) 0xBF)) 8)
(+ 48 (& (>> h 8) 0xBF)))))
(pub fn grey ((v int)) u32
(if (< v 0) (= v 0))

View File

@@ -6,12 +6,11 @@
(pub include "cJSON.h")
(include stdio.h stdlib.h string.h ctype.h)
;; TODO sex#49: rename to ident-start? once ? is mangled in names.
(fn ident-start-char ((c char)) bool
(fn ident-start? ((c char)) bool
(return (|| (isalpha (cast c u8)) (== c #\_) (== c #\$))))
(fn ident-char ((c char)) bool
(return (|| (ident-start-char c) (isdigit (cast c u8)))))
(fn ident-char? ((c char)) bool
(return (|| (ident-start? c) (isdigit (cast c u8)))))
;;; Drops a comma that was the last thing written before a closing
;;; bracket: `[1, 2, ]' -> `[1, 2 ]'.
@@ -58,10 +57,10 @@
(do
(= n (drop-trailing-comma out n))
(= [out n] c) (+= n 1) (+= p 1))
(ident-start-char c)
(ident-start? c)
(do
(var start (* const char) p)
(while (ident-char (* p))
(while (ident-char? (* p))
(+= p 1))
(var len size-t (cast (- p start) size-t))
(var bare bool (|| (&& (== len 4) (|| (== (strncmp start "true" 4) 0)

View File

@@ -10,11 +10,9 @@
;;; (size - pad-tl - pad-br) square.
(pub record int-map ((data (* i32)) (size i32) (pad-tl i32) (pad-br i32)))
;; TODO sex#50: write MAX-STRATA once constants in array types are
;; kebab-converted.
(pub record region-maps
((landform int-map) (climate int-map) (geoprov int-map) (upheaval int-map)
(ocean int-map) (strata [int-map MAX_STRATA]) (strata-count int)))
(ocean int-map) (strata [int-map MAX-STRATA]) (strata-count int)))
(pub fn region-maps/free ((m (* region-maps))) void
(free (. m landform data))

View File

@@ -7,16 +7,14 @@
(pub include stdint.h)
(include stdlib.h math.h)
;;; The C# folds these at compile time; written as divisions here they
;;; would be unparenthesized macros and round differently in context.
;; TODO sex#55: write the divisions, e.g. (/ -1.0 6.0), once define
;; bodies are translated and parenthesized.
;;; As the C# writes them. Its divisions are folded at compile time,
;;; and C's are too, to the same doubles.
(define STRETCH-2D -0.211324865405187)
(define SQUISH-2D 0.366025403784439)
(define STRETCH-3D -0.16666666666666666)
(define SQUISH-3D 0.3333333333333333)
(define NORM-2D 0.02127659574468085)
(define NORM-3D 0.009708737864077669)
(define STRETCH-3D (/ -1.0 6.0))
(define SQUISH-3D (/ 1.0 3.0))
(define NORM-2D (/ 1.0 47.0))
(define NORM-3D (/ 1.0 103.0))
(define NORM-2D-WARP 0.020435461373899677)
(pub define MAX-VALUE-3D 0.9871465422297019)
(define VALUE-MULTIPLIER 1.2)
@@ -250,12 +248,10 @@
;;; NormalizedSimplexNoise. Octave i samples at frequencies[i] and is
;;; weighted so the sum stays in a known range before tanh.
;; TODO sex#50: write MAX-OCTAVES once constants in array types are
;; kebab-converted.
(pub record simplex-noise
((count int) (frequencies [double MAX_OCTAVES]) (amplitudes [double MAX_OCTAVES])
(scaled-2d [double MAX_OCTAVES]) (scaled-3d [double MAX_OCTAVES])
(octaves [simplex-octave MAX_OCTAVES])))
((count int) (frequencies [double MAX-OCTAVES]) (amplitudes [double MAX-OCTAVES])
(scaled-2d [double MAX-OCTAVES]) (scaled-3d [double MAX-OCTAVES])
(octaves [simplex-octave MAX-OCTAVES])))
(ops simplex-noise
(pub fn make ((amplitudes (* const double)) (frequencies (* const double)) (count int)
@@ -280,10 +276,8 @@
;; PERSISTENCE^i.
(pub fn octaves ((count int) (base-frequency double) (persistence double) (seed i64))
(* simplex-noise)
;; TODO sex#50: write MAX-OCTAVES once constants in array types are
;; kebab-converted.
(var frequencies [double MAX_OCTAVES])
(var amplitudes [double MAX_OCTAVES])
(var frequencies [double MAX-OCTAVES])
(var amplitudes [double MAX-OCTAVES])
(for (var i int 0) (< i count) (++ i)
(= [frequencies i] (* (pow 2 i) base-frequency))
(= [amplitudes i] (pow persistence i)))
@@ -307,11 +301,9 @@
;;; SimplexNoise: plain octave sums, no normalising and no tanh. The
;;; rock strata thickness maps and the strata distortion use it.
;; TODO sex#50: write MAX-OCTAVES once constants in array types are
;; kebab-converted.
(pub record simplex-sum
((count int) (frequencies [double MAX_OCTAVES]) (amplitudes [double MAX_OCTAVES])
(octaves [simplex-octave MAX_OCTAVES])))
((count int) (frequencies [double MAX-OCTAVES]) (amplitudes [double MAX-OCTAVES])
(octaves [simplex-octave MAX-OCTAVES])))
(ops simplex-sum
(pub fn make ((amplitudes (* const double)) (frequencies (* const double)) (count int)

View File

@@ -20,18 +20,13 @@
;;; 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)
(import lcg)
(pub import lcg)
(import noise)
(import shape)
(import json)
(import worldgen)
(import fastnoise)
(import rivers)
(pub import worldgen)
(import terra)
(import strata)
@@ -63,8 +58,7 @@
(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 because a mineral holds one, and its importers need the size
(pub record natfloat ((dist int) (avg float) (var float)))
;;; A mineral is one code with an ore map. Its map is made with the
@@ -78,10 +72,8 @@
;; 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)))
((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)))
@@ -321,13 +313,11 @@
(= (. 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 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])
(var files [* cJSON MAX-DEPOSIT-FILES])
(for (var f int 0) (< f n) (++ f)
(var path [char 1024])
(snprintf path (sizeof path) "%s/%s" root [paths f])
@@ -420,9 +410,7 @@
(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))))
(return (c-bit-or val (max4 lt rt lb rb 0xFF0000) (max4 lt rt lb rb 0xFF00))))
;;; MapLayerOre.GenLayer.
(fn gen-unwobbled ((m (* const mineral)) (x0 int) (z0 int) (size-x int) (size-z int)

View File

@@ -8,51 +8,26 @@
(pub include stddef.h)
(include string.h)
;; TODO sex#46: make these a (pub enum wire ...) once explicit enum
;; values are kebab-converted.
(pub define WIRE-VARINT 0)
(pub define WIRE-FIXED64 1)
(pub define WIRE-BYTES 2)
(pub define WIRE-FIXED32 5)
(pub enum wire ((WIRE-VARINT 0) (WIRE-FIXED64 1) (WIRE-BYTES 2) (WIRE-FIXED32 5)))
(pub record pb ((p (* const u8)) (end (* const u8)) (err bool)))
;; TODO sex#44: the methods are ordered so each calls only those above
;; it; order them by topic once a method can call one below.
(ops pb
(pub fn of ((data (* const void)) (len size-t)) pb
(var p (* const u8) data)
(return #(pb : p (+ p len) false)))
(fn fail ((self (* pb))) void
(= (. self err) true)
(= (. self p) (. self end)))
(pub fn more ((self (* const pb))) bool
(return (< (. self p) (. self end))))
(pub fn len ((self (* const pb))) size-t
(return (cast (- (. self end) (. self p)) size-t)))
(pub fn varint ((self (* pb))) u64
(var v u64 0)
(for (var shift int 0) (< shift 64) (+= shift 7)
(if (>= (. self p) (. self end))
(break))
(var b u8 (* (. self p)))
(++ (. self p))
;; TODO sex#47: write the varint masks in hex once there are hex
;; literals.
(c-bit-or= v (<< (cast (& b 127) u64) shift))
(if (< b 128)
(return v)))
(.fail self)
(return 0))
;; A packed or repeated `int' field: negative values arrive as ten
;; byte varints and keep their meaning when truncated.
(pub fn int32 ((self (* pb))) i32
(return (cast (.varint self) i32)))
;; True when the rest of the window spells exactly S, e.g. a map
;; entry's key against "riverDistance".
(pub fn equals ((self (* const pb)) (s (* const char))) bool
(var n size-t (.len self))
(return (&& (== (strlen s) n) (== (memcmp (. self p) s n) 0))))
;; The next field's number, or 0 at the end. WIRE gets its wire type.
(pub fn field ((self (* pb)) (wire (* int))) u32
@@ -62,12 +37,29 @@
(= (* wire) (cast (& key 7) int))
(return (cast (>> key 3) u32)))
(pub fn varint ((self (* pb))) u64
(var v u64 0)
(for (var shift int 0) (< shift 64) (+= shift 7)
(if (>= (. self p) (. self end))
(break))
(var b u8 (* (. self p)))
(++ (. self p))
(c-bit-or= v (<< (cast (& b 0x7F) u64) shift))
(if (< b 0x80)
(return v)))
(.fail self)
(return 0))
;; A packed or repeated `int' field: negative values arrive as ten
;; byte varints and keep their meaning when truncated.
(pub fn int32 ((self (* pb))) i32
(return (cast (.varint self) i32)))
;; A length-delimited field as a reader of its own: a submessage, a
;; string, or a packed array.
(pub fn bytes ((self (* pb))) pb
(var len u64 (.varint self))
;; TODO sex#45: use (. self p) in the literals once member access
;; inside a compound literal follows the pointer.
;; where the window starts, before self moves past it
(var p (* const u8) (. self p))
(if (> len (.len self))
(do
@@ -76,21 +68,19 @@
(+= (. self p) len)
(return #(pb : p (+ p len) false)))
;; True when the rest of the window spells exactly S, e.g. a map
;; entry's key against "riverDistance".
(pub fn equals ((self (* const pb)) (s (* const char))) bool
(var n size-t (.len self))
(return (&& (== (strlen s) n) (== (memcmp (. self p) s n) 0))))
(fn advance ((self (* pb)) (n size-t)) void
(if (> n (.len self))
(.fail self)
(+= (. self p) n)))
(pub fn skip ((self (* pb)) (wire int)) void
(switch wire
(case WIRE-VARINT (.varint self) (break))
(case WIRE-BYTES (.bytes self) (break))
(case WIRE-FIXED64 (.advance self 8) (break))
(case WIRE-FIXED32 (.advance self 4) (break))
(default (.fail self)))))
(default (.fail self))))
(fn advance ((self (* pb)) (n size-t)) void
(if (> n (.len self))
(.fail self)
(+= (. self p) n)))
(fn fail ((self (* pb))) void
(= (. self err) true)
(= (. self p) (. self end))))

View File

@@ -10,16 +10,11 @@
;;; every RNG call is the mod's: it is what the layout depends on.
(include stdlib.h string.h math.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 maps)
(import lcg)
(import noise)
(pub import lcg)
(import shape)
(import json)
(import worldgen)
(import fastnoise)
(pub import fastnoise)
(pub record river-config
((min-fork-angle int) (fork-variation int) (normal-angle int)
@@ -151,11 +146,7 @@
(var dy double (- (. end y) (. start y)))
(var v double (+ (* (- (. p x) (. start x)) dx) (* (- (. p y) (. start y)) dy)))
(/= v (+ (* dx dx) (* dy dy)))
;; TODO sex#59: clamp inside the cast once a conditional operand
;; keeps its parentheses.
(if (< v 0) (= v 0))
(if (> v 1) (= v 1))
(return (cast v float)))
(return (cast (if (< v 0) 0 (if (> v 1) 1 v)) float)))
(fn distance-to-line ((p vec2) (s vec2) (e vec2)) double
(if (<= (+ (* (- (. s x) (. e x)) (- (. p x) (. e x))) (* (- (. s y) (. e y)) (- (. p y) (. e y)))) 0)
@@ -481,8 +472,7 @@
(var n int (. p config zones-in-region))
(var best-x int x)
(var best-z int z)
;; TODO sex#63: write -INFINITY once it reads as a negation.
(var best double (- INFINITY))
(var best double -INFINITY)
(var found bool false)
(for (var dx int -1) (<= dx 1) (++ dx)
(for (var dz int -1) (<= dz 1) (++ dz)

View File

@@ -17,11 +17,9 @@
(pub define CHUNK-AREA 1024)
(pub define MAX-STORIES 32)
;; TODO sex#50: write CHUNK-AREA once constants in array types are
;; kebab-converted.
(pub record chunk-maps
((terrain-height [u16 CHUNK_AREA]) (rain-height [u16 CHUNK_AREA])
(top-rock [i32 CHUNK_AREA]) (river-distance [u16 CHUNK_AREA])
((terrain-height [u16 CHUNK-AREA]) (rain-height [u16 CHUNK-AREA])
(top-rock [i32 CHUNK-AREA]) (river-distance [u16 CHUNK-AREA])
(has-river-distance bool)))
(pub record story-location ((code [char 64]) (x i32) (y i32) (z i32)))
@@ -34,9 +32,7 @@
((db (* sqlite3)) (seed i32) (size-x i32) (size-y i32) (size-z i32)
(config-keys (* * char)) (config-values (* * char)) (config-count int)
(block-codes (* * char)) (block-count int)
;; TODO sex#50: write MAX-STORIES once constants in array types are
;; kebab-converted.
(stories [story-location MAX_STORIES]) (story-count int)
(stories [story-location MAX-STORIES]) (story-count int)
;; every player's stored pick readings
(pick-readings (* pick-reading)) (pick-reading-count int)))
@@ -83,21 +79,17 @@
(switch type
(case 1 (snprintf value 64 "%d" (cast (read-le (& r) 4) i32)) (break))
(case 2 (snprintf value 64 "%lld" (cast (read-le (& r) 8) i64)) (break))
;; TODO sex#51: drop the (do ...) around case bodies once each
;; case gets its own scope.
(case 3
(do
(var bits u64 (read-le (& r) 8))
(var d double)
(memcpy (& d) (& bits) 8)
(snprintf value 64 "%.17g" d))
(snprintf value 64 "%.17g" d)
(break))
(case 4
(do
(var bits u32 (cast (read-le (& r) 4) u32))
(var f float)
(memcpy (& f) (& bits) 4)
(snprintf value 64 "%.9g" (cast f double)))
(snprintf value 64 "%.9g" (cast f double))
(break))
(case 5
(add-config s key (read-dotnet-string (& r)))
@@ -132,9 +124,7 @@
(if (> id max-id) (= max-id id)))
(.skip (& entry) wire))))
(= (. s block-count) (+ max-id 1))
;; TODO sex#52: write (sizeof (* char)) once sizeof takes a pointer
;; type.
(= (. s block-codes) (calloc (+ max-id 1) (sizeof [(. s block-codes) 0])))
(= (. s block-codes) (calloc (+ max-id 1) (sizeof (* char))))
(while (> (.field (& r) (& wire)) 0)
(var entry pb (.bytes (& r)))
(var id int -1)

View File

@@ -11,17 +11,9 @@
(pub include pthread.h)
(include stdio.h stdlib.h string.h math.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 maps)
(import lcg)
(import noise)
(import shape)
(import json)
(import worldgen)
(import fastnoise)
(import rivers)
(pub import worldgen)
(pub import rivers)
(import terra)
(import strata)
(import ores)
@@ -31,12 +23,10 @@
;;; mask the least reading category.
(pub define SEARCH-MINERAL 4)
(define SEARCH-STEP 16)
(define MAX-SEARCH-THREADS 16)
(pub define MAX-SEARCH-THREADS 16)
;; TODO sex#60: size threads with MAX-SEARCH-THREADS once arrays of a
;; header type take a symbolic size.
(pub record search
((lock pthread-mutex-t) (threads [pthread-t 16]) (thread-count int) (running bool)
((lock pthread-mutex-t) (threads [pthread-t MAX-SEARCH-THREADS]) (thread-count int) (running bool)
(cancel bool)
;; the question
(settings world-settings) (rivers river-config) (assets-dir [char 1024])
@@ -49,11 +39,9 @@
(record region-order ((rx int) (rz int) (distance double)))
(fn by-distance ((pa (* const void)) (pb (* const void))) int
;; TODO sex#61: cast inside the field access once a cast as the object
;; of . keeps its parentheses.
(var a (* const region-order) pa)
(var b (* const region-order) pb)
(return (- (> (. a distance) (. b distance)) (< (. a distance) (. b distance)))))
(var a _ (. (cast pa (* const region-order)) distance))
(var b _ (. (cast pb (* const region-order)) distance))
(return (- (> a b) (< a b))))
;;; A thread's generators, and the searched mineral's ore map of the
;;; region it last scanned in.

View File

@@ -3,17 +3,10 @@
;;; has the seed as typed and the world config as strings.
(include stdio.h stdlib.h string.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 json)
(import maps)
(import lcg)
(import noise)
(import shape)
(import save)
(import worldgen)
(pub import worldgen)
;;; Settings plus the seed as the user would type it.
(pub record world-source ((settings world-settings) (seed-text [char 128])))

View File

@@ -100,9 +100,7 @@
;;; NaturalShape -----------------------------------------------------
;; TODO sex#57: make this private again once a pub record can use a
;; private type.
(pub record shape-cell ((x int) (z int) (open [bool 4]) (live bool)))
(record shape-cell ((x int) (z int) (open [bool 4]) (live bool)))
(pub record natural-shape
((rng (* lcg))
@@ -240,9 +238,7 @@
(for (var i int 0) (< i (. in cap)) (++ i)
(if (== [(. in state) i] 1)
(.put set (+ x0 (cast (>> [(. in keys) i] 32) i32))
;; TODO sex#47: write the mask in hex once there are hex
;; literals.
(+ z0 (cast (& [(. in keys) i] 4294967295) i32)) 1))))
(+ z0 (cast (& [(. in keys) i] 0xFFFFFFFF) i32)) 1))))
(pub fn release ((self (* natural-shape))) void
(free (. self slots))

View File

@@ -10,18 +10,11 @@
;;; if it were rock, as the game's prospecting assumes too.
(include stdio.h stdlib.h string.h math.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 maps)
(import lcg)
(pub import maps)
(import noise)
(import shape)
(import json)
(import worldgen)
(import fastnoise)
(import rivers)
(import terra)
(pub import terra)
(pub define MAX-ROCKS 32)
;;; A column entry below any rock: the mantle at y = 0.

View File

@@ -10,27 +10,20 @@
;;; certainly air above.
(include stdio.h stdlib.h string.h math.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 maps)
(import lcg)
(pub import maps)
(import noise)
(import shape)
(import json)
(import worldgen)
(import fastnoise)
(import rivers)
(pub import worldgen)
(pub import fastnoise)
(pub import rivers)
;;; NewSimplexNoiseLayer --------------------------------------------
;; TODO sex#47: write these in hex, as the C# does, once there are hex
;; literals.
(define PRIME-X 5910200641878280303)
(define PRIME-Y 6452764530575939509)
(define PRIME-Z 6614699811220273867)
(define HASH-MULTIPLIER 6026932503003350773)
(define SEED-FLIP -5968755714895566377)
(define PRIME-X 0x5205402B9270C86F)
(define PRIME-Y 0x598CD327003817B5)
(define PRIME-Z 0x5BCC226E9FA0BACB)
(define HASH-MULTIPLIER 0x53A3F72DEEC546F5)
(define SEED-FLIP -0x52D547B2E96ED629)
(define NORMALIZER-3D 0.2781926117527186)
(define MAX-Y-SLOPE 5.0)
@@ -145,14 +138,9 @@
(var xf1 float (- (* (cast (- -2 (* xn 4)) float) x1) one))
(var yf1 float (- (* (cast (- -2 (* yn 4)) float) y1) one))
(var zf1 float (- (* (cast (- -2 (* zn 4)) float) z1) one))
;; TODO sex#62: cast (c-bit-or xn 1) to float directly once a cast of
;; a bitwise expression keeps its parentheses.
(var xsi int (c-bit-or xn 1))
(var ysi int (c-bit-or yn 1))
(var zsi int (c-bit-or zn 1))
(var xs float xsi)
(var ys float ysi)
(var zs float zsi)
(var xs float (cast (c-bit-or xn 1) float))
(var ys float (cast (c-bit-or yn 1) float))
(var zs float (cast (c-bit-or zn 1) float))
(var skip5 bool false)
(var a2 float (+ xf0 a0))
@@ -285,8 +273,7 @@
;;; *tanh* curve. GenTerra turns its threshold into a noise value with
;;; this one, though the column bounds use the new noise's own curve.
(fn tanh-curve-inverse ((v double)) double
;; TODO sex#63: write -INFINITY once it reads as a negation.
(if (<= v 0.0) (return (- INFINITY)))
(if (<= v 0.0) (return -INFINITY))
(if (>= v 1.0) (return INFINITY))
(return (* 0.5 (log (/ v (- 1.0 v))))))
@@ -680,11 +667,9 @@
(var oct-a [double 64])
(var oct-t [double 64])
(for (var k int 0) (< k 4) (++ k)
;; TODO sex#59: put the (if ...)s in the sums below once a
;; conditional operand keeps its parentheses.
(var ox float (if (& k 1) pixel 0))
(var oz float (if (& k 2) pixel 0))
(landform-weights/at (& (. t weights)) (+ base-x ox) (+ base-z oz) w n)
(landform-weights/at (& (. t weights))
(+ base-x (if (& k 1) pixel 0)) (+ base-z (if (& k 2) pixel 0))
w n)
(interpolated-octaves t w (+ oct-a (* k 16)) (+ oct-t (* k 16))))
(var delta float (/ (cast 1 float) (cast 32 float)))

View File

@@ -13,17 +13,11 @@
(pub include pthread.h SDL3/SDL.h)
(include stdio.h stdlib.h string.h math.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 json)
(import maps)
(import lcg)
(import noise)
(import shape)
(import worldgen)
(pub import shape)
(pub import worldgen)
(import fastnoise)
(import rivers)
(pub import rivers)
(import terra)
(import strata)
(import ores)
@@ -50,7 +44,7 @@
;;; River tiles are sampled at a resolution the viewer picks by zoom,
;;; in the key's `stratum': a column costs too much to sample them all
;;; for an overview.
(define MAX-WORKERS 32)
(pub define MAX-WORKERS 32)
(define TILE-FREE 0)
(define TILE-QUEUED 1)
@@ -72,54 +66,44 @@
(pub record tiles
((lock pthread-mutex-t) (wake pthread-cond-t)
;; TODO sex#50: write MAX-TILES once constants in array types are
;; kebab-converted.
(slots [tile MAX_TILES]) (index cell-map)
(queue [int MAX_TILES]) (queue-len int) (queue-pos int)
(free-slots [int MAX_TILES]) (free-count int)
(slots [tile MAX-TILES]) (index cell-map)
(queue [int MAX-TILES]) (queue-len int) (queue-pos int)
(free-slots [int MAX-TILES]) (free-count int)
(settings world-settings) (rivers river-config) (assets-dir [char 1024]) (generation int)
;; TODO sex#60: size with MAX-WORKERS once arrays of a header type
;; take a symbolic size.
(workers [pthread-t 32]) (worker-count int) (quit bool) (frame u64) (waiting int)
(workers [pthread-t MAX-WORKERS]) (worker-count int) (quit bool) (frame u64) (waiting int)
(palette [u32 256])))
(fn key-hash ((k tile-key)) u64
;; one-to-one: region x and z 16 bits each, map 4, stratum or
;; resolution 10, param 18
;; TODO sex#47: write the field masks in hex once there are hex
;; literals.
(return (c-bit-or (cast (& (. k rx) 65535) u64)
(<< (cast (& (. k rz) 65535) u64) 16)
(<< (cast (& (. k map) 15) u64) 32)
(<< (cast (& (. k stratum) 1023) u64) 36)
(<< (cast (& (. k param) 262143) u64) 46))))
(return (c-bit-or (cast (& (. k rx) 0xFFFF) u64)
(<< (cast (& (. k rz) 0xFFFF) u64) 16)
(<< (cast (& (. k map) 0xF) u64) 32)
(<< (cast (& (. k stratum) 0x3FF) u64) 36)
(<< (cast (& (. k param) 0x3FFFF) u64) 46))))
;;; The tile index is a cell-map keyed by (x, z): the hash is split
;;; over the two halves.
;; TODO sex#47: write the low-half masks below in hex once there are hex
;; literals.
(fn index-get ((t (* tiles)) (k tile-key)) int
(var h u64 (key-hash k))
(return (.get (& (. t index)) (cast (>> h 32) int) (cast (& h 4294967295) int))))
(return (.get (& (. t index)) (cast (>> h 32) int) (cast (& h 0xFFFFFFFF) int))))
(fn index-put ((t (* tiles)) (k tile-key) (slot int)) void
(var h u64 (key-hash k))
(.put (& (. t index)) (cast (>> h 32) int) (cast (& h 4294967295) int) slot))
(.put (& (. t index)) (cast (>> h 32) int) (cast (& h 0xFFFFFFFF) int) slot))
(fn index-remove ((t (* tiles)) (k tile-key)) void
(var h u64 (key-hash k))
(.remove (& (. t index)) (cast (>> h 32) int) (cast (& h 4294967295) int)))
(.remove (& (. t index)) (cast (>> h 32) int) (cast (& h 0xFFFFFFFF) int)))
;;; Colouring -----------------------------------------------------
;;; A distinct, stable colour per landform index.
(fn landform-colour ((i u32)) u32
;; TODO sex#47: write the multiplier (0x9E3779B1) and masks in hex
;; once there are hex literals.
(var h u32 (* (+ i 1) (cast 2654435761 u32)))
(return (c-bit-or (<< (+ 48 (& (>> h 24) 191)) 16)
(<< (+ 48 (& (>> h 16) 191)) 8)
(+ 48 (& (>> h 8) 191)))))
(var h u32 (* (+ i 1) (cast 0x9E3779B1 u32)))
(return (c-bit-or (<< (+ 48 (& (>> h 24) 0xBF)) 16)
(<< (+ 48 (& (>> h 16) 0xBF)) 8)
(+ 48 (& (>> h 8) 0xBF)))))
(fn lerp-colour ((a u32) (b u32) (t int)) u32
(var r u32 0)
@@ -130,20 +114,18 @@
(return r))
(fn colour-of ((g (* const worldgen)) (map int) (v int)) u32
;; TODO sex#47: write the colours and masks in hex once there are hex
;; literals.
(switch map
;; landforms.json gives most landforms the same green, so these
;; are coloured by index instead
(case MAP-LANDFORM (return (landform-colour (cast v u32))))
(case MAP-PROVINCE
(return (if (&& (>= v 0) (< v (. g provinces count))) [(. g provinces colours) v] 0)))
(case MAP-CLIMATE (return (& (cast v u32) 16777215)))
(case MAP-OCEAN (return (lerp-colour 6326847 1919884 v)))
(case MAP-CLIMATE (return (& (cast v u32) 0xFFFFFF)))
(case MAP-OCEAN (return (lerp-colour 0x608A3F 0x1D4B8C v)))
(default
(if (< v 0) (= v 0))
(if (> v 255) (= v 255))
(return (* (cast v u32) 65793)))))
(return (* (cast v u32) 0x010101)))))
;;; The tile's inner pixels, coloured, as ARGB.
(fn render ((g (* worldgen)) (k tile-key) (size (* int))) (* u32)
@@ -154,9 +136,7 @@
(for (var z int 0) (< z inner) (++ z)
(for (var x int 0) (< x inner) (++ x)
(var v int [(. m data) (+ (* (+ z (. m pad-tl)) (. m size)) x (. m pad-tl))])
;; TODO sex#47: write the opaque alpha in hex once there are hex
;; literals.
(= [px (+ (* z inner) x)] (c-bit-or 4278190080 (colour-of g (. k map) v)))))
(= [px (+ (* z inner) x)] (c-bit-or 0xFF000000 (colour-of g (. k map) v)))))
(free (. m data))
(= (* size) inner)
(return px))
@@ -197,12 +177,11 @@
(= (. c segments) (realloc (. c segments) (* (. c segment-cap) (sizeof int))))))
(return p))
;; TODO sex#47: write the colours in hex once there are hex literals.
(fn river-colour ((distance int) (valley int) (ocean bool)) u32
(if ocean (return 1784427))
(if (== distance 0) (return 3043288))
(if (< distance valley) (return (lerp-colour 8369128 5143098 (/ (* distance 255) valley))))
(return 4022062))
(if ocean (return 0x1B3A6B))
(if (== distance 0) (return 0x2E6FD8))
(if (< distance valley) (return (lerp-colour 0x7FB3E8 0x4E7A3A (/ (* distance 255) valley))))
(return 0x3D5F2E))
(fn render-rivers ((g (* worldgen)) (cache (* river-cache)) (config (* const river-config))
(k tile-key) (size (* int))) (* u32)
@@ -230,32 +209,26 @@
(var ox int (+ (* cx (/ 32 step)) dx))
(var oz int (+ (* cz (/ 32 step)) dz))
(= [px (+ (* oz res) ox)]
;; TODO sex#47: write the opaque alpha in hex once there
;; are hex literals.
(c-bit-or 4278190080 (river-colour d valley (river-plate/ocean-at p x z))))))))
(c-bit-or 0xFF000000 (river-colour d valley (river-plate/ocean-at p x z))))))))
(= (* size) res)
(return px))
;;; Terrain height, shaded: water by depth under sea level, land by
;;; height, lit from the north-west.
;; TODO sex#47: write the colours in hex once there are hex literals.
(fn height-colour ((h int) (west int) (north int) (sea int) (size-y int)) u32
;; TODO sex#59: clamp the depth inline once a conditional operand
;; keeps its parentheses.
(var depth int (- sea h))
(if (> depth 40) (= depth 40))
(if (< h sea)
(return (lerp-colour 6400472 1186659 (/ (* depth 255) 40))))
(return (lerp-colour 0x61A9D8 0x121B63 (/ (* (if (> depth 40) 40 depth) 255) 40))))
(var t int (/ (* (- h sea) 255) (- size-y sea)))
(var base u32 (if (< t 32) (lerp-colour 5605944 8299066 (* t 8))
(if (< t 96) (lerp-colour 8299066 9922899 (/ (* (- t 32) 255) 64))
(lerp-colour 9922899 15263976 (/ (* (- t 96) 255) 159)))))
(var base u32 (if (< t 32) (lerp-colour 0x558A38 0x7EA23A (* t 8))
(if (< t 96) (lerp-colour 0x7EA23A 0x976953 (/ (* (- t 32) 255) 64))
(lerp-colour 0x976953 0xE8E8E8 (/ (* (- t 96) 255) 159)))))
(var slope int (+ (- h west) (- h north)))
(var light int (+ 128 (* slope 24)))
(if (< light 0) (= light 0))
(if (> light 255) (= light 255))
(if (> light 128)
(return (lerp-colour base 16777215 (/ (* (- light 128) 160) 127))))
(return (lerp-colour base 0xFFFFFF (/ (* (- light 128) 160) 127))))
(return (lerp-colour 0 base (+ 96 (/ (* light 159) 128)))))
(fn render-heights ((tr (* terra)) (k tile-key) (size (* int))) (* u32)
@@ -282,9 +255,7 @@
(var west int (if (> x 0) [hs (+ (* z res) x -1)] h))
(var north int (if (> z 0) [hs (+ (* (- z 1) res) x)] h))
(= [px (+ (* z res) x)]
;; TODO sex#47: write the opaque alpha in hex once there are
;; hex literals.
(c-bit-or 4278190080 (height-colour h west north (. tr sea-level) (. tr size-y))))))
(c-bit-or 0xFF000000 (height-colour h west north (. tr sea-level) (. tr size-y))))))
(free hs)
(= (* size) res)
(return px))

View File

@@ -3,18 +3,11 @@
;;; map matches to the pixel, padding included.
(include stdio.h stdlib.h string.h math.h time.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 maps)
(import lcg)
(import noise)
(import json)
(import shape)
(import save)
(import worldgen)
(import fastnoise)
(import rivers)
(pub import rivers)
(import terra)
(import strata)
(import ores)
@@ -127,10 +120,8 @@
(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)))
;; TODO sex#50: write MAX-MINERALS once constants in array types are
;; kebab-converted.
(var codes [* char MAX_MINERALS])
(var want [int-map MAX_MINERALS])
(var codes [* char MAX-MINERALS])
(var want [int-map MAX-MINERALS])
(var t tally)
(memset (& t) 0 (sizeof t))
(var unknown int 0)
@@ -344,19 +335,9 @@
(if (== d 0) (++ matched))
(if (!= d 0)
(do
;; TODO sex#59: sum two (if ...)s once a conditional
;; operand keeps its parentheses.
(var cls int 0)
(if (< d 0) (+= cls 2))
(if (>= got (. t sea-level)) (+= cls 1))
(++ [by-class cls])))
(++ [by-class (+ (if (< d 0) 2 0) (if (>= got (. t sea-level)) 1 0))])))
;; -4..+4, the ends counting everything beyond
;; TODO sex#59: clamp inline once a conditional operand keeps its
;; parentheses.
(var clamped int d)
(if (< clamped -4) (= clamped -4))
(if (> clamped 4) (= clamped 4))
(var bucket int (+ clamped 4))
(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))

View File

@@ -12,17 +12,10 @@
;;; quits, which is how the viewer is checked without looking at it.
(include stdio.h stdlib.h string.h math.h unistd.h "nk.h" "stb_image_write.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 json)
(import maps)
(import lcg)
(import noise)
(import shape)
(import worldgen)
(import fastnoise)
(import rivers)
(import terra)
(import strata)
@@ -66,21 +59,17 @@
#("rock-granite" "rock-andesite" "rock-basalt" "rock-peridotite" "rock-kimberlite"
"rock-claystone" "rock-sandstone" "rock-shale" "rock-conglomerate" "rock-limestone"
"rock-chert" "rock-chalk" "rock-bauxite" "rock-slate" "rock-phyllite"))
;; TODO sex#47: write the colours in hex once there are hex literals.
(var rock-colour-values [u32 15]
;; Sex has no hex literals: B49A8C 6E6A66 3A3A3D 4F6B3A 5A6E7A C08A5A D8C38A
;; 5E6470 9A7B5C C9C7B8 8A5A48 F0F0E8 B5532E 4A5568 7D8A7A
#(11836044 7236198 3815997 5204794 5926522 12618330 14205834 6186096 10124124 13223864 9067080 15790312 11883310 4871528 8227450))
#(0xB49A8C 0x6E6A66 0x3A3A3D 0x4F6B3A 0x5A6E7A 0xC08A5A 0xD8C38A 0x5E6470
0x9A7B5C 0xC9C7B8 0x8A5A48 0xF0F0E8 0xB5532E 0x4A5568 0x7D8A7A))
(var climate-names [(* const char) 5] #("temperate" "hot" "warm" "cool" "icy"))
;;; The pick's reading categories, ores/category's numbering, and their
;;; colours on the Minerals overlay.
(var reading-names [(* const char) 8]
#("none" "miniscule" "very poor" "poor" "decent" "high" "very high" "ultra high"))
;; TODO sex#47: write the colours in hex once there are hex literals.
(var reading-colours [u32 8]
;; Sex has no hex literals: 000000 5A6E7A 3B6FB6 2BA3A3 7BC043 F3C623 F37735 D11141
#(0 5926522 3895222 2859939 8110147 15975971 15955765 13701441))
#(0x000000 0x5A6E7A 0x3B6FB6 0x2BA3A3 0x7BC043 0xF3C623 0xF37735 0xD11141))
;;; Find nearest's least reading, unless set: decent.
(define DEFAULT-MIN-READING 4)
@@ -98,12 +87,10 @@
;;; The strata of the column clicked last: top down, as (rock, top y,
;;; bottom y) runs; and the pick's readings there, highest first.
;; TODO sex#50: write MAX-MINERALS once constants in array types are
;; kebab-converted.
(record probe ((valid bool) (x int) (z int) (height int) (count int)
(rocks [int 64]) (tops [int 64]) (bottoms [int 64])
(reading-count int) (reading-minerals [int MAX_MINERALS])
(readings [mineral-reading MAX_MINERALS])))
(reading-count int) (reading-minerals [int MAX-MINERALS])
(readings [mineral-reading MAX-MINERALS])))
(record app
((window (* SDL-Window)) (renderer (* SDL-Renderer)) (ctx (* struct nk-context))
@@ -123,9 +110,7 @@
;; minerals: the one shown, the readable ones for the panel's list,
;; its share of the view per category, and what Find nearest wants
(minerals (* ores)) (mineral int) (mineral-code (* const char))
;; TODO sex#50: write MAX-MINERALS once constants in array types
;; are kebab-converted.
(mineral-items [* const char MAX_MINERALS]) (mineral-ids [int MAX_MINERALS])
(mineral-items [* const char MAX-MINERALS]) (mineral-ids [int MAX-MINERALS])
(mineral-item-count int)
(mineral-coverage [i64 256]) (mineral-coverage-total i64)
(min-reading int) (find-mineral-requested bool) (searching-mineral bool)
@@ -234,32 +219,28 @@
(for (var k int 0) (< k 15) (++ k)
(if (== (strcmp code [rock-colour-codes k]) 0)
(return [rock-colour-values k])))
;; TODO sex#47: write the multiplier and mask in hex once there are
;; hex literals.
(var hash u32 (* (cast (+ i 41) u32) (cast 2654435761 u32)))
(return (& hash 16777215)))
(var hash u32 (* (cast (+ i 41) u32) (cast 0x9E3779B1 u32)))
(return (& hash 0xFFFFFF)))
;;; The rock overlay's palette: every rock in its colour, or only the
;;; highlighted ones, the rest transparent.
(fn update-palette ((a (* app))) void
(var p [u32 256])
(for (var i int 0) (< i 256) (++ i)
;; TODO sex#47: write the palette colours and alpha in hex once
;; there are hex literals.
(= [p i] 4280295456))
(= [p i] 0xFF202020))
;; the base layer shows through water, air and the rocks not looked for
(= [p VALUE-WATER] 0)
(= [p VALUE-AIR] 0)
(= [p ROCK-MANTLE] 0)
(for (var i int 0) (< i (. a rocks rock-count)) (++ i)
(var c u32 (rock-colour a i))
(= [p i] (c-bit-or 4278190080 c))
(= [p i] (c-bit-or 0xFF000000 c))
(if (&& (!= (. a highlight) 0) (== (& (. a highlight) (<< (cast 1 u32) i)) 0))
(= [p i] 0)))
;; the base shows through where the pick reads nothing
(= [p MINERAL-VALUE-BASE] 0)
(for (var c int 1) (< c READING-CATEGORIES) (++ c)
(= [p (+ MINERAL-VALUE-BASE c)] (c-bit-or 4278190080 [reading-colours c])))
(= [p (+ MINERAL-VALUE-BASE c)] (c-bit-or 0xFF000000 [reading-colours c])))
(.set-palette (. a tiles) p))
;;; The highlighted rock when it is the only one, else -1.
@@ -920,11 +901,9 @@
(if (cursor-block a (& bx) (& bz))
(do
(var value int (rock-query-value a))
;; TODO sex#45: use (. a rock-mode) in the literal once member
;; access inside a compound literal follows the pointer.
(var mode int (. a rock-mode))
(var mineral int (if (!= (& (. a overlays) MINERAL-OVERLAY-BIT) 0) (. a mineral) -1))
(hover-worker/ask (. a hover-worker) #(hover-query : bx bz mode value mineral))))
(hover-worker/ask (. a hover-worker)
#(hover-query : bx bz (. a rock-mode) value mineral))))
(= (. a hover) (hover-worker/result (. a hover-worker))))
(fn rock-name ((a (* app)) (rock int)) (* const char)

View File

@@ -2,23 +2,12 @@
;;; against a saved world and dumps what either side produced.
(include stdio.h string.h)
;; An import brings in a module's own public forms but not the types
;; they mention, so dependencies come first.
;; 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 json)
(import maps)
(import lcg)
(import noise)
(import shape)
(import save)
(import dump)
(import worldgen)
(import fastnoise)
(import rivers)
(import terra)
(import strata)
(import settings)
(import ores)

View File

@@ -4,11 +4,11 @@
;;; the worldgen assets and the region's coordinates.
(include stdio.h stdlib.h string.h math.h)
(import maps)
(import lcg)
(pub import maps)
(pub import lcg)
(import noise)
(import json)
(import shape)
(pub import json)
(pub import shape)
(pub define MAX-PROVINCES 32)
@@ -80,11 +80,9 @@
;;; NoiseGeoProvince plus MapLayerGeoProvince: a weighted random
;;; province per 64x64 cell of province-map pixels, sampled through a
;;; wobble so the cells don't show.
;; TODO sex#50: write MAX-PROVINCES once constants in array types are
;; kebab-converted.
(pub record province-layer
((rng lcg) (weights [int MAX_PROVINCES]) (count int) (weight-sum int)
(codes [* char MAX_PROVINCES]) (colours [u32 MAX_PROVINCES])
((rng lcg) (weights [int MAX-PROVINCES]) (count int) (weight-sum int)
(codes [* char MAX-PROVINCES]) (colours [u32 MAX-PROVINCES])
(wobble-x (* simplex-noise)) (wobble-z (* simplex-noise))))
(define GEOPROV-MAP-SCALE 64)
@@ -167,10 +165,7 @@
(= [map (+ start (* i stride))] (cast [tmp i] i32))))
(fn box-blur ((map (* i32)) (w int) (h int) (range int)) void
;; TODO sex#59: inline the longer side once a conditional operand
;; keeps its parentheses.
(var longest int (if (> w h) w h))
(var tmp (* u32) (malloc (* longest (sizeof u32))))
(var tmp (* u32) (malloc (* (if (> w h) w h) (sizeof u32))))
(for (var y int 0) (< y h) (++ y)
(blur-line map (* y w) 1 w range tmp))
(for (var x int 0) (< x w) (++ x)
@@ -390,9 +385,7 @@
(= (. l octave-threshold-count) 11)))
(= (. l first-mutation) -1))
;; TODO sex#50: write MAX-LANDFORMS once constants in array types are
;; kebab-converted.
(pub record landform-table ((all [landform MAX_LANDFORMS]) (count int) (variant-count int)))
(pub record landform-table ((all [landform MAX-LANDFORMS]) (count int) (variant-count int)))
(pub fn terrain-octave-count ((size-y int)) int
(return (+ 9 (/ (- size-y 256) 128))))
@@ -452,11 +445,9 @@
(return (|| (!= (- rain clamped-rain) 0) (!= (- t clamped-temp) 0))))
;;; MapLayerLandforms over NoiseLandforms.
;; TODO sex#50: write MAX-LANDFORMS once constants in array types are
;; kebab-converted.
(pub record landform-layer
((rng lcg) (table (* const landform-table)) (climate climate-noise) (scale float)
(weights [double MAX_LANDFORMS])
(weights [double MAX-LANDFORMS])
(wobble-x (* simplex-noise)) (wobble-z (* simplex-noise))))
(define LANDFORM-MAP-SCALE 16)
@@ -724,11 +715,9 @@
;;; MapLayerCustomPerlin per stratum of rockstrata.json: how thick
;;; that stratum may be, in blocks, per 16 blocks of region.
;; TODO sex#50: write MAX-STRATA once constants in array types are
;; kebab-converted.
(pub record strata-layer
((count int) (noises [* simplex-sum MAX_STRATA]) (thresholds [* double MAX_STRATA])
(codes [* char MAX_STRATA])))
((count int) (noises [* simplex-sum MAX-STRATA]) (thresholds [* double MAX-STRATA])
(codes [* char MAX-STRATA])))
(fn json-doubles ((o (* cJSON)) (key (* const char)) (out (* double)) (max int)) int
(var arr (* cJSON) (cJSON-GetObjectItem o key))
@@ -744,10 +733,8 @@
(memset (& l) 0 (sizeof l))
(var variants (* cJSON) (cJSON-GetObjectItem rockstrata "variants"))
(for (var v (* cJSON) (. variants child)) (&& v (< (. l count) MAX-STRATA)) (= v (. v next))
;; TODO sex#50: write MAX-OCTAVES once constants in array types
;; are kebab-converted.
(var amplitudes [double MAX_OCTAVES])
(var frequencies [double MAX_OCTAVES])
(var amplitudes [double MAX-OCTAVES])
(var frequencies [double MAX-OCTAVES])
(var th (* double) (calloc MAX-OCTAVES (sizeof double)))
(var n int (json-doubles v "amplitudes" amplitudes MAX-OCTAVES))
(json-doubles v "frequencies" frequencies MAX-OCTAVES)
@@ -827,8 +814,7 @@
(= base-z (. parent z))
(= dir-x (. parent dir-x)))
(continue))
;; TODO sex#59: put the (if ...) in the product below once a
;; conditional operand keeps its parentheses.
;; drawn here, before the distances, as GenStoryStructures draws it
(var dir-z int (if (> (.next-float (& rng)) 0.5) -1 1))
(var dist-x int (+ min-x (.next-int (& rng) (- (+ max-x 1) min-x))))
(var dist-z int (+ min-z (.next-int (& rng) (- (+ max-z 1) min-z))))
@@ -981,7 +967,7 @@
((settings world-settings) (provinces province-layer) (landform-table landform-table)
(landforms landform-layer) (oceans ocean-layer) (upheaval upheaval-layer)
(climate climate-layer) (strata strata-layer)
(stories [story MAX_STORY_LOCATIONS]) (story-count int)
(stories [story MAX-STORY-LOCATIONS]) (story-count int)
(story-wobble-x (* simplex-noise)) (story-wobble-z (* simplex-noise))))
(pub fn worldgen/make ((settings world-settings) (assets-dir (* const char))) (* worldgen)