From c6d00c4200e8e63d784ee1dd34ac2759ce4e6d3b Mon Sep 17 00:00:00 2001 From: EmaMaker Date: Tue, 3 Oct 2023 22:25:46 +0200 Subject: [PATCH] fix type mismatch in chunk index/coordinates typedef an appropriate chunk_index_t and chunk_intcoord_t --- include/chunkmanager.hpp | 9 ++++++--- src/chunkmanager.cpp | 32 ++++++++++++++++---------------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/include/chunkmanager.hpp b/include/chunkmanager.hpp index b32a9af..a185381 100644 --- a/include/chunkmanager.hpp +++ b/include/chunkmanager.hpp @@ -17,9 +17,12 @@ #define MESHING_PRIORITY_PLAYER_EDIT 10 #define GENERATION_PRIORITY_NORMAL 0 +typedef int16_t chunk_intcoord_t; +typedef int32_t chunk_index_t; + namespace chunkmanager { - typedef oneapi::tbb::concurrent_hash_map ChunkTable; + typedef oneapi::tbb::concurrent_hash_map ChunkTable; typedef std::pair ChunkPQEntry; // The comparing function to use struct compare_f { @@ -31,12 +34,12 @@ namespace chunkmanager void init(); void blockpick(bool place); - uint32_t calculateIndex(uint16_t i, uint16_t j, uint16_t k); + chunk_index_t calculateIndex(chunk_intcoord_t i, chunk_intcoord_t j, chunk_intcoord_t k); void stop(); void destroy(); oneapi::tbb::concurrent_queue& getDeleteVector(); - std::array, chunks_volume>& getChunksIndices(); + std::array, chunks_volume>& getChunksIndices(); Block getBlockAtPos(int x, int y, int z); void update(); } diff --git a/src/chunkmanager.cpp b/src/chunkmanager.cpp index 1d5758b..54cf67a 100644 --- a/src/chunkmanager.cpp +++ b/src/chunkmanager.cpp @@ -24,7 +24,7 @@ namespace chunkmanager // Concurrent hash table of chunks ChunkTable chunks; // Chunk indices. Centered at (0,0,0), going in concentric sphere outwards - std::array, chunks_volume> chunks_indices; + std::array, chunks_volume> chunks_indices; /* Multithreading */ std::atomic_bool should_run; @@ -42,7 +42,7 @@ namespace chunkmanager int index{0}; int rr{RENDER_DISTANCE * RENDER_DISTANCE}; - int xp{0}, x{0}; + chunk_intcoord_t xp{0}, x{0}; bool b = true; // Iterate over all chunks, in concentric spheres starting fron the player and going outwards. Alternate left and right @@ -55,14 +55,14 @@ namespace chunkmanager // Step 1. At current x, get the corresponding y values (2nd degree equation, up to 2 // possible results) - int y1 = static_cast(sqrt((rr) - x*x)); + chunk_intcoord_t y1 = static_cast(sqrt((rr) - x*x)); - for (int y = -y1 + 1 ; y <= y1; y++) + for (chunk_intcoord_t y = -y1 + 1 ; y <= y1; y++) { // Step 2. At both y's, get the corresponding z values - int z1 = static_cast(sqrt( rr - x*x - y*y)); + chunk_intcoord_t z1 = static_cast(sqrt( rr - x*x - y*y)); - for (int z = -z1 + 1; z <= z1; z++){ + for (chunk_intcoord_t z = -z1 + 1; z <= z1; z++){ chunks_indices[index][0] = x; chunks_indices[index][1] = y; chunks_indices[index][2] = z; @@ -125,12 +125,12 @@ namespace chunkmanager // Update other chunks for(int i = 0; i < chunks_volume_real; i++) { - const uint16_t x = chunks_indices[i][0] + chunkX; - const uint16_t y = chunks_indices[i][1] + chunkY; - const uint16_t z = chunks_indices[i][2] + chunkZ; - const uint32_t index = calculateIndex(x, y, z); + const chunk_intcoord_t x = chunks_indices[i][0] + chunkX; + const chunk_intcoord_t y = chunks_indices[i][1] + chunkY; + const chunk_intcoord_t z = chunks_indices[i][2] + chunkZ; - if(x > 1023 || y > 1023 || z > 1023) continue; + if(x < 0 || y < 0 || z < 0 || x > 1023 || y > 1023 || z > 1023) continue; + const chunk_index_t index = calculateIndex(x, y, z); ChunkTable::accessor a, a1, a2, b1, b2, c1, c2; if(!chunks.find(a, index)) chunks.emplace(a, std::make_pair(index, new Chunk::Chunk(glm::vec3(x,y,z)))); @@ -164,9 +164,9 @@ namespace chunkmanager Chunk::Chunk* n; nUnloaded = 0; while(chunks_todelete.try_pop(n)){ - int x = static_cast(n->getPosition().x); - int y = static_cast(n->getPosition().y); - int z = static_cast(n->getPosition().z); + chunk_intcoord_t x = static_cast(n->getPosition().x); + chunk_intcoord_t y = static_cast(n->getPosition().y); + chunk_intcoord_t z = static_cast(n->getPosition().z); if(x > 1023 || y > 1023 || z > 1023) continue; const uint32_t index = calculateIndex(x, y, z); @@ -178,12 +178,12 @@ namespace chunkmanager } // uint32_t is fine, since i'm limiting the coordinate to only use up to ten bits (1023). There's actually two spare bits - uint32_t calculateIndex(uint16_t i, uint16_t j, uint16_t k){ + chunk_index_t calculateIndex(chunk_intcoord_t i, chunk_intcoord_t j, chunk_intcoord_t k){ return i | (j << 10) | (k << 20); } oneapi::tbb::concurrent_queue& getDeleteVector(){ return chunks_todelete; } - std::array, chunks_volume>& getChunksIndices(){ return chunks_indices; } + std::array, chunks_volume>& getChunksIndices(){ return chunks_indices; } void stop() { should_run=false;