Mercurial > vba-clojure
view clojure/com/aurellem/gb/hxc.clj @ 424:7bd806c4dbb6
changed assembly to handle mini-midi messages of different lengths.
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Mon, 23 Apr 2012 04:45:55 -0500 |
parents | acc3d1ad24e8 |
children |
line wrap: on
line source
2 (ns com.aurellem.gb.hxc3 (:use (com.aurellem.gb assembly characters gb-driver util mem-util4 constants species))5 (:import [com.aurellem.gb.gb_driver SaveState]))7 ; ************* HANDWRITTEN CONSTANTS9 (def pkmn-types10 [:normal ;;011 :fighting ;;112 :flying ;;213 :poison ;;314 :ground ;;415 :rock ;;516 :bird ;;617 :bug ;;718 :ghost ;;819 :A20 :B21 :C22 :D23 :E24 :F25 :G26 :H27 :I28 :J29 :K30 :fire ;;20 (0x14)31 :water ;;21 (0x15)32 :grass ;;22 (0x16)33 :electric ;;23 (0x17)34 :psychic ;;24 (0x18)35 :ice ;;25 (0x19)36 :dragon ;;26 (0x1A)37 ])40 ;; question: when status effects claim to take41 ;; their accuracy from the move accuracy, does42 ;; this mean that the move always "hits" but the43 ;; status effect may not?45 (def move-effects46 ["normal damage"47 "no damage, just opponent sleep" ;; how many turns? is atk power ignored?48 "0x4C chance of poison"49 "leech half of inflicted damage"50 "0x19 chance of burn"51 "0x19 chance of freeze"52 "0x19 chance of paralyze"53 "user faints; opponent defense halved during attack."54 "leech half of inflicted damage ONLY if sleeping opponent."55 "imitate last attack"56 "user atk +1"57 "user def +1"58 "user spd +1"59 "user spc +1"60 "user acr +1" ;; unused?!61 "user evd +1"62 "get post-battle $ = 2*level*uses"63 "0xFE acr, no matter what."64 "opponent atk -1" ;; acr taken from move acr?65 "opponent def -1" ;;66 "opponent spd -1" ;;67 "opponent spc -1" ;;68 "opponent acr -1";;69 "opponent evd -1"70 "converts user's type to opponent's."71 "(haze)"72 "(bide)"73 "(thrash)"74 "(teleport)"75 "(fury swipes)"76 "attacks 2-5 turns" ;; unused? like rollout?77 "0x19 chance of flinch"78 "opponent sleep for 1-7 turns"79 "0x66 chance of poison"80 "0x4D chance of burn"81 "0x4D chance of freeze"82 "0x4D chance of paralyze"83 "0x4D chance of flinch"84 "one-hit KO"85 "charge one turn, atk next."86 "fixed damage, leaves 1HP." ;; how is dmg determined?87 "fixed damage." ;; cf seismic toss, dragon rage, psywave.88 "atk 2-5 turns; opponent can't attack" ;; unnormalized? (0 0x60 0x60 0x20 0x20)89 "charge one turn, atk next. (can't be hit when charging)"90 "atk hits twice."91 "user takes 1 damage if misses."92 "evade status-lowering effects" ;;caused by you or also your opponent?93 "(broken) if user is slower than opponent, makes critical hit impossible, otherwise has no effect"94 "atk causes recoil dmg = 1/4 dmg dealt"95 "confuses opponent" ;; acr taken from move acr96 "user atk +2"97 "user def +2"98 "user spd +2"99 "user spc +2"100 "user acr +2" ;; unused!101 "user evd +2" ;; unused!102 "restores up to half of user's max hp." ;; broken: fails if the difference103 ;; b/w max and current hp is one less than a multiple of 256.104 "(transform)"105 "opponent atk -2"106 "opponent def -2"107 "opponent spd -2"108 "opponent spc -2"109 "opponent acr -2"110 "opponent evd -2"111 "doubles user spc when attacked"112 "doubles user def when attacked"113 "just poisons opponent" ;;acr taken from move acr114 "just paralyzes opponent" ;;115 "0x19 chance opponent atk -1"116 "0x19 chance opponent def -1"117 "0x19 chance opponent spd -1"118 "0x4C chance opponent spc -1" ;; context suggest chance is 0x19119 "0x19 chance opponent acr -1"120 "0x19 chance opponent evd -1"121 "???" ;; unused? no effect?122 "???" ;; unused? no effect?123 "0x19 chance opponent confused"124 "atk hits twice. 0x33 chance opponent poisioned."125 "broken. crash the game after attack."126 "(substitute)"127 "unless opponent faints, user must recharge after atk. some128 exceptions apply."129 "(rage)"130 "(mimic)"131 "(metronome)"132 "(leech seed)"133 "does nothing (splash)"134 "(disable)"135 ])137 ;; ************** HARDCODED DATA139 (defn hxc-thunk140 "Creates a thunk (nullary fn) that grabs data in a certain region of rom and141 splits it into a collection by 0x50. If rom is not supplied, uses the142 original rom data."143 [start length]144 (fn self145 ([rom]146 (take-nth 2147 (partition-by #(= % 0x50)148 (take length149 (drop start rom)))))150 ([]151 (self com.aurellem.gb.gb-driver/original-rom))))153 (def hxc-thunk-words154 "Same as hxc-thunk, except it interprets the rom data as characters,155 returning a collection of strings."156 (comp157 (partial comp (partial map character-codes->str))158 hxc-thunk))160 ;; --------------------------------------------------163 (defn hxc-pokenames-raw164 "The hardcoded names of the 190 species in memory. List begins at165 ROM@E8000. Although names in memory are padded with 0x50 to be 10 characters166 long, these names are stripped of padding. See also, hxc-pokedex-names"167 ([]168 (hxc-pokenames-raw com.aurellem.gb.gb-driver/original-rom))169 ([rom]170 (let [count-species 190171 name-length 10]172 (map character-codes->str173 (partition name-length174 (map #(if (= 0x50 %) 0x00 %)175 (take (* count-species name-length)176 (drop 0xE8000177 rom))))))))178 (def hxc-pokenames179 (comp180 (partial map format-name)181 hxc-pokenames-raw))186 (defn hxc-pokedex-names187 "The names of the pokemon in hardcoded pokedex order. List of the188 pokedex numbers of each pokemon (in internal order) begins at189 ROM@410B1. See also, hxc-pokenames."190 ([] (hxc-pokedex-names191 com.aurellem.gb.gb-driver/original-rom))192 ([rom]193 (let [names (hxc-pokenames rom)]194 (#(mapv %195 ((comp range count keys) %))196 (zipmap197 (take (count names)198 (drop 0x410b1 rom))200 names)))))202 (def hxc-types203 "The hardcoded type names in memory. List begins at ROM@27D99,204 shortly before hxc-titles."205 (hxc-thunk-words 0x27D99 102))208 ;; http://hax.iimarck.us/topic/581/209 (defn hxc-cry210 "The pokemon cry data in internal order. List begins at ROM@39462"211 ([](hxc-cry com.aurellem.gb.gb-driver/original-rom))212 ([rom]213 (zipmap214 (hxc-pokenames rom)215 (map216 (fn [[cry-id pitch length]]217 {:cry-id cry-id218 :pitch pitch219 :length length}220 )221 (partition 3222 (drop 0x39462 rom))))))224 (defn hxc-cry-groups225 ([] (hxc-cry-groups com.aurellem.gb.gb-driver/original-rom))226 ([rom]227 (map #(mapv first228 (filter229 (fn [[k v]]230 (= % (:cry-id v)))231 (hxc-cry)))232 ((comp233 range234 count235 set236 (partial map :cry-id)237 vals238 hxc-cry)239 rom))))242 (defn cry-conversion!243 "Convert Porygon's cry in ROM to be the cry of the given pokemon."244 [pkmn]245 (write-rom!246 (rewrite-memory247 (vec(rom))248 0x3965D249 (map second250 ((hxc-cry) pkmn)))))255 (def hxc-items-raw256 "The hardcoded names of the items in memory. List begins at257 ROM@045B7"258 (hxc-thunk-words 0x45B7 870))260 (def hxc-items261 "The hardcoded names of the items in memory, presented as262 keywords. List begins at ROM@045B7. See also, hxc-items-raw."263 (comp (partial map format-name) hxc-items-raw))267 (def hxc-titles268 "The hardcoded names of the trainer titles in memory. List begins at269 ROM@27E77"270 (hxc-thunk-words 0x27E77 196))273 (def hxc-pokedex-text-raw274 "The hardcoded pokedex entries in memory. List begins at275 ROM@B8000, shortly before move names."276 (hxc-thunk-words 0xB8000 14754))281 (defn hxc-pokedex-text282 "The hardcoded pokedex entries in memory, presented as an283 associative hash map. List begins at ROM@B8000."284 ([] (hxc-pokedex-text com.aurellem.gb.gb-driver/original-rom))285 ([rom]286 (zipmap287 (hxc-pokedex-names rom)288 (cons nil ;; for missingno.289 (hxc-pokedex-text-raw rom)))))291 ;; In red/blue, pokedex stats are in internal order.292 ;; In yellow, pokedex stats are in pokedex order.293 (defn hxc-pokedex-stats294 "The hardcoded pokedex stats (species height weight) in memory. List295 begins at ROM@40687"296 ([] (hxc-pokedex-stats com.aurellem.gb.gb-driver/original-rom))297 ([rom]298 (let [pokedex-names (zipmap (range) (hxc-pokedex-names rom))299 pkmn-count (count pokedex-names)300 ]301 ((fn capture-stats302 [n stats data]303 (if (zero? n) stats304 (let [[species305 [_306 height-ft307 height-in308 weight-1309 weight-2310 _311 dex-ptr-1312 dex-ptr-2313 dex-bank314 _315 & data]]316 (split-with (partial not= 0x50) data)]317 (recur (dec n)318 (assoc stats319 (pokedex-names (- pkmn-count (dec n)))320 {:species321 (format-name (character-codes->str species))322 :height-ft323 height-ft324 :height-in325 height-in326 :weight327 (/ (low-high weight-1 weight-2) 10.)329 ;; :text330 ;; (character-codes->str331 ;; (take-while332 ;; (partial not= 0x50)333 ;; (drop334 ;; (+ 0xB8000335 ;; -0x4000336 ;; (low-high dex-ptr-1 dex-ptr-2))337 ;; rom)))338 })340 data)343 )))345 pkmn-count346 {}347 (drop 0x40687 rom))) ))352 (def hxc-places353 "The hardcoded place names in memory. List begins at354 ROM@71500. [Cinnabar/Celadon] Mansion seems to be dynamically calculated."355 (hxc-thunk-words 0x71500 560))357 (defn hxc-dialog358 "The hardcoded dialogue in memory, including in-game alerts. Dialog359 seems to be separated by 0x57 instead of 0x50 (END). Begins at ROM@98000."360 ([rom]361 (map character-codes->str362 (take-nth 2363 (partition-by #(= % 0x57)364 (take 0x0F728365 (drop 0x98000 rom))))))366 ([]367 (hxc-dialog com.aurellem.gb.gb-driver/original-rom)))370 (def hxc-move-names371 "The hardcoded move names in memory. List begins at ROM@BC000"372 (hxc-thunk-words 0xBC000 1551))373 (defn hxc-move-data374 "The hardcoded (basic (move effects)) in memory. List begins at375 0x38000. Returns a map of {:name :power :accuracy :pp :fx-id376 :fx-txt}. The move descriptions are handwritten, not hardcoded."377 ([]378 (hxc-move-data com.aurellem.gb.gb-driver/original-rom))379 ([rom]380 (let [names (vec (hxc-move-names rom))381 move-count (count names)382 move-size 6383 types pkmn-types ;;; !! hardcoded types384 ]385 (zipmap (map format-name names)386 (map387 (fn [[idx effect power type-id accuracy pp]]388 {:name (names (dec idx))389 :power power390 :accuracy accuracy391 :pp pp392 :type (types type-id)393 :fx-id effect394 :fx-txt (get move-effects effect)395 }396 )398 (partition move-size399 (take (* move-size move-count)400 (drop 0x38000 rom))))))))404 (defn hxc-move-data*405 "Like hxc-move-data, but reports numbers as hexadecimal symbols instead."406 ([]407 (hxc-move-data* com.aurellem.gb.gb-driver/original-rom))408 ([rom]409 (let [names (vec (hxc-move-names rom))410 move-count (count names)411 move-size 6412 format-name (fn [s]413 (keyword (.toLowerCase414 (apply str415 (map #(if (= % \space) "-" %) s)))))416 ]417 (zipmap (map format-name names)418 (map419 (fn [[idx effect power type accuracy pp]]420 {:name (names (dec idx))421 :power power422 :accuracy (hex accuracy)423 :pp pp424 :fx-id (hex effect)425 :fx-txt (get move-effects effect)426 }427 )429 (partition move-size430 (take (* move-size move-count)431 (drop 0x38000 rom))))))))434 (defn hxc-machines435 "The hardcoded moves taught by TMs and HMs. List begins at ROM@1232D."436 ([] (hxc-machines437 com.aurellem.gb.gb-driver/original-rom))438 ([rom]439 (let [moves (hxc-move-names rom)]440 (zipmap441 (range)442 (take-while443 (comp not nil?)444 (map (comp445 format-name446 (zipmap447 (range)448 moves)449 dec)450 (take 100451 (drop 0x1232D rom))))))))456 (defn internal-id457 ([rom]458 (zipmap459 (hxc-pokenames rom)460 (range)))461 ([]462 (internal-id com.aurellem.gb.gb-driver/original-rom)))468 ;; nidoran gender change upon levelup469 ;; (->470 ;; @current-state471 ;; rom472 ;; vec473 ;; (rewrite-memory474 ;; (nth (hxc-ptrs-evolve) ((internal-id) :nidoran♂))475 ;; [1 1 15])476 ;; (rewrite-memory477 ;; (nth (hxc-ptrs-evolve) ((internal-id) :nidoran♀))478 ;; [1 1 3])479 ;; (write-rom!)481 ;; )485 (defn hxc-advantage486 ;; in-game multipliers are stored as 10x their effective value487 ;; to allow for fractional multipliers like 1/2489 "The hardcoded type advantages in memory, returned as tuples of490 atk-type def-type multiplier. By default (i.e. if not listed here),491 the multiplier is 1. List begins at 0x3E62D."492 ([] (hxc-advantage com.aurellem.gb.gb-driver/original-rom))493 ([rom]494 (map495 (fn [[atk def mult]] [(get pkmn-types atk (hex atk))496 (get pkmn-types def (hex def))497 (/ mult 10)])498 (partition 3499 (take-while (partial not= 0xFF)500 (drop 0x3E62D rom))))))504 (defn format-evo505 "Parse a sequence of evolution data, returning a map. First is the506 method: 0 = end-evolution-data. 1 = level-up, 2 = item, 3 = trade. Next is an item id, if the507 method of evolution is by item (only stones will actually make pokemon508 evolve, for some auxillary reason.) Finally, the minimum level for509 evolution to occur (level 1 means no limit, which is used for trade510 and item evolutions), followed by the internal id of the pokemon511 into which to evolve. Hence, level up and trade evolutions are512 described with 3513 bytes; item evolutions with four."514 [coll]515 (let [method (first coll)]516 (cond (empty? coll) []517 (= 0 method) [] ;; just in case518 (= 1 method) ;; level-up evolution519 (conj (format-evo (drop 3 coll))520 {:method :level-up521 :min-level (nth coll 1)522 :into (dec (nth coll 2))})524 (= 2 method) ;; item evolution525 (conj (format-evo (drop 4 coll))526 {:method :item527 :item (dec (nth coll 1))528 :min-level (nth coll 2)529 :into (dec (nth coll 3))})531 (= 3 method) ;; trade evolution532 (conj (format-evo (drop 3 coll))533 {:method :trade534 :min-level (nth coll 1) ;; always 1 for trade.535 :into (dec (nth coll 2))}))))538 (defn hxc-ptrs-evolve539 "A hardcoded collection of 190 pointers to alternating evolution/learnset data,540 in internal order."541 ([]542 (hxc-ptrs-evolve com.aurellem.gb.gb-driver/original-rom))543 ([rom]544 (let [545 pkmn-count (count (hxc-pokenames-raw)) ;; 190546 ptrs547 (map (fn [[a b]] (low-high a b))548 (partition 2549 (take (* 2 pkmn-count)550 (drop 0x3b1e5 rom))))]551 (map (partial + 0x34000) ptrs)553 )))555 (defn hxc-evolution556 "Hardcoded evolution data in memory. The data exists at ROM@34000,557 sorted by internal order. Pointers to the data exist at ROM@3B1E5; see also, hxc-ptrs-evolve."558 ([] (hxc-evolution com.aurellem.gb.gb-driver/original-rom))559 ([rom]560 (apply assoc {}561 (interleave562 (hxc-pokenames rom)563 (map564 (comp565 format-evo566 (partial take-while (comp not zero?))567 #(drop % rom))568 (hxc-ptrs-evolve rom)569 )))))571 (defn hxc-evolution-pretty572 "Like hxc-evolution, except it uses the names of items and pokemon573 --- grabbed from ROM --- rather than their numerical identifiers."574 ([] (hxc-evolution-pretty com.aurellem.gb.gb-driver/original-rom))575 ([rom]576 (let577 [poke-names (vec (hxc-pokenames rom))578 item-names (vec (hxc-items rom))579 use-names580 (fn [m]581 (loop [ks (keys m) new-map m]582 (let [k (first ks)]583 (cond (nil? ks) new-map584 (= k :into)585 (recur586 (next ks)587 (assoc new-map588 :into589 (poke-names590 (:into591 new-map))))592 (= k :item)593 (recur594 (next ks)595 (assoc new-map596 :item597 (item-names598 (:item new-map))))599 :else600 (recur601 (next ks)602 new-map)603 ))))]605 (into {}606 (map (fn [[pkmn evo-coll]]607 [pkmn (map use-names evo-coll)])608 (hxc-evolution rom))))))613 (defn hxc-learnsets614 "Hardcoded map associating pokemon names to lists of pairs [lvl615 move] of abilities they learn as they level up. The data616 exists at ROM@34000, sorted by internal order. Pointers to the data617 exist at ROM@3B1E5; see also, hxc-ptrs-evolve"618 ([] (hxc-learnsets com.aurellem.gb.gb-driver/original-rom))619 ([rom]620 (apply assoc621 {}622 (interleave623 (hxc-pokenames rom)624 (map (comp625 (partial map626 (fn [[lvl mv]] [lvl (dec mv)]))627 (partial partition 2)628 ;; keep the learnset data629 (partial take-while (comp not zero?))630 ;; skip the evolution data631 rest632 (partial drop-while (comp not zero?)))633 (map #(drop % rom)634 (hxc-ptrs-evolve rom)))))))636 (defn hxc-learnsets-pretty637 "Live hxc-learnsets except it reports the name of each move --- as638 it appears in rom --- rather than the move index."639 ([] (hxc-learnsets-pretty com.aurellem.gb.gb-driver/original-rom))640 ([rom]641 (let [moves (vec(map format-name (hxc-move-names)))]642 (into {}643 (map (fn [[pkmn learnset]]644 [pkmn (map (fn [[lvl mv]] [lvl (moves mv)])645 learnset)])646 (hxc-learnsets rom))))))650 (defn hxc-pokemon-base651 ([] (hxc-pokemon-base com.aurellem.gb.gb-driver/original-rom))652 ([rom]653 (let [entry-size 28655 pokemon (rest (hxc-pokedex-names))656 pkmn-count (inc(count pokemon))657 types (apply assoc {}658 (interleave659 (range)660 pkmn-types)) ;;!! softcoded661 moves (apply assoc {}662 (interleave663 (range)664 (map format-name665 (hxc-move-names rom))))666 machines (hxc-machines)667 ]668 (zipmap669 pokemon670 (map671 (fn [[n672 rating-hp673 rating-atk674 rating-def675 rating-speed676 rating-special677 type-1678 type-2679 rarity680 rating-xp681 pic-dimensions ;; tile_width|tile_height (8px/tile)682 ptr-pic-obverse-1683 ptr-pic-obverse-2684 ptr-pic-reverse-1685 ptr-pic-reverse-2686 move-1687 move-2688 move-3689 move-4690 growth-rate691 &692 TMs|HMs]]693 (let694 [base-moves695 (mapv moves696 ((comp697 ;; since the game uses zero as a delimiter,698 ;; it must also increment all move indices by 1.699 ;; heren we decrement to correct this.700 (partial map dec)701 (partial take-while (comp not zero?)))702 [move-1 move-2 move-3 move-4]))704 types705 (set (list (types type-1)706 (types type-2)))707 TMs|HMs708 (map709 (comp710 (partial map first)711 (partial remove (comp zero? second)))712 (split-at713 50714 (map vector715 (rest(range))716 (reduce concat717 (map718 #(take 8719 (concat (bit-list %)720 (repeat 0)))722 TMs|HMs)))))724 TMs (vec (first TMs|HMs))725 HMs (take 5 (map (partial + -50) (vec (second TMs|HMs))))728 ]731 {:dex# n732 :base-moves base-moves733 :types types734 :TMs TMs735 :HMs HMs736 :base-hp rating-hp737 :base-atk rating-atk738 :base-def rating-def739 :base-speed rating-speed740 :base-special rating-special741 :o0 pic-dimensions742 :o1 ptr-pic-obverse-1743 :o2 ptr-pic-obverse-2744 }))746 (partition entry-size747 (take (* entry-size pkmn-count)748 (drop 0x383DE749 rom))))))))753 (defn hxc-intro-pkmn754 "The hardcoded pokemon to display in Prof. Oak's introduction; the pokemon's755 internal id is stored at ROM@5EDB."756 ([] (hxc-intro-pkmn757 com.aurellem.gb.gb-driver/original-rom))758 ([rom]759 (nth (hxc-pokenames rom) (nth rom 0x5EDB))))761 (defn sxc-intro-pkmn!762 "Set the hardcoded pokemon to display in Prof. Oak's introduction."763 [pokemon]764 (write-rom!765 (rewrite-rom 0x5EDB766 [767 (inc768 ((zipmap769 (hxc-pokenames)770 (range))771 pokemon))])))774 (defn hxc-item-prices775 "The hardcoded list of item prices in memory. List begins at ROM@4495"776 ([] (hxc-item-prices com.aurellem.gb.gb-driver/original-rom))777 ([rom]778 (let [items (hxc-items rom)779 price-size 3]780 (zipmap items781 (map (comp782 ;; zero-cost items are "priceless"783 #(if (zero? %) :priceless %)784 decode-bcd butlast)785 (partition price-size786 (take (* price-size (count items))787 (drop 0x4495 rom))))))))789 (defn hxc-shops790 ([] (hxc-shops com.aurellem.gb.gb-driver/original-rom))791 ([rom]792 (let [items (zipmap (range) (hxc-items rom))794 ;; temporarily softcode the TM items795 items (into796 items797 (map (juxt identity798 (comp keyword799 (partial str "tm-")800 (partial + 1 -200)801 ))802 (take 200 (drop 200 (range)))))804 ]806 ((fn parse-shop [coll [num-items & items-etc]]807 (let [inventory (take-while808 (partial not= 0xFF)809 items-etc)810 [separator & items-etc] (drop num-items (rest items-etc))]811 (if (= separator 0x50)812 (map (partial mapv (comp items dec)) (conj coll inventory))813 (recur (conj coll inventory) items-etc)814 )815 ))817 '()818 (drop 0x233C rom))821 )))826 (defn hxc-ptrs-wild827 "A list of the hardcoded wild encounter data in memory. Pointers828 begin at ROM@0CB95; data begins at ROM@0x04D89"829 ([] (hxc-ptrs-wild com.aurellem.gb.gb-driver/original-rom))830 ([rom]831 (let [ptrs832 (map (fn [[a b]] (+ a (* 0x100 b)))833 (take-while (partial not= (list 0xFF 0xFF))834 (partition 2 (drop 0xCB95 rom))))]835 ptrs)))839 (defn hxc-wilds840 "A list of the hardcoded wild encounter data in memory. Pointers841 begin at ROM@0CB95; data begins at ROM@0x04D89"842 ([] (hxc-wilds com.aurellem.gb.gb-driver/original-rom))843 ([rom]844 (let [pokenames (zipmap (range) (hxc-pokenames rom))]845 (map846 (partial map (fn [[a b]] {:species (pokenames (dec b)) :level847 a}))848 (partition 10850 (take-while (comp (partial not= 1)851 first)852 (partition 2853 (drop 0xCD8C rom))855 ))))))859 ;; ********************** MANIPULATION FNS862 (defn same-type863 ([pkmn move]864 (same-type865 com.aurellem.gb.gb-driver/original-rom pkmn move))866 ([rom pkmn move]867 (((comp :types (hxc-pokemon-base rom)) pkmn)868 ((comp :type (hxc-move-data rom)) move))))873 (defn submap?874 "Compares the two maps. Returns true if map-big has the same associations as map-small, otherwise false."875 [map-small map-big]876 (cond (empty? map-small) true877 (and878 (contains? map-big (ffirst map-small))879 (= (get map-big (ffirst map-small))880 (second (first map-small))))881 (recur (next map-small) map-big)883 :else false))886 (defn search-map [proto-map maps]887 "Returns all the maps that make the same associations as proto-map."888 (some (partial submap? proto-map) maps))890 (defn filter-vals891 "Returns a map consisting of all the pairs [key val] for892 which (pred key) returns true."893 [pred map]894 (reduce (partial apply assoc) {}895 (filter (fn [[k v]] (pred v)) map)))898 (defn search-moves899 "Returns a subcollection of all hardcoded moves with the900 given attributes. Attributes consist of :name :power901 :accuracy :pp :fx-id902 (and also :fx-txt, but it contains the same information903 as :fx-id)"904 ([attribute-map]905 (search-moves906 com.aurellem.gb.gb-driver/original-rom attribute-map))907 ([rom attribute-map]908 (filter-vals (partial submap? attribute-map)909 (hxc-move-data rom))))915 ;; note: 0x2f31 contains the names "TM" "HM"?917 ;; note for later: credits start at F1290919 ;; note: DADB hyper-potion-hp _ _ _ super-potion-hp _ _ _ potion-hp ??921 ;; note: DD4D spells out pokemon vital stat names ("speed", etc.)923 ;; note: 1195C-6A says ABLE#NOT ABLE#, but so does 119C0-119CE.924 ;; The first instance is for Machines; the second, for stones.926 ;; note: according to927 ;; http://www.upokecenter.com/games/rby/guides/rgbtrainers.php928 ;; the amount of money given by a trainer is equal to the929 ;; base money times the level of the last Pokemon on that trainer's930 ;; list. Other sources say it's the the level of the last pokemon931 ;; /defeated/.933 ;; todo: find base money.936 ;; note: 0xDFEA (in indexable mem) is the dex# of the currently-viewed Pokemon in937 ;; in the pokedex. It's used for other purposes if there is none.939 ;; note: 0x9D35 (index.) switches from 0xFF to 0x00 temporarily when940 ;; you walk between areas.942 ;; note: 0xD059 (index.) is the special battle type of your next battle:943 ;; - 00 is a usual battle944 ;; - 01 is a pre-scripted OLD MAN battle which always fails to catch the945 ;; target Pokemon.946 ;; - 02 is a safari zone battle947 ;; - 03 obligates you to run away. (unused)948 ;; - 04 is a pre-scripted OAK battle, which (temporarily) causes the949 ;; enemy Pokemon to cry PIKAAA, and which always catches the target950 ;; Pokemon. The target Pokemon is erased after the battle.951 ;; - 05+ are glitch states in which you are sort of the Pokemon.954 ;; note: 0x251A (in indexable mem): image decompression routine seems to begin here.956 ;; note: 0x4845 (index): vending inventory is loaded here. possibly957 ;; other things, too.958 (comment959 ;; temporarily intercept/adjust what pops out of the vending960 ;; machine.961 ;; (and how much it costs)963 ;; located at 0x4845964 ;; not to be confused with shop inventory, 0xCF7B965 (do966 (step (read-state "vend-menu"))967 (write-memory! (rewrite-memory (vec(memory)) 0x4845 [2 0 1 0]))968 (step @current-state [:a])969 (step @current-state [])970 (nstep @current-state 200) ))973 ;; Note: There are two tile tables, one from 8000-8FFF, the other from974 ;; 8800-97FF. The latter contains symbols, possibly map tiles(?), with some japanese chars and stuff at the end.975 (defn print-pixel-letters!976 "The pixel tiles representing letters. Neat!"977 ([] (print-pixel-letters! (read-state "oak-speaks")))978 ([state]979 (map980 (comp981 println982 (partial map #(if (zero? %) \space 0))983 #(if (< (count %) 8)984 (recur (cons 0 %))985 %)986 reverse bit-list)988 (take 0xFFF (drop 0x8800 (memory state))))))991 ;; (defn test-2 []992 ;; (loop [n 0993 ;; pc-1 (pc-trail (-> state-defend (tick) (step [:a]) (step [:a]) (step []) (nstep 100)) 100000)994 ;; pc-2 (pc-trail (-> state-speed (tick) (step [:a]) (step [:a])995 ;; (step []) (nstep 100)) 100000)]996 ;; (cond (empty? (drop n pc-1)) [pc-1 n]997 ;; (not= (take 10 (drop n pc-1)) (take 10 pc-2))998 ;; (recur pc-1 pc-2 (inc n))999 ;; :else1000 ;; [(take 1000 pc-2) n])))1005 (defn test-31006 "Explore trainer data"1007 ([] (test-3 0x3A289))1008 ([start]1009 (let [pokenames (vec(hxc-pokenames-raw))]1010 (println1011 (reduce1012 str1013 (map1014 (fn [[adr lvl pkmn]]1015 (str (format "%-11s %4d %02X %02X \t %05X\n"1017 (cond1018 (zero? lvl) "+"1019 (nil? (get pokenames (dec pkmn)))1020 "-"1021 :else1022 (get pokenames (dec pkmn)))1023 lvl1024 pkmn1025 lvl1026 adr1027 )))1028 (map cons1029 (take-nth 2 (drop start (range)))1030 (partition 21031 (take 400;;7031032 (drop1033 start1034 ;; 0x3A75D1035 (rom)))))))))))1037 (defn search-memory* [mem codes k]1038 (loop [index 01039 index-next 11040 start-match 01041 to-match codes1042 matches []]1043 (cond1044 (>= index (count mem)) matches1046 (empty? to-match)1047 (recur1048 index-next1049 (inc index-next)1050 index-next1051 codes1052 (conj matches1053 [(hex start-match) (take k (drop start-match mem))])1054 )1056 (or (= (first to-match) \_) ;; wildcard1057 (= (first to-match) (nth mem index)))1058 (recur1059 (inc index)1060 index-next1061 start-match1062 (rest to-match)1063 matches)1065 :else1066 (recur1067 index-next1068 (inc index-next)1069 index-next1070 codes1071 matches))))1074 (defn search-pattern [ptn coll]1075 (loop1076 [index 01077 to-match ptn1078 binds {}1080 next-index 11081 match-start 01082 matches []]1084 (cond1085 (>= index (count coll)) matches1086 (empty? to-match)1087 (recur1088 next-index1089 ptn1090 {}1091 (inc next-index)1092 next-index1093 (conj match-start1094 [(hex match-start) binds]))1096 :else1097 (let [k (first to-match)1098 v (nth coll index)]1099 (cond1100 (= k \_) ;; wildcard1101 (recur1102 (inc index)1103 (rest to-match)1104 binds1106 next-index1107 match-start1108 matches)1110 (keyword? k)1111 (if (binds k)1112 (if (= (binds k) v)1114 ;; consistent bindings1115 (recur1116 (inc index)1117 (rest to-match)1118 binds1120 next-index1121 match-start1122 matches)1124 ;; inconsistent bindings1125 (recur1126 next-index1127 ptn1128 {}1129 (inc next-index)1130 next-index1131 matches))1133 (if ((set (vals binds)) v)1134 ;; bindings are not unique1135 (recur1136 next-index1137 ptn1138 {}1139 (inc next-index)1140 next-index1141 matches)1143 ;; bindings are unique1144 (recur1145 (inc index)1146 (rest to-match)1147 (assoc binds k v)1149 next-index1150 match-start1151 matches)))1153 :else ;; k is just a number1154 (if (= k v)1155 (recur1156 (inc index)1157 (rest to-match)1158 binds1160 next-index1161 match-start1162 matches)1164 (recur1165 next-index1166 ptn1167 {}1168 (inc next-index)1169 next-index1170 matches)))))))1180 (defn search-pattern* [ptn coll]1181 (loop1182 [1183 binds {}1184 index 01185 index-next 11186 start-match 01187 to-match ptn1188 matches []]1190 (cond1191 (>= index (count coll)) matches1192 (empty? to-match)1193 (recur1194 {}1195 index-next1196 (inc index-next)1197 index-next1198 ptn1199 (conj matches1200 [(hex start-match) binds]))1202 :else1203 (let [k (first to-match)1204 v (nth coll index)]1205 (cond1206 (= k \_) ;; wildcard1207 (recur1208 binds1209 (inc index)1210 index-next1211 start-match1212 (rest to-match)1213 matches)1215 (keyword? k)1216 (if (binds k)1217 (if (= (binds k) v)1218 (recur1219 binds1220 (inc index)1221 index-next1222 start-match1223 (rest to-match)1224 matches)1225 (recur1226 {}1227 index-next1228 (inc index-next)1229 index-next1230 ptn1231 matches))1232 (if1233 ;; every symbol must be bound to a different thing.1234 ((set (vals binds)) v)1235 (recur1236 {}1237 index-next1238 (inc index-next)1239 index-next1240 ptn1241 matches)1242 (recur1243 (assoc binds k v)1244 (inc index)1245 index-next1246 start-match1247 (rest to-match)1248 matches))))))))1253 ;; look for the rainbow badge in memory1254 (println (reduce str (map #(str (first %) "\t" (vec(second %)) "\n") (search-memory (rom) [221] 10))))1257 (comment1259 (def hxc-later1260 "Running this code produces, e.g. hardcoded names NPCs give1261 their pokemon. Will sort through it later."1262 (print (character-codes->str(take 100001263 (drop 0x715971264 (rom (root)))))))1266 (let [dex1267 (partition-by #(= 0x50 %)1268 (take 25401269 (drop 0x406871270 (rom (root)))))]1271 (def dex dex)1272 (def hxc-species1273 (map character-codes->str1274 (take-nth 4 dex))))1275 )