2008-09-07 09:51:44 -04:00
|
|
|
// $Id$
|
|
|
|
//
|
|
|
|
// SuperTuxKart - a fun racing game with go-kart
|
|
|
|
// Copyright (C) 2004-2006 Ingo Ruhnke <grumbel@gmx.de>
|
|
|
|
//
|
|
|
|
// 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 3
|
|
|
|
// 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 <stdexcept>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <ctime>
|
|
|
|
#include "file_manager.hpp"
|
|
|
|
#include "string_utils.hpp"
|
|
|
|
#include "kart_properties_manager.hpp"
|
|
|
|
#include "kart_properties.hpp"
|
|
|
|
#include "translation.hpp"
|
|
|
|
#include "user_config.hpp"
|
2008-09-07 09:58:37 -04:00
|
|
|
#include "unlock_manager.hpp"
|
|
|
|
|
2008-09-07 09:51:44 -04:00
|
|
|
#if defined(WIN32) && !defined(__CYGWIN__)
|
|
|
|
# define snprintf _snprintf
|
|
|
|
#endif
|
|
|
|
|
|
|
|
KartPropertiesManager *kart_properties_manager=0;
|
|
|
|
|
|
|
|
KartPropertiesManager::KartPropertiesManager()
|
|
|
|
{
|
|
|
|
m_all_groups.clear();
|
|
|
|
} // KartPropertiesManager
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
KartPropertiesManager::~KartPropertiesManager()
|
|
|
|
{
|
|
|
|
for(KartPropertiesVector::iterator i = m_karts_properties.begin();
|
|
|
|
i != m_karts_properties.end(); ++i)
|
|
|
|
delete *i;
|
|
|
|
} // ~KartPropertiesManager
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void KartPropertiesManager::removeTextures()
|
|
|
|
{
|
|
|
|
for(KartPropertiesVector::iterator i = m_karts_properties.begin();
|
|
|
|
i != m_karts_properties.end(); ++i)
|
|
|
|
{
|
|
|
|
delete *i;
|
|
|
|
}
|
|
|
|
m_karts_properties.clear();
|
|
|
|
callback_manager->clear(CB_KART);
|
|
|
|
} // removeTextures
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void KartPropertiesManager::loadKartData(bool dont_load_models)
|
|
|
|
{
|
|
|
|
m_max_steer_angle = -1.0f;
|
|
|
|
std::set<std::string> result;
|
|
|
|
file_manager->listFiles(result, file_manager->getKartDir(),
|
|
|
|
/*is_full_path*/ true);
|
|
|
|
|
|
|
|
// Find out which characters are available and load them
|
|
|
|
for(std::set<std::string>::iterator i = result.begin();
|
|
|
|
i != result.end(); ++i)
|
|
|
|
{
|
|
|
|
std::string kart_file;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
kart_file = file_manager->getKartFile((*i)+".kart");
|
|
|
|
}
|
|
|
|
catch (std::exception& e)
|
|
|
|
{
|
|
|
|
(void)e; // remove warning about unused variable
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
FILE *f=fopen(kart_file.c_str(),"r");
|
|
|
|
if(!f) continue;
|
|
|
|
fclose(f);
|
|
|
|
KartProperties* kp = new KartProperties();
|
|
|
|
kp->load(kart_file, "tuxkart-kart", dont_load_models);
|
|
|
|
m_karts_properties.push_back(kp);
|
2008-09-09 00:20:24 -04:00
|
|
|
m_kart_available.push_back(true);
|
2008-09-07 09:51:44 -04:00
|
|
|
if(kp->getMaxSteerAngle(0) > m_max_steer_angle)
|
|
|
|
{
|
|
|
|
m_max_steer_angle = kp->getMaxSteerAngle(0);
|
|
|
|
}
|
|
|
|
const std::vector<std::string>& groups=kp->getGroups();
|
|
|
|
for(unsigned int g=0; g<groups.size(); g++)
|
|
|
|
{
|
|
|
|
if(m_groups.find(groups[g])==m_groups.end())
|
|
|
|
{
|
|
|
|
m_all_groups.push_back(groups[g]);
|
|
|
|
}
|
|
|
|
m_groups[groups[g]].push_back(m_karts_properties.size()-1);
|
|
|
|
}
|
|
|
|
} // for i
|
|
|
|
} // loadKartData
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2008-09-09 00:20:24 -04:00
|
|
|
const int KartPropertiesManager::getKartId(const std::string &ident) const
|
2008-09-07 09:51:44 -04:00
|
|
|
{
|
|
|
|
for(KartPropertiesVector::const_iterator i = m_karts_properties.begin();
|
|
|
|
i != m_karts_properties.end(); ++i)
|
|
|
|
{
|
2008-09-09 00:20:24 -04:00
|
|
|
if ((*i)->getIdent() == ident)
|
|
|
|
return i-m_karts_properties.begin();
|
2008-09-07 09:51:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
char msg[MAX_ERROR_MESSAGE_LENGTH];
|
|
|
|
snprintf(msg, sizeof(msg), "KartPropertiesManager: Couldn't find kart: '%s'",
|
2008-09-09 00:20:24 -04:00
|
|
|
ident.c_str());
|
2008-09-07 09:51:44 -04:00
|
|
|
throw std::runtime_error(msg);
|
|
|
|
} // getKartId
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2008-09-09 00:20:24 -04:00
|
|
|
const KartProperties* KartPropertiesManager::getKart(const std::string &ident) const
|
2008-09-07 09:51:44 -04:00
|
|
|
{
|
|
|
|
for(KartPropertiesVector::const_iterator i = m_karts_properties.begin();
|
|
|
|
i != m_karts_properties.end(); ++i)
|
|
|
|
{
|
2008-09-09 00:20:24 -04:00
|
|
|
if ((*i)->getIdent() == ident)
|
2008-09-07 09:51:44 -04:00
|
|
|
return *i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
} // getKart
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
const KartProperties* KartPropertiesManager::getKartById(int i) const
|
|
|
|
{
|
|
|
|
if (i < 0 || i >= int(m_karts_properties.size()))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return m_karts_properties[i];
|
2008-09-09 00:20:24 -04:00
|
|
|
} // getKartById
|
2008-09-07 09:51:44 -04:00
|
|
|
|
2008-09-09 00:20:24 -04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
/** Returns a list of all available kart identifiers. */
|
|
|
|
std::vector<std::string> KartPropertiesManager::getAllAvailableKarts() const
|
|
|
|
{
|
|
|
|
std::vector<std::string> all;
|
|
|
|
for(KartPropertiesVector::const_iterator i = m_karts_properties.begin();
|
|
|
|
i != m_karts_properties.end(); ++i)
|
|
|
|
{
|
|
|
|
if(m_kart_available[i-m_karts_properties.begin()])
|
|
|
|
all.push_back((*i)->getIdent());
|
|
|
|
}
|
|
|
|
return all;
|
|
|
|
} // getAllAvailableKarts
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
/** Marks all karts except the ones listed in the string vector to be
|
|
|
|
* unavailable. This function is used on a client when receiving the list of
|
|
|
|
* karts from a client to mark all other karts as unavailable.
|
|
|
|
* \param karts List of karts that are available on a client.
|
|
|
|
*/
|
|
|
|
void KartPropertiesManager::setUnavailableKarts(std::vector<std::string> karts)
|
|
|
|
{
|
|
|
|
for(KartPropertiesVector::const_iterator i = m_karts_properties.begin();
|
|
|
|
i != m_karts_properties.end(); ++i)
|
|
|
|
{
|
|
|
|
if(!m_kart_available[i-m_karts_properties.begin()]) continue;
|
|
|
|
|
|
|
|
if(std::find(karts.begin(), karts.end(), (*i)->getIdent())
|
|
|
|
== karts.end())
|
|
|
|
{
|
|
|
|
m_kart_available[i-m_karts_properties.begin()]=false;
|
|
|
|
fprintf(stderr, "Kart '%s' not available on all clients, disabled.\n",
|
|
|
|
(*i)->getIdent().c_str());
|
|
|
|
|
|
|
|
} // kart not in list
|
|
|
|
} // for i in m_kart_properties
|
|
|
|
|
|
|
|
} // setUnavailableKarts
|
2008-09-07 09:51:44 -04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
/** Returns the (global) index of the n-th kart of a given group. If there is
|
2008-09-09 00:20:24 -04:00
|
|
|
* no such kart, -1 is returned.
|
2008-09-07 09:51:44 -04:00
|
|
|
*/
|
|
|
|
int KartPropertiesManager::getKartByGroup(const std::string& group, int n) const
|
|
|
|
{
|
|
|
|
int count=0;
|
|
|
|
for(KartPropertiesVector::const_iterator i = m_karts_properties.begin();
|
|
|
|
i != m_karts_properties.end(); ++i)
|
|
|
|
{
|
|
|
|
std::vector<std::string> groups=(*i)->getGroups();
|
|
|
|
if (std::find(groups.begin(), groups.end(), group)==groups.end()) continue;
|
|
|
|
if(count==n) return (int)(i-m_karts_properties.begin());
|
|
|
|
count=count+1;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
} // getKartByGroup
|
|
|
|
|
2008-09-07 09:58:37 -04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
bool KartPropertiesManager::testAndSetKart(int kartid)
|
|
|
|
{
|
|
|
|
if(!kartAvailable(kartid)) return false;
|
|
|
|
m_selected_karts.push_back(kartid);
|
|
|
|
return true;
|
|
|
|
} // testAndSetKart
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2008-09-09 00:20:24 -04:00
|
|
|
/** Returns true if a kart is available to be selected. A kart is available to
|
|
|
|
* be selected if it is available on all clients (i.e. m_kart_available is
|
|
|
|
* true), not yet selected, and not locked.
|
|
|
|
*/
|
2008-09-07 09:58:37 -04:00
|
|
|
bool KartPropertiesManager::kartAvailable(int kartid)
|
|
|
|
{
|
2008-09-09 00:20:24 -04:00
|
|
|
if(!m_kart_available[kartid]) return false;
|
2008-09-07 09:58:37 -04:00
|
|
|
std::vector<int>::iterator it;
|
|
|
|
for (it = m_selected_karts.begin(); it < m_selected_karts.end(); it++)
|
|
|
|
{
|
|
|
|
if ( kartid == *it) return false;
|
|
|
|
}
|
|
|
|
const KartProperties *kartprop=getKartById(kartid);
|
|
|
|
if(unlock_manager->isLocked(kartprop->getIdent())) return false;
|
|
|
|
return true;
|
|
|
|
} // testAndSetKart
|
|
|
|
|
2008-09-13 03:09:36 -04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
/** Sets a kart to be selected by specifying the identifier (name) of the kart.
|
|
|
|
* \param kart_name Name of the kart.
|
|
|
|
*/
|
|
|
|
void KartPropertiesManager::selectKartName(const std::string &kart_name)
|
|
|
|
{
|
|
|
|
int kart_id = getKartId(kart_name);
|
|
|
|
selectKart(kart_id);
|
|
|
|
} // selectKartName
|
|
|
|
|
2008-09-07 09:51:44 -04:00
|
|
|
//-----------------------------------------------------------------------------
|
2008-09-09 00:20:24 -04:00
|
|
|
/** Returns a list of randomly selected karts. This list firstly contains
|
|
|
|
* karts in the currently selected group, but which are not in the list
|
|
|
|
* of 'existing karts'. If not enough karts are available in the current
|
|
|
|
* group, karts from all other groups are used to fill up the list.
|
|
|
|
* This is used by the race manager to select the AI karts.
|
|
|
|
* \param count Number of karts to select randomly.
|
|
|
|
* \param existing_karst List of karts that should not be used. This is the
|
|
|
|
* list of karts selected by the players.
|
|
|
|
*/
|
2008-09-07 10:24:40 -04:00
|
|
|
std::vector<std::string> KartPropertiesManager::getRandomKartList(int count,
|
2008-09-09 00:20:24 -04:00
|
|
|
RemoteKartInfoList& existing_karts)
|
2008-09-07 09:51:44 -04:00
|
|
|
{
|
2008-09-07 10:24:40 -04:00
|
|
|
std::vector<std::string> random_karts;
|
|
|
|
|
2008-09-07 09:51:44 -04:00
|
|
|
// First: set up flags (based on global kart
|
|
|
|
// index) for which karts are already used
|
|
|
|
// -----------------------------------------
|
|
|
|
std::vector<bool> used;
|
|
|
|
used.resize(getNumberOfKarts(), false);
|
|
|
|
|
|
|
|
std::vector<std::string> all_karts;
|
2008-09-07 10:24:40 -04:00
|
|
|
for(unsigned int i=0; i<existing_karts.size(); i++)
|
2008-09-07 09:51:44 -04:00
|
|
|
{
|
2008-09-07 10:24:40 -04:00
|
|
|
int id=getKartId(existing_karts[i].getKartName());
|
2008-09-07 09:51:44 -04:00
|
|
|
used[id] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add karts from the current group
|
|
|
|
// --------------------------------
|
|
|
|
std::vector<int> karts = getKartsInGroup(user_config->m_kart_group);
|
|
|
|
std::vector<int>::iterator k;
|
2008-09-09 00:20:24 -04:00
|
|
|
// Remove karts that are already used or generally not available
|
|
|
|
// (i.e. locked or not available on all clients)
|
2008-09-07 09:51:44 -04:00
|
|
|
for(unsigned int i=0; i<karts.size();)
|
|
|
|
{
|
2008-09-09 00:20:24 -04:00
|
|
|
if(used[karts[i]] || !m_kart_available[karts[i]])
|
2008-09-07 09:51:44 -04:00
|
|
|
karts.erase(karts.begin()+i);
|
|
|
|
else
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
std::srand((unsigned int)std::time(0));
|
|
|
|
std::random_shuffle(karts.begin(), karts.end());
|
|
|
|
|
|
|
|
// Loop over all karts to fill till either all slots are filled, or
|
|
|
|
// there are no more karts in the current group
|
2008-09-07 10:24:40 -04:00
|
|
|
while(count>0 && karts.size()>0)
|
2008-09-07 09:51:44 -04:00
|
|
|
{
|
2008-09-07 10:24:40 -04:00
|
|
|
used[karts.back()] = true;
|
|
|
|
random_karts.push_back(m_karts_properties[karts.back()]->getIdent());
|
|
|
|
karts.pop_back();
|
|
|
|
count --;
|
2008-09-07 09:51:44 -04:00
|
|
|
}
|
2008-09-07 10:24:40 -04:00
|
|
|
if(count==0) return random_karts;
|
2008-09-07 09:51:44 -04:00
|
|
|
|
|
|
|
// Not enough karts in chosen group, so fill the rest with arbitrary karts
|
|
|
|
// -----------------------------------------------------------------------
|
|
|
|
// First create an index list with all unused karts.
|
|
|
|
karts.clear();
|
|
|
|
for(unsigned int i=0; i<getNumberOfKarts(); i++)
|
|
|
|
{
|
2008-09-09 00:20:24 -04:00
|
|
|
if(!used[i] && m_kart_available[i]) karts.push_back(i);
|
2008-09-07 09:51:44 -04:00
|
|
|
}
|
|
|
|
std::random_shuffle(karts.begin(), karts.end());
|
|
|
|
// Then fill up the remaining empty spaces
|
2008-09-07 10:24:40 -04:00
|
|
|
while(count>0 && karts.size()>0)
|
2008-09-07 09:51:44 -04:00
|
|
|
{
|
2008-09-07 10:24:40 -04:00
|
|
|
random_karts.push_back(m_karts_properties[karts.back()]->getIdent());
|
|
|
|
karts.pop_back();
|
|
|
|
count --;
|
2008-09-07 09:51:44 -04:00
|
|
|
}
|
2008-09-07 10:24:40 -04:00
|
|
|
// There should always be enough karts
|
2008-09-07 09:51:44 -04:00
|
|
|
assert(count==0);
|
2008-09-07 10:24:40 -04:00
|
|
|
return random_karts;
|
|
|
|
} // getRandomKartList
|
2008-09-07 09:51:44 -04:00
|
|
|
|
|
|
|
/* EOF */
|