changeset 88:65c2854c5875

can now save moves and states and am ready to continue past the title
author Robert McIntyre <rlm@mit.edu>
date Sat, 10 Mar 2012 15:36:26 -0600
parents e8855121f413
children 2f478abe57d0
files clojure/com/aurellem/gb_driver.clj clojure/com/aurellem/speedruns.clj clojure/com/aurellem/title.clj clojure/com/aurellem/vbm.clj
diffstat 4 files changed, 64 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
     1.1 --- a/clojure/com/aurellem/gb_driver.clj	Sat Mar 10 14:48:17 2012 -0600
     1.2 +++ b/clojure/com/aurellem/gb_driver.clj	Sat Mar 10 15:36:26 2012 -0600
     1.3 @@ -13,16 +13,16 @@
     1.4  (defn frame->filename [frame]
     1.5    (File. *save-state-cache* (format "%07d.sav" frame)))
     1.6    
     1.7 -(defn write-save! [^SaveState save]
     1.8 +(defn write-state! [^SaveState save]
     1.9    (let [buf (:data save)
    1.10          bytes (byte-array (.limit buf))
    1.11          dest (frame->filename (:frame save))]
    1.12      (.get buf bytes)
    1.13      (FileUtils/writeByteArrayToFile dest bytes)
    1.14      (.rewind buf)
    1.15 -    save))
    1.16 +    dest))
    1.17  
    1.18 -(defn read-save [frame]
    1.19 +(defn read-state [frame]
    1.20    (let [save (frame->filename frame)]
    1.21      (if (.exists save)
    1.22        (let [buf (Gb/saveBuffer)
    1.23 @@ -54,11 +54,13 @@
    1.24  ;;; The first state!
    1.25  (defn gen-root! []
    1.26    (restart!)
    1.27 -  (write-save! (SaveState. 0  (Gb/saveState))))
    1.28 +  (let [state (SaveState. 0  (Gb/saveState))]
    1.29 +    (write-state! state)
    1.30 +    state))
    1.31  
    1.32  (defn root []
    1.33    (if (.exists (frame->filename 0))
    1.34 -    (read-save 0)
    1.35 +    (read-state 0)
    1.36      (gen-root!)))
    1.37  
    1.38  ;;;; Press Buttons
    1.39 @@ -88,7 +90,6 @@
    1.40  
    1.41  (def current-state (atom nil))
    1.42  
    1.43 -
    1.44  (defn set-state! [^SaveState state]
    1.45    (assert (:data state) "Not a valid state!")
    1.46    (if (not @on?) (restart!))
    1.47 @@ -112,7 +113,6 @@
    1.48    [^Move move buttons]
    1.49    (Move. (step (:state move) buttons) buttons))
    1.50  
    1.51 -
    1.52  (defn play
    1.53    ([^SaveState state n]
    1.54       (reduce (fn [s _] (step s)) state (range n)))
     2.1 --- a/clojure/com/aurellem/speedruns.clj	Sat Mar 10 14:48:17 2012 -0600
     2.2 +++ b/clojure/com/aurellem/speedruns.clj	Sat Mar 10 15:36:26 2012 -0600
     2.3 @@ -1,6 +1,5 @@
     2.4 -(ns com.aurellem.test-vbm
     2.5 -  (:import java.io.File)
     2.6 -  (:use (com.aurellem vbm gb-driver)))
     2.7 +(ns com.aurellem.speedruns
     2.8 +  (:import java.io.File))
     2.9  
    2.10  (def speedrun-2942
    2.11    (File. "/home/r/proj/pokemon-escape/speedruns/yellow-2942.vbm"))
    2.12 @@ -11,3 +10,8 @@
    2.13  (def speedrun-2771
    2.14    (File. "/home/r/proj/pokemon-escape/speedruns/yellow-2771.vbm"))
    2.15  
    2.16 +(def broken-speedrun-1958
    2.17 +  (File. "/home/r/proj/pokemon-escape/speedruns/yellow-1958[bad].vbm"))
    2.18 +
    2.19 +(def broken-speedrun-3256
    2.20 +  (File. "/home/r/proj/pokemon-escape/speedruns/yellow-3256[bad].vbm"))
    2.21 \ No newline at end of file
     3.1 --- a/clojure/com/aurellem/title.clj	Sat Mar 10 14:48:17 2012 -0600
     3.2 +++ b/clojure/com/aurellem/title.clj	Sat Mar 10 15:36:26 2012 -0600
     3.3 @@ -25,14 +25,14 @@
     3.4  
     3.5  (defn start [] [[] (root)])
     3.6  
     3.7 -(defn-memo title [start]
     3.8 -  (->> start
     3.9 +(defn-memo title []
    3.10 +  (->> (start)
    3.11         (advance [] [:a])
    3.12         (advance [] [:start])
    3.13         (advance [] [:a])
    3.14         (advance [] [:start])))
    3.15  
    3.16 -(defn-memo oak [start]
    3.17 +(defn-memo oak []
    3.18    (->> (title)
    3.19         scroll-text
    3.20         scroll-text
    3.21 @@ -83,8 +83,19 @@
    3.22         scroll-text
    3.23         scroll-text))
    3.24  
    3.25 -(defn-memo intro []
    3.26 -  (-> (start) title oak name-entry rival-name-entry finish-title))
    3.27 +(defn write-intro! []
    3.28 +  (let [[moves state] (finish-title)]
    3.29 +    [(write-moves! moves) (write-state! state)]))
    3.30 +
    3.31 +(def title-frames 2323)
    3.32 +
    3.33 +(defn intro []
    3.34 +  [(read-moves title-frames)
    3.35 +   (read-state title-frames)])
    3.36 +
    3.37 +(defn test-intro []
    3.38 +  (play-vbm (moves->filename title-frames)))
    3.39 +
    3.40  
    3.41  
    3.42  ;; TODO might be able to glue these together more elegantly with monads
     4.1 --- a/clojure/com/aurellem/vbm.clj	Sat Mar 10 14:48:17 2012 -0600
     4.2 +++ b/clojure/com/aurellem/vbm.clj	Sat Mar 10 15:36:26 2012 -0600
     4.3 @@ -3,6 +3,11 @@
     4.4    (:import org.apache.commons.io.FileUtils)
     4.5    (:use com.aurellem.gb-driver))
     4.6  
     4.7 +;;;;;;;;;;;;; read vbm file 
     4.8 +
     4.9 +(def ^:dynamic *moves-cache*
    4.10 +  (File. "/home/r/proj/pokemon-escape/moves/"))
    4.11 +
    4.12  (defn buttons [mask]
    4.13    (loop [buttons []
    4.14           masks (seq (dissoc button-code :listen))]
    4.15 @@ -29,7 +34,7 @@
    4.16           pending vbm-masks]
    4.17      (if (empty? pending) fixed
    4.18          (let [mask (first pending)]
    4.19 -          (if (not= 0x0000 (bit-and mask (button-code :reset)))
    4.20 +          (if (not= 0x0000 (bit-and mask (button-code :restart)))
    4.21              (recur (conj fixed mask 0x0000 0x0000) (next pending))
    4.22              (recur (conj fixed mask) (next pending)))))))
    4.23  
    4.24 @@ -42,10 +47,6 @@
    4.25  
    4.26  (defn vbm-buttons [#^File vbm]
    4.27    (map buttons (vbm-masks vbm)))
    4.28 -  
    4.29 -(defn play-vbm [#^File vbm]
    4.30 -  (restart!)
    4.31 -  (dorun (map step (vbm-masks vbm))))
    4.32  
    4.33  (defn convert-buttons
    4.34    "To write a vbm file, we must remove the last two buttons after any
    4.35 @@ -59,6 +60,16 @@
    4.36              (recur (conj fixed mask) (drop 3 pending))
    4.37              (recur (conj fixed mask) (next pending)))))))
    4.38  
    4.39 +(defn moves->filename [frame]
    4.40 +  (File. *moves-cache* (format "%07d.vbm" frame)))
    4.41 +
    4.42 +(defn read-moves [frame]
    4.43 +  (let [target (moves->filename frame)]
    4.44 +    (if (.exists target)
    4.45 +      (vbm-buttons target))))
    4.46 +;;;;;;;;;;;;;; write moves to vbm file
    4.47 +
    4.48 +
    4.49  (def vbm-header
    4.50    (byte-array
    4.51     (map
    4.52 @@ -95,5 +106,21 @@
    4.53            vbm-trailer))]
    4.54      contents))
    4.55          
    4.56 -(defn write-vbm [buttons #^File out]
    4.57 -  (clojure.java.io/copy (buttons->vbm-bytes buttons) out))
    4.58 +(defn write-moves! [moves]
    4.59 +  (let [target (moves->filename (count moves))]
    4.60 +    (clojure.java.io/copy (buttons->vbm-bytes moves) target)
    4.61 +    target))
    4.62 +
    4.63 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;
    4.64 +
    4.65 +(use 'clojure.java.shell)
    4.66 +
    4.67 +
    4.68 +(defn play-vbm [#^File vbm]
    4.69 +  (.delete yellow-save-file)
    4.70 +  (if (.exists vbm)
    4.71 +    (sh "/home/r/bin/vba-linux"
    4.72 +        (str "--playmovie=" (.getCanonicalPath vbm))
    4.73 +        (.getCanonicalPath yellow-rom-image)))
    4.74 +  nil)
    4.75 +