Back to index

supertuxkart  0.5+dfsg1
kart_properties_manager.cpp
Go to the documentation of this file.
00001 //  $Id: kart_properties_manager.cpp 2111 2008-05-31 07:04:30Z cosmosninja $
00002 //
00003 //  SuperTuxKart - a fun racing game with go-kart
00004 //  Copyright (C) 2004-2006 Ingo Ruhnke <grumbel@gmx.de>
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 <algorithm>
00022 #include <ctime>
00023 #include "file_manager.hpp"
00024 #include "string_utils.hpp"
00025 #include "kart_properties_manager.hpp"
00026 #include "kart_properties.hpp"
00027 #include "translation.hpp"
00028 #if defined(WIN32) && !defined(__CYGWIN__)
00029 #  define snprintf _snprintf
00030 #endif
00031 
00032 KartPropertiesManager *kart_properties_manager=0;
00033 
00034 KartPropertiesManager::KartPropertiesManager()
00035 {}
00036 
00037 //-----------------------------------------------------------------------------
00038 KartPropertiesManager::~KartPropertiesManager()
00039 {
00040     for(KartPropertiesVector::iterator i  = m_karts_properties.begin();
00041         i != m_karts_properties.end(); ++i)
00042         delete *i;
00043 }   // ~KartPropertiesManager
00044 
00045 //-----------------------------------------------------------------------------
00046 void KartPropertiesManager::removeTextures()
00047 {
00048     for(KartPropertiesVector::iterator i  = m_karts_properties.begin();
00049         i != m_karts_properties.end(); ++i)
00050     {
00051         delete *i;
00052     }
00053     m_karts_properties.clear();
00054     callback_manager->clear(CB_KART);
00055 }   // removeTextures
00056 
00057 //-----------------------------------------------------------------------------
00058 void KartPropertiesManager::loadKartData(bool dont_load_models)
00059 {
00060     m_max_steer_angle = -1.0f;
00061     std::set<std::string> result;
00062     file_manager->listFiles(result, file_manager->getKartDir(), 
00063                             /*is_full_path*/ true);
00064 
00065     // Find out which characters are available and load them
00066     for(std::set<std::string>::iterator i = result.begin();
00067             i != result.end(); ++i)
00068     {
00069         std::string kart_file;
00070         try
00071         {
00072             kart_file = file_manager->getKartFile((*i)+".kart");
00073         }
00074         catch (std::exception& e)
00075         {
00076             (void)e;   // remove warning about unused variable
00077             continue;
00078         }
00079         FILE *f=fopen(kart_file.c_str(),"r");
00080         if(!f) continue;
00081         fclose(f);
00082         KartProperties* kp = new KartProperties();
00083         kp->load(kart_file, "tuxkart-kart", dont_load_models);
00084         m_karts_properties.push_back(kp);
00085         if(kp->getMaxSteerAngle() > m_max_steer_angle)
00086         {
00087             m_max_steer_angle = kp->getMaxSteerAngle();
00088         }
00089     }   // for i
00090 }   // loadKartData
00091 
00092 //-----------------------------------------------------------------------------
00093 const int KartPropertiesManager::getKartId(const std::string IDENT)
00094 {
00095     int j = 0;
00096     for(KartPropertiesVector::const_iterator i  = m_karts_properties.begin();
00097         i != m_karts_properties.end(); ++i)
00098     {
00099         if ((*i)->getIdent() == IDENT)
00100             return j;
00101         ++j;
00102     }
00103 
00104     char msg[MAX_ERROR_MESSAGE_LENGTH];
00105     snprintf(msg, sizeof(msg), "KartPropertiesManager: Couldn't find kart: '%s'",
00106              IDENT.c_str());
00107     throw std::runtime_error(msg);
00108 }   // getKartId
00109 
00110 //-----------------------------------------------------------------------------
00111 const KartProperties* KartPropertiesManager::getKart(const std::string IDENT)
00112 {
00113     for(KartPropertiesVector::const_iterator i  = m_karts_properties.begin();
00114         i != m_karts_properties.end(); ++i)
00115     {
00116         if ((*i)->getIdent() == IDENT)
00117             return *i;
00118     }
00119 
00120     return NULL;
00121 }   // getKart
00122 
00123 //-----------------------------------------------------------------------------
00124 const KartProperties* KartPropertiesManager::getKartById(int i)
00125 {
00126     if (i < 0 || i >= int(m_karts_properties.size()))
00127         return NULL;
00128 
00129     return m_karts_properties[i];
00130 }
00131 
00132 /*FIXME: the next function is unused, if it is not useful, it should be
00133   deleted.*/
00134 //-----------------------------------------------------------------------------
00135 std::vector<std::string> KartPropertiesManager::getRandomKarts(int len)
00136 {
00137     std::vector<std::string> all_karts;
00138 
00139     for(KartPropertiesVector::const_iterator i  = m_karts_properties.begin();
00140         i != m_karts_properties.end(); ++i)
00141     {
00142         all_karts.push_back((*i)->getIdent());
00143     }
00144 
00145     std::random_shuffle(all_karts.begin(), all_karts.end());
00146 
00147     all_karts.resize(len);
00148 
00149     return all_karts;
00150 }   // getRandomKart
00151 
00152 //-----------------------------------------------------------------------------
00153 void KartPropertiesManager::fillWithRandomKarts(std::vector<std::string>& vec)
00154 {
00155     std::vector<std::string> all_karts;
00156 
00157     for(KartPropertiesVector::const_iterator i = m_karts_properties.begin();
00158         i != m_karts_properties.end(); ++i)
00159         all_karts.push_back((*i)->getIdent());
00160 
00161     std::srand((unsigned int)std::time(0));
00162 
00163     std::random_shuffle(all_karts.begin(), all_karts.end());
00164 
00165     int new_kart = 0;
00166     for(int i = 0; i < int(vec.size()); ++i)
00167     {
00168         while(vec[i].empty())
00169         {
00170             if (std::find(vec.begin(), vec.end(), all_karts[new_kart]) == vec.end())
00171             { // Found a new kart, so use it
00172                 vec[i] = all_karts[new_kart];
00173             }
00174             else if (!(all_karts.size() >= vec.size()))
00175             { // We need to fill more karts than we have available, so don't care about dups
00176                 vec[i] = all_karts[new_kart];
00177             }
00178 
00179             new_kart += 1;
00180         }   // while
00181     }   // for i
00182 }
00183 
00184 /* EOF */