view org/rom.org @ 357:f8b374397426

got elemental stones; floor four complete.
author Robert McIntyre <rlm@mit.edu>
date Mon, 09 Apr 2012 07:20:41 -0500
parents 37616a97beaa
children b477970d0b7a
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 it 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]))
90 (println (take 100 (drop 0xE8000 (rom))))
92 (println (partition 10 (take 100 (drop 0xE8000 (rom)))))
94 (println (character-codes->str (take 100 (drop 0xE8000 (rom)))))
97 #+end_src
99 #+results:
100 : (145 135 152 131 142 141 80 80 80 80 138 128 141 134 128 146 138 135 128 141 141 136 131 142 145 128 141 239 80 80 130 139 132 133 128 136 145 152 80 80 146 143 132 128 145 142 150 80 80 80 149 142 139 147 142 145 129 80 80 80 141 136 131 142 138 136 141 134 80 80 146 139 142 150 129 145 142 80 80 80 136 149 152 146 128 148 145 80 80 80 132 151 132 134 134 148 147 142 145 80)
101 : ((145 135 152 131 142 141 80 80 80 80) (138 128 141 134 128 146 138 135 128 141) (141 136 131 142 145 128 141 239 80 80) (130 139 132 133 128 136 145 152 80 80) (146 143 132 128 145 142 150 80 80 80) (149 142 139 147 142 145 129 80 80 80) (141 136 131 142 138 136 141 134 80 80) (146 139 142 150 129 145 142 80 80 80) (136 149 152 146 128 148 145 80 80 80) (132 151 132 134 134 148 147 142 145 80))
102 : RHYDON####KANGASKHANNIDORAN♂##CLEFAIRY##SPEAROW###VOLTORB###NIDOKING##SLOWBRO###IVYSAUR###EXEGGUTOR#
105 *** Automatically grab the data.
107 #+name: pokenames
108 #+begin_src clojure
110 (defn hxc-pokenames-raw
111 "The hardcoded names of the 190 species in memory. List begins at
112 ROM@E8000. Although names in memory are padded with 0x50 to be 10 characters
113 long, these names are stripped of padding. See also, hxc-pokedex-names"
114 ([]
115 (hxc-pokenames-raw com.aurellem.gb.gb-driver/original-rom))
116 ([rom]
117 (let [count-species 190
118 name-length 10]
119 (map character-codes->str
120 (partition name-length
121 (map #(if (= 0x50 %) 0x00 %)
122 (take (* count-species name-length)
123 (drop 0xE8000
124 rom))))))))
125 (def hxc-pokenames
126 (comp
127 (partial map format-name)
128 hxc-pokenames-raw))
133 (defn hxc-pokedex-names
134 "The names of the pokemon in hardcoded pokedex order. List begins at
135 ROM@410B1. See also, hxc-pokenames."
136 ([] (hxc-pokedex-names
137 com.aurellem.gb.gb-driver/original-rom))
138 ([rom]
139 (let [names (hxc-pokenames rom)]
140 (#(mapv %
141 ((comp range count keys) %))
142 (zipmap
143 (take (count names)
144 (drop 0x410b1 rom))
146 names)))))
148 #+end_src
152 ** Generic species information
154 #+name: pokebase
155 #+begin_src clojure
156 (defn hxc-pokemon-base
157 ([] (hxc-pokemon-base com.aurellem.gb.gb-driver/original-rom))
158 ([rom]
159 (let [entry-size 28
160 pkmn-count (count (hxc-pokedex-text rom))
161 pokemon (rest (hxc-pokedex-names))
162 types (apply assoc {}
163 (interleave
164 (range)
165 pkmn-types)) ;;!! softcoded
166 moves (apply assoc {}
167 (interleave
168 (range)
169 (map format-name
170 (hxc-move-names rom))))
171 machines (hxc-machines)
172 ]
173 (zipmap
174 pokemon
175 (map
176 (fn [[n
177 rating-hp
178 rating-atk
179 rating-def
180 rating-speed
181 rating-special
182 type-1
183 type-2
184 rarity
185 rating-xp
186 pic-dimensions ;; tile_width|tile_height (8px/tile)
187 ptr-pic-obverse-1
188 ptr-pic-obverse-2
189 ptr-pic-reverse-1
190 ptr-pic-reverse-2
191 move-1
192 move-2
193 move-3
194 move-4
195 growth-rate
196 &
197 TMs|HMs]]
198 (let
199 [base-moves
200 (mapv moves
201 ((comp
202 ;; since the game uses zero as a delimiter,
203 ;; it must also increment all move indices by 1.
204 ;; heren we decrement to correct this.
205 (partial map dec)
206 (partial take-while (comp not zero?)))
207 [move-1 move-2 move-3 move-4]))
209 types
210 (set (list (types type-1)
211 (types type-2)))
212 TMs|HMs
213 (map
214 (comp
215 (partial map first)
216 (partial remove (comp zero? second)))
217 (split-at
218 50
219 (map vector
220 (rest(range))
221 (reduce concat
222 (map
223 #(take 8
224 (concat (bit-list %)
225 (repeat 0)))
227 TMs|HMs)))))
229 TMs (vec (first TMs|HMs))
230 HMs (take 5 (map (partial + -50) (vec (second TMs|HMs))))
233 ]
236 {:dex# n
237 :base-moves base-moves
238 :types types
239 :TMs TMs
240 :HMs HMs
241 :base-hp rating-hp
242 :base-atk rating-atk
243 :base-def rating-def
244 :base-speed rating-speed
245 :base-special rating-special
246 :o0 pic-dimensions
247 :o1 ptr-pic-obverse-1
248 :o2 ptr-pic-obverse-2
249 }))
251 (partition entry-size
252 (take (* entry-size pkmn-count)
253 (drop 0x383DE
254 rom))))))))
256 #+end_src
259 ** Pok\eacute{}mon evolutions
260 #+name: evolution-header
261 #+begin_src clojure
262 (defn format-evo
263 "Parse a sequence of evolution data, returning a map. First is the
264 method: 0 = end-evolution-data. 1 = level-up, 2 = item, 3 = trade. Next is an item id, if the
265 method of evolution is by item (only stones will actually make pokemon
266 evolve, for some auxillary reason.) Finally, the minimum level for
267 evolution to occur (level 1 means no limit, which is used for trade
268 and item evolutions), followed by the internal id of the pokemon
269 into which to evolve. Hence, level up and trade evolutions are
270 described with 3
271 bytes; item evolutions with four."
272 [coll]
273 (let [method (first coll)]
274 (cond (empty? coll) []
275 (= 0 method) [] ;; just in case
276 (= 1 method) ;; level-up evolution
277 (conj (format-evo (drop 3 coll))
278 {:method :level-up
279 :min-level (nth coll 1)
280 :into (dec (nth coll 2))})
282 (= 2 method) ;; item evolution
283 (conj (format-evo (drop 4 coll))
284 {:method :item
285 :item (dec (nth coll 1))
286 :min-level (nth coll 2)
287 :into (dec (nth coll 3))})
289 (= 3 method) ;; trade evolution
290 (conj (format-evo (drop 3 coll))
291 {:method :trade
292 :min-level (nth coll 1) ;; always 1 for trade.
293 :into (dec (nth coll 2))}))))
296 (defn hxc-ptrs-evolve
297 "A hardcoded collection of 190 pointers to alternating evolution/learnset data,
298 in internal order."
299 ([]
300 (hxc-ptrs-evolve com.aurellem.gb.gb-driver/original-rom))
301 ([rom]
302 (let [
303 pkmn-count (count (hxc-pokenames-raw)) ;; 190
304 ptrs
305 (map (fn [[a b]] (low-high a b))
306 (partition 2
307 (take (* 2 pkmn-count)
308 (drop 0x3b1e5 rom))))]
309 (map (partial + 0x34000) ptrs)
311 )))
312 #+end_src
314 #+name:evolution
315 #+begin_src clojure
317 (defn hxc-evolution
318 "Hardcoded evolution data in memory. The data exists at ROM@34000,
319 sorted by internal order. Pointers to the data exist at ROM@3B1E5; see also, hxc-ptrs-evolve."
320 ([] (hxc-evolution com.aurellem.gb.gb-driver/original-rom))
321 ([rom]
322 (apply assoc {}
323 (interleave
324 (hxc-pokenames rom)
325 (map
326 (comp
327 format-evo
328 (partial take-while (comp not zero?))
329 #(drop % rom))
330 (hxc-ptrs-evolve rom)
331 )))))
333 (defn hxc-evolution-pretty
334 "Like hxc-evolution, except it uses the names of items and pokemon
335 --- grabbed from ROM --- rather than their numerical identifiers."
336 ([] (hxc-evolution-pretty com.aurellem.gb.gb-driver/original-rom))
337 ([rom]
338 (let
339 [poke-names (vec (hxc-pokenames rom))
340 item-names (vec (hxc-items rom))
341 use-names
342 (fn [m]
343 (loop [ks (keys m) new-map m]
344 (let [k (first ks)]
345 (cond (nil? ks) new-map
346 (= k :into)
347 (recur
348 (next ks)
349 (assoc new-map
350 :into
351 (poke-names
352 (:into
353 new-map))))
354 (= k :item)
355 (recur
356 (next ks)
357 (assoc new-map
358 :item
359 (item-names
360 (:item new-map))))
361 :else
362 (recur
363 (next ks)
364 new-map)
365 ))))]
367 (into {}
368 (map (fn [[pkmn evo-coll]]
369 [pkmn (map use-names evo-coll)])
370 (hxc-evolution rom))))))
373 #+end_src
376 ** Level-up moves (learnsets)
377 #+name: learnsets
378 #+begin_src clojure
381 (defn hxc-learnsets
382 "Hardcoded map associating pokemon names to lists of pairs [lvl
383 move] of abilities they learn as they level up. The data
384 exists at ROM@34000, sorted by internal order. Pointers to the data
385 exist at ROM@3B1E5; see also, hxc-ptrs-evolve"
386 ([] (hxc-learnsets com.aurellem.gb.gb-driver/original-rom))
387 ([rom]
388 (apply assoc
389 {}
390 (interleave
391 (hxc-pokenames rom)
392 (map (comp
393 (partial map
394 (fn [[lvl mv]] [lvl (dec mv)]))
395 (partial partition 2)
396 ;; keep the learnset data
397 (partial take-while (comp not zero?))
398 ;; skip the evolution data
399 rest
400 (partial drop-while (comp not zero?)))
401 (map #(drop % rom)
402 (hxc-ptrs-evolve rom)))))))
404 (defn hxc-learnsets-pretty
405 "Live hxc-learnsets except it reports the name of each move --- as
406 it appears in rom --- rather than the move index."
407 ([] (hxc-learnsets-pretty com.aurellem.gb.gb-driver/original-rom))
408 ([rom]
409 (let [moves (vec(map format-name (hxc-move-names)))]
410 (into {}
411 (map (fn [[pkmn learnset]]
412 [pkmn (map (fn [[lvl mv]] [lvl (moves mv)])
413 learnset)])
414 (hxc-learnsets rom))))))
418 #+end_src
422 * Pok\eacute{}mon II : the Pok\eacute{}dex
423 ** Species vital stats
424 #+name: dex-stats
425 #+begin_src clojure
426 (defn hxc-pokedex-stats
427 "The hardcoded pokedex stats (species height weight) in memory. List
428 begins at ROM@40687"
429 ([] (hxc-pokedex-stats com.aurellem.gb.gb-driver/original-rom))
430 ([rom]
431 (let [pokedex-names (zipmap (range) (hxc-pokedex-names rom))
432 pkmn-count (count pokedex-names)
433 ]
434 ((fn capture-stats
435 [n stats data]
436 (if (zero? n) stats
437 (let [[species
438 [_
439 height-ft
440 height-in
441 weight-1
442 weight-2
443 _
444 dex-ptr-1
445 dex-ptr-2
446 dex-bank
447 _
448 & data]]
449 (split-with (partial not= 0x50) data)]
450 (recur (dec n)
451 (assoc stats
452 (pokedex-names (- pkmn-count (dec n)))
453 {:species
454 (format-name (character-codes->str species))
455 :height-ft
456 height-ft
457 :height-in
458 height-in
459 :weight
460 (/ (low-high weight-1 weight-2) 10.)
462 ;; :text
463 ;; (character-codes->str
464 ;; (take-while
465 ;; (partial not= 0x50)
466 ;; (drop
467 ;; (+ 0xB8000
468 ;; -0x4000
469 ;; (low-high dex-ptr-1 dex-ptr-2))
470 ;; rom)))
471 })
473 data)
476 )))
478 pkmn-count
479 {}
480 (drop 0x40687 rom))) ))
481 #+end_src
483 ** Species synopses
485 #+name: dex-text
486 #+begin_src clojure
487 (def hxc-pokedex-text-raw
488 "The hardcoded pokedex entries in memory. List begins at
489 ROM@B8000, shortly before move names."
490 (hxc-thunk-words 0xB8000 14754))
495 (defn hxc-pokedex-text
496 "The hardcoded pokedex entries in memory, presented as an
497 associative hash map. List begins at ROM@B8000."
498 ([] (hxc-pokedex-text com.aurellem.gb.gb-driver/original-rom))
499 ([rom]
500 (zipmap
501 (hxc-pokedex-names rom)
502 (cons nil ;; for missingno.
503 (hxc-pokedex-text-raw rom)))))
504 #+end_src
507 ** Pok\eacute{}mon cries
508 #+name: pokecry
509 #+begin_src clojure
510 (defn hxc-cry
511 "The pokemon cry data in internal order. List begins at ROM@39462"
512 ([](hxc-cry com.aurellem.gb.gb-driver/original-rom))
513 ([rom]
514 (zipmap
515 (hxc-pokenames rom)
516 (map
517 (fn [[cry-id pitch length]]
518 {:cry-id cry-id
519 :pitch pitch
520 :length length}
521 )
522 (partition 3
523 (drop 0x39462 rom))))))
525 (defn hxc-cry-groups
526 ([] (hxc-cry-groups com.aurellem.gb.gb-driver/original-rom))
527 ([rom]
528 (map #(mapv first
529 (filter
530 (fn [[k v]]
531 (= % (:cry-id v)))
532 (hxc-cry)))
533 ((comp
534 range
535 count
536 set
537 (partial map :cry-id)
538 vals
539 hxc-cry)
540 rom))))
543 (defn cry-conversion!
544 "Convert Porygon's cry in ROM to be the cry of the given pokemon."
545 [pkmn]
546 (write-rom!
547 (rewrite-memory
548 (vec(rom))
549 0x3965D
550 (map second
551 ((hxc-cry) pkmn)))))
553 #+end_src
555 ** COMMENT Names of permanent stats
556 0DD4D-DD72
558 * Items
559 ** Item names
560 #+name: item-names
561 #+begin_src clojure
563 (def hxc-items-raw
564 "The hardcoded names of the items in memory. List begins at
565 ROM@045B7"
566 (hxc-thunk-words 0x45B7 870))
568 (def hxc-items
569 "The hardcoded names of the items in memory, presented as
570 keywords. List begins at ROM@045B7. See also, hxc-items-raw."
571 (comp (partial map format-name) hxc-items-raw))
572 #+end_src
574 ** Item prices
575 #+name: item-prices
576 #+begin_src clojure
577 (defn hxc-item-prices
578 "The hardcoded list of item prices in memory. List begins at ROM@4495"
579 ([] (hxc-item-prices com.aurellem.gb.gb-driver/original-rom))
580 ([rom]
581 (let [items (hxc-items rom)
582 price-size 3]
583 (zipmap items
584 (map (comp
585 ;; zero-cost items are "priceless"
586 #(if (zero? %) :priceless %)
587 decode-bcd butlast)
588 (partition price-size
589 (take (* price-size (count items))
590 (drop 0x4495 rom))))))))
591 #+end_src
592 ** Vendor inventories
594 #+name: item-vendors
595 #+begin_src clojure
596 (defn hxc-shops
597 ([] (hxc-shops com.aurellem.gb.gb-driver/original-rom))
598 ([rom]
599 (let [items (zipmap (range) (hxc-items rom))
601 ;; temporarily softcode the TM items
602 items (into
603 items
604 (map (juxt identity
605 (comp keyword
606 (partial str "tm-")
607 (partial + 1 -200)
608 ))
609 (take 200 (drop 200 (range)))))
611 ]
613 ((fn parse-shop [coll [num-items & items-etc]]
614 (let [inventory (take-while
615 (partial not= 0xFF)
616 items-etc)
617 [separator & items-etc] (drop num-items (rest items-etc))]
618 (if (= separator 0x50)
619 (map (partial mapv (comp items dec)) (conj coll inventory))
620 (recur (conj coll inventory) items-etc)
621 )
622 ))
624 '()
625 (drop 0x233C rom))
628 )))
629 #+end_src
631 #+results: item-vendors
632 : #'com.aurellem.gb.hxc/hxc-shops
636 * Types
637 ** Names of types
638 #+name: type-names
639 #+begin_src clojure
640 (def hxc-types
641 "The hardcoded type names in memory. List begins at ROM@27D99,
642 shortly before hxc-titles."
643 (hxc-thunk-words 0x27D99 102))
645 #+end_src
647 ** Type effectiveness
648 #+name: type-advantage
649 #+begin_src clojure
650 (defn hxc-advantage
651 ;; in-game multipliers are stored as 10x their effective value
652 ;; to allow for fractional multipliers like 1/2
654 "The hardcoded type advantages in memory, returned as tuples of
655 atk-type def-type multiplier. By default (i.e. if not listed here),
656 the multiplier is 1. List begins at 0x3E62D."
657 ([] (hxc-advantage com.aurellem.gb.gb-driver/original-rom))
658 ([rom]
659 (map
660 (fn [[atk def mult]] [(get pkmn-types atk (hex atk))
661 (get pkmn-types def (hex def))
662 (/ mult 10)])
663 (partition 3
664 (take-while (partial not= 0xFF)
665 (drop 0x3E62D rom))))))
666 #+end_src
670 * Moves
671 ** Names of moves
672 #+name: move-names
673 #+begin_src clojure
674 (def hxc-move-names
675 "The hardcoded move names in memory. List begins at ROM@BC000"
676 (hxc-thunk-words 0xBC000 1551))
677 #+end_src
679 ** Properties of moves
681 #+name: move-data
682 #+begin_src clojure
683 (defn hxc-move-data
684 "The hardcoded (basic (move effects)) in memory. List begins at
685 0x38000. Returns a map of {:name :power :accuracy :pp :fx-id
686 :fx-txt}. The move descriptions are handwritten, not hardcoded."
687 ([]
688 (hxc-move-data com.aurellem.gb.gb-driver/original-rom))
689 ([rom]
690 (let [names (vec (hxc-move-names rom))
691 move-count (count names)
692 move-size 6
693 types pkmn-types ;;; !! hardcoded types
694 ]
695 (zipmap (map format-name names)
696 (map
697 (fn [[idx effect power type-id accuracy pp]]
698 {:name (names (dec idx))
699 :power power
700 :accuracy accuracy
701 :pp pp
702 :type (types type-id)
703 :fx-id effect
704 :fx-txt (get move-effects effect)
705 }
706 )
708 (partition move-size
709 (take (* move-size move-count)
710 (drop 0x38000 rom))))))))
714 (defn hxc-move-data*
715 "Like hxc-move-data, but reports numbers as hexadecimal symbols instead."
716 ([]
717 (hxc-move-data* com.aurellem.gb.gb-driver/original-rom))
718 ([rom]
719 (let [names (vec (hxc-move-names rom))
720 move-count (count names)
721 move-size 6
722 format-name (fn [s]
723 (keyword (.toLowerCase
724 (apply str
725 (map #(if (= % \space) "-" %) s)))))
726 ]
727 (zipmap (map format-name names)
728 (map
729 (fn [[idx effect power type accuracy pp]]
730 {:name (names (dec idx))
731 :power power
732 :accuracy (hex accuracy)
733 :pp pp
734 :fx-id (hex effect)
735 :fx-txt (get move-effects effect)
736 }
737 )
739 (partition move-size
740 (take (* move-size move-count)
741 (drop 0x38000 rom))))))))
743 #+end_src
745 ** TM and HM moves
747 #+name: machines
748 #+begin_src clojure
749 (defn hxc-machines
750 "The hardcoded moves taught by TMs and HMs. List begins at ROM@1232D."
751 ([] (hxc-machines
752 com.aurellem.gb.gb-driver/original-rom))
753 ([rom]
754 (let [moves (hxc-move-names rom)]
755 (zipmap
756 (range)
757 (take-while
758 (comp not nil?)
759 (map (comp
760 format-name
761 (zipmap
762 (range)
763 moves)
764 dec)
765 (take 100
766 (drop 0x1232D rom))))))))
768 #+end_src
774 ** COMMENT Status ailments
776 * Places
777 ** Names of places
779 #+name: places
780 #+begin_src clojure
781 (def hxc-places
782 "The hardcoded place names in memory. List begins at
783 ROM@71500. [Cinnabar] Mansion seems to be dynamically calculated."
784 (hxc-thunk-words 0x71500 560))
786 #+end_src
788 ** Wild Pok\eacute{}mon demographics
789 #+name: wilds
790 #+begin_src clojure
794 (defn hxc-ptrs-wild
795 "A list of the hardcoded wild encounter data in memory. Pointers
796 begin at ROM@0CB95; data begins at ROM@0x04D89"
797 ([] (hxc-ptrs-wild com.aurellem.gb.gb-driver/original-rom))
798 ([rom]
799 (let [ptrs
800 (map (fn [[a b]] (+ a (* 0x100 b)))
801 (take-while (partial not= (list 0xFF 0xFF))
802 (partition 2 (drop 0xCB95 rom))))]
803 ptrs)))
807 (defn hxc-wilds
808 "A list of the hardcoded wild encounter data in memory. Pointers
809 begin at ROM@0CB95; data begins at ROM@0x04D89"
810 ([] (hxc-wilds com.aurellem.gb.gb-driver/original-rom))
811 ([rom]
812 (let [pokenames (zipmap (range) (hxc-pokenames rom))]
813 (map
814 (partial map (fn [[a b]] {:species (pokenames (dec b)) :level
815 a}))
816 (partition 10
818 (take-while (comp (partial not= 1)
819 first)
820 (partition 2
821 (drop 0xCD8C rom))
823 ))))))
825 #+end_src
831 * Appendices
835 ** Mapping the ROM
837 | ROM address (hex) | Description | Format | Example |
838 |-------------------+-----------------+-----------------+-----------------|
839 | | <15> | <15> | <15> |
840 | 0233C- | Shop inventories. | | |
841 | 04495- | Prices of items. | Each price is two bytes of binary-coded decimal. Prices are separated by zeroes. Priceless items[fn::Like the Pok\eacute{}dex and other unsellable items.] are given a price of zero. | The cost of lemonade is 0x03 0x50, which translates to a price of ₱350. |
842 | 045B7-0491E | Names of the items in memory. | Variable-length item names (strings of character codes). Names are separated by a single 0x80 character. | MASTER BALL#ULTRA BALL#... |
843 | 04D89- | Lists of wild Pok\eacute{}mon to encounter in each region. | Each list contains ten Pokemon (ids) and their levels; twenty bytes in total. First, the level of the first Pokemon. Then the internal id of the first Pokemon. Next, the level of the second Pokemon, and so on. Since Pokemon cannot have level 0, the lists are separated by a pair 0 /X/, where /X/ is an apparently random Pokemon id. | The first list is (3 36 4 36 2 165 3 165 2 36 3 36 5 36 4 165 6 36 7 36 0 25), i.e. level 3 pidgey, level 4 pidgey, level 2 rattata, level 3 rattata, level 2 pidgey, level 3 pidgey, level 5 pidgey, level 4 rattata, level 6 pidgey, level 7 pidgey, \ldquo{}level 0 gastly\rdquo{} (i.e., end-of-list). |
844 | 05EDB. | Which Pok\eacute{}mon to show during Prof. Oak's introduction. | A single byte, the Pok\eacute{}mon's internal id. | In Pok\eacute{}mon Yellow, it shows Pikachu during the introduction; Pikachu's internal id is 0x54. |
845 | 06698- | ? Background music. | | |
846 | 0822E-082F? | Pointers to background music, part I. | | |
847 | 0CB95- | Pointers to lists of wild pokemon to encounter in each region. These lists begin at 04D89, see above. | Each pointer is a low-byte, high-byte pair. | The first entry is 0x89 0x4D, corresponding to the address 0x4D89, the location of the first list of wild Pok\eacute{}mon (see 04D89, above). |
848 |-------------------+-----------------+-----------------+-----------------|
849 | 0DADB. | Amount of HP restored by Hyper Potion. | The HP consists of a single byte. TODO: Discover what the surrounding data does, and find the data for the amount of HP restored by other items: Fresh Water (50HP), Soda (60HP), Lemonade(80HP). | 200 |
850 | 0DAE0. | Amount of HP restored by Super Potion. | " | 50 |
851 | 0DAE3. | Amount of HP restored by Potion. | " | 20 |
852 |-------------------+-----------------+-----------------+-----------------|
853 | 0DD4D-DD72 | Names of permanent stats. | Variable-length strings separated by 0x50. | #HEALTH#ATTACK#DEFENSE#SPEED#SPECIAL# |
854 | 1195C-1196A | The two terms for being able/unable to learn a TM/HM. | Variable-length strings separated by 0x50. | ABLE#NOT ABLE# |
855 | 119C0-119CE | The two terms for being able/unable to evolve using the current stone. | Variable-length strings separated by 0x50. | ABLE#NOT ABLE# |
856 | 1232D-12364 | Which moves are taught by the TMs and HMs | A list of 55 move ids (50 TMs, plus 5 HMs). First, the move that will be taught by TM01; second, the move that will be taught by TM02; and so on. The last five entries are the moves taught by HMs 1-5. (See also, BC000 below) | The first few entries are (5 13 14 18 ...) corresponding to Mega Punch (TM01), Razor Wind (TM02), Swords Dance (TM03), Whirlwind (TM04), ... |
857 | 27D99-27DFF | Names of the Pok\eacute{}mon types. | Variable-length type names (strings of character codes). Names are separated by a single 0x80 character. | NORMAL#FIGHTING#... |
858 | 27E77- | Trainer title names. | Variable-length names separated by 0x80. | YOUNGSTER#BUG CATCHER#LASS#... |
859 | 34000- | | | |
860 | 38000-383DE | The basic properties and effects of moves. (165 moves total) | Fixed-length (6 byte) continguous descriptions (no separating character): move-index, move-effect, power, move-type, accuracy, pp. | The entry for Pound, the first attack in the list, is (1 0 40 0 255 35). See below for more explanation. |
861 | 383DE- | Species data for the Pokemon, listed in Pokedex order: Pokedex number; base moves; types; learnable TMs and HMs; base HP, attack, defense, speed, special; sprite data. | | |
862 | 39462- | The Pok\eacute{}mon cry data. | Fixed-length (3 byte) descriptions of cries. | |
863 | 3B1E5- | Pointers to evolution/learnset data. | | |
864 | 3B361- | Evolution and learnset data. [fn::Evolution data consists of how to make Pok\eacute{}mon evolve, and what they evolve into. Learnset data consists of the moves that Pok\eacute{}mon learn as they level up.] | Variable-length evolution information (see below), followed by a list of level/move-id learnset pairs. | |
865 | 40687- | Species data from the Pok\eacute{}dex: species name, height, weight, etc. | Fixed-length sequences of bytes. See below for specifics. | |
866 | 410B1- | A conversion table between internal order and Pokedex order. | | |
867 | 5DE10-5DE30 | Abbreviations for status ailments. | Fixed-length strings, probably[fn::Here's something strange: all of the status messages start with 0x7F and end with 0x4F \mdash{}except PAR, which ends with 0x50.]. The last entry is QUIT##. | [0x7F] *SLP* [0x4E][0x7F] *PSN* [0x4E][0x7F] *PAR* [0x50][0x7F]... |
868 | 71500- | Names of places. | | |
869 | 7C249-7C2?? | Pointers to background music, pt II. | | |
870 | 98000- | Dialogue | | |
871 | B8000- | The text of each Pokemon's Pok\eacute{}dex entry. | | |
872 | BC000-BC60E | Move names. | Variable-length move names, separated by 0x80. The moves are in internal order. | POUND#KARATE CHOP#DOUBLESLAP#COMET PUNCH#... |
873 | E8000-E876C | Names of the \ldquo{}190\rdquo{} species of Pok\eacute{}mon in memory. | Fixed length (10-letter) Pok\eacute{}mon names. Any extra space is padded with the character 0x80. The names are in \ldquo{}internal order\rdquo{}. | RHYDON####KANGASKHANNIDORAN♂#... |
874 | | | | |
875 | | | | |
879 ** Internal Pok\eacute{}mon IDs
880 ** Type IDs
882 #+name: type-ids
883 #+begin_src clojure
884 (def pkmn-types
885 [:normal ;;0
886 :fighting ;;1
887 :flying ;;2
888 :poison ;;3
889 :ground ;;4
890 :rock ;;5
891 :bird ;;6
892 :bug ;;7
893 :ghost ;;8
894 :A
895 :B
896 :C
897 :D
898 :E
899 :F
900 :G
901 :H
902 :I
903 :J
904 :K
905 :fire ;;20 (0x14)
906 :water ;;21 (0x15)
907 :grass ;;22 (0x16)
908 :electric ;;23 (0x17)
909 :psychic ;;24 (0x18)
910 :ice ;;25 (0x19)
911 :dragon ;;26 (0x1A)
912 ])
913 #+end_src
915 ** Basic effects of moves
917 *** Table of basic effects
919 The possible effects of moves in Pok\eacute{}mon \mdash{} for example, dealing
920 damage, leeching health, or potentially poisoning the opponent
921 \mdash{} are stored in a table. Each move has exactly one effect, and
922 different moves might have the same effect.
924 For example, Leech Life, Mega Drain, and Absorb all have effect ID #3, which is \ldquo{}Leech half of the inflicted damage.\rdquo{}
926 All the legitimate move effects are listed in the table
927 below. Here are some notes for reading it:
929 - Whenever an effect has a chance of doing something (like a chance of
930 poisoning the opponent), I list the chance as a hexadecimal amount out of 256 to avoid rounding errors. To convert the hex amount into a percentage, divide by 256.
931 - For some effects, the description is too cumbersome to
932 write. Instead, I just write a move name
933 in parentheses, like: (leech seed). That move gives a characteristic example
934 of the effect.
935 - I use the abbreviations =atk=, =def=, =spd=, =spc=, =acr=, =evd= for
936 attack, defense, speed, special, accuracy, and evasion.
937 .
941 | ID (hex) | Description | Notes |
942 |----------+-------------------------------------------------------------------------------------------------+------------------------------------------------------------------|
943 | 0 | normal damage | |
944 | 1 | no damage, just sleep | TODO: find out how many turns |
945 | 2 | 0x4C chance of poison | |
946 | 3 | leech half of inflicted damage | |
947 | 4 | 0x19 chance of burn | |
948 | 5 | 0x19 chance of freeze | |
949 | 6 | 0x19 chance of paralysis | |
950 | 7 | user faints; opponent's defense is halved during attack. | |
951 | 8 | leech half of inflicted damage ONLY if the opponent is asleep | |
952 | 9 | imitate last attack | |
953 | A | user atk +1 | |
954 | B | user def +1 | |
955 | C | user spd +1 | |
956 | D | user spc +1 | |
957 | E | user acr +1 | This effect is unused. |
958 | F | user evd +1 | |
959 | 10 | get post-battle money = 2 * level * uses | |
960 | 11 | move has 0xFE acr, regardless of battle stat modifications. | |
961 | 12 | opponent atk -1 | |
962 | 13 | opponent def -1 | |
963 | 14 | opponent spd -1 | |
964 | 15 | opponent spc -1 | |
965 | 16 | opponent acr -1 | |
966 | 17 | opponent evd -1 | |
967 | 18 | converts user's type to opponent's. | |
968 | 19 | (haze) | |
969 | 1A | (bide) | |
970 | 1B | (thrash) | |
971 | 1C | (teleport) | |
972 | 1D | (fury swipes) | |
973 | 1E | attacks 2-5 turns | Unused. TODO: find out what it does. |
974 | 1F | 0x19 chance of flinching | |
975 | 20 | opponent sleep for 1-7 turns | |
976 | 21 | 0x66 chance of poison | |
977 | 22 | 0x4D chance of burn | |
978 | 23 | 0x4D chance of freeze | |
979 | 24 | 0x4D chance of paralysis | |
980 | 25 | 0x4D chance of flinching | |
981 | 26 | one-hit KO | |
982 | 27 | charge one turn, atk next. | |
983 | 28 | fixed damage, leaves 1HP. | Is the fixed damage the power of the move? |
984 | 29 | fixed damage. | Like seismic toss, dragon rage, psywave. |
985 | 2A | atk 2-5 turns; opponent can't attack | The odds of attacking for /n/ turns are: (0 0x60 0x60 0x20 0x20) |
986 | 2B | charge one turn, atk next. (can't be hit when charging) | |
987 | 2C | atk hits twice. | |
988 | 2D | user takes 1 damage if misses. | |
989 | 2E | evade status-lowering effects | Caused by you or also your opponent? |
990 | 2F | broken: if user is slower than opponent, makes critical hit impossible, otherwise has no effect | This is the effect of Focus Energy. It's (very) broken. |
991 | 30 | atk causes recoil dmg = 1/4 dmg dealt | |
992 | 31 | confuses opponent | |
993 | 32 | user atk +2 | |
994 | 33 | user def +2 | |
995 | 34 | user spd +2 | |
996 | 35 | user spc +2 | |
997 | 36 | user acr +2 | This effect is unused. |
998 | 37 | user evd +2 | This effect is unused. |
999 | 38 | restores up to half of user's max hp. | |
1000 | 39 | (transform) | |
1001 | 3A | opponent atk -2 | |
1002 | 3B | opponent def -2 | |
1003 | 3C | opponent spd -2 | |
1004 | 3D | opponent spc -2 | |
1005 | 3E | opponent acr -2 | |
1006 | 3F | opponent evd -2 | |
1007 | 40 | doubles user spc when attacked | |
1008 | 41 | doubles user def when attacked | |
1009 | 42 | just poisons opponent | |
1010 | 43 | just paralyzes opponent | |
1011 | 44 | 0x19 chance opponent atk -1 | |
1012 | 45 | 0x19 chance opponent def -1 | |
1013 | 46 | 0x19 chance opponent spd -1 | |
1014 | 47 | 0x4C chance opponent spc -1 | |
1015 | 48 | 0x19 chance opponent acr -1 | |
1016 | 49 | 0x19 chance opponent evd -1 | |
1017 | 4A | ??? | ;; unused? no effect? |
1018 | 4B | ??? | ;; unused? no effect? |
1019 | 4C | 0x19 chance of confusing the opponent | |
1020 | 4D | atk hits twice. 0x33 chance opponent poisioned. | |
1021 | 4E | broken. crash the game after attack. | |
1022 | 4F | (substitute) | |
1023 | 50 | unless opponent faints, user must recharge after atk. some exceptions apply | |
1024 | 51 | (rage) | |
1025 | 52 | (mimic) | |
1026 | 53 | (metronome) | |
1027 | 54 | (leech seed) | |
1028 | 55 | does nothing (splash) | |
1029 | 56 | (disable) | |
1030 #+end_src
1032 *** Source
1033 #+name: move-effects
1034 #+begin_src clojure
1035 (def move-effects
1036 ["normal damage"
1037 "no damage, just opponent sleep" ;; how many turns? is atk power ignored?
1038 "0x4C chance of poison"
1039 "leech half of inflicted damage"
1040 "0x19 chance of burn"
1041 "0x19 chance of freeze"
1042 "0x19 chance of paralyze"
1043 "user faints; opponent defense halved during attack."
1044 "leech half of inflicted damage ONLY if sleeping opponent."
1045 "imitate last attack"
1046 "user atk +1"
1047 "user def +1"
1048 "user spd +1"
1049 "user spc +1"
1050 "user acr +1" ;; unused?!
1051 "user evd +1"
1052 "get post-battle $ = 2*level*uses"
1053 "0xFE acr, no matter what."
1054 "opponent atk -1" ;; acr taken from move acr?
1055 "opponent def -1" ;;
1056 "opponent spd -1" ;;
1057 "opponent spc -1" ;;
1058 "opponent acr -1";;
1059 "opponent evd -1"
1060 "converts user's type to opponent's."
1061 "(haze)"
1062 "(bide)"
1063 "(thrash)"
1064 "(teleport)"
1065 "(fury swipes)"
1066 "attacks 2-5 turns" ;; unused? like rollout?
1067 "0x19 chance of flinch"
1068 "opponent sleep for 1-7 turns"
1069 "0x66 chance of poison"
1070 "0x4D chance of burn"
1071 "0x4D chance of freeze"
1072 "0x4D chance of paralyze"
1073 "0x4D chance of flinch"
1074 "one-hit KO"
1075 "charge one turn, atk next."
1076 "fixed damage, leaves 1HP." ;; how is dmg determined?
1077 "fixed damage." ;; cf seismic toss, dragon rage, psywave.
1078 "atk 2-5 turns; opponent can't attack" ;; unnormalized? (0 0x60 0x60 0x20 0x20)
1079 "charge one turn, atk next. (can't be hit when charging)"
1080 "atk hits twice."
1081 "user takes 1 damage if misses."
1082 "evade status-lowering effects" ;;caused by you or also your opponent?
1083 "(broken) if user is slower than opponent, makes critical hit impossible, otherwise has no effect"
1084 "atk causes recoil dmg = 1/4 dmg dealt"
1085 "confuses opponent" ;; acr taken from move acr
1086 "user atk +2"
1087 "user def +2"
1088 "user spd +2"
1089 "user spc +2"
1090 "user acr +2" ;; unused!
1091 "user evd +2" ;; unused!
1092 "restores up to half of user's max hp." ;; broken: fails if the difference
1093 ;; b/w max and current hp is one less than a multiple of 256.
1094 "(transform)"
1095 "opponent atk -2"
1096 "opponent def -2"
1097 "opponent spd -2"
1098 "opponent spc -2"
1099 "opponent acr -2"
1100 "opponent evd -2"
1101 "doubles user spc when attacked"
1102 "doubles user def when attacked"
1103 "just poisons opponent" ;;acr taken from move acr
1104 "just paralyzes opponent" ;;
1105 "0x19 chance opponent atk -1"
1106 "0x19 chance opponent def -1"
1107 "0x19 chance opponent spd -1"
1108 "0x4C chance opponent spc -1" ;; context suggest chance is 0x19
1109 "0x19 chance opponent acr -1"
1110 "0x19 chance opponent evd -1"
1111 "???" ;; unused? no effect?
1112 "???" ;; unused? no effect?
1113 "0x19 chance opponent confused"
1114 "atk hits twice. 0x33 chance opponent poisioned."
1115 "broken. crash the game after attack."
1116 "(substitute)"
1117 "unless opponent faints, user must recharge after atk. some
1118 exceptions apply."
1119 "(rage)"
1120 "(mimic)"
1121 "(metronome)"
1122 "(leech seed)"
1123 "does nothing (splash)"
1124 "(disable)"
1125 ])
1126 #+end_src
1129 ** Alphabet code
1131 * Source
1133 #+begin_src clojure :tangle ../clojure/com/aurellem/gb/hxc.clj
1135 (ns com.aurellem.gb.hxc
1136 (:use (com.aurellem.gb assembly characters gb-driver util mem-util
1137 constants species))
1138 (:import [com.aurellem.gb.gb_driver SaveState]))
1140 ; ************* HANDWRITTEN CONSTANTS
1142 <<type-ids>>
1145 ;; question: when status effects claim to take
1146 ;; their accuracy from the move accuracy, does
1147 ;; this mean that the move always "hits" but the
1148 ;; status effect may not?
1150 <<move-effects>>
1152 ;; ************** HARDCODED DATA
1154 <<hxc-thunks>>
1155 ;; --------------------------------------------------
1157 <<pokenames>>
1158 <<type-names>>
1160 ;; http://hax.iimarck.us/topic/581/
1161 <<pokecry>>
1164 <<item-names>>
1168 (def hxc-titles
1169 "The hardcoded names of the trainer titles in memory. List begins at
1170 ROM@27E77"
1171 (hxc-thunk-words 0x27E77 196))
1174 <<dex-text>>
1176 ;; In red/blue, pokedex stats are in internal order.
1177 ;; In yellow, pokedex stats are in pokedex order.
1178 <<dex-stats>>
1183 <<places>>
1185 (defn hxc-dialog
1186 "The hardcoded dialogue in memory, including in-game alerts. Dialog
1187 seems to be separated by 0x57 instead of 0x50 (END). Begins at ROM@98000."
1188 ([rom]
1189 (map character-codes->str
1190 (take-nth 2
1191 (partition-by #(= % 0x57)
1192 (take 0x0F728
1193 (drop 0x98000 rom))))))
1194 ([]
1195 (hxc-dialog com.aurellem.gb.gb-driver/original-rom)))
1198 <<move-names>>
1199 <<move-data>>
1201 <<machines>>
1205 (defn internal-id
1206 ([rom]
1207 (zipmap
1208 (hxc-pokenames rom)
1209 (range)))
1210 ([]
1211 (internal-id com.aurellem.gb.gb-driver/original-rom)))
1217 ;; nidoran gender change upon levelup
1218 ;; (->
1219 ;; @current-state
1220 ;; rom
1221 ;; vec
1222 ;; (rewrite-memory
1223 ;; (nth (hxc-ptrs-evolve) ((internal-id) :nidoran♂))
1224 ;; [1 1 15])
1225 ;; (rewrite-memory
1226 ;; (nth (hxc-ptrs-evolve) ((internal-id) :nidoran♀))
1227 ;; [1 1 3])
1228 ;; (write-rom!)
1230 ;; )
1234 <<type-advantage>>
1238 <<evolution-header>>
1239 <<evolution>>
1240 <<learnsets>>
1241 <<pokebase>>
1244 (defn hxc-intro-pkmn
1245 "The hardcoded pokemon to display in Prof. Oak's introduction; the pokemon's
1246 internal id is stored at ROM@5EDB."
1247 ([] (hxc-intro-pkmn
1248 com.aurellem.gb.gb-driver/original-rom))
1249 ([rom]
1250 (nth (hxc-pokenames rom) (nth rom 0x5EDB))))
1252 (defn sxc-intro-pkmn!
1253 "Set the hardcoded pokemon to display in Prof. Oak's introduction."
1254 [pokemon]
1255 (write-rom!
1256 (rewrite-rom 0x5EDB
1258 (inc
1259 ((zipmap
1260 (hxc-pokenames)
1261 (range))
1262 pokemon))])))
1265 <<item-prices>>
1267 <<item-vendors>>
1269 <<wilds>>
1272 ;; ********************** MANIPULATION FNS
1275 (defn same-type
1276 ([pkmn move]
1277 (same-type
1278 com.aurellem.gb.gb-driver/original-rom pkmn move))
1279 ([rom pkmn move]
1280 (((comp :types (hxc-pokemon-base rom)) pkmn)
1281 ((comp :type (hxc-move-data rom)) move))))
1286 (defn submap?
1287 "Compares the two maps. Returns true if map-big has the same associations as map-small, otherwise false."
1288 [map-small map-big]
1289 (cond (empty? map-small) true
1290 (and
1291 (contains? map-big (ffirst map-small))
1292 (= (get map-big (ffirst map-small))
1293 (second (first map-small))))
1294 (recur (next map-small) map-big)
1296 :else false))
1299 (defn search-map [proto-map maps]
1300 "Returns all the maps that make the same associations as proto-map."
1301 (some (partial submap? proto-map) maps))
1303 (defn filter-vals
1304 "Returns a map consisting of all the pairs [key val] for
1305 which (pred key) returns true."
1306 [pred map]
1307 (reduce (partial apply assoc) {}
1308 (filter (fn [[k v]] (pred v)) map)))
1311 (defn search-moves
1312 "Returns a subcollection of all hardcoded moves with the
1313 given attributes. Attributes consist of :name :power
1314 :accuracy :pp :fx-id
1315 (and also :fx-txt, but it contains the same information
1316 as :fx-id)"
1317 ([attribute-map]
1318 (search-moves
1319 com.aurellem.gb.gb-driver/original-rom attribute-map))
1320 ([rom attribute-map]
1321 (filter-vals (partial submap? attribute-map)
1322 (hxc-move-data rom))))
1328 ;; note: 0x2f31 contains the names "TM" "HM"?
1330 ;; note for later: credits start at F1290
1332 ;; note: DADB hyper-potion-hp _ _ _ super-potion-hp _ _ _ potion-hp ??
1334 ;; note: DD4D spells out pokemon vital stat names ("speed", etc.)
1336 ;; note: 1195C-6A says ABLE#NOT ABLE#, but so does 119C0-119CE.
1337 ;; The first instance is for Machines; the second, for stones.
1339 ;; 0x251A (in indexable mem): image decompression routine seems to begin here.
1342 (comment
1344 (def hxc-later
1345 "Running this code produces, e.g. hardcoded names NPCs give
1346 their pokemon. Will sort through it later."
1347 (print (character-codes->str(take 10000
1348 (drop 0x71597
1349 (rom (root)))))))
1351 (let [dex
1352 (partition-by #(= 0x50 %)
1353 (take 2540
1354 (drop 0x40687
1355 (rom (root)))))]
1356 (def dex dex)
1357 (def hxc-species
1358 (map character-codes->str
1359 (take-nth 4 dex))))
1363 #+end_src
1365 #+results:
1366 : nil