Skip to content

Commit

Permalink
Playerbot: Remove core ChatHandler mod and handle chat packets like a…
Browse files Browse the repository at this point in the history
… client
  • Loading branch information
BThallid authored and cala committed Jul 31, 2017
1 parent ff34f94 commit 63d014c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 44 deletions.
41 changes: 0 additions & 41 deletions src/game/Chat/ChatHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@
#include "Grids/GridNotifiersImpl.h"
#include "Grids/CellImpl.h"

#ifdef BUILD_PLAYERBOT
#include "PlayerBot/Base/PlayerbotAI.h"
#endif

bool WorldSession::processChatmessageFurtherAfterSecurityChecks(std::string& msg, uint32 lang)
{
if (lang != LANG_ADDON)
Expand Down Expand Up @@ -229,20 +225,7 @@ void WorldSession::HandleMessagechatOpcode(WorldPacket& recv_data)
}
}

#ifdef BUILD_PLAYERBOT
// Handle whispered command to bot
if (player->GetPlayerbotAI())
{
player->GetPlayerbotAI()->HandleCommand(msg, *GetPlayer());
GetPlayer()->m_speakTime = 0;
GetPlayer()->m_speakCount = 0;
}
else
// Unmodded core line code below
GetPlayer()->Whisper(msg, lang, player->GetObjectGuid());
#else
GetPlayer()->Whisper(msg, lang, player->GetObjectGuid());
#endif
} break;

case CHAT_MSG_PARTY:
Expand Down Expand Up @@ -275,30 +258,6 @@ void WorldSession::HandleMessagechatOpcode(WorldPacket& recv_data)
if ((type == CHAT_MSG_PARTY_LEADER) && !group->IsLeader(_player->GetObjectGuid()))
return;

#ifdef BUILD_PLAYERBOT
// Broadcast message to bot members
for(GroupReference* itr = group->GetFirstMember(); itr != nullptr; itr=itr->next())
{
Player* player = itr->getSource();
if (player && player->GetPlayerbotAI() && ((msg.find("help",0) != std::string::npos)
|| (msg.find("gm",0) != std::string::npos)
|| (msg.find("complete",0) != std::string::npos)))
{
player->GetPlayerbotAI()->HandleCommand(msg, *GetPlayer());
GetPlayer()->m_speakTime = 0;
GetPlayer()->m_speakCount = 0;
break;
}

if (player && player->GetPlayerbotAI())
{
player->GetPlayerbotAI()->HandleCommand(msg, *GetPlayer());
GetPlayer()->m_speakTime = 0;
GetPlayer()->m_speakCount = 0;
}
}
#endif

WorldPacket data;
ChatHandler::BuildChatPacket(data, ChatMsg(type), msg.c_str(), Language(lang), _player->GetChatTag(), _player->GetObjectGuid(), _player->GetName());
group->BroadcastPacket(data, false, group->GetMemberGroup(GetPlayer()->GetObjectGuid()));
Expand Down
63 changes: 60 additions & 3 deletions src/game/PlayerBot/Base/PlayerbotAI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2011,6 +2011,57 @@ void PlayerbotAI::HandleBotOutgoingPacket(const WorldPacket& packet)
return;
}

// Handle chat messages here
case SMSG_MESSAGECHAT:
{
WorldPacket p(packet);
uint8 msgtype;
uint32 language;

p >> msgtype; // 1 type
p >> language; // 4 language

if (language == LANG_ADDON)
return;

switch (msgtype)
{
case CHAT_MSG_RAID:
case CHAT_MSG_RAID_LEADER:
case CHAT_MSG_PARTY:
case CHAT_MSG_WHISPER:
{
ObjectGuid senderGuid;
std::string channelName;
uint32 length;
std::string text;
uint8 chattag;

p >> senderGuid; // 8 player from guid
if (msgtype == CHAT_MSG_PARTY)
p >> senderGuid; // 8 player from guid needs to be read again if message is from party channel

p >> length; // 4 length of text
p >> text; // string message
p >> chattag; // 1 AFK/DND/WHISPER_INFORM

Player *sender = sObjectMgr.GetPlayer(senderGuid);
if (!sender) // couldn't find player that sent message
return;

// do not listen to other bots
if (sender != m_bot && sender->GetPlayerbotAI())
return;
HandleCommand(text, *sender);
return;
}
default:
return;
}

return;
}

// If the leader role was given to the bot automatically give it to the master
// if the master is in the group, otherwise leave group
case SMSG_GROUP_SET_LEADER:
Expand Down Expand Up @@ -5138,9 +5189,15 @@ void PlayerbotAI::TellMaster(const char *fmt, ...) const

void PlayerbotAI::SendWhisper(const std::string& text, Player& player) const
{
WorldPacket data;
ChatHandler::BuildChatPacket(data, CHAT_MSG_WHISPER, text.c_str(), LANG_UNIVERSAL, m_bot->GetChatTag(), m_bot->GetObjectGuid(), m_bot->GetName());
player.GetSession()->SendPacket(data);
if (player.GetPlayerbotAI())
return;

WorldPacket* const packet = new WorldPacket(CMSG_MESSAGECHAT, 200);
*packet << uint32(CHAT_MSG_WHISPER);
*packet << uint32(LANG_UNIVERSAL);
*packet << player.GetName();
*packet << text;
m_bot->GetSession()->QueuePacket(std::move(std::unique_ptr<WorldPacket>(packet))); // queue the packet to get around race condition
}

bool PlayerbotAI::canObeyCommandFrom(const Player& player) const
Expand Down

0 comments on commit 63d014c

Please sign in to comment.