view org/rom.org @ 470:c02108ddcb35

Added introduction.
author Dylan Holmes <ocsenave@gmail.com>
date Sun, 29 Apr 2012 19:48:43 -0500
parents 13165fb5852b
children 5f87c3e46c22
line wrap: on
line source
1 #+title: Notes on Deconstructing Pokemon Yellow
2 #+author: Dylan Holmes
3 #+email: rlm@mit.edu
4 #+description: A detailed explication of Pok\eacute{}mon Yellow, helped by Clojure.
5 #+keywords: pokemon, pokemon yellow, rom, gameboy, assembly, hex, pointers, clojure
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
14 This article contains the results of my investigations with
15 Pok\eacute{}mon Yellow as I searched for interesting
16 data in the ROM. By using the Clojure language interface
17 written by Robert[fn::This Clojure interface will be published to aurellem.org soon.], I
18 was able to interact with the game in real-time, sending commands and
19 gathering data. The result is a manifestly accurate map of
20 Pok\eacute{}mon Yellow; every result
21 comes with runnable code that /works/. You can see the code and the output of
22 every function and confirm for yourself that they are all correct. I
23 hope you like the result!
25 To orient yourself, you can look for a specific topic in the table of contents
26 above, or browse the [[#sec-9-1][map of the ROM]], below.
29 (If you have any questions or comments, please e-mail =rlm@mit.edu=)
32 ** COMMENT Getting linguistic data: names, words, etc.
34 Some of the simplest data
37 One of the simplest data structures in the Pok\eacute{} ROM is an
38 unbroken list of strings that either (a) all have a specific length,
39 or (b) are all separated by the same character.
41 Because lots of good data has this format, we'll start by writing a
42 template function to extract it:
44 #+name: hxc-thunks
45 #+begin_src clojure :results silent
46 (defn hxc-thunk
47 "Creates a thunk (nullary fn) that grabs data in a certain region of rom and
48 splits it into a collection by 0x50. If rom is not supplied, uses the
49 original rom data."
50 [start length]
51 (fn self
52 ([rom]
53 (take-nth 2
54 (partition-by #(= % 0x50)
55 (take length
56 (drop start rom)))))
57 ([]
58 (self com.aurellem.gb.gb-driver/original-rom))))
60 (def hxc-thunk-words
61 "Same as hxc-thunk, except it interprets the rom data as characters,
62 returning a collection of strings."
63 (comp
64 (partial comp (partial map character-codes->str))
65 hxc-thunk))
67 #+end_src
70 * Pok\eacute{}mon I
71 ** Names of each species
72 The names of the Pok\eacute{}mon species are stored in
73 ROM@E8000. This name list is interesting, for a number of reasons:
74 - The names are stored in [[ ][internal order]] rather than in the familiar
75 Pok\eacute{}dex order. This seemingly random order probably represents the order in which the authors created or
76 programmed in the Pok\eacute{}mon; it's used throughout the game.
77 - There is enough space allocated for 190 Pok\eacute{}mon. As I
78 understand it, there were originally going to be 190 Pok\eacute{}mon
79 in Generation I, but the creators decided to defer some to
80 Generation II. This explains why many Gen I and Gen II Pok\eacute{}mon
81 have the same aesthetic feel.
82 - The list is pockmarked with random gaps, due to the strange internal
83 ordering
84 and the 39 unused spaces [fn::190 allocated spaces minus 151 true Pok\eacute{}mon]. These missing spaces are filled with the
85 placeholder name =MISSINGNO.= (\ldquo{}Missing number\rdquo{}).
87 Each name is exactly ten letters long; whenever a name would be too short, the extra
88 space is padded with the character 0x50.
90 *** See the data
92 Here you can see the raw data in three stages: in the first stage, we
93 just grab the first few bytes starting from position 0xE8000. In the
94 second stage, we partition the bytes into ten-letter chunks to show you
95 where the names begin and end. In the final stage, we convert each
96 byte into the letter it represents using the =character-codes->str=
97 function. (0x50 is rendered as the symbol \ldquo{} =#= \rdquo{} for
98 ease of reading).
100 #+begin_src clojure :exports both :cache no :results output
101 (ns com.aurellem.gb.hxc
102 (:use (com.aurellem.gb assembly characters gb-driver util mem-util
103 constants))
104 (:import [com.aurellem.gb.gb_driver SaveState]))
107 (println (take 100 (drop 0xE8000 (rom))))
109 (println (partition 10 (take 100 (drop 0xE8000 (rom)))))
111 (println (character-codes->str (take 100 (drop 0xE8000 (rom)))))
114 #+end_src
116 #+results:
117 : (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)
118 : ((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))
119 : RHYDON####KANGASKHANNIDORAN♂##CLEFAIRY##SPEAROW###VOLTORB###NIDOKING##SLOWBRO###IVYSAUR###EXEGGUTOR#
122 *** Automatically grab the data.
124 #+name: pokenames
125 #+begin_src clojure
127 (defn hxc-pokenames-raw
128 "The hardcoded names of the 190 species in memory. List begins at
129 ROM@E8000. Although names in memory are padded with 0x50 to be 10 characters
130 long, these names are stripped of padding. See also, hxc-pokedex-names"
131 ([]
132 (hxc-pokenames-raw com.aurellem.gb.gb-driver/original-rom))
133 ([rom]
134 (let [count-species 190
135 name-length 10]
136 (map character-codes->str
137 (partition name-length
138 (map #(if (= 0x50 %) 0x00 %)
139 (take (* count-species name-length)
140 (drop 0xE8000
141 rom))))))))
142 (def hxc-pokenames
143 (comp
144 (partial map format-name)
145 hxc-pokenames-raw))
150 (defn hxc-pokedex-names
151 "The names of the pokemon in hardcoded pokedex order. List of the
152 pokedex numbers of each pokemon (in internal order) begins at
153 ROM@410B1. See also, hxc-pokenames."
154 ([] (hxc-pokedex-names
155 com.aurellem.gb.gb-driver/original-rom))
156 ([rom]
157 (let [names (hxc-pokenames rom)]
158 (#(mapv %
159 ((comp range count keys) %))
160 (zipmap
161 (take (count names)
162 (drop 0x410b1 rom))
164 names)))))
166 #+end_src
170 ** Generic species information
172 #+name: pokebase
173 #+begin_src clojure
174 (defn hxc-pokemon-base
175 ([] (hxc-pokemon-base com.aurellem.gb.gb-driver/original-rom))
176 ([rom]
177 (let [entry-size 28
179 pokemon (rest (hxc-pokedex-names))
180 pkmn-count (inc(count pokemon))
181 types (apply assoc {}
182 (interleave
183 (range)
184 pkmn-types)) ;;!! softcoded
185 moves (apply assoc {}
186 (interleave
187 (range)
188 (map format-name
189 (hxc-move-names rom))))
190 machines (hxc-machines)
191 ]
192 (zipmap
193 pokemon
194 (map
195 (fn [[n
196 rating-hp
197 rating-atk
198 rating-def
199 rating-speed
200 rating-special
201 type-1
202 type-2
203 rarity
204 rating-xp
205 pic-dimensions ;; tile_width|tile_height (8px/tile)
206 ptr-pic-obverse-1
207 ptr-pic-obverse-2
208 ptr-pic-reverse-1
209 ptr-pic-reverse-2
210 move-1
211 move-2
212 move-3
213 move-4
214 growth-rate
215 &
216 TMs|HMs]]
217 (let
218 [base-moves
219 (mapv moves
220 ((comp
221 ;; since the game uses zero as a delimiter,
222 ;; it must also increment all move indices by 1.
223 ;; heren we decrement to correct this.
224 (partial map dec)
225 (partial take-while (comp not zero?)))
226 [move-1 move-2 move-3 move-4]))
228 types
229 (set (list (types type-1)
230 (types type-2)))
231 TMs|HMs
232 (map
233 (comp
234 (partial map first)
235 (partial remove (comp zero? second)))
236 (split-at
237 50
238 (map vector
239 (rest(range))
240 (reduce concat
241 (map
242 #(take 8
243 (concat (bit-list %)
244 (repeat 0)))
246 TMs|HMs)))))
248 TMs (vec (first TMs|HMs))
249 HMs (take 5 (map (partial + -50) (vec (second TMs|HMs))))
252 ]
255 {:dex# n
256 :base-moves base-moves
257 :types types
258 :TMs TMs
259 :HMs HMs
260 :base-hp rating-hp
261 :base-atk rating-atk
262 :base-def rating-def
263 :base-speed rating-speed
264 :base-special rating-special
265 :o0 pic-dimensions
266 :o1 ptr-pic-obverse-1
267 :o2 ptr-pic-obverse-2
268 }))
270 (partition entry-size
271 (take (* entry-size pkmn-count)
272 (drop 0x383DE
273 rom))))))))
275 #+end_src
278 ** Pok\eacute{}mon evolutions
279 #+name: evolution-header
280 #+begin_src clojure
281 (defn format-evo
282 "Parse a sequence of evolution data, returning a map. First is the
283 method: 0 = end-evolution-data. 1 = level-up, 2 = item, 3 = trade. Next is an item id, if the
284 method of evolution is by item (only stones will actually make pokemon
285 evolve, for some auxillary reason.) Finally, the minimum level for
286 evolution to occur (level 1 means no limit, which is used for trade
287 and item evolutions), followed by the internal id of the pokemon
288 into which to evolve. Hence, level up and trade evolutions are
289 described with 3
290 bytes; item evolutions with four."
291 [coll]
292 (let [method (first coll)]
293 (cond (empty? coll) []
294 (= 0 method) [] ;; just in case
295 (= 1 method) ;; level-up evolution
296 (conj (format-evo (drop 3 coll))
297 {:method :level-up
298 :min-level (nth coll 1)
299 :into (dec (nth coll 2))})
301 (= 2 method) ;; item evolution
302 (conj (format-evo (drop 4 coll))
303 {:method :item
304 :item (dec (nth coll 1))
305 :min-level (nth coll 2)
306 :into (dec (nth coll 3))})
308 (= 3 method) ;; trade evolution
309 (conj (format-evo (drop 3 coll))
310 {:method :trade
311 :min-level (nth coll 1) ;; always 1 for trade.
312 :into (dec (nth coll 2))}))))
315 (defn hxc-ptrs-evolve
316 "A hardcoded collection of 190 pointers to alternating evolution/learnset data,
317 in internal order."
318 ([]
319 (hxc-ptrs-evolve com.aurellem.gb.gb-driver/original-rom))
320 ([rom]
321 (let [
322 pkmn-count (count (hxc-pokenames-raw)) ;; 190
323 ptrs
324 (map (fn [[a b]] (low-high a b))
325 (partition 2
326 (take (* 2 pkmn-count)
327 (drop 0x3b1e5 rom))))]
328 (map (partial + 0x34000) ptrs)
330 )))
331 #+end_src
333 #+name:evolution
334 #+begin_src clojure
336 (defn hxc-evolution
337 "Hardcoded evolution data in memory. The data exists at ROM@34000,
338 sorted by internal order. Pointers to the data exist at ROM@3B1E5; see also, hxc-ptrs-evolve."
339 ([] (hxc-evolution com.aurellem.gb.gb-driver/original-rom))
340 ([rom]
341 (apply assoc {}
342 (interleave
343 (hxc-pokenames rom)
344 (map
345 (comp
346 format-evo
347 (partial take-while (comp not zero?))
348 #(drop % rom))
349 (hxc-ptrs-evolve rom)
350 )))))
352 (defn hxc-evolution-pretty
353 "Like hxc-evolution, except it uses the names of items and pokemon
354 --- grabbed from ROM --- rather than their numerical identifiers."
355 ([] (hxc-evolution-pretty com.aurellem.gb.gb-driver/original-rom))
356 ([rom]
357 (let
358 [poke-names (vec (hxc-pokenames rom))
359 item-names (vec (hxc-items rom))
360 use-names
361 (fn [m]
362 (loop [ks (keys m) new-map m]
363 (let [k (first ks)]
364 (cond (nil? ks) new-map
365 (= k :into)
366 (recur
367 (next ks)
368 (assoc new-map
369 :into
370 (poke-names
371 (:into
372 new-map))))
373 (= k :item)
374 (recur
375 (next ks)
376 (assoc new-map
377 :item
378 (item-names
379 (:item new-map))))
380 :else
381 (recur
382 (next ks)
383 new-map)
384 ))))]
386 (into {}
387 (map (fn [[pkmn evo-coll]]
388 [pkmn (map use-names evo-coll)])
389 (hxc-evolution rom))))))
392 #+end_src
395 ** Level-up moves (learnsets)
396 #+name: learnsets
397 #+begin_src clojure
400 (defn hxc-learnsets
401 "Hardcoded map associating pokemon names to lists of pairs [lvl
402 move] of abilities they learn as they level up. The data
403 exists at ROM@34000, sorted by internal order. Pointers to the data
404 exist at ROM@3B1E5; see also, hxc-ptrs-evolve"
405 ([] (hxc-learnsets com.aurellem.gb.gb-driver/original-rom))
406 ([rom]
407 (apply assoc
408 {}
409 (interleave
410 (hxc-pokenames rom)
411 (map (comp
412 (partial map
413 (fn [[lvl mv]] [lvl (dec mv)]))
414 (partial partition 2)
415 ;; keep the learnset data
416 (partial take-while (comp not zero?))
417 ;; skip the evolution data
418 rest
419 (partial drop-while (comp not zero?)))
420 (map #(drop % rom)
421 (hxc-ptrs-evolve rom)))))))
423 (defn hxc-learnsets-pretty
424 "Live hxc-learnsets except it reports the name of each move --- as
425 it appears in rom --- rather than the move index."
426 ([] (hxc-learnsets-pretty com.aurellem.gb.gb-driver/original-rom))
427 ([rom]
428 (let [moves (vec(map format-name (hxc-move-names)))]
429 (into {}
430 (map (fn [[pkmn learnset]]
431 [pkmn (map (fn [[lvl mv]] [lvl (moves mv)])
432 learnset)])
433 (hxc-learnsets rom))))))
437 #+end_src
441 * Pok\eacute{}mon II : the Pok\eacute{}dex
442 ** Species vital stats
443 #+name: dex-stats
444 #+begin_src clojure
445 (defn hxc-pokedex-stats
446 "The hardcoded pokedex stats (species height weight) in memory. List
447 begins at ROM@40687"
448 ([] (hxc-pokedex-stats com.aurellem.gb.gb-driver/original-rom))
449 ([rom]
450 (let [pokedex-names (zipmap (range) (hxc-pokedex-names rom))
451 pkmn-count (count pokedex-names)
452 ]
453 ((fn capture-stats
454 [n stats data]
455 (if (zero? n) stats
456 (let [[species
457 [_
458 height-ft
459 height-in
460 weight-1
461 weight-2
462 _
463 dex-ptr-1
464 dex-ptr-2
465 dex-bank
466 _
467 & data]]
468 (split-with (partial not= 0x50) data)]
469 (recur (dec n)
470 (assoc stats
471 (pokedex-names (- pkmn-count (dec n)))
472 {:species
473 (format-name (character-codes->str species))
474 :height-ft
475 height-ft
476 :height-in
477 height-in
478 :weight
479 (/ (low-high weight-1 weight-2) 10.)
481 ;; :text
482 ;; (character-codes->str
483 ;; (take-while
484 ;; (partial not= 0x50)
485 ;; (drop
486 ;; (+ 0xB8000
487 ;; -0x4000
488 ;; (low-high dex-ptr-1 dex-ptr-2))
489 ;; rom)))
490 })
492 data)
495 )))
497 pkmn-count
498 {}
499 (drop 0x40687 rom))) ))
500 #+end_src
502 #+results: dex-stats
503 : #'com.aurellem.gb.hxc/hxc-pokedex-stats
505 ** Species synopses
507 #+name: dex-text
508 #+begin_src clojure
509 (def hxc-pokedex-text-raw
510 "The hardcoded pokedex entries in memory. List begins at
511 ROM@B8000, shortly before move names."
512 (hxc-thunk-words 0xB8000 14754))
517 (defn hxc-pokedex-text
518 "The hardcoded pokedex entries in memory, presented as an
519 associative hash map. List begins at ROM@B8000."
520 ([] (hxc-pokedex-text com.aurellem.gb.gb-driver/original-rom))
521 ([rom]
522 (zipmap
523 (hxc-pokedex-names rom)
524 (cons nil ;; for missingno.
525 (hxc-pokedex-text-raw rom)))))
526 #+end_src
529 ** Pok\eacute{}mon cries
530 #+name: pokecry
531 #+begin_src clojure
532 (defn hxc-cry
533 "The pokemon cry data in internal order. List begins at ROM@39462"
534 ([](hxc-cry com.aurellem.gb.gb-driver/original-rom))
535 ([rom]
536 (zipmap
537 (hxc-pokenames rom)
538 (map
539 (fn [[cry-id pitch length]]
540 {:cry-id cry-id
541 :pitch pitch
542 :length length}
543 )
544 (partition 3
545 (drop 0x39462 rom))))))
547 (defn hxc-cry-groups
548 ([] (hxc-cry-groups com.aurellem.gb.gb-driver/original-rom))
549 ([rom]
550 (map #(mapv first
551 (filter
552 (fn [[k v]]
553 (= % (:cry-id v)))
554 (hxc-cry)))
555 ((comp
556 range
557 count
558 set
559 (partial map :cry-id)
560 vals
561 hxc-cry)
562 rom))))
565 (defn cry-conversion!
566 "Convert Porygon's cry in ROM to be the cry of the given pokemon."
567 [pkmn]
568 (write-rom!
569 (rewrite-memory
570 (vec(rom))
571 0x3965D
572 (map second
573 ((hxc-cry) pkmn)))))
575 #+end_src
577 ** COMMENT Names of permanent stats
578 0DD4D-DD72
580 * Items
581 ** Item names
583 *** See the data
584 #+begin_src clojure :exports both :results output
585 (ns com.aurellem.gb.hxc
586 (:use (com.aurellem.gb assembly characters gb-driver util mem-util
587 constants))
588 (:import [com.aurellem.gb.gb_driver SaveState]))
590 (println (take 100 (drop 0x045B7 (rom))))
592 (println
593 (partition-by
594 (partial = 0x50)
595 (take 100 (drop 0x045B7 (rom)))))
597 (println
598 (map character-codes->str
599 (partition-by
600 (partial = 0x50)
601 (take 100 (drop 0x045B7 (rom))))))
604 #+end_src
606 #+results:
607 : (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)
608 : ((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))
609 : (MASTER BALL # ULTRA BALL # GREAT BALL # POKé BALL # TOWN MAP # BICYCLE # ????? # SAFARI BALL # POKéDEX # MOON STONE # AN)
611 *** Automatically grab the data
612 #+name: item-names
613 #+begin_src clojure
615 (def hxc-items-raw
616 "The hardcoded names of the items in memory. List begins at
617 ROM@045B7"
618 (hxc-thunk-words 0x45B7 870))
620 (def hxc-items
621 "The hardcoded names of the items in memory, presented as
622 keywords. List begins at ROM@045B7. See also, hxc-items-raw."
623 (comp (partial map format-name) hxc-items-raw))
624 #+end_src
626 ** Item prices
628 ***
629 #+begin_src clojure :exports both :results output
630 (ns com.aurellem.gb.hxc
631 (:use (com.aurellem.gb assembly characters gb-driver util mem-util
632 constants))
633 (:import [com.aurellem.gb.gb_driver SaveState]))
635 (println (take 90 (drop 0x4495 (rom))))
637 (println
638 (partition 3
639 (take 90 (drop 0x4495 (rom)))))
641 (println
642 (partition 3
643 (map hex
644 (take 90 (drop 0x4495 (rom))))))
646 (println
647 (map decode-bcd
648 (map butlast
649 (partition 3
650 (take 90 (drop 0x4495 (rom)))))))
652 (println
653 (map
654 vector
655 (hxc-items (rom))
656 (map decode-bcd
657 (map butlast
658 (partition 3
659 (take 90 (drop 0x4495 (rom))))))))
665 #+end_src
667 #+results:
668 : (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)
669 : ((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))
670 : ((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))
671 : (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)
672 : ([: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])
675 ***
676 #+name: item-prices
677 #+begin_src clojure
678 (defn hxc-item-prices
679 "The hardcoded list of item prices in memory. List begins at ROM@4495"
680 ([] (hxc-item-prices com.aurellem.gb.gb-driver/original-rom))
681 ([rom]
682 (let [items (hxc-items rom)
683 price-size 3]
684 (zipmap items
685 (map (comp
686 ;; zero-cost items are "priceless"
687 #(if (zero? %) :priceless %)
688 decode-bcd butlast)
689 (partition price-size
690 (take (* price-size (count items))
691 (drop 0x4495 rom))))))))
692 #+end_src
693 ** Vendor inventories
695 #+name: item-vendors
696 #+begin_src clojure
697 (defn hxc-shops
698 ([] (hxc-shops com.aurellem.gb.gb-driver/original-rom))
699 ([rom]
700 (let [items (zipmap (range) (hxc-items rom))
702 ;; temporarily softcode the TM items
703 items (into
704 items
705 (map (juxt identity
706 (comp keyword
707 (partial str "tm-")
708 (partial + 1 -200)
709 ))
710 (take 200 (drop 200 (range)))))
712 ]
714 ((fn parse-shop [coll [num-items & items-etc]]
715 (let [inventory (take-while
716 (partial not= 0xFF)
717 items-etc)
718 [separator & items-etc] (drop num-items (rest items-etc))]
719 (if (= separator 0x50)
720 (map (partial mapv (comp items dec)) (conj coll inventory))
721 (recur (conj coll inventory) items-etc)
722 )
723 ))
725 '()
726 (drop 0x233C rom))
729 )))
730 #+end_src
732 #+results: item-vendors
733 : #'com.aurellem.gb.hxc/hxc-shops
737 * Types
738 ** Names of types
740 *** COMMENT Pointers to type names
741 #+begin_src clojure :exports both :results output
742 (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)))))
743 #+end_src
746 ***
747 #+begin_src clojure :exports both :results output
748 (ns com.aurellem.gb.hxc
749 (:use (com.aurellem.gb assembly characters gb-driver util mem-util
750 constants))
751 (:import [com.aurellem.gb.gb_driver SaveState]))
753 (println (take 90 (drop 0x27D99 (rom))))
755 (println
756 (partition-by (partial = 0x50)
757 (take 90 (drop 0x27D99 (rom)))))
759 (println
760 (map character-codes->str
761 (partition-by (partial = 0x50)
762 (take 90 (drop 0x27D99 (rom))))))
764 #+end_src
766 #+results:
767 : (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)
768 : ((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))
769 : (NORMAL # FIGHTING # FLYING # POISON # FIRE # WATER # GRASS # ELECTRIC # PSYCHIC # ICE # GROUND # ROCK # BIRD # BUG # G)
772 ***
773 #+name: type-names
774 #+begin_src clojure
775 (def hxc-types
776 "The hardcoded type names in memory. List begins at ROM@27D99,
777 shortly before hxc-titles."
778 (hxc-thunk-words 0x27D99 102))
780 #+end_src
782 ** Type effectiveness
783 ***
784 #+begin_src clojure :exports both :results output
785 (ns com.aurellem.gb.hxc
786 (:use (com.aurellem.gb assembly characters gb-driver util mem-util
787 constants))
788 (:import [com.aurellem.gb.gb_driver SaveState]))
791 ;; POKEMON TYPES
793 (println pkmn-types) ;; these are the pokemon types
794 (println (map vector (range) pkmn-types)) ;; each type has an id number.
796 (newline)
801 ;;; TYPE EFFECTIVENESS
803 (println (take 15 (drop 0x3E62D (rom))))
804 (println (partition 3 (take 15 (drop 0x3E62D (rom)))))
806 (println
807 (map
808 (fn [[atk-type def-type multiplier]]
809 (list atk-type def-type (/ multiplier 10.)))
811 (partition 3
812 (take 15 (drop 0x3E62D (rom))))))
815 (println
816 (map
817 (fn [[atk-type def-type multiplier]]
818 [
819 (get pkmn-types atk-type)
820 (get pkmn-types def-type)
821 (/ multiplier 10.)
822 ])
824 (partition 3
825 (take 15 (drop 0x3E62D (rom))))))
827 #+end_src
829 #+results:
830 : [: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]
831 : ([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])
832 :
833 : (0 5 5 0 8 0 8 8 20 20 7 20 20 5 5)
834 : ((0 5 5) (0 8 0) (8 8 20) (20 7 20) (20 5 5))
835 : ((0 5 0.5) (0 8 0.0) (8 8 2.0) (20 7 2.0) (20 5 0.5))
836 : ([:normal :rock 0.5] [:normal :ghost 0.0] [:ghost :ghost 2.0] [:fire :bug 2.0] [:fire :rock 0.5])
839 ***
841 #+name: type-advantage
842 #+begin_src clojure
843 (defn hxc-advantage
844 ;; in-game multipliers are stored as 10x their effective value
845 ;; to allow for fractional multipliers like 1/2
847 "The hardcoded type advantages in memory, returned as tuples of
848 atk-type def-type multiplier. By default (i.e. if not listed here),
849 the multiplier is 1. List begins at 0x3E62D."
850 ([] (hxc-advantage com.aurellem.gb.gb-driver/original-rom))
851 ([rom]
852 (map
853 (fn [[atk def mult]] [(get pkmn-types atk (hex atk))
854 (get pkmn-types def (hex def))
855 (/ mult 10)])
856 (partition 3
857 (take-while (partial not= 0xFF)
858 (drop 0x3E62D rom))))))
859 #+end_src
863 * Moves
864 ** Names of moves
865 *** See the data
866 #+begin_src clojure :exports both :results output
867 (ns com.aurellem.gb.hxc
868 (:use (com.aurellem.gb assembly characters gb-driver util mem-util
869 constants))
870 (:import [com.aurellem.gb.gb_driver SaveState]))
872 (println (take 100 (drop 0xBC000 (rom))))
874 (println
875 (partition-by
876 (partial = 0x50)
877 (take 100 (drop 0xBC000 (rom)))))
879 (println
880 (map character-codes->str
881 (partition-by
882 (partial = 0x50)
883 (take 100 (drop 0xBC000 (rom))))))
886 #+end_src
888 #+results:
889 : (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)
890 : ((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))
891 : (POUND # KARATE CHOP # DOUBLESLAP # COMET PUNCH # MEGA PUNCH # PAY DAY # FIRE PUNCH # ICE PUNCH # THUNDERPUNCH # SCRATC)
893 *** Automatically grab the data
895 #+name: move-names
896 #+begin_src clojure
897 (def hxc-move-names
898 "The hardcoded move names in memory. List begins at ROM@BC000"
899 (hxc-thunk-words 0xBC000 1551))
900 #+end_src
902 ** Properties of moves
904 #+name: move-data
905 #+begin_src clojure
906 (defn hxc-move-data
907 "The hardcoded (basic (move effects)) in memory. List begins at
908 0x38000. Returns a map of {:name :power :accuracy :pp :fx-id
909 :fx-txt}. The move descriptions are handwritten, not hardcoded."
910 ([]
911 (hxc-move-data com.aurellem.gb.gb-driver/original-rom))
912 ([rom]
913 (let [names (vec (hxc-move-names rom))
914 move-count (count names)
915 move-size 6
916 types pkmn-types ;;; !! hardcoded types
917 ]
918 (zipmap (map format-name names)
919 (map
920 (fn [[idx effect power type-id accuracy pp]]
921 {:name (names (dec idx))
922 :power power
923 :accuracy accuracy
924 :pp pp
925 :type (types type-id)
926 :fx-id effect
927 :fx-txt (get move-effects effect)
928 }
929 )
931 (partition move-size
932 (take (* move-size move-count)
933 (drop 0x38000 rom))))))))
937 (defn hxc-move-data*
938 "Like hxc-move-data, but reports numbers as hexadecimal symbols instead."
939 ([]
940 (hxc-move-data* com.aurellem.gb.gb-driver/original-rom))
941 ([rom]
942 (let [names (vec (hxc-move-names rom))
943 move-count (count names)
944 move-size 6
945 format-name (fn [s]
946 (keyword (.toLowerCase
947 (apply str
948 (map #(if (= % \space) "-" %) s)))))
949 ]
950 (zipmap (map format-name names)
951 (map
952 (fn [[idx effect power type accuracy pp]]
953 {:name (names (dec idx))
954 :power power
955 :accuracy (hex accuracy)
956 :pp pp
957 :fx-id (hex effect)
958 :fx-txt (get move-effects effect)
959 }
960 )
962 (partition move-size
963 (take (* move-size move-count)
964 (drop 0x38000 rom))))))))
966 #+end_src
968 ** TM and HM moves
969 ***
970 #+begin_src clojure :exports both :results output
971 (ns com.aurellem.gb.hxc
972 (:use (com.aurellem.gb assembly characters gb-driver util mem-util
973 constants))
974 (:import [com.aurellem.gb.gb_driver SaveState]))
977 (println (hxc-move-names))
978 (println (map vector (rest(range)) (hxc-move-names)))
980 (newline)
982 (println (take 55 (drop 0x1232D (rom))))
984 (println
985 (interpose "."
986 (map
987 (zipmap (rest (range)) (hxc-move-names))
988 (take 55 (drop 0x1232D (rom))))))
990 #+end_src
992 #+results:
993 : (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)
994 : ([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])
995 :
996 : (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)
997 : (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)
1000 ***
1001 #+name: machines
1002 #+begin_src clojure
1003 (defn hxc-machines
1004 "The hardcoded moves taught by TMs and HMs. List begins at ROM@1232D."
1005 ([] (hxc-machines
1006 com.aurellem.gb.gb-driver/original-rom))
1007 ([rom]
1008 (let [moves (hxc-move-names rom)]
1009 (zipmap
1010 (range)
1011 (take-while
1012 (comp not nil?)
1013 (map (comp
1014 format-name
1015 (zipmap
1016 (range)
1017 moves)
1018 dec)
1019 (take 100
1020 (drop 0x1232D rom))))))))
1022 #+end_src
1028 ** COMMENT Status ailments
1031 * NPC Trainers
1033 ** Trainer Pok\eacute{}mon
1034 # http://hax.iimarck.us/topic/103/
1035 There are two formats for specifying lists of NPC PPok\eacute{}mon:
1036 - If all the Pok\eacute{}mon will have the same level, the format is
1037 - Level (used for all the Pok\eacute{}mon)
1038 - Any number of Pok\eacute{}mon internal ids.
1039 - 0x00, to indicate end-of-list.
1040 - Otherwise, all the Pok\eacute{}mon will have their level
1041 specified. The format is
1042 - 0xFF, to indicate that we will be specifying the levels individually[fn::Because 0xFF is a
1043 forbidden level within the usual gameplay discipline, the game
1044 makers could safely use 0xFF as a mode indicator.].
1045 - Any number of alternating Level/Pokemon pairs
1046 - 0x00, to indicate end-of-list.
1048 *** Get the pointers
1049 *** See the data
1050 #+begin_src clojure :exports both :results output
1051 (ns com.aurellem.gb.hxc
1052 (:use (com.aurellem.gb assembly characters gb-driver util mem-util
1053 constants))
1054 (:import [com.aurellem.gb.gb_driver SaveState]))
1056 (->>
1057 (rom)
1058 (drop 0x39E2F)
1059 (take 21)
1060 (println))
1063 (->>
1064 (rom)
1065 (drop 0x39E2F)
1066 (take 21)
1067 (partition-by zero?)
1068 (take-nth 2)
1069 (println))
1073 (let
1074 [pokenames
1075 (zipmap
1076 (rest (range))
1077 (hxc-pokenames-raw))]
1079 (->>
1080 (rom)
1081 (drop 0x39E2F)
1082 (take 21) ;; (1922 in all)
1083 (partition-by zero?)
1084 (take-nth 2)
1085 (map
1086 (fn parse-team [[mode & team]]
1087 (if (not= 0xFF mode)
1088 (mapv
1089 #(hash-map :level mode :species (pokenames %))
1090 team)
1092 (mapv
1093 (fn [[lvl id]] (hash-map :level lvl :species (pokenames id)))
1094 (partition 2 team)))))
1096 (println)))
1100 #+end_src
1102 #+results:
1103 : (11 165 108 0 14 5 0 10 165 165 107 0 14 165 108 107 0 15 165 5 0)
1104 : ((11 165 108) (14 5) (10 165 165 107) (14 165 108 107) (15 165 5))
1105 : ([{:species RATTATA, :level 11} {:species EKANS, :level 11}] [{:species SPEAROW, :level 14}] [{:species RATTATA, :level 10} {:species RATTATA, :level 10} {:species ZUBAT, :level 10}] [{:species RATTATA, :level 14} {:species EKANS, :level 14} {:species ZUBAT, :level 14}] [{:species RATTATA, :level 15} {:species SPEAROW, :level 15}])
1107 * Places
1108 ** Names of places
1110 #+name: places
1111 #+begin_src clojure
1112 (def hxc-places
1113 "The hardcoded place names in memory. List begins at
1114 ROM@71500. [Cinnabar/Celadon] Mansion seems to be dynamically calculated."
1115 (hxc-thunk-words 0x71500 560))
1116 #+end_src
1118 *** See it work
1119 #+begin_src clojure :exports both :results output
1120 (println (hxc-places))
1121 #+end_src
1123 #+results:
1124 : (PALLET TOWN VIRIDIAN CITY PEWTER CITY CERULEAN CITY LAVENDER TOWN VERMILION CITY CELADON CITY FUCHSIA CITY CINNABAR ISLAND INDIGO PLATEAU SAFFRON CITY ROUTE 1 ROUTE 2 ROUTE 3 ROUTE 4 ROUTE 5 ROUTE 6 ROUTE 7 ROUTE 8 ROUTE 9 ROUTE 10 ROUTE 11 ROUTE 12 ROUTE 13 ROUTE 14 ROUTE 15 ROUTE 16 ROUTE 17 ROUTE 18 SEA ROUTE 19 SEA ROUTE 20 SEA ROUTE 21 ROUTE 22 ROUTE 23 ROUTE 24 ROUTE 25 VIRIDIAN FOREST MT.MOON ROCK TUNNEL SEA COTTAGE S.S.ANNE [POKE]MON LEAGUE UNDERGROUND PATH [POKE]MON TOWER SEAFOAM ISLANDS VICTORY ROAD DIGLETT's CAVE ROCKET HQ SILPH CO. [0x4A] MANSION SAFARI ZONE)
1126 ** Wild Pok\eacute{}mon demographics
1127 #+name: wilds
1128 #+begin_src clojure
1132 (defn hxc-ptrs-wild
1133 "A list of the hardcoded wild encounter data in memory. Pointers
1134 begin at ROM@0CB95; data begins at ROM@0x04D89"
1135 ([] (hxc-ptrs-wild com.aurellem.gb.gb-driver/original-rom))
1136 ([rom]
1137 (let [ptrs
1138 (map (fn [[a b]] (+ a (* 0x100 b)))
1139 (take-while (partial not= (list 0xFF 0xFF))
1140 (partition 2 (drop 0xCB95 rom))))]
1141 ptrs)))
1145 (defn hxc-wilds
1146 "A list of the hardcoded wild encounter data in memory. Pointers
1147 begin at ROM@0CB95; data begins at ROM@0x04D89"
1148 ([] (hxc-wilds com.aurellem.gb.gb-driver/original-rom))
1149 ([rom]
1150 (let [pokenames (zipmap (range) (hxc-pokenames rom))]
1151 (map
1152 (partial map (fn [[a b]] {:species (pokenames (dec b)) :level
1153 a}))
1154 (partition 10
1156 (take-while (comp (partial not= 1)
1157 first)
1158 (partition 2
1159 (drop 0xCD8C rom))
1161 ))))))
1163 #+end_src
1168 ** Map data
1170 # http://www.pokecommunity.com/showthread.php?t=235311
1171 # http://datacrystal.romhacking.net/wiki/Pokemon_Red/Blue:Notes
1173 #+name map
1174 #+begin_src clojure :exports both :results output
1175 (ns com.aurellem.gb.hxc
1176 (:use (com.aurellem.gb assembly characters gb-driver util mem-util
1177 constants))
1178 (:import [com.aurellem.gb.gb_driver SaveState]))
1181 (defn parse-header-tileset
1182 [[bank# ;; memory bank for blocks & tileset
1184 blocks-lo ;; structure
1185 blocks-hi
1187 tileset-lo ;; style
1188 tileset-hi
1190 collision-lo ;; collision info
1191 collision-hi
1193 talk-here-1 ;; positions of up to three
1194 talk-here-2 ;; talk-over-countertop tiles
1195 talk-here-3 ;; --- 0xFF if unused.
1197 grass ;; grass tile --- 0xFF if unused
1199 animation-flags ;; settings for animation
1200 & _]]
1202 [bank#
1204 blocks-lo ;; structure
1205 blocks-hi
1207 tileset-lo ;; style
1208 tileset-hi
1210 collision-lo ;; collision info
1211 collision-hi
1213 talk-here-1 ;; positions of up to three
1214 talk-here-2 ;; talk-over-countertop tiles
1215 talk-here-3 ;; --- 0xFF if unused.
1217 grass ;; grass tile --- 0xFF if unused
1219 animation-flags ;; settings for animation
1220 ])
1224 (defn parse-header-map
1225 [start]
1227 (let [connection-size 11
1229 [tileset-index
1230 map-height
1231 map-width
1232 layout-lo
1233 layout-hi
1234 text-lo
1235 text-hi
1236 script-lo
1237 script-hi
1238 adjacency-flags ;; x x x x N S W E
1239 & etc]
1240 (drop start (rom))
1242 [east? west? south? north?]
1243 (bit-list adjacency-flags)
1245 [connections object-data]
1246 (split-at
1247 (* connection-size (+ east? west? south? north?))
1248 etc)
1250 connections
1251 (partition connection-size connections)
1257 (ptr->offset
1259 (low-high layout-lo layout-hi))
1262 ))
1263 #+end_src
1265 #+results:
1270 * Appendices
1271 ** Mapping the ROM
1272 # D3AD: Script:Use Pokeball?
1274 | ROM address (hex) | Description | Format | Example |
1275 |-----------------------+-----------------+-----------------+-----------------|
1276 | | <15> | <15> | <15> |
1277 | 01823-0184A | Important prefix strings. | Variable-length strings, separated by 0x50. | TM#TRAINER#PC#ROCKET#POK\eacute{}#... |
1278 | 0233C- | Shop inventories. | | |
1279 | 02F47- | (?) Move ids of some HM moves. | One byte per move id | 0x0F 0x13 0x39 0x46 0x94 0xFF, the move ids of CUT, FLY, SURF, STRENGTH, FLASH, then cancel. |
1280 | 03E59- | Start of the Give-Pok\eacute{}mon script. | Assembly. When called, converts the contents of register BC into a Pok\eacute{}mon: (B) is the level; (C) is the species. | |
1281 | 03E3F- | Start of the give-item script. | Assembly. When called, converts the contents of register BC into an item: (B) is the item type; (C) is the quantity. | |
1282 | 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. |
1283 | 04524-04527 | (unconfirmed) possibly the bike price in Cerulean. | | |
1284 | 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#... |
1285 | 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). |
1286 |-----------------------+-----------------+-----------------+-----------------|
1287 | 05DD2-05DF2 | Menu text for player info. | | PLAYER [newline] BADGES [nelwine] POK\Eacute{}DEX [newline] TIME [0x50] |
1288 | 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. |
1289 | 06698- | ? Background music. | | |
1290 |-----------------------+-----------------+-----------------+-----------------|
1291 | 7550-7570 | Menu options for map directions[fn:unused:According to [[http://tcrf.net/Pok%C3%A9mon_Red_and_Blue#NORTH.2FWEST.2FSOUTH.2FEAST][The Cutting Room Floor]], this data is unused. ]. | Variable-length strings. | NORTH [newline] WEST [0x50] SOUTH [newline] EAST [0x50] NORTH [newline] EAST[0x50] |
1292 | 7570-757D | Menu options for trading Pok\eacute{}mon | | TRADE [newline] CANCEL [0x50] |
1293 | 757D-758A | Menu options for healing Pok\eacute{}mon | | HEAL [newline] CANCEL [0x50] |
1294 | 7635- | Menu options for selected Pok\eacute{}mon (Includes names of out-of-battle moves). | Variable-length strings separated by 0x50. | CUT [0x50] FLY [0x50] SURF [0x50] STRENGTH [0x50] FLASH [0x50] DIG [0x50] TELEPORT [0x50] SOFTBOILED [0x50] STATS [newline] SWITCH [newline] CANCEL [0x50] |
1295 | 7AF0-8000 | (empty space) | | 0 0 0 0 0 ... |
1296 | 0822E-082F? | Pointers to background music, part I. | | |
1297 | 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). |
1298 |-----------------------+-----------------+-----------------+-----------------|
1299 | 0DACB. | Amount of HP restored by Soda Pop | The HP consists of a single numerical byte. | 60 |
1300 | 0DACF. | Amount of HP restored by Lemonade | " | 80 |
1301 | 0DAD5. | Amount of HP restored by Fresh Water | " | 50 |
1302 | 0DADB. | Amount of HP restored by Hyper Potion. | " | 200 |
1303 | 0DAE0. | Amount of HP restored by Super Potion. | " | 50 |
1304 | 0DAE3. | Amount of HP restored by Potion. | " | 20 |
1305 |-----------------------+-----------------+-----------------+-----------------|
1306 | 0DD4D-DD72 | Names of permanent stats. | Variable-length strings separated by 0x50. | #HEALTH#ATTACK#DEFENSE#SPEED#SPECIAL# |
1307 |-----------------------+-----------------+-----------------+-----------------|
1308 | 0DE2F. | Duration of Repel. | A single byte, representing the number of steps you can take before the effect wears off. | 100 |
1309 | 0DF39. | Duration of Super Repel. | " | 200 |
1310 | 0DF3E. | Duration of Max Repel. | " | 250 |
1311 |-----------------------+-----------------+-----------------+-----------------|
1312 | 1164B- | Terminology for the Pok\eacute{}mon menu. | Contiguous, variable-length strings. | TYPE1[newline]TYPE2[newline] *№*,[newline]OT,[newline][0x50]STATUS,[0x50]OK |
1313 | 116DE- | Terminology for permanent stats in the Pok\eacute{}mon menu. | Contiguous, variable-length strings. | ATTACK[newline]DEFENSE[newline]SPEED[newline]SPECIAL[0x50] |
1314 | 11852- | Terminology for current stats in the Pok\eacute{}mon menu. | Contiguous, variable-length strings. | EXP POINTS[newline]LEVEL UP[0x50] |
1315 | 1195C-1196A | The two terms for being able/unable to learn a TM/HM. | Variable-length strings separated by 0x50. | ABLE#NOT ABLE# |
1316 | 119C0-119CE | The two terms for being able/unable to evolve using the current stone. | Variable-length strings separated by 0x50. | ABLE#NOT ABLE# |
1317 |-----------------------+-----------------+-----------------+-----------------|
1318 | 11D53. | Which badge is a prerequisite for CUT? | op code: which bit of A to test? | When this script is called, the bits of A contain your badges, and this op code will check a certain bit of A. The op codes for the badges are, in order, [0x47 0x4F 0x57 0x5F 0x67 0x6F 0x77 0x7F]. |
1319 | 11D67. | Which badge is a prerequisite for SURF? | " | 0x67 (test for Soul Badge) |
1320 | 11DAC. | Which badge is a prerequisite for STRENGTH? | " | 0x5F (test for Rainbow Badge) |
1321 |-----------------------+-----------------+-----------------+-----------------|
1322 | 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), ... |
1323 |-----------------------+-----------------+-----------------+-----------------|
1324 | 1CF8B-1CF8C | Which Pok\eacute{}mon does Melanie give you in Cerulean City? | A level/internal-id pair. | (10 153), corresponding to a level 10 Bulbasaur. |
1325 | 1D651-1D652 | Which Pok\eacute{}mon do you find at the top of Celadon Mansion? | A level/internal-id pair. | (25 102), corresponding to a level 25 Eevee. |
1326 |-----------------------+-----------------+-----------------+-----------------|
1327 | 27D56 & 27D57. | Pointer to the pointers to type names. | A single low-byte, high-byte pair. | 0x63 0x7D, corresponding to location 27D63\mdash{} the start of the next entry. |
1328 | 27D63-27D99 | Pointers to type names. | Each point is a low-byte, high-byte pair. The type names follows immediately after this section; see below. | The first pointer is [0x99 0x7D], corresponding to the location 27D99 ("NORMAL"). |
1329 | 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#... |
1330 | 27DFF-27E77 | ? | 120 bytes of unknown data. | |
1331 | 27E77- | Trainer title names. | Variable-length names separated by 0x50. | YOUNGSTER#BUG CATCHER#LASS#... |
1332 | 34000- | | | |
1333 | 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. |
1334 | 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. | | |
1335 | 39462- | The Pok\eacute{}mon cry data. | Fixed-length (3 byte) descriptions of cries. | |
1336 |-----------------------+-----------------+-----------------+-----------------|
1337 | 3997D-39B05 | Trainer titles (extended; see 27E77). This list includes strictly more trainers, seemingly at random inserted into the list from 27E77.[fn::The names added are in bold: YOUNGSTER, BUG CATCHER, LASS, *SAILOR*, JR TRAINER(m), JR TRAINER(f), POK\eacute{}MANIAC, SUPER NERD, *HIKER*, *BIKER*, BURGLAR, ENGINEER, JUGGLER, *FISHERMAN*, SWIMMER, *CUE BALL*, *GAMBLER*, BEAUTY, *PSYCHIC*, ROCKER, JUGGLER (again), *TAMER*, *BIRDKEEPER*, BLACKBELT, *RIVAL1*, PROF OAK, CHIEF, SCIENTIST, *GIOVANNI*, ROCKET, COOLTRAINER(m), COOLTRAINER(f), *BRUNO*, *BROCK*, *MISTY*, *LT. SURGE*, *ERIKA*, *KOGA*, *BLAINE*, *SABRINA*, *GENTLEMAN*, *RIVAL2*, *RIVAL3*, *LORELEI*, *CHANNELER*, *AGATHA*, *LANCE*.] | | |
1338 | 39B05-39DD0. | unknown | | |
1339 | 39DD1-39E2E | Pointers to trainer Pok\eacute{}mon | Pairs of low-high bits. | The first pair is 0x2F 0x5E, which corresponds to memory location 5E2F relative to this 38000-3C000 bank, i.e.[fn::For details about how relative bank pointers work, see the relevant Appendix.] position 39E2F overall. |
1340 | 39E2F-3A5B2 | Trainer Pok\eacute{}mon | Specially-formatted lists of various length, separated by 0x00. If the list starts with 0xFF, the rest of the list will alternate between levels and internal-ids. Otherwise, start of the list is the level of the whole team, and the rest of the list is internal-ids. | The first entry is (11 165 108 0), which means a level 11 team consisting of Rattata and Ekans. The entry for MISTY is (255 18 27 21 152 0), which means a team of various levels consisting of level 18 Staryu and level 21 Starmie. [fn::Incidentally, if you want to change your rival's starter Pok\eacute{}mon, it's enough just to change its species in all of your battles with him.].) |
1341 | 3B1E5-3B361 | Pointers to evolution/learnset data. | One high-low byte pair for each of the 190 Pok\eacute{}mon in internal order. | |
1342 |-----------------------+-----------------+-----------------+-----------------|
1343 | 3B361-3BBAA | Evolution and learnset data. [fn::Evolution data consists of how to make Pok\eacute{}mon evolve, and what they evolve into. Learnset data consists of the moves that Pok\eacute{}mon learn as they level up.] | Variable-length evolution information (see below), followed by a list of level/move-id learnset pairs. | |
1344 | 3BBAA-3C000 | (empty) | | 0 0 0 0 ... |
1345 |-----------------------+-----------------+-----------------+-----------------|
1346 | 3D131-3D133 | The inventory of both OLD MAN and PROF. OAK when they battle for you. | Pairs of [item-id quantity], terminated by 0xFF. | (0x04 0x01 0xFF) They only have one Pok\eacute{}ball [fn::If you give them any ball, OAK will catch the enemy Pok\eacute{}mon and OLD MAN will miss. (OLD MAN misses even if he throws a MASTER BALL, which is a sight to see!) If you give them some other item first in the list, you'll be able to use that item normally but then you'll trigger the Safari Zone message: Pa will claim you're out of SAFARI BALLs and the battle will end. If you engage in either an OLD MAN or OAK battle with a Gym Leader, you will [1] get reprimanded if you try to throw a ball [2] incur the Safari Zone message [3] automatically win no matter which item you use [4] earn whichever reward they give you as usual [5] permanently retain the name OLD MAN / PROF. OAK.]. |
1347 | 3D6C7-3D6D6 | Two miscellaneous strings. | Variable length, separated by 0x50 | Disabled!#TYPE |
1348 | 3E190-3E194 | Which moves have an increased critical-hit ratio? | List of move ids, terminated by 0xFF. | (0x02 0x4B 0x98 0xA3 0xFF) corresponding to karate-chop, razor-leaf, crabhammer, slash, end-of-list. |
1349 | 3E200-3E204 | " (???) | " | " |
1350 | 3E231. | Besides normal-type, which type of move can COUNTER counter? | A single byte representing a type id. | This is set to 1, the id of the FIGHTING type. |
1351 |-----------------------+-----------------+-----------------+-----------------|
1352 | 40252-4027B | Pok\eacute{}dex menu text | Variable-length strings separated by 0x50. | SEEN#OWN#CONTENTS#... |
1353 | 40370-40386 | Important constants for Pok\eacute{}dex entries | | HT _ _ *?′??″* [newline] WT _ _ _ *???* lb [0x50] *POK\Eacute{}* [0x50] |
1354 | 40687-41072 | Species data from the Pok\eacute{}dex: species name, height, weight, etc. | Variable-length species names, followed by 0x50, followed by fixed-length height/weight/etc. data. | The first entry is (*146 132 132 131*, 80, *2 4*, *150 0*, 23, 0 64 46, 80), which are the the stats of Bulbasaur: the first entry spells "SEED", then 0x80, then the height (2' 4"), then the weight (formatted as a low-high byte pair), then various Pokédex pointer data (see elsewhere). |
1355 | 41072- | Pok\eacute{} placeholder species, "???" | | |
1356 |-----------------------+-----------------+-----------------+-----------------|
1357 | 410B1-4116F | A conversion table between internal order and Pokedex order. | 190 bytes, corresponding to the Pok\eacute{}dex numbers of the 190 Pok\eacute{}mon listed in internal order. All =MISSINGNO.= are assigned a Pok\eacute{}dex number of 0. | The first few entries are (112 115 32 35 21 100 34 80 2 ...), which are the Pok\eacute{}dex numbers of Rhydon, Kangaskhan, Nidoran(m), Clefairy, Spearow, Voltorb, Nidoking, Slobrow, and Ivysaur. |
1358 |-----------------------+-----------------+-----------------+-----------------|
1359 | 509B4-509E0 | Saffron City's adjacency info. | Four adjacency lists, each 11 bytes long. (For more info on adjacency lists a.k.a. connection data, see [[http://datacrystal.romhacking.net/wiki/Pokemon_Red/Blue:Notes][here]]) | The first adjacency list is (0x10 0x70 0x46 0xF0 0xC6 0x0A 0x0A 0x23 0xF6 0x09 0xC8) |
1360 |-----------------------+-----------------+-----------------+-----------------|
1361 | 515AE-515AF | Which Pok\eacute{}mon does the trainer near Route 25 give you? | A level/internal-id pair. | (10 176) corresponding to a level 10 Charmander. |
1362 | 51DD5-51DD6 | Which Pok\eacute{}mon does the Silph Co. trainer give you? | A level/internal-id pair. | (15 19) corresponding to a level 15 Lapras. |
1363 |-----------------------+-----------------+-----------------+-----------------|
1364 | 527BA-527DB | The costs and kinds of prizes from Celadon Game Corner. | The following pattern repeats three times, once per window[fn::For the first two prize lists, ids are interpreted as Pok\eacute{}mon ids. For the last prize list, ids are (somehow) interpreted as item ids.]: Internal ids / 0x50 / Prices (two bytes of BCD)/ 0x50. | (0x94 0x52 0x65 0x50) Abra Vulpix Wigglytuff (0x02 0x30 0x10 0x00 0x26 0x80) 230C, 1000C, 2680C |
1365 | 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]... |
1366 |-----------------------+-----------------+-----------------+-----------------|
1367 | 70295- | Hall of fame | The text "HALL OF FAME" | |
1368 | 70442- | Play time/money | The text "PLAY TIME [0x50] MONEY" | |
1369 | 71500-7174B | Names of places. | Variable-length place names (strings), separated by 0x50. | PALLET TOWN#VIRIDIAN CITY#PEWTER CITY#CERULEAN CITY#... |
1370 | 71C1E-71CAA (approx.) | Tradeable NPC Pok\eacute{}mon. | Internal ID, followed by nickname (11 chars; extra space padded by 0x50). Some of the Pokemon have unknown extra data around the id. | The first entry is [0x76] "GURIO######", corresponding to a Dugtrio named "GURIO". |
1371 | 7C249-7C2?? | Pointers to background music, pt II. | | |
1372 |-----------------------+-----------------+-----------------+-----------------|
1373 | 98000-B7190 | Dialogue and other messsages. | Variable-length strings. | |
1374 | B7190-B8000 | (empty space) | | 0 0 0 0 0 ... |
1375 | B8000-BC000 | The text of each Pok\eacute{}mon's Pok\eacute{}dex entry. | Variable-length descriptions (strings) in Pok\eacute{}dex order, separated by 0x50. These entries use the special characters *0x49* (new page), *0x4E* (new line), and *0x5F* (end entry). | The first entry (Bulbasaur's) is: "It can go for days [0x4E] without eating a [0x4E] single morsel. [0x49] In the bulb on [0x4E] its back, it [0x4E] stores energy [0x5F] [0x50]." |
1376 | BC000-BC60F | Move names. | Variable-length move names, separated by 0x50. The moves are in internal order. | POUND#KARATE CHOP#DOUBLESLAP#COMET PUNCH#... |
1377 | BC610-BD000 | (empty space) | | 0 0 0 0 0 ... |
1378 | E8000-E876C | Names of the \ldquo{}190\rdquo{} species of Pok\eacute{}mon in memory. | Fixed length (10-letter) Pok\eacute{}mon names. Any extra space is padded with the character 0x50. The names are in \ldquo{}internal order\rdquo{}. | RHYDON####KANGASKHANNIDORAN♂#... |
1379 |-----------------------+-----------------+-----------------+-----------------|
1380 | E9BD5- | The text PLAY TIME (see above, 70442) | | |
1381 | F1A44-F1A45 | Which Pok\eacute{}mon does Officer Jenny give you? | A level/internal-id pair. | (10 177), corresponding to a level 10 Squirtle. |
1382 | F21BF-F21C0 | Which Pok\eacute{}mon does the salesman at the Mt. Moon Pok\eacute{}mon center give you? | A level/internal-id pair | (5 133), corresponding to a level 5 Magikarp. |
1383 | | |
1384 #+TBLFM:
1386 ** COMMENT
1387 Locations where Give Pokemon is used in a nonstraightforward way
1388 0x5287C
1389 0x5CE23
1390 0x5C36B
1391 0x7562E
1393 Find GivePokemon
1394 (search-memory* (vec(rom)) [120 234 \_ \_ 121 234 \_ \_ 175 234 \_ \_ 6] 10)
1396 F4011 : ASM script for asking if it's oak battle
1399 ** Understanding memory banks and pointers
1400 #+begin_src clojure
1402 (defn endian-flip
1403 "Flip the bytes of the two-byte number."
1404 [n]
1405 (assert (< n 0xFFFF))
1406 (+ (* 0x100 (rem n 0x100))
1407 (int (/ n 0x100))))
1410 (defn offset->ptr
1411 "Convert an offset into a little-endian pointer."
1412 [n]
1413 (->
1415 (rem 0x10000) ;; take last four bytes
1416 (rem 0x4000) ;; get relative offset from the start of the bank
1417 (+ 0x4000)
1418 endian-flip))
1420 (defn offset->bank
1421 "Get the bank of the offset."
1422 [n]
1423 (int (/ n 0x4000)))
1425 (defn ptr->offset
1426 "Convert a two-byte little-endian pointer into an offset."
1427 [bank ptr]
1428 (->
1429 ptr
1430 endian-flip
1431 (- 0x4000)
1432 (+ (* 0x4000 bank))
1433 ))
1435 (defn same-bank-offset
1436 "Convert a ptr into an absolute offset by using the bank of the reference."
1437 [reference ptr]
1438 (ptr->offset
1439 (offset->bank reference)
1440 ptr))
1441 #+end_src
1444 ** Internal Pok\eacute{}mon IDs
1445 ** Type IDs
1447 #+name: type-ids
1448 #+begin_src clojure
1449 (def pkmn-types
1450 [:normal ;;0
1451 :fighting ;;1
1452 :flying ;;2
1453 :poison ;;3
1454 :ground ;;4
1455 :rock ;;5
1456 :bird ;;6
1457 :bug ;;7
1458 :ghost ;;8
1459 :A
1460 :B
1461 :C
1462 :D
1463 :E
1464 :F
1465 :G
1466 :H
1467 :I
1468 :J
1469 :K
1470 :fire ;;20 (0x14)
1471 :water ;;21 (0x15)
1472 :grass ;;22 (0x16)
1473 :electric ;;23 (0x17)
1474 :psychic ;;24 (0x18)
1475 :ice ;;25 (0x19)
1476 :dragon ;;26 (0x1A)
1477 ])
1478 #+end_src
1480 ** Basic effects of moves
1482 *** Table of basic effects
1484 The possible effects of moves in Pok\eacute{}mon \mdash{} for example, dealing
1485 damage, leeching health, or potentially poisoning the opponent
1486 \mdash{} are stored in a table. Each move has exactly one effect, and
1487 different moves might have the same effect.
1489 For example, Leech Life, Mega Drain, and Absorb all have effect ID #3, which is \ldquo{}Leech half of the inflicted damage.\rdquo{}
1491 All the legitimate move effects are listed in the table
1492 below. Here are some notes for reading it:
1494 - Whenever an effect has a chance of doing something (like a chance of
1495 poisoning the opponent), I list the chance as a hexadecimal amount
1496 out of 256; this is to avoid rounding errors. To convert the hex amount into a percentage, divide by 256.
1497 - For some effects, the description is too cumbersome to
1498 write. Instead, I just write a move name
1499 in parentheses, like: (leech seed). That move gives a characteristic example
1500 of the effect.
1501 - I use the abbreviations =atk=, =def=, =spd=, =spc=, =acr=, =evd= for
1502 attack, defense, speed, special, accuracy, and evasiveness.
1507 | ID (hex) | Description | Notes |
1508 |----------+-------------------------------------------------------------------------------------------------+------------------------------------------------------------------|
1509 | 0 | normal damage | |
1510 | 1 | no damage, just sleep | TODO: find out how many turns |
1511 | 2 | 0x4C chance of poison | |
1512 | 3 | leech half of inflicted damage | |
1513 | 4 | 0x19 chance of burn | |
1514 | 5 | 0x19 chance of freeze | |
1515 | 6 | 0x19 chance of paralysis | |
1516 | 7 | user faints; opponent's defense is halved during attack. | |
1517 | 8 | leech half of inflicted damage ONLY if the opponent is asleep | |
1518 | 9 | imitate last attack | |
1519 | A | user atk +1 | |
1520 | B | user def +1 | |
1521 | C | user spd +1 | |
1522 | D | user spc +1 | |
1523 | E | user acr +1 | This effect is unused. |
1524 | F | user evd +1 | |
1525 | 10 | get post-battle money = 2 * level * uses | |
1526 | 11 | move has 0xFE acr, regardless of battle stat modifications. | |
1527 | 12 | opponent atk -1 | |
1528 | 13 | opponent def -1 | |
1529 | 14 | opponent spd -1 | |
1530 | 15 | opponent spc -1 | |
1531 | 16 | opponent acr -1 | |
1532 | 17 | opponent evd -1 | |
1533 | 18 | converts user's type to opponent's. | |
1534 | 19 | (haze) | |
1535 | 1A | (bide) | |
1536 | 1B | (thrash) | |
1537 | 1C | (teleport) | |
1538 | 1D | (fury swipes) | |
1539 | 1E | attacks 2-5 turns | Unused. TODO: find out what it does. |
1540 | 1F | 0x19 chance of flinching | |
1541 | 20 | opponent sleep for 1-7 turns | |
1542 | 21 | 0x66 chance of poison | |
1543 | 22 | 0x4D chance of burn | |
1544 | 23 | 0x4D chance of freeze | |
1545 | 24 | 0x4D chance of paralysis | |
1546 | 25 | 0x4D chance of flinching | |
1547 | 26 | one-hit KO | |
1548 | 27 | charge one turn, atk next. | |
1549 | 28 | fixed damage, leaves 1HP. | Is the fixed damage the power of the move? |
1550 | 29 | fixed damage. | Like seismic toss, dragon rage, psywave. |
1551 | 2A | atk 2-5 turns; opponent can't attack | The odds of attacking for /n/ turns are: (0 0x60 0x60 0x20 0x20) |
1552 | 2B | charge one turn, atk next. (can't be hit when charging) | |
1553 | 2C | atk hits twice. | |
1554 | 2D | user takes 1 damage if misses. | |
1555 | 2E | evade status-lowering effects | Caused by you or also your opponent? |
1556 | 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. |
1557 | 30 | atk causes recoil dmg = 1/4 dmg dealt | |
1558 | 31 | confuses opponent | |
1559 | 32 | user atk +2 | |
1560 | 33 | user def +2 | |
1561 | 34 | user spd +2 | |
1562 | 35 | user spc +2 | |
1563 | 36 | user acr +2 | This effect is unused. |
1564 | 37 | user evd +2 | This effect is unused. |
1565 | 38 | restores up to half of user's max hp. | |
1566 | 39 | (transform) | |
1567 | 3A | opponent atk -2 | |
1568 | 3B | opponent def -2 | |
1569 | 3C | opponent spd -2 | |
1570 | 3D | opponent spc -2 | |
1571 | 3E | opponent acr -2 | |
1572 | 3F | opponent evd -2 | |
1573 | 40 | doubles user spc when attacked | |
1574 | 41 | doubles user def when attacked | |
1575 | 42 | just poisons opponent | |
1576 | 43 | just paralyzes opponent | |
1577 | 44 | 0x19 chance opponent atk -1 | |
1578 | 45 | 0x19 chance opponent def -1 | |
1579 | 46 | 0x19 chance opponent spd -1 | |
1580 | 47 | 0x4C chance opponent spc -1 | |
1581 | 48 | 0x19 chance opponent acr -1 | |
1582 | 49 | 0x19 chance opponent evd -1 | |
1583 | 4A | ??? | ;; unused? no effect? |
1584 | 4B | ??? | ;; unused? no effect? |
1585 | 4C | 0x19 chance of confusing the opponent | |
1586 | 4D | atk hits twice. 0x33 chance opponent poisioned. | |
1587 | 4E | broken. crash the game after attack. | |
1588 | 4F | (substitute) | |
1589 | 50 | unless opponent faints, user must recharge after atk. some exceptions apply | |
1590 | 51 | (rage) | |
1591 | 52 | (mimic) | |
1592 | 53 | (metronome) | |
1593 | 54 | (leech seed) | |
1594 | 55 | does nothing (splash) | |
1595 | 56 | (disable) | |
1596 #+end_src
1598 *** Source
1599 #+name: move-effects
1600 #+begin_src clojure
1601 (def move-effects
1602 ["normal damage"
1603 "no damage, just opponent sleep" ;; how many turns? is atk power ignored?
1604 "0x4C chance of poison"
1605 "leech half of inflicted damage"
1606 "0x19 chance of burn"
1607 "0x19 chance of freeze"
1608 "0x19 chance of paralyze"
1609 "user faints; opponent defense halved during attack."
1610 "leech half of inflicted damage ONLY if sleeping opponent."
1611 "imitate last attack"
1612 "user atk +1"
1613 "user def +1"
1614 "user spd +1"
1615 "user spc +1"
1616 "user acr +1" ;; unused?!
1617 "user evd +1"
1618 "get post-battle $ = 2*level*uses"
1619 "0xFE acr, no matter what."
1620 "opponent atk -1" ;; acr taken from move acr?
1621 "opponent def -1" ;;
1622 "opponent spd -1" ;;
1623 "opponent spc -1" ;;
1624 "opponent acr -1";;
1625 "opponent evd -1"
1626 "converts user's type to opponent's."
1627 "(haze)"
1628 "(bide)"
1629 "(thrash)"
1630 "(teleport)"
1631 "(fury swipes)"
1632 "attacks 2-5 turns" ;; unused? like rollout?
1633 "0x19 chance of flinch"
1634 "opponent sleep for 1-7 turns"
1635 "0x66 chance of poison"
1636 "0x4D chance of burn"
1637 "0x4D chance of freeze"
1638 "0x4D chance of paralyze"
1639 "0x4D chance of flinch"
1640 "one-hit KO"
1641 "charge one turn, atk next."
1642 "fixed damage, leaves 1HP." ;; how is dmg determined?
1643 "fixed damage." ;; cf seismic toss, dragon rage, psywave.
1644 "atk 2-5 turns; opponent can't attack" ;; unnormalized? (0 0x60 0x60 0x20 0x20)
1645 "charge one turn, atk next. (can't be hit when charging)"
1646 "atk hits twice."
1647 "user takes 1 damage if misses."
1648 "evade status-lowering effects" ;;caused by you or also your opponent?
1649 "(broken) if user is slower than opponent, makes critical hit impossible, otherwise has no effect"
1650 "atk causes recoil dmg = 1/4 dmg dealt"
1651 "confuses opponent" ;; acr taken from move acr
1652 "user atk +2"
1653 "user def +2"
1654 "user spd +2"
1655 "user spc +2"
1656 "user acr +2" ;; unused!
1657 "user evd +2" ;; unused!
1658 "restores up to half of user's max hp." ;; broken: fails if the difference
1659 ;; b/w max and current hp is one less than a multiple of 256.
1660 "(transform)"
1661 "opponent atk -2"
1662 "opponent def -2"
1663 "opponent spd -2"
1664 "opponent spc -2"
1665 "opponent acr -2"
1666 "opponent evd -2"
1667 "doubles user spc when attacked"
1668 "doubles user def when attacked"
1669 "just poisons opponent" ;;acr taken from move acr
1670 "just paralyzes opponent" ;;
1671 "0x19 chance opponent atk -1"
1672 "0x19 chance opponent def -1"
1673 "0x19 chance opponent spd -1"
1674 "0x4C chance opponent spc -1" ;; context suggest chance is 0x19
1675 "0x19 chance opponent acr -1"
1676 "0x19 chance opponent evd -1"
1677 "???" ;; unused? no effect?
1678 "???" ;; unused? no effect?
1679 "0x19 chance opponent confused"
1680 "atk hits twice. 0x33 chance opponent poisioned."
1681 "broken. crash the game after attack."
1682 "(substitute)"
1683 "unless opponent faints, user must recharge after atk. some
1684 exceptions apply."
1685 "(rage)"
1686 "(mimic)"
1687 "(metronome)"
1688 "(leech seed)"
1689 "does nothing (splash)"
1690 "(disable)"
1691 ])
1692 #+end_src
1695 ** Alphabet code
1697 * Source
1699 #+begin_src clojure :tangle ../clojure/com/aurellem/gb/hxc.clj
1701 (ns com.aurellem.gb.hxc
1702 (:use (com.aurellem.gb assembly characters gb-driver util mem-util
1703 constants species))
1704 (:import [com.aurellem.gb.gb_driver SaveState]))
1706 ; ************* HANDWRITTEN CONSTANTS
1708 <<type-ids>>
1711 ;; question: when status effects claim to take
1712 ;; their accuracy from the move accuracy, does
1713 ;; this mean that the move always "hits" but the
1714 ;; status effect may not?
1716 <<move-effects>>
1718 ;; ************** HARDCODED DATA
1720 <<hxc-thunks>>
1721 ;; --------------------------------------------------
1723 <<pokenames>>
1724 <<type-names>>
1726 ;; http://hax.iimarck.us/topic/581/
1727 <<pokecry>>
1730 <<item-names>>
1734 (def hxc-titles
1735 "The hardcoded names of the trainer titles in memory. List begins at
1736 ROM@27E77"
1737 (hxc-thunk-words 0x27E77 196))
1740 <<dex-text>>
1742 ;; In red/blue, pokedex stats are in internal order.
1743 ;; In yellow, pokedex stats are in pokedex order.
1744 <<dex-stats>>
1749 <<places>>
1751 (defn hxc-dialog
1752 "The hardcoded dialogue in memory, including in-game alerts. Dialog
1753 seems to be separated by 0x57 instead of 0x50 (END). Begins at ROM@98000."
1754 ([rom]
1755 (map character-codes->str
1756 (take-nth 2
1757 (partition-by #(= % 0x57)
1758 (take 0x0F728
1759 (drop 0x98000 rom))))))
1760 ([]
1761 (hxc-dialog com.aurellem.gb.gb-driver/original-rom)))
1764 <<move-names>>
1765 <<move-data>>
1767 <<machines>>
1771 (defn internal-id
1772 ([rom]
1773 (zipmap
1774 (hxc-pokenames rom)
1775 (range)))
1776 ([]
1777 (internal-id com.aurellem.gb.gb-driver/original-rom)))
1783 ;; nidoran gender change upon levelup
1784 ;; (->
1785 ;; @current-state
1786 ;; rom
1787 ;; vec
1788 ;; (rewrite-memory
1789 ;; (nth (hxc-ptrs-evolve) ((internal-id) :nidoran♂))
1790 ;; [1 1 15])
1791 ;; (rewrite-memory
1792 ;; (nth (hxc-ptrs-evolve) ((internal-id) :nidoran♀))
1793 ;; [1 1 3])
1794 ;; (write-rom!)
1796 ;; )
1800 <<type-advantage>>
1804 <<evolution-header>>
1805 <<evolution>>
1806 <<learnsets>>
1807 <<pokebase>>
1810 (defn hxc-intro-pkmn
1811 "The hardcoded pokemon to display in Prof. Oak's introduction; the pokemon's
1812 internal id is stored at ROM@5EDB."
1813 ([] (hxc-intro-pkmn
1814 com.aurellem.gb.gb-driver/original-rom))
1815 ([rom]
1816 (nth (hxc-pokenames rom) (nth rom 0x5EDB))))
1818 (defn sxc-intro-pkmn!
1819 "Set the hardcoded pokemon to display in Prof. Oak's introduction."
1820 [pokemon]
1821 (write-rom!
1822 (rewrite-rom 0x5EDB
1824 (inc
1825 ((zipmap
1826 (hxc-pokenames)
1827 (range))
1828 pokemon))])))
1831 <<item-prices>>
1833 <<item-vendors>>
1835 <<wilds>>
1838 ;; ********************** MANIPULATION FNS
1841 (defn same-type
1842 ([pkmn move]
1843 (same-type
1844 com.aurellem.gb.gb-driver/original-rom pkmn move))
1845 ([rom pkmn move]
1846 (((comp :types (hxc-pokemon-base rom)) pkmn)
1847 ((comp :type (hxc-move-data rom)) move))))
1852 (defn submap?
1853 "Compares the two maps. Returns true if map-big has the same associations as map-small, otherwise false."
1854 [map-small map-big]
1855 (cond (empty? map-small) true
1856 (and
1857 (contains? map-big (ffirst map-small))
1858 (= (get map-big (ffirst map-small))
1859 (second (first map-small))))
1860 (recur (next map-small) map-big)
1862 :else false))
1865 (defn search-map [proto-map maps]
1866 "Returns all the maps that make the same associations as proto-map."
1867 (some (partial submap? proto-map) maps))
1869 (defn filter-vals
1870 "Returns a map consisting of all the pairs [key val] for
1871 which (pred key) returns true."
1872 [pred map]
1873 (reduce (partial apply assoc) {}
1874 (filter (fn [[k v]] (pred v)) map)))
1877 (defn search-moves
1878 "Returns a subcollection of all hardcoded moves with the
1879 given attributes. Attributes consist of :name :power
1880 :accuracy :pp :fx-id
1881 (and also :fx-txt, but it contains the same information
1882 as :fx-id)"
1883 ([attribute-map]
1884 (search-moves
1885 com.aurellem.gb.gb-driver/original-rom attribute-map))
1886 ([rom attribute-map]
1887 (filter-vals (partial submap? attribute-map)
1888 (hxc-move-data rom))))
1894 ;; note: 0x2f31 contains the names "TM" "HM"?
1896 ;; note for later: credits start at F1290
1898 ;; note: DADB hyper-potion-hp _ _ _ super-potion-hp _ _ _ potion-hp ??
1900 ;; note: DD4D spells out pokemon vital stat names ("speed", etc.)
1902 ;; note: 1195C-6A says ABLE#NOT ABLE#, but so does 119C0-119CE.
1903 ;; The first instance is for Machines; the second, for stones.
1905 ;; note: according to
1906 ;; http://www.upokecenter.com/games/rby/guides/rgbtrainers.php
1907 ;; the amount of money given by a trainer is equal to the
1908 ;; base money times the level of the last Pokemon on that trainer's
1909 ;; list. Other sources say it's the the level of the last pokemon
1910 ;; /defeated/.
1912 ;; todo: find base money.
1915 ;; note: 0xDFEA (in indexable mem) is the dex# of the currently-viewed Pokemon in
1916 ;; in the pokedex. It's used for other purposes if there is none.
1918 ;; note: 0x9D35 (index.) switches from 0xFF to 0x00 temporarily when
1919 ;; you walk between areas.
1921 ;; note: 0xD059 (index.) is the special battle type of your next battle:
1922 ;; - 00 is a usual battle
1923 ;; - 01 is a pre-scripted OLD MAN battle which always fails to catch the
1924 ;; target Pokemon.
1925 ;; - 02 is a safari zone battle
1926 ;; - 03 obligates you to run away. (unused)
1927 ;; - 04 is a pre-scripted OAK battle, which (temporarily) causes the
1928 ;; enemy Pokemon to cry PIKAAA, and which always catches the target
1929 ;; Pokemon. The target Pokemon is erased after the battle.
1930 ;; - 05+ are glitch states in which you are sort of the Pokemon.
1933 ;; note: 0x251A (in indexable mem): image decompression routine seems to begin here.
1935 ;; note: 0x4845 (index): vending inventory is loaded here. possibly
1936 ;; other things, too.
1937 (comment
1938 ;; temporarily intercept/adjust what pops out of the vending
1939 ;; machine.
1940 ;; (and how much it costs)
1942 ;; located at 0x4845
1943 ;; not to be confused with shop inventory, 0xCF7B
1944 (do
1945 (step (read-state "vend-menu"))
1946 (write-memory! (rewrite-memory (vec(memory)) 0x4845 [2 0 1 0]))
1947 (step @current-state [:a])
1948 (step @current-state [])
1949 (nstep @current-state 200) ))
1952 ;; Note: There are two tile tables, one from 8000-8FFF, the other from
1953 ;; 8800-97FF. The latter contains symbols, possibly map tiles(?), with some japanese chars and stuff at the end.
1954 (defn print-pixel-letters!
1955 "The pixel tiles representing letters. Neat!"
1956 ([] (print-pixel-letters! (read-state "oak-speaks")))
1957 ([state]
1958 (map
1959 (comp
1960 println
1961 (partial map #(if (zero? %) \space 0))
1962 #(if (< (count %) 8)
1963 (recur (cons 0 %))
1964 %)
1965 reverse bit-list)
1967 (take 0xFFF (drop 0x8800 (memory state))))))
1970 ;; (defn test-2 []
1971 ;; (loop [n 0
1972 ;; pc-1 (pc-trail (-> state-defend (tick) (step [:a]) (step [:a]) (step []) (nstep 100)) 100000)
1973 ;; pc-2 (pc-trail (-> state-speed (tick) (step [:a]) (step [:a])
1974 ;; (step []) (nstep 100)) 100000)]
1975 ;; (cond (empty? (drop n pc-1)) [pc-1 n]
1976 ;; (not= (take 10 (drop n pc-1)) (take 10 pc-2))
1977 ;; (recur pc-1 pc-2 (inc n))
1978 ;; :else
1979 ;; [(take 1000 pc-2) n])))
1984 (defn test-3
1985 "Explore trainer data"
1986 ([] (test-3 0x3A289))
1987 ([start]
1988 (let [pokenames (vec(hxc-pokenames-raw))]
1989 (println
1990 (reduce
1991 str
1992 (map
1993 (fn [[adr lvl pkmn]]
1994 (str (format "%-11s %4d %02X %02X \t %05X\n"
1996 (cond
1997 (zero? lvl) "+"
1998 (nil? (get pokenames (dec pkmn)))
1999 "-"
2000 :else
2001 (get pokenames (dec pkmn)))
2002 lvl
2003 pkmn
2004 lvl
2005 adr
2006 )))
2007 (map cons
2008 (take-nth 2 (drop start (range)))
2009 (partition 2
2010 (take 400;;703
2011 (drop
2012 start
2013 ;; 0x3A75D
2014 (rom)))))))))))
2016 (defn search-memory* [mem codes k]
2017 (loop [index 0
2018 index-next 1
2019 start-match 0
2020 to-match codes
2021 matches []]
2022 (cond
2023 (>= index (count mem)) matches
2025 (empty? to-match)
2026 (recur
2027 index-next
2028 (inc index-next)
2029 index-next
2030 codes
2031 (conj matches
2032 [(hex start-match) (take k (drop start-match mem))])
2035 (or (= (first to-match) \_) ;; wildcard
2036 (= (first to-match) (nth mem index)))
2037 (recur
2038 (inc index)
2039 index-next
2040 start-match
2041 (rest to-match)
2042 matches)
2044 :else
2045 (recur
2046 index-next
2047 (inc index-next)
2048 index-next
2049 codes
2050 matches))))
2053 (def script-use-ball
2054 [0xFA ;; ld A, nn
2055 \_
2056 \_
2057 0xA7 ;; and A
2058 0xCA ;; JP Z
2059 \_
2060 \_
2061 0x3D ;; dec A
2062 0xC2 ;; JP NZ
2063 \_
2064 \_
2065 0xFA ;; LD A
2066 \_
2067 \_
2068 ])
2072 (defn search-pattern [ptn coll]
2073 (loop
2074 [index 0
2075 to-match ptn
2076 binds {}
2078 next-index 1
2079 match-start 0
2080 matches []]
2082 (cond
2083 (>= index (count coll)) matches
2084 (empty? to-match)
2085 (recur
2086 next-index
2087 ptn
2088 {}
2089 (inc next-index)
2090 next-index
2091 (conj match-start
2092 [(hex match-start) binds]))
2094 :else
2095 (let [k (first to-match)
2096 v (nth coll index)]
2097 (cond
2098 (= k \_) ;; wildcard
2099 (recur
2100 (inc index)
2101 (rest to-match)
2102 binds
2104 next-index
2105 match-start
2106 matches)
2108 (keyword? k)
2109 (if (binds k)
2110 (if (= (binds k) v)
2111 (recur
2112 (inc index)
2113 (rest to-match)
2114 binds
2115 next-index
2116 match-start
2117 matches)
2119 (recur
2120 next-index
2121 ptn
2122 {}
2123 (inc next-index)
2124 next-index
2125 matches))
2127 ;; ;; consistent bindings
2128 ;; (recur
2129 ;; (inc index)
2130 ;; (rest to-match)
2131 ;; binds
2133 ;; next-index
2134 ;; match-start
2135 ;; matches)
2137 ;; ;; inconsistent bindings
2138 ;; (recur
2139 ;; next-index
2140 ;; ptn
2141 ;; {}
2142 ;; (inc next-index)
2143 ;; next-index
2144 ;; matches))
2146 (if ((set (vals binds)) v)
2147 ;; bindings are not unique
2148 (recur
2149 next-index
2150 ptn
2151 {}
2152 (inc next-index)
2153 next-index
2154 matches)
2156 ;; bindings are unique
2157 (recur
2158 (inc index)
2159 (rest to-match)
2160 (assoc binds k v)
2162 next-index
2163 match-start
2164 matches)))
2166 :else ;; k is just a number
2167 (if (= k v)
2168 (recur
2169 (inc index)
2170 (rest to-match)
2171 binds
2173 next-index
2174 match-start
2175 matches)
2177 (recur
2178 next-index
2179 ptn
2180 {}
2181 (inc next-index)
2182 next-index
2183 matches)))))))
2193 (defn search-pattern* [ptn coll]
2194 (loop
2196 binds {}
2197 index 0
2198 index-next 1
2199 start-match 0
2200 to-match ptn
2201 matches []]
2203 (cond
2204 (>= index (count coll)) matches
2205 (empty? to-match)
2206 (recur
2207 {}
2208 index-next
2209 (inc index-next)
2210 index-next
2211 ptn
2212 (conj matches
2213 [(hex start-match) binds]))
2215 :else
2216 (let [k (first to-match)
2217 v (nth coll index)]
2218 (cond
2219 (= k \_) ;; wildcard
2220 (recur
2221 binds
2222 (inc index)
2223 index-next
2224 start-match
2225 (rest to-match)
2226 matches)
2228 (keyword? k)
2229 (if (binds k)
2230 (if (= (binds k) v)
2231 (recur
2232 binds
2233 (inc index)
2234 index-next
2235 start-match
2236 (rest to-match)
2237 matches)
2238 (recur
2239 {}
2240 index-next
2241 (inc index-next)
2242 index-next
2243 ptn
2244 matches))
2245 (if
2246 ;; every symbol must be bound to a different thing.
2247 ((set (vals binds)) v)
2248 (recur
2249 {}
2250 index-next
2251 (inc index-next)
2252 index-next
2253 ptn
2254 matches)
2255 (recur
2256 (assoc binds k v)
2257 (inc index)
2258 index-next
2259 start-match
2260 (rest to-match)
2261 matches)))
2263 :else
2264 (if (= k v)
2265 (recur
2266 binds
2267 (inc index)
2268 index-next
2269 start-match
2270 (rest to-match)
2271 matches)
2272 (recur
2273 {}
2274 index-next
2275 (inc index-next)
2276 index-next
2277 ptn
2278 matches))
2282 )))))
2287 ;; look for the rainbow badge in memory
2288 (println (reduce str (map #(str (first %) "\t" (vec(second %)) "\n") (search-memory (rom) [221] 10))))
2291 (comment
2293 (def hxc-later
2294 "Running this code produces, e.g. hardcoded names NPCs give
2295 their pokemon. Will sort through it later."
2296 (print (character-codes->str(take 10000
2297 (drop 0x71597
2298 (rom (root)))))))
2300 (let [dex
2301 (partition-by #(= 0x50 %)
2302 (take 2540
2303 (drop 0x40687
2304 (rom (root)))))]
2305 (def dex dex)
2306 (def hxc-species
2307 (map character-codes->str
2308 (take-nth 4 dex))))
2312 #+end_src
2314 #+results:
2315 : nil