# HG changeset patch # User Steve Losh # Date 1314396131 14400 # Node ID 1e03d5cc372b00aedc77caf90141c2209bdfc7e2 # Parent 98dc76fd10322b34a03946d7df6e9de5ae899441 Refactor the loop stuff. diff -r 98dc76fd1032 -r 1e03d5cc372b src/clojurecraft/actions.clj --- a/src/clojurecraft/actions.clj Tue Aug 23 20:51:39 2011 -0400 +++ b/src/clojurecraft/actions.clj Fri Aug 26 18:02:11 2011 -0400 @@ -6,22 +6,24 @@ (defn move [bot x-change y-change z-change] (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})))))) + (when (:loc @player) + (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] (delay (let [player (:player bot)] - (dosync - (let [location (:loc @player)] - (alter player assoc-in [:loc :onground] false) - (alter player assoc :velocity physics/JUMP-VELOCITY)))))) + (when (:loc @player) + (dosync + (let [location (:loc @player)] + (alter player assoc-in [:loc :onground] false) + (alter player assoc :velocity physics/JUMP-VELOCITY))))))) (defn chat [bot message] (delay diff -r 98dc76fd1032 -r 1e03d5cc372b src/clojurecraft/core.clj --- a/src/clojurecraft/core.clj Tue Aug 23 20:51:39 2011 -0400 +++ b/src/clojurecraft/core.clj Fri Aug 26 18:02:11 2011 -0400 @@ -113,8 +113,8 @@ outqueue ^LinkedBlockingQueue (LinkedBlockingQueue.) actionqueue (LinkedBlockingQueue.) world (get-world server) - bot (Bot. conn username outqueue actionqueue nil world (ref {}) - (atom {}) (atom {}))] + bot (Bot. conn username outqueue actionqueue nil world + (ref {}) (ref {}) (atom {}) (atom {}))] (println "connecting") diff -r 98dc76fd1032 -r 1e03d5cc372b src/clojurecraft/data.clj --- a/src/clojurecraft/data.clj Tue Aug 23 20:51:39 2011 -0400 +++ b/src/clojurecraft/data.clj Fri Aug 26 18:02:11 2011 -0400 @@ -109,5 +109,5 @@ ; packet-counts-in -> (atom {:packet-type integer}) ; packet-counts-out -> (atom {:packet-type integer}) (defrecord Bot [connection username outqueue actionqueue player world - event-handlers packet-counts-in packet-counts-out]) + event-handlers loops packet-counts-in packet-counts-out]) diff -r 98dc76fd1032 -r 1e03d5cc372b src/clojurecraft/events.clj --- a/src/clojurecraft/events.clj Tue Aug 23 20:51:39 2011 -0400 +++ b/src/clojurecraft/events.clj Fri Aug 26 18:02:11 2011 -0400 @@ -1,5 +1,26 @@ (ns clojurecraft.events) +; Action Performing ---------------------------------------------------------------- +(defn- handle-action-group [bot action-group] + (println action-group) + (let [queue-action #(.put (:actionqueue bot) %) + queue-action-group #(dorun (map queue-action %))] + (queue-action-group action-group))) + + +; Event Handlers ------------------------------------------------------------------- +(defn- fire-handler [bot event-type & args] + (let [action-groups (map #(apply (deref %) (into [bot] args)) + (event-type @(:event-handlers bot)))] + (dorun (map handle-action-group (cycle [bot]) action-groups)))) + +(defn fire-chat [bot message] + (fire-handler bot :chat message)) + +(defn fire-dead [bot] + (fire-handler bot :dead)) + + (defn add-handler [bot event-type handler] (dosync (let [current-handlers (event-type @(:event-handlers bot)) @@ -10,29 +31,18 @@ (dosync (alter (:event-handlers bot) dissoc event-type))) -(defn- handle-action-group [bot action-group] - (println action-group) - (let [queue-action #(.put (:actionqueue bot) %) - queue-action-group #(dorun (map queue-action %))] - (queue-action-group action-group))) - -(defn- fire-handler [bot event-type & args] - (let [action-groups (map #(apply (deref %) (into [bot] args)) - (event-type @(:event-handlers bot)))] - (dorun (map handle-action-group (cycle [bot]) action-groups)))) +; Loops ---------------------------------------------------------------------------- +(defn- run-loop [bot function sleep-ms loop-id] + (while (and (nil? (:exit @(:connection bot))) + (@(:loops bot) loop-id)) + (handle-action-group bot ((deref function) bot)) + (Thread/sleep sleep-ms)) + (dosync (alter (:loops bot) dissoc loop-id))) -(defn fire-chat [bot message] - (fire-handler bot :chat message)) - -(defn fire-dead [bot] - (fire-handler bot :dead)) - +(defn remove-loop [bot loop-id] + (dosync (alter (:loops bot) assoc loop-id nil))) -(defn- run-loop [bot function sleep-ms] - (while (nil? (:exit @(:connection bot))) - (handle-action-group bot (function bot)) - (Thread/sleep sleep-ms))) - -(defn add-loop [bot function sleep-ms] - (.start (Thread. #(run-loop bot function sleep-ms)))) +(defn add-loop [bot function sleep-ms loop-id] + (dosync (alter (:loops bot) assoc loop-id :running)) + (.start (Thread. #(run-loop bot function sleep-ms loop-id)))) diff -r 98dc76fd1032 -r 1e03d5cc372b src/examples/jumpbot.clj --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/examples/jumpbot.clj Fri Aug 26 18:02:11 2011 -0400 @@ -0,0 +1,19 @@ +(ns examples.jumpbot + (:require [clojurecraft.core :as core]) + (:require [clojurecraft.events :as events]) + (:require [clojurecraft.actions :as actions])) + +(defn jump [bot] + [(actions/jump bot)]) + +(defn handle-dead [bot] + [(actions/chat bot "WHY DO YOU NOT WANT ME TO JUMP?!") + (actions/respawn bot)]) + + +(defn make-jumpbot [server username] + (let [bot (core/connect server username)] + (events/add-handler bot :dead #'handle-dead) + (events/add-loop bot #'jump 3000 :jump-loop) + bot)) +