def load_from_tag(chunk, tag): 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 "TileEntities" in level: for tag in level["TileEntities"].tags: try: tile = chunk.known_tile_entities[tag["id"].value]() tile.load_from_tag(tag) chunk.tiles[tile.x, tile.y, tile.z] = tile except: print "Unknown tile entity %s" % tag["id"].value 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: print "Unknown entity %s" % tag["id"].value print "Tag for entity:" print 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: print "Unknown tile entity %s" % tag["id"].value print "Tag for tile:" print 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: print "Unknown entity %s" % tag["id"].value print "Tag for entity:" print 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: print "Unknown tile entity %s" % tag["id"].value print "Tag for tile:" print tag.pretty_tree() chunk.dirty = not chunk.populated
def load_from_tag(chunk, tag): level = tag["Level"] chunk.blocks.ravel()[:] = [ord(i) for i in level["Blocks"].value] chunk.heightmap.ravel()[:] = [ord(i) for i in level["HeightMap"].value] chunk.blocklight.ravel()[:] = unpack_nibbles(level["BlockLight"].value) chunk.metadata.ravel()[:] = unpack_nibbles(level["Data"].value) chunk.skylight.ravel()[:] = unpack_nibbles(level["SkyLight"].value) chunk.populated = bool(level["TerrainPopulated"]) if "TileEntities" in level: for tag in level["TileEntities"].tags: try: tile = chunk.known_tile_entities[tag["id"].value]() tile.load_from_tag(tag) chunk.tiles[tile.x, tile.y, tile.z] = tile except: print "Unknown tile entity %s" % tag["id"].value chunk.dirty = not chunk.populated
def load_from_packet(self, packet): cx, bx = divmod(packet.x, 16) cz, bz = divmod(packet.z, 16) if (cx != self.x) or (cz != self.z): print "loading packet data in wrong chunk location" print cx, cz, self.x, self.z return self.fresh = False by = packet.y # protocol subtracts 1 before sending to a packet x_size = packet.x_size + 1 y_size = packet.y_size + 1 z_size = packet.z_size + 1 ex = bx + x_size ez = bz + z_size ey = by + y_size # indices for reading the packet data meta_index = x_size * z_size * y_size sky_index = meta_index light_index = meta_index * 2 # read packet data into an array block_data = fromstring(packet.data[:meta_index], dtype=uint8).reshape(x_size, z_size, y_size) nibbles = array(unpack_nibbles(packet.data[meta_index:])) # took me forever to figure this out! self.blocks[bx:ex, bz:ez, by:ey] = block_data self.metadata[bx:ex, bz:ez, by:ey] = nibbles[:sky_index].reshape(x_size, z_size, y_size) self.skylight[bx:ex, bz:ez, by:ey] = nibbles[sky_index:light_index].reshape(x_size, z_size, y_size) self.blocklight[bx:ex, bz:ez, by:ey] = nibbles[light_index:].reshape(x_size, z_size, y_size)