580584e8bfbd

Moar.
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Tue, 05 Jul 2011 19:22:59 -0400
parents 8af375fc623f
children c242e7b1f58c
branches/tags (none)
files src/clojurecraft/actions.clj src/clojurecraft/core.clj src/clojurecraft/in.clj src/clojurecraft/out.clj src/clojurecraft/units.clj src/clojurecraft/util.clj

Changes

--- a/src/clojurecraft/actions.clj	Mon Jun 27 22:21:33 2011 -0400
+++ b/src/clojurecraft/actions.clj	Tue Jul 05 19:22:59 2011 -0400
@@ -1,1 +1,3 @@
-(ns clojurecraft.actions)
+(ns clojurecraft.actions
+  (:use [clojurecraft.util])
+  )
--- a/src/clojurecraft/core.clj	Mon Jun 27 22:21:33 2011 -0400
+++ b/src/clojurecraft/core.clj	Tue Jul 05 19:22:59 2011 -0400
@@ -2,6 +2,8 @@
   (:use [clojurecraft.mappings])
   (:use [clojurecraft.in])
   (:use [clojurecraft.out])
+  (:use [clojurecraft.util])
+  (:use [clojure.contrib.pprint :only (pprint)])
   (:import (java.net Socket)
            (java.io DataOutputStream DataInputStream)
            (java.util.concurrent LinkedBlockingQueue)))
@@ -19,7 +21,7 @@
   (read-packet bot)
 
   ; Send login
-  (write-packet bot :login {:version 13 :username "timmy"})
+  (write-packet bot :login {:version 14 :username "timmy"})
 
   ; Get login
   (read-packet bot))
@@ -36,8 +38,7 @@
   (let [conn (:connection bot)
         outqueue (:outqueue bot)]
     (while (nil? (:exit @conn))
-      (let [player (:player bot)
-            location (:location @player)]
+      (let [location (:location @(:player bot))]
         (when (not (nil? location))
           (.put outqueue [:playerpositionlook location]))
         (Thread/sleep 50)))))
@@ -58,7 +59,8 @@
         outqueue (LinkedBlockingQueue.)
         player (ref {:location nil})
         world (ref {})
-        bot {:connection conn, :outqueue outqueue, :player player, :world world}]
+        bot {:connection conn, :outqueue outqueue, :player player, :world world,
+             :packet-counts-in (atom {}), :packet-counts-out(atom {})}]
 
     (println "connecting")
     (login bot)
@@ -85,7 +87,9 @@
 
 
 ; Scratch --------------------------------------------------------------------------
-(def bot (connect minecraft-local))
+;(def bot (connect minecraft-local))
+(pprint (:packet-counts-in bot))
+(pprint (:packet-counts-out bot))
 ;(println (:world bot))
 ;(disconnect bot)
 
--- a/src/clojurecraft/in.clj	Mon Jun 27 22:21:33 2011 -0400
+++ b/src/clojurecraft/in.clj	Tue Jul 05 19:22:59 2011 -0400
@@ -1,4 +1,5 @@
 (ns clojurecraft.in
+  (:use [clojurecraft.util])
   (:use [clojurecraft.mappings])
   (:import (java.util.zip Inflater)))
 
@@ -359,13 +360,9 @@
                                  (:sizey predata)
                                  (:sizez predata)) 2))
         decompressor (Inflater.)]
-    (println 1)
     (.setInput decompressor raw-data 0 (:compressedsize predata))
-    (println 2)
     (.inflate decompressor buffer)
-    (println 3)
     (.end decompressor)
-    (println 4)
     buffer))
 
 (defn- read-packet-mapchunk [bot conn]
@@ -472,9 +469,9 @@
   (let [prepayload (assoc {}
                      :windowid (-read-byte conn)
                      :count (-read-short conn))
-        items (repeatedly (:count prepayload)
-                #(-read-packet-windowitems-payloaditem conn))]
-    (println (assoc prepayload :items items))))
+        items (doall (repeatedly (:count prepayload)
+                                 #(-read-packet-windowitems-payloaditem conn)))]
+    (assoc prepayload :items items)))
 
 (defn- read-packet-updateprogressbar [bot conn]
   (assoc {}
@@ -576,9 +573,19 @@
     (let [packet-id (when (not (nil? packet-id-byte))
                       (int packet-id-byte))
           packet-type (packet-types packet-id)]
+
+      ; Record the packet type
+      (dosync
+        (let [counts (:packet-counts-in bot)
+              current (get @counts packet-type 0)]
+          (swap! counts
+                 assoc
+                 packet-type
+                 (inc current))))
+
+      ; Handle packet
       (if (nil? packet-type)
         (do
-          (println packet-type)
           (println (str "UNKNOWN PACKET TYPE: " (Integer/toHexString packet-id)))
           (/ 1 0))
         (let [payload (do ((packet-type packet-readers) bot conn))]
--- a/src/clojurecraft/out.clj	Mon Jun 27 22:21:33 2011 -0400
+++ b/src/clojurecraft/out.clj	Tue Jul 05 19:22:59 2011 -0400
@@ -1,4 +1,5 @@
 (ns clojurecraft.out
+  (:use [clojurecraft.util])
   (:use [clojurecraft.mappings]))
 
 ; Writing Data ---------------------------------------------------------------------
@@ -281,8 +282,16 @@
 (defn write-packet [bot packet-type payload]
   (let [conn (:connection bot)
         handler (packet-type packet-writers)]
-    ;(println (str "--WRITE--> " packet-type))
-    ;(println payload)
+
+    ; Record the packet type
+    (dosync
+      (let [counts (:packet-counts-out bot)
+            current (get @counts packet-type 0)]
+        (swap! counts
+               assoc
+               packet-type
+               (inc current))))
+
     (-write-byte conn (packet-type packet-ids))
     (handler conn payload)
     (flushc conn)))
--- a/src/clojurecraft/units.clj	Mon Jun 27 22:21:33 2011 -0400
+++ b/src/clojurecraft/units.clj	Tue Jul 05 19:22:59 2011 -0400
@@ -1,4 +1,6 @@
-(ns clojurecraft.units)
+(ns clojurecraft.units
+  (:use [clojurecraft.util])
+  )
 
 (defn to-pixels [n type]
   (case type
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/clojurecraft/util.clj	Tue Jul 05 19:22:59 2011 -0400
@@ -0,0 +1,6 @@
+(ns clojurecraft.util)
+
+(defmacro l [& body]
+  `(let [result# (~@body)]
+     (println result#)
+     result#))