view clojure/com/aurellem/run/util.clj @ 327:fe6fd2323264

corrected compilation bugs
author Robert McIntyre <rlm@mit.edu>
date Thu, 05 Apr 2012 12:59:30 -0500
parents 9637a0f52e7b
children 35960b03693f
line wrap: on
line source
1 (ns com.aurellem.run.util
2 (:use (com.aurellem.gb util gb-driver vbm characters saves))
3 (:import [com.aurellem.gb.gb_driver SaveState]))
5 (def ↑ [:u])
6 (def ↓ [:d])
7 (def ← [:l])
8 (def → [:r])
10 [↑ ↓ ← →]
12 (defn do-nothing [n script]
13 (->> script
14 (play-moves
15 (repeat n []))))
17 (defn first-difference
18 [base alt difference-metric [moves root :as script]]
19 (loop [branch-point root
20 actions moves]
21 (let [base-branch (step branch-point base)
22 base-val (difference-metric base-branch)
23 alt-branch (step branch-point alt)
24 alt-val (difference-metric alt-branch)]
25 (if (not= base-val alt-val)
26 [(conj actions alt) alt-branch]
27 (recur base-branch (conj actions base))))))
29 (defn repeat-until-different
30 [buttons metric [moves root :as script]]
31 (let [baseline (metric root)]
32 (loop [actions (vec moves)
33 state root]
34 (let [new-state (step state buttons)
35 new-actions (conj actions buttons)]
36 (if (not= (metric new-state) baseline)
37 [new-actions new-state]
38 (recur new-actions new-state))))))
40 (defn binary-search [metric]
41 (let [baseline (metric 0)]
42 (loop [low 1
43 high 2]
44 (let [low-val (metric low)
45 high-val (metric high)]
46 (println low high)
47 (cond
48 ;; base case
49 (and (= low (dec high))
50 (not= low-val high-val))
51 high
52 ;; exponential growth
53 (= baseline high-val low-val)
54 (recur high (* high 2))
56 ;; binary search
57 (and (= baseline low-val)
58 (not= baseline high-val))
59 (let [test (int (/ (+ low high) 2))
60 test-val (metric test)]
61 (if (= test-val baseline)
62 (recur test high)
63 (recur low test))))))))
65 (defn delayed-difference
66 [base alt delay difference-metric [moves root :as script]]
67 (let [generator
68 (memoize
69 (fn [n]
70 (run-moves
71 root
72 (repeat n base))))
73 len
74 (binary-search
75 (fn [n]
76 (= (difference-metric
77 (run-moves
78 (generator n)
79 (concat [alt] (repeat delay base))))
80 (difference-metric
81 (run-moves
82 (generator n)
83 (repeat (inc delay) base))))))
84 new-moves (concat moves (repeat len base) [alt])
85 new-state (run-moves (generator len) [alt])]
86 [new-moves new-state]))
88 (def x-position-address 0xD361)
89 (def y-position-address 0xD362)
91 (defn x-position
92 ([^SaveState state]
93 (aget (memory state) x-position-address))
94 ([] (x-position @current-state)))
96 (defn y-position
97 ([^SaveState state]
98 (aget (memory state) y-position-address))
99 ([] (y-position @current-state)))
101 (defn move
102 [dir script]
103 (let [current-position-fn
104 (cond (#{← →} dir) x-position
105 (#{↑ ↓} dir) y-position)]
106 (repeat-until-different dir current-position-fn script)))
108 (defn walk
109 "Move the character along the given directions."
110 [directions script]
111 (reduce (fn [script dir]
112 (move dir script)) script directions))
114 (defn search-string
115 ([^SaveState state string]
116 (let [codes
117 (str->character-codes string)
118 codes-length (count codes)
119 mem (vec (memory state))
120 mem-length (count mem)]
121 (loop [idx 0]
122 (if (< (- mem-length idx) codes-length)
123 nil
124 (if (= (subvec mem idx (+ idx codes-length))
125 codes)
126 idx
127 (recur (inc idx)))))))
128 ([string]
129 (search-string @current-state string)))
131 (def text-address 0x9DC1)
133 (defn displayed-text
134 ([^SaveState state]
135 (character-codes->str
136 (subvec (vec (memory state))
137 text-address
138 (+ text-address 82))))
139 ([] (displayed-text @current-state)))
141 (defn scroll-text
142 ([script]
143 (delayed-difference
144 [:b] [:a :b] 25 displayed-text script))
145 ([n script]
146 (reduce (fn [script _]
147 (scroll-text script))
148 script
149 (range n))))
151 (defn end-text
152 ([script]
153 (->>
154 script
155 (do-nothing 150)
156 (play-moves [[:b]]))))
158 (defn delayed-improbability-search
159 "insert blank frames before calling script-fn until
160 metric returns true."
161 [delay metric script-fn script]
162 (loop [blanks 0]
163 (let [new-script
164 (->> script
165 (play-moves
166 (concat (repeat blanks [])))
167 script-fn)
168 future-state
169 (run-moves (second new-script)
170 (repeat delay []))
171 result (metric future-state)]
172 (if result
173 (do
174 (println "improbability factor:" blanks)
175 new-script)
176 (recur (inc blanks))))))
178 (defn critical-hit
179 "Put the cursor over the desired attack. This program will
180 determine the appropriate amount of blank frames to
181 insert before pressing [:a] to ensure that the attack is
182 a critical hit."
183 [script]
184 (delayed-improbability-search
185 400
186 #(search-string % "Critical")
187 (partial play-moves [[:a][]])
188 script))
190 (defn move-thru-grass
191 [direction script]
192 (delayed-improbability-search
193 600
194 #(nil? (search-string % "Wild"))
195 (partial move direction)
196 script))
198 (defn walk-thru-grass
199 [directions script]
200 (reduce (fn [script direction]
201 (move-thru-grass direction script))
202 script directions))
204 (defn slowly
205 [delay moves script]
206 (reduce
207 (fn [script move]
208 (->> script
209 (do-nothing delay)
210 (play-moves (vector move))))
211 script moves))
213 (defn multiple-times
214 ([n command args script]
215 (reduce (fn [script _]
216 (apply command (concat args [script])))
217 script
218 (range n)))
219 ([n command script]
220 (multiple-times n command [] script)))