From 813176fbd1de6591201a840829cb3f4ab927f754 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Mon, 18 Jan 2021 16:09:10 +0000 Subject: [PATCH] cChunk: don't inherit from cChunkDef (#5106) --- src/BlockEntities/HopperEntity.cpp | 4 +- src/Blocks/BlockConcretePowder.h | 2 +- src/Blocks/BlockHandler.cpp | 2 +- src/Blocks/BlockSponge.h | 2 +- src/Blocks/BlockVine.h | 2 +- src/Chunk.cpp | 89 +++++++------------ src/Chunk.h | 7 +- src/Entities/Minecart.cpp | 2 +- .../ForEachSourceCallback.cpp | 4 +- 9 files changed, 43 insertions(+), 71 deletions(-) diff --git a/src/BlockEntities/HopperEntity.cpp b/src/BlockEntities/HopperEntity.cpp index 2e4264204..9c9f229e4 100644 --- a/src/BlockEntities/HopperEntity.cpp +++ b/src/BlockEntities/HopperEntity.cpp @@ -313,7 +313,7 @@ bool cHopperEntity::MoveItemsOut(cChunk & a_Chunk, Int64 a_CurrentTick) } // Convert coords to relative: - auto relCoord = a_Chunk.AbsoluteToRelative(out.second); + auto relCoord = cChunkDef::AbsoluteToRelative(out.second); auto destChunk = a_Chunk.GetRelNeighborChunkAdjustCoords(relCoord); if (destChunk == nullptr) { @@ -554,7 +554,7 @@ bool cHopperEntity::MoveItemsToChest(cChunk & a_Chunk, Vector3i a_Coords) { 0, 0, 1}, { 0, 0, -1}, } ; - auto relCoord = a_Chunk.AbsoluteToRelative(a_Coords); + auto relCoord = cChunkDef::AbsoluteToRelative(a_Coords); for (const auto & ofs: neighborOfs) { auto otherHalfRelCoord = relCoord + ofs; diff --git a/src/Blocks/BlockConcretePowder.h b/src/Blocks/BlockConcretePowder.h index a69001a65..52591b3c1 100644 --- a/src/Blocks/BlockConcretePowder.h +++ b/src/Blocks/BlockConcretePowder.h @@ -29,7 +29,7 @@ private: virtual void OnNeighborChanged(cChunkInterface & a_ChunkInterface, Vector3i a_BlockPos, eBlockFace a_WhichNeighbor) const override { - a_ChunkInterface.DoWithChunkAt(a_BlockPos, [&](cChunk & a_Chunk) { CheckSoaked(a_Chunk.AbsoluteToRelative(a_BlockPos), a_Chunk); return true; }); + a_ChunkInterface.DoWithChunkAt(a_BlockPos, [&](cChunk & a_Chunk) { CheckSoaked(cChunkDef::AbsoluteToRelative(a_BlockPos), a_Chunk); return true; }); } /** Check blocks above and around to see if they are water. If one is, converts this into concrete block. */ diff --git a/src/Blocks/BlockHandler.cpp b/src/Blocks/BlockHandler.cpp index 326f0de27..0e1672ce8 100644 --- a/src/Blocks/BlockHandler.cpp +++ b/src/Blocks/BlockHandler.cpp @@ -488,7 +488,7 @@ void cBlockHandler::OnUpdate( void cBlockHandler::OnNeighborChanged(cChunkInterface & a_ChunkInterface, Vector3i a_BlockPos, eBlockFace a_WhichNeighbor) const { - if (a_ChunkInterface.DoWithChunkAt(a_BlockPos, [&](cChunk & a_Chunk) { return CanBeAt(a_ChunkInterface, a_Chunk.AbsoluteToRelative(a_BlockPos), a_Chunk); })) + if (a_ChunkInterface.DoWithChunkAt(a_BlockPos, [&](cChunk & a_Chunk) { return CanBeAt(a_ChunkInterface, cChunkDef::AbsoluteToRelative(a_BlockPos), a_Chunk); })) { return; } diff --git a/src/Blocks/BlockSponge.h b/src/Blocks/BlockSponge.h index a089bc0d0..0e4865a8e 100644 --- a/src/Blocks/BlockSponge.h +++ b/src/Blocks/BlockSponge.h @@ -29,7 +29,7 @@ private: virtual void OnNeighborChanged(cChunkInterface & a_ChunkInterface, Vector3i a_BlockPos, eBlockFace a_WhichNeighbor) const override { - a_ChunkInterface.DoWithChunkAt(a_BlockPos, [&](cChunk & a_Chunk) { CheckSoaked(a_Chunk.AbsoluteToRelative(a_BlockPos), a_Chunk); return true; }); + a_ChunkInterface.DoWithChunkAt(a_BlockPos, [&](cChunk & a_Chunk) { CheckSoaked(cChunkDef::AbsoluteToRelative(a_BlockPos), a_Chunk); return true; }); } /** Check blocks around the sponge to see if they are water. diff --git a/src/Blocks/BlockVine.h b/src/Blocks/BlockVine.h index 9738cd89b..8d42137d4 100644 --- a/src/Blocks/BlockVine.h +++ b/src/Blocks/BlockVine.h @@ -162,7 +162,7 @@ private: a_ChunkInterface.DoWithChunkAt(a_BlockPos, [&](cChunk & a_Chunk) { - const auto a_RelPos = a_Chunk.AbsoluteToRelative(a_BlockPos); + const auto a_RelPos = cChunkDef::AbsoluteToRelative(a_BlockPos); NIBBLETYPE CurMeta = a_Chunk.GetMeta(a_RelPos); NIBBLETYPE MaxMeta = GetMaxMeta(a_Chunk, a_RelPos); diff --git a/src/Chunk.cpp b/src/Chunk.cpp index 0dc333468..e8757158e 100644 --- a/src/Chunk.cpp +++ b/src/Chunk.cpp @@ -483,7 +483,7 @@ void cChunk::WriteBlockArea(cBlockArea & a_Area, int a_MinBlockX, int a_MinBlock continue; } // This block entity is inside the chunk, clone it (and remove any that is there currently): - auto idx = static_cast(MakeIndex(posX - m_PosX * cChunkDef::Width, posY, posZ - m_PosZ * cChunkDef::Width)); + auto idx = static_cast(cChunkDef::MakeIndex(posX - m_PosX * cChunkDef::Width, posY, posZ - m_PosZ * cChunkDef::Width)); auto itr = m_BlockEntities.find(idx); if (itr != m_BlockEntities.end()) { @@ -584,7 +584,7 @@ void cChunk::GetRandomBlockCoords(int & a_X, int & a_Y, int & a_Z) // MG TODO : check if this kind of optimization (only one random call) is still needed // MG TODO : if so propagate it - GetThreeRandomNumbers(a_X, a_Y, a_Z, Width, Height - 2, Width); + GetThreeRandomNumbers(a_X, a_Y, a_Z, cChunkDef::Width, cChunkDef::Height - 2, cChunkDef::Width); a_Y++; } @@ -1201,8 +1201,8 @@ bool cChunk::UnboundedRelFastSetBlock(Vector3i a_RelPos, BLOCKTYPE a_BlockType, int cChunk::GetHeight(int a_X, int a_Z) const { - ASSERT((a_X >= 0) && (a_X < Width) && (a_Z >= 0) && (a_Z < Width)); - return m_HeightMap[a_X + a_Z * Width]; + ASSERT((a_X >= 0) && (a_X < cChunkDef::Width) && (a_Z >= 0) && (a_Z < cChunkDef::Width)); + return m_HeightMap[a_X + a_Z * cChunkDef::Width]; } @@ -1272,7 +1272,7 @@ void cChunk::WakeUpSimulators(void) for (size_t BlockIdx = 0; BlockIdx != cChunkData::SectionBlockCount; ++BlockIdx) { const auto BlockType = Section->m_BlockTypes[BlockIdx]; - auto Position = IndexToCoordinate(BlockIdx); + auto Position = cChunkDef::IndexToCoordinate(BlockIdx); Position.y += static_cast(SectionIdx * cChunkData::SectionHeight); RedstoneSimulator->AddBlock(*this, Position, BlockType); @@ -1286,29 +1286,6 @@ void cChunk::WakeUpSimulators(void) -void cChunk::CalculateHeightmap(const BLOCKTYPE * a_BlockTypes) -{ - for (int x = 0; x < Width; x++) - { - for (int z = 0; z < Width; z++) - { - for (int y = Height - 1; y > -1; y--) - { - int index = MakeIndex( x, y, z); - if (a_BlockTypes[index] != E_BLOCK_AIR) - { - m_HeightMap[x + z * Width] = static_cast(y); - break; - } - } // for y - } // for z - } // for x -} - - - - - void cChunk::SetBlock(Vector3i a_RelPos, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) { FastSetBlock(a_RelPos, a_BlockType, a_BlockMeta); @@ -1341,7 +1318,7 @@ void cChunk::SetBlock(Vector3i a_RelPos, BLOCKTYPE a_BlockType, NIBBLETYPE a_Blo void cChunk::FastSetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockType, BLOCKTYPE a_BlockMeta) { - ASSERT(!((a_RelX < 0) || (a_RelX >= Width) || (a_RelY < 0) || (a_RelY >= Height) || (a_RelZ < 0) || (a_RelZ >= Width))); + ASSERT(!((a_RelX < 0) || (a_RelX >= cChunkDef::Width) || (a_RelY < 0) || (a_RelY >= cChunkDef::Height) || (a_RelZ < 0) || (a_RelZ >= cChunkDef::Width))); ASSERT(IsValid()); @@ -1393,11 +1370,11 @@ void cChunk::FastSetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockT } // Update heightmap, if needed: - if (a_RelY >= m_HeightMap[a_RelX + a_RelZ * Width]) + if (a_RelY >= m_HeightMap[a_RelX + a_RelZ * cChunkDef::Width]) { if (a_BlockType != E_BLOCK_AIR) { - m_HeightMap[a_RelX + a_RelZ * Width] = static_cast(a_RelY); + m_HeightMap[a_RelX + a_RelZ * cChunkDef::Width] = static_cast(a_RelY); } else { @@ -1405,7 +1382,7 @@ void cChunk::FastSetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockT { if (GetBlock(a_RelX, y, a_RelZ) != E_BLOCK_AIR) { - m_HeightMap[a_RelX + a_RelZ * Width] = static_cast(y); + m_HeightMap[a_RelX + a_RelZ * cChunkDef::Width] = static_cast(y); break; } } // for y - column in m_BlockData @@ -1445,7 +1422,7 @@ void cChunk::SendBlockTo(int a_RelX, int a_RelY, int a_RelZ, cClientHandle * a_C void cChunk::AddBlockEntity(OwnedBlockEntity a_BlockEntity) { [[maybe_unused]] const auto Result = m_BlockEntities.emplace( - MakeIndex(a_BlockEntity->GetRelX(), a_BlockEntity->GetPosY(), a_BlockEntity->GetRelZ()), + cChunkDef::MakeIndex(a_BlockEntity->GetRelX(), a_BlockEntity->GetPosY(), a_BlockEntity->GetRelZ()), std::move(a_BlockEntity) ); ASSERT(Result.second); // No block entity already at this position @@ -1457,15 +1434,15 @@ void cChunk::AddBlockEntity(OwnedBlockEntity a_BlockEntity) cBlockEntity * cChunk::GetBlockEntity(Vector3i a_AbsPos) { - auto relPos = AbsoluteToRelative(a_AbsPos); + const auto relPos = cChunkDef::AbsoluteToRelative(a_AbsPos); - if (!IsValidRelPos(relPos)) + if (!cChunkDef::IsValidRelPos(relPos)) { // Coordinates are outside outside this chunk, no block entities here return nullptr; } - auto itr = m_BlockEntities.find(static_cast(MakeIndexNoCheck(relPos))); + auto itr = m_BlockEntities.find(static_cast(cChunkDef::MakeIndexNoCheck(relPos))); return (itr == m_BlockEntities.end()) ? nullptr : itr->second.get(); } @@ -1475,8 +1452,8 @@ cBlockEntity * cChunk::GetBlockEntity(Vector3i a_AbsPos) cBlockEntity * cChunk::GetBlockEntityRel(Vector3i a_RelPos) { - ASSERT(IsValidRelPos(a_RelPos)); - auto itr = m_BlockEntities.find(static_cast(MakeIndexNoCheck(a_RelPos))); + ASSERT(cChunkDef::IsValidRelPos(a_RelPos)); + auto itr = m_BlockEntities.find(static_cast(cChunkDef::MakeIndexNoCheck(a_RelPos))); return (itr == m_BlockEntities.end()) ? nullptr : itr->second.get(); } @@ -1588,7 +1565,7 @@ void cChunk::RemoveBlockEntity(cBlockEntity * a_BlockEntity) { MarkDirty(); ASSERT(a_BlockEntity != nullptr); - auto idx = static_cast(MakeIndex(a_BlockEntity->GetRelX(), a_BlockEntity->GetPosY(), a_BlockEntity->GetRelZ())); + auto idx = static_cast(cChunkDef::MakeIndex(a_BlockEntity->GetRelX(), a_BlockEntity->GetPosY(), a_BlockEntity->GetRelZ())); m_BlockEntities.erase(idx); } @@ -2171,7 +2148,7 @@ cChunk * cChunk::GetRelNeighborChunk(int a_RelX, int a_RelZ) int BlockX = m_PosX * cChunkDef::Width + a_RelX; int BlockZ = m_PosZ * cChunkDef::Width + a_RelZ; int ChunkX, ChunkZ; - BlockToChunk(BlockX, BlockZ, ChunkX, ChunkZ); + cChunkDef::BlockToChunk(BlockX, BlockZ, ChunkX, ChunkZ); return m_ChunkMap->FindChunk(ChunkX, ChunkZ); } @@ -2236,8 +2213,8 @@ cChunk * cChunk::GetRelNeighborChunkAdjustCoords(Vector3i & a_RelPos) const // The most common case: inside this chunk: if ( - (a_RelPos.x >= 0) && (a_RelPos.x < Width) && - (a_RelPos.z >= 0) && (a_RelPos.z < Width) + (a_RelPos.x >= 0) && (a_RelPos.x < cChunkDef::Width) && + (a_RelPos.z >= 0) && (a_RelPos.z < cChunkDef::Width) ) { return ToReturn; @@ -2246,24 +2223,24 @@ cChunk * cChunk::GetRelNeighborChunkAdjustCoords(Vector3i & a_RelPos) const // Request for a different chunk, calculate chunk offset: int RelX = a_RelPos.x; // Make a local copy of the coords (faster access) int RelZ = a_RelPos.z; - while ((RelX >= Width) && (ToReturn != nullptr)) + while ((RelX >= cChunkDef::Width) && (ToReturn != nullptr)) { - RelX -= Width; + RelX -= cChunkDef::Width; ToReturn = ToReturn->m_NeighborXP; } while ((RelX < 0) && (ToReturn != nullptr)) { - RelX += Width; + RelX += cChunkDef::Width; ToReturn = ToReturn->m_NeighborXM; } - while ((RelZ >= Width) && (ToReturn != nullptr)) + while ((RelZ >= cChunkDef::Width) && (ToReturn != nullptr)) { - RelZ -= Width; + RelZ -= cChunkDef::Width; ToReturn = ToReturn->m_NeighborZP; } while ((RelZ < 0) && (ToReturn != nullptr)) { - RelZ += Width; + RelZ += cChunkDef::Width; ToReturn = ToReturn->m_NeighborZM; } if (ToReturn != nullptr) @@ -2274,13 +2251,13 @@ cChunk * cChunk::GetRelNeighborChunkAdjustCoords(Vector3i & a_RelPos) const } // The chunk cannot be walked through neighbors, find it through the chunkmap: - int AbsX = a_RelPos.x + m_PosX * Width; - int AbsZ = a_RelPos.z + m_PosZ * Width; + int AbsX = a_RelPos.x + m_PosX * cChunkDef::Width; + int AbsZ = a_RelPos.z + m_PosZ * cChunkDef::Width; int DstChunkX, DstChunkZ; - BlockToChunk(AbsX, AbsZ, DstChunkX, DstChunkZ); + cChunkDef::BlockToChunk(AbsX, AbsZ, DstChunkX, DstChunkZ); ToReturn = m_ChunkMap->FindChunk(DstChunkX, DstChunkZ); - a_RelPos.x = AbsX - DstChunkX * Width; - a_RelPos.z = AbsZ - DstChunkZ * Width; + a_RelPos.x = AbsX - DstChunkX * cChunkDef::Width; + a_RelPos.z = AbsZ - DstChunkZ * cChunkDef::Width; return ToReturn; } @@ -2305,8 +2282,8 @@ void cChunk::SendBlockEntity(int a_BlockX, int a_BlockY, int a_BlockZ, cClientHa void cChunk::PositionToWorldPosition(int a_RelX, int a_RelY, int a_RelZ, int & a_BlockX, int & a_BlockY, int & a_BlockZ) { a_BlockY = a_RelY; - a_BlockX = m_PosX * Width + a_RelX; - a_BlockZ = m_PosZ * Width + a_RelZ; + a_BlockX = m_PosX * cChunkDef::Width + a_RelX; + a_BlockZ = m_PosZ * cChunkDef::Width + a_RelZ; } @@ -2315,7 +2292,7 @@ void cChunk::PositionToWorldPosition(int a_RelX, int a_RelY, int a_RelZ, int & a Vector3i cChunk::PositionToWorldPosition(int a_RelX, int a_RelY, int a_RelZ) { - return Vector3i(m_PosX * Width + a_RelX, a_RelY, m_PosZ * Width + a_RelZ); + return Vector3i(m_PosX * cChunkDef::Width + a_RelX, a_RelY, m_PosZ * cChunkDef::Width + a_RelZ); } diff --git a/src/Chunk.h b/src/Chunk.h index d0283f3e4..ec5407480 100644 --- a/src/Chunk.h +++ b/src/Chunk.h @@ -45,10 +45,7 @@ typedef std::list cClientHandleList; #define PREPARE_REL_AND_CHUNK(Position, OriginalChunk) cChunk * Chunk; Vector3i Rel; bool RelSuccess = (OriginalChunk).GetChunkAndRelByAbsolute(Position, &Chunk, Rel) -// This class is not to be used directly -// Instead, call actions on cChunkMap (such as cChunkMap::SetBlock() etc.) -class cChunk : - public cChunkDef // The inheritance is "misused" here only to inherit the functions and constants defined in cChunkDef +class cChunk { public: @@ -347,8 +344,6 @@ public: returns true if the use was successful, return false to use the block as a "normal" block */ bool UseBlockEntity(cPlayer * a_Player, int a_X, int a_Y, int a_Z); // [x, y, z] in world block coords - void CalculateHeightmap(const BLOCKTYPE * a_BlockTypes); - void SendBlockEntity (int a_BlockX, int a_BlockY, int a_BlockZ, cClientHandle & a_Client); Vector3i PositionToWorldPosition(Vector3i a_RelPos) diff --git a/src/Entities/Minecart.cpp b/src/Entities/Minecart.cpp index fbe4f756e..3ddcea6ee 100644 --- a/src/Entities/Minecart.cpp +++ b/src/Entities/Minecart.cpp @@ -137,7 +137,7 @@ void cMinecart::HandlePhysics(std::chrono::milliseconds a_Dt, cChunk & a_Chunk) return; } - auto relPos = a_Chunk.AbsoluteToRelative(GetPosition()); + auto relPos = cChunkDef::AbsoluteToRelative(GetPosition()); auto chunk = a_Chunk.GetRelNeighborChunkAdjustCoords(relPos); if (chunk == nullptr) { diff --git a/src/Simulator/IncrementalRedstoneSimulator/ForEachSourceCallback.cpp b/src/Simulator/IncrementalRedstoneSimulator/ForEachSourceCallback.cpp index bb86fbcc6..3afa42532 100644 --- a/src/Simulator/IncrementalRedstoneSimulator/ForEachSourceCallback.cpp +++ b/src/Simulator/IncrementalRedstoneSimulator/ForEachSourceCallback.cpp @@ -25,7 +25,7 @@ ForEachSourceCallback::ForEachSourceCallback(const cChunk & Chunk, const Vector3 void ForEachSourceCallback::operator()(Vector3i Location) { - if (!cChunk::IsValidHeight(Location.y)) + if (!cChunkDef::IsValidHeight(Location.y)) { return; } @@ -121,7 +121,7 @@ PowerLevel ForEachSourceCallback::QueryLinkedPower(const cChunk & Chunk, const V for (const auto & Offset : cSimulator::GetLinkedOffsets(SolidBlockPosition - QueryPosition)) { auto SourcePosition = QueryPosition + Offset; - if (!cChunk::IsValidHeight(SourcePosition.y)) + if (!cChunkDef::IsValidHeight(SourcePosition.y)) { continue; }