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