Added handling of local/global player and player ids.

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/trunk/supertuxkart@2229 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
hikerstk 2008-09-07 13:51:44 +00:00
parent ea579a3f62
commit ee0bb967c3
11 changed files with 423 additions and 379 deletions

View File

@ -22,11 +22,11 @@
#include "coord.hpp"
#include "world.hpp"
#include "player_kart.hpp"
#include "track_manager.hpp"
#include "track.hpp"
#include "camera.hpp"
#include "user_config.hpp"
#include "constants.hpp"
#include "race_manager.hpp"
Camera::Camera(int camera_index, const Kart* kart)
{

View File

@ -357,7 +357,7 @@ void CharSel::select()
const KartProperties* KP = kart_properties_manager->getKartById(kart_id);
if (KP != NULL)
{
race_manager->setLocalPlayerKart(m_player_index, KP->getIdent());
race_manager->setLocalKartInfo(m_player_index, KP->getIdent());
user_config->m_player[m_player_index].setLastKartId(kart_id);
// Add selected kart (token) to selected karts vector so it cannot be
// selected again

View File

@ -1,233 +1,233 @@
// $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"
#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);
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
//-----------------------------------------------------------------------------
const int KartPropertiesManager::getKartId(const std::string IDENT) const
{
int j = 0;
for(KartPropertiesVector::const_iterator i = m_karts_properties.begin();
i != m_karts_properties.end(); ++i)
{
if ((*i)->getIdent() == IDENT)
return j;
++j;
}
char msg[MAX_ERROR_MESSAGE_LENGTH];
snprintf(msg, sizeof(msg), "KartPropertiesManager: Couldn't find kart: '%s'",
IDENT.c_str());
throw std::runtime_error(msg);
} // getKartId
//-----------------------------------------------------------------------------
const KartProperties* KartPropertiesManager::getKart(const std::string IDENT) const
{
for(KartPropertiesVector::const_iterator i = m_karts_properties.begin();
i != m_karts_properties.end(); ++i)
{
if ((*i)->getIdent() == IDENT)
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];
}
//-----------------------------------------------------------------------------
/** Returns the (global) index of the n-th kart of a given group. If there is
* no such kart, -1 is returned
*/
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
//-----------------------------------------------------------------------------
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 --;
}
// 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());
// 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++)
{
if(vec[i].empty())
{
used[karts.back()] = true;
vec[i] = m_karts_properties[karts.back()]->getIdent();
karts.pop_back();
count --;
}
}
if(count==0) return;
// 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 */
// $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"
#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);
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
//-----------------------------------------------------------------------------
const int KartPropertiesManager::getKartId(const std::string IDENT) const
{
int j = 0;
for(KartPropertiesVector::const_iterator i = m_karts_properties.begin();
i != m_karts_properties.end(); ++i)
{
if ((*i)->getIdent() == IDENT)
return j;
++j;
}
char msg[MAX_ERROR_MESSAGE_LENGTH];
snprintf(msg, sizeof(msg), "KartPropertiesManager: Couldn't find kart: '%s'",
IDENT.c_str());
throw std::runtime_error(msg);
} // getKartId
//-----------------------------------------------------------------------------
const KartProperties* KartPropertiesManager::getKart(const std::string IDENT) const
{
for(KartPropertiesVector::const_iterator i = m_karts_properties.begin();
i != m_karts_properties.end(); ++i)
{
if ((*i)->getIdent() == IDENT)
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];
}
//-----------------------------------------------------------------------------
/** Returns the (global) index of the n-th kart of a given group. If there is
* no such kart, -1 is returned
*/
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
//-----------------------------------------------------------------------------
void KartPropertiesManager::fillWithRandomKarts(std::vector<RemoteKartInfo>& 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].getKartName()=="") continue;
int id=getKartId(vec[i].getKartName());
used[id] = true;
count --;
}
// 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());
// 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++)
{
if(vec[i].getKartName()=="")
{
used[karts.back()] = true;
vec[i] = RemoteKartInfo(m_karts_properties[karts.back()]->getIdent());
karts.pop_back();
count --;
}
}
if(count==0) return;
// 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].getKartName()=="")
{
vec[i] = RemoteKartInfo(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

@ -1,70 +1,71 @@
// $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.
#ifndef HEADER_KARTPROPERTIESMANAGER_H
#define HEADER_KARTPROPERTIESMANAGER_H
#include <vector>
#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;
typedef std::vector<KartProperties*> KartPropertiesVector;
/** All available kart configurations */
KartPropertiesVector m_karts_properties;
public:
KartPropertiesManager();
~KartPropertiesManager();
// vector containing kart numbers that have been selected in multiplayer
// games. This it used to ensure the same kart can not be selected more
// than once.
std::vector<int> m_selected_karts;
const KartProperties* getKartById (int i) const;
const KartProperties* getKart (const std::string IDENT) const;
const int getKartId (const std::string IDENT) const;
int getKartByGroup (const std::string& group, int i) const;
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; }
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);
void removeTextures ();
};
extern KartPropertiesManager *kart_properties_manager;
#endif
/* EOF */
// $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.
#ifndef HEADER_KARTPROPERTIESMANAGER_H
#define HEADER_KARTPROPERTIESMANAGER_H
#include <vector>
#include <map>
#include "network/remote_kart_info.hpp"
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;
typedef std::vector<KartProperties*> KartPropertiesVector;
/** All available kart configurations */
KartPropertiesVector m_karts_properties;
public:
KartPropertiesManager();
~KartPropertiesManager();
// vector containing kart numbers that have been selected in multiplayer
// games. This it used to ensure the same kart can not be selected more
// than once.
std::vector<int> m_selected_karts;
const KartProperties* getKartById (int i) const;
const KartProperties* getKart (const std::string IDENT) const;
const int getKartId (const std::string IDENT) const;
int getKartByGroup (const std::string& group, int i) const;
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; }
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<RemoteKartInfo>& vec);
void removeTextures ();
};
extern KartPropertiesManager *kart_properties_manager;
#endif
/* EOF */

View File

@ -184,7 +184,7 @@ int handleCmdLine(int argc, char **argv)
std::string filename=file_manager->getKartFile(std::string(argv[i+1])+".tkkf");
if(filename!="")
{
race_manager->setLocalPlayerKart(0, argv[i+1]);
race_manager->setLocalKartInfo(0, argv[i+1]);
fprintf ( stdout, "You choose to use kart '%s'.\n", argv[i+1] ) ;
}
else
@ -555,7 +555,7 @@ int main(int argc, char *argv[] )
// Profiling
// =========
race_manager->setNumPlayers(1);
race_manager->setLocalPlayerKart(0, kart_properties_manager->getKart("tuxkart")->getIdent());
race_manager->setLocalKartInfo(0, kart_properties_manager->getKart("tuxkart")->getIdent());
race_manager->setMajorMode (RaceManager::RM_SINGLE);
race_manager->setMinorMode (RaceManager::RM_QUICK_RACE);
race_manager->setDifficulty(RaceManager::RD_HARD);

View File

@ -38,9 +38,6 @@ NetworkManager::NetworkManager()
fprintf (stderr, "An error occurred while initializing ENet.\n");
exit(-1);
}
// FIXME: debugging
m_kart_info.push_back(RemoteKartInfo("tuxkart","xx", 1));
m_kart_info.push_back(RemoteKartInfo("yeti", "yy", 1));
#endif
} // NetworkManager
@ -211,6 +208,7 @@ void NetworkManager::update(float dt)
}
} // update
// ----------------------------------------------------------------------------
/** Send race_manager->getNumPlayers(), the kart and the name of each
player to the server.
@ -220,7 +218,7 @@ void NetworkManager::sendKartsInformationToServer()
for(int i=0; i<(int)race_manager->getNumLocalPlayers(); i++)
{
fprintf(stderr, "Sending name '%s', ",user_config->m_player[i].getName().c_str());
fprintf(stderr, "kart name '%s'\n", race_manager->getLocalPlayerKart(i).getKartName().c_str());
fprintf(stderr, "kart name '%s'\n", race_manager->getLocalKartInfo(i).getKartName().c_str());
} // for i<getNumLocalPlayers
fprintf(stderr, "Client sending kart information to server\n");
} // sendKartsInformationToServer
@ -230,23 +228,32 @@ void NetworkManager::sendKartsInformationToServer()
*/
void NetworkManager::waitForKartsInformation()
{
m_kart_info.clear();
fprintf(stderr, "Server receiving all kart information\n");
// FIXME: debugging
m_kart_info.push_back(RemoteKartInfo(0, "tuxkart","xx", 1));
m_kart_info.push_back(RemoteKartInfo(1, "yetikart", "yy", 1));
// First put the local karts into the race_manager:
// Get the local kart info
for(unsigned int i=0; i<race_manager->getNumLocalPlayers(); i++)
m_kart_info.push_back(race_manager->getLocalKartInfo(i));
for(unsigned int i=0; i<(int)race_manager->getNumLocalPlayers(); i++)
{
//FIXME race_manager->setPlayerKart(i, race_manager->getLocalPlayerKart(i),
// m_host_id);
}
int offset=race_manager->getNumLocalPlayers();
// Now sort by (hostid, playerid)
std::sort(m_kart_info.begin(), m_kart_info.end());
// Set the global player ID for each player
for(unsigned int i=0; i<m_kart_info.size(); i++)
m_kart_info[i].setGlobalPlayerId(i);
// FIXME: distribute m_kart_info to all clients
// Set the player kart information
race_manager->setNumPlayers(m_kart_info.size());
for(unsigned int i=0; i<m_kart_info.size(); i++)
{
// receive number of karts, kart name, and player name for each hosts,
// and put it into the race_manager data structure:
//FIXME race_manager->setPlayerKart(offset+i, m_kart_info[i].m_kart_name,
// m_kart_info[i].m_client_id);
} // for i
race_manager->setPlayerKart(i, m_kart_info[i]);
}
} // waitForKartsInformation
// ----------------------------------------------------------------------------
@ -255,10 +262,11 @@ void NetworkManager::waitForKartsInformation()
void NetworkManager::sendRaceInformationToClients()
{
fprintf(stderr, "server sending race_manager information to all clients\n");
for(unsigned i=0; i<race_manager->getNumPlayers(); i++)
for(unsigned i=0; i<race_manager->getNumLocalPlayers(); i++)
{
// fprintf(stderr, "Sending kart '%s' host %d\n",
//FIXME race_manager->getKartName(i), race_manager->getHostId(i));
const RemoteKartInfo& ki=race_manager->getLocalKartInfo(i);
fprintf(stderr, "Sending kart '%s' playerid %d host %d\n",
ki.getKartName(), ki.getLocalPlayerId(), ki.getHostId());
} // for i
} // sendRaceInformationToClients

View File

@ -59,14 +59,16 @@ private:
public:
NetworkManager();
~NetworkManager();
void setMode(NetworkMode m) {m_mode = m; }
NetworkMode getMode() const {return m_mode; }
void setState(NetworkState s) {m_state = s; }
NetworkState getState() const {return m_state; }
int getClientId() const {return m_host_id; }
int getNumClients() const {return m_num_clients;}
void setPort(int p) {m_port=p; }
void setServerIP(const std::string &s) {m_server_address=s; }
void setMode(NetworkMode m) {m_mode = m; }
NetworkMode getMode() const {return m_mode; }
void setState(NetworkState s) {m_state = s; }
NetworkState getState() const {return m_state; }
int getHostId() const {return m_host_id; }
int getNumClients() const {return m_num_clients; }
void setPort(int p) {m_port=p; }
void setServerIP(const std::string &s) {m_server_address=s; }
void setKartInfo(int player_id, const std::string& kart,
const std::string& user="", int hostid=-1);
bool initialiseConnections();
void update(float dt);
void sendKartsInformationToServer();

View File

@ -26,19 +26,36 @@ class RemoteKartInfo
{
std::string m_kart_name;
std::string m_user_name;
int m_client_id;
int m_player_id;
int m_host_id;
int m_local_player_id;
int m_global_player_id;
public:
RemoteKartInfo(std::string kart_name, std::string user_name, int id)
: m_kart_name(kart_name), m_user_name(user_name), m_client_id(id)
{};
RemoteKartInfo() {m_kart_name=""; m_user_name=""; m_client_id=-1; m_player_id=-1;}
void setKartName(const std::string& n) { m_kart_name = n; }
void setUserName(const std::string& u) { m_user_name = u; }
void setClientId(int id) { m_client_id = id; }
void setPlayerId(int id) { m_player_id = id; }
const std::string& getKartName() const {return m_kart_name; }
RemoteKartInfo(int player_id, const std::string& kart_name,
const std::string& user_name, int host_id)
: m_kart_name(kart_name), m_user_name(user_name),
m_local_player_id(player_id), m_host_id(host_id)
{};
RemoteKartInfo(const std::string& kart_name)
{m_kart_name=kart_name; m_user_name="";
m_host_id=-1; m_local_player_id=-1;}
RemoteKartInfo() {m_kart_name=""; m_user_name="";
m_host_id=-1; m_local_player_id=-1;}
void setKartName(const std::string& n) { m_kart_name = n; }
void setUserName(const std::string& u) { m_user_name = u; }
void setHostId(int id) { m_host_id = id; }
void setLocalPlayerId(int id) { m_local_player_id = id; }
void setGlobalPlayerId(int id) { m_global_player_id = id; }
int getHostId() const { return m_host_id; }
int getLocalPlayerId() const { return m_local_player_id; }
int getGlobalPlayerId() const { return m_global_player_id; }
const std::string& getKartName() const {return m_kart_name; }
const std::string& getPlayerName() const{return m_user_name; }
bool operator<(const RemoteKartInfo& other)
{
return (m_host_id<other.m_host_id ||
m_host_id==other.m_host_id && m_local_player_id<other.m_local_player_id);
}
}; // RemoteKartInfo
#endif

View File

@ -62,14 +62,16 @@ private:
double m_last_time; // needed for restart
int m_prev_finish_pos; // previous finished position
KartType m_kart_type; // Kart type: AI, player, network player etc.
int m_player_id; // player controling the kart, for AI: -1
int m_local_player_id; // player controling the kart, for AI: -1
int m_global_player_id; // global ID of player
KartStatus(const std::string& ident, const int& prev_finish_pos,
const int& player_id, KartType kt) :
int local_player_id, int global_player_id, KartType kt) :
m_ident(ident), m_score(0), m_last_score(0),
m_overall_time(0.0f), m_last_time(0.0f),
m_prev_finish_pos(prev_finish_pos), m_kart_type(kt),
m_player_id(player_id)
m_local_player_id(local_player_id),
m_global_player_id(global_player_id)
{}
}; // KartStatus
@ -78,8 +80,8 @@ private:
Difficulty m_difficulty;
RaceModeType m_major_mode, m_minor_mode;
typedef std::vector<std::string> PlayerKarts;
PlayerKarts m_player_karts;
std::vector<RemoteKartInfo> m_local_player_karts;
std::vector<RemoteKartInfo> m_player_karts;
std::vector<RemoteKartInfo> m_local_kart_info;
std::vector<std::string> m_tracks;
std::vector<int> m_host_ids;
std::vector<int> m_num_laps;
@ -90,7 +92,7 @@ private:
unsigned int m_num_finished_karts;
unsigned int m_num_finished_players;
int m_coin_target;
void startNextRace(); // start a next race
friend bool operator< (const KartStatus& left, const KartStatus& right)
@ -102,20 +104,19 @@ public:
bool m_active_race; //True if there is a race
public:
RaceManager();
~RaceManager();
void setPlayerKart(unsigned int player, const std::string& kart,
const std::string& player_name, int hostid);
void setLocalPlayerKart(unsigned int player, const std::string& kart);
const RemoteKartInfo&
getLocalPlayerKart(unsigned int p) const {return m_local_player_karts[p]; }
RaceManager();
~RaceManager();
void reset();
void setLocalKartInfo(unsigned int player_id, const std::string& kart);
const RemoteKartInfo&
getLocalKartInfo(unsigned int n) const {return m_local_kart_info[n];}
void setNumLocalPlayers(unsigned int n);
unsigned int getNumLocalPlayers() const {return m_local_kart_info.size(); };
void setNumPlayers(int num);
void setPlayerKart(unsigned int player_id, const RemoteKartInfo& ki);
void RaceFinished(const Kart* kart, float time);
void setTrack(const std::string& track);
void setNumPlayers(int num);
void setNumLocalPlayers(int num) {m_local_player_karts.resize(num); }
void setGrandPrix(const GrandPrixData &gp){ m_grand_prix = gp; }
void setDifficulty(Difficulty diff);
void setNumLaps(int num) { m_num_laps.clear();
@ -127,8 +128,7 @@ public:
RaceModeType getMajorMode() const { return m_major_mode; }
RaceModeType getMinorMode() const { return m_minor_mode; }
unsigned int getNumKarts() const { return m_num_karts; }
unsigned int getNumPlayers() const { return (int)m_player_karts.size();}
unsigned int getNumLocalPlayers() const { return (int)m_local_player_karts.size();}
unsigned int getNumPlayers() const { return m_player_karts.size(); }
int getNumLaps() const { return m_num_laps[m_track_number];}
Difficulty getDifficulty() const { return m_difficulty; }
const std::string&
@ -137,21 +137,22 @@ public:
*getGrandPrix() const { return &m_grand_prix; }
unsigned int getFinishedKarts() const { return m_num_finished_karts; }
unsigned int getFinishedPlayers() const { return m_num_finished_players; }
const std::string&
getHerringStyle() const { return m_grand_prix.getHerringStyle(); }
const std::string&
getKartName(int kart) const { return m_kart_status[kart].m_ident;}
const std::string&
getHerringStyle() const { return m_grand_prix.getHerringStyle();}
int getKartScore(int krt) const { return m_kart_status[krt].m_score;}
int getKartScore(int krt) const { return m_kart_status[krt].m_score; }
int getKartPrevScore(int krt)const { return m_kart_status[krt].m_last_score;}
int getKartPlayerId(int krt) const { return m_kart_status[krt].m_player_id;}
int getPositionScore(int p) const { return m_score_for_position[p-1]; }
int getKartLocalPlayerId(int k) const { return m_kart_status[k].m_local_player_id; }
int getKartGlobalPlayerId(int k) const { return m_kart_status[k].m_global_player_id; }
double getOverallTime(int kart) const { return m_kart_status[kart].m_overall_time;}
int getCoinTarget() const { return m_coin_target; }
bool raceHasLaps() const { return m_minor_mode!=RM_FOLLOW_LEADER;}
KartType getKartType(int kart) const { return m_kart_status[kart].m_kart_type;}
int getCoinTarget() const { return m_coin_target; }
bool raceHasLaps() const { return m_minor_mode!=RM_FOLLOW_LEADER; }
int getPositionScore(int p) const { return m_score_for_position[p-1]; }
int allPlayerFinished() const {return
m_num_finished_players==m_player_karts.size(); }
m_num_finished_players==m_player_karts.size();}
int raceIsActive() const { return m_active_race; }
KartType getKartType(int kart) const {return m_kart_status[kart].m_kart_type; }
void setMirror() {/*FIXME*/}
void setReverse(){/*FIXME*/}

View File

@ -45,6 +45,8 @@
#include "camera.hpp"
#include "robots/default_robot.hpp"
#include "unlock_manager.hpp"
#include "network/network_kart.hpp"
#include "network/network_manager.hpp"
#ifdef HAVE_GHOST_REPLAY
# include "replay_player.hpp"
#endif
@ -103,8 +105,9 @@ World::World()
int position = i+1; // position start with 1
btTransform init_pos=m_track->getStartTransform(position);
Kart* newkart;
const std::string& kart_name=race_manager->getKartName(i);
int player_id = race_manager->getKartPlayerId(i);
const std::string& kart_name = race_manager->getKartName(i);
int local_player_id = race_manager->getKartLocalPlayerId(i);
int global_player_id = race_manager->getKartGlobalPlayerId(i);
if(user_config->m_profile)
{
// In profile mode, load only the old kart
@ -113,7 +116,7 @@ World::World()
// karts can be seen.
if(i==race_manager->getNumKarts()-1)
{
scene->createCamera(player_id, newkart);
scene->createCamera(local_player_id, newkart);
}
}
else
@ -122,11 +125,22 @@ World::World()
{
case RaceManager::KT_PLAYER:
newkart = new PlayerKart(kart_name, position,
&(user_config->m_player[player_id]),
init_pos, player_id);
m_player_karts[player_id] = (PlayerKart*)newkart;
&(user_config->m_player[local_player_id]),
init_pos, local_player_id);
m_player_karts[global_player_id] = (PlayerKart*)newkart;
m_local_player_karts[local_player_id] = static_cast<PlayerKart*>(newkart);
break;
case RaceManager::KT_NETWORK_PLAYER:
if(network_manager->getMode()==NetworkManager::NW_SERVER)
{
newkart = new NetworkKart(kart_name, position, init_pos,
global_player_id);
}
else
{
newkart = new NetworkKart(kart_name, position, init_pos,
global_player_id);
}
break;
case RaceManager::KT_AI:
newkart = loadRobot(kart_name, position, init_pos);

View File

@ -21,6 +21,7 @@
#define HEADER_WORLD_H
#include <vector>
#define _WINSOCKAPI_
#include <plib/ssg.h>
#include "track.hpp"
#include "player_kart.hpp"