Merge pull request #1708 from hermensbas/feature/removeFromGroup_replaced_with_worldpackets

[fix crash] Crash on removeFromGroup
This commit is contained in:
kadeshar
2025-10-10 19:59:21 +02:00
committed by GitHub
5 changed files with 67 additions and 56 deletions

View File

@@ -378,10 +378,7 @@ void PlayerbotAI::UpdateAIGroupMembership()
PlayerbotAI* leaderAI = GET_PLAYERBOT_AI(leader); PlayerbotAI* leaderAI = GET_PLAYERBOT_AI(leader);
if (leaderAI && !leaderAI->IsRealPlayer()) if (leaderAI && !leaderAI->IsRealPlayer())
{ {
WorldPacket* packet = new WorldPacket(CMSG_GROUP_DISBAND); LeaveOrDisbandGroup();
bot->GetSession()->QueuePacket(packet);
// bot->RemoveFromGroup();
ResetStrategies();
} }
} }
} }
@@ -405,10 +402,7 @@ void PlayerbotAI::UpdateAIGroupMembership()
} }
if (!hasRealPlayer) if (!hasRealPlayer)
{ {
WorldPacket* packet = new WorldPacket(CMSG_GROUP_DISBAND); LeaveOrDisbandGroup();
bot->GetSession()->QueuePacket(packet);
// bot->RemoveFromGroup();
ResetStrategies();
} }
} }
} }
@@ -791,6 +785,16 @@ void PlayerbotAI::Reset(bool full)
} }
} }
void PlayerbotAI::LeaveOrDisbandGroup()
{
if (!bot || !bot->GetGroup() || IsRealPlayer())
return;
WorldPacket* packet = new WorldPacket(CMSG_GROUP_DISBAND);
bot->GetSession()->QueuePacket(packet);
ResetStrategies();
}
bool PlayerbotAI::IsAllowedCommand(std::string const text) bool PlayerbotAI::IsAllowedCommand(std::string const text)
{ {
if (unsecuredCommands.empty()) if (unsecuredCommands.empty())

View File

@@ -415,6 +415,7 @@ public:
void ResetStrategies(bool load = false); void ResetStrategies(bool load = false);
void ReInitCurrentEngine(); void ReInitCurrentEngine();
void Reset(bool full = false); void Reset(bool full = false);
void LeaveOrDisbandGroup();
static bool IsTank(Player* player, bool bySpec = false); static bool IsTank(Player* player, bool bySpec = false);
static bool IsHeal(Player* player, bool bySpec = false); static bool IsHeal(Player* player, bool bySpec = false);
static bool IsDps(Player* player, bool bySpec = false); static bool IsDps(Player* player, bool bySpec = false);

View File

@@ -514,7 +514,7 @@ void PlayerbotHolder::OnBotLogin(Player* const bot)
if (!groupValid) if (!groupValid)
{ {
bot->RemoveFromGroup(); botAI->LeaveOrDisbandGroup();
} }
} }

View File

@@ -1517,33 +1517,38 @@ bool RandomPlayerbotMgr::ProcessBot(uint32 bot)
return false; return false;
} }
bool RandomPlayerbotMgr::ProcessBot(Player* player) bool RandomPlayerbotMgr::ProcessBot(Player* bot)
{ {
uint32 bot = player->GetGUID().GetCounter();
if (player->InBattleground()) PlayerbotAI* botAI = GET_PLAYERBOT_AI(bot);
if (!botAI)
return false; return false;
if (player->InBattlegroundQueue()) if (bot->InBattleground())
return false; return false;
if (bot->InBattlegroundQueue())
return false;
uint32 botId = bot->GetGUID().GetCounter();
// if death revive // if death revive
if (player->isDead()) if (bot->isDead())
{ {
if (!GetEventValue(bot, "dead")) if (!GetEventValue(botId, "dead"))
{ {
uint32 randomTime = uint32 randomTime =
urand(sPlayerbotAIConfig->minRandomBotReviveTime, sPlayerbotAIConfig->maxRandomBotReviveTime); urand(sPlayerbotAIConfig->minRandomBotReviveTime, sPlayerbotAIConfig->maxRandomBotReviveTime);
LOG_DEBUG("playerbots", "Mark bot {} as dead, will be revived in {}s.", player->GetName().c_str(), LOG_DEBUG("playerbots", "Mark bot {} as dead, will be revived in {}s.", bot->GetName().c_str(),
randomTime); randomTime);
SetEventValue(bot, "dead", 1, sPlayerbotAIConfig->maxRandomBotInWorldTime); SetEventValue(botId, "dead", 1, sPlayerbotAIConfig->maxRandomBotInWorldTime);
SetEventValue(bot, "revive", 1, randomTime); SetEventValue(botId, "revive", 1, randomTime);
return false; return false;
} }
if (!GetEventValue(bot, "revive")) if (!GetEventValue(botId, "revive"))
{ {
Revive(player); Revive(bot);
return true; return true;
} }
@@ -1551,18 +1556,15 @@ bool RandomPlayerbotMgr::ProcessBot(Player* player)
} }
// leave group if leader is rndbot // leave group if leader is rndbot
Group* group = player->GetGroup(); Group* group = bot->GetGroup();
if (group && !group->isLFGGroup() && IsRandomBot(group->GetLeader())) if (group && !group->isLFGGroup() && IsRandomBot(group->GetLeader()))
{ {
player->RemoveFromGroup(); botAI->LeaveOrDisbandGroup();
LOG_INFO("playerbots", "Bot {} remove from group since leader is random bot.", player->GetName().c_str()); LOG_INFO("playerbots", "Bot {} remove from group since leader is random bot.", bot->GetName().c_str());
} }
// only randomize and teleport idle bots // only randomize and teleport idle bots
bool idleBot = false; bool idleBot = false;
PlayerbotAI* botAI = GET_PLAYERBOT_AI(player);
if (botAI)
{
if (TravelTarget* target = botAI->GetAiObjectContext()->GetValue<TravelTarget*>("travel target")->Get()) if (TravelTarget* target = botAI->GetAiObjectContext()->GetValue<TravelTarget*>("travel target")->Get())
{ {
if (target->getTravelState() == TravelState::TRAVEL_STATE_IDLE) if (target->getTravelState() == TravelState::TRAVEL_STATE_IDLE)
@@ -1574,11 +1576,11 @@ bool RandomPlayerbotMgr::ProcessBot(Player* player)
{ {
idleBot = true; idleBot = true;
} }
}
if (idleBot) if (idleBot)
{ {
// randomize // randomize
uint32 randomize = GetEventValue(bot, "randomize"); uint32 randomize = GetEventValue(botId, "randomize");
if (!randomize) if (!randomize)
{ {
// bool randomiser = true; // bool randomiser = true;
@@ -1602,12 +1604,12 @@ bool RandomPlayerbotMgr::ProcessBot(Player* player)
// } // }
// if (randomiser) // if (randomiser)
// { // {
Randomize(player); Randomize(bot);
LOG_DEBUG("playerbots", "Bot #{} {}:{} <{}>: randomized", bot, LOG_DEBUG("playerbots", "Bot #{} {}:{} <{}>: randomized", botId,
player->GetTeamId() == TEAM_ALLIANCE ? "A" : "H", player->GetLevel(), player->GetName()); bot->GetTeamId() == TEAM_ALLIANCE ? "A" : "H", bot->GetLevel(), bot->GetName());
uint32 randomTime = uint32 randomTime =
urand(sPlayerbotAIConfig->minRandomBotRandomizeTime, sPlayerbotAIConfig->maxRandomBotRandomizeTime); urand(sPlayerbotAIConfig->minRandomBotRandomizeTime, sPlayerbotAIConfig->maxRandomBotRandomizeTime);
ScheduleRandomize(bot, randomTime); ScheduleRandomize(botId, randomTime);
return true; return true;
} }
@@ -1619,15 +1621,15 @@ bool RandomPlayerbotMgr::ProcessBot(Player* player)
// return true; // return true;
// } // }
uint32 teleport = GetEventValue(bot, "teleport"); uint32 teleport = GetEventValue(botId, "teleport");
if (!teleport) if (!teleport)
{ {
LOG_DEBUG("playerbots", "Bot #{} <{}>: teleport for level and refresh", bot, player->GetName()); LOG_DEBUG("playerbots", "Bot #{} <{}>: teleport for level and refresh", botId, bot->GetName());
Refresh(player); Refresh(bot);
RandomTeleportForLevel(player); RandomTeleportForLevel(bot);
uint32 time = urand(sPlayerbotAIConfig->minRandomBotTeleportInterval, uint32 time = urand(sPlayerbotAIConfig->minRandomBotTeleportInterval,
sPlayerbotAIConfig->maxRandomBotTeleportInterval); sPlayerbotAIConfig->maxRandomBotTeleportInterval);
ScheduleTeleport(bot, time); ScheduleTeleport(botId, time);
return true; return true;
} }
} }
@@ -2377,6 +2379,10 @@ void RandomPlayerbotMgr::IncreaseLevel(Player* bot)
void RandomPlayerbotMgr::RandomizeFirst(Player* bot) void RandomPlayerbotMgr::RandomizeFirst(Player* bot)
{ {
PlayerbotAI* botAI = GET_PLAYERBOT_AI(bot);
if (!botAI)
return;
uint32 maxLevel = sPlayerbotAIConfig->randomBotMaxLevel; uint32 maxLevel = sPlayerbotAIConfig->randomBotMaxLevel;
if (maxLevel > sWorld->getIntConfig(CONFIG_MAX_PLAYER_LEVEL)) if (maxLevel > sWorld->getIntConfig(CONFIG_MAX_PLAYER_LEVEL))
maxLevel = sWorld->getIntConfig(CONFIG_MAX_PLAYER_LEVEL); maxLevel = sWorld->getIntConfig(CONFIG_MAX_PLAYER_LEVEL);
@@ -2434,7 +2440,6 @@ void RandomPlayerbotMgr::RandomizeFirst(Player* bot)
} }
SetValue(bot, "level", level); SetValue(bot, "level", level);
PlayerbotFactory factory(bot, level); PlayerbotFactory factory(bot, level);
factory.Randomize(false); factory.Randomize(false);
@@ -2456,11 +2461,10 @@ void RandomPlayerbotMgr::RandomizeFirst(Player* bot)
PlayerbotsDatabase.Execute(stmt); PlayerbotsDatabase.Execute(stmt);
// teleport to a random inn for bot level // teleport to a random inn for bot level
if (GET_PLAYERBOT_AI(bot)) botAI->Reset(true);
GET_PLAYERBOT_AI(bot)->Reset(true);
if (bot->GetGroup()) if (bot->GetGroup())
bot->RemoveFromGroup(); botAI->LeaveOrDisbandGroup();
if (pmo) if (pmo)
pmo->finish(); pmo->finish();
@@ -2470,12 +2474,13 @@ void RandomPlayerbotMgr::RandomizeFirst(Player* bot)
void RandomPlayerbotMgr::RandomizeMin(Player* bot) void RandomPlayerbotMgr::RandomizeMin(Player* bot)
{ {
PlayerbotAI* botAI = GET_PLAYERBOT_AI(bot);
if (!botAI)
return;
PerformanceMonitorOperation* pmo = sPerformanceMonitor->start(PERF_MON_RNDBOT, "RandomizeMin"); PerformanceMonitorOperation* pmo = sPerformanceMonitor->start(PERF_MON_RNDBOT, "RandomizeMin");
uint32 level = sPlayerbotAIConfig->randomBotMinLevel; uint32 level = sPlayerbotAIConfig->randomBotMinLevel;
SetValue(bot, "level", level); SetValue(bot, "level", level);
PlayerbotFactory factory(bot, level); PlayerbotFactory factory(bot, level);
factory.Randomize(false); factory.Randomize(false);
@@ -2497,11 +2502,10 @@ void RandomPlayerbotMgr::RandomizeMin(Player* bot)
PlayerbotsDatabase.Execute(stmt); PlayerbotsDatabase.Execute(stmt);
// teleport to a random inn for bot level // teleport to a random inn for bot level
if (GET_PLAYERBOT_AI(bot)) botAI->Reset(true);
GET_PLAYERBOT_AI(bot)->Reset(true);
if (bot->GetGroup()) if (bot->GetGroup())
bot->RemoveFromGroup(); botAI->LeaveOrDisbandGroup();
if (pmo) if (pmo)
pmo->finish(); pmo->finish();
@@ -2583,7 +2587,7 @@ void RandomPlayerbotMgr::Refresh(Player* bot)
bot->SetMoney(money + 500 * sqrt(urand(1, bot->GetLevel() * 5))); bot->SetMoney(money + 500 * sqrt(urand(1, bot->GetLevel() * 5)));
if (bot->GetGroup()) if (bot->GetGroup())
bot->RemoveFromGroup(); botAI->LeaveOrDisbandGroup();
if (pmo) if (pmo)
pmo->finish(); pmo->finish();

View File

@@ -72,8 +72,10 @@ bool UninviteAction::Execute(Event event)
bool LeaveGroupAction::Leave(Player* player) bool LeaveGroupAction::Leave(Player* player)
{ {
if (player && !GET_PLAYERBOT_AI(player) && if (player &&
!botAI &&
!botAI->GetSecurity()->CheckLevelFor(PLAYERBOT_SECURITY_INVITE, false, player)) !botAI->GetSecurity()->CheckLevelFor(PLAYERBOT_SECURITY_INVITE, false, player))
return false; return false;
bool aiMaster = GET_PLAYERBOT_AI(botAI->GetMaster()) != nullptr; bool aiMaster = GET_PLAYERBOT_AI(botAI->GetMaster()) != nullptr;
@@ -84,7 +86,7 @@ bool LeaveGroupAction::Leave(Player* player)
bool shouldStay = randomBot && bot->GetGroup() && player == bot; bool shouldStay = randomBot && bot->GetGroup() && player == bot;
if (!shouldStay) if (!shouldStay)
{ {
bot->RemoveFromGroup(); botAI->LeaveOrDisbandGroup();
} }
if (randomBot) if (randomBot)