Mercurial > vba-clojure
view clojure/com/aurellem/exp/pokemon.clj @ 158:339220ab3f5d
finally found the species identifiers!
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Tue, 20 Mar 2012 03:40:24 -0500 |
parents | 9367bb5e55e6 |
children | 9ca350a476f0 |
line wrap: on
line source
1 (ns com.aurellem.exp.pokemon2 "Here I find out how pokemon are stored in memory."3 (:use (com.aurellem.gb gb-driver items assembly util4 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-differences25 (map (comp vec memory)26 [pidgeot-lvl-3627 pidgeot-lvl-3728 pidgeot-lvl-3829 pidgeot-lvl-3930 pidgeot-lvl-40])))32 ;; inconclusive -- implies that level is calculated from33 ;; 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-differences46 (map (comp vec memory)47 [named-A48 named-B49 named-C50 named-D51 named-E52 named-F])))54 ;; resluted in 3 separate locations that could55 ;; possibly hold the first letter of the pokemon's name57 0xCF4A58 0xD2EB59 0xCEED61 ;; try changing each of them64 (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-character83 ([state character]84 (set-memory state sixth-pokemon-name-start85 (character->character-code character)))86 ([character]87 (set-sixth-pokemon-name-first-character @current-state88 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->str97 (take-while98 (partial not= end-of-name-marker) codes)))101 (defn sixth-pokemon-name [^SaveState state]102 (read-name103 (subvec (vec (memory state))104 sixth-pokemon-name-start105 (+ (inc max-name-length)106 sixth-pokemon-name-start))))108 (defn rename-sixth-pokemon109 ([^SaveState state new-name]110 (assert (< (count new-name) max-name-length))111 (set-memory-range state sixth-pokemon-name-start112 (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-text118 ([^SaveState state begin end]119 (dorun120 (map (fn [character-code line]121 (println122 (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-text132 named-A133 (- sixth-pokemon-name-start 100)134 (+ sixth-pokemon-name-start 100)))136 ;; results:137 ;; 0xD287: end-of-name-sentinel138 ;; 0xD288: R139 ;; 0xD289: L140 ;; 0xD28A: M141 ;; 0xD28B: end-of-pokemon-name-sentinel142 ;; 0xD28C: end-of-name-sentinel143 ;; 0xD28D: end-of-name-sentinel144 ;; 0xD28E: end-of-name-sentinel145 ;; 0xD28F: end-of-name-sentinel146 ;; 0xD290: end-of-name-sentinel147 ;; 0xD291: end-of-name-sentinel148 ;; 0xD292: end-of-name-sentinel149 ;; 0xD293: R150 ;; 0xD294: L151 ;; 0xD295: M152 ;; 0xD296: end-of-pokemon-name-sentinel153 ;; 0xD297: end-of-name-sentinel154 ;; 0xD298: end-of-name-sentinel155 ;; 0xD299: end-of-name-sentinel156 ;; 0xD29A: end-of-name-sentinel157 ;; 0xD29B: end-of-name-sentinel158 ;; 0xD29C: end-of-name-sentinel159 ;; 0xD29D: end-of-name-sentinel160 ;; 0xD29E: R161 ;; 0xD29F: L162 ;; 0xD2A0: M163 ;; 0xD2A1: end-of-pokemon-name-sentinel164 ;; 0xD2A2: end-of-name-sentinel165 ;; 0xD2A3: end-of-name-sentinel166 ;; 0xD2A4: end-of-name-sentinel167 ;; 0xD2A5: end-of-name-sentinel168 ;; 0xD2A6: end-of-name-sentinel169 ;; 0xD2A7: end-of-name-sentinel170 ;; 0xD2A8: end-of-name-sentinel171 ;; 0xD2A9: R172 ;; 0xD2AA: L173 ;; 0xD2AB: M174 ;; 0xD2AC: end-of-pokemon-name-sentinel175 ;; 0xD2AD: end-of-name-sentinel176 ;; 0xD2AE: end-of-name-sentinel177 ;; 0xD2AF: end-of-name-sentinel178 ;; 0xD2B0: end-of-name-sentinel179 ;; 0xD2B1: end-of-name-sentinel180 ;; 0xD2B2: end-of-name-sentinel181 ;; 0xD2B3: end-of-name-sentinel182 ;; 0xD2B4: P183 ;; 0xD2B5: I184 ;; 0xD2B6: D185 ;; 0xD2B7: G186 ;; 0xD2B8: E187 ;; 0xD2B9: O188 ;; 0xD2BA: T189 ;; 0xD2BB: end-of-pokemon-name-sentinel190 ;; 0xD2BC: end-of-pokemon-name-sentinel191 ;; 0xD2BD: end-of-pokemon-name-sentinel192 ;; 0xD2BE: end-of-pokemon-name-sentinel193 ;; 0xD2BF: P194 ;; 0xD2C0: I195 ;; 0xD2C1: K196 ;; 0xD2C2: A197 ;; 0xD2C3: C198 ;; 0xD2C4: H199 ;; 0xD2C5: U200 ;; 0xD2C6: end-of-pokemon-name-sentinel201 ;; 0xD2C7: end-of-pokemon-name-sentinel202 ;; 0xD2C8: end-of-pokemon-name-sentinel203 ;; 0xD2C9: end-of-pokemon-name-sentinel204 ;; 0xD2CA: C205 ;; 0xD2CB: H206 ;; 0xD2CC: A207 ;; 0xD2CD: R208 ;; 0xD2CE: I209 ;; 0xD2CF: Z210 ;; 0xD2D0: A211 ;; 0xD2D1: R212 ;; 0xD2D2: D213 ;; 0xD2D3: end-of-pokemon-name-sentinel214 ;; 0xD2D4: end-of-pokemon-name-sentinel215 ;; 0xD2D5: V216 ;; 0xD2D6: E217 ;; 0xD2D7: N218 ;; 0xD2D8: U219 ;; 0xD2D9: S220 ;; 0xD2DA: A221 ;; 0xD2DB: U222 ;; 0xD2DC: R223 ;; 0xD2DD: end-of-pokemon-name-sentinel224 ;; 0xD2DE: end-of-pokemon-name-sentinel225 ;; 0xD2DF: end-of-pokemon-name-sentinel226 ;; 0xD2E0: P227 ;; 0xD2E1: R228 ;; 0xD2E2: I229 ;; 0xD2E3: M230 ;; 0xD2E4: E231 ;; 0xD2E5: A232 ;; 0xD2E6: P233 ;; 0xD2E7: E234 ;; 0xD2E8: end-of-pokemon-name-sentinel235 ;; 0xD2E9: end-of-pokemon-name-sentinel236 ;; 0xD2EA: end-of-pokemon-name-sentinel237 ;; 0xD2EB: A238 ;; 0xD2EC: end-of-pokemon-name-sentinel239 ;; 0xD2ED: S240 ;; 0xD2EE: T241 ;; 0xD2EF: E242 ;; 0xD2F0: R243 ;; 0xD2F1:244 ;; 0xD2F2: B245 ;; 0xD2F3: A246 ;; 0xD2F4: L247 ;; 0xD2F5: L248 ;; 0xD2F6:249 ;; 0xD2F7: A250 ;; 0xD2F8:251 ;; 0xD2F9:252 ;; 0xD2FA: end-of-name-sentinel253 ;; 0xD2FB: end-of-name-sentinel254 ;; 0xD2FC: A255 ;; 0xD2FD:256 ;; 0xD2FE: end-of-name-sentinel257 ;; 0xD2FF: end-of-name-sentinel258 ;; 0xD300: end-of-name-sentinel259 ;; 0xD301: end-of-name-sentinel260 ;; 0xD302: end-of-name-sentinel261 ;; 0xD303: end-of-name-sentinel262 ;; 0xD304: end-of-name-sentinel263 ;; 0xD305: end-of-name-sentinel264 ;; 0xD306: end-of-name-sentinel265 ;; 0xD307: end-of-name-sentinel266 ;; 0xD308: end-of-name-sentinel267 ;; 0xD309:268 ;; 0xD30A: w269 ;; 0xD30B:270 ;; 0xD30C: V271 ;; 0xD30D:272 ;; 0xD30E:273 ;; 0xD30F: K274 ;; 0xD310:275 ;; 0xD311:276 ;; 0xD312:277 ;; 0xD313: A278 ;; 0xD314:279 ;; 0xD315:280 ;; 0xD316:281 ;; 0xD317: i282 ;; 0xD318:283 ;; 0xD319:284 ;; 0xD31A: end-of-name-sentinel285 ;; 0xD31B: end-of-name-sentinel286 ;; 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: 9308 ;; 0xD332: end-of-name-sentinel309 ;; 0xD333: 9310 ;; 0xD334:311 ;; 0xD335: 9312 ;; 0xD336:313 ;; 0xD337: 9314 ;; 0xD338: end-of-name-sentinel315 ;; 0xD339: end-of-name-sentinel316 ;; 0xD33A: end-of-name-sentinel317 ;; 0xD33B: end-of-name-sentinel318 ;; 0xD33C: end-of-name-sentinel319 ;; 0xD33D: end-of-name-sentinel320 ;; 0xD33E: end-of-name-sentinel321 ;; 0xD33F: end-of-name-sentinel322 ;; 0xD340: end-of-name-sentinel323 ;; 0xD341: end-of-name-sentinel324 ;; 0xD342: end-of-name-sentinel325 ;; 0xD343: end-of-name-sentinel326 ;; 0xD344: end-of-name-sentinel327 ;; 0xD345: end-of-name-sentinel328 ;; 0xD346:329 ;; 0xD347:330 ;; 0xD348:331 ;; 0xD349: G332 ;; 0xD34A: A333 ;; 0xD34B: R334 ;; 0xD34C: Y335 ;; 0xD34D: end-of-pokemon-name-sentinel336 ;; 0xD34E: J339 ;; from this, it looks like the pokemon names are stored all340 ;; together in one location that begins at 0xD2B4 and341 ;; 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-differences362 (map (comp vec memory)363 [one-pokemon364 two-pokemon365 three-pokemon366 four-pokemon367 five-pokemon368 six-pokemon])))370 ;; ;; results371 ;; ([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 0xDA7F397 ;; 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, finding406 ;; glitched pokemon in places where there were previously no407 ;; 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 third416 ;; pokemon.418 (def party-number-address 0xD162)420 (defn party-number421 ([^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-names428 ([^SaveState state]429 (let [raw-names430 (subvec (vec (memory state))431 pokemon-names-start432 (+ pokemon-names-start433 (* name-width 6)))]434 (map435 read-name436 (take437 (party-number state)438 (partition name-width439 raw-names)))))440 ([] (party-names @current-state)))443 (defn rename-pokemon444 ([^SaveState state n new-name]445 (assert (<= 0 n (dec (party-number state))))446 (assert (<= (count new-name) max-name-length))447 (set-memory-range448 state449 (+ (* 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 original455 ;; trainer info for each pokemon is also stored together,456 ;; starting at 0xD272 and continuing to 0xD2B3, with457 ;; 11 bytes reserved for each OT name.459 (def OT-start 0xD272)461 (defn original-trainers462 ([^SaveState state]463 (let [raw-names464 (subvec (vec (memory state))465 OT-start466 (+ OT-start467 (* name-width 6)))]468 (map read-name469 (take (party-number state)470 (partition name-width raw-names)))))471 ([] (original-trainers @current-state)))473 (defn set-original-trainer474 "Set the OT name for a pokemon.475 Note that a pokemon is still considered 'yours' if476 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-range481 state482 (+ (* 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 name488 ;; 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 between496 ;; 0xD162 and 0xD31B revealed the pattern 0x144B exactly497 ;; six times.499 ;; the locations were:501 (def OT-ID-addresses [0xD176 0xD1A2 0xD1CE 0xD1FA 0xD228 0xD252])504 (defn set-pokemon-id505 ([^SaveState state n new-id]506 (assert (<= 0 n (dec (party-number state))))507 (assert (<= 0 new-id 0xFFFF))508 (set-memory-range509 state510 (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)))