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