]> git.jsancho.org Git - dungeon-master.git/blob - dungeon-master/generators/town.scm
Voronoi mesh (work in progress)
[dungeon-master.git] / dungeon-master / generators / town.scm
1 (define-module (dungeon-master generators town)
2   #:use-module (dungeon-master geom voronoi)
3   #:use-module (dungeon-master geom point)
4   #:export (generate))
5
6 (define (random-bool)
7   (= (random 2) 1))
8
9 (define pi 3.141592654)
10
11 (define (generate patches)
12   "City generator from https://github.com/watabou/TownGeneratorOS/blob/master/Source/com/watabou/towngenerator/building/Model.hx"
13   (set! *random-state* (random-state-from-platform))
14   (when (= patches -1) (set! patches 15))
15   (build-patches patches))
16
17 (define (build-patches patches)
18   (define* (get-points n seed #:optional (l '()))
19     (cond ((> n 0)
20            (let* ((a (+ seed (* (sqrt n) 5)))
21                   (r (if (= n 0)
22                          0
23                          (+ 10 (* n (+ 2 (random:exp))))))
24                   (point (make-point
25                           (* (cos a) r)
26                           (* (sin a) r))))
27              (get-points (- n 1) seed (cons point l))))
28           (else
29            l)))
30
31   (let* ((sa (* (random:exp) 2 pi))
32          (points (get-points (* 8 patches) sa))
33          (voronoi (make-voronoi-mesh points)))
34     (format #t "~a~%~%~a~%" (voronoi-mesh-frame voronoi) (voronoi-mesh-points voronoi))))