Mercurial > vba-clojure
view clojure/com/aurellem/gb/hxc.clj @ 276:18336ab5d6ea
merge.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Tue, 27 Mar 2012 12:37:48 -0500 |
parents | 69184558fcf3 |
children | ca1afcef3542 |
line wrap: on
line source
1 (ns com.aurellem.gb.hxc2 (:use (com.aurellem.gb assembly characters gb-driver util3 constants))4 (:use (com.aurellem.world practice))5 (:import [com.aurellem.gb.gb_driver SaveState]))10 ; ************* HANDWRITTEN CONSTANTS14 (defn low-high15 [low high]16 (+ low (* 256 high)))19 (defn format-name20 "Convert the string of alphabetic/space characters into a keyword by21 replacing spaces with hyphens and converting to lowercase."22 [s]23 (keyword (.toLowerCase24 (apply str25 (map #(if (= % \space) "-" %) s)))))29 (def pkmn-types30 [:normal ;;031 :fighting ;;132 :flying ;;233 :poison ;;334 :ground ;;435 :rock ;;536 :bird ;;637 :bug ;;738 :ghost ;;839 :A40 :B41 :C42 :D43 :E44 :F45 :G46 :H47 :I48 :J49 :K50 :fire ;;20 (0x14)51 :water ;;21 (0x15)52 :grass ;;22 (0x16)53 :electric ;;23 (0x17)54 :psychic ;;24 (0x18)55 :ice ;;25 (0x19)56 :dragon ;;26 (0x1A)57 ])60 ;; question: when status effects claim to take61 ;; their accuracy from the move accuracy, does62 ;; this mean that the move always "hits" but the63 ;; status effect may not?65 (def move-effects66 ["normal damage"67 "no damage, just opponent sleep" ;; how many turns? is atk power ignored?68 "0x4C chance of poison"69 "leech half of inflicted damage"70 "0x19 chance of burn"71 "0x19 chance of freeze"72 "0x19 chance of paralyze"73 "user faints; opponent defense halved during attack."74 "leech half of inflicted damage ONLY if sleeping opponent."75 "imitate last attack"76 "user atk +1"77 "user def +1"78 "user spd +1"79 "user spc +1"80 "user acr +1" ;; unused?!81 "user evd +1"82 "get post-battle $ = 2*level*uses"83 "0xFE acr, no matter what."84 "opponent atk -1" ;; acr taken from move acr?85 "opponent def -1" ;;86 "opponent spd -1" ;;87 "opponent spc -1" ;;88 "opponent acr -1";;89 "opponent evd -1"90 "converts user's type to opponent's."91 "(haze)"92 "(bide)"93 "(thrash)"94 "(teleport)"95 "(fury swipes)"96 "attacks 2-5 turns" ;; unused? like rollout?97 "0x19 chance of flinch"98 "opponent sleep for 1-7 turns"99 "0x66 chance of poison"100 "0x4D chance of burn"101 "0x4D chance of freeze"102 "0x4D chance of paralyze"103 "0x4D chance of flinch"104 "one-hit KO"105 "charge one turn, atk next."106 "fixed damage, leaves 1HP." ;; how is dmg determined?107 "fixed damage." ;; cf seismic toss, dragon rage, psywave.108 "atk 2-5 turns; opponent can't attack" ;; unnormalized? (0 0x60 0x60 0x20 0x20)109 "charge one turn, atk next. (can't be hit when charging)"110 "atk hits twice."111 "user takes 1 damage if misses."112 "evade status-lowering effects" ;;caused by you or also your opponent?113 "(broken) if user is slower than opponent, makes critical hit impossible, otherwise has no effect"114 "atk causes recoil dmg = 1/4 dmg dealt"115 "confuses opponent" ;; acr taken from move acr116 "user atk +2"117 "user def +2"118 "user spd +2"119 "user spc +2"120 "user acr +2" ;; unused!121 "user evd +2" ;; unused!122 "restores up to half of user's max hp." ;; broken: fails if the difference123 ;; b/w max and current hp is one less than a multiple of 256.124 "(transform)"125 "opponent atk -2"126 "opponent def -2"127 "opponent spd -2"128 "opponent spc -2"129 "opponent acr -2"130 "opponent evd -2"131 "doubles user spc when attacked"132 "doubles user def when attacked"133 "just poisons opponent" ;;acr taken from move acr134 "just paralyzes opponent" ;;135 "0x19 chance opponent atk -1"136 "0x19 chance opponent def -1"137 "0x19 chance opponent spd -1"138 "0x4C chance opponent spc -1" ;; context suggest chance is 0x19139 "0x19 chance opponent acr -1"140 "0x19 chance opponent evd -1"141 "???" ;; unused? no effect?142 "???" ;; unused? no effect?143 "0x19 chance opponent confused"144 "atk hits twice. 0x33 chance opponent poisioned."145 "broken. crash the game after attack."146 "(substitute)"147 "unless opponent faints, user must recharge after atk. some148 exceptions apply."149 "(rage)"150 "(mimic)"151 "(metronome)"152 "(leech seed)"153 "does nothing (splash)"154 "(disable)"155 ])158 ;; ************** HARDCODED DATA160 (defn hxc-thunk161 "Creates a thunk (nullary fn) that grabs data in a certain region of rom and162 splits it into a collection by 0x50. If rom is not supplied, uses the163 original rom data."164 [start length]165 (fn self166 ([rom]167 (take-nth 2168 (partition-by #(= % 0x50)169 (take length170 (drop start rom)))))171 ([]172 (self com.aurellem.gb.gb-driver/original-rom))))174 (def hxc-thunk-words175 "Same as hxc-thunk, except it interprets the rom data as characters,176 returning a collection of strings."177 (comp178 (partial comp (partial map character-codes->str))179 hxc-thunk))182 ;; --------------------------------------------------184 (def hxc-items185 "The hardcoded names of the items in memory. List begins at186 ROM@045B7"187 (hxc-thunk-words 0x45B7 870))189 (def hxc-types190 "The hardcoded type names in memory. List begins at ROM@27D99,191 shortly before hxc-titles."192 (hxc-thunk-words 0x27D99 102))194 (def hxc-titles195 "The hardcoded names of the trainer titles in memory. List begins at196 ROM@27E77"197 (hxc-thunk-words 0x27E77 196))200 (def hxc-pokedex-text201 "The hardcoded pokedex entries in memory. List begins at202 ROM@B8000, shortly before move names."203 (hxc-thunk-words 0xB8000 14754))206 ;; In red/blue, pokedex stats are in internal order.207 ;; In yellow, pokedex stats are in pokedex order.209 (defn hxc-pokedex-stats210 "The hardcoded pokedex stats (species height weight) in memory. List211 begins at ROM@40687"212 ;; uses hxc-pokedex-text to count pokemon213 ;; since hxc-pokenames includes several missingno"214 ([] (hxc-pokedex-stats com.aurellem.gb.gb-driver/original-rom))215 ([rom]216 (let [poketext (hxc-pokedex-text)217 pkmn-count (count poketext)218 ]219 ((fn capture-stats220 [n stats data]221 (if (zero? n) stats222 (let [[species223 [_224 height-ft225 height-in226 weight-1227 weight-2228 _229 dex-ptr-1230 dex-ptr-2231 dex-bank232 _233 & data]]234 (split-with (partial not= 0x50) data)]235 (recur (dec n)236 (assoc stats237 (- pkmn-count n)238 {:species239 (character-codes->str species)240 :height-ft241 height-ft242 :height-in243 height-in244 :weight245 (/ (low-high weight-1 weight-2) 10.)247 ;; :text248 ;; (character-codes->str249 ;; (take-while250 ;; (partial not= 0x50)251 ;; (drop252 ;; (+ 0xB8000253 ;; -0x4000254 ;; (low-high dex-ptr-1 dex-ptr-2))255 ;; rom)))256 })258 data)261 )))263 pkmn-count264 {}265 (drop 0x40687 rom))) ))273 (def hxc-places274 "The hardcoded place names in memory. List begins at275 ROM@71500. [Cinnabar] Mansion seems to be dynamically calculated."276 (hxc-thunk-words 0x71500 560))279 (defn hxc-dialog280 "The hardcoded dialogue in memory, including in-game alerts. Dialog281 seems to be separated by 0x57 instead of 0x50 (END). Begins at ROM@98000."282 ([rom]283 (map character-codes->str284 (take-nth 2285 (partition-by #(= % 0x57)286 (take 0x0F728287 (drop 0x98000 rom))))))288 ([]289 (hxc-dialog com.aurellem.gb.gb-driver/original-rom)))294 (def hxc-move-names295 "The hardcoded move names in memory. List begins at ROM@BC000"296 (hxc-thunk-words 0xBC000 1551))299 (defn hxc-move-data300 "The hardcoded (basic (move effects)) in memory. List begins at301 0x38000. Returns a map of {:name :power :accuracy :pp :fx-id302 :fx-txt}. The move descriptions are handwritten, not hardcoded."303 ([]304 (hxc-move-data com.aurellem.gb.gb-driver/original-rom))305 ([rom]306 (let [names (vec (hxc-move-names rom))307 move-count (count names)308 move-size 6]309 (zipmap (map format-name names)310 (map311 (fn [[idx effect power type accuracy pp]]312 {:name (names (dec idx))313 :power power314 :accuracy accuracy315 :pp pp316 :fx-id effect317 :fx-txt (get move-effects effect)318 }319 )321 (partition move-size322 (take (* move-size move-count)323 (drop 0x38000 rom))))))))327 (defn hxc-move-data*328 "Like hxc-move-data, but reports numbers as hexadecimal symbols instead."329 ([]330 (hxc-move-data* com.aurellem.gb.gb-driver/original-rom))331 ([rom]332 (let [names (vec (hxc-move-names rom))333 move-count (count names)334 move-size 6335 format-name (fn [s]336 (keyword (.toLowerCase337 (apply str338 (map #(if (= % \space) "-" %) s)))))339 ]340 (zipmap (map format-name names)341 (map342 (fn [[idx effect power type accuracy pp]]343 {:name (names (dec idx))344 :power power345 :accuracy (hex accuracy)346 :pp pp347 :fx-id (hex effect)348 :fx-txt (get move-effects effect)349 }350 )352 (partition move-size353 (take (* move-size move-count)354 (drop 0x38000 rom))))))))358 (defn hxc-pokenames359 "The hardcoded names of the 190 species in memory. List begins at360 ROM@E8000. Although names in memory are padded with 0x50 to be 10 characters361 long, these names are stripped of padding."362 ([]363 (hxc-pokenames com.aurellem.gb.gb-driver/original-rom))364 ([rom]365 (let [count-species 190366 name-length 10]367 (map character-codes->str368 (partition name-length369 (map #(if (= 0x50 %) 0x00 %)370 (take (* count-species name-length)371 (drop 0xE8000372 rom))))))))377 (defn internal-id378 ([rom]379 (zipmap380 (map format-name (hxc-pokenames rom))381 (range)))382 ([]383 (internal-id com.aurellem.gb.gb-driver/original-rom)))387 ;; nidoran gender change upon levelup388 ;; (->389 ;; @current-state390 ;; rom391 ;; vec392 ;; (rewrite-memory393 ;; (nth (hxc-ptrs-evolve) ((internal-id) :nidoran♂))394 ;; [1 1 15])395 ;; (rewrite-memory396 ;; (nth (hxc-ptrs-evolve) ((internal-id) :nidoran♀))397 ;; [1 1 3])398 ;; (write-rom!)400 ;; )405 (defn hxc-advantage406 "The hardcoded type advantages in memory, returned as tuples of atk-type def-type multiplier. By default (i.e. if not listed here),407 the multiplier is 1."408 ([] (hxc-advantage com.aurellem.gb.gb-driver/original-rom))409 ([rom]410 (map411 (fn [[atk def mult]] [(get pkmn-types atk (hex atk))412 (get pkmn-types def (hex def))413 (/ mult 10)])414 (partition 3415 (take-while (partial not= 0xFF)416 (drop 0x3E62D rom))))))419 (defn format-evo420 [coll]421 (let [method (first coll)]422 (cond (empty? coll) []423 (= 0 method) [] ;; just in case424 (= 1 method) ;; level-up evolution425 (conj (format-evo (drop 3 coll))426 {:method :level-up427 :min-level (nth coll 1)428 :into (dec (nth coll 2))})430 (= 2 method) ;; item evolution431 (conj (format-evo (drop 4 coll))432 {:method :item433 :item (dec (nth coll 1))434 :min-level (nth coll 2)435 :into (dec (nth coll 3))})437 (= 3 method) ;; trade evolution438 (conj (format-evo (drop 3 coll))439 {:method :trade440 :min-level (nth coll 1) ;; always 1 for trade.441 :into (dec (nth coll 2))}))))444 (defn hxc-ptrs-evolve445 "A hardcoded collection of 190 pointers to alternating evolution/learnset data,446 in internal order."447 ([]448 (hxc-ptrs-evolve com.aurellem.gb.gb-driver/original-rom))449 ([rom]450 (let [names (hxc-pokenames rom)451 pkmn-count (count names)452 ptrs453 (map (fn [[a b]] (low-high a b))454 (partition 2455 (take (* 2 pkmn-count)456 (drop 0x3b1e5 rom))))]457 (map (partial + 0x34000) ptrs)459 )))462 (defn hxc-learnsets463 "Hardcoded map associating pokemon names to lists of pairs [lvl464 move] of abilities they learn as they level up. The data465 exists at ROM@3400, sorted by internal order. Pointers to the data466 exist at ROM@3B1E5; see also, hxc-ptrs-evolve"467 ([] (hxc-learnsets com.aurellem.gb.gb-driver/original-rom))468 ([rom]469 (apply assoc470 {}471 (interleave472 (map format-name (hxc-pokenames rom))473 (map (comp474 (partial map475 (fn [[lvl mv]] [lvl (dec mv)]))476 (partial partition 2)477 ;; keep the learnset data478 (partial take-while (comp not zero?))479 ;; skip the evolution data480 rest481 (partial drop-while (comp not zero?)))482 (map #(drop % rom)483 (hxc-ptrs-evolve rom)))))))485 (defn hxc-learnsets-pretty486 "Live hxc-learnsets except it reports the name of each move --- as487 it appears in rom --- rather than the move index."488 ([] (hxc-learnsets-pretty com.aurellem.gb.gb-driver/original-rom))489 ([rom]490 (let [moves (vec(map format-name (hxc-move-names)))]491 (into {}492 (map (fn [[pkmn learnset]]493 [pkmn (map (fn [[lvl mv]] [lvl (moves mv)])494 learnset)])495 (hxc-learnsets rom))))))500 (defn hxc-evolution501 "Hardcoded evolution data in memory. The data exists at ROM@34000,502 sorted by internal order. Pointers to the data exist at ROM@3B1E5; see also, hxc-ptrs-evolve."503 ([] (hxc-evolution com.aurellem.gb.gb-driver/original-rom))504 ([rom]505 (apply assoc {}506 (interleave507 (map format-name (hxc-pokenames rom))508 (map509 (comp510 format-evo511 (partial take-while (comp not zero?))512 #(drop % rom))513 (hxc-ptrs-evolve rom)514 )))))516 (defn hxc-evolution-pretty517 "Like hxc-evolution, except it uses the names of items and pokemon518 --- grabbed from ROM --- rather than their numerical identifiers."519 ([] (hxc-evolution-pretty com.aurellem.gb.gb-driver/original-rom))520 ([rom]521 (let522 [poke-names (vec (map format-name (hxc-pokenames rom)))523 item-names (vec (map format-name (hxc-items rom)))524 use-names525 (fn [m]526 (loop [ks (keys m) new-map m]527 (let [k (first ks)]528 (cond (nil? ks) new-map529 (= k :into)530 (recur531 (next ks)532 (assoc new-map533 :into534 (poke-names535 (:into536 new-map))))537 (= k :item)538 (recur539 (next ks)540 (assoc new-map541 :item542 (item-names543 (:item new-map))))544 :else545 (recur546 (next ks)547 new-map)548 ))))]550 (into {}551 (map (fn [[pkmn evo-coll]]552 [pkmn (map use-names evo-coll)])553 (hxc-evolution rom))))))559 (defn hxc-pokemon-base560 ([] (hxc-pokemon-base com.aurellem.gb.gb-driver/original-rom))561 ([rom]562 (let [entry-size 28563 pkmn-count (count (hxc-pokedex-text rom))564 types (apply assoc {}565 (interleave566 (range)567 pkmn-types)) ;;!! softcoded568 moves (apply assoc {}569 (interleave570 (range)571 (map format-name572 (hxc-move-names rom))))573 ]574 (map576 (fn [[n577 rating-hp578 rating-atk579 rating-def580 rating-speed581 rating-special582 type-1583 type-2584 rarity585 rating-xp586 pic-dimensions587 ptr-pic-obverse-1588 ptr-pic-obverse-2589 ptr-pic-reverse-1590 ptr-pic-reverse-2591 move-1592 move-2593 move-3594 move-4595 growth-rate596 &597 TMs|HMs]]598 (let599 [base-moves600 (mapv moves601 ((comp602 ;; since the game uses zero as a delimiter,603 ;; it must also increment all move indices by 1.604 ;; heren we decrement to correct this.605 (partial map dec)606 (partial take-while (comp not zero?)))607 [move-1 move-2 move-3 move-4]))609 types610 (set (list (types type-1)611 (types type-2)))612 TMs|HMs613 (map614 (comp615 (partial map first)616 (partial remove (comp zero? second)))617 (split-at618 50619 (map vector620 (rest(range))621 (reduce concat622 (map623 #(take 8624 (concat (bit-list %)625 (repeat 0)))627 TMs|HMs)))))629 TMs (vec (first TMs|HMs))630 HMs (take 5 (map (partial + -50) (vec (second TMs|HMs))))633 ]636 {:dex# n637 :base-moves base-moves638 :types types639 :TMs TMs640 :HMs HMs641 :base-hp rating-hp642 :base-atk rating-atk643 :base-def rating-def644 :base-speed rating-speed645 :base-special rating-special646 }))648 (partition entry-size649 (take (* entry-size pkmn-count)650 (drop 0x383DE651 rom)))))))654 ;; ********************** MANIPULATION FNS659 (defn submap?660 "Compares the two maps. Returns true if map-big has the same associations as map-small, otherwise false."661 [map-small map-big]662 (cond (empty? map-small) true663 (and664 (contains? map-big (ffirst map-small))665 (= (get map-big (ffirst map-small))666 (second (first map-small))))667 (recur (next map-small) map-big)669 :else false))672 (defn search-map [proto-map maps]673 "Returns all the maps that make the same associations as proto-map."674 (some (partial submap? proto-map) maps))676 (defn filter-vals677 "Returns a map consisting of all the pairs [key val] for678 which (pred key) returns true."679 [pred map]680 (reduce (partial apply assoc) {}681 (filter (fn [[k v]] (pred v)) map)))684 (defn search-moves685 "Returns a subcollection of all hardcoded moves with the686 given attributes. Attributes consist of :name :power687 :accuracy :pp :fx-id688 (and also :fx-txt, but it contains the same information689 as :fx-id)"690 ([attribute-map]691 (search-moves692 com.aurellem.gb.gb-driver/original-rom attribute-map))693 ([rom attribute-map]694 (filter-vals (partial submap? attribute-map)695 (hxc-move-data rom))))701 ;; note for later: credits start at F1290705 (comment707 (def hxc-later708 "Running this code produces, e.g. hardcoded names NPCs give709 their pokemon. Will sort through it later."710 (print (character-codes->str(take 10000711 (drop 0x71597712 (rom (root)))))))714 (let [dex715 (partition-by #(= 0x50 %)716 (take 2540717 (drop 0x40687718 (rom (root)))))]719 (def dex dex)720 (def hxc-species721 (map character-codes->str722 (take-nth 4 dex))))723 )