Mercurial > vba-clojure
view clojure/com/aurellem/exp/item_bridge.clj @ 272:a60ea8632ff4
beginning work on hxc-pokemon-base, the collection of base stats, battle sprite data, etc.
author | Dylan Holmes <ocsenave@gmail.com> |
---|---|
date | Tue, 27 Mar 2012 00:59:43 -0500 |
parents | b7f682bb3090 |
children | 8e63b0bb8ea3 |
line wrap: on
line source
1 (ns com.aurellem.exp.item-bridge2 (:use (com.aurellem.gb saves util constants gb-driver vbm items assembly))3 (:use (com.aurellem.run title save-corruption))4 ;;(:use (com.aurellem.exp pokemon))5 (:import [com.aurellem.gb.gb_driver SaveState]))7 (defn corrupt-item-state []8 (second (destroy-item-end-of-list-marker)))10 (defn corrupt-item-state []11 (read-state "corrupt-items"))13 (defn view-memory-range [state start end]14 (dorun15 (map (fn [loc val]16 (println (format "%04X : %02X" loc val)))17 (range start end) (subvec (vec (memory state)) start end)))18 state)20 (defn almost-broken21 "if one more memory location is turned into 0x03, the game crashes."22 [n]23 (view-memory-range24 (set-inv-mem (mid-game)25 (concat [0xFF] (repeat 64 0x03)26 (subvec (vec (memory (mid-game)))27 (+ item-list-start 65)28 (+ item-list-start 65 n))29 (repeat (- 255 65 n) 0x03)))30 item-list-start (+ item-list-start 255)))32 (defn actually-broken33 "if this memory location is turned into 0x03, the game crashes."34 []35 (set-memory (mid-game) 0xD35D 0x03))38 ;; (almost-broken 20) more or less works40 (defn capture-program-counter41 "records the program counter for each tick"42 [^SaveState state ticks]43 (let [i (atom 0)]44 (reduce (fn [[program-counters state] _]45 (println (swap! i inc))46 [(conj program-counters (PC state))47 (tick state)])48 [[] state]49 (range ticks))))52 (defn capture-program-counter53 [^SaveState state ticks]54 (tick state)56 (loop [i 057 pcs []]58 (if (= i ticks)59 (filter (partial < 0x2000)(sort (set pcs)))60 (do61 (com.aurellem.gb.Gb/tick)62 (recur (inc i)63 (conj pcs (first (registers))))))))65 (defn loop-program []66 [0x00 ;0xD31D ;; disable-interrupts68 0xC3 ;; loop forever69 0x1D70 0xD3])72 (def map-function-address-start 0xD36D)74 (defn test-loop []75 (continue!76 (-> (mid-game)77 (set-memory-range 0xD31D (loop-program))78 (set-memory-range79 map-function-address-start80 [0xD3 0x1D]))))82 (defn-memo corrupt-moves []83 (concat84 (first85 (->>86 [[] (mid-game)]87 (advance [:b] [:b :start])88 (advance [] [:d])89 (play-moves [[] [] [] [:d] [] [] [] [:d] [] [] [:a]])90 scroll-text91 (play-moves92 ;; this section is copied from speedrun-294293 ;; and corrupts the save so that the end-of-list marker94 ;; for the pokemon roster is destroyed, but the save is still95 ;; playable.96 [[] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] []97 [] [] [] [] [] [] [] [] [] [] [:select] [:restart]])98 (title)99 (advance [] [:start])100 (advance [] [:a])101 (advance [:a] [:a :start])))102 [[]]))104 (defn corrupt105 "enter the codes to destroy the106 pokemon list using save corruption"107 ([^SaveState state]108 (run-moves109 state110 (corrupt-moves)))111 ([] (corrupt @current-state)))113 (defn mid-game-corrupt []114 (read-state "corrupt-mid-game"))116 (defn gen-start-game-corrupt []117 (->> (second (intro))118 (advance [:b] [:a :b :start])119 (play-moves (corrupt-moves))))121 (defn start-game-corrupt []122 (read-state "corrupt-start-game"))124 (defn test-memory-fun [n]125 (capture-program-counter126 (set-memory-range127 (tick (mid-game))128 0xD36D129 [0 0])130 n))132 ;;(def good (test-memory-fun 17000))134 ;;(def bad (test-memory-fun 18000))138 (defn menu-open-state []139 (read-state "menu-open"))141 (defn prepare-memory142 ([^SaveState state]143 (-> state144 (set-memory-range 0xD31D (loop-program))145 (set-memory-range 0xD36D [0x1D 0xD3])))146 ([] (prepare-memory @current-state)))148 (def memory-function-address-start 0xD36D)150 (defn read-map-function-address151 ([^SaveState state]152 (let [mem (memory state)]153 [(aget mem memory-function-address-start)154 (aget mem (inc memory-function-address-start))]))155 ([] (read-map-function-address @current-state)))157 (defn succesful-PC-capture158 "This function demonstrates successful PC capturing by159 setting 0xD36D to the value of the start location of160 a specially prepared program.162 You must run the function and then exit the open menu163 to see the effect."164 []165 (dorun166 (map #(println (Integer/toHexString %))167 (capture-program-counter168 (prepare-memory (menu-open-state))169 9000000))))171 (defn trampoline-assembly [^SaveState state]172 (flatten173 [0x3E ;;174 0x3E ;; load lemonade into A176 0xEA177 0x1D178 0xD3 ;; set first item to lemonade180 0xC3 ;; return control to the game via absolute jump.181 (read-map-function-address state)182 ]))184 (defn test-trampoline185 "Demonstrates item-program execution via the map-function that186 returns control to the main pokemon game after one loop."187 [assembly-fn state]188 (let [insertion-address 0xD33D189 insertion-address-bits [0x3D 0xD3]]190 (->191 state192 (set-memory-range193 insertion-address194 (assembly-fn state))195 (set-memory-range196 memory-function-address-start197 insertion-address-bits))))199 (def lemonade-trampoline200 (partial test-trampoline201 trampoline-assembly202 (menu-open-state)))204 (defn trampoline-assembly-burn-heal [^SaveState state]205 (flatten206 [0x3E ;;207 0x3E ;; load lemonade into A209 0xEA210 0x1D211 0xD3 ;; set first item to lemonade213 0xC3 ;; return control to the game via absolute jump214 0x0C ;; to Route 3's map-function215 0x55216 ]))220 (def pc-item-list-start 0xD539)221 (def pc-item-list-width 101)223 (def corrupted-items-width 512)225 (defn items-record226 ([^SaveState state]227 (subvec (vec (memory state))228 item-list-start229 (+ item-list-start corrupted-items-width)))230 ([] (items-record @current-state)))232 (defn pc-items-record233 ([^SaveState state]234 (subvec (vec (memory state))235 pc-item-list-start236 (+ pc-item-list-width pc-item-list-start)))237 ([] (pc-items-record @current-state)))239 (defn print-listing-items240 ([^SaveState state]241 (print-listing state item-list-start242 (+ item-list-start corrupted-items-width))243 state)244 ([] (print-listing-items @current-state)))246 (defn print-listing-pc-items247 ([^SaveState state]248 (print-listing249 state250 pc-item-list-start251 (+ pc-item-list-width pc-item-list-start))252 state)253 ([] (print-listing-pc-items @current-state)))