changeset 224:10e26e7ceedb

added functions for setting current money.
author Robert McIntyre <rlm@mit.edu>
date Sat, 24 Mar 2012 04:29:37 -0500
parents 3dfb9dd3db9d
children 0587f266a735
files clojure/com/aurellem/exp/cartography clojure/com/aurellem/gb/money.clj
diffstat 2 files changed, 35 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
     1.1 --- a/clojure/com/aurellem/exp/cartography	Sat Mar 24 03:35:21 2012 -0500
     1.2 +++ b/clojure/com/aurellem/exp/cartography	Sat Mar 24 04:29:37 2012 -0500
     1.3 @@ -481,10 +481,10 @@
     1.4  D342                   Item 19 Quantity
     1.5  D343                   Item 20 Type    
     1.6  D344                   Item 20 Quantity
     1.7 -D345                   
     1.8 -D346                   
     1.9 -D347                   
    1.10 -D348                   
    1.11 +D345                   Item End-of-List Sentinel
    1.12 +D346                   /h (bcd) money
    1.13 +D347                   |m (bcd) money
    1.14 +D348                   \l (bcd) money
    1.15  D349                   
    1.16  D34A                   
    1.17  D34B                   
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/clojure/com/aurellem/gb/money.clj	Sat Mar 24 04:29:37 2012 -0500
     2.3 @@ -0,0 +1,31 @@
     2.4 +(ns com.aurellem.gb.money
     2.5 +  (:use (com.aurellem.gb gb-driver util))
     2.6 +  (:import [com.aurellem.gb.gb_driver SaveState]))
     2.7 +
     2.8 +(def money-address 0xD346)
     2.9 +
    2.10 +(defn read-money
    2.11 +  "lol money is stored in BCD."
    2.12 +  ([^SaveState state]
    2.13 +     (Integer/parseInt 
    2.14 +      (apply str
    2.15 +             (map
    2.16 +              #(Integer/toHexString %)
    2.17 +              (subvec (vec (memory state)) money-address
    2.18 +                      (+ money-address 3)))) 10))
    2.19 +     ([] (read-money @current-state)))
    2.20 +
    2.21 +(defn set-money
    2.22 +  ([^SaveState state new-money]
    2.23 +     (let [str-money (str new-money)]
    2.24 +       (assert (<= 0 (count str-money) 6))
    2.25 +       (set-memory-range
    2.26 +        state
    2.27 +        money-address
    2.28 +        (map #(Integer/parseInt % 16)
    2.29 +             (map (partial apply str)
    2.30 +                  (partition
    2.31 +                   2
    2.32 +                   (concat (repeat (- 6 (count str-money)) "0")
    2.33 +                           (vec str-money))))))))
    2.34 +  ([new-money] (set-money @current-state new-money)))