2007-05-27 12:01:53 -04:00
|
|
|
//
|
|
|
|
// SuperTuxKart - a fun racing game with go-kart
|
2015-03-29 20:31:42 -04:00
|
|
|
// Copyright (C) 2006-2015 Joerg Henrichs
|
2007-05-27 12:01:53 -04:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2009-06-02 21:36:48 -04:00
|
|
|
#include "race/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
|
|
|
|
2014-02-04 16:22:38 -05:00
|
|
|
#include "io/utf_writer.hpp"
|
2010-03-25 18:29:47 -04:00
|
|
|
#include "io/xml_node.hpp"
|
2009-06-02 21:36:48 -04:00
|
|
|
#include "race/race_manager.hpp"
|
2008-09-22 21:16:50 -04:00
|
|
|
|
2014-02-04 16:22:38 -05:00
|
|
|
#include <stdexcept>
|
|
|
|
#include <fstream>
|
|
|
|
|
2008-09-22 21:16:50 -04:00
|
|
|
// -----------------------------------------------------------------------------
|
2014-11-30 23:14:27 -05:00
|
|
|
Highscores::Highscores(const HighscoreType &highscore_type,
|
2013-05-29 18:04:35 -04:00
|
|
|
int num_karts,
|
2014-11-30 23:14:27 -05:00
|
|
|
const RaceManager::Difficulty &difficulty,
|
|
|
|
const std::string &track_name,
|
2012-03-14 18:37:56 -04:00
|
|
|
const int number_of_laps,
|
|
|
|
const bool reverse)
|
2008-09-22 21:16:50 -04:00
|
|
|
{
|
2014-11-30 23:14:27 -05:00
|
|
|
m_track = track_name;
|
2008-09-22 21:16:50 -04:00
|
|
|
m_highscore_type = highscore_type;
|
|
|
|
m_number_of_karts = num_karts;
|
|
|
|
m_difficulty = difficulty;
|
|
|
|
m_number_of_laps = number_of_laps;
|
2012-03-14 18:37:56 -04:00
|
|
|
m_reverse = reverse;
|
2013-05-29 18:04:35 -04:00
|
|
|
|
|
|
|
for(int i=0; i<HIGHSCORE_LEN; i++)
|
2008-09-22 21:16:50 -04:00
|
|
|
{
|
|
|
|
m_name[i] = "";
|
|
|
|
m_kart_name[i] = "";
|
|
|
|
m_time[i] = -9.9f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// -----------------------------------------------------------------------------
|
2010-03-25 18:29:47 -04:00
|
|
|
Highscores::Highscores(const XMLNode &node)
|
2007-05-27 12:01:53 -04:00
|
|
|
{
|
2013-05-29 18:04:35 -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;
|
2012-03-14 18:37:56 -04:00
|
|
|
m_reverse = false;
|
2013-05-29 18:04:35 -04:00
|
|
|
|
|
|
|
for(int i=0; i<HIGHSCORE_LEN; i++)
|
2007-05-27 12:01:53 -04:00
|
|
|
{
|
|
|
|
m_name[i] = "";
|
|
|
|
m_kart_name[i] = "";
|
|
|
|
m_time[i] = -9.9f;
|
|
|
|
}
|
2013-05-29 18:04:35 -04:00
|
|
|
|
2010-03-25 18:29:47 -04:00
|
|
|
readEntry(node);
|
|
|
|
} // Highscores
|
|
|
|
|
2007-05-27 12:01:53 -04:00
|
|
|
// -----------------------------------------------------------------------------
|
2010-03-25 18:29:47 -04:00
|
|
|
void Highscores::readEntry(const XMLNode &node)
|
2007-05-27 12:01:53 -04:00
|
|
|
{
|
2010-03-25 18:29:47 -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";
|
2010-03-25 18:29:47 -04:00
|
|
|
node.get("hscore-type", &hst );
|
2008-09-19 10:43:25 -04:00
|
|
|
m_highscore_type = (HighscoreType)hst;
|
2010-03-25 18:29:47 -04:00
|
|
|
node.get("difficulty", &m_difficulty );
|
|
|
|
node.get("number-of-laps", &m_number_of_laps );
|
2012-03-14 18:37:56 -04:00
|
|
|
node.get("reverse", &m_reverse );
|
2007-05-27 12:01:53 -04:00
|
|
|
|
2013-05-29 18:04:35 -04:00
|
|
|
for(unsigned int i=0; i<node.getNumNodes(); i++)
|
2007-05-27 12:01:53 -04:00
|
|
|
{
|
2010-03-25 18:29:47 -04:00
|
|
|
const XMLNode *entry = node.getNode(i);
|
2010-06-12 19:24:15 -04:00
|
|
|
entry->get("time", &m_time[i] );
|
|
|
|
entry->get("name", &m_name[i] );
|
|
|
|
entry->get("kartname", &m_kart_name[i] );
|
2013-05-29 18:04:35 -04:00
|
|
|
|
2010-06-12 19:24:15 -04:00
|
|
|
// a non-empty entry needs a non-empty kart name.
|
2010-06-13 14:30:07 -04:00
|
|
|
if (!(m_time[i] <= 0.0f || m_kart_name[i].size() > 0))
|
|
|
|
{
|
|
|
|
throw std::logic_error("Invalid highscore entry : empty kart name");
|
|
|
|
}
|
|
|
|
if (!(m_time[i] <= 0.0f || m_name[i].size() > 0))
|
|
|
|
{
|
|
|
|
throw std::logic_error("Invalid highscore entry : empty kart name");
|
|
|
|
}
|
2007-05-27 12:01:53 -04:00
|
|
|
}
|
2010-03-25 18:29:47 -04:00
|
|
|
} // readEntry
|
2007-05-27 12:01:53 -04:00
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
2010-03-25 18:29:47 -04:00
|
|
|
/** Writes the highscores in this entry to the writer. It will only write
|
|
|
|
* anything if there is actually a highscore recored (i.e. time >=0). Empty
|
|
|
|
* entries are created e.g. when changing the number of laps in the GUI,
|
|
|
|
* resulting in empty entries here.
|
|
|
|
* \param writer The file stream to write the data to.
|
|
|
|
*/
|
2014-02-04 16:22:38 -05:00
|
|
|
void Highscores::writeEntry(UTFWriter &writer)
|
2007-05-27 12:01:53 -04:00
|
|
|
{
|
2010-03-25 18:29:47 -04:00
|
|
|
// Only
|
|
|
|
bool one_is_set = false;
|
|
|
|
for(unsigned int i=0; i<HIGHSCORE_LEN; i++)
|
|
|
|
one_is_set |= m_time[i]>=0;
|
|
|
|
if(!one_is_set) return;
|
|
|
|
|
2010-12-27 20:29:36 -05:00
|
|
|
writer << L" <highscore track-name =\"" << m_track.c_str() << "\"\n";
|
|
|
|
writer << L" number-karts =\"" << m_number_of_karts << "\"\n";
|
|
|
|
writer << L" difficulty =\"" << m_difficulty << "\"\n";
|
|
|
|
writer << L" hscore-type =\"" << m_highscore_type.c_str() << "\"\n";
|
2012-03-14 18:37:56 -04:00
|
|
|
writer << L" number-of-laps=\"" << m_number_of_laps << "\"\n";
|
|
|
|
writer << L" reverse =\"" << m_reverse << "\">\n";
|
2010-03-25 18:29:47 -04:00
|
|
|
|
2013-05-29 18:04:35 -04:00
|
|
|
for(int i=0; i<HIGHSCORE_LEN; i++)
|
2007-05-27 12:01:53 -04:00
|
|
|
{
|
2010-06-12 19:03:55 -04:00
|
|
|
if (m_time[i] > 0.0f)
|
|
|
|
{
|
|
|
|
assert(m_kart_name[i].size() > 0);
|
2010-12-27 20:29:36 -05:00
|
|
|
writer << L" <entry time =\"" << m_time[i] << L"\"\n";
|
|
|
|
writer << L" name =\"" << m_name[i] << L"\"\n";
|
|
|
|
writer << L" kartname=\"" << m_kart_name[i].c_str()
|
|
|
|
<< L"\"/>\n";
|
2010-06-12 19:03:55 -04:00
|
|
|
}
|
2010-03-25 18:29:47 -04:00
|
|
|
} // for i
|
2010-12-27 20:29:36 -05:00
|
|
|
writer << L" </highscore>\n";
|
2010-03-25 18:29:47 -04:00
|
|
|
} // writeEntry
|
2007-05-27 12:01:53 -04:00
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
2014-11-30 23:14:27 -05:00
|
|
|
int Highscores::matches(const HighscoreType &highscore_type,
|
|
|
|
int num_karts, const RaceManager::Difficulty &difficulty,
|
|
|
|
const std::string &track, const int number_of_laps,
|
|
|
|
const bool reverse)
|
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 &&
|
2012-03-14 18:37:56 -04:00
|
|
|
m_number_of_karts == num_karts &&
|
|
|
|
m_reverse == reverse );
|
2007-05-27 12:01:53 -04:00
|
|
|
} // matches
|
2010-03-25 18:29:47 -04:00
|
|
|
|
2007-05-27 12:01:53 -04:00
|
|
|
// -----------------------------------------------------------------------------
|
2013-05-29 18:04:35 -04:00
|
|
|
/** Inserts the data into the highscore list.
|
2007-05-27 12:01:53 -04:00
|
|
|
* If the new entry is fast enough to
|
|
|
|
* be in the highscore list, the new position (1-HIGHSCORE_LEN) is returned,
|
|
|
|
* otherwise a 0.
|
|
|
|
*/
|
2010-03-25 18:29:47 -04:00
|
|
|
int Highscores::addData(const std::string& kart_name,
|
2010-12-27 20:29:36 -05:00
|
|
|
const core::stringw& 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
|
|
|
|
2013-05-29 18:04:35 -04:00
|
|
|
if(position>=0)
|
2007-05-27 12:01:53 -04:00
|
|
|
{
|
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();
|
2010-02-10 06:40:33 -05:00
|
|
|
m_number_of_karts = race_manager->getNumberOfKarts();
|
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_difficulty = race_manager->getDifficulty();
|
|
|
|
m_number_of_laps = race_manager->getNumLaps();
|
2012-03-14 18:37:56 -04:00
|
|
|
m_reverse = race_manager->getReverseTrack();
|
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
|
2010-02-10 06:40:33 -05:00
|
|
|
|
2007-05-27 12:01:53 -04:00
|
|
|
// -----------------------------------------------------------------------------
|
2010-03-25 18:29:47 -04:00
|
|
|
int Highscores::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
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
2010-03-25 18:29:47 -04:00
|
|
|
void Highscores::getEntry(int number, std::string &kart_name,
|
2010-12-27 20:29:36 -05:00
|
|
|
core::stringw &name, float *const time) const
|
2007-05-27 12:01:53 -04:00
|
|
|
{
|
|
|
|
if(number<0 || number>getNumberEntries())
|
|
|
|
{
|
2014-07-19 14:43:19 -04:00
|
|
|
Log::warn("Highscores", "Accessing undefined highscore entry:");
|
|
|
|
Log::warn("Highscores", "Number %d, but %d entries are defined.", number,
|
|
|
|
getNumberEntries());
|
|
|
|
Log::warn("Highscores", "This error can be ignored, but no highscores are available.");
|
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];
|
2013-05-29 18:04:35 -04:00
|
|
|
|
2007-05-27 12:01:53 -04:00
|
|
|
} // getEntry
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|