view clojure/com/aurellem/items.clj @ 105:2f8089eacab9

working on some actual assembly programs
author Robert McIntyre <rlm@mit.edu>
date Tue, 13 Mar 2012 14:40:01 -0500
parents 3305490a8f62
children 3a60bb14a64a
line wrap: on
line source
1 (ns com.aurellem.items
2 (:use (com.aurellem gb-driver vbm title))
3 ;; this is bullshit
4 (:import [com.aurellem.gb_driver SaveState]))
6 (defn game-name []
7 (map char (subvec (vec (memory)) 0x134 0x142)))
9 (def item-list-start 0xD31C)
11 (defn item-list [^SaveState state]
12 (subvec
13 (vec (memory state))
14 item-list-start
15 (+ item-list-start 150)))
17 (def item-hack-3 (read-state 77557))
19 (def item-code->item-name
20 (hash-map
21 0x01 :master-ball
22 0x02 :ultra-ball
23 0x03 :great-ball
24 0x04 :poke-ball
25 0x05 :town-map
26 0x06 :bicycle
27 0x08 :safari-ball
28 0x09 :pokedex
29 0x0A :moon-stone
30 0x0B :antidote
31 0x0C :burn-heal
32 0x0D :ice-heal
33 0x0E :awakening
34 0x0F :parlyz-heal
35 0x10 :full-restore
36 0x11 :max-potion
37 0x12 :hyper-potion
38 0x13 :super-potion
39 0x14 :potion
40 0x15 :boulderbadge
41 0x16 :cascadebadge
42 0x17 :thunderbadge
43 0x18 :rainbowbadge
44 0x19 :soulbadge
45 0x1A :marshbadge
46 0x1B :volcanobadge
47 0x1C :earthbadge
48 0x1D :escape-rope
49 0x1E :repel
50 0x1F :old-amber
51 0x20 :fire-stone
52 0x21 :thunderstone
53 0x22 :water-stone
54 0x23 :hp-up
55 0x24 :protein
56 0x25 :iron
57 0x26 :carbos
58 0x27 :calcium
59 0x28 :rare-candy
60 0x29 :dome-fossil
61 0x2A :helix-fossil
62 0x2B :secret-key
63 0x2D :bike-voucher
64 0x2E :x-accuracy
65 0x2F :leaf-stone
66 0x30 :card-key
67 0x31 :nugget
68 0x32 :pp-up
69 0x33 :poke-doll
70 0x34 :full-heal
71 0x35 :revive
72 0x36 :max-revive
73 0x37 :guard-spec
74 0x38 :super-repel
75 0x39 :max-repel
76 0x3A :dire-hit
77 0x3B :coin
78 0x3C :fresh-water
79 0x3D :soda-pop
80 0x3E :lemonade
81 0x3F :s.s.ticket
82 0x40 :gold-teeth
83 0x41 :x-attach
84 0x42 :x-defend
85 0x43 :x-speed
86 0x44 :x-special
87 0x45 :coin-case
88 0x46 :oaks-parcel
89 0x47 :itemfinder
90 0x48 :silph-scope
91 0x49 :poke-flute
92 0x4A :lift-key
93 0x4B :exp.all
94 0x4C :old-rod
95 0x4D :good-rod
96 0x4E :super-rod
97 0x4F :pp-up
98 0x50 :ether
99 0x51 :max-ether
100 0x52 :elixer
101 0x53 :max-elixer
102 0xC4 :HM01 ;; cut
103 0xC5 :HM02 ;; fly
104 0xC6 :HM03 ;; surf
105 0xC7 :HM04 ;; strength
106 0xC8 :HM05 ;; flash
107 0xC9 :TM01 ;; mega punch
108 0xCA :TM02 ;; razor wind
109 0xCB :TM03 ;; swords dance
110 0xCC :TM04 ;; whirlwind
111 0xCD :TM05 ;; mega kick
112 0xCE :TM06 ;; toxic
113 0xCF :TM07 ;; horn drill
114 0xD0 :TM08 ;; body slam
115 0xD1 :TM09 ;; take down
116 0xD2 :TM10 ;; double-edge
117 0xD3 :TM11 ;; bubblebeam
118 0xD4 :TM12 ;; water gun
119 0xD5 :TM13 ;; ice beam
120 0xD6 :TM14 ;; blizzard
121 0xD7 :TM15 ;; hyper beam
122 0xD8 :TM16 ;; pay day
123 0xD9 :TM17 ;; submission
124 0xDA :TM18 ;; counter
125 0xDB :TM19 ;; seismic toss
126 0xDC :TM20 ;; rage
127 0xDD :TM21 ;; mega drain
128 0xDE :TM22 ;; solarbeam
129 0xDF :TM23 ;; dragon rage
130 0xE0 :TM24 ;; thunderbolt
131 0xE1 :TM25 ;; thunder
132 0xE2 :TM26 ;; earthquake
133 0xE3 :TM27 ;; fissure
134 0xE4 :TM28 ;; dig
135 0xE5 :TM29 ;; psychic
136 0xE6 :TM30 ;; teleport
137 0xE7 :TM31 ;; mimic
138 0xE8 :TM32 ;; double team
139 0xE9 :TM33 ;; reflect
140 0xEA :TM34 ;; bide
141 0xEB :TM35 ;; metronome
142 0xEC :TM36 ;; self destruct
143 0xED :TM37 ;; eggbomb
144 0xEE :TM38 ;; fire blast
145 0xEF :TM39 ;; swift
146 0xF0 :TM40 ;; skull bash
147 0xF1 :TM41 ;; softboiled
148 0xF2 :TM42 ;; dream eater
149 0xF3 :TM43 ;; sky attack
150 0xF4 :TM44 ;; rest
151 0xF5 :TM45 ;; thunder wave
152 0xF6 :TM46 ;; psywave
153 0xF7 :TM47 ;; explosion
154 0xF8 :TM48 ;; rock slide
155 0xF9 :TM49 ;; tri attack
156 0xFA :TM50 ;; substitute
157 0xFB :TM51 ;; "cut"
158 0xFC :TM52 ;; "fly"
159 0xFD :TM53 ;; "surf"
160 0xFE :TM54 ;; "strength"
161 0xFF :end-of-list-sentinel
162 ))
164 (def item-name->item-code
165 (zipmap (vals item-code->item-name)
166 (keys item-code->item-name)))
168 (defn inventory [^SaveState state]
169 (let [items (item-list state)]
170 (map
171 (fn [[item-code quantity]]
172 [(item-code->item-name
173 item-code
174 (str ":0x" (.toUpperCase (Integer/toHexString item-code))))
175 quantity])
176 (partition
177 2
178 (next (take-while (partial not= 255) items))))))
180 (defn print-inventory
181 ([] (print-inventory @current-state))
182 ([^SaveState state]
183 (println
184 (let [inv (inventory state)]
185 (reduce
186 str
187 (concat
188 ["+-------------------+----------+\n"
189 "|##| Item | Quantity |\n"
190 "+--+----------------+----------+\n"]
192 (map
193 (fn [index [item-name quantity]]
194 (str
195 (format "|%-2d| %-14s | %3d |\n" index
196 (apply str (rest (str item-name)))
197 quantity)))
198 (range 0 (count inv)) inv)
199 ["+--+----------------+----------+\n"]))))
200 state))
202 (defn inventory-codes [inventory]
203 (flatten
204 (concat [(count inventory)]
205 (map (fn [[item-name quantity]]
206 [(item-name->item-code item-name)
207 quantity]) inventory)
208 [(item-name->item-code :end-of-list-sentinel)])))
210 (defn set-inv-mem [^SaveState state inv-codes]
211 (set-state! state)
212 (let [mem (memory state)]
213 (dorun (map (fn [index val]
214 (aset mem index val))
215 (range item-list-start
216 (+ item-list-start (count inv-codes))) inv-codes))
217 (write-memory! mem)))
220 (defn set-inventory [^SaveState state new-inventory]
221 (set-state! state)
222 (let [mem (memory state)
223 inv (inventory-codes new-inventory)]
225 (dorun (map (fn [index val]
226 (aset mem index val))
227 (range item-list-start
228 (+ item-list-start (count inv))) inv))
229 (write-memory! mem)
230 (update-state)))
232 (defn give
233 ([^SaveState state items]
234 (set-inventory state
235 (concat items (inventory state))))
236 ([items]
237 (give @current-state items)))
240 (def gliched-tms
241 [[:TM51 1]
242 [:TM52 1]
243 [:TM53 1]
244 [:TM54 1]])
246 (def good-items
247 [[:bicycle 1]
248 [:ultra-ball 15]
249 [:pp-up 1]
250 [:master-ball 5]
251 [:rare-candy 99]
252 [:full-restore 25]
253 [:max-revive 8]
254 [:max-repel 40]
255 [:TM25 1]
256 [:TM11 1]
257 [:TM15 1]
258 ])
260 (def some-badges
261 [[:cascadebadge 1]
262 [:thunderbadge 1]
263 [:rainbowbadge 1]
264 [:soulbadge 1]
265 ])
267 (defn run-item-program
268 "This is my first assembly/item program!
269 it just increments BC by one.
271 The code places a 3 'great balls' at the beginning of the
272 inventory, then directly sets the program counter to start
273 executing at the position of the 'great balls' in memory.
275 Since a 'great ball' is represented in memory as 0x03, which
276 corresponts to the opcode which increments BC by one, that is
277 what happens. Then the program counter to the 0x03 quantity entry
278 and BC is incremented again.
280 Obviously, the game crashes more or less immediately after the
281 program counter advances past the 'great balls' into the next items
282 in the inventory, thus I call shutdown! before anything bad happens."
283 []
284 (set-inventory (read-state 578544) [[:great-ball 3]])
285 (print-inventory)
286 (println "3 ticks") (tick) (tick) (tick)
287 (println "PC before:" (PC))
288 (println "BC before:" (BC))
289 (PC! (inc item-list-start))
290 (println "PC after setting:" (PC))
291 (println "data at PC:" (aget (memory) (PC)))
292 (println "one tick")
293 (tick)
294 (println "PC after one tick:" (PC))
295 (println "BC after one tick:" (BC))
296 (tick)
297 (println "PC after two ticks:" (PC))
298 (println "BC after two ticks:" (BC))
300 (shutdown!))
303 (defn test-opcodes-1
304 []
305 (set-inv-mem (read-state 578544)
306 [20 0x02 0x00 0x00 0x02 0x00 0x00
307 0x00 0x0 0xFF])
308 (print-inventory)
309 (println "3 ticks") (tick) (tick) (tick)
310 (println "PC before:" (PC))
311 (println "BC before:" (BC))
312 (println "AF:" (AF))
313 (PC! (inc item-list-start))
314 (BC! (+ 1 item-list-start))
315 (println "PC after setting:" (PC))
316 (println "data at PC:" (aget (memory) (PC)))
317 (println "data at " (BC) "(BC):" (aget (memory) (BC)))
319 (println "one tick")
320 (tick)
321 (println "PC after one tick:" (PC))
322 (println "BC after one tick:" (BC))
323 (println "data at PC:" (aget (memory) (PC)))
324 (println "data at " (BC) "(BC):" (aget (memory) (BC)))
325 (tick)
326 (AF! 0xFFFF)
327 (println "PC after two ticks:" (PC))
328 (println "BC after two ticks:" (BC))
329 (println "data at PC:" (aget (memory) (PC)))
330 (println "data at " (BC) "(BC):" (aget (memory) (BC)))
331 (tick)
332 (println "PC after three ticks:" (PC))
333 (println "BC after three ticks:" (BC))
334 (println "data at PC:" (aget (memory) (PC)))
335 (println "data at " (BC) "(BC):" (aget (memory) (BC)))
336 (tick)
337 (println "PC after four ticks:" (PC))
338 (println "BC after four ticks:" (BC))
339 (println "data at PC:" (aget (memory) (PC)))
340 (println "data at " (BC) "(BC):" (aget (memory) (BC)))
341 (tick)
342 (println "PC after five ticks:" (PC))
343 (println "BC after five ticks:" (BC))
344 (println "data at PC:" (aget (memory) (PC)))
345 (println "data at " (BC) "(BC):" (aget (memory) (BC)))
346 (print-inventory)
347 (shutdown!))
351 (defn test-opcodes-2
352 []
353 (set-inv-mem (read-state 578544)
354 [20 0x08 0x1D 0xD3 0x00 0x00 0x00
355 0x00 0x0 0xFF])
356 (print-inventory)
357 (println "3 ticks") (tick) (tick) (tick)
358 (println "PC before:" (PC))
359 (println "SP:" (SP))
360 (PC! (inc item-list-start))
361 (println "PC after setting:" (PC))
362 (println "SP:" (Integer/toBinaryString (SP)))
363 (println "data at PC:" (aget (memory) (PC)))
364 (println "data at 0xD31D:" (Integer/toBinaryString (aget (memory) 0xD31D)))
365 (println "data at 0xD31E:" (Integer/toBinaryString (aget (memory) 0xD31E)))
366 (println "one tick")
367 (tick)
368 (println "PC after one tick:" (PC))
369 (println "data at PC:" (aget (memory) (PC)))
370 (println "data at 0xD31D:" (Integer/toBinaryString (aget (memory) 0xD31D)))
371 (println "data at 0xD31E:" (Integer/toBinaryString (aget (memory) 0xD31E)))
372 (tick) (tick) (tick)
373 (println "PC aftter four tick:" (PC))
374 (println "data at PC:" (aget (memory) (PC)))
375 (println "data at 0xD31D:" (aget (memory) 0xD31D))
377 (print-inventory)
378 (shutdown!))