view clojure/com/aurellem/run/bootstrap_0.clj @ 313:8e63b0bb8ea3

major refactoring; made (walk) more robust
author Robert McIntyre <rlm@mit.edu>
date Mon, 02 Apr 2012 10:58:16 -0500
parents 7998b1cf18cf
children 92c47a9cdaea
line wrap: on
line source
1 (ns com.aurellem.run.bootstrap-0
2 (:use (com.aurellem.gb gb-driver util items vbm characters money))
3 (:use (com.aurellem.run title save-corruption))
4 (:use (com.aurellem.exp item-bridge))
5 (:import [com.aurellem.gb.gb_driver SaveState]))
7 (defn-memo boot-root []
8 [ [] (root)])
10 (defn-memo to-rival-name
11 ([] (to-rival-name (boot-root)))
12 ([script]
13 (-> script
14 title
15 oak
16 name-entry-rlm
17 scroll-text
18 scroll-text
19 scroll-text
20 scroll-text
21 scroll-text)))
23 (defn-memo name-rival-bootstrap
24 ([] (name-rival-bootstrap (to-rival-name)))
25 ([script]
26 (->> script
27 (advance [] [:a])
28 (advance [] [:r] DE)
29 (play-moves
30 [[]
31 [] [] [:r] [] [:d] [:a] ;; L
32 [:r] [] [:r] [] [:r] [] [:r] []
33 [:r] [] [:d] [] [:d] [:a] ;; [PK]
34 [:u] [] [:l] [] [:l] []
35 [:l] [] [:l] [] [:l] [:a] ;; U
36 [:r] [] [:r] [] [:r] []
37 [:r] [] [:r] [] [:d] [:a] ;; [PK]
38 [] [:a] ;; [PK]
39 [] [:a] ;; [PK]
40 [:r] [] [:d] [:a] ;; END
41 ]))))
43 (defn-memo leave-house
44 ([] (leave-house (name-rival-bootstrap)))
45 ([script]
46 (->> script
47 finish-title
48 start-walking
49 walk-to-stairs
50 walk-to-door
51 (walk [↓ ↓]))))
53 (defn-memo to-pallet-town-edge
54 ([] (to-pallet-town-edge (leave-house)))
55 ([script]
56 (->> script
57 start-walking
58 (walk [→ → → → →
59 ↑ ↑ ↑ ↑ ↑ ↑]))))
61 (defn-memo start-pikachu-battle
62 ([] (start-pikachu-battle
63 (to-pallet-town-edge)))
64 ([script]
65 (->> script
66 (advance [:b] [:b :a] DE)
67 (scroll-text)
68 (play-moves [[:b]])
69 (scroll-text)
70 (end-text) ;; battle begins
71 (scroll-text))))
73 (defn-memo capture-pikachu
74 ([] (capture-pikachu (start-pikachu-battle)))
75 ([script]
76 (->> script
77 (scroll-text 2)
78 (end-text))))
80 (defn-memo go-to-lab
81 ([] (go-to-lab (capture-pikachu)))
82 ([script]
83 (->> script
84 (scroll-text 5)
85 (end-text)
86 (scroll-text)
87 (end-text)
88 (scroll-text 8)
89 (end-text)
90 (scroll-text)
91 (end-text))))
93 (defn-memo obtain-pikachu
94 ([] (obtain-pikachu (go-to-lab)))
95 ([script]
96 (->> script
97 (scroll-text)
98 (play-moves
99 (concat
100 (repeat 51 [])
101 [[:a] []]))
102 (walk [↓ ↓ → → ↑])
103 (play-moves
104 (concat [[] [:a]]
105 (repeat 100 [])))
106 (scroll-text 9)
107 (end-text)
108 (scroll-text 7)
110 (play-moves
111 (concat
112 (repeat 50 [])
113 [[:b] [] []])))))
115 (defn-memo begin-battle-with-rival
116 ([] (begin-battle-with-rival
117 (obtain-pikachu)))
118 ([script]
119 (->> script
120 (walk [↓ ↓ ↓ ↓])
121 (scroll-text 3)
122 (end-text)
123 (scroll-text))))
125 (defn-memo battle-with-rival
126 ([] (battle-with-rival
127 (begin-battle-with-rival)))
128 ([script]
129 (->> script
130 (do-nothing 400)
131 (play-moves [[:a]])
132 (critical-hit)
133 (do-nothing 100)
134 (scroll-text)
135 (do-nothing 275)
136 (play-moves [[:a]])
137 (critical-hit)
138 (do-nothing 100)
139 (scroll-text)
140 (do-nothing 270)
141 (play-moves [[:a]])
142 (critical-hit)
143 (do-nothing 100)
144 (scroll-text))))
146 (defn-memo finish-rival-text
147 ([] (finish-rival-text
148 (battle-with-rival)))
149 ([script]
150 (->> script
151 (scroll-text 2)
152 (end-text)
153 (scroll-text 9)
154 (end-text))))
156 (defn-memo pikachu-comes-out
157 ([] (pikachu-comes-out
158 (finish-rival-text)))
159 ([script]
160 (->> script
161 (do-nothing 177)
162 (end-text)
163 (scroll-text 7)
164 (end-text))))
166 (defn-memo leave-oaks-lab
167 ([] (leave-oaks-lab
168 (pikachu-comes-out)))
169 ([script]
170 (->> script
171 (walk [← ← ↓ ↓ ↓ ↓ ↓ ↓]))))
173 (defn-memo oaks-lab->pallet-town-edge
174 ([] (oaks-lab->pallet-town-edge
175 (leave-oaks-lab)))
176 ([script]
177 (->> script
178 (walk [← ← ← ←
179 ↑ ↑ ↑ ↑
180 ↑ ↑ ↑ ↑ ↑ ↑
181 → ↑]))))
183 (defn-memo pallet-edge->viridian-mart
184 ([] (pallet-edge->viridian-mart true
185 (oaks-lab->pallet-town-edge)))
186 ([dodge-stupid-guy? script]
187 (let [dodge-1 (if dodge-stupid-guy?
188 [→ →]
189 [→])
190 dodge-2 (if dodge-stupid-guy?
191 [↑ ↑ ←]
192 [↑ ↑ ←])]
194 (->> script
195 ;; leave straight grass
196 (walk-thru-grass
197 [↑ ↑ ↑ ↑ ↑])
199 (walk [↑ ↑ ↑ ↑])
201 (walk-thru-grass
202 [← ← ↑])
204 (walk [↑ ↑ ↑ ↑ → → → ])
206 (walk-thru-grass
207 [→ ↑ ↑ ←])
209 (walk
210 [← ←
211 ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑
212 → → → → ])
214 ;; this part is dependent on that
215 ;; stupid NPC in the grass patch
216 (walk-thru-grass
217 (concat dodge-1
218 [↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ]))
220 (walk
221 (concat
222 dodge-2
223 [← ← ←
224 ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑
225 ← ←
226 ↑ ↑ ↑ ↑
227 → → → → → → → → → →
228 ↑ ↑ ↑ ↑ ↑ ↑ ↑]))))))
230 (defn-memo get-oaks-parcel
231 ([] (get-oaks-parcel
232 (pallet-edge->viridian-mart)))
233 ([script]
234 (->> script
235 (end-text)
236 (scroll-text 3)
237 (do-nothing 197)
238 (play-moves [[:a] []])
239 (walk [↓ ↓ → ↓]))))
241 (defn-memo viridian-store->oaks-lab
242 ([] (viridian-store->oaks-lab
243 (get-oaks-parcel)))
244 ([script]
245 (->> script
246 (walk [↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
247 ← ← ← ← ← ← ← ← ← ←
248 ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
249 ← ←
250 ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
251 ↓ ↓ ↓ ↓ ↓ ↓ ↓
252 → → → → → → → →
253 ↓ ↓ ↓ ↓
254 ← ← ← ← ←
255 ↓ ↓ ↓ ↓])
257 (walk-thru-grass
258 [↓ ↓ ↓ ↓ ↓ ↓ ↓])
260 (walk [↓ ↓ ← ↓ ↓ ↓ ←
261 ↓ ↓ ↓ ↓ ↓
262 → → → ↑]))))
264 (defn-memo viridian-store->oaks-lab-like-a-boss
265 ([] (viridian-store->oaks-lab-like-a-boss
266 (get-oaks-parcel)))
267 ([script]
268 (->> script
269 (walk [↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
270 ← ← ← ← ← ← ← ← ← ←
271 ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓])
273 (walk-thru-grass
274 [↓ ↓ ↓ ↓ ↓])
276 (walk
277 [↓ ↓ ← ↓
278 ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
279 → →])
281 (walk-thru-grass
282 [→ ↓ ↓ ↓])
284 (walk [↓ ← ← ↓ ↓ ↓ ↓ ↓ ↓])
286 (walk-thru-grass
287 [↓ ↓ ↓ ↓ ↓ ↓ ↓])
289 (walk [↓ ↓ ← ↓ ↓ ↓ ←
290 ↓ ↓ ↓ ↓ ↓
291 → → → ↑]))))
293 (defn-memo deliver-oaks-parcel
294 ([] (deliver-oaks-parcel
295 (viridian-store->oaks-lab-like-a-boss)))
296 ([script]
297 (->> script
298 (walk [↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑])
299 (play-moves [[:a]])
300 (scroll-text 11)
301 (end-text)
302 (end-text)
303 (do-nothing 200)
304 (end-text)
305 (scroll-text 3)
306 (end-text)
307 (scroll-text 2)
308 (end-text)
309 (scroll-text 5)
310 (end-text)
311 (scroll-text 2)
312 (end-text)
313 (scroll-text 9)
314 (end-text)
315 (scroll-text 7)
316 (end-text)
317 (walk [← ← ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓]))))
319 (defn-memo return-to-viridian-mart
320 ([] (return-to-viridian-mart
321 (deliver-oaks-parcel)))
322 ([script]
323 (->> script
324 oaks-lab->pallet-town-edge
325 (pallet-edge->viridian-mart false))))
327 (defn-memo walk-to-counter
328 ([] (walk-to-counter
329 (return-to-viridian-mart)))
330 ([script]
331 (->> script
332 (walk [↑ ↑ ← ←]))))
334 (defn buy-item
335 "Assumes that the main item-screen is up, and buys
336 quantity of the nth item in the list, assuming that you
337 have enough money."
338 [n quantity script]
339 (if (= 0 quantity)
340 script
341 (let [after-initial-pause
342 (do-nothing 20 script)
343 move-to-item
344 (reduce (fn [script _]
345 (->> script
346 (play-moves [[:d]])
347 (do-nothing 3)))
348 after-initial-pause
349 (range n))
350 select-item
351 (play-moves [[:a]] move-to-item)
352 request-items
353 (reduce (fn [script _]
354 (->> script
355 (play-moves [[:u]])
356 (do-nothing 1)))
357 select-item
358 (range (dec quantity)))
359 buy-items
360 (->> request-items
361 (do-nothing 10)
362 (play-moves [[:a]])
363 (scroll-text)
364 (scroll-text)
365 (do-nothing 10)
366 (play-moves [[:a]])
367 (scroll-text))]
368 buy-items)))
371 (defn buy-items
372 "Given a list of [item-no quantity], buys the quantity
373 from the shop's list. Assumes that the item list is
374 already up."
375 [item-pairs script]
376 (let [item-lookup (into {0 0 1 0 2 0 3 0 4 0} item-pairs)
377 initial-purchase
378 (->> script
379 (buy-item 0 (item-lookup 0))
380 (buy-item 1 (item-lookup 1))
381 (buy-item 2 (item-lookup 2)))]
382 (cond
383 (and
384 (not= 0 (item-lookup 3))
385 (not= 0 (item-lookup 4)))
386 (->> initial-purchase
387 (do-nothing 20)
388 (play-moves [[:d]])
389 (do-nothing 3)
390 (play-moves [[:d]])
391 (do-nothing 3)
392 (play-moves [[:d]])
393 (do-nothing 10)
394 (buy-item 0 (item-lookup 3))
395 (do-nothing 20)
396 (play-moves [[:d]])
397 (do-nothing 3)
398 (play-moves [[:d]])
399 (do-nothing 3)
400 (play-moves [[:d]])
401 (do-nothing 10)
402 (buy-item 0 (item-lookup 4)))
403 (and (= 0 (item-lookup 3))
404 (not= 0 (item-lookup 4)))
405 (->> initial-purchase
406 (do-nothing 20)
407 (play-moves [[:d]])
408 (do-nothing 3)
409 (play-moves [[:d]])
410 (do-nothing 3)
411 (play-moves [[:d]])
412 (do-nothing 10)
413 (play-moves [[:d]])
414 (do-nothing 10)
415 (buy-item 0 (item-lookup 4)))
416 (and (not= 0 (item-lookup 3))
417 (= 0 (item-lookup 4)))
418 (->> initial-purchase
419 (do-nothing 20)
420 (play-moves [[:d]])
421 (do-nothing 3)
422 (play-moves [[:d]])
423 (do-nothing 3)
424 (play-moves [[:d]])
425 (do-nothing 10)
426 (buy-item 0 (item-lookup 3)))
427 (and (= 0 (item-lookup 3))
428 (= 0 (item-lookup 4)))
429 initial-purchase)))
432 (defn test-buy-items
433 ([] (test-buy-items
434 (walk-to-counter)))
435 ([script]
436 (->> [(first script) (set-money (second script)
437 999999)]
438 (play-moves
439 [[] [:a] []])
440 (scroll-text)
441 (do-nothing 100)
442 (play-moves [[:a]])
443 (do-nothing 100)
444 (buy-items
445 [[0 1]
446 [1 15]
447 [2 1]
448 [3 20]
449 [4 95]
450 ]))))
452 (defn-memo buy-initial-items
453 ([] (buy-initial-items
454 (walk-to-counter)))
455 ([script]
456 (->> script
457 (play-moves
458 [[] [:a] []])
459 (scroll-text)
460 (do-nothing 100)
461 (play-moves [[:a]])
462 (do-nothing 100)
463 (buy-items
464 [[0 1]
465 [1 1]
466 [2 1]
467 [3 1]
468 [4 1]])
469 (do-nothing 100)
470 (play-moves [[:b]])
471 (do-nothing 100)
472 (play-moves [[:b]])
473 (do-nothing 100)
474 (play-moves [[:b] []])
475 (advance [:b] [:b :start]))))
478 (defn-memo do-save-corruption
479 ([] (do-save-corruption
480 (buy-initial-items)))
481 ([script]
482 (->> script
483 (advance [] [:d])
484 (play-moves [[] [] [] [:d]
485 [] [] [] [:d]
486 [] [] [] [:d]
487 [] [] [:a]])
488 scroll-text
489 (play-moves
490 ;; this section is copied from speedrun-2942 and corrupts
491 ;; the save so that the total number of pokemon is set to
492 ;; 0xFF, allowing manipulation of non-pokemon data in RAM
493 ;; via the pokemon interface.
494 [[] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] []
495 [] [] [] [] [] [] [] [] [] [] [] [:select] [:restart]])
496 (title)
497 (advance [] [:start])
498 (advance [] [:a])
499 (advance [:a] [:a :start]))))
501 (defn-memo corrupt-item-list
502 ([] (corrupt-item-list
503 (do-save-corruption)))
504 ([script]
505 (->> script
506 (do-nothing 200)
507 (menu [↓ [:a]]) ; select "POKEMON" from
508 ; from main menu
509 (menu [↓ ↓ ↓ ↓ ↓ ↓ ; go to 6th pokemon
510 [:a] ↓ [:a] ; select "switch"
511 ↓ ↓ ↓ [:a]]) ; switch with 9th "pokemon"
513 (do-nothing 1))))
515 (defn-memo get-burn-heals
516 ([] (get-burn-heals
517 (corrupt-item-list)))
518 ([script]
519 (->> script
520 (menu [[:b] [:b]])
521 (menu [[:a]])
522 (do-nothing 100)
523 (menu [↓ [:a]])
524 (do-nothing 100)
525 (menu [[:a] ↓ [:a]])
526 (scroll-text)
527 (menu [[:b][:b]])
528 (menu [[:a]])
530 (do-nothing 50)
531 (buy-items [[0 1]])
532 (do-nothing 60)
533 (menu [[:a]])
534 (scroll-text)
536 (do-nothing 50)
537 (buy-items [[0 1]])
538 (do-nothing 60)
539 ;;(menu [[:a]])
540 ;;(scroll-text)
542 ;;(do-nothing 300)
543 ;;(menu [[:b] [:b]])
544 ;;(do-nothing 300)
546 (buy-items [[0 1]
547 [1 1]
548 [1 1]
549 [2 1]
550 [3 1]
551 [4 97]])
553 (do-nothing 10))))
555 (defn-memo corrupt-item-list-again
556 ([] (corrupt-item-list-again (get-burn-heals)))
557 ([script]
558 (->> script
559 (do-nothing 10)
560 (play-moves [[:b]])
561 (do-nothing 100)
562 (play-moves [[:b]])
563 (do-nothing 40)
564 (play-moves [[:b]])
565 (advance [:b] [:start :b])
566 (menu [[:a] ↑ ↑ ↑ ↑ ↑ ; get fifth pokemon
567 [:a] ↓ [:a] ; and corrupt the
568 ↓ ↓ ↓ ↓ ↓ [:a]]) ; item list again by
569 ; switching it to
570 ))) ; tenth place.
572 (defn-memo viridian-store->viridian-poke-center
573 ([] (viridian-store->viridian-poke-center
574 (corrupt-item-list-again)))
575 ([script]
576 (->> script
577 (do-nothing 100)
578 (play-moves [[:b]])
579 (do-nothing 100)
580 (play-moves [[:b]])
581 (do-nothing 40)
582 ;; leave store
583 (walk [↓ ↓
584 → ↓ ↓])
585 (walk [← ← ← ←
586 ↓ ↓ ↓ ↓ ↓ ↓
587 ← ← ← ↑]))))
589 (defn-memo to-poke-center-computer
590 ([] (to-poke-center-computer
591 (viridian-store->viridian-poke-center)))
592 ([script]
593 (->> script
594 (walk [→ →
595 ↑ ↑ ↑
596 → → → → → → → → → ↑])
597 (do-nothing 1))))
599 (defn-memo begin-deposits
600 ([] (begin-deposits
601 (to-poke-center-computer)))
602 ([script]
603 (->> script
604 ;; access PC
605 (scroll-text 2)
607 ;; access item storage
608 (menu [[:a] [:d] [:a]])
609 (scroll-text 2)
611 ;; begin deposit
612 (menu [[:d] [:a]])
613 (do-nothing 40))))
615 (defn deposit-n-items
616 [n script]
617 (->> script
618 (do-nothing 100)
619 (play-moves [[:a]])
620 (do-nothing 80)
621 (multiple-times
622 (dec n)
623 (fn [script]
624 (->> script
625 (play-moves [[:u]])
626 (do-nothing 1))))
627 (play-moves [[:a]])
628 (scroll-text)))
630 (defn deposit-one-item
631 [script]
632 (->> script
633 (do-nothing 100)
634 (play-moves [[:a]])
635 (do-nothing 80)
636 (play-moves [[:a]])
637 (scroll-text)))
639 (defn-memo create-header
640 ([] (create-header (begin-deposits)))
641 ([script]
642 (->> script
643 (multiple-times 33 deposit-one-item)
644 (do-nothing 1))))
646 (defn bootstrap-init []
647 [(read-moves "bootstrap-init")
648 (read-state "bootstrap-init")])
650 (defn create-bootstrap-program
651 ([] (create-bootstrap-program
652 (create-header)))
653 ([script]
654 (->> script
655 (do-nothing 120)
656 (menu [↓ ↓ ↓ ↓ ↓ ↓ ↓])
657 ;;(deposit-n-items 33)
659 (menu (repeat 17 ↓))
663 (do-nothing 1))))
666 (defn test-pc-item-program []
667 (-> (read-state "bootstrap-init")
668 (set-memory pc-item-list-start 50)
669 (set-memory-range
670 map-function-address-start [0x8B 0xD5])
671 (set-memory-range
672 (inc pc-item-list-start)
673 (flatten
674 [(repeat
675 28
676 [0xFF 0x01])
677 [;; second part of item manipulation program
678 0x00 ;; this starts at address 0xD56C
679 0x2A ;; save (HL)=(target) to A, increment HL
681 0x00
682 0x47 ;; save A to B
684 0x00
685 0x3A ;; save (target+1) to A, decrement HL
687 0x00
688 0x22 ;; A -> target, increment HL [(target+1) -> target]
690 0x00
691 0x70 ;; load B into target+1 [(target) -> target+1]
693 0x00
694 0xC3 ;; first part of absolute jump
696 0x0C ;; return control to pokemon kernel
697 0x5F]
698 (repeat
699 5
700 [0xFF 0x01])
702 [;; first part of item manipulation program
703 0x00
704 0x21 ;; load target into HL
706 0x94 ;; this is the target address
707 0xD5
709 0x00 ;; relative jump back to first part
710 0x18
712 0xE1 ;; of program
713 0x01
715 0xFF ;; spacer
716 0x01
718 0x04 ;; target ID (pokeball)
719 0x3E ;; target Quantity (lemonade)
720 ]]))))