mirror of
https://github.com/neocturne/MinedMap.git
synced 2025-05-10 03:25:07 +02:00
Compute top non-air layer
This doesn't yet ignore non-opaque blocks.
This commit is contained in:
parent
2d2671a686
commit
cd45c4009c
4 changed files with 102 additions and 28 deletions
|
@ -42,14 +42,7 @@ int main(int argc, char *argv[]) {
|
||||||
World::Region region(argv[1]);
|
World::Region region(argv[1]);
|
||||||
|
|
||||||
const World::Chunk &chunk = *region(0, 0);
|
const World::Chunk &chunk = *region(0, 0);
|
||||||
|
World::Chunk::Blocks layer = chunk.getTopLayer();
|
||||||
for (auto &entry : chunk.getSections()) {
|
|
||||||
std::cout << entry->getType() << ":" << std::endl;
|
|
||||||
|
|
||||||
for (auto &entry2 : *entry) {
|
|
||||||
std::cout << entry2.first << " " << entry2.second->getType() << std::endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
41
src/World/Block.hpp
Normal file
41
src/World/Block.hpp
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
/*
|
||||||
|
Copyright (c) 2015, Matthias Schiffer <mschiffer@universe-factory.net>
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
1. Redistributions of source code must retain the above copyright notice,
|
||||||
|
this list of conditions and the following disclaimer.
|
||||||
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
this list of conditions and the following disclaimer in the documentation
|
||||||
|
and/or other materials provided with the distribution.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
|
||||||
|
namespace MinedMap {
|
||||||
|
namespace World {
|
||||||
|
|
||||||
|
struct Block {
|
||||||
|
uint8_t id;
|
||||||
|
uint8_t data;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -39,6 +39,20 @@
|
||||||
namespace MinedMap {
|
namespace MinedMap {
|
||||||
namespace World {
|
namespace World {
|
||||||
|
|
||||||
|
Chunk::Chunk(Buffer buffer) {
|
||||||
|
size_t size = buffer.get32();
|
||||||
|
|
||||||
|
Buffer buffer2(buffer.get(size), size);
|
||||||
|
|
||||||
|
uint8_t format = buffer2.get8();
|
||||||
|
if (format != 2)
|
||||||
|
throw std::invalid_argument("unknown chunk format");
|
||||||
|
|
||||||
|
inflateChunk(buffer2);
|
||||||
|
parseChunk();
|
||||||
|
analyzeChunk();
|
||||||
|
}
|
||||||
|
|
||||||
void Chunk::inflateChunk(Buffer buffer) {
|
void Chunk::inflateChunk(Buffer buffer) {
|
||||||
size_t outlen = 0;
|
size_t outlen = 0;
|
||||||
uint8_t *output = nullptr;
|
uint8_t *output = nullptr;
|
||||||
|
@ -97,11 +111,11 @@ void Chunk::analyzeChunk() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t Chunk::getBlockAt(const std::shared_ptr<const NBT::CompoundTag> §ion, size_t x, size_t y, size_t z) {
|
uint8_t Chunk::getBlockAt(const std::shared_ptr<const NBT::CompoundTag> §ion, size_t x, size_t y, size_t z) const {
|
||||||
return (*section->get<NBT::ByteArrayTag>("Blocks"))[getIndex(x, y, z)];
|
return (*section->get<NBT::ByteArrayTag>("Blocks"))[getIndex(x, y, z)];
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t Chunk::getDataAt(const std::shared_ptr<const NBT::CompoundTag> §ion, size_t x, size_t y, size_t z) {
|
uint8_t Chunk::getDataAt(const std::shared_ptr<const NBT::CompoundTag> §ion, size_t x, size_t y, size_t z) const {
|
||||||
size_t i = getIndex(x, y, z);
|
size_t i = getIndex(x, y, z);
|
||||||
uint8_t v = (*section->get<NBT::ByteArrayTag>("Data"))[i / 2];
|
uint8_t v = (*section->get<NBT::ByteArrayTag>("Data"))[i / 2];
|
||||||
|
|
||||||
|
@ -111,18 +125,37 @@ uint8_t Chunk::getDataAt(const std::shared_ptr<const NBT::CompoundTag> §ion,
|
||||||
return (v & 0xf);
|
return (v & 0xf);
|
||||||
}
|
}
|
||||||
|
|
||||||
Chunk::Chunk(Buffer buffer) {
|
Chunk::Blocks Chunk::getTopLayer() const {
|
||||||
size_t size = buffer.get32();
|
size_t done = 0;
|
||||||
|
Blocks blocks = {};
|
||||||
|
|
||||||
Buffer buffer2(buffer.get(size), size);
|
for (auto it = sections->rbegin(); it != sections->rend(); ++it) {
|
||||||
|
if (done == SIZE*SIZE)
|
||||||
|
break;
|
||||||
|
|
||||||
uint8_t format = buffer2.get8();
|
for (ssize_t y = SIZE-1; y >= 0; y--) {
|
||||||
if (format != 2)
|
if (done == SIZE*SIZE)
|
||||||
throw std::invalid_argument("unknown chunk format");
|
break;
|
||||||
|
|
||||||
inflateChunk(buffer2);
|
for (size_t x = 0; x < SIZE; x++) {
|
||||||
parseChunk();
|
for (size_t z = 0; z < SIZE; z++) {
|
||||||
analyzeChunk();
|
if (blocks.blocks[x][z].id)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
uint8_t block = getBlockAt(*it, x, y, z);
|
||||||
|
if (block) {
|
||||||
|
blocks.blocks[x][z].id = block;
|
||||||
|
blocks.blocks[x][z].data = getDataAt(*it, x, y, z);
|
||||||
|
done++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cerr << "Done: " << done << std::endl;
|
||||||
|
|
||||||
|
return blocks;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
|
||||||
|
#include "Block.hpp"
|
||||||
#include "../Buffer.hpp"
|
#include "../Buffer.hpp"
|
||||||
#include "../UniqueCPtr.hpp"
|
#include "../UniqueCPtr.hpp"
|
||||||
#include "../NBT/CompoundTag.hpp"
|
#include "../NBT/CompoundTag.hpp"
|
||||||
|
@ -43,7 +44,18 @@ class Chunk : public std::shared_ptr<const NBT::CompoundTag> {
|
||||||
public:
|
public:
|
||||||
static const size_t SIZE = 16;
|
static const size_t SIZE = 16;
|
||||||
|
|
||||||
|
struct Blocks {
|
||||||
|
Block blocks[SIZE][SIZE];
|
||||||
|
};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
static size_t getIndex(size_t x, size_t y, size_t z) {
|
||||||
|
if (x >= SIZE || y >= SIZE || z >= SIZE)
|
||||||
|
throw std::range_error("Chunk::getIndex(): bad coordinates");
|
||||||
|
|
||||||
|
return 256*y + 16*z + x;
|
||||||
|
}
|
||||||
|
|
||||||
size_t len;
|
size_t len;
|
||||||
UniqueCPtr<uint8_t[]> data;
|
UniqueCPtr<uint8_t[]> data;
|
||||||
|
|
||||||
|
@ -56,15 +68,8 @@ private:
|
||||||
void parseChunk();
|
void parseChunk();
|
||||||
void analyzeChunk();
|
void analyzeChunk();
|
||||||
|
|
||||||
size_t getIndex(size_t x, size_t y, size_t z) {
|
uint8_t getBlockAt(const std::shared_ptr<const NBT::CompoundTag> §ion, size_t x, size_t y, size_t z) const;
|
||||||
if (x >= SIZE || y >= SIZE || z >= SIZE)
|
uint8_t getDataAt(const std::shared_ptr<const NBT::CompoundTag> §ion, size_t x, size_t y, size_t z) const;
|
||||||
throw std::range_error("Chunk::getIndex(): bad coordinates");
|
|
||||||
|
|
||||||
return 256*y + 16*z + x;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t getBlockAt(const std::shared_ptr<const NBT::CompoundTag> §ion, size_t x, size_t y, size_t z);
|
|
||||||
uint8_t getDataAt(const std::shared_ptr<const NBT::CompoundTag> §ion, size_t x, size_t y, size_t z);
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Chunk(Buffer buffer);
|
Chunk(Buffer buffer);
|
||||||
|
@ -72,6 +77,8 @@ public:
|
||||||
const NBT::ListTag<NBT::CompoundTag> & getSections() const {
|
const NBT::ListTag<NBT::CompoundTag> & getSections() const {
|
||||||
return *sections;
|
return *sections;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Blocks getTopLayer() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue