Skip to content

Commit

Permalink
server: initially implemented server-side logic for weapon predicting
Browse files Browse the repository at this point in the history
  • Loading branch information
SNMetamorph committed Jan 19, 2025
1 parent 1756a89 commit 925f748
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 27 deletions.
132 changes: 107 additions & 25 deletions server/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "netadr.h"
#include "user_messages.h"
#include "beam.h"
#include <algorithm>
#include <locale>

extern DLL_GLOBAL ULONG g_ulModelIndexPlayer;
Expand Down Expand Up @@ -1600,8 +1601,47 @@ void RegisterEncoders( void )

int GetWeaponData( struct edict_s *player, struct weapon_data_s *info )
{
memset( info, 0, 32 * sizeof( weapon_data_t ) );
entvars_t *pev = &player->v;
CBasePlayer *playerEntity = dynamic_cast<CBasePlayer*>(CBasePlayer::Instance(&player->v));

memset(info, 0, MAX_LOCAL_WEAPONS * sizeof(weapon_data_t));

if (!player)
return 1;

for (int i = 0; i < MAX_ITEM_TYPES; i++)
{
CBasePlayerItem *item = playerEntity->m_rgpPlayerItems[i];
if (item)
{
while (item)
{
CBasePlayerWeapon *weapon = dynamic_cast<CBasePlayerWeapon*>(item->GetWeaponPtr());
CBaseWeaponContext *ctx = weapon->m_pWeaponContext;
if (weapon && ctx->UseDecrement())
{
ItemInfo itemInfo;
memset(&itemInfo, 0, sizeof(itemInfo));
weapon->GetItemInfo(&itemInfo);

if (itemInfo.iId >= 0 && itemInfo.iId < MAX_LOCAL_WEAPONS)
{
weapon_data_t *data = &info[itemInfo.iId];

data->m_iId = itemInfo.iId;
data->m_iClip = ctx->m_iClip;
data->m_flTimeWeaponIdle = std::max(ctx->m_flTimeWeaponIdle, -0.001f);
data->m_flNextPrimaryAttack = std::max(ctx->m_flNextPrimaryAttack, -0.001f);
data->m_flNextSecondaryAttack = std::max(ctx->m_flNextSecondaryAttack, -0.001f);
data->m_fInReload = ctx->m_fInReload;
data->m_fInSpecialReload = ctx->m_fInSpecialReload;
data->fuser1 = std::max(weapon->pev->fuser1, -0.001f);
}
}
item = item->m_pNext;
}
}
}
return 1;
}

Expand All @@ -1615,34 +1655,76 @@ engine sets cd to 0 before calling.
*/
void UpdateClientData ( const struct edict_s *ent, int sendweapons, struct clientdata_s *cd )
{
cd->flags = ent->v.flags;
cd->health = ent->v.health;
if (!ent || !ent->pvPrivateData)
return;

cd->viewmodel = MODEL_INDEX( STRING( ent->v.viewmodel ) );
entvars_t *pev = const_cast<entvars_t *>(&ent->v);
CBasePlayer *player = dynamic_cast<CBasePlayer *>(CBasePlayer::Instance(pev));

cd->waterlevel = ent->v.waterlevel;
cd->watertype = ent->v.watertype;
cd->weapons = 0; // not used
cd->flags = pev->flags;
cd->health = pev->health;
cd->viewmodel = MODEL_INDEX(STRING(pev->viewmodel));
cd->waterlevel = pev->waterlevel;
cd->watertype = pev->watertype;
cd->weapons = 0; // not used

// Vectors
cd->origin = ent->v.origin;
cd->velocity = ent->v.velocity;
cd->view_ofs = ent->v.view_ofs;
cd->punchangle = ent->v.punchangle;

cd->bInDuck = ent->v.bInDuck;
cd->flTimeStepSound = ent->v.flTimeStepSound;
cd->flDuckTime = ent->v.flDuckTime;
cd->flSwimTime = ent->v.flSwimTime;
cd->waterjumptime = ent->v.teleport_time;

strcpy( cd->physinfo, ENGINE_GETPHYSINFO( ent ) );

cd->maxspeed = ent->v.maxspeed;
cd->fov = ent->v.fov;
cd->weaponanim = ent->v.weaponanim;

cd->pushmsec = ent->v.pushmsec;
cd->origin = pev->origin;
cd->velocity = pev->velocity;
cd->view_ofs = pev->view_ofs;
cd->punchangle = pev->punchangle;

cd->bInDuck = pev->bInDuck;
cd->flTimeStepSound = pev->flTimeStepSound;
cd->flDuckTime = pev->flDuckTime;
cd->flSwimTime = pev->flSwimTime;
cd->waterjumptime = pev->teleport_time;

strncpy(cd->physinfo, ENGINE_GETPHYSINFO(ent), sizeof(cd->physinfo));

cd->maxspeed = pev->maxspeed;
cd->fov = pev->fov;
cd->weaponanim = pev->weaponanim;
cd->pushmsec = pev->pushmsec;

if (sendweapons && player)
{
cd->m_flNextAttack = player->m_flNextAttack;
//cd->fuser2 = player->m_flNextAmmoBurn;
//cd->fuser3 = player->m_flAmmoStartCharge;
//cd->vuser1.x = player->ammo_9mm;
//cd->vuser1.y = player->ammo_357;
//cd->vuser1.z = player->ammo_argrens;
//cd->ammo_nails = player->ammo_bolts;
//cd->ammo_shells = player->ammo_buckshot;
//cd->ammo_rockets = player->ammo_rockets;
//cd->ammo_cells = player->ammo_uranium;
//cd->vuser2.x = player->ammo_hornets;

CBasePlayerItem *item = player->m_pActiveItem;
if (item)
{
CBasePlayerWeapon *weapon = static_cast<CBasePlayerWeapon*>(item->GetWeaponPtr());
if (weapon && weapon->m_pWeaponContext->UseDecrement())
{
ItemInfo itemInfo;
memset(&itemInfo, 0, sizeof(itemInfo));
weapon->GetItemInfo(&itemInfo);

cd->m_iId = itemInfo.iId;
//cd->vuser3.z = weapon->m_iSecondaryAmmoType;
//cd->vuser4.x = weapon->m_iPrimaryAmmoType;
//cd->vuser4.y = player->m_rgAmmo[weapon->m_iPrimaryAmmoType];
//cd->vuser4.z = player->m_rgAmmo[weapon->m_iSecondaryAmmoType];

//if (weapon->iWeaponID() == WEAPON_RPG)
//{
// cd->vuser2.y = ((CRpg *)item)->m_fSpotActive;
// cd->vuser2.z = ((CRpg *)item)->m_cActiveRockets;
//}
}
}
}
}

/*
Expand Down
75 changes: 73 additions & 2 deletions server/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "hltv.h"
#include "user_messages.h"
#include "ropes/CRope.h"
#include <algorithm>

// #define DUCKFIX

Expand Down Expand Up @@ -2584,6 +2585,69 @@ void CBasePlayer :: UpdatePlayerSound ( void )
m_iWeaponFlash = 0;
}


void CBasePlayer::UpdatePlayerTimers()
{
m_flNextAttack -= gpGlobals->frametime;
if ( m_flNextAttack < -0.001 )
m_flNextAttack = -0.001;

//if ( m_flNextAmmoBurn != 1000 )
//{
// m_flNextAmmoBurn -= gpGlobals->frametime;
//
// if ( m_flNextAmmoBurn < -0.001 )
// m_flNextAmmoBurn = -0.001;
//}

//if ( m_flAmmoStartCharge != 1000 )
//{
// m_flAmmoStartCharge -= gpGlobals->frametime;
//
// if ( m_flAmmoStartCharge < -0.001 )
// m_flAmmoStartCharge = -0.001;
//}
}

void CBasePlayer::UpdateWeaponTimers()
{
for (int i = 0; i < MAX_ITEM_TYPES; i++)
{
CBasePlayerItem *pPlayerItem = m_rgpPlayerItems[i];
if (pPlayerItem)
{
while (pPlayerItem)
{
CBasePlayerWeapon *gun = (CBasePlayerWeapon *)pPlayerItem->GetWeaponPtr();
CBaseWeaponContext *ctx = gun->m_pWeaponContext;

if (gun && ctx->UseDecrement())
{
ctx->m_flNextPrimaryAttack = std::max(ctx->m_flNextPrimaryAttack - gpGlobals->frametime, -1.0f);
ctx->m_flNextSecondaryAttack = std::max(ctx->m_flNextSecondaryAttack - gpGlobals->frametime, -0.001f);

if (ctx->m_flTimeWeaponIdle != 1000)
{
ctx->m_flTimeWeaponIdle = std::max(ctx->m_flTimeWeaponIdle - gpGlobals->frametime, -0.001f);
}

if (gun->pev->fuser1 != 1000)
{
gun->pev->fuser1 = std::max(gun->pev->fuser1 - gpGlobals->frametime, -0.001f);
}

// Only decrement if not flagged as NO_DECREMENT
// if ( gun->m_flPumpTime != 1000 )
// {
// gun->m_flPumpTime = max( gun->m_flPumpTime - gpGlobals->frametime, -0.001f );
// }
}
pPlayerItem = pPlayerItem->m_pNext;
}
}
}
}

void CBasePlayer::PostThink()
{
if ( g_fGameOver )
Expand Down Expand Up @@ -2697,6 +2761,8 @@ void CBasePlayer::PostThink()
CheckPowerups(pev);

UpdatePlayerSound();
UpdatePlayerTimers();
UpdateWeaponTimers();

// Track button info so we can detect 'pressed' and 'released' buttons next frame
m_afButtonLast = pev->button;
Expand Down Expand Up @@ -3249,6 +3315,11 @@ int CBasePlayer::Restore( CRestore &restore )

RenewItems();

// HACK: This variable is saved/restored in CBaseMonster as a time variable, but we're using it
// as just a counter. Ideally, this needs its own variable that's saved as a plain float.
// Barring that, we clear it out here instead of using the incorrect restored time value.
m_flNextAttack = 0.0f;

return status;
}

Expand Down Expand Up @@ -4030,7 +4101,7 @@ Called every frame by the player PreThink
*/
void CBasePlayer::ItemPreFrame()
{
if ( gpGlobals->time < m_flNextAttack )
if ( m_flNextAttack > 0.0f )
{
return;
}
Expand Down Expand Up @@ -4059,7 +4130,7 @@ void CBasePlayer::ItemPostFrame()

ImpulseCommands();

if ( gpGlobals->time < m_flNextAttack )
if ( m_flNextAttack > 0.0f )
return;

if( FBitSet( m_iHideHUD, HIDEHUD_WEAPONS ))
Expand Down
2 changes: 2 additions & 0 deletions server/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ class CBasePlayer : public CBaseMonster
void FlashlightTurnOff( void );

void UpdatePlayerSound ( void );
void UpdatePlayerTimers();
void UpdateWeaponTimers();
void DeathSound ( void );

void TransferReset( void );
Expand Down

0 comments on commit 925f748

Please sign in to comment.