Mercurial > vba-clojure
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 Yellow2 #+author: Dylan Holmes3 #+email: rlm@mit.edu4 #+description:5 #+keywords:6 #+SETUPFILE: ../../aurellem/org/setup.org7 #+INCLUDE: ../../aurellem/org/level-0.org8 #+BABEL: :exports both :noweb yes :cache no :mkdirp yes10 # about map headers http://datacrystal.romhacking.net/wiki/Pokemon_Red/Blue:Notes11 # map headers Yellow http://www.pokecommunity.com/archive/index.php/t-235311.html12 # pokedollar: U+20B113 * Introduction15 ** Mapping the ROM17 | 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 data65 One of the simplest data structures in the Pok\eacute{} ROM is an66 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 a70 template function to extract it:72 #+name: hxc-thunks73 #+begin_src clojure :results silent74 (defn hxc-thunk75 "Creates a thunk (nullary fn) that grabs data in a certain region of rom and76 splits it into a collection by 0x50. If rom is not supplied, uses the77 original rom data."78 [start length]79 (fn self80 ([rom]81 (take-nth 282 (partition-by #(= % 0x50)83 (take length84 (drop start rom)))))85 ([]86 (self com.aurellem.gb.gb-driver/original-rom))))88 (def hxc-thunk-words89 "Same as hxc-thunk, except it interprets the rom data as characters,90 returning a collection of strings."91 (comp92 (partial comp (partial map character-codes->str))93 hxc-thunk))95 #+end_src97 * Pok\eacute{}mon98 ** Names of each species99 The names of the Pok\eacute{}mon species are stored in100 ROM@E8000. This name list is interesting, for a number of reasons:101 - The names are stored in [[ ][internal order]] rather than in the familiar102 Pok\eacute{}dex order. This seemingly random order probably represents the order in which the authors created or103 programmed in the Pok\eacute{}mon; it's used throughout the game.104 - There is enough space allocated for 190 Pok\eacute{}mon. As I105 understand it, there were originally going to be 190 Pok\eacute{}mon106 in Generation I, but the creators decided to defer some to107 Generation II. This explains why many Gen I and Gen II Pok\eacute{}mon108 have the same aesthetic feel.109 - The list is pockmarked with random gaps, due to the strange internal110 ordering111 and 39 unused spaces [fn::190 allocated spaces minus 151 true Pok\eacute{}mon]. These missing spaces are filled with the112 placeholder name, =MISSINGNO.= (\ldquo{}Missing number\rdquo{}).114 Each name is exactly ten letters long (Whenever a name is too short, the extra115 space is padded with the character 0x50).119 #+name: pokenames120 #+begin_src clojure122 (defn hxc-pokenames-raw123 "The hardcoded names of the 190 species in memory. List begins at124 ROM@E8000. Although names in memory are padded with 0x50 to be 10 characters125 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 190130 name-length 10]131 (map character-codes->str132 (partition name-length133 (map #(if (= 0x50 %) 0x00 %)134 (take (* count-species name-length)135 (drop 0xE8000136 rom))))))))137 (def hxc-pokenames138 (comp139 (partial map format-name)140 hxc-pokenames-raw))145 (defn hxc-pokedex-names146 "The names of the pokemon in hardcoded pokedex order. List begins at147 ROM@410B1. See also, hxc-pokenames."148 ([] (hxc-pokedex-names149 com.aurellem.gb.gb-driver/original-rom))150 ([rom]151 (let [names (hxc-pokenames rom)]152 (#(mapv %153 ((comp range count keys) %))154 (zipmap155 (take (count names)156 (drop 0x410b1 rom))158 names)))))160 #+end_src162 * Appendices163 ** Internal Pok\eacute{}mon IDs164 ** Type IDs166 #+name: type-ids167 #+begin_src clojure168 (def pkmn-types169 [:normal ;;0170 :fighting ;;1171 :flying ;;2172 :poison ;;3173 :ground ;;4174 :rock ;;5175 :bird ;;6176 :bug ;;7177 :ghost ;;8178 :A179 :B180 :C181 :D182 :E183 :F184 :G185 :H186 :I187 :J188 :K189 :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_src199 ** Basic effects of moves201 *** Table of basic effects203 The possible effects of moves in Pok\eacute{}mon \mdash{} for example, dealing204 damage, leeching health, or potentially poisoning the opponent205 \mdash{} are stored in a table. Each move has exactly one effect, and206 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 table211 below. Here are some notes for reading it:213 - Whenever an effect has a chance of doing something (like a chance of214 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 to216 write. Instead, I just write a move name217 in parentheses, like: (leech seed). That move gives a characteristic example218 of the effect.219 - I use the abbreviations =atk=, =def=, =spd=, =spc=, =acr=, =evd= for220 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_src316 *** Source317 #+name: move-effects318 #+begin_src clojure319 (def move-effects320 ["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 acr370 "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 difference377 ;; 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 acr388 "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 0x19393 "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. some402 exceptions apply."403 "(rage)"404 "(mimic)"405 "(metronome)"406 "(leech seed)"407 "does nothing (splash)"408 "(disable)"409 ])410 #+end_src413 ** Alphabet code415 #+begin_src clojure :tangle ../clojure/com/aurellem/gb/hxc.clj417 (ns com.aurellem.gb.hxc418 (:use (com.aurellem.gb assembly characters gb-driver util mem-util419 constants species))420 (:import [com.aurellem.gb.gb_driver SaveState]))425 ; ************* HANDWRITTEN CONSTANTS427 <<type-ids>>430 ;; question: when status effects claim to take431 ;; their accuracy from the move accuracy, does432 ;; this mean that the move always "hits" but the433 ;; status effect may not?435 <<move-effects>>437 ;; ************** HARDCODED DATA439 <<hxc-thunks>>440 ;; --------------------------------------------------443 <<pokenames>>445 ;; http://hax.iimarck.us/topic/581/446 (defn hxc-cry447 "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 (zipmap451 (hxc-pokenames rom)452 (map453 (fn [[cry-id pitch length]]454 {:cry-id cry-id455 :pitch pitch456 :length length}457 )458 (partition 3459 (drop 0x39462 rom))))))461 (defn hxc-cry-groups462 ([] (hxc-cry-groups com.aurellem.gb.gb-driver/original-rom))463 ([rom]464 (map #(mapv first465 (filter466 (fn [[k v]]467 (= % (:cry-id v)))468 (hxc-cry)))469 ((comp470 range471 count472 set473 (partial map :cry-id)474 vals475 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-memory484 (vec(rom))485 0x3965D486 (map second487 ((hxc-cry) pkmn)))))489 (def hxc-items-raw490 "The hardcoded names of the items in memory. List begins at491 ROM@045B7"492 (hxc-thunk-words 0x45B7 870))494 (def hxc-types495 "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-titles500 "The hardcoded names of the trainer titles in memory. List begins at501 ROM@27E77"502 (hxc-thunk-words 0x27E77 196))505 (def hxc-pokedex-text-raw506 "The hardcoded pokedex entries in memory. List begins at507 ROM@B8000, shortly before move names."508 (hxc-thunk-words 0xB8000 14754))512 (def hxc-items513 "The hardcoded names of the items in memory, presented as514 keywords. List begins at ROM@045B7. See also, hxc-items-raw."515 (comp (partial map format-name) hxc-items-raw))517 (defn hxc-pokedex-text518 "The hardcoded pokedex entries in memory, presented as an519 associative hash map. List begins at ROM@B8000."520 ([] (hxc-pokedex-text com.aurellem.gb.gb-driver/original-rom))521 ([rom]522 (zipmap523 (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-stats531 "The hardcoded pokedex stats (species height weight) in memory. List532 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-stats539 [n stats data]540 (if (zero? n) stats541 (let [[species542 [_543 height-ft544 height-in545 weight-1546 weight-2547 _548 dex-ptr-1549 dex-ptr-2550 dex-bank551 _552 & data]]553 (split-with (partial not= 0x50) data)]554 (recur (dec n)555 (assoc stats556 (pokedex-names (- pkmn-count (dec n)))557 {:species558 (format-name (character-codes->str species))559 :height-ft560 height-ft561 :height-in562 height-in563 :weight564 (/ (low-high weight-1 weight-2) 10.)566 ;; :text567 ;; (character-codes->str568 ;; (take-while569 ;; (partial not= 0x50)570 ;; (drop571 ;; (+ 0xB8000572 ;; -0x4000573 ;; (low-high dex-ptr-1 dex-ptr-2))574 ;; rom)))575 })577 data)580 )))582 pkmn-count583 {}584 (drop 0x40687 rom))) ))592 (def hxc-places593 "The hardcoded place names in memory. List begins at594 ROM@71500. [Cinnabar] Mansion seems to be dynamically calculated."595 (hxc-thunk-words 0x71500 560))598 (defn hxc-dialog599 "The hardcoded dialogue in memory, including in-game alerts. Dialog600 seems to be separated by 0x57 instead of 0x50 (END). Begins at ROM@98000."601 ([rom]602 (map character-codes->str603 (take-nth 2604 (partition-by #(= % 0x57)605 (take 0x0F728606 (drop 0x98000 rom))))))607 ([]608 (hxc-dialog com.aurellem.gb.gb-driver/original-rom)))611 (def hxc-move-names612 "The hardcoded move names in memory. List begins at ROM@BC000"613 (hxc-thunk-words 0xBC000 1551))616 (defn hxc-move-data617 "The hardcoded (basic (move effects)) in memory. List begins at618 0x38000. Returns a map of {:name :power :accuracy :pp :fx-id619 :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 6626 types pkmn-types ;;; !! hardcoded types627 ]628 (zipmap (map format-name names)629 (map630 (fn [[idx effect power type-id accuracy pp]]631 {:name (names (dec idx))632 :power power633 :accuracy accuracy634 :pp pp635 :type (types type-id)636 :fx-id effect637 :fx-txt (get move-effects effect)638 }639 )641 (partition move-size642 (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 6655 format-name (fn [s]656 (keyword (.toLowerCase657 (apply str658 (map #(if (= % \space) "-" %) s)))))659 ]660 (zipmap (map format-name names)661 (map662 (fn [[idx effect power type accuracy pp]]663 {:name (names (dec idx))664 :power power665 :accuracy (hex accuracy)666 :pp pp667 :fx-id (hex effect)668 :fx-txt (get move-effects effect)669 }670 )672 (partition move-size673 (take (* move-size move-count)674 (drop 0x38000 rom))))))))677 (defn hxc-machines678 "The hardcoded moves taught by TMs and HMs. List begins at ROM@1232D."679 ([] (hxc-machines680 com.aurellem.gb.gb-driver/original-rom))681 ([rom]682 (let [moves (hxc-move-names rom)]683 (zipmap684 (range)685 (take-while686 (comp not nil?)687 (map (comp688 format-name689 (zipmap690 (range)691 moves)692 dec)693 (take 100694 (drop 0x1232D rom))))))))698 (defn internal-id699 ([rom]700 (zipmap701 (hxc-pokenames rom)702 (range)))703 ([]704 (internal-id com.aurellem.gb.gb-driver/original-rom)))710 ;; nidoran gender change upon levelup711 ;; (->712 ;; @current-state713 ;; rom714 ;; vec715 ;; (rewrite-memory716 ;; (nth (hxc-ptrs-evolve) ((internal-id) :nidoran♂))717 ;; [1 1 15])718 ;; (rewrite-memory719 ;; (nth (hxc-ptrs-evolve) ((internal-id) :nidoran♀))720 ;; [1 1 3])721 ;; (write-rom!)723 ;; )728 (defn hxc-advantage729 ;; in-game multipliers are stored as 10x their effective value730 ;; to allow for fractional multipliers like 1/2732 "The hardcoded type advantages in memory, returned as tuples of733 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 (map738 (fn [[atk def mult]] [(get pkmn-types atk (hex atk))739 (get pkmn-types def (hex def))740 (/ mult 10)])741 (partition 3742 (take-while (partial not= 0xFF)743 (drop 0x3E62D rom))))))747 (defn format-evo748 "Parse a sequence of evolution data, returning a map. First is the749 method: 0 = end-evolution-data. 1 = level-up, 2 = item, 3 = trade. Next is an item id, if the750 method of evolution is by item (only stones will actually make pokemon751 evolve, for some auxillary reason.) Finally, the minimum level for752 evolution to occur (level 1 means no limit, which is used for trade753 and item evolutions), followed by the internal id of the pokemon754 into which to evolve. Hence, level up and trade evolutions are755 described with 3756 bytes; item evolutions with four."757 [coll]758 (let [method (first coll)]759 (cond (empty? coll) []760 (= 0 method) [] ;; just in case761 (= 1 method) ;; level-up evolution762 (conj (format-evo (drop 3 coll))763 {:method :level-up764 :min-level (nth coll 1)765 :into (dec (nth coll 2))})767 (= 2 method) ;; item evolution768 (conj (format-evo (drop 4 coll))769 {:method :item770 :item (dec (nth coll 1))771 :min-level (nth coll 2)772 :into (dec (nth coll 3))})774 (= 3 method) ;; trade evolution775 (conj (format-evo (drop 3 coll))776 {:method :trade777 :min-level (nth coll 1) ;; always 1 for trade.778 :into (dec (nth coll 2))}))))781 (defn hxc-ptrs-evolve782 "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)) ;; 190789 ptrs790 (map (fn [[a b]] (low-high a b))791 (partition 2792 (take (* 2 pkmn-count)793 (drop 0x3b1e5 rom))))]794 (map (partial + 0x34000) ptrs)796 )))799 (defn hxc-learnsets800 "Hardcoded map associating pokemon names to lists of pairs [lvl801 move] of abilities they learn as they level up. The data802 exists at ROM@34000, sorted by internal order. Pointers to the data803 exist at ROM@3B1E5; see also, hxc-ptrs-evolve"804 ([] (hxc-learnsets com.aurellem.gb.gb-driver/original-rom))805 ([rom]806 (apply assoc807 {}808 (interleave809 (hxc-pokenames rom)810 (map (comp811 (partial map812 (fn [[lvl mv]] [lvl (dec mv)]))813 (partial partition 2)814 ;; keep the learnset data815 (partial take-while (comp not zero?))816 ;; skip the evolution data817 rest818 (partial drop-while (comp not zero?)))819 (map #(drop % rom)820 (hxc-ptrs-evolve rom)))))))822 (defn hxc-learnsets-pretty823 "Live hxc-learnsets except it reports the name of each move --- as824 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-evolution838 "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 (interleave844 (hxc-pokenames rom)845 (map846 (comp847 format-evo848 (partial take-while (comp not zero?))849 #(drop % rom))850 (hxc-ptrs-evolve rom)851 )))))853 (defn hxc-evolution-pretty854 "Like hxc-evolution, except it uses the names of items and pokemon855 --- grabbed from ROM --- rather than their numerical identifiers."856 ([] (hxc-evolution-pretty com.aurellem.gb.gb-driver/original-rom))857 ([rom]858 (let859 [poke-names (vec (hxc-pokenames rom))860 item-names (vec (hxc-items rom))861 use-names862 (fn [m]863 (loop [ks (keys m) new-map m]864 (let [k (first ks)]865 (cond (nil? ks) new-map866 (= k :into)867 (recur868 (next ks)869 (assoc new-map870 :into871 (poke-names872 (:into873 new-map))))874 (= k :item)875 (recur876 (next ks)877 (assoc new-map878 :item879 (item-names880 (:item new-map))))881 :else882 (recur883 (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-base894 ([] (hxc-pokemon-base com.aurellem.gb.gb-driver/original-rom))895 ([rom]896 (let [entry-size 28897 pkmn-count (count (hxc-pokedex-text rom))898 pokemon (rest (hxc-pokedex-names))899 types (apply assoc {}900 (interleave901 (range)902 pkmn-types)) ;;!! softcoded903 moves (apply assoc {}904 (interleave905 (range)906 (map format-name907 (hxc-move-names rom))))908 machines (hxc-machines)909 ]910 (zipmap911 pokemon912 (map913 (fn [[n914 rating-hp915 rating-atk916 rating-def917 rating-speed918 rating-special919 type-1920 type-2921 rarity922 rating-xp923 pic-dimensions ;; tile_width|tile_height (8px/tile)924 ptr-pic-obverse-1925 ptr-pic-obverse-2926 ptr-pic-reverse-1927 ptr-pic-reverse-2928 move-1929 move-2930 move-3931 move-4932 growth-rate933 &934 TMs|HMs]]935 (let936 [base-moves937 (mapv moves938 ((comp939 ;; 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 types947 (set (list (types type-1)948 (types type-2)))949 TMs|HMs950 (map951 (comp952 (partial map first)953 (partial remove (comp zero? second)))954 (split-at955 50956 (map vector957 (rest(range))958 (reduce concat959 (map960 #(take 8961 (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# n974 :base-moves base-moves975 :types types976 :TMs TMs977 :HMs HMs978 :base-hp rating-hp979 :base-atk rating-atk980 :base-def rating-def981 :base-speed rating-speed982 :base-special rating-special983 :o0 pic-dimensions984 :o1 ptr-pic-obverse-1985 :o2 ptr-pic-obverse-2986 }))988 (partition entry-size989 (take (* entry-size pkmn-count)990 (drop 0x383DE991 rom))))))))994 (defn hxc-intro-pkmn995 "The hardcoded pokemon to display in Prof. Oak's introduction; the pokemon's996 internal id is stored at ROM@5EDB."997 ([] (hxc-intro-pkmn998 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 0x5EDB1007 [1008 (inc1009 ((zipmap1010 (hxc-pokenames)1011 (range))1012 pokemon))])))1015 (defn hxc-item-prices1016 "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 items1022 (map (comp1023 ;; zero-cost items are "priceless"1024 #(if (zero? %) :priceless %)1025 decode-bcd butlast)1026 (partition price-size1027 (take (* price-size (count items))1028 (drop 0x4495 rom))))))))1030 (defn hxc-shops1031 ([] (hxc-shops com.aurellem.gb.gb-driver/original-rom))1032 ([rom]1033 (let [items (zipmap (range) (hxc-items rom))1035 ;; temporarily softcode the TM items1036 items (into1037 items1038 (map (juxt identity1039 (comp keyword1040 (partial str "tm-")1041 (partial + 1 -200)1042 ))1043 (take 200 (drop 200 (range)))))1045 ]1047 ((fn parse-shop [coll [num-items & items-etc]]1048 (let [inventory (take-while1049 (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)1055 )1056 ))1058 '()1059 (drop 0x233C rom))1062 )))1068 (defn hxc-ptrs-wild1069 "A list of the hardcoded wild encounter data in memory. Pointers1070 begin at ROM@0CB95; data begins at ROM@0x04D89"1071 ([] (hxc-ptrs-wild com.aurellem.gb.gb-driver/original-rom))1072 ([rom]1073 (let [ptrs1074 (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-wilds1082 "A list of the hardcoded wild encounter data in memory. Pointers1083 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 (map1088 (partial map (fn [[a b]] {:species (pokenames (dec b)) :level1089 a}))1090 (partition 101092 (take-while (comp (partial not= 1)1093 first)1094 (partition 21095 (drop 0xCD8C rom))1097 ))))))1112 ;; ********************** MANIPULATION FNS1115 (defn same-type1116 ([pkmn move]1117 (same-type1118 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) true1130 (and1131 (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-vals1144 "Returns a map consisting of all the pairs [key val] for1145 which (pred key) returns true."1146 [pred map]1147 (reduce (partial apply assoc) {}1148 (filter (fn [[k v]] (pred v)) map)))1151 (defn search-moves1152 "Returns a subcollection of all hardcoded moves with the1153 given attributes. Attributes consist of :name :power1154 :accuracy :pp :fx-id1155 (and also :fx-txt, but it contains the same information1156 as :fx-id)"1157 ([attribute-map]1158 (search-moves1159 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 F12901172 ;; 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 (comment1184 (def hxc-later1185 "Running this code produces, e.g. hardcoded names NPCs give1186 their pokemon. Will sort through it later."1187 (print (character-codes->str(take 100001188 (drop 0x715971189 (rom (root)))))))1191 (let [dex1192 (partition-by #(= 0x50 %)1193 (take 25401194 (drop 0x406871195 (rom (root)))))]1196 (def dex dex)1197 (def hxc-species1198 (map character-codes->str1199 (take-nth 4 dex))))1200 )1203 #+end_src