diff options
Diffstat (limited to 'src/MinedMap.cpp')
-rw-r--r-- | src/MinedMap.cpp | 36 |
1 files changed, 26 insertions, 10 deletions
diff --git a/src/MinedMap.cpp b/src/MinedMap.cpp index bf6f4fb..5092ceb 100644 --- a/src/MinedMap.cpp +++ b/src/MinedMap.cpp @@ -52,16 +52,21 @@ namespace MinedMap { static const size_t DIM = World::Region::SIZE*World::Chunk::SIZE; -static void addChunk(uint32_t image[DIM*DIM], size_t X, size_t Z, const World::Chunk *chunk) { +static void addChunk(uint32_t image[DIM*DIM], uint8_t lightmap[2*DIM*DIM], size_t X, size_t Z, const World::Chunk *chunk) { World::Chunk::Blocks layer = chunk->getTopLayer(); for (size_t x = 0; x < World::Chunk::SIZE; x++) { - for (size_t z = 0; z < World::Chunk::SIZE; z++) - image[(Z*World::Chunk::SIZE+z)*DIM + X*World::Chunk::SIZE+x] = htonl(layer.blocks[x][z].getColor()); + for (size_t z = 0; z < World::Chunk::SIZE; z++) { + size_t i = (Z*World::Chunk::SIZE+z)*DIM + X*World::Chunk::SIZE+x; + const World::Block &block = layer.blocks[x][z]; + + image[i] = htonl(block.getColor()); + lightmap[2*i+1] = (1 - block.getBlockLight()/15.f)*128; + } } } -static void doRegion(const std::string &input, const std::string &output) { +static void doRegion(const std::string &input, const std::string &output, const std::string &output_light) { struct stat instat, outstat; if (stat(input.c_str(), &instat) < 0) { @@ -80,21 +85,30 @@ static void doRegion(const std::string &input, const std::string &output) { std::printf("Generating %s from %s...\n", output.c_str(), input.c_str()); const std::string tmpfile = output + ".tmp"; + const std::string tmpfile_light = output_light + ".tmp"; try { uint32_t image[DIM*DIM] = {}; - World::Region::visitChunks(input.c_str(), [&image] (size_t X, size_t Z, const World::Chunk *chunk) { addChunk(image, X, Z, chunk); }); + uint8_t lightmap[2*DIM*DIM] = {}; + World::Region::visitChunks(input.c_str(), [&] (size_t X, size_t Z, const World::Chunk *chunk) { addChunk(image, lightmap, X, Z, chunk); }); - PNG::write(tmpfile.c_str(), reinterpret_cast<const uint8_t*>(image), DIM, DIM); + PNG::write(tmpfile.c_str(), reinterpret_cast<const uint8_t*>(image), DIM, DIM, true); + PNG::write(tmpfile_light.c_str(), lightmap, DIM, DIM, false); struct timespec times[2] = {instat.st_mtim, instat.st_mtim}; if (utimensat(AT_FDCWD, tmpfile.c_str(), times, 0) < 0) std::fprintf(stderr, "Warning: failed to set utime on %s: %s\n", tmpfile.c_str(), std::strerror(errno)); + if (utimensat(AT_FDCWD, tmpfile_light.c_str(), times, 0) < 0) + std::fprintf(stderr, "Warning: failed to set utime on %s: %s\n", tmpfile_light.c_str(), std::strerror(errno)); if (std::rename(tmpfile.c_str(), output.c_str()) < 0) { std::fprintf(stderr, "Unable to save %s: %s\n", output.c_str(), std::strerror(errno)); unlink(tmpfile.c_str()); } + if (std::rename(tmpfile_light.c_str(), output_light.c_str()) < 0) { + std::fprintf(stderr, "Unable to save %s: %s\n", output_light.c_str(), std::strerror(errno)); + unlink(tmpfile_light.c_str()); + } } catch (const std::exception& ex) { std::fprintf(stderr, "Failed to generate %s: %s\n", output.c_str(), ex.what()); @@ -140,9 +154,11 @@ int main(int argc, char *argv[]) { std::string regiondir = inputdir + "/region"; std::string outputdir(argv[2]); - std::string regionoutdir = outputdir + "/0"; - makeDir(regionoutdir); + makeDir(outputdir + "/map"); + makeDir(outputdir + "/map/0"); + makeDir(outputdir + "/light"); + makeDir(outputdir + "/light/0"); DIR *dir = opendir(regiondir.c_str()); if (!dir) { @@ -160,8 +176,8 @@ int main(int argc, char *argv[]) { info.addRegion(x, z); - std::string name(entry->d_name); - doRegion(regiondir + "/" + name, regionoutdir + "/" + name.substr(0, name.length()-3) + "png"); + std::string name(entry->d_name), outname = name.substr(0, name.length()-3) + "png"; + doRegion(regiondir + "/" + name, outputdir + "/map/0/" + outname, outputdir + "/light/0/" + outname); } closedir(dir); |