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
021de090 sago007 2015-12-30 18:56 20
http://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"
15b9c93b sago007 2016-02-03 20:03 29
#include <stdarg.h>
769a41a0 sago007 2008-09-24 12:54 30
53001fa2 sago007 2015-11-08 15:13 31
using namespace std;
53001fa2 sago007 2015-11-08 15:13 32
using boost::format;
53001fa2 sago007 2015-11-08 15:13 33
444cec07 sago007 2012-05-05 16:42 34
static stringstream converter;
444cec07 sago007 2012-05-05 16:42 35
769a41a0 sago007 2008-09-24 12:54 36
//Function to convert numbers to string
ecef5838 sago007 2015-11-24 20:01 37
string itoa(int num) {
444cec07 sago007 2012-05-05 16:42 38
	converter.str(std::string());
444cec07 sago007 2012-05-05 16:42 39
	converter.clear();
c53e6443 sago007 2012-04-17 11:04 40
	converter << num;
c53e6443 sago007 2012-04-17 11:04 41
	return converter.str();
769a41a0 sago007 2008-09-24 12:54 42
}
769a41a0 sago007 2008-09-24 12:54 43
ecef5838 sago007 2015-11-24 20:01 44
string double2str(double 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();
c53e6443 sago007 2012-04-17 11:04 49
}
769a41a0 sago007 2008-09-24 12:54 50
f306e3ca sago007 2015-12-04 21:16 51
bool strequals(const char* a, const char* b) {
f306e3ca sago007 2015-12-04 21:16 52
	return strcmp(a,b) == 0;
f306e3ca sago007 2015-12-04 21:16 53
}
f306e3ca sago007 2015-12-04 21:16 54
078900fe sago007 2015-12-05 15:31 55
void dieOnNullptr(bool ptr, const char* msg) {
078900fe sago007 2015-12-05 15:31 56
	if (!ptr) {
4e257de2 sago007 2016-01-22 17:43 57
		sago::SagoFatalError(msg);
078900fe sago007 2015-12-05 15:31 58
	}
078900fe sago007 2015-12-05 15:31 59
}
078900fe sago007 2015-12-05 15:31 60
ecef5838 sago007 2015-11-24 20:01 61
double str2double(const string& str2parse) {
ecef5838 sago007 2015-11-24 20:01 62
	try {
444cec07 sago007 2012-05-05 16:42 63
		converter.clear();
444cec07 sago007 2012-05-05 16:42 64
		converter.str(str2parse);
c53e6443 sago007 2012-04-17 11:04 65
		double val = 0.0;
c53e6443 sago007 2012-04-17 11:04 66
		converter >> val;
c53e6443 sago007 2012-04-17 11:04 67
		return val;
c53e6443 sago007 2012-04-17 11:04 68
	}
ecef5838 sago007 2015-11-24 20:01 69
	catch (ios_base::failure& f) {
c53e6443 sago007 2012-04-17 11:04 70
		return 0.0;
c53e6443 sago007 2012-04-17 11:04 71
	}
769a41a0 sago007 2008-09-24 12:54 72
}
769a41a0 sago007 2008-09-24 12:54 73
15b9c93b sago007 2016-02-03 20:03 74
std::string SPrintStringF(const char* fmt, ...) {
15b9c93b sago007 2016-02-03 20:03 75
	std::string ret;
15b9c93b sago007 2016-02-03 20:03 76
	char buffer[1024];
15b9c93b sago007 2016-02-03 20:03 77
	va_list args;
15b9c93b sago007 2016-02-03 20:03 78
	va_start(args, fmt);
15b9c93b sago007 2016-02-03 20:03 79
	vsnprintf(buffer, sizeof(buffer), fmt, args);
15b9c93b sago007 2016-02-03 20:03 80
	ret = buffer;
15b9c93b sago007 2016-02-03 20:03 81
	va_end(args);
15b9c93b sago007 2016-02-03 20:03 82
	return ret;
15b9c93b sago007 2016-02-03 20:03 83
}
15b9c93b sago007 2016-02-03 20:03 84
15b9c93b sago007 2016-02-03 20:03 85
const char* SPrintCF(const char* fmt, ...) {
15b9c93b sago007 2016-02-03 20:03 86
	static char buffer[1024];
15b9c93b sago007 2016-02-03 20:03 87
	va_list args;
15b9c93b sago007 2016-02-03 20:03 88
	va_start(args, fmt);
15b9c93b sago007 2016-02-03 20:03 89
	vsnprintf(buffer, sizeof(buffer), fmt, args);
15b9c93b sago007 2016-02-03 20:03 90
	va_end(args);
15b9c93b sago007 2016-02-03 20:03 91
	return buffer;
15b9c93b sago007 2016-02-03 20:03 92
}
15b9c93b sago007 2016-02-03 20:03 93
ecef5838 sago007 2015-11-24 20:01 94
int str2int(const string& str2parse) {
ecef5838 sago007 2015-11-24 20:01 95
	try {
444cec07 sago007 2012-05-05 16:42 96
		converter.clear();
444cec07 sago007 2012-05-05 16:42 97
		converter.str(str2parse);
c53e6443 sago007 2012-04-17 11:04 98
		int val = 0;
c53e6443 sago007 2012-04-17 11:04 99
		converter >> val;
c53e6443 sago007 2012-04-17 11:04 100
		return val;
c53e6443 sago007 2012-04-17 11:04 101
	}
ecef5838 sago007 2015-11-24 20:01 102
	catch (ios_base::failure& f) {
c53e6443 sago007 2012-04-17 11:04 103
		return 0;
c53e6443 sago007 2012-04-17 11:04 104
	}
769a41a0 sago007 2008-09-24 12:54 105
}
89c4a3a6 sago007 2008-08-29 14:32 106
d07b805e sago007 2009-01-27 13:15 107
/**
d07b805e sago007 2009-01-27 13:15 108
 * Takes a number of milliseconds and returns the value in commonTime format.
d07b805e sago007 2009-01-27 13:15 109
 */
ecef5838 sago007 2015-11-24 20:01 110
commonTime TimeHandler::ms2ct(unsigned int milliseconds) {
c53e6443 sago007 2012-04-17 11:04 111
	commonTime ct;
c53e6443 sago007 2012-04-17 11:04 112
	ct.days = 0;
c53e6443 sago007 2012-04-17 11:04 113
	unsigned int time = milliseconds;
c53e6443 sago007 2012-04-17 11:04 114
	ct.hours = time/(1000*60*60);
c53e6443 sago007 2012-04-17 11:04 115
	time = time % (1000*60*60);
c53e6443 sago007 2012-04-17 11:04 116
	ct.minutes = time/(1000*60);
c53e6443 sago007 2012-04-17 11:04 117
	time = time % (1000*60);
c53e6443 sago007 2012-04-17 11:04 118
	ct.seconds = time/1000;
c53e6443 sago007 2012-04-17 11:04 119
	return ct;
c09d0e4f sago007 2009-01-27 01:15 120
}
89c4a3a6 sago007 2008-08-29 14:32 121
ecef5838 sago007 2015-11-24 20:01 122
commonTime TimeHandler::getTime(const string& name) {
c53e6443 sago007 2012-04-17 11:04 123
	commonTime ct;
c53e6443 sago007 2012-04-17 11:04 124
	ct.days = Config::getInstance()->getInt(name+"Days");
c53e6443 sago007 2012-04-17 11:04 125
	ct.hours = Config::getInstance()->getInt(name+"Hours");
c53e6443 sago007 2012-04-17 11:04 126
	ct.minutes = Config::getInstance()->getInt(name+"Minutes");
c53e6443 sago007 2012-04-17 11:04 127
	ct.seconds = Config::getInstance()->getInt(name+"Seconds");
c53e6443 sago007 2012-04-17 11:04 128
	return ct;
89c4a3a6 sago007 2008-08-29 14:32 129
}
89c4a3a6 sago007 2008-08-29 14:32 130
d07b805e sago007 2009-01-27 13:15 131
/**
c53e6443 sago007 2012-04-17 11:04 132
 * Returns the total runtime with toAdd added but without writing it to config file.
d07b805e sago007 2009-01-27 13:15 133
 * Used for stats
c09d0e4f sago007 2009-01-27 01:15 134
 */
ecef5838 sago007 2015-11-24 20:01 135
commonTime TimeHandler::peekTime(const string& name, const commonTime& toAdd) {
c53e6443 sago007 2012-04-17 11:04 136
	commonTime ct = getTime(name);
c09d0e4f sago007 2009-01-27 01:15 137
c53e6443 sago007 2012-04-17 11:04 138
	ct.seconds +=toAdd.seconds;
c53e6443 sago007 2012-04-17 11:04 139
	ct.minutes +=ct.seconds/60;
c53e6443 sago007 2012-04-17 11:04 140
	ct.seconds = ct.seconds%60;
c09d0e4f sago007 2009-01-27 01:15 141
c53e6443 sago007 2012-04-17 11:04 142
	ct.minutes += toAdd.minutes;
c53e6443 sago007 2012-04-17 11:04 143
	ct.hours += ct.minutes/60;
c53e6443 sago007 2012-04-17 11:04 144
	ct.minutes = ct.minutes%60;
c09d0e4f sago007 2009-01-27 01:15 145
c53e6443 sago007 2012-04-17 11:04 146
	ct.hours += toAdd.hours;
c53e6443 sago007 2012-04-17 11:04 147
	ct.days += ct.hours/24;
c53e6443 sago007 2012-04-17 11:04 148
	ct.hours = ct.hours%24;
c09d0e4f sago007 2009-01-27 01:15 149
c53e6443 sago007 2012-04-17 11:04 150
	ct.days += toAdd.days;
c53e6443 sago007 2012-04-17 11:04 151
	return ct;
c09d0e4f sago007 2009-01-27 01:15 152
}
c09d0e4f sago007 2009-01-27 01:15 153
d07b805e sago007 2009-01-27 13:15 154
/**
c09d0e4f sago007 2009-01-27 01:15 155
 * Same as peekTotalTime but writes the time to the config file.
d07b805e sago007 2009-01-27 13:15 156
 * Should only be called only once! when the program shuts down
c09d0e4f sago007 2009-01-27 01:15 157
 */
ecef5838 sago007 2015-11-24 20:01 158
commonTime TimeHandler::addTime(const string& name, const commonTime& toAdd) {
c53e6443 sago007 2012-04-17 11:04 159
	commonTime ct = peekTime(name,toAdd);
c53e6443 sago007 2012-04-17 11:04 160
c53e6443 sago007 2012-04-17 11:04 161
	Config::getInstance()->setInt(name+"Days",ct.days);
c53e6443 sago007 2012-04-17 11:04 162
	Config::getInstance()->setInt(name+"Hours",ct.hours);
c53e6443 sago007 2012-04-17 11:04 163
	Config::getInstance()->setInt(name+"Minutes",ct.minutes);
c53e6443 sago007 2012-04-17 11:04 164
	Config::getInstance()->setInt(name+"Seconds",ct.seconds);
c53e6443 sago007 2012-04-17 11:04 165
	return ct;
89c4a3a6 sago007 2008-08-29 14:32 166
}
769a41a0 sago007 2008-09-24 12:54 167
769a41a0 sago007 2008-09-24 12:54 168
Config* Config::instance = 0;
769a41a0 sago007 2008-09-24 12:54 169
ecef5838 sago007 2015-11-24 20:01 170
Config::Config() {
c53e6443 sago007 2012-04-17 11:04 171
	configMap.clear();
c53e6443 sago007 2012-04-17 11:04 172
	load();
c53e6443 sago007 2012-04-17 11:04 173
	shuttingDown = 0; // Not shutting down
769a41a0 sago007 2008-09-24 12:54 174
}
769a41a0 sago007 2008-09-24 12:54 175
ecef5838 sago007 2015-11-24 20:01 176
void Config::load() {
c53e6443 sago007 2012-04-17 11:04 177
	string filename = getPathToSaveFiles()+"/configFile";
c53e6443 sago007 2012-04-17 11:04 178
	ifstream inFile(filename.c_str());
c53e6443 sago007 2012-04-17 11:04 179
	string key;
c53e6443 sago007 2012-04-17 11:04 180
	string previuskey;
c53e6443 sago007 2012-04-17 11:04 181
	char value[MAX_VAR_LENGTH];
ecef5838 sago007 2015-11-24 20:01 182
	if (inFile) {
ecef5838 sago007 2015-11-24 20:01 183
		while (!inFile.eof()) {
c53e6443 sago007 2012-04-17 11:04 184
			inFile >> key;
ecef5838 sago007 2015-11-24 20:01 185
			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 186
				continue;
acd79baf sago007 2015-11-22 19:37 187
			}
c53e6443 sago007 2012-04-17 11:04 188
			previuskey = key;
c53e6443 sago007 2012-04-17 11:04 189
			inFile.get(); //Read the space between the key and the content
c53e6443 sago007 2012-04-17 11:04 190
			inFile.getline(value,MAX_VAR_LENGTH);
c53e6443 sago007 2012-04-17 11:04 191
#if DEBUG
c53e6443 sago007 2012-04-17 11:04 192
			cout << "Config "<< "read: " << key << " with:\"" << value << "\"" << endl;
c53e6443 sago007 2012-04-17 11:04 193
#endif
c53e6443 sago007 2012-04-17 11:04 194
			configMap[key] = (string)value;
c53e6443 sago007 2012-04-17 11:04 195
		}
c53e6443 sago007 2012-04-17 11:04 196
		inFile.close();
c53e6443 sago007 2012-04-17 11:04 197
	}
769a41a0 sago007 2008-09-24 12:54 198
}
769a41a0 sago007 2008-09-24 12:54 199
ecef5838 sago007 2015-11-24 20:01 200
Config* Config::getInstance() {
ecef5838 sago007 2015-11-24 20:01 201
	if (Config::instance==0) {
c53e6443 sago007 2012-04-17 11:04 202
		Config::instance = new Config();
c53e6443 sago007 2012-04-17 11:04 203
c53e6443 sago007 2012-04-17 11:04 204
	}
c53e6443 sago007 2012-04-17 11:04 205
	return Config::instance;
769a41a0 sago007 2008-09-24 12:54 206
}
769a41a0 sago007 2008-09-24 12:54 207
ecef5838 sago007 2015-11-24 20:01 208
void Config::save() {
c53e6443 sago007 2012-04-17 11:04 209
	string filename = getPathToSaveFiles()+"/configFile";
c53e6443 sago007 2012-04-17 11:04 210
	ofstream outFile(filename.c_str(),ios::trunc);
c53e6443 sago007 2012-04-17 11:04 211
ecef5838 sago007 2015-11-24 20:01 212
	if (outFile) {
c53e6443 sago007 2012-04-17 11:04 213
		map<string,string>::iterator iter;
ecef5838 sago007 2015-11-24 20:01 214
		for (iter = configMap.begin(); iter != configMap.end(); iter++) {
c53e6443 sago007 2012-04-17 11:04 215
			outFile << iter->first << " " << iter->second << endl;
c53e6443 sago007 2012-04-17 11:04 216
		}
c53e6443 sago007 2012-04-17 11:04 217
		outFile << "\n"; //The last entry in the file will be read double if a linebreak is missing
c53e6443 sago007 2012-04-17 11:04 218
		//This is checked on load too in case a user changes it himself.
c53e6443 sago007 2012-04-17 11:04 219
	}
c53e6443 sago007 2012-04-17 11:04 220
	outFile.close();
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