view clojure/com/aurellem/exp/pokemon.clj @ 162:34b518fc7404

moved important move code out of exp and into gb.
author Robert McIntyre <rlm@mit.edu>
date Tue, 20 Mar 2012 17:18:22 -0500
parents cd7979aa544e
children 76b031d2f586
line wrap: on
line source
1 (ns com.aurellem.exp.pokemon
2 "Here I find out how pokemon are stored in memory."
3 (:use (com.aurellem.gb gb-driver items assembly util
4 characters))
5 (:import java.io.File)
6 (:import [com.aurellem.gb.gb_driver SaveState]))
9 (def pidgeot-lvl-36 (mid-game))
12 (def pidgeot-lvl-37 (read-state "pidgeot-lvl-37"))
15 (def pidgeot-lvl-38 (read-state "pidgeot-lvl-38"))
18 (def pidgeot-lvl-39 (read-state "pidgeot-lvl-39"))
21 (def pidgeot-lvl-40 (read-state "pidgeot-lvl-40"))
24 (defn level-analysis []
25 (apply common-differences
26 (map (comp vec memory)
27 [pidgeot-lvl-36
28 pidgeot-lvl-37
29 pidgeot-lvl-38
30 pidgeot-lvl-39
31 pidgeot-lvl-40])))
33 ;; inconclusive -- implies that level is calculated from
34 ;; some other values.
37 (def name-pidgeotto (read-state "name-pidgeotto"))
38 (def named-A (read-state "named-A"))
39 (def named-B (read-state "named-B"))
40 (def named-C (read-state "named-C"))
41 (def named-D (read-state "named-D"))
42 (def named-E (read-state "named-E"))
43 (def named-F (read-state "named-F"))
45 (defn name-analysis []
46 (apply common-differences
47 (map (comp vec memory)
48 [named-A
49 named-B
50 named-C
51 named-D
52 named-E
53 named-F])))
55 ;; resluted in 3 separate locations that could
56 ;; possibly hold the first letter of the pokemon's name
58 0xCF4A
59 0xD2EB
60 0xCEED
62 ;; try changing each of them
65 (defn test-cf4a []
66 (continue!
67 (set-memory named-A 0xCF4A (character->character-code "Z"))))
68 ;; result -- pidgeotto named "A"
70 (defn test-d2eb []
71 (continue!
72 (set-memory named-A 0xD2EB (character->character-code "Z"))))
73 ;; result -- pidgeotto named "Z"
75 (defn test-ceed []
76 (continue!
77 (set-memory named-A 0xCEED (character->character-code "Z"))))
78 ;; result -- pidgeotto named "A"
80 (def sixth-pokemon-name-start 0xD2EB)
83 (defn set-sixth-pokemon-name-first-character
84 ([state character]
85 (set-memory state sixth-pokemon-name-start
86 (character->character-code character)))
87 ([character]
88 (set-sixth-pokemon-name-first-character @current-state
89 character)))
92 (def end-of-name-marker 0x50)
93 (def max-name-length 10)
94 (def name-width 11)
96 (defn read-name [codes]
97 (character-codes->str
98 (take-while
99 (partial not= end-of-name-marker) codes)))
102 (defn sixth-pokemon-name [^SaveState state]
103 (read-name
104 (subvec (vec (memory state))
105 sixth-pokemon-name-start
106 (+ (inc max-name-length)
107 sixth-pokemon-name-start))))
109 (defn rename-sixth-pokemon
110 ([^SaveState state new-name]
111 (assert (< (count new-name) max-name-length))
112 (set-memory-range state sixth-pokemon-name-start
113 (concat (str->character-codes new-name)
114 [end-of-name-marker])))
115 ([new-name]
116 (rename-sixth-pokemon @current-state new-name)))
118 (defn print-text
119 ([^SaveState state begin end]
120 (dorun
121 (map (fn [character-code line]
122 (println
123 (format "0x%04X: " line)
124 (str (character-code->character character-code))))
125 (subvec (vec (memory state)) begin end)
126 (range begin end)))
127 state)
128 ([begin end]
129 (print-text @current-state begin end)))
131 (defn examine-name-memory []
132 (print-text
133 named-A
134 (- sixth-pokemon-name-start 100)
135 (+ sixth-pokemon-name-start 100)))
137 ;; results:
138 ;; 0xD287: end-of-name-sentinel
139 ;; 0xD288: R
140 ;; 0xD289: L
141 ;; 0xD28A: M
142 ;; 0xD28B: end-of-pokemon-name-sentinel
143 ;; 0xD28C: end-of-name-sentinel
144 ;; 0xD28D: end-of-name-sentinel
145 ;; 0xD28E: end-of-name-sentinel
146 ;; 0xD28F: end-of-name-sentinel
147 ;; 0xD290: end-of-name-sentinel
148 ;; 0xD291: end-of-name-sentinel
149 ;; 0xD292: end-of-name-sentinel
150 ;; 0xD293: R
151 ;; 0xD294: L
152 ;; 0xD295: M
153 ;; 0xD296: end-of-pokemon-name-sentinel
154 ;; 0xD297: end-of-name-sentinel
155 ;; 0xD298: end-of-name-sentinel
156 ;; 0xD299: end-of-name-sentinel
157 ;; 0xD29A: end-of-name-sentinel
158 ;; 0xD29B: end-of-name-sentinel
159 ;; 0xD29C: end-of-name-sentinel
160 ;; 0xD29D: end-of-name-sentinel
161 ;; 0xD29E: R
162 ;; 0xD29F: L
163 ;; 0xD2A0: M
164 ;; 0xD2A1: end-of-pokemon-name-sentinel
165 ;; 0xD2A2: end-of-name-sentinel
166 ;; 0xD2A3: end-of-name-sentinel
167 ;; 0xD2A4: end-of-name-sentinel
168 ;; 0xD2A5: end-of-name-sentinel
169 ;; 0xD2A6: end-of-name-sentinel
170 ;; 0xD2A7: end-of-name-sentinel
171 ;; 0xD2A8: end-of-name-sentinel
172 ;; 0xD2A9: R
173 ;; 0xD2AA: L
174 ;; 0xD2AB: M
175 ;; 0xD2AC: end-of-pokemon-name-sentinel
176 ;; 0xD2AD: end-of-name-sentinel
177 ;; 0xD2AE: end-of-name-sentinel
178 ;; 0xD2AF: end-of-name-sentinel
179 ;; 0xD2B0: end-of-name-sentinel
180 ;; 0xD2B1: end-of-name-sentinel
181 ;; 0xD2B2: end-of-name-sentinel
182 ;; 0xD2B3: end-of-name-sentinel
183 ;; 0xD2B4: P
184 ;; 0xD2B5: I
185 ;; 0xD2B6: D
186 ;; 0xD2B7: G
187 ;; 0xD2B8: E
188 ;; 0xD2B9: O
189 ;; 0xD2BA: T
190 ;; 0xD2BB: end-of-pokemon-name-sentinel
191 ;; 0xD2BC: end-of-pokemon-name-sentinel
192 ;; 0xD2BD: end-of-pokemon-name-sentinel
193 ;; 0xD2BE: end-of-pokemon-name-sentinel
194 ;; 0xD2BF: P
195 ;; 0xD2C0: I
196 ;; 0xD2C1: K
197 ;; 0xD2C2: A
198 ;; 0xD2C3: C
199 ;; 0xD2C4: H
200 ;; 0xD2C5: U
201 ;; 0xD2C6: end-of-pokemon-name-sentinel
202 ;; 0xD2C7: end-of-pokemon-name-sentinel
203 ;; 0xD2C8: end-of-pokemon-name-sentinel
204 ;; 0xD2C9: end-of-pokemon-name-sentinel
205 ;; 0xD2CA: C
206 ;; 0xD2CB: H
207 ;; 0xD2CC: A
208 ;; 0xD2CD: R
209 ;; 0xD2CE: I
210 ;; 0xD2CF: Z
211 ;; 0xD2D0: A
212 ;; 0xD2D1: R
213 ;; 0xD2D2: D
214 ;; 0xD2D3: end-of-pokemon-name-sentinel
215 ;; 0xD2D4: end-of-pokemon-name-sentinel
216 ;; 0xD2D5: V
217 ;; 0xD2D6: E
218 ;; 0xD2D7: N
219 ;; 0xD2D8: U
220 ;; 0xD2D9: S
221 ;; 0xD2DA: A
222 ;; 0xD2DB: U
223 ;; 0xD2DC: R
224 ;; 0xD2DD: end-of-pokemon-name-sentinel
225 ;; 0xD2DE: end-of-pokemon-name-sentinel
226 ;; 0xD2DF: end-of-pokemon-name-sentinel
227 ;; 0xD2E0: P
228 ;; 0xD2E1: R
229 ;; 0xD2E2: I
230 ;; 0xD2E3: M
231 ;; 0xD2E4: E
232 ;; 0xD2E5: A
233 ;; 0xD2E6: P
234 ;; 0xD2E7: E
235 ;; 0xD2E8: end-of-pokemon-name-sentinel
236 ;; 0xD2E9: end-of-pokemon-name-sentinel
237 ;; 0xD2EA: end-of-pokemon-name-sentinel
238 ;; 0xD2EB: A
239 ;; 0xD2EC: end-of-pokemon-name-sentinel
240 ;; 0xD2ED: S
241 ;; 0xD2EE: T
242 ;; 0xD2EF: E
243 ;; 0xD2F0: R
244 ;; 0xD2F1:
245 ;; 0xD2F2: B
246 ;; 0xD2F3: A
247 ;; 0xD2F4: L
248 ;; 0xD2F5: L
249 ;; 0xD2F6:
250 ;; 0xD2F7: A
251 ;; 0xD2F8:
252 ;; 0xD2F9:
253 ;; 0xD2FA: end-of-name-sentinel
254 ;; 0xD2FB: end-of-name-sentinel
255 ;; 0xD2FC: A
256 ;; 0xD2FD:
257 ;; 0xD2FE: end-of-name-sentinel
258 ;; 0xD2FF: end-of-name-sentinel
259 ;; 0xD300: end-of-name-sentinel
260 ;; 0xD301: end-of-name-sentinel
261 ;; 0xD302: end-of-name-sentinel
262 ;; 0xD303: end-of-name-sentinel
263 ;; 0xD304: end-of-name-sentinel
264 ;; 0xD305: end-of-name-sentinel
265 ;; 0xD306: end-of-name-sentinel
266 ;; 0xD307: end-of-name-sentinel
267 ;; 0xD308: end-of-name-sentinel
268 ;; 0xD309:
269 ;; 0xD30A: w
270 ;; 0xD30B:
271 ;; 0xD30C: V
272 ;; 0xD30D:
273 ;; 0xD30E:
274 ;; 0xD30F: K
275 ;; 0xD310:
276 ;; 0xD311:
277 ;; 0xD312:
278 ;; 0xD313: A
279 ;; 0xD314:
280 ;; 0xD315:
281 ;; 0xD316:
282 ;; 0xD317: i
283 ;; 0xD318:
284 ;; 0xD319:
285 ;; 0xD31A: end-of-name-sentinel
286 ;; 0xD31B: end-of-name-sentinel
287 ;; 0xD31C:
288 ;; 0xD31D:
289 ;; 0xD31E:
290 ;; 0xD31F:
291 ;; 0xD320:
292 ;; 0xD321:
293 ;; 0xD322:
294 ;; 0xD323:
295 ;; 0xD324:
296 ;; 0xD325:
297 ;; 0xD326:
298 ;; 0xD327:
299 ;; 0xD328:
300 ;; 0xD329:
301 ;; 0xD32A:
302 ;; 0xD32B:
303 ;; 0xD32C:
304 ;; 0xD32D:
305 ;; 0xD32E:
306 ;; 0xD32F:
307 ;; 0xD330:
308 ;; 0xD331: 9
309 ;; 0xD332: end-of-name-sentinel
310 ;; 0xD333: 9
311 ;; 0xD334:
312 ;; 0xD335: 9
313 ;; 0xD336:
314 ;; 0xD337: 9
315 ;; 0xD338: end-of-name-sentinel
316 ;; 0xD339: end-of-name-sentinel
317 ;; 0xD33A: end-of-name-sentinel
318 ;; 0xD33B: end-of-name-sentinel
319 ;; 0xD33C: end-of-name-sentinel
320 ;; 0xD33D: end-of-name-sentinel
321 ;; 0xD33E: end-of-name-sentinel
322 ;; 0xD33F: end-of-name-sentinel
323 ;; 0xD340: end-of-name-sentinel
324 ;; 0xD341: end-of-name-sentinel
325 ;; 0xD342: end-of-name-sentinel
326 ;; 0xD343: end-of-name-sentinel
327 ;; 0xD344: end-of-name-sentinel
328 ;; 0xD345: end-of-name-sentinel
329 ;; 0xD346:
330 ;; 0xD347:
331 ;; 0xD348:
332 ;; 0xD349: G
333 ;; 0xD34A: A
334 ;; 0xD34B: R
335 ;; 0xD34C: Y
336 ;; 0xD34D: end-of-pokemon-name-sentinel
337 ;; 0xD34E: J
340 ;; from this, it looks like the pokemon names are stored all
341 ;; together in one location that begins at 0xD2B4 and
342 ;; extends until 0xD2F5, with each name taking up 11 bytes.
343 ;;
344 ;; rival's name again clearly starts at 0xD349.
347 (def pokemon-names-start 0xD2B4)
350 ;; determine whether "number of pokemon in party"
351 ;; might be kept in RAM and if so, where?
353 (def six-pokemon (read-state "6-pokemon"))
354 (def five-pokemon (read-state "5-pokemon"))
355 (def four-pokemon (read-state "4-pokemon"))
356 (def three-pokemon (read-state "3-pokemon"))
357 (def two-pokemon (read-state "2-pokemon"))
358 (def one-pokemon (read-state "1-pokemon"))
361 (defn analyze-num-pokemon []
362 (apply common-differences
363 (map (comp vec memory)
364 [one-pokemon
365 two-pokemon
366 three-pokemon
367 four-pokemon
368 five-pokemon
369 six-pokemon])))
371 ;; ;; results
372 ;; ([53602 (1 2 3 4 5 6)]
373 ;; [65314 (105 61 93 60 92 34)]
374 ;; [55875 (34 36 43 52 7 0)]
375 ;; [55876 (18 0 33 52 54 30)]
376 ;; [49158 (197 194 77 117 174 134)]
377 ;; [49160 (29 26 57 239 15 243)]
378 ;; [49736 (74 93 34 89 91 59)]
379 ;; [49162 (165 162 182 179 197 109)]
380 ;; [49227 (187 105 204 5 90 238)]
381 ;; [53067 (128 136 132 145 135 11)]
382 ;; [53068 (147 131 141 136 128 7)]
383 ;; [53069 (136 134 148 140 145 2)]
384 ;; [49904 (2 11 10 3 27 12)]
385 ;; [49172 (100 109 213 195 68 104)]
386 ;; [65492 (11 103 128 160 19 56)]
387 ;; [49173 (80 77 72 75 76 67)]
388 ;; [49334 (8 10 11 5 3 1)]
389 ;; [49335 (49 10 11 19 17 15)]
390 ;; [49336 (8 10 11 5 3 1)]
391 ;; [49720 (106 14 118 0 38 11)]
392 ;; [65304 (32 88 19 114 106 33)]
393 ;; [53561 (59 229 48 17 155 103)]
394 ;; [55935 (6 5 4 3 2 1)])
397 ;; two canidates : 0xD162 or 0xDA7F
398 ;; they seem to always sum to 6...
400 ;; try to set both of them when having only one pokemon.
402 (defn change-party-number [^SaveState state new-num]
403 (set-memory state 0xD162 new-num))
405 ;; (continue! (change-party-number one-pokemon 3))
406 ;; result -- can scroll down beyone first pokemon, finding
407 ;; glitched pokemon in places where there were previously no
408 ;; pokemon.
411 (defn change-party-number* [^SaveState state new-num]
412 (set-memory state 0xDA7F new-num))
415 ;; (continue! (change-party-number* one-pokemon 3))
416 ;; cannot widthdraw any pokemon from box 1 past the third
417 ;; pokemon.
419 (def party-number-address 0xD162)
421 (defn party-number
422 ([^SaveState state]
423 (aget (memory state) party-number-address))
424 ([] (party-number @current-state)))
426 (def pokemon-in-box-1-address 0xDA7F)
428 (defn party-names
429 ([^SaveState state]
430 (let [raw-names
431 (subvec (vec (memory state))
432 pokemon-names-start
433 (+ pokemon-names-start
434 (* name-width 6)))]
435 (map
436 read-name
437 (take
438 (party-number state)
439 (partition name-width
440 raw-names)))))
441 ([] (party-names @current-state)))
444 (defn rename-pokemon
445 ([^SaveState state n new-name]
446 (assert (<= 0 n (dec (party-number state))))
447 (assert (<= (count new-name) max-name-length))
448 (set-memory-range
449 state
450 (+ (* n name-width) pokemon-names-start)
451 (concat (str->character-codes new-name) [end-of-name-marker])))
452 ([n new-name]
453 (rename-pokemon @current-state n new-name)))
455 ;; on further analysis, it appears that the original
456 ;; trainer info for each pokemon is also stored together,
457 ;; starting at 0xD272 and continuing to 0xD2B3, with
458 ;; 11 bytes reserved for each OT name.
460 (def OT-start 0xD272)
462 (defn original-trainers
463 ([^SaveState state]
464 (let [raw-names
465 (subvec (vec (memory state))
466 OT-start
467 (+ OT-start
468 (* name-width 6)))]
469 (map read-name
470 (take (party-number state)
471 (partition name-width raw-names)))))
472 ([] (original-trainers @current-state)))
474 (defn set-original-trainer
475 "Set the OT name for a pokemon.
476 Note that a pokemon is still considered 'yours' if
477 the OT ID is the same as your own."
478 ([^SaveState state n new-name]
479 (assert (<= 0 n (dec (party-number state))))
480 (assert (<= (count new-name) max-name-length))
481 (set-memory-range
482 state
483 (+ (* n name-width) OT-start)
484 (concat (str->character-codes new-name) [end-of-name-marker])))
485 ([n new-name]
486 (set-original-trainer @current-state n new-name)))
488 ;; PIKACHU stops following if you set it's OT to another name
489 ;; and then back to you own.
490 ;; But not if you set it to your own name, obviously.
495 ;; OT ID Numbers.
496 ;; My own id is 05195. A quick search of memory between
497 ;; 0xD162 and 0xD31B revealed the pattern 0x144B exactly
498 ;; six times.
500 ;; the locations were:
502 (def OT-ID-addresses [0xD176 0xD1A2 0xD1CE 0xD1FA 0xD228 0xD252])
505 (defn set-pokemon-id
506 ([^SaveState state n new-id]
507 (assert (<= 0 n (dec (party-number state))))
508 (assert (<= 0 new-id 0xFFFF))
509 (set-memory-range
510 state
511 (OT-ID-addresses n)
512 [(bit-shift-right (bit-and new-id 0xFF00) 8)
513 (bit-and new-id 0xFF)
514 ]))
515 ([n new-id]
516 (set-pokemon-id @current-state n new-id)))
522 (defn get-pokemon-1-info
523 ([state]
524 (subvec (vec (memory state))
525 0xD162 0xD197))
526 ([] (get-pokemon-1-info @current-state)))
528 (def baseline (File. "/home/r/baseline-poke"))
533 (import java.io.File)
535 (defn set-baseline [state]
536 (clojure.java.io/copy
537 (with-out-str (print-listing state 0xD162 0xD197))
538 baseline))
540 (defn compare-pokemon
541 ([state]
542 (let [target (File. "/home/r/temp-poke")]
543 (clojure.java.io/copy
544 (with-out-str (print-listing state 0xD162 0xD197))
545 target)
546 (println (:out
547 (clojure.java.shell/sh "diff"
548 (.getCanonicalPath baseline)
549 (.getCanonicalPath target))))))
550 ([] (compare-pokemon @current-state)))
553 (defn crazy-pid []
554 (read-state "crazy-pidgeot"))