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

Revision 191, 24.8 kB (checked in by yumileroy, 17 years ago)

[svn] Fixed pets stopping attack after pressing the attack button multiple times.

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