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