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

Revision 200, 24.9 kB (checked in by yumileroy, 17 years ago)

[svn] * Disabled the move and stay commands while pet is possessed
* Make pet come back to its owner after possession ends if it's not currently in combat
* Allow the possessed unit to properly change attack targets
* Also remove charm effects from charmed target on owner aura cancel

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