2012-08-23 14:21:34 +02:00
|
|
|
#include <cstdio>
|
2018-03-25 16:47:23 +02:00
|
|
|
#include <cstdlib>
|
2014-03-05 17:19:37 +01:00
|
|
|
#include <climits>
|
2012-08-23 12:55:31 +02:00
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
2012-08-24 09:46:14 +02:00
|
|
|
#include <sstream>
|
2014-03-09 12:32:13 +01:00
|
|
|
#include <stdexcept>
|
2014-02-16 22:13:31 +01:00
|
|
|
#include <cstring>
|
2014-03-28 21:47:19 +01:00
|
|
|
#include <vector>
|
2018-03-25 15:19:48 +02:00
|
|
|
|
|
|
|
#include "TileGenerator.h"
|
2012-09-01 13:01:31 +02:00
|
|
|
#include "config.h"
|
2012-09-01 16:40:18 +02:00
|
|
|
#include "PlayerAttributes.h"
|
2018-03-25 15:19:48 +02:00
|
|
|
#include "BlockDecoder.h"
|
2014-07-08 14:54:27 +02:00
|
|
|
#include "util.h"
|
2014-03-05 21:41:27 +01:00
|
|
|
#include "db-sqlite3.h"
|
2017-01-08 23:24:09 +01:00
|
|
|
#if USE_POSTGRESQL
|
|
|
|
#include "db-postgresql.h"
|
|
|
|
#endif
|
2014-03-05 21:41:27 +01:00
|
|
|
#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
|
|
|
|
2012-08-23 12:55:31 +02:00
|
|
|
using namespace std;
|
|
|
|
|
2018-03-24 15:24:02 +01:00
|
|
|
template<typename T>
|
|
|
|
static inline T mymax(T a, T b)
|
|
|
|
{
|
|
|
|
return (a > b) ? a : b;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
static inline T mymin(T a, T b)
|
|
|
|
{
|
|
|
|
return (a > b) ? b : a;
|
|
|
|
}
|
|
|
|
|
2014-09-02 14:49:56 +02:00
|
|
|
// rounds n (away from 0) to a multiple of f while preserving the sign of n
|
2016-11-18 23:57:09 +01:00
|
|
|
static int round_multiple_nosign(int n, int f)
|
2014-09-01 18:37:07 +02:00
|
|
|
{
|
2014-09-02 14:49:56 +02:00
|
|
|
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
|
2014-09-02 14:49:56 +02:00
|
|
|
return sign * (abs_n + f - (abs_n % f));
|
2014-09-01 18:37:07 +02:00
|
|
|
}
|
|
|
|
|
2018-03-24 15:24:02 +01:00
|
|
|
static inline unsigned int colorSafeBounds (int channel)
|
2012-08-25 13:19:58 +02:00
|
|
|
{
|
2018-03-24 15:24:02 +01:00
|
|
|
return mymin(mymax(channel, 0), 255);
|
2012-08-25 13:19:58 +02:00
|
|
|
}
|
|
|
|
|
2016-11-18 23:57:09 +01:00
|
|
|
static Color mixColors(Color a, Color b)
|
2014-04-03 20:32:48 +02:00
|
|
|
{
|
2016-07-06 21:45:38 +02:00
|
|
|
Color result;
|
|
|
|
double a1 = a.a / 255.0;
|
|
|
|
double a2 = b.a / 255.0;
|
2014-04-03 20:32:48 +02:00
|
|
|
|
2016-07-06 21:45:38 +02:00
|
|
|
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)));
|
2016-11-18 23:57:09 +01:00
|
|
|
|
2016-07-06 21:45:38 +02:00
|
|
|
return result;
|
2014-04-03 20:32:48 +02:00
|
|
|
}
|
|
|
|
|
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),
|
2014-04-03 20:32:48 +02:00
|
|
|
m_drawAlpha(false),
|
2014-03-05 17:19:37 +01:00
|
|
|
m_shading(true),
|
2018-10-31 16:18:29 +01:00
|
|
|
m_dontWriteEmpty(false),
|
2014-07-11 10:54:50 +02:00
|
|
|
m_backend(""),
|
2016-11-18 22:01:15 +01:00
|
|
|
m_xBorder(0),
|
|
|
|
m_yBorder(0),
|
2016-10-13 23:26:59 +02:00
|
|
|
m_db(NULL),
|
|
|
|
m_image(NULL),
|
2014-03-05 17:19:37 +01:00
|
|
|
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),
|
2016-07-06 21:45:38 +02:00
|
|
|
m_geomY2(2048),
|
2016-11-18 22:01:15 +01:00
|
|
|
m_zoom(1),
|
|
|
|
m_scales(SCALE_LEFT | SCALE_TOP)
|
2012-08-23 12:46:22 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TileGenerator::~TileGenerator()
|
|
|
|
{
|
2016-10-13 23:26:59 +02:00
|
|
|
closeDatabase();
|
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);
|
|
|
|
}
|
|
|
|
|
2016-07-06 21:45:38 +02:00
|
|
|
void TileGenerator::setZoom(int zoom)
|
|
|
|
{
|
2016-11-18 23:57:09 +01:00
|
|
|
if (zoom < 1)
|
2016-07-06 21:45:38 +02:00
|
|
|
throw std::runtime_error("Zoom level needs to be a number: 1 or higher");
|
|
|
|
m_zoom = zoom;
|
|
|
|
}
|
|
|
|
|
2016-11-18 22:01:15 +01:00
|
|
|
void TileGenerator::setScales(uint flags)
|
|
|
|
{
|
|
|
|
m_scales = flags;
|
|
|
|
}
|
|
|
|
|
2012-08-23 14:43:11 +02:00
|
|
|
Color TileGenerator::parseColor(const std::string &color)
|
|
|
|
{
|
|
|
|
Color parsed;
|
2016-11-18 23:57:09 +01:00
|
|
|
if (color.length() != 7)
|
|
|
|
throw std::runtime_error("Color needs to be 7 characters long");
|
|
|
|
if (color[0] != '#')
|
|
|
|
throw std::runtime_error("Color needs to begin with #");
|
|
|
|
unsigned long col = strtoul(color.c_str() + 1, NULL, 16);
|
|
|
|
parsed.b = col & 0xff;
|
|
|
|
parsed.g = (col >> 8) & 0xff;
|
|
|
|
parsed.r = (col >> 16) & 0xff;
|
|
|
|
parsed.a = 255;
|
2012-08-23 14:43:11 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-04-03 20:32:48 +02:00
|
|
|
void TileGenerator::setDrawAlpha(bool drawAlpha)
|
|
|
|
{
|
2016-07-06 21:45:38 +02:00
|
|
|
m_drawAlpha = drawAlpha;
|
2014-04-03 20:32:48 +02:00
|
|
|
}
|
|
|
|
|
2014-03-05 17:19:37 +01:00
|
|
|
void TileGenerator::setShading(bool shading)
|
|
|
|
{
|
|
|
|
m_shading = shading;
|
|
|
|
}
|
|
|
|
|
2014-07-11 10:54:50 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-08-23 12:55:31 +02:00
|
|
|
void TileGenerator::parseColorsFile(const std::string &fileName)
|
|
|
|
{
|
|
|
|
ifstream in;
|
|
|
|
in.open(fileName.c_str(), ifstream::in);
|
2016-08-09 16:45:39 +02:00
|
|
|
if (!in.is_open())
|
2016-11-18 23:57:09 +01:00
|
|
|
throw std::runtime_error("Specified colors file could not be found");
|
2012-09-01 15:51:02 +02:00
|
|
|
parseColorsStream(in);
|
2012-08-23 12:55:31 +02:00
|
|
|
}
|
|
|
|
|
2018-10-20 17:40:01 +02:00
|
|
|
void TileGenerator::printGeometry(const std::string &input)
|
|
|
|
{
|
|
|
|
string input_path = input;
|
|
|
|
if (input_path[input.length() - 1] != PATH_SEPARATOR) {
|
|
|
|
input_path += PATH_SEPARATOR;
|
|
|
|
}
|
|
|
|
|
|
|
|
openDb(input_path);
|
|
|
|
loadBlocks();
|
|
|
|
|
|
|
|
std::cout << "Map extent: "
|
|
|
|
<< m_xMin*16 << ":" << m_zMin*16
|
|
|
|
<< "+" << (m_xMax - m_xMin+1)*16
|
|
|
|
<< "+" << (m_zMax - m_zMin+1)*16
|
|
|
|
<< std::endl;
|
|
|
|
|
|
|
|
closeDatabase();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-10-31 16:18:29 +01:00
|
|
|
void TileGenerator::setDontWriteEmpty(bool f)
|
|
|
|
{
|
|
|
|
m_dontWriteEmpty = f;
|
|
|
|
}
|
2018-10-20 17:40:01 +02:00
|
|
|
|
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();
|
2018-10-31 16:18:29 +01:00
|
|
|
|
|
|
|
if (m_dontWriteEmpty && ! m_positions.size())
|
|
|
|
{
|
|
|
|
closeDatabase();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-23 14:21:34 +02:00
|
|
|
createImage();
|
2012-08-23 15:35:00 +02:00
|
|
|
renderMap();
|
2016-10-13 23:26:59 +02:00
|
|
|
closeDatabase();
|
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)
|
|
|
|
{
|
2016-10-08 21:58:56 +02:00
|
|
|
char line[128];
|
2012-09-01 15:51:02 +02:00
|
|
|
while (in.good()) {
|
2014-05-21 15:16:29 +02:00
|
|
|
in.getline(line, 128);
|
2016-10-08 21:58:56 +02:00
|
|
|
|
|
|
|
for(char *p = line; *p; p++) {
|
2016-10-08 13:39:07 +02:00
|
|
|
if(*p != '#')
|
|
|
|
continue;
|
|
|
|
*p = '\0'; // Cut off at the first #
|
|
|
|
break;
|
|
|
|
}
|
2016-10-08 21:58:56 +02:00
|
|
|
if(strlen(line) == 0)
|
|
|
|
continue;
|
2016-10-08 13:39:07 +02:00
|
|
|
|
2018-10-20 23:01:03 +02:00
|
|
|
char name[64 + 1];
|
2014-12-20 13:08:51 +01:00
|
|
|
unsigned int r, g, b, a, t;
|
2014-05-21 15:16:29 +02:00
|
|
|
a = 255;
|
|
|
|
t = 0;
|
2016-10-08 21:58:56 +02:00
|
|
|
int items = sscanf(line, "%64s %u %u %u %u %u", name, &r, &g, &b, &a, &t);
|
|
|
|
if(items < 4) {
|
2016-10-09 15:19:35 +02:00
|
|
|
std::cerr << "Failed to parse color entry '" << line << "'" << std::endl;
|
2016-10-08 21:58:56 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-05-21 15:16:29 +02:00
|
|
|
ColorEntry color = ColorEntry(r, g, b, a, t);
|
2015-03-18 22:49:19 +01:00
|
|
|
m_colorMap[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)
|
|
|
|
{
|
2014-07-11 10:54:50 +02:00
|
|
|
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");
|
2018-03-25 14:32:11 +02:00
|
|
|
backend = read_setting("backend", ifs);
|
2014-07-11 10:54:50 +02:00
|
|
|
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);
|
2017-01-08 23:24:09 +01:00
|
|
|
#if USE_POSTGRESQL
|
|
|
|
else if(backend == "postgresql")
|
|
|
|
m_db = new DBPostgreSQL(input);
|
|
|
|
#endif
|
2014-03-05 21:41:27 +01:00
|
|
|
#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
|
|
|
}
|
|
|
|
|
2016-10-13 23:26:59 +02:00
|
|
|
void TileGenerator::closeDatabase()
|
|
|
|
{
|
|
|
|
delete m_db;
|
|
|
|
m_db = NULL;
|
|
|
|
}
|
|
|
|
|
2012-08-23 13:32:22 +02:00
|
|
|
void TileGenerator::loadBlocks()
|
|
|
|
{
|
2014-04-19 08:13:34 +02:00
|
|
|
std::vector<BlockPos> vec = m_db->getBlockPos();
|
|
|
|
for (std::vector<BlockPos>::iterator it = vec.begin(); it != vec.end(); ++it) {
|
|
|
|
BlockPos pos = *it;
|
2014-03-28 21:47:19 +01:00
|
|
|
// Check that it's in geometry (from --geometry option)
|
2016-11-18 23:57:09 +01: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;
|
2016-11-18 23:57:09 +01:00
|
|
|
// Check that it's between --min-y and --max-y
|
|
|
|
if (pos.y * 16 < m_yMin || pos.y * 16 > m_yMax)
|
2014-03-05 21:41:27 +01:00
|
|
|
continue;
|
2014-03-28 21:47:19 +01:00
|
|
|
// Adjust minimum and maximum positions to the nearest block
|
2016-11-18 23:57:09 +01:00
|
|
|
if (pos.x < m_xMin)
|
2014-03-05 21:41:27 +01:00
|
|
|
m_xMin = pos.x;
|
2016-11-18 23:57:09 +01:00
|
|
|
if (pos.x > m_xMax)
|
2014-03-05 21:41:27 +01:00
|
|
|
m_xMax = pos.x;
|
2016-11-18 23:57:09 +01:00
|
|
|
|
|
|
|
if (pos.z < m_zMin)
|
2014-03-05 21:41:27 +01:00
|
|
|
m_zMin = pos.z;
|
2016-11-18 23:57:09 +01:00
|
|
|
if (pos.z > m_zMax)
|
2014-03-05 21:41:27 +01:00
|
|
|
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()
|
|
|
|
{
|
2018-03-24 15:24:02 +01:00
|
|
|
const int scale_d = 40; // pixels reserved for a scale
|
|
|
|
if(!m_drawScale)
|
|
|
|
m_scales = 0;
|
|
|
|
|
2018-10-12 20:49:54 +02:00
|
|
|
|
|
|
|
// If a geometry is explicitly set, set the bounding box to the requested geometry
|
|
|
|
// instead of cropping to the content. This way we will always output a full tile
|
|
|
|
// of the correct size.
|
|
|
|
if (m_geomX > -2048 && m_geomX2 < 2048)
|
|
|
|
{
|
|
|
|
m_xMin = m_geomX;
|
|
|
|
m_xMax = m_geomX2-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_geomY > -2048 && m_geomY2 < 2048)
|
|
|
|
{
|
|
|
|
m_zMin = m_geomY;
|
|
|
|
m_zMax = m_geomY2-1;
|
|
|
|
}
|
|
|
|
|
2012-08-25 13:19:58 +02:00
|
|
|
m_mapWidth = (m_xMax - m_xMin + 1) * 16;
|
|
|
|
m_mapHeight = (m_zMax - m_zMin + 1) * 16;
|
2018-10-12 20:49:54 +02:00
|
|
|
|
2018-03-24 15:24:02 +01:00
|
|
|
m_xBorder = (m_scales & SCALE_LEFT) ? scale_d : 0;
|
|
|
|
m_yBorder = (m_scales & SCALE_TOP) ? scale_d : 0;
|
2016-11-18 23:57:09 +01:00
|
|
|
m_blockPixelAttributes.setWidth(m_mapWidth);
|
2016-11-18 22:01:15 +01:00
|
|
|
|
2016-08-09 16:04:58 +02:00
|
|
|
int image_width, image_height;
|
2016-11-18 22:01:15 +01:00
|
|
|
image_width = (m_mapWidth * m_zoom) + m_xBorder;
|
2018-03-24 15:24:02 +01:00
|
|
|
image_width += (m_scales & SCALE_RIGHT) ? scale_d : 0;
|
2016-11-18 22:01:15 +01:00
|
|
|
image_height = (m_mapHeight * m_zoom) + m_yBorder;
|
2018-03-24 15:24:02 +01:00
|
|
|
image_height += (m_scales & SCALE_BOTTOM) ? scale_d : 0;
|
2016-11-18 23:57:09 +01:00
|
|
|
|
2016-08-09 16:04:58 +02:00
|
|
|
if(image_width > 4096 || image_height > 4096)
|
|
|
|
std::cerr << "Warning: The width or height of the image to be created exceeds 4096 pixels!"
|
|
|
|
<< " (Dimensions: " << image_width << "x" << image_height << ")"
|
|
|
|
<< std::endl;
|
2016-11-18 23:34:31 +01:00
|
|
|
m_image = new Image(image_width, image_height);
|
|
|
|
m_image->drawFilledRect(0, 0, image_width, image_height, m_bgColor); // Background
|
2012-08-23 14:21:34 +02:00
|
|
|
}
|
|
|
|
|
2014-03-05 21:41:27 +01:00
|
|
|
void TileGenerator::renderMap()
|
|
|
|
{
|
2018-03-25 15:19:48 +02:00
|
|
|
BlockDecoder blk;
|
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;
|
2014-04-19 08:13:34 +02:00
|
|
|
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) {
|
2016-11-18 23:57:09 +01:00
|
|
|
if (position->second != zPos)
|
2012-08-24 11:01:48 +02:00
|
|
|
continue;
|
|
|
|
|
2018-03-25 15:44:30 +02:00
|
|
|
m_readPixels.reset();
|
|
|
|
m_readInfo.reset();
|
2014-07-08 14:58:40 +02:00
|
|
|
for (int i = 0; i < 16; i++) {
|
|
|
|
for (int j = 0; j < 16; j++) {
|
2015-03-18 22:49:19 +01:00
|
|
|
m_color[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_color[i][j].a = 0; // ..but set alpha to 0 to tell renderMapBlock() not to use this color to mix a shade
|
|
|
|
m_thickness[i][j] = 0;
|
2014-07-08 14:58:40 +02:00
|
|
|
}
|
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-08-24 14:13:46 +02:00
|
|
|
|
2018-03-25 15:19:48 +02:00
|
|
|
blk.reset();
|
|
|
|
blk.decode(it->second);
|
|
|
|
if (blk.isEmpty())
|
|
|
|
continue;
|
|
|
|
renderMapBlock(blk, pos);
|
2012-08-24 22:51:17 +02:00
|
|
|
|
2018-03-25 15:44:30 +02:00
|
|
|
// Exit out if all pixels for this MapBlock are covered
|
|
|
|
if (m_readPixels.full())
|
2012-08-24 22:51:17 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-03-25 15:44:30 +02:00
|
|
|
if (!m_readPixels.full())
|
2014-07-08 14:58:40 +02:00
|
|
|
renderMapBlockBottom(blockStack.begin()->first);
|
2012-08-24 22:51:17 +02:00
|
|
|
}
|
2018-03-25 15:44:30 +02:00
|
|
|
if (m_shading)
|
2014-03-05 17:19:37 +01:00
|
|
|
renderShading(zPos);
|
2012-08-24 22:51:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-25 15:19:48 +02:00
|
|
|
void TileGenerator::renderMapBlock(const BlockDecoder &blk, const BlockPos &pos)
|
2012-08-24 22:51:17 +02:00
|
|
|
{
|
|
|
|
int xBegin = (pos.x - m_xMin) * 16;
|
|
|
|
int zBegin = (m_zMax - pos.z) * 16;
|
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) {
|
2016-07-06 21:45:38 +02:00
|
|
|
int imageY = zBegin + 15 - z;
|
2012-08-24 22:51:17 +02:00
|
|
|
for (int x = 0; x < 16; ++x) {
|
2018-03-25 15:44:30 +02:00
|
|
|
if (m_readPixels.get(x, z))
|
2012-08-24 22:51:17 +02:00
|
|
|
continue;
|
2016-07-06 21:45:38 +02:00
|
|
|
int imageX = 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) {
|
2018-03-25 15:19:48 +02:00
|
|
|
string name = blk.getNode(x, y, z);
|
|
|
|
if (name == "")
|
2012-08-24 22:51:17 +02:00
|
|
|
continue;
|
2018-03-25 15:44:30 +02:00
|
|
|
ColorMap::const_iterator it = m_colorMap.find(name);
|
|
|
|
if (it == m_colorMap.end()) {
|
2014-04-03 20:32:48 +02:00
|
|
|
m_unknownNodes.insert(name);
|
|
|
|
continue;
|
2012-08-24 22:51:17 +02:00
|
|
|
}
|
2018-03-25 15:44:30 +02:00
|
|
|
const Color c = it->second.to_color();
|
|
|
|
if (m_drawAlpha) {
|
|
|
|
if (m_color[z][x].a == 0)
|
|
|
|
m_color[z][x] = c; // first visible time, no color mixing
|
|
|
|
else
|
|
|
|
m_color[z][x] = mixColors(m_color[z][x], c);
|
|
|
|
if(m_color[z][x].a < 0xff) {
|
|
|
|
// near thickness value to thickness of current node
|
|
|
|
m_thickness[z][x] = (m_thickness[z][x] + it->second.t) / 2.0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// color became opaque, draw it
|
|
|
|
setZoomed(imageX, imageY, m_color[z][x]);
|
|
|
|
m_blockPixelAttributes.attribute(15 - z, xBegin + x).thickness = m_thickness[z][x];
|
|
|
|
} else {
|
|
|
|
setZoomed(imageX, imageY, c.noAlpha());
|
|
|
|
}
|
|
|
|
m_readPixels.set(x, z);
|
|
|
|
|
2018-03-25 16:22:51 +02:00
|
|
|
// do this afterwards so we can record height values
|
2018-03-25 15:44:30 +02:00
|
|
|
// inside transparent nodes (water) too
|
2018-03-25 16:22:51 +02:00
|
|
|
if (!m_readInfo.get(x, z)) {
|
2018-03-25 15:44:30 +02:00
|
|
|
m_blockPixelAttributes.attribute(15 - z, xBegin + x).height = pos.y * 16 + y;
|
|
|
|
m_readInfo.set(x, z);
|
|
|
|
}
|
2014-04-03 20:32:48 +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
|
|
|
|
2016-11-18 23:57:09 +01:00
|
|
|
void TileGenerator::renderMapBlockBottom(const BlockPos &pos)
|
2014-07-08 14:58:40 +02:00
|
|
|
{
|
2018-03-25 15:44:30 +02:00
|
|
|
if (!m_drawAlpha)
|
|
|
|
return; // "missing" pixels can only happen with --drawalpha
|
|
|
|
|
2014-07-08 14:58:40 +02:00
|
|
|
int xBegin = (pos.x - m_xMin) * 16;
|
|
|
|
int zBegin = (m_zMax - pos.z) * 16;
|
|
|
|
for (int z = 0; z < 16; ++z) {
|
2016-07-06 21:45:38 +02:00
|
|
|
int imageY = zBegin + 15 - z;
|
2014-07-08 14:58:40 +02:00
|
|
|
for (int x = 0; x < 16; ++x) {
|
2018-03-25 15:44:30 +02:00
|
|
|
if (m_readPixels.get(x, z))
|
2014-07-08 14:58:40 +02:00
|
|
|
continue;
|
2016-07-06 21:45:38 +02:00
|
|
|
int imageX = xBegin + x;
|
2014-07-08 14:58:40 +02:00
|
|
|
|
2018-03-25 15:44:30 +02:00
|
|
|
// set color since it wasn't done in renderMapBlock()
|
|
|
|
setZoomed(imageX, imageY, m_color[z][x]);
|
|
|
|
m_readPixels.set(x, z);
|
|
|
|
m_blockPixelAttributes.attribute(15 - z, xBegin + x).thickness = m_thickness[z][x];
|
2014-07-08 14:58:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-18 23:57:09 +01:00
|
|
|
void TileGenerator::renderShading(int zPos)
|
2012-08-25 13:19:58 +02:00
|
|
|
{
|
|
|
|
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;
|
2016-11-18 23:57:09 +01:00
|
|
|
if (imageY >= m_mapHeight)
|
2012-08-25 13:19:58 +02:00
|
|
|
continue;
|
|
|
|
for (int x = 0; x < m_mapWidth; ++x) {
|
2016-11-18 23:57:09 +01: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()
|
|
|
|
)
|
2012-08-25 13:27:40 +02:00
|
|
|
continue;
|
2016-11-18 23:57:09 +01:00
|
|
|
|
|
|
|
// calculate shadow to apply
|
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;
|
2018-03-24 15:24:02 +01:00
|
|
|
if (m_drawAlpha) { // less visible shadow with increasing "thickness"
|
|
|
|
double t = m_blockPixelAttributes.attribute(z, x).thickness * 1.2;
|
|
|
|
d *= 1.0 - mymin(t, 255.0) / 255.0;
|
|
|
|
}
|
|
|
|
d = mymin(d, 36);
|
2016-11-18 23:57:09 +01:00
|
|
|
|
2016-11-18 23:34:31 +01:00
|
|
|
Color c = m_image->getPixel(getImageX(x), getImageY(imageY));
|
|
|
|
c.r = colorSafeBounds(c.r + d);
|
|
|
|
c.g = colorSafeBounds(c.g + d);
|
|
|
|
c.b = colorSafeBounds(c.b + d);
|
|
|
|
setZoomed(x, imageY, c);
|
2012-08-25 13:19:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
m_blockPixelAttributes.scroll();
|
|
|
|
}
|
|
|
|
|
2012-08-25 14:11:55 +02:00
|
|
|
void TileGenerator::renderScale()
|
|
|
|
{
|
2018-03-25 16:22:51 +02:00
|
|
|
const int scale_d = 40; // see createImage()
|
|
|
|
|
2016-11-18 22:01:15 +01:00
|
|
|
if (m_scales & SCALE_TOP) {
|
2016-11-18 23:34:31 +01:00
|
|
|
m_image->drawText(24, 0, "X", m_scaleColor);
|
2016-11-18 22:01:15 +01:00
|
|
|
for (int i = (m_xMin / 4) * 4; i <= m_xMax; i += 4) {
|
2018-10-20 23:01:03 +02:00
|
|
|
std::ostringstream buf;
|
2016-11-18 22:01:15 +01:00
|
|
|
buf << i * 16;
|
2012-08-25 15:06:11 +02:00
|
|
|
|
2018-03-24 14:54:45 +01:00
|
|
|
int xPos = getImageX(i * 16, true);
|
2018-03-24 13:57:27 +01:00
|
|
|
if (xPos >= 0) {
|
|
|
|
m_image->drawText(xPos + 2, 0, buf.str(), m_scaleColor);
|
|
|
|
m_image->drawLine(xPos, 0, xPos, m_yBorder - 1, m_scaleColor);
|
|
|
|
}
|
2016-11-18 22:01:15 +01:00
|
|
|
}
|
2012-08-25 15:06:11 +02:00
|
|
|
}
|
|
|
|
|
2016-11-18 22:01:15 +01:00
|
|
|
if (m_scales & SCALE_LEFT) {
|
2016-11-18 23:34:31 +01:00
|
|
|
m_image->drawText(2, 24, "Z", m_scaleColor);
|
2016-11-18 22:01:15 +01:00
|
|
|
for (int i = (m_zMax / 4) * 4; i >= m_zMin; i -= 4) {
|
2018-10-20 23:01:03 +02:00
|
|
|
std::ostringstream buf;
|
2016-11-18 22:01:15 +01:00
|
|
|
buf << i * 16;
|
2012-08-25 15:06:11 +02:00
|
|
|
|
2018-03-24 14:54:45 +01:00
|
|
|
int yPos = getImageY(i * 16 + 1, true);
|
2018-03-24 13:57:27 +01:00
|
|
|
if (yPos >= 0) {
|
|
|
|
m_image->drawText(2, yPos, buf.str(), m_scaleColor);
|
|
|
|
m_image->drawLine(0, yPos, m_xBorder - 1, yPos, m_scaleColor);
|
|
|
|
}
|
2016-11-18 22:01:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_scales & SCALE_BOTTOM) {
|
2018-03-25 16:22:51 +02:00
|
|
|
int xPos = m_xBorder + m_mapWidth*m_zoom - 24 - 8,
|
|
|
|
yPos = m_yBorder + m_mapHeight*m_zoom + scale_d - 12;
|
|
|
|
m_image->drawText(xPos, yPos, "X", m_scaleColor);
|
2016-11-18 22:01:15 +01:00
|
|
|
for (int i = (m_xMin / 4) * 4; i <= m_xMax; i += 4) {
|
2018-10-20 23:01:03 +02:00
|
|
|
std::ostringstream buf;
|
2016-11-18 22:01:15 +01:00
|
|
|
buf << i * 16;
|
|
|
|
|
2018-03-25 16:22:51 +02:00
|
|
|
xPos = getImageX(i * 16, true);
|
|
|
|
yPos = m_yBorder + m_mapHeight*m_zoom;
|
2018-03-24 13:57:27 +01:00
|
|
|
if (xPos >= 0) {
|
|
|
|
m_image->drawText(xPos + 2, yPos, buf.str(), m_scaleColor);
|
|
|
|
m_image->drawLine(xPos, yPos, xPos, yPos + 39, m_scaleColor);
|
|
|
|
}
|
2016-11-18 22:01:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_scales & SCALE_RIGHT) {
|
2018-03-25 16:22:51 +02:00
|
|
|
int xPos = m_xBorder + m_mapWidth*m_zoom + scale_d - 2 - 8,
|
|
|
|
yPos = m_yBorder + m_mapHeight*m_zoom - 24 - 12;
|
|
|
|
m_image->drawText(xPos, yPos, "Z", m_scaleColor);
|
2016-11-18 22:01:15 +01:00
|
|
|
for (int i = (m_zMax / 4) * 4; i >= m_zMin; i -= 4) {
|
2018-10-20 23:01:03 +02:00
|
|
|
std::ostringstream buf;
|
2016-11-18 22:01:15 +01:00
|
|
|
buf << i * 16;
|
|
|
|
|
2018-03-25 16:22:51 +02:00
|
|
|
xPos = m_xBorder + m_mapWidth*m_zoom;
|
|
|
|
yPos = getImageY(i * 16 + 1, true);
|
2018-03-24 13:57:27 +01:00
|
|
|
if (yPos >= 0) {
|
|
|
|
m_image->drawText(xPos + 2, yPos, buf.str(), m_scaleColor);
|
|
|
|
m_image->drawLine(xPos, yPos, xPos + 39, yPos, m_scaleColor);
|
|
|
|
}
|
2016-11-18 22:01:15 +01:00
|
|
|
}
|
2012-08-25 15:06:11 +02:00
|
|
|
}
|
2012-08-25 14:11:55 +02:00
|
|
|
}
|
|
|
|
|
2012-08-25 15:21:51 +02:00
|
|
|
void TileGenerator::renderOrigin()
|
|
|
|
{
|
2018-03-24 14:54:45 +01:00
|
|
|
if (m_xMin > 0 || m_xMax < 0 ||
|
|
|
|
m_zMin > 0 || m_zMax < 0)
|
|
|
|
return;
|
|
|
|
m_image->drawCircle(getImageX(0, true), getImageY(0, true), 12, 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)
|
|
|
|
{
|
2012-09-01 16:40:18 +02:00
|
|
|
PlayerAttributes players(inputPath);
|
|
|
|
for (PlayerAttributes::Players::iterator player = players.begin(); player != players.end(); ++player) {
|
2018-03-24 14:54:45 +01:00
|
|
|
if (player->x < m_xMin * 16 || player->x > m_xMax * 16 ||
|
|
|
|
player->z < m_zMin * 16 || player->z > m_zMax * 16)
|
|
|
|
continue;
|
2018-03-25 16:22:51 +02:00
|
|
|
if (player->y < m_yMin || player->y > m_yMax)
|
|
|
|
continue;
|
2018-03-24 14:54:45 +01:00
|
|
|
int imageX = getImageX(player->x, true),
|
|
|
|
imageY = getImageY(player->z, true);
|
2012-09-01 13:01:31 +02:00
|
|
|
|
2018-03-25 16:22:51 +02:00
|
|
|
m_image->drawFilledRect(imageX - 1, imageY, 3, 1, m_playerColor);
|
|
|
|
m_image->drawFilledRect(imageX, imageY - 1, 1, 3, m_playerColor);
|
|
|
|
m_image->drawText(imageX + 2, imageY, player->name, m_playerColor);
|
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;
|
2016-11-18 23:57:09 +01:00
|
|
|
for (std::list<std::pair<int, int> >::const_iterator position = m_positions.begin(); position != m_positions.end(); ++position)
|
2012-08-24 09:46:14 +02:00
|
|
|
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)
|
|
|
|
{
|
2016-11-18 23:34:31 +01:00
|
|
|
m_image->save(output);
|
|
|
|
delete m_image;
|
|
|
|
m_image = NULL;
|
2012-08-23 14:21:34 +02:00
|
|
|
}
|
|
|
|
|
2012-08-25 16:41:53 +02:00
|
|
|
void TileGenerator::printUnknown()
|
|
|
|
{
|
2018-03-25 16:22:51 +02:00
|
|
|
if (m_unknownNodes.size() == 0)
|
|
|
|
return;
|
|
|
|
std::cerr << "Unknown nodes:" << std::endl;
|
|
|
|
for (NameSet::iterator node = m_unknownNodes.begin(); node != m_unknownNodes.end(); ++node)
|
|
|
|
std::cerr << "\t" << *node << std::endl;
|
2012-08-25 16:41:53 +02:00
|
|
|
}
|
|
|
|
|
2018-03-24 14:54:45 +01:00
|
|
|
inline int TileGenerator::getImageX(int val, bool absolute) const
|
2012-08-25 14:11:55 +02:00
|
|
|
{
|
2018-03-24 14:54:45 +01:00
|
|
|
if (absolute)
|
|
|
|
val = (val - m_xMin * 16);
|
2016-11-18 22:01:15 +01:00
|
|
|
return (m_zoom*val) + m_xBorder;
|
2012-08-25 14:11:55 +02:00
|
|
|
}
|
|
|
|
|
2018-03-24 14:54:45 +01:00
|
|
|
inline int TileGenerator::getImageY(int val, bool absolute) const
|
2012-08-25 14:11:55 +02:00
|
|
|
{
|
2018-03-24 14:54:45 +01:00
|
|
|
if (absolute)
|
|
|
|
val = m_mapHeight - (val - m_zMin * 16); // Z axis is flipped on image
|
2016-11-18 22:01:15 +01:00
|
|
|
return (m_zoom*val) + m_yBorder;
|
2016-07-06 21:45:38 +02:00
|
|
|
}
|
|
|
|
|
2016-11-18 23:57:09 +01:00
|
|
|
inline void TileGenerator::setZoomed(int x, int y, Color color)
|
|
|
|
{
|
2016-11-18 23:34:31 +01:00
|
|
|
m_image->drawFilledRect(getImageX(x), getImageY(y), m_zoom, m_zoom, color);
|
2012-08-25 14:11:55 +02:00
|
|
|
}
|