Skip to content

Commit

Permalink
Fix negative coordinate blocs
Browse files Browse the repository at this point in the history
Getting blocks from the world storage at negative would result in incorrect ids being fetched, due to missing conversion from world position to chunk position.
  • Loading branch information
ElDonad committed Jan 1, 2024
1 parent 77cdf4b commit 4ac8c29
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
11 changes: 2 additions & 9 deletions cubic-server/Dimension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,15 +334,8 @@ void Dimension::updateBlock(Position position, int32_t id)
LDEBUG("Dimension block update {} -> {}", position, id);
auto &chunk = this->_level.getChunkColumnFromBlockPos(position.x, position.z);

// Weird ass modulo to get the correct block position in the chunk
auto x = position.x % 16;
auto z = position.z % 16;
if (x < 0)
x += 16;
if (z < 0)
z += 16;

chunk.updateBlock({x, position.y, z}, id);
auto chunkPosition = world_storage::convertPositionToChunkPosition(position);
chunk.updateBlock(chunkPosition, id);
std::lock_guard _(_playersMutex);
for (auto player : _players) {
player->sendBlockUpdate({position, id});
Expand Down
2 changes: 1 addition & 1 deletion cubic-server/Dimension.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class Dimension : public std::enable_shared_from_this<Dimension> {
* @param pos The position of the block
* @return BlockId The block ID
*/
virtual BlockId getBlock(const Position &pos) const { return getLevel().getChunkColumnFromBlockPos(pos.x, pos.z).getBlock(pos); }
virtual BlockId getBlock(const Position &pos) const { return getLevel().getChunkColumnFromBlockPos(pos.x, pos.z).getBlock(world_storage::convertPositionToChunkPosition(pos)); }

/**
* @brief Get the tps of the dimension
Expand Down
12 changes: 12 additions & 0 deletions cubic-server/world_storage/ChunkColumn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ class Dimension;

namespace world_storage {

// Weird ass modulo to get the correct block position in the chunk
inline Position convertPositionToChunkPosition(const Position &position)
{
auto x = position.x % 16;
auto z = position.z % 16;
if (x < 0)
x += 16;
if (z < 0)
z += 16;
return {x, position.y, z};
}

// Heightmap
constexpr int HEIGHTMAP_BITS = bitsNeeded(CHUNK_HEIGHT + 1);
constexpr int HEIGHTMAP_ARRAY_SIZE = (SECTION_2D_SIZE * HEIGHTMAP_BITS / 64) + ((SECTION_2D_SIZE * HEIGHTMAP_BITS % 64) != 0);
Expand Down

0 comments on commit 4ac8c29

Please sign in to comment.