view 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 source
1 (ns com.aurellem.gb.experience
2 (:use (com.aurellem.gb gb-driver util constants))
3 (:import [com.aurellem.gb.gb_driver SaveState]))
5 (def experience-pokemon-1 0xD178)
7 (defn experience-start-address [poke-num]
8 (+ experience-pokemon-1
9 (* pokemon-record-width poke-num)))
11 (def experience-record-width 13)
13 (defn read-experience
14 ([^SaveState state poke-num]
15 (let [start (experience-start-address poke-num)
16 [exp-h
17 exp-m
18 exp-l
19 hp-exp-h
20 hp-exp-l
21 attack-exp-h
22 attack-exp-l
23 defense-exp-h
24 defense-exp-l
25 speed-exp-h
26 speed-exp-l
27 special-exp-h
28 special-exp-l]
29 (subvec (vec (memory state))
30 start (+ experience-record-width start))
31 glue-bytes (fn [l h]
32 (+ l (bit-shift-left h 8)))]
33 {:main-exp (+ (glue-bytes exp-l exp-m)
34 (bit-shift-left exp-h 16))
35 :hp-exp (glue-bytes hp-exp-l hp-exp-h)
36 :attack-exp (glue-bytes attack-exp-l attack-exp-h)
37 :defense-exp (glue-bytes defense-exp-l defense-exp-h)
38 :speed-exp (glue-bytes speed-exp-l speed-exp-h)
39 :special-exp (glue-bytes special-exp-l special-exp-h)}))
40 ([poke-num]
41 (read-experience @current-state poke-num)))
43 (defn give-experience
44 ([^SaveState state poke-num exp]
45 (let [exp* (merge (read-experience state poke-num)
46 exp)
48 disect-bytes
49 (fn [exp]
50 [(bit-shift-right
51 (bit-and exp 0xFF00) 8)
52 (bit-and exp 0xFF)])
54 raw-exp-data
55 (flatten
56 [(bit-shift-right (bit-and (:main-exp exp*) 0xFF0000) 16)
57 (disect-bytes (:main-exp exp*))
58 (disect-bytes (:hp-exp exp*))
59 (disect-bytes (:attack-exp exp*))
60 (disect-bytes (:defense-exp exp*))
61 (disect-bytes (:speed-exp exp*))
62 (disect-bytes (:special-exp exp*))])]
63 (set-memory-range state
64 (experience-start-address poke-num)
65 raw-exp-data)))
66 ([poke-num exp]
67 (give-experience @current-state poke-num exp)))