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