1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
// Description:
// Score keeper.
//
// Copyright (C) 2001 Frank Becker
//
// This program is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation; either version 2 of the License, or (at your option) any later
// version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details
//
#include <iostream>
#include <string>
#include <algorithm>
#include <Trace.hpp>
#include <Config.hpp>
#include <Tokenizer.hpp>
#include <zStream.hpp>
#include <ScoreKeeper.hpp>
#include <GameState.hpp>
#include <GLBitmapFont.hpp>
#include <FontManager.hpp>
#include <ParticleGroup.hpp>
#include <ParticleGroupManager.hpp>
using namespace std;
const int LEADERBOARD_SIZE = 11; //top-10 plus current score
ScoreKeeper::ScoreKeeper( void):
_currentIndex( LEADERBOARD_SIZE-1),
_leaderBoard( LEADERBOARD_SIZE)
{
XTRACE();
for( int i=0; i<LEADERBOARD_SIZE; i++)
{
_leaderBoard[ i].score = 1000*(10-i);
switch( i % 4)
{
case 0:
_leaderBoard[ i].name = "Critical-Mass";
break;
case 1:
_leaderBoard[ i].name = "Frank";
break;
case 2:
_leaderBoard[ i].name = "ff<Doh!>";
break;
default:
_leaderBoard[ i].name = "SliQ";
break;
}
time( & _leaderBoard[ i].time);
}
}
ScoreKeeper::~ScoreKeeper( void)
{
XTRACE();
}
void ScoreKeeper::resetCurrentScore( void)
{
_currentIndex = LEADERBOARD_SIZE-1;
_leaderBoard[ _currentIndex].score = 0;
_leaderBoard[ _currentIndex].name = "???";
_leaderBoard[ _currentIndex].time = 0;
}
int ScoreKeeper::addToCurrentScore( int value)
{
XTRACE();
float multiplier = 1.0f;
switch( GameState::skill)
{
case Skill::eRookie:
multiplier = 0.5f;
break;
case Skill::eNormal:
multiplier = 1.0f;
break;
case Skill::eExpert:
multiplier = 1.5f;
break;
case Skill::eInsane:
multiplier = 3.0f;
break;
case Skill::eUnknown:
case Skill::eLAST:
default:
break;
}
int newValue = (int) ((float)value * multiplier);
_leaderBoard[ _currentIndex].score += newValue;
time( & _leaderBoard[ _currentIndex].time);
updateLeaderBoard();
//return the real value;
return newValue;
}
void ScoreKeeper::updateLeaderBoard( void)
{
XTRACE();
if( _currentIndex == 0) return; //we are leader, no update required
bool newPos = false;
ScoreData tmpData = _leaderBoard[ _currentIndex];
while( (_currentIndex>0) &&
(tmpData.score > _leaderBoard[ _currentIndex-1].score))
{
_leaderBoard[ _currentIndex] = _leaderBoard[ _currentIndex-1];
_currentIndex--;
newPos = true;
}
_leaderBoard[ _currentIndex] = tmpData;
if( newPos)
{
static ParticleGroup *effects =
ParticleGroupManagerS::instance()->getParticleGroup(EFFECTS_GROUP2);
ParticleInfo pi;
pi.position.x = 0.0;
pi.position.y =-20+3.5*(_currentIndex+1);
pi.position.z =-50.0;
char buf[128];
sprintf( buf, "You've climbed to #%d on the leader board!",
_currentIndex+1) ;
pi.text = buf;
pi.color.x = 0.2;
pi.color.y = 1.0;
pi.color.z = 0.2;
pi.extra.y = 0.05;
pi.extra.z = 0.05;
effects->newParticle( "StatusMessage", pi);
}
// dump();
}
void ScoreKeeper::dump( void)
{
LOG_INFO << "------LeaderBoard-----" << endl;
for( unsigned int i=0; i<_leaderBoard.size(); i++)
{
LOG_INFO.width(30);
LOG_VOID.fill('.');
LOG_VOID.setf( ios::left);
LOG_VOID << _leaderBoard[ i].name.c_str();
LOG_VOID.width(10);
LOG_VOID.setf( ios::right);
LOG_VOID << _leaderBoard[ i].score;
char buf[128];
strftime( buf, 127,"%a %d-%b-%Y %H:%M", localtime(&_leaderBoard[i].time));
LOG_VOID << " : " << buf << endl;
}
}
const string ScoreKeeper::getInfoText( unsigned int index)
{
string info = "";
if( index < _leaderBoard.size())
{
info = _leaderBoard[ index].name;
info += " made this run on ";
char buf[128];
strftime( buf, 127,"%a %d-%b-%Y %H:%M",
localtime(&_leaderBoard[index].time));
info += buf;
}
return info;
}
void ScoreKeeper::load( void)
{
XTRACE();
string scoreFilename = ConfigS::instance()->getConfigFileName();
scoreFilename += "-scores";
LOG_INFO << "Loading hi-scores from " << scoreFilename << endl;
int version = 1;
ifstream infile( scoreFilename.c_str(),
ios::in | ios::binary);
if( infile.good())
{
_leaderBoard.clear();
ziStream zin( infile);
string line;
while( !getline( zin, line).eof())
{
// LOG_INFO << "[" << line << "]" << endl;
//explicitly skip comments
if( line[0] == '#') continue;
ScoreData sData;
Tokenizer t( line, " \t\n\r");
string token = t.next();
if( token == "Version")
{
string versionString = t.next();
version = atoi( versionString.c_str());
}
else
{
switch( version)
{
case 1:
sData.name = token;
sData.score = atoi( t.next().c_str());
sData.time = (time_t) atoi( t.next().c_str());
_leaderBoard.push_back( sData);
break;
default:
break;
}
}
}
}
//should already be sorted, but better be safe...
sort( _leaderBoard.begin(), _leaderBoard.end());
dump();
}
void ScoreKeeper::save( void)
{
XTRACE();
string scoreFilename = ConfigS::instance()->getConfigFileName();
scoreFilename += "-scores";
LOG_INFO << "Saving hi-scores to " << scoreFilename << endl;
//Save scores in a compressed file to make it a bit tougher to cheat...
ofstream outfile( scoreFilename.c_str(),
ios::out | ios::binary);
if( outfile.good())
{
zoStream zout( outfile);
zout << "#------LeaderBoard-----#\n";
zout << "Version 1\n";
for( unsigned int i=0; i<_leaderBoard.size(); i++)
{
if( _leaderBoard[ i].name == "")
{
_leaderBoard[ i].name = "Anonymous";
}
zout << _leaderBoard[ i].name << " "
<< _leaderBoard[ i].score << " "
<< (long) _leaderBoard[ i].time << "\n";
}
}
}
void ScoreKeeper::draw( void)
{
static GLBitmapFont *fontWhite =
FontManagerS::instance()->getFont( "bitmaps/menuWhite");
static GLBitmapFont *fontShadow =
FontManagerS::instance()->getFont( "bitmaps/menuShadow");
for( unsigned int i=0; i<(_leaderBoard.size()-1); i++)
{
char buf[128];
sprintf( buf, "%d", _leaderBoard[ i].score);
char buf2[10];
sprintf( buf2, "%d.", i+1);
float width = fontWhite->GetWidth( buf, 1.0);
glColor4f(1.0, 1.0, 1.0, 1.0);
fontShadow->DrawString(
buf2,
250+7, 420-i*36-7, 1.0, 1.0);
fontShadow->DrawString(
_leaderBoard[ i].name.c_str(),
300+7, 420-i*36-7, 1.0, 1.0);
fontShadow->DrawString(
buf,
750-width+9, 420-i*36-9, 1.0, 1.0);
if( i == _currentIndex)
glColor4f(1.0, 0.852, 0.0, 1.0);
else
glColor4f(1.0, 1.0, 1.0, 1.0);
fontWhite->DrawString(
buf2,
250, 420-i*36, 1.0, 1.0);
fontWhite->DrawString(
_leaderBoard[ i].name.c_str(),
300, 420-i*36, 1.0, 1.0);
fontWhite->DrawString(
buf,
750-width, 420-i*36, 1.0, 1.0);
}
}
|