Mercurial > vba-clojure
view clojure/com/aurellem/vbm.clj @ 68:86093f2ce7d1
got the speedrun to play
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Thu, 08 Mar 2012 02:10:03 -0600 |
parents | |
children | ff6f1acae59e |
line wrap: on
line source
1 (ns com.aurellem.vbm2 (:import java.io.File)3 (:import org.apache.commons.io.FileUtils))5 (defn vbm-bytes [#^File vbm]6 (let [bytes (FileUtils/readFileToByteArray vbm)7 ints (int-array (count bytes))]8 (areduce bytes idx _ nil9 (aset ints idx10 (bit-and 0xFF (aget bytes idx))))11 ints))13 (def button-mask14 {;; main buttons15 :a 0x000116 :b 0x000218 ;; directional pad19 :r 0x001020 :l 0x002021 :u 0x004022 :d 0x008024 ;; meta buttons25 :select 0x000426 :start 0x000828 ;; hard reset -- not really a button29 :reset 0x0800})31 (defn button-code [buttons]32 (reduce bit-or 0x0000 (map button-mask buttons)))34 (defn buttons [mask]35 (loop [buttons []36 masks (seq button-mask)]37 (if (empty? masks) buttons38 (let [[button value] (first masks)]39 (if (not= 0x0000 (bit-and value mask))40 (recur (conj buttons button) (rest masks))41 (recur buttons (rest masks)))))))43 (def vbm-header-length 255)45 (defn repair-vbm46 "Two 0's must be inserted after every reset, and the first47 button must be dropped"48 [vbm-seq]49 (loop [fixed []50 pending (next vbm-seq)]51 (if (empty? pending) fixed52 (let [mask (first pending)]53 (if (not= 0x0000 (bit-and mask (button-mask :reset)))54 (recur (conj fixed mask 0x0000 0x0000) (next pending))55 (recur (conj fixed mask) (next pending)))))))57 (defn vbm-masks [#^File vbm]58 (repair-vbm59 (map (fn [[a b]]60 (+ (bit-shift-left a 8) b))61 (partition62 2 (drop vbm-header-length (vbm-bytes vbm))))))64 (defn vbm-buttons [#^File vbm]65 (map buttons (vbm-masks vbm)))