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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================//
#include "cbase.h"
#include "weapon_selection.h"
#include "iclientmode.h"
#include "history_resource.h"
#include "input.h"
#include <KeyValues.h>
#include <vgui/IScheme.h>
#include <vgui/ISurface.h>
#include <vgui/ISystem.h>
#include <vgui_controls/AnimationController.h>
#include <vgui_controls/Panel.h>
#include "vgui/ILocalize.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
//-----------------------------------------------------------------------------
// Purpose: hl2 weapon selection hud element
//-----------------------------------------------------------------------------
class CHudWeaponSelection : public CBaseHudWeaponSelection, public vgui::Panel
{
DECLARE_CLASS_SIMPLE( CHudWeaponSelection, vgui::Panel );
public:
CHudWeaponSelection(const char *pElementName );
virtual bool ShouldDraw();
virtual void OnWeaponPickup( C_BaseCombatWeapon *pWeapon );
virtual void CycleToNextWeapon( void );
virtual void CycleToPrevWeapon( void );
virtual C_BaseCombatWeapon *GetWeaponInSlot( int iSlot, int iSlotPos );
virtual void SelectWeaponSlot( int iSlot );
virtual C_BaseCombatWeapon *GetSelectedWeapon( void )
{
return m_hSelectedWeapon;
}
virtual void OpenSelection( void );
virtual void HideSelection( void );
virtual void LevelInit();
protected:
virtual void OnThink();
virtual void Paint();
virtual void ApplySchemeSettings(vgui::IScheme *pScheme);
virtual bool IsWeaponSelectable()
{
if (IsInSelectionMode())
return true;
return false;
}
private:
C_BaseCombatWeapon *FindNextWeaponInWeaponSelection(int iCurrentSlot, int iCurrentPosition);
C_BaseCombatWeapon *FindPrevWeaponInWeaponSelection(int iCurrentSlot, int iCurrentPosition);
void FastWeaponSwitch( int iWeaponSlot );
virtual void SetSelectedWeapon( C_BaseCombatWeapon *pWeapon )
{
m_hSelectedWeapon = pWeapon;
}
void DrawBox(int x, int y, int wide, int tall, Color color, float normalizedAlpha, int number);
CPanelAnimationVar( vgui::HFont, m_hNumberFont, "NumberFont", "HudSelectionNumbers" );
CPanelAnimationVar( vgui::HFont, m_hTextFont, "TextFont", "HudSelectionText" );
CPanelAnimationVarAliasType( float, m_flSmallBoxSize, "SmallBoxSize", "32", "proportional_float" );
CPanelAnimationVarAliasType( float, m_flLargeBoxWide, "LargeBoxWide", "108", "proportional_float" );
CPanelAnimationVarAliasType( float, m_flLargeBoxTall, "LargeBoxTall", "72", "proportional_float" );
CPanelAnimationVarAliasType( float, m_flBoxGap, "BoxGap", "12", "proportional_float" );
CPanelAnimationVarAliasType( float, m_flSelectionNumberXPos, "SelectionNumberXPos", "4", "proportional_float" );
CPanelAnimationVarAliasType( float, m_flSelectionNumberYPos, "SelectionNumberYPos", "4", "proportional_float" );
CPanelAnimationVarAliasType( float, m_flTextYPos, "TextYPos", "54", "proportional_float" );
CPanelAnimationVar( float, m_flAlphaOverride, "Alpha", "255" );
CPanelAnimationVar( float, m_flSelectionAlphaOverride, "SelectionAlpha", "255" );
CPanelAnimationVar( Color, m_TextColor, "TextColor", "SelectionTextFg" );
CPanelAnimationVar( Color, m_NumberColor, "NumberColor", "SelectionNumberFg" );
CPanelAnimationVar( Color, m_EmptyBoxColor, "EmptyBoxColor", "SelectionEmptyBoxBg" );
CPanelAnimationVar( Color, m_BoxColor, "BoxColor", "SelectionBoxBg" );
CPanelAnimationVar( Color, m_SelectedBoxColor, "SelectedBoxClor", "SelectionSelectedBoxBg" );
CPanelAnimationVar( float, m_flWeaponPickupGrowTime, "SelectionGrowTime", "0.1" );
CPanelAnimationVar( float, m_flTextScan, "TextScan", "1.0" );
bool m_bFadingOut;
};
DECLARE_HUDELEMENT( CHudWeaponSelection );
using namespace vgui;
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CHudWeaponSelection::CHudWeaponSelection( const char *pElementName ) : CBaseHudWeaponSelection(pElementName), BaseClass(NULL, "HudWeaponSelection")
{
vgui::Panel *pParent = g_pClientMode->GetViewport();
SetParent( pParent );
m_bFadingOut = false;
}
//-----------------------------------------------------------------------------
// Purpose: sets up display for showing weapon pickup
//-----------------------------------------------------------------------------
void CHudWeaponSelection::OnWeaponPickup( C_BaseCombatWeapon *pWeapon )
{
// add to pickup history
CHudHistoryResource *pHudHR = GET_HUDELEMENT( CHudHistoryResource );
if ( pHudHR )
{
pHudHR->AddToHistory( pWeapon );
}
}
#define SELECTION_TIMEOUT_THRESHOLD 5.0f // Seconds
#define SELECTION_FADEOUT_TIME 1.5f
//-----------------------------------------------------------------------------
// Purpose: updates animation status
//-----------------------------------------------------------------------------
void CHudWeaponSelection::OnThink( void )
{
// Time out after awhile of inactivity
if ( ( gpGlobals->curtime - m_flSelectionTime ) > SELECTION_TIMEOUT_THRESHOLD )
{
if (!m_bFadingOut)
{
// start fading out
g_pClientMode->GetViewportAnimationController()->StartAnimationSequence( "FadeOutWeaponSelectionMenu" );
m_bFadingOut = true;
}
else if (gpGlobals->curtime - m_flSelectionTime > SELECTION_TIMEOUT_THRESHOLD + SELECTION_FADEOUT_TIME)
{
// finished fade, close
HideSelection();
}
}
else if (m_bFadingOut)
{
// stop us fading out, show the animation again
g_pClientMode->GetViewportAnimationController()->StartAnimationSequence( "OpenWeaponSelectionMenu" );
m_bFadingOut = false;
}
}
//-----------------------------------------------------------------------------
// Purpose: returns true if the panel should draw
//-----------------------------------------------------------------------------
bool CHudWeaponSelection::ShouldDraw()
{
C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
if ( !pPlayer )
{
if ( IsInSelectionMode() )
{
HideSelection();
}
return false;
}
bool bret = CBaseHudWeaponSelection::ShouldDraw();
if ( !bret )
return false;
return ( m_bSelectionVisible ) ? true : false;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CHudWeaponSelection::LevelInit()
{
CHudElement::LevelInit();
}
//-------------------------------------------------------------------------
// Purpose: draws the selection area
//-------------------------------------------------------------------------
void CHudWeaponSelection::Paint()
{
if (!ShouldDraw())
return;
C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
if ( !pPlayer )
return;
// find and display our current selection
C_BaseCombatWeapon *pSelectedWeapon = GetSelectedWeapon();
if ( !pSelectedWeapon )
return;
int iActiveSlot = (pSelectedWeapon ? pSelectedWeapon->GetSlot() : -1);
// interpolate the selected box size between the small box size and the large box size
// interpolation has been removed since there is no weapon pickup animation anymore, so it's all at the largest size
float percentageDone = 1.0f; //min(1.0f, (gpGlobals->curtime - m_flPickupStartTime) / m_flWeaponPickupGrowTime);
int largeBoxWide = m_flSmallBoxSize + ((m_flLargeBoxWide - m_flSmallBoxSize) * percentageDone);
int largeBoxTall = m_flSmallBoxSize + ((m_flLargeBoxTall - m_flSmallBoxSize) * percentageDone);
Color selectedColor;
{for (int i = 0; i < 4; i++)
{
selectedColor[i] = m_BoxColor[i] + ((m_SelectedBoxColor[i] - m_BoxColor[i]) * percentageDone);
}}
// calculate where to start drawing
int width = (MAX_WEAPON_SLOTS - 1) * (m_flSmallBoxSize + m_flBoxGap) + largeBoxWide;
int xpos = (GetWide() - width) / 2;
int ypos = 0;
// iterate over all the weapon slots
for ( int i = 0; i < MAX_WEAPON_SLOTS; i++ )
{
if ( i == iActiveSlot )
{
bool bFirstItem = true;
for (int slotpos = 0; slotpos < MAX_WEAPON_POSITIONS; slotpos++)
{
C_BaseCombatWeapon *pWeapon = GetWeaponInSlot(i, slotpos);
if ( !pWeapon )
continue;
// draw box for selected weapon
DrawBox(xpos, ypos, largeBoxWide, largeBoxTall, selectedColor, m_flSelectionAlphaOverride, bFirstItem ? i + 1 : -1);
// draw icon
Color col = GetFgColor();
if ( pWeapon->GetSpriteActive() )
{
// find the center of the box to draw in
int iconWidth = pWeapon->GetSpriteActive()->Width();
int iconHeight = pWeapon->GetSpriteActive()->Height();
int x_offs = (largeBoxWide - iconWidth) / 2;
int y_offs = (largeBoxTall - iconHeight) / 2;
if (!pWeapon->CanBeSelected())
{
// unselectable weapon, display as such
col = Color(255, 0, 0, col[3]);
}
else if (pWeapon == pSelectedWeapon)
{
// currently selected weapon, display brighter
col[3] = m_flSelectionAlphaOverride;
// draw an active version over the top
pWeapon->GetSpriteActive()->DrawSelf( xpos + x_offs, ypos + y_offs, col );
}
// draw the inactive version
pWeapon->GetSpriteInactive()->DrawSelf( xpos + x_offs, ypos + y_offs, col );
}
// draw text
col = m_TextColor;
const FileWeaponInfo_t &weaponInfo = pWeapon->GetWpnData();
if (pWeapon == pSelectedWeapon)
{
wchar_t text[128];
wchar_t *tempString = g_pVGuiLocalize->Find(weaponInfo.szPrintName);
// setup our localized string
if ( tempString )
{
#ifdef WIN32
_snwprintf(text, sizeof(text)/sizeof(wchar_t) - 1, L"%s", tempString);
#else
_snwprintf(text, sizeof(text)/sizeof(wchar_t) - 1, L"%S", tempString);
#endif
text[sizeof(text)/sizeof(wchar_t) - 1] = 0;
}
else
{
// string wasn't found by g_pVGuiLocalize->Find()
g_pVGuiLocalize->ConvertANSIToUnicode(weaponInfo.szPrintName, text, sizeof(text));
}
surface()->DrawSetTextColor( col );
surface()->DrawSetTextFont( m_hTextFont );
// count the position
int slen = 0, charCount = 0, maxslen = 0;
int firstslen = 0;
{
for (wchar_t *pch = text; *pch != 0; pch++)
{
if (*pch == '\n')
{
// newline character, drop to the next line
if (slen > maxslen)
{
maxslen = slen;
}
if (!firstslen)
{
firstslen = slen;
}
slen = 0;
}
else if (*pch == '\r')
{
// do nothing
}
else
{
slen += surface()->GetCharacterWidth( m_hTextFont, *pch );
charCount++;
}
}
}
if (slen > maxslen)
{
maxslen = slen;
}
if (!firstslen)
{
firstslen = maxslen;
}
int tx = xpos + ((largeBoxWide - firstslen) / 2);
int ty = ypos + (int)m_flTextYPos;
surface()->DrawSetTextPos( tx, ty );
// adjust the charCount by the scan amount
charCount *= m_flTextScan;
for (wchar_t *pch = text; charCount > 0; pch++)
{
if (*pch == '\n')
{
// newline character, move to the next line
surface()->DrawSetTextPos( xpos + ((largeBoxWide - slen) / 2), ty + (surface()->GetFontTall(m_hTextFont) * 1.1f));
}
else if (*pch == '\r')
{
// do nothing
}
else
{
surface()->DrawUnicodeChar(*pch);
charCount--;
}
}
}
ypos += (largeBoxTall + m_flBoxGap);
bFirstItem = false;
}
xpos += largeBoxWide;
}
else
{
// check to see if there is a weapons in this bucket
if ( GetFirstPos( i ) )
{
// draw has weapon in slot
DrawBox(xpos, ypos, m_flSmallBoxSize, m_flSmallBoxSize, m_BoxColor, m_flAlphaOverride, i + 1);
}
else
{
// draw empty slot
DrawBox(xpos, ypos, m_flSmallBoxSize, m_flSmallBoxSize, m_EmptyBoxColor, m_flAlphaOverride, -1);
}
xpos += m_flSmallBoxSize;
}
// reset position
ypos = 0;
xpos += m_flBoxGap;
}
}
//-----------------------------------------------------------------------------
// Purpose: draws a selection box
//-----------------------------------------------------------------------------
void CHudWeaponSelection::DrawBox(int x, int y, int wide, int tall, Color color, float normalizedAlpha, int number)
{
BaseClass::DrawBox( x, y, wide, tall, color, normalizedAlpha / 255.0f );
// draw the number
if (number >= 0)
{
Color numberColor = m_NumberColor;
numberColor[3] *= normalizedAlpha / 255.0f;
surface()->DrawSetTextColor(numberColor);
surface()->DrawSetTextFont(m_hNumberFont);
wchar_t wch = '0' + number;
surface()->DrawSetTextPos(x + m_flSelectionNumberXPos, y + m_flSelectionNumberYPos);
surface()->DrawUnicodeChar(wch);
}
}
//-----------------------------------------------------------------------------
// Purpose: hud scheme settings
//-----------------------------------------------------------------------------
void CHudWeaponSelection::ApplySchemeSettings(vgui::IScheme *pScheme)
{
BaseClass::ApplySchemeSettings(pScheme);
SetPaintBackgroundEnabled(false);
// set our size
int screenWide, screenTall;
int x, y;
GetPos(x, y);
GetHudSize(screenWide, screenTall);
SetBounds(0, y, screenWide, screenTall - y);
}
//-----------------------------------------------------------------------------
// Purpose: Opens weapon selection control
//-----------------------------------------------------------------------------
void CHudWeaponSelection::OpenSelection( void )
{
Assert(!IsInSelectionMode());
CBaseHudWeaponSelection::OpenSelection();
g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("OpenWeaponSelectionMenu");
}
//-----------------------------------------------------------------------------
// Purpose: Closes weapon selection control immediately
//-----------------------------------------------------------------------------
void CHudWeaponSelection::HideSelection( void )
{
CBaseHudWeaponSelection::HideSelection();
g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("CloseWeaponSelectionMenu");
m_bFadingOut = false;
}
//-----------------------------------------------------------------------------
// Purpose: Returns the next available weapon item in the weapon selection
//-----------------------------------------------------------------------------
C_BaseCombatWeapon *CHudWeaponSelection::FindNextWeaponInWeaponSelection(int iCurrentSlot, int iCurrentPosition)
{
C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
if ( !pPlayer )
return NULL;
C_BaseCombatWeapon *pNextWeapon = NULL;
// search all the weapons looking for the closest next
int iLowestNextSlot = MAX_WEAPON_SLOTS;
int iLowestNextPosition = MAX_WEAPON_POSITIONS;
for ( int i = 0; i < MAX_WEAPONS; i++ )
{
C_BaseCombatWeapon *pWeapon = pPlayer->GetWeapon(i);
if ( !pWeapon )
continue;
if ( pWeapon->CanBeSelected() )
{
int weaponSlot = pWeapon->GetSlot(), weaponPosition = pWeapon->GetPosition();
// see if this weapon is further ahead in the selection list
if ( weaponSlot > iCurrentSlot || (weaponSlot == iCurrentSlot && weaponPosition > iCurrentPosition) )
{
// see if this weapon is closer than the current lowest
if ( weaponSlot < iLowestNextSlot || (weaponSlot == iLowestNextSlot && weaponPosition < iLowestNextPosition) )
{
iLowestNextSlot = weaponSlot;
iLowestNextPosition = weaponPosition;
pNextWeapon = pWeapon;
}
}
}
}
return pNextWeapon;
}
//-----------------------------------------------------------------------------
// Purpose: Returns the prior available weapon item in the weapon selection
//-----------------------------------------------------------------------------
C_BaseCombatWeapon *CHudWeaponSelection::FindPrevWeaponInWeaponSelection(int iCurrentSlot, int iCurrentPosition)
{
C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
if ( !pPlayer )
return NULL;
C_BaseCombatWeapon *pPrevWeapon = NULL;
// search all the weapons looking for the closest next
int iLowestPrevSlot = -1;
int iLowestPrevPosition = -1;
for ( int i = 0; i < MAX_WEAPONS; i++ )
{
C_BaseCombatWeapon *pWeapon = pPlayer->GetWeapon(i);
if ( !pWeapon )
continue;
if ( pWeapon->CanBeSelected() )
{
int weaponSlot = pWeapon->GetSlot(), weaponPosition = pWeapon->GetPosition();
// see if this weapon is further ahead in the selection list
if ( weaponSlot < iCurrentSlot || (weaponSlot == iCurrentSlot && weaponPosition < iCurrentPosition) )
{
// see if this weapon is closer than the current lowest
if ( weaponSlot > iLowestPrevSlot || (weaponSlot == iLowestPrevSlot && weaponPosition > iLowestPrevPosition) )
{
iLowestPrevSlot = weaponSlot;
iLowestPrevPosition = weaponPosition;
pPrevWeapon = pWeapon;
}
}
}
}
return pPrevWeapon;
}
//-----------------------------------------------------------------------------
// Purpose: Moves the selection to the next item in the menu
//-----------------------------------------------------------------------------
void CHudWeaponSelection::CycleToNextWeapon( void )
{
// Get the local player.
C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
if ( !pPlayer )
return;
C_BaseCombatWeapon *pNextWeapon = NULL;
if ( IsInSelectionMode() )
{
// find the next selection spot
C_BaseCombatWeapon *pWeapon = GetSelectedWeapon();
if ( !pWeapon )
return;
pNextWeapon = FindNextWeaponInWeaponSelection( pWeapon->GetSlot(), pWeapon->GetPosition() );
}
else
{
// open selection at the current place
pNextWeapon = pPlayer->GetActiveWeapon();
if ( pNextWeapon )
{
pNextWeapon = FindNextWeaponInWeaponSelection( pNextWeapon->GetSlot(), pNextWeapon->GetPosition() );
}
}
if ( !pNextWeapon )
{
// wrap around back to start
pNextWeapon = FindNextWeaponInWeaponSelection(-1, -1);
}
if ( pNextWeapon )
{
SetSelectedWeapon( pNextWeapon );
if ( !IsInSelectionMode() )
{
OpenSelection();
}
// Play the "cycle to next weapon" sound
pPlayer->EmitSound( "Player.WeaponSelectionMoveSlot" );
}
}
//-----------------------------------------------------------------------------
// Purpose: Moves the selection to the previous item in the menu
//-----------------------------------------------------------------------------
void CHudWeaponSelection::CycleToPrevWeapon( void )
{
// Get the local player.
C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
if ( !pPlayer )
return;
C_BaseCombatWeapon *pNextWeapon = NULL;
if ( IsInSelectionMode() )
{
// find the next selection spot
C_BaseCombatWeapon *pWeapon = GetSelectedWeapon();
if ( !pWeapon )
return;
pNextWeapon = FindPrevWeaponInWeaponSelection( pWeapon->GetSlot(), pWeapon->GetPosition() );
}
else
{
// open selection at the current place
pNextWeapon = pPlayer->GetActiveWeapon();
if ( pNextWeapon )
{
pNextWeapon = FindPrevWeaponInWeaponSelection( pNextWeapon->GetSlot(), pNextWeapon->GetPosition() );
}
}
if ( !pNextWeapon )
{
// wrap around back to end of weapon list
pNextWeapon = FindPrevWeaponInWeaponSelection(MAX_WEAPON_SLOTS, MAX_WEAPON_POSITIONS);
}
if ( pNextWeapon )
{
SetSelectedWeapon( pNextWeapon );
if ( !IsInSelectionMode() )
{
OpenSelection();
}
// Play the "cycle to next weapon" sound
pPlayer->EmitSound( "Player.WeaponSelectionMoveSlot" );
}
}
//-----------------------------------------------------------------------------
// Purpose: returns the weapon in the specified slot
//-----------------------------------------------------------------------------
C_BaseCombatWeapon *CHudWeaponSelection::GetWeaponInSlot( int iSlot, int iSlotPos )
{
C_BasePlayer *player = C_BasePlayer::GetLocalPlayer();
if ( !player )
return NULL;
for ( int i = 0; i < MAX_WEAPONS; i++ )
{
C_BaseCombatWeapon *pWeapon = player->GetWeapon(i);
if ( pWeapon == NULL )
continue;
if ( pWeapon->GetSlot() == iSlot && pWeapon->GetPosition() == iSlotPos )
return pWeapon;
}
return NULL;
}
//-----------------------------------------------------------------------------
// Purpose: Opens the next weapon in the slot
//-----------------------------------------------------------------------------
void CHudWeaponSelection::FastWeaponSwitch( int iWeaponSlot )
{
// get the slot the player's weapon is in
C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
if ( !pPlayer )
return;
// see where we should start selection
int iPosition = -1;
C_BaseCombatWeapon *pActiveWeapon = pPlayer->GetActiveWeapon();
if ( pActiveWeapon && pActiveWeapon->GetSlot() == iWeaponSlot )
{
// start after this weapon
iPosition = pActiveWeapon->GetPosition();
}
C_BaseCombatWeapon *pNextWeapon = NULL;
// search for the weapon after the current one
pNextWeapon = FindNextWeaponInWeaponSelection(iWeaponSlot, iPosition);
// make sure it's in the same bucket
if ( !pNextWeapon || pNextWeapon->GetSlot() != iWeaponSlot )
{
// just look for any weapon in this slot
pNextWeapon = FindNextWeaponInWeaponSelection(iWeaponSlot, -1);
}
// see if we found a weapon that's different from the current and in the selected slot
if ( pNextWeapon && pNextWeapon != pActiveWeapon && pNextWeapon->GetSlot() == iWeaponSlot )
{
// select the new weapon
::input->MakeWeaponSelection( pNextWeapon );
}
else if ( pNextWeapon != pActiveWeapon )
{
// error sound
pPlayer->EmitSound( "Player.DenyWeaponSelection" );
}
}
//-----------------------------------------------------------------------------
// Purpose: Moves selection to the specified slot
//-----------------------------------------------------------------------------
void CHudWeaponSelection::SelectWeaponSlot( int iSlot )
{
// iSlot is one higher than it should be, since it's the number key, not the 0-based index into the weapons
--iSlot;
// Get the local player.
C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
if ( !pPlayer )
return;
// Don't try and read past our possible number of slots
if ( iSlot > MAX_WEAPON_SLOTS )
return;
// Make sure the player's allowed to switch weapons
if ( pPlayer->IsAllowedToSwitchWeapons() == false )
return;
// do a fast switch if set
if ( hud_fastswitch.GetBool() )
{
FastWeaponSwitch( iSlot );
return;
}
int slotPos = 0;
C_BaseCombatWeapon *pActiveWeapon = GetSelectedWeapon();
// start later in the list
if ( IsInSelectionMode() && pActiveWeapon && pActiveWeapon->GetSlot() == iSlot )
{
slotPos = pActiveWeapon->GetPosition() + 1;
}
// find the weapon in this slot
pActiveWeapon = GetNextActivePos( iSlot, slotPos );
if ( !pActiveWeapon )
{
pActiveWeapon = GetNextActivePos( iSlot, 0 );
}
if ( pActiveWeapon != NULL )
{
if ( !IsInSelectionMode() )
{
// open the weapon selection
OpenSelection();
}
// Mark the change
SetSelectedWeapon( pActiveWeapon );
}
pPlayer->EmitSound( "Player.WeaponSelectionMoveSlot" );
}
|