view clojure/com/aurellem/gb/hxc.clj @ 272:a60ea8632ff4

beginning work on hxc-pokemon-base, the collection of base stats, battle sprite data, etc.
author Dylan Holmes <ocsenave@gmail.com>
date Tue, 27 Mar 2012 00:59:43 -0500
parents 82ee2704c973
children 69184558fcf3
line wrap: on
line source
1 (ns com.aurellem.gb.hxc
2 (:use (com.aurellem.gb assembly characters gb-driver util
3 constants))
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))
207 (defn hxc-pokemon-base
208 ([] (hxc-pokemon-base com.aurellem.gb.gb-driver/original-rom))
209 ([rom]
210 (let [entry-size 28
211 pkmn-count (count (hxc-pokedex-text rom))
212 types (apply assoc {}
213 (interleave
214 (range)
215 pkmn-types)) ;;!! softcoded
216 moves (apply assoc {}
217 (interleave
218 (range)
219 (map format-name
220 (hxc-move-names rom))))
222 ]
223 (map
225 (fn [[n
226 rating-hp
227 rating-atk
228 rating-def
229 rating-speed
230 rating-special
231 type-1
232 type-2
233 rarity
234 rating-xp
235 _
236 ptr-pic-obverse-1
237 ptr-pic-obverse-2
238 ptr-pic-reverse-1
239 ptr-pic-reverse-2
240 move-1
241 move-2
242 move-3
243 move-4
244 &
245 TMs]]
246 {:dex# n
247 :base-moves
248 (mapv moves
249 ((comp
250 ;; since the game uses zero as a delimiter,
251 ;; it must also increment all move indices by 1.
252 ;; here we decrement to correct this.
253 (partial map dec)
254 (partial take-while (comp not zero?)))
255 [move-1 move-2 move-3 move-4]))
257 :types (set (list (types type-1)
258 (types type-2)))})
260 (partition entry-size
261 (take (* entry-size pkmn-count)
262 (drop 0x383DE
263 rom)))))))
267 ;; In red/blue, pokedex stats are in internal order.
268 ;; In yellow, pokedex stats are in pokedex order.
270 (defn hxc-pokedex-stats
271 "The hardcoded pokedex stats (species height weight) in memory. List
272 begins at ROM@40687"
273 ;; uses hxc-pokedex-text to count pokemon
274 ;; since hxc-pokenames includes several missingno"
275 ([] (hxc-pokedex-stats com.aurellem.gb.gb-driver/original-rom))
276 ([rom]
277 (let [poketext (hxc-pokedex-text)
278 pkmn-count (count poketext)
279 ]
280 ((fn capture-stats
281 [n stats data]
282 (if (zero? n) stats
283 (let [[species
284 [_
285 height-ft
286 height-in
287 weight-1
288 weight-2
289 _
290 dex-ptr-1
291 dex-ptr-2
292 dex-bank
293 _
294 & data]]
295 (split-with (partial not= 0x50) data)]
296 (recur (dec n)
297 (assoc stats
298 (- pkmn-count n)
299 {:species
300 (character-codes->str species)
301 :height-ft
302 height-ft
303 :height-in
304 height-in
305 :weight
306 (/ (low-high weight-1 weight-2) 10.)
308 ;; :text
309 ;; (character-codes->str
310 ;; (take-while
311 ;; (partial not= 0x50)
312 ;; (drop
313 ;; (+ 0xB8000
314 ;; -0x4000
315 ;; (low-high dex-ptr-1 dex-ptr-2))
316 ;; rom)))
317 })
319 data)
322 )))
324 pkmn-count
325 {}
326 (drop 0x40687 rom))) ))
334 (def hxc-places
335 "The hardcoded place names in memory. List begins at
336 ROM@71500. [Cinnabar] Mansion seems to be dynamically calculated."
337 (hxc-thunk-words 0x71500 560))
340 (defn hxc-dialog
341 "The hardcoded dialogue in memory, including in-game alerts. Dialog
342 seems to be separated by 0x57 instead of 0x50 (END). Begins at ROM@98000."
343 ([rom]
344 (map character-codes->str
345 (take-nth 2
346 (partition-by #(= % 0x57)
347 (take 0x0F728
348 (drop 0x98000 rom))))))
349 ([]
350 (hxc-dialog com.aurellem.gb.gb-driver/original-rom)))
355 (def hxc-move-names
356 "The hardcoded move names in memory. List begins at ROM@BC000"
357 (hxc-thunk-words 0xBC000 1551))
360 (defn hxc-move-data
361 "The hardcoded (basic (move effects)) in memory. List begins at
362 0x38000. Returns a map of {:name :power :accuracy :pp :fx-id
363 :fx-txt}. The move descriptions are handwritten, not hardcoded."
364 ([]
365 (hxc-move-data com.aurellem.gb.gb-driver/original-rom))
366 ([rom]
367 (let [names (vec (hxc-move-names rom))
368 move-count (count names)
369 move-size 6]
370 (zipmap (map format-name names)
371 (map
372 (fn [[idx effect power type accuracy pp]]
373 {:name (names (dec idx))
374 :power power
375 :accuracy accuracy
376 :pp pp
377 :fx-id effect
378 :fx-txt (get move-effects effect)
379 }
380 )
382 (partition move-size
383 (take (* move-size move-count)
384 (drop 0x38000 rom))))))))
388 (defn hxc-move-data*
389 "Like hxc-move-data, but reports numbers as hexadecimal symbols instead."
390 ([]
391 (hxc-move-data* com.aurellem.gb.gb-driver/original-rom))
392 ([rom]
393 (let [names (vec (hxc-move-names rom))
394 move-count (count names)
395 move-size 6
396 format-name (fn [s]
397 (keyword (.toLowerCase
398 (apply str
399 (map #(if (= % \space) "-" %) s)))))
400 ]
401 (zipmap (map format-name names)
402 (map
403 (fn [[idx effect power type accuracy pp]]
404 {:name (names (dec idx))
405 :power power
406 :accuracy (hex accuracy)
407 :pp pp
408 :fx-id (hex effect)
409 :fx-txt (get move-effects effect)
410 }
411 )
413 (partition move-size
414 (take (* move-size move-count)
415 (drop 0x38000 rom))))))))
419 (defn hxc-pokenames
420 "The hardcoded names of the 190 species in memory. List begins at
421 ROM@E8000. Although names in memory are padded with 0x50 to be 10 characters
422 long, these names are stripped of padding."
423 ([]
424 (hxc-pokenames com.aurellem.gb.gb-driver/original-rom))
425 ([rom]
426 (let [count-species 190
427 name-length 10]
428 (map character-codes->str
429 (partition name-length
430 (map #(if (= 0x50 %) 0x00 %)
431 (take (* count-species name-length)
432 (drop 0xE8000
433 rom))))))))
438 (defn internal-id
439 ([rom]
440 (zipmap
441 (map format-name (hxc-pokenames rom))
442 (range)))
443 ([]
444 (internal-id com.aurellem.gb.gb-driver/original-rom)))
448 ;; nidoran gender change upon levelup
449 ;; (->
450 ;; @current-state
451 ;; rom
452 ;; vec
453 ;; (rewrite-memory
454 ;; (nth (hxc-ptrs-evolve) ((internal-id) :nidoran♂))
455 ;; [1 1 15])
456 ;; (rewrite-memory
457 ;; (nth (hxc-ptrs-evolve) ((internal-id) :nidoran♀))
458 ;; [1 1 3])
459 ;; (write-rom!)
461 ;; )
466 (defn hxc-advantage
467 "The hardcoded type advantages in memory, returned as tuples of atk-type def-type multiplier. By default (i.e. if not listed here),
468 the multiplier is 1."
469 ([] (hxc-advantage com.aurellem.gb.gb-driver/original-rom))
470 ([rom]
471 (map
472 (fn [[atk def mult]] [(get pkmn-types atk (hex atk))
473 (get pkmn-types def (hex def))
474 (/ mult 10)])
475 (partition 3
476 (take-while (partial not= 0xFF)
477 (drop 0x3E62D rom))))))
480 (defn format-evo
481 [coll]
482 (let [method (first coll)]
483 (cond (empty? coll) []
484 (= 0 method) [] ;; just in case
485 (= 1 method) ;; level-up evolution
486 (conj (format-evo (drop 3 coll))
487 {:method :level-up
488 :min-level (nth coll 1)
489 :into (dec (nth coll 2))})
491 (= 2 method) ;; item evolution
492 (conj (format-evo (drop 4 coll))
493 {:method :item
494 :item (dec (nth coll 1))
495 :min-level (nth coll 2)
496 :into (dec (nth coll 3))})
498 (= 3 method) ;; trade evolution
499 (conj (format-evo (drop 3 coll))
500 {:method :trade
501 :min-level (nth coll 1) ;; always 1 for trade.
502 :into (dec (nth coll 2))}))))
505 (defn hxc-ptrs-evolve
506 "A hardcoded collection of 190 pointers to alternating evolution/learnset data,
507 in internal order."
508 ([]
509 (hxc-ptrs-evolve com.aurellem.gb.gb-driver/original-rom))
510 ([rom]
511 (let [names (hxc-pokenames rom)
512 pkmn-count (count names)
513 ptrs
514 (map (fn [[a b]] (low-high a b))
515 (partition 2
516 (take (* 2 pkmn-count)
517 (drop 0x3b1e5 rom))))]
518 (map (partial + 0x34000) ptrs)
520 )))
523 (defn hxc-learnsets
524 "Hardcoded map associating pokemon names to lists of pairs [lvl
525 move] of abilities they learn as they level up. The data
526 exists at ROM@3400, sorted by internal order. Pointers to the data
527 exist at ROM@3B1E5; see also, hxc-ptrs-evolve"
528 ([] (hxc-learnsets com.aurellem.gb.gb-driver/original-rom))
529 ([rom]
530 (apply assoc
531 {}
532 (interleave
533 (map format-name (hxc-pokenames rom))
534 (map (comp
535 (partial map
536 (fn [[lvl mv]] [lvl (dec mv)]))
537 (partial partition 2)
538 ;; keep the learnset data
539 (partial take-while (comp not zero?))
540 ;; skip the evolution data
541 rest
542 (partial drop-while (comp not zero?)))
543 (map #(drop % rom)
544 (hxc-ptrs-evolve rom)))))))
546 (defn hxc-learnsets-pretty
547 "Live hxc-learnsets except it reports the name of each move --- as
548 it appears in rom --- rather than the move index."
549 ([] (hxc-learnsets-pretty com.aurellem.gb.gb-driver/original-rom))
550 ([rom]
551 (let [moves (vec(map format-name (hxc-move-names)))]
552 (into {}
553 (map (fn [[pkmn learnset]]
554 [pkmn (map (fn [[lvl mv]] [lvl (moves mv)])
555 learnset)])
556 (hxc-learnsets rom))))))
561 (defn hxc-evolution
562 "Hardcoded evolution data in memory. The data exists at ROM@34000,
563 sorted by internal order. Pointers to the data exist at ROM@3B1E5; see also, hxc-ptrs-evolve."
564 ([] (hxc-evolution com.aurellem.gb.gb-driver/original-rom))
565 ([rom]
566 (apply assoc {}
567 (interleave
568 (map format-name (hxc-pokenames rom))
569 (map
570 (comp
571 format-evo
572 (partial take-while (comp not zero?))
573 #(drop % rom))
574 (hxc-ptrs-evolve rom)
575 )))))
577 (defn hxc-evolution-pretty
578 "Like hxc-evolution, except it uses the names of items and pokemon
579 --- grabbed from ROM --- rather than their numerical identifiers."
580 ([] (hxc-evolution-pretty com.aurellem.gb.gb-driver/original-rom))
581 ([rom]
582 (let
583 [poke-names (vec (map format-name (hxc-pokenames rom)))
584 item-names (vec (map format-name (hxc-items rom)))
585 use-names
586 (fn [m]
587 (loop [ks (keys m) new-map m]
588 (let [k (first ks)]
589 (cond (nil? ks) new-map
590 (= k :into)
591 (recur
592 (next ks)
593 (assoc new-map
594 :into
595 (poke-names
596 (:into
597 new-map))))
598 (= k :item)
599 (recur
600 (next ks)
601 (assoc new-map
602 :item
603 (item-names
604 (:item new-map))))
605 :else
606 (recur
607 (next ks)
608 new-map)
609 ))))]
611 (into {}
612 (map (fn [[pkmn evo-coll]]
613 [pkmn (map use-names evo-coll)])
614 (hxc-evolution rom))))))
620 ;; ********************** MANIPULATION FNS
625 (defn submap?
626 "Compares the two maps. Returns true if map-big has the same associations as map-small, otherwise false."
627 [map-small map-big]
628 (cond (empty? map-small) true
629 (and
630 (contains? map-big (ffirst map-small))
631 (= (get map-big (ffirst map-small))
632 (second (first map-small))))
633 (recur (next map-small) map-big)
635 :else false))
638 (defn search-map [proto-map maps]
639 "Returns all the maps that make the same associations as proto-map."
640 (some (partial submap? proto-map) maps))
642 (defn filter-vals
643 "Returns a map consisting of all the pairs [key val] for
644 which (pred key) returns true."
645 [pred map]
646 (reduce (partial apply assoc) {}
647 (filter (fn [[k v]] (pred v)) map)))
650 (defn search-moves
651 "Returns a subcollection of all hardcoded moves with the
652 given attributes. Attributes consist of :name :power
653 :accuracy :pp :fx-id
654 (and also :fx-txt, but it contains the same information
655 as :fx-id)"
656 ([attribute-map]
657 (search-moves
658 com.aurellem.gb.gb-driver/original-rom attribute-map))
659 ([rom attribute-map]
660 (filter-vals (partial submap? attribute-map)
661 (hxc-move-data rom))))
667 ;; note for later: credits start at F1290
671 (comment
673 (def hxc-later
674 "Running this code produces, e.g. hardcoded names NPCs give
675 their pokemon. Will sort through it later."
676 (print (character-codes->str(take 10000
677 (drop 0x71597
678 (rom (root)))))))
680 (let [dex
681 (partition-by #(= 0x50 %)
682 (take 2540
683 (drop 0x40687
684 (rom (root)))))]
685 (def dex dex)
686 (def hxc-species
687 (map character-codes->str
688 (take-nth 4 dex))))
689 )