Permalink
Browse files

Fill out the block drawing implementation

Tweak the get-materials to always return material-data and never just Material for consistency - possible API breaking change
some repl scratchpatching
  • Loading branch information...
1 parent af62393 commit 8d41bce4da418821ecb1490510fd0daadd720750 @CmdrDats committed Dec 18, 2012
Showing with 157 additions and 106 deletions.
  1. +8 −0 README.md
  2. +134 −95 src/cljminecraft/blocks.clj
  3. +5 −5 src/cljminecraft/items.clj
  4. +10 −6 src/cljminecraft/repl.clj
View
8 README.md
@@ -23,6 +23,14 @@ understand what changes are made and adjust your plugins accordingly.
Changelog:
+19 December 2012:
+ - get-material now always returns MaterialData and never Material for consistency
+ - Note that this could lead to API breaks
+ - Implement the first working version of the block drawing primitives
+ - Similar idea to Logo, forward x, turn-right, forward x, extrude :up x
+ - Playing with more things in the repl.clj scratchpatch
+ - Noticed that you can send a block change to a player without changing the world.. I'll look into building the functionality to setup a 'virtual' paint mode in the block drawing. Either for all players, a list of players or a single player. It would need to track the virtual paint unless cleared so that it can resend it to players as required.
+
17 December 2012:
- Implement entity searching function
- Drop item function
View
229 src/cljminecraft/blocks.clj
@@ -1,120 +1,159 @@
(ns cljminecraft.blocks
- (:require [cljminecraft.items :as i]))
-
-(defn turn-direction
- "Set the current facing direction, relative to the current direction (imagine current direction as north)"
- [blockface-direction])
-
-(defn move-direction
- "Move relative to the origin and current facing direction"
- [[x y z]]
- )
+ (:require [cljminecraft.logging :as log]
+ [cljminecraft.items :as i]
+ [cljminecraft.player :as plr]
+ [cljminecraft.bukkit :as bk]))
+
+(defn left-face [key]
+ ({:up :up, :down :down
+ :north :east, :east :south
+ :south :west, :west :north} key))
+
+(defn right-face [key]
+ ({:up :up, :down :down
+ :north :west, :west :south
+ :south :east, :east :north} key))
+
+(defn opposite-face [key]
+ ({:up :down, :down :up
+ :north :south, :south :north
+ :east :west, :west :east} key))
+
+(defn find-relative-dir [d r]
+ ({:north d :south (opposite-face d) :east (left-face d) :west (right-face d) :up :up :down :down} r))
+
+(defn move [{:keys [origin direction material painting?] :as ctx} & [relativedir x]]
+ (let [d (find-relative-dir direction relativedir)
+ startblock (.getBlock origin)
+ m (i/get-material material)]
+ (when painting?
+ (doseq [i (range (or x 1))]
+ (doto (.getRelative startblock (get i/blockfaces d) i)
+ (.setData 0)
+ (.setType (.getItemType m))
+ (.setData (.getData m)))))
+ (assoc ctx :origin (.getLocation (.getRelative startblock (get i/blockfaces d) (or x 1))))))
+
+(defn turn [{:keys [direction] :as ctx} & [relativedir]]
+ (assoc ctx :direction (find-relative-dir direction relativedir)))
+
+(defmulti run-action (fn [ctx a] (:action a)))
+(defn run-actions [ctx & actions]
+ (loop [a (first actions)
+ r (rest actions)
+ context ctx]
+ (cond
+ (nil? a) context
+ (and (coll? a) (not (map? a))) (recur (first a) (concat (rest a) r) context)
+ :else
+ (recur (first r) (rest r) (run-action context a)))))
+
+(defmethod run-action :move [ctx {:keys [direction distance]}]
+ (move ctx direction distance))
+
+(defn forward [& [x]]
+ {:action :move :direction :north :distance x})
+
+(defn back [& [x]]
+ {:action :move :direction :south :distance x})
+
+(defn left [& [x]]
+ {:action :move :direction :east :distance x})
+
+(defn right [& [x]]
+ {:action :move :direction :west :distance x})
(defn up [& [x]]
- (move-direction [0 (- 0 (or x 1)) 0]))
+ {:action :move :direction :up :distance x})
(defn down [& [x]]
- (move-direction [0 (or x 1) 0]))
-
-(defn left [x]
- (move-direction [(- 0 (or x 1)) 0 0]))
-
-(defn right [x]
- (move-direction [(or x 1) 0 0]))
+ {:action :move :direction :down :distance x})
-(defn forward [x]
- (move-direction [0 0 (or x 1)]))
-(defn back [x]
- (move-direction [0 0 (- 0 (or x 1))]))
+(defmethod run-action :turn [ctx {:keys [direction]}]
+ (turn ctx direction))
(defn turn-left []
- (turn-direction :east))
+ {:action :turn :direction :east})
(defn turn-right []
- (turn-direction :west))
+ {:action :turn :direction :west})
-(defn turn-up []
- (turn-direction :up))
+(defn turn-around []
+ {:action :turn :direction :south})
-(defn turn-down []
- (turn-direction :down))
+(defmethod run-action :pen [ctx {:keys [type]}]
+ (case type
+ :up (assoc ctx :painting? false)
+ :down (assoc ctx :painting? true)
+ :toggle (assoc ctx :painting? (not (:painting? ctx)))))
-(defn turn-around []
- (turn-direction :south))
+(defn pen-up []
+ {:action :pen :type :up})
+
+(defn pen-down []
+ {:action :pen :type :down})
-(defn material
- "Set the current 'painted' material :none, or nil for 'pen up', ie - no effect."
- [])
+(defn pen-toggle []
+ {:action :pen :type :toggle})
-(defn penup [])
+(defmethod run-action :material [ctx {:keys [matkey]}]
+ (assoc ctx :material matkey))
-(defn pendown [])
+(defn material [material-key]
+ {:action :material :matkey material-key})
-(defn fork
- "Run the given actions seperate to the main context. This could run parallel to your main process, but it generally means that your position is left unchanged"
- [& actions])
+(defmethod run-action :mark [{:keys [marks origin direction] :as ctx} {:keys [uuid]}]
+ (assoc ctx :marks (assoc marks uuid (dissoc ctx marks))))
-(defn extend-actions
- "For the to-action, do the extended action after every movement in the to-action.."
- [extended-actions & to-actions]
- (for [to-action to-actions]
- [to-action
- (fork extended-action)]))
+(defmethod run-action :jump [{:keys [marks] :as ctx} {:keys [uuid clear]}]
+ (let [mark (get marks uuid {})]
+ (merge (if clear (update-in ctx [:marks] dissoc uuid) ctx) mark)))
-(defn test-fn
- "Test an expression, given the current context - executing actions in the true of false block as required"
- [fn true-block & [false-block]])
+(defn gen-mark []
+ (.toString (java.util.UUID/randomUUID)))
-(defn if-material
- "Decision branch depending on material"
- [material-key true-block & [false-block]])
+(defn mark [m]
+ {:action :mark :uuid m})
-(defn run-actions
- [origin direction])
+(defn jump [m & [clear-mark]]
+ {:action :jump :uuid m :clear clear-mark})
+(defn extrude [direction x & actions]
+ (let [m (gen-mark)]
+ (for [c (range x)]
+ [(mark m)
+ actions
+ (jump m true)
+ {:action :move :direction direction :distance 1}]
+ )))
+
+(defn setup-context [player-name]
+ {:origin (.getLocation (plr/get-player player-name))
+ :direction :north
+ :material :wool
+ :painting? true
+ :marks {}})
(comment
- "To define a house:"
- (defn wall [length height]
- [(fork
- (extend-actions
- [(forward length)]
- (for [_ (range heigth)] (up))))
- (penup)
- (forward length)
- ])
-
- (defn house-walls [width length height]
- [(wall width height)
- (turn-right)
- (wall length height)
- (turn-right)
- (wall width height)
- (turn-right)])
-
- (extend-actions
- [(forward width)
- (right length)
- (back width)
- (left length)]
- (up height))
-
- (let [marker (generate-marker)]
- (mark marker)
- (foward width)
- (right length)
- (up height)
- (fill-to-mark marker :air)
- (extend-actions
- [(forward width)
- (right length)
- (back width)
- (left length)]
- (up height))))
-
-
-
-(let [shuffled (shuffle players)]
- (dosomething)
- (doelsfklds))
+ (def ctx (setup-context (first (.getOnlinePlayers (bk/server)))))
+
+ (defn floor-part []
+ [(forward 5) (turn-right) (forward 1) (turn-right) (forward 5) (turn-left) (forward 1) (turn-left)])
+
+ (defn floor []
+ [(floor-part) (floor-part) (floor-part) (floor-part) (floor-part) (floor-part) (floor-part) (floor-part)])
+
+
+ (run-actions ctx
+ (material :air)
+ (floor) (turn-around) (up) (floor))
+
+ (run-actions
+ ctx
+ (material :air)
+ (floor)
+ (extrude
+ :up 10
+ (forward 10) (right 10) (back 8) (left 2) (back 2) (left 8))
+ (floor)))
View
10 src/cljminecraft/items.clj
@@ -1,5 +1,6 @@
(ns cljminecraft.items
- (:require [cljminecraft.util :as util])
+ (:require [cljminecraft.util :as util]
+ [cljminecraft.logging :as log])
(:require [cljminecraft.entity :as ent])
(:import [org.bukkit TreeSpecies Material])
(:import [org.bukkit.material
@@ -213,13 +214,12 @@
(if (number? val)
(.getNewData material val)
(get-new-data (.getData material) material (rest material-key))))
- (get materials material-key)))
+ (let [material (get materials material-key)]
+ (get-new-data (.getData material) material []))))
(defn item-stack [material-key & [qty]]
(let [material (get-material material-key)]
- (if (instance? MaterialData material)
- (.toItemStack (or qty 1))
- (ItemStack. material (or qty 1)))))
+ (.toItemStack material (or qty 1))))
(defn drop-item [location itemstack & [naturally?]]
(if naturally?
View
16 src/cljminecraft/repl.clj
@@ -36,12 +36,16 @@
(defn ondie [ev]
(.setDeathMessage ev "You fool!"))
+(let [m (i/get-material [:wool :yellow])]
+ (doseq [x (range 10)
+ y (range 10)]
+ (let [l (.getLocation (plr/get-player "CmdrDats"))]
+ (.setY l (- (.getY l) 1))
+ (.setX l (- (.getX l) x))
+ (.setZ l (- (.getZ l) y))
+ (if (not= (.getType (.getBlock l)) (i/get-material :air))
+ (.sendBlockChange (plr/get-player "DatsGuest") l (.getItemType m) (.getData m))))))
+
(ev/register-event @plugin "player.player-drop-item" #'drop-dirt)
(ev/register-event @plugin "player.player-interact" #'interact)
(ev/register-event @plugin "entity.player-death" #'ondie)
-
-
-
-
-
-

0 comments on commit 8d41bce

Please sign in to comment.