2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
// ChunkDef.h
|
|
|
|
|
|
|
|
// Interfaces to helper types for chunk definitions. Most modules want to include this instead of cChunk.h
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Used to smoothly convert to new axis ordering. One will be removed when deemed stable.
|
2014-07-17 10:33:09 -04:00
|
|
|
#define AXIS_ORDER_YZX 1 // Original (1.1-)
|
|
|
|
#define AXIS_ORDER_XZY 2 // New (1.2+)
|
2012-06-14 09:06:06 -04:00
|
|
|
#define AXIS_ORDER AXIS_ORDER_XZY
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// fwd
|
|
|
|
class cBlockEntity;
|
|
|
|
class cEntity;
|
|
|
|
class cClientHandle;
|
|
|
|
class cBlockEntity;
|
2017-08-17 09:48:38 -04:00
|
|
|
class cChunkCoords;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2019-09-27 11:51:44 -04:00
|
|
|
using OwnedEntity = std::unique_ptr<cEntity>;
|
|
|
|
using cEntityList = std::vector<OwnedEntity>;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-07-02 12:38:30 -04:00
|
|
|
// tolua_begin
|
2012-07-02 12:30:17 -04:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** The datatype used by blockdata */
|
2012-08-03 07:53:11 -04:00
|
|
|
typedef unsigned char BLOCKTYPE;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** The datatype used by nibbledata (meta, light, skylight) */
|
2012-06-14 09:06:06 -04:00
|
|
|
typedef unsigned char NIBBLETYPE;
|
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** The type used by the heightmap */
|
2012-06-14 09:06:06 -04:00
|
|
|
typedef unsigned char HEIGHTTYPE;
|
|
|
|
|
2012-07-02 12:38:30 -04:00
|
|
|
// tolua_end
|
2012-07-02 12:30:17 -04:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2017-08-17 09:48:38 -04:00
|
|
|
|
|
|
|
class cChunkCoords
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
int m_ChunkX;
|
|
|
|
int m_ChunkZ;
|
|
|
|
|
|
|
|
cChunkCoords(int a_ChunkX, int a_ChunkZ) : m_ChunkX(a_ChunkX), m_ChunkZ(a_ChunkZ) {}
|
|
|
|
|
2019-10-11 05:02:53 -04:00
|
|
|
|
2017-08-17 09:48:38 -04:00
|
|
|
bool operator == (const cChunkCoords & a_Other) const
|
|
|
|
{
|
|
|
|
return ((m_ChunkX == a_Other.m_ChunkX) && (m_ChunkZ == a_Other.m_ChunkZ));
|
|
|
|
}
|
2019-09-01 03:30:00 -04:00
|
|
|
|
2019-10-11 05:02:53 -04:00
|
|
|
|
2019-09-07 17:02:05 -04:00
|
|
|
bool operator != (const cChunkCoords & a_Other) const
|
|
|
|
{
|
|
|
|
return !(operator == (a_Other));
|
|
|
|
}
|
|
|
|
|
2019-10-11 05:02:53 -04:00
|
|
|
|
|
|
|
/** Simple comparison, to support ordering. */
|
|
|
|
bool operator < (const cChunkCoords & a_Other) const
|
|
|
|
{
|
|
|
|
if (a_Other.m_ChunkX == m_ChunkX)
|
|
|
|
{
|
|
|
|
return (m_ChunkZ < a_Other.m_ChunkZ);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return (m_ChunkX < a_Other.m_ChunkX);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-01 03:30:00 -04:00
|
|
|
/** Returns a string that describes the chunk coords, suitable for logging. */
|
|
|
|
AString ToString() const
|
|
|
|
{
|
|
|
|
return Printf("[%d, %d]", m_ChunkX, m_ChunkZ);
|
|
|
|
}
|
2017-08-17 09:48:38 -04:00
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-07-24 17:30:49 -04:00
|
|
|
/** Non-owning view of a chunk's client handles. */
|
|
|
|
class cChunkClientHandles
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using const_iterator = std::vector<cClientHandle *>::const_iterator;
|
|
|
|
using iterator = const_iterator;
|
|
|
|
|
|
|
|
explicit cChunkClientHandles(const std::vector<cClientHandle *> & a_Container):
|
|
|
|
m_Begin(a_Container.cbegin()),
|
|
|
|
m_End(a_Container.cend())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
const_iterator begin() const { return m_Begin; }
|
|
|
|
const_iterator cbegin() const { return m_Begin; }
|
|
|
|
|
|
|
|
const_iterator end() const { return m_End; }
|
|
|
|
const_iterator cend() const { return m_End; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
const_iterator m_Begin, m_End;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Constants used throughout the code, useful typedefs and utility functions */
|
2012-06-14 09:06:06 -04:00
|
|
|
class cChunkDef
|
|
|
|
{
|
|
|
|
public:
|
2014-03-09 16:58:12 -04:00
|
|
|
// Chunk dimensions:
|
|
|
|
static const int Width = 16;
|
|
|
|
static const int Height = 256;
|
|
|
|
static const int NumBlocks = Width * Height * Width;
|
2015-07-31 10:49:10 -04:00
|
|
|
|
|
|
|
/** If the data is collected into a single buffer, how large it needs to be: */
|
2014-03-09 16:58:12 -04:00
|
|
|
static const int BlockDataSize = cChunkDef::NumBlocks * 2 + (cChunkDef::NumBlocks / 2); // 2.5 * numblocks
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** The type used for any heightmap operations and storage; idx = x + Width * z; Height points to the highest non-air block in the column */
|
2012-06-14 09:06:06 -04:00
|
|
|
typedef HEIGHTTYPE HeightMap[Width * Width];
|
2014-10-11 22:39:55 -04:00
|
|
|
|
2015-09-25 04:14:17 -04:00
|
|
|
/** The type used for any biomemap operations and storage inside Cuberite,
|
|
|
|
using Cuberite biomes (need not correspond to client representation!)
|
2015-07-31 10:49:10 -04:00
|
|
|
idx = x + Width * z */
|
2012-06-14 09:06:06 -04:00
|
|
|
typedef EMCSBiome BiomeMap[Width * Width];
|
2014-04-25 16:22:43 -04:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** The type used for block type operations and storage, AXIS_ORDER ordering */
|
2012-06-14 09:06:06 -04:00
|
|
|
typedef BLOCKTYPE BlockTypes[NumBlocks];
|
2014-04-25 16:22:43 -04:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** The type used for block data in nibble format, AXIS_ORDER ordering */
|
2012-06-14 09:06:06 -04:00
|
|
|
typedef NIBBLETYPE BlockNibbles[NumBlocks / 2];
|
|
|
|
|
2014-04-25 16:22:43 -04:00
|
|
|
/** The storage wrapper used for compressed blockdata residing in RAMz */
|
|
|
|
typedef std::vector<BLOCKTYPE> COMPRESSED_BLOCKTYPE;
|
|
|
|
|
|
|
|
/** The storage wrapper used for compressed nibbledata residing in RAMz */
|
|
|
|
typedef std::vector<NIBBLETYPE> COMPRESSED_NIBBLETYPE;
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Converts absolute block coords into relative (chunk + block) coords: */
|
2014-07-21 09:19:48 -04:00
|
|
|
inline static void AbsoluteToRelative(/* in-out */ int & a_X, int & a_Y, int & a_Z, /* out */ int & a_ChunkX, int & a_ChunkZ)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-02-23 15:23:35 -05:00
|
|
|
UNUSED(a_Y);
|
2013-08-03 14:05:07 -04:00
|
|
|
BlockToChunk(a_X, a_Z, a_ChunkX, a_ChunkZ);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
a_X = a_X - a_ChunkX * Width;
|
|
|
|
a_Z = a_Z - a_ChunkZ * Width;
|
|
|
|
}
|
2014-10-11 22:39:55 -04:00
|
|
|
|
2019-10-11 05:02:53 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Converts the specified absolute position into a relative position within its chunk.
|
|
|
|
Use BlockToChunk to query the chunk coords. */
|
2015-04-25 19:38:41 -04:00
|
|
|
inline static Vector3i AbsoluteToRelative(Vector3i a_BlockPosition)
|
|
|
|
{
|
2019-10-11 05:02:53 -04:00
|
|
|
cChunkCoords chunkPos = BlockToChunk(a_BlockPosition);
|
|
|
|
return AbsoluteToRelative(a_BlockPosition, chunkPos);
|
2015-04-25 19:38:41 -04:00
|
|
|
}
|
|
|
|
|
2019-10-11 05:02:53 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Converts the absolute coords into coords relative to the specified chunk. */
|
2017-11-20 06:13:11 -05:00
|
|
|
inline static Vector3i AbsoluteToRelative(Vector3i a_BlockPosition, cChunkCoords a_ChunkPos)
|
2015-04-25 19:38:41 -04:00
|
|
|
{
|
2017-11-20 06:13:11 -05:00
|
|
|
return {a_BlockPosition.x - a_ChunkPos.m_ChunkX * Width, a_BlockPosition.y, a_BlockPosition.z - a_ChunkPos.m_ChunkZ * Width};
|
2015-04-25 19:38:41 -04:00
|
|
|
}
|
|
|
|
|
2019-10-11 05:02:53 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2015-02-08 11:35:10 -05:00
|
|
|
/** Converts relative block coordinates into absolute coordinates with a known chunk location */
|
2019-10-11 05:02:53 -04:00
|
|
|
inline static Vector3i RelativeToAbsolute(Vector3i a_RelBlockPosition, cChunkCoords a_ChunkCoords)
|
2015-02-08 11:35:10 -05:00
|
|
|
{
|
2019-10-11 05:02:53 -04:00
|
|
|
return Vector3i(
|
|
|
|
a_RelBlockPosition.x + a_ChunkCoords.m_ChunkX * Width,
|
|
|
|
a_RelBlockPosition.y,
|
|
|
|
a_RelBlockPosition.z + a_ChunkCoords.m_ChunkZ * Width
|
|
|
|
);
|
2015-02-08 11:35:10 -05:00
|
|
|
}
|
|
|
|
|
2019-10-11 05:02:53 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-11-10 10:11:47 -05:00
|
|
|
/** Validates a height-coordinate. Returns false if height-coordiante is out of height bounds */
|
|
|
|
inline static bool IsValidHeight(int a_Height)
|
|
|
|
{
|
|
|
|
return ((a_Height >= 0) && (a_Height < Height));
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2015-11-22 19:26:17 -05:00
|
|
|
/** 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));
|
|
|
|
}
|
2014-10-11 22:39:55 -04:00
|
|
|
|
2018-02-04 17:15:31 -05:00
|
|
|
/** 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.y) &&
|
|
|
|
IsValidWidth(a_RelPos.z)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Converts absolute block coords to chunk coords: */
|
2013-08-03 14:05:07 -04:00
|
|
|
inline static void BlockToChunk(int a_X, int a_Z, int & a_ChunkX, int & a_ChunkZ)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2017-08-17 09:48:38 -04:00
|
|
|
// This version is deprecated in favor of the vector version
|
|
|
|
// If you're developing new code, use the other version.
|
|
|
|
auto ChunkCoords = BlockToChunk({a_X, 0, a_Z});
|
|
|
|
a_ChunkX = ChunkCoords.m_ChunkX;
|
|
|
|
a_ChunkZ = ChunkCoords.m_ChunkZ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** The Y coordinate of a_Pos is ignored */
|
|
|
|
inline static cChunkCoords BlockToChunk(Vector3i a_Pos)
|
|
|
|
{
|
|
|
|
cChunkCoords Chunk(a_Pos.x / Width, a_Pos.z / Width);
|
|
|
|
if ((a_Pos.x < 0) && (a_Pos.x % Width != 0))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2017-08-17 09:48:38 -04:00
|
|
|
Chunk.m_ChunkX--;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2017-08-17 09:48:38 -04:00
|
|
|
if ((a_Pos.z < 0) && (a_Pos.z % Width != 0))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2017-08-17 09:48:38 -04:00
|
|
|
Chunk.m_ChunkZ--;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2017-08-17 09:48:38 -04:00
|
|
|
return Chunk;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-21 09:19:48 -04:00
|
|
|
inline static int MakeIndex(int x, int y, int z)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-10-21 15:38:31 -04:00
|
|
|
if (
|
|
|
|
(x < Width) && (x > -1) &&
|
|
|
|
(y < Height) && (y > -1) &&
|
|
|
|
(z < Width) && (z > -1)
|
|
|
|
)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-03-10 16:18:53 -04:00
|
|
|
return MakeIndexNoCheck(x, y, z);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2018-09-24 16:33:39 -04:00
|
|
|
FLOGERROR("cChunkDef::MakeIndex(): coords out of range: {0}; returning fake index 0", Vector3i{x, y, z});
|
2013-10-21 15:38:31 -04:00
|
|
|
ASSERT(!"cChunkDef::MakeIndex(): coords out of chunk range!");
|
2013-12-20 10:15:39 -05:00
|
|
|
return 0;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-11 15:33:26 -04:00
|
|
|
inline static int MakeIndexNoCheck(int x, int y, int z)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
#if AXIS_ORDER == AXIS_ORDER_XZY
|
|
|
|
// For some reason, NOT using the Horner schema is faster. Weird.
|
2014-07-17 13:13:23 -04:00
|
|
|
return x + (z * cChunkDef::Width) + (y * cChunkDef::Width * cChunkDef::Width); // 1.2 uses XZY
|
2012-06-14 09:06:06 -04:00
|
|
|
#elif AXIS_ORDER == AXIS_ORDER_YZX
|
2014-07-17 13:13:23 -04:00
|
|
|
return y + (z * cChunkDef::Width) + (x * cChunkDef::Height * cChunkDef::Width); // 1.1 uses YZX
|
2012-06-14 09:06:06 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-29 08:59:24 -04:00
|
|
|
|
|
|
|
inline static int MakeIndexNoCheck(Vector3i a_RelPos)
|
|
|
|
{
|
|
|
|
return MakeIndexNoCheck(a_RelPos.x, a_RelPos.y, a_RelPos.z);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-07-23 14:12:51 -04:00
|
|
|
inline static Vector3i IndexToCoordinate(size_t index)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
#if AXIS_ORDER == AXIS_ORDER_XZY
|
2014-07-17 10:33:09 -04:00
|
|
|
return Vector3i( // 1.2
|
2018-07-23 14:12:51 -04:00
|
|
|
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
|
2014-07-17 10:33:09 -04:00
|
|
|
);
|
2012-06-14 09:06:06 -04:00
|
|
|
#elif AXIS_ORDER == AXIS_ORDER_YZX
|
2014-07-17 10:33:09 -04:00
|
|
|
return Vector3i( // 1.1
|
2018-07-23 14:12:51 -04:00
|
|
|
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
|
2014-07-17 10:33:09 -04:00
|
|
|
);
|
2012-06-14 09:06:06 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline static void SetBlock(BLOCKTYPE * a_BlockTypes, int a_X, int a_Y, int a_Z, BLOCKTYPE a_Type)
|
|
|
|
{
|
|
|
|
ASSERT((a_X >= 0) && (a_X < Width));
|
|
|
|
ASSERT((a_Y >= 0) && (a_Y < Height));
|
|
|
|
ASSERT((a_Z >= 0) && (a_Z < Width));
|
2014-03-10 16:18:53 -04:00
|
|
|
a_BlockTypes[MakeIndexNoCheck(a_X, a_Y, a_Z)] = a_Type;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2014-10-11 22:39:55 -04:00
|
|
|
|
|
|
|
|
2013-02-08 11:01:44 -05:00
|
|
|
inline static void SetBlock(BLOCKTYPE * a_BlockTypes, int a_Index, BLOCKTYPE a_Type)
|
|
|
|
{
|
2013-10-21 15:38:31 -04:00
|
|
|
ASSERT((a_Index >= 0) && (a_Index <= NumBlocks));
|
2013-02-08 11:01:44 -05:00
|
|
|
a_BlockTypes[a_Index] = a_Type;
|
|
|
|
}
|
2014-10-11 22:39:55 -04:00
|
|
|
|
|
|
|
|
2019-09-29 08:59:24 -04:00
|
|
|
inline static BLOCKTYPE GetBlock(const BLOCKTYPE * a_BlockTypes, Vector3i a_RelPos)
|
|
|
|
{
|
|
|
|
ASSERT(IsValidRelPos(a_RelPos));
|
|
|
|
return a_BlockTypes[MakeIndexNoCheck(a_RelPos)];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-02 04:03:40 -04:00
|
|
|
inline static BLOCKTYPE GetBlock(const BLOCKTYPE * a_BlockTypes, int a_X, int a_Y, int a_Z)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
ASSERT((a_X >= 0) && (a_X < Width));
|
|
|
|
ASSERT((a_Y >= 0) && (a_Y < Height));
|
|
|
|
ASSERT((a_Z >= 0) && (a_Z < Width));
|
2014-03-10 16:18:53 -04:00
|
|
|
return a_BlockTypes[MakeIndexNoCheck(a_X, a_Y, a_Z)];
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2014-10-11 22:39:55 -04:00
|
|
|
|
|
|
|
|
2012-10-20 07:40:34 -04:00
|
|
|
inline static BLOCKTYPE GetBlock(const BLOCKTYPE * a_BlockTypes, int a_Idx)
|
|
|
|
{
|
2013-10-21 15:38:31 -04:00
|
|
|
ASSERT((a_Idx >= 0) && (a_Idx < NumBlocks));
|
2012-10-20 07:40:34 -04:00
|
|
|
return a_BlockTypes[a_Idx];
|
|
|
|
}
|
2014-10-11 22:39:55 -04:00
|
|
|
|
|
|
|
|
2015-05-19 06:50:59 -04:00
|
|
|
inline static HEIGHTTYPE GetHeight(const HeightMap & a_HeightMap, int a_X, int a_Z)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-09-01 15:17:22 -04:00
|
|
|
ASSERT((a_X >= 0) && (a_X < Width));
|
|
|
|
ASSERT((a_Z >= 0) && (a_Z < Width));
|
2012-06-14 09:06:06 -04:00
|
|
|
return a_HeightMap[a_X + Width * a_Z];
|
|
|
|
}
|
2014-10-11 22:39:55 -04:00
|
|
|
|
|
|
|
|
2015-05-19 06:50:59 -04:00
|
|
|
inline static void SetHeight(HeightMap & a_HeightMap, int a_X, int a_Z, HEIGHTTYPE a_Height)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-09-01 15:17:22 -04:00
|
|
|
ASSERT((a_X >= 0) && (a_X < Width));
|
|
|
|
ASSERT((a_Z >= 0) && (a_Z < Width));
|
2012-06-14 09:06:06 -04:00
|
|
|
a_HeightMap[a_X + Width * a_Z] = a_Height;
|
|
|
|
}
|
2014-10-11 22:39:55 -04:00
|
|
|
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
inline static EMCSBiome GetBiome(const BiomeMap & a_BiomeMap, int a_X, int a_Z)
|
|
|
|
{
|
2014-09-01 15:17:22 -04:00
|
|
|
ASSERT((a_X >= 0) && (a_X < Width));
|
|
|
|
ASSERT((a_Z >= 0) && (a_Z < Width));
|
2012-06-14 09:06:06 -04:00
|
|
|
return a_BiomeMap[a_X + Width * a_Z];
|
|
|
|
}
|
2014-10-11 22:39:55 -04:00
|
|
|
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
inline static void SetBiome(BiomeMap & a_BiomeMap, int a_X, int a_Z, EMCSBiome a_Biome)
|
|
|
|
{
|
2014-09-01 15:17:22 -04:00
|
|
|
ASSERT((a_X >= 0) && (a_X < Width));
|
|
|
|
ASSERT((a_Z >= 0) && (a_Z < Width));
|
2012-06-14 09:06:06 -04:00
|
|
|
a_BiomeMap[a_X + Width * a_Z] = a_Biome;
|
|
|
|
}
|
2014-04-04 18:16:52 -04:00
|
|
|
|
|
|
|
|
2014-04-25 16:22:43 -04:00
|
|
|
static NIBBLETYPE GetNibble(const COMPRESSED_NIBBLETYPE & a_Buffer, int a_BlockIdx, bool a_IsSkyLightNibble = false)
|
2014-04-04 18:16:52 -04:00
|
|
|
{
|
|
|
|
if ((a_BlockIdx > -1) && (a_BlockIdx < NumBlocks))
|
|
|
|
{
|
2014-10-21 12:12:40 -04:00
|
|
|
if (static_cast<size_t>(a_BlockIdx / 2) >= a_Buffer.size())
|
2014-04-04 18:16:52 -04:00
|
|
|
{
|
2014-04-06 18:30:21 -04:00
|
|
|
return (a_IsSkyLightNibble ? 0xff : 0);
|
2014-04-04 18:16:52 -04:00
|
|
|
}
|
2014-10-11 22:39:55 -04:00
|
|
|
return (a_Buffer[static_cast<size_t>(a_BlockIdx / 2)] >> ((a_BlockIdx & 1) * 4)) & 0x0f;
|
2014-04-04 18:16:52 -04:00
|
|
|
}
|
|
|
|
ASSERT(!"cChunkDef::GetNibble(): index out of chunk range!");
|
|
|
|
return 0;
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
2014-04-25 16:22:43 -04:00
|
|
|
static NIBBLETYPE GetNibble(const COMPRESSED_NIBBLETYPE & a_Buffer, int x, int y, int z, bool a_IsSkyLightNibble = false)
|
2014-04-04 18:16:52 -04:00
|
|
|
{
|
|
|
|
if ((x < Width) && (x > -1) && (y < Height) && (y > -1) && (z < Width) && (z > -1))
|
|
|
|
{
|
2014-10-11 22:39:55 -04:00
|
|
|
size_t Index = static_cast<size_t>(MakeIndexNoCheck(x, y, z));
|
2014-04-28 16:22:07 -04:00
|
|
|
if ((Index / 2) >= a_Buffer.size())
|
2014-04-04 18:16:52 -04:00
|
|
|
{
|
2014-04-06 18:30:21 -04:00
|
|
|
return (a_IsSkyLightNibble ? 0xff : 0);
|
2014-04-04 18:16:52 -04:00
|
|
|
}
|
2014-04-25 16:22:43 -04:00
|
|
|
return ExpandNibble(a_Buffer, Index);
|
2014-04-04 18:16:52 -04:00
|
|
|
}
|
|
|
|
ASSERT(!"cChunkDef::GetNibble(): coords out of chunk range!");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-29 08:59:24 -04:00
|
|
|
static NIBBLETYPE GetNibble(const NIBBLETYPE * a_Buffer, Vector3i a_RelPos)
|
|
|
|
{
|
|
|
|
if (IsValidRelPos(a_RelPos))
|
|
|
|
{
|
|
|
|
auto Index = MakeIndexNoCheck(a_RelPos);
|
|
|
|
return (a_Buffer[static_cast<size_t>(Index / 2)] >> ((Index & 1) * 4)) & 0x0f;
|
|
|
|
}
|
|
|
|
ASSERT(!"Coords out of chunk range!");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-24 16:49:56 -04:00
|
|
|
static NIBBLETYPE GetNibble(const NIBBLETYPE * a_Buffer, int x, int y, int z)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-04-24 16:49:56 -04:00
|
|
|
if ((x < Width) && (x > -1) && (y < Height) && (y > -1) && (z < Width) && (z > -1))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-04-24 16:49:56 -04:00
|
|
|
int Index = MakeIndexNoCheck(x, y, z);
|
2014-10-11 22:39:55 -04:00
|
|
|
return (a_Buffer[static_cast<size_t>(Index / 2)] >> ((Index & 1) * 4)) & 0x0f;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2014-04-24 16:49:56 -04:00
|
|
|
ASSERT(!"cChunkDef::GetNibble(): coords out of chunk range!");
|
|
|
|
return 0;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2014-04-04 18:16:52 -04:00
|
|
|
|
|
|
|
|
2014-04-25 16:22:43 -04:00
|
|
|
static void SetNibble(COMPRESSED_NIBBLETYPE & a_Buffer, int a_BlockIdx, NIBBLETYPE a_Nibble)
|
2014-04-04 18:16:52 -04:00
|
|
|
{
|
|
|
|
if ((a_BlockIdx < 0) || (a_BlockIdx >= NumBlocks))
|
|
|
|
{
|
|
|
|
ASSERT(!"cChunkDef::SetNibble(): index out of range!");
|
|
|
|
return;
|
|
|
|
}
|
2014-10-11 22:39:55 -04:00
|
|
|
if (static_cast<size_t>(a_BlockIdx / 2) >= a_Buffer.size())
|
2014-04-04 18:16:52 -04:00
|
|
|
{
|
2014-10-11 22:39:55 -04:00
|
|
|
a_Buffer.resize(static_cast<size_t>((a_BlockIdx / 2) + 1));
|
2014-04-04 18:16:52 -04:00
|
|
|
}
|
2014-10-11 22:39:55 -04:00
|
|
|
a_Buffer[static_cast<size_t>(a_BlockIdx / 2)] = PackNibble(a_Buffer, static_cast<size_t>(a_BlockIdx), a_Nibble);
|
2014-04-04 18:16:52 -04:00
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
2014-04-25 16:22:43 -04:00
|
|
|
static void SetNibble(COMPRESSED_NIBBLETYPE & a_Buffer, int x, int y, int z, NIBBLETYPE a_Nibble)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-10-21 15:38:31 -04:00
|
|
|
if (
|
|
|
|
(x >= Width) || (x < 0) ||
|
|
|
|
(y >= Height) || (y < 0) ||
|
|
|
|
(z >= Width) || (z < 0)
|
|
|
|
)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-10-21 15:38:31 -04:00
|
|
|
ASSERT(!"cChunkDef::SetNibble(): index out of range!");
|
|
|
|
return;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2013-10-21 15:38:31 -04:00
|
|
|
|
2014-10-11 22:39:55 -04:00
|
|
|
size_t Index = static_cast<size_t>(MakeIndexNoCheck(x, y, z));
|
2014-04-28 16:22:07 -04:00
|
|
|
if ((Index / 2) >= a_Buffer.size())
|
2014-04-04 18:16:52 -04:00
|
|
|
{
|
2014-04-28 16:22:07 -04:00
|
|
|
a_Buffer.resize(((Index / 2) + 1));
|
2014-04-04 18:16:52 -04:00
|
|
|
}
|
2014-04-28 16:22:07 -04:00
|
|
|
a_Buffer[(Index / 2)] = PackNibble(a_Buffer, Index, a_Nibble);
|
2014-04-04 18:16:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-25 16:22:43 -04:00
|
|
|
private:
|
|
|
|
|
|
|
|
|
2014-04-28 16:22:07 -04:00
|
|
|
inline static NIBBLETYPE PackNibble(const COMPRESSED_NIBBLETYPE & a_Buffer, size_t a_Index, NIBBLETYPE a_Nibble)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-04-25 16:22:43 -04:00
|
|
|
return static_cast<NIBBLETYPE>(
|
|
|
|
(a_Buffer[a_Index / 2] & (0xf0 >> ((a_Index & 1) * 4))) | // The untouched nibble
|
|
|
|
((a_Nibble & 0x0f) << ((a_Index & 1) * 4)) // The nibble being set
|
|
|
|
);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2014-04-04 18:16:52 -04:00
|
|
|
|
2014-04-25 16:22:43 -04:00
|
|
|
|
2014-04-28 16:22:07 -04:00
|
|
|
inline static NIBBLETYPE ExpandNibble(const COMPRESSED_NIBBLETYPE & a_Buffer, size_t a_Index)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-04-25 16:22:43 -04:00
|
|
|
return (a_Buffer[a_Index / 2] >> ((a_Index & 1) * 4)) & 0x0f;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2014-04-25 16:22:43 -04:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Interface class used for comparing clients of two chunks.
|
2015-07-31 10:49:10 -04:00
|
|
|
Used primarily for entity moving while both chunks are locked. */
|
2012-06-14 09:06:06 -04:00
|
|
|
class cClientDiffCallback
|
|
|
|
{
|
|
|
|
public:
|
2014-03-09 13:32:56 -04:00
|
|
|
|
|
|
|
virtual ~cClientDiffCallback() {}
|
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Called for clients that are in Chunk1 and not in Chunk2, */
|
2012-06-14 09:06:06 -04:00
|
|
|
virtual void Removed(cClientHandle * a_Client) = 0;
|
2014-10-11 22:39:55 -04:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Called for clients that are in Chunk2 and not in Chunk1. */
|
2012-06-14 09:06:06 -04:00
|
|
|
virtual void Added(cClientHandle * a_Client) = 0;
|
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct sSetBlock
|
|
|
|
{
|
2014-12-24 01:20:17 -05:00
|
|
|
int m_RelX, m_RelY, m_RelZ;
|
|
|
|
int m_ChunkX, m_ChunkZ;
|
|
|
|
BLOCKTYPE m_BlockType;
|
|
|
|
NIBBLETYPE m_BlockMeta;
|
|
|
|
|
2019-09-01 03:30:00 -04:00
|
|
|
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);
|
|
|
|
}
|
2014-12-24 01:20:17 -05:00
|
|
|
|
2019-12-22 17:38:11 -05:00
|
|
|
sSetBlock(Vector3i a_BlockPos, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) :
|
|
|
|
sSetBlock(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, a_BlockType, a_BlockMeta)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-12-24 01:20:17 -05:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
ASSERT((a_RelX >= 0) && (a_RelX < cChunkDef::Width));
|
|
|
|
ASSERT((a_RelZ >= 0) && (a_RelZ < cChunkDef::Width));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns the absolute X coord of the stored block. */
|
|
|
|
int GetX(void) const { return m_RelX + cChunkDef::Width * m_ChunkX; }
|
|
|
|
|
|
|
|
/** Returns the absolute Y coord of the stored block.
|
|
|
|
Is the same as relative Y coords, because there's no Y relativization. */
|
|
|
|
int GetY(void) const { return m_RelY; }
|
|
|
|
|
|
|
|
/** Returns the absolute Z coord of the stored block. */
|
|
|
|
int GetZ(void) const { return m_RelZ + cChunkDef::Width * m_ChunkZ; }
|
2017-07-28 12:59:21 -04:00
|
|
|
|
2020-04-21 16:19:22 -04:00
|
|
|
/** Returns the absolute coords of the stored block. */
|
|
|
|
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);
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::list<sSetBlock> sSetBlockList;
|
|
|
|
typedef std::vector<sSetBlock> sSetBlockVector;
|
|
|
|
|
|
|
|
typedef std::list<cChunkCoords> cChunkCoordsList;
|
2014-02-08 15:55:21 -05:00
|
|
|
typedef std::vector<cChunkCoords> cChunkCoordsVector;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-12-24 01:20:17 -05:00
|
|
|
/** 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, ...> */
|
2014-12-24 02:38:37 -05:00
|
|
|
class cChunkCoordsHash
|
2014-12-24 01:20:17 -05:00
|
|
|
{
|
2014-12-24 02:38:37 -05:00
|
|
|
public:
|
|
|
|
size_t operator () (const cChunkCoords & a_Coords) const
|
2014-12-24 01:20:17 -05:00
|
|
|
{
|
|
|
|
return (static_cast<size_t>(a_Coords.m_ChunkX) << 16) ^ static_cast<size_t>(a_Coords.m_ChunkZ);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-09-02 18:14:51 -04:00
|
|
|
class cChunkCoordsWithBool
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
int m_ChunkX;
|
|
|
|
int m_ChunkZ;
|
|
|
|
bool m_ForceGenerate;
|
|
|
|
|
|
|
|
cChunkCoordsWithBool(int a_ChunkX, int a_ChunkZ, bool a_ForceGenerate) : m_ChunkX(a_ChunkX), m_ChunkZ(a_ChunkZ), m_ForceGenerate(a_ForceGenerate){}
|
|
|
|
|
|
|
|
bool operator == (const cChunkCoordsWithBool & a_Other) const
|
|
|
|
{
|
|
|
|
return ((m_ChunkX == a_Other.m_ChunkX) && (m_ChunkZ == a_Other.m_ChunkZ) && (m_ForceGenerate == a_Other.m_ForceGenerate));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-09-02 18:28:08 -04:00
|
|
|
typedef std::list<cChunkCoordsWithBool> cChunkCoordsWithBoolList;
|
2014-09-02 18:14:51 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-12-10 16:35:16 -05:00
|
|
|
/** Interface class used as a callback for operations that involve chunk coords */
|
2012-06-14 09:06:06 -04:00
|
|
|
class cChunkCoordCallback
|
|
|
|
{
|
|
|
|
public:
|
2014-03-09 13:32:56 -04:00
|
|
|
|
|
|
|
virtual ~cChunkCoordCallback() {}
|
|
|
|
|
2015-10-04 08:06:37 -04:00
|
|
|
/** Called with the chunk's coords, and an optional operation status flag for operations that support it. */
|
2019-09-01 03:30:00 -04:00
|
|
|
virtual void Call(cChunkCoords a_Coords, bool a_IsSuccess) = 0;
|
2012-06-14 09:06:06 -04:00
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-03-01 14:35:29 -05:00
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Generic template that can store any kind of data together with a triplet of 3 coords */
|
2013-03-01 14:35:29 -05:00
|
|
|
template <typename X> class cCoordWithData
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
int z;
|
|
|
|
X Data;
|
2014-10-11 22:39:55 -04:00
|
|
|
|
2013-03-02 09:28:52 -05:00
|
|
|
cCoordWithData(int a_X, int a_Y, int a_Z) :
|
2014-08-21 16:39:53 -04:00
|
|
|
x(a_X), y(a_Y), z(a_Z), Data()
|
2013-03-01 14:35:29 -05:00
|
|
|
{
|
|
|
|
}
|
2014-10-11 22:39:55 -04:00
|
|
|
|
2013-03-02 09:28:52 -05:00
|
|
|
cCoordWithData(int a_X, int a_Y, int a_Z, const X & a_Data) :
|
2013-03-01 14:35:29 -05:00
|
|
|
x(a_X), y(a_Y), z(a_Z), Data(a_Data)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
} ;
|
|
|
|
|
2013-03-14 04:00:24 -04:00
|
|
|
typedef cCoordWithData<int> cCoordWithInt;
|
2014-01-10 15:31:05 -05:00
|
|
|
typedef cCoordWithData<BLOCKTYPE> cCoordWithBlock;
|
2014-01-29 14:19:14 -05:00
|
|
|
|
2013-03-14 04:00:24 -04:00
|
|
|
typedef std::list<cCoordWithInt> cCoordWithIntList;
|
|
|
|
typedef std::vector<cCoordWithInt> cCoordWithIntVector;
|