[9192] Fixed typo in error output for lock id gameobject template data check.
[getmangos.git] / src / game / Pet.cpp
blob5283be35257a0064e6fa184b1391d2072c0c4b23
1 /*
2 * Copyright (C) 2005-2010 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), m_petModeFlags(PET_MODE_DEFAULT)
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 delete m_declinedname;
63 void Pet::AddToWorld()
65 ///- Register the pet for guid lookup
66 if(!IsInWorld())
67 GetMap()->GetObjectsStore().insert<Pet>(GetGUID(), (Pet*)this);
69 Unit::AddToWorld();
72 void Pet::RemoveFromWorld()
74 ///- Remove the pet from the accessor
75 if(IsInWorld())
76 GetMap()->GetObjectsStore().erase<Pet>(GetGUID(), (Pet*)NULL);
78 ///- Don't call the function for Creature, normal mobs + totems go in a different storage
79 Unit::RemoveFromWorld();
82 bool Pet::LoadPetFromDB( Player* owner, uint32 petentry, uint32 petnumber, bool current )
84 m_loading = true;
86 uint32 ownerid = owner->GetGUIDLow();
88 QueryResult *result;
90 if (petnumber)
91 // known petnumber entry 0 1 2(?) 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
92 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 "
93 "FROM character_pet WHERE owner = '%u' AND id = '%u'",
94 ownerid, petnumber);
95 else if (current)
96 // current pet (slot 0) 0 1 2(?) 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
97 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 "
98 "FROM character_pet WHERE owner = '%u' AND slot = '%u'",
99 ownerid, PET_SAVE_AS_CURRENT );
100 else if (petentry)
101 // known petentry entry (unique for summoned pet, but non unique for hunter pet (only from current or not stabled pets)
102 // 0 1 2(?) 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
103 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 "
104 "FROM character_pet WHERE owner = '%u' AND entry = '%u' AND (slot = '%u' OR slot > '%u') ",
105 ownerid, petentry,PET_SAVE_AS_CURRENT,PET_SAVE_LAST_STABLE_SLOT);
106 else
107 // any current or other non-stabled pet (for hunter "call pet")
108 // 0 1 2(?) 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
109 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 "
110 "FROM character_pet WHERE owner = '%u' AND (slot = '%u' OR slot > '%u') ",
111 ownerid,PET_SAVE_AS_CURRENT,PET_SAVE_LAST_STABLE_SLOT);
113 if(!result)
114 return false;
116 Field *fields = result->Fetch();
118 // update for case of current pet "slot = 0"
119 petentry = fields[1].GetUInt32();
120 if (!petentry)
122 delete result;
123 return false;
126 uint32 summon_spell_id = fields[17].GetUInt32();
127 SpellEntry const* spellInfo = sSpellStore.LookupEntry(summon_spell_id);
129 bool is_temporary_summoned = spellInfo && GetSpellDuration(spellInfo) > 0;
131 // check temporary summoned pets like mage water elemental
132 if (current && is_temporary_summoned)
134 delete result;
135 return false;
138 PetType pet_type = PetType(fields[18].GetUInt8());
139 if(pet_type == HUNTER_PET)
141 CreatureInfo const* creatureInfo = ObjectMgr::GetCreatureTemplate(petentry);
142 if(!creatureInfo || !creatureInfo->isTameable(owner->CanTameExoticPets()))
144 delete result;
145 return false;
149 uint32 pet_number = fields[0].GetUInt32();
151 if (current && owner->IsPetNeedBeTemporaryUnsummoned())
153 owner->SetTemporaryUnsummonedPetNumber(pet_number);
154 delete result;
155 return false;
158 Map *map = owner->GetMap();
159 uint32 guid = map->GenerateLocalLowGuid(HIGHGUID_PET);
160 if (!Create(guid, map, owner->GetPhaseMask(), petentry, pet_number))
162 delete result;
163 return false;
166 float px, py, pz;
167 owner->GetClosePoint(px, py, pz, GetObjectSize(), PET_FOLLOW_DIST, PET_FOLLOW_ANGLE);
169 Relocate(px, py, pz, owner->GetOrientation());
171 if (!IsPositionValid())
173 sLog.outError("Pet (guidlow %d, entry %d) not loaded. Suggested coordinates isn't valid (X: %f Y: %f)",
174 GetGUIDLow(), GetEntry(), GetPositionX(), GetPositionY());
175 delete result;
176 return false;
179 setPetType(pet_type);
180 setFaction(owner->getFaction());
181 SetUInt32Value(UNIT_CREATED_BY_SPELL, summon_spell_id);
183 CreatureInfo const *cinfo = GetCreatureInfo();
184 if (cinfo->type == CREATURE_TYPE_CRITTER)
186 AIM_Initialize();
187 map->Add((Creature*)this);
188 delete result;
189 return true;
192 m_charmInfo->SetPetNumber(pet_number, IsPermanentPetFor(owner));
194 SetOwnerGUID(owner->GetGUID());
195 SetDisplayId(fields[3].GetUInt32());
196 SetNativeDisplayId(fields[3].GetUInt32());
197 uint32 petlevel = fields[4].GetUInt32();
198 SetUInt32Value(UNIT_NPC_FLAGS, UNIT_NPC_FLAG_NONE);
199 SetName(fields[8].GetString());
201 switch (getPetType())
203 case SUMMON_PET:
204 petlevel=owner->getLevel();
206 SetUInt32Value(UNIT_FIELD_BYTES_0, 2048);
207 SetUInt32Value(UNIT_FIELD_FLAGS, UNIT_FLAG_PVP_ATTACKABLE);
208 // this enables popup window (pet dismiss, cancel)
209 break;
210 case HUNTER_PET:
211 SetUInt32Value(UNIT_FIELD_BYTES_0, 0x02020100);
212 SetSheath(SHEATH_STATE_MELEE);
213 SetByteFlag(UNIT_FIELD_BYTES_2, 2, fields[9].GetBool() ? UNIT_CAN_BE_ABANDONED : UNIT_CAN_BE_RENAMED | UNIT_CAN_BE_ABANDONED);
215 SetUInt32Value(UNIT_FIELD_FLAGS, UNIT_FLAG_PVP_ATTACKABLE);
216 // this enables popup window (pet abandon, cancel)
217 SetMaxPower(POWER_HAPPINESS, GetCreatePowers(POWER_HAPPINESS));
218 SetPower(POWER_HAPPINESS, fields[12].GetUInt32());
219 setPowerType(POWER_FOCUS);
220 break;
221 default:
222 sLog.outError("Pet have incorrect type (%u) for pet loading.", getPetType());
225 if(owner->IsPvP())
226 SetPvP(true);
228 if(owner->IsFFAPvP())
229 SetFFAPvP(true);
231 SetCanModifyStats(true);
232 InitStatsForLevel(petlevel);
233 InitTalentForLevel(); // set original talents points before spell loading
235 SetUInt32Value(UNIT_FIELD_PET_NAME_TIMESTAMP, time(NULL));
236 SetUInt32Value(UNIT_FIELD_PETEXPERIENCE, fields[5].GetUInt32());
237 SetCreatorGUID(owner->GetGUID());
239 m_charmInfo->SetReactState(ReactStates(fields[6].GetUInt8()));
241 uint32 savedhealth = fields[10].GetUInt32();
242 uint32 savedmana = fields[11].GetUInt32();
244 // set current pet as current
245 // 0=current
246 // 1..MAX_PET_STABLES in stable slot
247 // PET_SAVE_NOT_IN_SLOT(100) = not stable slot (summoning))
248 if (fields[7].GetUInt32() != 0)
250 CharacterDatabase.BeginTransaction();
251 CharacterDatabase.PExecute("UPDATE character_pet SET slot = '%u' WHERE owner = '%u' AND slot = '%u' AND id <> '%u'",
252 PET_SAVE_NOT_IN_SLOT, ownerid, PET_SAVE_AS_CURRENT, m_charmInfo->GetPetNumber());
253 CharacterDatabase.PExecute("UPDATE character_pet SET slot = '%u' WHERE owner = '%u' AND id = '%u'",
254 PET_SAVE_AS_CURRENT, ownerid, m_charmInfo->GetPetNumber());
255 CharacterDatabase.CommitTransaction();
258 // load action bar, if data broken will fill later by default spells.
259 if (!is_temporary_summoned)
260 m_charmInfo->LoadPetActionBar(fields[13].GetCppString());
262 // since last save (in seconds)
263 uint32 timediff = (time(NULL) - fields[14].GetUInt32());
265 m_resetTalentsCost = fields[15].GetUInt32();
266 m_resetTalentsTime = fields[16].GetUInt64();
268 delete result;
270 //load spells/cooldowns/auras
271 _LoadAuras(timediff);
273 //init AB
274 if (is_temporary_summoned)
276 // Temporary summoned pets always have initial spell list at load
277 InitPetCreateSpells();
279 else
281 LearnPetPassives();
282 CastPetAuras(current);
285 if (getPetType() == SUMMON_PET && !current) //all (?) summon pets come with full health when called, but not when they are current
287 SetHealth(GetMaxHealth());
288 SetPower(POWER_MANA, GetMaxPower(POWER_MANA));
290 else
292 SetHealth(savedhealth > GetMaxHealth() ? GetMaxHealth() : savedhealth);
293 SetPower(POWER_MANA, savedmana > GetMaxPower(POWER_MANA) ? GetMaxPower(POWER_MANA) : savedmana);
296 AIM_Initialize();
297 map->Add((Creature*)this);
299 // Spells should be loaded after pet is added to map, because in CheckCast is check on it
300 _LoadSpells();
301 InitLevelupSpellsForLevel();
303 CleanupActionBar(); // remove unknown spells from action bar after load
305 _LoadSpellCooldowns();
307 owner->SetPet(this); // in DB stored only full controlled creature
308 sLog.outDebug("New Pet has guid %u", GetGUIDLow());
310 if (owner->GetTypeId() == TYPEID_PLAYER)
312 ((Player*)owner)->PetSpellInitialize();
313 if(((Player*)owner)->GetGroup())
314 ((Player*)owner)->SetGroupUpdateFlag(GROUP_UPDATE_PET);
316 ((Player*)owner)->SendTalentsInfoData(true);
319 if (owner->GetTypeId() == TYPEID_PLAYER && getPetType() == HUNTER_PET)
321 result = CharacterDatabase.PQuery("SELECT genitive, dative, accusative, instrumental, prepositional FROM character_pet_declinedname WHERE owner = '%u' AND id = '%u'", owner->GetGUIDLow(), GetCharmInfo()->GetPetNumber());
323 if(result)
325 if(m_declinedname)
326 delete m_declinedname;
328 m_declinedname = new DeclinedName;
329 Field *fields2 = result->Fetch();
330 for(int i = 0; i < MAX_DECLINED_NAME_CASES; ++i)
331 m_declinedname->name[i] = fields2[i].GetCppString();
333 delete result;
337 m_loading = false;
339 SynchronizeLevelWithOwner();
340 return true;
343 void Pet::SavePetToDB(PetSaveMode mode)
345 if (!GetEntry())
346 return;
348 // save only fully controlled creature
349 if (!isControlled())
350 return;
352 // not save not player pets
353 if(!IS_PLAYER_GUID(GetOwnerGUID()))
354 return;
356 Player* pOwner = (Player*)GetOwner();
357 if (!pOwner)
358 return;
360 // current/stable/not_in_slot
361 if (mode >= PET_SAVE_AS_CURRENT)
363 // not save pet as current if another pet temporary unsummoned
364 if (mode == PET_SAVE_AS_CURRENT && pOwner->GetTemporaryUnsummonedPetNumber() &&
365 pOwner->GetTemporaryUnsummonedPetNumber() != m_charmInfo->GetPetNumber())
367 // pet will lost anyway at restore temporary unsummoned
368 if(getPetType()==HUNTER_PET)
369 return;
371 // for warlock case
372 mode = PET_SAVE_NOT_IN_SLOT;
375 uint32 curhealth = GetHealth();
376 uint32 curmana = GetPower(POWER_MANA);
378 // stable and not in slot saves
379 if (mode != PET_SAVE_AS_CURRENT)
380 RemoveAllAuras();
382 _SaveSpells();
383 _SaveSpellCooldowns();
384 _SaveAuras();
386 uint32 owner = GUID_LOPART(GetOwnerGUID());
387 std::string name = m_name;
388 CharacterDatabase.escape_string(name);
389 CharacterDatabase.BeginTransaction();
390 // remove current data
391 CharacterDatabase.PExecute("DELETE FROM character_pet WHERE owner = '%u' AND id = '%u'", owner,m_charmInfo->GetPetNumber() );
393 // prevent duplicate using slot (except PET_SAVE_NOT_IN_SLOT)
394 if(mode <= PET_SAVE_LAST_STABLE_SLOT)
395 CharacterDatabase.PExecute("UPDATE character_pet SET slot = '%u' WHERE owner = '%u' AND slot = '%u'",
396 PET_SAVE_NOT_IN_SLOT, owner, uint32(mode) );
398 // prevent existence another hunter pet in PET_SAVE_AS_CURRENT and PET_SAVE_NOT_IN_SLOT
399 if(getPetType()==HUNTER_PET && (mode==PET_SAVE_AS_CURRENT||mode > PET_SAVE_LAST_STABLE_SLOT))
400 CharacterDatabase.PExecute("DELETE FROM character_pet WHERE owner = '%u' AND (slot = '%u' OR slot > '%u')",
401 owner,PET_SAVE_AS_CURRENT,PET_SAVE_LAST_STABLE_SLOT);
402 // save pet
403 std::ostringstream ss;
404 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) "
405 << "VALUES ("
406 << m_charmInfo->GetPetNumber() << ", "
407 << GetEntry() << ", "
408 << owner << ", "
409 << GetNativeDisplayId() << ", "
410 << getLevel() << ", "
411 << GetUInt32Value(UNIT_FIELD_PETEXPERIENCE) << ", "
412 << uint32(m_charmInfo->GetReactState()) << ", "
413 << uint32(mode) << ", '"
414 << name.c_str() << "', "
415 << uint32(HasByteFlag(UNIT_FIELD_BYTES_2, 2, UNIT_CAN_BE_RENAMED) ? 0 : 1) << ", "
416 << (curhealth < 1 ? 1 : curhealth) << ", "
417 << curmana << ", "
418 << GetPower(POWER_HAPPINESS) << ", '";
420 for(uint32 i = ACTION_BAR_INDEX_START; i < ACTION_BAR_INDEX_END; ++i)
422 ss << uint32(m_charmInfo->GetActionBarEntry(i)->GetType()) << " "
423 << uint32(m_charmInfo->GetActionBarEntry(i)->GetAction()) << " ";
426 ss << "', "
427 << time(NULL) << ", "
428 << uint32(m_resetTalentsCost) << ", "
429 << uint64(m_resetTalentsTime) << ", "
430 << GetUInt32Value(UNIT_CREATED_BY_SPELL) << ", "
431 << uint32(getPetType()) << ")";
433 CharacterDatabase.Execute( ss.str().c_str() );
434 CharacterDatabase.CommitTransaction();
436 // delete
437 else
439 RemoveAllAuras(AURA_REMOVE_BY_DELETE);
440 DeleteFromDB(m_charmInfo->GetPetNumber());
444 void Pet::DeleteFromDB(uint32 guidlow)
446 CharacterDatabase.PExecute("DELETE FROM character_pet WHERE id = '%u'", guidlow);
447 CharacterDatabase.PExecute("DELETE FROM character_pet_declinedname WHERE id = '%u'", guidlow);
448 CharacterDatabase.PExecute("DELETE FROM pet_aura WHERE guid = '%u'", guidlow);
449 CharacterDatabase.PExecute("DELETE FROM pet_spell WHERE guid = '%u'", guidlow);
450 CharacterDatabase.PExecute("DELETE FROM pet_spell_cooldown WHERE guid = '%u'", guidlow);
453 void Pet::setDeathState(DeathState s) // overwrite virtual Creature::setDeathState and Unit::setDeathState
455 Creature::setDeathState(s);
456 if(getDeathState()==CORPSE)
458 //remove summoned pet (no corpse)
459 if(getPetType()==SUMMON_PET)
460 Remove(PET_SAVE_NOT_IN_SLOT);
461 // other will despawn at corpse desppawning (Pet::Update code)
462 else
464 // pet corpse non lootable and non skinnable
465 SetUInt32Value( UNIT_DYNAMIC_FLAGS, 0x00 );
466 RemoveFlag (UNIT_FIELD_FLAGS, UNIT_FLAG_SKINNABLE);
468 //lose happiness when died and not in BG/Arena
469 MapEntry const* mapEntry = sMapStore.LookupEntry(GetMapId());
470 if(!mapEntry || (mapEntry->map_type != MAP_ARENA && mapEntry->map_type != MAP_BATTLEGROUND))
471 ModifyPower(POWER_HAPPINESS, -HAPPINESS_LEVEL_SIZE);
473 SetFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_STUNNED);
476 else if(getDeathState()==ALIVE)
478 RemoveFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_STUNNED);
479 CastPetAuras(true);
483 void Pet::Update(uint32 diff)
485 if(m_removed) // pet already removed, just wait in remove queue, no updates
486 return;
488 switch( m_deathState )
490 case CORPSE:
492 if( m_deathTimer <= diff )
494 assert(getPetType()!=SUMMON_PET && "Must be already removed.");
495 Remove(PET_SAVE_NOT_IN_SLOT); //hunters' pets never get removed because of death, NEVER!
496 return;
498 break;
500 case ALIVE:
502 // unsummon pet that lost owner
503 Unit* owner = GetOwner();
504 if(!owner || (!IsWithinDistInMap(owner, GetMap()->GetVisibilityDistance()) && (owner->GetCharmGUID() && (owner->GetCharmGUID() != GetGUID()))) || (isControlled() && !owner->GetPetGUID()))
506 Remove(PET_SAVE_NOT_IN_SLOT, true);
507 return;
510 if(isControlled())
512 if( owner->GetPetGUID() != GetGUID() )
514 Remove(getPetType()==HUNTER_PET?PET_SAVE_AS_DELETED:PET_SAVE_NOT_IN_SLOT);
515 return;
519 if(m_duration > 0)
521 if(m_duration > diff)
522 m_duration -= diff;
523 else
525 Remove(getPetType() != SUMMON_PET ? PET_SAVE_AS_DELETED:PET_SAVE_NOT_IN_SLOT);
526 return;
530 //regenerate focus for hunter pets or energy for deathknight's ghoul
531 if(m_regenTimer <= diff)
533 switch (getPowerType())
535 case POWER_FOCUS:
536 case POWER_ENERGY:
537 Regenerate(getPowerType());
538 break;
539 default:
540 break;
542 m_regenTimer = 4000;
544 else
545 m_regenTimer -= diff;
547 if(getPetType() != HUNTER_PET)
548 break;
550 if(m_happinessTimer <= diff)
552 LooseHappiness();
553 m_happinessTimer = 7500;
555 else
556 m_happinessTimer -= diff;
558 break;
560 default:
561 break;
563 Creature::Update(diff);
566 void Pet::Regenerate(Powers power)
568 uint32 curValue = GetPower(power);
569 uint32 maxValue = GetMaxPower(power);
571 if (curValue >= maxValue)
572 return;
574 float addvalue = 0.0f;
576 switch (power)
578 case POWER_FOCUS:
580 // For hunter pets.
581 addvalue = 24 * sWorld.getRate(RATE_POWER_FOCUS);
582 break;
584 case POWER_ENERGY:
586 // For deathknight's ghoul.
587 addvalue = 20;
588 break;
590 default:
591 return;
594 // Apply modifiers (if any).
595 AuraList const& ModPowerRegenPCTAuras = GetAurasByType(SPELL_AURA_MOD_POWER_REGEN_PERCENT);
596 for(AuraList::const_iterator i = ModPowerRegenPCTAuras.begin(); i != ModPowerRegenPCTAuras.end(); ++i)
597 if ((*i)->GetModifier()->m_miscvalue == power)
598 addvalue *= ((*i)->GetModifier()->m_amount + 100) / 100.0f;
600 ModifyPower(power, (int32)addvalue);
603 void Pet::LooseHappiness()
605 uint32 curValue = GetPower(POWER_HAPPINESS);
606 if (curValue <= 0)
607 return;
608 int32 addvalue = 670; //value is 70/35/17/8/4 (per min) * 1000 / 8 (timer 7.5 secs)
609 if(isInCombat()) //we know in combat happiness fades faster, multiplier guess
610 addvalue = int32(addvalue * 1.5);
611 ModifyPower(POWER_HAPPINESS, -addvalue);
614 HappinessState Pet::GetHappinessState()
616 if(GetPower(POWER_HAPPINESS) < HAPPINESS_LEVEL_SIZE)
617 return UNHAPPY;
618 else if(GetPower(POWER_HAPPINESS) >= HAPPINESS_LEVEL_SIZE * 2)
619 return HAPPY;
620 else
621 return CONTENT;
624 bool Pet::CanTakeMoreActiveSpells(uint32 spellid)
626 uint8 activecount = 1;
627 uint32 chainstartstore[ACTIVE_SPELLS_MAX];
629 if(IsPassiveSpell(spellid))
630 return true;
632 chainstartstore[0] = sSpellMgr.GetFirstSpellInChain(spellid);
634 for (PetSpellMap::const_iterator itr = m_spells.begin(); itr != m_spells.end(); ++itr)
636 if(itr->second.state == PETSPELL_REMOVED)
637 continue;
639 if(IsPassiveSpell(itr->first))
640 continue;
642 uint32 chainstart = sSpellMgr.GetFirstSpellInChain(itr->first);
644 uint8 x;
646 for(x = 0; x < activecount; x++)
648 if(chainstart == chainstartstore[x])
649 break;
652 if(x == activecount) //spellchain not yet saved -> add active count
654 ++activecount;
655 if(activecount > ACTIVE_SPELLS_MAX)
656 return false;
657 chainstartstore[x] = chainstart;
660 return true;
663 void Pet::Remove(PetSaveMode mode, bool returnreagent)
665 Unit* owner = GetOwner();
667 if(owner)
669 if(owner->GetTypeId()==TYPEID_PLAYER)
671 ((Player*)owner)->RemovePet(this,mode,returnreagent);
672 return;
675 // only if current pet in slot
676 if(owner->GetPetGUID()==GetGUID())
677 owner->SetPet(0);
680 AddObjectToRemoveList();
681 m_removed = true;
684 void Pet::GivePetXP(uint32 xp)
686 if(getPetType() != HUNTER_PET)
687 return;
689 if ( xp < 1 )
690 return;
692 if(!isAlive())
693 return;
695 uint32 level = getLevel();
697 // XP to money conversion processed in Player::RewardQuest
698 if(level >= sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL))
699 return;
701 uint32 curXP = GetUInt32Value(UNIT_FIELD_PETEXPERIENCE);
702 uint32 nextLvlXP = GetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP);
703 uint32 newXP = curXP + xp;
705 if(newXP >= nextLvlXP && level+1 > GetOwner()->getLevel())
707 SetUInt32Value(UNIT_FIELD_PETEXPERIENCE, nextLvlXP-1);
708 return;
711 while( newXP >= nextLvlXP && level < sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL) )
713 newXP -= nextLvlXP;
715 GivePetLevel(level+1);
716 SetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP, sObjectMgr.GetXPForLevel(level+1)/4);
718 level = getLevel();
719 nextLvlXP = GetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP);
722 SetUInt32Value(UNIT_FIELD_PETEXPERIENCE, newXP);
725 void Pet::GivePetLevel(uint32 level)
727 if(!level)
728 return;
730 InitStatsForLevel(level);
731 InitLevelupSpellsForLevel();
732 InitTalentForLevel();
735 bool Pet::CreateBaseAtCreature(Creature* creature)
737 if(!creature)
739 sLog.outError("CRITICAL: NULL pointer parsed into CreateBaseAtCreature()");
740 return false;
743 uint32 guid = creature->GetMap()->GenerateLocalLowGuid(HIGHGUID_PET);
745 sLog.outBasic("Create pet");
746 uint32 pet_number = sObjectMgr.GeneratePetNumber();
747 if(!Create(guid, creature->GetMap(), creature->GetPhaseMask(), creature->GetEntry(), pet_number))
748 return false;
750 Relocate(creature->GetPositionX(), creature->GetPositionY(), creature->GetPositionZ(), creature->GetOrientation());
752 if(!IsPositionValid())
754 sLog.outError("Pet (guidlow %d, entry %d) not created base at creature. Suggested coordinates isn't valid (X: %f Y: %f)",
755 GetGUIDLow(), GetEntry(), GetPositionX(), GetPositionY());
756 return false;
759 CreatureInfo const *cinfo = GetCreatureInfo();
760 if(!cinfo)
762 sLog.outError("CreateBaseAtCreature() failed, creatureInfo is missing!");
763 return false;
766 if(cinfo->type == CREATURE_TYPE_CRITTER)
768 setPetType(MINI_PET);
769 return true;
771 SetDisplayId(creature->GetDisplayId());
772 SetNativeDisplayId(creature->GetNativeDisplayId());
773 SetMaxPower(POWER_HAPPINESS, GetCreatePowers(POWER_HAPPINESS));
774 SetPower(POWER_HAPPINESS, 166500);
775 setPowerType(POWER_FOCUS);
776 SetUInt32Value(UNIT_FIELD_PET_NAME_TIMESTAMP, 0);
777 SetUInt32Value(UNIT_FIELD_PETEXPERIENCE, 0);
778 SetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP, sObjectMgr.GetXPForLevel(creature->getLevel())/4);
779 SetUInt32Value(UNIT_NPC_FLAGS, UNIT_NPC_FLAG_NONE);
781 if(CreatureFamilyEntry const* cFamily = sCreatureFamilyStore.LookupEntry(cinfo->family))
782 SetName(cFamily->Name[sWorld.GetDefaultDbcLocale()]);
783 else
784 SetName(creature->GetNameForLocaleIdx(sObjectMgr.GetDBCLocaleIndex()));
786 if(cinfo->type == CREATURE_TYPE_BEAST)
788 SetUInt32Value(UNIT_FIELD_BYTES_0, 0x02020100);
789 SetSheath(SHEATH_STATE_MELEE);
790 SetByteFlag(UNIT_FIELD_BYTES_2, 2, UNIT_CAN_BE_RENAMED | UNIT_CAN_BE_ABANDONED);
791 SetUInt32Value(UNIT_MOD_CAST_SPEED, creature->GetUInt32Value(UNIT_MOD_CAST_SPEED));
793 return true;
796 bool Pet::InitStatsForLevel(uint32 petlevel, Unit* owner)
798 CreatureInfo const *cinfo = GetCreatureInfo();
799 assert(cinfo);
801 if(!owner)
803 owner = GetOwner();
804 if(!owner)
806 sLog.outError("attempt to summon pet (Entry %u) without owner! Attempt terminated.", cinfo->Entry);
807 return false;
811 uint32 creature_ID = (getPetType() == HUNTER_PET) ? 1 : cinfo->Entry;
813 SetLevel(petlevel);
815 SetMeleeDamageSchool(SpellSchools(cinfo->dmgschool));
817 SetModifierValue(UNIT_MOD_ARMOR, BASE_VALUE, float(petlevel*50));
819 SetAttackTime(BASE_ATTACK, BASE_ATTACK_TIME);
820 SetAttackTime(OFF_ATTACK, BASE_ATTACK_TIME);
821 SetAttackTime(RANGED_ATTACK, BASE_ATTACK_TIME);
823 SetFloatValue(UNIT_MOD_CAST_SPEED, 1.0);
825 CreatureFamilyEntry const* cFamily = sCreatureFamilyStore.LookupEntry(cinfo->family);
826 if(cFamily && cFamily->minScale > 0.0f && getPetType()==HUNTER_PET)
828 float scale;
829 if (getLevel() >= cFamily->maxScaleLevel)
830 scale = cFamily->maxScale;
831 else if (getLevel() <= cFamily->minScaleLevel)
832 scale = cFamily->minScale;
833 else
834 scale = cFamily->minScale + float(getLevel() - cFamily->minScaleLevel) / cFamily->maxScaleLevel * (cFamily->maxScale - cFamily->minScale);
836 SetFloatValue(OBJECT_FIELD_SCALE_X, scale);
838 m_bonusdamage = 0;
840 int32 createResistance[MAX_SPELL_SCHOOL] = {0,0,0,0,0,0,0};
842 if(cinfo && getPetType() != HUNTER_PET)
844 createResistance[SPELL_SCHOOL_HOLY] = cinfo->resistance1;
845 createResistance[SPELL_SCHOOL_FIRE] = cinfo->resistance2;
846 createResistance[SPELL_SCHOOL_NATURE] = cinfo->resistance3;
847 createResistance[SPELL_SCHOOL_FROST] = cinfo->resistance4;
848 createResistance[SPELL_SCHOOL_SHADOW] = cinfo->resistance5;
849 createResistance[SPELL_SCHOOL_ARCANE] = cinfo->resistance6;
852 switch(getPetType())
854 case SUMMON_PET:
856 if(owner->GetTypeId() == TYPEID_PLAYER)
858 switch(owner->getClass())
860 case CLASS_WARLOCK:
863 //the damage bonus used for pets is either fire or shadow damage, whatever is higher
864 uint32 fire = owner->GetUInt32Value(PLAYER_FIELD_MOD_DAMAGE_DONE_POS + SPELL_SCHOOL_FIRE);
865 uint32 shadow = owner->GetUInt32Value(PLAYER_FIELD_MOD_DAMAGE_DONE_POS + SPELL_SCHOOL_SHADOW);
866 uint32 val = (fire > shadow) ? fire : shadow;
868 SetBonusDamage(int32 (val * 0.15f));
869 //bonusAP += val * 0.57;
870 break;
872 case CLASS_MAGE:
874 //40% damage bonus of mage's frost damage
875 float val = owner->GetUInt32Value(PLAYER_FIELD_MOD_DAMAGE_DONE_POS + SPELL_SCHOOL_FROST) * 0.4;
876 if(val < 0)
877 val = 0;
878 SetBonusDamage( int32(val));
879 break;
881 default:
882 break;
886 SetBaseWeaponDamage(BASE_ATTACK, MINDAMAGE, float(petlevel - (petlevel / 4)) );
887 SetBaseWeaponDamage(BASE_ATTACK, MAXDAMAGE, float(petlevel + (petlevel / 4)) );
889 //SetModifierValue(UNIT_MOD_ATTACK_POWER, BASE_VALUE, float(cinfo->attackpower));
891 PetLevelInfo const* pInfo = sObjectMgr.GetPetLevelInfo(creature_ID, petlevel);
892 if(pInfo) // exist in DB
894 SetCreateHealth(pInfo->health);
895 SetCreateMana(pInfo->mana);
897 if(pInfo->armor > 0)
898 SetModifierValue(UNIT_MOD_ARMOR, BASE_VALUE, float(pInfo->armor));
900 for(int stat = 0; stat < MAX_STATS; ++stat)
902 SetCreateStat(Stats(stat), float(pInfo->stats[stat]));
905 else // not exist in DB, use some default fake data
907 sLog.outErrorDb("Summoned pet (Entry: %u) not have pet stats data in DB",cinfo->Entry);
909 // remove elite bonuses included in DB values
910 SetCreateHealth(uint32(((float(cinfo->maxhealth) / cinfo->maxlevel) / (1 + 2 * cinfo->rank)) * petlevel) );
911 SetCreateMana( uint32(((float(cinfo->maxmana) / cinfo->maxlevel) / (1 + 2 * cinfo->rank)) * petlevel) );
913 SetCreateStat(STAT_STRENGTH, 22);
914 SetCreateStat(STAT_AGILITY, 22);
915 SetCreateStat(STAT_STAMINA, 25);
916 SetCreateStat(STAT_INTELLECT, 28);
917 SetCreateStat(STAT_SPIRIT, 27);
919 break;
921 case HUNTER_PET:
923 SetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP, sObjectMgr.GetXPForLevel(petlevel)/4);
924 //these formula may not be correct; however, it is designed to be close to what it should be
925 //this makes dps 0.5 of pets level
926 SetBaseWeaponDamage(BASE_ATTACK, MINDAMAGE, float(petlevel - (petlevel / 4)) );
927 //damage range is then petlevel / 2
928 SetBaseWeaponDamage(BASE_ATTACK, MAXDAMAGE, float(petlevel + (petlevel / 4)) );
929 //damage is increased afterwards as strength and pet scaling modify attack power
931 //stored standard pet stats are entry 1 in pet_levelinfo
932 PetLevelInfo const* pInfo = sObjectMgr.GetPetLevelInfo(creature_ID, petlevel);
933 if(pInfo) // exist in DB
935 SetCreateHealth(pInfo->health);
936 SetModifierValue(UNIT_MOD_ARMOR, BASE_VALUE, float(pInfo->armor));
937 //SetModifierValue(UNIT_MOD_ATTACK_POWER, BASE_VALUE, float(cinfo->attackpower));
939 for( int i = STAT_STRENGTH; i < MAX_STATS; ++i)
941 SetCreateStat(Stats(i), float(pInfo->stats[i]));
944 else // not exist in DB, use some default fake data
946 sLog.outErrorDb("Hunter pet levelstats missing in DB");
948 // remove elite bonuses included in DB values
949 SetCreateHealth( uint32(((float(cinfo->maxhealth) / cinfo->maxlevel) / (1 + 2 * cinfo->rank)) * petlevel) );
951 SetCreateStat(STAT_STRENGTH, 22);
952 SetCreateStat(STAT_AGILITY, 22);
953 SetCreateStat(STAT_STAMINA, 25);
954 SetCreateStat(STAT_INTELLECT, 28);
955 SetCreateStat(STAT_SPIRIT, 27);
957 break;
959 case GUARDIAN_PET:
960 SetUInt32Value(UNIT_FIELD_PETEXPERIENCE, 0);
961 SetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP, 1000);
963 SetCreateMana(28 + 10*petlevel);
964 SetCreateHealth(28 + 30*petlevel);
966 // FIXME: this is wrong formula, possible each guardian pet have own damage formula
967 //these formula may not be correct; however, it is designed to be close to what it should be
968 //this makes dps 0.5 of pets level
969 SetBaseWeaponDamage(BASE_ATTACK, MINDAMAGE, float(petlevel - (petlevel / 4)));
970 //damage range is then petlevel / 2
971 SetBaseWeaponDamage(BASE_ATTACK, MAXDAMAGE, float(petlevel + (petlevel / 4)));
972 break;
973 default:
974 sLog.outError("Pet have incorrect type (%u) for levelup.", getPetType());
975 break;
978 for (int i = SPELL_SCHOOL_HOLY; i < MAX_SPELL_SCHOOL; ++i)
979 SetModifierValue(UnitMods(UNIT_MOD_RESISTANCE_START + i), BASE_VALUE, float(createResistance[i]));
981 UpdateAllStats();
983 SetHealth(GetMaxHealth());
984 SetPower(POWER_MANA, GetMaxPower(POWER_MANA));
986 return true;
989 bool Pet::HaveInDiet(ItemPrototype const* item) const
991 if (!item->FoodType)
992 return false;
994 CreatureInfo const* cInfo = GetCreatureInfo();
995 if(!cInfo)
996 return false;
998 CreatureFamilyEntry const* cFamily = sCreatureFamilyStore.LookupEntry(cInfo->family);
999 if(!cFamily)
1000 return false;
1002 uint32 diet = cFamily->petFoodMask;
1003 uint32 FoodMask = 1 << (item->FoodType-1);
1004 return diet & FoodMask;
1007 uint32 Pet::GetCurrentFoodBenefitLevel(uint32 itemlevel)
1009 // -5 or greater food level
1010 if(getLevel() <= itemlevel + 5) //possible to feed level 60 pet with level 55 level food for full effect
1011 return 35000;
1012 // -10..-6
1013 else if(getLevel() <= itemlevel + 10) //pure guess, but sounds good
1014 return 17000;
1015 // -14..-11
1016 else if(getLevel() <= itemlevel + 14) //level 55 food gets green on 70, makes sense to me
1017 return 8000;
1018 // -15 or less
1019 else
1020 return 0; //food too low level
1023 void Pet::_LoadSpellCooldowns()
1025 m_CreatureSpellCooldowns.clear();
1026 m_CreatureCategoryCooldowns.clear();
1028 QueryResult *result = CharacterDatabase.PQuery("SELECT spell,time FROM pet_spell_cooldown WHERE guid = '%u'",m_charmInfo->GetPetNumber());
1030 if(result)
1032 time_t curTime = time(NULL);
1034 WorldPacket data(SMSG_SPELL_COOLDOWN, (8+1+result->GetRowCount()*8));
1035 data << GetGUID();
1036 data << uint8(0x0); // flags (0x1, 0x2)
1040 Field *fields = result->Fetch();
1042 uint32 spell_id = fields[0].GetUInt32();
1043 time_t db_time = (time_t)fields[1].GetUInt64();
1045 if(!sSpellStore.LookupEntry(spell_id))
1047 sLog.outError("Pet %u have unknown spell %u in `pet_spell_cooldown`, skipping.",m_charmInfo->GetPetNumber(),spell_id);
1048 continue;
1051 // skip outdated cooldown
1052 if(db_time <= curTime)
1053 continue;
1055 data << uint32(spell_id);
1056 data << uint32(uint32(db_time-curTime)*IN_MILISECONDS);
1058 _AddCreatureSpellCooldown(spell_id,db_time);
1060 sLog.outDebug("Pet (Number: %u) spell %u cooldown loaded (%u secs).", m_charmInfo->GetPetNumber(), spell_id, uint32(db_time-curTime));
1062 while( result->NextRow() );
1064 delete result;
1066 if(!m_CreatureSpellCooldowns.empty() && GetOwner())
1068 ((Player*)GetOwner())->GetSession()->SendPacket(&data);
1073 void Pet::_SaveSpellCooldowns()
1075 CharacterDatabase.PExecute("DELETE FROM pet_spell_cooldown WHERE guid = '%u'", m_charmInfo->GetPetNumber());
1077 time_t curTime = time(NULL);
1079 // remove oudated and save active
1080 for(CreatureSpellCooldowns::iterator itr = m_CreatureSpellCooldowns.begin();itr != m_CreatureSpellCooldowns.end();)
1082 if(itr->second <= curTime)
1083 m_CreatureSpellCooldowns.erase(itr++);
1084 else
1086 CharacterDatabase.PExecute("INSERT INTO pet_spell_cooldown (guid,spell,time) VALUES ('%u', '%u', '" UI64FMTD "')", m_charmInfo->GetPetNumber(), itr->first, uint64(itr->second));
1087 ++itr;
1092 void Pet::_LoadSpells()
1094 QueryResult *result = CharacterDatabase.PQuery("SELECT spell,active FROM pet_spell WHERE guid = '%u'",m_charmInfo->GetPetNumber());
1096 if(result)
1100 Field *fields = result->Fetch();
1102 addSpell(fields[0].GetUInt32(), ActiveStates(fields[1].GetUInt8()), PETSPELL_UNCHANGED);
1104 while( result->NextRow() );
1106 delete result;
1110 void Pet::_SaveSpells()
1112 for (PetSpellMap::iterator itr = m_spells.begin(), next = m_spells.begin(); itr != m_spells.end(); itr = next)
1114 ++next;
1116 // prevent saving family passives to DB
1117 if (itr->second.type == PETSPELL_FAMILY)
1118 continue;
1120 switch(itr->second.state)
1122 case PETSPELL_REMOVED:
1123 CharacterDatabase.PExecute("DELETE FROM pet_spell WHERE guid = '%u' and spell = '%u'", m_charmInfo->GetPetNumber(), itr->first);
1124 m_spells.erase(itr);
1125 continue;
1126 case PETSPELL_CHANGED:
1127 CharacterDatabase.PExecute("DELETE FROM pet_spell WHERE guid = '%u' and spell = '%u'", m_charmInfo->GetPetNumber(), itr->first);
1128 CharacterDatabase.PExecute("INSERT INTO pet_spell (guid,spell,active) VALUES ('%u', '%u', '%u')", m_charmInfo->GetPetNumber(), itr->first, itr->second.active);
1129 break;
1130 case PETSPELL_NEW:
1131 CharacterDatabase.PExecute("INSERT INTO pet_spell (guid,spell,active) VALUES ('%u', '%u', '%u')", m_charmInfo->GetPetNumber(), itr->first, itr->second.active);
1132 break;
1133 case PETSPELL_UNCHANGED:
1134 continue;
1137 itr->second.state = PETSPELL_UNCHANGED;
1141 void Pet::_LoadAuras(uint32 timediff)
1143 RemoveAllAuras();
1145 QueryResult *result = CharacterDatabase.PQuery("SELECT caster_guid,spell,effect_index,stackcount,amount,maxduration,remaintime,remaincharges FROM pet_aura WHERE guid = '%u'",m_charmInfo->GetPetNumber());
1147 if(result)
1151 Field *fields = result->Fetch();
1152 uint64 caster_guid = fields[0].GetUInt64();
1153 uint32 spellid = fields[1].GetUInt32();
1154 uint32 effindex = fields[2].GetUInt32();
1155 uint32 stackcount= fields[3].GetUInt32();
1156 int32 damage = (int32)fields[4].GetUInt32();
1157 int32 maxduration = (int32)fields[5].GetUInt32();
1158 int32 remaintime = (int32)fields[6].GetUInt32();
1159 int32 remaincharges = (int32)fields[7].GetUInt32();
1161 SpellEntry const* spellproto = sSpellStore.LookupEntry(spellid);
1162 if(!spellproto)
1164 sLog.outError("Unknown aura (spellid %u, effindex %u), ignore.",spellid,effindex);
1165 continue;
1168 if(effindex >= 3)
1170 sLog.outError("Invalid effect index (spellid %u, effindex %u), ignore.",spellid,effindex);
1171 continue;
1174 // negative effects should continue counting down after logout
1175 if (remaintime != -1 && !IsPositiveEffect(spellid, effindex))
1177 if (remaintime/IN_MILISECONDS <= int32(timediff))
1178 continue;
1180 remaintime -= timediff*IN_MILISECONDS;
1183 // prevent wrong values of remaincharges
1184 if(spellproto->procCharges)
1186 if(remaincharges <= 0 || remaincharges > spellproto->procCharges)
1187 remaincharges = spellproto->procCharges;
1189 else
1190 remaincharges = -1;
1192 /// do not load single target auras (unless they were cast by the player)
1193 if (caster_guid != GetGUID() && IsSingleTargetSpell(spellproto))
1194 continue;
1196 for(uint32 i=0; i<stackcount; ++i)
1198 Aura* aura = CreateAura(spellproto, effindex, NULL, this, NULL);
1200 if(!damage)
1201 damage = aura->GetModifier()->m_amount;
1202 aura->SetLoadedState(caster_guid,damage,maxduration,remaintime,remaincharges);
1203 AddAura(aura);
1206 while( result->NextRow() );
1208 delete result;
1212 void Pet::_SaveAuras()
1214 CharacterDatabase.PExecute("DELETE FROM pet_aura WHERE guid = '%u'", m_charmInfo->GetPetNumber());
1216 AuraMap const& auras = GetAuras();
1217 if (auras.empty())
1218 return;
1220 spellEffectPair lastEffectPair = auras.begin()->first;
1221 uint32 stackCounter = 1;
1223 for(AuraMap::const_iterator itr = auras.begin(); ; ++itr)
1225 if(itr == auras.end() || lastEffectPair != itr->first)
1227 AuraMap::const_iterator itr2 = itr;
1228 // save previous spellEffectPair to db
1229 itr2--;
1230 SpellEntry const *spellInfo = itr2->second->GetSpellProto();
1231 /// do not save single target auras (unless they were cast by the player)
1232 if (!(itr2->second->GetCasterGUID() != GetGUID() && IsSingleTargetSpell(spellInfo)))
1234 if(!itr2->second->IsPassive())
1236 // skip all auras from spell that apply at cast SPELL_AURA_MOD_SHAPESHIFT or pet area auras.
1237 uint8 i;
1238 for (i = 0; i < 3; ++i)
1239 if (spellInfo->EffectApplyAuraName[i] == SPELL_AURA_MOD_STEALTH ||
1240 spellInfo->Effect[i] == SPELL_EFFECT_APPLY_AREA_AURA_OWNER ||
1241 spellInfo->Effect[i] == SPELL_EFFECT_APPLY_AREA_AURA_PET )
1242 break;
1244 if (i == 3)
1246 CharacterDatabase.PExecute("INSERT INTO pet_aura (guid,caster_guid,spell,effect_index,stackcount,amount,maxduration,remaintime,remaincharges) "
1247 "VALUES ('%u', '" UI64FMTD "', '%u', '%u', '%u', '%d', '%d', '%d', '%d')",
1248 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()));
1252 if(itr == auras.end())
1253 break;
1256 if (lastEffectPair == itr->first)
1257 stackCounter++;
1258 else
1260 lastEffectPair = itr->first;
1261 stackCounter = 1;
1266 bool Pet::addSpell(uint32 spell_id,ActiveStates active /*= ACT_DECIDE*/, PetSpellState state /*= PETSPELL_NEW*/, PetSpellType type /*= PETSPELL_NORMAL*/)
1268 SpellEntry const *spellInfo = sSpellStore.LookupEntry(spell_id);
1269 if (!spellInfo)
1271 // do pet spell book cleanup
1272 if(state == PETSPELL_UNCHANGED) // spell load case
1274 sLog.outError("Pet::addSpell: Non-existed in SpellStore spell #%u request, deleting for all pets in `pet_spell`.",spell_id);
1275 CharacterDatabase.PExecute("DELETE FROM pet_spell WHERE spell = '%u'",spell_id);
1277 else
1278 sLog.outError("Pet::addSpell: Non-existed in SpellStore spell #%u request.",spell_id);
1280 return false;
1283 PetSpellMap::iterator itr = m_spells.find(spell_id);
1284 if (itr != m_spells.end())
1286 if (itr->second.state == PETSPELL_REMOVED)
1288 m_spells.erase(itr);
1289 state = PETSPELL_CHANGED;
1291 else if (state == PETSPELL_UNCHANGED && itr->second.state != PETSPELL_UNCHANGED)
1293 // can be in case spell loading but learned at some previous spell loading
1294 itr->second.state = PETSPELL_UNCHANGED;
1296 if(active == ACT_ENABLED)
1297 ToggleAutocast(spell_id, true);
1298 else if(active == ACT_DISABLED)
1299 ToggleAutocast(spell_id, false);
1301 return false;
1303 else
1304 return false;
1307 uint32 oldspell_id = 0;
1309 PetSpell newspell;
1310 newspell.state = state;
1311 newspell.type = type;
1313 if(active == ACT_DECIDE) //active was not used before, so we save it's autocast/passive state here
1315 if(IsPassiveSpell(spell_id))
1316 newspell.active = ACT_PASSIVE;
1317 else
1318 newspell.active = ACT_DISABLED;
1320 else
1321 newspell.active = active;
1323 // talent: unlearn all other talent ranks (high and low)
1324 if(TalentSpellPos const* talentPos = GetTalentSpellPos(spell_id))
1326 if(TalentEntry const *talentInfo = sTalentStore.LookupEntry( talentPos->talent_id ))
1328 for(int i=0; i < MAX_TALENT_RANK; ++i)
1330 // skip learning spell and no rank spell case
1331 uint32 rankSpellId = talentInfo->RankID[i];
1332 if(!rankSpellId || rankSpellId==spell_id)
1333 continue;
1335 // skip unknown ranks
1336 if(!HasSpell(rankSpellId))
1337 continue;
1338 removeSpell(rankSpellId,false,false);
1342 else if(sSpellMgr.GetSpellRank(spell_id)!=0)
1344 for (PetSpellMap::const_iterator itr2 = m_spells.begin(); itr2 != m_spells.end(); ++itr2)
1346 if(itr2->second.state == PETSPELL_REMOVED) continue;
1348 if( sSpellMgr.IsRankSpellDueToSpell(spellInfo,itr2->first) )
1350 // replace by new high rank
1351 if(sSpellMgr.IsHighRankOfSpell(spell_id,itr2->first))
1353 newspell.active = itr2->second.active;
1355 if(newspell.active == ACT_ENABLED)
1356 ToggleAutocast(itr2->first, false);
1358 oldspell_id = itr2->first;
1359 unlearnSpell(itr2->first,false,false);
1360 break;
1362 // ignore new lesser rank
1363 else if(sSpellMgr.IsHighRankOfSpell(itr2->first,spell_id))
1364 return false;
1369 m_spells[spell_id] = newspell;
1371 if (IsPassiveSpell(spell_id))
1372 CastSpell(this, spell_id, true);
1373 else
1374 m_charmInfo->AddSpellToActionBar(spell_id);
1376 if(newspell.active == ACT_ENABLED)
1377 ToggleAutocast(spell_id, true);
1379 uint32 talentCost = GetTalentSpellCost(spell_id);
1380 if (talentCost)
1382 int32 free_points = GetMaxTalentPointsForLevel(getLevel());
1383 m_usedTalentCount+=talentCost;
1384 // update free talent points
1385 free_points-=m_usedTalentCount;
1386 SetFreeTalentPoints(free_points > 0 ? free_points : 0);
1388 return true;
1391 bool Pet::learnSpell(uint32 spell_id)
1393 // prevent duplicated entires in spell book
1394 if (!addSpell(spell_id))
1395 return false;
1397 if(!m_loading)
1399 Unit* owner = GetOwner();
1400 if(owner && owner->GetTypeId() == TYPEID_PLAYER)
1402 WorldPacket data(SMSG_PET_LEARNED_SPELL, 4);
1403 data << uint32(spell_id);
1404 ((Player*)owner)->GetSession()->SendPacket(&data);
1406 ((Player*)owner)->PetSpellInitialize();
1409 return true;
1412 void Pet::InitLevelupSpellsForLevel()
1414 uint32 level = getLevel();
1416 if(PetLevelupSpellSet const *levelupSpells = GetCreatureInfo()->family ? sSpellMgr.GetPetLevelupSpellList(GetCreatureInfo()->family) : NULL)
1418 // PetLevelupSpellSet ordered by levels, process in reversed order
1419 for(PetLevelupSpellSet::const_reverse_iterator itr = levelupSpells->rbegin(); itr != levelupSpells->rend(); ++itr)
1421 // will called first if level down
1422 if(itr->first > level)
1423 unlearnSpell(itr->second,true); // will learn prev rank if any
1424 // will called if level up
1425 else
1426 learnSpell(itr->second); // will unlearn prev rank if any
1430 int32 petSpellsId = GetCreatureInfo()->PetSpellDataId ? -(int32)GetCreatureInfo()->PetSpellDataId : GetEntry();
1432 // default spells (can be not learned if pet level (as owner level decrease result for example) less first possible in normal game)
1433 if(PetDefaultSpellsEntry const *defSpells = sSpellMgr.GetPetDefaultSpellsEntry(petSpellsId))
1435 for(int i = 0; i < MAX_CREATURE_SPELL_DATA_SLOT; ++i)
1437 SpellEntry const* spellEntry = sSpellStore.LookupEntry(defSpells->spellid[i]);
1438 if(!spellEntry)
1439 continue;
1441 // will called first if level down
1442 if(spellEntry->spellLevel > level)
1443 unlearnSpell(spellEntry->Id,true);
1444 // will called if level up
1445 else
1446 learnSpell(spellEntry->Id);
1451 bool Pet::unlearnSpell(uint32 spell_id, bool learn_prev, bool clear_ab)
1453 if(removeSpell(spell_id,learn_prev,clear_ab))
1455 if(!m_loading)
1457 if (Unit* owner = GetOwner())
1459 if(owner->GetTypeId() == TYPEID_PLAYER)
1461 WorldPacket data(SMSG_PET_REMOVED_SPELL, 4);
1462 data << uint32(spell_id);
1463 ((Player*)owner)->GetSession()->SendPacket(&data);
1467 return true;
1469 return false;
1472 bool Pet::removeSpell(uint32 spell_id, bool learn_prev, bool clear_ab)
1474 PetSpellMap::iterator itr = m_spells.find(spell_id);
1475 if (itr == m_spells.end())
1476 return false;
1478 if(itr->second.state == PETSPELL_REMOVED)
1479 return false;
1481 if(itr->second.state == PETSPELL_NEW)
1482 m_spells.erase(itr);
1483 else
1484 itr->second.state = PETSPELL_REMOVED;
1486 RemoveAurasDueToSpell(spell_id);
1488 uint32 talentCost = GetTalentSpellCost(spell_id);
1489 if (talentCost > 0)
1491 if (m_usedTalentCount > talentCost)
1492 m_usedTalentCount-=talentCost;
1493 else
1494 m_usedTalentCount = 0;
1495 // update free talent points
1496 int32 free_points = GetMaxTalentPointsForLevel(getLevel()) - m_usedTalentCount;
1497 SetFreeTalentPoints(free_points > 0 ? free_points : 0);
1500 if (learn_prev)
1502 if (uint32 prev_id = sSpellMgr.GetPrevSpellInChain (spell_id))
1503 learnSpell(prev_id);
1504 else
1505 learn_prev = false;
1508 // if remove last rank or non-ranked then update action bar at server and client if need
1509 if (clear_ab && !learn_prev && m_charmInfo->RemoveSpellFromActionBar(spell_id))
1511 if(!m_loading)
1513 // need update action bar for last removed rank
1514 if (Unit* owner = GetOwner())
1515 if (owner->GetTypeId() == TYPEID_PLAYER)
1516 ((Player*)owner)->PetSpellInitialize();
1520 return true;
1524 void Pet::CleanupActionBar()
1526 for(int i = 0; i < MAX_UNIT_ACTION_BAR_INDEX; ++i)
1527 if(UnitActionBarEntry const* ab = m_charmInfo->GetActionBarEntry(i))
1528 if(uint32 action = ab->GetAction())
1529 if(ab->IsActionBarForSpell() && !HasSpell(action))
1530 m_charmInfo->SetActionBar(i,0,ACT_DISABLED);
1533 void Pet::InitPetCreateSpells()
1535 m_charmInfo->InitPetActionBar();
1536 m_spells.clear();
1538 LearnPetPassives();
1540 CastPetAuras(false);
1543 bool Pet::resetTalents(bool no_cost)
1545 Unit *owner = GetOwner();
1546 if (!owner || owner->GetTypeId()!=TYPEID_PLAYER)
1547 return false;
1549 // not need after this call
1550 if(((Player*)owner)->HasAtLoginFlag(AT_LOGIN_RESET_PET_TALENTS))
1551 ((Player*)owner)->RemoveAtLoginFlag(AT_LOGIN_RESET_PET_TALENTS,true);
1553 CreatureInfo const * ci = GetCreatureInfo();
1554 if(!ci)
1555 return false;
1556 // Check pet talent type
1557 CreatureFamilyEntry const *pet_family = sCreatureFamilyStore.LookupEntry(ci->family);
1558 if(!pet_family || pet_family->petTalentType < 0)
1559 return false;
1561 Player *player = (Player *)owner;
1563 uint32 level = getLevel();
1564 uint32 talentPointsForLevel = GetMaxTalentPointsForLevel(level);
1566 if (m_usedTalentCount == 0)
1568 SetFreeTalentPoints(talentPointsForLevel);
1569 return false;
1572 uint32 cost = 0;
1574 if(!no_cost)
1576 cost = resetTalentsCost();
1578 if (player->GetMoney() < cost)
1580 player->SendBuyError( BUY_ERR_NOT_ENOUGHT_MONEY, 0, 0, 0);
1581 return false;
1585 for (unsigned int i = 0; i < sTalentStore.GetNumRows(); ++i)
1587 TalentEntry const *talentInfo = sTalentStore.LookupEntry(i);
1589 if (!talentInfo) continue;
1591 TalentTabEntry const *talentTabInfo = sTalentTabStore.LookupEntry( talentInfo->TalentTab );
1593 if(!talentTabInfo)
1594 continue;
1596 // unlearn only talents for pets family talent type
1597 if(!((1 << pet_family->petTalentType) & talentTabInfo->petTalentMask))
1598 continue;
1600 for (int j = 0; j < MAX_TALENT_RANK; j++)
1602 for(PetSpellMap::const_iterator itr = m_spells.begin(); itr != m_spells.end();)
1604 if(itr->second.state == PETSPELL_REMOVED)
1606 ++itr;
1607 continue;
1609 // remove learned spells (all ranks)
1610 uint32 itrFirstId = sSpellMgr.GetFirstSpellInChain(itr->first);
1612 // unlearn if first rank is talent or learned by talent
1613 if (itrFirstId == talentInfo->RankID[j] || sSpellMgr.IsSpellLearnToSpell(talentInfo->RankID[j],itrFirstId))
1615 removeSpell(itr->first,false);
1616 itr = m_spells.begin();
1617 continue;
1619 else
1620 ++itr;
1625 SetFreeTalentPoints(talentPointsForLevel);
1627 if(!no_cost)
1629 player->ModifyMoney(-(int32)cost);
1631 m_resetTalentsCost = cost;
1632 m_resetTalentsTime = time(NULL);
1634 player->PetSpellInitialize();
1635 return true;
1638 void Pet::resetTalentsForAllPetsOf(Player* owner, Pet* online_pet /*= NULL*/)
1640 // not need after this call
1641 if(((Player*)owner)->HasAtLoginFlag(AT_LOGIN_RESET_PET_TALENTS))
1642 ((Player*)owner)->RemoveAtLoginFlag(AT_LOGIN_RESET_PET_TALENTS,true);
1644 // reset for online
1645 if(online_pet)
1646 online_pet->resetTalents(true);
1648 // now need only reset for offline pets (all pets except online case)
1649 uint32 except_petnumber = online_pet ? online_pet->GetCharmInfo()->GetPetNumber() : 0;
1651 QueryResult *resultPets = CharacterDatabase.PQuery(
1652 "SELECT id FROM character_pet WHERE owner = '%u' AND id <> '%u'",
1653 owner->GetGUIDLow(),except_petnumber);
1655 // no offline pets
1656 if(!resultPets)
1657 return;
1659 QueryResult *result = CharacterDatabase.PQuery(
1660 "SELECT DISTINCT pet_spell.spell FROM pet_spell, character_pet "
1661 "WHERE character_pet.owner = '%u' AND character_pet.id = pet_spell.guid AND character_pet.id <> %u",
1662 owner->GetGUIDLow(),except_petnumber);
1664 if(!result)
1666 delete resultPets;
1667 return;
1670 bool need_comma = false;
1671 std::ostringstream ss;
1672 ss << "DELETE FROM pet_spell WHERE guid IN (";
1676 Field *fields = resultPets->Fetch();
1678 uint32 id = fields[0].GetUInt32();
1680 if(need_comma)
1681 ss << ",";
1683 ss << id;
1685 need_comma = true;
1687 while( resultPets->NextRow() );
1689 delete resultPets;
1691 ss << ") AND spell IN (";
1693 bool need_execute = false;
1696 Field *fields = result->Fetch();
1698 uint32 spell = fields[0].GetUInt32();
1700 if(!GetTalentSpellCost(spell))
1701 continue;
1703 if(need_execute)
1704 ss << ",";
1706 ss << spell;
1708 need_execute = true;
1710 while( result->NextRow() );
1712 delete result;
1714 if(!need_execute)
1715 return;
1717 ss << ")";
1719 CharacterDatabase.Execute(ss.str().c_str());
1722 void Pet::InitTalentForLevel()
1724 uint32 level = getLevel();
1725 uint32 talentPointsForLevel = GetMaxTalentPointsForLevel(level);
1726 // Reset talents in case low level (on level down) or wrong points for level (hunter can unlearn TP increase talent)
1727 if(talentPointsForLevel == 0 || m_usedTalentCount > talentPointsForLevel)
1729 // Remove all talent points
1730 resetTalents(true);
1732 SetFreeTalentPoints(talentPointsForLevel - m_usedTalentCount);
1734 Unit *owner = GetOwner();
1735 if (!owner || owner->GetTypeId() != TYPEID_PLAYER)
1736 return;
1738 if(!m_loading)
1739 ((Player*)owner)->SendTalentsInfoData(true);
1742 uint32 Pet::resetTalentsCost() const
1744 uint32 days = (sWorld.GetGameTime() - m_resetTalentsTime)/DAY;
1746 // The first time reset costs 10 silver; after 1 day cost is reset to 10 silver
1747 if(m_resetTalentsCost < 10*SILVER || days > 0)
1748 return 10*SILVER;
1749 // then 50 silver
1750 else if(m_resetTalentsCost < 50*SILVER)
1751 return 50*SILVER;
1752 // then 1 gold
1753 else if(m_resetTalentsCost < 1*GOLD)
1754 return 1*GOLD;
1755 // then increasing at a rate of 1 gold; cap 10 gold
1756 else
1757 return (m_resetTalentsCost + 1*GOLD > 10*GOLD ? 10*GOLD : m_resetTalentsCost + 1*GOLD);
1760 uint8 Pet::GetMaxTalentPointsForLevel(uint32 level)
1762 uint8 points = (level >= 20) ? ((level - 16) / 4) : 0;
1763 // Mod points from owner SPELL_AURA_MOD_PET_TALENT_POINTS
1764 if (Unit *owner = GetOwner())
1765 points+=owner->GetTotalAuraModifier(SPELL_AURA_MOD_PET_TALENT_POINTS);
1766 return points;
1769 void Pet::ToggleAutocast(uint32 spellid, bool apply)
1771 if(IsPassiveSpell(spellid))
1772 return;
1774 PetSpellMap::iterator itr = m_spells.find(spellid);
1776 int i;
1778 if(apply)
1780 for (i = 0; i < m_autospells.size() && m_autospells[i] != spellid; ++i)
1781 ; // just search
1783 if (i == m_autospells.size())
1785 m_autospells.push_back(spellid);
1787 if(itr->second.active != ACT_ENABLED)
1789 itr->second.active = ACT_ENABLED;
1790 if(itr->second.state != PETSPELL_NEW)
1791 itr->second.state = PETSPELL_CHANGED;
1795 else
1797 AutoSpellList::iterator itr2 = m_autospells.begin();
1798 for (i = 0; i < m_autospells.size() && m_autospells[i] != spellid; ++i, itr2++)
1799 ; // just search
1801 if (i < m_autospells.size())
1803 m_autospells.erase(itr2);
1804 if(itr->second.active != ACT_DISABLED)
1806 itr->second.active = ACT_DISABLED;
1807 if(itr->second.state != PETSPELL_NEW)
1808 itr->second.state = PETSPELL_CHANGED;
1814 bool Pet::IsPermanentPetFor(Player* owner)
1816 switch(getPetType())
1818 case SUMMON_PET:
1819 switch(owner->getClass())
1821 case CLASS_WARLOCK:
1822 return GetCreatureInfo()->type == CREATURE_TYPE_DEMON;
1823 case CLASS_DEATH_KNIGHT:
1824 return GetCreatureInfo()->type == CREATURE_TYPE_UNDEAD;
1825 default:
1826 return false;
1828 case HUNTER_PET:
1829 return true;
1830 default:
1831 return false;
1835 bool Pet::Create(uint32 guidlow, Map *map, uint32 phaseMask, uint32 Entry, uint32 pet_number)
1837 SetMap(map);
1838 SetPhaseMask(phaseMask,false);
1840 Object::_Create(guidlow, pet_number, HIGHGUID_PET);
1842 m_DBTableGuid = guidlow;
1843 m_originalEntry = Entry;
1845 if(!InitEntry(Entry))
1846 return false;
1848 SetSheath(SHEATH_STATE_MELEE);
1850 if(getPetType() == MINI_PET) // always non-attackable
1851 SetFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_NON_ATTACKABLE);
1853 return true;
1856 bool Pet::HasSpell(uint32 spell) const
1858 PetSpellMap::const_iterator itr = m_spells.find(spell);
1859 return (itr != m_spells.end() && itr->second.state != PETSPELL_REMOVED );
1862 // Get all passive spells in our skill line
1863 void Pet::LearnPetPassives()
1865 CreatureInfo const* cInfo = GetCreatureInfo();
1866 if(!cInfo)
1867 return;
1869 CreatureFamilyEntry const* cFamily = sCreatureFamilyStore.LookupEntry(cInfo->family);
1870 if(!cFamily)
1871 return;
1873 PetFamilySpellsStore::const_iterator petStore = sPetFamilySpellsStore.find(cFamily->ID);
1874 if(petStore != sPetFamilySpellsStore.end())
1876 for(PetFamilySpellsSet::const_iterator petSet = petStore->second.begin(); petSet != petStore->second.end(); ++petSet)
1877 addSpell(*petSet, ACT_DECIDE, PETSPELL_NEW, PETSPELL_FAMILY);
1881 void Pet::CastPetAuras(bool current)
1883 Unit* owner = GetOwner();
1884 if(!owner || owner->GetTypeId()!=TYPEID_PLAYER)
1885 return;
1887 for(PetAuraSet::const_iterator itr = owner->m_petAuras.begin(); itr != owner->m_petAuras.end();)
1889 PetAura const* pa = *itr;
1890 ++itr;
1892 if(!current && pa->IsRemovedOnChangePet())
1893 owner->RemovePetAura(pa);
1894 else
1895 CastPetAura(pa);
1899 void Pet::CastPetAura(PetAura const* aura)
1901 uint32 auraId = aura->GetAura(GetEntry());
1902 if(!auraId)
1903 return;
1905 if(auraId == 35696) // Demonic Knowledge
1907 int32 basePoints = int32(aura->GetDamage() * (GetStat(STAT_STAMINA) + GetStat(STAT_INTELLECT)) / 100);
1908 CastCustomSpell(this, auraId, &basePoints, NULL, NULL, true);
1910 else
1911 CastSpell(this, auraId, true);
1914 struct DoPetLearnSpell
1916 DoPetLearnSpell(Pet& _pet) : pet(_pet) {}
1917 void operator() (uint32 spell_id) { pet.learnSpell(spell_id); }
1918 Pet& pet;
1921 void Pet::learnSpellHighRank(uint32 spellid)
1923 learnSpell(spellid);
1925 DoPetLearnSpell worker(*this);
1926 sSpellMgr.doForHighRanks(spellid,worker);
1929 void Pet::SynchronizeLevelWithOwner()
1931 Unit* owner = GetOwner();
1932 if (!owner || owner->GetTypeId() != TYPEID_PLAYER)
1933 return;
1935 switch(getPetType())
1937 // always same level
1938 case SUMMON_PET:
1939 GivePetLevel(owner->getLevel());
1940 break;
1941 // can't be greater owner level
1942 case HUNTER_PET:
1943 if(getLevel() > owner->getLevel())
1945 GivePetLevel(owner->getLevel());
1946 SetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP, sObjectMgr.GetXPForLevel(owner->getLevel())/4);
1947 SetUInt32Value(UNIT_FIELD_PETEXPERIENCE, GetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP)-1);
1949 break;
1950 default:
1951 break;
1955 void Pet::ApplyModeFlags(PetModeFlags mode, bool apply)
1957 if (apply)
1958 m_petModeFlags = PetModeFlags(m_petModeFlags | mode);
1959 else
1960 m_petModeFlags = PetModeFlags(m_petModeFlags & ~mode);
1962 Unit* owner = GetOwner();
1963 if(!owner || owner->GetTypeId()!=TYPEID_PLAYER)
1964 return;
1966 WorldPacket data(SMSG_PET_MODE, 12);
1967 data << uint64(GetGUID());
1968 data << uint32(m_petModeFlags);
1969 ((Player*)owner)->GetSession()->SendPacket(&data);