43f02bd6b197

Make handlers return lists.
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Wed, 10 Aug 2011 01:25:24 -0400
parents 2e42d7f29dde
children 29631ac2b821
branches/tags (none)
files src/clojurecraft/actions.clj src/clojurecraft/core.clj src/clojurecraft/data.clj src/clojurecraft/events.clj

Changes

--- a/src/clojurecraft/actions.clj	Wed Aug 10 00:37:28 2011 -0400
+++ b/src/clojurecraft/actions.clj	Wed Aug 10 01:25:24 2011 -0400
@@ -2,24 +2,23 @@
   (:use [clojurecraft.util])
   (:require [clojurecraft.physics :as physics]))
 
-
 (defn move [bot x-change y-change z-change]
-  (let [player (:player bot)]
-    (dosync
-      (let [location (:loc @player)
-            new-location (merge location
-                                {:x (+ x-change (:x location))
-                                 :y (+ y-change (:y location))
-                                 :z (+ z-change (:z location))
-                                 :stance (+ y-change (:stance location))})]
-        (alter player merge {:loc new-location}))))
-  nil)
+  (delay
+    (let [player (:player bot)]
+      (dosync
+        (let [location (:loc @player)
+              new-location (merge location
+                                  {:x (+ x-change (:x location))
+                                   :y (+ y-change (:y location))
+                                   :z (+ z-change (:z location))
+                                   :stance (+ y-change (:stance location))})]
+          (alter player merge {:loc new-location}))))))
 
 (defn jump [bot]
-  (let [player (:player bot)]
-    (dosync
-      (let [location (:loc @player)]
-        (alter player assoc-in [:loc :onground] false)
-        (alter player assoc :velocity physics/JUMP-VELOCITY))))
-  nil)
+  (delay
+    (let [player (:player bot)]
+      (dosync
+        (let [location (:loc @player)]
+          (alter player assoc-in [:loc :onground] false)
+          (alter player assoc :velocity physics/JUMP-VELOCITY))))))
 
--- a/src/clojurecraft/core.clj	Wed Aug 10 00:37:28 2011 -0400
+++ b/src/clojurecraft/core.clj	Wed Aug 10 01:25:24 2011 -0400
@@ -90,6 +90,15 @@
             (write-packet bot packet-type payload))))))
   (println "done - output handler"))
 
+(defn action-handler [bot]
+  (let [conn (:connection bot)
+        actionqueue (:actionqueue bot)]
+    (while (nil? (:exit @conn))
+      (let [action (.poll actionqueue 1 TimeUnit/SECONDS)]
+        (when action
+          (force action)))))
+  (println "done - action handler"))
+
 
 (defn connect [server username]
   (let [username (or username (random-username))
@@ -98,8 +107,9 @@
         out (DataOutputStream. (.getOutputStream socket))
         conn (ref {:in in :out out})
         outqueue (LinkedBlockingQueue.)
+        actionqueue (LinkedBlockingQueue.)
         world (get-world server)
-        bot (Bot. conn outqueue nil world (ref {})
+        bot (Bot. conn outqueue actionqueue nil world (ref {})
                   (atom {}) (atom {}))]
 
     (println "connecting")
@@ -133,6 +143,9 @@
       (println "starting location updating handler")
       (.start (Thread. #(location-handler bot)))
 
+      (println "starting action handler")
+      (.start (Thread. #(action-handler bot)))
+
       (println "all systems go!")
 
       bot)))
--- a/src/clojurecraft/data.clj	Wed Aug 10 00:37:28 2011 -0400
+++ b/src/clojurecraft/data.clj	Wed Aug 10 01:25:24 2011 -0400
@@ -105,6 +105,6 @@
 ;
 ; packet-counts-in  -> (atom {:packet-type integer})
 ; packet-counts-out -> (atom {:packet-type integer})
-(defrecord Bot [connection outqueue player world event-handlers
-                packet-counts-in packet-counts-out])
+(defrecord Bot [connection outqueue actionqueue player world
+                event-handlers packet-counts-in packet-counts-out])
 
--- a/src/clojurecraft/events.clj	Wed Aug 10 00:37:28 2011 -0400
+++ b/src/clojurecraft/events.clj	Wed Aug 10 01:25:24 2011 -0400
@@ -11,8 +11,12 @@
 
 
 (defn- fire-handler [bot event-type & args]
-  (dorun (map #(apply (eval %) (into [bot] args))
-              (event-type @(:event-handlers bot)))))
+  (let [action-groups (map #(apply (eval %) (into [bot] args))
+                           (event-type @(:event-handlers bot)))
+        queue-action #(.put (:actionqueue bot) %)
+        queue-action-group #(map queue-action %)]
+    (println action-groups)
+    (dorun (map queue-action-group action-groups))))
 
 (defn fire-chat [bot message]
   (fire-handler bot :chat message))