Back to index

supertuxkart  0.5+dfsg1
highscores.cpp
Go to the documentation of this file.
00001  //  $Id: highscores.hpp 921 2007-02-28 05:43:34Z hiker $
00002 //
00003 //  SuperTuxKart - a fun racing game with go-kart
00004 //  Copyright (C) 2006 Joerg Henrichs
00005 //
00006 //  This program is free software; you can redistribute it and/or
00007 //  modify it under the terms of the GNU General Public License
00008 //  as published by the Free Software Foundation; either version 2
00009 //  of the License, or (at your option) any later version.
00010 //
00011 //  This program is distributed in the hope that it will be useful,
00012 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //  GNU General Public License for more details.
00015 //
00016 //  You should have received a copy of the GNU General Public License
00017 //  along with this program; if not, write to the Free Software
00018 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 
00020 #include <stdexcept>
00021 #include "highscores.hpp"
00022 #include "race_manager.hpp"
00023 
00024 #if defined(WIN32) && !defined(__CYGWIN__)
00025 #  define snprintf _snprintf
00026 #endif
00027 Highscores::Highscores()
00028 {
00029     m_track           = ""; 
00030     m_highscore_type  = HST_UNDEFINED;
00031     m_number_of_karts =-1;
00032     m_difficulty      = -1;
00033     m_number_of_laps  = -1;
00034     for(int i=0; i<HIGHSCORE_LEN; i++) 
00035     {
00036         m_name[i]      = "";
00037         m_kart_name[i] = "";
00038         m_time[i]      = -9.9f;
00039     }
00040 }   // Highscores
00041 
00042 // -----------------------------------------------------------------------------
00043 void Highscores::Read(const lisp::Lisp* const node)
00044 {
00045     node->get("track-name",     m_track               );
00046     node->get("number-karts",   m_number_of_karts     );
00047     node->get("race-mode",      (int&)m_highscore_type);
00048     node->get("difficulty",     m_difficulty          );
00049     node->get("number-of-laps", m_number_of_laps      );
00050 
00051     for(int i=0; i<HIGHSCORE_LEN; i++) 
00052     {
00053         char s[128];
00054         snprintf(s, sizeof(s), "time-%d",     i);
00055         node->get(s,m_time[i]                  );
00056         snprintf(s, sizeof(s), "name-%d",     i);
00057         node->get(s,m_name[i]                  );
00058         snprintf(s, sizeof(s), "kartname-%d", i);
00059         node->get(s, m_kart_name[i]            );
00060     }
00061 }   // Read
00062 
00063 // -----------------------------------------------------------------------------
00064 void Highscores::Write(lisp::Writer *writer)
00065 {
00066     writer->write("track-name\t",     m_track            );
00067     writer->write("number-karts\t",   m_number_of_karts  );
00068     writer->write("difficulty\t\t",   m_difficulty       );
00069     writer->write("race-mode\t\t",    m_highscore_type   );
00070     writer->write("number-of-laps\t", m_number_of_laps   );
00071     for(int j=0; j<HIGHSCORE_LEN; j++) 
00072     {
00073         char s[128];
00074         snprintf(s, sizeof(s), "time-%d\t\t",     j);
00075         writer->write(s, m_time[j]                 );
00076         snprintf(s, sizeof(s), "name-%d\t\t",     j);
00077         writer->write(s, m_name[j]                 );
00078         snprintf(s, sizeof(s), "kartname-%d\t\t", j);
00079         writer->write(s, m_kart_name[j]            );
00080     }   // for j
00081     
00082 }   // Write
00083 
00084 // -----------------------------------------------------------------------------
00085 int Highscores::matches(HighscoreType highscore_type,
00086                         int num_karts, RaceManager::Difficulty difficulty,
00087                         const std::string &track, const int number_of_laps)
00088 {
00089     return (m_highscore_type  == highscore_type   &&
00090             m_track           == track            &&
00091             m_difficulty      == difficulty       &&
00092             m_number_of_laps  == number_of_laps   &&
00093             m_number_of_karts == num_karts          );
00094 }   // matches
00095 // -----------------------------------------------------------------------------
00101 int Highscores::addData(const HighscoreType highscore_type, const std::string kart_name,
00102                         const std::string name, const float time)
00103 {
00104     int position=-1;
00105     for(int i=0; i<HIGHSCORE_LEN; i++)
00106     {
00107         // Check for unused entry. If so, just insert the new record
00108         if(m_time[i]<0.0f)
00109         {
00110             position=i;
00111             break;
00112         }
00113         // Check if new entry is faster than previous entry, if so
00114         // move times etc and insert new entry
00115         if(time < m_time[i])
00116         {
00117             for(int j=HIGHSCORE_LEN-2;j>=i;j--)
00118             {
00119                 m_name[j+1]      = m_name[j];
00120                 m_kart_name[j+1] = m_kart_name[j];
00121                 m_time[j+1]      = m_time[j];
00122             }
00123             position = i;
00124             break;
00125         }
00126     }
00127 
00128     if(position>=0) 
00129     {
00130         m_track               = race_manager->getTrackName();
00131         m_highscore_type      = highscore_type;
00132         m_number_of_karts     = race_manager->getNumKarts();
00133         m_difficulty          = race_manager->getDifficulty();
00134         m_number_of_laps      = race_manager->getNumLaps();
00135         m_name[position]      = name;
00136         m_time[position]      = time;
00137         m_kart_name[position] = kart_name;
00138     }
00139 
00140     return position+1;
00141 
00142 }   // addData
00143 // -----------------------------------------------------------------------------
00144 int Highscores::getNumberEntries() const
00145 {
00146     for(int i=HIGHSCORE_LEN-1; i>=0; i--)
00147     {
00148         if(m_time[i]>0) return i+1;
00149     }
00150     return 0;
00151 }   // getNumberEntries
00152 
00153 // -----------------------------------------------------------------------------
00154 void Highscores::getEntry(int number, std::string &kart_name,
00155                           std::string &name, float *const time) const
00156 {
00157     if(number<0 || number>getNumberEntries())
00158     {
00159         fprintf(stderr, "Error, accessing undefined highscore entry:\n");
00160         fprintf(stderr,"number %d, but %d entries are defined\n",number,
00161                 getNumberEntries());
00162         fprintf(stderr, "This error can be ignored, but no highscores are available\n");
00163     }
00164     kart_name = m_kart_name[number];
00165     name      = m_name[number];
00166     *time     = m_time[number];
00167     
00168 }   // getEntry
00169 
00170 // -----------------------------------------------------------------------------