;;; NaturalShape: grows a blob of cells outward from a square, at ;;; random, to shape the land forced around spawn and story locations. ;;; ;;; The growth picks its next cell with outline.ElementAt(n) on a .NET ;;; Dictionary, so the result depends on that dictionary's enumeration ;;; order. That order is slot order: new keys take the most recently ;;; freed slot first, otherwise the next unused one. The outline here ;;; keeps exactly those slots. (include stdlib.h string.h) (import lcg) ;;; An (x, z) → int hash map, open addressing with tombstones. (pub record cell-map ((keys (* u64)) (vals (* int)) (state (* u8)) (cap int) (live int) (filled int))) (fn cell-key ((x int) (z int)) u64 (return (c-bit-or (<< (cast (cast x u32) u64) 32) (cast (cast z u32) u64)))) (fn mix ((k u64)) u64 (^= k (>> k 33)) (*= k 6364136223846793005) (^= k (>> k 33)) (return k)) (ops cell-map (pub fn make () cell-map (var m cell-map) (= (. m cap) 64) (= (. m keys) (calloc (. m cap) (sizeof u64))) (= (. m vals) (calloc (. m cap) (sizeof int))) (= (. m state) (calloc (. m cap) 1)) (= (. m live) 0) (= (. m filled) 0) (return m)) (pub fn release ((self (* cell-map))) void (free (. self keys)) (free (. self vals)) (free (. self state))) ;; The slot holding KEY, or the one it would go in. (fn find ((self (* const cell-map)) (key u64)) int (var mask int (- (. self cap) 1)) (var i int (cast (& (mix key) (cast mask u64)) int)) (var tomb int -1) (while (!= [(. self state) i] 0) (if (&& (== [(. self state) i] 1) (== [(. self keys) i] key)) (return i)) (if (&& (== [(. self state) i] 2) (< tomb 0)) (= tomb i)) (= i (& (+ i 1) mask))) (return (if (>= tomb 0) tomb i))) (pub fn get ((self (* const cell-map)) (x int) (z int)) int (var i int (.find self (cell-key x z))) (return (if (== [(. self state) i] 1) [(. self vals) i] -1))) (pub fn has ((self (* const cell-map)) (x int) (z int)) bool (return (>= (.get self x z) 0))) (fn grow ((self (* cell-map))) void (var old cell-map (* self)) (= (. self cap) (* (. old cap) 2)) (= (. self keys) (calloc (. self cap) (sizeof u64))) (= (. self vals) (calloc (. self cap) (sizeof int))) (= (. self state) (calloc (. self cap) 1)) (= (. self live) 0) (= (. self filled) 0) (for (var i int 0) (< i (. old cap)) (++ i) (if (== [(. old state) i] 1) (do (var j int (.find self [(. old keys) i])) (= [(. self keys) j] [(. old keys) i]) (= [(. self vals) j] [(. old vals) i]) (= [(. self state) j] 1) (++ (. self live)) (++ (. self filled))))) (.release (& old))) ;; Sets X, Z to VALUE (a non-negative int). (pub fn put ((self (* cell-map)) (x int) (z int) (value int)) void (if (>= (* 2 (+ (. self filled) 1)) (. self cap)) (.grow self)) (var key u64 (cell-key x z)) (var i int (.find self key)) (if (!= [(. self state) i] 1) (do (if (== [(. self state) i] 0) (++ (. self filled))) (++ (. self live)))) (= [(. self keys) i] key) (= [(. self vals) i] value) (= [(. self state) i] 1)) (pub fn remove ((self (* cell-map)) (x int) (z int)) void (var i int (.find self (cell-key x z))) (if (== [(. self state) i] 1) (do (= [(. self state) i] 2) (-- (. self live)))))) ;;; NaturalShape ----------------------------------------------------- (record shape-cell ((x int) (z int) (open [bool 4]) (live bool))) (pub record natural-shape ((rng (* lcg)) (slots (* shape-cell)) (slot-cap int) (slot-count int) (free (* int)) (free-count int) (outline cell-map) (inside cell-map))) (var offset-x [int 4] #(0 1 0 -1)) (var offset-z [int 4] #(-1 0 1 0)) (fn any-open ((open (* const bool))) bool (return (|| [open 0] [open 1] [open 2] [open 3]))) (ops natural-shape (fn open-sides ((self (* const natural-shape)) (x int) (z int) (out (* bool))) void (for (var i int 0) (< i 4) (++ i) (var nx int (+ x [offset-x i])) (var nz int (+ z [offset-z i])) (= [out i] (&& (! (.has (& (. self outline)) nx nz)) (! (.has (& (. self inside)) nx nz)))))) ;; Dictionary.TryAdd: the last freed slot, else a new one at the end. (fn outline-add ((self (* natural-shape)) (x int) (z int) (open (* const bool))) void (if (.has (& (. self outline)) x z) (return)) (var slot int 0) (if (> (. self free-count) 0) (do (-- (. self free-count)) (= slot [(. self free) (. self free-count)])) (do (if (== (. self slot-count) (. self slot-cap)) (do (= (. self slot-cap) (* 2 (. self slot-cap))) (= (. self slots) (realloc (. self slots) (* (. self slot-cap) (sizeof shape-cell)))) (= (. self free) (realloc (. self free) (* (. self slot-cap) (sizeof int)))))) (= slot (. self slot-count)) (++ (. self slot-count)))) (var c (* shape-cell) (& [(. self slots) slot])) (= (. c x) x) (= (. c z) z) (memcpy (. c open) open (* 4 (sizeof bool))) (= (. c live) true) (.put (& (. self outline)) x z slot)) (fn outline-remove ((self (* natural-shape)) (x int) (z int)) void (var slot int (.get (& (. self outline)) x z)) (if (< slot 0) (return)) (.remove (& (. self outline)) x z) (= (. [(. self slots) slot] live) false) (= [(. self free) (. self free-count)] slot) (++ (. self free-count))) ;; new NaturalShape(rand) followed by InitSquare(size-x, size-z). (pub fn square ((rng (* lcg)) (size-x int) (size-z int)) natural-shape (var s natural-shape) (memset (& s) 0 (sizeof s)) (= (. s rng) rng) (= (. s slot-cap) 64) (= (. s slots) (malloc (* (. s slot-cap) (sizeof shape-cell)))) (= (. s free) (malloc (* (. s slot-cap) (sizeof int)))) (= (. s outline) (cell-map/make)) (= (. s inside) (cell-map/make)) (for (var x int 0) (< x size-x) (++ x) (for (var z int 0) (< z size-z) (++ z) (var open [bool 4] #(false false false false)) (if (== z 0) (= [open 0] true) (== z (- size-z 1)) (= [open 2] true)) (if (== x 0) (= [open 3] true) (== x (- size-x 1)) (= [open 1] true)) (if (any-open open) (.outline-add (& s) x z open) (.put (& (. s inside)) x z 1)))) (return s)) (pub fn grow ((self (* natural-shape)) (steps int)) void (for (var step int 0) (< step steps) (++ step) (var count float (cast (. (. self outline) live) float)) (var avg float (* count (cast 0.5 float))) (var spread float (* count (cast 0.5 float))) ;; NatFloat GAUSSIAN; the three draws are sequenced as C# has them. (var r1 float (.next-float (. self rng))) (var r2 float (.next-float (. self rng))) (var r3 float (.next-float (. self rng))) (var rnd float (- (/ (+ (+ r1 r2) r3) (cast 3 float)) (cast 0.5 float))) (var next int (cast (* (cast 1 float) (+ avg (* (* rnd (cast 2 float)) spread))) int)) ;; outline.ElementAt(next) (var slot int -1) (for (var i int 0) (< i (. self slot-count)) (++ i) (if (. [(. self slots) i] live) (do (if (== next 0) (do (= slot i) (break))) (-- next)))) (if (< slot 0) (continue)) (var cell shape-cell [(. self slots) slot]) (var dir int (.next-int (. self rng) 4)) (var found bool false) (var nx int 0) (var nz int 0) (for (var j int dir) (< j (+ dir 4)) (++ j) (if (! [(. cell open) (% j 4)]) (continue)) (= nx (+ (. cell x) [offset-x (% j 4)])) (= nz (+ (. cell z) [offset-z (% j 4)])) (var open [bool 4]) (.open-sides self nx nz open) (if (any-open open) (.outline-add self nx nz open) (.put (& (. self inside)) nx nz 1)) (= found true) (break)) (if (! found) (continue)) (for (var j int 0) (< j 4) (++ j) (var px int (+ nx [offset-x j])) (var pz int (+ nz [offset-z j])) (var s int (.get (& (. self outline)) px pz)) (if (< s 0) (continue)) (.open-sides self px pz (. [(. self slots) s] open)) (if (any-open (. [(. self slots) s] open)) (continue)) (.outline-remove self px pz) (.put (& (. self inside)) px pz 1)))) ;; Every cell of the shape, outline and inside, added to SET at ;; (x0 + x, z0 + z). (pub fn add-to ((self (* const natural-shape)) (set (* cell-map)) (x0 int) (z0 int)) void (for (var i int 0) (< i (. self slot-count)) (++ i) (var c (* const shape-cell) (& [(. self slots) i])) (if (. c live) (.put set (+ x0 (. c x)) (+ z0 (. c z)) 1))) (var in (* const cell-map) (& (. self inside))) (for (var i int 0) (< i (. in cap)) (++ i) (if (== [(. in state) i] 1) (.put set (+ x0 (cast (>> [(. in keys) i] 32) i32)) (+ z0 (cast (& [(. in keys) i] 0xFFFFFFFF) i32)) 1)))) (pub fn release ((self (* natural-shape))) void (free (. self slots)) (free (. self free)) (.release (& (. self outline))) (.release (& (. self inside)))))