view org/rom.org @ 407:03ade2a04458

Astoundingly, it is not generally possible to convert decimal representations into hexadecimal simply by prepending 0x.
author Dylan Holmes <ocsenave@gmail.com>
date Thu, 12 Apr 2012 22:30:03 -0500
parents 8eb674700f15
children a2319e29205b
line wrap: on
line source
1 #+title: Notes on Deconstructing Pokemon Yellow
2 #+author: Dylan Holmes
3 #+email: rlm@mit.edu
4 #+description:
5 #+keywords:
6 #+SETUPFILE: ../../aurellem/org/setup.org
7 #+INCLUDE: ../../aurellem/org/level-0.org
8 #+BABEL: :exports both :noweb yes :cache no :mkdirp yes
10 # about map headers http://datacrystal.romhacking.net/wiki/Pokemon_Red/Blue:Notes
11 # map headers Yellow http://www.pokecommunity.com/archive/index.php/t-235311.html
12 # pokedollar: U+20B1
13 * Introduction
16 ** COMMENT Getting linguistic data: names, words, etc.
18 Some of the simplest data
21 One of the simplest data structures in the Pok\eacute{} ROM is an
22 unbroken list of strings that either (a) all have a specific length,
23 or (b) are all separated by the same character.
25 Because lots of good data has this format, we'll start by writing a
26 template function to extract it:
28 #+name: hxc-thunks
29 #+begin_src clojure :results silent
30 (defn hxc-thunk
31 "Creates a thunk (nullary fn) that grabs data in a certain region of rom and
32 splits it into a collection by 0x50. If rom is not supplied, uses the
33 original rom data."
34 [start length]
35 (fn self
36 ([rom]
37 (take-nth 2
38 (partition-by #(= % 0x50)
39 (take length
40 (drop start rom)))))
41 ([]
42 (self com.aurellem.gb.gb-driver/original-rom))))
44 (def hxc-thunk-words
45 "Same as hxc-thunk, except it interprets the rom data as characters,
46 returning a collection of strings."
47 (comp
48 (partial comp (partial map character-codes->str))
49 hxc-thunk))
51 #+end_src
54 * Pok\eacute{}mon I
55 ** Names of each species
56 The names of the Pok\eacute{}mon species are stored in
57 ROM@E8000. This name list is interesting, for a number of reasons:
58 - The names are stored in [[ ][internal order]] rather than in the familiar
59 Pok\eacute{}dex order. This seemingly random order probably represents the order in which the authors created or
60 programmed in the Pok\eacute{}mon; it's used throughout the game.
61 - There is enough space allocated for 190 Pok\eacute{}mon. As I
62 understand it, there were originally going to be 190 Pok\eacute{}mon
63 in Generation I, but the creators decided to defer some to
64 Generation II. This explains why many Gen I and Gen II Pok\eacute{}mon
65 have the same aesthetic feel.
66 - The list is pockmarked with random gaps, due to the strange internal
67 ordering
68 and the 39 unused spaces [fn::190 allocated spaces minus 151 true Pok\eacute{}mon]. These missing spaces are filled with the
69 placeholder name =MISSINGNO.= (\ldquo{}Missing number\rdquo{}).
71 Each name is exactly ten letters long; whenever a name would be too short, the extra
72 space is padded with the character 0x50.
74 *** See the data
76 Here you can see the raw data in three stages: in the first stage, we
77 just grab the first few bytes starting from position 0xE8000. In the
78 second stage, we partition the bytes into ten-letter chunks to show you
79 where the names begin and end. In the final stage, we convert each
80 byte into the letter it represents using the =character-codes->str=
81 function. (0x50 is rendered as the symbol \ldquo{} =#= \rdquo{} for
82 ease of reading).
84 #+begin_src clojure :exports both :cache no :results output
85 (ns com.aurellem.gb.hxc
86 (:use (com.aurellem.gb assembly characters gb-driver util mem-util
87 constants))
88 (:import [com.aurellem.gb.gb_driver SaveState]))
91 (println (take 100 (drop 0xE8000 (rom))))
93 (println (partition 10 (take 100 (drop 0xE8000 (rom)))))
95 (println (character-codes->str (take 100 (drop 0xE8000 (rom)))))
98 #+end_src
100 #+results:
101 : (145 135 152 131 142 141 80 80 80 80 138 128 141 134 128 146 138 135 128 141 141 136 131 142 145 128 141 239 80 80 130 139 132 133 128 136 145 152 80 80 146 143 132 128 145 142 150 80 80 80 149 142 139 147 142 145 129 80 80 80 141 136 131 142 138 136 141 134 80 80 146 139 142 150 129 145 142 80 80 80 136 149 152 146 128 148 145 80 80 80 132 151 132 134 134 148 147 142 145 80)
102 : ((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))
103 : RHYDON####KANGASKHANNIDORAN♂##CLEFAIRY##SPEAROW###VOLTORB###NIDOKING##SLOWBRO###IVYSAUR###EXEGGUTOR#
106 *** Automatically grab the data.
108 #+name: pokenames
109 #+begin_src clojure
111 (defn hxc-pokenames-raw
112 "The hardcoded names of the 190 species in memory. List begins at
113 ROM@E8000. Although names in memory are padded with 0x50 to be 10 characters
114 long, these names are stripped of padding. See also, hxc-pokedex-names"
115 ([]
116 (hxc-pokenames-raw com.aurellem.gb.gb-driver/original-rom))
117 ([rom]
118 (let [count-species 190
119 name-length 10]
120 (map character-codes->str
121 (partition name-length
122 (map #(if (= 0x50 %) 0x00 %)
123 (take (* count-species name-length)
124 (drop 0xE8000
125 rom))))))))
126 (def hxc-pokenames
127 (comp
128 (partial map format-name)
129 hxc-pokenames-raw))
134 (defn hxc-pokedex-names
135 "The names of the pokemon in hardcoded pokedex order. List begins at
136 ROM@410B1. See also, hxc-pokenames."
137 ([] (hxc-pokedex-names
138 com.aurellem.gb.gb-driver/original-rom))
139 ([rom]
140 (let [names (hxc-pokenames rom)]
141 (#(mapv %
142 ((comp range count keys) %))
143 (zipmap
144 (take (count names)
145 (drop 0x410b1 rom))
147 names)))))
149 #+end_src
153 ** Generic species information
155 #+name: pokebase
156 #+begin_src clojure
157 (defn hxc-pokemon-base
158 ([] (hxc-pokemon-base com.aurellem.gb.gb-driver/original-rom))
159 ([rom]
160 (let [entry-size 28
162 pokemon (rest (hxc-pokedex-names))
163 pkmn-count (inc(count pokemon))
164 types (apply assoc {}
165 (interleave
166 (range)
167 pkmn-types)) ;;!! softcoded
168 moves (apply assoc {}
169 (interleave
170 (range)
171 (map format-name
172 (hxc-move-names rom))))
173 machines (hxc-machines)
174 ]
175 (zipmap
176 pokemon
177 (map
178 (fn [[n
179 rating-hp
180 rating-atk
181 rating-def
182 rating-speed
183 rating-special
184 type-1
185 type-2
186 rarity
187 rating-xp
188 pic-dimensions ;; tile_width|tile_height (8px/tile)
189 ptr-pic-obverse-1
190 ptr-pic-obverse-2
191 ptr-pic-reverse-1
192 ptr-pic-reverse-2
193 move-1
194 move-2
195 move-3
196 move-4
197 growth-rate
198 &
199 TMs|HMs]]
200 (let
201 [base-moves
202 (mapv moves
203 ((comp
204 ;; since the game uses zero as a delimiter,
205 ;; it must also increment all move indices by 1.
206 ;; heren we decrement to correct this.
207 (partial map dec)
208 (partial take-while (comp not zero?)))
209 [move-1 move-2 move-3 move-4]))
211 types
212 (set (list (types type-1)
213 (types type-2)))
214 TMs|HMs
215 (map
216 (comp
217 (partial map first)
218 (partial remove (comp zero? second)))
219 (split-at
220 50
221 (map vector
222 (rest(range))
223 (reduce concat
224 (map
225 #(take 8
226 (concat (bit-list %)
227 (repeat 0)))
229 TMs|HMs)))))
231 TMs (vec (first TMs|HMs))
232 HMs (take 5 (map (partial + -50) (vec (second TMs|HMs))))
235 ]
238 {:dex# n
239 :base-moves base-moves
240 :types types
241 :TMs TMs
242 :HMs HMs
243 :base-hp rating-hp
244 :base-atk rating-atk
245 :base-def rating-def
246 :base-speed rating-speed
247 :base-special rating-special
248 :o0 pic-dimensions
249 :o1 ptr-pic-obverse-1
250 :o2 ptr-pic-obverse-2
251 }))
253 (partition entry-size
254 (take (* entry-size pkmn-count)
255 (drop 0x383DE
256 rom))))))))
258 #+end_src
261 ** Pok\eacute{}mon evolutions
262 #+name: evolution-header
263 #+begin_src clojure
264 (defn format-evo
265 "Parse a sequence of evolution data, returning a map. First is the
266 method: 0 = end-evolution-data. 1 = level-up, 2 = item, 3 = trade. Next is an item id, if the
267 method of evolution is by item (only stones will actually make pokemon
268 evolve, for some auxillary reason.) Finally, the minimum level for
269 evolution to occur (level 1 means no limit, which is used for trade
270 and item evolutions), followed by the internal id of the pokemon
271 into which to evolve. Hence, level up and trade evolutions are
272 described with 3
273 bytes; item evolutions with four."
274 [coll]
275 (let [method (first coll)]
276 (cond (empty? coll) []
277 (= 0 method) [] ;; just in case
278 (= 1 method) ;; level-up evolution
279 (conj (format-evo (drop 3 coll))
280 {:method :level-up
281 :min-level (nth coll 1)
282 :into (dec (nth coll 2))})
284 (= 2 method) ;; item evolution
285 (conj (format-evo (drop 4 coll))
286 {:method :item
287 :item (dec (nth coll 1))
288 :min-level (nth coll 2)
289 :into (dec (nth coll 3))})
291 (= 3 method) ;; trade evolution
292 (conj (format-evo (drop 3 coll))
293 {:method :trade
294 :min-level (nth coll 1) ;; always 1 for trade.
295 :into (dec (nth coll 2))}))))
298 (defn hxc-ptrs-evolve
299 "A hardcoded collection of 190 pointers to alternating evolution/learnset data,
300 in internal order."
301 ([]
302 (hxc-ptrs-evolve com.aurellem.gb.gb-driver/original-rom))
303 ([rom]
304 (let [
305 pkmn-count (count (hxc-pokenames-raw)) ;; 190
306 ptrs
307 (map (fn [[a b]] (low-high a b))
308 (partition 2
309 (take (* 2 pkmn-count)
310 (drop 0x3b1e5 rom))))]
311 (map (partial + 0x34000) ptrs)
313 )))
314 #+end_src
316 #+name:evolution
317 #+begin_src clojure
319 (defn hxc-evolution
320 "Hardcoded evolution data in memory. The data exists at ROM@34000,
321 sorted by internal order. Pointers to the data exist at ROM@3B1E5; see also, hxc-ptrs-evolve."
322 ([] (hxc-evolution com.aurellem.gb.gb-driver/original-rom))
323 ([rom]
324 (apply assoc {}
325 (interleave
326 (hxc-pokenames rom)
327 (map
328 (comp
329 format-evo
330 (partial take-while (comp not zero?))
331 #(drop % rom))
332 (hxc-ptrs-evolve rom)
333 )))))
335 (defn hxc-evolution-pretty
336 "Like hxc-evolution, except it uses the names of items and pokemon
337 --- grabbed from ROM --- rather than their numerical identifiers."
338 ([] (hxc-evolution-pretty com.aurellem.gb.gb-driver/original-rom))
339 ([rom]
340 (let
341 [poke-names (vec (hxc-pokenames rom))
342 item-names (vec (hxc-items rom))
343 use-names
344 (fn [m]
345 (loop [ks (keys m) new-map m]
346 (let [k (first ks)]
347 (cond (nil? ks) new-map
348 (= k :into)
349 (recur
350 (next ks)
351 (assoc new-map
352 :into
353 (poke-names
354 (:into
355 new-map))))
356 (= k :item)
357 (recur
358 (next ks)
359 (assoc new-map
360 :item
361 (item-names
362 (:item new-map))))
363 :else
364 (recur
365 (next ks)
366 new-map)
367 ))))]
369 (into {}
370 (map (fn [[pkmn evo-coll]]
371 [pkmn (map use-names evo-coll)])
372 (hxc-evolution rom))))))
375 #+end_src
378 ** Level-up moves (learnsets)
379 #+name: learnsets
380 #+begin_src clojure
383 (defn hxc-learnsets
384 "Hardcoded map associating pokemon names to lists of pairs [lvl
385 move] of abilities they learn as they level up. The data
386 exists at ROM@34000, sorted by internal order. Pointers to the data
387 exist at ROM@3B1E5; see also, hxc-ptrs-evolve"
388 ([] (hxc-learnsets com.aurellem.gb.gb-driver/original-rom))
389 ([rom]
390 (apply assoc
391 {}
392 (interleave
393 (hxc-pokenames rom)
394 (map (comp
395 (partial map
396 (fn [[lvl mv]] [lvl (dec mv)]))
397 (partial partition 2)
398 ;; keep the learnset data
399 (partial take-while (comp not zero?))
400 ;; skip the evolution data
401 rest
402 (partial drop-while (comp not zero?)))
403 (map #(drop % rom)
404 (hxc-ptrs-evolve rom)))))))
406 (defn hxc-learnsets-pretty
407 "Live hxc-learnsets except it reports the name of each move --- as
408 it appears in rom --- rather than the move index."
409 ([] (hxc-learnsets-pretty com.aurellem.gb.gb-driver/original-rom))
410 ([rom]
411 (let [moves (vec(map format-name (hxc-move-names)))]
412 (into {}
413 (map (fn [[pkmn learnset]]
414 [pkmn (map (fn [[lvl mv]] [lvl (moves mv)])
415 learnset)])
416 (hxc-learnsets rom))))))
420 #+end_src
424 * Pok\eacute{}mon II : the Pok\eacute{}dex
425 ** Species vital stats
426 #+name: dex-stats
427 #+begin_src clojure
428 (defn hxc-pokedex-stats
429 "The hardcoded pokedex stats (species height weight) in memory. List
430 begins at ROM@40687"
431 ([] (hxc-pokedex-stats com.aurellem.gb.gb-driver/original-rom))
432 ([rom]
433 (let [pokedex-names (zipmap (range) (hxc-pokedex-names rom))
434 pkmn-count (count pokedex-names)
435 ]
436 ((fn capture-stats
437 [n stats data]
438 (if (zero? n) stats
439 (let [[species
440 [_
441 height-ft
442 height-in
443 weight-1
444 weight-2
445 _
446 dex-ptr-1
447 dex-ptr-2
448 dex-bank
449 _
450 & data]]
451 (split-with (partial not= 0x50) data)]
452 (recur (dec n)
453 (assoc stats
454 (pokedex-names (- pkmn-count (dec n)))
455 {:species
456 (format-name (character-codes->str species))
457 :height-ft
458 height-ft
459 :height-in
460 height-in
461 :weight
462 (/ (low-high weight-1 weight-2) 10.)
464 ;; :text
465 ;; (character-codes->str
466 ;; (take-while
467 ;; (partial not= 0x50)
468 ;; (drop
469 ;; (+ 0xB8000
470 ;; -0x4000
471 ;; (low-high dex-ptr-1 dex-ptr-2))
472 ;; rom)))
473 })
475 data)
478 )))
480 pkmn-count
481 {}
482 (drop 0x40687 rom))) ))
483 #+end_src
485 ** Species synopses
487 #+name: dex-text
488 #+begin_src clojure
489 (def hxc-pokedex-text-raw
490 "The hardcoded pokedex entries in memory. List begins at
491 ROM@B8000, shortly before move names."
492 (hxc-thunk-words 0xB8000 14754))
497 (defn hxc-pokedex-text
498 "The hardcoded pokedex entries in memory, presented as an
499 associative hash map. List begins at ROM@B8000."
500 ([] (hxc-pokedex-text com.aurellem.gb.gb-driver/original-rom))
501 ([rom]
502 (zipmap
503 (hxc-pokedex-names rom)
504 (cons nil ;; for missingno.
505 (hxc-pokedex-text-raw rom)))))
506 #+end_src
509 ** Pok\eacute{}mon cries
510 #+name: pokecry
511 #+begin_src clojure
512 (defn hxc-cry
513 "The pokemon cry data in internal order. List begins at ROM@39462"
514 ([](hxc-cry com.aurellem.gb.gb-driver/original-rom))
515 ([rom]
516 (zipmap
517 (hxc-pokenames rom)
518 (map
519 (fn [[cry-id pitch length]]
520 {:cry-id cry-id
521 :pitch pitch
522 :length length}
523 )
524 (partition 3
525 (drop 0x39462 rom))))))
527 (defn hxc-cry-groups
528 ([] (hxc-cry-groups com.aurellem.gb.gb-driver/original-rom))
529 ([rom]
530 (map #(mapv first
531 (filter
532 (fn [[k v]]
533 (= % (:cry-id v)))
534 (hxc-cry)))
535 ((comp
536 range
537 count
538 set
539 (partial map :cry-id)
540 vals
541 hxc-cry)
542 rom))))
545 (defn cry-conversion!
546 "Convert Porygon's cry in ROM to be the cry of the given pokemon."
547 [pkmn]
548 (write-rom!
549 (rewrite-memory
550 (vec(rom))
551 0x3965D
552 (map second
553 ((hxc-cry) pkmn)))))
555 #+end_src
557 ** COMMENT Names of permanent stats
558 0DD4D-DD72
560 * Items
561 ** Item names
563 *** See the data
564 #+begin_src clojure :exports both :results output
565 (ns com.aurellem.gb.hxc
566 (:use (com.aurellem.gb assembly characters gb-driver util mem-util
567 constants))
568 (:import [com.aurellem.gb.gb_driver SaveState]))
570 (println (take 100 (drop 0x045B7 (rom))))
572 (println
573 (partition-by
574 (partial = 0x50)
575 (take 100 (drop 0x045B7 (rom)))))
577 (println
578 (map character-codes->str
579 (partition-by
580 (partial = 0x50)
581 (take 100 (drop 0x045B7 (rom))))))
584 #+end_src
586 #+results:
587 : (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)
588 : ((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))
589 : (MASTER BALL # ULTRA BALL # GREAT BALL # POKé BALL # TOWN MAP # BICYCLE # ????? # SAFARI BALL # POKéDEX # MOON STONE # AN)
591 *** Automatically grab the data
592 #+name: item-names
593 #+begin_src clojure
595 (def hxc-items-raw
596 "The hardcoded names of the items in memory. List begins at
597 ROM@045B7"
598 (hxc-thunk-words 0x45B7 870))
600 (def hxc-items
601 "The hardcoded names of the items in memory, presented as
602 keywords. List begins at ROM@045B7. See also, hxc-items-raw."
603 (comp (partial map format-name) hxc-items-raw))
604 #+end_src
606 ** Item prices
608 ***
609 #+begin_src clojure :exports both :results output
610 (ns com.aurellem.gb.hxc
611 (:use (com.aurellem.gb assembly characters gb-driver util mem-util
612 constants))
613 (:import [com.aurellem.gb.gb_driver SaveState]))
615 (println (take 90 (drop 0x4495 (rom))))
617 (println
618 (partition 3
619 (take 90 (drop 0x4495 (rom)))))
621 (println
622 (partition 3
623 (map hex
624 (take 90 (drop 0x4495 (rom))))))
626 (println
627 (map decode-bcd
628 (map butlast
629 (partition 3
630 (take 90 (drop 0x4495 (rom)))))))
632 (println
633 (map
634 vector
635 (hxc-items (rom))
636 (map decode-bcd
637 (map butlast
638 (partition 3
639 (take 90 (drop 0x4495 (rom))))))))
645 #+end_src
647 #+results:
648 : (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)
649 : ((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))
650 : ((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))
651 : (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)
652 : ([: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])
655 ***
656 #+name: item-prices
657 #+begin_src clojure
658 (defn hxc-item-prices
659 "The hardcoded list of item prices in memory. List begins at ROM@4495"
660 ([] (hxc-item-prices com.aurellem.gb.gb-driver/original-rom))
661 ([rom]
662 (let [items (hxc-items rom)
663 price-size 3]
664 (zipmap items
665 (map (comp
666 ;; zero-cost items are "priceless"
667 #(if (zero? %) :priceless %)
668 decode-bcd butlast)
669 (partition price-size
670 (take (* price-size (count items))
671 (drop 0x4495 rom))))))))
672 #+end_src
673 ** Vendor inventories
675 #+name: item-vendors
676 #+begin_src clojure
677 (defn hxc-shops
678 ([] (hxc-shops com.aurellem.gb.gb-driver/original-rom))
679 ([rom]
680 (let [items (zipmap (range) (hxc-items rom))
682 ;; temporarily softcode the TM items
683 items (into
684 items
685 (map (juxt identity
686 (comp keyword
687 (partial str "tm-")
688 (partial + 1 -200)
689 ))
690 (take 200 (drop 200 (range)))))
692 ]
694 ((fn parse-shop [coll [num-items & items-etc]]
695 (let [inventory (take-while
696 (partial not= 0xFF)
697 items-etc)
698 [separator & items-etc] (drop num-items (rest items-etc))]
699 (if (= separator 0x50)
700 (map (partial mapv (comp items dec)) (conj coll inventory))
701 (recur (conj coll inventory) items-etc)
702 )
703 ))
705 '()
706 (drop 0x233C rom))
709 )))
710 #+end_src
712 #+results: item-vendors
713 : #'com.aurellem.gb.hxc/hxc-shops
717 * Types
718 ** Names of types
720 *** COMMENT Pointers to type names
721 #+begin_src clojure :exports both :results output
722 (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)))))
723 #+end_src
726 ***
727 #+begin_src clojure :exports both :results output
728 (ns com.aurellem.gb.hxc
729 (:use (com.aurellem.gb assembly characters gb-driver util mem-util
730 constants))
731 (:import [com.aurellem.gb.gb_driver SaveState]))
733 (println (take 90 (drop 0x27D99 (rom))))
735 (println
736 (partition-by (partial = 0x50)
737 (take 90 (drop 0x27D99 (rom)))))
739 (println
740 (map character-codes->str
741 (partition-by (partial = 0x50)
742 (take 90 (drop 0x27D99 (rom))))))
744 #+end_src
746 #+results:
747 : (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)
748 : ((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))
749 : (NORMAL # FIGHTING # FLYING # POISON # FIRE # WATER # GRASS # ELECTRIC # PSYCHIC # ICE # GROUND # ROCK # BIRD # BUG # G)
752 ***
753 #+name: type-names
754 #+begin_src clojure
755 (def hxc-types
756 "The hardcoded type names in memory. List begins at ROM@27D99,
757 shortly before hxc-titles."
758 (hxc-thunk-words 0x27D99 102))
760 #+end_src
762 ** Type effectiveness
763 ***
764 #+begin_src clojure :exports both :results output
765 (ns com.aurellem.gb.hxc
766 (:use (com.aurellem.gb assembly characters gb-driver util mem-util
767 constants))
768 (:import [com.aurellem.gb.gb_driver SaveState]))
771 ;; POKEMON TYPES
773 (println pkmn-types) ;; these are the pokemon types
774 (println (map vector (range) pkmn-types)) ;; each type has an id number.
776 (newline)
781 ;;; TYPE EFFECTIVENESS
783 (println (take 15 (drop 0x3E62D (rom))))
784 (println (partition 3 (take 15 (drop 0x3E62D (rom)))))
786 (println
787 (map
788 (fn [[atk-type def-type multiplier]]
789 (list atk-type def-type (/ multiplier 10.)))
791 (partition 3
792 (take 15 (drop 0x3E62D (rom))))))
795 (println
796 (map
797 (fn [[atk-type def-type multiplier]]
798 [
799 (get pkmn-types atk-type)
800 (get pkmn-types def-type)
801 (/ multiplier 10.)
802 ])
804 (partition 3
805 (take 15 (drop 0x3E62D (rom))))))
807 #+end_src
809 #+results:
810 : [: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]
811 : ([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])
812 :
813 : (0 5 5 0 8 0 8 8 20 20 7 20 20 5 5)
814 : ((0 5 5) (0 8 0) (8 8 20) (20 7 20) (20 5 5))
815 : ((0 5 0.5) (0 8 0.0) (8 8 2.0) (20 7 2.0) (20 5 0.5))
816 : ([:normal :rock 0.5] [:normal :ghost 0.0] [:ghost :ghost 2.0] [:fire :bug 2.0] [:fire :rock 0.5])
819 ***
821 #+name: type-advantage
822 #+begin_src clojure
823 (defn hxc-advantage
824 ;; in-game multipliers are stored as 10x their effective value
825 ;; to allow for fractional multipliers like 1/2
827 "The hardcoded type advantages in memory, returned as tuples of
828 atk-type def-type multiplier. By default (i.e. if not listed here),
829 the multiplier is 1. List begins at 0x3E62D."
830 ([] (hxc-advantage com.aurellem.gb.gb-driver/original-rom))
831 ([rom]
832 (map
833 (fn [[atk def mult]] [(get pkmn-types atk (hex atk))
834 (get pkmn-types def (hex def))
835 (/ mult 10)])
836 (partition 3
837 (take-while (partial not= 0xFF)
838 (drop 0x3E62D rom))))))
839 #+end_src
843 * Moves
844 ** Names of moves
845 *** See the data
846 #+begin_src clojure :exports both :results output
847 (ns com.aurellem.gb.hxc
848 (:use (com.aurellem.gb assembly characters gb-driver util mem-util
849 constants))
850 (:import [com.aurellem.gb.gb_driver SaveState]))
852 (println (take 100 (drop 0xBC000 (rom))))
854 (println
855 (partition-by
856 (partial = 0x50)
857 (take 100 (drop 0xBC000 (rom)))))
859 (println
860 (map character-codes->str
861 (partition-by
862 (partial = 0x50)
863 (take 100 (drop 0xBC000 (rom))))))
866 #+end_src
868 #+results:
869 : (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)
870 : ((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))
871 : (POUND # KARATE CHOP # DOUBLESLAP # COMET PUNCH # MEGA PUNCH # PAY DAY # FIRE PUNCH # ICE PUNCH # THUNDERPUNCH # SCRATC)
873 *** Automatically grab the data
875 #+name: move-names
876 #+begin_src clojure
877 (def hxc-move-names
878 "The hardcoded move names in memory. List begins at ROM@BC000"
879 (hxc-thunk-words 0xBC000 1551))
880 #+end_src
882 ** Properties of moves
884 #+name: move-data
885 #+begin_src clojure
886 (defn hxc-move-data
887 "The hardcoded (basic (move effects)) in memory. List begins at
888 0x38000. Returns a map of {:name :power :accuracy :pp :fx-id
889 :fx-txt}. The move descriptions are handwritten, not hardcoded."
890 ([]
891 (hxc-move-data com.aurellem.gb.gb-driver/original-rom))
892 ([rom]
893 (let [names (vec (hxc-move-names rom))
894 move-count (count names)
895 move-size 6
896 types pkmn-types ;;; !! hardcoded types
897 ]
898 (zipmap (map format-name names)
899 (map
900 (fn [[idx effect power type-id accuracy pp]]
901 {:name (names (dec idx))
902 :power power
903 :accuracy accuracy
904 :pp pp
905 :type (types type-id)
906 :fx-id effect
907 :fx-txt (get move-effects effect)
908 }
909 )
911 (partition move-size
912 (take (* move-size move-count)
913 (drop 0x38000 rom))))))))
917 (defn hxc-move-data*
918 "Like hxc-move-data, but reports numbers as hexadecimal symbols instead."
919 ([]
920 (hxc-move-data* com.aurellem.gb.gb-driver/original-rom))
921 ([rom]
922 (let [names (vec (hxc-move-names rom))
923 move-count (count names)
924 move-size 6
925 format-name (fn [s]
926 (keyword (.toLowerCase
927 (apply str
928 (map #(if (= % \space) "-" %) s)))))
929 ]
930 (zipmap (map format-name names)
931 (map
932 (fn [[idx effect power type accuracy pp]]
933 {:name (names (dec idx))
934 :power power
935 :accuracy (hex accuracy)
936 :pp pp
937 :fx-id (hex effect)
938 :fx-txt (get move-effects effect)
939 }
940 )
942 (partition move-size
943 (take (* move-size move-count)
944 (drop 0x38000 rom))))))))
946 #+end_src
948 ** TM and HM moves
949 ***
950 #+begin_src clojure :exports both :results output
951 (ns com.aurellem.gb.hxc
952 (:use (com.aurellem.gb assembly characters gb-driver util mem-util
953 constants))
954 (:import [com.aurellem.gb.gb_driver SaveState]))
957 (println (hxc-move-names))
958 (println (map vector (rest(range)) (hxc-move-names)))
960 (newline)
962 (println (take 55 (drop 0x1232D (rom))))
964 (println
965 (interpose "."
966 (map
967 (zipmap (rest (range)) (hxc-move-names))
968 (take 55 (drop 0x1232D (rom))))))
970 #+end_src
972 #+results:
973 : (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)
974 : ([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])
975 :
976 : (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)
977 : (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)
980 ***
981 #+name: machines
982 #+begin_src clojure
983 (defn hxc-machines
984 "The hardcoded moves taught by TMs and HMs. List begins at ROM@1232D."
985 ([] (hxc-machines
986 com.aurellem.gb.gb-driver/original-rom))
987 ([rom]
988 (let [moves (hxc-move-names rom)]
989 (zipmap
990 (range)
991 (take-while
992 (comp not nil?)
993 (map (comp
994 format-name
995 (zipmap
996 (range)
997 moves)
998 dec)
999 (take 100
1000 (drop 0x1232D rom))))))))
1002 #+end_src
1008 ** COMMENT Status ailments
1010 * Places
1011 ** Names of places
1013 #+name: places
1014 #+begin_src clojure
1015 (def hxc-places
1016 "The hardcoded place names in memory. List begins at
1017 ROM@71500. [Cinnabar] Mansion seems to be dynamically calculated."
1018 (hxc-thunk-words 0x71500 560))
1020 #+end_src
1022 ** Wild Pok\eacute{}mon demographics
1023 #+name: wilds
1024 #+begin_src clojure
1028 (defn hxc-ptrs-wild
1029 "A list of the hardcoded wild encounter data in memory. Pointers
1030 begin at ROM@0CB95; data begins at ROM@0x04D89"
1031 ([] (hxc-ptrs-wild com.aurellem.gb.gb-driver/original-rom))
1032 ([rom]
1033 (let [ptrs
1034 (map (fn [[a b]] (+ a (* 0x100 b)))
1035 (take-while (partial not= (list 0xFF 0xFF))
1036 (partition 2 (drop 0xCB95 rom))))]
1037 ptrs)))
1041 (defn hxc-wilds
1042 "A list of the hardcoded wild encounter data in memory. Pointers
1043 begin at ROM@0CB95; data begins at ROM@0x04D89"
1044 ([] (hxc-wilds com.aurellem.gb.gb-driver/original-rom))
1045 ([rom]
1046 (let [pokenames (zipmap (range) (hxc-pokenames rom))]
1047 (map
1048 (partial map (fn [[a b]] {:species (pokenames (dec b)) :level
1049 a}))
1050 (partition 10
1052 (take-while (comp (partial not= 1)
1053 first)
1054 (partition 2
1055 (drop 0xCD8C rom))
1057 ))))))
1059 #+end_src
1065 * Appendices
1069 ** Mapping the ROM
1071 | ROM address (hex) | Description | Format | Example |
1072 |-------------------+-----------------+-----------------+-----------------|
1073 | | <15> | <15> | <15> |
1074 | 0233C- | Shop inventories. | | |
1075 | 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. |
1076 | 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#... |
1077 | 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). |
1078 | 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. |
1079 | 06698- | ? Background music. | | |
1080 | 0822E-082F? | Pointers to background music, part I. | | |
1081 | 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). |
1082 |-------------------+-----------------+-----------------+-----------------|
1083 | 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 |
1084 | 0DAE0. | Amount of HP restored by Super Potion. | " | 50 |
1085 | 0DAE3. | Amount of HP restored by Potion. | " | 20 |
1086 |-------------------+-----------------+-----------------+-----------------|
1087 | 0DD4D-DD72 | Names of permanent stats. | Variable-length strings separated by 0x50. | #HEALTH#ATTACK#DEFENSE#SPEED#SPECIAL# |
1088 | 1195C-1196A | The two terms for being able/unable to learn a TM/HM. | Variable-length strings separated by 0x50. | ABLE#NOT ABLE# |
1089 | 119C0-119CE | The two terms for being able/unable to evolve using the current stone. | Variable-length strings separated by 0x50. | ABLE#NOT ABLE# |
1090 | 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), ... |
1091 |-------------------+-----------------+-----------------+-----------------|
1092 | 27D56 & 27D57. | Pointer to the list of type pointers. | | |
1093 | 27D63-27D99 | Pointers to type names. | Each point is a low-byte, high-byte pair. The names of types follows immediately after this section; see below. | The first pointer is [0x99 0x7D], corresponding to the location 0x2.7D.99, the NORMAL type. |
1094 | 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#... |
1095 | 27E77- | Trainer title names. | Variable-length names separated by 0x50. | YOUNGSTER#BUG CATCHER#LASS#... |
1096 | 34000- | | | |
1097 | 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. |
1098 | 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. | | |
1099 | 39462- | The Pok\eacute{}mon cry data. | Fixed-length (3 byte) descriptions of cries. | |
1100 | 3B1E5- | Pointers to evolution/learnset data. | | |
1101 | 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. | |
1102 | 40687- | Species data from the Pok\eacute{}dex: species name, height, weight, etc. | Fixed-length sequences of bytes. See below for specifics. | |
1103 | 410B1- | A conversion table between internal order and Pokedex order. | | |
1104 | 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]... |
1105 | 71500- | Names of places. | | |
1106 | 7C249-7C2?? | Pointers to background music, pt II. | | |
1107 | 98000- | Dialogue | | |
1108 | B8000- | The text of each Pokemon's Pok\eacute{}dex entry. | | |
1109 | BC000-BC60E | Move names. | Variable-length move names, separated by 0x50. The moves are in internal order. | POUND#KARATE CHOP#DOUBLESLAP#COMET PUNCH#... |
1110 | 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♂#... |
1111 | | | | |
1112 | | | | |
1116 ** Internal Pok\eacute{}mon IDs
1117 ** Type IDs
1119 #+name: type-ids
1120 #+begin_src clojure
1121 (def pkmn-types
1122 [:normal ;;0
1123 :fighting ;;1
1124 :flying ;;2
1125 :poison ;;3
1126 :ground ;;4
1127 :rock ;;5
1128 :bird ;;6
1129 :bug ;;7
1130 :ghost ;;8
1131 :A
1132 :B
1133 :C
1134 :D
1135 :E
1136 :F
1137 :G
1138 :H
1139 :I
1140 :J
1141 :K
1142 :fire ;;20 (0x14)
1143 :water ;;21 (0x15)
1144 :grass ;;22 (0x16)
1145 :electric ;;23 (0x17)
1146 :psychic ;;24 (0x18)
1147 :ice ;;25 (0x19)
1148 :dragon ;;26 (0x1A)
1149 ])
1150 #+end_src
1152 ** Basic effects of moves
1154 *** Table of basic effects
1156 The possible effects of moves in Pok\eacute{}mon \mdash{} for example, dealing
1157 damage, leeching health, or potentially poisoning the opponent
1158 \mdash{} are stored in a table. Each move has exactly one effect, and
1159 different moves might have the same effect.
1161 For example, Leech Life, Mega Drain, and Absorb all have effect ID #3, which is \ldquo{}Leech half of the inflicted damage.\rdquo{}
1163 All the legitimate move effects are listed in the table
1164 below. Here are some notes for reading it:
1166 - Whenever an effect has a chance of doing something (like a chance of
1167 poisoning the opponent), I list the chance as a hexadecimal amount
1168 out of 256; this is to avoid rounding errors. To convert the hex amount into a percentage, divide by 256.
1169 - For some effects, the description is too cumbersome to
1170 write. Instead, I just write a move name
1171 in parentheses, like: (leech seed). That move gives a characteristic example
1172 of the effect.
1173 - I use the abbreviations =atk=, =def=, =spd=, =spc=, =acr=, =evd= for
1174 attack, defense, speed, special, accuracy, and evasiveness.
1179 | ID (hex) | Description | Notes |
1180 |----------+-------------------------------------------------------------------------------------------------+------------------------------------------------------------------|
1181 | 0 | normal damage | |
1182 | 1 | no damage, just sleep | TODO: find out how many turns |
1183 | 2 | 0x4C chance of poison | |
1184 | 3 | leech half of inflicted damage | |
1185 | 4 | 0x19 chance of burn | |
1186 | 5 | 0x19 chance of freeze | |
1187 | 6 | 0x19 chance of paralysis | |
1188 | 7 | user faints; opponent's defense is halved during attack. | |
1189 | 8 | leech half of inflicted damage ONLY if the opponent is asleep | |
1190 | 9 | imitate last attack | |
1191 | A | user atk +1 | |
1192 | B | user def +1 | |
1193 | C | user spd +1 | |
1194 | D | user spc +1 | |
1195 | E | user acr +1 | This effect is unused. |
1196 | F | user evd +1 | |
1197 | 10 | get post-battle money = 2 * level * uses | |
1198 | 11 | move has 0xFE acr, regardless of battle stat modifications. | |
1199 | 12 | opponent atk -1 | |
1200 | 13 | opponent def -1 | |
1201 | 14 | opponent spd -1 | |
1202 | 15 | opponent spc -1 | |
1203 | 16 | opponent acr -1 | |
1204 | 17 | opponent evd -1 | |
1205 | 18 | converts user's type to opponent's. | |
1206 | 19 | (haze) | |
1207 | 1A | (bide) | |
1208 | 1B | (thrash) | |
1209 | 1C | (teleport) | |
1210 | 1D | (fury swipes) | |
1211 | 1E | attacks 2-5 turns | Unused. TODO: find out what it does. |
1212 | 1F | 0x19 chance of flinching | |
1213 | 20 | opponent sleep for 1-7 turns | |
1214 | 21 | 0x66 chance of poison | |
1215 | 22 | 0x4D chance of burn | |
1216 | 23 | 0x4D chance of freeze | |
1217 | 24 | 0x4D chance of paralysis | |
1218 | 25 | 0x4D chance of flinching | |
1219 | 26 | one-hit KO | |
1220 | 27 | charge one turn, atk next. | |
1221 | 28 | fixed damage, leaves 1HP. | Is the fixed damage the power of the move? |
1222 | 29 | fixed damage. | Like seismic toss, dragon rage, psywave. |
1223 | 2A | atk 2-5 turns; opponent can't attack | The odds of attacking for /n/ turns are: (0 0x60 0x60 0x20 0x20) |
1224 | 2B | charge one turn, atk next. (can't be hit when charging) | |
1225 | 2C | atk hits twice. | |
1226 | 2D | user takes 1 damage if misses. | |
1227 | 2E | evade status-lowering effects | Caused by you or also your opponent? |
1228 | 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. |
1229 | 30 | atk causes recoil dmg = 1/4 dmg dealt | |
1230 | 31 | confuses opponent | |
1231 | 32 | user atk +2 | |
1232 | 33 | user def +2 | |
1233 | 34 | user spd +2 | |
1234 | 35 | user spc +2 | |
1235 | 36 | user acr +2 | This effect is unused. |
1236 | 37 | user evd +2 | This effect is unused. |
1237 | 38 | restores up to half of user's max hp. | |
1238 | 39 | (transform) | |
1239 | 3A | opponent atk -2 | |
1240 | 3B | opponent def -2 | |
1241 | 3C | opponent spd -2 | |
1242 | 3D | opponent spc -2 | |
1243 | 3E | opponent acr -2 | |
1244 | 3F | opponent evd -2 | |
1245 | 40 | doubles user spc when attacked | |
1246 | 41 | doubles user def when attacked | |
1247 | 42 | just poisons opponent | |
1248 | 43 | just paralyzes opponent | |
1249 | 44 | 0x19 chance opponent atk -1 | |
1250 | 45 | 0x19 chance opponent def -1 | |
1251 | 46 | 0x19 chance opponent spd -1 | |
1252 | 47 | 0x4C chance opponent spc -1 | |
1253 | 48 | 0x19 chance opponent acr -1 | |
1254 | 49 | 0x19 chance opponent evd -1 | |
1255 | 4A | ??? | ;; unused? no effect? |
1256 | 4B | ??? | ;; unused? no effect? |
1257 | 4C | 0x19 chance of confusing the opponent | |
1258 | 4D | atk hits twice. 0x33 chance opponent poisioned. | |
1259 | 4E | broken. crash the game after attack. | |
1260 | 4F | (substitute) | |
1261 | 50 | unless opponent faints, user must recharge after atk. some exceptions apply | |
1262 | 51 | (rage) | |
1263 | 52 | (mimic) | |
1264 | 53 | (metronome) | |
1265 | 54 | (leech seed) | |
1266 | 55 | does nothing (splash) | |
1267 | 56 | (disable) | |
1268 #+end_src
1270 *** Source
1271 #+name: move-effects
1272 #+begin_src clojure
1273 (def move-effects
1274 ["normal damage"
1275 "no damage, just opponent sleep" ;; how many turns? is atk power ignored?
1276 "0x4C chance of poison"
1277 "leech half of inflicted damage"
1278 "0x19 chance of burn"
1279 "0x19 chance of freeze"
1280 "0x19 chance of paralyze"
1281 "user faints; opponent defense halved during attack."
1282 "leech half of inflicted damage ONLY if sleeping opponent."
1283 "imitate last attack"
1284 "user atk +1"
1285 "user def +1"
1286 "user spd +1"
1287 "user spc +1"
1288 "user acr +1" ;; unused?!
1289 "user evd +1"
1290 "get post-battle $ = 2*level*uses"
1291 "0xFE acr, no matter what."
1292 "opponent atk -1" ;; acr taken from move acr?
1293 "opponent def -1" ;;
1294 "opponent spd -1" ;;
1295 "opponent spc -1" ;;
1296 "opponent acr -1";;
1297 "opponent evd -1"
1298 "converts user's type to opponent's."
1299 "(haze)"
1300 "(bide)"
1301 "(thrash)"
1302 "(teleport)"
1303 "(fury swipes)"
1304 "attacks 2-5 turns" ;; unused? like rollout?
1305 "0x19 chance of flinch"
1306 "opponent sleep for 1-7 turns"
1307 "0x66 chance of poison"
1308 "0x4D chance of burn"
1309 "0x4D chance of freeze"
1310 "0x4D chance of paralyze"
1311 "0x4D chance of flinch"
1312 "one-hit KO"
1313 "charge one turn, atk next."
1314 "fixed damage, leaves 1HP." ;; how is dmg determined?
1315 "fixed damage." ;; cf seismic toss, dragon rage, psywave.
1316 "atk 2-5 turns; opponent can't attack" ;; unnormalized? (0 0x60 0x60 0x20 0x20)
1317 "charge one turn, atk next. (can't be hit when charging)"
1318 "atk hits twice."
1319 "user takes 1 damage if misses."
1320 "evade status-lowering effects" ;;caused by you or also your opponent?
1321 "(broken) if user is slower than opponent, makes critical hit impossible, otherwise has no effect"
1322 "atk causes recoil dmg = 1/4 dmg dealt"
1323 "confuses opponent" ;; acr taken from move acr
1324 "user atk +2"
1325 "user def +2"
1326 "user spd +2"
1327 "user spc +2"
1328 "user acr +2" ;; unused!
1329 "user evd +2" ;; unused!
1330 "restores up to half of user's max hp." ;; broken: fails if the difference
1331 ;; b/w max and current hp is one less than a multiple of 256.
1332 "(transform)"
1333 "opponent atk -2"
1334 "opponent def -2"
1335 "opponent spd -2"
1336 "opponent spc -2"
1337 "opponent acr -2"
1338 "opponent evd -2"
1339 "doubles user spc when attacked"
1340 "doubles user def when attacked"
1341 "just poisons opponent" ;;acr taken from move acr
1342 "just paralyzes opponent" ;;
1343 "0x19 chance opponent atk -1"
1344 "0x19 chance opponent def -1"
1345 "0x19 chance opponent spd -1"
1346 "0x4C chance opponent spc -1" ;; context suggest chance is 0x19
1347 "0x19 chance opponent acr -1"
1348 "0x19 chance opponent evd -1"
1349 "???" ;; unused? no effect?
1350 "???" ;; unused? no effect?
1351 "0x19 chance opponent confused"
1352 "atk hits twice. 0x33 chance opponent poisioned."
1353 "broken. crash the game after attack."
1354 "(substitute)"
1355 "unless opponent faints, user must recharge after atk. some
1356 exceptions apply."
1357 "(rage)"
1358 "(mimic)"
1359 "(metronome)"
1360 "(leech seed)"
1361 "does nothing (splash)"
1362 "(disable)"
1363 ])
1364 #+end_src
1367 ** Alphabet code
1369 * Source
1371 #+begin_src clojure :tangle ../clojure/com/aurellem/gb/hxc.clj
1373 (ns com.aurellem.gb.hxc
1374 (:use (com.aurellem.gb assembly characters gb-driver util mem-util
1375 constants species))
1376 (:import [com.aurellem.gb.gb_driver SaveState]))
1378 ; ************* HANDWRITTEN CONSTANTS
1380 <<type-ids>>
1383 ;; question: when status effects claim to take
1384 ;; their accuracy from the move accuracy, does
1385 ;; this mean that the move always "hits" but the
1386 ;; status effect may not?
1388 <<move-effects>>
1390 ;; ************** HARDCODED DATA
1392 <<hxc-thunks>>
1393 ;; --------------------------------------------------
1395 <<pokenames>>
1396 <<type-names>>
1398 ;; http://hax.iimarck.us/topic/581/
1399 <<pokecry>>
1402 <<item-names>>
1406 (def hxc-titles
1407 "The hardcoded names of the trainer titles in memory. List begins at
1408 ROM@27E77"
1409 (hxc-thunk-words 0x27E77 196))
1412 <<dex-text>>
1414 ;; In red/blue, pokedex stats are in internal order.
1415 ;; In yellow, pokedex stats are in pokedex order.
1416 <<dex-stats>>
1421 <<places>>
1423 (defn hxc-dialog
1424 "The hardcoded dialogue in memory, including in-game alerts. Dialog
1425 seems to be separated by 0x57 instead of 0x50 (END). Begins at ROM@98000."
1426 ([rom]
1427 (map character-codes->str
1428 (take-nth 2
1429 (partition-by #(= % 0x57)
1430 (take 0x0F728
1431 (drop 0x98000 rom))))))
1432 ([]
1433 (hxc-dialog com.aurellem.gb.gb-driver/original-rom)))
1436 <<move-names>>
1437 <<move-data>>
1439 <<machines>>
1443 (defn internal-id
1444 ([rom]
1445 (zipmap
1446 (hxc-pokenames rom)
1447 (range)))
1448 ([]
1449 (internal-id com.aurellem.gb.gb-driver/original-rom)))
1455 ;; nidoran gender change upon levelup
1456 ;; (->
1457 ;; @current-state
1458 ;; rom
1459 ;; vec
1460 ;; (rewrite-memory
1461 ;; (nth (hxc-ptrs-evolve) ((internal-id) :nidoran♂))
1462 ;; [1 1 15])
1463 ;; (rewrite-memory
1464 ;; (nth (hxc-ptrs-evolve) ((internal-id) :nidoran♀))
1465 ;; [1 1 3])
1466 ;; (write-rom!)
1468 ;; )
1472 <<type-advantage>>
1476 <<evolution-header>>
1477 <<evolution>>
1478 <<learnsets>>
1479 <<pokebase>>
1482 (defn hxc-intro-pkmn
1483 "The hardcoded pokemon to display in Prof. Oak's introduction; the pokemon's
1484 internal id is stored at ROM@5EDB."
1485 ([] (hxc-intro-pkmn
1486 com.aurellem.gb.gb-driver/original-rom))
1487 ([rom]
1488 (nth (hxc-pokenames rom) (nth rom 0x5EDB))))
1490 (defn sxc-intro-pkmn!
1491 "Set the hardcoded pokemon to display in Prof. Oak's introduction."
1492 [pokemon]
1493 (write-rom!
1494 (rewrite-rom 0x5EDB
1496 (inc
1497 ((zipmap
1498 (hxc-pokenames)
1499 (range))
1500 pokemon))])))
1503 <<item-prices>>
1505 <<item-vendors>>
1507 <<wilds>>
1510 ;; ********************** MANIPULATION FNS
1513 (defn same-type
1514 ([pkmn move]
1515 (same-type
1516 com.aurellem.gb.gb-driver/original-rom pkmn move))
1517 ([rom pkmn move]
1518 (((comp :types (hxc-pokemon-base rom)) pkmn)
1519 ((comp :type (hxc-move-data rom)) move))))
1524 (defn submap?
1525 "Compares the two maps. Returns true if map-big has the same associations as map-small, otherwise false."
1526 [map-small map-big]
1527 (cond (empty? map-small) true
1528 (and
1529 (contains? map-big (ffirst map-small))
1530 (= (get map-big (ffirst map-small))
1531 (second (first map-small))))
1532 (recur (next map-small) map-big)
1534 :else false))
1537 (defn search-map [proto-map maps]
1538 "Returns all the maps that make the same associations as proto-map."
1539 (some (partial submap? proto-map) maps))
1541 (defn filter-vals
1542 "Returns a map consisting of all the pairs [key val] for
1543 which (pred key) returns true."
1544 [pred map]
1545 (reduce (partial apply assoc) {}
1546 (filter (fn [[k v]] (pred v)) map)))
1549 (defn search-moves
1550 "Returns a subcollection of all hardcoded moves with the
1551 given attributes. Attributes consist of :name :power
1552 :accuracy :pp :fx-id
1553 (and also :fx-txt, but it contains the same information
1554 as :fx-id)"
1555 ([attribute-map]
1556 (search-moves
1557 com.aurellem.gb.gb-driver/original-rom attribute-map))
1558 ([rom attribute-map]
1559 (filter-vals (partial submap? attribute-map)
1560 (hxc-move-data rom))))
1566 ;; note: 0x2f31 contains the names "TM" "HM"?
1568 ;; note for later: credits start at F1290
1570 ;; note: DADB hyper-potion-hp _ _ _ super-potion-hp _ _ _ potion-hp ??
1572 ;; note: DD4D spells out pokemon vital stat names ("speed", etc.)
1574 ;; note: 1195C-6A says ABLE#NOT ABLE#, but so does 119C0-119CE.
1575 ;; The first instance is for Machines; the second, for stones.
1577 ;; 0x251A (in indexable mem): image decompression routine seems to begin here.
1580 ;; Note: There are two tile tables, one from 8000-8FFF, the other from
1581 ;; 8800-97FF. The latter contains symbols, possibly map tiles(?), with some japanese chars and stuff at the end.
1582 (defn print-pixel-letters!
1583 "The pixel tiles representing letters. Neat!"
1584 ([] (print-pixel-letters! (read-state "oak-speaks")))
1585 ([state]
1586 (map
1587 (comp
1588 println
1589 (partial map #(if (zero? %) \space 0))
1590 #(if (< (count %) 8)
1591 (recur (cons 0 %))
1592 %)
1593 reverse bit-list)
1595 (take 0xFFF (drop 0x8800 (memory state))))))
1598 (defn test-2 []
1599 (loop [n 0
1600 pc-1 (pc-trail (-> state-defend (tick) (step [:a]) (step [:a]) (step []) (nstep 100)) 100000)
1601 pc-2 (pc-trail (-> state-speed (tick) (step [:a]) (step [:a])
1602 (step []) (nstep 100)) 100000)]
1603 (cond (empty? (drop n pc-1)) [pc-1 n]
1604 (not= (take 10 (drop n pc-1)) (take 10 pc-2))
1605 (recur pc-1 pc-2 (inc n))
1606 :else
1607 [(take 1000 pc-2) n])))
1615 (comment
1617 (def hxc-later
1618 "Running this code produces, e.g. hardcoded names NPCs give
1619 their pokemon. Will sort through it later."
1620 (print (character-codes->str(take 10000
1621 (drop 0x71597
1622 (rom (root)))))))
1624 (let [dex
1625 (partition-by #(= 0x50 %)
1626 (take 2540
1627 (drop 0x40687
1628 (rom (root)))))]
1629 (def dex dex)
1630 (def hxc-species
1631 (map character-codes->str
1632 (take-nth 4 dex))))
1636 #+end_src
1638 #+results:
1639 : nil