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

Revision 152, 24.1 kB (checked in by yumileroy, 17 years ago)

[svn] Fix the compile error. Patch provided by Tidus.
Merge part of mangos svn rev 6748: Use SMSG_PET_NAME_INVALID opcode instead of db string. Source: Mangos.

Original author: megamage
Date: 2008-11-03 09:53:31-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->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);
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    CHECK_PACKET_SIZE(recv_data, recv_data.rpos() + 1);
394    recv_data >> isdeclined;
395
396    Pet* pet = ObjectAccessor::GetPet(petguid);
397                                                            // check it!
398    if( !pet || !pet->isPet() || ((Pet*)pet)->getPetType()!= HUNTER_PET ||
399        pet->GetByteValue(UNIT_FIELD_BYTES_2, 2) != UNIT_RENAME_ALLOWED ||
400        pet->GetOwnerGUID() != _player->GetGUID() || !pet->GetCharmInfo() )
401        return;
402
403    if(!ObjectMgr::IsValidPetName(name))
404    {
405        SendPetNameInvalid(PET_NAME_INVALID, name, NULL);
406        return;
407    }
408
409    if(objmgr.IsReservedName(name))
410    {
411        SendPetNameInvalid(PET_NAME_RESERVED, name, NULL);
412        return;
413    }
414
415    pet->SetName(name);
416
417    Unit *owner = pet->GetOwner();
418    if(owner && (owner->GetTypeId() == TYPEID_PLAYER) && ((Player*)owner)->GetGroup())
419        ((Player*)owner)->SetGroupUpdateFlag(GROUP_UPDATE_FLAG_PET_NAME);
420
421    pet->SetByteValue(UNIT_FIELD_BYTES_2, 2, UNIT_RENAME_NOT_ALLOWED);
422
423    if(isdeclined)
424    {
425        for(int i = 0; i < MAX_DECLINED_NAME_CASES; ++i)
426        {
427            CHECK_PACKET_SIZE(recv_data, recv_data.rpos() + 1);
428            recv_data >> declinedname.name[i];
429        }
430
431        std::wstring wname;
432        Utf8toWStr(name, wname);
433        if(!ObjectMgr::CheckDeclinedNames(GetMainPartOfName(wname,0),declinedname))
434        {
435            SendPetNameInvalid(PET_NAME_DECLENSION_DOESNT_MATCH_BASE_NAME, name, &declinedname);
436            return;
437        }
438    }
439
440    CharacterDatabase.BeginTransaction();
441    if(isdeclined)
442    {
443        for(int i = 0; i < MAX_DECLINED_NAME_CASES; ++i)
444            CharacterDatabase.escape_string(declinedname.name[i]);
445        CharacterDatabase.PExecute("DELETE FROM character_pet_declinedname WHERE owner = '%u' AND id = '%u'", _player->GetGUIDLow(), pet->GetCharmInfo()->GetPetNumber());
446        CharacterDatabase.PExecute("INSERT INTO character_pet_declinedname (id, owner, genitive, dative, accusative, instrumental, prepositional) VALUES ('%u','%u','%s','%s','%s','%s','%s')",
447            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());
448    }
449
450    CharacterDatabase.escape_string(name);
451    CharacterDatabase.PExecute("UPDATE character_pet SET name = '%s', renamed = '1' WHERE owner = '%u' AND id = '%u'", name.c_str(), _player->GetGUIDLow(), pet->GetCharmInfo()->GetPetNumber());
452    CharacterDatabase.CommitTransaction();
453
454    pet->SetUInt32Value(UNIT_FIELD_PET_NAME_TIMESTAMP, time(NULL));
455}
456
457void WorldSession::HandlePetAbandon( WorldPacket & recv_data )
458{
459    CHECK_PACKET_SIZE(recv_data, 8);
460
461    uint64 guid;
462    recv_data >> guid;                                      //pet guid
463    sLog.outDetail( "HandlePetAbandon. CMSG_PET_ABANDON pet guid is %u", GUID_LOPART(guid) );
464
465    // pet/charmed
466    Creature* pet = ObjectAccessor::GetCreatureOrPet(*_player, guid);
467    if(pet)
468    {
469        if(pet->isPet())
470        {
471            if(pet->GetGUID() == _player->GetPetGUID())
472            {
473                uint32 feelty = pet->GetPower(POWER_HAPPINESS);
474                pet->SetPower(POWER_HAPPINESS ,(feelty-50000) > 0 ?(feelty-50000) : 0);
475            }
476
477            _player->RemovePet((Pet*)pet,PET_SAVE_AS_DELETED);
478        }
479        else if(pet->GetGUID() == _player->GetCharmGUID())
480        {
481            _player->Uncharm();
482        }
483    }
484}
485
486void WorldSession::HandlePetUnlearnOpcode(WorldPacket& recvPacket)
487{
488    CHECK_PACKET_SIZE(recvPacket,8);
489
490    sLog.outDetail("CMSG_PET_UNLEARN");
491    uint64 guid;
492    recvPacket >> guid;
493
494    Pet* pet = _player->GetPet();
495
496    if(!pet || pet->getPetType() != HUNTER_PET || pet->m_spells.size() <= 1)
497        return;
498
499    if(guid != pet->GetGUID())
500    {
501        sLog.outError( "HandlePetUnlearnOpcode.Pet %u isn't pet of player %s .\n", uint32(GUID_LOPART(guid)),GetPlayer()->GetName() );
502        return;
503    }
504
505    CharmInfo *charmInfo = pet->GetCharmInfo();
506    if(!charmInfo)
507    {
508        sLog.outError("WorldSession::HandlePetUnlearnOpcode: object "I64FMTD" is considered pet-like but doesn't have a charminfo!", pet->GetGUID());
509        return;
510    }
511
512    uint32 cost = pet->resetTalentsCost();
513
514    if (GetPlayer()->GetMoney() < cost)
515    {
516        GetPlayer()->SendBuyError( BUY_ERR_NOT_ENOUGHT_MONEY, 0, 0, 0);
517        return;
518    }
519
520    for(PetSpellMap::iterator itr = pet->m_spells.begin(); itr != pet->m_spells.end();)
521    {
522        uint32 spell_id = itr->first;                       // Pet::removeSpell can invalidate iterator at erase NEW spell
523        ++itr;
524        pet->removeSpell(spell_id);
525    }
526
527    pet->SetTP(pet->getLevel() * (pet->GetLoyaltyLevel() - 1));
528
529    for(uint8 i = 0; i < 10; i++)
530    {
531        if(charmInfo->GetActionBarEntry(i)->SpellOrAction && charmInfo->GetActionBarEntry(i)->Type == ACT_ENABLED || charmInfo->GetActionBarEntry(i)->Type == ACT_DISABLED)
532            charmInfo->GetActionBarEntry(i)->SpellOrAction = 0;
533    }
534
535    // relearn pet passives
536    pet->LearnPetPassives();
537
538    pet->m_resetTalentsTime = time(NULL);
539    pet->m_resetTalentsCost = cost;
540    GetPlayer()->ModifyMoney(-(int32)cost);
541
542    GetPlayer()->PetSpellInitialize();
543}
544
545void WorldSession::HandlePetSpellAutocastOpcode( WorldPacket& recvPacket )
546{
547    CHECK_PACKET_SIZE(recvPacket,8+2+2+1);
548
549    sLog.outDetail("CMSG_PET_SPELL_AUTOCAST");
550    uint64 guid;
551    uint16 spellid;
552    uint16 spellid2;                                        //maybe second spell, automatically toggled off when first toggled on?
553    uint8  state;                                           //1 for on, 0 for off
554    recvPacket >> guid >> spellid >> spellid2 >> state;
555
556    if(!_player->GetPet() && !_player->GetCharm())
557        return;
558
559    if(ObjectAccessor::FindPlayer(guid))
560        return;
561
562    Creature* pet=ObjectAccessor::GetCreatureOrPet(*_player,guid);
563
564    if(!pet || (pet != _player->GetPet() && pet != _player->GetCharm()))
565    {
566        sLog.outError( "HandlePetSpellAutocastOpcode.Pet %u isn't pet of player %s .\n", uint32(GUID_LOPART(guid)),GetPlayer()->GetName() );
567        return;
568    }
569
570    // do not add not learned spells/ passive spells
571    if(!pet->HasSpell(spellid) || IsPassiveSpell(spellid))
572        return;
573
574    CharmInfo *charmInfo = pet->GetCharmInfo();
575    if(!charmInfo)
576    {
577        sLog.outError("WorldSession::HandlePetSpellAutocastOpcod: object "I64FMTD" is considered pet-like but doesn't have a charminfo!", pet->GetGUID());
578        return;
579    }
580
581    if(pet->isCharmed())
582                                                            //state can be used as boolean
583        pet->GetCharmInfo()->ToggleCreatureAutocast(spellid, state);
584    else
585        ((Pet*)pet)->ToggleAutocast(spellid, state);
586
587    for(uint8 i = 0; i < 10; ++i)
588    {
589        if((charmInfo->GetActionBarEntry(i)->Type == ACT_ENABLED || charmInfo->GetActionBarEntry(i)->Type == ACT_DISABLED) && spellid == charmInfo->GetActionBarEntry(i)->SpellOrAction)
590            charmInfo->GetActionBarEntry(i)->Type = state ? ACT_ENABLED : ACT_DISABLED;
591    }
592}
593
594void WorldSession::HandlePetCastSpellOpcode( WorldPacket& recvPacket )
595{
596    sLog.outDetail("WORLD: CMSG_PET_CAST_SPELL");
597
598    CHECK_PACKET_SIZE(recvPacket,8+4);
599    uint64 guid;
600    uint32 spellid;
601
602    recvPacket >> guid >> spellid;
603
604    if(!_player->GetPet() && !_player->GetCharm())
605        return;
606
607    if(ObjectAccessor::FindPlayer(guid))
608        return;
609
610    Creature* pet=ObjectAccessor::GetCreatureOrPet(*_player,guid);
611
612    if(!pet || (pet != _player->GetPet() && pet!= _player->GetCharm()))
613    {
614        sLog.outError( "HandlePetCastSpellOpcode: Pet %u isn't pet of player %s .\n", uint32(GUID_LOPART(guid)),GetPlayer()->GetName() );
615        return;
616    }
617
618    if (pet->GetGlobalCooldown() > 0)
619        return;
620
621    SpellEntry const *spellInfo = sSpellStore.LookupEntry(spellid);
622    if(!spellInfo)
623    {
624        sLog.outError("WORLD: unknown PET spell id %i\n", spellid);
625        return;
626    }
627
628    // do not cast not learned spells
629    if(!pet->HasSpell(spellid) || IsPassiveSpell(spellid))
630        return;
631
632    SpellCastTargets targets;
633    if(!targets.read(&recvPacket,pet))
634        return;
635
636    pet->clearUnitState(UNIT_STAT_FOLLOW);
637
638    Spell *spell = new Spell(pet, spellInfo, false);
639    spell->m_targets = targets;
640
641    int16 result = spell->PetCanCast(NULL);
642    if(result == -1)
643    {
644        pet->AddCreatureSpellCooldown(spellid);
645        if(pet->isPet())
646        {
647            Pet* p = (Pet*)pet;
648            p->CheckLearning(spellid);
649            //10% chance to play special pet attack talk, else growl
650            //actually this only seems to happen on special spells, fire shield for imp, torment for voidwalker, but it's stupid to check every spell
651            if(p->getPetType() == SUMMON_PET && (urand(0, 100) < 10))
652                pet->SendPetTalk((uint32)PET_TALK_SPECIAL_SPELL);
653            else
654                pet->SendPetAIReaction(guid);
655        }
656
657        spell->prepare(&(spell->m_targets));
658    }
659    else
660    {
661        pet->SendPetCastFail(spellid, result);
662        if(!pet->HasSpellCooldown(spellid))
663            pet->SendPetClearCooldown(spellid);
664
665        spell->finish(false);
666        delete spell;
667    }
668}
669
670void WorldSession::SendPetNameInvalid(uint32 error, std::string name, DeclinedName *declinedName)
671{
672    WorldPacket data(SMSG_PET_NAME_INVALID, 4 + name.size() + 1 + 1);
673    data << uint32(error);
674    data << name;
675    if(declinedName)
676    {
677        data << uint8(1);
678        for(uint32 i = 0; i < MAX_DECLINED_NAME_CASES; ++i)
679            data << declinedName->name[i];
680    }
681    else
682        data << uint8(0);
683    SendPacket(&data);
684}
Note: See TracBrowser for help on using the browser.