summaryrefslogtreecommitdiffstats
path: root/src/BlockTypePalette.cpp
blob: fabf5698e4cf8e9a4ba8f43af4d80b0edcd93eb3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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;
}