From 8cf73bde88802d0ffc16327c51240e25c12e83d8 Mon Sep 17 00:00:00 2001
From: Mike-MF <TyroneMF@hotmail.com>
Date: Fri, 25 Oct 2024 04:48:40 +0100
Subject: [PATCH 1/7] Medical - Unconscious Effects

---
 addons/medical/XEH_PREP.hpp                   |  1 +
 addons/medical/XEH_postInit.sqf               | 20 ++++++++++
 addons/medical/XEH_preInit.sqf                |  4 ++
 addons/medical/config.cpp                     |  8 +++-
 .../medical/functions/fnc_unconsciousFX.sqf   | 38 +++++++++++++++++++
 addons/medical/initSettings.inc.sqf           | 31 ++++++++++++++-
 addons/medical/script_component.hpp           |  3 ++
 addons/medical/stringtable.xml                | 18 +++++++++
 8 files changed, 120 insertions(+), 3 deletions(-)
 create mode 100644 addons/medical/XEH_PREP.hpp
 create mode 100644 addons/medical/functions/fnc_unconsciousFX.sqf

diff --git a/addons/medical/XEH_PREP.hpp b/addons/medical/XEH_PREP.hpp
new file mode 100644
index 00000000..af24c01e
--- /dev/null
+++ b/addons/medical/XEH_PREP.hpp
@@ -0,0 +1 @@
+PREP(unconsciousFX);
diff --git a/addons/medical/XEH_postInit.sqf b/addons/medical/XEH_postInit.sqf
index bf995d92..b3ab699d 100644
--- a/addons/medical/XEH_postInit.sqf
+++ b/addons/medical/XEH_postInit.sqf
@@ -48,3 +48,23 @@
         };
     }, _unit, 1] call CBA_fnc_waitAndExecute;
 }] call CBA_fnc_addEventHandler;
+
+// Unconscious Moaning
+if (isServer) then {
+    ["ace_unconscious", {
+        params ["_unit", "_state"];
+
+        if !(GVAR(unconsciousFXEnabled)) exitWith {};
+
+        if (isPlayer _unit && _state) then {
+            // Knock out sound
+            private _knockOutNoise = selectRandom KO_NOISES;
+            [QEGVAR(mission,say3D), [_unit, _knockOutNoise]] call CBA_fnc_globalEvent;
+
+            // Local PFH for periodic groaning
+            [QGVAR(unconsciousFX), [], _unit] call CBA_fnc_targetEvent;
+        };
+    }] call CBA_fnc_addEventHandler;
+};
+
+[QGVAR(unconsciousFx), LINKFUNC(unconsciousFX)] call CBA_fnc_addEventHandler;
diff --git a/addons/medical/XEH_preInit.sqf b/addons/medical/XEH_preInit.sqf
index 7c7b7646..89477353 100644
--- a/addons/medical/XEH_preInit.sqf
+++ b/addons/medical/XEH_preInit.sqf
@@ -2,6 +2,10 @@
 
 ADDON = false;
 
+PREP_RECOMPILE_START;
+#include "XEH_PREP.hpp"
+PREP_RECOMPILE_END;
+
 #include "initSettings.inc.sqf"
 
 ADDON = true;
diff --git a/addons/medical/config.cpp b/addons/medical/config.cpp
index c39a7367..bd3f0a94 100644
--- a/addons/medical/config.cpp
+++ b/addons/medical/config.cpp
@@ -6,9 +6,13 @@ class CfgPatches {
         units[] = {};
         weapons[] = {};
         requiredVersion = REQUIRED_VERSION;
-        requiredAddons[] = {"tac_main", "ace_medical_engine"};
+        requiredAddons[] = {
+            "tac_main",
+            "tac_mission",
+            "ace_medical_engine"
+        };
         author = ECSTRING(main,Author);
-        authors[] = {"Jonpas"};
+        authors[] = {"Jonpas", "Mike"};
         url = ECSTRING(main,URL);
         VERSION_CONFIG;
     };
diff --git a/addons/medical/functions/fnc_unconsciousFX.sqf b/addons/medical/functions/fnc_unconsciousFX.sqf
new file mode 100644
index 00000000..8d0fbd27
--- /dev/null
+++ b/addons/medical/functions/fnc_unconsciousFX.sqf
@@ -0,0 +1,38 @@
+#include "..\script_component.hpp"
+/*
+ * Author: Mike
+ * Handles noises while unconscious
+ *
+ * Called locally per player via target event
+ *
+ * Arguments
+ * None
+ *
+ * Return Value:
+ * None
+ *
+ * Example:
+ * [] call tac_medical_fnc_unconsciousFX
+*/
+
+[{
+    params ["_args", "_handle"];
+    _args params [["_firstIteration", true]];
+
+    // If woken up or died end PFH.
+    if !(ace_player getVariable ["ACE_isUnconscious", false]) exitWith {
+        _handle call CBA_fnc_removePerFrameHandler;
+    };
+
+    // Don't want the groaning to happen instantly.
+    if (_firstIteration) exitWith {
+        _args set [0, false];
+    };
+
+    // 50% chance of making noises, requires a heart rate above CPR/Cardiac Arrest
+    private _heartRate = ace_player getVariable ["ace_medical_heartRate", 80];
+    if (random 1 > GVAR(unconsciousFXChance) && _heartRate > 40) then {
+        private _moan = selectRandom UNCONSCIOUS_NOISES;
+        [QEGVAR(mission,say3D), [ace_player, _moan]] call CBA_fnc_globalEvent;
+    };
+}, GVAR(unconsciousFXTimer)] call CBA_fnc_addPerFrameHandler;
diff --git a/addons/medical/initSettings.inc.sqf b/addons/medical/initSettings.inc.sqf
index 721ad054..046f7368 100644
--- a/addons/medical/initSettings.inc.sqf
+++ b/addons/medical/initSettings.inc.sqf
@@ -1,8 +1,37 @@
+private _category = format ["TAC %1", QUOTE(COMPONENT_BEAUTIFIED)];
+
 [
     QGVAR(fatalInjuriesCardiacArrestTimeCoefficient),
     "SLIDER",
     [LSTRING(FatalInjuriesCardiacArrestTimeCoefficient_DisplayName), LSTRING(FatalInjuriesCardiacArrestTimeCoefficient_Description)],
-    format ["TAC %1", QUOTE(COMPONENT_BEAUTIFIED)],
+    _category,
     [0.01, 1, 0.2, 2],
     true // isGlobal
 ] call CBA_fnc_addSetting;
+
+[
+    QGVAR(unconsciousFXEnabled),
+    "CHECKBOX",
+    [LSTRING(UnconsciousFX_DisplayName), LSTRING(UnconsciousFX_Description)],
+    _category,
+    true,
+    1
+] call CBA_fnc_addSetting;
+
+[
+    QGVAR(unconsciousFXChance),
+    "SLIDER",
+    [LSTRING(UnconsciousFXChance_DisplayName), LSTRING(UnconsciousFXChance_Description)],
+    _category,
+    [0, 1, 0.5, 1, true],
+    1
+] call CBA_fnc_addSetting;
+
+[
+    QGVAR(unconsciousFXTimer),
+    "SLIDER",
+    [LSTRING(UnconsciousFXTimer_DisplayName), LSTRING(UnconsciousFXTimer_Description)],
+    _category,
+    [5, 60, 30, 1, false],
+    1
+] call CBA_fnc_addSetting;
diff --git a/addons/medical/script_component.hpp b/addons/medical/script_component.hpp
index a08a9106..1f39b9a7 100644
--- a/addons/medical/script_component.hpp
+++ b/addons/medical/script_component.hpp
@@ -15,3 +15,6 @@
 #endif
 
 #include "\x\tac\addons\main\script_macros.hpp"
+
+#define KO_NOISES ["ace_fire_scream_12", "ACE_hit_Male05ENG_high_3", "ACE_hit_Male05ENG_high_4", "ACE_hit_Male05ENG_mid_4", "ACE_moan_Male06ENG_high_8"]
+#define UNCONSCIOUS_NOISES ["ace_fire_scream_8", "ACE_moan_Male07ENG_high_2", "ACE_moan_Male07ENG_high_4", "ACE_moan_Male09ENG_high_1", "ACE_moan_Male09ENG_high_2", "ACE_moan_Male09ENG_high_3", "ACE_moan_Male09ENG_high_4"]
diff --git a/addons/medical/stringtable.xml b/addons/medical/stringtable.xml
index d07195a5..a09fdcea 100644
--- a/addons/medical/stringtable.xml
+++ b/addons/medical/stringtable.xml
@@ -7,5 +7,23 @@
         <Key ID="STR_TAC_Medical_FatalInjuriesCardiacArrestTimeCoefficient_Description">
             <English>Coefficient for controlling the Cardiac Arrest Time on fatal injuries when 'Fatal Injuries' is NOT 'Always'.</English>
         </Key>
+        <Key ID="STR_TAC_Medical_UnconsciousFX_DisplayName">
+            <English>Unconscious Effect Sounds</English>
+        </Key>
+        <Key ID="STR_TAC_Medical_UnconsciousFX_Description">
+            <English>Enabled groaning sounds for unconscious players with a heart rate.</English>
+        </Key>
+        <Key ID="STR_TAC_Medical_UnconsciousFXChance_DisplayName">
+            <English>Unconscious Effect Sound Chance</English>
+        </Key>
+        <Key ID="STR_TAC_Medical_UnconsciousFXChance_Description">
+            <English>The chance of unconscious sounds playing each check.</English>
+        </Key>
+        <Key ID="STR_TAC_Medical_UnconsciousFXTimer_DisplayName">
+            <English>Unconscious Effect Timer</English>
+        </Key>
+        <Key ID="STR_TAC_Medical_UnconsciousFXTimer_Description">
+            <English>Time interval for checking if the sound effect should play</English>
+        </Key>
     </Package>
 </Project>

From b905b816c55a147c367baf8142b68eac864a511a Mon Sep 17 00:00:00 2001
From: Mike-MF <TyroneMF@hotmail.com>
Date: Fri, 25 Oct 2024 04:53:31 +0100
Subject: [PATCH 2/7] ACE requiredAddons

---
 addons/medical/config.cpp | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/addons/medical/config.cpp b/addons/medical/config.cpp
index bd3f0a94..042bbd5b 100644
--- a/addons/medical/config.cpp
+++ b/addons/medical/config.cpp
@@ -9,7 +9,9 @@ class CfgPatches {
         requiredAddons[] = {
             "tac_main",
             "tac_mission",
-            "ace_medical_engine"
+            "ace_fire",
+            "ace_medical_engine",
+            "ace_medical_feedback"
         };
         author = ECSTRING(main,Author);
         authors[] = {"Jonpas", "Mike"};

From 61dd4327c03c27b9254359ab6374d02cb8f19056 Mon Sep 17 00:00:00 2001
From: Mike-MF <TyroneMF@hotmail.com>
Date: Fri, 25 Oct 2024 07:29:38 +0100
Subject: [PATCH 3/7] Sort stringtable

---
 addons/medical/stringtable.xml | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/addons/medical/stringtable.xml b/addons/medical/stringtable.xml
index a09fdcea..c487ca33 100644
--- a/addons/medical/stringtable.xml
+++ b/addons/medical/stringtable.xml
@@ -1,29 +1,29 @@
 <?xml version="1.0" encoding="utf-8"?>
 <Project name="TAC">
     <Package name="Medical">
-        <Key ID="STR_TAC_Medical_FatalInjuriesCardiacArrestTimeCoefficient_DisplayName">
-            <English>Fatal Injuries Cardiac Arrest Time Coefficient</English>
-        </Key>
         <Key ID="STR_TAC_Medical_FatalInjuriesCardiacArrestTimeCoefficient_Description">
             <English>Coefficient for controlling the Cardiac Arrest Time on fatal injuries when 'Fatal Injuries' is NOT 'Always'.</English>
         </Key>
-        <Key ID="STR_TAC_Medical_UnconsciousFX_DisplayName">
-            <English>Unconscious Effect Sounds</English>
+        <Key ID="STR_TAC_Medical_FatalInjuriesCardiacArrestTimeCoefficient_DisplayName">
+            <English>Fatal Injuries Cardiac Arrest Time Coefficient</English>
         </Key>
-        <Key ID="STR_TAC_Medical_UnconsciousFX_Description">
-            <English>Enabled groaning sounds for unconscious players with a heart rate.</English>
+        <Key ID="STR_TAC_Medical_UnconsciousFXChance_Description">
+            <English>The chance of unconscious sounds playing each check.</English>
         </Key>
         <Key ID="STR_TAC_Medical_UnconsciousFXChance_DisplayName">
             <English>Unconscious Effect Sound Chance</English>
         </Key>
-        <Key ID="STR_TAC_Medical_UnconsciousFXChance_Description">
-            <English>The chance of unconscious sounds playing each check.</English>
+        <Key ID="STR_TAC_Medical_UnconsciousFXTimer_Description">
+            <English>Time interval for checking if the sound effect should play</English>
         </Key>
         <Key ID="STR_TAC_Medical_UnconsciousFXTimer_DisplayName">
             <English>Unconscious Effect Timer</English>
         </Key>
-        <Key ID="STR_TAC_Medical_UnconsciousFXTimer_Description">
-            <English>Time interval for checking if the sound effect should play</English>
+        <Key ID="STR_TAC_Medical_UnconsciousFX_Description">
+            <English>Enabled groaning sounds for unconscious players with a heart rate.</English>
+        </Key>
+        <Key ID="STR_TAC_Medical_UnconsciousFX_DisplayName">
+            <English>Unconscious Effect Sounds</English>
         </Key>
     </Package>
 </Project>

From 8ba7de7dc3b376d4e659eaefa2ad9cfceb773ebe Mon Sep 17 00:00:00 2001
From: Mike-MF <TyroneMF@hotmail.com>
Date: Sat, 14 Dec 2024 17:43:18 +0000
Subject: [PATCH 4/7] Update addons/medical/functions/fnc_unconsciousFX.sqf

Co-authored-by: jonpas <jonpas33@gmail.com>
---
 addons/medical/functions/fnc_unconsciousFX.sqf | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/addons/medical/functions/fnc_unconsciousFX.sqf b/addons/medical/functions/fnc_unconsciousFX.sqf
index 8d0fbd27..d9b27cc8 100644
--- a/addons/medical/functions/fnc_unconsciousFX.sqf
+++ b/addons/medical/functions/fnc_unconsciousFX.sqf
@@ -1,9 +1,9 @@
 #include "..\script_component.hpp"
 /*
  * Author: Mike
- * Handles noises while unconscious
+ * Handles noises while unconscious.
  *
- * Called locally per player via target event
+ * Called locally per player via target event.
  *
  * Arguments
  * None

From 8a83d1b3ece2c7e8abbd4a8f01c005d537113274 Mon Sep 17 00:00:00 2001
From: Mike-MF <TyroneMF@hotmail.com>
Date: Sat, 14 Dec 2024 17:47:36 +0000
Subject: [PATCH 5/7] Requested changes

---
 addons/medical/XEH_postInit.sqf     | 2 +-
 addons/medical/initSettings.inc.sqf | 9 ---------
 2 files changed, 1 insertion(+), 10 deletions(-)

diff --git a/addons/medical/XEH_postInit.sqf b/addons/medical/XEH_postInit.sqf
index b3ab699d..6c319ded 100644
--- a/addons/medical/XEH_postInit.sqf
+++ b/addons/medical/XEH_postInit.sqf
@@ -54,7 +54,7 @@ if (isServer) then {
     ["ace_unconscious", {
         params ["_unit", "_state"];
 
-        if !(GVAR(unconsciousFXEnabled)) exitWith {};
+        if (GVAR(unconsciousFXChance) == 0) exitWith {};
 
         if (isPlayer _unit && _state) then {
             // Knock out sound
diff --git a/addons/medical/initSettings.inc.sqf b/addons/medical/initSettings.inc.sqf
index 046f7368..f018d0d6 100644
--- a/addons/medical/initSettings.inc.sqf
+++ b/addons/medical/initSettings.inc.sqf
@@ -9,15 +9,6 @@ private _category = format ["TAC %1", QUOTE(COMPONENT_BEAUTIFIED)];
     true // isGlobal
 ] call CBA_fnc_addSetting;
 
-[
-    QGVAR(unconsciousFXEnabled),
-    "CHECKBOX",
-    [LSTRING(UnconsciousFX_DisplayName), LSTRING(UnconsciousFX_Description)],
-    _category,
-    true,
-    1
-] call CBA_fnc_addSetting;
-
 [
     QGVAR(unconsciousFXChance),
     "SLIDER",

From 719a8b39bdcc70db2d0c06ca4d94209ea007488f Mon Sep 17 00:00:00 2001
From: Mike-MF <TyroneMF@hotmail.com>
Date: Tue, 17 Dec 2024 21:57:48 +0000
Subject: [PATCH 6/7] Remove unnecessary stringtable entries

---
 addons/medical/stringtable.xml | 6 ------
 1 file changed, 6 deletions(-)

diff --git a/addons/medical/stringtable.xml b/addons/medical/stringtable.xml
index c487ca33..57d9d332 100644
--- a/addons/medical/stringtable.xml
+++ b/addons/medical/stringtable.xml
@@ -19,11 +19,5 @@
         <Key ID="STR_TAC_Medical_UnconsciousFXTimer_DisplayName">
             <English>Unconscious Effect Timer</English>
         </Key>
-        <Key ID="STR_TAC_Medical_UnconsciousFX_Description">
-            <English>Enabled groaning sounds for unconscious players with a heart rate.</English>
-        </Key>
-        <Key ID="STR_TAC_Medical_UnconsciousFX_DisplayName">
-            <English>Unconscious Effect Sounds</English>
-        </Key>
     </Package>
 </Project>

From bae8d743e1e0f81c741105d1fd387f0215e3b9a2 Mon Sep 17 00:00:00 2001
From: jonpas <jonpas33@gmail.com>
Date: Wed, 18 Dec 2024 13:01:32 +0100
Subject: [PATCH 7/7] Apply suggestions from code review

---
 addons/medical/XEH_postInit.sqf                | 3 ++-
 addons/medical/config.cpp                      | 1 -
 addons/medical/functions/fnc_unconsciousFX.sqf | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/addons/medical/XEH_postInit.sqf b/addons/medical/XEH_postInit.sqf
index 6c319ded..3bb97921 100644
--- a/addons/medical/XEH_postInit.sqf
+++ b/addons/medical/XEH_postInit.sqf
@@ -59,7 +59,7 @@ if (isServer) then {
         if (isPlayer _unit && _state) then {
             // Knock out sound
             private _knockOutNoise = selectRandom KO_NOISES;
-            [QEGVAR(mission,say3D), [_unit, _knockOutNoise]] call CBA_fnc_globalEvent;
+            [QGVAR(say3D), [_unit, _knockOutNoise]] call CBA_fnc_globalEvent;
 
             // Local PFH for periodic groaning
             [QGVAR(unconsciousFX), [], _unit] call CBA_fnc_targetEvent;
@@ -67,4 +67,5 @@ if (isServer) then {
     }] call CBA_fnc_addEventHandler;
 };
 
+[QGVAR(say3D), {(_this select 0) say3D (_this select 1)}] call CBA_fnc_addEventHandler;
 [QGVAR(unconsciousFx), LINKFUNC(unconsciousFX)] call CBA_fnc_addEventHandler;
diff --git a/addons/medical/config.cpp b/addons/medical/config.cpp
index 042bbd5b..d4af9761 100644
--- a/addons/medical/config.cpp
+++ b/addons/medical/config.cpp
@@ -8,7 +8,6 @@ class CfgPatches {
         requiredVersion = REQUIRED_VERSION;
         requiredAddons[] = {
             "tac_main",
-            "tac_mission",
             "ace_fire",
             "ace_medical_engine",
             "ace_medical_feedback"
diff --git a/addons/medical/functions/fnc_unconsciousFX.sqf b/addons/medical/functions/fnc_unconsciousFX.sqf
index d9b27cc8..864dbf27 100644
--- a/addons/medical/functions/fnc_unconsciousFX.sqf
+++ b/addons/medical/functions/fnc_unconsciousFX.sqf
@@ -33,6 +33,6 @@
     private _heartRate = ace_player getVariable ["ace_medical_heartRate", 80];
     if (random 1 > GVAR(unconsciousFXChance) && _heartRate > 40) then {
         private _moan = selectRandom UNCONSCIOUS_NOISES;
-        [QEGVAR(mission,say3D), [ace_player, _moan]] call CBA_fnc_globalEvent;
+        [QGVAR(say3D), [ace_player, _moan]] call CBA_fnc_globalEvent;
     };
 }, GVAR(unconsciousFXTimer)] call CBA_fnc_addPerFrameHandler;