comparison clojure/com/aurellem/run/bootstrap_0.clj @ 329:a452deec2882

improved efficiency of delayed-difference
author Robert McIntyre <rlm@mit.edu>
date Fri, 06 Apr 2012 07:22:18 -0500
parents fe6fd2323264
children ecff37d22293
comparison
equal deleted inserted replaced
328:35960b03693f 329:a452deec2882
366 366
367 (def item-quantity-selected-address 65432) 367 (def item-quantity-selected-address 65432)
368 368
369 (defn item-quantity-selected 369 (defn item-quantity-selected
370 ([^SaveState state] 370 ([^SaveState state]
371 (println "items:" (aget (memory state) item-quantity-selected-address))
371 (aget (memory state) item-quantity-selected-address)) 372 (aget (memory state) item-quantity-selected-address))
372 ([] (item-quantity-selected @current-state))) 373 ([] (item-quantity-selected @current-state)))
373 374
374 (defn set-cursor-relative 375 (defn set-cursor-relative
375 "Assumes the arrow keys currently control the cursor. 376 "Assumes the arrow keys currently control the cursor.
391 [n [moves state :as script]] 392 [n [moves state :as script]]
392 (let [current-position (list-offset state) 393 (let [current-position (list-offset state)
393 difference (- n current-position)] 394 difference (- n current-position)]
394 (println difference) 395 (println difference)
395 (set-cursor-relative difference script))) 396 (set-cursor-relative difference script)))
396 397
397 (defn buy-item 398 (defn set-quantity
398 "Assumes that the main item-screen is up, and buys 399 "Set the quantity of an item to buy or sell to the desired value
399 quantity of the nth item in the list, assuming that you 400 using the fewest possible button presses."
400 have enough money." 401 [total-quantity desired-quantity [moves state :as script]]
401 [n quantity script] 402 (let [current-quantity (item-quantity-selected state)
402 (if (= 0 quantity) 403 loop-point (if (> total-quantity 99) 0xFF 99)
403 script 404 distance (- desired-quantity current-quantity)
404 (let [after-initial-pause 405 loop-distance (int(* -1 (Math/signum (float distance))
405 (do-nothing 20 script) 406 (- loop-point (Math/abs distance))))
406 move-to-item 407 best-path (first (sort-by #(Math/abs %)
407 (reduce (fn [script _] 408 [distance loop-distance]))
408 (->> script 409 direction (if (< 0 best-path) ↑ ↓)]
409 (play-moves [[:d]]) 410 (println "best-path" best-path)
410 (do-nothing 3))) 411 (reduce
411 after-initial-pause 412 (fn [script _]
412 (range n)) 413 (delayed-difference [] direction 200 item-quantity-selected
413 select-item 414 script))
414 (play-moves [[:a]] move-to-item)
415 request-items
416 (reduce (fn [script _]
417 (->> script
418 (play-moves [[:u]])
419 (do-nothing 1)))
420 select-item
421 (range (dec quantity)))
422 buy-items
423 (->> request-items
424 (do-nothing 10)
425 (play-moves [[:a]])
426 (scroll-text)
427 (scroll-text)
428 (do-nothing 10)
429 (play-moves [[:a]])
430 (scroll-text))]
431 buy-items)))
432 415
433 416 script
434 (defn buy-items 417 (range (Math/abs best-path)))))
435 "Given a list of [item-no quantity], buys the quantity
436 from the shop's list. Assumes that the item list is
437 already up."
438 [item-pairs script]
439 (let [item-lookup (into {0 0 1 0 2 0 3 0 4 0} item-pairs)
440 initial-purchase
441 (->> script
442 (buy-item 0 (item-lookup 0))
443 (buy-item 1 (item-lookup 1))
444 (buy-item 2 (item-lookup 2)))]
445 (cond
446 (and
447 (not= 0 (item-lookup 3))
448 (not= 0 (item-lookup 4)))
449 (->> initial-purchase
450 (do-nothing 20)
451 (play-moves [[:d]])
452 (do-nothing 3)
453 (play-moves [[:d]])
454 (do-nothing 3)
455 (play-moves [[:d]])
456 (do-nothing 10)
457 (buy-item 0 (item-lookup 3))
458 (do-nothing 20)
459 (play-moves [[:d]])
460 (do-nothing 3)
461 (play-moves [[:d]])
462 (do-nothing 3)
463 (play-moves [[:d]])
464 (do-nothing 10)
465 (buy-item 0 (item-lookup 4)))
466 (and (= 0 (item-lookup 3))
467 (not= 0 (item-lookup 4)))
468 (->> initial-purchase
469 (do-nothing 20)
470 (play-moves [[:d]])
471 (do-nothing 3)
472 (play-moves [[:d]])
473 (do-nothing 3)
474 (play-moves [[:d]])
475 (do-nothing 10)
476 (play-moves [[:d]])
477 (do-nothing 10)
478 (buy-item 0 (item-lookup 4)))
479 (and (not= 0 (item-lookup 3))
480 (= 0 (item-lookup 4)))
481 (->> initial-purchase
482 (do-nothing 20)
483 (play-moves [[:d]])
484 (do-nothing 3)
485 (play-moves [[:d]])
486 (do-nothing 3)
487 (play-moves [[:d]])
488 (do-nothing 10)
489 (buy-item 0 (item-lookup 3)))
490 (and (= 0 (item-lookup 3))
491 (= 0 (item-lookup 4)))
492 initial-purchase)))
493
494
495 (defn test-buy-items
496 ([] (test-buy-items
497 (walk-to-counter)))
498 ([script]
499 (->> [(first script) (set-money (second script)
500 999999)]
501 (play-moves
502 [[] [:a] []])
503 (scroll-text)
504 (do-nothing 100)
505 (play-moves [[:a]])
506 (do-nothing 100)
507 (buy-items
508 [[0 1]
509 [1 15]
510 [2 1]
511 [3 20]
512 [4 95]
513 ]))))
514
515 (defn-memo buy-initial-items
516 ([] (buy-initial-items
517 (walk-to-counter)))
518 ([script]
519 (->> script
520 (play-moves
521 [[] [:a] []])
522 (scroll-text)
523 (do-nothing 100)
524 (play-moves [[:a]])
525 (do-nothing 100)
526 (buy-items
527 [[0 1]
528 [1 1]
529 [2 1]
530 [3 1]
531 [4 1]])
532 (do-nothing 100)
533 (play-moves [[:b]])
534 (do-nothing 100)
535 (play-moves [[:b]])
536 (do-nothing 100)
537 (play-moves [[:b] []])
538 (first-difference [:b] [:b :start] AF))))
539
540 418
541 (defn-memo do-save-corruption 419 (defn-memo do-save-corruption
542 ([] (do-save-corruption 420 ([] (do-save-corruption
543 (buy-initial-items))) 421 (walk-to-counter)))
544 ([script] 422 ([script]
545 (->> script 423 (->> script
546 (first-difference [] [:d] AF) 424 (first-difference [] [:start] AF)
547 (play-moves [[] [] [] [:d] 425 (set-cursor 4)
548 [] [] [] [:d] 426 (do-nothing 5)
549 [] [] [] [:d] 427 (play-moves [[:a]])
550 [] [] [:a]]) 428 (play-moves (repeat 85 [:b]))
551 scroll-text 429 (play-moves [[:a]])
552 (play-moves 430 (play-moves
553 ;; this section is copied from speedrun-2942 and corrupts 431 ;; this section is copied from speedrun-2942 and corrupts
554 ;; the save so that the total number of pokemon is set to 432 ;; the save so that the total number of pokemon is set to
555 ;; 0xFF, allowing manipulation of non-pokemon data in RAM 433 ;; 0xFF, allowing manipulation of non-pokemon data in RAM
556 ;; via the pokemon interface. 434 ;; via the pokemon interface.
557 [[] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] 435 [[] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] []
558 [] [] [] [] [] [] [] [] [] [] [] [:select] [:restart]]) 436 [] [] [] [] [] [] [] [] [] [] [] [:select] [:restart]])
559 (title) 437 (title)
560 (first-difference [] [:start] AF) 438 (first-difference [] [:start] AF)
439 (first-difference [] [:a] AF))))
440
441 (defn gen-corrupted-checkpoint! []
442 (let [[cor-moves cor-save] (do-save-corruption)]
443 (write-moves! cor-moves "cor-checkpoint")
444 (write-state! cor-save "cor-checkpoint")))
445
446 (defn corrupted-checkpoint []
447 [(read-moves "cor-checkpoint")
448 (read-state "cor-checkpoint")])
449
450 (def menu do-nothing )
451
452 (defn-memo corrupt-item-list
453 "Corrupt the num-of-items variable by switching a corrupted pokemon
454 into out-of-bounds memory."
455 ([] (corrupt-item-list
456 (corrupted-checkpoint)))
457 ([script]
458 (->> script
459 (first-difference [:a] [:a :start] AF)
460 (set-cursor 1) ; select "POKEMON" from
461 (first-difference [] [:a] AF) ; from main menu
462 (set-cursor 5) ; select 6th pokemon
561 (first-difference [] [:a] AF) 463 (first-difference [] [:a] AF)
562 (first-difference [:a] [:a :start] AF)))) 464 (set-cursor 1)
563 465 (first-difference [] [:a] AF)
564 (def menu do-nothing ) 466 (repeat-until-different [] list-offset)
565 467 (set-cursor 9)
566 (defn-memo corrupt-item-list 468 (first-difference [] [:a] AF) ; switch 6th with 10th
567 ([] (corrupt-item-list 469 (first-difference [] [:b] AF)
568 (do-save-corruption))) 470 (first-difference [] [:b] AF))))
569 ([script] 471
570 (->> script 472 (defn get-lots-of-money
571 (do-nothing 200) 473 "Sell 0xFE cancel buttons to make a tremendous amount of money."
572 (menu [↓ [:a]]) ; select "POKEMON" from 474 ([] (get-lots-of-money (corrupt-item-list)))
573 ; from main menu 475 ([script]
574 (menu [↓ ↓ ↓ ↓ ↓ ↓ ; go to 6th pokemon 476 (->> script
575 [:a] ↓ [:a] ; select "switch" 477 (first-difference [] [:a] AF)
576 ↓ ↓ ↓ [:a]]) ; switch with 9th "pokemon" 478 (repeat-until-different [] list-offset)
577 479 (set-cursor 1)
578 (do-nothing 1)))) 480 (first-difference [] [:a] AF)
579 481 (repeat-until-different [] list-offset)
482 (first-difference [] [:a] AF)
483 (set-quantity 0xFF 0xFE)
484 (do-nothing 1)
485 )))
486
487
488
489
490 (def buy-items do-nothing)
491
580 (defn-memo get-burn-heals 492 (defn-memo get-burn-heals
581 ([] (get-burn-heals 493 ([] (get-burn-heals
582 (corrupt-item-list))) 494 (corrupt-item-list)))
583 ([script] 495 ([script]
584 (->> script 496 (->> script