Skip to content

Commit

Permalink
Added a basic PalettedBlockArea implementation (cuberite#4377)
Browse files Browse the repository at this point in the history
  • Loading branch information
madmaxoft authored Aug 28, 2019
1 parent 74579fb commit 2504538
Show file tree
Hide file tree
Showing 9 changed files with 1,111 additions and 1 deletion.
80 changes: 80 additions & 0 deletions src/BlockTypePalette.cpp
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;
}
44 changes: 44 additions & 0 deletions src/BlockTypePalette.h
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;
};
4 changes: 4 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ SET (SRCS
BlockID.cpp
BlockInfo.cpp
BlockState.cpp
BlockTypePalette.cpp
BlockTypeRegistry.cpp
BrewingRecipes.cpp
Broadcaster.cpp
Expand Down Expand Up @@ -59,6 +60,7 @@ SET (SRCS
MonsterConfig.cpp
NetherPortalScanner.cpp
OverridesSettingsRepository.cpp
PalettedBlockArea.cpp
ProbabDistrib.cpp
RankManager.cpp
RCONServer.cpp
Expand Down Expand Up @@ -87,6 +89,7 @@ SET (HDRS
BlockInfo.h
BlockState.h
BlockTracer.h
BlockTypePalette.h
BlockTypeRegistry.h
BrewingRecipes.h
BoundingBox.h
Expand Down Expand Up @@ -140,6 +143,7 @@ SET (HDRS
NetherPortalScanner.h
OpaqueWorld.h
OverridesSettingsRepository.h
PalettedBlockArea.h
ProbabDistrib.h
RankManager.h
RCONServer.h
Expand Down
22 changes: 22 additions & 0 deletions src/Cuboid.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,28 @@ class cCuboid
/** If needed, expands the cuboid so that it contains the specified point. Assumes sorted. Doesn't contract. */
void Engulf(Vector3i a_Point);

// tolua_end

/** Checks the two cuboids for equality. */
bool operator == (const cCuboid & aOther) const
{
return (
(p1.x == aOther.p1.x) &&
(p1.y == aOther.p1.y) &&
(p1.z == aOther.p1.z) &&
(p2.x == aOther.p2.x) &&
(p2.y == aOther.p2.y) &&
(p2.z == aOther.p2.z)
);
}

bool operator != (const cCuboid & aOther) const
{
return !operator ==(aOther);
}


// tolua_begin

private:

Expand Down
Loading

0 comments on commit 2504538

Please sign in to comment.