Skip to content

Commit

Permalink
Optimized hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
ampreeT committed Sep 14, 2024
1 parent c380538 commit 0268058
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 19 deletions.
2 changes: 1 addition & 1 deletion scripting/include/srccoop/blackmesa/entitypatch.inc
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ public MRESReturn Hook_WeaponSetModel(int _this, DHookParam hParams)
{ "models/weapons/w_rpg_mp.mdl", "models/weapons/w_rpg.mdl" },
};

char szModelName[MAX_CLASSNAME];
char szModelName[MAX_MODELNAME];
DHookGetParamString(hParams, 1, szModelName, sizeof(szModelName));

for (int i = 0; i < sizeof(szWeaponModel); i++)
Expand Down
91 changes: 75 additions & 16 deletions scripting/include/srccoop/blackmesa/idle_anims.inc
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,93 @@ enum struct IdleAnimEntry

public IdleAnimEntry g_pIdle[MAXPLAYERS + 1];

public Action IdleAnims_SetTransmit(const int iEntity, const int iClient)
public MRESReturn IdleAnims_Hook_PlayerSetModel(const int iPlayerIndex, DHookParam hParams)
{
if (!DHookIsNullParam(hParams, 1) && g_pIdle[iPlayerIndex].m_pModel.IsValid())
{
char szModelName[MAX_MODELNAME];
DHookGetParamString(hParams, 1, szModelName, sizeof(szModelName));
if (PrecacheModel(szModelName))
{
g_pIdle[iPlayerIndex].m_pModel.SetModel(szModelName);
if (g_pIdle[iPlayerIndex].m_eIdleType == IdleNoWeapon)
{
g_pIdle[iPlayerIndex].m_pModel.SetAnimation("idle_noweapon");
}
else if (g_pIdle[iPlayerIndex].m_eIdleType == SwimIdleNoWeapon)
{
g_pIdle[iPlayerIndex].m_pModel.SetAnimation("swimidle_noweapon");
}
else if (g_pIdle[iPlayerIndex].m_eIdleType == SwimNoWeapon)
{
g_pIdle[iPlayerIndex].m_pModel.SetAnimation("swim_noweapon");
}
else if (g_pIdle[iPlayerIndex].m_eIdleType == RunNoWeapon)
{
g_pIdle[iPlayerIndex].m_pModel.SetAnimation("run_noweapon");
}
else if (g_pIdle[iPlayerIndex].m_eIdleType == LadderNoWeapon)
{
g_pIdle[iPlayerIndex].m_pModel.SetAnimation("ladder_noweapon");
}
else if (g_pIdle[iPlayerIndex].m_eIdleType == JumpStartNoWeapon)
{
g_pIdle[iPlayerIndex].m_pModel.SetAnimation("jump_start_noweapon");
}
else if (g_pIdle[iPlayerIndex].m_eIdleType == CIdleNoWeapon)
{
g_pIdle[iPlayerIndex].m_pModel.SetAnimation("cidle_noweapon");
}
else if (g_pIdle[iPlayerIndex].m_eIdleType == CWalkNoWeapon)
{
g_pIdle[iPlayerIndex].m_pModel.SetAnimation("cwalk_noweapon");
}
}
}
return MRES_Ignored;
}

public Action IdleAnims_Hook_SetTransmit(const int iEntity, const int iClient)
{
CDynamicProp pDynamicProp = CDynamicProp(iEntity);
CBasePlayer pPlayer = CBasePlayer(iClient);
CBasePlayer pParentPlayer = view_as<CBasePlayer>(pDynamicProp.GetParent());
if (pParentPlayer == pPlayer)
{
// TODO:
// Thirdperson support
if (pParentPlayer == NULL_CBASEENTITY)
return Plugin_Handled;

Obs_Mode iObserverMode = pPlayer.GetObserverMode();

if (iObserverMode != OBS_MODE_NONE)
{
if (iObserverMode == OBS_MODE_IN_EYE)
{
return Plugin_Handled;
}
}
if (pParentPlayer.IsValid() && pPlayer.GetObserverTarget() == pParentPlayer && pPlayer.GetObserverMode() == OBS_MODE_IN_EYE)
else if (pPlayer == pParentPlayer)
{
// TODO:
// Add thirdperson support
return Plugin_Handled;
}

return Plugin_Continue;
}

public void IdleAnims_Player_PreThink(const int iClient)
public void IdleAnims_Hook_PlayerPreThinkPost(const int iClient)
{
CBasePlayer pPlayer = CBasePlayer(iClient);
int iPlayerIndex = pPlayer.GetEntIndex();
if (pPlayer.IsAlive() && !pPlayer.GetActiveWeapon().IsValid())
bool bHasModel = g_pIdle[iPlayerIndex].m_pModel.IsValid();
if (pPlayer.IsAlive() && pPlayer.GetActiveWeapon() == NULL_CBASEENTITY)
{
if (!g_pIdle[iPlayerIndex].m_pModel.IsValid())
if (!bHasModel)
{
char szModelName[MAX_MODELNAME];
if (pPlayer.GetModelName(szModelName, sizeof(szModelName)))
{
g_pIdle[iPlayerIndex].m_pModel = CDynamicProp.Create(szModelName);
if (g_pIdle[iPlayerIndex].m_pModel.IsValid())
if (g_pIdle[iPlayerIndex].m_pModel != NULL_CBASEENTITY)
{
float vec3Pos[3];
pPlayer.GetAbsOrigin(vec3Pos);
Expand All @@ -74,22 +129,26 @@ public void IdleAnims_Player_PreThink(const int iClient)
g_pIdle[iPlayerIndex].m_pModel.SetAnimation("idle_noweapon");
g_pIdle[iPlayerIndex].m_eIdleType = IdleNoWeapon;

SDKHook(g_pIdle[iPlayerIndex].m_pModel.GetEntIndex(), SDKHook_SetTransmit, IdleAnims_SetTransmit);
SDKHook(g_pIdle[iPlayerIndex].m_pModel.GetEntIndex(), SDKHook_SetTransmit, IdleAnims_Hook_SetTransmit);

bHasModel = true;
}
}
}
}
else
{
if (g_pIdle[iPlayerIndex].m_pModel.IsValid())
if (bHasModel)
{
g_pIdle[iPlayerIndex].m_pModel.Kill();
g_pIdle[iPlayerIndex].m_pModel = NULL_CBASEENTITY;
bHasModel = false;
}
}

if (g_pIdle[iPlayerIndex].m_pModel.IsValid())
if (bHasModel)
{
bool bHasGroundEntity = pPlayer.GetGroundEntity() != NULL_CBASEENTITY;
float vec3Velocity[3];
pPlayer.GetAbsVelocity(vec3Velocity);

Expand Down Expand Up @@ -123,8 +182,8 @@ public void IdleAnims_Player_PreThink(const int iClient)
}
}
}
else if ((!pPlayer.GetGroundEntity().IsValid() && g_pIdle[iPlayerIndex].m_eIdleType == JumpStartNoWeapon) ||
(pPlayer.GetGroundEntity().IsValid() && !pPlayer.WasPressingButton(IN_JUMP) && pPlayer.IsPressingButton(IN_JUMP)))
else if ((!bHasGroundEntity && g_pIdle[iPlayerIndex].m_eIdleType == JumpStartNoWeapon) ||
(bHasGroundEntity && !pPlayer.WasPressingButton(IN_JUMP) && pPlayer.IsPressingButton(IN_JUMP)))
{
if (g_pIdle[iPlayerIndex].m_eIdleType != JumpStartNoWeapon)
{
Expand Down Expand Up @@ -199,7 +258,7 @@ public void IdleAnims_Player_PreThink(const int iClient)
int r, g, b, a;
pPlayer.GetRenderColor(r, g, b, a);
g_pIdle[iPlayerIndex].m_pModel.SetRenderColor(r, g, b, a);

for (int i = 0; i < 24; ++i)
{
g_pIdle[iPlayerIndex].m_pModel.SetPoseParameter(i, pPlayer.GetPoseParameter(i));
Expand Down
2 changes: 1 addition & 1 deletion scripting/include/srccoop/classdef.inc
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ stock void InitClassdef(GameData pGameConfig)

methodmap CBaseEntity
{
public CBaseEntity(const int iEntIndex = NULL_CBASEENTITY)
public CBaseEntity(const int iEntIndex = -1)
{
return view_as<CBaseEntity>(iEntIndex > -1 ? EntIndexToEntRef(iEntIndex) : iEntIndex);
}
Expand Down
2 changes: 1 addition & 1 deletion scripting/include/srccoop/playerpatch.inc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void Hook_PlayerPreThinkPost(int iClient)
return;

#if defined SRCCOOP_BLACKMESA
IdleAnims_Player_PreThink(iClient);
IdleAnims_Hook_PlayerPreThinkPost(iClient);
#endif

CBasePlayer pPlayer = CBasePlayer(iClient);
Expand Down
4 changes: 4 additions & 0 deletions scripting/srccoop.sp
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,10 @@ public void OnClientPutInServer(int client)
DHookEntity(hkEvent_Killed, false, client, _, Hook_PlayerKilled);
DHookEntity(hkEvent_Killed, true, client, _, Hook_PlayerKilledPost);

#if defined SRCCOOP_BLACKMESA
DHookEntity(hkSetModel, false, client, _, IdleAnims_Hook_PlayerSetModel);
#endif

#if defined PLAYERPATCH_RESTORE_MP_FORCERESPAWN
DHookEntity(hkForceRespawn, false, client, _, Hook_PlayerForceRespawn);
#endif
Expand Down

0 comments on commit 0268058

Please sign in to comment.