minetestmapper/TileGenerator.cpp

704 lines
18 KiB
C++
Raw Normal View History

2012-08-23 14:21:34 +02:00
#include <cstdio>
#include <cstdlib>
#include <climits>
#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>
2014-03-09 12:32:13 +01:00
#include <stdexcept>
#include <cerrno>
#include <cstring>
#include <vector>
2012-09-01 13:01:31 +02:00
#include "config.h"
#include "PlayerAttributes.h"
2012-08-23 12:46:22 +02:00
#include "TileGenerator.h"
2012-09-18 10:43:34 +02:00
#include "ZlibDecompressor.h"
2014-07-08 14:54:27 +02:00
#include "util.h"
2014-03-05 21:41:27 +01:00
#include "db-sqlite3.h"
#if USE_LEVELDB
#include "db-leveldb.h"
#endif
2014-04-26 15:01:35 +02:00
#if USE_REDIS
#include "db-redis.h"
#endif
2012-08-23 12:46:22 +02:00
using namespace std;
2012-09-18 10:15:50 +02:00
static inline uint16_t readU16(const unsigned char *data)
{
2012-09-18 10:15:50 +02:00
return data[0] << 8 | data[1];
}
static inline int rgb2int(uint8_t r, uint8_t g, uint8_t b, uint8_t a=0xFF)
{
return (a << 24) + (r << 16) + (g << 8) + b;
}
static inline int color2int(Color c)
{
return rgb2int(c.r, c.g, c.b, c.a);
}
// rounds n (away from 0) to a multiple of f while preserving the sign of n
2014-09-01 18:37:07 +02:00
static inline int round_multiple_nosign(int n, int f)
{
int abs_n, sign;
abs_n = (n >= 0) ? n : -n;
sign = (n >= 0) ? 1 : -1;
if (abs_n % f == 0)
return n; // n == abs_n * sign
2014-09-01 18:37:07 +02:00
else
return sign * (abs_n + f - (abs_n % f));
2014-09-01 18:37:07 +02:00
}
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 {
std::ostringstream oss;
oss << "Unsupported map version " << version;
throw std::runtime_error(oss.str());
}
}
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;
}
}
static inline Color mixColors(Color a, Color b)
{
Color result;
double a1 = a.a / 255.0;
double a2 = b.a / 255.0;
result.r = (int) (a1 * a.r + a2 * (1 - a1) * b.r);
result.g = (int) (a1 * a.g + a2 * (1 - a1) * b.g);
result.b = (int) (a1 * a.b + a2 * (1 - a1) * b.b);
result.a = (int) (255 * (a1 + a2 * (1 - a1)));
return result;
}
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),
m_drawAlpha(false),
m_shading(true),
m_backend(""),
2012-08-25 14:11:55 +02:00
m_border(0),
2012-08-23 14:21:34 +02:00
m_image(0),
m_xMin(INT_MAX),
m_xMax(INT_MIN),
m_zMin(INT_MAX),
m_zMax(INT_MIN),
2014-03-05 18:06:05 +01:00
m_yMin(-30000),
m_yMax(30000),
2014-09-01 18:37:07 +02:00
m_geomX(-2048),
m_geomY(-2048),
m_geomX2(2048),
m_geomY2(2048)
2012-08-23 12:46:22 +02:00
{
}
TileGenerator::~TileGenerator()
{
}
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 std::runtime_error("Color not 7 characters long");
2012-08-23 14:43:11 +02:00
}
if (color[0] != '#') {
throw std::runtime_error("Color does not begin with #");
2012-08-23 14:43:11 +02:00
}
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::setDrawAlpha(bool drawAlpha)
{
m_drawAlpha = drawAlpha;
}
void TileGenerator::setShading(bool shading)
{
m_shading = shading;
}
void TileGenerator::setBackend(std::string backend)
{
m_backend = backend;
}
2012-11-24 19:25:13 +01:00
void TileGenerator::setGeometry(int x, int y, int w, int h)
{
2014-09-01 18:37:07 +02:00
m_geomX = round_multiple_nosign(x, 16) / 16;
m_geomY = round_multiple_nosign(y, 16) / 16;
m_geomX2 = round_multiple_nosign(x + w, 16) / 16;
m_geomY2 = round_multiple_nosign(y + h, 16) / 16;
2012-11-24 19:25:13 +01:00
}
2014-03-05 18:06:05 +01:00
void TileGenerator::setMinY(int y)
{
m_yMin = y;
}
void TileGenerator::setMaxY(int y)
{
m_yMax = y;
}
void TileGenerator::parseColorsFile(const std::string &fileName)
{
ifstream in;
in.open(fileName.c_str(), ifstream::in);
if (!in.is_open()) {
2012-09-01 15:51:02 +02:00
return;
}
2012-09-01 15:51:02 +02:00
parseColorsStream(in);
}
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
{
2012-09-01 13:41:00 +02:00
string input_path = input;
if (input_path[input.length() - 1] != PATH_SEPARATOR) {
input_path += PATH_SEPARATOR;
}
openDb(input_path);
2012-08-23 13:32:22 +02:00
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) {
2012-09-01 13:41:00 +02:00
renderPlayers(input_path);
2012-08-25 16:29:41 +02:00
}
2012-08-23 14:21:34 +02:00
writeImage(output);
2012-08-25 16:41:53 +02:00
printUnknown();
2012-08-23 13:32:22 +02:00
}
2012-09-01 15:51:02 +02:00
void TileGenerator::parseColorsStream(std::istream &in)
{
char line[128], *p;
2012-09-01 15:51:02 +02:00
while (in.good()) {
in.getline(line, 128);
p = line;
while(*p++ != '\0')
if(*p == '#') {
*p = '\0'; // Cut off at the first #
break;
}
char name[75];
#ifdef __MINGW32__
// MinGW's sscanf doesn't support %hhu
unsigned int r, g, b, a, t;
#else
uint8_t r, g, b, a, t;
#endif
a = 255;
t = 0;
#ifdef __MINGW32__
sscanf(line, "%75s %u %u %u %u %u", name, &r, &g, &b, &a, &t);
#else
sscanf(line, "%75s %hhu %hhu %hhu %hhu %hhu", name, &r, &g, &b, &a, &t);
#endif
if(strlen(name) == 0)
break;
ColorEntry color = ColorEntry(r, g, b, a, t);
m_colors[name] = color;
2012-09-01 15:51:02 +02:00
}
}
2012-08-23 13:32:22 +02:00
void TileGenerator::openDb(const std::string &input)
{
std::string backend = m_backend;
if(backend == "") {
std::ifstream ifs((input + "/world.mt").c_str());
if(!ifs.good())
throw std::runtime_error("Failed to read world.mt");
backend = get_setting("backend", ifs);
ifs.close();
}
2014-07-08 14:54:27 +02:00
if(backend == "sqlite3")
2014-03-05 21:41:27 +01:00
m_db = new DBSQLite3(input);
#if USE_LEVELDB
2014-07-08 14:54:27 +02:00
else if(backend == "leveldb")
2014-03-05 21:41:27 +01:00
m_db = new DBLevelDB(input);
2014-04-26 15:01:35 +02:00
#endif
#if USE_REDIS
2014-07-08 14:54:27 +02:00
else if(backend == "redis")
2014-04-26 15:01:35 +02:00
m_db = new DBRedis(input);
2014-03-05 21:41:27 +01:00
#endif
else
2014-07-08 14:54:27 +02:00
throw std::runtime_error(((std::string) "Unknown map backend: ") + backend);
2012-08-23 13:32:22 +02:00
}
void TileGenerator::loadBlocks()
{
std::vector<BlockPos> vec = m_db->getBlockPos();
for (std::vector<BlockPos>::iterator it = vec.begin(); it != vec.end(); ++it) {
BlockPos pos = *it;
// Check that it's in geometry (from --geometry option)
2014-09-01 18:37:07 +02:00
if (pos.x < m_geomX || pos.x >= m_geomX2 || pos.z < m_geomY || pos.z >= m_geomY2) {
2014-03-05 21:41:27 +01:00
continue;
}
// Check that it's between --miny and --maxy
if (pos.y < m_yMin * 16 || pos.y > m_yMax * 16) {
2014-03-05 21:41:27 +01:00
continue;
2012-08-23 13:32:22 +02:00
}
// Adjust minimum and maximum positions to the nearest block
2014-03-05 21:41:27 +01:00
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;
}
m_positions.push_back(std::pair<int, int>(pos.x, pos.z));
2012-08-23 13:32:22 +02:00
}
2014-03-22 09:30:14 +01:00
m_positions.sort();
m_positions.unique();
2012-08-23 13:32:22 +02:00
}
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
gdImageFilledRectangle(m_image, 0, 0, m_mapWidth + m_border - 1, m_mapHeight + m_border -1, color2int(m_bgColor));
2012-08-23 14:21:34 +02:00
}
2014-03-05 21:41:27 +01:00
void TileGenerator::renderMap()
{
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;
std::map<int16_t, BlockList> blocks;
m_db->getBlocksOnZ(blocks, zPos);
2012-08-24 11:01:48 +02:00
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;
2014-07-08 14:58:40 +02:00
m_readInfo[i] = 0;
}
for (int i = 0; i < 16; i++) {
for (int j = 0; j < 16; j++) {
m_col[i][j] = m_bgColor; // This will be drawn by renderMapBlockBottom() for y-rows with only 'air', 'ignore' or unknown nodes if --drawalpha is used
m_col[i][j].a = 0; // ..but set alpha to 0 to tell renderMapBlock() not to use this color to mix a shade
m_th[i][j] = 0;
}
2012-08-24 22:51:17 +02:00
}
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-09-18 10:15:50 +02:00
const unsigned char *data = it->second.c_str();
2012-08-24 11:49:42 +02:00
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;
}
2012-09-18 10:43:34 +02:00
ZlibDecompressor decompressor(data, length);
decompressor.setSeekPos(dataOffset);
ustring mapData = decompressor.decompress();
ustring mapMetadata = decompressor.decompress();
2012-09-18 10:43:34 +02:00
dataOffset = decompressor.seekPos();
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-09-18 10:15:50 +02:00
uint16_t num = readU16(data + dataOffset);
2012-08-24 14:13:46 +02:00
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;
2012-09-18 10:15:50 +02:00
uint16_t dataSize = readU16(data + dataOffset);
2012-08-24 14:13:46 +02:00
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
2012-09-18 10:15:50 +02:00
uint16_t numMappings = readU16(data + dataOffset);
2012-08-24 14:13:46 +02:00
dataOffset += 2;
for (int i = 0; i < numMappings; ++i) {
2012-09-18 10:15:50 +02:00
uint16_t nodeId = readU16(data + dataOffset);
2012-08-24 14:13:46 +02:00
dataOffset += 2;
2012-09-18 10:15:50 +02:00
uint16_t nameLen = readU16(data + dataOffset);
2012-08-24 14:13:46 +02:00
dataOffset += 2;
2012-09-18 10:15:50 +02:00
string name = string(reinterpret_cast<const char *>(data) + dataOffset, nameLen);
2012-08-24 22:51:17 +02:00
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++;
2012-09-18 10:15:50 +02:00
uint16_t numTimers = readU16(data + dataOffset);
2012-08-24 14:48:55 +02:00
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;
}
}
2014-07-08 14:58:40 +02:00
bool allReaded = true;
for (int i = 0; i < 16; ++i) {
if (m_readedPixels[i] != 0xffff) {
allReaded = false;
}
}
if (!allReaded) {
renderMapBlockBottom(blockStack.begin()->first);
}
2012-08-24 22:51:17 +02:00
}
if(m_shading)
renderShading(zPos);
2012-08-24 22:51:17 +02:00
}
}
inline void TileGenerator::renderMapBlock(const ustring &mapBlock, const BlockPos &pos, int version)
2012-08-24 22:51:17 +02:00
{
int xBegin = (pos.x - m_xMin) * 16;
int zBegin = (m_zMax - pos.z) * 16;
2012-09-18 10:43:34 +02:00
const unsigned char *mapData = mapBlock.c_str();
2014-03-26 16:28:24 +01:00
int minY = (pos.y * 16 > m_yMin) ? 0 : m_yMin - pos.y * 16;
int maxY = (pos.y * 16 < m_yMax) ? 15 : m_yMax - pos.y * 16;
2012-08-24 22:51:17 +02:00
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);
2014-03-26 16:28:24 +01:00
2014-03-05 18:06:05 +01:00
for (int y = maxY; y >= minY; --y) {
2012-08-24 22:51:17 +02:00
int position = x + (y << 4) + (z << 8);
int content = readBlockContent(mapData, version, position);
if (content == m_blockIgnoreId || content == m_blockAirId) {
continue;
}
2014-08-02 16:30:52 +02:00
NameMap::iterator blockName = m_nameMap.find(content);
if (blockName == m_nameMap.end())
2014-04-03 20:38:09 +02:00
continue;
const string &name = blockName->second;
ColorMap::const_iterator color = m_colors.find(name);
if (color != m_colors.end()) {
const Color c = color->second.to_color();
if (m_drawAlpha) {
2014-07-08 14:58:40 +02:00
if (m_col[z][x].a == 0)
m_col[z][x] = c;
2014-04-03 20:38:09 +02:00
else
2014-07-08 14:58:40 +02:00
m_col[z][x] = mixColors(m_col[z][x], c);
if(m_col[z][x].a == 0xFF) {
m_image->tpixels[imageY][imageX] = color2int(m_col[z][x]);
m_readedPixels[z] |= (1 << x);
m_blockPixelAttributes.attribute(15 - z, xBegin + x).thicken = m_th[z][x];
2014-04-03 20:38:09 +02:00
} else {
2014-07-08 14:58:40 +02:00
m_th[z][x] = (m_th[z][x] + color->second.t) / 2.0;
2014-04-03 20:38:09 +02:00
continue;
}
2014-07-08 14:58:40 +02:00
} else {
2014-04-03 20:38:09 +02:00
m_image->tpixels[imageY][imageX] = color2int(c);
2014-07-08 14:58:40 +02:00
m_readedPixels[z] |= (1 << x);
}
if(!(m_readInfo[z] & (1 << x))) {
m_blockPixelAttributes.attribute(15 - z, xBegin + x).height = pos.y * 16 + y;
m_readInfo[z] |= (1 << x);
}
} else {
m_unknownNodes.insert(name);
continue;
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
2014-07-08 14:58:40 +02:00
inline void TileGenerator::renderMapBlockBottom(const BlockPos &pos)
{
int xBegin = (pos.x - m_xMin) * 16;
int zBegin = (m_zMax - pos.z) * 16;
for (int z = 0; z < 16; ++z) {
int imageY = getImageY(zBegin + 15 - z);
for (int x = 0; x < 16; ++x) {
if (m_readedPixels[z] & (1 << x)) {
continue;
}
int imageX = getImageX(xBegin + x);
if (m_drawAlpha) {
m_image->tpixels[imageY][imageX] = color2int(m_col[z][x]);
m_readedPixels[z] |= (1 << x);
m_blockPixelAttributes.attribute(15 - z, xBegin + x).thicken = m_th[z][x];
}
}
}
}
2012-08-25 13:19:58 +02:00
inline void TileGenerator::renderShading(int zPos)
{
int zBegin = (m_zMax - zPos) * 16;
2012-09-01 13:34:27 +02:00
for (int z = 0; z < 16; ++z) {
2012-08-25 13:19:58 +02:00
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;
}
// more thickness -> less visible shadows: t=0 -> 100% visible, t=255 -> 0% visible
if (m_drawAlpha)
2014-04-03 20:38:09 +02:00
d = d * ((0xFF - m_blockPixelAttributes.attribute(z, x).thicken) / 255.0);
2012-08-25 14:11:55 +02:00
int sourceColor = m_image->tpixels[imageY][getImageX(x)] & 0xffffff;
uint8_t r = (sourceColor & 0xff0000) >> 16;
uint8_t g = (sourceColor & 0x00ff00) >> 8;
uint8_t b = (sourceColor & 0x0000ff);
2012-08-25 13:19:58 +02:00
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()
{
int color = color2int(m_scaleColor);
2012-08-25 15:06:11 +02:00
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, color2int(m_originColor));
2012-08-25 15:21:51 +02:00
}
2012-08-25 16:29:41 +02:00
void TileGenerator::renderPlayers(const std::string &inputPath)
{
int color = color2int(m_playerColor);
2012-08-25 16:29:41 +02:00
PlayerAttributes players(inputPath);
for (PlayerAttributes::Players::iterator player = players.begin(); player != players.end(); ++player) {
int imageX = player->x / 10 - m_xMin * 16 + m_border;
int imageY = m_mapHeight - (player->z / 10 - m_zMin * 16) + m_border;
2012-09-01 13:01:31 +02:00
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 *>(player->name.c_str())), color);
2012-09-01 13:01:31 +02:00
}
2012-08-25 16:29:41 +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();
2012-09-01 13:34:27 +02:00
zlist.reverse();
2012-08-24 09:46:14 +02:00
return zlist;
}
2012-08-23 14:21:34 +02:00
void TileGenerator::writeImage(const std::string &output)
{
FILE *out;
2012-08-28 11:27:51 +02:00
out = fopen(output.c_str(), "wb");
if (!out) {
std::ostringstream oss;
oss << "Error opening '" << output.c_str() << "': " << std::strerror(errno);
throw std::runtime_error(oss.str());
}
2012-08-23 14:21:34 +02:00
gdImagePng(m_image, out);
fclose(out);
gdImageDestroy(m_image);
}
2012-08-25 16:41:53 +02:00
void TileGenerator::printUnknown()
{
if (m_unknownNodes.size() > 0) {
std::cerr << "Unknown nodes:" << std::endl;
2014-08-02 16:30:52 +02:00
for (NameSet::iterator node = m_unknownNodes.begin(); node != m_unknownNodes.end(); ++node) {
2012-08-25 16:41:53 +02:00
std::cerr << *node << std::endl;
}
}
}
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;
}