Mercurial > vba-clojure
view clojure/com/aurellem/gb/hxc.clj @ 282:0c3fbb313e49
added price data. also, shop data seems a bit off.
author | Dylan Holmes <ocsenave@gmail.com> |
---|---|
date | Wed, 28 Mar 2012 03:28:36 -0500 |
parents | ca1afcef3542 |
children | 516acb83410f |
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)))))28 ;; used to decode item prices30 (defn decode-bcd31 "Take a sequence of binary-coded digits (in written order) and return the number they represent."32 [digits]33 ((fn self [coll]34 (if (empty? coll) 035 (+ (first coll) (* 100 (self (rest coll))))))36 (map37 #(+ (* 10 (int (/ % 16)))38 (rem % 16))39 (reverse digits))))44 (def pkmn-types45 [:normal ;;046 :fighting ;;147 :flying ;;248 :poison ;;349 :ground ;;450 :rock ;;551 :bird ;;652 :bug ;;753 :ghost ;;854 :A55 :B56 :C57 :D58 :E59 :F60 :G61 :H62 :I63 :J64 :K65 :fire ;;20 (0x14)66 :water ;;21 (0x15)67 :grass ;;22 (0x16)68 :electric ;;23 (0x17)69 :psychic ;;24 (0x18)70 :ice ;;25 (0x19)71 :dragon ;;26 (0x1A)72 ])75 ;; question: when status effects claim to take76 ;; their accuracy from the move accuracy, does77 ;; this mean that the move always "hits" but the78 ;; status effect may not?80 (def move-effects81 ["normal damage"82 "no damage, just opponent sleep" ;; how many turns? is atk power ignored?83 "0x4C chance of poison"84 "leech half of inflicted damage"85 "0x19 chance of burn"86 "0x19 chance of freeze"87 "0x19 chance of paralyze"88 "user faints; opponent defense halved during attack."89 "leech half of inflicted damage ONLY if sleeping opponent."90 "imitate last attack"91 "user atk +1"92 "user def +1"93 "user spd +1"94 "user spc +1"95 "user acr +1" ;; unused?!96 "user evd +1"97 "get post-battle $ = 2*level*uses"98 "0xFE acr, no matter what."99 "opponent atk -1" ;; acr taken from move acr?100 "opponent def -1" ;;101 "opponent spd -1" ;;102 "opponent spc -1" ;;103 "opponent acr -1";;104 "opponent evd -1"105 "converts user's type to opponent's."106 "(haze)"107 "(bide)"108 "(thrash)"109 "(teleport)"110 "(fury swipes)"111 "attacks 2-5 turns" ;; unused? like rollout?112 "0x19 chance of flinch"113 "opponent sleep for 1-7 turns"114 "0x66 chance of poison"115 "0x4D chance of burn"116 "0x4D chance of freeze"117 "0x4D chance of paralyze"118 "0x4D chance of flinch"119 "one-hit KO"120 "charge one turn, atk next."121 "fixed damage, leaves 1HP." ;; how is dmg determined?122 "fixed damage." ;; cf seismic toss, dragon rage, psywave.123 "atk 2-5 turns; opponent can't attack" ;; unnormalized? (0 0x60 0x60 0x20 0x20)124 "charge one turn, atk next. (can't be hit when charging)"125 "atk hits twice."126 "user takes 1 damage if misses."127 "evade status-lowering effects" ;;caused by you or also your opponent?128 "(broken) if user is slower than opponent, makes critical hit impossible, otherwise has no effect"129 "atk causes recoil dmg = 1/4 dmg dealt"130 "confuses opponent" ;; acr taken from move acr131 "user atk +2"132 "user def +2"133 "user spd +2"134 "user spc +2"135 "user acr +2" ;; unused!136 "user evd +2" ;; unused!137 "restores up to half of user's max hp." ;; broken: fails if the difference138 ;; b/w max and current hp is one less than a multiple of 256.139 "(transform)"140 "opponent atk -2"141 "opponent def -2"142 "opponent spd -2"143 "opponent spc -2"144 "opponent acr -2"145 "opponent evd -2"146 "doubles user spc when attacked"147 "doubles user def when attacked"148 "just poisons opponent" ;;acr taken from move acr149 "just paralyzes opponent" ;;150 "0x19 chance opponent atk -1"151 "0x19 chance opponent def -1"152 "0x19 chance opponent spd -1"153 "0x4C chance opponent spc -1" ;; context suggest chance is 0x19154 "0x19 chance opponent acr -1"155 "0x19 chance opponent evd -1"156 "???" ;; unused? no effect?157 "???" ;; unused? no effect?158 "0x19 chance opponent confused"159 "atk hits twice. 0x33 chance opponent poisioned."160 "broken. crash the game after attack."161 "(substitute)"162 "unless opponent faints, user must recharge after atk. some163 exceptions apply."164 "(rage)"165 "(mimic)"166 "(metronome)"167 "(leech seed)"168 "does nothing (splash)"169 "(disable)"170 ])173 ;; ************** HARDCODED DATA175 (defn hxc-thunk176 "Creates a thunk (nullary fn) that grabs data in a certain region of rom and177 splits it into a collection by 0x50. If rom is not supplied, uses the178 original rom data."179 [start length]180 (fn self181 ([rom]182 (take-nth 2183 (partition-by #(= % 0x50)184 (take length185 (drop start rom)))))186 ([]187 (self com.aurellem.gb.gb-driver/original-rom))))189 (def hxc-thunk-words190 "Same as hxc-thunk, except it interprets the rom data as characters,191 returning a collection of strings."192 (comp193 (partial comp (partial map character-codes->str))194 hxc-thunk))197 ;; --------------------------------------------------199 (def hxc-items200 "The hardcoded names of the items in memory. List begins at201 ROM@045B7"202 (hxc-thunk-words 0x45B7 870))204 (def hxc-types205 "The hardcoded type names in memory. List begins at ROM@27D99,206 shortly before hxc-titles."207 (hxc-thunk-words 0x27D99 102))209 (def hxc-titles210 "The hardcoded names of the trainer titles in memory. List begins at211 ROM@27E77"212 (hxc-thunk-words 0x27E77 196))215 (def hxc-pokedex-text216 "The hardcoded pokedex entries in memory. List begins at217 ROM@B8000, shortly before move names."218 (hxc-thunk-words 0xB8000 14754))221 ;; In red/blue, pokedex stats are in internal order.222 ;; In yellow, pokedex stats are in pokedex order.224 (defn hxc-pokedex-stats225 "The hardcoded pokedex stats (species height weight) in memory. List226 begins at ROM@40687"227 ;; uses hxc-pokedex-text to count pokemon228 ;; since hxc-pokenames includes several missingno"229 ([] (hxc-pokedex-stats com.aurellem.gb.gb-driver/original-rom))230 ([rom]231 (let [poketext (hxc-pokedex-text)232 pkmn-count (count poketext)233 ]234 ((fn capture-stats235 [n stats data]236 (if (zero? n) stats237 (let [[species238 [_239 height-ft240 height-in241 weight-1242 weight-2243 _244 dex-ptr-1245 dex-ptr-2246 dex-bank247 _248 & data]]249 (split-with (partial not= 0x50) data)]250 (recur (dec n)251 (assoc stats252 (- pkmn-count n)253 {:species254 (character-codes->str species)255 :height-ft256 height-ft257 :height-in258 height-in259 :weight260 (/ (low-high weight-1 weight-2) 10.)262 ;; :text263 ;; (character-codes->str264 ;; (take-while265 ;; (partial not= 0x50)266 ;; (drop267 ;; (+ 0xB8000268 ;; -0x4000269 ;; (low-high dex-ptr-1 dex-ptr-2))270 ;; rom)))271 })273 data)276 )))278 pkmn-count279 {}280 (drop 0x40687 rom))) ))288 (def hxc-places289 "The hardcoded place names in memory. List begins at290 ROM@71500. [Cinnabar] Mansion seems to be dynamically calculated."291 (hxc-thunk-words 0x71500 560))294 (defn hxc-dialog295 "The hardcoded dialogue in memory, including in-game alerts. Dialog296 seems to be separated by 0x57 instead of 0x50 (END). Begins at ROM@98000."297 ([rom]298 (map character-codes->str299 (take-nth 2300 (partition-by #(= % 0x57)301 (take 0x0F728302 (drop 0x98000 rom))))))303 ([]304 (hxc-dialog com.aurellem.gb.gb-driver/original-rom)))309 (def hxc-move-names310 "The hardcoded move names in memory. List begins at ROM@BC000"311 (hxc-thunk-words 0xBC000 1551))314 (defn hxc-move-data315 "The hardcoded (basic (move effects)) in memory. List begins at316 0x38000. Returns a map of {:name :power :accuracy :pp :fx-id317 :fx-txt}. The move descriptions are handwritten, not hardcoded."318 ([]319 (hxc-move-data com.aurellem.gb.gb-driver/original-rom))320 ([rom]321 (let [names (vec (hxc-move-names rom))322 move-count (count names)323 move-size 6324 types pkmn-types ;;; !! hardcoded types325 ]326 (zipmap (map format-name names)327 (map328 (fn [[idx effect power type-id accuracy pp]]329 {:name (names (dec idx))330 :power power331 :accuracy accuracy332 :pp pp333 :type (types type-id)334 :fx-id effect335 :fx-txt (get move-effects effect)336 }337 )339 (partition move-size340 (take (* move-size move-count)341 (drop 0x38000 rom))))))))345 (defn hxc-move-data*346 "Like hxc-move-data, but reports numbers as hexadecimal symbols instead."347 ([]348 (hxc-move-data* com.aurellem.gb.gb-driver/original-rom))349 ([rom]350 (let [names (vec (hxc-move-names rom))351 move-count (count names)352 move-size 6353 format-name (fn [s]354 (keyword (.toLowerCase355 (apply str356 (map #(if (= % \space) "-" %) s)))))357 ]358 (zipmap (map format-name names)359 (map360 (fn [[idx effect power type accuracy pp]]361 {:name (names (dec idx))362 :power power363 :accuracy (hex accuracy)364 :pp pp365 :fx-id (hex effect)366 :fx-txt (get move-effects effect)367 }368 )370 (partition move-size371 (take (* move-size move-count)372 (drop 0x38000 rom))))))))376 (defn hxc-pokenames377 "The hardcoded names of the 190 species in memory. List begins at378 ROM@E8000. Although names in memory are padded with 0x50 to be 10 characters379 long, these names are stripped of padding."380 ([]381 (hxc-pokenames com.aurellem.gb.gb-driver/original-rom))382 ([rom]383 (let [count-species 190384 name-length 10]385 (map character-codes->str386 (partition name-length387 (map #(if (= 0x50 %) 0x00 %)388 (take (* count-species name-length)389 (drop 0xE8000390 rom))))))))395 (defn internal-id396 ([rom]397 (zipmap398 (map format-name (hxc-pokenames rom))399 (range)))400 ([]401 (internal-id com.aurellem.gb.gb-driver/original-rom)))405 ;; nidoran gender change upon levelup406 ;; (->407 ;; @current-state408 ;; rom409 ;; vec410 ;; (rewrite-memory411 ;; (nth (hxc-ptrs-evolve) ((internal-id) :nidoran♂))412 ;; [1 1 15])413 ;; (rewrite-memory414 ;; (nth (hxc-ptrs-evolve) ((internal-id) :nidoran♀))415 ;; [1 1 3])416 ;; (write-rom!)418 ;; )423 (defn hxc-advantage424 "The hardcoded type advantages in memory, returned as tuples of atk-type def-type multiplier. By default (i.e. if not listed here),425 the multiplier is 1."426 ([] (hxc-advantage com.aurellem.gb.gb-driver/original-rom))427 ([rom]428 (map429 (fn [[atk def mult]] [(get pkmn-types atk (hex atk))430 (get pkmn-types def (hex def))431 (/ mult 10)])432 (partition 3433 (take-while (partial not= 0xFF)434 (drop 0x3E62D rom))))))438 (defn format-evo439 [coll]440 (let [method (first coll)]441 (cond (empty? coll) []442 (= 0 method) [] ;; just in case443 (= 1 method) ;; level-up evolution444 (conj (format-evo (drop 3 coll))445 {:method :level-up446 :min-level (nth coll 1)447 :into (dec (nth coll 2))})449 (= 2 method) ;; item evolution450 (conj (format-evo (drop 4 coll))451 {:method :item452 :item (dec (nth coll 1))453 :min-level (nth coll 2)454 :into (dec (nth coll 3))})456 (= 3 method) ;; trade evolution457 (conj (format-evo (drop 3 coll))458 {:method :trade459 :min-level (nth coll 1) ;; always 1 for trade.460 :into (dec (nth coll 2))}))))463 (defn hxc-ptrs-evolve464 "A hardcoded collection of 190 pointers to alternating evolution/learnset data,465 in internal order."466 ([]467 (hxc-ptrs-evolve com.aurellem.gb.gb-driver/original-rom))468 ([rom]469 (let [names (hxc-pokenames rom)470 pkmn-count (count names)471 ptrs472 (map (fn [[a b]] (low-high a b))473 (partition 2474 (take (* 2 pkmn-count)475 (drop 0x3b1e5 rom))))]476 (map (partial + 0x34000) ptrs)478 )))481 (defn hxc-learnsets482 "Hardcoded map associating pokemon names to lists of pairs [lvl483 move] of abilities they learn as they level up. The data484 exists at ROM@3400, sorted by internal order. Pointers to the data485 exist at ROM@3B1E5; see also, hxc-ptrs-evolve"486 ([] (hxc-learnsets com.aurellem.gb.gb-driver/original-rom))487 ([rom]488 (apply assoc489 {}490 (interleave491 (map format-name (hxc-pokenames rom))492 (map (comp493 (partial map494 (fn [[lvl mv]] [lvl (dec mv)]))495 (partial partition 2)496 ;; keep the learnset data497 (partial take-while (comp not zero?))498 ;; skip the evolution data499 rest500 (partial drop-while (comp not zero?)))501 (map #(drop % rom)502 (hxc-ptrs-evolve rom)))))))504 (defn hxc-learnsets-pretty505 "Live hxc-learnsets except it reports the name of each move --- as506 it appears in rom --- rather than the move index."507 ([] (hxc-learnsets-pretty com.aurellem.gb.gb-driver/original-rom))508 ([rom]509 (let [moves (vec(map format-name (hxc-move-names)))]510 (into {}511 (map (fn [[pkmn learnset]]512 [pkmn (map (fn [[lvl mv]] [lvl (moves mv)])513 learnset)])514 (hxc-learnsets rom))))))519 (defn hxc-evolution520 "Hardcoded evolution data in memory. The data exists at ROM@34000,521 sorted by internal order. Pointers to the data exist at ROM@3B1E5; see also, hxc-ptrs-evolve."522 ([] (hxc-evolution com.aurellem.gb.gb-driver/original-rom))523 ([rom]524 (apply assoc {}525 (interleave526 (map format-name (hxc-pokenames rom))527 (map528 (comp529 format-evo530 (partial take-while (comp not zero?))531 #(drop % rom))532 (hxc-ptrs-evolve rom)533 )))))535 (defn hxc-evolution-pretty536 "Like hxc-evolution, except it uses the names of items and pokemon537 --- grabbed from ROM --- rather than their numerical identifiers."538 ([] (hxc-evolution-pretty com.aurellem.gb.gb-driver/original-rom))539 ([rom]540 (let541 [poke-names (vec (map format-name (hxc-pokenames rom)))542 item-names (vec (map format-name (hxc-items rom)))543 use-names544 (fn [m]545 (loop [ks (keys m) new-map m]546 (let [k (first ks)]547 (cond (nil? ks) new-map548 (= k :into)549 (recur550 (next ks)551 (assoc new-map552 :into553 (poke-names554 (:into555 new-map))))556 (= k :item)557 (recur558 (next ks)559 (assoc new-map560 :item561 (item-names562 (:item new-map))))563 :else564 (recur565 (next ks)566 new-map)567 ))))]569 (into {}570 (map (fn [[pkmn evo-coll]]571 [pkmn (map use-names evo-coll)])572 (hxc-evolution rom))))))578 (defn hxc-pokemon-base579 ([] (hxc-pokemon-base com.aurellem.gb.gb-driver/original-rom))580 ([rom]581 (let [entry-size 28582 pkmn-count (count (hxc-pokedex-text rom))583 types (apply assoc {}584 (interleave585 (range)586 pkmn-types)) ;;!! softcoded587 moves (apply assoc {}588 (interleave589 (range)590 (map format-name591 (hxc-move-names rom))))592 ]593 (map595 (fn [[n596 rating-hp597 rating-atk598 rating-def599 rating-speed600 rating-special601 type-1602 type-2603 rarity604 rating-xp605 pic-dimensions606 ptr-pic-obverse-1607 ptr-pic-obverse-2608 ptr-pic-reverse-1609 ptr-pic-reverse-2610 move-1611 move-2612 move-3613 move-4614 growth-rate615 &616 TMs|HMs]]617 (let618 [base-moves619 (mapv moves620 ((comp621 ;; since the game uses zero as a delimiter,622 ;; it must also increment all move indices by 1.623 ;; heren we decrement to correct this.624 (partial map dec)625 (partial take-while (comp not zero?)))626 [move-1 move-2 move-3 move-4]))628 types629 (set (list (types type-1)630 (types type-2)))631 TMs|HMs632 (map633 (comp634 (partial map first)635 (partial remove (comp zero? second)))636 (split-at637 50638 (map vector639 (rest(range))640 (reduce concat641 (map642 #(take 8643 (concat (bit-list %)644 (repeat 0)))646 TMs|HMs)))))648 TMs (vec (first TMs|HMs))649 HMs (take 5 (map (partial + -50) (vec (second TMs|HMs))))652 ]655 {:dex# n656 :base-moves base-moves657 :types types658 :TMs TMs659 :HMs HMs660 :base-hp rating-hp661 :base-atk rating-atk662 :base-def rating-def663 :base-speed rating-speed664 :base-special rating-special665 }))667 (partition entry-size668 (take (* entry-size pkmn-count)669 (drop 0x383DE670 rom)))))))674 (defn hxc-item-prices675 "The hardcoded list of item prices in memory. List begins at ROM@4495"676 ([] (hxc-item-prices com.aurellem.gb.gb-driver/original-rom))677 ([rom]678 (let [items (map format-name (hxc-items rom))679 price-size 3]680 (zipmap items681 (map (comp682 ;; zero-cost items are "priceless"683 #(if (zero? %) :priceless %)684 decode-bcd butlast)685 (partition price-size686 (take (* price-size (count items))687 (drop 0x4495 rom))))))))689 (defn hxc-shops690 ([] (hxc-shops com.aurellem.gb.gb-driver/original-rom))691 ([rom]692 (let [items (zipmap (range) (map format-name (hxc-items rom)))694 ;; temporarily softcode the TM items695 items (into696 items697 (map (juxt identity698 (comp keyword699 (partial str "tm-")700 (partial + 1 -200)701 ))702 (take 200 (drop 200 (range)))))704 ]706 ((fn parse-shop [coll [num-items & items-etc]]707 (let [inventory (take-while708 (partial not= 0xFF)709 items-etc)710 [separator & items-etc] (drop num-items (rest items-etc))]711 (if (= separator 0x50)712 (map (partial mapv (comp items dec)) (conj coll inventory))713 (recur (conj coll inventory) items-etc)714 )715 ))717 '()718 (drop 0x233C rom))721 )))725 ;; ********************** MANIPULATION FNS730 (defn submap?731 "Compares the two maps. Returns true if map-big has the same associations as map-small, otherwise false."732 [map-small map-big]733 (cond (empty? map-small) true734 (and735 (contains? map-big (ffirst map-small))736 (= (get map-big (ffirst map-small))737 (second (first map-small))))738 (recur (next map-small) map-big)740 :else false))743 (defn search-map [proto-map maps]744 "Returns all the maps that make the same associations as proto-map."745 (some (partial submap? proto-map) maps))747 (defn filter-vals748 "Returns a map consisting of all the pairs [key val] for749 which (pred key) returns true."750 [pred map]751 (reduce (partial apply assoc) {}752 (filter (fn [[k v]] (pred v)) map)))755 (defn search-moves756 "Returns a subcollection of all hardcoded moves with the757 given attributes. Attributes consist of :name :power758 :accuracy :pp :fx-id759 (and also :fx-txt, but it contains the same information760 as :fx-id)"761 ([attribute-map]762 (search-moves763 com.aurellem.gb.gb-driver/original-rom attribute-map))764 ([rom attribute-map]765 (filter-vals (partial submap? attribute-map)766 (hxc-move-data rom))))772 ;; note for later: credits start at F1290777 ;; (def dex-order778 ;; [:bulbasaur779 ;; :ivysaur780 ;; :venusaur781 ;; :charmander782 ;; :charmeleon783 ;; :charizard])786 ;; (defn same-type-attack-bonus?787 ;; ([pkmn move]788 ;; (same-type-attack-bonus?789 ;; com.aurellem.gb.gb-driver/original-rom pkmn move))790 ;; ([rom pkmn move]791 ;; (hxc-pokemon-base rom)))798 (comment800 (def hxc-later801 "Running this code produces, e.g. hardcoded names NPCs give802 their pokemon. Will sort through it later."803 (print (character-codes->str(take 10000804 (drop 0x71597805 (rom (root)))))))807 (let [dex808 (partition-by #(= 0x50 %)809 (take 2540810 (drop 0x40687811 (rom (root)))))]812 (def dex dex)813 (def hxc-species814 (map character-codes->str815 (take-nth 4 dex))))816 )