-
Notifications
You must be signed in to change notification settings - Fork 0
/
bridey-lib.scm
410 lines (385 loc) · 11.7 KB
/
bridey-lib.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
(define (initial-state char)
(set-state
(create-level '())
'action (list identity)
'nutrition 900
'encumbrance 0
'last-command '(#f #f)
'do-inventory? #t
'fountains '()
'thrones '()
'question-handler handle-question
'pickup-pred (lambda (x) #f)
'level-info (make-vector 91 #f)))
(define (process-turn state)
; need a macro for this :)
(let* ((state (set-state state
'messages '()
'interesting-squares '()))
(state (read-topl state))
(state (set-state state 'coord (get-coord)))
(state (fold process-message state (get-state state 'messages)))
(state (if (and (get-state state 'do-look?)
(not (get-state state 'blind?)))
(do-look (set-state state 'do-look? #f))
state))
(state (if (get-state state 'do-inventory?)
(set-state state
'inventory (get-inventory)
'do-inventory? #f)
state))
(state (begin (botl-update) (turns-passed state)))
(state (scan-map state))
(get (specialize get-state state))
(set (specialize set-state state))
(coord (get-coord)))
(mark-visited coord)
(mark-seen coord)
(if (not (get 'blind?))
(for-each mark-seen (neighbor-squares coord)))
(if (and (weird-position? state)
(eq? (car (get 'last-command)) 'move))
(let ((dest (get 'expected-coord)))
(cond ((and (char=? (square-char dest) #\^)
(eq? (square-color dest) 'magenta)
(not (trap? dest)))
; not levelport trap because those always give a message
(mark-trap dest 'teleportation))
((not (square-clear? state dest))
(mark-trap dest 'maybe)))))
(let ((squares-with-mons
(filter (lambda (c)
(let ((desc (far-look c)))
(not (or (string-prefix? "tame " desc)
(string-prefix? "peaceful " desc)))))
(filter (specialize monster? state)
(neighbor-squares))))
(search (get 'searching-for)))
(cond ((not (null? squares-with-mons)) (hit-something state))
(else (maybe-eat state))))))
(define (turns-passed state)
(define get (specialize get-state state))
(define set (specialize set-state state))
(define (hunger turn)
(count
identity
(list (not (or (get 'slow-digestion?)
(and (get 'polymorphed)
(monster-breathless? (get 'polymorphed)))))
(and (odd? turn) (eq? (get 'regeneration?) 'ring))
(and (odd? turn) (> (get 'encumbrance) 1))
(and (even? turn) (get 'rapid-hunger?))
(and (even? turn) (eq? (get 'conflict?) 'ring))
(and (= (modulo turn 20) 4) (get 'left-ring))
(and (= (modulo turn 20) 8) (get 'amulet))
(and (= (modulo turn 20) 12) (get 'right-ring))
(and (= (modulo turn 20) 16) (get 'have-aoy?)))))
(let ((last (or (get 'last-turn) 1)))
(set 'nutrition (- (get 'nutrition)
(* (if (eq? (car (get 'last-command)) 'fight) 2 1)
(apply + (map hunger (range last (- (turns) 1))))))
'last-turn (turns))))
(define (hit-something state)
(let ((squares-with-mons
(filter (lambda (c)
(let ((desc (far-look c)))
(not (or (string-prefix? "tame " desc)
(string-prefix? "peaceful " desc)))))
(filter (specialize monster? state)
(neighbor-squares)))))
(fight state (map - (car squares-with-mons) (get-coord)))))
;(define (want-item? item)
; (string=? (item-name item) "food ration"))
(define (draw-seen state)
(do ((y 1 (+ y 1)))
((= y 24))
(do ((x 1 (+ x 1)))
((= x 80))
(display (if (seen? (list x y)) "#" ".")))
(newline)))
(define (scan-map state)
(let ((scanners (get-state state 'scanners)))
(iterate-screen
(lambda (state coord glyph)
(case (cadr glyph)
((#\space)
(if (square-covered-by-item? coord)
(unmark-square-covered-by-item coord))
state)
((#\I) state)
(else
(if (not (seen? coord))
(mark-seen coord))
(or (any (lambda (scanner)
(scanner state coord glyph))
scanners)
state))))
state)))
(define (update-map state)
(iterate-screen
(lambda (state coord glyph)
(case (cadr glyph)
((#\space)
(if (square-covered-by-item? coord)
(unmark-square-covered-by-item coord))
state)
((#\I) state)
(else
(if (not (seen? coord))
(mark-seen coord))
(cond
((equal? glyph '(blue #\{))
(add-fountain state coord))
((and (door? coord)
(square-clear? state coord))
(unmark-door coord)
state)
((or (and (item? state coord)
(set-square-covered-by coord))
(member glyph
'((none #\_)
(none #\>)
(none #\<))))
(if (visited? coord)
state
(cons-state state 'interesting-squares coord)))
(else state)))))
state))
(define (walk state)
(if (and (dead-end? state)
(< (searched-for state (get-coord)) 15))
(search state)
(let* ((paths
(list-sort (lambda (a b) (< (length a) (length b)))
(filter-map
(lambda (c)
(find-path-towards state (get-coord) c))
(get-state state 'interesting-squares))))
(dest (and (not (null? paths))
(last (car paths)))))
(if (not dest)
(let ((next (new-nu state)))
(if next
(push-action-go state
(lambda (state)
(go-to state next)))
'done))
(move state (map - (cadr (car paths)) (get-coord)))))))
(define (handle-question state)
(send-expect "\e" expect-generic)
(display "opting out of question\n")
state)
(define (new-nu state)
(define (p? c)
(and (not (visited? c))
(or (any (lambda (c) (not (seen? c)))
(neighbor-squares c))
(and (dead-end? state c)
(< (searched-for state c) 15)))))
(define (cost state from to)
(let ((dir (map - to from)))
(cond ((door? to) 3)
((bad-trap? to) 25)
((and (boulder? to) (not (seen? (map + to dir))))
; explore other areas first if we don't know where
; we're pushing towards
15)
((and (char=? (square-char to) #\#)
(diagonal? dir))
0.7) ; explore corridors first. Shorter.
((and (eq? (car (get-state state 'last-command)) 'move)
(equal? dir (cadr (get-state state 'last-command))))
0.9) ; prefer to walk in a straight line
(else 1))))
(let ((path (find-path state
(get-coord)
p?
(lambda x 0)
(lambda (state from to seed)
(passable? state from to))
#f
cost)))
(and path (last path))))
(define go-towards
(let ((path #f)
(first-dark #f))
(lambda (state dest)
(if (not (and path
(member (get-coord) path)
(equal? (last path) dest)))
(let ((new-path (find-path-towards state (get-coord) dest)))
(and new-path
(begin (set! path new-path)
(set! first-dark
(any (lambda (c)
(and (char=? (square-char c) #\space)
c))
path))
(go-towards state dest))))
(if (and first-dark
(not (char=? (square-char first-dark) #\space)))
(begin (set! path #f)
(go-towards state dest))
(let* ((next-square
(cadr (find-tail (specialize equal? (get-coord)) path)))
(dir (map - next-square (get-coord))))
(if (not (passable? state (get-coord) next-square))
(begin (set! path #f)
(go-towards state dest))
(move (set-state state
'going-towards (and (not (equal? next-square
dest))
dest))
dir))))))))
(define go-to
(let ((path #f))
(lambda (state dest)
(cond ((equal? (get-coord) dest)
(pop-action-go state))
((not (and path
(member (get-coord) path)
(equal? (last path) dest)))
(let ((new-path (find-path-to state (get-coord) dest)))
(if (not new-path)
(pop-action-go (set-state state 'failure 'path-not-found))
(begin (set! path new-path)
(go-to state dest)))))
(else
(let* ((next (cadr (find-tail (specialize equal? (get-coord))
path))))
(cond ((monster? state next)
(push-action-go state handle-blocker))
((not (passable? state (get-coord) next))
(begin (set! path #f)
(go-to state dest)))
(else (move state (map - next (get-coord)))))))))))
(define (get-room-extents state)
(define (extend coord dir)
(let ((wall-char (if (zero? (car dir)) #\- #\|))
(door-char (if (zero? (car dir)) #\| #\-))
(from-here (map - coord (get-coord))))
(let loop ((n (cond ((or (equal? from-here '(0 0))
(orthogonal? from-here dir))
1)
((equal? from-here dir) 2)
(else 0)))
(c (map + coord dir))
(last coord))
(cond ((or (and (char=? (square-char c) wall-char)
(eq? (square-color c) 'none))
(and (char=? (square-char c) door-char)
(eq? (square-color c) 'brown)))
n)
((and (or (char=? (square-char c) #\#)
(and (char=? (square-char c) #\space) (> n 2))) ; problem with partially lit areas
(or (char=? (square-char last) #\.)
(square-clear? state last)))
(- n 1))
((and (char=? (square-char c) #\space)
(= n 2))
#f)
((not (valid-coord? (map + c dir)))
#f)
(else
(loop (+ n 1) (map + c dir) c))))))
(let* ((directions '((-1 0) (0 -1) (1 0) (0 1)))
(starting-points
(filter
(lambda (c)
(and (char=? (square-char c) #\.)
(every
(lambda (n)
(let ((dir (map - n c)))
(not (and (not (diagonal? dir))
(wall? n)
(if (zero? (car (map - n c)))
(char=? (square-char n) #\|)
(char=? (square-char n) #\-))))))
(neighbor-squares c))))
(neighbor-squares (get-coord))))
(distances
(let loop ((start (cons (get-coord) starting-points))
(acc '(#f #f #f #f)))
(cond ((every identity acc) acc)
((null? start) #f)
(else
(loop
(cdr start)
(map (lambda (dis dir)
(or dis (extend (car start) dir)))
acc
directions))))))
(displacements
(and distances
(map (lambda (dis dir)
(map (lambda (x) (* x dis)) dir))
distances
directions))))
(and displacements
; we combine the displacements for four directions into
; two, north-west and south-east, which we add to our
; starting coordinates to get the coordinates of the
; corners in those two directions.
(call-with-values
(lambda ()
(split-at displacements 2))
(lambda (nw se)
(list (map + (get-coord) (car nw) (cadr nw))
(map + (get-coord) (car se) (cadr se))))))))
(define (print-room-map extents)
(let* ((nw (car extents)) (se (cadr extents))
(x1 (car nw)) (y1 (cadr nw))
(x2 (car se)) (y2 (cadr se)))
(let loop ((y y1))
(if (<= y y2)
(begin
(display (get-row-plaintext y x1 x2))
(newline)
(loop (+ y 1)))))))
(define (corpse-yummy? state mon) #t)
(define (maybe-eat state)
(let ((objects (get-state state 'objects-here))
(nutrition (get-state state 'nutrition)))
(or (and (get-state state 'corpses)
objects
(not (null? objects))
(< nutrition 800)
(let ((cutoff (- (turns) 40))
(x (assoc (get-coord) (get-state state 'corpses))))
(and x
(> (caddr x) cutoff)
(corpse-yummy? state (cadr x))
(any (lambda (item)
(let ((mon (item-corpse-of item)))
(and mon (string=? (cadr x) mon))))
objects)
(eat-from-floor state
(lambda (item)
(let ((c (item-corpse-of item)))
(and c (string=? c (cadr x)))))))))
(continue-action state))))
(define (push-action state . ls)
(set-state state
'action
(append (reverse ls) (or (get-state state 'action) '()))))
(define (push-action-go state . ls)
((last ls)
(apply push-action state ls)))
(define (pop-action state)
(set-state state
'action
(let ((action (get-state state 'action)))
(if (or (not action)
(null? action))
'()
(cdr action)))))
(define (pop-action-go state)
(let* ((s (pop-action state))
(f (car (get-state s 'action))))
(f s)))
(define (continue-action state)
((car (get-state state 'action)) state))
(define (handle-blocker state)
'handle-blocker)
;(define (go-down-and-up state)
; (