Change actions to return maps, and add a perform function.
author |
Steve Losh <steve@stevelosh.com> |
date |
Fri, 26 Aug 2011 19:11:44 -0400 |
parents |
4c1055d72377
|
children |
4693c1d0c903
|
branches/tags |
(none) |
files |
src/clojurecraft/actions.clj src/clojurecraft/core.clj |
Changes
--- a/src/clojurecraft/actions.clj Fri Aug 26 18:40:45 2011 -0400
+++ b/src/clojurecraft/actions.clj Fri Aug 26 19:11:44 2011 -0400
@@ -9,72 +9,98 @@
(queue-action-group action-group)))
+; Performers -----------------------------------------------------------------------
+(defn- move! [bot x-change y-change z-change]
+ (let [player (:player bot)]
+ (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]
+ (let [player (:player bot)]
+ (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]
+ (write-packet bot :chat {:message message}))
+
+(defn- turn-to! [bot yaw]
+ (let [player (:player bot)]
+ (dosync
+ (let [location (:loc @player)
+ new-location (assoc location :yaw yaw)]
+ (alter player assoc :loc new-location)))))
+
+(defn- look-to! [bot pitch]
+ (let [player (:player bot)]
+ (dosync
+ (let [location (:loc @player)
+ new-location (assoc location :pitch pitch)]
+ (alter player assoc :loc new-location)))))
+
+(defn- respawn! [bot]
+ (write-packet bot :respawn {:world 0})) ; Always respawn in the normal (non-Nether) world for now.
+
+
+(defn perform [action]
+ (let [performer ({:move move!
+ :jump jump!
+ :chat chat!
+ :turn-to turn-to!
+ :look-to look-to!
+ :respawn respawn!} (:action action))]
+ (apply performer (:bot action) (:args action))))
+
+
+; Public Actions -------------------------------------------------------------------
(defn move [bot x-change y-change z-change]
- (delay
- (let [player (:player bot)]
- (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})))))))
+ {:bot bot :action :move :args [x-change y-change z-change]})
(defn jump [bot]
- (delay
- (let [player (:player bot)]
- (when (:loc @player)
- (dosync
- (let [location (:loc @player)]
- (alter player assoc-in [:loc :onground] false)
- (alter player assoc :velocity physics/JUMP-VELOCITY)))))))
+ {:bot bot :action :jump :args []})
(defn chat [bot message]
- (delay
- (write-packet bot :chat {:message message})))
+ {:bot bot :action :chat :args [message]})
(defn turn-to [bot yaw]
- (delay
- (let [player (:player bot)]
- (dosync
- (let [location (:loc @player)
- new-location (assoc location :yaw yaw)]
- (alter player assoc :loc new-location))))))
+ {:bot bot :action :turn-to :args [yaw]})
(defn turn-north [bot]
- (turn-to bot 90.0))
+ {:bot bot :action :turn-to :args [90.0]})
(defn turn-south [bot]
- (turn-to bot 270.0))
+ {:bot bot :action :turn-to :args [270.0]})
(defn turn-east [bot]
- (turn-to bot 180.0))
+ {:bot bot :action :turn-to :args [180.0]})
(defn turn-west [bot]
- (turn-to bot 0.0))
+ {:bot bot :action :turn-to :args [0.0]})
(defn look-to [bot pitch]
- (delay
- (let [player (:player bot)]
- (dosync
- (let [location (:loc @player)
- new-location (assoc location :pitch pitch)]
- (alter player assoc :loc new-location))))))
+ {:bot bot :action :look-to :args [pitch]})
(defn look-up [bot]
- (look-to bot -90.0))
+ {:bot bot :action :look-to :args [-90.0]})
-(defn look-down[bot]
- (look-to bot 90.0))
+(defn look-down [bot]
+ {:bot bot :action :look-to :args [-90.0]})
(defn look-straight [bot]
- (look-to bot 0.0))
+ {:bot bot :action :look-to :args [-0.0]})
(defn respawn [bot]
- (delay
- (write-packet bot :respawn {:world 0}))) ; Always respawn in the normal (non-Nether) world for now.
+ {:bot bot :action :respawn :args []})
+
--- a/src/clojurecraft/core.clj Fri Aug 26 18:40:45 2011 -0400
+++ b/src/clojurecraft/core.clj Fri Aug 26 19:11:44 2011 -0400
@@ -6,7 +6,7 @@
(:use [clojure.contrib.pprint :only (pprint)])
(:require [clojurecraft.chunks :as chunks])
(:require [clojurecraft.physics :as physics])
- (:require [clojurecraft.actions :as act])
+ (:require [clojurecraft.actions :as actions])
(:require (clojurecraft.data))
(:import [clojurecraft.data Location Entity Block Chunk World Bot])
(:import (java.net Socket)
@@ -100,7 +100,7 @@
(while (nil? (:exit @conn))
(let [action (.poll actionqueue 1 TimeUnit/SECONDS)]
(when action
- (force action)))))
+ (actions/perform action)))))
(println "done - action handler")
(println "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"))