Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

300 lines
9.4 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //===========================================================================//
  7. #include "CreateMultiplayerGameServerPage.h"
  8. using namespace vgui;
  9. #include <KeyValues.h>
  10. #include <vgui_controls/ComboBox.h>
  11. #include <vgui_controls/RadioButton.h>
  12. #include <vgui_controls/CheckButton.h>
  13. #include "FileSystem.h"
  14. #include "tier1/convar.h"
  15. #include "EngineInterface.h"
  16. #include "CvarToggleCheckButton.h"
  17. #include "ModInfo.h"
  18. // for SRC
  19. #include <vstdlib/random.h>
  20. // memdbgon must be the last include file in a .cpp file!!!
  21. #include <tier0/memdbgon.h>
  22. #define RANDOM_MAP "#GameUI_RandomMap"
  23. //-----------------------------------------------------------------------------
  24. // Purpose: Constructor
  25. //-----------------------------------------------------------------------------
  26. CCreateMultiplayerGameServerPage::CCreateMultiplayerGameServerPage(vgui::Panel *parent, const char *name) : PropertyPage(parent, name)
  27. {
  28. m_pSavedData = NULL;
  29. // we can use this if we decide we want to put "listen server" at the end of the game name
  30. m_pMapList = new ComboBox(this, "MapList", 12, false);
  31. m_pEnableBotsCheck = new CheckButton( this, "EnableBotsCheck", "" );
  32. m_pEnableBotsCheck->SetVisible( false );
  33. m_pEnableBotsCheck->SetEnabled( false );
  34. LoadControlSettings("Resource/CreateMultiplayerGameServerPage.res");
  35. LoadMapList();
  36. m_szMapName[0] = 0;
  37. // initialize hostname
  38. SetControlString("ServerNameEdit", ModInfo().GetGameName());//szHostName);
  39. // initialize password
  40. // SetControlString("PasswordEdit", engine->pfnGetCvarString("sv_password"));
  41. ConVarRef var( "sv_password" );
  42. if ( var.IsValid() )
  43. {
  44. SetControlString("PasswordEdit", var.GetString() );
  45. }
  46. }
  47. //-----------------------------------------------------------------------------
  48. // Purpose: Destructor
  49. //-----------------------------------------------------------------------------
  50. CCreateMultiplayerGameServerPage::~CCreateMultiplayerGameServerPage()
  51. {
  52. }
  53. //-----------------------------------------------------------------------------
  54. // Purpose:
  55. //-----------------------------------------------------------------------------
  56. void CCreateMultiplayerGameServerPage::EnableBots( KeyValues *data )
  57. {
  58. m_pSavedData = data;
  59. int quota = data->GetInt( "bot_quota", 0 );
  60. SetControlInt( "BotQuotaCombo", quota );
  61. m_pEnableBotsCheck->SetSelected( (quota > 0) );
  62. int difficulty = data->GetInt( "bot_difficulty", 0 );
  63. difficulty = MAX( difficulty, 0 );
  64. difficulty = MIN( 3, difficulty );
  65. char buttonName[64];
  66. Q_snprintf( buttonName, sizeof( buttonName ), "SkillLevel%d", difficulty );
  67. vgui::RadioButton *button = dynamic_cast< vgui::RadioButton * >(FindChildByName( buttonName ));
  68. if ( button )
  69. {
  70. button->SetSelected( true );
  71. }
  72. }
  73. //-----------------------------------------------------------------------------
  74. // Purpose: called to get the info from the dialog
  75. //-----------------------------------------------------------------------------
  76. void CCreateMultiplayerGameServerPage::OnApplyChanges()
  77. {
  78. KeyValues *kv = m_pMapList->GetActiveItemUserData();
  79. Q_strncpy(m_szMapName, kv->GetString("mapname", ""), DATA_STR_LENGTH);
  80. if ( m_pSavedData )
  81. {
  82. int quota = GetControlInt( "BotQuotaCombo", 0 );
  83. if ( !m_pEnableBotsCheck->IsSelected() )
  84. {
  85. quota = 0;
  86. }
  87. m_pSavedData->SetInt( "bot_quota", quota );
  88. ConVarRef bot_quota( "bot_quota" );
  89. bot_quota.SetValue( quota );
  90. int difficulty = 0;
  91. for ( int i=0; i<4; ++i )
  92. {
  93. char buttonName[64];
  94. Q_snprintf( buttonName, sizeof( buttonName ), "SkillLevel%d", i );
  95. vgui::RadioButton *button = dynamic_cast< vgui::RadioButton * >(FindChildByName( buttonName ));
  96. if ( button )
  97. {
  98. if ( button->IsSelected() )
  99. {
  100. difficulty = i;
  101. break;
  102. }
  103. }
  104. }
  105. m_pSavedData->SetInt( "bot_difficulty", difficulty );
  106. ConVarRef bot_difficulty( "bot_difficulty" );
  107. bot_difficulty.SetValue( difficulty );
  108. }
  109. }
  110. //-----------------------------------------------------------------------------
  111. // Purpose: loads the list of available maps into the map list
  112. //-----------------------------------------------------------------------------
  113. void CCreateMultiplayerGameServerPage::LoadMaps( const char *pszPathID )
  114. {
  115. FileFindHandle_t findHandle = NULL;
  116. KeyValues *hiddenMaps = ModInfo().GetHiddenMaps();
  117. const char *pszFilename = g_pFullFileSystem->FindFirst( "maps/*.bsp", &findHandle );
  118. while ( pszFilename )
  119. {
  120. const char *str = NULL;
  121. char *ext = NULL;
  122. char mapname[256];
  123. // FindFirst ignores the pszPathID, so check it here
  124. // TODO: this doesn't find maps in fallback dirs
  125. Q_snprintf( mapname, sizeof(mapname), "maps/%s", pszFilename );
  126. if ( !g_pFullFileSystem->FileExists( mapname, pszPathID ) )
  127. {
  128. goto nextFile;
  129. }
  130. // remove the text 'maps/' and '.bsp' from the file name to get the map name
  131. str = Q_strstr( pszFilename, "maps" );
  132. if ( str )
  133. {
  134. Q_strncpy( mapname, str + 5, sizeof(mapname) - 1 ); // maps + \\ = 5
  135. }
  136. else
  137. {
  138. Q_strncpy( mapname, pszFilename, sizeof(mapname) - 1 );
  139. }
  140. ext = Q_strstr( mapname, ".bsp" );
  141. if ( ext )
  142. {
  143. *ext = 0;
  144. }
  145. //!! hack: strip out single player HL maps
  146. // this needs to be specified in a seperate file
  147. if ( !stricmp( ModInfo().GetGameName(), "Half-Life" ) && ( mapname[0] == 'c' || mapname[0] == 't') && mapname[2] == 'a' && mapname[1] >= '0' && mapname[1] <= '5' )
  148. {
  149. goto nextFile;
  150. }
  151. // strip out maps that shouldn't be displayed
  152. if ( hiddenMaps )
  153. {
  154. if ( hiddenMaps->GetInt( mapname, 0 ) )
  155. {
  156. goto nextFile;
  157. }
  158. }
  159. // add to the map list
  160. m_pMapList->AddItem( mapname, new KeyValues( "data", "mapname", mapname ) );
  161. // get the next file
  162. nextFile:
  163. pszFilename = g_pFullFileSystem->FindNext( findHandle );
  164. }
  165. g_pFullFileSystem->FindClose( findHandle );
  166. }
  167. //-----------------------------------------------------------------------------
  168. // Purpose: loads the list of available maps into the map list
  169. //-----------------------------------------------------------------------------
  170. void CCreateMultiplayerGameServerPage::LoadMapList()
  171. {
  172. // clear the current list (if any)
  173. m_pMapList->DeleteAllItems();
  174. // add special "name" to represent loading a randomly selected map
  175. m_pMapList->AddItem( RANDOM_MAP, new KeyValues( "data", "mapname", RANDOM_MAP ) );
  176. // iterate the filesystem getting the list of all the files
  177. // UNDONE: steam wants this done in a special way, need to support that
  178. const char *pathID = "MOD";
  179. if ( !stricmp(ModInfo().GetGameName(), "Half-Life" ) )
  180. {
  181. pathID = NULL; // hl is the base dir
  182. }
  183. // Load the GameDir maps
  184. LoadMaps( pathID );
  185. // If we're not the Valve directory and we're using a "fallback_dir" in gameinfo.txt then include those maps...
  186. // (pathID is NULL if we're "Half-Life")
  187. const char *pszFallback = ModInfo().GetFallbackDir();
  188. if ( pathID && pszFallback[0] )
  189. {
  190. LoadMaps( "GAME_FALLBACK" );
  191. }
  192. // set the first item to be selected
  193. m_pMapList->ActivateItem( 0 );
  194. }
  195. //-----------------------------------------------------------------------------
  196. // Purpose:
  197. //-----------------------------------------------------------------------------
  198. bool CCreateMultiplayerGameServerPage::IsRandomMapSelected()
  199. {
  200. const char *mapname = m_pMapList->GetActiveItemUserData()->GetString("mapname");
  201. if (!stricmp( mapname, RANDOM_MAP ))
  202. {
  203. return true;
  204. }
  205. return false;
  206. }
  207. //-----------------------------------------------------------------------------
  208. // Purpose:
  209. //-----------------------------------------------------------------------------
  210. const char *CCreateMultiplayerGameServerPage::GetMapName()
  211. {
  212. int count = m_pMapList->GetItemCount();
  213. // if there is only one entry it's the special "select random map" entry
  214. if( count <= 1 )
  215. return NULL;
  216. const char *mapname = m_pMapList->GetActiveItemUserData()->GetString("mapname");
  217. if (!strcmp( mapname, RANDOM_MAP ))
  218. {
  219. int which = RandomInt( 1, count - 1 );
  220. mapname = m_pMapList->GetItemUserData( which )->GetString("mapname");
  221. }
  222. return mapname;
  223. }
  224. //-----------------------------------------------------------------------------
  225. // Purpose: Sets currently selected map in the map combobox
  226. //-----------------------------------------------------------------------------
  227. void CCreateMultiplayerGameServerPage::SetMap(const char *mapName)
  228. {
  229. for (int i = 0; i < m_pMapList->GetItemCount(); i++)
  230. {
  231. if (!m_pMapList->IsItemIDValid(i))
  232. continue;
  233. if (!stricmp(m_pMapList->GetItemUserData(i)->GetString("mapname"), mapName))
  234. {
  235. m_pMapList->ActivateItem(i);
  236. break;
  237. }
  238. }
  239. }
  240. //-----------------------------------------------------------------------------
  241. // Purpose:
  242. //-----------------------------------------------------------------------------
  243. void CCreateMultiplayerGameServerPage::OnCheckButtonChecked()
  244. {
  245. SetControlEnabled("SkillLevel0", m_pEnableBotsCheck->IsSelected());
  246. SetControlEnabled("SkillLevel1", m_pEnableBotsCheck->IsSelected());
  247. SetControlEnabled("SkillLevel2", m_pEnableBotsCheck->IsSelected());
  248. SetControlEnabled("SkillLevel3", m_pEnableBotsCheck->IsSelected());
  249. SetControlEnabled("BotQuotaCombo", m_pEnableBotsCheck->IsSelected());
  250. SetControlEnabled("BotQuotaLabel", m_pEnableBotsCheck->IsSelected());
  251. SetControlEnabled("BotDifficultyLabel", m_pEnableBotsCheck->IsSelected());
  252. }