annotate clojure/com/aurellem/world/practice.clj @ 200:1e2aa688e6e4

pokemon printing complete.
author Robert McIntyre <rlm@mit.edu>
date Fri, 23 Mar 2012 02:41:22 -0500
parents 67c42608ef9d
children d5dddf33543c
rev   line source
ocsenave@185 1 (ns com.aurellem.world.practice
ocsenave@185 2 (:use (com.aurellem.gb saves util constants gb-driver vbm items
ocsenave@194 3 assembly characters))
ocsenave@185 4 (:use (com.aurellem.run title save-corruption))
ocsenave@185 5 ;;(:use (com.aurellem.exp pokemon))
ocsenave@185 6 (:use (com.aurellem.exp item-bridge))
ocsenave@194 7 (:import [com.aurellem.gb.gb_driver SaveState])
ocsenave@194 8
ocsenave@194 9 )
ocsenave@185 10
ocsenave@185 11 (def hex-pc (comp hex PC))
ocsenave@185 12
ocsenave@194 13 (defn nstep [state n]
ocsenave@194 14 (if (zero? n) state
ocsenave@194 15 (recur (step state) (dec n))))
ocsenave@185 16
ocsenave@185 17
ocsenave@185 18 (defn view-memory* [state start length]
ocsenave@185 19 ((comp vec map)
ocsenave@194 20 #((comp aget) (memory state) %)
ocsenave@185 21 (range start (+ start length))))
ocsenave@185 22
ocsenave@185 23
ocsenave@185 24 (defn state-surprise
ocsenave@185 25 "This is one tick before the trainer goes [!]"
ocsenave@185 26 []
ocsenave@185 27 (->
ocsenave@185 28 (pre-trainer-battle)
ocsenave@185 29 (step [:r])
ocsenave@185 30 (step)
ocsenave@185 31 (ntick 88147)
ocsenave@185 32 ;(step [:r])
ocsenave@185 33
ocsenave@185 34 ;(step [:r])
ocsenave@185 35 ;(step [:r])
ocsenave@185 36 ;(step [:r])
ocsenave@185 37 ;(step [:r])
ocsenave@185 38 ))
ocsenave@194 39 (defn state-inject
ocsenave@194 40 "I have replaced the letter e with e-acute @ 0xC4E8."
ocsenave@194 41 []
ocsenave@194 42 (read-state "inject-surprise"))
ocsenave@185 43
ocsenave@185 44
ocsenave@185 45
ocsenave@185 46
ocsenave@194 47 (defn pc-trail
ocsenave@194 48 "Track the PC for a number of ticks."
ocsenave@194 49 [state ticks]
ocsenave@194 50 (tick state)
ocsenave@194 51 (set-state! state)
ocsenave@194 52 (loop [pcs [(PC)] ]
ocsenave@194 53 (if (> (count pcs) ticks) pcs
ocsenave@194 54 (do
ocsenave@194 55 (com.aurellem.gb.Gb/tick)
ocsenave@194 56 (recur (conj pcs (PC)))))))
ocsenave@185 57
ocsenave@185 58
ocsenave@185 59
ocsenave@194 60 (defn differences [list-1 list-2]
ocsenave@194 61 (remove
ocsenave@194 62 (fn [[a b c]] (= b c))
ocsenave@194 63 (map vector
ocsenave@194 64 (range)
ocsenave@194 65 list-1
ocsenave@194 66 list-2)))
ocsenave@194 67
ocsenave@194 68 (defn pc-diff [state-1 state-2]
ocsenave@194 69 (differences (map hex (pc-trail state-1 10000))
ocsenave@194 70 (map hex (pc-trail state-2 10000))))
ocsenave@194 71
ocsenave@194 72
ocsenave@194 73 (defn memory-diff [state-1 state-2]
ocsenave@194 74 (remove
ocsenave@194 75 (fn[[a b c]] (= b c))
ocsenave@194 76 (map (comp vec (partial map hex) list)
ocsenave@194 77 (range)
ocsenave@194 78 (vec (memory state-1))
ocsenave@194 79 (vec (memory state-2)))
ocsenave@194 80 )
ocsenave@194 81 )
ocsenave@194 82
ocsenave@194 83
ocsenave@194 84
ocsenave@194 85 (defn state-speak
ocsenave@194 86 "This is when the trainer speaks."
ocsenave@194 87 []
ocsenave@194 88 (->
ocsenave@194 89 (pre-trainer-battle)
ocsenave@194 90 (set-memory 0xD354 0x0)
ocsenave@194 91 (step [:r])
ocsenave@194 92 (step)
ocsenave@194 93 (ntick 88147)
ocsenave@194 94 (tick)
ocsenave@194 95 (nstep 102)
ocsenave@194 96
ocsenave@194 97 ;(step [:r])
ocsenave@194 98
ocsenave@194 99 ;(step [:r])
ocsenave@194 100 ;(step [:r])
ocsenave@194 101 ;(step [:r])
ocsenave@194 102 ;(step [:r])
ocsenave@194 103 ))
ocsenave@194 104
ocsenave@194 105
ocsenave@194 106
ocsenave@194 107 (defn get-memory [state n]
ocsenave@194 108 (aget (memory state) n))
ocsenave@194 109
ocsenave@194 110 (defn first-change
ocsenave@194 111 "Watch the current memory location as it ticks,
ocsenave@194 112 return the first state that differs at location mem."
ocsenave@194 113 [state n]
ocsenave@194 114 (tick state)
ocsenave@194 115 (set-state! state)
ocsenave@194 116 (let [init (aget (memory state) n)]
ocsenave@194 117 (loop []
ocsenave@194 118 (if (= (aget (memory) n) init)
ocsenave@194 119 (do
ocsenave@194 120 (com.aurellem.gb.Gb/tick)
ocsenave@194 121 (recur))))
ocsenave@194 122 (update-state)))
ocsenave@194 123
ocsenave@194 124
ocsenave@194 125
ocsenave@194 126 (defn spell [state mem n]
ocsenave@194 127 (print (character-codes->str
ocsenave@194 128 (take n (drop mem
ocsenave@194 129 (vec(memory state)))))))
ocsenave@194 130
ocsenave@194 131
ocsenave@194 132 (do
ocsenave@194 133 (println)
ocsenave@194 134 (print (character-codes->str (take 6000 (drop 0xA75F4
ocsenave@194 135 (vec(com.aurellem.gb.gb-driver/rom))))))
ocsenave@194 136 )
ocsenave@194 137
ocsenave@194 138 ;(dorun (map println (view-memory* (state-surprise) 0x1AEF 1600)))
ocsenave@194 139
ocsenave@194 140
ocsenave@194 141
ocsenave@194 142 (def surprise-words
ocsenave@194 143 [0x80 0xAB 0xAB 0x7F 0xB1 0xA8 0xA6 0xA7 0xB3 0xE7 0x7F 0x8B 0xA4 0xB3 0xE0 0xB2 0x7F 0xB1 0xAE 0xAB 0xAB 0x7F 0xB3 0xA7 0xA4 0x7F 0xA3 0xA8 0xA2 0xA4 0xE7])
ocsenave@194 144
ocsenave@194 145 (defn sublist
ocsenave@194 146 "Unshifts the list until the sublist is at the start."
ocsenave@194 147 [list sub]
ocsenave@194 148 (cond
ocsenave@194 149 (empty? sub) list
ocsenave@194 150 (empty? list) nil
ocsenave@194 151 (= (take (count sub) list) sub) list
ocsenave@194 152 :else (recur (rest list) sub)))
ocsenave@194 153
ocsenave@194 154 (defn subloc
ocsenave@194 155 "Returns the position of the first occurence of sublist."
ocsenave@194 156 [list sub]
ocsenave@194 157 (loop [n 0 a list]
ocsenave@194 158 (cond
ocsenave@194 159 (empty? a) nil
ocsenave@194 160 (= (take (count sub) a) sub) n
ocsenave@194 161 :else (recur (inc n) (rest a)))))
ocsenave@194 162
ocsenave@194 163
ocsenave@194 164
ocsenave@194 165
ocsenave@194 166 (defn change-speech
ocsenave@194 167 ([state str k]
ocsenave@194 168 (loop [ops str
ocsenave@194 169 s state
ocsenave@194 170 n k]
ocsenave@194 171 (if (empty? ops) s
ocsenave@194 172 (recur
ocsenave@194 173 (rest ops)
ocsenave@194 174 (set-memory (first-change s (+ 0xC4B9 n)) (+ 0xC4B9 n)
ocsenave@194 175 (first ops))
ocsenave@194 176 (if (not= n 19) (inc n)
ocsenave@194 177 (+ n 21))))))
ocsenave@194 178 ([str k]
ocsenave@194 179 (change-speech (state-speak) str k))
ocsenave@194 180 ([str]
ocsenave@194 181 (change-speech str 0))
ocsenave@194 182
ocsenave@194 183
ocsenave@194 184
ocsenave@194 185
ocsenave@194 186 )
ocsenave@194 187