Info: restructure mipmap level data structure

This commit is contained in:
Matthias Schiffer 2021-02-13 00:34:33 +01:00
parent cbc4a946c6
commit c06af49068
Signed by: neocturne
GPG key ID: 16EF3F64CB201D9C
2 changed files with 22 additions and 13 deletions

View file

@ -46,12 +46,14 @@ void Info::writeJSON(const char *filename) const {
std::fprintf(f, "{");
std::fprintf(f, "\"mipmaps\":[");
for (size_t level = 0; level < regions.size(); level++) {
if (level != 0)
bool first_level = true;
for (const auto &level : levels) {
if (!first_level)
std::fprintf(f, ",");
first_level = false;
int minX, maxX, minZ, maxZ;
std::tie(minX, maxX, minZ, maxZ) = getBounds(level);
std::tie(minX, maxX, minZ, maxZ) = level.bounds;
std::fprintf(f, "{");
std::fprintf(f, "\"bounds\":{");
@ -63,7 +65,7 @@ void Info::writeJSON(const char *filename) const {
std::fprintf(f, "\"regions\":{");
bool first_z = true;
for (const auto &item : regions[level]) {
for (const auto &item : level.regions) {
if (!first_z)
std::fprintf(f, ",");
first_z = false;

View file

@ -39,9 +39,14 @@
namespace MinedMap {
class Info {
public:
struct Level {
std::map<int, std::set<int>> regions;
std::tuple<int, int, int, int> bounds;
};
private:
std::vector<std::map<int, std::set<int>>> regions;
std::vector<std::tuple<int, int, int, int>> bounds;
std::vector<Level> levels;
int32_t spawnX, spawnZ;
@ -51,18 +56,18 @@ public:
}
std::tuple<int, int, int, int> getBounds(size_t level) const {
return bounds[level];
return levels[level].bounds;
}
void addRegion(int x, int z, size_t level) {
auto &level_r = regions[level];
auto z_regions = level_r.emplace(
auto &the_level = levels[level];
auto z_regions = the_level.regions.emplace(
std::piecewise_construct,
std::make_tuple(z),
std::make_tuple()).first;
z_regions->second.insert(x);
std::tuple<int, int, int, int> &b = bounds[level];
std::tuple<int, int, int, int> &b = the_level.bounds;
if (x < std::get<0>(b)) std::get<0>(b) = x;
if (x > std::get<1>(b)) std::get<1>(b) = x;
@ -71,12 +76,14 @@ public:
}
void addMipmapLevel() {
regions.emplace_back();
bounds.emplace_back(INT_MAX, INT_MIN, INT_MAX, INT_MIN);
levels.emplace_back(Level {
.regions = {},
.bounds = {INT_MAX, INT_MIN, INT_MAX, INT_MIN},
});
}
size_t getMipmapLevel() const {
return regions.size()-1;
return levels.size()-1;
}
void setSpawn(const std::pair<int32_t, int32_t> &v) {