git repos / blockattack-game

blame: source/code/common.cpp

normal view · raw

89c4a3a6 sago007 2008-08-29 14:32 1
/*
c53e6443 sago007 2012-04-17 11:04 2
===========================================================================
c53e6443 sago007 2012-04-17 11:04 3
blockattack - Block Attack - Rise of the Blocks
c53e6443 sago007 2012-04-17 11:04 4
Copyright (C) 2005-2012 Poul Sander
c53e6443 sago007 2012-04-17 11:04 5
c53e6443 sago007 2012-04-17 11:04 6
This program is free software: you can redistribute it and/or modify
c53e6443 sago007 2012-04-17 11:04 7
it under the terms of the GNU General Public License as published by
c53e6443 sago007 2012-04-17 11:04 8
the Free Software Foundation, either version 2 of the License, or
c53e6443 sago007 2012-04-17 11:04 9
(at your option) any later version.
c53e6443 sago007 2012-04-17 11:04 10
c53e6443 sago007 2012-04-17 11:04 11
This program is distributed in the hope that it will be useful,
c53e6443 sago007 2012-04-17 11:04 12
but WITHOUT ANY WARRANTY; without even the implied warranty of
c53e6443 sago007 2012-04-17 11:04 13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
c53e6443 sago007 2012-04-17 11:04 14
GNU General Public License for more details.
c53e6443 sago007 2012-04-17 11:04 15
c53e6443 sago007 2012-04-17 11:04 16
You should have received a copy of the GNU General Public License
c53e6443 sago007 2012-04-17 11:04 17
along with this program.  If not, see http://www.gnu.org/licenses/
c53e6443 sago007 2012-04-17 11:04 18
c53e6443 sago007 2012-04-17 11:04 19
Source information and contacts persons can be found at
30412ca6 sago007 2016-03-25 19:35 20
http://www.blockattack.net
c53e6443 sago007 2012-04-17 11:04 21
===========================================================================
89c4a3a6 sago007 2008-08-29 14:32 22
*/
89c4a3a6 sago007 2008-08-29 14:32 23
89c4a3a6 sago007 2008-08-29 14:32 24
#include "common.h"
769a41a0 sago007 2008-09-24 12:54 25
#include <sstream>
f306e3ca sago007 2015-12-04 21:16 26
#include <cstring>
1899da11 sago007 2015-12-30 19:04 27
#include "os.hpp"
4e257de2 sago007 2016-01-22 17:43 28
#include "sago/SagoMiscSdl2.hpp"
59dcb571 sago007 2016-02-14 15:34 29
#include "sago/SagoMisc.hpp"
15b9c93b sago007 2016-02-03 20:03 30
#include <stdarg.h>
769a41a0 sago007 2008-09-24 12:54 31
96eb7a16 sago007 2016-04-29 17:01 32
using std::string;
96eb7a16 sago007 2016-04-29 17:01 33
using std::stringstream;
96eb7a16 sago007 2016-04-29 17:01 34
using std::ios_base;
96eb7a16 sago007 2016-04-29 17:01 35
using std::cerr;
96eb7a16 sago007 2016-04-29 17:01 36
using std::endl;
96eb7a16 sago007 2016-04-29 17:01 37
using std::map;
96eb7a16 sago007 2016-04-29 17:01 38
using std::vector;
53001fa2 sago007 2015-11-08 15:13 39
using boost::format;
53001fa2 sago007 2015-11-08 15:13 40
444cec07 sago007 2012-05-05 16:42 41
static stringstream converter;
444cec07 sago007 2012-05-05 16:42 42
769a41a0 sago007 2008-09-24 12:54 43
//Function to convert numbers to string
ecef5838 sago007 2015-11-24 20:01 44
string itoa(int num) {
444cec07 sago007 2012-05-05 16:42 45
	converter.str(std::string());
444cec07 sago007 2012-05-05 16:42 46
	converter.clear();
c53e6443 sago007 2012-04-17 11:04 47
	converter << num;
c53e6443 sago007 2012-04-17 11:04 48
	return converter.str();
769a41a0 sago007 2008-09-24 12:54 49
}
769a41a0 sago007 2008-09-24 12:54 50
ecef5838 sago007 2015-11-24 20:01 51
string double2str(double num) {
444cec07 sago007 2012-05-05 16:42 52
	converter.str(std::string());
444cec07 sago007 2012-05-05 16:42 53
	converter.clear();
c53e6443 sago007 2012-04-17 11:04 54
	converter << num;
c53e6443 sago007 2012-04-17 11:04 55
	return converter.str();
c53e6443 sago007 2012-04-17 11:04 56
}
769a41a0 sago007 2008-09-24 12:54 57
f306e3ca sago007 2015-12-04 21:16 58
bool strequals(const char* a, const char* b) {
f306e3ca sago007 2015-12-04 21:16 59
	return strcmp(a,b) == 0;
f306e3ca sago007 2015-12-04 21:16 60
}
f306e3ca sago007 2015-12-04 21:16 61
078900fe sago007 2015-12-05 15:31 62
void dieOnNullptr(bool ptr, const char* msg) {
078900fe sago007 2015-12-05 15:31 63
	if (!ptr) {
4e257de2 sago007 2016-01-22 17:43 64
		sago::SagoFatalError(msg);
078900fe sago007 2015-12-05 15:31 65
	}
078900fe sago007 2015-12-05 15:31 66
}
078900fe sago007 2015-12-05 15:31 67
ecef5838 sago007 2015-11-24 20:01 68
double str2double(const string& str2parse) {
ecef5838 sago007 2015-11-24 20:01 69
	try {
444cec07 sago007 2012-05-05 16:42 70
		converter.clear();
444cec07 sago007 2012-05-05 16:42 71
		converter.str(str2parse);
c53e6443 sago007 2012-04-17 11:04 72
		double val = 0.0;
c53e6443 sago007 2012-04-17 11:04 73
		converter >> val;
c53e6443 sago007 2012-04-17 11:04 74
		return val;
c53e6443 sago007 2012-04-17 11:04 75
	}
ecef5838 sago007 2015-11-24 20:01 76
	catch (ios_base::failure& f) {
c53e6443 sago007 2012-04-17 11:04 77
		return 0.0;
c53e6443 sago007 2012-04-17 11:04 78
	}
769a41a0 sago007 2008-09-24 12:54 79
}
769a41a0 sago007 2008-09-24 12:54 80
15b9c93b sago007 2016-02-03 20:03 81
std::string SPrintStringF(const char* fmt, ...) {
15b9c93b sago007 2016-02-03 20:03 82
	std::string ret;
15b9c93b sago007 2016-02-03 20:03 83
	char buffer[1024];
15b9c93b sago007 2016-02-03 20:03 84
	va_list args;
15b9c93b sago007 2016-02-03 20:03 85
	va_start(args, fmt);
15b9c93b sago007 2016-02-03 20:03 86
	vsnprintf(buffer, sizeof(buffer), fmt, args);
15b9c93b sago007 2016-02-03 20:03 87
	ret = buffer;
15b9c93b sago007 2016-02-03 20:03 88
	va_end(args);
15b9c93b sago007 2016-02-03 20:03 89
	return ret;
15b9c93b sago007 2016-02-03 20:03 90
}
15b9c93b sago007 2016-02-03 20:03 91
15b9c93b sago007 2016-02-03 20:03 92
const char* SPrintCF(const char* fmt, ...) {
15b9c93b sago007 2016-02-03 20:03 93
	static char buffer[1024];
15b9c93b sago007 2016-02-03 20:03 94
	va_list args;
15b9c93b sago007 2016-02-03 20:03 95
	va_start(args, fmt);
15b9c93b sago007 2016-02-03 20:03 96
	vsnprintf(buffer, sizeof(buffer), fmt, args);
15b9c93b sago007 2016-02-03 20:03 97
	va_end(args);
15b9c93b sago007 2016-02-03 20:03 98
	return buffer;
15b9c93b sago007 2016-02-03 20:03 99
}
15b9c93b sago007 2016-02-03 20:03 100
ecef5838 sago007 2015-11-24 20:01 101
int str2int(const string& str2parse) {
ecef5838 sago007 2015-11-24 20:01 102
	try {
444cec07 sago007 2012-05-05 16:42 103
		converter.clear();
444cec07 sago007 2012-05-05 16:42 104
		converter.str(str2parse);
c53e6443 sago007 2012-04-17 11:04 105
		int val = 0;
c53e6443 sago007 2012-04-17 11:04 106
		converter >> val;
c53e6443 sago007 2012-04-17 11:04 107
		return val;
c53e6443 sago007 2012-04-17 11:04 108
	}
ecef5838 sago007 2015-11-24 20:01 109
	catch (ios_base::failure& f) {
c53e6443 sago007 2012-04-17 11:04 110
		return 0;
c53e6443 sago007 2012-04-17 11:04 111
	}
769a41a0 sago007 2008-09-24 12:54 112
}
89c4a3a6 sago007 2008-08-29 14:32 113
d07b805e sago007 2009-01-27 13:15 114
/**
d07b805e sago007 2009-01-27 13:15 115
 * Takes a number of milliseconds and returns the value in commonTime format.
d07b805e sago007 2009-01-27 13:15 116
 */
ecef5838 sago007 2015-11-24 20:01 117
commonTime TimeHandler::ms2ct(unsigned int milliseconds) {
c53e6443 sago007 2012-04-17 11:04 118
	commonTime ct;
c53e6443 sago007 2012-04-17 11:04 119
	ct.days = 0;
c53e6443 sago007 2012-04-17 11:04 120
	unsigned int time = milliseconds;
c53e6443 sago007 2012-04-17 11:04 121
	ct.hours = time/(1000*60*60);
c53e6443 sago007 2012-04-17 11:04 122
	time = time % (1000*60*60);
c53e6443 sago007 2012-04-17 11:04 123
	ct.minutes = time/(1000*60);
c53e6443 sago007 2012-04-17 11:04 124
	time = time % (1000*60);
c53e6443 sago007 2012-04-17 11:04 125
	ct.seconds = time/1000;
c53e6443 sago007 2012-04-17 11:04 126
	return ct;
c09d0e4f sago007 2009-01-27 01:15 127
}
89c4a3a6 sago007 2008-08-29 14:32 128
ecef5838 sago007 2015-11-24 20:01 129
commonTime TimeHandler::getTime(const string& name) {
c53e6443 sago007 2012-04-17 11:04 130
	commonTime ct;
c53e6443 sago007 2012-04-17 11:04 131
	ct.days = Config::getInstance()->getInt(name+"Days");
c53e6443 sago007 2012-04-17 11:04 132
	ct.hours = Config::getInstance()->getInt(name+"Hours");
c53e6443 sago007 2012-04-17 11:04 133
	ct.minutes = Config::getInstance()->getInt(name+"Minutes");
c53e6443 sago007 2012-04-17 11:04 134
	ct.seconds = Config::getInstance()->getInt(name+"Seconds");
c53e6443 sago007 2012-04-17 11:04 135
	return ct;
89c4a3a6 sago007 2008-08-29 14:32 136
}
89c4a3a6 sago007 2008-08-29 14:32 137
d07b805e sago007 2009-01-27 13:15 138
/**
c53e6443 sago007 2012-04-17 11:04 139
 * Returns the total runtime with toAdd added but without writing it to config file.
d07b805e sago007 2009-01-27 13:15 140
 * Used for stats
c09d0e4f sago007 2009-01-27 01:15 141
 */
ecef5838 sago007 2015-11-24 20:01 142
commonTime TimeHandler::peekTime(const string& name, const commonTime& toAdd) {
c53e6443 sago007 2012-04-17 11:04 143
	commonTime ct = getTime(name);
c09d0e4f sago007 2009-01-27 01:15 144
c53e6443 sago007 2012-04-17 11:04 145
	ct.seconds +=toAdd.seconds;
c53e6443 sago007 2012-04-17 11:04 146
	ct.minutes +=ct.seconds/60;
c53e6443 sago007 2012-04-17 11:04 147
	ct.seconds = ct.seconds%60;
c09d0e4f sago007 2009-01-27 01:15 148
c53e6443 sago007 2012-04-17 11:04 149
	ct.minutes += toAdd.minutes;
c53e6443 sago007 2012-04-17 11:04 150
	ct.hours += ct.minutes/60;
c53e6443 sago007 2012-04-17 11:04 151
	ct.minutes = ct.minutes%60;
c09d0e4f sago007 2009-01-27 01:15 152
c53e6443 sago007 2012-04-17 11:04 153
	ct.hours += toAdd.hours;
c53e6443 sago007 2012-04-17 11:04 154
	ct.days += ct.hours/24;
c53e6443 sago007 2012-04-17 11:04 155
	ct.hours = ct.hours%24;
c09d0e4f sago007 2009-01-27 01:15 156
c53e6443 sago007 2012-04-17 11:04 157
	ct.days += toAdd.days;
c53e6443 sago007 2012-04-17 11:04 158
	return ct;
c09d0e4f sago007 2009-01-27 01:15 159
}
c09d0e4f sago007 2009-01-27 01:15 160
d07b805e sago007 2009-01-27 13:15 161
/**
c09d0e4f sago007 2009-01-27 01:15 162
 * Same as peekTotalTime but writes the time to the config file.
d07b805e sago007 2009-01-27 13:15 163
 * Should only be called only once! when the program shuts down
c09d0e4f sago007 2009-01-27 01:15 164
 */
ecef5838 sago007 2015-11-24 20:01 165
commonTime TimeHandler::addTime(const string& name, const commonTime& toAdd) {
c53e6443 sago007 2012-04-17 11:04 166
	commonTime ct = peekTime(name,toAdd);
c53e6443 sago007 2012-04-17 11:04 167
c53e6443 sago007 2012-04-17 11:04 168
	Config::getInstance()->setInt(name+"Days",ct.days);
c53e6443 sago007 2012-04-17 11:04 169
	Config::getInstance()->setInt(name+"Hours",ct.hours);
c53e6443 sago007 2012-04-17 11:04 170
	Config::getInstance()->setInt(name+"Minutes",ct.minutes);
c53e6443 sago007 2012-04-17 11:04 171
	Config::getInstance()->setInt(name+"Seconds",ct.seconds);
c53e6443 sago007 2012-04-17 11:04 172
	return ct;
89c4a3a6 sago007 2008-08-29 14:32 173
}
769a41a0 sago007 2008-09-24 12:54 174
769a41a0 sago007 2008-09-24 12:54 175
Config* Config::instance = 0;
769a41a0 sago007 2008-09-24 12:54 176
ecef5838 sago007 2015-11-24 20:01 177
Config::Config() {
c53e6443 sago007 2012-04-17 11:04 178
	configMap.clear();
c53e6443 sago007 2012-04-17 11:04 179
	load();
c53e6443 sago007 2012-04-17 11:04 180
	shuttingDown = 0; // Not shutting down
769a41a0 sago007 2008-09-24 12:54 181
}
769a41a0 sago007 2008-09-24 12:54 182
ecef5838 sago007 2015-11-24 20:01 183
void Config::load() {
30412ca6 sago007 2016-03-25 19:35 184
	string filecontent = sago::GetFileContent("configFile");
30412ca6 sago007 2016-03-25 19:35 185
	stringstream inFile(filecontent);
c53e6443 sago007 2012-04-17 11:04 186
	string key;
c53e6443 sago007 2012-04-17 11:04 187
	string previuskey;
c53e6443 sago007 2012-04-17 11:04 188
	char value[MAX_VAR_LENGTH];
ecef5838 sago007 2015-11-24 20:01 189
	if (inFile) {
ecef5838 sago007 2015-11-24 20:01 190
		while (!inFile.eof()) {
c53e6443 sago007 2012-04-17 11:04 191
			inFile >> key;
ecef5838 sago007 2015-11-24 20:01 192
			if (key==previuskey) { //the last entry will be read 2 times if a linebreak is missing in the end
c53e6443 sago007 2012-04-17 11:04 193
				continue;
acd79baf sago007 2015-11-22 19:37 194
			}
c53e6443 sago007 2012-04-17 11:04 195
			previuskey = key;
c53e6443 sago007 2012-04-17 11:04 196
			inFile.get(); //Read the space between the key and the content
c53e6443 sago007 2012-04-17 11:04 197
			inFile.getline(value,MAX_VAR_LENGTH);
c53e6443 sago007 2012-04-17 11:04 198
#if DEBUG
c53e6443 sago007 2012-04-17 11:04 199
			cout << "Config "<< "read: " << key << " with:\"" << value << "\"" << endl;
c53e6443 sago007 2012-04-17 11:04 200
#endif
c53e6443 sago007 2012-04-17 11:04 201
			configMap[key] = (string)value;
c53e6443 sago007 2012-04-17 11:04 202
		}
c53e6443 sago007 2012-04-17 11:04 203
	}
769a41a0 sago007 2008-09-24 12:54 204
}
769a41a0 sago007 2008-09-24 12:54 205
ecef5838 sago007 2015-11-24 20:01 206
Config* Config::getInstance() {
ecef5838 sago007 2015-11-24 20:01 207
	if (Config::instance==0) {
c53e6443 sago007 2012-04-17 11:04 208
		Config::instance = new Config();
c53e6443 sago007 2012-04-17 11:04 209
c53e6443 sago007 2012-04-17 11:04 210
	}
c53e6443 sago007 2012-04-17 11:04 211
	return Config::instance;
769a41a0 sago007 2008-09-24 12:54 212
}
769a41a0 sago007 2008-09-24 12:54 213
ecef5838 sago007 2015-11-24 20:01 214
void Config::save() {
59dcb571 sago007 2016-02-14 15:34 215
	std::stringstream outFile;
59dcb571 sago007 2016-02-14 15:34 216
	map<string,string>::iterator iter;
59dcb571 sago007 2016-02-14 15:34 217
	for (iter = configMap.begin(); iter != configMap.end(); iter++) {
59dcb571 sago007 2016-02-14 15:34 218
		outFile << iter->first << " " << iter->second << endl;
c53e6443 sago007 2012-04-17 11:04 219
	}
59dcb571 sago007 2016-02-14 15:34 220
	outFile << "\n"; //The last entry in the file will be read double if a linebreak is missing
59dcb571 sago007 2016-02-14 15:34 221
	//This is checked on load too in case a user changes it himself.
59dcb571 sago007 2016-02-14 15:34 222
	sago::WriteFileContent("configFile", outFile.str());
769a41a0 sago007 2008-09-24 12:54 223
}
769a41a0 sago007 2008-09-24 12:54 224
ecef5838 sago007 2015-11-24 20:01 225
bool Config::exists(const string& varName) const {
c53e6443 sago007 2012-04-17 11:04 226
	//Using that find returns an iterator to the end of the map if not found
c53e6443 sago007 2012-04-17 11:04 227
	return configMap.find(varName) != configMap.end();
769a41a0 sago007 2008-09-24 12:54 228
}
769a41a0 sago007 2008-09-24 12:54 229
ecef5838 sago007 2015-11-24 20:01 230
void Config::setDefault(const string& varName,const string& content) {
ecef5838 sago007 2015-11-24 20:01 231
	if (exists(varName)) {
acd79baf sago007 2015-11-22 19:37 232
		return;    //Already exists do not change
acd79baf sago007 2015-11-22 19:37 233
	}
c53e6443 sago007 2012-04-17 11:04 234
	setString(varName,content);
42f8ed5f sago007 2009-03-05 11:47 235
}
42f8ed5f sago007 2009-03-05 11:47 236
ecef5838 sago007 2015-11-24 20:01 237
void Config::setShuttingDown(long shuttingDown) {
c53e6443 sago007 2012-04-17 11:04 238
	this->shuttingDown = shuttingDown;
cc3ef158 sago007 2011-07-03 16:13 239
}
cc3ef158 sago007 2011-07-03 16:13 240
ecef5838 sago007 2015-11-24 20:01 241
long Config::isShuttingDown() const {
c53e6443 sago007 2012-04-17 11:04 242
	return shuttingDown;
cc3ef158 sago007 2011-07-03 16:13 243
}
cc3ef158 sago007 2011-07-03 16:13 244
ecef5838 sago007 2015-11-24 20:01 245
void Config::setString(const string& varName, const string& content) {
c53e6443 sago007 2012-04-17 11:04 246
	configMap[varName] = content;
769a41a0 sago007 2008-09-24 12:54 247
}
769a41a0 sago007 2008-09-24 12:54 248
ecef5838 sago007 2015-11-24 20:01 249
void Config::setInt(const string& varName, int content) {
c53e6443 sago007 2012-04-17 11:04 250
	configMap[varName] = itoa(content);
769a41a0 sago007 2008-09-24 12:54 251
}
769a41a0 sago007 2008-09-24 12:54 252
ecef5838 sago007 2015-11-24 20:01 253
void Config::setValue(const string& varName,double content) {
c53e6443 sago007 2012-04-17 11:04 254
	configMap[varName] = double2str(content);
769a41a0 sago007 2008-09-24 12:54 255
}
769a41a0 sago007 2008-09-24 12:54 256
ecef5838 sago007 2015-11-24 20:01 257
string Config::getString(const string& varName) {
ecef5838 sago007 2015-11-24 20:01 258
	if (exists(varName)) {
c53e6443 sago007 2012-04-17 11:04 259
		return configMap[varName];
c53e6443 sago007 2012-04-17 11:04 260
	}
acd79baf sago007 2015-11-22 19:37 261
	else {
c53e6443 sago007 2012-04-17 11:04 262
		return "";
acd79baf sago007 2015-11-22 19:37 263
	}
769a41a0 sago007 2008-09-24 12:54 264
}
769a41a0 sago007 2008-09-24 12:54 265
ecef5838 sago007 2015-11-24 20:01 266
int Config::getInt(const string& varName) {
ecef5838 sago007 2015-11-24 20:01 267
	if (exists(varName)) {
c53e6443 sago007 2012-04-17 11:04 268
		return str2int(configMap[varName]);
c53e6443 sago007 2012-04-17 11:04 269
	}
acd79baf sago007 2015-11-22 19:37 270
	else {
c53e6443 sago007 2012-04-17 11:04 271
		return 0;
acd79baf sago007 2015-11-22 19:37 272
	}
769a41a0 sago007 2008-09-24 12:54 273
}
769a41a0 sago007 2008-09-24 12:54 274
ecef5838 sago007 2015-11-24 20:01 275
double Config::getValue(const string& varName) {
ecef5838 sago007 2015-11-24 20:01 276
	if (exists(varName)) {
c53e6443 sago007 2012-04-17 11:04 277
		return str2double(configMap[varName]);
c53e6443 sago007 2012-04-17 11:04 278
	}
acd79baf sago007 2015-11-22 19:37 279
	else {
c53e6443 sago007 2012-04-17 11:04 280
		return 0.0;
acd79baf sago007 2015-11-22 19:37 281
	}
769a41a0 sago007 2008-09-24 12:54 282
}
1970-01-01 00:00 283