-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuildawall.py
More file actions
177 lines (144 loc) · 5.29 KB
/
Copy pathbuildawall.py
File metadata and controls
177 lines (144 loc) · 5.29 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
import sys
import logging
from contextlib import contextmanager
from pymclevel import mclevel, ChunkNotPresent
"""
-x
-z +z
+x
"""
class WallBuilder(object):
def __init__(self, level, min_height=-119, max_height=119,
outer_mat_id=None, inner_mat_id=None):
self.mih = min_height
self.mah = max_height
self.om = outer_mat_id or level.materials.WhiteWool.ID
self.im = inner_mat_id or level.materials.Bedrock.ID
self.level = level
@contextmanager
def chunk(self, chunk_address):
chunk = self.level.getChunk(*chunk_address)
yield chunk
chunk.chunkChanged()
@contextmanager
def y_bound_blocks(self, chunk_address):
with self.chunk(chunk_address) as chunk:
yield chunk.Blocks[:,:,self.mih:self.mah+1]
def build_walls(self):
chunk_map = dict()
print "Scanning chunks."
for chunk_x, chunk_z in self.level.allChunks:
chunk_map.setdefault(chunk_x, set()).add(chunk_z)
print "Building walls."
walls_px = set()
walls_nx = set()
walls_pz = set()
walls_nz = set()
null_set = set()
for chunk_x, chunk_z in self.level.allChunks:
chunk = (chunk_x, chunk_z)
if not chunk_z in chunk_map.get(chunk_x - 1, null_set):
walls_nx.add(chunk)
self.wall_x(0, chunk)
if not chunk_z in chunk_map.get(chunk_x + 1, null_set):
walls_px.add(chunk)
self.wall_x(1, chunk)
if not (chunk_z - 1) in chunk_map.get(chunk_x, null_set):
walls_nz.add(chunk)
self.wall_z(0, chunk)
if not (chunk_z + 1) in chunk_map.get(chunk_x, null_set):
walls_pz.add(chunk)
self.wall_z(1, chunk)
for chunk_x, chunk_z in walls_nx:
if (chunk_x, chunk_z - 1) not in walls_nx:
self.corner(1, 2, (chunk_x, chunk_z - 1))
if (chunk_x, chunk_z + 1) not in walls_nx:
self.corner(0, 3, (chunk_x, chunk_z + 1))
for chunk_x, chunk_z in walls_px:
if (chunk_x, chunk_z - 1) not in walls_px:
self.corner(3, 0, (chunk_x, chunk_z - 1))
if (chunk_x, chunk_z + 1) not in walls_px:
self.corner(2, 1, (chunk_x, chunk_z + 1))
for chunk_x, chunk_z in walls_nz:
if (chunk_x - 1, chunk_z) not in walls_nz:
self.corner(2, 1, (chunk_x - 1, chunk_z))
if (chunk_x + 1, chunk_z) not in walls_nz:
self.corner(0, 3, (chunk_x + 1, chunk_z))
for chunk_x, chunk_z in walls_pz:
if (chunk_x - 1, chunk_z) not in walls_pz:
self.corner(3, 0, (chunk_x - 1, chunk_z))
if (chunk_x + 1, chunk_z) not in walls_pz:
self.corner(1, 2, (chunk_x + 1, chunk_z))
for chunk_a in walls_nx:
if chunk_a in walls_nz:
self.corner(0, 0, chunk_a)
if chunk_a in walls_pz:
self.corner(1, 1, chunk_a)
for chunk_a in walls_px:
if chunk_a in walls_nz:
self.corner(2, 2, chunk_a)
if chunk_a in walls_pz:
self.corner(3, 3, chunk_a)
def wall_x(self, side, chunk_a):
with self.y_bound_blocks(chunk_a) as blocks:
low_ix = 13 if side else -16
blocks[low_ix,:,:] = self.om
blocks[low_ix+1,:,:] = self.im
blocks[low_ix+2,:,:] = self.om
def wall_z(self, side, chunk_a):
with self.y_bound_blocks(chunk_a) as blocks:
low_ix = 13 if side else -16
blocks[:,low_ix,:] = self.om
blocks[:,low_ix+1,:] = self.im
blocks[:,low_ix+2,:] = self.om
def corner(self, corner, out_direction, chunk_a):
try:
with self.y_bound_blocks(chunk_a) as blocks:
corner_blocks = self.bound_corner(corner, blocks)
self.carve_corner(out_direction, corner_blocks)
except ChunkNotPresent:
pass
def carve_corner(self, out_direction, blocks):
"""
out_direction:
0: ooo 1: ooo
oii iio
oio oio
2: oio 3: oio
oii iio
ooo ooo
"""
x_flip = bool(out_direction & 2) * 2
z_flip = bool(out_direction & 1) * 2
blocks[:,:,:] = self.im
blocks[x_flip,:,:] = self.om
blocks[:,z_flip,:] = self.om
blocks[2-x_flip,2-z_flip,:] = self.om
def bound_corner(self, corner, blocks):
"""
0 1
2 3
"""
# lol
x = int((((bool(corner & 2) * 2) - 1) * 14.5) - 1.5)
z = int((((bool(corner & 1) * 2) - 1) * 14.5) - 1.5)
return blocks[x:x+3,z:z+3,:]
def main(args=sys.argv):
logging.basicConfig(level=logging.INFO, format="%(message)s",
stream=sys.stdout)
if not args[1:]:
print """\
USAGE
buildawall.py <level.dat>
"""
return 1
world = mclevel.fromFile(args[1])
WallBuilder(world).build_walls()
print "Generating light."
world.generateLights()
print "Saving."
world.saveInPlace()
print "Saved."
return 0
if __name__=='__main__':
sys.exit(main())