File: mapper.cpp

package info (click to toggle)
minetestmapper 20180325-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 236 kB
  • sloc: cpp: 2,101; ansic: 32; makefile: 9; sh: 9
file content (215 lines) | stat: -rw-r--r-- 4,858 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <cstdlib>
#include <cstring>
#include <getopt.h>
#include <fstream>
#include <iostream>
#include <map>
#include <string>
#include <sstream>
#include <stdexcept>
#include "cmake_config.h"
#include "TileGenerator.h"

void usage()
{
	const char *usage_text = "minetestmapper [options]\n"
			"  -i/--input <world_path>\n"
			"  -o/--output <output_image.png>\n"
			"  --bgcolor <color>\n"
			"  --scalecolor <color>\n"
			"  --playercolor <color>\n"
			"  --origincolor <color>\n"
			"  --drawscale\n"
			"  --drawplayers\n"
			"  --draworigin\n"
			"  --drawalpha\n"
			"  --noshading\n"
			"  --min-y <y>\n"
			"  --max-y <y>\n"
			"  --backend <backend>\n"
			"  --geometry x:y+w+h\n"
			"  --zoom <zoomlevel>\n"
			"  --colors <colors.txt>\n"
			"  --scales [t][b][l][r]\n"
			"Color format: '#000000'\n";
	std::cout << usage_text;
}

bool file_exists(const std::string &path)
{
	std::ifstream ifs(path.c_str());
	return ifs.is_open();
}

std::string search_colors(const std::string &worldpath)
{
	if(file_exists(worldpath + "/colors.txt"))
		return worldpath + "/colors.txt";

#ifndef _WIN32
	char *home = std::getenv("HOME");
	if(home) {
		std::string check = ((std::string) home) + "/.minetest/colors.txt";
		if(file_exists(check))
			return check;
	}
#endif

	if(!(SHAREDIR[0] == '.' || SHAREDIR[0] == '\0') && file_exists(SHAREDIR "/colors.txt"))
		return SHAREDIR "/colors.txt";

	std::cerr << "Warning: Falling back to using colors.txt from current directory." << std::endl;
	return "colors.txt";
}

int main(int argc, char *argv[])
{
	static struct option long_options[] =
	{
		{"help", no_argument, 0, 'h'},
		{"input", required_argument, 0, 'i'},
		{"output", required_argument, 0, 'o'},
		{"bgcolor", required_argument, 0, 'b'},
		{"scalecolor", required_argument, 0, 's'},
		{"origincolor", required_argument, 0, 'r'},
		{"playercolor", required_argument, 0, 'p'},
		{"draworigin", no_argument, 0, 'R'},
		{"drawplayers", no_argument, 0, 'P'},
		{"drawscale", no_argument, 0, 'S'},
		{"drawalpha", no_argument, 0, 'e'},
		{"noshading", no_argument, 0, 'H'},
		{"backend", required_argument, 0, 'd'},
		{"geometry", required_argument, 0, 'g'},
		{"min-y", required_argument, 0, 'a'},
		{"max-y", required_argument, 0, 'c'},
		{"zoom", required_argument, 0, 'z'},
		{"colors", required_argument, 0, 'C'},
		{"scales", required_argument, 0, 'f'},
		{0, 0, 0, 0}
	};

	std::string input;
	std::string output;
	std::string colors = "";

	TileGenerator generator;
	int option_index = 0;
	int c = 0;
	while (1) {
		c = getopt_long(argc, argv, "hi:o:", long_options, &option_index);
		if (c == -1) {
			if (input.empty() || output.empty()) {
				usage();
				return 0;
			}
			break;
		}
		switch (c) {
			case 'h':
				usage();
				return 0;
				break;
			case 'i':
				input = optarg;
				break;
			case 'o':
				output = optarg;
				break;
			case 'b':
				generator.setBgColor(optarg);
				break;
			case 's':
				generator.setScaleColor(optarg);
				break;
			case 'r':
				generator.setOriginColor(optarg);
				break;
			case 'p':
				generator.setPlayerColor(optarg);
				break;
			case 'R':
				generator.setDrawOrigin(true);
				break;
			case 'P':
				generator.setDrawPlayers(true);
				break;
			case 'S':
				generator.setDrawScale(true);
				break;
			case 'e':
				generator.setDrawAlpha(true);
				break;
			case 'H':
				generator.setShading(false);
				break;
			case 'd':
				generator.setBackend(optarg);
				break;
			case 'a': {
					std::istringstream iss;
					iss.str(optarg);
					int miny;
					iss >> miny;
					generator.setMinY(miny);
				}
				break;
			case 'c': {
					std::istringstream iss;
					iss.str(optarg);
					int maxy;
					iss >> maxy;
					generator.setMaxY(maxy);
				}
				break;
			case 'g': {
					std::istringstream geometry;
					geometry.str(optarg);
					int x, y, w, h;
					char c;
					geometry >> x >> c >> y >> w >> h;
					if (geometry.fail() || c != ':' || w < 1 || h < 1) {
						usage();
						exit(1);
					}
					generator.setGeometry(x, y, w, h);
				}
				break;
			case 'f': {
					uint flags = 0;
					if(strchr(optarg, 't') != NULL)
						flags |= SCALE_TOP;
					if(strchr(optarg, 'b') != NULL)
						flags |= SCALE_BOTTOM;
					if(strchr(optarg, 'l') != NULL)
						flags |= SCALE_LEFT;
					if(strchr(optarg, 'r') != NULL)
						flags |= SCALE_RIGHT;
					generator.setScales(flags);
				}
				break;
			case 'z': {
					std::istringstream iss;
					iss.str(optarg);
					int zoom;
					iss >> zoom;
					generator.setZoom(zoom);
				}
				break;
			case 'C':
				colors = optarg;
				break;
			default:
				exit(1);
		}
	}
	if(colors == "")
		colors = search_colors(input);
	try {
		generator.parseColorsFile(colors);
		generator.generate(input, output);
	} catch(std::runtime_error e) {
		std::cerr << "Exception: " << e.what() << std::endl;
		return 1;
	}
	return 0;
}