Skip to content

Commit

Permalink
Merge pull request #212 from Toodles2You/weaponselect
Browse files Browse the repository at this point in the history
Have clients select weapons by ID, rather than by name
  • Loading branch information
SamVanheer authored Aug 19, 2023
2 parents b20ff77 + 09e4338 commit 7709454
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 45 deletions.
4 changes: 2 additions & 2 deletions cl_dll/ammo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ void CHudAmmo::Think()
{
if (gpActiveSel != (WEAPON*)1)
{
ServerCmd(gpActiveSel->szName);
// ServerCmd(gpActiveSel->szName);
g_weaponselect = gpActiveSel->iId;
}

Expand Down Expand Up @@ -445,7 +445,7 @@ void WeaponsResource::SelectSlot(int iSlot, bool fAdvance, int iDirection)
WEAPON* p2 = GetNextActivePos(p->iSlot, p->iSlotPos);
if (!p2)
{ // only one active item in bucket, so change directly to weapon
ServerCmd(p->szName);
// ServerCmd(p->szName);
g_weaponselect = p->iId;
return;
}
Expand Down
3 changes: 3 additions & 0 deletions cl_dll/hl/hl_baseentity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@ bool CBasePlayer::Restore(CRestore& restore) { return false; }
void CBasePlayer::SelectNextItem(int iItem) {}
bool CBasePlayer::HasWeapons() { return false; }
void CBasePlayer::SelectPrevItem(int iItem) {}
CBasePlayerItem* CBasePlayer::GetNamedPlayerItem(const char* pstr) { return nullptr; }
CBasePlayerItem* CBasePlayer::GetPlayerItem(int iId) { return nullptr; }
void CBasePlayer::SelectItem(CBasePlayerItem* pItem) {}
bool CBasePlayer::FlashlightIsOn() { return false; }
void CBasePlayer::FlashlightTurnOn() {}
void CBasePlayer::FlashlightTurnOff() {}
Expand Down
33 changes: 0 additions & 33 deletions cl_dll/hl/hl_weapons.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,39 +256,6 @@ Vector CBaseEntity::FireBulletsPlayer(unsigned int cShots, Vector vecSrc, Vector
return Vector(x * vecSpread.x, y * vecSpread.y, 0.0);
}

/*
=====================
CBasePlayer::SelectItem
Switch weapons
=====================
*/
void CBasePlayer::SelectItem(const char* pstr)
{
if (!pstr)
return;

CBasePlayerItem* pItem = NULL;

if (!pItem)
return;


if (pItem == m_pActiveItem)
return;

if (m_pActiveItem)
m_pActiveItem->Holster();

m_pLastItem = m_pActiveItem;
m_pActiveItem = pItem;

if (m_pActiveItem)
{
m_pActiveItem->Deploy();
}
}

/*
=====================
CBasePlayer::Killed
Expand Down
26 changes: 24 additions & 2 deletions dlls/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -546,11 +546,21 @@ void ClientCommand(edict_t* pEntity)
}
else if (FStrEq(pcmd, "use"))
{
player->SelectItem((char*)CMD_ARGV(1));
auto weapon = player->GetNamedPlayerItem((char*)CMD_ARGV(1));

if (weapon != nullptr)
{
player->SelectItem(weapon);
}
}
else if (((pstr = strstr(pcmd, "weapon_")) != NULL) && (pstr == pcmd))
{
player->SelectItem(pcmd);
auto weapon = player->GetNamedPlayerItem(pcmd);

if (weapon != nullptr)
{
player->SelectItem(weapon);
}
}
else if (FStrEq(pcmd, "lastinv"))
{
Expand Down Expand Up @@ -1808,6 +1818,18 @@ void CmdStart(const edict_t* player, const struct usercmd_s* cmd, unsigned int r

if (!pl)
return;

if (cmd->weaponselect != 0)
{
auto weapon = pl->GetPlayerItem(cmd->weaponselect);

if (weapon != nullptr)
{
pl->SelectItem(weapon);
}

((usercmd_t*)cmd)->weaponselect = 0;
}

if (pl->pev->groupinfo != 0)
{
Expand Down
40 changes: 34 additions & 6 deletions dlls/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3047,10 +3047,10 @@ void CBasePlayer::SelectNextItem(int iItem)
}
}

void CBasePlayer::SelectItem(const char* pstr)
CBasePlayerItem* CBasePlayer::GetNamedPlayerItem(const char* pstr)
{
if (!pstr)
return;
return nullptr;

CBasePlayerItem* pItem = NULL;

Expand All @@ -3063,19 +3063,47 @@ void CBasePlayer::SelectItem(const char* pstr)
while (pItem)
{
if (FClassnameIs(pItem->pev, pstr))
break;
return pItem;

pItem = pItem->m_pNext;
}
}
}

if (pItem)
break;
return nullptr;
}

CBasePlayerItem* CBasePlayer::GetPlayerItem(int iId)
{
if (iId <= WEAPON_NONE)
return nullptr;

CBasePlayerItem* pItem = NULL;

for (int i = 0; i < MAX_ITEM_TYPES; i++)
{
if (m_rgpPlayerItems[i])
{
pItem = m_rgpPlayerItems[i];

while (pItem)
{
if (pItem->m_iId == iId)
return pItem;

pItem = pItem->m_pNext;
}
}
}

return nullptr;
}

void CBasePlayer::SelectItem(CBasePlayerItem* pItem)
{
if (!pItem)
return;


if (pItem == m_pActiveItem)
return;

Expand Down
4 changes: 3 additions & 1 deletion dlls/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,9 @@ class CBasePlayer : public CBaseMonster
void SelectPrevItem(int iItem);
void SelectNextItem(int iItem);
void SelectLastItem();
void SelectItem(const char* pstr);
CBasePlayerItem* GetNamedPlayerItem(const char* pstr);
CBasePlayerItem* GetPlayerItem(int iId);
void SelectItem(CBasePlayerItem* pItem);
void ItemPreFrame();
void ItemPostFrame();
void GiveNamedItem(const char* szName);
Expand Down
3 changes: 2 additions & 1 deletion network/delta.lst
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ usercmd_t none
DEFINE_DELTA( impact_index, DT_INTEGER, 6, 1.0 ),
DEFINE_DELTA( impact_position[0], DT_SIGNED | DT_FLOAT, 16, 8.0 ),
DEFINE_DELTA( impact_position[1], DT_SIGNED | DT_FLOAT, 16, 8.0 ),
DEFINE_DELTA( impact_position[2], DT_SIGNED | DT_FLOAT, 16, 8.0 )
DEFINE_DELTA( impact_position[2], DT_SIGNED | DT_FLOAT, 16, 8.0 ),
DEFINE_DELTA( weaponselect, DT_BYTE, 6, 1.0 )
}

weapon_data_t none
Expand Down

0 comments on commit 7709454

Please sign in to comment.