Mercurial > vba-clojure
view org/rom.org @ 471:5f87c3e46c22
Added interactive Pokemon cries.
author | Dylan Holmes <ocsenave@gmail.com> |
---|---|
date | Sun, 29 Apr 2012 20:35:58 -0500 |
parents | c02108ddcb35 |
children | 69d1787522c7 |
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 yes9 #+OPTIONS: num:212 # about map headers http://datacrystal.romhacking.net/wiki/Pokemon_Red/Blue:Notes13 # map headers Yellow http://www.pokecommunity.com/archive/index.php/t-235311.html14 # pokedollar: U+20B115 * Introduction16 This article contains the results of my investigations with17 Pok\eacute{}mon Yellow as I searched for interesting18 data in the ROM. By using the Clojure language interface19 written by Robert[fn::This Clojure interface will be published to aurellem.org soon.], I20 was able to interact with the game in real-time, sending commands and21 gathering data. The result is a manifestly accurate map of22 Pok\eacute{}mon Yellow; every result23 comes with runnable code that /works/. You can see the code and the output of24 every function and confirm for yourself that they are all correct. I25 hope you like the result!27 To orient yourself, you can look for a specific topic in the table of contents28 above, or browse the [[#sec-9-1][map of the ROM]], below.31 If you have any questions or comments, please e-mail =rlm@mit.edu=.34 ** COMMENT Getting linguistic data: names, words, etc.36 Some of the simplest data39 One of the simplest data structures in the Pok\eacute{} ROM is an40 unbroken list of strings that either (a) all have a specific length,41 or (b) are all separated by the same character.43 Because lots of good data has this format, we'll start by writing a44 template function to extract it:46 #+name: hxc-thunks47 #+begin_src clojure :results silent48 (defn hxc-thunk49 "Creates a thunk (nullary fn) that grabs data in a certain region of rom and50 splits it into a collection by 0x50. If rom is not supplied, uses the51 original rom data."52 [start length]53 (fn self54 ([rom]55 (take-nth 256 (partition-by #(= % 0x50)57 (take length58 (drop start rom)))))59 ([]60 (self com.aurellem.gb.gb-driver/original-rom))))62 (def hxc-thunk-words63 "Same as hxc-thunk, except it interprets the rom data as characters,64 returning a collection of strings."65 (comp66 (partial comp (partial map character-codes->str))67 hxc-thunk))69 #+end_src72 * Pok\eacute{}mon I73 ** Names of each species74 The names of the Pok\eacute{}mon species are stored in75 ROM@E8000. This name list is interesting, for a number of reasons:76 - The names are stored in [[ ][internal order]] rather than in the familiar77 Pok\eacute{}dex order. This seemingly random order probably represents the order in which the authors created or78 programmed in the Pok\eacute{}mon; it's used throughout the game.79 - There is enough space allocated for 190 Pok\eacute{}mon. As I80 understand it, there were originally going to be 190 Pok\eacute{}mon81 in Generation I, but the creators decided to defer some to82 Generation II. This explains why many Gen I and Gen II Pok\eacute{}mon83 have the same aesthetic feel.84 - The list is pockmarked with random gaps, due to the strange internal85 ordering86 and the 39 unused spaces [fn::190 allocated spaces minus 151 true Pok\eacute{}mon]. These missing spaces are filled with the87 placeholder name =MISSINGNO.= (\ldquo{}Missing number\rdquo{}).89 Each name is exactly ten letters long; whenever a name would be too short, the extra90 space is padded with the character 0x50.92 *** See the data94 Here you can see the raw data in three stages: in the first stage, we95 just grab the first few bytes starting from position 0xE8000. In the96 second stage, we partition the bytes into ten-letter chunks to show you97 where the names begin and end. In the final stage, we convert each98 byte into the letter it represents using the =character-codes->str=99 function. (0x50 is rendered as the symbol \ldquo{} =#= \rdquo{} for100 ease of reading).102 #+begin_src clojure :exports both :cache no :results output103 (ns com.aurellem.gb.hxc104 (:use (com.aurellem.gb assembly characters gb-driver util mem-util105 constants))106 (:import [com.aurellem.gb.gb_driver SaveState]))109 (println (take 100 (drop 0xE8000 (rom))))111 (println (partition 10 (take 100 (drop 0xE8000 (rom)))))113 (println (character-codes->str (take 100 (drop 0xE8000 (rom)))))116 #+end_src118 #+results:119 : (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)120 : ((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))121 : RHYDON####KANGASKHANNIDORAN♂##CLEFAIRY##SPEAROW###VOLTORB###NIDOKING##SLOWBRO###IVYSAUR###EXEGGUTOR#124 *** Automatically grab the data.126 #+name: pokenames127 #+begin_src clojure129 (defn hxc-pokenames-raw130 "The hardcoded names of the 190 species in memory. List begins at131 ROM@E8000. Although names in memory are padded with 0x50 to be 10 characters132 long, these names are stripped of padding. See also, hxc-pokedex-names"133 ([]134 (hxc-pokenames-raw com.aurellem.gb.gb-driver/original-rom))135 ([rom]136 (let [count-species 190137 name-length 10]138 (map character-codes->str139 (partition name-length140 (map #(if (= 0x50 %) 0x00 %)141 (take (* count-species name-length)142 (drop 0xE8000143 rom))))))))144 (def hxc-pokenames145 (comp146 (partial map format-name)147 hxc-pokenames-raw))152 (defn hxc-pokedex-names153 "The names of the pokemon in hardcoded pokedex order. List of the154 pokedex numbers of each pokemon (in internal order) begins at155 ROM@410B1. See also, hxc-pokenames."156 ([] (hxc-pokedex-names157 com.aurellem.gb.gb-driver/original-rom))158 ([rom]159 (let [names (hxc-pokenames rom)]160 (#(mapv %161 ((comp range count keys) %))162 (zipmap163 (take (count names)164 (drop 0x410b1 rom))166 names)))))168 #+end_src172 ** Generic species information174 #+name: pokebase175 #+begin_src clojure176 (defn hxc-pokemon-base177 ([] (hxc-pokemon-base com.aurellem.gb.gb-driver/original-rom))178 ([rom]179 (let [entry-size 28181 pokemon (rest (hxc-pokedex-names))182 pkmn-count (inc(count pokemon))183 types (apply assoc {}184 (interleave185 (range)186 pkmn-types)) ;;!! softcoded187 moves (apply assoc {}188 (interleave189 (range)190 (map format-name191 (hxc-move-names rom))))192 machines (hxc-machines)193 ]194 (zipmap195 pokemon196 (map197 (fn [[n198 rating-hp199 rating-atk200 rating-def201 rating-speed202 rating-special203 type-1204 type-2205 rarity206 rating-xp207 pic-dimensions ;; tile_width|tile_height (8px/tile)208 ptr-pic-obverse-1209 ptr-pic-obverse-2210 ptr-pic-reverse-1211 ptr-pic-reverse-2212 move-1213 move-2214 move-3215 move-4216 growth-rate217 &218 TMs|HMs]]219 (let220 [base-moves221 (mapv moves222 ((comp223 ;; since the game uses zero as a delimiter,224 ;; it must also increment all move indices by 1.225 ;; heren we decrement to correct this.226 (partial map dec)227 (partial take-while (comp not zero?)))228 [move-1 move-2 move-3 move-4]))230 types231 (set (list (types type-1)232 (types type-2)))233 TMs|HMs234 (map235 (comp236 (partial map first)237 (partial remove (comp zero? second)))238 (split-at239 50240 (map vector241 (rest(range))242 (reduce concat243 (map244 #(take 8245 (concat (bit-list %)246 (repeat 0)))248 TMs|HMs)))))250 TMs (vec (first TMs|HMs))251 HMs (take 5 (map (partial + -50) (vec (second TMs|HMs))))254 ]257 {:dex# n258 :base-moves base-moves259 :types types260 :TMs TMs261 :HMs HMs262 :base-hp rating-hp263 :base-atk rating-atk264 :base-def rating-def265 :base-speed rating-speed266 :base-special rating-special267 :o0 pic-dimensions268 :o1 ptr-pic-obverse-1269 :o2 ptr-pic-obverse-2270 }))272 (partition entry-size273 (take (* entry-size pkmn-count)274 (drop 0x383DE275 rom))))))))277 #+end_src280 ** Pok\eacute{}mon evolutions281 #+name: evolution-header282 #+begin_src clojure283 (defn format-evo284 "Parse a sequence of evolution data, returning a map. First is the285 method: 0 = end-evolution-data. 1 = level-up, 2 = item, 3 = trade. Next is an item id, if the286 method of evolution is by item (only stones will actually make pokemon287 evolve, for some auxillary reason.) Finally, the minimum level for288 evolution to occur (level 1 means no limit, which is used for trade289 and item evolutions), followed by the internal id of the pokemon290 into which to evolve. Hence, level up and trade evolutions are291 described with 3292 bytes; item evolutions with four."293 [coll]294 (let [method (first coll)]295 (cond (empty? coll) []296 (= 0 method) [] ;; just in case297 (= 1 method) ;; level-up evolution298 (conj (format-evo (drop 3 coll))299 {:method :level-up300 :min-level (nth coll 1)301 :into (dec (nth coll 2))})303 (= 2 method) ;; item evolution304 (conj (format-evo (drop 4 coll))305 {:method :item306 :item (dec (nth coll 1))307 :min-level (nth coll 2)308 :into (dec (nth coll 3))})310 (= 3 method) ;; trade evolution311 (conj (format-evo (drop 3 coll))312 {:method :trade313 :min-level (nth coll 1) ;; always 1 for trade.314 :into (dec (nth coll 2))}))))317 (defn hxc-ptrs-evolve318 "A hardcoded collection of 190 pointers to alternating evolution/learnset data,319 in internal order."320 ([]321 (hxc-ptrs-evolve com.aurellem.gb.gb-driver/original-rom))322 ([rom]323 (let [324 pkmn-count (count (hxc-pokenames-raw)) ;; 190325 ptrs326 (map (fn [[a b]] (low-high a b))327 (partition 2328 (take (* 2 pkmn-count)329 (drop 0x3b1e5 rom))))]330 (map (partial + 0x34000) ptrs)332 )))333 #+end_src335 #+name:evolution336 #+begin_src clojure338 (defn hxc-evolution339 "Hardcoded evolution data in memory. The data exists at ROM@34000,340 sorted by internal order. Pointers to the data exist at ROM@3B1E5; see also, hxc-ptrs-evolve."341 ([] (hxc-evolution com.aurellem.gb.gb-driver/original-rom))342 ([rom]343 (apply assoc {}344 (interleave345 (hxc-pokenames rom)346 (map347 (comp348 format-evo349 (partial take-while (comp not zero?))350 #(drop % rom))351 (hxc-ptrs-evolve rom)352 )))))354 (defn hxc-evolution-pretty355 "Like hxc-evolution, except it uses the names of items and pokemon356 --- grabbed from ROM --- rather than their numerical identifiers."357 ([] (hxc-evolution-pretty com.aurellem.gb.gb-driver/original-rom))358 ([rom]359 (let360 [poke-names (vec (hxc-pokenames rom))361 item-names (vec (hxc-items rom))362 use-names363 (fn [m]364 (loop [ks (keys m) new-map m]365 (let [k (first ks)]366 (cond (nil? ks) new-map367 (= k :into)368 (recur369 (next ks)370 (assoc new-map371 :into372 (poke-names373 (:into374 new-map))))375 (= k :item)376 (recur377 (next ks)378 (assoc new-map379 :item380 (item-names381 (:item new-map))))382 :else383 (recur384 (next ks)385 new-map)386 ))))]388 (into {}389 (map (fn [[pkmn evo-coll]]390 [pkmn (map use-names evo-coll)])391 (hxc-evolution rom))))))394 #+end_src397 ** Level-up moves (learnsets)398 #+name: learnsets399 #+begin_src clojure402 (defn hxc-learnsets403 "Hardcoded map associating pokemon names to lists of pairs [lvl404 move] of abilities they learn as they level up. The data405 exists at ROM@34000, sorted by internal order. Pointers to the data406 exist at ROM@3B1E5; see also, hxc-ptrs-evolve"407 ([] (hxc-learnsets com.aurellem.gb.gb-driver/original-rom))408 ([rom]409 (apply assoc410 {}411 (interleave412 (hxc-pokenames rom)413 (map (comp414 (partial map415 (fn [[lvl mv]] [lvl (dec mv)]))416 (partial partition 2)417 ;; keep the learnset data418 (partial take-while (comp not zero?))419 ;; skip the evolution data420 rest421 (partial drop-while (comp not zero?)))422 (map #(drop % rom)423 (hxc-ptrs-evolve rom)))))))425 (defn hxc-learnsets-pretty426 "Live hxc-learnsets except it reports the name of each move --- as427 it appears in rom --- rather than the move index."428 ([] (hxc-learnsets-pretty com.aurellem.gb.gb-driver/original-rom))429 ([rom]430 (let [moves (vec(map format-name (hxc-move-names)))]431 (into {}432 (map (fn [[pkmn learnset]]433 [pkmn (map (fn [[lvl mv]] [lvl (moves mv)])434 learnset)])435 (hxc-learnsets rom))))))439 #+end_src443 * Pok\eacute{}mon II : the Pok\eacute{}dex444 ** Species vital stats445 #+name: dex-stats446 #+begin_src clojure447 (defn hxc-pokedex-stats448 "The hardcoded pokedex stats (species height weight) in memory. List449 begins at ROM@40687"450 ([] (hxc-pokedex-stats com.aurellem.gb.gb-driver/original-rom))451 ([rom]452 (let [pokedex-names (zipmap (range) (hxc-pokedex-names rom))453 pkmn-count (count pokedex-names)454 ]455 ((fn capture-stats456 [n stats data]457 (if (zero? n) stats458 (let [[species459 [_460 height-ft461 height-in462 weight-1463 weight-2464 _465 dex-ptr-1466 dex-ptr-2467 dex-bank468 _469 & data]]470 (split-with (partial not= 0x50) data)]471 (recur (dec n)472 (assoc stats473 (pokedex-names (- pkmn-count (dec n)))474 {:species475 (format-name (character-codes->str species))476 :height-ft477 height-ft478 :height-in479 height-in480 :weight481 (/ (low-high weight-1 weight-2) 10.)483 ;; :text484 ;; (character-codes->str485 ;; (take-while486 ;; (partial not= 0x50)487 ;; (drop488 ;; (+ 0xB8000489 ;; -0x4000490 ;; (low-high dex-ptr-1 dex-ptr-2))491 ;; rom)))492 })494 data)497 )))499 pkmn-count500 {}501 (drop 0x40687 rom))) ))502 #+end_src504 #+results: dex-stats505 : #'com.aurellem.gb.hxc/hxc-pokedex-stats507 ** Species synopses509 #+name: dex-text510 #+begin_src clojure511 (def hxc-pokedex-text-raw512 "The hardcoded pokedex entries in memory. List begins at513 ROM@B8000, shortly before move names."514 (hxc-thunk-words 0xB8000 14754))519 (defn hxc-pokedex-text520 "The hardcoded pokedex entries in memory, presented as an521 associative hash map. List begins at ROM@B8000."522 ([] (hxc-pokedex-text com.aurellem.gb.gb-driver/original-rom))523 ([rom]524 (zipmap525 (hxc-pokedex-names rom)526 (cons nil ;; for missingno.527 (hxc-pokedex-text-raw rom)))))528 #+end_src531 ** Pok\eacute{}mon cries532 *** See the data534 Here, you can see that each Pok\eacute{}mon's cry data consists of535 three bytes: the =cry-id=, which is the basic sound byte to use, the536 =pitch=, which determines how high or low to make the sound byte, and537 the =length=, which is the amount of the sound byte to play.539 Even though there are only a few different basic sound bytes (cry540 ids) to build from, by varying the pitch and length,541 you can create a wide variety of different Pok\eacute{}mon cries.543 #+begin_src clojure :exports both :cache no :results output544 (ns com.aurellem.gb.hxc545 (:use (com.aurellem.gb assembly characters gb-driver util mem-util546 constants))547 (:import [com.aurellem.gb.gb_driver SaveState]))549 (->>550 (rom)551 (drop 0x39462)552 (take 12)553 (println))555 (->>556 (rom)557 (drop 0x39462)558 (take 12)559 (partition 3)560 (map vec)561 (println))563 (->>564 (rom)565 (drop 0x39462)566 (take 12)567 (partition 3)568 (map569 (fn [[id pitch length]]570 {:cry-id id :pitch pitch :length length}))571 (println))573 #+end_src575 #+results:576 : (17 0 128 3 0 128 0 0 128 25 204 1)577 : ([17 0 128] [3 0 128] [0 0 128] [25 204 1])578 : ({:cry-id 17, :pitch 0, :length 128} {:cry-id 3, :pitch 0, :length 128} {:cry-id 0, :pitch 0, :length 128} {:cry-id 25, :pitch 204, :length 1})581 It is interesting to note which Pok\eacute{}mon use the same basic582 =cry-id= --- I call these /cry groups/. Here, you can see which583 Pok\eacute{}mon belong to the same cry group.585 #+begin_src clojure :exports both :cache no :results output586 (ns com.aurellem.gb.hxc587 (:use (com.aurellem.gb assembly characters gb-driver util mem-util588 constants))589 (:import [com.aurellem.gb.gb_driver SaveState]))591 (println "POKEMON CRY GROUPS")592 (doall593 (map println594 (let [cry-id595 (->>596 (rom)597 (drop 0x39462) ;; get the cry data598 (partition 3) ;; partition it into groups of three599 (map first) ;; keep only the first item, the cry-id600 (zipmap (hxc-pokenames)) ;; associate each pokemon with its cry-id601 )]603 (->> (hxc-pokenames) ;; start with the list of pokemon604 (remove (partial = :missingno.)) ;; remove missingnos605 (sort-by cry-id)606 (partition-by cry-id)607 (map vec)))))609 #+end_src611 #+results:612 #+begin_example613 POKEMON CRY GROUPS614 [:nidoran♂ :sandshrew :sandslash :nidorino]615 [:nidoran♀ :nidorina]616 [:slowpoke]617 [:kangaskhan]618 [:rhyhorn :magmar :charmander :charmeleon :charizard]619 [:grimer :snorlax]620 [:voltorb :electabuzz :electrode]621 [:gengar :muk]622 [:marowak :oddish :gloom]623 [:nidoking :moltres :articuno :raichu]624 [:nidoqueen :mankey :primeape]625 [:exeggcute :diglett :doduo :dodrio :dugtrio]626 [:lickitung :hitmonchan :seel :dewgong]627 [:exeggutor :drowzee :jynx :hypno]628 [:pidgey :poliwag :ditto :jigglypuff :wigglytuff :poliwhirl :poliwrath]629 [:ivysaur :dragonite :pikachu :dratini :dragonair :bulbasaur :venusaur]630 [:spearow :farfetch'd]631 [:rhydon]632 [:tangela :hitmonlee :golem :koffing :weezing]633 [:blastoise :kakuna :beedrill]634 [:pinsir :chansey :pidgeotto :pidgeot]635 [:arcanine :weedle]636 [:scyther :kabuto :caterpie :butterfree :goldeen :seaking]637 [:gyarados :onix :arbok :ekans :magikarp]638 [:shellder :fearow :zapdos :kabutops :cloyster]639 [:clefairy :cubone :meowth :horsea :seadra :clefable :persian]640 [:tentacool :venonat :eevee :flareon :jolteon :vaporeon :venomoth :tentacruel]641 [:lapras]642 [:gastly :kadabra :magneton :metapod :haunter :abra :alakazam :magnemite]643 [:tauros :zubat :golbat :squirtle :wartortle]644 [:mew :staryu :parasect :paras :mewtwo :starmie]645 [:slowbro :growlithe :machoke :omanyte :omastar :machop :machamp]646 [:mr.mime :krabby :kingler]647 [:psyduck :golduck :bellsprout]648 [:rattata :raticate]649 [:aerodactyl :vileplume]650 [:graveler :vulpix :ninetales :geodude]651 [:ponyta :rapidash :porygon :weepinbell :victreebel]652 #+end_example659 *** Automatically grab the data660 #+name: pokecry661 #+begin_src clojure662 (defn hxc-cry663 "The pokemon cry data in internal order. List begins at ROM@39462"664 ([](hxc-cry com.aurellem.gb.gb-driver/original-rom))665 ([rom]666 (zipmap667 (hxc-pokenames rom)668 (map669 (fn [[cry-id pitch length]]670 {:cry-id cry-id671 :pitch pitch672 :length length}673 )674 (partition 3675 (drop 0x39462 rom))))))677 (defn hxc-cry-groups678 ([] (hxc-cry-groups com.aurellem.gb.gb-driver/original-rom))679 ([rom]680 (map #(mapv first681 (filter682 (fn [[k v]]683 (= % (:cry-id v)))684 (hxc-cry)))685 ((comp686 range687 count688 set689 (partial map :cry-id)690 vals691 hxc-cry)692 rom))))695 (defn cry-conversion!696 "Convert Porygon's cry in ROM to be the cry of the given pokemon."697 [pkmn]698 (write-rom!699 (rewrite-memory700 (vec(rom))701 0x3965D702 (map second703 ((hxc-cry) pkmn)))))705 #+end_src707 ** COMMENT Names of permanent stats708 0DD4D-DD72710 * Items711 ** Item names713 *** See the data714 #+begin_src clojure :exports both :results output715 (ns com.aurellem.gb.hxc716 (:use (com.aurellem.gb assembly characters gb-driver util mem-util717 constants))718 (:import [com.aurellem.gb.gb_driver SaveState]))720 (println (take 100 (drop 0x045B7 (rom))))722 (println723 (partition-by724 (partial = 0x50)725 (take 100 (drop 0x045B7 (rom)))))727 (println728 (map character-codes->str729 (partition-by730 (partial = 0x50)731 (take 100 (drop 0x045B7 (rom))))))734 #+end_src736 #+results:737 : (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)738 : ((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))739 : (MASTER BALL # ULTRA BALL # GREAT BALL # POKé BALL # TOWN MAP # BICYCLE # ????? # SAFARI BALL # POKéDEX # MOON STONE # AN)741 *** Automatically grab the data742 #+name: item-names743 #+begin_src clojure745 (def hxc-items-raw746 "The hardcoded names of the items in memory. List begins at747 ROM@045B7"748 (hxc-thunk-words 0x45B7 870))750 (def hxc-items751 "The hardcoded names of the items in memory, presented as752 keywords. List begins at ROM@045B7. See also, hxc-items-raw."753 (comp (partial map format-name) hxc-items-raw))754 #+end_src756 ** Item prices758 ***759 #+begin_src clojure :exports both :results output760 (ns com.aurellem.gb.hxc761 (:use (com.aurellem.gb assembly characters gb-driver util mem-util762 constants))763 (:import [com.aurellem.gb.gb_driver SaveState]))765 (println (take 90 (drop 0x4495 (rom))))767 (println768 (partition 3769 (take 90 (drop 0x4495 (rom)))))771 (println772 (partition 3773 (map hex774 (take 90 (drop 0x4495 (rom))))))776 (println777 (map decode-bcd778 (map butlast779 (partition 3780 (take 90 (drop 0x4495 (rom)))))))782 (println783 (map784 vector785 (hxc-items (rom))786 (map decode-bcd787 (map butlast788 (partition 3789 (take 90 (drop 0x4495 (rom))))))))795 #+end_src797 #+results:798 : (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)799 : ((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))800 : ((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))801 : (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)802 : ([: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])805 ***806 #+name: item-prices807 #+begin_src clojure808 (defn hxc-item-prices809 "The hardcoded list of item prices in memory. List begins at ROM@4495"810 ([] (hxc-item-prices com.aurellem.gb.gb-driver/original-rom))811 ([rom]812 (let [items (hxc-items rom)813 price-size 3]814 (zipmap items815 (map (comp816 ;; zero-cost items are "priceless"817 #(if (zero? %) :priceless %)818 decode-bcd butlast)819 (partition price-size820 (take (* price-size (count items))821 (drop 0x4495 rom))))))))822 #+end_src823 ** Vendor inventories825 #+name: item-vendors826 #+begin_src clojure827 (defn hxc-shops828 ([] (hxc-shops com.aurellem.gb.gb-driver/original-rom))829 ([rom]830 (let [items (zipmap (range) (hxc-items rom))832 ;; temporarily softcode the TM items833 items (into834 items835 (map (juxt identity836 (comp keyword837 (partial str "tm-")838 (partial + 1 -200)839 ))840 (take 200 (drop 200 (range)))))842 ]844 ((fn parse-shop [coll [num-items & items-etc]]845 (let [inventory (take-while846 (partial not= 0xFF)847 items-etc)848 [separator & items-etc] (drop num-items (rest items-etc))]849 (if (= separator 0x50)850 (map (partial mapv (comp items dec)) (conj coll inventory))851 (recur (conj coll inventory) items-etc)852 )853 ))855 '()856 (drop 0x233C rom))859 )))860 #+end_src862 #+results: item-vendors863 : #'com.aurellem.gb.hxc/hxc-shops867 * Types868 ** Names of types870 *** COMMENT Pointers to type names871 #+begin_src clojure :exports both :results output872 (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)))))873 #+end_src876 ***877 #+begin_src clojure :exports both :results output878 (ns com.aurellem.gb.hxc879 (:use (com.aurellem.gb assembly characters gb-driver util mem-util880 constants))881 (:import [com.aurellem.gb.gb_driver SaveState]))883 (println (take 90 (drop 0x27D99 (rom))))885 (println886 (partition-by (partial = 0x50)887 (take 90 (drop 0x27D99 (rom)))))889 (println890 (map character-codes->str891 (partition-by (partial = 0x50)892 (take 90 (drop 0x27D99 (rom))))))894 #+end_src896 #+results:897 : (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)898 : ((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))899 : (NORMAL # FIGHTING # FLYING # POISON # FIRE # WATER # GRASS # ELECTRIC # PSYCHIC # ICE # GROUND # ROCK # BIRD # BUG # G)902 ***903 #+name: type-names904 #+begin_src clojure905 (def hxc-types906 "The hardcoded type names in memory. List begins at ROM@27D99,907 shortly before hxc-titles."908 (hxc-thunk-words 0x27D99 102))910 #+end_src912 ** Type effectiveness913 ***914 #+begin_src clojure :exports both :results output915 (ns com.aurellem.gb.hxc916 (:use (com.aurellem.gb assembly characters gb-driver util mem-util917 constants))918 (:import [com.aurellem.gb.gb_driver SaveState]))921 ;; POKEMON TYPES923 (println pkmn-types) ;; these are the pokemon types924 (println (map vector (range) pkmn-types)) ;; each type has an id number.926 (newline)931 ;;; TYPE EFFECTIVENESS933 (println (take 15 (drop 0x3E62D (rom))))934 (println (partition 3 (take 15 (drop 0x3E62D (rom)))))936 (println937 (map938 (fn [[atk-type def-type multiplier]]939 (list atk-type def-type (/ multiplier 10.)))941 (partition 3942 (take 15 (drop 0x3E62D (rom))))))945 (println946 (map947 (fn [[atk-type def-type multiplier]]948 [949 (get pkmn-types atk-type)950 (get pkmn-types def-type)951 (/ multiplier 10.)952 ])954 (partition 3955 (take 15 (drop 0x3E62D (rom))))))957 #+end_src959 #+results:960 : [: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]961 : ([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])962 :963 : (0 5 5 0 8 0 8 8 20 20 7 20 20 5 5)964 : ((0 5 5) (0 8 0) (8 8 20) (20 7 20) (20 5 5))965 : ((0 5 0.5) (0 8 0.0) (8 8 2.0) (20 7 2.0) (20 5 0.5))966 : ([:normal :rock 0.5] [:normal :ghost 0.0] [:ghost :ghost 2.0] [:fire :bug 2.0] [:fire :rock 0.5])969 ***971 #+name: type-advantage972 #+begin_src clojure973 (defn hxc-advantage974 ;; in-game multipliers are stored as 10x their effective value975 ;; to allow for fractional multipliers like 1/2977 "The hardcoded type advantages in memory, returned as tuples of978 atk-type def-type multiplier. By default (i.e. if not listed here),979 the multiplier is 1. List begins at 0x3E62D."980 ([] (hxc-advantage com.aurellem.gb.gb-driver/original-rom))981 ([rom]982 (map983 (fn [[atk def mult]] [(get pkmn-types atk (hex atk))984 (get pkmn-types def (hex def))985 (/ mult 10)])986 (partition 3987 (take-while (partial not= 0xFF)988 (drop 0x3E62D rom))))))989 #+end_src993 * Moves994 ** Names of moves995 *** See the data996 #+begin_src clojure :exports both :results output997 (ns com.aurellem.gb.hxc998 (:use (com.aurellem.gb assembly characters gb-driver util mem-util999 constants))1000 (:import [com.aurellem.gb.gb_driver SaveState]))1002 (println (take 100 (drop 0xBC000 (rom))))1004 (println1005 (partition-by1006 (partial = 0x50)1007 (take 100 (drop 0xBC000 (rom)))))1009 (println1010 (map character-codes->str1011 (partition-by1012 (partial = 0x50)1013 (take 100 (drop 0xBC000 (rom))))))1016 #+end_src1018 #+results:1019 : (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)1020 : ((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))1021 : (POUND # KARATE CHOP # DOUBLESLAP # COMET PUNCH # MEGA PUNCH # PAY DAY # FIRE PUNCH # ICE PUNCH # THUNDERPUNCH # SCRATC)1023 *** Automatically grab the data1025 #+name: move-names1026 #+begin_src clojure1027 (def hxc-move-names1028 "The hardcoded move names in memory. List begins at ROM@BC000"1029 (hxc-thunk-words 0xBC000 1551))1030 #+end_src1032 ** Properties of moves1034 #+name: move-data1035 #+begin_src clojure1036 (defn hxc-move-data1037 "The hardcoded (basic (move effects)) in memory. List begins at1038 0x38000. Returns a map of {:name :power :accuracy :pp :fx-id1039 :fx-txt}. The move descriptions are handwritten, not hardcoded."1040 ([]1041 (hxc-move-data com.aurellem.gb.gb-driver/original-rom))1042 ([rom]1043 (let [names (vec (hxc-move-names rom))1044 move-count (count names)1045 move-size 61046 types pkmn-types ;;; !! hardcoded types1047 ]1048 (zipmap (map format-name names)1049 (map1050 (fn [[idx effect power type-id accuracy pp]]1051 {:name (names (dec idx))1052 :power power1053 :accuracy accuracy1054 :pp pp1055 :type (types type-id)1056 :fx-id effect1057 :fx-txt (get move-effects effect)1058 }1059 )1061 (partition move-size1062 (take (* move-size move-count)1063 (drop 0x38000 rom))))))))1067 (defn hxc-move-data*1068 "Like hxc-move-data, but reports numbers as hexadecimal symbols instead."1069 ([]1070 (hxc-move-data* com.aurellem.gb.gb-driver/original-rom))1071 ([rom]1072 (let [names (vec (hxc-move-names rom))1073 move-count (count names)1074 move-size 61075 format-name (fn [s]1076 (keyword (.toLowerCase1077 (apply str1078 (map #(if (= % \space) "-" %) s)))))1079 ]1080 (zipmap (map format-name names)1081 (map1082 (fn [[idx effect power type accuracy pp]]1083 {:name (names (dec idx))1084 :power power1085 :accuracy (hex accuracy)1086 :pp pp1087 :fx-id (hex effect)1088 :fx-txt (get move-effects effect)1089 }1090 )1092 (partition move-size1093 (take (* move-size move-count)1094 (drop 0x38000 rom))))))))1096 #+end_src1098 ** TM and HM moves1099 ***1100 #+begin_src clojure :exports both :results output1101 (ns com.aurellem.gb.hxc1102 (:use (com.aurellem.gb assembly characters gb-driver util mem-util1103 constants))1104 (:import [com.aurellem.gb.gb_driver SaveState]))1107 (println (hxc-move-names))1108 (println (map vector (rest(range)) (hxc-move-names)))1110 (newline)1112 (println (take 55 (drop 0x1232D (rom))))1114 (println1115 (interpose "."1116 (map1117 (zipmap (rest (range)) (hxc-move-names))1118 (take 55 (drop 0x1232D (rom))))))1120 #+end_src1122 #+results:1123 : (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)1124 : ([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])1125 :1126 : (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)1127 : (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)1130 ***1131 #+name: machines1132 #+begin_src clojure1133 (defn hxc-machines1134 "The hardcoded moves taught by TMs and HMs. List begins at ROM@1232D."1135 ([] (hxc-machines1136 com.aurellem.gb.gb-driver/original-rom))1137 ([rom]1138 (let [moves (hxc-move-names rom)]1139 (zipmap1140 (range)1141 (take-while1142 (comp not nil?)1143 (map (comp1144 format-name1145 (zipmap1146 (range)1147 moves)1148 dec)1149 (take 1001150 (drop 0x1232D rom))))))))1152 #+end_src1158 ** COMMENT Status ailments1161 * NPC Trainers1163 ** Trainer Pok\eacute{}mon1164 # http://hax.iimarck.us/topic/103/1165 There are two formats for specifying lists of NPC PPok\eacute{}mon:1166 - If all the Pok\eacute{}mon will have the same level, the format is1167 - Level (used for all the Pok\eacute{}mon)1168 - Any number of Pok\eacute{}mon internal ids.1169 - 0x00, to indicate end-of-list.1170 - Otherwise, all the Pok\eacute{}mon will have their level1171 specified. The format is1172 - 0xFF, to indicate that we will be specifying the levels individually[fn::Because 0xFF is a1173 forbidden level within the usual gameplay discipline, the game1174 makers could safely use 0xFF as a mode indicator.].1175 - Any number of alternating Level/Pokemon pairs1176 - 0x00, to indicate end-of-list.1178 *** Get the pointers1179 *** See the data1180 #+begin_src clojure :exports both :results output1181 (ns com.aurellem.gb.hxc1182 (:use (com.aurellem.gb assembly characters gb-driver util mem-util1183 constants))1184 (:import [com.aurellem.gb.gb_driver SaveState]))1186 (->>1187 (rom)1188 (drop 0x39E2F)1189 (take 21)1190 (println))1193 (->>1194 (rom)1195 (drop 0x39E2F)1196 (take 21)1197 (partition-by zero?)1198 (take-nth 2)1199 (println))1203 (let1204 [pokenames1205 (zipmap1206 (rest (range))1207 (hxc-pokenames-raw))]1209 (->>1210 (rom)1211 (drop 0x39E2F)1212 (take 21) ;; (1922 in all)1213 (partition-by zero?)1214 (take-nth 2)1215 (map1216 (fn parse-team [[mode & team]]1217 (if (not= 0xFF mode)1218 (mapv1219 #(hash-map :level mode :species (pokenames %))1220 team)1222 (mapv1223 (fn [[lvl id]] (hash-map :level lvl :species (pokenames id)))1224 (partition 2 team)))))1226 (println)))1230 #+end_src1232 #+results:1233 : (11 165 108 0 14 5 0 10 165 165 107 0 14 165 108 107 0 15 165 5 0)1234 : ((11 165 108) (14 5) (10 165 165 107) (14 165 108 107) (15 165 5))1235 : ([{: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}])1237 * Places1238 ** Names of places1240 #+name: places1241 #+begin_src clojure1242 (def hxc-places1243 "The hardcoded place names in memory. List begins at1244 ROM@71500. [Cinnabar/Celadon] Mansion seems to be dynamically calculated."1245 (hxc-thunk-words 0x71500 560))1246 #+end_src1248 *** See it work1249 #+begin_src clojure :exports both :results output1250 (println (hxc-places))1251 #+end_src1253 #+results:1254 : (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)1256 ** Wild Pok\eacute{}mon demographics1257 #+name: wilds1258 #+begin_src clojure1262 (defn hxc-ptrs-wild1263 "A list of the hardcoded wild encounter data in memory. Pointers1264 begin at ROM@0CB95; data begins at ROM@0x04D89"1265 ([] (hxc-ptrs-wild com.aurellem.gb.gb-driver/original-rom))1266 ([rom]1267 (let [ptrs1268 (map (fn [[a b]] (+ a (* 0x100 b)))1269 (take-while (partial not= (list 0xFF 0xFF))1270 (partition 2 (drop 0xCB95 rom))))]1271 ptrs)))1275 (defn hxc-wilds1276 "A list of the hardcoded wild encounter data in memory. Pointers1277 begin at ROM@0CB95; data begins at ROM@0x04D89"1278 ([] (hxc-wilds com.aurellem.gb.gb-driver/original-rom))1279 ([rom]1280 (let [pokenames (zipmap (range) (hxc-pokenames rom))]1281 (map1282 (partial map (fn [[a b]] {:species (pokenames (dec b)) :level1283 a}))1284 (partition 101286 (take-while (comp (partial not= 1)1287 first)1288 (partition 21289 (drop 0xCD8C rom))1291 ))))))1293 #+end_src1298 ** Map data1300 # http://www.pokecommunity.com/showthread.php?t=2353111301 # http://datacrystal.romhacking.net/wiki/Pokemon_Red/Blue:Notes1303 #+name map1304 #+begin_src clojure :exports both :results output1305 (ns com.aurellem.gb.hxc1306 (:use (com.aurellem.gb assembly characters gb-driver util mem-util1307 constants))1308 (:import [com.aurellem.gb.gb_driver SaveState]))1311 (defn parse-header-tileset1312 [[bank# ;; memory bank for blocks & tileset1314 blocks-lo ;; structure1315 blocks-hi1317 tileset-lo ;; style1318 tileset-hi1320 collision-lo ;; collision info1321 collision-hi1323 talk-here-1 ;; positions of up to three1324 talk-here-2 ;; talk-over-countertop tiles1325 talk-here-3 ;; --- 0xFF if unused.1327 grass ;; grass tile --- 0xFF if unused1329 animation-flags ;; settings for animation1330 & _]]1332 [bank#1334 blocks-lo ;; structure1335 blocks-hi1337 tileset-lo ;; style1338 tileset-hi1340 collision-lo ;; collision info1341 collision-hi1343 talk-here-1 ;; positions of up to three1344 talk-here-2 ;; talk-over-countertop tiles1345 talk-here-3 ;; --- 0xFF if unused.1347 grass ;; grass tile --- 0xFF if unused1349 animation-flags ;; settings for animation1350 ])1354 (defn parse-header-map1355 [start]1357 (let [connection-size 111359 [tileset-index1360 map-height1361 map-width1362 layout-lo1363 layout-hi1364 text-lo1365 text-hi1366 script-lo1367 script-hi1368 adjacency-flags ;; x x x x N S W E1369 & etc]1370 (drop start (rom))1372 [east? west? south? north?]1373 (bit-list adjacency-flags)1375 [connections object-data]1376 (split-at1377 (* connection-size (+ east? west? south? north?))1378 etc)1380 connections1381 (partition connection-size connections)1386 ]1387 (ptr->offset1388 31389 (low-high layout-lo layout-hi))1392 ))1393 #+end_src1395 #+results:1396 :1400 * Appendices1401 ** Mapping the ROM1402 # D3AD: Script:Use Pokeball?1404 | ROM address (hex) | Description | Format | Example |1405 |-----------------------+-----------------+-----------------+-----------------|1406 | | <15> | <15> | <15> |1407 | 01823-0184A | Important prefix strings. | Variable-length strings, separated by 0x50. | TM#TRAINER#PC#ROCKET#POK\eacute{}#... |1408 | 0233C- | Shop inventories. | | |1409 | 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. |1410 | 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. | |1411 | 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. | |1412 | 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. |1413 | 04524-04527 | (unconfirmed) possibly the bike price in Cerulean. | | |1414 | 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#... |1415 | 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). |1416 |-----------------------+-----------------+-----------------+-----------------|1417 | 05DD2-05DF2 | Menu text for player info. | | PLAYER [newline] BADGES [nelwine] POK\Eacute{}DEX [newline] TIME [0x50] |1418 | 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. |1419 | 06698- | ? Background music. | | |1420 |-----------------------+-----------------+-----------------+-----------------|1421 | 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] |1422 | 7570-757D | Menu options for trading Pok\eacute{}mon | | TRADE [newline] CANCEL [0x50] |1423 | 757D-758A | Menu options for healing Pok\eacute{}mon | | HEAL [newline] CANCEL [0x50] |1424 | 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] |1425 | 7AF0-8000 | (empty space) | | 0 0 0 0 0 ... |1426 | 0822E-082F? | Pointers to background music, part I. | | |1427 | 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). |1428 |-----------------------+-----------------+-----------------+-----------------|1429 | 0DACB. | Amount of HP restored by Soda Pop | The HP consists of a single numerical byte. | 60 |1430 | 0DACF. | Amount of HP restored by Lemonade | " | 80 |1431 | 0DAD5. | Amount of HP restored by Fresh Water | " | 50 |1432 | 0DADB. | Amount of HP restored by Hyper Potion. | " | 200 |1433 | 0DAE0. | Amount of HP restored by Super Potion. | " | 50 |1434 | 0DAE3. | Amount of HP restored by Potion. | " | 20 |1435 |-----------------------+-----------------+-----------------+-----------------|1436 | 0DD4D-DD72 | Names of permanent stats. | Variable-length strings separated by 0x50. | #HEALTH#ATTACK#DEFENSE#SPEED#SPECIAL# |1437 |-----------------------+-----------------+-----------------+-----------------|1438 | 0DE2F. | Duration of Repel. | A single byte, representing the number of steps you can take before the effect wears off. | 100 |1439 | 0DF39. | Duration of Super Repel. | " | 200 |1440 | 0DF3E. | Duration of Max Repel. | " | 250 |1441 |-----------------------+-----------------+-----------------+-----------------|1442 | 1164B- | Terminology for the Pok\eacute{}mon menu. | Contiguous, variable-length strings. | TYPE1[newline]TYPE2[newline] *№*,[newline]OT,[newline][0x50]STATUS,[0x50]OK |1443 | 116DE- | Terminology for permanent stats in the Pok\eacute{}mon menu. | Contiguous, variable-length strings. | ATTACK[newline]DEFENSE[newline]SPEED[newline]SPECIAL[0x50] |1444 | 11852- | Terminology for current stats in the Pok\eacute{}mon menu. | Contiguous, variable-length strings. | EXP POINTS[newline]LEVEL UP[0x50] |1445 | 1195C-1196A | The two terms for being able/unable to learn a TM/HM. | Variable-length strings separated by 0x50. | ABLE#NOT ABLE# |1446 | 119C0-119CE | The two terms for being able/unable to evolve using the current stone. | Variable-length strings separated by 0x50. | ABLE#NOT ABLE# |1447 |-----------------------+-----------------+-----------------+-----------------|1448 | 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]. |1449 | 11D67. | Which badge is a prerequisite for SURF? | " | 0x67 (test for Soul Badge) |1450 | 11DAC. | Which badge is a prerequisite for STRENGTH? | " | 0x5F (test for Rainbow Badge) |1451 |-----------------------+-----------------+-----------------+-----------------|1452 | 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), ... |1453 |-----------------------+-----------------+-----------------+-----------------|1454 | 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. |1455 | 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. |1456 |-----------------------+-----------------+-----------------+-----------------|1457 | 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. |1458 | 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"). |1459 | 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#... |1460 | 27DFF-27E77 | ? | 120 bytes of unknown data. | |1461 | 27E77- | Trainer title names. | Variable-length names separated by 0x50. | YOUNGSTER#BUG CATCHER#LASS#... |1462 | 34000- | | | |1463 | 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. |1464 | 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. | | |1465 | 39462- | The Pok\eacute{}mon cry data. | Fixed-length (3 byte) descriptions of cries. | |1466 |-----------------------+-----------------+-----------------+-----------------|1467 | 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*.] | | |1468 | 39B05-39DD0. | unknown | | |1469 | 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. |1470 | 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.].) |1471 | 3B1E5-3B361 | Pointers to evolution/learnset data. | One high-low byte pair for each of the 190 Pok\eacute{}mon in internal order. | |1472 |-----------------------+-----------------+-----------------+-----------------|1473 | 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. | |1474 | 3BBAA-3C000 | (empty) | | 0 0 0 0 ... |1475 |-----------------------+-----------------+-----------------+-----------------|1476 | 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.]. |1477 | 3D6C7-3D6D6 | Two miscellaneous strings. | Variable length, separated by 0x50 | Disabled!#TYPE |1478 | 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. |1479 | 3E200-3E204 | " (???) | " | " |1480 | 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. |1481 |-----------------------+-----------------+-----------------+-----------------|1482 | 40252-4027B | Pok\eacute{}dex menu text | Variable-length strings separated by 0x50. | SEEN#OWN#CONTENTS#... |1483 | 40370-40386 | Important constants for Pok\eacute{}dex entries | | HT _ _ *?′??″* [newline] WT _ _ _ *???* lb [0x50] *POK\Eacute{}* [0x50] |1484 | 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). |1485 | 41072- | Pok\eacute{} placeholder species, "???" | | |1486 |-----------------------+-----------------+-----------------+-----------------|1487 | 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. |1488 |-----------------------+-----------------+-----------------+-----------------|1489 | 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) |1490 |-----------------------+-----------------+-----------------+-----------------|1491 | 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. |1492 | 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. |1493 |-----------------------+-----------------+-----------------+-----------------|1494 | 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 |1495 | 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]... |1496 |-----------------------+-----------------+-----------------+-----------------|1497 | 70295- | Hall of fame | The text "HALL OF FAME" | |1498 | 70442- | Play time/money | The text "PLAY TIME [0x50] MONEY" | |1499 | 71500-7174B | Names of places. | Variable-length place names (strings), separated by 0x50. | PALLET TOWN#VIRIDIAN CITY#PEWTER CITY#CERULEAN CITY#... |1500 | 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". |1501 | 7C249-7C2?? | Pointers to background music, pt II. | | |1502 |-----------------------+-----------------+-----------------+-----------------|1503 | 98000-B7190 | Dialogue and other messsages. | Variable-length strings. | |1504 | B7190-B8000 | (empty space) | | 0 0 0 0 0 ... |1505 | 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]." |1506 | BC000-BC60F | Move names. | Variable-length move names, separated by 0x50. The moves are in internal order. | POUND#KARATE CHOP#DOUBLESLAP#COMET PUNCH#... |1507 | BC610-BD000 | (empty space) | | 0 0 0 0 0 ... |1508 | 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♂#... |1509 |-----------------------+-----------------+-----------------+-----------------|1510 | E9BD5- | The text PLAY TIME (see above, 70442) | | |1511 | F1A44-F1A45 | Which Pok\eacute{}mon does Officer Jenny give you? | A level/internal-id pair. | (10 177), corresponding to a level 10 Squirtle. |1512 | 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. |1513 | | |1514 #+TBLFM:1516 ** COMMENT1517 Locations where Give Pokemon is used in a nonstraightforward way1518 0x5287C1519 0x5CE231520 0x5C36B1521 0x7562E1523 Find GivePokemon1524 (search-memory* (vec(rom)) [120 234 \_ \_ 121 234 \_ \_ 175 234 \_ \_ 6] 10)1526 F4011 : ASM script for asking if it's oak battle1529 ** Understanding memory banks and pointers1530 #+begin_src clojure1532 (defn endian-flip1533 "Flip the bytes of the two-byte number."1534 [n]1535 (assert (< n 0xFFFF))1536 (+ (* 0x100 (rem n 0x100))1537 (int (/ n 0x100))))1540 (defn offset->ptr1541 "Convert an offset into a little-endian pointer."1542 [n]1543 (->1544 n1545 (rem 0x10000) ;; take last four bytes1546 (rem 0x4000) ;; get relative offset from the start of the bank1547 (+ 0x4000)1548 endian-flip))1550 (defn offset->bank1551 "Get the bank of the offset."1552 [n]1553 (int (/ n 0x4000)))1555 (defn ptr->offset1556 "Convert a two-byte little-endian pointer into an offset."1557 [bank ptr]1558 (->1559 ptr1560 endian-flip1561 (- 0x4000)1562 (+ (* 0x4000 bank))1563 ))1565 (defn same-bank-offset1566 "Convert a ptr into an absolute offset by using the bank of the reference."1567 [reference ptr]1568 (ptr->offset1569 (offset->bank reference)1570 ptr))1571 #+end_src1574 ** Internal Pok\eacute{}mon IDs1575 ** Type IDs1577 #+name: type-ids1578 #+begin_src clojure1579 (def pkmn-types1580 [:normal ;;01581 :fighting ;;11582 :flying ;;21583 :poison ;;31584 :ground ;;41585 :rock ;;51586 :bird ;;61587 :bug ;;71588 :ghost ;;81589 :A1590 :B1591 :C1592 :D1593 :E1594 :F1595 :G1596 :H1597 :I1598 :J1599 :K1600 :fire ;;20 (0x14)1601 :water ;;21 (0x15)1602 :grass ;;22 (0x16)1603 :electric ;;23 (0x17)1604 :psychic ;;24 (0x18)1605 :ice ;;25 (0x19)1606 :dragon ;;26 (0x1A)1607 ])1608 #+end_src1610 ** Basic effects of moves1612 *** Table of basic effects1614 The possible effects of moves in Pok\eacute{}mon \mdash{} for example, dealing1615 damage, leeching health, or potentially poisoning the opponent1616 \mdash{} are stored in a table. Each move has exactly one effect, and1617 different moves might have the same effect.1619 For example, Leech Life, Mega Drain, and Absorb all have effect ID #3, which is \ldquo{}Leech half of the inflicted damage.\rdquo{}1621 All the legitimate move effects are listed in the table1622 below. Here are some notes for reading it:1624 - Whenever an effect has a chance of doing something (like a chance of1625 poisoning the opponent), I list the chance as a hexadecimal amount1626 out of 256; this is to avoid rounding errors. To convert the hex amount into a percentage, divide by 256.1627 - For some effects, the description is too cumbersome to1628 write. Instead, I just write a move name1629 in parentheses, like: (leech seed). That move gives a characteristic example1630 of the effect.1631 - I use the abbreviations =atk=, =def=, =spd=, =spc=, =acr=, =evd= for1632 attack, defense, speed, special, accuracy, and evasiveness.1633 .1637 | ID (hex) | Description | Notes |1638 |----------+-------------------------------------------------------------------------------------------------+------------------------------------------------------------------|1639 | 0 | normal damage | |1640 | 1 | no damage, just sleep | TODO: find out how many turns |1641 | 2 | 0x4C chance of poison | |1642 | 3 | leech half of inflicted damage | |1643 | 4 | 0x19 chance of burn | |1644 | 5 | 0x19 chance of freeze | |1645 | 6 | 0x19 chance of paralysis | |1646 | 7 | user faints; opponent's defense is halved during attack. | |1647 | 8 | leech half of inflicted damage ONLY if the opponent is asleep | |1648 | 9 | imitate last attack | |1649 | A | user atk +1 | |1650 | B | user def +1 | |1651 | C | user spd +1 | |1652 | D | user spc +1 | |1653 | E | user acr +1 | This effect is unused. |1654 | F | user evd +1 | |1655 | 10 | get post-battle money = 2 * level * uses | |1656 | 11 | move has 0xFE acr, regardless of battle stat modifications. | |1657 | 12 | opponent atk -1 | |1658 | 13 | opponent def -1 | |1659 | 14 | opponent spd -1 | |1660 | 15 | opponent spc -1 | |1661 | 16 | opponent acr -1 | |1662 | 17 | opponent evd -1 | |1663 | 18 | converts user's type to opponent's. | |1664 | 19 | (haze) | |1665 | 1A | (bide) | |1666 | 1B | (thrash) | |1667 | 1C | (teleport) | |1668 | 1D | (fury swipes) | |1669 | 1E | attacks 2-5 turns | Unused. TODO: find out what it does. |1670 | 1F | 0x19 chance of flinching | |1671 | 20 | opponent sleep for 1-7 turns | |1672 | 21 | 0x66 chance of poison | |1673 | 22 | 0x4D chance of burn | |1674 | 23 | 0x4D chance of freeze | |1675 | 24 | 0x4D chance of paralysis | |1676 | 25 | 0x4D chance of flinching | |1677 | 26 | one-hit KO | |1678 | 27 | charge one turn, atk next. | |1679 | 28 | fixed damage, leaves 1HP. | Is the fixed damage the power of the move? |1680 | 29 | fixed damage. | Like seismic toss, dragon rage, psywave. |1681 | 2A | atk 2-5 turns; opponent can't attack | The odds of attacking for /n/ turns are: (0 0x60 0x60 0x20 0x20) |1682 | 2B | charge one turn, atk next. (can't be hit when charging) | |1683 | 2C | atk hits twice. | |1684 | 2D | user takes 1 damage if misses. | |1685 | 2E | evade status-lowering effects | Caused by you or also your opponent? |1686 | 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. |1687 | 30 | atk causes recoil dmg = 1/4 dmg dealt | |1688 | 31 | confuses opponent | |1689 | 32 | user atk +2 | |1690 | 33 | user def +2 | |1691 | 34 | user spd +2 | |1692 | 35 | user spc +2 | |1693 | 36 | user acr +2 | This effect is unused. |1694 | 37 | user evd +2 | This effect is unused. |1695 | 38 | restores up to half of user's max hp. | |1696 | 39 | (transform) | |1697 | 3A | opponent atk -2 | |1698 | 3B | opponent def -2 | |1699 | 3C | opponent spd -2 | |1700 | 3D | opponent spc -2 | |1701 | 3E | opponent acr -2 | |1702 | 3F | opponent evd -2 | |1703 | 40 | doubles user spc when attacked | |1704 | 41 | doubles user def when attacked | |1705 | 42 | just poisons opponent | |1706 | 43 | just paralyzes opponent | |1707 | 44 | 0x19 chance opponent atk -1 | |1708 | 45 | 0x19 chance opponent def -1 | |1709 | 46 | 0x19 chance opponent spd -1 | |1710 | 47 | 0x4C chance opponent spc -1 | |1711 | 48 | 0x19 chance opponent acr -1 | |1712 | 49 | 0x19 chance opponent evd -1 | |1713 | 4A | ??? | ;; unused? no effect? |1714 | 4B | ??? | ;; unused? no effect? |1715 | 4C | 0x19 chance of confusing the opponent | |1716 | 4D | atk hits twice. 0x33 chance opponent poisioned. | |1717 | 4E | broken. crash the game after attack. | |1718 | 4F | (substitute) | |1719 | 50 | unless opponent faints, user must recharge after atk. some exceptions apply | |1720 | 51 | (rage) | |1721 | 52 | (mimic) | |1722 | 53 | (metronome) | |1723 | 54 | (leech seed) | |1724 | 55 | does nothing (splash) | |1725 | 56 | (disable) | |1726 #+end_src1728 *** Source1729 #+name: move-effects1730 #+begin_src clojure1731 (def move-effects1732 ["normal damage"1733 "no damage, just opponent sleep" ;; how many turns? is atk power ignored?1734 "0x4C chance of poison"1735 "leech half of inflicted damage"1736 "0x19 chance of burn"1737 "0x19 chance of freeze"1738 "0x19 chance of paralyze"1739 "user faints; opponent defense halved during attack."1740 "leech half of inflicted damage ONLY if sleeping opponent."1741 "imitate last attack"1742 "user atk +1"1743 "user def +1"1744 "user spd +1"1745 "user spc +1"1746 "user acr +1" ;; unused?!1747 "user evd +1"1748 "get post-battle $ = 2*level*uses"1749 "0xFE acr, no matter what."1750 "opponent atk -1" ;; acr taken from move acr?1751 "opponent def -1" ;;1752 "opponent spd -1" ;;1753 "opponent spc -1" ;;1754 "opponent acr -1";;1755 "opponent evd -1"1756 "converts user's type to opponent's."1757 "(haze)"1758 "(bide)"1759 "(thrash)"1760 "(teleport)"1761 "(fury swipes)"1762 "attacks 2-5 turns" ;; unused? like rollout?1763 "0x19 chance of flinch"1764 "opponent sleep for 1-7 turns"1765 "0x66 chance of poison"1766 "0x4D chance of burn"1767 "0x4D chance of freeze"1768 "0x4D chance of paralyze"1769 "0x4D chance of flinch"1770 "one-hit KO"1771 "charge one turn, atk next."1772 "fixed damage, leaves 1HP." ;; how is dmg determined?1773 "fixed damage." ;; cf seismic toss, dragon rage, psywave.1774 "atk 2-5 turns; opponent can't attack" ;; unnormalized? (0 0x60 0x60 0x20 0x20)1775 "charge one turn, atk next. (can't be hit when charging)"1776 "atk hits twice."1777 "user takes 1 damage if misses."1778 "evade status-lowering effects" ;;caused by you or also your opponent?1779 "(broken) if user is slower than opponent, makes critical hit impossible, otherwise has no effect"1780 "atk causes recoil dmg = 1/4 dmg dealt"1781 "confuses opponent" ;; acr taken from move acr1782 "user atk +2"1783 "user def +2"1784 "user spd +2"1785 "user spc +2"1786 "user acr +2" ;; unused!1787 "user evd +2" ;; unused!1788 "restores up to half of user's max hp." ;; broken: fails if the difference1789 ;; b/w max and current hp is one less than a multiple of 256.1790 "(transform)"1791 "opponent atk -2"1792 "opponent def -2"1793 "opponent spd -2"1794 "opponent spc -2"1795 "opponent acr -2"1796 "opponent evd -2"1797 "doubles user spc when attacked"1798 "doubles user def when attacked"1799 "just poisons opponent" ;;acr taken from move acr1800 "just paralyzes opponent" ;;1801 "0x19 chance opponent atk -1"1802 "0x19 chance opponent def -1"1803 "0x19 chance opponent spd -1"1804 "0x4C chance opponent spc -1" ;; context suggest chance is 0x191805 "0x19 chance opponent acr -1"1806 "0x19 chance opponent evd -1"1807 "???" ;; unused? no effect?1808 "???" ;; unused? no effect?1809 "0x19 chance opponent confused"1810 "atk hits twice. 0x33 chance opponent poisioned."1811 "broken. crash the game after attack."1812 "(substitute)"1813 "unless opponent faints, user must recharge after atk. some1814 exceptions apply."1815 "(rage)"1816 "(mimic)"1817 "(metronome)"1818 "(leech seed)"1819 "does nothing (splash)"1820 "(disable)"1821 ])1822 #+end_src1825 ** Alphabet code1827 * Source1829 #+begin_src clojure :tangle ../clojure/com/aurellem/gb/hxc.clj1831 (ns com.aurellem.gb.hxc1832 (:use (com.aurellem.gb assembly characters gb-driver util mem-util1833 constants species))1834 (:import [com.aurellem.gb.gb_driver SaveState]))1836 ; ************* HANDWRITTEN CONSTANTS1838 <<type-ids>>1841 ;; question: when status effects claim to take1842 ;; their accuracy from the move accuracy, does1843 ;; this mean that the move always "hits" but the1844 ;; status effect may not?1846 <<move-effects>>1848 ;; ************** HARDCODED DATA1850 <<hxc-thunks>>1851 ;; --------------------------------------------------1853 <<pokenames>>1854 <<type-names>>1856 ;; http://hax.iimarck.us/topic/581/1857 <<pokecry>>1860 <<item-names>>1864 (def hxc-titles1865 "The hardcoded names of the trainer titles in memory. List begins at1866 ROM@27E77"1867 (hxc-thunk-words 0x27E77 196))1870 <<dex-text>>1872 ;; In red/blue, pokedex stats are in internal order.1873 ;; In yellow, pokedex stats are in pokedex order.1874 <<dex-stats>>1879 <<places>>1881 (defn hxc-dialog1882 "The hardcoded dialogue in memory, including in-game alerts. Dialog1883 seems to be separated by 0x57 instead of 0x50 (END). Begins at ROM@98000."1884 ([rom]1885 (map character-codes->str1886 (take-nth 21887 (partition-by #(= % 0x57)1888 (take 0x0F7281889 (drop 0x98000 rom))))))1890 ([]1891 (hxc-dialog com.aurellem.gb.gb-driver/original-rom)))1894 <<move-names>>1895 <<move-data>>1897 <<machines>>1901 (defn internal-id1902 ([rom]1903 (zipmap1904 (hxc-pokenames rom)1905 (range)))1906 ([]1907 (internal-id com.aurellem.gb.gb-driver/original-rom)))1913 ;; nidoran gender change upon levelup1914 ;; (->1915 ;; @current-state1916 ;; rom1917 ;; vec1918 ;; (rewrite-memory1919 ;; (nth (hxc-ptrs-evolve) ((internal-id) :nidoran♂))1920 ;; [1 1 15])1921 ;; (rewrite-memory1922 ;; (nth (hxc-ptrs-evolve) ((internal-id) :nidoran♀))1923 ;; [1 1 3])1924 ;; (write-rom!)1926 ;; )1930 <<type-advantage>>1934 <<evolution-header>>1935 <<evolution>>1936 <<learnsets>>1937 <<pokebase>>1940 (defn hxc-intro-pkmn1941 "The hardcoded pokemon to display in Prof. Oak's introduction; the pokemon's1942 internal id is stored at ROM@5EDB."1943 ([] (hxc-intro-pkmn1944 com.aurellem.gb.gb-driver/original-rom))1945 ([rom]1946 (nth (hxc-pokenames rom) (nth rom 0x5EDB))))1948 (defn sxc-intro-pkmn!1949 "Set the hardcoded pokemon to display in Prof. Oak's introduction."1950 [pokemon]1951 (write-rom!1952 (rewrite-rom 0x5EDB1953 [1954 (inc1955 ((zipmap1956 (hxc-pokenames)1957 (range))1958 pokemon))])))1961 <<item-prices>>1963 <<item-vendors>>1965 <<wilds>>1968 ;; ********************** MANIPULATION FNS1971 (defn same-type1972 ([pkmn move]1973 (same-type1974 com.aurellem.gb.gb-driver/original-rom pkmn move))1975 ([rom pkmn move]1976 (((comp :types (hxc-pokemon-base rom)) pkmn)1977 ((comp :type (hxc-move-data rom)) move))))1982 (defn submap?1983 "Compares the two maps. Returns true if map-big has the same associations as map-small, otherwise false."1984 [map-small map-big]1985 (cond (empty? map-small) true1986 (and1987 (contains? map-big (ffirst map-small))1988 (= (get map-big (ffirst map-small))1989 (second (first map-small))))1990 (recur (next map-small) map-big)1992 :else false))1995 (defn search-map [proto-map maps]1996 "Returns all the maps that make the same associations as proto-map."1997 (some (partial submap? proto-map) maps))1999 (defn filter-vals2000 "Returns a map consisting of all the pairs [key val] for2001 which (pred key) returns true."2002 [pred map]2003 (reduce (partial apply assoc) {}2004 (filter (fn [[k v]] (pred v)) map)))2007 (defn search-moves2008 "Returns a subcollection of all hardcoded moves with the2009 given attributes. Attributes consist of :name :power2010 :accuracy :pp :fx-id2011 (and also :fx-txt, but it contains the same information2012 as :fx-id)"2013 ([attribute-map]2014 (search-moves2015 com.aurellem.gb.gb-driver/original-rom attribute-map))2016 ([rom attribute-map]2017 (filter-vals (partial submap? attribute-map)2018 (hxc-move-data rom))))2024 ;; note: 0x2f31 contains the names "TM" "HM"?2026 ;; note for later: credits start at F12902028 ;; note: DADB hyper-potion-hp _ _ _ super-potion-hp _ _ _ potion-hp ??2030 ;; note: DD4D spells out pokemon vital stat names ("speed", etc.)2032 ;; note: 1195C-6A says ABLE#NOT ABLE#, but so does 119C0-119CE.2033 ;; The first instance is for Machines; the second, for stones.2035 ;; note: according to2036 ;; http://www.upokecenter.com/games/rby/guides/rgbtrainers.php2037 ;; the amount of money given by a trainer is equal to the2038 ;; base money times the level of the last Pokemon on that trainer's2039 ;; list. Other sources say it's the the level of the last pokemon2040 ;; /defeated/.2042 ;; todo: find base money.2045 ;; note: 0xDFEA (in indexable mem) is the dex# of the currently-viewed Pokemon in2046 ;; in the pokedex. It's used for other purposes if there is none.2048 ;; note: 0x9D35 (index.) switches from 0xFF to 0x00 temporarily when2049 ;; you walk between areas.2051 ;; note: 0xD059 (index.) is the special battle type of your next battle:2052 ;; - 00 is a usual battle2053 ;; - 01 is a pre-scripted OLD MAN battle which always fails to catch the2054 ;; target Pokemon.2055 ;; - 02 is a safari zone battle2056 ;; - 03 obligates you to run away. (unused)2057 ;; - 04 is a pre-scripted OAK battle, which (temporarily) causes the2058 ;; enemy Pokemon to cry PIKAAA, and which always catches the target2059 ;; Pokemon. The target Pokemon is erased after the battle.2060 ;; - 05+ are glitch states in which you are sort of the Pokemon.2063 ;; note: 0x251A (in indexable mem): image decompression routine seems to begin here.2065 ;; note: 0x4845 (index): vending inventory is loaded here. possibly2066 ;; other things, too.2067 (comment2068 ;; temporarily intercept/adjust what pops out of the vending2069 ;; machine.2070 ;; (and how much it costs)2072 ;; located at 0x48452073 ;; not to be confused with shop inventory, 0xCF7B2074 (do2075 (step (read-state "vend-menu"))2076 (write-memory! (rewrite-memory (vec(memory)) 0x4845 [2 0 1 0]))2077 (step @current-state [:a])2078 (step @current-state [])2079 (nstep @current-state 200) ))2082 ;; Note: There are two tile tables, one from 8000-8FFF, the other from2083 ;; 8800-97FF. The latter contains symbols, possibly map tiles(?), with some japanese chars and stuff at the end.2084 (defn print-pixel-letters!2085 "The pixel tiles representing letters. Neat!"2086 ([] (print-pixel-letters! (read-state "oak-speaks")))2087 ([state]2088 (map2089 (comp2090 println2091 (partial map #(if (zero? %) \space 0))2092 #(if (< (count %) 8)2093 (recur (cons 0 %))2094 %)2095 reverse bit-list)2097 (take 0xFFF (drop 0x8800 (memory state))))))2100 ;; (defn test-2 []2101 ;; (loop [n 02102 ;; pc-1 (pc-trail (-> state-defend (tick) (step [:a]) (step [:a]) (step []) (nstep 100)) 100000)2103 ;; pc-2 (pc-trail (-> state-speed (tick) (step [:a]) (step [:a])2104 ;; (step []) (nstep 100)) 100000)]2105 ;; (cond (empty? (drop n pc-1)) [pc-1 n]2106 ;; (not= (take 10 (drop n pc-1)) (take 10 pc-2))2107 ;; (recur pc-1 pc-2 (inc n))2108 ;; :else2109 ;; [(take 1000 pc-2) n])))2114 (defn test-32115 "Explore trainer data"2116 ([] (test-3 0x3A289))2117 ([start]2118 (let [pokenames (vec(hxc-pokenames-raw))]2119 (println2120 (reduce2121 str2122 (map2123 (fn [[adr lvl pkmn]]2124 (str (format "%-11s %4d %02X %02X \t %05X\n"2126 (cond2127 (zero? lvl) "+"2128 (nil? (get pokenames (dec pkmn)))2129 "-"2130 :else2131 (get pokenames (dec pkmn)))2132 lvl2133 pkmn2134 lvl2135 adr2136 )))2137 (map cons2138 (take-nth 2 (drop start (range)))2139 (partition 22140 (take 400;;7032141 (drop2142 start2143 ;; 0x3A75D2144 (rom)))))))))))2146 (defn search-memory* [mem codes k]2147 (loop [index 02148 index-next 12149 start-match 02150 to-match codes2151 matches []]2152 (cond2153 (>= index (count mem)) matches2155 (empty? to-match)2156 (recur2157 index-next2158 (inc index-next)2159 index-next2160 codes2161 (conj matches2162 [(hex start-match) (take k (drop start-match mem))])2163 )2165 (or (= (first to-match) \_) ;; wildcard2166 (= (first to-match) (nth mem index)))2167 (recur2168 (inc index)2169 index-next2170 start-match2171 (rest to-match)2172 matches)2174 :else2175 (recur2176 index-next2177 (inc index-next)2178 index-next2179 codes2180 matches))))2183 (def script-use-ball2184 [0xFA ;; ld A, nn2185 \_2186 \_2187 0xA7 ;; and A2188 0xCA ;; JP Z2189 \_2190 \_2191 0x3D ;; dec A2192 0xC2 ;; JP NZ2193 \_2194 \_2195 0xFA ;; LD A2196 \_2197 \_2198 ])2202 (defn search-pattern [ptn coll]2203 (loop2204 [index 02205 to-match ptn2206 binds {}2208 next-index 12209 match-start 02210 matches []]2212 (cond2213 (>= index (count coll)) matches2214 (empty? to-match)2215 (recur2216 next-index2217 ptn2218 {}2219 (inc next-index)2220 next-index2221 (conj match-start2222 [(hex match-start) binds]))2224 :else2225 (let [k (first to-match)2226 v (nth coll index)]2227 (cond2228 (= k \_) ;; wildcard2229 (recur2230 (inc index)2231 (rest to-match)2232 binds2234 next-index2235 match-start2236 matches)2238 (keyword? k)2239 (if (binds k)2240 (if (= (binds k) v)2241 (recur2242 (inc index)2243 (rest to-match)2244 binds2245 next-index2246 match-start2247 matches)2249 (recur2250 next-index2251 ptn2252 {}2253 (inc next-index)2254 next-index2255 matches))2257 ;; ;; consistent bindings2258 ;; (recur2259 ;; (inc index)2260 ;; (rest to-match)2261 ;; binds2263 ;; next-index2264 ;; match-start2265 ;; matches)2267 ;; ;; inconsistent bindings2268 ;; (recur2269 ;; next-index2270 ;; ptn2271 ;; {}2272 ;; (inc next-index)2273 ;; next-index2274 ;; matches))2276 (if ((set (vals binds)) v)2277 ;; bindings are not unique2278 (recur2279 next-index2280 ptn2281 {}2282 (inc next-index)2283 next-index2284 matches)2286 ;; bindings are unique2287 (recur2288 (inc index)2289 (rest to-match)2290 (assoc binds k v)2292 next-index2293 match-start2294 matches)))2296 :else ;; k is just a number2297 (if (= k v)2298 (recur2299 (inc index)2300 (rest to-match)2301 binds2303 next-index2304 match-start2305 matches)2307 (recur2308 next-index2309 ptn2310 {}2311 (inc next-index)2312 next-index2313 matches)))))))2323 (defn search-pattern* [ptn coll]2324 (loop2325 [2326 binds {}2327 index 02328 index-next 12329 start-match 02330 to-match ptn2331 matches []]2333 (cond2334 (>= index (count coll)) matches2335 (empty? to-match)2336 (recur2337 {}2338 index-next2339 (inc index-next)2340 index-next2341 ptn2342 (conj matches2343 [(hex start-match) binds]))2345 :else2346 (let [k (first to-match)2347 v (nth coll index)]2348 (cond2349 (= k \_) ;; wildcard2350 (recur2351 binds2352 (inc index)2353 index-next2354 start-match2355 (rest to-match)2356 matches)2358 (keyword? k)2359 (if (binds k)2360 (if (= (binds k) v)2361 (recur2362 binds2363 (inc index)2364 index-next2365 start-match2366 (rest to-match)2367 matches)2368 (recur2369 {}2370 index-next2371 (inc index-next)2372 index-next2373 ptn2374 matches))2375 (if2376 ;; every symbol must be bound to a different thing.2377 ((set (vals binds)) v)2378 (recur2379 {}2380 index-next2381 (inc index-next)2382 index-next2383 ptn2384 matches)2385 (recur2386 (assoc binds k v)2387 (inc index)2388 index-next2389 start-match2390 (rest to-match)2391 matches)))2393 :else2394 (if (= k v)2395 (recur2396 binds2397 (inc index)2398 index-next2399 start-match2400 (rest to-match)2401 matches)2402 (recur2403 {}2404 index-next2405 (inc index-next)2406 index-next2407 ptn2408 matches))2412 )))))2417 ;; look for the rainbow badge in memory2418 (println (reduce str (map #(str (first %) "\t" (vec(second %)) "\n") (search-memory (rom) [221] 10))))2421 (comment2423 (def hxc-later2424 "Running this code produces, e.g. hardcoded names NPCs give2425 their pokemon. Will sort through it later."2426 (print (character-codes->str(take 100002427 (drop 0x715972428 (rom (root)))))))2430 (let [dex2431 (partition-by #(= 0x50 %)2432 (take 25402433 (drop 0x406872434 (rom (root)))))]2435 (def dex dex)2436 (def hxc-species2437 (map character-codes->str2438 (take-nth 4 dex))))2439 )2442 #+end_src2444 #+results:2445 : nil