view org/rom.org @ 347:ff65ee0944fe

more progess: now rom.org tangles into hxc.clj; i'll be subdividing the code blocks as I write more.
author Dylan Holmes <ocsenave@gmail.com>
date Sun, 08 Apr 2012 06:13:39 -0500
parents 5639312a393f
children 497ca041f5af
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
15 ** Mapping the ROM
17 | ROM address (hex) | Description | Format | Example |
18 |-------------------+-----------------+-----------------+-----------------|
19 | | <15> | <15> | <15> |
20 | 0233C- | Shop inventories. | | |
21 | 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. |
22 | 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#... |
23 | 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). |
24 | 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. |
25 | 06698- | ? Background music. | | |
26 | 0822E-082F? | Pointers to background music, part I. | | |
27 | 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). |
28 |-------------------+-----------------+-----------------+-----------------|
29 | 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 |
30 | 0DAE0. | Amount of HP restored by Super Potion. | " | 50 |
31 | 0DAE3. | Amount of HP restored by Potion. | " | 20 |
32 |-------------------+-----------------+-----------------+-----------------|
33 | 0DD4D-DD72 | Names of permanent stats. | Variable-length strings separated by 0x50. | #HEALTH#ATTACK#DEFENSE#SPEED#SPECIAL# |
34 | 1195C-1196A | The two terms for being able/unable to learn a TM/HM. | Variable-length strings separated by 0x50. | ABLE#NOT ABLE# |
35 | 119C0-119CE | The two terms for being able/unable to evolve using the current stone. | Variable-length strings separated by 0x50. | ABLE#NOT ABLE# |
36 | 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), ... |
37 | 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#... |
38 | 27E77- | Trainer title names. | Variable-length names separated by 0x80. | YOUNGSTER#BUG CATCHER#LASS#... |
39 | 34000- | | | |
40 | 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. |
41 | 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. | | |
42 | 39462- | The Pok\eacute{}mon cry data. | Fixed-length (3 byte) descriptions of cries. | |
43 | 3B1E5- | Pointers to evolution/learnset data. | | |
44 | 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. | |
45 | 40687- | Species data from the Pok\eacute{}dex: species name, height, weight, etc. | Fixed-length sequences of bytes. See below for specifics. | |
46 | 410B1- | A conversion table between internal order and Pokedex order. | | |
47 | 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]... |
48 | 71500- | Names of places. | | |
49 | 7C249-7C2?? | Pointers to background music, pt II. | | |
50 | 98000- | Dialogue | | |
51 | B8000- | The text of each Pokemon's Pok\eacute{}dex entry. | | |
52 | BC000-BC60E | Move names. | Variable-length move names, separated by 0x80. The moves are in internal order. | POUND#KARATE CHOP#DOUBLESLAP#COMET PUNCH#... |
53 | 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♂#... |
54 | | | | |
55 | | | | |
60 ** COMMENT Getting linguistic data: names, words, etc.
62 Some of the simplest data
65 One of the simplest data structures in the Pok\eacute{} ROM is an
66 unbroken list of strings that either (a) all have a specific length,
67 or (b) are all separated by the same character.
69 Because lots of good data has this format, we'll start by writing a
70 template function to extract it:
72 #+name: hxc-thunks
73 #+begin_src clojure :results silent
74 (defn hxc-thunk
75 "Creates a thunk (nullary fn) that grabs data in a certain region of rom and
76 splits it into a collection by 0x50. If rom is not supplied, uses the
77 original rom data."
78 [start length]
79 (fn self
80 ([rom]
81 (take-nth 2
82 (partition-by #(= % 0x50)
83 (take length
84 (drop start rom)))))
85 ([]
86 (self com.aurellem.gb.gb-driver/original-rom))))
88 (def hxc-thunk-words
89 "Same as hxc-thunk, except it interprets the rom data as characters,
90 returning a collection of strings."
91 (comp
92 (partial comp (partial map character-codes->str))
93 hxc-thunk))
95 #+end_src
97 * Pok\eacute{}mon
98 ** Names of each species
99 The names of the Pok\eacute{}mon species are stored in
100 ROM@E8000. This name list is interesting, for a number of reasons:
101 - The names are stored in [[ ][internal order]] rather than in the familiar
102 Pok\eacute{}dex order. This seemingly random order probably represents the order in which the authors created or
103 programmed in the Pok\eacute{}mon; it's used throughout the game.
104 - There is enough space allocated for 190 Pok\eacute{}mon. As I
105 understand it, there were originally going to be 190 Pok\eacute{}mon
106 in Generation I, but the creators decided to defer some to
107 Generation II. This explains why many Gen I and Gen II Pok\eacute{}mon
108 have the same aesthetic feel.
109 - The list is pockmarked with random gaps, due to the strange internal
110 ordering
111 and 39 unused spaces [fn::190 allocated spaces minus 151 true Pok\eacute{}mon]. These missing spaces are filled with the
112 placeholder name, =MISSINGNO.= (\ldquo{}Missing number\rdquo{}).
114 Each name is exactly ten letters long (Whenever a name is too short, the extra
115 space is padded with the character 0x50).
119 #+name: pokenames
120 #+begin_src clojure
122 (defn hxc-pokenames-raw
123 "The hardcoded names of the 190 species in memory. List begins at
124 ROM@E8000. Although names in memory are padded with 0x50 to be 10 characters
125 long, these names are stripped of padding. See also, hxc-pokedex-names"
126 ([]
127 (hxc-pokenames-raw com.aurellem.gb.gb-driver/original-rom))
128 ([rom]
129 (let [count-species 190
130 name-length 10]
131 (map character-codes->str
132 (partition name-length
133 (map #(if (= 0x50 %) 0x00 %)
134 (take (* count-species name-length)
135 (drop 0xE8000
136 rom))))))))
137 (def hxc-pokenames
138 (comp
139 (partial map format-name)
140 hxc-pokenames-raw))
145 (defn hxc-pokedex-names
146 "The names of the pokemon in hardcoded pokedex order. List begins at
147 ROM@410B1. See also, hxc-pokenames."
148 ([] (hxc-pokedex-names
149 com.aurellem.gb.gb-driver/original-rom))
150 ([rom]
151 (let [names (hxc-pokenames rom)]
152 (#(mapv %
153 ((comp range count keys) %))
154 (zipmap
155 (take (count names)
156 (drop 0x410b1 rom))
158 names)))))
160 #+end_src
162 * Appendices
163 ** Internal Pok\eacute{}mon IDs
164 ** Type IDs
166 #+name: type-ids
167 #+begin_src clojure
168 (def pkmn-types
169 [:normal ;;0
170 :fighting ;;1
171 :flying ;;2
172 :poison ;;3
173 :ground ;;4
174 :rock ;;5
175 :bird ;;6
176 :bug ;;7
177 :ghost ;;8
178 :A
179 :B
180 :C
181 :D
182 :E
183 :F
184 :G
185 :H
186 :I
187 :J
188 :K
189 :fire ;;20 (0x14)
190 :water ;;21 (0x15)
191 :grass ;;22 (0x16)
192 :electric ;;23 (0x17)
193 :psychic ;;24 (0x18)
194 :ice ;;25 (0x19)
195 :dragon ;;26 (0x1A)
196 ])
197 #+end_src
199 ** Basic effects of moves
201 *** Table of basic effects
203 The possible effects of moves in Pok\eacute{}mon \mdash{} for example, dealing
204 damage, leeching health, or potentially poisoning the opponent
205 \mdash{} are stored in a table. Each move has exactly one effect, and
206 different moves might have the same effect.
208 For example, Leech Life, Mega Drain, and Absorb all have effect ID #3, which is \ldquo{}Leech half of the inflicted damage.\rdquo{}
210 All the legitimate move effects are listed in the table
211 below. Here are some notes for reading it:
213 - Whenever an effect has a chance of doing something (like a chance of
214 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.
215 - For some effects, the description is too cumbersome to
216 write. Instead, I just write a move name
217 in parentheses, like: (leech seed). That move gives a characteristic example
218 of the effect.
219 - I use the abbreviations =atk=, =def=, =spd=, =spc=, =acr=, =evd= for
220 attack, defense, speed, special, accuracy, and evasion.
221 .
225 | ID (hex) | Description | Notes |
226 |----------+-------------------------------------------------------------------------------------------------+------------------------------------------------------------------|
227 | 0 | normal damage | |
228 | 1 | no damage, just sleep | TODO: find out how many turns |
229 | 2 | 0x4C chance of poison | |
230 | 3 | leech half of inflicted damage | |
231 | 4 | 0x19 chance of burn | |
232 | 5 | 0x19 chance of freeze | |
233 | 6 | 0x19 chance of paralysis | |
234 | 7 | user faints; opponent's defense is halved during attack. | |
235 | 8 | leech half of inflicted damage ONLY if the opponent is asleep | |
236 | 9 | imitate last attack | |
237 | A | user atk +1 | |
238 | B | user def +1 | |
239 | C | user spd +1 | |
240 | D | user spc +1 | |
241 | E | user acr +1 | This effect is unused. |
242 | F | user evd +1 | |
243 | 10 | get post-battle money = 2 * level * uses | |
244 | 11 | move has 0xFE acr, regardless of battle stat modifications. | |
245 | 12 | opponent atk -1 | |
246 | 13 | opponent def -1 | |
247 | 14 | opponent spd -1 | |
248 | 15 | opponent spc -1 | |
249 | 16 | opponent acr -1 | |
250 | 17 | opponent evd -1 | |
251 | 18 | converts user's type to opponent's. | |
252 | 19 | (haze) | |
253 | 1A | (bide) | |
254 | 1B | (thrash) | |
255 | 1C | (teleport) | |
256 | 1D | (fury swipes) | |
257 | 1E | attacks 2-5 turns | Unused. TODO: find out what it does. |
258 | 1F | 0x19 chance of flinching | |
259 | 20 | opponent sleep for 1-7 turns | |
260 | 21 | 0x66 chance of poison | |
261 | 22 | 0x4D chance of burn | |
262 | 23 | 0x4D chance of freeze | |
263 | 24 | 0x4D chance of paralysis | |
264 | 25 | 0x4D chance of flinching | |
265 | 26 | one-hit KO | |
266 | 27 | charge one turn, atk next. | |
267 | 28 | fixed damage, leaves 1HP. | Is the fixed damage the power of the move? |
268 | 29 | fixed damage. | Like seismic toss, dragon rage, psywave. |
269 | 2A | atk 2-5 turns; opponent can't attack | The odds of attacking for /n/ turns are: (0 0x60 0x60 0x20 0x20) |
270 | 2B | charge one turn, atk next. (can't be hit when charging) | |
271 | 2C | atk hits twice. | |
272 | 2D | user takes 1 damage if misses. | |
273 | 2E | evade status-lowering effects | Caused by you or also your opponent? |
274 | 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. |
275 | 30 | atk causes recoil dmg = 1/4 dmg dealt | |
276 | 31 | confuses opponent | |
277 | 32 | user atk +2 | |
278 | 33 | user def +2 | |
279 | 34 | user spd +2 | |
280 | 35 | user spc +2 | |
281 | 36 | user acr +2 | This effect is unused. |
282 | 37 | user evd +2 | This effect is unused. |
283 | 38 | restores up to half of user's max hp. | |
284 | 39 | (transform) | |
285 | 3A | opponent atk -2 | |
286 | 3B | opponent def -2 | |
287 | 3C | opponent spd -2 | |
288 | 3D | opponent spc -2 | |
289 | 3E | opponent acr -2 | |
290 | 3F | opponent evd -2 | |
291 | 40 | doubles user spc when attacked | |
292 | 41 | doubles user def when attacked | |
293 | 42 | just poisons opponent | |
294 | 43 | just paralyzes opponent | |
295 | 44 | 0x19 chance opponent atk -1 | |
296 | 45 | 0x19 chance opponent def -1 | |
297 | 46 | 0x19 chance opponent spd -1 | |
298 | 47 | 0x4C chance opponent spc -1 | |
299 | 48 | 0x19 chance opponent acr -1 | |
300 | 49 | 0x19 chance opponent evd -1 | |
301 | 4A | ??? | ;; unused? no effect? |
302 | 4B | ??? | ;; unused? no effect? |
303 | 4C | 0x19 chance of confusing the opponent | |
304 | 4D | atk hits twice. 0x33 chance opponent poisioned. | |
305 | 4E | broken. crash the game after attack. | |
306 | 4F | (substitute) | |
307 | 50 | unless opponent faints, user must recharge after atk. some exceptions apply | |
308 | 51 | (rage) | |
309 | 52 | (mimic) | |
310 | 53 | (metronome) | |
311 | 54 | (leech seed) | |
312 | 55 | does nothing (splash) | |
313 | 56 | (disable) | |
314 #+end_src
316 *** Source
317 #+name: move-effects
318 #+begin_src clojure
319 (def move-effects
320 ["normal damage"
321 "no damage, just opponent sleep" ;; how many turns? is atk power ignored?
322 "0x4C chance of poison"
323 "leech half of inflicted damage"
324 "0x19 chance of burn"
325 "0x19 chance of freeze"
326 "0x19 chance of paralyze"
327 "user faints; opponent defense halved during attack."
328 "leech half of inflicted damage ONLY if sleeping opponent."
329 "imitate last attack"
330 "user atk +1"
331 "user def +1"
332 "user spd +1"
333 "user spc +1"
334 "user acr +1" ;; unused?!
335 "user evd +1"
336 "get post-battle $ = 2*level*uses"
337 "0xFE acr, no matter what."
338 "opponent atk -1" ;; acr taken from move acr?
339 "opponent def -1" ;;
340 "opponent spd -1" ;;
341 "opponent spc -1" ;;
342 "opponent acr -1";;
343 "opponent evd -1"
344 "converts user's type to opponent's."
345 "(haze)"
346 "(bide)"
347 "(thrash)"
348 "(teleport)"
349 "(fury swipes)"
350 "attacks 2-5 turns" ;; unused? like rollout?
351 "0x19 chance of flinch"
352 "opponent sleep for 1-7 turns"
353 "0x66 chance of poison"
354 "0x4D chance of burn"
355 "0x4D chance of freeze"
356 "0x4D chance of paralyze"
357 "0x4D chance of flinch"
358 "one-hit KO"
359 "charge one turn, atk next."
360 "fixed damage, leaves 1HP." ;; how is dmg determined?
361 "fixed damage." ;; cf seismic toss, dragon rage, psywave.
362 "atk 2-5 turns; opponent can't attack" ;; unnormalized? (0 0x60 0x60 0x20 0x20)
363 "charge one turn, atk next. (can't be hit when charging)"
364 "atk hits twice."
365 "user takes 1 damage if misses."
366 "evade status-lowering effects" ;;caused by you or also your opponent?
367 "(broken) if user is slower than opponent, makes critical hit impossible, otherwise has no effect"
368 "atk causes recoil dmg = 1/4 dmg dealt"
369 "confuses opponent" ;; acr taken from move acr
370 "user atk +2"
371 "user def +2"
372 "user spd +2"
373 "user spc +2"
374 "user acr +2" ;; unused!
375 "user evd +2" ;; unused!
376 "restores up to half of user's max hp." ;; broken: fails if the difference
377 ;; b/w max and current hp is one less than a multiple of 256.
378 "(transform)"
379 "opponent atk -2"
380 "opponent def -2"
381 "opponent spd -2"
382 "opponent spc -2"
383 "opponent acr -2"
384 "opponent evd -2"
385 "doubles user spc when attacked"
386 "doubles user def when attacked"
387 "just poisons opponent" ;;acr taken from move acr
388 "just paralyzes opponent" ;;
389 "0x19 chance opponent atk -1"
390 "0x19 chance opponent def -1"
391 "0x19 chance opponent spd -1"
392 "0x4C chance opponent spc -1" ;; context suggest chance is 0x19
393 "0x19 chance opponent acr -1"
394 "0x19 chance opponent evd -1"
395 "???" ;; unused? no effect?
396 "???" ;; unused? no effect?
397 "0x19 chance opponent confused"
398 "atk hits twice. 0x33 chance opponent poisioned."
399 "broken. crash the game after attack."
400 "(substitute)"
401 "unless opponent faints, user must recharge after atk. some
402 exceptions apply."
403 "(rage)"
404 "(mimic)"
405 "(metronome)"
406 "(leech seed)"
407 "does nothing (splash)"
408 "(disable)"
409 ])
410 #+end_src
413 ** Alphabet code
415 #+begin_src clojure :tangle ../clojure/com/aurellem/gb/hxc.clj
417 (ns com.aurellem.gb.hxc
418 (:use (com.aurellem.gb assembly characters gb-driver util mem-util
419 constants species))
420 (:import [com.aurellem.gb.gb_driver SaveState]))
425 ; ************* HANDWRITTEN CONSTANTS
427 <<type-ids>>
430 ;; question: when status effects claim to take
431 ;; their accuracy from the move accuracy, does
432 ;; this mean that the move always "hits" but the
433 ;; status effect may not?
435 <<move-effects>>
437 ;; ************** HARDCODED DATA
439 <<hxc-thunks>>
440 ;; --------------------------------------------------
443 <<pokenames>>
445 ;; http://hax.iimarck.us/topic/581/
446 (defn hxc-cry
447 "The pokemon cry data in internal order. List begins at ROM@39462"
448 ([](hxc-cry com.aurellem.gb.gb-driver/original-rom))
449 ([rom]
450 (zipmap
451 (hxc-pokenames rom)
452 (map
453 (fn [[cry-id pitch length]]
454 {:cry-id cry-id
455 :pitch pitch
456 :length length}
457 )
458 (partition 3
459 (drop 0x39462 rom))))))
461 (defn hxc-cry-groups
462 ([] (hxc-cry-groups com.aurellem.gb.gb-driver/original-rom))
463 ([rom]
464 (map #(mapv first
465 (filter
466 (fn [[k v]]
467 (= % (:cry-id v)))
468 (hxc-cry)))
469 ((comp
470 range
471 count
472 set
473 (partial map :cry-id)
474 vals
475 hxc-cry)
476 rom))))
479 (defn cry-conversion!
480 "Convert Porygon's cry in ROM to be the cry of the given pokemon."
481 [pkmn]
482 (write-rom!
483 (rewrite-memory
484 (vec(rom))
485 0x3965D
486 (map second
487 ((hxc-cry) pkmn)))))
489 (def hxc-items-raw
490 "The hardcoded names of the items in memory. List begins at
491 ROM@045B7"
492 (hxc-thunk-words 0x45B7 870))
494 (def hxc-types
495 "The hardcoded type names in memory. List begins at ROM@27D99,
496 shortly before hxc-titles."
497 (hxc-thunk-words 0x27D99 102))
499 (def hxc-titles
500 "The hardcoded names of the trainer titles in memory. List begins at
501 ROM@27E77"
502 (hxc-thunk-words 0x27E77 196))
505 (def hxc-pokedex-text-raw
506 "The hardcoded pokedex entries in memory. List begins at
507 ROM@B8000, shortly before move names."
508 (hxc-thunk-words 0xB8000 14754))
512 (def hxc-items
513 "The hardcoded names of the items in memory, presented as
514 keywords. List begins at ROM@045B7. See also, hxc-items-raw."
515 (comp (partial map format-name) hxc-items-raw))
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)))))
527 ;; In red/blue, pokedex stats are in internal order.
528 ;; In yellow, pokedex stats are in pokedex order.
530 (defn hxc-pokedex-stats
531 "The hardcoded pokedex stats (species height weight) in memory. List
532 begins at ROM@40687"
533 ([] (hxc-pokedex-stats com.aurellem.gb.gb-driver/original-rom))
534 ([rom]
535 (let [pokedex-names (zipmap (range) (hxc-pokedex-names rom))
536 pkmn-count (count pokedex-names)
537 ]
538 ((fn capture-stats
539 [n stats data]
540 (if (zero? n) stats
541 (let [[species
542 [_
543 height-ft
544 height-in
545 weight-1
546 weight-2
547 _
548 dex-ptr-1
549 dex-ptr-2
550 dex-bank
551 _
552 & data]]
553 (split-with (partial not= 0x50) data)]
554 (recur (dec n)
555 (assoc stats
556 (pokedex-names (- pkmn-count (dec n)))
557 {:species
558 (format-name (character-codes->str species))
559 :height-ft
560 height-ft
561 :height-in
562 height-in
563 :weight
564 (/ (low-high weight-1 weight-2) 10.)
566 ;; :text
567 ;; (character-codes->str
568 ;; (take-while
569 ;; (partial not= 0x50)
570 ;; (drop
571 ;; (+ 0xB8000
572 ;; -0x4000
573 ;; (low-high dex-ptr-1 dex-ptr-2))
574 ;; rom)))
575 })
577 data)
580 )))
582 pkmn-count
583 {}
584 (drop 0x40687 rom))) ))
592 (def hxc-places
593 "The hardcoded place names in memory. List begins at
594 ROM@71500. [Cinnabar] Mansion seems to be dynamically calculated."
595 (hxc-thunk-words 0x71500 560))
598 (defn hxc-dialog
599 "The hardcoded dialogue in memory, including in-game alerts. Dialog
600 seems to be separated by 0x57 instead of 0x50 (END). Begins at ROM@98000."
601 ([rom]
602 (map character-codes->str
603 (take-nth 2
604 (partition-by #(= % 0x57)
605 (take 0x0F728
606 (drop 0x98000 rom))))))
607 ([]
608 (hxc-dialog com.aurellem.gb.gb-driver/original-rom)))
611 (def hxc-move-names
612 "The hardcoded move names in memory. List begins at ROM@BC000"
613 (hxc-thunk-words 0xBC000 1551))
616 (defn hxc-move-data
617 "The hardcoded (basic (move effects)) in memory. List begins at
618 0x38000. Returns a map of {:name :power :accuracy :pp :fx-id
619 :fx-txt}. The move descriptions are handwritten, not hardcoded."
620 ([]
621 (hxc-move-data com.aurellem.gb.gb-driver/original-rom))
622 ([rom]
623 (let [names (vec (hxc-move-names rom))
624 move-count (count names)
625 move-size 6
626 types pkmn-types ;;; !! hardcoded types
627 ]
628 (zipmap (map format-name names)
629 (map
630 (fn [[idx effect power type-id accuracy pp]]
631 {:name (names (dec idx))
632 :power power
633 :accuracy accuracy
634 :pp pp
635 :type (types type-id)
636 :fx-id effect
637 :fx-txt (get move-effects effect)
638 }
639 )
641 (partition move-size
642 (take (* move-size move-count)
643 (drop 0x38000 rom))))))))
647 (defn hxc-move-data*
648 "Like hxc-move-data, but reports numbers as hexadecimal symbols instead."
649 ([]
650 (hxc-move-data* com.aurellem.gb.gb-driver/original-rom))
651 ([rom]
652 (let [names (vec (hxc-move-names rom))
653 move-count (count names)
654 move-size 6
655 format-name (fn [s]
656 (keyword (.toLowerCase
657 (apply str
658 (map #(if (= % \space) "-" %) s)))))
659 ]
660 (zipmap (map format-name names)
661 (map
662 (fn [[idx effect power type accuracy pp]]
663 {:name (names (dec idx))
664 :power power
665 :accuracy (hex accuracy)
666 :pp pp
667 :fx-id (hex effect)
668 :fx-txt (get move-effects effect)
669 }
670 )
672 (partition move-size
673 (take (* move-size move-count)
674 (drop 0x38000 rom))))))))
677 (defn hxc-machines
678 "The hardcoded moves taught by TMs and HMs. List begins at ROM@1232D."
679 ([] (hxc-machines
680 com.aurellem.gb.gb-driver/original-rom))
681 ([rom]
682 (let [moves (hxc-move-names rom)]
683 (zipmap
684 (range)
685 (take-while
686 (comp not nil?)
687 (map (comp
688 format-name
689 (zipmap
690 (range)
691 moves)
692 dec)
693 (take 100
694 (drop 0x1232D rom))))))))
698 (defn internal-id
699 ([rom]
700 (zipmap
701 (hxc-pokenames rom)
702 (range)))
703 ([]
704 (internal-id com.aurellem.gb.gb-driver/original-rom)))
710 ;; nidoran gender change upon levelup
711 ;; (->
712 ;; @current-state
713 ;; rom
714 ;; vec
715 ;; (rewrite-memory
716 ;; (nth (hxc-ptrs-evolve) ((internal-id) :nidoran♂))
717 ;; [1 1 15])
718 ;; (rewrite-memory
719 ;; (nth (hxc-ptrs-evolve) ((internal-id) :nidoran♀))
720 ;; [1 1 3])
721 ;; (write-rom!)
723 ;; )
728 (defn hxc-advantage
729 ;; in-game multipliers are stored as 10x their effective value
730 ;; to allow for fractional multipliers like 1/2
732 "The hardcoded type advantages in memory, returned as tuples of
733 atk-type def-type multiplier. By default (i.e. if not listed here),
734 the multiplier is 1. List begins at 0x3E62D."
735 ([] (hxc-advantage com.aurellem.gb.gb-driver/original-rom))
736 ([rom]
737 (map
738 (fn [[atk def mult]] [(get pkmn-types atk (hex atk))
739 (get pkmn-types def (hex def))
740 (/ mult 10)])
741 (partition 3
742 (take-while (partial not= 0xFF)
743 (drop 0x3E62D rom))))))
747 (defn format-evo
748 "Parse a sequence of evolution data, returning a map. First is the
749 method: 0 = end-evolution-data. 1 = level-up, 2 = item, 3 = trade. Next is an item id, if the
750 method of evolution is by item (only stones will actually make pokemon
751 evolve, for some auxillary reason.) Finally, the minimum level for
752 evolution to occur (level 1 means no limit, which is used for trade
753 and item evolutions), followed by the internal id of the pokemon
754 into which to evolve. Hence, level up and trade evolutions are
755 described with 3
756 bytes; item evolutions with four."
757 [coll]
758 (let [method (first coll)]
759 (cond (empty? coll) []
760 (= 0 method) [] ;; just in case
761 (= 1 method) ;; level-up evolution
762 (conj (format-evo (drop 3 coll))
763 {:method :level-up
764 :min-level (nth coll 1)
765 :into (dec (nth coll 2))})
767 (= 2 method) ;; item evolution
768 (conj (format-evo (drop 4 coll))
769 {:method :item
770 :item (dec (nth coll 1))
771 :min-level (nth coll 2)
772 :into (dec (nth coll 3))})
774 (= 3 method) ;; trade evolution
775 (conj (format-evo (drop 3 coll))
776 {:method :trade
777 :min-level (nth coll 1) ;; always 1 for trade.
778 :into (dec (nth coll 2))}))))
781 (defn hxc-ptrs-evolve
782 "A hardcoded collection of 190 pointers to alternating evolution/learnset data,
783 in internal order."
784 ([]
785 (hxc-ptrs-evolve com.aurellem.gb.gb-driver/original-rom))
786 ([rom]
787 (let [
788 pkmn-count (count (hxc-pokenames-raw)) ;; 190
789 ptrs
790 (map (fn [[a b]] (low-high a b))
791 (partition 2
792 (take (* 2 pkmn-count)
793 (drop 0x3b1e5 rom))))]
794 (map (partial + 0x34000) ptrs)
796 )))
799 (defn hxc-learnsets
800 "Hardcoded map associating pokemon names to lists of pairs [lvl
801 move] of abilities they learn as they level up. The data
802 exists at ROM@34000, sorted by internal order. Pointers to the data
803 exist at ROM@3B1E5; see also, hxc-ptrs-evolve"
804 ([] (hxc-learnsets com.aurellem.gb.gb-driver/original-rom))
805 ([rom]
806 (apply assoc
807 {}
808 (interleave
809 (hxc-pokenames rom)
810 (map (comp
811 (partial map
812 (fn [[lvl mv]] [lvl (dec mv)]))
813 (partial partition 2)
814 ;; keep the learnset data
815 (partial take-while (comp not zero?))
816 ;; skip the evolution data
817 rest
818 (partial drop-while (comp not zero?)))
819 (map #(drop % rom)
820 (hxc-ptrs-evolve rom)))))))
822 (defn hxc-learnsets-pretty
823 "Live hxc-learnsets except it reports the name of each move --- as
824 it appears in rom --- rather than the move index."
825 ([] (hxc-learnsets-pretty com.aurellem.gb.gb-driver/original-rom))
826 ([rom]
827 (let [moves (vec(map format-name (hxc-move-names)))]
828 (into {}
829 (map (fn [[pkmn learnset]]
830 [pkmn (map (fn [[lvl mv]] [lvl (moves mv)])
831 learnset)])
832 (hxc-learnsets rom))))))
837 (defn hxc-evolution
838 "Hardcoded evolution data in memory. The data exists at ROM@34000,
839 sorted by internal order. Pointers to the data exist at ROM@3B1E5; see also, hxc-ptrs-evolve."
840 ([] (hxc-evolution com.aurellem.gb.gb-driver/original-rom))
841 ([rom]
842 (apply assoc {}
843 (interleave
844 (hxc-pokenames rom)
845 (map
846 (comp
847 format-evo
848 (partial take-while (comp not zero?))
849 #(drop % rom))
850 (hxc-ptrs-evolve rom)
851 )))))
853 (defn hxc-evolution-pretty
854 "Like hxc-evolution, except it uses the names of items and pokemon
855 --- grabbed from ROM --- rather than their numerical identifiers."
856 ([] (hxc-evolution-pretty com.aurellem.gb.gb-driver/original-rom))
857 ([rom]
858 (let
859 [poke-names (vec (hxc-pokenames rom))
860 item-names (vec (hxc-items rom))
861 use-names
862 (fn [m]
863 (loop [ks (keys m) new-map m]
864 (let [k (first ks)]
865 (cond (nil? ks) new-map
866 (= k :into)
867 (recur
868 (next ks)
869 (assoc new-map
870 :into
871 (poke-names
872 (:into
873 new-map))))
874 (= k :item)
875 (recur
876 (next ks)
877 (assoc new-map
878 :item
879 (item-names
880 (:item new-map))))
881 :else
882 (recur
883 (next ks)
884 new-map)
885 ))))]
887 (into {}
888 (map (fn [[pkmn evo-coll]]
889 [pkmn (map use-names evo-coll)])
890 (hxc-evolution rom))))))
893 (defn hxc-pokemon-base
894 ([] (hxc-pokemon-base com.aurellem.gb.gb-driver/original-rom))
895 ([rom]
896 (let [entry-size 28
897 pkmn-count (count (hxc-pokedex-text rom))
898 pokemon (rest (hxc-pokedex-names))
899 types (apply assoc {}
900 (interleave
901 (range)
902 pkmn-types)) ;;!! softcoded
903 moves (apply assoc {}
904 (interleave
905 (range)
906 (map format-name
907 (hxc-move-names rom))))
908 machines (hxc-machines)
909 ]
910 (zipmap
911 pokemon
912 (map
913 (fn [[n
914 rating-hp
915 rating-atk
916 rating-def
917 rating-speed
918 rating-special
919 type-1
920 type-2
921 rarity
922 rating-xp
923 pic-dimensions ;; tile_width|tile_height (8px/tile)
924 ptr-pic-obverse-1
925 ptr-pic-obverse-2
926 ptr-pic-reverse-1
927 ptr-pic-reverse-2
928 move-1
929 move-2
930 move-3
931 move-4
932 growth-rate
933 &
934 TMs|HMs]]
935 (let
936 [base-moves
937 (mapv moves
938 ((comp
939 ;; since the game uses zero as a delimiter,
940 ;; it must also increment all move indices by 1.
941 ;; heren we decrement to correct this.
942 (partial map dec)
943 (partial take-while (comp not zero?)))
944 [move-1 move-2 move-3 move-4]))
946 types
947 (set (list (types type-1)
948 (types type-2)))
949 TMs|HMs
950 (map
951 (comp
952 (partial map first)
953 (partial remove (comp zero? second)))
954 (split-at
955 50
956 (map vector
957 (rest(range))
958 (reduce concat
959 (map
960 #(take 8
961 (concat (bit-list %)
962 (repeat 0)))
964 TMs|HMs)))))
966 TMs (vec (first TMs|HMs))
967 HMs (take 5 (map (partial + -50) (vec (second TMs|HMs))))
970 ]
973 {:dex# n
974 :base-moves base-moves
975 :types types
976 :TMs TMs
977 :HMs HMs
978 :base-hp rating-hp
979 :base-atk rating-atk
980 :base-def rating-def
981 :base-speed rating-speed
982 :base-special rating-special
983 :o0 pic-dimensions
984 :o1 ptr-pic-obverse-1
985 :o2 ptr-pic-obverse-2
986 }))
988 (partition entry-size
989 (take (* entry-size pkmn-count)
990 (drop 0x383DE
991 rom))))))))
994 (defn hxc-intro-pkmn
995 "The hardcoded pokemon to display in Prof. Oak's introduction; the pokemon's
996 internal id is stored at ROM@5EDB."
997 ([] (hxc-intro-pkmn
998 com.aurellem.gb.gb-driver/original-rom))
999 ([rom]
1000 (nth (hxc-pokenames rom) (nth rom 0x5EDB))))
1002 (defn sxc-intro-pkmn!
1003 "Set the hardcoded pokemon to display in Prof. Oak's introduction."
1004 [pokemon]
1005 (write-rom!
1006 (rewrite-rom 0x5EDB
1008 (inc
1009 ((zipmap
1010 (hxc-pokenames)
1011 (range))
1012 pokemon))])))
1015 (defn hxc-item-prices
1016 "The hardcoded list of item prices in memory. List begins at ROM@4495"
1017 ([] (hxc-item-prices com.aurellem.gb.gb-driver/original-rom))
1018 ([rom]
1019 (let [items (hxc-items rom)
1020 price-size 3]
1021 (zipmap items
1022 (map (comp
1023 ;; zero-cost items are "priceless"
1024 #(if (zero? %) :priceless %)
1025 decode-bcd butlast)
1026 (partition price-size
1027 (take (* price-size (count items))
1028 (drop 0x4495 rom))))))))
1030 (defn hxc-shops
1031 ([] (hxc-shops com.aurellem.gb.gb-driver/original-rom))
1032 ([rom]
1033 (let [items (zipmap (range) (hxc-items rom))
1035 ;; temporarily softcode the TM items
1036 items (into
1037 items
1038 (map (juxt identity
1039 (comp keyword
1040 (partial str "tm-")
1041 (partial + 1 -200)
1042 ))
1043 (take 200 (drop 200 (range)))))
1047 ((fn parse-shop [coll [num-items & items-etc]]
1048 (let [inventory (take-while
1049 (partial not= 0xFF)
1050 items-etc)
1051 [separator & items-etc] (drop num-items (rest items-etc))]
1052 (if (= separator 0x50)
1053 (map (partial mapv (comp items dec)) (conj coll inventory))
1054 (recur (conj coll inventory) items-etc)
1056 ))
1058 '()
1059 (drop 0x233C rom))
1062 )))
1068 (defn hxc-ptrs-wild
1069 "A list of the hardcoded wild encounter data in memory. Pointers
1070 begin at ROM@0CB95; data begins at ROM@0x04D89"
1071 ([] (hxc-ptrs-wild com.aurellem.gb.gb-driver/original-rom))
1072 ([rom]
1073 (let [ptrs
1074 (map (fn [[a b]] (+ a (* 0x100 b)))
1075 (take-while (partial not= (list 0xFF 0xFF))
1076 (partition 2 (drop 0xCB95 rom))))]
1077 ptrs)))
1081 (defn hxc-wilds
1082 "A list of the hardcoded wild encounter data in memory. Pointers
1083 begin at ROM@0CB95; data begins at ROM@0x04D89"
1084 ([] (hxc-wilds com.aurellem.gb.gb-driver/original-rom))
1085 ([rom]
1086 (let [pokenames (zipmap (range) (hxc-pokenames rom))]
1087 (map
1088 (partial map (fn [[a b]] {:species (pokenames (dec b)) :level
1089 a}))
1090 (partition 10
1092 (take-while (comp (partial not= 1)
1093 first)
1094 (partition 2
1095 (drop 0xCD8C rom))
1097 ))))))
1112 ;; ********************** MANIPULATION FNS
1115 (defn same-type
1116 ([pkmn move]
1117 (same-type
1118 com.aurellem.gb.gb-driver/original-rom pkmn move))
1119 ([rom pkmn move]
1120 (((comp :types (hxc-pokemon-base rom)) pkmn)
1121 ((comp :type (hxc-move-data rom)) move))))
1126 (defn submap?
1127 "Compares the two maps. Returns true if map-big has the same associations as map-small, otherwise false."
1128 [map-small map-big]
1129 (cond (empty? map-small) true
1130 (and
1131 (contains? map-big (ffirst map-small))
1132 (= (get map-big (ffirst map-small))
1133 (second (first map-small))))
1134 (recur (next map-small) map-big)
1136 :else false))
1139 (defn search-map [proto-map maps]
1140 "Returns all the maps that make the same associations as proto-map."
1141 (some (partial submap? proto-map) maps))
1143 (defn filter-vals
1144 "Returns a map consisting of all the pairs [key val] for
1145 which (pred key) returns true."
1146 [pred map]
1147 (reduce (partial apply assoc) {}
1148 (filter (fn [[k v]] (pred v)) map)))
1151 (defn search-moves
1152 "Returns a subcollection of all hardcoded moves with the
1153 given attributes. Attributes consist of :name :power
1154 :accuracy :pp :fx-id
1155 (and also :fx-txt, but it contains the same information
1156 as :fx-id)"
1157 ([attribute-map]
1158 (search-moves
1159 com.aurellem.gb.gb-driver/original-rom attribute-map))
1160 ([rom attribute-map]
1161 (filter-vals (partial submap? attribute-map)
1162 (hxc-move-data rom))))
1168 ;; note: 0x2f31 contains the names "TM" "HM"?
1170 ;; note for later: credits start at F1290
1172 ;; note: DADB hyper-potion-hp _ _ _ super-potion-hp _ _ _ potion-hp ??
1174 ;; note: DD4D spells out pokemon vital stat names ("speed", etc.)
1176 ;; note: 1195C-6A says ABLE#NOT ABLE#, but so does 119C0-119CE.
1177 ;; The first instance is for Machines; the second, for stones.
1179 ;; 0x251A (in indexable mem): image decompression routine seems to begin here.
1182 (comment
1184 (def hxc-later
1185 "Running this code produces, e.g. hardcoded names NPCs give
1186 their pokemon. Will sort through it later."
1187 (print (character-codes->str(take 10000
1188 (drop 0x71597
1189 (rom (root)))))))
1191 (let [dex
1192 (partition-by #(= 0x50 %)
1193 (take 2540
1194 (drop 0x40687
1195 (rom (root)))))]
1196 (def dex dex)
1197 (def hxc-species
1198 (map character-codes->str
1199 (take-nth 4 dex))))
1203 #+end_src