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.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)))))
28 ;; used to decode item prices
30 (defn decode-bcd
31 "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) 0
35 (+ (first coll) (* 100 (self (rest coll))))))
36 (map
37 #(+ (* 10 (int (/ % 16)))
38 (rem % 16))
39 (reverse digits))))
44 (def pkmn-types
45 [:normal ;;0
46 :fighting ;;1
47 :flying ;;2
48 :poison ;;3
49 :ground ;;4
50 :rock ;;5
51 :bird ;;6
52 :bug ;;7
53 :ghost ;;8
54 :A
55 :B
56 :C
57 :D
58 :E
59 :F
60 :G
61 :H
62 :I
63 :J
64 :K
65 :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 take
76 ;; their accuracy from the move accuracy, does
77 ;; this mean that the move always "hits" but the
78 ;; status effect may not?
80 (def move-effects
81 ["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 acr
131 "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 difference
138 ;; 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 acr
149 "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 0x19
154 "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. some
163 exceptions apply."
164 "(rage)"
165 "(mimic)"
166 "(metronome)"
167 "(leech seed)"
168 "does nothing (splash)"
169 "(disable)"
170 ])
173 ;; ************** HARDCODED DATA
175 (defn hxc-thunk
176 "Creates a thunk (nullary fn) that grabs data in a certain region of rom and
177 splits it into a collection by 0x50. If rom is not supplied, uses the
178 original rom data."
179 [start length]
180 (fn self
181 ([rom]
182 (take-nth 2
183 (partition-by #(= % 0x50)
184 (take length
185 (drop start rom)))))
186 ([]
187 (self com.aurellem.gb.gb-driver/original-rom))))
189 (def hxc-thunk-words
190 "Same as hxc-thunk, except it interprets the rom data as characters,
191 returning a collection of strings."
192 (comp
193 (partial comp (partial map character-codes->str))
194 hxc-thunk))
197 ;; --------------------------------------------------
199 (def hxc-items
200 "The hardcoded names of the items in memory. List begins at
201 ROM@045B7"
202 (hxc-thunk-words 0x45B7 870))
204 (def hxc-types
205 "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-titles
210 "The hardcoded names of the trainer titles in memory. List begins at
211 ROM@27E77"
212 (hxc-thunk-words 0x27E77 196))
215 (def hxc-pokedex-text
216 "The hardcoded pokedex entries in memory. List begins at
217 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-stats
225 "The hardcoded pokedex stats (species height weight) in memory. List
226 begins at ROM@40687"
227 ;; uses hxc-pokedex-text to count pokemon
228 ;; 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-stats
235 [n stats data]
236 (if (zero? n) stats
237 (let [[species
238 [_
239 height-ft
240 height-in
241 weight-1
242 weight-2
243 _
244 dex-ptr-1
245 dex-ptr-2
246 dex-bank
247 _
248 & data]]
249 (split-with (partial not= 0x50) data)]
250 (recur (dec n)
251 (assoc stats
252 (- pkmn-count n)
253 {:species
254 (character-codes->str species)
255 :height-ft
256 height-ft
257 :height-in
258 height-in
259 :weight
260 (/ (low-high weight-1 weight-2) 10.)
262 ;; :text
263 ;; (character-codes->str
264 ;; (take-while
265 ;; (partial not= 0x50)
266 ;; (drop
267 ;; (+ 0xB8000
268 ;; -0x4000
269 ;; (low-high dex-ptr-1 dex-ptr-2))
270 ;; rom)))
271 })
273 data)
276 )))
278 pkmn-count
279 {}
280 (drop 0x40687 rom))) ))
288 (def hxc-places
289 "The hardcoded place names in memory. List begins at
290 ROM@71500. [Cinnabar] Mansion seems to be dynamically calculated."
291 (hxc-thunk-words 0x71500 560))
294 (defn hxc-dialog
295 "The hardcoded dialogue in memory, including in-game alerts. Dialog
296 seems to be separated by 0x57 instead of 0x50 (END). Begins at ROM@98000."
297 ([rom]
298 (map character-codes->str
299 (take-nth 2
300 (partition-by #(= % 0x57)
301 (take 0x0F728
302 (drop 0x98000 rom))))))
303 ([]
304 (hxc-dialog com.aurellem.gb.gb-driver/original-rom)))
309 (def hxc-move-names
310 "The hardcoded move names in memory. List begins at ROM@BC000"
311 (hxc-thunk-words 0xBC000 1551))
314 (defn hxc-move-data
315 "The hardcoded (basic (move effects)) in memory. List begins at
316 0x38000. Returns a map of {:name :power :accuracy :pp :fx-id
317 :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 6
324 types pkmn-types ;;; !! hardcoded types
325 ]
326 (zipmap (map format-name names)
327 (map
328 (fn [[idx effect power type-id accuracy pp]]
329 {:name (names (dec idx))
330 :power power
331 :accuracy accuracy
332 :pp pp
333 :type (types type-id)
334 :fx-id effect
335 :fx-txt (get move-effects effect)
336 }
337 )
339 (partition move-size
340 (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 6
353 format-name (fn [s]
354 (keyword (.toLowerCase
355 (apply str
356 (map #(if (= % \space) "-" %) s)))))
357 ]
358 (zipmap (map format-name names)
359 (map
360 (fn [[idx effect power type accuracy pp]]
361 {:name (names (dec idx))
362 :power power
363 :accuracy (hex accuracy)
364 :pp pp
365 :fx-id (hex effect)
366 :fx-txt (get move-effects effect)
367 }
368 )
370 (partition move-size
371 (take (* move-size move-count)
372 (drop 0x38000 rom))))))))
376 (defn hxc-pokenames
377 "The hardcoded names of the 190 species in memory. List begins at
378 ROM@E8000. Although names in memory are padded with 0x50 to be 10 characters
379 long, these names are stripped of padding."
380 ([]
381 (hxc-pokenames com.aurellem.gb.gb-driver/original-rom))
382 ([rom]
383 (let [count-species 190
384 name-length 10]
385 (map character-codes->str
386 (partition name-length
387 (map #(if (= 0x50 %) 0x00 %)
388 (take (* count-species name-length)
389 (drop 0xE8000
390 rom))))))))
395 (defn internal-id
396 ([rom]
397 (zipmap
398 (map format-name (hxc-pokenames rom))
399 (range)))
400 ([]
401 (internal-id com.aurellem.gb.gb-driver/original-rom)))
405 ;; nidoran gender change upon levelup
406 ;; (->
407 ;; @current-state
408 ;; rom
409 ;; vec
410 ;; (rewrite-memory
411 ;; (nth (hxc-ptrs-evolve) ((internal-id) :nidoran♂))
412 ;; [1 1 15])
413 ;; (rewrite-memory
414 ;; (nth (hxc-ptrs-evolve) ((internal-id) :nidoran♀))
415 ;; [1 1 3])
416 ;; (write-rom!)
418 ;; )
423 (defn hxc-advantage
424 "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 (map
429 (fn [[atk def mult]] [(get pkmn-types atk (hex atk))
430 (get pkmn-types def (hex def))
431 (/ mult 10)])
432 (partition 3
433 (take-while (partial not= 0xFF)
434 (drop 0x3E62D rom))))))
438 (defn format-evo
439 [coll]
440 (let [method (first coll)]
441 (cond (empty? coll) []
442 (= 0 method) [] ;; just in case
443 (= 1 method) ;; level-up evolution
444 (conj (format-evo (drop 3 coll))
445 {:method :level-up
446 :min-level (nth coll 1)
447 :into (dec (nth coll 2))})
449 (= 2 method) ;; item evolution
450 (conj (format-evo (drop 4 coll))
451 {:method :item
452 :item (dec (nth coll 1))
453 :min-level (nth coll 2)
454 :into (dec (nth coll 3))})
456 (= 3 method) ;; trade evolution
457 (conj (format-evo (drop 3 coll))
458 {:method :trade
459 :min-level (nth coll 1) ;; always 1 for trade.
460 :into (dec (nth coll 2))}))))
463 (defn hxc-ptrs-evolve
464 "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 ptrs
472 (map (fn [[a b]] (low-high a b))
473 (partition 2
474 (take (* 2 pkmn-count)
475 (drop 0x3b1e5 rom))))]
476 (map (partial + 0x34000) ptrs)
478 )))
481 (defn hxc-learnsets
482 "Hardcoded map associating pokemon names to lists of pairs [lvl
483 move] of abilities they learn as they level up. The data
484 exists at ROM@3400, sorted by internal order. Pointers to the data
485 exist at ROM@3B1E5; see also, hxc-ptrs-evolve"
486 ([] (hxc-learnsets com.aurellem.gb.gb-driver/original-rom))
487 ([rom]
488 (apply assoc
489 {}
490 (interleave
491 (map format-name (hxc-pokenames rom))
492 (map (comp
493 (partial map
494 (fn [[lvl mv]] [lvl (dec mv)]))
495 (partial partition 2)
496 ;; keep the learnset data
497 (partial take-while (comp not zero?))
498 ;; skip the evolution data
499 rest
500 (partial drop-while (comp not zero?)))
501 (map #(drop % rom)
502 (hxc-ptrs-evolve rom)))))))
504 (defn hxc-learnsets-pretty
505 "Live hxc-learnsets except it reports the name of each move --- as
506 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-evolution
520 "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 (interleave
526 (map format-name (hxc-pokenames rom))
527 (map
528 (comp
529 format-evo
530 (partial take-while (comp not zero?))
531 #(drop % rom))
532 (hxc-ptrs-evolve rom)
533 )))))
535 (defn hxc-evolution-pretty
536 "Like hxc-evolution, except it uses the names of items and pokemon
537 --- grabbed from ROM --- rather than their numerical identifiers."
538 ([] (hxc-evolution-pretty com.aurellem.gb.gb-driver/original-rom))
539 ([rom]
540 (let
541 [poke-names (vec (map format-name (hxc-pokenames rom)))
542 item-names (vec (map format-name (hxc-items rom)))
543 use-names
544 (fn [m]
545 (loop [ks (keys m) new-map m]
546 (let [k (first ks)]
547 (cond (nil? ks) new-map
548 (= k :into)
549 (recur
550 (next ks)
551 (assoc new-map
552 :into
553 (poke-names
554 (:into
555 new-map))))
556 (= k :item)
557 (recur
558 (next ks)
559 (assoc new-map
560 :item
561 (item-names
562 (:item new-map))))
563 :else
564 (recur
565 (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-base
579 ([] (hxc-pokemon-base com.aurellem.gb.gb-driver/original-rom))
580 ([rom]
581 (let [entry-size 28
582 pkmn-count (count (hxc-pokedex-text rom))
583 types (apply assoc {}
584 (interleave
585 (range)
586 pkmn-types)) ;;!! softcoded
587 moves (apply assoc {}
588 (interleave
589 (range)
590 (map format-name
591 (hxc-move-names rom))))
592 ]
593 (map
595 (fn [[n
596 rating-hp
597 rating-atk
598 rating-def
599 rating-speed
600 rating-special
601 type-1
602 type-2
603 rarity
604 rating-xp
605 pic-dimensions
606 ptr-pic-obverse-1
607 ptr-pic-obverse-2
608 ptr-pic-reverse-1
609 ptr-pic-reverse-2
610 move-1
611 move-2
612 move-3
613 move-4
614 growth-rate
615 &
616 TMs|HMs]]
617 (let
618 [base-moves
619 (mapv moves
620 ((comp
621 ;; 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 types
629 (set (list (types type-1)
630 (types type-2)))
631 TMs|HMs
632 (map
633 (comp
634 (partial map first)
635 (partial remove (comp zero? second)))
636 (split-at
637 50
638 (map vector
639 (rest(range))
640 (reduce concat
641 (map
642 #(take 8
643 (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# n
656 :base-moves base-moves
657 :types types
658 :TMs TMs
659 :HMs HMs
660 :base-hp rating-hp
661 :base-atk rating-atk
662 :base-def rating-def
663 :base-speed rating-speed
664 :base-special rating-special
665 }))
667 (partition entry-size
668 (take (* entry-size pkmn-count)
669 (drop 0x383DE
670 rom)))))))
674 (defn hxc-item-prices
675 "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 items
681 (map (comp
682 ;; zero-cost items are "priceless"
683 #(if (zero? %) :priceless %)
684 decode-bcd butlast)
685 (partition price-size
686 (take (* price-size (count items))
687 (drop 0x4495 rom))))))))
689 (defn hxc-shops
690 ([] (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 items
695 items (into
696 items
697 (map (juxt identity
698 (comp keyword
699 (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-while
708 (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 FNS
730 (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) true
734 (and
735 (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-vals
748 "Returns a map consisting of all the pairs [key val] for
749 which (pred key) returns true."
750 [pred map]
751 (reduce (partial apply assoc) {}
752 (filter (fn [[k v]] (pred v)) map)))
755 (defn search-moves
756 "Returns a subcollection of all hardcoded moves with the
757 given attributes. Attributes consist of :name :power
758 :accuracy :pp :fx-id
759 (and also :fx-txt, but it contains the same information
760 as :fx-id)"
761 ([attribute-map]
762 (search-moves
763 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 F1290
777 ;; (def dex-order
778 ;; [:bulbasaur
779 ;; :ivysaur
780 ;; :venusaur
781 ;; :charmander
782 ;; :charmeleon
783 ;; :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 (comment
800 (def hxc-later
801 "Running this code produces, e.g. hardcoded names NPCs give
802 their pokemon. Will sort through it later."
803 (print (character-codes->str(take 10000
804 (drop 0x71597
805 (rom (root)))))))
807 (let [dex
808 (partition-by #(= 0x50 %)
809 (take 2540
810 (drop 0x40687
811 (rom (root)))))]
812 (def dex dex)
813 (def hxc-species
814 (map character-codes->str
815 (take-nth 4 dex))))
816 )