2015-02-02 17:56:43 +01:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "Info.hpp"
|
|
|
|
|
2015-02-04 11:38:06 +01:00
|
|
|
#include <cerrno>
|
2015-02-02 17:56:43 +01:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
|
|
|
|
namespace MinedMap {
|
|
|
|
|
2015-02-03 20:15:58 +01:00
|
|
|
void Info::writeJSON(const char *filename) const {
|
2015-02-02 17:56:43 +01:00
|
|
|
const std::string tmpfile = std::string(filename) + ".tmp";
|
|
|
|
|
2015-02-06 19:51:20 +01:00
|
|
|
FILE *f = std::fopen(tmpfile.c_str(), "w");
|
2015-02-02 17:56:43 +01:00
|
|
|
if (!f) {
|
|
|
|
std::fprintf(stderr, "Unable to open %s: %s\n", tmpfile.c_str(), std::strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-12 23:05:07 +01:00
|
|
|
std::fprintf(f, "{");
|
|
|
|
std::fprintf(f, "\"mipmaps\":[");
|
2015-02-03 20:15:58 +01:00
|
|
|
|
|
|
|
for (size_t level = 0; level < regions.size(); level++) {
|
|
|
|
int minX, maxX, minZ, maxZ;
|
|
|
|
std::tie(minX, maxX, minZ, maxZ) = getBounds(level);
|
|
|
|
|
2021-02-12 23:05:07 +01:00
|
|
|
std::fprintf(f, "{");
|
|
|
|
std::fprintf(f, "\"info\":{");
|
|
|
|
std::fprintf(f, "\"minX\":%i,", minX);
|
|
|
|
std::fprintf(f, "\"maxX\":%i,", maxX);
|
|
|
|
std::fprintf(f, "\"minZ\":%i,", minZ);
|
|
|
|
std::fprintf(f, "\"maxZ\":%i", maxZ);
|
|
|
|
std::fprintf(f, "},");
|
|
|
|
std::fprintf(f, "\"regions\":[");
|
2015-02-03 20:15:58 +01:00
|
|
|
|
|
|
|
for (int z = minZ; z <= maxZ; z++) {
|
2021-02-12 23:05:07 +01:00
|
|
|
std::fprintf(f, "[");
|
2015-02-03 20:15:58 +01:00
|
|
|
|
|
|
|
for (int x = minX; x <= maxX; x++) {
|
2015-02-06 19:51:20 +01:00
|
|
|
std::fprintf(f, "%s", regions[level].count(std::make_pair(x, z)) ? "true" : "false");
|
2015-02-03 20:15:58 +01:00
|
|
|
|
|
|
|
if (x < maxX)
|
2021-02-12 23:05:07 +01:00
|
|
|
std::fprintf(f, ",");
|
2015-02-03 20:15:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (z < maxZ)
|
2021-02-12 23:05:07 +01:00
|
|
|
std::fprintf(f, "],");
|
2015-02-03 20:15:58 +01:00
|
|
|
else
|
2021-02-12 23:05:07 +01:00
|
|
|
std::fprintf(f, "]");
|
2015-02-02 17:56:43 +01:00
|
|
|
}
|
|
|
|
|
2021-02-12 23:05:07 +01:00
|
|
|
std::fprintf(f, "]");
|
2015-02-03 20:15:58 +01:00
|
|
|
|
|
|
|
if (level < regions.size() - 1)
|
2021-02-12 23:05:07 +01:00
|
|
|
std::fprintf(f, "},");
|
2015-02-02 17:56:43 +01:00
|
|
|
else
|
2021-02-12 23:05:07 +01:00
|
|
|
std::fprintf(f, "}");
|
2015-02-02 17:56:43 +01:00
|
|
|
}
|
|
|
|
|
2021-02-12 23:05:07 +01:00
|
|
|
std::fprintf(f, "],");
|
|
|
|
std::fprintf(f, "\"spawn\":{");
|
|
|
|
std::fprintf(f, "\"x\":%li,", (long)spawnX);
|
|
|
|
std::fprintf(f, "\"z\":%li", (long)spawnZ);
|
|
|
|
std::fprintf(f, "}");
|
|
|
|
std::fprintf(f, "}");
|
2015-02-02 17:56:43 +01:00
|
|
|
|
2015-02-06 19:51:20 +01:00
|
|
|
std::fclose(f);
|
2015-02-02 17:56:43 +01:00
|
|
|
|
|
|
|
if (std::rename(tmpfile.c_str(), filename) < 0) {
|
|
|
|
std::fprintf(stderr, "Unable to save %s: %s\n", filename, std::strerror(errno));
|
2015-02-06 19:51:20 +01:00
|
|
|
std::remove(tmpfile.c_str());
|
2015-02-02 17:56:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|