annotate clojure/com/aurellem/gb/hxc.clj @ 348:497ca041f5af

finished working for now. i've completed more carving up of code blocks.
author Dylan Holmes <ocsenave@gmail.com>
date Sun, 08 Apr 2012 07:55:06 -0500
parents ff65ee0944fe
children 5aabbe326eb0
rev   line source
ocsenave@347 1
rlm@218 2 (ns com.aurellem.gb.hxc
ocsenave@308 3 (:use (com.aurellem.gb assembly characters gb-driver util mem-util
ocsenave@281 4 constants species))
rlm@218 5 (:import [com.aurellem.gb.gb_driver SaveState]))
rlm@218 6
ocsenave@249 7 ; ************* HANDWRITTEN CONSTANTS
ocsenave@249 8
ocsenave@243 9 (def pkmn-types
ocsenave@272 10 [:normal ;;0
ocsenave@272 11 :fighting ;;1
ocsenave@272 12 :flying ;;2
ocsenave@272 13 :poison ;;3
ocsenave@272 14 :ground ;;4
ocsenave@272 15 :rock ;;5
ocsenave@272 16 :bird ;;6
ocsenave@272 17 :bug ;;7
ocsenave@272 18 :ghost ;;8
ocsenave@244 19 :A
ocsenave@244 20 :B
ocsenave@244 21 :C
ocsenave@244 22 :D
ocsenave@244 23 :E
ocsenave@244 24 :F
ocsenave@244 25 :G
ocsenave@244 26 :H
ocsenave@244 27 :I
ocsenave@244 28 :J
ocsenave@244 29 :K
ocsenave@272 30 :fire ;;20 (0x14)
ocsenave@272 31 :water ;;21 (0x15)
ocsenave@272 32 :grass ;;22 (0x16)
ocsenave@272 33 :electric ;;23 (0x17)
ocsenave@272 34 :psychic ;;24 (0x18)
ocsenave@272 35 :ice ;;25 (0x19)
ocsenave@272 36 :dragon ;;26 (0x1A)
ocsenave@244 37 ])
ocsenave@243 38
ocsenave@243 39
ocsenave@246 40 ;; question: when status effects claim to take
ocsenave@246 41 ;; their accuracy from the move accuracy, does
ocsenave@246 42 ;; this mean that the move always "hits" but the
ocsenave@246 43 ;; status effect may not?
ocsenave@246 44
ocsenave@246 45 (def move-effects
ocsenave@246 46 ["normal damage"
ocsenave@246 47 "no damage, just opponent sleep" ;; how many turns? is atk power ignored?
ocsenave@246 48 "0x4C chance of poison"
ocsenave@246 49 "leech half of inflicted damage"
ocsenave@246 50 "0x19 chance of burn"
ocsenave@246 51 "0x19 chance of freeze"
ocsenave@246 52 "0x19 chance of paralyze"
ocsenave@259 53 "user faints; opponent defense halved during attack."
ocsenave@246 54 "leech half of inflicted damage ONLY if sleeping opponent."
ocsenave@246 55 "imitate last attack"
ocsenave@246 56 "user atk +1"
ocsenave@246 57 "user def +1"
ocsenave@246 58 "user spd +1"
ocsenave@246 59 "user spc +1"
ocsenave@246 60 "user acr +1" ;; unused?!
ocsenave@246 61 "user evd +1"
ocsenave@246 62 "get post-battle $ = 2*level*uses"
ocsenave@246 63 "0xFE acr, no matter what."
ocsenave@246 64 "opponent atk -1" ;; acr taken from move acr?
ocsenave@246 65 "opponent def -1" ;;
ocsenave@246 66 "opponent spd -1" ;;
ocsenave@246 67 "opponent spc -1" ;;
ocsenave@246 68 "opponent acr -1";;
ocsenave@246 69 "opponent evd -1"
ocsenave@246 70 "converts user's type to opponent's."
ocsenave@246 71 "(haze)"
ocsenave@246 72 "(bide)"
ocsenave@246 73 "(thrash)"
ocsenave@246 74 "(teleport)"
ocsenave@246 75 "(fury swipes)"
ocsenave@246 76 "attacks 2-5 turns" ;; unused? like rollout?
ocsenave@246 77 "0x19 chance of flinch"
ocsenave@246 78 "opponent sleep for 1-7 turns"
ocsenave@246 79 "0x66 chance of poison"
ocsenave@246 80 "0x4D chance of burn"
ocsenave@246 81 "0x4D chance of freeze"
ocsenave@246 82 "0x4D chance of paralyze"
ocsenave@246 83 "0x4D chance of flinch"
ocsenave@246 84 "one-hit KO"
ocsenave@246 85 "charge one turn, atk next."
ocsenave@246 86 "fixed damage, leaves 1HP." ;; how is dmg determined?
ocsenave@246 87 "fixed damage." ;; cf seismic toss, dragon rage, psywave.
ocsenave@246 88 "atk 2-5 turns; opponent can't attack" ;; unnormalized? (0 0x60 0x60 0x20 0x20)
ocsenave@246 89 "charge one turn, atk next. (can't be hit when charging)"
ocsenave@246 90 "atk hits twice."
ocsenave@246 91 "user takes 1 damage if misses."
ocsenave@246 92 "evade status-lowering effects" ;;caused by you or also your opponent?
ocsenave@246 93 "(broken) if user is slower than opponent, makes critical hit impossible, otherwise has no effect"
ocsenave@246 94 "atk causes recoil dmg = 1/4 dmg dealt"
ocsenave@246 95 "confuses opponent" ;; acr taken from move acr
ocsenave@246 96 "user atk +2"
ocsenave@246 97 "user def +2"
ocsenave@246 98 "user spd +2"
ocsenave@246 99 "user spc +2"
ocsenave@246 100 "user acr +2" ;; unused!
ocsenave@246 101 "user evd +2" ;; unused!
ocsenave@246 102 "restores up to half of user's max hp." ;; broken: fails if the difference
ocsenave@246 103 ;; b/w max and current hp is one less than a multiple of 256.
ocsenave@246 104 "(transform)"
ocsenave@246 105 "opponent atk -2"
ocsenave@246 106 "opponent def -2"
ocsenave@246 107 "opponent spd -2"
ocsenave@246 108 "opponent spc -2"
ocsenave@246 109 "opponent acr -2"
ocsenave@246 110 "opponent evd -2"
ocsenave@246 111 "doubles user spc when attacked"
ocsenave@246 112 "doubles user def when attacked"
ocsenave@249 113 "just poisons opponent" ;;acr taken from move acr
ocsenave@249 114 "just paralyzes opponent" ;;
ocsenave@246 115 "0x19 chance opponent atk -1"
ocsenave@246 116 "0x19 chance opponent def -1"
ocsenave@246 117 "0x19 chance opponent spd -1"
ocsenave@246 118 "0x4C chance opponent spc -1" ;; context suggest chance is 0x19
ocsenave@246 119 "0x19 chance opponent acr -1"
ocsenave@246 120 "0x19 chance opponent evd -1"
ocsenave@246 121 "???" ;; unused? no effect?
ocsenave@246 122 "???" ;; unused? no effect?
ocsenave@246 123 "0x19 chance opponent confused"
ocsenave@246 124 "atk hits twice. 0x33 chance opponent poisioned."
ocsenave@246 125 "broken. crash the game after attack."
ocsenave@246 126 "(substitute)"
ocsenave@246 127 "unless opponent faints, user must recharge after atk. some
ocsenave@246 128 exceptions apply."
ocsenave@246 129 "(rage)"
ocsenave@246 130 "(mimic)"
ocsenave@246 131 "(metronome)"
ocsenave@246 132 "(leech seed)"
ocsenave@246 133 "does nothing (splash)"
ocsenave@246 134 "(disable)"
ocsenave@246 135 ])
ocsenave@246 136
ocsenave@249 137 ;; ************** HARDCODED DATA
ocsenave@246 138
ocsenave@249 139 (defn hxc-thunk
ocsenave@259 140 "Creates a thunk (nullary fn) that grabs data in a certain region of rom and
ocsenave@249 141 splits it into a collection by 0x50. If rom is not supplied, uses the
ocsenave@249 142 original rom data."
ocsenave@249 143 [start length]
ocsenave@249 144 (fn self
ocsenave@249 145 ([rom]
ocsenave@249 146 (take-nth 2
ocsenave@249 147 (partition-by #(= % 0x50)
ocsenave@249 148 (take length
ocsenave@249 149 (drop start rom)))))
ocsenave@249 150 ([]
ocsenave@249 151 (self com.aurellem.gb.gb-driver/original-rom))))
ocsenave@246 152
ocsenave@249 153 (def hxc-thunk-words
ocsenave@249 154 "Same as hxc-thunk, except it interprets the rom data as characters,
ocsenave@249 155 returning a collection of strings."
ocsenave@249 156 (comp
ocsenave@249 157 (partial comp (partial map character-codes->str))
ocsenave@249 158 hxc-thunk))
ocsenave@249 159
ocsenave@249 160 ;; --------------------------------------------------
ocsenave@246 161
ocsenave@288 162
ocsenave@288 163 (defn hxc-pokenames-raw
ocsenave@288 164 "The hardcoded names of the 190 species in memory. List begins at
ocsenave@288 165 ROM@E8000. Although names in memory are padded with 0x50 to be 10 characters
ocsenave@288 166 long, these names are stripped of padding. See also, hxc-pokedex-names"
ocsenave@288 167 ([]
ocsenave@288 168 (hxc-pokenames-raw com.aurellem.gb.gb-driver/original-rom))
ocsenave@288 169 ([rom]
ocsenave@288 170 (let [count-species 190
ocsenave@288 171 name-length 10]
ocsenave@288 172 (map character-codes->str
ocsenave@288 173 (partition name-length
ocsenave@288 174 (map #(if (= 0x50 %) 0x00 %)
ocsenave@288 175 (take (* count-species name-length)
ocsenave@288 176 (drop 0xE8000
ocsenave@288 177 rom))))))))
ocsenave@288 178 (def hxc-pokenames
ocsenave@288 179 (comp
ocsenave@288 180 (partial map format-name)
ocsenave@288 181 hxc-pokenames-raw))
ocsenave@288 182
ocsenave@288 183
ocsenave@288 184
ocsenave@288 185
ocsenave@288 186 (defn hxc-pokedex-names
ocsenave@288 187 "The names of the pokemon in hardcoded pokedex order. List begins at
ocsenave@288 188 ROM@410B1. See also, hxc-pokenames."
ocsenave@288 189 ([] (hxc-pokedex-names
ocsenave@288 190 com.aurellem.gb.gb-driver/original-rom))
ocsenave@288 191 ([rom]
ocsenave@288 192 (let [names (hxc-pokenames rom)]
ocsenave@288 193 (#(mapv %
ocsenave@288 194 ((comp range count keys) %))
ocsenave@288 195 (zipmap
ocsenave@288 196 (take (count names)
ocsenave@288 197 (drop 0x410b1 rom))
ocsenave@288 198
ocsenave@288 199 names)))))
ocsenave@288 200
ocsenave@348 201 (def hxc-types
ocsenave@348 202 "The hardcoded type names in memory. List begins at ROM@27D99,
ocsenave@348 203 shortly before hxc-titles."
ocsenave@348 204 (hxc-thunk-words 0x27D99 102))
ocsenave@288 205
ocsenave@288 206
ocsenave@306 207 ;; http://hax.iimarck.us/topic/581/
ocsenave@307 208 (defn hxc-cry
ocsenave@307 209 "The pokemon cry data in internal order. List begins at ROM@39462"
ocsenave@308 210 ([](hxc-cry com.aurellem.gb.gb-driver/original-rom))
ocsenave@307 211 ([rom]
ocsenave@307 212 (zipmap
ocsenave@307 213 (hxc-pokenames rom)
ocsenave@307 214 (map
ocsenave@307 215 (fn [[cry-id pitch length]]
ocsenave@307 216 {:cry-id cry-id
ocsenave@307 217 :pitch pitch
ocsenave@307 218 :length length}
ocsenave@307 219 )
ocsenave@307 220 (partition 3
ocsenave@308 221 (drop 0x39462 rom))))))
ocsenave@306 222
ocsenave@307 223 (defn hxc-cry-groups
ocsenave@308 224 ([] (hxc-cry-groups com.aurellem.gb.gb-driver/original-rom))
ocsenave@307 225 ([rom]
ocsenave@307 226 (map #(mapv first
ocsenave@307 227 (filter
ocsenave@307 228 (fn [[k v]]
ocsenave@307 229 (= % (:cry-id v)))
ocsenave@308 230 (hxc-cry)))
ocsenave@307 231 ((comp
ocsenave@307 232 range
ocsenave@307 233 count
ocsenave@307 234 set
ocsenave@307 235 (partial map :cry-id)
ocsenave@307 236 vals
ocsenave@307 237 hxc-cry)
ocsenave@307 238 rom))))
ocsenave@306 239
ocsenave@288 240
ocsenave@307 241 (defn cry-conversion!
ocsenave@307 242 "Convert Porygon's cry in ROM to be the cry of the given pokemon."
ocsenave@307 243 [pkmn]
ocsenave@307 244 (write-rom!
ocsenave@307 245 (rewrite-memory
ocsenave@307 246 (vec(rom))
ocsenave@307 247 0x3965D
ocsenave@307 248 (map second
ocsenave@307 249 ((hxc-cry) pkmn)))))
ocsenave@307 250
ocsenave@348 251
ocsenave@348 252
ocsenave@348 253
ocsenave@288 254 (def hxc-items-raw
ocsenave@249 255 "The hardcoded names of the items in memory. List begins at
ocsenave@249 256 ROM@045B7"
ocsenave@249 257 (hxc-thunk-words 0x45B7 870))
ocsenave@246 258
ocsenave@348 259 (def hxc-items
ocsenave@348 260 "The hardcoded names of the items in memory, presented as
ocsenave@348 261 keywords. List begins at ROM@045B7. See also, hxc-items-raw."
ocsenave@348 262 (comp (partial map format-name) hxc-items-raw))
ocsenave@348 263
ocsenave@348 264
ocsenave@246 265
ocsenave@246 266 (def hxc-titles
ocsenave@246 267 "The hardcoded names of the trainer titles in memory. List begins at
ocsenave@246 268 ROM@27E77"
ocsenave@249 269 (hxc-thunk-words 0x27E77 196))
ocsenave@246 270
ocsenave@259 271
ocsenave@288 272 (def hxc-pokedex-text-raw
ocsenave@259 273 "The hardcoded pokedex entries in memory. List begins at
ocsenave@259 274 ROM@B8000, shortly before move names."
ocsenave@259 275 (hxc-thunk-words 0xB8000 14754))
ocsenave@259 276
ocsenave@288 277
ocsenave@288 278
ocsenave@288 279
ocsenave@285 280 (defn hxc-pokedex-text
ocsenave@285 281 "The hardcoded pokedex entries in memory, presented as an
ocsenave@285 282 associative hash map. List begins at ROM@B8000."
ocsenave@285 283 ([] (hxc-pokedex-text com.aurellem.gb.gb-driver/original-rom))
ocsenave@285 284 ([rom]
ocsenave@285 285 (zipmap
ocsenave@285 286 (hxc-pokedex-names rom)
ocsenave@285 287 (cons nil ;; for missingno.
ocsenave@288 288 (hxc-pokedex-text-raw rom)))))
ocsenave@259 289
ocsenave@272 290 ;; In red/blue, pokedex stats are in internal order.
ocsenave@272 291 ;; In yellow, pokedex stats are in pokedex order.
ocsenave@259 292 (defn hxc-pokedex-stats
ocsenave@272 293 "The hardcoded pokedex stats (species height weight) in memory. List
ocsenave@272 294 begins at ROM@40687"
ocsenave@259 295 ([] (hxc-pokedex-stats com.aurellem.gb.gb-driver/original-rom))
ocsenave@259 296 ([rom]
ocsenave@288 297 (let [pokedex-names (zipmap (range) (hxc-pokedex-names rom))
ocsenave@288 298 pkmn-count (count pokedex-names)
ocsenave@259 299 ]
ocsenave@259 300 ((fn capture-stats
ocsenave@259 301 [n stats data]
ocsenave@259 302 (if (zero? n) stats
ocsenave@259 303 (let [[species
ocsenave@259 304 [_
ocsenave@259 305 height-ft
ocsenave@259 306 height-in
ocsenave@259 307 weight-1
ocsenave@259 308 weight-2
ocsenave@259 309 _
ocsenave@259 310 dex-ptr-1
ocsenave@259 311 dex-ptr-2
ocsenave@259 312 dex-bank
ocsenave@259 313 _
ocsenave@259 314 & data]]
ocsenave@259 315 (split-with (partial not= 0x50) data)]
ocsenave@259 316 (recur (dec n)
ocsenave@259 317 (assoc stats
ocsenave@285 318 (pokedex-names (- pkmn-count (dec n)))
ocsenave@259 319 {:species
ocsenave@285 320 (format-name (character-codes->str species))
ocsenave@259 321 :height-ft
ocsenave@259 322 height-ft
ocsenave@259 323 :height-in
ocsenave@259 324 height-in
ocsenave@259 325 :weight
ocsenave@259 326 (/ (low-high weight-1 weight-2) 10.)
ocsenave@259 327
ocsenave@259 328 ;; :text
ocsenave@259 329 ;; (character-codes->str
ocsenave@259 330 ;; (take-while
ocsenave@259 331 ;; (partial not= 0x50)
ocsenave@259 332 ;; (drop
ocsenave@259 333 ;; (+ 0xB8000
ocsenave@259 334 ;; -0x4000
ocsenave@259 335 ;; (low-high dex-ptr-1 dex-ptr-2))
ocsenave@259 336 ;; rom)))
ocsenave@259 337 })
ocsenave@259 338
ocsenave@259 339 data)
ocsenave@259 340
ocsenave@259 341
ocsenave@259 342 )))
ocsenave@259 343
ocsenave@259 344 pkmn-count
ocsenave@259 345 {}
ocsenave@259 346 (drop 0x40687 rom))) ))
ocsenave@259 347
ocsenave@259 348
ocsenave@259 349
ocsenave@259 350
ocsenave@246 351 (def hxc-places
ocsenave@246 352 "The hardcoded place names in memory. List begins at
ocsenave@249 353 ROM@71500. [Cinnabar] Mansion seems to be dynamically calculated."
ocsenave@249 354 (hxc-thunk-words 0x71500 560))
ocsenave@246 355
ocsenave@246 356
ocsenave@249 357 (defn hxc-dialog
ocsenave@249 358 "The hardcoded dialogue in memory, including in-game alerts. Dialog
ocsenave@249 359 seems to be separated by 0x57 instead of 0x50 (END). Begins at ROM@98000."
ocsenave@249 360 ([rom]
ocsenave@249 361 (map character-codes->str
ocsenave@249 362 (take-nth 2
ocsenave@249 363 (partition-by #(= % 0x57)
ocsenave@249 364 (take 0x0F728
ocsenave@249 365 (drop 0x98000 rom))))))
ocsenave@249 366 ([]
ocsenave@249 367 (hxc-dialog com.aurellem.gb.gb-driver/original-rom)))
ocsenave@249 368
ocsenave@246 369
ocsenave@246 370 (def hxc-move-names
ocsenave@246 371 "The hardcoded move names in memory. List begins at ROM@BC000"
ocsenave@249 372 (hxc-thunk-words 0xBC000 1551))
ocsenave@249 373 (defn hxc-move-data
ocsenave@246 374 "The hardcoded (basic (move effects)) in memory. List begins at
ocsenave@249 375 0x38000. Returns a map of {:name :power :accuracy :pp :fx-id
ocsenave@249 376 :fx-txt}. The move descriptions are handwritten, not hardcoded."
ocsenave@249 377 ([]
ocsenave@249 378 (hxc-move-data com.aurellem.gb.gb-driver/original-rom))
ocsenave@249 379 ([rom]
ocsenave@249 380 (let [names (vec (hxc-move-names rom))
ocsenave@249 381 move-count (count names)
ocsenave@281 382 move-size 6
ocsenave@281 383 types pkmn-types ;;; !! hardcoded types
ocsenave@281 384 ]
ocsenave@249 385 (zipmap (map format-name names)
ocsenave@249 386 (map
ocsenave@281 387 (fn [[idx effect power type-id accuracy pp]]
ocsenave@249 388 {:name (names (dec idx))
ocsenave@249 389 :power power
ocsenave@249 390 :accuracy accuracy
ocsenave@249 391 :pp pp
ocsenave@281 392 :type (types type-id)
ocsenave@249 393 :fx-id effect
ocsenave@249 394 :fx-txt (get move-effects effect)
ocsenave@249 395 }
ocsenave@249 396 )
ocsenave@249 397
ocsenave@249 398 (partition move-size
ocsenave@249 399 (take (* move-size move-count)
ocsenave@249 400 (drop 0x38000 rom))))))))
ocsenave@246 401
ocsenave@246 402
ocsenave@246 403
ocsenave@249 404 (defn hxc-move-data*
ocsenave@249 405 "Like hxc-move-data, but reports numbers as hexadecimal symbols instead."
ocsenave@249 406 ([]
ocsenave@249 407 (hxc-move-data* com.aurellem.gb.gb-driver/original-rom))
ocsenave@249 408 ([rom]
ocsenave@249 409 (let [names (vec (hxc-move-names rom))
ocsenave@249 410 move-count (count names)
ocsenave@249 411 move-size 6
ocsenave@249 412 format-name (fn [s]
ocsenave@249 413 (keyword (.toLowerCase
ocsenave@249 414 (apply str
ocsenave@249 415 (map #(if (= % \space) "-" %) s)))))
ocsenave@249 416 ]
ocsenave@249 417 (zipmap (map format-name names)
ocsenave@249 418 (map
ocsenave@249 419 (fn [[idx effect power type accuracy pp]]
ocsenave@249 420 {:name (names (dec idx))
ocsenave@249 421 :power power
ocsenave@249 422 :accuracy (hex accuracy)
ocsenave@249 423 :pp pp
ocsenave@249 424 :fx-id (hex effect)
ocsenave@249 425 :fx-txt (get move-effects effect)
ocsenave@249 426 }
ocsenave@249 427 )
ocsenave@249 428
ocsenave@249 429 (partition move-size
ocsenave@249 430 (take (* move-size move-count)
ocsenave@249 431 (drop 0x38000 rom))))))))
ocsenave@243 432
ocsenave@243 433
ocsenave@283 434 (defn hxc-machines
ocsenave@312 435 "The hardcoded moves taught by TMs and HMs. List begins at ROM@1232D."
ocsenave@283 436 ([] (hxc-machines
ocsenave@283 437 com.aurellem.gb.gb-driver/original-rom))
ocsenave@283 438 ([rom]
ocsenave@283 439 (let [moves (hxc-move-names rom)]
ocsenave@283 440 (zipmap
ocsenave@283 441 (range)
ocsenave@283 442 (take-while
ocsenave@283 443 (comp not nil?)
ocsenave@283 444 (map (comp
ocsenave@283 445 format-name
ocsenave@283 446 (zipmap
ocsenave@283 447 (range)
ocsenave@283 448 moves)
ocsenave@283 449 dec)
ocsenave@283 450 (take 100
ocsenave@283 451 (drop 0x1232D rom))))))))
ocsenave@285 452
ocsenave@285 453
ocsenave@259 454
ocsenave@348 455
ocsenave@259 456 (defn internal-id
ocsenave@259 457 ([rom]
ocsenave@259 458 (zipmap
ocsenave@288 459 (hxc-pokenames rom)
ocsenave@259 460 (range)))
ocsenave@259 461 ([]
ocsenave@259 462 (internal-id com.aurellem.gb.gb-driver/original-rom)))
ocsenave@285 463
ocsenave@285 464
ocsenave@285 465
ocsenave@259 466
ocsenave@259 467
ocsenave@263 468 ;; nidoran gender change upon levelup
ocsenave@263 469 ;; (->
ocsenave@263 470 ;; @current-state
ocsenave@263 471 ;; rom
ocsenave@263 472 ;; vec
ocsenave@263 473 ;; (rewrite-memory
ocsenave@263 474 ;; (nth (hxc-ptrs-evolve) ((internal-id) :nidoran♂))
ocsenave@263 475 ;; [1 1 15])
ocsenave@263 476 ;; (rewrite-memory
ocsenave@263 477 ;; (nth (hxc-ptrs-evolve) ((internal-id) :nidoran♀))
ocsenave@263 478 ;; [1 1 3])
ocsenave@263 479 ;; (write-rom!)
ocsenave@263 480
ocsenave@263 481 ;; )
ocsenave@263 482
ocsenave@259 483
ocsenave@259 484
ocsenave@259 485
ocsenave@243 486
ocsenave@243 487
ocsenave@281 488
ocsenave@263 489 (defn format-evo
ocsenave@347 490 "Parse a sequence of evolution data, returning a map. First is the
ocsenave@347 491 method: 0 = end-evolution-data. 1 = level-up, 2 = item, 3 = trade. Next is an item id, if the
ocsenave@347 492 method of evolution is by item (only stones will actually make pokemon
ocsenave@347 493 evolve, for some auxillary reason.) Finally, the minimum level for
ocsenave@347 494 evolution to occur (level 1 means no limit, which is used for trade
ocsenave@347 495 and item evolutions), followed by the internal id of the pokemon
ocsenave@347 496 into which to evolve. Hence, level up and trade evolutions are
ocsenave@347 497 described with 3
ocsenave@347 498 bytes; item evolutions with four."
ocsenave@263 499 [coll]
ocsenave@263 500 (let [method (first coll)]
ocsenave@263 501 (cond (empty? coll) []
ocsenave@263 502 (= 0 method) [] ;; just in case
ocsenave@263 503 (= 1 method) ;; level-up evolution
ocsenave@263 504 (conj (format-evo (drop 3 coll))
ocsenave@263 505 {:method :level-up
ocsenave@263 506 :min-level (nth coll 1)
ocsenave@263 507 :into (dec (nth coll 2))})
ocsenave@263 508
ocsenave@263 509 (= 2 method) ;; item evolution
ocsenave@263 510 (conj (format-evo (drop 4 coll))
ocsenave@263 511 {:method :item
ocsenave@263 512 :item (dec (nth coll 1))
ocsenave@263 513 :min-level (nth coll 2)
ocsenave@263 514 :into (dec (nth coll 3))})
ocsenave@243 515
ocsenave@263 516 (= 3 method) ;; trade evolution
ocsenave@263 517 (conj (format-evo (drop 3 coll))
ocsenave@263 518 {:method :trade
ocsenave@263 519 :min-level (nth coll 1) ;; always 1 for trade.
ocsenave@263 520 :into (dec (nth coll 2))}))))
ocsenave@243 521
ocsenave@243 522
ocsenave@263 523 (defn hxc-ptrs-evolve
ocsenave@267 524 "A hardcoded collection of 190 pointers to alternating evolution/learnset data,
ocsenave@263 525 in internal order."
ocsenave@263 526 ([]
ocsenave@263 527 (hxc-ptrs-evolve com.aurellem.gb.gb-driver/original-rom))
ocsenave@259 528 ([rom]
ocsenave@288 529 (let [
ocsenave@288 530 pkmn-count (count (hxc-pokenames-raw)) ;; 190
ocsenave@259 531 ptrs
ocsenave@263 532 (map (fn [[a b]] (low-high a b))
ocsenave@259 533 (partition 2
ocsenave@259 534 (take (* 2 pkmn-count)
ocsenave@263 535 (drop 0x3b1e5 rom))))]
ocsenave@263 536 (map (partial + 0x34000) ptrs)
ocsenave@263 537
ocsenave@263 538 )))
ocsenave@263 539
ocsenave@267 540
ocsenave@267 541 (defn hxc-learnsets
ocsenave@267 542 "Hardcoded map associating pokemon names to lists of pairs [lvl
ocsenave@267 543 move] of abilities they learn as they level up. The data
ocsenave@347 544 exists at ROM@34000, sorted by internal order. Pointers to the data
ocsenave@267 545 exist at ROM@3B1E5; see also, hxc-ptrs-evolve"
ocsenave@267 546 ([] (hxc-learnsets com.aurellem.gb.gb-driver/original-rom))
ocsenave@267 547 ([rom]
ocsenave@267 548 (apply assoc
ocsenave@267 549 {}
ocsenave@267 550 (interleave
ocsenave@288 551 (hxc-pokenames rom)
ocsenave@267 552 (map (comp
ocsenave@268 553 (partial map
ocsenave@268 554 (fn [[lvl mv]] [lvl (dec mv)]))
ocsenave@267 555 (partial partition 2)
ocsenave@267 556 ;; keep the learnset data
ocsenave@267 557 (partial take-while (comp not zero?))
ocsenave@267 558 ;; skip the evolution data
ocsenave@267 559 rest
ocsenave@267 560 (partial drop-while (comp not zero?)))
ocsenave@267 561 (map #(drop % rom)
ocsenave@267 562 (hxc-ptrs-evolve rom)))))))
ocsenave@267 563
ocsenave@267 564 (defn hxc-learnsets-pretty
ocsenave@267 565 "Live hxc-learnsets except it reports the name of each move --- as
ocsenave@267 566 it appears in rom --- rather than the move index."
ocsenave@267 567 ([] (hxc-learnsets-pretty com.aurellem.gb.gb-driver/original-rom))
ocsenave@267 568 ([rom]
ocsenave@267 569 (let [moves (vec(map format-name (hxc-move-names)))]
ocsenave@267 570 (into {}
ocsenave@267 571 (map (fn [[pkmn learnset]]
ocsenave@268 572 [pkmn (map (fn [[lvl mv]] [lvl (moves mv)])
ocsenave@267 573 learnset)])
ocsenave@267 574 (hxc-learnsets rom))))))
ocsenave@267 575
ocsenave@267 576
ocsenave@267 577
ocsenave@273 578 (defn hxc-pokemon-base
ocsenave@273 579 ([] (hxc-pokemon-base com.aurellem.gb.gb-driver/original-rom))
ocsenave@273 580 ([rom]
ocsenave@273 581 (let [entry-size 28
ocsenave@273 582 pkmn-count (count (hxc-pokedex-text rom))
ocsenave@285 583 pokemon (rest (hxc-pokedex-names))
ocsenave@273 584 types (apply assoc {}
ocsenave@273 585 (interleave
ocsenave@273 586 (range)
ocsenave@273 587 pkmn-types)) ;;!! softcoded
ocsenave@273 588 moves (apply assoc {}
ocsenave@273 589 (interleave
ocsenave@273 590 (range)
ocsenave@273 591 (map format-name
ocsenave@273 592 (hxc-move-names rom))))
ocsenave@288 593 machines (hxc-machines)
ocsenave@273 594 ]
ocsenave@285 595 (zipmap
ocsenave@285 596 pokemon
ocsenave@285 597 (map
ocsenave@285 598 (fn [[n
ocsenave@285 599 rating-hp
ocsenave@285 600 rating-atk
ocsenave@285 601 rating-def
ocsenave@285 602 rating-speed
ocsenave@285 603 rating-special
ocsenave@285 604 type-1
ocsenave@285 605 type-2
ocsenave@285 606 rarity
ocsenave@285 607 rating-xp
ocsenave@285 608 pic-dimensions ;; tile_width|tile_height (8px/tile)
ocsenave@285 609 ptr-pic-obverse-1
ocsenave@285 610 ptr-pic-obverse-2
ocsenave@285 611 ptr-pic-reverse-1
ocsenave@285 612 ptr-pic-reverse-2
ocsenave@285 613 move-1
ocsenave@285 614 move-2
ocsenave@285 615 move-3
ocsenave@285 616 move-4
ocsenave@285 617 growth-rate
ocsenave@285 618 &
ocsenave@285 619 TMs|HMs]]
ocsenave@285 620 (let
ocsenave@285 621 [base-moves
ocsenave@285 622 (mapv moves
ocsenave@285 623 ((comp
ocsenave@285 624 ;; since the game uses zero as a delimiter,
ocsenave@285 625 ;; it must also increment all move indices by 1.
ocsenave@285 626 ;; heren we decrement to correct this.
ocsenave@285 627 (partial map dec)
ocsenave@285 628 (partial take-while (comp not zero?)))
ocsenave@285 629 [move-1 move-2 move-3 move-4]))
ocsenave@285 630
ocsenave@285 631 types
ocsenave@285 632 (set (list (types type-1)
ocsenave@285 633 (types type-2)))
ocsenave@285 634 TMs|HMs
ocsenave@285 635 (map
ocsenave@285 636 (comp
ocsenave@285 637 (partial map first)
ocsenave@285 638 (partial remove (comp zero? second)))
ocsenave@285 639 (split-at
ocsenave@285 640 50
ocsenave@285 641 (map vector
ocsenave@285 642 (rest(range))
ocsenave@285 643 (reduce concat
ocsenave@285 644 (map
ocsenave@285 645 #(take 8
ocsenave@285 646 (concat (bit-list %)
ocsenave@285 647 (repeat 0)))
ocsenave@285 648
ocsenave@273 649 TMs|HMs)))))
ocsenave@285 650
ocsenave@285 651 TMs (vec (first TMs|HMs))
ocsenave@285 652 HMs (take 5 (map (partial + -50) (vec (second TMs|HMs))))
ocsenave@285 653
ocsenave@285 654
ocsenave@285 655 ]
ocsenave@285 656
ocsenave@285 657
ocsenave@285 658 {:dex# n
ocsenave@285 659 :base-moves base-moves
ocsenave@285 660 :types types
ocsenave@285 661 :TMs TMs
ocsenave@285 662 :HMs HMs
ocsenave@285 663 :base-hp rating-hp
ocsenave@285 664 :base-atk rating-atk
ocsenave@285 665 :base-def rating-def
ocsenave@285 666 :base-speed rating-speed
ocsenave@285 667 :base-special rating-special
ocsenave@310 668 :o0 pic-dimensions
ocsenave@310 669 :o1 ptr-pic-obverse-1
ocsenave@310 670 :o2 ptr-pic-obverse-2
ocsenave@285 671 }))
ocsenave@285 672
ocsenave@285 673 (partition entry-size
ocsenave@285 674 (take (* entry-size pkmn-count)
ocsenave@285 675 (drop 0x383DE
ocsenave@285 676 rom))))))))
ocsenave@348 677
ocsenave@285 678
ocsenave@285 679
ocsenave@310 680 (defn hxc-intro-pkmn
ocsenave@310 681 "The hardcoded pokemon to display in Prof. Oak's introduction; the pokemon's
ocsenave@310 682 internal id is stored at ROM@5EDB."
ocsenave@310 683 ([] (hxc-intro-pkmn
ocsenave@310 684 com.aurellem.gb.gb-driver/original-rom))
ocsenave@310 685 ([rom]
ocsenave@310 686 (nth (hxc-pokenames rom) (nth rom 0x5EDB))))
ocsenave@310 687
ocsenave@310 688 (defn sxc-intro-pkmn!
ocsenave@310 689 "Set the hardcoded pokemon to display in Prof. Oak's introduction."
ocsenave@310 690 [pokemon]
ocsenave@310 691 (write-rom!
ocsenave@310 692 (rewrite-rom 0x5EDB
ocsenave@310 693 [
ocsenave@310 694 (inc
ocsenave@310 695 ((zipmap
ocsenave@310 696 (hxc-pokenames)
ocsenave@310 697 (range))
ocsenave@310 698 pokemon))])))
ocsenave@285 699
ocsenave@310 700
ocsenave@282 701 (defn hxc-item-prices
ocsenave@282 702 "The hardcoded list of item prices in memory. List begins at ROM@4495"
ocsenave@282 703 ([] (hxc-item-prices com.aurellem.gb.gb-driver/original-rom))
ocsenave@282 704 ([rom]
ocsenave@288 705 (let [items (hxc-items rom)
ocsenave@282 706 price-size 3]
ocsenave@282 707 (zipmap items
ocsenave@282 708 (map (comp
ocsenave@282 709 ;; zero-cost items are "priceless"
ocsenave@282 710 #(if (zero? %) :priceless %)
ocsenave@282 711 decode-bcd butlast)
ocsenave@282 712 (partition price-size
ocsenave@282 713 (take (* price-size (count items))
ocsenave@282 714 (drop 0x4495 rom))))))))
ocsenave@273 715
ocsenave@281 716 (defn hxc-shops
ocsenave@281 717 ([] (hxc-shops com.aurellem.gb.gb-driver/original-rom))
ocsenave@281 718 ([rom]
ocsenave@288 719 (let [items (zipmap (range) (hxc-items rom))
ocsenave@281 720
ocsenave@281 721 ;; temporarily softcode the TM items
ocsenave@281 722 items (into
ocsenave@281 723 items
ocsenave@281 724 (map (juxt identity
ocsenave@281 725 (comp keyword
ocsenave@281 726 (partial str "tm-")
ocsenave@281 727 (partial + 1 -200)
ocsenave@281 728 ))
ocsenave@281 729 (take 200 (drop 200 (range)))))
ocsenave@282 730
ocsenave@281 731 ]
ocsenave@281 732
ocsenave@281 733 ((fn parse-shop [coll [num-items & items-etc]]
ocsenave@282 734 (let [inventory (take-while
ocsenave@282 735 (partial not= 0xFF)
ocsenave@282 736 items-etc)
ocsenave@281 737 [separator & items-etc] (drop num-items (rest items-etc))]
ocsenave@281 738 (if (= separator 0x50)
ocsenave@281 739 (map (partial mapv (comp items dec)) (conj coll inventory))
ocsenave@281 740 (recur (conj coll inventory) items-etc)
ocsenave@281 741 )
ocsenave@281 742 ))
ocsenave@281 743
ocsenave@281 744 '()
ocsenave@282 745 (drop 0x233C rom))
ocsenave@281 746
ocsenave@281 747
ocsenave@281 748 )))
ocsenave@281 749
ocsenave@281 750
ocsenave@273 751
ocsenave@292 752
ocsenave@292 753 (defn hxc-ptrs-wild
ocsenave@292 754 "A list of the hardcoded wild encounter data in memory. Pointers
ocsenave@292 755 begin at ROM@0CB95; data begins at ROM@0x04D89"
ocsenave@292 756 ([] (hxc-ptrs-wild com.aurellem.gb.gb-driver/original-rom))
ocsenave@292 757 ([rom]
ocsenave@292 758 (let [ptrs
ocsenave@292 759 (map (fn [[a b]] (+ a (* 0x100 b)))
ocsenave@292 760 (take-while (partial not= (list 0xFF 0xFF))
ocsenave@292 761 (partition 2 (drop 0xCB95 rom))))]
ocsenave@292 762 ptrs)))
ocsenave@292 763
ocsenave@292 764
ocsenave@292 765
ocsenave@292 766 (defn hxc-wilds
ocsenave@292 767 "A list of the hardcoded wild encounter data in memory. Pointers
ocsenave@292 768 begin at ROM@0CB95; data begins at ROM@0x04D89"
ocsenave@292 769 ([] (hxc-wilds com.aurellem.gb.gb-driver/original-rom))
ocsenave@292 770 ([rom]
ocsenave@292 771 (let [pokenames (zipmap (range) (hxc-pokenames rom))]
ocsenave@292 772 (map
ocsenave@292 773 (partial map (fn [[a b]] {:species (pokenames (dec b)) :level
ocsenave@292 774 a}))
ocsenave@292 775 (partition 10
ocsenave@292 776
ocsenave@292 777 (take-while (comp (partial not= 1)
ocsenave@292 778 first)
ocsenave@292 779 (partition 2
ocsenave@292 780 (drop 0xCD8C rom))
ocsenave@292 781
ocsenave@292 782 ))))))
ocsenave@292 783
ocsenave@292 784
ocsenave@292 785
ocsenave@249 786 ;; ********************** MANIPULATION FNS
ocsenave@249 787
ocsenave@249 788
ocsenave@285 789 (defn same-type
ocsenave@285 790 ([pkmn move]
ocsenave@288 791 (same-type
ocsenave@285 792 com.aurellem.gb.gb-driver/original-rom pkmn move))
ocsenave@285 793 ([rom pkmn move]
ocsenave@285 794 (((comp :types (hxc-pokemon-base rom)) pkmn)
ocsenave@285 795 ((comp :type (hxc-move-data rom)) move))))
ocsenave@285 796
ocsenave@285 797
ocsenave@249 798
ocsenave@249 799
ocsenave@249 800 (defn submap?
ocsenave@249 801 "Compares the two maps. Returns true if map-big has the same associations as map-small, otherwise false."
ocsenave@249 802 [map-small map-big]
ocsenave@249 803 (cond (empty? map-small) true
ocsenave@249 804 (and
ocsenave@249 805 (contains? map-big (ffirst map-small))
ocsenave@249 806 (= (get map-big (ffirst map-small))
ocsenave@249 807 (second (first map-small))))
ocsenave@249 808 (recur (next map-small) map-big)
ocsenave@249 809
ocsenave@249 810 :else false))
ocsenave@249 811
ocsenave@249 812
ocsenave@249 813 (defn search-map [proto-map maps]
ocsenave@249 814 "Returns all the maps that make the same associations as proto-map."
ocsenave@249 815 (some (partial submap? proto-map) maps))
ocsenave@249 816
rlm@252 817 (defn filter-vals
rlm@252 818 "Returns a map consisting of all the pairs [key val] for
rlm@252 819 which (pred key) returns true."
rlm@252 820 [pred map]
rlm@252 821 (reduce (partial apply assoc) {}
rlm@252 822 (filter (fn [[k v]] (pred v)) map)))
ocsenave@249 823
ocsenave@249 824
ocsenave@249 825 (defn search-moves
rlm@252 826 "Returns a subcollection of all hardcoded moves with the
rlm@252 827 given attributes. Attributes consist of :name :power
rlm@252 828 :accuracy :pp :fx-id
rlm@252 829 (and also :fx-txt, but it contains the same information
rlm@252 830 as :fx-id)"
ocsenave@249 831 ([attribute-map]
rlm@252 832 (search-moves
rlm@252 833 com.aurellem.gb.gb-driver/original-rom attribute-map))
ocsenave@249 834 ([rom attribute-map]
rlm@252 835 (filter-vals (partial submap? attribute-map)
rlm@252 836 (hxc-move-data rom))))
ocsenave@249 837
ocsenave@249 838
ocsenave@249 839
ocsenave@249 840
ocsenave@243 841
ocsenave@283 842 ;; note: 0x2f31 contains the names "TM" "HM"?
ocsenave@283 843
ocsenave@246 844 ;; note for later: credits start at F1290
ocsenave@243 845
ocsenave@346 846 ;; note: DADB hyper-potion-hp _ _ _ super-potion-hp _ _ _ potion-hp ??
ocsenave@243 847
ocsenave@346 848 ;; note: DD4D spells out pokemon vital stat names ("speed", etc.)
ocsenave@346 849
ocsenave@346 850 ;; note: 1195C-6A says ABLE#NOT ABLE#, but so does 119C0-119CE.
ocsenave@346 851 ;; The first instance is for Machines; the second, for stones.
ocsenave@243 852
ocsenave@347 853 ;; 0x251A (in indexable mem): image decompression routine seems to begin here.
ocsenave@347 854
ocsenave@347 855
ocsenave@246 856 (comment
ocsenave@243 857
rlm@218 858 (def hxc-later
rlm@218 859 "Running this code produces, e.g. hardcoded names NPCs give
rlm@218 860 their pokemon. Will sort through it later."
rlm@218 861 (print (character-codes->str(take 10000
rlm@218 862 (drop 0x71597
rlm@218 863 (rom (root)))))))
rlm@218 864
rlm@218 865 (let [dex
rlm@218 866 (partition-by #(= 0x50 %)
rlm@218 867 (take 2540
rlm@218 868 (drop 0x40687
rlm@218 869 (rom (root)))))]
rlm@218 870 (def dex dex)
rlm@218 871 (def hxc-species
rlm@218 872 (map character-codes->str
rlm@218 873 (take-nth 4 dex))))
ocsenave@259 874 )