From e64603c6b6eac5f28c2ea105256c34982e9bb5c9 Mon Sep 17 00:00:00 2001 From: Alpyne Date: Fri, 30 Oct 2020 18:57:43 -0700 Subject: [PATCH] Move ChunkSection guts to PalettedContainer --- src/DedicatedServer.cpp | 12 ++++++------ src/world/Chunk.cpp | 24 ++++++++++++++++-------- src/world/Chunk.h | 22 +++++++++++++--------- src/world/PalettedContainer.h | 29 +++++++++++++++++++++++++---- 4 files changed, 60 insertions(+), 27 deletions(-) diff --git a/src/DedicatedServer.cpp b/src/DedicatedServer.cpp index 725d7d6..68ff9cb 100644 --- a/src/DedicatedServer.cpp +++ b/src/DedicatedServer.cpp @@ -232,24 +232,24 @@ namespace Feather // Block Count: fudge sections.Write(1024); - const uint8_t bitsPerBlock = sect.bitsPerBlock; + const uint8_t bitsPerBlock = sect.blockStates.bits; sections.Write(bitsPerBlock); - const size_t dataLength = (16 * 16 * 16) * bitsPerBlock / 64; + const size_t dataLength = ChunkSection::NUM_BLOCKS * bitsPerBlock / bitsizeof(uint64); if (bitsPerBlock <= 8) { // Palette - sections.WriteVarInt(sect.localPalette.Size()); + sections.WriteVarInt(sect.blockStates.palette.Size()); - for (size_t j = 0; j < sect.localPalette.Size(); j++) - sections.WriteVarInt(sect.localPalette.ByID(j)); + for (size_t j = 0; j < sect.blockStates.palette.Size(); j++) + sections.WriteVarInt(sect.blockStates.palette.ByID(j)); } sections.WriteVarInt(dataLength); // Can't just copy because endian-ness! for (size_t i = 0; i < dataLength; i++) - sections.Write(sect.blockStates[i]); + sections.Write(sect.blockStates.data[i]); } //Log::Trace("Section bits: {:#b}", sectionsBits); diff --git a/src/world/Chunk.cpp b/src/world/Chunk.cpp index ea0fe24..d6f2db4 100644 --- a/src/world/Chunk.cpp +++ b/src/world/Chunk.cpp @@ -45,7 +45,7 @@ namespace Feather ChunkSection* section = §ions[y]; - // Palette + // Section Palette ListTag palette = sectData.GetList("Palette"); @@ -74,13 +74,13 @@ namespace Feather catch (std::out_of_range& err) { (void)err; int defaultId = World::GLOBAL_PALETTE.GetDefaultID(); - section->localPalette.AddMapping(i, defaultId); + section->blockStates.palette.AddMapping(i, defaultId); Log::Warn("Chunk Section Palette maps id {} to invalid block state with name '{}'", i, name); continue; } // TODO: could use Add() - section->localPalette.AddMapping(i, id); + section->blockStates.palette.AddMapping(i, id); //Log::Trace(" {} -> {} {}", i, id, name); } @@ -93,13 +93,13 @@ namespace Feather Log::Warn("Skipping section with no block states."); continue; } - - Assert(blockStates.GetLength() < ChunkSection::MaximumLongCount); + + Assert(blockStates.GetLength() < section->blockStates.GetNumLongs()); for (size_t i = 0; i < blockStates.GetLength(); i++) - section->blockStates[i] = blockStates[i]; + section->blockStates.data[i] = blockStates[i]; - section->bitsPerBlock = (blockStates.GetLength() * 64) / (16 * 16 * 16); + section->blockStates.SetBits(Math::CeilDiv(blockStates.GetLength() * bitsizeof(int64), ChunkSection::NUM_BLOCKS)); n++; } @@ -107,4 +107,12 @@ namespace Feather } -} \ No newline at end of file + + bool Chunk::SetBlock(BlockPos& pos, BlockState& state) + { + ChunkSection& section = sections[GetSection(pos)]; + + + return false; + } +} diff --git a/src/world/Chunk.h b/src/world/Chunk.h index 71c9dc5..d4c9ed0 100644 --- a/src/world/Chunk.h +++ b/src/world/Chunk.h @@ -1,27 +1,27 @@ #pragma once +#include "Common.h" #include "Types.h" #include "nbt/NBT.h" #include "PalettedContainer.h" #include "block/state/BlockState.h" -#include - namespace Feather { // 16x16x16 sections of a chunk struct ChunkSection { - static constexpr size_t MaximumLongCount = (16 * 16 * 16) * 14 / 64; + static constexpr size_t NUM_BLOCKS = (16 * 16 * 16); - std::array blockStates; + static constexpr size_t MAX_BITS = Math::CeilLog2(NUM_BLOCKS); - // TODO: this should eventually be Palette - // Palette of local indexs to indexs in the global palette - IDMapper localPalette; + static constexpr size_t DEFAULT_BITS = 4; - uint8_t bitsPerBlock; + PalettedContainer blockStates; + + public: + ChunkSection() : blockStates(NUM_BLOCKS, DEFAULT_BITS) {} }; // Chunk: 16x256x16 world chunk @@ -43,5 +43,9 @@ namespace Feather //const NBT::CompoundTag* heightmaps; Chunk(ChunkPos& pos, std::unique_ptr tag); + + inline int GetSection(BlockPos& pos) { return pos.y >> 4; } + + bool SetBlock(BlockPos& pos, BlockState& state); }; -}; \ No newline at end of file +}; diff --git a/src/world/PalettedContainer.h b/src/world/PalettedContainer.h index 16d0fc5..422f6c8 100644 --- a/src/world/PalettedContainer.h +++ b/src/world/PalettedContainer.h @@ -1,13 +1,34 @@ #pragma once +#include "Common.h" #include "Palette.h" +#include + namespace Feather { // A container that maps values from a local palette to a - template - class PalettedContainer + template + struct PalettedContainer { - const Palette& m_globalPalette; + //const Palette& globalPalette; + + static constexpr size_t NUM_LONGS = Math::CeilDiv(MaxSize * MaxBits, bitsizeof(uint64)); + + std::array data; + + // TODO: this could eventually be Palette to avoid double lookup + // Palette of local indexs to indexs in the global palette + IDMapper palette; + + uint size = MaxSize; + uint8 bits = MaxBits; + + public: + PalettedContainer(uint size, uint8 bits) : size(size), bits(bits), data() {} + + inline void SetBits(uint8 nbits) { bits = nbits; } + + inline constexpr size_t GetNumLongs() { return NUM_LONGS; } }; -} \ No newline at end of file +}