diff options
author | Mattes D <github@xoft.cz> | 2019-08-28 08:29:02 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-28 08:29:02 +0200 |
commit | 2504538a3a164f27a96f413f5b389f8dad6b2cac (patch) | |
tree | e3478f6779a4dab9ed835edea23c83d1a019a31b /src/BlockTypePalette.cpp | |
parent | Improved testing framework. (#4376) (diff) | |
download | cuberite-2504538a3a164f27a96f413f5b389f8dad6b2cac.tar cuberite-2504538a3a164f27a96f413f5b389f8dad6b2cac.tar.gz cuberite-2504538a3a164f27a96f413f5b389f8dad6b2cac.tar.bz2 cuberite-2504538a3a164f27a96f413f5b389f8dad6b2cac.tar.lz cuberite-2504538a3a164f27a96f413f5b389f8dad6b2cac.tar.xz cuberite-2504538a3a164f27a96f413f5b389f8dad6b2cac.tar.zst cuberite-2504538a3a164f27a96f413f5b389f8dad6b2cac.zip |
Diffstat (limited to '')
-rw-r--r-- | src/BlockTypePalette.cpp | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/src/BlockTypePalette.cpp b/src/BlockTypePalette.cpp new file mode 100644 index 000000000..fabf5698e --- /dev/null +++ b/src/BlockTypePalette.cpp @@ -0,0 +1,80 @@ +#include "Globals.h" +#include "BlockTypePalette.h" + + + + +BlockTypePalette::BlockTypePalette() +{ + // Nothing needed yet +} + + + + + +UInt32 BlockTypePalette::index(const AString & aBlockTypeName, const BlockState & aBlockState) +{ + auto idx = maybeIndex(aBlockTypeName, aBlockState); + if (idx.second) + { + return idx.first; + } + + // Not found, append: + mPalette.push_back(std::make_pair(aBlockTypeName, aBlockState)); + return static_cast<UInt32>(mPalette.size() - 1); +} + + + + + +std::pair<UInt32, bool> BlockTypePalette::maybeIndex(const AString & aBlockTypeName, const BlockState & aBlockState) const +{ + auto count = mPalette.size(); + for (size_t idx = 0; idx < count; ++idx) + { + const auto & entry = mPalette[idx]; + if ((entry.first == aBlockTypeName) && (entry.second == aBlockState)) + { + return std::make_pair(static_cast<UInt32>(idx), true); + } + } + return std::make_pair(0, false); +} + + + + + +UInt32 BlockTypePalette::count() const +{ + return static_cast<UInt32>(mPalette.size()); +} + + + + + +const std::pair<AString, BlockState> & BlockTypePalette::entry(UInt32 aIndex) const +{ + ASSERT(aIndex < mPalette.size()); + return mPalette[aIndex]; +} + + + + + +std::map<UInt32, UInt32> BlockTypePalette::createTransformMap(const BlockTypePalette & aOther) +{ + std::map<UInt32, UInt32> res; + auto numIndices = aOther.count(); + for (UInt32 idx = 0; idx < numIndices; ++idx) + { + const auto & e = aOther.mPalette[idx]; + res[idx] = index(e.first, e.second); + } + return res; +} |