view clojure/com/aurellem/gb/hxc.clj @ 289:df9cad9909d2

Fixed missing delimiter in gb.moves; preparing to play with type effectiveness.
author Dylan Holmes <ocsenave@gmail.com>
date Thu, 29 Mar 2012 13:49:40 -0500
parents eec3e69500d9
children c31cb3043087
line wrap: on
line source
1 (ns com.aurellem.gb.hxc
2 (:use (com.aurellem.gb assembly characters gb-driver util
3 constants species))
4 ;; (:use (com.aurellem.world practice))
5 (:import [com.aurellem.gb.gb_driver SaveState]))
10 ; ************* HANDWRITTEN CONSTANTS
14 (def pkmn-types
15 [:normal ;;0
16 :fighting ;;1
17 :flying ;;2
18 :poison ;;3
19 :ground ;;4
20 :rock ;;5
21 :bird ;;6
22 :bug ;;7
23 :ghost ;;8
24 :A
25 :B
26 :C
27 :D
28 :E
29 :F
30 :G
31 :H
32 :I
33 :J
34 :K
35 :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 take
46 ;; their accuracy from the move accuracy, does
47 ;; this mean that the move always "hits" but the
48 ;; status effect may not?
50 (def move-effects
51 ["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 acr
101 "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 difference
108 ;; 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 acr
119 "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 0x19
124 "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. some
133 exceptions apply."
134 "(rage)"
135 "(mimic)"
136 "(metronome)"
137 "(leech seed)"
138 "does nothing (splash)"
139 "(disable)"
140 ])
143 ;; ************** HARDCODED DATA
145 (defn hxc-thunk
146 "Creates a thunk (nullary fn) that grabs data in a certain region of rom and
147 splits it into a collection by 0x50. If rom is not supplied, uses the
148 original rom data."
149 [start length]
150 (fn self
151 ([rom]
152 (take-nth 2
153 (partition-by #(= % 0x50)
154 (take length
155 (drop start rom)))))
156 ([]
157 (self com.aurellem.gb.gb-driver/original-rom))))
159 (def hxc-thunk-words
160 "Same as hxc-thunk, except it interprets the rom data as characters,
161 returning a collection of strings."
162 (comp
163 (partial comp (partial map character-codes->str))
164 hxc-thunk))
167 ;; --------------------------------------------------
171 (defn hxc-pokenames-raw
172 "The hardcoded names of the 190 species in memory. List begins at
173 ROM@E8000. Although names in memory are padded with 0x50 to be 10 characters
174 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 190
179 name-length 10]
180 (map character-codes->str
181 (partition name-length
182 (map #(if (= 0x50 %) 0x00 %)
183 (take (* count-species name-length)
184 (drop 0xE8000
185 rom))))))))
186 (def hxc-pokenames
187 (comp
188 (partial map format-name)
189 hxc-pokenames-raw))
194 (defn hxc-pokedex-names
195 "The names of the pokemon in hardcoded pokedex order. List begins at
196 ROM@410B1. See also, hxc-pokenames."
197 ([] (hxc-pokedex-names
198 com.aurellem.gb.gb-driver/original-rom))
199 ([rom]
200 (let [names (hxc-pokenames rom)]
201 (#(mapv %
202 ((comp range count keys) %))
203 (zipmap
204 (take (count names)
205 (drop 0x410b1 rom))
207 names)))))
212 (def hxc-items-raw
213 "The hardcoded names of the items in memory. List begins at
214 ROM@045B7"
215 (hxc-thunk-words 0x45B7 870))
217 (def hxc-types
218 "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-titles
223 "The hardcoded names of the trainer titles in memory. List begins at
224 ROM@27E77"
225 (hxc-thunk-words 0x27E77 196))
228 (def hxc-pokedex-text-raw
229 "The hardcoded pokedex entries in memory. List begins at
230 ROM@B8000, shortly before move names."
231 (hxc-thunk-words 0xB8000 14754))
235 (def hxc-items
236 "The hardcoded names of the items in memory, presented as
237 keywords. List begins at ROM@045B7. See also, hxc-items-raw."
238 (comp (partial map format-name) hxc-items-raw))
240 (defn hxc-pokedex-text
241 "The hardcoded pokedex entries in memory, presented as an
242 associative hash map. List begins at ROM@B8000."
243 ([] (hxc-pokedex-text com.aurellem.gb.gb-driver/original-rom))
244 ([rom]
245 (zipmap
246 (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-stats
254 "The hardcoded pokedex stats (species height weight) in memory. List
255 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-stats
262 [n stats data]
263 (if (zero? n) stats
264 (let [[species
265 [_
266 height-ft
267 height-in
268 weight-1
269 weight-2
270 _
271 dex-ptr-1
272 dex-ptr-2
273 dex-bank
274 _
275 & data]]
276 (split-with (partial not= 0x50) data)]
277 (recur (dec n)
278 (assoc stats
279 (pokedex-names (- pkmn-count (dec n)))
280 {:species
281 (format-name (character-codes->str species))
282 :height-ft
283 height-ft
284 :height-in
285 height-in
286 :weight
287 (/ (low-high weight-1 weight-2) 10.)
289 ;; :text
290 ;; (character-codes->str
291 ;; (take-while
292 ;; (partial not= 0x50)
293 ;; (drop
294 ;; (+ 0xB8000
295 ;; -0x4000
296 ;; (low-high dex-ptr-1 dex-ptr-2))
297 ;; rom)))
298 })
300 data)
303 )))
305 pkmn-count
306 {}
307 (drop 0x40687 rom))) ))
315 (def hxc-places
316 "The hardcoded place names in memory. List begins at
317 ROM@71500. [Cinnabar] Mansion seems to be dynamically calculated."
318 (hxc-thunk-words 0x71500 560))
321 (defn hxc-dialog
322 "The hardcoded dialogue in memory, including in-game alerts. Dialog
323 seems to be separated by 0x57 instead of 0x50 (END). Begins at ROM@98000."
324 ([rom]
325 (map character-codes->str
326 (take-nth 2
327 (partition-by #(= % 0x57)
328 (take 0x0F728
329 (drop 0x98000 rom))))))
330 ([]
331 (hxc-dialog com.aurellem.gb.gb-driver/original-rom)))
334 (def hxc-move-names
335 "The hardcoded move names in memory. List begins at ROM@BC000"
336 (hxc-thunk-words 0xBC000 1551))
339 (defn hxc-move-data
340 "The hardcoded (basic (move effects)) in memory. List begins at
341 0x38000. Returns a map of {:name :power :accuracy :pp :fx-id
342 :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 6
349 types pkmn-types ;;; !! hardcoded types
350 ]
351 (zipmap (map format-name names)
352 (map
353 (fn [[idx effect power type-id accuracy pp]]
354 {:name (names (dec idx))
355 :power power
356 :accuracy accuracy
357 :pp pp
358 :type (types type-id)
359 :fx-id effect
360 :fx-txt (get move-effects effect)
361 }
362 )
364 (partition move-size
365 (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 6
378 format-name (fn [s]
379 (keyword (.toLowerCase
380 (apply str
381 (map #(if (= % \space) "-" %) s)))))
382 ]
383 (zipmap (map format-name names)
384 (map
385 (fn [[idx effect power type accuracy pp]]
386 {:name (names (dec idx))
387 :power power
388 :accuracy (hex accuracy)
389 :pp pp
390 :fx-id (hex effect)
391 :fx-txt (get move-effects effect)
392 }
393 )
395 (partition move-size
396 (take (* move-size move-count)
397 (drop 0x38000 rom))))))))
400 (defn hxc-machines
401 "The hardcoded moves taught by TMs and HMs. List begins at ROM@0x1232D."
402 ([] (hxc-machines
403 com.aurellem.gb.gb-driver/original-rom))
404 ([rom]
405 (let [moves (hxc-move-names rom)]
406 (zipmap
407 (range)
408 (take-while
409 (comp not nil?)
410 (map (comp
411 format-name
412 (zipmap
413 (range)
414 moves)
415 dec)
416 (take 100
417 (drop 0x1232D rom))))))))
421 (defn internal-id
422 ([rom]
423 (zipmap
424 (hxc-pokenames rom)
425 (range)))
426 ([]
427 (internal-id com.aurellem.gb.gb-driver/original-rom)))
433 ;; nidoran gender change upon levelup
434 ;; (->
435 ;; @current-state
436 ;; rom
437 ;; vec
438 ;; (rewrite-memory
439 ;; (nth (hxc-ptrs-evolve) ((internal-id) :nidoran♂))
440 ;; [1 1 15])
441 ;; (rewrite-memory
442 ;; (nth (hxc-ptrs-evolve) ((internal-id) :nidoran♀))
443 ;; [1 1 3])
444 ;; (write-rom!)
446 ;; )
451 (defn hxc-advantage
452 "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 (map
457 (fn [[atk def mult]] [(get pkmn-types atk (hex atk))
458 (get pkmn-types def (hex def))
459 (/ mult 10)])
460 (partition 3
461 (take-while (partial not= 0xFF)
462 (drop 0x3E62D rom))))))
466 (defn format-evo
467 [coll]
468 (let [method (first coll)]
469 (cond (empty? coll) []
470 (= 0 method) [] ;; just in case
471 (= 1 method) ;; level-up evolution
472 (conj (format-evo (drop 3 coll))
473 {:method :level-up
474 :min-level (nth coll 1)
475 :into (dec (nth coll 2))})
477 (= 2 method) ;; item evolution
478 (conj (format-evo (drop 4 coll))
479 {:method :item
480 :item (dec (nth coll 1))
481 :min-level (nth coll 2)
482 :into (dec (nth coll 3))})
484 (= 3 method) ;; trade evolution
485 (conj (format-evo (drop 3 coll))
486 {:method :trade
487 :min-level (nth coll 1) ;; always 1 for trade.
488 :into (dec (nth coll 2))}))))
491 (defn hxc-ptrs-evolve
492 "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)) ;; 190
499 ptrs
500 (map (fn [[a b]] (low-high a b))
501 (partition 2
502 (take (* 2 pkmn-count)
503 (drop 0x3b1e5 rom))))]
504 (map (partial + 0x34000) ptrs)
506 )))
509 (defn hxc-learnsets
510 "Hardcoded map associating pokemon names to lists of pairs [lvl
511 move] of abilities they learn as they level up. The data
512 exists at ROM@3400, sorted by internal order. Pointers to the data
513 exist at ROM@3B1E5; see also, hxc-ptrs-evolve"
514 ([] (hxc-learnsets com.aurellem.gb.gb-driver/original-rom))
515 ([rom]
516 (apply assoc
517 {}
518 (interleave
519 (hxc-pokenames rom)
520 (map (comp
521 (partial map
522 (fn [[lvl mv]] [lvl (dec mv)]))
523 (partial partition 2)
524 ;; keep the learnset data
525 (partial take-while (comp not zero?))
526 ;; skip the evolution data
527 rest
528 (partial drop-while (comp not zero?)))
529 (map #(drop % rom)
530 (hxc-ptrs-evolve rom)))))))
532 (defn hxc-learnsets-pretty
533 "Live hxc-learnsets except it reports the name of each move --- as
534 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-evolution
548 "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 (interleave
554 (hxc-pokenames rom)
555 (map
556 (comp
557 format-evo
558 (partial take-while (comp not zero?))
559 #(drop % rom))
560 (hxc-ptrs-evolve rom)
561 )))))
563 (defn hxc-evolution-pretty
564 "Like hxc-evolution, except it uses the names of items and pokemon
565 --- grabbed from ROM --- rather than their numerical identifiers."
566 ([] (hxc-evolution-pretty com.aurellem.gb.gb-driver/original-rom))
567 ([rom]
568 (let
569 [poke-names (vec (hxc-pokenames rom))
570 item-names (vec (hxc-items rom))
571 use-names
572 (fn [m]
573 (loop [ks (keys m) new-map m]
574 (let [k (first ks)]
575 (cond (nil? ks) new-map
576 (= k :into)
577 (recur
578 (next ks)
579 (assoc new-map
580 :into
581 (poke-names
582 (:into
583 new-map))))
584 (= k :item)
585 (recur
586 (next ks)
587 (assoc new-map
588 :item
589 (item-names
590 (:item new-map))))
591 :else
592 (recur
593 (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-base
604 ([] (hxc-pokemon-base com.aurellem.gb.gb-driver/original-rom))
605 ([rom]
606 (let [entry-size 28
607 pkmn-count (count (hxc-pokedex-text rom))
608 pokemon (rest (hxc-pokedex-names))
609 types (apply assoc {}
610 (interleave
611 (range)
612 pkmn-types)) ;;!! softcoded
613 moves (apply assoc {}
614 (interleave
615 (range)
616 (map format-name
617 (hxc-move-names rom))))
618 machines (hxc-machines)
619 ]
620 (zipmap
621 pokemon
622 (map
623 (fn [[n
624 rating-hp
625 rating-atk
626 rating-def
627 rating-speed
628 rating-special
629 type-1
630 type-2
631 rarity
632 rating-xp
633 pic-dimensions ;; tile_width|tile_height (8px/tile)
634 ptr-pic-obverse-1
635 ptr-pic-obverse-2
636 ptr-pic-reverse-1
637 ptr-pic-reverse-2
638 move-1
639 move-2
640 move-3
641 move-4
642 growth-rate
643 &
644 TMs|HMs]]
645 (let
646 [base-moves
647 (mapv moves
648 ((comp
649 ;; 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 types
657 (set (list (types type-1)
658 (types type-2)))
659 TMs|HMs
660 (map
661 (comp
662 (partial map first)
663 (partial remove (comp zero? second)))
664 (split-at
665 50
666 (map vector
667 (rest(range))
668 (reduce concat
669 (map
670 #(take 8
671 (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# n
684 :base-moves base-moves
685 :types types
686 :TMs TMs
687 :HMs HMs
688 :base-hp rating-hp
689 :base-atk rating-atk
690 :base-def rating-def
691 :base-speed rating-speed
692 :base-special rating-special
693 }))
695 (partition entry-size
696 (take (* entry-size pkmn-count)
697 (drop 0x383DE
698 rom))))))))
702 (defn hxc-item-prices
703 "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 items
709 (map (comp
710 ;; zero-cost items are "priceless"
711 #(if (zero? %) :priceless %)
712 decode-bcd butlast)
713 (partition price-size
714 (take (* price-size (count items))
715 (drop 0x4495 rom))))))))
717 (defn hxc-shops
718 ([] (hxc-shops com.aurellem.gb.gb-driver/original-rom))
719 ([rom]
720 (let [items (zipmap (range) (hxc-items rom))
722 ;; temporarily softcode the TM items
723 items (into
724 items
725 (map (juxt identity
726 (comp keyword
727 (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-while
736 (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 FNS
756 (defn same-type
757 ([pkmn move]
758 (same-type
759 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) true
771 (and
772 (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-vals
785 "Returns a map consisting of all the pairs [key val] for
786 which (pred key) returns true."
787 [pred map]
788 (reduce (partial apply assoc) {}
789 (filter (fn [[k v]] (pred v)) map)))
792 (defn search-moves
793 "Returns a subcollection of all hardcoded moves with the
794 given attributes. Attributes consist of :name :power
795 :accuracy :pp :fx-id
796 (and also :fx-txt, but it contains the same information
797 as :fx-id)"
798 ([attribute-map]
799 (search-moves
800 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 F1290
815 (comment
817 (def hxc-later
818 "Running this code produces, e.g. hardcoded names NPCs give
819 their pokemon. Will sort through it later."
820 (print (character-codes->str(take 10000
821 (drop 0x71597
822 (rom (root)))))))
824 (let [dex
825 (partition-by #(= 0x50 %)
826 (take 2540
827 (drop 0x40687
828 (rom (root)))))]
829 (def dex dex)
830 (def hxc-species
831 (map character-codes->str
832 (take-nth 4 dex))))
833 )