diff clojure/com/aurellem/gb/experience.clj @ 191:893c753f8088

added function to set ROM
author Robert McIntyre <rlm@mit.edu>
date Thu, 22 Mar 2012 20:10:09 -0500
parents
children fd549c8f42ae
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/clojure/com/aurellem/gb/experience.clj	Thu Mar 22 20:10:09 2012 -0500
     1.3 @@ -0,0 +1,67 @@
     1.4 +(ns com.aurellem.gb.experience
     1.5 +  (:use (com.aurellem.gb gb-driver util constants))
     1.6 +  (:import [com.aurellem.gb.gb_driver SaveState]))
     1.7 +
     1.8 +(def experience-pokemon-1 0xD178)
     1.9 +
    1.10 +(defn experience-start-address [poke-num]
    1.11 +  (+ experience-pokemon-1
    1.12 +     (* pokemon-record-width poke-num)))
    1.13 +
    1.14 +(def experience-record-width 13)
    1.15 +
    1.16 +(defn read-experience
    1.17 +  ([^SaveState state poke-num]
    1.18 +     (let [start (experience-start-address poke-num)
    1.19 +           [exp-h
    1.20 +            exp-m
    1.21 +            exp-l
    1.22 +            hp-exp-h
    1.23 +            hp-exp-l
    1.24 +            attack-exp-h
    1.25 +            attack-exp-l
    1.26 +            defense-exp-h
    1.27 +            defense-exp-l
    1.28 +            speed-exp-h
    1.29 +            speed-exp-l
    1.30 +            special-exp-h
    1.31 +            special-exp-l]
    1.32 +           (subvec (vec (memory state))
    1.33 +                   start (+ experience-record-width start))
    1.34 +           glue-bytes (fn [l h]
    1.35 +                        (+ l (bit-shift-left h 8)))]
    1.36 +       {:main-exp (+ (glue-bytes exp-l exp-m)
    1.37 +                     (bit-shift-left exp-h 16))
    1.38 +        :hp-exp (glue-bytes hp-exp-l hp-exp-h)
    1.39 +        :attack-exp (glue-bytes attack-exp-l attack-exp-h)
    1.40 +        :defense-exp (glue-bytes defense-exp-l defense-exp-h)
    1.41 +        :speed-exp (glue-bytes speed-exp-l speed-exp-h)
    1.42 +        :special-exp (glue-bytes special-exp-l special-exp-h)}))
    1.43 +  ([poke-num]
    1.44 +     (read-experience @current-state poke-num)))
    1.45 +
    1.46 +(defn give-experience
    1.47 +  ([^SaveState state poke-num exp]
    1.48 +     (let [exp* (merge (read-experience state poke-num)
    1.49 +                       exp)
    1.50 +
    1.51 +           disect-bytes
    1.52 +           (fn [exp]
    1.53 +             [(bit-shift-right
    1.54 +               (bit-and exp 0xFF00) 8)
    1.55 +              (bit-and exp 0xFF)])
    1.56 +
    1.57 +           raw-exp-data
    1.58 +           (flatten
    1.59 +            [(bit-shift-right (bit-and (:main-exp exp*) 0xFF0000) 16)
    1.60 +             (disect-bytes (:main-exp exp*))
    1.61 +             (disect-bytes (:hp-exp exp*))
    1.62 +             (disect-bytes (:attack-exp exp*))
    1.63 +             (disect-bytes (:defense-exp exp*))
    1.64 +             (disect-bytes (:speed-exp exp*))
    1.65 +             (disect-bytes (:special-exp exp*))])]
    1.66 +       (set-memory-range state 
    1.67 +                         (experience-start-address poke-num)
    1.68 +                         raw-exp-data)))
    1.69 +  ([poke-num exp]
    1.70 +     (give-experience @current-state poke-num exp)))