git repos / blockattack-game

blame: source/code/highscore.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
89c4a3a6 sago007 2008-08-29 14:32 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.
89c4a3a6 sago007 2008-08-29 14:32 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.
89c4a3a6 sago007 2008-08-29 14:32 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/
89c4a3a6 sago007 2008-08-29 14:32 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 "highscore.h"
89c4a3a6 sago007 2008-08-29 14:32 25
cdb45e21 sago007 2015-11-22 21:35 26
using namespace std;
cdb45e21 sago007 2015-11-22 21:35 27
89c4a3a6 sago007 2008-08-29 14:32 28
#ifdef WIN32
89c4a3a6 sago007 2008-08-29 14:32 29
89c4a3a6 sago007 2008-08-29 14:32 30
//Returns path to "my Documents" in windows:
89c4a3a6 sago007 2008-08-29 14:32 31
string getMyDocumentsPath1()
89c4a3a6 sago007 2008-08-29 14:32 32
{
c53e6443 sago007 2012-04-17 11:04 33
	TCHAR pszPath[MAX_PATH];
acd79baf sago007 2015-11-22 19:37 34
	//if (SUCCEEDED(SHGetSpecialFolderPath(nullptr, pszPath, CSIDL_PERSONAL, FALSE))) {
acd79baf sago007 2015-11-22 19:37 35
	if (SUCCEEDED(SHGetSpecialFolderPath(nullptr, pszPath, CSIDL_PERSONAL, FALSE)))
c53e6443 sago007 2012-04-17 11:04 36
	{
c53e6443 sago007 2012-04-17 11:04 37
		// pszPath is now the path that you want
c53e6443 sago007 2012-04-17 11:04 38
		cout << "MyDocuments Located: " << pszPath << endl;
c53e6443 sago007 2012-04-17 11:04 39
		string theResult= pszPath;
c53e6443 sago007 2012-04-17 11:04 40
		return theResult;
c53e6443 sago007 2012-04-17 11:04 41
	}
c53e6443 sago007 2012-04-17 11:04 42
	else
c53e6443 sago007 2012-04-17 11:04 43
	{
c53e6443 sago007 2012-04-17 11:04 44
		cout << "Warning: My Documents not found!" << endl;
c53e6443 sago007 2012-04-17 11:04 45
		string theResult ="";
c53e6443 sago007 2012-04-17 11:04 46
		return theResult;
c53e6443 sago007 2012-04-17 11:04 47
	}
89c4a3a6 sago007 2008-08-29 14:32 48
}
89c4a3a6 sago007 2008-08-29 14:32 49
89c4a3a6 sago007 2008-08-29 14:32 50
#endif
89c4a3a6 sago007 2008-08-29 14:32 51
89c4a3a6 sago007 2008-08-29 14:32 52
Highscore::Highscore(int type)
89c4a3a6 sago007 2008-08-29 14:32 53
{
89c4a3a6 sago007 2008-08-29 14:32 54
#if defined(__unix__)
c53e6443 sago007 2012-04-17 11:04 55
	string home = getenv("HOME");
c53e6443 sago007 2012-04-17 11:04 56
	string filename1 = home+"/.gamesaves/blockattack/endless.dat";
c53e6443 sago007 2012-04-17 11:04 57
	string filename2 = home+"/.gamesaves/blockattack/timetrial.dat";
89c4a3a6 sago007 2008-08-29 14:32 58
#elif defined(_WIN32)
c53e6443 sago007 2012-04-17 11:04 59
	string home = getMyDocumentsPath1();
c53e6443 sago007 2012-04-17 11:04 60
	string filename1, filename2;
acd79baf sago007 2015-11-22 19:37 61
	if (&home!=nullptr)
c53e6443 sago007 2012-04-17 11:04 62
	{
c53e6443 sago007 2012-04-17 11:04 63
		filename1 = home+"/My Games/blockattack/endless.dat";
c53e6443 sago007 2012-04-17 11:04 64
		filename2 = home+"/My Games/blockattack/timetrial.dat";
c53e6443 sago007 2012-04-17 11:04 65
	}
c53e6443 sago007 2012-04-17 11:04 66
	else
c53e6443 sago007 2012-04-17 11:04 67
	{
c53e6443 sago007 2012-04-17 11:04 68
		filename1 = "endless.dat";
c53e6443 sago007 2012-04-17 11:04 69
		filename2 = "timetrial.dat";
c53e6443 sago007 2012-04-17 11:04 70
	}
89c4a3a6 sago007 2008-08-29 14:32 71
#else
c53e6443 sago007 2012-04-17 11:04 72
	string filename1 = "endless.dat";
c53e6443 sago007 2012-04-17 11:04 73
	string filename2 = "timetrial.dat";
89c4a3a6 sago007 2008-08-29 14:32 74
#endif
c53e6443 sago007 2012-04-17 11:04 75
	ourType = type;
acd79baf sago007 2015-11-22 19:37 76
	if (type == 1) {
acd79baf sago007 2015-11-22 19:37 77
		filename = filename1;
acd79baf sago007 2015-11-22 19:37 78
	}
acd79baf sago007 2015-11-22 19:37 79
	if (type == 2) {
acd79baf sago007 2015-11-22 19:37 80
		filename = filename2;
acd79baf sago007 2015-11-22 19:37 81
	}
c53e6443 sago007 2012-04-17 11:04 82
	ifstream scorefile(filename.c_str(), ios::binary);
c53e6443 sago007 2012-04-17 11:04 83
	if (scorefile)
c53e6443 sago007 2012-04-17 11:04 84
	{
c53e6443 sago007 2012-04-17 11:04 85
		for (int i = 0; i<top; i++)
c53e6443 sago007 2012-04-17 11:04 86
		{
c53e6443 sago007 2012-04-17 11:04 87
			scorefile.read(tabel[i].name,30*sizeof(char));
c53e6443 sago007 2012-04-17 11:04 88
			scorefile.read(reinterpret_cast<char*>(&tabel[i].score), sizeof(int));
c53e6443 sago007 2012-04-17 11:04 89
		}
c53e6443 sago007 2012-04-17 11:04 90
	}
c53e6443 sago007 2012-04-17 11:04 91
	else
c53e6443 sago007 2012-04-17 11:04 92
	{
c53e6443 sago007 2012-04-17 11:04 93
		for (int i = 0; i<top; i++)
c53e6443 sago007 2012-04-17 11:04 94
		{
c53e6443 sago007 2012-04-17 11:04 95
			strcpy(tabel[i].name,"Poul Sander                  \0");
c53e6443 sago007 2012-04-17 11:04 96
			tabel[i].score = 2000 - i*100;
c53e6443 sago007 2012-04-17 11:04 97
		}
c53e6443 sago007 2012-04-17 11:04 98
	}
c53e6443 sago007 2012-04-17 11:04 99
	scorefile.close();
c53e6443 sago007 2012-04-17 11:04 100
	writeFile();
89c4a3a6 sago007 2008-08-29 14:32 101
}
89c4a3a6 sago007 2008-08-29 14:32 102
89c4a3a6 sago007 2008-08-29 14:32 103
void Highscore::writeFile()
89c4a3a6 sago007 2008-08-29 14:32 104
{
89c4a3a6 sago007 2008-08-29 14:32 105
#if defined(__unix__)
c53e6443 sago007 2012-04-17 11:04 106
	string home = getenv("HOME");
c53e6443 sago007 2012-04-17 11:04 107
	string filename1 = home+"/.gamesaves/blockattack/endless.dat";
c53e6443 sago007 2012-04-17 11:04 108
	string filename2 = home+"/.gamesaves/blockattack/timetrial.dat";
89c4a3a6 sago007 2008-08-29 14:32 109
#elif defined(_WIN32)
c53e6443 sago007 2012-04-17 11:04 110
	string home = getMyDocumentsPath1();
c53e6443 sago007 2012-04-17 11:04 111
	string filename1, filename2;
acd79baf sago007 2015-11-22 19:37 112
	if (&home!=nullptr)
c53e6443 sago007 2012-04-17 11:04 113
	{
c53e6443 sago007 2012-04-17 11:04 114
		filename1 = home+"/My Games/blockattack/endless.dat";
c53e6443 sago007 2012-04-17 11:04 115
		filename2 = home+"/My Games/blockattack/timetrial.dat";
c53e6443 sago007 2012-04-17 11:04 116
	}
c53e6443 sago007 2012-04-17 11:04 117
	else
c53e6443 sago007 2012-04-17 11:04 118
	{
c53e6443 sago007 2012-04-17 11:04 119
		filename1 = "endless.dat";
c53e6443 sago007 2012-04-17 11:04 120
		filename2 = "timetrial.dat";
c53e6443 sago007 2012-04-17 11:04 121
	}
89c4a3a6 sago007 2008-08-29 14:32 122
#else
c53e6443 sago007 2012-04-17 11:04 123
	string filename1 = "endless.dat";
c53e6443 sago007 2012-04-17 11:04 124
	string filename2 = "timetrial.dat";
89c4a3a6 sago007 2008-08-29 14:32 125
#endif
acd79baf sago007 2015-11-22 19:37 126
	if (ourType == 1) {
acd79baf sago007 2015-11-22 19:37 127
		filename = filename1;
acd79baf sago007 2015-11-22 19:37 128
	}
acd79baf sago007 2015-11-22 19:37 129
	if (ourType == 2) {
acd79baf sago007 2015-11-22 19:37 130
		filename = filename2;
acd79baf sago007 2015-11-22 19:37 131
	}
c53e6443 sago007 2012-04-17 11:04 132
c53e6443 sago007 2012-04-17 11:04 133
	ofstream outfile;
c53e6443 sago007 2012-04-17 11:04 134
	outfile.open(Highscore::filename.c_str(), ios::binary |ios::trunc);
c53e6443 sago007 2012-04-17 11:04 135
	if (!outfile)
c53e6443 sago007 2012-04-17 11:04 136
	{
c53e6443 sago007 2012-04-17 11:04 137
		cout << "Error writing to file: " << filename << endl;
c53e6443 sago007 2012-04-17 11:04 138
		exit(1);
c53e6443 sago007 2012-04-17 11:04 139
	}
c53e6443 sago007 2012-04-17 11:04 140
	for (int i = 0; i<top; i++)
c53e6443 sago007 2012-04-17 11:04 141
	{
c53e6443 sago007 2012-04-17 11:04 142
		outfile.write(tabel[i].name,30*sizeof(char));
c53e6443 sago007 2012-04-17 11:04 143
		outfile.write(reinterpret_cast<char*>(&tabel[i].score),sizeof(int));
c53e6443 sago007 2012-04-17 11:04 144
	}
c53e6443 sago007 2012-04-17 11:04 145
	outfile.close();
89c4a3a6 sago007 2008-08-29 14:32 146
}
89c4a3a6 sago007 2008-08-29 14:32 147
89c4a3a6 sago007 2008-08-29 14:32 148
bool Highscore::isHighScore(int newScore)
89c4a3a6 sago007 2008-08-29 14:32 149
{
acd79baf sago007 2015-11-22 19:37 150
	if (newScore>tabel[top-1].score) {
c53e6443 sago007 2012-04-17 11:04 151
		return true;
acd79baf sago007 2015-11-22 19:37 152
	}
acd79baf sago007 2015-11-22 19:37 153
	else {
c53e6443 sago007 2012-04-17 11:04 154
		return false;
acd79baf sago007 2015-11-22 19:37 155
	}
89c4a3a6 sago007 2008-08-29 14:32 156
}
89c4a3a6 sago007 2008-08-29 14:32 157
8f632c05 sago007 2015-08-22 16:21 158
void Highscore::addScore(const string& newName, int newScore)
89c4a3a6 sago007 2008-08-29 14:32 159
{
c53e6443 sago007 2012-04-17 11:04 160
	int ranking = top-1;
acd79baf sago007 2015-11-22 19:37 161
	while ((tabel[ranking-1].score<newScore) && (ranking != 0)) {
c53e6443 sago007 2012-04-17 11:04 162
		ranking--;
acd79baf sago007 2015-11-22 19:37 163
	}
c53e6443 sago007 2012-04-17 11:04 164
	for (int i=top-1; i>ranking; i--)
c53e6443 sago007 2012-04-17 11:04 165
	{
c53e6443 sago007 2012-04-17 11:04 166
		tabel[i].score = tabel[i-1].score;
c53e6443 sago007 2012-04-17 11:04 167
		strcpy(tabel[i].name,"                            \0");
c53e6443 sago007 2012-04-17 11:04 168
		strcpy(tabel[i].name,tabel[i-1].name);
c53e6443 sago007 2012-04-17 11:04 169
	}
c53e6443 sago007 2012-04-17 11:04 170
	tabel[ranking].score = newScore;
c53e6443 sago007 2012-04-17 11:04 171
	strcpy(tabel[ranking].name,"                            \0");
8f632c05 sago007 2015-08-22 16:21 172
	snprintf(tabel[ranking].name, sizeof(tabel[ranking].name), "%s", newName.c_str());
c53e6443 sago007 2012-04-17 11:04 173
	Highscore::writeFile();
89c4a3a6 sago007 2008-08-29 14:32 174
}
89c4a3a6 sago007 2008-08-29 14:32 175
89c4a3a6 sago007 2008-08-29 14:32 176
int Highscore::getScoreNumber(int room)
89c4a3a6 sago007 2008-08-29 14:32 177
{
c53e6443 sago007 2012-04-17 11:04 178
	return tabel[room].score;
89c4a3a6 sago007 2008-08-29 14:32 179
}
89c4a3a6 sago007 2008-08-29 14:32 180
89c4a3a6 sago007 2008-08-29 14:32 181
char* Highscore::getScoreName(int room)
89c4a3a6 sago007 2008-08-29 14:32 182
{
c53e6443 sago007 2012-04-17 11:04 183
	return &tabel[room].name[0];
89c4a3a6 sago007 2008-08-29 14:32 184
}
1970-01-01 00:00 185