[7918] Improve portability in work with uint64 string format specifiers and in code...
[getmangos.git] / src / game / Pet.cpp
blob1b1905110d8752f83aa020e33742eb6bf198ae01
1 /*
2 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "Common.h"
20 #include "Database/DatabaseEnv.h"
21 #include "Log.h"
22 #include "WorldPacket.h"
23 #include "ObjectMgr.h"
24 #include "SpellMgr.h"
25 #include "Pet.h"
26 #include "Formulas.h"
27 #include "SpellAuras.h"
28 #include "CreatureAI.h"
29 #include "Unit.h"
30 #include "Util.h"
32 char const* petTypeSuffix[MAX_PET_TYPE] =
34 "'s Minion", // SUMMON_PET
35 "'s Pet", // HUNTER_PET
36 "'s Guardian", // GUARDIAN_PET
37 "'s Companion" // MINI_PET
40 Pet::Pet(PetType type) :
41 Creature(), m_removed(false), m_petType(type), m_happinessTimer(7500), m_duration(0), m_resetTalentsCost(0),
42 m_bonusdamage(0), m_resetTalentsTime(0), m_usedTalentCount(0), m_auraUpdateMask(0), m_loading(false),
43 m_declinedname(NULL)
45 m_isPet = true;
46 m_name = "Pet";
47 m_regenTimer = 4000;
49 // pets always have a charminfo, even if they are not actually charmed
50 CharmInfo* charmInfo = InitCharmInfo(this);
52 if(type == MINI_PET) // always passive
53 charmInfo->SetReactState(REACT_PASSIVE);
54 else if(type == GUARDIAN_PET) // always aggressive
55 charmInfo->SetReactState(REACT_AGGRESSIVE);
58 Pet::~Pet()
60 if(m_uint32Values) // only for fully created Object
61 ObjectAccessor::Instance().RemoveObject(this);
63 delete m_declinedname;
66 void Pet::AddToWorld()
68 ///- Register the pet for guid lookup
69 if(!IsInWorld()) ObjectAccessor::Instance().AddObject(this);
70 Unit::AddToWorld();
73 void Pet::RemoveFromWorld()
75 ///- Remove the pet from the accessor
76 if(IsInWorld()) ObjectAccessor::Instance().RemoveObject(this);
77 ///- Don't call the function for Creature, normal mobs + totems go in a different storage
78 Unit::RemoveFromWorld();
81 bool Pet::LoadPetFromDB( Player* owner, uint32 petentry, uint32 petnumber, bool current )
83 m_loading = true;
85 uint32 ownerid = owner->GetGUIDLow();
87 QueryResult *result;
89 if (petnumber)
90 // known petnumber entry 0 1 2(?) 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
91 result = CharacterDatabase.PQuery("SELECT id, entry, owner, modelid, level, exp, Reactstate, slot, name, renamed, curhealth, curmana, curhappiness, abdata, savetime, resettalents_cost, resettalents_time, CreatedBySpell, PetType "
92 "FROM character_pet WHERE owner = '%u' AND id = '%u'",
93 ownerid, petnumber);
94 else if (current)
95 // current pet (slot 0) 0 1 2(?) 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
96 result = CharacterDatabase.PQuery("SELECT id, entry, owner, modelid, level, exp, Reactstate, slot, name, renamed, curhealth, curmana, curhappiness, abdata, savetime, resettalents_cost, resettalents_time, CreatedBySpell, PetType "
97 "FROM character_pet WHERE owner = '%u' AND slot = '%u'",
98 ownerid, PET_SAVE_AS_CURRENT );
99 else if (petentry)
100 // known petentry entry (unique for summoned pet, but non unique for hunter pet (only from current or not stabled pets)
101 // 0 1 2(?) 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
102 result = CharacterDatabase.PQuery("SELECT id, entry, owner, modelid, level, exp, Reactstate, slot, name, renamed, curhealth, curmana, curhappiness, abdata, savetime, resettalents_cost, resettalents_time, CreatedBySpell, PetType "
103 "FROM character_pet WHERE owner = '%u' AND entry = '%u' AND (slot = '%u' OR slot > '%u') ",
104 ownerid, petentry,PET_SAVE_AS_CURRENT,PET_SAVE_LAST_STABLE_SLOT);
105 else
106 // any current or other non-stabled pet (for hunter "call pet")
107 // 0 1 2(?) 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
108 result = CharacterDatabase.PQuery("SELECT id, entry, owner, modelid, level, exp, Reactstate, slot, name, renamed, curhealth, curmana, curhappiness, abdata, savetime, resettalents_cost, resettalents_time, CreatedBySpell, PetType "
109 "FROM character_pet WHERE owner = '%u' AND (slot = '%u' OR slot > '%u') ",
110 ownerid,PET_SAVE_AS_CURRENT,PET_SAVE_LAST_STABLE_SLOT);
112 if(!result)
113 return false;
115 Field *fields = result->Fetch();
117 // update for case of current pet "slot = 0"
118 petentry = fields[1].GetUInt32();
119 if (!petentry)
121 delete result;
122 return false;
125 uint32 summon_spell_id = fields[17].GetUInt32();
126 SpellEntry const* spellInfo = sSpellStore.LookupEntry(summon_spell_id);
128 bool is_temporary_summoned = spellInfo && GetSpellDuration(spellInfo) > 0;
130 // check temporary summoned pets like mage water elemental
131 if (current && is_temporary_summoned)
133 delete result;
134 return false;
137 uint32 pet_number = fields[0].GetUInt32();
139 if (current && owner->IsPetNeedBeTemporaryUnsummoned())
141 owner->SetTemporaryUnsummonedPetNumber(pet_number);
142 delete result;
143 return false;
146 Map *map = owner->GetMap();
147 uint32 guid = objmgr.GenerateLowGuid(HIGHGUID_PET);
148 if (!Create(guid, map, owner->GetPhaseMask(), petentry, pet_number))
150 delete result;
151 return false;
154 float px, py, pz;
155 owner->GetClosePoint(px, py, pz, GetObjectSize(), PET_FOLLOW_DIST, PET_FOLLOW_ANGLE);
157 Relocate(px, py, pz, owner->GetOrientation());
159 if (!IsPositionValid())
161 sLog.outError("Pet (guidlow %d, entry %d) not loaded. Suggested coordinates isn't valid (X: %f Y: %f)",
162 GetGUIDLow(), GetEntry(), GetPositionX(), GetPositionY());
163 delete result;
164 return false;
167 setPetType(PetType(fields[18].GetUInt8()));
168 SetUInt32Value(UNIT_FIELD_FACTIONTEMPLATE, owner->getFaction());
169 SetUInt32Value(UNIT_CREATED_BY_SPELL, summon_spell_id);
171 CreatureInfo const *cinfo = GetCreatureInfo();
172 if (cinfo->type == CREATURE_TYPE_CRITTER)
174 AIM_Initialize();
175 map->Add((Creature*)this);
176 delete result;
177 return true;
180 m_charmInfo->SetPetNumber(pet_number, IsPermanentPetFor(owner));
182 SetOwnerGUID(owner->GetGUID());
183 SetDisplayId(fields[3].GetUInt32());
184 SetNativeDisplayId(fields[3].GetUInt32());
185 uint32 petlevel = fields[4].GetUInt32();
186 SetUInt32Value(UNIT_NPC_FLAGS, 0);
187 SetName(fields[8].GetString());
189 switch (getPetType())
191 case SUMMON_PET:
192 petlevel=owner->getLevel();
194 SetUInt32Value(UNIT_FIELD_BYTES_0, 2048);
195 SetUInt32Value(UNIT_FIELD_FLAGS, UNIT_FLAG_PVP_ATTACKABLE);
196 // this enables popup window (pet dismiss, cancel)
197 break;
198 case HUNTER_PET:
199 SetUInt32Value(UNIT_FIELD_BYTES_0, 0x02020100);
200 SetSheath(SHEATH_STATE_MELEE);
201 SetByteValue(UNIT_FIELD_BYTES_2, 2, fields[9].GetBool() ? UNIT_RENAME_NOT_ALLOWED : UNIT_RENAME_ALLOWED);
203 SetUInt32Value(UNIT_FIELD_FLAGS, UNIT_FLAG_PVP_ATTACKABLE);
204 // this enables popup window (pet abandon, cancel)
205 SetMaxPower(POWER_HAPPINESS, GetCreatePowers(POWER_HAPPINESS));
206 SetPower(POWER_HAPPINESS, fields[12].GetUInt32());
207 setPowerType(POWER_FOCUS);
208 break;
209 default:
210 sLog.outError("Pet have incorrect type (%u) for pet loading.", getPetType());
213 if(owner->IsPvP())
214 SetPvP(true);
216 InitStatsForLevel(petlevel);
217 InitTalentForLevel(); // set original talents points before spell loading
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[10].GetUInt32();
226 uint32 savedmana = fields[11].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[7].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 if(!m_charmInfo->LoadActionBar(fields[13].GetCppString()))
246 delete result;
247 return false;
251 // since last save (in seconds)
252 uint32 timediff = (time(NULL) - fields[14].GetUInt32());
254 m_resetTalentsCost = fields[15].GetUInt32();
255 m_resetTalentsTime = fields[16].GetUInt64();
257 delete result;
259 //load spells/cooldowns/auras
260 SetCanModifyStats(true);
261 _LoadAuras(timediff);
263 //init AB
264 if (is_temporary_summoned)
266 // Temporary summoned pets always have initial spell list at load
267 InitPetCreateSpells();
269 else
271 LearnPetPassives();
272 CastPetAuras(current);
275 if (getPetType() == SUMMON_PET && !current) //all (?) summon pets come with full health when called, but not when they are current
277 SetHealth(GetMaxHealth());
278 SetPower(POWER_MANA, GetMaxPower(POWER_MANA));
280 else
282 SetHealth(savedhealth > GetMaxHealth() ? GetMaxHealth() : savedhealth);
283 SetPower(POWER_MANA, savedmana > GetMaxPower(POWER_MANA) ? GetMaxPower(POWER_MANA) : savedmana);
286 AIM_Initialize();
287 map->Add((Creature*)this);
289 // Spells should be loaded after pet is added to map, because in CheckCast is check on it
290 _LoadSpells();
291 InitLevelupSpellsForLevel();
293 CleanupActionBar(); // remove unknown spells from action bar after load
295 _LoadSpellCooldowns();
297 owner->SetPet(this); // in DB stored only full controlled creature
298 sLog.outDebug("New Pet has guid %u", GetGUIDLow());
300 if (owner->GetTypeId() == TYPEID_PLAYER)
302 ((Player*)owner)->PetSpellInitialize();
303 if(((Player*)owner)->GetGroup())
304 ((Player*)owner)->SetGroupUpdateFlag(GROUP_UPDATE_PET);
307 if (owner->GetTypeId() == TYPEID_PLAYER && getPetType() == HUNTER_PET)
309 result = CharacterDatabase.PQuery("SELECT genitive, dative, accusative, instrumental, prepositional FROM character_pet_declinedname WHERE owner = '%u' AND id = '%u'", owner->GetGUIDLow(), GetCharmInfo()->GetPetNumber());
311 if(result)
313 if(m_declinedname)
314 delete m_declinedname;
316 m_declinedname = new DeclinedName;
317 Field *fields2 = result->Fetch();
318 for(int i = 0; i < MAX_DECLINED_NAME_CASES; ++i)
320 m_declinedname->name[i] = fields2[i].GetCppString();
325 m_loading = false;
327 SynchronizeLevelWithOwner();
328 return true;
331 void Pet::SavePetToDB(PetSaveMode mode)
333 if (!GetEntry())
334 return;
336 // save only fully controlled creature
337 if (!isControlled())
338 return;
340 // not save not player pets
341 if(!IS_PLAYER_GUID(GetOwnerGUID()))
342 return;
344 Player* pOwner = (Player*)GetOwner();
345 if (!pOwner)
346 return;
348 // not save pet as current if another pet temporary unsummoned
349 if (mode == PET_SAVE_AS_CURRENT && pOwner->GetTemporaryUnsummonedPetNumber() &&
350 pOwner->GetTemporaryUnsummonedPetNumber() != m_charmInfo->GetPetNumber())
352 // pet will lost anyway at restore temporary unsummoned
353 if(getPetType()==HUNTER_PET)
354 return;
356 // for warlock case
357 mode = PET_SAVE_NOT_IN_SLOT;
360 uint32 curhealth = GetHealth();
361 uint32 curmana = GetPower(POWER_MANA);
363 // stable and not in slot saves
364 if(mode > PET_SAVE_AS_CURRENT)
366 RemoveAllAuras();
368 //only alive hunter pets get auras saved, the others don't
369 if(!(getPetType() == HUNTER_PET && isAlive()))
370 m_Auras.clear();
373 _SaveSpells();
374 _SaveSpellCooldowns();
375 _SaveAuras();
377 // current/stable/not_in_slot
378 if(mode >= PET_SAVE_AS_CURRENT)
380 uint32 owner = GUID_LOPART(GetOwnerGUID());
381 std::string name = m_name;
382 CharacterDatabase.escape_string(name);
383 CharacterDatabase.BeginTransaction();
384 // remove current data
385 CharacterDatabase.PExecute("DELETE FROM character_pet WHERE owner = '%u' AND id = '%u'", owner,m_charmInfo->GetPetNumber() );
387 // prevent duplicate using slot (except PET_SAVE_NOT_IN_SLOT)
388 if(mode <= PET_SAVE_LAST_STABLE_SLOT)
389 CharacterDatabase.PExecute("UPDATE character_pet SET slot = '%u' WHERE owner = '%u' AND slot = '%u'",
390 PET_SAVE_NOT_IN_SLOT, owner, uint32(mode) );
392 // prevent existence another hunter pet in PET_SAVE_AS_CURRENT and PET_SAVE_NOT_IN_SLOT
393 if(getPetType()==HUNTER_PET && (mode==PET_SAVE_AS_CURRENT||mode > PET_SAVE_LAST_STABLE_SLOT))
394 CharacterDatabase.PExecute("DELETE FROM character_pet WHERE owner = '%u' AND (slot = '%u' OR slot > '%u')",
395 owner,PET_SAVE_AS_CURRENT,PET_SAVE_LAST_STABLE_SLOT);
396 // save pet
397 std::ostringstream ss;
398 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) "
399 << "VALUES ("
400 << m_charmInfo->GetPetNumber() << ", "
401 << GetEntry() << ", "
402 << owner << ", "
403 << GetNativeDisplayId() << ", "
404 << getLevel() << ", "
405 << GetUInt32Value(UNIT_FIELD_PETEXPERIENCE) << ", "
406 << uint32(m_charmInfo->GetReactState()) << ", "
407 << uint32(mode) << ", '"
408 << name.c_str() << "', "
409 << uint32((GetByteValue(UNIT_FIELD_BYTES_2, 2) == UNIT_RENAME_ALLOWED)?0:1) << ", "
410 << (curhealth<1?1:curhealth) << ", "
411 << curmana << ", "
412 << GetPower(POWER_HAPPINESS) << ", '";
414 for(uint32 i = 0; i < MAX_UNIT_ACTION_BAR_INDEX; ++i)
416 ss << uint32(m_charmInfo->GetActionBarEntry(i)->Type) << " "
417 << uint32(m_charmInfo->GetActionBarEntry(i)->SpellOrAction) << " ";
420 ss << "', "
421 << time(NULL) << ", "
422 << uint32(m_resetTalentsCost) << ", "
423 << uint64(m_resetTalentsTime) << ", "
424 << GetUInt32Value(UNIT_CREATED_BY_SPELL) << ", "
425 << uint32(getPetType()) << ")";
427 CharacterDatabase.Execute( ss.str().c_str() );
428 CharacterDatabase.CommitTransaction();
430 // delete
431 else
433 RemoveAllAuras();
434 DeleteFromDB(m_charmInfo->GetPetNumber());
438 void Pet::DeleteFromDB(uint32 guidlow)
440 CharacterDatabase.PExecute("DELETE FROM character_pet WHERE id = '%u'", guidlow);
441 CharacterDatabase.PExecute("DELETE FROM character_pet_declinedname WHERE id = '%u'", guidlow);
442 CharacterDatabase.PExecute("DELETE FROM pet_aura WHERE guid = '%u'", guidlow);
443 CharacterDatabase.PExecute("DELETE FROM pet_spell WHERE guid = '%u'", guidlow);
444 CharacterDatabase.PExecute("DELETE FROM pet_spell_cooldown WHERE guid = '%u'", guidlow);
447 void Pet::setDeathState(DeathState s) // overwrite virtual Creature::setDeathState and Unit::setDeathState
449 Creature::setDeathState(s);
450 if(getDeathState()==CORPSE)
452 //remove summoned pet (no corpse)
453 if(getPetType()==SUMMON_PET)
454 Remove(PET_SAVE_NOT_IN_SLOT);
455 // other will despawn at corpse desppawning (Pet::Update code)
456 else
458 // pet corpse non lootable and non skinnable
459 SetUInt32Value( UNIT_DYNAMIC_FLAGS, 0x00 );
460 RemoveFlag (UNIT_FIELD_FLAGS, UNIT_FLAG_SKINNABLE);
462 //lose happiness when died and not in BG/Arena
463 MapEntry const* mapEntry = sMapStore.LookupEntry(GetMapId());
464 if(!mapEntry || (mapEntry->map_type != MAP_ARENA && mapEntry->map_type != MAP_BATTLEGROUND))
465 ModifyPower(POWER_HAPPINESS, -HAPPINESS_LEVEL_SIZE);
467 SetFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_STUNNED);
470 else if(getDeathState()==ALIVE)
472 RemoveFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_STUNNED);
473 CastPetAuras(true);
477 void Pet::Update(uint32 diff)
479 if(m_removed) // pet already removed, just wait in remove queue, no updates
480 return;
482 switch( m_deathState )
484 case CORPSE:
486 if( m_deathTimer <= diff )
488 assert(getPetType()!=SUMMON_PET && "Must be already removed.");
489 Remove(PET_SAVE_NOT_IN_SLOT); //hunters' pets never get removed because of death, NEVER!
490 return;
492 break;
494 case ALIVE:
496 // unsummon pet that lost owner
497 Unit* owner = GetOwner();
498 if(!owner || (!IsWithinDistInMap(owner, OWNER_MAX_DISTANCE) && (owner->GetCharmGUID() && (owner->GetCharmGUID() != GetGUID()))) || (isControlled() && !owner->GetPetGUID()))
500 Remove(PET_SAVE_NOT_IN_SLOT, true);
501 return;
504 if(isControlled())
506 if( owner->GetPetGUID() != GetGUID() )
508 Remove(getPetType()==HUNTER_PET?PET_SAVE_AS_DELETED:PET_SAVE_NOT_IN_SLOT);
509 return;
513 if(m_duration > 0)
515 if(m_duration > diff)
516 m_duration -= diff;
517 else
519 Remove(getPetType() != SUMMON_PET ? PET_SAVE_AS_DELETED:PET_SAVE_NOT_IN_SLOT);
520 return;
524 //regenerate focus for hunter pets or energy for deathknight's ghoul
525 if(m_regenTimer <= diff)
527 switch (getPowerType())
529 case POWER_FOCUS:
530 case POWER_ENERGY:
531 Regenerate(getPowerType());
532 break;
533 default:
534 break;
536 m_regenTimer = 4000;
538 else
539 m_regenTimer -= diff;
541 if(getPetType() != HUNTER_PET)
542 break;
544 if(m_happinessTimer <= diff)
546 LooseHappiness();
547 m_happinessTimer = 7500;
549 else
550 m_happinessTimer -= diff;
552 break;
554 default:
555 break;
557 Creature::Update(diff);
560 void Pet::Regenerate(Powers power)
562 uint32 curValue = GetPower(power);
563 uint32 maxValue = GetMaxPower(power);
565 if (curValue >= maxValue)
566 return;
568 float addvalue = 0.0f;
570 switch (power)
572 case POWER_FOCUS:
574 // For hunter pets.
575 addvalue = 24 * sWorld.getRate(RATE_POWER_FOCUS);
576 break;
578 case POWER_ENERGY:
580 // For deathknight's ghoul.
581 addvalue = 20;
582 break;
584 default:
585 return;
588 // Apply modifiers (if any).
589 AuraList const& ModPowerRegenPCTAuras = GetAurasByType(SPELL_AURA_MOD_POWER_REGEN_PERCENT);
590 for(AuraList::const_iterator i = ModPowerRegenPCTAuras.begin(); i != ModPowerRegenPCTAuras.end(); ++i)
591 if ((*i)->GetModifier()->m_miscvalue == power)
592 addvalue *= ((*i)->GetModifier()->m_amount + 100) / 100.0f;
594 ModifyPower(power, (int32)addvalue);
597 void Pet::LooseHappiness()
599 uint32 curValue = GetPower(POWER_HAPPINESS);
600 if (curValue <= 0)
601 return;
602 int32 addvalue = 670; //value is 70/35/17/8/4 (per min) * 1000 / 8 (timer 7.5 secs)
603 if(isInCombat()) //we know in combat happiness fades faster, multiplier guess
604 addvalue = int32(addvalue * 1.5);
605 ModifyPower(POWER_HAPPINESS, -addvalue);
608 HappinessState Pet::GetHappinessState()
610 if(GetPower(POWER_HAPPINESS) < HAPPINESS_LEVEL_SIZE)
611 return UNHAPPY;
612 else if(GetPower(POWER_HAPPINESS) >= HAPPINESS_LEVEL_SIZE * 2)
613 return HAPPY;
614 else
615 return CONTENT;
618 bool Pet::CanTakeMoreActiveSpells(uint32 spellid)
620 uint8 activecount = 1;
621 uint32 chainstartstore[ACTIVE_SPELLS_MAX];
623 if(IsPassiveSpell(spellid))
624 return true;
626 chainstartstore[0] = spellmgr.GetFirstSpellInChain(spellid);
628 for (PetSpellMap::const_iterator itr = m_spells.begin(); itr != m_spells.end(); ++itr)
630 if(itr->second.state == PETSPELL_REMOVED)
631 continue;
633 if(IsPassiveSpell(itr->first))
634 continue;
636 uint32 chainstart = spellmgr.GetFirstSpellInChain(itr->first);
638 uint8 x;
640 for(x = 0; x < activecount; x++)
642 if(chainstart == chainstartstore[x])
643 break;
646 if(x == activecount) //spellchain not yet saved -> add active count
648 ++activecount;
649 if(activecount > ACTIVE_SPELLS_MAX)
650 return false;
651 chainstartstore[x] = chainstart;
654 return true;
657 void Pet::Remove(PetSaveMode mode, bool returnreagent)
659 Unit* owner = GetOwner();
661 if(owner)
663 if(owner->GetTypeId()==TYPEID_PLAYER)
665 ((Player*)owner)->RemovePet(this,mode,returnreagent);
666 return;
669 // only if current pet in slot
670 if(owner->GetPetGUID()==GetGUID())
671 owner->SetPet(0);
674 CleanupsBeforeDelete();
675 AddObjectToRemoveList();
676 m_removed = true;
679 void Pet::GivePetXP(uint32 xp)
681 if(getPetType() != HUNTER_PET)
682 return;
684 if ( xp < 1 )
685 return;
687 if(!isAlive())
688 return;
690 uint32 level = getLevel();
692 // XP to money conversion processed in Player::RewardQuest
693 if(level >= sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL))
694 return;
696 uint32 curXP = GetUInt32Value(UNIT_FIELD_PETEXPERIENCE);
697 uint32 nextLvlXP = GetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP);
698 uint32 newXP = curXP + xp;
700 if(newXP >= nextLvlXP && level+1 > GetOwner()->getLevel())
702 SetUInt32Value(UNIT_FIELD_PETEXPERIENCE, nextLvlXP-1);
703 return;
706 while( newXP >= nextLvlXP && level < sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL) )
708 newXP -= nextLvlXP;
710 GivePetLevel(level+1);
711 SetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP, objmgr.GetXPForLevel(level+1)/4);
713 level = getLevel();
714 nextLvlXP = GetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP);
717 SetUInt32Value(UNIT_FIELD_PETEXPERIENCE, newXP);
720 void Pet::GivePetLevel(uint32 level)
722 if(!level)
723 return;
725 InitStatsForLevel(level);
726 InitLevelupSpellsForLevel();
727 InitTalentForLevel();
730 bool Pet::CreateBaseAtCreature(Creature* creature)
732 if(!creature)
734 sLog.outError("CRITICAL: NULL pointer parsed into CreateBaseAtCreature()");
735 return false;
737 uint32 guid=objmgr.GenerateLowGuid(HIGHGUID_PET);
739 sLog.outBasic("SetInstanceID()");
740 SetInstanceId(creature->GetInstanceId());
742 sLog.outBasic("Create pet");
743 uint32 pet_number = objmgr.GeneratePetNumber();
744 if(!Create(guid, creature->GetMap(), creature->GetPhaseMask(), creature->GetEntry(), pet_number))
745 return false;
747 Relocate(creature->GetPositionX(), creature->GetPositionY(), creature->GetPositionZ(), creature->GetOrientation());
749 if(!IsPositionValid())
751 sLog.outError("Pet (guidlow %d, entry %d) not created base at creature. Suggested coordinates isn't valid (X: %f Y: %f)",
752 GetGUIDLow(), GetEntry(), GetPositionX(), GetPositionY());
753 return false;
756 CreatureInfo const *cinfo = GetCreatureInfo();
757 if(!cinfo)
759 sLog.outError("CreateBaseAtCreature() failed, creatureInfo is missing!");
760 return false;
763 if(cinfo->type == CREATURE_TYPE_CRITTER)
765 setPetType(MINI_PET);
766 return true;
768 SetDisplayId(creature->GetDisplayId());
769 SetNativeDisplayId(creature->GetNativeDisplayId());
770 SetMaxPower(POWER_HAPPINESS, GetCreatePowers(POWER_HAPPINESS));
771 SetPower(POWER_HAPPINESS, 166500);
772 setPowerType(POWER_FOCUS);
773 SetUInt32Value(UNIT_FIELD_PET_NAME_TIMESTAMP, 0);
774 SetUInt32Value(UNIT_FIELD_PETEXPERIENCE, 0);
775 SetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP, objmgr.GetXPForLevel(creature->getLevel())/4);
776 SetUInt32Value(UNIT_NPC_FLAGS, 0);
778 if(CreatureFamilyEntry const* cFamily = sCreatureFamilyStore.LookupEntry(cinfo->family))
779 SetName(cFamily->Name[sWorld.GetDefaultDbcLocale()]);
780 else
781 SetName(creature->GetNameForLocaleIdx(objmgr.GetDBCLocaleIndex()));
783 if(cinfo->type == CREATURE_TYPE_BEAST)
785 SetUInt32Value(UNIT_FIELD_BYTES_0, 0x02020100);
786 SetSheath(SHEATH_STATE_MELEE);
787 SetByteValue(UNIT_FIELD_BYTES_2, 2, UNIT_RENAME_ALLOWED);
788 SetUInt32Value(UNIT_MOD_CAST_SPEED, creature->GetUInt32Value(UNIT_MOD_CAST_SPEED));
790 return true;
793 bool Pet::InitStatsForLevel(uint32 petlevel)
795 CreatureInfo const *cinfo = GetCreatureInfo();
796 assert(cinfo);
798 Unit* owner = GetOwner();
799 if(!owner)
801 sLog.outError("attempt to summon pet (Entry %u) without owner! Attempt terminated.", cinfo->Entry);
802 return false;
805 uint32 creature_ID = (getPetType() == HUNTER_PET) ? 1 : cinfo->Entry;
807 SetLevel(petlevel);
809 SetMeleeDamageSchool(SpellSchools(cinfo->dmgschool));
811 SetModifierValue(UNIT_MOD_ARMOR, BASE_VALUE, float(petlevel*50));
813 SetAttackTime(BASE_ATTACK, BASE_ATTACK_TIME);
814 SetAttackTime(OFF_ATTACK, BASE_ATTACK_TIME);
815 SetAttackTime(RANGED_ATTACK, BASE_ATTACK_TIME);
817 SetFloatValue(UNIT_MOD_CAST_SPEED, 1.0);
819 CreatureFamilyEntry const* cFamily = sCreatureFamilyStore.LookupEntry(cinfo->family);
820 if(cFamily && cFamily->minScale > 0.0f && getPetType()==HUNTER_PET)
822 float scale;
823 if (getLevel() >= cFamily->maxScaleLevel)
824 scale = cFamily->maxScale;
825 else if (getLevel() <= cFamily->minScaleLevel)
826 scale = cFamily->minScale;
827 else
828 scale = cFamily->minScale + float(getLevel() - cFamily->minScaleLevel) / cFamily->maxScaleLevel * (cFamily->maxScale - cFamily->minScale);
830 SetFloatValue(OBJECT_FIELD_SCALE_X, scale);
832 m_bonusdamage = 0;
834 int32 createResistance[MAX_SPELL_SCHOOL] = {0,0,0,0,0,0,0};
836 if(cinfo && getPetType() != HUNTER_PET)
838 createResistance[SPELL_SCHOOL_HOLY] = cinfo->resistance1;
839 createResistance[SPELL_SCHOOL_FIRE] = cinfo->resistance2;
840 createResistance[SPELL_SCHOOL_NATURE] = cinfo->resistance3;
841 createResistance[SPELL_SCHOOL_FROST] = cinfo->resistance4;
842 createResistance[SPELL_SCHOOL_SHADOW] = cinfo->resistance5;
843 createResistance[SPELL_SCHOOL_ARCANE] = cinfo->resistance6;
846 switch(getPetType())
848 case SUMMON_PET:
850 if(owner->GetTypeId() == TYPEID_PLAYER)
852 switch(owner->getClass())
854 case CLASS_WARLOCK:
857 //the damage bonus used for pets is either fire or shadow damage, whatever is higher
858 uint32 fire = owner->GetUInt32Value(PLAYER_FIELD_MOD_DAMAGE_DONE_POS + SPELL_SCHOOL_FIRE);
859 uint32 shadow = owner->GetUInt32Value(PLAYER_FIELD_MOD_DAMAGE_DONE_POS + SPELL_SCHOOL_SHADOW);
860 uint32 val = (fire > shadow) ? fire : shadow;
862 SetBonusDamage(int32 (val * 0.15f));
863 //bonusAP += val * 0.57;
864 break;
866 case CLASS_MAGE:
868 //40% damage bonus of mage's frost damage
869 float val = owner->GetUInt32Value(PLAYER_FIELD_MOD_DAMAGE_DONE_POS + SPELL_SCHOOL_FROST) * 0.4;
870 if(val < 0)
871 val = 0;
872 SetBonusDamage( int32(val));
873 break;
875 default:
876 break;
880 SetBaseWeaponDamage(BASE_ATTACK, MINDAMAGE, float(petlevel - (petlevel / 4)) );
881 SetBaseWeaponDamage(BASE_ATTACK, MAXDAMAGE, float(petlevel + (petlevel / 4)) );
883 //SetModifierValue(UNIT_MOD_ATTACK_POWER, BASE_VALUE, float(cinfo->attackpower));
885 PetLevelInfo const* pInfo = objmgr.GetPetLevelInfo(creature_ID, petlevel);
886 if(pInfo) // exist in DB
888 SetCreateHealth(pInfo->health);
889 SetCreateMana(pInfo->mana);
891 if(pInfo->armor > 0)
892 SetModifierValue(UNIT_MOD_ARMOR, BASE_VALUE, float(pInfo->armor));
894 for(int stat = 0; stat < MAX_STATS; ++stat)
896 SetCreateStat(Stats(stat), float(pInfo->stats[stat]));
899 else // not exist in DB, use some default fake data
901 sLog.outErrorDb("Summoned pet (Entry: %u) not have pet stats data in DB",cinfo->Entry);
903 // remove elite bonuses included in DB values
904 SetCreateHealth(uint32(((float(cinfo->maxhealth) / cinfo->maxlevel) / (1 + 2 * cinfo->rank)) * petlevel) );
905 SetCreateMana( uint32(((float(cinfo->maxmana) / cinfo->maxlevel) / (1 + 2 * cinfo->rank)) * petlevel) );
907 SetCreateStat(STAT_STRENGTH, 22);
908 SetCreateStat(STAT_AGILITY, 22);
909 SetCreateStat(STAT_STAMINA, 25);
910 SetCreateStat(STAT_INTELLECT, 28);
911 SetCreateStat(STAT_SPIRIT, 27);
913 break;
915 case HUNTER_PET:
917 SetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP, objmgr.GetXPForLevel(petlevel)/4);
918 //these formula may not be correct; however, it is designed to be close to what it should be
919 //this makes dps 0.5 of pets level
920 SetBaseWeaponDamage(BASE_ATTACK, MINDAMAGE, float(petlevel - (petlevel / 4)) );
921 //damage range is then petlevel / 2
922 SetBaseWeaponDamage(BASE_ATTACK, MAXDAMAGE, float(petlevel + (petlevel / 4)) );
923 //damage is increased afterwards as strength and pet scaling modify attack power
925 //stored standard pet stats are entry 1 in pet_levelinfo
926 PetLevelInfo const* pInfo = objmgr.GetPetLevelInfo(creature_ID, petlevel);
927 if(pInfo) // exist in DB
929 SetCreateHealth(pInfo->health);
930 SetModifierValue(UNIT_MOD_ARMOR, BASE_VALUE, float(pInfo->armor));
931 //SetModifierValue(UNIT_MOD_ATTACK_POWER, BASE_VALUE, float(cinfo->attackpower));
933 for( int i = STAT_STRENGTH; i < MAX_STATS; ++i)
935 SetCreateStat(Stats(i), float(pInfo->stats[i]));
938 else // not exist in DB, use some default fake data
940 sLog.outErrorDb("Hunter pet levelstats missing in DB");
942 // remove elite bonuses included in DB values
943 SetCreateHealth( uint32(((float(cinfo->maxhealth) / cinfo->maxlevel) / (1 + 2 * cinfo->rank)) * petlevel) );
945 SetCreateStat(STAT_STRENGTH, 22);
946 SetCreateStat(STAT_AGILITY, 22);
947 SetCreateStat(STAT_STAMINA, 25);
948 SetCreateStat(STAT_INTELLECT, 28);
949 SetCreateStat(STAT_SPIRIT, 27);
951 break;
953 case GUARDIAN_PET:
954 SetUInt32Value(UNIT_FIELD_PETEXPERIENCE, 0);
955 SetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP, 1000);
957 SetCreateMana(28 + 10*petlevel);
958 SetCreateHealth(28 + 30*petlevel);
960 // FIXME: this is wrong formula, possible each guardian pet have own damage formula
961 //these formula may not be correct; however, it is designed to be close to what it should be
962 //this makes dps 0.5 of pets level
963 SetBaseWeaponDamage(BASE_ATTACK, MINDAMAGE, float(petlevel - (petlevel / 4)));
964 //damage range is then petlevel / 2
965 SetBaseWeaponDamage(BASE_ATTACK, MAXDAMAGE, float(petlevel + (petlevel / 4)));
966 break;
967 default:
968 sLog.outError("Pet have incorrect type (%u) for levelup.", getPetType());
969 break;
972 for (int i = SPELL_SCHOOL_HOLY; i < MAX_SPELL_SCHOOL; ++i)
973 SetModifierValue(UnitMods(UNIT_MOD_RESISTANCE_START + i), BASE_VALUE, float(createResistance[i]));
975 UpdateAllStats();
977 SetHealth(GetMaxHealth());
978 SetPower(POWER_MANA, GetMaxPower(POWER_MANA));
980 return true;
983 bool Pet::HaveInDiet(ItemPrototype const* item) const
985 if (!item->FoodType)
986 return false;
988 CreatureInfo const* cInfo = GetCreatureInfo();
989 if(!cInfo)
990 return false;
992 CreatureFamilyEntry const* cFamily = sCreatureFamilyStore.LookupEntry(cInfo->family);
993 if(!cFamily)
994 return false;
996 uint32 diet = cFamily->petFoodMask;
997 uint32 FoodMask = 1 << (item->FoodType-1);
998 return diet & FoodMask;
1001 uint32 Pet::GetCurrentFoodBenefitLevel(uint32 itemlevel)
1003 // -5 or greater food level
1004 if(getLevel() <= itemlevel + 5) //possible to feed level 60 pet with level 55 level food for full effect
1005 return 35000;
1006 // -10..-6
1007 else if(getLevel() <= itemlevel + 10) //pure guess, but sounds good
1008 return 17000;
1009 // -14..-11
1010 else if(getLevel() <= itemlevel + 14) //level 55 food gets green on 70, makes sense to me
1011 return 8000;
1012 // -15 or less
1013 else
1014 return 0; //food too low level
1017 void Pet::_LoadSpellCooldowns()
1019 m_CreatureSpellCooldowns.clear();
1020 m_CreatureCategoryCooldowns.clear();
1022 QueryResult *result = CharacterDatabase.PQuery("SELECT spell,time FROM pet_spell_cooldown WHERE guid = '%u'",m_charmInfo->GetPetNumber());
1024 if(result)
1026 time_t curTime = time(NULL);
1028 WorldPacket data(SMSG_SPELL_COOLDOWN, (8+1+result->GetRowCount()*8));
1029 data << GetGUID();
1030 data << uint8(0x0); // flags (0x1, 0x2)
1034 Field *fields = result->Fetch();
1036 uint32 spell_id = fields[0].GetUInt32();
1037 time_t db_time = (time_t)fields[1].GetUInt64();
1039 if(!sSpellStore.LookupEntry(spell_id))
1041 sLog.outError("Pet %u have unknown spell %u in `pet_spell_cooldown`, skipping.",m_charmInfo->GetPetNumber(),spell_id);
1042 continue;
1045 // skip outdated cooldown
1046 if(db_time <= curTime)
1047 continue;
1049 data << uint32(spell_id);
1050 data << uint32(uint32(db_time-curTime)*IN_MILISECONDS);
1052 _AddCreatureSpellCooldown(spell_id,db_time);
1054 sLog.outDebug("Pet (Number: %u) spell %u cooldown loaded (%u secs).", m_charmInfo->GetPetNumber(), spell_id, uint32(db_time-curTime));
1056 while( result->NextRow() );
1058 delete result;
1060 if(!m_CreatureSpellCooldowns.empty() && GetOwner())
1062 ((Player*)GetOwner())->GetSession()->SendPacket(&data);
1067 void Pet::_SaveSpellCooldowns()
1069 CharacterDatabase.PExecute("DELETE FROM pet_spell_cooldown WHERE guid = '%u'", m_charmInfo->GetPetNumber());
1071 time_t curTime = time(NULL);
1073 // remove oudated and save active
1074 for(CreatureSpellCooldowns::iterator itr = m_CreatureSpellCooldowns.begin();itr != m_CreatureSpellCooldowns.end();)
1076 if(itr->second <= curTime)
1077 m_CreatureSpellCooldowns.erase(itr++);
1078 else
1080 CharacterDatabase.PExecute("INSERT INTO pet_spell_cooldown (guid,spell,time) VALUES ('%u', '%u', '" UI64FMTD "')", m_charmInfo->GetPetNumber(), itr->first, uint64(itr->second));
1081 ++itr;
1086 void Pet::_LoadSpells()
1088 QueryResult *result = CharacterDatabase.PQuery("SELECT spell,active FROM pet_spell WHERE guid = '%u'",m_charmInfo->GetPetNumber());
1090 if(result)
1094 Field *fields = result->Fetch();
1096 uint32 spell_id = fields[0].GetUInt32();
1098 // load only pet talents, other spell types auto-learned
1099 if(GetTalentSpellCost(spell_id)==0)
1101 CharacterDatabase.PExecute("DELETE FROM pet_spell WHERE spell = '%u'",spell_id);
1102 sLog.outError("Table `pet_spell` have non-talent spell %u , spell removed from table for all pets.",spell_id);
1103 continue;
1106 addSpell(spell_id, ActiveStates(fields[1].GetUInt16()), PETSPELL_UNCHANGED,PETSPELL_TALENT);
1108 while( result->NextRow() );
1110 delete result;
1114 void Pet::_SaveSpells()
1116 for (PetSpellMap::iterator itr = m_spells.begin(), next = m_spells.begin(); itr != m_spells.end(); itr = next)
1118 ++next;
1120 // save only talent spells for pets, other spells auto-applied
1121 if (itr->second.type != PETSPELL_TALENT)
1122 continue;
1124 switch(itr->second.state)
1126 case PETSPELL_REMOVED:
1127 CharacterDatabase.PExecute("DELETE FROM pet_spell WHERE guid = '%u' and spell = '%u'", m_charmInfo->GetPetNumber(), itr->first);
1128 m_spells.erase(itr);
1129 continue;
1130 case PETSPELL_CHANGED:
1131 CharacterDatabase.PExecute("DELETE FROM pet_spell WHERE guid = '%u' and spell = '%u'", m_charmInfo->GetPetNumber(), itr->first);
1132 CharacterDatabase.PExecute("INSERT INTO pet_spell (guid,spell,active) VALUES ('%u', '%u', '%u')", m_charmInfo->GetPetNumber(), itr->first, itr->second.active);
1133 break;
1134 case PETSPELL_NEW:
1135 CharacterDatabase.PExecute("INSERT INTO pet_spell (guid,spell,active) VALUES ('%u', '%u', '%u')", m_charmInfo->GetPetNumber(), itr->first, itr->second.active);
1136 break;
1137 case PETSPELL_UNCHANGED:
1138 continue;
1141 itr->second.state = PETSPELL_UNCHANGED;
1145 void Pet::_LoadAuras(uint32 timediff)
1147 m_Auras.clear();
1148 for (int i = 0; i < TOTAL_AURAS; ++i)
1149 m_modAuras[i].clear();
1151 QueryResult *result = CharacterDatabase.PQuery("SELECT caster_guid,spell,effect_index,stackcount,amount,maxduration,remaintime,remaincharges FROM pet_aura WHERE guid = '%u'",m_charmInfo->GetPetNumber());
1153 if(result)
1157 Field *fields = result->Fetch();
1158 uint64 caster_guid = fields[0].GetUInt64();
1159 uint32 spellid = fields[1].GetUInt32();
1160 uint32 effindex = fields[2].GetUInt32();
1161 uint32 stackcount= fields[3].GetUInt32();
1162 int32 damage = (int32)fields[4].GetUInt32();
1163 int32 maxduration = (int32)fields[5].GetUInt32();
1164 int32 remaintime = (int32)fields[6].GetUInt32();
1165 int32 remaincharges = (int32)fields[7].GetUInt32();
1167 SpellEntry const* spellproto = sSpellStore.LookupEntry(spellid);
1168 if(!spellproto)
1170 sLog.outError("Unknown aura (spellid %u, effindex %u), ignore.",spellid,effindex);
1171 continue;
1174 if(effindex >= 3)
1176 sLog.outError("Invalid effect index (spellid %u, effindex %u), ignore.",spellid,effindex);
1177 continue;
1180 // negative effects should continue counting down after logout
1181 if (remaintime != -1 && !IsPositiveEffect(spellid, effindex))
1183 if(remaintime <= int32(timediff))
1184 continue;
1186 remaintime -= timediff;
1189 // prevent wrong values of remaincharges
1190 if(spellproto->procCharges)
1192 if(remaincharges <= 0 || remaincharges > spellproto->procCharges)
1193 remaincharges = spellproto->procCharges;
1195 else
1196 remaincharges = -1;
1198 /// do not load single target auras (unless they were cast by the player)
1199 if (caster_guid != GetGUID() && IsSingleTargetSpell(spellproto))
1200 continue;
1202 for(uint32 i=0; i<stackcount; ++i)
1204 Aura* aura = CreateAura(spellproto, effindex, NULL, this, NULL);
1206 if(!damage)
1207 damage = aura->GetModifier()->m_amount;
1208 aura->SetLoadedState(caster_guid,damage,maxduration,remaintime,remaincharges);
1209 AddAura(aura);
1212 while( result->NextRow() );
1214 delete result;
1218 void Pet::_SaveAuras()
1220 CharacterDatabase.PExecute("DELETE FROM pet_aura WHERE guid = '%u'", m_charmInfo->GetPetNumber());
1222 AuraMap const& auras = GetAuras();
1223 if (auras.empty())
1224 return;
1226 spellEffectPair lastEffectPair = auras.begin()->first;
1227 uint32 stackCounter = 1;
1229 for(AuraMap::const_iterator itr = auras.begin(); ; ++itr)
1231 if(itr == auras.end() || lastEffectPair != itr->first)
1233 AuraMap::const_iterator itr2 = itr;
1234 // save previous spellEffectPair to db
1235 itr2--;
1236 SpellEntry const *spellInfo = itr2->second->GetSpellProto();
1237 /// do not save single target auras (unless they were cast by the player)
1238 if (!(itr2->second->GetCasterGUID() != GetGUID() && IsSingleTargetSpell(spellInfo)))
1240 if(!itr2->second->IsPassive())
1242 // skip all auras from spell that apply at cast SPELL_AURA_MOD_SHAPESHIFT or pet area auras.
1243 uint8 i;
1244 for (i = 0; i < 3; ++i)
1245 if (spellInfo->EffectApplyAuraName[i] == SPELL_AURA_MOD_STEALTH ||
1246 spellInfo->Effect[i] == SPELL_EFFECT_APPLY_AREA_AURA_OWNER ||
1247 spellInfo->Effect[i] == SPELL_EFFECT_APPLY_AREA_AURA_PET )
1248 break;
1250 if (i == 3)
1252 CharacterDatabase.PExecute("INSERT INTO pet_aura (guid,caster_guid,spell,effect_index,stackcount,amount,maxduration,remaintime,remaincharges) "
1253 "VALUES ('%u', '" UI64FMTD "', '%u', '%u', '%u', '%d', '%d', '%d', '%d')",
1254 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()));
1258 if(itr == auras.end())
1259 break;
1262 if (lastEffectPair == itr->first)
1263 stackCounter++;
1264 else
1266 lastEffectPair = itr->first;
1267 stackCounter = 1;
1272 bool Pet::addSpell(uint32 spell_id,ActiveStates active /*= ACT_DECIDE*/, PetSpellState state /*= PETSPELL_NEW*/, PetSpellType type /*= PETSPELL_NORMAL*/)
1274 SpellEntry const *spellInfo = sSpellStore.LookupEntry(spell_id);
1275 if (!spellInfo)
1277 // do pet spell book cleanup
1278 if(state == PETSPELL_UNCHANGED) // spell load case
1280 sLog.outError("Pet::addSpell: Non-existed in SpellStore spell #%u request, deleting for all pets in `pet_spell`.",spell_id);
1281 CharacterDatabase.PExecute("DELETE FROM pet_spell WHERE spell = '%u'",spell_id);
1283 else
1284 sLog.outError("Pet::addSpell: Non-existed in SpellStore spell #%u request.",spell_id);
1286 return false;
1289 PetSpellMap::iterator itr = m_spells.find(spell_id);
1290 if (itr != m_spells.end())
1292 if (itr->second.state == PETSPELL_REMOVED)
1294 m_spells.erase(itr);
1295 state = PETSPELL_CHANGED;
1297 else if (state == PETSPELL_UNCHANGED && itr->second.state != PETSPELL_UNCHANGED)
1299 // can be in case spell loading but learned at some previous spell loading
1300 itr->second.state = PETSPELL_UNCHANGED;
1302 if(active == ACT_ENABLED)
1303 ToggleAutocast(spell_id, true);
1304 else if(active == ACT_DISABLED)
1305 ToggleAutocast(spell_id, false);
1307 return false;
1309 else
1310 return false;
1313 uint32 oldspell_id = 0;
1315 PetSpell newspell;
1316 newspell.state = state;
1317 newspell.type = type;
1319 if(active == ACT_DECIDE) //active was not used before, so we save it's autocast/passive state here
1321 if(IsPassiveSpell(spell_id))
1322 newspell.active = ACT_PASSIVE;
1323 else
1324 newspell.active = ACT_DISABLED;
1326 else
1327 newspell.active = active;
1329 // talent: unlearn all other talent ranks (high and low)
1330 if(TalentSpellPos const* talentPos = GetTalentSpellPos(spell_id))
1332 // propertly mark spell for allow save
1333 newspell.type = PETSPELL_TALENT;
1335 if(TalentEntry const *talentInfo = sTalentStore.LookupEntry( talentPos->talent_id ))
1337 for(int i=0; i < MAX_TALENT_RANK; ++i)
1339 // skip learning spell and no rank spell case
1340 uint32 rankSpellId = talentInfo->RankID[i];
1341 if(!rankSpellId || rankSpellId==spell_id)
1342 continue;
1344 // skip unknown ranks
1345 if(!HasSpell(rankSpellId))
1346 continue;
1347 removeSpell(rankSpellId,false,false);
1351 else if(spellmgr.GetSpellRank(spell_id)!=0)
1353 for (PetSpellMap::const_iterator itr2 = m_spells.begin(); itr2 != m_spells.end(); ++itr2)
1355 if(itr2->second.state == PETSPELL_REMOVED) continue;
1357 if( spellmgr.IsRankSpellDueToSpell(spellInfo,itr2->first) )
1359 // replace by new high rank
1360 if(spellmgr.IsHighRankOfSpell(spell_id,itr2->first))
1362 newspell.active = itr2->second.active;
1364 if(newspell.active == ACT_ENABLED)
1365 ToggleAutocast(itr2->first, false);
1367 oldspell_id = itr2->first;
1368 unlearnSpell(itr2->first,false,false);
1369 break;
1371 // ignore new lesser rank
1372 else if(spellmgr.IsHighRankOfSpell(itr2->first,spell_id))
1373 return false;
1378 m_spells[spell_id] = newspell;
1380 if (IsPassiveSpell(spell_id))
1381 CastSpell(this, spell_id, true);
1382 else
1383 m_charmInfo->AddSpellToActionBar(spell_id);
1385 if(newspell.active == ACT_ENABLED)
1386 ToggleAutocast(spell_id, true);
1388 uint32 talentCost = GetTalentSpellCost(spell_id);
1389 if (talentCost)
1391 int32 free_points = GetMaxTalentPointsForLevel(getLevel());
1392 m_usedTalentCount+=talentCost;
1393 // update free talent points
1394 free_points-=m_usedTalentCount;
1395 SetFreeTalentPoints(free_points > 0 ? free_points : 0);
1397 return true;
1400 bool Pet::learnSpell(uint32 spell_id)
1402 // prevent duplicated entires in spell book
1403 if (!addSpell(spell_id))
1404 return false;
1406 if(!m_loading)
1408 Unit* owner = GetOwner();
1409 if(owner && owner->GetTypeId() == TYPEID_PLAYER)
1411 WorldPacket data(SMSG_PET_LEARNED_SPELL, 2);
1412 data << uint16(spell_id);
1413 ((Player*)owner)->GetSession()->SendPacket(&data);
1415 ((Player*)owner)->PetSpellInitialize();
1418 return true;
1421 void Pet::InitLevelupSpellsForLevel()
1423 uint32 level = getLevel();
1425 if(PetLevelupSpellSet const *levelupSpells = GetCreatureInfo()->family ? spellmgr.GetPetLevelupSpellList(GetCreatureInfo()->family) : NULL)
1427 // PetLevelupSpellSet ordered by levels, process in reversed order
1428 for(PetLevelupSpellSet::const_reverse_iterator itr = levelupSpells->rbegin(); itr != levelupSpells->rend(); ++itr)
1430 // will called first if level down
1431 if(itr->first > level)
1432 unlearnSpell(itr->second,true); // will learn prev rank if any
1433 // will called if level up
1434 else
1435 learnSpell(itr->second); // will unlearn prev rank if any
1439 int32 petSpellsId = GetCreatureInfo()->PetSpellDataId ? -(int32)GetCreatureInfo()->PetSpellDataId : GetEntry();
1441 // default spells (can be not learned if pet level (as owner level decrease result for example) less first possible in normal game)
1442 if(PetDefaultSpellsEntry const *defSpells = spellmgr.GetPetDefaultSpellsEntry(petSpellsId))
1444 for(int i = 0; i < MAX_CREATURE_SPELL_DATA_SLOT; ++i)
1446 SpellEntry const* spellEntry = sSpellStore.LookupEntry(defSpells->spellid[i]);
1447 if(!spellEntry)
1448 continue;
1450 // will called first if level down
1451 if(spellEntry->spellLevel > level)
1452 unlearnSpell(spellEntry->Id,true);
1453 // will called if level up
1454 else
1455 learnSpell(spellEntry->Id);
1460 bool Pet::unlearnSpell(uint32 spell_id, bool learn_prev, bool clear_ab)
1462 if(removeSpell(spell_id,learn_prev,clear_ab))
1464 if(GetOwner()->GetTypeId() == TYPEID_PLAYER)
1466 if(!m_loading)
1468 WorldPacket data(SMSG_PET_REMOVED_SPELL, 2);
1469 data << uint16(spell_id);
1470 ((Player*)GetOwner())->GetSession()->SendPacket(&data);
1473 return true;
1475 return false;
1478 bool Pet::removeSpell(uint32 spell_id, bool learn_prev, bool clear_ab)
1480 PetSpellMap::iterator itr = m_spells.find(spell_id);
1481 if (itr == m_spells.end())
1482 return false;
1484 if(itr->second.state == PETSPELL_REMOVED)
1485 return false;
1487 if(itr->second.state == PETSPELL_NEW)
1488 m_spells.erase(itr);
1489 else
1490 itr->second.state = PETSPELL_REMOVED;
1492 RemoveAurasDueToSpell(spell_id);
1494 uint32 talentCost = GetTalentSpellCost(spell_id);
1495 if (talentCost > 0)
1497 if (m_usedTalentCount > talentCost)
1498 m_usedTalentCount-=talentCost;
1499 else
1500 m_usedTalentCount = 0;
1501 // update free talent points
1502 int32 free_points = GetMaxTalentPointsForLevel(getLevel()) - m_usedTalentCount;
1503 SetFreeTalentPoints(free_points > 0 ? free_points : 0);
1506 if (learn_prev)
1508 if (uint32 prev_id = spellmgr.GetPrevSpellInChain (spell_id))
1509 learnSpell(prev_id);
1510 else
1511 learn_prev = false;
1514 // if remove last rank or non-ranked then update action bar at server and client if need
1515 if (clear_ab && !learn_prev && m_charmInfo->RemoveSpellFromActionBar(spell_id))
1517 if(!m_loading)
1519 // need update action bar for last removed rank
1520 if (Unit* owner = GetOwner())
1521 if (owner->GetTypeId() == TYPEID_PLAYER)
1522 ((Player*)owner)->PetSpellInitialize();
1526 return true;
1530 void Pet::CleanupActionBar()
1532 for(int i = 0; i < MAX_UNIT_ACTION_BAR_INDEX; ++i)
1533 if(UnitActionBarEntry const* ab = m_charmInfo->GetActionBarEntry(i))
1534 if(ab->SpellOrAction && ab->IsActionBarForSpell() && !HasSpell(ab->SpellOrAction))
1535 m_charmInfo->SetActionBar(i,0,ACT_DISABLED);
1538 void Pet::InitPetCreateSpells()
1540 m_charmInfo->InitPetActionBar();
1541 m_spells.clear();
1543 LearnPetPassives();
1545 CastPetAuras(false);
1548 bool Pet::resetTalents(bool no_cost)
1550 Unit *owner = GetOwner();
1551 if (!owner || owner->GetTypeId()!=TYPEID_PLAYER)
1552 return false;
1554 CreatureInfo const * ci = GetCreatureInfo();
1555 if(!ci)
1556 return false;
1557 // Check pet talent type
1558 CreatureFamilyEntry const *pet_family = sCreatureFamilyStore.LookupEntry(ci->family);
1559 if(!pet_family || pet_family->petTalentType < 0)
1560 return false;
1562 Player *player = (Player *)owner;
1564 uint32 level = getLevel();
1565 uint32 talentPointsForLevel = GetMaxTalentPointsForLevel(level);
1567 if (m_usedTalentCount == 0)
1569 SetFreeTalentPoints(talentPointsForLevel);
1570 return false;
1573 uint32 cost = 0;
1575 if(!no_cost)
1577 cost = resetTalentsCost();
1579 if (player->GetMoney() < cost)
1581 player->SendBuyError( BUY_ERR_NOT_ENOUGHT_MONEY, 0, 0, 0);
1582 return false;
1586 for (unsigned int i = 0; i < sTalentStore.GetNumRows(); ++i)
1588 TalentEntry const *talentInfo = sTalentStore.LookupEntry(i);
1590 if (!talentInfo) continue;
1592 TalentTabEntry const *talentTabInfo = sTalentTabStore.LookupEntry( talentInfo->TalentTab );
1594 if(!talentTabInfo)
1595 continue;
1597 // unlearn only talents for pets family talent type
1598 if(!((1 << pet_family->petTalentType) & talentTabInfo->petTalentMask))
1599 continue;
1601 for (int j = 0; j < MAX_TALENT_RANK; j++)
1603 for(PetSpellMap::const_iterator itr = m_spells.begin(); itr != m_spells.end();)
1605 if(itr->second.state == PETSPELL_REMOVED)
1607 ++itr;
1608 continue;
1610 // remove learned spells (all ranks)
1611 uint32 itrFirstId = spellmgr.GetFirstSpellInChain(itr->first);
1613 // unlearn if first rank is talent or learned by talent
1614 if (itrFirstId == talentInfo->RankID[j] || spellmgr.IsSpellLearnToSpell(talentInfo->RankID[j],itrFirstId))
1616 removeSpell(itr->first,false);
1617 itr = m_spells.begin();
1618 continue;
1620 else
1621 ++itr;
1626 SetFreeTalentPoints(talentPointsForLevel);
1628 if(!no_cost)
1630 player->ModifyMoney(-(int32)cost);
1632 m_resetTalentsCost = cost;
1633 m_resetTalentsTime = time(NULL);
1635 player->PetSpellInitialize();
1636 return true;
1639 void Pet::InitTalentForLevel()
1641 uint32 level = getLevel();
1642 uint32 talentPointsForLevel = GetMaxTalentPointsForLevel(level);
1643 // Reset talents in case low level (on level down) or wrong points for level (hunter can unlearn TP increase talent)
1644 if(talentPointsForLevel == 0 || m_usedTalentCount > talentPointsForLevel)
1646 // Remove all talent points
1647 resetTalents(true);
1649 SetFreeTalentPoints(talentPointsForLevel - m_usedTalentCount);
1652 uint32 Pet::resetTalentsCost() const
1654 uint32 days = (sWorld.GetGameTime() - m_resetTalentsTime)/DAY;
1656 // The first time reset costs 10 silver; after 1 day cost is reset to 10 silver
1657 if(m_resetTalentsCost < 10*SILVER || days > 0)
1658 return 10*SILVER;
1659 // then 50 silver
1660 else if(m_resetTalentsCost < 50*SILVER)
1661 return 50*SILVER;
1662 // then 1 gold
1663 else if(m_resetTalentsCost < 1*GOLD)
1664 return 1*GOLD;
1665 // then increasing at a rate of 1 gold; cap 10 gold
1666 else
1667 return (m_resetTalentsCost + 1*GOLD > 10*GOLD ? 10*GOLD : m_resetTalentsCost + 1*GOLD);
1670 uint8 Pet::GetMaxTalentPointsForLevel(uint32 level)
1672 uint8 points = (level >= 20) ? ((level - 16) / 4) : 0;
1673 // Mod points from owner SPELL_AURA_MOD_PET_TALENT_POINTS
1674 if (Unit *owner = GetOwner())
1675 points+=owner->GetTotalAuraModifier(SPELL_AURA_MOD_PET_TALENT_POINTS);
1676 return points;
1679 void Pet::ToggleAutocast(uint32 spellid, bool apply)
1681 if(IsPassiveSpell(spellid))
1682 return;
1684 PetSpellMap::iterator itr = m_spells.find(spellid);
1686 int i;
1688 if(apply)
1690 for (i = 0; i < m_autospells.size() && m_autospells[i] != spellid; ++i)
1691 ; // just search
1693 if (i == m_autospells.size())
1695 m_autospells.push_back(spellid);
1697 if(itr->second.active != ACT_ENABLED)
1699 itr->second.active = ACT_ENABLED;
1700 if(itr->second.state != PETSPELL_NEW)
1701 itr->second.state = PETSPELL_CHANGED;
1705 else
1707 AutoSpellList::iterator itr2 = m_autospells.begin();
1708 for (i = 0; i < m_autospells.size() && m_autospells[i] != spellid; ++i, itr2++)
1709 ; // just search
1711 if (i < m_autospells.size())
1713 m_autospells.erase(itr2);
1714 if(itr->second.active != ACT_DISABLED)
1716 itr->second.active = ACT_DISABLED;
1717 if(itr->second.state != PETSPELL_NEW)
1718 itr->second.state = PETSPELL_CHANGED;
1724 bool Pet::IsPermanentPetFor(Player* owner)
1726 switch(getPetType())
1728 case SUMMON_PET:
1729 switch(owner->getClass())
1731 case CLASS_WARLOCK:
1732 return GetCreatureInfo()->type == CREATURE_TYPE_DEMON;
1733 case CLASS_DEATH_KNIGHT:
1734 return GetCreatureInfo()->type == CREATURE_TYPE_UNDEAD;
1735 default:
1736 return false;
1738 case HUNTER_PET:
1739 return true;
1740 default:
1741 return false;
1745 bool Pet::Create(uint32 guidlow, Map *map, uint32 phaseMask, uint32 Entry, uint32 pet_number)
1747 SetMapId(map->GetId());
1748 SetInstanceId(map->GetInstanceId());
1749 SetPhaseMask(phaseMask,false);
1751 Object::_Create(guidlow, pet_number, HIGHGUID_PET);
1753 m_DBTableGuid = guidlow;
1754 m_originalEntry = Entry;
1756 if(!InitEntry(Entry))
1757 return false;
1759 SetSheath(SHEATH_STATE_MELEE);
1761 if(getPetType() == MINI_PET) // always non-attackable
1762 SetFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_NON_ATTACKABLE);
1764 return true;
1767 bool Pet::HasSpell(uint32 spell) const
1769 PetSpellMap::const_iterator itr = m_spells.find(spell);
1770 return (itr != m_spells.end() && itr->second.state != PETSPELL_REMOVED );
1773 // Get all passive spells in our skill line
1774 void Pet::LearnPetPassives()
1776 CreatureInfo const* cInfo = GetCreatureInfo();
1777 if(!cInfo)
1778 return;
1780 CreatureFamilyEntry const* cFamily = sCreatureFamilyStore.LookupEntry(cInfo->family);
1781 if(!cFamily)
1782 return;
1784 PetFamilySpellsStore::const_iterator petStore = sPetFamilySpellsStore.find(cFamily->ID);
1785 if(petStore != sPetFamilySpellsStore.end())
1787 for(PetFamilySpellsSet::const_iterator petSet = petStore->second.begin(); petSet != petStore->second.end(); ++petSet)
1788 addSpell(*petSet, ACT_DECIDE, PETSPELL_NEW, PETSPELL_FAMILY);
1792 void Pet::CastPetAuras(bool current)
1794 Unit* owner = GetOwner();
1795 if(!owner || owner->GetTypeId()!=TYPEID_PLAYER)
1796 return;
1798 if(!IsPermanentPetFor((Player*)owner))
1799 return;
1801 for(PetAuraSet::const_iterator itr = owner->m_petAuras.begin(); itr != owner->m_petAuras.end();)
1803 PetAura const* pa = *itr;
1804 ++itr;
1806 if(!current && pa->IsRemovedOnChangePet())
1807 owner->RemovePetAura(pa);
1808 else
1809 CastPetAura(pa);
1813 void Pet::CastPetAura(PetAura const* aura)
1815 uint16 auraId = aura->GetAura(GetEntry());
1816 if(!auraId)
1817 return;
1819 if(auraId == 35696) // Demonic Knowledge
1821 int32 basePoints = int32(aura->GetDamage() * (GetStat(STAT_STAMINA) + GetStat(STAT_INTELLECT)) / 100);
1822 CastCustomSpell(this, auraId, &basePoints, NULL, NULL, true);
1824 else
1825 CastSpell(this, auraId, true);
1828 void Pet::learnSpellHighRank(uint32 spellid)
1830 learnSpell(spellid);
1832 SpellChainMapNext const& nextMap = spellmgr.GetSpellChainNext();
1833 for(SpellChainMapNext::const_iterator itr = nextMap.lower_bound(spellid); itr != nextMap.upper_bound(spellid); ++itr)
1834 learnSpellHighRank(itr->second);
1837 void Pet::SynchronizeLevelWithOwner()
1839 Unit* owner = GetOwner();
1840 if (!owner || owner->GetTypeId() != TYPEID_PLAYER)
1841 return;
1843 switch(getPetType())
1845 // always same level
1846 case SUMMON_PET:
1847 GivePetLevel(owner->getLevel());
1848 break;
1849 // can't be greater owner level
1850 case HUNTER_PET:
1851 if(getLevel() > owner->getLevel())
1853 GivePetLevel(owner->getLevel());
1854 SetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP, objmgr.GetXPForLevel(owner->getLevel())/4);
1855 SetUInt32Value(UNIT_FIELD_PETEXPERIENCE, GetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP)-1);
1857 break;
1858 default:
1859 break;