view clojure/com/aurellem/gb/stats.clj @ 553:0901694725f0

saving progress for the night.
author Robert McIntyre <rlm@mit.edu>
date Thu, 30 Aug 2012 14:11:11 -0500
parents 912496041f98
children
line wrap: on
line source
1 (ns com.aurellem.gb.stats
2 (:use (com.aurellem.gb gb-driver util constants))
3 (:import [com.aurellem.gb.gb_driver SaveState]))
5 (def pokemon-1-stats-start-address 0xD18B)
7 (defn pokemon-stats-address [poke-num]
8 (+ pokemon-1-stats-start-address
9 (* poke-num pokemon-record-width)))
11 (def stats-record-size 11)
13 (def pokemon-1-current-hp-address 0xD16B)
15 (defn current-hp-address [poke-num]
16 (+ pokemon-1-current-hp-address
17 (* pokemon-record-width poke-num)))
19 (defn read-current-hp
20 ([^SaveState state poke-num]
21 (let [mem (memory state)
22 start (current-hp-address poke-num)
23 hp-h (aget mem start)
24 hp-l (aget mem (inc start))]
25 (glue-bytes hp-h hp-l)))
26 ([poke-num]
27 (read-current-hp @current-state poke-num)))
29 (defn set-current-hp
30 ([^SaveState state poke-num new-hp]
31 (set-memory-range state (current-hp-address poke-num)
32 (disect-bytes-2 new-hp)))
33 ([poke-num new-hp]
34 (set-current-hp @current-state poke-num new-hp)))
36 (defn read-stats
37 ([^SaveState state poke-num]
38 (let [start (pokemon-stats-address poke-num)
39 [level
40 hp-h
41 hp-l
42 attack-h
43 attack-l
44 defense-h
45 defense-l
46 speed-h
47 speed-l
48 special-h
49 special-l]
50 (subvec (vec (memory state))
51 start (+ start stats-record-size ))]
52 {:level level
53 :current-hp (read-current-hp state poke-num)
54 :hp (glue-bytes hp-h hp-l)
55 :attack (glue-bytes attack-h attack-l)
56 :defense (glue-bytes defense-h defense-l)
57 :speed (glue-bytes speed-h speed-l)
58 :special (glue-bytes special-h special-l)}))
59 ([poke-num]
60 (read-stats @current-state poke-num)))
62 (defn give-stats
63 ([^SaveState state poke-num new-stats]
64 (let [new-stats* (merge (read-stats state poke-num)
65 new-stats)
66 raw-stats
67 (flatten
68 [(:level new-stats*)
69 (disect-bytes-2 (:hp new-stats*))
70 (disect-bytes-2 (:attack new-stats*))
71 (disect-bytes-2 (:defense new-stats*))
72 (disect-bytes-2 (:speed new-stats*))
73 (disect-bytes-2 (:special new-stats*))])]
74 (set-current-hp
75 (set-memory-range state (pokemon-stats-address poke-num)
76 raw-stats)
77 poke-num
78 (:current-hp new-stats*))))
79 ([poke-num new-stats]
80 (give-stats @current-state poke-num new-stats)))