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>
This commit is contained in:
2026-10-09 18:09:08 +03:00
parent 9bc1167782
commit 74200559b8
9 changed files with 65 additions and 110 deletions

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,8 +9,7 @@
(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)))
;;; The inner square of one region's map, placed at (ox, oz) and
@@ -132,11 +131,9 @@
;; 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))
(if (== d 0) (= c 0x4060BF))
(.set (& river) x y c))
(.set (& river) x y 2105376)))))
(.set (& river) x y 0x202020)))))
(write-image (& height) dir "height")
(write-image (& rock) dir "toprock")

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

@@ -416,9 +416,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

@@ -41,10 +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)
(c-bit-or= v (<< (cast (& b 0x7F) u64) shift))
(if (< b 0x80)
(return v)))
(.fail self)
(return 0))

View File

@@ -240,9 +240,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

@@ -24,13 +24,11 @@
;;; 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)

View File

@@ -82,40 +82,34 @@
(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)
@@ -126,20 +120,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)
@@ -150,9 +142,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))
@@ -193,12 +183,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)
@@ -226,32 +215,29 @@
(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 (/ (* 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)
@@ -278,9 +264,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

@@ -66,21 +66,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)
@@ -230,32 +226,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.