view clojure/com/aurellem/world/practice.clj @ 247:22f58fa47c3c

starting work on unoptimized bootstrap script.
author Robert McIntyre <rlm@mit.edu>
date Mon, 26 Mar 2012 03:08:54 -0500
parents d5dddf33543c
children b2f9a0cb13e3
line wrap: on
line source
1 (ns com.aurellem.world.practice
2 (:use (com.aurellem.gb gb-driver))
4 (:use (com.aurellem.gb saves util constants gb-driver vbm items assembly characters))
5 (:use (com.aurellem.run title))
6 (:use (com.aurellem.exp pokemon))
7 (:use (com.aurellem.exp item-bridge))
8 (:import [com.aurellem.gb.gb_driver SaveState]))
11 ;;(def original-rom (rom(root)))
14 (def hex-pc (comp hex PC))
16 (defn nstep [state n]
17 (if (zero? n) state
18 (recur (step state) (dec n))))
21 (defn view-memory* [state start length]
22 ((comp vec map)
23 #((comp aget) (memory state) %)
24 (range start (+ start length))))
27 (defn state-surprise
28 "This is one tick before the trainer goes [!]"
29 []
30 (->
31 (pre-trainer-battle)
32 (step [:r])
33 (step)
34 (ntick 88147)
35 ;(step [:r])
37 ;(step [:r])
38 ;(step [:r])
39 ;(step [:r])
40 ;(step [:r])
41 ))
42 (defn state-inject
43 "I have replaced the letter e with e-acute @ 0xC4E8."
44 []
45 (read-state "inject-surprise"))
50 (defn pc-trail
51 "Track the PC for a number of ticks."
52 [state ticks]
53 (tick state)
54 (set-state! state)
55 (loop [pcs [(PC)] ]
56 (if (> (count pcs) ticks) pcs
57 (do
58 (com.aurellem.gb.Gb/tick)
59 (recur (conj pcs (PC)))))))
63 (defn differences [list-1 list-2]
64 (remove
65 (fn [[a b c]] (= b c))
66 (map vector
67 (range)
68 list-1
69 list-2)))
71 (defn pc-diff [state-1 state-2]
72 (differences (map hex (pc-trail state-1 10000))
73 (map hex (pc-trail state-2 10000))))
76 (defn memory-diff [state-1 state-2]
77 (remove
78 (fn[[a b c]] (= b c))
79 (map (comp vec (partial map hex) list)
80 (range)
81 (vec (memory state-1))
82 (vec (memory state-2)))
83 )
84 )
88 (defn state-speak
89 "This is when the trainer speaks."
90 []
91 (->
92 (pre-trainer-battle)
93 (set-memory 0xD354 0x0)
94 (step [:r])
95 (step)
96 (ntick 88147)
97 (tick)
98 (nstep 102)
100 ;(step [:r])
102 ;(step [:r])
103 ;(step [:r])
104 ;(step [:r])
105 ;(step [:r])
106 ))
110 (defn get-memory [state n]
111 (aget (memory state) n))
113 (defn first-change
114 "Watch the current memory location as it ticks,
115 return the first state that differs at location mem."
116 [state n]
117 (tick state)
118 (set-state! state)
119 (let [init (aget (memory state) n)]
120 (loop []
121 (if (= (aget (memory) n) init)
122 (do
123 (com.aurellem.gb.Gb/tick)
124 (recur))))
125 (update-state)))
130 (defn spell-array
131 [array mem n]
132 (character-codes->str
133 (take n (drop mem
134 (vec array)))))
136 (defn spell
137 ([state mem n]
138 (spell (memory state) mem n))
139 ([mem n] (spell @current-state mem n)))
143 (comment
144 (println)
145 (print (character-codes->str (take 6000 (drop 0xA75F4
146 (vec(com.aurellem.gb.gb-driver/rom)))))))
148 ;(dorun (map println (view-memory* (state-surprise) 0x1AEF 1600)))
152 (def surprise-words
153 [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])
155 (defn sublist
156 "Unshifts the list until the sublist is at the start."
157 [list sub]
158 (cond
159 (empty? sub) list
160 (empty? list) nil
161 (= (take (count sub) list) sub) list
162 :else (recur (rest list) sub)))
164 (defn find-sublist
165 "Returns the position of the first occurence of sublist."
166 [list sub]
167 (loop [n 0 a list]
168 (cond
169 (empty? a) nil
170 (= (take (count sub) a) sub) n
171 :else (recur (inc n) (rest a)))))
173 (defn find-sublists
174 "Returns a vector of the occurences of sublists."
175 [list sub]
176 (let [m (find-sublist list sub)]
177 (if (nil? m) '()
178 (cons m
179 (map (partial + (inc m))
180 (find-sublists
181 (drop (inc m) list)
182 sub))))))
184 (defn spelling-bee
185 "Search for the given string in ROM, returning short snippets of
186 text around the results."
187 [str k]
188 (let [rom (rom(root))]
189 (map
190 (fn [n]
191 [(hex n)
192 (character-codes->str (take k (drop n rom)))])
194 (find-sublists
195 rom
196 (str->character-codes str)))))
201 (defn change-speech
202 ([state str k]
203 (loop [ops str
204 s state
205 n k]
206 (if (empty? ops) s
207 (recur
208 (rest ops)
209 (set-memory (first-change s (+ 0xC4B9 n)) (+ 0xC4B9 n)
210 (first ops))
211 (if (not= n 19) (inc n)
212 (+ n 21))))))
213 ([str k]
214 (change-speech (state-speak) str k))
215 ([str]
216 (change-speech str 0)))
220 (defn rewrite-memory
221 "Alters the vector of memory. Treats strings as lists of character
222 ops."
223 ([mem start strs-or-ops]
224 (let [x (first strs-or-ops)]
225 (cond (empty? strs-or-ops) mem
226 (string? x)
228 (recur mem start
229 (concat
230 (str->character-codes x)
231 (rest strs-or-ops)))
232 :else
233 (recur
234 (assoc mem start x)
235 (inc start)
236 (rest strs-or-ops))))))
238 (def rewrite-rom
239 "Alters the ROM array using write-memory."
240 (partial rewrite-memory (vec (rom(root)))))
242 (defn restore-rom! [] (write-rom! original-rom))
246 (def oak-intro
248 (list "Hello there!"
249 0x4F
250 "Welcome to the"
251 0x55
252 "world of "
253 0x54
254 "MON!"
255 0x51
256 "My name is OAK!"
257 0x4F
258 "People call me"
259 0x55
260 "the "
261 0x54
262 "MON PROF!"
263 0x58
264 "This world is"
265 0x4F
266 "inhabited by"
267 0x55
268 "creatures called"
269 0x55
270 0x54
271 "MON!"
272 0x50
273 0x50
274 0x51
275 "For some people, "
276 0x4F
277 0x54
278 "MON are"
279 0x55
280 "pets. Others use"
281 0x55
282 "them for fights."
283 0x51
284 "Myself..."
285 0x51
286 "I study "
287 0x54
288 "MON"
289 0x4F
290 "as a profession."
291 0x58
292 "First, what is"
293 0x4F
294 "your name?"
295 0x58
296 "This is my grand-"
297 0x4F
298 "son. He's been"
299 0x55
300 "your rival since"
301 0x55
302 "you were a baby."
303 0x51
304 "...Erm, what is"
305 0x4F
306 "his name again?"
307 0x58
308 0x52
309 "!"
310 0x51
311 "Your very own"
312 0x4F
313 0x54
314 "MON legend is"
315 0x55
316 "about to unfold!"
317 0x51
318 "A world of dreams"
319 0x4F
320 "and adventures"
321 0x55
322 "with "
323 0x54
324 "MON"
325 0x55
326 "awaits! Let's go!"
327 0x57) )
329 (defn state-intro
330 "Professor Oak speaks!"
331 []
332 (do
333 (->
334 (rewrite-rom 0xA07BB
335 (list 0x87 0xA4 0xAB 0xAB 0xAE 0x7F 0xB3 0xA7 0xA4 0xB1 0xA4 0xE7 0x4F 0x96 0xA4 0xAB 0xA2 0xAE 0xAC 0xA4 0x7F 0xB3 0xAE 0x7F 0xB3 0xA7 0xA4 0x55 0xB6 0xAE 0xB1 0xAB 0xA3 0x7F 0xAE 0xA5 0x7F 0x54 0x8C 0x8E 0x8D 0xE7 0x51 0x8C 0xB8 0x7F 0xAD 0xA0 0xAC 0xA4 0x7F 0xA8 0xB2 0x7F 0x8E 0x80 0x8A 0xE7 0x4F 0x8F 0xA4 0xAE 0xAF 0xAB 0xA4 0x7F 0xA2 0xA0 0xAB 0xAB 0x7F 0xAC 0xA4 0x55 0xB3 0xA7 0xA4 0x7F 0x8F 0x8C 0x8E 0x8D 0x7F 0x8F 0x91 0x8E 0x85 0xE7 0x58 0x93 0xA7 0xA8 0xB2 0x7F 0xB6 0xAE 0xB1 0xAB 0xA3 0x7F 0xA8 0xB2 0x4F 0xA8 0xAD 0xA7 0xA0 0xA1 0xA8 0xB3 0xA4 0xA3 0x7F 0xA1 0xB8 0x55 0xA2 0xB1 0xA4 0xA0 0xB3 0xB4 0xB1 0xA4 0xB2 0x7F 0xA2 0xA0 0xAB 0xAB 0xA4 0xA3 0x55 0x54 0x8C 0x8E 0x8D 0xE7 0x50 0x50 0x51 0x85 0xAE 0xB1 0x7F 0xB2 0xAE 0xAC 0xA4 0x7F 0xAF 0xA4 0xAE 0xAF 0xAB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xE7 0x58 0x1 0x6D 0xCD 0x0)
336 ;; (list
337 ;; "Sleeping on the"
338 ;; 0x4F
339 ;; "job again are we?"
340 ;; 0x51
341 ;; 0x00
342 ;; "test"
343 ;; 0x59
344 ;; "EOM")
347 )
348 (int-array)
349 (write-rom!))
350 (root)
352 )
354 )
356 (comment
357 "Hello there![0x4F]Welcome to the[0x55]world of [POKE]MON![0x51]My
358 name is OAK![0x4F]People call me[0x55]the [POKE]MON PROF![0x58]This
359 world is[0x4F]inhabited by[0x55]creatures
360 called[0x55][POKE]MON![0x50][0x50][0x51]For some people,
361 [0x4F][POKE]MON are[0x55]pets. Others use[0x55]them for
362 fights.[0x51]Myself...[0x51]I study [POKE]MON[0x4F]as a
363 profession.[0x58]First, what is[0x4F]your name?[0x58]This is my
364 grand-[0x4F]son. He's been[0x55]your rival since[0x55]you were a
365 baby.[0x51]...Erm, what is[0x4F]his name again?[0x58][RED]![0x51]Your
366 very own[0x4F][POKE]MON legend is[0x55]about to unfold![0x51]A world
367 of dreams[0x4F]and adventures[0x55]with [POKE]MON[0x55]awaits! Let's
368 go![0x57]")