comparison 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
comparison
equal deleted inserted replaced
248:e94d20ad853e 249:99227bec1123
3 constants)) 3 constants))
4 (:use (com.aurellem.world practice)) 4 (:use (com.aurellem.world practice))
5 (:import [com.aurellem.gb.gb_driver SaveState])) 5 (:import [com.aurellem.gb.gb_driver SaveState]))
6 6
7 7
8
9
10 ; ************* HANDWRITTEN CONSTANTS
8 11
9 (def pkmn-types 12 (def pkmn-types
10 [:normal 13 [:normal
11 :fighting 14 :fighting
12 :flying 15 :flying
108 "opponent spc -2" 111 "opponent spc -2"
109 "opponent acr -2" 112 "opponent acr -2"
110 "opponent evd -2" 113 "opponent evd -2"
111 "doubles user spc when attacked" 114 "doubles user spc when attacked"
112 "doubles user def when attacked" 115 "doubles user def when attacked"
113 "poisons opponent" ;;acr taken from move acr 116 "just poisons opponent" ;;acr taken from move acr
114 "paralyzes opponent" ;; 117 "just paralyzes opponent" ;;
115 "0x19 chance opponent atk -1" 118 "0x19 chance opponent atk -1"
116 "0x19 chance opponent def -1" 119 "0x19 chance opponent def -1"
117 "0x19 chance opponent spd -1" 120 "0x19 chance opponent spd -1"
118 "0x4C chance opponent spc -1" ;; context suggest chance is 0x19 121 "0x4C chance opponent spc -1" ;; context suggest chance is 0x19
119 "0x19 chance opponent acr -1" 122 "0x19 chance opponent acr -1"
133 "does nothing (splash)" 136 "does nothing (splash)"
134 "(disable)" 137 "(disable)"
135 ]) 138 ])
136 139
137 140
138 141 ;; ************** HARDCODED DATA
139 142
140 143 (defn hxc-thunk
141 (def hxc-items 144 "Creates a thunk (unary fn) that grabs data in a certain region of rom and
142 "The hardcoded names of the items in memory. List begins at ROM@045B7 " 145 splits it into a collection by 0x50. If rom is not supplied, uses the
143 (map character-codes->str 146 original rom data."
147 [start length]
148 (fn self
149 ([rom]
144 (take-nth 2 150 (take-nth 2
145 (partition-by #(= % 0x50) 151 (partition-by #(= % 0x50)
146 (take 1200 152 (take length
147 (drop 0x45B7 (rom (root)))))))) 153 (drop start rom)))))
154 ([]
155 (self com.aurellem.gb.gb-driver/original-rom))))
156
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))
163
164
165 ;; --------------------------------------------------
166
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))
148 171
149 (def hxc-types 172 (def hxc-types
150 "The hardcoded type names in memory. List begins at ROM@27D99, 173 "The hardcoded type names in memory. List begins at ROM@27D99,
151 shortly before hxc-titles." 174 shortly before hxc-titles."
152 (map character-codes->str 175 (hxc-thunk-words 0x27D99 102))
153 (take-nth 2
154 (partition-by #(= 0x50 %)
155 (take 102
156 (drop 0x27D99
157 (rom (root))))))))
158 176
159 (def hxc-titles 177 (def hxc-titles
160 "The hardcoded names of the trainer titles in memory. List begins at 178 "The hardcoded names of the trainer titles in memory. List begins at
161 ROM@27E77" 179 ROM@27E77"
162 (map character-codes->str 180 (hxc-thunk-words 0x27E77 196))
163 (take-nth 2
164 (partition-by #(= 0x50 %)
165 (take 196
166 (drop 0x27E77
167 (rom (root))))))))
168
169 181
170 (def hxc-places 182 (def hxc-places
171 "The hardcoded place names in memory. List begins at 183 "The hardcoded place names in memory. List begins at
172 ROM@71500. Cinnabar Mansion is dynamically calculated." 184 ROM@71500. [Cinnabar] Mansion seems to be dynamically calculated."
173 (map character-codes->str 185 (hxc-thunk-words 0x71500 560))
174 (take-nth 2 186
175 (partition-by #(= % 0x50) 187
176 (take 560 188 (defn hxc-dialog
177 (drop 0x71500 189 "The hardcoded dialogue in memory, including in-game alerts. Dialog
178 (rom (root)))))))) 190 seems to be separated by 0x57 instead of 0x50 (END). Begins at ROM@98000."
179 191 ([rom]
180 192 (map character-codes->str
181 (def hxc-dialog 193 (take-nth 2
182 "The hardcoded dialogue in memory, including in-game alerts. List begins at ROM@98000." 194 (partition-by #(= % 0x57)
183 (character-codes->str(take 0x0F728 195 (take 0x0F728
184 (drop (+ 0x98000) 196 (drop 0x98000 rom))))))
185 (rom (root)))))) 197 ([]
198 (hxc-dialog com.aurellem.gb.gb-driver/original-rom)))
199
186 200
187 (def hxc-pokedex 201 (def hxc-pokedex
188 "The hardcoded pokedex entries in memory. List begins at 202 "The hardcoded pokedex entries in memory. List begins at
189 ROM@B8000, shortly before move names." 203 ROM@B8000, shortly before move names."
190 (map character-codes->str 204 (hxc-thunk-words 0xB8000 14754))
191 (take-nth 2 205
192 (partition-by #(= % 0x50)
193 (take 14754
194 (drop 0xB8000
195 (rom (root))))))))
196 (def hxc-move-names 206 (def hxc-move-names
197 "The hardcoded move names in memory. List begins at ROM@BC000" 207 "The hardcoded move names in memory. List begins at ROM@BC000"
198 (map character-codes->str 208 (hxc-thunk-words 0xBC000 1551))
199 (take-nth 2 209
200 (partition-by #(= % 0x50) 210
201 (take 1551 211 (defn hxc-move-data
202 (drop 0xBC000
203 (rom (root))))))))
204
205 (def hxc-move-data
206 "The hardcoded (basic (move effects)) in memory. List begins at 212 "The hardcoded (basic (move effects)) in memory. List begins at
207 0x38000. Effect descriptions were handwritten and aren't hardcoded." 213 0x38000. Returns a map of {:name :power :accuracy :pp :fx-id
208 (let [names (vec hxc-move-names) 214 :fx-txt}. The move descriptions are handwritten, not hardcoded."
209 move-count (count names) 215 ([]
210 move-size 6 216 (hxc-move-data com.aurellem.gb.gb-driver/original-rom))
211 format-name (fn [s] 217 ([rom]
218 (let [names (vec (hxc-move-names rom))
219 move-count (count names)
220 move-size 6
221 format-name (fn [s]
212 (keyword (.toLowerCase 222 (keyword (.toLowerCase
213 (apply str 223 (apply str
214 (map #(if (= % \space) "-" %) s))))) 224 (map #(if (= % \space) "-" %) s)))))]
215 ] 225 (zipmap (map format-name names)
216 (zipmap (map format-name names) 226 (map
217 (map 227 (fn [[idx effect power type accuracy pp]]
218 (fn [[idx effect power type accuracy pp]] 228 {:name (names (dec idx))
219 {:name (names (dec idx)) 229 :power power
220 :power power 230 :accuracy accuracy
221 :accuracy (hex accuracy) 231 :pp pp
222 :pp pp 232 :fx-id effect
223 :fx-id (hex effect) 233 :fx-txt (get move-effects effect)
224 :fx-txt (get move-effects effect) 234 }
225 } 235 )
226 ) 236
227 237 (partition move-size
228 (partition move-size 238 (take (* move-size move-count)
229 (take (* move-size move-count) 239 (drop 0x38000 rom))))))))
230 (drop 0x38000 (rom(root)))))))))
231 240
232 241
233 242
234 (def hxc-pokenames 243 (defn hxc-move-data*
235 "The hardcoded names of the 190 species in memory. List begins at ROM@E8000." 244 "Like hxc-move-data, but reports numbers as hexadecimal symbols instead."
236 (let [count-species 190 245 ([]
237 name-length 10] 246 (hxc-move-data* com.aurellem.gb.gb-driver/original-rom))
238 (map character-codes->str 247 ([rom]
239 (partition name-length 248 (let [names (vec (hxc-move-names rom))
240 (take (* count-species name-length) 249 move-count (count names)
241 (drop 0xE8000 250 move-size 6
242 (rom(root)))))))) 251 format-name (fn [s]
243 252 (keyword (.toLowerCase
244 253 (apply str
245 254 (map #(if (= % \space) "-" %) s)))))
246 255 ]
247 256 (zipmap (map format-name names)
248 257 (map
249 258 (fn [[idx effect power type accuracy pp]]
250 259 {:name (names (dec idx))
251 260 :power power
252 (def hxc-advantage 261 :accuracy (hex accuracy)
253 "The hardcoded type advantages in memory, returned as tuples of [atk-type def-type multiplier]. By default (i.e. if not listed here), the multiplier is 1." 262 :pp pp
254 (map 263 :fx-id (hex effect)
255 (fn [[atk def mult]] [(get pkmn-types atk (hex atk)) 264 :fx-txt (get move-effects effect)
256 (get pkmn-types def (hex def)) 265 }
257 (/ mult 10)]) 266 )
258 (partition 3 267
259 (take-while (partial not= 0xFF) 268 (partition move-size
260 (drop 0x3E62D (rom(root))))))) 269 (take (* move-size move-count)
270 (drop 0x38000 rom))))))))
261 271
262 272
263 273
264 274 (defn hxc-pokenames
265 275 "The hardcoded names of the 190 species in memory. List begins at
266 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))))))))
289
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))))))
302
303
304
305
306
307
308
309
310
311
312 ;; ********************** MANIPULATION FNS
313
314
315
316
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)
326
327 :else false))
328
329
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))
333
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)))
341
342
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))))
352
353
267 354
268 355
269 356
270 357
271 358