-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze_maze-map_cell.scm
211 lines (200 loc) · 10.4 KB
/
maze_maze-map_cell.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
;;; maze_maze-map_cell.scm
;;; maze.maze-map.cell -olio.
;;; sis. tiedot etelä- ja länsiseinien olemassaolosta sekä huoneen valaistuksesta
(define (new-cell wall-at-south? wall-at-west?)
; Valoja ei tallenneta save-metodissa.
; *-lighting sisältö:
; Jos kyseistä valoa ei ole:
; false
; Jos kyseinen valo on olemassa, mutta sitä ei ole vielä laskettu:
; 'delayed
; Muuten:
; Molemmat muuttujat sisältävät color-cycle-steps -pituisen vektorin, joka sis:
; cell-lightning -rakenteita.
; (= 5 -pituisen vektorin (0=lattia, 1-4=ilmansuunnat), joka sis:
; valoarvon ko. ilmansuuntaan/lattialle.)
; *-lightning-components sisältö:
; Lista kaikkien huonetta valaisevien valonlähteiden valon kirkkauksista eri color-cycle -vaiheissa,
; ja suunnasta tässä huoneessa. (eli lista color-cycle-steps -pituisia vektoreita, jotka sis valoarvon)
(let ((solve-route-lightning false)
(solve-route-lightning-components '())
(dynamic-lights-lightning false)
(dynamic-lights-lightning-components '()))
; Apufunktiot ---------------------------------------------
; light-component, sis:
; -valonlähteen tunnisteen
; -color-cycle-steps -pituisen vektorin valoarvoja
; -valon suuntavektorin
(define (make-light-component id lights dir-vec)
(vector id
lights
dir-vec))
(define (light-component-id c)
(vector-ref c 0))
(define (light-component-lights c)
(vector-ref c 1))
(define (light-component-dir-vec c)
(vector-ref c 2))
; Kokoaa komponenteista kokonaisvalaistuksen.
; Paluuarvo:
; Sama kuin *-lightning -muuttujien sisältö, ks yllä.
(define (compile-lightning components)
; Valon kirkkaus halutussa suunnassa. Jos light-dir = <number>, on kirkkaus sivuille 1.0 ja
; alaspäin ko. kertoimen verran
; Kirkkaus valon suuntaan: 1.0
; 90' sivulle: light-ambient-diffusion
; vastasuuntaan: light-ambient-diffusion + reflected-light-magnitude-factor
; requested-dir oltava jonkin kantavektorin suuntainen yksikkövektori!
; (lattiavalaistussähläys rajoittaa..)
(define (light-magnitude-in-dir requested-dir light-dir)
(if (vector? light-dir)
; Suuntavektori
(linear-interpolate (inner-product requested-dir
light-dir) ; = t
(cons -1 (+ light-ambient-diffusion
reflected-light-magnitude-factor)) ; 1. ohjauspiste
(cons 0 light-ambient-diffusion) ; 2. ohjauspiste
(cons 1 1.0)) ; 3. ohjauspiste
; Skalaari = lattian kirkkauskerroin
(if (= (z-component requested-dir) 0)
; Seinien kirkkaus
1.0
; Lattian kirkkaus
light-dir)))
(define (compile-light-in-dir lights dir-vecs requested-dir)
(apply add
(map mul
lights
(map (lambda (light-dir-vec)
(light-magnitude-in-dir requested-dir
light-dir-vec))
dir-vecs))))
(define (compile-phase-n-lightning components color-cycle-phase)
(let ((component-lights-in-this-phase (map (lambda (component)
(vector-ref (light-component-lights component)
color-cycle-phase))
components))
(component-dir-vecs (map (lambda (component)
(light-component-dir-vec component))
components)))
(vector
; Lattia
(compile-light-in-dir component-lights-in-this-phase
component-dir-vecs
(vector 0 0 -1))
; east
(compile-light-in-dir component-lights-in-this-phase
component-dir-vecs
(vector 1 0 0))
; north
(compile-light-in-dir component-lights-in-this-phase
component-dir-vecs
(vector 0 1 0))
; west
(compile-light-in-dir component-lights-in-this-phase
component-dir-vecs
(vector -1 0 0))
; south
(compile-light-in-dir component-lights-in-this-phase
component-dir-vecs
(vector 0 -1 0)))))
(if (null? components)
(make-vector color-cycle-steps
dark-cell-lightning)
(generate-vector-with-i color-cycle-steps
(lambda (color-cycle-phase)
(compile-phase-n-lightning components
color-cycle-phase)))))
; Metodit -------------------------------------------------
(define (set-wall! dir value)
(cond ((eq? dir 's) (set! wall-at-south? value))
((eq? dir 'w) (set! wall-at-west? value))
(else (error "Unknown direction! maze.cell.set-wall! " dir))))
(define (wall-at? dir)
(cond ((eq? dir 's) wall-at-south?)
((eq? dir 'w) wall-at-west?)
(else (error "Unknown direction! maze.cell.wall-at? " dir))))
(define (reset-solve-route-lightning!)
(set! solve-route-lightning false)
(set! solve-route-lightning-components '()))
; Palauttaa cell-lightning -rakenteen
(define (get-solve-route-lightning color-cycle-phase)
(if solve-route-lightning
(begin
(if (eq? solve-route-lightning 'delayed)
(set! solve-route-lightning
(compile-lightning solve-route-lightning-components)))
(vector-ref solve-route-lightning color-cycle-phase))
dark-cell-lightning))
(define (reset-dynamic-lights-lightning!)
(set! dynamic-lights-lightning false)
(set! dynamic-lights-lightning-components '()))
; Palauttaa cell-lightning -rakenteen
(define (get-dynamic-lights-lightning color-cycle-phase)
(if dynamic-lights-lightning
(begin
(if (eq? dynamic-lights-lightning 'delayed)
(set! dynamic-lights-lightning
(compile-lightning dynamic-lights-lightning-components)))
;(display (list 'cell-get-dyn: dynamic-lights-lightning)) (newline)
(vector-ref dynamic-lights-lightning color-cycle-phase))
dark-cell-lightning))
; Lisää valonlähteen vaikutuksen tähän huoneeseen.
; light-source-id = mikä tahansa yksilöllinen tunniste, esim valonlähteen osoitin.
; light-source-type = 'solve-route-light tai 'dynamic-light
; lights = color-cycle-steps -pituinen vektori joka sis valoarvoja ko. phasessa.
; dir-vec = valon yksikkö(!)suuntavektori, tai <number> jos valo säteilee joka suuntaan.
; tällöin ko. luku on lattian kirkkauskerroin.
(define (add-light! light-source-id light-source-type lights dir-vec)
(cond ((eq? light-source-type 'solve-route-light)
(set! solve-route-lightning-components
(cons (make-light-component light-source-id
lights
dir-vec)
solve-route-lightning-components))
(set! solve-route-lightning 'delayed))
((eq? light-source-type 'dynamic-light)
(set! dynamic-lights-lightning-components
(cons (make-light-component light-source-id
lights
dir-vec)
dynamic-lights-lightning-components))
(set! dynamic-lights-lightning 'delayed))
(else (error "Unknown light type! maze.maze-map.cell.add-light!." light-source-type))))
; Poistaa valonlähteen vaikutuksen tästä huoneesta
(define (remove-light! light-source-id light-source-type)
(cond ((eq? light-source-type 'solve-route-light)
(set! solve-route-lightning-components
(remove-from-list-if (lambda (component)
(eq? (light-component-id component)
light-source-id))
solve-route-lightning-components))
(set! solve-route-lightning 'delayed))
((eq? light-source-type 'dynamic-light)
(set! dynamic-lights-lightning-components
(remove-from-list-if (lambda (component)
(eq? (light-component-id component)
light-source-id))
dynamic-lights-lightning-components))
(set! dynamic-lights-lightning 'delayed))
(else (error "Unknown light type! maze.maze-map.cell.remove-light!." light-source-type))))
(define (load-from-current!)
(set! wall-at-south? (read))
(set! wall-at-west? (read)))
(define (save-to-current)
(write wall-at-south?)
(write wall-at-west?))
; Dispatch ------------------------------------------------
(define (dispatch method)
(cond ((eq? method 'set-wall!) set-wall!)
((eq? method 'wall-at?) wall-at?)
((eq? method 'reset-solve-route-lightning!) reset-solve-route-lightning!)
((eq? method 'get-solve-route-lightning) get-solve-route-lightning)
((eq? method 'reset-dynamic-lights-lightning!) reset-dynamic-lights-lightning!)
((eq? method 'get-dynamic-lights-lightning) get-dynamic-lights-lightning)
((eq? method 'add-light!) add-light!)
((eq? method 'remove-light!) remove-light!)
((eq? method 'load-from-current!) load-from-current!)
((eq? method 'save-to-current) save-to-current)
(else (error "Unknow method! maze.cell." method))))
dispatch))