[7297] Fixed profession spells sorting in trainer spell list at client.
[getmangos.git] / src / game / PetHandler.cpp
blob72921513a3b729ebfa245c9cb9471c12be0ce9db
1 /*
2 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "Common.h"
20 #include "WorldPacket.h"
21 #include "WorldSession.h"
22 #include "World.h"
23 #include "ObjectMgr.h"
24 #include "SpellMgr.h"
25 #include "Log.h"
26 #include "Opcodes.h"
27 #include "Spell.h"
28 #include "ObjectAccessor.h"
29 #include "MapManager.h"
30 #include "CreatureAI.h"
31 #include "Util.h"
32 #include "Pet.h"
33 #include "Language.h"
35 void WorldSession::HandlePetAction( WorldPacket & recv_data )
37 CHECK_PACKET_SIZE(recv_data, 8+2+2+8);
39 uint64 guid1;
40 uint16 spellid;
41 uint16 flag;
42 uint64 guid2;
43 recv_data >> guid1; //pet guid
44 recv_data >> spellid;
45 recv_data >> flag; //delete = 0x0700 CastSpell = C100
46 recv_data >> guid2; //tag guid
48 // used also for charmed creature
49 Unit* pet= ObjectAccessor::GetUnit(*_player, guid1);
50 sLog.outDetail("HandlePetAction.Pet %u flag is %u, spellid is %u, target %u.\n", uint32(GUID_LOPART(guid1)), flag, spellid, uint32(GUID_LOPART(guid2)) );
51 if(!pet)
53 sLog.outError( "Pet %u not exist.\n", uint32(GUID_LOPART(guid1)) );
54 return;
57 if(pet != GetPlayer()->GetPet() && pet != GetPlayer()->GetCharm())
59 sLog.outError("HandlePetAction.Pet %u isn't pet of player %s.\n", uint32(GUID_LOPART(guid1)), GetPlayer()->GetName() );
60 return;
63 if(!pet->isAlive())
64 return;
66 if(pet->GetTypeId() == TYPEID_PLAYER && !(flag == ACT_COMMAND && spellid == COMMAND_ATTACK))
67 return;
69 CharmInfo *charmInfo = pet->GetCharmInfo();
70 if(!charmInfo)
72 sLog.outError("WorldSession::HandlePetAction: object "I64FMTD" is considered pet-like but doesn't have a charminfo!", pet->GetGUID());
73 return;
76 switch(flag)
78 case ACT_COMMAND: //0x0700
79 switch(spellid)
81 case COMMAND_STAY: //flat=1792 //STAY
82 pet->StopMoving();
83 pet->GetMotionMaster()->Clear();
84 pet->GetMotionMaster()->MoveIdle();
85 charmInfo->SetCommandState( COMMAND_STAY );
86 break;
87 case COMMAND_FOLLOW: //spellid=1792 //FOLLOW
88 pet->AttackStop();
89 pet->GetMotionMaster()->MoveFollow(_player,PET_FOLLOW_DIST,PET_FOLLOW_ANGLE);
90 charmInfo->SetCommandState( COMMAND_FOLLOW );
91 break;
92 case COMMAND_ATTACK: //spellid=1792 //ATTACK
94 const uint64& selguid = _player->GetSelection();
95 Unit *TargetUnit = ObjectAccessor::GetUnit(*_player, selguid);
96 if(!TargetUnit)
97 return;
99 // not let attack friendly units.
100 if(GetPlayer()->IsFriendlyTo(TargetUnit))
101 return;
102 // Not let attack through obstructions
103 if(!pet->IsWithinLOSInMap(TargetUnit))
104 return;
106 // This is true if pet has no target or has target but targets differs.
107 if(pet->getVictim() != TargetUnit)
109 if (pet->getVictim())
110 pet->AttackStop();
112 if(pet->GetTypeId() != TYPEID_PLAYER)
114 pet->GetMotionMaster()->Clear();
115 if (((Creature*)pet)->AI())
116 ((Creature*)pet)->AI()->AttackStart(TargetUnit);
118 //10% chance to play special pet attack talk, else growl
119 if(((Creature*)pet)->isPet() && ((Pet*)pet)->getPetType() == SUMMON_PET && pet != TargetUnit && urand(0, 100) < 10)
120 pet->SendPetTalk((uint32)PET_TALK_ATTACK);
121 else
123 // 90% chance for pet and 100% chance for charmed creature
124 pet->SendPetAIReaction(guid1);
127 else // charmed player
129 pet->Attack(TargetUnit,true);
130 pet->SendPetAIReaction(guid1);
133 break;
135 case COMMAND_ABANDON: // abandon (hunter pet) or dismiss (summoned pet)
136 if(((Creature*)pet)->isPet())
138 Pet* p = (Pet*)pet;
139 if(p->getPetType() == HUNTER_PET)
140 _player->RemovePet(p,PET_SAVE_AS_DELETED);
141 else
142 //dismissing a summoned pet is like killing them (this prevents returning a soulshard...)
143 p->setDeathState(CORPSE);
145 else // charmed
146 _player->Uncharm();
147 break;
148 default:
149 sLog.outError("WORLD: unknown PET flag Action %i and spellid %i.\n", flag, spellid);
151 break;
152 case ACT_REACTION: // 0x600
153 switch(spellid)
155 case REACT_PASSIVE: //passive
156 case REACT_DEFENSIVE: //recovery
157 case REACT_AGGRESSIVE: //activete
158 charmInfo->SetReactState( ReactStates(spellid) );
159 break;
161 break;
162 case ACT_DISABLED: // 0x8100 spell (disabled), ignore
163 case ACT_PASSIVE: // 0x0100
164 case ACT_ENABLED: // 0xC100 spell
166 Unit* unit_target = NULL;
167 if (((Creature*)pet)->GetGlobalCooldown() > 0)
168 return;
170 if(guid2)
171 unit_target = ObjectAccessor::GetUnit(*_player,guid2);
173 // do not cast unknown spells
174 SpellEntry const *spellInfo = sSpellStore.LookupEntry(spellid );
175 if(!spellInfo)
177 sLog.outError("WORLD: unknown PET spell id %i\n", spellid);
178 return;
181 for(uint32 i = 0; i < 3;i++)
183 if(spellInfo->EffectImplicitTargetA[i] == TARGET_ALL_ENEMY_IN_AREA || spellInfo->EffectImplicitTargetA[i] == TARGET_ALL_ENEMY_IN_AREA_INSTANT || spellInfo->EffectImplicitTargetA[i] == TARGET_ALL_ENEMY_IN_AREA_CHANNELED)
184 return;
187 // do not cast not learned spells
188 if(!pet->HasSpell(spellid) || IsPassiveSpell(spellid))
189 return;
191 pet->clearUnitState(UNIT_STAT_FOLLOW);
193 Spell *spell = new Spell(pet, spellInfo, false);
195 int16 result = spell->PetCanCast(unit_target);
197 //auto turn to target unless possessed
198 if(result == SPELL_FAILED_UNIT_NOT_INFRONT && !pet->HasAuraType(SPELL_AURA_MOD_POSSESS))
200 pet->SetInFront(unit_target);
201 if( unit_target->GetTypeId() == TYPEID_PLAYER )
202 pet->SendUpdateToPlayer( (Player*)unit_target );
203 if(Unit* powner = pet->GetCharmerOrOwner())
204 if(powner->GetTypeId() == TYPEID_PLAYER)
205 pet->SendUpdateToPlayer((Player*)powner);
206 result = -1;
209 if(result == -1)
211 ((Creature*)pet)->AddCreatureSpellCooldown(spellid);
212 if (((Creature*)pet)->isPet())
213 ((Pet*)pet)->CheckLearning(spellid);
215 unit_target = spell->m_targets.getUnitTarget();
217 //10% chance to play special pet attack talk, else growl
218 //actually this only seems to happen on special spells, fire shield for imp, torment for voidwalker, but it's stupid to check every spell
219 if(((Creature*)pet)->isPet() && (((Pet*)pet)->getPetType() == SUMMON_PET) && (pet != unit_target) && (urand(0, 100) < 10))
220 pet->SendPetTalk((uint32)PET_TALK_SPECIAL_SPELL);
221 else
223 pet->SendPetAIReaction(guid1);
226 if( unit_target && !GetPlayer()->IsFriendlyTo(unit_target) && !pet->HasAuraType(SPELL_AURA_MOD_POSSESS))
228 // This is true if pet has no target or has target but targets differs.
229 if (pet->getVictim() != unit_target)
231 if (pet->getVictim())
232 pet->AttackStop();
233 pet->GetMotionMaster()->Clear();
234 if (((Creature*)pet)->AI())
235 ((Creature*)pet)->AI()->AttackStart(unit_target);
239 spell->prepare(&(spell->m_targets));
241 else
243 if(pet->HasAuraType(SPELL_AURA_MOD_POSSESS))
245 WorldPacket data(SMSG_CAST_FAILED, (4+1+1));
246 data << uint8(0) << uint32(spellid) << uint8(result);
247 switch (result)
249 case SPELL_FAILED_REQUIRES_SPELL_FOCUS:
250 data << uint32(spellInfo->RequiresSpellFocus);
251 break;
253 SendPacket(&data);
255 else
256 pet->SendPetCastFail(spellid, result);
258 if(!((Creature*)pet)->HasSpellCooldown(spellid))
259 pet->SendPetClearCooldown(spellid);
261 spell->finish(false);
262 delete spell;
264 break;
266 default:
267 sLog.outError("WORLD: unknown PET flag Action %i and spellid %i.\n", flag, spellid);
271 void WorldSession::HandlePetNameQuery( WorldPacket & recv_data )
273 CHECK_PACKET_SIZE(recv_data,4+8);
275 sLog.outDetail( "HandlePetNameQuery. CMSG_PET_NAME_QUERY\n" );
277 uint32 petnumber;
278 uint64 petguid;
280 recv_data >> petnumber;
281 recv_data >> petguid;
283 SendPetNameQuery(petguid,petnumber);
286 void WorldSession::SendPetNameQuery( uint64 petguid, uint32 petnumber)
288 Creature* pet = ObjectAccessor::GetCreatureOrPetOrVehicle(*_player, petguid);
289 if(!pet || !pet->GetCharmInfo() || pet->GetCharmInfo()->GetPetNumber() != petnumber)
290 return;
292 std::string name = pet->GetName();
294 WorldPacket data(SMSG_PET_NAME_QUERY_RESPONSE, (4+4+name.size()+1));
295 data << uint32(petnumber);
296 data << name.c_str();
297 data << uint32(pet->GetUInt32Value(UNIT_FIELD_PET_NAME_TIMESTAMP));
299 if( pet->isPet() && ((Pet*)pet)->GetDeclinedNames() )
301 data << uint8(1);
302 for(int i = 0; i < MAX_DECLINED_NAME_CASES; ++i)
303 data << ((Pet*)pet)->GetDeclinedNames()->name[i];
305 else
306 data << uint8(0);
308 _player->GetSession()->SendPacket(&data);
311 void WorldSession::HandlePetSetAction( WorldPacket & recv_data )
313 CHECK_PACKET_SIZE(recv_data, 8+4+2+2);
315 sLog.outDetail( "HandlePetSetAction. CMSG_PET_SET_ACTION\n" );
317 uint64 petguid;
318 uint32 position;
319 uint16 spell_id;
320 uint16 act_state;
321 uint8 count;
323 recv_data >> petguid;
325 // FIXME: charmed case
326 //Pet* pet = ObjectAccessor::Instance().GetPet(petguid);
327 if(ObjectAccessor::FindPlayer(petguid))
328 return;
330 Creature* pet = ObjectAccessor::GetCreatureOrPetOrVehicle(*_player, petguid);
332 if(!pet || (pet != _player->GetPet() && pet != _player->GetCharm()))
334 sLog.outError( "HandlePetSetAction: Unknown pet or pet owner.\n" );
335 return;
338 CharmInfo *charmInfo = pet->GetCharmInfo();
339 if(!charmInfo)
341 sLog.outError("WorldSession::HandlePetSetAction: object "I64FMTD" is considered pet-like but doesn't have a charminfo!", pet->GetGUID());
342 return;
345 count = (recv_data.size() == 24) ? 2 : 1;
346 for(uint8 i = 0; i < count; i++)
348 recv_data >> position;
349 recv_data >> spell_id;
350 recv_data >> act_state;
352 sLog.outDetail( "Player %s has changed pet spell action. Position: %u, Spell: %u, State: 0x%X\n", _player->GetName(), position, spell_id, act_state);
354 //if it's act for spell (en/disable/cast) and there is a spell given (0 = remove spell) which pet doesn't know, don't add
355 if(!((act_state == ACT_ENABLED || act_state == ACT_DISABLED || act_state == ACT_PASSIVE) && spell_id && !pet->HasSpell(spell_id)))
357 //sign for autocast
358 if(act_state == ACT_ENABLED && spell_id)
360 if(pet->isCharmed())
361 charmInfo->ToggleCreatureAutocast(spell_id, true);
362 else
363 ((Pet*)pet)->ToggleAutocast(spell_id, true);
365 //sign for no/turn off autocast
366 else if(act_state == ACT_DISABLED && spell_id)
368 if(pet->isCharmed())
369 charmInfo->ToggleCreatureAutocast(spell_id, false);
370 else
371 ((Pet*)pet)->ToggleAutocast(spell_id, false);
374 charmInfo->GetActionBarEntry(position)->Type = act_state;
375 charmInfo->GetActionBarEntry(position)->SpellOrAction = spell_id;
380 void WorldSession::HandlePetRename( WorldPacket & recv_data )
382 CHECK_PACKET_SIZE(recv_data, 8+1);
384 sLog.outDetail( "HandlePetRename. CMSG_PET_RENAME\n" );
386 uint64 petguid;
387 uint8 isdeclined;
389 std::string name;
390 DeclinedName declinedname;
392 recv_data >> petguid;
393 recv_data >> name;
394 CHECK_PACKET_SIZE(recv_data, recv_data.rpos() + 1);
395 recv_data >> isdeclined;
397 Pet* pet = ObjectAccessor::GetPet(petguid);
398 // check it!
399 if( !pet || !pet->isPet() || ((Pet*)pet)->getPetType()!= HUNTER_PET ||
400 pet->GetByteValue(UNIT_FIELD_BYTES_2, 2) != UNIT_RENAME_ALLOWED ||
401 pet->GetOwnerGUID() != _player->GetGUID() || !pet->GetCharmInfo() )
402 return;
404 if(!ObjectMgr::IsValidPetName(name))
406 SendPetNameInvalid(PET_NAME_INVALID, name, NULL);
407 return;
410 if(objmgr.IsReservedName(name))
412 SendPetNameInvalid(PET_NAME_RESERVED, name, NULL);
413 return;
416 pet->SetName(name);
418 Unit *owner = pet->GetOwner();
419 if(owner && (owner->GetTypeId() == TYPEID_PLAYER) && ((Player*)owner)->GetGroup())
420 ((Player*)owner)->SetGroupUpdateFlag(GROUP_UPDATE_FLAG_PET_NAME);
422 pet->SetByteValue(UNIT_FIELD_BYTES_2, 2, UNIT_RENAME_NOT_ALLOWED);
424 if(isdeclined)
426 for(int i = 0; i < MAX_DECLINED_NAME_CASES; ++i)
428 CHECK_PACKET_SIZE(recv_data, recv_data.rpos() + 1);
429 recv_data >> declinedname.name[i];
432 std::wstring wname;
433 Utf8toWStr(name, wname);
434 if(!ObjectMgr::CheckDeclinedNames(GetMainPartOfName(wname,0),declinedname))
436 SendPetNameInvalid(PET_NAME_DECLENSION_DOESNT_MATCH_BASE_NAME, name, &declinedname);
437 return;
441 CharacterDatabase.BeginTransaction();
442 if(isdeclined)
444 for(int i = 0; i < MAX_DECLINED_NAME_CASES; ++i)
445 CharacterDatabase.escape_string(declinedname.name[i]);
446 CharacterDatabase.PExecute("DELETE FROM character_pet_declinedname WHERE owner = '%u' AND id = '%u'", _player->GetGUIDLow(), pet->GetCharmInfo()->GetPetNumber());
447 CharacterDatabase.PExecute("INSERT INTO character_pet_declinedname (id, owner, genitive, dative, accusative, instrumental, prepositional) VALUES ('%u','%u','%s','%s','%s','%s','%s')",
448 pet->GetCharmInfo()->GetPetNumber(), _player->GetGUIDLow(), declinedname.name[0].c_str(), declinedname.name[1].c_str(), declinedname.name[2].c_str(), declinedname.name[3].c_str(), declinedname.name[4].c_str());
451 CharacterDatabase.escape_string(name);
452 CharacterDatabase.PExecute("UPDATE character_pet SET name = '%s', renamed = '1' WHERE owner = '%u' AND id = '%u'", name.c_str(), _player->GetGUIDLow(), pet->GetCharmInfo()->GetPetNumber());
453 CharacterDatabase.CommitTransaction();
455 pet->SetUInt32Value(UNIT_FIELD_PET_NAME_TIMESTAMP, time(NULL));
458 void WorldSession::HandlePetAbandon( WorldPacket & recv_data )
460 CHECK_PACKET_SIZE(recv_data, 8);
462 uint64 guid;
463 recv_data >> guid; //pet guid
464 sLog.outDetail( "HandlePetAbandon. CMSG_PET_ABANDON pet guid is %u", GUID_LOPART(guid) );
466 // pet/charmed
467 Creature* pet = ObjectAccessor::GetCreatureOrPetOrVehicle(*_player, guid);
468 if(pet)
470 if(pet->isPet())
472 if(pet->GetGUID() == _player->GetPetGUID())
474 uint32 feelty = pet->GetPower(POWER_HAPPINESS);
475 pet->SetPower(POWER_HAPPINESS ,(feelty-50000) > 0 ?(feelty-50000) : 0);
478 _player->RemovePet((Pet*)pet,PET_SAVE_AS_DELETED);
480 else if(pet->GetGUID() == _player->GetCharmGUID())
482 _player->Uncharm();
487 void WorldSession::HandlePetUnlearnOpcode(WorldPacket& recvPacket)
489 CHECK_PACKET_SIZE(recvPacket,8);
491 sLog.outDetail("CMSG_PET_UNLEARN");
492 uint64 guid;
493 recvPacket >> guid; // Pet guid
495 Pet* pet = _player->GetPet();
497 if(!pet || pet->getPetType() != HUNTER_PET || pet->m_usedTalentCount == 0)
498 return;
500 if(guid != pet->GetGUID())
502 sLog.outError( "HandlePetUnlearnOpcode.Pet %u isn't pet of player %s .\n", uint32(GUID_LOPART(guid)),GetPlayer()->GetName() );
503 return;
506 CharmInfo *charmInfo = pet->GetCharmInfo();
507 if(!charmInfo)
509 sLog.outError("WorldSession::HandlePetUnlearnOpcode: object "I64FMTD" is considered pet-like but doesn't have a charminfo!", pet->GetGUID());
510 return;
512 pet->resetTalents();
515 void WorldSession::HandlePetSpellAutocastOpcode( WorldPacket& recvPacket )
517 CHECK_PACKET_SIZE(recvPacket,8+2+2+1);
519 sLog.outDetail("CMSG_PET_SPELL_AUTOCAST");
520 uint64 guid;
521 uint16 spellid;
522 uint16 spellid2; //maybe second spell, automatically toggled off when first toggled on?
523 uint8 state; //1 for on, 0 for off
524 recvPacket >> guid >> spellid >> spellid2 >> state;
526 if(!_player->GetPet() && !_player->GetCharm())
527 return;
529 if(ObjectAccessor::FindPlayer(guid))
530 return;
532 Creature* pet=ObjectAccessor::GetCreatureOrPetOrVehicle(*_player,guid);
534 if(!pet || (pet != _player->GetPet() && pet != _player->GetCharm()))
536 sLog.outError( "HandlePetSpellAutocastOpcode.Pet %u isn't pet of player %s .\n", uint32(GUID_LOPART(guid)),GetPlayer()->GetName() );
537 return;
540 // do not add not learned spells/ passive spells
541 if(!pet->HasSpell(spellid) || IsPassiveSpell(spellid))
542 return;
544 CharmInfo *charmInfo = pet->GetCharmInfo();
545 if(!charmInfo)
547 sLog.outError("WorldSession::HandlePetSpellAutocastOpcod: object "I64FMTD" is considered pet-like but doesn't have a charminfo!", pet->GetGUID());
548 return;
551 if(pet->isCharmed())
552 //state can be used as boolean
553 pet->GetCharmInfo()->ToggleCreatureAutocast(spellid, state);
554 else
555 ((Pet*)pet)->ToggleAutocast(spellid, state);
557 for(uint8 i = 0; i < 10; ++i)
559 if((charmInfo->GetActionBarEntry(i)->Type == ACT_ENABLED || charmInfo->GetActionBarEntry(i)->Type == ACT_DISABLED) && spellid == charmInfo->GetActionBarEntry(i)->SpellOrAction)
560 charmInfo->GetActionBarEntry(i)->Type = state ? ACT_ENABLED : ACT_DISABLED;
564 void WorldSession::HandlePetCastSpellOpcode( WorldPacket& recvPacket )
566 sLog.outDetail("WORLD: CMSG_PET_CAST_SPELL");
568 CHECK_PACKET_SIZE(recvPacket,8+1+4+1);
569 uint64 guid;
570 uint32 spellid;
571 uint8 cast_count;
572 uint8 unk_flags; // flags (if 0x02 - some additional data are received)
574 recvPacket >> guid >> cast_count >> spellid >> unk_flags;
576 sLog.outDebug("WORLD: CMSG_PET_CAST_SPELL, cast_count: %u, spellid %u, unk_flags %u", cast_count, spellid, unk_flags);
578 if(!_player->GetPet() && !_player->GetCharm())
579 return;
581 if (GUID_HIPART(guid) == HIGHGUID_PLAYER)
582 return;
584 Creature* pet = ObjectAccessor::GetCreatureOrPetOrVehicle(*_player,guid);
586 if(!pet || (pet != _player->GetPet() && pet!= _player->GetCharm()))
588 sLog.outError( "HandlePetCastSpellOpcode: Pet %u isn't pet of player %s .\n", uint32(GUID_LOPART(guid)),GetPlayer()->GetName() );
589 return;
592 if (pet->GetGlobalCooldown() > 0)
593 return;
595 SpellEntry const *spellInfo = sSpellStore.LookupEntry(spellid);
596 if(!spellInfo)
598 sLog.outError("WORLD: unknown PET spell id %i\n", spellid);
599 return;
602 // do not cast not learned spells
603 if(!pet->HasSpell(spellid) || IsPassiveSpell(spellid))
604 return;
606 SpellCastTargets targets;
607 if(!targets.read(&recvPacket,pet))
608 return;
610 pet->clearUnitState(UNIT_STAT_FOLLOW);
612 Spell *spell = new Spell(pet, spellInfo, false);
613 spell->m_cast_count = cast_count; // probably pending spell cast
614 spell->m_targets = targets;
616 int16 result = spell->PetCanCast(NULL);
617 if(result == -1)
619 pet->AddCreatureSpellCooldown(spellid);
620 if(pet->isPet())
622 Pet* p = (Pet*)pet;
623 p->CheckLearning(spellid);
624 //10% chance to play special pet attack talk, else growl
625 //actually this only seems to happen on special spells, fire shield for imp, torment for voidwalker, but it's stupid to check every spell
626 if(p->getPetType() == SUMMON_PET && (urand(0, 100) < 10))
627 pet->SendPetTalk((uint32)PET_TALK_SPECIAL_SPELL);
628 else
629 pet->SendPetAIReaction(guid);
632 spell->prepare(&(spell->m_targets));
634 else
636 pet->SendPetCastFail(spellid, result);
637 if(!pet->HasSpellCooldown(spellid))
638 pet->SendPetClearCooldown(spellid);
640 spell->finish(false);
641 delete spell;
645 void WorldSession::SendPetNameInvalid(uint32 error, const std::string& name, DeclinedName *declinedName)
647 WorldPacket data(SMSG_PET_NAME_INVALID, 4 + name.size() + 1 + 1);
648 data << uint32(error);
649 data << name;
650 if(declinedName)
652 data << uint8(1);
653 for(uint32 i = 0; i < MAX_DECLINED_NAME_CASES; ++i)
654 data << declinedName->name[i];
656 else
657 data << uint8(0);
658 SendPacket(&data);
661 void WorldSession::HandlePetLearnTalent( WorldPacket & recv_data )
663 sLog.outDebug("WORLD: CMSG_PET_LEARN_TALENT");
664 recv_data.hexlike();
666 CHECK_PACKET_SIZE(recv_data, 8+4+4);
668 uint64 guid;
669 uint32 talent_id, requested_rank;
670 recv_data >> guid >> talent_id >> requested_rank;
672 Pet *pet = _player->GetPet();
674 if(!pet)
675 return;
677 if(guid != pet->GetGUID())
678 return;
680 uint32 CurTalentPoints = pet->GetFreeTalentPoints();
682 if(CurTalentPoints == 0)
683 return;
685 if (requested_rank > 4)
686 return;
688 TalentEntry const *talentInfo = sTalentStore.LookupEntry(talent_id);
690 if(!talentInfo)
691 return;
693 TalentTabEntry const *talentTabInfo = sTalentTabStore.LookupEntry(talentInfo->TalentTab);
695 if(!talentTabInfo)
696 return;
698 CreatureInfo const *ci = pet->GetCreatureInfo();
700 if(!ci)
701 return;
703 CreatureFamilyEntry const *pet_family = sCreatureFamilyStore.LookupEntry(ci->family);
705 if(!pet_family)
706 return;
708 if(pet_family->petTalentType < 0) // not hunter pet
709 return;
711 // prevent learn talent for different family (cheating)
712 if(!((1 << pet_family->petTalentType) & talentTabInfo->petTalentMask))
713 return;
715 // prevent skip talent ranks (cheating)
716 if(requested_rank > 0 && !pet->HasSpell(talentInfo->RankID[requested_rank-1]))
717 return;
719 // Check if it requires another talent
720 if (talentInfo->DependsOn > 0)
722 if(TalentEntry const *depTalentInfo = sTalentStore.LookupEntry(talentInfo->DependsOn))
724 bool hasEnoughRank = false;
725 for (int i = talentInfo->DependsOnRank; i <= 4; i++)
727 if (depTalentInfo->RankID[i] != 0)
728 if (pet->HasSpell(depTalentInfo->RankID[i]))
729 hasEnoughRank = true;
731 if (!hasEnoughRank)
732 return;
736 // Find out how many points we have in this field
737 uint32 spentPoints = 0;
739 uint32 tTab = talentInfo->TalentTab;
740 if (talentInfo->Row > 0)
742 unsigned int numRows = sTalentStore.GetNumRows();
743 for (unsigned int i = 0; i < numRows; i++) // Loop through all talents.
745 // Someday, someone needs to revamp
746 const TalentEntry *tmpTalent = sTalentStore.LookupEntry(i);
747 if (tmpTalent) // the way talents are tracked
749 if (tmpTalent->TalentTab == tTab)
751 for (int j = 0; j <= 4; j++)
753 if (tmpTalent->RankID[j] != 0)
755 if (pet->HasSpell(tmpTalent->RankID[j]))
757 spentPoints += j + 1;
766 // not have required min points spent in talent tree
767 if(spentPoints < (talentInfo->Row * 3))
768 return;
770 // spell not set in talent.dbc
771 uint32 spellid = talentInfo->RankID[requested_rank];
772 if( spellid == 0 )
774 sLog.outError("Talent.dbc have for talent: %u Rank: %u spell id = 0", talent_id, requested_rank);
775 return;
778 // already known
779 if(pet->HasSpell(spellid))
780 return;
782 // learn! (other talent ranks will unlearned at learning)
783 pet->learnSpell(spellid);
784 sLog.outDetail("TalentID: %u Rank: %u Spell: %u\n", talent_id, requested_rank, spellid);