comparison clojure/com/aurellem/experiments/items.clj @ 106:3a60bb14a64a

better functional assembly interface; removed frame numbers from SaveStates
author Robert McIntyre <rlm@mit.edu>
date Wed, 14 Mar 2012 21:37:37 -0500
parents 9fad96094950
children
comparison
equal deleted inserted replaced
105:2f8089eacab9 106:3a60bb14a64a
1 (ns com.aurellem.experiments.items
2 (:use (com.aurellem gb-driver vbm title items))
3 (:import [com.aurellem.gb_driver SaveState]))
4
1 ;; try just buying five potions in sequence and see what changes 5 ;; try just buying five potions in sequence and see what changes
2 ;; each time. 6 ;; each time.
3 7
4 (defn common-differences [& seqs] 8 (defn common-differences [& seqs]
5 (let [backbone (range (count (first seqs)))] 9 (let [backbone (range (count (first seqs)))]
8 (zipmap backbone 12 (zipmap backbone
9 (apply (partial map list) seqs))))) 13 (apply (partial map list) seqs)))))
10 14
11 ;; trying to find how items are represented in memory 15 ;; trying to find how items are represented in memory
12 16
13 (comment 17 (def zero-potions (read-state "zero-potions"))
14 (def empty-inventory @current-state) 18
15 19 (def one-potion (read-state "one-potion"))
16 (def one-potion @current-state) 20
17 21 (def two-potions (read-state "two-potions"))
18 (def two-potions @current-state) 22
19 23 (def three-potions (read-state "three-potions"))
20 (def three-potions @current-state) 24
21 25 (def four-potions (read-state "four-potions"))
22 (def four-potions @current-state) 26
23 27 (def five-potions (read-state "five-potions"))
24 (def five-potions @current-state)
25 28
26 29
27 ;; result 30 ;; result
28 (def canidates 31 (defn canidates []
29 (apply common-differences 32 (apply common-differences
30 (map (comp vec memory) 33 (map (comp vec memory)
31 [empty-inventory one-potion two-potions three-potions 34 [zero-potions one-potion two-potions three-potions
32 four-potions five-potions]))) 35 four-potions five-potions])))
33 36
34 [55875 (37 15 49 27 14 44)] 37 (comment [55875 (37 15 49 27 14 44)]
35 [55876 (30 1 49 56 55 23)] 38 [55876 (30 1 49 56 55 23)]
36 [49158 (154 191 78 135 70 73)] 39 [49158 (154 191 78 135 70 73)]
37 [54087 (49 40 37 34 25 22)] 40 [54087 (49 40 37 34 25 22)]
38 [49160 (7 24 59 243 50 217)] 41 [49160 (7 24 59 243 50 217)]
39 [49704 (31 14 72 33 84 27)] 42 [49704 (31 14 72 33 84 27)]
51 ;;; hmmmmmm...... I guess that the potion quantities are at 54046, 54 ;;; hmmmmmm...... I guess that the potion quantities are at 54046,
52 ;;;huh? 55 ;;;huh?
53 56
54 57
55 58
56 (def item-hack (read-state 7999))
57
58 (def item-hack2 (read-state 75882))
59
60 (defn get-mem [] 59 (defn get-mem []
61 (subvec (vec (memory @current-state)) 54040 (+ 54046 100))) 60 (subvec (vec (memory @current-state)) 54040 (+ 54046 100)))
62 61
63 62
64 ;; potion -- 99 63 ;; potion -- 99
90 89
91 90
92 91
93 ;; now it's time to learn the item codes 92 ;; now it's time to learn the item codes
94 93
95 (def item-hack-3 (read-state 77557)) 94 (def inventory-begin
95 (read-state "inventory-begin"))
96
96 (defn show-item 97 (defn show-item
97 "Run a saved pokemon with the first item replaced by the item named 98 "Run a saved pokemon with the first item replaced by the item named
98 by n." 99 by n."
99 [n] 100 [n]
100 (set-state! item-hack-3) 101 (set-state! inventory-begin)
101 (let [mem (memory)] 102 (let [mem (memory)]
103 (aset mem 54044 1)
102 (aset mem 54045 n) 104 (aset mem 54045 n)
105 (aset mem 54046 1)
106 (aset mem 54047 255)
103 (write-memory! mem)) 107 (write-memory! mem))
104 (step) 108 (step)
105 (->> [[] @current-state] 109 (->> [[] @current-state]
106 (play-moves 110 (play-moves
107 [[:a] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] 111 [[:a] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] []
370 ;; 251 TM51 374 ;; 251 TM51
371 ;; 252 TM52 375 ;; 252 TM52
372 ;; 253 TM53 376 ;; 253 TM53
373 ;; 254 TM54 377 ;; 254 TM54
374 ;; 255 end-of-list-sentinel 378 ;; 255 end-of-list-sentinel
379
380
381
382 (defn run-item-program
383 "This is my first assembly/item program!
384 it just increments BC by one.
385
386 The code places a 3 'great balls' at the beginning of the
387 inventory, then directly sets the program counter to start
388 executing at the position of the 'great balls' in memory.
389
390 Since a 'great ball' is represented in memory as 0x03, which
391 corresponts to the opcode which increments BC by one, that is
392 what happens. Then the program counter to the 0x03 quantity entry
393 and BC is incremented again.
394
395 Obviously, the game crashes more or less immediately after the
396 program counter advances past the 'great balls' into the next items
397 in the inventory, thus I call shutdown! before anything bad happens."
398 []
399 (set-inventory (read-state "mid-game") [[:great-ball 3]])
400 (print-inventory)
401 (println "3 ticks") (tick) (tick) (tick)
402 (println "PC before:" (PC))
403 (println "BC before:" (BC))
404 (PC! (inc item-list-start))
405 (println "PC after setting:" (PC))
406 (println "data at PC:" (aget (memory) (PC)))
407 (println "one tick")
408 (tick)
409 (println "PC after one tick:" (PC))
410 (println "BC after one tick:" (BC))
411 (tick)
412 (println "PC after two ticks:" (PC))
413 (println "BC after two ticks:" (BC))
414
415 (shutdown!))
416
417
418
419
420 (defn test-opcodes-1
421 []
422 (let [final-state
423 (->
424 (read-state "mid-game")
425 (set-inv-mem
426 [20 0x02 0x00 0x00 0x02 0x00 0x00
427 0x00 0x0 0xFF])
428 (print-inventory)
429 ;;((fn [_] (println "3 ticks") _))
430 (tick) (tick) (tick)
431
432 ;;(println "PC before:" (PC))
433 ;;(println "BC before:" (BC))
434 ;;(println "AF:" (AF))
435 (PC! (inc item-list-start))
436 (BC! (+ 1 item-list-start))
437 ;;(println "PC after setting:" (PC))
438 ;;(println "data at PC:" (aget (memory) (PC)))
439 ;;(println "data at " (BC) "(BC):" (aget (memory) (BC)))
440
441 ;;(println "one tick")
442 (tick)
443 ;;(println "PC after one tick:" (PC))
444 ;;(println "BC after one tick:" (BC))
445 ;;(println "data at PC:" (aget (memory) (PC)))
446 ;;(println "data at " (BC) "(BC):" (aget (memory) (BC)))
447 (tick)
448 (AF! 0xFFFF)
449 ;;(println "PC after two ticks:" (PC))
450 ;;(println "BC after two ticks:" (BC))
451 ;;(println "data at PC:" (aget (memory) (PC)))
452 ;;(println "data at " (BC) "(BC):" (aget (memory) (BC)))
453 (tick)
454 ;;(println "PC after three ticks:" (PC))
455 ;;(println "BC after three ticks:" (BC))
456 ;;(println "data at PC:" (aget (memory) (PC)))
457 ;;(println "data at " (BC) "(BC):" (aget (memory) (BC)))
458 (tick)
459 ;;(println "PC after four ticks:" (PC))
460 ;;(println "BC after four ticks:" (BC))
461 ;;(println "data at PC:" (aget (memory) (PC)))
462 ;;(println "data at " (BC) "(BC):" (aget (memory) (BC)))
463 (tick)
464 ;;(println "PC after five ticks:" (PC))
465 ;;(println "BC after five ticks:" (BC))
466 ;;(println "data at PC:" (aget (memory) (PC)))
467 ;;(println "data at " (BC) "(BC):" (aget (memory) (BC)))
468 (print-inventory)
469 )]
470
471 (shutdown!)
472 final-state))
473
474
475
476 (defn test-opcodes-2
477 []
478 (set-inv-mem (read-state "mid-game")
479 [20 0x08 0x1D 0xD3 0x00 0x00 0x00
480 0x00 0x0 0xFF])
481 (print-inventory)
482 (println "3 ticks") (tick) (tick) (tick)
483 (println "PC before:" (PC))
484 (println "SP:" (SP))
485 (PC! (inc item-list-start))
486 (println "PC after setting:" (PC))
487 (println "SP:" (Integer/toBinaryString (SP)))
488 (println "data at PC:" (aget (memory) (PC)))
489 (println "data at 0xD31D:" (Integer/toBinaryString (aget (memory) 0xD31D)))
490 (println "data at 0xD31E:" (Integer/toBinaryString (aget (memory) 0xD31E)))
491 (println "one tick")
492 (tick)
493 (println "PC after one tick:" (PC))
494 (println "data at PC:" (aget (memory) (PC)))
495 (println "data at 0xD31D:" (Integer/toBinaryString (aget (memory) 0xD31D)))
496 (println "data at 0xD31E:" (Integer/toBinaryString (aget (memory) 0xD31E)))
497 (tick) (tick) (tick)
498 (println "PC aftter four tick:" (PC))
499 (println "data at PC:" (aget (memory) (PC)))
500 (println "data at 0xD31D:" (aget (memory) 0xD31D))
501
502 (print-inventory)
503 (shutdown!))