313f4e566541

Fix vehicle packet handling.  Derp.
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Tue, 09 Aug 2011 23:36:06 -0400
parents 53bc495283f5
children 99b79096ff32
branches/tags (none)
files src/clojurecraft/core.clj src/clojurecraft/in.clj src/clojurecraft/util.clj

Changes

--- a/src/clojurecraft/core.clj	Tue Aug 09 23:14:05 2011 -0400
+++ b/src/clojurecraft/core.clj	Tue Aug 09 23:36:06 2011 -0400
@@ -38,22 +38,20 @@
   (write-packet bot :handshake {:username username})
 
   ; Get handshake
-  (read-packet bot nil nil)
+  (read-packet bot nil nil nil)
 
   ; Send login
   (write-packet bot :login {:version 14 :username username})
 
   ; Get login
-  (get (read-packet bot nil nil) 1))
+  (get (read-packet bot nil nil nil) 1))
 
 
 (defn input-handler [bot]
   (let [conn (:connection bot)]
-    (loop [prev nil
-           prev-prev nil]
-      (if (nil? (:exit @conn))
-        (recur (read-packet bot prev prev-prev) prev)
-        prev)))
+    (loop [prevs [nil nil nil]]
+      (when (nil? (:exit @conn))
+        (recur (read-packet bot (get prevs 0) (get prevs 1) (get prevs 2))))))
   (println "done - input handler"))
 
 
--- a/src/clojurecraft/in.clj	Tue Aug 09 23:14:05 2011 -0400
+++ b/src/clojurecraft/in.clj	Tue Aug 09 23:36:06 2011 -0400
@@ -237,12 +237,12 @@
                           :y (-read-int conn)
                           :z (-read-int conn)
                           :moar (-read-int conn))]
-    (if (< 0 (:moar basepacket))
-      basepacket
+    (if (> (:moar basepacket) 0)
       (assoc basepacket
              :unknownx (-read-int conn)
              :unknowny (-read-int conn)
-             :unknownz (-read-int conn)))))
+             :unknownz (-read-int conn))
+      basepacket)))
 
 (defn- read-packet-mobspawn [bot conn]
   (assoc {}
@@ -430,7 +430,6 @@
 
 
 (defn -update-single-block [bot x y z type meta]
-  (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
@@ -463,12 +462,10 @@
                        :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))
 
 
@@ -644,7 +641,7 @@
                      :disconnectkick            read-packet-disconnectkick})
 
 ; Reading Wrappers -----------------------------------------------------------------
-(defn read-packet [bot prev prev-prev]
+(defn read-packet [bot prev prev-prev prev-prev-prev]
   (let [conn (:connection bot)
         packet-id-byte (to-unsigned (-read-byte conn))]
     (let [packet-id (when (not (nil? packet-id-byte))
@@ -663,12 +660,15 @@
       ; Handle packet
       (if (nil? packet-type)
         (do
-          (println (str "UNKNOWN PACKET TYPE: " (Integer/toHexString packet-id) packet-id
-                        " ---------- PREVIOUS: " prev))
+          (println "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
+          (println "UNKNOWN PACKET TYPE:" (Integer/toHexString packet-id) packet-id)
+          (println "THREE-AGO:" prev-prev-prev)
+          (println "TWO-AGO:" prev-prev)
+          (println "ONE-AGO:" prev)
           (/ 1 0))
         (let [payload (do ((packet-type packet-readers) bot conn))]
           (do
             (when (#{} packet-type)
               (println (str "--PACKET--> " packet-type)))
-            [[packet-type payload] prev]))))))
+            [[packet-type payload] prev prev-prev]))))))
 
--- a/src/clojurecraft/util.clj	Tue Aug 09 23:14:05 2011 -0400
+++ b/src/clojurecraft/util.clj	Tue Aug 09 23:36:06 2011 -0400
@@ -1,5 +1,6 @@
 (ns clojurecraft.util)
 
+; Logging---------------------------------------------------------------------------
 (defmacro l [& body]
   `(let [result# (~@body)]
      (println result#)
@@ -11,6 +12,8 @@
      (println (class result#))
      result#))
 
+
+; Other ----------------------------------------------------------------------------
 (defn invert [m]
   (apply assoc {} (mapcat reverse m)))
 
@@ -56,10 +59,9 @@
 (defn to-unsigned [b]
   (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)))
 
@@ -71,7 +73,7 @@
 
 (defn bottom-8 [b]
   "Return the bottom eight bits of a short.
-  
+
   ........XXXXXXXX"
   (byte (bit-and b 0xff)))