root/trunk/src/game/PetHandler.cpp @ 102

Revision 102, 23.2 kB (checked in by yumileroy, 17 years ago)

[svn] Fixed copyright notices to comply with GPL.

Original author: w12x
Date: 2008-10-23 03:29:52-05:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
4 * Copyright (C) 2008 Trinity <http://www.trinitycore.org/>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include "Common.h"
22#include "WorldPacket.h"
23#include "WorldSession.h"
24#include "World.h"
25#include "ObjectMgr.h"
26#include "SpellMgr.h"
27#include "Log.h"
28#include "Opcodes.h"
29#include "Spell.h"
30#include "ObjectAccessor.h"
31#include "MapManager.h"
32#include "CreatureAI.h"
33#include "Util.h"
34#include "Pet.h"
35#include "Language.h"
36
37void WorldSession::HandlePetAction( WorldPacket & recv_data )
38{
39    CHECK_PACKET_SIZE(recv_data,8+2+2+8);
40
41    uint64 guid1;
42    uint16 spellid;
43    uint16 flag;
44    uint64 guid2;
45    recv_data >> guid1;                                     //pet guid
46    recv_data >> spellid;
47    recv_data >> flag;                                      //delete = 0x0700 CastSpell = C100
48    recv_data >> guid2;                                     //tag guid
49
50    // used also for charmed creature
51    Unit* pet= ObjectAccessor::GetUnit(*_player,guid1);
52    sLog.outDetail( "HandlePetAction.Pet %u flag is %u, spellid is %u, target %u.\n", uint32(GUID_LOPART(guid1)), flag, spellid, uint32(GUID_LOPART(guid2)) );
53    if(!pet)
54    {
55        sLog.outError( "Pet %u not exist.\n", uint32(GUID_LOPART(guid1)) );
56        return;
57    }
58
59    if(pet != GetPlayer()->GetPet() && pet != GetPlayer()->GetCharm())
60    {
61        sLog.outError( "HandlePetAction.Pet %u isn't pet of player %s .\n", uint32(GUID_LOPART(guid1)),GetPlayer()->GetName() );
62        return;
63    }
64
65    if(!pet->isAlive())
66        return;
67
68    if(pet->GetTypeId() == TYPEID_PLAYER && !(flag == ACT_COMMAND && spellid == COMMAND_ATTACK))
69        return;
70
71    CharmInfo *charmInfo = pet->GetCharmInfo();
72    if(!charmInfo)
73    {
74        sLog.outError("WorldSession::HandlePetAction: object "I64FMTD" is considered pet-like but doesn't have a charminfo!", pet->GetGUID());
75        return;
76    }
77
78    switch(flag)
79    {
80        case ACT_COMMAND:                                   //0x0700
81            switch(spellid)
82            {
83                case COMMAND_STAY:                          //flat=1792  //STAY
84                    pet->StopMoving();
85                    pet->GetMotionMaster()->Clear();
86                    pet->GetMotionMaster()->MoveIdle();
87                    charmInfo->SetCommandState( COMMAND_STAY );
88                    break;
89                case COMMAND_FOLLOW:                        //spellid=1792  //FOLLOW
90                    pet->AttackStop();
91                    pet->GetMotionMaster()->MoveFollow(_player,PET_FOLLOW_DIST,PET_FOLLOW_ANGLE);
92                    charmInfo->SetCommandState( COMMAND_FOLLOW );
93                    break;
94                case COMMAND_ATTACK:                        //spellid=1792  //ATTACK
95                {
96                    // only place where pet can be player
97                    pet->clearUnitState(UNIT_STAT_FOLLOW);
98                    uint64 selguid = _player->GetSelection();
99                    Unit *TargetUnit = ObjectAccessor::GetUnit(*_player, selguid);
100                    if(!TargetUnit)
101                        return;
102
103                    // not let attack friendly units.
104                    if( GetPlayer()->IsFriendlyTo(TargetUnit))
105                        return;
106
107                    if(pet->getVictim())
108                        pet->AttackStop();
109
110                    if(pet->GetTypeId() != TYPEID_PLAYER)
111                    {
112                        pet->GetMotionMaster()->Clear();
113                        if (((Creature*)pet)->AI())
114                            ((Creature*)pet)->AI()->AttackStart(TargetUnit);
115
116                        //10% chance to play special pet attack talk, else growl
117                        if(((Creature*)pet)->isPet() && ((Pet*)pet)->getPetType() == SUMMON_PET && pet != TargetUnit && urand(0, 100) < 10)
118                            pet->SendPetTalk((uint32)PET_TALK_ATTACK);
119                        else
120                        {
121                            // 90% chance for pet and 100% chance for charmed creature
122                            pet->SendPetAIReaction(guid1);
123                        }
124                    }
125                    else                                    // charmed player
126                    {
127                        pet->Attack(TargetUnit,true);
128                        pet->SendPetAIReaction(guid1);
129                    }
130                    break;
131                }
132                case COMMAND_ABANDON:                       // abandon (hunter pet) or dismiss (summoned pet)
133                    if(((Creature*)pet)->isPet())
134                    {
135                        Pet* p = (Pet*)pet;
136                        if(p->getPetType() == HUNTER_PET)
137                            _player->RemovePet(p,PET_SAVE_AS_DELETED);
138                        else
139                            //dismissing a summoned pet is like killing them (this prevents returning a soulshard...)
140                            p->setDeathState(CORPSE);
141                    }
142                    else                                    // charmed
143                        _player->Uncharm();
144                    break;
145                default:
146                    sLog.outError("WORLD: unknown PET flag Action %i and spellid %i.\n", flag, spellid);
147            }
148            break;
149        case ACT_REACTION:                                  // 0x600
150            switch(spellid)
151            {
152                case REACT_PASSIVE:                         //passive
153                case REACT_DEFENSIVE:                       //recovery
154                case REACT_AGGRESSIVE:                      //activete
155                    charmInfo->SetReactState( ReactStates(spellid) );
156                    break;
157            }
158            break;
159        case ACT_DISABLED:                                  //0x8100    spell (disabled), ignore
160        case ACT_CAST:                                      //0x0100
161        case ACT_ENABLED:                                   //0xc100    spell
162        {
163            Unit* unit_target;
164            if(guid2)
165                unit_target = ObjectAccessor::GetUnit(*_player,guid2);
166            else
167                unit_target = NULL;
168
169            // do not cast unknown spells
170            SpellEntry const *spellInfo = sSpellStore.LookupEntry(spellid );
171            if(!spellInfo)
172            {
173                sLog.outError("WORLD: unknown PET spell id %i\n", spellid);
174                return;
175            }
176
177            for(uint32 i = 0; i < 3;i++)
178            {
179                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)
180                    return;
181            }
182
183            // do not cast not learned spells
184            if(!pet->HasSpell(spellid) || IsPassiveSpell(spellid))
185                return;
186
187            pet->clearUnitState(UNIT_STAT_FOLLOW);
188
189            Spell *spell = new Spell(pet, spellInfo, false);
190
191            int16 result = spell->PetCanCast(unit_target);
192
193                                                            //auto turn to target unless possessed
194            if(result == SPELL_FAILED_UNIT_NOT_INFRONT && !pet->HasAuraType(SPELL_AURA_MOD_POSSESS))
195            {
196                pet->SetInFront(unit_target);
197                if( unit_target->GetTypeId() == TYPEID_PLAYER )
198                    pet->SendUpdateToPlayer( (Player*)unit_target );
199                if(Unit* powner = pet->GetCharmerOrOwner())
200                    if(powner->GetTypeId() == TYPEID_PLAYER)
201                        pet->SendUpdateToPlayer((Player*)powner);
202                result = -1;
203            }
204
205            if(result == -1)
206            {
207                ((Creature*)pet)->AddCreatureSpellCooldown(spellid);
208                if (((Creature*)pet)->isPet())
209                    ((Pet*)pet)->CheckLearning(spellid);
210
211                unit_target = spell->m_targets.getUnitTarget();
212
213                //10% chance to play special pet attack talk, else growl
214                //actually this only seems to happen on special spells, fire shield for imp, torment for voidwalker, but it's stupid to check every spell
215                if(((Creature*)pet)->isPet() && (((Pet*)pet)->getPetType() == SUMMON_PET) && (pet != unit_target) && (urand(0, 100) < 10))
216                    pet->SendPetTalk((uint32)PET_TALK_SPECIAL_SPELL);
217                else
218                {
219                    pet->SendPetAIReaction(guid1);
220                }
221
222                if( unit_target && !GetPlayer()->IsFriendlyTo(unit_target) && !pet->HasAuraType(SPELL_AURA_MOD_POSSESS))
223                {
224                    pet->clearUnitState(UNIT_STAT_FOLLOW);
225                    if(pet->getVictim())
226                        pet->AttackStop();
227                    pet->GetMotionMaster()->Clear();
228                    if (((Creature*)pet)->AI())
229                        ((Creature*)pet)->AI()->AttackStart(unit_target);
230                }
231
232                spell->prepare(&(spell->m_targets));
233            }
234            else
235            {
236                if(pet->HasAuraType(SPELL_AURA_MOD_POSSESS))
237                {
238                    WorldPacket data(SMSG_CAST_FAILED, (4+1+1));
239                    data << uint32(spellid) << uint8(2) << uint8(result);
240                    switch (result)
241                    {
242                        case SPELL_FAILED_REQUIRES_SPELL_FOCUS:
243                            data << uint32(spellInfo->RequiresSpellFocus);
244                            break;
245                        case SPELL_FAILED_REQUIRES_AREA:
246                            data << uint32(spellInfo->AreaId);
247                            break;
248                    }
249                    SendPacket(&data);
250                }
251                else
252                    pet->SendPetCastFail(spellid, result);
253
254                if(!((Creature*)pet)->HasSpellCooldown(spellid))
255                    pet->SendPetClearCooldown(spellid);
256
257                spell->finish(false);
258                delete spell;
259            }
260            break;
261        }
262        default:
263            sLog.outError("WORLD: unknown PET flag Action %i and spellid %i.\n", flag, spellid);
264    }
265}
266
267void WorldSession::HandlePetNameQuery( WorldPacket & recv_data )
268{
269    CHECK_PACKET_SIZE(recv_data,4+8);
270
271    sLog.outDetail( "HandlePetNameQuery. CMSG_PET_NAME_QUERY\n" );
272
273    uint32 petnumber;
274    uint64 petguid;
275
276    recv_data >> petnumber;
277    recv_data >> petguid;
278
279    SendPetNameQuery(petguid,petnumber);
280}
281
282void WorldSession::SendPetNameQuery( uint64 petguid, uint32 petnumber)
283{
284    Creature* pet = ObjectAccessor::GetCreatureOrPet(*_player, petguid);
285    if(!pet || !pet->GetCharmInfo() || pet->GetCharmInfo()->GetPetNumber() != petnumber)
286        return;
287
288    std::string name = pet->GetName();
289
290    WorldPacket data(SMSG_PET_NAME_QUERY_RESPONSE, (4+4+name.size()+1));
291    data << uint32(petnumber);
292    data << name.c_str();
293    data << uint32(pet->GetUInt32Value(UNIT_FIELD_PET_NAME_TIMESTAMP));
294
295    if( pet->isPet() && ((Pet*)pet)->GetDeclinedNames() )
296    {
297        data << uint8(1);
298        for(int i = 0; i < MAX_DECLINED_NAME_CASES; ++i)
299            data << ((Pet*)pet)->GetDeclinedNames()->name[i];
300    }
301    else
302        data << uint8(0);
303
304    _player->GetSession()->SendPacket(&data);
305}
306
307void WorldSession::HandlePetSetAction( WorldPacket & recv_data )
308{
309    CHECK_PACKET_SIZE(recv_data,8+4+2+2);
310
311    sLog.outDetail( "HandlePetSetAction. CMSG_PET_SET_ACTION\n" );
312
313    uint64 petguid;
314    uint32 position;
315    uint16 spell_id;
316    uint16 act_state;
317    uint8  count;
318
319    recv_data >> petguid;
320
321    // FIXME: charmed case
322    //Pet* pet = ObjectAccessor::Instance().GetPet(petguid);
323    if(ObjectAccessor::FindPlayer(petguid))
324        return;
325
326    Creature* pet = ObjectAccessor::GetCreatureOrPet(*_player, petguid);
327
328    if(!pet || (pet != _player->GetPet() && pet != _player->GetCharm()))
329    {
330        sLog.outError( "HandlePetSetAction: Unknown pet or pet owner.\n" );
331        return;
332    }
333
334    CharmInfo *charmInfo = pet->GetCharmInfo();
335    if(!charmInfo)
336    {
337        sLog.outError("WorldSession::HandlePetSetAction: object "I64FMTD" is considered pet-like but doesn't have a charminfo!", pet->GetGUID());
338        return;
339    }
340
341    count = (recv_data.size() == 24) ? 2 : 1;
342    for(uint8 i = 0; i < count; i++)
343    {
344        recv_data >> position;
345        recv_data >> spell_id;
346        recv_data >> act_state;
347
348        sLog.outDetail( "Player %s has changed pet spell action. Position: %u, Spell: %u, State: 0x%X\n", _player->GetName(), position, spell_id, act_state);
349
350                                                            //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
351        if(!((act_state == ACT_ENABLED || act_state == ACT_DISABLED || act_state == ACT_CAST) && spell_id && !pet->HasSpell(spell_id)))
352        {
353            //sign for autocast
354            if(act_state == ACT_ENABLED && spell_id)
355            {
356                if(pet->isCharmed())
357                    charmInfo->ToggleCreatureAutocast(spell_id, true);
358                else
359                    ((Pet*)pet)->ToggleAutocast(spell_id, true);
360            }
361            //sign for no/turn off autocast
362            else if(act_state == ACT_DISABLED && spell_id)
363            {
364                if(pet->isCharmed())
365                    charmInfo->ToggleCreatureAutocast(spell_id, false);
366                else
367                    ((Pet*)pet)->ToggleAutocast(spell_id, false);
368            }
369
370            charmInfo->GetActionBarEntry(position)->Type = act_state;
371            charmInfo->GetActionBarEntry(position)->SpellOrAction = spell_id;
372        }
373    }
374}
375
376void WorldSession::HandlePetRename( WorldPacket & recv_data )
377{
378    CHECK_PACKET_SIZE(recv_data,8+1+1+1+1+1+1+1);
379
380    sLog.outDetail( "HandlePetRename. CMSG_PET_RENAME\n" );
381
382    uint64 petguid;
383    uint8 isdeclined;
384
385    std::string name;
386    DeclinedName declinedname;
387
388    recv_data >> petguid;
389    recv_data >> name;
390    recv_data >> isdeclined;
391
392    Pet* pet = ObjectAccessor::GetPet(petguid);
393                                                            // check it!
394    if( !pet || !pet->isPet() || ((Pet*)pet)->getPetType()!= HUNTER_PET ||
395        pet->GetByteValue(UNIT_FIELD_BYTES_2, 2) != UNIT_RENAME_ALLOWED ||
396        pet->GetOwnerGUID() != _player->GetGUID() || !pet->GetCharmInfo() )
397        return;
398
399    if((!ObjectMgr::IsValidPetName(name)) || (objmgr.IsReservedName(name)))
400    {
401        SendNotification(LANG_PET_INVALID_NAME);
402        return;
403    }
404    pet->SetName(name);
405
406    Unit *owner = pet->GetOwner();
407    if(owner && (owner->GetTypeId() == TYPEID_PLAYER) && ((Player*)owner)->GetGroup())
408        ((Player*)owner)->SetGroupUpdateFlag(GROUP_UPDATE_FLAG_PET_NAME);
409
410    pet->SetByteValue(UNIT_FIELD_BYTES_2, 2, UNIT_RENAME_NOT_ALLOWED);
411
412    if(isdeclined)
413    {
414        for(int i = 0; i < MAX_DECLINED_NAME_CASES; ++i)
415            recv_data >> declinedname.name[i];
416
417        std::wstring wname;
418        Utf8toWStr(name,wname);
419        if(!ObjectMgr::CheckDeclinedNames(GetMainPartOfName(wname,0),declinedname))
420        {
421            SendNotification(LANG_PET_INVALID_NAME);
422            return;
423        }
424    }
425
426    CharacterDatabase.BeginTransaction();
427    if(isdeclined)
428    {
429        for(int i = 0; i < MAX_DECLINED_NAME_CASES; ++i)
430            CharacterDatabase.escape_string(declinedname.name[i]);
431        CharacterDatabase.PExecute("DELETE FROM character_pet_declinedname WHERE owner = '%u' AND id = '%u'", _player->GetGUIDLow(), pet->GetCharmInfo()->GetPetNumber());
432        CharacterDatabase.PExecute("INSERT INTO character_pet_declinedname (id, owner, genitive, dative, accusative, instrumental, prepositional) VALUES ('%u','%u','%s','%s','%s','%s','%s')",
433            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());
434    }
435
436    CharacterDatabase.escape_string(name);
437    CharacterDatabase.PExecute("UPDATE character_pet SET name = '%s', renamed = '1' WHERE owner = '%u' AND id = '%u'", name.c_str(),_player->GetGUIDLow(),pet->GetCharmInfo()->GetPetNumber() );
438    CharacterDatabase.CommitTransaction();
439
440    pet->SetUInt32Value(UNIT_FIELD_PET_NAME_TIMESTAMP, time(NULL));
441}
442
443void WorldSession::HandlePetAbandon( WorldPacket & recv_data )
444{
445    CHECK_PACKET_SIZE(recv_data,8);
446
447    uint64 guid;
448    recv_data >> guid;                                      //pet guid
449    sLog.outDetail( "HandlePetAbandon. CMSG_PET_ABANDON pet guid is %u", GUID_LOPART(guid) );
450
451    // pet/charmed
452    Creature* pet=ObjectAccessor::GetCreatureOrPet(*_player, guid);
453    if(pet)
454    {
455        if(pet->isPet())
456        {
457            if(pet->GetGUID() == _player->GetPetGUID())
458            {
459                uint32 feelty = pet->GetPower(POWER_HAPPINESS);
460                pet->SetPower(POWER_HAPPINESS ,(feelty-50000) > 0 ?(feelty-50000) : 0);
461            }
462
463            _player->RemovePet((Pet*)pet,PET_SAVE_AS_DELETED);
464        }
465        else if(pet->GetGUID() == _player->GetCharmGUID())
466        {
467            _player->Uncharm();
468        }
469    }
470}
471
472void WorldSession::HandlePetUnlearnOpcode(WorldPacket& recvPacket)
473{
474    CHECK_PACKET_SIZE(recvPacket,8);
475
476    sLog.outDetail("CMSG_PET_UNLEARN");
477    uint64 guid;
478    recvPacket >> guid;
479
480    Pet* pet = _player->GetPet();
481
482    if(!pet || pet->getPetType() != HUNTER_PET || pet->m_spells.size() <= 1)
483        return;
484
485    if(guid != pet->GetGUID())
486    {
487        sLog.outError( "HandlePetUnlearnOpcode.Pet %u isn't pet of player %s .\n", uint32(GUID_LOPART(guid)),GetPlayer()->GetName() );
488        return;
489    }
490
491    CharmInfo *charmInfo = pet->GetCharmInfo();
492    if(!charmInfo)
493    {
494        sLog.outError("WorldSession::HandlePetUnlearnOpcode: object "I64FMTD" is considered pet-like but doesn't have a charminfo!", pet->GetGUID());
495        return;
496    }
497
498    uint32 cost = pet->resetTalentsCost();
499
500    if (GetPlayer()->GetMoney() < cost)
501    {
502        GetPlayer()->SendBuyError( BUY_ERR_NOT_ENOUGHT_MONEY, 0, 0, 0);
503        return;
504    }
505
506    for(PetSpellMap::iterator itr = pet->m_spells.begin(); itr != pet->m_spells.end();)
507    {
508        uint32 spell_id = itr->first;                       // Pet::removeSpell can invalidate iterator at erase NEW spell
509        ++itr;
510        pet->removeSpell(spell_id);
511    }
512
513    pet->SetTP(pet->getLevel() * (pet->GetLoyaltyLevel() - 1));
514
515    for(uint8 i = 0; i < 10; i++)
516    {
517        if(charmInfo->GetActionBarEntry(i)->SpellOrAction && charmInfo->GetActionBarEntry(i)->Type == ACT_ENABLED || charmInfo->GetActionBarEntry(i)->Type == ACT_DISABLED)
518            charmInfo->GetActionBarEntry(i)->SpellOrAction = 0;
519    }
520
521    // relearn pet passives
522    pet->LearnPetPassives();
523
524    pet->m_resetTalentsTime = time(NULL);
525    pet->m_resetTalentsCost = cost;
526    GetPlayer()->ModifyMoney(-(int32)cost);
527
528    GetPlayer()->PetSpellInitialize();
529}
530
531void WorldSession::HandlePetSpellAutocastOpcode( WorldPacket& recvPacket )
532{
533    CHECK_PACKET_SIZE(recvPacket,8+2+2+1);
534
535    sLog.outDetail("CMSG_PET_SPELL_AUTOCAST");
536    uint64 guid;
537    uint16 spellid;
538    uint16 spellid2;                                        //maybe second spell, automatically toggled off when first toggled on?
539    uint8  state;                                           //1 for on, 0 for off
540    recvPacket >> guid >> spellid >> spellid2 >> state;
541
542    if(!_player->GetPet() && !_player->GetCharm())
543        return;
544
545    if(ObjectAccessor::FindPlayer(guid))
546        return;
547
548    Creature* pet=ObjectAccessor::GetCreatureOrPet(*_player,guid);
549
550    if(!pet || (pet != _player->GetPet() && pet != _player->GetCharm()))
551    {
552        sLog.outError( "HandlePetSpellAutocastOpcode.Pet %u isn't pet of player %s .\n", uint32(GUID_LOPART(guid)),GetPlayer()->GetName() );
553        return;
554    }
555
556    // do not add not learned spells/ passive spells
557    if(!pet->HasSpell(spellid) || IsPassiveSpell(spellid))
558        return;
559
560    CharmInfo *charmInfo = pet->GetCharmInfo();
561    if(!charmInfo)
562    {
563        sLog.outError("WorldSession::HandlePetSpellAutocastOpcod: object "I64FMTD" is considered pet-like but doesn't have a charminfo!", pet->GetGUID());
564        return;
565    }
566
567    if(pet->isCharmed())
568                                                            //state can be used as boolean
569        pet->GetCharmInfo()->ToggleCreatureAutocast(spellid, state);
570    else
571        ((Pet*)pet)->ToggleAutocast(spellid, state);
572
573    for(uint8 i = 0; i < 10; ++i)
574    {
575        if((charmInfo->GetActionBarEntry(i)->Type == ACT_ENABLED || charmInfo->GetActionBarEntry(i)->Type == ACT_DISABLED) && spellid == charmInfo->GetActionBarEntry(i)->SpellOrAction)
576            charmInfo->GetActionBarEntry(i)->Type = state ? ACT_ENABLED : ACT_DISABLED;
577    }
578}
579
580void WorldSession::HandleAddDynamicTargetObsoleteOpcode( WorldPacket& recvPacket )
581{
582    sLog.outDetail("WORLD: CMSG_PET_CAST_SPELL");
583
584    CHECK_PACKET_SIZE(recvPacket,8+4);
585    uint64 guid;
586    uint32 spellid;
587
588    recvPacket >> guid >> spellid;
589
590    if(!_player->GetPet() && !_player->GetCharm())
591        return;
592
593    if(ObjectAccessor::FindPlayer(guid))
594        return;
595
596    Creature* pet=ObjectAccessor::GetCreatureOrPet(*_player,guid);
597
598    if(!pet || (pet != _player->GetPet() && pet!= _player->GetCharm()))
599    {
600        sLog.outError( "HandleAddDynamicTargetObsoleteOpcode.Pet %u isn't pet of player %s .\n", uint32(GUID_LOPART(guid)),GetPlayer()->GetName() );
601        return;
602    }
603
604    SpellEntry const *spellInfo = sSpellStore.LookupEntry(spellid);
605    if(!spellInfo)
606    {
607        sLog.outError("WORLD: unknown PET spell id %i\n", spellid);
608        return;
609    }
610
611    // do not cast not learned spells
612    if(!pet->HasSpell(spellid) || IsPassiveSpell(spellid))
613        return;
614
615    SpellCastTargets targets;
616    if(!targets.read(&recvPacket,pet))
617        return;
618
619    pet->clearUnitState(UNIT_STAT_FOLLOW);
620
621    Spell *spell = new Spell(pet, spellInfo, false);
622    spell->m_targets = targets;
623
624    int16 result = spell->PetCanCast(NULL);
625    if(result == -1)
626    {
627        pet->AddCreatureSpellCooldown(spellid);
628        if(pet->isPet())
629        {
630            Pet* p = (Pet*)pet;
631            p->CheckLearning(spellid);
632            //10% chance to play special pet attack talk, else growl
633            //actually this only seems to happen on special spells, fire shield for imp, torment for voidwalker, but it's stupid to check every spell
634            if(p->getPetType() == SUMMON_PET && (urand(0, 100) < 10))
635                pet->SendPetTalk((uint32)PET_TALK_SPECIAL_SPELL);
636            else
637                pet->SendPetAIReaction(guid);
638        }
639
640        spell->prepare(&(spell->m_targets));
641    }
642    else
643    {
644        pet->SendPetCastFail(spellid, result);
645        if(!pet->HasSpellCooldown(spellid))
646            pet->SendPetClearCooldown(spellid);
647
648        spell->finish(false);
649        delete spell;
650    }
651}
Note: See TracBrowser for help on using the browser.