optimization loop

This commit is contained in:
antony 2024-08-02 16:41:50 +02:00
parent ab4e3c2c45
commit 234460e298
2 changed files with 33 additions and 51 deletions

View File

@ -341,25 +341,17 @@ void PlayerbotAI::UpdateAIInternal([[maybe_unused]] uint32 elapsed, bool minimal
ExternalEventHelper helper(aiObjectContext); ExternalEventHelper helper(aiObjectContext);
// chat replies // chat replies
std::list<ChatQueuedReply> delayedResponses; for (auto it = chatReplies.begin(); it != chatReplies.end(); )
while (!chatReplies.empty())
{ {
ChatQueuedReply& holder = chatReplies.front(); time_t checkTime = it->m_time;
time_t& checkTime = holder.m_time;
if (checkTime && time(0) < checkTime) if (checkTime && time(0) < checkTime)
{ {
delayedResponses.push_back(std::move(holder)); ++it;
chatReplies.pop();
continue; continue;
} }
ChatReplyAction::ChatReplyDo(bot, holder.m_type, holder.m_guid1, holder.m_guid2, holder.m_msg, holder.m_chanName, holder.m_name); ChatReplyAction::ChatReplyDo(bot, it->m_type, it->m_guid1, it->m_guid2, it->m_msg, it->m_chanName, it->m_name);
chatReplies.pop(); it = chatReplies.erase(it);
}
for (std::list<ChatQueuedReply>::iterator i = delayedResponses.begin(); i != delayedResponses.end(); ++i)
{
chatReplies.push(*i);
} }
HandleCommands(); HandleCommands();
@ -417,33 +409,24 @@ void PlayerbotAI::UpdateAIInternal([[maybe_unused]] uint32 elapsed, bool minimal
void PlayerbotAI::HandleCommands() void PlayerbotAI::HandleCommands()
{ {
ExternalEventHelper helper(aiObjectContext); ExternalEventHelper helper(aiObjectContext);
std::list<ChatCommandHolder> delayed; for (auto it = chatCommands.begin(); it != chatCommands.end(); )
while (!chatCommands.empty())
{ {
ChatCommandHolder& holder = chatCommands.front(); time_t& checkTime = it->GetTime();
time_t checkTime = holder.GetTime();
if (checkTime && time(0) < checkTime) if (checkTime && time(0) < checkTime)
{ {
delayed.push_back(std::move(holder)); ++it;
chatCommands.pop();
continue; continue;
} }
std::string command = holder.GetCommand(); const std::string& command = it->GetCommand();
Player* owner = holder.GetOwner(); Player* owner = it->GetOwner();
if (!helper.ParseChatCommand(command, owner) && holder.GetType() == CHAT_MSG_WHISPER) if (!helper.ParseChatCommand(command, owner) && it->GetType() == CHAT_MSG_WHISPER)
{ {
//ostringstream out; out << "Unknown command " << command; //ostringstream out; out << "Unknown command " << command;
//TellPlayer(out); //TellPlayer(out);
//helper.ParseChatCommand("help"); //helper.ParseChatCommand("help");
} }
it = chatCommands.erase(it);
chatCommands.pop();
}
for (std::list<ChatCommandHolder>::iterator i = delayed.begin(); i != delayed.end(); ++i)
{
chatCommands.push(*i);
} }
} }
@ -523,8 +506,7 @@ void PlayerbotAI::HandleCommand(uint32 type, const std::string& text, Player& fr
if (type == CHAT_MSG_RAID_WARNING && filtered.find(bot->GetName()) != std::string::npos && filtered.find("award") == std::string::npos) if (type == CHAT_MSG_RAID_WARNING && filtered.find(bot->GetName()) != std::string::npos && filtered.find("award") == std::string::npos)
{ {
ChatCommandHolder cmd("warning", &fromPlayer, type); chatCommands.push_back(ChatCommandHolder("warning", &fromPlayer, type));
chatCommands.push(cmd);
return; return;
} }
@ -560,8 +542,8 @@ void PlayerbotAI::HandleCommand(uint32 type, const std::string& text, Player& fr
index++; index++;
} }
} }
ChatCommandHolder cmd(remaining, &fromPlayer, type, time(0) + index);
chatCommands.push(cmd); chatCommands.push_back(ChatCommandHolder(remaining, &fromPlayer, type, time(0) + index));
} }
else if (filtered == "reset") else if (filtered == "reset")
{ {
@ -610,8 +592,7 @@ void PlayerbotAI::HandleCommand(uint32 type, const std::string& text, Player& fr
else else
{ {
ChatCommandHolder cmd(filtered, &fromPlayer, type); chatCommands.push_back(ChatCommandHolder(filtered, &fromPlayer, type));
chatCommands.push(cmd);
} }
} }
@ -805,8 +786,7 @@ void PlayerbotAI::HandleCommand(uint32 type, std::string const text, Player* fro
if (type == CHAT_MSG_RAID_WARNING && filtered.find(bot->GetName()) != std::string::npos && if (type == CHAT_MSG_RAID_WARNING && filtered.find(bot->GetName()) != std::string::npos &&
filtered.find("award") == std::string::npos) filtered.find("award") == std::string::npos)
{ {
ChatCommandHolder cmd("warning", fromPlayer, type); chatCommands.push_back(ChatCommandHolder("warning", fromPlayer, type));
chatCommands.push(cmd);
return; return;
} }
@ -834,8 +814,7 @@ void PlayerbotAI::HandleCommand(uint32 type, std::string const text, Player* fro
} }
} }
ChatCommandHolder cmd(remaining, fromPlayer, type, time(nullptr) + index); chatCommands.push_back(ChatCommandHolder(remaining, fromPlayer, type, time(nullptr) + index));
chatCommands.push(cmd);
} }
else if (filtered == "reset") else if (filtered == "reset")
{ {
@ -868,8 +847,7 @@ void PlayerbotAI::HandleCommand(uint32 type, std::string const text, Player* fro
} }
else else
{ {
ChatCommandHolder cmd(filtered, fromPlayer, type); chatCommands.push_back(ChatCommandHolder(filtered, fromPlayer, type));
chatCommands.push(cmd);
} }
} }
@ -935,7 +913,10 @@ void PlayerbotAI::HandleBotOutgoingPacket(WorldPacket const& packet)
uint8 msgtype, chatTag; uint8 msgtype, chatTag;
uint32 lang, textLen, nameLen, unused; uint32 lang, textLen, nameLen, unused;
ObjectGuid guid1, guid2; ObjectGuid guid1, guid2;
std::string name, chanName, message; std::string name = "";
std::string chanName = "";
std::string message = "";
p >> msgtype >> lang; p >> msgtype >> lang;
p >> guid1 >> unused; p >> guid1 >> unused;
if (guid1.IsEmpty() || p.size() > p.DEFAULT_SIZE) if (guid1.IsEmpty() || p.size() > p.DEFAULT_SIZE)
@ -1026,7 +1007,8 @@ void PlayerbotAI::HandleBotOutgoingPacket(WorldPacket const& packet)
return; return;
} }
} }
QueueChatResponse(msgtype, guid1, ObjectGuid(), message, chanName, name);
QueueChatResponse(std::move(ChatQueuedReply{msgtype, guid1.GetCounter(), guid2.GetCounter(), message, chanName, name, time(nullptr) + urand(inCombat ? 10 : 5, inCombat ? 25 : 15)}));
GetAiObjectContext()->GetValue<time_t>("last said", "chat")->Set(time(0) + urand(5, 25)); GetAiObjectContext()->GetValue<time_t>("last said", "chat")->Set(time(0) + urand(5, 25));
return; return;
} }
@ -5023,9 +5005,9 @@ bool PlayerbotAI::IsInRealGuild()
return !(sPlayerbotAIConfig->IsInRandomAccountList(leaderAccount)); return !(sPlayerbotAIConfig->IsInRandomAccountList(leaderAccount));
} }
void PlayerbotAI::QueueChatResponse(uint8& msgtype, ObjectGuid& guid1, ObjectGuid guid2, std::string& message, std::string& chanName, std::string& name) void PlayerbotAI::QueueChatResponse(const ChatQueuedReply chatReply)
{ {
chatReplies.push({ msgtype, guid1.GetCounter(), guid2.GetCounter(), message, chanName, name, time(nullptr) + urand(inCombat ? 10 : 5, inCombat ? 25 : 15) }); chatReplies.push_back(std::move(chatReply));
} }
bool PlayerbotAI::EqualLowercaseName(std::string s1, std::string s2) bool PlayerbotAI::EqualLowercaseName(std::string s1, std::string s2)

View File

@ -340,10 +340,10 @@ class ChatCommandHolder
ChatCommandHolder(std::string const command, Player* owner = nullptr, uint32 type = CHAT_MSG_WHISPER, time_t time = 0) : command(command), owner(owner), type(type), time(time) { } ChatCommandHolder(std::string const command, Player* owner = nullptr, uint32 type = CHAT_MSG_WHISPER, time_t time = 0) : command(command), owner(owner), type(type), time(time) { }
ChatCommandHolder(ChatCommandHolder const& other) : command(other.command), owner(other.owner), type(other.type), time(other.time) { } ChatCommandHolder(ChatCommandHolder const& other) : command(other.command), owner(other.owner), type(other.type), time(other.time) { }
std::string const GetCommand() { return command; } const std::string& GetCommand() { return command; }
Player* GetOwner() { return owner; } Player* GetOwner() { return owner; }
uint32 GetType() { return type; } uint32& GetType() { return type; }
time_t GetTime() { return time; } time_t& GetTime() { return time; }
private: private:
std::string const command; std::string const command;
@ -364,7 +364,7 @@ class PlayerbotAI : public PlayerbotAIBase
std::string const HandleRemoteCommand(std::string const command); std::string const HandleRemoteCommand(std::string const command);
void HandleCommand(uint32 type, std::string const text, Player* fromPlayer); void HandleCommand(uint32 type, std::string const text, Player* fromPlayer);
void QueueChatResponse(uint8& msgtype, ObjectGuid& guid1, ObjectGuid guid2, std::string& message, std::string& chanName, std::string& name); void QueueChatResponse(const ChatQueuedReply reply);
void HandleBotOutgoingPacket(WorldPacket const& packet); void HandleBotOutgoingPacket(WorldPacket const& packet);
void HandleMasterIncomingPacket(WorldPacket const& packet); void HandleMasterIncomingPacket(WorldPacket const& packet);
void HandleMasterOutgoingPacket(WorldPacket const& packet); void HandleMasterOutgoingPacket(WorldPacket const& packet);
@ -551,8 +551,8 @@ class PlayerbotAI : public PlayerbotAIBase
Engine* engines[BOT_STATE_MAX]; Engine* engines[BOT_STATE_MAX];
BotState currentState; BotState currentState;
ChatHelper chatHelper; ChatHelper chatHelper;
std::queue<ChatCommandHolder> chatCommands; std::list<ChatCommandHolder> chatCommands;
std::queue<ChatQueuedReply> chatReplies; std::list<ChatQueuedReply> chatReplies;
PacketHandlingHelper botOutgoingPacketHandlers; PacketHandlingHelper botOutgoingPacketHandlers;
PacketHandlingHelper masterIncomingPacketHandlers; PacketHandlingHelper masterIncomingPacketHandlers;
PacketHandlingHelper masterOutgoingPacketHandlers; PacketHandlingHelper masterOutgoingPacketHandlers;