Projekt

Allgemein

Profil

Fehler #25782 » spell_item_anti_venom.cpp

IceNekromant, 12.08.2022 19:11

 
1
// This enumeration helps to create a spell script for all three anti-venoms of the First Aid profession.
2
enum AntiVenomSpellIDs
3
{
4
    SPELL_ANTI_VENOM          = 7932,
5
    SPELL_STRONG_ANTI_VENOM   = 7933,
6
    SPELL_POWERFUL_ANTI_VENOM = 23786
7
};
8

    
9
// Item:  6452 - Anti-Venom          | Spell: 7932 - Anti-Venom
10
// Item:  6453 - Strong Anti-Venom   | Spell: 7933 - Strong Anti-Venom
11
// Item: 19440 - Powerful Anti-Venom | Spell: 23786 - Powerful Anti-Venom
12
class spell_item_anti_venom : public SpellScript
13
{
14
    PrepareSpellScript(spell_item_anti_venom);
15

    
16
    // Only load spell script if the Anti-Venom spell is used by an existing player.
17
    bool Load() override
18
    {
19
        return GetCaster() && GetCaster()->GetTypeId() == TYPEID_PLAYER;
20
    }
21

    
22
    /*
23
    Check if target is affected by poisons.
24
    - If yes, HandleDummy() will be executed, handling the removal of poisons.
25
    - If not, error message "Nothing to dispel" will be shown.
26
    */
27
    SpellCastResult CheckForPoison()
28
    {
29
        Unit* t = GetExplTargetUnit();
30
        if (!t)
31
            t = GetCaster();
32

    
33
        bool b = false;
34

    
35
        uint32 maxSLevel;
36
        switch (GetSpellInfo()->Id)
37
        {
38
            case SPELL_ANTI_VENOM:
39
                maxSLevel = 25;
40
                break;
41
            case SPELL_STRONG_ANTI_VENOM:
42
                maxSLevel = 35;
43
                break;
44
            case SPELL_POWERFUL_ANTI_VENOM:
45
                maxSLevel = 60;
46
                break;
47
			default: // should not happen. fail save.
48
				maxSLevel = 0;
49
				break;
50
        }
51

    
52
        // Iterate through all auras of the player.
53
		for (auto itr = t->GetOwnedAuras().begin(); itr != t->GetOwnedAuras().end(); ++itr)
54
        {
55
            // Second contained item is the actual spell behind the aura.
56
            Aura* aura = itr->second;
57

    
58
            // dMask: dispel mask | sLevel: spell level
59
            uint32 dMask = aura->GetSpellInfo()->GetDispelMask();
60
            uint32 sLevel = aura->GetSpellInfo()->SpellLevel;
61

    
62
            /* If the aura
63
            - is removable by effects dispeling poisons and
64
            - the spell level is less or equal to the maximum spell level of the poison debuff the anti venom can cure, then ...
65
            */
66
			if (dMask == (1 << DISPEL_POISON) && sLevel <= maxSLevel)
67
                b = true; // ... remember it for later.
68
        }
69

    
70
        // If no poison debuff aura was found...
71
		if (!b)
72
            return SPELL_FAILED_NOTHING_TO_DISPEL; // ... return with error message: "Nothing to dispel."
73
        else
74
            return SPELL_CAST_OK; // ... else, all good. Continue to handle the spell effect with HandleDummy().
75
    }
76

    
77
    /*
78
    The method handling the effect.
79
    - Determines the target the Anti-Venom will be used on.
80
    - Removes all poisons on the target up to the designated spell level.
81
    */
82
    void HandleDummy(SpellEffIndex /*effIndex*/)
83
    {
84
        // p: Player using the Anti-Venom. | t: Target the Anti-Venom is used on.
85
        Unit* p = GetCaster();
86
        Unit* t = GetExplTargetUnit();
87

    
88
        // maxSLevel: maximum spell level of the poison debuff the anti venom can cure
89
        uint32 maxSLevel;
90

    
91
        // Anti-Venom used with no target. Set target t to the caster, to use the Anti-Venom on himself.
92
        if (!t)
93
            t = GetCaster();
94

    
95
        /*
96
        set maxSLevel according to the anti venom used.
97
        Anti-Venom          : up to spell level 25.
98
        Strong Anti-Venom   : up to spell level 35.
99
        Powerful Anti-Venom : up to spell level 60.
100
        */
101
        switch (GetSpellInfo()->Id)
102
        {
103
        case SPELL_ANTI_VENOM:
104
            maxSLevel = 25;
105
            break;
106
        case SPELL_STRONG_ANTI_VENOM:
107
            maxSLevel = 35;
108
            break;
109
        case SPELL_POWERFUL_ANTI_VENOM:
110
            maxSLevel = 60;
111
            break;
112
        default: // should not happen. fail save.
113
            maxSLevel = 0;
114
            break;
115
        }
116

    
117
        // Iterate through all auras of the player.
118
        for (auto itr = t->GetOwnedAuras().begin(); itr != t->GetOwnedAuras().end(); ++itr)
119
        {
120
            // Second contained item is the actual spell behind the aura.
121
            Aura* aura = itr->second;
122

    
123
            // dMask: dispel mask of the aura | sLevel: spell level of the aura
124
            uint32 dMask = aura->GetSpellInfo()->GetDispelMask();
125
            uint32 sLevel = aura->GetSpellInfo()->SpellLevel;
126

    
127
            /* If the aura
128
            - is removable by effects dispeling poisons and
129
            - the spell level is less or equal to the maximum spell level of the poison debuff the anti venom can cure, then ...
130
            */
131
            if (dMask == (1 << DISPEL_POISON) && sLevel <= maxSLevel)
132
                t->RemoveAura(aura, AURA_REMOVE_BY_DEFAULT); // ... remove the poison aura.
133
        }
134
    }
135

    
136
    void Register() override
137
    {
138
        OnCheckCast += SpellCheckCastFn(spell_item_anti_venom::CheckForPoison);
139
        OnEffectHit += SpellEffectFn(spell_item_anti_venom::HandleDummy, EFFECT_0, SPELL_EFFECT_DUMMY);
140
    }
141
};
142

    
143
void AddSC_item_spell_scripts()
144
{
145
	RegisterSpellScript(spell_item_anti_venom);
146
}
(3-3/3)