view clojure/com/aurellem/gb/hxc.clj @ 281:ca1afcef3542

Replaced manually listed move data in gb.moves with a call to hxc-move-data. Also, began work on hxc-shops, which lists shop inventory. todo: find out how the game converts internal order to pokedex order; todo: get hardcoded tm data.
author Dylan Holmes <ocsenave@gmail.com>
date Tue, 27 Mar 2012 22:41:24 -0500
parents 69184558fcf3
children 0c3fbb313e49
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 (defn low-high
15 [low high]
16 (+ low (* 256 high)))
19 (defn format-name
20 "Convert the string of alphabetic/space characters into a keyword by
21 replacing spaces with hyphens and converting to lowercase."
22 [s]
23 (keyword (.toLowerCase
24 (apply str
25 (map #(if (= % \space) "-" %) s)))))
29 (def pkmn-types
30 [:normal ;;0
31 :fighting ;;1
32 :flying ;;2
33 :poison ;;3
34 :ground ;;4
35 :rock ;;5
36 :bird ;;6
37 :bug ;;7
38 :ghost ;;8
39 :A
40 :B
41 :C
42 :D
43 :E
44 :F
45 :G
46 :H
47 :I
48 :J
49 :K
50 :fire ;;20 (0x14)
51 :water ;;21 (0x15)
52 :grass ;;22 (0x16)
53 :electric ;;23 (0x17)
54 :psychic ;;24 (0x18)
55 :ice ;;25 (0x19)
56 :dragon ;;26 (0x1A)
57 ])
60 ;; question: when status effects claim to take
61 ;; their accuracy from the move accuracy, does
62 ;; this mean that the move always "hits" but the
63 ;; status effect may not?
65 (def move-effects
66 ["normal damage"
67 "no damage, just opponent sleep" ;; how many turns? is atk power ignored?
68 "0x4C chance of poison"
69 "leech half of inflicted damage"
70 "0x19 chance of burn"
71 "0x19 chance of freeze"
72 "0x19 chance of paralyze"
73 "user faints; opponent defense halved during attack."
74 "leech half of inflicted damage ONLY if sleeping opponent."
75 "imitate last attack"
76 "user atk +1"
77 "user def +1"
78 "user spd +1"
79 "user spc +1"
80 "user acr +1" ;; unused?!
81 "user evd +1"
82 "get post-battle $ = 2*level*uses"
83 "0xFE acr, no matter what."
84 "opponent atk -1" ;; acr taken from move acr?
85 "opponent def -1" ;;
86 "opponent spd -1" ;;
87 "opponent spc -1" ;;
88 "opponent acr -1";;
89 "opponent evd -1"
90 "converts user's type to opponent's."
91 "(haze)"
92 "(bide)"
93 "(thrash)"
94 "(teleport)"
95 "(fury swipes)"
96 "attacks 2-5 turns" ;; unused? like rollout?
97 "0x19 chance of flinch"
98 "opponent sleep for 1-7 turns"
99 "0x66 chance of poison"
100 "0x4D chance of burn"
101 "0x4D chance of freeze"
102 "0x4D chance of paralyze"
103 "0x4D chance of flinch"
104 "one-hit KO"
105 "charge one turn, atk next."
106 "fixed damage, leaves 1HP." ;; how is dmg determined?
107 "fixed damage." ;; cf seismic toss, dragon rage, psywave.
108 "atk 2-5 turns; opponent can't attack" ;; unnormalized? (0 0x60 0x60 0x20 0x20)
109 "charge one turn, atk next. (can't be hit when charging)"
110 "atk hits twice."
111 "user takes 1 damage if misses."
112 "evade status-lowering effects" ;;caused by you or also your opponent?
113 "(broken) if user is slower than opponent, makes critical hit impossible, otherwise has no effect"
114 "atk causes recoil dmg = 1/4 dmg dealt"
115 "confuses opponent" ;; acr taken from move acr
116 "user atk +2"
117 "user def +2"
118 "user spd +2"
119 "user spc +2"
120 "user acr +2" ;; unused!
121 "user evd +2" ;; unused!
122 "restores up to half of user's max hp." ;; broken: fails if the difference
123 ;; b/w max and current hp is one less than a multiple of 256.
124 "(transform)"
125 "opponent atk -2"
126 "opponent def -2"
127 "opponent spd -2"
128 "opponent spc -2"
129 "opponent acr -2"
130 "opponent evd -2"
131 "doubles user spc when attacked"
132 "doubles user def when attacked"
133 "just poisons opponent" ;;acr taken from move acr
134 "just paralyzes opponent" ;;
135 "0x19 chance opponent atk -1"
136 "0x19 chance opponent def -1"
137 "0x19 chance opponent spd -1"
138 "0x4C chance opponent spc -1" ;; context suggest chance is 0x19
139 "0x19 chance opponent acr -1"
140 "0x19 chance opponent evd -1"
141 "???" ;; unused? no effect?
142 "???" ;; unused? no effect?
143 "0x19 chance opponent confused"
144 "atk hits twice. 0x33 chance opponent poisioned."
145 "broken. crash the game after attack."
146 "(substitute)"
147 "unless opponent faints, user must recharge after atk. some
148 exceptions apply."
149 "(rage)"
150 "(mimic)"
151 "(metronome)"
152 "(leech seed)"
153 "does nothing (splash)"
154 "(disable)"
155 ])
158 ;; ************** HARDCODED DATA
160 (defn hxc-thunk
161 "Creates a thunk (nullary fn) that grabs data in a certain region of rom and
162 splits it into a collection by 0x50. If rom is not supplied, uses the
163 original rom data."
164 [start length]
165 (fn self
166 ([rom]
167 (take-nth 2
168 (partition-by #(= % 0x50)
169 (take length
170 (drop start rom)))))
171 ([]
172 (self com.aurellem.gb.gb-driver/original-rom))))
174 (def hxc-thunk-words
175 "Same as hxc-thunk, except it interprets the rom data as characters,
176 returning a collection of strings."
177 (comp
178 (partial comp (partial map character-codes->str))
179 hxc-thunk))
182 ;; --------------------------------------------------
184 (def hxc-items
185 "The hardcoded names of the items in memory. List begins at
186 ROM@045B7"
187 (hxc-thunk-words 0x45B7 870))
189 (def hxc-types
190 "The hardcoded type names in memory. List begins at ROM@27D99,
191 shortly before hxc-titles."
192 (hxc-thunk-words 0x27D99 102))
194 (def hxc-titles
195 "The hardcoded names of the trainer titles in memory. List begins at
196 ROM@27E77"
197 (hxc-thunk-words 0x27E77 196))
200 (def hxc-pokedex-text
201 "The hardcoded pokedex entries in memory. List begins at
202 ROM@B8000, shortly before move names."
203 (hxc-thunk-words 0xB8000 14754))
206 ;; In red/blue, pokedex stats are in internal order.
207 ;; In yellow, pokedex stats are in pokedex order.
209 (defn hxc-pokedex-stats
210 "The hardcoded pokedex stats (species height weight) in memory. List
211 begins at ROM@40687"
212 ;; uses hxc-pokedex-text to count pokemon
213 ;; since hxc-pokenames includes several missingno"
214 ([] (hxc-pokedex-stats com.aurellem.gb.gb-driver/original-rom))
215 ([rom]
216 (let [poketext (hxc-pokedex-text)
217 pkmn-count (count poketext)
218 ]
219 ((fn capture-stats
220 [n stats data]
221 (if (zero? n) stats
222 (let [[species
223 [_
224 height-ft
225 height-in
226 weight-1
227 weight-2
228 _
229 dex-ptr-1
230 dex-ptr-2
231 dex-bank
232 _
233 & data]]
234 (split-with (partial not= 0x50) data)]
235 (recur (dec n)
236 (assoc stats
237 (- pkmn-count n)
238 {:species
239 (character-codes->str species)
240 :height-ft
241 height-ft
242 :height-in
243 height-in
244 :weight
245 (/ (low-high weight-1 weight-2) 10.)
247 ;; :text
248 ;; (character-codes->str
249 ;; (take-while
250 ;; (partial not= 0x50)
251 ;; (drop
252 ;; (+ 0xB8000
253 ;; -0x4000
254 ;; (low-high dex-ptr-1 dex-ptr-2))
255 ;; rom)))
256 })
258 data)
261 )))
263 pkmn-count
264 {}
265 (drop 0x40687 rom))) ))
273 (def hxc-places
274 "The hardcoded place names in memory. List begins at
275 ROM@71500. [Cinnabar] Mansion seems to be dynamically calculated."
276 (hxc-thunk-words 0x71500 560))
279 (defn hxc-dialog
280 "The hardcoded dialogue in memory, including in-game alerts. Dialog
281 seems to be separated by 0x57 instead of 0x50 (END). Begins at ROM@98000."
282 ([rom]
283 (map character-codes->str
284 (take-nth 2
285 (partition-by #(= % 0x57)
286 (take 0x0F728
287 (drop 0x98000 rom))))))
288 ([]
289 (hxc-dialog com.aurellem.gb.gb-driver/original-rom)))
294 (def hxc-move-names
295 "The hardcoded move names in memory. List begins at ROM@BC000"
296 (hxc-thunk-words 0xBC000 1551))
299 (defn hxc-move-data
300 "The hardcoded (basic (move effects)) in memory. List begins at
301 0x38000. Returns a map of {:name :power :accuracy :pp :fx-id
302 :fx-txt}. The move descriptions are handwritten, not hardcoded."
303 ([]
304 (hxc-move-data com.aurellem.gb.gb-driver/original-rom))
305 ([rom]
306 (let [names (vec (hxc-move-names rom))
307 move-count (count names)
308 move-size 6
309 types pkmn-types ;;; !! hardcoded types
310 ]
311 (zipmap (map format-name names)
312 (map
313 (fn [[idx effect power type-id accuracy pp]]
314 {:name (names (dec idx))
315 :power power
316 :accuracy accuracy
317 :pp pp
318 :type (types type-id)
319 :fx-id effect
320 :fx-txt (get move-effects effect)
321 }
322 )
324 (partition move-size
325 (take (* move-size move-count)
326 (drop 0x38000 rom))))))))
330 (defn hxc-move-data*
331 "Like hxc-move-data, but reports numbers as hexadecimal symbols instead."
332 ([]
333 (hxc-move-data* com.aurellem.gb.gb-driver/original-rom))
334 ([rom]
335 (let [names (vec (hxc-move-names rom))
336 move-count (count names)
337 move-size 6
338 format-name (fn [s]
339 (keyword (.toLowerCase
340 (apply str
341 (map #(if (= % \space) "-" %) s)))))
342 ]
343 (zipmap (map format-name names)
344 (map
345 (fn [[idx effect power type accuracy pp]]
346 {:name (names (dec idx))
347 :power power
348 :accuracy (hex accuracy)
349 :pp pp
350 :fx-id (hex effect)
351 :fx-txt (get move-effects effect)
352 }
353 )
355 (partition move-size
356 (take (* move-size move-count)
357 (drop 0x38000 rom))))))))
361 (defn hxc-pokenames
362 "The hardcoded names of the 190 species in memory. List begins at
363 ROM@E8000. Although names in memory are padded with 0x50 to be 10 characters
364 long, these names are stripped of padding."
365 ([]
366 (hxc-pokenames com.aurellem.gb.gb-driver/original-rom))
367 ([rom]
368 (let [count-species 190
369 name-length 10]
370 (map character-codes->str
371 (partition name-length
372 (map #(if (= 0x50 %) 0x00 %)
373 (take (* count-species name-length)
374 (drop 0xE8000
375 rom))))))))
380 (defn internal-id
381 ([rom]
382 (zipmap
383 (map format-name (hxc-pokenames rom))
384 (range)))
385 ([]
386 (internal-id com.aurellem.gb.gb-driver/original-rom)))
390 ;; nidoran gender change upon levelup
391 ;; (->
392 ;; @current-state
393 ;; rom
394 ;; vec
395 ;; (rewrite-memory
396 ;; (nth (hxc-ptrs-evolve) ((internal-id) :nidoran♂))
397 ;; [1 1 15])
398 ;; (rewrite-memory
399 ;; (nth (hxc-ptrs-evolve) ((internal-id) :nidoran♀))
400 ;; [1 1 3])
401 ;; (write-rom!)
403 ;; )
408 (defn hxc-advantage
409 "The hardcoded type advantages in memory, returned as tuples of atk-type def-type multiplier. By default (i.e. if not listed here),
410 the multiplier is 1."
411 ([] (hxc-advantage com.aurellem.gb.gb-driver/original-rom))
412 ([rom]
413 (map
414 (fn [[atk def mult]] [(get pkmn-types atk (hex atk))
415 (get pkmn-types def (hex def))
416 (/ mult 10)])
417 (partition 3
418 (take-while (partial not= 0xFF)
419 (drop 0x3E62D rom))))))
423 (defn format-evo
424 [coll]
425 (let [method (first coll)]
426 (cond (empty? coll) []
427 (= 0 method) [] ;; just in case
428 (= 1 method) ;; level-up evolution
429 (conj (format-evo (drop 3 coll))
430 {:method :level-up
431 :min-level (nth coll 1)
432 :into (dec (nth coll 2))})
434 (= 2 method) ;; item evolution
435 (conj (format-evo (drop 4 coll))
436 {:method :item
437 :item (dec (nth coll 1))
438 :min-level (nth coll 2)
439 :into (dec (nth coll 3))})
441 (= 3 method) ;; trade evolution
442 (conj (format-evo (drop 3 coll))
443 {:method :trade
444 :min-level (nth coll 1) ;; always 1 for trade.
445 :into (dec (nth coll 2))}))))
448 (defn hxc-ptrs-evolve
449 "A hardcoded collection of 190 pointers to alternating evolution/learnset data,
450 in internal order."
451 ([]
452 (hxc-ptrs-evolve com.aurellem.gb.gb-driver/original-rom))
453 ([rom]
454 (let [names (hxc-pokenames rom)
455 pkmn-count (count names)
456 ptrs
457 (map (fn [[a b]] (low-high a b))
458 (partition 2
459 (take (* 2 pkmn-count)
460 (drop 0x3b1e5 rom))))]
461 (map (partial + 0x34000) ptrs)
463 )))
466 (defn hxc-learnsets
467 "Hardcoded map associating pokemon names to lists of pairs [lvl
468 move] of abilities they learn as they level up. The data
469 exists at ROM@3400, sorted by internal order. Pointers to the data
470 exist at ROM@3B1E5; see also, hxc-ptrs-evolve"
471 ([] (hxc-learnsets com.aurellem.gb.gb-driver/original-rom))
472 ([rom]
473 (apply assoc
474 {}
475 (interleave
476 (map format-name (hxc-pokenames rom))
477 (map (comp
478 (partial map
479 (fn [[lvl mv]] [lvl (dec mv)]))
480 (partial partition 2)
481 ;; keep the learnset data
482 (partial take-while (comp not zero?))
483 ;; skip the evolution data
484 rest
485 (partial drop-while (comp not zero?)))
486 (map #(drop % rom)
487 (hxc-ptrs-evolve rom)))))))
489 (defn hxc-learnsets-pretty
490 "Live hxc-learnsets except it reports the name of each move --- as
491 it appears in rom --- rather than the move index."
492 ([] (hxc-learnsets-pretty com.aurellem.gb.gb-driver/original-rom))
493 ([rom]
494 (let [moves (vec(map format-name (hxc-move-names)))]
495 (into {}
496 (map (fn [[pkmn learnset]]
497 [pkmn (map (fn [[lvl mv]] [lvl (moves mv)])
498 learnset)])
499 (hxc-learnsets rom))))))
504 (defn hxc-evolution
505 "Hardcoded evolution data in memory. The data exists at ROM@34000,
506 sorted by internal order. Pointers to the data exist at ROM@3B1E5; see also, hxc-ptrs-evolve."
507 ([] (hxc-evolution com.aurellem.gb.gb-driver/original-rom))
508 ([rom]
509 (apply assoc {}
510 (interleave
511 (map format-name (hxc-pokenames rom))
512 (map
513 (comp
514 format-evo
515 (partial take-while (comp not zero?))
516 #(drop % rom))
517 (hxc-ptrs-evolve rom)
518 )))))
520 (defn hxc-evolution-pretty
521 "Like hxc-evolution, except it uses the names of items and pokemon
522 --- grabbed from ROM --- rather than their numerical identifiers."
523 ([] (hxc-evolution-pretty com.aurellem.gb.gb-driver/original-rom))
524 ([rom]
525 (let
526 [poke-names (vec (map format-name (hxc-pokenames rom)))
527 item-names (vec (map format-name (hxc-items rom)))
528 use-names
529 (fn [m]
530 (loop [ks (keys m) new-map m]
531 (let [k (first ks)]
532 (cond (nil? ks) new-map
533 (= k :into)
534 (recur
535 (next ks)
536 (assoc new-map
537 :into
538 (poke-names
539 (:into
540 new-map))))
541 (= k :item)
542 (recur
543 (next ks)
544 (assoc new-map
545 :item
546 (item-names
547 (:item new-map))))
548 :else
549 (recur
550 (next ks)
551 new-map)
552 ))))]
554 (into {}
555 (map (fn [[pkmn evo-coll]]
556 [pkmn (map use-names evo-coll)])
557 (hxc-evolution rom))))))
563 (defn hxc-pokemon-base
564 ([] (hxc-pokemon-base com.aurellem.gb.gb-driver/original-rom))
565 ([rom]
566 (let [entry-size 28
567 pkmn-count (count (hxc-pokedex-text rom))
568 types (apply assoc {}
569 (interleave
570 (range)
571 pkmn-types)) ;;!! softcoded
572 moves (apply assoc {}
573 (interleave
574 (range)
575 (map format-name
576 (hxc-move-names rom))))
577 ]
578 (map
580 (fn [[n
581 rating-hp
582 rating-atk
583 rating-def
584 rating-speed
585 rating-special
586 type-1
587 type-2
588 rarity
589 rating-xp
590 pic-dimensions
591 ptr-pic-obverse-1
592 ptr-pic-obverse-2
593 ptr-pic-reverse-1
594 ptr-pic-reverse-2
595 move-1
596 move-2
597 move-3
598 move-4
599 growth-rate
600 &
601 TMs|HMs]]
602 (let
603 [base-moves
604 (mapv moves
605 ((comp
606 ;; since the game uses zero as a delimiter,
607 ;; it must also increment all move indices by 1.
608 ;; heren we decrement to correct this.
609 (partial map dec)
610 (partial take-while (comp not zero?)))
611 [move-1 move-2 move-3 move-4]))
613 types
614 (set (list (types type-1)
615 (types type-2)))
616 TMs|HMs
617 (map
618 (comp
619 (partial map first)
620 (partial remove (comp zero? second)))
621 (split-at
622 50
623 (map vector
624 (rest(range))
625 (reduce concat
626 (map
627 #(take 8
628 (concat (bit-list %)
629 (repeat 0)))
631 TMs|HMs)))))
633 TMs (vec (first TMs|HMs))
634 HMs (take 5 (map (partial + -50) (vec (second TMs|HMs))))
637 ]
640 {:dex# n
641 :base-moves base-moves
642 :types types
643 :TMs TMs
644 :HMs HMs
645 :base-hp rating-hp
646 :base-atk rating-atk
647 :base-def rating-def
648 :base-speed rating-speed
649 :base-special rating-special
650 }))
652 (partition entry-size
653 (take (* entry-size pkmn-count)
654 (drop 0x383DE
655 rom)))))))
657 (defn hxc-shops
658 ([] (hxc-shops com.aurellem.gb.gb-driver/original-rom))
659 ([rom]
660 (let [items (zipmap (range) (map format-name (hxc-items rom)))
662 ;; temporarily softcode the TM items
663 items (into
664 items
665 (map (juxt identity
666 (comp keyword
667 (partial str "tm-")
668 (partial + 1 -200)
669 ))
670 (take 200 (drop 200 (range)))))
672 ]
674 ((fn parse-shop [coll [num-items & items-etc]]
675 (let [inventory (take (dec num-items) (rest items-etc))
676 [separator & items-etc] (drop num-items (rest items-etc))]
677 (if (= separator 0x50)
678 (map (partial mapv (comp items dec)) (conj coll inventory))
679 (recur (conj coll inventory) items-etc)
680 )
681 ))
683 '()
684 (take 1000 (drop 0x233C rom)))
687 )))
691 ;; ********************** MANIPULATION FNS
696 (defn submap?
697 "Compares the two maps. Returns true if map-big has the same associations as map-small, otherwise false."
698 [map-small map-big]
699 (cond (empty? map-small) true
700 (and
701 (contains? map-big (ffirst map-small))
702 (= (get map-big (ffirst map-small))
703 (second (first map-small))))
704 (recur (next map-small) map-big)
706 :else false))
709 (defn search-map [proto-map maps]
710 "Returns all the maps that make the same associations as proto-map."
711 (some (partial submap? proto-map) maps))
713 (defn filter-vals
714 "Returns a map consisting of all the pairs [key val] for
715 which (pred key) returns true."
716 [pred map]
717 (reduce (partial apply assoc) {}
718 (filter (fn [[k v]] (pred v)) map)))
721 (defn search-moves
722 "Returns a subcollection of all hardcoded moves with the
723 given attributes. Attributes consist of :name :power
724 :accuracy :pp :fx-id
725 (and also :fx-txt, but it contains the same information
726 as :fx-id)"
727 ([attribute-map]
728 (search-moves
729 com.aurellem.gb.gb-driver/original-rom attribute-map))
730 ([rom attribute-map]
731 (filter-vals (partial submap? attribute-map)
732 (hxc-move-data rom))))
738 ;; note for later: credits start at F1290
743 ;; (def dex-order
744 ;; [:bulbasaur
745 ;; :ivysaur
746 ;; :venusaur
747 ;; :charmander
748 ;; :charmeleon
749 ;; :charizard])
752 ;; (defn same-type-attack-bonus?
753 ;; ([pkmn move]
754 ;; (same-type-attack-bonus?
755 ;; com.aurellem.gb.gb-driver/original-rom pkmn move))
756 ;; ([rom pkmn move]
757 ;; (hxc-pokemon-base rom)))
764 (comment
766 (def hxc-later
767 "Running this code produces, e.g. hardcoded names NPCs give
768 their pokemon. Will sort through it later."
769 (print (character-codes->str(take 10000
770 (drop 0x71597
771 (rom (root)))))))
773 (let [dex
774 (partition-by #(= 0x50 %)
775 (take 2540
776 (drop 0x40687
777 (rom (root)))))]
778 (def dex dex)
779 (def hxc-species
780 (map character-codes->str
781 (take-nth 4 dex))))
782 )