port over some changes from Trinity version including one possible memory leak fix
[AHbot.git] / src / game / Pet.cpp
blob8010486d23f1cb4e20801dadebed82a1b879dcd8
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
62 for (PetSpellMap::const_iterator i = m_spells.begin(); i != m_spells.end(); ++i)
63 delete i->second;
64 ObjectAccessor::Instance().RemoveObject(this);
67 delete m_declinedname;
70 void Pet::AddToWorld()
72 ///- Register the pet for guid lookup
73 if(!IsInWorld()) ObjectAccessor::Instance().AddObject(this);
74 Unit::AddToWorld();
77 void Pet::RemoveFromWorld()
79 ///- Remove the pet from the accessor
80 if(IsInWorld()) ObjectAccessor::Instance().RemoveObject(this);
81 ///- Don't call the function for Creature, normal mobs + totems go in a different storage
82 Unit::RemoveFromWorld();
85 bool Pet::LoadPetFromDB( Player* owner, uint32 petentry, uint32 petnumber, bool current )
87 m_loading = true;
89 uint32 ownerid = owner->GetGUIDLow();
91 QueryResult *result;
93 if (petnumber)
94 // known petnumber entry 0 1 2(?) 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
95 result = CharacterDatabase.PQuery("SELECT id, entry, owner, modelid, level, exp, Reactstate, talentpoints, slot, name, renamed, curhealth, curmana, curhappiness, abdata, TeachSpelldata, savetime, resettalents_cost, resettalents_time, CreatedBySpell, PetType "
96 "FROM character_pet WHERE owner = '%u' AND id = '%u'",
97 ownerid, petnumber);
98 else if (current)
99 // current pet (slot 0) 0 1 2(?) 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
100 result = CharacterDatabase.PQuery("SELECT id, entry, owner, modelid, level, exp, Reactstate, talentpoints, slot, name, renamed, curhealth, curmana, curhappiness, abdata, TeachSpelldata, savetime, resettalents_cost, resettalents_time, CreatedBySpell, PetType "
101 "FROM character_pet WHERE owner = '%u' AND slot = '%u'",
102 ownerid, PET_SAVE_AS_CURRENT );
103 else if (petentry)
104 // known petentry entry (unique for summoned pet, but non unique for hunter pet (only from current or not stabled pets)
105 // 0 1 2(?) 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
106 result = CharacterDatabase.PQuery("SELECT id, entry, owner, modelid, level, exp, Reactstate, talentpoints, slot, name, renamed, curhealth, curmana, curhappiness, abdata, TeachSpelldata, savetime, resettalents_cost, resettalents_time, CreatedBySpell, PetType "
107 "FROM character_pet WHERE owner = '%u' AND entry = '%u' AND (slot = '%u' OR slot > '%u') ",
108 ownerid, petentry,PET_SAVE_AS_CURRENT,PET_SAVE_LAST_STABLE_SLOT);
109 else
110 // any current or other non-stabled pet (for hunter "call pet")
111 // 0 1 2(?) 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
112 result = CharacterDatabase.PQuery("SELECT id, entry, owner, modelid, level, exp, Reactstate, talentpoints, slot, name, renamed, curhealth, curmana, curhappiness, abdata, TeachSpelldata, savetime, resettalents_cost, resettalents_time, CreatedBySpell, PetType "
113 "FROM character_pet WHERE owner = '%u' AND (slot = '%u' OR slot > '%u') ",
114 ownerid,PET_SAVE_AS_CURRENT,PET_SAVE_LAST_STABLE_SLOT);
116 if(!result)
117 return false;
119 Field *fields = result->Fetch();
121 // update for case of current pet "slot = 0"
122 petentry = fields[1].GetUInt32();
123 if (!petentry)
125 delete result;
126 return false;
129 uint32 summon_spell_id = fields[19].GetUInt32();
130 SpellEntry const* spellInfo = sSpellStore.LookupEntry(summon_spell_id);
132 bool is_temporary_summoned = spellInfo && GetSpellDuration(spellInfo) > 0;
134 // check temporary summoned pets like mage water elemental
135 if (current && is_temporary_summoned)
137 delete result;
138 return false;
141 uint32 pet_number = fields[0].GetUInt32();
143 if (current && owner->IsPetNeedBeTemporaryUnsummoned())
145 owner->SetTemporaryUnsummonedPetNumber(pet_number);
146 delete result;
147 return false;
150 Map *map = owner->GetMap();
151 uint32 guid = objmgr.GenerateLowGuid(HIGHGUID_PET);
152 if (!Create(guid, map, owner->GetPhaseMask(), petentry, pet_number))
154 delete result;
155 return false;
158 float px, py, pz;
159 owner->GetClosePoint(px, py, pz, GetObjectSize(), PET_FOLLOW_DIST, PET_FOLLOW_ANGLE);
161 Relocate(px, py, pz, owner->GetOrientation());
163 if (!IsPositionValid())
165 sLog.outError("Pet (guidlow %d, entry %d) not loaded. Suggested coordinates isn't valid (X: %f Y: %f)",
166 GetGUIDLow(), GetEntry(), GetPositionX(), GetPositionY());
167 delete result;
168 return false;
171 setPetType(PetType(fields[20].GetUInt8()));
172 SetUInt32Value(UNIT_FIELD_FACTIONTEMPLATE, owner->getFaction());
173 SetUInt32Value(UNIT_CREATED_BY_SPELL, summon_spell_id);
175 CreatureInfo const *cinfo = GetCreatureInfo();
176 if (cinfo->type == CREATURE_TYPE_CRITTER)
178 AIM_Initialize();
179 map->Add((Creature*)this);
180 delete result;
181 return true;
184 m_charmInfo->SetPetNumber(pet_number, IsPermanentPetFor(owner));
186 SetOwnerGUID(owner->GetGUID());
187 SetDisplayId(fields[3].GetUInt32());
188 SetNativeDisplayId(fields[3].GetUInt32());
189 uint32 petlevel = fields[4].GetUInt32();
190 SetUInt32Value(UNIT_NPC_FLAGS, 0);
191 SetName(fields[9].GetString());
193 switch (getPetType())
195 case SUMMON_PET:
196 petlevel=owner->getLevel();
198 SetUInt32Value(UNIT_FIELD_BYTES_0, 2048);
199 SetUInt32Value(UNIT_FIELD_FLAGS, UNIT_FLAG_PVP_ATTACKABLE);
200 // this enables popup window (pet dismiss, cancel)
201 break;
202 case HUNTER_PET:
203 SetUInt32Value(UNIT_FIELD_BYTES_0, 0x02020100);
204 SetByteValue(UNIT_FIELD_BYTES_1, 1, fields[7].GetUInt32());
205 SetByteValue(UNIT_FIELD_BYTES_2, 0, SHEATH_STATE_MELEE);
206 SetByteValue(UNIT_FIELD_BYTES_2, 2, fields[10].GetBool() ? UNIT_RENAME_NOT_ALLOWED : UNIT_RENAME_ALLOWED);
208 SetUInt32Value(UNIT_FIELD_FLAGS, UNIT_FLAG_PVP_ATTACKABLE);
209 // this enables popup window (pet abandon, cancel)
210 SetMaxPower(POWER_HAPPINESS, GetCreatePowers(POWER_HAPPINESS));
211 SetPower(POWER_HAPPINESS, fields[13].GetUInt32());
212 setPowerType(POWER_FOCUS);
213 break;
214 default:
215 sLog.outError("Pet have incorrect type (%u) for pet loading.", getPetType());
218 InitStatsForLevel(petlevel);
219 SetUInt32Value(UNIT_FIELD_PET_NAME_TIMESTAMP, time(NULL));
220 SetUInt32Value(UNIT_FIELD_PETEXPERIENCE, fields[5].GetUInt32());
221 SetCreatorGUID(owner->GetGUID());
223 m_charmInfo->SetReactState(ReactStates(fields[6].GetUInt8()));
225 uint32 savedhealth = fields[11].GetUInt32();
226 uint32 savedmana = fields[12].GetUInt32();
228 // set current pet as current
229 // 0=current
230 // 1..MAX_PET_STABLES in stable slot
231 // PET_SAVE_NOT_IN_SLOT(100) = not stable slot (summoning))
232 if (fields[8].GetUInt32() != 0)
234 CharacterDatabase.BeginTransaction();
235 CharacterDatabase.PExecute("UPDATE character_pet SET slot = '%u' WHERE owner = '%u' AND slot = '%u' AND id <> '%u'",
236 PET_SAVE_NOT_IN_SLOT, ownerid, PET_SAVE_AS_CURRENT, m_charmInfo->GetPetNumber());
237 CharacterDatabase.PExecute("UPDATE character_pet SET slot = '%u' WHERE owner = '%u' AND id = '%u'",
238 PET_SAVE_AS_CURRENT, ownerid, m_charmInfo->GetPetNumber());
239 CharacterDatabase.CommitTransaction();
242 if (!is_temporary_summoned)
244 // permanent controlled pets store state in DB
245 Tokens tokens = StrSplit(fields[14].GetString(), " ");
247 if (tokens.size() != 20)
249 delete result;
250 return false;
253 int index;
254 Tokens::iterator iter;
255 for(iter = tokens.begin(), index = 0; index < 10; ++iter, ++index )
257 m_charmInfo->GetActionBarEntry(index)->Type = atol((*iter).c_str());
258 ++iter;
259 m_charmInfo->GetActionBarEntry(index)->SpellOrAction = atol((*iter).c_str());
262 //init teach spells
263 tokens = StrSplit(fields[15].GetString(), " ");
264 for (iter = tokens.begin(), index = 0; index < 4; ++iter, ++index)
266 uint32 tmp = atol((*iter).c_str());
268 ++iter;
270 if(tmp)
271 AddTeachSpell(tmp, atol((*iter).c_str()));
272 else
273 break;
277 // since last save (in seconds)
278 uint32 timediff = (time(NULL) - fields[16].GetUInt32());
280 m_resetTalentsCost = fields[17].GetUInt32();
281 m_resetTalentsTime = fields[18].GetUInt64();
283 delete result;
285 //load spells/cooldowns/auras
286 SetCanModifyStats(true);
287 _LoadAuras(timediff);
289 //init AB
290 if (is_temporary_summoned)
292 // Temporary summoned pets always have initial spell list at load
293 InitPetCreateSpells();
295 else
297 LearnPetPassives();
298 CastPetAuras(current);
301 if (getPetType() == SUMMON_PET && !current) //all (?) summon pets come with full health when called, but not when they are current
303 SetHealth(GetMaxHealth());
304 SetPower(POWER_MANA, GetMaxPower(POWER_MANA));
306 else
308 SetHealth(savedhealth > GetMaxHealth() ? GetMaxHealth() : savedhealth);
309 SetPower(POWER_MANA, savedmana > GetMaxPower(POWER_MANA) ? GetMaxPower(POWER_MANA) : savedmana);
312 AIM_Initialize();
313 map->Add((Creature*)this);
315 // Spells should be loaded after pet is added to map, because in CheckCast is check on it
316 _LoadSpells();
317 _LoadSpellCooldowns();
319 owner->SetPet(this); // in DB stored only full controlled creature
320 sLog.outDebug("New Pet has guid %u", GetGUIDLow());
322 if (owner->GetTypeId() == TYPEID_PLAYER)
324 ((Player*)owner)->PetSpellInitialize();
325 if(((Player*)owner)->GetGroup())
326 ((Player*)owner)->SetGroupUpdateFlag(GROUP_UPDATE_PET);
329 if (owner->GetTypeId() == TYPEID_PLAYER && getPetType() == HUNTER_PET)
331 result = CharacterDatabase.PQuery("SELECT genitive, dative, accusative, instrumental, prepositional FROM character_pet_declinedname WHERE owner = '%u' AND id = '%u'", owner->GetGUIDLow(), GetCharmInfo()->GetPetNumber());
333 if(result)
335 if(m_declinedname)
336 delete m_declinedname;
338 m_declinedname = new DeclinedName;
339 Field *fields2 = result->Fetch();
340 for(int i = 0; i < MAX_DECLINED_NAME_CASES; ++i)
342 m_declinedname->name[i] = fields2[i].GetCppString();
347 m_loading = false;
348 return true;
351 void Pet::SavePetToDB(PetSaveMode mode)
353 if (!GetEntry())
354 return;
356 // save only fully controlled creature
357 if (!isControlled())
358 return;
360 // not save not player pets
361 if(!IS_PLAYER_GUID(GetOwnerGUID()))
362 return;
364 Player* pOwner = (Player*)GetOwner();
365 if (!pOwner)
366 return;
368 // not save pet as current if another pet temporary unsummoned
369 if (mode == PET_SAVE_AS_CURRENT && pOwner->GetTemporaryUnsummonedPetNumber() &&
370 pOwner->GetTemporaryUnsummonedPetNumber() != m_charmInfo->GetPetNumber())
372 // pet will lost anyway at restore temporary unsummoned
373 if(getPetType()==HUNTER_PET)
374 return;
376 // for warlock case
377 mode = PET_SAVE_NOT_IN_SLOT;
380 uint32 curhealth = GetHealth();
381 uint32 curmana = GetPower(POWER_MANA);
383 // stable and not in slot saves
384 if(mode > PET_SAVE_AS_CURRENT)
386 RemoveAllAuras();
388 //only alive hunter pets get auras saved, the others don't
389 if(!(getPetType() == HUNTER_PET && isAlive()))
390 m_Auras.clear();
393 _SaveSpells();
394 _SaveSpellCooldowns();
395 _SaveAuras();
397 // current/stable/not_in_slot
398 if(mode >= PET_SAVE_AS_CURRENT)
400 uint32 owner = GUID_LOPART(GetOwnerGUID());
401 std::string name = m_name;
402 CharacterDatabase.escape_string(name);
403 CharacterDatabase.BeginTransaction();
404 // remove current data
405 CharacterDatabase.PExecute("DELETE FROM character_pet WHERE owner = '%u' AND id = '%u'", owner,m_charmInfo->GetPetNumber() );
407 // prevent duplicate using slot (except PET_SAVE_NOT_IN_SLOT)
408 if(mode <= PET_SAVE_LAST_STABLE_SLOT)
409 CharacterDatabase.PExecute("UPDATE character_pet SET slot = '%u' WHERE owner = '%u' AND slot = '%u'",
410 PET_SAVE_NOT_IN_SLOT, owner, uint32(mode) );
412 // prevent existence another hunter pet in PET_SAVE_AS_CURRENT and PET_SAVE_NOT_IN_SLOT
413 if(getPetType()==HUNTER_PET && (mode==PET_SAVE_AS_CURRENT||mode > PET_SAVE_LAST_STABLE_SLOT))
414 CharacterDatabase.PExecute("DELETE FROM character_pet WHERE owner = '%u' AND (slot = '%u' OR slot > '%u')",
415 owner,PET_SAVE_AS_CURRENT,PET_SAVE_LAST_STABLE_SLOT);
416 // save pet
417 std::ostringstream ss;
418 ss << "INSERT INTO character_pet ( id, entry, owner, modelid, level, exp, Reactstate, talentpoints, slot, name, renamed, curhealth, curmana, curhappiness, abdata, TeachSpelldata, savetime, resettalents_cost, resettalents_time, CreatedBySpell, PetType) "
419 << "VALUES ("
420 << m_charmInfo->GetPetNumber() << ", "
421 << GetEntry() << ", "
422 << owner << ", "
423 << GetNativeDisplayId() << ", "
424 << getLevel() << ", "
425 << GetUInt32Value(UNIT_FIELD_PETEXPERIENCE) << ", "
426 << uint32(m_charmInfo->GetReactState()) << ", "
427 << uint32(GetFreeTalentPoints()) << ", "
428 << uint32(mode) << ", '"
429 << name.c_str() << "', "
430 << uint32((GetByteValue(UNIT_FIELD_BYTES_2, 2) == UNIT_RENAME_ALLOWED)?0:1) << ", "
431 << (curhealth<1?1:curhealth) << ", "
432 << curmana << ", "
433 << GetPower(POWER_HAPPINESS) << ", '";
435 for(uint32 i = 0; i < 10; ++i)
436 ss << uint32(m_charmInfo->GetActionBarEntry(i)->Type) << " " << uint32(m_charmInfo->GetActionBarEntry(i)->SpellOrAction) << " ";
437 ss << "', '";
439 //save spells the pet can teach to it's Master
441 int i = 0;
442 for(TeachSpellMap::const_iterator itr = m_teachspells.begin(); i < 4 && itr != m_teachspells.end(); ++i, ++itr)
443 ss << itr->first << " " << itr->second << " ";
444 for(; i < 4; ++i)
445 ss << uint32(0) << " " << uint32(0) << " ";
448 ss << "', "
449 << time(NULL) << ", "
450 << uint32(m_resetTalentsCost) << ", "
451 << uint64(m_resetTalentsTime) << ", "
452 << GetUInt32Value(UNIT_CREATED_BY_SPELL) << ", "
453 << uint32(getPetType()) << ")";
455 CharacterDatabase.Execute( ss.str().c_str() );
456 CharacterDatabase.CommitTransaction();
458 // delete
459 else
461 RemoveAllAuras();
462 DeleteFromDB(m_charmInfo->GetPetNumber());
466 void Pet::DeleteFromDB(uint32 guidlow)
468 CharacterDatabase.PExecute("DELETE FROM character_pet WHERE id = '%u'", guidlow);
469 CharacterDatabase.PExecute("DELETE FROM character_pet_declinedname WHERE id = '%u'", guidlow);
470 CharacterDatabase.PExecute("DELETE FROM pet_aura WHERE guid = '%u'", guidlow);
471 CharacterDatabase.PExecute("DELETE FROM pet_spell WHERE guid = '%u'", guidlow);
472 CharacterDatabase.PExecute("DELETE FROM pet_spell_cooldown WHERE guid = '%u'", guidlow);
475 void Pet::setDeathState(DeathState s) // overwrite virtual Creature::setDeathState and Unit::setDeathState
477 Creature::setDeathState(s);
478 if(getDeathState()==CORPSE)
480 //remove summoned pet (no corpse)
481 if(getPetType()==SUMMON_PET)
482 Remove(PET_SAVE_NOT_IN_SLOT);
483 // other will despawn at corpse desppawning (Pet::Update code)
484 else
486 // pet corpse non lootable and non skinnable
487 SetUInt32Value( UNIT_DYNAMIC_FLAGS, 0x00 );
488 RemoveFlag (UNIT_FIELD_FLAGS, UNIT_FLAG_SKINNABLE);
490 //lose happiness when died and not in BG/Arena
491 MapEntry const* mapEntry = sMapStore.LookupEntry(GetMapId());
492 if(!mapEntry || (mapEntry->map_type != MAP_ARENA && mapEntry->map_type != MAP_BATTLEGROUND))
493 ModifyPower(POWER_HAPPINESS, -HAPPINESS_LEVEL_SIZE);
495 SetFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_STUNNED);
498 else if(getDeathState()==ALIVE)
500 RemoveFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_STUNNED);
501 CastPetAuras(true);
505 void Pet::Update(uint32 diff)
507 if(m_removed) // pet already removed, just wait in remove queue, no updates
508 return;
510 switch( m_deathState )
512 case CORPSE:
514 if( m_deathTimer <= diff )
516 assert(getPetType()!=SUMMON_PET && "Must be already removed.");
517 Remove(PET_SAVE_NOT_IN_SLOT); //hunters' pets never get removed because of death, NEVER!
518 return;
520 break;
522 case ALIVE:
524 // unsummon pet that lost owner
525 Unit* owner = GetOwner();
526 if(!owner || (!IsWithinDistInMap(owner, OWNER_MAX_DISTANCE) && (owner->GetCharmGUID() && (owner->GetCharmGUID() != GetGUID()))) || (isControlled() && !owner->GetPetGUID()))
528 Remove(PET_SAVE_NOT_IN_SLOT, true);
529 return;
532 if(isControlled())
534 if( owner->GetPetGUID() != GetGUID() )
536 Remove(getPetType()==HUNTER_PET?PET_SAVE_AS_DELETED:PET_SAVE_NOT_IN_SLOT);
537 return;
541 if(m_duration > 0)
543 if(m_duration > diff)
544 m_duration -= diff;
545 else
547 Remove(getPetType() != SUMMON_PET ? PET_SAVE_AS_DELETED:PET_SAVE_NOT_IN_SLOT);
548 return;
552 //regenerate focus for hunter pets or energy for deathknight's ghoul
553 if(m_regenTimer <= diff)
555 switch (getPowerType())
557 case POWER_FOCUS:
558 case POWER_ENERGY:
559 Regenerate(getPowerType());
560 break;
561 default:
562 break;
564 m_regenTimer = 4000;
566 else
567 m_regenTimer -= diff;
569 if(getPetType() != HUNTER_PET)
570 break;
572 if(m_happinessTimer <= diff)
574 LooseHappiness();
575 m_happinessTimer = 7500;
577 else
578 m_happinessTimer -= diff;
580 break;
582 default:
583 break;
585 Creature::Update(diff);
588 void Pet::Regenerate(Powers power)
590 uint32 curValue = GetPower(power);
591 uint32 maxValue = GetMaxPower(power);
593 if (curValue >= maxValue)
594 return;
596 float addvalue = 0.0f;
598 switch (power)
600 case POWER_FOCUS:
602 // For hunter pets.
603 addvalue = 24 * sWorld.getRate(RATE_POWER_FOCUS);
604 break;
606 case POWER_ENERGY:
608 // For deathknight's ghoul.
609 addvalue = 20;
610 break;
612 default:
613 return;
616 // Apply modifiers (if any).
617 AuraList const& ModPowerRegenPCTAuras = GetAurasByType(SPELL_AURA_MOD_POWER_REGEN_PERCENT);
618 for(AuraList::const_iterator i = ModPowerRegenPCTAuras.begin(); i != ModPowerRegenPCTAuras.end(); ++i)
619 if ((*i)->GetModifier()->m_miscvalue == power)
620 addvalue *= ((*i)->GetModifier()->m_amount + 100) / 100.0f;
622 ModifyPower(power, (int32)addvalue);
625 void Pet::LooseHappiness()
627 uint32 curValue = GetPower(POWER_HAPPINESS);
628 if (curValue <= 0)
629 return;
630 int32 addvalue = 670; //value is 70/35/17/8/4 (per min) * 1000 / 8 (timer 7.5 secs)
631 if(isInCombat()) //we know in combat happiness fades faster, multiplier guess
632 addvalue = int32(addvalue * 1.5);
633 ModifyPower(POWER_HAPPINESS, -addvalue);
636 HappinessState Pet::GetHappinessState()
638 if(GetPower(POWER_HAPPINESS) < HAPPINESS_LEVEL_SIZE)
639 return UNHAPPY;
640 else if(GetPower(POWER_HAPPINESS) >= HAPPINESS_LEVEL_SIZE * 2)
641 return HAPPY;
642 else
643 return CONTENT;
646 bool Pet::CanTakeMoreActiveSpells(uint32 spellid)
648 uint8 activecount = 1;
649 uint32 chainstartstore[ACTIVE_SPELLS_MAX];
651 if(IsPassiveSpell(spellid))
652 return true;
654 chainstartstore[0] = spellmgr.GetFirstSpellInChain(spellid);
656 for (PetSpellMap::const_iterator itr = m_spells.begin(); itr != m_spells.end(); ++itr)
658 if(IsPassiveSpell(itr->first))
659 continue;
661 uint32 chainstart = spellmgr.GetFirstSpellInChain(itr->first);
663 uint8 x;
665 for(x = 0; x < activecount; x++)
667 if(chainstart == chainstartstore[x])
668 break;
671 if(x == activecount) //spellchain not yet saved -> add active count
673 ++activecount;
674 if(activecount > ACTIVE_SPELLS_MAX)
675 return false;
676 chainstartstore[x] = chainstart;
679 return true;
682 void Pet::Remove(PetSaveMode mode, bool returnreagent)
684 Unit* owner = GetOwner();
686 if(owner)
688 if(owner->GetTypeId()==TYPEID_PLAYER)
690 ((Player*)owner)->RemovePet(this,mode,returnreagent);
691 return;
694 // only if current pet in slot
695 if(owner->GetPetGUID()==GetGUID())
696 owner->SetPet(0);
699 CleanupsBeforeDelete();
700 AddObjectToRemoveList();
701 m_removed = true;
704 void Pet::GivePetXP(uint32 xp)
706 if(getPetType() != HUNTER_PET)
707 return;
709 if ( xp < 1 )
710 return;
712 if(!isAlive())
713 return;
715 uint32 level = getLevel();
717 // XP to money conversion processed in Player::RewardQuest
718 if(level >= sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL))
719 return;
721 uint32 curXP = GetUInt32Value(UNIT_FIELD_PETEXPERIENCE);
722 uint32 nextLvlXP = GetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP);
723 uint32 newXP = curXP + xp;
725 if(newXP >= nextLvlXP && level+1 > GetOwner()->getLevel())
727 SetUInt32Value(UNIT_FIELD_PETEXPERIENCE, nextLvlXP-1);
728 return;
731 while( newXP >= nextLvlXP && level < sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL) )
733 newXP -= nextLvlXP;
735 SetLevel( level + 1 );
736 SetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP, objmgr.GetXPForLevel(level+1)/4);
738 level = getLevel();
739 nextLvlXP = GetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP);
740 GivePetLevel(level);
743 SetUInt32Value(UNIT_FIELD_PETEXPERIENCE, newXP);
746 void Pet::GivePetLevel(uint32 level)
748 if(!level)
749 return;
751 InitStatsForLevel(level);
752 InitTalentForLevel();
755 bool Pet::CreateBaseAtCreature(Creature* creature)
757 if(!creature)
759 sLog.outError("CRITICAL: NULL pointer parsed into CreateBaseAtCreature()");
760 return false;
762 uint32 guid=objmgr.GenerateLowGuid(HIGHGUID_PET);
764 sLog.outBasic("SetInstanceID()");
765 SetInstanceId(creature->GetInstanceId());
767 sLog.outBasic("Create pet");
768 uint32 pet_number = objmgr.GeneratePetNumber();
769 if(!Create(guid, creature->GetMap(), creature->GetPhaseMask(), creature->GetEntry(), pet_number))
770 return false;
772 Relocate(creature->GetPositionX(), creature->GetPositionY(), creature->GetPositionZ(), creature->GetOrientation());
774 if(!IsPositionValid())
776 sLog.outError("Pet (guidlow %d, entry %d) not created base at creature. Suggested coordinates isn't valid (X: %f Y: %f)",
777 GetGUIDLow(), GetEntry(), GetPositionX(), GetPositionY());
778 return false;
781 CreatureInfo const *cinfo = GetCreatureInfo();
782 if(!cinfo)
784 sLog.outError("CreateBaseAtCreature() failed, creatureInfo is missing!");
785 return false;
788 if(cinfo->type == CREATURE_TYPE_CRITTER)
790 setPetType(MINI_PET);
791 return true;
793 SetDisplayId(creature->GetDisplayId());
794 SetNativeDisplayId(creature->GetNativeDisplayId());
795 SetMaxPower(POWER_HAPPINESS, GetCreatePowers(POWER_HAPPINESS));
796 SetPower(POWER_HAPPINESS, 166500);
797 setPowerType(POWER_FOCUS);
798 SetUInt32Value(UNIT_FIELD_PET_NAME_TIMESTAMP, 0);
799 SetUInt32Value(UNIT_FIELD_PETEXPERIENCE, 0);
800 SetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP, objmgr.GetXPForLevel(creature->getLevel())/4);
801 SetUInt32Value(UNIT_NPC_FLAGS, 0);
803 if(CreatureFamilyEntry const* cFamily = sCreatureFamilyStore.LookupEntry(cinfo->family))
804 SetName(cFamily->Name[sWorld.GetDefaultDbcLocale()]);
805 else
806 SetName(creature->GetNameForLocaleIdx(objmgr.GetDBCLocaleIndex()));
808 if(cinfo->type == CREATURE_TYPE_BEAST)
810 SetUInt32Value(UNIT_FIELD_BYTES_0, 0x02020100);
811 SetByteValue(UNIT_FIELD_BYTES_2, 0, SHEATH_STATE_MELEE );
812 SetByteValue(UNIT_FIELD_BYTES_2, 2, UNIT_RENAME_ALLOWED);
813 SetUInt32Value(UNIT_MOD_CAST_SPEED, creature->GetUInt32Value(UNIT_MOD_CAST_SPEED));
815 return true;
818 bool Pet::InitStatsForLevel(uint32 petlevel)
820 CreatureInfo const *cinfo = GetCreatureInfo();
821 assert(cinfo);
823 Unit* owner = GetOwner();
824 if(!owner)
826 sLog.outError("attempt to summon pet (Entry %u) without owner! Attempt terminated.", cinfo->Entry);
827 return false;
830 uint32 creature_ID = (getPetType() == HUNTER_PET) ? 1 : cinfo->Entry;
832 SetLevel(petlevel);
834 SetMeleeDamageSchool(SpellSchools(cinfo->dmgschool));
836 SetModifierValue(UNIT_MOD_ARMOR, BASE_VALUE, float(petlevel*50));
838 SetAttackTime(BASE_ATTACK, BASE_ATTACK_TIME);
839 SetAttackTime(OFF_ATTACK, BASE_ATTACK_TIME);
840 SetAttackTime(RANGED_ATTACK, BASE_ATTACK_TIME);
842 SetFloatValue(UNIT_MOD_CAST_SPEED, 1.0);
844 CreatureFamilyEntry const* cFamily = sCreatureFamilyStore.LookupEntry(cinfo->family);
845 if(cFamily && cFamily->minScale > 0.0f && getPetType()==HUNTER_PET)
847 float scale;
848 if (getLevel() >= cFamily->maxScaleLevel)
849 scale = cFamily->maxScale;
850 else if (getLevel() <= cFamily->minScaleLevel)
851 scale = cFamily->minScale;
852 else
853 scale = cFamily->minScale + (getLevel() - cFamily->minScaleLevel) / cFamily->maxScaleLevel * (cFamily->maxScale - cFamily->minScale);
855 SetFloatValue(OBJECT_FIELD_SCALE_X, scale);
857 m_bonusdamage = 0;
859 int32 createResistance[MAX_SPELL_SCHOOL] = {0,0,0,0,0,0,0};
861 if(cinfo && getPetType() != HUNTER_PET)
863 createResistance[SPELL_SCHOOL_HOLY] = cinfo->resistance1;
864 createResistance[SPELL_SCHOOL_FIRE] = cinfo->resistance2;
865 createResistance[SPELL_SCHOOL_NATURE] = cinfo->resistance3;
866 createResistance[SPELL_SCHOOL_FROST] = cinfo->resistance4;
867 createResistance[SPELL_SCHOOL_SHADOW] = cinfo->resistance5;
868 createResistance[SPELL_SCHOOL_ARCANE] = cinfo->resistance6;
871 switch(getPetType())
873 case SUMMON_PET:
875 if(owner->GetTypeId() == TYPEID_PLAYER)
877 switch(owner->getClass())
879 case CLASS_WARLOCK:
882 //the damage bonus used for pets is either fire or shadow damage, whatever is higher
883 uint32 fire = owner->GetUInt32Value(PLAYER_FIELD_MOD_DAMAGE_DONE_POS + SPELL_SCHOOL_FIRE);
884 uint32 shadow = owner->GetUInt32Value(PLAYER_FIELD_MOD_DAMAGE_DONE_POS + SPELL_SCHOOL_SHADOW);
885 uint32 val = (fire > shadow) ? fire : shadow;
887 SetBonusDamage(int32 (val * 0.15f));
888 //bonusAP += val * 0.57;
889 break;
891 case CLASS_MAGE:
893 //40% damage bonus of mage's frost damage
894 float val = owner->GetUInt32Value(PLAYER_FIELD_MOD_DAMAGE_DONE_POS + SPELL_SCHOOL_FROST) * 0.4;
895 if(val < 0)
896 val = 0;
897 SetBonusDamage( int32(val));
898 break;
900 default:
901 break;
905 SetBaseWeaponDamage(BASE_ATTACK, MINDAMAGE, float(petlevel - (petlevel / 4)) );
906 SetBaseWeaponDamage(BASE_ATTACK, MAXDAMAGE, float(petlevel + (petlevel / 4)) );
908 //SetModifierValue(UNIT_MOD_ATTACK_POWER, BASE_VALUE, float(cinfo->attackpower));
910 PetLevelInfo const* pInfo = objmgr.GetPetLevelInfo(creature_ID, petlevel);
911 if(pInfo) // exist in DB
913 SetCreateHealth(pInfo->health);
914 SetCreateMana(pInfo->mana);
916 if(pInfo->armor > 0)
917 SetModifierValue(UNIT_MOD_ARMOR, BASE_VALUE, float(pInfo->armor));
919 for(int stat = 0; stat < MAX_STATS; ++stat)
921 SetCreateStat(Stats(stat), float(pInfo->stats[stat]));
924 else // not exist in DB, use some default fake data
926 sLog.outErrorDb("Summoned pet (Entry: %u) not have pet stats data in DB",cinfo->Entry);
928 // remove elite bonuses included in DB values
929 SetCreateHealth(uint32(((float(cinfo->maxhealth) / cinfo->maxlevel) / (1 + 2 * cinfo->rank)) * petlevel) );
930 SetCreateMana( uint32(((float(cinfo->maxmana) / cinfo->maxlevel) / (1 + 2 * cinfo->rank)) * petlevel) );
932 SetCreateStat(STAT_STRENGTH, 22);
933 SetCreateStat(STAT_AGILITY, 22);
934 SetCreateStat(STAT_STAMINA, 25);
935 SetCreateStat(STAT_INTELLECT, 28);
936 SetCreateStat(STAT_SPIRIT, 27);
938 break;
940 case HUNTER_PET:
942 SetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP, objmgr.GetXPForLevel(petlevel)/4);
943 learnLevelupSpells();
944 //these formula may not be correct; however, it is designed to be close to what it should be
945 //this makes dps 0.5 of pets level
946 SetBaseWeaponDamage(BASE_ATTACK, MINDAMAGE, float(petlevel - (petlevel / 4)) );
947 //damage range is then petlevel / 2
948 SetBaseWeaponDamage(BASE_ATTACK, MAXDAMAGE, float(petlevel + (petlevel / 4)) );
949 //damage is increased afterwards as strength and pet scaling modify attack power
951 //stored standard pet stats are entry 1 in pet_levelinfo
952 PetLevelInfo const* pInfo = objmgr.GetPetLevelInfo(creature_ID, petlevel);
953 if(pInfo) // exist in DB
955 SetCreateHealth(pInfo->health);
956 SetModifierValue(UNIT_MOD_ARMOR, BASE_VALUE, float(pInfo->armor));
957 //SetModifierValue(UNIT_MOD_ATTACK_POWER, BASE_VALUE, float(cinfo->attackpower));
959 for( int i = STAT_STRENGTH; i < MAX_STATS; ++i)
961 SetCreateStat(Stats(i), float(pInfo->stats[i]));
964 else // not exist in DB, use some default fake data
966 sLog.outErrorDb("Hunter pet levelstats missing in DB");
968 // remove elite bonuses included in DB values
969 SetCreateHealth( uint32(((float(cinfo->maxhealth) / cinfo->maxlevel) / (1 + 2 * cinfo->rank)) * petlevel) );
971 SetCreateStat(STAT_STRENGTH, 22);
972 SetCreateStat(STAT_AGILITY, 22);
973 SetCreateStat(STAT_STAMINA, 25);
974 SetCreateStat(STAT_INTELLECT, 28);
975 SetCreateStat(STAT_SPIRIT, 27);
977 break;
979 case GUARDIAN_PET:
980 SetUInt32Value(UNIT_FIELD_PETEXPERIENCE, 0);
981 SetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP, 1000);
983 SetCreateMana(28 + 10*petlevel);
984 SetCreateHealth(28 + 30*petlevel);
986 // FIXME: this is wrong formula, possible each guardian pet have own damage formula
987 //these formula may not be correct; however, it is designed to be close to what it should be
988 //this makes dps 0.5 of pets level
989 SetBaseWeaponDamage(BASE_ATTACK, MINDAMAGE, float(petlevel - (petlevel / 4)));
990 //damage range is then petlevel / 2
991 SetBaseWeaponDamage(BASE_ATTACK, MAXDAMAGE, float(petlevel + (petlevel / 4)));
992 break;
993 default:
994 sLog.outError("Pet have incorrect type (%u) for levelup.", getPetType());
995 break;
998 for (int i = SPELL_SCHOOL_HOLY; i < MAX_SPELL_SCHOOL; ++i)
999 SetModifierValue(UnitMods(UNIT_MOD_RESISTANCE_START + i), BASE_VALUE, float(createResistance[i]));
1001 UpdateAllStats();
1003 SetHealth(GetMaxHealth());
1004 SetPower(POWER_MANA, GetMaxPower(POWER_MANA));
1006 return true;
1009 bool Pet::HaveInDiet(ItemPrototype const* item) const
1011 if (!item->FoodType)
1012 return false;
1014 CreatureInfo const* cInfo = GetCreatureInfo();
1015 if(!cInfo)
1016 return false;
1018 CreatureFamilyEntry const* cFamily = sCreatureFamilyStore.LookupEntry(cInfo->family);
1019 if(!cFamily)
1020 return false;
1022 uint32 diet = cFamily->petFoodMask;
1023 uint32 FoodMask = 1 << (item->FoodType-1);
1024 return diet & FoodMask;
1027 uint32 Pet::GetCurrentFoodBenefitLevel(uint32 itemlevel)
1029 // -5 or greater food level
1030 if(getLevel() <= itemlevel + 5) //possible to feed level 60 pet with level 55 level food for full effect
1031 return 35000;
1032 // -10..-6
1033 else if(getLevel() <= itemlevel + 10) //pure guess, but sounds good
1034 return 17000;
1035 // -14..-11
1036 else if(getLevel() <= itemlevel + 14) //level 55 food gets green on 70, makes sense to me
1037 return 8000;
1038 // -15 or less
1039 else
1040 return 0; //food too low level
1043 void Pet::_LoadSpellCooldowns()
1045 m_CreatureSpellCooldowns.clear();
1046 m_CreatureCategoryCooldowns.clear();
1048 QueryResult *result = CharacterDatabase.PQuery("SELECT spell,time FROM pet_spell_cooldown WHERE guid = '%u'",m_charmInfo->GetPetNumber());
1050 if(result)
1052 time_t curTime = time(NULL);
1054 WorldPacket data(SMSG_SPELL_COOLDOWN, (8+1+result->GetRowCount()*8));
1055 data << GetGUID();
1056 data << uint8(0x0); // flags (0x1, 0x2)
1060 Field *fields = result->Fetch();
1062 uint32 spell_id = fields[0].GetUInt32();
1063 time_t db_time = (time_t)fields[1].GetUInt64();
1065 if(!sSpellStore.LookupEntry(spell_id))
1067 sLog.outError("Pet %u have unknown spell %u in `pet_spell_cooldown`, skipping.",m_charmInfo->GetPetNumber(),spell_id);
1068 continue;
1071 // skip outdated cooldown
1072 if(db_time <= curTime)
1073 continue;
1075 data << uint32(spell_id);
1076 data << uint32(uint32(db_time-curTime)*IN_MILISECONDS);
1078 _AddCreatureSpellCooldown(spell_id,db_time);
1080 sLog.outDebug("Pet (Number: %u) spell %u cooldown loaded (%u secs).", m_charmInfo->GetPetNumber(), spell_id, uint32(db_time-curTime));
1082 while( result->NextRow() );
1084 delete result;
1086 if(!m_CreatureSpellCooldowns.empty() && GetOwner())
1088 ((Player*)GetOwner())->GetSession()->SendPacket(&data);
1093 void Pet::_SaveSpellCooldowns()
1095 CharacterDatabase.PExecute("DELETE FROM pet_spell_cooldown WHERE guid = '%u'", m_charmInfo->GetPetNumber());
1097 time_t curTime = time(NULL);
1099 // remove oudated and save active
1100 for(CreatureSpellCooldowns::iterator itr = m_CreatureSpellCooldowns.begin();itr != m_CreatureSpellCooldowns.end();)
1102 if(itr->second <= curTime)
1103 m_CreatureSpellCooldowns.erase(itr++);
1104 else
1106 CharacterDatabase.PExecute("INSERT INTO pet_spell_cooldown (guid,spell,time) VALUES ('%u', '%u', '" I64FMTD "')", m_charmInfo->GetPetNumber(), itr->first, uint64(itr->second));
1107 ++itr;
1112 void Pet::_LoadSpells()
1114 QueryResult *result = CharacterDatabase.PQuery("SELECT spell,active FROM pet_spell WHERE guid = '%u'",m_charmInfo->GetPetNumber());
1116 if(result)
1120 Field *fields = result->Fetch();
1122 addSpell(fields[0].GetUInt32(), fields[1].GetUInt16(), PETSPELL_UNCHANGED);
1124 while( result->NextRow() );
1126 delete result;
1130 void Pet::_SaveSpells()
1132 for (PetSpellMap::const_iterator itr = m_spells.begin(), next = m_spells.begin(); itr != m_spells.end(); itr = next)
1134 ++next;
1135 if (itr->second->type == PETSPELL_FAMILY) continue; // prevent saving family passives to DB
1136 if (itr->second->state == PETSPELL_REMOVED || itr->second->state == PETSPELL_CHANGED)
1137 CharacterDatabase.PExecute("DELETE FROM pet_spell WHERE guid = '%u' and spell = '%u'", m_charmInfo->GetPetNumber(), itr->first);
1138 if (itr->second->state == PETSPELL_NEW || itr->second->state == PETSPELL_CHANGED)
1139 CharacterDatabase.PExecute("INSERT INTO pet_spell (guid,spell,active) VALUES ('%u', '%u', '%u')", m_charmInfo->GetPetNumber(), itr->first, itr->second->active);
1141 if (itr->second->state == PETSPELL_REMOVED)
1142 _removeSpell(itr->first);
1143 else
1144 itr->second->state = PETSPELL_UNCHANGED;
1148 void Pet::_LoadAuras(uint32 timediff)
1150 m_Auras.clear();
1151 for (int i = 0; i < TOTAL_AURAS; ++i)
1152 m_modAuras[i].clear();
1154 QueryResult *result = CharacterDatabase.PQuery("SELECT caster_guid,spell,effect_index,stackcount,amount,maxduration,remaintime,remaincharges FROM pet_aura WHERE guid = '%u'",m_charmInfo->GetPetNumber());
1156 if(result)
1160 Field *fields = result->Fetch();
1161 uint64 caster_guid = fields[0].GetUInt64();
1162 uint32 spellid = fields[1].GetUInt32();
1163 uint32 effindex = fields[2].GetUInt32();
1164 uint32 stackcount= fields[3].GetUInt32();
1165 int32 damage = (int32)fields[4].GetUInt32();
1166 int32 maxduration = (int32)fields[5].GetUInt32();
1167 int32 remaintime = (int32)fields[6].GetUInt32();
1168 int32 remaincharges = (int32)fields[7].GetUInt32();
1170 SpellEntry const* spellproto = sSpellStore.LookupEntry(spellid);
1171 if(!spellproto)
1173 sLog.outError("Unknown aura (spellid %u, effindex %u), ignore.",spellid,effindex);
1174 continue;
1177 if(effindex >= 3)
1179 sLog.outError("Invalid effect index (spellid %u, effindex %u), ignore.",spellid,effindex);
1180 continue;
1183 // negative effects should continue counting down after logout
1184 if (remaintime != -1 && !IsPositiveEffect(spellid, effindex))
1186 if(remaintime <= int32(timediff))
1187 continue;
1189 remaintime -= timediff;
1192 // prevent wrong values of remaincharges
1193 if(spellproto->procCharges)
1195 if(remaincharges <= 0 || remaincharges > spellproto->procCharges)
1196 remaincharges = spellproto->procCharges;
1198 else
1199 remaincharges = -1;
1201 /// do not load single target auras (unless they were cast by the player)
1202 if (caster_guid != GetGUID() && IsSingleTargetSpell(spellproto))
1203 continue;
1205 for(uint32 i=0; i<stackcount; ++i)
1207 Aura* aura = CreateAura(spellproto, effindex, NULL, this, NULL);
1209 if(!damage)
1210 damage = aura->GetModifier()->m_amount;
1211 aura->SetLoadedState(caster_guid,damage,maxduration,remaintime,remaincharges);
1212 AddAura(aura);
1215 while( result->NextRow() );
1217 delete result;
1221 void Pet::_SaveAuras()
1223 CharacterDatabase.PExecute("DELETE FROM pet_aura WHERE guid = '%u'", m_charmInfo->GetPetNumber());
1225 AuraMap const& auras = GetAuras();
1226 if (auras.empty())
1227 return;
1229 spellEffectPair lastEffectPair = auras.begin()->first;
1230 uint32 stackCounter = 1;
1232 for(AuraMap::const_iterator itr = auras.begin(); ; ++itr)
1234 if(itr == auras.end() || lastEffectPair != itr->first)
1236 AuraMap::const_iterator itr2 = itr;
1237 // save previous spellEffectPair to db
1238 itr2--;
1239 SpellEntry const *spellInfo = itr2->second->GetSpellProto();
1240 /// do not save single target auras (unless they were cast by the player)
1241 if (!(itr2->second->GetCasterGUID() != GetGUID() && IsSingleTargetSpell(spellInfo)))
1243 if(!itr2->second->IsPassive())
1245 // skip all auras from spell that apply at cast SPELL_AURA_MOD_SHAPESHIFT or pet area auras.
1246 uint8 i;
1247 for (i = 0; i < 3; ++i)
1248 if (spellInfo->EffectApplyAuraName[i] == SPELL_AURA_MOD_STEALTH ||
1249 spellInfo->Effect[i] == SPELL_EFFECT_APPLY_AREA_AURA_OWNER ||
1250 spellInfo->Effect[i] == SPELL_EFFECT_APPLY_AREA_AURA_PET )
1251 break;
1253 if (i == 3)
1255 CharacterDatabase.PExecute("INSERT INTO pet_aura (guid,caster_guid,spell,effect_index,stackcount,amount,maxduration,remaintime,remaincharges) "
1256 "VALUES ('%u', '" I64FMTD "', '%u', '%u', '%u', '%d', '%d', '%d', '%d')",
1257 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()));
1261 if(itr == auras.end())
1262 break;
1265 if (lastEffectPair == itr->first)
1266 stackCounter++;
1267 else
1269 lastEffectPair = itr->first;
1270 stackCounter = 1;
1275 bool Pet::addSpell(uint32 spell_id, uint16 active, PetSpellState state, PetSpellType type)
1277 SpellEntry const *spellInfo = sSpellStore.LookupEntry(spell_id);
1278 if (!spellInfo)
1280 // do pet spell book cleanup
1281 if(state == PETSPELL_UNCHANGED) // spell load case
1283 sLog.outError("Pet::addSpell: Non-existed in SpellStore spell #%u request, deleting for all pets in `pet_spell`.",spell_id);
1284 CharacterDatabase.PExecute("DELETE FROM pet_spell WHERE spell = '%u'",spell_id);
1286 else
1287 sLog.outError("Pet::addSpell: Non-existed in SpellStore spell #%u request.",spell_id);
1289 return false;
1292 PetSpellMap::iterator itr = m_spells.find(spell_id);
1293 if (itr != m_spells.end())
1295 if (itr->second->state == PETSPELL_REMOVED)
1297 delete itr->second;
1298 m_spells.erase(itr);
1299 state = PETSPELL_CHANGED;
1301 else if (state == PETSPELL_UNCHANGED && itr->second->state != PETSPELL_UNCHANGED)
1303 // can be in case spell loading but learned at some previous spell loading
1304 itr->second->state = PETSPELL_UNCHANGED;
1306 if(active == ACT_ENABLED)
1307 ToggleAutocast(spell_id, true);
1308 else if(active == ACT_DISABLED)
1309 ToggleAutocast(spell_id, false);
1311 return false;
1313 else
1314 return false;
1317 uint32 oldspell_id = 0;
1319 PetSpell *newspell = new PetSpell;
1320 newspell->state = state;
1321 newspell->type = type;
1323 if(active == ACT_DECIDE) //active was not used before, so we save it's autocast/passive state here
1325 if(IsPassiveSpell(spell_id))
1326 newspell->active = ACT_PASSIVE;
1327 else
1328 newspell->active = ACT_DISABLED;
1330 else
1331 newspell->active = active;
1333 // talent: unlearn all other talent ranks (high and low)
1334 if(TalentSpellPos const* talentPos = GetTalentSpellPos(spell_id))
1336 if(TalentEntry const *talentInfo = sTalentStore.LookupEntry( talentPos->talent_id ))
1338 for(int i=0; i < MAX_TALENT_RANK; ++i)
1340 // skip learning spell and no rank spell case
1341 uint32 rankSpellId = talentInfo->RankID[i];
1342 if(!rankSpellId || rankSpellId==spell_id)
1343 continue;
1345 // skip unknown ranks
1346 if(!HasSpell(rankSpellId))
1347 continue;
1348 removeSpell(rankSpellId);
1352 else if(uint32 chainstart = spellmgr.GetFirstSpellInChain(spell_id))
1354 for (PetSpellMap::const_iterator itr2 = m_spells.begin(); itr2 != m_spells.end(); ++itr2)
1356 if(itr2->second->state == PETSPELL_REMOVED) continue;
1358 if(spellmgr.GetFirstSpellInChain(itr2->first) == chainstart)
1360 newspell->active = itr2->second->active;
1362 if(newspell->active == ACT_ENABLED)
1363 ToggleAutocast(itr2->first, false);
1365 oldspell_id = itr2->first;
1366 unlearnSpell(itr2->first);
1367 break;
1372 m_spells[spell_id] = newspell;
1374 if (IsPassiveSpell(spell_id))
1375 CastSpell(this, spell_id, true);
1376 else if(state == PETSPELL_NEW)
1377 m_charmInfo->AddSpellToAB(oldspell_id, spell_id);
1379 if(newspell->active == ACT_ENABLED)
1380 ToggleAutocast(spell_id, true);
1382 uint32 talentCost = GetTalentSpellCost(spell_id);
1383 if (talentCost)
1385 int32 free_points = GetMaxTalentPointsForLevel(getLevel());
1386 m_usedTalentCount+=talentCost;
1387 // update free talent points
1388 free_points-=m_usedTalentCount;
1389 SetFreeTalentPoints(free_points > 0 ? free_points : 0);
1391 return true;
1394 bool Pet::learnSpell(uint32 spell_id)
1396 // prevent duplicated entires in spell book
1397 if (!addSpell(spell_id))
1398 return false;
1400 Unit* owner = GetOwner();
1401 if(owner && owner->GetTypeId() == TYPEID_PLAYER)
1403 if(!m_loading)
1405 WorldPacket data(SMSG_PET_LEARNED_SPELL, 2);
1406 data << uint16(spell_id);
1407 ((Player*)owner)->GetSession()->SendPacket(&data);
1409 ((Player*)owner)->PetSpellInitialize();
1411 return true;
1414 void Pet::learnLevelupSpells()
1416 PetLevelupSpellSet const *levelupSpells = spellmgr.GetPetLevelupSpellList(GetCreatureInfo()->family);
1417 if(!levelupSpells)
1418 return;
1420 uint32 level = getLevel();
1422 for(PetLevelupSpellSet::const_iterator itr = levelupSpells->begin(); itr != levelupSpells->end(); ++itr)
1424 if(itr->first <= level)
1425 learnSpell(itr->second);
1426 else
1427 unlearnSpell(itr->second);
1431 bool Pet::unlearnSpell(uint32 spell_id)
1433 if(removeSpell(spell_id))
1435 if(GetOwner()->GetTypeId() == TYPEID_PLAYER)
1437 if(!m_loading)
1439 WorldPacket data(SMSG_PET_REMOVED_SPELL, 2);
1440 data << uint16(spell_id);
1441 ((Player*)GetOwner())->GetSession()->SendPacket(&data);
1444 return true;
1446 return false;
1449 bool Pet::removeSpell(uint32 spell_id)
1451 PetSpellMap::iterator itr = m_spells.find(spell_id);
1452 if (itr == m_spells.end())
1453 return false;
1455 if(itr->second->state == PETSPELL_REMOVED)
1456 return false;
1458 if(itr->second->state == PETSPELL_NEW)
1460 delete itr->second;
1461 m_spells.erase(itr);
1463 else
1464 itr->second->state = PETSPELL_REMOVED;
1466 RemoveAurasDueToSpell(spell_id);
1468 uint32 talentCost = GetTalentSpellCost(spell_id);
1469 if (talentCost > 0)
1471 if (m_usedTalentCount > talentCost)
1472 m_usedTalentCount-=talentCost;
1473 else
1474 m_usedTalentCount = 0;
1475 // update free talent points
1476 int32 free_points = GetMaxTalentPointsForLevel(getLevel()) - m_usedTalentCount;
1477 SetFreeTalentPoints(free_points > 0 ? free_points : 0);
1480 return true;
1483 bool Pet::_removeSpell(uint32 spell_id)
1485 PetSpellMap::iterator itr = m_spells.find(spell_id);
1486 if (itr != m_spells.end())
1488 delete itr->second;
1489 m_spells.erase(itr);
1490 return true;
1492 return false;
1495 void Pet::InitPetCreateSpells()
1497 m_charmInfo->InitPetActionBar();
1499 for (PetSpellMap::const_iterator i = m_spells.begin(); i != m_spells.end(); ++i)
1500 delete i->second;
1501 m_spells.clear();
1503 uint32 petspellid;
1504 PetCreateSpellEntry const* CreateSpells = objmgr.GetPetCreateSpellEntry(GetEntry());
1505 if(CreateSpells)
1507 for(uint8 i = 0; i < 4; ++i)
1509 if(!CreateSpells->spellid[i])
1510 break;
1512 SpellEntry const *learn_spellproto = sSpellStore.LookupEntry(CreateSpells->spellid[i]);
1513 if(!learn_spellproto)
1514 continue;
1516 if(learn_spellproto->Effect[0] == SPELL_EFFECT_LEARN_SPELL || learn_spellproto->Effect[0] == SPELL_EFFECT_LEARN_PET_SPELL)
1518 petspellid = learn_spellproto->EffectTriggerSpell[0];
1519 Unit* owner = GetOwner();
1520 if(owner->GetTypeId() == TYPEID_PLAYER && !((Player*)owner)->HasSpell(learn_spellproto->Id))
1522 if(IsPassiveSpell(petspellid)) //learn passive skills when tamed, not sure if thats right
1523 ((Player*)owner)->learnSpell(learn_spellproto->Id,false);
1524 else
1525 AddTeachSpell(learn_spellproto->EffectTriggerSpell[0], learn_spellproto->Id);
1528 else
1529 petspellid = learn_spellproto->Id;
1531 addSpell(petspellid);
1535 LearnPetPassives();
1537 CastPetAuras(false);
1540 void Pet::CheckLearning(uint32 spellid)
1542 //charmed case -> prevent crash
1543 if(GetTypeId() == TYPEID_PLAYER || getPetType() != HUNTER_PET)
1544 return;
1546 Unit* owner = GetOwner();
1548 if(m_teachspells.empty() || !owner || owner->GetTypeId() != TYPEID_PLAYER)
1549 return;
1551 TeachSpellMap::iterator itr = m_teachspells.find(spellid);
1552 if(itr == m_teachspells.end())
1553 return;
1555 if(urand(0, 100) < 10)
1557 ((Player*)owner)->learnSpell(itr->second,false);
1558 m_teachspells.erase(itr);
1562 bool Pet::resetTalents(bool no_cost)
1564 Unit *owner = GetOwner();
1565 if (!owner || owner->GetTypeId()!=TYPEID_PLAYER)
1566 return false;
1568 CreatureInfo const * ci = GetCreatureInfo();
1569 if(!ci)
1570 return false;
1571 // Check pet talent type
1572 CreatureFamilyEntry const *pet_family = sCreatureFamilyStore.LookupEntry(ci->family);
1573 if(!pet_family || pet_family->petTalentType < 0)
1574 return false;
1576 Player *player = (Player *)owner;
1578 uint32 level = getLevel();
1579 uint32 talentPointsForLevel = GetMaxTalentPointsForLevel(level);
1581 if (m_usedTalentCount == 0)
1583 SetFreeTalentPoints(talentPointsForLevel);
1584 return false;
1587 uint32 cost = 0;
1589 if(!no_cost)
1591 cost = resetTalentsCost();
1593 if (player->GetMoney() < cost)
1595 player->SendBuyError( BUY_ERR_NOT_ENOUGHT_MONEY, 0, 0, 0);
1596 return false;
1600 for (unsigned int i = 0; i < sTalentStore.GetNumRows(); ++i)
1602 TalentEntry const *talentInfo = sTalentStore.LookupEntry(i);
1604 if (!talentInfo) continue;
1606 TalentTabEntry const *talentTabInfo = sTalentTabStore.LookupEntry( talentInfo->TalentTab );
1608 if(!talentTabInfo)
1609 continue;
1611 // unlearn only talents for pets family talent type
1612 if(!((1 << pet_family->petTalentType) & talentTabInfo->petTalentMask))
1613 continue;
1615 for (int j = 0; j < MAX_TALENT_RANK; j++)
1617 for(PetSpellMap::const_iterator itr = m_spells.begin(); itr != m_spells.end();)
1619 if(itr->second->state == PETSPELL_REMOVED)
1621 ++itr;
1622 continue;
1624 // remove learned spells (all ranks)
1625 uint32 itrFirstId = spellmgr.GetFirstSpellInChain(itr->first);
1627 // unlearn if first rank is talent or learned by talent
1628 if (itrFirstId == talentInfo->RankID[j] || spellmgr.IsSpellLearnToSpell(talentInfo->RankID[j],itrFirstId))
1630 removeSpell(itr->first);
1631 itr = m_spells.begin();
1632 continue;
1634 else
1635 ++itr;
1640 SetFreeTalentPoints(talentPointsForLevel);
1642 if(!no_cost)
1644 player->ModifyMoney(-(int32)cost);
1646 m_resetTalentsCost = cost;
1647 m_resetTalentsTime = time(NULL);
1649 player->PetSpellInitialize();
1650 return true;
1653 void Pet::InitTalentForLevel()
1655 uint32 level = getLevel();
1656 uint32 talentPointsForLevel = GetMaxTalentPointsForLevel(level);
1657 // Reset talents in case low level (on level down) or wrong points for level (hunter can unlearn TP increase talent)
1658 if(talentPointsForLevel == 0 || m_usedTalentCount > talentPointsForLevel)
1660 // Remove all talent points
1661 resetTalents(true);
1663 SetFreeTalentPoints(talentPointsForLevel - m_usedTalentCount);
1665 Unit *owner = GetOwner();
1666 if (!owner || owner->GetTypeId() != TYPEID_PLAYER)
1667 return;
1669 ((Player*)owner)->SendTalentsInfoData(true);
1672 uint32 Pet::resetTalentsCost() const
1674 uint32 days = (sWorld.GetGameTime() - m_resetTalentsTime)/DAY;
1676 // The first time reset costs 10 silver; after 1 day cost is reset to 10 silver
1677 if(m_resetTalentsCost < 10*SILVER || days > 0)
1678 return 10*SILVER;
1679 // then 50 silver
1680 else if(m_resetTalentsCost < 50*SILVER)
1681 return 50*SILVER;
1682 // then 1 gold
1683 else if(m_resetTalentsCost < 1*GOLD)
1684 return 1*GOLD;
1685 // then increasing at a rate of 1 gold; cap 10 gold
1686 else
1687 return (m_resetTalentsCost + 1*GOLD > 10*GOLD ? 10*GOLD : m_resetTalentsCost + 1*GOLD);
1690 uint8 Pet::GetMaxTalentPointsForLevel(uint32 level)
1692 uint8 points = (level >= 20) ? ((level - 16) / 4) : 0;
1693 // Mod points from owner SPELL_AURA_MOD_PET_TALENT_POINTS
1694 if (Unit *owner = GetOwner())
1695 points+=owner->GetTotalAuraModifier(SPELL_AURA_MOD_PET_TALENT_POINTS);
1696 return points;
1699 void Pet::ToggleAutocast(uint32 spellid, bool apply)
1701 if(IsPassiveSpell(spellid))
1702 return;
1704 PetSpellMap::const_iterator itr = m_spells.find(spellid);
1706 int i;
1708 if(apply)
1710 for (i = 0; i < m_autospells.size() && m_autospells[i] != spellid; ++i)
1711 ; // just search
1713 if (i == m_autospells.size())
1715 m_autospells.push_back(spellid);
1717 if(itr->second->active != ACT_ENABLED)
1719 itr->second->active = ACT_ENABLED;
1720 if(itr->second->state != PETSPELL_NEW)
1721 itr->second->state = PETSPELL_CHANGED;
1725 else
1727 AutoSpellList::iterator itr2 = m_autospells.begin();
1728 for (i = 0; i < m_autospells.size() && m_autospells[i] != spellid; ++i, itr2++)
1729 ; // just search
1731 if (i < m_autospells.size())
1733 m_autospells.erase(itr2);
1734 if(itr->second->active != ACT_DISABLED)
1736 itr->second->active = ACT_DISABLED;
1737 if(itr->second->state != PETSPELL_NEW)
1738 itr->second->state = PETSPELL_CHANGED;
1744 bool Pet::IsPermanentPetFor(Player* owner)
1746 switch(getPetType())
1748 case SUMMON_PET:
1749 switch(owner->getClass())
1751 case CLASS_WARLOCK:
1752 return GetCreatureInfo()->type == CREATURE_TYPE_DEMON;
1753 case CLASS_DEATH_KNIGHT:
1754 return GetCreatureInfo()->type == CREATURE_TYPE_UNDEAD;
1755 default:
1756 return false;
1758 case HUNTER_PET:
1759 return true;
1760 default:
1761 return false;
1765 bool Pet::Create(uint32 guidlow, Map *map, uint32 phaseMask, uint32 Entry, uint32 pet_number)
1767 SetMapId(map->GetId());
1768 SetInstanceId(map->GetInstanceId());
1769 SetPhaseMask(phaseMask,false);
1771 Object::_Create(guidlow, pet_number, HIGHGUID_PET);
1773 m_DBTableGuid = guidlow;
1774 m_originalEntry = Entry;
1776 if(!InitEntry(Entry))
1777 return false;
1779 SetByteValue(UNIT_FIELD_BYTES_2, 0, SHEATH_STATE_MELEE);
1781 if(getPetType() == MINI_PET) // always non-attackable
1782 SetFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_NON_ATTACKABLE);
1784 return true;
1787 bool Pet::HasSpell(uint32 spell) const
1789 PetSpellMap::const_iterator itr = m_spells.find(spell);
1790 return (itr != m_spells.end() && itr->second->state != PETSPELL_REMOVED );
1793 // Get all passive spells in our skill line
1794 void Pet::LearnPetPassives()
1796 CreatureInfo const* cInfo = GetCreatureInfo();
1797 if(!cInfo)
1798 return;
1800 CreatureFamilyEntry const* cFamily = sCreatureFamilyStore.LookupEntry(cInfo->family);
1801 if(!cFamily)
1802 return;
1804 PetFamilySpellsStore::const_iterator petStore = sPetFamilySpellsStore.find(cFamily->ID);
1805 if(petStore != sPetFamilySpellsStore.end())
1807 for(PetFamilySpellsSet::const_iterator petSet = petStore->second.begin(); petSet != petStore->second.end(); ++petSet)
1808 addSpell(*petSet, ACT_DECIDE, PETSPELL_NEW, PETSPELL_FAMILY);
1812 void Pet::CastPetAuras(bool current)
1814 Unit* owner = GetOwner();
1815 if(!owner || owner->GetTypeId()!=TYPEID_PLAYER)
1816 return;
1818 if(!IsPermanentPetFor((Player*)owner))
1819 return;
1821 for(PetAuraSet::const_iterator itr = owner->m_petAuras.begin(); itr != owner->m_petAuras.end();)
1823 PetAura const* pa = *itr;
1824 ++itr;
1826 if(!current && pa->IsRemovedOnChangePet())
1827 owner->RemovePetAura(pa);
1828 else
1829 CastPetAura(pa);
1833 void Pet::CastPetAura(PetAura const* aura)
1835 uint16 auraId = aura->GetAura(GetEntry());
1836 if(!auraId)
1837 return;
1839 if(auraId == 35696) // Demonic Knowledge
1841 int32 basePoints = int32(aura->GetDamage() * (GetStat(STAT_STAMINA) + GetStat(STAT_INTELLECT)) / 100);
1842 CastCustomSpell(this, auraId, &basePoints, NULL, NULL, true);
1844 else
1845 CastSpell(this, auraId, true);
1848 void Pet::learnSpellHighRank(uint32 spellid)
1850 learnSpell(spellid);
1852 SpellChainMapNext const& nextMap = spellmgr.GetSpellChainNext();
1853 for(SpellChainMapNext::const_iterator itr = nextMap.lower_bound(spellid); itr != nextMap.upper_bound(spellid); ++itr)
1854 learnSpellHighRank(itr->second);