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

Revision 277, 25.3 kB (checked in by yumileroy, 17 years ago)

* Don't allow pets to attack in melee if owner is pacified. This applies for possessed "pets" as well.
* Use proper spell ID in unsummoning possessed units on channeling interrupt.
* Allow only once to init pet bar for CharmInfo?
* Export CharmInfo? to be available to scripts
* Allow to disable the melee attack command when initializing the pet action bar

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