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 "coord.hpp"
#include "world.hpp" #include "world.hpp"
#include "player_kart.hpp" #include "player_kart.hpp"
#include "track_manager.hpp"
#include "track.hpp" #include "track.hpp"
#include "camera.hpp" #include "camera.hpp"
#include "user_config.hpp" #include "user_config.hpp"
#include "constants.hpp" #include "constants.hpp"
#include "race_manager.hpp"
Camera::Camera(int camera_index, const Kart* kart) 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); const KartProperties* KP = kart_properties_manager->getKartById(kart_id);
if (KP != NULL) 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); user_config->m_player[m_player_index].setLastKartId(kart_id);
// Add selected kart (token) to selected karts vector so it cannot be // Add selected kart (token) to selected karts vector so it cannot be
// selected again // selected again

View File

@ -1,233 +1,233 @@
// $Id$ // $Id$
// //
// SuperTuxKart - a fun racing game with go-kart // SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2004-2006 Ingo Ruhnke <grumbel@gmx.de> // Copyright (C) 2004-2006 Ingo Ruhnke <grumbel@gmx.de>
// //
// This program is free software; you can redistribute it and/or // This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License // modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3 // as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version. // of the License, or (at your option) any later version.
// //
// This program is distributed in the hope that it will be useful, // This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of // but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details. // GNU General Public License for more details.
// //
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software // along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include <stdexcept> #include <stdexcept>
#include <algorithm> #include <algorithm>
#include <ctime> #include <ctime>
#include "file_manager.hpp" #include "file_manager.hpp"
#include "string_utils.hpp" #include "string_utils.hpp"
#include "kart_properties_manager.hpp" #include "kart_properties_manager.hpp"
#include "kart_properties.hpp" #include "kart_properties.hpp"
#include "translation.hpp" #include "translation.hpp"
#include "user_config.hpp" #include "user_config.hpp"
#if defined(WIN32) && !defined(__CYGWIN__) #if defined(WIN32) && !defined(__CYGWIN__)
# define snprintf _snprintf # define snprintf _snprintf
#endif #endif
KartPropertiesManager *kart_properties_manager=0; KartPropertiesManager *kart_properties_manager=0;
KartPropertiesManager::KartPropertiesManager() KartPropertiesManager::KartPropertiesManager()
{ {
m_all_groups.clear(); m_all_groups.clear();
} // KartPropertiesManager } // KartPropertiesManager
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
KartPropertiesManager::~KartPropertiesManager() KartPropertiesManager::~KartPropertiesManager()
{ {
for(KartPropertiesVector::iterator i = m_karts_properties.begin(); for(KartPropertiesVector::iterator i = m_karts_properties.begin();
i != m_karts_properties.end(); ++i) i != m_karts_properties.end(); ++i)
delete *i; delete *i;
} // ~KartPropertiesManager } // ~KartPropertiesManager
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void KartPropertiesManager::removeTextures() void KartPropertiesManager::removeTextures()
{ {
for(KartPropertiesVector::iterator i = m_karts_properties.begin(); for(KartPropertiesVector::iterator i = m_karts_properties.begin();
i != m_karts_properties.end(); ++i) i != m_karts_properties.end(); ++i)
{ {
delete *i; delete *i;
} }
m_karts_properties.clear(); m_karts_properties.clear();
callback_manager->clear(CB_KART); callback_manager->clear(CB_KART);
} // removeTextures } // removeTextures
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void KartPropertiesManager::loadKartData(bool dont_load_models) void KartPropertiesManager::loadKartData(bool dont_load_models)
{ {
m_max_steer_angle = -1.0f; m_max_steer_angle = -1.0f;
std::set<std::string> result; std::set<std::string> result;
file_manager->listFiles(result, file_manager->getKartDir(), file_manager->listFiles(result, file_manager->getKartDir(),
/*is_full_path*/ true); /*is_full_path*/ true);
// Find out which characters are available and load them // Find out which characters are available and load them
for(std::set<std::string>::iterator i = result.begin(); for(std::set<std::string>::iterator i = result.begin();
i != result.end(); ++i) i != result.end(); ++i)
{ {
std::string kart_file; std::string kart_file;
try try
{ {
kart_file = file_manager->getKartFile((*i)+".kart"); kart_file = file_manager->getKartFile((*i)+".kart");
} }
catch (std::exception& e) catch (std::exception& e)
{ {
(void)e; // remove warning about unused variable (void)e; // remove warning about unused variable
continue; continue;
} }
FILE *f=fopen(kart_file.c_str(),"r"); FILE *f=fopen(kart_file.c_str(),"r");
if(!f) continue; if(!f) continue;
fclose(f); fclose(f);
KartProperties* kp = new KartProperties(); KartProperties* kp = new KartProperties();
kp->load(kart_file, "tuxkart-kart", dont_load_models); kp->load(kart_file, "tuxkart-kart", dont_load_models);
m_karts_properties.push_back(kp); m_karts_properties.push_back(kp);
if(kp->getMaxSteerAngle(0) > m_max_steer_angle) if(kp->getMaxSteerAngle(0) > m_max_steer_angle)
{ {
m_max_steer_angle = kp->getMaxSteerAngle(0); m_max_steer_angle = kp->getMaxSteerAngle(0);
} }
const std::vector<std::string>& groups=kp->getGroups(); const std::vector<std::string>& groups=kp->getGroups();
for(unsigned int g=0; g<groups.size(); g++) for(unsigned int g=0; g<groups.size(); g++)
{ {
if(m_groups.find(groups[g])==m_groups.end()) if(m_groups.find(groups[g])==m_groups.end())
{ {
m_all_groups.push_back(groups[g]); m_all_groups.push_back(groups[g]);
} }
m_groups[groups[g]].push_back(m_karts_properties.size()-1); m_groups[groups[g]].push_back(m_karts_properties.size()-1);
} }
} // for i } // for i
} // loadKartData } // loadKartData
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
const int KartPropertiesManager::getKartId(const std::string IDENT) const const int KartPropertiesManager::getKartId(const std::string IDENT) const
{ {
int j = 0; int j = 0;
for(KartPropertiesVector::const_iterator i = m_karts_properties.begin(); for(KartPropertiesVector::const_iterator i = m_karts_properties.begin();
i != m_karts_properties.end(); ++i) i != m_karts_properties.end(); ++i)
{ {
if ((*i)->getIdent() == IDENT) if ((*i)->getIdent() == IDENT)
return j; return j;
++j; ++j;
} }
char msg[MAX_ERROR_MESSAGE_LENGTH]; char msg[MAX_ERROR_MESSAGE_LENGTH];
snprintf(msg, sizeof(msg), "KartPropertiesManager: Couldn't find kart: '%s'", snprintf(msg, sizeof(msg), "KartPropertiesManager: Couldn't find kart: '%s'",
IDENT.c_str()); IDENT.c_str());
throw std::runtime_error(msg); throw std::runtime_error(msg);
} // getKartId } // getKartId
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
const KartProperties* KartPropertiesManager::getKart(const std::string IDENT) const const KartProperties* KartPropertiesManager::getKart(const std::string IDENT) const
{ {
for(KartPropertiesVector::const_iterator i = m_karts_properties.begin(); for(KartPropertiesVector::const_iterator i = m_karts_properties.begin();
i != m_karts_properties.end(); ++i) i != m_karts_properties.end(); ++i)
{ {
if ((*i)->getIdent() == IDENT) if ((*i)->getIdent() == IDENT)
return *i; return *i;
} }
return NULL; return NULL;
} // getKart } // getKart
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
const KartProperties* KartPropertiesManager::getKartById(int i) const const KartProperties* KartPropertiesManager::getKartById(int i) const
{ {
if (i < 0 || i >= int(m_karts_properties.size())) if (i < 0 || i >= int(m_karts_properties.size()))
return NULL; return NULL;
return m_karts_properties[i]; return m_karts_properties[i];
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** Returns the (global) index of the n-th kart of a given group. If there is /** Returns the (global) index of the n-th kart of a given group. If there is
* no such kart, -1 is returned * no such kart, -1 is returned
*/ */
int KartPropertiesManager::getKartByGroup(const std::string& group, int n) const int KartPropertiesManager::getKartByGroup(const std::string& group, int n) const
{ {
int count=0; int count=0;
for(KartPropertiesVector::const_iterator i = m_karts_properties.begin(); for(KartPropertiesVector::const_iterator i = m_karts_properties.begin();
i != m_karts_properties.end(); ++i) i != m_karts_properties.end(); ++i)
{ {
std::vector<std::string> groups=(*i)->getGroups(); std::vector<std::string> groups=(*i)->getGroups();
if (std::find(groups.begin(), groups.end(), group)==groups.end()) continue; if (std::find(groups.begin(), groups.end(), group)==groups.end()) continue;
if(count==n) return (int)(i-m_karts_properties.begin()); if(count==n) return (int)(i-m_karts_properties.begin());
count=count+1; count=count+1;
} }
return -1; return -1;
} // getKartByGroup } // getKartByGroup
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void KartPropertiesManager::fillWithRandomKarts(std::vector<std::string>& vec) void KartPropertiesManager::fillWithRandomKarts(std::vector<RemoteKartInfo>& vec)
{ {
// First: set up flags (based on global kart // First: set up flags (based on global kart
// index) for which karts are already used // index) for which karts are already used
// ----------------------------------------- // -----------------------------------------
std::vector<bool> used; std::vector<bool> used;
used.resize(getNumberOfKarts(), false); used.resize(getNumberOfKarts(), false);
int count=vec.size(); int count=vec.size();
std::vector<std::string> all_karts; std::vector<std::string> all_karts;
for(unsigned int i=0; i<vec.size(); i++) for(unsigned int i=0; i<vec.size(); i++)
{ {
if(vec[i].empty()) continue; if(vec[i].getKartName()=="") continue;
int id=getKartId(vec[i]); int id=getKartId(vec[i].getKartName());
used[id] = true; used[id] = true;
count --; count --;
} }
// Add karts from the current group // Add karts from the current group
// -------------------------------- // --------------------------------
std::vector<int> karts = getKartsInGroup(user_config->m_kart_group); std::vector<int> karts = getKartsInGroup(user_config->m_kart_group);
std::vector<int>::iterator k; std::vector<int>::iterator k;
// Remove karts that are already used // Remove karts that are already used
for(unsigned int i=0; i<karts.size();) for(unsigned int i=0; i<karts.size();)
{ {
if(used[karts[i]]) if(used[karts[i]])
karts.erase(karts.begin()+i); karts.erase(karts.begin()+i);
else else
i++; i++;
} }
std::srand((unsigned int)std::time(0)); std::srand((unsigned int)std::time(0));
std::random_shuffle(karts.begin(), karts.end()); std::random_shuffle(karts.begin(), karts.end());
// Loop over all karts to fill till either all slots are filled, or // Loop over all karts to fill till either all slots are filled, or
// there are no more karts in the current group // there are no more karts in the current group
for(unsigned int i=0; i<vec.size() && count>0 && karts.size()>0; i++) for(unsigned int i=0; i<vec.size() && count>0 && karts.size()>0; i++)
{ {
if(vec[i].empty()) if(vec[i].getKartName()=="")
{ {
used[karts.back()] = true; used[karts.back()] = true;
vec[i] = m_karts_properties[karts.back()]->getIdent(); vec[i] = RemoteKartInfo(m_karts_properties[karts.back()]->getIdent());
karts.pop_back(); karts.pop_back();
count --; count --;
} }
} }
if(count==0) return; if(count==0) return;
// Not enough karts in chosen group, so fill the rest with arbitrary karts // Not enough karts in chosen group, so fill the rest with arbitrary karts
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// First create an index list with all unused karts. // First create an index list with all unused karts.
karts.clear(); karts.clear();
for(unsigned int i=0; i<getNumberOfKarts(); i++) for(unsigned int i=0; i<getNumberOfKarts(); i++)
{ {
if(!used[i]) karts.push_back(i); if(!used[i]) karts.push_back(i);
} }
std::random_shuffle(karts.begin(), karts.end()); std::random_shuffle(karts.begin(), karts.end());
// Then fill up the remaining empty spaces // Then fill up the remaining empty spaces
for(unsigned int i=0; i<vec.size() && count>0 && karts.size()>0; i++) for(unsigned int i=0; i<vec.size() && count>0 && karts.size()>0; i++)
{ {
if(vec[i].empty()) if(vec[i].getKartName()=="")
{ {
vec[i] = m_karts_properties[karts.back()]->getIdent(); vec[i] = RemoteKartInfo(m_karts_properties[karts.back()]->getIdent());
karts.pop_back(); karts.pop_back();
count --; count --;
} }
} }
// There should never be more karts to be selected than there are. // There should never be more karts to be selected than there are.
assert(count==0); assert(count==0);
} // fillWithRandomKarts } // fillWithRandomKarts
/* EOF */ /* EOF */

View File

@ -1,70 +1,71 @@
// $Id$ // $Id$
// //
// SuperTuxKart - a fun racing game with go-kart // SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2004-2006 Ingo Ruhnke <grumbel@gmx.de> // Copyright (C) 2004-2006 Ingo Ruhnke <grumbel@gmx.de>
// //
// This program is free software; you can redistribute it and/or // This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License // modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3 // as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version. // of the License, or (at your option) any later version.
// //
// This program is distributed in the hope that it will be useful, // This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of // but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details. // GNU General Public License for more details.
// //
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software // along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#ifndef HEADER_KARTPROPERTIESMANAGER_H #ifndef HEADER_KARTPROPERTIESMANAGER_H
#define HEADER_KARTPROPERTIESMANAGER_H #define HEADER_KARTPROPERTIESMANAGER_H
#include <vector> #include <vector>
#include <map> #include <map>
#include "network/remote_kart_info.hpp"
class KartProperties;
class KartProperties;
class KartPropertiesManager
{ class KartPropertiesManager
private: {
std::vector<std::string> m_all_groups; private:
std::map<std::string, std::vector<int> > m_groups; std::vector<std::string> m_all_groups;
protected: std::map<std::string, std::vector<int> > m_groups;
float m_max_steer_angle; protected:
float m_max_steer_angle;
typedef std::vector<KartProperties*> KartPropertiesVector;
/** All available kart configurations */ typedef std::vector<KartProperties*> KartPropertiesVector;
KartPropertiesVector m_karts_properties; /** All available kart configurations */
KartPropertiesVector m_karts_properties;
public:
KartPropertiesManager(); public:
~KartPropertiesManager(); 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 // vector containing kart numbers that have been selected in multiplayer
// than once. // games. This it used to ensure the same kart can not be selected more
std::vector<int> m_selected_karts; // than once.
std::vector<int> m_selected_karts;
const KartProperties* getKartById (int i) const;
const KartProperties* getKart (const std::string IDENT) const; const KartProperties* getKartById (int i) const;
const int getKartId (const std::string IDENT) const; const KartProperties* getKart (const std::string IDENT) const;
int getKartByGroup (const std::string& group, int i) const; const int getKartId (const std::string IDENT) const;
void loadKartData (bool dont_load_models=false); int getKartByGroup (const std::string& group, int i) const;
const float getMaximumSteeringAngle() const {return m_max_steer_angle;} void loadKartData (bool dont_load_models=false);
const unsigned int getNumberOfKarts () const {return (unsigned int)m_karts_properties.size();} const float getMaximumSteeringAngle() const {return m_max_steer_angle;}
const std::vector<std::string>& const unsigned int getNumberOfKarts () const {return (unsigned int)m_karts_properties.size();}
getAllGroups () const {return m_all_groups; } const std::vector<std::string>&
const std::vector<int>& getKartsInGroup (const std::string& g) getAllGroups () const {return m_all_groups; }
{return m_groups[g]; } 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); /** Fill the empty positions in the given vector with random karts */
void removeTextures (); void fillWithRandomKarts (std::vector<RemoteKartInfo>& vec);
}; void removeTextures ();
};
extern KartPropertiesManager *kart_properties_manager;
extern KartPropertiesManager *kart_properties_manager;
#endif
#endif
/* EOF */
/* 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"); std::string filename=file_manager->getKartFile(std::string(argv[i+1])+".tkkf");
if(filename!="") 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] ) ; fprintf ( stdout, "You choose to use kart '%s'.\n", argv[i+1] ) ;
} }
else else
@ -555,7 +555,7 @@ int main(int argc, char *argv[] )
// Profiling // Profiling
// ========= // =========
race_manager->setNumPlayers(1); 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->setMajorMode (RaceManager::RM_SINGLE);
race_manager->setMinorMode (RaceManager::RM_QUICK_RACE); race_manager->setMinorMode (RaceManager::RM_QUICK_RACE);
race_manager->setDifficulty(RaceManager::RD_HARD); race_manager->setDifficulty(RaceManager::RD_HARD);

View File

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

View File

@ -59,14 +59,16 @@ private:
public: public:
NetworkManager(); NetworkManager();
~NetworkManager(); ~NetworkManager();
void setMode(NetworkMode m) {m_mode = m; } void setMode(NetworkMode m) {m_mode = m; }
NetworkMode getMode() const {return m_mode; } NetworkMode getMode() const {return m_mode; }
void setState(NetworkState s) {m_state = s; } void setState(NetworkState s) {m_state = s; }
NetworkState getState() const {return m_state; } NetworkState getState() const {return m_state; }
int getClientId() const {return m_host_id; } int getHostId() const {return m_host_id; }
int getNumClients() const {return m_num_clients;} int getNumClients() const {return m_num_clients; }
void setPort(int p) {m_port=p; } void setPort(int p) {m_port=p; }
void setServerIP(const std::string &s) {m_server_address=s; } 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(); bool initialiseConnections();
void update(float dt); void update(float dt);
void sendKartsInformationToServer(); void sendKartsInformationToServer();

View File

@ -26,19 +26,36 @@ class RemoteKartInfo
{ {
std::string m_kart_name; std::string m_kart_name;
std::string m_user_name; std::string m_user_name;
int m_client_id; int m_host_id;
int m_player_id; int m_local_player_id;
int m_global_player_id;
public: public:
RemoteKartInfo(std::string kart_name, std::string user_name, int id) RemoteKartInfo(int player_id, const std::string& kart_name,
: m_kart_name(kart_name), m_user_name(user_name), m_client_id(id) const std::string& user_name, int host_id)
{}; : m_kart_name(kart_name), m_user_name(user_name),
RemoteKartInfo() {m_kart_name=""; m_user_name=""; m_client_id=-1; m_player_id=-1;} m_local_player_id(player_id), m_host_id(host_id)
void setKartName(const std::string& n) { m_kart_name = n; } {};
void setUserName(const std::string& u) { m_user_name = u; } RemoteKartInfo(const std::string& kart_name)
void setClientId(int id) { m_client_id = id; } {m_kart_name=kart_name; m_user_name="";
void setPlayerId(int id) { m_player_id = id; } m_host_id=-1; m_local_player_id=-1;}
const std::string& getKartName() const {return m_kart_name; } 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 }; // RemoteKartInfo
#endif #endif

View File

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

View File

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

View File

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