1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Dialog for setting customizable game options
//
//=============================================================================//
#include "sessionoptionsdialog.h"
#include "engine/imatchmaking.h"
#include "EngineInterface.h"
#include "vgui_controls/ImagePanel.h"
#include "vgui_controls/Label.h"
#include "KeyValues.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
//---------------------------------------------------------------------
// CSessionOptionsDialog
//---------------------------------------------------------------------
CSessionOptionsDialog::CSessionOptionsDialog( vgui::Panel *pParent ) : BaseClass( pParent, "SessionOptions" )
{
SetDeleteSelfOnClose( true );
m_pDialogKeys = NULL;
m_bModifySession = false;
}
CSessionOptionsDialog::~CSessionOptionsDialog()
{
m_pScenarioInfos.PurgeAndDeleteElements();
}
//---------------------------------------------------------------------
// Purpose: Dialog keys contain session contexts and properties
//---------------------------------------------------------------------
void CSessionOptionsDialog::SetDialogKeys( KeyValues *pKeys )
{
m_pDialogKeys = pKeys;
}
//---------------------------------------------------------------------
// Purpose: Strip off the game type from the resource name
//---------------------------------------------------------------------
void CSessionOptionsDialog::SetGameType( const char *pString )
{
// Get the full gametype from the resource name
const char *pGametype = Q_stristr( pString, "_" );
if ( !pGametype )
return;
Q_strncpy( m_szGametype, pGametype + 1, sizeof( m_szGametype ) );
// set the menu filter
m_Menu.SetFilter( m_szGametype );
}
//---------------------------------------------------------------------
// Purpose: Center the dialog on the screen
//---------------------------------------------------------------------
void CSessionOptionsDialog::PerformLayout( void )
{
BaseClass::PerformLayout();
UpdateScenarioDisplay();
MoveToCenterOfScreen();
if ( m_pRecommendedLabel )
{
bool bHosting = ( Q_stristr( m_szGametype, "Host" ) );
m_pRecommendedLabel->SetVisible( bHosting );
}
}
//---------------------------------------------------------------------
// Purpose: Apply common properties and contexts
//---------------------------------------------------------------------
void CSessionOptionsDialog::ApplyCommonProperties( KeyValues *pKeys )
{
for ( KeyValues *pProperty = pKeys->GetFirstSubKey(); pProperty != NULL; pProperty = pProperty->GetNextKey() )
{
const char *pName = pProperty->GetName();
if ( !Q_stricmp( pName, "SessionContext" ) )
{
// Create a new session context
sessionProperty_t ctx;
ctx.nType = SESSION_CONTEXT;
Q_strncpy( ctx.szID, pProperty->GetString( "id", "NULL" ), sizeof( ctx.szID ) );
Q_strncpy( ctx.szValue, pProperty->GetString( "value", "NULL" ), sizeof( ctx.szValue ) );
// ctx.szValueType not used
m_SessionProperties.AddToTail( ctx );
}
else if ( !Q_stricmp( pName, "SessionProperty" ) )
{
// Create a new session property
sessionProperty_t prop;
prop.nType = SESSION_PROPERTY;
Q_strncpy( prop.szID, pProperty->GetString( "id", "NULL" ), sizeof( prop.szID ) );
Q_strncpy( prop.szValue, pProperty->GetString( "value", "NULL" ), sizeof( prop.szValue ) );
Q_strncpy( prop.szValueType, pProperty->GetString( "valuetype", "NULL" ), sizeof( prop.szValueType ) );
m_SessionProperties.AddToTail( prop );
}
else if ( !Q_stricmp( pName, "SessionFlag" ) )
{
sessionProperty_t flag;
flag.nType = SESSION_FLAG;
Q_strncpy( flag.szID, pProperty->GetString(), sizeof( flag.szID ) );
m_SessionProperties.AddToTail( flag );
}
}
}
//---------------------------------------------------------------------
// Purpose: Parse session properties and contexts from the resource file
//---------------------------------------------------------------------
void CSessionOptionsDialog::ApplySettings( KeyValues *pResourceData )
{
BaseClass::ApplySettings( pResourceData );
// Apply settings common to all game types
ApplyCommonProperties( pResourceData );
// Apply settings specific to this game type
KeyValues *pSettings = pResourceData->FindKey( m_szGametype );
if ( pSettings )
{
Q_strncpy( m_szCommand, pSettings->GetString( "commandstring", "NULL" ), sizeof( m_szCommand ) );
m_pTitle->SetText( pSettings->GetString( "title", "Unknown" ) );
ApplyCommonProperties( pSettings );
}
KeyValues *pScenarios = pResourceData->FindKey( "ScenarioInfoPanels" );
if ( pScenarios )
{
for ( KeyValues *pScenario = pScenarios->GetFirstSubKey(); pScenario != NULL; pScenario = pScenario->GetNextKey() )
{
CScenarioInfoPanel *pScenarioInfo = new CScenarioInfoPanel( this, "ScenarioInfoPanel" );
SETUP_PANEL( pScenarioInfo );
pScenarioInfo->m_pTitle->SetText( pScenario->GetString( "title" ) );
pScenarioInfo->m_pSubtitle->SetText( pScenario->GetString( "subtitle" ) );
pScenarioInfo->m_pMapImage->SetImage( pScenario->GetString( "image" ) );
int nTall = pScenario->GetInt( "tall", -1 );
if ( nTall > 0 )
{
pScenarioInfo->SetTall( nTall );
}
m_pScenarioInfos.AddToTail( pScenarioInfo );
}
}
if ( Q_stristr( m_szGametype, "Modify" ) )
{
m_bModifySession = true;
}
}
int CSessionOptionsDialog::GetMaxPlayersRecommendedOption( void )
{
MM_QOS_t qos = matchmaking->GetQosWithLIVE();
// Conservatively assume that every player needs ~ 7 kBytes/s
// plus one for the hosting player.
int numPlayersCanService = 1 + int( qos.flBwUpKbs / 7.0f );
// Determine the option that suits our B/W bests
int options[] = { 8, 12, 16 };
for ( int k = 1; k < ARRAYSIZE( options ); ++ k )
{
if ( options[k] > numPlayersCanService )
{
Msg( "[SessionOptionsDialog] Defaulting number of players to %d (upstream b/w = %.1f kB/s ~ %d players).\n",
options[k - 1], qos.flBwUpKbs, numPlayersCanService );
return k - 1;
}
}
return ARRAYSIZE( options ) - 1;
}
//---------------------------------------------------------------------
// Purpose: Set up colors and other such stuff
//---------------------------------------------------------------------
void CSessionOptionsDialog::ApplySchemeSettings( vgui::IScheme *pScheme )
{
BaseClass::ApplySchemeSettings( pScheme );
for ( int i = 0; i < m_pScenarioInfos.Count(); ++i )
{
m_pScenarioInfos[i]->SetBgColor( pScheme->GetColor( "TanDark", Color( 0, 0, 0, 255 ) ) );
}
m_pRecommendedLabel = dynamic_cast<vgui::Label *>(FindChildByName( "RecommendedLabel" ));
}
//---------------------------------------------------------------------
// Purpose: Send all properties and contexts to the matchmaking session
//---------------------------------------------------------------------
void CSessionOptionsDialog::SetupSession( void )
{
KeyValues *pKeys = new KeyValues( "SessionKeys" );
// Send user-selected properties and contexts
for ( int i = 0; i < m_Menu.GetItemCount(); ++i )
{
COptionsItem *pItem = dynamic_cast< COptionsItem* >( m_Menu.GetItem( i ) );
if ( !pItem )
{
continue;
}
const sessionProperty_t &prop = pItem->GetActiveOption();
KeyValues *pProperty = pKeys->CreateNewKey();
pProperty->SetName( prop.szID );
pProperty->SetInt( "type", prop.nType );
pProperty->SetString( "valuestring", prop.szValue );
pProperty->SetString( "valuetype", prop.szValueType );
pProperty->SetInt( "optionindex", pItem->GetActiveOptionIndex() );
}
// Send contexts and properties parsed from the resource file
for ( int i = 0; i < m_SessionProperties.Count(); ++i )
{
const sessionProperty_t &prop = m_SessionProperties[i];
KeyValues *pProperty = pKeys->CreateNewKey();
pProperty->SetName( prop.szID );
pProperty->SetInt( "type", prop.nType );
pProperty->SetString( "valuestring", prop.szValue );
pProperty->SetString( "valuetype", prop.szValueType );
}
// Matchmaking will make a copy of these keys
matchmaking->SetSessionProperties( pKeys );
pKeys->deleteThis();
}
//-----------------------------------------------------------------
// Purpose: Show the correct scenario image and text
//-----------------------------------------------------------------
void CSessionOptionsDialog::UpdateScenarioDisplay( void )
{
// Check if the selected map has changed (first menu item)
int idx = m_Menu.GetActiveOptionIndex( 0 );
for ( int i = 0; i < m_pScenarioInfos.Count(); ++i )
{
m_pScenarioInfos[i]->SetVisible( i == idx );
}
}
//-----------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------
void CSessionOptionsDialog::OnMenuItemChanged( KeyValues *pData )
{
// which item changed
int iItem = pData->GetInt( "item", -1 );
if ( iItem >= 0 && iItem < m_Menu.GetItemCount() )
{
COptionsItem *pActiveOption = dynamic_cast< COptionsItem* >( m_Menu.GetItem( iItem ) );
if ( pActiveOption )
{
const sessionProperty_t &activeOption = pActiveOption->GetActiveOption();
if ( !Q_strncmp( activeOption.szID, "PROPERTY_GAME_SIZE", sessionProperty_t::MAX_KEY_LEN ) )
{
// make sure the private slots is less than prop.szValue
int iMaxPlayers = atoi(activeOption.szValue);
bool bShouldWarnMaxPlayers = ( pActiveOption->GetActiveOptionIndex() > GetMaxPlayersRecommendedOption() );
m_pRecommendedLabel->SetVisible( bShouldWarnMaxPlayers );
// find the private slots option and repopulate it
for ( int iMenu = 0; iMenu < m_Menu.GetItemCount(); ++iMenu )
{
COptionsItem *pItem = dynamic_cast< COptionsItem* >( m_Menu.GetItem( iMenu ) );
if ( !pItem )
{
continue;
}
const sessionProperty_t &prop = pItem->GetActiveOption();
if ( !Q_strncmp( prop.szID, "PROPERTY_PRIVATE_SLOTS", sessionProperty_t::MAX_KEY_LEN ) )
{
const sessionProperty_t baseProp = pItem->GetActiveOption();
// preserve the selection
int iActiveItem = pItem->GetActiveOptionIndex();
// clear all options
pItem->DeleteAllOptions();
// re-add the items 0 - maxplayers
int nStart = 0;
int nEnd = iMaxPlayers;
int nInterval = 1;
for ( int i = nStart; i <= nEnd; i += nInterval )
{
sessionProperty_t propNew;
propNew.nType = SESSION_PROPERTY;
Q_strncpy( propNew.szID, baseProp.szID, sizeof( propNew.szID ) );
Q_strncpy( propNew.szValueType, baseProp.szValueType, sizeof( propNew.szValueType ) );
Q_snprintf( propNew.szValue, sizeof( propNew.szValue), "%d", i );
pItem->AddOption( propNew.szValue, propNew );
}
// re-set the focus
pItem->SetOptionFocus( min( iActiveItem, iMaxPlayers ) );
// fixup the option sizes
m_Menu.InvalidateLayout();
}
}
}
}
}
}
//-----------------------------------------------------------------
// Purpose: Change properties of a menu item
//-----------------------------------------------------------------
void CSessionOptionsDialog::OverrideMenuItem( KeyValues *pItemKeys )
{
if ( m_bModifySession && m_pDialogKeys )
{
if ( !Q_stricmp( pItemKeys->GetName(), "OptionsItem" ) )
{
const char *pID = pItemKeys->GetString( "id", "NULL" );
KeyValues *pKey = m_pDialogKeys->FindKey( pID );
if ( pKey )
{
pItemKeys->SetInt( "activeoption", pKey->GetInt( "optionindex" ) );
}
}
}
//
// When hosting a new session on LIVE:
// - restrict max number of players to bandwidth allowed
//
if ( !m_bModifySession &&
( !Q_stricmp( m_szGametype, "hoststandard" ) || !Q_stricmp( m_szGametype, "hostranked" ) )
)
{
if ( !Q_stricmp( pItemKeys->GetName(), "OptionsItem" ) )
{
const char *pID = pItemKeys->GetString( "id", "NULL" );
if ( !Q_stricmp( pID, "PROPERTY_GAME_SIZE" ) )
{
pItemKeys->SetInt( "activeoption", GetMaxPlayersRecommendedOption() );
}
}
}
}
//-----------------------------------------------------------------
// Purpose: Send key presses to the dialog's menu
//-----------------------------------------------------------------
void CSessionOptionsDialog::OnKeyCodePressed( vgui::KeyCode code )
{
if ( code == KEY_XBUTTON_A || code == STEAMCONTROLLER_A )
{
OnCommand( m_szCommand );
}
else if ( (code == KEY_XBUTTON_B || code == STEAMCONTROLLER_B) && m_bModifySession )
{
// Return to the session lobby without making any changes
OnCommand( "DialogClosing" );
}
else
{
BaseClass::OnKeyCodePressed( code );
}
UpdateScenarioDisplay();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CSessionOptionsDialog::OnThink()
{
vgui::KeyCode code = m_KeyRepeat.KeyRepeated();
if ( code )
{
m_Menu.HandleKeyCode( code );
UpdateScenarioDisplay();
}
BaseClass::OnThink();
}
//---------------------------------------------------------------------
// Purpose: Handle menu commands
//---------------------------------------------------------------------
void CSessionOptionsDialog::OnCommand( const char *pCommand )
{
// Don't set up the session if the dialog is just closing
if ( Q_stricmp( pCommand, "DialogClosing" ) )
{
SetupSession();
OnClose();
}
GetParent()->OnCommand( pCommand );
}
|