2010-10-07 15:35:36 +02:00
/*
2014-01-01 00:07:53 +01:00
* Copyright (C) 2008-2014 TrinityCore <http://www.trinitycore.org/>
2010-10-07 15:35:36 +02:00
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
2008-10-11 14:16:25 -05:00
*/
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
#include "Common.h"
#include "WorldPacket.h"
#include "WorldSession.h"
#include "ObjectMgr.h"
#include "SpellMgr.h"
#include "Log.h"
#include "Opcodes.h"
#include "Spell.h"
#include "ObjectAccessor.h"
#include "CreatureAI.h"
#include "Util.h"
#include "Pet.h"
2009-05-25 12:51:14 -05:00
#include "World.h"
2010-11-29 07:50:31 +01:00
#include "Group.h"
2011-07-26 23:09:28 +02:00
#include "SpellInfo.h"
2012-11-17 05:18:37 +01:00
#include "Player.h"
2009-10-17 15:51:44 -07:00
2012-11-09 13:13:45 +01:00
void WorldSession :: HandleDismissCritter ( WorldPacket & recvData )
2010-04-29 23:24:31 +02:00
{
uint64 guid ;
2012-11-02 22:23:14 +01:00
recvData >> guid ;
2010-04-29 23:24:31 +02:00
2013-11-08 10:50:51 +01:00
TC_LOG_DEBUG ( "network" , "WORLD: Received CMSG_DISMISS_CRITTER for GUID " UI64FMTD , guid );
2010-04-29 23:24:31 +02:00
Unit * pet = ObjectAccessor :: GetCreatureOrPetOrVehicle ( * _player , guid );
if ( ! pet )
{
2013-11-08 10:50:51 +01:00
TC_LOG_DEBUG ( "network" , "Vanitypet (guid: %u) does not exist - player '%s' (guid: %u / account: %u) attempted to dismiss it (possibly lagged out)" ,
2012-10-24 13:29:34 +02:00
uint32 ( GUID_LOPART ( guid )), GetPlayer () -> GetName (). c_str (), GetPlayer () -> GetGUIDLow (), GetAccountId ());
2010-04-29 23:24:31 +02:00
return ;
}
if ( _player -> GetCritterGUID () == pet -> GetGUID ())
{
2013-06-11 19:54:27 -02:30
if ( pet -> GetTypeId () == TYPEID_UNIT && pet -> ToCreature () -> IsSummon ())
2010-04-29 23:24:31 +02:00
pet -> ToTempSummon () -> UnSummon ();
}
}
2012-11-27 13:02:04 +01:00
void WorldSession :: HandlePetAction ( WorldPacket & recvData )
2008-10-11 14:16:25 -05:00
{
uint64 guid1 ;
2009-07-01 18:07:20 -05:00
uint32 data ;
2008-10-11 14:16:25 -05:00
uint64 guid2 ;
2012-11-02 22:23:14 +01:00
recvData >> guid1 ; //pet guid
recvData >> data ;
recvData >> guid2 ; //tag guid
2009-10-17 15:51:44 -07:00
2009-07-01 18:07:20 -05:00
uint32 spellid = UNIT_ACTION_BUTTON_ACTION ( data );
uint8 flag = UNIT_ACTION_BUTTON_TYPE ( data ); //delete = 0x07 CastSpell = C1
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
// used also for charmed creature
2008-12-12 11:21:28 -06:00
Unit * pet = ObjectAccessor :: GetUnit ( * _player , guid1 );
2013-11-08 10:50:51 +01:00
TC_LOG_INFO ( "network" , "HandlePetAction: Pet %u - flag: %u, spellid: %u, target: %u." , uint32 ( GUID_LOPART ( guid1 )), uint32 ( flag ), spellid , uint32 ( GUID_LOPART ( guid2 )));
2010-12-26 01:24:29 +01:00
2010-04-07 19:14:10 +02:00
if ( ! pet )
2008-10-11 14:16:25 -05:00
{
2013-11-08 10:50:51 +01:00
TC_LOG_ERROR ( "network" , "HandlePetAction: Pet (GUID: %u) doesn't exist for player %s (GUID: %u)" , uint32 ( GUID_LOPART ( guid1 )), GetPlayer () -> GetName (). c_str (), GUID_LOPART ( GetPlayer () -> GetGUID ()));
2008-10-11 14:16:25 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2010-04-07 19:14:10 +02:00
if ( pet != GetPlayer () -> GetFirstControlled ())
2008-10-11 14:16:25 -05:00
{
2013-11-08 10:50:51 +01:00
TC_LOG_ERROR ( "network" , "HandlePetAction: Pet (GUID: %u) does not belong to player %s (GUID: %u)" , uint32 ( GUID_LOPART ( guid1 )), GetPlayer () -> GetName (). c_str (), GUID_LOPART ( GetPlayer () -> GetGUID ()));
2008-10-11 14:16:25 -05:00
return ;
}
2010-04-07 18:09:10 +02:00
2013-06-11 19:54:27 -02:30
if ( ! pet -> IsAlive ())
2010-11-11 22:53:52 +01:00
{
2011-07-26 23:09:28 +02:00
SpellInfo const * spell = ( flag == ACT_ENABLED || flag == ACT_PASSIVE ) ? sSpellMgr -> GetSpellInfo ( spellid ) : NULL ;
2010-11-11 22:53:52 +01:00
if ( ! spell )
return ;
2010-12-19 05:15:47 +01:00
if ( ! ( spell -> Attributes & SPELL_ATTR0_CASTABLE_WHILE_DEAD ))
2010-11-11 22:53:52 +01:00
return ;
}
2009-10-17 15:51:44 -07:00
2013-03-08 21:41:30 +01:00
/// @todo allow control charmed player?
2010-04-07 19:14:10 +02:00
if ( pet -> GetTypeId () == TYPEID_PLAYER && ! ( flag == ACT_COMMAND && spellid == COMMAND_ATTACK ))
2008-10-11 14:16:25 -05:00
return ;
2009-10-17 15:51:44 -07:00
2010-04-07 19:14:10 +02:00
if ( GetPlayer () -> m_Controlled . size () == 1 )
2009-03-21 13:42:09 -06:00
HandlePetActionHelper ( pet , guid1 , spellid , flag , guid2 );
else
2009-03-21 12:16:14 -06:00
{
2009-03-21 13:42:09 -06:00
//If a pet is dismissed, m_Controlled will change
std :: vector < Unit *> controlled ;
2009-10-17 16:20:24 -07:00
for ( Unit :: ControlList :: iterator itr = GetPlayer () -> m_Controlled . begin (); itr != GetPlayer () -> m_Controlled . end (); ++ itr )
2013-06-11 19:54:27 -02:30
if (( * itr ) -> GetEntry () == pet -> GetEntry () && ( * itr ) -> IsAlive ())
2009-03-21 13:42:09 -06:00
controlled . push_back ( * itr );
2009-10-17 16:20:24 -07:00
for ( std :: vector < Unit *>:: iterator itr = controlled . begin (); itr != controlled . end (); ++ itr )
2009-03-21 13:42:09 -06:00
HandlePetActionHelper ( * itr , guid1 , spellid , flag , guid2 );
2009-03-21 12:16:14 -06:00
}
2009-03-21 11:15:55 -06:00
}
2009-10-17 15:51:44 -07:00
2012-11-02 22:23:14 +01:00
void WorldSession :: HandlePetStopAttack ( WorldPacket & recvData )
2010-10-22 01:10:39 +02:00
{
uint64 guid ;
2012-11-02 22:23:14 +01:00
recvData >> guid ;
2010-10-22 01:10:39 +02:00
2013-11-08 10:50:51 +01:00
TC_LOG_DEBUG ( "network" , "WORLD: Received CMSG_PET_STOP_ATTACK for GUID " UI64FMTD "" , guid );
2010-10-22 01:10:39 +02:00
Unit * pet = ObjectAccessor :: GetCreatureOrPetOrVehicle ( * _player , guid );
if ( ! pet )
{
2013-11-08 10:50:51 +01:00
TC_LOG_ERROR ( "network" , "HandlePetStopAttack: Pet %u does not exist" , uint32 ( GUID_LOPART ( guid )));
2010-10-22 01:10:39 +02:00
return ;
}
if ( pet != GetPlayer () -> GetPet () && pet != GetPlayer () -> GetCharm ())
{
2013-11-08 10:50:51 +01:00
TC_LOG_ERROR ( "network" , "HandlePetStopAttack: Pet GUID %u isn't a pet or charmed creature of player %s" ,
2012-10-24 13:29:34 +02:00
uint32 ( GUID_LOPART ( guid )), GetPlayer () -> GetName (). c_str ());
2010-10-22 01:10:39 +02:00
return ;
}
2013-06-11 19:54:27 -02:30
if ( ! pet -> IsAlive ())
2010-10-22 01:10:39 +02:00
return ;
pet -> AttackStop ();
}
2012-11-09 13:13:45 +01:00
void WorldSession :: HandlePetActionHelper ( Unit * pet , uint64 guid1 , uint32 spellid , uint16 flag , uint64 guid2 )
2009-03-21 11:15:55 -06:00
{
2011-09-15 14:08:17 +02:00
CharmInfo * charmInfo = pet -> GetCharmInfo ();
2010-04-07 19:14:10 +02:00
if ( ! charmInfo )
2008-10-11 14:16:25 -05:00
{
2013-11-24 22:48:47 +01:00
TC_LOG_ERROR ( "network" , "WorldSession::HandlePetAction(petGuid: " UI64FMTD ", tagGuid: " UI64FMTD ", spellId: %u, flag: %u): object (GUID: %u Entry: %u TypeId: %u) is considered pet-like but doesn't have a charminfo!" ,
guid1 , guid2 , spellid , flag , pet -> GetGUIDLow (), pet -> GetEntry (), pet -> GetTypeId ());
2008-10-11 14:16:25 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2011-09-29 12:43:05 +02:00
switch ( flag )
2008-10-11 14:16:25 -05:00
{
2009-07-01 18:07:20 -05:00
case ACT_COMMAND : //0x07
2011-09-29 12:43:05 +02:00
switch ( spellid )
2008-10-11 14:16:25 -05:00
{
case COMMAND_STAY : //flat=1792 //STAY
2012-02-21 11:12:10 +01:00
pet -> StopMoving ();
2012-01-13 16:43:49 +01:00
pet -> GetMotionMaster () -> Clear ( false );
2008-10-11 14:16:25 -05:00
pet -> GetMotionMaster () -> MoveIdle ();
2010-04-07 22:59:46 +02:00
charmInfo -> SetCommandState ( COMMAND_STAY );
2009-10-17 15:51:44 -07:00
2009-09-10 09:59:16 -07:00
charmInfo -> SetIsCommandAttack ( false );
charmInfo -> SetIsAtStay ( true );
2012-12-06 09:58:32 -05:00
charmInfo -> SetIsCommandFollow ( false );
2009-09-10 09:59:16 -07:00
charmInfo -> SetIsFollowing ( false );
charmInfo -> SetIsReturning ( false );
charmInfo -> SaveStayPosition ();
2008-10-11 14:16:25 -05:00
break ;
case COMMAND_FOLLOW : //spellid=1792 //FOLLOW
pet -> AttackStop ();
2009-03-12 14:15:44 -06:00
pet -> InterruptNonMeleeSpells ( false );
2011-04-29 20:47:02 +02:00
pet -> GetMotionMaster () -> MoveFollow ( _player , PET_FOLLOW_DIST , pet -> GetFollowAngle ());
2010-04-07 22:59:46 +02:00
charmInfo -> SetCommandState ( COMMAND_FOLLOW );
2009-10-17 15:51:44 -07:00
2009-09-10 09:59:16 -07:00
charmInfo -> SetIsCommandAttack ( false );
charmInfo -> SetIsAtStay ( false );
charmInfo -> SetIsReturning ( true );
2012-12-06 09:58:32 -05:00
charmInfo -> SetIsCommandFollow ( true );
2009-09-10 09:59:16 -07:00
charmInfo -> SetIsFollowing ( false );
2008-10-11 14:16:25 -05:00
break ;
case COMMAND_ATTACK : //spellid=1792 //ATTACK
{
2008-11-20 20:27:04 -05:00
// Can't attack if owner is pacified
if ( _player -> HasAuraType ( SPELL_AURA_MOD_PACIFY ))
{
//pet->SendPetCastFail(spellid, SPELL_FAILED_PACIFIED);
2013-03-08 21:41:30 +01:00
/// @todo Send proper error message to client
2008-11-20 20:27:04 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
// only place where pet can be player
2011-09-15 14:08:17 +02:00
Unit * TargetUnit = ObjectAccessor :: GetUnit ( * _player , guid2 );
2010-04-07 19:14:10 +02:00
if ( ! TargetUnit )
2008-10-11 14:16:25 -05:00
return ;
2009-10-17 15:51:44 -07:00
2011-09-15 14:08:17 +02:00
if ( Unit * owner = pet -> GetOwner ())
2011-09-16 23:49:31 +02:00
if ( ! owner -> IsValidAttackTarget ( TargetUnit ))
2010-08-23 14:46:10 -07:00
return ;
2009-10-17 15:51:44 -07:00
2012-02-03 19:02:17 +02:00
pet -> ClearUnitState ( UNIT_STATE_FOLLOW );
2009-01-21 16:18:57 -06:00
// This is true if pet has no target or has target but targets differs.
2013-06-11 19:54:27 -02:30
if ( pet -> GetVictim () != TargetUnit || ( pet -> GetVictim () == TargetUnit && ! pet -> GetCharmInfo () -> IsCommandAttack ()))
2008-10-11 14:16:25 -05:00
{
2013-06-11 19:54:27 -02:30
if ( pet -> GetVictim ())
2009-01-21 16:18:57 -06:00
pet -> AttackStop ();
2009-10-17 15:51:44 -07:00
2010-04-07 19:14:10 +02:00
if ( pet -> GetTypeId () != TYPEID_PLAYER && pet -> ToCreature () -> IsAIEnabled )
2008-10-11 14:16:25 -05:00
{
2009-09-10 09:59:16 -07:00
charmInfo -> SetIsCommandAttack ( true );
charmInfo -> SetIsAtStay ( false );
charmInfo -> SetIsFollowing ( false );
2012-12-06 09:58:32 -05:00
charmInfo -> SetIsCommandFollow ( false );
2009-09-10 09:59:16 -07:00
charmInfo -> SetIsReturning ( false );
2009-10-17 15:51:44 -07:00
2010-03-07 18:30:53 +01:00
pet -> ToCreature () -> AI () -> AttackStart ( TargetUnit );
2009-10-17 15:51:44 -07:00
2009-01-21 16:18:57 -06:00
//10% chance to play special pet attack talk, else growl
2013-06-11 18:39:51 -02:30
if ( pet -> ToCreature () -> IsPet () && (( Pet * ) pet ) -> getPetType () == SUMMON_PET && pet != TargetUnit && urand ( 0 , 100 ) < 10 )
2009-01-21 16:18:57 -06:00
pet -> SendPetTalk (( uint32 ) PET_TALK_ATTACK );
else
{
// 90% chance for pet and 100% chance for charmed creature
pet -> SendPetAIReaction ( guid1 );
}
2008-10-11 14:16:25 -05:00
}
2009-07-01 18:07:20 -05:00
else // charmed player
2009-01-21 16:18:57 -06:00
{
2013-06-11 19:54:27 -02:30
if ( pet -> GetVictim () && pet -> GetVictim () != TargetUnit )
2009-01-21 16:18:57 -06:00
pet -> AttackStop ();
2009-10-17 15:51:44 -07:00
2009-09-10 09:59:16 -07:00
charmInfo -> SetIsCommandAttack ( true );
charmInfo -> SetIsAtStay ( false );
charmInfo -> SetIsFollowing ( false );
2012-12-06 09:58:32 -05:00
charmInfo -> SetIsCommandFollow ( false );
2009-09-10 09:59:16 -07:00
charmInfo -> SetIsReturning ( false );
2009-10-17 15:51:44 -07:00
2011-04-29 20:47:02 +02:00
pet -> Attack ( TargetUnit , true );
2009-01-21 16:18:57 -06:00
pet -> SendPetAIReaction ( guid1 );
}
2008-10-11 14:16:25 -05:00
}
break ;
}
case COMMAND_ABANDON : // abandon (hunter pet) or dismiss (summoned pet)
2010-04-07 19:14:10 +02:00
if ( pet -> GetCharmerGUID () == GetPlayer () -> GetGUID ())
2009-05-09 21:59:01 -05:00
_player -> StopCastingCharm ();
2010-04-07 19:14:10 +02:00
else if ( pet -> GetOwnerGUID () == GetPlayer () -> GetGUID ())
2009-05-06 10:13:33 -05:00
{
2010-07-30 20:04:28 +02:00
ASSERT ( pet -> GetTypeId () == TYPEID_UNIT );
2013-06-11 18:39:51 -02:30
if ( pet -> IsPet ())
2009-05-06 10:13:33 -05:00
{
2010-04-07 19:14:10 +02:00
if ((( Pet * ) pet ) -> getPetType () == HUNTER_PET )
2009-05-06 10:13:33 -05:00
GetPlayer () -> RemovePet (( Pet * ) pet , PET_SAVE_AS_DELETED );
else
//dismissing a summoned pet is like killing them (this prevents returning a soulshard...)
pet -> setDeathState ( CORPSE );
}
2010-04-07 19:14:10 +02:00
else if ( pet -> HasUnitTypeMask ( UNIT_MASK_MINION ))
2009-05-06 10:13:33 -05:00
{
(( Minion * ) pet ) -> UnSummon ();
}
2008-10-11 14:16:25 -05:00
}
break ;
default :
2013-11-08 10:50:51 +01:00
TC_LOG_ERROR ( "network" , "WORLD: unknown PET flag Action %i and spellid %i." , uint32 ( flag ), spellid );
2008-10-11 14:16:25 -05:00
}
break ;
2009-07-01 18:07:20 -05:00
case ACT_REACTION : // 0x6
2011-09-29 12:43:05 +02:00
switch ( spellid )
2008-10-11 14:16:25 -05:00
{
case REACT_PASSIVE : //passive
2009-09-10 09:59:16 -07:00
pet -> AttackStop ();
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
case REACT_DEFENSIVE : //recovery
case REACT_AGGRESSIVE : //activete
2010-04-07 19:14:10 +02:00
if ( pet -> GetTypeId () == TYPEID_UNIT )
2010-04-07 22:59:46 +02:00
pet -> ToCreature () -> SetReactState ( ReactStates ( spellid ));
2008-10-11 14:16:25 -05:00
break ;
}
break ;
2009-07-01 18:07:20 -05:00
case ACT_DISABLED : // 0x81 spell (disabled), ignore
case ACT_PASSIVE : // 0x01
case ACT_ENABLED : // 0xC1 spell
2008-10-11 14:16:25 -05:00
{
2009-01-21 16:18:57 -06:00
Unit * unit_target = NULL ;
2009-10-17 15:51:44 -07:00
2010-04-07 19:14:10 +02:00
if ( guid2 )
2011-04-29 20:47:02 +02:00
unit_target = ObjectAccessor :: GetUnit ( * _player , guid2 );
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
// do not cast unknown spells
2011-09-15 14:08:17 +02:00
SpellInfo const * spellInfo = sSpellMgr -> GetSpellInfo ( spellid );
2010-04-07 19:14:10 +02:00
if ( ! spellInfo )
2008-10-11 14:16:25 -05:00
{
2013-11-08 10:50:51 +01:00
TC_LOG_ERROR ( "network" , "WORLD: unknown PET spell id %i" , spellid );
2008-10-11 14:16:25 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2010-09-28 08:21:51 +03:00
for ( uint32 i = 0 ; i < MAX_SPELL_EFFECTS ; ++ i )
2008-10-11 14:16:25 -05:00
{
2011-08-16 19:44:18 +02:00
if ( spellInfo -> Effects [ i ]. TargetA . GetTarget () == TARGET_UNIT_SRC_AREA_ENEMY || spellInfo -> Effects [ i ]. TargetA . GetTarget () == TARGET_UNIT_DEST_AREA_ENEMY || spellInfo -> Effects [ i ]. TargetA . GetTarget () == TARGET_DEST_DYNOBJ_ENEMY )
2008-10-11 14:16:25 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
// do not cast not learned spells
2011-07-26 23:09:28 +02:00
if ( ! pet -> HasSpell ( spellid ) || spellInfo -> IsPassive ())
2008-10-11 14:16:25 -05:00
return ;
2009-10-17 15:51:44 -07:00
2009-09-10 09:59:16 -07:00
// Clear the flags as if owner clicked 'attack'. AI will reset them
// after AttackStart, even if spell failed
if ( pet -> GetCharmInfo ())
{
pet -> GetCharmInfo () -> SetIsAtStay ( false );
pet -> GetCharmInfo () -> SetIsCommandAttack ( true );
pet -> GetCharmInfo () -> SetIsReturning ( false );
pet -> GetCharmInfo () -> SetIsFollowing ( false );
}
2009-10-17 15:51:44 -07:00
2011-07-31 19:03:06 +02:00
Spell * spell = new Spell ( pet , spellInfo , TRIGGERED_NONE );
2009-10-17 15:51:44 -07:00
2009-03-16 12:00:14 -06:00
SpellCastResult result = spell -> CheckPetCast ( unit_target );
2009-10-17 15:51:44 -07:00
2009-01-21 16:18:57 -06:00
//auto turn to target unless possessed
2010-04-07 19:14:10 +02:00
if ( result == SPELL_FAILED_UNIT_NOT_INFRONT && ! pet -> isPossessed () && ! pet -> IsVehicle ())
2008-10-11 14:16:25 -05:00
{
2010-04-07 19:14:10 +02:00
if ( unit_target )
2009-04-27 18:42:35 -05:00
{
pet -> SetInFront ( unit_target );
2013-07-20 21:52:05 +02:00
if ( Player * player = unit_target -> ToPlayer ())
pet -> SendUpdateToPlayer ( player );
2009-04-27 18:42:35 -05:00
}
2011-09-15 14:08:17 +02:00
else if ( Unit * unit_target2 = spell -> m_targets . GetUnitTarget ())
2009-04-27 18:42:35 -05:00
{
pet -> SetInFront ( unit_target2 );
2013-07-20 21:52:05 +02:00
if ( Player * player = unit_target2 -> ToPlayer ())
pet -> SendUpdateToPlayer ( player );
2009-04-27 18:42:35 -05:00
}
2012-06-03 05:41:30 +02:00
2009-04-27 18:42:35 -05:00
if ( Unit * powner = pet -> GetCharmerOrOwner ())
2013-07-20 21:52:05 +02:00
if ( Player * player = powner -> ToPlayer ())
pet -> SendUpdateToPlayer ( player );
2012-06-03 05:41:30 +02:00
2009-03-16 12:00:14 -06:00
result = SPELL_CAST_OK ;
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2010-04-07 19:14:10 +02:00
if ( result == SPELL_CAST_OK )
2008-10-11 14:16:25 -05:00
{
2010-03-07 18:30:53 +01:00
pet -> ToCreature () -> AddCreatureSpellCooldown ( spellid );
2009-10-17 15:51:44 -07:00
2011-06-23 13:28:52 +02:00
unit_target = spell -> m_targets . GetUnitTarget ();
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
//10% chance to play special pet attack talk, else growl
//actually this only seems to happen on special spells, fire shield for imp, torment for voidwalker, but it's stupid to check every spell
2013-06-11 18:39:51 -02:30
if ( pet -> ToCreature () -> IsPet () && ((( Pet * ) pet ) -> getPetType () == SUMMON_PET ) && ( pet != unit_target ) && ( urand ( 0 , 100 ) < 10 ))
2008-10-11 14:16:25 -05:00
pet -> SendPetTalk (( uint32 ) PET_TALK_SPECIAL_SPELL );
else
{
pet -> SendPetAIReaction ( guid1 );
}
2009-10-17 15:51:44 -07:00
2010-04-07 22:59:46 +02:00
if ( unit_target && ! GetPlayer () -> IsFriendlyTo ( unit_target ) && ! pet -> isPossessed () && ! pet -> IsVehicle ())
2008-10-11 14:16:25 -05:00
{
2009-01-21 16:18:57 -06:00
// This is true if pet has no target or has target but targets differs.
2013-06-11 19:54:27 -02:30
if ( pet -> GetVictim () != unit_target )
2009-01-21 16:18:57 -06:00
{
2013-06-11 19:54:27 -02:30
if ( pet -> GetVictim ())
2009-01-21 16:18:57 -06:00
pet -> AttackStop ();
pet -> GetMotionMaster () -> Clear ();
2010-03-07 18:30:53 +01:00
if ( pet -> ToCreature () -> IsAIEnabled )
pet -> ToCreature () -> AI () -> AttackStart ( unit_target );
2009-01-21 16:18:57 -06:00
}
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
spell -> prepare ( & ( spell -> m_targets ));
}
else
{
2013-08-14 15:45:36 +02:00
if ( pet -> isPossessed () || pet -> IsVehicle ()) /// @todo: confirm this check
2011-04-29 20:47:02 +02:00
Spell :: SendCastResult ( GetPlayer (), spellInfo , 0 , result );
2008-10-11 14:16:25 -05:00
else
2013-08-14 15:45:36 +02:00
spell -> SendPetCastResult ( result );
2009-10-17 15:51:44 -07:00
2010-04-07 19:14:10 +02:00
if ( ! pet -> ToCreature () -> HasSpellCooldown ( spellid ))
2009-06-23 14:05:37 +02:00
GetPlayer () -> SendClearCooldown ( spellid , pet );
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
spell -> finish ( false );
delete spell ;
2009-10-17 15:51:44 -07:00
2009-09-10 09:59:16 -07:00
// reset specific flags in case of spell fail. AI will reset other flags
if ( pet -> GetCharmInfo ())
pet -> GetCharmInfo () -> SetIsCommandAttack ( false );
2008-10-11 14:16:25 -05:00
}
break ;
}
default :
2013-11-08 10:50:51 +01:00
TC_LOG_ERROR ( "network" , "WORLD: unknown PET flag Action %i and spellid %i." , uint32 ( flag ), spellid );
2008-10-11 14:16:25 -05:00
}
}
2009-10-17 15:51:44 -07:00
2012-11-27 13:02:04 +01:00
void WorldSession :: HandlePetNameQuery ( WorldPacket & recvData )
2008-10-11 14:16:25 -05:00
{
2013-11-08 10:50:51 +01:00
TC_LOG_INFO ( "network" , "HandlePetNameQuery. CMSG_PET_NAME_QUERY" );
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
uint32 petnumber ;
uint64 petguid ;
2009-10-17 15:51:44 -07:00
2012-11-02 22:23:14 +01:00
recvData >> petnumber ;
recvData >> petguid ;
2009-10-17 15:51:44 -07:00
2011-04-29 20:47:02 +02:00
SendPetNameQuery ( petguid , petnumber );
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2010-04-07 22:59:46 +02:00
void WorldSession :: SendPetNameQuery ( uint64 petguid , uint32 petnumber )
2008-10-11 14:16:25 -05:00
{
2009-01-24 11:28:00 -06:00
Creature * pet = ObjectAccessor :: GetCreatureOrPetOrVehicle ( * _player , petguid );
2010-04-07 19:14:10 +02:00
if ( ! pet )
2010-01-30 13:54:34 +01:00
{
2010-11-26 01:35:32 +01:00
WorldPacket data ( SMSG_PET_NAME_QUERY_RESPONSE , ( 4 + 1 + 4 + 1 ));
2010-01-30 13:54:34 +01:00
data << uint32 ( petnumber );
2010-11-26 01:35:32 +01:00
data << uint8 ( 0 );
2010-01-30 13:54:34 +01:00
data << uint32 ( 0 );
data << uint8 ( 0 );
_player -> GetSession () -> SendPacket ( & data );
2008-10-11 14:16:25 -05:00
return ;
2010-01-30 13:54:34 +01:00
}
2009-10-17 15:51:44 -07:00
2012-10-24 13:29:34 +02:00
WorldPacket data ( SMSG_PET_NAME_QUERY_RESPONSE , ( 4 + 4 + pet -> GetName (). size () + 1 ));
2008-10-11 14:16:25 -05:00
data << uint32 ( petnumber );
2012-10-24 13:29:34 +02:00
data << pet -> GetName ();
2008-10-11 14:16:25 -05:00
data << uint32 ( pet -> GetUInt32Value ( UNIT_FIELD_PET_NAME_TIMESTAMP ));
2009-10-17 15:51:44 -07:00
2013-06-11 18:39:51 -02:30
if ( pet -> IsPet () && (( Pet * ) pet ) -> GetDeclinedNames ())
2008-10-11 14:16:25 -05:00
{
data << uint8 ( 1 );
2009-10-17 16:20:24 -07:00
for ( uint8 i = 0 ; i < MAX_DECLINED_NAME_CASES ; ++ i )
2008-10-11 14:16:25 -05:00
data << (( Pet * ) pet ) -> GetDeclinedNames () -> name [ i ];
}
else
data << uint8 ( 0 );
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
_player -> GetSession () -> SendPacket ( & data );
}
2009-10-17 15:51:44 -07:00
2010-07-22 03:12:28 +02:00
bool WorldSession :: CheckStableMaster ( uint64 guid )
{
// spell case or GM
if ( guid == GetPlayer () -> GetGUID ())
{
2013-06-11 21:25:12 -02:30
if ( ! GetPlayer () -> IsGameMaster () && ! GetPlayer () -> HasAuraType ( SPELL_AURA_OPEN_STABLE ))
2010-09-12 01:40:27 +02:00
{
2013-11-08 10:50:51 +01:00
TC_LOG_DEBUG ( "network" , "Player (GUID:%u) attempt open stable in cheating way." , GUID_LOPART ( guid ));
2010-07-22 03:12:28 +02:00
return false ;
}
}
// stable master case
else
{
if ( ! GetPlayer () -> GetNPCIfCanInteractWith ( guid , UNIT_NPC_FLAG_STABLEMASTER ))
{
2013-11-08 10:50:51 +01:00
TC_LOG_DEBUG ( "network" , "Stablemaster (GUID:%u) not found or you can't interact with him." , GUID_LOPART ( guid ));
2010-07-22 03:12:28 +02:00
return false ;
}
}
return true ;
}
2012-11-27 13:02:04 +01:00
void WorldSession :: HandlePetSetAction ( WorldPacket & recvData )
2008-10-11 14:16:25 -05:00
{
2013-11-08 10:50:51 +01:00
TC_LOG_INFO ( "network" , "HandlePetSetAction. CMSG_PET_SET_ACTION" );
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
uint64 petguid ;
uint8 count ;
2009-10-17 15:51:44 -07:00
2012-11-02 22:23:14 +01:00
recvData >> petguid ;
2009-10-17 15:51:44 -07:00
2009-03-21 10:17:24 -06:00
Unit * pet = ObjectAccessor :: GetUnit ( * _player , petguid );
2009-10-17 15:51:44 -07:00
2010-04-07 19:14:10 +02:00
if ( ! pet || pet != _player -> GetFirstControlled ())
2008-10-11 14:16:25 -05:00
{
2013-11-08 10:50:51 +01:00
TC_LOG_ERROR ( "network" , "HandlePetSetAction: Unknown pet (GUID: %u) or pet owner (GUID: %u)" , GUID_LOPART ( petguid ), _player -> GetGUIDLow ());
2008-10-11 14:16:25 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2011-09-15 14:08:17 +02:00
CharmInfo * charmInfo = pet -> GetCharmInfo ();
2010-04-07 19:14:10 +02:00
if ( ! charmInfo )
2008-10-11 14:16:25 -05:00
{
2013-11-08 10:50:51 +01:00
TC_LOG_ERROR ( "network" , "WorldSession::HandlePetSetAction: object (GUID: %u TypeId: %u) is considered pet-like but doesn't have a charminfo!" , pet -> GetGUIDLow (), pet -> GetTypeId ());
2008-10-11 14:16:25 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2012-11-02 22:23:14 +01:00
count = ( recvData . size () == 24 ) ? 2 : 1 ;
2009-10-17 15:51:44 -07:00
2009-12-17 18:21:06 +01:00
uint32 position [ 2 ];
uint32 data [ 2 ];
bool move_command = false ;
2009-12-18 13:41:30 -08:00
2010-04-07 19:12:44 +02:00
for ( uint8 i = 0 ; i < count ; ++ i )
2009-12-17 18:21:06 +01:00
{
2012-11-02 22:23:14 +01:00
recvData >> position [ i ];
recvData >> data [ i ];
2009-12-18 13:41:30 -08:00
2009-12-17 18:21:06 +01:00
uint8 act_state = UNIT_ACTION_BUTTON_TYPE ( data [ i ]);
2009-12-18 13:41:30 -08:00
2009-05-27 17:02:39 -05:00
//ignore invalid position
2010-04-07 19:14:10 +02:00
if ( position [ i ] >= MAX_UNIT_ACTION_BAR_INDEX )
2009-12-17 18:21:06 +01:00
return ;
2009-12-18 13:41:30 -08:00
2009-12-17 18:21:06 +01:00
// in the normal case, command and reaction buttons can only be moved, not removed
2010-04-07 23:56:35 +02:00
// at moving count == 2, at removing count == 1
2009-12-17 18:21:06 +01:00
// ignore attempt to remove command|reaction buttons (not possible at normal case)
if ( act_state == ACT_COMMAND || act_state == ACT_REACTION )
{
if ( count == 1 )
return ;
2009-12-18 13:41:30 -08:00
2009-12-17 18:21:06 +01:00
move_command = true ;
}
}
2009-12-18 13:41:30 -08:00
2009-12-17 18:25:50 +01:00
// check swap (at command->spell swap client remove spell first in another packet, so check only command move correctness)
2009-12-17 18:21:06 +01:00
if ( move_command )
{
uint8 act_state_0 = UNIT_ACTION_BUTTON_TYPE ( data [ 0 ]);
2010-04-07 19:14:10 +02:00
if ( act_state_0 == ACT_COMMAND || act_state_0 == ACT_REACTION )
2009-12-17 18:25:50 +01:00
{
uint32 spell_id_0 = UNIT_ACTION_BUTTON_ACTION ( data [ 0 ]);
UnitActionBarEntry const * actionEntry_1 = charmInfo -> GetActionBarEntry ( position [ 1 ]);
if ( ! actionEntry_1 || spell_id_0 != actionEntry_1 -> GetAction () ||
act_state_0 != actionEntry_1 -> GetType ())
return ;
}
2009-12-18 13:41:30 -08:00
2009-12-17 18:21:06 +01:00
uint8 act_state_1 = UNIT_ACTION_BUTTON_TYPE ( data [ 1 ]);
2010-04-07 19:14:10 +02:00
if ( act_state_1 == ACT_COMMAND || act_state_1 == ACT_REACTION )
2009-12-17 18:25:50 +01:00
{
uint32 spell_id_1 = UNIT_ACTION_BUTTON_ACTION ( data [ 1 ]);
UnitActionBarEntry const * actionEntry_0 = charmInfo -> GetActionBarEntry ( position [ 0 ]);
if ( ! actionEntry_0 || spell_id_1 != actionEntry_0 -> GetAction () ||
act_state_1 != actionEntry_0 -> GetType ())
return ;
}
2009-12-17 18:21:06 +01:00
}
2009-12-18 13:41:30 -08:00
2010-04-07 19:12:44 +02:00
for ( uint8 i = 0 ; i < count ; ++ i )
2009-12-17 18:21:06 +01:00
{
uint32 spell_id = UNIT_ACTION_BUTTON_ACTION ( data [ i ]);
uint8 act_state = UNIT_ACTION_BUTTON_TYPE ( data [ i ]);
2009-12-18 13:41:30 -08:00
2013-11-08 10:50:51 +01:00
TC_LOG_INFO ( "network" , "Player %s has changed pet spell action. Position: %u, Spell: %u, State: 0x%X" ,
2012-10-24 13:29:34 +02:00
_player -> GetName (). c_str (), position [ i ], spell_id , uint32 ( act_state ));
2009-10-17 15:51:44 -07:00
2009-05-27 17:02:39 -05:00
//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
2010-04-07 19:14:10 +02:00
if ( ! (( act_state == ACT_ENABLED || act_state == ACT_DISABLED || act_state == ACT_PASSIVE ) && spell_id && ! pet -> HasSpell ( spell_id )))
2008-10-11 14:16:25 -05:00
{
2011-07-26 23:09:28 +02:00
if ( SpellInfo const * spellInfo = sSpellMgr -> GetSpellInfo ( spell_id ))
2008-10-11 14:16:25 -05:00
{
2011-07-26 23:09:28 +02:00
//sign for autocast
if ( act_state == ACT_ENABLED )
{
2013-06-11 18:39:51 -02:30
if ( pet -> GetTypeId () == TYPEID_UNIT && pet -> ToCreature () -> IsPet ())
2011-07-26 23:09:28 +02:00
(( Pet * ) pet ) -> ToggleAutocast ( spellInfo , true );
else
for ( Unit :: ControlList :: iterator itr = GetPlayer () -> m_Controlled . begin (); itr != GetPlayer () -> m_Controlled . end (); ++ itr )
if (( * itr ) -> GetEntry () == pet -> GetEntry ())
( * itr ) -> GetCharmInfo () -> ToggleCreatureAutocast ( spellInfo , true );
}
//sign for no/turn off autocast
else if ( act_state == ACT_DISABLED )
{
2013-06-11 18:39:51 -02:30
if ( pet -> GetTypeId () == TYPEID_UNIT && pet -> ToCreature () -> IsPet ())
2011-07-26 23:09:28 +02:00
(( Pet * ) pet ) -> ToggleAutocast ( spellInfo , false );
else
for ( Unit :: ControlList :: iterator itr = GetPlayer () -> m_Controlled . begin (); itr != GetPlayer () -> m_Controlled . end (); ++ itr )
if (( * itr ) -> GetEntry () == pet -> GetEntry ())
( * itr ) -> GetCharmInfo () -> ToggleCreatureAutocast ( spellInfo , false );
}
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2011-04-29 20:47:02 +02:00
charmInfo -> SetActionBar ( position [ i ], spell_id , ActiveStates ( act_state ));
2008-10-11 14:16:25 -05:00
}
}
}
2009-10-17 15:51:44 -07:00
2012-11-27 13:02:04 +01:00
void WorldSession :: HandlePetRename ( WorldPacket & recvData )
2008-10-11 14:16:25 -05:00
{
2013-11-08 10:50:51 +01:00
TC_LOG_INFO ( "network" , "HandlePetRename. CMSG_PET_RENAME" );
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
uint64 petguid ;
uint8 isdeclined ;
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
std :: string name ;
DeclinedName declinedname ;
2009-10-17 15:51:44 -07:00
2012-11-02 22:23:14 +01:00
recvData >> petguid ;
recvData >> name ;
recvData >> isdeclined ;
2009-10-17 15:51:44 -07:00
2010-07-28 00:08:21 +02:00
Pet * pet = ObjectAccessor :: FindPet ( petguid );
2008-10-11 14:16:25 -05:00
// check it!
2013-06-11 18:39:51 -02:30
if ( ! pet || ! pet -> IsPet () || (( Pet * ) pet ) -> getPetType () != HUNTER_PET ||
2010-02-14 19:13:14 -07:00
! pet -> HasByteFlag ( UNIT_FIELD_BYTES_2 , 2 , UNIT_CAN_BE_RENAMED ) ||
2010-04-07 22:59:46 +02:00
pet -> GetOwnerGUID () != _player -> GetGUID () || ! pet -> GetCharmInfo ())
2008-10-11 14:16:25 -05:00
return ;
2009-10-17 15:51:44 -07:00
2009-07-10 08:25:33 +08:00
PetNameInvalidReason res = ObjectMgr :: CheckPetName ( name );
2010-04-07 19:14:10 +02:00
if ( res != PET_NAME_SUCCESS )
2008-10-11 14:16:25 -05:00
{
2009-07-10 08:25:33 +08:00
SendPetNameInvalid ( res , name , NULL );
2008-10-11 14:16:25 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2010-12-22 21:25:23 +01:00
if ( sObjectMgr -> IsReservedName ( name ))
2008-11-03 09:53:31 -06:00
{
SendPetNameInvalid ( PET_NAME_RESERVED , name , NULL );
return ;
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
pet -> SetName ( name );
2009-10-17 15:51:44 -07:00
2013-07-20 21:52:05 +02:00
Player * owner = pet -> GetOwner ();
if ( owner && owner -> GetGroup ())
owner -> SetGroupUpdateFlag ( GROUP_UPDATE_FLAG_PET_NAME );
2009-10-17 15:51:44 -07:00
2010-02-14 19:13:14 -07:00
pet -> RemoveByteFlag ( UNIT_FIELD_BYTES_2 , 2 , UNIT_CAN_BE_RENAMED );
2009-10-17 15:51:44 -07:00
2010-04-07 19:14:10 +02:00
if ( isdeclined )
2008-10-11 14:16:25 -05:00
{
2009-10-17 16:20:24 -07:00
for ( uint8 i = 0 ; i < MAX_DECLINED_NAME_CASES ; ++ i )
2008-11-03 09:53:31 -06:00
{
2012-11-02 22:23:14 +01:00
recvData >> declinedname . name [ i ];
2008-11-03 09:53:31 -06:00
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
std :: wstring wname ;
2013-09-15 04:25:19 +02:00
if ( ! Utf8toWStr ( name , wname ))
return ;
2011-05-10 18:52:28 +07:00
if ( ! ObjectMgr :: CheckDeclinedNames ( wname , declinedname ))
2008-10-11 14:16:25 -05:00
{
2008-11-03 09:53:31 -06:00
SendPetNameInvalid ( PET_NAME_DECLENSION_DOESNT_MATCH_BASE_NAME , name , & declinedname );
2008-10-11 14:16:25 -05:00
return ;
}
}
2009-10-17 15:51:44 -07:00
2010-08-21 03:19:25 +02:00
SQLTransaction trans = CharacterDatabase . BeginTransaction ();
2010-04-07 19:14:10 +02:00
if ( isdeclined )
2008-10-11 14:16:25 -05:00
{
2012-03-25 16:22:24 +02:00
PreparedStatement * stmt = CharacterDatabase . GetPreparedStatement ( CHAR_DEL_CHAR_PET_DECLINEDNAME );
stmt -> setUInt32 ( 0 , pet -> GetCharmInfo () -> GetPetNumber ());
trans -> Append ( stmt );
2014-03-02 00:00:39 +01:00
stmt = CharacterDatabase . GetPreparedStatement ( CHAR_INS_CHAR_PET_DECLINEDNAME );
2012-03-25 16:22:24 +02:00
stmt -> setUInt32 ( 0 , _player -> GetGUIDLow ());
for ( uint8 i = 0 ; i < 5 ; i ++ )
stmt -> setString ( i + 1 , declinedname . name [ i ]);
trans -> Append ( stmt );
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2012-03-25 16:22:24 +02:00
PreparedStatement * stmt = CharacterDatabase . GetPreparedStatement ( CHAR_UPD_CHAR_PET_NAME );
stmt -> setString ( 0 , name );
stmt -> setUInt32 ( 1 , _player -> GetGUIDLow ());
stmt -> setUInt32 ( 2 , pet -> GetCharmInfo () -> GetPetNumber ());
trans -> Append ( stmt );
2010-08-21 03:19:25 +02:00
CharacterDatabase . CommitTransaction ( trans );
2009-10-17 15:51:44 -07:00
2010-09-03 21:55:57 -07:00
pet -> SetUInt32Value ( UNIT_FIELD_PET_NAME_TIMESTAMP , uint32 ( time ( NULL ))); // cast can't be helped
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2012-11-27 13:02:04 +01:00
void WorldSession :: HandlePetAbandon ( WorldPacket & recvData )
2008-10-11 14:16:25 -05:00
{
uint64 guid ;
2012-11-02 22:23:14 +01:00
recvData >> guid ; //pet guid
2013-11-08 10:50:51 +01:00
TC_LOG_INFO ( "network" , "HandlePetAbandon. CMSG_PET_ABANDON pet guid is %u" , GUID_LOPART ( guid ));
2009-10-17 15:51:44 -07:00
2010-04-07 19:14:10 +02:00
if ( ! _player -> IsInWorld ())
2009-07-01 18:36:03 -05:00
return ;
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
// pet/charmed
2009-01-24 11:28:00 -06:00
Creature * pet = ObjectAccessor :: GetCreatureOrPetOrVehicle ( * _player , guid );
2010-04-07 19:14:10 +02:00
if ( pet )
2008-10-11 14:16:25 -05:00
{
2013-06-11 18:39:51 -02:30
if ( pet -> IsPet ())
2008-10-11 14:16:25 -05:00
{
2010-04-07 19:14:10 +02:00
if ( pet -> GetGUID () == _player -> GetPetGUID ())
2008-10-11 14:16:25 -05:00
{
uint32 feelty = pet -> GetPower ( POWER_HAPPINESS );
2011-09-29 12:43:05 +02:00
pet -> SetPower ( POWER_HAPPINESS , feelty > 50000 ? ( feelty - 50000 ) : 0 );
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2011-04-29 20:47:02 +02:00
_player -> RemovePet (( Pet * ) pet , PET_SAVE_AS_DELETED );
2008-10-11 14:16:25 -05:00
}
2010-04-07 19:14:10 +02:00
else if ( pet -> GetGUID () == _player -> GetCharmGUID ())
2009-03-20 14:01:46 -06:00
_player -> StopCastingCharm ();
2008-10-11 14:16:25 -05:00
}
}
2009-10-17 15:51:44 -07:00
2010-04-07 22:59:46 +02:00
void WorldSession :: HandlePetSpellAutocastOpcode ( WorldPacket & recvPacket )
2008-10-11 14:16:25 -05:00
{
2013-11-08 10:50:51 +01:00
TC_LOG_INFO ( "network" , "CMSG_PET_SPELL_AUTOCAST" );
2008-10-11 14:16:25 -05:00
uint64 guid ;
2009-07-01 18:07:20 -05:00
uint32 spellid ;
2008-10-11 14:16:25 -05:00
uint8 state ; //1 for on, 0 for off
2009-07-01 18:07:20 -05:00
recvPacket >> guid >> spellid >> state ;
2009-10-17 15:51:44 -07:00
2010-04-07 19:14:10 +02:00
if ( ! _player -> GetGuardianPet () && ! _player -> GetCharm ())
2008-10-11 14:16:25 -05:00
return ;
2009-10-17 15:51:44 -07:00
2010-04-07 19:14:10 +02:00
if ( ObjectAccessor :: FindPlayer ( guid ))
2008-10-11 14:16:25 -05:00
return ;
2009-10-17 15:51:44 -07:00
2011-04-29 20:47:02 +02:00
Creature * pet = ObjectAccessor :: GetCreatureOrPetOrVehicle ( * _player , guid );
2009-10-17 15:51:44 -07:00
2010-04-07 19:14:10 +02:00
if ( ! pet || ( pet != _player -> GetGuardianPet () && pet != _player -> GetCharm ()))
2008-10-11 14:16:25 -05:00
{
2013-11-08 10:50:51 +01:00
TC_LOG_ERROR ( "network" , "HandlePetSpellAutocastOpcode.Pet %u isn't pet of player %s (GUID: %u)." , uint32 ( GUID_LOPART ( guid )), GetPlayer () -> GetName (). c_str (), GUID_LOPART ( GetPlayer () -> GetGUID ()));
2008-10-11 14:16:25 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2011-07-26 23:09:28 +02:00
SpellInfo const * spellInfo = sSpellMgr -> GetSpellInfo ( spellid );
2013-12-03 21:46:00 +01:00
if ( ! spellInfo )
{
TC_LOG_ERROR ( "network" , "WORLD: unknown PET spell id %u" , spellid );
return ;
}
2008-10-11 14:16:25 -05:00
// do not add not learned spells/ passive spells
2012-11-03 16:01:46 +01:00
if ( ! pet -> HasSpell ( spellid ) || ! spellInfo -> IsAutocastable ())
2008-10-11 14:16:25 -05:00
return ;
2009-10-17 15:51:44 -07:00
2011-09-15 14:08:17 +02:00
CharmInfo * charmInfo = pet -> GetCharmInfo ();
2010-04-07 19:14:10 +02:00
if ( ! charmInfo )
2008-10-11 14:16:25 -05:00
{
2013-11-08 10:50:51 +01:00
TC_LOG_ERROR ( "network" , "WorldSession::HandlePetSpellAutocastOpcod: object (GUID: %u TypeId: %u) is considered pet-like but doesn't have a charminfo!" , pet -> GetGUIDLow (), pet -> GetTypeId ());
2008-10-11 14:16:25 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2013-06-11 18:39:51 -02:30
if ( pet -> IsPet ())
2011-07-26 23:09:28 +02:00
(( Pet * ) pet ) -> ToggleAutocast ( spellInfo , state );
2009-03-28 11:56:45 -06:00
else
2011-07-26 23:09:28 +02:00
pet -> GetCharmInfo () -> ToggleCreatureAutocast ( spellInfo , state );
2009-10-17 15:51:44 -07:00
2011-07-26 23:09:28 +02:00
charmInfo -> SetSpellAutocast ( spellInfo , state );
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2010-04-07 22:59:46 +02:00
void WorldSession :: HandlePetCastSpellOpcode ( WorldPacket & recvPacket )
2008-10-11 14:16:25 -05:00
{
2013-11-08 10:50:51 +01:00
TC_LOG_DEBUG ( "network" , "WORLD: CMSG_PET_CAST_SPELL" );
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
uint64 guid ;
2010-08-08 20:32:14 +02:00
uint8 castCount ;
uint32 spellId ;
uint8 castFlags ;
2009-10-17 15:51:44 -07:00
2010-08-08 20:32:14 +02:00
recvPacket >> guid >> castCount >> spellId >> castFlags ;
2009-10-17 15:51:44 -07:00
2013-11-08 10:50:51 +01:00
TC_LOG_DEBUG ( "network" , "WORLD: CMSG_PET_CAST_SPELL, guid: " UI64FMTD ", castCount: %u, spellId %u, castFlags %u" , guid , castCount , spellId , castFlags );
2009-10-17 15:51:44 -07:00
2008-11-05 20:51:05 -06:00
// This opcode is also sent from charmed and possessed units (players and creatures)
2010-04-07 19:14:10 +02:00
if ( ! _player -> GetGuardianPet () && ! _player -> GetCharm ())
2008-10-11 14:16:25 -05:00
return ;
2009-10-17 15:51:44 -07:00
2008-11-05 20:51:05 -06:00
Unit * caster = ObjectAccessor :: GetUnit ( * _player , guid );
2009-10-17 15:51:44 -07:00
2010-04-07 19:14:10 +02:00
if ( ! caster || ( caster != _player -> GetGuardianPet () && caster != _player -> GetCharm ()))
2008-10-11 14:16:25 -05:00
{
2013-11-08 10:50:51 +01:00
TC_LOG_ERROR ( "network" , "HandlePetCastSpellOpcode: Pet %u isn't pet of player %s (GUID: %u)." , uint32 ( GUID_LOPART ( guid )), GetPlayer () -> GetName (). c_str (), GUID_LOPART ( GetPlayer () -> GetGUID ()));
2008-10-11 14:16:25 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2011-09-15 14:08:17 +02:00
SpellInfo const * spellInfo = sSpellMgr -> GetSpellInfo ( spellId );
2010-04-07 19:14:10 +02:00
if ( ! spellInfo )
2008-10-11 14:16:25 -05:00
{
2013-11-08 10:50:51 +01:00
TC_LOG_ERROR ( "network" , "WORLD: unknown PET spell id %i" , spellId );
2008-10-11 14:16:25 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
// do not cast not learned spells
2011-07-26 23:09:28 +02:00
if ( ! caster -> HasSpell ( spellId ) || spellInfo -> IsPassive ())
2008-10-11 14:16:25 -05:00
return ;
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
SpellCastTargets targets ;
2011-06-23 13:28:52 +02:00
targets . Read ( recvPacket , caster );
2010-08-08 20:32:14 +02:00
HandleClientCastFlags ( recvPacket , castFlags , targets );
2009-10-17 15:51:44 -07:00
2012-02-03 19:02:17 +02:00
caster -> ClearUnitState ( UNIT_STATE_FOLLOW );
2009-10-17 15:51:44 -07:00
2011-07-31 19:03:06 +02:00
Spell * spell = new Spell ( caster , spellInfo , TRIGGERED_NONE );
2010-09-19 17:02:08 +02:00
spell -> m_cast_count = castCount ; // probably pending spell cast
2008-10-11 14:16:25 -05:00
spell -> m_targets = targets ;
2009-10-17 15:51:44 -07:00
2013-08-14 15:45:36 +02:00
SpellCastResult result = spell -> CheckPetCast ( NULL );
2013-08-12 21:56:39 +02:00
2010-04-07 19:14:10 +02:00
if ( result == SPELL_CAST_OK )
2008-10-11 14:16:25 -05:00
{
2013-08-14 15:45:36 +02:00
if ( Creature * creature = caster -> ToCreature ())
2008-10-11 14:16:25 -05:00
{
2013-08-14 15:45:36 +02:00
creature -> AddCreatureSpellCooldown ( spellId );
if ( Pet * pet = creature -> ToPet ())
2008-11-05 20:51:05 -06:00
{
// 10% chance to play special pet attack talk, else growl
// actually this only seems to happen on special spells, fire shield for imp, torment for voidwalker, but it's stupid to check every spell
2013-08-14 15:45:36 +02:00
if ( pet -> getPetType () == SUMMON_PET && ( urand ( 0 , 100 ) < 10 ))
pet -> SendPetTalk ( PET_TALK_SPECIAL_SPELL );
2008-11-05 20:51:05 -06:00
else
pet -> SendPetAIReaction ( guid );
}
2008-10-11 14:16:25 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
spell -> prepare ( & ( spell -> m_targets ));
}
else
{
2013-08-14 15:45:36 +02:00
spell -> SendPetCastResult ( result );
2010-04-07 19:14:10 +02:00
if ( caster -> GetTypeId () == TYPEID_PLAYER )
2008-11-05 20:51:05 -06:00
{
2010-08-08 20:32:14 +02:00
if ( ! caster -> ToPlayer () -> HasSpellCooldown ( spellId ))
GetPlayer () -> SendClearCooldown ( spellId , caster );
2008-11-05 20:51:05 -06:00
}
else
{
2010-08-08 20:32:14 +02:00
if ( ! caster -> ToCreature () -> HasSpellCooldown ( spellId ))
GetPlayer () -> SendClearCooldown ( spellId , caster );
2008-11-05 20:51:05 -06:00
}
2009-10-17 15:51:44 -07:00
2008-10-11 14:16:25 -05:00
spell -> finish ( false );
delete spell ;
}
}
2009-10-17 15:51:44 -07:00
2008-12-12 11:21:28 -06:00
void WorldSession :: SendPetNameInvalid ( uint32 error , const std :: string & name , DeclinedName * declinedName )
2008-11-03 09:53:31 -06:00
{
WorldPacket data ( SMSG_PET_NAME_INVALID , 4 + name . size () + 1 + 1 );
data << uint32 ( error );
data << name ;
2010-04-07 19:14:10 +02:00
if ( declinedName )
2008-11-03 09:53:31 -06:00
{
data << uint8 ( 1 );
2009-10-17 16:20:24 -07:00
for ( uint32 i = 0 ; i < MAX_DECLINED_NAME_CASES ; ++ i )
2008-11-03 09:53:31 -06:00
data << declinedName -> name [ i ];
}
else
data << uint8 ( 0 );
SendPacket ( & data );
}
2009-10-17 15:51:44 -07:00
2012-11-09 13:13:45 +01:00
void WorldSession :: HandlePetLearnTalent ( WorldPacket & recvData )
2008-12-24 09:58:26 -06:00
{
2013-11-08 10:50:51 +01:00
TC_LOG_DEBUG ( "network" , "WORLD: CMSG_PET_LEARN_TALENT" );
2009-10-17 15:51:44 -07:00
2008-12-24 09:58:26 -06:00
uint64 guid ;
2012-11-09 13:13:45 +01:00
uint32 talentId , requestedRank ;
recvData >> guid >> talentId >> requestedRank ;
2009-10-17 15:51:44 -07:00
2012-11-09 13:13:45 +01:00
_player -> LearnPetTalent ( guid , talentId , requestedRank );
2009-06-11 00:45:59 -05:00
_player -> SendTalentsInfoData ( true );
}
2009-10-17 15:51:44 -07:00
2012-11-27 13:02:04 +01:00
void WorldSession :: HandleLearnPreviewTalentsPet ( WorldPacket & recvData )
2009-06-11 00:45:59 -05:00
{
2013-11-08 10:50:51 +01:00
TC_LOG_DEBUG ( "network" , "CMSG_LEARN_PREVIEW_TALENTS_PET" );
2009-10-17 15:51:44 -07:00
2009-06-11 00:45:59 -05:00
uint64 guid ;
2012-11-02 22:23:14 +01:00
recvData >> guid ;
2009-10-17 15:51:44 -07:00
2009-06-11 00:45:59 -05:00
uint32 talentsCount ;
2012-11-02 22:23:14 +01:00
recvData >> talentsCount ;
2009-10-17 15:51:44 -07:00
2009-06-11 00:45:59 -05:00
uint32 talentId , talentRank ;
2009-10-17 15:51:44 -07:00
2009-10-17 16:20:24 -07:00
for ( uint32 i = 0 ; i < talentsCount ; ++ i )
2009-06-11 00:45:59 -05:00
{
2012-11-02 22:23:14 +01:00
recvData >> talentId >> talentRank ;
2009-10-17 15:51:44 -07:00
2009-06-11 00:45:59 -05:00
_player -> LearnPetTalent ( guid , talentId , talentRank );
}
2009-10-17 15:51:44 -07:00
2009-06-11 00:45:59 -05:00
_player -> SendTalentsInfoData ( true );
2009-02-18 22:39:00 +01:00
}