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.

189 lines
5.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "CreateMultiplayerGameDialog.h"
  8. #include "CreateMultiplayerGameServerPage.h"
  9. #include "CreateMultiplayerGameGameplayPage.h"
  10. #include "CreateMultiplayerGameBotPage.h"
  11. #include "EngineInterface.h"
  12. #include "ModInfo.h"
  13. #include "GameUI_Interface.h"
  14. #include <stdio.h>
  15. using namespace vgui;
  16. #include "vgui_controls/ComboBox.h"
  17. #include <vgui/ILocalize.h>
  18. #include "filesystem.h"
  19. #include <KeyValues.h>
  20. // memdbgon must be the last include file in a .cpp file!!!
  21. #include <tier0/memdbgon.h>
  22. //-----------------------------------------------------------------------------
  23. // Purpose: Constructor
  24. //-----------------------------------------------------------------------------
  25. CCreateMultiplayerGameDialog::CCreateMultiplayerGameDialog(vgui::Panel *parent) : PropertyDialog(parent, "CreateMultiplayerGameDialog")
  26. {
  27. m_bBotsEnabled = false;
  28. SetDeleteSelfOnClose(true);
  29. SetSize(348, 460);
  30. SetTitle("#GameUI_CreateServer", true);
  31. SetOKButtonText("#GameUI_Start");
  32. if ( ModInfo().UseBots() )
  33. {
  34. m_bBotsEnabled = true;
  35. }
  36. m_pServerPage = new CCreateMultiplayerGameServerPage(this, "ServerPage");
  37. m_pGameplayPage = new CCreateMultiplayerGameGameplayPage(this, "GameplayPage");
  38. m_pBotPage = NULL;
  39. AddPage(m_pServerPage, "#GameUI_Server");
  40. AddPage(m_pGameplayPage, "#GameUI_Game");
  41. // create KeyValues object to load/save config options
  42. m_pSavedData = new KeyValues( "ServerConfig" );
  43. // load the config data
  44. if (m_pSavedData)
  45. {
  46. m_pSavedData->LoadFromFile( g_pFullFileSystem, "ServerConfig.vdf", "GAME" ); // this is game-specific data, so it should live in GAME, not CONFIG
  47. const char *startMap = m_pSavedData->GetString("map", "");
  48. if (startMap[0])
  49. {
  50. m_pServerPage->SetMap(startMap);
  51. }
  52. }
  53. if ( m_bBotsEnabled )
  54. {
  55. // add a page of advanced bot controls
  56. // NOTE: These controls will use the bot keys to initialize their values
  57. m_pBotPage = new CCreateMultiplayerGameBotPage( this, "BotPage", m_pSavedData );
  58. AddPage( m_pBotPage, "#GameUI_CPUPlayerOptions" );
  59. m_pServerPage->EnableBots( m_pSavedData );
  60. }
  61. }
  62. //-----------------------------------------------------------------------------
  63. // Purpose: Destructor
  64. //-----------------------------------------------------------------------------
  65. CCreateMultiplayerGameDialog::~CCreateMultiplayerGameDialog()
  66. {
  67. if (m_pSavedData)
  68. {
  69. m_pSavedData->deleteThis();
  70. m_pSavedData = NULL;
  71. }
  72. }
  73. //-----------------------------------------------------------------------------
  74. // Purpose: runs the server when the OK button is pressed
  75. //-----------------------------------------------------------------------------
  76. bool CCreateMultiplayerGameDialog::OnOK(bool applyOnly)
  77. {
  78. // reset server enforced cvars
  79. g_pCVar->RevertFlaggedConVars( FCVAR_REPLICATED );
  80. // Cheats were disabled; revert all cheat cvars to their default values.
  81. // This must be done heading into multiplayer games because people can play
  82. // demos etc and set cheat cvars with sv_cheats 0.
  83. g_pCVar->RevertFlaggedConVars( FCVAR_CHEAT );
  84. DevMsg( "FCVAR_CHEAT cvars reverted to defaults.\n" );
  85. BaseClass::OnOK(applyOnly);
  86. // get these values from m_pServerPage and store them temporarily
  87. char szMapName[64], szHostName[64], szPassword[64];
  88. strncpy(szMapName, m_pServerPage->GetMapName(), sizeof( szMapName ));
  89. strncpy(szHostName, m_pGameplayPage->GetHostName(), sizeof( szHostName ));
  90. strncpy(szPassword, m_pGameplayPage->GetPassword(), sizeof( szPassword ));
  91. // save the config data
  92. if (m_pSavedData)
  93. {
  94. if (m_pServerPage->IsRandomMapSelected())
  95. {
  96. // it's set to random map, just save an
  97. m_pSavedData->SetString("map", "");
  98. }
  99. else
  100. {
  101. m_pSavedData->SetString("map", szMapName);
  102. }
  103. // save config to a file
  104. m_pSavedData->SaveToFile( g_pFullFileSystem, "ServerConfig.vdf", "GAME" );
  105. }
  106. char szMapCommand[1024];
  107. // create the command to execute
  108. Q_snprintf(szMapCommand, sizeof( szMapCommand ), "disconnect\nwait\nwait\nsv_lan 1\nsetmaster enable\nmaxplayers %i\nsv_password \"%s\"\nhostname \"%s\"\nprogress_enable\nmap %s\n",
  109. m_pGameplayPage->GetMaxPlayers(),
  110. szPassword,
  111. szHostName,
  112. szMapName
  113. );
  114. // exec
  115. engine->ClientCmd_Unrestricted(szMapCommand);
  116. return true;
  117. }
  118. void CCreateMultiplayerGameDialog::OnKeyCodePressed( vgui::KeyCode code )
  119. {
  120. // Handle close here, CBasePanel parent doesn't support "DialogClosing" command
  121. ButtonCode_t nButtonCode = GetBaseButtonCode( code );
  122. if ( nButtonCode == KEY_XBUTTON_B || nButtonCode == STEAMCONTROLLER_B )
  123. {
  124. OnCommand( "Close" );
  125. }
  126. else if ( nButtonCode == KEY_XBUTTON_A || nButtonCode == STEAMCONTROLLER_A )
  127. {
  128. OnOK( false );
  129. }
  130. else if ( nButtonCode == KEY_XBUTTON_UP ||
  131. nButtonCode == KEY_XSTICK1_UP ||
  132. nButtonCode == KEY_XSTICK2_UP ||
  133. nButtonCode == STEAMCONTROLLER_DPAD_UP ||
  134. nButtonCode == KEY_UP )
  135. {
  136. int nItem = m_pServerPage->GetMapList()->GetActiveItem() - 1;
  137. if ( nItem < 0 )
  138. {
  139. nItem = m_pServerPage->GetMapList()->GetItemCount() - 1;
  140. }
  141. m_pServerPage->GetMapList()->ActivateItem( nItem );
  142. }
  143. else if ( nButtonCode == KEY_XBUTTON_DOWN ||
  144. nButtonCode == KEY_XSTICK1_DOWN ||
  145. nButtonCode == KEY_XSTICK2_DOWN ||
  146. nButtonCode == STEAMCONTROLLER_DPAD_DOWN ||
  147. nButtonCode == KEY_DOWN )
  148. {
  149. int nItem = m_pServerPage->GetMapList()->GetActiveItem() + 1;
  150. if ( nItem >= m_pServerPage->GetMapList()->GetItemCount() )
  151. {
  152. nItem = 0;
  153. }
  154. m_pServerPage->GetMapList()->ActivateItem( nItem );
  155. }
  156. else
  157. {
  158. BaseClass::OnKeyCodePressed( code );
  159. }
  160. }