added bounds property, returning a bounding box surrounding the level.

calls getWorldBounds to find the edges and caches the box in _bounds, which is cleared by create/deleteChunk
This commit is contained in:
David Vierra 2010-11-15 20:03:37 -10:00
parent ee2f30935d
commit 45d16bb0b9
2 changed files with 26 additions and 9 deletions

12
mce.py
View File

@ -422,7 +422,7 @@ class mce(object):
count = self.readInt(command)
chest = mclevel.MCSchematic.chestWithItemID(itemID, count);
self.level.copyBlocksFrom(chest, chest.getWorldBounds(), point);
self.level.copyBlocksFrom(chest, chest.bounds, point);
self.needsSave = True;
def _analyze(self, command):
@ -473,7 +473,7 @@ class mce(object):
tempSchematic.saveToFile(filename)
print "Exported {0} blocks.".format(tempSchematic.getWorldBounds().volume)
print "Exported {0} blocks.".format(tempSchematic.bounds.volume)
def _import(self, command):
"""
@ -500,11 +500,11 @@ class mce(object):
destBox = BoundingBox(destPoint, importLevel.size)
self.level.createChunksInBox(destBox);
self.level.copyBlocksFrom(importLevel, importLevel.getWorldBounds(), destPoint, blocksToCopy);
self.level.copyBlocksFrom(importLevel, importLevel.bounds, destPoint, blocksToCopy);
self.needsSave = True;
print "Imported {0} blocks.".format(importLevel.getWorldBounds().volume)
print "Imported {0} blocks.".format(importLevel.bounds.volume)
def _player(self, command):
"""
@ -797,7 +797,7 @@ class mce(object):
Also see removeEntities
"""
box = self.level.getWorldBounds();
box = self.level.bounds;
box.miny = 32
if len(command):
try:
@ -894,7 +894,7 @@ class mce(object):
Computes and prints the dimensions of the world. For infinite worlds,
also prints the most negative corner.
"""
bounds = self.level.getWorldBounds();
bounds = self.level.bounds;
if isinstance(self.level, mclevel.MCInfdevOldLevel):
print "\nWorld size: \n {0[0]:7} north to south\n {0[2]:7} east to west\n".format(bounds.size);
print "Smallest and largest points: ({0[0]},{0[2]}), ({1[0]},{1[2]})".format(bounds.origin, bounds.maximum);

View File

@ -368,6 +368,10 @@ class MCLevel(object):
"Returns the level's dimensions as a tuple (X,Y,Z)"
return (self.Width, self.Height, self.Length)
@property
def bounds(self):
return BoundingBox( (0,0,0), self.size )
def compressedSize(self):
"return the size of the compressed data for this level, in bytes."
self.compress();
@ -531,7 +535,7 @@ class MCLevel(object):
return self.Blocks[x:x+w,z:z+l,y:y+h]
def fillBlocks(self, box, blockType, blockData = 0, blocksToReplace = None):
if box is None: box = self.getWorldBounds()
if box is None: box = self.bounds
info( u"Filling blocks in {0} with {1}, data={2} replacing{3}".format(box, blockType, blockData, blocksToReplace) )
slices = map(slice, box.origin, box.maximum)
@ -1820,6 +1824,16 @@ class MCInfdevOldLevel(MCLevel):
def LastPlayed(self, val):
self.root_tag[Data]['LastPlayed'].value = val
_bounds = None
@property
def bounds(self):
if self._bounds is None: self._bounds = self.getWorldBounds();
return self._bounds
@property
def size(self):
return self.bounds.size
def create(self, filename, random_seed, last_played):
if filename == None:
@ -2862,10 +2876,12 @@ class MCInfdevOldLevel(MCLevel):
debug( u"Forgetting malformed chunk {0} ({1})".format((cx,cz), self.chunkFilename(cx,cz)) )
if (cx,cz) in self._presentChunks:
del self._presentChunks[(cx,cz)]
self._bounds = None
def createChunk(self, cx, cz):
if (cx,cz) in self._presentChunks: raise ValueError, "{0}:Chunk {1} already present!".format(self, (cx,cz) )
self._presentChunks[cx,cz] = InfdevChunk(self, (cx,cz), create = True)
self._bounds = None
def createChunksInBox(self, box):
info( u"Creating {0} chunks in {1}".format((box.maxcx-box.mincx)*( box.maxcz-box.mincz), ((box.mincx, box.mincz), (box.maxcx, box.maxcz))) )
@ -2889,6 +2905,7 @@ class MCInfdevOldLevel(MCLevel):
self._presentChunks[(cx,cz)].remove();
del self._presentChunks[(cx,cz)]
self._bounds = None
def deleteChunksInBox(self, box):
info( u"Deleting {0} chunks in {1}".format((box.maxcx-box.mincx)*( box.maxcz-box.mincz), ((box.mincx, box.mincz), (box.maxcx, box.maxcz))) )