Mercurial > vba-clojure
view clojure/com/aurellem/items.clj @ 105:2f8089eacab9
working on some actual assembly programs
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Tue, 13 Mar 2012 14:40:01 -0500 |
parents | 3305490a8f62 |
children | 3a60bb14a64a |
line wrap: on
line source
1 (ns com.aurellem.items2 (:use (com.aurellem gb-driver vbm title))3 ;; this is bullshit4 (:import [com.aurellem.gb_driver SaveState]))6 (defn game-name []7 (map char (subvec (vec (memory)) 0x134 0x142)))9 (def item-list-start 0xD31C)11 (defn item-list [^SaveState state]12 (subvec13 (vec (memory state))14 item-list-start15 (+ item-list-start 150)))17 (def item-hack-3 (read-state 77557))19 (def item-code->item-name20 (hash-map21 0x01 :master-ball22 0x02 :ultra-ball23 0x03 :great-ball24 0x04 :poke-ball25 0x05 :town-map26 0x06 :bicycle27 0x08 :safari-ball28 0x09 :pokedex29 0x0A :moon-stone30 0x0B :antidote31 0x0C :burn-heal32 0x0D :ice-heal33 0x0E :awakening34 0x0F :parlyz-heal35 0x10 :full-restore36 0x11 :max-potion37 0x12 :hyper-potion38 0x13 :super-potion39 0x14 :potion40 0x15 :boulderbadge41 0x16 :cascadebadge42 0x17 :thunderbadge43 0x18 :rainbowbadge44 0x19 :soulbadge45 0x1A :marshbadge46 0x1B :volcanobadge47 0x1C :earthbadge48 0x1D :escape-rope49 0x1E :repel50 0x1F :old-amber51 0x20 :fire-stone52 0x21 :thunderstone53 0x22 :water-stone54 0x23 :hp-up55 0x24 :protein56 0x25 :iron57 0x26 :carbos58 0x27 :calcium59 0x28 :rare-candy60 0x29 :dome-fossil61 0x2A :helix-fossil62 0x2B :secret-key63 0x2D :bike-voucher64 0x2E :x-accuracy65 0x2F :leaf-stone66 0x30 :card-key67 0x31 :nugget68 0x32 :pp-up69 0x33 :poke-doll70 0x34 :full-heal71 0x35 :revive72 0x36 :max-revive73 0x37 :guard-spec74 0x38 :super-repel75 0x39 :max-repel76 0x3A :dire-hit77 0x3B :coin78 0x3C :fresh-water79 0x3D :soda-pop80 0x3E :lemonade81 0x3F :s.s.ticket82 0x40 :gold-teeth83 0x41 :x-attach84 0x42 :x-defend85 0x43 :x-speed86 0x44 :x-special87 0x45 :coin-case88 0x46 :oaks-parcel89 0x47 :itemfinder90 0x48 :silph-scope91 0x49 :poke-flute92 0x4A :lift-key93 0x4B :exp.all94 0x4C :old-rod95 0x4D :good-rod96 0x4E :super-rod97 0x4F :pp-up98 0x50 :ether99 0x51 :max-ether100 0x52 :elixer101 0x53 :max-elixer102 0xC4 :HM01 ;; cut103 0xC5 :HM02 ;; fly104 0xC6 :HM03 ;; surf105 0xC7 :HM04 ;; strength106 0xC8 :HM05 ;; flash107 0xC9 :TM01 ;; mega punch108 0xCA :TM02 ;; razor wind109 0xCB :TM03 ;; swords dance110 0xCC :TM04 ;; whirlwind111 0xCD :TM05 ;; mega kick112 0xCE :TM06 ;; toxic113 0xCF :TM07 ;; horn drill114 0xD0 :TM08 ;; body slam115 0xD1 :TM09 ;; take down116 0xD2 :TM10 ;; double-edge117 0xD3 :TM11 ;; bubblebeam118 0xD4 :TM12 ;; water gun119 0xD5 :TM13 ;; ice beam120 0xD6 :TM14 ;; blizzard121 0xD7 :TM15 ;; hyper beam122 0xD8 :TM16 ;; pay day123 0xD9 :TM17 ;; submission124 0xDA :TM18 ;; counter125 0xDB :TM19 ;; seismic toss126 0xDC :TM20 ;; rage127 0xDD :TM21 ;; mega drain128 0xDE :TM22 ;; solarbeam129 0xDF :TM23 ;; dragon rage130 0xE0 :TM24 ;; thunderbolt131 0xE1 :TM25 ;; thunder132 0xE2 :TM26 ;; earthquake133 0xE3 :TM27 ;; fissure134 0xE4 :TM28 ;; dig135 0xE5 :TM29 ;; psychic136 0xE6 :TM30 ;; teleport137 0xE7 :TM31 ;; mimic138 0xE8 :TM32 ;; double team139 0xE9 :TM33 ;; reflect140 0xEA :TM34 ;; bide141 0xEB :TM35 ;; metronome142 0xEC :TM36 ;; self destruct143 0xED :TM37 ;; eggbomb144 0xEE :TM38 ;; fire blast145 0xEF :TM39 ;; swift146 0xF0 :TM40 ;; skull bash147 0xF1 :TM41 ;; softboiled148 0xF2 :TM42 ;; dream eater149 0xF3 :TM43 ;; sky attack150 0xF4 :TM44 ;; rest151 0xF5 :TM45 ;; thunder wave152 0xF6 :TM46 ;; psywave153 0xF7 :TM47 ;; explosion154 0xF8 :TM48 ;; rock slide155 0xF9 :TM49 ;; tri attack156 0xFA :TM50 ;; substitute157 0xFB :TM51 ;; "cut"158 0xFC :TM52 ;; "fly"159 0xFD :TM53 ;; "surf"160 0xFE :TM54 ;; "strength"161 0xFF :end-of-list-sentinel162 ))164 (def item-name->item-code165 (zipmap (vals item-code->item-name)166 (keys item-code->item-name)))168 (defn inventory [^SaveState state]169 (let [items (item-list state)]170 (map171 (fn [[item-code quantity]]172 [(item-code->item-name173 item-code174 (str ":0x" (.toUpperCase (Integer/toHexString item-code))))175 quantity])176 (partition177 2178 (next (take-while (partial not= 255) items))))))180 (defn print-inventory181 ([] (print-inventory @current-state))182 ([^SaveState state]183 (println184 (let [inv (inventory state)]185 (reduce186 str187 (concat188 ["+-------------------+----------+\n"189 "|##| Item | Quantity |\n"190 "+--+----------------+----------+\n"]192 (map193 (fn [index [item-name quantity]]194 (str195 (format "|%-2d| %-14s | %3d |\n" index196 (apply str (rest (str item-name)))197 quantity)))198 (range 0 (count inv)) inv)199 ["+--+----------------+----------+\n"]))))200 state))202 (defn inventory-codes [inventory]203 (flatten204 (concat [(count inventory)]205 (map (fn [[item-name quantity]]206 [(item-name->item-code item-name)207 quantity]) inventory)208 [(item-name->item-code :end-of-list-sentinel)])))210 (defn set-inv-mem [^SaveState state inv-codes]211 (set-state! state)212 (let [mem (memory state)]213 (dorun (map (fn [index val]214 (aset mem index val))215 (range item-list-start216 (+ item-list-start (count inv-codes))) inv-codes))217 (write-memory! mem)))220 (defn set-inventory [^SaveState state new-inventory]221 (set-state! state)222 (let [mem (memory state)223 inv (inventory-codes new-inventory)]225 (dorun (map (fn [index val]226 (aset mem index val))227 (range item-list-start228 (+ item-list-start (count inv))) inv))229 (write-memory! mem)230 (update-state)))232 (defn give233 ([^SaveState state items]234 (set-inventory state235 (concat items (inventory state))))236 ([items]237 (give @current-state items)))240 (def gliched-tms241 [[:TM51 1]242 [:TM52 1]243 [:TM53 1]244 [:TM54 1]])246 (def good-items247 [[:bicycle 1]248 [:ultra-ball 15]249 [:pp-up 1]250 [:master-ball 5]251 [:rare-candy 99]252 [:full-restore 25]253 [:max-revive 8]254 [:max-repel 40]255 [:TM25 1]256 [:TM11 1]257 [:TM15 1]258 ])260 (def some-badges261 [[:cascadebadge 1]262 [:thunderbadge 1]263 [:rainbowbadge 1]264 [:soulbadge 1]265 ])267 (defn run-item-program268 "This is my first assembly/item program!269 it just increments BC by one.271 The code places a 3 'great balls' at the beginning of the272 inventory, then directly sets the program counter to start273 executing at the position of the 'great balls' in memory.275 Since a 'great ball' is represented in memory as 0x03, which276 corresponts to the opcode which increments BC by one, that is277 what happens. Then the program counter to the 0x03 quantity entry278 and BC is incremented again.280 Obviously, the game crashes more or less immediately after the281 program counter advances past the 'great balls' into the next items282 in the inventory, thus I call shutdown! before anything bad happens."283 []284 (set-inventory (read-state 578544) [[:great-ball 3]])285 (print-inventory)286 (println "3 ticks") (tick) (tick) (tick)287 (println "PC before:" (PC))288 (println "BC before:" (BC))289 (PC! (inc item-list-start))290 (println "PC after setting:" (PC))291 (println "data at PC:" (aget (memory) (PC)))292 (println "one tick")293 (tick)294 (println "PC after one tick:" (PC))295 (println "BC after one tick:" (BC))296 (tick)297 (println "PC after two ticks:" (PC))298 (println "BC after two ticks:" (BC))300 (shutdown!))303 (defn test-opcodes-1304 []305 (set-inv-mem (read-state 578544)306 [20 0x02 0x00 0x00 0x02 0x00 0x00307 0x00 0x0 0xFF])308 (print-inventory)309 (println "3 ticks") (tick) (tick) (tick)310 (println "PC before:" (PC))311 (println "BC before:" (BC))312 (println "AF:" (AF))313 (PC! (inc item-list-start))314 (BC! (+ 1 item-list-start))315 (println "PC after setting:" (PC))316 (println "data at PC:" (aget (memory) (PC)))317 (println "data at " (BC) "(BC):" (aget (memory) (BC)))319 (println "one tick")320 (tick)321 (println "PC after one tick:" (PC))322 (println "BC after one tick:" (BC))323 (println "data at PC:" (aget (memory) (PC)))324 (println "data at " (BC) "(BC):" (aget (memory) (BC)))325 (tick)326 (AF! 0xFFFF)327 (println "PC after two ticks:" (PC))328 (println "BC after two ticks:" (BC))329 (println "data at PC:" (aget (memory) (PC)))330 (println "data at " (BC) "(BC):" (aget (memory) (BC)))331 (tick)332 (println "PC after three ticks:" (PC))333 (println "BC after three ticks:" (BC))334 (println "data at PC:" (aget (memory) (PC)))335 (println "data at " (BC) "(BC):" (aget (memory) (BC)))336 (tick)337 (println "PC after four ticks:" (PC))338 (println "BC after four ticks:" (BC))339 (println "data at PC:" (aget (memory) (PC)))340 (println "data at " (BC) "(BC):" (aget (memory) (BC)))341 (tick)342 (println "PC after five ticks:" (PC))343 (println "BC after five ticks:" (BC))344 (println "data at PC:" (aget (memory) (PC)))345 (println "data at " (BC) "(BC):" (aget (memory) (BC)))346 (print-inventory)347 (shutdown!))351 (defn test-opcodes-2352 []353 (set-inv-mem (read-state 578544)354 [20 0x08 0x1D 0xD3 0x00 0x00 0x00355 0x00 0x0 0xFF])356 (print-inventory)357 (println "3 ticks") (tick) (tick) (tick)358 (println "PC before:" (PC))359 (println "SP:" (SP))360 (PC! (inc item-list-start))361 (println "PC after setting:" (PC))362 (println "SP:" (Integer/toBinaryString (SP)))363 (println "data at PC:" (aget (memory) (PC)))364 (println "data at 0xD31D:" (Integer/toBinaryString (aget (memory) 0xD31D)))365 (println "data at 0xD31E:" (Integer/toBinaryString (aget (memory) 0xD31E)))366 (println "one tick")367 (tick)368 (println "PC after one tick:" (PC))369 (println "data at PC:" (aget (memory) (PC)))370 (println "data at 0xD31D:" (Integer/toBinaryString (aget (memory) 0xD31D)))371 (println "data at 0xD31E:" (Integer/toBinaryString (aget (memory) 0xD31E)))372 (tick) (tick) (tick)373 (println "PC aftter four tick:" (PC))374 (println "data at PC:" (aget (memory) (PC)))375 (println "data at 0xD31D:" (aget (memory) 0xD31D))377 (print-inventory)378 (shutdown!))