Mercurial > vba-clojure
view org/rom.org @ 365:dc39dfcad61f
almost halfway there!
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Mon, 09 Apr 2012 11:31:09 -0500 |
parents | 37616a97beaa |
children | b477970d0b7a |
line wrap: on
line source
1 #+title: Notes on Deconstructing Pokemon Yellow2 #+author: Dylan Holmes3 #+email: rlm@mit.edu4 #+description:5 #+keywords:6 #+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 * Introduction16 ** COMMENT Getting linguistic data: names, words, etc.18 Some of the simplest data21 One of the simplest data structures in the Pok\eacute{} ROM is an22 unbroken list of strings that either (a) all have a specific length,23 or (b) are all separated by the same character.25 Because lots of good data has this format, we'll start by writing a26 template function to extract it:28 #+name: hxc-thunks29 #+begin_src clojure :results silent30 (defn hxc-thunk31 "Creates a thunk (nullary fn) that grabs data in a certain region of rom and32 splits it into a collection by 0x50. If rom is not supplied, uses the33 original rom data."34 [start length]35 (fn self36 ([rom]37 (take-nth 238 (partition-by #(= % 0x50)39 (take length40 (drop start rom)))))41 ([]42 (self com.aurellem.gb.gb-driver/original-rom))))44 (def hxc-thunk-words45 "Same as hxc-thunk, except it interprets the rom data as characters,46 returning a collection of strings."47 (comp48 (partial comp (partial map character-codes->str))49 hxc-thunk))51 #+end_src54 * Pok\eacute{}mon I55 ** Names of each species56 The names of the Pok\eacute{}mon species are stored in57 ROM@E8000. This name list is interesting, for a number of reasons:58 - The names are stored in [[ ][internal order]] rather than in the familiar59 Pok\eacute{}dex order. This seemingly random order probably represents the order in which the authors created or60 programmed in the Pok\eacute{}mon; it's used throughout the game.61 - There is enough space allocated for 190 Pok\eacute{}mon. As I62 understand it, there were originally going to be 190 Pok\eacute{}mon63 in Generation I, but the creators decided to defer some to64 Generation II. This explains why many Gen I and Gen II Pok\eacute{}mon65 have the same aesthetic feel.66 - The list is pockmarked with random gaps, due to the strange internal67 ordering68 and the 39 unused spaces [fn::190 allocated spaces minus 151 true Pok\eacute{}mon]. These missing spaces are filled with the69 placeholder name =MISSINGNO.= (\ldquo{}Missing number\rdquo{}).71 Each name is exactly ten letters long; whenever a name would be too short, the extra72 space is padded with the character 0x50.74 *** See the data76 Here you can see the raw data in three stages: in the first stage, we77 just grab the first few bytes starting from position 0xE8000. In the78 second stage, we partition it into ten-letter chunks to show you79 where the names begin and end. In the final stage, we convert each80 byte into the letter it represents using the =character-codes->str=81 function. (0x50 is rendered as the symbol \ldquo{} =#= \rdquo{} for82 ease of reading).84 #+begin_src clojure :exports both :cache no :results output85 (ns com.aurellem.gb.hxc86 (:use (com.aurellem.gb assembly characters gb-driver util mem-util87 constants))88 (: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 begins at135 ROM@410B1. See also, hxc-pokenames."136 ([] (hxc-pokedex-names137 com.aurellem.gb.gb-driver/original-rom))138 ([rom]139 (let [names (hxc-pokenames rom)]140 (#(mapv %141 ((comp range count keys) %))142 (zipmap143 (take (count names)144 (drop 0x410b1 rom))146 names)))))148 #+end_src152 ** Generic species information154 #+name: pokebase155 #+begin_src clojure156 (defn hxc-pokemon-base157 ([] (hxc-pokemon-base com.aurellem.gb.gb-driver/original-rom))158 ([rom]159 (let [entry-size 28160 pkmn-count (count (hxc-pokedex-text rom))161 pokemon (rest (hxc-pokedex-names))162 types (apply assoc {}163 (interleave164 (range)165 pkmn-types)) ;;!! softcoded166 moves (apply assoc {}167 (interleave168 (range)169 (map format-name170 (hxc-move-names rom))))171 machines (hxc-machines)172 ]173 (zipmap174 pokemon175 (map176 (fn [[n177 rating-hp178 rating-atk179 rating-def180 rating-speed181 rating-special182 type-1183 type-2184 rarity185 rating-xp186 pic-dimensions ;; tile_width|tile_height (8px/tile)187 ptr-pic-obverse-1188 ptr-pic-obverse-2189 ptr-pic-reverse-1190 ptr-pic-reverse-2191 move-1192 move-2193 move-3194 move-4195 growth-rate196 &197 TMs|HMs]]198 (let199 [base-moves200 (mapv moves201 ((comp202 ;; since the game uses zero as a delimiter,203 ;; it must also increment all move indices by 1.204 ;; heren we decrement to correct this.205 (partial map dec)206 (partial take-while (comp not zero?)))207 [move-1 move-2 move-3 move-4]))209 types210 (set (list (types type-1)211 (types type-2)))212 TMs|HMs213 (map214 (comp215 (partial map first)216 (partial remove (comp zero? second)))217 (split-at218 50219 (map vector220 (rest(range))221 (reduce concat222 (map223 #(take 8224 (concat (bit-list %)225 (repeat 0)))227 TMs|HMs)))))229 TMs (vec (first TMs|HMs))230 HMs (take 5 (map (partial + -50) (vec (second TMs|HMs))))233 ]236 {:dex# n237 :base-moves base-moves238 :types types239 :TMs TMs240 :HMs HMs241 :base-hp rating-hp242 :base-atk rating-atk243 :base-def rating-def244 :base-speed rating-speed245 :base-special rating-special246 :o0 pic-dimensions247 :o1 ptr-pic-obverse-1248 :o2 ptr-pic-obverse-2249 }))251 (partition entry-size252 (take (* entry-size pkmn-count)253 (drop 0x383DE254 rom))))))))256 #+end_src259 ** Pok\eacute{}mon evolutions260 #+name: evolution-header261 #+begin_src clojure262 (defn format-evo263 "Parse a sequence of evolution data, returning a map. First is the264 method: 0 = end-evolution-data. 1 = level-up, 2 = item, 3 = trade. Next is an item id, if the265 method of evolution is by item (only stones will actually make pokemon266 evolve, for some auxillary reason.) Finally, the minimum level for267 evolution to occur (level 1 means no limit, which is used for trade268 and item evolutions), followed by the internal id of the pokemon269 into which to evolve. Hence, level up and trade evolutions are270 described with 3271 bytes; item evolutions with four."272 [coll]273 (let [method (first coll)]274 (cond (empty? coll) []275 (= 0 method) [] ;; just in case276 (= 1 method) ;; level-up evolution277 (conj (format-evo (drop 3 coll))278 {:method :level-up279 :min-level (nth coll 1)280 :into (dec (nth coll 2))})282 (= 2 method) ;; item evolution283 (conj (format-evo (drop 4 coll))284 {:method :item285 :item (dec (nth coll 1))286 :min-level (nth coll 2)287 :into (dec (nth coll 3))})289 (= 3 method) ;; trade evolution290 (conj (format-evo (drop 3 coll))291 {:method :trade292 :min-level (nth coll 1) ;; always 1 for trade.293 :into (dec (nth coll 2))}))))296 (defn hxc-ptrs-evolve297 "A hardcoded collection of 190 pointers to alternating evolution/learnset data,298 in internal order."299 ([]300 (hxc-ptrs-evolve com.aurellem.gb.gb-driver/original-rom))301 ([rom]302 (let [303 pkmn-count (count (hxc-pokenames-raw)) ;; 190304 ptrs305 (map (fn [[a b]] (low-high a b))306 (partition 2307 (take (* 2 pkmn-count)308 (drop 0x3b1e5 rom))))]309 (map (partial + 0x34000) ptrs)311 )))312 #+end_src314 #+name:evolution315 #+begin_src clojure317 (defn hxc-evolution318 "Hardcoded evolution data in memory. The data exists at ROM@34000,319 sorted by internal order. Pointers to the data exist at ROM@3B1E5; see also, hxc-ptrs-evolve."320 ([] (hxc-evolution com.aurellem.gb.gb-driver/original-rom))321 ([rom]322 (apply assoc {}323 (interleave324 (hxc-pokenames rom)325 (map326 (comp327 format-evo328 (partial take-while (comp not zero?))329 #(drop % rom))330 (hxc-ptrs-evolve rom)331 )))))333 (defn hxc-evolution-pretty334 "Like hxc-evolution, except it uses the names of items and pokemon335 --- grabbed from ROM --- rather than their numerical identifiers."336 ([] (hxc-evolution-pretty com.aurellem.gb.gb-driver/original-rom))337 ([rom]338 (let339 [poke-names (vec (hxc-pokenames rom))340 item-names (vec (hxc-items rom))341 use-names342 (fn [m]343 (loop [ks (keys m) new-map m]344 (let [k (first ks)]345 (cond (nil? ks) new-map346 (= k :into)347 (recur348 (next ks)349 (assoc new-map350 :into351 (poke-names352 (:into353 new-map))))354 (= k :item)355 (recur356 (next ks)357 (assoc new-map358 :item359 (item-names360 (:item new-map))))361 :else362 (recur363 (next ks)364 new-map)365 ))))]367 (into {}368 (map (fn [[pkmn evo-coll]]369 [pkmn (map use-names evo-coll)])370 (hxc-evolution rom))))))373 #+end_src376 ** Level-up moves (learnsets)377 #+name: learnsets378 #+begin_src clojure381 (defn hxc-learnsets382 "Hardcoded map associating pokemon names to lists of pairs [lvl383 move] of abilities they learn as they level up. The data384 exists at ROM@34000, sorted by internal order. Pointers to the data385 exist at ROM@3B1E5; see also, hxc-ptrs-evolve"386 ([] (hxc-learnsets com.aurellem.gb.gb-driver/original-rom))387 ([rom]388 (apply assoc389 {}390 (interleave391 (hxc-pokenames rom)392 (map (comp393 (partial map394 (fn [[lvl mv]] [lvl (dec mv)]))395 (partial partition 2)396 ;; keep the learnset data397 (partial take-while (comp not zero?))398 ;; skip the evolution data399 rest400 (partial drop-while (comp not zero?)))401 (map #(drop % rom)402 (hxc-ptrs-evolve rom)))))))404 (defn hxc-learnsets-pretty405 "Live hxc-learnsets except it reports the name of each move --- as406 it appears in rom --- rather than the move index."407 ([] (hxc-learnsets-pretty com.aurellem.gb.gb-driver/original-rom))408 ([rom]409 (let [moves (vec(map format-name (hxc-move-names)))]410 (into {}411 (map (fn [[pkmn learnset]]412 [pkmn (map (fn [[lvl mv]] [lvl (moves mv)])413 learnset)])414 (hxc-learnsets rom))))))418 #+end_src422 * Pok\eacute{}mon II : the Pok\eacute{}dex423 ** Species vital stats424 #+name: dex-stats425 #+begin_src clojure426 (defn hxc-pokedex-stats427 "The hardcoded pokedex stats (species height weight) in memory. List428 begins at ROM@40687"429 ([] (hxc-pokedex-stats com.aurellem.gb.gb-driver/original-rom))430 ([rom]431 (let [pokedex-names (zipmap (range) (hxc-pokedex-names rom))432 pkmn-count (count pokedex-names)433 ]434 ((fn capture-stats435 [n stats data]436 (if (zero? n) stats437 (let [[species438 [_439 height-ft440 height-in441 weight-1442 weight-2443 _444 dex-ptr-1445 dex-ptr-2446 dex-bank447 _448 & data]]449 (split-with (partial not= 0x50) data)]450 (recur (dec n)451 (assoc stats452 (pokedex-names (- pkmn-count (dec n)))453 {:species454 (format-name (character-codes->str species))455 :height-ft456 height-ft457 :height-in458 height-in459 :weight460 (/ (low-high weight-1 weight-2) 10.)462 ;; :text463 ;; (character-codes->str464 ;; (take-while465 ;; (partial not= 0x50)466 ;; (drop467 ;; (+ 0xB8000468 ;; -0x4000469 ;; (low-high dex-ptr-1 dex-ptr-2))470 ;; rom)))471 })473 data)476 )))478 pkmn-count479 {}480 (drop 0x40687 rom))) ))481 #+end_src483 ** Species synopses485 #+name: dex-text486 #+begin_src clojure487 (def hxc-pokedex-text-raw488 "The hardcoded pokedex entries in memory. List begins at489 ROM@B8000, shortly before move names."490 (hxc-thunk-words 0xB8000 14754))495 (defn hxc-pokedex-text496 "The hardcoded pokedex entries in memory, presented as an497 associative hash map. List begins at ROM@B8000."498 ([] (hxc-pokedex-text com.aurellem.gb.gb-driver/original-rom))499 ([rom]500 (zipmap501 (hxc-pokedex-names rom)502 (cons nil ;; for missingno.503 (hxc-pokedex-text-raw rom)))))504 #+end_src507 ** Pok\eacute{}mon cries508 #+name: pokecry509 #+begin_src clojure510 (defn hxc-cry511 "The pokemon cry data in internal order. List begins at ROM@39462"512 ([](hxc-cry com.aurellem.gb.gb-driver/original-rom))513 ([rom]514 (zipmap515 (hxc-pokenames rom)516 (map517 (fn [[cry-id pitch length]]518 {:cry-id cry-id519 :pitch pitch520 :length length}521 )522 (partition 3523 (drop 0x39462 rom))))))525 (defn hxc-cry-groups526 ([] (hxc-cry-groups com.aurellem.gb.gb-driver/original-rom))527 ([rom]528 (map #(mapv first529 (filter530 (fn [[k v]]531 (= % (:cry-id v)))532 (hxc-cry)))533 ((comp534 range535 count536 set537 (partial map :cry-id)538 vals539 hxc-cry)540 rom))))543 (defn cry-conversion!544 "Convert Porygon's cry in ROM to be the cry of the given pokemon."545 [pkmn]546 (write-rom!547 (rewrite-memory548 (vec(rom))549 0x3965D550 (map second551 ((hxc-cry) pkmn)))))553 #+end_src555 ** COMMENT Names of permanent stats556 0DD4D-DD72558 * Items559 ** Item names560 #+name: item-names561 #+begin_src clojure563 (def hxc-items-raw564 "The hardcoded names of the items in memory. List begins at565 ROM@045B7"566 (hxc-thunk-words 0x45B7 870))568 (def hxc-items569 "The hardcoded names of the items in memory, presented as570 keywords. List begins at ROM@045B7. See also, hxc-items-raw."571 (comp (partial map format-name) hxc-items-raw))572 #+end_src574 ** Item prices575 #+name: item-prices576 #+begin_src clojure577 (defn hxc-item-prices578 "The hardcoded list of item prices in memory. List begins at ROM@4495"579 ([] (hxc-item-prices com.aurellem.gb.gb-driver/original-rom))580 ([rom]581 (let [items (hxc-items rom)582 price-size 3]583 (zipmap items584 (map (comp585 ;; zero-cost items are "priceless"586 #(if (zero? %) :priceless %)587 decode-bcd butlast)588 (partition price-size589 (take (* price-size (count items))590 (drop 0x4495 rom))))))))591 #+end_src592 ** Vendor inventories594 #+name: item-vendors595 #+begin_src clojure596 (defn hxc-shops597 ([] (hxc-shops com.aurellem.gb.gb-driver/original-rom))598 ([rom]599 (let [items (zipmap (range) (hxc-items rom))601 ;; temporarily softcode the TM items602 items (into603 items604 (map (juxt identity605 (comp keyword606 (partial str "tm-")607 (partial + 1 -200)608 ))609 (take 200 (drop 200 (range)))))611 ]613 ((fn parse-shop [coll [num-items & items-etc]]614 (let [inventory (take-while615 (partial not= 0xFF)616 items-etc)617 [separator & items-etc] (drop num-items (rest items-etc))]618 (if (= separator 0x50)619 (map (partial mapv (comp items dec)) (conj coll inventory))620 (recur (conj coll inventory) items-etc)621 )622 ))624 '()625 (drop 0x233C rom))628 )))629 #+end_src631 #+results: item-vendors632 : #'com.aurellem.gb.hxc/hxc-shops636 * Types637 ** Names of types638 #+name: type-names639 #+begin_src clojure640 (def hxc-types641 "The hardcoded type names in memory. List begins at ROM@27D99,642 shortly before hxc-titles."643 (hxc-thunk-words 0x27D99 102))645 #+end_src647 ** Type effectiveness648 #+name: type-advantage649 #+begin_src clojure650 (defn hxc-advantage651 ;; in-game multipliers are stored as 10x their effective value652 ;; to allow for fractional multipliers like 1/2654 "The hardcoded type advantages in memory, returned as tuples of655 atk-type def-type multiplier. By default (i.e. if not listed here),656 the multiplier is 1. List begins at 0x3E62D."657 ([] (hxc-advantage com.aurellem.gb.gb-driver/original-rom))658 ([rom]659 (map660 (fn [[atk def mult]] [(get pkmn-types atk (hex atk))661 (get pkmn-types def (hex def))662 (/ mult 10)])663 (partition 3664 (take-while (partial not= 0xFF)665 (drop 0x3E62D rom))))))666 #+end_src670 * Moves671 ** Names of moves672 #+name: move-names673 #+begin_src clojure674 (def hxc-move-names675 "The hardcoded move names in memory. List begins at ROM@BC000"676 (hxc-thunk-words 0xBC000 1551))677 #+end_src679 ** Properties of moves681 #+name: move-data682 #+begin_src clojure683 (defn hxc-move-data684 "The hardcoded (basic (move effects)) in memory. List begins at685 0x38000. Returns a map of {:name :power :accuracy :pp :fx-id686 :fx-txt}. The move descriptions are handwritten, not hardcoded."687 ([]688 (hxc-move-data com.aurellem.gb.gb-driver/original-rom))689 ([rom]690 (let [names (vec (hxc-move-names rom))691 move-count (count names)692 move-size 6693 types pkmn-types ;;; !! hardcoded types694 ]695 (zipmap (map format-name names)696 (map697 (fn [[idx effect power type-id accuracy pp]]698 {:name (names (dec idx))699 :power power700 :accuracy accuracy701 :pp pp702 :type (types type-id)703 :fx-id effect704 :fx-txt (get move-effects effect)705 }706 )708 (partition move-size709 (take (* move-size move-count)710 (drop 0x38000 rom))))))))714 (defn hxc-move-data*715 "Like hxc-move-data, but reports numbers as hexadecimal symbols instead."716 ([]717 (hxc-move-data* com.aurellem.gb.gb-driver/original-rom))718 ([rom]719 (let [names (vec (hxc-move-names rom))720 move-count (count names)721 move-size 6722 format-name (fn [s]723 (keyword (.toLowerCase724 (apply str725 (map #(if (= % \space) "-" %) s)))))726 ]727 (zipmap (map format-name names)728 (map729 (fn [[idx effect power type accuracy pp]]730 {:name (names (dec idx))731 :power power732 :accuracy (hex accuracy)733 :pp pp734 :fx-id (hex effect)735 :fx-txt (get move-effects effect)736 }737 )739 (partition move-size740 (take (* move-size move-count)741 (drop 0x38000 rom))))))))743 #+end_src745 ** TM and HM moves747 #+name: machines748 #+begin_src clojure749 (defn hxc-machines750 "The hardcoded moves taught by TMs and HMs. List begins at ROM@1232D."751 ([] (hxc-machines752 com.aurellem.gb.gb-driver/original-rom))753 ([rom]754 (let [moves (hxc-move-names rom)]755 (zipmap756 (range)757 (take-while758 (comp not nil?)759 (map (comp760 format-name761 (zipmap762 (range)763 moves)764 dec)765 (take 100766 (drop 0x1232D rom))))))))768 #+end_src774 ** COMMENT Status ailments776 * Places777 ** Names of places779 #+name: places780 #+begin_src clojure781 (def hxc-places782 "The hardcoded place names in memory. List begins at783 ROM@71500. [Cinnabar] Mansion seems to be dynamically calculated."784 (hxc-thunk-words 0x71500 560))786 #+end_src788 ** Wild Pok\eacute{}mon demographics789 #+name: wilds790 #+begin_src clojure794 (defn hxc-ptrs-wild795 "A list of the hardcoded wild encounter data in memory. Pointers796 begin at ROM@0CB95; data begins at ROM@0x04D89"797 ([] (hxc-ptrs-wild com.aurellem.gb.gb-driver/original-rom))798 ([rom]799 (let [ptrs800 (map (fn [[a b]] (+ a (* 0x100 b)))801 (take-while (partial not= (list 0xFF 0xFF))802 (partition 2 (drop 0xCB95 rom))))]803 ptrs)))807 (defn hxc-wilds808 "A list of the hardcoded wild encounter data in memory. Pointers809 begin at ROM@0CB95; data begins at ROM@0x04D89"810 ([] (hxc-wilds com.aurellem.gb.gb-driver/original-rom))811 ([rom]812 (let [pokenames (zipmap (range) (hxc-pokenames rom))]813 (map814 (partial map (fn [[a b]] {:species (pokenames (dec b)) :level815 a}))816 (partition 10818 (take-while (comp (partial not= 1)819 first)820 (partition 2821 (drop 0xCD8C rom))823 ))))))825 #+end_src831 * Appendices835 ** Mapping the ROM837 | ROM address (hex) | Description | Format | Example |838 |-------------------+-----------------+-----------------+-----------------|839 | | <15> | <15> | <15> |840 | 0233C- | Shop inventories. | | |841 | 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. |842 | 045B7-0491E | Names of the items in memory. | Variable-length item names (strings of character codes). Names are separated by a single 0x80 character. | MASTER BALL#ULTRA BALL#... |843 | 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). |844 | 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. |845 | 06698- | ? Background music. | | |846 | 0822E-082F? | Pointers to background music, part I. | | |847 | 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). |848 |-------------------+-----------------+-----------------+-----------------|849 | 0DADB. | Amount of HP restored by Hyper Potion. | The HP consists of a single byte. TODO: Discover what the surrounding data does, and find the data for the amount of HP restored by other items: Fresh Water (50HP), Soda (60HP), Lemonade(80HP). | 200 |850 | 0DAE0. | Amount of HP restored by Super Potion. | " | 50 |851 | 0DAE3. | Amount of HP restored by Potion. | " | 20 |852 |-------------------+-----------------+-----------------+-----------------|853 | 0DD4D-DD72 | Names of permanent stats. | Variable-length strings separated by 0x50. | #HEALTH#ATTACK#DEFENSE#SPEED#SPECIAL# |854 | 1195C-1196A | The two terms for being able/unable to learn a TM/HM. | Variable-length strings separated by 0x50. | ABLE#NOT ABLE# |855 | 119C0-119CE | The two terms for being able/unable to evolve using the current stone. | Variable-length strings separated by 0x50. | ABLE#NOT ABLE# |856 | 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), ... |857 | 27D99-27DFF | Names of the Pok\eacute{}mon types. | Variable-length type names (strings of character codes). Names are separated by a single 0x80 character. | NORMAL#FIGHTING#... |858 | 27E77- | Trainer title names. | Variable-length names separated by 0x80. | YOUNGSTER#BUG CATCHER#LASS#... |859 | 34000- | | | |860 | 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. |861 | 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. | | |862 | 39462- | The Pok\eacute{}mon cry data. | Fixed-length (3 byte) descriptions of cries. | |863 | 3B1E5- | Pointers to evolution/learnset data. | | |864 | 3B361- | 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. | |865 | 40687- | Species data from the Pok\eacute{}dex: species name, height, weight, etc. | Fixed-length sequences of bytes. See below for specifics. | |866 | 410B1- | A conversion table between internal order and Pokedex order. | | |867 | 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]... |868 | 71500- | Names of places. | | |869 | 7C249-7C2?? | Pointers to background music, pt II. | | |870 | 98000- | Dialogue | | |871 | B8000- | The text of each Pokemon's Pok\eacute{}dex entry. | | |872 | BC000-BC60E | Move names. | Variable-length move names, separated by 0x80. The moves are in internal order. | POUND#KARATE CHOP#DOUBLESLAP#COMET PUNCH#... |873 | 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 0x80. The names are in \ldquo{}internal order\rdquo{}. | RHYDON####KANGASKHANNIDORAN♂#... |874 | | | | |875 | | | | |879 ** Internal Pok\eacute{}mon IDs880 ** Type IDs882 #+name: type-ids883 #+begin_src clojure884 (def pkmn-types885 [:normal ;;0886 :fighting ;;1887 :flying ;;2888 :poison ;;3889 :ground ;;4890 :rock ;;5891 :bird ;;6892 :bug ;;7893 :ghost ;;8894 :A895 :B896 :C897 :D898 :E899 :F900 :G901 :H902 :I903 :J904 :K905 :fire ;;20 (0x14)906 :water ;;21 (0x15)907 :grass ;;22 (0x16)908 :electric ;;23 (0x17)909 :psychic ;;24 (0x18)910 :ice ;;25 (0x19)911 :dragon ;;26 (0x1A)912 ])913 #+end_src915 ** Basic effects of moves917 *** Table of basic effects919 The possible effects of moves in Pok\eacute{}mon \mdash{} for example, dealing920 damage, leeching health, or potentially poisoning the opponent921 \mdash{} are stored in a table. Each move has exactly one effect, and922 different moves might have the same effect.924 For example, Leech Life, Mega Drain, and Absorb all have effect ID #3, which is \ldquo{}Leech half of the inflicted damage.\rdquo{}926 All the legitimate move effects are listed in the table927 below. Here are some notes for reading it:929 - Whenever an effect has a chance of doing something (like a chance of930 poisoning the opponent), I list the chance as a hexadecimal amount out of 256 to avoid rounding errors. To convert the hex amount into a percentage, divide by 256.931 - For some effects, the description is too cumbersome to932 write. Instead, I just write a move name933 in parentheses, like: (leech seed). That move gives a characteristic example934 of the effect.935 - I use the abbreviations =atk=, =def=, =spd=, =spc=, =acr=, =evd= for936 attack, defense, speed, special, accuracy, and evasion.937 .941 | ID (hex) | Description | Notes |942 |----------+-------------------------------------------------------------------------------------------------+------------------------------------------------------------------|943 | 0 | normal damage | |944 | 1 | no damage, just sleep | TODO: find out how many turns |945 | 2 | 0x4C chance of poison | |946 | 3 | leech half of inflicted damage | |947 | 4 | 0x19 chance of burn | |948 | 5 | 0x19 chance of freeze | |949 | 6 | 0x19 chance of paralysis | |950 | 7 | user faints; opponent's defense is halved during attack. | |951 | 8 | leech half of inflicted damage ONLY if the opponent is asleep | |952 | 9 | imitate last attack | |953 | A | user atk +1 | |954 | B | user def +1 | |955 | C | user spd +1 | |956 | D | user spc +1 | |957 | E | user acr +1 | This effect is unused. |958 | F | user evd +1 | |959 | 10 | get post-battle money = 2 * level * uses | |960 | 11 | move has 0xFE acr, regardless of battle stat modifications. | |961 | 12 | opponent atk -1 | |962 | 13 | opponent def -1 | |963 | 14 | opponent spd -1 | |964 | 15 | opponent spc -1 | |965 | 16 | opponent acr -1 | |966 | 17 | opponent evd -1 | |967 | 18 | converts user's type to opponent's. | |968 | 19 | (haze) | |969 | 1A | (bide) | |970 | 1B | (thrash) | |971 | 1C | (teleport) | |972 | 1D | (fury swipes) | |973 | 1E | attacks 2-5 turns | Unused. TODO: find out what it does. |974 | 1F | 0x19 chance of flinching | |975 | 20 | opponent sleep for 1-7 turns | |976 | 21 | 0x66 chance of poison | |977 | 22 | 0x4D chance of burn | |978 | 23 | 0x4D chance of freeze | |979 | 24 | 0x4D chance of paralysis | |980 | 25 | 0x4D chance of flinching | |981 | 26 | one-hit KO | |982 | 27 | charge one turn, atk next. | |983 | 28 | fixed damage, leaves 1HP. | Is the fixed damage the power of the move? |984 | 29 | fixed damage. | Like seismic toss, dragon rage, psywave. |985 | 2A | atk 2-5 turns; opponent can't attack | The odds of attacking for /n/ turns are: (0 0x60 0x60 0x20 0x20) |986 | 2B | charge one turn, atk next. (can't be hit when charging) | |987 | 2C | atk hits twice. | |988 | 2D | user takes 1 damage if misses. | |989 | 2E | evade status-lowering effects | Caused by you or also your opponent? |990 | 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. |991 | 30 | atk causes recoil dmg = 1/4 dmg dealt | |992 | 31 | confuses opponent | |993 | 32 | user atk +2 | |994 | 33 | user def +2 | |995 | 34 | user spd +2 | |996 | 35 | user spc +2 | |997 | 36 | user acr +2 | This effect is unused. |998 | 37 | user evd +2 | This effect is unused. |999 | 38 | restores up to half of user's max hp. | |1000 | 39 | (transform) | |1001 | 3A | opponent atk -2 | |1002 | 3B | opponent def -2 | |1003 | 3C | opponent spd -2 | |1004 | 3D | opponent spc -2 | |1005 | 3E | opponent acr -2 | |1006 | 3F | opponent evd -2 | |1007 | 40 | doubles user spc when attacked | |1008 | 41 | doubles user def when attacked | |1009 | 42 | just poisons opponent | |1010 | 43 | just paralyzes opponent | |1011 | 44 | 0x19 chance opponent atk -1 | |1012 | 45 | 0x19 chance opponent def -1 | |1013 | 46 | 0x19 chance opponent spd -1 | |1014 | 47 | 0x4C chance opponent spc -1 | |1015 | 48 | 0x19 chance opponent acr -1 | |1016 | 49 | 0x19 chance opponent evd -1 | |1017 | 4A | ??? | ;; unused? no effect? |1018 | 4B | ??? | ;; unused? no effect? |1019 | 4C | 0x19 chance of confusing the opponent | |1020 | 4D | atk hits twice. 0x33 chance opponent poisioned. | |1021 | 4E | broken. crash the game after attack. | |1022 | 4F | (substitute) | |1023 | 50 | unless opponent faints, user must recharge after atk. some exceptions apply | |1024 | 51 | (rage) | |1025 | 52 | (mimic) | |1026 | 53 | (metronome) | |1027 | 54 | (leech seed) | |1028 | 55 | does nothing (splash) | |1029 | 56 | (disable) | |1030 #+end_src1032 *** Source1033 #+name: move-effects1034 #+begin_src clojure1035 (def move-effects1036 ["normal damage"1037 "no damage, just opponent sleep" ;; how many turns? is atk power ignored?1038 "0x4C chance of poison"1039 "leech half of inflicted damage"1040 "0x19 chance of burn"1041 "0x19 chance of freeze"1042 "0x19 chance of paralyze"1043 "user faints; opponent defense halved during attack."1044 "leech half of inflicted damage ONLY if sleeping opponent."1045 "imitate last attack"1046 "user atk +1"1047 "user def +1"1048 "user spd +1"1049 "user spc +1"1050 "user acr +1" ;; unused?!1051 "user evd +1"1052 "get post-battle $ = 2*level*uses"1053 "0xFE acr, no matter what."1054 "opponent atk -1" ;; acr taken from move acr?1055 "opponent def -1" ;;1056 "opponent spd -1" ;;1057 "opponent spc -1" ;;1058 "opponent acr -1";;1059 "opponent evd -1"1060 "converts user's type to opponent's."1061 "(haze)"1062 "(bide)"1063 "(thrash)"1064 "(teleport)"1065 "(fury swipes)"1066 "attacks 2-5 turns" ;; unused? like rollout?1067 "0x19 chance of flinch"1068 "opponent sleep for 1-7 turns"1069 "0x66 chance of poison"1070 "0x4D chance of burn"1071 "0x4D chance of freeze"1072 "0x4D chance of paralyze"1073 "0x4D chance of flinch"1074 "one-hit KO"1075 "charge one turn, atk next."1076 "fixed damage, leaves 1HP." ;; how is dmg determined?1077 "fixed damage." ;; cf seismic toss, dragon rage, psywave.1078 "atk 2-5 turns; opponent can't attack" ;; unnormalized? (0 0x60 0x60 0x20 0x20)1079 "charge one turn, atk next. (can't be hit when charging)"1080 "atk hits twice."1081 "user takes 1 damage if misses."1082 "evade status-lowering effects" ;;caused by you or also your opponent?1083 "(broken) if user is slower than opponent, makes critical hit impossible, otherwise has no effect"1084 "atk causes recoil dmg = 1/4 dmg dealt"1085 "confuses opponent" ;; acr taken from move acr1086 "user atk +2"1087 "user def +2"1088 "user spd +2"1089 "user spc +2"1090 "user acr +2" ;; unused!1091 "user evd +2" ;; unused!1092 "restores up to half of user's max hp." ;; broken: fails if the difference1093 ;; b/w max and current hp is one less than a multiple of 256.1094 "(transform)"1095 "opponent atk -2"1096 "opponent def -2"1097 "opponent spd -2"1098 "opponent spc -2"1099 "opponent acr -2"1100 "opponent evd -2"1101 "doubles user spc when attacked"1102 "doubles user def when attacked"1103 "just poisons opponent" ;;acr taken from move acr1104 "just paralyzes opponent" ;;1105 "0x19 chance opponent atk -1"1106 "0x19 chance opponent def -1"1107 "0x19 chance opponent spd -1"1108 "0x4C chance opponent spc -1" ;; context suggest chance is 0x191109 "0x19 chance opponent acr -1"1110 "0x19 chance opponent evd -1"1111 "???" ;; unused? no effect?1112 "???" ;; unused? no effect?1113 "0x19 chance opponent confused"1114 "atk hits twice. 0x33 chance opponent poisioned."1115 "broken. crash the game after attack."1116 "(substitute)"1117 "unless opponent faints, user must recharge after atk. some1118 exceptions apply."1119 "(rage)"1120 "(mimic)"1121 "(metronome)"1122 "(leech seed)"1123 "does nothing (splash)"1124 "(disable)"1125 ])1126 #+end_src1129 ** Alphabet code1131 * Source1133 #+begin_src clojure :tangle ../clojure/com/aurellem/gb/hxc.clj1135 (ns com.aurellem.gb.hxc1136 (:use (com.aurellem.gb assembly characters gb-driver util mem-util1137 constants species))1138 (:import [com.aurellem.gb.gb_driver SaveState]))1140 ; ************* HANDWRITTEN CONSTANTS1142 <<type-ids>>1145 ;; question: when status effects claim to take1146 ;; their accuracy from the move accuracy, does1147 ;; this mean that the move always "hits" but the1148 ;; status effect may not?1150 <<move-effects>>1152 ;; ************** HARDCODED DATA1154 <<hxc-thunks>>1155 ;; --------------------------------------------------1157 <<pokenames>>1158 <<type-names>>1160 ;; http://hax.iimarck.us/topic/581/1161 <<pokecry>>1164 <<item-names>>1168 (def hxc-titles1169 "The hardcoded names of the trainer titles in memory. List begins at1170 ROM@27E77"1171 (hxc-thunk-words 0x27E77 196))1174 <<dex-text>>1176 ;; In red/blue, pokedex stats are in internal order.1177 ;; In yellow, pokedex stats are in pokedex order.1178 <<dex-stats>>1183 <<places>>1185 (defn hxc-dialog1186 "The hardcoded dialogue in memory, including in-game alerts. Dialog1187 seems to be separated by 0x57 instead of 0x50 (END). Begins at ROM@98000."1188 ([rom]1189 (map character-codes->str1190 (take-nth 21191 (partition-by #(= % 0x57)1192 (take 0x0F7281193 (drop 0x98000 rom))))))1194 ([]1195 (hxc-dialog com.aurellem.gb.gb-driver/original-rom)))1198 <<move-names>>1199 <<move-data>>1201 <<machines>>1205 (defn internal-id1206 ([rom]1207 (zipmap1208 (hxc-pokenames rom)1209 (range)))1210 ([]1211 (internal-id com.aurellem.gb.gb-driver/original-rom)))1217 ;; nidoran gender change upon levelup1218 ;; (->1219 ;; @current-state1220 ;; rom1221 ;; vec1222 ;; (rewrite-memory1223 ;; (nth (hxc-ptrs-evolve) ((internal-id) :nidoran♂))1224 ;; [1 1 15])1225 ;; (rewrite-memory1226 ;; (nth (hxc-ptrs-evolve) ((internal-id) :nidoran♀))1227 ;; [1 1 3])1228 ;; (write-rom!)1230 ;; )1234 <<type-advantage>>1238 <<evolution-header>>1239 <<evolution>>1240 <<learnsets>>1241 <<pokebase>>1244 (defn hxc-intro-pkmn1245 "The hardcoded pokemon to display in Prof. Oak's introduction; the pokemon's1246 internal id is stored at ROM@5EDB."1247 ([] (hxc-intro-pkmn1248 com.aurellem.gb.gb-driver/original-rom))1249 ([rom]1250 (nth (hxc-pokenames rom) (nth rom 0x5EDB))))1252 (defn sxc-intro-pkmn!1253 "Set the hardcoded pokemon to display in Prof. Oak's introduction."1254 [pokemon]1255 (write-rom!1256 (rewrite-rom 0x5EDB1257 [1258 (inc1259 ((zipmap1260 (hxc-pokenames)1261 (range))1262 pokemon))])))1265 <<item-prices>>1267 <<item-vendors>>1269 <<wilds>>1272 ;; ********************** MANIPULATION FNS1275 (defn same-type1276 ([pkmn move]1277 (same-type1278 com.aurellem.gb.gb-driver/original-rom pkmn move))1279 ([rom pkmn move]1280 (((comp :types (hxc-pokemon-base rom)) pkmn)1281 ((comp :type (hxc-move-data rom)) move))))1286 (defn submap?1287 "Compares the two maps. Returns true if map-big has the same associations as map-small, otherwise false."1288 [map-small map-big]1289 (cond (empty? map-small) true1290 (and1291 (contains? map-big (ffirst map-small))1292 (= (get map-big (ffirst map-small))1293 (second (first map-small))))1294 (recur (next map-small) map-big)1296 :else false))1299 (defn search-map [proto-map maps]1300 "Returns all the maps that make the same associations as proto-map."1301 (some (partial submap? proto-map) maps))1303 (defn filter-vals1304 "Returns a map consisting of all the pairs [key val] for1305 which (pred key) returns true."1306 [pred map]1307 (reduce (partial apply assoc) {}1308 (filter (fn [[k v]] (pred v)) map)))1311 (defn search-moves1312 "Returns a subcollection of all hardcoded moves with the1313 given attributes. Attributes consist of :name :power1314 :accuracy :pp :fx-id1315 (and also :fx-txt, but it contains the same information1316 as :fx-id)"1317 ([attribute-map]1318 (search-moves1319 com.aurellem.gb.gb-driver/original-rom attribute-map))1320 ([rom attribute-map]1321 (filter-vals (partial submap? attribute-map)1322 (hxc-move-data rom))))1328 ;; note: 0x2f31 contains the names "TM" "HM"?1330 ;; note for later: credits start at F12901332 ;; note: DADB hyper-potion-hp _ _ _ super-potion-hp _ _ _ potion-hp ??1334 ;; note: DD4D spells out pokemon vital stat names ("speed", etc.)1336 ;; note: 1195C-6A says ABLE#NOT ABLE#, but so does 119C0-119CE.1337 ;; The first instance is for Machines; the second, for stones.1339 ;; 0x251A (in indexable mem): image decompression routine seems to begin here.1342 (comment1344 (def hxc-later1345 "Running this code produces, e.g. hardcoded names NPCs give1346 their pokemon. Will sort through it later."1347 (print (character-codes->str(take 100001348 (drop 0x715971349 (rom (root)))))))1351 (let [dex1352 (partition-by #(= 0x50 %)1353 (take 25401354 (drop 0x406871355 (rom (root)))))]1356 (def dex dex)1357 (def hxc-species1358 (map character-codes->str1359 (take-nth 4 dex))))1360 )1363 #+end_src1365 #+results:1366 : nil