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