2007-05-27 12:01:53 -04:00
|
|
|
// $Id: highscores.hpp 921 2007-02-28 05:43:34Z hiker $
|
|
|
|
//
|
|
|
|
// SuperTuxKart - a fun racing game with go-kart
|
|
|
|
// Copyright (C) 2006 Joerg Henrichs
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License
|
2008-06-12 20:53:52 -04:00
|
|
|
// as published by the Free Software Foundation; either version 3
|
2007-05-27 12:01:53 -04:00
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
#include "highscores.hpp"
|
1) Removed race_setup and race_mode data structures. All this
information is now only managed by the race_manager, no
more in-between objects to transfer information along.
2) The scores for grand prix are now defined in the stk_config.dat
file (10, 8, 6, 5, 4, .., 1, 0, 0) points
3) Bugfix: unlock information wasn't saved anymore. Added specific
saving after unlocking, plus re-inserted the 'generic' save
at the end of STK again.
4) bugfix/work around: Visual Studio complains about incompatible
iterators in sdldrv - apparently caused by using erase, and
then keep on using the iterator.
5) Fixed bug when running a race in a GP again (scores/times
were added each time).
git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/trunk/supertuxkart@1681 178a84e3-b1eb-0310-8ba1-8eac791a3b58
2008-04-09 09:52:48 -04:00
|
|
|
|
2009-01-27 19:38:28 -05:00
|
|
|
#include <stdexcept>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
#include "race_manager.hpp"
|
2008-09-22 21:16:50 -04:00
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
HighscoreEntry::HighscoreEntry(const HighscoreEntry::HighscoreType highscore_type,
|
|
|
|
int num_karts, const RaceManager::Difficulty difficulty,
|
|
|
|
const std::string trackName, const int number_of_laps)
|
|
|
|
{
|
|
|
|
m_track = trackName;
|
|
|
|
m_highscore_type = highscore_type;
|
|
|
|
m_number_of_karts = num_karts;
|
|
|
|
m_difficulty = difficulty;
|
|
|
|
m_number_of_laps = number_of_laps;
|
|
|
|
|
|
|
|
for(int i=0; i<HIGHSCORE_LEN; i++)
|
|
|
|
{
|
|
|
|
m_name[i] = "";
|
|
|
|
m_kart_name[i] = "";
|
|
|
|
m_time[i] = -9.9f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
HighscoreEntry::HighscoreEntry(const lisp::Lisp* const node)
|
2007-05-27 12:01:53 -04:00
|
|
|
{
|
|
|
|
m_track = "";
|
2008-09-22 21:16:50 -04:00
|
|
|
m_highscore_type = "HST_UNDEFINED";
|
|
|
|
m_number_of_karts = -1;
|
2007-05-27 12:01:53 -04:00
|
|
|
m_difficulty = -1;
|
|
|
|
m_number_of_laps = -1;
|
2008-09-22 21:16:50 -04:00
|
|
|
|
2007-05-27 12:01:53 -04:00
|
|
|
for(int i=0; i<HIGHSCORE_LEN; i++)
|
|
|
|
{
|
|
|
|
m_name[i] = "";
|
|
|
|
m_kart_name[i] = "";
|
|
|
|
m_time[i] = -9.9f;
|
|
|
|
}
|
2008-09-22 21:16:50 -04:00
|
|
|
|
|
|
|
Read(node);
|
|
|
|
}
|
2007-05-27 12:01:53 -04:00
|
|
|
// -----------------------------------------------------------------------------
|
2008-09-22 21:16:50 -04:00
|
|
|
void HighscoreEntry::Read(const lisp::Lisp* const node)
|
2007-05-27 12:01:53 -04:00
|
|
|
{
|
|
|
|
node->get("track-name", m_track );
|
|
|
|
node->get("number-karts", m_number_of_karts );
|
2008-09-22 21:16:50 -04:00
|
|
|
std::string hst="HST_UNDEFINED";
|
|
|
|
node->get("hscore-type", hst );
|
2008-09-19 10:43:25 -04:00
|
|
|
m_highscore_type = (HighscoreType)hst;
|
2007-05-27 12:01:53 -04:00
|
|
|
node->get("difficulty", m_difficulty );
|
|
|
|
node->get("number-of-laps", m_number_of_laps );
|
|
|
|
|
|
|
|
for(int i=0; i<HIGHSCORE_LEN; i++)
|
|
|
|
{
|
2009-01-27 19:38:28 -05:00
|
|
|
std::ostringstream s;
|
|
|
|
s << "time-" << i;
|
|
|
|
node->get(s.str(),m_time[i] );
|
2009-01-27 23:12:16 -05:00
|
|
|
s.str(""); s << "name-" << i;
|
2009-01-27 19:38:28 -05:00
|
|
|
node->get(s.str(),m_name[i] );
|
2009-01-27 23:12:16 -05:00
|
|
|
s.str(""); s << "kartname-" << i;
|
2009-01-27 19:38:28 -05:00
|
|
|
node->get(s.str(), m_kart_name[i] );
|
2007-05-27 12:01:53 -04:00
|
|
|
}
|
|
|
|
} // Read
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
2008-09-22 21:16:50 -04:00
|
|
|
void HighscoreEntry::Write(lisp::Writer *writer)
|
2007-05-27 12:01:53 -04:00
|
|
|
{
|
|
|
|
writer->write("track-name\t", m_track );
|
|
|
|
writer->write("number-karts\t", m_number_of_karts );
|
|
|
|
writer->write("difficulty\t\t", m_difficulty );
|
2009-01-27 19:38:28 -05:00
|
|
|
writer->write("hscore-type\t\t", m_highscore_type );
|
2007-05-27 12:01:53 -04:00
|
|
|
writer->write("number-of-laps\t", m_number_of_laps );
|
|
|
|
for(int j=0; j<HIGHSCORE_LEN; j++)
|
|
|
|
{
|
2009-01-27 19:38:28 -05:00
|
|
|
std::ostringstream s;
|
|
|
|
s << "time-" << j << "\t\t";
|
|
|
|
writer->write(s.str(), m_time[j] );
|
2009-01-27 23:12:16 -05:00
|
|
|
s.str(""); s << "name-" << j << "\t\t";
|
2009-01-27 19:38:28 -05:00
|
|
|
writer->write(s.str(), m_name[j] );
|
2009-01-27 23:12:16 -05:00
|
|
|
s.str(""); s << "kartname-" << j << "\t\t";
|
2009-01-27 19:38:28 -05:00
|
|
|
writer->write(s.str(), m_kart_name[j] );
|
2007-05-27 12:01:53 -04:00
|
|
|
} // for j
|
|
|
|
|
|
|
|
} // Write
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
2008-09-22 21:16:50 -04:00
|
|
|
int HighscoreEntry::matches(HighscoreType highscore_type,
|
|
|
|
int num_karts, RaceManager::Difficulty difficulty,
|
|
|
|
const std::string track, const int number_of_laps)
|
2007-05-27 12:01:53 -04:00
|
|
|
{
|
|
|
|
return (m_highscore_type == highscore_type &&
|
|
|
|
m_track == track &&
|
|
|
|
m_difficulty == difficulty &&
|
|
|
|
m_number_of_laps == number_of_laps &&
|
|
|
|
m_number_of_karts == num_karts );
|
|
|
|
} // matches
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
/** Inserts the data into the highscore list.
|
|
|
|
* If the new entry is fast enough to
|
|
|
|
* be in the highscore list, the new position (1-HIGHSCORE_LEN) is returned,
|
|
|
|
* otherwise a 0.
|
|
|
|
*/
|
2008-09-22 21:16:50 -04:00
|
|
|
int HighscoreEntry::addData(const std::string& kart_name,
|
|
|
|
const std::string& name, const float time)
|
2007-05-27 12:01:53 -04:00
|
|
|
{
|
|
|
|
int position=-1;
|
|
|
|
for(int i=0; i<HIGHSCORE_LEN; i++)
|
|
|
|
{
|
|
|
|
// Check for unused entry. If so, just insert the new record
|
|
|
|
if(m_time[i]<0.0f)
|
|
|
|
{
|
|
|
|
position=i;
|
|
|
|
break;
|
|
|
|
}
|
2008-09-22 21:16:50 -04:00
|
|
|
// Check if new entry is faster than than in slot 'i', if so
|
2007-05-27 12:01:53 -04:00
|
|
|
// move times etc and insert new entry
|
|
|
|
if(time < m_time[i])
|
|
|
|
{
|
|
|
|
for(int j=HIGHSCORE_LEN-2;j>=i;j--)
|
|
|
|
{
|
|
|
|
m_name[j+1] = m_name[j];
|
|
|
|
m_kart_name[j+1] = m_kart_name[j];
|
|
|
|
m_time[j+1] = m_time[j];
|
|
|
|
}
|
|
|
|
position = i;
|
|
|
|
break;
|
|
|
|
}
|
2008-09-22 21:16:50 -04:00
|
|
|
}//next score slot
|
2007-05-27 12:01:53 -04:00
|
|
|
|
|
|
|
if(position>=0)
|
|
|
|
{
|
1) Removed race_setup and race_mode data structures. All this
information is now only managed by the race_manager, no
more in-between objects to transfer information along.
2) The scores for grand prix are now defined in the stk_config.dat
file (10, 8, 6, 5, 4, .., 1, 0, 0) points
3) Bugfix: unlock information wasn't saved anymore. Added specific
saving after unlocking, plus re-inserted the 'generic' save
at the end of STK again.
4) bugfix/work around: Visual Studio complains about incompatible
iterators in sdldrv - apparently caused by using erase, and
then keep on using the iterator.
5) Fixed bug when running a race in a GP again (scores/times
were added each time).
git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/trunk/supertuxkart@1681 178a84e3-b1eb-0310-8ba1-8eac791a3b58
2008-04-09 09:52:48 -04:00
|
|
|
m_track = race_manager->getTrackName();
|
|
|
|
m_number_of_karts = race_manager->getNumKarts();
|
|
|
|
m_difficulty = race_manager->getDifficulty();
|
|
|
|
m_number_of_laps = race_manager->getNumLaps();
|
2007-05-27 12:01:53 -04:00
|
|
|
m_name[position] = name;
|
|
|
|
m_time[position] = time;
|
|
|
|
m_kart_name[position] = kart_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return position+1;
|
|
|
|
|
|
|
|
} // addData
|
|
|
|
// -----------------------------------------------------------------------------
|
2008-09-22 21:16:50 -04:00
|
|
|
int HighscoreEntry::getNumberEntries() const
|
2007-05-27 12:01:53 -04:00
|
|
|
{
|
|
|
|
for(int i=HIGHSCORE_LEN-1; i>=0; i--)
|
|
|
|
{
|
|
|
|
if(m_time[i]>0) return i+1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
} // getNumberEntries
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
2008-09-22 21:16:50 -04:00
|
|
|
void HighscoreEntry::getEntry(int number, std::string &kart_name,
|
2007-05-27 12:01:53 -04:00
|
|
|
std::string &name, float *const time) const
|
|
|
|
{
|
|
|
|
if(number<0 || number>getNumberEntries())
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error, accessing undefined highscore entry:\n");
|
|
|
|
fprintf(stderr,"number %d, but %d entries are defined\n",number,
|
|
|
|
getNumberEntries());
|
2007-11-20 19:31:20 -05:00
|
|
|
fprintf(stderr, "This error can be ignored, but no highscores are available\n");
|
2008-10-24 09:49:56 -04:00
|
|
|
return;
|
2007-05-27 12:01:53 -04:00
|
|
|
}
|
|
|
|
kart_name = m_kart_name[number];
|
|
|
|
name = m_name[number];
|
|
|
|
*time = m_time[number];
|
|
|
|
|
|
|
|
} // getEntry
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|