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