;;; FastNoiseLite, the part Rivers uses: 2D OpenSimplex2 with FBm, ;;; built with FNLfloat = double. Coordinates stay double, noise values ;;; are float, as in the C#. Rivers seeds these with constants, so the ;;; river noise is the same in every world. (include math.h) (define PRIME-X 501125321) (define PRIME-Y 1136930381) ;; 0x27d4eb2d (define HASH-MUL 668265261) ;;; Generated from Rivers' FastNoise.cs Gradients2D; do not edit. (var gradients [float 256] #(0.130526192220052 0.99144486137381 0.38268343236509 0.923879532511287 0.608761429008721 0.793353340291235 0.793353340291235 0.608761429008721 0.923879532511287 0.38268343236509 0.99144486137381 0.130526192220051 0.99144486137381 -0.130526192220051 0.923879532511287 -0.38268343236509 0.793353340291235 -0.60876142900872 0.608761429008721 -0.793353340291235 0.38268343236509 -0.923879532511287 0.130526192220052 -0.99144486137381 -0.130526192220052 -0.99144486137381 -0.38268343236509 -0.923879532511287 -0.608761429008721 -0.793353340291235 -0.793353340291235 -0.608761429008721 -0.923879532511287 -0.38268343236509 -0.99144486137381 -0.130526192220052 -0.99144486137381 0.130526192220051 -0.923879532511287 0.38268343236509 -0.793353340291235 0.608761429008721 -0.608761429008721 0.793353340291235 -0.38268343236509 0.923879532511287 -0.130526192220052 0.99144486137381 0.130526192220052 0.99144486137381 0.38268343236509 0.923879532511287 0.608761429008721 0.793353340291235 0.793353340291235 0.608761429008721 0.923879532511287 0.38268343236509 0.99144486137381 0.130526192220051 0.99144486137381 -0.130526192220051 0.923879532511287 -0.38268343236509 0.793353340291235 -0.60876142900872 0.608761429008721 -0.793353340291235 0.38268343236509 -0.923879532511287 0.130526192220052 -0.99144486137381 -0.130526192220052 -0.99144486137381 -0.38268343236509 -0.923879532511287 -0.608761429008721 -0.793353340291235 -0.793353340291235 -0.608761429008721 -0.923879532511287 -0.38268343236509 -0.99144486137381 -0.130526192220052 -0.99144486137381 0.130526192220051 -0.923879532511287 0.38268343236509 -0.793353340291235 0.608761429008721 -0.608761429008721 0.793353340291235 -0.38268343236509 0.923879532511287 -0.130526192220052 0.99144486137381 0.130526192220052 0.99144486137381 0.38268343236509 0.923879532511287 0.608761429008721 0.793353340291235 0.793353340291235 0.608761429008721 0.923879532511287 0.38268343236509 0.99144486137381 0.130526192220051 0.99144486137381 -0.130526192220051 0.923879532511287 -0.38268343236509 0.793353340291235 -0.60876142900872 0.608761429008721 -0.793353340291235 0.38268343236509 -0.923879532511287 0.130526192220052 -0.99144486137381 -0.130526192220052 -0.99144486137381 -0.38268343236509 -0.923879532511287 -0.608761429008721 -0.793353340291235 -0.793353340291235 -0.608761429008721 -0.923879532511287 -0.38268343236509 -0.99144486137381 -0.130526192220052 -0.99144486137381 0.130526192220051 -0.923879532511287 0.38268343236509 -0.793353340291235 0.608761429008721 -0.608761429008721 0.793353340291235 -0.38268343236509 0.923879532511287 -0.130526192220052 0.99144486137381 0.130526192220052 0.99144486137381 0.38268343236509 0.923879532511287 0.608761429008721 0.793353340291235 0.793353340291235 0.608761429008721 0.923879532511287 0.38268343236509 0.99144486137381 0.130526192220051 0.99144486137381 -0.130526192220051 0.923879532511287 -0.38268343236509 0.793353340291235 -0.60876142900872 0.608761429008721 -0.793353340291235 0.38268343236509 -0.923879532511287 0.130526192220052 -0.99144486137381 -0.130526192220052 -0.99144486137381 -0.38268343236509 -0.923879532511287 -0.608761429008721 -0.793353340291235 -0.793353340291235 -0.608761429008721 -0.923879532511287 -0.38268343236509 -0.99144486137381 -0.130526192220052 -0.99144486137381 0.130526192220051 -0.923879532511287 0.38268343236509 -0.793353340291235 0.608761429008721 -0.608761429008721 0.793353340291235 -0.38268343236509 0.923879532511287 -0.130526192220052 0.99144486137381 0.130526192220052 0.99144486137381 0.38268343236509 0.923879532511287 0.608761429008721 0.793353340291235 0.793353340291235 0.608761429008721 0.923879532511287 0.38268343236509 0.99144486137381 0.130526192220051 0.99144486137381 -0.130526192220051 0.923879532511287 -0.38268343236509 0.793353340291235 -0.60876142900872 0.608761429008721 -0.793353340291235 0.38268343236509 -0.923879532511287 0.130526192220052 -0.99144486137381 -0.130526192220052 -0.99144486137381 -0.38268343236509 -0.923879532511287 -0.608761429008721 -0.793353340291235 -0.793353340291235 -0.608761429008721 -0.923879532511287 -0.38268343236509 -0.99144486137381 -0.130526192220052 -0.99144486137381 0.130526192220051 -0.923879532511287 0.38268343236509 -0.793353340291235 0.608761429008721 -0.608761429008721 0.793353340291235 -0.38268343236509 0.923879532511287 -0.130526192220052 0.99144486137381 0.38268343236509 0.923879532511287 0.923879532511287 0.38268343236509 0.923879532511287 -0.38268343236509 0.38268343236509 -0.923879532511287 -0.38268343236509 -0.923879532511287 -0.923879532511287 -0.38268343236509 -0.923879532511287 0.38268343236509 -0.38268343236509 0.923879532511287)) (pub record fast-noise ((seed int) (frequency float) (octaves int) (gain float) (lacunarity float) (weighted-strength float) (bounding float))) (fn fast-floor ((f double)) int (return (if (>= f 0) (cast f int) (- (cast f int) 1)))) (fn hash ((seed int) (x int) (y int)) int (return (* (^ seed x y) HASH-MUL))) (fn grad-coord ((seed int) (x int) (y int) (xd float) (yd float)) float (var h int (hash seed x y)) (^= h (>> h 15)) (&= h 254) (return (+ (* xd [gradients h]) (* yd [gradients (c-bit-or h 1)])))) ;;; SingleSimplex: the 2D OpenSimplex2 kernel, after the skew. (fn single-simplex ((seed int) (x double) (y double)) float (var sqrt3 float 1.7320508075688772935274463415059) (var g2 float (/ (- (cast 3 float) sqrt3) (cast 6 float))) (var i int (fast-floor x)) (var j int (fast-floor y)) (var xi float (cast (- x i) float)) (var yi float (cast (- y j) float)) (var t float (* (+ xi yi) g2)) (var x0 float (- xi t)) (var y0 float (- yi t)) (*= i PRIME-X) (*= j PRIME-Y) (var one float 1) (var two float 2) (var a float (- (cast 0.5 float) (* x0 x0) (* y0 y0))) (var n0 float 0) (if (> a 0) (= n0 (* (* a a) (* a a) (grad-coord seed i j x0 y0)))) (var c1 float (* (* (* two (- one (* two g2))) (- (/ one g2) two)) t)) (var c2 float (* (* (- two) (- one (* two g2))) (- one (* two g2)))) (var c float (+ c1 (+ c2 a))) (var n2 float 0) (if (> c 0) (do (var x2 float (+ x0 (- (* two g2) one))) (var y2 float (+ y0 (- (* two g2) one))) (= n2 (* (* c c) (* c c) (grad-coord seed (+ i PRIME-X) (+ j PRIME-Y) x2 y2))))) (var n1 float 0) (if (> y0 x0) (do (var x1 float (+ x0 g2)) (var y1 float (+ y0 (- g2 one))) (var b float (- (cast 0.5 float) (* x1 x1) (* y1 y1))) (if (> b 0) (= n1 (* (* b b) (* b b) (grad-coord seed i (+ j PRIME-Y) x1 y1))))) (do (var x1 float (+ x0 (- g2 one))) (var y1 float (+ y0 g2)) (var b float (- (cast 0.5 float) (* x1 x1) (* y1 y1))) (if (> b 0) (= n1 (* (* b b) (* b b) (grad-coord seed (+ i PRIME-X) j x1 y1)))))) (return (* (+ n0 n1 n2) (cast 99.83685446303647 float)))) (ops fast-noise ;; Rivers' Noise(seed, frequency, octaves, gain, lacunarity): FBm with ;; weighted strength -0.2. (pub fn make ((seed int) (frequency float) (octaves int) (gain float) (lacunarity float)) fast-noise (var n fast-noise #(fast-noise : seed frequency octaves gain lacunarity (cast -0.2 float) 0)) (var g float (if (< gain 0) (- gain) gain)) (var amp float g) (var fractal float 1) (for (var i int 1) (< i octaves) (++ i) (+= fractal amp) (*= amp g)) (= (. n bounding) (/ (cast 1 float) fractal)) (return n)) (pub fn get ((self (* const fast-noise)) (x double) (y double)) float (*= x (. self frequency)) (*= y (. self frequency)) (var sqrt3 double 1.7320508075688772935274463415059) (var f2 double (* 0.5 (- sqrt3 1))) (var t double (* (+ x y) f2)) (+= x t) (+= y t) (var seed int (. self seed)) (var sum float 0) (var amp float (. self bounding)) (for (var i int 0) (< i (. self octaves)) (++ i) (var noise float (single-simplex seed x y)) (++ seed) (+= sum (* noise amp)) (var m float (+ noise 1)) (if (> m 2) (= m 2)) (var b float (* m (cast 0.5 float))) (*= amp (+ (cast 1 float) (* (. self weighted-strength) (- b (cast 1 float))))) (*= x (. self lacunarity)) (*= y (. self lacunarity)) (*= amp (. self gain))) (return sum)))