minetestmapper/TileGenerator.cpp

650 lines
16 KiB
C++
Raw Normal View History

2012-08-23 12:46:22 +02:00
/*
* =====================================================================
* Version: 1.0
* Created: 23.08.2012 12:35:53
* Author: Miroslav Bendík
* Company: LinuxOS.sk
* =====================================================================
*/
2012-08-23 14:21:34 +02:00
#include <cstdio>
#include <cstdlib>
#include <fstream>
2012-08-25 15:06:11 +02:00
#include <gdfontmb.h>
#include <iostream>
2012-08-24 09:46:14 +02:00
#include <sstream>
2012-08-24 11:49:42 +02:00
#include <zlib.h>
2012-08-25 16:29:41 +02:00
#include <boost/filesystem.hpp>
2012-08-23 12:46:22 +02:00
#include "TileGenerator.h"
using namespace std;
static inline sqlite3_int64 pythonmodulo(sqlite3_int64 i, sqlite3_int64 mod)
{
if (i >= 0) {
return i % mod;
}
else {
return mod - ((-i) % mod);
}
}
static inline int unsignedToSigned(long i, long max_positive)
{
if (i < max_positive) {
return i;
}
else {
return i - 2l * max_positive;
}
}
static inline int readU16(const char *data)
{
return int(data[0]) * 256 + data[1];
}
static inline int rgb2int(uint8_t r, uint8_t g, uint8_t b)
{
return (r << 16) + (g << 8) + b;
}
static inline int readBlockContent(const unsigned char *mapData, int version, int datapos)
{
if (version >= 24) {
size_t index = datapos << 1;
return (mapData[index] << 8) | mapData[index + 1];
}
else if (version >= 20) {
if (mapData[datapos] <= 0x80) {
return mapData[datapos];
}
else {
return (int(mapData[datapos]) << 4) | (int(mapData[datapos + 0x2000]) >> 4);
}
}
else {
throw VersionError();
}
}
static inline std::string zlibDecompress(const char *data, std::size_t size, std::size_t *processed)
{
string buffer;
const size_t BUFSIZE = 128 * 1024;
uint8_t temp_buffer[BUFSIZE];
z_stream strm;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.next_in = Z_NULL;
strm.avail_in = size;
if (inflateInit(&strm) != Z_OK) {
throw DecompressError();
}
strm.next_in = reinterpret_cast<Bytef *>(const_cast<char *>(data));
int ret = 0;
do {
strm.avail_out = BUFSIZE;
strm.next_out = temp_buffer;
ret = inflate(&strm, Z_NO_FLUSH);
buffer += string(reinterpret_cast<char *>(temp_buffer), BUFSIZE - strm.avail_out);
} while (ret == Z_OK);
if (ret != Z_STREAM_END) {
throw DecompressError();
}
*processed = (const char *)strm.next_in - (const char *)data;
(void)inflateEnd(&strm);
return buffer;
}
2012-08-25 13:19:58 +02:00
static inline int colorSafeBounds(int color)
{
if (color > 255) {
return 255;
}
else if (color < 0) {
return 0;
}
else {
return color;
}
}
2012-08-23 12:46:22 +02:00
TileGenerator::TileGenerator():
2012-08-24 09:46:14 +02:00
m_bgColor(255, 255, 255),
m_scaleColor(0, 0, 0),
m_originColor(255, 0, 0),
m_playerColor(255, 0, 0),
2012-08-23 12:46:22 +02:00
m_drawOrigin(false),
m_drawPlayers(false),
m_drawScale(false),
2012-08-25 14:11:55 +02:00
m_border(0),
2012-08-23 14:06:16 +02:00
m_db(0),
2012-08-23 14:21:34 +02:00
m_image(0),
2012-08-23 14:06:16 +02:00
m_xMin(0),
m_xMax(0),
m_zMin(0),
m_zMax(0)
2012-08-23 12:46:22 +02:00
{
}
TileGenerator::~TileGenerator()
{
2012-08-23 13:32:22 +02:00
if (m_db != 0) {
sqlite3_close(m_db);
m_db = 0;
}
2012-08-23 12:46:22 +02:00
}
void TileGenerator::setBgColor(const std::string &bgColor)
{
2012-08-23 14:43:11 +02:00
m_bgColor = parseColor(bgColor);
2012-08-23 12:46:22 +02:00
}
void TileGenerator::setScaleColor(const std::string &scaleColor)
{
2012-08-23 14:43:11 +02:00
m_scaleColor = parseColor(scaleColor);
2012-08-23 12:46:22 +02:00
}
void TileGenerator::setOriginColor(const std::string &originColor)
{
2012-08-23 14:43:11 +02:00
m_originColor = parseColor(originColor);
2012-08-23 12:46:22 +02:00
}
void TileGenerator::setPlayerColor(const std::string &playerColor)
{
2012-08-23 14:43:11 +02:00
m_playerColor = parseColor(playerColor);
}
Color TileGenerator::parseColor(const std::string &color)
{
Color parsed;
if (color.length() != 7) {
throw ColorError();
}
if (color[0] != '#') {
throw ColorError();
}
long col = strtol(color.c_str() + 1, NULL, 16);
parsed.b = col % 256;
col = col / 256;
parsed.g = col % 256;
col = col / 256;
parsed.r = col % 256;
return parsed;
2012-08-23 12:46:22 +02:00
}
void TileGenerator::setDrawOrigin(bool drawOrigin)
{
m_drawOrigin = drawOrigin;
}
void TileGenerator::setDrawPlayers(bool drawPlayers)
{
m_drawPlayers = drawPlayers;
}
void TileGenerator::setDrawScale(bool drawScale)
{
m_drawScale = drawScale;
2012-08-25 14:11:55 +02:00
if (m_drawScale) {
m_border = 40;
}
2012-08-23 12:46:22 +02:00
}
void TileGenerator::parseColorsFile(const std::string &fileName)
{
ifstream in;
in.open(fileName.c_str(), ifstream::in);
if (!in.is_open()) {
std::cerr << "File colors.txt does not exist" << std::endl;
exit(-2);
}
while (in.good()) {
string name;
Color color;
in >> name;
if (name[0] == '#') {
in.ignore(65536, '\n');
in >> name;
}
while (name == "\n" && in.good()) {
in >> name;
}
int r, g, b;
in >> r;
in >> g;
in >> b;
if (in.good()) {
color.r = r;
color.g = g;
color.b = b;
2012-08-24 22:51:17 +02:00
m_colors[name] = color;
}
}
}
2012-08-23 14:21:34 +02:00
void TileGenerator::generate(const std::string &input, const std::string &output)
2012-08-23 13:32:22 +02:00
{
openDb(input);
loadBlocks();
2012-08-23 14:21:34 +02:00
createImage();
2012-08-23 15:35:00 +02:00
renderMap();
2012-08-25 14:11:55 +02:00
if (m_drawScale) {
renderScale();
}
2012-08-25 15:21:51 +02:00
if (m_drawOrigin) {
renderOrigin();
}
2012-08-25 16:29:41 +02:00
if (m_drawPlayers) {
renderPlayers(input);
}
2012-08-23 14:21:34 +02:00
writeImage(output);
2012-08-23 13:32:22 +02:00
}
void TileGenerator::openDb(const std::string &input)
{
string db_name = input + "map.sqlite";
if (sqlite3_open(db_name.c_str(), &m_db) != SQLITE_OK) {
throw DbError();
}
}
void TileGenerator::loadBlocks()
{
sqlite3_stmt *statement;
string sql = "SELECT pos FROM blocks";
if (sqlite3_prepare_v2(m_db, sql.c_str(), sql.length(), &statement, 0) == SQLITE_OK) {
int result = 0;
while (true) {
result = sqlite3_step(statement);
if(result == SQLITE_ROW) {
sqlite3_int64 blocknum = sqlite3_column_int64(statement, 0);
2012-08-23 14:06:16 +02:00
BlockPos pos = decodeBlockPos(blocknum);
if (pos.x > SectorXMax || pos.x < SectorXMin || pos.z > SectorZMax || pos.z < SectorZMin) {
continue;
}
if (pos.x < m_xMin) {
m_xMin = pos.x;
}
if (pos.x > m_xMax) {
m_xMax = pos.x;
}
if (pos.z < m_zMin) {
m_zMin = pos.z;
}
if (pos.z > m_zMax) {
m_zMax = pos.z;
}
2012-08-23 15:35:00 +02:00
m_positions.push_back(std::pair<int, int>(pos.x, pos.z));
2012-08-23 13:32:22 +02:00
}
else {
break;
}
}
2012-08-23 15:35:00 +02:00
m_positions.sort();
m_positions.unique();
2012-08-23 13:32:22 +02:00
}
else {
throw DbError();
}
}
2012-08-24 09:46:14 +02:00
inline BlockPos TileGenerator::decodeBlockPos(sqlite3_int64 blockId) const
2012-08-23 13:32:22 +02:00
{
BlockPos pos;
pos.x = unsignedToSigned(pythonmodulo(blockId, 4096), 2048);
2012-08-23 14:06:16 +02:00
blockId = (blockId - pos.x) / 4096;
pos.y = unsignedToSigned(pythonmodulo(blockId, 4096), 2048);
2012-08-23 14:06:16 +02:00
blockId = (blockId - pos.y) / 4096;
pos.z = unsignedToSigned(pythonmodulo(blockId, 4096), 2048);
2012-08-23 13:32:22 +02:00
return pos;
}
2012-08-23 14:21:34 +02:00
void TileGenerator::createImage()
{
2012-08-25 13:19:58 +02:00
m_mapWidth = (m_xMax - m_xMin + 1) * 16;
m_mapHeight = (m_zMax - m_zMin + 1) * 16;
2012-08-25 14:11:55 +02:00
m_image = gdImageCreateTrueColor(m_mapWidth + m_border, m_mapHeight + m_border);
2012-08-25 13:19:58 +02:00
m_blockPixelAttributes.setWidth(m_mapWidth);
2012-08-23 14:21:34 +02:00
// Background
2012-08-25 14:11:55 +02:00
gdImageFilledRectangle(m_image, 0, 0, m_mapWidth + m_border - 1, m_mapHeight + m_border -1, rgb2int(m_bgColor.r, m_bgColor.g, m_bgColor.b));
2012-08-23 14:21:34 +02:00
}
2012-08-23 15:35:00 +02:00
void TileGenerator::renderMap()
{
sqlite3_stmt *statement;
string sql = "SELECT pos, data FROM blocks WHERE (pos >= ? AND pos <= ?)";
2012-08-23 15:35:00 +02:00
if (sqlite3_prepare_v2(m_db, sql.c_str(), sql.length(), &statement, 0) != SQLITE_OK) {
throw DbError();
}
2012-08-24 09:46:14 +02:00
std::list<int> zlist = getZValueList();
2012-08-24 11:01:48 +02:00
for (std::list<int>::iterator zPosition = zlist.begin(); zPosition != zlist.end(); ++zPosition) {
int zPos = *zPosition;
map<int, BlockList> blocks = getBlocksOnZ(zPos, statement);
for (std::list<std::pair<int, int> >::const_iterator position = m_positions.begin(); position != m_positions.end(); ++position) {
if (position->second != zPos) {
continue;
}
2012-08-24 22:51:17 +02:00
for (int i = 0; i < 16; ++i) {
m_readedPixels[i] = 0;
}
2012-08-24 11:01:48 +02:00
int xPos = position->first;
blocks[xPos].sort();
const BlockList &blockStack = blocks[xPos];
for (BlockList::const_iterator it = blockStack.begin(); it != blockStack.end(); ++it) {
2012-08-24 22:51:17 +02:00
const BlockPos &pos = it->first;
2012-08-24 11:49:42 +02:00
const char *data = it->second.c_str();
size_t length = it->second.length();
uint8_t version = data[0];
//uint8_t flags = data[1];
size_t dataOffset = 0;
if (version >= 22) {
dataOffset = 4;
}
else {
dataOffset = 2;
}
size_t processed;
2012-08-24 12:10:26 +02:00
string mapData = zlibDecompress(data + dataOffset, length - dataOffset, &processed);
dataOffset += processed;
string mapMetadata = zlibDecompress(data + dataOffset, length - dataOffset, &processed);
2012-08-24 14:13:46 +02:00
dataOffset += processed;
2012-08-24 12:10:26 +02:00
2012-08-24 14:13:46 +02:00
// Skip unused data
2012-08-24 12:10:26 +02:00
if (version <= 21) {
dataOffset += 2;
}
if (version == 23) {
dataOffset += 1;
}
if (version == 24) {
2012-08-24 14:13:46 +02:00
uint8_t ver = data[dataOffset++];
2012-08-24 12:10:26 +02:00
if (ver == 1) {
2012-08-24 14:13:46 +02:00
int num = readU16(data + dataOffset);
dataOffset += 2;
dataOffset += 10 * num;
}
}
// Skip unused static objects
dataOffset++; // Skip static object version
int staticObjectCount = readU16(data + dataOffset);
dataOffset += 2;
for (int i = 0; i < staticObjectCount; ++i) {
dataOffset += 13;
int dataSize = readU16(data + dataOffset);
dataOffset += dataSize + 2;
}
dataOffset += 4; // Skip timestamp
2012-08-24 22:51:17 +02:00
m_blockAirId = -1;
m_blockIgnoreId = -1;
2012-08-24 14:13:46 +02:00
// Read mapping
if (version >= 22) {
dataOffset++; // mapping version
int numMappings = readU16(data + dataOffset);
dataOffset += 2;
for (int i = 0; i < numMappings; ++i) {
int nodeId = readU16(data + dataOffset);
dataOffset += 2;
int nameLen = readU16(data + dataOffset);
dataOffset += 2;
2012-08-24 22:51:17 +02:00
string name = string(data + dataOffset, nameLen);
if (name == "air") {
m_blockAirId = nodeId;
}
else if (name == "ignore") {
m_blockIgnoreId = nodeId;
}
else {
m_nameMap[nodeId] = name;
}
2012-08-24 14:13:46 +02:00
dataOffset += nameLen;
2012-08-24 12:10:26 +02:00
}
}
2012-08-24 14:48:55 +02:00
// Node timers
if (version >= 25) {
dataOffset++;
int numTimers = readU16(data + dataOffset);
dataOffset += 2;
dataOffset += numTimers * 10;
}
2012-08-24 22:51:17 +02:00
renderMapBlock(mapData, pos, version);
bool allReaded = true;
for (int i = 0; i < 16; ++i) {
if (m_readedPixels[i] != 0xffff) {
allReaded = false;
}
}
if (allReaded) {
break;
}
}
}
2012-08-25 13:19:58 +02:00
renderShading(zPos);
2012-08-24 22:51:17 +02:00
}
}
inline void TileGenerator::renderMapBlock(const std::string &mapBlock, const BlockPos &pos, int version)
{
int xBegin = (pos.x - m_xMin) * 16;
int zBegin = (m_zMax - pos.z) * 16;
const unsigned char *mapData = reinterpret_cast<const unsigned char *>(mapBlock.c_str());
for (int z = 0; z < 16; ++z) {
2012-08-25 14:11:55 +02:00
int imageY = getImageY(zBegin + 15 - z);
2012-08-24 22:51:17 +02:00
for (int x = 0; x < 16; ++x) {
if (m_readedPixels[z] & (1 << x)) {
continue;
}
2012-08-25 14:11:55 +02:00
int imageX = getImageX(xBegin + x);
2012-08-24 22:51:17 +02:00
for (int y = 15; y >= 0; --y) {
int position = x + (y << 4) + (z << 8);
int content = readBlockContent(mapData, version, position);
if (content == m_blockIgnoreId || content == m_blockAirId) {
continue;
}
std::map<int, std::string>::iterator blockName = m_nameMap.find(content);
if (blockName != m_nameMap.end()) {
const string &name = blockName->second;
ColorMap::const_iterator color = m_colors.find(name);
if (color != m_colors.end()) {
const Color &c = color->second;
m_image->tpixels[imageY][imageX] = rgb2int(c.r, c.g, c.b);
m_readedPixels[z] |= (1 << x);
2012-08-25 13:19:58 +02:00
m_blockPixelAttributes.attribute(15 - z, xBegin + x).height = pos.y * 16 + y;
2012-08-24 22:51:17 +02:00
}
break;
}
2012-08-24 11:01:48 +02:00
}
}
2012-08-24 09:46:14 +02:00
}
}
2012-08-23 15:35:00 +02:00
2012-08-25 13:19:58 +02:00
inline void TileGenerator::renderShading(int zPos)
{
int zBegin = (m_zMax - zPos) * 16;
for (int z = 1; z < 17; ++z) {
int imageY = zBegin + z;
if (imageY >= m_mapHeight) {
continue;
}
2012-08-25 14:11:55 +02:00
imageY = getImageY(imageY);
2012-08-25 13:19:58 +02:00
for (int x = 0; x < m_mapWidth; ++x) {
2012-08-25 13:27:40 +02:00
if (!m_blockPixelAttributes.attribute(z, x).valid_height() || !m_blockPixelAttributes.attribute(z, x - 1).valid_height() || !m_blockPixelAttributes.attribute(z - 1, x).valid_height()) {
continue;
}
2012-08-25 13:19:58 +02:00
int y = m_blockPixelAttributes.attribute(z, x).height;
int y1 = m_blockPixelAttributes.attribute(z, x - 1).height;
int y2 = m_blockPixelAttributes.attribute(z - 1, x).height;
int d = ((y - y1) + (y - y2)) * 12;
if (d > 36) {
d = 36;
}
2012-08-25 14:11:55 +02:00
int sourceColor = m_image->tpixels[imageY][getImageX(x)] & 0xffffff;
2012-08-25 13:19:58 +02:00
int r = (sourceColor & 0xff0000) >> 16;
int g = (sourceColor & 0x00ff00) >> 8;
int b = (sourceColor & 0x0000ff);
r = colorSafeBounds(r + d);
g = colorSafeBounds(g + d);
b = colorSafeBounds(b + d);
2012-08-25 14:11:55 +02:00
m_image->tpixels[imageY][getImageX(x)] = rgb2int(r, g, b);
2012-08-25 13:19:58 +02:00
}
}
m_blockPixelAttributes.scroll();
}
2012-08-25 14:11:55 +02:00
void TileGenerator::renderScale()
{
2012-08-25 15:06:11 +02:00
int color = rgb2int(m_scaleColor.r, m_scaleColor.g, m_scaleColor.b);
gdImageString(m_image, gdFontGetMediumBold(), 24, 0, reinterpret_cast<unsigned char *>(const_cast<char *>("X")), color);
gdImageString(m_image, gdFontGetMediumBold(), 2, 24, reinterpret_cast<unsigned char *>(const_cast<char *>("Z")), color);
string scaleText;
for (int i = (m_xMin / 4) * 4; i <= m_xMax; i += 4) {
stringstream buf;
buf << i * 16;
scaleText = buf.str();
int xPos = m_xMin * -16 + i * 16 + m_border;
gdImageString(m_image, gdFontGetMediumBold(), xPos + 2, 0, reinterpret_cast<unsigned char *>(const_cast<char *>(scaleText.c_str())), color);
gdImageLine(m_image, xPos, 0, xPos, m_border - 1, color);
}
for (int i = (m_zMax / 4) * 4; i >= m_zMin; i -= 4) {
stringstream buf;
buf << i * 16;
scaleText = buf.str();
int yPos = m_mapHeight - 1 - (i * 16 - m_zMin * 16) + m_border;
gdImageString(m_image, gdFontGetMediumBold(), 2, yPos, reinterpret_cast<unsigned char *>(const_cast<char *>(scaleText.c_str())), color);
gdImageLine(m_image, 0, yPos, m_border - 1, yPos, color);
}
2012-08-25 14:11:55 +02:00
}
2012-08-25 15:21:51 +02:00
void TileGenerator::renderOrigin()
{
int imageX = -m_xMin * 16 + m_border;
int imageY = m_mapHeight - m_zMin * -16 + m_border;
gdImageArc(m_image, imageX, imageY, 12, 12, 0, 360, rgb2int(m_originColor.r, m_originColor.g, m_originColor.b));
}
2012-08-25 16:29:41 +02:00
void TileGenerator::renderPlayers(const std::string &inputPath)
{
int color = rgb2int(m_playerColor.r, m_playerColor.g, m_playerColor.b);
string playersPath = inputPath + "players";
boost::filesystem::path path(playersPath.c_str());
boost::filesystem::directory_iterator end_iter;
boost::filesystem::directory_iterator iter(path);
for (; iter != end_iter; ++iter) {
if (!boost::filesystem::is_directory(iter->status())) {
string path = iter->path().string();
ifstream in;
in.open(path.c_str(), ifstream::in);
string buffer;
string name;
string position;
while (getline(in, buffer)) {
if (buffer.find("name = ") == 0) {
name = buffer.substr(7);
}
else if (buffer.find("position = ") == 0) {
position = buffer.substr(12, buffer.length() - 13);
}
}
double x, y, z;
char comma;
istringstream positionStream(position, istringstream::in);
positionStream >> x;
positionStream >> comma;
positionStream >> y;
positionStream >> comma;
positionStream >> z;
int imageX = x / 10 - m_xMin * 16 + m_border;
int imageY = m_mapHeight - (z / 10 - m_zMin * 16) + m_border;
gdImageArc(m_image, imageX, imageY, 5, 5, 0, 360, color);
gdImageString(m_image, gdFontGetMediumBold(), imageX + 2, imageY + 2, reinterpret_cast<unsigned char *>(const_cast<char *>(name.c_str())), color);
}
}
}
2012-08-24 09:46:14 +02:00
inline std::list<int> TileGenerator::getZValueList() const
{
std::list<int> zlist;
for (std::list<std::pair<int, int> >::const_iterator position = m_positions.begin(); position != m_positions.end(); ++position) {
zlist.push_back(position->second);
}
zlist.sort();
zlist.unique();
return zlist;
}
2012-08-24 10:44:48 +02:00
std::map<int, TileGenerator::BlockList> TileGenerator::getBlocksOnZ(int zPos, sqlite3_stmt *statement) const
2012-08-24 09:46:14 +02:00
{
2012-08-24 10:44:48 +02:00
map<int, BlockList> blocks;
2012-08-24 09:46:14 +02:00
2012-08-24 22:51:17 +02:00
sqlite3_int64 psMin;
sqlite3_int64 psMax;
psMin = (static_cast<sqlite3_int64>(zPos) * 16777216l) - 0x800000;
psMax = (static_cast<sqlite3_int64>(zPos) * 16777216l) + 0x7fffff;
sqlite3_bind_int64(statement, 1, psMin);
sqlite3_bind_int64(statement, 2, psMax);
2012-08-24 09:46:14 +02:00
int result = 0;
while (true) {
result = sqlite3_step(statement);
if(result == SQLITE_ROW) {
sqlite3_int64 blocknum = sqlite3_column_int64(statement, 0);
2012-08-24 10:44:48 +02:00
const char *data = reinterpret_cast<const char *>(sqlite3_column_blob(statement, 1));
int size = sqlite3_column_bytes(statement, 1);
2012-08-24 09:46:14 +02:00
BlockPos pos = decodeBlockPos(blocknum);
2012-08-24 10:44:48 +02:00
blocks[pos.x].push_back(Block(pos, string(data, size)));
2012-08-24 09:46:14 +02:00
}
else {
break;
2012-08-23 15:35:00 +02:00
}
}
2012-08-24 09:46:14 +02:00
sqlite3_reset(statement);
2012-08-24 10:44:48 +02:00
return blocks;
2012-08-23 15:35:00 +02:00
}
2012-08-23 14:21:34 +02:00
void TileGenerator::writeImage(const std::string &output)
{
FILE *out;
out = fopen(output.c_str(), "w");
gdImagePng(m_image, out);
fclose(out);
gdImageDestroy(m_image);
}
2012-08-25 14:11:55 +02:00
inline int TileGenerator::getImageX(int val) const
{
return val + m_border;
}
inline int TileGenerator::getImageY(int val) const
{
return val + m_border;
}