aa67f93d6fe4

Handle namedentityspawn packets.
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Fri, 26 Aug 2011 20:12:29 -0400
parents cd03086eadf7
children 19fc4b6444f5
branches/tags (none)
files src/clojurecraft/core.clj src/clojurecraft/data.clj src/clojurecraft/in.clj

Changes

--- a/src/clojurecraft/core.clj	Fri Aug 26 20:12:02 2011 -0400
+++ b/src/clojurecraft/core.clj	Fri Aug 26 20:12:29 2011 -0400
@@ -122,7 +122,7 @@
     ; We need to log in to find out our bot's entity ID, so we delay creation of the
     ; player until then.
     (let [player-id (:eid (login bot username))
-          player (ref (Entity. player-id nil false 0.0))
+          player (ref (Entity. player-id nil username nil false 0.0))
           bot (assoc bot :player player)]
 
       ; Theoretically another connected bot could fill in the player's entity entry
--- a/src/clojurecraft/data.clj	Fri Aug 26 20:12:02 2011 -0400
+++ b/src/clojurecraft/data.clj	Fri Aug 26 20:12:29 2011 -0400
@@ -20,6 +20,13 @@
 ; eid -> Integer
 ; loc -> Location
 ;
+; name -> String
+;   The name of the player, or nil if this entity is not a player.
+;
+; holding -> Keyword (Item Type)
+;   The type of item this entity is holding, or nil if this entity is not holding
+;   anything.
+;
 ; despawned -> boolean
 ;   True if the entity has been despawned.
 ;
@@ -27,7 +34,7 @@
 ;   something has changed, since otherwise there would be no way to tell
 ;   (despawning would simply remove the object from the entity list
 ;   without modifying it).
-(defrecord Entity [eid loc despawned velocity])
+(defrecord Entity [eid loc name holding despawned velocity])
 
 ; Block
 ;
--- a/src/clojurecraft/in.clj	Fri Aug 26 20:12:02 2011 -0400
+++ b/src/clojurecraft/in.clj	Fri Aug 26 20:12:29 2011 -0400
@@ -11,6 +11,13 @@
 (def FULL-CHUNK (* 16 16 128))
 (def BLANK-CHUNK-ARRAY (byte-array FULL-CHUNK))
 
+; Convenience Functions ------------------------------------------------------------
+(defn- blank-entity []
+  (Entity. nil
+           (Location. nil nil nil nil nil nil nil)
+           nil nil false 0.0))
+
+
 ; Reading Data ---------------------------------------------------------------------
 (defn- -read-byte-bare [conn]
   (io!
@@ -209,15 +216,32 @@
          :action (-read-byte conn)))
 
 (defn- read-packet-namedentityspawn [bot conn]
-  (assoc {}
-         :eid (-read-int conn)
-         :playername (-read-string-ucs2 conn)
-         :x (-read-int conn)
-         :y (-read-int conn)
-         :z (-read-int conn)
-         :rotation (-read-byte conn)
-         :pitch (-read-byte conn)
-         :currentitem (-read-short conn)))
+  (let [payload (assoc {}
+                       :eid (-read-int conn)
+                       :playername (-read-string-ucs2 conn)
+                       :x (-read-int conn)
+                       :y (-read-int conn)
+                       :z (-read-int conn)
+                       :rotation (-read-byte conn)
+                       :pitch (-read-byte conn)
+                       :currentitem (-read-short conn))
+        entity-data {:eid (:eid payload)
+                     :name (:playername payload)
+                     :holding (item-types (:currentitem payload))
+                     :despawned false}
+        location-data {:x (:x payload)
+                       :y (:y payload)
+                       :z (:z payload)}]
+    (dosync
+      (let [eid (:eid payload)
+            entities (:entities (:world bot))
+            entity (or (let [e (@entities eid)] (when e @e)) (blank-entity))
+            new-loc (merge (:loc entity) location-data)
+            new-entity-data (assoc entity-data :loc new-loc)]
+        ; I'm not sure this is right.  We should probably be altering the entity ref
+        ; if it already exists.
+        (alter entities assoc eid (ref (merge entity new-entity-data)))))
+    payload))
 
 (defn- read-packet-pickupspawn [bot conn]
   (assoc {}
@@ -300,7 +324,7 @@
             entities (:entities (:world bot))
             entity (@entities eid)]
         (when-not entity
-          (alter entities assoc eid (Entity. eid nil false 0.0)))))
+          (alter entities assoc eid (ref (Entity. eid nil nil nil false 0.0))))))
     payload))
 
 (defn- read-packet-entityrelativemove [bot conn]