Skip to content

Commit

Permalink
Create weaponbox hook (#521)
Browse files Browse the repository at this point in the history
* Add SpawnWeaponBox hook
  • Loading branch information
fant1kua authored Mar 27, 2020
1 parent 9461d03 commit 73f5d6d
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 61 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
majorVersion=5
minorVersion=14
minorVersion=15
maintenanceVersion=0
2 changes: 2 additions & 0 deletions regamedll/dlls/API/CAPI_Impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ GAMEHOOK_REGISTRY(CBasePlayerWeapon_DefaultReload);
GAMEHOOK_REGISTRY(CBasePlayerWeapon_DefaultShotgunReload);
GAMEHOOK_REGISTRY(CBasePlayer_DropIdlePlayer);

GAMEHOOK_REGISTRY(CreateWeaponBox);

int CReGameApi::GetMajorVersion() {
return REGAMEDLL_API_VERSION_MAJOR;
}
Expand Down
6 changes: 6 additions & 0 deletions regamedll/dlls/API/CAPI_Impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,10 @@ typedef IHookChainRegistryClassImpl<bool, CBasePlayerWeapon, int, int, float, fl
typedef IHookChainClassImpl<void, CBasePlayer, const char *> CReGameHook_CBasePlayer_DropIdlePlayer;
typedef IHookChainRegistryClassImpl<void, CBasePlayer, const char *> CReGameHookRegistry_CBasePlayer_DropIdlePlayer;

// CreateWeaponBox hook
typedef IHookChainImpl<CWeaponBox *, CBasePlayerItem *, CBasePlayer *, const char *, const Vector &, const Vector &, const Vector &, float, bool> CReGameHook_CreateWeaponBox;
typedef IHookChainRegistryImpl<CWeaponBox *, CBasePlayerItem *, CBasePlayer *, const char *, const Vector &, const Vector &, const Vector &, float, bool> CReGameHookRegistry_CreateWeaponBox;

class CReGameHookchains: public IReGameHookchains {
public:
// CBasePlayer virtual
Expand Down Expand Up @@ -681,6 +685,7 @@ class CReGameHookchains: public IReGameHookchains {
CReGameHookRegistry_CBasePlayerWeapon_DefaultReload m_CBasePlayerWeapon_DefaultReload;
CReGameHookRegistry_CBasePlayerWeapon_DefaultShotgunReload m_CBasePlayerWeapon_DefaultShotgunReload;
CReGameHookRegistry_CBasePlayer_DropIdlePlayer m_CBasePlayer_DropIdlePlayer;
CReGameHookRegistry_CreateWeaponBox m_CreateWeaponBox;

public:
virtual IReGameHookRegistry_CBasePlayer_Spawn *CBasePlayer_Spawn();
Expand Down Expand Up @@ -794,6 +799,7 @@ class CReGameHookchains: public IReGameHookchains {
virtual IReGameHookRegistry_CBasePlayerWeapon_DefaultReload *CBasePlayerWeapon_DefaultReload();
virtual IReGameHookRegistry_CBasePlayerWeapon_DefaultShotgunReload *CBasePlayerWeapon_DefaultShotgunReload();
virtual IReGameHookRegistry_CBasePlayer_DropIdlePlayer *CBasePlayer_DropIdlePlayer();
virtual IReGameHookRegistry_CreateWeaponBox *CreateWeaponBox();
};

extern CReGameHookchains g_ReGameHookchains;
Expand Down
109 changes: 50 additions & 59 deletions regamedll/dlls/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1266,33 +1266,58 @@ BOOL EXT_FUNC CBasePlayer::__API_HOOK(TakeDamage)(entvars_t *pevInflictor, entva
return bTookDamage;
}

void PackPlayerItem(CBasePlayer *pPlayer, CBasePlayerItem *pItem, bool packAmmo)
LINK_HOOK_CHAIN(CWeaponBox *, CreateWeaponBox, (CBasePlayerItem *pItem, CBasePlayer *pPlayerOwner, const char *modelName, const Vector &origin, const Vector &angles, const Vector &velocity, float lifeTime, bool packAmmo), pItem, pPlayerOwner, modelName, origin, angles, velocity, lifeTime, packAmmo)

CWeaponBox *EXT_FUNC __API_HOOK(CreateWeaponBox)(CBasePlayerItem *pItem, CBasePlayer *pPlayerOwner, const char *modelName, const Vector &origin, const Vector &angles, const Vector &velocity, float lifeTime, bool packAmmo)
{
if (!pItem)
return;
// create a box to pack the stuff into.
CWeaponBox *pWeaponBox = (CWeaponBox *)CBaseEntity::Create("weaponbox", origin, angles, ENT(pPlayerOwner->pev));

const char *modelName = GetCSModelName(pItem->m_iId);
if (modelName)
if (pWeaponBox)
{
// create a box to pack the stuff into.
CWeaponBox *pWeaponBox = (CWeaponBox *)CBaseEntity::Create("weaponbox", pPlayer->pev->origin, pPlayer->pev->angles, ENT(pPlayer->pev));

// don't let weaponbox tilt.
pWeaponBox->pev->angles.x = 0;
pWeaponBox->pev->angles.z = 0;
pWeaponBox->pev->velocity = pPlayer->pev->velocity * 0.75f;
pWeaponBox->pev->velocity = velocity;
pWeaponBox->SetThink(&CWeaponBox::Kill);
pWeaponBox->pev->nextthink = gpGlobals->time + CGameRules::GetItemKillDelay();
pWeaponBox->pev->nextthink = gpGlobals->time + lifeTime;
pWeaponBox->PackWeapon(pItem); // now pack all of the items in the lists

// pack the ammo
if (packAmmo)
bool exhaustibleAmmo = (pItem->iFlags() & ITEM_FLAG_EXHAUSTIBLE) == ITEM_FLAG_EXHAUSTIBLE;
if (exhaustibleAmmo || packAmmo)
{
pWeaponBox->PackAmmo(MAKE_STRING(pItem->pszAmmo1()), pPlayer->m_rgAmmo[pItem->PrimaryAmmoIndex()]);
pWeaponBox->PackAmmo(MAKE_STRING(pItem->pszAmmo1()), pPlayerOwner->m_rgAmmo[pItem->PrimaryAmmoIndex()]);

if (exhaustibleAmmo)
{
pPlayerOwner->m_rgAmmo[pItem->PrimaryAmmoIndex()] = 0;
}
}

pWeaponBox->SetModel(modelName);
}

return pWeaponBox;
}

void PackPlayerItem(CBasePlayer *pPlayer, CBasePlayerItem *pItem, bool packAmmo)
{
if (!pItem)
return;

const char *modelName = GetCSModelName(pItem->m_iId);
if (modelName)
{
// create a box to pack the stuff into
CreateWeaponBox(pItem, pPlayer,
modelName,
pPlayer->pev->origin,
pPlayer->pev->angles,
pPlayer->pev->velocity * 0.75f,
CGameRules::GetItemKillDelay(), packAmmo
);
}
}

#ifdef REGAMEDLL_ADD
Expand Down Expand Up @@ -1329,25 +1354,13 @@ void PackPlayerNade(CBasePlayer *pPlayer, CBasePlayerItem *pItem, bool packAmmo)
vecAngles.y += 45.0f;

// create a box to pack the stuff into.
CWeaponBox *pWeaponBox = (CWeaponBox *)CBaseEntity::Create("weaponbox", pPlayer->pev->origin + dir, vecAngles, ENT(pPlayer->pev));

// don't let weaponbox tilt.
pWeaponBox->pev->angles.x = 0;
pWeaponBox->pev->angles.z = 0;

pWeaponBox->pev->velocity = pPlayer->pev->velocity * 0.75f;

pWeaponBox->SetThink(&CWeaponBox::Kill);
pWeaponBox->pev->nextthink = gpGlobals->time + CGameRules::GetItemKillDelay();
pWeaponBox->PackWeapon(pItem); // now pack all of the items in the lists

// pack the ammo
if (packAmmo)
{
pWeaponBox->PackAmmo(MAKE_STRING(pItem->pszAmmo1()), pPlayer->m_rgAmmo[pItem->PrimaryAmmoIndex()]);
}

pWeaponBox->SetModel(modelName);
CreateWeaponBox(pItem, pPlayer,
modelName,
pPlayer->pev->origin + dir,
vecAngles,
pPlayer->pev->velocity * 0.75f,
CGameRules::GetItemKillDelay(),
packAmmo);
}
}
#endif
Expand Down Expand Up @@ -7838,13 +7851,12 @@ CBaseEntity *EXT_FUNC CBasePlayer::__API_HOOK(DropPlayerItem)(const char *pszIte
}
}

CWeaponBox *pWeaponBox = (CWeaponBox *)CBaseEntity::Create("weaponbox", pev->origin + gpGlobals->v_forward * 10, pev->angles, edict());
pWeaponBox->pev->angles.x = 0;
pWeaponBox->pev->angles.z = 0;
pWeaponBox->SetThink(&CWeaponBox::Kill);
pWeaponBox->pev->nextthink = gpGlobals->time + CGameRules::GetItemKillDelay();
pWeaponBox->PackWeapon(pWeapon);
pWeaponBox->pev->velocity = gpGlobals->v_forward * 300 + gpGlobals->v_forward * 100;
const char *modelname = GetCSModelName(pWeapon->m_iId);
CWeaponBox *pWeaponBox = CreateWeaponBox(pWeapon, this, modelname, pev->origin + gpGlobals->v_forward * 10, pev->angles, gpGlobals->v_forward * 300 + gpGlobals->v_forward * 100, CGameRules::GetItemKillDelay(), false);
if (!pWeaponBox)
{
return nullptr;
}

if (FClassnameIs(pWeapon->pev, "weapon_c4"))
{
Expand All @@ -7860,27 +7872,6 @@ CBaseEntity *EXT_FUNC CBasePlayer::__API_HOOK(DropPlayerItem)(const char *pszIte
}
}

if (pWeapon->iFlags() & ITEM_FLAG_EXHAUSTIBLE)
{
int iAmmoIndex = GetAmmoIndex(pWeapon->pszAmmo1());
if (iAmmoIndex != -1)
{
#ifdef REGAMEDLL_FIXES
// why not pack the ammo more than one?
pWeaponBox->PackAmmo(MAKE_STRING(pWeapon->pszAmmo1()), m_rgAmmo[iAmmoIndex]);
#else
pWeaponBox->PackAmmo(MAKE_STRING(pWeapon->pszAmmo1()), m_rgAmmo[iAmmoIndex] > 0);
#endif
m_rgAmmo[iAmmoIndex] = 0;
}
}

const char *modelname = GetCSModelName(pWeapon->m_iId);
if (modelname)
{
pWeaponBox->SetModel(modelname);
}

return pWeaponBox;
}

Expand Down
3 changes: 3 additions & 0 deletions regamedll/dlls/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,9 @@ class CBasePlayer: public CBaseMonster {
#endif
};

CWeaponBox *CreateWeaponBox(CBasePlayerItem *pItem, CBasePlayer *pPlayerOwner, const char *modelName, const Vector &origin, const Vector &angles, const Vector &velocity, float lifeTime, bool packAmmo);
CWeaponBox *CreateWeaponBox_OrigFunc(CBasePlayerItem *pItem, CBasePlayer *pPlayerOwner, const char *modelName, const Vector &origin, const Vector &angles, const Vector &velocity, float lifeTime, bool packAmmo);

class CWShield: public CBaseEntity
{
public:
Expand Down
7 changes: 6 additions & 1 deletion regamedll/public/regamedll/regamedll_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#include <API/CSInterfaces.h>

#define REGAMEDLL_API_VERSION_MAJOR 5
#define REGAMEDLL_API_VERSION_MINOR 14
#define REGAMEDLL_API_VERSION_MINOR 15

// CBasePlayer::Spawn hook
typedef IHookChainClass<void, class CBasePlayer> IReGameHook_CBasePlayer_Spawn;
Expand Down Expand Up @@ -464,6 +464,10 @@ typedef IHookChainRegistryClass<bool, class CBasePlayerWeapon, int, int, float,
typedef IHookChainClass<void, CBasePlayer, const char *> IReGameHook_CBasePlayer_DropIdlePlayer;
typedef IHookChainRegistryClass<void, CBasePlayer, const char *> IReGameHookRegistry_CBasePlayer_DropIdlePlayer;

// CreateWeaponBox hook
typedef IHookChain<class CWeaponBox *, class CBasePlayerItem *, class CBasePlayer *, const char *, const Vector &, const Vector &, const Vector &, float, bool> IReGameHook_CreateWeaponBox;
typedef IHookChainRegistry<class CWeaponBox *, class CBasePlayerItem *, class CBasePlayer *, const char *, const Vector &, const Vector &, const Vector &, float, bool> IReGameHookRegistry_CreateWeaponBox;

class IReGameHookchains {
public:
virtual ~IReGameHookchains() {}
Expand Down Expand Up @@ -579,6 +583,7 @@ class IReGameHookchains {
virtual IReGameHookRegistry_CBasePlayerWeapon_DefaultReload *CBasePlayerWeapon_DefaultReload() = 0;
virtual IReGameHookRegistry_CBasePlayerWeapon_DefaultShotgunReload *CBasePlayerWeapon_DefaultShotgunReload() = 0;
virtual IReGameHookRegistry_CBasePlayer_DropIdlePlayer *CBasePlayer_DropIdlePlayer() = 0;
virtual IReGameHookRegistry_CreateWeaponBox *CreateWeaponBox() = 0;
};

struct ReGameFuncs_t {
Expand Down

0 comments on commit 73f5d6d

Please sign in to comment.