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
20 #include "Database/DatabaseEnv.h"
22 #include "WorldPacket.h"
23 #include "ObjectMgr.h"
27 #include "SpellAuras.h"
28 #include "CreatureAI.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),
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
);
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);
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
)
85 uint32 ownerid
= owner
->GetGUIDLow();
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'",
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
);
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
);
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
);
115 Field
*fields
= result
->Fetch();
117 // update for case of current pet "slot = 0"
118 petentry
= fields
[1].GetUInt32();
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
)
137 PetType pet_type
= PetType(fields
[18].GetUInt8());
138 if(pet_type
==HUNTER_PET
)
140 CreatureInfo
const* creatureInfo
= objmgr
.GetCreatureTemplate(petentry
);
141 if(!creatureInfo
|| !creatureInfo
->isTameable(owner
->CanTameExoticPets()))
148 uint32 pet_number
= fields
[0].GetUInt32();
150 if (current
&& owner
->IsPetNeedBeTemporaryUnsummoned())
152 owner
->SetTemporaryUnsummonedPetNumber(pet_number
);
157 Map
*map
= owner
->GetMap();
158 uint32 guid
= objmgr
.GenerateLowGuid(HIGHGUID_PET
);
159 if (!Create(guid
, map
, owner
->GetPhaseMask(), petentry
, pet_number
))
166 owner
->GetClosePoint(px
, py
, pz
, GetObjectSize(), PET_FOLLOW_DIST
, PET_FOLLOW_ANGLE
);
168 Relocate(px
, py
, pz
, owner
->GetOrientation());
170 if (!IsPositionValid())
172 sLog
.outError("Pet (guidlow %d, entry %d) not loaded. Suggested coordinates isn't valid (X: %f Y: %f)",
173 GetGUIDLow(), GetEntry(), GetPositionX(), GetPositionY());
178 setPetType(pet_type
);
179 setFaction(owner
->getFaction());
180 SetUInt32Value(UNIT_CREATED_BY_SPELL
, summon_spell_id
);
182 CreatureInfo
const *cinfo
= GetCreatureInfo();
183 if (cinfo
->type
== CREATURE_TYPE_CRITTER
)
186 map
->Add((Creature
*)this);
191 m_charmInfo
->SetPetNumber(pet_number
, IsPermanentPetFor(owner
));
193 SetOwnerGUID(owner
->GetGUID());
194 SetDisplayId(fields
[3].GetUInt32());
195 SetNativeDisplayId(fields
[3].GetUInt32());
196 uint32 petlevel
= fields
[4].GetUInt32();
197 SetUInt32Value(UNIT_NPC_FLAGS
, UNIT_NPC_FLAG_NONE
);
198 SetName(fields
[8].GetString());
200 switch (getPetType())
203 petlevel
=owner
->getLevel();
205 SetUInt32Value(UNIT_FIELD_BYTES_0
, 2048);
206 SetUInt32Value(UNIT_FIELD_FLAGS
, UNIT_FLAG_PVP_ATTACKABLE
);
207 // this enables popup window (pet dismiss, cancel)
210 SetUInt32Value(UNIT_FIELD_BYTES_0
, 0x02020100);
211 SetSheath(SHEATH_STATE_MELEE
);
212 SetByteValue(UNIT_FIELD_BYTES_2
, 2, fields
[9].GetBool() ? UNIT_RENAME_NOT_ALLOWED
: UNIT_RENAME_ALLOWED
);
214 SetUInt32Value(UNIT_FIELD_FLAGS
, UNIT_FLAG_PVP_ATTACKABLE
);
215 // this enables popup window (pet abandon, cancel)
216 SetMaxPower(POWER_HAPPINESS
, GetCreatePowers(POWER_HAPPINESS
));
217 SetPower(POWER_HAPPINESS
, fields
[12].GetUInt32());
218 setPowerType(POWER_FOCUS
);
221 sLog
.outError("Pet have incorrect type (%u) for pet loading.", getPetType());
227 SetCanModifyStats(true);
228 InitStatsForLevel(petlevel
);
229 InitTalentForLevel(); // set original talents points before spell loading
231 SetUInt32Value(UNIT_FIELD_PET_NAME_TIMESTAMP
, time(NULL
));
232 SetUInt32Value(UNIT_FIELD_PETEXPERIENCE
, fields
[5].GetUInt32());
233 SetCreatorGUID(owner
->GetGUID());
235 m_charmInfo
->SetReactState(ReactStates(fields
[6].GetUInt8()));
237 uint32 savedhealth
= fields
[10].GetUInt32();
238 uint32 savedmana
= fields
[11].GetUInt32();
240 // set current pet as current
242 // 1..MAX_PET_STABLES in stable slot
243 // PET_SAVE_NOT_IN_SLOT(100) = not stable slot (summoning))
244 if (fields
[7].GetUInt32() != 0)
246 CharacterDatabase
.BeginTransaction();
247 CharacterDatabase
.PExecute("UPDATE character_pet SET slot = '%u' WHERE owner = '%u' AND slot = '%u' AND id <> '%u'",
248 PET_SAVE_NOT_IN_SLOT
, ownerid
, PET_SAVE_AS_CURRENT
, m_charmInfo
->GetPetNumber());
249 CharacterDatabase
.PExecute("UPDATE character_pet SET slot = '%u' WHERE owner = '%u' AND id = '%u'",
250 PET_SAVE_AS_CURRENT
, ownerid
, m_charmInfo
->GetPetNumber());
251 CharacterDatabase
.CommitTransaction();
254 // load action bar, if data broken will fill later by default spells.
255 if (!is_temporary_summoned
)
256 m_charmInfo
->LoadPetActionBar(fields
[13].GetCppString());
258 // since last save (in seconds)
259 uint32 timediff
= (time(NULL
) - fields
[14].GetUInt32());
261 m_resetTalentsCost
= fields
[15].GetUInt32();
262 m_resetTalentsTime
= fields
[16].GetUInt64();
266 //load spells/cooldowns/auras
267 _LoadAuras(timediff
);
270 if (is_temporary_summoned
)
272 // Temporary summoned pets always have initial spell list at load
273 InitPetCreateSpells();
278 CastPetAuras(current
);
281 if (getPetType() == SUMMON_PET
&& !current
) //all (?) summon pets come with full health when called, but not when they are current
283 SetHealth(GetMaxHealth());
284 SetPower(POWER_MANA
, GetMaxPower(POWER_MANA
));
288 SetHealth(savedhealth
> GetMaxHealth() ? GetMaxHealth() : savedhealth
);
289 SetPower(POWER_MANA
, savedmana
> GetMaxPower(POWER_MANA
) ? GetMaxPower(POWER_MANA
) : savedmana
);
293 map
->Add((Creature
*)this);
295 // Spells should be loaded after pet is added to map, because in CheckCast is check on it
297 InitLevelupSpellsForLevel();
299 CleanupActionBar(); // remove unknown spells from action bar after load
301 _LoadSpellCooldowns();
303 owner
->SetPet(this); // in DB stored only full controlled creature
304 sLog
.outDebug("New Pet has guid %u", GetGUIDLow());
306 if (owner
->GetTypeId() == TYPEID_PLAYER
)
308 ((Player
*)owner
)->PetSpellInitialize();
309 if(((Player
*)owner
)->GetGroup())
310 ((Player
*)owner
)->SetGroupUpdateFlag(GROUP_UPDATE_PET
);
312 ((Player
*)owner
)->SendTalentsInfoData(true);
315 if (owner
->GetTypeId() == TYPEID_PLAYER
&& getPetType() == HUNTER_PET
)
317 result
= CharacterDatabase
.PQuery("SELECT genitive, dative, accusative, instrumental, prepositional FROM character_pet_declinedname WHERE owner = '%u' AND id = '%u'", owner
->GetGUIDLow(), GetCharmInfo()->GetPetNumber());
322 delete m_declinedname
;
324 m_declinedname
= new DeclinedName
;
325 Field
*fields2
= result
->Fetch();
326 for(int i
= 0; i
< MAX_DECLINED_NAME_CASES
; ++i
)
328 m_declinedname
->name
[i
] = fields2
[i
].GetCppString();
335 SynchronizeLevelWithOwner();
339 void Pet::SavePetToDB(PetSaveMode mode
)
344 // save only fully controlled creature
348 // not save not player pets
349 if(!IS_PLAYER_GUID(GetOwnerGUID()))
352 Player
* pOwner
= (Player
*)GetOwner();
356 // not save pet as current if another pet temporary unsummoned
357 if (mode
== PET_SAVE_AS_CURRENT
&& pOwner
->GetTemporaryUnsummonedPetNumber() &&
358 pOwner
->GetTemporaryUnsummonedPetNumber() != m_charmInfo
->GetPetNumber())
360 // pet will lost anyway at restore temporary unsummoned
361 if(getPetType()==HUNTER_PET
)
365 mode
= PET_SAVE_NOT_IN_SLOT
;
368 uint32 curhealth
= GetHealth();
369 uint32 curmana
= GetPower(POWER_MANA
);
371 // stable and not in slot saves
372 if(mode
> PET_SAVE_AS_CURRENT
)
376 //only alive hunter pets get auras saved, the others don't
377 if(!(getPetType() == HUNTER_PET
&& isAlive()))
382 _SaveSpellCooldowns();
385 // current/stable/not_in_slot
386 if(mode
>= PET_SAVE_AS_CURRENT
)
388 uint32 owner
= GUID_LOPART(GetOwnerGUID());
389 std::string name
= m_name
;
390 CharacterDatabase
.escape_string(name
);
391 CharacterDatabase
.BeginTransaction();
392 // remove current data
393 CharacterDatabase
.PExecute("DELETE FROM character_pet WHERE owner = '%u' AND id = '%u'", owner
,m_charmInfo
->GetPetNumber() );
395 // prevent duplicate using slot (except PET_SAVE_NOT_IN_SLOT)
396 if(mode
<= PET_SAVE_LAST_STABLE_SLOT
)
397 CharacterDatabase
.PExecute("UPDATE character_pet SET slot = '%u' WHERE owner = '%u' AND slot = '%u'",
398 PET_SAVE_NOT_IN_SLOT
, owner
, uint32(mode
) );
400 // prevent existence another hunter pet in PET_SAVE_AS_CURRENT and PET_SAVE_NOT_IN_SLOT
401 if(getPetType()==HUNTER_PET
&& (mode
==PET_SAVE_AS_CURRENT
||mode
> PET_SAVE_LAST_STABLE_SLOT
))
402 CharacterDatabase
.PExecute("DELETE FROM character_pet WHERE owner = '%u' AND (slot = '%u' OR slot > '%u')",
403 owner
,PET_SAVE_AS_CURRENT
,PET_SAVE_LAST_STABLE_SLOT
);
405 std::ostringstream ss
;
406 ss
<< "INSERT INTO character_pet ( id, entry, owner, modelid, level, exp, Reactstate, slot, name, renamed, curhealth, curmana, curhappiness, abdata, savetime, resettalents_cost, resettalents_time, CreatedBySpell, PetType) "
408 << m_charmInfo
->GetPetNumber() << ", "
409 << GetEntry() << ", "
411 << GetNativeDisplayId() << ", "
412 << getLevel() << ", "
413 << GetUInt32Value(UNIT_FIELD_PETEXPERIENCE
) << ", "
414 << uint32(m_charmInfo
->GetReactState()) << ", "
415 << uint32(mode
) << ", '"
416 << name
.c_str() << "', "
417 << uint32((GetByteValue(UNIT_FIELD_BYTES_2
, 2) == UNIT_RENAME_ALLOWED
)?0:1) << ", "
418 << (curhealth
<1?1:curhealth
) << ", "
420 << GetPower(POWER_HAPPINESS
) << ", '";
422 // save only spell slots from action bar
423 for(uint32 i
= ACTION_BAR_INDEX_PET_SPELL_START
; i
< ACTION_BAR_INDEX_PET_SPELL_END
; ++i
)
425 ss
<< uint32(m_charmInfo
->GetActionBarEntry(i
)->GetType()) << " "
426 << uint32(m_charmInfo
->GetActionBarEntry(i
)->GetAction()) << " ";
430 << time(NULL
) << ", "
431 << uint32(m_resetTalentsCost
) << ", "
432 << uint64(m_resetTalentsTime
) << ", "
433 << GetUInt32Value(UNIT_CREATED_BY_SPELL
) << ", "
434 << uint32(getPetType()) << ")";
436 CharacterDatabase
.Execute( ss
.str().c_str() );
437 CharacterDatabase
.CommitTransaction();
443 DeleteFromDB(m_charmInfo
->GetPetNumber());
447 void Pet::DeleteFromDB(uint32 guidlow
)
449 CharacterDatabase
.PExecute("DELETE FROM character_pet WHERE id = '%u'", guidlow
);
450 CharacterDatabase
.PExecute("DELETE FROM character_pet_declinedname WHERE id = '%u'", guidlow
);
451 CharacterDatabase
.PExecute("DELETE FROM pet_aura WHERE guid = '%u'", guidlow
);
452 CharacterDatabase
.PExecute("DELETE FROM pet_spell WHERE guid = '%u'", guidlow
);
453 CharacterDatabase
.PExecute("DELETE FROM pet_spell_cooldown WHERE guid = '%u'", guidlow
);
456 void Pet::setDeathState(DeathState s
) // overwrite virtual Creature::setDeathState and Unit::setDeathState
458 Creature::setDeathState(s
);
459 if(getDeathState()==CORPSE
)
461 //remove summoned pet (no corpse)
462 if(getPetType()==SUMMON_PET
)
463 Remove(PET_SAVE_NOT_IN_SLOT
);
464 // other will despawn at corpse desppawning (Pet::Update code)
467 // pet corpse non lootable and non skinnable
468 SetUInt32Value( UNIT_DYNAMIC_FLAGS
, 0x00 );
469 RemoveFlag (UNIT_FIELD_FLAGS
, UNIT_FLAG_SKINNABLE
);
471 //lose happiness when died and not in BG/Arena
472 MapEntry
const* mapEntry
= sMapStore
.LookupEntry(GetMapId());
473 if(!mapEntry
|| (mapEntry
->map_type
!= MAP_ARENA
&& mapEntry
->map_type
!= MAP_BATTLEGROUND
))
474 ModifyPower(POWER_HAPPINESS
, -HAPPINESS_LEVEL_SIZE
);
476 SetFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_STUNNED
);
479 else if(getDeathState()==ALIVE
)
481 RemoveFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_STUNNED
);
486 void Pet::Update(uint32 diff
)
488 if(m_removed
) // pet already removed, just wait in remove queue, no updates
491 switch( m_deathState
)
495 if( m_deathTimer
<= diff
)
497 assert(getPetType()!=SUMMON_PET
&& "Must be already removed.");
498 Remove(PET_SAVE_NOT_IN_SLOT
); //hunters' pets never get removed because of death, NEVER!
505 // unsummon pet that lost owner
506 Unit
* owner
= GetOwner();
507 if(!owner
|| (!IsWithinDistInMap(owner
, OWNER_MAX_DISTANCE
) && (owner
->GetCharmGUID() && (owner
->GetCharmGUID() != GetGUID()))) || (isControlled() && !owner
->GetPetGUID()))
509 Remove(PET_SAVE_NOT_IN_SLOT
, true);
515 if( owner
->GetPetGUID() != GetGUID() )
517 Remove(getPetType()==HUNTER_PET
?PET_SAVE_AS_DELETED
:PET_SAVE_NOT_IN_SLOT
);
524 if(m_duration
> diff
)
528 Remove(getPetType() != SUMMON_PET
? PET_SAVE_AS_DELETED
:PET_SAVE_NOT_IN_SLOT
);
533 //regenerate focus for hunter pets or energy for deathknight's ghoul
534 if(m_regenTimer
<= diff
)
536 switch (getPowerType())
540 Regenerate(getPowerType());
548 m_regenTimer
-= diff
;
550 if(getPetType() != HUNTER_PET
)
553 if(m_happinessTimer
<= diff
)
556 m_happinessTimer
= 7500;
559 m_happinessTimer
-= diff
;
566 Creature::Update(diff
);
569 void Pet::Regenerate(Powers power
)
571 uint32 curValue
= GetPower(power
);
572 uint32 maxValue
= GetMaxPower(power
);
574 if (curValue
>= maxValue
)
577 float addvalue
= 0.0f
;
584 addvalue
= 24 * sWorld
.getRate(RATE_POWER_FOCUS
);
589 // For deathknight's ghoul.
597 // Apply modifiers (if any).
598 AuraList
const& ModPowerRegenPCTAuras
= GetAurasByType(SPELL_AURA_MOD_POWER_REGEN_PERCENT
);
599 for(AuraList::const_iterator i
= ModPowerRegenPCTAuras
.begin(); i
!= ModPowerRegenPCTAuras
.end(); ++i
)
600 if ((*i
)->GetModifier()->m_miscvalue
== power
)
601 addvalue
*= ((*i
)->GetModifier()->m_amount
+ 100) / 100.0f
;
603 ModifyPower(power
, (int32
)addvalue
);
606 void Pet::LooseHappiness()
608 uint32 curValue
= GetPower(POWER_HAPPINESS
);
611 int32 addvalue
= 670; //value is 70/35/17/8/4 (per min) * 1000 / 8 (timer 7.5 secs)
612 if(isInCombat()) //we know in combat happiness fades faster, multiplier guess
613 addvalue
= int32(addvalue
* 1.5);
614 ModifyPower(POWER_HAPPINESS
, -addvalue
);
617 HappinessState
Pet::GetHappinessState()
619 if(GetPower(POWER_HAPPINESS
) < HAPPINESS_LEVEL_SIZE
)
621 else if(GetPower(POWER_HAPPINESS
) >= HAPPINESS_LEVEL_SIZE
* 2)
627 bool Pet::CanTakeMoreActiveSpells(uint32 spellid
)
629 uint8 activecount
= 1;
630 uint32 chainstartstore
[ACTIVE_SPELLS_MAX
];
632 if(IsPassiveSpell(spellid
))
635 chainstartstore
[0] = spellmgr
.GetFirstSpellInChain(spellid
);
637 for (PetSpellMap::const_iterator itr
= m_spells
.begin(); itr
!= m_spells
.end(); ++itr
)
639 if(itr
->second
.state
== PETSPELL_REMOVED
)
642 if(IsPassiveSpell(itr
->first
))
645 uint32 chainstart
= spellmgr
.GetFirstSpellInChain(itr
->first
);
649 for(x
= 0; x
< activecount
; x
++)
651 if(chainstart
== chainstartstore
[x
])
655 if(x
== activecount
) //spellchain not yet saved -> add active count
658 if(activecount
> ACTIVE_SPELLS_MAX
)
660 chainstartstore
[x
] = chainstart
;
666 void Pet::Remove(PetSaveMode mode
, bool returnreagent
)
668 Unit
* owner
= GetOwner();
672 if(owner
->GetTypeId()==TYPEID_PLAYER
)
674 ((Player
*)owner
)->RemovePet(this,mode
,returnreagent
);
678 // only if current pet in slot
679 if(owner
->GetPetGUID()==GetGUID())
683 AddObjectToRemoveList();
687 void Pet::GivePetXP(uint32 xp
)
689 if(getPetType() != HUNTER_PET
)
698 uint32 level
= getLevel();
700 // XP to money conversion processed in Player::RewardQuest
701 if(level
>= sWorld
.getConfig(CONFIG_MAX_PLAYER_LEVEL
))
704 uint32 curXP
= GetUInt32Value(UNIT_FIELD_PETEXPERIENCE
);
705 uint32 nextLvlXP
= GetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP
);
706 uint32 newXP
= curXP
+ xp
;
708 if(newXP
>= nextLvlXP
&& level
+1 > GetOwner()->getLevel())
710 SetUInt32Value(UNIT_FIELD_PETEXPERIENCE
, nextLvlXP
-1);
714 while( newXP
>= nextLvlXP
&& level
< sWorld
.getConfig(CONFIG_MAX_PLAYER_LEVEL
) )
718 GivePetLevel(level
+1);
719 SetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP
, objmgr
.GetXPForLevel(level
+1)/4);
722 nextLvlXP
= GetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP
);
725 SetUInt32Value(UNIT_FIELD_PETEXPERIENCE
, newXP
);
728 void Pet::GivePetLevel(uint32 level
)
733 InitStatsForLevel(level
);
734 InitLevelupSpellsForLevel();
735 InitTalentForLevel();
738 bool Pet::CreateBaseAtCreature(Creature
* creature
)
742 sLog
.outError("CRITICAL: NULL pointer parsed into CreateBaseAtCreature()");
745 uint32 guid
=objmgr
.GenerateLowGuid(HIGHGUID_PET
);
747 sLog
.outBasic("Create pet");
748 uint32 pet_number
= objmgr
.GeneratePetNumber();
749 if(!Create(guid
, creature
->GetMap(), creature
->GetPhaseMask(), creature
->GetEntry(), pet_number
))
752 Relocate(creature
->GetPositionX(), creature
->GetPositionY(), creature
->GetPositionZ(), creature
->GetOrientation());
754 if(!IsPositionValid())
756 sLog
.outError("Pet (guidlow %d, entry %d) not created base at creature. Suggested coordinates isn't valid (X: %f Y: %f)",
757 GetGUIDLow(), GetEntry(), GetPositionX(), GetPositionY());
761 CreatureInfo
const *cinfo
= GetCreatureInfo();
764 sLog
.outError("CreateBaseAtCreature() failed, creatureInfo is missing!");
768 if(cinfo
->type
== CREATURE_TYPE_CRITTER
)
770 setPetType(MINI_PET
);
773 SetDisplayId(creature
->GetDisplayId());
774 SetNativeDisplayId(creature
->GetNativeDisplayId());
775 SetMaxPower(POWER_HAPPINESS
, GetCreatePowers(POWER_HAPPINESS
));
776 SetPower(POWER_HAPPINESS
, 166500);
777 setPowerType(POWER_FOCUS
);
778 SetUInt32Value(UNIT_FIELD_PET_NAME_TIMESTAMP
, 0);
779 SetUInt32Value(UNIT_FIELD_PETEXPERIENCE
, 0);
780 SetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP
, objmgr
.GetXPForLevel(creature
->getLevel())/4);
781 SetUInt32Value(UNIT_NPC_FLAGS
, UNIT_NPC_FLAG_NONE
);
783 if(CreatureFamilyEntry
const* cFamily
= sCreatureFamilyStore
.LookupEntry(cinfo
->family
))
784 SetName(cFamily
->Name
[sWorld
.GetDefaultDbcLocale()]);
786 SetName(creature
->GetNameForLocaleIdx(objmgr
.GetDBCLocaleIndex()));
788 if(cinfo
->type
== CREATURE_TYPE_BEAST
)
790 SetUInt32Value(UNIT_FIELD_BYTES_0
, 0x02020100);
791 SetSheath(SHEATH_STATE_MELEE
);
792 SetByteValue(UNIT_FIELD_BYTES_2
, 2, UNIT_RENAME_ALLOWED
);
793 SetUInt32Value(UNIT_MOD_CAST_SPEED
, creature
->GetUInt32Value(UNIT_MOD_CAST_SPEED
));
798 bool Pet::InitStatsForLevel(uint32 petlevel
, Unit
* owner
)
800 CreatureInfo
const *cinfo
= GetCreatureInfo();
808 sLog
.outError("attempt to summon pet (Entry %u) without owner! Attempt terminated.", cinfo
->Entry
);
813 uint32 creature_ID
= (getPetType() == HUNTER_PET
) ? 1 : cinfo
->Entry
;
817 SetMeleeDamageSchool(SpellSchools(cinfo
->dmgschool
));
819 SetModifierValue(UNIT_MOD_ARMOR
, BASE_VALUE
, float(petlevel
*50));
821 SetAttackTime(BASE_ATTACK
, BASE_ATTACK_TIME
);
822 SetAttackTime(OFF_ATTACK
, BASE_ATTACK_TIME
);
823 SetAttackTime(RANGED_ATTACK
, BASE_ATTACK_TIME
);
825 SetFloatValue(UNIT_MOD_CAST_SPEED
, 1.0);
827 CreatureFamilyEntry
const* cFamily
= sCreatureFamilyStore
.LookupEntry(cinfo
->family
);
828 if(cFamily
&& cFamily
->minScale
> 0.0f
&& getPetType()==HUNTER_PET
)
831 if (getLevel() >= cFamily
->maxScaleLevel
)
832 scale
= cFamily
->maxScale
;
833 else if (getLevel() <= cFamily
->minScaleLevel
)
834 scale
= cFamily
->minScale
;
836 scale
= cFamily
->minScale
+ float(getLevel() - cFamily
->minScaleLevel
) / cFamily
->maxScaleLevel
* (cFamily
->maxScale
- cFamily
->minScale
);
838 SetFloatValue(OBJECT_FIELD_SCALE_X
, scale
);
842 int32 createResistance
[MAX_SPELL_SCHOOL
] = {0,0,0,0,0,0,0};
844 if(cinfo
&& getPetType() != HUNTER_PET
)
846 createResistance
[SPELL_SCHOOL_HOLY
] = cinfo
->resistance1
;
847 createResistance
[SPELL_SCHOOL_FIRE
] = cinfo
->resistance2
;
848 createResistance
[SPELL_SCHOOL_NATURE
] = cinfo
->resistance3
;
849 createResistance
[SPELL_SCHOOL_FROST
] = cinfo
->resistance4
;
850 createResistance
[SPELL_SCHOOL_SHADOW
] = cinfo
->resistance5
;
851 createResistance
[SPELL_SCHOOL_ARCANE
] = cinfo
->resistance6
;
858 if(owner
->GetTypeId() == TYPEID_PLAYER
)
860 switch(owner
->getClass())
865 //the damage bonus used for pets is either fire or shadow damage, whatever is higher
866 uint32 fire
= owner
->GetUInt32Value(PLAYER_FIELD_MOD_DAMAGE_DONE_POS
+ SPELL_SCHOOL_FIRE
);
867 uint32 shadow
= owner
->GetUInt32Value(PLAYER_FIELD_MOD_DAMAGE_DONE_POS
+ SPELL_SCHOOL_SHADOW
);
868 uint32 val
= (fire
> shadow
) ? fire
: shadow
;
870 SetBonusDamage(int32 (val
* 0.15f
));
871 //bonusAP += val * 0.57;
876 //40% damage bonus of mage's frost damage
877 float val
= owner
->GetUInt32Value(PLAYER_FIELD_MOD_DAMAGE_DONE_POS
+ SPELL_SCHOOL_FROST
) * 0.4;
880 SetBonusDamage( int32(val
));
888 SetBaseWeaponDamage(BASE_ATTACK
, MINDAMAGE
, float(petlevel
- (petlevel
/ 4)) );
889 SetBaseWeaponDamage(BASE_ATTACK
, MAXDAMAGE
, float(petlevel
+ (petlevel
/ 4)) );
891 //SetModifierValue(UNIT_MOD_ATTACK_POWER, BASE_VALUE, float(cinfo->attackpower));
893 PetLevelInfo
const* pInfo
= objmgr
.GetPetLevelInfo(creature_ID
, petlevel
);
894 if(pInfo
) // exist in DB
896 SetCreateHealth(pInfo
->health
);
897 SetCreateMana(pInfo
->mana
);
900 SetModifierValue(UNIT_MOD_ARMOR
, BASE_VALUE
, float(pInfo
->armor
));
902 for(int stat
= 0; stat
< MAX_STATS
; ++stat
)
904 SetCreateStat(Stats(stat
), float(pInfo
->stats
[stat
]));
907 else // not exist in DB, use some default fake data
909 sLog
.outErrorDb("Summoned pet (Entry: %u) not have pet stats data in DB",cinfo
->Entry
);
911 // remove elite bonuses included in DB values
912 SetCreateHealth(uint32(((float(cinfo
->maxhealth
) / cinfo
->maxlevel
) / (1 + 2 * cinfo
->rank
)) * petlevel
) );
913 SetCreateMana( uint32(((float(cinfo
->maxmana
) / cinfo
->maxlevel
) / (1 + 2 * cinfo
->rank
)) * petlevel
) );
915 SetCreateStat(STAT_STRENGTH
, 22);
916 SetCreateStat(STAT_AGILITY
, 22);
917 SetCreateStat(STAT_STAMINA
, 25);
918 SetCreateStat(STAT_INTELLECT
, 28);
919 SetCreateStat(STAT_SPIRIT
, 27);
925 SetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP
, objmgr
.GetXPForLevel(petlevel
)/4);
926 //these formula may not be correct; however, it is designed to be close to what it should be
927 //this makes dps 0.5 of pets level
928 SetBaseWeaponDamage(BASE_ATTACK
, MINDAMAGE
, float(petlevel
- (petlevel
/ 4)) );
929 //damage range is then petlevel / 2
930 SetBaseWeaponDamage(BASE_ATTACK
, MAXDAMAGE
, float(petlevel
+ (petlevel
/ 4)) );
931 //damage is increased afterwards as strength and pet scaling modify attack power
933 //stored standard pet stats are entry 1 in pet_levelinfo
934 PetLevelInfo
const* pInfo
= objmgr
.GetPetLevelInfo(creature_ID
, petlevel
);
935 if(pInfo
) // exist in DB
937 SetCreateHealth(pInfo
->health
);
938 SetModifierValue(UNIT_MOD_ARMOR
, BASE_VALUE
, float(pInfo
->armor
));
939 //SetModifierValue(UNIT_MOD_ATTACK_POWER, BASE_VALUE, float(cinfo->attackpower));
941 for( int i
= STAT_STRENGTH
; i
< MAX_STATS
; ++i
)
943 SetCreateStat(Stats(i
), float(pInfo
->stats
[i
]));
946 else // not exist in DB, use some default fake data
948 sLog
.outErrorDb("Hunter pet levelstats missing in DB");
950 // remove elite bonuses included in DB values
951 SetCreateHealth( uint32(((float(cinfo
->maxhealth
) / cinfo
->maxlevel
) / (1 + 2 * cinfo
->rank
)) * petlevel
) );
953 SetCreateStat(STAT_STRENGTH
, 22);
954 SetCreateStat(STAT_AGILITY
, 22);
955 SetCreateStat(STAT_STAMINA
, 25);
956 SetCreateStat(STAT_INTELLECT
, 28);
957 SetCreateStat(STAT_SPIRIT
, 27);
962 SetUInt32Value(UNIT_FIELD_PETEXPERIENCE
, 0);
963 SetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP
, 1000);
965 SetCreateMana(28 + 10*petlevel
);
966 SetCreateHealth(28 + 30*petlevel
);
968 // FIXME: this is wrong formula, possible each guardian pet have own damage formula
969 //these formula may not be correct; however, it is designed to be close to what it should be
970 //this makes dps 0.5 of pets level
971 SetBaseWeaponDamage(BASE_ATTACK
, MINDAMAGE
, float(petlevel
- (petlevel
/ 4)));
972 //damage range is then petlevel / 2
973 SetBaseWeaponDamage(BASE_ATTACK
, MAXDAMAGE
, float(petlevel
+ (petlevel
/ 4)));
976 sLog
.outError("Pet have incorrect type (%u) for levelup.", getPetType());
980 for (int i
= SPELL_SCHOOL_HOLY
; i
< MAX_SPELL_SCHOOL
; ++i
)
981 SetModifierValue(UnitMods(UNIT_MOD_RESISTANCE_START
+ i
), BASE_VALUE
, float(createResistance
[i
]));
985 SetHealth(GetMaxHealth());
986 SetPower(POWER_MANA
, GetMaxPower(POWER_MANA
));
991 bool Pet::HaveInDiet(ItemPrototype
const* item
) const
996 CreatureInfo
const* cInfo
= GetCreatureInfo();
1000 CreatureFamilyEntry
const* cFamily
= sCreatureFamilyStore
.LookupEntry(cInfo
->family
);
1004 uint32 diet
= cFamily
->petFoodMask
;
1005 uint32 FoodMask
= 1 << (item
->FoodType
-1);
1006 return diet
& FoodMask
;
1009 uint32
Pet::GetCurrentFoodBenefitLevel(uint32 itemlevel
)
1011 // -5 or greater food level
1012 if(getLevel() <= itemlevel
+ 5) //possible to feed level 60 pet with level 55 level food for full effect
1015 else if(getLevel() <= itemlevel
+ 10) //pure guess, but sounds good
1018 else if(getLevel() <= itemlevel
+ 14) //level 55 food gets green on 70, makes sense to me
1022 return 0; //food too low level
1025 void Pet::_LoadSpellCooldowns()
1027 m_CreatureSpellCooldowns
.clear();
1028 m_CreatureCategoryCooldowns
.clear();
1030 QueryResult
*result
= CharacterDatabase
.PQuery("SELECT spell,time FROM pet_spell_cooldown WHERE guid = '%u'",m_charmInfo
->GetPetNumber());
1034 time_t curTime
= time(NULL
);
1036 WorldPacket
data(SMSG_SPELL_COOLDOWN
, (8+1+result
->GetRowCount()*8));
1038 data
<< uint8(0x0); // flags (0x1, 0x2)
1042 Field
*fields
= result
->Fetch();
1044 uint32 spell_id
= fields
[0].GetUInt32();
1045 time_t db_time
= (time_t)fields
[1].GetUInt64();
1047 if(!sSpellStore
.LookupEntry(spell_id
))
1049 sLog
.outError("Pet %u have unknown spell %u in `pet_spell_cooldown`, skipping.",m_charmInfo
->GetPetNumber(),spell_id
);
1053 // skip outdated cooldown
1054 if(db_time
<= curTime
)
1057 data
<< uint32(spell_id
);
1058 data
<< uint32(uint32(db_time
-curTime
)*IN_MILISECONDS
);
1060 _AddCreatureSpellCooldown(spell_id
,db_time
);
1062 sLog
.outDebug("Pet (Number: %u) spell %u cooldown loaded (%u secs).", m_charmInfo
->GetPetNumber(), spell_id
, uint32(db_time
-curTime
));
1064 while( result
->NextRow() );
1068 if(!m_CreatureSpellCooldowns
.empty() && GetOwner())
1070 ((Player
*)GetOwner())->GetSession()->SendPacket(&data
);
1075 void Pet::_SaveSpellCooldowns()
1077 CharacterDatabase
.PExecute("DELETE FROM pet_spell_cooldown WHERE guid = '%u'", m_charmInfo
->GetPetNumber());
1079 time_t curTime
= time(NULL
);
1081 // remove oudated and save active
1082 for(CreatureSpellCooldowns::iterator itr
= m_CreatureSpellCooldowns
.begin();itr
!= m_CreatureSpellCooldowns
.end();)
1084 if(itr
->second
<= curTime
)
1085 m_CreatureSpellCooldowns
.erase(itr
++);
1088 CharacterDatabase
.PExecute("INSERT INTO pet_spell_cooldown (guid,spell,time) VALUES ('%u', '%u', '" UI64FMTD
"')", m_charmInfo
->GetPetNumber(), itr
->first
, uint64(itr
->second
));
1094 void Pet::_LoadSpells()
1096 QueryResult
*result
= CharacterDatabase
.PQuery("SELECT spell,active FROM pet_spell WHERE guid = '%u'",m_charmInfo
->GetPetNumber());
1102 Field
*fields
= result
->Fetch();
1104 addSpell(fields
[0].GetUInt32(), ActiveStates(fields
[1].GetUInt8()), PETSPELL_UNCHANGED
);
1106 while( result
->NextRow() );
1112 void Pet::_SaveSpells()
1114 for (PetSpellMap::iterator itr
= m_spells
.begin(), next
= m_spells
.begin(); itr
!= m_spells
.end(); itr
= next
)
1118 // prevent saving family passives to DB
1119 if (itr
->second
.type
== PETSPELL_FAMILY
)
1122 switch(itr
->second
.state
)
1124 case PETSPELL_REMOVED
:
1125 CharacterDatabase
.PExecute("DELETE FROM pet_spell WHERE guid = '%u' and spell = '%u'", m_charmInfo
->GetPetNumber(), itr
->first
);
1126 m_spells
.erase(itr
);
1128 case PETSPELL_CHANGED
:
1129 CharacterDatabase
.PExecute("DELETE FROM pet_spell WHERE guid = '%u' and spell = '%u'", m_charmInfo
->GetPetNumber(), itr
->first
);
1130 CharacterDatabase
.PExecute("INSERT INTO pet_spell (guid,spell,active) VALUES ('%u', '%u', '%u')", m_charmInfo
->GetPetNumber(), itr
->first
, itr
->second
.active
);
1133 CharacterDatabase
.PExecute("INSERT INTO pet_spell (guid,spell,active) VALUES ('%u', '%u', '%u')", m_charmInfo
->GetPetNumber(), itr
->first
, itr
->second
.active
);
1135 case PETSPELL_UNCHANGED
:
1139 itr
->second
.state
= PETSPELL_UNCHANGED
;
1143 void Pet::_LoadAuras(uint32 timediff
)
1146 for (int i
= 0; i
< TOTAL_AURAS
; ++i
)
1147 m_modAuras
[i
].clear();
1149 QueryResult
*result
= CharacterDatabase
.PQuery("SELECT caster_guid,spell,effect_index,stackcount,amount,maxduration,remaintime,remaincharges FROM pet_aura WHERE guid = '%u'",m_charmInfo
->GetPetNumber());
1155 Field
*fields
= result
->Fetch();
1156 uint64 caster_guid
= fields
[0].GetUInt64();
1157 uint32 spellid
= fields
[1].GetUInt32();
1158 uint32 effindex
= fields
[2].GetUInt32();
1159 uint32 stackcount
= fields
[3].GetUInt32();
1160 int32 damage
= (int32
)fields
[4].GetUInt32();
1161 int32 maxduration
= (int32
)fields
[5].GetUInt32();
1162 int32 remaintime
= (int32
)fields
[6].GetUInt32();
1163 int32 remaincharges
= (int32
)fields
[7].GetUInt32();
1165 SpellEntry
const* spellproto
= sSpellStore
.LookupEntry(spellid
);
1168 sLog
.outError("Unknown aura (spellid %u, effindex %u), ignore.",spellid
,effindex
);
1174 sLog
.outError("Invalid effect index (spellid %u, effindex %u), ignore.",spellid
,effindex
);
1178 // negative effects should continue counting down after logout
1179 if (remaintime
!= -1 && !IsPositiveEffect(spellid
, effindex
))
1181 if(remaintime
<= int32(timediff
))
1184 remaintime
-= timediff
;
1187 // prevent wrong values of remaincharges
1188 if(spellproto
->procCharges
)
1190 if(remaincharges
<= 0 || remaincharges
> spellproto
->procCharges
)
1191 remaincharges
= spellproto
->procCharges
;
1196 /// do not load single target auras (unless they were cast by the player)
1197 if (caster_guid
!= GetGUID() && IsSingleTargetSpell(spellproto
))
1200 for(uint32 i
=0; i
<stackcount
; ++i
)
1202 Aura
* aura
= CreateAura(spellproto
, effindex
, NULL
, this, NULL
);
1205 damage
= aura
->GetModifier()->m_amount
;
1206 aura
->SetLoadedState(caster_guid
,damage
,maxduration
,remaintime
,remaincharges
);
1210 while( result
->NextRow() );
1216 void Pet::_SaveAuras()
1218 CharacterDatabase
.PExecute("DELETE FROM pet_aura WHERE guid = '%u'", m_charmInfo
->GetPetNumber());
1220 AuraMap
const& auras
= GetAuras();
1224 spellEffectPair lastEffectPair
= auras
.begin()->first
;
1225 uint32 stackCounter
= 1;
1227 for(AuraMap::const_iterator itr
= auras
.begin(); ; ++itr
)
1229 if(itr
== auras
.end() || lastEffectPair
!= itr
->first
)
1231 AuraMap::const_iterator itr2
= itr
;
1232 // save previous spellEffectPair to db
1234 SpellEntry
const *spellInfo
= itr2
->second
->GetSpellProto();
1235 /// do not save single target auras (unless they were cast by the player)
1236 if (!(itr2
->second
->GetCasterGUID() != GetGUID() && IsSingleTargetSpell(spellInfo
)))
1238 if(!itr2
->second
->IsPassive())
1240 // skip all auras from spell that apply at cast SPELL_AURA_MOD_SHAPESHIFT or pet area auras.
1242 for (i
= 0; i
< 3; ++i
)
1243 if (spellInfo
->EffectApplyAuraName
[i
] == SPELL_AURA_MOD_STEALTH
||
1244 spellInfo
->Effect
[i
] == SPELL_EFFECT_APPLY_AREA_AURA_OWNER
||
1245 spellInfo
->Effect
[i
] == SPELL_EFFECT_APPLY_AREA_AURA_PET
)
1250 CharacterDatabase
.PExecute("INSERT INTO pet_aura (guid,caster_guid,spell,effect_index,stackcount,amount,maxduration,remaintime,remaincharges) "
1251 "VALUES ('%u', '" UI64FMTD
"', '%u', '%u', '%u', '%d', '%d', '%d', '%d')",
1252 m_charmInfo
->GetPetNumber(), itr2
->second
->GetCasterGUID(),(uint32
)itr2
->second
->GetId(), (uint32
)itr2
->second
->GetEffIndex(), stackCounter
, itr2
->second
->GetModifier()->m_amount
,int(itr2
->second
->GetAuraMaxDuration()),int(itr2
->second
->GetAuraDuration()),int(itr2
->second
->GetAuraCharges()));
1256 if(itr
== auras
.end())
1260 if (lastEffectPair
== itr
->first
)
1264 lastEffectPair
= itr
->first
;
1270 bool Pet::addSpell(uint32 spell_id
,ActiveStates active
/*= ACT_DECIDE*/, PetSpellState state
/*= PETSPELL_NEW*/, PetSpellType type
/*= PETSPELL_NORMAL*/)
1272 SpellEntry
const *spellInfo
= sSpellStore
.LookupEntry(spell_id
);
1275 // do pet spell book cleanup
1276 if(state
== PETSPELL_UNCHANGED
) // spell load case
1278 sLog
.outError("Pet::addSpell: Non-existed in SpellStore spell #%u request, deleting for all pets in `pet_spell`.",spell_id
);
1279 CharacterDatabase
.PExecute("DELETE FROM pet_spell WHERE spell = '%u'",spell_id
);
1282 sLog
.outError("Pet::addSpell: Non-existed in SpellStore spell #%u request.",spell_id
);
1287 PetSpellMap::iterator itr
= m_spells
.find(spell_id
);
1288 if (itr
!= m_spells
.end())
1290 if (itr
->second
.state
== PETSPELL_REMOVED
)
1292 m_spells
.erase(itr
);
1293 state
= PETSPELL_CHANGED
;
1295 else if (state
== PETSPELL_UNCHANGED
&& itr
->second
.state
!= PETSPELL_UNCHANGED
)
1297 // can be in case spell loading but learned at some previous spell loading
1298 itr
->second
.state
= PETSPELL_UNCHANGED
;
1300 if(active
== ACT_ENABLED
)
1301 ToggleAutocast(spell_id
, true);
1302 else if(active
== ACT_DISABLED
)
1303 ToggleAutocast(spell_id
, false);
1311 uint32 oldspell_id
= 0;
1314 newspell
.state
= state
;
1315 newspell
.type
= type
;
1317 if(active
== ACT_DECIDE
) //active was not used before, so we save it's autocast/passive state here
1319 if(IsPassiveSpell(spell_id
))
1320 newspell
.active
= ACT_PASSIVE
;
1322 newspell
.active
= ACT_DISABLED
;
1325 newspell
.active
= active
;
1327 // talent: unlearn all other talent ranks (high and low)
1328 if(TalentSpellPos
const* talentPos
= GetTalentSpellPos(spell_id
))
1330 if(TalentEntry
const *talentInfo
= sTalentStore
.LookupEntry( talentPos
->talent_id
))
1332 for(int i
=0; i
< MAX_TALENT_RANK
; ++i
)
1334 // skip learning spell and no rank spell case
1335 uint32 rankSpellId
= talentInfo
->RankID
[i
];
1336 if(!rankSpellId
|| rankSpellId
==spell_id
)
1339 // skip unknown ranks
1340 if(!HasSpell(rankSpellId
))
1342 removeSpell(rankSpellId
,false,false);
1346 else if(spellmgr
.GetSpellRank(spell_id
)!=0)
1348 for (PetSpellMap::const_iterator itr2
= m_spells
.begin(); itr2
!= m_spells
.end(); ++itr2
)
1350 if(itr2
->second
.state
== PETSPELL_REMOVED
) continue;
1352 if( spellmgr
.IsRankSpellDueToSpell(spellInfo
,itr2
->first
) )
1354 // replace by new high rank
1355 if(spellmgr
.IsHighRankOfSpell(spell_id
,itr2
->first
))
1357 newspell
.active
= itr2
->second
.active
;
1359 if(newspell
.active
== ACT_ENABLED
)
1360 ToggleAutocast(itr2
->first
, false);
1362 oldspell_id
= itr2
->first
;
1363 unlearnSpell(itr2
->first
,false,false);
1366 // ignore new lesser rank
1367 else if(spellmgr
.IsHighRankOfSpell(itr2
->first
,spell_id
))
1373 m_spells
[spell_id
] = newspell
;
1375 if (IsPassiveSpell(spell_id
))
1376 CastSpell(this, spell_id
, true);
1378 m_charmInfo
->AddSpellToActionBar(spell_id
);
1380 if(newspell
.active
== ACT_ENABLED
)
1381 ToggleAutocast(spell_id
, true);
1383 uint32 talentCost
= GetTalentSpellCost(spell_id
);
1386 int32 free_points
= GetMaxTalentPointsForLevel(getLevel());
1387 m_usedTalentCount
+=talentCost
;
1388 // update free talent points
1389 free_points
-=m_usedTalentCount
;
1390 SetFreeTalentPoints(free_points
> 0 ? free_points
: 0);
1395 bool Pet::learnSpell(uint32 spell_id
)
1397 // prevent duplicated entires in spell book
1398 if (!addSpell(spell_id
))
1403 Unit
* owner
= GetOwner();
1404 if(owner
&& owner
->GetTypeId() == TYPEID_PLAYER
)
1406 WorldPacket
data(SMSG_PET_LEARNED_SPELL
, 4);
1407 data
<< uint32(spell_id
);
1408 ((Player
*)owner
)->GetSession()->SendPacket(&data
);
1410 ((Player
*)owner
)->PetSpellInitialize();
1416 void Pet::InitLevelupSpellsForLevel()
1418 uint32 level
= getLevel();
1420 if(PetLevelupSpellSet
const *levelupSpells
= GetCreatureInfo()->family
? spellmgr
.GetPetLevelupSpellList(GetCreatureInfo()->family
) : NULL
)
1422 // PetLevelupSpellSet ordered by levels, process in reversed order
1423 for(PetLevelupSpellSet::const_reverse_iterator itr
= levelupSpells
->rbegin(); itr
!= levelupSpells
->rend(); ++itr
)
1425 // will called first if level down
1426 if(itr
->first
> level
)
1427 unlearnSpell(itr
->second
,true); // will learn prev rank if any
1428 // will called if level up
1430 learnSpell(itr
->second
); // will unlearn prev rank if any
1434 int32 petSpellsId
= GetCreatureInfo()->PetSpellDataId
? -(int32
)GetCreatureInfo()->PetSpellDataId
: GetEntry();
1436 // default spells (can be not learned if pet level (as owner level decrease result for example) less first possible in normal game)
1437 if(PetDefaultSpellsEntry
const *defSpells
= spellmgr
.GetPetDefaultSpellsEntry(petSpellsId
))
1439 for(int i
= 0; i
< MAX_CREATURE_SPELL_DATA_SLOT
; ++i
)
1441 SpellEntry
const* spellEntry
= sSpellStore
.LookupEntry(defSpells
->spellid
[i
]);
1445 // will called first if level down
1446 if(spellEntry
->spellLevel
> level
)
1447 unlearnSpell(spellEntry
->Id
,true);
1448 // will called if level up
1450 learnSpell(spellEntry
->Id
);
1455 bool Pet::unlearnSpell(uint32 spell_id
, bool learn_prev
, bool clear_ab
)
1457 if(removeSpell(spell_id
,learn_prev
,clear_ab
))
1461 if (Unit
* owner
= GetOwner())
1463 if(owner
->GetTypeId() == TYPEID_PLAYER
)
1465 WorldPacket
data(SMSG_PET_REMOVED_SPELL
, 4);
1466 data
<< uint32(spell_id
);
1467 ((Player
*)owner
)->GetSession()->SendPacket(&data
);
1476 bool Pet::removeSpell(uint32 spell_id
, bool learn_prev
, bool clear_ab
)
1478 PetSpellMap::iterator itr
= m_spells
.find(spell_id
);
1479 if (itr
== m_spells
.end())
1482 if(itr
->second
.state
== PETSPELL_REMOVED
)
1485 if(itr
->second
.state
== PETSPELL_NEW
)
1486 m_spells
.erase(itr
);
1488 itr
->second
.state
= PETSPELL_REMOVED
;
1490 RemoveAurasDueToSpell(spell_id
);
1492 uint32 talentCost
= GetTalentSpellCost(spell_id
);
1495 if (m_usedTalentCount
> talentCost
)
1496 m_usedTalentCount
-=talentCost
;
1498 m_usedTalentCount
= 0;
1499 // update free talent points
1500 int32 free_points
= GetMaxTalentPointsForLevel(getLevel()) - m_usedTalentCount
;
1501 SetFreeTalentPoints(free_points
> 0 ? free_points
: 0);
1506 if (uint32 prev_id
= spellmgr
.GetPrevSpellInChain (spell_id
))
1507 learnSpell(prev_id
);
1512 // if remove last rank or non-ranked then update action bar at server and client if need
1513 if (clear_ab
&& !learn_prev
&& m_charmInfo
->RemoveSpellFromActionBar(spell_id
))
1517 // need update action bar for last removed rank
1518 if (Unit
* owner
= GetOwner())
1519 if (owner
->GetTypeId() == TYPEID_PLAYER
)
1520 ((Player
*)owner
)->PetSpellInitialize();
1528 void Pet::CleanupActionBar()
1530 for(int i
= 0; i
< MAX_UNIT_ACTION_BAR_INDEX
; ++i
)
1531 if(UnitActionBarEntry
const* ab
= m_charmInfo
->GetActionBarEntry(i
))
1532 if(uint32 action
= ab
->GetAction())
1533 if(ab
->IsActionBarForSpell() && !HasSpell(action
))
1534 m_charmInfo
->SetActionBar(i
,0,ACT_DISABLED
);
1537 void Pet::InitPetCreateSpells()
1539 m_charmInfo
->InitPetActionBar();
1544 CastPetAuras(false);
1547 bool Pet::resetTalents(bool no_cost
)
1549 Unit
*owner
= GetOwner();
1550 if (!owner
|| owner
->GetTypeId()!=TYPEID_PLAYER
)
1553 // not need after this call
1554 if(((Player
*)owner
)->HasAtLoginFlag(AT_LOGIN_RESET_PET_TALENTS
))
1555 ((Player
*)owner
)->RemoveAtLoginFlag(AT_LOGIN_RESET_PET_TALENTS
,true);
1557 CreatureInfo
const * ci
= GetCreatureInfo();
1560 // Check pet talent type
1561 CreatureFamilyEntry
const *pet_family
= sCreatureFamilyStore
.LookupEntry(ci
->family
);
1562 if(!pet_family
|| pet_family
->petTalentType
< 0)
1565 Player
*player
= (Player
*)owner
;
1567 uint32 level
= getLevel();
1568 uint32 talentPointsForLevel
= GetMaxTalentPointsForLevel(level
);
1570 if (m_usedTalentCount
== 0)
1572 SetFreeTalentPoints(talentPointsForLevel
);
1580 cost
= resetTalentsCost();
1582 if (player
->GetMoney() < cost
)
1584 player
->SendBuyError( BUY_ERR_NOT_ENOUGHT_MONEY
, 0, 0, 0);
1589 for (unsigned int i
= 0; i
< sTalentStore
.GetNumRows(); ++i
)
1591 TalentEntry
const *talentInfo
= sTalentStore
.LookupEntry(i
);
1593 if (!talentInfo
) continue;
1595 TalentTabEntry
const *talentTabInfo
= sTalentTabStore
.LookupEntry( talentInfo
->TalentTab
);
1600 // unlearn only talents for pets family talent type
1601 if(!((1 << pet_family
->petTalentType
) & talentTabInfo
->petTalentMask
))
1604 for (int j
= 0; j
< MAX_TALENT_RANK
; j
++)
1606 for(PetSpellMap::const_iterator itr
= m_spells
.begin(); itr
!= m_spells
.end();)
1608 if(itr
->second
.state
== PETSPELL_REMOVED
)
1613 // remove learned spells (all ranks)
1614 uint32 itrFirstId
= spellmgr
.GetFirstSpellInChain(itr
->first
);
1616 // unlearn if first rank is talent or learned by talent
1617 if (itrFirstId
== talentInfo
->RankID
[j
] || spellmgr
.IsSpellLearnToSpell(talentInfo
->RankID
[j
],itrFirstId
))
1619 removeSpell(itr
->first
,false);
1620 itr
= m_spells
.begin();
1629 SetFreeTalentPoints(talentPointsForLevel
);
1633 player
->ModifyMoney(-(int32
)cost
);
1635 m_resetTalentsCost
= cost
;
1636 m_resetTalentsTime
= time(NULL
);
1638 player
->PetSpellInitialize();
1642 void Pet::resetTalentsForAllPetsOf(Player
* owner
, Pet
* online_pet
/*= NULL*/)
1644 // not need after this call
1645 if(((Player
*)owner
)->HasAtLoginFlag(AT_LOGIN_RESET_PET_TALENTS
))
1646 ((Player
*)owner
)->RemoveAtLoginFlag(AT_LOGIN_RESET_PET_TALENTS
,true);
1650 online_pet
->resetTalents(true);
1652 // now need only reset for offline pets (all pets except online case)
1653 uint32 except_petnumber
= online_pet
? online_pet
->GetCharmInfo()->GetPetNumber() : 0;
1655 QueryResult
*resultPets
= CharacterDatabase
.PQuery(
1656 "SELECT id FROM character_pet WHERE owner = '%u' AND id <> '%u'",
1657 owner
->GetGUIDLow(),except_petnumber
);
1663 QueryResult
*result
= CharacterDatabase
.PQuery(
1664 "SELECT DISTINCT pet_spell.spell FROM pet_spell, character_pet "
1665 "WHERE character_pet.owner = '%u' AND character_pet.id = pet_spell.guid AND character_pet.id <> %u",
1666 owner
->GetGUIDLow(),except_petnumber
);
1674 bool need_comma
= false;
1675 std::ostringstream ss
;
1676 ss
<< "DELETE FROM pet_spell WHERE guid IN (";
1680 Field
*fields
= resultPets
->Fetch();
1682 uint32 id
= fields
[0].GetUInt32();
1691 while( resultPets
->NextRow() );
1695 ss
<< ") AND spell IN (";
1697 bool need_execute
= false;
1700 Field
*fields
= result
->Fetch();
1702 uint32 spell
= fields
[0].GetUInt32();
1704 if(!GetTalentSpellCost(spell
))
1712 need_execute
= true;
1714 while( result
->NextRow() );
1723 CharacterDatabase
.Execute(ss
.str().c_str());
1726 void Pet::InitTalentForLevel()
1728 uint32 level
= getLevel();
1729 uint32 talentPointsForLevel
= GetMaxTalentPointsForLevel(level
);
1730 // Reset talents in case low level (on level down) or wrong points for level (hunter can unlearn TP increase talent)
1731 if(talentPointsForLevel
== 0 || m_usedTalentCount
> talentPointsForLevel
)
1733 // Remove all talent points
1736 SetFreeTalentPoints(talentPointsForLevel
- m_usedTalentCount
);
1738 Unit
*owner
= GetOwner();
1739 if (!owner
|| owner
->GetTypeId() != TYPEID_PLAYER
)
1743 ((Player
*)owner
)->SendTalentsInfoData(true);
1746 uint32
Pet::resetTalentsCost() const
1748 uint32 days
= (sWorld
.GetGameTime() - m_resetTalentsTime
)/DAY
;
1750 // The first time reset costs 10 silver; after 1 day cost is reset to 10 silver
1751 if(m_resetTalentsCost
< 10*SILVER
|| days
> 0)
1754 else if(m_resetTalentsCost
< 50*SILVER
)
1757 else if(m_resetTalentsCost
< 1*GOLD
)
1759 // then increasing at a rate of 1 gold; cap 10 gold
1761 return (m_resetTalentsCost
+ 1*GOLD
> 10*GOLD
? 10*GOLD
: m_resetTalentsCost
+ 1*GOLD
);
1764 uint8
Pet::GetMaxTalentPointsForLevel(uint32 level
)
1766 uint8 points
= (level
>= 20) ? ((level
- 16) / 4) : 0;
1767 // Mod points from owner SPELL_AURA_MOD_PET_TALENT_POINTS
1768 if (Unit
*owner
= GetOwner())
1769 points
+=owner
->GetTotalAuraModifier(SPELL_AURA_MOD_PET_TALENT_POINTS
);
1773 void Pet::ToggleAutocast(uint32 spellid
, bool apply
)
1775 if(IsPassiveSpell(spellid
))
1778 PetSpellMap::iterator itr
= m_spells
.find(spellid
);
1784 for (i
= 0; i
< m_autospells
.size() && m_autospells
[i
] != spellid
; ++i
)
1787 if (i
== m_autospells
.size())
1789 m_autospells
.push_back(spellid
);
1791 if(itr
->second
.active
!= ACT_ENABLED
)
1793 itr
->second
.active
= ACT_ENABLED
;
1794 if(itr
->second
.state
!= PETSPELL_NEW
)
1795 itr
->second
.state
= PETSPELL_CHANGED
;
1801 AutoSpellList::iterator itr2
= m_autospells
.begin();
1802 for (i
= 0; i
< m_autospells
.size() && m_autospells
[i
] != spellid
; ++i
, itr2
++)
1805 if (i
< m_autospells
.size())
1807 m_autospells
.erase(itr2
);
1808 if(itr
->second
.active
!= ACT_DISABLED
)
1810 itr
->second
.active
= ACT_DISABLED
;
1811 if(itr
->second
.state
!= PETSPELL_NEW
)
1812 itr
->second
.state
= PETSPELL_CHANGED
;
1818 bool Pet::IsPermanentPetFor(Player
* owner
)
1820 switch(getPetType())
1823 switch(owner
->getClass())
1826 return GetCreatureInfo()->type
== CREATURE_TYPE_DEMON
;
1827 case CLASS_DEATH_KNIGHT
:
1828 return GetCreatureInfo()->type
== CREATURE_TYPE_UNDEAD
;
1839 bool Pet::Create(uint32 guidlow
, Map
*map
, uint32 phaseMask
, uint32 Entry
, uint32 pet_number
)
1842 SetPhaseMask(phaseMask
,false);
1844 Object::_Create(guidlow
, pet_number
, HIGHGUID_PET
);
1846 m_DBTableGuid
= guidlow
;
1847 m_originalEntry
= Entry
;
1849 if(!InitEntry(Entry
))
1852 SetSheath(SHEATH_STATE_MELEE
);
1854 if(getPetType() == MINI_PET
) // always non-attackable
1855 SetFlag(UNIT_FIELD_FLAGS
, UNIT_FLAG_NON_ATTACKABLE
);
1860 bool Pet::HasSpell(uint32 spell
) const
1862 PetSpellMap::const_iterator itr
= m_spells
.find(spell
);
1863 return (itr
!= m_spells
.end() && itr
->second
.state
!= PETSPELL_REMOVED
);
1866 // Get all passive spells in our skill line
1867 void Pet::LearnPetPassives()
1869 CreatureInfo
const* cInfo
= GetCreatureInfo();
1873 CreatureFamilyEntry
const* cFamily
= sCreatureFamilyStore
.LookupEntry(cInfo
->family
);
1877 PetFamilySpellsStore::const_iterator petStore
= sPetFamilySpellsStore
.find(cFamily
->ID
);
1878 if(petStore
!= sPetFamilySpellsStore
.end())
1880 for(PetFamilySpellsSet::const_iterator petSet
= petStore
->second
.begin(); petSet
!= petStore
->second
.end(); ++petSet
)
1881 addSpell(*petSet
, ACT_DECIDE
, PETSPELL_NEW
, PETSPELL_FAMILY
);
1885 void Pet::CastPetAuras(bool current
)
1887 Unit
* owner
= GetOwner();
1888 if(!owner
|| owner
->GetTypeId()!=TYPEID_PLAYER
)
1891 if(!IsPermanentPetFor((Player
*)owner
))
1894 for(PetAuraSet::const_iterator itr
= owner
->m_petAuras
.begin(); itr
!= owner
->m_petAuras
.end();)
1896 PetAura
const* pa
= *itr
;
1899 if(!current
&& pa
->IsRemovedOnChangePet())
1900 owner
->RemovePetAura(pa
);
1906 void Pet::CastPetAura(PetAura
const* aura
)
1908 uint32 auraId
= aura
->GetAura(GetEntry());
1912 if(auraId
== 35696) // Demonic Knowledge
1914 int32 basePoints
= int32(aura
->GetDamage() * (GetStat(STAT_STAMINA
) + GetStat(STAT_INTELLECT
)) / 100);
1915 CastCustomSpell(this, auraId
, &basePoints
, NULL
, NULL
, true);
1918 CastSpell(this, auraId
, true);
1921 struct DoPetLearnSpell
1923 DoPetLearnSpell(Pet
& _pet
) : pet(_pet
) {}
1924 void operator() (uint32 spell_id
) { pet
.learnSpell(spell_id
); }
1928 void Pet::learnSpellHighRank(uint32 spellid
)
1930 learnSpell(spellid
);
1932 DoPetLearnSpell
worker(*this);
1933 spellmgr
.doForHighRanks(spellid
,worker
);
1936 void Pet::SynchronizeLevelWithOwner()
1938 Unit
* owner
= GetOwner();
1939 if (!owner
|| owner
->GetTypeId() != TYPEID_PLAYER
)
1942 switch(getPetType())
1944 // always same level
1946 GivePetLevel(owner
->getLevel());
1948 // can't be greater owner level
1950 if(getLevel() > owner
->getLevel())
1952 GivePetLevel(owner
->getLevel());
1953 SetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP
, objmgr
.GetXPForLevel(owner
->getLevel())/4);
1954 SetUInt32Value(UNIT_FIELD_PETEXPERIENCE
, GetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP
)-1);