-
Notifications
You must be signed in to change notification settings - Fork 2
/
mcmerge.py
267 lines (216 loc) · 9.36 KB
/
mcmerge.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
#!/usr/bin/env python
import sys, os.path, errno, logging
import ancillary, cli, filter
from various import Shifter, Relighter
from contour import Contour, ContourLoadError
from merge import ChunkShaper, Merger
logging.basicConfig(format="... %(message)s")
pymclevel_log = logging.getLogger('pymclevel')
pymclevel_log.setLevel(logging.CRITICAL)
def get_contour(reset=False):
""" Gets existing contour data """
contour = Contour()
if reset:
return contour
contour_data_file = os.path.join(cli.world_dir, cli.contour_file_name)
try:
contour.read(contour_data_file)
except (EnvironmentError, ContourLoadError), e:
if e.errno != errno.ENOENT:
error('could not read contour data: %s' % e)
return contour
if __name__ == '__main__':
# Values and helpers
class Modes(object):
__metaclass__ = ancillary.Enum
__elements__ = cli.command_list
def error(msg):
print "Error: %s" % msg
sys.exit(1)
# Parse command line
try:
mode, _ = cli.parse(sys.argv[1:])
except cli.CommandParsingError, e:
cli.error(e)
# No command given
if mode is None:
cli.error("must specify command")
# Trace contour of the old world
if mode == Modes.trace:
print "Getting existing world contour..."
contour = get_contour(cli.contour_reset)
print "Tracing world contour..."
try:
contour.trace_combine(cli.world_dir, cli.contour_combine, cli.merge_types, cli.contour_select, cli.contour_join)
except (EnvironmentError, ContourLoadError), e:
error('could not read world contour: %s' % e)
print "Recording world contour data..."
try:
contour.write(os.path.join(cli.world_dir, cli.contour_file_name))
except EnvironmentError, e:
error('could not write world contour data: %s' % e)
print "World contour detection complete"
# Shift the map height
elif mode == Modes.shift:
if not cli.shift_immediate:
print "Getting existing world contour..."
contour = get_contour(cli.contour_reset)
print "Loading world..."
try:
shift = Shifter(cli.world_dir)
except EnvironmentError, e:
error('could not read world data: %s' % e)
print "Marking chunks for shifting..."
shift.mark(contour, -cli.shift_down)
print "Recording world shift data..."
try:
contour.write(os.path.join(cli.world_dir, cli.contour_file_name))
except EnvironmentError, e:
error('could not write world contour data: %s' % e)
print
print "World shift marking complete"
else:
print "Loading world..."
try:
shift = Shifter(cli.world_dir)
except EnvironmentError, e:
error('could not read world data: %s' % e)
print "Shifting chunks:"
print
total = sum(1 for _ in shift.level.allChunks)
width = len(str(total))
def progress(n):
print ("... %%%dd/%%d (%%.1f%%%%)" % width) % (n, total, 100.0*n/total)
shift.log_interval = 200
shift.log_function = progress
shifted = shift.shift_all(-cli.shift_down)
print
print "Relighting and saving:"
print
pymclevel_log.setLevel(logging.INFO)
try:
shift.commit()
except EnvironmentError, e:
error('could not save world data: %s' % e)
pymclevel_log.setLevel(logging.CRITICAL)
print
print "Finished shifting, shifted: %d chunks" % shifted
# Attempt to merge new chunks with old chunks
elif mode == Modes.merge:
contour_data_file = os.path.join(cli.world_dir, cli.contour_file_name)
if 'gauss' in (ChunkShaper.filt_name_river, ChunkShaper.filt_name_even):
if not hasattr(filter, 'scipy'):
print "You must install SciPy to use the '%s' filter" % 'gauss'
sys.exit(1)
print "Getting saved world contour..."
contour = Contour()
try:
contour.read(contour_data_file)
except (EnvironmentError, ContourLoadError), e:
if e.errno == errno.ENOENT:
if os.path.exists(cli.world_dir):
error("no contour data to merge with (use trace mode to generate)")
else:
error('could not read world data: File not found: %s' % cli.world_dir)
else:
error('could not read contour data: %s' % e)
def save_contour():
try:
if contour.empty:
os.remove(contour_data_file)
else:
contour.write(contour_data_file)
except EnvironmentError, e:
error('could not updated world contour data: %s' % e)
if contour.shift and not cli.merge_no_shift:
print "Loading world..."
print
try:
shift = Shifter(cli.world_dir)
except EnvironmentError, e:
error('could not read world data: %s' % e)
print "Shifting chunks:"
print
total = sum(1 for _ in shift.level.allChunks)
width = len(str(total))
def progress(n):
print ("... %%%dd/%%d (%%.1f%%%%)" % width) % (n, total, 100.0*n/total)
shift.log_interval = 200
shift.log_function = progress
shifted = shift.shift_marked(contour)
print
print "Relighting and saving:"
print
pymclevel_log.setLevel(logging.INFO)
try:
shift.commit()
except EnvironmentError, e:
error('could not save world data: %s' % e)
pymclevel_log.setLevel(logging.CRITICAL)
print
print "Finished shifting, shifted: %d chunks" % shifted
print "Updating contour data"
contour.shift.clear()
save_contour()
print
if contour.edges and not cli.merge_no_merge:
print "Loading world..."
print
try:
merge = Merger(cli.world_dir)
except EnvironmentError, e:
error('could not read world data: %s' % e)
print "Merging chunks:"
print
active = [Contour.methods[x].bit for x in Merger.processing_order]
total = sum(sum((1 if x.method & y else 0) for y in active) for x in contour.edges.itervalues())
width = len(str(total))
def progress(n):
print ("... %%%dd/%%d (%%.1f%%%%)" % width) % (n, total, 100.0*n/total)
merge.log_interval = 10
merge.log_function = progress
reshaped = merge.erode(contour)
print
print "Relighting and saving:"
print
pymclevel_log.setLevel(logging.INFO)
try:
merge.commit()
except EnvironmentError, e:
error('could not save world data: %s' % e)
pymclevel_log.setLevel(logging.CRITICAL)
print
print "Finished merging, merged: %d/%d chunks" % (sum(len(x) for x in reshaped.itervalues()), total)
print "Updating contour data"
mask = reduce(lambda a, x: a | x, active, 0)
for method, coords in reshaped.iteritems():
method_bit = Contour.methods[method].bit
for coord in coords:
contour.edges[coord].method &= ~method_bit
if contour.edges[coord].method & mask == 0:
del contour.edges[coord]
save_contour()
# Relight all the chunks on the map
elif mode == Modes.relight:
print "Loading world..."
try:
relight = Relighter(cli.world_dir)
except EnvironmentError, e:
error('could not read world data: %s' % e)
print "Marking and relighting chunks:"
print
pymclevel_log.setLevel(logging.INFO)
relit = relight.relight()
print
print "Saving:"
print
try:
relight.commit()
except EnvironmentError, e:
error('could not save world data: %s' % e)
pymclevel_log.setLevel(logging.CRITICAL)
print
print "Finished relighting, relit: %d chunks" % relit
# Should have found the right mode already!
else:
error("something went horribly wrong performing mode '%s'" % mode)