Skip to content

[ALL] Add the ability to override weapon slot and position, with very basic TF2 UI support #1202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
16 changes: 0 additions & 16 deletions src/game/client/tf/c_tf_weapon_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,22 +239,6 @@ bool C_TFWeaponBuilder::IsPlacingObject( void )
return false;
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
int C_TFWeaponBuilder::GetSlot( void ) const
{
return GetObjectInfo( m_iObjectType )->m_SelectionSlot;
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
int C_TFWeaponBuilder::GetPosition( void ) const
{
return GetObjectInfo( m_iObjectType )->m_SelectionPosition;
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
Expand Down
3 changes: 0 additions & 3 deletions src/game/client/tf/c_tf_weapon_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ class C_TFWeaponBuilder : public C_TFWeaponBase

virtual void UpdateAttachmentModels( void );

virtual int GetSlot( void ) const;
virtual int GetPosition( void ) const;

void SetupObjectSelectionSprite( void );

virtual CHudTexture const *GetSpriteActive( void ) const;
Expand Down
90 changes: 49 additions & 41 deletions src/game/client/tf/tf_hud_weaponselection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ void CHudWeaponSelection::PerformLayout( void )
// calculate where to start drawing

int iActiveSlot = (pSelectedWeapon ? pSelectedWeapon->GetSlot() : -1);
int iActivePosition = (pSelectedWeapon ? pSelectedWeapon->GetPosition() : -1);

SlotLayout_t rSlot[ MAX_WEAPON_SLOTS ];
ComputeSlotLayout( rSlot, iActiveSlot, fastswitch );
Expand All @@ -550,7 +551,7 @@ void CHudWeaponSelection::PerformLayout( void )
{
for ( int slotpos = 0; slotpos < MAX_WEAPON_POSITIONS; slotpos++ )
{
C_BaseCombatWeapon *pWeapon = GetWeaponInSlot(i, slotpos);
C_BaseCombatWeapon *pWeapon = GetWeaponInSlot(i, ( iActivePosition + slotpos ) % MAX_WEAPON_POSITIONS );
if ( !pWeapon )
continue;

Expand All @@ -573,6 +574,7 @@ void CHudWeaponSelection::PerformLayout( void )

m_pModelPanels[i]->SetPos( rSlot[i].x, rSlot[ i ].y );
m_pModelPanels[i]->SetVisible( true );
break;
}
}
else
Expand Down Expand Up @@ -705,6 +707,7 @@ void CHudWeaponSelection::DrawSelection( C_BaseCombatWeapon *pSelectedWeapon )

// calculate where to start drawing
int iActiveSlot = (pSelectedWeapon ? pSelectedWeapon->GetSlot() : -1);
int iActivePosition = (pSelectedWeapon ? pSelectedWeapon->GetPosition() : -1);
int nFastswitchMode = hud_fastswitch.GetInt();
if ( ::input->IsSteamControllerActive() )
{
Expand Down Expand Up @@ -732,10 +735,9 @@ void CHudWeaponSelection::DrawSelection( C_BaseCombatWeapon *pSelectedWeapon )

if ( i == iActiveSlot )
{
bool bFirstItem = true;
for ( int slotpos = 0; slotpos < MAX_WEAPON_POSITIONS; slotpos++ )
{
C_BaseCombatWeapon *pWeapon = GetWeaponInSlot(i, slotpos);
C_BaseCombatWeapon *pWeapon = GetWeaponInSlot(i, ( iActivePosition + slotpos ) % MAX_WEAPON_POSITIONS );
if ( !pWeapon )
continue;

Expand All @@ -753,20 +755,18 @@ void CHudWeaponSelection::DrawSelection( C_BaseCombatWeapon *pSelectedWeapon )

if ( pWeapon == pSelectedWeapon || ( m_iDemoModeSlot == i ) )
{
// draw the number
int shortcut = bFirstItem ? i + 1 : -1;
if ( IsPC() && shortcut >= 0 && nFastswitchMode != HUDTYPE_PLUS )
if ( IsPC() && nFastswitchMode != HUDTYPE_PLUS )
{
Color numberColor = m_NumberColor;
numberColor[3] *= m_flSelectionAlphaOverride / 255.0f;
surface()->DrawSetTextColor(numberColor);
surface()->DrawSetTextFont(m_hNumberFont);
wchar_t wch = '0' + shortcut;
wchar_t wch = '0' + i + 1;
surface()->DrawSetTextPos( xpos + wide - XRES(5) - m_flSelectionNumberXPos, ypos + YRES(5) + m_flSelectionNumberYPos );
surface()->DrawUnicodeChar(wch);
}
}
bFirstItem = false;
break;
}
}
else
Expand Down Expand Up @@ -922,8 +922,6 @@ void CHudWeaponSelection::OpenSelection( void )

CBaseHudWeaponSelection::OpenSelection();
g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("OpenWeaponSelectionMenu");
m_iSelectedBoxPosition = 0;
m_iSelectedSlot = -1;
}

//-----------------------------------------------------------------------------
Expand All @@ -940,6 +938,8 @@ void CHudWeaponSelection::HideSelection( void )
}

m_flSelectionTime = 0;
m_iSelectedBoxPosition = 0;
m_iSelectedSlot = -1;
CBaseHudWeaponSelection::HideSelection();
g_pClientMode->GetViewportAnimationController()->StartAnimationSequence("CloseWeaponSelectionMenu");
}
Expand Down Expand Up @@ -1278,48 +1278,46 @@ void CHudWeaponSelection::PlusTypeFastWeaponSwitch( int iWeaponSlot, bool *pbPla
if ( !pPlayer )
return;

int newSlot = m_iSelectedSlot;

// Changing slot number does not necessarily mean we need to change the slot - the player could be
// scrolling through the same slot but in the opposite direction. Slot pairs are 0,2 and 1,3 - so
// compare the 0 bits to see if we're within a pair. Otherwise, reset the box to the zero position.
if ( -1 == m_iSelectedSlot || ( ( m_iSelectedSlot ^ iWeaponSlot ) & 1 ) )
if ( -1 == m_iSelectedSlot || iWeaponSlot >= 4 || ( ( m_iSelectedSlot ^ iWeaponSlot ) & 1 ) )
{
// Changing vertical/horizontal direction. Reset the selected box position to zero.
m_iSelectedBoxPosition = 0;
m_iSelectedSlot = iWeaponSlot;

C_BaseCombatWeapon *pWeapon = GetFirstPos( iWeaponSlot );
if ( pWeapon )
{
m_iSelectedBoxPosition = pWeapon->GetPosition();
}
}
else
{
int newSlot = -1;
int newPos = -1;

// Still in the same horizontal/vertical direction. Determine which way we're moving in the slot.
int increment = 1;
if ( m_iSelectedSlot != iWeaponSlot )
{
// Decrementing within the slot. If we're at the zero position in this slot,
// jump to the zero position of the opposite slot. This also counts as our increment.
increment = -1;
if ( 0 == m_iSelectedBoxPosition )
{
newSlot = ( m_iSelectedSlot + 2 ) % 4;
increment = 0;
}
}
int increment = m_iSelectedSlot == iWeaponSlot ? 1 : -1;

// Find out of the box position is at the end of the slot
int lastSlotPos = -1;
for ( int slotPos = 0; slotPos < MAX_WEAPON_POSITIONS; ++slotPos )
// Lay out the paired slots ranging from -20 to 19, where negative is the opposite slot and goes from -1 to -20.
for ( int slotPos = m_iSelectedBoxPosition + increment; slotPos < MAX_WEAPON_POSITIONS && slotPos >= -MAX_WEAPON_POSITIONS; slotPos += increment )
{
C_BaseCombatWeapon *pWeapon = GetWeaponInSlot( newSlot, slotPos );
if ( pWeapon )
int slot = slotPos < 0 ? ( m_iSelectedSlot + 2 ) % 4 : m_iSelectedSlot;
int pos = slotPos < 0 ? -slotPos - 1 : slotPos;
C_BaseCombatWeapon *pWeapon = GetWeaponInSlot( slot, pos );
if ( pWeapon && pWeapon->VisibleInWeaponSelection() )
{
lastSlotPos = slotPos;
newSlot = slot;
newPos = pos;
break;
}
}

// Increment/Decrement the selected box position
if ( m_iSelectedBoxPosition + increment <= lastSlotPos )
// Set the selected box position
if ( newSlot > -1 && newPos > -1 )
{
m_iSelectedBoxPosition += increment;
m_iSelectedBoxPosition = newPos;
m_iSelectedSlot = newSlot;
}
else
Expand Down Expand Up @@ -1390,13 +1388,23 @@ void CHudWeaponSelection::SelectWeaponSlot( int iSlot )
}

case HUDTYPE_PLUS:
PlusTypeFastWeaponSwitch( iSlot, &bPlaySwitchSound );

// ------------------------------------------------------
// FALLTHROUGH! Plus and buckets both use the item model
// panels so fix them up in both cases.
// ------------------------------------------------------
{
PlusTypeFastWeaponSwitch( iSlot, &bPlaySwitchSound );
C_BaseCombatWeapon *pActiveWeapon = GetSelectedWeapon();
if ( pActiveWeapon != NULL )
{
if ( !IsInSelectionMode() )
{
// open the weapon selection
OpenSelection();
}

InvalidateLayout();
m_iDemoModeSlot = -1;
m_flDemoStartTime = -1;
}
return;
}

case HUDTYPE_BUCKETS:
{
Expand Down
10 changes: 5 additions & 5 deletions src/game/client/weapon_selection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,15 @@ bool CBaseHudWeaponSelection::CanBeSelectedInHUD( C_BaseCombatWeapon *pWeapon )
nFastswitchMode = HUDTYPE_FASTSWITCH;
}

// Xbox: In plus type, weapons without ammo can still be selected in the HUD
if( HUDTYPE_PLUS == nFastswitchMode )
if ( !pWeapon->VisibleInWeaponSelection() )
{
return pWeapon->VisibleInWeaponSelection();
return false;
}

if ( !pWeapon->VisibleInWeaponSelection() )
// Xbox: In plus type, weapons without ammo can still be selected in the HUD
if( HUDTYPE_PLUS == nFastswitchMode )
{
return false;
return true;
}

// All other current hud types
Expand Down
2 changes: 2 additions & 0 deletions src/game/server/basecombatweapon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ BEGIN_ENT_SCRIPTDESC( CBaseCombatWeapon, BASECOMBATWEAPON_DERIVED_FROM, "Base Co
DEFINE_SCRIPTFUNC( GetWeight, "Get the weapon weighting/importance" )
DEFINE_SCRIPTFUNC( GetWeaponFlags, "Get the weapon flags" )
DEFINE_SCRIPTFUNC( GetSlot, "Gets the weapon's current slot" )
DEFINE_SCRIPTFUNC( SetSlot, "Sets the weapon's current slot" )
DEFINE_SCRIPTFUNC( GetPosition, "Gets the weapon's current position" )
DEFINE_SCRIPTFUNC( SetPosition, "Sets the weapon's current position" )
DEFINE_SCRIPTFUNC( GetName, "Gets the weapon's name" )
DEFINE_SCRIPTFUNC( GetPrintName, "Gets the weapon's print name" )
DEFINE_SCRIPTFUNC( IsMeleeWeapon, "Returns whether this is a melee weapon" )
Expand Down
22 changes: 5 additions & 17 deletions src/game/server/tf/tf_weapon_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ CTFWeaponBuilder::~CTFWeaponBuilder()
void CTFWeaponBuilder::SetSubType( int iSubType )
{
m_iObjectType = iSubType;


const CObjectInfo *pInfo = GetObjectInfo( m_iObjectType );
SetSlot( pInfo->m_SelectionSlot );
SetPosition( pInfo->m_SelectionPosition );

// m_iViewModelIndex is set by the base Precache(), which didn't know what
// type of object we built, so it didn't get the right viewmodel index.
// Now that our data is filled in, go and get the right index.
Expand Down Expand Up @@ -1148,22 +1152,6 @@ bool CTFWeaponBuilder::HasAmmo( void )
return ( pOwner->GetBuildResources() >= iCost );
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
int CTFWeaponBuilder::GetSlot( void ) const
{
return GetObjectInfo( m_iObjectType )->m_SelectionSlot;
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
int CTFWeaponBuilder::GetPosition( void ) const
{
return GetObjectInfo( m_iObjectType )->m_SelectionPosition;
}

//-----------------------------------------------------------------------------
// Purpose:
// Output : char const
Expand Down
2 changes: 0 additions & 2 deletions src/game/server/tf/tf_weapon_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ class CTFWeaponBuilder : public CTFWeaponBase

// Selection
bool HasAmmo( void );
int GetSlot( void ) const;
int GetPosition( void ) const;
const char *GetPrintName( void ) const;
bool CanBuildObjectType( int iObjectType );
void SetObjectTypeAsBuildable( int iObjectType );
Expand Down
Loading