diff --git a/src/gui/start_race_feedback.cpp b/src/gui/start_race_feedback.cpp index 48c1040b5..2cca446b5 100644 --- a/src/gui/start_race_feedback.cpp +++ b/src/gui/start_race_feedback.cpp @@ -70,22 +70,20 @@ void StartRaceFeedback::update(float delta) return; } - static bool first=true; - if(first) + if(network_manager->getMode()==NetworkManager::NW_SERVER) { - if(network_manager->getMode()==NetworkManager::NW_SERVER) - { - network_manager->sendRaceInformationToClients(); - widget_manager->setWgtText(WTOK_MSG, m_loading_text); - } - else if(network_manager->getMode()==NetworkManager::NW_CLIENT) - { - // Client received race information - widget_manager->setWgtText(WTOK_MSG, m_loading_text); - } - first=false; + network_manager->sendRaceInformationToClients(); + widget_manager->setWgtText(WTOK_MSG, m_loading_text); + } + else if(network_manager->getMode()==NetworkManager::NW_CLIENT) + { + // Client received race information + widget_manager->setWgtText(WTOK_MSG, m_loading_text); } + widget_manager->update(delta); + + // Pops this menu race_manager->startNew(); } // update diff --git a/src/network/message.hpp b/src/network/message.hpp index b8c1ace0c..6b82affd6 100644 --- a/src/network/message.hpp +++ b/src/network/message.hpp @@ -21,6 +21,7 @@ #define HEADER_MESSAGE_H #include +#include #include #ifdef HAVE_ENET @@ -46,18 +47,26 @@ private: protected: bool add(int data); +#ifndef WIN32 // on windows size_t is unsigned int bool add(size_t data) { return add((int)data); } +#endif bool add(unsigned int data) { return add((int)data); } bool add(float data); bool add(const std::string &data); + bool add(const std::vector& vs); int getInt(); float getFloat(); std::string getString(); - int getLength(const std::string& s) { return s.size()+1; } + std::vector + getStringVector(); int getLength(int n) { return sizeof(int); } int getLength(unsigned int n) { return sizeof(int); } - int getLength(size_t n) { return sizeof(int); } int getLength(float f) { return sizeof(float); } + int getLength(const std::string& s) { return s.size()+1; } + int getLength(const std::vector& vs); +#ifndef WIN32 + int getLength(size_t n) { return sizeof(int); } +#endif public: Message(MessageType m); // create from scratch (to send) diff --git a/src/network/network_manager.cpp b/src/network/network_manager.cpp index aef4b3f0f..a48b00f55 100644 --- a/src/network/network_manager.cpp +++ b/src/network/network_manager.cpp @@ -240,6 +240,7 @@ void NetworkManager::handleMessageAtServer(ENetEvent *event) RaceStartMessage m; broadcastToClients(m); } + break; } default: assert(0); // should not happen } // switch m_state @@ -266,6 +267,9 @@ void NetworkManager::handleMessageAtClient(ENetEvent *event) } case NS_READY_SET_GO_BARRIER: { + // Not actually needed, but the destructor of RaceStartMessage + // will free the memory of the event. + RaceStartMessage m(event->packet); m_state = NS_RACING; break; } @@ -323,7 +327,7 @@ void NetworkManager::switchToCharacterSelection() // Change state to wait for list of characters from server m_state = NS_WAIT_FOR_AVAILABLE_CHARACTERS; } - else + else if(m_mode==NW_SERVER) { // server: create message with all valid characters // ================================================ CharacterInfoMessage m; @@ -365,14 +369,8 @@ void NetworkManager::worldLoaded() { WorldLoadedMessage m; sendToServer(&m); + m_state = NS_READY_SET_GO_BARRIER; } - else if(m_mode==NW_SERVER) - { - assert(m_state=NS_READY_SET_GO_BARRIER); - RaceStartMessage m; - broadcastToClients(m); - } - } // worldLoaded // ---------------------------------------------------------------------------- @@ -398,6 +396,7 @@ void NetworkManager::setupPlayerKartInfo() { race_manager->setPlayerKart(i, m_kart_info[i]); } + race_manager->computeRandomKartList(); } // setupPlayerKartInfo // ---------------------------------------------------------------------------- @@ -405,9 +404,11 @@ void NetworkManager::setupPlayerKartInfo() */ void NetworkManager::sendRaceInformationToClients() { - setupPlayerKartInfo(); - RaceInfoMessage m(m_kart_info); - broadcastToClients(m); + if(m_mode==NW_SERVER) + { + RaceInfoMessage m(m_kart_info); + broadcastToClients(m); + } m_state = NS_READY_SET_GO_BARRIER; m_barrier_count = 0; if(m_num_clients==0) m_state = NS_RACING; diff --git a/src/network/race_info_message.cpp b/src/network/race_info_message.cpp index f98e5a1e9..fcbe821f8 100644 --- a/src/network/race_info_message.cpp +++ b/src/network/race_info_message.cpp @@ -27,15 +27,18 @@ RaceInfoMessage::RaceInfoMessage(const std::vector& kart_info) const GrandPrixData *cup=NULL; int len = getLength(race_manager->getMajorMode() ) + getLength(race_manager->getMinorMode() ) - + getLength(race_manager->getDifficulty()); + + getLength(race_manager->getDifficulty()) + + getLength(race_manager->getNumKarts() ); if(race_manager->getMajorMode()==RaceManager::RM_GRAND_PRIX) { cup = race_manager->getGrandPrix(); len += getLength(cup->getId()); } else + { len += getLength(race_manager->getTrackName()); - + len += getLength(race_manager->getNumLaps()); + } len += getLength(kart_info.size()); for(unsigned int i=0; i& kart_info) + getLength(kart_info[i].getLocalPlayerId()) + getLength(kart_info[i].getPlayerName()); } + const std::vector& rkl=race_manager->getRandomKartList(); + len += getLength(rkl); + allocate(len); add(race_manager->getMajorMode() ); add(race_manager->getMinorMode() ); add(race_manager->getDifficulty()); + add(race_manager->getNumKarts() ); if(race_manager->getMajorMode()==RaceManager::RM_GRAND_PRIX) add(cup->getName()); else + { add(race_manager->getTrackName()); + add(race_manager->getNumLaps()); + } add(kart_info.size()); for(unsigned int i=0; i& kart_info) add(kart_info[i].getLocalPlayerId()); add(kart_info[i].getPlayerName()); } - + add(rkl); } // RaceInfoMessage // ---------------------------------------------------------------------------- @@ -72,6 +82,7 @@ RaceInfoMessage::RaceInfoMessage(ENetPacket* pkt):Message(pkt, MT_RACE_INFO) race_manager->setMajorMode ( RaceManager::RaceModeType(getInt()) ); race_manager->setMinorMode ( RaceManager::RaceModeType(getInt()) ); race_manager->setDifficulty( RaceManager::Difficulty (getInt()) ); + race_manager->setNumKarts ( getInt() ); if(race_manager->getMajorMode()==RaceManager::RM_GRAND_PRIX) { GrandPrixData cup; @@ -79,7 +90,10 @@ RaceInfoMessage::RaceInfoMessage(ENetPacket* pkt):Message(pkt, MT_RACE_INFO) race_manager->setGrandPrix(cup); } else + { race_manager->setTrack(getString()); + race_manager->setNumLaps(getInt()); + } std::vector kart_info; kart_info.resize(getInt()); @@ -99,5 +113,6 @@ RaceInfoMessage::RaceInfoMessage(ENetPacket* pkt):Message(pkt, MT_RACE_INFO) { race_manager->setPlayerKart(i, kart_info[i]); } - + std::vector rkl=getStringVector(); + race_manager->setRandomKartList(rkl); } // RaceInfoMessage diff --git a/src/network/race_start_message.hpp b/src/network/race_start_message.hpp index 76530d822..2ff36fa14 100644 --- a/src/network/race_start_message.hpp +++ b/src/network/race_start_message.hpp @@ -34,7 +34,7 @@ public: allocate(0); } // RaceStartMessage - RaceStartMessage(ENetPacket* pkt):Message(pkt, MT_RACE_INFO) + RaceStartMessage(ENetPacket* pkt):Message(pkt, MT_RACE_START) { } }; // RaceStartMessage