view clojure/com/aurellem/run/bootstrap_0.clj @ 336:25b7bb7da3b1

Fixed two major bugs related to restart events which were causing desync. The entire video now syncs properly.
author Robert McIntyre <rlm@mit.edu>
date Sat, 07 Apr 2012 07:31:59 -0500
parents 57f4c57d2897
children 2dd40f6b6a1f
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 list-screen-offset-address)
352 (aget mem list-cursor-offset-address))))
353 ([] (list-offset @current-state)))
355 (defn exp-item-selection []
356 (clojure.pprint/pprint
357 (apply memory-compare
358 (map read-state
359 ["1-item"
360 "2-items"
361 "3-items"
362 "4-items"
363 ]))))
365 (def item-quantity-selected-address 65432)
367 (defn item-quantity-selected
368 ([^SaveState state]
369 (println "items:" (aget (memory state) item-quantity-selected-address))
370 (aget (memory state) item-quantity-selected-address))
371 ([] (item-quantity-selected @current-state)))
373 (defn set-cursor-relative
374 "Assumes the arrow keys currently control the cursor.
375 Moves the cursor n steps relative to its current
376 position."
377 [n script]
378 (let [key (if (< 0 n) ↓ ↑)]
379 (multiple-times
380 (Math/abs n)
381 (partial first-difference
382 [] key list-offset)
383 script)))
385 (defn set-cursor
386 "Assumes the arrow keys currently control the cursor. Sets
387 the cursor to the desired position. Works for any menu
388 that uses a cursor including the start menu, item menu,
389 pokemon menu, and battle menu."
390 [n [moves state :as script]]
391 (let [current-position (list-offset state)
392 difference (- n current-position)]
393 (println difference)
394 (set-cursor-relative difference script)))
396 (defn set-quantity
397 "Set the quantity of an item to buy or sell to the desired value
398 using the fewest possible button presses."
399 ([total-quantity desired-quantity [moves state :as script]]
400 (let [current-quantity (item-quantity-selected state)
401 loop-point (if (> total-quantity 99) 0xFF 99)
402 distance (- desired-quantity current-quantity)
403 loop-distance (int(* -1 (Math/signum (float distance))
404 (- loop-point (Math/abs distance))))
405 best-path (first (sort-by #(Math/abs %)
406 [distance loop-distance]))
407 direction (if (< 0 best-path) ↑ ↓)]
408 (println "best-path" best-path)
409 (reduce
410 (fn [script _]
411 (delayed-difference [] direction 5 item-quantity-selected
412 script))
414 script
415 (range (Math/abs best-path)))))
416 ([desired-quantity [moves state :as script]]
417 (set-quantity 99 desired-quantity script)))
419 (defn activate-start-menu [script]
420 (first-difference [:b] [:b :start] AF script))
422 (defn wait-until [script-fn script]
423 (let [wait-time
424 (- (dec (count (first (script-fn script))))
425 (count (first script)))]
426 (println "wait-time" wait-time)
427 (do-nothing wait-time script)))
429 (defn select-menu-entry [script]
430 (->> script
431 (wait-until (partial set-cursor-relative 1))
432 (play-moves [[:a] []])))
434 (defn restart
435 "Two button presses after a restart event are lost when converting to
436 VBM format. To compensate, step the state forward two steps."
437 [[moves state :as script]]
438 (->> [(concat moves [[:restart]])
439 (run-moves state [[:restart]
442 ;;[] []
444 ])]
445 (do-nothing 2)))
447 (defn restart
448 "The two button presses after a restart event are converted to
449 blanks. Due to weirdness with the VBM format. To compensate, ensure
450 that the two button presses after restart are both blanks."
451 [script]
452 (play-moves [[:restart] [] []] script))
454 (defn restart-wrong
455 [[moves state :as script]]
456 [(concat moves [[:restart] [] []])
457 (run-moves state [[:restart]])])
459 (defn do-save-corruption
460 ([] (do-save-corruption
461 (walk-to-counter)))
462 ([script]
463 (->> script
464 activate-start-menu
465 (set-cursor 4)
466 select-menu-entry
467 select-menu-entry
468 (play-moves
469 ;; this section is copied from speedrun-2942 and corrupts
470 ;; the save so that the total number of pokemon is set to
471 ;; 0xFF, allowing manipulation of non-pokemon data in RAM
472 ;; via the pokemon interface.
473 [[] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] []
474 [] [] [] [] [] [] [] [] [] [] [] []])
475 (restart)
476 (title)
477 (first-difference [] [:start] AF)
478 (first-difference [] [:a] AF))))
480 (defn gen-corrupted-checkpoint! []
481 (let [[cor-moves cor-save] (do-save-corruption)]
482 (write-moves! cor-moves "cor-checkpoint")
483 (write-state! cor-save "cor-checkpoint")))
485 (defn corrupted-checkpoint []
486 [(read-moves "cor-checkpoint")
487 (read-state "cor-checkpoint")])
489 (def menu do-nothing )
491 (defn close-menu [script]
492 (first-difference [] [:b] AF script))
494 (defn purchase-item
495 "Assumes that the cursor is over the desired item, and purchases
496 quantity of that item."
497 [n script]
498 (->> script
499 select-menu-entry
500 (set-quantity n)
501 (first-difference [] [:a] AF)
502 scroll-text
503 select-menu-entry
504 scroll-text))
506 (defn corrupt-item-list
507 "Corrupt the num-of-items variable by switching a corrupted pokemon
508 into out-of-bounds memory."
509 ([] (corrupt-item-list
510 ;;(corrupted-checkpoint)
511 (do-save-corruption)
512 ))
513 ([script]
514 (->> script
515 activate-start-menu
516 (set-cursor 1) ; select "POKEMON" from
517 select-menu-entry ; from main menu.
518 (set-cursor 5) ; select 6th pokemon
519 select-menu-entry
520 (set-cursor 1)
521 select-menu-entry
522 (repeat-until-different [] list-offset)
523 (set-cursor 9)
524 select-menu-entry ; switch 6th with 10th
525 close-menu
526 close-menu)))
528 (defn get-lots-of-money
529 "Sell 0xFE cancel buttons to make a tremendous amount of money."
530 ([] (get-lots-of-money (corrupt-item-list)))
531 ([script]
532 (->> script
533 (first-difference [] [:a] AF) ; talk to shopkeep
534 (repeat-until-different [] list-offset)
535 (set-cursor 1)
536 select-menu-entry
537 (repeat-until-different [] list-offset)
538 select-menu-entry
539 (set-quantity 0xFF 0xF7)
540 (first-difference [] [:a] AF)
541 select-menu-entry
542 close-menu)))
544 (defn note [str script]
545 (println str) script)
547 (defn buy-bootstrapping-items
548 "Buy items that will become part of the bootstrapping
549 program."
550 ([] (buy-bootstrapping-items (get-lots-of-money)))
551 ([script]
552 (->> script
553 close-menu
554 select-menu-entry
555 (purchase-item 1) ; buying a pokeball overflows
556 ; the item-counter from 0xFF to 0x00
557 ; repairing the item-list.
558 (set-cursor 1)
559 (purchase-item 1) ; these other items are here to
560 ; protect the burn heals when the
561 (set-cursor 2) ; item list is corrupted again.
562 (purchase-item 1)
564 (set-cursor 3)
565 (purchase-item 1)
567 (set-cursor 4) ; 95 burn-heals spells out the
568 (purchase-item 96) ; return address to the pokemon
569 ; kernel. 96 so that they can be
570 ; deposited without causing a shift.
572 close-menu ; stop talking to shopkeep
573 (wait-until select-menu-entry)
574 (play-moves [[:b]])
575 end-text)))
577 (defn corrupt-item-list-again
578 ([] (corrupt-item-list-again (buy-bootstrapping-items)))
579 ([script]
580 (->> script
581 activate-start-menu
582 (set-cursor-relative 0)
583 select-menu-entry
585 ;; repair list-offset for pokemon-list
586 (set-cursor-relative -1)
588 (set-cursor 4) ; switching it to
589 select-menu-entry ; tenth place.
590 (set-cursor 1)
591 select-menu-entry ; select "switch" on 5th
593 (repeat-until-different [] list-offset)
594 (set-cursor 9) ; goto 10th pokemon
595 select-menu-entry ; do switch
596 close-menu
597 close-menu)))
599 (defn leave-viridian-store
600 ([] (leave-viridian-store (corrupt-item-list-again)))
601 ([script]
602 (->> script
603 ;; leave store
604 (walk [↓ ↓ → ↓]))))
606 (defn force-encounter [direction script]
607 (delayed-improbability-search
608 600
609 #(search-string % "Wild")
610 (partial move direction) script))
612 (defn fight-wild-pokemon
613 ([] (fight-wild-pokemon (leave-viridian-store)))
614 ([script]
615 (->> script
616 (walk [↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
617 ← ← ← ← ← ← ← ←
618 ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓])
619 (force-encounter →))))
621 (defn run-from-pokemon
622 ([] (run-from-pokemon (fight-wild-pokemon)))
623 ([script]
624 (->> script
625 (scroll-text)
626 (play-moves [[:a]])
627 (wait-until select-menu-entry)
628 (set-cursor 1)
629 (first-difference [] → AF)
630 (scroll-text)
631 (scroll-text))))
633 29952
635 (defn to-poke-center-computer
636 ([] (to-poke-center-computer
637 (run-from-pokemon)))
638 ([script]
639 (->> script
640 (walk-thru-grass [→ → ↑])
641 (walk [↑ ← ← ←
642 ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑
643 ← ←
644 ↑ ↑ ↑ ↑
645 → → → → ↑])
646 (walk [→ →
647 ↑ ↑ ↑
648 → → → → → → → → →])
649 (first-difference [] ↑ AF))))
651 (defn-memo begin-deposits
652 ([] (begin-deposits
653 (to-poke-center-computer)))
654 ([script]
655 (->> script
656 ;; access PC
657 (scroll-text 2)
659 ;; access item storage
660 (menu [[:a] [:d] [:a]])
661 (scroll-text 2)
663 ;; begin deposit
664 (menu [[:d] [:a]])
665 (do-nothing 40))))
667 (defn deposit-n-items
668 [n script]
669 (->> script
670 (do-nothing 100)
671 (play-moves [[:a]])
672 (do-nothing 80)
673 (multiple-times
674 (dec n)
675 (fn [script]
676 (->> script
677 (play-moves [[:u]])
678 (do-nothing 1))))
679 (play-moves [[:a]])
680 (scroll-text)))
682 (defn deposit-one-item
683 [script]
684 (->> script
685 (do-nothing 100)
686 (play-moves [[:a]])
687 (do-nothing 80)
688 (play-moves [[:a]])
689 (scroll-text)))
691 (defn-memo create-header
692 ([] (create-header (begin-deposits)))
693 ([script]
694 (->> script
695 (multiple-times 33 deposit-one-item)
696 (do-nothing 1))))
698 (defn bootstrap-init []
699 [(read-moves "bootstrap-init")
700 (read-state "bootstrap-init")])
702 (defn create-bootstrap-program
703 ([] (create-bootstrap-program
704 (create-header)))
705 ([script]
706 (->> script
707 (do-nothing 120)
708 (menu [↓ ↓ ↓ ↓ ↓ ↓ ↓])
709 ;;(deposit-n-items 33)
711 (menu (repeat 17 ↓))
715 (do-nothing 1))))
718 (defn test-pc-item-program []
719 (-> (read-state "bootstrap-init")
720 (set-memory pc-item-list-start 50)
721 (set-memory-range
722 map-function-address-start [0x8B 0xD5])
723 (set-memory-range
724 (inc pc-item-list-start)
725 (flatten
726 [(repeat
727 28
728 [0xFF 0x01])
729 [;; second part of item manipulation program
730 0x00 ;; this starts at address 0xD56C
731 0x2A ;; save (HL)=(target) to A, increment HL
733 0x00
734 0x47 ;; save A to B
736 0x00
737 0x3A ;; save (target+1) to A, decrement HL
739 0x00
740 0x22 ;; A -> target, increment HL [(target+1) -> target]
742 0x00
743 0x70 ;; load B into target+1 [(target) -> target+1]
745 0x00
746 0xC3 ;; first part of absolute jump
748 0x0C ;; return control to pokemon kernel
749 0x5F]
750 (repeat
751 5
752 [0xFF 0x01])
754 [;; first part of item manipulation program
755 0x00
756 0x21 ;; load target into HL
758 0x94 ;; this is the target address
759 0xD5
761 0x00 ;; relative jump back to first part
762 0x18
764 0xE1 ;; of program
765 0x01
767 0xFF ;; spacer
768 0x01
770 0x04 ;; target ID (pokeball)
771 0x3E ;; target Quantity (lemonade)
772 ]]))))