Mercurial > vba-clojure
view 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 |
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 (defn first-difference11 [base alt difference-metric [moves root :as script]]12 (loop [branch-point root13 actions moves]14 (let [base-branch (step branch-point base)15 base-val (difference-metric base-branch)16 alt-branch (step branch-point alt)17 alt-val (difference-metric alt-branch)]18 (if (not= base-val alt-val)19 [(conj actions alt) alt-branch]20 (recur base-branch (conj actions base))))))22 (defn repeat-until-different23 [buttons metric [moves root :as script]]24 (let [baseline (metric root)]25 (loop [actions (vec moves)26 state root]27 (let [new-state (step state buttons)28 new-actions (conj actions buttons)]29 (if (not= (metric new-state) baseline)30 [new-actions new-state]31 (recur new-actions new-state))))))34 (defn binary-search [metric]35 (let [baseline (metric 0)]36 (loop [low 137 high 2]38 (let [low-val (metric low)39 high-val (metric high)]40 (println low high)41 (cond42 ;; base case43 (and (= low (dec high))44 (not= low-val high-val))45 high46 ;; exponential growth47 (= baseline high-val low-val)48 (recur high (* high 2))50 ;; binary search51 (and (= baseline low-val)52 (not= baseline high-val))53 (let [test (int (/ (+ low high) 2))54 test-val (metric test)]55 (if (= test-val baseline)56 (recur test high)57 (recur low test))))))))59 (defn delayed-difference60 [base alt delay difference-metric [moves root :as script]]61 (let [generator62 (memoize63 (fn [n]64 (run-moves65 root66 (repeat n base))))67 len68 (binary-search69 (fn [n]70 (= (difference-metric71 (run-moves72 (generator n)73 (concat [alt] (repeat delay base))))74 (difference-metric75 (run-moves76 (generator n)77 (repeat (inc delay) base))))))78 new-moves (concat moves (repeat len base) [alt])79 new-state (run-moves (generator len) [alt])]80 [new-moves new-state]))82 (def x-position-address 0xD361)83 (def y-position-address 0xD362)85 (defn x-position86 ([^SaveState state]87 (aget (memory state) x-position-address))88 ([] (x-position @current-state)))90 (defn y-position91 ([^SaveState state]92 (aget (memory state) y-position-address))93 ([] (y-position @current-state)))95 (defn move96 [dir script]97 (let [current-position-fn98 (cond (#{← →} dir) x-position99 (#{↑ ↓} dir) y-position)]100 (repeat-until-different dir current-position-fn script)))102 (defn walk103 "Move the character along the given directions."104 [directions script]105 (reduce (fn [script dir]106 (move dir script)) script directions))108 (defn search-string109 [^SaveState state string]110 (let [codes111 (str->character-codes string)112 codes-length (count codes)113 mem (vec (memory state))114 mem-length (count mem)]115 (loop [idx 0]116 (if (< (- mem-length idx) codes-length)117 nil118 (if (= (subvec mem idx (+ idx codes-length))119 codes)120 idx121 (recur (inc idx)))))))123 (def text-address 0x9DC1)125 (defn displayed-text126 ([^SaveState state]127 (character-codes->str128 (subvec (vec (memory state))129 text-address130 (+ text-address 82))))131 ([] (displayed-text @current-state)))133 (defn scroll-text134 ([script]135 (delayed-difference136 [:b] [:a :b] 25 displayed-text script))137 ([n script]138 (reduce (fn [script _]139 (scroll-text script))140 script141 (range n))))143 (defn do-nothing [n script]144 (->> script145 (play-moves146 (repeat n []))))149 (defn critical-hit150 "Put the cursor over the desired attack. This program will151 determine the appropriate amount of blank frames to152 insert before pressing [:a] to ensure that the attack is153 a critical hit."154 [script]155 (loop [blanks 6]156 (let [new-script157 (->> script158 (play-moves159 (concat (repeat blanks [])160 [[:a][]])))]161 (if (let [future-state162 (run-moves (second new-script)163 (repeat 400 []))165 result (search-string (memory future-state)166 "Critical")]167 (if result168 (println "critical hit with" blanks "blank frames"))169 result)170 new-script171 (recur (inc blanks))))))173 (defn move-thru-grass174 [direction script]175 (loop [blanks 0]176 (let [new-script177 (->> script178 (play-moves (repeat blanks []))179 (move direction))181 future-state182 (run-moves (second new-script)183 (repeat 600 []))185 result (search-string (memory future-state)186 "Wild")]187 (if (nil? result)188 (do189 (if (< 0 blanks)190 (do191 (println "avoided pokemon with"192 blanks "blank frames")))193 new-script)194 (recur (inc blanks))))))196 (defn walk-thru-grass197 [directions script]198 (reduce (fn [script direction]199 (move-thru-grass direction script))200 script directions))202 (defn slowly203 [delay moves script]204 (reduce205 (fn [script move]206 (->> script207 (do-nothing delay)208 (play-moves (vector move))))209 script moves))211 (defn multiple-times212 ([n command args script]213 (reduce (fn [script _]214 (apply command (concat args [script])))215 script216 (range n)))217 ([n command script]218 (multiple-times n command [] script)))