root/trunk/src/game/Pet.cpp @ 174

Revision 174, 59.5 kB (checked in by yumileroy, 17 years ago)

[svn] Implemented player on player and player on creature possession:
* Implemented packet and vision forwarding through possessed units
* Added new OnPossess? script call alerting scripts on when possession is applied/removed
* Moved fall damage and fall under map calculations into the Player class
* Added new PossessedAI that is applied only while possession on creature is active
* Implemented summon possessed spell effect
* Fixed Eyes of the Beast

Original author: gvcoman
Date: 2008-11-05 20:51:05-06:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
4 * Copyright (C) 2008 Trinity <http://www.trinitycore.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 "Common.h"
22#include "Database/DatabaseEnv.h"
23#include "Log.h"
24#include "WorldSession.h"
25#include "WorldPacket.h"
26#include "ObjectMgr.h"
27#include "SpellMgr.h"
28#include "Pet.h"
29#include "MapManager.h"
30#include "Formulas.h"
31#include "SpellAuras.h"
32#include "CreatureAI.h"
33#include "Unit.h"
34#include "Util.h"
35
36char const* petTypeSuffix[MAX_PET_TYPE] =
37{
38    "'s Minion",                                            // SUMMON_PET
39    "'s Pet",                                               // HUNTER_PET
40    "'s Guardian",                                          // GUARDIAN_PET
41    "'s Companion"                                          // MINI_PET
42};
43
44//numbers represent minutes * 100 while happy (you get 100 loyalty points per min while happy)
45uint32 const LevelUpLoyalty[6] =
46{
47    5500,
48    11500,
49    17000,
50    23500,
51    31000,
52    39500,
53};
54
55uint32 const LevelStartLoyalty[6] =
56{
57    2000,
58    4500,
59    7000,
60    10000,
61    13500,
62    17500,
63};
64
65Pet::Pet(PetType type) : Creature()
66{
67    m_isPet = true;
68    m_name = "Pet";
69    m_petType = type;
70
71    m_removed = false;
72    m_regenTimer = 4000;
73    m_happinessTimer = 7500;
74    m_loyaltyTimer = 12000;
75    m_duration = 0;
76    m_bonusdamage = 0;
77
78    m_loyaltyPoints = 0;
79    m_TrainingPoints = 0;
80    m_resetTalentsCost = 0;
81    m_resetTalentsTime = 0;
82
83    m_auraUpdateMask = 0;
84
85    // pets always have a charminfo, even if they are not actually charmed
86    CharmInfo* charmInfo = InitCharmInfo(this);
87
88    if(type == MINI_PET)                                    // always passive
89        charmInfo->SetReactState(REACT_PASSIVE);
90    else if(type == GUARDIAN_PET)                           // always aggressive
91        charmInfo->SetReactState(REACT_AGGRESSIVE);
92
93    m_spells.clear();
94    m_Auras.clear();
95    m_CreatureSpellCooldowns.clear();
96    m_CreatureCategoryCooldowns.clear();
97    m_autospells.clear();
98    m_declinedname = NULL;
99    m_isActive = true;
100}
101
102Pet::~Pet()
103{
104    if(m_uint32Values)                                      // only for fully created Object
105    {
106        for (PetSpellMap::iterator i = m_spells.begin(); i != m_spells.end(); ++i)
107            delete i->second;
108        ObjectAccessor::Instance().RemoveObject(this);
109    }
110
111    delete m_declinedname;
112}
113
114void Pet::AddToWorld()
115{
116    ///- Register the pet for guid lookup
117    if(!IsInWorld()) ObjectAccessor::Instance().AddObject(this);
118    Unit::AddToWorld();
119}
120
121void Pet::RemoveFromWorld()
122{
123    ///- Remove the pet from the accessor
124    if(IsInWorld()) ObjectAccessor::Instance().RemoveObject(this);
125    ///- Don't call the function for Creature, normal mobs + totems go in a different storage
126    Unit::RemoveFromWorld();
127}
128
129bool Pet::LoadPetFromDB( Unit* owner, uint32 petentry, uint32 petnumber, bool current )
130{
131    uint32 ownerid = owner->GetGUIDLow();
132
133    QueryResult *result;
134
135    if(petnumber)
136        // known petnumber entry                  0   1      2      3        4      5    6           7              8        9           10    11    12       13         14       15            16      17              18        19                 20                 21              22
137        result = CharacterDatabase.PQuery("SELECT id, entry, owner, modelid, level, exp, Reactstate, loyaltypoints, loyalty, trainpoint, slot, name, renamed, curhealth, curmana, curhappiness, abdata, TeachSpelldata, savetime, resettalents_cost, resettalents_time, CreatedBySpell, PetType FROM character_pet WHERE owner = '%u' AND id = '%u'",ownerid, petnumber);
138    else if(current)
139        // current pet (slot 0)                   0   1      2      3        4      5    6           7              8        9           10    11    12       13         14       15            16      17              18        19                 20                 21              22
140        result = CharacterDatabase.PQuery("SELECT id, entry, owner, modelid, level, exp, Reactstate, loyaltypoints, loyalty, trainpoint, slot, name, renamed, curhealth, curmana, curhappiness, abdata, TeachSpelldata, savetime, resettalents_cost, resettalents_time, CreatedBySpell, PetType FROM character_pet WHERE owner = '%u' AND slot = '0'",ownerid );
141    else if(petentry)
142        // known petentry entry (unique for summoned pet, but non unique for hunter pet (only from current or not stabled pets)
143        //                                        0   1      2      3        4      5    6           7              8        9           10    11    12       13         14       15            16      17              18        19                 20                 21              22
144        result = CharacterDatabase.PQuery("SELECT id, entry, owner, modelid, level, exp, Reactstate, loyaltypoints, loyalty, trainpoint, slot, name, renamed, curhealth, curmana, curhappiness, abdata, TeachSpelldata, savetime, resettalents_cost, resettalents_time, CreatedBySpell, PetType FROM character_pet WHERE owner = '%u' AND entry = '%u' AND (slot = '0' OR slot = '3') ",ownerid, petentry );
145    else
146        // any current or other non-stabled pet (for hunter "call pet")
147        //                                        0   1      2      3        4      5    6           7              8        9           10    11    12       13         14       15            16      17              18        19                 20                 21              22
148        result = CharacterDatabase.PQuery("SELECT id, entry, owner, modelid, level, exp, Reactstate, loyaltypoints, loyalty, trainpoint, slot, name, renamed, curhealth, curmana, curhappiness, abdata, TeachSpelldata, savetime, resettalents_cost, resettalents_time, CreatedBySpell, PetType FROM character_pet WHERE owner = '%u' AND (slot = '0' OR slot = '3') ",ownerid);
149
150    if(!result)
151        return false;
152
153    Field *fields = result->Fetch();
154
155    // update for case of current pet "slot = 0"
156    petentry = fields[1].GetUInt32();
157    if(!petentry)
158    {
159        delete result;
160        return false;
161    }
162
163    uint32 summon_spell_id = fields[21].GetUInt32();
164    SpellEntry const* spellInfo = sSpellStore.LookupEntry(summon_spell_id);
165
166    bool is_temporary_summoned = spellInfo && GetSpellDuration(spellInfo) > 0;
167
168    // check temporary summoned pets like mage water elemental
169    if(current && is_temporary_summoned)
170    {
171        delete result;
172        return false;
173    }
174
175    Map *map = owner->GetMap();
176    uint32 guid=objmgr.GenerateLowGuid(HIGHGUID_PET);
177    uint32 pet_number = fields[0].GetUInt32();
178    if(!Create(guid, map, petentry, pet_number))
179    {
180        delete result;
181        return false;
182    }
183
184    float px, py, pz;
185    owner->GetClosePoint(px, py, pz,GetObjectSize(),PET_FOLLOW_DIST,PET_FOLLOW_ANGLE);
186
187    Relocate(px, py, pz, owner->GetOrientation());
188
189    if(!IsPositionValid())
190    {
191        sLog.outError("ERROR: Pet (guidlow %d, entry %d) not loaded. Suggested coordinates isn't valid (X: %f Y: %f)",
192            GetGUIDLow(), GetEntry(), GetPositionX(), GetPositionY());
193        delete result;
194        return false;
195    }
196
197    setPetType(PetType(fields[22].GetUInt8()));
198    SetUInt32Value(UNIT_FIELD_FACTIONTEMPLATE,owner->getFaction());
199    SetUInt32Value(UNIT_CREATED_BY_SPELL, summon_spell_id);
200
201    CreatureInfo const *cinfo = GetCreatureInfo();
202    if(cinfo->type == CREATURE_TYPE_CRITTER)
203    {
204        AIM_Initialize();
205        map->Add((Creature*)this);
206        delete result;
207        return true;
208    }
209    if(getPetType()==HUNTER_PET || (getPetType()==SUMMON_PET && cinfo->type == CREATURE_TYPE_DEMON && owner->getClass() == CLASS_WARLOCK))
210        m_charmInfo->SetPetNumber(pet_number, true);
211    else
212        m_charmInfo->SetPetNumber(pet_number, false);
213    SetUInt64Value(UNIT_FIELD_SUMMONEDBY, owner->GetGUID());
214    SetDisplayId(fields[3].GetUInt32());
215    SetNativeDisplayId(fields[3].GetUInt32());
216    uint32 petlevel=fields[4].GetUInt32();
217    SetUInt32Value(UNIT_NPC_FLAGS , 0);
218    SetName(fields[11].GetString());
219
220    switch(getPetType())
221    {
222
223        case SUMMON_PET:
224            petlevel=owner->getLevel();
225
226            SetUInt32Value(UNIT_FIELD_BYTES_0,2048);
227            SetUInt32Value(UNIT_FIELD_FLAGS, UNIT_FLAG_PVP_ATTACKABLE);
228                                                            // this enables popup window (pet dismiss, cancel)
229            break;
230        case HUNTER_PET:
231            SetUInt32Value(UNIT_FIELD_BYTES_0, 0x02020100);
232            SetByteValue(UNIT_FIELD_BYTES_1, 1, fields[8].GetUInt32());
233            SetByteValue(UNIT_FIELD_BYTES_2, 0, SHEATH_STATE_MELEE );
234            SetByteValue(UNIT_FIELD_BYTES_2, 1, UNIT_BYTE2_FLAG_UNK3 | UNIT_BYTE2_FLAG_AURAS | UNIT_BYTE2_FLAG_UNK5 );
235
236            if(fields[12].GetBool())
237                SetByteValue(UNIT_FIELD_BYTES_2, 2, UNIT_RENAME_NOT_ALLOWED);
238            else
239                SetByteValue(UNIT_FIELD_BYTES_2, 2, UNIT_RENAME_ALLOWED);
240
241            SetUInt32Value(UNIT_FIELD_FLAGS, UNIT_FLAG_PVP_ATTACKABLE);
242                                                            // this enables popup window (pet abandon, cancel)
243            SetTP(fields[9].GetInt32());
244            SetMaxPower(POWER_HAPPINESS,GetCreatePowers(POWER_HAPPINESS));
245            SetPower(   POWER_HAPPINESS,fields[15].GetUInt32());
246            setPowerType(POWER_FOCUS);
247            break;
248        default:
249            sLog.outError("Pet have incorrect type (%u) for pet loading.",getPetType());
250    }
251    InitStatsForLevel( petlevel);
252    SetUInt32Value(UNIT_FIELD_PET_NAME_TIMESTAMP, time(NULL));
253    SetUInt32Value(UNIT_FIELD_PETEXPERIENCE, fields[5].GetUInt32());
254    SetUInt64Value(UNIT_FIELD_CREATEDBY, owner->GetGUID());
255
256    m_charmInfo->SetReactState( ReactStates( fields[6].GetUInt8() ));
257    m_loyaltyPoints = fields[7].GetInt32();
258
259    uint32 savedhealth = fields[13].GetUInt32();
260    uint32 savedmana = fields[14].GetUInt32();
261
262    // set current pet as current
263    if(fields[10].GetUInt32() != 0)
264    {
265        CharacterDatabase.BeginTransaction();
266        CharacterDatabase.PExecute("UPDATE character_pet SET slot = '3' WHERE owner = '%u' AND slot = '0' AND id <> '%u'",ownerid, m_charmInfo->GetPetNumber());
267        CharacterDatabase.PExecute("UPDATE character_pet SET slot = '0' WHERE owner = '%u' AND id = '%u'",ownerid, m_charmInfo->GetPetNumber());
268        CharacterDatabase.CommitTransaction();
269    }
270
271    if(!is_temporary_summoned)
272    {
273        // permanent controlled pets store state in DB
274        Tokens tokens = StrSplit(fields[16].GetString(), " ");
275
276        if(tokens.size() != 20)
277        {
278            delete result;
279            return false;
280        }
281
282        int index;
283        Tokens::iterator iter;
284        for(iter = tokens.begin(), index = 0; index < 10; ++iter, ++index )
285        {
286            m_charmInfo->GetActionBarEntry(index)->Type = atol((*iter).c_str());
287            ++iter;
288            m_charmInfo->GetActionBarEntry(index)->SpellOrAction = atol((*iter).c_str());
289        }
290
291        //init teach spells
292        tokens = StrSplit(fields[17].GetString(), " ");
293        for (iter = tokens.begin(), index = 0; index < 4; ++iter, ++index)
294        {
295            uint32 tmp = atol((*iter).c_str());
296
297            ++iter;
298
299            if(tmp)
300                AddTeachSpell(tmp, atol((*iter).c_str()));
301            else
302                break;
303        }
304    }
305
306    // since last save (in seconds)
307    uint32 timediff = (time(NULL) - fields[18].GetUInt32());
308
309    delete result;
310
311    //load spells/cooldowns/auras
312    SetCanModifyStats(true);
313    _LoadAuras(timediff);
314
315    //init AB
316    if(is_temporary_summoned)
317    {
318        // Temporary summoned pets always have initial spell list at load
319        InitPetCreateSpells();
320    }
321    else
322    {
323        LearnPetPassives();
324        CastPetAuras(current);
325    }
326
327    if(getPetType() == SUMMON_PET && !current)              //all (?) summon pets come with full health when called, but not when they are current
328    {
329        SetHealth(GetMaxHealth());
330        SetPower(POWER_MANA, GetMaxPower(POWER_MANA));
331    }
332    else
333    {
334        SetHealth(savedhealth > GetMaxHealth() ? GetMaxHealth() : savedhealth);
335        SetPower(POWER_MANA, savedmana > GetMaxPower(POWER_MANA) ? GetMaxPower(POWER_MANA) : savedmana);
336    }
337
338    AIM_Initialize();
339    map->Add((Creature*)this);
340
341    // Spells should be loaded after pet is added to map, because in CanCast is check on it
342    _LoadSpells();
343    _LoadSpellCooldowns();
344
345    owner->SetPet(this);                                    // in DB stored only full controlled creature
346    sLog.outDebug("New Pet has guid %u", GetGUIDLow());
347
348    if(owner->GetTypeId() == TYPEID_PLAYER)
349    {
350        ((Player*)owner)->PetSpellInitialize();
351        if(((Player*)owner)->GetGroup())
352            ((Player*)owner)->SetGroupUpdateFlag(GROUP_UPDATE_PET);
353    }
354
355    if(owner->GetTypeId() == TYPEID_PLAYER && getPetType() == HUNTER_PET)
356    {
357        result = CharacterDatabase.PQuery("SELECT genitive, dative, accusative, instrumental, prepositional FROM character_pet_declinedname WHERE owner = '%u' AND id = '%u'",owner->GetGUIDLow(),GetCharmInfo()->GetPetNumber());
358
359        if(result)
360        {
361            if(m_declinedname)
362                delete m_declinedname;
363
364            m_declinedname = new DeclinedName;
365            Field *fields = result->Fetch();
366            for(int i = 0; i < MAX_DECLINED_NAME_CASES; ++i)
367            {
368                m_declinedname->name[i] = fields[i].GetCppString();
369            }
370        }
371    }
372
373    return true;
374}
375
376void Pet::SavePetToDB(PetSaveMode mode)
377{
378    if(!GetEntry())
379        return;
380
381    // save only fully controlled creature
382    if(!isControlled())
383        return;
384
385    uint32 curhealth = GetHealth();
386    uint32 curmana = GetPower(POWER_MANA);
387
388    switch(mode)
389    {
390        case PET_SAVE_IN_STABLE_SLOT_1:
391        case PET_SAVE_IN_STABLE_SLOT_2:
392        case PET_SAVE_NOT_IN_SLOT:
393        {
394            RemoveAllAuras();
395
396            //only alive hunter pets get auras saved, the others don't
397            if(!(getPetType() == HUNTER_PET && isAlive()))
398                m_Auras.clear();
399        }
400        default:
401            break;
402    }
403
404    _SaveSpells();
405    _SaveSpellCooldowns();
406    _SaveAuras();
407
408    switch(mode)
409    {
410        case PET_SAVE_AS_CURRENT:
411        case PET_SAVE_IN_STABLE_SLOT_1:
412        case PET_SAVE_IN_STABLE_SLOT_2:
413        case PET_SAVE_NOT_IN_SLOT:
414        {
415            uint32 loyalty =1;
416            if(getPetType()!=HUNTER_PET)
417                loyalty = GetLoyaltyLevel();
418
419            uint32 owner = GUID_LOPART(GetOwnerGUID());
420            std::string name = m_name;
421            CharacterDatabase.escape_string(name);
422            CharacterDatabase.BeginTransaction();
423            // remove current data
424            CharacterDatabase.PExecute("DELETE FROM character_pet WHERE owner = '%u' AND id = '%u'", owner,m_charmInfo->GetPetNumber() );
425
426            // prevent duplicate using slot (except PET_SAVE_NOT_IN_SLOT)
427            if(mode!=PET_SAVE_NOT_IN_SLOT)
428                CharacterDatabase.PExecute("UPDATE character_pet SET slot = 3 WHERE owner = '%u' AND slot = '%u'", owner, uint32(mode) );
429
430            // prevent existence another hunter pet in PET_SAVE_AS_CURRENT and PET_SAVE_NOT_IN_SLOT
431            if(getPetType()==HUNTER_PET && (mode==PET_SAVE_AS_CURRENT||mode==PET_SAVE_NOT_IN_SLOT))
432                CharacterDatabase.PExecute("DELETE FROM character_pet WHERE owner = '%u' AND (slot = '0' OR slot = '3')", owner );
433            // save pet
434            std::ostringstream ss;
435            ss  << "INSERT INTO character_pet ( id, entry,  owner, modelid, level, exp, Reactstate, loyaltypoints, loyalty, trainpoint, slot, name, renamed, curhealth, curmana, curhappiness, abdata,TeachSpelldata,savetime,resettalents_cost,resettalents_time,CreatedBySpell,PetType) "
436                << "VALUES ("
437                << m_charmInfo->GetPetNumber() << ", "
438                << GetEntry() << ", "
439                << owner << ", "
440                << GetNativeDisplayId() << ", "
441                << getLevel() << ", "
442                << GetUInt32Value(UNIT_FIELD_PETEXPERIENCE) << ", "
443                << uint32(m_charmInfo->GetReactState()) << ", "
444                << m_loyaltyPoints << ", "
445                << GetLoyaltyLevel() << ", "
446                << m_TrainingPoints << ", "
447                << uint32(mode) << ", '"
448                << name.c_str() << "', "
449                << uint32((GetByteValue(UNIT_FIELD_BYTES_2, 2) == UNIT_RENAME_ALLOWED)?0:1) << ", "
450                << (curhealth<1?1:curhealth) << ", "
451                << curmana << ", "
452                << GetPower(POWER_HAPPINESS) << ", '";
453
454            for(uint32 i = 0; i < 10; i++)
455                ss << uint32(m_charmInfo->GetActionBarEntry(i)->Type) << " " << uint32(m_charmInfo->GetActionBarEntry(i)->SpellOrAction) << " ";
456            ss << "', '";
457
458            //save spells the pet can teach to it's Master
459            {
460                int i = 0;
461                for(TeachSpellMap::iterator itr = m_teachspells.begin(); i < 4 && itr != m_teachspells.end(); ++i, ++itr)
462                    ss << itr->first << " " << itr->second << " ";
463                for(; i < 4; ++i)
464                    ss << uint32(0) << " " << uint32(0) << " ";
465            }
466
467            ss  << "', "
468                << time(NULL) << ", "
469                << uint32(m_resetTalentsCost) << ", "
470                << uint64(m_resetTalentsTime) << ", "
471                << GetUInt32Value(UNIT_CREATED_BY_SPELL) << ", "
472                << uint32(getPetType()) << ")";
473
474            CharacterDatabase.Execute( ss.str().c_str() );
475
476            CharacterDatabase.CommitTransaction();
477            break;
478        }
479        case PET_SAVE_AS_DELETED:
480        {
481            RemoveAllAuras();
482            uint32 owner = GUID_LOPART(GetOwnerGUID());
483            DeleteFromDB(m_charmInfo->GetPetNumber());
484            break;
485        }
486        default:
487            sLog.outError("Unknown pet save/remove mode: %d",mode);
488    }
489}
490
491void Pet::DeleteFromDB(uint32 guidlow)
492{
493    CharacterDatabase.PExecute("DELETE FROM character_pet WHERE id = '%u'", guidlow);
494    CharacterDatabase.PExecute("DELETE FROM character_pet_declinedname WHERE id = '%u'", guidlow);
495    CharacterDatabase.PExecute("DELETE FROM pet_aura WHERE guid = '%u'", guidlow);
496    CharacterDatabase.PExecute("DELETE FROM pet_spell WHERE guid = '%u'", guidlow);
497    CharacterDatabase.PExecute("DELETE FROM pet_spell_cooldown WHERE guid = '%u'", guidlow);
498}
499
500void Pet::setDeathState(DeathState s)                       // overwrite virtual Creature::setDeathState and Unit::setDeathState
501{
502    Creature::setDeathState(s);
503    if(getDeathState()==CORPSE)
504    {
505        //remove summoned pet (no corpse)
506        if(getPetType()==SUMMON_PET)
507            Remove(PET_SAVE_NOT_IN_SLOT);
508        // other will despawn at corpse desppawning (Pet::Update code)
509        else
510        {
511            // pet corpse non lootable and non skinnable
512            SetUInt32Value( UNIT_DYNAMIC_FLAGS, 0x00 );
513            RemoveFlag (UNIT_FIELD_FLAGS, UNIT_FLAG_SKINNABLE);
514
515             //lose happiness when died and not in BG/Arena
516            MapEntry const* mapEntry = sMapStore.LookupEntry(GetMapId());
517            if(!mapEntry || (mapEntry->map_type != MAP_ARENA && mapEntry->map_type != MAP_BATTLEGROUND))
518                ModifyPower(POWER_HAPPINESS, -HAPPINESS_LEVEL_SIZE);
519
520            SetFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_DISABLE_ROTATE);
521        }
522    }
523    else if(getDeathState()==ALIVE)
524    {
525        RemoveFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_DISABLE_ROTATE);
526        CastPetAuras(true);
527    }
528}
529
530void Pet::Update(uint32 diff)
531{
532    if(m_removed)                                           // pet already removed, just wait in remove queue, no updates
533        return;
534
535    switch( m_deathState )
536    {
537        case CORPSE:
538        {
539            if( m_deathTimer <= diff )
540            {
541                assert(getPetType()!=SUMMON_PET && "Must be already removed.");
542                Remove(PET_SAVE_NOT_IN_SLOT);               //hunters' pets never get removed because of death, NEVER!
543                return;
544            }
545            break;
546        }
547        case ALIVE:
548        {
549            // unsummon pet that lost owner
550            Unit* owner = GetOwner();
551            if(!owner || (!IsWithinDistInMap(owner, OWNER_MAX_DISTANCE) && !isPossessed()) || isControlled() && !owner->GetPetGUID())
552            {
553                Remove(PET_SAVE_NOT_IN_SLOT, true);
554                return;
555            }
556
557            if(isControlled())
558            {
559                if( owner->GetPetGUID() != GetGUID() )
560                {
561                    Remove(getPetType()==HUNTER_PET?PET_SAVE_AS_DELETED:PET_SAVE_NOT_IN_SLOT);
562                    return;
563                }
564            }
565
566            if(m_duration > 0)
567            {
568                if(m_duration > diff)
569                    m_duration -= diff;
570                else
571                {
572                    Remove(getPetType() != SUMMON_PET ? PET_SAVE_AS_DELETED:PET_SAVE_NOT_IN_SLOT);
573                    return;
574                }
575            }
576
577            if(getPetType() != HUNTER_PET)
578                break;
579
580            //regenerate Focus
581            if(m_regenTimer <= diff)
582            {
583                RegenerateFocus();
584                m_regenTimer = 4000;
585            }
586            else
587                m_regenTimer -= diff;
588
589            if(m_happinessTimer <= diff)
590            {
591                LooseHappiness();
592                m_happinessTimer = 7500;
593            }
594            else
595                m_happinessTimer -= diff;
596
597            if(m_loyaltyTimer <= diff)
598            {
599                TickLoyaltyChange();
600                m_loyaltyTimer = 12000;
601            }
602            else
603                m_loyaltyTimer -= diff;
604
605            break;
606        }
607        default:
608            break;
609    }
610    Creature::Update(diff);
611}
612
613void Pet::RegenerateFocus()
614{
615    uint32 curValue = GetPower(POWER_FOCUS);
616    uint32 maxValue = GetMaxPower(POWER_FOCUS);
617
618    if (curValue >= maxValue)
619        return;
620
621    float addvalue = 24 * sWorld.getRate(RATE_POWER_FOCUS);
622
623    AuraList const& ModPowerRegenPCTAuras = GetAurasByType(SPELL_AURA_MOD_POWER_REGEN_PERCENT);
624    for(AuraList::const_iterator i = ModPowerRegenPCTAuras.begin(); i != ModPowerRegenPCTAuras.end(); ++i)
625        if ((*i)->GetModifier()->m_miscvalue == POWER_FOCUS)
626            addvalue *= ((*i)->GetModifier()->m_amount + 100) / 100.0f;
627
628    ModifyPower(POWER_FOCUS, (int32)addvalue);
629}
630
631void Pet::LooseHappiness()
632{
633    uint32 curValue = GetPower(POWER_HAPPINESS);
634    if (curValue <= 0)
635        return;
636    int32 addvalue = (140 >> GetLoyaltyLevel()) * 125;      //value is 70/35/17/8/4 (per min) * 1000 / 8 (timer 7.5 secs)
637    if(isInCombat())                                        //we know in combat happiness fades faster, multiplier guess
638        addvalue = int32(addvalue * 1.5);
639    ModifyPower(POWER_HAPPINESS, -addvalue);
640}
641
642void Pet::ModifyLoyalty(int32 addvalue)
643{
644    uint32 loyaltylevel = GetLoyaltyLevel();
645
646    if(addvalue > 0)                                        //only gain influenced, not loss
647        addvalue = int32((float)addvalue * sWorld.getRate(RATE_LOYALTY));
648
649    if(loyaltylevel >= BEST_FRIEND && (addvalue + m_loyaltyPoints) > int32(GetMaxLoyaltyPoints(loyaltylevel)))
650        return;
651
652    m_loyaltyPoints += addvalue;
653
654    if(m_loyaltyPoints < 0)
655    {
656        if(loyaltylevel > REBELLIOUS)
657        {
658            //level down
659            --loyaltylevel;
660            SetLoyaltyLevel(LoyaltyLevel(loyaltylevel));
661            m_loyaltyPoints = GetStartLoyaltyPoints(loyaltylevel);
662            SetTP(m_TrainingPoints - int32(getLevel()));
663        }
664        else
665        {
666            m_loyaltyPoints = 0;
667            Unit* owner = GetOwner();
668            if(owner && owner->GetTypeId() == TYPEID_PLAYER)
669            {
670                WorldPacket data(SMSG_PET_BROKEN, 0);
671                ((Player*)owner)->GetSession()->SendPacket(&data);
672
673                //run away
674                ((Player*)owner)->RemovePet(this,PET_SAVE_AS_DELETED);
675            }
676        }
677    }
678    //level up
679    else if(m_loyaltyPoints > int32(GetMaxLoyaltyPoints(loyaltylevel)))
680    {
681        ++loyaltylevel;
682        SetLoyaltyLevel(LoyaltyLevel(loyaltylevel));
683        m_loyaltyPoints = GetStartLoyaltyPoints(loyaltylevel);
684        SetTP(m_TrainingPoints + getLevel());
685    }
686}
687
688void Pet::TickLoyaltyChange()
689{
690    int32 addvalue;
691
692    switch(GetHappinessState())
693    {
694        case HAPPY:   addvalue =  20; break;
695        case CONTENT: addvalue =  10; break;
696        case UNHAPPY: addvalue = -20; break;
697        default:
698            return;
699    }
700    ModifyLoyalty(addvalue);
701}
702
703void Pet::KillLoyaltyBonus(uint32 level)
704{
705    if(level > 100)
706        return;
707
708                                                            //at lower levels gain is faster | the lower loyalty the more loyalty is gained
709    uint32 bonus = uint32(((100 - level) / 10) + (6 - GetLoyaltyLevel()));
710    ModifyLoyalty(bonus);
711}
712
713HappinessState Pet::GetHappinessState()
714{
715    if(GetPower(POWER_HAPPINESS) < HAPPINESS_LEVEL_SIZE)
716        return UNHAPPY;
717    else if(GetPower(POWER_HAPPINESS) >= HAPPINESS_LEVEL_SIZE * 2)
718        return HAPPY;
719    else
720        return CONTENT;
721}
722
723void Pet::SetLoyaltyLevel(LoyaltyLevel level)
724{
725    SetByteValue(UNIT_FIELD_BYTES_1, 1, level);
726}
727
728bool Pet::CanTakeMoreActiveSpells(uint32 spellid)
729{
730    uint8  activecount = 1;
731    uint32 chainstartstore[ACTIVE_SPELLS_MAX];
732
733    if(IsPassiveSpell(spellid))
734        return true;
735
736    chainstartstore[0] = spellmgr.GetFirstSpellInChain(spellid);
737
738    for (PetSpellMap::iterator itr = m_spells.begin(); itr != m_spells.end(); ++itr)
739    {
740        if(IsPassiveSpell(itr->first))
741            continue;
742
743        uint32 chainstart = spellmgr.GetFirstSpellInChain(itr->first);
744
745        uint8 x;
746
747        for(x = 0; x < activecount; x++)
748        {
749            if(chainstart == chainstartstore[x])
750                break;
751        }
752
753        if(x == activecount)                                //spellchain not yet saved -> add active count
754        {
755            ++activecount;
756            if(activecount > ACTIVE_SPELLS_MAX)
757                return false;
758            chainstartstore[x] = chainstart;
759        }
760    }
761    return true;
762}
763
764bool Pet::HasTPForSpell(uint32 spellid)
765{
766    int32 neededtrainp = GetTPForSpell(spellid);
767    if((m_TrainingPoints - neededtrainp < 0 || neededtrainp < 0) && neededtrainp != 0)
768        return false;
769    return true;
770}
771
772int32 Pet::GetTPForSpell(uint32 spellid)
773{
774    uint32 basetrainp = 0;
775
776    SkillLineAbilityMap::const_iterator lower = spellmgr.GetBeginSkillLineAbilityMap(spellid);
777    SkillLineAbilityMap::const_iterator upper = spellmgr.GetEndSkillLineAbilityMap(spellid);
778    for(SkillLineAbilityMap::const_iterator _spell_idx = lower; _spell_idx != upper; ++_spell_idx)
779    {
780        if(!_spell_idx->second->reqtrainpoints)
781            return 0;
782
783        basetrainp = _spell_idx->second->reqtrainpoints;
784        break;
785    }
786
787    uint32 spenttrainp = 0;
788    uint32 chainstart = spellmgr.GetFirstSpellInChain(spellid);
789
790    for (PetSpellMap::iterator itr = m_spells.begin(); itr != m_spells.end(); ++itr)
791    {
792        if(itr->second->state == PETSPELL_REMOVED)
793            continue;
794
795        if(spellmgr.GetFirstSpellInChain(itr->first) == chainstart)
796        {
797            SkillLineAbilityMap::const_iterator _lower = spellmgr.GetBeginSkillLineAbilityMap(itr->first);
798            SkillLineAbilityMap::const_iterator _upper = spellmgr.GetEndSkillLineAbilityMap(itr->first);
799
800            for(SkillLineAbilityMap::const_iterator _spell_idx2 = _lower; _spell_idx2 != _upper; ++_spell_idx2)
801            {
802                if(_spell_idx2->second->reqtrainpoints > spenttrainp)
803                {
804                    spenttrainp = _spell_idx2->second->reqtrainpoints;
805                    break;
806                }
807            }
808        }
809    }
810
811    return int32(basetrainp) - int32(spenttrainp);
812}
813
814uint32 Pet::GetMaxLoyaltyPoints(uint32 level)
815{
816    return LevelUpLoyalty[level - 1];
817}
818
819uint32 Pet::GetStartLoyaltyPoints(uint32 level)
820{
821    return LevelStartLoyalty[level - 1];
822}
823
824void Pet::SetTP(int32 TP)
825{
826    m_TrainingPoints = TP;
827    SetUInt32Value(UNIT_TRAINING_POINTS, (uint32)GetDispTP());
828}
829
830int32 Pet::GetDispTP()
831{
832    if(getPetType()!= HUNTER_PET)
833        return(0);
834    if(m_TrainingPoints < 0)
835        return -m_TrainingPoints;
836    else
837        return -(m_TrainingPoints + 1);
838}
839
840void Pet::Remove(PetSaveMode mode, bool returnreagent)
841{
842    Unit* owner = GetOwner();
843
844    if(owner)
845    {
846        if(owner->GetTypeId()==TYPEID_PLAYER)
847        {
848            ((Player*)owner)->RemovePet(this,mode,returnreagent);
849            return;
850        }
851
852        // only if current pet in slot
853        if(owner->GetPetGUID()==GetGUID())
854            owner->SetPet(0);
855    }
856
857    CleanupsBeforeDelete();
858    AddObjectToRemoveList();
859    m_removed = true;
860}
861
862void Pet::GivePetXP(uint32 xp)
863{
864    if(getPetType() != HUNTER_PET)
865        return;
866
867    if ( xp < 1 )
868        return;
869
870    if(!isAlive())
871        return;
872
873    uint32 level = getLevel();
874
875    // XP to money conversion processed in Player::RewardQuest
876    if(level >= sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL))
877        return;
878
879    uint32 curXP = GetUInt32Value(UNIT_FIELD_PETEXPERIENCE);
880    uint32 nextLvlXP = GetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP);
881    uint32 newXP = curXP + xp;
882
883    if(newXP >= nextLvlXP && level+1 > GetOwner()->getLevel())
884    {
885        SetUInt32Value(UNIT_FIELD_PETEXPERIENCE, nextLvlXP-1);
886        return;
887    }
888
889    while( newXP >= nextLvlXP && level < sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL) )
890    {
891        newXP -= nextLvlXP;
892
893        SetLevel( level + 1 );
894        SetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP, uint32((Trinity::XP::xp_to_level(level+1))/4));
895
896        level = getLevel();
897        nextLvlXP = GetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP);
898        GivePetLevel(level);
899    }
900
901    SetUInt32Value(UNIT_FIELD_PETEXPERIENCE, newXP);
902
903    if(getPetType() == HUNTER_PET)
904        KillLoyaltyBonus(level);
905}
906
907void Pet::GivePetLevel(uint32 level)
908{
909    if(!level)
910        return;
911
912    InitStatsForLevel( level);
913
914    SetTP(m_TrainingPoints + (GetLoyaltyLevel() - 1));
915}
916
917bool Pet::CreateBaseAtCreature(Creature* creature)
918{
919    if(!creature)
920    {
921        sLog.outError("CRITICAL ERROR: NULL pointer parsed into CreateBaseAtCreature()");
922        return false;
923    }
924    uint32 guid=objmgr.GenerateLowGuid(HIGHGUID_PET);
925
926    sLog.outBasic("SetInstanceID()");
927    SetInstanceId(creature->GetInstanceId());
928
929    sLog.outBasic("Create pet");
930    uint32 pet_number = objmgr.GeneratePetNumber();
931    if(!Create(guid, creature->GetMap(), creature->GetEntry(), pet_number))
932        return false;
933
934    Relocate(creature->GetPositionX(), creature->GetPositionY(), creature->GetPositionZ(), creature->GetOrientation());
935
936    if(!IsPositionValid())
937    {
938        sLog.outError("ERROR: Pet (guidlow %d, entry %d) not created base at creature. Suggested coordinates isn't valid (X: %d Y: ^%d)", GetGUIDLow(), GetEntry(), GetPositionX(), GetPositionY());
939        return false;
940    }
941
942    CreatureInfo const *cinfo = GetCreatureInfo();
943    if(!cinfo)
944    {
945        sLog.outError("ERROR: CreateBaseAtCreature() failed, creatureInfo is missing!");
946        return false;
947    }
948
949    if(cinfo->type == CREATURE_TYPE_CRITTER)
950    {
951        setPetType(MINI_PET);
952        return true;
953    }
954    SetDisplayId(creature->GetDisplayId());
955    SetNativeDisplayId(creature->GetNativeDisplayId());
956    SetMaxPower(POWER_HAPPINESS,GetCreatePowers(POWER_HAPPINESS));
957    SetPower(   POWER_HAPPINESS,166500);
958    setPowerType(POWER_FOCUS);
959    SetUInt32Value(UNIT_FIELD_PET_NAME_TIMESTAMP,0);
960    SetUInt32Value(UNIT_FIELD_PETEXPERIENCE,0);
961    SetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP, uint32((Trinity::XP::xp_to_level(creature->getLevel()))/4));
962    SetUInt32Value(UNIT_FIELD_FLAGS, UNIT_FLAG_PVP_ATTACKABLE);
963    SetUInt32Value(UNIT_NPC_FLAGS , 0);
964
965    CreatureFamilyEntry const* cFamily = sCreatureFamilyStore.LookupEntry(creature->GetCreatureInfo()->family);
966    if( char* familyname = cFamily->Name[sWorld.GetDefaultDbcLocale()] )
967        SetName(familyname);
968    else
969        SetName(creature->GetName());
970
971    m_loyaltyPoints = 1000;
972    if(cinfo->type == CREATURE_TYPE_BEAST)
973    {
974        SetUInt32Value(UNIT_FIELD_BYTES_0, 0x02020100);
975        SetByteValue(UNIT_FIELD_BYTES_2, 0, SHEATH_STATE_MELEE );
976        SetByteValue(UNIT_FIELD_BYTES_2, 1, UNIT_BYTE2_FLAG_UNK3 | UNIT_BYTE2_FLAG_AURAS | UNIT_BYTE2_FLAG_UNK5 );
977        SetByteValue(UNIT_FIELD_BYTES_2, 2, UNIT_RENAME_ALLOWED);
978
979        SetUInt32Value(UNIT_MOD_CAST_SPEED, creature->GetUInt32Value(UNIT_MOD_CAST_SPEED) );
980        SetLoyaltyLevel(REBELLIOUS);
981    }
982    return true;
983}
984
985bool Pet::InitStatsForLevel(uint32 petlevel)
986{
987    CreatureInfo const *cinfo = GetCreatureInfo();
988    assert(cinfo);
989
990    Unit* owner = GetOwner();
991    if(!owner)
992    {
993        sLog.outError("ERROR: attempt to summon pet (Entry %u) without owner! Attempt terminated.", cinfo->Entry);
994        return false;
995    }
996
997    uint32 creature_ID = (getPetType() == HUNTER_PET) ? 1 : cinfo->Entry;
998
999    SetLevel( petlevel);
1000
1001    SetMeleeDamageSchool(SpellSchools(cinfo->dmgschool));
1002
1003    SetModifierValue(UNIT_MOD_ARMOR, BASE_VALUE, float(petlevel*50));
1004
1005    SetAttackTime(BASE_ATTACK, BASE_ATTACK_TIME);
1006    SetAttackTime(OFF_ATTACK, BASE_ATTACK_TIME);
1007    SetAttackTime(RANGED_ATTACK, BASE_ATTACK_TIME);
1008
1009    SetFloatValue(UNIT_MOD_CAST_SPEED, 1.0);
1010
1011    CreatureFamilyEntry const* cFamily = sCreatureFamilyStore.LookupEntry(cinfo->family);
1012    if(cFamily && cFamily->minScale > 0.0f)
1013    {
1014        float scale;
1015        if (getLevel() >= cFamily->maxScaleLevel)
1016            scale = cFamily->maxScale;
1017        else if (getLevel() <= cFamily->minScaleLevel)
1018            scale = cFamily->minScale;
1019        else
1020            scale = cFamily->minScale + (getLevel() - cFamily->minScaleLevel) / cFamily->maxScaleLevel * (cFamily->maxScale - cFamily->minScale);
1021
1022        SetFloatValue(OBJECT_FIELD_SCALE_X, scale);
1023    }
1024    m_bonusdamage = 0;
1025
1026    int32 createResistance[MAX_SPELL_SCHOOL] = {0,0,0,0,0,0,0};
1027
1028    if(cinfo && getPetType() != HUNTER_PET)
1029    {
1030        createResistance[SPELL_SCHOOL_HOLY]   = cinfo->resistance1;
1031        createResistance[SPELL_SCHOOL_FIRE]   = cinfo->resistance2;
1032        createResistance[SPELL_SCHOOL_NATURE] = cinfo->resistance3;
1033        createResistance[SPELL_SCHOOL_FROST]  = cinfo->resistance4;
1034        createResistance[SPELL_SCHOOL_SHADOW] = cinfo->resistance5;
1035        createResistance[SPELL_SCHOOL_ARCANE] = cinfo->resistance6;
1036    }
1037
1038    switch(getPetType())
1039    {
1040        case SUMMON_PET:
1041        {
1042            if(owner->GetTypeId() == TYPEID_PLAYER)
1043            {
1044                switch(owner->getClass())
1045                {
1046                    case CLASS_WARLOCK:
1047                    {
1048
1049                        //the damage bonus used for pets is either fire or shadow damage, whatever is higher
1050                        uint32 fire  = owner->GetUInt32Value(PLAYER_FIELD_MOD_DAMAGE_DONE_POS + SPELL_SCHOOL_FIRE);
1051                        uint32 shadow = owner->GetUInt32Value(PLAYER_FIELD_MOD_DAMAGE_DONE_POS + SPELL_SCHOOL_SHADOW);
1052                        uint32 val  = (fire > shadow) ? fire : shadow;
1053
1054                        SetBonusDamage(int32 (val * 0.15f));
1055                        //bonusAP += val * 0.57;
1056                        break;
1057                    }
1058                    case CLASS_MAGE:
1059                    {
1060                                                            //40% damage bonus of mage's frost damage
1061                        float val = owner->GetUInt32Value(PLAYER_FIELD_MOD_DAMAGE_DONE_POS + SPELL_SCHOOL_FROST) * 0.4;
1062                        if(val < 0)
1063                            val = 0;
1064                        SetBonusDamage( int32(val));
1065                        break;
1066                    }
1067                    default:
1068                        break;
1069                }
1070            }
1071
1072            SetBaseWeaponDamage(BASE_ATTACK, MINDAMAGE, float(petlevel - (petlevel / 4)) );
1073            SetBaseWeaponDamage(BASE_ATTACK, MAXDAMAGE, float(petlevel + (petlevel / 4)) );
1074
1075            //SetModifierValue(UNIT_MOD_ATTACK_POWER, BASE_VALUE, float(cinfo->attackpower));
1076
1077            PetLevelInfo const* pInfo = objmgr.GetPetLevelInfo(creature_ID, petlevel);
1078            if(pInfo)                                       // exist in DB
1079            {
1080                SetCreateHealth(pInfo->health);
1081                SetCreateMana(pInfo->mana);
1082
1083                if(pInfo->armor > 0)
1084                    SetModifierValue(UNIT_MOD_ARMOR, BASE_VALUE, float(pInfo->armor));
1085
1086                for(int stat = 0; stat < MAX_STATS; ++stat)
1087                {
1088                    SetCreateStat(Stats(stat),float(pInfo->stats[stat]));
1089                }
1090            }
1091            else                                            // not exist in DB, use some default fake data
1092            {
1093                sLog.outErrorDb("Summoned pet (Entry: %u) not have pet stats data in DB",cinfo->Entry);
1094
1095                // remove elite bonuses included in DB values
1096                SetCreateHealth(uint32(((float(cinfo->maxhealth) / cinfo->maxlevel) / (1 + 2 * cinfo->rank)) * petlevel) );
1097                SetCreateMana(  uint32(((float(cinfo->maxmana)   / cinfo->maxlevel) / (1 + 2 * cinfo->rank)) * petlevel) );
1098
1099                SetCreateStat(STAT_STRENGTH,22);
1100                SetCreateStat(STAT_AGILITY,22);
1101                SetCreateStat(STAT_STAMINA,25);
1102                SetCreateStat(STAT_INTELLECT,28);
1103                SetCreateStat(STAT_SPIRIT,27);
1104            }
1105            break;
1106        }
1107        case HUNTER_PET:
1108        {
1109            SetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP, uint32((Trinity::XP::xp_to_level(petlevel))/4));
1110
1111            //these formula may not be correct; however, it is designed to be close to what it should be
1112            //this makes dps 0.5 of pets level
1113            SetBaseWeaponDamage(BASE_ATTACK, MINDAMAGE, float(petlevel - (petlevel / 4)) );
1114            //damage range is then petlevel / 2
1115            SetBaseWeaponDamage(BASE_ATTACK, MAXDAMAGE, float(petlevel + (petlevel / 4)) );
1116            //damage is increased afterwards as strength and pet scaling modify attack power
1117
1118            //stored standard pet stats are entry 1 in pet_levelinfo
1119            PetLevelInfo const* pInfo = objmgr.GetPetLevelInfo(creature_ID, petlevel);
1120            if(pInfo)                                       // exist in DB
1121            {
1122                SetCreateHealth(pInfo->health);
1123                SetModifierValue(UNIT_MOD_ARMOR, BASE_VALUE, float(pInfo->armor));
1124                //SetModifierValue(UNIT_MOD_ATTACK_POWER, BASE_VALUE, float(cinfo->attackpower));
1125
1126                for( int i = STAT_STRENGTH; i < MAX_STATS; i++)
1127                {
1128                    SetCreateStat(Stats(i),  float(pInfo->stats[i]));
1129                }
1130            }
1131            else                                            // not exist in DB, use some default fake data
1132            {
1133                sLog.outErrorDb("Hunter pet levelstats missing in DB");
1134
1135                // remove elite bonuses included in DB values
1136                SetCreateHealth( uint32(((float(cinfo->maxhealth) / cinfo->maxlevel) / (1 + 2 * cinfo->rank)) * petlevel) );
1137
1138                SetCreateStat(STAT_STRENGTH,22);
1139                SetCreateStat(STAT_AGILITY,22);
1140                SetCreateStat(STAT_STAMINA,25);
1141                SetCreateStat(STAT_INTELLECT,28);
1142                SetCreateStat(STAT_SPIRIT,27);
1143            }
1144            break;
1145        }
1146        case GUARDIAN_PET:
1147            SetUInt32Value(UNIT_FIELD_PETEXPERIENCE,0);
1148            SetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP,1000);
1149
1150            SetCreateMana(   28 + 10*petlevel );
1151            SetCreateHealth( 28 + 30*petlevel );
1152
1153            // FIXME: this is wrong formula, possible each guardian pet have own damage formula
1154            //these formula may not be correct; however, it is designed to be close to what it should be
1155            //this makes dps 0.5 of pets level
1156            SetBaseWeaponDamage(BASE_ATTACK, MINDAMAGE, float(petlevel - (petlevel / 4)) );
1157            //damage range is then petlevel / 2
1158            SetBaseWeaponDamage(BASE_ATTACK, MAXDAMAGE, float(petlevel + (petlevel / 4)) );
1159            break;
1160        default:
1161            sLog.outError("Pet have incorrect type (%u) for levelup.",getPetType());            break;
1162    }
1163
1164    for (int i = SPELL_SCHOOL_HOLY; i < MAX_SPELL_SCHOOL; ++i)
1165        SetModifierValue(UnitMods(UNIT_MOD_RESISTANCE_START + i), BASE_VALUE, float(createResistance[i]) );
1166
1167    UpdateAllStats();
1168
1169    SetHealth(GetMaxHealth());
1170    SetPower(POWER_MANA, GetMaxPower(POWER_MANA));
1171
1172    return true;
1173}
1174
1175bool Pet::HaveInDiet(ItemPrototype const* item) const
1176{
1177    if (!item->FoodType)
1178        return false;
1179
1180    CreatureInfo const* cInfo = GetCreatureInfo();
1181    if(!cInfo)
1182        return false;
1183
1184    CreatureFamilyEntry const* cFamily = sCreatureFamilyStore.LookupEntry(cInfo->family);
1185    if(!cFamily)
1186        return false;
1187
1188    uint32 diet = cFamily->petFoodMask;
1189    uint32 FoodMask = 1 << (item->FoodType-1);
1190    return diet & FoodMask;
1191}
1192
1193uint32 Pet::GetCurrentFoodBenefitLevel(uint32 itemlevel)
1194{
1195    // -5 or greater food level
1196    if(getLevel() <= itemlevel +5)                          //possible to feed level 60 pet with level 55 level food for full effect
1197        return 35000;
1198    // -10..-6
1199    else if(getLevel() <= itemlevel + 10)                   //pure guess, but sounds good
1200        return 17000;
1201    // -14..-11
1202    else if(getLevel() <= itemlevel + 14)                   //level 55 food gets green on 70, makes sense to me
1203        return 8000;
1204    // -15 or less
1205    else
1206        return 0;                                           //food too low level
1207}
1208
1209void Pet::_LoadSpellCooldowns()
1210{
1211    m_CreatureSpellCooldowns.clear();
1212    m_CreatureCategoryCooldowns.clear();
1213
1214    QueryResult *result = CharacterDatabase.PQuery("SELECT spell,time FROM pet_spell_cooldown WHERE guid = '%u'",m_charmInfo->GetPetNumber());
1215
1216    if(result)
1217    {
1218        time_t curTime = time(NULL);
1219
1220        WorldPacket data(SMSG_SPELL_COOLDOWN, (8+1+result->GetRowCount()*8));
1221        data << GetGUID();
1222        data << uint8(0x0);                                 // flags (0x1, 0x2)
1223
1224        do
1225        {
1226            Field *fields = result->Fetch();
1227
1228            uint32 spell_id = fields[0].GetUInt32();
1229            time_t db_time  = (time_t)fields[1].GetUInt64();
1230
1231            if(!sSpellStore.LookupEntry(spell_id))
1232            {
1233                sLog.outError("Pet %u have unknown spell %u in `pet_spell_cooldown`, skipping.",m_charmInfo->GetPetNumber(),spell_id);
1234                continue;
1235            }
1236
1237            // skip outdated cooldown
1238            if(db_time <= curTime)
1239                continue;
1240
1241            data << uint32(spell_id);
1242            data << uint32(uint32(db_time-curTime)*1000);   // in m.secs
1243
1244            _AddCreatureSpellCooldown(spell_id,db_time);
1245
1246            sLog.outDebug("Pet (Number: %u) spell %u cooldown loaded (%u secs).",m_charmInfo->GetPetNumber(),spell_id,uint32(db_time-curTime));
1247        }
1248        while( result->NextRow() );
1249
1250        delete result;
1251
1252        if(!m_CreatureSpellCooldowns.empty() && GetOwner())
1253        {
1254            ((Player*)GetOwner())->GetSession()->SendPacket(&data);
1255        }
1256    }
1257}
1258
1259void Pet::_SaveSpellCooldowns()
1260{
1261    CharacterDatabase.PExecute("DELETE FROM pet_spell_cooldown WHERE guid = '%u'", m_charmInfo->GetPetNumber());
1262
1263    time_t curTime = time(NULL);
1264
1265    // remove oudated and save active
1266    for(CreatureSpellCooldowns::iterator itr = m_CreatureSpellCooldowns.begin();itr != m_CreatureSpellCooldowns.end();)
1267    {
1268        if(itr->second <= curTime)
1269            m_CreatureSpellCooldowns.erase(itr++);
1270        else
1271        {
1272            CharacterDatabase.PExecute("INSERT INTO pet_spell_cooldown (guid,spell,time) VALUES ('%u', '%u', '" I64FMTD "')", m_charmInfo->GetPetNumber(), itr->first, uint64(itr->second));
1273            ++itr;
1274        }
1275    }
1276}
1277
1278void Pet::_LoadSpells()
1279{
1280    QueryResult *result = CharacterDatabase.PQuery("SELECT spell,slot,active FROM pet_spell WHERE guid = '%u'",m_charmInfo->GetPetNumber());
1281
1282    if(result)
1283    {
1284        do
1285        {
1286            Field *fields = result->Fetch();
1287
1288            addSpell(fields[0].GetUInt16(), fields[2].GetUInt16(), PETSPELL_UNCHANGED, fields[1].GetUInt16());
1289        }
1290        while( result->NextRow() );
1291
1292        delete result;
1293    }
1294}
1295
1296void Pet::_SaveSpells()
1297{
1298    for (PetSpellMap::const_iterator itr = m_spells.begin(), next = m_spells.begin(); itr != m_spells.end(); itr = next)
1299    {
1300        ++next;
1301        if (itr->second->type == PETSPELL_FAMILY) continue; // prevent saving family passives to DB
1302        if (itr->second->state == PETSPELL_REMOVED || itr->second->state == PETSPELL_CHANGED)
1303            CharacterDatabase.PExecute("DELETE FROM pet_spell WHERE guid = '%u' and spell = '%u'", m_charmInfo->GetPetNumber(), itr->first);
1304        if (itr->second->state == PETSPELL_NEW || itr->second->state == PETSPELL_CHANGED)
1305            CharacterDatabase.PExecute("INSERT INTO pet_spell (guid,spell,slot,active) VALUES ('%u', '%u', '%u','%u')", m_charmInfo->GetPetNumber(), itr->first, itr->second->slotId,itr->second->active);
1306
1307        if (itr->second->state == PETSPELL_REMOVED)
1308            _removeSpell(itr->first);
1309        else
1310            itr->second->state = PETSPELL_UNCHANGED;
1311    }
1312}
1313
1314void Pet::_LoadAuras(uint32 timediff)
1315{
1316    m_Auras.clear();
1317    for (int i = 0; i < TOTAL_AURAS; i++)
1318        m_modAuras[i].clear();
1319
1320    // all aura related fields
1321    for(int i = UNIT_FIELD_AURA; i <= UNIT_FIELD_AURASTATE; ++i)
1322        SetUInt32Value(i, 0);
1323
1324    QueryResult *result = CharacterDatabase.PQuery("SELECT caster_guid,spell,effect_index,amount,maxduration,remaintime,remaincharges FROM pet_aura WHERE guid = '%u'",m_charmInfo->GetPetNumber());
1325
1326    if(result)
1327    {
1328        do
1329        {
1330            Field *fields = result->Fetch();
1331            uint64 caster_guid = fields[0].GetUInt64();
1332            uint32 spellid = fields[1].GetUInt32();
1333            uint32 effindex = fields[2].GetUInt32();
1334            int32 damage     = (int32)fields[3].GetUInt32();
1335            int32 maxduration = (int32)fields[4].GetUInt32();
1336            int32 remaintime = (int32)fields[5].GetUInt32();
1337            int32 remaincharges = (int32)fields[6].GetUInt32();
1338
1339            SpellEntry const* spellproto = sSpellStore.LookupEntry(spellid);
1340            if(!spellproto)
1341            {
1342                sLog.outError("Unknown aura (spellid %u, effindex %u), ignore.",spellid,effindex);
1343                continue;
1344            }
1345
1346            if(effindex >= 3)
1347            {
1348                sLog.outError("Invalid effect index (spellid %u, effindex %u), ignore.",spellid,effindex);
1349                continue;
1350            }
1351
1352            // negative effects should continue counting down after logout
1353            if (remaintime != -1 && !IsPositiveEffect(spellid, effindex))
1354            {
1355                if(remaintime  <= int32(timediff))
1356                    continue;
1357
1358                remaintime -= timediff;
1359            }
1360
1361            // prevent wrong values of remaincharges
1362            if(spellproto->procCharges)
1363            {
1364                if(remaincharges <= 0 || remaincharges > spellproto->procCharges)
1365                    remaincharges = spellproto->procCharges;
1366            }
1367            else
1368                remaincharges = -1;
1369
1370            /// do not load single target auras (unless they were cast by the player)
1371            if (caster_guid != GetGUID() && IsSingleTargetSpell(spellproto))
1372                continue;
1373
1374            Aura* aura = CreateAura(spellproto, effindex, NULL, this, NULL);
1375
1376            if(!damage)
1377                damage = aura->GetModifier()->m_amount;
1378            aura->SetLoadedState(caster_guid,damage,maxduration,remaintime,remaincharges);
1379            AddAura(aura);
1380        }
1381        while( result->NextRow() );
1382
1383        delete result;
1384    }
1385}
1386
1387void Pet::_SaveAuras()
1388{
1389    CharacterDatabase.PExecute("DELETE FROM pet_aura WHERE guid = '%u'",m_charmInfo->GetPetNumber());
1390
1391    AuraMap const& auras = GetAuras();
1392    for(AuraMap::const_iterator itr = auras.begin(); itr != auras.end(); ++itr)
1393    {
1394        // skip all auras from spell that apply at cast SPELL_AURA_MOD_SHAPESHIFT or pet area auras.
1395        SpellEntry const *spellInfo = itr->second->GetSpellProto();
1396        uint8 i;
1397        for (i = 0; i < 3; i++)
1398            if (spellInfo->EffectApplyAuraName[i] == SPELL_AURA_MOD_STEALTH ||
1399                spellInfo->Effect[i] == SPELL_EFFECT_APPLY_AREA_AURA_OWNER ||
1400                spellInfo->Effect[i] == SPELL_EFFECT_APPLY_AREA_AURA_PET )
1401                break;
1402
1403        if (i != 3)
1404            continue;
1405       
1406        if(itr->second->IsPassive())
1407            continue;
1408
1409        /// do not save single target auras (unless they were cast by the player)
1410        if (itr->second->GetCasterGUID() != GetGUID() && IsSingleTargetSpell(spellInfo))
1411            continue;
1412
1413        CharacterDatabase.PExecute("INSERT INTO pet_aura (guid,caster_guid,spell,effect_index,amount,maxduration,remaintime,remaincharges) "
1414            "VALUES ('%u', '" I64FMTD "', '%u', '%u', '%d', '%d', '%d', '%d')",
1415            m_charmInfo->GetPetNumber(), itr->second->GetCasterGUID(),(uint32)(*itr).second->GetId(), (uint32)(*itr).second->GetEffIndex(),(*itr).second->GetModifier()->m_amount,int((*itr).second->GetAuraMaxDuration()),int((*itr).second->GetAuraDuration()),int((*itr).second->m_procCharges));
1416    }
1417}
1418
1419bool Pet::addSpell(uint16 spell_id, uint16 active, PetSpellState state, uint16 slot_id, PetSpellType type)
1420{
1421    SpellEntry const *spellInfo = sSpellStore.LookupEntry(spell_id);
1422    if (!spellInfo)
1423    {
1424        // do pet spell book cleanup
1425        if(state == PETSPELL_UNCHANGED)                     // spell load case
1426        {
1427            sLog.outError("Pet::addSpell: Non-existed in SpellStore spell #%u request, deleting for all pets in `pet_spell`.",spell_id);
1428            CharacterDatabase.PExecute("DELETE FROM pet_spell WHERE spell = '%u'",spell_id);
1429        }
1430        else
1431            sLog.outError("Pet::addSpell: Non-existed in SpellStore spell #%u request.",spell_id);
1432
1433        return false;
1434    }
1435
1436    PetSpellMap::iterator itr = m_spells.find(spell_id);
1437    if (itr != m_spells.end())
1438    {
1439        if (itr->second->state == PETSPELL_REMOVED)
1440        {
1441            delete itr->second;
1442            m_spells.erase(itr);
1443            state = PETSPELL_CHANGED;
1444        }
1445        else if (state == PETSPELL_UNCHANGED && itr->second->state != PETSPELL_UNCHANGED)
1446        {
1447            // can be in case spell loading but learned at some previous spell loading
1448            itr->second->state = PETSPELL_UNCHANGED;
1449            return false;
1450        }
1451        else
1452            return false;
1453    }
1454
1455    uint32 oldspell_id = 0;
1456
1457    PetSpell *newspell = new PetSpell;
1458    newspell->state = state;
1459    newspell->type = type;
1460
1461    if(active == ACT_DECIDE)                                //active was not used before, so we save it's autocast/passive state here
1462    {
1463        if(IsPassiveSpell(spell_id))
1464            newspell->active = ACT_PASSIVE;
1465        else
1466            newspell->active = ACT_DISABLED;
1467    }
1468    else
1469        newspell->active = active;
1470
1471    uint32 chainstart = spellmgr.GetFirstSpellInChain(spell_id);
1472
1473    for (PetSpellMap::iterator itr = m_spells.begin(); itr != m_spells.end(); itr++)
1474    {
1475        if(itr->second->state == PETSPELL_REMOVED) continue;
1476
1477        if(spellmgr.GetFirstSpellInChain(itr->first) == chainstart)
1478        {
1479            slot_id = itr->second->slotId;
1480            newspell->active = itr->second->active;
1481
1482            if(newspell->active == ACT_ENABLED)
1483                ToggleAutocast(itr->first, false);
1484
1485            oldspell_id = itr->first;
1486            removeSpell(itr->first);
1487        }
1488    }
1489
1490    uint16 tmpslot=slot_id;
1491
1492    if (tmpslot == 0xffff)
1493    {
1494        uint16 maxid = 0;
1495        PetSpellMap::iterator itr;
1496        for (itr = m_spells.begin(); itr != m_spells.end(); ++itr)
1497        {
1498            if(itr->second->state == PETSPELL_REMOVED) continue;
1499            if (itr->second->slotId > maxid) maxid = itr->second->slotId;
1500        }
1501        tmpslot = maxid + 1;
1502    }
1503
1504    newspell->slotId = tmpslot;
1505    m_spells[spell_id] = newspell;
1506
1507    if (IsPassiveSpell(spell_id))
1508        CastSpell(this, spell_id, true);
1509    else if(state == PETSPELL_NEW)
1510        m_charmInfo->AddSpellToAB(oldspell_id, spell_id);
1511
1512    if(newspell->active == ACT_ENABLED)
1513        ToggleAutocast(spell_id, true);
1514
1515    return true;
1516}
1517
1518bool Pet::learnSpell(uint16 spell_id)
1519{
1520    // prevent duplicated entires in spell book
1521    if (!addSpell(spell_id))
1522        return false;
1523
1524    Unit* owner = GetOwner();
1525    if(owner->GetTypeId()==TYPEID_PLAYER)
1526        ((Player*)owner)->PetSpellInitialize();
1527    return true;
1528}
1529
1530void Pet::removeSpell(uint16 spell_id)
1531{
1532    PetSpellMap::iterator itr = m_spells.find(spell_id);
1533    if (itr == m_spells.end())
1534        return;
1535
1536    if(itr->second->state == PETSPELL_REMOVED)
1537        return;
1538
1539    if(itr->second->state == PETSPELL_NEW)
1540    {
1541        delete itr->second;
1542        m_spells.erase(itr);
1543    }
1544    else
1545        itr->second->state = PETSPELL_REMOVED;
1546
1547    RemoveAurasDueToSpell(spell_id);
1548}
1549
1550bool Pet::_removeSpell(uint16 spell_id)
1551{
1552    PetSpellMap::iterator itr = m_spells.find(spell_id);
1553    if (itr != m_spells.end())
1554    {
1555        delete itr->second;
1556        m_spells.erase(itr);
1557        return true;
1558    }
1559    return false;
1560}
1561
1562void Pet::InitPetCreateSpells()
1563{
1564    m_charmInfo->InitPetActionBar();
1565
1566    m_spells.clear();
1567    int32 usedtrainpoints = 0, petspellid;
1568    PetCreateSpellEntry const* CreateSpells = objmgr.GetPetCreateSpellEntry(GetEntry());
1569    if(CreateSpells)
1570    {
1571        for(uint8 i = 0; i < 4; i++)
1572        {
1573            if(!CreateSpells->spellid[i])
1574                break;
1575
1576            SpellEntry const *learn_spellproto = sSpellStore.LookupEntry(CreateSpells->spellid[i]);
1577            if(!learn_spellproto)
1578                continue;
1579
1580            if(learn_spellproto->Effect[0] == SPELL_EFFECT_LEARN_SPELL || learn_spellproto->Effect[0] == SPELL_EFFECT_LEARN_PET_SPELL)
1581            {
1582                petspellid = learn_spellproto->EffectTriggerSpell[0];
1583                Unit* owner = GetOwner();
1584                if(owner->GetTypeId() == TYPEID_PLAYER && !((Player*)owner)->HasSpell(learn_spellproto->Id))
1585                {
1586                    if(IsPassiveSpell(petspellid))          //learn passive skills when tamed, not sure if thats right
1587                        ((Player*)owner)->learnSpell(learn_spellproto->Id);
1588                    else
1589                        AddTeachSpell(learn_spellproto->EffectTriggerSpell[0], learn_spellproto->Id);
1590                }
1591            }
1592            else
1593                petspellid = learn_spellproto->Id;
1594
1595            addSpell(petspellid);
1596
1597            SkillLineAbilityMap::const_iterator lower = spellmgr.GetBeginSkillLineAbilityMap(learn_spellproto->EffectTriggerSpell[0]);
1598            SkillLineAbilityMap::const_iterator upper = spellmgr.GetEndSkillLineAbilityMap(learn_spellproto->EffectTriggerSpell[0]);
1599
1600            for(SkillLineAbilityMap::const_iterator _spell_idx = lower; _spell_idx != upper; ++_spell_idx)
1601            {
1602                usedtrainpoints += _spell_idx->second->reqtrainpoints;
1603                break;
1604            }
1605        }
1606    }
1607
1608    LearnPetPassives();
1609
1610    CastPetAuras(false);
1611
1612    SetTP(-usedtrainpoints);
1613}
1614
1615void Pet::CheckLearning(uint32 spellid)
1616{
1617                                                            //charmed case -> prevent crash
1618    if(GetTypeId() == TYPEID_PLAYER || getPetType() != HUNTER_PET)
1619        return;
1620
1621    Unit* owner = GetOwner();
1622
1623    if(m_teachspells.empty() || !owner || owner->GetTypeId() != TYPEID_PLAYER)
1624        return;
1625
1626    TeachSpellMap::iterator itr = m_teachspells.find(spellid);
1627    if(itr == m_teachspells.end())
1628        return;
1629
1630    if(urand(0, 100) < 10)
1631    {
1632        ((Player*)owner)->learnSpell(itr->second);
1633        m_teachspells.erase(itr);
1634    }
1635}
1636
1637uint32 Pet::resetTalentsCost() const
1638{
1639    uint32 days = (sWorld.GetGameTime() - m_resetTalentsTime)/DAY;
1640
1641    // The first time reset costs 10 silver; after 1 day cost is reset to 10 silver
1642    if(m_resetTalentsCost < 10*SILVER || days > 0)
1643        return 10*SILVER;
1644    // then 50 silver
1645    else if(m_resetTalentsCost < 50*SILVER)
1646        return 50*SILVER;
1647    // then 1 gold
1648    else if(m_resetTalentsCost < 1*GOLD)
1649        return 1*GOLD;
1650    // then increasing at a rate of 1 gold; cap 10 gold
1651    else
1652        return (m_resetTalentsCost + 1*GOLD > 10*GOLD ? 10*GOLD : m_resetTalentsCost + 1*GOLD);
1653}
1654
1655void Pet::ToggleAutocast(uint32 spellid, bool apply)
1656{
1657    if(IsPassiveSpell(spellid))
1658        return;
1659
1660    PetSpellMap::const_iterator itr = m_spells.find((uint16)spellid);
1661
1662    int i;
1663
1664    if(apply)
1665    {
1666        for (i = 0; i < m_autospells.size() && m_autospells[i] != spellid; i++);
1667        if (i == m_autospells.size())
1668        {
1669            m_autospells.push_back(spellid);
1670            itr->second->active = ACT_ENABLED;
1671            itr->second->state = PETSPELL_CHANGED;
1672        }
1673    }
1674    else
1675    {
1676        AutoSpellList::iterator itr2 = m_autospells.begin();
1677        for (i = 0; i < m_autospells.size() && m_autospells[i] != spellid; i++, itr2++);
1678        if (i < m_autospells.size())
1679        {
1680            m_autospells.erase(itr2);
1681            itr->second->active = ACT_DISABLED;
1682            itr->second->state = PETSPELL_CHANGED;
1683        }
1684    }
1685}
1686
1687bool Pet::Create(uint32 guidlow, Map *map, uint32 Entry, uint32 pet_number)
1688{
1689    SetMapId(map->GetId());
1690    SetInstanceId(map->GetInstanceId());
1691
1692    Object::_Create(guidlow, pet_number, HIGHGUID_PET);
1693
1694    m_DBTableGuid = guidlow;
1695    m_originalEntry = Entry;
1696
1697    if(!InitEntry(Entry))
1698        return false;
1699
1700    SetByteValue(UNIT_FIELD_BYTES_2, 0, SHEATH_STATE_MELEE );
1701    SetByteValue(UNIT_FIELD_BYTES_2, 1, UNIT_BYTE2_FLAG_UNK3 | UNIT_BYTE2_FLAG_AURAS | UNIT_BYTE2_FLAG_UNK5 );
1702
1703    if(getPetType() == MINI_PET)                            // always non-attackable
1704        SetFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_NON_ATTACKABLE);
1705
1706    return true;
1707}
1708
1709bool Pet::HasSpell(uint32 spell) const
1710{
1711    return (m_spells.find(spell) != m_spells.end());
1712}
1713
1714// Get all passive spells in our skill line
1715void Pet::LearnPetPassives()
1716{
1717    CreatureInfo const* cInfo = GetCreatureInfo();
1718    if(!cInfo)
1719        return;
1720
1721    CreatureFamilyEntry const* cFamily = sCreatureFamilyStore.LookupEntry(cInfo->family);
1722    if(!cFamily)
1723        return;
1724
1725    PetFamilySpellsStore::const_iterator petStore = sPetFamilySpellsStore.find(cFamily->ID);
1726    if(petStore != sPetFamilySpellsStore.end())
1727    {
1728        for(PetFamilySpellsSet::const_iterator petSet = petStore->second.begin(); petSet != petStore->second.end(); ++petSet)
1729            addSpell(*petSet, ACT_DECIDE, PETSPELL_NEW, 0xffff, PETSPELL_FAMILY);
1730    }
1731}
1732
1733void Pet::CastPetAuras(bool current)
1734{
1735    Unit* owner = GetOwner();
1736    if(!owner)
1737        return;
1738
1739    if(getPetType() != HUNTER_PET && (getPetType() != SUMMON_PET || owner->getClass() != CLASS_WARLOCK))
1740        return;
1741
1742    for(PetAuraSet::iterator itr = owner->m_petAuras.begin(); itr != owner->m_petAuras.end(); )
1743    {
1744        PetAura const* pa = *itr;
1745        ++itr;
1746
1747        if(!current && pa->IsRemovedOnChangePet())
1748            owner->RemovePetAura(pa);
1749        else
1750            CastPetAura(pa);
1751    }
1752}
1753
1754void Pet::CastPetAura(PetAura const* aura)
1755{
1756    uint16 auraId = aura->GetAura(GetEntry());
1757    if(!auraId)
1758        return;
1759
1760    if(auraId == 35696)                                       // Demonic Knowledge
1761    {
1762        int32 basePoints = int32(aura->GetDamage() * (GetStat(STAT_STAMINA) + GetStat(STAT_INTELLECT)) / 100);
1763        CastCustomSpell(this,auraId,&basePoints, NULL, NULL, true );
1764    }
1765    else
1766        CastSpell(this, auraId, true);
1767}
Note: See TracBrowser for help on using the browser.