FeatherMC/src/world/Chunk.h

61 lines
1.3 KiB
C++

#pragma once
#include "Common.h"
#include "Types.h"
#include "nbt/NBT.h"
#include "PalettedContainer.h"
#include "block/state/BlockState.h"
namespace Feather
{
// 16x16x16 sections of a chunk
struct ChunkSection
{
static constexpr size_t NUM_BLOCKS = (16 * 16 * 16);
static constexpr size_t MAX_BITS = Math::CeilLog2(NUM_BLOCKS);
static constexpr size_t DEFAULT_BITS = 4;
PalettedContainer<uint64, NUM_BLOCKS, MAX_BITS> blockStates;
public:
ChunkSection() : blockStates(NUM_BLOCKS, DEFAULT_BITS) {}
};
// Chunk: 16x256x16 world chunk
class Chunk
{
public:
static constexpr int NUM_SECTIONS = 16;
// Each chunk is made of up 16 ChunkSections
ChunkSection sections[NUM_SECTIONS];
ChunkPos pos;
// TODO: Nuke this shit!
std::unique_ptr<NBT::CompoundTag> nbt;
// TODO: save heightmaps here
// UNSERIALIZED
//const NBT::CompoundTag* heightmaps;
Chunk(ChunkPos& pos, std::unique_ptr<NBT::CompoundTag> tag);
inline int GetSection(const BlockPos& pos) { return pos.y >> 4; }
inline int GetSectionIndex(const BlockPos& pos)
{
int x = pos.x & 0xF;
int y = pos.y & 0xF;
int z = pos.z & 0xF;
return y << 8 | z << 4 | x;
}
bool SetBlock(const BlockPos& pos, const BlockState& state);
};
};