comparison 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
comparison
equal deleted inserted replaced
168:04dfda91db9c 169:4857f59f63a6
655 655
656 656
657 (defn pre-battle [] (read-state "prepare-for-battle")) 657 (defn pre-battle [] (read-state "prepare-for-battle"))
658 658
659 659
660 660 (defn pika-lvl-100-DV-0 []
661 661 (read-state "at-pc-lv-100-pikachu"))
662 662
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.
667
668 ;; then, for each pattern, deposited pikachu into
669 ;; Box 1 and immediately widthdrew it and observed the
670 ;; stats.
671
672
673 ;; Pikachu L:100 Base Stats with DVs = 0
674 ;; HP : 187
675 ;; Attack : 123
676 ;; Defense : 73
677 ;; Speed : 194
678 ;; Special : 112
679
680 ;; 0xD185:
681
682 ;; 00000001
683 ;; Defense => 75 (+2)
684 ;; HP => 195 (+8)
685
686 ;; 00000010
687 ;; Defense => 77 (+4)
688
689 ;; 00000100
690 ;; Defense => 81 (+8)
691
692 ;; 00001000
693 ;; Defense => 89 (+16)
694
695 ;; 00010000
696 ;; HP => 203 (+16)
697 ;; Attack => 125 (+2)
698
699 ;; 00100000
700 ;; Attack => 127 (+4)
701
702 ;; 01000000
703 ;; Attack -> 131 (+8)
704
705 ;; 10000000
706 ;; Attack -> 139 (+16)
707
708 ;; 0xD186
709
710 ;; 00000001
711 ;; HP => 189 (+2)
712 ;; Special => (+2)
713
714 ;; 00000010
715 ;; Special => 116 (+4)
716
717 ;; 00000100
718 ;; Special => 120 (+8)
719
720 ;; 00001000
721 ;; Special => 128 (+16)
722
723 ;; 00010000
724 ;; HP => 191 (+4)
725 ;; Speed => 196 (+2)
726
727 ;; 00100000
728 ;; Speed => 198 (+4)
729
730 ;; 01000000
731 ;; Speed => 202 (+8)
732
733 ;; 10000000
734 ;; Speed => 210 (+16)
735
736 (def pokemon-1-DV-start 0xD185)
737
738 (defn pokemon-DV-start-point [poke-num]
739 (+ (* poke-num pokemon-record-width)
740 pokemon-1-DV-start))
741
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
753
754 8 1
755 4 2
756 12 3
757 10 5
758 11 13
759 14 7 })
760
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)))
790
791
792 (defn give-DV
793 ([^SaveState state poke-num dv-values]
794
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)))
815
816 raw-dv-1 (reduce set-hp-bits raw-dv-1*
817 hp-masks-1)
818
819 raw-dv-2 (reduce set-hp-bits raw-dv-2*
820 hp-masks-2)
821
822 dv-start (pokemon-DV-start-point poke-num)]
823
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."))
828
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)))
834
835 (def good-DVs
836 {:attack 15
837 :defense 15
838 :speed 15
839 :special 15
840 :hp 15})
841
842 (def bad-DVs
843 {:attack 0
844 :defense 0
845 :speed 0
846 :special 0
847 :hp 0})
848
849