rlm@345: (ns com.aurellem.run.bootstrap-1 rlm@345: (:use (com.aurellem.gb saves gb-driver util rlm@345: items vbm characters money)) rlm@345: (:use (com.aurellem.run util title save-corruption bootstrap-0)) rlm@345: (:use (com.aurellem.exp item-bridge)) rlm@345: (:import [com.aurellem.gb.gb_driver SaveState])) rlm@345: rlm@345: (defn pc-item-writer-program rlm@345: [] rlm@345: (let [limit 201 rlm@345: [target-high target-low] (disect-bytes-2 0xD162)] rlm@345: (flatten rlm@345: [[0x00 ;; (item-hack) set increment stack pointer no-op rlm@345: 0x1E ;; load limit into E rlm@345: limit rlm@345: 0x3F ;; (item-hack) set carry flag no-op rlm@345: rlm@345: ;; load 2 into C. rlm@345: 0x0E ;; C == 1 means input-first nybble rlm@345: 0x04 ;; C == 0 means input-second nybble rlm@345: rlm@345: 0x21 ;; load target into HL rlm@345: target-low rlm@345: target-high rlm@345: 0x37 ;; (item-hack) set carry flag no-op rlm@345: rlm@345: 0x00 ;; (item-hack) no-op rlm@345: 0x37 ;; (item-hack) set carry flag no-op rlm@345: rlm@345: 0x00 ;; (item-hack) no-op rlm@345: 0xF3 ;; disable interrupts rlm@345: ;; Input Section rlm@345: rlm@345: 0x3E ;; load 0x20 into A, to measure buttons rlm@345: 0x10 rlm@345: rlm@345: 0x00 ;; (item-hack) no-op rlm@345: 0xE0 ;; load A into [FF00] rlm@345: 0x00 rlm@345: rlm@345: 0xF0 ;; load 0xFF00 into A to get rlm@345: 0x00 ;; button presses rlm@345: rlm@345: 0xE6 rlm@345: 0x0F ;; select bottom four bits of A rlm@345: 0x37 ;; (item-hack) set carry flag no-op rlm@345: rlm@345: 0x00 ;; (item-hack) no-op rlm@345: 0xB8 ;; see if input is different (CP A B) rlm@345: rlm@345: 0x00 ;; (item-hack) (INC SP) rlm@345: 0x28 ;; repeat above steps if input is not different rlm@345: ;; (jump relative backwards if B != A) rlm@345: 0xED ;; (literal -19) (item-hack) -19 == egg bomb (TM37) rlm@345: rlm@345: 0x47 ;; load A into B rlm@345: rlm@345: 0x0D ;; dec C rlm@345: 0x37 ;; (item-hack) set-carry flag rlm@345: ;; branch based on C: rlm@345: 0x20 ;; JR NZ rlm@345: 23 ;; skip "input second nybble" and "jump to target" below rlm@345: rlm@345: ;; input second nybble rlm@345: rlm@345: 0x0C ;; inc C rlm@345: 0x0C ;; inc C rlm@345: rlm@345: 0x00 ;; (item-hack) no-op rlm@345: 0xE6 ;; select bottom bits rlm@345: 0x0F rlm@345: 0x37 ;; (item-hack) set-carry flag no-op rlm@345: rlm@345: 0x00 ;; (item-hack) no-op rlm@345: 0xB2 ;; (OR A D) -> A rlm@345: rlm@345: 0x22 ;; (do (A -> (HL)) (INC HL)) rlm@345: rlm@345: 0x1D ;; (DEC E) rlm@345: rlm@345: 0x00 ;; (item-hack) rlm@345: 0x20 ;; jump back to input section if not done rlm@345: 0xDA ;; literal -36 == TM 18 (counter) rlm@345: 0x01 ;; (item-hack) set BC to literal (no-op) rlm@345: rlm@345: ;; jump to target rlm@345: 0x00 ;; (item-hack) these two bytes can be anything. rlm@345: 0x01 rlm@345: rlm@345: 0x00 ;; (item-hack) no-op rlm@345: 0xBF ;; (CP A A) ensures Z rlm@345: rlm@345: 0xCA ;; (item-hack) jump if Z rlm@345: target-low rlm@345: target-high rlm@345: 0x01 ;; (item-hack) will never be reached. rlm@345: rlm@345: ;; input first nybble rlm@345: 0x00 rlm@345: 0xCB rlm@345: 0x37 ;; swap nybbles on A rlm@345: rlm@345: 0x57 ;; A -> D rlm@345: rlm@345: 0x37 ;; (item-hack) set carry flag no-op rlm@345: 0x18 ;; relative jump backwards rlm@345: 0xCD ;; literal -51 == TM05; go back to input section rlm@345: 0x01 ;; (item-hack) will never reach this instruction rlm@345: rlm@345: ] rlm@345: (repeat 8 [0xFF 0x01]) rlm@345: rlm@345: [;; jump to actual program rlm@345: 0x00 rlm@345: 0x37 ;; (item-hack) set carry flag no-op rlm@345: rlm@345: 0x2E ;; 0x3A -> L rlm@345: 0x3A rlm@345: rlm@345: rlm@345: 0x00 ;; (item-hack) no-op rlm@345: 0x26 ;; 0xD5 -> L rlm@345: 0xD5 rlm@345: 0x01 ;; (item-hack) set-carry BC rlm@345: rlm@345: 0x00 ;; (item-hack) these can be anything rlm@345: 0x01 rlm@345: rlm@345: 0x00 rlm@345: 0xE9 ;; jump to (HL) rlm@345: ]]))) rlm@345: rlm@345: (defn view-desired-item-layout [] rlm@345: (clojure.pprint/pprint rlm@345: (raw-inventory->inventory (pc-item-writer-program)))) rlm@345: rlm@345: (defn pc-item-writer-state [] rlm@345: (-> (read-state "bootstrap-init") rlm@345: (set-memory pc-item-list-start 50) rlm@345: (set-memory-range rlm@345: map-function-address-start rlm@345: [0x8B 0xD5]) rlm@345: (set-memory-range rlm@345: (inc pc-item-list-start) rlm@345: (pc-item-writer-program)))) rlm@345: rlm@345: (defn test-pc-item-writer [] rlm@345: (let [orig (read-state "pc-item-writer")] rlm@345: (-> orig rlm@345: (print-listing 0xD162 (+ 0xD162 20)) rlm@345: (run-moves (reduce concat rlm@345: (repeat 10 [[:a :b :start :select] []]))) rlm@345: ((fn [_] (println "===========") _)) rlm@345: (print-listing 0xD162 (+ 0xD162 20))))) rlm@345: rlm@345: (defn close-all-menus [[moves state :as script]] rlm@345: (loop [s script] rlm@345: (let [depth (current-depth (second (do-nothing 50 s)))] rlm@345: (println "depth" depth) rlm@345: (if (= depth 1) rlm@345: s rlm@345: (recur (close-menu s)))))) rlm@345: rlm@345: (defn-memo name-rival rlm@345: ([] (name-rival (to-rival-name))) rlm@345: ([script] rlm@345: (->> script rlm@345: (first-difference [] [:a] AF) rlm@345: (first-difference [] [:r] DE) rlm@345: (play-moves rlm@345: [[] rlm@345: [] [] [:r] [] [:d] [:a] ;; L rlm@345: [:r] [] [:r] [] [:r] [] [:r] [] rlm@345: [:r] [] [:d] [] [:d] [:a] ;; [PK] rlm@345: [:d] [] [:r] [:a] rlm@345: ])))) rlm@345: rlm@345: (defn-memo to-room-pc rlm@345: ([] (to-room-pc (name-rival))) rlm@345: ([script] rlm@345: (->> script rlm@345: finish-title rlm@345: (walk [← ← ↑ ← ↑ ↑ ↑])))) rlm@345: rlm@345: (defn-memo bootstrap-corrupt-save rlm@345: ([] (bootstrap-corrupt-save (to-room-pc))) rlm@345: ([script] rlm@345: (->> script rlm@345: (do-save-corruption 2) rlm@345: (corrupt-item-list 0) rlm@345: close-all-menus))) rlm@345: rlm@345: (defn-memo begin-initial-deposits rlm@345: ([] (begin-initial-deposits rlm@345: (bootstrap-corrupt-save))) rlm@345: ([script] rlm@345: (->> script rlm@345: (first-difference [] [:a] AF) rlm@345: (scroll-text) rlm@345: (set-cursor 1) rlm@345: select-menu-entry))) rlm@345: rlm@353: (defn wait-for-quantity rlm@353: [[moves state :as script]] rlm@353: (if (not= (item-quantity-selected state) 1) rlm@353: (repeat-until-different [] item-quantity-selected script) rlm@353: script)) rlm@353: rlm@353: (defn wait-for-cursor rlm@353: [[moves state :as script]] rlm@353: (if (not= (list-offset state) 0) rlm@353: (repeat-until-different [] list-offset script) rlm@353: script)) rlm@353: rlm@345: (defn deposit-held-item [n quantity [moves state :as script]] rlm@345: (let [total-quantity (second (nth-item state n))] rlm@345: (println "total-quantity" total-quantity) rlm@345: (->> script rlm@345: (set-cursor n) rlm@345: (select-menu-entry 1) rlm@353: (wait-for-quantity) rlm@345: (set-quantity total-quantity quantity) rlm@345: (delayed-difference [] [:a] 100 #(search-string % "stored")) rlm@345: (scroll-text)))) rlm@345: rlm@353: (defn sell-held-item [n quantity [moves state :as script]] rlm@353: (let [total-quantity (second (nth-item state n))] rlm@353: (->> script rlm@353: (wait-for-cursor) ;; when selling, the cursor always rlm@353: (set-cursor n) ;; returns to the top of the list. rlm@353: (select-menu-entry 1) rlm@353: (wait-for-quantity) rlm@353: (set-quantity total-quantity quantity) rlm@353: (delayed-difference [] [:a] 100 current-depth) rlm@353: (play-moves (repeat 20 [:b])) rlm@353: (delayed-difference [] [:a] 100 #(search-string % "What")) rlm@353: ))) rlm@353: rlm@345: (defn widthdraw-pc-item [n quantity [moves state :as script]] rlm@345: (let [total-quantity (second (nth-pc-item state n))] rlm@345: (->> script rlm@345: (set-cursor n) rlm@345: (select-menu-entry 1) rlm@353: (wait-for-quantity) rlm@345: (set-quantity total-quantity quantity) rlm@345: (delayed-difference [] [:a] 100 #(search-string % "Withdrew")) rlm@345: (scroll-text)))) rlm@345: rlm@345: (defn toss-held-item [n quantity [moves state :as script]] rlm@353: (let [total-quantity (second (nth-item state n))] rlm@345: (->> script rlm@345: (set-cursor n) rlm@345: (select-menu-entry 1) rlm@345: (set-cursor-relative 1) rlm@345: (select-menu-entry -1) rlm@353: (wait-for-quantity) rlm@345: (set-quantity total-quantity quantity) rlm@345: (play-moves [[:a]]) rlm@345: (scroll-text) rlm@345: (delayed-difference [] [:a] 100 #(search-string % "Threw")) rlm@345: (scroll-text) rlm@345: ))) rlm@345: rlm@354: (defn buy-item [n quantity [moves state :as script]] rlm@354: (->> script rlm@354: (set-cursor n) rlm@354: (purchase-item quantity))) rlm@354: rlm@345: rlm@345: (def desired-zero-quantities rlm@345: (map second (filter (comp (partial = 0) first) rlm@345: (partition 2 (pc-item-writer-program))))) rlm@345: rlm@345: (defn-memo initial-deposits rlm@345: ([] (initial-deposits (begin-initial-deposits))) rlm@345: ([script] rlm@345: (->> script rlm@354: (deposit-held-item 0 0x1) rlm@345: ((fn [script] rlm@345: (reduce rlm@345: (fn [script item] (deposit-held-item item 0xFF script)) rlm@345: script rlm@353: (range 3 (+ 12 3))))) rlm@345: close-all-menus))) rlm@345: rlm@345: rlm@345: (defn-memo prepare-celadon-warp rlm@345: ([] (prepare-celadon-warp (initial-deposits))) rlm@345: ([script] rlm@345: (->> script rlm@345: (activate-start-menu) rlm@345: (set-cursor-relative 1) rlm@345: (select-menu-entry) rlm@345: (toss-held-item 35 0xFA) rlm@345: (close-all-menus)))) rlm@345: rlm@345: rlm@345: ;;0 -- 256 rlm@345: ;;1 -- 254 rlm@345: ;;2 -- 254 rlm@345: ;;3 -- 255 rlm@345: rlm@345: (defn-memo restore-items rlm@345: ([] (restore-items (prepare-celadon-warp))) rlm@345: ([script] rlm@345: (->> script rlm@345: (first-difference [] [:a] AF) rlm@345: (scroll-text) rlm@345: (select-menu-entry) rlm@345: (widthdraw-pc-item 0 1) rlm@354: ;;(widthdraw-pc-item 0 99) rlm@354: ;;(widthdraw-pc-item 1 1) rlm@353: (widthdraw-pc-item 2 5) rlm@345: (close-all-menus)))) rlm@345: rlm@345: (defn-memo to-celadon rlm@345: ([] (to-celadon (restore-items))) rlm@345: ([script] rlm@345: (->> script rlm@345: (walk [→ → → → → → → ↑ rlm@345: ↓ ↓ ↓ ↓ ↓ ← ← ← ← rlm@345: ↓ ↓])))) rlm@345: rlm@345: rlm@351: ;; celadon store inventory rlm@345: rlm@351: ;; Floor 2 rlm@351: ;;===================================== rlm@351: ;; Great Ball TM32 (double-team) rlm@351: ;; Super Potion TM33 (reflect) rlm@351: ;; Revive TM02 (razor-wind) rlm@351: ;; Super Repel TM07 (horn-drill) rlm@351: ;; Antidote TM37 (egg-bomb) rlm@351: ;; Burn Heal TM01 (mega-punch) rlm@351: ;; Ice Heal TM05 (mega-kick) rlm@351: ;; Awakening TM09 (take-down) rlm@351: ;; Parlyz Heal TM17 (submission) rlm@345: rlm@345: rlm@351: ;; Floor 3 rlm@351: ;;===================================== rlm@351: ;; TM18 (counter) rlm@345: rlm@351: rlm@351: ;; Floor 4 rlm@351: ;;===================================== rlm@351: ;; Poke Doll rlm@351: ;; Fire Stone rlm@351: ;; Thunder Stone rlm@351: ;; Water Stone rlm@351: ;; Leaf Stone rlm@351: rlm@351: ;; Floor 5 rlm@351: ;;===================================== rlm@351: ;; X Accuracy HP UP rlm@351: ;; Guard Spec. Protein rlm@351: ;; Dire Hit Iron rlm@351: ;; X Attack Carbos rlm@351: ;; X Defend Calcium rlm@351: ;; X Speed rlm@351: ;; X Special rlm@351: rlm@351: ;; Roof rlm@351: ;;===================================== rlm@351: ;; Fresh Water TM13 (ice-beam) rlm@351: ;; Soda Pop TM48 (rock-slide) rlm@352: ;; Lemonade :) TM49 (tri-attack) rlm@352: rlm@352: rlm@353: (defn-memo go-to-floor-two rlm@352: ([] (go-to-floor-two (to-celadon))) rlm@352: ([script] rlm@352: (->> script rlm@352: (walk [↑ → → → → → → → → → → → rlm@352: ↑ ↑ ↑ ↑ ↑ ↑ rlm@352: ← ← ← ← rlm@352: ↓ ↓ ↓ rlm@352: ← ←]) rlm@352: (first-difference [] ↑ AF)))) rlm@352: rlm@353: (defn talk rlm@353: "Assumes that you are facing a person and engages in conversation" rlm@353: [[moves state :as script]] rlm@353: (delayed-difference [] [:a] 100 rlm@353: #(aget (memory %) text-address) rlm@353: script)) rlm@353: rlm@353: rlm@354: (defn-memo get-money-floor-two rlm@353: ([] (get-money-floor-two (go-to-floor-two))) rlm@353: ([script] rlm@353: (->> script rlm@353: talk rlm@353: (set-cursor 1) rlm@353: (select-menu-entry) rlm@354: (sell-held-item 0 1) rlm@354: (sell-held-item 0 1) rlm@354: (close-menu)))) rlm@352: rlm@354: (defn-memo floor-two-TMs rlm@354: ([] (floor-two-TMs (get-money-floor-two))) rlm@354: ([script] rlm@354: (->> script rlm@354: (wait-for-cursor) rlm@354: (select-menu-entry) rlm@354: (buy-item 2 98) ;; TM02 (razor-wind) rlm@354: (buy-item 4 71) ;; TM37 (doubleteam) rlm@354: (buy-item 5 63) ;; TM01 (mega-punch) rlm@354: (buy-item 6 1) ;; TM05 (mega-kick) rlm@354: (buy-item 7 56) ;; TM09 (take-down) rlm@354: (close-menu)))) rlm@354: rlm@354: (defn end-shop-conversation rlm@354: [script] rlm@354: (->> script rlm@354: (wait-until scroll-text [:b]) rlm@354: (play-moves [[] [:b]]) rlm@354: close-menu)) rlm@354: rlm@354: (defn-memo floor-two-more-money rlm@354: ([] (floor-two-more-money (floor-two-TMs))) rlm@354: ([script] rlm@354: (->> script rlm@354: (wait-for-cursor) rlm@354: (set-cursor 1) rlm@354: (select-menu-entry) rlm@354: (sell-held-item 0 1) rlm@354: (sell-held-item 0 1) rlm@354: close-menu rlm@354: end-shop-conversation))) rlm@354: rlm@354: (defn turn [direction script] rlm@354: (->> script rlm@354: (first-difference [] direction AF))) rlm@352: rlm@352: (defn floor-two-items rlm@352: []) rlm@354: rlm@352: rlm@352: (defn go-to-floor-three rlm@352: []) rlm@352: rlm@352: (defn get-TM18 rlm@352: []) rlm@352: rlm@352: (defn go-to-floor-four rlm@352: []) rlm@352: rlm@352: (defn floor-four-items rlm@352: []) rlm@352: rlm@352: (defn go-to-floor-five rlm@352: []) rlm@352: rlm@352: (defn floor-five-items rlm@352: []) rlm@352: rlm@352: (defn go-to-roof rlm@352: []) rlm@352: rlm@352: (defn roof-drinks rlm@352: []) rlm@352: rlm@352: (defn get-TM13 rlm@352: []) rlm@352: rlm@352: