comparison clojure/com/aurellem/items.clj @ 103:3305490a8f62

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