2010-03-13 23:00:01 +01: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_custom_game.h"
# include "renderer.h"
# include "sound_renderer.h"
# include "core_data.h"
# include "config.h"
# include "menu_state_new_game.h"
2010-05-16 18:40:21 +02:00
# include "menu_state_masterserver.h"
2010-03-13 23:00:01 +01:00
# include "metrics.h"
# include "network_manager.h"
# include "network_message.h"
# include "client_interface.h"
# include "conversion.h"
# include "socket.h"
# include "game.h"
2010-05-13 00:58:00 +02:00
# include "util.h"
2010-03-27 19:43:38 +01:00
# include <algorithm>
# include <time.h>
2010-05-13 00:58:00 +02:00
# include <curl/curl.h>
2010-03-13 23:00:01 +01:00
# include "leak_dumper.h"
2010-03-27 19:43:38 +01:00
2010-03-13 23:00:01 +01:00
namespace Glest { namespace Game {
using namespace Shared : : Util ;
2010-03-27 08:09:34 +01:00
struct FormatString {
void operator ( ) ( string & s ) {
s = formatString ( s ) ;
}
} ;
2010-03-13 23:00:01 +01:00
// =====================================================
// class MenuStateCustomGame
// =====================================================
2010-05-16 18:40:21 +02:00
MenuStateCustomGame : : MenuStateCustomGame ( Program * program , MainMenu * mainMenu , bool openNetworkSlots , bool parentMenuIsMasterserver ) :
2010-03-13 23:00:01 +01:00
MenuState ( program , mainMenu , " new-game " )
{
2010-06-01 01:39:07 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-17 08:41:05 +02:00
publishToMasterserverThread = NULL ;
2010-03-13 23:00:01 +01:00
Lang & lang = Lang : : getInstance ( ) ;
NetworkManager & networkManager = NetworkManager : : getInstance ( ) ;
2010-03-27 08:09:34 +01:00
Config & config = Config : : getInstance ( ) ;
2010-05-16 18:40:21 +02:00
parentMenuIsMs = parentMenuIsMasterserver ;
2010-03-13 23:00:01 +01:00
needToSetChangedGameSettings = false ;
2010-05-13 08:43:56 +02:00
needToRepublishToMasterserver = false ;
2010-05-13 13:19:10 +02:00
needToBroadcastServerSettings = false ;
2010-05-17 01:03:22 +02:00
showMasterserverError = false ;
masterServererErrorToShow = " --- " ;
2010-05-17 18:02:47 +02:00
lastSetChangedGameSettings = 0 ;
lastMasterserverPublishing = 0 ;
2010-05-16 18:40:21 +02:00
soundConnectionCount = 0 ;
2010-03-13 23:00:01 +01:00
2010-05-17 22:05:31 +02:00
mainMessageBox . init ( lang . get ( " Ok " ) ) ;
2010-05-17 01:03:22 +02:00
mainMessageBox . setEnabled ( false ) ;
mainMessageBoxState = 0 ;
2010-03-27 08:09:34 +01:00
vector < string > teamItems , controlItems , results ;
2010-03-13 23:00:01 +01:00
//create
2010-06-01 02:32:24 +02:00
buttonReturn . init ( 300 , 180 , 125 ) ;
buttonRestoreLastSettings . init ( 440 , 180 , 125 ) ;
buttonPlayNow . init ( 580 , 180 , 125 ) ;
2010-03-13 23:00:01 +01:00
//map listBox
2010-03-25 13:15:10 +01:00
// 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)
set < string > allMaps ;
2010-03-27 08:09:34 +01:00
findAll ( config . getPathListForType ( ptMaps ) , " *.gbm " , results , true , false ) ;
copy ( results . begin ( ) , results . end ( ) , std : : inserter ( allMaps , allMaps . begin ( ) ) ) ;
results . clear ( ) ;
findAll ( config . getPathListForType ( ptMaps ) , " *.mgm " , results , true , false ) ;
copy ( results . begin ( ) , results . end ( ) , std : : inserter ( allMaps , allMaps . begin ( ) ) ) ;
results . clear ( ) ;
if ( allMaps . empty ( ) ) {
throw runtime_error ( " No maps were found! " ) ;
2010-03-13 23:00:01 +01:00
}
2010-03-27 08:09:34 +01:00
copy ( allMaps . begin ( ) , allMaps . end ( ) , std : : back_inserter ( results ) ) ;
mapFiles = results ;
std : : for_each ( results . begin ( ) , results . end ( ) , FormatString ( ) ) ;
listBoxMap . init ( 100 , 260 , 200 ) ;
listBoxMap . setItems ( results ) ;
labelMap . init ( 100 , 290 ) ;
labelMapInfo . init ( 100 , 230 , 200 , 40 ) ;
2010-05-13 00:58:00 +02:00
2010-03-27 08:09:34 +01:00
// fog - o - war
// @350 ? 300 ?
2010-05-31 12:20:18 +02:00
labelFogOfWar . init ( 320 , 290 , 65 ) ;
listBoxFogOfWar . init ( 320 , 260 , 65 ) ;
2010-03-27 17:08:03 +01:00
listBoxFogOfWar . pushBackItem ( lang . get ( " Yes " ) ) ;
listBoxFogOfWar . pushBackItem ( lang . get ( " No " ) ) ;
2010-03-27 08:09:34 +01:00
listBoxFogOfWar . setSelectedItemIndex ( 0 ) ;
2010-03-13 23:00:01 +01:00
2010-05-31 12:20:18 +02:00
// Enable Observer Mode
labelEnableObserverMode . init ( 390 , 290 , 80 ) ;
listBoxEnableObserverMode . init ( 390 , 260 , 80 ) ;
listBoxEnableObserverMode . pushBackItem ( lang . get ( " Yes " ) ) ;
listBoxEnableObserverMode . pushBackItem ( lang . get ( " No " ) ) ;
listBoxEnableObserverMode . setSelectedItemIndex ( 0 ) ;
2010-06-03 03:10:40 +02:00
// Enable Server Controlled AI
labelEnableServerControlledAI . init ( 390 , 235 , 80 ) ;
listBoxEnableServerControlledAI . init ( 390 , 215 , 80 ) ;
listBoxEnableServerControlledAI . pushBackItem ( lang . get ( " Yes " ) ) ;
listBoxEnableServerControlledAI . pushBackItem ( lang . get ( " No " ) ) ;
listBoxEnableServerControlledAI . setSelectedItemIndex ( 1 ) ;
2010-03-13 23:00:01 +01:00
//tileset listBox
2010-03-18 22:26:40 +01:00
findDirs ( config . getPathListForType ( ptTilesets ) , results ) ;
2010-03-27 08:09:34 +01:00
if ( results . empty ( ) ) {
throw runtime_error ( " No tile-sets were found! " ) ;
2010-03-13 23:00:01 +01:00
}
tilesetFiles = results ;
2010-03-27 08:09:34 +01:00
std : : for_each ( results . begin ( ) , results . end ( ) , FormatString ( ) ) ;
listBoxTileset . init ( 500 , 260 , 150 ) ;
2010-03-13 23:00:01 +01:00
listBoxTileset . setItems ( results ) ;
2010-03-27 08:09:34 +01:00
labelTileset . init ( 500 , 290 ) ;
2010-03-13 23:00:01 +01:00
//tech Tree listBox
2010-03-18 22:26:40 +01:00
findDirs ( config . getPathListForType ( ptTechs ) , results ) ;
2010-03-27 08:09:34 +01:00
if ( results . empty ( ) ) {
throw runtime_error ( " No tech-trees were found! " ) ;
2010-03-13 23:00:01 +01:00
}
techTreeFiles = results ;
2010-03-27 08:09:34 +01:00
std : : for_each ( results . begin ( ) , results . end ( ) , FormatString ( ) ) ;
listBoxTechTree . init ( 700 , 260 , 150 ) ;
2010-03-13 23:00:01 +01:00
listBoxTechTree . setItems ( results ) ;
2010-03-27 08:09:34 +01:00
labelTechTree . init ( 700 , 290 ) ;
2010-03-13 23:00:01 +01:00
2010-05-13 00:58:00 +02:00
2010-05-13 02:42:49 +02:00
labelPublishServer . init ( 350 , 690 , 100 ) ;
2010-05-13 09:22:33 +02:00
labelPublishServer . setText ( lang . get ( " PublishServer " ) ) ;
2010-05-13 02:42:49 +02:00
listBoxPublishServer . init ( 350 , 660 , 100 ) ;
2010-05-13 00:58:00 +02:00
listBoxPublishServer . pushBackItem ( lang . get ( " Yes " ) ) ;
listBoxPublishServer . pushBackItem ( lang . get ( " No " ) ) ;
listBoxPublishServer . setSelectedItemIndex ( 1 ) ;
2010-03-13 23:00:01 +01:00
//list boxes
for ( int i = 0 ; i < GameConstants : : maxPlayers ; + + i ) {
2010-04-04 01:31:10 +02:00
labelPlayers [ i ] . init ( 100 , 550 - i * 30 ) ;
listBoxControls [ i ] . init ( 200 , 550 - i * 30 ) ;
listBoxFactions [ i ] . init ( 400 , 550 - i * 30 ) ;
listBoxTeams [ i ] . init ( 600 , 550 - i * 30 , 60 ) ;
labelNetStatus [ i ] . init ( 700 , 550 - i * 30 , 60 ) ;
2010-03-13 23:00:01 +01:00
}
2010-04-04 01:31:10 +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 ) ;
2010-03-13 23:00:01 +01:00
//texts
buttonReturn . setText ( lang . get ( " Return " ) ) ;
buttonPlayNow . setText ( lang . get ( " PlayNow " ) ) ;
2010-06-01 02:32:24 +02:00
buttonRestoreLastSettings . setText ( lang . get ( " ReloadLastGameSettings " ) ) ;
2010-03-13 23:00:01 +01:00
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 " ) ;
reloadFactions ( ) ;
2010-03-18 22:26:40 +01: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 ;
}
}
if ( results . size ( ) = = 0 ) {
2010-03-20 11:52:55 +01:00
throw runtime_error ( " (1) There are no factions for the tech tree [ " + techTreeFiles[listBoxTechTree.getSelectedItemIndex()] + " ] " ) ;
2010-03-18 22:26:40 +01:00
}
2010-03-13 23:00:01 +01:00
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 ) ;
labelNetStatus [ i ] . setText ( " " ) ;
}
labelMap . setText ( lang . get ( " Map " ) ) ;
2010-03-27 08:09:34 +01:00
labelFogOfWar . setText ( lang . get ( " FogOfWar " ) ) ;
2010-03-13 23:00:01 +01:00
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 " ) ) ;
2010-05-31 12:20:18 +02:00
labelEnableObserverMode . setText ( lang . get ( " EnableObserverMode " ) ) ;
2010-06-03 03:10:40 +02:00
labelEnableServerControlledAI . setText ( lang . get ( " EnableServerControlledAI " ) ) ;
2010-05-31 12:20:18 +02:00
2010-03-13 23:00:01 +01:00
loadMapInfo ( Map : : getMapPath ( mapFiles [ listBoxMap . getSelectedItemIndex ( ) ] ) , & mapInfo ) ;
labelMapInfo . setText ( mapInfo . desc ) ;
2010-06-01 01:39:07 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-13 23:00:01 +01:00
//initialize network interface
networkManager . init ( nrServer ) ;
//init controllers
listBoxControls [ 0 ] . setSelectedItemIndex ( ctHuman ) ;
if ( openNetworkSlots ) {
for ( int i = 1 ; i < mapInfo . players ; + + i ) {
listBoxControls [ i ] . setSelectedItemIndex ( ctNetwork ) ;
}
}
else {
listBoxControls [ 1 ] . setSelectedItemIndex ( ctCpu ) ;
}
updateControlers ( ) ;
updateNetworkSlots ( ) ;
2010-06-01 01:39:07 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-31 21:57:10 +02:00
// Ensure we have set the gamesettings at least once
2010-06-01 02:32:24 +02:00
GameSettings gameSettings ;
loadGameSettings ( & gameSettings ) ;
2010-05-31 08:11:31 +02:00
ServerInterface * serverInterface = NetworkManager : : getInstance ( ) . getServerInterface ( ) ;
2010-05-31 22:49:23 +02:00
serverInterface - > setGameSettings ( & gameSettings , false ) ;
2010-05-31 08:11:31 +02:00
2010-06-01 01:39:07 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-13 23:00:01 +01:00
//chatManager.init(&console, world.getThisTeamIndex());
chatManager . init ( & console , - 1 ) ;
2010-05-13 08:43:56 +02:00
2010-06-01 21:48:35 +02:00
publishToMasterserverThread = new SimpleTaskThread ( this , 0 , 50 ) ;
2010-05-13 08:43:56 +02:00
publishToMasterserverThread - > start ( ) ;
2010-06-01 01:39:07 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-13 08:43:56 +02:00
}
MenuStateCustomGame : : ~ MenuStateCustomGame ( ) {
2010-06-01 01:39:07 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-13 23:47:14 +02:00
needToBroadcastServerSettings = false ;
needToRepublishToMasterserver = false ;
2010-05-13 08:43:56 +02:00
BaseThread : : shutdownAndWait ( publishToMasterserverThread ) ;
2010-06-01 01:39:07 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-13 08:43:56 +02:00
delete publishToMasterserverThread ;
publishToMasterserverThread = NULL ;
2010-06-01 01:39:07 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-13 23:00:01 +01:00
}
2010-05-16 18:40:21 +02:00
void MenuStateCustomGame : : returnToParentMenu ( ) {
needToBroadcastServerSettings = false ;
needToRepublishToMasterserver = false ;
BaseThread : : shutdownAndWait ( publishToMasterserverThread ) ;
if ( parentMenuIsMs )
{
mainMenu - > setState ( new MenuStateMasterserver ( program , mainMenu ) ) ;
}
else
{
mainMenu - > setState ( new MenuStateNewGame ( program , mainMenu ) ) ;
}
}
2010-03-13 23:00:01 +01:00
void MenuStateCustomGame : : mouseClick ( int x , int y , MouseButton mouseButton ) {
CoreData & coreData = CoreData : : getInstance ( ) ;
SoundRenderer & soundRenderer = SoundRenderer : : getInstance ( ) ;
ServerInterface * serverInterface = NetworkManager : : getInstance ( ) . getServerInterface ( ) ;
2010-05-17 01:03:22 +02:00
if ( mainMessageBox . getEnabled ( ) ) {
int button = 1 ;
if ( mainMessageBox . mouseClick ( x , y , button ) )
{
soundRenderer . playFx ( coreData . getClickSoundA ( ) ) ;
if ( button = = 1 )
{
mainMessageBox . setEnabled ( false ) ;
}
}
}
else if ( buttonReturn . mouseClick ( x , y ) ) {
2010-04-12 23:40:57 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-13 23:00:01 +01:00
soundRenderer . playFx ( coreData . getClickSoundA ( ) ) ;
2010-05-13 08:43:56 +02:00
/*
if ( listBoxPublishServer . getEditable ( ) & &
listBoxPublishServer . getSelectedItemIndex ( ) = = 0 ) {
needToRepublishToMasterserver = true ;
lastMasterserverPublishing = 0 ;
publishToMasterserver ( ) ;
simpleTask ( ) ;
}
*/
2010-05-31 22:49:23 +02:00
2010-05-16 18:40:21 +02:00
returnToParentMenu ( ) ;
2010-03-13 23:00:01 +01:00
}
else if ( buttonPlayNow . mouseClick ( x , y ) & & buttonPlayNow . getEnabled ( ) ) {
2010-04-12 23:40:57 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-13 23:00:01 +01:00
closeUnusedSlots ( ) ;
soundRenderer . playFx ( coreData . getClickSoundC ( ) ) ;
2010-04-12 23:40:57 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-13 23:00:01 +01:00
GameSettings gameSettings ;
loadGameSettings ( & gameSettings ) ;
2010-04-12 23:40:57 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-13 23:00:01 +01:00
// Send the game settings to each client if we have at least one networked client
if ( hasNetworkGameSettings ( ) = = true & &
needToSetChangedGameSettings = = true )
{
2010-04-12 23:40:57 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-13 23:00:01 +01:00
serverInterface - > setGameSettings ( & gameSettings , true ) ;
needToSetChangedGameSettings = false ;
lastSetChangedGameSettings = time ( NULL ) ;
}
2010-04-12 23:40:57 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-13 23:00:01 +01:00
bool bOkToStart = serverInterface - > launchGame ( & gameSettings ) ;
if ( bOkToStart = = true )
{
2010-05-13 08:43:56 +02:00
if ( listBoxPublishServer . getEditable ( ) & &
listBoxPublishServer . getSelectedItemIndex ( ) = = 0 ) {
2010-06-01 01:39:07 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-13 08:43:56 +02:00
needToRepublishToMasterserver = true ;
lastMasterserverPublishing = 0 ;
publishToMasterserver ( ) ;
simpleTask ( ) ;
2010-06-01 01:39:07 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-13 08:43:56 +02:00
}
2010-05-13 23:47:14 +02:00
needToBroadcastServerSettings = false ;
needToRepublishToMasterserver = false ;
2010-05-31 21:57:10 +02:00
2010-05-31 22:49:23 +02:00
saveGameSettingsToFile ( " lastCustomGamSettings.mgg " ) ;
2010-05-13 09:22:33 +02:00
BaseThread : : shutdownAndWait ( publishToMasterserverThread ) ;
2010-06-01 21:48:35 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
delete publishToMasterserverThread ;
publishToMasterserverThread = NULL ;
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
assert ( program ! = NULL ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-13 23:00:01 +01:00
program - > setState ( new Game ( program , & gameSettings ) ) ;
}
2010-04-12 23:40:57 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-13 23:00:01 +01:00
}
2010-06-01 02:32:24 +02:00
else if ( buttonRestoreLastSettings . mouseClick ( x , y ) & & buttonRestoreLastSettings . getEnabled ( ) ) {
// Ensure we have set the gamesettings at least once
GameSettings gameSettings = loadGameSettingsFromFile ( " lastCustomGamSettings.mgg " ) ;
if ( gameSettings . getMap ( ) = = " " ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
loadGameSettings ( & gameSettings ) ;
}
ServerInterface * serverInterface = NetworkManager : : getInstance ( ) . getServerInterface ( ) ;
serverInterface - > setGameSettings ( & gameSettings , false ) ;
needToRepublishToMasterserver = true ;
if ( hasNetworkGameSettings ( ) = = true )
{
needToSetChangedGameSettings = true ;
lastSetChangedGameSettings = time ( NULL ) ;
}
}
2010-03-13 23:00:01 +01:00
else if ( listBoxMap . mouseClick ( x , y ) ) {
2010-03-20 00:26:00 +01:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " %s \n " , mapFiles [ listBoxMap . getSelectedItemIndex ( ) ] . c_str ( ) ) ;
2010-03-13 23:00:01 +01:00
loadMapInfo ( Map : : getMapPath ( mapFiles [ listBoxMap . getSelectedItemIndex ( ) ] ) , & mapInfo ) ;
labelMapInfo . setText ( mapInfo . desc ) ;
updateControlers ( ) ;
2010-04-23 02:14:22 +02:00
updateNetworkSlots ( ) ;
2010-05-13 08:43:56 +02:00
needToRepublishToMasterserver = true ;
2010-03-13 23:00:01 +01:00
2010-03-27 08:09:34 +01:00
if ( hasNetworkGameSettings ( ) = = true )
{
needToSetChangedGameSettings = true ;
2010-05-13 08:43:56 +02:00
lastSetChangedGameSettings = time ( NULL ) ;
2010-03-27 08:09:34 +01:00
}
2010-05-31 22:49:23 +02:00
saveGameSettingsToFile ( " lastCustomGamSettings.mgg " ) ;
2010-03-27 08:09:34 +01:00
}
else if ( listBoxFogOfWar . mouseClick ( x , y ) ) {
2010-05-13 08:43:56 +02:00
needToRepublishToMasterserver = true ;
2010-05-31 12:20:18 +02:00
if ( hasNetworkGameSettings ( ) = = true )
{
needToSetChangedGameSettings = true ;
lastSetChangedGameSettings = time ( NULL ) ;
}
2010-05-31 22:49:23 +02:00
saveGameSettingsToFile ( " lastCustomGamSettings.mgg " ) ;
2010-05-31 12:20:18 +02:00
}
else if ( listBoxEnableObserverMode . mouseClick ( x , y ) ) {
needToRepublishToMasterserver = true ;
2010-06-03 03:10:40 +02:00
if ( hasNetworkGameSettings ( ) = = true )
{
needToSetChangedGameSettings = true ;
lastSetChangedGameSettings = time ( NULL ) ;
}
saveGameSettingsToFile ( " lastCustomGamSettings.mgg " ) ;
}
else if ( listBoxEnableServerControlledAI . mouseClick ( x , y ) ) {
needToRepublishToMasterserver = true ;
2010-03-13 23:00:01 +01:00
if ( hasNetworkGameSettings ( ) = = true )
{
needToSetChangedGameSettings = true ;
2010-05-13 08:43:56 +02:00
lastSetChangedGameSettings = time ( NULL ) ;
2010-03-13 23:00:01 +01:00
}
2010-05-31 22:49:23 +02:00
saveGameSettingsToFile ( " lastCustomGamSettings.mgg " ) ;
2010-03-13 23:00:01 +01:00
}
else if ( listBoxTileset . mouseClick ( x , y ) ) {
2010-05-13 08:43:56 +02:00
needToRepublishToMasterserver = true ;
2010-03-13 23:00:01 +01:00
if ( hasNetworkGameSettings ( ) = = true )
{
needToSetChangedGameSettings = true ;
2010-05-13 08:43:56 +02:00
lastSetChangedGameSettings = time ( NULL ) ;
2010-03-13 23:00:01 +01:00
}
2010-05-31 22:49:23 +02:00
saveGameSettingsToFile ( " lastCustomGamSettings.mgg " ) ;
2010-03-13 23:00:01 +01:00
}
else if ( listBoxTechTree . mouseClick ( x , y ) ) {
reloadFactions ( ) ;
2010-05-13 08:43:56 +02:00
needToRepublishToMasterserver = true ;
2010-03-13 23:00:01 +01:00
if ( hasNetworkGameSettings ( ) = = true )
{
needToSetChangedGameSettings = true ;
2010-05-13 08:43:56 +02:00
lastSetChangedGameSettings = time ( NULL ) ;
2010-03-13 23:00:01 +01:00
}
2010-05-31 22:49:23 +02:00
saveGameSettingsToFile ( " lastCustomGamSettings.mgg " ) ;
2010-03-13 23:00:01 +01:00
}
2010-05-13 00:58:00 +02:00
else if ( listBoxPublishServer . mouseClick ( x , y ) & & listBoxPublishServer . getEditable ( ) ) {
2010-05-13 08:43:56 +02:00
needToRepublishToMasterserver = true ;
2010-05-13 00:58:00 +02:00
soundRenderer . playFx ( coreData . getClickSoundC ( ) ) ;
2010-05-31 22:49:23 +02:00
saveGameSettingsToFile ( " lastCustomGamSettings.mgg " ) ;
2010-05-13 00:58:00 +02:00
}
2010-03-13 23:00:01 +01:00
else
{
for ( int i = 0 ; i < mapInfo . players ; + + i )
{
//ensure thet only 1 human player is present
if ( listBoxControls [ i ] . mouseClick ( x , y ) )
{
//look for human players
int humanIndex1 = - 1 ;
int humanIndex2 = - 1 ;
for ( int j = 0 ; j < GameConstants : : maxPlayers ; + + j ) {
ControlType ct = static_cast < ControlType > ( listBoxControls [ j ] . getSelectedItemIndex ( ) ) ;
if ( ct = = ctHuman ) {
if ( humanIndex1 = = - 1 ) {
humanIndex1 = j ;
}
else {
humanIndex2 = j ;
}
}
}
//no human
if ( humanIndex1 = = - 1 & & humanIndex2 = = - 1 ) {
listBoxControls [ i ] . setSelectedItemIndex ( ctHuman ) ;
}
//2 humans
if ( humanIndex1 ! = - 1 & & humanIndex2 ! = - 1 ) {
listBoxControls [ humanIndex1 = = i ? humanIndex2 : humanIndex1 ] . setSelectedItemIndex ( ctClosed ) ;
}
updateNetworkSlots ( ) ;
2010-05-13 08:43:56 +02:00
needToRepublishToMasterserver = true ;
2010-03-13 23:00:01 +01:00
if ( hasNetworkGameSettings ( ) = = true )
{
needToSetChangedGameSettings = true ;
lastSetChangedGameSettings = time ( NULL ) ; ;
}
2010-05-31 22:49:23 +02:00
saveGameSettingsToFile ( " lastCustomGamSettings.mgg " ) ;
2010-03-13 23:00:01 +01:00
}
else if ( listBoxFactions [ i ] . mouseClick ( x , y ) ) {
2010-05-13 08:43:56 +02:00
needToRepublishToMasterserver = true ;
2010-03-13 23:00:01 +01:00
if ( hasNetworkGameSettings ( ) = = true )
{
needToSetChangedGameSettings = true ;
lastSetChangedGameSettings = time ( NULL ) ; ;
}
2010-05-31 22:49:23 +02:00
saveGameSettingsToFile ( " lastCustomGamSettings.mgg " ) ;
2010-03-13 23:00:01 +01:00
}
else if ( listBoxTeams [ i ] . mouseClick ( x , y ) )
{
2010-05-13 08:43:56 +02:00
needToRepublishToMasterserver = true ;
2010-03-13 23:00:01 +01:00
if ( hasNetworkGameSettings ( ) = = true )
{
needToSetChangedGameSettings = true ;
lastSetChangedGameSettings = time ( NULL ) ; ;
}
2010-05-31 22:49:23 +02:00
saveGameSettingsToFile ( " lastCustomGamSettings.mgg " ) ;
2010-03-13 23:00:01 +01:00
}
}
}
2010-05-31 21:57:10 +02:00
2010-04-12 23:40:57 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-13 23:00:01 +01:00
}
void MenuStateCustomGame : : mouseMove ( int x , int y , const MouseState * ms ) {
2010-05-17 01:03:22 +02:00
if ( mainMessageBox . getEnabled ( ) ) {
mainMessageBox . mouseMove ( x , y ) ;
}
2010-03-13 23:00:01 +01:00
buttonReturn . mouseMove ( x , y ) ;
buttonPlayNow . mouseMove ( x , y ) ;
2010-06-01 02:32:24 +02:00
buttonRestoreLastSettings . mouseMove ( x , y ) ;
2010-03-13 23:00:01 +01:00
for ( int i = 0 ; i < GameConstants : : maxPlayers ; + + i ) {
listBoxControls [ i ] . mouseMove ( x , y ) ;
listBoxFactions [ i ] . mouseMove ( x , y ) ;
listBoxTeams [ i ] . mouseMove ( x , y ) ;
}
listBoxMap . mouseMove ( x , y ) ;
2010-03-27 08:09:34 +01:00
listBoxFogOfWar . mouseMove ( x , y ) ;
2010-03-13 23:00:01 +01:00
listBoxTileset . mouseMove ( x , y ) ;
listBoxTechTree . mouseMove ( x , y ) ;
2010-05-13 00:58:00 +02:00
listBoxPublishServer . mouseMove ( x , y ) ;
2010-05-31 12:20:18 +02:00
listBoxEnableObserverMode . mouseMove ( x , y ) ;
2010-06-03 03:10:40 +02:00
listBoxEnableServerControlledAI . mouseMove ( x , y ) ;
2010-03-13 23:00:01 +01:00
}
void MenuStateCustomGame : : render ( ) {
2010-04-11 03:25:06 +02:00
try {
Renderer & renderer = Renderer : : getInstance ( ) ;
2010-03-13 23:00:01 +01:00
2010-05-17 01:03:22 +02:00
if ( mainMessageBox . getEnabled ( ) ) {
2010-06-01 01:39:07 +02:00
renderer . renderMessageBox ( & mainMessageBox ) ;
2010-03-13 23:00:01 +01:00
}
2010-05-17 01:03:22 +02:00
else
2010-05-13 00:58:00 +02:00
{
2010-05-17 01:03:22 +02:00
int i ;
renderer . renderButton ( & buttonReturn ) ;
renderer . renderButton ( & buttonPlayNow ) ;
2010-06-01 02:32:24 +02:00
renderer . renderButton ( & buttonRestoreLastSettings ) ;
2010-05-17 01:03:22 +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 ] ) ;
renderer . renderLabel ( & labelNetStatus [ i ] ) ;
}
}
renderer . renderLabel ( & labelMap ) ;
renderer . renderLabel ( & labelFogOfWar ) ;
renderer . renderLabel ( & labelTileset ) ;
renderer . renderLabel ( & labelTechTree ) ;
renderer . renderLabel ( & labelControl ) ;
renderer . renderLabel ( & labelFaction ) ;
renderer . renderLabel ( & labelTeam ) ;
renderer . renderLabel ( & labelMapInfo ) ;
2010-05-31 12:20:18 +02:00
renderer . renderLabel ( & labelEnableObserverMode ) ;
2010-06-03 03:10:40 +02:00
renderer . renderLabel ( & labelEnableServerControlledAI ) ;
2010-05-17 01:03:22 +02:00
renderer . renderListBox ( & listBoxMap ) ;
renderer . renderListBox ( & listBoxFogOfWar ) ;
renderer . renderListBox ( & listBoxTileset ) ;
renderer . renderListBox ( & listBoxTechTree ) ;
2010-05-31 12:20:18 +02:00
renderer . renderListBox ( & listBoxEnableObserverMode ) ;
2010-06-03 03:10:40 +02:00
renderer . renderListBox ( & listBoxEnableServerControlledAI ) ;
2010-05-17 01:03:22 +02:00
renderer . renderChatManager ( & chatManager ) ;
renderer . renderConsole ( & console ) ;
if ( listBoxPublishServer . getEditable ( ) )
{
renderer . renderListBox ( & listBoxPublishServer ) ;
renderer . renderLabel ( & labelPublishServer ) ;
}
2010-05-13 00:58:00 +02:00
}
2010-04-11 03:25:06 +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 ) ;
}
2010-03-13 23:00:01 +01:00
}
void MenuStateCustomGame : : update ( )
{
2010-04-11 03:25:06 +02:00
try {
2010-06-01 01:39:07 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-13 23:00:01 +01:00
2010-04-11 03:25:06 +02:00
ServerInterface * serverInterface = NetworkManager : : getInstance ( ) . getServerInterface ( ) ;
Lang & lang = Lang : : getInstance ( ) ;
bool haveAtLeastOneNetworkClientConnected = false ;
2010-05-13 00:58:00 +02:00
bool hasOneNetworkSlotOpen = false ;
2010-05-16 18:40:21 +02:00
int currentConnectionCount = 0 ;
2010-04-11 03:25:06 +02:00
Config & config = Config : : getInstance ( ) ;
2010-04-30 03:08:29 +02:00
2010-05-17 01:03:22 +02:00
if ( showMasterserverError )
{
2010-05-17 07:38:07 +02:00
if ( EndsWith ( masterServererErrorToShow , " wrong router setup " ) = = true )
2010-05-17 01:03:22 +02:00
{
2010-05-17 07:38:07 +02:00
masterServererErrorToShow = lang . get ( " wrong router setup " ) ;
2010-05-17 01:03:22 +02:00
}
2010-05-17 22:05:31 +02:00
showMasterserverError = false ;
2010-05-17 01:03:22 +02:00
listBoxPublishServer . setSelectedItemIndex ( 1 ) ;
mainMessageBoxState = 1 ;
showMessageBox ( masterServererErrorToShow , lang . get ( " ErrorFromMasterserver " ) , false ) ;
}
2010-04-30 03:08:29 +02:00
2010-06-01 01:39:07 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-30 03:08:29 +02:00
// handle setting changes from clients
SwitchSetupRequest * * switchSetupRequests = serverInterface - > getSwitchSetupRequests ( ) ;
for ( int i = 0 ; i < mapInfo . players ; + + i )
{
if ( switchSetupRequests [ i ] ! = NULL )
{
2010-05-17 08:41:05 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-30 03:08:29 +02:00
if ( listBoxControls [ i ] . getSelectedItemIndex ( ) = = ctNetwork )
{
2010-05-17 08:41:05 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-30 03:08:29 +02:00
//printf("switchSetupRequests[i]->getSelectedFactionName()=%s\n",switchSetupRequests[i]->getSelectedFactionName().c_str());
//printf("switchSetupRequests[i]->getToTeam()=%d\n",switchSetupRequests[i]->getToTeam());
if ( switchSetupRequests [ i ] - > getToFactionIndex ( ) ! = - 1 )
{
//printf("switchSlot request from %d to %d\n",switchSetupRequests[i]->getCurrentFactionIndex(),switchSetupRequests[i]->getToFactionIndex());
if ( serverInterface - > switchSlot ( switchSetupRequests [ i ] - > getCurrentFactionIndex ( ) , switchSetupRequests [ i ] - > getToFactionIndex ( ) ) ) {
int k = switchSetupRequests [ i ] - > getToFactionIndex ( ) ;
try {
if ( switchSetupRequests [ i ] - > getSelectedFactionName ( ) ! = " " ) {
listBoxFactions [ k ] . setSelectedItem ( switchSetupRequests [ i ] - > getSelectedFactionName ( ) ) ;
}
if ( switchSetupRequests [ i ] - > getToTeam ( ) ! = - 1 )
listBoxTeams [ k ] . setSelectedItemIndex ( switchSetupRequests [ i ] - > getToTeam ( ) ) ;
}
catch ( const runtime_error & e ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] caught exception error = [%s] \n " , __FILE__ , __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 ( ) ) ;
}
catch ( const runtime_error & e ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] caught exception error = [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , e . what ( ) ) ;
}
}
}
delete switchSetupRequests [ i ] ;
switchSetupRequests [ i ] = NULL ;
}
}
2010-06-01 01:39:07 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] mapInfo.players = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , mapInfo . players ) ;
2010-04-11 03:25:06 +02:00
for ( int i = 0 ; i < mapInfo . players ; + + i )
2010-03-13 23:00:01 +01:00
{
2010-06-01 01:39:07 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-11 03:25:06 +02:00
if ( listBoxControls [ i ] . getSelectedItemIndex ( ) = = ctNetwork )
{
2010-06-01 01:39:07 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-11 03:25:06 +02:00
ConnectionSlot * connectionSlot = serverInterface - > getSlot ( i ) ;
assert ( connectionSlot ! = NULL ) ;
2010-05-13 00:58:00 +02:00
2010-06-01 01:39:07 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-13 00:58:00 +02:00
hasOneNetworkSlotOpen = true ;
2010-03-13 23:00:01 +01:00
2010-04-11 03:25:06 +02:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] A - ctNetwork\n",__FILE__,__FUNCTION__);
2010-03-13 23:00:01 +01:00
2010-04-11 03:25:06 +02:00
if ( connectionSlot - > isConnected ( ) )
{
2010-05-17 08:41:05 +02:00
//printf("FYI we have at least 1 client connected, slot = %d'\n",i);
2010-04-11 03:25:06 +02:00
haveAtLeastOneNetworkClientConnected = true ;
2010-05-20 01:23:42 +02:00
if ( connectionSlot - > getConnectHasHandshaked ( ) )
2010-05-17 22:05:31 +02:00
currentConnectionCount + + ;
2010-04-11 03:25:06 +02:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] B - ctNetwork\n",__FILE__,__FUNCTION__);
string label = connectionSlot - > getName ( ) ;
if ( connectionSlot - > getAllowDownloadDataSynch ( ) = = true & &
connectionSlot - > getAllowGameDataSynchCheck ( ) = = true )
{
if ( connectionSlot - > getNetworkGameDataSynchCheckOk ( ) = = false )
{
label = connectionSlot - > getName ( ) + " - waiting to synch: " ;
if ( connectionSlot - > getNetworkGameDataSynchCheckOkMap ( ) = = false )
{
label = label + " map " ;
}
if ( connectionSlot - > getNetworkGameDataSynchCheckOkTile ( ) = = false )
{
label = label + " tile " ;
}
if ( connectionSlot - > getNetworkGameDataSynchCheckOkTech ( ) = = false )
{
label = label + " techtree " ;
}
}
else
{
label = connectionSlot - > getName ( ) + " - data synch is ok " ;
}
}
else
{
label = connectionSlot - > getName ( ) ;
if ( connectionSlot - > getAllowGameDataSynchCheck ( ) = = true & &
connectionSlot - > getNetworkGameDataSynchCheckOk ( ) = = false )
{
label + = " - warning synch mismatch for: " ;
if ( connectionSlot - > getNetworkGameDataSynchCheckOkMap ( ) = = false )
{
label = label + " map " ;
}
if ( connectionSlot - > getNetworkGameDataSynchCheckOkTile ( ) = = false )
{
label = label + " tile " ;
}
if ( connectionSlot - > getNetworkGameDataSynchCheckOkTech ( ) = = false )
{
label = label + " techtree " ;
}
}
}
2010-03-13 23:00:01 +01:00
2010-04-11 03:25:06 +02:00
labelNetStatus [ i ] . setText ( label ) ;
2010-04-04 01:31:10 +02:00
}
else
{
2010-04-11 03:25:06 +02:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] C - ctNetwork\n",__FILE__,__FUNCTION__);
string port = intToStr ( config . getInt ( " ServerPort " ) ) ;
if ( port ! = " 61357 " ) {
port = port + lang . get ( " NonStandardPort " ) + " ! " ;
}
else
{
port = port + " ) " ;
}
port = " ( " + port ;
labelNetStatus [ i ] . setText ( " --- " + port ) ;
2010-04-04 01:31:10 +02:00
}
2010-03-13 23:00:01 +01:00
2010-04-11 03:25:06 +02:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] END - ctNetwork\n",__FILE__,__FUNCTION__);
}
else {
labelNetStatus [ i ] . setText ( " " ) ;
}
2010-03-13 23:00:01 +01:00
}
2010-06-01 01:39:07 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-04-11 03:25:06 +02:00
// Send the game settings to each client if we have at least one networked client
if ( serverInterface - > getAllowGameDataSynchCheck ( ) = = true & &
2010-05-17 18:02:47 +02:00
//haveAtLeastOneNetworkClientConnected == true &&
2010-04-11 03:25:06 +02:00
needToSetChangedGameSettings = = true & &
difftime ( time ( NULL ) , lastSetChangedGameSettings ) > = 2 )
{
GameSettings gameSettings ;
loadGameSettings ( & gameSettings ) ;
serverInterface - > setGameSettings ( & gameSettings ) ;
2010-03-13 23:00:01 +01:00
2010-04-11 03:25:06 +02:00
needToSetChangedGameSettings = false ;
2010-04-22 01:13:39 +02:00
}
2010-06-01 01:39:07 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-13 00:58:00 +02:00
if ( hasOneNetworkSlotOpen )
{
//listBoxPublishServer.setSelectedItemIndex(0);
listBoxPublishServer . setEditable ( true ) ;
}
else
{
listBoxPublishServer . setSelectedItemIndex ( 1 ) ;
listBoxPublishServer . setEditable ( false ) ;
}
2010-05-13 13:19:10 +02:00
if ( difftime ( time ( NULL ) , lastMasterserverPublishing ) > = 5 ) {
needToRepublishToMasterserver = true ;
lastMasterserverPublishing = time ( NULL ) ;
}
2010-05-13 08:43:56 +02:00
if ( listBoxPublishServer . getEditable ( ) & &
listBoxPublishServer . getSelectedItemIndex ( ) = = 0 & &
needToRepublishToMasterserver = = true ) {
2010-05-13 00:58:00 +02:00
// give it to me baby, aha aha ...
publishToMasterserver ( ) ;
}
2010-04-22 01:13:39 +02:00
if ( difftime ( time ( NULL ) , lastSetChangedGameSettings ) > = 2 )
{
2010-05-13 13:19:10 +02:00
needToBroadcastServerSettings = true ;
2010-04-11 03:25:06 +02:00
}
//call the chat manager
chatManager . updateNetwork ( ) ;
2010-03-13 23:00:01 +01:00
2010-04-11 03:25:06 +02:00
//console
console . update ( ) ;
2010-03-13 23:00:01 +01:00
2010-04-22 01:13:39 +02:00
if ( difftime ( time ( NULL ) , lastSetChangedGameSettings ) > = 2 )
{ // reset timer here on bottom becasue used for different things
lastSetChangedGameSettings = time ( NULL ) ;
}
2010-06-01 01:39:07 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-16 18:40:21 +02:00
if ( currentConnectionCount > soundConnectionCount ) {
2010-05-20 01:23:42 +02:00
soundConnectionCount = currentConnectionCount ;
2010-05-16 18:40:21 +02:00
SoundRenderer : : getInstance ( ) . playFx ( CoreData : : getInstance ( ) . getAttentionSound ( ) ) ;
}
soundConnectionCount = currentConnectionCount ;
2010-04-11 03:25:06 +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 ) ;
}
2010-03-13 23:00:01 +01:00
}
2010-05-13 00:58:00 +02:00
void MenuStateCustomGame : : publishToMasterserver ( )
{
int slotCountUsed = 0 ;
int slotCountHumans = 0 ;
int slotCountConnectedPlayers = 0 ;
ServerInterface * serverInterface = NetworkManager : : getInstance ( ) . getServerInterface ( ) ;
GameSettings gameSettings ;
loadGameSettings ( & gameSettings ) ;
string serverinfo = " " ;
2010-05-13 08:43:56 +02:00
for ( int i = 0 ; i < mapInfo . players ; + + i )
2010-05-13 00:58:00 +02:00
{
if ( listBoxControls [ i ] . getSelectedItemIndex ( ) ! = ctClosed )
{
slotCountUsed + + ;
}
2010-05-13 08:43:56 +02:00
2010-05-13 00:58:00 +02:00
if ( listBoxControls [ i ] . getSelectedItemIndex ( ) = = ctNetwork )
{
slotCountHumans + + ;
ConnectionSlot * connectionSlot = serverInterface - > getSlot ( i ) ;
if ( ( connectionSlot ! = NULL ) & & ( connectionSlot - > isConnected ( ) ) )
{
slotCountConnectedPlayers + + ;
}
}
else if ( listBoxControls [ i ] . getSelectedItemIndex ( ) = = ctHuman )
{
slotCountHumans + + ;
slotCountConnectedPlayers + + ;
}
}
//?status=waiting&system=linux&info=titus
2010-05-13 08:43:56 +02:00
serverinfo + = " glestVersion= " + SystemFlags : : escapeURL ( glestVersionString ) + " & " ;
serverinfo + = " platform= " + SystemFlags : : escapeURL ( getPlatformNameString ( ) ) + " & " ;
serverinfo + = " binaryCompileDate= " + SystemFlags : : escapeURL ( getCompileDateTime ( ) ) + " & " ;
2010-05-13 00:58:00 +02:00
//game info:
2010-05-13 08:43:56 +02:00
serverinfo + = " serverTitle= " + SystemFlags : : escapeURL ( Config : : getInstance ( ) . getString ( " NetPlayerName " ) + " 's game " ) + " & " ;
2010-05-13 00:58:00 +02:00
//ip is automatically set
2010-05-13 08:43:56 +02:00
2010-05-13 00:58:00 +02:00
//game setup info:
2010-05-13 08:43:56 +02:00
serverinfo + = " tech= " + SystemFlags : : escapeURL ( listBoxTechTree . getSelectedItem ( ) ) + " & " ;
serverinfo + = " map= " + SystemFlags : : escapeURL ( listBoxMap . getSelectedItem ( ) ) + " & " ;
serverinfo + = " tileset= " + SystemFlags : : escapeURL ( listBoxTileset . getSelectedItem ( ) ) + " & " ;
serverinfo + = " activeSlots= " + intToStr ( slotCountUsed ) + " & " ;
serverinfo + = " networkSlots= " + intToStr ( slotCountHumans ) + " & " ;
serverinfo + = " connectedClients= " + intToStr ( slotCountConnectedPlayers ) ;
publishToServerInfo = serverinfo ;
2010-05-13 00:58:00 +02:00
}
2010-05-13 08:43:56 +02:00
void MenuStateCustomGame : : simpleTask ( ) {
2010-06-01 01:39:07 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-13 13:19:10 +02:00
if ( needToRepublishToMasterserver = = true & &
2010-05-13 09:13:53 +02:00
publishToServerInfo ! = " " ) {
2010-05-13 08:43:56 +02:00
2010-06-01 01:39:07 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-13 08:43:56 +02:00
needToRepublishToMasterserver = false ;
string request = Config : : getInstance ( ) . getString ( " Masterserver " ) + " addServerInfo.php? " + publishToServerInfo ;
2010-05-13 09:13:53 +02:00
publishToServerInfo = " " ;
2010-05-13 08:43:56 +02:00
printf ( " the request is: \n %s \n " , request . c_str ( ) ) ;
std : : string serverInfo = SystemFlags : : getHTTP ( request ) ;
2010-05-17 01:03:22 +02:00
printf ( " the result is: \n '%s' \n " , serverInfo . c_str ( ) ) ;
2010-05-17 07:38:07 +02:00
// uncomment to enable router setup check of this server
//if(serverInfo!="OK")
if ( EndsWith ( serverInfo , " OK " ) = = false )
{
showMasterserverError = true ;
masterServererErrorToShow = serverInfo ;
}
2010-05-13 08:43:56 +02:00
}
2010-06-01 01:39:07 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-13 13:19:10 +02:00
if ( needToBroadcastServerSettings )
2010-05-13 09:13:53 +02:00
{
2010-06-01 01:39:07 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-13 13:19:10 +02:00
needToBroadcastServerSettings = false ;
2010-05-13 09:13:53 +02:00
ServerInterface * serverInterface = NetworkManager : : getInstance ( ) . getServerInterface ( ) ;
2010-05-17 08:41:05 +02:00
if ( serverInterface - > hasClientConnection ( ) = = true ) {
//printf("Sending game settings broadcast since we have at least 1 client connected'\n");
GameSettings gameSettings ;
loadGameSettings ( & gameSettings ) ;
serverInterface - > setGameSettings ( & gameSettings ) ;
serverInterface - > broadcastGameSetup ( & gameSettings ) ;
}
2010-05-13 09:13:53 +02:00
}
2010-06-01 01:39:07 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-05-13 00:58:00 +02:00
}
2010-05-31 21:57:10 +02:00
void MenuStateCustomGame : : loadGameSettings ( GameSettings * gameSettings ) {
2010-04-11 03:25:06 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-13 23:00:01 +01:00
int factionCount = 0 ;
2010-04-23 01:18:04 +02:00
ServerInterface * serverInterface = NetworkManager : : getInstance ( ) . getServerInterface ( ) ;
2010-03-13 23:00:01 +01:00
2010-05-31 22:49:23 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-13 23:00:01 +01:00
gameSettings - > setDescription ( formatString ( mapFiles [ listBoxMap . getSelectedItemIndex ( ) ] ) ) ;
gameSettings - > setMap ( mapFiles [ listBoxMap . getSelectedItemIndex ( ) ] ) ;
gameSettings - > setTileset ( tilesetFiles [ listBoxTileset . getSelectedItemIndex ( ) ] ) ;
gameSettings - > setTech ( techTreeFiles [ listBoxTechTree . getSelectedItemIndex ( ) ] ) ;
gameSettings - > setDefaultUnits ( true ) ;
gameSettings - > setDefaultResources ( true ) ;
gameSettings - > setDefaultVictoryConditions ( true ) ;
2010-03-27 08:09:34 +01:00
gameSettings - > setFogOfWar ( listBoxFogOfWar . getSelectedItemIndex ( ) = = 0 ) ;
2010-05-31 12:20:18 +02:00
gameSettings - > setEnableObserverModeAtEndGame ( listBoxEnableObserverMode . getSelectedItemIndex ( ) = = 0 ) ;
2010-03-13 23:00:01 +01:00
for ( int i = 0 ; i < mapInfo . players ; + + i )
{
ControlType ct = static_cast < ControlType > ( listBoxControls [ i ] . getSelectedItemIndex ( ) ) ;
if ( ct ! = ctClosed )
{
if ( ct = = ctHuman )
{
gameSettings - > setThisFactionIndex ( factionCount ) ;
}
gameSettings - > setFactionControl ( factionCount , ct ) ;
gameSettings - > setTeam ( factionCount , listBoxTeams [ i ] . getSelectedItemIndex ( ) ) ;
gameSettings - > setStartLocationIndex ( factionCount , i ) ;
gameSettings - > setFactionTypeName ( factionCount , factionFiles [ listBoxFactions [ i ] . getSelectedItemIndex ( ) ] ) ;
2010-04-23 01:18:04 +02:00
if ( listBoxControls [ i ] . getSelectedItemIndex ( ) = = ctNetwork )
{
ConnectionSlot * connectionSlot = serverInterface - > getSlot ( i ) ;
if ( ( connectionSlot ! = NULL ) & & ( connectionSlot - > isConnected ( ) ) )
{
gameSettings - > setNetworkPlayerName ( factionCount , connectionSlot - > getName ( ) ) ;
}
else
{
gameSettings - > setNetworkPlayerName ( factionCount , " ??? " ) ;
}
}
else if ( listBoxControls [ i ] . getSelectedItemIndex ( ) = = ctHuman )
{
gameSettings - > setNetworkPlayerName ( factionCount , Config : : getInstance ( ) . getString ( " NetPlayerName " , Socket : : getHostName ( ) . c_str ( ) ) ) ;
}
else
{
gameSettings - > setNetworkPlayerName ( factionCount , " " ) ;
}
2010-03-13 23:00:01 +01:00
factionCount + + ;
}
}
gameSettings - > setFactionCount ( factionCount ) ;
2010-06-03 03:10:40 +02:00
gameSettings - > setEnableServerControlledAI ( listBoxEnableServerControlledAI . getSelectedItemIndex ( ) = = 0 ) ;
2010-03-13 23:00:01 +01:00
2010-03-20 00:26:00 +01:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] gameSettings->getTileset() = [%s]\n",__FILE__,__FUNCTION__,gameSettings->getTileset().c_str());
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] gameSettings->getTech() = [%s]\n",__FILE__,__FUNCTION__,gameSettings->getTech().c_str());
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] gameSettings->getMap() = [%s]\n",__FILE__,__FUNCTION__,gameSettings->getMap().c_str());
2010-03-13 23:00:01 +01:00
2010-04-11 03:25:06 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-03-13 23:00:01 +01:00
}
2010-05-31 21:57:10 +02:00
void MenuStateCustomGame : : saveGameSettingsToFile ( std : : string fileName ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
if ( getGameReadWritePath ( ) ! = " " ) {
fileName = getGameReadWritePath ( ) + fileName ;
}
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] fileName = [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , fileName . c_str ( ) ) ;
GameSettings gameSettings ;
loadGameSettings ( & gameSettings ) ;
std : : ofstream saveGameFile ;
saveGameFile . open ( fileName . c_str ( ) , ios_base : : out | ios_base : : trunc ) ;
//int factionCount= 0;
//ServerInterface* serverInterface= NetworkManager::getInstance().getServerInterface();
saveGameFile < < " Description= " < < gameSettings . getDescription ( ) < < std : : endl ;
saveGameFile < < " Map= " < < gameSettings . getMap ( ) < < std : : endl ;
saveGameFile < < " Tileset= " < < gameSettings . getTileset ( ) < < std : : endl ;
saveGameFile < < " TechTree= " < < gameSettings . getTech ( ) < < std : : endl ;
saveGameFile < < " DefaultUnits= " < < gameSettings . getDefaultUnits ( ) < < std : : endl ;
saveGameFile < < " DefaultResources= " < < gameSettings . getDefaultResources ( ) < < std : : endl ;
saveGameFile < < " DefaultVictoryConditions= " < < gameSettings . getDefaultVictoryConditions ( ) < < std : : endl ;
saveGameFile < < " FogOfWar= " < < gameSettings . getFogOfWar ( ) < < std : : endl ;
saveGameFile < < " EnableObserverModeAtEndGame= " < < gameSettings . getEnableObserverModeAtEndGame ( ) < < std : : endl ;
2010-06-03 03:10:40 +02:00
saveGameFile < < " EnableServerControlledAI= " < < gameSettings . getEnableServerControlledAI ( ) < < std : : endl ;
2010-05-31 21:57:10 +02:00
saveGameFile < < " FactionThisFactionIndex= " < < gameSettings . getThisFactionIndex ( ) < < std : : endl ;
saveGameFile < < " FactionCount= " < < gameSettings . getFactionCount ( ) < < std : : endl ;
for ( int i = 0 ; i < gameSettings . getFactionCount ( ) ; + + i ) {
saveGameFile < < " FactionControlForIndex " < < i < < " = " < < gameSettings . getFactionControl ( i ) < < std : : endl ;
saveGameFile < < " FactionTeamForIndex " < < i < < " = " < < gameSettings . getTeam ( i ) < < std : : endl ;
saveGameFile < < " FactionStartLocationForIndex " < < i < < " = " < < gameSettings . getStartLocationIndex ( i ) < < std : : endl ;
saveGameFile < < " FactionTypeNameForIndex " < < i < < " = " < < gameSettings . getFactionTypeName ( i ) < < std : : endl ;
saveGameFile < < " FactionPlayerNameForIndex " < < i < < " = " < < gameSettings . getNetworkPlayerName ( i ) < < std : : endl ;
}
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
}
GameSettings MenuStateCustomGame : : loadGameSettingsFromFile ( std : : string fileName ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
GameSettings gameSettings ;
if ( getGameReadWritePath ( ) ! = " " ) {
fileName = getGameReadWritePath ( ) + fileName ;
}
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] fileName = [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , fileName . c_str ( ) ) ;
if ( fileExists ( fileName ) = = false ) {
return gameSettings ;
}
try {
Properties properties ;
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] fileName = [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , fileName . c_str ( ) ) ;
properties . load ( fileName ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] fileName = [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , fileName . c_str ( ) ) ;
gameSettings . setDescription ( properties . getString ( " Description " ) ) ;
gameSettings . setMap ( properties . getString ( " Map " ) ) ;
gameSettings . setTileset ( properties . getString ( " Tileset " ) ) ;
gameSettings . setTech ( properties . getString ( " TechTree " ) ) ;
gameSettings . setDefaultUnits ( properties . getBool ( " DefaultUnits " ) ) ;
gameSettings . setDefaultResources ( properties . getBool ( " DefaultResources " ) ) ;
gameSettings . setDefaultVictoryConditions ( properties . getBool ( " DefaultVictoryConditions " ) ) ;
gameSettings . setFogOfWar ( properties . getBool ( " FogOfWar " ) ) ;
gameSettings . setEnableObserverModeAtEndGame ( properties . getBool ( " EnableObserverModeAtEndGame " ) ) ;
2010-06-03 03:10:40 +02:00
gameSettings . setEnableServerControlledAI ( properties . getBool ( " EnableServerControlledAI " , " false " ) ) ;
2010-05-31 21:57:10 +02:00
gameSettings . setThisFactionIndex ( properties . getInt ( " FactionThisFactionIndex " ) ) ;
gameSettings . setFactionCount ( properties . getInt ( " FactionCount " ) ) ;
for ( int i = 0 ; i < gameSettings . getFactionCount ( ) ; + + i ) {
gameSettings . setFactionControl ( i , ( ControlType ) properties . getInt ( string ( " FactionControlForIndex " ) + intToStr ( i ) ) ) ;
gameSettings . setTeam ( i , properties . getInt ( string ( " FactionTeamForIndex " ) + intToStr ( i ) ) ) ;
gameSettings . setStartLocationIndex ( i , properties . getInt ( string ( " FactionStartLocationForIndex " ) + intToStr ( i ) ) ) ;
gameSettings . setFactionTypeName ( i , properties . getString ( string ( " FactionTypeNameForIndex " ) + intToStr ( i ) ) ) ;
gameSettings . setNetworkPlayerName ( i , properties . getString ( string ( " FactionPlayerNameForIndex " ) + intToStr ( i ) ) ) ;
}
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
string mapFile = gameSettings . getMap ( ) ;
mapFile = formatString ( mapFile ) ;
listBoxMap . setSelectedItem ( mapFile ) ;
2010-06-01 01:39:07 +02:00
loadMapInfo ( Map : : getMapPath ( mapFiles [ listBoxMap . getSelectedItemIndex ( ) ] ) , & mapInfo ) ;
labelMapInfo . setText ( mapInfo . desc ) ;
2010-05-31 21:57:10 +02:00
string tilesetFile = gameSettings . getTileset ( ) ;
tilesetFile = formatString ( tilesetFile ) ;
listBoxTileset . setSelectedItem ( tilesetFile ) ;
string techtreeFile = gameSettings . getTech ( ) ;
techtreeFile = formatString ( techtreeFile ) ;
listBoxTechTree . setSelectedItem ( techtreeFile ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
//gameSettings->setDefaultUnits(true);
//gameSettings->setDefaultResources(true);
//gameSettings->setDefaultVictoryConditions(true);
2010-06-01 01:39:07 +02:00
Lang & lang = Lang : : getInstance ( ) ;
2010-05-31 21:57:10 +02:00
listBoxFogOfWar . setSelectedItem ( gameSettings . getFogOfWar ( ) = = true ? lang . get ( " Yes " ) : lang . get ( " No " ) ) ;
listBoxEnableObserverMode . setSelectedItem ( gameSettings . getEnableObserverModeAtEndGame ( ) = = true ? lang . get ( " Yes " ) : lang . get ( " No " ) ) ;
2010-06-03 03:10:40 +02:00
listBoxEnableServerControlledAI . setSelectedItem ( gameSettings . getEnableServerControlledAI ( ) = = true ? lang . get ( " Yes " ) : lang . get ( " No " ) ) ;
2010-05-31 21:57:10 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
reloadFactions ( ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s] Line: %d] gameSettings.getFactionCount() = %d \n " , __FILE__ , __FUNCTION__ , __LINE__ , gameSettings . getFactionCount ( ) ) ;
for ( int i = 0 ; i < gameSettings . getFactionCount ( ) ; + + i ) {
listBoxControls [ i ] . setSelectedItemIndex ( gameSettings . getFactionControl ( i ) ) ;
listBoxTeams [ i ] . setSelectedItemIndex ( gameSettings . getTeam ( i ) ) ;
string factionName = gameSettings . getFactionTypeName ( i ) ;
factionName = formatString ( factionName ) ;
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] factionName = [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , factionName . c_str ( ) ) ;
listBoxFactions [ i ] . setSelectedItem ( factionName ) ;
}
2010-06-01 01:39:07 +02:00
updateControlers ( ) ;
updateNetworkSlots ( ) ;
needToRepublishToMasterserver = true ;
if ( hasNetworkGameSettings ( ) = = true )
{
needToSetChangedGameSettings = true ;
lastSetChangedGameSettings = time ( NULL ) ;
}
2010-05-31 21:57:10 +02:00
//!!!
}
catch ( const exception & ex ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] ERROR = [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , ex . what ( ) ) ;
showMessageBox ( ex . what ( ) , " Error " , false ) ;
gameSettings = GameSettings ( ) ;
}
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s] Line: %d \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
}
2010-03-13 23:00:01 +01:00
// ============ PRIVATE ===========================
bool MenuStateCustomGame : : hasNetworkGameSettings ( )
{
bool hasNetworkSlot = false ;
2010-04-11 03:25:06 +02:00
try {
for ( int i = 0 ; i < mapInfo . players ; + + i )
2010-03-13 23:00:01 +01:00
{
2010-04-11 03:25:06 +02:00
ControlType ct = static_cast < ControlType > ( listBoxControls [ i ] . getSelectedItemIndex ( ) ) ;
if ( ct ! = ctClosed )
2010-03-13 23:00:01 +01:00
{
2010-04-11 03:25:06 +02:00
if ( ct = = ctNetwork )
{
hasNetworkSlot = true ;
break ;
}
2010-03-13 23:00:01 +01:00
}
}
}
2010-04-11 03:25:06 +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 ) ;
}
2010-03-13 23:00:01 +01:00
return hasNetworkSlot ;
}
void MenuStateCustomGame : : loadMapInfo ( string file , MapInfo * mapInfo ) {
struct MapFileHeader {
int32 version ;
int32 maxPlayers ;
int32 width ;
int32 height ;
int32 altFactor ;
int32 waterLevel ;
int8 title [ 128 ] ;
} ;
Lang & lang = Lang : : getInstance ( ) ;
try {
FILE * f = fopen ( file . c_str ( ) , " rb " ) ;
if ( f = = NULL )
throw runtime_error ( " Can't open file " ) ;
MapFileHeader header ;
size_t readBytes = fread ( & header , sizeof ( MapFileHeader ) , 1 , f ) ;
mapInfo - > size . x = header . width ;
mapInfo - > size . y = header . height ;
mapInfo - > players = header . maxPlayers ;
mapInfo - > desc = lang . get ( " MaxPlayers " ) + " : " + intToStr ( mapInfo - > players ) + " \n " ;
mapInfo - > desc + = lang . get ( " Size " ) + " : " + intToStr ( mapInfo - > size . x ) + " x " + intToStr ( mapInfo - > size . y ) ;
fclose ( f ) ;
}
catch ( exception e ) {
throw runtime_error ( " Error loading map file: " + file + ' \n ' + e . what ( ) ) ;
}
}
void MenuStateCustomGame : : reloadFactions ( ) {
vector < string > results ;
2010-03-18 22:26:40 +01:00
Config & config = Config : : getInstance ( ) ;
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 ;
}
}
if ( results . size ( ) = = 0 ) {
2010-03-20 11:52:55 +01:00
throw runtime_error ( " (2) There are no factions for the tech tree [ " + techTreeFiles[listBoxTechTree.getSelectedItemIndex()] + " ] " ) ;
2010-03-13 23:00:01 +01:00
}
2010-03-18 22:26:40 +01:00
factionFiles = results ;
for ( int i = 0 ; i < results . size ( ) ; + + i ) {
results [ i ] = formatString ( results [ i ] ) ;
2010-03-20 00:26:00 +01:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " Tech [%s] has faction [%s] \n " , techTreeFiles [ listBoxTechTree . getSelectedItemIndex ( ) ] . c_str ( ) , results [ i ] . c_str ( ) ) ;
2010-03-18 22:26:40 +01:00
}
for ( int i = 0 ; i < GameConstants : : maxPlayers ; + + i ) {
listBoxFactions [ i ] . setItems ( results ) ;
listBoxFactions [ i ] . setSelectedItemIndex ( i % results . size ( ) ) ;
}
2010-03-13 23:00:01 +01:00
}
void MenuStateCustomGame : : updateControlers ( ) {
2010-04-11 03:25:06 +02:00
try {
bool humanPlayer = false ;
2010-03-13 23:00:01 +01:00
2010-04-11 03:25:06 +02:00
for ( int i = 0 ; i < mapInfo . players ; + + i ) {
if ( listBoxControls [ i ] . getSelectedItemIndex ( ) = = ctHuman ) {
humanPlayer = true ;
}
2010-03-13 23:00:01 +01:00
}
2010-04-11 03:25:06 +02:00
if ( ! humanPlayer ) {
listBoxControls [ 0 ] . setSelectedItemIndex ( ctHuman ) ;
}
2010-03-13 23:00:01 +01:00
2010-04-11 03:25:06 +02:00
for ( int i = mapInfo . players ; i < GameConstants : : maxPlayers ; + + i ) {
listBoxControls [ i ] . setSelectedItemIndex ( ctClosed ) ;
}
}
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 ) ;
2010-03-13 23:00:01 +01:00
}
}
void MenuStateCustomGame : : closeUnusedSlots ( ) {
2010-04-11 03:25:06 +02:00
try {
ServerInterface * serverInterface = NetworkManager : : getInstance ( ) . getServerInterface ( ) ;
for ( int i = 0 ; i < mapInfo . players ; + + i ) {
if ( listBoxControls [ i ] . getSelectedItemIndex ( ) = = ctNetwork ) {
if ( ! serverInterface - > getSlot ( i ) - > isConnected ( ) ) {
listBoxControls [ i ] . setSelectedItemIndex ( ctClosed ) ;
}
2010-03-13 23:00:01 +01:00
}
}
2010-04-11 03:25:06 +02:00
updateNetworkSlots ( ) ;
}
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 ) ;
2010-03-13 23:00:01 +01:00
}
}
void MenuStateCustomGame : : updateNetworkSlots ( )
{
2010-04-11 03:25:06 +02:00
try {
ServerInterface * serverInterface = NetworkManager : : getInstance ( ) . getServerInterface ( ) ;
2010-03-13 23:00:01 +01:00
2010-04-11 03:25:06 +02:00
for ( int i = 0 ; i < GameConstants : : maxPlayers ; + + i )
2010-03-13 23:00:01 +01:00
{
2010-04-11 03:25:06 +02:00
if ( serverInterface - > getSlot ( i ) = = NULL & & listBoxControls [ i ] . getSelectedItemIndex ( ) = = ctNetwork )
{
serverInterface - > addSlot ( i ) ;
}
if ( serverInterface - > getSlot ( i ) ! = NULL & & listBoxControls [ i ] . getSelectedItemIndex ( ) ! = ctNetwork )
{
serverInterface - > removeSlot ( i ) ;
}
2010-03-13 23:00:01 +01:00
}
}
2010-04-11 03:25:06 +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 ) ;
}
2010-03-13 23:00:01 +01:00
}
void MenuStateCustomGame : : keyDown ( char key )
{
//send key to the chat manager
chatManager . keyDown ( key ) ;
}
void MenuStateCustomGame : : keyPress ( char c )
{
chatManager . keyPress ( c ) ;
}
2010-05-17 01:03:22 +02:00
void MenuStateCustomGame : : showMessageBox ( const string & text , const string & header , bool toggle ) {
if ( ! toggle ) {
mainMessageBox . setEnabled ( false ) ;
}
if ( ! mainMessageBox . getEnabled ( ) ) {
mainMessageBox . setText ( text ) ;
mainMessageBox . setHeader ( header ) ;
mainMessageBox . setEnabled ( true ) ;
}
else {
mainMessageBox . setEnabled ( false ) ;
}
}
2010-03-13 23:00:01 +01:00
} } //end namespace