diff clojure/com/aurellem/run/bootstrap_1.clj @ 345:3b3cd62b6106

script: made it to cereulean with TM55s worth 2 million.
author Robert McIntyre <rlm@mit.edu>
date Sun, 08 Apr 2012 20:58:14 -0500
parents
children a6a123af22f6
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/clojure/com/aurellem/run/bootstrap_1.clj	Sun Apr 08 20:58:14 2012 -0500
     1.3 @@ -0,0 +1,321 @@
     1.4 +(ns com.aurellem.run.bootstrap-1
     1.5 +  (:use (com.aurellem.gb saves gb-driver util
     1.6 +                         items vbm characters money))
     1.7 +  (:use (com.aurellem.run util title save-corruption bootstrap-0))
     1.8 +  (:use (com.aurellem.exp item-bridge))
     1.9 +  (:import [com.aurellem.gb.gb_driver SaveState]))
    1.10 +
    1.11 +(defn pc-item-writer-program
    1.12 +  []
    1.13 +  (let [limit 201
    1.14 +        [target-high target-low] (disect-bytes-2 0xD162)]
    1.15 +    (flatten
    1.16 +     [[0x00  ;; (item-hack) set increment stack pointer no-op
    1.17 +       0x1E  ;; load limit into E
    1.18 +       limit
    1.19 +       0x3F  ;; (item-hack) set carry flag no-op
    1.20 +
    1.21 +       ;; load 2 into C.
    1.22 +       0x0E   ;; C == 1 means input-first nybble
    1.23 +       0x04   ;; C == 0 means input-second nybble
    1.24 +
    1.25 +       0x21 ;; load target into HL
    1.26 +       target-low
    1.27 +       target-high
    1.28 +       0x37 ;; (item-hack) set carry flag no-op
    1.29 +
    1.30 +       0x00 ;; (item-hack) no-op
    1.31 +       0x37 ;; (item-hack) set carry flag no-op
    1.32 +       
    1.33 +       0x00 ;; (item-hack) no-op
    1.34 +       0xF3 ;; disable interrupts
    1.35 +       ;; Input Section
    1.36 +
    1.37 +       0x3E ;; load 0x20 into A, to measure buttons
    1.38 +       0x10 
    1.39 +
    1.40 +       0x00 ;; (item-hack) no-op
    1.41 +       0xE0 ;; load A into [FF00]
    1.42 +       0x00
    1.43 +
    1.44 +       0xF0 ;; load 0xFF00 into A to get
    1.45 +       0x00 ;; button presses
    1.46 +       
    1.47 +       0xE6
    1.48 +       0x0F ;; select bottom four bits of A
    1.49 +       0x37 ;; (item-hack) set carry flag no-op
    1.50 +
    1.51 +       0x00 ;; (item-hack) no-op
    1.52 +       0xB8 ;; see if input is different (CP A B)
    1.53 +
    1.54 +       0x00 ;; (item-hack) (INC SP)
    1.55 +       0x28 ;; repeat above steps if input is not different
    1.56 +       ;; (jump relative backwards if B != A)
    1.57 +       0xED ;; (literal -19) (item-hack) -19 == egg bomb (TM37)
    1.58 +
    1.59 +       0x47 ;; load A into B
    1.60 +       
    1.61 +       0x0D ;; dec C
    1.62 +       0x37 ;; (item-hack) set-carry flag
    1.63 +       ;; branch based on C:
    1.64 +       0x20 ;; JR NZ
    1.65 +       23 ;; skip "input second nybble" and "jump to target" below
    1.66 +       
    1.67 +       ;; input second nybble
    1.68 +
    1.69 +       0x0C ;; inc C
    1.70 +       0x0C ;; inc C
    1.71 +
    1.72 +       0x00 ;; (item-hack) no-op
    1.73 +       0xE6 ;; select bottom bits
    1.74 +       0x0F
    1.75 +       0x37 ;; (item-hack) set-carry flag no-op
    1.76 +
    1.77 +       0x00 ;; (item-hack) no-op
    1.78 +       0xB2 ;; (OR A D) -> A
    1.79 +
    1.80 +       0x22 ;; (do (A -> (HL)) (INC HL))
    1.81 +
    1.82 +       0x1D ;; (DEC E)
    1.83 +
    1.84 +       0x00 ;; (item-hack) 
    1.85 +       0x20 ;; jump back to input section if not done
    1.86 +       0xDA ;; literal -36 == TM 18 (counter)
    1.87 +       0x01 ;; (item-hack) set BC to literal (no-op)
    1.88 +
    1.89 +       ;; jump to target
    1.90 +       0x00  ;; (item-hack) these two bytes can be anything.
    1.91 +       0x01 
    1.92 +
    1.93 +       0x00   ;; (item-hack) no-op
    1.94 +       0xBF   ;; (CP A A) ensures Z
    1.95 +       
    1.96 +       0xCA   ;; (item-hack) jump if Z
    1.97 +       target-low
    1.98 +       target-high
    1.99 +       0x01   ;; (item-hack) will never be reached.
   1.100 +       
   1.101 +       ;; input first nybble
   1.102 +       0x00
   1.103 +       0xCB
   1.104 +       0x37  ;; swap nybbles on A
   1.105 +
   1.106 +       0x57  ;; A -> D
   1.107 +
   1.108 +       0x37  ;; (item-hack) set carry flag no-op
   1.109 +       0x18  ;; relative jump backwards
   1.110 +       0xCD  ;; literal -51 == TM05; go back to input section
   1.111 +       0x01  ;; (item-hack) will never reach this instruction
   1.112 +
   1.113 +       ]
   1.114 +      (repeat 8 [0xFF 0x01])
   1.115 +
   1.116 +      [;; jump to actual program
   1.117 +       0x00
   1.118 +       0x37  ;; (item-hack) set carry flag no-op
   1.119 +
   1.120 +       0x2E  ;; 0x3A -> L
   1.121 +       0x3A
   1.122 +
   1.123 +
   1.124 +       0x00  ;; (item-hack) no-op
   1.125 +       0x26  ;; 0xD5 -> L
   1.126 +       0xD5  
   1.127 +       0x01  ;; (item-hack) set-carry BC
   1.128 +
   1.129 +       0x00  ;; (item-hack) these can be anything
   1.130 +       0x01  
   1.131 +
   1.132 +       0x00
   1.133 +       0xE9 ;; jump to (HL)
   1.134 +       ]])))
   1.135 +
   1.136 +(defn view-desired-item-layout []
   1.137 +  (clojure.pprint/pprint
   1.138 +   (raw-inventory->inventory (pc-item-writer-program))))
   1.139 +
   1.140 +(defn pc-item-writer-state []
   1.141 +    (-> (read-state "bootstrap-init")
   1.142 +      (set-memory pc-item-list-start 50)
   1.143 +      (set-memory-range
   1.144 +       map-function-address-start
   1.145 +       [0x8B 0xD5])
   1.146 +      (set-memory-range
   1.147 +       (inc pc-item-list-start)
   1.148 +       (pc-item-writer-program))))
   1.149 +
   1.150 +(defn test-pc-item-writer []
   1.151 +  (let [orig (read-state "pc-item-writer")]
   1.152 +    (-> orig
   1.153 +    (print-listing 0xD162 (+ 0xD162 20))
   1.154 +    (run-moves (reduce concat
   1.155 +                (repeat 10 [[:a :b :start :select] []])))
   1.156 +    ((fn [_] (println "===========") _))
   1.157 +    (print-listing 0xD162 (+ 0xD162 20)))))
   1.158 +
   1.159 +(defn close-all-menus [[moves state :as script]]
   1.160 +  (loop [s script]
   1.161 +    (let [depth (current-depth (second (do-nothing 50 s)))]
   1.162 +      (println "depth" depth)
   1.163 +      (if (= depth 1)
   1.164 +        s
   1.165 +        (recur (close-menu s))))))
   1.166 +
   1.167 +(defn-memo name-rival
   1.168 +  ([] (name-rival (to-rival-name)))
   1.169 +  ([script]
   1.170 +     (->> script
   1.171 +          (first-difference [] [:a] AF)
   1.172 +          (first-difference [] [:r] DE)
   1.173 +          (play-moves
   1.174 +           [[]
   1.175 +            [] [] [:r] [] [:d] [:a]           ;; L
   1.176 +            [:r] [] [:r] [] [:r] [] [:r] [] 
   1.177 +            [:r] [] [:d] [] [:d] [:a]         ;; [PK]
   1.178 +            [:d] [] [:r] [:a]
   1.179 +            ]))))
   1.180 +
   1.181 +(defn-memo to-room-pc
   1.182 +  ([] (to-room-pc (name-rival)))
   1.183 +  ([script]
   1.184 +     (->> script
   1.185 +          finish-title
   1.186 +          (walk [← ← ↑ ← ↑ ↑ ↑]))))
   1.187 +
   1.188 +(defn-memo bootstrap-corrupt-save
   1.189 +  ([] (bootstrap-corrupt-save (to-room-pc)))
   1.190 +  ([script]
   1.191 +   (->> script
   1.192 +        (do-save-corruption 2)
   1.193 +        (corrupt-item-list 0)
   1.194 +        close-all-menus)))
   1.195 +
   1.196 +(defn-memo begin-initial-deposits
   1.197 +  ([] (begin-initial-deposits
   1.198 +       (bootstrap-corrupt-save)))
   1.199 +  ([script]
   1.200 +     (->> script
   1.201 +          (first-difference [] [:a] AF)
   1.202 +          (scroll-text)
   1.203 +          (set-cursor 1)
   1.204 +          select-menu-entry)))
   1.205 +
   1.206 +(defn deposit-held-item [n quantity [moves state :as script]]
   1.207 +  (let [total-quantity (second (nth-item state n))]
   1.208 +    (println "total-quantity" total-quantity)
   1.209 +    (->> script
   1.210 +         (set-cursor n)
   1.211 +         (select-menu-entry 1)
   1.212 +         ((fn [[moves state :as script]]
   1.213 +            (if (not= (item-quantity-selected state) 1)
   1.214 +              (repeat-until-different [] item-quantity-selected script)
   1.215 +              script)))
   1.216 +         (set-quantity total-quantity quantity)
   1.217 +         (delayed-difference [] [:a] 100 #(search-string % "stored"))
   1.218 +         (scroll-text))))
   1.219 +
   1.220 +(defn widthdraw-pc-item [n quantity [moves state :as script]]
   1.221 +  (let [total-quantity (second (nth-pc-item state n))]
   1.222 +    (->> script
   1.223 +         (set-cursor n)
   1.224 +         (select-menu-entry 1)
   1.225 +         ((fn [[moves state :as script]]
   1.226 +            (if (not= (item-quantity-selected state) 1)
   1.227 +              (repeat-until-different [] item-quantity-selected
   1.228 +                                      script)
   1.229 +              script)))
   1.230 +         (set-quantity total-quantity quantity)
   1.231 +         (delayed-difference [] [:a] 100 #(search-string % "Withdrew"))
   1.232 +         (scroll-text))))
   1.233 +
   1.234 +(defn toss-held-item [n quantity [moves state :as script]]
   1.235 +   (let [total-quantity (second (nth-item state n))]
   1.236 +    (->> script
   1.237 +         (set-cursor n)
   1.238 +         (select-menu-entry 1)
   1.239 +         (set-cursor-relative 1)
   1.240 +         (select-menu-entry -1)
   1.241 +         ((fn [[moves state :as script]]
   1.242 +            (if (not= (item-quantity-selected state) 1)
   1.243 +              (repeat-until-different [] item-quantity-selected
   1.244 +                                      script)
   1.245 +              script)))
   1.246 +         (set-quantity total-quantity quantity)
   1.247 +         (play-moves [[:a]])
   1.248 +         (scroll-text)
   1.249 +         (delayed-difference [] [:a] 100 #(search-string % "Threw"))
   1.250 +         (scroll-text)
   1.251 +         )))
   1.252 +
   1.253 +
   1.254 +(def desired-zero-quantities
   1.255 +  (map second (filter (comp (partial = 0) first)
   1.256 +                      (partition 2 (pc-item-writer-program)))))
   1.257 +  
   1.258 +(defn-memo initial-deposits
   1.259 +  ([] (initial-deposits (begin-initial-deposits)))
   1.260 +  ([script]
   1.261 +     (->> script
   1.262 +          (deposit-held-item 0 0xFE)
   1.263 +          (deposit-held-item 1 0xFE)
   1.264 +          ((fn [script]
   1.265 +             (reduce 
   1.266 +              (fn [script item] (deposit-held-item item 0xFF script))
   1.267 +              script
   1.268 +              (range 2 (+ 12 2)))))
   1.269 +          close-all-menus)))
   1.270 +
   1.271 +
   1.272 +(defn-memo prepare-celadon-warp
   1.273 +  ([] (prepare-celadon-warp (initial-deposits)))
   1.274 +  ([script]
   1.275 +     (->> script
   1.276 +          (activate-start-menu)
   1.277 +          (set-cursor-relative 1)
   1.278 +          (select-menu-entry)
   1.279 +          (toss-held-item 35 0xFA)
   1.280 +          (close-all-menus))))
   1.281 +
   1.282 +
   1.283 +;;0 -- 256
   1.284 +;;1 -- 254
   1.285 +;;2 -- 254
   1.286 +;;3 -- 255
   1.287 +
   1.288 +(defn-memo restore-items
   1.289 +  ([] (restore-items (prepare-celadon-warp)))
   1.290 +  ([script]
   1.291 +     (->> script
   1.292 +          (first-difference [] [:a] AF)
   1.293 +          (scroll-text)
   1.294 +          (select-menu-entry)
   1.295 +          (widthdraw-pc-item 0 1)
   1.296 +          (widthdraw-pc-item 3 99)
   1.297 +          (widthdraw-pc-item 3 99)
   1.298 +          (widthdraw-pc-item 3 57)
   1.299 +          (widthdraw-pc-item 2 99)
   1.300 +          (widthdraw-pc-item 2 99)
   1.301 +          (widthdraw-pc-item 2 56)
   1.302 +          (widthdraw-pc-item 1 99)
   1.303 +          (widthdraw-pc-item 1 99)
   1.304 +          (widthdraw-pc-item 1 56)
   1.305 +          (widthdraw-pc-item 0 99)
   1.306 +          (widthdraw-pc-item 0 99)
   1.307 +          (widthdraw-pc-item 0 57)
   1.308 +          (close-all-menus))))
   1.309 +
   1.310 +(defn-memo to-celadon
   1.311 +  ([] (to-celadon (restore-items)))
   1.312 +  ([script]
   1.313 +     (->> script
   1.314 +          (walk [→ → → → → → → ↑
   1.315 +                 ↓ ↓ ↓ ↓ ↓ ← ← ← ←
   1.316 +                 ↓ ↓]))))
   1.317 +
   1.318 +
   1.319 +
   1.320 +
   1.321 +
   1.322 +          
   1.323 +        
   1.324 +