changeset 350:5e816fa56393

merge dylan's changes.
author Robert McIntyre <rlm@mit.edu>
date Sun, 08 Apr 2012 20:59:06 -0500
parents 3b3cd62b6106 (current diff) 37616a97beaa (diff)
children a6a123af22f6
files
diffstat 3 files changed, 1403 insertions(+), 124 deletions(-) [+]
line wrap: on
line diff
     1.1 --- a/clojure/com/aurellem/gb/hxc.clj	Sun Apr 08 20:58:14 2012 -0500
     1.2 +++ b/clojure/com/aurellem/gb/hxc.clj	Sun Apr 08 20:59:06 2012 -0500
     1.3 @@ -1,15 +1,11 @@
     1.4 +
     1.5  (ns com.aurellem.gb.hxc
     1.6    (:use (com.aurellem.gb assembly characters gb-driver util mem-util
     1.7                           constants species))
     1.8    (:import [com.aurellem.gb.gb_driver SaveState]))
     1.9  
    1.10 -
    1.11 -
    1.12 -
    1.13  ; ************* HANDWRITTEN CONSTANTS
    1.14  
    1.15 -
    1.16 -
    1.17  (def pkmn-types
    1.18    [:normal    ;;0
    1.19     :fighting  ;;1
    1.20 @@ -138,7 +134,6 @@
    1.21     "(disable)"
    1.22     ])
    1.23  
    1.24 -
    1.25  ;; ************** HARDCODED DATA
    1.26  
    1.27  (defn hxc-thunk
    1.28 @@ -162,11 +157,9 @@
    1.29     (partial comp (partial map character-codes->str))
    1.30     hxc-thunk))
    1.31  
    1.32 -
    1.33  ;; --------------------------------------------------
    1.34  
    1.35  
    1.36 -
    1.37  (defn hxc-pokenames-raw
    1.38    "The hardcoded names of the 190 species in memory. List begins at
    1.39  ROM@E8000. Although names in memory are padded with 0x50 to be 10 characters
    1.40 @@ -205,6 +198,10 @@
    1.41           
    1.42           names)))))
    1.43  
    1.44 +(def hxc-types
    1.45 +  "The hardcoded type names in memory. List begins at ROM@27D99,
    1.46 +  shortly before hxc-titles."
    1.47 +  (hxc-thunk-words 0x27D99 102))
    1.48  
    1.49  
    1.50  ;; http://hax.iimarck.us/topic/581/
    1.51 @@ -251,15 +248,20 @@
    1.52      (map second
    1.53           ((hxc-cry) pkmn)))))
    1.54  
    1.55 +
    1.56 +
    1.57 +
    1.58  (def hxc-items-raw
    1.59    "The hardcoded names of the items in memory. List begins at
    1.60  ROM@045B7"
    1.61    (hxc-thunk-words 0x45B7 870))
    1.62  
    1.63 -(def hxc-types
    1.64 -  "The hardcoded type names in memory. List begins at ROM@27D99,
    1.65 -  shortly before hxc-titles."
    1.66 -  (hxc-thunk-words 0x27D99 102))
    1.67 +(def hxc-items
    1.68 +  "The hardcoded names of the items in memory, presented as
    1.69 +  keywords. List begins at ROM@045B7. See also, hxc-items-raw."
    1.70 +  (comp (partial map format-name) hxc-items-raw))
    1.71 +
    1.72 +
    1.73  
    1.74  (def hxc-titles
    1.75    "The hardcoded names of the trainer titles in memory. List begins at
    1.76 @@ -274,10 +276,6 @@
    1.77  
    1.78  
    1.79  
    1.80 -(def hxc-items
    1.81 -  "The hardcoded names of the items in memory, presented as
    1.82 -  keywords. List begins at ROM@045B7. See also, hxc-items-raw."
    1.83 -  (comp (partial map format-name) hxc-items-raw))
    1.84  
    1.85  (defn hxc-pokedex-text
    1.86    "The hardcoded pokedex entries in memory, presented as an
    1.87 @@ -291,7 +289,6 @@
    1.88  
    1.89  ;; In red/blue, pokedex stats are in internal order.
    1.90  ;; In yellow, pokedex stats are in pokedex order.
    1.91 -
    1.92  (defn hxc-pokedex-stats
    1.93    "The hardcoded pokedex stats (species height weight) in memory. List
    1.94  begins at ROM@40687"
    1.95 @@ -351,9 +348,6 @@
    1.96  
    1.97  
    1.98  
    1.99 -
   1.100 -
   1.101 -
   1.102  (def hxc-places
   1.103    "The hardcoded place names in memory. List begins at
   1.104  ROM@71500. [Cinnabar] Mansion seems to be dynamically calculated."
   1.105 @@ -376,8 +370,6 @@
   1.106  (def hxc-move-names
   1.107    "The hardcoded move names in memory. List begins at ROM@BC000"
   1.108    (hxc-thunk-words 0xBC000 1551))
   1.109 -
   1.110 -
   1.111  (defn hxc-move-data
   1.112    "The hardcoded (basic (move effects)) in memory. List begins at
   1.113  0x38000. Returns a map of {:name :power :accuracy :pp :fx-id
   1.114 @@ -460,6 +452,7 @@
   1.115  
   1.116  
   1.117  
   1.118 +
   1.119  (defn internal-id
   1.120    ([rom]
   1.121       (zipmap 
   1.122 @@ -490,26 +483,19 @@
   1.123  
   1.124  
   1.125  
   1.126 -(defn hxc-advantage
   1.127 -  ;; in-game multipliers are stored as 10x their effective value
   1.128 -  ;; to allow for fractional multipliers like 1/2
   1.129 -  
   1.130 -  "The hardcoded type advantages in memory, returned as tuples of
   1.131 -  atk-type def-type multiplier. By default (i.e. if not listed here),
   1.132 -the multiplier is 1. List begins at 0x3E62D."
   1.133 -  ([] (hxc-advantage com.aurellem.gb.gb-driver/original-rom))
   1.134 -  ([rom]
   1.135 -     (map
   1.136 -      (fn [[atk def mult]] [(get pkmn-types atk (hex atk))
   1.137 -                            (get pkmn-types def (hex def))
   1.138 -                            (/ mult 10)])
   1.139 -      (partition 3
   1.140 -                 (take-while (partial not= 0xFF)
   1.141 -                             (drop 0x3E62D rom))))))
   1.142  
   1.143  
   1.144  
   1.145  (defn format-evo
   1.146 +  "Parse a sequence of evolution data, returning a map. First is the
   1.147 +method: 0 = end-evolution-data. 1 = level-up, 2 = item, 3 = trade. Next is an item id, if the
   1.148 +  method of evolution is by item (only stones will actually make pokemon
   1.149 +  evolve, for some auxillary reason.) Finally, the minimum level for
   1.150 +  evolution to occur (level 1 means no limit, which is used for trade
   1.151 +  and item evolutions), followed by the internal id of the pokemon
   1.152 +  into which to evolve. Hence, level up and trade evolutions are
   1.153 +  described with 3
   1.154 +  bytes; item evolutions with four."
   1.155    [coll]
   1.156    (let [method (first coll)]
   1.157      (cond (empty? coll) []
   1.158 @@ -555,7 +541,7 @@
   1.159  (defn hxc-learnsets
   1.160    "Hardcoded map associating pokemon names to lists of pairs [lvl
   1.161    move] of abilities they learn as they level up. The data
   1.162 -exists at ROM@3400, sorted by internal order. Pointers to the data
   1.163 +exists at ROM@34000, sorted by internal order. Pointers to the data
   1.164    exist at ROM@3B1E5; see also, hxc-ptrs-evolve"
   1.165    ([] (hxc-learnsets com.aurellem.gb.gb-driver/original-rom))
   1.166    ([rom]
   1.167 @@ -589,63 +575,6 @@
   1.168               
   1.169                                    
   1.170  
   1.171 -
   1.172 -(defn hxc-evolution
   1.173 -  "Hardcoded evolution data in memory. The data exists at ROM@34000,
   1.174 -  sorted by internal order. Pointers to the data exist at ROM@3B1E5; see also, hxc-ptrs-evolve."
   1.175 -  ([] (hxc-evolution com.aurellem.gb.gb-driver/original-rom))
   1.176 -  ([rom]
   1.177 -       (apply assoc {}
   1.178 -               (interleave
   1.179 -                (hxc-pokenames rom)
   1.180 -                (map
   1.181 -                 (comp
   1.182 -                  format-evo
   1.183 -                  (partial take-while (comp not zero?))
   1.184 -                  #(drop % rom))
   1.185 -                 (hxc-ptrs-evolve rom)
   1.186 -                 )))))
   1.187 -
   1.188 -(defn hxc-evolution-pretty
   1.189 -  "Like hxc-evolution, except it uses the names of items and pokemon
   1.190 ---- grabbed from ROM --- rather than their numerical identifiers."
   1.191 -  ([] (hxc-evolution-pretty com.aurellem.gb.gb-driver/original-rom))
   1.192 -  ([rom]
   1.193 -     (let
   1.194 -         [poke-names (vec (hxc-pokenames rom))
   1.195 -          item-names (vec (hxc-items rom))
   1.196 -          use-names
   1.197 -          (fn [m]
   1.198 -            (loop [ks (keys m) new-map m]
   1.199 -              (let [k (first ks)]
   1.200 -                (cond (nil? ks) new-map
   1.201 -                      (= k :into)
   1.202 -                      (recur
   1.203 -                       (next ks)
   1.204 -                       (assoc new-map
   1.205 -                         :into
   1.206 -                         (poke-names
   1.207 -                          (:into
   1.208 -                           new-map))))
   1.209 -                      (= k :item)
   1.210 -                      (recur
   1.211 -                       (next ks)
   1.212 -                       (assoc new-map
   1.213 -                         :item
   1.214 -                         (item-names
   1.215 -                          (:item new-map))))
   1.216 -                      :else
   1.217 -                      (recur
   1.218 -                       (next ks)
   1.219 -                       new-map)
   1.220 -                      ))))]
   1.221 -
   1.222 -       (into {}
   1.223 -             (map (fn [[pkmn evo-coll]]
   1.224 -                    [pkmn (map use-names evo-coll)])
   1.225 -                  (hxc-evolution rom))))))
   1.226 -
   1.227 -
   1.228  (defn hxc-pokemon-base
   1.229    ([] (hxc-pokemon-base com.aurellem.gb.gb-driver/original-rom))
   1.230    ([rom]
   1.231 @@ -745,6 +674,7 @@
   1.232                      (take (* entry-size pkmn-count)
   1.233                            (drop 0x383DE
   1.234                                  rom))))))))
   1.235 +
   1.236    
   1.237    
   1.238  (defn hxc-intro-pkmn
   1.239 @@ -820,7 +750,6 @@
   1.240  
   1.241  
   1.242  
   1.243 -
   1.244  (defn hxc-ptrs-wild
   1.245    "A list of the hardcoded wild encounter data in memory. Pointers
   1.246    begin at ROM@0CB95; data begins at ROM@0x04D89" 
   1.247 @@ -854,17 +783,6 @@
   1.248  
   1.249  
   1.250  
   1.251 -
   1.252 -
   1.253 -
   1.254 -
   1.255 -
   1.256 -
   1.257 -
   1.258 -
   1.259 -
   1.260 -
   1.261 -
   1.262  ;; ********************** MANIPULATION FNS
   1.263  
   1.264  
   1.265 @@ -925,6 +843,14 @@
   1.266  
   1.267  ;; note for later: credits start at F1290
   1.268  
   1.269 +;; note: DADB hyper-potion-hp _ _ _ super-potion-hp _ _ _ potion-hp ??
   1.270 +
   1.271 +;; note: DD4D spells out pokemon vital stat names ("speed", etc.)
   1.272 +
   1.273 +;; note: 1195C-6A says ABLE#NOT ABLE#, but so does 119C0-119CE.
   1.274 +;; The first instance is for Machines; the second, for stones.
   1.275 +
   1.276 +;; 0x251A (in indexable mem): image decompression routine seems to begin here.
   1.277  
   1.278  
   1.279  (comment 
   1.280 @@ -946,9 +872,3 @@
   1.281      (map character-codes->str
   1.282           (take-nth 4 dex))))
   1.283  )
   1.284 -
   1.285 -
   1.286 -
   1.287 -
   1.288 -
   1.289 -
     2.1 --- a/clojure/com/aurellem/gb/mem_util.clj	Sun Apr 08 20:58:14 2012 -0500
     2.2 +++ b/clojure/com/aurellem/gb/mem_util.clj	Sun Apr 08 20:59:06 2012 -0500
     2.3 @@ -190,3 +190,43 @@
     2.4  
     2.5  (defn restore-rom! [] (write-rom! original-rom))
     2.6  
     2.7 +
     2.8 +(defn endian-flip
     2.9 +  "Flip the bytes of the two-byte number."
    2.10 +  [n]
    2.11 +  (assert (< n 0xFFFF))
    2.12 +  (+ (* 0x100 (rem n 0x100))
    2.13 +        (int (/ n 0x100))))
    2.14 +
    2.15 +
    2.16 +(defn offset->ptr
    2.17 +  "Convert an offset into a little-endian pointer."
    2.18 +  [n]
    2.19 +  (->
    2.20 +   n
    2.21 +   (rem 0x10000) ;; take last four bytes
    2.22 +   (rem 0x4000)   ;; get relative offset from the start of the bank
    2.23 +   (+ 0x4000)
    2.24 +   endian-flip))
    2.25 +
    2.26 +(defn offset->bank
    2.27 +  "Get the bank of the offset."
    2.28 +  [n]
    2.29 +  (int (/ n 0x4000)))
    2.30 +
    2.31 +(defn ptr->offset
    2.32 +  "Convert a two-byte little-endian pointer into an offset."
    2.33 +  [bank ptr]
    2.34 +  (->
    2.35 +   ptr
    2.36 +   endian-flip
    2.37 +   (- 0x4000)
    2.38 +   (+ (* 0x4000 bank))
    2.39 +   ))
    2.40 +
    2.41 +(defn same-bank-offset
    2.42 +  "Convert a ptr into an absolute offset by using the bank of the reference."
    2.43 +  [reference ptr]
    2.44 +  (ptr->offset
    2.45 +   (offset->bank reference)
    2.46 +   ptr))
    2.47 \ No newline at end of file
     3.1 --- a/org/rom.org	Sun Apr 08 20:58:14 2012 -0500
     3.2 +++ b/org/rom.org	Sun Apr 08 20:59:06 2012 -0500
     3.3 @@ -7,9 +7,832 @@
     3.4  #+INCLUDE: ../../aurellem/org/level-0.org
     3.5  #+BABEL: :exports both :noweb yes :cache no :mkdirp yes
     3.6  
     3.7 +# about map headers http://datacrystal.romhacking.net/wiki/Pokemon_Red/Blue:Notes
     3.8 +# map headers Yellow http://www.pokecommunity.com/archive/index.php/t-235311.html
     3.9 +# pokedollar: U+20B1
    3.10 +* Introduction
    3.11  
    3.12 -# pokedollar: U+20B1
    3.13 -* Mapping the ROM
    3.14 +
    3.15 +** COMMENT Getting linguistic data: names, words, etc.
    3.16 +
    3.17 +Some of the simplest data 
    3.18 +
    3.19 +
    3.20 +One of the simplest data structures in the Pok\eacute{} ROM is an
    3.21 +unbroken list of strings that either (a) all have a specific length,
    3.22 +or (b) are all separated by the same character.
    3.23 +
    3.24 +Because lots of good data has this format, we'll start by writing a
    3.25 +template function to extract it:
    3.26 +
    3.27 +#+name: hxc-thunks
    3.28 +#+begin_src clojure :results silent
    3.29 +(defn hxc-thunk
    3.30 +  "Creates a thunk (nullary fn) that grabs data in a certain region of rom and
    3.31 +splits it into a collection by 0x50. If rom is not supplied, uses the
    3.32 +  original rom data."
    3.33 +  [start length]
    3.34 +  (fn self
    3.35 +    ([rom]
    3.36 +       (take-nth 2
    3.37 +                 (partition-by #(= % 0x50)
    3.38 +                               (take length
    3.39 +                                     (drop start rom)))))
    3.40 +    ([]
    3.41 +      (self com.aurellem.gb.gb-driver/original-rom))))
    3.42 +
    3.43 +(def hxc-thunk-words
    3.44 +  "Same as hxc-thunk, except it interprets the rom data as characters,
    3.45 +  returning a collection of strings."
    3.46 +  (comp
    3.47 +   (partial comp (partial map character-codes->str))
    3.48 +   hxc-thunk))
    3.49 +
    3.50 +#+end_src
    3.51 +
    3.52 +
    3.53 +* Pok\eacute{}mon I
    3.54 +** Names of each species
    3.55 +The names of the Pok\eacute{}mon species are stored in
    3.56 +ROM@E8000. This name list is interesting, for a number of reasons:
    3.57 +- The names are stored in [[ ][internal order]] rather than in the familiar
    3.58 +  Pok\eacute{}dex order. This seemingly random order probably represents the order in which the authors created or
    3.59 +  programmed in the Pok\eacute{}mon; it's used throughout the game.
    3.60 +- There is enough space allocated for 190 Pok\eacute{}mon. As I
    3.61 +  understand it, there were originally going to be 190 Pok\eacute{}mon
    3.62 +  in Generation I, but the creators decided to defer some to
    3.63 +  Generation II. This explains why many Gen I and Gen II Pok\eacute{}mon
    3.64 +  have the same aesthetic feel.
    3.65 +- The list is pockmarked with random gaps, due to the strange internal
    3.66 +  ordering
    3.67 +  and the 39 unused spaces [fn::190 allocated spaces minus 151 true Pok\eacute{}mon]. These missing spaces are filled with the
    3.68 +  placeholder name  =MISSINGNO.= (\ldquo{}Missing number\rdquo{}).
    3.69 +
    3.70 +Each name is exactly ten letters long; whenever a name would be too short, the extra
    3.71 +space is padded with the character 0x50. 
    3.72 +
    3.73 +*** See the data
    3.74 +
    3.75 +Here you can see the raw data in three stages: in the first stage, we
    3.76 +just grab the first few bytes starting from position 0xE8000. In the
    3.77 +second stage, we partition it into ten-letter chunks to show you
    3.78 +where the names begin and end. In the final stage, we convert each
    3.79 +byte into the letter it represents using the =character-codes->str=
    3.80 +function. (0x50 is rendered as the symbol \ldquo{} =#= \rdquo{} for
    3.81 +ease of reading).
    3.82 +
    3.83 +#+begin_src clojure :exports both :cache no :results output 
    3.84 +(ns com.aurellem.gb.hxc
    3.85 +  (:use (com.aurellem.gb assembly characters gb-driver util mem-util
    3.86 +                         constants))
    3.87 +  (:import [com.aurellem.gb.gb_driver SaveState]))
    3.88 +
    3.89 +(println (take 100 (drop 0xE8000 (rom))))
    3.90 +
    3.91 +(println (partition 10 (take 100 (drop 0xE8000 (rom)))))
    3.92 +
    3.93 +(println (character-codes->str (take 100 (drop 0xE8000 (rom)))))
    3.94 +
    3.95 +
    3.96 +#+end_src
    3.97 +
    3.98 +#+results:
    3.99 +: (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)
   3.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))
   3.101 +: RHYDON####KANGASKHANNIDORAN♂##CLEFAIRY##SPEAROW###VOLTORB###NIDOKING##SLOWBRO###IVYSAUR###EXEGGUTOR#
   3.102 +
   3.103 +
   3.104 +*** Automatically grab the data.
   3.105 +
   3.106 +    #+name: pokenames
   3.107 +    #+begin_src clojure
   3.108 +
   3.109 +(defn hxc-pokenames-raw
   3.110 +  "The hardcoded names of the 190 species in memory. List begins at
   3.111 +ROM@E8000. Although names in memory are padded with 0x50 to be 10 characters
   3.112 +  long, these names are stripped of padding. See also, hxc-pokedex-names"
   3.113 +  ([]
   3.114 +     (hxc-pokenames-raw com.aurellem.gb.gb-driver/original-rom))
   3.115 +  ([rom]
   3.116 +     (let [count-species 190
   3.117 +           name-length 10]
   3.118 +       (map character-codes->str
   3.119 +            (partition name-length
   3.120 +                       (map #(if (= 0x50 %) 0x00 %)
   3.121 +                            (take (* count-species name-length)
   3.122 +                                  (drop 0xE8000
   3.123 +                                        rom))))))))
   3.124 +(def hxc-pokenames
   3.125 +  (comp
   3.126 +   (partial map format-name)
   3.127 +   hxc-pokenames-raw))
   3.128 +
   3.129 +
   3.130 +
   3.131 +
   3.132 +(defn hxc-pokedex-names
   3.133 +  "The names of the pokemon in hardcoded pokedex order. List begins at
   3.134 +ROM@410B1. See also, hxc-pokenames."
   3.135 +  ([] (hxc-pokedex-names
   3.136 +       com.aurellem.gb.gb-driver/original-rom))
   3.137 +  ([rom]
   3.138 +     (let [names (hxc-pokenames rom)]
   3.139 +       (#(mapv %
   3.140 +               ((comp range count keys) %))
   3.141 +        (zipmap
   3.142 +         (take (count names)
   3.143 +               (drop 0x410b1 rom))
   3.144 +         
   3.145 +         names)))))
   3.146 +
   3.147 +    #+end_src
   3.148 +
   3.149 +
   3.150 +
   3.151 +** Generic species information
   3.152 +
   3.153 +#+name: pokebase
   3.154 +#+begin_src clojure
   3.155 +(defn hxc-pokemon-base
   3.156 +  ([] (hxc-pokemon-base com.aurellem.gb.gb-driver/original-rom))
   3.157 +  ([rom]
   3.158 +     (let [entry-size 28
   3.159 +           pkmn-count (count (hxc-pokedex-text rom))
   3.160 +           pokemon (rest (hxc-pokedex-names))
   3.161 +           types (apply assoc {}
   3.162 +                        (interleave
   3.163 +                         (range)
   3.164 +                         pkmn-types)) ;;!! softcoded
   3.165 +           moves (apply assoc {}
   3.166 +                        (interleave
   3.167 +                         (range)
   3.168 +                         (map format-name
   3.169 +                              (hxc-move-names rom))))
   3.170 +           machines (hxc-machines)
   3.171 +           ]
   3.172 +       (zipmap
   3.173 +        pokemon
   3.174 +        (map
   3.175 +         (fn [[n
   3.176 +               rating-hp
   3.177 +               rating-atk
   3.178 +               rating-def
   3.179 +               rating-speed
   3.180 +               rating-special
   3.181 +               type-1
   3.182 +               type-2
   3.183 +               rarity
   3.184 +               rating-xp
   3.185 +               pic-dimensions ;; tile_width|tile_height (8px/tile)
   3.186 +               ptr-pic-obverse-1
   3.187 +               ptr-pic-obverse-2
   3.188 +               ptr-pic-reverse-1
   3.189 +               ptr-pic-reverse-2
   3.190 +               move-1
   3.191 +               move-2
   3.192 +               move-3
   3.193 +               move-4
   3.194 +               growth-rate
   3.195 +               &
   3.196 +               TMs|HMs]]
   3.197 +           (let
   3.198 +               [base-moves
   3.199 +                (mapv moves
   3.200 +                      ((comp
   3.201 +                        ;; since the game uses zero as a delimiter,
   3.202 +                        ;; it must also increment all move indices by 1.
   3.203 +                        ;; heren we decrement to correct this.
   3.204 +                        (partial map dec)
   3.205 +                        (partial take-while (comp not zero?)))
   3.206 +                       [move-1 move-2 move-3 move-4]))
   3.207 +                
   3.208 +                types
   3.209 +                (set (list (types type-1)
   3.210 +                           (types type-2)))
   3.211 +                TMs|HMs
   3.212 +                (map
   3.213 +                 (comp
   3.214 +                  (partial map first)
   3.215 +                  (partial remove (comp zero? second)))
   3.216 +                 (split-at
   3.217 +                  50
   3.218 +                  (map vector
   3.219 +                       (rest(range))
   3.220 +                       (reduce concat
   3.221 +                               (map
   3.222 +                                #(take 8
   3.223 +                                       (concat (bit-list %)
   3.224 +                                               (repeat 0)))
   3.225 +                                
   3.226 +                                TMs|HMs)))))
   3.227 +                
   3.228 +                TMs  (vec (first TMs|HMs))
   3.229 +                HMs (take 5 (map (partial + -50) (vec (second TMs|HMs))))
   3.230 +                
   3.231 +                
   3.232 +                ]
   3.233 +             
   3.234 +             
   3.235 +             {:dex# n
   3.236 +              :base-moves base-moves
   3.237 +              :types types
   3.238 +              :TMs TMs
   3.239 +              :HMs HMs
   3.240 +              :base-hp rating-hp
   3.241 +              :base-atk rating-atk
   3.242 +              :base-def rating-def
   3.243 +              :base-speed rating-speed
   3.244 +              :base-special rating-special
   3.245 +              :o0 pic-dimensions
   3.246 +              :o1 ptr-pic-obverse-1
   3.247 +              :o2 ptr-pic-obverse-2
   3.248 +              }))
   3.249 +         
   3.250 +         (partition entry-size
   3.251 +                    (take (* entry-size pkmn-count)
   3.252 +                          (drop 0x383DE
   3.253 +                                rom))))))))
   3.254 +
   3.255 +#+end_src
   3.256 +
   3.257 +
   3.258 +** Pok\eacute{}mon evolutions
   3.259 +#+name: evolution-header
   3.260 +#+begin_src clojure
   3.261 +(defn format-evo
   3.262 +  "Parse a sequence of evolution data, returning a map. First is the
   3.263 +method: 0 = end-evolution-data. 1 = level-up, 2 = item, 3 = trade. Next is an item id, if the
   3.264 +  method of evolution is by item (only stones will actually make pokemon
   3.265 +  evolve, for some auxillary reason.) Finally, the minimum level for
   3.266 +  evolution to occur (level 1 means no limit, which is used for trade
   3.267 +  and item evolutions), followed by the internal id of the pokemon
   3.268 +  into which to evolve. Hence, level up and trade evolutions are
   3.269 +  described with 3
   3.270 +  bytes; item evolutions with four."
   3.271 +  [coll]
   3.272 +  (let [method (first coll)]
   3.273 +    (cond (empty? coll) []
   3.274 +          (= 0 method) [] ;; just in case
   3.275 +          (= 1 method) ;; level-up evolution
   3.276 +          (conj (format-evo (drop 3 coll))
   3.277 +                            {:method :level-up
   3.278 +                             :min-level (nth coll 1)
   3.279 +                             :into (dec (nth coll 2))})
   3.280 +          
   3.281 +          (= 2 method) ;; item evolution
   3.282 +          (conj (format-evo (drop 4 coll))
   3.283 +                {:method :item
   3.284 +                 :item (dec (nth coll 1))
   3.285 +                 :min-level (nth coll 2)
   3.286 +                 :into (dec (nth coll 3))})
   3.287 +
   3.288 +          (= 3 method) ;; trade evolution
   3.289 +          (conj (format-evo (drop 3 coll))
   3.290 +                {:method :trade
   3.291 +                 :min-level (nth coll 1) ;; always 1 for trade.
   3.292 +                 :into (dec (nth coll 2))}))))
   3.293 +
   3.294 +
   3.295 +(defn hxc-ptrs-evolve
   3.296 +  "A hardcoded collection of 190 pointers to alternating evolution/learnset data,
   3.297 +in internal order."
   3.298 +  ([]
   3.299 +     (hxc-ptrs-evolve com.aurellem.gb.gb-driver/original-rom))
   3.300 +  ([rom]
   3.301 +     (let [
   3.302 +           pkmn-count (count (hxc-pokenames-raw)) ;; 190
   3.303 +           ptrs
   3.304 +           (map (fn [[a b]] (low-high a b))
   3.305 +                (partition 2
   3.306 +                           (take (* 2 pkmn-count)
   3.307 +                                 (drop 0x3b1e5 rom))))]
   3.308 +       (map (partial + 0x34000) ptrs)
   3.309 +
   3.310 +       )))
   3.311 +#+end_src
   3.312 +
   3.313 +#+name:evolution
   3.314 +#+begin_src clojure
   3.315 +
   3.316 +(defn hxc-evolution
   3.317 +  "Hardcoded evolution data in memory. The data exists at ROM@34000,
   3.318 +  sorted by internal order. Pointers to the data exist at ROM@3B1E5; see also, hxc-ptrs-evolve."
   3.319 +  ([] (hxc-evolution com.aurellem.gb.gb-driver/original-rom))
   3.320 +  ([rom]
   3.321 +       (apply assoc {}
   3.322 +               (interleave
   3.323 +                (hxc-pokenames rom)
   3.324 +                (map
   3.325 +                 (comp
   3.326 +                  format-evo
   3.327 +                  (partial take-while (comp not zero?))
   3.328 +                  #(drop % rom))
   3.329 +                 (hxc-ptrs-evolve rom)
   3.330 +                 )))))
   3.331 +
   3.332 +(defn hxc-evolution-pretty
   3.333 +  "Like hxc-evolution, except it uses the names of items and pokemon
   3.334 +--- grabbed from ROM --- rather than their numerical identifiers."
   3.335 +  ([] (hxc-evolution-pretty com.aurellem.gb.gb-driver/original-rom))
   3.336 +  ([rom]
   3.337 +     (let
   3.338 +         [poke-names (vec (hxc-pokenames rom))
   3.339 +          item-names (vec (hxc-items rom))
   3.340 +          use-names
   3.341 +          (fn [m]
   3.342 +            (loop [ks (keys m) new-map m]
   3.343 +              (let [k (first ks)]
   3.344 +                (cond (nil? ks) new-map
   3.345 +                      (= k :into)
   3.346 +                      (recur
   3.347 +                       (next ks)
   3.348 +                       (assoc new-map
   3.349 +                         :into
   3.350 +                         (poke-names
   3.351 +                          (:into
   3.352 +                           new-map))))
   3.353 +                      (= k :item)
   3.354 +                      (recur
   3.355 +                       (next ks)
   3.356 +                       (assoc new-map
   3.357 +                         :item
   3.358 +                         (item-names
   3.359 +                          (:item new-map))))
   3.360 +                      :else
   3.361 +                      (recur
   3.362 +                       (next ks)
   3.363 +                       new-map)
   3.364 +                      ))))]
   3.365 +
   3.366 +       (into {}
   3.367 +             (map (fn [[pkmn evo-coll]]
   3.368 +                    [pkmn (map use-names evo-coll)])
   3.369 +                  (hxc-evolution rom))))))
   3.370 +
   3.371 +
   3.372 +#+end_src
   3.373 +
   3.374 +
   3.375 +** Level-up moves (learnsets)
   3.376 +#+name: learnsets
   3.377 +#+begin_src clojure
   3.378 +
   3.379 +
   3.380 +(defn hxc-learnsets
   3.381 +  "Hardcoded map associating pokemon names to lists of pairs [lvl
   3.382 +  move] of abilities they learn as they level up. The data
   3.383 +exists at ROM@34000, sorted by internal order. Pointers to the data
   3.384 +  exist at ROM@3B1E5; see also, hxc-ptrs-evolve"
   3.385 +  ([] (hxc-learnsets com.aurellem.gb.gb-driver/original-rom))
   3.386 +  ([rom]
   3.387 +     (apply assoc
   3.388 +            {}
   3.389 +            (interleave
   3.390 +             (hxc-pokenames rom)
   3.391 +             (map (comp
   3.392 +                   (partial map
   3.393 +                            (fn [[lvl mv]] [lvl (dec mv)]))
   3.394 +                   (partial partition 2)
   3.395 +                   ;; keep the learnset data
   3.396 +                   (partial take-while (comp not zero?))
   3.397 +                   ;; skip the evolution data
   3.398 +                   rest
   3.399 +                   (partial drop-while (comp not zero?)))
   3.400 +                  (map #(drop % rom) 
   3.401 +                       (hxc-ptrs-evolve rom)))))))
   3.402 +
   3.403 +(defn hxc-learnsets-pretty
   3.404 +  "Live hxc-learnsets except it reports the name of each move --- as
   3.405 +it appears in rom --- rather than the move index."
   3.406 +  ([] (hxc-learnsets-pretty com.aurellem.gb.gb-driver/original-rom))
   3.407 +  ([rom]
   3.408 +     (let [moves (vec(map format-name (hxc-move-names)))]
   3.409 +       (into {}
   3.410 +             (map (fn [[pkmn learnset]]
   3.411 +                    [pkmn (map (fn [[lvl mv]] [lvl (moves mv)])
   3.412 +                               learnset)])
   3.413 +                  (hxc-learnsets rom))))))
   3.414 +             
   3.415 +                                  
   3.416 +
   3.417 +#+end_src
   3.418 +
   3.419 +
   3.420 +
   3.421 +* Pok\eacute{}mon II : the Pok\eacute{}dex
   3.422 +** Species vital stats
   3.423 +#+name: dex-stats
   3.424 +#+begin_src clojure
   3.425 +(defn hxc-pokedex-stats
   3.426 +  "The hardcoded pokedex stats (species height weight) in memory. List
   3.427 +begins at ROM@40687"
   3.428 +  ([] (hxc-pokedex-stats com.aurellem.gb.gb-driver/original-rom))
   3.429 +  ([rom]
   3.430 +     (let [pokedex-names (zipmap (range) (hxc-pokedex-names rom))
   3.431 +           pkmn-count (count pokedex-names)
   3.432 +           ]
   3.433 +     ((fn capture-stats
   3.434 +        [n stats data]
   3.435 +        (if (zero? n) stats
   3.436 +            (let [[species
   3.437 +                   [_
   3.438 +                    height-ft
   3.439 +                    height-in
   3.440 +                    weight-1
   3.441 +                    weight-2
   3.442 +                    _
   3.443 +                    dex-ptr-1
   3.444 +                    dex-ptr-2
   3.445 +                    dex-bank
   3.446 +                    _
   3.447 +                    & data]]
   3.448 +                  (split-with (partial not= 0x50) data)]
   3.449 +              (recur (dec n)
   3.450 +                     (assoc stats
   3.451 +                       (pokedex-names (- pkmn-count (dec n)))
   3.452 +                       {:species
   3.453 +                        (format-name (character-codes->str species))
   3.454 +                        :height-ft
   3.455 +                        height-ft
   3.456 +                        :height-in
   3.457 +                        height-in
   3.458 +                        :weight
   3.459 +                        (/ (low-high weight-1 weight-2) 10.)
   3.460 +                        
   3.461 +                    ;;    :text
   3.462 +                    ;;    (character-codes->str
   3.463 +                    ;;     (take-while
   3.464 +                    ;;      (partial not= 0x50)
   3.465 +                    ;;      (drop
   3.466 +                    ;;       (+ 0xB8000
   3.467 +                    ;;          -0x4000
   3.468 +                    ;;          (low-high dex-ptr-1 dex-ptr-2))
   3.469 +                    ;;       rom)))
   3.470 +                        })
   3.471 +                     
   3.472 +                     data)
   3.473 +              
   3.474 +              
   3.475 +              )))
   3.476 +      
   3.477 +      pkmn-count
   3.478 +      {}
   3.479 +      (drop 0x40687 rom)))       ))
   3.480 +#+end_src
   3.481 +
   3.482 +** Species synopses
   3.483 +
   3.484 +#+name: dex-text
   3.485 +#+begin_src clojure
   3.486 +(def hxc-pokedex-text-raw
   3.487 +  "The hardcoded pokedex entries in memory. List begins at
   3.488 +ROM@B8000, shortly before move names."
   3.489 +  (hxc-thunk-words 0xB8000 14754))
   3.490 +
   3.491 +
   3.492 +
   3.493 +
   3.494 +(defn hxc-pokedex-text
   3.495 +  "The hardcoded pokedex entries in memory, presented as an
   3.496 +associative hash map. List begins at ROM@B8000."
   3.497 +  ([] (hxc-pokedex-text com.aurellem.gb.gb-driver/original-rom))
   3.498 +  ([rom]
   3.499 +     (zipmap
   3.500 +      (hxc-pokedex-names rom)
   3.501 +      (cons nil ;; for missingno.
   3.502 +            (hxc-pokedex-text-raw rom)))))
   3.503 +#+end_src
   3.504 +
   3.505 +
   3.506 +** Pok\eacute{}mon cries
   3.507 +#+name: pokecry
   3.508 +#+begin_src clojure
   3.509 +(defn hxc-cry
   3.510 +  "The pokemon cry data in internal order. List begins at ROM@39462"
   3.511 +  ([](hxc-cry com.aurellem.gb.gb-driver/original-rom))
   3.512 +  ([rom]
   3.513 +     (zipmap
   3.514 +      (hxc-pokenames rom)
   3.515 +      (map
   3.516 +       (fn [[cry-id pitch length]]
   3.517 +         {:cry-id cry-id
   3.518 +         :pitch pitch
   3.519 +          :length length}
   3.520 +         )
   3.521 +       (partition 3
   3.522 +                  (drop 0x39462 rom))))))
   3.523 +
   3.524 +(defn hxc-cry-groups
   3.525 +  ([] (hxc-cry-groups com.aurellem.gb.gb-driver/original-rom))
   3.526 +  ([rom]
   3.527 +     (map #(mapv first
   3.528 +                (filter
   3.529 +                 (fn [[k v]]
   3.530 +                   (= % (:cry-id v)))
   3.531 +                 (hxc-cry)))
   3.532 +          ((comp
   3.533 +            range
   3.534 +            count
   3.535 +            set
   3.536 +            (partial map :cry-id)
   3.537 +            vals
   3.538 +            hxc-cry)
   3.539 +           rom))))
   3.540 +
   3.541 +
   3.542 +(defn cry-conversion!
   3.543 +  "Convert Porygon's cry in ROM to be the cry of the given pokemon."
   3.544 +  [pkmn]
   3.545 +  (write-rom!
   3.546 +   (rewrite-memory
   3.547 +    (vec(rom))
   3.548 +    0x3965D 
   3.549 +    (map second
   3.550 +         ((hxc-cry) pkmn)))))
   3.551 +
   3.552 +#+end_src
   3.553 +
   3.554 +** COMMENT Names of permanent stats
   3.555 +0DD4D-DD72   
   3.556 +
   3.557 +* Items
   3.558 +** Item names
   3.559 +#+name: item-names
   3.560 +#+begin_src clojure
   3.561 +
   3.562 +(def hxc-items-raw
   3.563 +  "The hardcoded names of the items in memory. List begins at
   3.564 +ROM@045B7"
   3.565 +  (hxc-thunk-words 0x45B7 870))
   3.566 +
   3.567 +(def hxc-items
   3.568 +  "The hardcoded names of the items in memory, presented as
   3.569 +  keywords. List begins at ROM@045B7. See also, hxc-items-raw."
   3.570 +  (comp (partial map format-name) hxc-items-raw))
   3.571 +#+end_src
   3.572 +
   3.573 +** Item prices
   3.574 +#+name: item-prices
   3.575 +#+begin_src clojure
   3.576 +(defn hxc-item-prices
   3.577 +  "The hardcoded list of item prices in memory. List begins at ROM@4495"
   3.578 +  ([] (hxc-item-prices com.aurellem.gb.gb-driver/original-rom))
   3.579 +  ([rom]
   3.580 +     (let [items (hxc-items rom)
   3.581 +           price-size 3]
   3.582 +       (zipmap items
   3.583 +               (map (comp
   3.584 +                     ;; zero-cost items are "priceless"
   3.585 +                     #(if (zero? %) :priceless %)
   3.586 +                     decode-bcd butlast)
   3.587 +                    (partition price-size
   3.588 +                               (take (* price-size (count items))
   3.589 +                                     (drop 0x4495 rom))))))))
   3.590 +#+end_src
   3.591 +** Vendor inventories
   3.592 +
   3.593 +#+name: item-vendors
   3.594 +#+begin_src clojure
   3.595 +(defn hxc-shops
   3.596 +  ([] (hxc-shops com.aurellem.gb.gb-driver/original-rom))
   3.597 +  ([rom]
   3.598 +     (let [items (zipmap (range) (hxc-items rom))
   3.599 +
   3.600 +           ;; temporarily softcode the TM items
   3.601 +           items (into
   3.602 +                  items
   3.603 +                  (map (juxt identity
   3.604 +                             (comp keyword
   3.605 +                                   (partial str "tm-")
   3.606 +                                   (partial + 1 -200)
   3.607 +                                   )) 
   3.608 +                       (take 200 (drop 200 (range)))))
   3.609 +                       
   3.610 +           ]
   3.611 +
   3.612 +       ((fn parse-shop [coll [num-items & items-etc]]
   3.613 +          (let [inventory (take-while
   3.614 +                           (partial not= 0xFF)
   3.615 +                           items-etc)
   3.616 +               [separator & items-etc] (drop num-items (rest items-etc))]
   3.617 +           (if (= separator 0x50)
   3.618 +             (map (partial mapv (comp items dec)) (conj coll inventory))
   3.619 +             (recur (conj coll inventory) items-etc)
   3.620 +             )
   3.621 +         ))
   3.622 +
   3.623 +        '()
   3.624 +        (drop 0x233C rom))
   3.625 +
   3.626 +     
   3.627 +     )))
   3.628 +#+end_src
   3.629 +
   3.630 +#+results: item-vendors
   3.631 +: #'com.aurellem.gb.hxc/hxc-shops
   3.632 +
   3.633 +
   3.634 +
   3.635 +* Types
   3.636 +** Names of types
   3.637 +#+name: type-names
   3.638 +#+begin_src clojure
   3.639 +(def hxc-types
   3.640 +  "The hardcoded type names in memory. List begins at ROM@27D99,
   3.641 +  shortly before hxc-titles."
   3.642 +  (hxc-thunk-words 0x27D99 102))
   3.643 +
   3.644 +#+end_src
   3.645 +
   3.646 +** Type effectiveness
   3.647 +#+name: type-advantage
   3.648 +#+begin_src clojure
   3.649 +(defn hxc-advantage
   3.650 +  ;; in-game multipliers are stored as 10x their effective value
   3.651 +  ;; to allow for fractional multipliers like 1/2
   3.652 +  
   3.653 +  "The hardcoded type advantages in memory, returned as tuples of
   3.654 +  atk-type def-type multiplier. By default (i.e. if not listed here),
   3.655 +the multiplier is 1. List begins at 0x3E62D."
   3.656 +  ([] (hxc-advantage com.aurellem.gb.gb-driver/original-rom))
   3.657 +  ([rom]
   3.658 +     (map
   3.659 +      (fn [[atk def mult]] [(get pkmn-types atk (hex atk))
   3.660 +                            (get pkmn-types def (hex def))
   3.661 +                            (/ mult 10)])
   3.662 +      (partition 3
   3.663 +                 (take-while (partial not= 0xFF)
   3.664 +                             (drop 0x3E62D rom))))))
   3.665 +#+end_src
   3.666 +
   3.667 +
   3.668 +
   3.669 +* Moves
   3.670 +** Names of moves
   3.671 +#+name: move-names
   3.672 +#+begin_src clojure
   3.673 +(def hxc-move-names
   3.674 +  "The hardcoded move names in memory. List begins at ROM@BC000"
   3.675 +  (hxc-thunk-words 0xBC000 1551))
   3.676 +#+end_src
   3.677 +   
   3.678 +** Properties of moves 
   3.679 +
   3.680 +#+name: move-data
   3.681 +#+begin_src clojure
   3.682 +(defn hxc-move-data
   3.683 +  "The hardcoded (basic (move effects)) in memory. List begins at
   3.684 +0x38000. Returns a map of {:name :power :accuracy :pp :fx-id
   3.685 +  :fx-txt}. The move descriptions are handwritten, not hardcoded."
   3.686 +  ([]
   3.687 +     (hxc-move-data com.aurellem.gb.gb-driver/original-rom))
   3.688 +  ([rom]
   3.689 +     (let [names (vec (hxc-move-names rom))
   3.690 +           move-count (count names)
   3.691 +           move-size 6
   3.692 +           types pkmn-types ;;; !! hardcoded types
   3.693 +           ]
   3.694 +       (zipmap (map format-name names)
   3.695 +               (map
   3.696 +                (fn [[idx effect power type-id accuracy pp]]
   3.697 +                  {:name (names (dec idx))
   3.698 +                   :power power
   3.699 +                   :accuracy  accuracy
   3.700 +                   :pp pp
   3.701 +                   :type (types type-id)
   3.702 +                   :fx-id effect
   3.703 +                   :fx-txt (get move-effects effect) 
   3.704 +                   }
   3.705 +                  )
   3.706 +                
   3.707 +                (partition move-size
   3.708 +                           (take (* move-size move-count)
   3.709 +                                 (drop 0x38000 rom))))))))
   3.710 +  
   3.711 +
   3.712 +
   3.713 +(defn hxc-move-data*
   3.714 +  "Like hxc-move-data, but reports numbers as hexadecimal symbols instead."
   3.715 +  ([]
   3.716 +     (hxc-move-data* com.aurellem.gb.gb-driver/original-rom))
   3.717 +  ([rom]
   3.718 +     (let [names (vec (hxc-move-names rom))
   3.719 +           move-count (count names)
   3.720 +           move-size 6
   3.721 +           format-name (fn [s]
   3.722 +                         (keyword (.toLowerCase
   3.723 +                                   (apply str
   3.724 +                                          (map #(if (= % \space) "-" %) s)))))
   3.725 +           ]
   3.726 +       (zipmap (map format-name names)
   3.727 +               (map
   3.728 +                (fn [[idx effect power type accuracy pp]]
   3.729 +                  {:name (names (dec idx))
   3.730 +                   :power power
   3.731 +                   :accuracy (hex accuracy)
   3.732 +                   :pp pp
   3.733 +                   :fx-id (hex effect)
   3.734 +                   :fx-txt (get move-effects effect) 
   3.735 +                   }
   3.736 +                  )
   3.737 +                
   3.738 +                (partition move-size
   3.739 +                           (take (* move-size move-count)
   3.740 +                                 (drop 0x38000 rom))))))))
   3.741 +  
   3.742 +#+end_src
   3.743 +
   3.744 +** TM and HM moves
   3.745 +
   3.746 +#+name: machines
   3.747 +#+begin_src clojure
   3.748 +(defn hxc-machines
   3.749 +  "The hardcoded moves taught by TMs and HMs. List begins at ROM@1232D."
   3.750 +  ([] (hxc-machines
   3.751 +       com.aurellem.gb.gb-driver/original-rom))
   3.752 +  ([rom]
   3.753 +     (let [moves (hxc-move-names rom)]
   3.754 +       (zipmap
   3.755 +        (range)
   3.756 +        (take-while
   3.757 +            (comp not nil?)
   3.758 +            (map (comp
   3.759 +                  format-name
   3.760 +                  (zipmap
   3.761 +                   (range)
   3.762 +                   moves)
   3.763 +              dec)
   3.764 +                 (take 100
   3.765 +                       (drop 0x1232D rom))))))))
   3.766 +
   3.767 +#+end_src
   3.768 +
   3.769 +
   3.770 +
   3.771 +
   3.772 +
   3.773 +** COMMENT Status ailments
   3.774 +
   3.775 +* Places
   3.776 +** Names of places
   3.777 +
   3.778 +#+name: places
   3.779 +#+begin_src clojure
   3.780 +(def hxc-places
   3.781 +  "The hardcoded place names in memory. List begins at
   3.782 +ROM@71500. [Cinnabar] Mansion seems to be dynamically calculated."
   3.783 +  (hxc-thunk-words 0x71500 560))
   3.784 +
   3.785 +#+end_src
   3.786 +
   3.787 +** Wild Pok\eacute{}mon demographics
   3.788 +#+name: wilds
   3.789 +#+begin_src clojure
   3.790 +
   3.791 +
   3.792 +
   3.793 +(defn hxc-ptrs-wild
   3.794 +  "A list of the hardcoded wild encounter data in memory. Pointers
   3.795 +  begin at ROM@0CB95; data begins at ROM@0x04D89" 
   3.796 +  ([] (hxc-ptrs-wild com.aurellem.gb.gb-driver/original-rom))
   3.797 +  ([rom]
   3.798 +     (let [ptrs
   3.799 +           (map (fn [[a b]] (+ a (* 0x100 b)))
   3.800 +            (take-while (partial not= (list 0xFF 0xFF))
   3.801 +                            (partition 2 (drop 0xCB95 rom))))]
   3.802 +       ptrs)))
   3.803 +
   3.804 +
   3.805 +
   3.806 +(defn hxc-wilds
   3.807 +  "A list of the hardcoded wild encounter data in memory. Pointers
   3.808 +  begin at ROM@0CB95; data begins at ROM@0x04D89" 
   3.809 +  ([] (hxc-wilds com.aurellem.gb.gb-driver/original-rom))
   3.810 +  ([rom]
   3.811 +     (let [pokenames (zipmap (range) (hxc-pokenames rom))]
   3.812 +       (map
   3.813 +        (partial map (fn [[a b]] {:species (pokenames (dec b)) :level
   3.814 +                                  a}))
   3.815 +        (partition 10
   3.816 +                     
   3.817 +                      (take-while (comp (partial not= 1)
   3.818 +                                        first) 
   3.819 +                                 (partition 2
   3.820 +                                            (drop 0xCD8C rom))
   3.821 +                   
   3.822 +                   ))))))
   3.823 +
   3.824 +#+end_src
   3.825 +
   3.826 +
   3.827 +
   3.828 +
   3.829 +
   3.830 +* Appendices
   3.831 +
   3.832 +
   3.833 +
   3.834 +** Mapping the ROM
   3.835  
   3.836  | ROM address (hex) | Description     | Format          | Example         |
   3.837  |-------------------+-----------------+-----------------+-----------------|
   3.838 @@ -17,32 +840,528 @@
   3.839  | 0233C-            | Shop inventories. |                 |                 |
   3.840  | 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. |
   3.841  | 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#... |
   3.842 -| 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, level 0 end-of-list. |
   3.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). |
   3.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. |
   3.845  | 06698-            | ? Background music. |                 |                 |
   3.846  | 0822E-082F?       | Pointers to background music, part I. |                 |                 |
   3.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 above). |
   3.848 -| 1232D-            | Which moves are taught by the TMs and HMs |                 |                 |
   3.849 +| 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). |
   3.850 +|-------------------+-----------------+-----------------+-----------------|
   3.851 +| 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             |
   3.852 +| 0DAE0.            | Amount of HP restored by Super Potion. | "               | 50              |
   3.853 +| 0DAE3.            | Amount of HP restored by Potion. | "               | 20              |
   3.854 +|-------------------+-----------------+-----------------+-----------------|
   3.855 +| 0DD4D-DD72        | Names of permanent stats. | Variable-length strings separated by 0x50. | #HEALTH#ATTACK#DEFENSE#SPEED#SPECIAL# |
   3.856 +| 1195C-1196A       | The two terms for being able/unable to learn a TM/HM. | Variable-length strings separated by 0x50. | ABLE#NOT ABLE#  |
   3.857 +| 119C0-119CE       | The two terms for being able/unable to evolve using the current stone. | Variable-length strings separated by 0x50. | ABLE#NOT ABLE#  |
   3.858 +| 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), ... |
   3.859  | 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#... |
   3.860 -| 27E77-            | Trainer title names. |                 | YOUNGSTER#BUG CATCHER#LASS#... |
   3.861 -| 34000-            | Evolution and learnset data. |                 |                 |
   3.862 -| 38000-            | The basic properties and effects of moves. | 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. |
   3.863 +| 27E77-            | Trainer title names. | Variable-length names separated by 0x80. | YOUNGSTER#BUG CATCHER#LASS#... |
   3.864 +| 34000-            |                 |                 |                 |
   3.865 +| 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. |
   3.866  | 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. |                 |                 |
   3.867  | 39462-            | The Pok\eacute{}mon cry data. | Fixed-length (3 byte) descriptions of cries. |                 |
   3.868  | 3B1E5-            | Pointers to evolution/learnset data. |                 |                 |
   3.869 +| 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. |                 |
   3.870  | 40687-            | Species data from the Pok\eacute{}dex: species name, height, weight, etc. | Fixed-length sequences of bytes. See below for specifics. |                 |
   3.871  | 410B1-            | A conversion table between internal order and Pokedex order. |                 |                 |
   3.872 +| 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]... |
   3.873  | 71500-            | Names of places. |                 |                 |
   3.874  | 7C249-7C2??       | Pointers to background music, pt II. |                 |                 |
   3.875  | 98000-            | Dialogue        |                 |                 |
   3.876  | B8000-            | The text of each Pokemon's Pok\eacute{}dex entry. |                 |                 |
   3.877 -| BC000-            | Move names.     |                 |                 |
   3.878 +| BC000-BC60E       | Move names.     | Variable-length move names, separated by 0x80. The moves are in internal order. | POUND#KARATE CHOP#DOUBLESLAP#COMET PUNCH#... |
   3.879  | 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♂#... |
   3.880  |                   |                 |                 |                 |
   3.881  |                   |                 |                 |                 |
   3.882  
   3.883 -* Appendices
   3.884 +
   3.885 +
   3.886  ** Internal Pok\eacute{}mon IDs
   3.887  ** Type IDs
   3.888 +
   3.889 +#+name: type-ids
   3.890 +#+begin_src clojure
   3.891 +(def pkmn-types
   3.892 +  [:normal    ;;0
   3.893 +   :fighting  ;;1
   3.894 +   :flying    ;;2
   3.895 +   :poison    ;;3
   3.896 +   :ground    ;;4
   3.897 +   :rock      ;;5
   3.898 +   :bird      ;;6
   3.899 +   :bug       ;;7
   3.900 +   :ghost     ;;8
   3.901 +   :A
   3.902 +   :B
   3.903 +   :C
   3.904 +   :D
   3.905 +   :E
   3.906 +   :F
   3.907 +   :G
   3.908 +   :H
   3.909 +   :I
   3.910 +   :J
   3.911 +   :K
   3.912 +   :fire     ;;20 (0x14)
   3.913 +   :water    ;;21 (0x15)
   3.914 +   :grass    ;;22 (0x16)
   3.915 +   :electric ;;23 (0x17)
   3.916 +   :psychic  ;;24 (0x18)
   3.917 +   :ice      ;;25 (0x19)
   3.918 +   :dragon   ;;26 (0x1A)
   3.919 +   ])
   3.920 +#+end_src
   3.921 +
   3.922  ** Basic effects of moves
   3.923 +
   3.924 +*** Table of basic effects
   3.925 +
   3.926 +The possible effects of moves in Pok\eacute{}mon \mdash{} for example, dealing
   3.927 +damage, leeching health, or potentially poisoning the opponent
   3.928 +\mdash{} are stored in a table. Each move has exactly one effect, and
   3.929 +different moves might have the same effect.
   3.930 +
   3.931 +For example, Leech Life, Mega Drain, and Absorb all have effect ID #3, which is \ldquo{}Leech half of the inflicted damage.\rdquo{}
   3.932 +
   3.933 +All the legitimate move effects are listed in the table
   3.934 +below. Here are some notes for reading it:
   3.935 +
   3.936 +- Whenever an effect has a chance of doing something (like a chance of
   3.937 +  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.
   3.938 +- For some effects, the description is too cumbersome to
   3.939 +  write. Instead, I just write a move name
   3.940 +  in parentheses, like: (leech seed). That move gives a characteristic example
   3.941 +  of the effect.
   3.942 +- I use the abbreviations =atk=, =def=, =spd=, =spc=, =acr=, =evd= for
   3.943 +  attack, defense, speed, special, accuracy, and evasion. 
   3.944 +.
   3.945 +
   3.946 +
   3.947 +
   3.948 +| ID (hex) | Description                                                                                     | Notes                                                            |
   3.949 +|----------+-------------------------------------------------------------------------------------------------+------------------------------------------------------------------|
   3.950 +|        0 | normal damage                                                                                   |                                                                  |
   3.951 +|        1 | no damage, just sleep                                                                           | TODO: find out how many turns                                    |
   3.952 +|        2 | 0x4C chance of poison                                                                           |                                                                  |
   3.953 +|        3 | leech half of inflicted damage                                                                  |                                                                  |
   3.954 +|        4 | 0x19 chance of burn                                                                             |                                                                  |
   3.955 +|        5 | 0x19 chance of freeze                                                                           |                                                                  |
   3.956 +|        6 | 0x19 chance of paralysis                                                                        |                                                                  |
   3.957 +|        7 | user faints; opponent's defense is halved during attack.                                        |                                                                  |
   3.958 +|        8 | leech half of inflicted damage ONLY if the opponent is asleep                                   |                                                                  |
   3.959 +|        9 | imitate last attack                                                                             |                                                                  |
   3.960 +|        A | user atk +1                                                                                     |                                                                  |
   3.961 +|        B | user def +1                                                                                     |                                                                  |
   3.962 +|        C | user spd +1                                                                                     |                                                                  |
   3.963 +|        D | user spc +1                                                                                     |                                                                  |
   3.964 +|        E | user acr +1                                                                                     | This effect is unused.                                           |
   3.965 +|        F | user evd +1                                                                                     |                                                                  |
   3.966 +|       10 | get post-battle money = 2 * level * uses                                                        |                                                                  |
   3.967 +|       11 | move has 0xFE acr, regardless of battle stat modifications.                                     |                                                                  |
   3.968 +|       12 | opponent atk -1                                                                                 |                                                                  |
   3.969 +|       13 | opponent def -1                                                                                 |                                                                  |
   3.970 +|       14 | opponent spd -1                                                                                 |                                                                  |
   3.971 +|       15 | opponent spc -1                                                                                 |                                                                  |
   3.972 +|       16 | opponent acr -1                                                                                 |                                                                  |
   3.973 +|       17 | opponent evd -1                                                                                 |                                                                  |
   3.974 +|       18 | converts user's type to opponent's.                                                             |                                                                  |
   3.975 +|       19 | (haze)                                                                                          |                                                                  |
   3.976 +|       1A | (bide)                                                                                          |                                                                  |
   3.977 +|       1B | (thrash)                                                                                        |                                                                  |
   3.978 +|       1C | (teleport)                                                                                      |                                                                  |
   3.979 +|       1D | (fury swipes)                                                                                   |                                                                  |
   3.980 +|       1E | attacks 2-5 turns                                                                               | Unused. TODO: find out what it does.                             |
   3.981 +|       1F | 0x19 chance of flinching                                                                        |                                                                  |
   3.982 +|       20 | opponent sleep for 1-7 turns                                                                    |                                                                  |
   3.983 +|       21 | 0x66 chance of poison                                                                           |                                                                  |
   3.984 +|       22 | 0x4D chance of burn                                                                             |                                                                  |
   3.985 +|       23 | 0x4D chance of freeze                                                                           |                                                                  |
   3.986 +|       24 | 0x4D chance of paralysis                                                                        |                                                                  |
   3.987 +|       25 | 0x4D chance of flinching                                                                        |                                                                  |
   3.988 +|       26 | one-hit KO                                                                                      |                                                                  |
   3.989 +|       27 | charge one turn, atk next.                                                                      |                                                                  |
   3.990 +|       28 | fixed damage, leaves 1HP.                                                                       | Is the fixed damage the power of the move?                       |
   3.991 +|       29 | fixed damage.                                                                                   | Like seismic toss, dragon rage, psywave.                         |
   3.992 +|       2A | atk 2-5 turns; opponent can't attack                                                            | The odds of attacking for /n/ turns are: (0 0x60 0x60 0x20 0x20) |
   3.993 +|       2B | charge one turn, atk next. (can't be hit when charging)                                         |                                                                  |
   3.994 +|       2C | atk hits twice.                                                                                 |                                                                  |
   3.995 +|       2D | user takes 1 damage if misses.                                                                  |                                                                  |
   3.996 +|       2E | evade status-lowering effects                                                                   | Caused by you or also your opponent?                             |
   3.997 +|       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.          |
   3.998 +|       30 | atk causes recoil dmg = 1/4 dmg dealt                                                           |                                                                  |
   3.999 +|       31 | confuses opponent                                                                               |                                                                  |
  3.1000 +|       32 | user atk +2                                                                                     |                                                                  |
  3.1001 +|       33 | user def +2                                                                                     |                                                                  |
  3.1002 +|       34 | user spd +2                                                                                     |                                                                  |
  3.1003 +|       35 | user spc +2                                                                                     |                                                                  |
  3.1004 +|       36 | user acr +2                                                                                     | This effect is unused.                                           |
  3.1005 +|       37 | user evd +2                                                                                     | This effect is unused.                                           |
  3.1006 +|       38 | restores up to half of user's max hp.                                                           |                                                                  |
  3.1007 +|       39 | (transform)                                                                                     |                                                                  |
  3.1008 +|       3A | opponent atk -2                                                                                 |                                                                  |
  3.1009 +|       3B | opponent def -2                                                                                 |                                                                  |
  3.1010 +|       3C | opponent spd -2                                                                                 |                                                                  |
  3.1011 +|       3D | opponent spc -2                                                                                 |                                                                  |
  3.1012 +|       3E | opponent acr -2                                                                                 |                                                                  |
  3.1013 +|       3F | opponent evd -2                                                                                 |                                                                  |
  3.1014 +|       40 | doubles user spc when attacked                                                                  |                                                                  |
  3.1015 +|       41 | doubles user def when attacked                                                                  |                                                                  |
  3.1016 +|       42 | just poisons opponent                                                                           |                                                                  |
  3.1017 +|       43 | just paralyzes opponent                                                                         |                                                                  |
  3.1018 +|       44 | 0x19 chance opponent atk -1                                                                     |                                                                  |
  3.1019 +|       45 | 0x19 chance opponent def -1                                                                     |                                                                  |
  3.1020 +|       46 | 0x19 chance opponent spd -1                                                                     |                                                                  |
  3.1021 +|       47 | 0x4C chance opponent spc -1                                                                     |                                                                  |
  3.1022 +|       48 | 0x19 chance opponent acr -1                                                                     |                                                                  |
  3.1023 +|       49 | 0x19 chance opponent evd -1                                                                     |                                                                  |
  3.1024 +|       4A | ???                                                                                             | ;; unused? no effect?                                            |
  3.1025 +|       4B | ???                                                                                             | ;; unused? no effect?                                            |
  3.1026 +|       4C | 0x19 chance of confusing the opponent                                                           |                                                                  |
  3.1027 +|       4D | atk hits twice. 0x33 chance opponent poisioned.                                                 |                                                                  |
  3.1028 +|       4E | broken. crash the game after attack.                                                            |                                                                  |
  3.1029 +|       4F | (substitute)                                                                                    |                                                                  |
  3.1030 +|       50 | unless opponent faints, user must recharge after atk. some exceptions apply                     |                                                                  |
  3.1031 +|       51 | (rage)                                                                                          |                                                                  |
  3.1032 +|       52 | (mimic)                                                                                         |                                                                  |
  3.1033 +|       53 | (metronome)                                                                                     |                                                                  |
  3.1034 +|       54 | (leech seed)                                                                                    |                                                                  |
  3.1035 +|       55 | does nothing (splash)                                                                           |                                                                  |
  3.1036 +|       56 | (disable)                                                                                       |                                                                  |
  3.1037 +#+end_src
  3.1038 +
  3.1039 +*** Source
  3.1040 +#+name: move-effects
  3.1041 +#+begin_src clojure
  3.1042 +(def move-effects
  3.1043 +  ["normal damage"
  3.1044 +   "no damage, just opponent sleep" ;; how many turns? is atk power ignored?
  3.1045 +   "0x4C chance of poison"
  3.1046 +   "leech half of inflicted damage"
  3.1047 +   "0x19 chance of burn"
  3.1048 +   "0x19 chance of freeze"
  3.1049 +   "0x19 chance of paralyze"
  3.1050 +   "user faints; opponent defense halved during attack."
  3.1051 +   "leech half of inflicted damage ONLY if sleeping opponent."
  3.1052 +   "imitate last attack"
  3.1053 +   "user atk +1"
  3.1054 +   "user def +1"
  3.1055 +   "user spd +1"
  3.1056 +   "user spc +1"
  3.1057 +   "user acr +1" ;; unused?!
  3.1058 +   "user evd +1"
  3.1059 +   "get post-battle $ = 2*level*uses"
  3.1060 +   "0xFE acr, no matter what."
  3.1061 +   "opponent atk -1"     ;; acr taken from move acr?
  3.1062 +   "opponent def -1"     ;;
  3.1063 +   "opponent spd -1"   ;;
  3.1064 +   "opponent spc -1" ;;
  3.1065 +   "opponent acr -1";;
  3.1066 +   "opponent evd -1"
  3.1067 +   "converts user's type to opponent's."
  3.1068 +   "(haze)"
  3.1069 +   "(bide)"
  3.1070 +   "(thrash)"
  3.1071 +   "(teleport)"
  3.1072 +   "(fury swipes)"
  3.1073 +   "attacks 2-5 turns" ;; unused? like rollout?
  3.1074 +   "0x19 chance of flinch"
  3.1075 +   "opponent sleep for 1-7 turns"
  3.1076 +   "0x66 chance of poison"
  3.1077 +   "0x4D chance of burn"
  3.1078 +   "0x4D chance of freeze"
  3.1079 +   "0x4D chance of paralyze"
  3.1080 +   "0x4D chance of flinch"
  3.1081 +   "one-hit KO"
  3.1082 +   "charge one turn, atk next."
  3.1083 +   "fixed damage, leaves 1HP." ;; how is dmg determined?
  3.1084 +   "fixed damage."             ;; cf seismic toss, dragon rage, psywave.
  3.1085 +   "atk 2-5 turns; opponent can't attack" ;; unnormalized? (0 0x60 0x60 0x20 0x20)
  3.1086 +   "charge one turn, atk next. (can't be hit when charging)"
  3.1087 +   "atk hits twice."
  3.1088 +   "user takes 1 damage if misses."
  3.1089 +   "evade status-lowering effects" ;;caused by you or also your opponent?
  3.1090 +   "(broken) if user is slower than opponent, makes critical hit impossible, otherwise has no effect"
  3.1091 +   "atk causes recoil dmg = 1/4 dmg dealt"
  3.1092 +   "confuses opponent" ;; acr taken from move acr
  3.1093 +   "user atk +2"
  3.1094 +   "user def +2"
  3.1095 +   "user spd +2"
  3.1096 +   "user spc +2"
  3.1097 +   "user acr +2" ;; unused!
  3.1098 +   "user evd +2" ;; unused!
  3.1099 +   "restores up to half of user's max hp." ;; broken: fails if the difference
  3.1100 +   ;; b/w max and current hp is one less than a multiple of 256.
  3.1101 +   "(transform)"
  3.1102 +   "opponent atk -2"
  3.1103 +   "opponent def -2"
  3.1104 +   "opponent spd -2"
  3.1105 +   "opponent spc -2"
  3.1106 +   "opponent acr -2"
  3.1107 +   "opponent evd -2"
  3.1108 +   "doubles user spc when attacked"
  3.1109 +   "doubles user def when attacked"
  3.1110 +   "just poisons opponent"   ;;acr taken from move acr
  3.1111 +   "just paralyzes opponent" ;;
  3.1112 +   "0x19 chance opponent atk -1"
  3.1113 +   "0x19 chance opponent def -1"
  3.1114 +   "0x19 chance opponent spd -1"
  3.1115 +   "0x4C chance opponent spc -1" ;; context suggest chance is 0x19
  3.1116 +   "0x19 chance opponent acr -1"
  3.1117 +   "0x19 chance opponent evd -1"
  3.1118 +   "???" ;; unused? no effect?
  3.1119 +   "???" ;; unused? no effect?
  3.1120 +   "0x19 chance opponent confused"
  3.1121 +   "atk hits twice. 0x33 chance opponent poisioned."
  3.1122 +   "broken. crash the game after attack."
  3.1123 +   "(substitute)"
  3.1124 +   "unless opponent faints, user must recharge after atk. some
  3.1125 +  exceptions apply."
  3.1126 +   "(rage)"
  3.1127 +   "(mimic)"
  3.1128 +   "(metronome)"
  3.1129 +   "(leech seed)"
  3.1130 +   "does nothing (splash)"
  3.1131 +   "(disable)"
  3.1132 +   ])
  3.1133 +#+end_src
  3.1134 +
  3.1135 +
  3.1136  ** Alphabet code
  3.1137 +
  3.1138 +* Source
  3.1139 +
  3.1140 +#+begin_src clojure :tangle ../clojure/com/aurellem/gb/hxc.clj
  3.1141 +
  3.1142 +(ns com.aurellem.gb.hxc
  3.1143 +  (:use (com.aurellem.gb assembly characters gb-driver util mem-util
  3.1144 +                         constants species))
  3.1145 +  (:import [com.aurellem.gb.gb_driver SaveState]))
  3.1146 +
  3.1147 +; ************* HANDWRITTEN CONSTANTS
  3.1148 +
  3.1149 +<<type-ids>>
  3.1150 +
  3.1151 +
  3.1152 +;; question: when status effects claim to take
  3.1153 +;; their accuracy from the move accuracy, does
  3.1154 +;; this mean that the move always "hits" but the
  3.1155 +;; status effect may not?
  3.1156 +
  3.1157 +<<move-effects>>
  3.1158 +
  3.1159 +;; ************** HARDCODED DATA
  3.1160 +
  3.1161 +<<hxc-thunks>>
  3.1162 +;; --------------------------------------------------
  3.1163 +
  3.1164 +<<pokenames>>
  3.1165 +<<type-names>>
  3.1166 +
  3.1167 +;; http://hax.iimarck.us/topic/581/
  3.1168 +<<pokecry>>
  3.1169 +
  3.1170 +
  3.1171 +<<item-names>>
  3.1172 +
  3.1173 +
  3.1174 +
  3.1175 +(def hxc-titles
  3.1176 +  "The hardcoded names of the trainer titles in memory. List begins at
  3.1177 +ROM@27E77"
  3.1178 +  (hxc-thunk-words 0x27E77 196))
  3.1179 +
  3.1180 +
  3.1181 +<<dex-text>>
  3.1182 +
  3.1183 +;; In red/blue, pokedex stats are in internal order.
  3.1184 +;; In yellow, pokedex stats are in pokedex order.
  3.1185 +<<dex-stats>>
  3.1186 +
  3.1187 +
  3.1188 +
  3.1189 +
  3.1190 +<<places>>
  3.1191 +
  3.1192 +(defn hxc-dialog
  3.1193 +  "The hardcoded dialogue in memory, including in-game alerts. Dialog
  3.1194 +  seems to be separated by 0x57 instead of 0x50 (END). Begins at ROM@98000."
  3.1195 +  ([rom]
  3.1196 +     (map character-codes->str
  3.1197 +          (take-nth 2
  3.1198 +                    (partition-by #(= % 0x57)
  3.1199 +                                  (take 0x0F728
  3.1200 +                                        (drop 0x98000 rom))))))
  3.1201 +  ([]
  3.1202 +     (hxc-dialog com.aurellem.gb.gb-driver/original-rom)))
  3.1203 +
  3.1204 +
  3.1205 +<<move-names>>
  3.1206 +<<move-data>>
  3.1207 +
  3.1208 +<<machines>>
  3.1209 +
  3.1210 +
  3.1211 +
  3.1212 +(defn internal-id
  3.1213 +  ([rom]
  3.1214 +     (zipmap 
  3.1215 +      (hxc-pokenames rom)
  3.1216 +      (range)))
  3.1217 +  ([]
  3.1218 +     (internal-id com.aurellem.gb.gb-driver/original-rom)))
  3.1219 +
  3.1220 +
  3.1221 +
  3.1222 +
  3.1223 +
  3.1224 +;; nidoran gender change upon levelup
  3.1225 +;; (->
  3.1226 +;;  @current-state
  3.1227 +;;  rom
  3.1228 +;;  vec
  3.1229 +;;  (rewrite-memory
  3.1230 +;;   (nth (hxc-ptrs-evolve) ((internal-id) :nidoran♂))
  3.1231 +;;   [1 1 15])
  3.1232 +;;  (rewrite-memory
  3.1233 +;;   (nth (hxc-ptrs-evolve) ((internal-id) :nidoran♀))
  3.1234 +;;   [1 1 3])
  3.1235 +;;  (write-rom!)
  3.1236 +
  3.1237 +;;  )
  3.1238 +
  3.1239 +
  3.1240 +
  3.1241 +<<type-advantage>>
  3.1242 +
  3.1243 +
  3.1244 +
  3.1245 +<<evolution-header>>
  3.1246 +<<evolution>>
  3.1247 +<<learnsets>>
  3.1248 +<<pokebase>>
  3.1249 +  
  3.1250 +  
  3.1251 +(defn hxc-intro-pkmn
  3.1252 +  "The hardcoded pokemon to display in Prof. Oak's introduction; the pokemon's
  3.1253 +internal id is stored at ROM@5EDB."
  3.1254 +  ([] (hxc-intro-pkmn
  3.1255 +       com.aurellem.gb.gb-driver/original-rom))
  3.1256 +  ([rom]
  3.1257 +     (nth (hxc-pokenames rom) (nth rom 0x5EDB))))
  3.1258 +
  3.1259 +(defn sxc-intro-pkmn!
  3.1260 +  "Set the hardcoded pokemon to display in Prof. Oak's introduction."
  3.1261 +  [pokemon]
  3.1262 +  (write-rom! 
  3.1263 +   (rewrite-rom 0x5EDB
  3.1264 +                [
  3.1265 +                 (inc
  3.1266 +                  ((zipmap
  3.1267 +                    (hxc-pokenames)
  3.1268 +                    (range))
  3.1269 +                   pokemon))])))
  3.1270 +  
  3.1271 +
  3.1272 +<<item-prices>>
  3.1273 +
  3.1274 +<<item-vendors>>
  3.1275 +
  3.1276 +<<wilds>>
  3.1277 +
  3.1278 +
  3.1279 +;; ********************** MANIPULATION FNS
  3.1280 +
  3.1281 +
  3.1282 +(defn same-type
  3.1283 +  ([pkmn move]
  3.1284 +     (same-type
  3.1285 +      com.aurellem.gb.gb-driver/original-rom pkmn move))
  3.1286 +  ([rom pkmn move]
  3.1287 +     (((comp :types (hxc-pokemon-base rom)) pkmn)
  3.1288 +      ((comp :type (hxc-move-data rom)) move))))
  3.1289 +
  3.1290 +
  3.1291 +
  3.1292 +
  3.1293 +(defn submap?
  3.1294 +  "Compares the two maps. Returns true if map-big has the same associations as map-small, otherwise false." 
  3.1295 +  [map-small map-big]
  3.1296 +  (cond (empty? map-small) true
  3.1297 +        (and
  3.1298 +         (contains? map-big (ffirst map-small))
  3.1299 +         (= (get map-big (ffirst map-small))
  3.1300 +            (second (first map-small))))
  3.1301 +        (recur (next map-small) map-big)
  3.1302 +
  3.1303 +        :else false))  
  3.1304 +
  3.1305 +
  3.1306 +(defn search-map [proto-map maps]
  3.1307 +  "Returns all the maps that make the same associations as proto-map."
  3.1308 +  (some (partial submap? proto-map) maps))
  3.1309 +
  3.1310 +(defn filter-vals
  3.1311 +  "Returns a map consisting of all the pairs [key val] for
  3.1312 +  which (pred key) returns true."
  3.1313 +  [pred map]
  3.1314 +  (reduce (partial apply assoc) {}
  3.1315 +          (filter (fn [[k v]] (pred v)) map)))
  3.1316 +
  3.1317 +
  3.1318 +(defn search-moves
  3.1319 +  "Returns a subcollection of all hardcoded moves with the
  3.1320 +  given attributes. Attributes consist of :name :power
  3.1321 +  :accuracy :pp :fx-id
  3.1322 +  (and also :fx-txt, but it contains the same information
  3.1323 +  as :fx-id)"
  3.1324 +  ([attribute-map]
  3.1325 +     (search-moves
  3.1326 +      com.aurellem.gb.gb-driver/original-rom attribute-map))
  3.1327 +  ([rom attribute-map]
  3.1328 +     (filter-vals (partial submap? attribute-map)
  3.1329 +                  (hxc-move-data rom))))
  3.1330 +
  3.1331 +
  3.1332 +
  3.1333 +
  3.1334 +
  3.1335 +;; note: 0x2f31 contains the names "TM" "HM"?
  3.1336 +
  3.1337 +;; note for later: credits start at F1290
  3.1338 +
  3.1339 +;; note: DADB hyper-potion-hp _ _ _ super-potion-hp _ _ _ potion-hp ??
  3.1340 +
  3.1341 +;; note: DD4D spells out pokemon vital stat names ("speed", etc.)
  3.1342 +
  3.1343 +;; note: 1195C-6A says ABLE#NOT ABLE#, but so does 119C0-119CE.
  3.1344 +;; The first instance is for Machines; the second, for stones.
  3.1345 +
  3.1346 +;; 0x251A (in indexable mem): image decompression routine seems to begin here.
  3.1347 +
  3.1348 +
  3.1349 +(comment 
  3.1350 +
  3.1351 +(def hxc-later
  3.1352 +  "Running this code produces, e.g. hardcoded names NPCs give
  3.1353 +their pokemon. Will sort through it later."
  3.1354 +(print (character-codes->str(take 10000
  3.1355 +                          (drop 0x71597
  3.1356 +                                (rom (root)))))))
  3.1357 +
  3.1358 +(let [dex
  3.1359 +      (partition-by #(= 0x50 %)
  3.1360 +                    (take 2540
  3.1361 +                          (drop 0x40687
  3.1362 +                                (rom (root)))))]
  3.1363 +  (def dex dex)
  3.1364 +  (def hxc-species
  3.1365 +    (map character-codes->str
  3.1366 +         (take-nth 4 dex))))
  3.1367 +)
  3.1368 +
  3.1369 +
  3.1370 +#+end_src
  3.1371 +
  3.1372 +#+results:
  3.1373 +: nil
  3.1374 +