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

Revision 67, 82.5 kB (checked in by yumileroy, 17 years ago)

[svn] Change aura stack check.
Add some boss yells. Patched provided by BroodWyrm? and Blaymoira.

Original author: megamage
Date: 2008-10-19 13:56:11-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                            return false;
329                        default:
330                            break;
331                    }
332                }   break;
333                case SPELL_AURA_MOD_STAT:
334                case SPELL_AURA_MOD_DAMAGE_DONE:            // dependent from bas point sign (negative -> negative)
335                case SPELL_AURA_MOD_HEALING_DONE:
336                {
337                    if(spellproto->EffectBasePoints[effIndex]+int32(spellproto->EffectBaseDice[effIndex]) < 0)
338                        return false;
339                    break;
340                }
341                case SPELL_AURA_ADD_TARGET_TRIGGER:
342                    return true;
343                case SPELL_AURA_PERIODIC_TRIGGER_SPELL:
344                    if(spellId != spellproto->EffectTriggerSpell[effIndex])
345                    {
346                        uint32 spellTriggeredId = spellproto->EffectTriggerSpell[effIndex];
347                        SpellEntry const *spellTriggeredProto = sSpellStore.LookupEntry(spellTriggeredId);
348
349                        if(spellTriggeredProto)
350                        {
351                            // non-positive targets of main spell return early
352                            for(int i = 0; i < 3; ++i)
353                            {
354                                // if non-positive trigger cast targeted to positive target this main cast is non-positive
355                                // this will place this spell auras as debuffs
356                                if(IsPositiveTarget(spellTriggeredProto->EffectImplicitTargetA[effIndex],spellTriggeredProto->EffectImplicitTargetB[effIndex]) && !IsPositiveEffect(spellTriggeredId,i))
357                                    return false;
358                            }
359                        }
360                    }
361                    break;
362                case SPELL_AURA_PROC_TRIGGER_SPELL:
363                    // many positive auras have negative triggered spells at damage for example and this not make it negative (it can be canceled for example)
364                    break;
365                case SPELL_AURA_MOD_STUN:                   //have positive and negative spells, we can't sort its correctly at this moment.
366                    if(effIndex==0 && spellproto->Effect[1]==0 && spellproto->Effect[2]==0)
367                        return false;                       // but all single stun aura spells is negative
368
369                    // Petrification
370                    if(spellproto->Id == 17624)
371                        return false;
372                    break;
373                case SPELL_AURA_MOD_ROOT:
374                case SPELL_AURA_MOD_SILENCE:
375                case SPELL_AURA_GHOST:
376                case SPELL_AURA_PERIODIC_LEECH:
377                case SPELL_AURA_MOD_PACIFY_SILENCE:
378                case SPELL_AURA_MOD_STALKED:
379                case SPELL_AURA_PERIODIC_DAMAGE_PERCENT:
380                    return false;
381                case SPELL_AURA_PERIODIC_DAMAGE:            // used in positive spells also.
382                    // part of negative spell if casted at self (prevent cancel)
383                    if(spellproto->EffectImplicitTargetA[effIndex] == TARGET_SELF)
384                        return false;
385                    break;
386                case SPELL_AURA_MOD_DECREASE_SPEED:         // used in positive spells also
387                    // part of positive spell if casted at self
388                    if(spellproto->EffectImplicitTargetA[effIndex] != TARGET_SELF)
389                        return false;
390                    // but not this if this first effect (don't found batter check)
391                    if(spellproto->Attributes & 0x4000000 && effIndex==0)
392                        return false;
393                    break;
394                case SPELL_AURA_TRANSFORM:
395                    // some spells negative
396                    switch(spellproto->Id)
397                    {
398                        case 36897:                         // Transporter Malfunction (race mutation to horde)
399                        case 36899:                         // Transporter Malfunction (race mutation to alliance)
400                            return false;
401                    }
402                    break;
403                case SPELL_AURA_MOD_SCALE:
404                    // some spells negative
405                    switch(spellproto->Id)
406                    {
407                        case 36900:                         // Soul Split: Evil!
408                        case 36901:                         // Soul Split: Good
409                        case 36893:                         // Transporter Malfunction (decrease size case)
410                        case 36895:                         // Transporter Malfunction (increase size case)
411                            return false;
412                    }
413                    break;
414                case SPELL_AURA_MECHANIC_IMMUNITY:
415                {
416                    // non-positive immunities
417                    switch(spellproto->EffectMiscValue[effIndex])
418                    {
419                        case MECHANIC_BANDAGE:
420                        case MECHANIC_SHIELD:
421                        case MECHANIC_MOUNT:
422                        case MECHANIC_INVULNERABILITY:
423                            return false;
424                        default:
425                            break;
426                    }
427                }   break;
428                case SPELL_AURA_ADD_FLAT_MODIFIER:          // mods
429                case SPELL_AURA_ADD_PCT_MODIFIER:
430                {
431                    // non-positive mods
432                    switch(spellproto->EffectMiscValue[effIndex])
433                    {
434                        case SPELLMOD_COST:                 // dependent from bas point sign (negative -> positive)
435                            if(spellproto->EffectBasePoints[effIndex]+int32(spellproto->EffectBaseDice[effIndex]) > 0)
436                                return false;
437                            break;
438                        default:
439                            break;
440                    }
441                }   break;
442                case SPELL_AURA_MOD_HEALING_PCT:
443                    if(spellproto->EffectBasePoints[effIndex]+int32(spellproto->EffectBaseDice[effIndex]) < 0)
444                        return false;
445                    break;
446                case SPELL_AURA_MOD_SKILL:
447                    if(spellproto->EffectBasePoints[effIndex]+int32(spellproto->EffectBaseDice[effIndex]) < 0)
448                        return false;
449                    break;
450                case SPELL_AURA_FORCE_REACTION:
451                    if(spellproto->Id==42792)               // Recently Dropped Flag (prevent cancel)
452                        return false;
453                    break;
454                default:
455                    break;
456            }
457            break;
458        }
459        default:
460            break;
461    }
462
463    // non-positive targets
464    if(!IsPositiveTarget(spellproto->EffectImplicitTargetA[effIndex],spellproto->EffectImplicitTargetB[effIndex]))
465        return false;
466
467    // AttributesEx check
468    if(spellproto->AttributesEx & (1<<7))
469        return false;
470
471    // ok, positive
472    return true;
473}
474
475bool IsPositiveSpell(uint32 spellId)
476{
477    SpellEntry const *spellproto = sSpellStore.LookupEntry(spellId);
478    if (!spellproto) return false;
479
480    // spells with atleast one negative effect are considered negative
481    // some self-applied spells have negative effects but in self casting case negative check ignored.
482    for (int i = 0; i < 3; i++)
483        if (!IsPositiveEffect(spellId, i))
484            return false;
485    return true;
486}
487
488bool IsSingleTargetSpell(SpellEntry const *spellInfo)
489{
490    // all other single target spells have if it has AttributesEx5
491    if ( spellInfo->AttributesEx5 & SPELL_ATTR_EX5_SINGLE_TARGET_SPELL )
492        return true;
493
494    // TODO - need found Judgements rule
495    switch(GetSpellSpecific(spellInfo->Id))
496    {
497        case SPELL_JUDGEMENT:
498            return true;
499    }
500
501    // single target triggered spell.
502    // Not real client side single target spell, but it' not triggered until prev. aura expired.
503    // This is allow store it in single target spells list for caster for spell proc checking
504    if(spellInfo->Id==38324)                                // Regeneration (triggered by 38299 (HoTs on Heals))
505        return true;
506
507    return false;
508}
509
510bool IsSingleTargetSpells(SpellEntry const *spellInfo1, SpellEntry const *spellInfo2)
511{
512    // TODO - need better check
513    // Equal icon and spellfamily
514    if( spellInfo1->SpellFamilyName == spellInfo2->SpellFamilyName &&
515        spellInfo1->SpellIconID == spellInfo2->SpellIconID )
516        return true;
517
518    // TODO - need found Judgements rule
519    SpellSpecific spec1 = GetSpellSpecific(spellInfo1->Id);
520    // spell with single target specific types
521    switch(spec1)
522    {
523        case SPELL_JUDGEMENT:
524            if(GetSpellSpecific(spellInfo2->Id) == spec1)
525                return true;
526            break;
527    }
528
529    return false;
530}
531
532uint8 GetErrorAtShapeshiftedCast (SpellEntry const *spellInfo, uint32 form)
533{
534    // talents that learn spells can have stance requirements that need ignore
535    // (this requirement only for client-side stance show in talent description)
536    if( GetTalentSpellCost(spellInfo->Id) > 0 &&
537        (spellInfo->Effect[0]==SPELL_EFFECT_LEARN_SPELL || spellInfo->Effect[1]==SPELL_EFFECT_LEARN_SPELL || spellInfo->Effect[2]==SPELL_EFFECT_LEARN_SPELL) )
538        return 0;
539
540    uint32 stanceMask = (form ? 1 << (form - 1) : 0);
541
542    if (stanceMask & spellInfo->StancesNot)                 // can explicitly not be casted in this stance
543        return SPELL_FAILED_NOT_SHAPESHIFT;
544
545    if (stanceMask & spellInfo->Stances)                    // can explicitly be casted in this stance
546        return 0;
547
548    bool actAsShifted = false;
549    if (form > 0)
550    {
551        SpellShapeshiftEntry const *shapeInfo = sSpellShapeshiftStore.LookupEntry(form);
552        if (!shapeInfo)
553        {
554            sLog.outError("GetErrorAtShapeshiftedCast: unknown shapeshift %u", form);
555            return 0;
556        }
557        actAsShifted = !(shapeInfo->flags1 & 1);            // shapeshift acts as normal form for spells
558    }
559
560    if(actAsShifted)
561    {
562        if (spellInfo->Attributes & SPELL_ATTR_NOT_SHAPESHIFT) // not while shapeshifted
563            return SPELL_FAILED_NOT_SHAPESHIFT;
564        else if (spellInfo->Stances != 0)                   // needs other shapeshift
565            return SPELL_FAILED_ONLY_SHAPESHIFT;
566    }
567    else
568    {
569        // needs shapeshift
570        if(!(spellInfo->AttributesEx2 & SPELL_ATTR_EX2_NOT_NEED_SHAPESHIFT) && spellInfo->Stances != 0)
571            return SPELL_FAILED_ONLY_SHAPESHIFT;
572    }
573
574    return 0;
575}
576
577void SpellMgr::LoadSpellTargetPositions()
578{
579    mSpellTargetPositions.clear();                                // need for reload case
580
581    uint32 count = 0;
582
583    //                                                0   1           2                  3                  4                  5
584    QueryResult *result = WorldDatabase.Query("SELECT id, target_map, target_position_x, target_position_y, target_position_z, target_orientation FROM spell_target_position");
585    if( !result )
586    {
587
588        barGoLink bar( 1 );
589
590        bar.step();
591
592        sLog.outString();
593        sLog.outString( ">> Loaded %u spell target coordinates", count );
594        return;
595    }
596
597    barGoLink bar( result->GetRowCount() );
598
599    do
600    {
601        Field *fields = result->Fetch();
602
603        bar.step();
604
605        ++count;
606
607        uint32 Spell_ID = fields[0].GetUInt32();
608
609        SpellTargetPosition st;
610
611        st.target_mapId       = fields[1].GetUInt32();
612        st.target_X           = fields[2].GetFloat();
613        st.target_Y           = fields[3].GetFloat();
614        st.target_Z           = fields[4].GetFloat();
615        st.target_Orientation = fields[5].GetFloat();
616
617        SpellEntry const* spellInfo = sSpellStore.LookupEntry(Spell_ID);
618        if(!spellInfo)
619        {
620            sLog.outErrorDb("Spell (ID:%u) listed in `spell_target_position` does not exist.",Spell_ID);
621            continue;
622        }
623
624        bool found = false;
625        for(int i = 0; i < 3; ++i)
626        {
627            if( spellInfo->EffectImplicitTargetA[i]==TARGET_TABLE_X_Y_Z_COORDINATES || spellInfo->EffectImplicitTargetB[i]==TARGET_TABLE_X_Y_Z_COORDINATES )
628            {
629                found = true;
630                break;
631            }
632        }
633        if(!found)
634        {
635            sLog.outErrorDb("Spell (Id: %u) listed in `spell_target_position` does not have target TARGET_TABLE_X_Y_Z_COORDINATES (17).",Spell_ID);
636            continue;
637        }
638
639        MapEntry const* mapEntry = sMapStore.LookupEntry(st.target_mapId);
640        if(!mapEntry)
641        {
642            sLog.outErrorDb("Spell (ID:%u) target map (ID: %u) does not exist in `Map.dbc`.",Spell_ID,st.target_mapId);
643            continue;
644        }
645
646        if(st.target_X==0 && st.target_Y==0 && st.target_Z==0)
647        {
648            sLog.outErrorDb("Spell (ID:%u) target coordinates not provided.",Spell_ID);
649            continue;
650        }
651
652        mSpellTargetPositions[Spell_ID] = st;
653
654    } while( result->NextRow() );
655
656    delete result;
657
658    sLog.outString();
659    sLog.outString( ">> Loaded %u spell teleport coordinates", count );
660}
661
662void SpellMgr::LoadSpellAffects()
663{
664    mSpellAffectMap.clear();                                // need for reload case
665
666    uint32 count = 0;
667
668    //                                                0      1         2
669    QueryResult *result = WorldDatabase.Query("SELECT entry, effectId, SpellFamilyMask FROM spell_affect");
670    if( !result )
671    {
672
673        barGoLink bar( 1 );
674
675        bar.step();
676
677        sLog.outString();
678        sLog.outString( ">> Loaded %u spell affect definitions", count );
679        return;
680    }
681
682    barGoLink bar( result->GetRowCount() );
683
684    do
685    {
686        Field *fields = result->Fetch();
687
688        bar.step();
689
690        uint16 entry = fields[0].GetUInt16();
691        uint8 effectId = fields[1].GetUInt8();
692
693        SpellEntry const* spellInfo = sSpellStore.LookupEntry(entry);
694
695        if (!spellInfo)
696        {
697            sLog.outErrorDb("Spell %u listed in `spell_affect` does not exist", entry);
698            continue;
699        }
700
701        if (effectId >= 3)
702        {
703            sLog.outErrorDb("Spell %u listed in `spell_affect` have invalid effect index (%u)", entry,effectId);
704            continue;
705        }
706
707        if( spellInfo->Effect[effectId] != SPELL_EFFECT_APPLY_AURA ||
708            spellInfo->EffectApplyAuraName[effectId] != SPELL_AURA_ADD_FLAT_MODIFIER &&
709            spellInfo->EffectApplyAuraName[effectId] != SPELL_AURA_ADD_PCT_MODIFIER  &&
710            spellInfo->EffectApplyAuraName[effectId] != SPELL_AURA_ADD_TARGET_TRIGGER )
711        {
712            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);
713            continue;
714        }
715
716        uint64 spellAffectMask = fields[2].GetUInt64();
717
718        // Spell.dbc have own data for low part of SpellFamilyMask
719        if( spellInfo->EffectItemType[effectId])
720        {
721            if(spellInfo->EffectItemType[effectId] == spellAffectMask)
722            {
723                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);
724                continue;
725            }
726
727            // 24429 have wrong data in EffectItemType and overwrites by DB, possible bug in client
728            if(spellInfo->Id!=24429 && spellInfo->EffectItemType[effectId] != spellAffectMask)
729            {
730                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);
731                continue;
732            }
733        }
734
735        mSpellAffectMap.insert(SpellAffectMap::value_type((entry<<8) + effectId,spellAffectMask));
736
737        ++count;
738    } while( result->NextRow() );
739
740    delete result;
741
742    sLog.outString();
743    sLog.outString( ">> Loaded %u spell affect definitions", count );
744
745    /*
746    // Commented for now, as it still produces many errors (still quite many spells miss spell_affect)
747    for (uint32 id = 0; id < sSpellStore.GetNumRows(); ++id)
748    {
749        SpellEntry const* spellInfo = sSpellStore.LookupEntry(id);
750        if (!spellInfo)
751            continue;
752
753        for (int effectId = 0; effectId < 3; ++effectId)
754        {
755            if( spellInfo->Effect[effectId] != SPELL_EFFECT_APPLY_AURA ||
756                (spellInfo->EffectApplyAuraName[effectId] != SPELL_AURA_ADD_FLAT_MODIFIER &&
757                spellInfo->EffectApplyAuraName[effectId] != SPELL_AURA_ADD_PCT_MODIFIER  &&
758                spellInfo->EffectApplyAuraName[effectId] != SPELL_AURA_ADD_TARGET_TRIGGER) )
759                continue;
760
761            if(spellInfo->EffectItemType[effectId] != 0)
762                continue;
763
764            if(mSpellAffectMap.find((id<<8) + effectId) !=  mSpellAffectMap.end())
765                continue;
766
767            sLog.outErrorDb("Spell %u (%s) misses spell_affect for effect %u",id,spellInfo->SpellName[sWorld.GetDBClang()], effectId);
768        }
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)
1037        return false;
1038
1039    //I think we don't check this correctly because i need a exception for spell:
1040    //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.
1041    if(spellInfo_2->SpellFamilyFlags == 2048)
1042        return false;
1043
1044    // Resurrection sickness
1045    if((spellInfo_1->Id == SPELL_ID_PASSIVE_RESURRECTION_SICKNESS) != (spellInfo_2->Id==SPELL_ID_PASSIVE_RESURRECTION_SICKNESS))
1046        return false;
1047
1048    // Specific spell family spells
1049    switch(spellInfo_1->SpellFamilyName)
1050    {
1051        case SPELLFAMILY_GENERIC:
1052            switch(spellInfo_2->SpellFamilyName)
1053            {
1054                case SPELLFAMILY_GENERIC:                   // same family case
1055                {
1056                    // Thunderfury
1057                    if( spellInfo_1->Id == 21992 && spellInfo_2->Id == 27648 || spellInfo_2->Id == 21992 && spellInfo_1->Id == 27648 )
1058                        return false;
1059
1060                    // Lightning Speed (Mongoose) and Fury of the Crashing Waves (Tsunami Talisman)
1061                    if( spellInfo_1->Id == 28093 && spellInfo_2->Id == 42084 ||
1062                        spellInfo_2->Id == 28093 && spellInfo_1->Id == 42084 )
1063                        return false;
1064
1065                    // Soulstone Resurrection and Twisting Nether (resurrector)
1066                    if( spellInfo_1->SpellIconID == 92 && spellInfo_2->SpellIconID == 92 && (
1067                        spellInfo_1->SpellVisual == 99 && spellInfo_2->SpellVisual == 0 ||
1068                        spellInfo_2->SpellVisual == 99 && spellInfo_1->SpellVisual == 0 ) )
1069                        return false;
1070
1071                    // Heart of the Wild and (Primal Instinct (Idol of Terror) triggering spell or Agility)
1072                    if( spellInfo_1->SpellIconID == 240 && spellInfo_2->SpellIconID == 240 && (
1073                        spellInfo_1->SpellVisual == 0 && spellInfo_2->SpellVisual == 78 ||
1074                        spellInfo_2->SpellVisual == 0 && spellInfo_1->SpellVisual == 78 ) )
1075                        return false;
1076
1077                    // Personalized Weather (thunder effect should overwrite rainy aura)
1078                    if(spellInfo_1->SpellIconID == 2606 && spellInfo_2->SpellIconID == 2606)
1079                        return false;
1080
1081                    // Brood Affliction: Bronze
1082                    if( (spellInfo_1->Id == 23170 && spellInfo_2->Id == 23171) ||
1083                        (spellInfo_2->Id == 23170 && spellInfo_1->Id == 23171) )
1084                        return false;
1085
1086                    break;
1087                }
1088                case SPELLFAMILY_WARRIOR:
1089                {
1090                    // Scroll of Protection and Defensive Stance (multi-family check)
1091                    if( spellInfo_1->SpellIconID == 276 && spellInfo_1->SpellVisual == 196 && spellInfo_2->Id == 71)
1092                        return false;
1093
1094                    // Improved Hamstring -> Hamstring (multi-family check)
1095                    if( (spellInfo_2->SpellFamilyFlags & 2) && spellInfo_1->Id == 23694 )
1096                        return false;
1097
1098                    break;
1099                }
1100                case SPELLFAMILY_DRUID:
1101                {
1102                    // Scroll of Stamina and Leader of the Pack (multi-family check)
1103                    if( spellInfo_1->SpellIconID == 312 && spellInfo_1->SpellVisual == 216 && spellInfo_2->Id == 24932 )
1104                        return false;
1105
1106                    // Dragonmaw Illusion (multi-family check)
1107                    if (spellId_1 == 40216 && spellId_2 == 42016 )
1108                        return false;
1109
1110                    break;
1111                }
1112                case SPELLFAMILY_ROGUE:
1113                {
1114                    // Garrote-Silence -> Garrote (multi-family check)
1115                    if( spellInfo_1->SpellIconID == 498 && spellInfo_1->SpellVisual == 0 && spellInfo_2->SpellIconID == 498  )
1116                        return false;
1117
1118                    break;
1119                }
1120                case SPELLFAMILY_HUNTER:
1121                {
1122                    // Concussive Shot and Imp. Concussive Shot (multi-family check)
1123                    if( spellInfo_1->Id == 19410 && spellInfo_2->Id == 5116 )
1124                        return false;
1125
1126                    // Improved Wing Clip -> Wing Clip (multi-family check)
1127                    if( (spellInfo_2->SpellFamilyFlags & 0x40) && spellInfo_1->Id == 19229 )
1128                        return false;
1129                    break;
1130                }
1131                case SPELLFAMILY_PALADIN:
1132                {
1133                    // Unstable Currents and other -> *Sanctity Aura (multi-family check)
1134                    if( spellInfo_2->SpellIconID==502 && spellInfo_1->SpellIconID==502 && spellInfo_1->SpellVisual==969 )
1135                        return false;
1136
1137                    // *Band of Eternal Champion and Seal of Command(multi-family check)
1138                    if( spellId_1 == 35081 && spellInfo_2->SpellIconID==561 && spellInfo_2->SpellVisual==7992)
1139                        return false;
1140
1141                    break;
1142                }
1143            }
1144            break;
1145        case SPELLFAMILY_MAGE:
1146            if( spellInfo_2->SpellFamilyName == SPELLFAMILY_MAGE )
1147            {
1148                // Blizzard & Chilled (and some other stacked with blizzard spells
1149                if( (spellInfo_1->SpellFamilyFlags & 0x80) && (spellInfo_2->SpellFamilyFlags & 0x100000) ||
1150                    (spellInfo_2->SpellFamilyFlags & 0x80) && (spellInfo_1->SpellFamilyFlags & 0x100000) )
1151                    return false;
1152
1153                // Blink & Improved Blink
1154                if( (spellInfo_1->SpellFamilyFlags & 0x0000000000010000LL) && (spellInfo_2->SpellVisual == 72 && spellInfo_2->SpellIconID == 1499) ||
1155                    (spellInfo_2->SpellFamilyFlags & 0x0000000000010000LL) && (spellInfo_1->SpellVisual == 72 && spellInfo_1->SpellIconID == 1499) )
1156                    return false;
1157            }
1158            // Detect Invisibility and Mana Shield (multi-family check)
1159            if( spellInfo_2->Id == 132 && spellInfo_1->SpellIconID == 209 && spellInfo_1->SpellVisual == 968 )
1160                return false;
1161
1162            // Combustion and Fire Protection Aura (multi-family check)
1163            if( spellInfo_1->Id == 11129 && spellInfo_2->SpellIconID == 33 && spellInfo_2->SpellVisual == 321 )
1164                return false;
1165
1166            break;
1167        case SPELLFAMILY_WARLOCK:
1168            if( spellInfo_2->SpellFamilyName == SPELLFAMILY_WARLOCK )
1169            {
1170                // Siphon Life and Drain Life
1171                if( spellInfo_1->SpellIconID == 152 && spellInfo_2->SpellIconID == 546 ||
1172                    spellInfo_2->SpellIconID == 152 && spellInfo_1->SpellIconID == 546 )
1173                    return false;
1174
1175                //Corruption & Seed of corruption
1176                if( spellInfo_1->SpellIconID == 313 && spellInfo_2->SpellIconID == 1932 ||
1177                    spellInfo_2->SpellIconID == 313 && spellInfo_1->SpellIconID == 1932 )
1178                    if(spellInfo_1->SpellVisual != 0 && spellInfo_2->SpellVisual != 0)
1179                        return true;                        // can't be stacked
1180
1181                // Corruption and Unstable Affliction
1182                if( spellInfo_1->SpellIconID == 313 && spellInfo_2->SpellIconID == 2039 ||
1183                    spellInfo_2->SpellIconID == 313 && spellInfo_1->SpellIconID == 2039 )
1184                    return false;
1185
1186                // (Corruption or Unstable Affliction) and (Curse of Agony or Curse of Doom)
1187                if( (spellInfo_1->SpellIconID == 313 || spellInfo_1->SpellIconID == 2039) && (spellInfo_2->SpellIconID == 544  || spellInfo_2->SpellIconID == 91) ||
1188                    (spellInfo_2->SpellIconID == 313 || spellInfo_2->SpellIconID == 2039) && (spellInfo_1->SpellIconID == 544  || spellInfo_1->SpellIconID == 91) )
1189                    return false;
1190            }
1191            // Detect Invisibility and Mana Shield (multi-family check)
1192            if( spellInfo_1->Id == 132 && spellInfo_2->SpellIconID == 209 && spellInfo_2->SpellVisual == 968 )
1193                return false;
1194            break;
1195        case SPELLFAMILY_WARRIOR:
1196            if( spellInfo_2->SpellFamilyName == SPELLFAMILY_WARRIOR )
1197            {
1198                // Rend and Deep Wound
1199                if( (spellInfo_1->SpellFamilyFlags & 0x20) && (spellInfo_2->SpellFamilyFlags & 0x1000000000LL) ||
1200                    (spellInfo_2->SpellFamilyFlags & 0x20) && (spellInfo_1->SpellFamilyFlags & 0x1000000000LL) )
1201                    return false;
1202
1203                // Battle Shout and Rampage
1204                if( (spellInfo_1->SpellIconID == 456 && spellInfo_2->SpellIconID == 2006) ||
1205                    (spellInfo_2->SpellIconID == 456 && spellInfo_1->SpellIconID == 2006) )
1206                    return false;
1207            }
1208
1209            // Hamstring -> Improved Hamstring (multi-family check)
1210            if( (spellInfo_1->SpellFamilyFlags & 2) && spellInfo_2->Id == 23694 )
1211                return false;
1212
1213            // Defensive Stance and Scroll of Protection (multi-family check)
1214            if( spellInfo_1->Id == 71 && spellInfo_2->SpellIconID == 276 && spellInfo_2->SpellVisual == 196 )
1215                return false;
1216
1217            // Bloodlust and Bloodthirst (multi-family check)
1218            if( spellInfo_2->Id == 2825 && spellInfo_1->SpellIconID == 38 && spellInfo_1->SpellVisual == 0 )
1219                return false;
1220
1221            break;
1222        case SPELLFAMILY_PRIEST:
1223            if( spellInfo_2->SpellFamilyName == SPELLFAMILY_PRIEST )
1224            {
1225                //Devouring Plague and Shadow Vulnerability
1226                if( (spellInfo_1->SpellFamilyFlags & 0x2000000) && (spellInfo_2->SpellFamilyFlags & 0x800000000LL) ||
1227                    (spellInfo_2->SpellFamilyFlags & 0x2000000) && (spellInfo_1->SpellFamilyFlags & 0x800000000LL) )
1228                    return false;
1229
1230                //StarShards and Shadow Word: Pain
1231                if( (spellInfo_1->SpellFamilyFlags & 0x200000) && (spellInfo_2->SpellFamilyFlags & 0x8000) ||
1232                    (spellInfo_2->SpellFamilyFlags & 0x200000) && (spellInfo_1->SpellFamilyFlags & 0x8000) )
1233                    return false;
1234            }
1235            break;
1236        case SPELLFAMILY_DRUID:
1237            if( spellInfo_2->SpellFamilyName == SPELLFAMILY_DRUID )
1238            {
1239                //Omen of Clarity and Blood Frenzy
1240                if( (spellInfo_1->SpellFamilyFlags == 0x0 && spellInfo_1->SpellIconID == 108) && (spellInfo_2->SpellFamilyFlags & 0x20000000000000LL) ||
1241                    (spellInfo_2->SpellFamilyFlags == 0x0 && spellInfo_2->SpellIconID == 108) && (spellInfo_1->SpellFamilyFlags & 0x20000000000000LL) )
1242                    return false;
1243
1244                //  Tree of Life (Shapeshift) and 34123 Tree of Life (Passive)
1245                if ((spellId_1 == 33891 && spellId_2 == 34123) ||
1246                    (spellId_2 == 33891 && spellId_1 == 34123))
1247                    return false;
1248
1249                // Wrath of Elune and Nature's Grace
1250                if( spellInfo_1->Id == 16886 && spellInfo_2->Id == 46833 || spellInfo_2->Id == 16886 && spellInfo_1->Id == 46833 )
1251                    return false;
1252
1253                // Bear Rage (Feral T4 (2)) and Omen of Clarity
1254                if( spellInfo_1->Id == 16864 && spellInfo_2->Id == 37306 || spellInfo_2->Id == 16864 && spellInfo_1->Id == 37306 )
1255                    return false;
1256
1257                // Cat Energy (Feral T4 (2)) and Omen of Clarity
1258                if( spellInfo_1->Id == 16864 && spellInfo_2->Id == 37311 || spellInfo_2->Id == 16864 && spellInfo_1->Id == 37311 )
1259                    return false;
1260            }
1261
1262            // Leader of the Pack and Scroll of Stamina (multi-family check)
1263            if( spellInfo_1->Id == 24932 && spellInfo_2->SpellIconID == 312 && spellInfo_2->SpellVisual == 216 )
1264                return false;
1265
1266            // Dragonmaw Illusion (multi-family check)
1267            if (spellId_1 == 42016 && spellId_2 == 40216 )
1268                return false;
1269
1270            break;
1271        case SPELLFAMILY_ROGUE:
1272            if( spellInfo_2->SpellFamilyName == SPELLFAMILY_ROGUE )
1273            {
1274                // Master of Subtlety
1275                if (spellId_1 == 31665 && spellId_2 == 31666 || spellId_1 == 31666 && spellId_2 == 31665 )
1276                    return false;
1277            }
1278
1279            // Garrote -> Garrote-Silence (multi-family check)
1280            if( spellInfo_1->SpellIconID == 498 && spellInfo_2->SpellIconID == 498 && spellInfo_2->SpellVisual == 0 )
1281                return false;
1282            break;
1283        case SPELLFAMILY_HUNTER:
1284            if( spellInfo_2->SpellFamilyName == SPELLFAMILY_HUNTER )
1285            {
1286                // Rapid Fire & Quick Shots
1287                if( (spellInfo_1->SpellFamilyFlags & 0x20) && (spellInfo_2->SpellFamilyFlags & 0x20000000000LL) ||
1288                    (spellInfo_2->SpellFamilyFlags & 0x20) && (spellInfo_1->SpellFamilyFlags & 0x20000000000LL) )
1289                    return false;
1290
1291                // Serpent Sting & (Immolation/Explosive Trap Effect)
1292                if( (spellInfo_1->SpellFamilyFlags & 0x4) && (spellInfo_2->SpellFamilyFlags & 0x00000004000LL) ||
1293                    (spellInfo_2->SpellFamilyFlags & 0x4) && (spellInfo_1->SpellFamilyFlags & 0x00000004000LL) )
1294                    return false;
1295
1296                // Bestial Wrath
1297                if( spellInfo_1->SpellIconID == 1680 && spellInfo_2->SpellIconID == 1680 )
1298                    return false;
1299            }
1300
1301            // Wing Clip -> Improved Wing Clip (multi-family check)
1302            if( (spellInfo_1->SpellFamilyFlags & 0x40) && spellInfo_2->Id == 19229 )
1303                return false;
1304
1305            // Concussive Shot and Imp. Concussive Shot (multi-family check)
1306            if( spellInfo_2->Id == 19410 && spellInfo_1->Id == 5116 )
1307                return false;
1308            break;
1309        case SPELLFAMILY_PALADIN:
1310            if( spellInfo_2->SpellFamilyName == SPELLFAMILY_PALADIN )
1311            {
1312                // Paladin Seals
1313                if( IsSealSpell(spellInfo_1) && IsSealSpell(spellInfo_2) )
1314                    return true;
1315            }
1316            // Combustion and Fire Protection Aura (multi-family check)
1317            if( spellInfo_2->Id == 11129 && spellInfo_1->SpellIconID == 33 && spellInfo_1->SpellVisual == 321 )
1318                return false;
1319
1320            // *Sanctity Aura -> Unstable Currents and other (multi-family check)
1321            if( spellInfo_1->SpellIconID==502 && spellInfo_2->SpellFamilyName == SPELLFAMILY_GENERIC && spellInfo_2->SpellIconID==502 && spellInfo_2->SpellVisual==969 )
1322                return false;
1323
1324            // *Seal of Command and Band of Eternal Champion (multi-family check)
1325            if( spellInfo_1->SpellIconID==561 && spellInfo_1->SpellVisual==7992 && spellId_2 == 35081)
1326                return false;
1327            break;
1328        case SPELLFAMILY_SHAMAN:
1329            if( spellInfo_2->SpellFamilyName == SPELLFAMILY_SHAMAN )
1330            {
1331                // shaman shields
1332                if( IsElementalShield(spellInfo_1) && IsElementalShield(spellInfo_2) )
1333                    return true;
1334
1335                // Windfury weapon
1336                if( spellInfo_1->SpellIconID==220 && spellInfo_2->SpellIconID==220 &&
1337                    spellInfo_1->SpellFamilyFlags != spellInfo_2->SpellFamilyFlags )
1338                    return false;
1339            }
1340            // Bloodlust and Bloodthirst (multi-family check)
1341            if( spellInfo_1->Id == 2825 && spellInfo_2->SpellIconID == 38 && spellInfo_2->SpellVisual == 0 )
1342                return false;
1343            break;
1344        default:
1345            break;
1346    }
1347
1348    if (IsRankSpellDueToSpell(spellInfo_1, spellId_2))
1349        return true;
1350
1351    if (spellInfo_1->SpellIconID != spellInfo_2->SpellIconID ||
1352        !spellInfo_1->SpellIconID)
1353        return false;
1354
1355    if (spellInfo_1->SpellFamilyName != spellInfo_2->SpellFamilyName)
1356        return false;
1357
1358    for (int i = 0; i < 3; ++i)
1359        if (spellInfo_1->Effect[i] != spellInfo_2->Effect[i] ||
1360        spellInfo_1->EffectItemType[i] != spellInfo_2->EffectItemType[i] ||
1361        spellInfo_1->EffectMiscValue[i] != spellInfo_2->EffectMiscValue[i] ||
1362        spellInfo_1->EffectApplyAuraName[i] != spellInfo_2->EffectApplyAuraName[i])
1363            return false;
1364
1365    return true;
1366}
1367
1368bool SpellMgr::IsProfessionSpell(uint32 spellId)
1369{
1370    SpellEntry const *spellInfo = sSpellStore.LookupEntry(spellId);
1371    if(!spellInfo)
1372        return false;
1373
1374    if(spellInfo->Effect[1] != SPELL_EFFECT_SKILL)
1375        return false;
1376
1377    uint32 skill = spellInfo->EffectMiscValue[1];
1378
1379    return IsProfessionSkill(skill);
1380}
1381
1382bool SpellMgr::IsPrimaryProfessionSpell(uint32 spellId)
1383{
1384    SpellEntry const *spellInfo = sSpellStore.LookupEntry(spellId);
1385    if(!spellInfo)
1386        return false;
1387
1388    if(spellInfo->Effect[1] != SPELL_EFFECT_SKILL)
1389        return false;
1390
1391    uint32 skill = spellInfo->EffectMiscValue[1];
1392
1393    return IsPrimaryProfessionSkill(skill);
1394}
1395
1396bool SpellMgr::IsPrimaryProfessionFirstRankSpell(uint32 spellId) const
1397{
1398    return IsPrimaryProfessionSpell(spellId) && GetSpellRank(spellId)==1;
1399}
1400
1401SpellEntry const* SpellMgr::SelectAuraRankForPlayerLevel(SpellEntry const* spellInfo, uint32 playerLevel) const
1402{
1403    // ignore passive spells
1404    if(IsPassiveSpell(spellInfo->Id))
1405        return spellInfo;
1406
1407    bool needRankSelection = false;
1408    for(int i=0;i<3;i++)
1409    {
1410        if( IsPositiveEffect(spellInfo->Id, i) && (
1411            spellInfo->Effect[i] == SPELL_EFFECT_APPLY_AURA ||
1412            spellInfo->Effect[i] == SPELL_EFFECT_APPLY_AREA_AURA_PARTY
1413            ) )
1414        {
1415            needRankSelection = true;
1416            break;
1417        }
1418    }
1419
1420    // not required
1421    if(!needRankSelection)
1422        return spellInfo;
1423
1424    for(uint32 nextSpellId = spellInfo->Id; nextSpellId != 0; nextSpellId = GetPrevSpellInChain(nextSpellId))
1425    {
1426        SpellEntry const *nextSpellInfo = sSpellStore.LookupEntry(nextSpellId);
1427        if(!nextSpellInfo)
1428            break;
1429
1430        // if found appropriate level
1431        if(playerLevel + 10 >= nextSpellInfo->spellLevel)
1432            return nextSpellInfo;
1433
1434        // one rank less then
1435    }
1436
1437    // not found
1438    return NULL;
1439}
1440
1441void SpellMgr::LoadSpellChains()
1442{
1443    mSpellChains.clear();                                   // need for reload case
1444    mSpellChainsNext.clear();                               // need for reload case
1445
1446    QueryResult *result = WorldDatabase.PQuery("SELECT spell_id, prev_spell, first_spell, rank, req_spell FROM spell_chain");
1447    if(result == NULL)
1448    {
1449        barGoLink bar( 1 );
1450        bar.step();
1451
1452        sLog.outString();
1453        sLog.outString( ">> Loaded 0 spell chain records" );
1454        sLog.outErrorDb("`spell_chains` table is empty!");
1455        return;
1456    }
1457
1458    uint32 count = 0;
1459
1460    barGoLink bar( result->GetRowCount() );
1461    do
1462    {
1463        bar.step();
1464        Field *fields = result->Fetch();
1465
1466        uint32 spell_id = fields[0].GetUInt32();
1467
1468        SpellChainNode node;
1469        node.prev  = fields[1].GetUInt32();
1470        node.first = fields[2].GetUInt32();
1471        node.rank  = fields[3].GetUInt8();
1472        node.req   = fields[4].GetUInt32();
1473
1474        if(!sSpellStore.LookupEntry(spell_id))
1475        {
1476            sLog.outErrorDb("Spell %u listed in `spell_chain` does not exist",spell_id);
1477            continue;
1478        }
1479
1480        if(node.prev!=0 && !sSpellStore.LookupEntry(node.prev))
1481        {
1482            sLog.outErrorDb("Spell %u (prev: %u, first: %u, rank: %d, req: %u) listed in `spell_chain` has not existed previous rank spell.",
1483                spell_id,node.prev,node.first,node.rank,node.req);
1484            continue;
1485        }
1486
1487        if(!sSpellStore.LookupEntry(node.first))
1488        {
1489            sLog.outErrorDb("Spell %u (prev: %u, first: %u, rank: %d, req: %u) listed in `spell_chain` has not existing first rank spell.",
1490                spell_id,node.prev,node.first,node.rank,node.req);
1491            continue;
1492        }
1493
1494        // check basic spell chain data integrity (note: rank can be equal 0 or 1 for first/single spell)
1495        if( (spell_id == node.first) != (node.rank <= 1) ||
1496            (spell_id == node.first) != (node.prev == 0) ||
1497            (node.rank <= 1) != (node.prev == 0) )
1498        {
1499            sLog.outErrorDb("Spell %u (prev: %u, first: %u, rank: %d, req: %u) listed in `spell_chain` has not compatible chain data.",
1500                spell_id,node.prev,node.first,node.rank,node.req);
1501            continue;
1502        }
1503
1504        if(node.req!=0 && !sSpellStore.LookupEntry(node.req))
1505        {
1506            sLog.outErrorDb("Spell %u (prev: %u, first: %u, rank: %d, req: %u) listed in `spell_chain` has not existing required spell.",
1507                spell_id,node.prev,node.first,node.rank,node.req);
1508            continue;
1509        }
1510
1511        // talents not required data in spell chain for work, but must be checked if present for intergrity
1512        if(TalentSpellPos const* pos = GetTalentSpellPos(spell_id))
1513        {
1514            if(node.rank!=pos->rank+1)
1515            {
1516                sLog.outErrorDb("Talent %u (prev: %u, first: %u, rank: %d, req: %u) listed in `spell_chain` has wrong rank.",
1517                    spell_id,node.prev,node.first,node.rank,node.req);
1518                continue;
1519            }
1520
1521            if(TalentEntry const* talentEntry = sTalentStore.LookupEntry(pos->talent_id))
1522            {
1523                if(node.first!=talentEntry->RankID[0])
1524                {
1525                    sLog.outErrorDb("Talent %u (prev: %u, first: %u, rank: %d, req: %u) listed in `spell_chain` has wrong first rank spell.",
1526                        spell_id,node.prev,node.first,node.rank,node.req);
1527                    continue;
1528                }
1529
1530                if(node.rank > 1 && node.prev != talentEntry->RankID[node.rank-1-1])
1531                {
1532                    sLog.outErrorDb("Talent %u (prev: %u, first: %u, rank: %d, req: %u) listed in `spell_chain` has wrong prev rank spell.",
1533                        spell_id,node.prev,node.first,node.rank,node.req);
1534                    continue;
1535                }
1536
1537                if(node.req!=talentEntry->DependsOnSpell)
1538                {
1539                    sLog.outErrorDb("Talent %u (prev: %u, first: %u, rank: %d, req: %u) listed in `spell_chain` has wrong required spell.",
1540                        spell_id,node.prev,node.first,node.rank,node.req);
1541                    continue;
1542                }
1543            }
1544        }
1545
1546        mSpellChains[spell_id] = node;
1547
1548        if(node.prev)
1549            mSpellChainsNext.insert(SpellChainMapNext::value_type(node.prev,spell_id));
1550
1551        if(node.req)
1552            mSpellChainsNext.insert(SpellChainMapNext::value_type(node.req,spell_id));
1553
1554        ++count;
1555    } while( result->NextRow() );
1556
1557    // additional integrity checks
1558    for(SpellChainMap::iterator i = mSpellChains.begin(); i != mSpellChains.end(); ++i)
1559    {
1560        if(i->second.prev)
1561        {
1562            SpellChainMap::iterator i_prev = mSpellChains.find(i->second.prev);
1563            if(i_prev == mSpellChains.end())
1564            {
1565                sLog.outErrorDb("Spell %u (prev: %u, first: %u, rank: %d, req: %u) listed in `spell_chain` has not found previous rank spell in table.",
1566                    i->first,i->second.prev,i->second.first,i->second.rank,i->second.req);
1567            }
1568            else if( i_prev->second.first != i->second.first )
1569            {
1570                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).",
1571                    i->first,i->second.prev,i->second.first,i->second.rank,i->second.req,
1572                    i_prev->second.prev,i_prev->second.first,i_prev->second.rank,i_prev->second.req);
1573            }
1574            else if( i_prev->second.rank+1 != i->second.rank )
1575            {
1576                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).",
1577                    i->first,i->second.prev,i->second.first,i->second.rank,i->second.req,
1578                    i_prev->second.prev,i_prev->second.first,i_prev->second.rank,i_prev->second.req);
1579            }
1580        }
1581
1582        if(i->second.req)
1583        {
1584            SpellChainMap::iterator i_req = mSpellChains.find(i->second.req);
1585            if(i_req == mSpellChains.end())
1586            {
1587                sLog.outErrorDb("Spell %u (prev: %u, first: %u, rank: %d, req: %u) listed in `spell_chain` has not found required rank spell in table.",
1588                    i->first,i->second.prev,i->second.first,i->second.rank,i->second.req);
1589            }
1590            else if( i_req->second.first == i->second.first )
1591            {
1592                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).",
1593                    i->first,i->second.prev,i->second.first,i->second.rank,i->second.req,
1594                    i_req->second.prev,i_req->second.first,i_req->second.rank,i_req->second.req);
1595            }
1596            else if( i_req->second.req )
1597            {
1598                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).",
1599                    i->first,i->second.prev,i->second.first,i->second.rank,i->second.req,
1600                    i_req->second.prev,i_req->second.first,i_req->second.rank,i_req->second.req);
1601            }
1602        }
1603    }
1604
1605    delete result;
1606
1607    sLog.outString();
1608    sLog.outString( ">> Loaded %u spell chain records", count );
1609}
1610
1611void SpellMgr::LoadSpellLearnSkills()
1612{
1613    mSpellLearnSkills.clear();                              // need for reload case
1614
1615    // search auto-learned skills and add its to map also for use in unlearn spells/talents
1616    uint32 dbc_count = 0;
1617    for(uint32 spell = 0; spell < sSpellStore.GetNumRows(); ++spell)
1618    {
1619        SpellEntry const* entry = sSpellStore.LookupEntry(spell);
1620
1621        if(!entry)
1622            continue;
1623
1624        for(int i = 0; i < 3; ++i)
1625        {
1626            if(entry->Effect[i]==SPELL_EFFECT_SKILL)
1627            {
1628                SpellLearnSkillNode dbc_node;
1629                dbc_node.skill    = entry->EffectMiscValue[i];
1630                if ( dbc_node.skill != SKILL_RIDING )
1631                    dbc_node.value    = 1;
1632                else
1633                    dbc_node.value    = (entry->EffectBasePoints[i]+1)*75;
1634                dbc_node.maxvalue = (entry->EffectBasePoints[i]+1)*75;
1635
1636                SpellLearnSkillNode const* db_node = GetSpellLearnSkill(spell);
1637
1638                mSpellLearnSkills[spell] = dbc_node;
1639                ++dbc_count;
1640                break;
1641            }
1642        }
1643    }
1644
1645    sLog.outString();
1646    sLog.outString( ">> Loaded %u Spell Learn Skills from DBC", dbc_count );
1647}
1648
1649void SpellMgr::LoadSpellLearnSpells()
1650{
1651    mSpellLearnSpells.clear();                              // need for reload case
1652
1653    QueryResult *result = WorldDatabase.PQuery("SELECT entry, SpellID FROM spell_learn_spell");
1654    if(!result)
1655    {
1656        barGoLink bar( 1 );
1657        bar.step();
1658
1659        sLog.outString();
1660        sLog.outString( ">> Loaded 0 spell learn spells" );
1661        sLog.outErrorDb("`spell_learn_spell` table is empty!");
1662        return;
1663    }
1664
1665    uint32 count = 0;
1666
1667    barGoLink bar( result->GetRowCount() );
1668    do
1669    {
1670        bar.step();
1671        Field *fields = result->Fetch();
1672
1673        uint32 spell_id    = fields[0].GetUInt32();
1674
1675        SpellLearnSpellNode node;
1676        node.spell      = fields[1].GetUInt32();
1677        node.autoLearned= false;
1678
1679        if(!sSpellStore.LookupEntry(spell_id))
1680        {
1681            sLog.outErrorDb("Spell %u listed in `spell_learn_spell` does not exist",spell_id);
1682            continue;
1683        }
1684
1685        if(!sSpellStore.LookupEntry(node.spell))
1686        {
1687            sLog.outErrorDb("Spell %u listed in `spell_learn_spell` does not exist",node.spell);
1688            continue;
1689        }
1690
1691        mSpellLearnSpells.insert(SpellLearnSpellMap::value_type(spell_id,node));
1692
1693        ++count;
1694    } while( result->NextRow() );
1695
1696    delete result;
1697
1698    // search auto-learned spells and add its to map also for use in unlearn spells/talents
1699    uint32 dbc_count = 0;
1700    for(uint32 spell = 0; spell < sSpellStore.GetNumRows(); ++spell)
1701    {
1702        SpellEntry const* entry = sSpellStore.LookupEntry(spell);
1703
1704        if(!entry)
1705            continue;
1706
1707        for(int i = 0; i < 3; ++i)
1708        {
1709            if(entry->Effect[i]==SPELL_EFFECT_LEARN_SPELL)
1710            {
1711                SpellLearnSpellNode dbc_node;
1712                dbc_node.spell       = entry->EffectTriggerSpell[i];
1713                dbc_node.autoLearned = true;
1714
1715                SpellLearnSpellMap::const_iterator db_node_begin = GetBeginSpellLearnSpell(spell);
1716                SpellLearnSpellMap::const_iterator db_node_end   = GetEndSpellLearnSpell(spell);
1717
1718                bool found = false;
1719                for(SpellLearnSpellMap::const_iterator itr = db_node_begin; itr != db_node_end; ++itr)
1720                {
1721                    if(itr->second.spell == dbc_node.spell)
1722                    {
1723                        sLog.outErrorDb("Spell %u auto-learn spell %u in spell.dbc then the record in `spell_learn_spell` is redundant, please fix DB.",
1724                            spell,dbc_node.spell);
1725                        found = true;
1726                        break;
1727                    }
1728                }
1729
1730                if(!found)                                  // add new spell-spell pair if not found
1731                {
1732                    mSpellLearnSpells.insert(SpellLearnSpellMap::value_type(spell,dbc_node));
1733                    ++dbc_count;
1734                }
1735            }
1736        }
1737    }
1738
1739    sLog.outString();
1740    sLog.outString( ">> Loaded %u spell learn spells + %u found in DBC", count, dbc_count );
1741}
1742
1743void SpellMgr::LoadSpellScriptTarget()
1744{
1745    mSpellScriptTarget.clear();                             // need for reload case
1746
1747    uint32 count = 0;
1748
1749    QueryResult *result = WorldDatabase.Query("SELECT entry,type,targetEntry FROM spell_script_target");
1750
1751    if(!result)
1752    {
1753        barGoLink bar(1);
1754
1755        bar.step();
1756
1757        sLog.outString();
1758        sLog.outErrorDb(">> Loaded 0 SpellScriptTarget. DB table `spell_script_target` is empty.");
1759        return;
1760    }
1761
1762    barGoLink bar(result->GetRowCount());
1763
1764    do
1765    {
1766        Field *fields = result->Fetch();
1767        bar.step();
1768
1769        uint32 spellId     = fields[0].GetUInt32();
1770        uint32 type        = fields[1].GetUInt32();
1771        uint32 targetEntry = fields[2].GetUInt32();
1772
1773        SpellEntry const* spellProto = sSpellStore.LookupEntry(spellId);
1774
1775        if(!spellProto)
1776        {
1777            sLog.outErrorDb("Table `spell_script_target`: spellId %u listed for TargetEntry %u does not exist.",spellId,targetEntry);
1778            continue;
1779        }
1780
1781        bool targetfound = false;
1782        for(int i = 0; i <3; ++i)
1783        {
1784            if( spellProto->EffectImplicitTargetA[i]==TARGET_SCRIPT ||
1785                spellProto->EffectImplicitTargetB[i]==TARGET_SCRIPT ||
1786                spellProto->EffectImplicitTargetA[i]==TARGET_SCRIPT_COORDINATES ||
1787                spellProto->EffectImplicitTargetB[i]==TARGET_SCRIPT_COORDINATES )
1788            {
1789                targetfound = true;
1790                break;
1791            }
1792        }
1793        if(!targetfound)
1794        {
1795            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);
1796            continue;
1797        }
1798
1799        if( type >= MAX_SPELL_TARGET_TYPE )
1800        {
1801            sLog.outErrorDb("Table `spell_script_target`: target type %u for TargetEntry %u is incorrect.",type,targetEntry);
1802            continue;
1803        }
1804
1805        switch(type)
1806        {
1807            case SPELL_TARGET_TYPE_GAMEOBJECT:
1808            {
1809                if( targetEntry==0 )
1810                    break;
1811
1812                if(!sGOStorage.LookupEntry<GameObjectInfo>(targetEntry))
1813                {
1814                    sLog.outErrorDb("Table `spell_script_target`: gameobject template entry %u does not exist.",targetEntry);
1815                    continue;
1816                }
1817                break;
1818            }
1819            default:
1820            {
1821                if( targetEntry==0 )
1822                {
1823                    sLog.outErrorDb("Table `spell_script_target`: target entry == 0 for not GO target type (%u).",type);
1824                    continue;
1825                }
1826                if(!sCreatureStorage.LookupEntry<CreatureInfo>(targetEntry))
1827                {
1828                    sLog.outErrorDb("Table `spell_script_target`: creature template entry %u does not exist.",targetEntry);
1829                    continue;
1830                }
1831                const CreatureInfo* cInfo = sCreatureStorage.LookupEntry<CreatureInfo>(targetEntry);
1832
1833                if(spellId == 30427 && !cInfo->SkinLootId)
1834                {
1835                    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);
1836                    continue;
1837                }
1838                break;
1839            }
1840        }
1841
1842        mSpellScriptTarget.insert(SpellScriptTarget::value_type(spellId,SpellTargetEntry(SpellTargetType(type),targetEntry)));
1843
1844        ++count;
1845    } while (result->NextRow());
1846
1847    delete result;
1848
1849    // Check all spells
1850    /* Disabled (lot errors at this moment)
1851    for(uint32 i = 1; i < sSpellStore.nCount; ++i)
1852    {
1853        SpellEntry const * spellInfo = sSpellStore.LookupEntry(i);
1854        if(!spellInfo)
1855            continue;
1856
1857        bool found = false;
1858        for(int j=0; j<3; ++j)
1859        {
1860            if( spellInfo->EffectImplicitTargetA[j] == TARGET_SCRIPT || spellInfo->EffectImplicitTargetA[j] != TARGET_SELF && spellInfo->EffectImplicitTargetB[j] == TARGET_SCRIPT )
1861            {
1862                SpellScriptTarget::const_iterator lower = spellmgr.GetBeginSpellScriptTarget(spellInfo->Id);
1863                SpellScriptTarget::const_iterator upper = spellmgr.GetEndSpellScriptTarget(spellInfo->Id);
1864                if(lower==upper)
1865                {
1866                    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);
1867                    break;                                  // effects of spell
1868                }
1869            }
1870        }
1871    }
1872    */
1873
1874    sLog.outString();
1875    sLog.outString(">> Loaded %u Spell Script Targets", count);
1876}
1877
1878void SpellMgr::LoadSpellPetAuras()
1879{
1880    mSpellPetAuraMap.clear();                                  // need for reload case
1881
1882    uint32 count = 0;
1883
1884    //                                                0      1    2
1885    QueryResult *result = WorldDatabase.Query("SELECT spell, pet, aura FROM spell_pet_auras");
1886    if( !result )
1887    {
1888
1889        barGoLink bar( 1 );
1890
1891        bar.step();
1892
1893        sLog.outString();
1894        sLog.outString( ">> Loaded %u spell pet auras", count );
1895        return;
1896    }
1897
1898    barGoLink bar( result->GetRowCount() );
1899
1900    do
1901    {
1902        Field *fields = result->Fetch();
1903
1904        bar.step();
1905
1906        uint16 spell = fields[0].GetUInt16();
1907        uint16 pet = fields[1].GetUInt16();
1908        uint16 aura = fields[2].GetUInt16();
1909
1910        SpellPetAuraMap::iterator itr = mSpellPetAuraMap.find(spell);
1911        if(itr != mSpellPetAuraMap.end())
1912        {
1913            itr->second.AddAura(pet, aura);
1914        }
1915        else
1916        {
1917            SpellEntry const* spellInfo = sSpellStore.LookupEntry(spell);
1918            if (!spellInfo)
1919            {
1920                sLog.outErrorDb("Spell %u listed in `spell_pet_auras` does not exist", spell);
1921                continue;
1922            }
1923            int i = 0;
1924            for(; i < 3; ++i)
1925                if((spellInfo->Effect[i] == SPELL_EFFECT_APPLY_AURA &&
1926                    spellInfo->EffectApplyAuraName[i] == SPELL_AURA_DUMMY) ||
1927                    spellInfo->Effect[i] == SPELL_EFFECT_DUMMY)
1928                    break;
1929
1930            if(i == 3)
1931            {
1932                sLog.outError("Spell %u listed in `spell_pet_auras` does not have dummy aura or dummy effect", spell);
1933                continue;
1934            }
1935
1936            SpellEntry const* spellInfo2 = sSpellStore.LookupEntry(aura);
1937            if (!spellInfo2)
1938            {
1939                sLog.outErrorDb("Aura %u listed in `spell_pet_auras` does not exist", aura);
1940                continue;
1941            }
1942
1943            PetAura pa(pet, aura, spellInfo->EffectImplicitTargetA[i] == TARGET_PET, spellInfo->EffectBasePoints[i] + spellInfo->EffectBaseDice[i]);
1944            mSpellPetAuraMap[spell] = pa;
1945        }
1946
1947        ++count;
1948    } while( result->NextRow() );
1949
1950    delete result;
1951
1952    sLog.outString();
1953    sLog.outString( ">> Loaded %u spell pet auras", count );
1954}
1955
1956/// Some checks for spells, to prevent adding depricated/broken spells for trainers, spell book, etc
1957bool SpellMgr::IsSpellValid(SpellEntry const* spellInfo, Player* pl, bool msg)
1958{
1959    // not exist
1960    if(!spellInfo)
1961        return false;
1962
1963    bool need_check_reagents = false;
1964
1965    // check effects
1966    for(int i=0; i<3; ++i)
1967    {
1968        switch(spellInfo->Effect[i])
1969        {
1970            case 0:
1971                continue;
1972
1973                // craft spell for crafting non-existed item (break client recipes list show)
1974            case SPELL_EFFECT_CREATE_ITEM:
1975            {
1976                if(!ObjectMgr::GetItemPrototype( spellInfo->EffectItemType[i] ))
1977                {
1978                    if(msg)
1979                    {
1980                        if(pl)
1981                            ChatHandler(pl).PSendSysMessage("Craft spell %u create not-exist in DB item (Entry: %u) and then...",spellInfo->Id,spellInfo->EffectItemType[i]);
1982                        else
1983                            sLog.outErrorDb("Craft spell %u create not-exist in DB item (Entry: %u) and then...",spellInfo->Id,spellInfo->EffectItemType[i]);
1984                    }
1985                    return false;
1986                }
1987
1988                need_check_reagents = true;
1989                break;
1990            }
1991            case SPELL_EFFECT_LEARN_SPELL:
1992            {
1993                SpellEntry const* spellInfo2 = sSpellStore.LookupEntry(spellInfo->EffectTriggerSpell[0]);
1994                if( !IsSpellValid(spellInfo2,pl,msg) )
1995                {
1996                    if(msg)
1997                    {
1998                        if(pl)
1999                            ChatHandler(pl).PSendSysMessage("Spell %u learn to broken spell %u, and then...",spellInfo->Id,spellInfo->EffectTriggerSpell[0]);
2000                        else
2001                            sLog.outErrorDb("Spell %u learn to invalid spell %u, and then...",spellInfo->Id,spellInfo->EffectTriggerSpell[0]);
2002                    }
2003                    return false;
2004                }
2005                break;
2006            }
2007        }
2008    }
2009
2010    if(need_check_reagents)
2011    {
2012        for(int j = 0; j < 8; ++j)
2013        {
2014            if(spellInfo->Reagent[j] > 0 && !ObjectMgr::GetItemPrototype( spellInfo->Reagent[j] ))
2015            {
2016                if(msg)
2017                {
2018                    if(pl)
2019                        ChatHandler(pl).PSendSysMessage("Craft spell %u have not-exist reagent in DB item (Entry: %u) and then...",spellInfo->Id,spellInfo->Reagent[j]);
2020                    else
2021                        sLog.outErrorDb("Craft spell %u have not-exist reagent in DB item (Entry: %u) and then...",spellInfo->Id,spellInfo->Reagent[j]);
2022                }
2023                return false;
2024            }
2025        }
2026    }
2027
2028    return true;
2029}
2030
2031bool IsSpellAllowedInLocation(SpellEntry const *spellInfo,uint32 map_id,uint32 zone_id,uint32 area_id)
2032{
2033    // normal case
2034    if( spellInfo->AreaId && spellInfo->AreaId != zone_id && spellInfo->AreaId != area_id )
2035        return false;
2036
2037    // elixirs (all area dependent elixirs have family SPELLFAMILY_POTION, use this for speedup)
2038    if(spellInfo->SpellFamilyName==SPELLFAMILY_POTION)
2039    {
2040        if(uint32 mask = spellmgr.GetSpellElixirMask(spellInfo->Id))
2041        {
2042            if(mask & ELIXIR_UNSTABLE_MASK)
2043            {
2044                // in the Blade's Edge Mountains Plateaus and Gruul's Lair.
2045                return zone_id ==3522 || map_id==565;
2046            }
2047            if(mask & ELIXIR_UNSTABLE_MASK)
2048            {
2049                // in Tempest Keep, Serpentshrine Cavern, Caverns of Time: Mount Hyjal, Black Temple
2050                // TODO: and the Sunwell Plateau
2051                if(zone_id ==3607 || map_id==534 || map_id==564)
2052                    return true;
2053
2054                MapEntry const* mapEntry = sMapStore.LookupEntry(map_id);
2055                if(!mapEntry)
2056                    return false;
2057
2058                return mapEntry->multimap_id==206;
2059            }
2060
2061            // elixirs not have another limitations
2062            return true;
2063        }
2064    }
2065
2066    // special cases zone check (maps checked by multimap common id)
2067    switch(spellInfo->Id)
2068    {
2069        case 41618:
2070        case 41620:
2071        {
2072            MapEntry const* mapEntry = sMapStore.LookupEntry(map_id);
2073            if(!mapEntry)
2074                return false;
2075
2076            return mapEntry->multimap_id==206;
2077        }
2078
2079        case 41617:
2080        case 41619:
2081        {
2082            MapEntry const* mapEntry = sMapStore.LookupEntry(map_id);
2083            if(!mapEntry)
2084                return false;
2085
2086            return mapEntry->multimap_id==207;
2087        }
2088        // Dragonmaw Illusion
2089        case 40216:
2090        case 42016:
2091        {
2092            if ( area_id != 3759 && area_id != 3966 && area_id != 3939 )
2093                return false;
2094            break;
2095        }
2096    }
2097
2098    return true;
2099}
2100
2101void SpellMgr::LoadSkillLineAbilityMap()
2102{
2103    mSkillLineAbilityMap.clear();
2104
2105    uint32 count = 0;
2106
2107    for (uint32 i = 0; i < sSkillLineAbilityStore.GetNumRows(); i++)
2108    {
2109        SkillLineAbilityEntry const *SkillInfo = sSkillLineAbilityStore.LookupEntry(i);
2110        if(!SkillInfo)
2111            continue;
2112
2113        mSkillLineAbilityMap.insert(SkillLineAbilityMap::value_type(SkillInfo->spellId,SkillInfo));
2114        ++count;
2115    }
2116
2117    sLog.outString();
2118    sLog.outString(">> Loaded %u SkillLineAbility MultiMap", count);
2119}
2120
2121DiminishingGroup GetDiminishingReturnsGroupForSpell(SpellEntry const* spellproto, bool triggered)
2122{
2123    // Explicit Diminishing Groups
2124    switch(spellproto->SpellFamilyName)
2125    {
2126        case SPELLFAMILY_MAGE:
2127        {
2128            // Polymorph
2129            if ((spellproto->SpellFamilyFlags & 0x00001000000LL) && spellproto->EffectApplyAuraName[0]==SPELL_AURA_MOD_CONFUSE)
2130                return DIMINISHING_POLYMORPH;
2131            break;
2132        }
2133        case SPELLFAMILY_ROGUE:
2134        {
2135            // Kidney Shot
2136            if (spellproto->SpellFamilyFlags & 0x00000200000LL)
2137                return DIMINISHING_KIDNEYSHOT;
2138            // Blind
2139            else if (spellproto->SpellFamilyFlags & 0x00001000000LL)
2140                return DIMINISHING_BLIND_CYCLONE;
2141            break;
2142        }
2143        case SPELLFAMILY_WARLOCK:
2144        {
2145            // Death Coil
2146            if (spellproto->SpellFamilyFlags & 0x00000080000LL)
2147                return DIMINISHING_DEATHCOIL;
2148            // Fear
2149            else if (spellproto->SpellFamilyFlags & 0x40840000000LL)
2150                return DIMINISHING_WARLOCK_FEAR;
2151            // Curses/etc
2152            else if (spellproto->SpellFamilyFlags & 0x00080000000LL)
2153                return DIMINISHING_LIMITONLY;
2154            break;
2155        }
2156        case SPELLFAMILY_DRUID:
2157        {
2158            // Cyclone
2159            if (spellproto->SpellFamilyFlags & 0x02000000000LL)
2160                return DIMINISHING_BLIND_CYCLONE;
2161            break;
2162        }
2163        case SPELLFAMILY_WARRIOR:
2164        {
2165            // Hamstring - limit duration to 10s in PvP
2166            if (spellproto->SpellFamilyFlags & 0x00000000002LL)
2167                return DIMINISHING_LIMITONLY;
2168            break;
2169        }
2170        default:
2171            break;
2172    }
2173
2174    // Get by mechanic
2175    for (uint8 i=0;i<3;++i)
2176    {
2177        if (spellproto->Mechanic      == MECHANIC_STUN    || spellproto->EffectMechanic[i] == MECHANIC_STUN)
2178            return triggered ? DIMINISHING_TRIGGER_STUN : DIMINISHING_CONTROL_STUN;
2179        else if (spellproto->Mechanic == MECHANIC_SLEEP   || spellproto->EffectMechanic[i] == MECHANIC_SLEEP)
2180            return DIMINISHING_SLEEP;
2181        else if (spellproto->Mechanic == MECHANIC_ROOT    || spellproto->EffectMechanic[i] == MECHANIC_ROOT)
2182            return triggered ? DIMINISHING_TRIGGER_ROOT : DIMINISHING_CONTROL_ROOT;
2183        else if (spellproto->Mechanic == MECHANIC_FEAR    || spellproto->EffectMechanic[i] == MECHANIC_FEAR)
2184            return DIMINISHING_FEAR;
2185        else if (spellproto->Mechanic == MECHANIC_CHARM   || spellproto->EffectMechanic[i] == MECHANIC_CHARM)
2186            return DIMINISHING_CHARM;
2187        else if (spellproto->Mechanic == MECHANIC_SILENCE || spellproto->EffectMechanic[i] == MECHANIC_SILENCE)
2188            return DIMINISHING_SILENCE;
2189        else if (spellproto->Mechanic == MECHANIC_DISARM  || spellproto->EffectMechanic[i] == MECHANIC_DISARM)
2190            return DIMINISHING_DISARM;
2191        else if (spellproto->Mechanic == MECHANIC_FREEZE  || spellproto->EffectMechanic[i] == MECHANIC_FREEZE)
2192            return DIMINISHING_FREEZE;
2193        else if (spellproto->Mechanic == MECHANIC_KNOCKOUT|| spellproto->EffectMechanic[i] == MECHANIC_KNOCKOUT ||
2194                 spellproto->Mechanic == MECHANIC_SAPPED  || spellproto->EffectMechanic[i] == MECHANIC_SAPPED)
2195            return DIMINISHING_KNOCKOUT;
2196        else if (spellproto->Mechanic == MECHANIC_BANISH  || spellproto->EffectMechanic[i] == MECHANIC_BANISH)
2197            return DIMINISHING_BANISH;
2198    }
2199
2200    return DIMINISHING_NONE;
2201}
2202
2203bool IsDiminishingReturnsGroupDurationLimited(DiminishingGroup group)
2204{
2205    switch(group)
2206    {
2207        case DIMINISHING_CONTROL_STUN:
2208        case DIMINISHING_TRIGGER_STUN:
2209        case DIMINISHING_KIDNEYSHOT:
2210        case DIMINISHING_SLEEP:
2211        case DIMINISHING_CONTROL_ROOT:
2212        case DIMINISHING_TRIGGER_ROOT:
2213        case DIMINISHING_FEAR:
2214        case DIMINISHING_WARLOCK_FEAR:
2215        case DIMINISHING_CHARM:
2216        case DIMINISHING_POLYMORPH:
2217        case DIMINISHING_FREEZE:
2218        case DIMINISHING_KNOCKOUT:
2219        case DIMINISHING_BLIND_CYCLONE:
2220        case DIMINISHING_BANISH:
2221        case DIMINISHING_LIMITONLY:
2222            return true;
2223    }
2224    return false;
2225}
2226
2227DiminishingReturnsType GetDiminishingReturnsGroupType(DiminishingGroup group)
2228{
2229    switch(group)
2230    {
2231        case DIMINISHING_BLIND_CYCLONE:
2232        case DIMINISHING_CONTROL_STUN:
2233        case DIMINISHING_TRIGGER_STUN:
2234        case DIMINISHING_KIDNEYSHOT:
2235            return DRTYPE_ALL;
2236        case DIMINISHING_SLEEP:
2237        case DIMINISHING_CONTROL_ROOT:
2238        case DIMINISHING_TRIGGER_ROOT:
2239        case DIMINISHING_FEAR:
2240        case DIMINISHING_CHARM:
2241        case DIMINISHING_POLYMORPH:
2242        case DIMINISHING_SILENCE:
2243        case DIMINISHING_DISARM:
2244        case DIMINISHING_DEATHCOIL:
2245        case DIMINISHING_FREEZE:
2246        case DIMINISHING_BANISH:
2247        case DIMINISHING_WARLOCK_FEAR:
2248        case DIMINISHING_KNOCKOUT:
2249            return DRTYPE_PLAYER;
2250    }
2251
2252    return DRTYPE_NONE;
2253}
Note: See TracBrowser for help on using the browser.