From f798575cac383ded45cdbe9aa9d5856af86646a9 Mon Sep 17 00:00:00 2001 From: EmaMaker Date: Thu, 1 Jun 2023 21:22:52 +0200 Subject: [PATCH] chunkmanager: add function getBlockAtPos, returns block at world pos Returns Block::NULLBLK only to signal an invalid position --- include/chunkmanager.hpp | 1 + src/chunkmanager.cpp | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/include/chunkmanager.hpp b/include/chunkmanager.hpp index 5c296fa..fbd4ec7 100644 --- a/include/chunkmanager.hpp +++ b/include/chunkmanager.hpp @@ -21,6 +21,7 @@ namespace chunkmanager void destroy(); oneapi::tbb::concurrent_queue& getDeleteVector(); 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 4f9797f..077f52a 100644 --- a/src/chunkmanager.cpp +++ b/src/chunkmanager.cpp @@ -10,6 +10,7 @@ #include +#include "block.hpp" #include "chunk.hpp" #include "chunkgenerator.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(x / CHUNK_SIZE); + int cy = static_cast(y / CHUNK_SIZE); + int cz = static_cast(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; + } + } };