view clojure/com/aurellem/exp/pokemon.clj @ 166:76b031d2f586

determined position of pokemon color maps
author Robert McIntyre <rlm@mit.edu>
date Tue, 20 Mar 2012 18:50:49 -0500
parents cd7979aa544e
children bfd06de5a934
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 constants
5 characters species moves))
6 (:import java.io.File)
7 (:import [com.aurellem.gb.gb_driver SaveState]))
10 (def pidgeot-lvl-36 (mid-game))
13 (def pidgeot-lvl-37 (read-state "pidgeot-lvl-37"))
16 (def pidgeot-lvl-38 (read-state "pidgeot-lvl-38"))
19 (def pidgeot-lvl-39 (read-state "pidgeot-lvl-39"))
22 (def pidgeot-lvl-40 (read-state "pidgeot-lvl-40"))
25 (defn level-analysis []
26 (apply common-differences
27 (map (comp vec memory)
28 [pidgeot-lvl-36
29 pidgeot-lvl-37
30 pidgeot-lvl-38
31 pidgeot-lvl-39
32 pidgeot-lvl-40])))
34 ;; inconclusive -- implies that level is calculated from
35 ;; some other values.
38 (def name-pidgeotto (read-state "name-pidgeotto"))
39 (def named-A (read-state "named-A"))
40 (def named-B (read-state "named-B"))
41 (def named-C (read-state "named-C"))
42 (def named-D (read-state "named-D"))
43 (def named-E (read-state "named-E"))
44 (def named-F (read-state "named-F"))
46 (defn name-analysis []
47 (apply common-differences
48 (map (comp vec memory)
49 [named-A
50 named-B
51 named-C
52 named-D
53 named-E
54 named-F])))
56 ;; resluted in 3 separate locations that could
57 ;; possibly hold the first letter of the pokemon's name
59 0xCF4A
60 0xD2EB
61 0xCEED
63 ;; try changing each of them
66 (defn test-cf4a []
67 (continue!
68 (set-memory named-A 0xCF4A (character->character-code "Z"))))
69 ;; result -- pidgeotto named "A"
71 (defn test-d2eb []
72 (continue!
73 (set-memory named-A 0xD2EB (character->character-code "Z"))))
74 ;; result -- pidgeotto named "Z"
76 (defn test-ceed []
77 (continue!
78 (set-memory named-A 0xCEED (character->character-code "Z"))))
79 ;; result -- pidgeotto named "A"
81 (def sixth-pokemon-name-start 0xD2EB)
84 (defn set-sixth-pokemon-name-first-character
85 ([state character]
86 (set-memory state sixth-pokemon-name-start
87 (character->character-code character)))
88 ([character]
89 (set-sixth-pokemon-name-first-character @current-state
90 character)))
93 (def end-of-name-marker 0x50)
94 (def max-name-length 10)
95 (def name-width 11)
97 (defn read-name [codes]
98 (character-codes->str
99 (take-while
100 (partial not= end-of-name-marker) codes)))
103 (defn sixth-pokemon-name [^SaveState state]
104 (read-name
105 (subvec (vec (memory state))
106 sixth-pokemon-name-start
107 (+ (inc max-name-length)
108 sixth-pokemon-name-start))))
110 (defn rename-sixth-pokemon
111 ([^SaveState state new-name]
112 (assert (< (count new-name) max-name-length))
113 (set-memory-range state sixth-pokemon-name-start
114 (concat (str->character-codes new-name)
115 [end-of-name-marker])))
116 ([new-name]
117 (rename-sixth-pokemon @current-state new-name)))
119 (defn print-text
120 ([^SaveState state begin end]
121 (dorun
122 (map (fn [character-code line]
123 (println
124 (format "0x%04X: " line)
125 (str (character-code->character character-code))))
126 (subvec (vec (memory state)) begin end)
127 (range begin end)))
128 state)
129 ([begin end]
130 (print-text @current-state begin end)))
132 (defn examine-name-memory []
133 (print-text
134 named-A
135 (- sixth-pokemon-name-start 100)
136 (+ sixth-pokemon-name-start 100)))
138 ;; results:
139 ;; 0xD287: end-of-name-sentinel
140 ;; 0xD288: R
141 ;; 0xD289: L
142 ;; 0xD28A: M
143 ;; 0xD28B: end-of-pokemon-name-sentinel
144 ;; 0xD28C: end-of-name-sentinel
145 ;; 0xD28D: end-of-name-sentinel
146 ;; 0xD28E: end-of-name-sentinel
147 ;; 0xD28F: end-of-name-sentinel
148 ;; 0xD290: end-of-name-sentinel
149 ;; 0xD291: end-of-name-sentinel
150 ;; 0xD292: end-of-name-sentinel
151 ;; 0xD293: R
152 ;; 0xD294: L
153 ;; 0xD295: M
154 ;; 0xD296: end-of-pokemon-name-sentinel
155 ;; 0xD297: end-of-name-sentinel
156 ;; 0xD298: end-of-name-sentinel
157 ;; 0xD299: end-of-name-sentinel
158 ;; 0xD29A: end-of-name-sentinel
159 ;; 0xD29B: end-of-name-sentinel
160 ;; 0xD29C: end-of-name-sentinel
161 ;; 0xD29D: end-of-name-sentinel
162 ;; 0xD29E: R
163 ;; 0xD29F: L
164 ;; 0xD2A0: M
165 ;; 0xD2A1: end-of-pokemon-name-sentinel
166 ;; 0xD2A2: end-of-name-sentinel
167 ;; 0xD2A3: end-of-name-sentinel
168 ;; 0xD2A4: end-of-name-sentinel
169 ;; 0xD2A5: end-of-name-sentinel
170 ;; 0xD2A6: end-of-name-sentinel
171 ;; 0xD2A7: end-of-name-sentinel
172 ;; 0xD2A8: end-of-name-sentinel
173 ;; 0xD2A9: R
174 ;; 0xD2AA: L
175 ;; 0xD2AB: M
176 ;; 0xD2AC: end-of-pokemon-name-sentinel
177 ;; 0xD2AD: end-of-name-sentinel
178 ;; 0xD2AE: end-of-name-sentinel
179 ;; 0xD2AF: end-of-name-sentinel
180 ;; 0xD2B0: end-of-name-sentinel
181 ;; 0xD2B1: end-of-name-sentinel
182 ;; 0xD2B2: end-of-name-sentinel
183 ;; 0xD2B3: end-of-name-sentinel
184 ;; 0xD2B4: P
185 ;; 0xD2B5: I
186 ;; 0xD2B6: D
187 ;; 0xD2B7: G
188 ;; 0xD2B8: E
189 ;; 0xD2B9: O
190 ;; 0xD2BA: T
191 ;; 0xD2BB: end-of-pokemon-name-sentinel
192 ;; 0xD2BC: end-of-pokemon-name-sentinel
193 ;; 0xD2BD: end-of-pokemon-name-sentinel
194 ;; 0xD2BE: end-of-pokemon-name-sentinel
195 ;; 0xD2BF: P
196 ;; 0xD2C0: I
197 ;; 0xD2C1: K
198 ;; 0xD2C2: A
199 ;; 0xD2C3: C
200 ;; 0xD2C4: H
201 ;; 0xD2C5: U
202 ;; 0xD2C6: end-of-pokemon-name-sentinel
203 ;; 0xD2C7: end-of-pokemon-name-sentinel
204 ;; 0xD2C8: end-of-pokemon-name-sentinel
205 ;; 0xD2C9: end-of-pokemon-name-sentinel
206 ;; 0xD2CA: C
207 ;; 0xD2CB: H
208 ;; 0xD2CC: A
209 ;; 0xD2CD: R
210 ;; 0xD2CE: I
211 ;; 0xD2CF: Z
212 ;; 0xD2D0: A
213 ;; 0xD2D1: R
214 ;; 0xD2D2: D
215 ;; 0xD2D3: end-of-pokemon-name-sentinel
216 ;; 0xD2D4: end-of-pokemon-name-sentinel
217 ;; 0xD2D5: V
218 ;; 0xD2D6: E
219 ;; 0xD2D7: N
220 ;; 0xD2D8: U
221 ;; 0xD2D9: S
222 ;; 0xD2DA: A
223 ;; 0xD2DB: U
224 ;; 0xD2DC: R
225 ;; 0xD2DD: end-of-pokemon-name-sentinel
226 ;; 0xD2DE: end-of-pokemon-name-sentinel
227 ;; 0xD2DF: end-of-pokemon-name-sentinel
228 ;; 0xD2E0: P
229 ;; 0xD2E1: R
230 ;; 0xD2E2: I
231 ;; 0xD2E3: M
232 ;; 0xD2E4: E
233 ;; 0xD2E5: A
234 ;; 0xD2E6: P
235 ;; 0xD2E7: E
236 ;; 0xD2E8: end-of-pokemon-name-sentinel
237 ;; 0xD2E9: end-of-pokemon-name-sentinel
238 ;; 0xD2EA: end-of-pokemon-name-sentinel
239 ;; 0xD2EB: A
240 ;; 0xD2EC: end-of-pokemon-name-sentinel
241 ;; 0xD2ED: S
242 ;; 0xD2EE: T
243 ;; 0xD2EF: E
244 ;; 0xD2F0: R
245 ;; 0xD2F1:
246 ;; 0xD2F2: B
247 ;; 0xD2F3: A
248 ;; 0xD2F4: L
249 ;; 0xD2F5: L
250 ;; 0xD2F6:
251 ;; 0xD2F7: A
252 ;; 0xD2F8:
253 ;; 0xD2F9:
254 ;; 0xD2FA: end-of-name-sentinel
255 ;; 0xD2FB: end-of-name-sentinel
256 ;; 0xD2FC: A
257 ;; 0xD2FD:
258 ;; 0xD2FE: end-of-name-sentinel
259 ;; 0xD2FF: end-of-name-sentinel
260 ;; 0xD300: end-of-name-sentinel
261 ;; 0xD301: end-of-name-sentinel
262 ;; 0xD302: end-of-name-sentinel
263 ;; 0xD303: end-of-name-sentinel
264 ;; 0xD304: end-of-name-sentinel
265 ;; 0xD305: end-of-name-sentinel
266 ;; 0xD306: end-of-name-sentinel
267 ;; 0xD307: end-of-name-sentinel
268 ;; 0xD308: end-of-name-sentinel
269 ;; 0xD309:
270 ;; 0xD30A: w
271 ;; 0xD30B:
272 ;; 0xD30C: V
273 ;; 0xD30D:
274 ;; 0xD30E:
275 ;; 0xD30F: K
276 ;; 0xD310:
277 ;; 0xD311:
278 ;; 0xD312:
279 ;; 0xD313: A
280 ;; 0xD314:
281 ;; 0xD315:
282 ;; 0xD316:
283 ;; 0xD317: i
284 ;; 0xD318:
285 ;; 0xD319:
286 ;; 0xD31A: end-of-name-sentinel
287 ;; 0xD31B: end-of-name-sentinel
288 ;; 0xD31C:
289 ;; 0xD31D:
290 ;; 0xD31E:
291 ;; 0xD31F:
292 ;; 0xD320:
293 ;; 0xD321:
294 ;; 0xD322:
295 ;; 0xD323:
296 ;; 0xD324:
297 ;; 0xD325:
298 ;; 0xD326:
299 ;; 0xD327:
300 ;; 0xD328:
301 ;; 0xD329:
302 ;; 0xD32A:
303 ;; 0xD32B:
304 ;; 0xD32C:
305 ;; 0xD32D:
306 ;; 0xD32E:
307 ;; 0xD32F:
308 ;; 0xD330:
309 ;; 0xD331: 9
310 ;; 0xD332: end-of-name-sentinel
311 ;; 0xD333: 9
312 ;; 0xD334:
313 ;; 0xD335: 9
314 ;; 0xD336:
315 ;; 0xD337: 9
316 ;; 0xD338: end-of-name-sentinel
317 ;; 0xD339: end-of-name-sentinel
318 ;; 0xD33A: end-of-name-sentinel
319 ;; 0xD33B: end-of-name-sentinel
320 ;; 0xD33C: end-of-name-sentinel
321 ;; 0xD33D: end-of-name-sentinel
322 ;; 0xD33E: end-of-name-sentinel
323 ;; 0xD33F: end-of-name-sentinel
324 ;; 0xD340: end-of-name-sentinel
325 ;; 0xD341: end-of-name-sentinel
326 ;; 0xD342: end-of-name-sentinel
327 ;; 0xD343: end-of-name-sentinel
328 ;; 0xD344: end-of-name-sentinel
329 ;; 0xD345: end-of-name-sentinel
330 ;; 0xD346:
331 ;; 0xD347:
332 ;; 0xD348:
333 ;; 0xD349: G
334 ;; 0xD34A: A
335 ;; 0xD34B: R
336 ;; 0xD34C: Y
337 ;; 0xD34D: end-of-pokemon-name-sentinel
338 ;; 0xD34E: J
341 ;; from this, it looks like the pokemon names are stored all
342 ;; together in one location that begins at 0xD2B4 and
343 ;; extends until 0xD2F5, with each name taking up 11 bytes.
344 ;;
345 ;; rival's name again clearly starts at 0xD349.
348 (def pokemon-names-start 0xD2B4)
351 ;; determine whether "number of pokemon in party"
352 ;; might be kept in RAM and if so, where?
354 (def six-pokemon (read-state "6-pokemon"))
355 (def five-pokemon (read-state "5-pokemon"))
356 (def four-pokemon (read-state "4-pokemon"))
357 (def three-pokemon (read-state "3-pokemon"))
358 (def two-pokemon (read-state "2-pokemon"))
359 (def one-pokemon (read-state "1-pokemon"))
362 (defn analyze-num-pokemon []
363 (apply common-differences
364 (map (comp vec memory)
365 [one-pokemon
366 two-pokemon
367 three-pokemon
368 four-pokemon
369 five-pokemon
370 six-pokemon])))
372 ;; ;; results
373 ;; ([53602 (1 2 3 4 5 6)]
374 ;; [65314 (105 61 93 60 92 34)]
375 ;; [55875 (34 36 43 52 7 0)]
376 ;; [55876 (18 0 33 52 54 30)]
377 ;; [49158 (197 194 77 117 174 134)]
378 ;; [49160 (29 26 57 239 15 243)]
379 ;; [49736 (74 93 34 89 91 59)]
380 ;; [49162 (165 162 182 179 197 109)]
381 ;; [49227 (187 105 204 5 90 238)]
382 ;; [53067 (128 136 132 145 135 11)]
383 ;; [53068 (147 131 141 136 128 7)]
384 ;; [53069 (136 134 148 140 145 2)]
385 ;; [49904 (2 11 10 3 27 12)]
386 ;; [49172 (100 109 213 195 68 104)]
387 ;; [65492 (11 103 128 160 19 56)]
388 ;; [49173 (80 77 72 75 76 67)]
389 ;; [49334 (8 10 11 5 3 1)]
390 ;; [49335 (49 10 11 19 17 15)]
391 ;; [49336 (8 10 11 5 3 1)]
392 ;; [49720 (106 14 118 0 38 11)]
393 ;; [65304 (32 88 19 114 106 33)]
394 ;; [53561 (59 229 48 17 155 103)]
395 ;; [55935 (6 5 4 3 2 1)])
398 ;; two canidates : 0xD162 or 0xDA7F
399 ;; they seem to always sum to 6...
401 ;; try to set both of them when having only one pokemon.
403 (defn change-party-number [^SaveState state new-num]
404 (set-memory state 0xD162 new-num))
406 ;; (continue! (change-party-number one-pokemon 3))
407 ;; result -- can scroll down beyone first pokemon, finding
408 ;; glitched pokemon in places where there were previously no
409 ;; pokemon.
412 (defn change-party-number* [^SaveState state new-num]
413 (set-memory state 0xDA7F new-num))
416 ;; (continue! (change-party-number* one-pokemon 3))
417 ;; cannot widthdraw any pokemon from box 1 past the third
418 ;; pokemon.
420 (def party-number-address 0xD162)
422 (defn party-number
423 ([^SaveState state]
424 (aget (memory state) party-number-address))
425 ([] (party-number @current-state)))
427 (def pokemon-in-box-1-address 0xDA7F)
429 (defn party-names
430 ([^SaveState state]
431 (let [raw-names
432 (subvec (vec (memory state))
433 pokemon-names-start
434 (+ pokemon-names-start
435 (* name-width 6)))]
436 (map
437 read-name
438 (take
439 (party-number state)
440 (partition name-width
441 raw-names)))))
442 ([] (party-names @current-state)))
445 (defn rename-pokemon
446 ([^SaveState state n new-name]
447 (assert (<= 0 n (dec (party-number state))))
448 (assert (<= (count new-name) max-name-length))
449 (set-memory-range
450 state
451 (+ (* n name-width) pokemon-names-start)
452 (concat (str->character-codes new-name) [end-of-name-marker])))
453 ([n new-name]
454 (rename-pokemon @current-state n new-name)))
456 ;; on further analysis, it appears that the original
457 ;; trainer info for each pokemon is also stored together,
458 ;; starting at 0xD272 and continuing to 0xD2B3, with
459 ;; 11 bytes reserved for each OT name.
461 (def OT-start 0xD272)
463 (defn original-trainers
464 ([^SaveState state]
465 (let [raw-names
466 (subvec (vec (memory state))
467 OT-start
468 (+ OT-start
469 (* name-width 6)))]
470 (map read-name
471 (take (party-number state)
472 (partition name-width raw-names)))))
473 ([] (original-trainers @current-state)))
475 (defn set-original-trainer
476 "Set the OT name for a pokemon.
477 Note that a pokemon is still considered 'yours' if
478 the OT ID is the same as your own."
479 ([^SaveState state n new-name]
480 (assert (<= 0 n (dec (party-number state))))
481 (assert (<= (count new-name) max-name-length))
482 (set-memory-range
483 state
484 (+ (* n name-width) OT-start)
485 (concat (str->character-codes new-name) [end-of-name-marker])))
486 ([n new-name]
487 (set-original-trainer @current-state n new-name)))
489 ;; PIKACHU stops following if you set it's OT to another name
490 ;; and then back to you own.
491 ;; But not if you set it to your own name, obviously.
496 ;; OT ID Numbers.
497 ;; My own id is 05195. A quick search of memory between
498 ;; 0xD162 and 0xD31B revealed the pattern 0x144B exactly
499 ;; six times.
501 ;; the locations were:
503 (def OT-ID-addresses [0xD176 0xD1A2 0xD1CE 0xD1FA 0xD228 0xD252])
506 (defn set-pokemon-id
507 ([^SaveState state n new-id]
508 (assert (<= 0 n (dec (party-number state))))
509 (assert (<= 0 new-id 0xFFFF))
510 (set-memory-range
511 state
512 (OT-ID-addresses n)
513 [(bit-shift-right (bit-and new-id 0xFF00) 8)
514 (bit-and new-id 0xFF)
515 ]))
516 ([n new-id]
517 (set-pokemon-id @current-state n new-id)))
523 (defn get-pokemon-1-info
524 ([state]
525 (subvec (vec (memory state))
526 0xD162 0xD197))
527 ([] (get-pokemon-1-info @current-state)))
529 (def baseline (File. "/home/r/baseline-poke"))
534 (import java.io.File)
536 (defn set-baseline [state]
537 (clojure.java.io/copy
538 (with-out-str (print-listing state 0xD162 0xD197))
539 baseline))
541 (defn compare-pokemon
542 ([state]
543 (let [target (File. "/home/r/temp-poke")]
544 (clojure.java.io/copy
545 (with-out-str (print-listing state 0xD162 0xD197))
546 target)
547 (println (:out
548 (clojure.java.shell/sh "diff"
549 (.getCanonicalPath baseline)
550 (.getCanonicalPath target))))))
551 ([] (compare-pokemon @current-state)))
554 (defn crazy-pid []
555 (read-state "crazy-pidgeot"))
557 (def unknown "")
559 (def pokemon-1-info
560 {0xD16A "Color Map" ;; 0
561 0xD16B "Current-HP (h)" ;; 1
562 0xD16C "Current-HP (l)" ;; 2
563 0XD16D unknown ;; 3
564 0xD16E unknown ;; 4
565 0xD16F unknown ;; 5
566 0xD170 unknown ;; 6
567 0xD171 unknown ;; 7
568 0xD172 "Move 1 ID" ;; 8
569 0xD173 "Move 2 ID" ;; 9
570 0xD174 "Move 3 ID" ;; 10
571 0xD175 "Move 4 ID" ;; 11
572 0xD176 "OT-ID (h)" ;; 12
573 0xD177 "OT-ID (l)" ;; 13
574 0xD178 "Exp. Points (h)" ;; 14
575 0xD179 "Exp. Points (m)" ;; 15
576 0xD17A "Exp. Points (l)" ;; 16
577 0xD17B "HP DV" ;; 17
578 0xD17C unknown ;; 18
579 0xD17D "Attack DV" ;; 19
580 0xD17E unknown ;; 20
581 0xD17F "Defense DV" ;; 21
582 0xD180 unknown ;; 22
583 0xD181 "Speed DV" ;; 23
584 0xD182 unknown ;; 24
585 0xD183 "Special DV" ;; 25
586 0xD184 unknown ;; 26
587 0xD185 unknown ;; 27
588 0xD186 unknown ;; 28
589 0xD187 "PP Move 1" ;; 29
590 0xD188 "PP Move 2" ;; 30
591 0xD189 "PP Move 3" ;; 31
592 0xD18A "PP Move 4" ;; 32
593 0xD18B "Current Level" ;; 33
594 0xD18C "HP Total (h)" ;; 34
595 0xD18D "HP Total (l)" ;; 35
596 0xD18E "Attack (h)" ;; 36
597 0xD18F "Attack (l)" ;; 37
598 0xD190 "Defense (h)" ;; 38
599 0xD191 "Defense (l)" ;; 39
600 0xD192 "Speed (h)" ;; 40
601 0xD193 "Speed (l)" ;; 41
602 0xD194 "Special (h)" ;; 42
603 0xD195 "Special (h)" ;; 43
604 })
606 (defn pokemon-data
607 ([^SaveState state pokemon-num]
608 (assert (<= 0 pokemon-num 5))
609 (let [base (+ (* pokemon-num pokemon-record-width) 0xD16A)]
610 (subvec (vec (memory state)) base
611 (+ base pokemon-record-width))))
612 ([pokemon-num] (pokemon-data @current-state pokemon-num)))
614 (defn set-pokemon-data
615 ([^SaveState state pokemon-num new-data]
616 (assert (<= 0 pokemon-num 5))
617 (let [base (+ (* pokemon-num pokemon-record-width) 0xD16A)]
618 (set-memory-range state base new-data)))
619 ([pokemon-num new-data]
620 (set-pokemon-data @current-state pokemon-num new-data)))
622 (defn print-pokemon-data [^SaveState save pokemon-num])
625 (defn pre-battle [] (read-state "prepare-for-battle"))