From 1e5e31581689d372fbf32bfdcc0a89417dbf6b70 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 2 Feb 2015 00:46:39 +0100 Subject: Chunk: refactor getTopLayer() --- src/World/Block.hpp | 2 ++ src/World/Chunk.cpp | 47 +++++++++++++++++++++++------------------------ src/World/Chunk.hpp | 1 + 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/World/Block.hpp b/src/World/Block.hpp index 3613cac..f078503 100644 --- a/src/World/Block.hpp +++ b/src/World/Block.hpp @@ -44,6 +44,8 @@ struct Block { uint8_t biome; Block() : id(0), data(0), height(0), blockLight(0), skyLight(0), biome(0) {} + Block(uint8_t id0, uint8_t data0, unsigned height0, uint8_t blockLight0, uint8_t skyLight0, uint8_t biome0) + : id(id0), data(data0), height(height0), blockLight(blockLight0), skyLight(skyLight0), biome(biome0) {} uint32_t getColor() const; }; diff --git a/src/World/Chunk.cpp b/src/World/Chunk.cpp index 64b3988..17b5406 100644 --- a/src/World/Chunk.cpp +++ b/src/World/Chunk.cpp @@ -138,6 +138,28 @@ void Chunk::analyzeChunk() { } } +Block Chunk::getBlock(size_t x, size_t y, size_t z) const { + size_t y2 = y; + if (y2 < maxY-1) + y2++; + + unsigned h; + for (h = y; h > 0; h--) { + uint8_t id2 = getBlockAt(x, h, z); + if (id2 != 8 && id2 != 9) + break; + } + + return Block( + getBlockAt(x, y, z), + getDataAt(x, y, z), + h, + getBlockLightAt(x, y2, z), + getSkyLightAt(x, y2, z), + getBiomeAt(x, z) + ); +} + Chunk::Blocks Chunk::getTopLayer() const { size_t done = 0; Blocks ret; @@ -155,30 +177,7 @@ Chunk::Blocks Chunk::getTopLayer() const { if (!Resource::BLOCK_TYPES[id].opaque) continue; - Block &b = ret.blocks[x][z]; - - b.id = id; - b.data = getDataAt(x, y, z); - - size_t y2 = y; - if (y2 < maxY-1) - y2++; - - b.blockLight = getBlockLightAt(x, y2, z); - b.skyLight = getSkyLightAt(x, y2, z); - - - unsigned h; - for (h = y; h > 0; h--) { - uint8_t id2 = getBlockAt(x, h, z); - if (id2 != 8 && id2 != 9) - break; - } - - b.height = h; - - b.biome = getBiomeAt(x, z); - + ret.blocks[x][z] = getBlock(x, y, z); done++; } } diff --git a/src/World/Chunk.hpp b/src/World/Chunk.hpp index d05328c..a08f9e6 100644 --- a/src/World/Chunk.hpp +++ b/src/World/Chunk.hpp @@ -122,6 +122,7 @@ public: return *sections; } + Block getBlock(size_t x, size_t y, size_t z) const; Blocks getTopLayer() const; }; -- cgit v1.2.3