From 73f5d6d473c1e9f821d69bc83235acaa036b038c Mon Sep 17 00:00:00 2001 From: fant1kua Date: Fri, 27 Mar 2020 04:17:41 +0200 Subject: [PATCH] Create weaponbox hook (#521) * Add SpawnWeaponBox hook --- gradle.properties | 2 +- regamedll/dlls/API/CAPI_Impl.cpp | 2 + regamedll/dlls/API/CAPI_Impl.h | 6 + regamedll/dlls/player.cpp | 121 ++++++++++----------- regamedll/dlls/player.h | 3 + regamedll/public/regamedll/regamedll_api.h | 7 +- 6 files changed, 74 insertions(+), 67 deletions(-) diff --git a/gradle.properties b/gradle.properties index e6c302fc..a382be98 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,3 +1,3 @@ majorVersion=5 -minorVersion=14 +minorVersion=15 maintenanceVersion=0 diff --git a/regamedll/dlls/API/CAPI_Impl.cpp b/regamedll/dlls/API/CAPI_Impl.cpp index 8d209b31..388ae83e 100644 --- a/regamedll/dlls/API/CAPI_Impl.cpp +++ b/regamedll/dlls/API/CAPI_Impl.cpp @@ -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; } diff --git a/regamedll/dlls/API/CAPI_Impl.h b/regamedll/dlls/API/CAPI_Impl.h index 2f9020bb..fd6e23f0 100644 --- a/regamedll/dlls/API/CAPI_Impl.h +++ b/regamedll/dlls/API/CAPI_Impl.h @@ -567,6 +567,10 @@ typedef IHookChainRegistryClassImpl CReGameHook_CBasePlayer_DropIdlePlayer; typedef IHookChainRegistryClassImpl CReGameHookRegistry_CBasePlayer_DropIdlePlayer; +// CreateWeaponBox hook +typedef IHookChainImpl CReGameHook_CreateWeaponBox; +typedef IHookChainRegistryImpl CReGameHookRegistry_CreateWeaponBox; + class CReGameHookchains: public IReGameHookchains { public: // CBasePlayer virtual @@ -681,6 +685,7 @@ public: 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(); @@ -794,6 +799,7 @@ public: 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; diff --git a/regamedll/dlls/player.cpp b/regamedll/dlls/player.cpp index 27d6693e..a1fb5f3a 100644 --- a/regamedll/dlls/player.cpp +++ b/regamedll/dlls/player.cpp @@ -1266,6 +1266,41 @@ BOOL EXT_FUNC CBasePlayer::__API_HOOK(TakeDamage)(entvars_t *pevInflictor, entva return bTookDamage; } +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) +{ + // create a box to pack the stuff into. + CWeaponBox *pWeaponBox = (CWeaponBox *)CBaseEntity::Create("weaponbox", origin, angles, ENT(pPlayerOwner->pev)); + + if (pWeaponBox) + { + // don't let weaponbox tilt. + pWeaponBox->pev->angles.x = 0; + pWeaponBox->pev->angles.z = 0; + pWeaponBox->pev->velocity = velocity; + pWeaponBox->SetThink(&CWeaponBox::Kill); + pWeaponBox->pev->nextthink = gpGlobals->time + lifeTime; + pWeaponBox->PackWeapon(pItem); // now pack all of the items in the lists + + // pack the ammo + bool exhaustibleAmmo = (pItem->iFlags() & ITEM_FLAG_EXHAUSTIBLE) == ITEM_FLAG_EXHAUSTIBLE; + if (exhaustibleAmmo || packAmmo) + { + 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) @@ -1274,24 +1309,14 @@ void PackPlayerItem(CBasePlayer *pPlayer, CBasePlayerItem *pItem, bool packAmmo) const char *modelName = GetCSModelName(pItem->m_iId); if (modelName) { - // 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->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); + // 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 + ); } } @@ -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 @@ -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")) { @@ -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; } diff --git a/regamedll/dlls/player.h b/regamedll/dlls/player.h index 3c706091..4af1352e 100644 --- a/regamedll/dlls/player.h +++ b/regamedll/dlls/player.h @@ -887,6 +887,9 @@ public: #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: diff --git a/regamedll/public/regamedll/regamedll_api.h b/regamedll/public/regamedll/regamedll_api.h index b52caa1e..c4ec98d9 100644 --- a/regamedll/public/regamedll/regamedll_api.h +++ b/regamedll/public/regamedll/regamedll_api.h @@ -38,7 +38,7 @@ #include #define REGAMEDLL_API_VERSION_MAJOR 5 -#define REGAMEDLL_API_VERSION_MINOR 14 +#define REGAMEDLL_API_VERSION_MINOR 15 // CBasePlayer::Spawn hook typedef IHookChainClass IReGameHook_CBasePlayer_Spawn; @@ -464,6 +464,10 @@ typedef IHookChainRegistryClass IReGameHook_CBasePlayer_DropIdlePlayer; typedef IHookChainRegistryClass IReGameHookRegistry_CBasePlayer_DropIdlePlayer; +// CreateWeaponBox hook +typedef IHookChain IReGameHook_CreateWeaponBox; +typedef IHookChainRegistry IReGameHookRegistry_CreateWeaponBox; + class IReGameHookchains { public: virtual ~IReGameHookchains() {} @@ -579,6 +583,7 @@ public: 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 {