Mercurial > vba-clojure
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.util2 (: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 (->> script14 (play-moves15 (repeat n []))))17 (defn first-difference18 [base alt difference-metric [moves root :as script]]19 (loop [branch-point root20 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-different30 [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 143 high 2]44 (let [low-val (metric low)45 high-val (metric high)]46 (println low high)47 (cond48 ;; base case49 (and (= low (dec high))50 (not= low-val high-val))51 high52 ;; exponential growth53 (= baseline high-val low-val)54 (recur high (* high 2))56 ;; binary search57 (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-difference66 [base alt delay difference-metric [moves root :as script]]67 (let [generator68 (memoize69 (fn [n]70 (run-moves71 root72 (repeat n base))))73 len74 (binary-search75 (fn [n]76 (= (difference-metric77 (run-moves78 (generator n)79 (concat [alt] (repeat delay base))))80 (difference-metric81 (run-moves82 (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-position92 ([^SaveState state]93 (aget (memory state) x-position-address))94 ([] (x-position @current-state)))96 (defn y-position97 ([^SaveState state]98 (aget (memory state) y-position-address))99 ([] (y-position @current-state)))101 (defn move102 [dir script]103 (let [current-position-fn104 (cond (#{← →} dir) x-position105 (#{↑ ↓} dir) y-position)]106 (repeat-until-different dir current-position-fn script)))108 (defn walk109 "Move the character along the given directions."110 [directions script]111 (reduce (fn [script dir]112 (move dir script)) script directions))114 (defn search-string115 ([^SaveState state string]116 (let [codes117 (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 nil124 (if (= (subvec mem idx (+ idx codes-length))125 codes)126 idx127 (recur (inc idx)))))))128 ([string]129 (search-string @current-state string)))131 (def text-address 0x9DC1)133 (defn displayed-text134 ([^SaveState state]135 (character-codes->str136 (subvec (vec (memory state))137 text-address138 (+ text-address 82))))139 ([] (displayed-text @current-state)))141 (defn scroll-text142 ([script]143 (delayed-difference144 [:b] [:a :b] 25 displayed-text script))145 ([n script]146 (reduce (fn [script _]147 (scroll-text script))148 script149 (range n))))151 (defn end-text152 ([script]153 (->>154 script155 (do-nothing 150)156 (play-moves [[:b]]))))158 (defn delayed-improbability-search159 "insert blank frames before calling script-fn until160 metric returns true."161 [delay metric script-fn script]162 (loop [blanks 0]163 (let [new-script164 (->> script165 (play-moves166 (concat (repeat blanks [])))167 script-fn)168 future-state169 (run-moves (second new-script)170 (repeat delay []))171 result (metric future-state)]172 (if result173 (do174 (println "improbability factor:" blanks)175 new-script)176 (recur (inc blanks))))))178 (defn critical-hit179 "Put the cursor over the desired attack. This program will180 determine the appropriate amount of blank frames to181 insert before pressing [:a] to ensure that the attack is182 a critical hit."183 [script]184 (delayed-improbability-search185 400186 #(search-string % "Critical")187 (partial play-moves [[:a][]])188 script))190 (defn move-thru-grass191 [direction script]192 (delayed-improbability-search193 600194 #(nil? (search-string % "Wild"))195 (partial move direction)196 script))198 (defn walk-thru-grass199 [directions script]200 (reduce (fn [script direction]201 (move-thru-grass direction script))202 script directions))204 (defn slowly205 [delay moves script]206 (reduce207 (fn [script move]208 (->> script209 (do-nothing delay)210 (play-moves (vector move))))211 script moves))213 (defn multiple-times214 ([n command args script]215 (reduce (fn [script _]216 (apply command (concat args [script])))217 script218 (range n)))219 ([n command script]220 (multiple-times n command [] script)))