view clojure/com/aurellem/gb/items.clj @ 582:393b49a83394

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