Multiple block change and more.
author |
Steve Losh <steve@stevelosh.com> |
date |
Tue, 09 Aug 2011 23:14:05 -0400 |
parents |
7102fee989ed
|
children |
313f4e566541
|
branches/tags |
(none) |
files |
src/clojurecraft/core.clj src/clojurecraft/events.clj src/clojurecraft/in.clj src/clojurecraft/util.clj |
Changes
--- a/src/clojurecraft/core.clj Tue Aug 09 22:25:57 2011 -0400
+++ b/src/clojurecraft/core.clj Tue Aug 09 23:14:05 2011 -0400
@@ -38,20 +38,21 @@
(write-packet bot :handshake {:username username})
; Get handshake
- (read-packet bot nil)
+ (read-packet bot nil nil)
; Send login
(write-packet bot :login {:version 14 :username username})
; Get login
- (get (read-packet bot nil) 1))
+ (get (read-packet bot nil nil) 1))
(defn input-handler [bot]
(let [conn (:connection bot)]
- (loop [prev nil]
+ (loop [prev nil
+ prev-prev nil]
(if (nil? (:exit @conn))
- (recur (read-packet bot prev))
+ (recur (read-packet bot prev prev-prev) prev)
prev)))
(println "done - input handler"))
--- a/src/clojurecraft/events.clj Tue Aug 09 22:25:57 2011 -0400
+++ b/src/clojurecraft/events.clj Tue Aug 09 23:14:05 2011 -0400
@@ -10,7 +10,10 @@
(dosync (alter (:event-handlers bot) dissoc event-type)))
+(defn- fire-handler [bot event-type & args]
+ (dorun (map #(apply (eval %) (into [bot] args))
+ (event-type @(:event-handlers bot)))))
+
(defn fire-chat [bot message]
- (dorun (map #((eval %) bot message)
- (:chat @(:event-handlers bot)))))
+ (fire-handler bot :chat message))
--- a/src/clojurecraft/in.clj Tue Aug 09 22:25:57 2011 -0400
+++ b/src/clojurecraft/in.clj Tue Aug 09 23:14:05 2011 -0400
@@ -430,7 +430,7 @@
(defn -update-single-block [bot x y z type meta]
- (println "Updating block" x y z "to be type" type)
+ (println "Updating block" x y z "to be type" (block-types type))
(dosync (let [chunk (chunk-containing x z (:chunks (:world bot)))
i (block-index-in-chunk x y z)]
(when chunk
@@ -438,15 +438,6 @@
:types (replace-array-index (:types @chunk) i type)
:metadata (replace-array-index (:metadata @chunk) i meta))))))
-(defn- read-packet-multiblockchange [bot conn]
- (let [prearrays (assoc {}
- :chunkx (-read-int conn)
- :chunkz (-read-int conn)
- :arraysize (-read-short conn))]
- (assoc prearrays
- :coordinatearray (-read-shortarray conn (:arraysize prearrays))
- :typearray (-read-bytearray conn (:arraysize prearrays))
- :metadataarray (-read-bytearray conn (:arraysize prearrays)))))
(defn- read-packet-blockchange [bot conn]
(let [data (assoc {}
@@ -461,6 +452,26 @@
data))
+(defn- read-packet-multiblockchange [bot conn]
+ (let [prearrays (assoc {}
+ :chunkx (-read-int conn)
+ :chunkz (-read-int conn)
+ :arraysize (-read-short conn))
+ payload (assoc prearrays
+ :coordinatearray (-read-shortarray conn (:arraysize prearrays))
+ :typearray (-read-bytearray conn (:arraysize prearrays))
+ :metadataarray (-read-bytearray conn (:arraysize prearrays)))
+ parse-coords (fn [s] [(top-4 s) (mid-4 s) (bottom-8 s)])
+ coords (map parse-coords (:coordinatearray payload))]
+ (println "Reading a Multiple Block Change!")
+ (dorun (map #(-update-single-block bot (get %1 0) (get %1 2) (get %1 1) %2 %3)
+ coords
+ (:typearray payload)
+ (:metadataarray payload)))
+ (println "Done with a Multiple Block Change!")
+ payload))
+
+
(defn- read-packet-playnoteblock [bot conn]
(assoc {}
:x (-read-int conn)
@@ -633,7 +644,7 @@
:disconnectkick read-packet-disconnectkick})
; Reading Wrappers -----------------------------------------------------------------
-(defn read-packet [bot prev]
+(defn read-packet [bot prev prev-prev]
(let [conn (:connection bot)
packet-id-byte (to-unsigned (-read-byte conn))]
(let [packet-id (when (not (nil? packet-id-byte))
@@ -659,5 +670,5 @@
(do
(when (#{} packet-type)
(println (str "--PACKET--> " packet-type)))
- [packet-type payload]))))))
+ [[packet-type payload] prev]))))))
--- a/src/clojurecraft/util.clj Tue Aug 09 22:25:57 2011 -0400
+++ b/src/clojurecraft/util.clj Tue Aug 09 23:14:05 2011 -0400
@@ -39,6 +39,7 @@
(defn any? [s]
(not (empty? (filter identity s))))
+
; Bytes ----------------------------------------------------------------------------
(defn byte-seq [b]
(loop [n 0 b b s []]
@@ -56,3 +57,21 @@
(bit-and b 0xff))
+(defn top-4 [b]
+ "Return the top four bits of a short.
+
+ XXXX............"
+ (byte (bit-shift-right (bit-and b 0xf000) 12)))
+
+(defn mid-4 [b]
+ "Return the middle four bits of a short.
+
+ ....XXXX........"
+ (byte (bit-shift-right (bit-and b 0x0f00) 8)))
+
+(defn bottom-8 [b]
+ "Return the bottom eight bits of a short.
+
+ ........XXXXXXXX"
+ (byte (bit-and b 0xff)))
+