[7615] Continue sorting chat command function declarations. Move send commands in...
[AHbot.git] / src / game / Pet.cpp
blob34426115f19185604feed380f3439171d542d23f
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_petType(type), m_removed(false), m_happinessTimer(7500), m_duration(0), m_bonusdamage(0),
42 m_resetTalentsCost(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::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 FROM character_pet WHERE owner = '%u' AND id = '%u'",ownerid, petnumber);
96 else if(current)
97 // 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
98 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 FROM character_pet WHERE owner = '%u' AND slot = '0'",ownerid );
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 19 20
102 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 FROM character_pet WHERE owner = '%u' AND entry = '%u' AND (slot = '0' OR slot = '3') ",ownerid, petentry );
103 else
104 // any current or other non-stabled pet (for hunter "call pet")
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 FROM character_pet WHERE owner = '%u' AND (slot = '0' OR slot = '3') ",ownerid);
108 if(!result)
109 return false;
111 Field *fields = result->Fetch();
113 // update for case of current pet "slot = 0"
114 petentry = fields[1].GetUInt32();
115 if(!petentry)
117 delete result;
118 return false;
121 uint32 summon_spell_id = fields[19].GetUInt32();
122 SpellEntry const* spellInfo = sSpellStore.LookupEntry(summon_spell_id);
124 bool is_temporary_summoned = spellInfo && GetSpellDuration(spellInfo) > 0;
126 // check temporary summoned pets like mage water elemental
127 if(current && is_temporary_summoned)
129 delete result;
130 return false;
133 Map *map = owner->GetMap();
134 uint32 guid = objmgr.GenerateLowGuid(HIGHGUID_PET);
135 uint32 pet_number = fields[0].GetUInt32();
136 if(!Create(guid, map, owner->GetPhaseMask(), petentry, pet_number))
138 delete result;
139 return false;
142 float px, py, pz;
143 owner->GetClosePoint(px, py, pz, GetObjectSize(), PET_FOLLOW_DIST, PET_FOLLOW_ANGLE);
145 Relocate(px, py, pz, owner->GetOrientation());
147 if(!IsPositionValid())
149 sLog.outError("Pet (guidlow %d, entry %d) not loaded. Suggested coordinates isn't valid (X: %f Y: %f)",
150 GetGUIDLow(), GetEntry(), GetPositionX(), GetPositionY());
151 delete result;
152 return false;
155 setPetType(PetType(fields[20].GetUInt8()));
156 SetUInt32Value(UNIT_FIELD_FACTIONTEMPLATE, owner->getFaction());
157 SetUInt32Value(UNIT_CREATED_BY_SPELL, summon_spell_id);
159 CreatureInfo const *cinfo = GetCreatureInfo();
160 if(cinfo->type == CREATURE_TYPE_CRITTER)
162 AIM_Initialize();
163 map->Add((Creature*)this);
164 delete result;
165 return true;
168 if(getPetType() == HUNTER_PET || (getPetType() == SUMMON_PET && cinfo->type == CREATURE_TYPE_DEMON && owner->getClass() == CLASS_WARLOCK))
169 m_charmInfo->SetPetNumber(pet_number, true);
170 else
171 m_charmInfo->SetPetNumber(pet_number, false);
173 SetOwnerGUID(owner->GetGUID());
174 SetDisplayId(fields[3].GetUInt32());
175 SetNativeDisplayId(fields[3].GetUInt32());
176 uint32 petlevel = fields[4].GetUInt32();
177 SetUInt32Value(UNIT_NPC_FLAGS, 0);
178 SetName(fields[9].GetString());
180 switch(getPetType())
182 case SUMMON_PET:
183 petlevel=owner->getLevel();
185 SetUInt32Value(UNIT_FIELD_BYTES_0, 2048);
186 SetUInt32Value(UNIT_FIELD_FLAGS, UNIT_FLAG_PVP_ATTACKABLE);
187 // this enables popup window (pet dismiss, cancel)
188 break;
189 case HUNTER_PET:
190 SetUInt32Value(UNIT_FIELD_BYTES_0, 0x02020100);
191 SetByteValue(UNIT_FIELD_BYTES_1, 1, fields[7].GetUInt32());
192 SetByteValue(UNIT_FIELD_BYTES_2, 0, SHEATH_STATE_MELEE);
193 SetByteValue(UNIT_FIELD_BYTES_2, 2, fields[10].GetBool() ? UNIT_RENAME_NOT_ALLOWED : UNIT_RENAME_ALLOWED);
195 SetUInt32Value(UNIT_FIELD_FLAGS, UNIT_FLAG_PVP_ATTACKABLE);
196 // this enables popup window (pet abandon, cancel)
197 SetMaxPower(POWER_HAPPINESS, GetCreatePowers(POWER_HAPPINESS));
198 SetPower(POWER_HAPPINESS, fields[13].GetUInt32());
199 setPowerType(POWER_FOCUS);
200 break;
201 default:
202 sLog.outError("Pet have incorrect type (%u) for pet loading.", getPetType());
205 InitStatsForLevel(petlevel);
206 SetUInt32Value(UNIT_FIELD_PET_NAME_TIMESTAMP, time(NULL));
207 SetUInt32Value(UNIT_FIELD_PETEXPERIENCE, fields[5].GetUInt32());
208 SetCreatorGUID(owner->GetGUID());
210 m_charmInfo->SetReactState(ReactStates(fields[6].GetUInt8()));
212 uint32 savedhealth = fields[11].GetUInt32();
213 uint32 savedmana = fields[12].GetUInt32();
215 // set current pet as current
216 if(fields[8].GetUInt32() != 0)
218 CharacterDatabase.BeginTransaction();
219 CharacterDatabase.PExecute("UPDATE character_pet SET slot = '3' WHERE owner = '%u' AND slot = '0' AND id <> '%u'", ownerid, m_charmInfo->GetPetNumber());
220 CharacterDatabase.PExecute("UPDATE character_pet SET slot = '0' WHERE owner = '%u' AND id = '%u'", ownerid, m_charmInfo->GetPetNumber());
221 CharacterDatabase.CommitTransaction();
224 if(!is_temporary_summoned)
226 // permanent controlled pets store state in DB
227 Tokens tokens = StrSplit(fields[14].GetString(), " ");
229 if(tokens.size() != 20)
231 delete result;
232 return false;
235 int index;
236 Tokens::iterator iter;
237 for(iter = tokens.begin(), index = 0; index < 10; ++iter, ++index )
239 m_charmInfo->GetActionBarEntry(index)->Type = atol((*iter).c_str());
240 ++iter;
241 m_charmInfo->GetActionBarEntry(index)->SpellOrAction = atol((*iter).c_str());
244 //init teach spells
245 tokens = StrSplit(fields[15].GetString(), " ");
246 for (iter = tokens.begin(), index = 0; index < 4; ++iter, ++index)
248 uint32 tmp = atol((*iter).c_str());
250 ++iter;
252 if(tmp)
253 AddTeachSpell(tmp, atol((*iter).c_str()));
254 else
255 break;
259 // since last save (in seconds)
260 uint32 timediff = (time(NULL) - fields[16].GetUInt32());
262 m_resetTalentsCost = fields[17].GetUInt32();
263 m_resetTalentsTime = fields[18].GetUInt64();
265 delete result;
267 //load spells/cooldowns/auras
268 SetCanModifyStats(true);
269 _LoadAuras(timediff);
271 //init AB
272 if(is_temporary_summoned)
274 // Temporary summoned pets always have initial spell list at load
275 InitPetCreateSpells();
277 else
279 LearnPetPassives();
280 CastPetAuras(current);
283 if(getPetType() == SUMMON_PET && !current) //all (?) summon pets come with full health when called, but not when they are current
285 SetHealth(GetMaxHealth());
286 SetPower(POWER_MANA, GetMaxPower(POWER_MANA));
288 else
290 SetHealth(savedhealth > GetMaxHealth() ? GetMaxHealth() : savedhealth);
291 SetPower(POWER_MANA, savedmana > GetMaxPower(POWER_MANA) ? GetMaxPower(POWER_MANA) : savedmana);
294 AIM_Initialize();
295 map->Add((Creature*)this);
297 // Spells should be loaded after pet is added to map, because in CheckCast is check on it
298 _LoadSpells();
299 _LoadSpellCooldowns();
301 owner->SetPet(this); // in DB stored only full controlled creature
302 sLog.outDebug("New Pet has guid %u", GetGUIDLow());
304 if(owner->GetTypeId() == TYPEID_PLAYER)
306 ((Player*)owner)->PetSpellInitialize();
307 if(((Player*)owner)->GetGroup())
308 ((Player*)owner)->SetGroupUpdateFlag(GROUP_UPDATE_PET);
311 if(owner->GetTypeId() == TYPEID_PLAYER && getPetType() == HUNTER_PET)
313 result = CharacterDatabase.PQuery("SELECT genitive, dative, accusative, instrumental, prepositional FROM character_pet_declinedname WHERE owner = '%u' AND id = '%u'", owner->GetGUIDLow(), GetCharmInfo()->GetPetNumber());
315 if(result)
317 if(m_declinedname)
318 delete m_declinedname;
320 m_declinedname = new DeclinedName;
321 Field *fields = result->Fetch();
322 for(int i = 0; i < MAX_DECLINED_NAME_CASES; ++i)
324 m_declinedname->name[i] = fields[i].GetCppString();
329 m_loading = false;
330 return true;
333 void Pet::SavePetToDB(PetSaveMode mode)
335 if(!GetEntry())
336 return;
338 // save only fully controlled creature
339 if(!isControlled())
340 return;
342 uint32 curhealth = GetHealth();
343 uint32 curmana = GetPower(POWER_MANA);
345 switch(mode)
347 case PET_SAVE_IN_STABLE_SLOT_1:
348 case PET_SAVE_IN_STABLE_SLOT_2:
349 case PET_SAVE_NOT_IN_SLOT:
351 RemoveAllAuras();
353 //only alive hunter pets get auras saved, the others don't
354 if(!(getPetType() == HUNTER_PET && isAlive()))
355 m_Auras.clear();
357 default:
358 break;
361 _SaveSpells();
362 _SaveSpellCooldowns();
363 _SaveAuras();
365 switch(mode)
367 case PET_SAVE_AS_CURRENT:
368 case PET_SAVE_IN_STABLE_SLOT_1:
369 case PET_SAVE_IN_STABLE_SLOT_2:
370 case PET_SAVE_NOT_IN_SLOT:
372 uint32 owner = GUID_LOPART(GetOwnerGUID());
373 std::string name = m_name;
374 CharacterDatabase.escape_string(name);
375 CharacterDatabase.BeginTransaction();
376 // remove current data
377 CharacterDatabase.PExecute("DELETE FROM character_pet WHERE owner = '%u' AND id = '%u'", owner,m_charmInfo->GetPetNumber() );
379 // prevent duplicate using slot (except PET_SAVE_NOT_IN_SLOT)
380 if(mode!=PET_SAVE_NOT_IN_SLOT)
381 CharacterDatabase.PExecute("UPDATE character_pet SET slot = 3 WHERE owner = '%u' AND slot = '%u'", owner, uint32(mode) );
383 // prevent existence another hunter pet in PET_SAVE_AS_CURRENT and PET_SAVE_NOT_IN_SLOT
384 if(getPetType()==HUNTER_PET && (mode==PET_SAVE_AS_CURRENT||mode==PET_SAVE_NOT_IN_SLOT))
385 CharacterDatabase.PExecute("DELETE FROM character_pet WHERE owner = '%u' AND (slot = '0' OR slot = '3')", owner );
386 // save pet
387 std::ostringstream ss;
388 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) "
389 << "VALUES ("
390 << m_charmInfo->GetPetNumber() << ", "
391 << GetEntry() << ", "
392 << owner << ", "
393 << GetNativeDisplayId() << ", "
394 << getLevel() << ", "
395 << GetUInt32Value(UNIT_FIELD_PETEXPERIENCE) << ", "
396 << uint32(m_charmInfo->GetReactState()) << ", "
397 << uint32(GetFreeTalentPoints()) << ", "
398 << uint32(mode) << ", '"
399 << name.c_str() << "', "
400 << uint32((GetByteValue(UNIT_FIELD_BYTES_2, 2) == UNIT_RENAME_ALLOWED)?0:1) << ", "
401 << (curhealth<1?1:curhealth) << ", "
402 << curmana << ", "
403 << GetPower(POWER_HAPPINESS) << ", '";
405 for(uint32 i = 0; i < 10; i++)
406 ss << uint32(m_charmInfo->GetActionBarEntry(i)->Type) << " " << uint32(m_charmInfo->GetActionBarEntry(i)->SpellOrAction) << " ";
407 ss << "', '";
409 //save spells the pet can teach to it's Master
411 int i = 0;
412 for(TeachSpellMap::iterator itr = m_teachspells.begin(); i < 4 && itr != m_teachspells.end(); ++i, ++itr)
413 ss << itr->first << " " << itr->second << " ";
414 for(; i < 4; ++i)
415 ss << uint32(0) << " " << uint32(0) << " ";
418 ss << "', "
419 << time(NULL) << ", "
420 << uint32(m_resetTalentsCost) << ", "
421 << uint64(m_resetTalentsTime) << ", "
422 << GetUInt32Value(UNIT_CREATED_BY_SPELL) << ", "
423 << uint32(getPetType()) << ")";
425 CharacterDatabase.Execute( ss.str().c_str() );
427 CharacterDatabase.CommitTransaction();
428 break;
430 case PET_SAVE_AS_DELETED:
432 RemoveAllAuras();
433 DeleteFromDB(m_charmInfo->GetPetNumber());
434 break;
436 default:
437 sLog.outError("Unknown pet save/remove mode: %d",mode);
441 void Pet::DeleteFromDB(uint32 guidlow)
443 CharacterDatabase.PExecute("DELETE FROM character_pet WHERE id = '%u'", guidlow);
444 CharacterDatabase.PExecute("DELETE FROM character_pet_declinedname WHERE id = '%u'", guidlow);
445 CharacterDatabase.PExecute("DELETE FROM pet_aura WHERE guid = '%u'", guidlow);
446 CharacterDatabase.PExecute("DELETE FROM pet_spell WHERE guid = '%u'", guidlow);
447 CharacterDatabase.PExecute("DELETE FROM pet_spell_cooldown WHERE guid = '%u'", guidlow);
450 void Pet::setDeathState(DeathState s) // overwrite virtual Creature::setDeathState and Unit::setDeathState
452 Creature::setDeathState(s);
453 if(getDeathState()==CORPSE)
455 //remove summoned pet (no corpse)
456 if(getPetType()==SUMMON_PET)
457 Remove(PET_SAVE_NOT_IN_SLOT);
458 // other will despawn at corpse desppawning (Pet::Update code)
459 else
461 // pet corpse non lootable and non skinnable
462 SetUInt32Value( UNIT_DYNAMIC_FLAGS, 0x00 );
463 RemoveFlag (UNIT_FIELD_FLAGS, UNIT_FLAG_SKINNABLE);
465 //lose happiness when died and not in BG/Arena
466 MapEntry const* mapEntry = sMapStore.LookupEntry(GetMapId());
467 if(!mapEntry || (mapEntry->map_type != MAP_ARENA && mapEntry->map_type != MAP_BATTLEGROUND))
468 ModifyPower(POWER_HAPPINESS, -HAPPINESS_LEVEL_SIZE);
470 SetFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_STUNNED);
473 else if(getDeathState()==ALIVE)
475 RemoveFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_STUNNED);
476 CastPetAuras(true);
480 void Pet::Update(uint32 diff)
482 if(m_removed) // pet already removed, just wait in remove queue, no updates
483 return;
485 switch( m_deathState )
487 case CORPSE:
489 if( m_deathTimer <= diff )
491 assert(getPetType()!=SUMMON_PET && "Must be already removed.");
492 Remove(PET_SAVE_NOT_IN_SLOT); //hunters' pets never get removed because of death, NEVER!
493 return;
495 break;
497 case ALIVE:
499 // unsummon pet that lost owner
500 Unit* owner = GetOwner();
501 if(!owner || (!IsWithinDistInMap(owner, OWNER_MAX_DISTANCE) && (owner->GetCharmGUID() && (owner->GetCharmGUID() != GetGUID()))) || (isControlled() && !owner->GetPetGUID()))
503 Remove(PET_SAVE_NOT_IN_SLOT, true);
504 return;
507 if(isControlled())
509 if( owner->GetPetGUID() != GetGUID() )
511 Remove(getPetType()==HUNTER_PET?PET_SAVE_AS_DELETED:PET_SAVE_NOT_IN_SLOT);
512 return;
516 if(m_duration > 0)
518 if(m_duration > diff)
519 m_duration -= diff;
520 else
522 Remove(getPetType() != SUMMON_PET ? PET_SAVE_AS_DELETED:PET_SAVE_NOT_IN_SLOT);
523 return;
527 if(getPetType() != HUNTER_PET)
528 break;
530 //regenerate Focus
531 if(m_regenTimer <= diff)
533 RegenerateFocus();
534 m_regenTimer = 4000;
536 else
537 m_regenTimer -= diff;
539 if(m_happinessTimer <= diff)
541 LooseHappiness();
542 m_happinessTimer = 7500;
544 else
545 m_happinessTimer -= diff;
547 break;
549 default:
550 break;
552 Creature::Update(diff);
555 void Pet::RegenerateFocus()
557 uint32 curValue = GetPower(POWER_FOCUS);
558 uint32 maxValue = GetMaxPower(POWER_FOCUS);
560 if (curValue >= maxValue)
561 return;
563 float addvalue = 24 * sWorld.getRate(RATE_POWER_FOCUS);
565 AuraList const& ModPowerRegenPCTAuras = GetAurasByType(SPELL_AURA_MOD_POWER_REGEN_PERCENT);
566 for(AuraList::const_iterator i = ModPowerRegenPCTAuras.begin(); i != ModPowerRegenPCTAuras.end(); ++i)
567 if ((*i)->GetModifier()->m_miscvalue == POWER_FOCUS)
568 addvalue *= ((*i)->GetModifier()->m_amount + 100) / 100.0f;
570 ModifyPower(POWER_FOCUS, (int32)addvalue);
573 void Pet::LooseHappiness()
575 uint32 curValue = GetPower(POWER_HAPPINESS);
576 if (curValue <= 0)
577 return;
578 int32 addvalue = 670; //value is 70/35/17/8/4 (per min) * 1000 / 8 (timer 7.5 secs)
579 if(isInCombat()) //we know in combat happiness fades faster, multiplier guess
580 addvalue = int32(addvalue * 1.5);
581 ModifyPower(POWER_HAPPINESS, -addvalue);
584 HappinessState Pet::GetHappinessState()
586 if(GetPower(POWER_HAPPINESS) < HAPPINESS_LEVEL_SIZE)
587 return UNHAPPY;
588 else if(GetPower(POWER_HAPPINESS) >= HAPPINESS_LEVEL_SIZE * 2)
589 return HAPPY;
590 else
591 return CONTENT;
594 bool Pet::CanTakeMoreActiveSpells(uint32 spellid)
596 uint8 activecount = 1;
597 uint32 chainstartstore[ACTIVE_SPELLS_MAX];
599 if(IsPassiveSpell(spellid))
600 return true;
602 chainstartstore[0] = spellmgr.GetFirstSpellInChain(spellid);
604 for (PetSpellMap::iterator itr = m_spells.begin(); itr != m_spells.end(); ++itr)
606 if(IsPassiveSpell(itr->first))
607 continue;
609 uint32 chainstart = spellmgr.GetFirstSpellInChain(itr->first);
611 uint8 x;
613 for(x = 0; x < activecount; x++)
615 if(chainstart == chainstartstore[x])
616 break;
619 if(x == activecount) //spellchain not yet saved -> add active count
621 ++activecount;
622 if(activecount > ACTIVE_SPELLS_MAX)
623 return false;
624 chainstartstore[x] = chainstart;
627 return true;
630 void Pet::Remove(PetSaveMode mode, bool returnreagent)
632 Unit* owner = GetOwner();
634 if(owner)
636 if(owner->GetTypeId()==TYPEID_PLAYER)
638 ((Player*)owner)->RemovePet(this,mode,returnreagent);
639 return;
642 // only if current pet in slot
643 if(owner->GetPetGUID()==GetGUID())
644 owner->SetPet(0);
647 CleanupsBeforeDelete();
648 AddObjectToRemoveList();
649 m_removed = true;
652 void Pet::GivePetXP(uint32 xp)
654 if(getPetType() != HUNTER_PET)
655 return;
657 if ( xp < 1 )
658 return;
660 if(!isAlive())
661 return;
663 uint32 level = getLevel();
665 // XP to money conversion processed in Player::RewardQuest
666 if(level >= sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL))
667 return;
669 uint32 curXP = GetUInt32Value(UNIT_FIELD_PETEXPERIENCE);
670 uint32 nextLvlXP = GetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP);
671 uint32 newXP = curXP + xp;
673 if(newXP >= nextLvlXP && level+1 > GetOwner()->getLevel())
675 SetUInt32Value(UNIT_FIELD_PETEXPERIENCE, nextLvlXP-1);
676 return;
679 while( newXP >= nextLvlXP && level < sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL) )
681 newXP -= nextLvlXP;
683 SetLevel( level + 1 );
684 SetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP, objmgr.GetXPForLevel(level+1)/4);
686 level = getLevel();
687 nextLvlXP = GetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP);
688 GivePetLevel(level);
691 SetUInt32Value(UNIT_FIELD_PETEXPERIENCE, newXP);
694 void Pet::GivePetLevel(uint32 level)
696 if(!level)
697 return;
699 InitStatsForLevel(level);
700 InitTalentForLevel();
703 bool Pet::CreateBaseAtCreature(Creature* creature)
705 if(!creature)
707 sLog.outError("CRITICAL: NULL pointer parsed into CreateBaseAtCreature()");
708 return false;
710 uint32 guid=objmgr.GenerateLowGuid(HIGHGUID_PET);
712 sLog.outBasic("SetInstanceID()");
713 SetInstanceId(creature->GetInstanceId());
715 sLog.outBasic("Create pet");
716 uint32 pet_number = objmgr.GeneratePetNumber();
717 if(!Create(guid, creature->GetMap(), creature->GetPhaseMask(), creature->GetEntry(), pet_number))
718 return false;
720 Relocate(creature->GetPositionX(), creature->GetPositionY(), creature->GetPositionZ(), creature->GetOrientation());
722 if(!IsPositionValid())
724 sLog.outError("Pet (guidlow %d, entry %d) not created base at creature. Suggested coordinates isn't valid (X: %f Y: %f)",
725 GetGUIDLow(), GetEntry(), GetPositionX(), GetPositionY());
726 return false;
729 CreatureInfo const *cinfo = GetCreatureInfo();
730 if(!cinfo)
732 sLog.outError("CreateBaseAtCreature() failed, creatureInfo is missing!");
733 return false;
736 if(cinfo->type == CREATURE_TYPE_CRITTER)
738 setPetType(MINI_PET);
739 return true;
741 SetDisplayId(creature->GetDisplayId());
742 SetNativeDisplayId(creature->GetNativeDisplayId());
743 SetMaxPower(POWER_HAPPINESS, GetCreatePowers(POWER_HAPPINESS));
744 SetPower(POWER_HAPPINESS, 166500);
745 setPowerType(POWER_FOCUS);
746 SetUInt32Value(UNIT_FIELD_PET_NAME_TIMESTAMP, 0);
747 SetUInt32Value(UNIT_FIELD_PETEXPERIENCE, 0);
748 SetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP, objmgr.GetXPForLevel(creature->getLevel())/4);
749 SetUInt32Value(UNIT_NPC_FLAGS, 0);
751 if(CreatureFamilyEntry const* cFamily = sCreatureFamilyStore.LookupEntry(cinfo->family))
752 SetName(cFamily->Name[sWorld.GetDefaultDbcLocale()]);
753 else
754 SetName(creature->GetNameForLocaleIdx(objmgr.GetDBCLocaleIndex()));
756 if(cinfo->type == CREATURE_TYPE_BEAST)
758 SetUInt32Value(UNIT_FIELD_BYTES_0, 0x02020100);
759 SetByteValue(UNIT_FIELD_BYTES_2, 0, SHEATH_STATE_MELEE );
760 SetByteValue(UNIT_FIELD_BYTES_2, 2, UNIT_RENAME_ALLOWED);
761 SetUInt32Value(UNIT_MOD_CAST_SPEED, creature->GetUInt32Value(UNIT_MOD_CAST_SPEED));
763 return true;
766 bool Pet::InitStatsForLevel(uint32 petlevel)
768 CreatureInfo const *cinfo = GetCreatureInfo();
769 assert(cinfo);
771 Unit* owner = GetOwner();
772 if(!owner)
774 sLog.outError("attempt to summon pet (Entry %u) without owner! Attempt terminated.", cinfo->Entry);
775 return false;
778 uint32 creature_ID = (getPetType() == HUNTER_PET) ? 1 : cinfo->Entry;
780 SetLevel(petlevel);
782 SetMeleeDamageSchool(SpellSchools(cinfo->dmgschool));
784 SetModifierValue(UNIT_MOD_ARMOR, BASE_VALUE, float(petlevel*50));
786 SetAttackTime(BASE_ATTACK, BASE_ATTACK_TIME);
787 SetAttackTime(OFF_ATTACK, BASE_ATTACK_TIME);
788 SetAttackTime(RANGED_ATTACK, BASE_ATTACK_TIME);
790 SetFloatValue(UNIT_MOD_CAST_SPEED, 1.0);
792 CreatureFamilyEntry const* cFamily = sCreatureFamilyStore.LookupEntry(cinfo->family);
793 if(cFamily && cFamily->minScale > 0.0f && getPetType()==HUNTER_PET)
795 float scale;
796 if (getLevel() >= cFamily->maxScaleLevel)
797 scale = cFamily->maxScale;
798 else if (getLevel() <= cFamily->minScaleLevel)
799 scale = cFamily->minScale;
800 else
801 scale = cFamily->minScale + (getLevel() - cFamily->minScaleLevel) / cFamily->maxScaleLevel * (cFamily->maxScale - cFamily->minScale);
803 SetFloatValue(OBJECT_FIELD_SCALE_X, scale);
805 m_bonusdamage = 0;
807 int32 createResistance[MAX_SPELL_SCHOOL] = {0,0,0,0,0,0,0};
809 if(cinfo && getPetType() != HUNTER_PET)
811 createResistance[SPELL_SCHOOL_HOLY] = cinfo->resistance1;
812 createResistance[SPELL_SCHOOL_FIRE] = cinfo->resistance2;
813 createResistance[SPELL_SCHOOL_NATURE] = cinfo->resistance3;
814 createResistance[SPELL_SCHOOL_FROST] = cinfo->resistance4;
815 createResistance[SPELL_SCHOOL_SHADOW] = cinfo->resistance5;
816 createResistance[SPELL_SCHOOL_ARCANE] = cinfo->resistance6;
819 switch(getPetType())
821 case SUMMON_PET:
823 if(owner->GetTypeId() == TYPEID_PLAYER)
825 switch(owner->getClass())
827 case CLASS_WARLOCK:
830 //the damage bonus used for pets is either fire or shadow damage, whatever is higher
831 uint32 fire = owner->GetUInt32Value(PLAYER_FIELD_MOD_DAMAGE_DONE_POS + SPELL_SCHOOL_FIRE);
832 uint32 shadow = owner->GetUInt32Value(PLAYER_FIELD_MOD_DAMAGE_DONE_POS + SPELL_SCHOOL_SHADOW);
833 uint32 val = (fire > shadow) ? fire : shadow;
835 SetBonusDamage(int32 (val * 0.15f));
836 //bonusAP += val * 0.57;
837 break;
839 case CLASS_MAGE:
841 //40% damage bonus of mage's frost damage
842 float val = owner->GetUInt32Value(PLAYER_FIELD_MOD_DAMAGE_DONE_POS + SPELL_SCHOOL_FROST) * 0.4;
843 if(val < 0)
844 val = 0;
845 SetBonusDamage( int32(val));
846 break;
848 default:
849 break;
853 SetBaseWeaponDamage(BASE_ATTACK, MINDAMAGE, float(petlevel - (petlevel / 4)) );
854 SetBaseWeaponDamage(BASE_ATTACK, MAXDAMAGE, float(petlevel + (petlevel / 4)) );
856 //SetModifierValue(UNIT_MOD_ATTACK_POWER, BASE_VALUE, float(cinfo->attackpower));
858 PetLevelInfo const* pInfo = objmgr.GetPetLevelInfo(creature_ID, petlevel);
859 if(pInfo) // exist in DB
861 SetCreateHealth(pInfo->health);
862 SetCreateMana(pInfo->mana);
864 if(pInfo->armor > 0)
865 SetModifierValue(UNIT_MOD_ARMOR, BASE_VALUE, float(pInfo->armor));
867 for(int stat = 0; stat < MAX_STATS; ++stat)
869 SetCreateStat(Stats(stat), float(pInfo->stats[stat]));
872 else // not exist in DB, use some default fake data
874 sLog.outErrorDb("Summoned pet (Entry: %u) not have pet stats data in DB",cinfo->Entry);
876 // remove elite bonuses included in DB values
877 SetCreateHealth(uint32(((float(cinfo->maxhealth) / cinfo->maxlevel) / (1 + 2 * cinfo->rank)) * petlevel) );
878 SetCreateMana( uint32(((float(cinfo->maxmana) / cinfo->maxlevel) / (1 + 2 * cinfo->rank)) * petlevel) );
880 SetCreateStat(STAT_STRENGTH, 22);
881 SetCreateStat(STAT_AGILITY, 22);
882 SetCreateStat(STAT_STAMINA, 25);
883 SetCreateStat(STAT_INTELLECT, 28);
884 SetCreateStat(STAT_SPIRIT, 27);
886 break;
888 case HUNTER_PET:
890 SetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP, objmgr.GetXPForLevel(petlevel)/4);
891 learnLevelupSpells();
892 //these formula may not be correct; however, it is designed to be close to what it should be
893 //this makes dps 0.5 of pets level
894 SetBaseWeaponDamage(BASE_ATTACK, MINDAMAGE, float(petlevel - (petlevel / 4)) );
895 //damage range is then petlevel / 2
896 SetBaseWeaponDamage(BASE_ATTACK, MAXDAMAGE, float(petlevel + (petlevel / 4)) );
897 //damage is increased afterwards as strength and pet scaling modify attack power
899 //stored standard pet stats are entry 1 in pet_levelinfo
900 PetLevelInfo const* pInfo = objmgr.GetPetLevelInfo(creature_ID, petlevel);
901 if(pInfo) // exist in DB
903 SetCreateHealth(pInfo->health);
904 SetModifierValue(UNIT_MOD_ARMOR, BASE_VALUE, float(pInfo->armor));
905 //SetModifierValue(UNIT_MOD_ATTACK_POWER, BASE_VALUE, float(cinfo->attackpower));
907 for( int i = STAT_STRENGTH; i < MAX_STATS; i++)
909 SetCreateStat(Stats(i), float(pInfo->stats[i]));
912 else // not exist in DB, use some default fake data
914 sLog.outErrorDb("Hunter pet levelstats missing in DB");
916 // remove elite bonuses included in DB values
917 SetCreateHealth( uint32(((float(cinfo->maxhealth) / cinfo->maxlevel) / (1 + 2 * cinfo->rank)) * petlevel) );
919 SetCreateStat(STAT_STRENGTH, 22);
920 SetCreateStat(STAT_AGILITY, 22);
921 SetCreateStat(STAT_STAMINA, 25);
922 SetCreateStat(STAT_INTELLECT, 28);
923 SetCreateStat(STAT_SPIRIT, 27);
925 break;
927 case GUARDIAN_PET:
928 SetUInt32Value(UNIT_FIELD_PETEXPERIENCE, 0);
929 SetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP, 1000);
931 SetCreateMana(28 + 10*petlevel);
932 SetCreateHealth(28 + 30*petlevel);
934 // FIXME: this is wrong formula, possible each guardian pet have own damage formula
935 //these formula may not be correct; however, it is designed to be close to what it should be
936 //this makes dps 0.5 of pets level
937 SetBaseWeaponDamage(BASE_ATTACK, MINDAMAGE, float(petlevel - (petlevel / 4)));
938 //damage range is then petlevel / 2
939 SetBaseWeaponDamage(BASE_ATTACK, MAXDAMAGE, float(petlevel + (petlevel / 4)));
940 break;
941 default:
942 sLog.outError("Pet have incorrect type (%u) for levelup.", getPetType());
943 break;
946 for (int i = SPELL_SCHOOL_HOLY; i < MAX_SPELL_SCHOOL; ++i)
947 SetModifierValue(UnitMods(UNIT_MOD_RESISTANCE_START + i), BASE_VALUE, float(createResistance[i]));
949 UpdateAllStats();
951 SetHealth(GetMaxHealth());
952 SetPower(POWER_MANA, GetMaxPower(POWER_MANA));
954 return true;
957 bool Pet::HaveInDiet(ItemPrototype const* item) const
959 if (!item->FoodType)
960 return false;
962 CreatureInfo const* cInfo = GetCreatureInfo();
963 if(!cInfo)
964 return false;
966 CreatureFamilyEntry const* cFamily = sCreatureFamilyStore.LookupEntry(cInfo->family);
967 if(!cFamily)
968 return false;
970 uint32 diet = cFamily->petFoodMask;
971 uint32 FoodMask = 1 << (item->FoodType-1);
972 return diet & FoodMask;
975 uint32 Pet::GetCurrentFoodBenefitLevel(uint32 itemlevel)
977 // -5 or greater food level
978 if(getLevel() <= itemlevel + 5) //possible to feed level 60 pet with level 55 level food for full effect
979 return 35000;
980 // -10..-6
981 else if(getLevel() <= itemlevel + 10) //pure guess, but sounds good
982 return 17000;
983 // -14..-11
984 else if(getLevel() <= itemlevel + 14) //level 55 food gets green on 70, makes sense to me
985 return 8000;
986 // -15 or less
987 else
988 return 0; //food too low level
991 void Pet::_LoadSpellCooldowns()
993 m_CreatureSpellCooldowns.clear();
994 m_CreatureCategoryCooldowns.clear();
996 QueryResult *result = CharacterDatabase.PQuery("SELECT spell,time FROM pet_spell_cooldown WHERE guid = '%u'",m_charmInfo->GetPetNumber());
998 if(result)
1000 time_t curTime = time(NULL);
1002 WorldPacket data(SMSG_SPELL_COOLDOWN, (8+1+result->GetRowCount()*8));
1003 data << GetGUID();
1004 data << uint8(0x0); // flags (0x1, 0x2)
1008 Field *fields = result->Fetch();
1010 uint32 spell_id = fields[0].GetUInt32();
1011 time_t db_time = (time_t)fields[1].GetUInt64();
1013 if(!sSpellStore.LookupEntry(spell_id))
1015 sLog.outError("Pet %u have unknown spell %u in `pet_spell_cooldown`, skipping.",m_charmInfo->GetPetNumber(),spell_id);
1016 continue;
1019 // skip outdated cooldown
1020 if(db_time <= curTime)
1021 continue;
1023 data << uint32(spell_id);
1024 data << uint32(uint32(db_time-curTime)*IN_MILISECONDS);
1026 _AddCreatureSpellCooldown(spell_id,db_time);
1028 sLog.outDebug("Pet (Number: %u) spell %u cooldown loaded (%u secs).", m_charmInfo->GetPetNumber(), spell_id, uint32(db_time-curTime));
1030 while( result->NextRow() );
1032 delete result;
1034 if(!m_CreatureSpellCooldowns.empty() && GetOwner())
1036 ((Player*)GetOwner())->GetSession()->SendPacket(&data);
1041 void Pet::_SaveSpellCooldowns()
1043 CharacterDatabase.PExecute("DELETE FROM pet_spell_cooldown WHERE guid = '%u'", m_charmInfo->GetPetNumber());
1045 time_t curTime = time(NULL);
1047 // remove oudated and save active
1048 for(CreatureSpellCooldowns::iterator itr = m_CreatureSpellCooldowns.begin();itr != m_CreatureSpellCooldowns.end();)
1050 if(itr->second <= curTime)
1051 m_CreatureSpellCooldowns.erase(itr++);
1052 else
1054 CharacterDatabase.PExecute("INSERT INTO pet_spell_cooldown (guid,spell,time) VALUES ('%u', '%u', '" I64FMTD "')", m_charmInfo->GetPetNumber(), itr->first, uint64(itr->second));
1055 ++itr;
1060 void Pet::_LoadSpells()
1062 QueryResult *result = CharacterDatabase.PQuery("SELECT spell,active FROM pet_spell WHERE guid = '%u'",m_charmInfo->GetPetNumber());
1064 if(result)
1068 Field *fields = result->Fetch();
1070 addSpell(fields[0].GetUInt32(), fields[1].GetUInt16(), PETSPELL_UNCHANGED);
1072 while( result->NextRow() );
1074 delete result;
1078 void Pet::_SaveSpells()
1080 for (PetSpellMap::const_iterator itr = m_spells.begin(), next = m_spells.begin(); itr != m_spells.end(); itr = next)
1082 ++next;
1083 if (itr->second->type == PETSPELL_FAMILY) continue; // prevent saving family passives to DB
1084 if (itr->second->state == PETSPELL_REMOVED || itr->second->state == PETSPELL_CHANGED)
1085 CharacterDatabase.PExecute("DELETE FROM pet_spell WHERE guid = '%u' and spell = '%u'", m_charmInfo->GetPetNumber(), itr->first);
1086 if (itr->second->state == PETSPELL_NEW || itr->second->state == PETSPELL_CHANGED)
1087 CharacterDatabase.PExecute("INSERT INTO pet_spell (guid,spell,active) VALUES ('%u', '%u', '%u')", m_charmInfo->GetPetNumber(), itr->first, itr->second->active);
1089 if (itr->second->state == PETSPELL_REMOVED)
1090 _removeSpell(itr->first);
1091 else
1092 itr->second->state = PETSPELL_UNCHANGED;
1096 void Pet::_LoadAuras(uint32 timediff)
1098 m_Auras.clear();
1099 for (int i = 0; i < TOTAL_AURAS; i++)
1100 m_modAuras[i].clear();
1102 QueryResult *result = CharacterDatabase.PQuery("SELECT caster_guid,spell,effect_index,stackcount,amount,maxduration,remaintime,remaincharges FROM pet_aura WHERE guid = '%u'",m_charmInfo->GetPetNumber());
1104 if(result)
1108 Field *fields = result->Fetch();
1109 uint64 caster_guid = fields[0].GetUInt64();
1110 uint32 spellid = fields[1].GetUInt32();
1111 uint32 effindex = fields[2].GetUInt32();
1112 uint32 stackcount= fields[3].GetUInt32();
1113 int32 damage = (int32)fields[4].GetUInt32();
1114 int32 maxduration = (int32)fields[5].GetUInt32();
1115 int32 remaintime = (int32)fields[6].GetUInt32();
1116 int32 remaincharges = (int32)fields[7].GetUInt32();
1118 SpellEntry const* spellproto = sSpellStore.LookupEntry(spellid);
1119 if(!spellproto)
1121 sLog.outError("Unknown aura (spellid %u, effindex %u), ignore.",spellid,effindex);
1122 continue;
1125 if(effindex >= 3)
1127 sLog.outError("Invalid effect index (spellid %u, effindex %u), ignore.",spellid,effindex);
1128 continue;
1131 // negative effects should continue counting down after logout
1132 if (remaintime != -1 && !IsPositiveEffect(spellid, effindex))
1134 if(remaintime <= int32(timediff))
1135 continue;
1137 remaintime -= timediff;
1140 // prevent wrong values of remaincharges
1141 if(spellproto->procCharges)
1143 if(remaincharges <= 0 || remaincharges > spellproto->procCharges)
1144 remaincharges = spellproto->procCharges;
1146 else
1147 remaincharges = -1;
1149 /// do not load single target auras (unless they were cast by the player)
1150 if (caster_guid != GetGUID() && IsSingleTargetSpell(spellproto))
1151 continue;
1153 for(uint32 i=0; i<stackcount; i++)
1155 Aura* aura = CreateAura(spellproto, effindex, NULL, this, NULL);
1157 if(!damage)
1158 damage = aura->GetModifier()->m_amount;
1159 aura->SetLoadedState(caster_guid,damage,maxduration,remaintime,remaincharges);
1160 AddAura(aura);
1163 while( result->NextRow() );
1165 delete result;
1169 void Pet::_SaveAuras()
1171 CharacterDatabase.PExecute("DELETE FROM pet_aura WHERE guid = '%u'", m_charmInfo->GetPetNumber());
1173 AuraMap const& auras = GetAuras();
1174 if (auras.empty())
1175 return;
1177 spellEffectPair lastEffectPair = auras.begin()->first;
1178 uint32 stackCounter = 1;
1180 for(AuraMap::const_iterator itr = auras.begin(); ; ++itr)
1182 if(itr == auras.end() || lastEffectPair != itr->first)
1184 AuraMap::const_iterator itr2 = itr;
1185 // save previous spellEffectPair to db
1186 itr2--;
1187 SpellEntry const *spellInfo = itr2->second->GetSpellProto();
1188 /// do not save single target auras (unless they were cast by the player)
1189 if (!(itr2->second->GetCasterGUID() != GetGUID() && IsSingleTargetSpell(spellInfo)))
1191 if(!itr2->second->IsPassive())
1193 // skip all auras from spell that apply at cast SPELL_AURA_MOD_SHAPESHIFT or pet area auras.
1194 uint8 i;
1195 for (i = 0; i < 3; i++)
1196 if (spellInfo->EffectApplyAuraName[i] == SPELL_AURA_MOD_STEALTH ||
1197 spellInfo->Effect[i] == SPELL_EFFECT_APPLY_AREA_AURA_OWNER ||
1198 spellInfo->Effect[i] == SPELL_EFFECT_APPLY_AREA_AURA_PET )
1199 break;
1201 if (i == 3)
1203 CharacterDatabase.PExecute("INSERT INTO pet_aura (guid,caster_guid,spell,effect_index,stackcount,amount,maxduration,remaintime,remaincharges) "
1204 "VALUES ('%u', '" I64FMTD "', '%u', '%u', '%u', '%d', '%d', '%d', '%d')",
1205 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()));
1209 if(itr == auras.end())
1210 break;
1213 if (lastEffectPair == itr->first)
1214 stackCounter++;
1215 else
1217 lastEffectPair = itr->first;
1218 stackCounter = 1;
1223 bool Pet::addSpell(uint32 spell_id, uint16 active, PetSpellState state, PetSpellType type)
1225 SpellEntry const *spellInfo = sSpellStore.LookupEntry(spell_id);
1226 if (!spellInfo)
1228 // do pet spell book cleanup
1229 if(state == PETSPELL_UNCHANGED) // spell load case
1231 sLog.outError("Pet::addSpell: Non-existed in SpellStore spell #%u request, deleting for all pets in `pet_spell`.",spell_id);
1232 CharacterDatabase.PExecute("DELETE FROM pet_spell WHERE spell = '%u'",spell_id);
1234 else
1235 sLog.outError("Pet::addSpell: Non-existed in SpellStore spell #%u request.",spell_id);
1237 return false;
1240 PetSpellMap::iterator itr = m_spells.find(spell_id);
1241 if (itr != m_spells.end())
1243 if (itr->second->state == PETSPELL_REMOVED)
1245 delete itr->second;
1246 m_spells.erase(itr);
1247 state = PETSPELL_CHANGED;
1249 else if (state == PETSPELL_UNCHANGED && itr->second->state != PETSPELL_UNCHANGED)
1251 // can be in case spell loading but learned at some previous spell loading
1252 itr->second->state = PETSPELL_UNCHANGED;
1253 return false;
1255 else
1256 return false;
1259 uint32 oldspell_id = 0;
1261 PetSpell *newspell = new PetSpell;
1262 newspell->state = state;
1263 newspell->type = type;
1265 if(active == ACT_DECIDE) //active was not used before, so we save it's autocast/passive state here
1267 if(IsPassiveSpell(spell_id))
1268 newspell->active = ACT_PASSIVE;
1269 else
1270 newspell->active = ACT_DISABLED;
1272 else
1273 newspell->active = active;
1275 // talent: unlearn all other talent ranks (high and low)
1276 if(TalentSpellPos const* talentPos = GetTalentSpellPos(spell_id))
1278 if(TalentEntry const *talentInfo = sTalentStore.LookupEntry( talentPos->talent_id ))
1280 for(int i=0; i < MAX_TALENT_RANK; ++i)
1282 // skip learning spell and no rank spell case
1283 uint32 rankSpellId = talentInfo->RankID[i];
1284 if(!rankSpellId || rankSpellId==spell_id)
1285 continue;
1287 // skip unknown ranks
1288 if(!HasSpell(rankSpellId))
1289 continue;
1290 removeSpell(rankSpellId);
1294 else if(uint32 chainstart = spellmgr.GetFirstSpellInChain(spell_id))
1296 for (PetSpellMap::iterator itr = m_spells.begin(); itr != m_spells.end(); ++itr)
1298 if(itr->second->state == PETSPELL_REMOVED) continue;
1300 if(spellmgr.GetFirstSpellInChain(itr->first) == chainstart)
1302 newspell->active = itr->second->active;
1304 if(newspell->active == ACT_ENABLED)
1305 ToggleAutocast(itr->first, false);
1307 oldspell_id = itr->first;
1308 unlearnSpell(itr->first);
1309 break;
1314 m_spells[spell_id] = newspell;
1316 if (IsPassiveSpell(spell_id))
1317 CastSpell(this, spell_id, true);
1318 else if(state == PETSPELL_NEW)
1319 m_charmInfo->AddSpellToAB(oldspell_id, spell_id);
1321 if(newspell->active == ACT_ENABLED)
1322 ToggleAutocast(spell_id, true);
1324 uint32 talentCost = GetTalentSpellCost(spell_id);
1325 if (talentCost)
1327 int32 free_points = GetMaxTalentPointsForLevel(getLevel());
1328 m_usedTalentCount+=talentCost;
1329 // update free talent points
1330 free_points-=m_usedTalentCount;
1331 SetFreeTalentPoints(free_points > 0 ? free_points : 0);
1333 return true;
1336 bool Pet::learnSpell(uint32 spell_id)
1338 // prevent duplicated entires in spell book
1339 if (!addSpell(spell_id))
1340 return false;
1342 Unit* owner = GetOwner();
1343 if(owner && owner->GetTypeId() == TYPEID_PLAYER)
1345 if(!m_loading)
1347 WorldPacket data(SMSG_PET_LEARNED_SPELL, 2);
1348 data << uint16(spell_id);
1349 ((Player*)owner)->GetSession()->SendPacket(&data);
1351 ((Player*)owner)->PetSpellInitialize();
1353 return true;
1356 void Pet::learnLevelupSpells()
1358 PetLevelupSpellSet const *levelupSpells = spellmgr.GetPetLevelupSpellList(GetCreatureInfo()->family);
1359 if(!levelupSpells)
1360 return;
1362 uint32 level = getLevel();
1364 for(PetLevelupSpellSet::const_iterator itr = levelupSpells->begin(); itr != levelupSpells->end(); ++itr)
1366 if(itr->first <= level)
1367 learnSpell(itr->second);
1368 else
1369 unlearnSpell(itr->second);
1373 bool Pet::unlearnSpell(uint32 spell_id)
1375 if(removeSpell(spell_id))
1377 if(GetOwner()->GetTypeId() == TYPEID_PLAYER)
1379 if(!m_loading)
1381 WorldPacket data(SMSG_PET_REMOVED_SPELL, 2);
1382 data << uint16(spell_id);
1383 ((Player*)GetOwner())->GetSession()->SendPacket(&data);
1386 return true;
1388 return false;
1391 bool Pet::removeSpell(uint32 spell_id)
1393 PetSpellMap::iterator itr = m_spells.find(spell_id);
1394 if (itr == m_spells.end())
1395 return false;
1397 if(itr->second->state == PETSPELL_REMOVED)
1398 return false;
1400 if(itr->second->state == PETSPELL_NEW)
1402 delete itr->second;
1403 m_spells.erase(itr);
1405 else
1406 itr->second->state = PETSPELL_REMOVED;
1408 RemoveAurasDueToSpell(spell_id);
1410 uint32 talentCost = GetTalentSpellCost(spell_id);
1411 if (talentCost > 0)
1413 if (m_usedTalentCount > talentCost)
1414 m_usedTalentCount-=talentCost;
1415 else
1416 m_usedTalentCount = 0;
1417 // update free talent points
1418 int32 free_points = GetMaxTalentPointsForLevel(getLevel()) - m_usedTalentCount;
1419 SetFreeTalentPoints(free_points > 0 ? free_points : 0);
1422 return true;
1425 bool Pet::_removeSpell(uint32 spell_id)
1427 PetSpellMap::iterator itr = m_spells.find(spell_id);
1428 if (itr != m_spells.end())
1430 delete itr->second;
1431 m_spells.erase(itr);
1432 return true;
1434 return false;
1437 void Pet::InitPetCreateSpells()
1439 m_charmInfo->InitPetActionBar();
1441 for (PetSpellMap::iterator i = m_spells.begin(); i != m_spells.end(); ++i)
1442 delete i->second;
1443 m_spells.clear();
1445 uint32 petspellid;
1446 PetCreateSpellEntry const* CreateSpells = objmgr.GetPetCreateSpellEntry(GetEntry());
1447 if(CreateSpells)
1449 for(uint8 i = 0; i < 4; i++)
1451 if(!CreateSpells->spellid[i])
1452 break;
1454 SpellEntry const *learn_spellproto = sSpellStore.LookupEntry(CreateSpells->spellid[i]);
1455 if(!learn_spellproto)
1456 continue;
1458 if(learn_spellproto->Effect[0] == SPELL_EFFECT_LEARN_SPELL || learn_spellproto->Effect[0] == SPELL_EFFECT_LEARN_PET_SPELL)
1460 petspellid = learn_spellproto->EffectTriggerSpell[0];
1461 Unit* owner = GetOwner();
1462 if(owner->GetTypeId() == TYPEID_PLAYER && !((Player*)owner)->HasSpell(learn_spellproto->Id))
1464 if(IsPassiveSpell(petspellid)) //learn passive skills when tamed, not sure if thats right
1465 ((Player*)owner)->learnSpell(learn_spellproto->Id,false);
1466 else
1467 AddTeachSpell(learn_spellproto->EffectTriggerSpell[0], learn_spellproto->Id);
1470 else
1471 petspellid = learn_spellproto->Id;
1473 addSpell(petspellid);
1477 LearnPetPassives();
1479 CastPetAuras(false);
1482 void Pet::CheckLearning(uint32 spellid)
1484 //charmed case -> prevent crash
1485 if(GetTypeId() == TYPEID_PLAYER || getPetType() != HUNTER_PET)
1486 return;
1488 Unit* owner = GetOwner();
1490 if(m_teachspells.empty() || !owner || owner->GetTypeId() != TYPEID_PLAYER)
1491 return;
1493 TeachSpellMap::iterator itr = m_teachspells.find(spellid);
1494 if(itr == m_teachspells.end())
1495 return;
1497 if(urand(0, 100) < 10)
1499 ((Player*)owner)->learnSpell(itr->second,false);
1500 m_teachspells.erase(itr);
1504 bool Pet::resetTalents(bool no_cost)
1506 Unit *owner = GetOwner();
1507 if (!owner || owner->GetTypeId()!=TYPEID_PLAYER)
1508 return false;
1510 CreatureInfo const * ci = GetCreatureInfo();
1511 if(!ci)
1512 return false;
1513 // Check pet talent type
1514 CreatureFamilyEntry const *pet_family = sCreatureFamilyStore.LookupEntry(ci->family);
1515 if(!pet_family || pet_family->petTalentType < 0)
1516 return false;
1518 Player *player = (Player *)owner;
1520 uint32 level = getLevel();
1521 uint32 talentPointsForLevel = GetMaxTalentPointsForLevel(level);
1523 if (m_usedTalentCount == 0)
1525 SetFreeTalentPoints(talentPointsForLevel);
1526 return false;
1529 uint32 cost = 0;
1531 if(!no_cost)
1533 cost = resetTalentsCost();
1535 if (player->GetMoney() < cost)
1537 player->SendBuyError( BUY_ERR_NOT_ENOUGHT_MONEY, 0, 0, 0);
1538 return false;
1542 for (unsigned int i = 0; i < sTalentStore.GetNumRows(); i++)
1544 TalentEntry const *talentInfo = sTalentStore.LookupEntry(i);
1546 if (!talentInfo) continue;
1548 TalentTabEntry const *talentTabInfo = sTalentTabStore.LookupEntry( talentInfo->TalentTab );
1550 if(!talentTabInfo)
1551 continue;
1553 // unlearn only talents for pets family talent type
1554 if(!((1 << pet_family->petTalentType) & talentTabInfo->petTalentMask))
1555 continue;
1557 for (int j = 0; j < MAX_TALENT_RANK; j++)
1559 for(PetSpellMap::iterator itr = m_spells.begin(); itr != m_spells.end();)
1561 if(itr->second->state == PETSPELL_REMOVED)
1563 ++itr;
1564 continue;
1566 // remove learned spells (all ranks)
1567 uint32 itrFirstId = spellmgr.GetFirstSpellInChain(itr->first);
1569 // unlearn if first rank is talent or learned by talent
1570 if (itrFirstId == talentInfo->RankID[j] || spellmgr.IsSpellLearnToSpell(talentInfo->RankID[j],itrFirstId))
1572 removeSpell(itr->first);
1573 itr = m_spells.begin();
1574 continue;
1576 else
1577 ++itr;
1582 SetFreeTalentPoints(talentPointsForLevel);
1584 if(!no_cost)
1586 player->ModifyMoney(-(int32)cost);
1588 m_resetTalentsCost = cost;
1589 m_resetTalentsTime = time(NULL);
1591 player->PetSpellInitialize();
1592 return true;
1595 void Pet::InitTalentForLevel()
1597 uint32 level = getLevel();
1598 uint32 talentPointsForLevel = GetMaxTalentPointsForLevel(level);
1599 // Reset talents in case low level (on level down) or wrong points for level (hunter can unlearn TP increase talent)
1600 if(talentPointsForLevel == 0 || m_usedTalentCount > talentPointsForLevel)
1602 // Remove all talent points
1603 resetTalents(true);
1605 SetFreeTalentPoints(talentPointsForLevel - m_usedTalentCount);
1608 uint32 Pet::resetTalentsCost() const
1610 uint32 days = (sWorld.GetGameTime() - m_resetTalentsTime)/DAY;
1612 // The first time reset costs 10 silver; after 1 day cost is reset to 10 silver
1613 if(m_resetTalentsCost < 10*SILVER || days > 0)
1614 return 10*SILVER;
1615 // then 50 silver
1616 else if(m_resetTalentsCost < 50*SILVER)
1617 return 50*SILVER;
1618 // then 1 gold
1619 else if(m_resetTalentsCost < 1*GOLD)
1620 return 1*GOLD;
1621 // then increasing at a rate of 1 gold; cap 10 gold
1622 else
1623 return (m_resetTalentsCost + 1*GOLD > 10*GOLD ? 10*GOLD : m_resetTalentsCost + 1*GOLD);
1626 uint8 Pet::GetMaxTalentPointsForLevel(uint32 level)
1628 uint8 points = (level >= 20) ? ((level - 16) / 4) : 0;
1629 // Mod points from owner SPELL_AURA_MOD_PET_TALENT_POINTS
1630 if (Unit *owner = GetOwner())
1631 points+=owner->GetTotalAuraModifier(SPELL_AURA_MOD_PET_TALENT_POINTS);
1632 return points;
1635 void Pet::ToggleAutocast(uint32 spellid, bool apply)
1637 if(IsPassiveSpell(spellid))
1638 return;
1640 PetSpellMap::const_iterator itr = m_spells.find(spellid);
1642 int i;
1644 if(apply)
1646 for (i = 0; i < m_autospells.size() && m_autospells[i] != spellid; i++)
1647 ; // just search
1649 if (i == m_autospells.size())
1651 m_autospells.push_back(spellid);
1652 itr->second->active = ACT_ENABLED;
1653 itr->second->state = PETSPELL_CHANGED;
1656 else
1658 AutoSpellList::iterator itr2 = m_autospells.begin();
1659 for (i = 0; i < m_autospells.size() && m_autospells[i] != spellid; i++, itr2++)
1660 ; // just search
1662 if (i < m_autospells.size())
1664 m_autospells.erase(itr2);
1665 itr->second->active = ACT_DISABLED;
1666 itr->second->state = PETSPELL_CHANGED;
1671 bool Pet::Create(uint32 guidlow, Map *map, uint32 phaseMask, uint32 Entry, uint32 pet_number)
1673 SetMapId(map->GetId());
1674 SetInstanceId(map->GetInstanceId());
1675 SetPhaseMask(phaseMask,false);
1677 Object::_Create(guidlow, pet_number, HIGHGUID_PET);
1679 m_DBTableGuid = guidlow;
1680 m_originalEntry = Entry;
1682 if(!InitEntry(Entry))
1683 return false;
1685 SetByteValue(UNIT_FIELD_BYTES_2, 0, SHEATH_STATE_MELEE);
1687 if(getPetType() == MINI_PET) // always non-attackable
1688 SetFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_NON_ATTACKABLE);
1690 return true;
1693 bool Pet::HasSpell(uint32 spell) const
1695 PetSpellMap::const_iterator itr = m_spells.find(spell);
1696 return (itr != m_spells.end() && itr->second->state != PETSPELL_REMOVED );
1699 // Get all passive spells in our skill line
1700 void Pet::LearnPetPassives()
1702 CreatureInfo const* cInfo = GetCreatureInfo();
1703 if(!cInfo)
1704 return;
1706 CreatureFamilyEntry const* cFamily = sCreatureFamilyStore.LookupEntry(cInfo->family);
1707 if(!cFamily)
1708 return;
1710 PetFamilySpellsStore::const_iterator petStore = sPetFamilySpellsStore.find(cFamily->ID);
1711 if(petStore != sPetFamilySpellsStore.end())
1713 for(PetFamilySpellsSet::const_iterator petSet = petStore->second.begin(); petSet != petStore->second.end(); ++petSet)
1714 addSpell(*petSet, ACT_DECIDE, PETSPELL_NEW, PETSPELL_FAMILY);
1718 void Pet::CastPetAuras(bool current)
1720 Unit* owner = GetOwner();
1721 if(!owner)
1722 return;
1724 if(getPetType() != HUNTER_PET && (getPetType() != SUMMON_PET || owner->getClass() != CLASS_WARLOCK))
1725 return;
1727 for(PetAuraSet::iterator itr = owner->m_petAuras.begin(); itr != owner->m_petAuras.end();)
1729 PetAura const* pa = *itr;
1730 ++itr;
1732 if(!current && pa->IsRemovedOnChangePet())
1733 owner->RemovePetAura(pa);
1734 else
1735 CastPetAura(pa);
1739 void Pet::CastPetAura(PetAura const* aura)
1741 uint16 auraId = aura->GetAura(GetEntry());
1742 if(!auraId)
1743 return;
1745 if(auraId == 35696) // Demonic Knowledge
1747 int32 basePoints = int32(aura->GetDamage() * (GetStat(STAT_STAMINA) + GetStat(STAT_INTELLECT)) / 100);
1748 CastCustomSpell(this, auraId, &basePoints, NULL, NULL, true);
1750 else
1751 CastSpell(this, auraId, true);
1754 void Pet::learnSpellHighRank(uint32 spellid)
1756 learnSpell(spellid);
1758 SpellChainMapNext const& nextMap = spellmgr.GetSpellChainNext();
1759 for(SpellChainMapNext::const_iterator itr = nextMap.lower_bound(spellid); itr != nextMap.upper_bound(spellid); ++itr)
1760 learnSpellHighRank(itr->second);