2019-08-28 02:29:02 -04:00
|
|
|
#include "Globals.h"
|
|
|
|
#include "BlockTypePalette.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-12-01 08:41:46 -05:00
|
|
|
BlockTypePalette::BlockTypePalette():
|
|
|
|
mMaxIndex(0)
|
2019-08-28 02:29:02 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
UInt32 BlockTypePalette::index(const AString & aBlockTypeName, const BlockState & aBlockState)
|
|
|
|
{
|
|
|
|
auto idx = maybeIndex(aBlockTypeName, aBlockState);
|
|
|
|
if (idx.second)
|
|
|
|
{
|
|
|
|
return idx.first;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not found, append:
|
2019-12-01 08:41:46 -05:00
|
|
|
auto index = mMaxIndex++;
|
|
|
|
mBlockToNumber[aBlockTypeName][aBlockState] = index;
|
|
|
|
mNumberToBlock[index] = {aBlockTypeName, aBlockState};
|
|
|
|
return index;
|
2019-08-28 02:29:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::pair<UInt32, bool> BlockTypePalette::maybeIndex(const AString & aBlockTypeName, const BlockState & aBlockState) const
|
|
|
|
{
|
2019-12-01 08:41:46 -05:00
|
|
|
auto itr1 = mBlockToNumber.find(aBlockTypeName);
|
|
|
|
if (itr1 == mBlockToNumber.end())
|
2019-08-28 02:29:02 -04:00
|
|
|
{
|
2019-12-01 08:41:46 -05:00
|
|
|
return {0, false};
|
|
|
|
}
|
|
|
|
auto itr2 = itr1->second.find(aBlockState);
|
|
|
|
if (itr2 == itr1->second.end())
|
|
|
|
{
|
|
|
|
return {0, false};
|
2019-08-28 02:29:02 -04:00
|
|
|
}
|
2019-12-01 08:41:46 -05:00
|
|
|
return {itr2->second, true};
|
2019-08-28 02:29:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
UInt32 BlockTypePalette::count() const
|
|
|
|
{
|
2019-12-01 08:41:46 -05:00
|
|
|
return static_cast<UInt32>(mNumberToBlock.size());
|
2019-08-28 02:29:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const std::pair<AString, BlockState> & BlockTypePalette::entry(UInt32 aIndex) const
|
|
|
|
{
|
2019-12-01 08:41:46 -05:00
|
|
|
auto itr = mNumberToBlock.find(aIndex);
|
|
|
|
if (itr == mNumberToBlock.end())
|
|
|
|
{
|
|
|
|
throw NoSuchIndexException(aIndex);
|
|
|
|
}
|
|
|
|
return itr->second;
|
2019-08-28 02:29:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-12-01 08:41:46 -05:00
|
|
|
std::map<UInt32, UInt32> BlockTypePalette::createTransformMapAddMissing(const BlockTypePalette & aFrom)
|
2019-08-28 02:29:02 -04:00
|
|
|
{
|
|
|
|
std::map<UInt32, UInt32> res;
|
2019-12-01 08:41:46 -05:00
|
|
|
for (const auto & fromEntry: aFrom.mNumberToBlock)
|
2019-08-28 02:29:02 -04:00
|
|
|
{
|
2019-12-01 08:41:46 -05:00
|
|
|
auto fromIndex = fromEntry.first;
|
|
|
|
const auto & blockTypeName = fromEntry.second.first;
|
|
|
|
const auto & blockState = fromEntry.second.second;
|
|
|
|
res[fromIndex] = index(blockTypeName, blockState);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::map<UInt32, UInt32> BlockTypePalette::createTransformMapWithFallback(const BlockTypePalette & aFrom, UInt32 aFallbackIndex) const
|
|
|
|
{
|
|
|
|
std::map<UInt32, UInt32> res;
|
|
|
|
for (const auto & fromEntry: aFrom.mNumberToBlock)
|
|
|
|
{
|
|
|
|
auto fromIndex = fromEntry.first;
|
|
|
|
const auto & blockTypeName = fromEntry.second.first;
|
|
|
|
const auto & blockState = fromEntry.second.second;
|
|
|
|
auto thisIndex = maybeIndex(blockTypeName, blockState);
|
|
|
|
if (thisIndex.second)
|
|
|
|
{
|
|
|
|
// The entry was found in this
|
|
|
|
res[fromIndex] = thisIndex.first;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// The entry was NOT found in this, replace with fallback:
|
|
|
|
res[fromIndex] = aFallbackIndex;
|
|
|
|
}
|
2019-08-28 02:29:02 -04:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|