AI karts are now filled firstly from karts of the current group (before

other karts are then used).


git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/trunk/supertuxkart@2154 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
hikerstk 2008-07-10 04:46:54 +00:00
parent fd3e3adafa
commit a9d967d724
5 changed files with 556 additions and 536 deletions

View File

@ -208,13 +208,13 @@ void CharSel::switchGroup()
m_index_avail_karts.clear();
// This loop is too long (since getNumberOfKarts returns all karts in all groups),
// but the loop is left if no more kart is found.
for(unsigned int i=0; i<kart_properties_manager->getNumberOfKarts(); i++)
const std::vector<int> &karts =
kart_properties_manager->getKartsInGroup(user_config->m_kart_group);
for(unsigned int i=0; i<karts.size(); i++)
{
int globalIndex = kart_properties_manager->getKartByGroup(user_config->m_kart_group, i);
if(globalIndex==-1) break;
if(kartAvailable(globalIndex))
if(kartAvailable(karts[i]))
{
m_index_avail_karts.push_back(globalIndex);
m_index_avail_karts.push_back(karts[i]);
}
}

View File

@ -25,6 +25,7 @@
#include "kart_properties_manager.hpp"
#include "kart_properties.hpp"
#include "translation.hpp"
#include "user_config.hpp"
#if defined(WIN32) && !defined(__CYGWIN__)
# define snprintf _snprintf
#endif
@ -34,8 +35,7 @@ KartPropertiesManager *kart_properties_manager=0;
KartPropertiesManager::KartPropertiesManager()
{
m_all_groups.clear();
m_all_groups.push_back("standard");
}
} // KartPropertiesManager
//-----------------------------------------------------------------------------
KartPropertiesManager::~KartPropertiesManager()
@ -90,13 +90,13 @@ void KartPropertiesManager::loadKartData(bool dont_load_models)
m_max_steer_angle = kp->getMaxSteerAngle();
}
const std::vector<std::string>& groups=kp->getGroups();
for(unsigned int i=0; i<groups.size(); i++)
for(unsigned int g=0; g<groups.size(); g++)
{
if(std::find(m_all_groups.begin(), m_all_groups.end(), groups[i])
== m_all_groups.end())
if(m_groups.find(groups[g])==m_groups.end())
{
m_all_groups.push_back(groups[i]);
m_all_groups.push_back(groups[g]);
}
m_groups[groups[g]].push_back(m_karts_properties.size()-1);
}
} // for i
} // loadKartData
@ -159,57 +159,75 @@ int KartPropertiesManager::getKartByGroup(const std::string& group, int n) const
return -1;
} // getKartByGroup
/*FIXME: the next function is unused, if it is not useful, it should be
deleted.*/
//-----------------------------------------------------------------------------
std::vector<std::string> KartPropertiesManager::getRandomKarts(int len)
{
std::vector<std::string> all_karts;
for(KartPropertiesVector::const_iterator i = m_karts_properties.begin();
i != m_karts_properties.end(); ++i)
{
all_karts.push_back((*i)->getIdent());
}
std::random_shuffle(all_karts.begin(), all_karts.end());
all_karts.resize(len);
return all_karts;
} // getRandomKart
//-----------------------------------------------------------------------------
void KartPropertiesManager::fillWithRandomKarts(std::vector<std::string>& vec)
{
// First: set up flags (based on global kart
// index) for which karts are already used
// -----------------------------------------
std::vector<bool> used;
used.resize(getNumberOfKarts(), false);
int count=vec.size();
std::vector<std::string> all_karts;
for(unsigned int i=0; i<vec.size(); i++)
{
if(vec[i].empty()) continue;
int id=getKartId(vec[i]);
used[id] = true;
count --;
}
for(KartPropertiesVector::const_iterator i = m_karts_properties.begin();
i != m_karts_properties.end(); ++i)
all_karts.push_back((*i)->getIdent());
// Add karts from the current group
// --------------------------------
std::vector<int> karts = getKartsInGroup(user_config->m_kart_group);
std::vector<int>::iterator k;
// Remove karts that are already used
for(unsigned int i=0; i<karts.size();)
{
if(used[karts[i]])
karts.erase(karts.begin()+i);
else
i++;
}
std::srand((unsigned int)std::time(0));
std::random_shuffle(karts.begin(), karts.end());
std::random_shuffle(all_karts.begin(), all_karts.end());
int new_kart = 0;
for(int i = 0; i < int(vec.size()); ++i)
// Loop over all karts to fill till either all slots are filled, or
// there are no more karts in the current group
for(unsigned int i=0; i<vec.size() && count>0 && karts.size()>0; i++)
{
while(vec[i].empty())
if(vec[i].empty())
{
if (std::find(vec.begin(), vec.end(), all_karts[new_kart]) == vec.end())
{ // Found a new kart, so use it
vec[i] = all_karts[new_kart];
used[karts.back()] = true;
vec[i] = m_karts_properties[karts.back()]->getIdent();
karts.pop_back();
count --;
}
else if (!(all_karts.size() >= vec.size()))
{ // We need to fill more karts than we have available, so don't care about dups
vec[i] = all_karts[new_kart];
}
if(count==0) return;
new_kart += 1;
} // while
} // for i
// 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++)
{
if(!used[i]) karts.push_back(i);
}
std::random_shuffle(karts.begin(), karts.end());
// Then fill up the remaining empty spaces
for(unsigned int i=0; i<vec.size() && count>0 && karts.size()>0; i++)
{
if(vec[i].empty())
{
vec[i] = m_karts_properties[karts.back()]->getIdent();
karts.pop_back();
count --;
}
}
// There should never be more karts to be selected than there are.
assert(count==0);
} // fillWithRandomKarts
/* EOF */

View File

@ -21,14 +21,15 @@
#define HEADER_KARTPROPERTIESMANAGER_H
#include <vector>
//#include "kart_properties.hpp"
#include <map>
class KartProperties;
class KartPropertiesManager
{
private:
std::vector<std::string> m_all_groups;
std::map<std::string, std::vector<int>> m_groups;
protected:
float m_max_steer_angle;
@ -52,9 +53,10 @@ public:
void loadKartData (bool dont_load_models=false);
const float getMaximumSteeringAngle() const {return m_max_steer_angle;}
const unsigned int getNumberOfKarts () const {return (unsigned int)m_karts_properties.size();}
const std::vector<std::string>& getAllGroups () const {return m_all_groups; }
/** Return len random karts */
std::vector<std::string> getRandomKarts (int len);
const std::vector<std::string>&
getAllGroups () const {return m_all_groups; }
const std::vector<int>& getKartsInGroup (const std::string& g)
{return m_groups[g]; }
/** Fill the empty positions in the given vector with random karts */
void fillWithRandomKarts (std::vector<std::string>& vec);

View File

@ -1367,7 +1367,7 @@ void Track::getTerrainInfo(const Vec3 &pos, float *hot, Vec3 *normal,
*normal = rayCallback.m_hitNormalWorld;
*material = rayCallback.m_material;
// Note: material might be NULL. This happens if the ray cast does not
// hit the track, but anoterh rigid body (kart, moving_physics) - e.g.
// hit the track, but another rigid body (kart, moving_physics) - e.g.
// assume two karts falling down, one over the other. Bullet does not
// have any triangle/material information in this case!
} // getTerrainInfo