Mercurial > vba-clojure
view clojure/com/aurellem/gb/hxc.clj @ 293:4a0dbaed7078
preliminary idea for a better pre-bootstrapping program.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Fri, 30 Mar 2012 18:14:14 -0500 |
parents | eec3e69500d9 |
children | c31cb3043087 |
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 "The hardcoded type advantages in memory, returned as tuples of atk-type def-type multiplier. By default (i.e. if not listed here),453 the multiplier is 1."454 ([] (hxc-advantage com.aurellem.gb.gb-driver/original-rom))455 ([rom]456 (map457 (fn [[atk def mult]] [(get pkmn-types atk (hex atk))458 (get pkmn-types def (hex def))459 (/ mult 10)])460 (partition 3461 (take-while (partial not= 0xFF)462 (drop 0x3E62D rom))))))466 (defn format-evo467 [coll]468 (let [method (first coll)]469 (cond (empty? coll) []470 (= 0 method) [] ;; just in case471 (= 1 method) ;; level-up evolution472 (conj (format-evo (drop 3 coll))473 {:method :level-up474 :min-level (nth coll 1)475 :into (dec (nth coll 2))})477 (= 2 method) ;; item evolution478 (conj (format-evo (drop 4 coll))479 {:method :item480 :item (dec (nth coll 1))481 :min-level (nth coll 2)482 :into (dec (nth coll 3))})484 (= 3 method) ;; trade evolution485 (conj (format-evo (drop 3 coll))486 {:method :trade487 :min-level (nth coll 1) ;; always 1 for trade.488 :into (dec (nth coll 2))}))))491 (defn hxc-ptrs-evolve492 "A hardcoded collection of 190 pointers to alternating evolution/learnset data,493 in internal order."494 ([]495 (hxc-ptrs-evolve com.aurellem.gb.gb-driver/original-rom))496 ([rom]497 (let [498 pkmn-count (count (hxc-pokenames-raw)) ;; 190499 ptrs500 (map (fn [[a b]] (low-high a b))501 (partition 2502 (take (* 2 pkmn-count)503 (drop 0x3b1e5 rom))))]504 (map (partial + 0x34000) ptrs)506 )))509 (defn hxc-learnsets510 "Hardcoded map associating pokemon names to lists of pairs [lvl511 move] of abilities they learn as they level up. The data512 exists at ROM@3400, sorted by internal order. Pointers to the data513 exist at ROM@3B1E5; see also, hxc-ptrs-evolve"514 ([] (hxc-learnsets com.aurellem.gb.gb-driver/original-rom))515 ([rom]516 (apply assoc517 {}518 (interleave519 (hxc-pokenames rom)520 (map (comp521 (partial map522 (fn [[lvl mv]] [lvl (dec mv)]))523 (partial partition 2)524 ;; keep the learnset data525 (partial take-while (comp not zero?))526 ;; skip the evolution data527 rest528 (partial drop-while (comp not zero?)))529 (map #(drop % rom)530 (hxc-ptrs-evolve rom)))))))532 (defn hxc-learnsets-pretty533 "Live hxc-learnsets except it reports the name of each move --- as534 it appears in rom --- rather than the move index."535 ([] (hxc-learnsets-pretty com.aurellem.gb.gb-driver/original-rom))536 ([rom]537 (let [moves (vec(map format-name (hxc-move-names)))]538 (into {}539 (map (fn [[pkmn learnset]]540 [pkmn (map (fn [[lvl mv]] [lvl (moves mv)])541 learnset)])542 (hxc-learnsets rom))))))547 (defn hxc-evolution548 "Hardcoded evolution data in memory. The data exists at ROM@34000,549 sorted by internal order. Pointers to the data exist at ROM@3B1E5; see also, hxc-ptrs-evolve."550 ([] (hxc-evolution com.aurellem.gb.gb-driver/original-rom))551 ([rom]552 (apply assoc {}553 (interleave554 (hxc-pokenames rom)555 (map556 (comp557 format-evo558 (partial take-while (comp not zero?))559 #(drop % rom))560 (hxc-ptrs-evolve rom)561 )))))563 (defn hxc-evolution-pretty564 "Like hxc-evolution, except it uses the names of items and pokemon565 --- grabbed from ROM --- rather than their numerical identifiers."566 ([] (hxc-evolution-pretty com.aurellem.gb.gb-driver/original-rom))567 ([rom]568 (let569 [poke-names (vec (hxc-pokenames rom))570 item-names (vec (hxc-items rom))571 use-names572 (fn [m]573 (loop [ks (keys m) new-map m]574 (let [k (first ks)]575 (cond (nil? ks) new-map576 (= k :into)577 (recur578 (next ks)579 (assoc new-map580 :into581 (poke-names582 (:into583 new-map))))584 (= k :item)585 (recur586 (next ks)587 (assoc new-map588 :item589 (item-names590 (:item new-map))))591 :else592 (recur593 (next ks)594 new-map)595 ))))]597 (into {}598 (map (fn [[pkmn evo-coll]]599 [pkmn (map use-names evo-coll)])600 (hxc-evolution rom))))))603 (defn hxc-pokemon-base604 ([] (hxc-pokemon-base com.aurellem.gb.gb-driver/original-rom))605 ([rom]606 (let [entry-size 28607 pkmn-count (count (hxc-pokedex-text rom))608 pokemon (rest (hxc-pokedex-names))609 types (apply assoc {}610 (interleave611 (range)612 pkmn-types)) ;;!! softcoded613 moves (apply assoc {}614 (interleave615 (range)616 (map format-name617 (hxc-move-names rom))))618 machines (hxc-machines)619 ]620 (zipmap621 pokemon622 (map623 (fn [[n624 rating-hp625 rating-atk626 rating-def627 rating-speed628 rating-special629 type-1630 type-2631 rarity632 rating-xp633 pic-dimensions ;; tile_width|tile_height (8px/tile)634 ptr-pic-obverse-1635 ptr-pic-obverse-2636 ptr-pic-reverse-1637 ptr-pic-reverse-2638 move-1639 move-2640 move-3641 move-4642 growth-rate643 &644 TMs|HMs]]645 (let646 [base-moves647 (mapv moves648 ((comp649 ;; since the game uses zero as a delimiter,650 ;; it must also increment all move indices by 1.651 ;; heren we decrement to correct this.652 (partial map dec)653 (partial take-while (comp not zero?)))654 [move-1 move-2 move-3 move-4]))656 types657 (set (list (types type-1)658 (types type-2)))659 TMs|HMs660 (map661 (comp662 (partial map first)663 (partial remove (comp zero? second)))664 (split-at665 50666 (map vector667 (rest(range))668 (reduce concat669 (map670 #(take 8671 (concat (bit-list %)672 (repeat 0)))674 TMs|HMs)))))676 TMs (vec (first TMs|HMs))677 HMs (take 5 (map (partial + -50) (vec (second TMs|HMs))))680 ]683 {:dex# n684 :base-moves base-moves685 :types types686 :TMs TMs687 :HMs HMs688 :base-hp rating-hp689 :base-atk rating-atk690 :base-def rating-def691 :base-speed rating-speed692 :base-special rating-special693 }))695 (partition entry-size696 (take (* entry-size pkmn-count)697 (drop 0x383DE698 rom))))))))702 (defn hxc-item-prices703 "The hardcoded list of item prices in memory. List begins at ROM@4495"704 ([] (hxc-item-prices com.aurellem.gb.gb-driver/original-rom))705 ([rom]706 (let [items (hxc-items rom)707 price-size 3]708 (zipmap items709 (map (comp710 ;; zero-cost items are "priceless"711 #(if (zero? %) :priceless %)712 decode-bcd butlast)713 (partition price-size714 (take (* price-size (count items))715 (drop 0x4495 rom))))))))717 (defn hxc-shops718 ([] (hxc-shops com.aurellem.gb.gb-driver/original-rom))719 ([rom]720 (let [items (zipmap (range) (hxc-items rom))722 ;; temporarily softcode the TM items723 items (into724 items725 (map (juxt identity726 (comp keyword727 (partial str "tm-")728 (partial + 1 -200)729 ))730 (take 200 (drop 200 (range)))))732 ]734 ((fn parse-shop [coll [num-items & items-etc]]735 (let [inventory (take-while736 (partial not= 0xFF)737 items-etc)738 [separator & items-etc] (drop num-items (rest items-etc))]739 (if (= separator 0x50)740 (map (partial mapv (comp items dec)) (conj coll inventory))741 (recur (conj coll inventory) items-etc)742 )743 ))745 '()746 (drop 0x233C rom))749 )))753 ;; ********************** MANIPULATION FNS756 (defn same-type757 ([pkmn move]758 (same-type759 com.aurellem.gb.gb-driver/original-rom pkmn move))760 ([rom pkmn move]761 (((comp :types (hxc-pokemon-base rom)) pkmn)762 ((comp :type (hxc-move-data rom)) move))))767 (defn submap?768 "Compares the two maps. Returns true if map-big has the same associations as map-small, otherwise false."769 [map-small map-big]770 (cond (empty? map-small) true771 (and772 (contains? map-big (ffirst map-small))773 (= (get map-big (ffirst map-small))774 (second (first map-small))))775 (recur (next map-small) map-big)777 :else false))780 (defn search-map [proto-map maps]781 "Returns all the maps that make the same associations as proto-map."782 (some (partial submap? proto-map) maps))784 (defn filter-vals785 "Returns a map consisting of all the pairs [key val] for786 which (pred key) returns true."787 [pred map]788 (reduce (partial apply assoc) {}789 (filter (fn [[k v]] (pred v)) map)))792 (defn search-moves793 "Returns a subcollection of all hardcoded moves with the794 given attributes. Attributes consist of :name :power795 :accuracy :pp :fx-id796 (and also :fx-txt, but it contains the same information797 as :fx-id)"798 ([attribute-map]799 (search-moves800 com.aurellem.gb.gb-driver/original-rom attribute-map))801 ([rom attribute-map]802 (filter-vals (partial submap? attribute-map)803 (hxc-move-data rom))))809 ;; note: 0x2f31 contains the names "TM" "HM"?811 ;; note for later: credits start at F1290815 (comment817 (def hxc-later818 "Running this code produces, e.g. hardcoded names NPCs give819 their pokemon. Will sort through it later."820 (print (character-codes->str(take 10000821 (drop 0x71597822 (rom (root)))))))824 (let [dex825 (partition-by #(= 0x50 %)826 (take 2540827 (drop 0x40687828 (rom (root)))))]829 (def dex dex)830 (def hxc-species831 (map character-codes->str832 (take-nth 4 dex))))833 )