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>
|
2012-08-23 12:55:31 +02:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
2012-08-24 09:46:14 +02:00
|
|
|
#include <sstream>
|
2012-08-24 10:44:48 +02:00
|
|
|
#include <vector>
|
2012-08-24 11:49:42 +02:00
|
|
|
#include <zlib.h>
|
2012-08-23 12:46:22 +02:00
|
|
|
#include "TileGenerator.h"
|
|
|
|
|
2012-08-23 12:55:31 +02:00
|
|
|
using namespace std;
|
|
|
|
|
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-23 13:32:22 +02:00
|
|
|
m_drawUnderground(false),
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TileGenerator::setDrawUnderground(bool drawUnderground)
|
|
|
|
{
|
|
|
|
m_drawUnderground = drawUnderground;
|
|
|
|
}
|
|
|
|
|
2012-08-23 12:55:31 +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()) {
|
|
|
|
m_colors[name] = color;
|
|
|
|
color.r = r;
|
|
|
|
color.g = g;
|
|
|
|
color.b = b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-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(blockId % 4096, 2048);
|
2012-08-23 14:06:16 +02:00
|
|
|
blockId = (blockId - pos.x) / 4096;
|
2012-08-23 13:32:22 +02:00
|
|
|
pos.y = unsignedToSigned(blockId % 4096, 2048);
|
2012-08-23 14:06:16 +02:00
|
|
|
blockId = (blockId - pos.y) / 4096;
|
2012-08-23 13:32:22 +02:00
|
|
|
pos.z = unsignedToSigned(blockId % 4096, 2048);
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
2012-08-24 09:46:14 +02:00
|
|
|
inline sqlite3_int64 TileGenerator::encodeBlockPos(int x, int y, int z) const
|
2012-08-23 15:35:00 +02:00
|
|
|
{
|
|
|
|
return sqlite3_int64(z) * 16777216l + sqlite3_int64(y) * 4096l + sqlite3_int64(x);
|
|
|
|
}
|
|
|
|
|
2012-08-24 09:46:14 +02:00
|
|
|
inline int TileGenerator::unsignedToSigned(long i, long max_positive) const
|
2012-08-23 13:32:22 +02:00
|
|
|
{
|
|
|
|
if (i < max_positive) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return i - 2l * max_positive;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-23 14:21:34 +02:00
|
|
|
void TileGenerator::createImage()
|
|
|
|
{
|
|
|
|
m_imgWidth = (m_xMax - m_xMin) * 16;
|
|
|
|
m_imgHeight = (m_zMax - m_zMin) * 16;
|
|
|
|
m_image = gdImageCreate(m_imgWidth, m_imgHeight);
|
|
|
|
// Background
|
2012-08-23 14:43:11 +02:00
|
|
|
gdImageColorAllocate(m_image, 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;
|
2012-08-24 08:31:31 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 11:49:42 +02:00
|
|
|
//const BlockPos &pos = it->first;
|
|
|
|
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;
|
|
|
|
zlibDecompress(data + dataOffset, length - dataOffset, &processed);
|
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-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
|
|
|
|
|
|
|
sqlite3_int64 psMin = encodeBlockPos(-2048, -2048, zPos);
|
|
|
|
sqlite3_int64 psMax = encodeBlockPos( 2047, 2047, zPos);
|
|
|
|
std::stringstream minStream;
|
|
|
|
std::stringstream maxStream;
|
|
|
|
minStream << psMin;
|
|
|
|
maxStream << psMax;
|
|
|
|
string minStr = minStream.str();
|
|
|
|
string maxStr = maxStream.str();
|
|
|
|
sqlite3_bind_text(statement, 1, minStr.c_str(), -1, SQLITE_TRANSIENT);
|
|
|
|
sqlite3_bind_text(statement, 2, maxStr.c_str(), -1, SQLITE_TRANSIENT);
|
|
|
|
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-24 11:49:42 +02:00
|
|
|
inline std::string TileGenerator::zlibDecompress(const char *data, std::size_t size, std::size_t *processed) const
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|