diff clojure/com/aurellem/gb/money.clj @ 224:10e26e7ceedb

added functions for setting current money.
author Robert McIntyre <rlm@mit.edu>
date Sat, 24 Mar 2012 04:29:37 -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/money.clj	Sat Mar 24 04:29:37 2012 -0500
     1.3 @@ -0,0 +1,31 @@
     1.4 +(ns com.aurellem.gb.money
     1.5 +  (:use (com.aurellem.gb gb-driver util))
     1.6 +  (:import [com.aurellem.gb.gb_driver SaveState]))
     1.7 +
     1.8 +(def money-address 0xD346)
     1.9 +
    1.10 +(defn read-money
    1.11 +  "lol money is stored in BCD."
    1.12 +  ([^SaveState state]
    1.13 +     (Integer/parseInt 
    1.14 +      (apply str
    1.15 +             (map
    1.16 +              #(Integer/toHexString %)
    1.17 +              (subvec (vec (memory state)) money-address
    1.18 +                      (+ money-address 3)))) 10))
    1.19 +     ([] (read-money @current-state)))
    1.20 +
    1.21 +(defn set-money
    1.22 +  ([^SaveState state new-money]
    1.23 +     (let [str-money (str new-money)]
    1.24 +       (assert (<= 0 (count str-money) 6))
    1.25 +       (set-memory-range
    1.26 +        state
    1.27 +        money-address
    1.28 +        (map #(Integer/parseInt % 16)
    1.29 +             (map (partial apply str)
    1.30 +                  (partition
    1.31 +                   2
    1.32 +                   (concat (repeat (- 6 (count str-money)) "0")
    1.33 +                           (vec str-money))))))))
    1.34 +  ([new-money] (set-money @current-state new-money)))