root/trunk/src/game/SpellMgr.cpp @ 94

Revision 87, 83.3 kB (checked in by yumileroy, 17 years ago)

[svn] Try to remove the big hack in SpellMgr::IsNoStackSpellDueToSpell?. Not sure if this works.

Original author: megamage
Date: 2008-10-21 11:22:12-05:00

Line 
1/*
2 * Copyright (C) 2008 Trinity <http://www.trinitycore.org/>
3 *
4 * Thanks to the original authors: MaNGOS <http://www.mangosproject.org/>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include "SpellMgr.h"
22#include "ObjectMgr.h"
23#include "SpellAuraDefines.h"
24#include "ProgressBar.h"
25#include "Database/DBCStores.h"
26#include "World.h"
27#include "Chat.h"
28#include "Spell.h"
29
30SpellMgr::SpellMgr()
31{
32}
33
34SpellMgr::~SpellMgr()
35{
36}
37
38SpellMgr& SpellMgr::Instance()
39{
40    static SpellMgr spellMgr;
41    return spellMgr;
42}
43
44int32 GetSpellDuration(SpellEntry const *spellInfo)
45{
46    if(!spellInfo)
47        return 0;
48    SpellDurationEntry const *du = sSpellDurationStore.LookupEntry(spellInfo->DurationIndex);
49    if(!du)
50        return 0;
51    return (du->Duration[0] == -1) ? -1 : abs(du->Duration[0]);
52}
53
54int32 GetSpellMaxDuration(SpellEntry const *spellInfo)
55{
56    if(!spellInfo)
57        return 0;
58    SpellDurationEntry const *du = sSpellDurationStore.LookupEntry(spellInfo->DurationIndex);
59    if(!du)
60        return 0;
61    return (du->Duration[2] == -1) ? -1 : abs(du->Duration[2]);
62}
63
64uint32 GetSpellCastTime(SpellEntry const* spellInfo, Spell const* spell)
65{
66    SpellCastTimesEntry const *spellCastTimeEntry = sSpellCastTimesStore.LookupEntry(spellInfo->CastingTimeIndex);
67
68    // not all spells have cast time index and this is all is pasiive abilities
69    if(!spellCastTimeEntry)
70        return 0;
71
72    int32 castTime = spellCastTimeEntry->CastTime;
73
74    if (spell)
75    {
76        if(Player* modOwner = spell->GetCaster()->GetSpellModOwner())
77            modOwner->ApplySpellMod(spellInfo->Id, SPELLMOD_CASTING_TIME, castTime, spell);
78
79        if( !(spellInfo->Attributes & (SPELL_ATTR_UNK4|SPELL_ATTR_UNK5)) )
80            castTime = int32(castTime * spell->GetCaster()->GetFloatValue(UNIT_MOD_CAST_SPEED));
81        else
82        {
83            if (spell->IsRangedSpell() && !spell->IsAutoRepeat())
84                castTime = int32(castTime * spell->GetCaster()->m_modAttackSpeedPct[RANGED_ATTACK]);
85        }
86    }
87
88    if (spellInfo->Attributes & SPELL_ATTR_RANGED && (!spell || !(spell->IsAutoRepeat())))
89        castTime += 500;
90
91    return (castTime > 0) ? uint32(castTime) : 0;
92}
93
94bool IsPassiveSpell(uint32 spellId)
95{
96    SpellEntry const *spellInfo = sSpellStore.LookupEntry(spellId);
97    if (!spellInfo)
98        return false;
99    return (spellInfo->Attributes & SPELL_ATTR_PASSIVE) != 0;
100}
101
102bool IsNoStackAuraDueToAura(uint32 spellId_1, uint32 effIndex_1, uint32 spellId_2, uint32 effIndex_2)
103{
104    SpellEntry const *spellInfo_1 = sSpellStore.LookupEntry(spellId_1);
105    SpellEntry const *spellInfo_2 = sSpellStore.LookupEntry(spellId_2);
106    if(!spellInfo_1 || !spellInfo_2) return false;
107    if(spellInfo_1->Id == spellId_2) return false;
108
109    if (spellInfo_1->Effect[effIndex_1] != spellInfo_2->Effect[effIndex_2] ||
110        spellInfo_1->EffectItemType[effIndex_1] != spellInfo_2->EffectItemType[effIndex_2] ||
111        spellInfo_1->EffectMiscValue[effIndex_1] != spellInfo_2->EffectMiscValue[effIndex_2] ||
112        spellInfo_1->EffectApplyAuraName[effIndex_1] != spellInfo_2->EffectApplyAuraName[effIndex_2])
113        return false;
114
115    return true;
116}
117
118int32 CompareAuraRanks(uint32 spellId_1, uint32 effIndex_1, uint32 spellId_2, uint32 effIndex_2)
119{
120    SpellEntry const*spellInfo_1 = sSpellStore.LookupEntry(spellId_1);
121    SpellEntry const*spellInfo_2 = sSpellStore.LookupEntry(spellId_2);
122    if(!spellInfo_1 || !spellInfo_2) return 0;
123    if (spellId_1 == spellId_2) return 0;
124
125    int32 diff = spellInfo_1->EffectBasePoints[effIndex_1] - spellInfo_2->EffectBasePoints[effIndex_2];
126    if (spellInfo_1->EffectBasePoints[effIndex_1]+1 < 0 && spellInfo_2->EffectBasePoints[effIndex_2]+1 < 0) return -diff;
127    else return diff;
128}
129
130SpellSpecific GetSpellSpecific(uint32 spellId)
131{
132    SpellEntry const *spellInfo = sSpellStore.LookupEntry(spellId);
133    if(!spellInfo)
134        return SPELL_NORMAL;
135
136    switch(spellInfo->SpellFamilyName)
137    {
138        case SPELLFAMILY_MAGE:
139        {
140            // family flags 18(Molten), 25(Frost/Ice), 28(Mage)
141            if (spellInfo->SpellFamilyFlags & 0x12040000)
142                return SPELL_MAGE_ARMOR;
143
144            if ((spellInfo->SpellFamilyFlags & 0x1000000) && spellInfo->EffectApplyAuraName[0]==SPELL_AURA_MOD_CONFUSE)
145                return SPELL_MAGE_POLYMORPH;
146
147            break;
148        }
149        case SPELLFAMILY_WARRIOR:
150        {
151            if (spellInfo->SpellFamilyFlags & 0x00008000010000LL)
152                return SPELL_POSITIVE_SHOUT;
153
154            break;
155        }
156        case SPELLFAMILY_WARLOCK:
157        {
158            // only warlock curses have this
159            if (spellInfo->Dispel == DISPEL_CURSE)
160                return SPELL_CURSE;
161
162            // family flag 37 (only part spells have family name)
163            if (spellInfo->SpellFamilyFlags & 0x2000000000LL)
164                return SPELL_WARLOCK_ARMOR;
165
166            break;
167        }
168        case SPELLFAMILY_HUNTER:
169        {
170            // only hunter stings have this
171            if (spellInfo->Dispel == DISPEL_POISON)
172                return SPELL_STING;
173
174            break;
175        }
176        case SPELLFAMILY_PALADIN:
177        {
178            if (IsSealSpell(spellInfo))
179                return SPELL_SEAL;
180
181            if (spellInfo->SpellFamilyFlags & 0x10000100LL)
182                return SPELL_BLESSING;
183
184            if ((spellInfo->SpellFamilyFlags & 0x00000820180400LL) && (spellInfo->AttributesEx3 & 0x200))
185                return SPELL_JUDGEMENT;
186
187            for (int i = 0; i < 3; i++)
188            {
189                // only paladin auras have this
190                if (spellInfo->Effect[i] == SPELL_EFFECT_APPLY_AREA_AURA_PARTY)
191                    return SPELL_AURA;
192            }
193            break;
194        }
195        case SPELLFAMILY_SHAMAN:
196        {
197            if (IsElementalShield(spellInfo))
198                return SPELL_ELEMENTAL_SHIELD;
199
200            break;
201        }
202
203        case SPELLFAMILY_POTION:
204            return spellmgr.GetSpellElixirSpecific(spellInfo->Id);
205    }
206
207    // only warlock armor/skin have this (in additional to family cases)
208    if( spellInfo->SpellVisual == 130 && spellInfo->SpellIconID == 89)
209    {
210        return SPELL_WARLOCK_ARMOR;
211    }
212
213    // only hunter aspects have this (but not all aspects in hunter family)
214    if( spellInfo->activeIconID == 122 && (GetSpellSchoolMask(spellInfo) & SPELL_SCHOOL_MASK_NATURE) &&
215        (spellInfo->Attributes & 0x50000) != 0 && (spellInfo->Attributes & 0x9000010) == 0)
216    {
217        return SPELL_ASPECT;
218    }
219
220    for(int i = 0; i < 3; i++)
221        if( spellInfo->Effect[i] == SPELL_EFFECT_APPLY_AURA && (
222        spellInfo->EffectApplyAuraName[i] == SPELL_AURA_TRACK_CREATURES ||
223        spellInfo->EffectApplyAuraName[i] == SPELL_AURA_TRACK_RESOURCES ||
224        spellInfo->EffectApplyAuraName[i] == SPELL_AURA_TRACK_STEALTHED ) )
225            return SPELL_TRACKER;
226
227    // elixirs can have different families, but potion most ofc.
228    if(SpellSpecific sp = spellmgr.GetSpellElixirSpecific(spellInfo->Id))
229        return sp;
230
231    return SPELL_NORMAL;
232}
233
234bool IsSingleFromSpellSpecificPerCaster(uint32 spellSpec1,uint32 spellSpec2)
235{
236    switch(spellSpec1)
237    {
238        case SPELL_SEAL:
239        case SPELL_BLESSING:
240        case SPELL_AURA:
241        case SPELL_STING:
242        case SPELL_CURSE:
243        case SPELL_ASPECT:
244        case SPELL_TRACKER:
245        case SPELL_WARLOCK_ARMOR:
246        case SPELL_MAGE_ARMOR:
247        case SPELL_MAGE_POLYMORPH:
248        case SPELL_POSITIVE_SHOUT:
249        case SPELL_JUDGEMENT:
250            return spellSpec1==spellSpec2;
251        case SPELL_BATTLE_ELIXIR:
252            return spellSpec2==SPELL_BATTLE_ELIXIR
253                || spellSpec2==SPELL_FLASK_ELIXIR;
254        case SPELL_GUARDIAN_ELIXIR:
255            return spellSpec2==SPELL_GUARDIAN_ELIXIR
256                || spellSpec2==SPELL_FLASK_ELIXIR;
257        case SPELL_FLASK_ELIXIR:
258            return spellSpec2==SPELL_BATTLE_ELIXIR
259                || spellSpec2==SPELL_GUARDIAN_ELIXIR
260                || spellSpec2==SPELL_FLASK_ELIXIR;
261        default:
262            return false;
263    }
264}
265
266bool IsPositiveTarget(uint32 targetA, uint32 targetB)
267{
268    // non-positive targets
269    switch(targetA)
270    {
271        case TARGET_CHAIN_DAMAGE:
272        case TARGET_ALL_ENEMY_IN_AREA:
273        case TARGET_ALL_ENEMY_IN_AREA_INSTANT:
274        case TARGET_IN_FRONT_OF_CASTER:
275        case TARGET_ALL_ENEMY_IN_AREA_CHANNELED:
276        case TARGET_CURRENT_ENEMY_COORDINATES:
277        case TARGET_SINGLE_ENEMY:
278            return false;
279        case TARGET_ALL_AROUND_CASTER:
280            return (targetB == TARGET_ALL_PARTY || targetB == TARGET_ALL_FRIENDLY_UNITS_AROUND_CASTER);
281        default:
282            break;
283    }
284    if (targetB)
285        return IsPositiveTarget(targetB, 0);
286    return true;
287}
288
289bool IsPositiveEffect(uint32 spellId, uint32 effIndex)
290{
291    SpellEntry const *spellproto = sSpellStore.LookupEntry(spellId);
292    if (!spellproto) return false;
293
294    switch(spellId)
295    {
296        case 23333:                                         // BG spell
297        case 23335:                                         // BG spell
298        case 34976:                                         // BG spell
299            return true;
300        case 28441:                                         // not positive dummy spell
301        case 37675:                                         // Chaos Blast
302            return false;
303    }
304
305    switch(spellproto->Effect[effIndex])
306    {
307        // always positive effects (check before target checks that provided non-positive result in some case for positive effects)
308        case SPELL_EFFECT_HEAL:
309        case SPELL_EFFECT_LEARN_SPELL:
310        case SPELL_EFFECT_SKILL_STEP:
311        case SPELL_EFFECT_HEAL_PCT:
312        case SPELL_EFFECT_ENERGIZE_PCT:
313            return true;
314
315            // non-positive aura use
316        case SPELL_EFFECT_APPLY_AURA:
317        case SPELL_EFFECT_APPLY_AREA_AURA_FRIEND:
318        {
319            switch(spellproto->EffectApplyAuraName[effIndex])
320            {
321                case SPELL_AURA_DUMMY:
322                {
323                    // dummy aura can be positive or negative dependent from casted spell
324                    switch(spellproto->Id)
325                    {
326                        case 13139:                         // net-o-matic special effect
327                        case 23445:                         // evil twin
328                        case 38637:                         // Nether Exhaustion (red)
329                        case 38638:                         // Nether Exhaustion (green)
330                        case 38639:                         // Nether Exhaustion (blue)
331                            return false;
332                        default:
333                            break;
334                    }
335                }   break;
336                case SPELL_AURA_MOD_STAT:
337                case SPELL_AURA_MOD_DAMAGE_DONE:            // dependent from bas point sign (negative -> negative)
338                case SPELL_AURA_MOD_HEALING_DONE:
339                {
340                    if(spellproto->EffectBasePoints[effIndex]+int32(spellproto->EffectBaseDice[effIndex]) < 0)
341                        return false;
342                    break;
343                }
344                case SPELL_AURA_ADD_TARGET_TRIGGER:
345                    return true;
346                case SPELL_AURA_PERIODIC_TRIGGER_SPELL:
347                    if(spellId != spellproto->EffectTriggerSpell[effIndex])
348                    {
349                        uint32 spellTriggeredId = spellproto->EffectTriggerSpell[effIndex];
350                        SpellEntry const *spellTriggeredProto = sSpellStore.LookupEntry(spellTriggeredId);
351
352                        if(spellTriggeredProto)
353                        {
354                            // non-positive targets of main spell return early
355                            for(int i = 0; i < 3; ++i)
356                            {
357                                // if non-positive trigger cast targeted to positive target this main cast is non-positive
358                                // this will place this spell auras as debuffs
359                                if(IsPositiveTarget(spellTriggeredProto->EffectImplicitTargetA[effIndex],spellTriggeredProto->EffectImplicitTargetB[effIndex]) && !IsPositiveEffect(spellTriggeredId,i))
360                                    return false;
361                            }
362                        }
363                    }
364                    break;
365                case SPELL_AURA_PROC_TRIGGER_SPELL:
366                    // many positive auras have negative triggered spells at damage for example and this not make it negative (it can be canceled for example)
367                    break;
368                case SPELL_AURA_MOD_STUN:                   //have positive and negative spells, we can't sort its correctly at this moment.
369                    if(effIndex==0 && spellproto->Effect[1]==0 && spellproto->Effect[2]==0)
370                        return false;                       // but all single stun aura spells is negative
371
372                    // Petrification
373                    if(spellproto->Id == 17624)
374                        return false;
375                    break;
376                case SPELL_AURA_MOD_ROOT:
377                case SPELL_AURA_MOD_SILENCE:
378                case SPELL_AURA_GHOST:
379                case SPELL_AURA_PERIODIC_LEECH:
380                case SPELL_AURA_MOD_PACIFY_SILENCE:
381                case SPELL_AURA_MOD_STALKED:
382                case SPELL_AURA_PERIODIC_DAMAGE_PERCENT:
383                    return false;
384                case SPELL_AURA_PERIODIC_DAMAGE:            // used in positive spells also.
385                    // part of negative spell if casted at self (prevent cancel)
386                    if(spellproto->EffectImplicitTargetA[effIndex] == TARGET_SELF)
387                        return false;
388                    break;
389                case SPELL_AURA_MOD_DECREASE_SPEED:         // used in positive spells also
390                    // part of positive spell if casted at self
391                    if(spellproto->EffectImplicitTargetA[effIndex] != TARGET_SELF)
392                        return false;
393                    // but not this if this first effect (don't found batter check)
394                    if(spellproto->Attributes & 0x4000000 && effIndex==0)
395                        return false;
396                    break;
397                case SPELL_AURA_TRANSFORM:
398                    // some spells negative
399                    switch(spellproto->Id)
400                    {
401                        case 36897:                         // Transporter Malfunction (race mutation to horde)
402                        case 36899:                         // Transporter Malfunction (race mutation to alliance)
403                            return false;
404                    }
405                    break;
406                case SPELL_AURA_MOD_SCALE:
407                    // some spells negative
408                    switch(spellproto->Id)
409                    {
410                        case 36900:                         // Soul Split: Evil!
411                        case 36901:                         // Soul Split: Good
412                        case 36893:                         // Transporter Malfunction (decrease size case)
413                        case 36895:                         // Transporter Malfunction (increase size case)
414                            return false;
415                    }
416                    break;
417                case SPELL_AURA_MECHANIC_IMMUNITY:
418                {
419                    // non-positive immunities
420                    switch(spellproto->EffectMiscValue[effIndex])
421                    {
422                        case MECHANIC_BANDAGE:
423                        case MECHANIC_SHIELD:
424                        case MECHANIC_MOUNT:
425                        case MECHANIC_INVULNERABILITY:
426                            return false;
427                        default:
428                            break;
429                    }
430                }   break;
431                case SPELL_AURA_ADD_FLAT_MODIFIER:          // mods
432                case SPELL_AURA_ADD_PCT_MODIFIER:
433                {
434                    // non-positive mods
435                    switch(spellproto->EffectMiscValue[effIndex])
436                    {
437                        case SPELLMOD_COST:                 // dependent from bas point sign (negative -> positive)
438                            if(spellproto->EffectBasePoints[effIndex]+int32(spellproto->EffectBaseDice[effIndex]) > 0)
439                                return false;
440                            break;
441                        default:
442                            break;
443                    }
444                }   break;
445                case SPELL_AURA_MOD_HEALING_PCT:
446                    if(spellproto->EffectBasePoints[effIndex]+int32(spellproto->EffectBaseDice[effIndex]) < 0)
447                        return false;
448                    break;
449                case SPELL_AURA_MOD_SKILL:
450                    if(spellproto->EffectBasePoints[effIndex]+int32(spellproto->EffectBaseDice[effIndex]) < 0)
451                        return false;
452                    break;
453                case SPELL_AURA_FORCE_REACTION:
454                    if(spellproto->Id==42792)               // Recently Dropped Flag (prevent cancel)
455                        return false;
456                    break;
457                default:
458                    break;
459            }
460            break;
461        }
462        default:
463            break;
464    }
465
466    // non-positive targets
467    if(!IsPositiveTarget(spellproto->EffectImplicitTargetA[effIndex],spellproto->EffectImplicitTargetB[effIndex]))
468        return false;
469
470    // AttributesEx check
471    if(spellproto->AttributesEx & (1<<7))
472        return false;
473
474    // ok, positive
475    return true;
476}
477
478bool IsPositiveSpell(uint32 spellId)
479{
480    SpellEntry const *spellproto = sSpellStore.LookupEntry(spellId);
481    if (!spellproto) return false;
482
483    // spells with atleast one negative effect are considered negative
484    // some self-applied spells have negative effects but in self casting case negative check ignored.
485    for (int i = 0; i < 3; i++)
486        if (!IsPositiveEffect(spellId, i))
487            return false;
488    return true;
489}
490
491bool IsSingleTargetSpell(SpellEntry const *spellInfo)
492{
493    // all other single target spells have if it has AttributesEx5
494    if ( spellInfo->AttributesEx5 & SPELL_ATTR_EX5_SINGLE_TARGET_SPELL )
495        return true;
496
497    // TODO - need found Judgements rule
498    switch(GetSpellSpecific(spellInfo->Id))
499    {
500        case SPELL_JUDGEMENT:
501            return true;
502    }
503
504    // single target triggered spell.
505    // Not real client side single target spell, but it' not triggered until prev. aura expired.
506    // This is allow store it in single target spells list for caster for spell proc checking
507    if(spellInfo->Id==38324)                                // Regeneration (triggered by 38299 (HoTs on Heals))
508        return true;
509
510    return false;
511}
512
513bool IsSingleTargetSpells(SpellEntry const *spellInfo1, SpellEntry const *spellInfo2)
514{
515    // TODO - need better check
516    // Equal icon and spellfamily
517    if( spellInfo1->SpellFamilyName == spellInfo2->SpellFamilyName &&
518        spellInfo1->SpellIconID == spellInfo2->SpellIconID )
519        return true;
520
521    // TODO - need found Judgements rule
522    SpellSpecific spec1 = GetSpellSpecific(spellInfo1->Id);
523    // spell with single target specific types
524    switch(spec1)
525    {
526        case SPELL_JUDGEMENT:
527            if(GetSpellSpecific(spellInfo2->Id) == spec1)
528                return true;
529            break;
530    }
531
532    return false;
533}
534
535uint8 GetErrorAtShapeshiftedCast (SpellEntry const *spellInfo, uint32 form)
536{
537    // talents that learn spells can have stance requirements that need ignore
538    // (this requirement only for client-side stance show in talent description)
539    if( GetTalentSpellCost(spellInfo->Id) > 0 &&
540        (spellInfo->Effect[0]==SPELL_EFFECT_LEARN_SPELL || spellInfo->Effect[1]==SPELL_EFFECT_LEARN_SPELL || spellInfo->Effect[2]==SPELL_EFFECT_LEARN_SPELL) )
541        return 0;
542
543    uint32 stanceMask = (form ? 1 << (form - 1) : 0);
544
545    if (stanceMask & spellInfo->StancesNot)                 // can explicitly not be casted in this stance
546        return SPELL_FAILED_NOT_SHAPESHIFT;
547
548    if (stanceMask & spellInfo->Stances)                    // can explicitly be casted in this stance
549        return 0;
550
551    bool actAsShifted = false;
552    if (form > 0)
553    {
554        SpellShapeshiftEntry const *shapeInfo = sSpellShapeshiftStore.LookupEntry(form);
555        if (!shapeInfo)
556        {
557            sLog.outError("GetErrorAtShapeshiftedCast: unknown shapeshift %u", form);
558            return 0;
559        }
560        actAsShifted = !(shapeInfo->flags1 & 1);            // shapeshift acts as normal form for spells
561    }
562
563    if(actAsShifted)
564    {
565        if (spellInfo->Attributes & SPELL_ATTR_NOT_SHAPESHIFT) // not while shapeshifted
566            return SPELL_FAILED_NOT_SHAPESHIFT;
567        else if (spellInfo->Stances != 0)                   // needs other shapeshift
568            return SPELL_FAILED_ONLY_SHAPESHIFT;
569    }
570    else
571    {
572        // needs shapeshift
573        if(!(spellInfo->AttributesEx2 & SPELL_ATTR_EX2_NOT_NEED_SHAPESHIFT) && spellInfo->Stances != 0)
574            return SPELL_FAILED_ONLY_SHAPESHIFT;
575    }
576
577    return 0;
578}
579
580void SpellMgr::LoadSpellTargetPositions()
581{
582    mSpellTargetPositions.clear();                                // need for reload case
583
584    uint32 count = 0;
585
586    //                                                0   1           2                  3                  4                  5
587    QueryResult *result = WorldDatabase.Query("SELECT id, target_map, target_position_x, target_position_y, target_position_z, target_orientation FROM spell_target_position");
588    if( !result )
589    {
590
591        barGoLink bar( 1 );
592
593        bar.step();
594
595        sLog.outString();
596        sLog.outString( ">> Loaded %u spell target coordinates", count );
597        return;
598    }
599
600    barGoLink bar( result->GetRowCount() );
601
602    do
603    {
604        Field *fields = result->Fetch();
605
606        bar.step();
607
608        ++count;
609
610        uint32 Spell_ID = fields[0].GetUInt32();
611
612        SpellTargetPosition st;
613
614        st.target_mapId       = fields[1].GetUInt32();
615        st.target_X           = fields[2].GetFloat();
616        st.target_Y           = fields[3].GetFloat();
617        st.target_Z           = fields[4].GetFloat();
618        st.target_Orientation = fields[5].GetFloat();
619
620        SpellEntry const* spellInfo = sSpellStore.LookupEntry(Spell_ID);
621        if(!spellInfo)
622        {
623            sLog.outErrorDb("Spell (ID:%u) listed in `spell_target_position` does not exist.",Spell_ID);
624            continue;
625        }
626
627        bool found = false;
628        for(int i = 0; i < 3; ++i)
629        {
630            if( spellInfo->EffectImplicitTargetA[i]==TARGET_TABLE_X_Y_Z_COORDINATES || spellInfo->EffectImplicitTargetB[i]==TARGET_TABLE_X_Y_Z_COORDINATES )
631            {
632                found = true;
633                break;
634            }
635        }
636        if(!found)
637        {
638            sLog.outErrorDb("Spell (Id: %u) listed in `spell_target_position` does not have target TARGET_TABLE_X_Y_Z_COORDINATES (17).",Spell_ID);
639            continue;
640        }
641
642        MapEntry const* mapEntry = sMapStore.LookupEntry(st.target_mapId);
643        if(!mapEntry)
644        {
645            sLog.outErrorDb("Spell (ID:%u) target map (ID: %u) does not exist in `Map.dbc`.",Spell_ID,st.target_mapId);
646            continue;
647        }
648
649        if(st.target_X==0 && st.target_Y==0 && st.target_Z==0)
650        {
651            sLog.outErrorDb("Spell (ID:%u) target coordinates not provided.",Spell_ID);
652            continue;
653        }
654
655        mSpellTargetPositions[Spell_ID] = st;
656
657    } while( result->NextRow() );
658
659    delete result;
660
661    sLog.outString();
662    sLog.outString( ">> Loaded %u spell teleport coordinates", count );
663}
664
665void SpellMgr::LoadSpellAffects()
666{
667    mSpellAffectMap.clear();                                // need for reload case
668
669    uint32 count = 0;
670
671    //                                                0      1         2
672    QueryResult *result = WorldDatabase.Query("SELECT entry, effectId, SpellFamilyMask FROM spell_affect");
673    if( !result )
674    {
675
676        barGoLink bar( 1 );
677
678        bar.step();
679
680        sLog.outString();
681        sLog.outString( ">> Loaded %u spell affect definitions", count );
682        return;
683    }
684
685    barGoLink bar( result->GetRowCount() );
686
687    do
688    {
689        Field *fields = result->Fetch();
690
691        bar.step();
692
693        uint16 entry = fields[0].GetUInt16();
694        uint8 effectId = fields[1].GetUInt8();
695
696        SpellEntry const* spellInfo = sSpellStore.LookupEntry(entry);
697
698        if (!spellInfo)
699        {
700            sLog.outErrorDb("Spell %u listed in `spell_affect` does not exist", entry);
701            continue;
702        }
703
704        if (effectId >= 3)
705        {
706            sLog.outErrorDb("Spell %u listed in `spell_affect` have invalid effect index (%u)", entry,effectId);
707            continue;
708        }
709
710        if( spellInfo->Effect[effectId] != SPELL_EFFECT_APPLY_AURA ||
711            spellInfo->EffectApplyAuraName[effectId] != SPELL_AURA_ADD_FLAT_MODIFIER &&
712            spellInfo->EffectApplyAuraName[effectId] != SPELL_AURA_ADD_PCT_MODIFIER  &&
713            spellInfo->EffectApplyAuraName[effectId] != SPELL_AURA_ADD_TARGET_TRIGGER )
714        {
715            sLog.outErrorDb("Spell %u listed in `spell_affect` have not SPELL_AURA_ADD_FLAT_MODIFIER (%u) or SPELL_AURA_ADD_PCT_MODIFIER (%u) or SPELL_AURA_ADD_TARGET_TRIGGER (%u) for effect index (%u)", entry,SPELL_AURA_ADD_FLAT_MODIFIER,SPELL_AURA_ADD_PCT_MODIFIER,SPELL_AURA_ADD_TARGET_TRIGGER,effectId);
716            continue;
717        }
718
719        uint64 spellAffectMask = fields[2].GetUInt64();
720
721        // Spell.dbc have own data for low part of SpellFamilyMask
722        if( spellInfo->EffectItemType[effectId])
723        {
724            if(spellInfo->EffectItemType[effectId] == spellAffectMask)
725            {
726                sLog.outErrorDb("Spell %u listed in `spell_affect` have redundant (same with EffectItemType%d) data for effect index (%u) and not needed, skipped.", entry,effectId+1,effectId);
727                continue;
728            }
729
730            // 24429 have wrong data in EffectItemType and overwrites by DB, possible bug in client
731            if(spellInfo->Id!=24429 && spellInfo->EffectItemType[effectId] != spellAffectMask)
732            {
733                sLog.outErrorDb("Spell %u listed in `spell_affect` have different low part from EffectItemType%d for effect index (%u) and not needed, skipped.", entry,effectId+1,effectId);
734                continue;
735            }
736        }
737
738        mSpellAffectMap.insert(SpellAffectMap::value_type((entry<<8) + effectId,spellAffectMask));
739
740        ++count;
741    } while( result->NextRow() );
742
743    delete result;
744
745    sLog.outString();
746    sLog.outString( ">> Loaded %u spell affect definitions", count );
747
748    for (uint32 id = 0; id < sSpellStore.GetNumRows(); ++id)
749    {
750        SpellEntry const* spellInfo = sSpellStore.LookupEntry(id);
751        if (!spellInfo)
752            continue;
753
754        for (int effectId = 0; effectId < 3; ++effectId)
755        {
756            if( spellInfo->Effect[effectId] != SPELL_EFFECT_APPLY_AURA ||
757                (spellInfo->EffectApplyAuraName[effectId] != SPELL_AURA_ADD_FLAT_MODIFIER &&
758                spellInfo->EffectApplyAuraName[effectId] != SPELL_AURA_ADD_PCT_MODIFIER  &&
759                spellInfo->EffectApplyAuraName[effectId] != SPELL_AURA_ADD_TARGET_TRIGGER) )
760                continue;
761
762            if(spellInfo->EffectItemType[effectId] != 0)
763                continue;
764
765            if(mSpellAffectMap.find((id<<8) + effectId) !=  mSpellAffectMap.end())
766                continue;
767
768            sLog.outErrorDb("Spell %u (%s) misses spell_affect for effect %u",id,spellInfo->SpellName[sWorld.GetDefaultDbcLocale()], effectId);
769        }
770    }
771}
772
773bool SpellMgr::IsAffectedBySpell(SpellEntry const *spellInfo, uint32 spellId, uint8 effectId, uint64 familyFlags) const
774{
775    // false for spellInfo == NULL
776    if (!spellInfo)
777        return false;
778
779    SpellEntry const *affect_spell = sSpellStore.LookupEntry(spellId);
780    // false for affect_spell == NULL
781    if (!affect_spell)
782        return false;
783
784    // False if spellFamily not equal
785    if (affect_spell->SpellFamilyName != spellInfo->SpellFamilyName)
786        return false;
787
788    // If familyFlags == 0
789    if (!familyFlags)
790    {
791        // Get it from spellAffect table
792        familyFlags = GetSpellAffectMask(spellId,effectId);
793        // false if familyFlags == 0
794        if (!familyFlags)
795            return false;
796    }
797
798    // true
799    if (familyFlags & spellInfo->SpellFamilyFlags)
800        return true;
801
802    return false;
803}
804
805void SpellMgr::LoadSpellProcEvents()
806{
807    mSpellProcEventMap.clear();                             // need for reload case
808
809    uint32 count = 0;
810
811    //                                                0      1           2         3        4                5                6          7        8
812    QueryResult *result = WorldDatabase.Query("SELECT entry, SchoolMask, Category, SkillID, SpellFamilyName, SpellFamilyMask, procFlags, ppmRate, cooldown FROM spell_proc_event");
813    if( !result )
814    {
815
816        barGoLink bar( 1 );
817
818        bar.step();
819
820        sLog.outString();
821        sLog.outString( ">> Loaded %u spell proc event conditions", count  );
822        return;
823    }
824
825    barGoLink bar( result->GetRowCount() );
826
827    do
828    {
829        Field *fields = result->Fetch();
830
831        bar.step();
832
833        uint16 entry = fields[0].GetUInt16();
834
835        if (!sSpellStore.LookupEntry(entry))
836        {
837            sLog.outErrorDb("Spell %u listed in `spell_proc_event` does not exist", entry);
838            continue;
839        }
840
841        SpellProcEventEntry spe;
842
843        spe.schoolMask      = fields[1].GetUInt32();
844        spe.category        = fields[2].GetUInt32();
845        spe.skillId         = fields[3].GetUInt32();
846        spe.spellFamilyName = fields[4].GetUInt32();
847        spe.spellFamilyMask = fields[5].GetUInt64();
848        spe.procFlags       = fields[6].GetUInt32();
849        spe.ppmRate         = fields[7].GetFloat();
850        spe.cooldown        = fields[8].GetUInt32();
851
852        mSpellProcEventMap[entry] = spe;
853
854        ++count;
855    } while( result->NextRow() );
856
857    delete result;
858
859    sLog.outString();
860    sLog.outString( ">> Loaded %u spell proc event conditions", count  );
861
862    /*
863    // Commented for now, as it still produces many errors (still quite many spells miss spell_proc_event)
864    for (uint32 id = 0; id < sSpellStore.GetNumRows(); ++id)
865    {
866        SpellEntry const* spellInfo = sSpellStore.LookupEntry(id);
867        if (!spellInfo)
868            continue;
869
870        bool found = false;
871        for (int effectId = 0; effectId < 3; ++effectId)
872        {
873            // at this moment check only SPELL_AURA_PROC_TRIGGER_SPELL
874            if( spellInfo->EffectApplyAuraName[effectId] == SPELL_AURA_PROC_TRIGGER_SPELL )
875            {
876                found = true;
877                break;
878            }
879        }
880
881        if(!found)
882            continue;
883
884        if(GetSpellProcEvent(id))
885            continue;
886
887        sLog.outErrorDb("Spell %u (%s) misses spell_proc_event",id,spellInfo->SpellName[sWorld.GetDBClang()]);
888    }
889    */
890}
891
892bool SpellMgr::IsSpellProcEventCanTriggeredBy( SpellProcEventEntry const * spellProcEvent, SpellEntry const * procSpell, uint32 procFlags )
893{
894    if((procFlags & spellProcEvent->procFlags) == 0)
895        return false;
896
897    // Additional checks in case spell cast/hit/crit is the event
898    // Check (if set) school, category, skill line, spell talent mask
899    if(spellProcEvent->schoolMask && (!procSpell || (GetSpellSchoolMask(procSpell) & spellProcEvent->schoolMask) == 0))
900        return false;
901    if(spellProcEvent->category && (!procSpell || procSpell->Category != spellProcEvent->category))
902        return false;
903    if(spellProcEvent->skillId)
904    {
905        if (!procSpell)
906            return false;
907
908        SkillLineAbilityMap::const_iterator lower = spellmgr.GetBeginSkillLineAbilityMap(procSpell->Id);
909        SkillLineAbilityMap::const_iterator upper = spellmgr.GetEndSkillLineAbilityMap(procSpell->Id);
910
911        bool found = false;
912        for(SkillLineAbilityMap::const_iterator _spell_idx = lower; _spell_idx != upper; ++_spell_idx)
913        {
914            if(_spell_idx->second->skillId == spellProcEvent->skillId)
915            {
916                found = true;
917                break;
918            }
919        }
920        if (!found)
921            return false;
922    }
923    if(spellProcEvent->spellFamilyName && (!procSpell || spellProcEvent->spellFamilyName != procSpell->SpellFamilyName))
924        return false;
925    if(spellProcEvent->spellFamilyMask && (!procSpell || (spellProcEvent->spellFamilyMask & procSpell->SpellFamilyFlags) == 0))
926        return false;
927
928    return true;
929}
930
931void SpellMgr::LoadSpellElixirs()
932{
933    mSpellElixirs.clear();                                  // need for reload case
934
935    uint32 count = 0;
936
937    //                                                0      1
938    QueryResult *result = WorldDatabase.Query("SELECT entry, mask FROM spell_elixir");
939    if( !result )
940    {
941
942        barGoLink bar( 1 );
943
944        bar.step();
945
946        sLog.outString();
947        sLog.outString( ">> Loaded %u spell elixir definitions", count );
948        return;
949    }
950
951    barGoLink bar( result->GetRowCount() );
952
953    do
954    {
955        Field *fields = result->Fetch();
956
957        bar.step();
958
959        uint16 entry = fields[0].GetUInt16();
960        uint8 mask = fields[1].GetUInt8();
961
962        SpellEntry const* spellInfo = sSpellStore.LookupEntry(entry);
963
964        if (!spellInfo)
965        {
966            sLog.outErrorDb("Spell %u listed in `spell_elixir` does not exist", entry);
967            continue;
968        }
969
970        mSpellElixirs[entry] = mask;
971
972        ++count;
973    } while( result->NextRow() );
974
975    delete result;
976
977    sLog.outString();
978    sLog.outString( ">> Loaded %u spell elixir definitions", count );
979}
980
981void SpellMgr::LoadSpellThreats()
982{
983    sSpellThreatStore.Free();                               // for reload
984
985    sSpellThreatStore.Load();
986
987    sLog.outString( ">> Loaded %u aggro generating spells", sSpellThreatStore.RecordCount );
988    sLog.outString();
989}
990
991bool SpellMgr::IsRankSpellDueToSpell(SpellEntry const *spellInfo_1,uint32 spellId_2) const
992{
993    SpellEntry const *spellInfo_2 = sSpellStore.LookupEntry(spellId_2);
994    if(!spellInfo_1 || !spellInfo_2) return false;
995    if(spellInfo_1->Id == spellId_2) return false;
996
997    return GetFirstSpellInChain(spellInfo_1->Id)==GetFirstSpellInChain(spellId_2);
998}
999
1000bool SpellMgr::canStackSpellRanks(SpellEntry const *spellInfo)
1001{
1002    if(spellInfo->powerType != POWER_MANA && spellInfo->powerType != POWER_HEALTH)
1003        return false;
1004    if(IsProfessionSpell(spellInfo->Id))
1005        return false;
1006
1007    // All stance spells. if any better way, change it.
1008    for (int i = 0; i < 3; i++)
1009    {
1010        // Paladin aura Spell
1011        if(spellInfo->SpellFamilyName == SPELLFAMILY_PALADIN
1012            && spellInfo->Effect[i]==SPELL_EFFECT_APPLY_AREA_AURA_PARTY)
1013            return false;
1014        // Druid form Spell
1015        if(spellInfo->SpellFamilyName == SPELLFAMILY_DRUID
1016            && spellInfo->Effect[i]==SPELL_EFFECT_APPLY_AURA
1017            && spellInfo->EffectApplyAuraName[i] == SPELL_AURA_MOD_SHAPESHIFT)
1018            return false;
1019        // Rogue Stealth
1020        if(spellInfo->SpellFamilyName == SPELLFAMILY_ROGUE
1021            && spellInfo->Effect[i]==SPELL_EFFECT_APPLY_AURA
1022            && spellInfo->EffectApplyAuraName[i] == SPELL_AURA_MOD_SHAPESHIFT)
1023            return false;
1024    }
1025    return true;
1026}
1027
1028bool SpellMgr::IsNoStackSpellDueToSpell(uint32 spellId_1, uint32 spellId_2) const
1029{
1030    SpellEntry const *spellInfo_1 = sSpellStore.LookupEntry(spellId_1);
1031    SpellEntry const *spellInfo_2 = sSpellStore.LookupEntry(spellId_2);
1032
1033    if(!spellInfo_1 || !spellInfo_2)
1034        return false;
1035
1036    if(spellInfo_1->Id == spellId_2) //checked before
1037        return false;
1038
1039    if(spellInfo_1->SpellFamilyName && spellInfo_1->SpellFamilyName == spellInfo_2->SpellFamilyName) //resurrection sickness
1040    {
1041        if(spellInfo_1->SpellFamilyFlags == spellInfo_2->SpellFamilyFlags)
1042            return true;
1043        //Corruption & Seed of corruption
1044        if(spellInfo_1->SpellFamilyName == SPELLFAMILY_WARLOCK)
1045            if( spellInfo_1->SpellIconID == 313 && spellInfo_2->SpellIconID == 1932 ||
1046                spellInfo_2->SpellIconID == 313 && spellInfo_1->SpellIconID == 1932 )
1047                if(spellInfo_1->SpellVisual != 0 && spellInfo_2->SpellVisual != 0)
1048                    return true;                        // can't be stacked
1049    }
1050
1051    //I think we don't check this correctly because i need a exception for spell:
1052    //72,11327,18461...(called from 1856,1857...) Call Aura 16,31, after trigger another spell who call aura 77 and 77 remove 16 and 31, this should not happen.
1053    //if(spellInfo_2->SpellFamilyFlags == 2048)
1054    //    return false;
1055
1056    // Resurrection sickness
1057    //if((spellInfo_1->Id == SPELL_ID_PASSIVE_RESURRECTION_SICKNESS) != (spellInfo_2->Id==SPELL_ID_PASSIVE_RESURRECTION_SICKNESS))
1058    //    return false;
1059
1060    // Specific spell family spells
1061    /*switch(spellInfo_1->SpellFamilyName)
1062    {
1063        case SPELLFAMILY_GENERIC:
1064            switch(spellInfo_2->SpellFamilyName)
1065            {
1066                case SPELLFAMILY_GENERIC:                   // same family case
1067                {
1068                    // Thunderfury
1069                    if( spellInfo_1->Id == 21992 && spellInfo_2->Id == 27648 || spellInfo_2->Id == 21992 && spellInfo_1->Id == 27648 )
1070                        return false;
1071
1072                    // Lightning Speed (Mongoose) and Fury of the Crashing Waves (Tsunami Talisman)
1073                    if( spellInfo_1->Id == 28093 && spellInfo_2->Id == 42084 ||
1074                        spellInfo_2->Id == 28093 && spellInfo_1->Id == 42084 )
1075                        return false;
1076
1077                    // Soulstone Resurrection and Twisting Nether (resurrector)
1078                    if( spellInfo_1->SpellIconID == 92 && spellInfo_2->SpellIconID == 92 && (
1079                        spellInfo_1->SpellVisual == 99 && spellInfo_2->SpellVisual == 0 ||
1080                        spellInfo_2->SpellVisual == 99 && spellInfo_1->SpellVisual == 0 ) )
1081                        return false;
1082
1083                    // Heart of the Wild and (Primal Instinct (Idol of Terror) triggering spell or Agility)
1084                    if( spellInfo_1->SpellIconID == 240 && spellInfo_2->SpellIconID == 240 && (
1085                        spellInfo_1->SpellVisual == 0 && spellInfo_2->SpellVisual == 78 ||
1086                        spellInfo_2->SpellVisual == 0 && spellInfo_1->SpellVisual == 78 ) )
1087                        return false;
1088
1089                    // Personalized Weather (thunder effect should overwrite rainy aura)
1090                    if(spellInfo_1->SpellIconID == 2606 && spellInfo_2->SpellIconID == 2606)
1091                        return false;
1092
1093                    // Brood Affliction: Bronze
1094                    if( (spellInfo_1->Id == 23170 && spellInfo_2->Id == 23171) ||
1095                        (spellInfo_2->Id == 23170 && spellInfo_1->Id == 23171) )
1096                        return false;
1097
1098                    break;
1099                }
1100                case SPELLFAMILY_WARRIOR:
1101                {
1102                    // Scroll of Protection and Defensive Stance (multi-family check)
1103                    if( spellInfo_1->SpellIconID == 276 && spellInfo_1->SpellVisual == 196 && spellInfo_2->Id == 71)
1104                        return false;
1105
1106                    // Improved Hamstring -> Hamstring (multi-family check)
1107                    if( (spellInfo_2->SpellFamilyFlags & 2) && spellInfo_1->Id == 23694 )
1108                        return false;
1109
1110                    break;
1111                }
1112                case SPELLFAMILY_DRUID:
1113                {
1114                    // Scroll of Stamina and Leader of the Pack (multi-family check)
1115                    if( spellInfo_1->SpellIconID == 312 && spellInfo_1->SpellVisual == 216 && spellInfo_2->Id == 24932 )
1116                        return false;
1117
1118                    // Dragonmaw Illusion (multi-family check)
1119                    if (spellId_1 == 40216 && spellId_2 == 42016 )
1120                        return false;
1121
1122                    break;
1123                }
1124                case SPELLFAMILY_ROGUE:
1125                {
1126                    // Garrote-Silence -> Garrote (multi-family check)
1127                    if( spellInfo_1->SpellIconID == 498 && spellInfo_1->SpellVisual == 0 && spellInfo_2->SpellIconID == 498  )
1128                        return false;
1129
1130                    break;
1131                }
1132                case SPELLFAMILY_HUNTER:
1133                {
1134                    // Concussive Shot and Imp. Concussive Shot (multi-family check)
1135                    if( spellInfo_1->Id == 19410 && spellInfo_2->Id == 5116 )
1136                        return false;
1137
1138                    // Improved Wing Clip -> Wing Clip (multi-family check)
1139                    if( (spellInfo_2->SpellFamilyFlags & 0x40) && spellInfo_1->Id == 19229 )
1140                        return false;
1141                    break;
1142                }
1143                case SPELLFAMILY_PALADIN:
1144                {
1145                    // Unstable Currents and other -> *Sanctity Aura (multi-family check)
1146                    if( spellInfo_2->SpellIconID==502 && spellInfo_1->SpellIconID==502 && spellInfo_1->SpellVisual==969 )
1147                        return false;
1148
1149                    // *Band of Eternal Champion and Seal of Command(multi-family check)
1150                    if( spellId_1 == 35081 && spellInfo_2->SpellIconID==561 && spellInfo_2->SpellVisual==7992)
1151                        return false;
1152
1153                    break;
1154                }
1155            }
1156            break;
1157        case SPELLFAMILY_MAGE:
1158            if( spellInfo_2->SpellFamilyName == SPELLFAMILY_MAGE )
1159            {
1160                // Blizzard & Chilled (and some other stacked with blizzard spells
1161                if( (spellInfo_1->SpellFamilyFlags & 0x80) && (spellInfo_2->SpellFamilyFlags & 0x100000) ||
1162                    (spellInfo_2->SpellFamilyFlags & 0x80) && (spellInfo_1->SpellFamilyFlags & 0x100000) )
1163                    return false;
1164
1165                // Blink & Improved Blink
1166                if( (spellInfo_1->SpellFamilyFlags & 0x0000000000010000LL) && (spellInfo_2->SpellVisual == 72 && spellInfo_2->SpellIconID == 1499) ||
1167                    (spellInfo_2->SpellFamilyFlags & 0x0000000000010000LL) && (spellInfo_1->SpellVisual == 72 && spellInfo_1->SpellIconID == 1499) )
1168                    return false;
1169            }
1170            // Detect Invisibility and Mana Shield (multi-family check)
1171            if( spellInfo_2->Id == 132 && spellInfo_1->SpellIconID == 209 && spellInfo_1->SpellVisual == 968 )
1172                return false;
1173
1174            // Combustion and Fire Protection Aura (multi-family check)
1175            if( spellInfo_1->Id == 11129 && spellInfo_2->SpellIconID == 33 && spellInfo_2->SpellVisual == 321 )
1176                return false;
1177
1178            break;
1179        case SPELLFAMILY_WARLOCK:
1180            if( spellInfo_2->SpellFamilyName == SPELLFAMILY_WARLOCK )
1181            {
1182                // Siphon Life and Drain Life
1183                if( spellInfo_1->SpellIconID == 152 && spellInfo_2->SpellIconID == 546 ||
1184                    spellInfo_2->SpellIconID == 152 && spellInfo_1->SpellIconID == 546 )
1185                    return false;
1186
1187                //Corruption & Seed of corruption
1188                if( spellInfo_1->SpellIconID == 313 && spellInfo_2->SpellIconID == 1932 ||
1189                    spellInfo_2->SpellIconID == 313 && spellInfo_1->SpellIconID == 1932 )
1190                    if(spellInfo_1->SpellVisual != 0 && spellInfo_2->SpellVisual != 0)
1191                        return true;                        // can't be stacked
1192
1193                // Corruption and Unstable Affliction
1194                if( spellInfo_1->SpellIconID == 313 && spellInfo_2->SpellIconID == 2039 ||
1195                    spellInfo_2->SpellIconID == 313 && spellInfo_1->SpellIconID == 2039 )
1196                    return false;
1197
1198                // (Corruption or Unstable Affliction) and (Curse of Agony or Curse of Doom)
1199                if( (spellInfo_1->SpellIconID == 313 || spellInfo_1->SpellIconID == 2039) && (spellInfo_2->SpellIconID == 544  || spellInfo_2->SpellIconID == 91) ||
1200                    (spellInfo_2->SpellIconID == 313 || spellInfo_2->SpellIconID == 2039) && (spellInfo_1->SpellIconID == 544  || spellInfo_1->SpellIconID == 91) )
1201                    return false;
1202            }
1203            // Detect Invisibility and Mana Shield (multi-family check)
1204            if( spellInfo_1->Id == 132 && spellInfo_2->SpellIconID == 209 && spellInfo_2->SpellVisual == 968 )
1205                return false;
1206            break;
1207        case SPELLFAMILY_WARRIOR:
1208            if( spellInfo_2->SpellFamilyName == SPELLFAMILY_WARRIOR )
1209            {
1210                // Rend and Deep Wound
1211                if( (spellInfo_1->SpellFamilyFlags & 0x20) && (spellInfo_2->SpellFamilyFlags & 0x1000000000LL) ||
1212                    (spellInfo_2->SpellFamilyFlags & 0x20) && (spellInfo_1->SpellFamilyFlags & 0x1000000000LL) )
1213                    return false;
1214
1215                // Battle Shout and Rampage
1216                if( (spellInfo_1->SpellIconID == 456 && spellInfo_2->SpellIconID == 2006) ||
1217                    (spellInfo_2->SpellIconID == 456 && spellInfo_1->SpellIconID == 2006) )
1218                    return false;
1219            }
1220
1221            // Hamstring -> Improved Hamstring (multi-family check)
1222            if( (spellInfo_1->SpellFamilyFlags & 2) && spellInfo_2->Id == 23694 )
1223                return false;
1224
1225            // Defensive Stance and Scroll of Protection (multi-family check)
1226            if( spellInfo_1->Id == 71 && spellInfo_2->SpellIconID == 276 && spellInfo_2->SpellVisual == 196 )
1227                return false;
1228
1229            // Bloodlust and Bloodthirst (multi-family check)
1230            if( spellInfo_2->Id == 2825 && spellInfo_1->SpellIconID == 38 && spellInfo_1->SpellVisual == 0 )
1231                return false;
1232
1233            break;
1234        case SPELLFAMILY_PRIEST:
1235            if( spellInfo_2->SpellFamilyName == SPELLFAMILY_PRIEST )
1236            {
1237                //Devouring Plague and Shadow Vulnerability
1238                if( (spellInfo_1->SpellFamilyFlags & 0x2000000) && (spellInfo_2->SpellFamilyFlags & 0x800000000LL) ||
1239                    (spellInfo_2->SpellFamilyFlags & 0x2000000) && (spellInfo_1->SpellFamilyFlags & 0x800000000LL) )
1240                    return false;
1241
1242                //StarShards and Shadow Word: Pain
1243                if( (spellInfo_1->SpellFamilyFlags & 0x200000) && (spellInfo_2->SpellFamilyFlags & 0x8000) ||
1244                    (spellInfo_2->SpellFamilyFlags & 0x200000) && (spellInfo_1->SpellFamilyFlags & 0x8000) )
1245                    return false;
1246            }
1247            break;
1248        case SPELLFAMILY_DRUID:
1249            if( spellInfo_2->SpellFamilyName == SPELLFAMILY_DRUID )
1250            {
1251                //Omen of Clarity and Blood Frenzy
1252                if( (spellInfo_1->SpellFamilyFlags == 0x0 && spellInfo_1->SpellIconID == 108) && (spellInfo_2->SpellFamilyFlags & 0x20000000000000LL) ||
1253                    (spellInfo_2->SpellFamilyFlags == 0x0 && spellInfo_2->SpellIconID == 108) && (spellInfo_1->SpellFamilyFlags & 0x20000000000000LL) )
1254                    return false;
1255
1256                //  Tree of Life (Shapeshift) and 34123 Tree of Life (Passive)
1257                if ((spellId_1 == 33891 && spellId_2 == 34123) ||
1258                    (spellId_2 == 33891 && spellId_1 == 34123))
1259                    return false;
1260
1261                // Wrath of Elune and Nature's Grace
1262                if( spellInfo_1->Id == 16886 && spellInfo_2->Id == 46833 || spellInfo_2->Id == 16886 && spellInfo_1->Id == 46833 )
1263                    return false;
1264
1265                // Bear Rage (Feral T4 (2)) and Omen of Clarity
1266                if( spellInfo_1->Id == 16864 && spellInfo_2->Id == 37306 || spellInfo_2->Id == 16864 && spellInfo_1->Id == 37306 )
1267                    return false;
1268
1269                // Cat Energy (Feral T4 (2)) and Omen of Clarity
1270                if( spellInfo_1->Id == 16864 && spellInfo_2->Id == 37311 || spellInfo_2->Id == 16864 && spellInfo_1->Id == 37311 )
1271                    return false;
1272            }
1273
1274            // Leader of the Pack and Scroll of Stamina (multi-family check)
1275            if( spellInfo_1->Id == 24932 && spellInfo_2->SpellIconID == 312 && spellInfo_2->SpellVisual == 216 )
1276                return false;
1277
1278            // Dragonmaw Illusion (multi-family check)
1279            if (spellId_1 == 42016 && spellId_2 == 40216 )
1280                return false;
1281
1282            break;
1283        case SPELLFAMILY_ROGUE:
1284            if( spellInfo_2->SpellFamilyName == SPELLFAMILY_ROGUE )
1285            {
1286                // Master of Subtlety
1287                if (spellId_1 == 31665 && spellId_2 == 31666 || spellId_1 == 31666 && spellId_2 == 31665 )
1288                    return false;
1289            }
1290
1291            // Garrote -> Garrote-Silence (multi-family check)
1292            if( spellInfo_1->SpellIconID == 498 && spellInfo_2->SpellIconID == 498 && spellInfo_2->SpellVisual == 0 )
1293                return false;
1294            break;
1295        case SPELLFAMILY_HUNTER:
1296            if( spellInfo_2->SpellFamilyName == SPELLFAMILY_HUNTER )
1297            {
1298                // Rapid Fire & Quick Shots
1299                if( (spellInfo_1->SpellFamilyFlags & 0x20) && (spellInfo_2->SpellFamilyFlags & 0x20000000000LL) ||
1300                    (spellInfo_2->SpellFamilyFlags & 0x20) && (spellInfo_1->SpellFamilyFlags & 0x20000000000LL) )
1301                    return false;
1302
1303                // Serpent Sting & (Immolation/Explosive Trap Effect)
1304                if( (spellInfo_1->SpellFamilyFlags & 0x4) && (spellInfo_2->SpellFamilyFlags & 0x00000004000LL) ||
1305                    (spellInfo_2->SpellFamilyFlags & 0x4) && (spellInfo_1->SpellFamilyFlags & 0x00000004000LL) )
1306                    return false;
1307
1308                // Bestial Wrath
1309                if( spellInfo_1->SpellIconID == 1680 && spellInfo_2->SpellIconID == 1680 )
1310                    return false;
1311            }
1312
1313            // Wing Clip -> Improved Wing Clip (multi-family check)
1314            if( (spellInfo_1->SpellFamilyFlags & 0x40) && spellInfo_2->Id == 19229 )
1315                return false;
1316
1317            // Concussive Shot and Imp. Concussive Shot (multi-family check)
1318            if( spellInfo_2->Id == 19410 && spellInfo_1->Id == 5116 )
1319                return false;
1320            break;
1321        case SPELLFAMILY_PALADIN:
1322            if( spellInfo_2->SpellFamilyName == SPELLFAMILY_PALADIN )
1323            {
1324                // Paladin Seals
1325                if( IsSealSpell(spellInfo_1) && IsSealSpell(spellInfo_2) )
1326                    return true;
1327            }
1328            // Combustion and Fire Protection Aura (multi-family check)
1329            if( spellInfo_2->Id == 11129 && spellInfo_1->SpellIconID == 33 && spellInfo_1->SpellVisual == 321 )
1330                return false;
1331
1332            // *Sanctity Aura -> Unstable Currents and other (multi-family check)
1333            if( spellInfo_1->SpellIconID==502 && spellInfo_2->SpellFamilyName == SPELLFAMILY_GENERIC && spellInfo_2->SpellIconID==502 && spellInfo_2->SpellVisual==969 )
1334                return false;
1335
1336            // *Seal of Command and Band of Eternal Champion (multi-family check)
1337            if( spellInfo_1->SpellIconID==561 && spellInfo_1->SpellVisual==7992 && spellId_2 == 35081)
1338                return false;
1339            break;
1340        case SPELLFAMILY_SHAMAN:
1341            if( spellInfo_2->SpellFamilyName == SPELLFAMILY_SHAMAN )
1342            {
1343                // shaman shields
1344                if( IsElementalShield(spellInfo_1) && IsElementalShield(spellInfo_2) )
1345                    return true;
1346
1347                // Windfury weapon
1348                if( spellInfo_1->SpellIconID==220 && spellInfo_2->SpellIconID==220 &&
1349                    spellInfo_1->SpellFamilyFlags != spellInfo_2->SpellFamilyFlags )
1350                    return false;
1351            }
1352            // Bloodlust and Bloodthirst (multi-family check)
1353            if( spellInfo_1->Id == 2825 && spellInfo_2->SpellIconID == 38 && spellInfo_2->SpellVisual == 0 )
1354                return false;
1355            break;
1356        default:
1357            break;
1358    }
1359
1360    if (IsRankSpellDueToSpell(spellInfo_1, spellId_2))
1361        return true;*/
1362
1363    if (spellInfo_1->SpellIconID != spellInfo_2->SpellIconID
1364        || !spellInfo_1->SpellIconID)
1365        return false;
1366
1367    if (spellInfo_1->SpellFamilyName != spellInfo_2->SpellFamilyName)
1368        return false;
1369
1370    for (int i = 0; i < 3; ++i)
1371        if (spellInfo_1->Effect[i] != spellInfo_2->Effect[i] ||
1372        spellInfo_1->EffectItemType[i] != spellInfo_2->EffectItemType[i] ||
1373        spellInfo_1->EffectMiscValue[i] != spellInfo_2->EffectMiscValue[i] ||
1374        spellInfo_1->EffectApplyAuraName[i] != spellInfo_2->EffectApplyAuraName[i])
1375            return false;
1376
1377    return true;
1378}
1379
1380bool SpellMgr::IsProfessionSpell(uint32 spellId)
1381{
1382    SpellEntry const *spellInfo = sSpellStore.LookupEntry(spellId);
1383    if(!spellInfo)
1384        return false;
1385
1386    if(spellInfo->Effect[1] != SPELL_EFFECT_SKILL)
1387        return false;
1388
1389    uint32 skill = spellInfo->EffectMiscValue[1];
1390
1391    return IsProfessionSkill(skill);
1392}
1393
1394bool SpellMgr::IsPrimaryProfessionSpell(uint32 spellId)
1395{
1396    SpellEntry const *spellInfo = sSpellStore.LookupEntry(spellId);
1397    if(!spellInfo)
1398        return false;
1399
1400    if(spellInfo->Effect[1] != SPELL_EFFECT_SKILL)
1401        return false;
1402
1403    uint32 skill = spellInfo->EffectMiscValue[1];
1404
1405    return IsPrimaryProfessionSkill(skill);
1406}
1407
1408bool SpellMgr::IsPrimaryProfessionFirstRankSpell(uint32 spellId) const
1409{
1410    return IsPrimaryProfessionSpell(spellId) && GetSpellRank(spellId)==1;
1411}
1412
1413SpellEntry const* SpellMgr::SelectAuraRankForPlayerLevel(SpellEntry const* spellInfo, uint32 playerLevel) const
1414{
1415    // ignore passive spells
1416    if(IsPassiveSpell(spellInfo->Id))
1417        return spellInfo;
1418
1419    bool needRankSelection = false;
1420    for(int i=0;i<3;i++)
1421    {
1422        if( IsPositiveEffect(spellInfo->Id, i) && (
1423            spellInfo->Effect[i] == SPELL_EFFECT_APPLY_AURA ||
1424            spellInfo->Effect[i] == SPELL_EFFECT_APPLY_AREA_AURA_PARTY
1425            ) )
1426        {
1427            needRankSelection = true;
1428            break;
1429        }
1430    }
1431
1432    // not required
1433    if(!needRankSelection)
1434        return spellInfo;
1435
1436    for(uint32 nextSpellId = spellInfo->Id; nextSpellId != 0; nextSpellId = GetPrevSpellInChain(nextSpellId))
1437    {
1438        SpellEntry const *nextSpellInfo = sSpellStore.LookupEntry(nextSpellId);
1439        if(!nextSpellInfo)
1440            break;
1441
1442        // if found appropriate level
1443        if(playerLevel + 10 >= nextSpellInfo->spellLevel)
1444            return nextSpellInfo;
1445
1446        // one rank less then
1447    }
1448
1449    // not found
1450    return NULL;
1451}
1452
1453void SpellMgr::LoadSpellChains()
1454{
1455    mSpellChains.clear();                                   // need for reload case
1456    mSpellChainsNext.clear();                               // need for reload case
1457
1458    QueryResult *result = WorldDatabase.PQuery("SELECT spell_id, prev_spell, first_spell, rank, req_spell FROM spell_chain");
1459    if(result == NULL)
1460    {
1461        barGoLink bar( 1 );
1462        bar.step();
1463
1464        sLog.outString();
1465        sLog.outString( ">> Loaded 0 spell chain records" );
1466        sLog.outErrorDb("`spell_chains` table is empty!");
1467        return;
1468    }
1469
1470    uint32 count = 0;
1471
1472    barGoLink bar( result->GetRowCount() );
1473    do
1474    {
1475        bar.step();
1476        Field *fields = result->Fetch();
1477
1478        uint32 spell_id = fields[0].GetUInt32();
1479
1480        SpellChainNode node;
1481        node.prev  = fields[1].GetUInt32();
1482        node.first = fields[2].GetUInt32();
1483        node.rank  = fields[3].GetUInt8();
1484        node.req   = fields[4].GetUInt32();
1485
1486        if(!sSpellStore.LookupEntry(spell_id))
1487        {
1488            sLog.outErrorDb("Spell %u listed in `spell_chain` does not exist",spell_id);
1489            continue;
1490        }
1491
1492        if(node.prev!=0 && !sSpellStore.LookupEntry(node.prev))
1493        {
1494            sLog.outErrorDb("Spell %u (prev: %u, first: %u, rank: %d, req: %u) listed in `spell_chain` has not existed previous rank spell.",
1495                spell_id,node.prev,node.first,node.rank,node.req);
1496            continue;
1497        }
1498
1499        if(!sSpellStore.LookupEntry(node.first))
1500        {
1501            sLog.outErrorDb("Spell %u (prev: %u, first: %u, rank: %d, req: %u) listed in `spell_chain` has not existing first rank spell.",
1502                spell_id,node.prev,node.first,node.rank,node.req);
1503            continue;
1504        }
1505
1506        // check basic spell chain data integrity (note: rank can be equal 0 or 1 for first/single spell)
1507        if( (spell_id == node.first) != (node.rank <= 1) ||
1508            (spell_id == node.first) != (node.prev == 0) ||
1509            (node.rank <= 1) != (node.prev == 0) )
1510        {
1511            sLog.outErrorDb("Spell %u (prev: %u, first: %u, rank: %d, req: %u) listed in `spell_chain` has not compatible chain data.",
1512                spell_id,node.prev,node.first,node.rank,node.req);
1513            continue;
1514        }
1515
1516        if(node.req!=0 && !sSpellStore.LookupEntry(node.req))
1517        {
1518            sLog.outErrorDb("Spell %u (prev: %u, first: %u, rank: %d, req: %u) listed in `spell_chain` has not existing required spell.",
1519                spell_id,node.prev,node.first,node.rank,node.req);
1520            continue;
1521        }
1522
1523        // talents not required data in spell chain for work, but must be checked if present for intergrity
1524        if(TalentSpellPos const* pos = GetTalentSpellPos(spell_id))
1525        {
1526            if(node.rank!=pos->rank+1)
1527            {
1528                sLog.outErrorDb("Talent %u (prev: %u, first: %u, rank: %d, req: %u) listed in `spell_chain` has wrong rank.",
1529                    spell_id,node.prev,node.first,node.rank,node.req);
1530                continue;
1531            }
1532
1533            if(TalentEntry const* talentEntry = sTalentStore.LookupEntry(pos->talent_id))
1534            {
1535                if(node.first!=talentEntry->RankID[0])
1536                {
1537                    sLog.outErrorDb("Talent %u (prev: %u, first: %u, rank: %d, req: %u) listed in `spell_chain` has wrong first rank spell.",
1538                        spell_id,node.prev,node.first,node.rank,node.req);
1539                    continue;
1540                }
1541
1542                if(node.rank > 1 && node.prev != talentEntry->RankID[node.rank-1-1])
1543                {
1544                    sLog.outErrorDb("Talent %u (prev: %u, first: %u, rank: %d, req: %u) listed in `spell_chain` has wrong prev rank spell.",
1545                        spell_id,node.prev,node.first,node.rank,node.req);
1546                    continue;
1547                }
1548
1549                if(node.req!=talentEntry->DependsOnSpell)
1550                {
1551                    sLog.outErrorDb("Talent %u (prev: %u, first: %u, rank: %d, req: %u) listed in `spell_chain` has wrong required spell.",
1552                        spell_id,node.prev,node.first,node.rank,node.req);
1553                    continue;
1554                }
1555            }
1556        }
1557
1558        mSpellChains[spell_id] = node;
1559
1560        if(node.prev)
1561            mSpellChainsNext.insert(SpellChainMapNext::value_type(node.prev,spell_id));
1562
1563        if(node.req)
1564            mSpellChainsNext.insert(SpellChainMapNext::value_type(node.req,spell_id));
1565
1566        ++count;
1567    } while( result->NextRow() );
1568
1569    // additional integrity checks
1570    for(SpellChainMap::iterator i = mSpellChains.begin(); i != mSpellChains.end(); ++i)
1571    {
1572        if(i->second.prev)
1573        {
1574            SpellChainMap::iterator i_prev = mSpellChains.find(i->second.prev);
1575            if(i_prev == mSpellChains.end())
1576            {
1577                sLog.outErrorDb("Spell %u (prev: %u, first: %u, rank: %d, req: %u) listed in `spell_chain` has not found previous rank spell in table.",
1578                    i->first,i->second.prev,i->second.first,i->second.rank,i->second.req);
1579            }
1580            else if( i_prev->second.first != i->second.first )
1581            {
1582                sLog.outErrorDb("Spell %u (prev: %u, first: %u, rank: %d, req: %u) listed in `spell_chain` has different first spell in chain compared to previous rank spell (prev: %u, first: %u, rank: %d, req: %u).",
1583                    i->first,i->second.prev,i->second.first,i->second.rank,i->second.req,
1584                    i_prev->second.prev,i_prev->second.first,i_prev->second.rank,i_prev->second.req);
1585            }
1586            else if( i_prev->second.rank+1 != i->second.rank )
1587            {
1588                sLog.outErrorDb("Spell %u (prev: %u, first: %u, rank: %d, req: %u) listed in `spell_chain` has different rank compared to previous rank spell (prev: %u, first: %u, rank: %d, req: %u).",
1589                    i->first,i->second.prev,i->second.first,i->second.rank,i->second.req,
1590                    i_prev->second.prev,i_prev->second.first,i_prev->second.rank,i_prev->second.req);
1591            }
1592        }
1593
1594        if(i->second.req)
1595        {
1596            SpellChainMap::iterator i_req = mSpellChains.find(i->second.req);
1597            if(i_req == mSpellChains.end())
1598            {
1599                sLog.outErrorDb("Spell %u (prev: %u, first: %u, rank: %d, req: %u) listed in `spell_chain` has not found required rank spell in table.",
1600                    i->first,i->second.prev,i->second.first,i->second.rank,i->second.req);
1601            }
1602            else if( i_req->second.first == i->second.first )
1603            {
1604                sLog.outErrorDb("Spell %u (prev: %u, first: %u, rank: %d, req: %u) listed in `spell_chain` has required rank spell from same spell chain (prev: %u, first: %u, rank: %d, req: %u).",
1605                    i->first,i->second.prev,i->second.first,i->second.rank,i->second.req,
1606                    i_req->second.prev,i_req->second.first,i_req->second.rank,i_req->second.req);
1607            }
1608            else if( i_req->second.req )
1609            {
1610                sLog.outErrorDb("Spell %u (prev: %u, first: %u, rank: %d, req: %u) listed in `spell_chain` has required rank spell with required spell (prev: %u, first: %u, rank: %d, req: %u).",
1611                    i->first,i->second.prev,i->second.first,i->second.rank,i->second.req,
1612                    i_req->second.prev,i_req->second.first,i_req->second.rank,i_req->second.req);
1613            }
1614        }
1615    }
1616
1617    delete result;
1618
1619    sLog.outString();
1620    sLog.outString( ">> Loaded %u spell chain records", count );
1621}
1622
1623void SpellMgr::LoadSpellLearnSkills()
1624{
1625    mSpellLearnSkills.clear();                              // need for reload case
1626
1627    // search auto-learned skills and add its to map also for use in unlearn spells/talents
1628    uint32 dbc_count = 0;
1629    for(uint32 spell = 0; spell < sSpellStore.GetNumRows(); ++spell)
1630    {
1631        SpellEntry const* entry = sSpellStore.LookupEntry(spell);
1632
1633        if(!entry)
1634            continue;
1635
1636        for(int i = 0; i < 3; ++i)
1637        {
1638            if(entry->Effect[i]==SPELL_EFFECT_SKILL)
1639            {
1640                SpellLearnSkillNode dbc_node;
1641                dbc_node.skill    = entry->EffectMiscValue[i];
1642                if ( dbc_node.skill != SKILL_RIDING )
1643                    dbc_node.value    = 1;
1644                else
1645                    dbc_node.value    = (entry->EffectBasePoints[i]+1)*75;
1646                dbc_node.maxvalue = (entry->EffectBasePoints[i]+1)*75;
1647
1648                SpellLearnSkillNode const* db_node = GetSpellLearnSkill(spell);
1649
1650                mSpellLearnSkills[spell] = dbc_node;
1651                ++dbc_count;
1652                break;
1653            }
1654        }
1655    }
1656
1657    sLog.outString();
1658    sLog.outString( ">> Loaded %u Spell Learn Skills from DBC", dbc_count );
1659}
1660
1661void SpellMgr::LoadSpellLearnSpells()
1662{
1663    mSpellLearnSpells.clear();                              // need for reload case
1664
1665    QueryResult *result = WorldDatabase.PQuery("SELECT entry, SpellID FROM spell_learn_spell");
1666    if(!result)
1667    {
1668        barGoLink bar( 1 );
1669        bar.step();
1670
1671        sLog.outString();
1672        sLog.outString( ">> Loaded 0 spell learn spells" );
1673        sLog.outErrorDb("`spell_learn_spell` table is empty!");
1674        return;
1675    }
1676
1677    uint32 count = 0;
1678
1679    barGoLink bar( result->GetRowCount() );
1680    do
1681    {
1682        bar.step();
1683        Field *fields = result->Fetch();
1684
1685        uint32 spell_id    = fields[0].GetUInt32();
1686
1687        SpellLearnSpellNode node;
1688        node.spell      = fields[1].GetUInt32();
1689        node.autoLearned= false;
1690
1691        if(!sSpellStore.LookupEntry(spell_id))
1692        {
1693            sLog.outErrorDb("Spell %u listed in `spell_learn_spell` does not exist",spell_id);
1694            continue;
1695        }
1696
1697        if(!sSpellStore.LookupEntry(node.spell))
1698        {
1699            sLog.outErrorDb("Spell %u listed in `spell_learn_spell` does not exist",node.spell);
1700            continue;
1701        }
1702
1703        mSpellLearnSpells.insert(SpellLearnSpellMap::value_type(spell_id,node));
1704
1705        ++count;
1706    } while( result->NextRow() );
1707
1708    delete result;
1709
1710    // search auto-learned spells and add its to map also for use in unlearn spells/talents
1711    uint32 dbc_count = 0;
1712    for(uint32 spell = 0; spell < sSpellStore.GetNumRows(); ++spell)
1713    {
1714        SpellEntry const* entry = sSpellStore.LookupEntry(spell);
1715
1716        if(!entry)
1717            continue;
1718
1719        for(int i = 0; i < 3; ++i)
1720        {
1721            if(entry->Effect[i]==SPELL_EFFECT_LEARN_SPELL)
1722            {
1723                SpellLearnSpellNode dbc_node;
1724                dbc_node.spell       = entry->EffectTriggerSpell[i];
1725                dbc_node.autoLearned = true;
1726
1727                SpellLearnSpellMap::const_iterator db_node_begin = GetBeginSpellLearnSpell(spell);
1728                SpellLearnSpellMap::const_iterator db_node_end   = GetEndSpellLearnSpell(spell);
1729
1730                bool found = false;
1731                for(SpellLearnSpellMap::const_iterator itr = db_node_begin; itr != db_node_end; ++itr)
1732                {
1733                    if(itr->second.spell == dbc_node.spell)
1734                    {
1735                        sLog.outErrorDb("Spell %u auto-learn spell %u in spell.dbc then the record in `spell_learn_spell` is redundant, please fix DB.",
1736                            spell,dbc_node.spell);
1737                        found = true;
1738                        break;
1739                    }
1740                }
1741
1742                if(!found)                                  // add new spell-spell pair if not found
1743                {
1744                    mSpellLearnSpells.insert(SpellLearnSpellMap::value_type(spell,dbc_node));
1745                    ++dbc_count;
1746                }
1747            }
1748        }
1749    }
1750
1751    sLog.outString();
1752    sLog.outString( ">> Loaded %u spell learn spells + %u found in DBC", count, dbc_count );
1753}
1754
1755void SpellMgr::LoadSpellScriptTarget()
1756{
1757    mSpellScriptTarget.clear();                             // need for reload case
1758
1759    uint32 count = 0;
1760
1761    QueryResult *result = WorldDatabase.Query("SELECT entry,type,targetEntry FROM spell_script_target");
1762
1763    if(!result)
1764    {
1765        barGoLink bar(1);
1766
1767        bar.step();
1768
1769        sLog.outString();
1770        sLog.outErrorDb(">> Loaded 0 SpellScriptTarget. DB table `spell_script_target` is empty.");
1771        return;
1772    }
1773
1774    barGoLink bar(result->GetRowCount());
1775
1776    do
1777    {
1778        Field *fields = result->Fetch();
1779        bar.step();
1780
1781        uint32 spellId     = fields[0].GetUInt32();
1782        uint32 type        = fields[1].GetUInt32();
1783        uint32 targetEntry = fields[2].GetUInt32();
1784
1785        SpellEntry const* spellProto = sSpellStore.LookupEntry(spellId);
1786
1787        if(!spellProto)
1788        {
1789            sLog.outErrorDb("Table `spell_script_target`: spellId %u listed for TargetEntry %u does not exist.",spellId,targetEntry);
1790            continue;
1791        }
1792
1793        bool targetfound = false;
1794        for(int i = 0; i <3; ++i)
1795        {
1796            if( spellProto->EffectImplicitTargetA[i]==TARGET_SCRIPT ||
1797                spellProto->EffectImplicitTargetB[i]==TARGET_SCRIPT ||
1798                spellProto->EffectImplicitTargetA[i]==TARGET_SCRIPT_COORDINATES ||
1799                spellProto->EffectImplicitTargetB[i]==TARGET_SCRIPT_COORDINATES )
1800            {
1801                targetfound = true;
1802                break;
1803            }
1804        }
1805        if(!targetfound)
1806        {
1807            sLog.outErrorDb("Table `spell_script_target`: spellId %u listed for TargetEntry %u does not have any implicit target TARGET_SCRIPT(38) or TARGET_SCRIPT_COORDINATES (46).",spellId,targetEntry);
1808            continue;
1809        }
1810
1811        if( type >= MAX_SPELL_TARGET_TYPE )
1812        {
1813            sLog.outErrorDb("Table `spell_script_target`: target type %u for TargetEntry %u is incorrect.",type,targetEntry);
1814            continue;
1815        }
1816
1817        switch(type)
1818        {
1819            case SPELL_TARGET_TYPE_GAMEOBJECT:
1820            {
1821                if( targetEntry==0 )
1822                    break;
1823
1824                if(!sGOStorage.LookupEntry<GameObjectInfo>(targetEntry))
1825                {
1826                    sLog.outErrorDb("Table `spell_script_target`: gameobject template entry %u does not exist.",targetEntry);
1827                    continue;
1828                }
1829                break;
1830            }
1831            default:
1832            {
1833                if( targetEntry==0 )
1834                {
1835                    sLog.outErrorDb("Table `spell_script_target`: target entry == 0 for not GO target type (%u).",type);
1836                    continue;
1837                }
1838                if(!sCreatureStorage.LookupEntry<CreatureInfo>(targetEntry))
1839                {
1840                    sLog.outErrorDb("Table `spell_script_target`: creature template entry %u does not exist.",targetEntry);
1841                    continue;
1842                }
1843                const CreatureInfo* cInfo = sCreatureStorage.LookupEntry<CreatureInfo>(targetEntry);
1844
1845                if(spellId == 30427 && !cInfo->SkinLootId)
1846                {
1847                    sLog.outErrorDb("Table `spell_script_target` has creature %u as a target of spellid 30427, but this creature has no skinlootid. Gas extraction will not work!", cInfo->Entry);
1848                    continue;
1849                }
1850                break;
1851            }
1852        }
1853
1854        mSpellScriptTarget.insert(SpellScriptTarget::value_type(spellId,SpellTargetEntry(SpellTargetType(type),targetEntry)));
1855
1856        ++count;
1857    } while (result->NextRow());
1858
1859    delete result;
1860
1861    // Check all spells
1862    /* Disabled (lot errors at this moment)
1863    for(uint32 i = 1; i < sSpellStore.nCount; ++i)
1864    {
1865        SpellEntry const * spellInfo = sSpellStore.LookupEntry(i);
1866        if(!spellInfo)
1867            continue;
1868
1869        bool found = false;
1870        for(int j=0; j<3; ++j)
1871        {
1872            if( spellInfo->EffectImplicitTargetA[j] == TARGET_SCRIPT || spellInfo->EffectImplicitTargetA[j] != TARGET_SELF && spellInfo->EffectImplicitTargetB[j] == TARGET_SCRIPT )
1873            {
1874                SpellScriptTarget::const_iterator lower = spellmgr.GetBeginSpellScriptTarget(spellInfo->Id);
1875                SpellScriptTarget::const_iterator upper = spellmgr.GetEndSpellScriptTarget(spellInfo->Id);
1876                if(lower==upper)
1877                {
1878                    sLog.outErrorDb("Spell (ID: %u) has effect EffectImplicitTargetA/EffectImplicitTargetB = %u (TARGET_SCRIPT), but does not have record in `spell_script_target`",spellInfo->Id,TARGET_SCRIPT);
1879                    break;                                  // effects of spell
1880                }
1881            }
1882        }
1883    }
1884    */
1885
1886    sLog.outString();
1887    sLog.outString(">> Loaded %u Spell Script Targets", count);
1888}
1889
1890void SpellMgr::LoadSpellPetAuras()
1891{
1892    mSpellPetAuraMap.clear();                                  // need for reload case
1893
1894    uint32 count = 0;
1895
1896    //                                                0      1    2
1897    QueryResult *result = WorldDatabase.Query("SELECT spell, pet, aura FROM spell_pet_auras");
1898    if( !result )
1899    {
1900
1901        barGoLink bar( 1 );
1902
1903        bar.step();
1904
1905        sLog.outString();
1906        sLog.outString( ">> Loaded %u spell pet auras", count );
1907        return;
1908    }
1909
1910    barGoLink bar( result->GetRowCount() );
1911
1912    do
1913    {
1914        Field *fields = result->Fetch();
1915
1916        bar.step();
1917
1918        uint16 spell = fields[0].GetUInt16();
1919        uint16 pet = fields[1].GetUInt16();
1920        uint16 aura = fields[2].GetUInt16();
1921
1922        SpellPetAuraMap::iterator itr = mSpellPetAuraMap.find(spell);
1923        if(itr != mSpellPetAuraMap.end())
1924        {
1925            itr->second.AddAura(pet, aura);
1926        }
1927        else
1928        {
1929            SpellEntry const* spellInfo = sSpellStore.LookupEntry(spell);
1930            if (!spellInfo)
1931            {
1932                sLog.outErrorDb("Spell %u listed in `spell_pet_auras` does not exist", spell);
1933                continue;
1934            }
1935            int i = 0;
1936            for(; i < 3; ++i)
1937                if((spellInfo->Effect[i] == SPELL_EFFECT_APPLY_AURA &&
1938                    spellInfo->EffectApplyAuraName[i] == SPELL_AURA_DUMMY) ||
1939                    spellInfo->Effect[i] == SPELL_EFFECT_DUMMY)
1940                    break;
1941
1942            if(i == 3)
1943            {
1944                sLog.outError("Spell %u listed in `spell_pet_auras` does not have dummy aura or dummy effect", spell);
1945                continue;
1946            }
1947
1948            SpellEntry const* spellInfo2 = sSpellStore.LookupEntry(aura);
1949            if (!spellInfo2)
1950            {
1951                sLog.outErrorDb("Aura %u listed in `spell_pet_auras` does not exist", aura);
1952                continue;
1953            }
1954
1955            PetAura pa(pet, aura, spellInfo->EffectImplicitTargetA[i] == TARGET_PET, spellInfo->EffectBasePoints[i] + spellInfo->EffectBaseDice[i]);
1956            mSpellPetAuraMap[spell] = pa;
1957        }
1958
1959        ++count;
1960    } while( result->NextRow() );
1961
1962    delete result;
1963
1964    sLog.outString();
1965    sLog.outString( ">> Loaded %u spell pet auras", count );
1966}
1967
1968/// Some checks for spells, to prevent adding depricated/broken spells for trainers, spell book, etc
1969bool SpellMgr::IsSpellValid(SpellEntry const* spellInfo, Player* pl, bool msg)
1970{
1971    // not exist
1972    if(!spellInfo)
1973        return false;
1974
1975    bool need_check_reagents = false;
1976
1977    // check effects
1978    for(int i=0; i<3; ++i)
1979    {
1980        switch(spellInfo->Effect[i])
1981        {
1982            case 0:
1983                continue;
1984
1985                // craft spell for crafting non-existed item (break client recipes list show)
1986            case SPELL_EFFECT_CREATE_ITEM:
1987            {
1988                if(!ObjectMgr::GetItemPrototype( spellInfo->EffectItemType[i] ))
1989                {
1990                    if(msg)
1991                    {
1992                        if(pl)
1993                            ChatHandler(pl).PSendSysMessage("Craft spell %u create not-exist in DB item (Entry: %u) and then...",spellInfo->Id,spellInfo->EffectItemType[i]);
1994                        else
1995                            sLog.outErrorDb("Craft spell %u create not-exist in DB item (Entry: %u) and then...",spellInfo->Id,spellInfo->EffectItemType[i]);
1996                    }
1997                    return false;
1998                }
1999
2000                need_check_reagents = true;
2001                break;
2002            }
2003            case SPELL_EFFECT_LEARN_SPELL:
2004            {
2005                SpellEntry const* spellInfo2 = sSpellStore.LookupEntry(spellInfo->EffectTriggerSpell[0]);
2006                if( !IsSpellValid(spellInfo2,pl,msg) )
2007                {
2008                    if(msg)
2009                    {
2010                        if(pl)
2011                            ChatHandler(pl).PSendSysMessage("Spell %u learn to broken spell %u, and then...",spellInfo->Id,spellInfo->EffectTriggerSpell[0]);
2012                        else
2013                            sLog.outErrorDb("Spell %u learn to invalid spell %u, and then...",spellInfo->Id,spellInfo->EffectTriggerSpell[0]);
2014                    }
2015                    return false;
2016                }
2017                break;
2018            }
2019        }
2020    }
2021
2022    if(need_check_reagents)
2023    {
2024        for(int j = 0; j < 8; ++j)
2025        {
2026            if(spellInfo->Reagent[j] > 0 && !ObjectMgr::GetItemPrototype( spellInfo->Reagent[j] ))
2027            {
2028                if(msg)
2029                {
2030                    if(pl)
2031                        ChatHandler(pl).PSendSysMessage("Craft spell %u have not-exist reagent in DB item (Entry: %u) and then...",spellInfo->Id,spellInfo->Reagent[j]);
2032                    else
2033                        sLog.outErrorDb("Craft spell %u have not-exist reagent in DB item (Entry: %u) and then...",spellInfo->Id,spellInfo->Reagent[j]);
2034                }
2035                return false;
2036            }
2037        }
2038    }
2039
2040    return true;
2041}
2042
2043bool IsSpellAllowedInLocation(SpellEntry const *spellInfo,uint32 map_id,uint32 zone_id,uint32 area_id)
2044{
2045    // normal case
2046    if( spellInfo->AreaId && spellInfo->AreaId != zone_id && spellInfo->AreaId != area_id )
2047        return false;
2048
2049    // elixirs (all area dependent elixirs have family SPELLFAMILY_POTION, use this for speedup)
2050    if(spellInfo->SpellFamilyName==SPELLFAMILY_POTION)
2051    {
2052        if(uint32 mask = spellmgr.GetSpellElixirMask(spellInfo->Id))
2053        {
2054            if(mask & ELIXIR_UNSTABLE_MASK)
2055            {
2056                // in the Blade's Edge Mountains Plateaus and Gruul's Lair.
2057                return zone_id ==3522 || map_id==565;
2058            }
2059            if(mask & ELIXIR_UNSTABLE_MASK)
2060            {
2061                // in Tempest Keep, Serpentshrine Cavern, Caverns of Time: Mount Hyjal, Black Temple
2062                // TODO: and the Sunwell Plateau
2063                if(zone_id ==3607 || map_id==534 || map_id==564)
2064                    return true;
2065
2066                MapEntry const* mapEntry = sMapStore.LookupEntry(map_id);
2067                if(!mapEntry)
2068                    return false;
2069
2070                return mapEntry->multimap_id==206;
2071            }
2072
2073            // elixirs not have another limitations
2074            return true;
2075        }
2076    }
2077
2078    // special cases zone check (maps checked by multimap common id)
2079    switch(spellInfo->Id)
2080    {
2081        case 41618:
2082        case 41620:
2083        {
2084            MapEntry const* mapEntry = sMapStore.LookupEntry(map_id);
2085            if(!mapEntry)
2086                return false;
2087
2088            return mapEntry->multimap_id==206;
2089        }
2090
2091        case 41617:
2092        case 41619:
2093        {
2094            MapEntry const* mapEntry = sMapStore.LookupEntry(map_id);
2095            if(!mapEntry)
2096                return false;
2097
2098            return mapEntry->multimap_id==207;
2099        }
2100        // Dragonmaw Illusion
2101        case 40216:
2102        case 42016:
2103        {
2104            if ( area_id != 3759 && area_id != 3966 && area_id != 3939 )
2105                return false;
2106            break;
2107        }
2108    }
2109
2110    return true;
2111}
2112
2113void SpellMgr::LoadSkillLineAbilityMap()
2114{
2115    mSkillLineAbilityMap.clear();
2116
2117    uint32 count = 0;
2118
2119    for (uint32 i = 0; i < sSkillLineAbilityStore.GetNumRows(); i++)
2120    {
2121        SkillLineAbilityEntry const *SkillInfo = sSkillLineAbilityStore.LookupEntry(i);
2122        if(!SkillInfo)
2123            continue;
2124
2125        mSkillLineAbilityMap.insert(SkillLineAbilityMap::value_type(SkillInfo->spellId,SkillInfo));
2126        ++count;
2127    }
2128
2129    sLog.outString();
2130    sLog.outString(">> Loaded %u SkillLineAbility MultiMap", count);
2131}
2132
2133DiminishingGroup GetDiminishingReturnsGroupForSpell(SpellEntry const* spellproto, bool triggered)
2134{
2135    // Explicit Diminishing Groups
2136    switch(spellproto->SpellFamilyName)
2137    {
2138        case SPELLFAMILY_MAGE:
2139        {
2140            // Polymorph
2141            if ((spellproto->SpellFamilyFlags & 0x00001000000LL) && spellproto->EffectApplyAuraName[0]==SPELL_AURA_MOD_CONFUSE)
2142                return DIMINISHING_POLYMORPH;
2143            break;
2144        }
2145        case SPELLFAMILY_ROGUE:
2146        {
2147            // Kidney Shot
2148            if (spellproto->SpellFamilyFlags & 0x00000200000LL)
2149                return DIMINISHING_KIDNEYSHOT;
2150            // Blind
2151            else if (spellproto->SpellFamilyFlags & 0x00001000000LL)
2152                return DIMINISHING_BLIND_CYCLONE;
2153            break;
2154        }
2155        case SPELLFAMILY_WARLOCK:
2156        {
2157            // Death Coil
2158            if (spellproto->SpellFamilyFlags & 0x00000080000LL)
2159                return DIMINISHING_DEATHCOIL;
2160            // Fear
2161            else if (spellproto->SpellFamilyFlags & 0x40840000000LL)
2162                return DIMINISHING_WARLOCK_FEAR;
2163            // Curses/etc
2164            else if (spellproto->SpellFamilyFlags & 0x00080000000LL)
2165                return DIMINISHING_LIMITONLY;
2166            break;
2167        }
2168        case SPELLFAMILY_DRUID:
2169        {
2170            // Cyclone
2171            if (spellproto->SpellFamilyFlags & 0x02000000000LL)
2172                return DIMINISHING_BLIND_CYCLONE;
2173            break;
2174        }
2175        case SPELLFAMILY_WARRIOR:
2176        {
2177            // Hamstring - limit duration to 10s in PvP
2178            if (spellproto->SpellFamilyFlags & 0x00000000002LL)
2179                return DIMINISHING_LIMITONLY;
2180            break;
2181        }
2182        default:
2183            break;
2184    }
2185
2186    // Get by mechanic
2187    for (uint8 i=0;i<3;++i)
2188    {
2189        if (spellproto->Mechanic      == MECHANIC_STUN    || spellproto->EffectMechanic[i] == MECHANIC_STUN)
2190            return triggered ? DIMINISHING_TRIGGER_STUN : DIMINISHING_CONTROL_STUN;
2191        else if (spellproto->Mechanic == MECHANIC_SLEEP   || spellproto->EffectMechanic[i] == MECHANIC_SLEEP)
2192            return DIMINISHING_SLEEP;
2193        else if (spellproto->Mechanic == MECHANIC_ROOT    || spellproto->EffectMechanic[i] == MECHANIC_ROOT)
2194            return triggered ? DIMINISHING_TRIGGER_ROOT : DIMINISHING_CONTROL_ROOT;
2195        else if (spellproto->Mechanic == MECHANIC_FEAR    || spellproto->EffectMechanic[i] == MECHANIC_FEAR)
2196            return DIMINISHING_FEAR;
2197        else if (spellproto->Mechanic == MECHANIC_CHARM   || spellproto->EffectMechanic[i] == MECHANIC_CHARM)
2198            return DIMINISHING_CHARM;
2199        else if (spellproto->Mechanic == MECHANIC_SILENCE || spellproto->EffectMechanic[i] == MECHANIC_SILENCE)
2200            return DIMINISHING_SILENCE;
2201        else if (spellproto->Mechanic == MECHANIC_DISARM  || spellproto->EffectMechanic[i] == MECHANIC_DISARM)
2202            return DIMINISHING_DISARM;
2203        else if (spellproto->Mechanic == MECHANIC_FREEZE  || spellproto->EffectMechanic[i] == MECHANIC_FREEZE)
2204            return DIMINISHING_FREEZE;
2205        else if (spellproto->Mechanic == MECHANIC_KNOCKOUT|| spellproto->EffectMechanic[i] == MECHANIC_KNOCKOUT ||
2206                 spellproto->Mechanic == MECHANIC_SAPPED  || spellproto->EffectMechanic[i] == MECHANIC_SAPPED)
2207            return DIMINISHING_KNOCKOUT;
2208        else if (spellproto->Mechanic == MECHANIC_BANISH  || spellproto->EffectMechanic[i] == MECHANIC_BANISH)
2209            return DIMINISHING_BANISH;
2210    }
2211
2212    return DIMINISHING_NONE;
2213}
2214
2215bool IsDiminishingReturnsGroupDurationLimited(DiminishingGroup group)
2216{
2217    switch(group)
2218    {
2219        case DIMINISHING_CONTROL_STUN:
2220        case DIMINISHING_TRIGGER_STUN:
2221        case DIMINISHING_KIDNEYSHOT:
2222        case DIMINISHING_SLEEP:
2223        case DIMINISHING_CONTROL_ROOT:
2224        case DIMINISHING_TRIGGER_ROOT:
2225        case DIMINISHING_FEAR:
2226        case DIMINISHING_WARLOCK_FEAR:
2227        case DIMINISHING_CHARM:
2228        case DIMINISHING_POLYMORPH:
2229        case DIMINISHING_FREEZE:
2230        case DIMINISHING_KNOCKOUT:
2231        case DIMINISHING_BLIND_CYCLONE:
2232        case DIMINISHING_BANISH:
2233        case DIMINISHING_LIMITONLY:
2234            return true;
2235    }
2236    return false;
2237}
2238
2239DiminishingReturnsType GetDiminishingReturnsGroupType(DiminishingGroup group)
2240{
2241    switch(group)
2242    {
2243        case DIMINISHING_BLIND_CYCLONE:
2244        case DIMINISHING_CONTROL_STUN:
2245        case DIMINISHING_TRIGGER_STUN:
2246        case DIMINISHING_KIDNEYSHOT:
2247            return DRTYPE_ALL;
2248        case DIMINISHING_SLEEP:
2249        case DIMINISHING_CONTROL_ROOT:
2250        case DIMINISHING_TRIGGER_ROOT:
2251        case DIMINISHING_FEAR:
2252        case DIMINISHING_CHARM:
2253        case DIMINISHING_POLYMORPH:
2254        case DIMINISHING_SILENCE:
2255        case DIMINISHING_DISARM:
2256        case DIMINISHING_DEATHCOIL:
2257        case DIMINISHING_FREEZE:
2258        case DIMINISHING_BANISH:
2259        case DIMINISHING_WARLOCK_FEAR:
2260        case DIMINISHING_KNOCKOUT:
2261            return DRTYPE_PLAYER;
2262    }
2263
2264    return DRTYPE_NONE;
2265}
Note: See TracBrowser for help on using the browser.