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