view clojure/com/aurellem/gb/dv.clj @ 316:d263df762c59

greatly speed up scroll-text by using binary-search.
author Robert McIntyre <rlm@mit.edu>
date Mon, 02 Apr 2012 21:20:54 -0500
parents 95b2758dd517
children
line wrap: on
line source
1 (ns com.aurellem.gb.dv
2 (:use (com.aurellem.gb gb-driver util constants))
3 (:import [com.aurellem.gb.gb_driver SaveState]))
5 (def pokemon-1-DV-start 0xD185)
7 (defn pokemon-DV-start-point [poke-num]
8 (+ (* poke-num pokemon-record-width)
9 pokemon-1-DV-start))
11 (defn read-DV
12 ([^SaveState state poke-num]
13 (assert (<= 0 poke-num 5))
14 (let [[raw-DV-1
15 raw-DV-2]
16 (subvec (vec (memory state))
17 (pokemon-DV-start-point poke-num)
18 (+ 2 (pokemon-DV-start-point poke-num)))
19 defense-DV (bit-and raw-DV-1 0x0F)
20 attack-DV (bit-shift-right
21 (bit-and raw-DV-1 0xF0)
22 4)
23 special-DV (bit-and raw-DV-2 0x0F)
24 speed-DV (bit-shift-right
25 (bit-and raw-DV-2 0xF0)
26 4)
27 HP-DV
28 (+
29 (if (bit-test special-DV 0) 1 0)
30 (if (bit-test speed-DV 0) 2 0)
31 (if (bit-test defense-DV 0) 4 0)
32 (if (bit-test attack-DV 0) 8 0))]
33 {:attack attack-DV
34 :defense defense-DV
35 :speed speed-DV
36 :special special-DV
37 :hp HP-DV}))
38 ([poke-num]
39 (read-DV @current-state poke-num)))
42 (defn give-DV
43 ([^SaveState state poke-num dv-values]
45 (assert (<= 0 poke-num 5))
46 (map #(assert (<= 0 % 15)) (vals dv-values))
47 (let [raw-dv-1*
48 (+ (:defense dv-values)
49 (bit-shift-left (:attack dv-values) 4))
50 raw-dv-2*
51 (+ (:special dv-values)
52 (bit-shift-left (:speed dv-values) 4))
53 hp-dv (:hp dv-values)
54 hp-masks-1
55 [[0 (bit-test hp-dv 2)]
56 [4 (bit-test hp-dv 3)]]
57 hp-masks-2
58 [[0 (bit-test hp-dv 0)]
59 [4 (bit-test hp-dv 1)]]
60 set-hp-bits
61 (fn [init [index hp?]]
62 (if hp?
63 (bit-set init index)
64 (bit-clear init index)))
66 raw-dv-1 (reduce set-hp-bits raw-dv-1*
67 hp-masks-1)
69 raw-dv-2 (reduce set-hp-bits raw-dv-2*
70 hp-masks-2)
72 dv-start (pokemon-DV-start-point poke-num)]
74 (if (or (not= raw-dv-1* raw-dv-1)
75 (not= raw-dv-2* raw-dv-2))
76 (println "Warning: inconsistent DV-values."
77 "Using HP settings."))
79 (set-memory
80 (set-memory state dv-start raw-dv-1)
81 (inc dv-start) raw-dv-2)))
82 ([poke-num dv-values]
83 (give-DV @current-state poke-num dv-values)))
85 (def good-DVs
86 {:attack 15
87 :defense 15
88 :speed 15
89 :special 15
90 :hp 15})
92 (def bad-DVs
93 {:attack 0
94 :defense 0
95 :speed 0
96 :special 0
97 :hp 0})