From 2b06f0c1495dd3f2035972eff65bc4b86f6e3d73 Mon Sep 17 00:00:00 2001 From: DankParrot Date: Thu, 13 Aug 2020 18:47:19 -0700 Subject: [PATCH] BlockState: store properties --- src/block/state/BlockState.h | 25 +++++++++++++++++++++++-- src/data/Registry.cpp | 17 ++++++++++++++++- src/world/Chunk.cpp | 10 ++++++++++ 3 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/block/state/BlockState.h b/src/block/state/BlockState.h index 2b962dc..d6bc0d6 100644 --- a/src/block/state/BlockState.h +++ b/src/block/state/BlockState.h @@ -4,21 +4,35 @@ #include #include #include +#include namespace Feather { class BlockState { + friend struct std::hash; + // PLACEHOLDER std::string_view m_name; + std::map m_properties; public: BlockState(std::string_view name) : m_name(name) {} inline const std::string_view GetName() const { return m_name; } + inline void AddProperty(std::string_view key, std::string_view value) { m_properties.insert({ key, value }); } + inline bool operator ==(const BlockState that) const { - return m_name == that.m_name; + if (m_name != that.m_name) return false; + + for (auto& [key, value] : that.m_properties) + { + if (!m_properties.contains(key) || m_properties.at(key) != value) + return false; + } + + return true; } }; } @@ -31,6 +45,13 @@ struct std::hash using std::hash; using std::string_view; - return hash()(state.GetName()); + size_t accum = 0; + accum += hash()(state.GetName()); + + // pretty basic hash, sum of all keys and values + block name + for (auto& [key, value] : state.m_properties) + accum += hash()(key) + hash()(value); + + return accum; } }; \ No newline at end of file diff --git a/src/data/Registry.cpp b/src/data/Registry.cpp index 9fcca9a..085f6c8 100644 --- a/src/data/Registry.cpp +++ b/src/data/Registry.cpp @@ -42,13 +42,28 @@ namespace Feather for (auto& block : doc.GetObject()) { - //Log::Info("{}: {} states", m.name.GetString(), m.value["states"].Size()); + // TODO + //if (block.value.HasMember("properties")) {} + + //Log::Info("{}: {} states", block.name.GetString(), block.value["states"].Size()); for (auto& stateData : block.value["states"].GetArray()) { std::string* name = new std::string(block.name.GetString()); BlockState state(*name); + + if (stateData.HasMember("properties")) + { + for (auto& prop : stateData["properties"].GetObject()) + { + std::string* propName = new std::string(prop.name.GetString()); + std::string* propValue = new std::string(prop.value.GetString()); + state.AddProperty(*propName, *propValue); + } + } + BLOCK_STATES.AddMapping(stateData["id"].GetInt(), state); } + } Log::Info("Loaded {} block states.", BLOCK_STATES.Size()); diff --git a/src/world/Chunk.cpp b/src/world/Chunk.cpp index 01e495e..cf778f2 100644 --- a/src/world/Chunk.cpp +++ b/src/world/Chunk.cpp @@ -55,6 +55,16 @@ namespace Feather const char* name = state.Get("Name").GetValue(); BlockState stateLookup(name); + + CompoundTag properties = state.GetCompound("Properties"); + if (properties) + { + for (auto& prop : properties) + { + stateLookup.AddProperty(prop.GetName(), prop.GetValueString()); + } + } + int id; // TODO: handle this in IdMapper