[8419] Allow updating pet stats at loading more early (before InitStatsForLevel).
[getmangos.git] / src / game / Pet.cpp
blob3581c456f7d75df9d7fda6c6b4986d62148891c8
1 /*
2 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "Common.h"
20 #include "Database/DatabaseEnv.h"
21 #include "Log.h"
22 #include "WorldPacket.h"
23 #include "ObjectMgr.h"
24 #include "SpellMgr.h"
25 #include "Pet.h"
26 #include "Formulas.h"
27 #include "SpellAuras.h"
28 #include "CreatureAI.h"
29 #include "Unit.h"
30 #include "Util.h"
32 char const* petTypeSuffix[MAX_PET_TYPE] =
34 "'s Minion", // SUMMON_PET
35 "'s Pet", // HUNTER_PET
36 "'s Guardian", // GUARDIAN_PET
37 "'s Companion" // MINI_PET
40 Pet::Pet(PetType type) :
41 Creature(), m_removed(false), m_petType(type), m_happinessTimer(7500), m_duration(0), m_resetTalentsCost(0),
42 m_bonusdamage(0), m_resetTalentsTime(0), m_usedTalentCount(0), m_auraUpdateMask(0), m_loading(false),
43 m_declinedname(NULL)
45 m_isPet = true;
46 m_name = "Pet";
47 m_regenTimer = 4000;
49 // pets always have a charminfo, even if they are not actually charmed
50 CharmInfo* charmInfo = InitCharmInfo(this);
52 if(type == MINI_PET) // always passive
53 charmInfo->SetReactState(REACT_PASSIVE);
54 else if(type == GUARDIAN_PET) // always aggressive
55 charmInfo->SetReactState(REACT_AGGRESSIVE);
58 Pet::~Pet()
60 if(m_uint32Values) // only for fully created Object
61 ObjectAccessor::Instance().RemoveObject(this);
63 delete m_declinedname;
66 void Pet::AddToWorld()
68 ///- Register the pet for guid lookup
69 if(!IsInWorld()) ObjectAccessor::Instance().AddObject(this);
70 Unit::AddToWorld();
73 void Pet::RemoveFromWorld()
75 ///- Remove the pet from the accessor
76 if(IsInWorld()) ObjectAccessor::Instance().RemoveObject(this);
77 ///- Don't call the function for Creature, normal mobs + totems go in a different storage
78 Unit::RemoveFromWorld();
81 bool Pet::LoadPetFromDB( Player* owner, uint32 petentry, uint32 petnumber, bool current )
83 m_loading = true;
85 uint32 ownerid = owner->GetGUIDLow();
87 QueryResult *result;
89 if (petnumber)
90 // known petnumber entry 0 1 2(?) 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
91 result = CharacterDatabase.PQuery("SELECT id, entry, owner, modelid, level, exp, Reactstate, slot, name, renamed, curhealth, curmana, curhappiness, abdata, savetime, resettalents_cost, resettalents_time, CreatedBySpell, PetType "
92 "FROM character_pet WHERE owner = '%u' AND id = '%u'",
93 ownerid, petnumber);
94 else if (current)
95 // current pet (slot 0) 0 1 2(?) 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
96 result = CharacterDatabase.PQuery("SELECT id, entry, owner, modelid, level, exp, Reactstate, slot, name, renamed, curhealth, curmana, curhappiness, abdata, savetime, resettalents_cost, resettalents_time, CreatedBySpell, PetType "
97 "FROM character_pet WHERE owner = '%u' AND slot = '%u'",
98 ownerid, PET_SAVE_AS_CURRENT );
99 else if (petentry)
100 // known petentry entry (unique for summoned pet, but non unique for hunter pet (only from current or not stabled pets)
101 // 0 1 2(?) 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
102 result = CharacterDatabase.PQuery("SELECT id, entry, owner, modelid, level, exp, Reactstate, slot, name, renamed, curhealth, curmana, curhappiness, abdata, savetime, resettalents_cost, resettalents_time, CreatedBySpell, PetType "
103 "FROM character_pet WHERE owner = '%u' AND entry = '%u' AND (slot = '%u' OR slot > '%u') ",
104 ownerid, petentry,PET_SAVE_AS_CURRENT,PET_SAVE_LAST_STABLE_SLOT);
105 else
106 // any current or other non-stabled pet (for hunter "call pet")
107 // 0 1 2(?) 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
108 result = CharacterDatabase.PQuery("SELECT id, entry, owner, modelid, level, exp, Reactstate, slot, name, renamed, curhealth, curmana, curhappiness, abdata, savetime, resettalents_cost, resettalents_time, CreatedBySpell, PetType "
109 "FROM character_pet WHERE owner = '%u' AND (slot = '%u' OR slot > '%u') ",
110 ownerid,PET_SAVE_AS_CURRENT,PET_SAVE_LAST_STABLE_SLOT);
112 if(!result)
113 return false;
115 Field *fields = result->Fetch();
117 // update for case of current pet "slot = 0"
118 petentry = fields[1].GetUInt32();
119 if (!petentry)
121 delete result;
122 return false;
125 uint32 summon_spell_id = fields[17].GetUInt32();
126 SpellEntry const* spellInfo = sSpellStore.LookupEntry(summon_spell_id);
128 bool is_temporary_summoned = spellInfo && GetSpellDuration(spellInfo) > 0;
130 // check temporary summoned pets like mage water elemental
131 if (current && is_temporary_summoned)
133 delete result;
134 return false;
137 PetType pet_type = PetType(fields[18].GetUInt8());
138 if(pet_type==HUNTER_PET)
140 CreatureInfo const* creatureInfo = objmgr.GetCreatureTemplate(petentry);
141 if(!creatureInfo || !creatureInfo->isTameable(owner->CanTameExoticPets()))
143 delete result;
144 return false;
148 uint32 pet_number = fields[0].GetUInt32();
150 if (current && owner->IsPetNeedBeTemporaryUnsummoned())
152 owner->SetTemporaryUnsummonedPetNumber(pet_number);
153 delete result;
154 return false;
157 Map *map = owner->GetMap();
158 uint32 guid = objmgr.GenerateLowGuid(HIGHGUID_PET);
159 if (!Create(guid, map, owner->GetPhaseMask(), petentry, pet_number))
161 delete result;
162 return false;
165 float px, py, pz;
166 owner->GetClosePoint(px, py, pz, GetObjectSize(), PET_FOLLOW_DIST, PET_FOLLOW_ANGLE);
168 Relocate(px, py, pz, owner->GetOrientation());
170 if (!IsPositionValid())
172 sLog.outError("Pet (guidlow %d, entry %d) not loaded. Suggested coordinates isn't valid (X: %f Y: %f)",
173 GetGUIDLow(), GetEntry(), GetPositionX(), GetPositionY());
174 delete result;
175 return false;
178 setPetType(pet_type);
179 setFaction(owner->getFaction());
180 SetUInt32Value(UNIT_CREATED_BY_SPELL, summon_spell_id);
182 CreatureInfo const *cinfo = GetCreatureInfo();
183 if (cinfo->type == CREATURE_TYPE_CRITTER)
185 AIM_Initialize();
186 map->Add((Creature*)this);
187 delete result;
188 return true;
191 m_charmInfo->SetPetNumber(pet_number, IsPermanentPetFor(owner));
193 SetOwnerGUID(owner->GetGUID());
194 SetDisplayId(fields[3].GetUInt32());
195 SetNativeDisplayId(fields[3].GetUInt32());
196 uint32 petlevel = fields[4].GetUInt32();
197 SetUInt32Value(UNIT_NPC_FLAGS, UNIT_NPC_FLAG_NONE);
198 SetName(fields[8].GetString());
200 switch (getPetType())
202 case SUMMON_PET:
203 petlevel=owner->getLevel();
205 SetUInt32Value(UNIT_FIELD_BYTES_0, 2048);
206 SetUInt32Value(UNIT_FIELD_FLAGS, UNIT_FLAG_PVP_ATTACKABLE);
207 // this enables popup window (pet dismiss, cancel)
208 break;
209 case HUNTER_PET:
210 SetUInt32Value(UNIT_FIELD_BYTES_0, 0x02020100);
211 SetSheath(SHEATH_STATE_MELEE);
212 SetByteValue(UNIT_FIELD_BYTES_2, 2, fields[9].GetBool() ? UNIT_RENAME_NOT_ALLOWED : UNIT_RENAME_ALLOWED);
214 SetUInt32Value(UNIT_FIELD_FLAGS, UNIT_FLAG_PVP_ATTACKABLE);
215 // this enables popup window (pet abandon, cancel)
216 SetMaxPower(POWER_HAPPINESS, GetCreatePowers(POWER_HAPPINESS));
217 SetPower(POWER_HAPPINESS, fields[12].GetUInt32());
218 setPowerType(POWER_FOCUS);
219 break;
220 default:
221 sLog.outError("Pet have incorrect type (%u) for pet loading.", getPetType());
224 if(owner->IsPvP())
225 SetPvP(true);
227 SetCanModifyStats(true);
228 InitStatsForLevel(petlevel);
229 InitTalentForLevel(); // set original talents points before spell loading
231 SetUInt32Value(UNIT_FIELD_PET_NAME_TIMESTAMP, time(NULL));
232 SetUInt32Value(UNIT_FIELD_PETEXPERIENCE, fields[5].GetUInt32());
233 SetCreatorGUID(owner->GetGUID());
235 m_charmInfo->SetReactState(ReactStates(fields[6].GetUInt8()));
237 uint32 savedhealth = fields[10].GetUInt32();
238 uint32 savedmana = fields[11].GetUInt32();
240 // set current pet as current
241 // 0=current
242 // 1..MAX_PET_STABLES in stable slot
243 // PET_SAVE_NOT_IN_SLOT(100) = not stable slot (summoning))
244 if (fields[7].GetUInt32() != 0)
246 CharacterDatabase.BeginTransaction();
247 CharacterDatabase.PExecute("UPDATE character_pet SET slot = '%u' WHERE owner = '%u' AND slot = '%u' AND id <> '%u'",
248 PET_SAVE_NOT_IN_SLOT, ownerid, PET_SAVE_AS_CURRENT, m_charmInfo->GetPetNumber());
249 CharacterDatabase.PExecute("UPDATE character_pet SET slot = '%u' WHERE owner = '%u' AND id = '%u'",
250 PET_SAVE_AS_CURRENT, ownerid, m_charmInfo->GetPetNumber());
251 CharacterDatabase.CommitTransaction();
254 // load action bar, if data broken will fill later by default spells.
255 if (!is_temporary_summoned)
256 m_charmInfo->LoadPetActionBar(fields[13].GetCppString());
258 // since last save (in seconds)
259 uint32 timediff = (time(NULL) - fields[14].GetUInt32());
261 m_resetTalentsCost = fields[15].GetUInt32();
262 m_resetTalentsTime = fields[16].GetUInt64();
264 delete result;
266 //load spells/cooldowns/auras
267 _LoadAuras(timediff);
269 //init AB
270 if (is_temporary_summoned)
272 // Temporary summoned pets always have initial spell list at load
273 InitPetCreateSpells();
275 else
277 LearnPetPassives();
278 CastPetAuras(current);
281 if (getPetType() == SUMMON_PET && !current) //all (?) summon pets come with full health when called, but not when they are current
283 SetHealth(GetMaxHealth());
284 SetPower(POWER_MANA, GetMaxPower(POWER_MANA));
286 else
288 SetHealth(savedhealth > GetMaxHealth() ? GetMaxHealth() : savedhealth);
289 SetPower(POWER_MANA, savedmana > GetMaxPower(POWER_MANA) ? GetMaxPower(POWER_MANA) : savedmana);
292 AIM_Initialize();
293 map->Add((Creature*)this);
295 // Spells should be loaded after pet is added to map, because in CheckCast is check on it
296 _LoadSpells();
297 InitLevelupSpellsForLevel();
299 CleanupActionBar(); // remove unknown spells from action bar after load
301 _LoadSpellCooldowns();
303 owner->SetPet(this); // in DB stored only full controlled creature
304 sLog.outDebug("New Pet has guid %u", GetGUIDLow());
306 if (owner->GetTypeId() == TYPEID_PLAYER)
308 ((Player*)owner)->PetSpellInitialize();
309 if(((Player*)owner)->GetGroup())
310 ((Player*)owner)->SetGroupUpdateFlag(GROUP_UPDATE_PET);
312 ((Player*)owner)->SendTalentsInfoData(true);
315 if (owner->GetTypeId() == TYPEID_PLAYER && getPetType() == HUNTER_PET)
317 result = CharacterDatabase.PQuery("SELECT genitive, dative, accusative, instrumental, prepositional FROM character_pet_declinedname WHERE owner = '%u' AND id = '%u'", owner->GetGUIDLow(), GetCharmInfo()->GetPetNumber());
319 if(result)
321 if(m_declinedname)
322 delete m_declinedname;
324 m_declinedname = new DeclinedName;
325 Field *fields2 = result->Fetch();
326 for(int i = 0; i < MAX_DECLINED_NAME_CASES; ++i)
328 m_declinedname->name[i] = fields2[i].GetCppString();
333 m_loading = false;
335 SynchronizeLevelWithOwner();
336 return true;
339 void Pet::SavePetToDB(PetSaveMode mode)
341 if (!GetEntry())
342 return;
344 // save only fully controlled creature
345 if (!isControlled())
346 return;
348 // not save not player pets
349 if(!IS_PLAYER_GUID(GetOwnerGUID()))
350 return;
352 Player* pOwner = (Player*)GetOwner();
353 if (!pOwner)
354 return;
356 // not save pet as current if another pet temporary unsummoned
357 if (mode == PET_SAVE_AS_CURRENT && pOwner->GetTemporaryUnsummonedPetNumber() &&
358 pOwner->GetTemporaryUnsummonedPetNumber() != m_charmInfo->GetPetNumber())
360 // pet will lost anyway at restore temporary unsummoned
361 if(getPetType()==HUNTER_PET)
362 return;
364 // for warlock case
365 mode = PET_SAVE_NOT_IN_SLOT;
368 uint32 curhealth = GetHealth();
369 uint32 curmana = GetPower(POWER_MANA);
371 // stable and not in slot saves
372 if(mode > PET_SAVE_AS_CURRENT)
374 RemoveAllAuras();
376 //only alive hunter pets get auras saved, the others don't
377 if(!(getPetType() == HUNTER_PET && isAlive()))
378 m_Auras.clear();
381 _SaveSpells();
382 _SaveSpellCooldowns();
383 _SaveAuras();
385 // current/stable/not_in_slot
386 if(mode >= PET_SAVE_AS_CURRENT)
388 uint32 owner = GUID_LOPART(GetOwnerGUID());
389 std::string name = m_name;
390 CharacterDatabase.escape_string(name);
391 CharacterDatabase.BeginTransaction();
392 // remove current data
393 CharacterDatabase.PExecute("DELETE FROM character_pet WHERE owner = '%u' AND id = '%u'", owner,m_charmInfo->GetPetNumber() );
395 // prevent duplicate using slot (except PET_SAVE_NOT_IN_SLOT)
396 if(mode <= PET_SAVE_LAST_STABLE_SLOT)
397 CharacterDatabase.PExecute("UPDATE character_pet SET slot = '%u' WHERE owner = '%u' AND slot = '%u'",
398 PET_SAVE_NOT_IN_SLOT, owner, uint32(mode) );
400 // prevent existence another hunter pet in PET_SAVE_AS_CURRENT and PET_SAVE_NOT_IN_SLOT
401 if(getPetType()==HUNTER_PET && (mode==PET_SAVE_AS_CURRENT||mode > PET_SAVE_LAST_STABLE_SLOT))
402 CharacterDatabase.PExecute("DELETE FROM character_pet WHERE owner = '%u' AND (slot = '%u' OR slot > '%u')",
403 owner,PET_SAVE_AS_CURRENT,PET_SAVE_LAST_STABLE_SLOT);
404 // save pet
405 std::ostringstream ss;
406 ss << "INSERT INTO character_pet ( id, entry, owner, modelid, level, exp, Reactstate, slot, name, renamed, curhealth, curmana, curhappiness, abdata, savetime, resettalents_cost, resettalents_time, CreatedBySpell, PetType) "
407 << "VALUES ("
408 << m_charmInfo->GetPetNumber() << ", "
409 << GetEntry() << ", "
410 << owner << ", "
411 << GetNativeDisplayId() << ", "
412 << getLevel() << ", "
413 << GetUInt32Value(UNIT_FIELD_PETEXPERIENCE) << ", "
414 << uint32(m_charmInfo->GetReactState()) << ", "
415 << uint32(mode) << ", '"
416 << name.c_str() << "', "
417 << uint32((GetByteValue(UNIT_FIELD_BYTES_2, 2) == UNIT_RENAME_ALLOWED)?0:1) << ", "
418 << (curhealth<1?1:curhealth) << ", "
419 << curmana << ", "
420 << GetPower(POWER_HAPPINESS) << ", '";
422 // save only spell slots from action bar
423 for(uint32 i = ACTION_BAR_INDEX_PET_SPELL_START; i < ACTION_BAR_INDEX_PET_SPELL_END; ++i)
425 ss << uint32(m_charmInfo->GetActionBarEntry(i)->GetType()) << " "
426 << uint32(m_charmInfo->GetActionBarEntry(i)->GetAction()) << " ";
429 ss << "', "
430 << time(NULL) << ", "
431 << uint32(m_resetTalentsCost) << ", "
432 << uint64(m_resetTalentsTime) << ", "
433 << GetUInt32Value(UNIT_CREATED_BY_SPELL) << ", "
434 << uint32(getPetType()) << ")";
436 CharacterDatabase.Execute( ss.str().c_str() );
437 CharacterDatabase.CommitTransaction();
439 // delete
440 else
442 RemoveAllAuras();
443 DeleteFromDB(m_charmInfo->GetPetNumber());
447 void Pet::DeleteFromDB(uint32 guidlow)
449 CharacterDatabase.PExecute("DELETE FROM character_pet WHERE id = '%u'", guidlow);
450 CharacterDatabase.PExecute("DELETE FROM character_pet_declinedname WHERE id = '%u'", guidlow);
451 CharacterDatabase.PExecute("DELETE FROM pet_aura WHERE guid = '%u'", guidlow);
452 CharacterDatabase.PExecute("DELETE FROM pet_spell WHERE guid = '%u'", guidlow);
453 CharacterDatabase.PExecute("DELETE FROM pet_spell_cooldown WHERE guid = '%u'", guidlow);
456 void Pet::setDeathState(DeathState s) // overwrite virtual Creature::setDeathState and Unit::setDeathState
458 Creature::setDeathState(s);
459 if(getDeathState()==CORPSE)
461 //remove summoned pet (no corpse)
462 if(getPetType()==SUMMON_PET)
463 Remove(PET_SAVE_NOT_IN_SLOT);
464 // other will despawn at corpse desppawning (Pet::Update code)
465 else
467 // pet corpse non lootable and non skinnable
468 SetUInt32Value( UNIT_DYNAMIC_FLAGS, 0x00 );
469 RemoveFlag (UNIT_FIELD_FLAGS, UNIT_FLAG_SKINNABLE);
471 //lose happiness when died and not in BG/Arena
472 MapEntry const* mapEntry = sMapStore.LookupEntry(GetMapId());
473 if(!mapEntry || (mapEntry->map_type != MAP_ARENA && mapEntry->map_type != MAP_BATTLEGROUND))
474 ModifyPower(POWER_HAPPINESS, -HAPPINESS_LEVEL_SIZE);
476 SetFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_STUNNED);
479 else if(getDeathState()==ALIVE)
481 RemoveFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_STUNNED);
482 CastPetAuras(true);
486 void Pet::Update(uint32 diff)
488 if(m_removed) // pet already removed, just wait in remove queue, no updates
489 return;
491 switch( m_deathState )
493 case CORPSE:
495 if( m_deathTimer <= diff )
497 assert(getPetType()!=SUMMON_PET && "Must be already removed.");
498 Remove(PET_SAVE_NOT_IN_SLOT); //hunters' pets never get removed because of death, NEVER!
499 return;
501 break;
503 case ALIVE:
505 // unsummon pet that lost owner
506 Unit* owner = GetOwner();
507 if(!owner || (!IsWithinDistInMap(owner, OWNER_MAX_DISTANCE) && (owner->GetCharmGUID() && (owner->GetCharmGUID() != GetGUID()))) || (isControlled() && !owner->GetPetGUID()))
509 Remove(PET_SAVE_NOT_IN_SLOT, true);
510 return;
513 if(isControlled())
515 if( owner->GetPetGUID() != GetGUID() )
517 Remove(getPetType()==HUNTER_PET?PET_SAVE_AS_DELETED:PET_SAVE_NOT_IN_SLOT);
518 return;
522 if(m_duration > 0)
524 if(m_duration > diff)
525 m_duration -= diff;
526 else
528 Remove(getPetType() != SUMMON_PET ? PET_SAVE_AS_DELETED:PET_SAVE_NOT_IN_SLOT);
529 return;
533 //regenerate focus for hunter pets or energy for deathknight's ghoul
534 if(m_regenTimer <= diff)
536 switch (getPowerType())
538 case POWER_FOCUS:
539 case POWER_ENERGY:
540 Regenerate(getPowerType());
541 break;
542 default:
543 break;
545 m_regenTimer = 4000;
547 else
548 m_regenTimer -= diff;
550 if(getPetType() != HUNTER_PET)
551 break;
553 if(m_happinessTimer <= diff)
555 LooseHappiness();
556 m_happinessTimer = 7500;
558 else
559 m_happinessTimer -= diff;
561 break;
563 default:
564 break;
566 Creature::Update(diff);
569 void Pet::Regenerate(Powers power)
571 uint32 curValue = GetPower(power);
572 uint32 maxValue = GetMaxPower(power);
574 if (curValue >= maxValue)
575 return;
577 float addvalue = 0.0f;
579 switch (power)
581 case POWER_FOCUS:
583 // For hunter pets.
584 addvalue = 24 * sWorld.getRate(RATE_POWER_FOCUS);
585 break;
587 case POWER_ENERGY:
589 // For deathknight's ghoul.
590 addvalue = 20;
591 break;
593 default:
594 return;
597 // Apply modifiers (if any).
598 AuraList const& ModPowerRegenPCTAuras = GetAurasByType(SPELL_AURA_MOD_POWER_REGEN_PERCENT);
599 for(AuraList::const_iterator i = ModPowerRegenPCTAuras.begin(); i != ModPowerRegenPCTAuras.end(); ++i)
600 if ((*i)->GetModifier()->m_miscvalue == power)
601 addvalue *= ((*i)->GetModifier()->m_amount + 100) / 100.0f;
603 ModifyPower(power, (int32)addvalue);
606 void Pet::LooseHappiness()
608 uint32 curValue = GetPower(POWER_HAPPINESS);
609 if (curValue <= 0)
610 return;
611 int32 addvalue = 670; //value is 70/35/17/8/4 (per min) * 1000 / 8 (timer 7.5 secs)
612 if(isInCombat()) //we know in combat happiness fades faster, multiplier guess
613 addvalue = int32(addvalue * 1.5);
614 ModifyPower(POWER_HAPPINESS, -addvalue);
617 HappinessState Pet::GetHappinessState()
619 if(GetPower(POWER_HAPPINESS) < HAPPINESS_LEVEL_SIZE)
620 return UNHAPPY;
621 else if(GetPower(POWER_HAPPINESS) >= HAPPINESS_LEVEL_SIZE * 2)
622 return HAPPY;
623 else
624 return CONTENT;
627 bool Pet::CanTakeMoreActiveSpells(uint32 spellid)
629 uint8 activecount = 1;
630 uint32 chainstartstore[ACTIVE_SPELLS_MAX];
632 if(IsPassiveSpell(spellid))
633 return true;
635 chainstartstore[0] = spellmgr.GetFirstSpellInChain(spellid);
637 for (PetSpellMap::const_iterator itr = m_spells.begin(); itr != m_spells.end(); ++itr)
639 if(itr->second.state == PETSPELL_REMOVED)
640 continue;
642 if(IsPassiveSpell(itr->first))
643 continue;
645 uint32 chainstart = spellmgr.GetFirstSpellInChain(itr->first);
647 uint8 x;
649 for(x = 0; x < activecount; x++)
651 if(chainstart == chainstartstore[x])
652 break;
655 if(x == activecount) //spellchain not yet saved -> add active count
657 ++activecount;
658 if(activecount > ACTIVE_SPELLS_MAX)
659 return false;
660 chainstartstore[x] = chainstart;
663 return true;
666 void Pet::Remove(PetSaveMode mode, bool returnreagent)
668 Unit* owner = GetOwner();
670 if(owner)
672 if(owner->GetTypeId()==TYPEID_PLAYER)
674 ((Player*)owner)->RemovePet(this,mode,returnreagent);
675 return;
678 // only if current pet in slot
679 if(owner->GetPetGUID()==GetGUID())
680 owner->SetPet(0);
683 AddObjectToRemoveList();
684 m_removed = true;
687 void Pet::GivePetXP(uint32 xp)
689 if(getPetType() != HUNTER_PET)
690 return;
692 if ( xp < 1 )
693 return;
695 if(!isAlive())
696 return;
698 uint32 level = getLevel();
700 // XP to money conversion processed in Player::RewardQuest
701 if(level >= sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL))
702 return;
704 uint32 curXP = GetUInt32Value(UNIT_FIELD_PETEXPERIENCE);
705 uint32 nextLvlXP = GetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP);
706 uint32 newXP = curXP + xp;
708 if(newXP >= nextLvlXP && level+1 > GetOwner()->getLevel())
710 SetUInt32Value(UNIT_FIELD_PETEXPERIENCE, nextLvlXP-1);
711 return;
714 while( newXP >= nextLvlXP && level < sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL) )
716 newXP -= nextLvlXP;
718 GivePetLevel(level+1);
719 SetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP, objmgr.GetXPForLevel(level+1)/4);
721 level = getLevel();
722 nextLvlXP = GetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP);
725 SetUInt32Value(UNIT_FIELD_PETEXPERIENCE, newXP);
728 void Pet::GivePetLevel(uint32 level)
730 if(!level)
731 return;
733 InitStatsForLevel(level);
734 InitLevelupSpellsForLevel();
735 InitTalentForLevel();
738 bool Pet::CreateBaseAtCreature(Creature* creature)
740 if(!creature)
742 sLog.outError("CRITICAL: NULL pointer parsed into CreateBaseAtCreature()");
743 return false;
745 uint32 guid=objmgr.GenerateLowGuid(HIGHGUID_PET);
747 sLog.outBasic("Create pet");
748 uint32 pet_number = objmgr.GeneratePetNumber();
749 if(!Create(guid, creature->GetMap(), creature->GetPhaseMask(), creature->GetEntry(), pet_number))
750 return false;
752 Relocate(creature->GetPositionX(), creature->GetPositionY(), creature->GetPositionZ(), creature->GetOrientation());
754 if(!IsPositionValid())
756 sLog.outError("Pet (guidlow %d, entry %d) not created base at creature. Suggested coordinates isn't valid (X: %f Y: %f)",
757 GetGUIDLow(), GetEntry(), GetPositionX(), GetPositionY());
758 return false;
761 CreatureInfo const *cinfo = GetCreatureInfo();
762 if(!cinfo)
764 sLog.outError("CreateBaseAtCreature() failed, creatureInfo is missing!");
765 return false;
768 if(cinfo->type == CREATURE_TYPE_CRITTER)
770 setPetType(MINI_PET);
771 return true;
773 SetDisplayId(creature->GetDisplayId());
774 SetNativeDisplayId(creature->GetNativeDisplayId());
775 SetMaxPower(POWER_HAPPINESS, GetCreatePowers(POWER_HAPPINESS));
776 SetPower(POWER_HAPPINESS, 166500);
777 setPowerType(POWER_FOCUS);
778 SetUInt32Value(UNIT_FIELD_PET_NAME_TIMESTAMP, 0);
779 SetUInt32Value(UNIT_FIELD_PETEXPERIENCE, 0);
780 SetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP, objmgr.GetXPForLevel(creature->getLevel())/4);
781 SetUInt32Value(UNIT_NPC_FLAGS, UNIT_NPC_FLAG_NONE);
783 if(CreatureFamilyEntry const* cFamily = sCreatureFamilyStore.LookupEntry(cinfo->family))
784 SetName(cFamily->Name[sWorld.GetDefaultDbcLocale()]);
785 else
786 SetName(creature->GetNameForLocaleIdx(objmgr.GetDBCLocaleIndex()));
788 if(cinfo->type == CREATURE_TYPE_BEAST)
790 SetUInt32Value(UNIT_FIELD_BYTES_0, 0x02020100);
791 SetSheath(SHEATH_STATE_MELEE);
792 SetByteValue(UNIT_FIELD_BYTES_2, 2, UNIT_RENAME_ALLOWED);
793 SetUInt32Value(UNIT_MOD_CAST_SPEED, creature->GetUInt32Value(UNIT_MOD_CAST_SPEED));
795 return true;
798 bool Pet::InitStatsForLevel(uint32 petlevel, Unit* owner)
800 CreatureInfo const *cinfo = GetCreatureInfo();
801 assert(cinfo);
803 if(!owner)
805 owner = GetOwner();
806 if(!owner)
808 sLog.outError("attempt to summon pet (Entry %u) without owner! Attempt terminated.", cinfo->Entry);
809 return false;
813 uint32 creature_ID = (getPetType() == HUNTER_PET) ? 1 : cinfo->Entry;
815 SetLevel(petlevel);
817 SetMeleeDamageSchool(SpellSchools(cinfo->dmgschool));
819 SetModifierValue(UNIT_MOD_ARMOR, BASE_VALUE, float(petlevel*50));
821 SetAttackTime(BASE_ATTACK, BASE_ATTACK_TIME);
822 SetAttackTime(OFF_ATTACK, BASE_ATTACK_TIME);
823 SetAttackTime(RANGED_ATTACK, BASE_ATTACK_TIME);
825 SetFloatValue(UNIT_MOD_CAST_SPEED, 1.0);
827 CreatureFamilyEntry const* cFamily = sCreatureFamilyStore.LookupEntry(cinfo->family);
828 if(cFamily && cFamily->minScale > 0.0f && getPetType()==HUNTER_PET)
830 float scale;
831 if (getLevel() >= cFamily->maxScaleLevel)
832 scale = cFamily->maxScale;
833 else if (getLevel() <= cFamily->minScaleLevel)
834 scale = cFamily->minScale;
835 else
836 scale = cFamily->minScale + float(getLevel() - cFamily->minScaleLevel) / cFamily->maxScaleLevel * (cFamily->maxScale - cFamily->minScale);
838 SetFloatValue(OBJECT_FIELD_SCALE_X, scale);
840 m_bonusdamage = 0;
842 int32 createResistance[MAX_SPELL_SCHOOL] = {0,0,0,0,0,0,0};
844 if(cinfo && getPetType() != HUNTER_PET)
846 createResistance[SPELL_SCHOOL_HOLY] = cinfo->resistance1;
847 createResistance[SPELL_SCHOOL_FIRE] = cinfo->resistance2;
848 createResistance[SPELL_SCHOOL_NATURE] = cinfo->resistance3;
849 createResistance[SPELL_SCHOOL_FROST] = cinfo->resistance4;
850 createResistance[SPELL_SCHOOL_SHADOW] = cinfo->resistance5;
851 createResistance[SPELL_SCHOOL_ARCANE] = cinfo->resistance6;
854 switch(getPetType())
856 case SUMMON_PET:
858 if(owner->GetTypeId() == TYPEID_PLAYER)
860 switch(owner->getClass())
862 case CLASS_WARLOCK:
865 //the damage bonus used for pets is either fire or shadow damage, whatever is higher
866 uint32 fire = owner->GetUInt32Value(PLAYER_FIELD_MOD_DAMAGE_DONE_POS + SPELL_SCHOOL_FIRE);
867 uint32 shadow = owner->GetUInt32Value(PLAYER_FIELD_MOD_DAMAGE_DONE_POS + SPELL_SCHOOL_SHADOW);
868 uint32 val = (fire > shadow) ? fire : shadow;
870 SetBonusDamage(int32 (val * 0.15f));
871 //bonusAP += val * 0.57;
872 break;
874 case CLASS_MAGE:
876 //40% damage bonus of mage's frost damage
877 float val = owner->GetUInt32Value(PLAYER_FIELD_MOD_DAMAGE_DONE_POS + SPELL_SCHOOL_FROST) * 0.4;
878 if(val < 0)
879 val = 0;
880 SetBonusDamage( int32(val));
881 break;
883 default:
884 break;
888 SetBaseWeaponDamage(BASE_ATTACK, MINDAMAGE, float(petlevel - (petlevel / 4)) );
889 SetBaseWeaponDamage(BASE_ATTACK, MAXDAMAGE, float(petlevel + (petlevel / 4)) );
891 //SetModifierValue(UNIT_MOD_ATTACK_POWER, BASE_VALUE, float(cinfo->attackpower));
893 PetLevelInfo const* pInfo = objmgr.GetPetLevelInfo(creature_ID, petlevel);
894 if(pInfo) // exist in DB
896 SetCreateHealth(pInfo->health);
897 SetCreateMana(pInfo->mana);
899 if(pInfo->armor > 0)
900 SetModifierValue(UNIT_MOD_ARMOR, BASE_VALUE, float(pInfo->armor));
902 for(int stat = 0; stat < MAX_STATS; ++stat)
904 SetCreateStat(Stats(stat), float(pInfo->stats[stat]));
907 else // not exist in DB, use some default fake data
909 sLog.outErrorDb("Summoned pet (Entry: %u) not have pet stats data in DB",cinfo->Entry);
911 // remove elite bonuses included in DB values
912 SetCreateHealth(uint32(((float(cinfo->maxhealth) / cinfo->maxlevel) / (1 + 2 * cinfo->rank)) * petlevel) );
913 SetCreateMana( uint32(((float(cinfo->maxmana) / cinfo->maxlevel) / (1 + 2 * cinfo->rank)) * petlevel) );
915 SetCreateStat(STAT_STRENGTH, 22);
916 SetCreateStat(STAT_AGILITY, 22);
917 SetCreateStat(STAT_STAMINA, 25);
918 SetCreateStat(STAT_INTELLECT, 28);
919 SetCreateStat(STAT_SPIRIT, 27);
921 break;
923 case HUNTER_PET:
925 SetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP, objmgr.GetXPForLevel(petlevel)/4);
926 //these formula may not be correct; however, it is designed to be close to what it should be
927 //this makes dps 0.5 of pets level
928 SetBaseWeaponDamage(BASE_ATTACK, MINDAMAGE, float(petlevel - (petlevel / 4)) );
929 //damage range is then petlevel / 2
930 SetBaseWeaponDamage(BASE_ATTACK, MAXDAMAGE, float(petlevel + (petlevel / 4)) );
931 //damage is increased afterwards as strength and pet scaling modify attack power
933 //stored standard pet stats are entry 1 in pet_levelinfo
934 PetLevelInfo const* pInfo = objmgr.GetPetLevelInfo(creature_ID, petlevel);
935 if(pInfo) // exist in DB
937 SetCreateHealth(pInfo->health);
938 SetModifierValue(UNIT_MOD_ARMOR, BASE_VALUE, float(pInfo->armor));
939 //SetModifierValue(UNIT_MOD_ATTACK_POWER, BASE_VALUE, float(cinfo->attackpower));
941 for( int i = STAT_STRENGTH; i < MAX_STATS; ++i)
943 SetCreateStat(Stats(i), float(pInfo->stats[i]));
946 else // not exist in DB, use some default fake data
948 sLog.outErrorDb("Hunter pet levelstats missing in DB");
950 // remove elite bonuses included in DB values
951 SetCreateHealth( uint32(((float(cinfo->maxhealth) / cinfo->maxlevel) / (1 + 2 * cinfo->rank)) * petlevel) );
953 SetCreateStat(STAT_STRENGTH, 22);
954 SetCreateStat(STAT_AGILITY, 22);
955 SetCreateStat(STAT_STAMINA, 25);
956 SetCreateStat(STAT_INTELLECT, 28);
957 SetCreateStat(STAT_SPIRIT, 27);
959 break;
961 case GUARDIAN_PET:
962 SetUInt32Value(UNIT_FIELD_PETEXPERIENCE, 0);
963 SetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP, 1000);
965 SetCreateMana(28 + 10*petlevel);
966 SetCreateHealth(28 + 30*petlevel);
968 // FIXME: this is wrong formula, possible each guardian pet have own damage formula
969 //these formula may not be correct; however, it is designed to be close to what it should be
970 //this makes dps 0.5 of pets level
971 SetBaseWeaponDamage(BASE_ATTACK, MINDAMAGE, float(petlevel - (petlevel / 4)));
972 //damage range is then petlevel / 2
973 SetBaseWeaponDamage(BASE_ATTACK, MAXDAMAGE, float(petlevel + (petlevel / 4)));
974 break;
975 default:
976 sLog.outError("Pet have incorrect type (%u) for levelup.", getPetType());
977 break;
980 for (int i = SPELL_SCHOOL_HOLY; i < MAX_SPELL_SCHOOL; ++i)
981 SetModifierValue(UnitMods(UNIT_MOD_RESISTANCE_START + i), BASE_VALUE, float(createResistance[i]));
983 UpdateAllStats();
985 SetHealth(GetMaxHealth());
986 SetPower(POWER_MANA, GetMaxPower(POWER_MANA));
988 return true;
991 bool Pet::HaveInDiet(ItemPrototype const* item) const
993 if (!item->FoodType)
994 return false;
996 CreatureInfo const* cInfo = GetCreatureInfo();
997 if(!cInfo)
998 return false;
1000 CreatureFamilyEntry const* cFamily = sCreatureFamilyStore.LookupEntry(cInfo->family);
1001 if(!cFamily)
1002 return false;
1004 uint32 diet = cFamily->petFoodMask;
1005 uint32 FoodMask = 1 << (item->FoodType-1);
1006 return diet & FoodMask;
1009 uint32 Pet::GetCurrentFoodBenefitLevel(uint32 itemlevel)
1011 // -5 or greater food level
1012 if(getLevel() <= itemlevel + 5) //possible to feed level 60 pet with level 55 level food for full effect
1013 return 35000;
1014 // -10..-6
1015 else if(getLevel() <= itemlevel + 10) //pure guess, but sounds good
1016 return 17000;
1017 // -14..-11
1018 else if(getLevel() <= itemlevel + 14) //level 55 food gets green on 70, makes sense to me
1019 return 8000;
1020 // -15 or less
1021 else
1022 return 0; //food too low level
1025 void Pet::_LoadSpellCooldowns()
1027 m_CreatureSpellCooldowns.clear();
1028 m_CreatureCategoryCooldowns.clear();
1030 QueryResult *result = CharacterDatabase.PQuery("SELECT spell,time FROM pet_spell_cooldown WHERE guid = '%u'",m_charmInfo->GetPetNumber());
1032 if(result)
1034 time_t curTime = time(NULL);
1036 WorldPacket data(SMSG_SPELL_COOLDOWN, (8+1+result->GetRowCount()*8));
1037 data << GetGUID();
1038 data << uint8(0x0); // flags (0x1, 0x2)
1042 Field *fields = result->Fetch();
1044 uint32 spell_id = fields[0].GetUInt32();
1045 time_t db_time = (time_t)fields[1].GetUInt64();
1047 if(!sSpellStore.LookupEntry(spell_id))
1049 sLog.outError("Pet %u have unknown spell %u in `pet_spell_cooldown`, skipping.",m_charmInfo->GetPetNumber(),spell_id);
1050 continue;
1053 // skip outdated cooldown
1054 if(db_time <= curTime)
1055 continue;
1057 data << uint32(spell_id);
1058 data << uint32(uint32(db_time-curTime)*IN_MILISECONDS);
1060 _AddCreatureSpellCooldown(spell_id,db_time);
1062 sLog.outDebug("Pet (Number: %u) spell %u cooldown loaded (%u secs).", m_charmInfo->GetPetNumber(), spell_id, uint32(db_time-curTime));
1064 while( result->NextRow() );
1066 delete result;
1068 if(!m_CreatureSpellCooldowns.empty() && GetOwner())
1070 ((Player*)GetOwner())->GetSession()->SendPacket(&data);
1075 void Pet::_SaveSpellCooldowns()
1077 CharacterDatabase.PExecute("DELETE FROM pet_spell_cooldown WHERE guid = '%u'", m_charmInfo->GetPetNumber());
1079 time_t curTime = time(NULL);
1081 // remove oudated and save active
1082 for(CreatureSpellCooldowns::iterator itr = m_CreatureSpellCooldowns.begin();itr != m_CreatureSpellCooldowns.end();)
1084 if(itr->second <= curTime)
1085 m_CreatureSpellCooldowns.erase(itr++);
1086 else
1088 CharacterDatabase.PExecute("INSERT INTO pet_spell_cooldown (guid,spell,time) VALUES ('%u', '%u', '" UI64FMTD "')", m_charmInfo->GetPetNumber(), itr->first, uint64(itr->second));
1089 ++itr;
1094 void Pet::_LoadSpells()
1096 QueryResult *result = CharacterDatabase.PQuery("SELECT spell,active FROM pet_spell WHERE guid = '%u'",m_charmInfo->GetPetNumber());
1098 if(result)
1102 Field *fields = result->Fetch();
1104 addSpell(fields[0].GetUInt32(), ActiveStates(fields[1].GetUInt8()), PETSPELL_UNCHANGED);
1106 while( result->NextRow() );
1108 delete result;
1112 void Pet::_SaveSpells()
1114 for (PetSpellMap::iterator itr = m_spells.begin(), next = m_spells.begin(); itr != m_spells.end(); itr = next)
1116 ++next;
1118 // prevent saving family passives to DB
1119 if (itr->second.type == PETSPELL_FAMILY)
1120 continue;
1122 switch(itr->second.state)
1124 case PETSPELL_REMOVED:
1125 CharacterDatabase.PExecute("DELETE FROM pet_spell WHERE guid = '%u' and spell = '%u'", m_charmInfo->GetPetNumber(), itr->first);
1126 m_spells.erase(itr);
1127 continue;
1128 case PETSPELL_CHANGED:
1129 CharacterDatabase.PExecute("DELETE FROM pet_spell WHERE guid = '%u' and spell = '%u'", m_charmInfo->GetPetNumber(), itr->first);
1130 CharacterDatabase.PExecute("INSERT INTO pet_spell (guid,spell,active) VALUES ('%u', '%u', '%u')", m_charmInfo->GetPetNumber(), itr->first, itr->second.active);
1131 break;
1132 case PETSPELL_NEW:
1133 CharacterDatabase.PExecute("INSERT INTO pet_spell (guid,spell,active) VALUES ('%u', '%u', '%u')", m_charmInfo->GetPetNumber(), itr->first, itr->second.active);
1134 break;
1135 case PETSPELL_UNCHANGED:
1136 continue;
1139 itr->second.state = PETSPELL_UNCHANGED;
1143 void Pet::_LoadAuras(uint32 timediff)
1145 m_Auras.clear();
1146 for (int i = 0; i < TOTAL_AURAS; ++i)
1147 m_modAuras[i].clear();
1149 QueryResult *result = CharacterDatabase.PQuery("SELECT caster_guid,spell,effect_index,stackcount,amount,maxduration,remaintime,remaincharges FROM pet_aura WHERE guid = '%u'",m_charmInfo->GetPetNumber());
1151 if(result)
1155 Field *fields = result->Fetch();
1156 uint64 caster_guid = fields[0].GetUInt64();
1157 uint32 spellid = fields[1].GetUInt32();
1158 uint32 effindex = fields[2].GetUInt32();
1159 uint32 stackcount= fields[3].GetUInt32();
1160 int32 damage = (int32)fields[4].GetUInt32();
1161 int32 maxduration = (int32)fields[5].GetUInt32();
1162 int32 remaintime = (int32)fields[6].GetUInt32();
1163 int32 remaincharges = (int32)fields[7].GetUInt32();
1165 SpellEntry const* spellproto = sSpellStore.LookupEntry(spellid);
1166 if(!spellproto)
1168 sLog.outError("Unknown aura (spellid %u, effindex %u), ignore.",spellid,effindex);
1169 continue;
1172 if(effindex >= 3)
1174 sLog.outError("Invalid effect index (spellid %u, effindex %u), ignore.",spellid,effindex);
1175 continue;
1178 // negative effects should continue counting down after logout
1179 if (remaintime != -1 && !IsPositiveEffect(spellid, effindex))
1181 if(remaintime <= int32(timediff))
1182 continue;
1184 remaintime -= timediff;
1187 // prevent wrong values of remaincharges
1188 if(spellproto->procCharges)
1190 if(remaincharges <= 0 || remaincharges > spellproto->procCharges)
1191 remaincharges = spellproto->procCharges;
1193 else
1194 remaincharges = -1;
1196 /// do not load single target auras (unless they were cast by the player)
1197 if (caster_guid != GetGUID() && IsSingleTargetSpell(spellproto))
1198 continue;
1200 for(uint32 i=0; i<stackcount; ++i)
1202 Aura* aura = CreateAura(spellproto, effindex, NULL, this, NULL);
1204 if(!damage)
1205 damage = aura->GetModifier()->m_amount;
1206 aura->SetLoadedState(caster_guid,damage,maxduration,remaintime,remaincharges);
1207 AddAura(aura);
1210 while( result->NextRow() );
1212 delete result;
1216 void Pet::_SaveAuras()
1218 CharacterDatabase.PExecute("DELETE FROM pet_aura WHERE guid = '%u'", m_charmInfo->GetPetNumber());
1220 AuraMap const& auras = GetAuras();
1221 if (auras.empty())
1222 return;
1224 spellEffectPair lastEffectPair = auras.begin()->first;
1225 uint32 stackCounter = 1;
1227 for(AuraMap::const_iterator itr = auras.begin(); ; ++itr)
1229 if(itr == auras.end() || lastEffectPair != itr->first)
1231 AuraMap::const_iterator itr2 = itr;
1232 // save previous spellEffectPair to db
1233 itr2--;
1234 SpellEntry const *spellInfo = itr2->second->GetSpellProto();
1235 /// do not save single target auras (unless they were cast by the player)
1236 if (!(itr2->second->GetCasterGUID() != GetGUID() && IsSingleTargetSpell(spellInfo)))
1238 if(!itr2->second->IsPassive())
1240 // skip all auras from spell that apply at cast SPELL_AURA_MOD_SHAPESHIFT or pet area auras.
1241 uint8 i;
1242 for (i = 0; i < 3; ++i)
1243 if (spellInfo->EffectApplyAuraName[i] == SPELL_AURA_MOD_STEALTH ||
1244 spellInfo->Effect[i] == SPELL_EFFECT_APPLY_AREA_AURA_OWNER ||
1245 spellInfo->Effect[i] == SPELL_EFFECT_APPLY_AREA_AURA_PET )
1246 break;
1248 if (i == 3)
1250 CharacterDatabase.PExecute("INSERT INTO pet_aura (guid,caster_guid,spell,effect_index,stackcount,amount,maxduration,remaintime,remaincharges) "
1251 "VALUES ('%u', '" UI64FMTD "', '%u', '%u', '%u', '%d', '%d', '%d', '%d')",
1252 m_charmInfo->GetPetNumber(), itr2->second->GetCasterGUID(),(uint32)itr2->second->GetId(), (uint32)itr2->second->GetEffIndex(), stackCounter, itr2->second->GetModifier()->m_amount,int(itr2->second->GetAuraMaxDuration()),int(itr2->second->GetAuraDuration()),int(itr2->second->GetAuraCharges()));
1256 if(itr == auras.end())
1257 break;
1260 if (lastEffectPair == itr->first)
1261 stackCounter++;
1262 else
1264 lastEffectPair = itr->first;
1265 stackCounter = 1;
1270 bool Pet::addSpell(uint32 spell_id,ActiveStates active /*= ACT_DECIDE*/, PetSpellState state /*= PETSPELL_NEW*/, PetSpellType type /*= PETSPELL_NORMAL*/)
1272 SpellEntry const *spellInfo = sSpellStore.LookupEntry(spell_id);
1273 if (!spellInfo)
1275 // do pet spell book cleanup
1276 if(state == PETSPELL_UNCHANGED) // spell load case
1278 sLog.outError("Pet::addSpell: Non-existed in SpellStore spell #%u request, deleting for all pets in `pet_spell`.",spell_id);
1279 CharacterDatabase.PExecute("DELETE FROM pet_spell WHERE spell = '%u'",spell_id);
1281 else
1282 sLog.outError("Pet::addSpell: Non-existed in SpellStore spell #%u request.",spell_id);
1284 return false;
1287 PetSpellMap::iterator itr = m_spells.find(spell_id);
1288 if (itr != m_spells.end())
1290 if (itr->second.state == PETSPELL_REMOVED)
1292 m_spells.erase(itr);
1293 state = PETSPELL_CHANGED;
1295 else if (state == PETSPELL_UNCHANGED && itr->second.state != PETSPELL_UNCHANGED)
1297 // can be in case spell loading but learned at some previous spell loading
1298 itr->second.state = PETSPELL_UNCHANGED;
1300 if(active == ACT_ENABLED)
1301 ToggleAutocast(spell_id, true);
1302 else if(active == ACT_DISABLED)
1303 ToggleAutocast(spell_id, false);
1305 return false;
1307 else
1308 return false;
1311 uint32 oldspell_id = 0;
1313 PetSpell newspell;
1314 newspell.state = state;
1315 newspell.type = type;
1317 if(active == ACT_DECIDE) //active was not used before, so we save it's autocast/passive state here
1319 if(IsPassiveSpell(spell_id))
1320 newspell.active = ACT_PASSIVE;
1321 else
1322 newspell.active = ACT_DISABLED;
1324 else
1325 newspell.active = active;
1327 // talent: unlearn all other talent ranks (high and low)
1328 if(TalentSpellPos const* talentPos = GetTalentSpellPos(spell_id))
1330 if(TalentEntry const *talentInfo = sTalentStore.LookupEntry( talentPos->talent_id ))
1332 for(int i=0; i < MAX_TALENT_RANK; ++i)
1334 // skip learning spell and no rank spell case
1335 uint32 rankSpellId = talentInfo->RankID[i];
1336 if(!rankSpellId || rankSpellId==spell_id)
1337 continue;
1339 // skip unknown ranks
1340 if(!HasSpell(rankSpellId))
1341 continue;
1342 removeSpell(rankSpellId,false,false);
1346 else if(spellmgr.GetSpellRank(spell_id)!=0)
1348 for (PetSpellMap::const_iterator itr2 = m_spells.begin(); itr2 != m_spells.end(); ++itr2)
1350 if(itr2->second.state == PETSPELL_REMOVED) continue;
1352 if( spellmgr.IsRankSpellDueToSpell(spellInfo,itr2->first) )
1354 // replace by new high rank
1355 if(spellmgr.IsHighRankOfSpell(spell_id,itr2->first))
1357 newspell.active = itr2->second.active;
1359 if(newspell.active == ACT_ENABLED)
1360 ToggleAutocast(itr2->first, false);
1362 oldspell_id = itr2->first;
1363 unlearnSpell(itr2->first,false,false);
1364 break;
1366 // ignore new lesser rank
1367 else if(spellmgr.IsHighRankOfSpell(itr2->first,spell_id))
1368 return false;
1373 m_spells[spell_id] = newspell;
1375 if (IsPassiveSpell(spell_id))
1376 CastSpell(this, spell_id, true);
1377 else
1378 m_charmInfo->AddSpellToActionBar(spell_id);
1380 if(newspell.active == ACT_ENABLED)
1381 ToggleAutocast(spell_id, true);
1383 uint32 talentCost = GetTalentSpellCost(spell_id);
1384 if (talentCost)
1386 int32 free_points = GetMaxTalentPointsForLevel(getLevel());
1387 m_usedTalentCount+=talentCost;
1388 // update free talent points
1389 free_points-=m_usedTalentCount;
1390 SetFreeTalentPoints(free_points > 0 ? free_points : 0);
1392 return true;
1395 bool Pet::learnSpell(uint32 spell_id)
1397 // prevent duplicated entires in spell book
1398 if (!addSpell(spell_id))
1399 return false;
1401 if(!m_loading)
1403 Unit* owner = GetOwner();
1404 if(owner && owner->GetTypeId() == TYPEID_PLAYER)
1406 WorldPacket data(SMSG_PET_LEARNED_SPELL, 4);
1407 data << uint32(spell_id);
1408 ((Player*)owner)->GetSession()->SendPacket(&data);
1410 ((Player*)owner)->PetSpellInitialize();
1413 return true;
1416 void Pet::InitLevelupSpellsForLevel()
1418 uint32 level = getLevel();
1420 if(PetLevelupSpellSet const *levelupSpells = GetCreatureInfo()->family ? spellmgr.GetPetLevelupSpellList(GetCreatureInfo()->family) : NULL)
1422 // PetLevelupSpellSet ordered by levels, process in reversed order
1423 for(PetLevelupSpellSet::const_reverse_iterator itr = levelupSpells->rbegin(); itr != levelupSpells->rend(); ++itr)
1425 // will called first if level down
1426 if(itr->first > level)
1427 unlearnSpell(itr->second,true); // will learn prev rank if any
1428 // will called if level up
1429 else
1430 learnSpell(itr->second); // will unlearn prev rank if any
1434 int32 petSpellsId = GetCreatureInfo()->PetSpellDataId ? -(int32)GetCreatureInfo()->PetSpellDataId : GetEntry();
1436 // default spells (can be not learned if pet level (as owner level decrease result for example) less first possible in normal game)
1437 if(PetDefaultSpellsEntry const *defSpells = spellmgr.GetPetDefaultSpellsEntry(petSpellsId))
1439 for(int i = 0; i < MAX_CREATURE_SPELL_DATA_SLOT; ++i)
1441 SpellEntry const* spellEntry = sSpellStore.LookupEntry(defSpells->spellid[i]);
1442 if(!spellEntry)
1443 continue;
1445 // will called first if level down
1446 if(spellEntry->spellLevel > level)
1447 unlearnSpell(spellEntry->Id,true);
1448 // will called if level up
1449 else
1450 learnSpell(spellEntry->Id);
1455 bool Pet::unlearnSpell(uint32 spell_id, bool learn_prev, bool clear_ab)
1457 if(removeSpell(spell_id,learn_prev,clear_ab))
1459 if(!m_loading)
1461 if (Unit* owner = GetOwner())
1463 if(owner->GetTypeId() == TYPEID_PLAYER)
1465 WorldPacket data(SMSG_PET_REMOVED_SPELL, 4);
1466 data << uint32(spell_id);
1467 ((Player*)owner)->GetSession()->SendPacket(&data);
1471 return true;
1473 return false;
1476 bool Pet::removeSpell(uint32 spell_id, bool learn_prev, bool clear_ab)
1478 PetSpellMap::iterator itr = m_spells.find(spell_id);
1479 if (itr == m_spells.end())
1480 return false;
1482 if(itr->second.state == PETSPELL_REMOVED)
1483 return false;
1485 if(itr->second.state == PETSPELL_NEW)
1486 m_spells.erase(itr);
1487 else
1488 itr->second.state = PETSPELL_REMOVED;
1490 RemoveAurasDueToSpell(spell_id);
1492 uint32 talentCost = GetTalentSpellCost(spell_id);
1493 if (talentCost > 0)
1495 if (m_usedTalentCount > talentCost)
1496 m_usedTalentCount-=talentCost;
1497 else
1498 m_usedTalentCount = 0;
1499 // update free talent points
1500 int32 free_points = GetMaxTalentPointsForLevel(getLevel()) - m_usedTalentCount;
1501 SetFreeTalentPoints(free_points > 0 ? free_points : 0);
1504 if (learn_prev)
1506 if (uint32 prev_id = spellmgr.GetPrevSpellInChain (spell_id))
1507 learnSpell(prev_id);
1508 else
1509 learn_prev = false;
1512 // if remove last rank or non-ranked then update action bar at server and client if need
1513 if (clear_ab && !learn_prev && m_charmInfo->RemoveSpellFromActionBar(spell_id))
1515 if(!m_loading)
1517 // need update action bar for last removed rank
1518 if (Unit* owner = GetOwner())
1519 if (owner->GetTypeId() == TYPEID_PLAYER)
1520 ((Player*)owner)->PetSpellInitialize();
1524 return true;
1528 void Pet::CleanupActionBar()
1530 for(int i = 0; i < MAX_UNIT_ACTION_BAR_INDEX; ++i)
1531 if(UnitActionBarEntry const* ab = m_charmInfo->GetActionBarEntry(i))
1532 if(uint32 action = ab->GetAction())
1533 if(ab->IsActionBarForSpell() && !HasSpell(action))
1534 m_charmInfo->SetActionBar(i,0,ACT_DISABLED);
1537 void Pet::InitPetCreateSpells()
1539 m_charmInfo->InitPetActionBar();
1540 m_spells.clear();
1542 LearnPetPassives();
1544 CastPetAuras(false);
1547 bool Pet::resetTalents(bool no_cost)
1549 Unit *owner = GetOwner();
1550 if (!owner || owner->GetTypeId()!=TYPEID_PLAYER)
1551 return false;
1553 // not need after this call
1554 if(((Player*)owner)->HasAtLoginFlag(AT_LOGIN_RESET_PET_TALENTS))
1555 ((Player*)owner)->RemoveAtLoginFlag(AT_LOGIN_RESET_PET_TALENTS,true);
1557 CreatureInfo const * ci = GetCreatureInfo();
1558 if(!ci)
1559 return false;
1560 // Check pet talent type
1561 CreatureFamilyEntry const *pet_family = sCreatureFamilyStore.LookupEntry(ci->family);
1562 if(!pet_family || pet_family->petTalentType < 0)
1563 return false;
1565 Player *player = (Player *)owner;
1567 uint32 level = getLevel();
1568 uint32 talentPointsForLevel = GetMaxTalentPointsForLevel(level);
1570 if (m_usedTalentCount == 0)
1572 SetFreeTalentPoints(talentPointsForLevel);
1573 return false;
1576 uint32 cost = 0;
1578 if(!no_cost)
1580 cost = resetTalentsCost();
1582 if (player->GetMoney() < cost)
1584 player->SendBuyError( BUY_ERR_NOT_ENOUGHT_MONEY, 0, 0, 0);
1585 return false;
1589 for (unsigned int i = 0; i < sTalentStore.GetNumRows(); ++i)
1591 TalentEntry const *talentInfo = sTalentStore.LookupEntry(i);
1593 if (!talentInfo) continue;
1595 TalentTabEntry const *talentTabInfo = sTalentTabStore.LookupEntry( talentInfo->TalentTab );
1597 if(!talentTabInfo)
1598 continue;
1600 // unlearn only talents for pets family talent type
1601 if(!((1 << pet_family->petTalentType) & talentTabInfo->petTalentMask))
1602 continue;
1604 for (int j = 0; j < MAX_TALENT_RANK; j++)
1606 for(PetSpellMap::const_iterator itr = m_spells.begin(); itr != m_spells.end();)
1608 if(itr->second.state == PETSPELL_REMOVED)
1610 ++itr;
1611 continue;
1613 // remove learned spells (all ranks)
1614 uint32 itrFirstId = spellmgr.GetFirstSpellInChain(itr->first);
1616 // unlearn if first rank is talent or learned by talent
1617 if (itrFirstId == talentInfo->RankID[j] || spellmgr.IsSpellLearnToSpell(talentInfo->RankID[j],itrFirstId))
1619 removeSpell(itr->first,false);
1620 itr = m_spells.begin();
1621 continue;
1623 else
1624 ++itr;
1629 SetFreeTalentPoints(talentPointsForLevel);
1631 if(!no_cost)
1633 player->ModifyMoney(-(int32)cost);
1635 m_resetTalentsCost = cost;
1636 m_resetTalentsTime = time(NULL);
1638 player->PetSpellInitialize();
1639 return true;
1642 void Pet::resetTalentsForAllPetsOf(Player* owner, Pet* online_pet /*= NULL*/)
1644 // not need after this call
1645 if(((Player*)owner)->HasAtLoginFlag(AT_LOGIN_RESET_PET_TALENTS))
1646 ((Player*)owner)->RemoveAtLoginFlag(AT_LOGIN_RESET_PET_TALENTS,true);
1648 // reset for online
1649 if(online_pet)
1650 online_pet->resetTalents(true);
1652 // now need only reset for offline pets (all pets except online case)
1653 uint32 except_petnumber = online_pet ? online_pet->GetCharmInfo()->GetPetNumber() : 0;
1655 QueryResult *resultPets = CharacterDatabase.PQuery(
1656 "SELECT id FROM character_pet WHERE owner = '%u' AND id <> '%u'",
1657 owner->GetGUIDLow(),except_petnumber);
1659 // no offline pets
1660 if(!resultPets)
1661 return;
1663 QueryResult *result = CharacterDatabase.PQuery(
1664 "SELECT DISTINCT pet_spell.spell FROM pet_spell, character_pet "
1665 "WHERE character_pet.owner = '%u' AND character_pet.id = pet_spell.guid AND character_pet.id <> %u",
1666 owner->GetGUIDLow(),except_petnumber);
1668 if(!result)
1670 delete resultPets;
1671 return;
1674 bool need_comma = false;
1675 std::ostringstream ss;
1676 ss << "DELETE FROM pet_spell WHERE guid IN (";
1680 Field *fields = resultPets->Fetch();
1682 uint32 id = fields[0].GetUInt32();
1684 if(need_comma)
1685 ss << ",";
1687 ss << id;
1689 need_comma = true;
1691 while( resultPets->NextRow() );
1693 delete resultPets;
1695 ss << ") AND spell IN (";
1697 bool need_execute = false;
1700 Field *fields = result->Fetch();
1702 uint32 spell = fields[0].GetUInt32();
1704 if(!GetTalentSpellCost(spell))
1705 continue;
1707 if(need_execute)
1708 ss << ",";
1710 ss << spell;
1712 need_execute = true;
1714 while( result->NextRow() );
1716 delete result;
1718 if(!need_execute)
1719 return;
1721 ss << ")";
1723 CharacterDatabase.Execute(ss.str().c_str());
1726 void Pet::InitTalentForLevel()
1728 uint32 level = getLevel();
1729 uint32 talentPointsForLevel = GetMaxTalentPointsForLevel(level);
1730 // Reset talents in case low level (on level down) or wrong points for level (hunter can unlearn TP increase talent)
1731 if(talentPointsForLevel == 0 || m_usedTalentCount > talentPointsForLevel)
1733 // Remove all talent points
1734 resetTalents(true);
1736 SetFreeTalentPoints(talentPointsForLevel - m_usedTalentCount);
1738 Unit *owner = GetOwner();
1739 if (!owner || owner->GetTypeId() != TYPEID_PLAYER)
1740 return;
1742 if(!m_loading)
1743 ((Player*)owner)->SendTalentsInfoData(true);
1746 uint32 Pet::resetTalentsCost() const
1748 uint32 days = (sWorld.GetGameTime() - m_resetTalentsTime)/DAY;
1750 // The first time reset costs 10 silver; after 1 day cost is reset to 10 silver
1751 if(m_resetTalentsCost < 10*SILVER || days > 0)
1752 return 10*SILVER;
1753 // then 50 silver
1754 else if(m_resetTalentsCost < 50*SILVER)
1755 return 50*SILVER;
1756 // then 1 gold
1757 else if(m_resetTalentsCost < 1*GOLD)
1758 return 1*GOLD;
1759 // then increasing at a rate of 1 gold; cap 10 gold
1760 else
1761 return (m_resetTalentsCost + 1*GOLD > 10*GOLD ? 10*GOLD : m_resetTalentsCost + 1*GOLD);
1764 uint8 Pet::GetMaxTalentPointsForLevel(uint32 level)
1766 uint8 points = (level >= 20) ? ((level - 16) / 4) : 0;
1767 // Mod points from owner SPELL_AURA_MOD_PET_TALENT_POINTS
1768 if (Unit *owner = GetOwner())
1769 points+=owner->GetTotalAuraModifier(SPELL_AURA_MOD_PET_TALENT_POINTS);
1770 return points;
1773 void Pet::ToggleAutocast(uint32 spellid, bool apply)
1775 if(IsPassiveSpell(spellid))
1776 return;
1778 PetSpellMap::iterator itr = m_spells.find(spellid);
1780 int i;
1782 if(apply)
1784 for (i = 0; i < m_autospells.size() && m_autospells[i] != spellid; ++i)
1785 ; // just search
1787 if (i == m_autospells.size())
1789 m_autospells.push_back(spellid);
1791 if(itr->second.active != ACT_ENABLED)
1793 itr->second.active = ACT_ENABLED;
1794 if(itr->second.state != PETSPELL_NEW)
1795 itr->second.state = PETSPELL_CHANGED;
1799 else
1801 AutoSpellList::iterator itr2 = m_autospells.begin();
1802 for (i = 0; i < m_autospells.size() && m_autospells[i] != spellid; ++i, itr2++)
1803 ; // just search
1805 if (i < m_autospells.size())
1807 m_autospells.erase(itr2);
1808 if(itr->second.active != ACT_DISABLED)
1810 itr->second.active = ACT_DISABLED;
1811 if(itr->second.state != PETSPELL_NEW)
1812 itr->second.state = PETSPELL_CHANGED;
1818 bool Pet::IsPermanentPetFor(Player* owner)
1820 switch(getPetType())
1822 case SUMMON_PET:
1823 switch(owner->getClass())
1825 case CLASS_WARLOCK:
1826 return GetCreatureInfo()->type == CREATURE_TYPE_DEMON;
1827 case CLASS_DEATH_KNIGHT:
1828 return GetCreatureInfo()->type == CREATURE_TYPE_UNDEAD;
1829 default:
1830 return false;
1832 case HUNTER_PET:
1833 return true;
1834 default:
1835 return false;
1839 bool Pet::Create(uint32 guidlow, Map *map, uint32 phaseMask, uint32 Entry, uint32 pet_number)
1841 SetMap(map);
1842 SetPhaseMask(phaseMask,false);
1844 Object::_Create(guidlow, pet_number, HIGHGUID_PET);
1846 m_DBTableGuid = guidlow;
1847 m_originalEntry = Entry;
1849 if(!InitEntry(Entry))
1850 return false;
1852 SetSheath(SHEATH_STATE_MELEE);
1854 if(getPetType() == MINI_PET) // always non-attackable
1855 SetFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_NON_ATTACKABLE);
1857 return true;
1860 bool Pet::HasSpell(uint32 spell) const
1862 PetSpellMap::const_iterator itr = m_spells.find(spell);
1863 return (itr != m_spells.end() && itr->second.state != PETSPELL_REMOVED );
1866 // Get all passive spells in our skill line
1867 void Pet::LearnPetPassives()
1869 CreatureInfo const* cInfo = GetCreatureInfo();
1870 if(!cInfo)
1871 return;
1873 CreatureFamilyEntry const* cFamily = sCreatureFamilyStore.LookupEntry(cInfo->family);
1874 if(!cFamily)
1875 return;
1877 PetFamilySpellsStore::const_iterator petStore = sPetFamilySpellsStore.find(cFamily->ID);
1878 if(petStore != sPetFamilySpellsStore.end())
1880 for(PetFamilySpellsSet::const_iterator petSet = petStore->second.begin(); petSet != petStore->second.end(); ++petSet)
1881 addSpell(*petSet, ACT_DECIDE, PETSPELL_NEW, PETSPELL_FAMILY);
1885 void Pet::CastPetAuras(bool current)
1887 Unit* owner = GetOwner();
1888 if(!owner || owner->GetTypeId()!=TYPEID_PLAYER)
1889 return;
1891 if(!IsPermanentPetFor((Player*)owner))
1892 return;
1894 for(PetAuraSet::const_iterator itr = owner->m_petAuras.begin(); itr != owner->m_petAuras.end();)
1896 PetAura const* pa = *itr;
1897 ++itr;
1899 if(!current && pa->IsRemovedOnChangePet())
1900 owner->RemovePetAura(pa);
1901 else
1902 CastPetAura(pa);
1906 void Pet::CastPetAura(PetAura const* aura)
1908 uint32 auraId = aura->GetAura(GetEntry());
1909 if(!auraId)
1910 return;
1912 if(auraId == 35696) // Demonic Knowledge
1914 int32 basePoints = int32(aura->GetDamage() * (GetStat(STAT_STAMINA) + GetStat(STAT_INTELLECT)) / 100);
1915 CastCustomSpell(this, auraId, &basePoints, NULL, NULL, true);
1917 else
1918 CastSpell(this, auraId, true);
1921 struct DoPetLearnSpell
1923 DoPetLearnSpell(Pet& _pet) : pet(_pet) {}
1924 void operator() (uint32 spell_id) { pet.learnSpell(spell_id); }
1925 Pet& pet;
1928 void Pet::learnSpellHighRank(uint32 spellid)
1930 learnSpell(spellid);
1932 DoPetLearnSpell worker(*this);
1933 spellmgr.doForHighRanks(spellid,worker);
1936 void Pet::SynchronizeLevelWithOwner()
1938 Unit* owner = GetOwner();
1939 if (!owner || owner->GetTypeId() != TYPEID_PLAYER)
1940 return;
1942 switch(getPetType())
1944 // always same level
1945 case SUMMON_PET:
1946 GivePetLevel(owner->getLevel());
1947 break;
1948 // can't be greater owner level
1949 case HUNTER_PET:
1950 if(getLevel() > owner->getLevel())
1952 GivePetLevel(owner->getLevel());
1953 SetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP, objmgr.GetXPForLevel(owner->getLevel())/4);
1954 SetUInt32Value(UNIT_FIELD_PETEXPERIENCE, GetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP)-1);
1956 break;
1957 default:
1958 break;