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