comparison clojure/com/aurellem/gb/hxc.clj @ 350:5e816fa56393

merge dylan's changes.
author Robert McIntyre <rlm@mit.edu>
date Sun, 08 Apr 2012 20:59:06 -0500
parents 497ca041f5af
children 5aabbe326eb0
comparison
equal deleted inserted replaced
345:3b3cd62b6106 350:5e816fa56393
1
1 (ns com.aurellem.gb.hxc 2 (ns com.aurellem.gb.hxc
2 (:use (com.aurellem.gb assembly characters gb-driver util mem-util 3 (:use (com.aurellem.gb assembly characters gb-driver util mem-util
3 constants species)) 4 constants species))
4 (:import [com.aurellem.gb.gb_driver SaveState])) 5 (:import [com.aurellem.gb.gb_driver SaveState]))
5 6
6
7
8
9 ; ************* HANDWRITTEN CONSTANTS 7 ; ************* HANDWRITTEN CONSTANTS
10
11
12 8
13 (def pkmn-types 9 (def pkmn-types
14 [:normal ;;0 10 [:normal ;;0
15 :fighting ;;1 11 :fighting ;;1
16 :flying ;;2 12 :flying ;;2
136 "(leech seed)" 132 "(leech seed)"
137 "does nothing (splash)" 133 "does nothing (splash)"
138 "(disable)" 134 "(disable)"
139 ]) 135 ])
140 136
141
142 ;; ************** HARDCODED DATA 137 ;; ************** HARDCODED DATA
143 138
144 (defn hxc-thunk 139 (defn hxc-thunk
145 "Creates a thunk (nullary fn) that grabs data in a certain region of rom and 140 "Creates a thunk (nullary fn) that grabs data in a certain region of rom and
146 splits it into a collection by 0x50. If rom is not supplied, uses the 141 splits it into a collection by 0x50. If rom is not supplied, uses the
160 returning a collection of strings." 155 returning a collection of strings."
161 (comp 156 (comp
162 (partial comp (partial map character-codes->str)) 157 (partial comp (partial map character-codes->str))
163 hxc-thunk)) 158 hxc-thunk))
164 159
165
166 ;; -------------------------------------------------- 160 ;; --------------------------------------------------
167
168 161
169 162
170 (defn hxc-pokenames-raw 163 (defn hxc-pokenames-raw
171 "The hardcoded names of the 190 species in memory. List begins at 164 "The hardcoded names of the 190 species in memory. List begins at
172 ROM@E8000. Although names in memory are padded with 0x50 to be 10 characters 165 ROM@E8000. Although names in memory are padded with 0x50 to be 10 characters
203 (take (count names) 196 (take (count names)
204 (drop 0x410b1 rom)) 197 (drop 0x410b1 rom))
205 198
206 names))))) 199 names)))))
207 200
201 (def hxc-types
202 "The hardcoded type names in memory. List begins at ROM@27D99,
203 shortly before hxc-titles."
204 (hxc-thunk-words 0x27D99 102))
208 205
209 206
210 ;; http://hax.iimarck.us/topic/581/ 207 ;; http://hax.iimarck.us/topic/581/
211 (defn hxc-cry 208 (defn hxc-cry
212 "The pokemon cry data in internal order. List begins at ROM@39462" 209 "The pokemon cry data in internal order. List begins at ROM@39462"
249 (vec(rom)) 246 (vec(rom))
250 0x3965D 247 0x3965D
251 (map second 248 (map second
252 ((hxc-cry) pkmn))))) 249 ((hxc-cry) pkmn)))))
253 250
251
252
253
254 (def hxc-items-raw 254 (def hxc-items-raw
255 "The hardcoded names of the items in memory. List begins at 255 "The hardcoded names of the items in memory. List begins at
256 ROM@045B7" 256 ROM@045B7"
257 (hxc-thunk-words 0x45B7 870)) 257 (hxc-thunk-words 0x45B7 870))
258 258
259 (def hxc-types 259 (def hxc-items
260 "The hardcoded type names in memory. List begins at ROM@27D99, 260 "The hardcoded names of the items in memory, presented as
261 shortly before hxc-titles." 261 keywords. List begins at ROM@045B7. See also, hxc-items-raw."
262 (hxc-thunk-words 0x27D99 102)) 262 (comp (partial map format-name) hxc-items-raw))
263
264
263 265
264 (def hxc-titles 266 (def hxc-titles
265 "The hardcoded names of the trainer titles in memory. List begins at 267 "The hardcoded names of the trainer titles in memory. List begins at
266 ROM@27E77" 268 ROM@27E77"
267 (hxc-thunk-words 0x27E77 196)) 269 (hxc-thunk-words 0x27E77 196))
272 ROM@B8000, shortly before move names." 274 ROM@B8000, shortly before move names."
273 (hxc-thunk-words 0xB8000 14754)) 275 (hxc-thunk-words 0xB8000 14754))
274 276
275 277
276 278
277 (def hxc-items
278 "The hardcoded names of the items in memory, presented as
279 keywords. List begins at ROM@045B7. See also, hxc-items-raw."
280 (comp (partial map format-name) hxc-items-raw))
281 279
282 (defn hxc-pokedex-text 280 (defn hxc-pokedex-text
283 "The hardcoded pokedex entries in memory, presented as an 281 "The hardcoded pokedex entries in memory, presented as an
284 associative hash map. List begins at ROM@B8000." 282 associative hash map. List begins at ROM@B8000."
285 ([] (hxc-pokedex-text com.aurellem.gb.gb-driver/original-rom)) 283 ([] (hxc-pokedex-text com.aurellem.gb.gb-driver/original-rom))
289 (cons nil ;; for missingno. 287 (cons nil ;; for missingno.
290 (hxc-pokedex-text-raw rom))))) 288 (hxc-pokedex-text-raw rom)))))
291 289
292 ;; In red/blue, pokedex stats are in internal order. 290 ;; In red/blue, pokedex stats are in internal order.
293 ;; In yellow, pokedex stats are in pokedex order. 291 ;; In yellow, pokedex stats are in pokedex order.
294
295 (defn hxc-pokedex-stats 292 (defn hxc-pokedex-stats
296 "The hardcoded pokedex stats (species height weight) in memory. List 293 "The hardcoded pokedex stats (species height weight) in memory. List
297 begins at ROM@40687" 294 begins at ROM@40687"
298 ([] (hxc-pokedex-stats com.aurellem.gb.gb-driver/original-rom)) 295 ([] (hxc-pokedex-stats com.aurellem.gb.gb-driver/original-rom))
299 ([rom] 296 ([rom]
349 (drop 0x40687 rom))) )) 346 (drop 0x40687 rom))) ))
350 347
351 348
352 349
353 350
354
355
356
357 (def hxc-places 351 (def hxc-places
358 "The hardcoded place names in memory. List begins at 352 "The hardcoded place names in memory. List begins at
359 ROM@71500. [Cinnabar] Mansion seems to be dynamically calculated." 353 ROM@71500. [Cinnabar] Mansion seems to be dynamically calculated."
360 (hxc-thunk-words 0x71500 560)) 354 (hxc-thunk-words 0x71500 560))
361 355
374 368
375 369
376 (def hxc-move-names 370 (def hxc-move-names
377 "The hardcoded move names in memory. List begins at ROM@BC000" 371 "The hardcoded move names in memory. List begins at ROM@BC000"
378 (hxc-thunk-words 0xBC000 1551)) 372 (hxc-thunk-words 0xBC000 1551))
379
380
381 (defn hxc-move-data 373 (defn hxc-move-data
382 "The hardcoded (basic (move effects)) in memory. List begins at 374 "The hardcoded (basic (move effects)) in memory. List begins at
383 0x38000. Returns a map of {:name :power :accuracy :pp :fx-id 375 0x38000. Returns a map of {:name :power :accuracy :pp :fx-id
384 :fx-txt}. The move descriptions are handwritten, not hardcoded." 376 :fx-txt}. The move descriptions are handwritten, not hardcoded."
385 ([] 377 ([]
458 (take 100 450 (take 100
459 (drop 0x1232D rom)))))))) 451 (drop 0x1232D rom))))))))
460 452
461 453
462 454
455
463 (defn internal-id 456 (defn internal-id
464 ([rom] 457 ([rom]
465 (zipmap 458 (zipmap
466 (hxc-pokenames rom) 459 (hxc-pokenames rom)
467 (range))) 460 (range)))
488 ;; ) 481 ;; )
489 482
490 483
491 484
492 485
493 (defn hxc-advantage
494 ;; in-game multipliers are stored as 10x their effective value
495 ;; to allow for fractional multipliers like 1/2
496
497 "The hardcoded type advantages in memory, returned as tuples of
498 atk-type def-type multiplier. By default (i.e. if not listed here),
499 the multiplier is 1. List begins at 0x3E62D."
500 ([] (hxc-advantage com.aurellem.gb.gb-driver/original-rom))
501 ([rom]
502 (map
503 (fn [[atk def mult]] [(get pkmn-types atk (hex atk))
504 (get pkmn-types def (hex def))
505 (/ mult 10)])
506 (partition 3
507 (take-while (partial not= 0xFF)
508 (drop 0x3E62D rom))))))
509 486
510 487
511 488
512 (defn format-evo 489 (defn format-evo
490 "Parse a sequence of evolution data, returning a map. First is the
491 method: 0 = end-evolution-data. 1 = level-up, 2 = item, 3 = trade. Next is an item id, if the
492 method of evolution is by item (only stones will actually make pokemon
493 evolve, for some auxillary reason.) Finally, the minimum level for
494 evolution to occur (level 1 means no limit, which is used for trade
495 and item evolutions), followed by the internal id of the pokemon
496 into which to evolve. Hence, level up and trade evolutions are
497 described with 3
498 bytes; item evolutions with four."
513 [coll] 499 [coll]
514 (let [method (first coll)] 500 (let [method (first coll)]
515 (cond (empty? coll) [] 501 (cond (empty? coll) []
516 (= 0 method) [] ;; just in case 502 (= 0 method) [] ;; just in case
517 (= 1 method) ;; level-up evolution 503 (= 1 method) ;; level-up evolution
553 539
554 540
555 (defn hxc-learnsets 541 (defn hxc-learnsets
556 "Hardcoded map associating pokemon names to lists of pairs [lvl 542 "Hardcoded map associating pokemon names to lists of pairs [lvl
557 move] of abilities they learn as they level up. The data 543 move] of abilities they learn as they level up. The data
558 exists at ROM@3400, sorted by internal order. Pointers to the data 544 exists at ROM@34000, sorted by internal order. Pointers to the data
559 exist at ROM@3B1E5; see also, hxc-ptrs-evolve" 545 exist at ROM@3B1E5; see also, hxc-ptrs-evolve"
560 ([] (hxc-learnsets com.aurellem.gb.gb-driver/original-rom)) 546 ([] (hxc-learnsets com.aurellem.gb.gb-driver/original-rom))
561 ([rom] 547 ([rom]
562 (apply assoc 548 (apply assoc
563 {} 549 {}
586 [pkmn (map (fn [[lvl mv]] [lvl (moves mv)]) 572 [pkmn (map (fn [[lvl mv]] [lvl (moves mv)])
587 learnset)]) 573 learnset)])
588 (hxc-learnsets rom)))))) 574 (hxc-learnsets rom))))))
589 575
590 576
591
592
593 (defn hxc-evolution
594 "Hardcoded evolution data in memory. The data exists at ROM@34000,
595 sorted by internal order. Pointers to the data exist at ROM@3B1E5; see also, hxc-ptrs-evolve."
596 ([] (hxc-evolution com.aurellem.gb.gb-driver/original-rom))
597 ([rom]
598 (apply assoc {}
599 (interleave
600 (hxc-pokenames rom)
601 (map
602 (comp
603 format-evo
604 (partial take-while (comp not zero?))
605 #(drop % rom))
606 (hxc-ptrs-evolve rom)
607 )))))
608
609 (defn hxc-evolution-pretty
610 "Like hxc-evolution, except it uses the names of items and pokemon
611 --- grabbed from ROM --- rather than their numerical identifiers."
612 ([] (hxc-evolution-pretty com.aurellem.gb.gb-driver/original-rom))
613 ([rom]
614 (let
615 [poke-names (vec (hxc-pokenames rom))
616 item-names (vec (hxc-items rom))
617 use-names
618 (fn [m]
619 (loop [ks (keys m) new-map m]
620 (let [k (first ks)]
621 (cond (nil? ks) new-map
622 (= k :into)
623 (recur
624 (next ks)
625 (assoc new-map
626 :into
627 (poke-names
628 (:into
629 new-map))))
630 (= k :item)
631 (recur
632 (next ks)
633 (assoc new-map
634 :item
635 (item-names
636 (:item new-map))))
637 :else
638 (recur
639 (next ks)
640 new-map)
641 ))))]
642
643 (into {}
644 (map (fn [[pkmn evo-coll]]
645 [pkmn (map use-names evo-coll)])
646 (hxc-evolution rom))))))
647
648 577
649 (defn hxc-pokemon-base 578 (defn hxc-pokemon-base
650 ([] (hxc-pokemon-base com.aurellem.gb.gb-driver/original-rom)) 579 ([] (hxc-pokemon-base com.aurellem.gb.gb-driver/original-rom))
651 ([rom] 580 ([rom]
652 (let [entry-size 28 581 (let [entry-size 28
743 672
744 (partition entry-size 673 (partition entry-size
745 (take (* entry-size pkmn-count) 674 (take (* entry-size pkmn-count)
746 (drop 0x383DE 675 (drop 0x383DE
747 rom)))))))) 676 rom))))))))
677
748 678
749 679
750 (defn hxc-intro-pkmn 680 (defn hxc-intro-pkmn
751 "The hardcoded pokemon to display in Prof. Oak's introduction; the pokemon's 681 "The hardcoded pokemon to display in Prof. Oak's introduction; the pokemon's
752 internal id is stored at ROM@5EDB." 682 internal id is stored at ROM@5EDB."
818 ))) 748 )))
819 749
820 750
821 751
822 752
823
824 (defn hxc-ptrs-wild 753 (defn hxc-ptrs-wild
825 "A list of the hardcoded wild encounter data in memory. Pointers 754 "A list of the hardcoded wild encounter data in memory. Pointers
826 begin at ROM@0CB95; data begins at ROM@0x04D89" 755 begin at ROM@0CB95; data begins at ROM@0x04D89"
827 ([] (hxc-ptrs-wild com.aurellem.gb.gb-driver/original-rom)) 756 ([] (hxc-ptrs-wild com.aurellem.gb.gb-driver/original-rom))
828 ([rom] 757 ([rom]
852 781
853 )))))) 782 ))))))
854 783
855 784
856 785
857
858
859
860
861
862
863
864
865
866
867
868 ;; ********************** MANIPULATION FNS 786 ;; ********************** MANIPULATION FNS
869 787
870 788
871 (defn same-type 789 (defn same-type
872 ([pkmn move] 790 ([pkmn move]
923 841
924 ;; note: 0x2f31 contains the names "TM" "HM"? 842 ;; note: 0x2f31 contains the names "TM" "HM"?
925 843
926 ;; note for later: credits start at F1290 844 ;; note for later: credits start at F1290
927 845
846 ;; note: DADB hyper-potion-hp _ _ _ super-potion-hp _ _ _ potion-hp ??
847
848 ;; note: DD4D spells out pokemon vital stat names ("speed", etc.)
849
850 ;; note: 1195C-6A says ABLE#NOT ABLE#, but so does 119C0-119CE.
851 ;; The first instance is for Machines; the second, for stones.
852
853 ;; 0x251A (in indexable mem): image decompression routine seems to begin here.
928 854
929 855
930 (comment 856 (comment
931 857
932 (def hxc-later 858 (def hxc-later
944 (def dex dex) 870 (def dex dex)
945 (def hxc-species 871 (def hxc-species
946 (map character-codes->str 872 (map character-codes->str
947 (take-nth 4 dex)))) 873 (take-nth 4 dex))))
948 ) 874 )
949
950
951
952
953
954