-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcursors.py
364 lines (337 loc) · 13.9 KB
/
cursors.py
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# This library is meant to be used with Minecraft Python.
# It gives you a nice little cursor abstraction to draw with.
# Use it like this:
#
# from mcpi.minecraft import Minecraft
# from cursors import *
# from blocks import *
# mc=Minecraft.create('192.168.1.1')
# minecraft_connection(mc)
#
# (replace 192.168.1.1 with your RaspberryJuice-enabled Minecraft server's
# IP address)
# Then for example to draw a square:
#
# square = cursor_here().square(3)
# trace(square, block=stone)
# If you see this error:
# AttributeError: 'NoneType' object has no attribute 'player'
# Double-check you didn't forget to call minecraft_connection.
import time
# This will hold the connection to Minecraft.
# Update it by calling minecraft_connection.
mc = None
class Stencil:
"""A bunch of blocks, copy/pasted from the game."""
def __init__(self, rectangle_cursor, height, blocks):
self.rectangle_cursor = rectangle_cursor
self.height = height
self.blocks = blocks
def __repr__(self):
return 'Stencil({}, {}, {})'.format(
self.rectangle_cursor.__repr__(),
self.height,
self.blocks)
def move(self, dx, dy, dz):
"""Move the draw area, leaving its size unchanged."""
return Stencil(self.rectangle_cursor.move(dx, dy, dz), self.height, self.blocks)
def boundary(self, height=None):
if height: raise ValueError('Cannot specify height when tracing Stencil.')
return self.rectangle_cursor.boundary(height=self.height)
def interior(self, height=None):
if height: raise ValueError('Cannot specify height when filling Stencil.')
return self.rectangle_cursor.interior(height=self.height)
def paste(self):
"""Paste the blocks at the current position."""
index = 0;
for j in range(self.rectangle_cursor.y, self.rectangle_cursor.y + self.height):
for i in range(self.rectangle_cursor.x, self.rectangle_cursor.x + self.rectangle_cursor.x_width):
for k in range(self.rectangle_cursor.z, self.rectangle_cursor.z + self.rectangle_cursor.z_depth):
mc.setBlock(i, j, k, self.blocks[index])
index += 1
def cursor(self):
"""The underlying cursor."""
return self.rectangle_cursor
class PointCursor:
def __init__(self, x, y ,z):
self.x = x
self.y = y
self.z = z
def __repr__(self):
return 'PointCursor({},{},{})'.format(self.x, self.y, self.z)
def move(self, dx, dy, dz):
"""Move the point by this much."""
return PointCursor(self.x+dx, self.y+dy, self.z+dz)
def grow(self, dx, dz):
"""Grow by this amount in both directions."""
return RectangleCursor(self.x, self.y, self.z, 1, 1).grow(dx, dz)
def rectangle(self, width, depth):
"""RectangleCursor of the specified size, centered on this cursor."""
return RectangleCursor(self.x-width/2, self.y, self.z-depth/2, width, depth)
def square(self, side):
return self.rectangle(side, side)
def circle(self, radius):
return CircleCursor(self.x, self.y, self.z, radius)
def center(self):
"""The center point, so here it's the point itself of course."""
return self
def boundary(self, height=1):
if not height: height=1
return [(self.x, self.y+h, self.z) for h in range(height)]
def interior(self):
return self.boundary();
def bounding_rectangle(self, other=None):
"""RectangleCursor that contains this cursor (and 'other', if specified)."""
return self.rectangle(1,1).bounding_rectangle(other)
class CursorList:
def __init__(self, cursors):
# Make a copy of the list.
self.cursors = cursors[:]
def __repr__(self):
return 'CursorList[{}])'.format(len(self.cursors))
def __len__(self):
return len(self.cursors)
def move(self, dx, dy, dz):
moved_cursors = [c.move(dx, dy, dz) for c in self.cursors]
return CursorList(moved_cursors)
def each(self, function):
return CursorList([function(cursor) for cursor in self.cursors])
def rectangle(self, width, depth):
return self.each(lambda cursor: cursor.rectangle(width, depth))
def square(self, side):
return self.each(lambda cursor: cursor.square(side))
def center(self):
"""The center point, computed as center of the bounding box."""
return self.bounding_rectangle().center()
def grow(self, dx, dz):
return self.each(lambda cursor: cursor.grow(dx, dz))
def shrink(self, dx, dz):
return self.each(lambda cursor: cursor.shrink(dx, dz))
def boundary(self, height=1):
if not height: height=1
for cursor in self.cursors:
for xyz in cursor.boundary(height=height): yield xyz
def interior(self):
for cursor in self.cursors:
for xyz in cursor.interior(): yield xyz
def bounding_rectangle(self, other=None):
"""RectangleCursor that contains this cursor (and 'other', if specified)."""
for cursor in self.cursors:
other = cursor.bounding_rectangle(other)
return other
class RectangleCursor:
def __init__(self, x, y, z, x_width, z_depth):
"""(x,y,z) is lowest-coordinate point, we add width and depth to that."""
self.x = x
self.y = y
self.z = z
if x_width<1: x_width=1
if z_depth<1: z_depth=1
self.x_width = x_width
self.z_depth = z_depth
def __repr__(self):
return 'RectangleCursor({},{},{},{},{})'.format(self.x, self.y, self.z, self.x_width, self.z_depth)
def move(self, dx, dy, dz):
"""Move the rectangle, leaving its size unchanged."""
return RectangleCursor(self.x+dx, self.y+dy, self.z+dz, self.x_width, self.z_depth)
def shrink(self, dx, dz):
"""Shrink all sides by the specified amounts (negatives ok)."""
if dx==dz==0: return self
if self.x_width - 2*dx < 1:
dx = (self.x_width)//2
if self.z_depth - 2*dz < 1:
dz = (self.z_depth)//2
return RectangleCursor(self.x+dx, self.y, self.z+dz, max(1,self.x_width-2*dx), max(1,self.z_depth-2*dz))
def grow(self, dx, dz):
"""Grow all sides by the specified amounts (negatives ok)."""
return self.shrink(-dx, -dz)
def center(self):
"""The center point (as a cursor)."""
return PointCursor(self.x+self.x_width/2, self.y, self.z+self.z_depth/2)
def corners(self):
"""Corners. 4 for a proper rectangle, 2 for a line, 1 for a point."""
corner_coords = [
(self.x, self.y, self.z)
]
if self.x_width > 1:
corner_coords.append(
(self.x + self.x_width - 1, self.y, self.z) )
if self.z_depth > 1:
corner_coords.append(
(self.x + self.x_width - 1, self.y, self.z + self.z_depth - 1))
if self.z_depth > 1:
corner_coords.append(
(self.x, self.y, self.z + self.z_depth - 1))
return CursorList([PointCursor(x,y,z) for x,y,z in corner_coords])
def side(self, dx, dz):
"""Given a direction, get that side of the rectangle (a line, returned as a RectangleCursor)."""
if (dx==dz==0): return self.center()
if (dx and dz): raise ValueError('Direction cannot be diagonal.')
width = self.x_width if dx==0 else 1
depth = self.z_depth if dz==0 else 1
start_x = self.x
if dx>0: start_x = self.x + self.x_width - 1
start_z = self.z
if dz>0: start_z = self.z + self.z_depth - 1
return RectangleCursor(start_x, self.y, start_z, width, depth)
def boundary(self, height=1):
"""All the coordinates on the boundary."""
if not height: height=1
for i in range(self.x, self.x + self.x_width):
yield (i, self.y, self.z)
for j in range(self.z, self.z + self.z_depth):
yield (self.x + self.x_width - 1, self.y, j)
if self.z_depth>1:
for i in range(0, self.x_width):
yield (self.x + self.x_width - i - 1, self.y, self.z + self.z_depth - 1)
if self.x_width>1:
for j in range(0, self.z_depth):
yield (self.x, self.y, self.z + self.z_depth - j - 1)
if height>1:
for xyz in self.corners().move(0,1,0).boundary(height-1):
yield xyz
def interior(self):
"""All the coordinates on the boundary or inside it."""
for i in range(self.x, self.x + self.x_width):
for k in range(self.z, self.z + self.z_depth):
yield (i, self.y, k)
def bounding_rectangle(self, other=None):
"""RectangleCursor that contains this cursor and 'other' (if specified)."""
if not other: return self
other = other.bounding_rectangle()
x1 = min(self.x, other.x)
y1 = min(sely.y, other.y)
z1 = min(self.z, other.z)
x2 = max(self.x + self.x_width, other.x + other.x_width)
y2 = max(self.y, other.y)
z2 = max(self.z + self.z_depth, other.z + other.z_depth)
return RectangleCursor(x1,y1,z1, (x2-x1), (z2-z1))
def copy(self, height):
"""Copy the blocks inside the selected rectangular volume."""
blocks = mc.getBlocks(self.x, self.y, self.z, self.x+self.x_width-1, self.y+height-1, self.z+self.z_depth-1)
return Blocks(self, height, blocks)
class CircleCursor:
def __init__(self, x, y, z, radius):
"""(x,y,z) is the center point."""
self.x = x
self.y = y
self.z = z
self.radius = radius
def move(self, dx, dy, dz):
self.x += dx
self.y += dy
self.z += dz
def grow(self, dr):
ret = CircleCursor(self.x, self.y, self.z, self.radius)
ret.radius += dr
if ret.radius<1: ret.radius=1
return ret
def shrink(self, dr):
return self.grow(-dr)
def center(self):
return PointCursor(self.x, self.y, self.z)
def interior(self):
"""Lists coordinates at the edge or inside of the circle."""
for x in range(0, self.radius+1):
for z in range(0, self.radius+1):
dist = sqr(x) + sqr(z)
if (dist<sqr(self.radius)):
yield (self.x + x, self.y, self.z + z)
if x>0:
yield (self.x - x, self.y, self.z + z)
if z>0:
yield (self.x + x, self.y, self.z - z)
if x>0 and z>0:
yield (self.x - x, self.y, self.z - z)
def boundary(self, height=1):
"""Lists coordinates at the edge of the circle."""
if not height: height=1
coords = set(self.interior())
for (x,y,z) in coords:
if ((x+1,y,z) not in coords or
(x-1,y,z) not in coords or
(x,y,z+1) not in coords or
(x,y,z-1) not in coords):
for h in range(height): yield (x,y+h,z)
def bounding_rectangle(self, other=None):
"""RectangleCursor that contains the circle (and 'other', if specified)."""
rectangle = RectangleCursor(self.x-self.radius+1, self.y, self.z-self.radius+1,
self.radius*2-1, self.radius*2-1)
return rectangle.bounding_rectangle(other)
def minecraft_connection(new_mc):
"""Pass the Minecraft connection to enable cursor_here() and related."""
global mc
mc = new_mc
def rounded_pos(xyz):
x0,y0,z0 = xyz
return int(x0), int(y0), int(z0)
def sqr(n):
return n*n
def rounded_player_pos():
"""Current player position, as whole numbers."""
return rounded_pos(mc.player.getPos())
def put_here(block=112):
"""Put a block of this type next to the player."""
x,y,z = rounded_player_pos()
mc.setBlock(x,y,z,block)
def cursor_here():
global mc
x,y,z = rounded_pos(mc.player.getPos())
return PointCursor(x,y,z)
def cursor_nearby(block=112):
"""Find the nearest block of this type and return a cursor there."""
global mc
x,y,z = rounded_pos(mc.player.getPos())
best_dist=999
best_cur=None
size=4
# This is much faster than calling getBlock repeatedly.
blocks = mc.getBlocks(x-size, y-size, z-size, x+size-1, y+size-1, z+size-1)
index = 0
for j in range(y-size, y+size):
for i in range(x-size, x+size):
for k in range(z-size, z+size):
b = blocks[index]
index += 1
if b != block: continue
dist = sqr(i-x) + sqr(j-y) + sqr(k-z)
if dist > best_dist: continue
best_dist = dist
best_cur = PointCursor(i,j,k)
return best_cur
def trace(cursor, block=1, height=1, stride=1):
"""Draw on the outline of the cursor."""
count=0
for (x,y,z) in cursor.boundary():
count += 1
if (count % stride) != 0: continue
for h in range(height):
mc.setBlock(x,y+h,z, block)
def fill(cursor, block=1, height=1):
"""Fill the area of the cursor and inside it."""
for (x,y,z) in cursor.interior():
for h in range(height):
mc.setBlock(x,y+h,z, block)
def fill_except(cursor, height, cursor_except, height_except, block=1):
"""Fill the area that is inside the cursor but not inside cursor_except."""
places = {
(x,y+h,z): block for (x,y,z) in cursor.interior() for h in range(height)
}
for (x,y,z) in cursor_except.interior():
for h in range(height_except):
if (x,y+h,z) in places:
del places[(x,y+h,z)]
for (x,y,z),b in places.items():
mc.setBlock(x,y,z,b)
def blink(cursor, height=0, block=41):
"""Show the cursor, then hide it again."""
positions = list(cursor.boundary(height=height))
old_values = [mc.getBlock(x,y,z) for x,y,z in positions]
for loop in range(3):
for x,y,z in positions:
mc.setBlock(x,y,z,block)
time.sleep(1)
for i in range(len(positions)):
x,y,z = positions[i]
mc.setBlock(x,y,z,old_values[i])
time.sleep(1)