# HG changeset patch # User Steve Losh # Date 1312953924 14400 # Node ID 43f02bd6b1977e1f3c5ef4bb6f559f1fceb9d6a5 # Parent 2e42d7f29dde1843788e390c8588b50ef5186659 Make handlers return lists. diff -r 2e42d7f29dde -r 43f02bd6b197 src/clojurecraft/actions.clj --- 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)))))) diff -r 2e42d7f29dde -r 43f02bd6b197 src/clojurecraft/core.clj --- 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))) diff -r 2e42d7f29dde -r 43f02bd6b197 src/clojurecraft/data.clj --- 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]) diff -r 2e42d7f29dde -r 43f02bd6b197 src/clojurecraft/events.clj --- 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))