diff clojure/com/aurellem/run/bootstrap_0.clj @ 368:08f8284e2f1b

cleaning up code
author Robert McIntyre <rlm@mit.edu>
date Tue, 10 Apr 2012 03:32:47 -0500
parents 3b3cd62b6106
children abcc522a3242
line wrap: on
line diff
     1.1 --- a/clojure/com/aurellem/run/bootstrap_0.clj	Mon Apr 09 13:21:34 2012 -0500
     1.2 +++ b/clojure/com/aurellem/run/bootstrap_0.clj	Tue Apr 10 03:32:47 2012 -0500
     1.3 @@ -370,6 +370,16 @@
     1.4       (aget (memory state) item-quantity-selected-address))
     1.5    ([] (item-quantity-selected @current-state)))
     1.6  
     1.7 +(defn wait-until
     1.8 +  ([script-fn default-key script]
     1.9 +     (let [wait-time
    1.10 +           (- (dec (count (first (script-fn script))))
    1.11 +              (count (first script)))]
    1.12 +       (println "wait-time" wait-time)
    1.13 +       (play-moves (repeat wait-time default-key) script)))
    1.14 +  ([script-fn script]
    1.15 +     (wait-until script-fn [] script)))
    1.16 +
    1.17  (defn set-cursor-relative
    1.18    "Assumes the arrow keys currently control the cursor.
    1.19     Moves the cursor n steps relative to its current
    1.20 @@ -382,21 +392,29 @@
    1.21        [] key list-offset)
    1.22       script)))
    1.23  
    1.24 +(defn set-cursor*
    1.25 +  [n [moves state :as script]]
    1.26 +  (let [current-position (list-offset state)
    1.27 +        difference (- n current-position)]
    1.28 +    (set-cursor-relative difference script)))
    1.29 +
    1.30  (defn set-cursor
    1.31    "Assumes the arrow keys currently control the cursor. Sets
    1.32     the cursor to the desired position. Works for any menu
    1.33     that uses a cursor including the start menu, item menu,
    1.34     pokemon menu, and battle menu."
    1.35    [n [moves state :as script]]
    1.36 -  (let [current-position (list-offset state)
    1.37 -        difference (- n current-position)]
    1.38 -    (println difference)
    1.39 -    (set-cursor-relative difference script)))
    1.40 +  (->> script
    1.41 +       (wait-until (partial set-cursor-relative 1))
    1.42 +       (set-cursor* n)))
    1.43  
    1.44 -(defn set-quantity
    1.45 +(defn first-character [state]
    1.46 +  (aget (memory state) text-address))
    1.47 +
    1.48 +(defn set-quantity*
    1.49    "Set the quantity of an item to buy or sell to the desired value
    1.50    using the fewest possible button presses."
    1.51 -  ([total-quantity desired-quantity [moves state :as script]]
    1.52 +  [total-quantity desired-quantity [moves state :as script]]
    1.53       (cond (= desired-quantity 1) (do (println "1 of 1") script)
    1.54             (= total-quantity desired-quantity)
    1.55             (do (println "get everything!")
    1.56 @@ -421,19 +439,19 @@
    1.57                                      script))
    1.58                script
    1.59                (range (Math/abs best-path))))))
    1.60 +
    1.61 +(defn set-quantity
    1.62 +  ([total-quantity desired-quantity [moves state :as script]]
    1.63 +     (->> script (wait-until (partial first-difference [] [:a]
    1.64 +                                      first-character))
    1.65 +          (set-quantity* total-quantity desired-quantity script)))
    1.66    ([desired-quantity [moves state :as script]]
    1.67       (set-quantity 99 desired-quantity script)))
    1.68  
    1.69 +
    1.70  (defn activate-start-menu [script]
    1.71    (first-difference [:b] [:b :start] AF script))
    1.72  
    1.73 -(defn wait-until [script-fn script]
    1.74 -  (let [wait-time
    1.75 -        (- (dec (count (first (script-fn script))))
    1.76 -           (count (first script)))]
    1.77 -    (println "wait-time" wait-time)
    1.78 -    (do-nothing wait-time script)))
    1.79 -
    1.80  (defn select-menu-entry
    1.81    ([test-direction [moves state :as script]]
    1.82       (->> script