-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathblocks.clj
executable file
·305 lines (252 loc) · 8.89 KB
/
blocks.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
(ns cljminecraft.blocks
(: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))
(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)))))
(defmacro defaction [name docstring ctx-binding params & method-body]
(let [params (map #(symbol (.getName (symbol %))) params)]
`(do
(defn ~name ~docstring [~@params]
(zipmap [:action ~@(map keyword params)] [~(keyword name) ~@params]))
(defmethod run-action ~(keyword name) [~ctx-binding {:keys [~@params]}]
~@method-body))))
(defaction move
"Move the current point in a direction"
{:keys [origin material painting?] :as ctx} [direction distance]
(let [[direction distance]
(if (neg? distance) ;; If we're negative, do the opposite thing.
[(opposite-face direction) (Math/abs distance)]
[direction distance])
d (find-relative-dir (:direction ctx) direction)
startblock (.getBlock origin)
m (i/get-material material)]
(when painting?
(doseq [i (range (or distance 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 distance 1))))))
(defn forward [& [x]]
(move :north x))
(defn back [& [x]]
(move :south x))
(defn left [& [x]]
(move :east x))
(defn right [& [x]]
(move :west x))
(defn up [& [x]]
(move :up x))
(defn down [& [x]]
(move :down x))
(defaction turn
"Turn the direction the current context is facing"
{:keys [direction] :as ctx} [relativedir]
(assoc ctx :direction (find-relative-dir direction relativedir)))
(defn turn-left []
(turn :east))
(defn turn-right []
(turn :west))
(defn turn-around []
(turn :south))
(defaction pen
"Do something with the 'pen', set whether it should paint as you move or not"
ctx [type]
(case type
:up (assoc ctx :painting? false)
:down (assoc ctx :painting? true)
:toggle (assoc ctx :painting? (not (:painting? ctx)))))
(defn pen-up []
(pen :up))
(defn pen-down []
(pen :down))
(defn pen-toggle []
(pen :toggle))
(defaction pen-from-mark
"Restore the pen state from mark"
ctx [mark]
(assoc :ctx :painting? (get-in ctx [:marks mark :painting?] true)))
(defaction material
"Set the current material to paint with"
ctx [material-key]
(assoc ctx :material material-key))
(defaction fork
"Run actions with ctx but don't update current ctx - effectively a subprocess"
ctx [actions]
(run-actions ctx actions)
ctx)
(defaction mark
"Stow away the state of a context into a given key"
{:keys [marks] :as ctx} [mark]
(assoc ctx :marks (assoc marks mark (dissoc ctx marks))))
(defn gen-mark []
(.toString (java.util.UUID/randomUUID)))
(defaction jump
"Jump your pointer to a given mark"
{:keys [marks] :as ctx} [mark]
(merge ctx (get marks mark {})))
(defaction copy
"copy a sphere of a given radius into a mark"
{:keys [marks origin] :as ctx} [mark radius]
(let [distance (* radius radius)
copy-blob
(doall
(for [x (range (- 0 radius) (inc radius))
y (range (- 0 radius) (inc radius))
z (range (- 0 radius) (inc radius))
:when (<= (+ (* x x) (* y y) (* z z)) distance)]
[x y z (.getData (.getState (.getRelative (.getBlock origin) x y z)))]))
m (get-in ctx [:marks mark] {})]
(assoc ctx :marks (assoc marks mark (assoc m :copy {:blob (doall copy-blob)})))))
(defaction cut
"Cut a sphere of a given radius into a mark"
ctx [mark radius]
(let [{:keys [origin material] :as ctx} (run-action ctx (copy mark radius))
mat (i/get-material material)
distance (* radius radius)]
(doseq [x (range (- 0 radius) (inc radius))
y (range (- 0 radius) (inc radius))
z (range (- 0 radius) (inc radius))
:when (<= (+ (* x x) (* y y) (* z z)) distance)]
(let [block (.getRelative (.getBlock origin) x y z)]
(.setTypeIdAndData block (.getItemTypeId mat) (.getData mat) false)))
ctx))
(defaction paste
"Paste a previously copied or cut block against a mark"
{:keys [origin] :as ctx} [mark]
(let [{:keys [blob]} (get-in ctx [:marks mark :copy] {})]
(doseq [[x y z data] blob]
(let [block (.getRelative (.getBlock origin) x y z)]
(.setTypeIdAndData block (.getItemTypeId data) (.getData data) false)))
ctx))
(defn location-to-point [origin point]
[(- (.getX point) (.getX origin))
(- (.getY point) (.getY origin))
(- (.getZ point) (.getZ origin))])
(defaction copy-to-mark
"Copy a block to a mark"
{:keys [origin marks] :as ctx} [mark]
(let [[px py pz] (location-to-point origin (:origin (get marks mark)))
copy-blob
(doall
(for [x (range (min px 0) (max px 0))
y (range (min py 0) (max py 0))
z (range (min pz 0) (max pz 0))]
[x y z (.getData (.getState (.getRelative (.getBlock origin) x y z)))]))
m (get-in ctx [:marks mark] {})]
(assoc ctx :marks (assoc marks mark (assoc m :copy {:blob (doall copy-blob)})))))
(defaction cut-to-mark
"Cut a block to a mark, replacing everything with a given material or air if not provided"
ctx [mark]
(let [{:keys [origin marks material] :as ctx} (run-action ctx (copy-to-mark mark))
mat (i/get-material material)
[px py pz] (location-to-point origin (:origin (get marks mark)))]
(doseq [x (range (min px 0) (max px 0))
y (range (min py 0) (max py 0))
z (range (min pz 0) (max pz 0))]
(let [block (.getRelative (.getBlock origin) x y z)]
(.setTypeIdAndData block (.getItemTypeId mat) (.getData mat) false)))
ctx))
(defaction clear-mark
"Clears a mark"
ctx [mark]
(update-in ctx [:marks mark] {}))
(defn calcline
"This returns a set of points for a line"
[xt yt zt]
(if (= [xt yt zt] [0 0 0])
'([0 0 0])
(let [q (max (Math/abs xt) (Math/abs yt) (Math/abs zt))
m (/ yt q)
n (/ zt q)
o (/ xt q)]
(for [qi (range q)]
[(Math/round (double (* o qi)))
(Math/round (double (* m qi)))
(Math/round (double (* n qi)))]))))
;; to be finished......
(defaction line-to-mark
"Draw a line directly to a given mark from current point"
{:keys [origin material marks] :as ctx} [mark]
(let [originblock (.getBlock origin)
mat (i/get-material material)
point (location-to-point origin (:origin (get marks mark)))
linepoints (apply calcline point)]
(doseq [[x y z] linepoints]
(let [block (.getRelative originblock x y z)]
(.setTypeIdAndData block (.getItemTypeId mat) (.getData mat) false)))
ctx))
(defn line
"Draw a line, relative to current position and direction"
[fwd lft u]
(let [m (gen-mark)]
[(mark m)
(pen :up)
(forward fwd)
(left lft)
(up u)
(pen :down)
(line-to-mark m)
(clear-mark m)]))
(defn extrude [direction x & actions]
(for [c (range x)]
(fork
{:action :move :direction direction :distance c}
actions)))
(defn setup-context [player-name]
{:origin (.getLocation (plr/get-player player-name))
:direction :north
:material :wool
:painting? true
:marks {}})
(comment
(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)
(extrude
:up 10
(forward 10) (right 10) (back 8) (left 2) (back 2) (left 8))
)
(run-actions
ctx
;(material :air)
(line 10 10 10)
(line 1 2 3)
(line -5 0 0)
(line 0 -5 0)
(line 0 0 -5))
(bk/ui-sync
@cljminecraft.core/clj-plugin
#(run-actions ctx (material :air) (mark :start) (left 100) (forward 100) (up 40) (cut-to-mark :start) (clear-mark :start))))