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

@@ -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))