changeset 68:86093f2ce7d1

got the speedrun to play
author Robert McIntyre <rlm@mit.edu>
date Thu, 08 Mar 2012 02:10:03 -0600
parents 8cb500493ec2
children 063b78119835
files clojure/com/aurellem/test_vba.clj clojure/com/aurellem/vba.clj clojure/com/aurellem/vbm.clj
diffstat 3 files changed, 73 insertions(+), 60 deletions(-) [+]
line wrap: on
line diff
     1.1 --- a/clojure/com/aurellem/test_vba.clj	Thu Mar 08 01:58:36 2012 -0600
     1.2 +++ b/clojure/com/aurellem/test_vba.clj	Thu Mar 08 02:10:03 2012 -0600
     1.3 @@ -1,6 +1,6 @@
     1.4  (ns com.aurellem.test-vba
     1.5    (:import java.io.File)
     1.6 -  (:use (com.aurellem vba gb-driver)))
     1.7 +  (:use (com.aurellem vbm gb-driver)))
     1.8  
     1.9  (def test-file (File."/home/r/proj/pokemon-escape/speedruns/rlm.vbm"))
    1.10  (def speedrun-2942
    1.11 @@ -106,7 +106,7 @@
    1.12  
    1.13                ;; rlm additions
    1.14  
    1.15 -              0
    1.16 +              0 0 
    1.17                ;; end additions
    1.18                
    1.19                0 0 0 0 0 0 0 0 0 0 0 0 0 0
     2.1 --- a/clojure/com/aurellem/vba.clj	Thu Mar 08 01:58:36 2012 -0600
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,58 +0,0 @@
     2.4 -(ns com.aurellem.vba
     2.5 -  (:import java.io.File)
     2.6 -  (:import org.apache.commons.io.FileUtils))
     2.7 -
     2.8 -(defn vba-bytes [#^File vba]
     2.9 -  (let [bytes (FileUtils/readFileToByteArray vba)
    2.10 -        ints (int-array (count bytes))]
    2.11 -    (areduce bytes idx _ nil
    2.12 -             (aset ints idx
    2.13 -                   (bit-and 0xFF (aget bytes idx))))
    2.14 -    ints))
    2.15 -
    2.16 -(def button-mask
    2.17 -  {;; main buttons
    2.18 -   :a         0x0001
    2.19 -   :b         0x0002
    2.20 -
    2.21 -   ;; directional pad
    2.22 -   :r         0x0010
    2.23 -   :l         0x0020
    2.24 -   :u         0x0040
    2.25 -   :d         0x0080
    2.26 -
    2.27 -   ;; meta buttons
    2.28 -   :select    0x0004
    2.29 -   :start     0x0008
    2.30 -
    2.31 -   ;; hard reset -- not really a button
    2.32 -   :reset   0x0800})
    2.33 -
    2.34 -(defn button-code [buttons]
    2.35 -  (reduce bit-or 0x0000 (map button-mask buttons)))
    2.36 -
    2.37 -(defn buttons [mask]
    2.38 -  (loop [buttons []
    2.39 -         masks (seq button-mask)]
    2.40 -    (if (empty? masks) buttons
    2.41 -        (let [[button value] (first masks)]
    2.42 -          (if (not= 0x0 (bit-and value mask))
    2.43 -            (recur (conj buttons button) (rest masks))
    2.44 -            (recur buttons (rest masks)))))))
    2.45 -
    2.46 -(def vba-header-length 257)
    2.47 -
    2.48 -(defn vba-masks [#^File vba]
    2.49 -  (map (fn [[a b]]
    2.50 -         (+ (bit-shift-left a 8) b))
    2.51 -       (partition
    2.52 -        2 (drop vba-header-length (vba-bytes vba)))))
    2.53 -
    2.54 -(defn vba-buttons [#^File vba]
    2.55 -  (map buttons (vba-masks vba)))
    2.56 -  
    2.57 -
    2.58 -
    2.59 -         
    2.60 -    
    2.61 -  
    2.62 \ No newline at end of file
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/clojure/com/aurellem/vbm.clj	Thu Mar 08 02:10:03 2012 -0600
     3.3 @@ -0,0 +1,71 @@
     3.4 +(ns com.aurellem.vbm
     3.5 +  (:import java.io.File)
     3.6 +  (:import org.apache.commons.io.FileUtils))
     3.7 +
     3.8 +(defn vbm-bytes [#^File vbm]
     3.9 +  (let [bytes (FileUtils/readFileToByteArray vbm)
    3.10 +        ints (int-array (count bytes))]
    3.11 +    (areduce bytes idx _ nil
    3.12 +             (aset ints idx
    3.13 +                   (bit-and 0xFF (aget bytes idx))))
    3.14 +    ints))
    3.15 +
    3.16 +(def button-mask
    3.17 +  {;; main buttons
    3.18 +   :a         0x0001
    3.19 +   :b         0x0002
    3.20 +
    3.21 +   ;; directional pad
    3.22 +   :r         0x0010
    3.23 +   :l         0x0020
    3.24 +   :u         0x0040
    3.25 +   :d         0x0080
    3.26 +
    3.27 +   ;; meta buttons
    3.28 +   :select    0x0004
    3.29 +   :start     0x0008
    3.30 +
    3.31 +   ;; hard reset -- not really a button
    3.32 +   :reset   0x0800})
    3.33 +
    3.34 +(defn button-code [buttons]
    3.35 +  (reduce bit-or 0x0000 (map button-mask buttons)))
    3.36 +
    3.37 +(defn buttons [mask]
    3.38 +  (loop [buttons []
    3.39 +         masks (seq button-mask)]
    3.40 +    (if (empty? masks) buttons
    3.41 +        (let [[button value] (first masks)]
    3.42 +          (if (not= 0x0000 (bit-and value mask))
    3.43 +            (recur (conj buttons button) (rest masks))
    3.44 +            (recur buttons (rest masks)))))))
    3.45 +
    3.46 +(def vbm-header-length 255)
    3.47 +
    3.48 +(defn repair-vbm
    3.49 +  "Two 0's must be inserted after every reset, and the first
    3.50 +   button must be dropped"
    3.51 +  [vbm-seq]
    3.52 +  (loop [fixed []
    3.53 +         pending (next vbm-seq)]
    3.54 +    (if (empty? pending) fixed
    3.55 +        (let [mask (first pending)]
    3.56 +          (if (not= 0x0000 (bit-and mask (button-mask :reset)))
    3.57 +            (recur (conj fixed mask 0x0000 0x0000) (next pending))
    3.58 +            (recur (conj fixed mask) (next pending)))))))
    3.59 +
    3.60 +(defn vbm-masks [#^File vbm]
    3.61 +  (repair-vbm
    3.62 +   (map (fn [[a b]]
    3.63 +          (+ (bit-shift-left a 8) b))
    3.64 +        (partition
    3.65 +         2 (drop vbm-header-length (vbm-bytes vbm))))))
    3.66 +
    3.67 +(defn vbm-buttons [#^File vbm]
    3.68 +  (map buttons (vbm-masks vbm)))
    3.69 +  
    3.70 +
    3.71 +
    3.72 +         
    3.73 +    
    3.74 +  
    3.75 \ No newline at end of file