chunkmanager: add function getBlockAtPos, returns block at world pos

Returns Block::NULLBLK only to signal an invalid position
pull/4/head
EmaMaker 2023-06-01 21:22:52 +02:00
parent 0ebbb897dc
commit f798575cac
2 changed files with 25 additions and 0 deletions

View File

@ -21,6 +21,7 @@ namespace chunkmanager
void destroy(); void destroy();
oneapi::tbb::concurrent_queue<Chunk::Chunk*>& getDeleteVector(); oneapi::tbb::concurrent_queue<Chunk::Chunk*>& getDeleteVector();
std::array<std::array<int, 3>, chunks_volume>& getChunksIndices(); std::array<std::array<int, 3>, chunks_volume>& getChunksIndices();
Block getBlockAtPos(int x, int y, int z);
void update(); void update();
} }

View File

@ -10,6 +10,7 @@
#include <oneapi/tbb/concurrent_hash_map.h> #include <oneapi/tbb/concurrent_hash_map.h>
#include "block.hpp"
#include "chunk.hpp" #include "chunk.hpp"
#include "chunkgenerator.hpp" #include "chunkgenerator.hpp"
#include "chunkmesher.hpp" #include "chunkmesher.hpp"
@ -202,4 +203,27 @@ namespace chunkmanager
} }
} }
} }
Block getBlockAtPos(int x, int y, int z){
if(x < 0 || y < 0 || z < 0) return Block::NULLBLK;
int cx = static_cast<int>(x / CHUNK_SIZE);
int cy = static_cast<int>(y / CHUNK_SIZE);
int cz = static_cast<int>(z / CHUNK_SIZE);
if(cx < 0 || cy < 0 || cz < 0 || cx > 1023 || cy > 1023 || cz > 1023) return Block::NULLBLK;
//std::cout << "Block at " << x << ", " << y << ", " << z << " is in chunk " << cx << "," << cy << "," << cz << "\n";
ChunkTable::accessor a;
if(!chunks.find(a, calculateIndex(cx, cy, cz))) return Block::NULLBLK;
else {
int bx = x % CHUNK_SIZE;
int by = y % CHUNK_SIZE;
int bz = z % CHUNK_SIZE;
Block b = a->second->getBlock(bx, by, bz);
//std::cout << "Block is at " << bx << "," << by << "," << bz << "(" << (int)b << ")\n";
return b;
}
}
}; };