git repos / blockattack-game

blame: source/code/highscore.cpp

normal view · raw

ecef5838 sago007 2015-11-24 20:01 1
/*
ecef5838 sago007 2015-11-24 20:01 2
===========================================================================
ecef5838 sago007 2015-11-24 20:01 3
blockattack - Block Attack - Rise of the Blocks
ecef5838 sago007 2015-11-24 20:01 4
Copyright (C) 2005-2012 Poul Sander
ecef5838 sago007 2015-11-24 20:01 5
ecef5838 sago007 2015-11-24 20:01 6
This program is free software: you can redistribute it and/or modify
ecef5838 sago007 2015-11-24 20:01 7
it under the terms of the GNU General Public License as published by
ecef5838 sago007 2015-11-24 20:01 8
the Free Software Foundation, either version 2 of the License, or
ecef5838 sago007 2015-11-24 20:01 9
(at your option) any later version.
ecef5838 sago007 2015-11-24 20:01 10
ecef5838 sago007 2015-11-24 20:01 11
This program is distributed in the hope that it will be useful,
ecef5838 sago007 2015-11-24 20:01 12
but WITHOUT ANY WARRANTY; without even the implied warranty of
ecef5838 sago007 2015-11-24 20:01 13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ecef5838 sago007 2015-11-24 20:01 14
GNU General Public License for more details.
ecef5838 sago007 2015-11-24 20:01 15
ecef5838 sago007 2015-11-24 20:01 16
You should have received a copy of the GNU General Public License
ecef5838 sago007 2015-11-24 20:01 17
along with this program.  If not, see http://www.gnu.org/licenses/
ecef5838 sago007 2015-11-24 20:01 18
ecef5838 sago007 2015-11-24 20:01 19
Source information and contacts persons can be found at
021de090 sago007 2015-12-30 18:56 20
http://blockattack.net
ecef5838 sago007 2015-11-24 20:01 21
===========================================================================
ecef5838 sago007 2015-11-24 20:01 22
*/
ecef5838 sago007 2015-11-24 20:01 23
ecef5838 sago007 2015-11-24 20:01 24
#include "highscore.h"
ecef5838 sago007 2015-11-24 20:01 25
ecef5838 sago007 2015-11-24 20:01 26
using namespace std;
ecef5838 sago007 2015-11-24 20:01 27
b81f58ad sago007 2015-12-25 22:48 28
#ifdef _WIN32
ecef5838 sago007 2015-11-24 20:01 29
ecef5838 sago007 2015-11-24 20:01 30
//Returns path to "my Documents" in windows:
ecef5838 sago007 2015-11-24 20:01 31
string getMyDocumentsPath1() {
ecef5838 sago007 2015-11-24 20:01 32
	TCHAR pszPath[MAX_PATH];
ecef5838 sago007 2015-11-24 20:01 33
	//if (SUCCEEDED(SHGetSpecialFolderPath(nullptr, pszPath, CSIDL_PERSONAL, FALSE))) {
ecef5838 sago007 2015-11-24 20:01 34
	if (SUCCEEDED(SHGetSpecialFolderPath(nullptr, pszPath, CSIDL_PERSONAL, FALSE))) {
ecef5838 sago007 2015-11-24 20:01 35
		// pszPath is now the path that you want
ecef5838 sago007 2015-11-24 20:01 36
		cout << "MyDocuments Located: " << pszPath << endl;
ecef5838 sago007 2015-11-24 20:01 37
		string theResult= pszPath;
ecef5838 sago007 2015-11-24 20:01 38
		return theResult;
ecef5838 sago007 2015-11-24 20:01 39
	}
ecef5838 sago007 2015-11-24 20:01 40
	else {
ecef5838 sago007 2015-11-24 20:01 41
		cout << "Warning: My Documents not found!" << endl;
ecef5838 sago007 2015-11-24 20:01 42
		string theResult ="";
ecef5838 sago007 2015-11-24 20:01 43
		return theResult;
ecef5838 sago007 2015-11-24 20:01 44
	}
ecef5838 sago007 2015-11-24 20:01 45
}
ecef5838 sago007 2015-11-24 20:01 46
ecef5838 sago007 2015-11-24 20:01 47
#endif
ecef5838 sago007 2015-11-24 20:01 48
ecef5838 sago007 2015-11-24 20:01 49
Highscore::Highscore(int type) {
ecef5838 sago007 2015-11-24 20:01 50
#if defined(__unix__)
ecef5838 sago007 2015-11-24 20:01 51
	string home = getenv("HOME");
ecef5838 sago007 2015-11-24 20:01 52
	string filename1 = home+"/.gamesaves/blockattack/endless.dat";
ecef5838 sago007 2015-11-24 20:01 53
	string filename2 = home+"/.gamesaves/blockattack/timetrial.dat";
ecef5838 sago007 2015-11-24 20:01 54
#elif defined(_WIN32)
ecef5838 sago007 2015-11-24 20:01 55
	string home = getMyDocumentsPath1();
ecef5838 sago007 2015-11-24 20:01 56
	string filename1, filename2;
ecef5838 sago007 2015-11-24 20:01 57
	if (&home!=nullptr) {
ecef5838 sago007 2015-11-24 20:01 58
		filename1 = home+"/My Games/blockattack/endless.dat";
ecef5838 sago007 2015-11-24 20:01 59
		filename2 = home+"/My Games/blockattack/timetrial.dat";
ecef5838 sago007 2015-11-24 20:01 60
	}
ecef5838 sago007 2015-11-24 20:01 61
	else {
ecef5838 sago007 2015-11-24 20:01 62
		filename1 = "endless.dat";
ecef5838 sago007 2015-11-24 20:01 63
		filename2 = "timetrial.dat";
ecef5838 sago007 2015-11-24 20:01 64
	}
ecef5838 sago007 2015-11-24 20:01 65
#else
ecef5838 sago007 2015-11-24 20:01 66
	string filename1 = "endless.dat";
ecef5838 sago007 2015-11-24 20:01 67
	string filename2 = "timetrial.dat";
ecef5838 sago007 2015-11-24 20:01 68
#endif
ecef5838 sago007 2015-11-24 20:01 69
	ourType = type;
ecef5838 sago007 2015-11-24 20:01 70
	if (type == 1) {
ecef5838 sago007 2015-11-24 20:01 71
		filename = filename1;
ecef5838 sago007 2015-11-24 20:01 72
	}
ecef5838 sago007 2015-11-24 20:01 73
	if (type == 2) {
ecef5838 sago007 2015-11-24 20:01 74
		filename = filename2;
ecef5838 sago007 2015-11-24 20:01 75
	}
ecef5838 sago007 2015-11-24 20:01 76
	ifstream scorefile(filename.c_str(), ios::binary);
ecef5838 sago007 2015-11-24 20:01 77
	if (scorefile) {
ecef5838 sago007 2015-11-24 20:01 78
		for (int i = 0; i<top; i++) {
ecef5838 sago007 2015-11-24 20:01 79
			scorefile.read(tabel[i].name,30*sizeof(char));
ecef5838 sago007 2015-11-24 20:01 80
			scorefile.read(reinterpret_cast<char*>(&tabel[i].score), sizeof(int));
ecef5838 sago007 2015-11-24 20:01 81
		}
ecef5838 sago007 2015-11-24 20:01 82
	}
ecef5838 sago007 2015-11-24 20:01 83
	else {
ecef5838 sago007 2015-11-24 20:01 84
		for (int i = 0; i<top; i++) {
ecef5838 sago007 2015-11-24 20:01 85
			strcpy(tabel[i].name,"Poul Sander                  \0");
ecef5838 sago007 2015-11-24 20:01 86
			tabel[i].score = 2000 - i*100;
ecef5838 sago007 2015-11-24 20:01 87
		}
ecef5838 sago007 2015-11-24 20:01 88
	}
ecef5838 sago007 2015-11-24 20:01 89
	scorefile.close();
ecef5838 sago007 2015-11-24 20:01 90
	writeFile();
ecef5838 sago007 2015-11-24 20:01 91
}
ecef5838 sago007 2015-11-24 20:01 92
ecef5838 sago007 2015-11-24 20:01 93
void Highscore::writeFile() {
ecef5838 sago007 2015-11-24 20:01 94
#if defined(__unix__)
ecef5838 sago007 2015-11-24 20:01 95
	string home = getenv("HOME");
ecef5838 sago007 2015-11-24 20:01 96
	string filename1 = home+"/.gamesaves/blockattack/endless.dat";
ecef5838 sago007 2015-11-24 20:01 97
	string filename2 = home+"/.gamesaves/blockattack/timetrial.dat";
ecef5838 sago007 2015-11-24 20:01 98
#elif defined(_WIN32)
ecef5838 sago007 2015-11-24 20:01 99
	string home = getMyDocumentsPath1();
ecef5838 sago007 2015-11-24 20:01 100
	string filename1, filename2;
ecef5838 sago007 2015-11-24 20:01 101
	if (&home!=nullptr) {
ecef5838 sago007 2015-11-24 20:01 102
		filename1 = home+"/My Games/blockattack/endless.dat";
ecef5838 sago007 2015-11-24 20:01 103
		filename2 = home+"/My Games/blockattack/timetrial.dat";
ecef5838 sago007 2015-11-24 20:01 104
	}
ecef5838 sago007 2015-11-24 20:01 105
	else {
ecef5838 sago007 2015-11-24 20:01 106
		filename1 = "endless.dat";
ecef5838 sago007 2015-11-24 20:01 107
		filename2 = "timetrial.dat";
ecef5838 sago007 2015-11-24 20:01 108
	}
ecef5838 sago007 2015-11-24 20:01 109
#else
ecef5838 sago007 2015-11-24 20:01 110
	string filename1 = "endless.dat";
ecef5838 sago007 2015-11-24 20:01 111
	string filename2 = "timetrial.dat";
ecef5838 sago007 2015-11-24 20:01 112
#endif
ecef5838 sago007 2015-11-24 20:01 113
	if (ourType == 1) {
ecef5838 sago007 2015-11-24 20:01 114
		filename = filename1;
ecef5838 sago007 2015-11-24 20:01 115
	}
ecef5838 sago007 2015-11-24 20:01 116
	if (ourType == 2) {
ecef5838 sago007 2015-11-24 20:01 117
		filename = filename2;
ecef5838 sago007 2015-11-24 20:01 118
	}
ecef5838 sago007 2015-11-24 20:01 119
ecef5838 sago007 2015-11-24 20:01 120
	ofstream outfile;
ecef5838 sago007 2015-11-24 20:01 121
	outfile.open(Highscore::filename.c_str(), ios::binary |ios::trunc);
ecef5838 sago007 2015-11-24 20:01 122
	if (!outfile) {
ecef5838 sago007 2015-11-24 20:01 123
		cout << "Error writing to file: " << filename << endl;
ecef5838 sago007 2015-11-24 20:01 124
		exit(1);
ecef5838 sago007 2015-11-24 20:01 125
	}
ecef5838 sago007 2015-11-24 20:01 126
	for (int i = 0; i<top; i++) {
ecef5838 sago007 2015-11-24 20:01 127
		outfile.write(tabel[i].name,30*sizeof(char));
ecef5838 sago007 2015-11-24 20:01 128
		outfile.write(reinterpret_cast<char*>(&tabel[i].score),sizeof(int));
ecef5838 sago007 2015-11-24 20:01 129
	}
ecef5838 sago007 2015-11-24 20:01 130
	outfile.close();
ecef5838 sago007 2015-11-24 20:01 131
}
ecef5838 sago007 2015-11-24 20:01 132
ecef5838 sago007 2015-11-24 20:01 133
bool Highscore::isHighScore(int newScore) {
ecef5838 sago007 2015-11-24 20:01 134
	if (newScore>tabel[top-1].score) {
ecef5838 sago007 2015-11-24 20:01 135
		return true;
ecef5838 sago007 2015-11-24 20:01 136
	}
ecef5838 sago007 2015-11-24 20:01 137
	else {
ecef5838 sago007 2015-11-24 20:01 138
		return false;
ecef5838 sago007 2015-11-24 20:01 139
	}
ecef5838 sago007 2015-11-24 20:01 140
}
ecef5838 sago007 2015-11-24 20:01 141
ecef5838 sago007 2015-11-24 20:01 142
void Highscore::addScore(const string& newName, int newScore) {
ecef5838 sago007 2015-11-24 20:01 143
	int ranking = top-1;
dfdb7d66 sago007 2015-11-29 19:21 144
	while ( (ranking != 0) && (tabel[ranking-1].score<newScore)) {
ecef5838 sago007 2015-11-24 20:01 145
		ranking--;
ecef5838 sago007 2015-11-24 20:01 146
	}
ecef5838 sago007 2015-11-24 20:01 147
	for (int i=top-1; i>ranking; i--) {
ecef5838 sago007 2015-11-24 20:01 148
		tabel[i].score = tabel[i-1].score;
ecef5838 sago007 2015-11-24 20:01 149
		strcpy(tabel[i].name,tabel[i-1].name);
ecef5838 sago007 2015-11-24 20:01 150
	}
ecef5838 sago007 2015-11-24 20:01 151
	tabel[ranking].score = newScore;
ecef5838 sago007 2015-11-24 20:01 152
	snprintf(tabel[ranking].name, sizeof(tabel[ranking].name), "%s", newName.c_str());
ecef5838 sago007 2015-11-24 20:01 153
	Highscore::writeFile();
ecef5838 sago007 2015-11-24 20:01 154
}
ecef5838 sago007 2015-11-24 20:01 155
ecef5838 sago007 2015-11-24 20:01 156
int Highscore::getScoreNumber(int room) {
ecef5838 sago007 2015-11-24 20:01 157
	return tabel[room].score;
ecef5838 sago007 2015-11-24 20:01 158
}
ecef5838 sago007 2015-11-24 20:01 159
ecef5838 sago007 2015-11-24 20:01 160
char* Highscore::getScoreName(int room) {
ecef5838 sago007 2015-11-24 20:01 161
	return &tabel[room].name[0];
ecef5838 sago007 2015-11-24 20:01 162
}
1970-01-01 00:00 163