Followbox and associated improvements.
It's still a bit buggy, but I'm done for the night.
Changes
--- a/src/clojurecraft/actions.clj Wed Aug 31 19:13:21 2011 -0400
+++ b/src/clojurecraft/actions.clj Fri Sep 02 20:10:35 2011 -0400
@@ -58,6 +58,7 @@
:turn-to turn-to!
:look-to look-to!
:respawn respawn!} (:action action))]
+ (println "PERFORMING" (:action action) (:args action))
(apply performer (:bot action) (:args action))))
--- a/src/clojurecraft/in.clj Wed Aug 31 19:13:21 2011 -0400
+++ b/src/clojurecraft/in.clj Fri Sep 02 20:10:35 2011 -0400
@@ -229,9 +229,9 @@
: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))
@@ -313,8 +313,11 @@
: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 {}
@@ -328,11 +331,21 @@
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 {}
@@ -350,13 +363,22 @@
: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 {}
@@ -455,15 +477,16 @@
(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))
--- a/src/clojurecraft/loops.clj Wed Aug 31 19:13:21 2011 -0400
+++ b/src/clojurecraft/loops.clj Fri Sep 02 20:10:35 2011 -0400
@@ -7,7 +7,9 @@
(@(: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]
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/examples/followbot.clj Fri Sep 02 20:10:35 2011 -0400
@@ -0,0 +1,66 @@
+(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))
+
--- a/src/examples/jumpbot.clj Wed Aug 31 19:13:21 2011 -0400
+++ b/src/examples/jumpbot.clj Fri Sep 02 20:10:35 2011 -0400
@@ -16,6 +16,5 @@
(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))