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.

228 lines
8.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "CreateMultiplayerGameBotPage.h"
  9. using namespace vgui;
  10. #include <KeyValues.h>
  11. #include <vgui_controls/ComboBox.h>
  12. #include <vgui_controls/CheckButton.h>
  13. #include <vgui_controls/Label.h>
  14. #include <vgui_controls/TextEntry.h>
  15. #include "filesystem.h"
  16. #include "PanelListPanel.h"
  17. #include "scriptobject.h"
  18. #include <tier0/vcrmode.h>
  19. #include "tier1/convar.h"
  20. #include "EngineInterface.h"
  21. #include "CvarToggleCheckButton.h"
  22. // memdbgon must be the last include file in a .cpp file!!!
  23. #include <tier0/memdbgon.h>
  24. // for join team combo box
  25. enum BotGUITeamType
  26. {
  27. BOT_GUI_TEAM_RANDOM = 0,
  28. BOT_GUI_TEAM_CT = 1,
  29. BOT_GUI_TEAM_T = 2
  30. };
  31. // these must correlate with above enum
  32. static const char *joinTeamArg[] = { "any", "ct", "t", NULL };
  33. // for bot chatter combo box
  34. enum BotGUIChatterType
  35. {
  36. BOT_GUI_CHATTER_NORMAL = 0,
  37. BOT_GUI_CHATTER_MINIMAL = 1,
  38. BOT_GUI_CHATTER_RADIO = 2,
  39. BOT_GUI_CHATTER_OFF = 3
  40. };
  41. // these must correlate with above enum
  42. static const char *chatterArg[] = { "normal", "minimal", "radio", "off", NULL };
  43. extern void UTIL_StripInvalidCharacters( char *pszInput );
  44. //-----------------------------------------------------------------------------
  45. void CCreateMultiplayerGameBotPage::SetJoinTeamCombo( const char *team )
  46. {
  47. if (team)
  48. {
  49. for( int i=0; joinTeamArg[i]; ++i )
  50. if (!stricmp( team, joinTeamArg[i] ))
  51. {
  52. m_joinTeamCombo->ActivateItemByRow( i );
  53. return;
  54. }
  55. }
  56. else
  57. {
  58. m_joinTeamCombo->ActivateItemByRow( BOT_GUI_TEAM_RANDOM );
  59. }
  60. }
  61. //-----------------------------------------------------------------------------
  62. void CCreateMultiplayerGameBotPage::SetChatterCombo( const char *chatter )
  63. {
  64. if (chatter)
  65. {
  66. for( int i=0; chatterArg[i]; ++i )
  67. if (!stricmp( chatter, chatterArg[i] ))
  68. {
  69. m_chatterCombo->ActivateItemByRow( i );
  70. return;
  71. }
  72. }
  73. else
  74. {
  75. m_joinTeamCombo->ActivateItemByRow( BOT_GUI_CHATTER_NORMAL );
  76. }
  77. }
  78. //-----------------------------------------------------------------------------
  79. // Constructor
  80. //-----------------------------------------------------------------------------
  81. CCreateMultiplayerGameBotPage::CCreateMultiplayerGameBotPage( vgui::Panel *parent, const char *name, KeyValues *botKeys ) : PropertyPage( parent, name )
  82. {
  83. m_pSavedData = botKeys;
  84. m_allowRogues = new CCvarToggleCheckButton( this, "BotAllowRogueCheck", "", "bot_allow_rogues" );
  85. m_allowPistols = new CCvarToggleCheckButton( this, "BotAllowPistolsCheck", "", "bot_allow_pistols" );
  86. m_allowShotguns = new CCvarToggleCheckButton( this, "BotAllowShotgunsCheck", "", "bot_allow_shotguns" );
  87. m_allowSubmachineGuns = new CCvarToggleCheckButton( this, "BotAllowSubmachineGunsCheck", "", "bot_allow_sub_machine_guns" );
  88. m_allowRifles = new CCvarToggleCheckButton( this, "BotAllowRiflesCheck", "", "bot_allow_rifles" );
  89. m_allowMachineGuns = new CCvarToggleCheckButton( this, "BotAllowMachineGunsCheck", "", "bot_allow_machine_guns" );
  90. m_allowGrenades = new CCvarToggleCheckButton( this, "BotAllowGrenadesCheck", "", "bot_allow_grenades" );
  91. m_allowSnipers = new CCvarToggleCheckButton( this, "BotAllowSnipersCheck", "", "bot_allow_snipers" );
  92. #ifdef CS_SHIELD_ENABLED
  93. m_allowShields = new CCvarToggleCheckButton( this, "BotAllowShieldCheck", "", "bot_allow_shield" );
  94. #endif // CS_SHIELD_ENABLED
  95. m_joinAfterPlayer = new CCvarToggleCheckButton( this, "BotJoinAfterPlayerCheck", "", "bot_join_after_player" );
  96. m_deferToHuman = new CCvarToggleCheckButton( this, "BotDeferToHumanCheck", "", "bot_defer_to_human" );
  97. // set up team join combo box
  98. // NOTE: If order of AddItem is changed, update the associated enum
  99. m_joinTeamCombo = new ComboBox( this, "BotJoinTeamCombo", 3, false );
  100. m_joinTeamCombo->AddItem( "#Cstrike_Random", NULL );
  101. m_joinTeamCombo->AddItem( "#Cstrike_Team_CT", NULL );
  102. m_joinTeamCombo->AddItem( "#Cstrike_Team_T", NULL );
  103. // set up chatter combo box
  104. // NOTE: If order of AddItem is changed, update the associated enum
  105. m_chatterCombo = new ComboBox( this, "BotChatterCombo", 4, false );
  106. m_chatterCombo->AddItem( "#Cstrike_Bot_Chatter_Normal", NULL );
  107. m_chatterCombo->AddItem( "#Cstrike_Bot_Chatter_Minimal", NULL );
  108. m_chatterCombo->AddItem( "#Cstrike_Bot_Chatter_Radio", NULL );
  109. m_chatterCombo->AddItem( "#Cstrike_Bot_Chatter_Off", NULL );
  110. // create text entry fields for quota and prefix
  111. m_prefixEntry = new TextEntry( this, "BotPrefixEntry" );
  112. // set positions and sizes from resources file
  113. LoadControlSettings( "Resource/CreateMultiplayerGameBotPage.res" );
  114. // get initial values from bot keys
  115. m_joinAfterPlayer->SetSelected( botKeys->GetInt( "bot_join_after_player", 1 ) );
  116. m_allowRogues->SetSelected( botKeys->GetInt( "bot_allow_rogues", 1 ) );
  117. m_allowPistols->SetSelected( botKeys->GetInt( "bot_allow_pistols", 1 ) );
  118. m_allowShotguns->SetSelected( botKeys->GetInt( "bot_allow_shotguns", 1 ) );
  119. m_allowSubmachineGuns->SetSelected( botKeys->GetInt( "bot_allow_sub_machine_guns", 1 ) );
  120. m_allowMachineGuns->SetSelected( botKeys->GetInt( "bot_allow_machine_guns", 1 ) );
  121. m_allowRifles->SetSelected( botKeys->GetInt( "bot_allow_rifles", 1 ) );
  122. m_allowSnipers->SetSelected( botKeys->GetInt( "bot_allow_snipers", 1 ) );
  123. m_allowGrenades->SetSelected( botKeys->GetInt( "bot_allow_grenades", 1 ) );
  124. #ifdef CS_SHIELD_ENABLED
  125. m_allowShields->SetSelected( botKeys->GetInt( "bot_allow_shield", 1 ) );
  126. #endif // CS_SHIELD_ENABLED
  127. m_deferToHuman->SetSelected( botKeys->GetInt( "bot_defer_to_human", 1 ) );
  128. SetJoinTeamCombo( botKeys->GetString( "bot_join_team", "any" ) );
  129. SetChatterCombo( botKeys->GetString( "bot_chatter", "normal" ) );
  130. // set bot_prefix
  131. const char *prefix = botKeys->GetString( "bot_prefix" );
  132. if (prefix)
  133. SetControlString( "BotPrefixEntry", prefix );
  134. }
  135. //-----------------------------------------------------------------------------
  136. // Destructor
  137. //-----------------------------------------------------------------------------
  138. CCreateMultiplayerGameBotPage::~CCreateMultiplayerGameBotPage()
  139. {
  140. // vgui handles deletion of children automatically through the hierarchy
  141. }
  142. //-----------------------------------------------------------------------------
  143. // Reset values
  144. //-----------------------------------------------------------------------------
  145. void CCreateMultiplayerGameBotPage::OnResetChanges()
  146. {
  147. }
  148. //-----------------------------------------------------------------------------
  149. //-----------------------------------------------------------------------------
  150. void UpdateValue( KeyValues *data, const char *cvarName, int value )
  151. {
  152. data->SetInt( cvarName, value );
  153. ConVarRef var( cvarName );
  154. var.SetValue( value );
  155. }
  156. //-----------------------------------------------------------------------------
  157. //-----------------------------------------------------------------------------
  158. void UpdateValue( KeyValues *data, const char *cvarName, const char *value )
  159. {
  160. data->SetString( cvarName, value );
  161. ConVarRef var( cvarName );
  162. var.SetValue( value );
  163. }
  164. //-----------------------------------------------------------------------------
  165. // Called to get data from the page
  166. //-----------------------------------------------------------------------------
  167. void CCreateMultiplayerGameBotPage::OnApplyChanges()
  168. {
  169. UpdateValue( m_pSavedData, "bot_join_after_player", m_joinAfterPlayer->IsSelected() );
  170. UpdateValue( m_pSavedData, "bot_allow_rogues", m_allowRogues->IsSelected() );
  171. UpdateValue( m_pSavedData, "bot_allow_pistols", m_allowPistols->IsSelected() );
  172. UpdateValue( m_pSavedData, "bot_allow_shotguns", m_allowShotguns->IsSelected() );
  173. UpdateValue( m_pSavedData, "bot_allow_sub_machine_guns", m_allowSubmachineGuns->IsSelected() );
  174. UpdateValue( m_pSavedData, "bot_allow_machine_guns", m_allowMachineGuns->IsSelected() );
  175. UpdateValue( m_pSavedData, "bot_allow_rifles", m_allowRifles->IsSelected() );
  176. UpdateValue( m_pSavedData, "bot_allow_snipers", m_allowSnipers->IsSelected() );
  177. UpdateValue( m_pSavedData, "bot_allow_grenades", m_allowGrenades->IsSelected() );
  178. #ifdef CS_SHIELD_ENABLED
  179. UpdateValue( m_pSavedData, "bot_allow_shield", m_allowShields->IsSelected() );
  180. #endif // CS_SHIELD_ENABLED
  181. UpdateValue( m_pSavedData, "bot_defer_to_human", m_deferToHuman->IsSelected() );
  182. // set bot_join_team
  183. UpdateValue( m_pSavedData, "bot_join_team", joinTeamArg[ m_joinTeamCombo->GetActiveItem() ] );
  184. // set bot_chatter
  185. UpdateValue( m_pSavedData, "bot_chatter", chatterArg[ m_chatterCombo->GetActiveItem() ] );
  186. // set bot_prefix
  187. #define BUF_LENGTH 256
  188. char entryBuffer[ BUF_LENGTH ];
  189. m_prefixEntry->GetText( entryBuffer, BUF_LENGTH );
  190. UpdateValue( m_pSavedData, "bot_prefix", entryBuffer );
  191. }