-
Notifications
You must be signed in to change notification settings - Fork 81
/
block_copy.py
151 lines (103 loc) · 5.76 KB
/
block_copy.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
from datetime import datetime
import logging
log = logging.getLogger(__name__)
import numpy
from box import BoundingBox, Vector
from mclevelbase import exhaust
import materials
from entity import Entity, TileEntity
def convertBlocks(destLevel, sourceLevel, blocks, blockData):
return materials.convertBlocks(destLevel.materials, sourceLevel.materials, blocks, blockData)
def sourceMaskFunc(blocksToCopy):
if blocksToCopy is not None:
typemask = numpy.zeros(materials.id_limit, dtype='bool')
typemask[blocksToCopy] = 1
def maskedSourceMask(sourceBlocks):
return typemask[sourceBlocks]
return maskedSourceMask
def unmaskedSourceMask(_sourceBlocks):
return slice(None, None)
return unmaskedSourceMask
def adjustCopyParameters(destLevel, sourceLevel, sourceBox, destinationPoint):
# if the destination box is outside the level, it and the source corners are moved inward to fit.
(dx, dy, dz) = map(int, destinationPoint)
log.debug(u"Asked to copy {} blocks \n\tfrom {} in {}\n\tto {} in {}" .format(
sourceBox.volume, sourceBox, sourceLevel, destinationPoint, destLevel))
if destLevel.Width == 0:
return sourceBox, destinationPoint
destBox = BoundingBox(destinationPoint, sourceBox.size)
actualDestBox = destBox.intersect(destLevel.bounds)
actualSourceBox = BoundingBox(sourceBox.origin + actualDestBox.origin - destBox.origin, destBox.size)
actualDestPoint = actualDestBox.origin
return actualSourceBox, actualDestPoint
def copyBlocksFromIter(destLevel, sourceLevel, sourceBox, destinationPoint, blocksToCopy=None, entities=True, create=False, biomes=False):
""" copy blocks between two infinite levels by looping through the
destination's chunks. make a sub-box of the source level for each chunk
and copy block and entities in the sub box to the dest chunk."""
(lx, ly, lz) = sourceBox.size
sourceBox, destinationPoint = adjustCopyParameters(destLevel, sourceLevel, sourceBox, destinationPoint)
# needs work xxx
log.info(u"Copying {0} blocks from {1} to {2}" .format(ly * lz * lx, sourceBox, destinationPoint))
startTime = datetime.now()
destBox = BoundingBox(destinationPoint, sourceBox.size)
chunkCount = destBox.chunkCount
i = 0
e = 0
t = 0
sourceMask = sourceMaskFunc(blocksToCopy)
copyOffset = [d - s for s, d in zip(sourceBox.origin, destinationPoint)]
# Visit each chunk in the destination area.
# Get the region of the source area corresponding to that chunk
# Visit each chunk of the region of the source area
# Get the slices of the destination chunk
# Get the slices of the source chunk
# Copy blocks and data
for destCpos in destBox.chunkPositions:
cx, cz = destCpos
destChunkBox = BoundingBox((cx << 4, 0, cz << 4), (16, destLevel.Height, 16)).intersect(destBox)
destChunkBoxInSourceLevel = BoundingBox([d - o for o, d in zip(copyOffset, destChunkBox.origin)], destChunkBox.size)
if not destLevel.containsChunk(*destCpos):
if create and any(sourceLevel.containsChunk(*c) for c in destChunkBoxInSourceLevel.chunkPositions):
# Only create chunks in the destination level if the source level has chunks covering them.
destLevel.createChunk(*destCpos)
else:
continue
destChunk = destLevel.getChunk(*destCpos)
i += 1
yield (i, chunkCount)
if i % 100 == 0:
log.info("Chunk {0}...".format(i))
for srcCpos in destChunkBoxInSourceLevel.chunkPositions:
if not sourceLevel.containsChunk(*srcCpos):
continue
sourceChunk = sourceLevel.getChunk(*srcCpos)
sourceChunkBox, sourceSlices = sourceChunk.getChunkSlicesForBox(destChunkBoxInSourceLevel)
if sourceChunkBox.volume == 0:
continue
sourceChunkBoxInDestLevel = BoundingBox([d + o for o, d in zip(copyOffset, sourceChunkBox.origin)], sourceChunkBox.size)
_, destSlices = destChunk.getChunkSlicesForBox(sourceChunkBoxInDestLevel)
sourceBlocks = sourceChunk.Blocks[sourceSlices]
sourceData = sourceChunk.Data[sourceSlices]
mask = sourceMask(sourceBlocks)
convertedSourceBlocks, convertedSourceData = convertBlocks(destLevel, sourceLevel, sourceBlocks, sourceData)
destChunk.Blocks[destSlices][mask] = convertedSourceBlocks[mask]
if convertedSourceData is not None:
destChunk.Data[destSlices][mask] = convertedSourceData[mask]
if entities:
ents = sourceChunk.getEntitiesInBox(destChunkBoxInSourceLevel)
e += len(ents)
for entityTag in ents:
eTag = Entity.copyWithOffset(entityTag, copyOffset)
destLevel.addEntity(eTag)
tileEntities = sourceChunk.getTileEntitiesInBox(destChunkBoxInSourceLevel)
t += len(tileEntities)
for tileEntityTag in tileEntities:
eTag = TileEntity.copyWithOffset(tileEntityTag, copyOffset)
destLevel.addTileEntity(eTag)
if biomes and hasattr(destChunk, 'Biomes') and hasattr(sourceChunk, 'Biomes'):
destChunk.Biomes[destSlices[:2]] = sourceChunk.Biomes[sourceSlices[:2]]
destChunk.chunkChanged()
log.info("Duration: {0}".format(datetime.now() - startTime))
log.info("Copied {0} entities and {1} tile entities".format(e, t))
def copyBlocksFrom(destLevel, sourceLevel, sourceBox, destinationPoint, blocksToCopy=None, entities=True, create=False, biomes=False):
return exhaust(copyBlocksFromIter(destLevel, sourceLevel, sourceBox, destinationPoint, blocksToCopy, entities, create, biomes))