-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda-ai.lisp
423 lines (383 loc) · 17.4 KB
/
lambda-ai.lisp
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
(in-package :pc)
(define-client-constant +wall+ 0)
(define-client-constant +empty+ 1)
(define-client-constant +pill+ 2)
(define-client-constant +power-pill+ 3)
(define-client-constant +fruit+ 4)
(define-client-constant +lm-start-pos+ 5)
(define-client-constant +gh-start-pos+ 6)
(define-client-constant +stay-away-marker+ 99)
(define-client-constant +ghost-step-base+ 100)
(define-client-constant +vitality-standard+ 0)
(define-client-constant +vitality-fright+ 1)
(define-client-constant +vitality-invisible+ 2)
(define-client-constant +up+ 0)
(define-client-constant +down+ 2)
(define-client-constant +left+ 3)
(define-client-constant +right+ 1)
(define-client-constant +lambda-man+ 100)
(define-client-constant +no-move+ 101)
(define-client-constant +frighten-ghost+ 97)
(define-client-constant +frighten-mode-reserve-ticks+ 5)
(define-client-constant +ghost-stay-away-distance+ 7)
(define-client-macro define-tuple (name &rest fields)
(labels ((%gen (fields top-fields)
(when fields
(let* ((field (car fields))
(rest-fields (cdr fields))
(fname (intern (concatenate 'string (symbol-name name) "-" (symbol-name field))))
(cdrs (reduce (lambda (acc f) (declare (ignore f)) `(cdr ,acc))
top-fields
:initial-value 'x)))
(cons
(if rest-fields
`(defun ,fname (x) (car ,cdrs))
`(defun ,fname (x) ,cdrs))
(%gen rest-fields (cons field top-fields)))))))
`(progn
,@(%gen fields nil))))
(define-client-macro untuple (vars form &rest body)
(let ((tmp (gensym "tmp")))
(labels ((%gen (fields accum)
(if fields
(let* ((field (car fields))
(rest-fields (cdr fields))
(cdrs (reduce (lambda (acc f) (declare (ignore f)) `(cdr ,acc))
accum
:initial-value tmp)))
(%gen rest-fields
(cons `(,field ,(if rest-fields `(car ,cdrs) cdrs))
accum)))
(reverse accum))))
`(let* ((,tmp ,form)
,@(%gen vars nil))
,@body))))
(define-tuple world-state
map lambda-man ghosts fruit)
(define-tuple lambda-man
vitality coord direction lives score)
(define-tuple ghost
vitality coord direction)
(define-client-macro mcase (what-expr &rest forms)
(let ((what (gensym "what")))
(labels ((%gen (forms)
(if forms
(let ((form (car forms)))
(if (eq (car form) 'otherwise)
`(progn ,@(cdr form))
`(if (= ,what ,(car form))
(progn ,@(cdr form))
,(%gen (cdr forms)))))
(error "'case' without 'otherwise' final form:~% ~A" forms))))
`(let ((,what ,what-expr)) ,(%gen forms)))))
(defun free? (val)
(mcase val
(+wall+ 0)
(+stay-away-marker+ 0)
(otherwise 1)))
(defun gfree? (val len)
#-secd(declare (optimize (debug 3) (safety 3)))
(if (> val +ghost-step-base+)
(if (> (- val +ghost-step-base+) len) 1 0)
(free? val)))
(defun pill? (val)
(mcase val
(+pill+ 1)
(+power-pill+ 1)
;; (+fruit+ 1)
(otherwise 0)))
(defun pill-or-fruit? (val fruit-ticks ticks-to-go ghost-hunter)
(if ghost-hunter
(if (= val +frighten-ghost+)
1
0)
(mcase val
(+pill+ 1)
(+power-pill+ 1)
(+fruit+ (if (> fruit-ticks ticks-to-go)
1
0))
;; (+fruit+ 1)
(otherwise 0))))
(defun inner-lists-to-bin-tries (lsts acc)
(if (null lsts)
(reverse acc)
(let ((current-lst (car lsts)))
(inner-lists-to-bin-tries (cdr lsts)
(cons (list-to-bin-trie current-lst +wall+)
acc)))))
(defun get-trie-for-map (map)
(list-to-bin-trie (inner-lists-to-bin-tries map nil)
(list-to-bin-trie (cons +wall+ nil) +wall+)))
(define-client-macro tuple (&rest args)
(reduce (lambda (x acc)
`(cons ,x ,acc))
args
:from-end t))
;; =======================================================
;; QUEUE IMPLEMENTATIN
(defun make-queue ()
(cons nil nil))
(defun queue-get (queue)
(let ((get-lst (car queue))
(put-lst (cdr queue)))
(if (null get-lst)
(if (null put-lst)
(cons nil queue)
(queue-get (cons (reverse put-lst) nil)))
(cons (car get-lst)
(cons (cdr get-lst) put-lst)))))
(defun queue-put (new-elem queue)
(let ((get-lst (car queue))
(put-lst (cdr queue)))
(cons get-lst (cons new-elem put-lst))))
(defun queue-empty? (queue)
(if (null (car queue))
(if (null (cdr queue))
1
0)
0))
;; =========================================
(defun get-map-value (map coord)
(let ((x (car coord))
(y (cdr coord)))
(bin-trie-nth (bin-trie-nth map y) x)))
(defun put-map-value (map coord val)
(let* ((x (car coord))
(y (cdr coord))
(y-trie (bin-trie-nth map y)))
(bin-trie-nth-update map y (bin-trie-nth-update y-trie x val))))
(defun up-coord (c)
(cons (car c)
(- (cdr c) 1)))
(defun down-coord (c)
(cons (car c)
(+ (cdr c) 1)))
(defun left-coord (c)
(cons (- (car c) 1)
(cdr c)))
(defun right-coord (c)
(cons (+ (car c) 1)
(cdr c)))
(defun wave3 (map ghost-map front fruit-ticks ghost-hunter)
#-secd(declare (optimize (debug 3) (safety 3)))
#-secd(format t "wave3 map:~%~{ ~{~3d ~}~%~}~%" (mapcar #'bin-trie-to-list (bin-trie-to-list map)))
#-secd(format t "wave3 ghost map:~%~{ ~{~3d ~}~%~}~%" (mapcar #'bin-trie-to-list (bin-trie-to-list ghost-map)))
(let ((longest-path-len 0)
(longest-path-coords 0))
(labels ((%decode-move (prev-coord coord)
(let ((px (car prev-coord))
(py (cdr prev-coord))
(x (car coord))
(y (cdr coord)))
(if (= x px)
(if (> y py) +down+ +up+)
(if (> x px) +right+ +left+))))
(%restore-path (map prev-coord coord)
;; (format t "restore-path: prev-coord = ~A val = ~A~%" coord (get-map-value map prev-coord))
(if (= (car (get-map-value map prev-coord))
+lambda-man+)
(%decode-move prev-coord coord)
(%restore-path map (get-map-value map prev-coord) prev-coord)))
(%wave (map front)
(if (= 1 (queue-empty? front))
(progn
#+secd(dbug 7777)
#-secd(format t "rec map:~%~{ ~{~3d ~}~%~}~%" (mapcar #'bin-trie-to-list (bin-trie-to-list map)))
#-secd (format t "Trying recovery strategy: len = ~A~%" longest-path-len)
(if (> longest-path-len 0)
(untuple (coord prev-coord) longest-path-coords
#-secd (format t "Trying recovery strategy: coord = ~A prev-coord = ~A~%"
coord prev-coord)
(%restore-path map prev-coord coord))
(progn
#+secd(dbug 8888)
+no-move+)))
(untuple
(step-info front) (queue-get front)
(untuple
(coord prev-coord len) step-info
(let ((val (get-map-value map coord))
(gval (get-map-value ghost-map coord)))
;;#+secd (dbug (cons step-info val))
(if (atom val)
(if (= 1 (gfree? gval len))
(progn
(if (> len longest-path-len)
(progn
(setq longest-path-len len)
(setq longest-path-coords (tuple coord prev-coord)))
0)
(if (= 1 (pill-or-fruit? val fruit-ticks (* len 127) ghost-hunter))
(%restore-path map prev-coord coord)
(labels ((%update-front (front move-func)
(queue-put (tuple (funcall move-func coord)
coord
(+ len 1))
front)))
(%wave (put-map-value map coord prev-coord)
(%update-front
(%update-front
(%update-front
(%update-front front #'up-coord) #'down-coord) #'left-coord) #'right-coord)))))
(%wave map front))
(%wave map front))))))))
(%wave map front))))
(defun move-coord (coord direction)
#-secd(declare (optimize (debug 3) (safety 3)))
(mcase direction
(+up+ (up-coord coord))
(+down+ (down-coord coord))
(+left+ (left-coord coord))
(otherwise (right-coord coord))))
;; TODO: select closest ghost steps number
(defun wave2 (map front)
#-secd(declare (optimize (debug 3) (safety 3)))
#-secd(format t "wave2 map:~%~{ ~{~3d ~}~%~}~%" (mapcar #'bin-trie-to-list (bin-trie-to-list map)))
(if (= 1 (queue-empty? front))
map
(untuple
(step-info front) (queue-get front)
(untuple (length1 coord direction) step-info
(let* ((next-coord (move-coord coord direction))
(next-value (get-map-value map next-coord))
(map (put-map-value map coord (if (> length1 3)
(+ +ghost-step-base+ length1)
+stay-away-marker+))))
#-secd(format t "wave2: dir = ~A c = ~A nc = ~A nv = ~A free? = ~A len = ~A~%"
direction coord next-coord next-value (free? next-value) length1)
(if (> length1 +ghost-stay-away-distance+)
;; (if (= 1 (free? next-value))
;; (progn
;; #-secd (format t ">> taking direction, length = ~A~%" length1)
;; (wave2 map (queue-put (tuple (- length1 1) next-coord direction)
;; front))))
map
(let* ((moves1 (mcase direction
(+up+ (tuple (tuple +up+ +left+ +right+) +down+))
(+down+ (tuple (tuple +down+ +left+ +right+) +up+))
(+left+ (tuple (tuple +left+ +up+ +down+) +right+))
(otherwise (tuple (tuple +right+ +down+ +up+) +left+))
))
(moves (car moves1))
(opposite (cdr moves1))
(good-updates-happened 0))
(untuple
(m1 m2 m3) moves
(labels ((%update-front (front direction)
(let* ((next-coord1 (move-coord coord direction))
(next-value1 (get-map-value map next-coord1)))
#-secd (format t "trying direction ~A nc = ~A nv = ~A free? = ~A~%"
direction next-coord1 next-value1 (free? next-value1))
(if (= 1 (gfree? next-value1 length1))
(progn
(setq good-updates-happened 1)
(queue-put (tuple (+ length1 1) next-coord1 direction)
front))
front))))
(let ((front (%update-front
(%update-front
(%update-front front m1) m2) m3)))
;;#-secd(format t "<< bad direction~%")
(wave2 map
(if (= 1 good-updates-happened)
front
(%update-front front opposite)))))))))))))
(defun mark-way (map lm-coord coord direction)
(let* ((lm (get-map-value map lm-coord))
(map (wave2 (put-map-value map lm-coord +stay-away-marker+)
(queue-put (tuple 0 coord direction) (make-queue))))
(map (put-map-value map lm-coord lm)))
#-secd(format t "map:~%~{ ~{~3d ~}~%~}~%" (mapcar #'bin-trie-to-list (bin-trie-to-list map)))
map))
(defun mark-ghost-ways (map lm-coord ghosts ghost-hunter)
#-secd(declare (optimize (debug 3)))
(labels ((%mark (map ghosts)
(if (null ghosts)
map
(let ((g (car ghosts)))
;;#+secd(dbug g)
(%mark
(if (= +vitality-standard+ (ghost-vitality g))
(mark-way map lm-coord (ghost-coord g) (ghost-direction g))
(if ghost-hunter
map
(mark-way map lm-coord (ghost-coord g) (ghost-direction g))))
(cdr ghosts))))))
(%mark map ghosts)))
(defun mark-frighten-ghosts (map ghosts)
#-secd(declare (optimize (debug 3)))
(labels ((%mark (map ghosts)
(if (null ghosts)
map
(let ((g (car ghosts)))
;;#+secd(dbug g)
(%mark
(mcase (ghost-vitality g)
(+vitality-fright+ (put-map-value map (ghost-coord g) +frighten-ghost+))
(otherwise map))
(cdr ghosts))))))
(%mark map ghosts)))
(defun make-wave-step ()
(lambda (ai-state world-state)
#-secd(declare (ignore ai-state))
(let* ((lambda-man (world-state-lambda-man world-state))
(lambda-man-coords (lambda-man-coord lambda-man))
(frighten-ticks (lambda-man-vitality lambda-man))
(ghost-hunter (> frighten-ticks (* 137 +frighten-mode-reserve-ticks+)))
(ghosts (world-state-ghosts world-state))
(map (get-trie-for-map (car world-state)))
(map (if (> frighten-ticks 0)
(mark-frighten-ghosts map ghosts)
map))
(ghost-map (mark-ghost-ways map lambda-man-coords ghosts ghost-hunter))
)
#-secd(format t "final ghost map:~%~{ ~{~3d ~}~%~}~%" (mapcar #'bin-trie-to-list (bin-trie-to-list ghost-map)))
;;(dbug lambda-man-coords)
(let ((result (cons nil (wave3 map
ghost-map
(queue-put (tuple lambda-man-coords (cons +lambda-man+ +lambda-man+) 0) (make-queue))
(world-state-fruit world-state)
ghost-hunter))))
;;#+secd(dbug (cons 5555 result))
result)
)))
(defun wave-main (init-state ghost-programs)
#-secd(declare (ignore init-state ghost-programs))
(cons nil (make-wave-step)))
#-secd
(defun test-wave ()
(funcall (make-wave-step)
nil ;; ai state
(tuple '((0 0 0 0 0 0 0 0 0) ;; map
(0 0 1 1 1 1 1 0 0)
(0 1 1 0 1 0 1 1 0)
(0 1 0 0 1 0 0 1 0)
(0 1 1 1 1 1 1 1 0)
(0 1 0 0 1 0 0 1 0)
(0 1 1 0 1 0 1 1 0)
(0 0 1 1 1 1 1 0 0)
(0 0 0 0 0 0 0 0 0))
(tuple 0 (cons 1 3) +left+) ;; lambda man
(list (tuple 0 (cons 4 4) +down+)) ;; ghosts
nil ;; fruit
)))
;; ;; LIGHTING MAN
;; (defun lightning-main (init-state ghost-programs)
;; #-secd(declare (ignore init-state ghost-programs))
;; (cons nil
;; (lambda (ai-state world-state)
;; #-secd(declare (ignore ai-state))
;; (let* ((map (car world-state))
;; (lm-status (car (cdr world-state)))
;; (lm-coord (car (cdr lm-status)))
;; (x (car lm-coord))
;; (y (cdr lm-coord))
;; (parsed-map (get-trie-for-map map)))
;; (if (free? (bin-trie-nth (bin-trie-nth parsed-map (- y 1)) x))
;; (cons nil +up+)
;; (if (free? (bin-trie-nth (bin-trie-nth parsed-map y) (+ x 1)))
;; (cons nil +right+)
;; (if (free? (bin-trie-nth (bin-trie-nth parsed-map (+ y 1)) x))
;; (cons nil +down+)
;; (cons nil +left+))))))))