changeset 368:08f8284e2f1b

cleaning up code
author Robert McIntyre <rlm@mit.edu>
date Tue, 10 Apr 2012 03:32:47 -0500
parents 845b2d88bef9
children abcc522a3242
files clojure/com/aurellem/run/bootstrap_0.clj clojure/com/aurellem/run/bootstrap_1.clj
diffstat 2 files changed, 118 insertions(+), 25 deletions(-) [+]
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
     2.1 --- a/clojure/com/aurellem/run/bootstrap_1.clj	Mon Apr 09 13:21:34 2012 -0500
     2.2 +++ b/clojure/com/aurellem/run/bootstrap_1.clj	Tue Apr 10 03:32:47 2012 -0500
     2.3 @@ -108,7 +108,7 @@
     2.4         0x01  ;; (item-hack) will never reach this instruction
     2.5  
     2.6         ]
     2.7 -      (repeat 8 [0x00 0x01])
     2.8 +      (repeat 8 [0x00 0x02]);; these can be anything
     2.9  
    2.10        [;; jump to actual program
    2.11         0x00
    2.12 @@ -206,6 +206,9 @@
    2.13      (repeat-until-different [] item-quantity-selected script)
    2.14      script))
    2.15  
    2.16 +;; TODO use this:
    2.17 +;;(wait-until (partial set-cursor-relative 1))
    2.18 +
    2.19  (defn wait-for-cursor
    2.20    [[moves state :as script]]
    2.21    (if (not= (list-offset state) 0)
    2.22 @@ -384,7 +387,7 @@
    2.23    [script]
    2.24    (->> script
    2.25         (delayed-difference [] [:a] 100
    2.26 -                           #(aget (memory %) text-address))))
    2.27 +                           first-character)))
    2.28  
    2.29  (defn-memo get-money-floor-two
    2.30    ([] (get-money-floor-two (go-to-floor-two)))
    2.31 @@ -646,13 +649,13 @@
    2.32            (widthdraw-pc-item 0 99)
    2.33            (widthdraw-pc-item 0 1)
    2.34            (widthdraw-pc-item 2 0xFE)
    2.35 -          (widthdraw-pc-item 3 0xFE))))
    2.36 +          (widthdraw-pc-item 3 0xFE)
    2.37 +          close-menu)))
    2.38            
    2.39  (defn-memo hacking-4
    2.40    ([] (hacking-4 (hacking-3)))
    2.41    ([script]
    2.42       (->> script
    2.43 -          close-menu
    2.44            begin-deposit
    2.45            (deposit-held-item 19 243)
    2.46            (deposit-held-item-named :lemonade 16)
    2.47 @@ -696,6 +699,23 @@
    2.48            (deposit-held-item-named :burn-heal 12)
    2.49            close-menu)))
    2.50  
    2.51 +(defn switch-items [item-fn idx-1 idx-2 script]
    2.52 +  (->> script
    2.53 +       (wait-until select-menu-entry)
    2.54 +       (set-cursor idx-1)
    2.55 +       (wait-until select-menu-entry)
    2.56 +       (play-moves [[][:select][]])
    2.57 +       (set-cursor idx-2)
    2.58 +       (delayed-difference [] [:select] 100
    2.59 +                           #(item-fn % (list-offset %)))))
    2.60 +
    2.61 +(def switch-pc-items (partial switch-items nth-pc-item))
    2.62 +(def switch-held-items (partial switch-items nth-item))
    2.63 +
    2.64 +(defn combine-pc-items [idx-1 script]
    2.65 +  (->> script
    2.66 +       (switch-pc-items idx-1 (inc idx-1))))
    2.67 +
    2.68  (defn combine-items [idx-1 script]
    2.69    (->> script
    2.70         (wait-until select-menu-entry)
    2.71 @@ -731,7 +751,7 @@
    2.72            (widthdraw-pc-item 9 0xFE)
    2.73            (multiple-times
    2.74             7
    2.75 -           (partial combine-items 2))
    2.76 +           (partial combine-pc-items 2))
    2.77            close-menu)))
    2.78            
    2.79  (defn-memo hacking-10
    2.80 @@ -802,7 +822,7 @@
    2.81           select-menu-entry
    2.82           (scroll-text))))
    2.83         
    2.84 -(defn hacking-13
    2.85 +(defn-memo hacking-13
    2.86    ([] (hacking-13 (hacking-12)))
    2.87    ([script]
    2.88       (->> script
    2.89 @@ -810,14 +830,69 @@
    2.90            (select-menu-entry)
    2.91            (toss-pc-item 1 1)
    2.92            (toss-pc-item 0 156)
    2.93 -          (toss-pc-item 0 11)
    2.94 +          (toss-pc-item 0 11))))
    2.95 +
    2.96 +(defn confirm-pattern []
    2.97 +  (let [start-address (inc pc-item-list-start)
    2.98 +        target-pattern (pc-item-writer-program)
    2.99 +        actual-pattern
   2.100 +        (subvec (vec (memory (second (hacking-13))))
   2.101 +                start-address
   2.102 +                (+ start-address (count target-pattern)))]
   2.103 +    (println target-pattern)
   2.104 +    (println actual-pattern)
   2.105 +    (= target-pattern actual-pattern)))
   2.106 +
   2.107 +;; this will be useful for starting program
   2.108 +
   2.109 +
   2.110 +(defn-memo go-to-mansion-for-the-lulz
   2.111 +  ([] (go-to-mansion-for-the-lulz (hacking-13)))
   2.112 +  ([script]
   2.113 +     (->> script
   2.114 +          close-menu
   2.115 +          close-menu
   2.116 +          end-text ;;grr
   2.117 +          (walk [↓ ← ← ← ← ← ← ← ← ← ↓ ↓ ↓])
   2.118 +          (walk (repeat 17 ←))
   2.119 +          (walk [↑ → → → → ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑])
   2.120 +          (walk [↓ ← ↑])
   2.121 +          (walk [↓ ↓ ↓ ↓ ↓ ↓ ↓
   2.122 +                 ← ← ← ← ↑ ↑ ↑ ← ↑])
   2.123 +          (talk)
   2.124 +          (scroll-text 2)
   2.125            close-menu)))
   2.126  
   2.127 +(defn get-cursor [script]
   2.128 +  (wait-until (partial set-cursor-relative 1) script))
   2.129 +
   2.130 +(defn-memo launch-bootstrap-program
   2.131 +  ([] (launch-bootstrap-program
   2.132 +       (go-to-mansion-for-the-lulz)))
   2.133 +  ([script]
   2.134 +     (->> script
   2.135 +          ;; must corrupt item list again by switching pokemon
   2.136 +          activate-start-menu
   2.137 +          ;;get-cursor
   2.138 +          (set-cursor 0)
   2.139 +          select-menu-entry
   2.140 +          ;;get-cursor
   2.141 +          (select-menu-entry)
   2.142 +          ;;get-cursor
   2.143 +          (set-cursor 1)
   2.144 +          (select-menu-entry)
   2.145 +          ;;get-cursor
   2.146 +          (set-cursor 3)
   2.147 +          (delayed-difference [] [:a] 50 first-character)
   2.148 +          close-menu
   2.149 +          ;; now, open items and set map-function
   2.150 +          ;;get-cursor
   2.151 +          (set-cursor 1)
   2.152 +          (select-menu-entry)
   2.153 +          (toss-held-item 22 12)
   2.154 +          (switch-held-items 22 40)
   2.155 +          close-all-menus)))
   2.156            
   2.157            
   2.158            
   2.159 -     
   2.160 -
   2.161 -
   2.162 -          
   2.163 -          
   2.164 \ No newline at end of file
   2.165 +     
   2.166 \ No newline at end of file