Mercurial > vba-clojure
view clojure/com/aurellem/gb/hxc.clj @ 281:ca1afcef3542
Replaced manually listed move data in gb.moves with a call to hxc-move-data. Also, began work on hxc-shops, which lists shop inventory. todo: find out how the game converts internal order to pokedex order; todo: get hardcoded tm data.
author | Dylan Holmes <ocsenave@gmail.com> |
---|---|
date | Tue, 27 Mar 2012 22:41:24 -0500 |
parents | 69184558fcf3 |
children | 0c3fbb313e49 |
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 (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 6309 types pkmn-types ;;; !! hardcoded types310 ]311 (zipmap (map format-name names)312 (map313 (fn [[idx effect power type-id accuracy pp]]314 {:name (names (dec idx))315 :power power316 :accuracy accuracy317 :pp pp318 :type (types type-id)319 :fx-id effect320 :fx-txt (get move-effects effect)321 }322 )324 (partition move-size325 (take (* move-size move-count)326 (drop 0x38000 rom))))))))330 (defn hxc-move-data*331 "Like hxc-move-data, but reports numbers as hexadecimal symbols instead."332 ([]333 (hxc-move-data* com.aurellem.gb.gb-driver/original-rom))334 ([rom]335 (let [names (vec (hxc-move-names rom))336 move-count (count names)337 move-size 6338 format-name (fn [s]339 (keyword (.toLowerCase340 (apply str341 (map #(if (= % \space) "-" %) s)))))342 ]343 (zipmap (map format-name names)344 (map345 (fn [[idx effect power type accuracy pp]]346 {:name (names (dec idx))347 :power power348 :accuracy (hex accuracy)349 :pp pp350 :fx-id (hex effect)351 :fx-txt (get move-effects effect)352 }353 )355 (partition move-size356 (take (* move-size move-count)357 (drop 0x38000 rom))))))))361 (defn hxc-pokenames362 "The hardcoded names of the 190 species in memory. List begins at363 ROM@E8000. Although names in memory are padded with 0x50 to be 10 characters364 long, these names are stripped of padding."365 ([]366 (hxc-pokenames com.aurellem.gb.gb-driver/original-rom))367 ([rom]368 (let [count-species 190369 name-length 10]370 (map character-codes->str371 (partition name-length372 (map #(if (= 0x50 %) 0x00 %)373 (take (* count-species name-length)374 (drop 0xE8000375 rom))))))))380 (defn internal-id381 ([rom]382 (zipmap383 (map format-name (hxc-pokenames rom))384 (range)))385 ([]386 (internal-id com.aurellem.gb.gb-driver/original-rom)))390 ;; nidoran gender change upon levelup391 ;; (->392 ;; @current-state393 ;; rom394 ;; vec395 ;; (rewrite-memory396 ;; (nth (hxc-ptrs-evolve) ((internal-id) :nidoran♂))397 ;; [1 1 15])398 ;; (rewrite-memory399 ;; (nth (hxc-ptrs-evolve) ((internal-id) :nidoran♀))400 ;; [1 1 3])401 ;; (write-rom!)403 ;; )408 (defn hxc-advantage409 "The hardcoded type advantages in memory, returned as tuples of atk-type def-type multiplier. By default (i.e. if not listed here),410 the multiplier is 1."411 ([] (hxc-advantage com.aurellem.gb.gb-driver/original-rom))412 ([rom]413 (map414 (fn [[atk def mult]] [(get pkmn-types atk (hex atk))415 (get pkmn-types def (hex def))416 (/ mult 10)])417 (partition 3418 (take-while (partial not= 0xFF)419 (drop 0x3E62D rom))))))423 (defn format-evo424 [coll]425 (let [method (first coll)]426 (cond (empty? coll) []427 (= 0 method) [] ;; just in case428 (= 1 method) ;; level-up evolution429 (conj (format-evo (drop 3 coll))430 {:method :level-up431 :min-level (nth coll 1)432 :into (dec (nth coll 2))})434 (= 2 method) ;; item evolution435 (conj (format-evo (drop 4 coll))436 {:method :item437 :item (dec (nth coll 1))438 :min-level (nth coll 2)439 :into (dec (nth coll 3))})441 (= 3 method) ;; trade evolution442 (conj (format-evo (drop 3 coll))443 {:method :trade444 :min-level (nth coll 1) ;; always 1 for trade.445 :into (dec (nth coll 2))}))))448 (defn hxc-ptrs-evolve449 "A hardcoded collection of 190 pointers to alternating evolution/learnset data,450 in internal order."451 ([]452 (hxc-ptrs-evolve com.aurellem.gb.gb-driver/original-rom))453 ([rom]454 (let [names (hxc-pokenames rom)455 pkmn-count (count names)456 ptrs457 (map (fn [[a b]] (low-high a b))458 (partition 2459 (take (* 2 pkmn-count)460 (drop 0x3b1e5 rom))))]461 (map (partial + 0x34000) ptrs)463 )))466 (defn hxc-learnsets467 "Hardcoded map associating pokemon names to lists of pairs [lvl468 move] of abilities they learn as they level up. The data469 exists at ROM@3400, sorted by internal order. Pointers to the data470 exist at ROM@3B1E5; see also, hxc-ptrs-evolve"471 ([] (hxc-learnsets com.aurellem.gb.gb-driver/original-rom))472 ([rom]473 (apply assoc474 {}475 (interleave476 (map format-name (hxc-pokenames rom))477 (map (comp478 (partial map479 (fn [[lvl mv]] [lvl (dec mv)]))480 (partial partition 2)481 ;; keep the learnset data482 (partial take-while (comp not zero?))483 ;; skip the evolution data484 rest485 (partial drop-while (comp not zero?)))486 (map #(drop % rom)487 (hxc-ptrs-evolve rom)))))))489 (defn hxc-learnsets-pretty490 "Live hxc-learnsets except it reports the name of each move --- as491 it appears in rom --- rather than the move index."492 ([] (hxc-learnsets-pretty com.aurellem.gb.gb-driver/original-rom))493 ([rom]494 (let [moves (vec(map format-name (hxc-move-names)))]495 (into {}496 (map (fn [[pkmn learnset]]497 [pkmn (map (fn [[lvl mv]] [lvl (moves mv)])498 learnset)])499 (hxc-learnsets rom))))))504 (defn hxc-evolution505 "Hardcoded evolution data in memory. The data exists at ROM@34000,506 sorted by internal order. Pointers to the data exist at ROM@3B1E5; see also, hxc-ptrs-evolve."507 ([] (hxc-evolution com.aurellem.gb.gb-driver/original-rom))508 ([rom]509 (apply assoc {}510 (interleave511 (map format-name (hxc-pokenames rom))512 (map513 (comp514 format-evo515 (partial take-while (comp not zero?))516 #(drop % rom))517 (hxc-ptrs-evolve rom)518 )))))520 (defn hxc-evolution-pretty521 "Like hxc-evolution, except it uses the names of items and pokemon522 --- grabbed from ROM --- rather than their numerical identifiers."523 ([] (hxc-evolution-pretty com.aurellem.gb.gb-driver/original-rom))524 ([rom]525 (let526 [poke-names (vec (map format-name (hxc-pokenames rom)))527 item-names (vec (map format-name (hxc-items rom)))528 use-names529 (fn [m]530 (loop [ks (keys m) new-map m]531 (let [k (first ks)]532 (cond (nil? ks) new-map533 (= k :into)534 (recur535 (next ks)536 (assoc new-map537 :into538 (poke-names539 (:into540 new-map))))541 (= k :item)542 (recur543 (next ks)544 (assoc new-map545 :item546 (item-names547 (:item new-map))))548 :else549 (recur550 (next ks)551 new-map)552 ))))]554 (into {}555 (map (fn [[pkmn evo-coll]]556 [pkmn (map use-names evo-coll)])557 (hxc-evolution rom))))))563 (defn hxc-pokemon-base564 ([] (hxc-pokemon-base com.aurellem.gb.gb-driver/original-rom))565 ([rom]566 (let [entry-size 28567 pkmn-count (count (hxc-pokedex-text rom))568 types (apply assoc {}569 (interleave570 (range)571 pkmn-types)) ;;!! softcoded572 moves (apply assoc {}573 (interleave574 (range)575 (map format-name576 (hxc-move-names rom))))577 ]578 (map580 (fn [[n581 rating-hp582 rating-atk583 rating-def584 rating-speed585 rating-special586 type-1587 type-2588 rarity589 rating-xp590 pic-dimensions591 ptr-pic-obverse-1592 ptr-pic-obverse-2593 ptr-pic-reverse-1594 ptr-pic-reverse-2595 move-1596 move-2597 move-3598 move-4599 growth-rate600 &601 TMs|HMs]]602 (let603 [base-moves604 (mapv moves605 ((comp606 ;; since the game uses zero as a delimiter,607 ;; it must also increment all move indices by 1.608 ;; heren we decrement to correct this.609 (partial map dec)610 (partial take-while (comp not zero?)))611 [move-1 move-2 move-3 move-4]))613 types614 (set (list (types type-1)615 (types type-2)))616 TMs|HMs617 (map618 (comp619 (partial map first)620 (partial remove (comp zero? second)))621 (split-at622 50623 (map vector624 (rest(range))625 (reduce concat626 (map627 #(take 8628 (concat (bit-list %)629 (repeat 0)))631 TMs|HMs)))))633 TMs (vec (first TMs|HMs))634 HMs (take 5 (map (partial + -50) (vec (second TMs|HMs))))637 ]640 {:dex# n641 :base-moves base-moves642 :types types643 :TMs TMs644 :HMs HMs645 :base-hp rating-hp646 :base-atk rating-atk647 :base-def rating-def648 :base-speed rating-speed649 :base-special rating-special650 }))652 (partition entry-size653 (take (* entry-size pkmn-count)654 (drop 0x383DE655 rom)))))))657 (defn hxc-shops658 ([] (hxc-shops com.aurellem.gb.gb-driver/original-rom))659 ([rom]660 (let [items (zipmap (range) (map format-name (hxc-items rom)))662 ;; temporarily softcode the TM items663 items (into664 items665 (map (juxt identity666 (comp keyword667 (partial str "tm-")668 (partial + 1 -200)669 ))670 (take 200 (drop 200 (range)))))672 ]674 ((fn parse-shop [coll [num-items & items-etc]]675 (let [inventory (take (dec num-items) (rest items-etc))676 [separator & items-etc] (drop num-items (rest items-etc))]677 (if (= separator 0x50)678 (map (partial mapv (comp items dec)) (conj coll inventory))679 (recur (conj coll inventory) items-etc)680 )681 ))683 '()684 (take 1000 (drop 0x233C rom)))687 )))691 ;; ********************** MANIPULATION FNS696 (defn submap?697 "Compares the two maps. Returns true if map-big has the same associations as map-small, otherwise false."698 [map-small map-big]699 (cond (empty? map-small) true700 (and701 (contains? map-big (ffirst map-small))702 (= (get map-big (ffirst map-small))703 (second (first map-small))))704 (recur (next map-small) map-big)706 :else false))709 (defn search-map [proto-map maps]710 "Returns all the maps that make the same associations as proto-map."711 (some (partial submap? proto-map) maps))713 (defn filter-vals714 "Returns a map consisting of all the pairs [key val] for715 which (pred key) returns true."716 [pred map]717 (reduce (partial apply assoc) {}718 (filter (fn [[k v]] (pred v)) map)))721 (defn search-moves722 "Returns a subcollection of all hardcoded moves with the723 given attributes. Attributes consist of :name :power724 :accuracy :pp :fx-id725 (and also :fx-txt, but it contains the same information726 as :fx-id)"727 ([attribute-map]728 (search-moves729 com.aurellem.gb.gb-driver/original-rom attribute-map))730 ([rom attribute-map]731 (filter-vals (partial submap? attribute-map)732 (hxc-move-data rom))))738 ;; note for later: credits start at F1290743 ;; (def dex-order744 ;; [:bulbasaur745 ;; :ivysaur746 ;; :venusaur747 ;; :charmander748 ;; :charmeleon749 ;; :charizard])752 ;; (defn same-type-attack-bonus?753 ;; ([pkmn move]754 ;; (same-type-attack-bonus?755 ;; com.aurellem.gb.gb-driver/original-rom pkmn move))756 ;; ([rom pkmn move]757 ;; (hxc-pokemon-base rom)))764 (comment766 (def hxc-later767 "Running this code produces, e.g. hardcoded names NPCs give768 their pokemon. Will sort through it later."769 (print (character-codes->str(take 10000770 (drop 0x71597771 (rom (root)))))))773 (let [dex774 (partition-by #(= 0x50 %)775 (take 2540776 (drop 0x40687777 (rom (root)))))]778 (def dex dex)779 (def hxc-species780 (map character-codes->str781 (take-nth 4 dex))))782 )