From 77b9c3bd970f2b04b30c57e928cd663034895038 Mon Sep 17 00:00:00 2001
From: neon <>
Date: Sun, 27 Oct 2019 02:29:01 +0200
Subject: [PATCH] Hitmarker: check for outputs

---
 Hitmarker/scripting/Hitmarker.sp | 47 ++++++++++++++++++++++++--------
 1 file changed, 36 insertions(+), 11 deletions(-)

diff --git a/Hitmarker/scripting/Hitmarker.sp b/Hitmarker/scripting/Hitmarker.sp
index c06a90d0..21e22ab6 100644
--- a/Hitmarker/scripting/Hitmarker.sp
+++ b/Hitmarker/scripting/Hitmarker.sp
@@ -5,14 +5,18 @@
 #include <clientprefs>
 #include <multicolors>
 #include <dhooks>
+#include <outputinfo>
+
+#pragma newdecls required
+#pragma	semicolon 1
 
 #define SPECMODE_NONE           0
 #define SPECMODE_FIRSTPERSON    4
 #define SPECMODE_THIRDPERSON    5
 #define SPECMODE_FREELOOK       6
 
-#pragma newdecls required
-#pragma	semicolon 1
+#define MAX_EDICTS 4096
+bool g_bHasOutputs[MAX_EDICTS];
 
 bool g_bShowBossHitmarker[MAXPLAYERS+1];
 bool g_bShowZombieHitmarker[MAXPLAYERS+1];
@@ -33,6 +37,8 @@ Handle g_hCookie_HitmarkerSound;
 Handle g_hCookie_HitmarkerSoundVolume;
 Handle g_hCookie_HitmarkerSkin;
 
+ConVar g_hCVar_Debug;
+
 bool g_bLate;
 
 //----------------------------------------------------------------------------------------------------
@@ -41,9 +47,9 @@ bool g_bLate;
 public Plugin myinfo =
 {
 	name         = "Hitmarker",
-	author       = "Neon & Nano",
+	author       = "Neon",
 	description  = "Players can enable or disable their hitmarkers while shooting zombies or bosses",
-	version      = "3.0.1",
+	version      = "3.1",
 };
 
 //----------------------------------------------------------------------------------------------------
@@ -80,6 +86,9 @@ public void OnPluginStart()
 	g_hCookie_HitmarkerSoundVolume = RegClientCookie("hitmarker_sound_volume", "", CookieAccess_Private);
 	g_hCookie_HitmarkerSkin = RegClientCookie("hitmarker_skin", "", CookieAccess_Private);
 
+	g_hCVar_Debug = CreateConVar("sm_hitmarker_debug", "0", "", FCVAR_NONE, true, 0.0, true, 1.0);
+	AutoExecConfig();
+
 	RegConsoleCmd("sm_hm", OnHitmarkerSettings);
 	RegConsoleCmd("sm_hitmarker", OnHitmarkerSettings);
 	RegConsoleCmd("sm_bhm", OnToggleBossHitmarker);
@@ -420,6 +429,18 @@ public int MenuHandler_MainMenu(Menu menu, MenuAction action, int client, int se
 	}
 }
 
+//----------------------------------------------------------------------------------------------------
+// Purpose:
+//----------------------------------------------------------------------------------------------------
+public void OnEntitySpawned(int Entity, const char[] sClassname)
+{
+	int ent = EntRefToEntIndex(Entity);
+	if ((GetOutputCount(ent, "m_OnDamaged") > 0) || (GetOutputCount(ent, "m_OnHealthChanged") > 0))
+		g_bHasOutputs[ent] = true;
+	else
+		g_bHasOutputs[ent] = false;
+}
+
 //----------------------------------------------------------------------------------------------------
 // Purpose:
 //----------------------------------------------------------------------------------------------------
@@ -450,14 +471,18 @@ public void OnBossDamaged(CBoss Boss, CConfig Config, int client, float damage)
 //----------------------------------------------------------------------------------------------------
 public MRESReturn Detour_OnTakeDamage(int entity, Handle hReturn, Handle hParams)
 {
-	int iHealth = GetEntProp(entity, Prop_Data, "m_iHealth");
-	if (!iHealth)
-		return MRES_Ignored;
-
-	int client = DHookGetParamObjectPtrVar(hParams, 1, 3*(3*4) + 0, ObjectValueType_Ehandle);
 	//https://github.com/alliedmodders/hl2sdk/blob/css/game/shared/takedamageinfo.h#L115
-	//float dmg = DHookGetParamObjectPtrVar(hParams, 1, 48, ObjectValueType_Float);
-	//int ret = DHookGetReturn(hReturn);
+	int iHealth = GetEntProp(entity, Prop_Data, "m_iHealth");
+	int client = DHookGetParamObjectPtrVar(hParams, 1, 3*(3*4) + 0, ObjectValueType_Ehandle);
+
+	if (g_hCVar_Debug.BoolValue)
+	{
+		float dmg = DHookGetParamObjectPtrVar(hParams, 1, 48, ObjectValueType_Float);
+		PrintToChatAll("Detour_OnTakeDamage: ent: %d; client: %d; m_iHealth: %d; output: %d; dmg: %f", entity, client, iHealth, g_bHasOutputs[entity], dmg);
+	}
+
+	if (!iHealth && !g_bHasOutputs[entity])
+		return MRES_Ignored;
 
 	if (!IsValidClient(client))
 		return MRES_Ignored;