def _load_chunk_from_tag(self, chunk, tag): """ Load a chunk from a tag. We cannot instantiate chunks, ever, so pass it in from above. """ level = tag["Level"] # These fromstring() calls are designed to raise if there are any # issues, but still be speedy. # Loop through the sections and unpack anything that we find. for tag in level["Sections"].tags: index = tag["Y"].value section = Section() section.blocks = array("B") section.blocks.fromstring(tag["Blocks"].value) section.metadata = array("B", unpack_nibbles(tag["Data"].value)) section.skylight = array("B", unpack_nibbles(tag["SkyLight"].value)) chunk.sections[index] = section chunk.heightmap = array("B") chunk.heightmap.fromstring(level["HeightMap"].value) chunk.blocklight = array("B", unpack_nibbles(level["BlockLight"].value)) chunk.populated = bool(level["TerrainPopulated"]) if "Entities" in level: for tag in level["Entities"].tags: try: entity = self._load_entity_from_tag(tag) chunk.entities.add(entity) except KeyError: log.msg("Unknown entity %s" % tag["id"].value) log.msg("Tag for entity:") log.msg(tag.pretty_tree()) if "TileEntities" in level: for tag in level["TileEntities"].tags: try: tile = self._load_tile_from_tag(tag) chunk.tiles[tile.x, tile.y, tile.z] = tile except KeyError: log.msg("Unknown tile entity %s" % tag["id"].value) log.msg("Tag for tile:") log.msg(tag.pretty_tree()) chunk.dirty = not chunk.populated
def _load_chunk_from_tag(self, chunk, tag): """ Load a chunk from a tag. We cannot instantiate chunks, ever, so pass it in from above. """ level = tag["Level"] # These are designed to raise if there are any issues, but still be # speedy. chunk.blocks = fromstring(level["Blocks"].value, dtype="uint8").reshape(chunk.blocks.shape) chunk.heightmap = fromstring(level["HeightMap"].value, dtype="uint8").reshape(chunk.heightmap.shape) chunk.blocklight = array(unpack_nibbles( level["BlockLight"].value)).reshape(chunk.blocklight.shape) chunk.metadata = array(unpack_nibbles( level["Data"].value)).reshape(chunk.metadata.shape) chunk.skylight = array(unpack_nibbles( level["SkyLight"].value)).reshape(chunk.skylight.shape) chunk.populated = bool(level["TerrainPopulated"]) if "Entities" in level: for tag in level["Entities"].tags: try: entity = self._load_entity_from_tag(tag) chunk.entities.add(entity) except KeyError: log.msg("Unknown entity %s" % tag["id"].value) log.msg("Tag for entity:") log.msg(tag.pretty_tree()) if "TileEntities" in level: for tag in level["TileEntities"].tags: try: tile = self._load_tile_from_tag(tag) chunk.tiles[tile.x, tile.y, tile.z] = tile except KeyError: log.msg("Unknown tile entity %s" % tag["id"].value) log.msg("Tag for tile:") log.msg(tag.pretty_tree()) chunk.dirty = not chunk.populated
def test_nibble_reflexivity(self): self.assertEqual("nibbles", pack_nibbles(unpack_nibbles("nibbles")))
def test_unpack_nibbles_multiple(self): self.assertEqual(unpack_nibbles("nibbles"), array("B", [14, 6, 9, 6, 2, 6, 2, 6, 12, 6, 5, 6, 3, 7]) )
def test_unpack_nibbles_single(self): self.assertEqual(unpack_nibbles("a"), array("B", [1, 6]))
def test_unpack_nibbles(self): assert_array_equal(unpack_nibbles("a"), [1, 6]) assert_array_equal(unpack_nibbles("nibbles"), [14, 6, 9, 6, 2, 6, 2, 6, 12, 6, 5, 6, 3, 7])
def test_unpack_nibbles_multiple(self): self.assertEqual( unpack_nibbles("nibbles"), array("B", [14, 6, 9, 6, 2, 6, 2, 6, 12, 6, 5, 6, 3, 7]))