Mercurial > vba-clojure
view clojure/com/aurellem/run/bootstrap_1.clj @ 367:845b2d88bef9
script: item-program completely deposited. Will test tomorrow :)
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Mon, 09 Apr 2012 13:21:34 -0500 |
parents | 985e91060567 |
children | 08f8284e2f1b |
line wrap: on
line source
1 (ns com.aurellem.run.bootstrap-12 (:use (com.aurellem.gb saves gb-driver util3 items vbm characters money))4 (:use (com.aurellem.run util title save-corruption bootstrap-0))5 (:use (com.aurellem.exp item-bridge))6 (:import [com.aurellem.gb.gb_driver SaveState]))8 (defn pc-item-writer-program9 []10 (let [limit 20111 [target-high target-low] (disect-bytes-2 0xD162)]12 (flatten13 [[0x00 ;; (item-hack) set increment stack pointer no-op14 0x1E ;; load limit into E15 limit16 0x3F ;; (item-hack) set carry flag no-op18 ;; load 2 into C.19 0x0E ;; C == 1 means input-first nybble20 0x04 ;; C == 0 means input-second nybble22 0x21 ;; load target into HL23 target-low24 target-high25 0x37 ;; (item-hack) set carry flag no-op27 0x00 ;; (item-hack) no-op28 0x37 ;; (item-hack) set carry flag no-op30 0x00 ;; (item-hack) no-op31 0xF3 ;; disable interrupts32 ;; Input Section34 0x3E ;; load 0x20 into A, to measure buttons35 0x1037 0x00 ;; (item-hack) no-op38 0xE0 ;; load A into [FF00]39 0x0041 0xF0 ;; load 0xFF00 into A to get42 0x00 ;; button presses44 0xE645 0x0F ;; select bottom four bits of A46 0x37 ;; (item-hack) set carry flag no-op48 0x00 ;; (item-hack) no-op49 0xB8 ;; see if input is different (CP A B)51 0x00 ;; (item-hack) (INC SP)52 0x28 ;; repeat above steps if input is not different53 ;; (jump relative backwards if B != A)54 0xED ;; (literal -19) (item-hack) -19 == egg bomb (TM37)56 0x47 ;; load A into B58 0x0D ;; dec C59 0x37 ;; (item-hack) set-carry flag60 ;; branch based on C:61 0x20 ;; JR NZ62 23 ;; skip "input second nybble" and "jump to target" below64 ;; input second nybble66 0x0C ;; inc C67 0x0C ;; inc C69 0x00 ;; (item-hack) no-op70 0xE6 ;; select bottom bits71 0x0F72 0x37 ;; (item-hack) set-carry flag no-op74 0x00 ;; (item-hack) no-op75 0xB2 ;; (OR A D) -> A77 0x22 ;; (do (A -> (HL)) (INC HL))79 0x1D ;; (DEC E)81 0x00 ;; (item-hack)82 0x20 ;; jump back to input section if not done83 0xDA ;; literal -36 == TM 18 (counter)84 0x01 ;; (item-hack) set BC to literal (no-op)86 ;; jump to target87 0x00 ;; (item-hack) these two bytes can be anything.88 0x0190 0x00 ;; (item-hack) no-op91 0xBF ;; (CP A A) ensures Z93 0xCA ;; (item-hack) jump if Z94 target-low95 target-high96 0x01 ;; (item-hack) will never be reached.98 ;; input first nybble99 0x00100 0xCB101 0x37 ;; swap nybbles on A103 0x57 ;; A -> D105 0x37 ;; (item-hack) set carry flag no-op106 0x18 ;; relative jump backwards107 0xCD ;; literal -51 == TM05; go back to input section108 0x01 ;; (item-hack) will never reach this instruction110 ]111 (repeat 8 [0x00 0x01])113 [;; jump to actual program114 0x00115 0x37 ;; (item-hack) set carry flag no-op117 0x2E ;; 0x3A -> L118 0x3A121 0x00 ;; (item-hack) no-op122 0x26 ;; 0xD5 -> L123 0xD5124 0x01 ;; (item-hack) set-carry BC126 0x00 ;; (item-hack) these can be anything127 0x01129 0x00130 0xE9 ;; jump to (HL)131 ]])))133 (defn print-desired-item-layout []134 (clojure.pprint/pprint135 (raw-inventory->inventory (pc-item-writer-program))))137 (defn pc-item-writer-state []138 (-> (read-state "bootstrap-init")139 (set-memory pc-item-list-start 50)140 (set-memory-range141 map-function-address-start142 [0x8B 0xD5])143 (set-memory-range144 (inc pc-item-list-start)145 (pc-item-writer-program))))147 (defn test-pc-item-writer []148 (let [orig (read-state "pc-item-writer")]149 (-> orig150 (print-listing 0xD162 (+ 0xD162 20))151 (run-moves (reduce concat152 (repeat 10 [[:a :b :start :select] []])))153 ((fn [_] (println "===========") _))154 (print-listing 0xD162 (+ 0xD162 20)))))156 (defn close-all-menus [[moves state :as script]]157 (loop [s script]158 (let [depth (current-depth (second (do-nothing 50 s)))]159 (println "depth" depth)160 (if (= depth 1)161 s162 (recur (close-menu s))))))164 (defn-memo name-rival165 ([] (name-rival (to-rival-name)))166 ([script]167 (->> script168 (first-difference [] [:a] AF)169 (first-difference [] [:r] DE)170 (play-moves171 [[]172 [] [] [:r] [] [:d] [:a] ;; L173 [:r] [] [:r] [] [:r] [] [:r] []174 [:r] [] [:d] [] [:d] [:a] ;; [PK]175 [:d] [] [:r] [:a]176 ]))))178 (defn-memo to-room-pc179 ([] (to-room-pc (name-rival)))180 ([script]181 (->> script182 finish-title183 (walk [← ← ↑ ← ↑ ↑ ↑]))))185 (defn-memo bootstrap-corrupt-save186 ([] (bootstrap-corrupt-save (to-room-pc)))187 ([script]188 (->> script189 (do-save-corruption 2)190 (corrupt-item-list 0)191 close-all-menus)))193 (defn-memo begin-initial-deposits194 ([] (begin-initial-deposits195 (bootstrap-corrupt-save)))196 ([script]197 (->> script198 (first-difference [] [:a] AF)199 (scroll-text)200 (set-cursor 1)201 select-menu-entry)))203 (defn wait-for-quantity204 [[moves state :as script]]205 (if (not= (item-quantity-selected state) 1)206 (repeat-until-different [] item-quantity-selected script)207 script))209 (defn wait-for-cursor210 [[moves state :as script]]211 (if (not= (list-offset state) 0)212 (repeat-until-different [] list-offset script)213 script))215 (defn deposit-held-item [n quantity [moves state :as script]]216 (let [total-quantity (second (nth-item state n))]217 (println "total-quantity" total-quantity)218 (->> script219 (set-cursor n)220 (select-menu-entry 1)221 (wait-for-quantity)222 (set-quantity total-quantity quantity)223 (delayed-difference [] [:a] 100 #(search-string % "stored"))224 (scroll-text))))226 (defn sell-held-item [n quantity [moves state :as script]]227 (let [total-quantity (second (nth-item state n))]228 (->> script229 (wait-for-cursor) ;; when selling, the cursor always230 (set-cursor n) ;; returns to the top of the list.231 (select-menu-entry 1)232 (wait-for-quantity)233 (set-quantity total-quantity quantity)234 (delayed-difference [] [:a] 100 current-depth)235 (play-moves (repeat 20 [:b]))236 (delayed-difference [] [:a] 100 #(search-string % "What"))237 )))239 (defn widthdraw-pc-item [n quantity [moves state :as script]]240 (let [total-quantity (second (nth-pc-item state n))]241 (->> script242 (set-cursor n)243 (select-menu-entry 1)244 (wait-for-quantity)245 (set-quantity total-quantity quantity)246 (delayed-difference [] [:a] 100 #(search-string % "Withdrew"))247 (scroll-text))))249 (defn toss-held-item [n quantity [moves state :as script]]250 (let [total-quantity (second (nth-item state n))]251 (->> script252 (set-cursor n)253 (select-menu-entry 1)254 (set-cursor-relative 1)255 (select-menu-entry -1)256 (wait-for-quantity)257 (set-quantity total-quantity quantity)258 (play-moves [[:a]])259 (scroll-text)260 (delayed-difference [] [:a] 100 #(search-string % "Threw"))261 (scroll-text)262 )))264 (defn buy-item [n quantity [moves state :as script]]265 (->> script266 (set-cursor n)267 (purchase-item quantity)))270 (def desired-zero-quantities271 (map second (filter (comp (partial = 0) first)272 (partition 2 (pc-item-writer-program)))))274 (defn-memo initial-deposits275 ([] (initial-deposits (begin-initial-deposits)))276 ([script]277 (->> script278 (deposit-held-item 0 0x1)279 ((fn [script]280 (reduce281 (fn [script item] (deposit-held-item item 0xFF script))282 script283 (range 3 (+ 13 3)))))284 close-all-menus)))287 (defn-memo prepare-celadon-warp288 ([] (prepare-celadon-warp (initial-deposits)))289 ([script]290 (->> script291 (activate-start-menu)292 (set-cursor-relative 1)293 (select-menu-entry)294 (toss-held-item 35 0xFA)295 (close-all-menus))))298 ;;0 -- 256299 ;;1 -- 254300 ;;2 -- 254301 ;;3 -- 255303 (defn-memo restore-items304 ([] (restore-items (prepare-celadon-warp)))305 ([script]306 (->> script307 (first-difference [] [:a] AF)308 (scroll-text)309 (select-menu-entry)310 (widthdraw-pc-item 0 1)311 ;;(widthdraw-pc-item 0 99)312 ;;(widthdraw-pc-item 1 1)313 (widthdraw-pc-item 13 255)314 (close-all-menus))))316 (defn-memo to-celadon317 ([] (to-celadon (restore-items)))318 ([script]319 (->> script320 (walk [→ → → → → → → ↑321 ↓ ↓ ↓ ↓ ↓ ← ← ← ←322 ↓ ↓]))))325 ;; celadon store inventory327 ;; Floor 2328 ;;=====================================329 ;; Great Ball TM32 (double-team)330 ;; Super Potion TM33 (reflect)331 ;; Revive TM02 (razor-wind)332 ;; Super Repel TM07 (horn-drill)333 ;; Antidote TM37 (egg-bomb)334 ;; Burn Heal TM01 (mega-punch)335 ;; Ice Heal TM05 (mega-kick)336 ;; Awakening TM09 (take-down)337 ;; Parlyz Heal TM17 (submission)340 ;; Floor 3341 ;;=====================================342 ;; TM18 (counter)345 ;; Floor 4346 ;;=====================================347 ;; Poke Doll348 ;; Fire Stone349 ;; Thunder Stone350 ;; Water Stone351 ;; Leaf Stone353 ;; Floor 5354 ;;=====================================355 ;; X Accuracy HP UP356 ;; Guard Spec. Protein357 ;; Dire Hit Iron358 ;; X Attack Carbos359 ;; X Defend Calcium360 ;; X Speed361 ;; X Special363 ;; Roof364 ;;=====================================365 ;; Fresh Water TM13 (ice-beam)366 ;; Soda Pop TM48 (rock-slide)367 ;; Lemonade :) TM49 (tri-attack)370 (defn-memo go-to-floor-two371 ([] (go-to-floor-two (to-celadon)))372 ([script]373 (->> script374 (walk [↑ → → → → → → → → → → →375 ↑ ↑ ↑ ↑ ↑ ↑376 ← ← ← ←377 ↓ ↓ ↓378 ← ←])379 (first-difference [] ↑ AF))))381 (defn talk382 "Assumes that you are facing something that initiates text and383 causes it to do so."384 [script]385 (->> script386 (delayed-difference [] [:a] 100387 #(aget (memory %) text-address))))389 (defn-memo get-money-floor-two390 ([] (get-money-floor-two (go-to-floor-two)))391 ([script]392 (->> script393 talk394 (set-cursor 1)395 (select-menu-entry)396 (sell-held-item 0 1)397 (sell-held-item 0 1)398 (close-menu))))400 (defn-memo floor-two-TMs401 ([] (floor-two-TMs (get-money-floor-two)))402 ([script]403 (->> script404 (wait-for-cursor)405 (select-menu-entry)406 (buy-item 2 98) ;; TM02 (razor-wind)407 (buy-item 4 71) ;; TM37 (doubleteam)408 (buy-item 5 63) ;; TM01 (mega-punch)409 (buy-item 6 1) ;; TM05 (mega-kick)410 (buy-item 7 56) ;; TM09 (take-down)411 (close-menu))))413 (defn end-shop-conversation414 [script]415 (->> script416 (wait-until scroll-text [:b])417 (play-moves [[] [:b]])418 close-menu))420 (defn-memo floor-two-more-money421 ([] (floor-two-more-money (floor-two-TMs)))422 ([script]423 (->> script424 (wait-for-cursor)425 (set-cursor 1)426 (select-menu-entry)427 (sell-held-item 0 1)428 (sell-held-item 0 1)429 close-menu430 end-shop-conversation)))432 (defn turn [direction script]433 (->> script434 (first-difference [] direction AF)))436 (defn-memo floor-two-items437 ([] (floor-two-items (floor-two-more-money)))438 ([script]439 (->> script440 (walk [←])441 (turn ↑)442 talk443 select-menu-entry444 (buy-item 5 12) ;; burn heal445 (buy-item 6 55) ;; ice heal446 (buy-item 7 4) ;; awakening447 (buy-item 8 99) ;; parlyz heal448 (buy-item 8 55) ;; parlyz heal449 close-menu450 end-shop-conversation)))452 (defn-memo go-to-floor-three453 ([] (go-to-floor-three (floor-two-items)))454 ([script]455 (->> script456 (walk [→ → → → → → → → → → ↑ ↑ ↑457 → ↑]))))458 (defn-memo get-TM18459 ([] (get-TM18 (go-to-floor-three)))460 ([script]461 (->> script462 (walk [↓ ↓])463 talk464 (scroll-text 3)465 end-text)))467 (defn-memo go-to-floor-four468 ([] (go-to-floor-four (get-TM18)))469 ([script]470 (->> script471 (walk [← ← ← ← ↑ ↑472 ↓ ← ← ↓ ↓ ↓473 ← ← ← ← ←])474 (turn ↓))))476 (defn-memo floor-four-items477 ([] (floor-four-items (go-to-floor-four)))478 ([script]479 (->> script480 talk481 select-menu-entry482 (buy-item 1 23) ;; Fire Stone483 (buy-item 2 98) ;; Thunder Stone484 (buy-item 3 29) ;; Water Stone485 close-menu486 end-shop-conversation)))488 (defn-memo go-to-floor-five489 ([] (go-to-floor-five (floor-four-items)))490 ([script]491 (->> script492 (walk [→ → → → → →493 ↑ ↑ ↑494 → → → → → ↑ ;; leave floor four495 ↓ ← ← ← ← ← ← ← ←496 ↓ ↓ ↓ ← ← ← ]);; go to five's clerk497 (turn ↑))))499 (defn-memo floor-five-items500 ([] (floor-five-items (go-to-floor-five)))501 ([script]502 (->> script503 talk504 select-menu-entry505 (buy-item 0 58) ;; X-Accuracy506 (buy-item 1 99) ;; Guard Spec.507 (buy-item 1 24) ;; Guard Spec.508 close-menu509 end-shop-conversation)))511 (defn-memo go-to-roof512 ([] (go-to-roof (floor-five-items)))513 ([script]514 (->> script515 (walk [→ → → → ↑ ↑ ↑ → → → ↑ ;; leave floor five516 ↓ ← ← ←]) ;; walk to vending machine517 (turn ↑))))519 (defn buy-drink520 "Assumes you're in front of the vending machine. Buys the indicated521 drink."522 [n script]523 (->> script524 (do-nothing 20)525 (play-moves [[:a][:a]])526 scroll-text527 (wait-for-cursor)528 (set-cursor n)529 select-menu-entry530 close-menu))532 (defn-memo roof-drinks533 ([] (roof-drinks (go-to-roof)))534 ([script]535 (->> script536 (buy-drink 0) ;; fresh water (for TM13)537 ;; buy 16 lemonades538 ;; LEMONADE is the best item <3 :)539 (multiple-times 16 (partial buy-drink 2)))))541 (defn-memo get-TM13542 ([] (get-TM13 (roof-drinks)))543 ([script]544 (->> script545 (walk [← ← ← ← ← ← ↓])546 (play-moves [[][:a][:a][]])547 (scroll-text 3)548 select-menu-entry549 select-menu-entry550 (scroll-text 6)551 close-menu)))553 (defn-memo to-celadon-poke-center554 ([] (to-celadon-poke-center (get-TM13)))555 ([script]556 (->> script557 (walk [↑ → → → → → → → → → ↑]) ; leave roof558 (walk [↓ ← ← ← ← ↓ ↓ ↓ ← ← ← ← ←559 ↑ ↑ ↑ ← ← ↑]) ; to elevator561 (walk [→ → ↑ ↑]) ; to controls562 talk563 select-menu-entry ; to floor 1564 (walk [↓ ↓ ← ←])565 (walk [↓ → ↓ ↓ ↓ ↓ ↓ ↓]) ; leave store566 (walk [↓ → → → → → → → → → → ↑ ↑])567 (walk (repeat 23 →))568 (walk [↑ ↑ ↑ ↑]) ; enter poke center569 (walk [↑ ↑ ↑ → → → → → → → → → →]) ; to computer570 (turn ↑))))572 (defn activate-rlm-pc [script]573 (->> script574 talk575 scroll-text576 wait-for-cursor577 (set-cursor 1)578 select-menu-entry579 (scroll-text 2)))581 (defn begin-deposit [script]582 (->> script583 (set-cursor 1)584 select-menu-entry))586 (defn begin-withdraw [script]587 (->> script588 (set-cursor 0)589 (select-menu-entry)))591 (defn deposit-held-item-named592 [item-name quantity [moves state :as script]]593 (let [index (count594 (take-while595 (fn [[name quant]]596 (or (not= name item-name)597 (< quant quantity)))598 (inventory state)))]599 (println "index" index)600 (deposit-held-item index quantity script)))603 (defn-memo begin-hacking604 ([] (begin-hacking(to-celadon-poke-center)))605 ([script]606 (->> script607 activate-rlm-pc608 begin-deposit609 (deposit-held-item-named 0x00 30)610 (deposit-held-item-named :TM01 63)611 (deposit-held-item-named :awakening 4)612 (deposit-held-item-named :thunderstone 98)613 (deposit-held-item-named :TM09 55)614 (deposit-held-item-named 0x00 55))))616 (defn open-held-items617 [script]618 (->> script619 select-menu-entry))621 (defn to-held-items622 [script]623 (->> script624 close-menu625 close-menu626 end-text;;; grr628 activate-start-menu629 open-held-items))631 (defn-memo hacking-2632 ([] (hacking-2 (begin-hacking)))633 ([script]634 (->> script635 (to-held-items)636 (toss-held-item 0 166) ;; discard cruft637 close-menu638 close-menu)))640 (defn-memo hacking-3641 ([] (hacking-3 (hacking-2)))642 ([script]643 (->> script644 activate-rlm-pc645 begin-withdraw646 (widthdraw-pc-item 0 99)647 (widthdraw-pc-item 0 1)648 (widthdraw-pc-item 2 0xFE)649 (widthdraw-pc-item 3 0xFE))))651 (defn-memo hacking-4652 ([] (hacking-4 (hacking-3)))653 ([script]654 (->> script655 close-menu656 begin-deposit657 (deposit-held-item 19 243)658 (deposit-held-item-named :lemonade 16)659 (deposit-held-item 18 224))))661 (defn-memo hacking-5662 "clean out the held-item list again"663 ([] (hacking-5 (hacking-4)))664 ([script]665 (->> script666 (to-held-items)667 (toss-held-item 18 30)668 (toss-held-item 17 1)669 close-menu670 close-menu)))672 (defn-memo hacking-6673 ([] (hacking-6 (hacking-5)))674 ([script]675 (->> script676 activate-rlm-pc677 begin-withdraw678 (widthdraw-pc-item 4 0xFE)679 (widthdraw-pc-item 5 0xFE)680 (widthdraw-pc-item 6 0xFE)681 close-menu)))683 (defn-memo hacking-7684 ([] (hacking-7 (hacking-6)))685 ([script]686 (->> script687 begin-deposit688 (deposit-held-item 19 240)689 (deposit-held-item 18 230)690 (deposit-held-item-named :parlyz-heal 55)691 (deposit-held-item 17 184)692 (deposit-held-item 17 40)693 (deposit-held-item-named :TM37 71)694 (deposit-held-item-named :ice-heal 55)695 (deposit-held-item-named :fire-stone 23)696 (deposit-held-item-named :burn-heal 12)697 close-menu)))699 (defn combine-items [idx-1 script]700 (->> script701 (wait-until select-menu-entry)702 (set-cursor idx-1)703 (wait-until select-menu-entry)704 (play-moves [[][:select][]])705 (set-cursor-relative 1)706 (delayed-difference [] [:select] 100707 #(nth-pc-item % (list-offset %)))))709 (defn-memo hacking-8710 "Clear cruft away from held item list."711 ([] (hacking-8 (hacking-7)))712 ([script]713 (->> script714 to-held-items715 (toss-held-item 15 1)716 (toss-held-item 14 1)717 (toss-held-item 13 1)718 close-menu719 close-menu)))721 (defn-memo hacking-9722 ([] (hacking-9 (hacking-8)))723 ([script]724 (->> script725 activate-rlm-pc726 begin-withdraw727 (widthdraw-pc-item 7 0xFE)728 (widthdraw-pc-item 8 0xFC)729 (widthdraw-pc-item 8 1)730 (widthdraw-pc-item 8 1)731 (widthdraw-pc-item 9 0xFE)732 (multiple-times733 7734 (partial combine-items 2))735 close-menu)))737 (defn-memo hacking-10738 ([] (hacking-10 (hacking-9)))739 ([script]740 (->> script741 begin-deposit742 (deposit-held-item 17 230)743 (deposit-held-item-named :parlyz-heal 55)744 (deposit-held-item 14 178)745 (deposit-held-item-named :water-stone 29)746 (deposit-held-item 14 32)747 (deposit-held-item-named :TM18 1)748 (deposit-held-item 13 1)749 (deposit-held-item 13 191)750 (deposit-held-item-named :TM02 98)751 (deposit-held-item-named :TM09 1)752 close-menu753 close-menu)))755 (defn-memo hacking-11756 ([] (hacking-11 (hacking-10)))757 ([script]758 (->> script759 begin-withdraw760 (widthdraw-pc-item 3 0xFE)761 (widthdraw-pc-item 4 0xFE)762 (widthdraw-pc-item 5 1)763 (widthdraw-pc-item 5 1)764 (widthdraw-pc-item 5 1)765 (widthdraw-pc-item 5 0xFB)766 (multiple-times767 3768 (partial combine-items 2))769 close-menu)))771 (defn-memo hacking-12772 ([] (hacking-12 (hacking-11)))773 ([script]774 (->> script775 begin-deposit776 (deposit-held-item 18 203)777 (deposit-held-item-named :guard-spec 87)778 (deposit-held-item-named :guard-spec 24)779 (deposit-held-item-named :TM05 1)780 (multiple-times781 8782 (partial deposit-held-item 14 2))783 (deposit-held-item 14 55)784 (deposit-held-item-named :x-accuracy 58)785 (deposit-held-item 14 38)786 (deposit-held-item-named :TM13 1)787 (deposit-held-item 13 1)788 (deposit-held-item 13 233)789 close-menu790 close-menu)))793 (defn toss-pc-item [n quantity [moves state :as script]]794 (let [total-quantity (second (nth-pc-item state n))]795 (->> script796 (set-cursor n)797 (select-menu-entry 1)798 (wait-for-quantity)799 (set-quantity total-quantity quantity)800 (delayed-difference [] [:a] 100 #(search-string % "Is"))801 (scroll-text)802 select-menu-entry803 (scroll-text))))805 (defn hacking-13806 ([] (hacking-13 (hacking-12)))807 ([script]808 (->> script809 (set-cursor-relative 1)810 (select-menu-entry)811 (toss-pc-item 1 1)812 (toss-pc-item 0 156)813 (toss-pc-item 0 11)814 close-menu)))