Skip to content

Commit

Permalink
extractHeights always returns a heightmap without its axes swapped. I…
Browse files Browse the repository at this point in the history
…nstead, computeChunkHeightMap swaps the axes and handles filling in a chunk's existing HeightMap array.
  • Loading branch information
codewarrior0 committed Dec 23, 2011
1 parent 3e1520e commit de8dd71
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
2 changes: 1 addition & 1 deletion indev.py
Expand Up @@ -87,7 +87,7 @@

__all__ = ["MCIndevLevel"]

from level import EntityLevel, extractLightMap
from level import EntityLevel, computeChunkHeightMap

class MCIndevLevel(EntityLevel):
""" IMPORTANT: self.Blocks and self.Data are indexed with [x,z,y] via axis
Expand Down
4 changes: 2 additions & 2 deletions infiniteworld.py
Expand Up @@ -17,7 +17,7 @@
log = logging.getLogger(__name__)
warn, error, info, debug = log.warn, log.error, log.info, log.debug

from level import MCLevel, EntityLevel, extractLightMap
from level import MCLevel, EntityLevel, computeChunkHeightMap

#infinite
Level = 'Level'
Expand Down Expand Up @@ -862,7 +862,7 @@ def generateHeightMap(self):
if self.world.dimNo == DIM_NETHER:
self.HeightMap[:] = 0
else:
extractLightMap(self.materials, self.Blocks, self.HeightMap)
computeChunkHeightMap(self.materials, self.Blocks, self.HeightMap)

def chunkChanged(self, calcLighting=True):
""" You are required to call this function after you are done modifying
Expand Down
30 changes: 19 additions & 11 deletions level.py
Expand Up @@ -13,17 +13,25 @@
log = logging.getLogger(__name__)
warn, error, info, debug = log.warn, log.error, log.info, log.debug

def extractLightMap(materials, blocks, heightMap = None):
def computeChunkHeightMap(materials, blocks, HeightMap = None):
"""Computes the HeightMap array for a chunk, which stores the lowest
y-coordinate of each column where the sunlight is still at full strength.
The HeightMap array is indexed z,x contrary to the blocks array which is x,z,y"""
The HeightMap array is indexed z,x contrary to the blocks array which is x,z,y.
If HeightMap is passed, fills it with the result and returns it. Otherwise, returns a
new array.
"""

lightAbsorption = materials.lightAbsorption[blocks]
heightMap = extractHeights(lightAbsorption, heightMap)
heightMap.swapaxes(0, 1)
return heightMap

def extractHeights(array, heightMap = None):
heights = extractHeights(lightAbsorption)
heights = heights.swapaxes(0, 1)
if HeightMap is None:
return heights
else:
HeightMap[:] = heights
return HeightMap

def extractHeights(array):
""" Given an array of bytes shaped (x, z, y), return the coordinates of the highest
non-zero value in each y-column into heightMap
"""
Expand All @@ -32,8 +40,8 @@ def extractHeights(array, heightMap = None):
# then turn it upside down with ::-1 and use argmax to get the _first_ nonzero
# from each column.

if heightMap is None:
heightMap = zeros((16, 16), 'uint8')
w, h = array.shape[:2]
heightMap = zeros((w, h), 'uint8')


heights = argmax((array>0)[..., ::-1], 2)
Expand All @@ -43,7 +51,7 @@ def extractHeights(array, heightMap = None):
#top height columns won't ever have air in the top block so we can find air columns by checking for both
heights[(array[..., -1]==0) & (heights == array.shape[2])] = 0

heightMap[:] = heights.swapaxes(0, 1)
heightMap[:] = heights

return heightMap

Expand Down Expand Up @@ -776,6 +784,6 @@ def HeightMap(self):
if hasattr(self, "_heightMap"):
return self._heightMap

self._heightMap = extractLightMap(self.materials, self.Blocks)
self._heightMap = computeChunkHeightMap(self.materials, self.Blocks)
return self._heightMap

0 comments on commit de8dd71

Please sign in to comment.