diff 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 diff
     1.1 --- a/clojure/com/aurellem/gb/hxc.clj	Mon Mar 26 03:09:26 2012 -0500
     1.2 +++ b/clojure/com/aurellem/gb/hxc.clj	Mon Mar 26 03:45:24 2012 -0500
     1.3 @@ -6,6 +6,9 @@
     1.4  
     1.5  
     1.6  
     1.7 +
     1.8 +; ************* HANDWRITTEN CONSTANTS
     1.9 +
    1.10  (def pkmn-types
    1.11    [:normal
    1.12     :fighting
    1.13 @@ -110,8 +113,8 @@
    1.14     "opponent evd -2"
    1.15     "doubles user spc when attacked"
    1.16     "doubles user def when attacked"
    1.17 -   "poisons opponent"   ;;acr taken from move acr
    1.18 -   "paralyzes opponent" ;;
    1.19 +   "just poisons opponent"   ;;acr taken from move acr
    1.20 +   "just paralyzes opponent" ;;
    1.21     "0x19 chance opponent atk -1"
    1.22     "0x19 chance opponent def -1"
    1.23     "0x19 chance opponent spd -1"
    1.24 @@ -135,133 +138,167 @@
    1.25     ])
    1.26  
    1.27  
    1.28 +;; ************** HARDCODED DATA
    1.29  
    1.30 +(defn hxc-thunk
    1.31 +  "Creates a thunk (unary fn) that grabs data in a certain region of rom and
    1.32 +splits it into a collection by 0x50. If rom is not supplied, uses the
    1.33 +  original rom data."
    1.34 +  [start length]
    1.35 +  (fn self
    1.36 +    ([rom]
    1.37 +       (take-nth 2
    1.38 +                 (partition-by #(= % 0x50)
    1.39 +                               (take length
    1.40 +                                     (drop start rom)))))
    1.41 +    ([]
    1.42 +      (self com.aurellem.gb.gb-driver/original-rom))))
    1.43  
    1.44 +(def hxc-thunk-words
    1.45 +  "Same as hxc-thunk, except it interprets the rom data as characters,
    1.46 +  returning a collection of strings."
    1.47 +  (comp
    1.48 +   (partial comp (partial map character-codes->str))
    1.49 +   hxc-thunk))
    1.50 +
    1.51 +
    1.52 +;; --------------------------------------------------
    1.53  
    1.54  (def hxc-items
    1.55 -  "The hardcoded names of the items in memory. List begins at ROM@045B7 "
    1.56 -  (map character-codes->str
    1.57 -       (take-nth 2
    1.58 -                 (partition-by #(= % 0x50)
    1.59 -                               (take 1200
    1.60 -                                     (drop 0x45B7 (rom (root))))))))
    1.61 +  "The hardcoded names of the items in memory. List begins at
    1.62 +ROM@045B7"
    1.63 +  (hxc-thunk-words 0x45B7 870))
    1.64  
    1.65  (def hxc-types
    1.66    "The hardcoded type names in memory. List begins at ROM@27D99,
    1.67    shortly before hxc-titles."
    1.68 -  (map character-codes->str
    1.69 -       (take-nth 2
    1.70 -                 (partition-by #(= 0x50 %)
    1.71 -                               (take 102
    1.72 -                                     (drop 0x27D99
    1.73 -                                           (rom (root))))))))
    1.74 +  (hxc-thunk-words 0x27D99 102))
    1.75  
    1.76  (def hxc-titles
    1.77    "The hardcoded names of the trainer titles in memory. List begins at
    1.78  ROM@27E77"
    1.79 -  (map character-codes->str
    1.80 -       (take-nth 2
    1.81 -                 (partition-by #(= 0x50 %)
    1.82 -                               (take 196
    1.83 -                                     (drop 0x27E77
    1.84 -                                           (rom (root))))))))
    1.85 -
    1.86 +  (hxc-thunk-words 0x27E77 196))
    1.87  
    1.88  (def hxc-places
    1.89    "The hardcoded place names in memory. List begins at
    1.90 -ROM@71500. Cinnabar Mansion is dynamically calculated."
    1.91 -  (map character-codes->str
    1.92 -       (take-nth 2 
    1.93 -                (partition-by #(= % 0x50)
    1.94 -                               (take 560
    1.95 -                                     (drop 0x71500
    1.96 -                                           (rom (root))))))))
    1.97 +ROM@71500. [Cinnabar] Mansion seems to be dynamically calculated."
    1.98 +  (hxc-thunk-words 0x71500 560))
    1.99  
   1.100  
   1.101 -(def hxc-dialog
   1.102 -  "The hardcoded dialogue in memory, including in-game alerts. List begins at ROM@98000."
   1.103 -(character-codes->str(take 0x0F728
   1.104 -                          (drop (+ 0x98000)
   1.105 -                                (rom (root))))))
   1.106 +(defn hxc-dialog
   1.107 +  "The hardcoded dialogue in memory, including in-game alerts. Dialog
   1.108 +  seems to be separated by 0x57 instead of 0x50 (END). Begins at ROM@98000."
   1.109 +  ([rom]
   1.110 +     (map character-codes->str
   1.111 +          (take-nth 2
   1.112 +                    (partition-by #(= % 0x57)
   1.113 +                                  (take 0x0F728
   1.114 +                                        (drop 0x98000 rom))))))
   1.115 +  ([]
   1.116 +     (hxc-dialog com.aurellem.gb.gb-driver/original-rom)))
   1.117 +
   1.118  
   1.119  (def hxc-pokedex
   1.120    "The hardcoded pokedex entries in memory. List begins at
   1.121  ROM@B8000, shortly before move names."
   1.122 -  (map character-codes->str
   1.123 -       (take-nth 2
   1.124 -                 (partition-by #(= % 0x50)
   1.125 -                               (take 14754
   1.126 -                                     (drop 0xB8000
   1.127 -                                           (rom (root))))))))
   1.128 +  (hxc-thunk-words 0xB8000 14754))
   1.129 +
   1.130  (def hxc-move-names
   1.131    "The hardcoded move names in memory. List begins at ROM@BC000"
   1.132 -  (map character-codes->str
   1.133 -       (take-nth 2
   1.134 -                 (partition-by #(= % 0x50)
   1.135 -                               (take 1551
   1.136 -                                     (drop 0xBC000
   1.137 -                                           (rom (root))))))))   
   1.138 +  (hxc-thunk-words 0xBC000 1551))
   1.139  
   1.140 -(def hxc-move-data
   1.141 +
   1.142 +(defn hxc-move-data
   1.143    "The hardcoded (basic (move effects)) in memory. List begins at
   1.144 -0x38000. Effect descriptions were handwritten and aren't hardcoded."
   1.145 -  (let [names (vec hxc-move-names)
   1.146 -        move-count (count names)
   1.147 -        move-size 6
   1.148 -        format-name (fn [s]
   1.149 +0x38000. Returns a map of {:name :power :accuracy :pp :fx-id
   1.150 +  :fx-txt}. The move descriptions are handwritten, not hardcoded."
   1.151 +  ([]
   1.152 +     (hxc-move-data com.aurellem.gb.gb-driver/original-rom))
   1.153 +  ([rom]
   1.154 +     (let [names (vec (hxc-move-names rom))
   1.155 +           move-count (count names)
   1.156 +           move-size 6
   1.157 +           format-name (fn [s]
   1.158                        (keyword (.toLowerCase
   1.159 -                       (apply str
   1.160 -                              (map #(if (= % \space) "-" %) s)))))
   1.161 -        ]
   1.162 -    (zipmap (map format-name names)
   1.163 -            (map
   1.164 -             (fn [[idx effect power type accuracy pp]]
   1.165 -               {:name (names (dec idx))
   1.166 -                :power power
   1.167 -                :accuracy (hex accuracy)
   1.168 -                :pp pp
   1.169 -                :fx-id (hex effect)
   1.170 -                :fx-txt (get move-effects effect) 
   1.171 -                }
   1.172 -               )
   1.173 -    
   1.174 -             (partition move-size
   1.175 -                        (take (* move-size move-count)
   1.176 -                              (drop 0x38000 (rom(root)))))))))
   1.177 +                                (apply str
   1.178 +                                       (map #(if (= % \space) "-" %) s)))))]
   1.179 +       (zipmap (map format-name names)
   1.180 +               (map
   1.181 +                (fn [[idx effect power type accuracy pp]]
   1.182 +                  {:name (names (dec idx))
   1.183 +                   :power power
   1.184 +                   :accuracy  accuracy
   1.185 +                   :pp pp
   1.186 +                   :fx-id effect
   1.187 +                   :fx-txt (get move-effects effect) 
   1.188 +                   }
   1.189 +                  )
   1.190 +                
   1.191 +                (partition move-size
   1.192 +                           (take (* move-size move-count)
   1.193 +                                 (drop 0x38000 rom))))))))
   1.194    
   1.195  
   1.196  
   1.197 -(def hxc-pokenames
   1.198 -  "The hardcoded names of the 190 species in memory. List begins at ROM@E8000."
   1.199 -  (let [count-species 190
   1.200 -        name-length 10]
   1.201 -    (map character-codes->str
   1.202 -         (partition name-length
   1.203 -                    (take (* count-species name-length)
   1.204 -                          (drop 0xE8000
   1.205 -                                (rom(root))))))))
   1.206 -
   1.207 -
   1.208 -
   1.209 -
   1.210 -
   1.211 -
   1.212 -
   1.213 -
   1.214 -
   1.215 -(def hxc-advantage
   1.216 -  "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."
   1.217 -  (map
   1.218 -   (fn [[atk def mult]] [(get pkmn-types atk (hex atk))
   1.219 -                         (get pkmn-types def (hex def))
   1.220 -                         (/ mult 10)])
   1.221 -   (partition 3
   1.222 -              (take-while (partial not= 0xFF)
   1.223 -                          (drop 0x3E62D (rom(root)))))))
   1.224 +(defn hxc-move-data*
   1.225 +  "Like hxc-move-data, but reports numbers as hexadecimal symbols instead."
   1.226 +  ([]
   1.227 +     (hxc-move-data* com.aurellem.gb.gb-driver/original-rom))
   1.228 +  ([rom]
   1.229 +     (let [names (vec (hxc-move-names rom))
   1.230 +           move-count (count names)
   1.231 +           move-size 6
   1.232 +           format-name (fn [s]
   1.233 +                         (keyword (.toLowerCase
   1.234 +                                   (apply str
   1.235 +                                          (map #(if (= % \space) "-" %) s)))))
   1.236 +           ]
   1.237 +       (zipmap (map format-name names)
   1.238 +               (map
   1.239 +                (fn [[idx effect power type accuracy pp]]
   1.240 +                  {:name (names (dec idx))
   1.241 +                   :power power
   1.242 +                   :accuracy (hex accuracy)
   1.243 +                   :pp pp
   1.244 +                   :fx-id (hex effect)
   1.245 +                   :fx-txt (get move-effects effect) 
   1.246 +                   }
   1.247 +                  )
   1.248 +                
   1.249 +                (partition move-size
   1.250 +                           (take (* move-size move-count)
   1.251 +                                 (drop 0x38000 rom))))))))
   1.252    
   1.253  
   1.254  
   1.255 +(defn hxc-pokenames
   1.256 +  "The hardcoded names of the 190 species in memory. List begins at
   1.257 +ROM@E8000. Although names in memory are padded with 0x50 to be 10 characters
   1.258 +  long, these names are stripped of padding."
   1.259 +  ([]
   1.260 +     (hxc-pokenames com.aurellem.gb.gb-driver/original-rom))
   1.261 +  ([rom]
   1.262 +     (let [count-species 190
   1.263 +           name-length 10]
   1.264 +       (map character-codes->str
   1.265 +            (partition name-length
   1.266 +                       (map #(if (= 0x50 %) 0x00 %)
   1.267 +                            (take (* count-species name-length)
   1.268 +                                  (drop 0xE8000
   1.269 +                                        rom))))))))
   1.270  
   1.271 +(defn hxc-advantage
   1.272 +  "The hardcoded type advantages in memory, returned as tuples of atk-type def-type multiplier. By default (i.e. if not listed here),
   1.273 +the multiplier is 1."
   1.274 +  ([] (hxc-advantage com.aurellem.gb.gb-driver/original-rom))
   1.275 +  ([rom]
   1.276 +     (map
   1.277 +      (fn [[atk def mult]] [(get pkmn-types atk (hex atk))
   1.278 +                            (get pkmn-types def (hex def))
   1.279 +                            (/ mult 10)])
   1.280 +      (partition 3
   1.281 +                 (take-while (partial not= 0xFF)
   1.282 +                             (drop 0x3E62D rom))))))
   1.283  
   1.284  
   1.285  
   1.286 @@ -272,6 +309,56 @@
   1.287  
   1.288  
   1.289  
   1.290 +;; ********************** MANIPULATION FNS
   1.291 +
   1.292 +
   1.293 +
   1.294 +
   1.295 +(defn submap?
   1.296 +  "Compares the two maps. Returns true if map-big has the same associations as map-small, otherwise false." 
   1.297 +  [map-small map-big]
   1.298 +  (cond (empty? map-small) true
   1.299 +        (and
   1.300 +         (contains? map-big (ffirst map-small))
   1.301 +         (= (get map-big (ffirst map-small))
   1.302 +            (second (first map-small))))
   1.303 +        (recur (next map-small) map-big)
   1.304 +
   1.305 +        :else false))  
   1.306 +
   1.307 +
   1.308 +(defn search-map [proto-map maps]
   1.309 +  "Returns all the maps that make the same associations as proto-map."
   1.310 +  (some (partial submap? proto-map) maps))
   1.311 +
   1.312 +;; (I don't use this for anything so far.)
   1.313 +;;
   1.314 +;; (defn filter-vals
   1.315 +;;   "Returns a map consisting of all the pairs [key val] for which (pred
   1.316 +;;   key) returns true."
   1.317 +;;   [pred map]
   1.318 +;;   (reduce (partial apply assoc) {} (filter (fn [[k v]] (pred v)) map)))
   1.319 +
   1.320 +
   1.321 +(defn search-moves
   1.322 +  "Returns a subcollection of all hardcoded moves with the given
   1.323 +  attributes. Attributes consist of :name :power :accuracy :pp :fx-id
   1.324 +  (and also :fx-txt, but it contains the same information as :fx-id)" 
   1.325 +  ([attribute-map]
   1.326 +     (search-moves com.aurellem.gb.gb-driver/original-rom attribute-map))
   1.327 +  ([rom attribute-map]
   1.328 +     (filter-vals (partial submap? attribute-map) (hxc-move-data
   1.329 +                                                   rom))))
   1.330 +
   1.331 +  
   1.332 +
   1.333 +
   1.334 +
   1.335 +
   1.336 +
   1.337 +
   1.338 +
   1.339 +
   1.340  
   1.341  ;; note for later: credits start at F1290
   1.342