diff clojure/com/aurellem/gb/dv.clj @ 176:95b2758dd517

wrote functions to read and write pokemon DV values and status
author Robert McIntyre <rlm@mit.edu>
date Wed, 21 Mar 2012 22:13:43 -0500
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/clojure/com/aurellem/gb/dv.clj	Wed Mar 21 22:13:43 2012 -0500
     1.3 @@ -0,0 +1,97 @@
     1.4 +(ns com.aurellem.gb.dv
     1.5 +  (:use (com.aurellem.gb gb-driver util constants))
     1.6 +  (:import [com.aurellem.gb.gb_driver SaveState]))
     1.7 +
     1.8 +(def pokemon-1-DV-start 0xD185)
     1.9 +
    1.10 +(defn pokemon-DV-start-point [poke-num]
    1.11 +  (+ (* poke-num pokemon-record-width)
    1.12 +     pokemon-1-DV-start))
    1.13 +   
    1.14 +(defn read-DV
    1.15 +  ([^SaveState state poke-num]
    1.16 +     (assert (<= 0 poke-num 5))
    1.17 +     (let [[raw-DV-1
    1.18 +            raw-DV-2]
    1.19 +           (subvec (vec (memory state))
    1.20 +                   (pokemon-DV-start-point poke-num)
    1.21 +                   (+ 2 (pokemon-DV-start-point poke-num)))
    1.22 +           defense-DV (bit-and raw-DV-1 0x0F)
    1.23 +           attack-DV (bit-shift-right
    1.24 +                       (bit-and raw-DV-1 0xF0)
    1.25 +                        4)
    1.26 +           special-DV (bit-and raw-DV-2 0x0F)
    1.27 +           speed-DV (bit-shift-right
    1.28 +                        (bit-and raw-DV-2 0xF0)
    1.29 +                        4)
    1.30 +           HP-DV
    1.31 +           (+
    1.32 +            (if (bit-test special-DV 0)   1 0)
    1.33 +            (if (bit-test speed-DV 0)     2 0)
    1.34 +            (if (bit-test defense-DV 0)   4 0)
    1.35 +            (if (bit-test attack-DV 0)    8 0))]
    1.36 +       {:attack  attack-DV
    1.37 +        :defense  defense-DV
    1.38 +        :speed  speed-DV
    1.39 +        :special  special-DV
    1.40 +        :hp  HP-DV}))
    1.41 +  ([poke-num]
    1.42 +     (read-DV @current-state poke-num)))
    1.43 +     
    1.44 +
    1.45 +(defn give-DV
    1.46 +  ([^SaveState state poke-num dv-values]
    1.47 +     
    1.48 +     (assert (<= 0 poke-num 5))
    1.49 +     (map #(assert (<= 0 % 15)) (vals dv-values))
    1.50 +     (let [raw-dv-1*
    1.51 +           (+ (:defense dv-values)
    1.52 +              (bit-shift-left (:attack dv-values) 4))
    1.53 +           raw-dv-2*
    1.54 +           (+ (:special dv-values)
    1.55 +              (bit-shift-left (:speed dv-values) 4))
    1.56 +           hp-dv (:hp dv-values)
    1.57 +           hp-masks-1
    1.58 +           [[0 (bit-test hp-dv 2)]
    1.59 +            [4 (bit-test hp-dv 3)]]
    1.60 +           hp-masks-2
    1.61 +           [[0 (bit-test hp-dv 0)]
    1.62 +            [4 (bit-test hp-dv 1)]]
    1.63 +           set-hp-bits
    1.64 +           (fn [init [index hp?]]
    1.65 +             (if hp?
    1.66 +               (bit-set init index)
    1.67 +               (bit-clear init index)))
    1.68 +
    1.69 +           raw-dv-1 (reduce set-hp-bits raw-dv-1*
    1.70 +                            hp-masks-1)
    1.71 +                     
    1.72 +           raw-dv-2 (reduce set-hp-bits raw-dv-2*
    1.73 +                            hp-masks-2)
    1.74 +           
    1.75 +           dv-start (pokemon-DV-start-point poke-num)]
    1.76 +
    1.77 +       (if (or (not= raw-dv-1* raw-dv-1)
    1.78 +               (not= raw-dv-2* raw-dv-2))
    1.79 +         (println "Warning: inconsistent DV-values."
    1.80 +                  "Using HP settings."))
    1.81 +       
    1.82 +       (set-memory 
    1.83 +        (set-memory state dv-start raw-dv-1)
    1.84 +        (inc dv-start) raw-dv-2)))
    1.85 +  ([poke-num dv-values]
    1.86 +     (give-DV @current-state poke-num dv-values)))
    1.87 +
    1.88 +(def good-DVs
    1.89 +  {:attack  15
    1.90 +   :defense 15
    1.91 +   :speed   15
    1.92 +   :special 15
    1.93 +   :hp      15})
    1.94 +
    1.95 +(def bad-DVs
    1.96 +  {:attack  0
    1.97 +   :defense 0
    1.98 +   :speed   0
    1.99 +   :special 0
   1.100 +   :hp      0})