changeset 73:8a895ed4c0f9

added history facilities
author Robert McIntyre <rlm@mit.edu>
date Thu, 08 Mar 2012 04:37:10 -0600
parents c88ad4f6d9b4
children aaddd7b72a0e
files clojure/com/aurellem/fragments.clj clojure/com/aurellem/gb_driver.clj clojure/com/aurellem/vbm.clj
diffstat 3 files changed, 40 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/clojure/com/aurellem/fragments.clj	Thu Mar 08 04:37:10 2012 -0600
     1.3 @@ -0,0 +1,3 @@
     1.4 +(ns com.aurellem.fragments
     1.5 +  (:use (com.aurellem gb-driver vbm)))
     1.6 +
     2.1 --- a/clojure/com/aurellem/gb_driver.clj	Thu Mar 08 03:41:24 2012 -0600
     2.2 +++ b/clojure/com/aurellem/gb_driver.clj	Thu Mar 08 04:37:10 2012 -0600
     2.3 @@ -11,7 +11,10 @@
     2.4  (def yellow-save-file
     2.5    (File. "/home/r/proj/pokemon-escape/roms/yellow.sav"))
     2.6  
     2.7 +(def current-frame (atom 0))
     2.8 +
     2.9  (defn vba-init []
    2.10 +  (reset! current-frame 0)
    2.11    (.delete yellow-save-file)
    2.12    (Gb/startEmulator (.getCanonicalPath yellow-rom-image)))
    2.13  
    2.14 @@ -70,9 +73,41 @@
    2.15              (recur (conj buttons button) (rest masks))
    2.16              (recur buttons (rest masks)))))))
    2.17  
    2.18 +
    2.19 +(defn save-state [] (Gb/saveState))
    2.20 +
    2.21 +(def history (atom {}))
    2.22 +
    2.23 +(defn goto [frame]
    2.24 +  (let [save (@history frame)]
    2.25 +    (if (not (nil? save))
    2.26 +      (do
    2.27 +        (reset! current-frame frame)
    2.28 +        (Gb/loadState save))
    2.29 +      (println "no backup state"))))
    2.30 +
    2.31 +(defn clear-history [] (reset! history {}))
    2.32 +
    2.33 +(defn rewind [] (goto (dec @current-frame)))
    2.34 +  
    2.35 +(defn backup-state [frame]
    2.36 +  (swap! history #(assoc % frame (save-state))))
    2.37 +
    2.38 +(def ^:dynamic *save-history* true)
    2.39 +
    2.40 +(defn advance []
    2.41 +  (let [save (save-state)]
    2.42 +    (backup-state @current-frame)
    2.43 +    (swap! current-frame inc)))
    2.44 +
    2.45  (defn step
    2.46 -  ([] (Gb/step))
    2.47 +  ([] (advance) (Gb/step))
    2.48    ([mask-or-buttons]
    2.49 +     (advance)
    2.50       (if (number? mask-or-buttons)
    2.51         (Gb/step mask-or-buttons)
    2.52         (Gb/step (button-mask mask-or-buttons)))))
    2.53 +
    2.54 +(defn frame [] @current-frame)
    2.55 +  
    2.56 +
     3.1 --- a/clojure/com/aurellem/vbm.clj	Thu Mar 08 03:41:24 2012 -0600
     3.2 +++ b/clojure/com/aurellem/vbm.clj	Thu Mar 08 04:37:10 2012 -0600
     3.3 @@ -46,7 +46,7 @@
     3.4           pending buttons]
     3.5      (if (empty? pending) fixed
     3.6          (let [mask (first pending)]
     3.7 -          (if (contains? (first pending) :reset)
     3.8 +          (if (contains? (set (first pending)) :reset)
     3.9              (recur (conj fixed mask) (drop 3 pending))
    3.10              (recur (conj fixed mask) (next pending)))))))
    3.11  
    3.12 @@ -88,8 +88,3 @@
    3.13          
    3.14  (defn write-vbm [buttons #^File out]
    3.15    (clojure.java.io/copy (buttons->vbm-bytes buttons) out))
    3.16 -    
    3.17 -  
    3.18 -         
    3.19 -    
    3.20 -  
    3.21 \ No newline at end of file