-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmapmakingtools.py
More file actions
314 lines (284 loc) · 11.6 KB
/
mapmakingtools.py
File metadata and controls
314 lines (284 loc) · 11.6 KB
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
from commands import add, admin
from pyspades.contained import BlockAction, SetColor
from pyspades.constants import *
from math import *
EAST = 0
SOUTH = 1
WEST = 2
NORTH = 3
def make_color(r, g, b, a):
return b | (g << 8) | (r << 16) | (int((a / 255.0) * 128) << 24)
def make_color_tuple(color):
return make_color(color[0], color[1], color[2], 255)
def get_color_tuple(color):
b = color & 0xFF
g = (color & 0xFF00) >> 8
r = (color & 0xFF0000) >> 16
a = int((((color & 0xFF000000) >> 24) / 128.0) * 255)
return (r, g, b, a)
def set_color(prt, color, player_id = 32):
c = SetColor()
c.player_id = player_id
c.value = color
prt.send_contained(c)
def add_block(prt, x, y, z, color, player_id = 32, mirror_x = False, mirror_y = False):
if x >= 0 and x < 512 and y >= 0 and y < 512 and z >= 0 and z < 64:
if mirror_x == True or mirror_y == True:
x2 = x
y2 = y
if mirror_x == True:
x2 = 511 - x
if mirror_y == True:
y2 = 511 - y
add_block(prt, x2, y2, z, color, player_id, False, False)
if not prt.map.get_solid(x, y, z):
block_action = BlockAction()
block_action.player_id = player_id
block_action.value = BUILD_BLOCK
block_action.x = x
block_action.y = y
block_action.z = z
prt.send_contained(block_action)
prt.map.set_point(x, y, z, get_color_tuple(color))
def remove_block(prt, x, y, z, mirror_x = False, mirror_y = False):
if x >= 0 and x < 512 and y >= 0 and y < 512 and z >= 0 and z < 64:
if mirror_x == True or mirror_y == True:
x2 = x
y2 = y
if mirror_x == True:
x2 = 511 - x
if mirror_y == True:
y2 = 511 - y
remove_block(prt, x2, y2, z, False, False)
if prt.map.get_solid(x, y, z):
block_action = BlockAction()
block_action.player_id = 32
block_action.value = DESTROY_BLOCK
block_action.x = x
block_action.y = y
block_action.z = z
prt.map.remove_point(x, y, z)
prt.send_contained(block_action)
return True
return False
def mirror(connection, mirror_x, mirror_y):
connection.mirror_x = bool(mirror_x)
connection.mirror_y = bool(mirror_y)
add(mirror)
def tunnel(*arguments):
connection = arguments[0]
connection.reset_build()
connection.callback = tunnel_r
connection.arguments = arguments
connection.select = True
connection.points = 1
add(tunnel)
def tunnel_r(connection, radius, length, zoffset = 0):
radius = int(radius)
length = int(length)
zoffset = int(zoffset)
facing = connection.get_direction()
if facing == WEST or facing == NORTH:
length = -length
for rel_h in xrange(-radius, radius + 1):
for rel_v in xrange(-radius, 1):
if round(sqrt(rel_h**2 + rel_v**2)) <= radius:
if facing == NORTH or facing == SOUTH:
y1 = connection.block1_y
y2 = connection.block1_y + length
for y in xrange(min(y1, y2), max(y1, y2) + 1):
remove_block(connection.protocol, connection.block1_x + rel_h, y, connection.block1_z + rel_v + zoffset, connection.mirror_x, connection.mirror_y)
elif facing == WEST or facing == EAST:
x1 = connection.block1_x
x2 = connection.block1_x + length
for x in xrange(min(x1, x2), max(x1, x2) + 1):
remove_block(connection.protocol, x, connection.block1_y + rel_h, connection.block1_z + rel_v + zoffset, connection.mirror_x, connection.mirror_y)
def insert(*arguments):
connection = arguments[0]
connection.reset_build()
connection.callback = insert_r
connection.arguments = arguments
connection.select = True
connection.points = 2
add(insert)
def insert_r(connection):
x1 = min(connection.block1_x, connection.block2_x)
x2 = max(connection.block1_x, connection.block2_x)
y1 = min(connection.block1_y, connection.block2_y)
y2 = max(connection.block1_y, connection.block2_y)
z1 = min(connection.block1_z, connection.block2_z)
z2 = max(connection.block1_z, connection.block2_z)
color = make_color_tuple(connection.color)
for xx in xrange(x1, x2 + 1):
for yy in xrange(y1, y2 + 1):
for zz in xrange(z1, z2 + 1):
add_block(connection.protocol, xx, yy, zz, color, connection.player_id, connection.mirror_x, connection.mirror_y)
def delete(*arguments):
connection = arguments[0]
connection.reset_build()
connection.callback = delete_r
connection.arguments = arguments
connection.select = True
connection.points = 2
add(delete)
def delete_r(connection):
x1 = min(connection.block1_x, connection.block2_x)
x2 = max(connection.block1_x, connection.block2_x)
y1 = min(connection.block1_y, connection.block2_y)
y2 = max(connection.block1_y, connection.block2_y)
z1 = min(connection.block1_z, connection.block2_z)
z2 = max(connection.block1_z, connection.block2_z)
for xx in xrange(x1, x2 + 1):
for yy in xrange(y1, y2 + 1):
for zz in xrange(z1, z2 + 1):
remove_block(connection.protocol, xx, yy, zz, connection.mirror_x, connection.mirror_y)
def pattern(*arguments):
connection = arguments[0]
connection.reset_build()
connection.callback = pattern_r
connection.arguments = arguments
connection.select = True
connection.points = 2
add(pattern)
def pattern_r(connection, copies):
copies = int(copies)
x1 = min(connection.block1_x, connection.block2_x)
x2 = max(connection.block1_x, connection.block2_x)
y1 = min(connection.block1_y, connection.block2_y)
y2 = max(connection.block1_y, connection.block2_y)
z1 = min(connection.block1_z, connection.block2_z)
z2 = max(connection.block1_z, connection.block2_z)
delta_z = (z2 - z1) + 1
for xx in xrange(x1, x2 + 1):
for yy in xrange(y1, y2 + 1):
for zz in xrange(z1, z2 + 1):
if connection.protocol.map.get_solid(xx, yy, zz):
color = make_color_tuple(connection.protocol.map.get_point(xx, yy, zz)[1])
set_color(connection.protocol, color, 32)
for i in xrange(1, copies + 1):
z_offset = delta_z * i
add_block(connection.protocol, xx, yy, zz - z_offset, color, 32, connection.mirror_x, connection.mirror_y)
def hollow(*arguments):
connection = arguments[0]
connection.reset_build()
connection.callback = hollow_r
connection.arguments = arguments
connection.select = True
connection.points = 2
add(hollow)
def hollow_r(connection, thickness = 1):
m = connection.protocol.map
thickness = int(thickness) - 1
x1 = min(connection.block1_x, connection.block2_x)
x2 = max(connection.block1_x, connection.block2_x)
y1 = min(connection.block1_y, connection.block2_y)
y2 = max(connection.block1_y, connection.block2_y)
z1 = min(connection.block1_z, connection.block2_z)
z2 = max(connection.block1_z, connection.block2_z)
blocks = []
xr = x2 - x1 + 1
yr = y2 - y1 + 1
zr = z2 - z1 + 1
for x in xrange(0, xr):
blocks.append([])
for y in xrange(0, yr):
blocks[x].append([])
for z in xrange(0, zr):
blocks[x][y].append(False)
def hollow_check(xc, yc, zc, thickness):
if thickness > 0:
for xx in xrange(xc - 1, xc + 2):
if xx >= 0 and xx < xr:
for yy in xrange(yc - 1, yc + 2):
if yy >= 0 and yy < yr:
for zz in xrange(zc - 1, zc + 2):
if zz >= 0 and zz < zr:
blocks[xx][yy][zz] = True
if m.get_solid(x1 + xx, y1 + yy, z1 + zz):
hollow_check(xx, yy, zz, thickness - 1)
for x in xrange(0, xr):
for y in xrange(0, yr):
for z in xrange(0, zr):
if m.get_solid(x1 + x, y1 + y, z1 + z):
if m.is_surface(x1 + x, y1 + y, z1 + z):
blocks[x][y][z] = True
hollow_check(x, y, z, thickness)
else:
blocks[x][y][z] = True
for x in xrange(0, xr):
for y in xrange(0, yr):
for z in xrange(0, zr):
if not blocks[x][y][z]:
remove_block(connection.protocol, x1 + x, y1 + y, z1 + z)
def apply_script(protocol, connection, config):
class MapMakingToolsConnection(connection):
select = False
mirror_x = False
mirror_y = False
def reset_build(self):
self.block1_x = None
self.block1_y = None
self.block1_z = None
self.block2_x = None
self.block2_y = None
self.block2_z = None
self.callback = None
self.arguments = None
self.select = False
self.points = None
def get_direction(self):
orientation = self.world_object.orientation
angle = atan2(orientation.y, orientation.x)
if angle < 0:
angle += 6.283185307179586476925286766559
# Convert to units of quadrents
angle *= 0.63661977236758134307553505349006
angle = round(angle)
if angle == 4:
angle = 0
return angle
def on_block_destroy(self, x, y, z, value):
if self.select == True:
if self.points == 1:
self.block1_x = x
self.block1_y = y
self.block1_z = z
self.callback(*self.arguments)
self.reset_build()
return False
elif self.points == 2:
if self.block1_x == None:
self.block1_x = x
self.block1_y = y
self.block1_z = z
self.send_chat('First block selected')
return False
else:
if not (x == self.block1_x and y == self.block1_y and z == self.block1_z):
self.block2_x = x
self.block2_y = y
self.block2_z = z
self.callback(*self.arguments)
self.reset_build()
self.send_chat('Second block selected')
return False
if self.mirror_x == True or self.mirror_y == True:
x2 = x
y2 = y
if self.mirror_x == True:
x2 = 511 - x
if self.mirror_y == True:
y2 = 511 - y
remove_block(self.protocol, x2, y2, z)
connection.on_block_destroy(self, x, y, z, value)
def on_block_build(self, x, y, z):
if self.mirror_x == True or self.mirror_y == True:
x2 = x
y2 = y
if self.mirror_x == True:
x2 = 511 - x
if self.mirror_y == True:
y2 = 511 - y
add_block(self.protocol, x2, y2, z, make_color_tuple(self.color), self.player_id)
connection.on_block_build(self, x, y, z)
return protocol, MapMakingToolsConnection