view clojure/com/aurellem/exp/pokemon.clj @ 169:4857f59f63a6

added functions to read and set DV values for pokemon.
author Robert McIntyre <rlm@mit.edu>
date Wed, 21 Mar 2012 01:37:10 -0500
parents 04dfda91db9c
children 63ec3db6f6d1
line wrap: on
line source
1 (ns com.aurellem.exp.pokemon
2 "Here I find out how pokemon are stored in memory."
3 (:use (com.aurellem.gb gb-driver items assembly util
4 constants
5 characters species moves))
6 (:import java.io.File)
7 (:import [com.aurellem.gb.gb_driver SaveState]))
10 (def pidgeot-lvl-36 (mid-game))
13 (def pidgeot-lvl-37 (read-state "pidgeot-lvl-37"))
16 (def pidgeot-lvl-38 (read-state "pidgeot-lvl-38"))
19 (def pidgeot-lvl-39 (read-state "pidgeot-lvl-39"))
22 (def pidgeot-lvl-40 (read-state "pidgeot-lvl-40"))
25 (defn level-analysis []
26 (apply common-differences
27 (map (comp vec memory)
28 [pidgeot-lvl-36
29 pidgeot-lvl-37
30 pidgeot-lvl-38
31 pidgeot-lvl-39
32 pidgeot-lvl-40])))
34 ;; inconclusive -- implies that level is calculated from
35 ;; some other values.
38 (def name-pidgeotto (read-state "name-pidgeotto"))
39 (def named-A (read-state "named-A"))
40 (def named-B (read-state "named-B"))
41 (def named-C (read-state "named-C"))
42 (def named-D (read-state "named-D"))
43 (def named-E (read-state "named-E"))
44 (def named-F (read-state "named-F"))
46 (defn name-analysis []
47 (apply common-differences
48 (map (comp vec memory)
49 [named-A
50 named-B
51 named-C
52 named-D
53 named-E
54 named-F])))
56 ;; resluted in 3 separate locations that could
57 ;; possibly hold the first letter of the pokemon's name
59 0xCF4A
60 0xD2EB
61 0xCEED
63 ;; try changing each of them
66 (defn test-cf4a []
67 (continue!
68 (set-memory named-A 0xCF4A (character->character-code "Z"))))
69 ;; result -- pidgeotto named "A"
71 (defn test-d2eb []
72 (continue!
73 (set-memory named-A 0xD2EB (character->character-code "Z"))))
74 ;; result -- pidgeotto named "Z"
76 (defn test-ceed []
77 (continue!
78 (set-memory named-A 0xCEED (character->character-code "Z"))))
79 ;; result -- pidgeotto named "A"
81 (def sixth-pokemon-name-start 0xD2EB)
84 (defn set-sixth-pokemon-name-first-character
85 ([state character]
86 (set-memory state sixth-pokemon-name-start
87 (character->character-code character)))
88 ([character]
89 (set-sixth-pokemon-name-first-character @current-state
90 character)))
93 (def end-of-name-marker 0x50)
94 (def max-name-length 10)
95 (def name-width 11)
97 (defn read-name [codes]
98 (character-codes->str
99 (take-while
100 (partial not= end-of-name-marker) codes)))
103 (defn sixth-pokemon-name [^SaveState state]
104 (read-name
105 (subvec (vec (memory state))
106 sixth-pokemon-name-start
107 (+ (inc max-name-length)
108 sixth-pokemon-name-start))))
110 (defn rename-sixth-pokemon
111 ([^SaveState state new-name]
112 (assert (< (count new-name) max-name-length))
113 (set-memory-range state sixth-pokemon-name-start
114 (concat (str->character-codes new-name)
115 [end-of-name-marker])))
116 ([new-name]
117 (rename-sixth-pokemon @current-state new-name)))
119 (defn print-text
120 ([^SaveState state begin end]
121 (dorun
122 (map (fn [character-code line]
123 (println
124 (format "0x%04X: " line)
125 (str (character-code->character character-code))))
126 (subvec (vec (memory state)) begin end)
127 (range begin end)))
128 state)
129 ([begin end]
130 (print-text @current-state begin end)))
132 (defn examine-name-memory []
133 (print-text
134 named-A
135 (- sixth-pokemon-name-start 100)
136 (+ sixth-pokemon-name-start 100)))
138 ;; results:
139 ;; 0xD287: end-of-name-sentinel
140 ;; 0xD288: R
141 ;; 0xD289: L
142 ;; 0xD28A: M
143 ;; 0xD28B: end-of-pokemon-name-sentinel
144 ;; 0xD28C: end-of-name-sentinel
145 ;; 0xD28D: end-of-name-sentinel
146 ;; 0xD28E: end-of-name-sentinel
147 ;; 0xD28F: end-of-name-sentinel
148 ;; 0xD290: end-of-name-sentinel
149 ;; 0xD291: end-of-name-sentinel
150 ;; 0xD292: end-of-name-sentinel
151 ;; 0xD293: R
152 ;; 0xD294: L
153 ;; 0xD295: M
154 ;; 0xD296: end-of-pokemon-name-sentinel
155 ;; 0xD297: end-of-name-sentinel
156 ;; 0xD298: end-of-name-sentinel
157 ;; 0xD299: end-of-name-sentinel
158 ;; 0xD29A: end-of-name-sentinel
159 ;; 0xD29B: end-of-name-sentinel
160 ;; 0xD29C: end-of-name-sentinel
161 ;; 0xD29D: end-of-name-sentinel
162 ;; 0xD29E: R
163 ;; 0xD29F: L
164 ;; 0xD2A0: M
165 ;; 0xD2A1: end-of-pokemon-name-sentinel
166 ;; 0xD2A2: end-of-name-sentinel
167 ;; 0xD2A3: end-of-name-sentinel
168 ;; 0xD2A4: end-of-name-sentinel
169 ;; 0xD2A5: end-of-name-sentinel
170 ;; 0xD2A6: end-of-name-sentinel
171 ;; 0xD2A7: end-of-name-sentinel
172 ;; 0xD2A8: end-of-name-sentinel
173 ;; 0xD2A9: R
174 ;; 0xD2AA: L
175 ;; 0xD2AB: M
176 ;; 0xD2AC: end-of-pokemon-name-sentinel
177 ;; 0xD2AD: end-of-name-sentinel
178 ;; 0xD2AE: end-of-name-sentinel
179 ;; 0xD2AF: end-of-name-sentinel
180 ;; 0xD2B0: end-of-name-sentinel
181 ;; 0xD2B1: end-of-name-sentinel
182 ;; 0xD2B2: end-of-name-sentinel
183 ;; 0xD2B3: end-of-name-sentinel
184 ;; 0xD2B4: P
185 ;; 0xD2B5: I
186 ;; 0xD2B6: D
187 ;; 0xD2B7: G
188 ;; 0xD2B8: E
189 ;; 0xD2B9: O
190 ;; 0xD2BA: T
191 ;; 0xD2BB: end-of-pokemon-name-sentinel
192 ;; 0xD2BC: end-of-pokemon-name-sentinel
193 ;; 0xD2BD: end-of-pokemon-name-sentinel
194 ;; 0xD2BE: end-of-pokemon-name-sentinel
195 ;; 0xD2BF: P
196 ;; 0xD2C0: I
197 ;; 0xD2C1: K
198 ;; 0xD2C2: A
199 ;; 0xD2C3: C
200 ;; 0xD2C4: H
201 ;; 0xD2C5: U
202 ;; 0xD2C6: end-of-pokemon-name-sentinel
203 ;; 0xD2C7: end-of-pokemon-name-sentinel
204 ;; 0xD2C8: end-of-pokemon-name-sentinel
205 ;; 0xD2C9: end-of-pokemon-name-sentinel
206 ;; 0xD2CA: C
207 ;; 0xD2CB: H
208 ;; 0xD2CC: A
209 ;; 0xD2CD: R
210 ;; 0xD2CE: I
211 ;; 0xD2CF: Z
212 ;; 0xD2D0: A
213 ;; 0xD2D1: R
214 ;; 0xD2D2: D
215 ;; 0xD2D3: end-of-pokemon-name-sentinel
216 ;; 0xD2D4: end-of-pokemon-name-sentinel
217 ;; 0xD2D5: V
218 ;; 0xD2D6: E
219 ;; 0xD2D7: N
220 ;; 0xD2D8: U
221 ;; 0xD2D9: S
222 ;; 0xD2DA: A
223 ;; 0xD2DB: U
224 ;; 0xD2DC: R
225 ;; 0xD2DD: end-of-pokemon-name-sentinel
226 ;; 0xD2DE: end-of-pokemon-name-sentinel
227 ;; 0xD2DF: end-of-pokemon-name-sentinel
228 ;; 0xD2E0: P
229 ;; 0xD2E1: R
230 ;; 0xD2E2: I
231 ;; 0xD2E3: M
232 ;; 0xD2E4: E
233 ;; 0xD2E5: A
234 ;; 0xD2E6: P
235 ;; 0xD2E7: E
236 ;; 0xD2E8: end-of-pokemon-name-sentinel
237 ;; 0xD2E9: end-of-pokemon-name-sentinel
238 ;; 0xD2EA: end-of-pokemon-name-sentinel
239 ;; 0xD2EB: A
240 ;; 0xD2EC: end-of-pokemon-name-sentinel
241 ;; 0xD2ED: S
242 ;; 0xD2EE: T
243 ;; 0xD2EF: E
244 ;; 0xD2F0: R
245 ;; 0xD2F1:
246 ;; 0xD2F2: B
247 ;; 0xD2F3: A
248 ;; 0xD2F4: L
249 ;; 0xD2F5: L
250 ;; 0xD2F6:
251 ;; 0xD2F7: A
252 ;; 0xD2F8:
253 ;; 0xD2F9:
254 ;; 0xD2FA: end-of-name-sentinel
255 ;; 0xD2FB: end-of-name-sentinel
256 ;; 0xD2FC: A
257 ;; 0xD2FD:
258 ;; 0xD2FE: end-of-name-sentinel
259 ;; 0xD2FF: end-of-name-sentinel
260 ;; 0xD300: end-of-name-sentinel
261 ;; 0xD301: end-of-name-sentinel
262 ;; 0xD302: end-of-name-sentinel
263 ;; 0xD303: end-of-name-sentinel
264 ;; 0xD304: end-of-name-sentinel
265 ;; 0xD305: end-of-name-sentinel
266 ;; 0xD306: end-of-name-sentinel
267 ;; 0xD307: end-of-name-sentinel
268 ;; 0xD308: end-of-name-sentinel
269 ;; 0xD309:
270 ;; 0xD30A: w
271 ;; 0xD30B:
272 ;; 0xD30C: V
273 ;; 0xD30D:
274 ;; 0xD30E:
275 ;; 0xD30F: K
276 ;; 0xD310:
277 ;; 0xD311:
278 ;; 0xD312:
279 ;; 0xD313: A
280 ;; 0xD314:
281 ;; 0xD315:
282 ;; 0xD316:
283 ;; 0xD317: i
284 ;; 0xD318:
285 ;; 0xD319:
286 ;; 0xD31A: end-of-name-sentinel
287 ;; 0xD31B: end-of-name-sentinel
288 ;; 0xD31C:
289 ;; 0xD31D:
290 ;; 0xD31E:
291 ;; 0xD31F:
292 ;; 0xD320:
293 ;; 0xD321:
294 ;; 0xD322:
295 ;; 0xD323:
296 ;; 0xD324:
297 ;; 0xD325:
298 ;; 0xD326:
299 ;; 0xD327:
300 ;; 0xD328:
301 ;; 0xD329:
302 ;; 0xD32A:
303 ;; 0xD32B:
304 ;; 0xD32C:
305 ;; 0xD32D:
306 ;; 0xD32E:
307 ;; 0xD32F:
308 ;; 0xD330:
309 ;; 0xD331: 9
310 ;; 0xD332: end-of-name-sentinel
311 ;; 0xD333: 9
312 ;; 0xD334:
313 ;; 0xD335: 9
314 ;; 0xD336:
315 ;; 0xD337: 9
316 ;; 0xD338: end-of-name-sentinel
317 ;; 0xD339: end-of-name-sentinel
318 ;; 0xD33A: end-of-name-sentinel
319 ;; 0xD33B: end-of-name-sentinel
320 ;; 0xD33C: end-of-name-sentinel
321 ;; 0xD33D: end-of-name-sentinel
322 ;; 0xD33E: end-of-name-sentinel
323 ;; 0xD33F: end-of-name-sentinel
324 ;; 0xD340: end-of-name-sentinel
325 ;; 0xD341: end-of-name-sentinel
326 ;; 0xD342: end-of-name-sentinel
327 ;; 0xD343: end-of-name-sentinel
328 ;; 0xD344: end-of-name-sentinel
329 ;; 0xD345: end-of-name-sentinel
330 ;; 0xD346:
331 ;; 0xD347:
332 ;; 0xD348:
333 ;; 0xD349: G
334 ;; 0xD34A: A
335 ;; 0xD34B: R
336 ;; 0xD34C: Y
337 ;; 0xD34D: end-of-pokemon-name-sentinel
338 ;; 0xD34E: J
341 ;; from this, it looks like the pokemon names are stored all
342 ;; together in one location that begins at 0xD2B4 and
343 ;; extends until 0xD2F5, with each name taking up 11 bytes.
344 ;;
345 ;; rival's name again clearly starts at 0xD349.
348 (def pokemon-names-start 0xD2B4)
351 ;; determine whether "number of pokemon in party"
352 ;; might be kept in RAM and if so, where?
354 (def six-pokemon (read-state "6-pokemon"))
355 (def five-pokemon (read-state "5-pokemon"))
356 (def four-pokemon (read-state "4-pokemon"))
357 (def three-pokemon (read-state "3-pokemon"))
358 (def two-pokemon (read-state "2-pokemon"))
359 (def one-pokemon (read-state "1-pokemon"))
362 (defn analyze-num-pokemon []
363 (apply common-differences
364 (map (comp vec memory)
365 [one-pokemon
366 two-pokemon
367 three-pokemon
368 four-pokemon
369 five-pokemon
370 six-pokemon])))
372 ;; ;; results
373 ;; ([53602 (1 2 3 4 5 6)]
374 ;; [65314 (105 61 93 60 92 34)]
375 ;; [55875 (34 36 43 52 7 0)]
376 ;; [55876 (18 0 33 52 54 30)]
377 ;; [49158 (197 194 77 117 174 134)]
378 ;; [49160 (29 26 57 239 15 243)]
379 ;; [49736 (74 93 34 89 91 59)]
380 ;; [49162 (165 162 182 179 197 109)]
381 ;; [49227 (187 105 204 5 90 238)]
382 ;; [53067 (128 136 132 145 135 11)]
383 ;; [53068 (147 131 141 136 128 7)]
384 ;; [53069 (136 134 148 140 145 2)]
385 ;; [49904 (2 11 10 3 27 12)]
386 ;; [49172 (100 109 213 195 68 104)]
387 ;; [65492 (11 103 128 160 19 56)]
388 ;; [49173 (80 77 72 75 76 67)]
389 ;; [49334 (8 10 11 5 3 1)]
390 ;; [49335 (49 10 11 19 17 15)]
391 ;; [49336 (8 10 11 5 3 1)]
392 ;; [49720 (106 14 118 0 38 11)]
393 ;; [65304 (32 88 19 114 106 33)]
394 ;; [53561 (59 229 48 17 155 103)]
395 ;; [55935 (6 5 4 3 2 1)])
398 ;; two canidates : 0xD162 or 0xDA7F
399 ;; they seem to always sum to 6...
401 ;; try to set both of them when having only one pokemon.
403 (defn change-party-number [^SaveState state new-num]
404 (set-memory state 0xD162 new-num))
406 ;; (continue! (change-party-number one-pokemon 3))
407 ;; result -- can scroll down beyone first pokemon, finding
408 ;; glitched pokemon in places where there were previously no
409 ;; pokemon.
412 (defn change-party-number* [^SaveState state new-num]
413 (set-memory state 0xDA7F new-num))
416 ;; (continue! (change-party-number* one-pokemon 3))
417 ;; cannot widthdraw any pokemon from box 1 past the third
418 ;; pokemon.
420 (def party-number-address 0xD162)
422 (defn party-number
423 ([^SaveState state]
424 (aget (memory state) party-number-address))
425 ([] (party-number @current-state)))
427 (def pokemon-in-box-1-address 0xDA7F)
429 (defn party-names
430 ([^SaveState state]
431 (let [raw-names
432 (subvec (vec (memory state))
433 pokemon-names-start
434 (+ pokemon-names-start
435 (* name-width 6)))]
436 (map
437 read-name
438 (take
439 (party-number state)
440 (partition name-width
441 raw-names)))))
442 ([] (party-names @current-state)))
445 (defn rename-pokemon
446 ([^SaveState state n new-name]
447 (assert (<= 0 n (dec (party-number state))))
448 (assert (<= (count new-name) max-name-length))
449 (set-memory-range
450 state
451 (+ (* n name-width) pokemon-names-start)
452 (concat (str->character-codes new-name) [end-of-name-marker])))
453 ([n new-name]
454 (rename-pokemon @current-state n new-name)))
456 ;; on further analysis, it appears that the original
457 ;; trainer info for each pokemon is also stored together,
458 ;; starting at 0xD272 and continuing to 0xD2B3, with
459 ;; 11 bytes reserved for each OT name.
461 (def OT-start 0xD272)
463 (defn original-trainers
464 ([^SaveState state]
465 (let [raw-names
466 (subvec (vec (memory state))
467 OT-start
468 (+ OT-start
469 (* name-width 6)))]
470 (map read-name
471 (take (party-number state)
472 (partition name-width raw-names)))))
473 ([] (original-trainers @current-state)))
475 (defn set-original-trainer
476 "Set the OT name for a pokemon.
477 Note that a pokemon is still considered 'yours' if
478 the OT ID is the same as your own."
479 ([^SaveState state n new-name]
480 (assert (<= 0 n (dec (party-number state))))
481 (assert (<= (count new-name) max-name-length))
482 (set-memory-range
483 state
484 (+ (* n name-width) OT-start)
485 (concat (str->character-codes new-name) [end-of-name-marker])))
486 ([n new-name]
487 (set-original-trainer @current-state n new-name)))
489 ;; PIKACHU stops following if you set it's OT to another name
490 ;; and then back to you own.
491 ;; But not if you set it to your own name, obviously.
496 ;; OT ID Numbers.
497 ;; My own id is 05195. A quick search of memory between
498 ;; 0xD162 and 0xD31B revealed the pattern 0x144B exactly
499 ;; six times.
501 ;; the locations were:
503 (def OT-ID-addresses [0xD176 0xD1A2 0xD1CE 0xD1FA 0xD228 0xD252])
506 (defn set-pokemon-id
507 ([^SaveState state n new-id]
508 (assert (<= 0 n (dec (party-number state))))
509 (assert (<= 0 new-id 0xFFFF))
510 (set-memory-range
511 state
512 (OT-ID-addresses n)
513 [(bit-shift-right (bit-and new-id 0xFF00) 8)
514 (bit-and new-id 0xFF)
515 ]))
516 ([n new-id]
517 (set-pokemon-id @current-state n new-id)))
523 (defn get-pokemon-1-info
524 ([state]
525 (subvec (vec (memory state))
526 0xD162 0xD197))
527 ([] (get-pokemon-1-info @current-state)))
529 (def baseline (File. "/home/r/baseline-poke"))
534 (import java.io.File)
536 (defn set-baseline [state]
537 (clojure.java.io/copy
538 (with-out-str (print-listing state 0xD162 0xD197))
539 baseline))
541 (defn compare-pokemon
542 ([state]
543 (let [target (File. "/home/r/temp-poke")]
544 (clojure.java.io/copy
545 (with-out-str (print-listing state 0xD162 0xD197))
546 target)
547 (println (:out
548 (clojure.java.shell/sh "diff"
549 (.getCanonicalPath baseline)
550 (.getCanonicalPath target))))))
551 ([] (compare-pokemon @current-state)))
554 (defn crazy-pid []
555 (read-state "crazy-pidgeot"))
557 (def unknown "[[[UNKNOWN]]]")
559 (def unknown "")
561 (def pokemon-1-info
562 {0xD16A "Color Map" ;; 0
563 0xD16B "Current-HP (h)" ;; 1
564 0xD16C "Current-HP (l)" ;; 2
565 0XD16D unknown ;; 3
566 0xD16E unknown ;; 4
567 0xD16F unknown ;; 5
568 0xD170 unknown ;; 6
569 0xD171 unknown ;; 7
570 0xD172 "Move 1 ID" ;; 8
571 0xD173 "Move 2 ID" ;; 9
572 0xD174 "Move 3 ID" ;; 10
573 0xD175 "Move 4 ID" ;; 11
574 0xD176 "OT-ID (h)" ;; 12
575 0xD177 "OT-ID (l)" ;; 13
576 0xD178 "Exp. Points (h)" ;; 14
577 0xD179 "Exp. Points (m)" ;; 15
578 0xD17A "Exp. Points (l)" ;; 16
579 0xD17B "HP Exp. (h)" ;; 17
580 0xD17C "HP Exp. (l)" ;; 18
581 0xD17D "Attack Exp. (h)" ;; 19
582 0xD17E "Attack Exp. (l)" ;; 20
583 0xD17F "Defense Exp. (h)" ;; 21
584 0xD180 "Defense Exp. (l)" ;; 22
585 0xD181 "Speed Exp. (h)" ;; 23
586 0xD182 "Speed Exp. (l)" ;; 24
587 0xD183 "Special Exp. (h)" ;; 25
588 0xD184 "Special Exp. (l)" ;; 26
589 0xD185 unknown ;; 27
590 0xD186 unknown ;; 28
591 0xD187 "PP Move 1" ;; 29
592 0xD188 "PP Move 2" ;; 30
593 0xD189 "PP Move 3" ;; 31
594 0xD18A "PP Move 4" ;; 32
595 0xD18B "Current Level" ;; 33
596 0xD18C "HP Total (h)" ;; 34
597 0xD18D "HP Total (l)" ;; 35
598 0xD18E "Attack (h)" ;; 36
599 0xD18F "Attack (l)" ;; 37
600 0xD190 "Defense (h)" ;; 38
601 0xD191 "Defense (l)" ;; 39
602 0xD192 "Speed (h)" ;; 40
603 0xD193 "Speed (l)" ;; 41
604 0xD194 "Special (h)" ;; 42
605 0xD195 "Special (l)" ;; 43
606 })
608 (defn pokemon-data
609 ([^SaveState state pokemon-num]
610 (assert (<= 0 pokemon-num 5))
611 (let [base (+ (* pokemon-num pokemon-record-width) 0xD16A)]
612 (subvec (vec (memory state)) base
613 (+ base pokemon-record-width))))
614 ([pokemon-num] (pokemon-data @current-state pokemon-num)))
616 (defn set-pokemon-data
617 ([^SaveState state pokemon-num new-data]
618 (assert (<= 0 pokemon-num 5))
619 (let [base (+ (* pokemon-num pokemon-record-width) 0xD16A)]
620 (set-memory-range state base new-data)))
621 ([pokemon-num new-data]
622 (set-pokemon-data @current-state pokemon-num new-data)))
624 (defn print-pokemon-data
625 ([^SaveState state pokemon-num]
626 (assert (<= 0 pokemon-num 5))
627 (let [poke-data (pokemon-data state pokemon-num)
628 backbone (sort (keys pokemon-1-info))]
629 (println "Pokemon " pokemon-num " -- "
630 (nth (party-names state)
631 pokemon-num) \newline)
633 (println " Desc. | Hex | Dec | Binary |")
634 (println "-------------------+------+-----+----------+")
635 (dorun
636 (map
637 (comp println
638 (fn [desc data]
639 (format "%-16s | 0x%02X | %3d | %s |"
640 desc data data
641 (let [s (Integer/toBinaryString data)]
642 (apply
643 str
644 (concat (repeat (- 8 (count s)) "0" )
645 s)))
648 )))
650 (map pokemon-1-info backbone)
651 poke-data))))
652 ([pokemon-num]
653 (print-pokemon-data @current-state pokemon-num)))
657 (defn pre-battle [] (read-state "prepare-for-battle"))
660 (defn pika-lvl-100-DV-0 []
661 (read-state "at-pc-lv-100-pikachu"))
663 ;; Performed following experiment:
664 ;; Raised Pikachu to Lvl 100 with rare-candies,
665 ;; then put it at the head of the party and
666 ;; set 0xD185 and 0xD186 to zero.
668 ;; then, for each pattern, deposited pikachu into
669 ;; Box 1 and immediately widthdrew it and observed the
670 ;; stats.
673 ;; Pikachu L:100 Base Stats with DVs = 0
674 ;; HP : 187
675 ;; Attack : 123
676 ;; Defense : 73
677 ;; Speed : 194
678 ;; Special : 112
680 ;; 0xD185:
682 ;; 00000001
683 ;; Defense => 75 (+2)
684 ;; HP => 195 (+8)
686 ;; 00000010
687 ;; Defense => 77 (+4)
689 ;; 00000100
690 ;; Defense => 81 (+8)
692 ;; 00001000
693 ;; Defense => 89 (+16)
695 ;; 00010000
696 ;; HP => 203 (+16)
697 ;; Attack => 125 (+2)
699 ;; 00100000
700 ;; Attack => 127 (+4)
702 ;; 01000000
703 ;; Attack -> 131 (+8)
705 ;; 10000000
706 ;; Attack -> 139 (+16)
708 ;; 0xD186
710 ;; 00000001
711 ;; HP => 189 (+2)
712 ;; Special => (+2)
714 ;; 00000010
715 ;; Special => 116 (+4)
717 ;; 00000100
718 ;; Special => 120 (+8)
720 ;; 00001000
721 ;; Special => 128 (+16)
723 ;; 00010000
724 ;; HP => 191 (+4)
725 ;; Speed => 196 (+2)
727 ;; 00100000
728 ;; Speed => 198 (+4)
730 ;; 01000000
731 ;; Speed => 202 (+8)
733 ;; 10000000
734 ;; Speed => 210 (+16)
736 (def pokemon-1-DV-start 0xD185)
738 (defn pokemon-DV-start-point [poke-num]
739 (+ (* poke-num pokemon-record-width)
740 pokemon-1-DV-start))
742 (def reverse-4-bit
743 {0 0
744 1 8
745 2 4
746 3 12
747 5 10
748 6 6
749 7 14
750 9 9
751 15 15
752 13 11
754 8 1
755 4 2
756 12 3
757 10 5
758 11 13
759 14 7 })
761 (defn read-DV
762 ([^SaveState state poke-num]
763 (assert (<= 0 poke-num 5))
764 (let [[raw-DV-1
765 raw-DV-2]
766 (subvec (vec (memory state))
767 (pokemon-DV-start-point poke-num)
768 (+ 2 (pokemon-DV-start-point poke-num)))
769 defense-DV (bit-and raw-DV-1 0x0F)
770 attack-DV (bit-shift-right
771 (bit-and raw-DV-1 0xF0)
772 4)
773 special-DV (bit-and raw-DV-2 0x0F)
774 speed-DV (bit-shift-right
775 (bit-and raw-DV-2 0xF0)
776 4)
777 HP-DV
778 (+
779 (if (bit-test special-DV 0) 1 0)
780 (if (bit-test speed-DV 0) 2 0)
781 (if (bit-test defense-DV 0) 4 0)
782 (if (bit-test attack-DV 0) 8 0))]
783 {:attack attack-DV
784 :defense defense-DV
785 :speed speed-DV
786 :special special-DV
787 :hp HP-DV}))
788 ([poke-num]
789 (read-DV @current-state poke-num)))
792 (defn give-DV
793 ([^SaveState state poke-num dv-values]
795 (assert (<= 0 poke-num 5))
796 (map #(assert (<= 0 % 15)) (vals dv-values))
797 (let [raw-dv-1*
798 (+ (:defense dv-values)
799 (bit-shift-left (:attack dv-values) 4))
800 raw-dv-2*
801 (+ (:special dv-values)
802 (bit-shift-left (:speed dv-values) 4))
803 hp-dv (:hp dv-values)
804 hp-masks-1
805 [[0 (bit-test hp-dv 2)]
806 [4 (bit-test hp-dv 3)]]
807 hp-masks-2
808 [[0 (bit-test hp-dv 0)]
809 [4 (bit-test hp-dv 1)]]
810 set-hp-bits
811 (fn [init [index hp?]]
812 (if hp?
813 (bit-set init index)
814 (bit-clear init index)))
816 raw-dv-1 (reduce set-hp-bits raw-dv-1*
817 hp-masks-1)
819 raw-dv-2 (reduce set-hp-bits raw-dv-2*
820 hp-masks-2)
822 dv-start (pokemon-DV-start-point poke-num)]
824 (if (or (not= raw-dv-1* raw-dv-1)
825 (not= raw-dv-2* raw-dv-2))
826 (println "Warning: inconsistent DV-values."
827 "Using HP settings."))
829 (set-memory
830 (set-memory state dv-start raw-dv-1)
831 (inc dv-start) raw-dv-2)))
832 ([poke-num dv-values]
833 (give-DV @current-state poke-num dv-values)))
835 (def good-DVs
836 {:attack 15
837 :defense 15
838 :speed 15
839 :special 15
840 :hp 15})
842 (def bad-DVs
843 {:attack 0
844 :defense 0
845 :speed 0
846 :special 0
847 :hp 0})