2010-04-22 01:13:39 +02:00
// ==============================================================
// This file is part of Glest (www.glest.org)
//
// Copyright (C) 2001-2005 Marti<74> o Figueroa
//
// You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version
// ==============================================================
# include "menu_state_connected_game.h"
# include "menu_state_join_game.h"
2010-05-09 23:23:13 +02:00
# include "menu_state_masterserver.h"
2010-04-22 01:13:39 +02:00
# include "renderer.h"
# include "sound_renderer.h"
# include "core_data.h"
# include "config.h"
# include "menu_state_new_game.h"
# include "metrics.h"
# include "network_manager.h"
# include "network_message.h"
# include "client_interface.h"
# include "conversion.h"
# include "socket.h"
# include "game.h"
# include <algorithm>
# include <time.h>
# include "leak_dumper.h"
namespace Glest { namespace Game {
using namespace Shared : : Util ;
struct FormatString {
void operator ( ) ( string & s ) {
s = formatString ( s ) ;
}
} ;
// =====================================================
// class MenuStateConnectedGame
// =====================================================
2010-05-09 23:23:13 +02:00
MenuStateConnectedGame : : MenuStateConnectedGame ( Program * program , MainMenu * mainMenu , JoinMenu joinMenuInfo , bool openNetworkSlots ) :
2010-05-16 18:40:21 +02:00
MenuState ( program , mainMenu , " connected-game " ) //← set on connected-game
2010-04-22 01:13:39 +02:00
{
2010-07-09 20:02:31 +02:00
lastNetworkSendPing = 0 ;
2010-07-09 17:01:49 +02:00
2010-05-09 23:23:13 +02:00
returnMenuInfo = joinMenuInfo ;
2010-04-22 01:13:39 +02:00
Lang & lang = Lang : : getInstance ( ) ;
NetworkManager & networkManager = NetworkManager : : getInstance ( ) ;
Config & config = Config : : getInstance ( ) ;
needToSetChangedGameSettings = false ;
2010-04-29 01:59:37 +02:00
lastSetChangedGameSettings = time ( NULL ) ;
2010-06-09 00:50:37 +02:00
showFullConsole = false ;
2010-04-23 01:18:04 +02:00
currentFactionName = " " ;
currentMap = " " ;
settingsReceivedFromServer = false ;
2010-04-22 01:13:39 +02:00
vector < string > teamItems , controlItems , results ;
//state
labelStatus . init ( 330 , 700 ) ;
labelStatus . setText ( " " ) ;
labelInfo . init ( 30 , 700 ) ;
labelInfo . setText ( " " ) ;
//create
buttonDisconnect . init ( 350 , 180 , 125 ) ;
buttonPlayNow . init ( 525 , 180 , 125 ) ;
//map listBox
// put them all in a set, to weed out duplicates (gbm & mgm with same name)
// will also ensure they are alphabetically listed (rather than how the OS provides them)
listBoxMap . init ( 100 , 260 , 200 ) ;
listBoxMap . setEditable ( false ) ;
//listBoxMap.setItems(results);
labelMap . init ( 100 , 290 ) ;
labelMapInfo . init ( 100 , 230 , 200 , 40 ) ;
// fog - o - war
// @350 ? 300 ?
labelFogOfWar . init ( 350 , 290 , 100 ) ;
listBoxFogOfWar . init ( 350 , 260 , 100 ) ;
listBoxFogOfWar . pushBackItem ( lang . get ( " Yes " ) ) ;
listBoxFogOfWar . pushBackItem ( lang . get ( " No " ) ) ;
listBoxFogOfWar . setSelectedItemIndex ( 0 ) ;
listBoxFogOfWar . setEditable ( false ) ;
//tileset listBox
listBoxTileset . init ( 500 , 260 , 150 ) ;
listBoxTileset . setEditable ( false ) ;
//listBoxTileset.setItems(results);
labelTileset . init ( 500 , 290 ) ;
//tech Tree listBox
listBoxTechTree . init ( 700 , 260 , 150 ) ;
listBoxTechTree . setEditable ( false ) ;
//listBoxTechTree.setItems(results);
labelTechTree . init ( 700 , 290 ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
//list boxes
for ( int i = 0 ; i < GameConstants : : maxPlayers ; + + i ) {
labelPlayers [ i ] . init ( 100 , 550 - i * 30 ) ;
labelPlayers [ i ] . setEditable ( false ) ;
listBoxControls [ i ] . init ( 200 , 550 - i * 30 ) ;
listBoxControls [ i ] . setEditable ( false ) ;
listBoxFactions [ i ] . init ( 400 , 550 - i * 30 ) ;
listBoxFactions [ i ] . setEditable ( false ) ;
listBoxTeams [ i ] . init ( 600 , 550 - i * 30 , 60 ) ;
listBoxTeams [ i ] . setEditable ( false ) ;
labelNetStatus [ i ] . init ( 700 , 550 - i * 30 , 60 ) ;
2010-04-29 01:59:37 +02:00
grabSlotButton [ i ] . init ( 700 , 550 - i * 30 , 30 ) ;
grabSlotButton [ i ] . setText ( " > " ) ;
2010-04-22 01:13:39 +02:00
}
labelControl . init ( 200 , 600 , GraphicListBox : : defW , GraphicListBox : : defH , true ) ;
labelFaction . init ( 400 , 600 , GraphicListBox : : defW , GraphicListBox : : defH , true ) ;
labelTeam . init ( 600 , 600 , 60 , GraphicListBox : : defH , true ) ;
//texts
buttonDisconnect . setText ( lang . get ( " Return " ) ) ;
buttonPlayNow . setText ( lang . get ( " PlayNow " ) ) ;
controlItems . push_back ( lang . get ( " Closed " ) ) ;
controlItems . push_back ( lang . get ( " CpuEasy " ) ) ;
controlItems . push_back ( lang . get ( " Cpu " ) ) ;
controlItems . push_back ( lang . get ( " CpuUltra " ) ) ;
controlItems . push_back ( lang . get ( " CpuMega " ) ) ;
controlItems . push_back ( lang . get ( " Network " ) ) ;
controlItems . push_back ( lang . get ( " Human " ) ) ;
teamItems . push_back ( " 1 " ) ;
teamItems . push_back ( " 2 " ) ;
teamItems . push_back ( " 3 " ) ;
teamItems . push_back ( " 4 " ) ;
teamItems . push_back ( " 5 " ) ;
teamItems . push_back ( " 6 " ) ;
teamItems . push_back ( " 7 " ) ;
teamItems . push_back ( " 8 " ) ;
for ( int i = 0 ; i < GameConstants : : maxPlayers ; + + i ) {
labelPlayers [ i ] . setText ( lang . get ( " Player " ) + " " + intToStr ( i ) ) ;
listBoxTeams [ i ] . setItems ( teamItems ) ;
listBoxTeams [ i ] . setSelectedItemIndex ( i ) ;
listBoxControls [ i ] . setItems ( controlItems ) ;
2010-04-23 01:18:04 +02:00
labelNetStatus [ i ] . setText ( " V " ) ;
2010-04-22 01:13:39 +02:00
}
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
labelMap . setText ( lang . get ( " Map " ) ) ;
labelFogOfWar . setText ( lang . get ( " FogOfWar " ) ) ;
labelTileset . setText ( lang . get ( " Tileset " ) ) ;
labelTechTree . setText ( lang . get ( " TechTree " ) ) ;
labelControl . setText ( lang . get ( " Control " ) ) ;
labelFaction . setText ( lang . get ( " Faction " ) ) ;
labelTeam . setText ( lang . get ( " Team " ) ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
labelMapInfo . setText ( mapInfo . desc ) ;
//init controllers
listBoxControls [ 0 ] . setSelectedItemIndex ( ctHuman ) ;
2010-06-09 00:50:37 +02:00
chatManager . init ( & console , - 1 , true ) ;
2010-04-22 01:13:39 +02:00
}
void MenuStateConnectedGame : : mouseClick ( int x , int y , MouseButton mouseButton ) {
CoreData & coreData = CoreData : : getInstance ( ) ;
SoundRenderer & soundRenderer = SoundRenderer : : getInstance ( ) ;
NetworkManager & networkManager = NetworkManager : : getInstance ( ) ;
ClientInterface * clientInterface = networkManager . getClientInterface ( ) ;
2010-04-30 03:08:29 +02:00
if ( ! settingsReceivedFromServer ) return ;
2010-04-22 01:13:39 +02:00
if ( buttonDisconnect . mouseClick ( x , y ) ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
soundRenderer . playFx ( coreData . getClickSoundA ( ) ) ;
if ( clientInterface - > getSocket ( ) ! = NULL )
{
if ( clientInterface - > isConnected ( ) = = true )
{
string sQuitText = Config : : getInstance ( ) . getString ( " NetPlayerName " , Socket : : getHostName ( ) . c_str ( ) ) + " has chosen to leave the game! " ;
clientInterface - > sendTextMessage ( sQuitText , - 1 ) ;
2010-07-02 18:54:28 +02:00
sleep ( 1 ) ;
2010-04-22 01:13:39 +02:00
}
clientInterface - > close ( ) ;
}
clientInterface - > reset ( ) ;
networkManager . end ( ) ;
currentFactionName = " " ;
currentMap = " " ;
2010-05-09 23:23:13 +02:00
returnToJoinMenu ( ) ;
2010-04-22 01:13:39 +02:00
}
2010-06-30 16:51:30 +02:00
// Only allow changes after we get game settings from the server
2010-07-02 18:54:28 +02:00
else if ( clientInterface - > isConnected ( ) = = true & &
2010-06-30 16:51:30 +02:00
clientInterface - > getGameSettingsReceived ( ) = = true ) {
if ( buttonPlayNow . mouseClick ( x , y ) & & buttonPlayNow . getEnabled ( ) ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
soundRenderer . playFx ( coreData . getClickSoundC ( ) ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-30 03:08:29 +02:00
}
2010-06-30 16:51:30 +02:00
else
2010-04-29 01:59:37 +02:00
{
2010-06-30 16:51:30 +02:00
int myCurrentIndex = - 1 ;
for ( int i = 0 ; i < GameConstants : : maxPlayers ; + + i )
{ // find my current index by looking at editable listBoxes
if ( listBoxFactions [ i ] . getEditable ( ) ) {
myCurrentIndex = i ;
2010-04-29 01:59:37 +02:00
}
}
2010-06-30 16:51:30 +02:00
if ( myCurrentIndex ! = - 1 )
for ( int i = 0 ; i < GameConstants : : maxPlayers ; + + i )
{
if ( listBoxFactions [ i ] . getEditable ( ) ) {
if ( listBoxFactions [ i ] . mouseClick ( x , y ) ) {
soundRenderer . playFx ( coreData . getClickSoundA ( ) ) ;
ClientInterface * clientInterface = NetworkManager : : getInstance ( ) . getClientInterface ( ) ;
if ( clientInterface - > isConnected ( ) ) {
clientInterface - > setGameSettingsReceived ( false ) ;
clientInterface - > sendSwitchSetupRequest ( listBoxFactions [ i ] . getSelectedItem ( ) , i , - 1 , listBoxTeams [ i ] . getSelectedItemIndex ( ) ) ;
}
break ;
2010-04-29 01:59:37 +02:00
}
2010-04-30 03:08:29 +02:00
}
2010-06-30 16:51:30 +02:00
if ( listBoxTeams [ i ] . getEditable ( ) ) {
if ( listBoxTeams [ i ] . mouseClick ( x , y ) ) {
soundRenderer . playFx ( coreData . getClickSoundA ( ) ) ;
if ( clientInterface - > isConnected ( ) ) {
clientInterface - > setGameSettingsReceived ( false ) ;
clientInterface - > sendSwitchSetupRequest ( listBoxFactions [ i ] . getSelectedItem ( ) , i , - 1 , listBoxTeams [ i ] . getSelectedItemIndex ( ) ) ;
}
break ;
}
}
if ( ( listBoxControls [ i ] . getSelectedItemIndex ( ) = = ctNetwork ) & &
( labelNetStatus [ i ] . getText ( ) = = GameConstants : : NETWORK_SLOT_UNCONNECTED_SLOTNAME ) ) {
if ( grabSlotButton [ i ] . mouseClick ( x , y ) )
{
soundRenderer . playFx ( coreData . getClickSoundA ( ) ) ;
clientInterface - > setGameSettingsReceived ( false ) ;
settingsReceivedFromServer = false ;
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] sending a switchSlot request from %d to %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , clientInterface - > getGameSettings ( ) - > getThisFactionIndex ( ) , i ) ;
clientInterface - > sendSwitchSetupRequest ( listBoxFactions [ myCurrentIndex ] . getSelectedItem ( ) , myCurrentIndex , i , listBoxTeams [ myCurrentIndex ] . getSelectedItemIndex ( ) ) ;
break ;
}
2010-04-29 01:59:37 +02:00
}
}
}
2010-04-22 01:13:39 +02:00
}
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
}
2010-07-02 18:54:28 +02:00
void MenuStateConnectedGame : : returnToJoinMenu ( ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
if ( returnMenuInfo = = jmSimple ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-09 23:23:13 +02:00
mainMenu - > setState ( new MenuStateJoinGame ( program , mainMenu ) ) ;
}
2010-07-02 18:54:28 +02:00
else {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-09 23:23:13 +02:00
mainMenu - > setState ( new MenuStateMasterserver ( program , mainMenu ) ) ;
}
}
2010-04-22 01:13:39 +02:00
void MenuStateConnectedGame : : mouseMove ( int x , int y , const MouseState * ms ) {
buttonDisconnect . mouseMove ( x , y ) ;
buttonPlayNow . mouseMove ( x , y ) ;
for ( int i = 0 ; i < GameConstants : : maxPlayers ; + + i ) {
listBoxControls [ i ] . mouseMove ( x , y ) ;
listBoxFactions [ i ] . mouseMove ( x , y ) ;
listBoxTeams [ i ] . mouseMove ( x , y ) ;
2010-04-30 03:08:29 +02:00
grabSlotButton [ i ] . mouseMove ( x , y ) ;
2010-04-22 01:13:39 +02:00
}
listBoxMap . mouseMove ( x , y ) ;
listBoxFogOfWar . mouseMove ( x , y ) ;
listBoxTileset . mouseMove ( x , y ) ;
listBoxTechTree . mouseMove ( x , y ) ;
}
void MenuStateConnectedGame : : render ( ) {
try {
2010-07-09 17:59:49 +02:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
2010-07-08 10:29:51 +02:00
2010-04-23 01:18:04 +02:00
if ( ! settingsReceivedFromServer ) return ;
2010-07-08 10:29:51 +02:00
2010-07-09 17:59:49 +02:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
2010-07-08 10:29:51 +02:00
2010-04-22 01:13:39 +02:00
Renderer & renderer = Renderer : : getInstance ( ) ;
2010-07-09 17:59:49 +02:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
2010-07-08 10:29:51 +02:00
2010-04-22 01:13:39 +02:00
int i ;
renderer . renderButton ( & buttonDisconnect ) ;
2010-04-30 03:08:29 +02:00
//renderer.renderButton(&buttonPlayNow);
2010-04-22 01:13:39 +02:00
2010-07-09 17:59:49 +02:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
2010-07-08 10:29:51 +02:00
2010-04-22 01:13:39 +02:00
for ( i = 0 ; i < GameConstants : : maxPlayers ; + + i ) {
renderer . renderLabel ( & labelPlayers [ i ] ) ;
renderer . renderListBox ( & listBoxControls [ i ] ) ;
if ( listBoxControls [ i ] . getSelectedItemIndex ( ) ! = ctClosed ) {
renderer . renderListBox ( & listBoxFactions [ i ] ) ;
renderer . renderListBox ( & listBoxTeams [ i ] ) ;
2010-04-23 01:18:04 +02:00
//renderer.renderLabel(&labelNetStatus[i]);
2010-06-29 08:50:35 +02:00
if ( ( listBoxControls [ i ] . getSelectedItemIndex ( ) = = ctNetwork ) & &
( labelNetStatus [ i ] . getText ( ) = = GameConstants : : NETWORK_SLOT_UNCONNECTED_SLOTNAME ) ) {
2010-04-29 01:59:37 +02:00
renderer . renderButton ( & grabSlotButton [ i ] ) ;
}
else if ( ( listBoxControls [ i ] . getSelectedItemIndex ( ) = = ctNetwork ) | |
2010-04-23 01:18:04 +02:00
( listBoxControls [ i ] . getSelectedItemIndex ( ) = = ctHuman ) ) {
renderer . renderLabel ( & labelNetStatus [ i ] ) ;
}
2010-04-22 01:13:39 +02:00
}
}
2010-07-09 17:59:49 +02:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
2010-07-08 10:29:51 +02:00
2010-04-22 01:13:39 +02:00
renderer . renderLabel ( & labelStatus ) ;
renderer . renderLabel ( & labelInfo ) ;
renderer . renderLabel ( & labelMap ) ;
renderer . renderLabel ( & labelFogOfWar ) ;
renderer . renderLabel ( & labelTileset ) ;
renderer . renderLabel ( & labelTechTree ) ;
renderer . renderLabel ( & labelControl ) ;
renderer . renderLabel ( & labelFaction ) ;
renderer . renderLabel ( & labelTeam ) ;
renderer . renderLabel ( & labelMapInfo ) ;
renderer . renderListBox ( & listBoxMap ) ;
renderer . renderListBox ( & listBoxFogOfWar ) ;
renderer . renderListBox ( & listBoxTileset ) ;
renderer . renderListBox ( & listBoxTechTree ) ;
2010-07-08 10:29:51 +02:00
if ( program ! = NULL ) program - > renderProgramMsgBox ( ) ;
2010-04-22 01:13:39 +02:00
renderer . renderChatManager ( & chatManager ) ;
2010-06-09 00:50:37 +02:00
renderer . renderConsole ( & console , showFullConsole , true ) ;
2010-04-22 01:13:39 +02:00
}
catch ( const std : : exception & ex ) {
char szBuf [ 1024 ] = " " ;
sprintf ( szBuf , " In [%s::%s %d] error [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , ex . what ( ) ) ;
throw runtime_error ( szBuf ) ;
}
}
void MenuStateConnectedGame : : update ( )
{
ClientInterface * clientInterface = NetworkManager : : getInstance ( ) . getClientInterface ( ) ;
Lang & lang = Lang : : getInstance ( ) ;
2010-07-09 20:02:31 +02:00
if ( clientInterface ! = NULL & & clientInterface - > isConnected ( ) ) {
if ( difftime ( time ( NULL ) , lastNetworkSendPing ) > = GameConstants : : networkPingInterval ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] about to sendPingMessage... \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-07-09 17:59:49 +02:00
2010-07-09 20:02:31 +02:00
bool isFirstPing = ( lastNetworkSendPing = = 0 ) ;
lastNetworkSendPing = time ( NULL ) ;
clientInterface - > sendPingMessage ( GameConstants : : networkPingInterval , time ( NULL ) ) ;
2010-07-09 19:12:57 +02:00
2010-07-09 20:02:31 +02:00
if ( isFirstPing = = false & & clientInterface - > getLastPingLag ( ) > = ( GameConstants : : networkPingInterval * 2 ) ) {
string playerNameStr = Config : : getInstance ( ) . getString ( " NetPlayerName " , Socket : : getHostName ( ) . c_str ( ) ) ;
clientInterface - > sendTextMessage ( playerNameStr + " 's connection timed out communicating with server. " , - 1 ) ;
clientInterface - > close ( ) ;
}
2010-07-09 19:12:57 +02:00
}
2010-07-09 17:01:49 +02:00
}
2010-04-22 01:13:39 +02:00
//update status label
2010-07-08 10:29:51 +02:00
if ( clientInterface ! = NULL & & clientInterface - > isConnected ( ) ) {
2010-04-22 01:13:39 +02:00
buttonDisconnect . setText ( lang . get ( " Disconnect " ) ) ;
if ( clientInterface - > getAllowDownloadDataSynch ( ) = = false )
{
string label = lang . get ( " ConnectedToServer " ) ;
if ( ! clientInterface - > getServerName ( ) . empty ( ) )
{
label = label + " " + clientInterface - > getServerName ( ) ;
}
if ( clientInterface - > getAllowGameDataSynchCheck ( ) = = true & &
clientInterface - > getNetworkGameDataSynchCheckOk ( ) = = false )
{
label = label + " - warning synch mismatch for: " ;
if ( clientInterface - > getNetworkGameDataSynchCheckOkMap ( ) = = false )
{
label = label + " map " ;
}
if ( clientInterface - > getNetworkGameDataSynchCheckOkTile ( ) = = false )
{
label = label + " tile " ;
}
if ( clientInterface - > getNetworkGameDataSynchCheckOkTech ( ) = = false )
{
label = label + " techtree " ;
}
//if(clientInterface->getNetworkGameDataSynchCheckOkFogOfWar() == false)
//{
// label = label + " FogOfWar == false";
//}
}
else if ( clientInterface - > getAllowGameDataSynchCheck ( ) = = true )
{
label + = " - data synch is ok " ;
}
2010-06-05 09:52:14 +02:00
std : : string networkFrameString = lang . get ( " NetworkFramePeriod " ) + " " + intToStr ( clientInterface - > getGameSettings ( ) - > getNetworkFramePeriod ( ) ) ;
2010-07-02 04:50:20 +02:00
//float pingTime = clientInterface->getThreadedPingMS(clientInterface->getServerIpAddress().c_str());
2010-06-04 21:42:58 +02:00
char szBuf [ 1024 ] = " " ;
2010-07-02 04:50:20 +02:00
//sprintf(szBuf,"%s, ping = %.2fms, %s",label.c_str(),pingTime,networkFrameString.c_str());
sprintf ( szBuf , " %s, %s " , label . c_str ( ) , networkFrameString . c_str ( ) ) ;
2010-06-04 21:42:58 +02:00
labelStatus . setText ( szBuf ) ;
2010-04-22 01:13:39 +02:00
}
else
{
string label = lang . get ( " ConnectedToServer " ) ;
if ( ! clientInterface - > getServerName ( ) . empty ( ) )
{
label = label + " " + clientInterface - > getServerName ( ) ;
}
if ( clientInterface - > getAllowGameDataSynchCheck ( ) = = true & &
clientInterface - > getNetworkGameDataSynchCheckOk ( ) = = false )
{
label = label + " - waiting to synch: " ;
if ( clientInterface - > getNetworkGameDataSynchCheckOkMap ( ) = = false )
{
label = label + " map " ;
}
if ( clientInterface - > getNetworkGameDataSynchCheckOkTile ( ) = = false )
{
label = label + " tile " ;
}
if ( clientInterface - > getNetworkGameDataSynchCheckOkTech ( ) = = false )
{
label = label + " techtree " ;
}
//if(clientInterface->getNetworkGameDataSynchCheckOkFogOfWar() == false)
//{
// label = label + " FogOfWar == false";
//}
}
else if ( clientInterface - > getAllowGameDataSynchCheck ( ) = = true )
{
label + = " - data synch is ok " ;
}
2010-06-05 09:52:14 +02:00
std : : string networkFrameString = lang . get ( " NetworkFramePeriod " ) + " " + intToStr ( clientInterface - > getGameSettings ( ) - > getNetworkFramePeriod ( ) ) ;
2010-07-02 04:50:20 +02:00
//float pingTime = clientInterface->getThreadedPingMS(clientInterface->getServerIpAddress().c_str());
2010-06-04 21:42:58 +02:00
char szBuf [ 1024 ] = " " ;
2010-07-02 04:50:20 +02:00
//sprintf(szBuf,"%s, ping = %.2fms, %s",label.c_str(),pingTime,networkFrameString.c_str());
sprintf ( szBuf , " %s, %s " , label . c_str ( ) , networkFrameString . c_str ( ) ) ;
2010-06-04 21:42:58 +02:00
labelStatus . setText ( szBuf ) ;
2010-04-22 01:13:39 +02:00
}
}
2010-07-08 10:29:51 +02:00
else {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
if ( clientInterface ! = NULL & & clientInterface - > isConnected ( ) = = true ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-22 01:13:39 +02:00
clientInterface - > close ( ) ;
}
2010-07-08 10:29:51 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-09 23:23:13 +02:00
returnToJoinMenu ( ) ;
2010-04-23 01:18:04 +02:00
return ;
2010-04-22 01:13:39 +02:00
}
//process network messages
if ( clientInterface - > isConnected ( ) )
{
if ( clientInterface - > getGameSettingsReceived ( ) ) {
2010-04-29 09:59:44 +02:00
bool errorOnMissingData = ( clientInterface - > getAllowGameDataSynchCheck ( ) = = false ) ;
2010-04-22 01:13:39 +02:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
vector < string > maps , tilesets , techtree ;
const GameSettings * gameSettings = clientInterface - > getGameSettings ( ) ;
// tileset
tilesets . push_back ( formatString ( gameSettings - > getTileset ( ) ) ) ;
listBoxTileset . setItems ( tilesets ) ;
// techtree
techtree . push_back ( formatString ( gameSettings - > getTech ( ) ) ) ;
listBoxTechTree . setItems ( techtree ) ;
// factions
2010-04-29 09:59:44 +02:00
bool hasFactions = true ;
if ( currentFactionName ! = gameSettings - > getTech ( ) )
2010-04-22 01:13:39 +02:00
{
2010-06-04 00:52:56 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] hasFactions = %d, currentFactionName [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , hasFactions , currentFactionName . c_str ( ) ) ;
2010-04-29 09:59:44 +02:00
currentFactionName = gameSettings - > getTech ( ) ;
2010-06-03 08:03:41 +02:00
hasFactions = loadFactions ( gameSettings , false ) ;
2010-04-22 01:13:39 +02:00
}
2010-07-09 17:59:49 +02:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] hasFactions = %d, currentFactionName [%s]\n",__FILE__,__FUNCTION__,__LINE__,hasFactions,currentFactionName.c_str());
2010-04-29 09:59:44 +02:00
2010-04-22 01:13:39 +02:00
// map
maps . push_back ( formatString ( gameSettings - > getMap ( ) ) ) ;
listBoxMap . setItems ( maps ) ;
2010-04-29 09:59:44 +02:00
if ( currentMap ! = gameSettings - > getMap ( ) )
2010-04-22 01:13:39 +02:00
{ // load the setup again
2010-04-29 09:59:44 +02:00
currentMap = gameSettings - > getMap ( ) ;
2010-04-22 01:13:39 +02:00
}
2010-07-09 17:59:49 +02:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
2010-04-29 09:59:44 +02:00
2010-04-22 01:13:39 +02:00
// FogOfWar
if ( gameSettings - > getFogOfWar ( ) ) {
listBoxFogOfWar . setSelectedItemIndex ( 0 ) ;
}
else
{
listBoxFogOfWar . setSelectedItemIndex ( 1 ) ;
}
2010-04-29 09:59:44 +02:00
2010-05-31 08:11:31 +02:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
2010-04-29 09:59:44 +02:00
2010-04-22 01:13:39 +02:00
// Control
for ( int i = 0 ; i < GameConstants : : maxPlayers ; + + i ) {
listBoxControls [ i ] . setSelectedItemIndex ( ctClosed ) ;
2010-04-29 01:59:37 +02:00
listBoxFactions [ i ] . setEditable ( false ) ;
listBoxTeams [ i ] . setEditable ( false ) ;
2010-04-22 01:13:39 +02:00
}
2010-07-09 17:59:49 +02:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
2010-04-29 09:59:44 +02:00
if ( hasFactions = = true ) {
2010-06-14 08:38:24 +02:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] errorOnMissingData = %d\n",__FILE__,__FUNCTION__,__LINE__,errorOnMissingData);
2010-04-29 09:59:44 +02:00
for ( int i = 0 ; i < gameSettings - > getFactionCount ( ) ; + + i ) {
int slot = gameSettings - > getStartLocationIndex ( i ) ;
listBoxControls [ slot ] . setSelectedItemIndex ( gameSettings - > getFactionControl ( i ) , errorOnMissingData ) ;
listBoxTeams [ slot ] . setSelectedItemIndex ( gameSettings - > getTeam ( i ) , errorOnMissingData ) ;
2010-06-04 00:52:56 +02:00
//listBoxFactions[slot].setSelectedItem(formatString(gameSettings->getFactionTypeName(i)),errorOnMissingData);
listBoxFactions [ slot ] . setSelectedItem ( formatString ( gameSettings - > getFactionTypeName ( i ) ) , false ) ;
2010-04-29 09:59:44 +02:00
if ( gameSettings - > getFactionControl ( i ) = = ctNetwork ) {
labelNetStatus [ slot ] . setText ( gameSettings - > getNetworkPlayerName ( i ) ) ;
}
if ( gameSettings - > getFactionControl ( i ) = = ctNetwork & & gameSettings - > getThisFactionIndex ( ) = = i ) {
// set my current slot to ctHuman
listBoxControls [ slot ] . setSelectedItemIndex ( ctHuman ) ;
listBoxFactions [ slot ] . setEditable ( true ) ;
listBoxTeams [ slot ] . setEditable ( true ) ;
}
settingsReceivedFromServer = true ;
2010-04-22 01:26:10 +02:00
}
2010-04-22 01:13:39 +02:00
}
}
//update lobby
clientInterface - > updateLobby ( ) ;
2010-07-08 10:29:51 +02:00
clientInterface = NetworkManager : : getInstance ( ) . getClientInterface ( ) ;
if ( clientInterface ! = NULL & & clientInterface - > isConnected ( ) ) {
//call the chat manager
chatManager . updateNetwork ( ) ;
2010-04-22 01:13:39 +02:00
2010-07-08 10:29:51 +02:00
//console
console . update ( ) ;
2010-04-22 01:13:39 +02:00
2010-07-08 10:29:51 +02:00
//intro
if ( clientInterface - > getIntroDone ( ) )
{
labelInfo . setText ( lang . get ( " WaitingHost " ) ) ;
//servers.setString(clientInterface->getServerName(), Ip(labelServerIp.getText()).getString());
}
2010-04-22 01:13:39 +02:00
2010-07-08 10:29:51 +02:00
//launch
if ( clientInterface - > getLaunchGame ( ) )
{
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-22 01:13:39 +02:00
2010-07-08 10:29:51 +02:00
//servers.save(serversSavedFile);
2010-04-22 01:13:39 +02:00
2010-07-08 10:29:51 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-06-01 18:54:44 +02:00
2010-07-08 10:29:51 +02:00
assert ( clientInterface ! = NULL ) ;
2010-06-01 18:54:44 +02:00
2010-07-08 10:29:51 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-22 01:13:39 +02:00
2010-07-08 10:29:51 +02:00
program - > setState ( new Game ( program , clientInterface - > getGameSettings ( ) ) ) ;
2010-04-22 01:13:39 +02:00
2010-07-08 10:29:51 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
}
2010-04-22 01:13:39 +02:00
}
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);
}
2010-07-08 10:29:51 +02:00
if ( clientInterface ! = NULL & & clientInterface - > getLaunchGame ( ) ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s] clientInterface->getLaunchGame() - D \n " , __FILE__ , __FUNCTION__ ) ;
2010-04-22 01:13:39 +02:00
}
2010-06-03 08:03:41 +02:00
bool MenuStateConnectedGame : : loadFactions ( const GameSettings * gameSettings , bool errorOnNoFactions ) {
2010-04-22 01:13:39 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-06-04 00:52:56 +02:00
bool foundFactions = false ;
2010-04-22 01:13:39 +02:00
vector < string > results ;
Config & config = Config : : getInstance ( ) ;
vector < string > techPaths = config . getPathListForType ( ptTechs ) ;
for ( int idx = 0 ; idx < techPaths . size ( ) ; idx + + ) {
string & techPath = techPaths [ idx ] ;
2010-06-03 09:52:17 +02:00
findAll ( techPath + " / " + gameSettings - > getTech ( ) + " /factions/*. " , results , false , false ) ;
2010-04-22 01:13:39 +02:00
if ( results . size ( ) > 0 ) {
break ;
}
}
if ( results . size ( ) = = 0 ) {
2010-04-29 09:59:44 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-22 01:13:39 +02:00
2010-04-29 09:59:44 +02:00
NetworkManager & networkManager = NetworkManager : : getInstance ( ) ;
ClientInterface * clientInterface = networkManager . getClientInterface ( ) ;
if ( clientInterface - > getAllowGameDataSynchCheck ( ) = = false ) {
2010-06-03 08:03:41 +02:00
if ( errorOnNoFactions = = true ) {
throw runtime_error ( " (2) There are no factions for the tech tree [ " + gameSettings->getTech() + " ] " ) ;
}
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] (2)There are no factions for the tech tree [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , gameSettings - > getTech ( ) . c_str ( ) ) ;
}
results . push_back ( " ***missing*** " ) ;
factionFiles = results ;
for ( int i = 0 ; i < GameConstants : : maxPlayers ; + + i ) {
listBoxFactions [ i ] . setItems ( results ) ;
2010-04-29 09:59:44 +02:00
}
2010-04-22 01:13:39 +02:00
2010-06-03 08:03:41 +02:00
char szMsg [ 1024 ] = " " ;
sprintf ( szMsg , " Player: %s is missing the techtree: %s " , Config : : getInstance ( ) . getString ( " NetPlayerName " , Socket : : getHostName ( ) . c_str ( ) ) . c_str ( ) , gameSettings - > getTech ( ) . c_str ( ) ) ;
2010-07-01 02:08:59 +02:00
clientInterface - > sendTextMessage ( szMsg , - 1 , true ) ;
2010-06-03 08:03:41 +02:00
2010-06-04 00:52:56 +02:00
foundFactions = false ;
2010-04-29 09:59:44 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
}
else {
factionFiles = results ;
for ( int i = 0 ; i < results . size ( ) ; + + i ) {
results [ i ] = formatString ( results [ i ] ) ;
2010-04-22 01:13:39 +02:00
2010-04-29 09:59:44 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " Tech [%s] has faction [%s] \n " , gameSettings - > getTech ( ) . c_str ( ) , results [ i ] . c_str ( ) ) ;
}
for ( int i = 0 ; i < GameConstants : : maxPlayers ; + + i ) {
listBoxFactions [ i ] . setItems ( results ) ;
}
2010-06-04 00:52:56 +02:00
foundFactions = ( results . size ( ) > 0 ) ;
2010-04-29 09:59:44 +02:00
}
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-22 01:13:39 +02:00
2010-06-04 00:52:56 +02:00
return foundFactions ;
2010-04-29 09:59:44 +02:00
}
2010-04-22 01:13:39 +02:00
// ============ PRIVATE ===========================
bool MenuStateConnectedGame : : hasNetworkGameSettings ( )
{
bool hasNetworkSlot = false ;
try {
for ( int i = 0 ; i < mapInfo . players ; + + i )
{
ControlType ct = static_cast < ControlType > ( listBoxControls [ i ] . getSelectedItemIndex ( ) ) ;
if ( ct ! = ctClosed )
{
if ( ct = = ctNetwork )
{
hasNetworkSlot = true ;
break ;
}
}
}
}
catch ( const std : : exception & ex ) {
char szBuf [ 1024 ] = " " ;
sprintf ( szBuf , " In [%s::%s %d] error [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , ex . what ( ) ) ;
throw runtime_error ( szBuf ) ;
}
return hasNetworkSlot ;
}
void MenuStateConnectedGame : : reloadFactions ( ) {
vector < string > results ;
Config & config = Config : : getInstance ( ) ;
2010-04-29 09:59:44 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-22 01:13:39 +02:00
vector < string > techPaths = config . getPathListForType ( ptTechs ) ;
for ( int idx = 0 ; idx < techPaths . size ( ) ; idx + + ) {
string & techPath = techPaths [ idx ] ;
findAll ( techPath + " / " + techTreeFiles [ listBoxTechTree . getSelectedItemIndex ( ) ] + " /factions/*. " , results , false , false ) ;
if ( results . size ( ) > 0 ) {
break ;
}
}
2010-04-29 09:59:44 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-22 01:13:39 +02:00
if ( results . size ( ) = = 0 ) {
throw runtime_error ( " (2) There are no factions for the tech tree [ " + techTreeFiles[listBoxTechTree.getSelectedItemIndex()] + " ] " ) ;
}
factionFiles = results ;
for ( int i = 0 ; i < results . size ( ) ; + + i ) {
results [ i ] = formatString ( results [ i ] ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " Tech [%s] has faction [%s] \n " , techTreeFiles [ listBoxTechTree . getSelectedItemIndex ( ) ] . c_str ( ) , results [ i ] . c_str ( ) ) ;
}
2010-04-29 09:59:44 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-22 01:13:39 +02:00
for ( int i = 0 ; i < GameConstants : : maxPlayers ; + + i ) {
listBoxFactions [ i ] . setItems ( results ) ;
listBoxFactions [ i ] . setSelectedItemIndex ( i % results . size ( ) ) ;
}
2010-04-29 09:59:44 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-22 01:13:39 +02:00
}
void MenuStateConnectedGame : : keyDown ( char key )
{
//send key to the chat manager
chatManager . keyDown ( key ) ;
2010-06-09 00:50:37 +02:00
if ( ! chatManager . getEditEnabled ( ) ) {
if ( key = = ' M ' ) {
showFullConsole = true ;
}
}
2010-04-22 01:13:39 +02:00
}
void MenuStateConnectedGame : : keyPress ( char c )
{
chatManager . keyPress ( c ) ;
}
2010-06-08 02:00:28 +02:00
void MenuStateConnectedGame : : keyUp ( char key )
{
chatManager . keyUp ( key ) ;
2010-06-09 00:50:37 +02:00
if ( chatManager . getEditEnabled ( ) ) {
//send key to the chat manager
chatManager . keyUp ( key ) ;
}
else if ( key = = ' M ' ) {
showFullConsole = false ;
}
2010-06-08 02:00:28 +02:00
}
2010-04-22 01:13:39 +02:00
} } //end namespace