comparison 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
comparison
equal deleted inserted replaced
367:845b2d88bef9 368:08f8284e2f1b
368 ([^SaveState state] 368 ([^SaveState state]
369 (println "items:" (aget (memory state) item-quantity-selected-address)) 369 (println "items:" (aget (memory state) item-quantity-selected-address))
370 (aget (memory state) item-quantity-selected-address)) 370 (aget (memory state) item-quantity-selected-address))
371 ([] (item-quantity-selected @current-state))) 371 ([] (item-quantity-selected @current-state)))
372 372
373 (defn wait-until
374 ([script-fn default-key script]
375 (let [wait-time
376 (- (dec (count (first (script-fn script))))
377 (count (first script)))]
378 (println "wait-time" wait-time)
379 (play-moves (repeat wait-time default-key) script)))
380 ([script-fn script]
381 (wait-until script-fn [] script)))
382
373 (defn set-cursor-relative 383 (defn set-cursor-relative
374 "Assumes the arrow keys currently control the cursor. 384 "Assumes the arrow keys currently control the cursor.
375 Moves the cursor n steps relative to its current 385 Moves the cursor n steps relative to its current
376 position." 386 position."
377 [n script] 387 [n script]
380 (Math/abs n) 390 (Math/abs n)
381 (partial first-difference 391 (partial first-difference
382 [] key list-offset) 392 [] key list-offset)
383 script))) 393 script)))
384 394
395 (defn set-cursor*
396 [n [moves state :as script]]
397 (let [current-position (list-offset state)
398 difference (- n current-position)]
399 (set-cursor-relative difference script)))
400
385 (defn set-cursor 401 (defn set-cursor
386 "Assumes the arrow keys currently control the cursor. Sets 402 "Assumes the arrow keys currently control the cursor. Sets
387 the cursor to the desired position. Works for any menu 403 the cursor to the desired position. Works for any menu
388 that uses a cursor including the start menu, item menu, 404 that uses a cursor including the start menu, item menu,
389 pokemon menu, and battle menu." 405 pokemon menu, and battle menu."
390 [n [moves state :as script]] 406 [n [moves state :as script]]
391 (let [current-position (list-offset state) 407 (->> script
392 difference (- n current-position)] 408 (wait-until (partial set-cursor-relative 1))
393 (println difference) 409 (set-cursor* n)))
394 (set-cursor-relative difference script))) 410
395 411 (defn first-character [state]
396 (defn set-quantity 412 (aget (memory state) text-address))
413
414 (defn set-quantity*
397 "Set the quantity of an item to buy or sell to the desired value 415 "Set the quantity of an item to buy or sell to the desired value
398 using the fewest possible button presses." 416 using the fewest possible button presses."
399 ([total-quantity desired-quantity [moves state :as script]] 417 [total-quantity desired-quantity [moves state :as script]]
400 (cond (= desired-quantity 1) (do (println "1 of 1") script) 418 (cond (= desired-quantity 1) (do (println "1 of 1") script)
401 (= total-quantity desired-quantity) 419 (= total-quantity desired-quantity)
402 (do (println "get everything!") 420 (do (println "get everything!")
403 (delayed-difference [] ↓ 5 item-quantity-selected 421 (delayed-difference [] ↓ 5 item-quantity-selected
404 script)) 422 script))
419 (fn [script _] 437 (fn [script _]
420 (delayed-difference [] direction 5 item-quantity-selected 438 (delayed-difference [] direction 5 item-quantity-selected
421 script)) 439 script))
422 script 440 script
423 (range (Math/abs best-path)))))) 441 (range (Math/abs best-path))))))
442
443 (defn set-quantity
444 ([total-quantity desired-quantity [moves state :as script]]
445 (->> script (wait-until (partial first-difference [] [:a]
446 first-character))
447 (set-quantity* total-quantity desired-quantity script)))
424 ([desired-quantity [moves state :as script]] 448 ([desired-quantity [moves state :as script]]
425 (set-quantity 99 desired-quantity script))) 449 (set-quantity 99 desired-quantity script)))
426 450
451
427 (defn activate-start-menu [script] 452 (defn activate-start-menu [script]
428 (first-difference [:b] [:b :start] AF script)) 453 (first-difference [:b] [:b :start] AF script))
429
430 (defn wait-until [script-fn script]
431 (let [wait-time
432 (- (dec (count (first (script-fn script))))
433 (count (first script)))]
434 (println "wait-time" wait-time)
435 (do-nothing wait-time script)))
436 454
437 (defn select-menu-entry 455 (defn select-menu-entry
438 ([test-direction [moves state :as script]] 456 ([test-direction [moves state :as script]]
439 (->> script 457 (->> script
440 (wait-until (partial set-cursor-relative test-direction)) 458 (wait-until (partial set-cursor-relative test-direction))