# HG changeset patch # User Robert McIntyre # Date 1331415386 21600 # Node ID 65c2854c587582295570b13a5ba417ff56742cc6 # Parent e8855121f4137ed09187b9be30f308524440a0dc can now save moves and states and am ready to continue past the title diff -r e8855121f413 -r 65c2854c5875 clojure/com/aurellem/gb_driver.clj --- a/clojure/com/aurellem/gb_driver.clj Sat Mar 10 14:48:17 2012 -0600 +++ b/clojure/com/aurellem/gb_driver.clj Sat Mar 10 15:36:26 2012 -0600 @@ -13,16 +13,16 @@ (defn frame->filename [frame] (File. *save-state-cache* (format "%07d.sav" frame))) -(defn write-save! [^SaveState save] +(defn write-state! [^SaveState save] (let [buf (:data save) bytes (byte-array (.limit buf)) dest (frame->filename (:frame save))] (.get buf bytes) (FileUtils/writeByteArrayToFile dest bytes) (.rewind buf) - save)) + dest)) -(defn read-save [frame] +(defn read-state [frame] (let [save (frame->filename frame)] (if (.exists save) (let [buf (Gb/saveBuffer) @@ -54,11 +54,13 @@ ;;; The first state! (defn gen-root! [] (restart!) - (write-save! (SaveState. 0 (Gb/saveState)))) + (let [state (SaveState. 0 (Gb/saveState))] + (write-state! state) + state)) (defn root [] (if (.exists (frame->filename 0)) - (read-save 0) + (read-state 0) (gen-root!))) ;;;; Press Buttons @@ -88,7 +90,6 @@ (def current-state (atom nil)) - (defn set-state! [^SaveState state] (assert (:data state) "Not a valid state!") (if (not @on?) (restart!)) @@ -112,7 +113,6 @@ [^Move move buttons] (Move. (step (:state move) buttons) buttons)) - (defn play ([^SaveState state n] (reduce (fn [s _] (step s)) state (range n))) diff -r e8855121f413 -r 65c2854c5875 clojure/com/aurellem/speedruns.clj --- a/clojure/com/aurellem/speedruns.clj Sat Mar 10 14:48:17 2012 -0600 +++ b/clojure/com/aurellem/speedruns.clj Sat Mar 10 15:36:26 2012 -0600 @@ -1,6 +1,5 @@ -(ns com.aurellem.test-vbm - (:import java.io.File) - (:use (com.aurellem vbm gb-driver))) +(ns com.aurellem.speedruns + (:import java.io.File)) (def speedrun-2942 (File. "/home/r/proj/pokemon-escape/speedruns/yellow-2942.vbm")) @@ -11,3 +10,8 @@ (def speedrun-2771 (File. "/home/r/proj/pokemon-escape/speedruns/yellow-2771.vbm")) +(def broken-speedrun-1958 + (File. "/home/r/proj/pokemon-escape/speedruns/yellow-1958[bad].vbm")) + +(def broken-speedrun-3256 + (File. "/home/r/proj/pokemon-escape/speedruns/yellow-3256[bad].vbm")) \ No newline at end of file diff -r e8855121f413 -r 65c2854c5875 clojure/com/aurellem/title.clj --- a/clojure/com/aurellem/title.clj Sat Mar 10 14:48:17 2012 -0600 +++ b/clojure/com/aurellem/title.clj Sat Mar 10 15:36:26 2012 -0600 @@ -25,14 +25,14 @@ (defn start [] [[] (root)]) -(defn-memo title [start] - (->> start +(defn-memo title [] + (->> (start) (advance [] [:a]) (advance [] [:start]) (advance [] [:a]) (advance [] [:start]))) -(defn-memo oak [start] +(defn-memo oak [] (->> (title) scroll-text scroll-text @@ -83,8 +83,19 @@ scroll-text scroll-text)) -(defn-memo intro [] - (-> (start) title oak name-entry rival-name-entry finish-title)) +(defn write-intro! [] + (let [[moves state] (finish-title)] + [(write-moves! moves) (write-state! state)])) + +(def title-frames 2323) + +(defn intro [] + [(read-moves title-frames) + (read-state title-frames)]) + +(defn test-intro [] + (play-vbm (moves->filename title-frames))) + ;; TODO might be able to glue these together more elegantly with monads diff -r e8855121f413 -r 65c2854c5875 clojure/com/aurellem/vbm.clj --- a/clojure/com/aurellem/vbm.clj Sat Mar 10 14:48:17 2012 -0600 +++ b/clojure/com/aurellem/vbm.clj Sat Mar 10 15:36:26 2012 -0600 @@ -3,6 +3,11 @@ (:import org.apache.commons.io.FileUtils) (:use com.aurellem.gb-driver)) +;;;;;;;;;;;;; read vbm file + +(def ^:dynamic *moves-cache* + (File. "/home/r/proj/pokemon-escape/moves/")) + (defn buttons [mask] (loop [buttons [] masks (seq (dissoc button-code :listen))] @@ -29,7 +34,7 @@ pending vbm-masks] (if (empty? pending) fixed (let [mask (first pending)] - (if (not= 0x0000 (bit-and mask (button-code :reset))) + (if (not= 0x0000 (bit-and mask (button-code :restart))) (recur (conj fixed mask 0x0000 0x0000) (next pending)) (recur (conj fixed mask) (next pending))))))) @@ -42,10 +47,6 @@ (defn vbm-buttons [#^File vbm] (map buttons (vbm-masks vbm))) - -(defn play-vbm [#^File vbm] - (restart!) - (dorun (map step (vbm-masks vbm)))) (defn convert-buttons "To write a vbm file, we must remove the last two buttons after any @@ -59,6 +60,16 @@ (recur (conj fixed mask) (drop 3 pending)) (recur (conj fixed mask) (next pending))))))) +(defn moves->filename [frame] + (File. *moves-cache* (format "%07d.vbm" frame))) + +(defn read-moves [frame] + (let [target (moves->filename frame)] + (if (.exists target) + (vbm-buttons target)))) +;;;;;;;;;;;;;; write moves to vbm file + + (def vbm-header (byte-array (map @@ -95,5 +106,21 @@ vbm-trailer))] contents)) -(defn write-vbm [buttons #^File out] - (clojure.java.io/copy (buttons->vbm-bytes buttons) out)) +(defn write-moves! [moves] + (let [target (moves->filename (count moves))] + (clojure.java.io/copy (buttons->vbm-bytes moves) target) + target)) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(use 'clojure.java.shell) + + +(defn play-vbm [#^File vbm] + (.delete yellow-save-file) + (if (.exists vbm) + (sh "/home/r/bin/vba-linux" + (str "--playmovie=" (.getCanonicalPath vbm)) + (.getCanonicalPath yellow-rom-image))) + nil) +