1
0
Fork 0

Chunk: use FAST_FLOOR_DIV

This commit is contained in:
Tiger Wang 2020-12-18 20:44:51 +00:00
parent e7331fe820
commit 491238f799
3 changed files with 6 additions and 17 deletions

View File

@ -43,7 +43,6 @@ typedef std::list<cClientHandle *> cClientHandleList;
// A convenience macro for calling GetChunkAndRelByAbsolute.
#define PREPARE_REL_AND_CHUNK(Position, OriginalChunk) cChunk * Chunk; Vector3i Rel; bool RelSuccess = (OriginalChunk).GetChunkAndRelByAbsolute(Position, &Chunk, Rel)
#define PREPARE_BLOCKDATA BLOCKTYPE BlockType; NIBBLETYPE BlockMeta;
// This class is not to be used directly

View File

@ -187,7 +187,7 @@ 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 };
}
@ -234,24 +234,15 @@ public:
{
// 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});
const 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)
inline static cChunkCoords BlockToChunk(const Vector3i a_Position)
{
cChunkCoords Chunk(a_Pos.x / Width, a_Pos.z / Width);
if ((a_Pos.x < 0) && (a_Pos.x % Width != 0))
{
Chunk.m_ChunkX--;
}
if ((a_Pos.z < 0) && (a_Pos.z % Width != 0))
{
Chunk.m_ChunkZ--;
}
return Chunk;
return { FAST_FLOOR_DIV(a_Position.x, Width), FAST_FLOOR_DIV(a_Position.z, Width) };
}

View File

@ -153,9 +153,8 @@ bool cChunkMap::DoWithChunk(int a_ChunkX, int a_ChunkZ, cChunkCallback a_Callbac
bool cChunkMap::DoWithChunkAt(Vector3i a_BlockPos, cChunkCallback a_Callback)
{
int ChunkX, ChunkZ;
cChunkDef::BlockToChunk(a_BlockPos.x, a_BlockPos.z, ChunkX, ChunkZ);
return DoWithChunk(ChunkX, ChunkZ, a_Callback);
const auto Position = cChunkDef::BlockToChunk(a_BlockPos);
return DoWithChunk(Position.m_ChunkX, Position.m_ChunkZ, a_Callback);
}