comparison clojure/com/aurellem/gb_driver.clj @ 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 4c60ebca1a9d
comparison
equal deleted inserted replaced
87:e8855121f413 88:65c2854c5875
11 (File. "/home/r/proj/pokemon-escape/save-states/")) 11 (File. "/home/r/proj/pokemon-escape/save-states/"))
12 12
13 (defn frame->filename [frame] 13 (defn frame->filename [frame]
14 (File. *save-state-cache* (format "%07d.sav" frame))) 14 (File. *save-state-cache* (format "%07d.sav" frame)))
15 15
16 (defn write-save! [^SaveState save] 16 (defn write-state! [^SaveState save]
17 (let [buf (:data save) 17 (let [buf (:data save)
18 bytes (byte-array (.limit buf)) 18 bytes (byte-array (.limit buf))
19 dest (frame->filename (:frame save))] 19 dest (frame->filename (:frame save))]
20 (.get buf bytes) 20 (.get buf bytes)
21 (FileUtils/writeByteArrayToFile dest bytes) 21 (FileUtils/writeByteArrayToFile dest bytes)
22 (.rewind buf) 22 (.rewind buf)
23 save)) 23 dest))
24 24
25 (defn read-save [frame] 25 (defn read-state [frame]
26 (let [save (frame->filename frame)] 26 (let [save (frame->filename frame)]
27 (if (.exists save) 27 (if (.exists save)
28 (let [buf (Gb/saveBuffer) 28 (let [buf (Gb/saveBuffer)
29 bytes (FileUtils/readFileToByteArray save)] 29 bytes (FileUtils/readFileToByteArray save)]
30 (.put buf bytes) 30 (.put buf bytes)
52 (reset! on? true)) 52 (reset! on? true))
53 53
54 ;;; The first state! 54 ;;; The first state!
55 (defn gen-root! [] 55 (defn gen-root! []
56 (restart!) 56 (restart!)
57 (write-save! (SaveState. 0 (Gb/saveState)))) 57 (let [state (SaveState. 0 (Gb/saveState))]
58 (write-state! state)
59 state))
58 60
59 (defn root [] 61 (defn root []
60 (if (.exists (frame->filename 0)) 62 (if (.exists (frame->filename 0))
61 (read-save 0) 63 (read-state 0)
62 (gen-root!))) 64 (gen-root!)))
63 65
64 ;;;; Press Buttons 66 ;;;; Press Buttons
65 67
66 (def button-code 68 (def button-code
86 (defn button-mask [buttons] 88 (defn button-mask [buttons]
87 (reduce bit-or 0x0000 (map button-code buttons))) 89 (reduce bit-or 0x0000 (map button-code buttons)))
88 90
89 (def current-state (atom nil)) 91 (def current-state (atom nil))
90 92
91
92 (defn set-state! [^SaveState state] 93 (defn set-state! [^SaveState state]
93 (assert (:data state) "Not a valid state!") 94 (assert (:data state) "Not a valid state!")
94 (if (not @on?) (restart!)) 95 (if (not @on?) (restart!))
95 (if (not= @current-state state) 96 (if (not= @current-state state)
96 (Gb/loadState (:data state))) 97 (Gb/loadState (:data state)))
109 ([] (step (if @current-state @current-state (root))))) 110 ([] (step (if @current-state @current-state (root)))))
110 111
111 (defn move 112 (defn move
112 [^Move move buttons] 113 [^Move move buttons]
113 (Move. (step (:state move) buttons) buttons)) 114 (Move. (step (:state move) buttons) buttons))
114
115 115
116 (defn play 116 (defn play
117 ([^SaveState state n] 117 ([^SaveState state n]
118 (reduce (fn [s _] (step s)) state (range n))) 118 (reduce (fn [s _] (step s)) state (range n)))
119 ([n] 119 ([n]