Mercurial > vba-clojure
view org/rom.org @ 455:1c10fa8366a7
synchronized pony song.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Thu, 03 May 2012 10:28:05 -0500 |
parents | 13165fb5852b |
children | c02108ddcb35 |
line wrap: on
line source
1 #+title: Notes on Deconstructing Pokemon Yellow2 #+author: Dylan Holmes3 #+email: rlm@mit.edu4 #+description: A detailed explication of Pok\eacute{}mon Yellow, helped by Clojure.5 #+keywords: pokemon, pokemon yellow, rom, gameboy, assembly, hex, pointers, clojure6 #+SETUPFILE: ../../aurellem/org/setup.org7 #+INCLUDE: ../../aurellem/org/level-0.org8 #+BABEL: :exports both :noweb yes :cache no :mkdirp yes10 # about map headers http://datacrystal.romhacking.net/wiki/Pokemon_Red/Blue:Notes11 # map headers Yellow http://www.pokecommunity.com/archive/index.php/t-235311.html12 # pokedollar: U+20B113 * Introduction15 ** COMMENT Getting linguistic data: names, words, etc.17 Some of the simplest data20 One of the simplest data structures in the Pok\eacute{} ROM is an21 unbroken list of strings that either (a) all have a specific length,22 or (b) are all separated by the same character.24 Because lots of good data has this format, we'll start by writing a25 template function to extract it:27 #+name: hxc-thunks28 #+begin_src clojure :results silent29 (defn hxc-thunk30 "Creates a thunk (nullary fn) that grabs data in a certain region of rom and31 splits it into a collection by 0x50. If rom is not supplied, uses the32 original rom data."33 [start length]34 (fn self35 ([rom]36 (take-nth 237 (partition-by #(= % 0x50)38 (take length39 (drop start rom)))))40 ([]41 (self com.aurellem.gb.gb-driver/original-rom))))43 (def hxc-thunk-words44 "Same as hxc-thunk, except it interprets the rom data as characters,45 returning a collection of strings."46 (comp47 (partial comp (partial map character-codes->str))48 hxc-thunk))50 #+end_src53 * Pok\eacute{}mon I54 ** Names of each species55 The names of the Pok\eacute{}mon species are stored in56 ROM@E8000. This name list is interesting, for a number of reasons:57 - The names are stored in [[ ][internal order]] rather than in the familiar58 Pok\eacute{}dex order. This seemingly random order probably represents the order in which the authors created or59 programmed in the Pok\eacute{}mon; it's used throughout the game.60 - There is enough space allocated for 190 Pok\eacute{}mon. As I61 understand it, there were originally going to be 190 Pok\eacute{}mon62 in Generation I, but the creators decided to defer some to63 Generation II. This explains why many Gen I and Gen II Pok\eacute{}mon64 have the same aesthetic feel.65 - The list is pockmarked with random gaps, due to the strange internal66 ordering67 and the 39 unused spaces [fn::190 allocated spaces minus 151 true Pok\eacute{}mon]. These missing spaces are filled with the68 placeholder name =MISSINGNO.= (\ldquo{}Missing number\rdquo{}).70 Each name is exactly ten letters long; whenever a name would be too short, the extra71 space is padded with the character 0x50.73 *** See the data75 Here you can see the raw data in three stages: in the first stage, we76 just grab the first few bytes starting from position 0xE8000. In the77 second stage, we partition the bytes into ten-letter chunks to show you78 where the names begin and end. In the final stage, we convert each79 byte into the letter it represents using the =character-codes->str=80 function. (0x50 is rendered as the symbol \ldquo{} =#= \rdquo{} for81 ease of reading).83 #+begin_src clojure :exports both :cache no :results output84 (ns com.aurellem.gb.hxc85 (:use (com.aurellem.gb assembly characters gb-driver util mem-util86 constants))87 (:import [com.aurellem.gb.gb_driver SaveState]))90 (println (take 100 (drop 0xE8000 (rom))))92 (println (partition 10 (take 100 (drop 0xE8000 (rom)))))94 (println (character-codes->str (take 100 (drop 0xE8000 (rom)))))97 #+end_src99 #+results:100 : (145 135 152 131 142 141 80 80 80 80 138 128 141 134 128 146 138 135 128 141 141 136 131 142 145 128 141 239 80 80 130 139 132 133 128 136 145 152 80 80 146 143 132 128 145 142 150 80 80 80 149 142 139 147 142 145 129 80 80 80 141 136 131 142 138 136 141 134 80 80 146 139 142 150 129 145 142 80 80 80 136 149 152 146 128 148 145 80 80 80 132 151 132 134 134 148 147 142 145 80)101 : ((145 135 152 131 142 141 80 80 80 80) (138 128 141 134 128 146 138 135 128 141) (141 136 131 142 145 128 141 239 80 80) (130 139 132 133 128 136 145 152 80 80) (146 143 132 128 145 142 150 80 80 80) (149 142 139 147 142 145 129 80 80 80) (141 136 131 142 138 136 141 134 80 80) (146 139 142 150 129 145 142 80 80 80) (136 149 152 146 128 148 145 80 80 80) (132 151 132 134 134 148 147 142 145 80))102 : RHYDON####KANGASKHANNIDORAN♂##CLEFAIRY##SPEAROW###VOLTORB###NIDOKING##SLOWBRO###IVYSAUR###EXEGGUTOR#105 *** Automatically grab the data.107 #+name: pokenames108 #+begin_src clojure110 (defn hxc-pokenames-raw111 "The hardcoded names of the 190 species in memory. List begins at112 ROM@E8000. Although names in memory are padded with 0x50 to be 10 characters113 long, these names are stripped of padding. See also, hxc-pokedex-names"114 ([]115 (hxc-pokenames-raw com.aurellem.gb.gb-driver/original-rom))116 ([rom]117 (let [count-species 190118 name-length 10]119 (map character-codes->str120 (partition name-length121 (map #(if (= 0x50 %) 0x00 %)122 (take (* count-species name-length)123 (drop 0xE8000124 rom))))))))125 (def hxc-pokenames126 (comp127 (partial map format-name)128 hxc-pokenames-raw))133 (defn hxc-pokedex-names134 "The names of the pokemon in hardcoded pokedex order. List of the135 pokedex numbers of each pokemon (in internal order) begins at136 ROM@410B1. See also, hxc-pokenames."137 ([] (hxc-pokedex-names138 com.aurellem.gb.gb-driver/original-rom))139 ([rom]140 (let [names (hxc-pokenames rom)]141 (#(mapv %142 ((comp range count keys) %))143 (zipmap144 (take (count names)145 (drop 0x410b1 rom))147 names)))))149 #+end_src153 ** Generic species information155 #+name: pokebase156 #+begin_src clojure157 (defn hxc-pokemon-base158 ([] (hxc-pokemon-base com.aurellem.gb.gb-driver/original-rom))159 ([rom]160 (let [entry-size 28162 pokemon (rest (hxc-pokedex-names))163 pkmn-count (inc(count pokemon))164 types (apply assoc {}165 (interleave166 (range)167 pkmn-types)) ;;!! softcoded168 moves (apply assoc {}169 (interleave170 (range)171 (map format-name172 (hxc-move-names rom))))173 machines (hxc-machines)174 ]175 (zipmap176 pokemon177 (map178 (fn [[n179 rating-hp180 rating-atk181 rating-def182 rating-speed183 rating-special184 type-1185 type-2186 rarity187 rating-xp188 pic-dimensions ;; tile_width|tile_height (8px/tile)189 ptr-pic-obverse-1190 ptr-pic-obverse-2191 ptr-pic-reverse-1192 ptr-pic-reverse-2193 move-1194 move-2195 move-3196 move-4197 growth-rate198 &199 TMs|HMs]]200 (let201 [base-moves202 (mapv moves203 ((comp204 ;; since the game uses zero as a delimiter,205 ;; it must also increment all move indices by 1.206 ;; heren we decrement to correct this.207 (partial map dec)208 (partial take-while (comp not zero?)))209 [move-1 move-2 move-3 move-4]))211 types212 (set (list (types type-1)213 (types type-2)))214 TMs|HMs215 (map216 (comp217 (partial map first)218 (partial remove (comp zero? second)))219 (split-at220 50221 (map vector222 (rest(range))223 (reduce concat224 (map225 #(take 8226 (concat (bit-list %)227 (repeat 0)))229 TMs|HMs)))))231 TMs (vec (first TMs|HMs))232 HMs (take 5 (map (partial + -50) (vec (second TMs|HMs))))235 ]238 {:dex# n239 :base-moves base-moves240 :types types241 :TMs TMs242 :HMs HMs243 :base-hp rating-hp244 :base-atk rating-atk245 :base-def rating-def246 :base-speed rating-speed247 :base-special rating-special248 :o0 pic-dimensions249 :o1 ptr-pic-obverse-1250 :o2 ptr-pic-obverse-2251 }))253 (partition entry-size254 (take (* entry-size pkmn-count)255 (drop 0x383DE256 rom))))))))258 #+end_src261 ** Pok\eacute{}mon evolutions262 #+name: evolution-header263 #+begin_src clojure264 (defn format-evo265 "Parse a sequence of evolution data, returning a map. First is the266 method: 0 = end-evolution-data. 1 = level-up, 2 = item, 3 = trade. Next is an item id, if the267 method of evolution is by item (only stones will actually make pokemon268 evolve, for some auxillary reason.) Finally, the minimum level for269 evolution to occur (level 1 means no limit, which is used for trade270 and item evolutions), followed by the internal id of the pokemon271 into which to evolve. Hence, level up and trade evolutions are272 described with 3273 bytes; item evolutions with four."274 [coll]275 (let [method (first coll)]276 (cond (empty? coll) []277 (= 0 method) [] ;; just in case278 (= 1 method) ;; level-up evolution279 (conj (format-evo (drop 3 coll))280 {:method :level-up281 :min-level (nth coll 1)282 :into (dec (nth coll 2))})284 (= 2 method) ;; item evolution285 (conj (format-evo (drop 4 coll))286 {:method :item287 :item (dec (nth coll 1))288 :min-level (nth coll 2)289 :into (dec (nth coll 3))})291 (= 3 method) ;; trade evolution292 (conj (format-evo (drop 3 coll))293 {:method :trade294 :min-level (nth coll 1) ;; always 1 for trade.295 :into (dec (nth coll 2))}))))298 (defn hxc-ptrs-evolve299 "A hardcoded collection of 190 pointers to alternating evolution/learnset data,300 in internal order."301 ([]302 (hxc-ptrs-evolve com.aurellem.gb.gb-driver/original-rom))303 ([rom]304 (let [305 pkmn-count (count (hxc-pokenames-raw)) ;; 190306 ptrs307 (map (fn [[a b]] (low-high a b))308 (partition 2309 (take (* 2 pkmn-count)310 (drop 0x3b1e5 rom))))]311 (map (partial + 0x34000) ptrs)313 )))314 #+end_src316 #+name:evolution317 #+begin_src clojure319 (defn hxc-evolution320 "Hardcoded evolution data in memory. The data exists at ROM@34000,321 sorted by internal order. Pointers to the data exist at ROM@3B1E5; see also, hxc-ptrs-evolve."322 ([] (hxc-evolution com.aurellem.gb.gb-driver/original-rom))323 ([rom]324 (apply assoc {}325 (interleave326 (hxc-pokenames rom)327 (map328 (comp329 format-evo330 (partial take-while (comp not zero?))331 #(drop % rom))332 (hxc-ptrs-evolve rom)333 )))))335 (defn hxc-evolution-pretty336 "Like hxc-evolution, except it uses the names of items and pokemon337 --- grabbed from ROM --- rather than their numerical identifiers."338 ([] (hxc-evolution-pretty com.aurellem.gb.gb-driver/original-rom))339 ([rom]340 (let341 [poke-names (vec (hxc-pokenames rom))342 item-names (vec (hxc-items rom))343 use-names344 (fn [m]345 (loop [ks (keys m) new-map m]346 (let [k (first ks)]347 (cond (nil? ks) new-map348 (= k :into)349 (recur350 (next ks)351 (assoc new-map352 :into353 (poke-names354 (:into355 new-map))))356 (= k :item)357 (recur358 (next ks)359 (assoc new-map360 :item361 (item-names362 (:item new-map))))363 :else364 (recur365 (next ks)366 new-map)367 ))))]369 (into {}370 (map (fn [[pkmn evo-coll]]371 [pkmn (map use-names evo-coll)])372 (hxc-evolution rom))))))375 #+end_src378 ** Level-up moves (learnsets)379 #+name: learnsets380 #+begin_src clojure383 (defn hxc-learnsets384 "Hardcoded map associating pokemon names to lists of pairs [lvl385 move] of abilities they learn as they level up. The data386 exists at ROM@34000, sorted by internal order. Pointers to the data387 exist at ROM@3B1E5; see also, hxc-ptrs-evolve"388 ([] (hxc-learnsets com.aurellem.gb.gb-driver/original-rom))389 ([rom]390 (apply assoc391 {}392 (interleave393 (hxc-pokenames rom)394 (map (comp395 (partial map396 (fn [[lvl mv]] [lvl (dec mv)]))397 (partial partition 2)398 ;; keep the learnset data399 (partial take-while (comp not zero?))400 ;; skip the evolution data401 rest402 (partial drop-while (comp not zero?)))403 (map #(drop % rom)404 (hxc-ptrs-evolve rom)))))))406 (defn hxc-learnsets-pretty407 "Live hxc-learnsets except it reports the name of each move --- as408 it appears in rom --- rather than the move index."409 ([] (hxc-learnsets-pretty com.aurellem.gb.gb-driver/original-rom))410 ([rom]411 (let [moves (vec(map format-name (hxc-move-names)))]412 (into {}413 (map (fn [[pkmn learnset]]414 [pkmn (map (fn [[lvl mv]] [lvl (moves mv)])415 learnset)])416 (hxc-learnsets rom))))))420 #+end_src424 * Pok\eacute{}mon II : the Pok\eacute{}dex425 ** Species vital stats426 #+name: dex-stats427 #+begin_src clojure428 (defn hxc-pokedex-stats429 "The hardcoded pokedex stats (species height weight) in memory. List430 begins at ROM@40687"431 ([] (hxc-pokedex-stats com.aurellem.gb.gb-driver/original-rom))432 ([rom]433 (let [pokedex-names (zipmap (range) (hxc-pokedex-names rom))434 pkmn-count (count pokedex-names)435 ]436 ((fn capture-stats437 [n stats data]438 (if (zero? n) stats439 (let [[species440 [_441 height-ft442 height-in443 weight-1444 weight-2445 _446 dex-ptr-1447 dex-ptr-2448 dex-bank449 _450 & data]]451 (split-with (partial not= 0x50) data)]452 (recur (dec n)453 (assoc stats454 (pokedex-names (- pkmn-count (dec n)))455 {:species456 (format-name (character-codes->str species))457 :height-ft458 height-ft459 :height-in460 height-in461 :weight462 (/ (low-high weight-1 weight-2) 10.)464 ;; :text465 ;; (character-codes->str466 ;; (take-while467 ;; (partial not= 0x50)468 ;; (drop469 ;; (+ 0xB8000470 ;; -0x4000471 ;; (low-high dex-ptr-1 dex-ptr-2))472 ;; rom)))473 })475 data)478 )))480 pkmn-count481 {}482 (drop 0x40687 rom))) ))483 #+end_src485 #+results: dex-stats486 : #'com.aurellem.gb.hxc/hxc-pokedex-stats488 ** Species synopses490 #+name: dex-text491 #+begin_src clojure492 (def hxc-pokedex-text-raw493 "The hardcoded pokedex entries in memory. List begins at494 ROM@B8000, shortly before move names."495 (hxc-thunk-words 0xB8000 14754))500 (defn hxc-pokedex-text501 "The hardcoded pokedex entries in memory, presented as an502 associative hash map. List begins at ROM@B8000."503 ([] (hxc-pokedex-text com.aurellem.gb.gb-driver/original-rom))504 ([rom]505 (zipmap506 (hxc-pokedex-names rom)507 (cons nil ;; for missingno.508 (hxc-pokedex-text-raw rom)))))509 #+end_src512 ** Pok\eacute{}mon cries513 #+name: pokecry514 #+begin_src clojure515 (defn hxc-cry516 "The pokemon cry data in internal order. List begins at ROM@39462"517 ([](hxc-cry com.aurellem.gb.gb-driver/original-rom))518 ([rom]519 (zipmap520 (hxc-pokenames rom)521 (map522 (fn [[cry-id pitch length]]523 {:cry-id cry-id524 :pitch pitch525 :length length}526 )527 (partition 3528 (drop 0x39462 rom))))))530 (defn hxc-cry-groups531 ([] (hxc-cry-groups com.aurellem.gb.gb-driver/original-rom))532 ([rom]533 (map #(mapv first534 (filter535 (fn [[k v]]536 (= % (:cry-id v)))537 (hxc-cry)))538 ((comp539 range540 count541 set542 (partial map :cry-id)543 vals544 hxc-cry)545 rom))))548 (defn cry-conversion!549 "Convert Porygon's cry in ROM to be the cry of the given pokemon."550 [pkmn]551 (write-rom!552 (rewrite-memory553 (vec(rom))554 0x3965D555 (map second556 ((hxc-cry) pkmn)))))558 #+end_src560 ** COMMENT Names of permanent stats561 0DD4D-DD72563 * Items564 ** Item names566 *** See the data567 #+begin_src clojure :exports both :results output568 (ns com.aurellem.gb.hxc569 (:use (com.aurellem.gb assembly characters gb-driver util mem-util570 constants))571 (:import [com.aurellem.gb.gb_driver SaveState]))573 (println (take 100 (drop 0x045B7 (rom))))575 (println576 (partition-by577 (partial = 0x50)578 (take 100 (drop 0x045B7 (rom)))))580 (println581 (map character-codes->str582 (partition-by583 (partial = 0x50)584 (take 100 (drop 0x045B7 (rom))))))587 #+end_src589 #+results:590 : (140 128 146 147 132 145 127 129 128 139 139 80 148 139 147 145 128 127 129 128 139 139 80 134 145 132 128 147 127 129 128 139 139 80 143 142 138 186 127 129 128 139 139 80 147 142 150 141 127 140 128 143 80 129 136 130 152 130 139 132 80 230 230 230 230 230 80 146 128 133 128 145 136 127 129 128 139 139 80 143 142 138 186 131 132 151 80 140 142 142 141 127 146 147 142 141 132 80 128 141)591 : ((140 128 146 147 132 145 127 129 128 139 139) (80) (148 139 147 145 128 127 129 128 139 139) (80) (134 145 132 128 147 127 129 128 139 139) (80) (143 142 138 186 127 129 128 139 139) (80) (147 142 150 141 127 140 128 143) (80) (129 136 130 152 130 139 132) (80) (230 230 230 230 230) (80) (146 128 133 128 145 136 127 129 128 139 139) (80) (143 142 138 186 131 132 151) (80) (140 142 142 141 127 146 147 142 141 132) (80) (128 141))592 : (MASTER BALL # ULTRA BALL # GREAT BALL # POKé BALL # TOWN MAP # BICYCLE # ????? # SAFARI BALL # POKéDEX # MOON STONE # AN)594 *** Automatically grab the data595 #+name: item-names596 #+begin_src clojure598 (def hxc-items-raw599 "The hardcoded names of the items in memory. List begins at600 ROM@045B7"601 (hxc-thunk-words 0x45B7 870))603 (def hxc-items604 "The hardcoded names of the items in memory, presented as605 keywords. List begins at ROM@045B7. See also, hxc-items-raw."606 (comp (partial map format-name) hxc-items-raw))607 #+end_src609 ** Item prices611 ***612 #+begin_src clojure :exports both :results output613 (ns com.aurellem.gb.hxc614 (:use (com.aurellem.gb assembly characters gb-driver util mem-util615 constants))616 (:import [com.aurellem.gb.gb_driver SaveState]))618 (println (take 90 (drop 0x4495 (rom))))620 (println621 (partition 3622 (take 90 (drop 0x4495 (rom)))))624 (println625 (partition 3626 (map hex627 (take 90 (drop 0x4495 (rom))))))629 (println630 (map decode-bcd631 (map butlast632 (partition 3633 (take 90 (drop 0x4495 (rom)))))))635 (println636 (map637 vector638 (hxc-items (rom))639 (map decode-bcd640 (map butlast641 (partition 3642 (take 90 (drop 0x4495 (rom))))))))648 #+end_src650 #+results:651 : (0 0 0 18 0 0 6 0 0 2 0 0 0 0 0 0 0 0 0 0 0 16 0 0 0 0 0 0 0 0 1 0 0 2 80 0 2 80 0 2 0 0 2 0 0 48 0 0 37 0 0 21 0 0 7 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 80 0 3 80 0)652 : ((0 0 0) (18 0 0) (6 0 0) (2 0 0) (0 0 0) (0 0 0) (0 0 0) (16 0 0) (0 0 0) (0 0 0) (1 0 0) (2 80 0) (2 80 0) (2 0 0) (2 0 0) (48 0 0) (37 0 0) (21 0 0) (7 0 0) (3 0 0) (0 0 0) (0 0 0) (0 0 0) (0 0 0) (0 0 0) (0 0 0) (0 0 0) (0 0 0) (5 80 0) (3 80 0))653 : ((0x0 0x0 0x0) (0x12 0x0 0x0) (0x6 0x0 0x0) (0x2 0x0 0x0) (0x0 0x0 0x0) (0x0 0x0 0x0) (0x0 0x0 0x0) (0x10 0x0 0x0) (0x0 0x0 0x0) (0x0 0x0 0x0) (0x1 0x0 0x0) (0x2 0x50 0x0) (0x2 0x50 0x0) (0x2 0x0 0x0) (0x2 0x0 0x0) (0x30 0x0 0x0) (0x25 0x0 0x0) (0x15 0x0 0x0) (0x7 0x0 0x0) (0x3 0x0 0x0) (0x0 0x0 0x0) (0x0 0x0 0x0) (0x0 0x0 0x0) (0x0 0x0 0x0) (0x0 0x0 0x0) (0x0 0x0 0x0) (0x0 0x0 0x0) (0x0 0x0 0x0) (0x5 0x50 0x0) (0x3 0x50 0x0))654 : (0 1200 600 200 0 0 0 1000 0 0 100 250 250 200 200 3000 2500 1500 700 300 0 0 0 0 0 0 0 0 550 350)655 : ([:master-ball 0] [:ultra-ball 1200] [:great-ball 600] [:poké-ball 200] [:town-map 0] [:bicycle 0] [:????? 0] [:safari-ball 1000] [:pokédex 0] [:moon-stone 0] [:antidote 100] [:burn-heal 250] [:ice-heal 250] [:awakening 200] [:parlyz-heal 200] [:full-restore 3000] [:max-potion 2500] [:hyper-potion 1500] [:super-potion 700] [:potion 300] [:boulderbadge 0] [:cascadebadge 0] [:thunderbadge 0] [:rainbowbadge 0] [:soulbadge 0] [:marshbadge 0] [:volcanobadge 0] [:earthbadge 0] [:escape-rope 550] [:repel 350])658 ***659 #+name: item-prices660 #+begin_src clojure661 (defn hxc-item-prices662 "The hardcoded list of item prices in memory. List begins at ROM@4495"663 ([] (hxc-item-prices com.aurellem.gb.gb-driver/original-rom))664 ([rom]665 (let [items (hxc-items rom)666 price-size 3]667 (zipmap items668 (map (comp669 ;; zero-cost items are "priceless"670 #(if (zero? %) :priceless %)671 decode-bcd butlast)672 (partition price-size673 (take (* price-size (count items))674 (drop 0x4495 rom))))))))675 #+end_src676 ** Vendor inventories678 #+name: item-vendors679 #+begin_src clojure680 (defn hxc-shops681 ([] (hxc-shops com.aurellem.gb.gb-driver/original-rom))682 ([rom]683 (let [items (zipmap (range) (hxc-items rom))685 ;; temporarily softcode the TM items686 items (into687 items688 (map (juxt identity689 (comp keyword690 (partial str "tm-")691 (partial + 1 -200)692 ))693 (take 200 (drop 200 (range)))))695 ]697 ((fn parse-shop [coll [num-items & items-etc]]698 (let [inventory (take-while699 (partial not= 0xFF)700 items-etc)701 [separator & items-etc] (drop num-items (rest items-etc))]702 (if (= separator 0x50)703 (map (partial mapv (comp items dec)) (conj coll inventory))704 (recur (conj coll inventory) items-etc)705 )706 ))708 '()709 (drop 0x233C rom))712 )))713 #+end_src715 #+results: item-vendors716 : #'com.aurellem.gb.hxc/hxc-shops720 * Types721 ** Names of types723 *** COMMENT Pointers to type names724 #+begin_src clojure :exports both :results output725 (map (comp character-codes->str #(take-while (partial not= 80) (drop % (rom))) (partial + 0x20000) (partial apply low-high)) (partition 2 (take 54 (drop 0x27D63 (rom)))))726 #+end_src729 ***730 #+begin_src clojure :exports both :results output731 (ns com.aurellem.gb.hxc732 (:use (com.aurellem.gb assembly characters gb-driver util mem-util733 constants))734 (:import [com.aurellem.gb.gb_driver SaveState]))736 (println (take 90 (drop 0x27D99 (rom))))738 (println739 (partition-by (partial = 0x50)740 (take 90 (drop 0x27D99 (rom)))))742 (println743 (map character-codes->str744 (partition-by (partial = 0x50)745 (take 90 (drop 0x27D99 (rom))))))747 #+end_src749 #+results:750 : (141 142 145 140 128 139 80 133 136 134 135 147 136 141 134 80 133 139 152 136 141 134 80 143 142 136 146 142 141 80 133 136 145 132 80 150 128 147 132 145 80 134 145 128 146 146 80 132 139 132 130 147 145 136 130 80 143 146 152 130 135 136 130 80 136 130 132 80 134 145 142 148 141 131 80 145 142 130 138 80 129 136 145 131 80 129 148 134 80 134)751 : ((141 142 145 140 128 139) (80) (133 136 134 135 147 136 141 134) (80) (133 139 152 136 141 134) (80) (143 142 136 146 142 141) (80) (133 136 145 132) (80) (150 128 147 132 145) (80) (134 145 128 146 146) (80) (132 139 132 130 147 145 136 130) (80) (143 146 152 130 135 136 130) (80) (136 130 132) (80) (134 145 142 148 141 131) (80) (145 142 130 138) (80) (129 136 145 131) (80) (129 148 134) (80) (134))752 : (NORMAL # FIGHTING # FLYING # POISON # FIRE # WATER # GRASS # ELECTRIC # PSYCHIC # ICE # GROUND # ROCK # BIRD # BUG # G)755 ***756 #+name: type-names757 #+begin_src clojure758 (def hxc-types759 "The hardcoded type names in memory. List begins at ROM@27D99,760 shortly before hxc-titles."761 (hxc-thunk-words 0x27D99 102))763 #+end_src765 ** Type effectiveness766 ***767 #+begin_src clojure :exports both :results output768 (ns com.aurellem.gb.hxc769 (:use (com.aurellem.gb assembly characters gb-driver util mem-util770 constants))771 (:import [com.aurellem.gb.gb_driver SaveState]))774 ;; POKEMON TYPES776 (println pkmn-types) ;; these are the pokemon types777 (println (map vector (range) pkmn-types)) ;; each type has an id number.779 (newline)784 ;;; TYPE EFFECTIVENESS786 (println (take 15 (drop 0x3E62D (rom))))787 (println (partition 3 (take 15 (drop 0x3E62D (rom)))))789 (println790 (map791 (fn [[atk-type def-type multiplier]]792 (list atk-type def-type (/ multiplier 10.)))794 (partition 3795 (take 15 (drop 0x3E62D (rom))))))798 (println799 (map800 (fn [[atk-type def-type multiplier]]801 [802 (get pkmn-types atk-type)803 (get pkmn-types def-type)804 (/ multiplier 10.)805 ])807 (partition 3808 (take 15 (drop 0x3E62D (rom))))))810 #+end_src812 #+results:813 : [:normal :fighting :flying :poison :ground :rock :bird :bug :ghost :A :B :C :D :E :F :G :H :I :J :K :fire :water :grass :electric :psychic :ice :dragon]814 : ([0 :normal] [1 :fighting] [2 :flying] [3 :poison] [4 :ground] [5 :rock] [6 :bird] [7 :bug] [8 :ghost] [9 :A] [10 :B] [11 :C] [12 :D] [13 :E] [14 :F] [15 :G] [16 :H] [17 :I] [18 :J] [19 :K] [20 :fire] [21 :water] [22 :grass] [23 :electric] [24 :psychic] [25 :ice] [26 :dragon])815 :816 : (0 5 5 0 8 0 8 8 20 20 7 20 20 5 5)817 : ((0 5 5) (0 8 0) (8 8 20) (20 7 20) (20 5 5))818 : ((0 5 0.5) (0 8 0.0) (8 8 2.0) (20 7 2.0) (20 5 0.5))819 : ([:normal :rock 0.5] [:normal :ghost 0.0] [:ghost :ghost 2.0] [:fire :bug 2.0] [:fire :rock 0.5])822 ***824 #+name: type-advantage825 #+begin_src clojure826 (defn hxc-advantage827 ;; in-game multipliers are stored as 10x their effective value828 ;; to allow for fractional multipliers like 1/2830 "The hardcoded type advantages in memory, returned as tuples of831 atk-type def-type multiplier. By default (i.e. if not listed here),832 the multiplier is 1. List begins at 0x3E62D."833 ([] (hxc-advantage com.aurellem.gb.gb-driver/original-rom))834 ([rom]835 (map836 (fn [[atk def mult]] [(get pkmn-types atk (hex atk))837 (get pkmn-types def (hex def))838 (/ mult 10)])839 (partition 3840 (take-while (partial not= 0xFF)841 (drop 0x3E62D rom))))))842 #+end_src846 * Moves847 ** Names of moves848 *** See the data849 #+begin_src clojure :exports both :results output850 (ns com.aurellem.gb.hxc851 (:use (com.aurellem.gb assembly characters gb-driver util mem-util852 constants))853 (:import [com.aurellem.gb.gb_driver SaveState]))855 (println (take 100 (drop 0xBC000 (rom))))857 (println858 (partition-by859 (partial = 0x50)860 (take 100 (drop 0xBC000 (rom)))))862 (println863 (map character-codes->str864 (partition-by865 (partial = 0x50)866 (take 100 (drop 0xBC000 (rom))))))869 #+end_src871 #+results:872 : (143 142 148 141 131 80 138 128 145 128 147 132 127 130 135 142 143 80 131 142 148 129 139 132 146 139 128 143 80 130 142 140 132 147 127 143 148 141 130 135 80 140 132 134 128 127 143 148 141 130 135 80 143 128 152 127 131 128 152 80 133 136 145 132 127 143 148 141 130 135 80 136 130 132 127 143 148 141 130 135 80 147 135 148 141 131 132 145 143 148 141 130 135 80 146 130 145 128 147 130)873 : ((143 142 148 141 131) (80) (138 128 145 128 147 132 127 130 135 142 143) (80) (131 142 148 129 139 132 146 139 128 143) (80) (130 142 140 132 147 127 143 148 141 130 135) (80) (140 132 134 128 127 143 148 141 130 135) (80) (143 128 152 127 131 128 152) (80) (133 136 145 132 127 143 148 141 130 135) (80) (136 130 132 127 143 148 141 130 135) (80) (147 135 148 141 131 132 145 143 148 141 130 135) (80) (146 130 145 128 147 130))874 : (POUND # KARATE CHOP # DOUBLESLAP # COMET PUNCH # MEGA PUNCH # PAY DAY # FIRE PUNCH # ICE PUNCH # THUNDERPUNCH # SCRATC)876 *** Automatically grab the data878 #+name: move-names879 #+begin_src clojure880 (def hxc-move-names881 "The hardcoded move names in memory. List begins at ROM@BC000"882 (hxc-thunk-words 0xBC000 1551))883 #+end_src885 ** Properties of moves887 #+name: move-data888 #+begin_src clojure889 (defn hxc-move-data890 "The hardcoded (basic (move effects)) in memory. List begins at891 0x38000. Returns a map of {:name :power :accuracy :pp :fx-id892 :fx-txt}. The move descriptions are handwritten, not hardcoded."893 ([]894 (hxc-move-data com.aurellem.gb.gb-driver/original-rom))895 ([rom]896 (let [names (vec (hxc-move-names rom))897 move-count (count names)898 move-size 6899 types pkmn-types ;;; !! hardcoded types900 ]901 (zipmap (map format-name names)902 (map903 (fn [[idx effect power type-id accuracy pp]]904 {:name (names (dec idx))905 :power power906 :accuracy accuracy907 :pp pp908 :type (types type-id)909 :fx-id effect910 :fx-txt (get move-effects effect)911 }912 )914 (partition move-size915 (take (* move-size move-count)916 (drop 0x38000 rom))))))))920 (defn hxc-move-data*921 "Like hxc-move-data, but reports numbers as hexadecimal symbols instead."922 ([]923 (hxc-move-data* com.aurellem.gb.gb-driver/original-rom))924 ([rom]925 (let [names (vec (hxc-move-names rom))926 move-count (count names)927 move-size 6928 format-name (fn [s]929 (keyword (.toLowerCase930 (apply str931 (map #(if (= % \space) "-" %) s)))))932 ]933 (zipmap (map format-name names)934 (map935 (fn [[idx effect power type accuracy pp]]936 {:name (names (dec idx))937 :power power938 :accuracy (hex accuracy)939 :pp pp940 :fx-id (hex effect)941 :fx-txt (get move-effects effect)942 }943 )945 (partition move-size946 (take (* move-size move-count)947 (drop 0x38000 rom))))))))949 #+end_src951 ** TM and HM moves952 ***953 #+begin_src clojure :exports both :results output954 (ns com.aurellem.gb.hxc955 (:use (com.aurellem.gb assembly characters gb-driver util mem-util956 constants))957 (:import [com.aurellem.gb.gb_driver SaveState]))960 (println (hxc-move-names))961 (println (map vector (rest(range)) (hxc-move-names)))963 (newline)965 (println (take 55 (drop 0x1232D (rom))))967 (println968 (interpose "."969 (map970 (zipmap (rest (range)) (hxc-move-names))971 (take 55 (drop 0x1232D (rom))))))973 #+end_src975 #+results:976 : (POUND KARATE CHOP DOUBLESLAP COMET PUNCH MEGA PUNCH PAY DAY FIRE PUNCH ICE PUNCH THUNDERPUNCH SCRATCH VICEGRIP GUILLOTINE RAZOR WIND SWORDS DANCE CUT GUST WING ATTACK WHIRLWIND FLY BIND SLAM VINE WHIP STOMP DOUBLE KICK MEGA KICK JUMP KICK ROLLING KICK SAND-ATTACK HEADBUTT HORN ATTACK FURY ATTACK HORN DRILL TACKLE BODY SLAM WRAP TAKE DOWN THRASH DOUBLE-EDGE TAIL WHIP POISON STING TWINEEDLE PIN MISSILE LEER BITE GROWL ROAR SING SUPERSONIC SONICBOOM DISABLE ACID EMBER FLAMETHROWER MIST WATER GUN HYDRO PUMP SURF ICE BEAM BLIZZARD PSYBEAM BUBBLEBEAM AURORA BEAM HYPER BEAM PECK DRILL PECK SUBMISSION LOW KICK COUNTER SEISMIC TOSS STRENGTH ABSORB MEGA DRAIN LEECH SEED GROWTH RAZOR LEAF SOLARBEAM POISONPOWDER STUN SPORE SLEEP POWDER PETAL DANCE STRING SHOT DRAGON RAGE FIRE SPIN THUNDERSHOCK THUNDERBOLT THUNDER WAVE THUNDER ROCK THROW EARTHQUAKE FISSURE DIG TOXIC CONFUSION PSYCHIC HYPNOSIS MEDITATE AGILITY QUICK ATTACK RAGE TELEPORT NIGHT SHADE MIMIC SCREECH DOUBLE TEAM RECOVER HARDEN MINIMIZE SMOKESCREEN CONFUSE RAY WITHDRAW DEFENSE CURL BARRIER LIGHT SCREEN HAZE REFLECT FOCUS ENERGY BIDE METRONOME MIRROR MOVE SELFDESTRUCT EGG BOMB LICK SMOG SLUDGE BONE CLUB FIRE BLAST WATERFALL CLAMP SWIFT SKULL BASH SPIKE CANNON CONSTRICT AMNESIA KINESIS SOFTBOILED HI JUMP KICK GLARE DREAM EATER POISON GAS BARRAGE LEECH LIFE LOVELY KISS SKY ATTACK TRANSFORM BUBBLE DIZZY PUNCH SPORE FLASH PSYWAVE SPLASH ACID ARMOR CRABHAMMER EXPLOSION FURY SWIPES BONEMERANG REST ROCK SLIDE HYPER FANG SHARPEN CONVERSION TRI ATTACK SUPER FANG SLASH SUBSTITUTE STRUGGLE)977 : ([1 POUND] [2 KARATE CHOP] [3 DOUBLESLAP] [4 COMET PUNCH] [5 MEGA PUNCH] [6 PAY DAY] [7 FIRE PUNCH] [8 ICE PUNCH] [9 THUNDERPUNCH] [10 SCRATCH] [11 VICEGRIP] [12 GUILLOTINE] [13 RAZOR WIND] [14 SWORDS DANCE] [15 CUT] [16 GUST] [17 WING ATTACK] [18 WHIRLWIND] [19 FLY] [20 BIND] [21 SLAM] [22 VINE WHIP] [23 STOMP] [24 DOUBLE KICK] [25 MEGA KICK] [26 JUMP KICK] [27 ROLLING KICK] [28 SAND-ATTACK] [29 HEADBUTT] [30 HORN ATTACK] [31 FURY ATTACK] [32 HORN DRILL] [33 TACKLE] [34 BODY SLAM] [35 WRAP] [36 TAKE DOWN] [37 THRASH] [38 DOUBLE-EDGE] [39 TAIL WHIP] [40 POISON STING] [41 TWINEEDLE] [42 PIN MISSILE] [43 LEER] [44 BITE] [45 GROWL] [46 ROAR] [47 SING] [48 SUPERSONIC] [49 SONICBOOM] [50 DISABLE] [51 ACID] [52 EMBER] [53 FLAMETHROWER] [54 MIST] [55 WATER GUN] [56 HYDRO PUMP] [57 SURF] [58 ICE BEAM] [59 BLIZZARD] [60 PSYBEAM] [61 BUBBLEBEAM] [62 AURORA BEAM] [63 HYPER BEAM] [64 PECK] [65 DRILL PECK] [66 SUBMISSION] [67 LOW KICK] [68 COUNTER] [69 SEISMIC TOSS] [70 STRENGTH] [71 ABSORB] [72 MEGA DRAIN] [73 LEECH SEED] [74 GROWTH] [75 RAZOR LEAF] [76 SOLARBEAM] [77 POISONPOWDER] [78 STUN SPORE] [79 SLEEP POWDER] [80 PETAL DANCE] [81 STRING SHOT] [82 DRAGON RAGE] [83 FIRE SPIN] [84 THUNDERSHOCK] [85 THUNDERBOLT] [86 THUNDER WAVE] [87 THUNDER] [88 ROCK THROW] [89 EARTHQUAKE] [90 FISSURE] [91 DIG] [92 TOXIC] [93 CONFUSION] [94 PSYCHIC] [95 HYPNOSIS] [96 MEDITATE] [97 AGILITY] [98 QUICK ATTACK] [99 RAGE] [100 TELEPORT] [101 NIGHT SHADE] [102 MIMIC] [103 SCREECH] [104 DOUBLE TEAM] [105 RECOVER] [106 HARDEN] [107 MINIMIZE] [108 SMOKESCREEN] [109 CONFUSE RAY] [110 WITHDRAW] [111 DEFENSE CURL] [112 BARRIER] [113 LIGHT SCREEN] [114 HAZE] [115 REFLECT] [116 FOCUS ENERGY] [117 BIDE] [118 METRONOME] [119 MIRROR MOVE] [120 SELFDESTRUCT] [121 EGG BOMB] [122 LICK] [123 SMOG] [124 SLUDGE] [125 BONE CLUB] [126 FIRE BLAST] [127 WATERFALL] [128 CLAMP] [129 SWIFT] [130 SKULL BASH] [131 SPIKE CANNON] [132 CONSTRICT] [133 AMNESIA] [134 KINESIS] [135 SOFTBOILED] [136 HI JUMP KICK] [137 GLARE] [138 DREAM EATER] [139 POISON GAS] [140 BARRAGE] [141 LEECH LIFE] [142 LOVELY KISS] [143 SKY ATTACK] [144 TRANSFORM] [145 BUBBLE] [146 DIZZY PUNCH] [147 SPORE] [148 FLASH] [149 PSYWAVE] [150 SPLASH] [151 ACID ARMOR] [152 CRABHAMMER] [153 EXPLOSION] [154 FURY SWIPES] [155 BONEMERANG] [156 REST] [157 ROCK SLIDE] [158 HYPER FANG] [159 SHARPEN] [160 CONVERSION] [161 TRI ATTACK] [162 SUPER FANG] [163 SLASH] [164 SUBSTITUTE] [165 STRUGGLE])978 :979 : (5 13 14 18 25 92 32 34 36 38 61 55 58 59 63 6 66 68 69 99 72 76 82 85 87 89 90 91 94 100 102 104 115 117 118 120 121 126 129 130 135 138 143 156 86 149 153 157 161 164 15 19 57 70 148)980 : (MEGA PUNCH . RAZOR WIND . SWORDS DANCE . WHIRLWIND . MEGA KICK . TOXIC . HORN DRILL . BODY SLAM . TAKE DOWN . DOUBLE-EDGE . BUBBLEBEAM . WATER GUN . ICE BEAM . BLIZZARD . HYPER BEAM . PAY DAY . SUBMISSION . COUNTER . SEISMIC TOSS . RAGE . MEGA DRAIN . SOLARBEAM . DRAGON RAGE . THUNDERBOLT . THUNDER . EARTHQUAKE . FISSURE . DIG . PSYCHIC . TELEPORT . MIMIC . DOUBLE TEAM . REFLECT . BIDE . METRONOME . SELFDESTRUCT . EGG BOMB . FIRE BLAST . SWIFT . SKULL BASH . SOFTBOILED . DREAM EATER . SKY ATTACK . REST . THUNDER WAVE . PSYWAVE . EXPLOSION . ROCK SLIDE . TRI ATTACK . SUBSTITUTE . CUT . FLY . SURF . STRENGTH . FLASH)983 ***984 #+name: machines985 #+begin_src clojure986 (defn hxc-machines987 "The hardcoded moves taught by TMs and HMs. List begins at ROM@1232D."988 ([] (hxc-machines989 com.aurellem.gb.gb-driver/original-rom))990 ([rom]991 (let [moves (hxc-move-names rom)]992 (zipmap993 (range)994 (take-while995 (comp not nil?)996 (map (comp997 format-name998 (zipmap999 (range)1000 moves)1001 dec)1002 (take 1001003 (drop 0x1232D rom))))))))1005 #+end_src1011 ** COMMENT Status ailments1014 * NPC Trainers1016 ** Trainer Pok\eacute{}mon1017 # http://hax.iimarck.us/topic/103/1018 There are two formats for specifying lists of NPC PPok\eacute{}mon:1019 - If all the Pok\eacute{}mon will have the same level, the format is1020 - Level (used for all the Pok\eacute{}mon)1021 - Any number of Pok\eacute{}mon internal ids.1022 - 0x00, to indicate end-of-list.1023 - Otherwise, all the Pok\eacute{}mon will have their level1024 specified. The format is1025 - 0xFF, to indicate that we will be specifying the levels individually[fn::Because 0xFF is a1026 forbidden level within the usual gameplay discipline, the game1027 makers could safely use 0xFF as a mode indicator.].1028 - Any number of alternating Level/Pokemon pairs1029 - 0x00, to indicate end-of-list.1031 *** Get the pointers1032 *** See the data1033 #+begin_src clojure :exports both :results output1034 (ns com.aurellem.gb.hxc1035 (:use (com.aurellem.gb assembly characters gb-driver util mem-util1036 constants))1037 (:import [com.aurellem.gb.gb_driver SaveState]))1039 (->>1040 (rom)1041 (drop 0x39E2F)1042 (take 21)1043 (println))1046 (->>1047 (rom)1048 (drop 0x39E2F)1049 (take 21)1050 (partition-by zero?)1051 (take-nth 2)1052 (println))1056 (let1057 [pokenames1058 (zipmap1059 (rest (range))1060 (hxc-pokenames-raw))]1062 (->>1063 (rom)1064 (drop 0x39E2F)1065 (take 21) ;; (1922 in all)1066 (partition-by zero?)1067 (take-nth 2)1068 (map1069 (fn parse-team [[mode & team]]1070 (if (not= 0xFF mode)1071 (mapv1072 #(hash-map :level mode :species (pokenames %))1073 team)1075 (mapv1076 (fn [[lvl id]] (hash-map :level lvl :species (pokenames id)))1077 (partition 2 team)))))1079 (println)))1083 #+end_src1085 #+results:1086 : (11 165 108 0 14 5 0 10 165 165 107 0 14 165 108 107 0 15 165 5 0)1087 : ((11 165 108) (14 5) (10 165 165 107) (14 165 108 107) (15 165 5))1088 : ([{:species RATTATA, :level 11} {:species EKANS, :level 11}] [{:species SPEAROW, :level 14}] [{:species RATTATA, :level 10} {:species RATTATA, :level 10} {:species ZUBAT, :level 10}] [{:species RATTATA, :level 14} {:species EKANS, :level 14} {:species ZUBAT, :level 14}] [{:species RATTATA, :level 15} {:species SPEAROW, :level 15}])1090 * Places1091 ** Names of places1093 #+name: places1094 #+begin_src clojure1095 (def hxc-places1096 "The hardcoded place names in memory. List begins at1097 ROM@71500. [Cinnabar/Celadon] Mansion seems to be dynamically calculated."1098 (hxc-thunk-words 0x71500 560))1099 #+end_src1101 *** See it work1102 #+begin_src clojure :exports both :results output1103 (println (hxc-places))1104 #+end_src1106 #+results:1107 : (PALLET TOWN VIRIDIAN CITY PEWTER CITY CERULEAN CITY LAVENDER TOWN VERMILION CITY CELADON CITY FUCHSIA CITY CINNABAR ISLAND INDIGO PLATEAU SAFFRON CITY ROUTE 1 ROUTE 2 ROUTE 3 ROUTE 4 ROUTE 5 ROUTE 6 ROUTE 7 ROUTE 8 ROUTE 9 ROUTE 10 ROUTE 11 ROUTE 12 ROUTE 13 ROUTE 14 ROUTE 15 ROUTE 16 ROUTE 17 ROUTE 18 SEA ROUTE 19 SEA ROUTE 20 SEA ROUTE 21 ROUTE 22 ROUTE 23 ROUTE 24 ROUTE 25 VIRIDIAN FOREST MT.MOON ROCK TUNNEL SEA COTTAGE S.S.ANNE [POKE]MON LEAGUE UNDERGROUND PATH [POKE]MON TOWER SEAFOAM ISLANDS VICTORY ROAD DIGLETT's CAVE ROCKET HQ SILPH CO. [0x4A] MANSION SAFARI ZONE)1109 ** Wild Pok\eacute{}mon demographics1110 #+name: wilds1111 #+begin_src clojure1115 (defn hxc-ptrs-wild1116 "A list of the hardcoded wild encounter data in memory. Pointers1117 begin at ROM@0CB95; data begins at ROM@0x04D89"1118 ([] (hxc-ptrs-wild com.aurellem.gb.gb-driver/original-rom))1119 ([rom]1120 (let [ptrs1121 (map (fn [[a b]] (+ a (* 0x100 b)))1122 (take-while (partial not= (list 0xFF 0xFF))1123 (partition 2 (drop 0xCB95 rom))))]1124 ptrs)))1128 (defn hxc-wilds1129 "A list of the hardcoded wild encounter data in memory. Pointers1130 begin at ROM@0CB95; data begins at ROM@0x04D89"1131 ([] (hxc-wilds com.aurellem.gb.gb-driver/original-rom))1132 ([rom]1133 (let [pokenames (zipmap (range) (hxc-pokenames rom))]1134 (map1135 (partial map (fn [[a b]] {:species (pokenames (dec b)) :level1136 a}))1137 (partition 101139 (take-while (comp (partial not= 1)1140 first)1141 (partition 21142 (drop 0xCD8C rom))1144 ))))))1146 #+end_src1151 ** Map data1153 # http://www.pokecommunity.com/showthread.php?t=2353111154 # http://datacrystal.romhacking.net/wiki/Pokemon_Red/Blue:Notes1156 #+name map1157 #+begin_src clojure :exports both :results output1158 (ns com.aurellem.gb.hxc1159 (:use (com.aurellem.gb assembly characters gb-driver util mem-util1160 constants))1161 (:import [com.aurellem.gb.gb_driver SaveState]))1164 (defn parse-header-tileset1165 [[bank# ;; memory bank for blocks & tileset1167 blocks-lo ;; structure1168 blocks-hi1170 tileset-lo ;; style1171 tileset-hi1173 collision-lo ;; collision info1174 collision-hi1176 talk-here-1 ;; positions of up to three1177 talk-here-2 ;; talk-over-countertop tiles1178 talk-here-3 ;; --- 0xFF if unused.1180 grass ;; grass tile --- 0xFF if unused1182 animation-flags ;; settings for animation1183 & _]]1185 [bank#1187 blocks-lo ;; structure1188 blocks-hi1190 tileset-lo ;; style1191 tileset-hi1193 collision-lo ;; collision info1194 collision-hi1196 talk-here-1 ;; positions of up to three1197 talk-here-2 ;; talk-over-countertop tiles1198 talk-here-3 ;; --- 0xFF if unused.1200 grass ;; grass tile --- 0xFF if unused1202 animation-flags ;; settings for animation1203 ])1207 (defn parse-header-map1208 [start]1210 (let [connection-size 111212 [tileset-index1213 map-height1214 map-width1215 layout-lo1216 layout-hi1217 text-lo1218 text-hi1219 script-lo1220 script-hi1221 adjacency-flags ;; x x x x N S W E1222 & etc]1223 (drop start (rom))1225 [east? west? south? north?]1226 (bit-list adjacency-flags)1228 [connections object-data]1229 (split-at1230 (* connection-size (+ east? west? south? north?))1231 etc)1233 connections1234 (partition connection-size connections)1239 ]1240 (ptr->offset1241 31242 (low-high layout-lo layout-hi))1245 ))1246 #+end_src1248 #+results:1249 :1253 * Appendices1254 ** Mapping the ROM1255 # D3AD: Script:Use Pokeball?1257 | ROM address (hex) | Description | Format | Example |1258 |-----------------------+-----------------+-----------------+-----------------|1259 | | <15> | <15> | <15> |1260 | 01823-0184A | Important prefix strings. | Variable-length strings, separated by 0x50. | TM#TRAINER#PC#ROCKET#POK\eacute{}#... |1261 | 0233C- | Shop inventories. | | |1262 | 02F47- | (?) Move ids of some HM moves. | One byte per move id | 0x0F 0x13 0x39 0x46 0x94 0xFF, the move ids of CUT, FLY, SURF, STRENGTH, FLASH, then cancel. |1263 | 03E59- | Start of the Give-Pok\eacute{}mon script. | Assembly. When called, converts the contents of register BC into a Pok\eacute{}mon: (B) is the level; (C) is the species. | |1264 | 03E3F- | Start of the give-item script. | Assembly. When called, converts the contents of register BC into an item: (B) is the item type; (C) is the quantity. | |1265 | 04495- | Prices of items. | Each price is two bytes of binary-coded decimal. Prices are separated by zeroes. Priceless items[fn::Like the Pok\eacute{}dex and other unsellable items.] are given a price of zero. | The cost of lemonade is 0x03 0x50, which translates to a price of ₱350. |1266 | 04524-04527 | (unconfirmed) possibly the bike price in Cerulean. | | |1267 | 045B7-0491E | Names of the items in memory. | Variable-length item names (strings of character codes). Names are separated by a single 0x50 character. | MASTER BALL#ULTRA BALL#... |1268 | 04D89- | Lists of wild Pok\eacute{}mon to encounter in each region. | Each list contains ten Pokemon (ids) and their levels; twenty bytes in total. First, the level of the first Pokemon. Then the internal id of the first Pokemon. Next, the level of the second Pokemon, and so on. Since Pokemon cannot have level 0, the lists are separated by a pair 0 /X/, where /X/ is an apparently random Pokemon id. | The first list is (3 36 4 36 2 165 3 165 2 36 3 36 5 36 4 165 6 36 7 36 0 25), i.e. level 3 pidgey, level 4 pidgey, level 2 rattata, level 3 rattata, level 2 pidgey, level 3 pidgey, level 5 pidgey, level 4 rattata, level 6 pidgey, level 7 pidgey, \ldquo{}level 0 gastly\rdquo{} (i.e., end-of-list). |1269 |-----------------------+-----------------+-----------------+-----------------|1270 | 05DD2-05DF2 | Menu text for player info. | | PLAYER [newline] BADGES [nelwine] POK\Eacute{}DEX [newline] TIME [0x50] |1271 | 05EDB. | Which Pok\eacute{}mon to show during Prof. Oak's introduction. | A single byte, the Pok\eacute{}mon's internal id. | In Pok\eacute{}mon Yellow, it shows Pikachu during the introduction; Pikachu's internal id is 0x54. |1272 | 06698- | ? Background music. | | |1273 |-----------------------+-----------------+-----------------+-----------------|1274 | 7550-7570 | Menu options for map directions[fn:unused:According to [[http://tcrf.net/Pok%C3%A9mon_Red_and_Blue#NORTH.2FWEST.2FSOUTH.2FEAST][The Cutting Room Floor]], this data is unused. ]. | Variable-length strings. | NORTH [newline] WEST [0x50] SOUTH [newline] EAST [0x50] NORTH [newline] EAST[0x50] |1275 | 7570-757D | Menu options for trading Pok\eacute{}mon | | TRADE [newline] CANCEL [0x50] |1276 | 757D-758A | Menu options for healing Pok\eacute{}mon | | HEAL [newline] CANCEL [0x50] |1277 | 7635- | Menu options for selected Pok\eacute{}mon (Includes names of out-of-battle moves). | Variable-length strings separated by 0x50. | CUT [0x50] FLY [0x50] SURF [0x50] STRENGTH [0x50] FLASH [0x50] DIG [0x50] TELEPORT [0x50] SOFTBOILED [0x50] STATS [newline] SWITCH [newline] CANCEL [0x50] |1278 | 7AF0-8000 | (empty space) | | 0 0 0 0 0 ... |1279 | 0822E-082F? | Pointers to background music, part I. | | |1280 | 0CB95- | Pointers to lists of wild pokemon to encounter in each region. These lists begin at 04D89, see above. | Each pointer is a low-byte, high-byte pair. | The first entry is 0x89 0x4D, corresponding to the address 0x4D89, the location of the first list of wild Pok\eacute{}mon (see 04D89, above). |1281 |-----------------------+-----------------+-----------------+-----------------|1282 | 0DACB. | Amount of HP restored by Soda Pop | The HP consists of a single numerical byte. | 60 |1283 | 0DACF. | Amount of HP restored by Lemonade | " | 80 |1284 | 0DAD5. | Amount of HP restored by Fresh Water | " | 50 |1285 | 0DADB. | Amount of HP restored by Hyper Potion. | " | 200 |1286 | 0DAE0. | Amount of HP restored by Super Potion. | " | 50 |1287 | 0DAE3. | Amount of HP restored by Potion. | " | 20 |1288 |-----------------------+-----------------+-----------------+-----------------|1289 | 0DD4D-DD72 | Names of permanent stats. | Variable-length strings separated by 0x50. | #HEALTH#ATTACK#DEFENSE#SPEED#SPECIAL# |1290 |-----------------------+-----------------+-----------------+-----------------|1291 | 0DE2F. | Duration of Repel. | A single byte, representing the number of steps you can take before the effect wears off. | 100 |1292 | 0DF39. | Duration of Super Repel. | " | 200 |1293 | 0DF3E. | Duration of Max Repel. | " | 250 |1294 |-----------------------+-----------------+-----------------+-----------------|1295 | 1164B- | Terminology for the Pok\eacute{}mon menu. | Contiguous, variable-length strings. | TYPE1[newline]TYPE2[newline] *№*,[newline]OT,[newline][0x50]STATUS,[0x50]OK |1296 | 116DE- | Terminology for permanent stats in the Pok\eacute{}mon menu. | Contiguous, variable-length strings. | ATTACK[newline]DEFENSE[newline]SPEED[newline]SPECIAL[0x50] |1297 | 11852- | Terminology for current stats in the Pok\eacute{}mon menu. | Contiguous, variable-length strings. | EXP POINTS[newline]LEVEL UP[0x50] |1298 | 1195C-1196A | The two terms for being able/unable to learn a TM/HM. | Variable-length strings separated by 0x50. | ABLE#NOT ABLE# |1299 | 119C0-119CE | The two terms for being able/unable to evolve using the current stone. | Variable-length strings separated by 0x50. | ABLE#NOT ABLE# |1300 |-----------------------+-----------------+-----------------+-----------------|1301 | 11D53. | Which badge is a prerequisite for CUT? | op code: which bit of A to test? | When this script is called, the bits of A contain your badges, and this op code will check a certain bit of A. The op codes for the badges are, in order, [0x47 0x4F 0x57 0x5F 0x67 0x6F 0x77 0x7F]. |1302 | 11D67. | Which badge is a prerequisite for SURF? | " | 0x67 (test for Soul Badge) |1303 | 11DAC. | Which badge is a prerequisite for STRENGTH? | " | 0x5F (test for Rainbow Badge) |1304 |-----------------------+-----------------+-----------------+-----------------|1305 | 1232D-12364 | Which moves are taught by the TMs and HMs | A list of 55 move ids (50 TMs, plus 5 HMs). First, the move that will be taught by TM01; second, the move that will be taught by TM02; and so on. The last five entries are the moves taught by HMs 1-5. (See also, BC000 below) | The first few entries are (5 13 14 18 ...) corresponding to Mega Punch (TM01), Razor Wind (TM02), Swords Dance (TM03), Whirlwind (TM04), ... |1306 |-----------------------+-----------------+-----------------+-----------------|1307 | 1CF8B-1CF8C | Which Pok\eacute{}mon does Melanie give you in Cerulean City? | A level/internal-id pair. | (10 153), corresponding to a level 10 Bulbasaur. |1308 | 1D651-1D652 | Which Pok\eacute{}mon do you find at the top of Celadon Mansion? | A level/internal-id pair. | (25 102), corresponding to a level 25 Eevee. |1309 |-----------------------+-----------------+-----------------+-----------------|1310 | 27D56 & 27D57. | Pointer to the pointers to type names. | A single low-byte, high-byte pair. | 0x63 0x7D, corresponding to location 27D63\mdash{} the start of the next entry. |1311 | 27D63-27D99 | Pointers to type names. | Each point is a low-byte, high-byte pair. The type names follows immediately after this section; see below. | The first pointer is [0x99 0x7D], corresponding to the location 27D99 ("NORMAL"). |1312 | 27D99-27DFF | Names of the Pok\eacute{}mon types. | Variable-length type names (strings of character codes). Names are separated by a single 0x50 character. | NORMAL#FIGHTING#... |1313 | 27DFF-27E77 | ? | 120 bytes of unknown data. | |1314 | 27E77- | Trainer title names. | Variable-length names separated by 0x50. | YOUNGSTER#BUG CATCHER#LASS#... |1315 | 34000- | | | |1316 | 38000-383DE | The basic properties and effects of moves. (165 moves total) | Fixed-length (6 byte) continguous descriptions (no separating character): move-index, move-effect, power, move-type, accuracy, pp. | The entry for Pound, the first attack in the list, is (1 0 40 0 255 35). See below for more explanation. |1317 | 383DE- | Species data for the Pokemon, listed in Pokedex order: Pokedex number; base moves; types; learnable TMs and HMs; base HP, attack, defense, speed, special; sprite data. | | |1318 | 39462- | The Pok\eacute{}mon cry data. | Fixed-length (3 byte) descriptions of cries. | |1319 |-----------------------+-----------------+-----------------+-----------------|1320 | 3997D-39B05 | Trainer titles (extended; see 27E77). This list includes strictly more trainers, seemingly at random inserted into the list from 27E77.[fn::The names added are in bold: YOUNGSTER, BUG CATCHER, LASS, *SAILOR*, JR TRAINER(m), JR TRAINER(f), POK\eacute{}MANIAC, SUPER NERD, *HIKER*, *BIKER*, BURGLAR, ENGINEER, JUGGLER, *FISHERMAN*, SWIMMER, *CUE BALL*, *GAMBLER*, BEAUTY, *PSYCHIC*, ROCKER, JUGGLER (again), *TAMER*, *BIRDKEEPER*, BLACKBELT, *RIVAL1*, PROF OAK, CHIEF, SCIENTIST, *GIOVANNI*, ROCKET, COOLTRAINER(m), COOLTRAINER(f), *BRUNO*, *BROCK*, *MISTY*, *LT. SURGE*, *ERIKA*, *KOGA*, *BLAINE*, *SABRINA*, *GENTLEMAN*, *RIVAL2*, *RIVAL3*, *LORELEI*, *CHANNELER*, *AGATHA*, *LANCE*.] | | |1321 | 39B05-39DD0. | unknown | | |1322 | 39DD1-39E2E | Pointers to trainer Pok\eacute{}mon | Pairs of low-high bits. | The first pair is 0x2F 0x5E, which corresponds to memory location 5E2F relative to this 38000-3C000 bank, i.e.[fn::For details about how relative bank pointers work, see the relevant Appendix.] position 39E2F overall. |1323 | 39E2F-3A5B2 | Trainer Pok\eacute{}mon | Specially-formatted lists of various length, separated by 0x00. If the list starts with 0xFF, the rest of the list will alternate between levels and internal-ids. Otherwise, start of the list is the level of the whole team, and the rest of the list is internal-ids. | The first entry is (11 165 108 0), which means a level 11 team consisting of Rattata and Ekans. The entry for MISTY is (255 18 27 21 152 0), which means a team of various levels consisting of level 18 Staryu and level 21 Starmie. [fn::Incidentally, if you want to change your rival's starter Pok\eacute{}mon, it's enough just to change its species in all of your battles with him.].) |1324 | 3B1E5-3B361 | Pointers to evolution/learnset data. | One high-low byte pair for each of the 190 Pok\eacute{}mon in internal order. | |1325 |-----------------------+-----------------+-----------------+-----------------|1326 | 3B361-3BBAA | Evolution and learnset data. [fn::Evolution data consists of how to make Pok\eacute{}mon evolve, and what they evolve into. Learnset data consists of the moves that Pok\eacute{}mon learn as they level up.] | Variable-length evolution information (see below), followed by a list of level/move-id learnset pairs. | |1327 | 3BBAA-3C000 | (empty) | | 0 0 0 0 ... |1328 |-----------------------+-----------------+-----------------+-----------------|1329 | 3D131-3D133 | The inventory of both OLD MAN and PROF. OAK when they battle for you. | Pairs of [item-id quantity], terminated by 0xFF. | (0x04 0x01 0xFF) They only have one Pok\eacute{}ball [fn::If you give them any ball, OAK will catch the enemy Pok\eacute{}mon and OLD MAN will miss. (OLD MAN misses even if he throws a MASTER BALL, which is a sight to see!) If you give them some other item first in the list, you'll be able to use that item normally but then you'll trigger the Safari Zone message: Pa will claim you're out of SAFARI BALLs and the battle will end. If you engage in either an OLD MAN or OAK battle with a Gym Leader, you will [1] get reprimanded if you try to throw a ball [2] incur the Safari Zone message [3] automatically win no matter which item you use [4] earn whichever reward they give you as usual [5] permanently retain the name OLD MAN / PROF. OAK.]. |1330 | 3D6C7-3D6D6 | Two miscellaneous strings. | Variable length, separated by 0x50 | Disabled!#TYPE |1331 | 3E190-3E194 | Which moves have an increased critical-hit ratio? | List of move ids, terminated by 0xFF. | (0x02 0x4B 0x98 0xA3 0xFF) corresponding to karate-chop, razor-leaf, crabhammer, slash, end-of-list. |1332 | 3E200-3E204 | " (???) | " | " |1333 | 3E231. | Besides normal-type, which type of move can COUNTER counter? | A single byte representing a type id. | This is set to 1, the id of the FIGHTING type. |1334 |-----------------------+-----------------+-----------------+-----------------|1335 | 40252-4027B | Pok\eacute{}dex menu text | Variable-length strings separated by 0x50. | SEEN#OWN#CONTENTS#... |1336 | 40370-40386 | Important constants for Pok\eacute{}dex entries | | HT _ _ *?′??″* [newline] WT _ _ _ *???* lb [0x50] *POK\Eacute{}* [0x50] |1337 | 40687-41072 | Species data from the Pok\eacute{}dex: species name, height, weight, etc. | Variable-length species names, followed by 0x50, followed by fixed-length height/weight/etc. data. | The first entry is (*146 132 132 131*, 80, *2 4*, *150 0*, 23, 0 64 46, 80), which are the the stats of Bulbasaur: the first entry spells "SEED", then 0x80, then the height (2' 4"), then the weight (formatted as a low-high byte pair), then various Pokédex pointer data (see elsewhere). |1338 | 41072- | Pok\eacute{} placeholder species, "???" | | |1339 |-----------------------+-----------------+-----------------+-----------------|1340 | 410B1-4116F | A conversion table between internal order and Pokedex order. | 190 bytes, corresponding to the Pok\eacute{}dex numbers of the 190 Pok\eacute{}mon listed in internal order. All =MISSINGNO.= are assigned a Pok\eacute{}dex number of 0. | The first few entries are (112 115 32 35 21 100 34 80 2 ...), which are the Pok\eacute{}dex numbers of Rhydon, Kangaskhan, Nidoran(m), Clefairy, Spearow, Voltorb, Nidoking, Slobrow, and Ivysaur. |1341 |-----------------------+-----------------+-----------------+-----------------|1342 | 509B4-509E0 | Saffron City's adjacency info. | Four adjacency lists, each 11 bytes long. (For more info on adjacency lists a.k.a. connection data, see [[http://datacrystal.romhacking.net/wiki/Pokemon_Red/Blue:Notes][here]]) | The first adjacency list is (0x10 0x70 0x46 0xF0 0xC6 0x0A 0x0A 0x23 0xF6 0x09 0xC8) |1343 |-----------------------+-----------------+-----------------+-----------------|1344 | 515AE-515AF | Which Pok\eacute{}mon does the trainer near Route 25 give you? | A level/internal-id pair. | (10 176) corresponding to a level 10 Charmander. |1345 | 51DD5-51DD6 | Which Pok\eacute{}mon does the Silph Co. trainer give you? | A level/internal-id pair. | (15 19) corresponding to a level 15 Lapras. |1346 |-----------------------+-----------------+-----------------+-----------------|1347 | 527BA-527DB | The costs and kinds of prizes from Celadon Game Corner. | The following pattern repeats three times, once per window[fn::For the first two prize lists, ids are interpreted as Pok\eacute{}mon ids. For the last prize list, ids are (somehow) interpreted as item ids.]: Internal ids / 0x50 / Prices (two bytes of BCD)/ 0x50. | (0x94 0x52 0x65 0x50) Abra Vulpix Wigglytuff (0x02 0x30 0x10 0x00 0x26 0x80) 230C, 1000C, 2680C |1348 | 5DE10-5DE30 | Abbreviations for status ailments. | Fixed-length strings, probably[fn::Here's something strange: all of the status messages start with 0x7F and end with 0x4F \mdash{}except PAR, which ends with 0x50.]. The last entry is QUIT##. | [0x7F] *SLP* [0x4E][0x7F] *PSN* [0x4E][0x7F] *PAR* [0x50][0x7F]... |1349 |-----------------------+-----------------+-----------------+-----------------|1350 | 70295- | Hall of fame | The text "HALL OF FAME" | |1351 | 70442- | Play time/money | The text "PLAY TIME [0x50] MONEY" | |1352 | 71500-7174B | Names of places. | Variable-length place names (strings), separated by 0x50. | PALLET TOWN#VIRIDIAN CITY#PEWTER CITY#CERULEAN CITY#... |1353 | 71C1E-71CAA (approx.) | Tradeable NPC Pok\eacute{}mon. | Internal ID, followed by nickname (11 chars; extra space padded by 0x50). Some of the Pokemon have unknown extra data around the id. | The first entry is [0x76] "GURIO######", corresponding to a Dugtrio named "GURIO". |1354 | 7C249-7C2?? | Pointers to background music, pt II. | | |1355 |-----------------------+-----------------+-----------------+-----------------|1356 | 98000-B7190 | Dialogue and other messsages. | Variable-length strings. | |1357 | B7190-B8000 | (empty space) | | 0 0 0 0 0 ... |1358 | B8000-BC000 | The text of each Pok\eacute{}mon's Pok\eacute{}dex entry. | Variable-length descriptions (strings) in Pok\eacute{}dex order, separated by 0x50. These entries use the special characters *0x49* (new page), *0x4E* (new line), and *0x5F* (end entry). | The first entry (Bulbasaur's) is: "It can go for days [0x4E] without eating a [0x4E] single morsel. [0x49] In the bulb on [0x4E] its back, it [0x4E] stores energy [0x5F] [0x50]." |1359 | BC000-BC60F | Move names. | Variable-length move names, separated by 0x50. The moves are in internal order. | POUND#KARATE CHOP#DOUBLESLAP#COMET PUNCH#... |1360 | BC610-BD000 | (empty space) | | 0 0 0 0 0 ... |1361 | E8000-E876C | Names of the \ldquo{}190\rdquo{} species of Pok\eacute{}mon in memory. | Fixed length (10-letter) Pok\eacute{}mon names. Any extra space is padded with the character 0x50. The names are in \ldquo{}internal order\rdquo{}. | RHYDON####KANGASKHANNIDORAN♂#... |1362 |-----------------------+-----------------+-----------------+-----------------|1363 | E9BD5- | The text PLAY TIME (see above, 70442) | | |1364 | F1A44-F1A45 | Which Pok\eacute{}mon does Officer Jenny give you? | A level/internal-id pair. | (10 177), corresponding to a level 10 Squirtle. |1365 | F21BF-F21C0 | Which Pok\eacute{}mon does the salesman at the Mt. Moon Pok\eacute{}mon center give you? | A level/internal-id pair | (5 133), corresponding to a level 5 Magikarp. |1366 | | | | |1367 #+TBLFM:1369 ** COMMENT1370 Locations where Give Pokemon is used in a nonstraightforward way1371 0x5287C1372 0x5CE231373 0x5C36B1374 0x7562E1377 ** Understanding memory banks and pointers1378 #+begin_src clojure1380 (defn endian-flip1381 "Flip the bytes of the two-byte number."1382 [n]1383 (assert (< n 0xFFFF))1384 (+ (* 0x100 (rem n 0x100))1385 (int (/ n 0x100))))1388 (defn offset->ptr1389 "Convert an offset into a little-endian pointer."1390 [n]1391 (->1392 n1393 (rem 0x10000) ;; take last four bytes1394 (rem 0x4000) ;; get relative offset from the start of the bank1395 (+ 0x4000)1396 endian-flip))1398 (defn offset->bank1399 "Get the bank of the offset."1400 [n]1401 (int (/ n 0x4000)))1403 (defn ptr->offset1404 "Convert a two-byte little-endian pointer into an offset."1405 [bank ptr]1406 (->1407 ptr1408 endian-flip1409 (- 0x4000)1410 (+ (* 0x4000 bank))1411 ))1413 (defn same-bank-offset1414 "Convert a ptr into an absolute offset by using the bank of the reference."1415 [reference ptr]1416 (ptr->offset1417 (offset->bank reference)1418 ptr))1419 #+end_src1422 ** Internal Pok\eacute{}mon IDs1423 ** Type IDs1425 #+name: type-ids1426 #+begin_src clojure1427 (def pkmn-types1428 [:normal ;;01429 :fighting ;;11430 :flying ;;21431 :poison ;;31432 :ground ;;41433 :rock ;;51434 :bird ;;61435 :bug ;;71436 :ghost ;;81437 :A1438 :B1439 :C1440 :D1441 :E1442 :F1443 :G1444 :H1445 :I1446 :J1447 :K1448 :fire ;;20 (0x14)1449 :water ;;21 (0x15)1450 :grass ;;22 (0x16)1451 :electric ;;23 (0x17)1452 :psychic ;;24 (0x18)1453 :ice ;;25 (0x19)1454 :dragon ;;26 (0x1A)1455 ])1456 #+end_src1458 ** Basic effects of moves1460 *** Table of basic effects1462 The possible effects of moves in Pok\eacute{}mon \mdash{} for example, dealing1463 damage, leeching health, or potentially poisoning the opponent1464 \mdash{} are stored in a table. Each move has exactly one effect, and1465 different moves might have the same effect.1467 For example, Leech Life, Mega Drain, and Absorb all have effect ID #3, which is \ldquo{}Leech half of the inflicted damage.\rdquo{}1469 All the legitimate move effects are listed in the table1470 below. Here are some notes for reading it:1472 - Whenever an effect has a chance of doing something (like a chance of1473 poisoning the opponent), I list the chance as a hexadecimal amount1474 out of 256; this is to avoid rounding errors. To convert the hex amount into a percentage, divide by 256.1475 - For some effects, the description is too cumbersome to1476 write. Instead, I just write a move name1477 in parentheses, like: (leech seed). That move gives a characteristic example1478 of the effect.1479 - I use the abbreviations =atk=, =def=, =spd=, =spc=, =acr=, =evd= for1480 attack, defense, speed, special, accuracy, and evasiveness.1481 .1485 | ID (hex) | Description | Notes |1486 |----------+-------------------------------------------------------------------------------------------------+------------------------------------------------------------------|1487 | 0 | normal damage | |1488 | 1 | no damage, just sleep | TODO: find out how many turns |1489 | 2 | 0x4C chance of poison | |1490 | 3 | leech half of inflicted damage | |1491 | 4 | 0x19 chance of burn | |1492 | 5 | 0x19 chance of freeze | |1493 | 6 | 0x19 chance of paralysis | |1494 | 7 | user faints; opponent's defense is halved during attack. | |1495 | 8 | leech half of inflicted damage ONLY if the opponent is asleep | |1496 | 9 | imitate last attack | |1497 | A | user atk +1 | |1498 | B | user def +1 | |1499 | C | user spd +1 | |1500 | D | user spc +1 | |1501 | E | user acr +1 | This effect is unused. |1502 | F | user evd +1 | |1503 | 10 | get post-battle money = 2 * level * uses | |1504 | 11 | move has 0xFE acr, regardless of battle stat modifications. | |1505 | 12 | opponent atk -1 | |1506 | 13 | opponent def -1 | |1507 | 14 | opponent spd -1 | |1508 | 15 | opponent spc -1 | |1509 | 16 | opponent acr -1 | |1510 | 17 | opponent evd -1 | |1511 | 18 | converts user's type to opponent's. | |1512 | 19 | (haze) | |1513 | 1A | (bide) | |1514 | 1B | (thrash) | |1515 | 1C | (teleport) | |1516 | 1D | (fury swipes) | |1517 | 1E | attacks 2-5 turns | Unused. TODO: find out what it does. |1518 | 1F | 0x19 chance of flinching | |1519 | 20 | opponent sleep for 1-7 turns | |1520 | 21 | 0x66 chance of poison | |1521 | 22 | 0x4D chance of burn | |1522 | 23 | 0x4D chance of freeze | |1523 | 24 | 0x4D chance of paralysis | |1524 | 25 | 0x4D chance of flinching | |1525 | 26 | one-hit KO | |1526 | 27 | charge one turn, atk next. | |1527 | 28 | fixed damage, leaves 1HP. | Is the fixed damage the power of the move? |1528 | 29 | fixed damage. | Like seismic toss, dragon rage, psywave. |1529 | 2A | atk 2-5 turns; opponent can't attack | The odds of attacking for /n/ turns are: (0 0x60 0x60 0x20 0x20) |1530 | 2B | charge one turn, atk next. (can't be hit when charging) | |1531 | 2C | atk hits twice. | |1532 | 2D | user takes 1 damage if misses. | |1533 | 2E | evade status-lowering effects | Caused by you or also your opponent? |1534 | 2F | broken: if user is slower than opponent, makes critical hit impossible, otherwise has no effect | This is the effect of Focus Energy. It's (very) broken. |1535 | 30 | atk causes recoil dmg = 1/4 dmg dealt | |1536 | 31 | confuses opponent | |1537 | 32 | user atk +2 | |1538 | 33 | user def +2 | |1539 | 34 | user spd +2 | |1540 | 35 | user spc +2 | |1541 | 36 | user acr +2 | This effect is unused. |1542 | 37 | user evd +2 | This effect is unused. |1543 | 38 | restores up to half of user's max hp. | |1544 | 39 | (transform) | |1545 | 3A | opponent atk -2 | |1546 | 3B | opponent def -2 | |1547 | 3C | opponent spd -2 | |1548 | 3D | opponent spc -2 | |1549 | 3E | opponent acr -2 | |1550 | 3F | opponent evd -2 | |1551 | 40 | doubles user spc when attacked | |1552 | 41 | doubles user def when attacked | |1553 | 42 | just poisons opponent | |1554 | 43 | just paralyzes opponent | |1555 | 44 | 0x19 chance opponent atk -1 | |1556 | 45 | 0x19 chance opponent def -1 | |1557 | 46 | 0x19 chance opponent spd -1 | |1558 | 47 | 0x4C chance opponent spc -1 | |1559 | 48 | 0x19 chance opponent acr -1 | |1560 | 49 | 0x19 chance opponent evd -1 | |1561 | 4A | ??? | ;; unused? no effect? |1562 | 4B | ??? | ;; unused? no effect? |1563 | 4C | 0x19 chance of confusing the opponent | |1564 | 4D | atk hits twice. 0x33 chance opponent poisioned. | |1565 | 4E | broken. crash the game after attack. | |1566 | 4F | (substitute) | |1567 | 50 | unless opponent faints, user must recharge after atk. some exceptions apply | |1568 | 51 | (rage) | |1569 | 52 | (mimic) | |1570 | 53 | (metronome) | |1571 | 54 | (leech seed) | |1572 | 55 | does nothing (splash) | |1573 | 56 | (disable) | |1574 #+end_src1576 *** Source1577 #+name: move-effects1578 #+begin_src clojure1579 (def move-effects1580 ["normal damage"1581 "no damage, just opponent sleep" ;; how many turns? is atk power ignored?1582 "0x4C chance of poison"1583 "leech half of inflicted damage"1584 "0x19 chance of burn"1585 "0x19 chance of freeze"1586 "0x19 chance of paralyze"1587 "user faints; opponent defense halved during attack."1588 "leech half of inflicted damage ONLY if sleeping opponent."1589 "imitate last attack"1590 "user atk +1"1591 "user def +1"1592 "user spd +1"1593 "user spc +1"1594 "user acr +1" ;; unused?!1595 "user evd +1"1596 "get post-battle $ = 2*level*uses"1597 "0xFE acr, no matter what."1598 "opponent atk -1" ;; acr taken from move acr?1599 "opponent def -1" ;;1600 "opponent spd -1" ;;1601 "opponent spc -1" ;;1602 "opponent acr -1";;1603 "opponent evd -1"1604 "converts user's type to opponent's."1605 "(haze)"1606 "(bide)"1607 "(thrash)"1608 "(teleport)"1609 "(fury swipes)"1610 "attacks 2-5 turns" ;; unused? like rollout?1611 "0x19 chance of flinch"1612 "opponent sleep for 1-7 turns"1613 "0x66 chance of poison"1614 "0x4D chance of burn"1615 "0x4D chance of freeze"1616 "0x4D chance of paralyze"1617 "0x4D chance of flinch"1618 "one-hit KO"1619 "charge one turn, atk next."1620 "fixed damage, leaves 1HP." ;; how is dmg determined?1621 "fixed damage." ;; cf seismic toss, dragon rage, psywave.1622 "atk 2-5 turns; opponent can't attack" ;; unnormalized? (0 0x60 0x60 0x20 0x20)1623 "charge one turn, atk next. (can't be hit when charging)"1624 "atk hits twice."1625 "user takes 1 damage if misses."1626 "evade status-lowering effects" ;;caused by you or also your opponent?1627 "(broken) if user is slower than opponent, makes critical hit impossible, otherwise has no effect"1628 "atk causes recoil dmg = 1/4 dmg dealt"1629 "confuses opponent" ;; acr taken from move acr1630 "user atk +2"1631 "user def +2"1632 "user spd +2"1633 "user spc +2"1634 "user acr +2" ;; unused!1635 "user evd +2" ;; unused!1636 "restores up to half of user's max hp." ;; broken: fails if the difference1637 ;; b/w max and current hp is one less than a multiple of 256.1638 "(transform)"1639 "opponent atk -2"1640 "opponent def -2"1641 "opponent spd -2"1642 "opponent spc -2"1643 "opponent acr -2"1644 "opponent evd -2"1645 "doubles user spc when attacked"1646 "doubles user def when attacked"1647 "just poisons opponent" ;;acr taken from move acr1648 "just paralyzes opponent" ;;1649 "0x19 chance opponent atk -1"1650 "0x19 chance opponent def -1"1651 "0x19 chance opponent spd -1"1652 "0x4C chance opponent spc -1" ;; context suggest chance is 0x191653 "0x19 chance opponent acr -1"1654 "0x19 chance opponent evd -1"1655 "???" ;; unused? no effect?1656 "???" ;; unused? no effect?1657 "0x19 chance opponent confused"1658 "atk hits twice. 0x33 chance opponent poisioned."1659 "broken. crash the game after attack."1660 "(substitute)"1661 "unless opponent faints, user must recharge after atk. some1662 exceptions apply."1663 "(rage)"1664 "(mimic)"1665 "(metronome)"1666 "(leech seed)"1667 "does nothing (splash)"1668 "(disable)"1669 ])1670 #+end_src1673 ** Alphabet code1675 * Source1677 #+begin_src clojure :tangle ../clojure/com/aurellem/gb/hxc.clj1679 (ns com.aurellem.gb.hxc1680 (:use (com.aurellem.gb assembly characters gb-driver util mem-util1681 constants species))1682 (:import [com.aurellem.gb.gb_driver SaveState]))1684 ; ************* HANDWRITTEN CONSTANTS1686 <<type-ids>>1689 ;; question: when status effects claim to take1690 ;; their accuracy from the move accuracy, does1691 ;; this mean that the move always "hits" but the1692 ;; status effect may not?1694 <<move-effects>>1696 ;; ************** HARDCODED DATA1698 <<hxc-thunks>>1699 ;; --------------------------------------------------1701 <<pokenames>>1702 <<type-names>>1704 ;; http://hax.iimarck.us/topic/581/1705 <<pokecry>>1708 <<item-names>>1712 (def hxc-titles1713 "The hardcoded names of the trainer titles in memory. List begins at1714 ROM@27E77"1715 (hxc-thunk-words 0x27E77 196))1718 <<dex-text>>1720 ;; In red/blue, pokedex stats are in internal order.1721 ;; In yellow, pokedex stats are in pokedex order.1722 <<dex-stats>>1727 <<places>>1729 (defn hxc-dialog1730 "The hardcoded dialogue in memory, including in-game alerts. Dialog1731 seems to be separated by 0x57 instead of 0x50 (END). Begins at ROM@98000."1732 ([rom]1733 (map character-codes->str1734 (take-nth 21735 (partition-by #(= % 0x57)1736 (take 0x0F7281737 (drop 0x98000 rom))))))1738 ([]1739 (hxc-dialog com.aurellem.gb.gb-driver/original-rom)))1742 <<move-names>>1743 <<move-data>>1745 <<machines>>1749 (defn internal-id1750 ([rom]1751 (zipmap1752 (hxc-pokenames rom)1753 (range)))1754 ([]1755 (internal-id com.aurellem.gb.gb-driver/original-rom)))1761 ;; nidoran gender change upon levelup1762 ;; (->1763 ;; @current-state1764 ;; rom1765 ;; vec1766 ;; (rewrite-memory1767 ;; (nth (hxc-ptrs-evolve) ((internal-id) :nidoran♂))1768 ;; [1 1 15])1769 ;; (rewrite-memory1770 ;; (nth (hxc-ptrs-evolve) ((internal-id) :nidoran♀))1771 ;; [1 1 3])1772 ;; (write-rom!)1774 ;; )1778 <<type-advantage>>1782 <<evolution-header>>1783 <<evolution>>1784 <<learnsets>>1785 <<pokebase>>1788 (defn hxc-intro-pkmn1789 "The hardcoded pokemon to display in Prof. Oak's introduction; the pokemon's1790 internal id is stored at ROM@5EDB."1791 ([] (hxc-intro-pkmn1792 com.aurellem.gb.gb-driver/original-rom))1793 ([rom]1794 (nth (hxc-pokenames rom) (nth rom 0x5EDB))))1796 (defn sxc-intro-pkmn!1797 "Set the hardcoded pokemon to display in Prof. Oak's introduction."1798 [pokemon]1799 (write-rom!1800 (rewrite-rom 0x5EDB1801 [1802 (inc1803 ((zipmap1804 (hxc-pokenames)1805 (range))1806 pokemon))])))1809 <<item-prices>>1811 <<item-vendors>>1813 <<wilds>>1816 ;; ********************** MANIPULATION FNS1819 (defn same-type1820 ([pkmn move]1821 (same-type1822 com.aurellem.gb.gb-driver/original-rom pkmn move))1823 ([rom pkmn move]1824 (((comp :types (hxc-pokemon-base rom)) pkmn)1825 ((comp :type (hxc-move-data rom)) move))))1830 (defn submap?1831 "Compares the two maps. Returns true if map-big has the same associations as map-small, otherwise false."1832 [map-small map-big]1833 (cond (empty? map-small) true1834 (and1835 (contains? map-big (ffirst map-small))1836 (= (get map-big (ffirst map-small))1837 (second (first map-small))))1838 (recur (next map-small) map-big)1840 :else false))1843 (defn search-map [proto-map maps]1844 "Returns all the maps that make the same associations as proto-map."1845 (some (partial submap? proto-map) maps))1847 (defn filter-vals1848 "Returns a map consisting of all the pairs [key val] for1849 which (pred key) returns true."1850 [pred map]1851 (reduce (partial apply assoc) {}1852 (filter (fn [[k v]] (pred v)) map)))1855 (defn search-moves1856 "Returns a subcollection of all hardcoded moves with the1857 given attributes. Attributes consist of :name :power1858 :accuracy :pp :fx-id1859 (and also :fx-txt, but it contains the same information1860 as :fx-id)"1861 ([attribute-map]1862 (search-moves1863 com.aurellem.gb.gb-driver/original-rom attribute-map))1864 ([rom attribute-map]1865 (filter-vals (partial submap? attribute-map)1866 (hxc-move-data rom))))1872 ;; note: 0x2f31 contains the names "TM" "HM"?1874 ;; note for later: credits start at F12901876 ;; note: DADB hyper-potion-hp _ _ _ super-potion-hp _ _ _ potion-hp ??1878 ;; note: DD4D spells out pokemon vital stat names ("speed", etc.)1880 ;; note: 1195C-6A says ABLE#NOT ABLE#, but so does 119C0-119CE.1881 ;; The first instance is for Machines; the second, for stones.1883 ;; note: according to1884 ;; http://www.upokecenter.com/games/rby/guides/rgbtrainers.php1885 ;; the amount of money given by a trainer is equal to the1886 ;; base money times the level of the last Pokemon on that trainer's1887 ;; list. Other sources say it's the the level of the last pokemon1888 ;; /defeated/.1890 ;; todo: find base money.1893 ;; note: 0xDFEA (in indexable mem) is the dex# of the currently-viewed Pokemon in1894 ;; in the pokedex. It's used for other purposes if there is none.1896 ;; note: 0x9D35 (index.) switches from 0xFF to 0x00 temporarily when1897 ;; you walk between areas.1899 ;; note: 0xD059 (index.) is the special battle type of your next battle:1900 ;; - 00 is a usual battle1901 ;; - 01 is a pre-scripted OLD MAN battle which always fails to catch the1902 ;; target Pokemon.1903 ;; - 02 is a safari zone battle1904 ;; - 03 obligates you to run away. (unused)1905 ;; - 04 is a pre-scripted OAK battle, which (temporarily) causes the1906 ;; enemy Pokemon to cry PIKAAA, and which always catches the target1907 ;; Pokemon. The target Pokemon is erased after the battle.1908 ;; - 05+ are glitch states in which you are sort of the Pokemon.1911 ;; note: 0x251A (in indexable mem): image decompression routine seems to begin here.1913 ;; note: 0x4845 (index): vending inventory is loaded here. possibly1914 ;; other things, too.1915 (comment1916 ;; temporarily intercept/adjust what pops out of the vending1917 ;; machine.1918 ;; (and how much it costs)1920 ;; located at 0x48451921 ;; not to be confused with shop inventory, 0xCF7B1922 (do1923 (step (read-state "vend-menu"))1924 (write-memory! (rewrite-memory (vec(memory)) 0x4845 [2 0 1 0]))1925 (step @current-state [:a])1926 (step @current-state [])1927 (nstep @current-state 200) ))1930 ;; Note: There are two tile tables, one from 8000-8FFF, the other from1931 ;; 8800-97FF. The latter contains symbols, possibly map tiles(?), with some japanese chars and stuff at the end.1932 (defn print-pixel-letters!1933 "The pixel tiles representing letters. Neat!"1934 ([] (print-pixel-letters! (read-state "oak-speaks")))1935 ([state]1936 (map1937 (comp1938 println1939 (partial map #(if (zero? %) \space 0))1940 #(if (< (count %) 8)1941 (recur (cons 0 %))1942 %)1943 reverse bit-list)1945 (take 0xFFF (drop 0x8800 (memory state))))))1948 ;; (defn test-2 []1949 ;; (loop [n 01950 ;; pc-1 (pc-trail (-> state-defend (tick) (step [:a]) (step [:a]) (step []) (nstep 100)) 100000)1951 ;; pc-2 (pc-trail (-> state-speed (tick) (step [:a]) (step [:a])1952 ;; (step []) (nstep 100)) 100000)]1953 ;; (cond (empty? (drop n pc-1)) [pc-1 n]1954 ;; (not= (take 10 (drop n pc-1)) (take 10 pc-2))1955 ;; (recur pc-1 pc-2 (inc n))1956 ;; :else1957 ;; [(take 1000 pc-2) n])))1962 (defn test-31963 "Explore trainer data"1964 ([] (test-3 0x3A289))1965 ([start]1966 (let [pokenames (vec(hxc-pokenames-raw))]1967 (println1968 (reduce1969 str1970 (map1971 (fn [[adr lvl pkmn]]1972 (str (format "%-11s %4d %02X %02X \t %05X\n"1974 (cond1975 (zero? lvl) "+"1976 (nil? (get pokenames (dec pkmn)))1977 "-"1978 :else1979 (get pokenames (dec pkmn)))1980 lvl1981 pkmn1982 lvl1983 adr1984 )))1985 (map cons1986 (take-nth 2 (drop start (range)))1987 (partition 21988 (take 400;;7031989 (drop1990 start1991 ;; 0x3A75D1992 (rom)))))))))))1994 (defn search-memory* [mem codes k]1995 (loop [index 01996 index-next 11997 start-match 01998 to-match codes1999 matches []]2000 (cond2001 (>= index (count mem)) matches2003 (empty? to-match)2004 (recur2005 index-next2006 (inc index-next)2007 index-next2008 codes2009 (conj matches2010 [(hex start-match) (take k (drop start-match mem))])2011 )2013 (or (= (first to-match) \_) ;; wildcard2014 (= (first to-match) (nth mem index)))2015 (recur2016 (inc index)2017 index-next2018 start-match2019 (rest to-match)2020 matches)2022 :else2023 (recur2024 index-next2025 (inc index-next)2026 index-next2027 codes2028 matches))))2031 (def script-use-ball2032 [0xFA ;; ld A, nn2033 \_2034 \_2035 0xA7 ;; and A2036 0xCA ;; JP Z2037 \_2038 \_2039 0x3D ;; dec A2040 0xC2 ;; JP NZ2041 \_2042 \_2043 0xFA ;; LD A2044 \_2045 \_2046 ])2050 (defn search-pattern [ptn coll]2051 (loop2052 [index 02053 to-match ptn2054 binds {}2056 next-index 12057 match-start 02058 matches []]2060 (cond2061 (>= index (count coll)) matches2062 (empty? to-match)2063 (recur2064 next-index2065 ptn2066 {}2067 (inc next-index)2068 next-index2069 (conj match-start2070 [(hex match-start) binds]))2072 :else2073 (let [k (first to-match)2074 v (nth coll index)]2075 (cond2076 (= k \_) ;; wildcard2077 (recur2078 (inc index)2079 (rest to-match)2080 binds2082 next-index2083 match-start2084 matches)2086 (keyword? k)2087 (if (binds k)2088 (if (= (binds k) v)2089 (recur2090 (inc index)2091 (rest to-match)2092 binds2093 next-index2094 match-start2095 matches)2097 (recur2098 next-index2099 ptn2100 {}2101 (inc next-index)2102 next-index2103 matches))2105 ;; ;; consistent bindings2106 ;; (recur2107 ;; (inc index)2108 ;; (rest to-match)2109 ;; binds2111 ;; next-index2112 ;; match-start2113 ;; matches)2115 ;; ;; inconsistent bindings2116 ;; (recur2117 ;; next-index2118 ;; ptn2119 ;; {}2120 ;; (inc next-index)2121 ;; next-index2122 ;; matches))2124 (if ((set (vals binds)) v)2125 ;; bindings are not unique2126 (recur2127 next-index2128 ptn2129 {}2130 (inc next-index)2131 next-index2132 matches)2134 ;; bindings are unique2135 (recur2136 (inc index)2137 (rest to-match)2138 (assoc binds k v)2140 next-index2141 match-start2142 matches)))2144 :else ;; k is just a number2145 (if (= k v)2146 (recur2147 (inc index)2148 (rest to-match)2149 binds2151 next-index2152 match-start2153 matches)2155 (recur2156 next-index2157 ptn2158 {}2159 (inc next-index)2160 next-index2161 matches)))))))2171 (defn search-pattern* [ptn coll]2172 (loop2173 [2174 binds {}2175 index 02176 index-next 12177 start-match 02178 to-match ptn2179 matches []]2181 (cond2182 (>= index (count coll)) matches2183 (empty? to-match)2184 (recur2185 {}2186 index-next2187 (inc index-next)2188 index-next2189 ptn2190 (conj matches2191 [(hex start-match) binds]))2193 :else2194 (let [k (first to-match)2195 v (nth coll index)]2196 (cond2197 (= k \_) ;; wildcard2198 (recur2199 binds2200 (inc index)2201 index-next2202 start-match2203 (rest to-match)2204 matches)2206 (keyword? k)2207 (if (binds k)2208 (if (= (binds k) v)2209 (recur2210 binds2211 (inc index)2212 index-next2213 start-match2214 (rest to-match)2215 matches)2216 (recur2217 {}2218 index-next2219 (inc index-next)2220 index-next2221 ptn2222 matches))2223 (if2224 ;; every symbol must be bound to a different thing.2225 ((set (vals binds)) v)2226 (recur2227 {}2228 index-next2229 (inc index-next)2230 index-next2231 ptn2232 matches)2233 (recur2234 (assoc binds k v)2235 (inc index)2236 index-next2237 start-match2238 (rest to-match)2239 matches)))2241 :else2242 (if (= k v)2243 (recur2244 binds2245 (inc index)2246 index-next2247 start-match2248 (rest to-match)2249 matches)2250 (recur2251 {}2252 index-next2253 (inc index-next)2254 index-next2255 ptn2256 matches))2260 )))))2265 ;; look for the rainbow badge in memory2266 (println (reduce str (map #(str (first %) "\t" (vec(second %)) "\n") (search-memory (rom) [221] 10))))2269 (comment2271 (def hxc-later2272 "Running this code produces, e.g. hardcoded names NPCs give2273 their pokemon. Will sort through it later."2274 (print (character-codes->str(take 100002275 (drop 0x715972276 (rom (root)))))))2278 (let [dex2279 (partition-by #(= 0x50 %)2280 (take 25402281 (drop 0x406872282 (rom (root)))))]2283 (def dex dex)2284 (def hxc-species2285 (map character-codes->str2286 (take-nth 4 dex))))2287 )2290 #+end_src2292 #+results:2293 : nil