Skip to content

Commit

Permalink
Pethandler cleanup
Browse files Browse the repository at this point in the history
We have _player, no need to use GetPlayer()
remove a bit of code redundancy and simplify code flow a bit in some
areas.
  • Loading branch information
Phatcat committed Aug 3, 2016
1 parent 8837c45 commit 1ce1d20
Showing 1 changed file with 40 additions and 43 deletions.
83 changes: 40 additions & 43 deletions src/game/PetHandler.cpp
Expand Up @@ -51,19 +51,19 @@ void WorldSession::HandlePetAction(WorldPacket& recv_data)
return;
}

if (GetPlayer()->GetObjectGuid() != pet->GetCharmerOrOwnerGuid())
if (_player->GetObjectGuid() != pet->GetCharmerOrOwnerGuid())
{
sLog.outError("HandlePetAction: %s isn't controlled by %s.", petGuid.GetString().c_str(), GetPlayer()->GetGuidStr().c_str());
sLog.outError("HandlePetAction: %s isn't controlled by %s.", petGuid.GetString().c_str(), _player->GetGuidStr().c_str());
return;
}

if (!pet->isAlive())
return;

if (pet->GetTypeId() == TYPEID_PLAYER)
if (pet->GetTypeId() == TYPEID_PLAYER && pet->GetCharmer()->GetTypeId() == TYPEID_PLAYER)
{
// controller player can only do melee attack
if (!(flag == ACT_COMMAND && spellid == COMMAND_ATTACK))
// controller player cannot use controlled player's spells
if (flag != (ACT_COMMAND || ACT_REACTION))
return;
}
else if (((Creature*)pet)->IsPet())
Expand All @@ -89,8 +89,7 @@ void WorldSession::HandlePetAction(WorldPacket& recv_data)
{
pet->StopMoving();
pet->AttackStop(true, true);
pet->GetMotionMaster()->Clear(false);
pet->GetMotionMaster()->MoveIdle();
pet->GetMotionMaster()->Clear();
((Pet*)pet)->SetStayPosition(true);
((Pet*)pet)->SetIsRetreating();
((Pet*)pet)->SetSpellOpener();
Expand All @@ -99,46 +98,45 @@ void WorldSession::HandlePetAction(WorldPacket& recv_data)
}
case COMMAND_FOLLOW: // spellid=1792 // FOLLOW
{
((Pet*)pet)->SetStayPosition();
pet->StopMoving();
pet->AttackStop(true, true);
pet->GetMotionMaster()->MoveFollow(_player, PET_FOLLOW_DIST, PET_FOLLOW_ANGLE);
pet->GetMotionMaster()->Clear();
((Pet*)pet)->SetStayPosition();
((Pet*)pet)->SetIsRetreating(true);
((Pet*)pet)->SetSpellOpener();
charmInfo->SetCommandState(COMMAND_FOLLOW);
break;
}
case COMMAND_ATTACK: // spellid=1792 // ATTACK
{
Unit* targetUnit = _player->GetMap()->GetUnit(targetGuid);
if (!targetUnit)
return;

// not let attack friendly units.
if (GetPlayer()->IsFriendlyTo(targetUnit))
return;

((Pet*)pet)->SetIsRetreating();
((Pet*)pet)->SetSpellOpener();

// This is true if pet has no target or has target but targets differs.
if (pet->getVictim() != targetUnit)
pet->AttackStop();

pet->GetMotionMaster()->Clear();
Unit* targetUnit = targetGuid ? _player->GetMap()->GetUnit(targetGuid) : nullptr;

_player->SetInCombatState(true, targetUnit);

if (((Creature*)pet)->AI())
if (targetUnit && targetUnit != pet && targetUnit->isTargetableForAttack() && targetUnit->isInAccessablePlaceFor((Creature*)pet))
{
((Creature*)pet)->AI()->AttackStart(targetUnit);
// 10% chance to play special warlock pet attack talk, else growl
if (((Creature*)pet)->IsPet() && ((Pet*)pet)->getPetType() == SUMMON_PET && pet != targetUnit && roll_chance_i(10))
pet->SendPetTalk((uint32)PET_TALK_ATTACK);
_player->SetInCombatState(true, targetUnit);

pet->SendPetAIReaction();
// This is true if pet has no target or has target but targets differs.
if (pet->getVictim() != targetUnit)
{
pet->AttackStop();
pet->GetMotionMaster()->Clear();

if (((Creature*)pet)->AI())
{
((Creature*)pet)->AI()->AttackStart(targetUnit);
// 10% chance to play special warlock pet attack talk, else growl
if (((Creature*)pet)->IsPet() && ((Pet*)pet)->getPetType() == SUMMON_PET && roll_chance_i(10))
pet->SendPetTalk((uint32)PET_TALK_ATTACK);

pet->SendPetAIReaction();
}
else
pet->Attack(targetUnit, true);
}
}
else
pet->Attack(targetUnit, true);

break;
}
Expand Down Expand Up @@ -178,8 +176,6 @@ void WorldSession::HandlePetAction(WorldPacket& recv_data)
{
pet->AttackStop(true, true);
((Pet*)pet)->SetSpellOpener();
if (!charmInfo->GetCommandState() == COMMAND_STAY)
pet->GetMotionMaster()->MoveFollow(_player, PET_FOLLOW_DIST, PET_FOLLOW_ANGLE);
}
case REACT_DEFENSIVE: // recovery
case REACT_AGGRESSIVE: // activete
Expand All @@ -193,9 +189,10 @@ void WorldSession::HandlePetAction(WorldPacket& recv_data)
case ACT_PASSIVE: // 0x01
case ACT_ENABLED: // 0xC1 spell
{
Unit* unit_target = nullptr;
if (targetGuid)
unit_target = _player->GetMap()->GetUnit(targetGuid);
((Pet*)pet)->SetIsRetreating();
((Pet*)pet)->SetSpellOpener();

Unit* unit_target = targetGuid ? _player->GetMap()->GetUnit(targetGuid) : nullptr;

// do not cast unknown spells
SpellEntry const* spellInfo = sSpellStore.LookupEntry(spellid);
Expand Down Expand Up @@ -335,16 +332,16 @@ void WorldSession::HandlePetStopAttack(WorldPacket& recv_data)
ObjectGuid petGuid;
recv_data >> petGuid;

Unit* pet = GetPlayer()->GetMap()->GetUnit(petGuid); // pet or controlled creature/player
Unit* pet = _player->GetMap()->GetUnit(petGuid); // pet or controlled creature/player
if (!pet)
{
sLog.outError("%s doesn't exist.", petGuid.GetString().c_str());
return;
}

if (GetPlayer()->GetObjectGuid() != pet->GetCharmerOrOwnerGuid())
if (_player->GetObjectGuid() != pet->GetCharmerOrOwnerGuid())
{
sLog.outError("HandlePetStopAttack: %s isn't charm/pet of %s.", petGuid.GetString().c_str(), GetPlayer()->GetGuidStr().c_str());
sLog.outError("HandlePetStopAttack: %s isn't charm/pet of %s.", petGuid.GetString().c_str(), _player->GetGuidStr().c_str());
return;
}

Expand Down Expand Up @@ -632,7 +629,7 @@ void WorldSession::HandlePetUnlearnOpcode(WorldPacket& recvPacket)

if (!pet || guid != pet->GetObjectGuid())
{
sLog.outError("HandlePetUnlearnOpcode. %s isn't pet of %s .", guid.GetString().c_str(), GetPlayer()->GetGuidStr().c_str());
sLog.outError("HandlePetUnlearnOpcode. %s isn't pet of %s .", guid.GetString().c_str(), _player->GetGuidStr().c_str());
return;
}

Expand Down Expand Up @@ -661,7 +658,7 @@ void WorldSession::HandlePetSpellAutocastOpcode(WorldPacket& recvPacket)
Creature* pet = _player->GetMap()->GetAnyTypeCreature(guid);
if (!pet || (guid != _player->GetPetGuid() && guid != _player->GetCharmGuid()))
{
sLog.outError("HandlePetSpellAutocastOpcode. %s isn't pet of %s .", guid.GetString().c_str(), GetPlayer()->GetGuidStr().c_str());
sLog.outError("HandlePetSpellAutocastOpcode. %s isn't pet of %s .", guid.GetString().c_str(), _player->GetGuidStr().c_str());
return;
}

Expand Down Expand Up @@ -702,7 +699,7 @@ void WorldSession::HandlePetCastSpellOpcode(WorldPacket& recvPacket)

if (!pet || (guid != _player->GetPetGuid() && guid != _player->GetCharmGuid()))
{
sLog.outError("HandlePetCastSpellOpcode: %s isn't pet of %s .", guid.GetString().c_str(), GetPlayer()->GetGuidStr().c_str());
sLog.outError("HandlePetCastSpellOpcode: %s isn't pet of %s .", guid.GetString().c_str(), _player->GetGuidStr().c_str());
return;
}

Expand Down

0 comments on commit 1ce1d20

Please sign in to comment.