view clojure/com/aurellem/gb/hxc.clj @ 249:99227bec1123

Reworked the hxc definitions; they are now fns that optionally take a single rom arg and dynamically grab the data. Their commonalities are also consolidated into a single template function, hxc-thunk. Minor fixes include stripping pokenames of their padding and splitting the huge hxc-dialog string into a list.
author Dylan Holmes <ocsenave@gmail.com>
date Mon, 26 Mar 2012 03:45:24 -0500
parents 921d2277bb57
children 2b6bd03feb4f
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
12 (def pkmn-types
13 [:normal
14 :fighting
15 :flying
16 :poison
17 :ground
18 :rock
19 :bird
20 :bug
21 :ghost
22 :A
23 :B
24 :C
25 :D
26 :E
27 :F
28 :G
29 :H
30 :I
31 :J
32 :K
33 :fire
34 :water
35 :grass
36 :electric
37 :psychic
38 :ice
39 :dragon
40 ])
43 ;; question: when status effects claim to take
44 ;; their accuracy from the move accuracy, does
45 ;; this mean that the move always "hits" but the
46 ;; status effect may not?
48 (def move-effects
49 ["normal damage"
50 "no damage, just opponent sleep" ;; how many turns? is atk power ignored?
51 "0x4C chance of poison"
52 "leech half of inflicted damage"
53 "0x19 chance of burn"
54 "0x19 chance of freeze"
55 "0x19 chance of paralyze"
56 "user faints; opponent defense halved."
57 "leech half of inflicted damage ONLY if sleeping opponent."
58 "imitate last attack"
59 "user atk +1"
60 "user def +1"
61 "user spd +1"
62 "user spc +1"
63 "user acr +1" ;; unused?!
64 "user evd +1"
65 "get post-battle $ = 2*level*uses"
66 "0xFE acr, no matter what."
67 "opponent atk -1" ;; acr taken from move acr?
68 "opponent def -1" ;;
69 "opponent spd -1" ;;
70 "opponent spc -1" ;;
71 "opponent acr -1";;
72 "opponent evd -1"
73 "converts user's type to opponent's."
74 "(haze)"
75 "(bide)"
76 "(thrash)"
77 "(teleport)"
78 "(fury swipes)"
79 "attacks 2-5 turns" ;; unused? like rollout?
80 "0x19 chance of flinch"
81 "opponent sleep for 1-7 turns"
82 "0x66 chance of poison"
83 "0x4D chance of burn"
84 "0x4D chance of freeze"
85 "0x4D chance of paralyze"
86 "0x4D chance of flinch"
87 "one-hit KO"
88 "charge one turn, atk next."
89 "fixed damage, leaves 1HP." ;; how is dmg determined?
90 "fixed damage." ;; cf seismic toss, dragon rage, psywave.
91 "atk 2-5 turns; opponent can't attack" ;; unnormalized? (0 0x60 0x60 0x20 0x20)
92 "charge one turn, atk next. (can't be hit when charging)"
93 "atk hits twice."
94 "user takes 1 damage if misses."
95 "evade status-lowering effects" ;;caused by you or also your opponent?
96 "(broken) if user is slower than opponent, makes critical hit impossible, otherwise has no effect"
97 "atk causes recoil dmg = 1/4 dmg dealt"
98 "confuses opponent" ;; acr taken from move acr
99 "user atk +2"
100 "user def +2"
101 "user spd +2"
102 "user spc +2"
103 "user acr +2" ;; unused!
104 "user evd +2" ;; unused!
105 "restores up to half of user's max hp." ;; broken: fails if the difference
106 ;; b/w max and current hp is one less than a multiple of 256.
107 "(transform)"
108 "opponent atk -2"
109 "opponent def -2"
110 "opponent spd -2"
111 "opponent spc -2"
112 "opponent acr -2"
113 "opponent evd -2"
114 "doubles user spc when attacked"
115 "doubles user def when attacked"
116 "just poisons opponent" ;;acr taken from move acr
117 "just paralyzes opponent" ;;
118 "0x19 chance opponent atk -1"
119 "0x19 chance opponent def -1"
120 "0x19 chance opponent spd -1"
121 "0x4C chance opponent spc -1" ;; context suggest chance is 0x19
122 "0x19 chance opponent acr -1"
123 "0x19 chance opponent evd -1"
124 "???" ;; unused? no effect?
125 "???" ;; unused? no effect?
126 "0x19 chance opponent confused"
127 "atk hits twice. 0x33 chance opponent poisioned."
128 "broken. crash the game after attack."
129 "(substitute)"
130 "unless opponent faints, user must recharge after atk. some
131 exceptions apply."
132 "(rage)"
133 "(mimic)"
134 "(metronome)"
135 "(leech seed)"
136 "does nothing (splash)"
137 "(disable)"
138 ])
141 ;; ************** HARDCODED DATA
143 (defn hxc-thunk
144 "Creates a thunk (unary fn) that grabs data in a certain region of rom and
145 splits it into a collection by 0x50. If rom is not supplied, uses the
146 original rom data."
147 [start length]
148 (fn self
149 ([rom]
150 (take-nth 2
151 (partition-by #(= % 0x50)
152 (take length
153 (drop start rom)))))
154 ([]
155 (self com.aurellem.gb.gb-driver/original-rom))))
157 (def hxc-thunk-words
158 "Same as hxc-thunk, except it interprets the rom data as characters,
159 returning a collection of strings."
160 (comp
161 (partial comp (partial map character-codes->str))
162 hxc-thunk))
165 ;; --------------------------------------------------
167 (def hxc-items
168 "The hardcoded names of the items in memory. List begins at
169 ROM@045B7"
170 (hxc-thunk-words 0x45B7 870))
172 (def hxc-types
173 "The hardcoded type names in memory. List begins at ROM@27D99,
174 shortly before hxc-titles."
175 (hxc-thunk-words 0x27D99 102))
177 (def hxc-titles
178 "The hardcoded names of the trainer titles in memory. List begins at
179 ROM@27E77"
180 (hxc-thunk-words 0x27E77 196))
182 (def hxc-places
183 "The hardcoded place names in memory. List begins at
184 ROM@71500. [Cinnabar] Mansion seems to be dynamically calculated."
185 (hxc-thunk-words 0x71500 560))
188 (defn hxc-dialog
189 "The hardcoded dialogue in memory, including in-game alerts. Dialog
190 seems to be separated by 0x57 instead of 0x50 (END). Begins at ROM@98000."
191 ([rom]
192 (map character-codes->str
193 (take-nth 2
194 (partition-by #(= % 0x57)
195 (take 0x0F728
196 (drop 0x98000 rom))))))
197 ([]
198 (hxc-dialog com.aurellem.gb.gb-driver/original-rom)))
201 (def hxc-pokedex
202 "The hardcoded pokedex entries in memory. List begins at
203 ROM@B8000, shortly before move names."
204 (hxc-thunk-words 0xB8000 14754))
206 (def hxc-move-names
207 "The hardcoded move names in memory. List begins at ROM@BC000"
208 (hxc-thunk-words 0xBC000 1551))
211 (defn hxc-move-data
212 "The hardcoded (basic (move effects)) in memory. List begins at
213 0x38000. Returns a map of {:name :power :accuracy :pp :fx-id
214 :fx-txt}. The move descriptions are handwritten, not hardcoded."
215 ([]
216 (hxc-move-data com.aurellem.gb.gb-driver/original-rom))
217 ([rom]
218 (let [names (vec (hxc-move-names rom))
219 move-count (count names)
220 move-size 6
221 format-name (fn [s]
222 (keyword (.toLowerCase
223 (apply str
224 (map #(if (= % \space) "-" %) s)))))]
225 (zipmap (map format-name names)
226 (map
227 (fn [[idx effect power type accuracy pp]]
228 {:name (names (dec idx))
229 :power power
230 :accuracy accuracy
231 :pp pp
232 :fx-id effect
233 :fx-txt (get move-effects effect)
234 }
235 )
237 (partition move-size
238 (take (* move-size move-count)
239 (drop 0x38000 rom))))))))
243 (defn hxc-move-data*
244 "Like hxc-move-data, but reports numbers as hexadecimal symbols instead."
245 ([]
246 (hxc-move-data* com.aurellem.gb.gb-driver/original-rom))
247 ([rom]
248 (let [names (vec (hxc-move-names rom))
249 move-count (count names)
250 move-size 6
251 format-name (fn [s]
252 (keyword (.toLowerCase
253 (apply str
254 (map #(if (= % \space) "-" %) s)))))
255 ]
256 (zipmap (map format-name names)
257 (map
258 (fn [[idx effect power type accuracy pp]]
259 {:name (names (dec idx))
260 :power power
261 :accuracy (hex accuracy)
262 :pp pp
263 :fx-id (hex effect)
264 :fx-txt (get move-effects effect)
265 }
266 )
268 (partition move-size
269 (take (* move-size move-count)
270 (drop 0x38000 rom))))))))
274 (defn hxc-pokenames
275 "The hardcoded names of the 190 species in memory. List begins at
276 ROM@E8000. Although names in memory are padded with 0x50 to be 10 characters
277 long, these names are stripped of padding."
278 ([]
279 (hxc-pokenames com.aurellem.gb.gb-driver/original-rom))
280 ([rom]
281 (let [count-species 190
282 name-length 10]
283 (map character-codes->str
284 (partition name-length
285 (map #(if (= 0x50 %) 0x00 %)
286 (take (* count-species name-length)
287 (drop 0xE8000
288 rom))))))))
290 (defn hxc-advantage
291 "The hardcoded type advantages in memory, returned as tuples of atk-type def-type multiplier. By default (i.e. if not listed here),
292 the multiplier is 1."
293 ([] (hxc-advantage com.aurellem.gb.gb-driver/original-rom))
294 ([rom]
295 (map
296 (fn [[atk def mult]] [(get pkmn-types atk (hex atk))
297 (get pkmn-types def (hex def))
298 (/ mult 10)])
299 (partition 3
300 (take-while (partial not= 0xFF)
301 (drop 0x3E62D rom))))))
312 ;; ********************** MANIPULATION FNS
317 (defn submap?
318 "Compares the two maps. Returns true if map-big has the same associations as map-small, otherwise false."
319 [map-small map-big]
320 (cond (empty? map-small) true
321 (and
322 (contains? map-big (ffirst map-small))
323 (= (get map-big (ffirst map-small))
324 (second (first map-small))))
325 (recur (next map-small) map-big)
327 :else false))
330 (defn search-map [proto-map maps]
331 "Returns all the maps that make the same associations as proto-map."
332 (some (partial submap? proto-map) maps))
334 ;; (I don't use this for anything so far.)
335 ;;
336 ;; (defn filter-vals
337 ;; "Returns a map consisting of all the pairs [key val] for which (pred
338 ;; key) returns true."
339 ;; [pred map]
340 ;; (reduce (partial apply assoc) {} (filter (fn [[k v]] (pred v)) map)))
343 (defn search-moves
344 "Returns a subcollection of all hardcoded moves with the given
345 attributes. Attributes consist of :name :power :accuracy :pp :fx-id
346 (and also :fx-txt, but it contains the same information as :fx-id)"
347 ([attribute-map]
348 (search-moves com.aurellem.gb.gb-driver/original-rom attribute-map))
349 ([rom attribute-map]
350 (filter-vals (partial submap? attribute-map) (hxc-move-data
351 rom))))
363 ;; note for later: credits start at F1290
367 (comment
369 (def hxc-later
370 "Running this code produces, e.g. hardcoded names NPCs give
371 their pokemon. Will sort through it later."
372 (print (character-codes->str(take 10000
373 (drop 0x71597
374 (rom (root)))))))
376 (let [dex
377 (partition-by #(= 0x50 %)
378 (take 2540
379 (drop 0x40687
380 (rom (root)))))]
381 (def dex dex)
382 (def hxc-species
383 (map character-codes->str
384 (take-nth 4 dex))))