annotate clojure/com/aurellem/gb/dv.clj @ 280:d5e5c73af7e6

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