annotate clojure/com/aurellem/run/bootstrap_1.clj @ 352:82e3ba144773

wrote skeleton for celadon purchase spree.
author Robert McIntyre <rlm@mit.edu>
date Sun, 08 Apr 2012 21:21:39 -0500
parents a6a123af22f6
children a9e7507379e9
rev   line source
rlm@345 1 (ns com.aurellem.run.bootstrap-1
rlm@345 2 (:use (com.aurellem.gb saves gb-driver util
rlm@345 3 items vbm characters money))
rlm@345 4 (:use (com.aurellem.run util title save-corruption bootstrap-0))
rlm@345 5 (:use (com.aurellem.exp item-bridge))
rlm@345 6 (:import [com.aurellem.gb.gb_driver SaveState]))
rlm@345 7
rlm@345 8 (defn pc-item-writer-program
rlm@345 9 []
rlm@345 10 (let [limit 201
rlm@345 11 [target-high target-low] (disect-bytes-2 0xD162)]
rlm@345 12 (flatten
rlm@345 13 [[0x00 ;; (item-hack) set increment stack pointer no-op
rlm@345 14 0x1E ;; load limit into E
rlm@345 15 limit
rlm@345 16 0x3F ;; (item-hack) set carry flag no-op
rlm@345 17
rlm@345 18 ;; load 2 into C.
rlm@345 19 0x0E ;; C == 1 means input-first nybble
rlm@345 20 0x04 ;; C == 0 means input-second nybble
rlm@345 21
rlm@345 22 0x21 ;; load target into HL
rlm@345 23 target-low
rlm@345 24 target-high
rlm@345 25 0x37 ;; (item-hack) set carry flag no-op
rlm@345 26
rlm@345 27 0x00 ;; (item-hack) no-op
rlm@345 28 0x37 ;; (item-hack) set carry flag no-op
rlm@345 29
rlm@345 30 0x00 ;; (item-hack) no-op
rlm@345 31 0xF3 ;; disable interrupts
rlm@345 32 ;; Input Section
rlm@345 33
rlm@345 34 0x3E ;; load 0x20 into A, to measure buttons
rlm@345 35 0x10
rlm@345 36
rlm@345 37 0x00 ;; (item-hack) no-op
rlm@345 38 0xE0 ;; load A into [FF00]
rlm@345 39 0x00
rlm@345 40
rlm@345 41 0xF0 ;; load 0xFF00 into A to get
rlm@345 42 0x00 ;; button presses
rlm@345 43
rlm@345 44 0xE6
rlm@345 45 0x0F ;; select bottom four bits of A
rlm@345 46 0x37 ;; (item-hack) set carry flag no-op
rlm@345 47
rlm@345 48 0x00 ;; (item-hack) no-op
rlm@345 49 0xB8 ;; see if input is different (CP A B)
rlm@345 50
rlm@345 51 0x00 ;; (item-hack) (INC SP)
rlm@345 52 0x28 ;; repeat above steps if input is not different
rlm@345 53 ;; (jump relative backwards if B != A)
rlm@345 54 0xED ;; (literal -19) (item-hack) -19 == egg bomb (TM37)
rlm@345 55
rlm@345 56 0x47 ;; load A into B
rlm@345 57
rlm@345 58 0x0D ;; dec C
rlm@345 59 0x37 ;; (item-hack) set-carry flag
rlm@345 60 ;; branch based on C:
rlm@345 61 0x20 ;; JR NZ
rlm@345 62 23 ;; skip "input second nybble" and "jump to target" below
rlm@345 63
rlm@345 64 ;; input second nybble
rlm@345 65
rlm@345 66 0x0C ;; inc C
rlm@345 67 0x0C ;; inc C
rlm@345 68
rlm@345 69 0x00 ;; (item-hack) no-op
rlm@345 70 0xE6 ;; select bottom bits
rlm@345 71 0x0F
rlm@345 72 0x37 ;; (item-hack) set-carry flag no-op
rlm@345 73
rlm@345 74 0x00 ;; (item-hack) no-op
rlm@345 75 0xB2 ;; (OR A D) -> A
rlm@345 76
rlm@345 77 0x22 ;; (do (A -> (HL)) (INC HL))
rlm@345 78
rlm@345 79 0x1D ;; (DEC E)
rlm@345 80
rlm@345 81 0x00 ;; (item-hack)
rlm@345 82 0x20 ;; jump back to input section if not done
rlm@345 83 0xDA ;; literal -36 == TM 18 (counter)
rlm@345 84 0x01 ;; (item-hack) set BC to literal (no-op)
rlm@345 85
rlm@345 86 ;; jump to target
rlm@345 87 0x00 ;; (item-hack) these two bytes can be anything.
rlm@345 88 0x01
rlm@345 89
rlm@345 90 0x00 ;; (item-hack) no-op
rlm@345 91 0xBF ;; (CP A A) ensures Z
rlm@345 92
rlm@345 93 0xCA ;; (item-hack) jump if Z
rlm@345 94 target-low
rlm@345 95 target-high
rlm@345 96 0x01 ;; (item-hack) will never be reached.
rlm@345 97
rlm@345 98 ;; input first nybble
rlm@345 99 0x00
rlm@345 100 0xCB
rlm@345 101 0x37 ;; swap nybbles on A
rlm@345 102
rlm@345 103 0x57 ;; A -> D
rlm@345 104
rlm@345 105 0x37 ;; (item-hack) set carry flag no-op
rlm@345 106 0x18 ;; relative jump backwards
rlm@345 107 0xCD ;; literal -51 == TM05; go back to input section
rlm@345 108 0x01 ;; (item-hack) will never reach this instruction
rlm@345 109
rlm@345 110 ]
rlm@345 111 (repeat 8 [0xFF 0x01])
rlm@345 112
rlm@345 113 [;; jump to actual program
rlm@345 114 0x00
rlm@345 115 0x37 ;; (item-hack) set carry flag no-op
rlm@345 116
rlm@345 117 0x2E ;; 0x3A -> L
rlm@345 118 0x3A
rlm@345 119
rlm@345 120
rlm@345 121 0x00 ;; (item-hack) no-op
rlm@345 122 0x26 ;; 0xD5 -> L
rlm@345 123 0xD5
rlm@345 124 0x01 ;; (item-hack) set-carry BC
rlm@345 125
rlm@345 126 0x00 ;; (item-hack) these can be anything
rlm@345 127 0x01
rlm@345 128
rlm@345 129 0x00
rlm@345 130 0xE9 ;; jump to (HL)
rlm@345 131 ]])))
rlm@345 132
rlm@345 133 (defn view-desired-item-layout []
rlm@345 134 (clojure.pprint/pprint
rlm@345 135 (raw-inventory->inventory (pc-item-writer-program))))
rlm@345 136
rlm@345 137 (defn pc-item-writer-state []
rlm@345 138 (-> (read-state "bootstrap-init")
rlm@345 139 (set-memory pc-item-list-start 50)
rlm@345 140 (set-memory-range
rlm@345 141 map-function-address-start
rlm@345 142 [0x8B 0xD5])
rlm@345 143 (set-memory-range
rlm@345 144 (inc pc-item-list-start)
rlm@345 145 (pc-item-writer-program))))
rlm@345 146
rlm@345 147 (defn test-pc-item-writer []
rlm@345 148 (let [orig (read-state "pc-item-writer")]
rlm@345 149 (-> orig
rlm@345 150 (print-listing 0xD162 (+ 0xD162 20))
rlm@345 151 (run-moves (reduce concat
rlm@345 152 (repeat 10 [[:a :b :start :select] []])))
rlm@345 153 ((fn [_] (println "===========") _))
rlm@345 154 (print-listing 0xD162 (+ 0xD162 20)))))
rlm@345 155
rlm@345 156 (defn close-all-menus [[moves state :as script]]
rlm@345 157 (loop [s script]
rlm@345 158 (let [depth (current-depth (second (do-nothing 50 s)))]
rlm@345 159 (println "depth" depth)
rlm@345 160 (if (= depth 1)
rlm@345 161 s
rlm@345 162 (recur (close-menu s))))))
rlm@345 163
rlm@345 164 (defn-memo name-rival
rlm@345 165 ([] (name-rival (to-rival-name)))
rlm@345 166 ([script]
rlm@345 167 (->> script
rlm@345 168 (first-difference [] [:a] AF)
rlm@345 169 (first-difference [] [:r] DE)
rlm@345 170 (play-moves
rlm@345 171 [[]
rlm@345 172 [] [] [:r] [] [:d] [:a] ;; L
rlm@345 173 [:r] [] [:r] [] [:r] [] [:r] []
rlm@345 174 [:r] [] [:d] [] [:d] [:a] ;; [PK]
rlm@345 175 [:d] [] [:r] [:a]
rlm@345 176 ]))))
rlm@345 177
rlm@345 178 (defn-memo to-room-pc
rlm@345 179 ([] (to-room-pc (name-rival)))
rlm@345 180 ([script]
rlm@345 181 (->> script
rlm@345 182 finish-title
rlm@345 183 (walk [← ← ↑ ← ↑ ↑ ↑]))))
rlm@345 184
rlm@345 185 (defn-memo bootstrap-corrupt-save
rlm@345 186 ([] (bootstrap-corrupt-save (to-room-pc)))
rlm@345 187 ([script]
rlm@345 188 (->> script
rlm@345 189 (do-save-corruption 2)
rlm@345 190 (corrupt-item-list 0)
rlm@345 191 close-all-menus)))
rlm@345 192
rlm@345 193 (defn-memo begin-initial-deposits
rlm@345 194 ([] (begin-initial-deposits
rlm@345 195 (bootstrap-corrupt-save)))
rlm@345 196 ([script]
rlm@345 197 (->> script
rlm@345 198 (first-difference [] [:a] AF)
rlm@345 199 (scroll-text)
rlm@345 200 (set-cursor 1)
rlm@345 201 select-menu-entry)))
rlm@345 202
rlm@345 203 (defn deposit-held-item [n quantity [moves state :as script]]
rlm@345 204 (let [total-quantity (second (nth-item state n))]
rlm@345 205 (println "total-quantity" total-quantity)
rlm@345 206 (->> script
rlm@345 207 (set-cursor n)
rlm@345 208 (select-menu-entry 1)
rlm@345 209 ((fn [[moves state :as script]]
rlm@345 210 (if (not= (item-quantity-selected state) 1)
rlm@345 211 (repeat-until-different [] item-quantity-selected script)
rlm@345 212 script)))
rlm@345 213 (set-quantity total-quantity quantity)
rlm@345 214 (delayed-difference [] [:a] 100 #(search-string % "stored"))
rlm@345 215 (scroll-text))))
rlm@345 216
rlm@345 217 (defn widthdraw-pc-item [n quantity [moves state :as script]]
rlm@345 218 (let [total-quantity (second (nth-pc-item state n))]
rlm@345 219 (->> script
rlm@345 220 (set-cursor n)
rlm@345 221 (select-menu-entry 1)
rlm@345 222 ((fn [[moves state :as script]]
rlm@345 223 (if (not= (item-quantity-selected state) 1)
rlm@345 224 (repeat-until-different [] item-quantity-selected
rlm@345 225 script)
rlm@345 226 script)))
rlm@345 227 (set-quantity total-quantity quantity)
rlm@345 228 (delayed-difference [] [:a] 100 #(search-string % "Withdrew"))
rlm@345 229 (scroll-text))))
rlm@345 230
rlm@345 231 (defn toss-held-item [n quantity [moves state :as script]]
rlm@345 232 (let [total-quantity (second (nth-item state n))]
rlm@345 233 (->> script
rlm@345 234 (set-cursor n)
rlm@345 235 (select-menu-entry 1)
rlm@345 236 (set-cursor-relative 1)
rlm@345 237 (select-menu-entry -1)
rlm@345 238 ((fn [[moves state :as script]]
rlm@345 239 (if (not= (item-quantity-selected state) 1)
rlm@345 240 (repeat-until-different [] item-quantity-selected
rlm@345 241 script)
rlm@345 242 script)))
rlm@345 243 (set-quantity total-quantity quantity)
rlm@345 244 (play-moves [[:a]])
rlm@345 245 (scroll-text)
rlm@345 246 (delayed-difference [] [:a] 100 #(search-string % "Threw"))
rlm@345 247 (scroll-text)
rlm@345 248 )))
rlm@345 249
rlm@345 250
rlm@345 251 (def desired-zero-quantities
rlm@345 252 (map second (filter (comp (partial = 0) first)
rlm@345 253 (partition 2 (pc-item-writer-program)))))
rlm@345 254
rlm@345 255 (defn-memo initial-deposits
rlm@345 256 ([] (initial-deposits (begin-initial-deposits)))
rlm@345 257 ([script]
rlm@345 258 (->> script
rlm@345 259 (deposit-held-item 0 0xFE)
rlm@345 260 (deposit-held-item 1 0xFE)
rlm@345 261 ((fn [script]
rlm@345 262 (reduce
rlm@345 263 (fn [script item] (deposit-held-item item 0xFF script))
rlm@345 264 script
rlm@345 265 (range 2 (+ 12 2)))))
rlm@345 266 close-all-menus)))
rlm@345 267
rlm@345 268
rlm@345 269 (defn-memo prepare-celadon-warp
rlm@345 270 ([] (prepare-celadon-warp (initial-deposits)))
rlm@345 271 ([script]
rlm@345 272 (->> script
rlm@345 273 (activate-start-menu)
rlm@345 274 (set-cursor-relative 1)
rlm@345 275 (select-menu-entry)
rlm@345 276 (toss-held-item 35 0xFA)
rlm@345 277 (close-all-menus))))
rlm@345 278
rlm@345 279
rlm@345 280 ;;0 -- 256
rlm@345 281 ;;1 -- 254
rlm@345 282 ;;2 -- 254
rlm@345 283 ;;3 -- 255
rlm@345 284
rlm@345 285 (defn-memo restore-items
rlm@345 286 ([] (restore-items (prepare-celadon-warp)))
rlm@345 287 ([script]
rlm@345 288 (->> script
rlm@345 289 (first-difference [] [:a] AF)
rlm@345 290 (scroll-text)
rlm@345 291 (select-menu-entry)
rlm@345 292 (widthdraw-pc-item 0 1)
rlm@345 293 (widthdraw-pc-item 3 99)
rlm@345 294 (widthdraw-pc-item 3 99)
rlm@345 295 (widthdraw-pc-item 3 57)
rlm@345 296 (widthdraw-pc-item 2 99)
rlm@345 297 (widthdraw-pc-item 2 99)
rlm@345 298 (widthdraw-pc-item 2 56)
rlm@345 299 (widthdraw-pc-item 1 99)
rlm@345 300 (widthdraw-pc-item 1 99)
rlm@345 301 (widthdraw-pc-item 1 56)
rlm@345 302 (widthdraw-pc-item 0 99)
rlm@345 303 (widthdraw-pc-item 0 99)
rlm@345 304 (widthdraw-pc-item 0 57)
rlm@345 305 (close-all-menus))))
rlm@345 306
rlm@345 307 (defn-memo to-celadon
rlm@345 308 ([] (to-celadon (restore-items)))
rlm@345 309 ([script]
rlm@345 310 (->> script
rlm@345 311 (walk [→ → → → → → → ↑
rlm@345 312 ↓ ↓ ↓ ↓ ↓ ← ← ← ←
rlm@345 313 ↓ ↓]))))
rlm@345 314
rlm@345 315
rlm@351 316 ;; celadon store inventory
rlm@345 317
rlm@351 318 ;; Floor 2
rlm@351 319 ;;=====================================
rlm@351 320 ;; Great Ball TM32 (double-team)
rlm@351 321 ;; Super Potion TM33 (reflect)
rlm@351 322 ;; Revive TM02 (razor-wind)
rlm@351 323 ;; Super Repel TM07 (horn-drill)
rlm@351 324 ;; Antidote TM37 (egg-bomb)
rlm@351 325 ;; Burn Heal TM01 (mega-punch)
rlm@351 326 ;; Ice Heal TM05 (mega-kick)
rlm@351 327 ;; Awakening TM09 (take-down)
rlm@351 328 ;; Parlyz Heal TM17 (submission)
rlm@345 329
rlm@345 330
rlm@351 331 ;; Floor 3
rlm@351 332 ;;=====================================
rlm@351 333 ;; TM18 (counter)
rlm@345 334
rlm@351 335
rlm@351 336 ;; Floor 4
rlm@351 337 ;;=====================================
rlm@351 338 ;; Poke Doll
rlm@351 339 ;; Fire Stone
rlm@351 340 ;; Thunder Stone
rlm@351 341 ;; Water Stone
rlm@351 342 ;; Leaf Stone
rlm@351 343
rlm@351 344 ;; Floor 5
rlm@351 345 ;;=====================================
rlm@351 346 ;; X Accuracy HP UP
rlm@351 347 ;; Guard Spec. Protein
rlm@351 348 ;; Dire Hit Iron
rlm@351 349 ;; X Attack Carbos
rlm@351 350 ;; X Defend Calcium
rlm@351 351 ;; X Speed
rlm@351 352 ;; X Special
rlm@351 353
rlm@351 354 ;; Roof
rlm@351 355 ;;=====================================
rlm@351 356 ;; Fresh Water TM13 (ice-beam)
rlm@351 357 ;; Soda Pop TM48 (rock-slide)
rlm@352 358 ;; Lemonade :) TM49 (tri-attack)
rlm@352 359
rlm@352 360
rlm@352 361 (defn go-to-floor-two
rlm@352 362 ([] (go-to-floor-two (to-celadon)))
rlm@352 363 ([script]
rlm@352 364 (->> script
rlm@352 365 (walk [↑ → → → → → → → → → → →
rlm@352 366 ↑ ↑ ↑ ↑ ↑ ↑
rlm@352 367 ← ← ← ←
rlm@352 368 ↓ ↓ ↓
rlm@352 369 ← ←])
rlm@352 370 (first-difference [] ↑ AF))))
rlm@352 371
rlm@352 372 (defn get-money-floor-two
rlm@352 373 [])
rlm@352 374
rlm@352 375 (defn floor-two-TMs
rlm@352 376 [])
rlm@352 377
rlm@352 378 (defn floor-two-items
rlm@352 379 [])
rlm@352 380
rlm@352 381 (defn go-to-floor-three
rlm@352 382 [])
rlm@352 383
rlm@352 384 (defn get-TM18
rlm@352 385 [])
rlm@352 386
rlm@352 387 (defn go-to-floor-four
rlm@352 388 [])
rlm@352 389
rlm@352 390 (defn floor-four-items
rlm@352 391 [])
rlm@352 392
rlm@352 393 (defn go-to-floor-five
rlm@352 394 [])
rlm@352 395
rlm@352 396 (defn floor-five-items
rlm@352 397 [])
rlm@352 398
rlm@352 399 (defn go-to-roof
rlm@352 400 [])
rlm@352 401
rlm@352 402 (defn roof-drinks
rlm@352 403 [])
rlm@352 404
rlm@352 405 (defn get-TM13
rlm@352 406 [])
rlm@352 407
rlm@352 408