FeatherMC/src/world/Chunk.cpp

99 lines
2.2 KiB
C++

#include "Chunk.h"
#include "World.h"
namespace Feather
{
Chunk::Chunk(ChunkPos& pos, NBT::CompoundTag& tag) :
pos(pos)
{
using namespace NBT;
CompoundTag level = tag.GetCompound("Level");
Log::Trace("Loading chunk ({}, {}) from NBT", pos.x, pos.z);
if (pos.x != level.Get<int>("xPos") || pos.z != level.Get<int>("zPos"))
{
Log::Error("Tried to load chunk ({}, {}) from NBT but NBT says chunk pos is ({}, {})",
pos.x, pos.z,
level.Get<int>("xPos"), level.Get<int>("zPos")
);
}
// TODO: Heightmaps
ListTag<CompoundTag> sectionsList = level.GetList<CompoundTag>("Sections");
int n = 0;
for (CompoundTag& sectData : sectionsList)
{
//std::cout << sectData << "\n";
int8_t y = sectData.Get<int8_t>("Y");
if (y < 0 || y >= NUM_SECTIONS) {
Log::Trace("Skipping section {}", y);
continue;
}
Log::Trace("Loading section {}/{} at Y {}", n + 1, NUM_SECTIONS, y);
if (n >= NUM_SECTIONS)
{
Log::Error("Chunk ({}, {}) has more than {} sections!", pos.x, pos.z, NUM_SECTIONS);
break;
}
// Palette
ListTag<CompoundTag> palette = sectData.GetList<CompoundTag>("Palette");
for (int i = 0; i < palette.GetLength(); i++)
{
CompoundTag state = palette[i];
const char* name = state.Get<StringTag>("Name").GetValue();
BlockState stateLookup(name);
int id;
// TODO: handle this in IdMapper
try {
id = World::GLOBAL_PALETTE.GetID(stateLookup);
}
catch (std::out_of_range& err) {
(void)err;
Log::Warn("Chunk Section Palette maps id {} to invalid block state with name '{}'", i, name);
continue;
}
Log::Trace(" {} -> {} {}", i, id, name);
}
// Block States
LongArrayTag blockStates = sectData.Get<LongArrayTag>("BlockStates");
if (!blockStates) {
Log::Warn("Skipping section with no block states.");
continue;
}
int len = blockStates.GetLength();
if (len != ChunkSection::NUM_LONGS) {
Log::Warn("Section {} has {} longs, expected {}", y, len, ChunkSection::NUM_LONGS);
}
ChunkSection section = Sections[y];
for (int i = 0; i < blockStates.GetLength() && i < ChunkSection::NUM_LONGS; i++)
{
section.blockStates[i] = blockStates[i];
}
n++;
}
}
}