forked from cuberite/cuberite
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a basic PalettedBlockArea implementation (cuberite#4377)
- Loading branch information
Showing
9 changed files
with
1,111 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#pragma once | ||
|
||
#include <utility> | ||
#include "BlockState.h" | ||
|
||
|
||
|
||
|
||
|
||
/** Holds a palette that maps block type + state into numbers. | ||
Used primarily by PalettedBlockArea to translate between numeric and stringular block representation. | ||
The object itself provides no thread safety, users of this class need to handle locking, if required. */ | ||
class BlockTypePalette | ||
{ | ||
public: | ||
|
||
/** Create a new empty instance. */ | ||
BlockTypePalette(); | ||
|
||
/** Returns the index of the specified block type name and state. | ||
If the combination is not found, it is added to the palette and the new index is returned. */ | ||
UInt32 index(const AString & aBlockTypeName, const BlockState & aBlockState); | ||
|
||
/** Returns the <index, true> of the specified block type name and state, if it exists. | ||
If the combination is not found, returns <undefined, false>. */ | ||
std::pair<UInt32, bool> maybeIndex(const AString & aBlockTypeName, const BlockState & aBlockState) const; | ||
|
||
/** Returns the total number of entries in the palette. */ | ||
UInt32 count() const; | ||
|
||
/** Returns the blockspec represented by the specified palette index. | ||
The index must be valid (ASSERTed). */ | ||
const std::pair<AString, BlockState> & entry(UInt32 aIndex) const; | ||
|
||
/** Adds missing entries from aOther to this, and returns an index-transform map from aOther to this. | ||
Used when pasting two areas, to transform the src palette to dst palette. */ | ||
std::map<UInt32, UInt32> createTransformMap(const BlockTypePalette & aOther); | ||
|
||
|
||
protected: | ||
|
||
/** The palette. Each item in the vector represents a single entry in the palette, the vector index is the palette index. */ | ||
std::vector<std::pair<AString, BlockState>> mPalette; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.