Skip to content

Commit f0d4727

Browse files
committed
weapon refactor : client prediction (ugly but works!)
1 parent 1d0d14f commit f0d4727

18 files changed

+249
-10
lines changed

cl_dll/cs_wpn/bte_weapons.cpp

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
2+
#include "extdll.h"
3+
#include "util.h"
4+
#include "cbase.h"
5+
#include "player.h"
6+
7+
#include "com_weapons.h"
8+
9+
#include "parsemsg.h"
10+
11+
#include "bte_weapons.h"
12+
#include "weapons_msg.h"
13+
14+
#include "wpn_shared.h"
15+
16+
#include <string>
17+
#include <map>
18+
19+
/*
20+
Weapon Registers
21+
*/
22+
template<class T>
23+
CBasePlayerWeapon *WeaponEntityPlaceHolderFactory() // Static
24+
{
25+
static T w;
26+
static entvars_t ev;
27+
28+
CBasePlayerWeapon *pEntity = &w;
29+
pEntity->pev = &ev;
30+
pEntity->Precache();
31+
pEntity->Spawn();
32+
33+
return pEntity;
34+
}
35+
36+
const std::map<std::string, CBasePlayerWeapon *(*)()> g_WeaponEntityFindList = {
37+
{ "weapon_ak47l", WeaponEntityPlaceHolderFactory<CAK47_Long> },
38+
{ "weapon_deagled", WeaponEntityPlaceHolderFactory<CDeagleD> }
39+
};
40+
41+
/*
42+
Entity Pools
43+
*/
44+
extern CBasePlayerWeapon *g_pWpns[MAX_WEAPONS]; // cs_weapons.cpp
45+
46+
/*
47+
CBTEClientWeapons impls
48+
*/
49+
50+
void CBTEClientWeapons::PrepEntity(CBasePlayer *pWeaponOwner)
51+
{
52+
for (auto &kv : g_WeaponEntityFindList)
53+
{
54+
CBasePlayerWeapon *pEntity = kv.second();
55+
56+
if (pWeaponOwner)
57+
{
58+
((CBasePlayerWeapon *)pEntity)->m_pPlayer = pWeaponOwner;
59+
}
60+
}
61+
62+
}
63+
64+
void CBTEClientWeapons::ActiveWeapon(const char *name)
65+
{
66+
m_pActiveWeapon = nullptr;
67+
68+
auto iter = g_WeaponEntityFindList.find({ name });
69+
if (iter != g_WeaponEntityFindList.end())
70+
{
71+
m_pActiveWeapon = iter->second();
72+
73+
ItemInfo info;
74+
memset(&info, 0, sizeof(ItemInfo));
75+
m_pActiveWeapon->GetItemInfo(&info);
76+
g_pWpns[info.iId] = m_pActiveWeapon;
77+
}
78+
79+
}
80+
81+
/*
82+
Message Handlers
83+
*/
84+
85+
typedef void MsgDispatchFunction_t(BufferReader &reader);
86+
87+
void MsgDispatch_Active(BufferReader &reader)
88+
{
89+
const char *name = reader.ReadString();
90+
BTEClientWeapons().ActiveWeapon(name);
91+
}
92+
93+
MsgDispatchFunction_t *gMsgDispatchFunctions[BTE_Weapon_MaxMsgs] = {
94+
MsgDispatch_Active
95+
};
96+
97+
int __MsgFunc_BTEWeapon(const char *pszName, int iSize, void *pbuf)
98+
{
99+
BufferReader reader(pszName, pbuf, iSize);
100+
101+
BTEWeaponMsgType type = static_cast<BTEWeaponMsgType>(reader.ReadByte());
102+
gMsgDispatchFunctions[type](reader);
103+
return 1;
104+
}
105+
106+
107+
void CBTEClientWeapons::Init()
108+
{
109+
gEngfuncs.pfnHookUserMsg("BTEWeapon", __MsgFunc_BTEWeapon);
110+
}
111+
112+
CBTEClientWeapons::CBTEClientWeapons() : m_pActiveWeapon(nullptr)
113+
{
114+
115+
}
116+
117+
CBTEClientWeapons &BTEClientWeapons()
118+
{
119+
static CBTEClientWeapons x;
120+
return x;
121+
}

cl_dll/cs_wpn/bte_weapons.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
3+
class CBasePlayer;
4+
class CBasePlayerWeapon;
5+
6+
class CBTEClientWeapons
7+
{
8+
private:
9+
CBTEClientWeapons();
10+
11+
public:
12+
void Init();
13+
14+
void PrepEntity(CBasePlayer *pWeaponOwner);
15+
void ActiveWeapon(const char *name);
16+
CBasePlayerWeapon *GetActiveWeaponEntity()
17+
{
18+
return m_pActiveWeapon;
19+
}
20+
21+
private:
22+
CBasePlayerWeapon *m_pActiveWeapon;
23+
24+
public:
25+
// singleton accessor
26+
friend CBTEClientWeapons &BTEClientWeapons();
27+
};

cl_dll/cs_wpn/cs_weapons.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ extern "C"
4747

4848
#include "wpn_shared.h"
4949

50+
#include "bte_weapons.h"
51+
5052
#include "minmax.h"
5153

5254
extern globalvars_t *gpGlobals;
@@ -67,7 +69,8 @@ static CBasePlayer player;
6769
// Local version of game .dll global variables ( time, etc. )
6870
static globalvars_t Globals = { };
6971

70-
static CBasePlayerWeapon *g_pWpns[ MAX_WEAPONS ];
72+
// ref from bte_weapons.cpp
73+
CBasePlayerWeapon *g_pWpns[ MAX_WEAPONS ];
7174

7275

7376
// CS Weapon placeholder entities
@@ -923,6 +926,8 @@ void HUD_InitClientWeapons( void )
923926
HUD_PrepEntity( &g_AK47, &player);
924927
HUD_PrepEntity( &g_Knife, &player);
925928
HUD_PrepEntity( &g_P90, &player );
929+
930+
BTEClientWeapons().PrepEntity(&player);
926931
}
927932

928933

@@ -1159,6 +1164,11 @@ void HUD_WeaponsPostThink( local_state_s *from, local_state_s *to, usercmd_t *cm
11591164
break;*/
11601165
}
11611166

1167+
// if we have BTE weapon entity, use it.
1168+
CBasePlayerWeapon *pActiveBTEWeapon = BTEClientWeapons().GetActiveWeaponEntity();
1169+
if (pActiveBTEWeapon)
1170+
pWeapon = pActiveBTEWeapon;
1171+
11621172
// Store pointer to our destination entity_state_t so we can get our origin, etc. from it
11631173
// for setting up events on the client
11641174
g_finalstate = to;
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,7 @@
2828
#include "wpn_shared/wpn_ump45.h"
2929
#include "wpn_shared/wpn_sg550.h"
3030
#include "wpn_shared/wpn_galil.h"
31-
#include "wpn_shared/wpn_famas.h"
31+
#include "wpn_shared/wpn_famas.h"
32+
33+
#include "wpn_shared/wpn_ak47l.h"
34+
#include "wpn_shared/wpn_deagled.h"

cl_dll/hud/hud.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
#include "camera.h"
4040

41+
#include "cs_wpn/bte_weapons.h"
4142

4243
extern client_sprite_t *GetSpriteList(client_sprite_t *pList, const char *psz, int iRes, int iCount);
4344

@@ -264,6 +265,8 @@ void CHud :: Init( void )
264265

265266
InitRain();
266267

268+
BTEClientWeapons().Init();
269+
267270
//ServersInit();
268271

269272
gEngfuncs.Cvar_SetValue( "hand", 1 );

dlls/player.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,6 +1269,7 @@ void CBasePlayer::GiveDefaultItems()
12691269
break;
12701270
}
12711271
GiveNamedItem("weapon_ak47l");
1272+
GiveNamedItem("weapon_deagled");
12721273
}
12731274
}
12741275

dlls/player/player_msg.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ int gmsgShowTimer = 0;
9090

9191
int gmsgZBSTip = 0;
9292
int gmsgZBSLevel = 0;
93+
int gmsgBTEWeapon = 0;
9394

9495
// utils
9596
inline int FNullEnt(CBaseEntity *ent) { return (!ent) || FNullEnt(ent->edict()); }
@@ -184,6 +185,7 @@ void LinkUserMessages()
184185

185186
gmsgZBSTip = REG_USER_MSG("ZBSTip", -1);
186187
gmsgZBSLevel = REG_USER_MSG("ZBSLevel", -1);
188+
gmsgBTEWeapon = REG_USER_MSG("BTEWeapon", -1);
187189
}
188190

189191
/*void WriteWeaponInfo(const ItemInfo &II)

dlls/player/player_msg.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ extern int gmsgShowTimer;
8888

8989
extern int gmsgZBSTip;
9090
extern int gmsgZBSLevel;
91+
extern int gmsgBTEWeapon;
9192

9293
void LinkUserMessages();
9394
void WriteSigonMessages();

dlls/weapons.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,7 +1208,10 @@ int CBasePlayerWeapon::UpdateClientData(CBasePlayer *pPlayer)
12081208
if (this == pPlayer->m_pActiveItem || this == pPlayer->m_pClientActiveItem)
12091209
{
12101210
if (pPlayer->m_pActiveItem != pPlayer->m_pClientActiveItem)
1211+
{
12111212
bSend = TRUE;
1213+
UpdateItemInfo();
1214+
}
12121215
}
12131216

12141217
if (m_iClip != m_iClientClip || state != m_iClientWeaponState || pPlayer->m_iFOV != pPlayer->m_iClientFOV)

dlls/weapons.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "weapons_const.h"
3636
#include "weapons_buy.h"
3737
#include "weapons_data.h"
38+
#include "weapons_msg.h"
3839
#include "player/player_knockback.h"
3940

4041
class CBasePlayer;
@@ -355,7 +356,11 @@ class CBasePlayerWeapon: public CBasePlayerItem
355356
virtual BOOL CanDeploy();
356357
virtual BOOL IsWeapon() { return TRUE; }
357358
virtual void Holster(int skiplocal = 0);
359+
#ifdef CLIENT_DLL
358360
virtual void UpdateItemInfo() {};
361+
#else
362+
virtual void UpdateItemInfo();
363+
#endif
359364
virtual void ItemPostFrame();
360365
#ifdef CLIENT_DLL
361366
int PrimaryAmmoIndex(void) { return -1; }

0 commit comments

Comments
 (0)