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

Revision 135, 23.4 kB (checked in by yumileroy, 17 years ago)

[svn] Merge from mangos.
Wed Oct 29 15:06:00 http://github.com/mangos/mangos/commit/034b8a34a9b621443e97d0ac4b667b603ba31ef1
Mon Oct 27 14:36:42 http://github.com/mangos/mangos/commit/fa072d15107b44344af8c9ef686839984e241a1b

Original author: megamage
Date: 2008-10-29 18:06:01-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            if (((Creature*)pet)->GetGlobalCooldown() > 0)
170                return;
171
172            // do not cast unknown spells
173            SpellEntry const *spellInfo = sSpellStore.LookupEntry(spellid );
174            if(!spellInfo)
175            {
176                sLog.outError("WORLD: unknown PET spell id %i\n", spellid);
177                return;
178            }
179
180            for(uint32 i = 0; i < 3;i++)
181            {
182                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)
183                    return;
184            }
185
186            // do not cast not learned spells
187            if(!pet->HasSpell(spellid) || IsPassiveSpell(spellid))
188                return;
189
190            pet->clearUnitState(UNIT_STAT_FOLLOW);
191
192            Spell *spell = new Spell(pet, spellInfo, false);
193
194            int16 result = spell->PetCanCast(unit_target);
195
196                                                            //auto turn to target unless possessed
197            if(result == SPELL_FAILED_UNIT_NOT_INFRONT && !pet->HasAuraType(SPELL_AURA_MOD_POSSESS))
198            {
199                pet->SetInFront(unit_target);
200                if( unit_target->GetTypeId() == TYPEID_PLAYER )
201                    pet->SendUpdateToPlayer( (Player*)unit_target );
202                if(Unit* powner = pet->GetCharmerOrOwner())
203                    if(powner->GetTypeId() == TYPEID_PLAYER)
204                        pet->SendUpdateToPlayer((Player*)powner);
205                result = -1;
206            }
207
208            if(result == -1)
209            {
210                ((Creature*)pet)->AddCreatureSpellCooldown(spellid);
211                if (((Creature*)pet)->isPet())
212                    ((Pet*)pet)->CheckLearning(spellid);
213
214                unit_target = spell->m_targets.getUnitTarget();
215
216                //10% chance to play special pet attack talk, else growl
217                //actually this only seems to happen on special spells, fire shield for imp, torment for voidwalker, but it's stupid to check every spell
218                if(((Creature*)pet)->isPet() && (((Pet*)pet)->getPetType() == SUMMON_PET) && (pet != unit_target) && (urand(0, 100) < 10))
219                    pet->SendPetTalk((uint32)PET_TALK_SPECIAL_SPELL);
220                else
221                {
222                    pet->SendPetAIReaction(guid1);
223                }
224
225                if( unit_target && !GetPlayer()->IsFriendlyTo(unit_target) && !pet->HasAuraType(SPELL_AURA_MOD_POSSESS))
226                {
227                    pet->clearUnitState(UNIT_STAT_FOLLOW);
228                    if(pet->getVictim())
229                        pet->AttackStop();
230                    pet->GetMotionMaster()->Clear();
231                    if (((Creature*)pet)->AI())
232                        ((Creature*)pet)->AI()->AttackStart(unit_target);
233                }
234
235                spell->prepare(&(spell->m_targets));
236            }
237            else
238            {
239                if(pet->HasAuraType(SPELL_AURA_MOD_POSSESS))
240                {
241                    WorldPacket data(SMSG_CAST_FAILED, (4+1+1));
242                    data << uint32(spellid) << uint8(2) << uint8(result);
243                    switch (result)
244                    {
245                        case SPELL_FAILED_REQUIRES_SPELL_FOCUS:
246                            data << uint32(spellInfo->RequiresSpellFocus);
247                            break;
248                        case SPELL_FAILED_REQUIRES_AREA:
249                            data << uint32(spellInfo->AreaId);
250                            break;
251                    }
252                    SendPacket(&data);
253                }
254                else
255                    pet->SendPetCastFail(spellid, result);
256
257                if(!((Creature*)pet)->HasSpellCooldown(spellid))
258                    pet->SendPetClearCooldown(spellid);
259
260                spell->finish(false);
261                delete spell;
262            }
263            break;
264        }
265        default:
266            sLog.outError("WORLD: unknown PET flag Action %i and spellid %i.\n", flag, spellid);
267    }
268}
269
270void WorldSession::HandlePetNameQuery( WorldPacket & recv_data )
271{
272    CHECK_PACKET_SIZE(recv_data,4+8);
273
274    sLog.outDetail( "HandlePetNameQuery. CMSG_PET_NAME_QUERY\n" );
275
276    uint32 petnumber;
277    uint64 petguid;
278
279    recv_data >> petnumber;
280    recv_data >> petguid;
281
282    SendPetNameQuery(petguid,petnumber);
283}
284
285void WorldSession::SendPetNameQuery( uint64 petguid, uint32 petnumber)
286{
287    Creature* pet = ObjectAccessor::GetCreatureOrPet(*_player, petguid);
288    if(!pet || !pet->GetCharmInfo() || pet->GetCharmInfo()->GetPetNumber() != petnumber)
289        return;
290
291    std::string name = pet->GetName();
292
293    WorldPacket data(SMSG_PET_NAME_QUERY_RESPONSE, (4+4+name.size()+1));
294    data << uint32(petnumber);
295    data << name.c_str();
296    data << uint32(pet->GetUInt32Value(UNIT_FIELD_PET_NAME_TIMESTAMP));
297
298    if( pet->isPet() && ((Pet*)pet)->GetDeclinedNames() )
299    {
300        data << uint8(1);
301        for(int i = 0; i < MAX_DECLINED_NAME_CASES; ++i)
302            data << ((Pet*)pet)->GetDeclinedNames()->name[i];
303    }
304    else
305        data << uint8(0);
306
307    _player->GetSession()->SendPacket(&data);
308}
309
310void WorldSession::HandlePetSetAction( WorldPacket & recv_data )
311{
312    CHECK_PACKET_SIZE(recv_data,8+4+2+2);
313
314    sLog.outDetail( "HandlePetSetAction. CMSG_PET_SET_ACTION\n" );
315
316    uint64 petguid;
317    uint32 position;
318    uint16 spell_id;
319    uint16 act_state;
320    uint8  count;
321
322    recv_data >> petguid;
323
324    // FIXME: charmed case
325    //Pet* pet = ObjectAccessor::Instance().GetPet(petguid);
326    if(ObjectAccessor::FindPlayer(petguid))
327        return;
328
329    Creature* pet = ObjectAccessor::GetCreatureOrPet(*_player, petguid);
330
331    if(!pet || (pet != _player->GetPet() && pet != _player->GetCharm()))
332    {
333        sLog.outError( "HandlePetSetAction: Unknown pet or pet owner.\n" );
334        return;
335    }
336
337    CharmInfo *charmInfo = pet->GetCharmInfo();
338    if(!charmInfo)
339    {
340        sLog.outError("WorldSession::HandlePetSetAction: object "I64FMTD" is considered pet-like but doesn't have a charminfo!", pet->GetGUID());
341        return;
342    }
343
344    count = (recv_data.size() == 24) ? 2 : 1;
345    for(uint8 i = 0; i < count; i++)
346    {
347        recv_data >> position;
348        recv_data >> spell_id;
349        recv_data >> act_state;
350
351        sLog.outDetail( "Player %s has changed pet spell action. Position: %u, Spell: %u, State: 0x%X\n", _player->GetName(), position, spell_id, act_state);
352
353                                                            //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
354        if(!((act_state == ACT_ENABLED || act_state == ACT_DISABLED || act_state == ACT_CAST) && spell_id && !pet->HasSpell(spell_id)))
355        {
356            //sign for autocast
357            if(act_state == ACT_ENABLED && spell_id)
358            {
359                if(pet->isCharmed())
360                    charmInfo->ToggleCreatureAutocast(spell_id, true);
361                else
362                    ((Pet*)pet)->ToggleAutocast(spell_id, true);
363            }
364            //sign for no/turn off autocast
365            else if(act_state == ACT_DISABLED && spell_id)
366            {
367                if(pet->isCharmed())
368                    charmInfo->ToggleCreatureAutocast(spell_id, false);
369                else
370                    ((Pet*)pet)->ToggleAutocast(spell_id, false);
371            }
372
373            charmInfo->GetActionBarEntry(position)->Type = act_state;
374            charmInfo->GetActionBarEntry(position)->SpellOrAction = spell_id;
375        }
376    }
377}
378
379void WorldSession::HandlePetRename( WorldPacket & recv_data )
380{
381    CHECK_PACKET_SIZE(recv_data,8+1+1+1+1+1+1+1);
382
383    sLog.outDetail( "HandlePetRename. CMSG_PET_RENAME\n" );
384
385    uint64 petguid;
386    uint8 isdeclined;
387
388    std::string name;
389    DeclinedName declinedname;
390
391    recv_data >> petguid;
392    recv_data >> name;
393    recv_data >> isdeclined;
394
395    Pet* pet = ObjectAccessor::GetPet(petguid);
396                                                            // check it!
397    if( !pet || !pet->isPet() || ((Pet*)pet)->getPetType()!= HUNTER_PET ||
398        pet->GetByteValue(UNIT_FIELD_BYTES_2, 2) != UNIT_RENAME_ALLOWED ||
399        pet->GetOwnerGUID() != _player->GetGUID() || !pet->GetCharmInfo() )
400        return;
401
402    if((!ObjectMgr::IsValidPetName(name)) || (objmgr.IsReservedName(name)))
403    {
404        SendNotification(LANG_PET_INVALID_NAME);
405        return;
406    }
407    pet->SetName(name);
408
409    Unit *owner = pet->GetOwner();
410    if(owner && (owner->GetTypeId() == TYPEID_PLAYER) && ((Player*)owner)->GetGroup())
411        ((Player*)owner)->SetGroupUpdateFlag(GROUP_UPDATE_FLAG_PET_NAME);
412
413    pet->SetByteValue(UNIT_FIELD_BYTES_2, 2, UNIT_RENAME_NOT_ALLOWED);
414
415    if(isdeclined)
416    {
417        for(int i = 0; i < MAX_DECLINED_NAME_CASES; ++i)
418            recv_data >> declinedname.name[i];
419
420        std::wstring wname;
421        Utf8toWStr(name,wname);
422        if(!ObjectMgr::CheckDeclinedNames(GetMainPartOfName(wname,0),declinedname))
423        {
424            SendNotification(LANG_PET_INVALID_NAME);
425            return;
426        }
427    }
428
429    CharacterDatabase.BeginTransaction();
430    if(isdeclined)
431    {
432        for(int i = 0; i < MAX_DECLINED_NAME_CASES; ++i)
433            CharacterDatabase.escape_string(declinedname.name[i]);
434        CharacterDatabase.PExecute("DELETE FROM character_pet_declinedname WHERE owner = '%u' AND id = '%u'", _player->GetGUIDLow(), pet->GetCharmInfo()->GetPetNumber());
435        CharacterDatabase.PExecute("INSERT INTO character_pet_declinedname (id, owner, genitive, dative, accusative, instrumental, prepositional) VALUES ('%u','%u','%s','%s','%s','%s','%s')",
436            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());
437    }
438
439    CharacterDatabase.escape_string(name);
440    CharacterDatabase.PExecute("UPDATE character_pet SET name = '%s', renamed = '1' WHERE owner = '%u' AND id = '%u'", name.c_str(),_player->GetGUIDLow(),pet->GetCharmInfo()->GetPetNumber() );
441    CharacterDatabase.CommitTransaction();
442
443    pet->SetUInt32Value(UNIT_FIELD_PET_NAME_TIMESTAMP, time(NULL));
444}
445
446void WorldSession::HandlePetAbandon( WorldPacket & recv_data )
447{
448    CHECK_PACKET_SIZE(recv_data,8);
449
450    uint64 guid;
451    recv_data >> guid;                                      //pet guid
452    sLog.outDetail( "HandlePetAbandon. CMSG_PET_ABANDON pet guid is %u", GUID_LOPART(guid) );
453
454    // pet/charmed
455    Creature* pet=ObjectAccessor::GetCreatureOrPet(*_player, guid);
456    if(pet)
457    {
458        if(pet->isPet())
459        {
460            if(pet->GetGUID() == _player->GetPetGUID())
461            {
462                uint32 feelty = pet->GetPower(POWER_HAPPINESS);
463                pet->SetPower(POWER_HAPPINESS ,(feelty-50000) > 0 ?(feelty-50000) : 0);
464            }
465
466            _player->RemovePet((Pet*)pet,PET_SAVE_AS_DELETED);
467        }
468        else if(pet->GetGUID() == _player->GetCharmGUID())
469        {
470            _player->Uncharm();
471        }
472    }
473}
474
475void WorldSession::HandlePetUnlearnOpcode(WorldPacket& recvPacket)
476{
477    CHECK_PACKET_SIZE(recvPacket,8);
478
479    sLog.outDetail("CMSG_PET_UNLEARN");
480    uint64 guid;
481    recvPacket >> guid;
482
483    Pet* pet = _player->GetPet();
484
485    if(!pet || pet->getPetType() != HUNTER_PET || pet->m_spells.size() <= 1)
486        return;
487
488    if(guid != pet->GetGUID())
489    {
490        sLog.outError( "HandlePetUnlearnOpcode.Pet %u isn't pet of player %s .\n", uint32(GUID_LOPART(guid)),GetPlayer()->GetName() );
491        return;
492    }
493
494    CharmInfo *charmInfo = pet->GetCharmInfo();
495    if(!charmInfo)
496    {
497        sLog.outError("WorldSession::HandlePetUnlearnOpcode: object "I64FMTD" is considered pet-like but doesn't have a charminfo!", pet->GetGUID());
498        return;
499    }
500
501    uint32 cost = pet->resetTalentsCost();
502
503    if (GetPlayer()->GetMoney() < cost)
504    {
505        GetPlayer()->SendBuyError( BUY_ERR_NOT_ENOUGHT_MONEY, 0, 0, 0);
506        return;
507    }
508
509    for(PetSpellMap::iterator itr = pet->m_spells.begin(); itr != pet->m_spells.end();)
510    {
511        uint32 spell_id = itr->first;                       // Pet::removeSpell can invalidate iterator at erase NEW spell
512        ++itr;
513        pet->removeSpell(spell_id);
514    }
515
516    pet->SetTP(pet->getLevel() * (pet->GetLoyaltyLevel() - 1));
517
518    for(uint8 i = 0; i < 10; i++)
519    {
520        if(charmInfo->GetActionBarEntry(i)->SpellOrAction && charmInfo->GetActionBarEntry(i)->Type == ACT_ENABLED || charmInfo->GetActionBarEntry(i)->Type == ACT_DISABLED)
521            charmInfo->GetActionBarEntry(i)->SpellOrAction = 0;
522    }
523
524    // relearn pet passives
525    pet->LearnPetPassives();
526
527    pet->m_resetTalentsTime = time(NULL);
528    pet->m_resetTalentsCost = cost;
529    GetPlayer()->ModifyMoney(-(int32)cost);
530
531    GetPlayer()->PetSpellInitialize();
532}
533
534void WorldSession::HandlePetSpellAutocastOpcode( WorldPacket& recvPacket )
535{
536    CHECK_PACKET_SIZE(recvPacket,8+2+2+1);
537
538    sLog.outDetail("CMSG_PET_SPELL_AUTOCAST");
539    uint64 guid;
540    uint16 spellid;
541    uint16 spellid2;                                        //maybe second spell, automatically toggled off when first toggled on?
542    uint8  state;                                           //1 for on, 0 for off
543    recvPacket >> guid >> spellid >> spellid2 >> state;
544
545    if(!_player->GetPet() && !_player->GetCharm())
546        return;
547
548    if(ObjectAccessor::FindPlayer(guid))
549        return;
550
551    Creature* pet=ObjectAccessor::GetCreatureOrPet(*_player,guid);
552
553    if(!pet || (pet != _player->GetPet() && pet != _player->GetCharm()))
554    {
555        sLog.outError( "HandlePetSpellAutocastOpcode.Pet %u isn't pet of player %s .\n", uint32(GUID_LOPART(guid)),GetPlayer()->GetName() );
556        return;
557    }
558
559    // do not add not learned spells/ passive spells
560    if(!pet->HasSpell(spellid) || IsPassiveSpell(spellid))
561        return;
562
563    CharmInfo *charmInfo = pet->GetCharmInfo();
564    if(!charmInfo)
565    {
566        sLog.outError("WorldSession::HandlePetSpellAutocastOpcod: object "I64FMTD" is considered pet-like but doesn't have a charminfo!", pet->GetGUID());
567        return;
568    }
569
570    if(pet->isCharmed())
571                                                            //state can be used as boolean
572        pet->GetCharmInfo()->ToggleCreatureAutocast(spellid, state);
573    else
574        ((Pet*)pet)->ToggleAutocast(spellid, state);
575
576    for(uint8 i = 0; i < 10; ++i)
577    {
578        if((charmInfo->GetActionBarEntry(i)->Type == ACT_ENABLED || charmInfo->GetActionBarEntry(i)->Type == ACT_DISABLED) && spellid == charmInfo->GetActionBarEntry(i)->SpellOrAction)
579            charmInfo->GetActionBarEntry(i)->Type = state ? ACT_ENABLED : ACT_DISABLED;
580    }
581}
582
583void WorldSession::HandleAddDynamicTargetObsoleteOpcode( WorldPacket& recvPacket )
584{
585    sLog.outDetail("WORLD: CMSG_PET_CAST_SPELL");
586
587    CHECK_PACKET_SIZE(recvPacket,8+4);
588    uint64 guid;
589    uint32 spellid;
590
591    recvPacket >> guid >> spellid;
592
593    if(!_player->GetPet() && !_player->GetCharm())
594        return;
595
596    if(ObjectAccessor::FindPlayer(guid))
597        return;
598
599    Creature* pet=ObjectAccessor::GetCreatureOrPet(*_player,guid);
600
601    if(!pet || (pet != _player->GetPet() && pet!= _player->GetCharm()))
602    {
603        sLog.outError( "HandleAddDynamicTargetObsoleteOpcode.Pet %u isn't pet of player %s .\n", uint32(GUID_LOPART(guid)),GetPlayer()->GetName() );
604        return;
605    }
606
607    if (pet->GetGlobalCooldown() > 0)
608        return;
609
610    SpellEntry const *spellInfo = sSpellStore.LookupEntry(spellid);
611    if(!spellInfo)
612    {
613        sLog.outError("WORLD: unknown PET spell id %i\n", spellid);
614        return;
615    }
616
617    // do not cast not learned spells
618    if(!pet->HasSpell(spellid) || IsPassiveSpell(spellid))
619        return;
620
621    SpellCastTargets targets;
622    if(!targets.read(&recvPacket,pet))
623        return;
624
625    pet->clearUnitState(UNIT_STAT_FOLLOW);
626
627    Spell *spell = new Spell(pet, spellInfo, false);
628    spell->m_targets = targets;
629
630    int16 result = spell->PetCanCast(NULL);
631    if(result == -1)
632    {
633        pet->AddCreatureSpellCooldown(spellid);
634        if(pet->isPet())
635        {
636            Pet* p = (Pet*)pet;
637            p->CheckLearning(spellid);
638            //10% chance to play special pet attack talk, else growl
639            //actually this only seems to happen on special spells, fire shield for imp, torment for voidwalker, but it's stupid to check every spell
640            if(p->getPetType() == SUMMON_PET && (urand(0, 100) < 10))
641                pet->SendPetTalk((uint32)PET_TALK_SPECIAL_SPELL);
642            else
643                pet->SendPetAIReaction(guid);
644        }
645
646        spell->prepare(&(spell->m_targets));
647    }
648    else
649    {
650        pet->SendPetCastFail(spellid, result);
651        if(!pet->HasSpellCooldown(spellid))
652            pet->SendPetClearCooldown(spellid);
653
654        spell->finish(false);
655        delete spell;
656    }
657}
Note: See TracBrowser for help on using the browser.