# HG changeset patch # User Robert McIntyre # Date 1332473757 18000 # Node ID fd549c8f42aee934c5de107c7028a144395bc390 # Parent 893c753f808840b7d7541653a144c1ec6e1cbf2f fixed compilation problems, added more functionality to pokemon-info diff -r 893c753f8088 -r fd549c8f42ae clojure/com/aurellem/gb/experience.clj --- a/clojure/com/aurellem/gb/experience.clj Thu Mar 22 20:10:09 2012 -0500 +++ b/clojure/com/aurellem/gb/experience.clj Thu Mar 22 22:35:57 2012 -0500 @@ -27,39 +27,27 @@ special-exp-h special-exp-l] (subvec (vec (memory state)) - start (+ experience-record-width start)) - glue-bytes (fn [l h] - (+ l (bit-shift-left h 8)))] - {:main-exp (+ (glue-bytes exp-l exp-m) - (bit-shift-left exp-h 16)) - :hp-exp (glue-bytes hp-exp-l hp-exp-h) - :attack-exp (glue-bytes attack-exp-l attack-exp-h) - :defense-exp (glue-bytes defense-exp-l defense-exp-h) - :speed-exp (glue-bytes speed-exp-l speed-exp-h) - :special-exp (glue-bytes special-exp-l special-exp-h)})) - ([poke-num] + start (+ experience-record-width start))] + {:main-exp (glue-bytes exp-h exp-m exp-l) + :hp-exp (glue-bytes hp-exp-h hp-exp-l) + :attack-exp (glue-bytes attack-exp-h attack-exp-l) + :defense-exp (glue-bytes defense-exp-h defense-exp-l) + :speed-exp (glue-bytes speed-exp-h speed-exp-l) + :special-exp (glue-bytes special-exp-h special-exp-l)})) + ([poke-num] (read-experience @current-state poke-num))) (defn give-experience ([^SaveState state poke-num exp] - (let [exp* (merge (read-experience state poke-num) - exp) - - disect-bytes - (fn [exp] - [(bit-shift-right - (bit-and exp 0xFF00) 8) - (bit-and exp 0xFF)]) - + (let [exp* (merge (read-experience state poke-num) exp) raw-exp-data (flatten - [(bit-shift-right (bit-and (:main-exp exp*) 0xFF0000) 16) - (disect-bytes (:main-exp exp*)) - (disect-bytes (:hp-exp exp*)) - (disect-bytes (:attack-exp exp*)) - (disect-bytes (:defense-exp exp*)) - (disect-bytes (:speed-exp exp*)) - (disect-bytes (:special-exp exp*))])] + [(disect-bytes-3 (:main-exp exp*)) + (disect-bytes-2 (:hp-exp exp*)) + (disect-bytes-2 (:attack-exp exp*)) + (disect-bytes-2 (:defense-exp exp*)) + (disect-bytes-2 (:speed-exp exp*)) + (disect-bytes-2 (:special-exp exp*))])] (set-memory-range state (experience-start-address poke-num) raw-exp-data))) diff -r 893c753f8088 -r fd549c8f42ae clojure/com/aurellem/gb/moves.clj --- a/clojure/com/aurellem/gb/moves.clj Thu Mar 22 20:10:09 2012 -0500 +++ b/clojure/com/aurellem/gb/moves.clj Thu Mar 22 22:35:57 2012 -0500 @@ -191,7 +191,7 @@ (map move-code->move-name (subvec (vec (memory state)) - start (+ start (num-moves state poke-num)))))))) + start (+ start 4))))))) ([poke-num] (read-moves @current-state poke-num))) @@ -240,7 +240,7 @@ (bit-and pp-raw (Integer/parseInt "00111111" 2))] - [pp-up current-pp])) + {:pp-ups pp-up :current-pp current-pp})) ([pokemon-num move-num] (read-pp @current-state pokemon-num move-num))) diff -r 893c753f8088 -r fd549c8f42ae clojure/com/aurellem/gb/pokemon.clj --- a/clojure/com/aurellem/gb/pokemon.clj Thu Mar 22 20:10:09 2012 -0500 +++ b/clojure/com/aurellem/gb/pokemon.clj Thu Mar 22 22:35:57 2012 -0500 @@ -1,7 +1,7 @@ (ns com.aurellem.gb.pokemon (:use (com.aurellem.gb gb-driver util constants characters moves types items status dv species - experience + experience stats )) (:import [com.aurellem.gb.gb_driver SaveState])) @@ -66,7 +66,12 @@ (partition name-width raw-names))))) ([] (original-trainers @current-state))) -(defn set-original-trainer +(defn read-OT-name + ([^SaveState state poke-num] + (nth (original-trainers state) poke-num)) + ([poke-num] (read-OT @current-state poke-num))) + +(defn set-OT-name "Set the OT name for a pokemon. Note that a pokemon is still considered 'yours' if the OT ID is the same as your own." @@ -82,18 +87,25 @@ (def OT-ID-addresses [0xD176 0xD1A2 0xD1CE 0xD1FA 0xD228 0xD252]) -(defn set-pokemon-id - ([^SaveState state n new-id] - (assert (<= 0 n (dec (party-number state)))) - (assert (<= 0 new-id 0xFFFF)) +(defn read-OT-id + ([^SaveState state poke-num] + (let [mem (memory state) + start (OT-ID-addresses poke-num)] + (glue-bytes + (aget mem start) + (aget mem (inc start))))) + ([poke-num] (read-OT-id @current-state poke-num))) + +(defn set-OT-id + ([^SaveState state poke-num new-OT-num] + (assert (<= 0 poke-num 5)) + (assert (<= 0 new-OT-num 0xFFFF)) (set-memory-range state - (OT-ID-addresses n) - [(bit-shift-right (bit-and new-id 0xFF00) 8) - (bit-and new-id 0xFF) - ])) - ([n new-id] - (set-pokemon-id @current-state n new-id))) + (OT-ID-addresses poke-num) + (disect-bytes-2 new-OT-num))) + ([poke-num new-OT-num] + (set-pokemon-id @current-state poke-num new-OT-num))) (def unknown "[[[UNKNOWN]]]") @@ -204,7 +216,10 @@ (range (count moves))) nickname (pokemon-nickname state poke-num) status (read-status state poke-num) + stats (read-stats state poke-num) experience (read-experience state poke-num) + OT-name (read-OT-name state poke-num) + ID (read-OT-id state poke-num) ] {:name nickname @@ -212,12 +227,26 @@ :species2 species2 :type type :dv dv-values + :original-trainer OT-name + :ID ID + :moves (zipmap moves moves-pp) :satus status + :stats stats :experience experience + } )) ([poke-num] (pokemon-info @current-state poke-num))) + +(defn give-status-all + ([^SaveState state status] + (reduce (fn [state num] + (give-status state num status)) + state + (range (party-number state)))) + ([status] + (give-status-all @current-state status))) diff -r 893c753f8088 -r fd549c8f42ae clojure/com/aurellem/gb/stats.clj --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/clojure/com/aurellem/gb/stats.clj Thu Mar 22 22:35:57 2012 -0500 @@ -0,0 +1,53 @@ +(ns com.aurellem.gb.stats + (:use (com.aurellem.gb gb-driver util constants)) + (:import [com.aurellem.gb.gb_driver SaveState])) + +(def pokemon-1-stats-start-address 0xD18B) + +(defn pokemon-stats-address [poke-num] + (+ pokemon-1-stats-start-address + (* poke-num pokemon-record-width))) + +(def stats-record-size 11) + +(defn read-stats + ([^SaveState state poke-num] + (let [start (pokemon-stats-address poke-num) + [level + hp-h + hp-l + attack-h + attack-l + defense-h + defense-l + speed-h + speed-l + special-h + special-l] + (subvec (vec (memory state)) + start (+ start stats-record-size ))] + {:level level + :hp (glue-bytes hp-h hp-l) + :attack (glue-bytes attack-h attack-l) + :defense (glue-bytes defense-h defense-l) + :speed (glue-bytes speed-h speed-l) + :special (glue-bytes special-h special-l)})) + ([poke-num] + (read-stats @current-state poke-num))) + +(defn give-stats + ([^SaveState state poke-num new-stats] + (let [new-stats* (merge (read-stats state poke-num) + new-stats) + raw-stats + (flatten + [(:level new-stats*) + (disect-bytes-2 (:hp new-stats*)) + (disect-bytes-2 (:attack new-stats*)) + (disect-bytes-2 (:defense new-stats*)) + (disect-bytes-2 (:speed new-stats*)) + (disect-bytes-2 (:special new-stats*))])] + (set-memory-range state (pokemon-stats-address poke-num) + raw-stats))) + ([poke-num new-stats] + (give-stats @current-state poke-num new-stats))) diff -r 893c753f8088 -r fd549c8f42ae clojure/com/aurellem/gb/status.clj --- a/clojure/com/aurellem/gb/status.clj Thu Mar 22 20:10:09 2012 -0500 +++ b/clojure/com/aurellem/gb/status.clj Thu Mar 22 22:35:57 2012 -0500 @@ -1,5 +1,5 @@ (ns com.aurellem.gb.status - (:use (com.aurellem.gb gb-driver util constants pokemon)) + (:use (com.aurellem.gb gb-driver util constants)) (:import [com.aurellem.gb.gb_driver SaveState])) (def status-name->status-code @@ -44,15 +44,7 @@ (pokemon-status-address poke-num) status-code))) ([poke-num status] - (give-stat @current-state poke-num status)) + (give-status @current-state poke-num status)) ([status] - (give-stat @current-state 0 status))) + (give-status @current-state 0 status))) -(defn give-status-all - ([^SaveState state status] - (reduce (fn [state num] - (give-stat state num status)) - state - (range (party-number state)))) - ([status] - (give-stat-all @current-state status))) diff -r 893c753f8088 -r fd549c8f42ae clojure/com/aurellem/gb/util.clj --- a/clojure/com/aurellem/gb/util.clj Thu Mar 22 20:10:09 2012 -0500 +++ b/clojure/com/aurellem/gb/util.clj Thu Mar 22 22:35:57 2012 -0500 @@ -110,4 +110,34 @@ (read-state "mid-game")) - \ No newline at end of file + +(defn disect-bytes-2 + "return a vector consiting of the last 16 bytes of the + integer expressed as two 8 bit nimbers (inside an integer) + in the form [high-bits low-bits." + [num] + [(bit-shift-right + (bit-and num 0xFF00) 8) + (bit-and num 0xFF)]) + +(defn disect-bytes-3 + "same as disect-bytes-2 except that it assumes the input is a + 24 bit number and returns [high-bits medium-bits low-bits]" + [num] + (vec + (concat + [(bit-shift-right (bit-and num 0xFF0000) 16)] + (disect-bytes-2 num)))) + +(defn glue-bytes + "Given two or three 8-bit numbers inside 32-bit integers, + combine them into the integer number that they together + represent." + ([h l] + (+ l (bit-shift-left h 8))) + + ([h m l] + (+ (glue-bytes m l) + (bit-shift-left h 16)))) + +