view clojure/com/aurellem/gb/items.clj @ 187:426fce7e609d

successfully captured program counter!
author Robert McIntyre <rlm@mit.edu>
date Thu, 22 Mar 2012 07:36:54 -0500
parents f0c1e5574e81
children 531e1342ff56
line wrap: on
line source
1 (ns com.aurellem.gb.items
2 (:use (com.aurellem.gb gb-driver util))
3 ;; this is bullshit
4 (:import [com.aurellem.gb.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
12 ([^SaveState state]
13 (subvec
14 (vec (memory state))
15 item-list-start
16 (+ item-list-start 255)))
17 ([] (item-list @current-state)))
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 0x07 :surfboard
28 0x08 :safari-ball
29 0x09 :pokedex
30 0x0A :moon-stone
31 0x0B :antidote
32 0x0C :burn-heal
33 0x0D :ice-heal
34 0x0E :awakening
35 0x0F :parlyz-heal
36 0x10 :full-restore
37 0x11 :max-potion
38 0x12 :hyper-potion
39 0x13 :super-potion
40 0x14 :potion
41 0x15 :boulderbadge
42 0x16 :cascadebadge
43 0x17 :thunderbadge
44 0x18 :rainbowbadge
45 0x19 :soulbadge
46 0x1A :marshbadge
47 0x1B :volcanobadge
48 0x1C :earthbadge
49 0x1D :escape-rope
50 0x1E :repel
51 0x1F :old-amber
52 0x20 :fire-stone
53 0x21 :thunderstone
54 0x22 :water-stone
55 0x23 :hp-up
56 0x24 :protein
57 0x25 :iron
58 0x26 :carbos
59 0x27 :calcium
60 0x28 :rare-candy
61 0x29 :dome-fossil
62 0x2A :helix-fossil
63 0x2B :secret-key
64 0x2D :bike-voucher
65 0x2E :x-accuracy
66 0x2F :leaf-stone
67 0x30 :card-key
68 0x31 :nugget
69 0x32 :pp-up
70 0x33 :poke-doll
71 0x34 :full-heal
72 0x35 :revive
73 0x36 :max-revive
74 0x37 :guard-spec
75 0x38 :super-repel
76 0x39 :max-repel
77 0x3A :dire-hit
78 0x3B :coin
79 0x3C :fresh-water
80 0x3D :soda-pop
81 0x3E :lemonade
82 0x3F :s.s.ticket
83 0x40 :gold-teeth
84 0x41 :x-attack
85 0x42 :x-defend
86 0x43 :x-speed
87 0x44 :x-special
88 0x45 :coin-case
89 0x46 :oaks-parcel
90 0x47 :itemfinder
91 0x48 :silph-scope
92 0x49 :poke-flute
93 0x4A :lift-key
94 0x4B :exp.all
95 0x4C :old-rod
96 0x4D :good-rod
97 0x4E :super-rod
98 0x4F :pp-up
99 0x50 :ether
100 0x51 :max-ether
101 0x52 :elixer
102 0x53 :max-elixer
103 0xC4 :HM01 ;; cut
104 0xC5 :HM02 ;; fly
105 0xC6 :HM03 ;; surf
106 0xC7 :HM04 ;; strength
107 0xC8 :HM05 ;; flash
108 0xC9 :TM01 ;; mega punch
109 0xCA :TM02 ;; razor wind
110 0xCB :TM03 ;; swords dance
111 0xCC :TM04 ;; whirlwind
112 0xCD :TM05 ;; mega kick
113 0xCE :TM06 ;; toxic
114 0xCF :TM07 ;; horn drill
115 0xD0 :TM08 ;; body slam
116 0xD1 :TM09 ;; take down
117 0xD2 :TM10 ;; double-edge
118 0xD3 :TM11 ;; bubblebeam
119 0xD4 :TM12 ;; water gun
120 0xD5 :TM13 ;; ice beam
121 0xD6 :TM14 ;; blizzard
122 0xD7 :TM15 ;; hyper beam
123 0xD8 :TM16 ;; pay day
124 0xD9 :TM17 ;; submission
125 0xDA :TM18 ;; counter
126 0xDB :TM19 ;; seismic toss
127 0xDC :TM20 ;; rage
128 0xDD :TM21 ;; mega drain
129 0xDE :TM22 ;; solarbeam
130 0xDF :TM23 ;; dragon rage
131 0xE0 :TM24 ;; thunderbolt
132 0xE1 :TM25 ;; thunder
133 0xE2 :TM26 ;; earthquake
134 0xE3 :TM27 ;; fissure
135 0xE4 :TM28 ;; dig
136 0xE5 :TM29 ;; psychic
137 0xE6 :TM30 ;; teleport
138 0xE7 :TM31 ;; mimic
139 0xE8 :TM32 ;; double team
140 0xE9 :TM33 ;; reflect
141 0xEA :TM34 ;; bide
142 0xEB :TM35 ;; metronome
143 0xEC :TM36 ;; self destruct
144 0xED :TM37 ;; eggbomb
145 0xEE :TM38 ;; fire blast
146 0xEF :TM39 ;; swift
147 0xF0 :TM40 ;; skull bash
148 0xF1 :TM41 ;; softboiled
149 0xF2 :TM42 ;; dream eater
150 0xF3 :TM43 ;; sky attack
151 0xF4 :TM44 ;; rest
152 0xF5 :TM45 ;; thunder wave
153 0xF6 :TM46 ;; psywave
154 0xF7 :TM47 ;; explosion
155 0xF8 :TM48 ;; rock slide
156 0xF9 :TM49 ;; tri attack
157 0xFA :TM50 ;; substitute
158 0xFB :TM51 ;; "cut"
159 0xFC :TM52 ;; "fly"
160 0xFD :TM53 ;; "surf"
161 0xFE :TM54 ;; "strength"
162 0xFF :end-of-list-sentinel ;; also "flash"
163 ))
165 (def item-name->item-code
166 (zipmap (vals item-code->item-name)
167 (keys item-code->item-name)))
169 (defn inventory [^SaveState state]
170 (let [items (item-list state)]
171 (map
172 (fn [[item-code quantity]]
173 [(item-code->item-name
174 item-code
175 item-code)
176 quantity])
177 (partition
178 2
179 (next (take-while (partial not= 255) items))))))
181 (defn print-inventory
182 ([] (print-inventory @current-state))
183 ([^SaveState state]
184 (println
185 (let [inv (inventory state)]
186 (reduce
187 str
188 (concat
189 ["+-------------------+----------+\n"
190 "|##| Item | Quantity |\n"
191 "+--+----------------+----------+\n"]
193 (map
194 (fn [index [item-name quantity]]
195 (str
196 (format "|%-2d| %-14s | %3d |\n" index
197 (apply str (rest (str item-name)))
198 quantity)))
199 (range 0 (count inv)) inv)
200 ["+--+----------------+----------+\n"]))))
201 state))
203 (defn inventory-codes [inventory]
204 (flatten
205 (concat [(count inventory)]
206 (map (fn [[item-name quantity]]
207 [(item-name->item-code item-name item-name)
208 quantity]) inventory)
209 [(item-name->item-code :end-of-list-sentinel)])))
211 (defn set-inv-mem [^SaveState state inv-codes]
212 (set-memory-range state item-list-start
213 inv-codes))
216 (defn set-inventory [^SaveState state new-inventory]
217 (set-inv-mem state (inventory-codes new-inventory)))
219 (defn give-items
220 ([^SaveState state items]
221 (set-inventory state
222 (concat items (inventory state))))
223 ([items]
224 (give-items @current-state items)))
226 (defn clear-inventory
227 ([^SaveState state]
228 (set-inventory state []))
229 ([] (clear-inventory @current-state)))