forked from fogleman/Craft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.py
148 lines (135 loc) · 4.9 KB
/
builder.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
# This file allows you to programmatically create blocks in Craft.
# Please use this wisely. Test on your own server first. Do not abuse it.
import socket
HOST = '127.0.0.1'
PORT = 4080
GRASS = 1
SAND = 2
STONE = 3
BRICK = 4
WOOD = 5
CEMENT = 6
DIRT = 7
PLANK = 8
SNOW = 9
GLASS = 10
COBBLE = 11
OFFSETS = [
(-0.5, -0.5, -0.5),
(-0.5, -0.5, 0.5),
(-0.5, 0.5, -0.5),
(-0.5, 0.5, 0.5),
(0.5, -0.5, -0.5),
(0.5, -0.5, 0.5),
(0.5, 0.5, -0.5),
(0.5, 0.5, 0.5),
]
def sphere(cx, cy, cz, r, fill=False, fx=False, fy=False, fz=False):
result = set()
for x in range(cx - r, cx + r + 1):
if fx and x != cx:
continue
for y in range(cy - r, cy + r + 1):
# if y < cy:
# continue # top hemisphere only
if fy and y != cy:
continue
for z in range(cz - r, cz + r + 1):
if fz and z != cz:
continue
inside = False
outside = fill
for dx, dy, dz in OFFSETS:
ox, oy, oz = x + dx, y + dy, z + dz
d2 = (ox - cx) ** 2 + (oy - cy) ** 2 + (oz - cz) ** 2
d = d2 ** 0.5
if d < r:
inside = True
else:
outside = True
if inside and outside:
result.add((x, y, z))
return result
def circle_x(cx, cy, cz, r, fill=False):
return sphere(cx, cy, cz, r, fill, fx=True)
def circle_y(cx, cy, cz, r, fill=False):
return sphere(cx, cy, cz, r, fill, fy=True)
def circle_z(cx, cy, cz, r, fill=False):
return sphere(cx, cy, cz, r, fill, fz=True)
def cuboid(x1, x2, y1, y2, z1, z2, fill=True):
result = set()
a = 0
a += x1 == x2
a += y1 == y2
a += z1 == z2
for x in range(x1, x2 + 1):
for y in range(y1, y2 + 1):
for z in range(z1, z2 + 1):
n = 0
n += x in (x1, x2)
n += y in (y1, y2)
n += z in (z1, z2)
if not fill and n <= a:
continue
result.add((x, y, z))
return result
def pyramid(y, x1, x2, z1, z2, fill=False):
result = set()
while x2 >= x1 and z2 >= z2:
result |= cuboid(x1, x2, y, y, z1, z2, fill)
y, x1, x2, z1, z2 = y + 1, x1 + 1, x2 - 1, z1 + 1, z2 - 1
return result
class Client(object):
def __init__(self, host=HOST, port=PORT):
self.conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.conn.connect((host, port))
def set_block(self, x, y, z, w):
self.conn.sendall('B,%d,%d,%d,%d\n' % (x, y, z, w))
def set_blocks(self, blocks, w):
key = lambda block: (block[1], block[0], block[2])
for x, y, z in sorted(blocks, key=key):
self.set_block(x, y, z, w)
def main():
client = Client()
set_blocks = client.set_blocks
# set_blocks(circle_y(0, 32, 0, 16, True), STONE)
# set_blocks(circle_y(0, 33, 0, 16), BRICK)
# set_blocks(cuboid(-1, 1, 1, 31, -1, 1), CEMENT)
# set_blocks(cuboid(-1024, 1024, 32, 32, -3, 3), STONE)
# set_blocks(cuboid(-3, 3, 32, 32, -1024, 1024), STONE)
# set_blocks(cuboid(-1024, 1024, 33, 33, -3, -3), BRICK)
# set_blocks(cuboid(-1024, 1024, 33, 33, 3, 3), BRICK)
# set_blocks(cuboid(-3, -3, 33, 33, -1024, 1024), BRICK)
# set_blocks(cuboid(3, 3, 33, 33, -1024, 1024), BRICK)
# set_blocks(sphere(0, 32, 0, 16), GLASS)
# for y in range(1, 32):
# set_blocks(circle_y(0, y, 0, 4, True), CEMENT)
# set_blocks(circle_x(16, 33, 0, 3), BRICK)
# set_blocks(circle_x(-16, 33, 0, 3), BRICK)
# set_blocks(circle_z(0, 33, 16, 3), BRICK)
# set_blocks(circle_z(0, 33, -16, 3), BRICK)
# for x in range(0, 1024, 32):
# set_blocks(cuboid(x - 1, x + 1, 31, 32, -1, 1), CEMENT)
# set_blocks(cuboid(-x - 1, -x + 1, 31, 32, -1, 1), CEMENT)
# set_blocks(cuboid(x, x, 1, 32, -1, 1), CEMENT)
# set_blocks(cuboid(-x, -x, 1, 32, -1, 1), CEMENT)
# for z in range(0, 1024, 32):
# set_blocks(cuboid(-1, 1, 31, 32, z - 1, z + 1), CEMENT)
# set_blocks(cuboid(-1, 1, 31, 32, -z - 1, -z + 1), CEMENT)
# set_blocks(cuboid(-1, 1, 1, 32, z, z), CEMENT)
# set_blocks(cuboid(-1, 1, 1, 32, -z, -z), CEMENT)
# for x in range(0, 1024, 8):
# set_blocks(set_block(x, 32, 0), CEMENT)
# set_blocks(set_block(-x, 32, 0), CEMENT)
# for z in range(0, 1024, 8):
# set_blocks(set_block(0, 32, z), CEMENT)
# set_blocks(set_block(0, 32, -z), CEMENT)
# set_blocks(pyramid(12, 32, 32+64-1, 32, 32+64-1), COBBLE)
# outer = circle_y(0, 11, 0, 176 + 3, True)
# inner = circle_y(0, 11, 0, 176 - 3, True)
# set_blocks(outer - inner, STONE)
# a = sphere(-32, 48, -32, 24, True)
# b = sphere(-24, 40, -24, 24, True)
# set_blocks(a - b, PLANK)
if __name__ == '__main__':
main()