annotate clojure/com/aurellem/run/bootstrap_1.clj @ 353:a9e7507379e9

adjusting run to use 0x00 items for money since they are worth a half million each.
author Robert McIntyre <rlm@mit.edu>
date Mon, 09 Apr 2012 05:36:56 -0500
parents 82e3ba144773
children 4141489d1406
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@353 203 (defn wait-for-quantity
rlm@353 204 [[moves state :as script]]
rlm@353 205 (if (not= (item-quantity-selected state) 1)
rlm@353 206 (repeat-until-different [] item-quantity-selected script)
rlm@353 207 script))
rlm@353 208
rlm@353 209 (defn wait-for-cursor
rlm@353 210 [[moves state :as script]]
rlm@353 211 (if (not= (list-offset state) 0)
rlm@353 212 (repeat-until-different [] list-offset script)
rlm@353 213 script))
rlm@353 214
rlm@345 215 (defn deposit-held-item [n quantity [moves state :as script]]
rlm@345 216 (let [total-quantity (second (nth-item state n))]
rlm@345 217 (println "total-quantity" total-quantity)
rlm@345 218 (->> script
rlm@345 219 (set-cursor n)
rlm@345 220 (select-menu-entry 1)
rlm@353 221 (wait-for-quantity)
rlm@345 222 (set-quantity total-quantity quantity)
rlm@345 223 (delayed-difference [] [:a] 100 #(search-string % "stored"))
rlm@345 224 (scroll-text))))
rlm@345 225
rlm@353 226 (defn sell-held-item [n quantity [moves state :as script]]
rlm@353 227 (let [total-quantity (second (nth-item state n))]
rlm@353 228 (->> script
rlm@353 229 (wait-for-cursor) ;; when selling, the cursor always
rlm@353 230 (set-cursor n) ;; returns to the top of the list.
rlm@353 231 (select-menu-entry 1)
rlm@353 232 (wait-for-quantity)
rlm@353 233 (set-quantity total-quantity quantity)
rlm@353 234 (delayed-difference [] [:a] 100 current-depth)
rlm@353 235 (play-moves (repeat 20 [:b]))
rlm@353 236 (delayed-difference [] [:a] 100 #(search-string % "What"))
rlm@353 237 )))
rlm@353 238
rlm@345 239 (defn widthdraw-pc-item [n quantity [moves state :as script]]
rlm@345 240 (let [total-quantity (second (nth-pc-item state n))]
rlm@345 241 (->> script
rlm@345 242 (set-cursor n)
rlm@345 243 (select-menu-entry 1)
rlm@353 244 (wait-for-quantity)
rlm@345 245 (set-quantity total-quantity quantity)
rlm@345 246 (delayed-difference [] [:a] 100 #(search-string % "Withdrew"))
rlm@345 247 (scroll-text))))
rlm@345 248
rlm@345 249 (defn toss-held-item [n quantity [moves state :as script]]
rlm@353 250 (let [total-quantity (second (nth-item state n))]
rlm@345 251 (->> script
rlm@345 252 (set-cursor n)
rlm@345 253 (select-menu-entry 1)
rlm@345 254 (set-cursor-relative 1)
rlm@345 255 (select-menu-entry -1)
rlm@353 256 (wait-for-quantity)
rlm@345 257 (set-quantity total-quantity quantity)
rlm@345 258 (play-moves [[:a]])
rlm@345 259 (scroll-text)
rlm@345 260 (delayed-difference [] [:a] 100 #(search-string % "Threw"))
rlm@345 261 (scroll-text)
rlm@345 262 )))
rlm@345 263
rlm@345 264
rlm@345 265 (def desired-zero-quantities
rlm@345 266 (map second (filter (comp (partial = 0) first)
rlm@345 267 (partition 2 (pc-item-writer-program)))))
rlm@345 268
rlm@345 269 (defn-memo initial-deposits
rlm@345 270 ([] (initial-deposits (begin-initial-deposits)))
rlm@345 271 ([script]
rlm@345 272 (->> script
rlm@353 273 (deposit-held-item 1 0x2)
rlm@345 274 ((fn [script]
rlm@345 275 (reduce
rlm@345 276 (fn [script item] (deposit-held-item item 0xFF script))
rlm@345 277 script
rlm@353 278 (range 3 (+ 12 3)))))
rlm@345 279 close-all-menus)))
rlm@345 280
rlm@345 281
rlm@345 282 (defn-memo prepare-celadon-warp
rlm@345 283 ([] (prepare-celadon-warp (initial-deposits)))
rlm@345 284 ([script]
rlm@345 285 (->> script
rlm@345 286 (activate-start-menu)
rlm@345 287 (set-cursor-relative 1)
rlm@345 288 (select-menu-entry)
rlm@345 289 (toss-held-item 35 0xFA)
rlm@345 290 (close-all-menus))))
rlm@345 291
rlm@345 292
rlm@345 293 ;;0 -- 256
rlm@345 294 ;;1 -- 254
rlm@345 295 ;;2 -- 254
rlm@345 296 ;;3 -- 255
rlm@345 297
rlm@345 298 (defn-memo restore-items
rlm@345 299 ([] (restore-items (prepare-celadon-warp)))
rlm@345 300 ([script]
rlm@345 301 (->> script
rlm@345 302 (first-difference [] [:a] AF)
rlm@345 303 (scroll-text)
rlm@345 304 (select-menu-entry)
rlm@345 305 (widthdraw-pc-item 0 1)
rlm@345 306 (widthdraw-pc-item 0 99)
rlm@353 307 (widthdraw-pc-item 1 1)
rlm@353 308 (widthdraw-pc-item 2 5)
rlm@353 309 ;; (widthdraw-pc-item 3 99)
rlm@353 310 ;; (widthdraw-pc-item 3 99)
rlm@353 311 ;; (widthdraw-pc-item 3 57) ;; take all
rlm@353 312 ;; (widthdraw-pc-item 2 99)
rlm@353 313 ;; (widthdraw-pc-item 2 99)
rlm@353 314 ;; (widthdraw-pc-item 2 56) ;; take all
rlm@353 315 ;; (widthdraw-pc-item 1 99)
rlm@353 316 ;; (widthdraw-pc-item 1 99)
rlm@353 317 ;; (widthdraw-pc-item 1 55) ;; leave one
rlm@353 318 ;; (widthdraw-pc-item 0 99)
rlm@353 319 ;; (widthdraw-pc-item 0 99)
rlm@353 320 ;; (widthdraw-pc-item 0 57) ;; leave one
rlm@345 321 (close-all-menus))))
rlm@345 322
rlm@345 323 (defn-memo to-celadon
rlm@345 324 ([] (to-celadon (restore-items)))
rlm@345 325 ([script]
rlm@345 326 (->> script
rlm@345 327 (walk [→ → → → → → → ↑
rlm@345 328 ↓ ↓ ↓ ↓ ↓ ← ← ← ←
rlm@345 329 ↓ ↓]))))
rlm@345 330
rlm@345 331
rlm@351 332 ;; celadon store inventory
rlm@345 333
rlm@351 334 ;; Floor 2
rlm@351 335 ;;=====================================
rlm@351 336 ;; Great Ball TM32 (double-team)
rlm@351 337 ;; Super Potion TM33 (reflect)
rlm@351 338 ;; Revive TM02 (razor-wind)
rlm@351 339 ;; Super Repel TM07 (horn-drill)
rlm@351 340 ;; Antidote TM37 (egg-bomb)
rlm@351 341 ;; Burn Heal TM01 (mega-punch)
rlm@351 342 ;; Ice Heal TM05 (mega-kick)
rlm@351 343 ;; Awakening TM09 (take-down)
rlm@351 344 ;; Parlyz Heal TM17 (submission)
rlm@345 345
rlm@345 346
rlm@351 347 ;; Floor 3
rlm@351 348 ;;=====================================
rlm@351 349 ;; TM18 (counter)
rlm@345 350
rlm@351 351
rlm@351 352 ;; Floor 4
rlm@351 353 ;;=====================================
rlm@351 354 ;; Poke Doll
rlm@351 355 ;; Fire Stone
rlm@351 356 ;; Thunder Stone
rlm@351 357 ;; Water Stone
rlm@351 358 ;; Leaf Stone
rlm@351 359
rlm@351 360 ;; Floor 5
rlm@351 361 ;;=====================================
rlm@351 362 ;; X Accuracy HP UP
rlm@351 363 ;; Guard Spec. Protein
rlm@351 364 ;; Dire Hit Iron
rlm@351 365 ;; X Attack Carbos
rlm@351 366 ;; X Defend Calcium
rlm@351 367 ;; X Speed
rlm@351 368 ;; X Special
rlm@351 369
rlm@351 370 ;; Roof
rlm@351 371 ;;=====================================
rlm@351 372 ;; Fresh Water TM13 (ice-beam)
rlm@351 373 ;; Soda Pop TM48 (rock-slide)
rlm@352 374 ;; Lemonade :) TM49 (tri-attack)
rlm@352 375
rlm@352 376
rlm@353 377 (defn-memo go-to-floor-two
rlm@352 378 ([] (go-to-floor-two (to-celadon)))
rlm@352 379 ([script]
rlm@352 380 (->> script
rlm@352 381 (walk [↑ → → → → → → → → → → →
rlm@352 382 ↑ ↑ ↑ ↑ ↑ ↑
rlm@352 383 ← ← ← ←
rlm@352 384 ↓ ↓ ↓
rlm@352 385 ← ←])
rlm@352 386 (first-difference [] ↑ AF))))
rlm@352 387
rlm@353 388 (defn talk
rlm@353 389 "Assumes that you are facing a person and engages in conversation"
rlm@353 390 [[moves state :as script]]
rlm@353 391 (delayed-difference [] [:a] 100
rlm@353 392 #(aget (memory %) text-address)
rlm@353 393 script))
rlm@353 394
rlm@353 395
rlm@352 396 (defn get-money-floor-two
rlm@353 397 ([] (get-money-floor-two (go-to-floor-two)))
rlm@353 398 ([script]
rlm@353 399 (->> script
rlm@353 400 talk
rlm@353 401 (set-cursor 1)
rlm@353 402 (select-menu-entry)
rlm@353 403 (sell-held-item 2 1)
rlm@353 404 (sell-held-item 2 1)
rlm@353 405
rlm@353 406 ;; (sell-held-item 11)
rlm@353 407 ;; (sell-held-item 10)
rlm@353 408 ;; (sell-held-item 9)
rlm@353 409 ;; (sell-held-item 8)
rlm@353 410 ;; (sell-held-item 7)
rlm@353 411 ;; (sell-held-item 6)
rlm@353 412 (do-nothing 40))))
rlm@353 413
rlm@353 414
rlm@353 415
rlm@352 416
rlm@352 417 (defn floor-two-TMs
rlm@352 418 [])
rlm@352 419
rlm@352 420 (defn floor-two-items
rlm@352 421 [])
rlm@352 422
rlm@352 423 (defn go-to-floor-three
rlm@352 424 [])
rlm@352 425
rlm@352 426 (defn get-TM18
rlm@352 427 [])
rlm@352 428
rlm@352 429 (defn go-to-floor-four
rlm@352 430 [])
rlm@352 431
rlm@352 432 (defn floor-four-items
rlm@352 433 [])
rlm@352 434
rlm@352 435 (defn go-to-floor-five
rlm@352 436 [])
rlm@352 437
rlm@352 438 (defn floor-five-items
rlm@352 439 [])
rlm@352 440
rlm@352 441 (defn go-to-roof
rlm@352 442 [])
rlm@352 443
rlm@352 444 (defn roof-drinks
rlm@352 445 [])
rlm@352 446
rlm@352 447 (defn get-TM13
rlm@352 448 [])
rlm@352 449
rlm@352 450