From 53efce2202df71a3c96986d607ce5f91873ff38d Mon Sep 17 00:00:00 2001
From: killerwife <killerwife@gmail.com>
Date: Wed, 24 Apr 2024 00:17:40 +0200
Subject: [PATCH] Spell: Add auto break proc script for auras and their
 respective glyphs

Capped with formula for cls because I cant be bothered to research it too hard.
---
 sql/scriptdev2/spell.sql                      | 18 +++++
 .../scripts/world/spell_scripts_wotlk.cpp     | 65 +++++++++++++++++++
 2 files changed, 83 insertions(+)

diff --git a/sql/scriptdev2/spell.sql b/sql/scriptdev2/spell.sql
index 63125eb086c..fae03aef03c 100644
--- a/sql/scriptdev2/spell.sql
+++ b/sql/scriptdev2/spell.sql
@@ -1381,6 +1381,9 @@ INSERT INTO spell_scripts(Id, ScriptName) VALUES
 (47198,'spell_deaths_embrace'),
 (47199,'spell_deaths_embrace'),
 (47200,'spell_deaths_embrace'),
+(5782,'spell_auto_break_proc'),
+(6213,'spell_auto_break_proc'),
+(6215,'spell_auto_break_proc'),
 (1120,'spell_drain_soul'),
 (8288,'spell_drain_soul'),
 (8289,'spell_drain_soul'),
@@ -1574,6 +1577,12 @@ INSERT INTO spell_scripts(Id, ScriptName) VALUES
 (29447,'spell_torment_the_weak'),
 (55339,'spell_torment_the_weak'),
 (55340,'spell_torment_the_weak'),
+(122,'spell_auto_break_proc'),
+(865,'spell_auto_break_proc'),
+(6131,'spell_auto_break_proc'),
+(10230,'spell_auto_break_proc'),
+(27088,'spell_auto_break_proc'),
+(42917,'spell_auto_break_proc'),
 (56369,'spell_glyph_of_fire_blast'),
 (58838,'spell_inherit_masters_threat_list'),
 (59638,'spell_mirror_image_frostbolt');
@@ -1631,6 +1640,14 @@ INSERT INTO spell_scripts(Id, ScriptName) VALUES
 (53227,'spell_typhoon'),
 (54845,'spell_glyph_of_starfire'),
 (40121,'spell_swift_flight_form_passive'),
+(339,'spell_auto_break_proc'),
+(1062,'spell_auto_break_proc'),
+(5195,'spell_auto_break_proc'),
+(5196,'spell_auto_break_proc'),
+(9852,'spell_auto_break_proc'),
+(9853,'spell_auto_break_proc'),
+(26989,'spell_auto_break_proc'),
+(53308,'spell_auto_break_proc'),
 (467,'spell_thorns_druid'),
 (782,'spell_thorns_druid'),
 (1075,'spell_thorns_druid'),
@@ -1743,6 +1760,7 @@ INSERT INTO spell_scripts(Id, ScriptName) VALUES
 (39610,'spell_mana_tide_totem_effect'),
 (55440,'spell_glyph_of_healing_wave'),
 (63280,'spell_glyph_of_totem_of_wrath'),
+(51514,'spell_auto_break_proc'),
 (52041,'spell_healing_stream_totem_effect'),
 (52046,'spell_healing_stream_totem_effect'),
 (52047,'spell_healing_stream_totem_effect'),
diff --git a/src/game/AI/ScriptDevAI/scripts/world/spell_scripts_wotlk.cpp b/src/game/AI/ScriptDevAI/scripts/world/spell_scripts_wotlk.cpp
index a740f24c8c8..94e8d531df8 100644
--- a/src/game/AI/ScriptDevAI/scripts/world/spell_scripts_wotlk.cpp
+++ b/src/game/AI/ScriptDevAI/scripts/world/spell_scripts_wotlk.cpp
@@ -99,6 +99,70 @@ struct BloodReserveEnchant : public AuraScript
     }
 };
 
+// 5782 - Fear, 339 - Entangling Roots, 122 - Frost Nova, 51514 - Hex
+struct AutoBreakProc : public AuraScript
+{
+    void OnAuraInit(Aura* aura) const override
+    {
+        if (aura->GetEffIndex() == EFFECT_INDEX_1)
+        {
+            Unit* caster = aura->GetCaster();
+            // fear - rank 3 - at 80 - 2600
+            // not impacted by gear
+            // not impacted by target level
+            // not impacted by rank
+            // asumption - depends on caster level
+            uint32 damageThreshold = 0;
+            auto stats = sObjectMgr.GetCreatureClassLvlStats(caster->GetLevel(), CLASS_WARRIOR, EXPANSION_WOTLK);
+
+            switch (aura->GetId()) // keeping them separate for future research
+            {
+                case 5782: // Fear
+                case 6213:
+                case 6215:
+                    damageThreshold = stats->BaseHealth / 4.75;
+                    break;
+                case 51514: // Hex
+                    damageThreshold = stats->BaseHealth / 4.75;
+                    break;
+                case 339: // Entangling Roots
+                case 1062:
+                case 5195:
+                case 5196:
+                case 9852:
+                case 9853:
+                case 26989:
+                case 53308:
+                    damageThreshold = stats->BaseHealth / 4.75;
+                    break;
+                case 122: // Frost nova
+                case 865:
+                case 6131:
+                case 10230:
+                case 27088:
+                case 42917:
+                    damageThreshold = stats->BaseHealth / 4.75;
+                    break;
+            }
+            if (Aura* script = aura->GetCaster()->GetOverrideScript(7801)) // Glyph of Fear, Glyph of Hex, Glyph of Entangling Roots, Glyph of Frost Nova
+                damageThreshold *= script->GetAmount() / 100;
+            aura->SetScriptValue(damageThreshold);
+        }
+    }
+
+    SpellAuraProcResult OnProc(Aura* aura, ProcExecutionData& procData) const override
+    {
+        if (aura->GetEffIndex() != EFFECT_INDEX_1)
+            return SPELL_AURA_PROC_OK;
+
+        if (int32(aura->GetScriptValue()) - procData.damage <= 0)
+            return SPELL_AURA_PROC_OK;
+
+        aura->SetScriptValue(aura->GetScriptValue() - procData.damage);
+        return SPELL_AURA_PROC_CANT_TRIGGER;
+    }
+};
+
 void AddSC_spell_scripts_wotlk()
 {
     RegisterSpellScript<Replenishment>("spell_replenishment");
@@ -106,4 +170,5 @@ void AddSC_spell_scripts_wotlk()
     RegisterSpellScript<Shadowmeld>("spell_shadowmeld");
     RegisterSpellScript<StoicismAbsorb>("spell_stoicism");
     RegisterSpellScript<BloodReserveEnchant>("spell_blood_reserve_enchant");
+    RegisterSpellScript<AutoBreakProc>("spell_auto_break_proc");
 }
\ No newline at end of file