From c14c1543035a7b2aa442941f2270f4315fb8e1f7 Mon Sep 17 00:00:00 2001 From: alex-eg Date: Thu, 8 Oct 2026 21:01:59 +0300 Subject: [PATCH] fix four conditional operands sexc emitted without parentheses, and mark every Sex workaround with a TODO A conditional as an operand loses its parentheses (sex#59): box-blur allocated a quarter of its buffer, deep water got the shallow colour, and verify's delta histogram wrote out of bounds into the height classes. Plan.org quoted the corrupted count. Co-Authored-By: Claude Opus 5.5 --- Makefile | 4 ++++ Plan.org | 2 +- src/cs.sex | 6 ++++++ src/dump.sex | 9 +++++++++ src/hover.sex | 3 +++ src/img.sex | 2 ++ src/json.sex | 1 + src/maps.sex | 2 ++ src/noise.sex | 8 ++++++++ src/pb.sex | 8 ++++++++ src/rivers.sex | 10 +++++++++- src/save.sex | 8 ++++++++ src/search.sex | 7 +++++++ src/settings.sex | 3 +++ src/shape.sex | 4 ++++ src/strata.sex | 3 +++ src/terra.sex | 10 ++++++++++ src/tiles.sex | 29 ++++++++++++++++++++++++++++- src/verify.sex | 12 +++++++++++- src/viewer.sex | 10 ++++++++++ src/vsmv.sex | 3 +++ src/worldgen.sex | 17 ++++++++++++++++- 22 files changed, 156 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 50df5a3..c3215eb 100644 --- a/Makefile +++ b/Makefile @@ -16,10 +16,14 @@ 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) # 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) diff --git a/Plan.org b/Plan.org index 50a9c47..efea661 100644 --- a/Plan.org +++ b/Plan.org @@ -424,7 +424,7 @@ lets carving reach y = 1. Against the 11,687,936 stored columns: - *Exact:* 11,481,361 (98.2%). -- *Stored higher, under water:* 141,978. GenBlockLayers raises dry +- *Stored higher, under water:* 126,543. GenBlockLayers raises dry seabeds after the strata. - *Stored lower, on land:* 77,222. GenCaves lowers the height where a cave opens the surface. diff --git a/src/cs.sex b/src/cs.sex index bb3da28..4d5c203 100644 --- a/src/cs.sex +++ b/src/cs.sex @@ -10,6 +10,8 @@ (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)) @@ -37,6 +39,8 @@ (= [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))) @@ -67,6 +71,8 @@ (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))) ;;; The world seed from the seed text in world settings. A number is diff --git a/src/dump.sex b/src/dump.sex index 05409f2..6ba068c 100644 --- a/src/dump.sex +++ b/src/dump.sex @@ -9,12 +9,15 @@ (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 byte-colour ((v i32)) u32 (return (grey v))) ;;; 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 (var inner int (- (. m size) (. m pad-tl) (. m pad-br))) @@ -48,6 +51,8 @@ (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-count int 0) @@ -126,7 +131,11 @@ (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))))) diff --git a/src/hover.sex b/src/hover.sex index d440de7..51c3d21 100644 --- a/src/hover.sex +++ b/src/hover.sex @@ -9,6 +9,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) diff --git a/src/img.sex b/src/img.sex index 1cdd16d..7e3aac8 100644 --- a/src/img.sex +++ b/src/img.sex @@ -27,6 +27,8 @@ ;;; 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) diff --git a/src/json.sex b/src/json.sex index 3560b85..bc8ee05 100644 --- a/src/json.sex +++ b/src/json.sex @@ -6,6 +6,7 @@ (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 (return (|| (isalpha (cast c u8)) (== c #\_) (== c #\$)))) diff --git a/src/maps.sex b/src/maps.sex index a4adc03..b952105 100644 --- a/src/maps.sex +++ b/src/maps.sex @@ -10,6 +10,8 @@ ;;; (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))) diff --git a/src/noise.sex b/src/noise.sex index f3e5389..430380d 100644 --- a/src/noise.sex +++ b/src/noise.sex @@ -9,6 +9,8 @@ ;;; 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. (define STRETCH-2D -0.211324865405187) (define SQUISH-2D 0.366025403784439) (define STRETCH-3D -0.16666666666666666) @@ -248,6 +250,8 @@ ;;; 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]) @@ -276,6 +280,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]) (for (var i int 0) (< i count) (++ i) @@ -301,6 +307,8 @@ ;;; 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]))) diff --git a/src/pb.sex b/src/pb.sex index 884ce8e..0506ffb 100644 --- a/src/pb.sex +++ b/src/pb.sex @@ -8,6 +8,8 @@ (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) @@ -15,6 +17,8 @@ (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) @@ -37,6 +41,8 @@ (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))) @@ -60,6 +66,8 @@ ;; 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. (var p (* const u8) (. self p)) (if (> len (.len self)) (do diff --git a/src/rivers.sex b/src/rivers.sex index 24aefe7..7136c90 100644 --- a/src/rivers.sex +++ b/src/rivers.sex @@ -10,6 +10,9 @@ ;;; 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) @@ -148,7 +151,11 @@ (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))) - (return (cast (if (< v 0) 0 (if (> v 1) 1 v)) float))) + ;; 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))) (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) @@ -474,6 +481,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 found bool false) (for (var dx int -1) (<= dx 1) (++ dx) diff --git a/src/save.sex b/src/save.sex index 24916b0..55b90eb 100644 --- a/src/save.sex +++ b/src/save.sex @@ -17,6 +17,8 @@ (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]) @@ -28,6 +30,8 @@ ((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))) ;;; Map regions and map chunks share one key: x in the low 27 bits, @@ -73,6 +77,8 @@ (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)) @@ -120,6 +126,8 @@ (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]))) (while (> (.field (& r) (& wire)) 0) (var entry pb (.bytes (& r))) diff --git a/src/search.sex b/src/search.sex index 84b16a4..7311115 100644 --- a/src/search.sex +++ b/src/search.sex @@ -10,6 +10,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) @@ -25,6 +28,8 @@ (define SEARCH-STEP 16) (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) (cancel bool) @@ -39,6 +44,8 @@ (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))))) diff --git a/src/settings.sex b/src/settings.sex index e2a511e..4fa199d 100644 --- a/src/settings.sex +++ b/src/settings.sex @@ -3,6 +3,9 @@ ;;; 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) diff --git a/src/shape.sex b/src/shape.sex index 5e81583..499f602 100644 --- a/src/shape.sex +++ b/src/shape.sex @@ -100,6 +100,8 @@ ;;; 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))) (pub record natural-shape @@ -238,6 +240,8 @@ (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)))) (pub fn release ((self (* natural-shape))) void diff --git a/src/strata.sex b/src/strata.sex index 566f2d1..e15ef96 100644 --- a/src/strata.sex +++ b/src/strata.sex @@ -10,6 +10,9 @@ ;;; 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) (import noise) diff --git a/src/terra.sex b/src/terra.sex index 24d5257..20d91bc 100644 --- a/src/terra.sex +++ b/src/terra.sex @@ -10,6 +10,9 @@ ;;; 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) (import noise) @@ -21,6 +24,8 @@ ;;; 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) @@ -140,6 +145,8 @@ (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)) @@ -278,6 +285,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 1.0) (return INFINITY)) (return (* 0.5 (log (/ v (- 1.0 v)))))) @@ -672,6 +680,8 @@ (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) diff --git a/src/tiles.sex b/src/tiles.sex index 1d4af07..f641c85 100644 --- a/src/tiles.sex +++ b/src/tiles.sex @@ -13,6 +13,9 @@ (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) @@ -59,16 +62,22 @@ (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) (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) (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) @@ -77,6 +86,8 @@ ;;; 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)))) @@ -93,6 +104,8 @@ ;;; 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) @@ -107,6 +120,8 @@ (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 @@ -129,6 +144,8 @@ (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))))) (free (. m data)) (= (* size) inner) @@ -170,6 +187,7 @@ (= (. 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)) @@ -202,15 +220,22 @@ (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)))))))) (= (* 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 (/ (* (if (> (- sea h) 40) 40 (- sea h)) 255) 40)))) + (return (lerp-colour 6400472 1186659 (/ (* 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)) @@ -247,6 +272,8 @@ (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)))))) (free hs) (= (* size) res) diff --git a/src/verify.sex b/src/verify.sex index b6a2c4d..71936b1 100644 --- a/src/verify.sex +++ b/src/verify.sex @@ -3,6 +3,9 @@ ;;; map matches to the pixel, padding included. (include stdio.h stdlib.h string.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) @@ -215,12 +218,19 @@ (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]))) ;; -4..+4, the ends counting everything beyond - (var bucket int (+ (if (< d -4) -4 (if (> d 4) 4 d)) 4)) + ;; 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)) (++ [by-delta bucket]) (var colour u32 (grey (/ (* got 255) (. g settings size-y)))) (if (> d 0) (= colour 16719904)) diff --git a/src/viewer.sex b/src/viewer.sex index 26f1784..611e394 100644 --- a/src/viewer.sex +++ b/src/viewer.sex @@ -11,6 +11,9 @@ ;;; 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) @@ -57,6 +60,7 @@ #("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 @@ -188,6 +192,8 @@ (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))) @@ -196,6 +202,8 @@ (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)) ;; the base layer shows through water, air and the rocks not looked for (= [p VALUE-WATER] 0) @@ -696,6 +704,8 @@ (do (var value int (. a rock-value)) (if (== (. a rock-mode) ROCK-AT-SURFACE) (= value 0)) + ;; 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)) (hover-worker/ask (. a hover-worker) #(hover-query : bx bz mode value)))) (= (. a hover) (hover-worker/result (. a hover-worker)))) diff --git a/src/vsmv.sex b/src/vsmv.sex index 798e217..f6559af 100644 --- a/src/vsmv.sex +++ b/src/vsmv.sex @@ -4,6 +4,9 @@ (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) diff --git a/src/worldgen.sex b/src/worldgen.sex index 40c2756..7a8f119 100644 --- a/src/worldgen.sex +++ b/src/worldgen.sex @@ -80,6 +80,8 @@ ;;; 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]) @@ -165,7 +167,10 @@ (= [map (+ start (* i stride))] (cast [tmp i] i32)))) (fn box-blur ((map (* i32)) (w int) (h int) (range int)) void - (var tmp (* u32) (malloc (* (if (> w h) w h) (sizeof u32)))) + ;; 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)))) (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) @@ -385,6 +390,8 @@ (= (. 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 fn terrain-octave-count ((size-y int)) int @@ -445,6 +452,8 @@ (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]) @@ -715,6 +724,8 @@ ;;; 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]))) @@ -733,6 +744,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 th (* double) (calloc MAX-OCTAVES (sizeof double))) @@ -814,6 +827,8 @@ (= 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. (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))))