# HG changeset patch
# User Robert McIntyre <rlm@mit.edu>
# Date 1332311830 18000
# Node ID 4857f59f63a67dd9743d5ae5b5b9d87d28378d19
# Parent  04dfda91db9c609f0b5767931a8c3e3fd8d75335
added functions to read and set DV values for pokemon.

diff -r 04dfda91db9c -r 4857f59f63a6 clojure/com/aurellem/exp/pokemon-layout
--- a/clojure/com/aurellem/exp/pokemon-layout	Tue Mar 20 20:26:00 2012 -0500
+++ b/clojure/com/aurellem/exp/pokemon-layout	Wed Mar 21 01:37:10 2012 -0500
@@ -33,8 +33,8 @@
 D182                   \l	  				    
 D183                   /h Special Stat Experience
 D184                   \l				    
-D185                     				    
-D186                    				    
+D185                   Diversification -- Defense, Attack (HP)
+D186                   Diversification -- Special, Speed (HP)  
 D187                   Current/Maximum PP Move 1		    
 D188                   Current/Maximum PP Move 2		    
 D189                   Current/Maximum PP Move 3		    
@@ -77,8 +77,8 @@
 D1AE                   \l	 			      
 D1AF                   /h Special Stat Experience
 D1B0                   \l				      
-D1B1                     				      
-D1B2                    				      
+D1B1                   Diversification -- Defense, Attack (HP)   
+D1B2                   Diversification -- Special, Speed (HP)       
 D1B3                   Current/Maximum PP Move 1	      
 D1B4                   Current/Maximum PP Move 2	      
 D1B5                   Current/Maximum PP Move 3	      
@@ -121,8 +121,8 @@
 D1DA                   \l	  				    
 D1DB                   /h Special Stat Experience
 D1DC                   \l				    
-D1DD                     				    
-D1DE                    				    
+D1DD                   Diversification -- Defense, Attack (HP)	    
+D1DE                   Diversification -- Special, Speed (HP)     
 D1DF                   Current/Maximum PP Move 1		    
 D1E0                   Current/Maximum PP Move 2		    
 D1E1                   Current/Maximum PP Move 3		    
@@ -165,8 +165,8 @@
 D206                   \l	  				    
 D207                   /h Special Stat Experience
 D208                   \l				    
-D209                     				    
-D20A                    				    
+D209                   Diversification -- Defense, Attack (HP)	    
+D20A                   Diversification -- Special, Speed (HP)	    
 D20B                   Current/Maximum PP Move 1		    
 D20C                   Current/Maximum PP Move 2		    
 D20D                   Current/Maximum PP Move 3		    
@@ -209,8 +209,8 @@
 D232                   \l	  				     
 D233                   /h Special Stat Experience
 D234                   \l				     
-D235                     				     
-D236                    				     
+D235                   Diversification -- Defense, Attack (HP)     
+D236                   Diversification -- Special, Speed (HP)    
 D237                   Current/Maximum PP Move 1		     
 D238                   Current/Maximum PP Move 2		     
 D239                   Current/Maximum PP Move 3		     
@@ -253,8 +253,8 @@
 D25E                   \l	  				     
 D25F                   /h Special Stat Experience
 D260                   \l				     
-D261                     				     
-D262                    				     
+D261                   Diversification -- Defense, Attack (HP)  
+D262                   Diversification -- Special, Speed (HP)  
 D263                   Current/Maximum PP Move 1		     
 D264                   Current/Maximum PP Move 2		     
 D265                   Current/Maximum PP Move 3		     
diff -r 04dfda91db9c -r 4857f59f63a6 clojure/com/aurellem/exp/pokemon.clj
--- a/clojure/com/aurellem/exp/pokemon.clj	Tue Mar 20 20:26:00 2012 -0500
+++ b/clojure/com/aurellem/exp/pokemon.clj	Wed Mar 21 01:37:10 2012 -0500
@@ -657,6 +657,193 @@
 (defn pre-battle [] (read-state "prepare-for-battle"))
 
 
+(defn pika-lvl-100-DV-0 []
+  (read-state "at-pc-lv-100-pikachu"))
 
+;; Performed following experiment:
+;; Raised Pikachu to Lvl 100 with rare-candies,
+;; then put it at the head of the party and
+;; set 0xD185 and 0xD186 to zero.
 
+;; then, for each pattern, deposited pikachu into
+;; Box 1 and immediately widthdrew it and observed the
+;; stats.
 
+
+;; Pikachu L:100 Base Stats with DVs = 0
+;; HP      :  187
+;; Attack  :  123
+;; Defense :   73
+;; Speed   :  194
+;; Special :  112
+
+;; 0xD185:
+
+;; 00000001
+;; Defense => 75  (+2)
+;; HP => 195      (+8)
+
+;; 00000010
+;; Defense => 77  (+4)
+
+;; 00000100
+;; Defense => 81  (+8)
+
+;; 00001000
+;; Defense => 89  (+16)
+
+;; 00010000
+;; HP => 203      (+16)
+;; Attack => 125  (+2)
+
+;; 00100000
+;; Attack => 127  (+4)
+
+;; 01000000
+;; Attack -> 131  (+8)
+
+;; 10000000
+;; Attack -> 139  (+16)
+
+;; 0xD186
+
+;; 00000001
+;; HP => 189      (+2)
+;; Special =>     (+2)
+
+;; 00000010
+;; Special => 116 (+4)
+
+;; 00000100
+;; Special => 120 (+8)
+
+;; 00001000
+;; Special => 128 (+16)
+
+;; 00010000
+;; HP => 191      (+4)
+;; Speed => 196   (+2)
+
+;; 00100000
+;; Speed => 198   (+4)
+
+;; 01000000
+;; Speed => 202   (+8)
+
+;; 10000000
+;; Speed => 210   (+16)
+
+(def pokemon-1-DV-start 0xD185)
+
+(defn pokemon-DV-start-point [poke-num]
+  (+ (* poke-num pokemon-record-width)
+     pokemon-1-DV-start))
+
+(def reverse-4-bit
+  {0   0
+   1   8
+   2   4
+   3   12
+   5   10
+   6   6
+   7   14
+   9   9
+   15  15
+   13  11
+
+   8   1 
+   4   2 
+   12  3 
+   10  5
+   11  13
+   14  7 })
+   
+(defn read-DV
+  ([^SaveState state poke-num]
+     (assert (<= 0 poke-num 5))
+     (let [[raw-DV-1
+            raw-DV-2]
+           (subvec (vec (memory state))
+                   (pokemon-DV-start-point poke-num)
+                   (+ 2 (pokemon-DV-start-point poke-num)))
+           defense-DV (bit-and raw-DV-1 0x0F)
+           attack-DV (bit-shift-right
+                       (bit-and raw-DV-1 0xF0)
+                        4)
+           special-DV (bit-and raw-DV-2 0x0F)
+           speed-DV (bit-shift-right
+                        (bit-and raw-DV-2 0xF0)
+                        4)
+           HP-DV
+           (+
+            (if (bit-test special-DV 0)   1 0)
+            (if (bit-test speed-DV 0)     2 0)
+            (if (bit-test defense-DV 0)   4 0)
+            (if (bit-test attack-DV 0)    8 0))]
+       {:attack  attack-DV
+        :defense  defense-DV
+        :speed  speed-DV
+        :special  special-DV
+        :hp  HP-DV}))
+  ([poke-num]
+     (read-DV @current-state poke-num)))
+     
+
+(defn give-DV
+  ([^SaveState state poke-num dv-values]
+     
+     (assert (<= 0 poke-num 5))
+     (map #(assert (<= 0 % 15)) (vals dv-values))
+     (let [raw-dv-1*
+           (+ (:defense dv-values)
+              (bit-shift-left (:attack dv-values) 4))
+           raw-dv-2*
+           (+ (:special dv-values)
+              (bit-shift-left (:speed dv-values) 4))
+           hp-dv (:hp dv-values)
+           hp-masks-1
+           [[0 (bit-test hp-dv 2)]
+            [4 (bit-test hp-dv 3)]]
+           hp-masks-2
+           [[0 (bit-test hp-dv 0)]
+            [4 (bit-test hp-dv 1)]]
+           set-hp-bits
+           (fn [init [index hp?]]
+             (if hp?
+               (bit-set init index)
+               (bit-clear init index)))
+
+           raw-dv-1 (reduce set-hp-bits raw-dv-1*
+                            hp-masks-1)
+                     
+           raw-dv-2 (reduce set-hp-bits raw-dv-2*
+                            hp-masks-2)
+           
+           dv-start (pokemon-DV-start-point poke-num)]
+
+       (if (or (not= raw-dv-1* raw-dv-1)
+               (not= raw-dv-2* raw-dv-2))
+         (println "Warning: inconsistent DV-values."
+                  "Using HP settings."))
+       
+       (set-memory 
+        (set-memory state dv-start raw-dv-1)
+        (inc dv-start) raw-dv-2)))
+  ([poke-num dv-values]
+     (give-DV @current-state poke-num dv-values)))
+
+(def good-DVs
+  {:attack  15
+   :defense 15
+   :speed   15
+   :special 15
+   :hp      15})
+
+(def bad-DVs
+  {:attack  0
+   :defense 0
+   :speed   0
+   :special 0
+   :hp      0})
+
+   
\ No newline at end of file
diff -r 04dfda91db9c -r 4857f59f63a6 save-states/at-pc-lv-100-pikachu.sav
Binary file save-states/at-pc-lv-100-pikachu.sav has changed