Team Fortress 2 Source Code as on 22/4/2020
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.

313 lines
9.2 KiB

  1. //========= Copyright 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. void CCreateMultiplayerGameServerPage::OnKeyCodePressed( vgui::KeyCode code )
  54. {
  55. if ( code == KEY_XBUTTON_LEFT || code == KEY_XSTICK1_LEFT || code == KEY_XSTICK2_LEFT )
  56. {
  57. int nItem = m_pMapList->GetActiveItem();
  58. nItem -= 1;
  59. if ( nItem < 0 )
  60. {
  61. nItem = m_pMapList->GetItemCount() - 1;
  62. }
  63. m_pMapList->SilentActivateItem( nItem );
  64. }
  65. else if ( code == KEY_XBUTTON_RIGHT || code == KEY_XSTICK1_RIGHT || code == KEY_XSTICK2_RIGHT )
  66. {
  67. int nItem = m_pMapList->GetActiveItem();
  68. nItem += 1;
  69. if ( nItem >= m_pMapList->GetItemCount() )
  70. {
  71. nItem = 0;
  72. }
  73. m_pMapList->SilentActivateItem( nItem );
  74. }
  75. else
  76. {
  77. BaseClass::OnKeyCodePressed(code);
  78. }
  79. }
  80. //-----------------------------------------------------------------------------
  81. // Purpose:
  82. //-----------------------------------------------------------------------------
  83. void CCreateMultiplayerGameServerPage::EnableBots( KeyValues *data )
  84. {
  85. m_pSavedData = data;
  86. int quota = data->GetInt( "bot_quota", 0 );
  87. SetControlInt( "BotQuotaCombo", quota );
  88. m_pEnableBotsCheck->SetSelected( (quota > 0) );
  89. int difficulty = data->GetInt( "bot_difficulty", 0 );
  90. difficulty = max( difficulty, 0 );
  91. difficulty = min( 3, difficulty );
  92. char buttonName[64];
  93. Q_snprintf( buttonName, sizeof( buttonName ), "SkillLevel%d", difficulty );
  94. vgui::RadioButton *button = dynamic_cast< vgui::RadioButton * >(FindChildByName( buttonName ));
  95. if ( button )
  96. {
  97. button->SetSelected( true );
  98. }
  99. }
  100. //-----------------------------------------------------------------------------
  101. // Purpose: called to get the info from the dialog
  102. //-----------------------------------------------------------------------------
  103. void CCreateMultiplayerGameServerPage::OnApplyChanges()
  104. {
  105. KeyValues *kv = m_pMapList->GetActiveItemUserData();
  106. strncpy(m_szMapName, kv->GetString("mapname", ""), DATA_STR_LENGTH);
  107. if ( m_pSavedData )
  108. {
  109. int quota = GetControlInt( "BotQuotaCombo", 0 );
  110. if ( !m_pEnableBotsCheck->IsSelected() )
  111. {
  112. quota = 0;
  113. }
  114. m_pSavedData->SetInt( "bot_quota", quota );
  115. ConVarRef bot_quota( "bot_quota" );
  116. bot_quota.SetValue( quota );
  117. int difficulty = 0;
  118. for ( int i=0; i<4; ++i )
  119. {
  120. char buttonName[64];
  121. Q_snprintf( buttonName, sizeof( buttonName ), "SkillLevel%d", i );
  122. vgui::RadioButton *button = dynamic_cast< vgui::RadioButton * >(FindChildByName( buttonName ));
  123. if ( button )
  124. {
  125. if ( button->IsSelected() )
  126. {
  127. difficulty = i;
  128. break;
  129. }
  130. }
  131. }
  132. m_pSavedData->SetInt( "bot_difficulty", difficulty );
  133. ConVarRef bot_difficulty( "bot_difficulty" );
  134. bot_difficulty.SetValue( difficulty );
  135. }
  136. }
  137. //-----------------------------------------------------------------------------
  138. // Purpose: loads the list of available maps into the map list
  139. //-----------------------------------------------------------------------------
  140. void CCreateMultiplayerGameServerPage::LoadMaps( const char *pszPathID )
  141. {
  142. FileFindHandle_t findHandle = NULL;
  143. KeyValues *hiddenMaps = ModInfo().GetHiddenMaps();
  144. const char *pszFilename = g_pFullFileSystem->FindFirstEx( "maps/*.bsp", pszPathID, &findHandle );
  145. while ( pszFilename )
  146. {
  147. char mapname[256];
  148. char *ext, *str;
  149. // FindFirst ignores the pszPathID, so check it here
  150. // TODO: this doesn't find maps in fallback dirs
  151. _snprintf( mapname, sizeof(mapname), "maps/%s", pszFilename );
  152. if ( !g_pFullFileSystem->FileExists( mapname, pszPathID ) )
  153. {
  154. goto nextFile;
  155. }
  156. // remove the text 'maps/' and '.bsp' from the file name to get the map name
  157. str = Q_strstr( pszFilename, "maps" );
  158. if ( str )
  159. {
  160. strncpy( mapname, str + 5, sizeof(mapname) - 1 ); // maps + \\ = 5
  161. }
  162. else
  163. {
  164. strncpy( mapname, pszFilename, sizeof(mapname) - 1 );
  165. }
  166. ext = Q_strstr( mapname, ".bsp" );
  167. if ( ext )
  168. {
  169. *ext = 0;
  170. }
  171. //!! hack: strip out single player HL maps
  172. // this needs to be specified in a seperate file
  173. if ( !stricmp( ModInfo().GetGameName(), "Half-Life" ) && ( mapname[0] == 'c' || mapname[0] == 't') && mapname[2] == 'a' && mapname[1] >= '0' && mapname[1] <= '5' )
  174. {
  175. goto nextFile;
  176. }
  177. // strip out maps that shouldn't be displayed
  178. if ( hiddenMaps )
  179. {
  180. if ( hiddenMaps->GetInt( mapname, 0 ) )
  181. {
  182. goto nextFile;
  183. }
  184. }
  185. // add to the map list
  186. m_pMapList->AddItem( mapname, new KeyValues( "data", "mapname", mapname ) );
  187. // get the next file
  188. nextFile:
  189. pszFilename = g_pFullFileSystem->FindNext( findHandle );
  190. }
  191. g_pFullFileSystem->FindClose( findHandle );
  192. }
  193. //-----------------------------------------------------------------------------
  194. // Purpose: loads the list of available maps into the map list
  195. //-----------------------------------------------------------------------------
  196. void CCreateMultiplayerGameServerPage::LoadMapList()
  197. {
  198. // clear the current list (if any)
  199. m_pMapList->DeleteAllItems();
  200. // add special "name" to represent loading a randomly selected map
  201. m_pMapList->AddItem( RANDOM_MAP, new KeyValues( "data", "mapname", RANDOM_MAP ) );
  202. // Load the GameDir maps
  203. LoadMaps( "GAME" );
  204. // set the first item to be selected
  205. m_pMapList->ActivateItem( 0 );
  206. }
  207. //-----------------------------------------------------------------------------
  208. // Purpose:
  209. //-----------------------------------------------------------------------------
  210. bool CCreateMultiplayerGameServerPage::IsRandomMapSelected()
  211. {
  212. const char *mapname = m_pMapList->GetActiveItemUserData()->GetString("mapname");
  213. if (!stricmp( mapname, RANDOM_MAP ))
  214. {
  215. return true;
  216. }
  217. return false;
  218. }
  219. //-----------------------------------------------------------------------------
  220. // Purpose:
  221. //-----------------------------------------------------------------------------
  222. const char *CCreateMultiplayerGameServerPage::GetMapName()
  223. {
  224. int count = m_pMapList->GetItemCount();
  225. // if there is only one entry it's the special "select random map" entry
  226. if( count <= 1 )
  227. return NULL;
  228. const char *mapname = m_pMapList->GetActiveItemUserData()->GetString("mapname");
  229. if (!strcmp( mapname, RANDOM_MAP ))
  230. {
  231. int which = RandomInt( 1, count - 1 );
  232. mapname = m_pMapList->GetItemUserData( which )->GetString("mapname");
  233. }
  234. return mapname;
  235. }
  236. //-----------------------------------------------------------------------------
  237. // Purpose: Sets currently selected map in the map combobox
  238. //-----------------------------------------------------------------------------
  239. void CCreateMultiplayerGameServerPage::SetMap(const char *mapName)
  240. {
  241. for (int i = 0; i < m_pMapList->GetItemCount(); i++)
  242. {
  243. if (!m_pMapList->IsItemIDValid(i))
  244. continue;
  245. if (!stricmp(m_pMapList->GetItemUserData(i)->GetString("mapname"), mapName))
  246. {
  247. m_pMapList->ActivateItem(i);
  248. break;
  249. }
  250. }
  251. }
  252. //-----------------------------------------------------------------------------
  253. // Purpose:
  254. //-----------------------------------------------------------------------------
  255. void CCreateMultiplayerGameServerPage::OnCheckButtonChecked()
  256. {
  257. SetControlEnabled("SkillLevel0", m_pEnableBotsCheck->IsSelected());
  258. SetControlEnabled("SkillLevel1", m_pEnableBotsCheck->IsSelected());
  259. SetControlEnabled("SkillLevel2", m_pEnableBotsCheck->IsSelected());
  260. SetControlEnabled("SkillLevel3", m_pEnableBotsCheck->IsSelected());
  261. SetControlEnabled("BotQuotaCombo", m_pEnableBotsCheck->IsSelected());
  262. SetControlEnabled("BotQuotaLabel", m_pEnableBotsCheck->IsSelected());
  263. SetControlEnabled("BotDifficultyLabel", m_pEnableBotsCheck->IsSelected());
  264. }