]> git.jsancho.org Git - dungeon-master.git/blob - dungeon-master/generators/town.scm
Voronoi relax (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 (define relax-steps 3)
11
12 (define (generate patches)
13   "City generator from https://github.com/watabou/TownGeneratorOS/blob/master/Source/com/watabou/towngenerator/building/Model.hx"
14   (set! *random-state* (random-state-from-platform))
15   (when (= patches -1) (set! patches 15))
16   (build-patches patches))
17
18 (define (build-patches n-patches)
19   (define* (get-points n seed #:optional (l '()))
20     (cond ((> n 0)
21            (let* ((a (+ seed (* (sqrt n) 5)))
22                   (r (if (= n 0)
23                          0
24                          (+ 10 (* n (+ 2 (random:exp))))))
25                   (point (make-point
26                           (* (cos a) r)
27                           (* (sin a) r))))
28              (get-points (- n 1) seed (cons point l))))
29           (else
30            l)))
31
32   (define (relax voronoi n step)
33     "Relaxing central wards"
34     (cond ((> step 0)
35            (let* ((voronoi-points (voronoi-mesh-points voronoi))
36                   (n-points (length voronoi-points))
37                   (to-relax (cons (list-ref voronoi-points (- n-points n-patches))
38                                   (list-tail voronoi-points (- n-points 3)))))
39              (relax (voronoi-mesh-relax voronoi to-relax) n (- step 1))))
40           (else
41            voronoi)))
42
43   (let* ((sa (* (random:exp) 2 pi))
44          (points (get-points (* 8 n-patches) sa))
45          (voronoi (relax (make-voronoi-mesh points) n-patches relax-steps)))
46     "end"))