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

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

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

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