2015-02-01 00:21:17 +01:00
|
|
|
/*
|
|
|
|
Copyright (c) 2015, Matthias Schiffer <mschiffer@universe-factory.net>
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
1. Redistributions of source code must retain the above copyright notice,
|
|
|
|
this list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
this list of conditions and the following disclaimer in the documentation
|
|
|
|
and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "Chunk.hpp"
|
2015-02-02 00:31:52 +01:00
|
|
|
#include "../Resource/BlockType.hpp"
|
2015-02-01 00:21:17 +01:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <cstdlib>
|
2015-02-01 14:35:31 +01:00
|
|
|
#include <cstring>
|
2015-02-01 00:21:17 +01:00
|
|
|
|
|
|
|
#include <zlib.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace MinedMap {
|
|
|
|
namespace World {
|
|
|
|
|
2015-02-01 07:47:22 +01:00
|
|
|
Chunk::Chunk(Buffer buffer) {
|
|
|
|
size_t size = buffer.get32();
|
|
|
|
|
|
|
|
Buffer buffer2(buffer.get(size), size);
|
|
|
|
|
|
|
|
uint8_t format = buffer2.get8();
|
|
|
|
if (format != 2)
|
|
|
|
throw std::invalid_argument("unknown chunk format");
|
|
|
|
|
|
|
|
inflateChunk(buffer2);
|
|
|
|
parseChunk();
|
|
|
|
analyzeChunk();
|
|
|
|
}
|
|
|
|
|
2015-02-01 06:04:56 +01:00
|
|
|
void Chunk::inflateChunk(Buffer buffer) {
|
2015-02-01 00:21:17 +01:00
|
|
|
size_t outlen = 0;
|
|
|
|
uint8_t *output = nullptr;
|
|
|
|
|
|
|
|
z_stream stream = {};
|
|
|
|
int ret = inflateInit(&stream);
|
|
|
|
if (ret != Z_OK)
|
|
|
|
throw std::runtime_error("inflateInit() failed");
|
|
|
|
|
2015-02-01 06:04:56 +01:00
|
|
|
stream.avail_in = buffer.getRemaining();
|
|
|
|
stream.next_in = const_cast<uint8_t *>(buffer.get(stream.avail_in));
|
2015-02-01 00:21:17 +01:00
|
|
|
|
|
|
|
while (stream.avail_in) {
|
|
|
|
outlen += 65536;
|
|
|
|
output = static_cast<uint8_t *>(std::realloc(output, outlen));
|
|
|
|
|
|
|
|
stream.next_out = output + stream.total_out;
|
|
|
|
stream.avail_out = outlen - stream.total_out;
|
|
|
|
|
|
|
|
ret = inflate(&stream, Z_NO_FLUSH);
|
|
|
|
switch (ret) {
|
|
|
|
case Z_NEED_DICT:
|
|
|
|
case Z_DATA_ERROR:
|
|
|
|
case Z_MEM_ERROR:
|
|
|
|
inflateEnd(&stream);
|
|
|
|
throw std::runtime_error("inflate() failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inflateEnd(&stream);
|
|
|
|
|
2015-02-01 05:18:35 +01:00
|
|
|
len = stream.total_out;
|
|
|
|
data = UniqueCPtr<uint8_t[]>(output);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Chunk::parseChunk() {
|
|
|
|
Buffer nbt(data.get(), len);
|
|
|
|
std::pair<std::string, std::shared_ptr<const NBT::Tag>> tag = NBT::Tag::readNamedTag(&nbt);
|
2015-02-01 14:02:04 +01:00
|
|
|
if (tag.first != "")
|
2015-02-01 05:18:35 +01:00
|
|
|
throw std::invalid_argument("invalid root tag");
|
|
|
|
|
2015-02-01 14:02:04 +01:00
|
|
|
root = assertValue(std::dynamic_pointer_cast<const NBT::CompoundTag>(tag.second));
|
|
|
|
level = assertValue(root->get<NBT::CompoundTag>("Level"));
|
2015-02-01 05:18:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Chunk::analyzeChunk() {
|
2015-02-01 14:02:04 +01:00
|
|
|
std::shared_ptr<const NBT::ByteTag> lightPopulatedTag = level->get<NBT::ByteTag>("LightPopulated");
|
|
|
|
if (!lightPopulatedTag && lightPopulatedTag->getValue())
|
|
|
|
throw std::invalid_argument("light data missing");
|
2015-02-01 05:18:35 +01:00
|
|
|
|
2015-02-01 14:35:31 +01:00
|
|
|
sections = assertValue(level->get<NBT::ListTag<NBT::CompoundTag>>("Sections"));
|
|
|
|
maxY = (assertValue(sections->back()->get<NBT::ByteTag>("Y"))->getValue() + 1) * SIZE;
|
2015-02-01 05:54:42 +01:00
|
|
|
|
2015-02-01 15:19:18 +01:00
|
|
|
|
|
|
|
std::shared_ptr<const NBT::ByteArrayTag> biomeTag = assertValue(level->get<NBT::ByteArrayTag>("Biomes"));
|
|
|
|
if (biomeTag->getLength() != SIZE*SIZE)
|
|
|
|
throw std::invalid_argument("corrupt biome data");
|
|
|
|
|
|
|
|
biomes = biomeTag->getValue();
|
|
|
|
|
2015-02-01 14:35:31 +01:00
|
|
|
blockIDs.reset(new uint8_t[maxY * SIZE * SIZE]);
|
|
|
|
blockData.reset(new uint8_t[maxY * SIZE * SIZE / 2]);
|
|
|
|
blockSkyLight.reset(new uint8_t[maxY * SIZE * SIZE / 2]);
|
|
|
|
blockBlockLight.reset(new uint8_t[maxY * SIZE * SIZE / 2]);
|
2015-02-01 05:54:42 +01:00
|
|
|
|
2015-02-01 14:35:31 +01:00
|
|
|
std::memset(blockSkyLight.get(), 0xff, maxY * SIZE * SIZE / 2);
|
2015-02-01 05:54:42 +01:00
|
|
|
|
2015-02-01 14:02:04 +01:00
|
|
|
|
2015-02-01 14:35:31 +01:00
|
|
|
for (auto §ion : *sections) {
|
|
|
|
std::shared_ptr<const NBT::ByteArrayTag> blocks = assertValue(section->get<NBT::ByteArrayTag>("Blocks"));
|
|
|
|
std::shared_ptr<const NBT::ByteArrayTag> data = assertValue(section->get<NBT::ByteArrayTag>("Data"));
|
|
|
|
std::shared_ptr<const NBT::ByteArrayTag> blockLight = assertValue(section->get<NBT::ByteArrayTag>("BlockLight"));
|
|
|
|
std::shared_ptr<const NBT::ByteArrayTag> skyLight = assertValue(section->get<NBT::ByteArrayTag>("SkyLight"));
|
|
|
|
size_t Y = assertValue(section->get<NBT::ByteTag>("Y"))->getValue();
|
|
|
|
|
|
|
|
if (blocks->getLength() != SIZE*SIZE*SIZE || data->getLength() != SIZE*SIZE*SIZE/2
|
|
|
|
|| blockLight->getLength() != SIZE*SIZE*SIZE/2 || skyLight->getLength() != SIZE*SIZE*SIZE/2)
|
|
|
|
throw std::invalid_argument("corrupt chunk data");
|
|
|
|
|
|
|
|
std::memcpy(blockIDs.get() + Y*SIZE*SIZE*SIZE, blocks->getValue(), SIZE*SIZE*SIZE);
|
|
|
|
std::memcpy(blockData.get() + Y*SIZE*SIZE*SIZE/2, data->getValue(), SIZE*SIZE*SIZE/2);
|
|
|
|
std::memcpy(blockBlockLight.get() + Y*SIZE*SIZE*SIZE/2, blockLight->getValue(), SIZE*SIZE*SIZE/2);
|
|
|
|
std::memcpy(blockSkyLight.get() + Y*SIZE*SIZE*SIZE/2, skyLight->getValue(), SIZE*SIZE*SIZE/2);
|
2015-02-01 14:02:04 +01:00
|
|
|
}
|
2015-02-01 00:21:17 +01:00
|
|
|
}
|
|
|
|
|
2015-02-01 07:47:22 +01:00
|
|
|
Chunk::Blocks Chunk::getTopLayer() const {
|
|
|
|
size_t done = 0;
|
2015-02-01 14:02:04 +01:00
|
|
|
Blocks ret;
|
2015-02-01 07:47:22 +01:00
|
|
|
|
2015-02-01 14:02:04 +01:00
|
|
|
for (ssize_t y = maxY-1; y >= 0; y--) {
|
2015-02-01 07:47:22 +01:00
|
|
|
if (done == SIZE*SIZE)
|
|
|
|
break;
|
|
|
|
|
2015-02-01 14:02:04 +01:00
|
|
|
for (size_t z = 0; z < SIZE; z++) {
|
2015-02-01 07:47:22 +01:00
|
|
|
for (size_t x = 0; x < SIZE; x++) {
|
2015-02-01 14:02:04 +01:00
|
|
|
if (ret.blocks[x][z].id)
|
|
|
|
continue;
|
|
|
|
|
2015-02-01 14:35:31 +01:00
|
|
|
uint8_t id = getBlockAt(x, y, z);
|
2015-02-02 00:31:52 +01:00
|
|
|
if (!Resource::BLOCK_TYPES[id].opaque)
|
2015-02-01 14:02:04 +01:00
|
|
|
continue;
|
|
|
|
|
|
|
|
Block &b = ret.blocks[x][z];
|
|
|
|
|
2015-02-01 14:35:31 +01:00
|
|
|
b.id = id;
|
|
|
|
b.data = getDataAt(x, y, z);
|
2015-02-01 14:02:04 +01:00
|
|
|
|
2015-02-01 14:35:31 +01:00
|
|
|
size_t y2 = y;
|
|
|
|
if (y2 < maxY-1)
|
|
|
|
y2++;
|
2015-02-01 14:02:04 +01:00
|
|
|
|
2015-02-01 14:35:31 +01:00
|
|
|
b.blockLight = getBlockLightAt(x, y2, z);
|
|
|
|
b.skyLight = getSkyLightAt(x, y2, z);
|
2015-02-01 14:02:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
unsigned h;
|
|
|
|
for (h = y; h > 0; h--) {
|
2015-02-01 14:35:31 +01:00
|
|
|
uint8_t id2 = getBlockAt(x, h, z);
|
|
|
|
if (id2 != 8 && id2 != 9)
|
2015-02-01 14:02:04 +01:00
|
|
|
break;
|
2015-02-01 07:47:22 +01:00
|
|
|
}
|
2015-02-01 14:02:04 +01:00
|
|
|
|
|
|
|
b.height = h;
|
|
|
|
|
2015-02-01 15:19:18 +01:00
|
|
|
b.biome = getBiomeAt(x, z);
|
|
|
|
|
2015-02-01 14:02:04 +01:00
|
|
|
done++;
|
2015-02-01 07:47:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-01 00:21:17 +01:00
|
|
|
|
2015-02-01 14:02:04 +01:00
|
|
|
return ret;
|
2015-02-01 00:21:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|