diff options
Diffstat (limited to 'src/ChunkDef.h')
-rw-r--r-- | src/ChunkDef.h | 182 |
1 files changed, 89 insertions, 93 deletions
diff --git a/src/ChunkDef.h b/src/ChunkDef.h index 7d99ca6e8..eb8545bd6 100644 --- a/src/ChunkDef.h +++ b/src/ChunkDef.h @@ -55,27 +55,27 @@ typedef unsigned char HEIGHTTYPE; /** Wraps the chunk coords into a single structure. */ class cChunkCoords { -public: + public: int m_ChunkX; int m_ChunkZ; - cChunkCoords(int a_ChunkX, int a_ChunkZ) : m_ChunkX(a_ChunkX), m_ChunkZ(a_ChunkZ) {} + cChunkCoords(int a_ChunkX, int a_ChunkZ) : + m_ChunkX(a_ChunkX), m_ChunkZ(a_ChunkZ) + { + } - bool operator == (const cChunkCoords & a_Other) const + bool operator==(const cChunkCoords & a_Other) const { return ((m_ChunkX == a_Other.m_ChunkX) && (m_ChunkZ == a_Other.m_ChunkZ)); } - bool operator != (const cChunkCoords & a_Other) const - { - return !(operator == (a_Other)); - } + bool operator!=(const cChunkCoords & a_Other) const { return !(operator==(a_Other)); } /** Simple comparison, to support ordering. */ - bool operator < (const cChunkCoords & a_Other) const + bool operator<(const cChunkCoords & a_Other) const { if (a_Other.m_ChunkX == m_ChunkX) { @@ -89,11 +89,8 @@ public: /** Returns a string that describes the chunk coords, suitable for logging. */ - AString ToString() const - { - return fmt::format(FMT_STRING("[{}, {}]"), m_ChunkX, m_ChunkZ); - } -} ; + AString ToString() const { return fmt::format(FMT_STRING("[{}, {}]"), m_ChunkX, m_ChunkZ); } +}; @@ -102,14 +99,14 @@ public: /** Implements custom fmtlib formatting for cChunkCoords. */ namespace fmt { - template <> struct formatter<cChunkCoords>: formatter<int> +template <> struct formatter<cChunkCoords> : formatter<int> +{ + auto format(cChunkCoords a_Coords, format_context & a_Ctx) { - auto format(cChunkCoords a_Coords, format_context & a_Ctx) - { - return format_to(a_Ctx.out(), "[{}, {}]", a_Coords.m_ChunkX, a_Coords.m_ChunkZ); - } - }; -} + return format_to(a_Ctx.out(), "[{}, {}]", a_Coords.m_ChunkX, a_Coords.m_ChunkZ); + } +}; +} // namespace fmt @@ -118,8 +115,7 @@ namespace fmt /** Constants used throughout the code, useful typedefs and utility functions */ class cChunkDef { -public: - + public: // Chunk dimensions: static const int Width = 16; static const int Height = 256; @@ -128,7 +124,8 @@ public: static const int SectionHeight = 16; static const size_t NumSections = (cChunkDef::Height / SectionHeight); - /** The type used for any heightmap operations and storage; idx = x + Width * z; Height points to the highest non-air block in the column */ + /** The type used for any heightmap operations and storage; idx = x + Width * z; Height points to the highest + * non-air block in the column */ typedef HEIGHTTYPE HeightMap[Width * Width]; /** The type used for any biomemap operations and storage inside Cuberite, @@ -144,7 +141,13 @@ public: /** Converts absolute block coords into relative (chunk + block) coords: */ - inline static void AbsoluteToRelative(/* in-out */ int & a_X, int & a_Y, int & a_Z, /* out */ int & a_ChunkX, int & a_ChunkZ) + inline static void AbsoluteToRelative( + /* in-out */ int & a_X, + int & a_Y, + int & a_Z, + /* out */ int & a_ChunkX, + int & a_ChunkZ + ) { UNUSED(a_Y); BlockToChunk(a_X, a_Z, a_ChunkX, a_ChunkZ); @@ -166,7 +169,11 @@ public: /** Converts the absolute coords into coords relative to the specified chunk. */ inline static Vector3i AbsoluteToRelative(Vector3i a_BlockPosition, cChunkCoords a_ChunkPos) { - return { a_BlockPosition.x - a_ChunkPos.m_ChunkX * Width, a_BlockPosition.y, a_BlockPosition.z - a_ChunkPos.m_ChunkZ * Width }; + return { + a_BlockPosition.x - a_ChunkPos.m_ChunkX * Width, + a_BlockPosition.y, + a_BlockPosition.z - a_ChunkPos.m_ChunkZ * Width + }; } @@ -189,20 +196,13 @@ public: /** Validates a width-coordinate. Returns false if width-coordiante is out of width bounds */ - inline static bool IsValidWidth(int a_Width) - { - return ((a_Width >= 0) && (a_Width < Width)); - } + inline static bool IsValidWidth(int a_Width) { return ((a_Width >= 0) && (a_Width < Width)); } /** Validates a chunk relative coordinate. Returns false if the coordiante is out of bounds for a chunk. */ inline static bool IsValidRelPos(Vector3i a_RelPos) { - return ( - IsValidWidth(a_RelPos.x) && - IsValidHeight(a_RelPos) && - IsValidWidth(a_RelPos.z) - ); + return (IsValidWidth(a_RelPos.x) && IsValidHeight(a_RelPos) && IsValidWidth(a_RelPos.z)); } @@ -211,7 +211,7 @@ public: { // This version is deprecated in favor of the vector version // If you're developing new code, use the other version. - const auto ChunkCoords = BlockToChunk({ a_X, 0, a_Z }); + const auto ChunkCoords = BlockToChunk({a_X, 0, a_Z}); a_ChunkX = ChunkCoords.m_ChunkX; a_ChunkZ = ChunkCoords.m_ChunkZ; } @@ -220,44 +220,41 @@ public: /** The Y coordinate of a_Pos is ignored */ inline static cChunkCoords BlockToChunk(const Vector3i a_Position) { - return { FAST_FLOOR_DIV(a_Position.x, Width), FAST_FLOOR_DIV(a_Position.z, Width) }; + return {FAST_FLOOR_DIV(a_Position.x, Width), FAST_FLOOR_DIV(a_Position.z, Width)}; } inline static size_t MakeIndex(int x, int y, int z) { - ASSERT(IsValidRelPos({ x, y, z })); + ASSERT(IsValidRelPos({x, y, z})); - #if AXIS_ORDER == AXIS_ORDER_XZY - // For some reason, NOT using the Horner schema is faster. Weird. - return static_cast<size_t>(x + (z * Width) + (y * Width * Width)); // 1.2 uses XZY - #elif AXIS_ORDER == AXIS_ORDER_YZX - return static_cast<size_t>(y + (z * Width) + (x * Height * Width)); // 1.1 uses YZX - #endif +#if AXIS_ORDER == AXIS_ORDER_XZY + // For some reason, NOT using the Horner schema is faster. Weird. + return static_cast<size_t>(x + (z * Width) + (y * Width * Width)); // 1.2 uses XZY +#elif AXIS_ORDER == AXIS_ORDER_YZX + return static_cast<size_t>(y + (z * Width) + (x * Height * Width)); // 1.1 uses YZX +#endif } - inline static size_t MakeIndex(Vector3i a_RelPos) - { - return MakeIndex(a_RelPos.x, a_RelPos.y, a_RelPos.z); - } + inline static size_t MakeIndex(Vector3i a_RelPos) { return MakeIndex(a_RelPos.x, a_RelPos.y, a_RelPos.z); } inline static Vector3i IndexToCoordinate(size_t index) { - #if AXIS_ORDER == AXIS_ORDER_XZY - return Vector3i( // 1.2 - static_cast<int>(index % cChunkDef::Width), // X - static_cast<int>(index / (cChunkDef::Width * cChunkDef::Width)), // Y - static_cast<int>((index / cChunkDef::Width) % cChunkDef::Width) // Z - ); - #elif AXIS_ORDER == AXIS_ORDER_YZX - return Vector3i( // 1.1 - static_cast<int>(index / (cChunkDef::Height * cChunkDef::Width)), // X - static_cast<int>(index % cChunkDef::Height), // Y - static_cast<int>((index / cChunkDef::Height) % cChunkDef::Width) // Z - ); - #endif +#if AXIS_ORDER == AXIS_ORDER_XZY + return Vector3i( // 1.2 + static_cast<int>(index % cChunkDef::Width), // X + static_cast<int>(index / (cChunkDef::Width * cChunkDef::Width)), // Y + static_cast<int>((index / cChunkDef::Width) % cChunkDef::Width) // Z + ); +#elif AXIS_ORDER == AXIS_ORDER_YZX + return Vector3i( // 1.1 + static_cast<int>(index / (cChunkDef::Height * cChunkDef::Width)), // X + static_cast<int>(index % cChunkDef::Height), // Y + static_cast<int>((index / cChunkDef::Height) % cChunkDef::Width) // Z + ); +#endif } @@ -358,7 +355,7 @@ public: { return (a_Buffer[a_Index / 2] >> ((a_Index & 1) * 4)) & 0x0f; } -} ; +}; @@ -368,8 +365,7 @@ public: Used primarily for entity moving while both chunks are locked. */ class cClientDiffCallback { -public: - + public: virtual ~cClientDiffCallback() {} /** Called for clients that are in Chunk1 and not in Chunk2, */ @@ -377,7 +373,7 @@ public: /** Called for clients that are in Chunk2 and not in Chunk1. */ virtual void Added(cClientHandle * a_Client) = 0; -} ; +}; @@ -390,12 +386,8 @@ struct sSetBlock BLOCKTYPE m_BlockType; NIBBLETYPE m_BlockMeta; - sSetBlock(int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta): - m_RelX(a_BlockX), - m_RelY(a_BlockY), - m_RelZ(a_BlockZ), - m_BlockType(a_BlockType), - m_BlockMeta(a_BlockMeta) + sSetBlock(int a_BlockX, int a_BlockY, int a_BlockZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) : + m_RelX(a_BlockX), m_RelY(a_BlockY), m_RelZ(a_BlockZ), m_BlockType(a_BlockType), m_BlockMeta(a_BlockMeta) { cChunkDef::AbsoluteToRelative(m_RelX, m_RelY, m_RelZ, m_ChunkX, m_ChunkZ); } @@ -405,9 +397,20 @@ struct sSetBlock { } - sSetBlock(int a_ChunkX, int a_ChunkZ, int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) : - m_RelX(a_RelX), m_RelY(a_RelY), m_RelZ(a_RelZ), - m_ChunkX(a_ChunkX), m_ChunkZ(a_ChunkZ), + sSetBlock( + int a_ChunkX, + int a_ChunkZ, + int a_RelX, + int a_RelY, + int a_RelZ, + BLOCKTYPE a_BlockType, + NIBBLETYPE a_BlockMeta + ) : + m_RelX(a_RelX), + m_RelY(a_RelY), + m_RelZ(a_RelZ), + m_ChunkX(a_ChunkX), + m_ChunkZ(a_ChunkZ), m_BlockType(a_BlockType), m_BlockMeta(a_BlockMeta) { @@ -426,16 +429,10 @@ struct sSetBlock int GetZ(void) const { return m_RelZ + cChunkDef::Width * m_ChunkZ; } /** Returns the absolute coords of the stored block. */ - Vector3i GetAbsolutePos() const - { - return Vector3i(GetX(), GetY(), GetZ()); - } + Vector3i GetAbsolutePos() const { return Vector3i(GetX(), GetY(), GetZ()); } /** Returns the relative position of the stored block within its chunk. */ - Vector3i GetRelativePos() const - { - return Vector3i(m_RelX, m_RelY, m_RelZ); - } + Vector3i GetRelativePos() const { return Vector3i(m_RelX, m_RelY, m_RelZ); } }; typedef std::vector<sSetBlock> sSetBlockVector; @@ -447,12 +444,12 @@ typedef std::vector<cChunkCoords> cChunkCoordsVector; -/** A simple hash function for chunk coords, we assume that chunk coords won't use more than 16 bits, so the hash is almost an identity. -Used for std::unordered_map<cChunkCoords, ...> */ +/** A simple hash function for chunk coords, we assume that chunk coords won't use more than 16 bits, so the hash is +almost an identity. Used for std::unordered_map<cChunkCoords, ...> */ class cChunkCoordsHash { -public: - size_t operator () (const cChunkCoords & a_Coords) const + public: + size_t operator()(const cChunkCoords & a_Coords) const { return (static_cast<size_t>(a_Coords.m_ChunkX) << 16) ^ static_cast<size_t>(a_Coords.m_ChunkZ); } @@ -465,13 +462,12 @@ public: /** Interface class used as a callback for operations that involve chunk coords */ class cChunkCoordCallback { -public: - + public: virtual ~cChunkCoordCallback() {} /** Called with the chunk's coords, and an optional operation status flag for operations that support it. */ virtual void Call(cChunkCoords a_Coords, bool a_IsSuccess) = 0; -} ; +}; @@ -480,11 +476,11 @@ public: /** Generic template that can store any kind of data together with a triplet of 3 coords */ template <typename X> class cCoordWithData { -public: + public: int x; int y; int z; - X Data; + X Data; cCoordWithData(int a_X, int a_Y, int a_Z) : x(a_X), y(a_Y), z(a_Z), Data() @@ -495,9 +491,9 @@ public: x(a_X), y(a_Y), z(a_Z), Data(a_Data) { } -} ; +}; -typedef cCoordWithData<int> cCoordWithInt; +typedef cCoordWithData<int> cCoordWithInt; -typedef std::list<cCoordWithInt> cCoordWithIntList; +typedef std::list<cCoordWithInt> cCoordWithIntList; typedef std::vector<cCoordWithInt> cCoordWithIntVector; |