view clojure/com/aurellem/run/bootstrap_0.clj @ 323:92ee94945327

created functions to set the cursor to any position in any list
author Robert McIntyre <rlm@mit.edu>
date Wed, 04 Apr 2012 00:56:53 -0500
parents d604bd3c122c
children 7876781520ea
line wrap: on
line source
1 (ns com.aurellem.run.bootstrap-0
2 (:use (com.aurellem.gb saves gb-driver util
3 items vbm characters money))
4 (:use (com.aurellem.run util title save-corruption))
5 (:use (com.aurellem.exp item-bridge))
6 (:import [com.aurellem.gb.gb_driver SaveState]))
8 (defn-memo boot-root []
9 [ [] (root)])
11 (defn-memo to-rival-name
12 ([] (to-rival-name (boot-root)))
13 ([script]
14 (->> script
15 title
16 oak
17 name-entry-rlm
18 (scroll-text 5))))
20 (defn-memo name-rival-bootstrap
21 ([] (name-rival-bootstrap (to-rival-name)))
22 ([script]
23 (->> script
24 (first-difference [] [:a] AF)
25 (first-difference [] [:r] DE)
26 (play-moves
27 [[]
28 [] [] [:r] [] [:d] [:a] ;; L
29 [:r] [] [:r] [] [:r] [] [:r] []
30 [:r] [] [:d] [] [:d] [:a] ;; [PK]
31 [:u] [] [:l] [] [:l] []
32 [:l] [] [:l] [] [:l] [:a] ;; U
33 [:r] [] [:r] [] [:r] []
34 [:r] [] [:r] [] [:d] [:a] ;; [PK]
35 [] [:a] ;; [PK]
36 [] [:a] ;; [PK]
37 [:r] [] [:d] [:a] ;; END
38 ]))))
40 (defn-memo leave-house
41 ([] (leave-house (name-rival-bootstrap)))
42 ([script]
43 (->> script
44 finish-title
45 walk-to-stairs
46 walk-to-door
47 (walk [↓ ↓]))))
49 (defn-memo to-pallet-town-edge
50 ([] (to-pallet-town-edge (leave-house)))
51 ([script]
52 (->> script
53 (walk [→ → → → →
54 ↑ ↑ ↑ ↑ ↑ ↑]))))
56 (defn-memo start-pikachu-battle
57 ([] (start-pikachu-battle
58 (to-pallet-town-edge)))
59 ([script]
60 (->> script
61 (first-difference [:b] [:b :a] DE)
62 scroll-text
63 (do-nothing 200)
64 (play-moves [[:b]]))))
66 (defn-memo capture-pikachu
67 ([] (capture-pikachu (start-pikachu-battle)))
68 ([script]
69 (->> script
70 (scroll-text 3))))
72 (defn-memo go-to-lab
73 ([] (go-to-lab (capture-pikachu)))
74 ([script]
75 (->> script
76 end-text
77 (scroll-text 5)
78 end-text
79 ;; oak walks you to his lab; no input required.
80 (do-nothing 400))))
82 (defn-memo talk-to-oak-in-lab
83 ([] (talk-to-oak-in-lab (go-to-lab)))
84 ([script]
85 (->> script
86 (scroll-text 14)
87 end-text)))
89 (defn-memo try-to-get-eevee
90 ([] (try-to-get-eevee (talk-to-oak-in-lab)))
91 ([script]
92 (->> script
93 ;; walk to pokeball
94 (walk [↓ → →])
95 ;; and try to grab it
96 (play-moves
97 (concat [↑ ↑ [:a]]
98 (repeat 100 [])))
99 (scroll-text 10)
100 (end-text))))
102 (defn-memo obtain-pikachu
103 ([] (obtain-pikachu (try-to-get-eevee)))
104 ([script]
105 (->> script
106 (scroll-text 6)
107 (end-text))))
110 (defn-memo begin-battle-with-rival
111 ([] (begin-battle-with-rival
112 (obtain-pikachu)))
113 ([script]
114 (->> script
115 (walk [↓ ↓ ↓])
116 (scroll-text 3)
117 (end-text)
118 (scroll-text))))
120 (defn-memo defeat-eevee
121 ([] (defeat-eevee
122 (begin-battle-with-rival)))
123 ([script]
124 (->> script
125 (do-nothing 400)
126 (play-moves [[:a]])
127 (critical-hit)
128 (do-nothing 200)
129 (scroll-text 2) ;; for eevee's tail-whip
130 (do-nothing 10)
131 (play-moves [[:a]])
132 (critical-hit)
133 (do-nothing 200)
134 (scroll-text 2) ;; tail whip again
135 (do-nothing 10)
136 (play-moves [[:a]])
137 (critical-hit)
138 (do-nothing 200))))
140 (defn-memo finish-rival-text
141 ([] (finish-rival-text
142 (defeat-eevee)))
143 ([script]
144 (->> script
145 (scroll-text 12)
146 (end-text))))
148 (defn-memo pikachu-comes-out
149 ([] (pikachu-comes-out
150 (finish-rival-text)))
151 ([script]
152 (->> script
153 (scroll-text 8)
154 (end-text))))
156 (defn-memo leave-oaks-lab
157 ([] (leave-oaks-lab
158 (pikachu-comes-out)))
159 ([script]
160 (->> script
161 (walk [↓ ↓ ↓ ↓ ↓ ↓]))))
163 (defn-memo oaks-lab->pallet-town-edge
164 ([] (oaks-lab->pallet-town-edge
165 (leave-oaks-lab)))
166 ([script]
167 (->> script
168 (walk [← ← ←
169 ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ →]))))
171 (defn-memo pallet-edge->viridian-mart
172 ([] (pallet-edge->viridian-mart true
173 (oaks-lab->pallet-town-edge)))
174 ([dodge-stupid-guy? script]
175 (let [dodge-1 (if dodge-stupid-guy?
176 [→ →]
177 [→])
178 dodge-2 (if dodge-stupid-guy?
179 [↑ ↑ ←]
180 [↑ ↑])]
182 (->> script
183 ;; leave straight grass
184 (walk-thru-grass
185 [↑ ↑ ↑ ↑ ↑])
187 (walk [↑ ↑ ↑ ↑])
189 (walk-thru-grass
190 [← ← ↑])
192 (walk [↑ ↑ ↑ ↑ → → → ])
194 (walk-thru-grass
195 [→ ↑ ↑ ←])
197 (walk
198 [← ←
199 ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑
200 → → → → ])
202 ;; this part is dependent on that
203 ;; stupid NPC in the grass patch
204 (walk-thru-grass
205 (concat dodge-1
206 [↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ]))
208 (walk
209 (concat
210 dodge-2
211 [← ← ←
212 ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑
213 ← ←
214 ↑ ↑ ↑ ↑
215 → → → → → → → → → →
216 ↑ ↑ ↑ ↑ ↑ ↑ ↑]))))))
218 (defn-memo get-oaks-parcel
219 ([] (get-oaks-parcel
220 (pallet-edge->viridian-mart)))
221 ([script]
222 (->> script
223 (do-nothing 50)
224 (end-text)
225 (scroll-text 3)
226 (do-nothing 197)
227 (play-moves [[:a] []])
228 (walk [↓ ↓ → ↓]))))
230 (defn-memo viridian-store->oaks-lab
231 ([] (viridian-store->oaks-lab
232 (get-oaks-parcel)))
233 ([script]
234 (->> script
235 (walk [↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
236 ← ← ← ← ← ← ← ← ←
237 ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
238 ← ←
239 ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
240 ↓ ↓ ↓ ↓ ↓ ↓ ↓
241 → → → → → → → →
242 ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
243 ← ← ← ← ←
244 ↓ ↓ ↓ ↓
245 ])
246 (walk-thru-grass
247 [↓ ↓ ↓ ↓ ↓ ↓ ↓])
248 (walk [↓ ↓ ← ↓ ↓ ↓ ←
249 ↓ ↓ ↓ ↓ ↓ ↓
250 → → → ↑])
252 (do-nothing 1))))
255 (defn-memo viridian-store->oaks-lab-like-a-boss
256 ([] (viridian-store->oaks-lab-like-a-boss
257 (get-oaks-parcel)))
258 ([script]
259 (->> script
260 (walk [↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
261 ← ← ← ← ← ← ← ← ←
262 ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓])
264 (walk-thru-grass
265 [↓ ↓ ↓ ↓ ↓])
267 (walk
268 [↓ ↓ ← ↓
269 ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
270 → → → ↓])
272 (walk-thru-grass
273 [↓ ↓ ↓])
275 (walk [↓ ← ← ↓ ↓ ↓ ↓ ↓ ↓])
277 (walk-thru-grass
278 [↓ ↓ ↓ ↓ ↓ ↓])
280 (walk [↓ ↓ ↓ ← ↓ ↓ ↓
281 ↓ ↓ ↓ ↓ ↓
282 → → → ↑]))))
284 (defn-memo deliver-oaks-parcel
285 ([] (deliver-oaks-parcel
286 (viridian-store->oaks-lab-like-a-boss)))
287 ([script]
288 (->> script
289 (walk [↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑])
290 (play-moves [[] [:a]])
291 (scroll-text 13)
292 (end-text)
293 (do-nothing 200)
294 (scroll-text 2)
295 (end-text)
296 (scroll-text 2)
297 (end-text)
298 (scroll-text 8)
299 (end-text)
300 (scroll-text 9)
301 (end-text)
302 (scroll-text 7)
303 (end-text)
304 (walk [↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓]))))
306 (defn-memo return-to-viridian-mart
307 ([] (return-to-viridian-mart
308 (deliver-oaks-parcel)))
309 ([script]
310 (->> script
311 oaks-lab->pallet-town-edge
312 (pallet-edge->viridian-mart false))))
314 (defn-memo walk-to-counter
315 ([] (walk-to-counter
316 (return-to-viridian-mart)))
317 ([script]
318 (->> script
319 (walk [↑ ↑ ←]))))
323 ;; useful addresses
324 52262 ;; --- current-cursor-offset
325 52278 ;; --- current screen-offset
328 (defn exp-item-list []
329 (clojure.pprint/pprint
330 (apply harmonic-compare
331 (map read-state
332 ["up-1" "down-1"
333 "up-2" "down-2"
334 "up-3" "down-3"
335 "up-4" "down-4"
336 "up-5" "down-5"
337 "up-6"]))))
340 ;; turns out that these addresses are the cursor position
341 ;; for all lists in the game (start list, pokemon list, shop
342 ;; lists, inventory lists, battle list, basically
343 ;; everything!)
345 (def list-cursor-offset-address 52262)
346 (def list-screen-offset-address 52278)
348 (defn list-offset
349 ([^SaveState state]
350 (let [mem (memory state)]
351 (+ (aget mem item-screen-offset-address)
352 (aget mem item-cursor-offset-address))))
353 ([] (item-offset @current-state)))
357 (defn exp-item-selection []
358 (clojure.pprint/pprint
359 (apply memory-compare
360 (map read-state
361 ["1-item"
362 "2-items"
363 "3-items"
364 "4-items"
365 ]))))
367 (def item-quantity-selected-address 65432)
369 (defn item-quantity-selected
370 ([^SaveState state]
371 (aget (memory state) item-quantity-selected-address))
372 ([] (item-quantity-selected @current-state)))
374 (defn set-cursor-relative
375 "Assumes the arrow keys currently control the cursor.
376 Moves the cursor n steps relative to its current
377 position."
378 [n script]
379 (let [key (if (< 0 n) ↓ ↑)]
380 (multiple-times
381 n (partial repeat-until-different
382 key list-offset) script)))
384 (defn set-cursor
385 "Assumes the arrow keys currently control the cursor. Sets
386 the cursor to the desired position. Works for any menu
387 that uses a cursor including the start menu, item menu,
388 pokemon menu, and battle menu."
389 [n [moves state :as script]]
390 (let [current-position (list-offset state)
391 difference (- n current-position)]
392 (println difference)
393 (set-cursor-relative difference script)))
395 (defn buy-item
396 "Assumes that the main item-screen is up, and buys
397 quantity of the nth item in the list, assuming that you
398 have enough money."
399 [n quantity script]
400 (if (= 0 quantity)
401 script
402 (let [after-initial-pause
403 (do-nothing 20 script)
404 move-to-item
405 (reduce (fn [script _]
406 (->> script
407 (play-moves [[:d]])
408 (do-nothing 3)))
409 after-initial-pause
410 (range n))
411 select-item
412 (play-moves [[:a]] move-to-item)
413 request-items
414 (reduce (fn [script _]
415 (->> script
416 (play-moves [[:u]])
417 (do-nothing 1)))
418 select-item
419 (range (dec quantity)))
420 buy-items
421 (->> request-items
422 (do-nothing 10)
423 (play-moves [[:a]])
424 (scroll-text)
425 (scroll-text)
426 (do-nothing 10)
427 (play-moves [[:a]])
428 (scroll-text))]
429 buy-items)))
432 (defn buy-items
433 "Given a list of [item-no quantity], buys the quantity
434 from the shop's list. Assumes that the item list is
435 already up."
436 [item-pairs script]
437 (let [item-lookup (into {0 0 1 0 2 0 3 0 4 0} item-pairs)
438 initial-purchase
439 (->> script
440 (buy-item 0 (item-lookup 0))
441 (buy-item 1 (item-lookup 1))
442 (buy-item 2 (item-lookup 2)))]
443 (cond
444 (and
445 (not= 0 (item-lookup 3))
446 (not= 0 (item-lookup 4)))
447 (->> initial-purchase
448 (do-nothing 20)
449 (play-moves [[:d]])
450 (do-nothing 3)
451 (play-moves [[:d]])
452 (do-nothing 3)
453 (play-moves [[:d]])
454 (do-nothing 10)
455 (buy-item 0 (item-lookup 3))
456 (do-nothing 20)
457 (play-moves [[:d]])
458 (do-nothing 3)
459 (play-moves [[:d]])
460 (do-nothing 3)
461 (play-moves [[:d]])
462 (do-nothing 10)
463 (buy-item 0 (item-lookup 4)))
464 (and (= 0 (item-lookup 3))
465 (not= 0 (item-lookup 4)))
466 (->> initial-purchase
467 (do-nothing 20)
468 (play-moves [[:d]])
469 (do-nothing 3)
470 (play-moves [[:d]])
471 (do-nothing 3)
472 (play-moves [[:d]])
473 (do-nothing 10)
474 (play-moves [[:d]])
475 (do-nothing 10)
476 (buy-item 0 (item-lookup 4)))
477 (and (not= 0 (item-lookup 3))
478 (= 0 (item-lookup 4)))
479 (->> initial-purchase
480 (do-nothing 20)
481 (play-moves [[:d]])
482 (do-nothing 3)
483 (play-moves [[:d]])
484 (do-nothing 3)
485 (play-moves [[:d]])
486 (do-nothing 10)
487 (buy-item 0 (item-lookup 3)))
488 (and (= 0 (item-lookup 3))
489 (= 0 (item-lookup 4)))
490 initial-purchase)))
493 (defn test-buy-items
494 ([] (test-buy-items
495 (walk-to-counter)))
496 ([script]
497 (->> [(first script) (set-money (second script)
498 999999)]
499 (play-moves
500 [[] [:a] []])
501 (scroll-text)
502 (do-nothing 100)
503 (play-moves [[:a]])
504 (do-nothing 100)
505 (buy-items
506 [[0 1]
507 [1 15]
508 [2 1]
509 [3 20]
510 [4 95]
511 ]))))
513 (defn-memo buy-initial-items
514 ([] (buy-initial-items
515 (walk-to-counter)))
516 ([script]
517 (->> script
518 (play-moves
519 [[] [:a] []])
520 (scroll-text)
521 (do-nothing 100)
522 (play-moves [[:a]])
523 (do-nothing 100)
524 (buy-items
525 [[0 1]
526 [1 1]
527 [2 1]
528 [3 1]
529 [4 1]])
530 (do-nothing 100)
531 (play-moves [[:b]])
532 (do-nothing 100)
533 (play-moves [[:b]])
534 (do-nothing 100)
535 (play-moves [[:b] []])
536 (first-difference [:b] [:b :start] AF))))
539 (defn-memo do-save-corruption
540 ([] (do-save-corruption
541 (buy-initial-items)))
542 ([script]
543 (->> script
544 (first-difference [] [:d] AF)
545 (play-moves [[] [] [] [:d]
546 [] [] [] [:d]
547 [] [] [] [:d]
548 [] [] [:a]])
549 scroll-text
550 (play-moves
551 ;; this section is copied from speedrun-2942 and corrupts
552 ;; the save so that the total number of pokemon is set to
553 ;; 0xFF, allowing manipulation of non-pokemon data in RAM
554 ;; via the pokemon interface.
555 [[] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] []
556 [] [] [] [] [] [] [] [] [] [] [] [:select] [:restart]])
557 (title)
558 (first-difference [] [:start] AF)
559 (first-difference [] [:a] AF)
560 (first-difference [:a] [:a :start] AF))))
562 (def menu do-nothing )
564 (defn-memo corrupt-item-list
565 ([] (corrupt-item-list
566 (do-save-corruption)))
567 ([script]
568 (->> script
569 (do-nothing 200)
570 (menu [↓ [:a]]) ; select "POKEMON" from
571 ; from main menu
572 (menu [↓ ↓ ↓ ↓ ↓ ↓ ; go to 6th pokemon
573 [:a] ↓ [:a] ; select "switch"
574 ↓ ↓ ↓ [:a]]) ; switch with 9th "pokemon"
576 (do-nothing 1))))
578 (defn-memo get-burn-heals
579 ([] (get-burn-heals
580 (corrupt-item-list)))
581 ([script]
582 (->> script
583 (menu [[:b] [:b]])
584 (menu [[:a]])
585 (do-nothing 100)
586 (menu [↓ [:a]])
587 (do-nothing 100)
588 (menu [[:a] ↓ [:a]])
589 (scroll-text)
590 (menu [[:b][:b]])
591 (menu [[:a]])
593 (do-nothing 50)
594 (buy-items [[0 1]])
595 (do-nothing 60)
596 (menu [[:a]])
597 (scroll-text)
599 (do-nothing 50)
600 (buy-items [[0 1]])
601 (do-nothing 60)
602 ;;(menu [[:a]])
603 ;;(scroll-text)
605 ;;(do-nothing 300)
606 ;;(menu [[:b] [:b]])
607 ;;(do-nothing 300)
609 (buy-items [[0 1]
610 [1 1]
611 [1 1]
612 [2 1]
613 [3 1]
614 [4 97]])
616 (do-nothing 10))))
618 (defn-memo corrupt-item-list-again
619 ([] (corrupt-item-list-again (get-burn-heals)))
620 ([script]
621 (->> script
622 (do-nothing 10)
623 (play-moves [[:b]])
624 (do-nothing 100)
625 (play-moves [[:b]])
626 (do-nothing 40)
627 (play-moves [[:b]])
628 (first-difference [:b] [:start :b] AF)
629 (menu [[:a] ↑ ↑ ↑ ↑ ↑ ; get fifth pokemon
630 [:a] ↓ [:a] ; and corrupt the
631 ↓ ↓ ↓ ↓ ↓ [:a]]) ; item list again by
632 ; switching it to
633 ))) ; tenth place.
635 (defn-memo viridian-store->viridian-poke-center
636 ([] (viridian-store->viridian-poke-center
637 (corrupt-item-list-again)))
638 ([script]
639 (->> script
640 (do-nothing 100)
641 (play-moves [[:b]])
642 (do-nothing 100)
643 (play-moves [[:b]])
644 (do-nothing 40)
645 ;; leave store
646 (walk [↓ ↓
647 → ↓ ↓])
648 (walk [← ← ← ←
649 ↓ ↓ ↓ ↓ ↓ ↓
650 ← ← ← ↑]))))
652 (defn-memo to-poke-center-computer
653 ([] (to-poke-center-computer
654 (viridian-store->viridian-poke-center)))
655 ([script]
656 (->> script
657 (walk [→ →
658 ↑ ↑ ↑
659 → → → → → → → → → ↑])
660 (do-nothing 1))))
662 (defn-memo begin-deposits
663 ([] (begin-deposits
664 (to-poke-center-computer)))
665 ([script]
666 (->> script
667 ;; access PC
668 (scroll-text 2)
670 ;; access item storage
671 (menu [[:a] [:d] [:a]])
672 (scroll-text 2)
674 ;; begin deposit
675 (menu [[:d] [:a]])
676 (do-nothing 40))))
678 (defn deposit-n-items
679 [n script]
680 (->> script
681 (do-nothing 100)
682 (play-moves [[:a]])
683 (do-nothing 80)
684 (multiple-times
685 (dec n)
686 (fn [script]
687 (->> script
688 (play-moves [[:u]])
689 (do-nothing 1))))
690 (play-moves [[:a]])
691 (scroll-text)))
693 (defn deposit-one-item
694 [script]
695 (->> script
696 (do-nothing 100)
697 (play-moves [[:a]])
698 (do-nothing 80)
699 (play-moves [[:a]])
700 (scroll-text)))
702 (defn-memo create-header
703 ([] (create-header (begin-deposits)))
704 ([script]
705 (->> script
706 (multiple-times 33 deposit-one-item)
707 (do-nothing 1))))
709 (defn bootstrap-init []
710 [(read-moves "bootstrap-init")
711 (read-state "bootstrap-init")])
713 (defn create-bootstrap-program
714 ([] (create-bootstrap-program
715 (create-header)))
716 ([script]
717 (->> script
718 (do-nothing 120)
719 (menu [↓ ↓ ↓ ↓ ↓ ↓ ↓])
720 ;;(deposit-n-items 33)
722 (menu (repeat 17 ↓))
726 (do-nothing 1))))
729 (defn test-pc-item-program []
730 (-> (read-state "bootstrap-init")
731 (set-memory pc-item-list-start 50)
732 (set-memory-range
733 map-function-address-start [0x8B 0xD5])
734 (set-memory-range
735 (inc pc-item-list-start)
736 (flatten
737 [(repeat
738 28
739 [0xFF 0x01])
740 [;; second part of item manipulation program
741 0x00 ;; this starts at address 0xD56C
742 0x2A ;; save (HL)=(target) to A, increment HL
744 0x00
745 0x47 ;; save A to B
747 0x00
748 0x3A ;; save (target+1) to A, decrement HL
750 0x00
751 0x22 ;; A -> target, increment HL [(target+1) -> target]
753 0x00
754 0x70 ;; load B into target+1 [(target) -> target+1]
756 0x00
757 0xC3 ;; first part of absolute jump
759 0x0C ;; return control to pokemon kernel
760 0x5F]
761 (repeat
762 5
763 [0xFF 0x01])
765 [;; first part of item manipulation program
766 0x00
767 0x21 ;; load target into HL
769 0x94 ;; this is the target address
770 0xD5
772 0x00 ;; relative jump back to first part
773 0x18
775 0xE1 ;; of program
776 0x01
778 0xFF ;; spacer
779 0x01
781 0x04 ;; target ID (pokeball)
782 0x3E ;; target Quantity (lemonade)
783 ]]))))