Commits

Steve Losh  committed 7942222

Followbox and associated improvements.

It's still a bit buggy, but I'm done for the night.

  • Participants
  • Parent commits c14f351

Comments (0)

Files changed (5)

File src/clojurecraft/actions.clj

                     :turn-to turn-to!
                     :look-to look-to!
                     :respawn respawn!} (:action action))]
+    (println "PERFORMING" (:action action) (:args action))
     (apply performer (:bot action) (:args action))))
 
 

File src/clojurecraft/in.clj

                      :name (:playername payload)
                      :holding (item-types (:currentitem payload))
                      :despawned false}
-        location-data {:x (:x payload)
-                       :y (:y payload)
-                       :z (:z payload)}]
+        location-data {:x (float (/ (:x payload) 32)) ; These are sent as absolute
+                       :y (float (/ (:y payload) 32)) ; ints, not normal floats.
+                       :z (float (/ (:z payload) 32))}]
     (dosync
       (let [eid (:eid payload)
             entities (:entities (:world bot))
          :velocityz (-read-short conn)))
 
 (defn- read-packet-entitydestroy [bot conn]
-  (assoc {}
-         :eid (-read-int conn)))
+  (let [payload (assoc {}
+                       :eid (-read-int conn))]
+    (println "KILLING -->" (:eid payload))
+    (dosync (alter (:entities (:world bot)) dissoc (:eid payload)))
+    payload))
 
 (defn- read-packet-entity [bot conn]
   (let [payload (assoc {}
     payload))
 
 (defn- read-packet-entityrelativemove [bot conn]
-  (assoc {}
-         :eid (-read-int conn)
-         :dx (-read-byte conn)
-         :dy (-read-byte conn)
-         :dz (-read-byte conn)))
+  ; TODO: handle items
+  (let [payload (assoc {}
+                       :eid (-read-int conn)
+                       :dx (float (/ (-read-byte conn) 32))
+                       :dy (float (/ (-read-byte conn) 32))
+                       :dz (float (/ (-read-byte conn) 32)))]
+    (dosync
+      (let [entity (@(:entities (:world bot)) (:eid payload))]
+        (when entity
+          (let [old-loc (:loc @entity)
+                new-loc (merge old-loc {:x (+ (:x old-loc) (:dx payload))
+                                        :y (+ (:y old-loc) (:dy payload))
+                                        :z (+ (:z old-loc) (:dz payload))})]
+            (alter entity assoc :loc new-loc)))))
+    payload))
 
 (defn- read-packet-entitylook [bot conn]
   (assoc {}
          :pitch (-read-byte conn)))
 
 (defn- read-packet-entityteleport [bot conn]
-  (assoc {}
-         :eid (-read-int conn)
-         :x (-read-int conn)
-         :y (-read-int conn)
-         :z (-read-int conn)
-         :yaw (-read-byte conn)
-         :pitch (-read-byte conn)))
+  ; TODO: record yaw/pitch
+  (let [payload (assoc {}
+                       :eid (-read-int conn)
+                       :x (float (/ (-read-int conn) 32))
+                       :y (float (/ (-read-int conn) 32))
+                       :z (float (/ (-read-int conn) 32))
+                       :yaw (-read-byte conn)
+                       :pitch (-read-byte conn))]
+    (dosync
+      (let [entity (@(:entities (:world bot)) (:eid payload))
+            old-loc (:loc @entity)
+            new-loc (merge old-loc {:x (:x payload)
+                                    :y (:y payload)
+                                    :z (:z payload)})]
+        (alter entity assoc :loc new-loc)))
+    payload))
 
 (defn- read-packet-entitystatus [bot conn]
   (let [payload (assoc {}
             (replace-array-slice (:sky-light (force @chunk)) start-index sky))))
 
 (defn- read-packet-mapchunk [bot conn]
-  (time (let [predata (-read-mapchunk-predata conn)
-              postdata (assoc predata :raw-data (-read-bytearray-bare conn (:compressedsize predata)))
-              chunk-size (* (:sizex postdata) (:sizey postdata) (:sizez postdata))
-              chunk-coords (coords-of-chunk-containing (:x postdata) (:z postdata))]
-          (dosync (alter (:chunks (:world bot))
-                         assoc chunk-coords (if (= FULL-CHUNK chunk-size)
-                                              (ref (delay (-chunk-from-full-data postdata)))
-                                              (ref (-chunk-from-partial-data bot postdata)))))
-          predata)))
+  (let [predata (-read-mapchunk-predata conn)
+        postdata (assoc predata :raw-data (-read-bytearray-bare conn
+                                                                (:compressedsize predata)))
+        chunk-size (* (:sizex postdata) (:sizey postdata) (:sizez postdata))
+        chunk-coords (coords-of-chunk-containing (:x postdata) (:z postdata))]
+    (dosync (alter (:chunks (:world bot))
+                   assoc chunk-coords (if (= FULL-CHUNK chunk-size)
+                                        (ref (delay (-chunk-from-full-data postdata)))
+                                        (ref (-chunk-from-partial-data bot postdata)))))
+    predata))
 
 
 

File src/clojurecraft/loops.clj

               (@(:loops bot) loop-id))
     (handle-action-group bot ((deref function) bot))
     (Thread/sleep sleep-ms))
-  (dosync (alter (:loops bot) dissoc loop-id)))
+  (dosync (alter (:loops bot) dissoc loop-id))
+  (println "done - loop -" loop-id)
+  (println "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"))
 
 
 (defn remove-loop [bot loop-id]

File src/examples/followbot.clj

+(ns examples.followbot
+  (:use [clojure.contrib.math :only (abs)])
+  (:require [clojurecraft.core :as core])
+  (:require [clojurecraft.events :as events])
+  (:require [clojurecraft.loops :as loops])
+  (:require [clojurecraft.actions :as actions]))
+
+; Helper functions -----------------------------------------------------------------
+(defn- locs [bot entity]
+  [(:loc @(:player bot))
+   (:loc @entity)])
+
+(defn- distance-between [bot entity]
+  (let [[from to] (locs bot entity)]
+    (+ (abs (- (:x from) (:x to)))
+       (abs (- (:y from) (:y to)))
+       (abs (- (:z from) (:z to))))))
+
+(defn- toward-single [from to]
+  (if (<= (abs (- from to)) 1)
+    0
+    (if (< from to) 1 -1)))
+
+(defn- toward [bot entity]
+  (let [[from to] (locs bot entity)]
+    {:x (toward-single (:x from) (:x to))
+     :z (toward-single (:z from) (:z to))}))
+
+(defn- find-other-players [bot]
+  (remove #(= (:name @%)
+              (:name @(:player bot)))
+          (filter #(:name @%)
+                  (vals @(:entities (:world bot))))))
+
+(defn- closest-entity [bot entities]
+  (when (seq entities)
+    (first (sort #(< (distance-between bot %1)
+                     (distance-between bot %2))
+                 entities))))
+
+; Loops ----------------------------------------------------------------------------
+(defn follow [bot]
+  (when (:loc @(:player bot))
+    (let [players (find-other-players bot)
+          closest (closest-entity bot players)]
+      (when closest
+        (println (:name @(:player bot)) "following" (:name @closest))
+        (println (:loc @(:player bot)))
+        (println (:loc @closest))
+        (let [{x :x z :z} (toward bot closest)]
+          [(actions/move bot x 0 z)])))))
+
+
+; Event handlers -------------------------------------------------------------------
+(defn handle-dead [bot]
+  [(actions/chat bot "WTF mate?")
+   (actions/respawn bot)])
+
+
+; Creation function ----------------------------------------------------------------
+(defn make-followbot [server username]
+  (let [bot (core/connect server username)]
+    (events/add-handler bot :dead #'handle-dead)
+    (loops/add-loop bot #'follow 200 :follow-loop)
+    bot))
+

File src/examples/jumpbot.clj

   (let [bot (core/connect server username)]
     (events/add-handler bot :dead #'handle-dead)
     (loops/add-loop bot #'jump 2000 :jump-loop)
-    (Thread/sleep 10000)
     bot))