Skip to content

Commit

Permalink
Cleanup ragdoll code once again
Browse files Browse the repository at this point in the history
- this fixes creating ragdolls in vehicles in hl2dm
  • Loading branch information
Alienmario committed Jul 21, 2024
1 parent 7749628 commit 513dc86
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 48 deletions.
14 changes: 11 additions & 3 deletions scripting/include/srccoop/classdef.inc
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,14 @@ methodmap CBaseAnimating < CBaseEntity
{
return GetEntityAttachment(this.GetEntIndex(), iAttachment, vecOrigin, vecAngles);
}
public int GetForceBone()
{
return GetEntProp(this.GetEntIndex(), Prop_Send, "m_nForceBone");
}
public void SetForceBone(int iForceBone)
{
SetEntProp(this.GetEntIndex(), Prop_Send, "m_nForceBone", iForceBone);
}
}

methodmap CBaseTrigger < CBaseEntity
Expand Down Expand Up @@ -1882,11 +1890,11 @@ methodmap CBasePlayer < CBaseCombatCharacter
{
SDKCall(g_pCreateViewModel, this.entindex, iIndex);
}
public CBaseEntity GetRagdoll()
public CBaseAnimating GetRagdoll()
{
return CBaseEntity(GetEntPropEnt(this.GetEntIndex(), Prop_Send, "m_hRagdoll"));
return CBaseAnimating(GetEntPropEnt(this.GetEntIndex(), Prop_Send, "m_hRagdoll"));
}
public void SetRagdoll(CBaseEntity pEntity)
public void SetRagdoll(CBaseAnimating pEntity)
{
SetEntPropEnt(this.GetEntIndex(), Prop_Send, "m_hRagdoll", pEntity.GetEntIndex());
}
Expand Down
87 changes: 42 additions & 45 deletions scripting/include/srccoop/playerpatch.inc
Original file line number Diff line number Diff line change
Expand Up @@ -422,95 +422,92 @@ public MRESReturn Hook_PlayerSpawnPost(int iClient, Handle hReturn, Handle hPara
// - Force serverside ragdolls
//------------------------------------------------------
#if defined PLAYERPATCH_SERVERSIDE_RAGDOLLS
CBaseAnimating g_pNewRagdoll = NULL_CBASEENTITY;
bool g_bServerRagdoll[MAXPLAYERS + 1];
#endif

public MRESReturn Hook_PlayerKilled(int _this, DHookParam hParams)
{
#if defined PLAYERPATCH_SERVERSIDE_RAGDOLLS
g_pNewRagdoll = NULL_CBASEENTITY;
g_bServerRagdoll[_this] = false;
#endif

if (!CoopManager.IsCoopModeEnabled())
return MRES_Ignored;

CBasePlayer pPlayer = CBasePlayer(_this);
if (!pPlayer.IsValid())
return MRES_Ignored;

PackPlayerItems(pPlayer);

#if defined PLAYERPATCH_SERVERSIDE_RAGDOLLS
Address pTakeDmgInfo = hParams.GetAddress(1);
int dmgType = LoadFromAddress(pTakeDmgInfo + view_as<Address>(60), NumberType_Int32);
if (dmgType & DMG_DISSOLVE)
{
// Dont bother
return MRES_Ignored;
}

int iForceBone = GetEntProp(_this, Prop_Send, "m_nForceBone");
g_pNewRagdoll = CBaseAnimating(SDKCall(g_pCreateServerRagdoll, _this, iForceBone, pTakeDmgInfo, COLLISION_GROUP_DEBRIS_TRIGGER, false));
SDKHook(g_pNewRagdoll.GetEntIndex(), SDKHook_OnTakeDamage, Hook_NoDmg);

#if defined SRCCOOP_HL2DM
PlayerPatch_ApplyServerRagdoll(pPlayer);
#endif
#endif // PLAYERPATCH_SERVERSIDE_RAGDOLLS

return MRES_Ignored;
}

public MRESReturn Hook_PlayerKilledPost(int _this, DHookParam hParams)
{
if (CoopManager.IsCoopModeEnabled())
{
CBasePlayer pPlayer = CBasePlayer(_this);
if (!CoopManager.IsCoopModeEnabled())
return MRES_Ignored;

CBasePlayer pPlayer = CBasePlayer(_this);

#if defined PLAYERPATCH_SERVERSIDE_RAGDOLLS
Address pTakeDmgInfo = hParams.GetAddress(1);
PlayerPatch_ApplyServerRagdoll(pPlayer, pTakeDmgInfo);
#endif

#if defined PLAYERPATCH_SERVERSIDE_RAGDOLLS && defined SRCCOOP_BLACKMESA
if (g_pNewRagdoll.IsValid())
{
PlayerPatch_ApplyServerRagdoll(pPlayer);
}
#endif
SurvivalManager.OnPlayerDeath(pPlayer);

SurvivalManager.OnPlayerDeath(pPlayer);
}
return MRES_Ignored;
}

#if defined PLAYERPATCH_SERVERSIDE_RAGDOLLS
void PlayerPatch_ApplyServerRagdoll(CBasePlayer pPlayer)

void PlayerPatch_ApplyServerRagdoll(CBasePlayer pPlayer, Address pTakeDmgInfo)
{
CBaseEntity pRagdoll = pPlayer.GetRagdoll();
if (pRagdoll.IsValid())
int iDmgType = LoadFromAddress(pTakeDmgInfo + view_as<Address>(60), NumberType_Int32);
if (iDmgType & DMG_DISSOLVE)
{
// dont bother
return;
}

CBaseAnimating pRagdoll = pPlayer.GetRagdoll();
if (pRagdoll != NULL_CBASEENTITY)
{
pRagdoll.Kill();
}

if (pPlayer.GetTeam() == TEAM_SPECTATOR)
{
// teamchange induced suicide - no ragdolls
g_pNewRagdoll.Kill();
return;
}
pPlayer.SetRagdoll(g_pNewRagdoll);

#if defined SRCCOOP_BLACKMESA
pPlayer.SetEffects(pPlayer.GetEffects() & ~EF_NODRAW);
#endif

pRagdoll = CBaseAnimating(SDKCall(g_pCreateServerRagdoll, pPlayer.entindex, pPlayer.GetForceBone(), pTakeDmgInfo, COLLISION_GROUP_DEBRIS_TRIGGER, false));
SDKHook(pRagdoll.entindex, SDKHook_OnTakeDamage, Hook_NoDmg);

pPlayer.SetRagdoll(pRagdoll);
pPlayer.SetObserverMode(OBS_MODE_CHASE);
pPlayer.SetObserverTarget(g_pNewRagdoll);
pPlayer.SetObserverTarget(pRagdoll);
g_bServerRagdoll[pPlayer.entindex] = true;

Call_StartForward(g_OnPlayerRagdollCreatedFwd);
Call_PushCell(pPlayer);
Call_PushCell(g_pNewRagdoll);
Call_PushCell(pRagdoll);
Call_Finish();
}

public MRESReturn Hook_CreateRagdollEntity(int _this)
{
if (!g_pNewRagdoll.IsValid())
return MRES_Ignored;

// avoid invalid cast on m_hRagdoll in hl2mp with the serverside ragdolls
return MRES_Supercede;
if (g_bServerRagdoll[_this])
return MRES_Supercede;

return MRES_Ignored;

}

#endif // PLAYERPATCH_SERVERSIDE_RAGDOLLS

//------------------------------------------------------
Expand Down

0 comments on commit 513dc86

Please sign in to comment.