Advertisement
Guest User

Untitled

a guest
Jul 8th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. (use srfi-1 srfi-4 srfi-69 engine-bindings posix)
  2. (declare (unit terrain-generation))
  3.  
  4. ;Bijection between natural pairs and naturals
  5. ;Used to resolve coordinates into a deterministic
  6. ;random number for terrain generation
  7. (define (cantor x y)
  8. (+ (quotient
  9. (* (+ x y)
  10. (+ x y 1))
  11. 2)
  12. y))
  13.  
  14. ;Bijection between integers and naturals
  15. (define (z->n x)
  16. (if (positive? x)
  17. (* 2 x)
  18. (- (* -2 x) 1)))
  19.  
  20. ;Bijection between integer pairs and naturals
  21. ;What we really need to seed the rng
  22. (define (z2->n x y)
  23. (cantor (z->n x) (z->n y)))
  24.  
  25. (define (divides? a b) (= (modulo b a) 0))
  26.  
  27. ;assumes x and y are between 0 and 1
  28. ;a3 a4
  29. ; xy
  30. ;a1 a2
  31. (define (interpolate x y f00 f10 f01 f11)
  32. (let ((xp (- 1 x))
  33. (yp (- 1 y)))
  34. (+ (* f00 xp yp)
  35. (* f10 x yp)
  36. (* f01 xp y )
  37. (* f11 x y ))))
  38.  
  39.  
  40. (define (fixedpoints x y minnum maxnum num aseed)
  41. (xoroshiro_randomize (z2->n x y) aseed)
  42. (map (lambda (x) (+ minnum (xoroshiro_random maxnum))) (make-list num)))
  43.  
  44.  
  45. (define (octaves-i x y minnum maxnum aseed)
  46. (define (iter xp yp ct)
  47. (if (and (even? xp) (even? yp) (< ct 4))
  48. (iter (/ xp 2) (/ yp 2) (+ ct 1))
  49. (fixedpoints x y minnum maxnum ct aseed)))
  50. (if (and (divides? 8 x) (divides? 8 y))
  51. (iter (/ x 8) (/ y 8) 1)
  52. '()))
  53.  
  54. (define (octaves x y aseed)
  55. (map (lambda (x) (/ x 1024)) (octaves-i x y 0 1024 aseed)))
  56.  
  57. (define (perlin-layer x y i aseed)
  58. (let ((j (* 4 (expt 2 i))))
  59. (let ((xl (* j (quotient x j)))
  60. (xh (* j (+ 1 (quotient x j))))
  61. (yl (* j (quotient y j)))
  62. (yh (* j (+ 1 (quotient y j)))))
  63. (let ((xp (/ (- x xl) j))
  64. (yp (/ (- y yl) j)))
  65. (interpolate xp yp
  66. (list-ref (octaves xl yl aseed) (- i 1))
  67. (list-ref (octaves xh yl aseed) (- i 1))
  68. (list-ref (octaves xl yh aseed) (- i 1))
  69. (list-ref (octaves xh yh aseed) (- i 1)))))))
  70.  
  71. (define (perlin x y aseed)
  72. (* (+ (/ (perlin-layer x y 1 aseed) 2)
  73. (/ (perlin-layer x y 2 aseed) 4)
  74. (/ (perlin-layer x y 3 aseed) 8)
  75. (/ (perlin-layer x y 4 aseed) 16))
  76. 16/15))
  77. (define (construct-boring x-len y-len z-len)
  78. (define (loop x y z)
  79. (if (= y y-len)
  80. (begin (set! y 0) (set! x (+ x 1))))
  81. (if (= x x-len)
  82. (begin (set! x 0) (set! z (+ z 1))))
  83. (if (= z z-len)
  84. (diorama_end)
  85. (begin
  86. (diorama_add
  87. (if (< y 2)
  88. (hash-table-ref block-table 'stone)
  89. (hash-table-ref block-table 'grass)))
  90. (loop x (+ y 1) z))))
  91. (diorama_begin x-len y-len z-len)
  92. (list
  93. 'diorama
  94. "boring"
  95. (loop 0 0 0)))
  96.  
  97. (define (make-perlinworld aseed)
  98. (define (loop x y z result)
  99. (if (= y 128)
  100. (begin (set! y 0) (set! x (+ x 1))))
  101. (if (= x 16)
  102. (begin (set! x 0) (set! z (+ z 1))))
  103. (if (= z 16)
  104. (reverse result)
  105. (let ((height (* 128 (perlin x z aseed))))
  106. (loop x (+ 1 y) z (cons (if (< y height) grass-block air) result)))))
  107. (make_finiteworld "perlinworld" (loop 0 0 0 '()) 16 128 16 16 16 16))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement