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