-
Notifications
You must be signed in to change notification settings - Fork 0
/
soko.scm
423 lines (393 loc) · 12.2 KB
/
soko.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
(define maps
'(("-------- ------"
"|<|>...---....|"
"|^|-.AB....C..|"
"|^||..DE|.F.G.|"
"|^||....|.....|"
"|^|------H----|"
"|^| |......|"
"|^------......|"
"|..^^^^IJKL...|"
"|??-----......|"
"---- --------")
("------ ----- "
"|....| |...| "
"|.A..----.B.| "
"|.C......D..| "
"|..--->---E.| "
"|---------.---"
"|..^^^<|.....|"
"|..----|F....|"
"--^| |.G...|"
" |^-----.H...|"
" |..^^^^I.J..|"
" |??----------"
" ---- ")
(" ---- -----------"
"--.>-------- |.........|"
"|..........| |.........|"
"|.A-----B-.| |.........|"
"|..|...|.C.| |....<....|"
"|.D.E....F-| |.........|"
"|.G..H..|..| |.........|"
"|.----I.--.| |.........|"
"|..J...K.|.-- |.........|"
"|.---L-...M.------------+|"
"|...|..N-.O.^^^^^^^^^^^^.|"
"|..P......----------------"
"-----..|..| "
" ------- ")
("----------- -----------"
"|....|....--- |.........|"
"|..AB|CD...>| |.........|"
"|.....E...--- |.........|"
"|....|....| |....<....|"
"--.--------- |.........|"
"|..F.|.....| |.........|"
"|.GH.|I.J.K| |.........|"
"|..L.....M.| |.........|"
"|.NOP|Q..R.----------------+-"
"|....|..S.T.^^^^^^^^^^^^^^^.|"
"-----------------------------")
(" -------- "
"---.|....| "
"|...A....|----------"
"|.-.BC-DE|.|.......|"
"|.FG-......|.......|"
"|.-..H.|...|.......|"
"|....-I--J-|...<...|"
"|..KL..M...|.......|"
"|.--...|...|.......|"
"|....-N|---|.......|"
"---..O.-----------+|"
" |..P>^^^^^^^^^^^.|"
" ------------------")
("--------------------"
"|........|...|.....|"
"|.AB..-CD|.-.|.....|"
"|..|.E.F.|GH.|.....|"
"|-.|..-..|.-.|..<..|"
"|...--.......|.....|"
"|...|.I.-...-|.....|"
"|.J.|K.|...--|.....|"
"|-L.|..-----------+|"
"|..M....^^^^^^^^^^.|"
"|...|.>-------------"
"--------")
("--------------------------"
"|>......^^^^^^^^^^^^^^^^.|"
"|.......----------------.|"
"-------.------ |.|"
" |...........| |.|"
" |.A.B.C.D.E.| |.|"
"--------.----| |.|"
"|...F.G..H.I.| |.|"
"|...J........| |.|"
"-----.-------- ------|.|"
" |..K.L.M...| --|.....|.|"
" |.....N....| |.+.....|.|"
" |.O.P...Q.-- |-|.....|.|"
"-------.---- |.+.....+.|"
"|..R.....| |-|.....|--"
"|........| |.+.....| "
"|...------ --|.....| "
"----- ------- ")
(" ------------------------"
" |..^^^^^^^^^^^^^^^^^^..|"
" |..-------------------.|"
"----.| ----- |.|"
"|..|A-- --...| |.|"
"|.....|--|.B..| |.|"
"|.CD..|..|..E.| |.|"
"--..FG|...HI.-- |.|"
" |J..K...|L..| ------|.|"
" |.MN.|..|..O| --|.....|.|"
" |.P.Q---|.R.| |.+.....|.|"
" |.......|..-- |-|.....|.|"
" ----.S..|.-- |.+.....+.|"
" ---.--.| |-|.....|--"
" |.T...| |.+.....| "
" |>.|..| --|.....| "
" ------- ------- ")))
(define solutions
'((A (5 3)
B (10 2)
H (9 7)
J (8 7)
plug
L (10 7)
plug
F (11 3))
(A (3 2)
C (2 2)
D (10 3)
D (3 3)
E (10 7)
H (8 9))))
; Same as in pathfinding.scm
(define (make-standard-heuristic goal)
(lambda (start)
(min-distance start goal)))
; Walking. Don't push boulders. Pretend square boulder-original has no boulder, and
; square boulder-now has a boulder.
(define (find-path-soko state start goal boulder-original boulder-now)
(find-path state
start
(specialize equal? goal)
(make-standard-heuristic goal)
(lambda (state from to seed)
(define (bldr? c)
(or (equal? c boulder-now)
(and (not (equal? c boulder-original))
(boulder? c))))
(let ((dir (map - to from)))
(not (or (wall? to)
(bldr? to)
(every (lambda (d)
(or (bldr? (map + from d))
(wall? (map + from d))))
(list (list (car dir) 0)
(list 0 (cadr dir))))))))
#f
(lambda junk 1)))
; the paths monsters can take in soko.
(define (find-path-soko-monster state start goal)
(find-path state
start
(specialize equal? goal)
(make-standard-heuristic goal)
(lambda (state from to seed)
(not (or (boulder? to)
(wall? to)
(member to (get-state state 'soko-holes)))))
#f
(lambda junk 1)))
; Boulder transition rules for pathfinder. We save the last vantage point as
; the seed to check if we can get around to push the boulder.
(define boulder-transition
(let ((boulder #f))
(lambda (state from to seed)
(if (eq? (car seed) 'boulder)
(begin (set! boulder (cadr seed))
(set! seed #f)))
(let* ((dir (map - to from))
(standing (map - from dir))) ; where we push from
(and (not (or (diagonal? dir)
(wall? to)
(and (boulder? to)
(not (equal? boulder to)))))
(find-path-soko state (or seed (get-coord)) standing boulder from)
standing)))))
(define (find-path-boulder state start goal)
(find-path state
start
(specialize equal? goal)
(make-standard-heuristic goal)
boulder-transition
(list 'boulder start)
(lambda junk 1)))
(define (get-layout n)
(list-ref maps (- n 1)))
(define (get-layout-square n coord)
(string-ref (list-ref (get-layout n) (cadr coord))
(car coord)))
(define (get-layout-dimensions n)
(let ((layout (get-layout n)))
(list (- (string-length (car layout)) 1)
(- (length layout) 1))))
(define (get-total-extents)
(iterate-screen
(lambda (seed coord glyph)
(if (char=? (cadr glyph) #\space)
seed
(apply (lambda (min-coord max-coord)
(list (list (min (car coord) (car min-coord))
(min (cadr coord) (cadr min-coord)))
(list (max (car coord) (car max-coord))
(max (cadr coord) (cadr max-coord)))))
seed)))
'((80 24) (1 1))))
(define (iterate-layout f layout seed)
(let ((dimensions (get-layout-dimensions layout)))
(let loop ((c '(0 0))
(seed seed))
(let ((next (if (< (car c) (car dimensions))
(map + c '(1 0))
(and (< (cadr c) (cadr dimensions))
(list 0 (+ (cadr c) 1)))))
(seed (f c seed)))
(if (not next)
seed
(loop next seed))))))
(define (walls-match? layout offset)
(call/cc
(lambda (esc)
(iterate-layout
(lambda (c seed)
(let ((char (get-layout-square layout c)))
(or (not (member char '(#\| #\- #\space)))
(char=? char (square-char (map + offset c)))
(esc #f))))
layout
#f))))
(define (find-layout level)
(let* ((extents (get-total-extents))
(offset (car extents))
(dimensions (apply (specialize map -) (reverse extents)))
(a (- (* level 2) 1))
(b (* level 2))
(layout (if (equal? dimensions (get-layout-dimensions a))
a
b)))
(values (and (walls-match? layout offset)
layout)
offset)))
(define (get-solution n)
(list-ref solutions (- n 1)))
(define (in-soko?)
(call-with-values
(lambda ()
(find-layout 1))
(lambda (layout offset)
layout)))
(define (parse-starting-boulders layout)
(iterate-layout
(lambda (c seed)
(let ((char (get-layout-square layout c)))
(if (char-alphabetic? char)
(cons (list (string (char-downcase char)) c) seed)
seed)))
layout
'()))
(define (parse-holes layout)
(iterate-layout
(lambda (c seed)
(if (char=? (get-layout-square layout c) #\^)
(cons c seed)
seed))
layout
'()))
(define (find-closest-hole state)
(let ((path (find-path
state
(get-coord)
(let ((holes (get-state state 'soko-holes))
(offset (get-state state 'soko-offset)))
(lambda (c) (member (map - c offset) holes)))
(lambda junk 0)
(lambda (state from to seed)
(not (wall? to)))
#f
(lambda junk 1))))
(and path (last path))))
(define (plug state)
(let ((hole (find-closest-hole state))
(offset (get-state state 'soko-offset))
(solution (get-state state 'soko-solution)))
(if (not hole)
'done?
(let* ((boulder
(any (lambda (b)
(and (find-path-boulder state (map + (cadr b) offset) hole)
(car b)))
(get-state state 'soko-boulders)))
(instr (and boulder
(list (string->symbol (string-upcase boulder))
(map - hole offset)))))
(if (not instr)
(cond ((null? solution) 'solution-incomplete)
((eq? (car solution) 'plug)
(solve-soko (set-state state
'soko-solution (cdr solution))))
(else 'maybe-the-solution-is-wrong))
(solve-soko (set-state state
'soko-solution (append instr solution))))))))
; maybe generalize go-to to the point of handling this?
(define next-step
(let ((path #f))
(lambda (state dest)
(if (not (and path
(member (get-coord) path)
(equal? (last path) dest)))
(begin (set! path (find-path-soko state (get-coord) dest #f #f))
(next-step state dest))
(cadr (find-tail (specialize equal? (get-coord)) path))))))
(define (check-boulders alist offset)
(every (lambda (e) (char=? (square-char (map + offset (cadr e))) #\0))
alist))
(define (solve-soko state)
(define get (specialize get-state state))
(define set (specialize set-state state))
(if (not (get 'soko-layout))
(call-with-values
(lambda () (find-layout 1))
(lambda (layout offset)
(solve-soko (set 'soko-layout layout
'soko-offset offset
'soko-solution (get-solution layout)
'soko-boulders (parse-starting-boulders layout)
'soko-holes (parse-holes layout)
'soko-waited 0))))
(let* ((layout (get 'soko-layout))
(offset (get 'soko-offset))
(boulders (get 'soko-boulders))
(solution (get 'soko-solution)))
(if (or (null? solution)
(eq? (car solution) 'plug))
(if (check-boulders boulders offset)
(plug state)
'soko-failed)
(let* ((piece (string-downcase (symbol->string (car solution))))
(boulder (map + offset (cadr (assoc piece boulders))))
(dest (map + offset (cadr solution)))
(path (or (and (not (null? (get 'soko-path)))
(get 'soko-path))
(let ((new (find-path-boulder state boulder dest)))
(and new (cdr new)))))
(next (and path (not (null? path)) (car path)))
(dir (and next (map - next boulder)))
(standing (and dir (map - boulder dir))))
(cond
((not path)
'mimic-handling?)
((and (eq? (car (get 'last-command)) 'move)
(not (weird-position? state))
(equal? boulder (get-coord)))
(let* ((last-dir (cadr (get 'last-command)))
(newpos (map + (get-coord) last-dir (map - offset))))
(solve-soko
(set 'soko-boulders (assoc-replace (list piece newpos)
(get 'soko-boulders))
'soko-path (cdr path)))))
((equal? boulder dest)
(if (any (let ((msgs (get 'messages)))
(lambda (str) (member str msgs)))
'("The boulder fills a pit."
"You hear the boulder fall."
"The boulder falls into and plugs a hole in the floor!"
"Kerplunk!"))
(solve-soko
(set 'soko-boulders (assoc-delete piece boulders)
'soko-holes (delete (map - dest offset)
(get 'soko-holes))
'soko-solution (cddr (get 'soko-solution))))
(solve-soko
(set 'soko-solution (cddr (get 'soko-solution))))))
((equal? (get-coord) standing)
(if (or (char=? (square-char next) #\I)
(monster? state next))
; try and go kill it
(let* ((p (find-path-soko state (get-coord) next #f #f))
(sq (and p (cadr p))))
(if sq
(move state (map - sq (get-coord)))
; try to wait for it. monsters can move diagonally
; between boulders, right?
(if (and (find-path-soko-monster state next (get-coord))
(< (get 'soko-waited) 12))
(wait (set 'soko-waited (+ (get 'soko-waited) 1)))
'throw-crap)))
(move (set 'soko-path path) dir)))
(else
(let ((sq (next-step state standing)))
(move (set 'soko-path path) (map - sq (get-coord)))))))))))