view clojure/com/aurellem/exp/pokemon.clj @ 157:da652c72e55d

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