diff clojure/com/aurellem/gb/items.clj @ 145:412ca096a9ba

major refactoring complete.
author Robert McIntyre <rlm@mit.edu>
date Mon, 19 Mar 2012 21:23:46 -0500
parents
children cd7979aa544e
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/clojure/com/aurellem/gb/items.clj	Mon Mar 19 21:23:46 2012 -0500
     1.3 @@ -0,0 +1,226 @@
     1.4 +(ns com.aurellem.gb.items
     1.5 +  (:use (com.aurellem.gb gb-driver util))
     1.6 +  ;; this is bullshit
     1.7 +  (:import [com.aurellem.gb.gb_driver SaveState]))
     1.8 +
     1.9 +(defn game-name []
    1.10 +  (map char (subvec (vec (memory)) 0x134 0x142)))
    1.11 +
    1.12 +(def item-list-start 0xD31C)
    1.13 +
    1.14 +(defn item-list [^SaveState state]
    1.15 +  (subvec
    1.16 +   (vec (memory state))
    1.17 +   item-list-start
    1.18 +   (+ item-list-start 150)))
    1.19 +
    1.20 +(def item-code->item-name
    1.21 +  (hash-map
    1.22 +   0x01 :master-ball       
    1.23 +   0x02 :ultra-ball
    1.24 +   0x03 :great-ball
    1.25 +   0x04 :poke-ball
    1.26 +   0x05 :town-map
    1.27 +   0x06 :bicycle
    1.28 +   0x08 :safari-ball
    1.29 +   0x09 :pokedex
    1.30 +   0x0A :moon-stone
    1.31 +   0x0B :antidote
    1.32 +   0x0C :burn-heal
    1.33 +   0x0D :ice-heal
    1.34 +   0x0E :awakening
    1.35 +   0x0F :parlyz-heal
    1.36 +   0x10 :full-restore
    1.37 +   0x11 :max-potion
    1.38 +   0x12 :hyper-potion
    1.39 +   0x13 :super-potion
    1.40 +   0x14 :potion
    1.41 +   0x15 :boulderbadge
    1.42 +   0x16 :cascadebadge
    1.43 +   0x17 :thunderbadge
    1.44 +   0x18 :rainbowbadge
    1.45 +   0x19 :soulbadge
    1.46 +   0x1A :marshbadge
    1.47 +   0x1B :volcanobadge
    1.48 +   0x1C :earthbadge
    1.49 +   0x1D :escape-rope
    1.50 +   0x1E :repel
    1.51 +   0x1F :old-amber
    1.52 +   0x20 :fire-stone
    1.53 +   0x21 :thunderstone
    1.54 +   0x22 :water-stone
    1.55 +   0x23 :hp-up
    1.56 +   0x24 :protein
    1.57 +   0x25 :iron
    1.58 +   0x26 :carbos
    1.59 +   0x27 :calcium
    1.60 +   0x28 :rare-candy
    1.61 +   0x29 :dome-fossil
    1.62 +   0x2A :helix-fossil
    1.63 +   0x2B :secret-key
    1.64 +   0x2D :bike-voucher
    1.65 +   0x2E :x-accuracy
    1.66 +   0x2F :leaf-stone
    1.67 +   0x30 :card-key
    1.68 +   0x31 :nugget
    1.69 +   0x32 :pp-up
    1.70 +   0x33 :poke-doll
    1.71 +   0x34 :full-heal
    1.72 +   0x35 :revive
    1.73 +   0x36 :max-revive
    1.74 +   0x37 :guard-spec
    1.75 +   0x38 :super-repel
    1.76 +   0x39 :max-repel
    1.77 +   0x3A :dire-hit
    1.78 +   0x3B :coin
    1.79 +   0x3C :fresh-water
    1.80 +   0x3D :soda-pop
    1.81 +   0x3E :lemonade
    1.82 +   0x3F :s.s.ticket
    1.83 +   0x40 :gold-teeth
    1.84 +   0x41 :x-attach
    1.85 +   0x42 :x-defend
    1.86 +   0x43 :x-speed
    1.87 +   0x44 :x-special
    1.88 +   0x45 :coin-case
    1.89 +   0x46 :oaks-parcel
    1.90 +   0x47 :itemfinder
    1.91 +   0x48 :silph-scope
    1.92 +   0x49 :poke-flute
    1.93 +   0x4A :lift-key
    1.94 +   0x4B :exp.all
    1.95 +   0x4C :old-rod
    1.96 +   0x4D :good-rod
    1.97 +   0x4E :super-rod
    1.98 +   0x4F :pp-up
    1.99 +   0x50 :ether
   1.100 +   0x51 :max-ether
   1.101 +   0x52 :elixer
   1.102 +   0x53 :max-elixer
   1.103 +   0xC4 :HM01     ;; cut		   
   1.104 +   0xC5 :HM02     ;; fly		   
   1.105 +   0xC6 :HM03     ;; surf		   
   1.106 +   0xC7 :HM04     ;; strength	   
   1.107 +   0xC8 :HM05     ;; flash	   
   1.108 +   0xC9 :TM01     ;; mega punch	   
   1.109 +   0xCA :TM02     ;; razor wind	   
   1.110 +   0xCB :TM03     ;; swords dance	   
   1.111 +   0xCC :TM04     ;; whirlwind	   
   1.112 +   0xCD :TM05     ;; mega kick	   
   1.113 +   0xCE :TM06     ;; toxic	   
   1.114 +   0xCF :TM07     ;; horn drill	   
   1.115 +   0xD0 :TM08     ;; body slam	   
   1.116 +   0xD1 :TM09     ;; take down	   
   1.117 +   0xD2 :TM10     ;; double-edge	   
   1.118 +   0xD3 :TM11     ;; bubblebeam	   
   1.119 +   0xD4 :TM12     ;; water gun	   
   1.120 +   0xD5 :TM13     ;; ice beam 	   
   1.121 +   0xD6 :TM14     ;; blizzard	   
   1.122 +   0xD7 :TM15     ;; hyper beam	   
   1.123 +   0xD8 :TM16     ;; pay day	   
   1.124 +   0xD9 :TM17     ;; submission	   
   1.125 +   0xDA :TM18     ;; counter	   
   1.126 +   0xDB :TM19     ;; seismic toss	   
   1.127 +   0xDC :TM20     ;; rage		   
   1.128 +   0xDD :TM21     ;; mega drain	   
   1.129 +   0xDE :TM22     ;; solarbeam	   
   1.130 +   0xDF :TM23     ;; dragon rage	   
   1.131 +   0xE0 :TM24     ;; thunderbolt	   
   1.132 +   0xE1 :TM25     ;; thunder	   
   1.133 +   0xE2 :TM26     ;; earthquake	   
   1.134 +   0xE3 :TM27     ;; fissure	   
   1.135 +   0xE4 :TM28     ;; dig		   
   1.136 +   0xE5 :TM29     ;; psychic 	   
   1.137 +   0xE6 :TM30     ;; teleport	   
   1.138 +   0xE7 :TM31     ;; mimic	   
   1.139 +   0xE8 :TM32     ;; double team	   
   1.140 +   0xE9 :TM33     ;; reflect	   
   1.141 +   0xEA :TM34     ;; bide		   
   1.142 +   0xEB :TM35     ;; metronome	   
   1.143 +   0xEC :TM36     ;; self destruct   
   1.144 +   0xED :TM37     ;; eggbomb	   
   1.145 +   0xEE :TM38     ;; fire blast	   
   1.146 +   0xEF :TM39     ;; swift	   
   1.147 +   0xF0 :TM40     ;; skull bash	   
   1.148 +   0xF1 :TM41     ;; softboiled	   
   1.149 +   0xF2 :TM42     ;; dream eater	   
   1.150 +   0xF3 :TM43     ;; sky attack	   
   1.151 +   0xF4 :TM44     ;; rest		   
   1.152 +   0xF5 :TM45     ;; thunder wave	   
   1.153 +   0xF6 :TM46     ;; psywave	   
   1.154 +   0xF7 :TM47     ;; explosion	   
   1.155 +   0xF8 :TM48     ;; rock slide	   
   1.156 +   0xF9 :TM49     ;; tri attack	   
   1.157 +   0xFA :TM50     ;; substitute	   
   1.158 +   0xFB :TM51     ;; "cut"	   
   1.159 +   0xFC :TM52     ;; "fly"	   
   1.160 +   0xFD :TM53     ;; "surf"	   
   1.161 +   0xFE :TM54     ;; "strength"      
   1.162 +   0xFF :end-of-list-sentinel))
   1.163 +
   1.164 +(def item-name->item-code
   1.165 +  (zipmap (vals item-code->item-name)
   1.166 +          (keys item-code->item-name)))
   1.167 +
   1.168 +(defn inventory [^SaveState state]
   1.169 +  (let [items (item-list state)]
   1.170 +    (map
   1.171 +     (fn [[item-code quantity]]
   1.172 +       [(item-code->item-name
   1.173 +         item-code
   1.174 +         (str ":0x" (.toUpperCase (Integer/toHexString item-code))))
   1.175 +           quantity])
   1.176 +     (partition
   1.177 +      2
   1.178 +      (next (take-while (partial not= 255) items))))))
   1.179 +
   1.180 +(defn print-inventory
   1.181 +  ([] (print-inventory @current-state))
   1.182 +  ([^SaveState state]
   1.183 +     (println
   1.184 +      (let [inv (inventory state)]
   1.185 +        (reduce
   1.186 +         str
   1.187 +         (concat
   1.188 +          ["+-------------------+----------+\n"
   1.189 +           "|##| Item           | Quantity |\n"
   1.190 +           "+--+----------------+----------+\n"]
   1.191 +
   1.192 +          (map
   1.193 +           (fn [index [item-name quantity]]
   1.194 +             (str 
   1.195 +              (format "|%-2d| %-14s | %3d      |\n" index
   1.196 +                      (apply str (rest (str item-name)))
   1.197 +                      quantity)))
   1.198 +           (range 0 (count inv)) inv)
   1.199 +          ["+--+----------------+----------+\n"]))))
   1.200 +     state))
   1.201 +
   1.202 +(defn inventory-codes [inventory]
   1.203 +  (flatten
   1.204 +   (concat [(count inventory)]
   1.205 +           (map (fn [[item-name quantity]]
   1.206 +                  [(item-name->item-code item-name)
   1.207 +                   quantity]) inventory)
   1.208 +           [(item-name->item-code :end-of-list-sentinel)])))
   1.209 +
   1.210 +(defn set-inv-mem [^SaveState state inv-codes]
   1.211 +  (set-memory-range state item-list-start
   1.212 +                    inv-codes))
   1.213 +  
   1.214 +
   1.215 +(defn set-inventory [^SaveState state new-inventory]
   1.216 +  (set-inv-mem state (inventory-codes new-inventory)))
   1.217 +
   1.218 +(defn give
   1.219 +  ([^SaveState state items]
   1.220 +     (set-inventory state
   1.221 +                    (concat items (inventory state))))
   1.222 +  ([items]
   1.223 +     (give @current-state items)))
   1.224 +
   1.225 +(defn clear-inventory
   1.226 +  ([^SaveState state]
   1.227 +     (set-inventory state []))
   1.228 +  ([] (clear-inventory @current-state)))
   1.229 +