MinedMap/src/World/Chunk.cpp

147 lines
5 KiB
C++
Raw Normal View History

2015-02-01 00:21:17 +01:00
/*
Copyright (c) 2015-2018, Matthias Schiffer <mschiffer@universe-factory.net>
2015-02-01 00:21:17 +01:00
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"
#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 {
Chunk::Chunk(const ChunkData *data) {
level = assertValue(data->getRoot().get<NBT::CompoundTag>("Level"));
2015-02-01 14:02:04 +01:00
std::shared_ptr<const NBT::ByteTag> lightPopulatedTag = level->get<NBT::ByteTag>("LightPopulated");
bool lightPopulated = lightPopulatedTag && lightPopulatedTag->getValue();
2015-02-01 05:18:35 +01:00
2018-07-21 18:07:45 +02:00
sections = assertValue(level->get<NBT::ListTag>("Sections"));
const NBT::CompoundTag *lastSection = assertValue(dynamic_cast<const NBT::CompoundTag *>(sections->back().get()));
maxY = (assertValue(lastSection->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
std::memset(blockIDs.get(), 0, maxY * SIZE * SIZE);
std::memset(blockData.get(), 0, maxY * SIZE * SIZE / 2);
2015-02-01 14:35:31 +01:00
std::memset(blockSkyLight.get(), 0xff, maxY * SIZE * SIZE / 2);
std::memset(blockBlockLight.get(), 0, maxY * SIZE * SIZE / 2);
2015-02-01 05:54:42 +01:00
2015-02-01 14:02:04 +01:00
2018-07-21 18:07:45 +02:00
for (auto &sectionTag : *sections) {
const NBT::CompoundTag *section = assertValue(dynamic_cast<const NBT::CompoundTag *>(sectionTag.get()));
2015-02-01 14:35:31 +01:00
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"));
size_t Y = assertValue(section->get<NBT::ByteTag>("Y"))->getValue();
if (blocks->getLength() != SIZE*SIZE*SIZE || data->getLength() != SIZE*SIZE*SIZE/2)
2015-02-01 14:35:31 +01:00
throw std::invalid_argument("corrupt chunk data");
if (lightPopulated) {
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"));
if (blockLight->getLength() != SIZE*SIZE*SIZE/2 || skyLight->getLength() != SIZE*SIZE*SIZE/2)
throw std::invalid_argument("corrupt chunk data");
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:35:31 +01:00
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);
2015-02-01 14:02:04 +01:00
}
2015-02-01 00:21:17 +01:00
}
2015-02-02 00:46:39 +01:00
Block Chunk::getBlock(size_t x, size_t y, size_t z) const {
size_t y2 = y;
if (y2 < maxY-1)
y2++;
unsigned h;
for (h = y; h > 0; h--) {
uint8_t id2 = getBlockAt(x, h, z);
if (id2 != 8 && id2 != 9)
break;
}
return Block(
getBlockAt(x, y, z),
getDataAt(x, y, z),
h,
getBlockLightAt(x, y2, z),
getSkyLightAt(x, y2, z),
getBiomeAt(x, z)
);
}
Chunk::Blocks Chunk::getTopLayer() const {
size_t done = 0;
2015-02-01 14:02:04 +01:00
Blocks ret;
2015-02-01 14:02:04 +01:00
for (ssize_t y = maxY-1; y >= 0; y--) {
if (done == SIZE*SIZE)
break;
2015-02-01 14:02:04 +01:00
for (size_t z = 0; z < SIZE; z++) {
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);
uint8_t data = getDataAt(x, y, z);
const Resource::BlockType *type = Resource::LEGACY_BLOCK_TYPES.types[id][data];
if (!type || !type->opaque)
2015-02-01 14:02:04 +01:00
continue;
2015-02-02 00:46:39 +01:00
ret.blocks[x][z] = getBlock(x, y, z);
2015-02-01 14:02:04 +01:00
done++;
}
}
}
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
}
}
}