Mercurial > vba-clojure
view clojure/com/aurellem/gb/hxc.clj @ 294:9f466a332448
merge
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Fri, 30 Mar 2012 18:15:28 -0500 |
parents | c31cb3043087 |
children | 2873f50b7291 |
line wrap: on
line source
1 (ns com.aurellem.gb.hxc2 (:use (com.aurellem.gb assembly characters gb-driver util3 constants species))4 ;; (:use (com.aurellem.world practice))5 (:import [com.aurellem.gb.gb_driver SaveState]))10 ; ************* HANDWRITTEN CONSTANTS14 (def pkmn-types15 [:normal ;;016 :fighting ;;117 :flying ;;218 :poison ;;319 :ground ;;420 :rock ;;521 :bird ;;622 :bug ;;723 :ghost ;;824 :A25 :B26 :C27 :D28 :E29 :F30 :G31 :H32 :I33 :J34 :K35 :fire ;;20 (0x14)36 :water ;;21 (0x15)37 :grass ;;22 (0x16)38 :electric ;;23 (0x17)39 :psychic ;;24 (0x18)40 :ice ;;25 (0x19)41 :dragon ;;26 (0x1A)42 ])45 ;; question: when status effects claim to take46 ;; their accuracy from the move accuracy, does47 ;; this mean that the move always "hits" but the48 ;; status effect may not?50 (def move-effects51 ["normal damage"52 "no damage, just opponent sleep" ;; how many turns? is atk power ignored?53 "0x4C chance of poison"54 "leech half of inflicted damage"55 "0x19 chance of burn"56 "0x19 chance of freeze"57 "0x19 chance of paralyze"58 "user faints; opponent defense halved during attack."59 "leech half of inflicted damage ONLY if sleeping opponent."60 "imitate last attack"61 "user atk +1"62 "user def +1"63 "user spd +1"64 "user spc +1"65 "user acr +1" ;; unused?!66 "user evd +1"67 "get post-battle $ = 2*level*uses"68 "0xFE acr, no matter what."69 "opponent atk -1" ;; acr taken from move acr?70 "opponent def -1" ;;71 "opponent spd -1" ;;72 "opponent spc -1" ;;73 "opponent acr -1";;74 "opponent evd -1"75 "converts user's type to opponent's."76 "(haze)"77 "(bide)"78 "(thrash)"79 "(teleport)"80 "(fury swipes)"81 "attacks 2-5 turns" ;; unused? like rollout?82 "0x19 chance of flinch"83 "opponent sleep for 1-7 turns"84 "0x66 chance of poison"85 "0x4D chance of burn"86 "0x4D chance of freeze"87 "0x4D chance of paralyze"88 "0x4D chance of flinch"89 "one-hit KO"90 "charge one turn, atk next."91 "fixed damage, leaves 1HP." ;; how is dmg determined?92 "fixed damage." ;; cf seismic toss, dragon rage, psywave.93 "atk 2-5 turns; opponent can't attack" ;; unnormalized? (0 0x60 0x60 0x20 0x20)94 "charge one turn, atk next. (can't be hit when charging)"95 "atk hits twice."96 "user takes 1 damage if misses."97 "evade status-lowering effects" ;;caused by you or also your opponent?98 "(broken) if user is slower than opponent, makes critical hit impossible, otherwise has no effect"99 "atk causes recoil dmg = 1/4 dmg dealt"100 "confuses opponent" ;; acr taken from move acr101 "user atk +2"102 "user def +2"103 "user spd +2"104 "user spc +2"105 "user acr +2" ;; unused!106 "user evd +2" ;; unused!107 "restores up to half of user's max hp." ;; broken: fails if the difference108 ;; b/w max and current hp is one less than a multiple of 256.109 "(transform)"110 "opponent atk -2"111 "opponent def -2"112 "opponent spd -2"113 "opponent spc -2"114 "opponent acr -2"115 "opponent evd -2"116 "doubles user spc when attacked"117 "doubles user def when attacked"118 "just poisons opponent" ;;acr taken from move acr119 "just paralyzes opponent" ;;120 "0x19 chance opponent atk -1"121 "0x19 chance opponent def -1"122 "0x19 chance opponent spd -1"123 "0x4C chance opponent spc -1" ;; context suggest chance is 0x19124 "0x19 chance opponent acr -1"125 "0x19 chance opponent evd -1"126 "???" ;; unused? no effect?127 "???" ;; unused? no effect?128 "0x19 chance opponent confused"129 "atk hits twice. 0x33 chance opponent poisioned."130 "broken. crash the game after attack."131 "(substitute)"132 "unless opponent faints, user must recharge after atk. some133 exceptions apply."134 "(rage)"135 "(mimic)"136 "(metronome)"137 "(leech seed)"138 "does nothing (splash)"139 "(disable)"140 ])143 ;; ************** HARDCODED DATA145 (defn hxc-thunk146 "Creates a thunk (nullary fn) that grabs data in a certain region of rom and147 splits it into a collection by 0x50. If rom is not supplied, uses the148 original rom data."149 [start length]150 (fn self151 ([rom]152 (take-nth 2153 (partition-by #(= % 0x50)154 (take length155 (drop start rom)))))156 ([]157 (self com.aurellem.gb.gb-driver/original-rom))))159 (def hxc-thunk-words160 "Same as hxc-thunk, except it interprets the rom data as characters,161 returning a collection of strings."162 (comp163 (partial comp (partial map character-codes->str))164 hxc-thunk))167 ;; --------------------------------------------------171 (defn hxc-pokenames-raw172 "The hardcoded names of the 190 species in memory. List begins at173 ROM@E8000. Although names in memory are padded with 0x50 to be 10 characters174 long, these names are stripped of padding. See also, hxc-pokedex-names"175 ([]176 (hxc-pokenames-raw com.aurellem.gb.gb-driver/original-rom))177 ([rom]178 (let [count-species 190179 name-length 10]180 (map character-codes->str181 (partition name-length182 (map #(if (= 0x50 %) 0x00 %)183 (take (* count-species name-length)184 (drop 0xE8000185 rom))))))))186 (def hxc-pokenames187 (comp188 (partial map format-name)189 hxc-pokenames-raw))194 (defn hxc-pokedex-names195 "The names of the pokemon in hardcoded pokedex order. List begins at196 ROM@410B1. See also, hxc-pokenames."197 ([] (hxc-pokedex-names198 com.aurellem.gb.gb-driver/original-rom))199 ([rom]200 (let [names (hxc-pokenames rom)]201 (#(mapv %202 ((comp range count keys) %))203 (zipmap204 (take (count names)205 (drop 0x410b1 rom))207 names)))))212 (def hxc-items-raw213 "The hardcoded names of the items in memory. List begins at214 ROM@045B7"215 (hxc-thunk-words 0x45B7 870))217 (def hxc-types218 "The hardcoded type names in memory. List begins at ROM@27D99,219 shortly before hxc-titles."220 (hxc-thunk-words 0x27D99 102))222 (def hxc-titles223 "The hardcoded names of the trainer titles in memory. List begins at224 ROM@27E77"225 (hxc-thunk-words 0x27E77 196))228 (def hxc-pokedex-text-raw229 "The hardcoded pokedex entries in memory. List begins at230 ROM@B8000, shortly before move names."231 (hxc-thunk-words 0xB8000 14754))235 (def hxc-items236 "The hardcoded names of the items in memory, presented as237 keywords. List begins at ROM@045B7. See also, hxc-items-raw."238 (comp (partial map format-name) hxc-items-raw))240 (defn hxc-pokedex-text241 "The hardcoded pokedex entries in memory, presented as an242 associative hash map. List begins at ROM@B8000."243 ([] (hxc-pokedex-text com.aurellem.gb.gb-driver/original-rom))244 ([rom]245 (zipmap246 (hxc-pokedex-names rom)247 (cons nil ;; for missingno.248 (hxc-pokedex-text-raw rom)))))250 ;; In red/blue, pokedex stats are in internal order.251 ;; In yellow, pokedex stats are in pokedex order.253 (defn hxc-pokedex-stats254 "The hardcoded pokedex stats (species height weight) in memory. List255 begins at ROM@40687"256 ([] (hxc-pokedex-stats com.aurellem.gb.gb-driver/original-rom))257 ([rom]258 (let [pokedex-names (zipmap (range) (hxc-pokedex-names rom))259 pkmn-count (count pokedex-names)260 ]261 ((fn capture-stats262 [n stats data]263 (if (zero? n) stats264 (let [[species265 [_266 height-ft267 height-in268 weight-1269 weight-2270 _271 dex-ptr-1272 dex-ptr-2273 dex-bank274 _275 & data]]276 (split-with (partial not= 0x50) data)]277 (recur (dec n)278 (assoc stats279 (pokedex-names (- pkmn-count (dec n)))280 {:species281 (format-name (character-codes->str species))282 :height-ft283 height-ft284 :height-in285 height-in286 :weight287 (/ (low-high weight-1 weight-2) 10.)289 ;; :text290 ;; (character-codes->str291 ;; (take-while292 ;; (partial not= 0x50)293 ;; (drop294 ;; (+ 0xB8000295 ;; -0x4000296 ;; (low-high dex-ptr-1 dex-ptr-2))297 ;; rom)))298 })300 data)303 )))305 pkmn-count306 {}307 (drop 0x40687 rom))) ))315 (def hxc-places316 "The hardcoded place names in memory. List begins at317 ROM@71500. [Cinnabar] Mansion seems to be dynamically calculated."318 (hxc-thunk-words 0x71500 560))321 (defn hxc-dialog322 "The hardcoded dialogue in memory, including in-game alerts. Dialog323 seems to be separated by 0x57 instead of 0x50 (END). Begins at ROM@98000."324 ([rom]325 (map character-codes->str326 (take-nth 2327 (partition-by #(= % 0x57)328 (take 0x0F728329 (drop 0x98000 rom))))))330 ([]331 (hxc-dialog com.aurellem.gb.gb-driver/original-rom)))334 (def hxc-move-names335 "The hardcoded move names in memory. List begins at ROM@BC000"336 (hxc-thunk-words 0xBC000 1551))339 (defn hxc-move-data340 "The hardcoded (basic (move effects)) in memory. List begins at341 0x38000. Returns a map of {:name :power :accuracy :pp :fx-id342 :fx-txt}. The move descriptions are handwritten, not hardcoded."343 ([]344 (hxc-move-data com.aurellem.gb.gb-driver/original-rom))345 ([rom]346 (let [names (vec (hxc-move-names rom))347 move-count (count names)348 move-size 6349 types pkmn-types ;;; !! hardcoded types350 ]351 (zipmap (map format-name names)352 (map353 (fn [[idx effect power type-id accuracy pp]]354 {:name (names (dec idx))355 :power power356 :accuracy accuracy357 :pp pp358 :type (types type-id)359 :fx-id effect360 :fx-txt (get move-effects effect)361 }362 )364 (partition move-size365 (take (* move-size move-count)366 (drop 0x38000 rom))))))))370 (defn hxc-move-data*371 "Like hxc-move-data, but reports numbers as hexadecimal symbols instead."372 ([]373 (hxc-move-data* com.aurellem.gb.gb-driver/original-rom))374 ([rom]375 (let [names (vec (hxc-move-names rom))376 move-count (count names)377 move-size 6378 format-name (fn [s]379 (keyword (.toLowerCase380 (apply str381 (map #(if (= % \space) "-" %) s)))))382 ]383 (zipmap (map format-name names)384 (map385 (fn [[idx effect power type accuracy pp]]386 {:name (names (dec idx))387 :power power388 :accuracy (hex accuracy)389 :pp pp390 :fx-id (hex effect)391 :fx-txt (get move-effects effect)392 }393 )395 (partition move-size396 (take (* move-size move-count)397 (drop 0x38000 rom))))))))400 (defn hxc-machines401 "The hardcoded moves taught by TMs and HMs. List begins at ROM@0x1232D."402 ([] (hxc-machines403 com.aurellem.gb.gb-driver/original-rom))404 ([rom]405 (let [moves (hxc-move-names rom)]406 (zipmap407 (range)408 (take-while409 (comp not nil?)410 (map (comp411 format-name412 (zipmap413 (range)414 moves)415 dec)416 (take 100417 (drop 0x1232D rom))))))))421 (defn internal-id422 ([rom]423 (zipmap424 (hxc-pokenames rom)425 (range)))426 ([]427 (internal-id com.aurellem.gb.gb-driver/original-rom)))433 ;; nidoran gender change upon levelup434 ;; (->435 ;; @current-state436 ;; rom437 ;; vec438 ;; (rewrite-memory439 ;; (nth (hxc-ptrs-evolve) ((internal-id) :nidoran♂))440 ;; [1 1 15])441 ;; (rewrite-memory442 ;; (nth (hxc-ptrs-evolve) ((internal-id) :nidoran♀))443 ;; [1 1 3])444 ;; (write-rom!)446 ;; )451 (defn hxc-advantage452 ;; in-game multipliers are stored as 10x their effective value453 ;; to allow for fractional multipliers like 1/2455 "The hardcoded type advantages in memory, returned as tuples of456 atk-type def-type multiplier. By default (i.e. if not listed here),457 the multiplier is 1. List begins at 0x3E62D."458 ([] (hxc-advantage com.aurellem.gb.gb-driver/original-rom))459 ([rom]460 (map461 (fn [[atk def mult]] [(get pkmn-types atk (hex atk))462 (get pkmn-types def (hex def))463 (/ mult 10)])464 (partition 3465 (take-while (partial not= 0xFF)466 (drop 0x3E62D rom))))))470 (defn format-evo471 [coll]472 (let [method (first coll)]473 (cond (empty? coll) []474 (= 0 method) [] ;; just in case475 (= 1 method) ;; level-up evolution476 (conj (format-evo (drop 3 coll))477 {:method :level-up478 :min-level (nth coll 1)479 :into (dec (nth coll 2))})481 (= 2 method) ;; item evolution482 (conj (format-evo (drop 4 coll))483 {:method :item484 :item (dec (nth coll 1))485 :min-level (nth coll 2)486 :into (dec (nth coll 3))})488 (= 3 method) ;; trade evolution489 (conj (format-evo (drop 3 coll))490 {:method :trade491 :min-level (nth coll 1) ;; always 1 for trade.492 :into (dec (nth coll 2))}))))495 (defn hxc-ptrs-evolve496 "A hardcoded collection of 190 pointers to alternating evolution/learnset data,497 in internal order."498 ([]499 (hxc-ptrs-evolve com.aurellem.gb.gb-driver/original-rom))500 ([rom]501 (let [502 pkmn-count (count (hxc-pokenames-raw)) ;; 190503 ptrs504 (map (fn [[a b]] (low-high a b))505 (partition 2506 (take (* 2 pkmn-count)507 (drop 0x3b1e5 rom))))]508 (map (partial + 0x34000) ptrs)510 )))513 (defn hxc-learnsets514 "Hardcoded map associating pokemon names to lists of pairs [lvl515 move] of abilities they learn as they level up. The data516 exists at ROM@3400, sorted by internal order. Pointers to the data517 exist at ROM@3B1E5; see also, hxc-ptrs-evolve"518 ([] (hxc-learnsets com.aurellem.gb.gb-driver/original-rom))519 ([rom]520 (apply assoc521 {}522 (interleave523 (hxc-pokenames rom)524 (map (comp525 (partial map526 (fn [[lvl mv]] [lvl (dec mv)]))527 (partial partition 2)528 ;; keep the learnset data529 (partial take-while (comp not zero?))530 ;; skip the evolution data531 rest532 (partial drop-while (comp not zero?)))533 (map #(drop % rom)534 (hxc-ptrs-evolve rom)))))))536 (defn hxc-learnsets-pretty537 "Live hxc-learnsets except it reports the name of each move --- as538 it appears in rom --- rather than the move index."539 ([] (hxc-learnsets-pretty com.aurellem.gb.gb-driver/original-rom))540 ([rom]541 (let [moves (vec(map format-name (hxc-move-names)))]542 (into {}543 (map (fn [[pkmn learnset]]544 [pkmn (map (fn [[lvl mv]] [lvl (moves mv)])545 learnset)])546 (hxc-learnsets rom))))))551 (defn hxc-evolution552 "Hardcoded evolution data in memory. The data exists at ROM@34000,553 sorted by internal order. Pointers to the data exist at ROM@3B1E5; see also, hxc-ptrs-evolve."554 ([] (hxc-evolution com.aurellem.gb.gb-driver/original-rom))555 ([rom]556 (apply assoc {}557 (interleave558 (hxc-pokenames rom)559 (map560 (comp561 format-evo562 (partial take-while (comp not zero?))563 #(drop % rom))564 (hxc-ptrs-evolve rom)565 )))))567 (defn hxc-evolution-pretty568 "Like hxc-evolution, except it uses the names of items and pokemon569 --- grabbed from ROM --- rather than their numerical identifiers."570 ([] (hxc-evolution-pretty com.aurellem.gb.gb-driver/original-rom))571 ([rom]572 (let573 [poke-names (vec (hxc-pokenames rom))574 item-names (vec (hxc-items rom))575 use-names576 (fn [m]577 (loop [ks (keys m) new-map m]578 (let [k (first ks)]579 (cond (nil? ks) new-map580 (= k :into)581 (recur582 (next ks)583 (assoc new-map584 :into585 (poke-names586 (:into587 new-map))))588 (= k :item)589 (recur590 (next ks)591 (assoc new-map592 :item593 (item-names594 (:item new-map))))595 :else596 (recur597 (next ks)598 new-map)599 ))))]601 (into {}602 (map (fn [[pkmn evo-coll]]603 [pkmn (map use-names evo-coll)])604 (hxc-evolution rom))))))607 (defn hxc-pokemon-base608 ([] (hxc-pokemon-base com.aurellem.gb.gb-driver/original-rom))609 ([rom]610 (let [entry-size 28611 pkmn-count (count (hxc-pokedex-text rom))612 pokemon (rest (hxc-pokedex-names))613 types (apply assoc {}614 (interleave615 (range)616 pkmn-types)) ;;!! softcoded617 moves (apply assoc {}618 (interleave619 (range)620 (map format-name621 (hxc-move-names rom))))622 machines (hxc-machines)623 ]624 (zipmap625 pokemon626 (map627 (fn [[n628 rating-hp629 rating-atk630 rating-def631 rating-speed632 rating-special633 type-1634 type-2635 rarity636 rating-xp637 pic-dimensions ;; tile_width|tile_height (8px/tile)638 ptr-pic-obverse-1639 ptr-pic-obverse-2640 ptr-pic-reverse-1641 ptr-pic-reverse-2642 move-1643 move-2644 move-3645 move-4646 growth-rate647 &648 TMs|HMs]]649 (let650 [base-moves651 (mapv moves652 ((comp653 ;; since the game uses zero as a delimiter,654 ;; it must also increment all move indices by 1.655 ;; heren we decrement to correct this.656 (partial map dec)657 (partial take-while (comp not zero?)))658 [move-1 move-2 move-3 move-4]))660 types661 (set (list (types type-1)662 (types type-2)))663 TMs|HMs664 (map665 (comp666 (partial map first)667 (partial remove (comp zero? second)))668 (split-at669 50670 (map vector671 (rest(range))672 (reduce concat673 (map674 #(take 8675 (concat (bit-list %)676 (repeat 0)))678 TMs|HMs)))))680 TMs (vec (first TMs|HMs))681 HMs (take 5 (map (partial + -50) (vec (second TMs|HMs))))684 ]687 {:dex# n688 :base-moves base-moves689 :types types690 :TMs TMs691 :HMs HMs692 :base-hp rating-hp693 :base-atk rating-atk694 :base-def rating-def695 :base-speed rating-speed696 :base-special rating-special697 }))699 (partition entry-size700 (take (* entry-size pkmn-count)701 (drop 0x383DE702 rom))))))))706 (defn hxc-item-prices707 "The hardcoded list of item prices in memory. List begins at ROM@4495"708 ([] (hxc-item-prices com.aurellem.gb.gb-driver/original-rom))709 ([rom]710 (let [items (hxc-items rom)711 price-size 3]712 (zipmap items713 (map (comp714 ;; zero-cost items are "priceless"715 #(if (zero? %) :priceless %)716 decode-bcd butlast)717 (partition price-size718 (take (* price-size (count items))719 (drop 0x4495 rom))))))))721 (defn hxc-shops722 ([] (hxc-shops com.aurellem.gb.gb-driver/original-rom))723 ([rom]724 (let [items (zipmap (range) (hxc-items rom))726 ;; temporarily softcode the TM items727 items (into728 items729 (map (juxt identity730 (comp keyword731 (partial str "tm-")732 (partial + 1 -200)733 ))734 (take 200 (drop 200 (range)))))736 ]738 ((fn parse-shop [coll [num-items & items-etc]]739 (let [inventory (take-while740 (partial not= 0xFF)741 items-etc)742 [separator & items-etc] (drop num-items (rest items-etc))]743 (if (= separator 0x50)744 (map (partial mapv (comp items dec)) (conj coll inventory))745 (recur (conj coll inventory) items-etc)746 )747 ))749 '()750 (drop 0x233C rom))753 )))759 (defn hxc-ptrs-wild760 "A list of the hardcoded wild encounter data in memory. Pointers761 begin at ROM@0CB95; data begins at ROM@0x04D89"762 ([] (hxc-ptrs-wild com.aurellem.gb.gb-driver/original-rom))763 ([rom]764 (let [ptrs765 (map (fn [[a b]] (+ a (* 0x100 b)))766 (take-while (partial not= (list 0xFF 0xFF))767 (partition 2 (drop 0xCB95 rom))))]768 ptrs)))772 (defn hxc-wilds773 "A list of the hardcoded wild encounter data in memory. Pointers774 begin at ROM@0CB95; data begins at ROM@0x04D89"775 ([] (hxc-wilds com.aurellem.gb.gb-driver/original-rom))776 ([rom]777 (let [pokenames (zipmap (range) (hxc-pokenames rom))]778 (map779 (partial map (fn [[a b]] {:species (pokenames (dec b)) :level780 a}))781 (partition 10783 (take-while (comp (partial not= 1)784 first)785 (partition 2786 (drop 0xCD8C rom))788 ))))))803 ;; ********************** MANIPULATION FNS806 (defn same-type807 ([pkmn move]808 (same-type809 com.aurellem.gb.gb-driver/original-rom pkmn move))810 ([rom pkmn move]811 (((comp :types (hxc-pokemon-base rom)) pkmn)812 ((comp :type (hxc-move-data rom)) move))))817 (defn submap?818 "Compares the two maps. Returns true if map-big has the same associations as map-small, otherwise false."819 [map-small map-big]820 (cond (empty? map-small) true821 (and822 (contains? map-big (ffirst map-small))823 (= (get map-big (ffirst map-small))824 (second (first map-small))))825 (recur (next map-small) map-big)827 :else false))830 (defn search-map [proto-map maps]831 "Returns all the maps that make the same associations as proto-map."832 (some (partial submap? proto-map) maps))834 (defn filter-vals835 "Returns a map consisting of all the pairs [key val] for836 which (pred key) returns true."837 [pred map]838 (reduce (partial apply assoc) {}839 (filter (fn [[k v]] (pred v)) map)))842 (defn search-moves843 "Returns a subcollection of all hardcoded moves with the844 given attributes. Attributes consist of :name :power845 :accuracy :pp :fx-id846 (and also :fx-txt, but it contains the same information847 as :fx-id)"848 ([attribute-map]849 (search-moves850 com.aurellem.gb.gb-driver/original-rom attribute-map))851 ([rom attribute-map]852 (filter-vals (partial submap? attribute-map)853 (hxc-move-data rom))))859 ;; note: 0x2f31 contains the names "TM" "HM"?861 ;; note for later: credits start at F1290865 (comment867 (def hxc-later868 "Running this code produces, e.g. hardcoded names NPCs give869 their pokemon. Will sort through it later."870 (print (character-codes->str(take 10000871 (drop 0x71597872 (rom (root)))))))874 (let [dex875 (partition-by #(= 0x50 %)876 (take 2540877 (drop 0x40687878 (rom (root)))))]879 (def dex dex)880 (def hxc-species881 (map character-codes->str882 (take-nth 4 dex))))883 )