Mercurial > vba-clojure
view clojure/com/aurellem/run/bootstrap_0.clj @ 328:35960b03693f
improved delayed-difference with memoization
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Thu, 05 Apr 2012 15:05:08 -0500 |
parents | fe6fd2323264 |
children | a452deec2882 |
line wrap: on
line source
1 (ns com.aurellem.run.bootstrap-02 (:use (com.aurellem.gb saves gb-driver util3 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-name12 ([] (to-rival-name (boot-root)))13 ([script]14 (->> script15 title16 oak17 name-entry-rlm18 (scroll-text 5))))20 (defn-memo name-rival-bootstrap21 ([] (name-rival-bootstrap (to-rival-name)))22 ([script]23 (->> script24 (first-difference [] [:a] AF)25 (first-difference [] [:r] DE)26 (play-moves27 [[]28 [] [] [:r] [] [:d] [:a] ;; L29 [:r] [] [:r] [] [:r] [] [:r] []30 [:r] [] [:d] [] [:d] [:a] ;; [PK]31 [:u] [] [:l] [] [:l] []32 [:l] [] [:l] [] [:l] [:a] ;; U33 [:r] [] [:r] [] [:r] []34 [:r] [] [:r] [] [:d] [:a] ;; [PK]35 [] [:a] ;; [PK]36 [] [:a] ;; [PK]37 [:r] [] [:d] [:a] ;; END38 ]))))40 (defn-memo leave-house41 ([] (leave-house (name-rival-bootstrap)))42 ([script]43 (->> script44 finish-title45 walk-to-stairs46 walk-to-door47 (walk [↓ ↓]))))49 (defn-memo to-pallet-town-edge50 ([] (to-pallet-town-edge (leave-house)))51 ([script]52 (->> script53 (walk [→ → → → →54 ↑ ↑ ↑ ↑ ↑ ↑]))))56 (defn-memo start-pikachu-battle57 ([] (start-pikachu-battle58 (to-pallet-town-edge)))59 ([script]60 (->> script61 (first-difference [:b] [:b :a] DE)62 scroll-text63 (do-nothing 200)64 (play-moves [[:b]]))))66 (defn-memo capture-pikachu67 ([] (capture-pikachu (start-pikachu-battle)))68 ([script]69 (->> script70 (scroll-text 3))))72 (defn-memo go-to-lab73 ([] (go-to-lab (capture-pikachu)))74 ([script]75 (->> script76 end-text77 (scroll-text 5)78 end-text79 ;; oak walks you to his lab; no input required.80 (do-nothing 400))))82 (defn-memo talk-to-oak-in-lab83 ([] (talk-to-oak-in-lab (go-to-lab)))84 ([script]85 (->> script86 (scroll-text 14)87 end-text)))89 (defn-memo try-to-get-eevee90 ([] (try-to-get-eevee (talk-to-oak-in-lab)))91 ([script]92 (->> script93 ;; walk to pokeball94 (walk [↓ → →])95 ;; and try to grab it96 (play-moves97 (concat [↑ ↑ [:a]]98 (repeat 100 [])))99 (scroll-text 10)100 (end-text))))102 (defn-memo obtain-pikachu103 ([] (obtain-pikachu (try-to-get-eevee)))104 ([script]105 (->> script106 (scroll-text 6)107 (end-text))))110 (defn-memo begin-battle-with-rival111 ([] (begin-battle-with-rival112 (obtain-pikachu)))113 ([script]114 (->> script115 (walk [↓ ↓ ↓])116 (scroll-text 3)117 (end-text)118 (scroll-text))))120 (defn-memo defeat-eevee121 ([] (defeat-eevee122 (begin-battle-with-rival)))123 ([script]124 (->> script125 (do-nothing 400)126 (play-moves [[:a]])127 (critical-hit)128 (do-nothing 200)129 (scroll-text 2) ;; for eevee's tail-whip130 (do-nothing 10)131 (play-moves [[:a]])132 (critical-hit)133 (do-nothing 200)134 (scroll-text 2) ;; tail whip again135 (do-nothing 10)136 (play-moves [[:a]])137 (critical-hit)138 (do-nothing 200))))140 (defn-memo finish-rival-text141 ([] (finish-rival-text142 (defeat-eevee)))143 ([script]144 (->> script145 (scroll-text 12)146 (end-text))))148 (defn-memo pikachu-comes-out149 ([] (pikachu-comes-out150 (finish-rival-text)))151 ([script]152 (->> script153 (scroll-text 8)154 (end-text))))156 (defn-memo leave-oaks-lab157 ([] (leave-oaks-lab158 (pikachu-comes-out)))159 ([script]160 (->> script161 (walk [↓ ↓ ↓ ↓ ↓ ↓]))))163 (defn-memo oaks-lab->pallet-town-edge164 ([] (oaks-lab->pallet-town-edge165 (leave-oaks-lab)))166 ([script]167 (->> script168 (walk [← ← ←169 ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ →]))))171 (defn-memo pallet-edge->viridian-mart172 ([] (pallet-edge->viridian-mart true173 (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 (->> script183 ;; leave straight grass184 (walk-thru-grass185 [↑ ↑ ↑ ↑ ↑])187 (walk [↑ ↑ ↑ ↑])189 (walk-thru-grass190 [← ← ↑])192 (walk [↑ ↑ ↑ ↑ → → → ])194 (walk-thru-grass195 [→ ↑ ↑ ←])197 (walk198 [← ←199 ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑200 → → → → ])202 ;; this part is dependent on that203 ;; stupid NPC in the grass patch204 (walk-thru-grass205 (concat dodge-1206 [↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ]))208 (walk209 (concat210 dodge-2211 [← ← ←212 ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑213 ← ←214 ↑ ↑ ↑ ↑215 → → → → → → → → → →216 ↑ ↑ ↑ ↑ ↑ ↑ ↑]))))))218 (defn-memo get-oaks-parcel219 ([] (get-oaks-parcel220 (pallet-edge->viridian-mart)))221 ([script]222 (->> script223 (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-lab231 ([] (viridian-store->oaks-lab232 (get-oaks-parcel)))233 ([script]234 (->> script235 (walk [↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓236 ← ← ← ← ← ← ← ← ←237 ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓238 ← ←239 ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓240 ↓ ↓ ↓ ↓ ↓ ↓ ↓241 → → → → → → → →242 ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓243 ← ← ← ← ←244 ↓ ↓ ↓ ↓245 ])246 (walk-thru-grass247 [↓ ↓ ↓ ↓ ↓ ↓ ↓])248 (walk [↓ ↓ ← ↓ ↓ ↓ ←249 ↓ ↓ ↓ ↓ ↓ ↓250 → → → ↑])252 (do-nothing 1))))255 (defn-memo viridian-store->oaks-lab-like-a-boss256 ([] (viridian-store->oaks-lab-like-a-boss257 (get-oaks-parcel)))258 ([script]259 (->> script260 (walk [↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓261 ← ← ← ← ← ← ← ← ←262 ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓])264 (walk-thru-grass265 [↓ ↓ ↓ ↓ ↓])267 (walk268 [↓ ↓ ← ↓269 ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓270 → → → ↓])272 (walk-thru-grass273 [↓ ↓ ↓])275 (walk [↓ ← ← ↓ ↓ ↓ ↓ ↓ ↓])277 (walk-thru-grass278 [↓ ↓ ↓ ↓ ↓ ↓])280 (walk [↓ ↓ ↓ ← ↓ ↓ ↓281 ↓ ↓ ↓ ↓ ↓282 → → → ↑]))))284 (defn-memo deliver-oaks-parcel285 ([] (deliver-oaks-parcel286 (viridian-store->oaks-lab-like-a-boss)))287 ([script]288 (->> script289 (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-mart307 ([] (return-to-viridian-mart308 (deliver-oaks-parcel)))309 ([script]310 (->> script311 oaks-lab->pallet-town-edge312 (pallet-edge->viridian-mart false))))314 (defn-memo walk-to-counter315 ([] (walk-to-counter316 (return-to-viridian-mart)))317 ([script]318 (->> script319 (walk [↑ ↑ ←]))))323 ;; useful addresses324 52262 ;; --- current-cursor-offset325 52278 ;; --- current screen-offset328 (defn exp-item-list []329 (clojure.pprint/pprint330 (apply harmonic-compare331 (map read-state332 ["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 position341 ;; for all lists in the game (start list, pokemon list, shop342 ;; lists, inventory lists, battle list, basically343 ;; everything!)345 (def list-cursor-offset-address 52262)346 (def list-screen-offset-address 52278)348 (defn list-offset349 ([^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)))357 (defn exp-item-selection []358 (clojure.pprint/pprint359 (apply memory-compare360 (map read-state361 ["1-item"362 "2-items"363 "3-items"364 "4-items"365 ]))))367 (def item-quantity-selected-address 65432)369 (defn item-quantity-selected370 ([^SaveState state]371 (aget (memory state) item-quantity-selected-address))372 ([] (item-quantity-selected @current-state)))374 (defn set-cursor-relative375 "Assumes the arrow keys currently control the cursor.376 Moves the cursor n steps relative to its current377 position."378 [n script]379 (let [key (if (< 0 n) ↓ ↑)]380 (multiple-times381 (Math/abs n)382 (partial first-difference383 [] key list-offset)384 script)))386 (defn set-cursor387 "Assumes the arrow keys currently control the cursor. Sets388 the cursor to the desired position. Works for any menu389 that uses a cursor including the start menu, item menu,390 pokemon menu, and battle menu."391 [n [moves state :as script]]392 (let [current-position (list-offset state)393 difference (- n current-position)]394 (println difference)395 (set-cursor-relative difference script)))397 (defn buy-item398 "Assumes that the main item-screen is up, and buys399 quantity of the nth item in the list, assuming that you400 have enough money."401 [n quantity script]402 (if (= 0 quantity)403 script404 (let [after-initial-pause405 (do-nothing 20 script)406 move-to-item407 (reduce (fn [script _]408 (->> script409 (play-moves [[:d]])410 (do-nothing 3)))411 after-initial-pause412 (range n))413 select-item414 (play-moves [[:a]] move-to-item)415 request-items416 (reduce (fn [script _]417 (->> script418 (play-moves [[:u]])419 (do-nothing 1)))420 select-item421 (range (dec quantity)))422 buy-items423 (->> request-items424 (do-nothing 10)425 (play-moves [[:a]])426 (scroll-text)427 (scroll-text)428 (do-nothing 10)429 (play-moves [[:a]])430 (scroll-text))]431 buy-items)))434 (defn buy-items435 "Given a list of [item-no quantity], buys the quantity436 from the shop's list. Assumes that the item list is437 already up."438 [item-pairs script]439 (let [item-lookup (into {0 0 1 0 2 0 3 0 4 0} item-pairs)440 initial-purchase441 (->> script442 (buy-item 0 (item-lookup 0))443 (buy-item 1 (item-lookup 1))444 (buy-item 2 (item-lookup 2)))]445 (cond446 (and447 (not= 0 (item-lookup 3))448 (not= 0 (item-lookup 4)))449 (->> initial-purchase450 (do-nothing 20)451 (play-moves [[:d]])452 (do-nothing 3)453 (play-moves [[:d]])454 (do-nothing 3)455 (play-moves [[:d]])456 (do-nothing 10)457 (buy-item 0 (item-lookup 3))458 (do-nothing 20)459 (play-moves [[:d]])460 (do-nothing 3)461 (play-moves [[:d]])462 (do-nothing 3)463 (play-moves [[:d]])464 (do-nothing 10)465 (buy-item 0 (item-lookup 4)))466 (and (= 0 (item-lookup 3))467 (not= 0 (item-lookup 4)))468 (->> initial-purchase469 (do-nothing 20)470 (play-moves [[:d]])471 (do-nothing 3)472 (play-moves [[:d]])473 (do-nothing 3)474 (play-moves [[:d]])475 (do-nothing 10)476 (play-moves [[:d]])477 (do-nothing 10)478 (buy-item 0 (item-lookup 4)))479 (and (not= 0 (item-lookup 3))480 (= 0 (item-lookup 4)))481 (->> initial-purchase482 (do-nothing 20)483 (play-moves [[:d]])484 (do-nothing 3)485 (play-moves [[:d]])486 (do-nothing 3)487 (play-moves [[:d]])488 (do-nothing 10)489 (buy-item 0 (item-lookup 3)))490 (and (= 0 (item-lookup 3))491 (= 0 (item-lookup 4)))492 initial-purchase)))495 (defn test-buy-items496 ([] (test-buy-items497 (walk-to-counter)))498 ([script]499 (->> [(first script) (set-money (second script)500 999999)]501 (play-moves502 [[] [:a] []])503 (scroll-text)504 (do-nothing 100)505 (play-moves [[:a]])506 (do-nothing 100)507 (buy-items508 [[0 1]509 [1 15]510 [2 1]511 [3 20]512 [4 95]513 ]))))515 (defn-memo buy-initial-items516 ([] (buy-initial-items517 (walk-to-counter)))518 ([script]519 (->> script520 (play-moves521 [[] [:a] []])522 (scroll-text)523 (do-nothing 100)524 (play-moves [[:a]])525 (do-nothing 100)526 (buy-items527 [[0 1]528 [1 1]529 [2 1]530 [3 1]531 [4 1]])532 (do-nothing 100)533 (play-moves [[:b]])534 (do-nothing 100)535 (play-moves [[:b]])536 (do-nothing 100)537 (play-moves [[:b] []])538 (first-difference [:b] [:b :start] AF))))541 (defn-memo do-save-corruption542 ([] (do-save-corruption543 (buy-initial-items)))544 ([script]545 (->> script546 (first-difference [] [:d] AF)547 (play-moves [[] [] [] [:d]548 [] [] [] [:d]549 [] [] [] [:d]550 [] [] [:a]])551 scroll-text552 (play-moves553 ;; this section is copied from speedrun-2942 and corrupts554 ;; the save so that the total number of pokemon is set to555 ;; 0xFF, allowing manipulation of non-pokemon data in RAM556 ;; via the pokemon interface.557 [[] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] []558 [] [] [] [] [] [] [] [] [] [] [] [:select] [:restart]])559 (title)560 (first-difference [] [:start] AF)561 (first-difference [] [:a] AF)562 (first-difference [:a] [:a :start] AF))))564 (def menu do-nothing )566 (defn-memo corrupt-item-list567 ([] (corrupt-item-list568 (do-save-corruption)))569 ([script]570 (->> script571 (do-nothing 200)572 (menu [↓ [:a]]) ; select "POKEMON" from573 ; from main menu574 (menu [↓ ↓ ↓ ↓ ↓ ↓ ; go to 6th pokemon575 [:a] ↓ [:a] ; select "switch"576 ↓ ↓ ↓ [:a]]) ; switch with 9th "pokemon"578 (do-nothing 1))))580 (defn-memo get-burn-heals581 ([] (get-burn-heals582 (corrupt-item-list)))583 ([script]584 (->> script585 (menu [[:b] [:b]])586 (menu [[:a]])587 (do-nothing 100)588 (menu [↓ [:a]])589 (do-nothing 100)590 (menu [[:a] ↓ [:a]])591 (scroll-text)592 (menu [[:b][:b]])593 (menu [[:a]])595 (do-nothing 50)596 (buy-items [[0 1]])597 (do-nothing 60)598 (menu [[:a]])599 (scroll-text)601 (do-nothing 50)602 (buy-items [[0 1]])603 (do-nothing 60)604 ;;(menu [[:a]])605 ;;(scroll-text)607 ;;(do-nothing 300)608 ;;(menu [[:b] [:b]])609 ;;(do-nothing 300)611 (buy-items [[0 1]612 [1 1]613 [1 1]614 [2 1]615 [3 1]616 [4 97]])618 (do-nothing 10))))620 (defn-memo corrupt-item-list-again621 ([] (corrupt-item-list-again (get-burn-heals)))622 ([script]623 (->> script624 (do-nothing 10)625 (play-moves [[:b]])626 (do-nothing 100)627 (play-moves [[:b]])628 (do-nothing 40)629 (play-moves [[:b]])630 (first-difference [:b] [:start :b] AF)631 (menu [[:a] ↑ ↑ ↑ ↑ ↑ ; get fifth pokemon632 [:a] ↓ [:a] ; and corrupt the633 ↓ ↓ ↓ ↓ ↓ [:a]]) ; item list again by634 ; switching it to635 ))) ; tenth place.637 (defn-memo viridian-store->viridian-poke-center638 ([] (viridian-store->viridian-poke-center639 (corrupt-item-list-again)))640 ([script]641 (->> script642 (do-nothing 100)643 (play-moves [[:b]])644 (do-nothing 100)645 (play-moves [[:b]])646 (do-nothing 40)647 ;; leave store648 (walk [↓ ↓649 → ↓ ↓])650 (walk [← ← ← ←651 ↓ ↓ ↓ ↓ ↓ ↓652 ← ← ← ↑]))))654 (defn-memo to-poke-center-computer655 ([] (to-poke-center-computer656 (viridian-store->viridian-poke-center)))657 ([script]658 (->> script659 (walk [→ →660 ↑ ↑ ↑661 → → → → → → → → → ↑])662 (do-nothing 1))))664 (defn-memo begin-deposits665 ([] (begin-deposits666 (to-poke-center-computer)))667 ([script]668 (->> script669 ;; access PC670 (scroll-text 2)672 ;; access item storage673 (menu [[:a] [:d] [:a]])674 (scroll-text 2)676 ;; begin deposit677 (menu [[:d] [:a]])678 (do-nothing 40))))680 (defn deposit-n-items681 [n script]682 (->> script683 (do-nothing 100)684 (play-moves [[:a]])685 (do-nothing 80)686 (multiple-times687 (dec n)688 (fn [script]689 (->> script690 (play-moves [[:u]])691 (do-nothing 1))))692 (play-moves [[:a]])693 (scroll-text)))695 (defn deposit-one-item696 [script]697 (->> script698 (do-nothing 100)699 (play-moves [[:a]])700 (do-nothing 80)701 (play-moves [[:a]])702 (scroll-text)))704 (defn-memo create-header705 ([] (create-header (begin-deposits)))706 ([script]707 (->> script708 (multiple-times 33 deposit-one-item)709 (do-nothing 1))))711 (defn bootstrap-init []712 [(read-moves "bootstrap-init")713 (read-state "bootstrap-init")])715 (defn create-bootstrap-program716 ([] (create-bootstrap-program717 (create-header)))718 ([script]719 (->> script720 (do-nothing 120)721 (menu [↓ ↓ ↓ ↓ ↓ ↓ ↓])722 ;;(deposit-n-items 33)724 (menu (repeat 17 ↓))728 (do-nothing 1))))731 (defn test-pc-item-program []732 (-> (read-state "bootstrap-init")733 (set-memory pc-item-list-start 50)734 (set-memory-range735 map-function-address-start [0x8B 0xD5])736 (set-memory-range737 (inc pc-item-list-start)738 (flatten739 [(repeat740 28741 [0xFF 0x01])742 [;; second part of item manipulation program743 0x00 ;; this starts at address 0xD56C744 0x2A ;; save (HL)=(target) to A, increment HL746 0x00747 0x47 ;; save A to B749 0x00750 0x3A ;; save (target+1) to A, decrement HL752 0x00753 0x22 ;; A -> target, increment HL [(target+1) -> target]755 0x00756 0x70 ;; load B into target+1 [(target) -> target+1]758 0x00759 0xC3 ;; first part of absolute jump761 0x0C ;; return control to pokemon kernel762 0x5F]763 (repeat764 5765 [0xFF 0x01])767 [;; first part of item manipulation program768 0x00769 0x21 ;; load target into HL771 0x94 ;; this is the target address772 0xD5774 0x00 ;; relative jump back to first part775 0x18777 0xE1 ;; of program778 0x01780 0xFF ;; spacer781 0x01783 0x04 ;; target ID (pokeball)784 0x3E ;; target Quantity (lemonade)785 ]]))))