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