From b38f6b20a1fcaa95c1e8f635d713e13a0bc2554b Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Sat, 16 Feb 2013 10:07:36 +0000 Subject: [PATCH] - updates to join in progress game, and now allow for switching slots --- source/glest_game/ai/ai_interface.cpp | 4 +- source/glest_game/ai/ai_interface.h | 3 +- source/glest_game/game/game.cpp | 172 +++++++++++++++++- source/glest_game/game/game.h | 5 + .../menu/menu_state_connected_game.cpp | 45 ++++- .../menu/menu_state_custom_game.cpp | 8 +- .../glest_game/network/client_interface.cpp | 24 ++- source/glest_game/network/client_interface.h | 4 +- source/glest_game/network/connection_slot.cpp | 40 +++- source/glest_game/network/connection_slot.h | 3 + source/glest_game/network/network_message.cpp | 31 ++-- source/glest_game/network/network_message.h | 8 +- .../glest_game/network/server_interface.cpp | 42 +++++ source/glest_game/network/server_interface.h | 14 +- 14 files changed, 344 insertions(+), 59 deletions(-) diff --git a/source/glest_game/ai/ai_interface.cpp b/source/glest_game/ai/ai_interface.cpp index e504514d..6b9fe3f1 100644 --- a/source/glest_game/ai/ai_interface.cpp +++ b/source/glest_game/ai/ai_interface.cpp @@ -252,7 +252,9 @@ AiInterface::~AiInterface() { if(workerThread != NULL) { workerThread->signalQuit(); - if(workerThread->shutdownAndWait() == true) { + sleep(0); + if(workerThread->canShutdown(true) == true && + workerThread->shutdownAndWait() == true) { delete workerThread; } workerThread = NULL; diff --git a/source/glest_game/ai/ai_interface.h b/source/glest_game/ai/ai_interface.h index 5bd4346a..2534d6cf 100644 --- a/source/glest_game/ai/ai_interface.h +++ b/source/glest_game/ai/ai_interface.h @@ -42,7 +42,6 @@ protected: virtual void setQuitStatus(bool value); virtual void setTaskCompleted(int frameIndex); - virtual bool canShutdown(bool deleteSelfIfShutdownDelayed=false); public: AiInterfaceThread(AiInterface *aiIntf); @@ -55,7 +54,7 @@ public: virtual void signalSlave(void *userdata) { signal(*((int *)(userdata))); } virtual void signalQuit(); - + virtual bool canShutdown(bool deleteSelfIfShutdownDelayed=false); }; class AiInterface { diff --git a/source/glest_game/game/game.cpp b/source/glest_game/game/game.cpp index fb7a9a37..d9739ad1 100644 --- a/source/glest_game/game/game.cpp +++ b/source/glest_game/game/game.cpp @@ -1626,11 +1626,13 @@ void Game::update() { server->setStartInGameConnectionLaunch(false); + Lang &lang= Lang::getInstance(); + bool pauseAndSaveGameForNewClient = false; for(int i = 0; i < world.getFactionCount(); ++i) { Faction *faction = world.getFaction(i); ConnectionSlot *slot = server->getSlot(faction->getStartLocationIndex()); if(slot != NULL && slot->getJoinGameInProgress() == true) { - //printf("$$$ signalling client to start game!\n"); + //printf("$$$ signalling client to start game [deleting AI player] factionIndex: %d slot: %d startlocation: %d!\n",i,slot->getPlayerIndex(),faction->getStartLocationIndex()); this->gameSettings.setFactionControl(i,ctNetwork); this->gameSettings.setNetworkPlayerName(i,server->gameSettings.getNetworkPlayerName(i)); @@ -1641,8 +1643,22 @@ void Game::update() { aiInterfaces[i] = NULL; //printf("END Purging AI player for index: %d\n",i); - commander.tryPauseGame(); + Faction *faction = world.getFaction(i); + faction->setControlType(ctNetwork); + + pauseAndSaveGameForNewClient = true; } + else if((slot == NULL || slot->isConnected() == false) && + this->gameSettings.getFactionControl(i) == ctNetwork && + aiInterfaces[i] == NULL) { + faction->setFactionDisconnectHandled(false); + //this->gameSettings.setNetworkPlayerName(i,lang.get("AI") + intToStr(i+1)); + //server->gameSettings.setNetworkPlayerName(i,lang.get("AI") + intToStr(i+1)); + } + } + + if(pauseAndSaveGameForNewClient == true) { + commander.tryPauseGame(); } } else if(server->getUnPauseForInGameConnection() == true && paused == true) { @@ -1651,6 +1667,26 @@ void Game::update() { server->setUnPauseForInGameConnection(false); commander.tryResumeGame(); } + else { + // handle setting changes from clients + Map *map= world.getMap(); + //printf("switchSetupRequests != NULL\n"); + + bool switchRequested = switchSetupForSlots(server, 0, map->getMaxPlayers(), false); + switchRequested = switchRequested || switchSetupForSlots(server, map->getMaxPlayers(), GameConstants::maxPlayers, true); + + if(switchRequested == true) { + //printf("Send new game setup from switch: %d\n",switchRequested); + + //for(int i= 0; i < gameSettings.getFactionCount(); ++i) { + //printf("#1 Faction Index: %d control: %d startlocation: %d\n",i,gameSettings.getFactionControl(i),gameSettings.getStartLocationIndex(i)); + + //printf("#2 Faction Index: %d control: %d startlocation: %d\n",i,server->gameSettings.getFactionControl(i),server->gameSettings.getStartLocationIndex(i)); + //} + + server->broadcastGameSetup(&server->gameSettings,true); + } + } } // Check to see if we are playing a network game and if any players @@ -2172,6 +2208,137 @@ void Game::update() { } } +bool Game::switchSetupForSlots(ServerInterface *& serverInterface, + int startIndex, int endIndex, bool onlyNetworkUnassigned) { + bool switchRequested = false; + if(serverInterface == NULL) { + return switchRequested; + } + + MutexSafeWrapper safeMutex(serverInterface->getSwitchSetupRequestsMutex(),CODE_AT_LINE); + SwitchSetupRequest ** switchSetupRequests = serverInterface->getSwitchSetupRequests(); + if(switchSetupRequests == NULL) { + return switchRequested; + } + + Map *map= world.getMap(); + for(int i= startIndex; i < endIndex; ++i) { + if(switchSetupRequests[i] != NULL) { + //printf("Faction Index: %d Switch slot = %d to = %d current control = %d\n",i,switchSetupRequests[i]->getCurrentSlotIndex(),switchSetupRequests[i]->getToSlotIndex(),gameSettings.getFactionControl(i)); + + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] switchSetupRequests[i]->getSwitchFlags() = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,switchSetupRequests[i]->getSwitchFlags()); + + if(onlyNetworkUnassigned == true && gameSettings.getFactionControl(i) != ctNetworkUnassigned) { + if(i < map->getMaxPlayers() || (i >= map->getMaxPlayers() && gameSettings.getFactionControl(i) != ctNetwork)) { + continue; + } + } + + if(gameSettings.getFactionControl(i) == ctNetwork || + gameSettings.getFactionControl(i) == ctNetworkUnassigned || + //(gameSettings.getFactionControl(i) != ctClosed && gameSettings.getFactionControl(i) != ctHuman)) { + (gameSettings.getFactionControl(i) != ctHuman)) { + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] switchSetupRequests[i]->getToFactionIndex() = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,switchSetupRequests[i]->getToSlotIndex()); + + if(switchSetupRequests[i]->getToSlotIndex() != -1) { + int newSlotIdx = switchSetupRequests[i]->getToSlotIndex(); + + //printf("switchSlot request from %d to %d\n",switchSetupRequests[i]->getCurrentSlotIndex(),switchSetupRequests[i]->getToSlotIndex()); + + int switchSlotIdx = switchSetupRequests[i]->getCurrentSlotIndex(); + if(serverInterface->switchSlot(switchSlotIdx,newSlotIdx)) { + //printf("switchSlot returned true\n"); + switchRequested = true; + + int oldFactionIndex = gameSettings.getFactionIndexForStartLocation(switchSlotIdx); + int newFactionIndex = gameSettings.getFactionIndexForStartLocation(newSlotIdx); + + //printf("Switching faction for index %d [%d] to %d\n",newSlotIdx,switchSlotIdx,gameSettings.getFactionControl(newFactionIndex)); + + gameSettings.setNetworkPlayerName(oldFactionIndex, ""); + serverInterface->gameSettings.setNetworkPlayerName(oldFactionIndex, ""); + + gameSettings.setFactionControl(newFactionIndex,ctNetwork); + serverInterface->gameSettings.setFactionControl(newFactionIndex,ctNetwork); + + //printf("#1a Faction Index: %d control: %d startlocation: %d\n",newFactionIndex,gameSettings.getFactionControl(newFactionIndex),gameSettings.getStartLocationIndex(newFactionIndex)); + //printf("#2a Faction Index: %d control: %d startlocation: %d\n",newFactionIndex,serverInterface->gameSettings.getFactionControl(newFactionIndex),serverInterface->gameSettings.getStartLocationIndex(newFactionIndex)); + + try { + //if(switchSetupRequests[i]->getSelectedFactionName() != "") { + // listBoxFactions[newFactionIdx].setSelectedItem(switchSetupRequests[i]->getSelectedFactionName()); + //} + //if(switchSetupRequests[i]->getToTeam() != -1) { + // listBoxTeams[newFactionIdx].setSelectedItemIndex(switchSetupRequests[i]->getToTeam()); + //} + if(switchSetupRequests[i]->getNetworkPlayerName() != "") { + //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d] i = %d, labelPlayerNames[newFactionIdx].getText() [%s] switchSetupRequests[i]->getNetworkPlayerName() [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,i,labelPlayerNames[newFactionIdx].getText().c_str(),switchSetupRequests[i]->getNetworkPlayerName().c_str()); + gameSettings.setNetworkPlayerName(newFactionIndex,switchSetupRequests[i]->getNetworkPlayerName()); + serverInterface->gameSettings.setNetworkPlayerName(newFactionIndex,switchSetupRequests[i]->getNetworkPlayerName()); + } + +// if(gameSettings.getFactionControl(switchFactionIdx) == ctNetworkUnassigned) { +// serverInterface->removeSlot(switchFactionIdx); +// //listBoxControls[switchFactionIdx].setSelectedItemIndex(ctClosed); +// gameSettings.getFactionControl(switchFactionIdx) +// +// labelPlayers[switchFactionIdx].setVisible(switchFactionIdx+1 <= mapInfo.players); +// labelPlayerNames[switchFactionIdx].setVisible(switchFactionIdx+1 <= mapInfo.players); +// listBoxControls[switchFactionIdx].setVisible(switchFactionIdx+1 <= mapInfo.players); +// listBoxFactions[switchFactionIdx].setVisible(switchFactionIdx+1 <= mapInfo.players); +// listBoxTeams[switchFactionIdx].setVisible(switchFactionIdx+1 <= mapInfo.players); +// labelNetStatus[switchSlotIdx].setVisible(switchSlotIdx+1 <= mapInfo.players); +// } + } + catch(const runtime_error &e) { + SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,e.what()); + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] caught exception error = [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,e.what()); + } + } + } + else { + try { + //if(switchSetupRequests[i]->getSelectedFactionName() != "") { + // listBoxFactions[i].setSelectedItem(switchSetupRequests[i]->getSelectedFactionName()); + //} + //if(switchSetupRequests[i]->getToTeam() != -1) { + // listBoxTeams[i].setSelectedItemIndex(switchSetupRequests[i]->getToTeam()); + //} + + if((switchSetupRequests[i]->getSwitchFlags() & ssrft_NetworkPlayerName) == ssrft_NetworkPlayerName) { + //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] i = %d, switchSetupRequests[i]->getSwitchFlags() = %d, switchSetupRequests[i]->getNetworkPlayerName() [%s], labelPlayerNames[i].getText() [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,i,switchSetupRequests[i]->getSwitchFlags(),switchSetupRequests[i]->getNetworkPlayerName().c_str(),labelPlayerNames[i].getText().c_str()); + + if(switchSetupRequests[i]->getNetworkPlayerName() != GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME) { + //labelPlayerNames[i].setText(switchSetupRequests[i]->getNetworkPlayerName()); + gameSettings.setNetworkPlayerName(i,switchSetupRequests[i]->getNetworkPlayerName()); + serverInterface->gameSettings.setNetworkPlayerName(i,switchSetupRequests[i]->getNetworkPlayerName()); + switchRequested = true; + } + else { + //labelPlayerNames[i].setText(""); + gameSettings.setNetworkPlayerName(i,""); + serverInterface->gameSettings.setNetworkPlayerName(i,""); + switchRequested = true; + } + //SetActivePlayerNameEditor(); + //switchSetupRequests[i]->clearSwitchFlag(ssrft_NetworkPlayerName); + } + } + catch(const runtime_error &e) { + SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,e.what()); + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] caught exception error = [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,e.what()); + } + } + } + + delete switchSetupRequests[i]; + switchSetupRequests[i]=NULL; + } + } + + return switchRequested; +} + void Game::updateNetworkMarkedCells() { try { GameNetworkInterface *gameNetworkInterface= NetworkManager::getInstance().getGameNetworkInterface(); @@ -2227,6 +2394,7 @@ void Game::updateNetworkUnMarkedCells() { } } + void Game::updateNetworkHighligtedCells() { try { GameNetworkInterface *gameNetworkInterface= NetworkManager::getInstance().getGameNetworkInterface(); diff --git a/source/glest_game/game/game.h b/source/glest_game/game/game.h index b9a0c222..5cdb133c 100644 --- a/source/glest_game/game/game.h +++ b/source/glest_game/game/game.h @@ -42,6 +42,7 @@ namespace Shared { namespace Graphics { namespace Glest{ namespace Game{ class GraphicMessageBox; +class ServerInterface; enum LoadGameItem { lgt_FactionPreview = 0x01, @@ -359,6 +360,10 @@ private: void startMarkCell(); void startCameraFollowUnit(); + + bool switchSetupForSlots(ServerInterface *& serverInterface, + int startIndex, int endIndex, bool onlyNetworkUnassigned); + }; }}//end namespace diff --git a/source/glest_game/menu/menu_state_connected_game.cpp b/source/glest_game/menu/menu_state_connected_game.cpp index f711b9f3..1b9e18b7 100644 --- a/source/glest_game/menu/menu_state_connected_game.cpp +++ b/source/glest_game/menu/menu_state_connected_game.cpp @@ -1505,15 +1505,22 @@ void MenuStateConnectedGame::mouseClick(int x, int y, MouseButton mouseButton){ break; } } - //if(grabSlotButton[i].mouseClick(x, y)) { - // printf("Send slot switch request for slot = %d, myCurrentIndex = %d name [%s] control = %d, mapInfo.players = %d\n",i,myCurrentIndex,labelNetStatus[i].getText().c_str(),listBoxControls[i].getSelectedItemIndex(),mapInfo.players); - //} - //else { - // printf("No Click i = %d!\n",i); - //} - if(listBoxControls[i].getSelectedItemIndex() == ctNetwork && - labelNetStatus[i].getText() == GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME) { + bool canGrabSlot = false; + ClientInterface *clientInterface = networkManager.getClientInterface(); + if(clientInterface != NULL && clientInterface->getJoinGameInProgress() == true) { + canGrabSlot = ((listBoxControls[i].getSelectedItemIndex() == ctNetwork && + labelNetStatus[i].getText() == GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME) || + (listBoxControls[i].getSelectedItemIndex() != ctHuman && + listBoxControls[i].getSelectedItemIndex() != ctClosed && + listBoxControls[i].getSelectedItemIndex() != ctNetwork)); + } + else { + canGrabSlot = (listBoxControls[i].getSelectedItemIndex() == ctNetwork && + labelNetStatus[i].getText() == GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME); + } + + if(canGrabSlot == true) { if(i < mapInfo.players && grabSlotButton[i].mouseClick(x, y)) { //printf("Send slot switch request for slot = %d, myCurrentIndex = %d\n",i,myCurrentIndex); @@ -1532,6 +1539,8 @@ void MenuStateConnectedGame::mouseClick(int x, int y, MouseButton mouseButton){ desiredTeamIndex = listBoxTeams[i].getSelectedItemIndex(); } + //printf("Sending switch slot request to server...\n"); + clientInterface->sendSwitchSetupRequest( desiredFactionName, myCurrentIndex, @@ -2505,8 +2514,21 @@ void MenuStateConnectedGame::render() { renderer.renderListBox(&listBoxFactions[i]); renderer.renderListBox(&listBoxTeams[i]); - if((listBoxControls[i].getSelectedItemIndex() == ctNetwork) && - (labelNetStatus[i].getText() == GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME)) { + bool canGrabSlot = false; + ClientInterface *clientInterface = networkManager.getClientInterface(); + if(clientInterface != NULL && clientInterface->getJoinGameInProgress() == true) { + canGrabSlot = ((listBoxControls[i].getSelectedItemIndex() == ctNetwork && + labelNetStatus[i].getText() == GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME) || + (listBoxControls[i].getSelectedItemIndex() != ctHuman && + listBoxControls[i].getSelectedItemIndex() != ctClosed && + listBoxControls[i].getSelectedItemIndex() != ctNetwork)); + } + else { + canGrabSlot = (listBoxControls[i].getSelectedItemIndex() == ctNetwork && + labelNetStatus[i].getText() == GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME); + } + + if(canGrabSlot == true) { if(i < mapInfo.players) { renderer.renderButton(&grabSlotButton[i]); } @@ -3183,6 +3205,9 @@ void MenuStateConnectedGame::update() { lastGameSettingsReceivedCount = clientInterface->getGameSettingsReceivedCount(); bool errorOnMissingData = (clientInterface->getAllowGameDataSynchCheck() == false); GameSettings *gameSettings = clientInterface->getGameSettingsPtr(); + + //printf("Menu got new settings thisfactionindex = %d startlocation: %d control = %d\n",gameSettings->getThisFactionIndex(),clientInterface->getGameSettings()->getStartLocationIndex(clientInterface->getGameSettings()->getThisFactionIndex()),gameSettings->getFactionControl(clientInterface->getGameSettings()->getThisFactionIndex())); + setupUIFromGameSettings(gameSettings, errorOnMissingData); // // check if we are joining an in progress game diff --git a/source/glest_game/menu/menu_state_custom_game.cpp b/source/glest_game/menu/menu_state_custom_game.cpp index c321b341..618e3b18 100644 --- a/source/glest_game/menu/menu_state_custom_game.cpp +++ b/source/glest_game/menu/menu_state_custom_game.cpp @@ -2129,13 +2129,13 @@ void MenuStateCustomGame::switchSetupForSlots(SwitchSetupRequest **switchSetupRe } if(listBoxControls[i].getSelectedItemIndex() == ctNetwork || listBoxControls[i].getSelectedItemIndex() == ctNetworkUnassigned) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] switchSetupRequests[i]->getToFactionIndex() = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,switchSetupRequests[i]->getToFactionIndex()); + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] switchSetupRequests[i]->getToFactionIndex() = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,switchSetupRequests[i]->getToSlotIndex()); - if(switchSetupRequests[i]->getToFactionIndex() != -1) { - int newFactionIdx = switchSetupRequests[i]->getToFactionIndex(); + if(switchSetupRequests[i]->getToSlotIndex() != -1) { + int newFactionIdx = switchSetupRequests[i]->getToSlotIndex(); //printf("switchSlot request from %d to %d\n",switchSetupRequests[i]->getCurrentFactionIndex(),switchSetupRequests[i]->getToFactionIndex()); - int switchFactionIdx = switchSetupRequests[i]->getCurrentFactionIndex(); + int switchFactionIdx = switchSetupRequests[i]->getCurrentSlotIndex(); if(serverInterface->switchSlot(switchFactionIdx,newFactionIdx)) { try { if(switchSetupRequests[i]->getSelectedFactionName() != "") { diff --git a/source/glest_game/network/client_interface.cpp b/source/glest_game/network/client_interface.cpp index 53d992f1..529f7e2c 100644 --- a/source/glest_game/network/client_interface.cpp +++ b/source/glest_game/network/client_interface.cpp @@ -565,6 +565,8 @@ void ClientInterface::updateLobby() { case nmtLaunch: case nmtBroadCastSetup: { + //printf("#1 Got new game setup playerIndex = %d!\n",playerIndex); + NetworkMessageLaunch networkMessageLaunch; if(receiveMessage(&networkMessageLaunch)) { if(networkMessageLaunch.getMessageType() == nmtLaunch) { @@ -596,8 +598,12 @@ void ClientInterface::updateLobby() { gameSettings.setFactionControl(i, ctNetwork); } - //set the faction index - if(gameSettings.getStartLocationIndex(i) == playerIndex) { + //printf("i = %d gameSettings.getStartLocationIndex(i) = %d playerIndex = %d, gameSettings.getFactionControl(i) = %d\n",i,gameSettings.getStartLocationIndex(i),playerIndex,gameSettings.getFactionControl(i)); + + //set the faction index + if(gameSettings.getStartLocationIndex(i) == playerIndex) { + //printf("Setting my factionindex to: %d for playerIndex: %d\n",i,playerIndex); + gameSettings.setThisFactionIndex(i); //printf("Client got game settings playerIndex = %d factionIndex = %d control = %d name = %s\n",playerIndex,i,gameSettings.getFactionControl(i),gameSettings.getFactionTypeName(i).c_str()); @@ -622,6 +628,8 @@ void ClientInterface::updateLobby() { if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] got nmtPlayerIndexMessage, playerIndex = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,playerIndex); } + + //printf("Got player index changed msg: %d\n",playerIndex); } break; @@ -839,6 +847,8 @@ void ClientInterface::updateFrame(int *checkFrame) { case nmtLaunch: case nmtBroadCastSetup: { + //printf("#2 Got new game setup playerIndex = %d!\n",playerIndex); + NetworkMessageLaunch networkMessageLaunch; if(receiveMessage(&networkMessageLaunch)) { if(networkMessageLaunch.getMessageType() == nmtLaunch) { @@ -865,8 +875,12 @@ void ClientInterface::updateFrame(int *checkFrame) { gameSettings.setFactionControl(i, ctNetwork); } + //printf("i = %d gameSettings.getStartLocationIndex(i) = %d playerIndex = %d!\n",i,gameSettings.getStartLocationIndex(i),playerIndex); + //set the faction index if(gameSettings.getStartLocationIndex(i) == playerIndex) { + //printf("Setting my factionindex to: %d for playerIndex: %d\n",i,playerIndex); + gameSettings.setThisFactionIndex(i); if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] gameSettings.getThisFactionIndex(i) = %d, playerIndex = %d, i = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,gameSettings.getThisFactionIndex(),playerIndex,i); } @@ -1511,13 +1525,13 @@ void ClientInterface::stopServerDiscovery() { if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); } -void ClientInterface::sendSwitchSetupRequest(string selectedFactionName, int8 currentFactionIndex, - int8 toFactionIndex,int8 toTeam, string networkPlayerName, +void ClientInterface::sendSwitchSetupRequest(string selectedFactionName, int8 currentSlotIndex, + int8 toSlotIndex,int8 toTeam, string networkPlayerName, int8 networkPlayerStatus, int8 flags, string language) { if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] networkPlayerName [%s] flags = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkPlayerName.c_str(),flags); SwitchSetupRequest message=SwitchSetupRequest(selectedFactionName, - currentFactionIndex, toFactionIndex,toTeam,networkPlayerName, + currentSlotIndex, toSlotIndex,toTeam,networkPlayerName, networkPlayerStatus, flags,language); sendMessage(&message); if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); diff --git a/source/glest_game/network/client_interface.h b/source/glest_game/network/client_interface.h index 2d584123..062ed0bd 100644 --- a/source/glest_game/network/client_interface.h +++ b/source/glest_game/network/client_interface.h @@ -118,8 +118,8 @@ public: void discoverServers(DiscoveredServersInterface *cb); void stopServerDiscovery(); - void sendSwitchSetupRequest(string selectedFactionName, int8 currentFactionIndex, - int8 toFactionIndex, int8 toTeam,string networkPlayerName, + void sendSwitchSetupRequest(string selectedFactionName, int8 currentSlotIndex, + int8 toSlotIndex, int8 toTeam,string networkPlayerName, int8 networkPlayerStatus, int8 flags, string language); virtual bool getConnectHasHandshaked() const { return gotIntro; } diff --git a/source/glest_game/network/connection_slot.cpp b/source/glest_game/network/connection_slot.cpp index fe655444..9f26ff4b 100644 --- a/source/glest_game/network/connection_slot.cpp +++ b/source/glest_game/network/connection_slot.cpp @@ -858,8 +858,9 @@ void ConnectionSlot::update(bool checkForNewClients,int lockedSlotIndex) { serverInterface->getAllowInGameConnections() == true) { //printf("Sent intro to client connection on slot!\n"); - this->skipLagCheck = true; - this->joinGameInProgress = true; + //this->skipLagCheck = true; + //this->joinGameInProgress = true; + setJoinGameInProgressFlags(); serverInterface->setPauseForInGameConnection(true); //printf("Got intro from client sending game settings..\n"); @@ -1170,7 +1171,7 @@ void ConnectionSlot::update(bool checkForNewClients,int lockedSlotIndex) { case nmtSwitchSetupRequest: { - //printf("Got nmtSwitchSetupRequest A\n"); + //printf("Got nmtSwitchSetupRequest A gotIntro = %d\n",gotIntro); if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] got nmtSwitchSetupRequest gotIntro = %d\n",__FILE__,__FUNCTION__,__LINE__,gotIntro); @@ -1181,11 +1182,17 @@ void ConnectionSlot::update(bool checkForNewClients,int lockedSlotIndex) { if(receiveMessage(&switchSetupRequest)) { MutexSafeWrapper safeMutex(getServerSynchAccessor(),CODE_AT_LINE); - int factionIdx = switchSetupRequest.getCurrentFactionIndex(); - if(serverInterface->getSwitchSetupRequests()[factionIdx] == NULL) { - serverInterface->getSwitchSetupRequests()[factionIdx]= new SwitchSetupRequest(); + int slotIdx = switchSetupRequest.getCurrentSlotIndex(); + int newSlotIdx = switchSetupRequest.getToSlotIndex(); + + //printf("slotIdx = %d newSlotIdx = %d\n",slotIdx,newSlotIdx); + + if(serverInterface->getSwitchSetupRequests(slotIdx) == NULL) { + serverInterface->setSwitchSetupRequests(slotIdx,new SwitchSetupRequest()); } - *(serverInterface->getSwitchSetupRequests()[factionIdx]) = switchSetupRequest; + *(serverInterface->getSwitchSetupRequests(slotIdx)) = switchSetupRequest; + + //printf("slotIdx = %d newSlotIdx = %d\n",serverInterface->getSwitchSetupRequests(slotIdx)->getCurrentSlotIndex(),serverInterface->getSwitchSetupRequests(slotIdx)->getToSlotIndex()); this->playerStatus = switchSetupRequest.getNetworkPlayerStatus(); this->name = switchSetupRequest.getNetworkPlayerName(); @@ -1194,9 +1201,9 @@ void ConnectionSlot::update(bool checkForNewClients,int lockedSlotIndex) { //printf("Got nmtSwitchSetupRequest C\n"); //printf("In [%s::%s Line %d] networkPlayerName [%s]\n",__FILE__,__FUNCTION__,__LINE__,serverInterface->getSwitchSetupRequests()[factionIdx]->getNetworkPlayerName().c_str()); - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d] networkPlayerName [%s]\n",__FILE__,__FUNCTION__,__LINE__,serverInterface->getSwitchSetupRequests()[factionIdx]->getNetworkPlayerName().c_str()); + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d] networkPlayerName [%s]\n",__FILE__,__FUNCTION__,__LINE__,serverInterface->getSwitchSetupRequests()[slotIdx]->getNetworkPlayerName().c_str()); - if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] factionIdx = %d, switchSetupRequest.getNetworkPlayerName() [%s] switchSetupRequest.getNetworkPlayerStatus() = %d, switchSetupRequest.getSwitchFlags() = %d\n",__FILE__,__FUNCTION__,__LINE__,factionIdx,switchSetupRequest.getNetworkPlayerName().c_str(),switchSetupRequest.getNetworkPlayerStatus(),switchSetupRequest.getSwitchFlags()); + if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] factionIdx = %d, switchSetupRequest.getNetworkPlayerName() [%s] switchSetupRequest.getNetworkPlayerStatus() = %d, switchSetupRequest.getSwitchFlags() = %d\n",__FILE__,__FUNCTION__,__LINE__,slotIdx,switchSetupRequest.getNetworkPlayerName().c_str(),switchSetupRequest.getNetworkPlayerStatus(),switchSetupRequest.getSwitchFlags()); } else { if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d]\nInvalid message type before intro handshake [%d]\nDisconnecting socket for slot: %d [%s].\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,networkMessageType,this->playerIndex,this->getIpAddress().c_str()); @@ -1214,6 +1221,7 @@ void ConnectionSlot::update(bool checkForNewClients,int lockedSlotIndex) { break; } + case nmtReady: { NetworkMessageReady networkMessageReady; @@ -1338,6 +1346,20 @@ void ConnectionSlot::validateConnection() { } } +void ConnectionSlot::resetJoinGameInProgressFlags() { + this->gotIntro = false; + this->skipLagCheck = false; + this->joinGameInProgress = false; + this->ready= false; +} + +void ConnectionSlot::setJoinGameInProgressFlags() { + this->gotIntro = true; + this->skipLagCheck = true; + this->joinGameInProgress = true; + this->ready= false; +} + void ConnectionSlot::close() { if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s LINE: %d]\n",__FILE__,__FUNCTION__,__LINE__); diff --git a/source/glest_game/network/connection_slot.h b/source/glest_game/network/connection_slot.h index 1f3c3be4..f6ac8740 100644 --- a/source/glest_game/network/connection_slot.h +++ b/source/glest_game/network/connection_slot.h @@ -216,6 +216,9 @@ public: virtual void saveGame(XmlNode *rootNode) {}; + void resetJoinGameInProgressFlags(); + void setJoinGameInProgressFlags(); + protected: Mutex * getServerSynchAccessor(); diff --git a/source/glest_game/network/network_message.cpp b/source/glest_game/network/network_message.cpp index e9b0b5f3..0ccda5ed 100644 --- a/source/glest_game/network/network_message.cpp +++ b/source/glest_game/network/network_message.cpp @@ -1027,6 +1027,7 @@ unsigned char * NetworkMessageLaunch::packMessage() { } bool NetworkMessageLaunch::receive(Socket* socket) { + //printf("Receive NetworkMessageLaunch\n"); bool result = false; if(useOldProtocol == true) { result = NetworkMessage::receive(socket, &data, sizeof(data), true); @@ -1058,6 +1059,8 @@ bool NetworkMessageLaunch::receive(Socket* socket) { } void NetworkMessageLaunch::send(Socket* socket) { + //printf("Sending NetworkMessageLaunch\n"); + if(data.messageType == nmtLaunch) { if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] nmtLaunch\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); } @@ -2389,8 +2392,8 @@ void NetworkMessageSynchNetworkGameDataFileGet::fromEndian() { SwitchSetupRequest::SwitchSetupRequest() { data.messageType= nmtSwitchSetupRequest; data.selectedFactionName=""; - data.currentFactionIndex=-1; - data.toFactionIndex=-1; + data.currentSlotIndex=-1; + data.toSlotIndex=-1; data.toTeam = -1; data.networkPlayerName=""; data.networkPlayerStatus = npst_None; @@ -2404,8 +2407,8 @@ SwitchSetupRequest::SwitchSetupRequest(string selectedFactionName, int8 currentF string language) { data.messageType= nmtSwitchSetupRequest; data.selectedFactionName=selectedFactionName; - data.currentFactionIndex=currentFactionIndex; - data.toFactionIndex=toFactionIndex; + data.currentSlotIndex=currentFactionIndex; + data.toSlotIndex=toFactionIndex; data.toTeam = toTeam; data.networkPlayerName=networkPlayerName; data.networkPlayerStatus=networkPlayerStatus; @@ -2425,8 +2428,8 @@ unsigned int SwitchSetupRequest::getPackedSize() { result = pack(buf, getPackedMessageFormat(), packedData.messageType, packedData.selectedFactionName.getBuffer(), - packedData.currentFactionIndex, - packedData.toFactionIndex, + packedData.currentSlotIndex, + packedData.toSlotIndex, packedData.toTeam, packedData.networkPlayerName.getBuffer(), packedData.networkPlayerStatus, @@ -2440,8 +2443,8 @@ void SwitchSetupRequest::unpackMessage(unsigned char *buf) { unpack(buf, getPackedMessageFormat(), &data.messageType, data.selectedFactionName.getBuffer(), - &data.currentFactionIndex, - &data.toFactionIndex, + &data.currentSlotIndex, + &data.toSlotIndex, &data.toTeam, data.networkPlayerName.getBuffer(), &data.networkPlayerStatus, @@ -2454,8 +2457,8 @@ unsigned char * SwitchSetupRequest::packMessage() { pack(buf, getPackedMessageFormat(), data.messageType, data.selectedFactionName.getBuffer(), - data.currentFactionIndex, - data.toFactionIndex, + data.currentSlotIndex, + data.toSlotIndex, data.toTeam, data.networkPlayerName.getBuffer(), data.networkPlayerStatus, @@ -2509,8 +2512,8 @@ void SwitchSetupRequest::toEndian() { static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); if(bigEndianSystem == true) { data.messageType = Shared::PlatformByteOrder::toCommonEndian(data.messageType); - data.currentFactionIndex = Shared::PlatformByteOrder::toCommonEndian(data.currentFactionIndex); - data.toFactionIndex = Shared::PlatformByteOrder::toCommonEndian(data.toFactionIndex); + data.currentSlotIndex = Shared::PlatformByteOrder::toCommonEndian(data.currentSlotIndex); + data.toSlotIndex = Shared::PlatformByteOrder::toCommonEndian(data.toSlotIndex); data.toTeam = Shared::PlatformByteOrder::toCommonEndian(data.toTeam); data.networkPlayerStatus = Shared::PlatformByteOrder::toCommonEndian(data.networkPlayerStatus); data.switchFlags = Shared::PlatformByteOrder::toCommonEndian(data.switchFlags); @@ -2520,8 +2523,8 @@ void SwitchSetupRequest::fromEndian() { static bool bigEndianSystem = Shared::PlatformByteOrder::isBigEndian(); if(bigEndianSystem == true) { data.messageType = Shared::PlatformByteOrder::fromCommonEndian(data.messageType); - data.currentFactionIndex = Shared::PlatformByteOrder::fromCommonEndian(data.currentFactionIndex); - data.toFactionIndex = Shared::PlatformByteOrder::fromCommonEndian(data.toFactionIndex); + data.currentSlotIndex = Shared::PlatformByteOrder::fromCommonEndian(data.currentSlotIndex); + data.toSlotIndex = Shared::PlatformByteOrder::fromCommonEndian(data.toSlotIndex); data.toTeam = Shared::PlatformByteOrder::fromCommonEndian(data.toTeam); data.networkPlayerStatus = Shared::PlatformByteOrder::fromCommonEndian(data.networkPlayerStatus); data.switchFlags = Shared::PlatformByteOrder::fromCommonEndian(data.switchFlags); diff --git a/source/glest_game/network/network_message.h b/source/glest_game/network/network_message.h index 35b112c7..88934873 100644 --- a/source/glest_game/network/network_message.h +++ b/source/glest_game/network/network_message.h @@ -763,8 +763,8 @@ private: struct Data { int8 messageType; NetworkString selectedFactionName; //wanted faction name - int8 currentFactionIndex; - int8 toFactionIndex; + int8 currentSlotIndex; + int8 toSlotIndex; int8 toTeam; NetworkString networkPlayerName; int8 networkPlayerStatus; @@ -793,8 +793,8 @@ public: virtual size_t getDataSize() const { return sizeof(Data); } string getSelectedFactionName() const {return data.selectedFactionName.getString();} - int getCurrentFactionIndex() const {return data.currentFactionIndex;} - int getToFactionIndex() const {return data.toFactionIndex;} + int getCurrentSlotIndex() const {return data.currentSlotIndex;} + int getToSlotIndex() const {return data.toSlotIndex;} int getToTeam() const {return data.toTeam;} string getNetworkPlayerName() const {return data.networkPlayerName.getString(); } int getSwitchFlags() const {return data.switchFlags;} diff --git a/source/glest_game/network/server_interface.cpp b/source/glest_game/network/server_interface.cpp index 211d5451..c0668462 100644 --- a/source/glest_game/network/server_interface.cpp +++ b/source/glest_game/network/server_interface.cpp @@ -56,6 +56,8 @@ ServerInterface::ServerInterface(bool publishEnabled) :GameNetworkInterface() { unPauseForInGameConnection = false; serverSynchAccessor = new Mutex(); + switchSetupRequestsSynchAccessor = new Mutex(); + for(int i= 0; i < GameConstants::maxPlayers; ++i) { slotAccessorMutexes[i] = new Mutex(); } @@ -325,9 +327,27 @@ ServerInterface::~ServerInterface() { } broadcastMessageQueue.clear(); + delete switchSetupRequestsSynchAccessor; + switchSetupRequestsSynchAccessor = NULL; + if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); } +SwitchSetupRequest ** ServerInterface::getSwitchSetupRequests() { + MutexSafeWrapper safeMutex(switchSetupRequestsSynchAccessor,CODE_AT_LINE); + return &switchSetupRequests[0]; +} + +SwitchSetupRequest * ServerInterface::getSwitchSetupRequests(int index) { + MutexSafeWrapper safeMutex(switchSetupRequestsSynchAccessor,CODE_AT_LINE); + return switchSetupRequests[index]; +} + +void ServerInterface::setSwitchSetupRequests(int index,SwitchSetupRequest *ptr) { + MutexSafeWrapper safeMutex(switchSetupRequestsSynchAccessor,CODE_AT_LINE); + switchSetupRequests[index] = ptr; +} + int ServerInterface::isValidClientType(uint32 clientIp) { int result = 0; for(int i= 0; exitServer == false && i < GameConstants::maxPlayers; ++i) { @@ -460,22 +480,34 @@ bool ServerInterface::switchSlot(int fromPlayerIndex, int toPlayerIndex) { MutexSafeWrapper safeMutex(serverSynchAccessor,CODE_AT_LINE); MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[fromPlayerIndex],CODE_AT_LINE_X(fromPlayerIndex)); MutexSafeWrapper safeMutexSlot2(slotAccessorMutexes[toPlayerIndex],CODE_AT_LINE_X(toPlayerIndex)); + + //printf("#1 Server is switching slots\n"); if(slots[toPlayerIndex] != NULL && slots[toPlayerIndex]->isConnected() == false) { + + //printf("#2 Server is switching slots\n"); + slots[fromPlayerIndex]->setPlayerIndex(toPlayerIndex); slots[toPlayerIndex]->setPlayerIndex(fromPlayerIndex); ConnectionSlot *tmp = slots[toPlayerIndex]; slots[toPlayerIndex] = slots[fromPlayerIndex]; slots[fromPlayerIndex] = tmp; safeMutex.ReleaseLock(); + PlayerIndexMessage playerIndexMessage(toPlayerIndex); slots[toPlayerIndex]->sendMessage(&playerIndexMessage); + + //slots[fromPlayerIndex]->resetJoinGameInProgressFlags(); + //slots[toPlayerIndex]->setJoinGameInProgressFlags(); + safeMutexSlot.ReleaseLock(); safeMutexSlot2.ReleaseLock(); result = true; updateListen(); } else { + //printf("#3 Server is switching slots aborted, is slot already connected?\n"); + safeMutexSlot.ReleaseLock(); safeMutexSlot2.ReleaseLock(); safeMutex.ReleaseLock(); @@ -2227,6 +2259,16 @@ bool ServerInterface::launchGame(const GameSettings *gameSettings) { void ServerInterface::broadcastGameSetup(GameSettings *gameSettingsBuffer, bool setGameSettingsBuffer) { if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + + if(gameSettingsBuffer != NULL) { + for(unsigned int i = 0; i < gameSettingsBuffer->getFactionCount(); ++i) { + int slotIndex = gameSettingsBuffer->getStartLocationIndex(i); + if(gameSettingsBuffer->getFactionControl(i) == ctNetwork && + isClientConnected(slotIndex) == false) { + gameSettingsBuffer->setNetworkPlayerName(i,GameConstants::NETWORK_SLOT_UNCONNECTED_SLOTNAME); + } + } + } if(setGameSettingsBuffer == true) { validateGameSettings(gameSettingsBuffer); //setGameSettings(gameSettingsBuffer,false); diff --git a/source/glest_game/network/server_interface.h b/source/glest_game/network/server_interface.h index a4d4dd0b..87972155 100644 --- a/source/glest_game/network/server_interface.h +++ b/source/glest_game/network/server_interface.h @@ -58,7 +58,10 @@ private: ServerSocket serverSocket; bool gameHasBeenInitiated; int gameSettingsUpdateCount; + + Mutex *switchSetupRequestsSynchAccessor; SwitchSetupRequest* switchSetupRequests[GameConstants::maxPlayers]; + Mutex *serverSynchAccessor; int currentFrameCount; @@ -147,15 +150,14 @@ public: virtual void quitGame(bool userManuallyQuit); virtual string getNetworkStatus(); - ServerSocket *getServerSocket() - { + ServerSocket *getServerSocket() { return &serverSocket; } - SwitchSetupRequest **getSwitchSetupRequests() - { - return &switchSetupRequests[0]; - } + SwitchSetupRequest **getSwitchSetupRequests(); + SwitchSetupRequest *getSwitchSetupRequests(int index); + void setSwitchSetupRequests(int index,SwitchSetupRequest *ptr); + Mutex * getSwitchSetupRequestsMutex() { return switchSetupRequestsSynchAccessor; } void addSlot(int playerIndex); bool switchSlot(int fromPlayerIndex, int toPlayerIndex);