view clojure/com/aurellem/items.clj @ 102:4d9ce3188655

made first 'hello-world' item based assembly insertion.
author Robert McIntyre <rlm@mit.edu>
date Mon, 12 Mar 2012 20:03:05 -0500
parents 2090bcb78f44
children 3305490a8f62
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 1 :master-ball
22 2 :ultra-ball
23 3 :great-ball
24 4 :poke-ball
25 5 :town-map
26 6 :bicycle
27 8 :safari-ball
28 9 :pokedex
29 10 :moon-stone
30 11 :antidote
31 12 :burn-heal
32 13 :ice-heal
33 14 :awakening
34 15 :parlyz-heal
35 16 :full-restore
36 17 :max-potion
37 18 :hyper-potion
38 19 :super-potion
39 20 :potion
40 21 :boulderbadge
41 22 :cascadebadge
42 23 :thunderbadge
43 24 :rainbowbadge
44 25 :soulbadge
45 26 :marshbadge
46 27 :volcanobadge
47 28 :earthbadge
48 29 :escape-rope
49 30 :repel
50 31 :old-amber
51 32 :fire-stone
52 33 :thunderstone
53 34 :water-stone
54 35 :hp-up
55 36 :protein
56 37 :iron
57 38 :carbos
58 39 :calcium
59 40 :rare-candy
60 41 :dome-fossil
61 42 :helix-fossil
62 43 :secret-key
63 45 :bike-voucher
64 46 :x-accuracy
65 47 :leaf-stone
66 48 :card-key
67 49 :nugget
68 50 :pp-up
69 51 :poke-doll
70 52 :full-heal
71 53 :revive
72 54 :max-revive
73 55 :guard-spec
74 56 :super-repel
75 57 :max-repel
76 58 :dire-hit
77 59 :coin
78 60 :fresh-water
79 61 :soda-pop
80 62 :lemonade
81 63 :s.s.ticket
82 64 :gold-teeth
83 65 :x-attach
84 66 :x-defend
85 67 :x-speed
86 68 :x-special
87 69 :coin-case
88 70 :oaks-parcel
89 71 :itemfinder
90 72 :silph-scope
91 73 :poke-flute
92 74 :lift-key
93 75 :exp.all
94 76 :old-rod
95 77 :good-rod
96 78 :super-rod
97 79 :pp-up
98 80 :ether
99 81 :max-ether
100 82 :elixer
101 83 :max-elixer
102 196 :HM01 ;; cut
103 197 :HM02 ;; fly
104 198 :HM03 ;; surf
105 199 :HM04 ;; strength
106 200 :HM05 ;; flash
107 201 :TM01 ;; mega punch
108 202 :TM02 ;; razor wind
109 203 :TM03 ;; swords dance
110 204 :TM04 ;; whirlwind
111 205 :TM05 ;; mega kick
112 206 :TM06 ;; toxic
113 207 :TM07 ;; horn drill
114 208 :TM08 ;; body slam
115 209 :TM09 ;; take down
116 210 :TM10 ;; double-edge
117 211 :TM11 ;; bubblebeam
118 212 :TM12 ;; water gun
119 213 :TM13 ;; ice beam
120 214 :TM14 ;; blizzard
121 215 :TM15 ;; hyper beam
122 216 :TM16 ;; pay day
123 217 :TM17 ;; submission
124 218 :TM18 ;; counter
125 219 :TM19 ;; seismic toss
126 220 :TM20 ;; rage
127 221 :TM21 ;; mega drain
128 222 :TM22 ;; solarbeam
129 223 :TM23 ;; dragon rage
130 224 :TM24 ;; thunderbolt
131 225 :TM25 ;; thunder
132 226 :TM26 ;; earthquake
133 227 :TM27 ;; fissure
134 228 :TM28 ;; dig
135 229 :TM29 ;; psychic
136 230 :TM30 ;; teleport
137 231 :TM31 ;; mimic
138 232 :TM32 ;; double team
139 233 :TM33 ;; reflect
140 234 :TM34 ;; bide
141 235 :TM35 ;; metronome
142 236 :TM36 ;; self destruct
143 237 :TM37 ;; eggbomb
144 238 :TM38 ;; fire blast
145 239 :TM39 ;; swift
146 240 :TM40 ;; skull bash
147 241 :TM41 ;; softboiled
148 242 :TM42 ;; dream eater
149 243 :TM43 ;; sky attack
150 244 :TM44 ;; rest
151 245 :TM45 ;; thunder wave
152 246 :TM46 ;; psywave
153 247 :TM47 ;; explosion
154 248 :TM48 ;; rock slide
155 249 :TM49 ;; tri attack
156 250 :TM50 ;; substitute
157 251 :TM51 ;; "cut"
158 252 :TM52 ;; "fly"
159 253 :TM53 ;; "surf"
160 254 :TM54 ;; "strength"
161 255 :end-of-list-sentinel))
163 (def item-name->item-code
164 (zipmap (vals item-code->item-name)
165 (keys item-code->item-name)))
167 (defn inventory [^SaveState state]
168 (let [items (item-list state)]
169 (map
170 (fn [[item-code quantity]]
171 [(item-code->item-name item-code)
172 quantity])
173 (partition
174 2
175 (next (take-while (partial not= 255) items))))))
177 (defn print-inventory
178 ([] (print-inventory @current-state))
179 ([^SaveState state]
180 (println
181 (let [inv (inventory state)]
182 (reduce
183 str
184 (concat
185 ["+-------------------+----------+\n"
186 "|##| Item | Quantity |\n"
187 "+--+----------------+----------+\n"]
189 (map
190 (fn [index [item-name quantity]]
191 (str
192 (format "|%-2d| %-14s | %3d |\n" index
193 (apply str (rest (str item-name)))
194 quantity)))
195 (range 0 (count inv)) inv)
196 ["+--+----------------+----------+\n"]))))))
198 (defn inventory-codes [inventory]
199 (flatten
200 (concat [(count inventory)]
201 (map (fn [[item-name quantity]]
202 [(item-name->item-code item-name)
203 quantity]) inventory)
204 [(item-name->item-code :end-of-list-sentinel)])))
206 (defn set-inventory [^SaveState state new-inventory]
207 (set-state! state)
208 (let [mem (memory state)
209 inv (inventory-codes new-inventory)]
211 (dorun (map (fn [index val]
212 (aset mem index val))
213 (range item-list-start
214 (+ item-list-start (count inv))) inv))
215 (write-memory! mem)
216 (update-state)))
218 (def gliched-tms
219 [[:TM51 1]
220 [:TM52 1]
221 [:TM53 1]
222 [:TM54 1]])
224 (def good-items
225 [[:bicycle 1]
226 [:ultra-ball 15]
227 [:pp-up 1]
228 [:master-ball 5]
229 [:rare-candy 99]
230 [:full-restore 25]
231 [:max-revive 8]
232 [:max-repel 40]
233 [:TM25 1]
234 [:TM11 1]
235 [:TM15 1]
236 ])
238 (def some-badges
239 [[:cascadebadge 1]
240 [:thunderbadge 1]
241 [:rainbowbadge 1]
242 [:soulbadge 1]
243 ])
245 (defn run-item-program
246 "This is my first assembly/item program!
247 it just increments BC by one.
249 The code places a single 'great ball' at the beginning of the
250 inventory, then directly sets the program counter to start
251 executing at the position of the 'great ball' in memory.
253 Since a 'great ball' is represented in memory as 0x03, which
254 corresponts to the opcode which increments BC by one, that is
255 what happens.
257 Obviously, the game crashes more or less immediately after the
258 program counter advances past the 'great ball' into the next items
259 in the inventory."
260 []
261 (set-inventory (read-state 578544) [[:great-ball 1]])
262 (print-inventory)
263 (println "3 ticks") (tick) (tick) (tick)
264 (println "PC before:" (PC))
265 (println "BC before:" (BC))
266 (PC! (inc item-list-start))
267 (println "PC after setting:" (PC))
268 (println "data at PC:" (aget (memory) (PC)))
269 (println "one tick")
270 (tick)
271 (println "PC after one tick:" (PC))
272 (println "BC after one tick:" (BC)))