annotate clojure/com/aurellem/run/util.clj @ 317:3c5bf2221ea0

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