Skip to content

Commit

Permalink
Fix actions to use the requester instead of the master
Browse files Browse the repository at this point in the history
  • Loading branch information
davidonete committed Nov 26, 2023
1 parent ea697d6 commit da55660
Show file tree
Hide file tree
Showing 112 changed files with 1,118 additions and 898 deletions.
58 changes: 40 additions & 18 deletions playerbot/PlayerbotAI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1907,7 +1907,7 @@ void PlayerbotAI::ChangeStrategy(const string& names, BotState type)
Engine* engine = engines[i];
if (engine)
{
engine->ChangeStrategy(names, BotStateToString(BotState(i)));
engine->ChangeStrategy(names);
}
}
}
Expand All @@ -1916,7 +1916,30 @@ void PlayerbotAI::ChangeStrategy(const string& names, BotState type)
Engine* engine = engines[(uint8)type];
if (engine)
{
engine->ChangeStrategy(names, BotStateToString(type));
engine->ChangeStrategy(names);
}
}
}

void PlayerbotAI::PrintStrategies(Player* requester, BotState type)
{
if (type == BotState::BOT_STATE_ALL)
{
for (uint8 i = 0; i < (uint8)BotState::BOT_STATE_ALL; i++)
{
Engine* engine = engines[i];
if (engine)
{
engine->PrintStrategies(requester, BotStateToString(BotState(i)));
}
}
}
else
{
Engine* engine = engines[(uint8)type];
if (engine)
{
engine->PrintStrategies(requester, BotStateToString(type));
}
}
}
Expand Down Expand Up @@ -1978,6 +2001,7 @@ bool PlayerbotAI::CanDoSpecificAction(const string& name, bool isUseful, bool is

bool PlayerbotAI::DoSpecificAction(const string& name, Event event, bool silent)
{
Player* requester = event.getOwner() ? event.getOwner() : GetMaster();
for (uint8 i = 0 ; i < (uint8)BotState::BOT_STATE_ALL; i++)
{
Engine* engine = engines[i];
Expand All @@ -2002,7 +2026,7 @@ bool PlayerbotAI::DoSpecificAction(const string& name, Event event, bool silent)
{
ostringstream out;
out << name << ": impossible";
TellError(out.str());
TellError(requester, out.str());
PlaySound(TEXTEMOTE_NO);
}

Expand All @@ -2015,7 +2039,7 @@ bool PlayerbotAI::DoSpecificAction(const string& name, Event event, bool silent)
{
ostringstream out;
out << name << ": useless";
TellError(out.str());
TellError(requester, out.str());
PlaySound(TEXTEMOTE_NO);
}

Expand All @@ -2028,7 +2052,7 @@ bool PlayerbotAI::DoSpecificAction(const string& name, Event event, bool silent)
{
ostringstream out;
out << name << ": failed";
TellError(out.str());
TellError(requester, out.str());
}

return false;
Expand All @@ -2040,7 +2064,7 @@ bool PlayerbotAI::DoSpecificAction(const string& name, Event event, bool silent)
{
ostringstream out;
out << name << ": unknown action";
TellError(out.str());
TellError(requester, out.str());
}

return false;
Expand All @@ -2053,7 +2077,7 @@ bool PlayerbotAI::DoSpecificAction(const string& name, Event event, bool silent)
{
ostringstream out;
out << name << ": engine not ready";
TellError(out.str());
TellError(requester, out.str());
}
}
}
Expand Down Expand Up @@ -2379,7 +2403,7 @@ bool PlayerbotAI::TellPlayerNoFacing(Player* player, string text, PlayerbotSecur

return true;
case CHAT_MSG_WHISPER:
if (!IsTellAllowed(securityLevel))
if (!IsTellAllowed(player, securityLevel))
return false;

if (!HasRealPlayerMaster())
Expand All @@ -2401,30 +2425,28 @@ bool PlayerbotAI::TellPlayerNoFacing(Player* player, string text, PlayerbotSecur
return true;
}

bool PlayerbotAI::TellError(string text, PlayerbotSecurityLevel securityLevel)
bool PlayerbotAI::TellError(Player* player, string text, PlayerbotSecurityLevel securityLevel)
{
Player* master = GetMaster();
if (!IsTellAllowed(securityLevel) || !IsSafe(master) || master->GetPlayerbotAI())
if (!IsTellAllowed(player, securityLevel) || !IsSafe(player) || player->GetPlayerbotAI())
return false;

PlayerbotMgr* mgr = master->GetPlayerbotMgr();
PlayerbotMgr* mgr = player->GetPlayerbotMgr();
if (mgr) mgr->TellError(bot->GetName(), text);

return false;
}

bool PlayerbotAI::IsTellAllowed(PlayerbotSecurityLevel securityLevel)
bool PlayerbotAI::IsTellAllowed(Player* player, PlayerbotSecurityLevel securityLevel)
{
Player* master = GetMaster();
if (!master || master->IsBeingTeleported())
if (!player || player->IsBeingTeleported())
return false;

if (!GetSecurity()->CheckLevelFor(securityLevel, true, master))
if (!GetSecurity()->CheckLevelFor(securityLevel, true, player))
return false;

if (sPlayerbotAIConfig.whisperDistance && !bot->GetGroup() && sRandomPlayerbotMgr.IsFreeBot(bot) &&
master->GetSession()->GetSecurity() < SEC_GAMEMASTER &&
(bot->GetMapId() != master->GetMapId() || sServerFacade.GetDistance2d(bot, master) > sPlayerbotAIConfig.whisperDistance))
player->GetSession()->GetSecurity() < SEC_GAMEMASTER &&
(bot->GetMapId() != player->GetMapId() || sServerFacade.GetDistance2d(bot, player) > sPlayerbotAIConfig.whisperDistance))
return false;

return true;
Expand Down
5 changes: 3 additions & 2 deletions playerbot/PlayerbotAI.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ class PlayerbotAI : public PlayerbotAIBase
bool CanDoSpecificAction(const string& name, bool isUseful = true, bool isPossible = true);
virtual bool DoSpecificAction(const string& name, Event event = Event(), bool silent = false);
void ChangeStrategy(const string& name, BotState type);
void PrintStrategies(Player* requester, BotState type);
void ClearStrategies(BotState type);
list<string_view> GetStrategies(BotState type);
bool ContainsStrategy(StrategyType type);
Expand All @@ -338,7 +339,7 @@ class PlayerbotAI : public PlayerbotAIBase
bool TellPlayer(Player* player, string text, PlayerbotSecurityLevel securityLevel = PlayerbotSecurityLevel::PLAYERBOT_SECURITY_ALLOW_ALL, bool isPrivate = true);
bool TellPlayerNoFacing(Player* player, ostringstream& stream, PlayerbotSecurityLevel securityLevel = PlayerbotSecurityLevel::PLAYERBOT_SECURITY_ALLOW_ALL, bool isPrivate = true, bool noRepeat = true) { return TellPlayerNoFacing(player, stream.str(), securityLevel, isPrivate, noRepeat); }
bool TellPlayerNoFacing(Player* player, string text, PlayerbotSecurityLevel securityLevel = PlayerbotSecurityLevel::PLAYERBOT_SECURITY_ALLOW_ALL, bool isPrivate = true, bool noRepeat = true);
bool TellError(string text, PlayerbotSecurityLevel securityLevel = PlayerbotSecurityLevel::PLAYERBOT_SECURITY_ALLOW_ALL);
bool TellError(Player* player, string text, PlayerbotSecurityLevel securityLevel = PlayerbotSecurityLevel::PLAYERBOT_SECURITY_ALLOW_ALL);
void SpellInterrupted(uint32 spellid);
int32 CalculateGlobalCooldown(uint32 spellid);
void InterruptSpell();
Expand Down Expand Up @@ -436,7 +437,7 @@ class PlayerbotAI : public PlayerbotAIBase

private:
void _fillGearScoreData(Player *player, Item* item, std::vector<uint32>* gearScore, uint32& twoHandScore);
bool IsTellAllowed(PlayerbotSecurityLevel securityLevel = PlayerbotSecurityLevel::PLAYERBOT_SECURITY_ALLOW_ALL);
bool IsTellAllowed(Player* player, PlayerbotSecurityLevel securityLevel = PlayerbotSecurityLevel::PLAYERBOT_SECURITY_ALLOW_ALL);

public:
Player* GetBot() { return bot; }
Expand Down
18 changes: 9 additions & 9 deletions playerbot/strategy/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ void Engine::LogAction(const char* format, ...)
}
}

void Engine::ChangeStrategy(const string& names, string engineType)
void Engine::ChangeStrategy(const string& names)
{
vector<string> splitted = split(names, ',');
for (vector<string>::iterator i = splitted.begin(); i != splitted.end(); i++)
Expand All @@ -808,18 +808,18 @@ void Engine::ChangeStrategy(const string& names, string engineType)
toggleStrategy(name+1);
break;
}
case '?':
{
string engineStrategies = engineType;
engineStrategies.append(" Strategies: ");
engineStrategies.append(ListStrategies());
ai->TellPlayer(ai->GetMaster(), engineStrategies);
break;
}
}
}
}

void Engine::PrintStrategies(Player* requester, const string& engineType)
{
string engineStrategies = engineType;
engineStrategies.append(" Strategies: ");
engineStrategies.append(ListStrategies());
ai->TellPlayer(requester, engineStrategies);
}

void Engine::LogValues()
{
if (testMode)
Expand Down
5 changes: 3 additions & 2 deletions playerbot/strategy/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ namespace ai
std::string ListStrategies();
list<string_view> GetStrategies();
bool ContainsStrategy(StrategyType type);
void ChangeStrategy(const string& names, string engineType = "");
string GetLastAction() { return lastAction; }
void ChangeStrategy(const string& names);
void PrintStrategies(Player* requester, const string& engineType);
string GetLastAction() { return lastAction; }
const Action* GetLastExecutedAction() const { return lastExecutedAction; }

public:
Expand Down
6 changes: 4 additions & 2 deletions playerbot/strategy/actions/AcceptInvitationAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

namespace ai
{
class AcceptInvitationAction : public Action {
class AcceptInvitationAction : public Action
{
public:
AcceptInvitationAction(PlayerbotAI* ai) : Action(ai, "accept invitation") {}

virtual bool Execute(Event& event)
{
Player* requester = event.getOwner() ? event.getOwner() : GetMaster();
Group* grp = bot->GetGroupInvite();
if (!grp)
return false;
Expand Down Expand Up @@ -74,7 +76,7 @@ namespace ai
value->Load(masterFormation->getName());
}

ai->TellPlayer(GetMaster(), BOT_TEXT("hello"), PlayerbotSecurityLevel::PLAYERBOT_SECURITY_ALLOW_ALL, false);
ai->TellPlayer(requester, BOT_TEXT("hello"), PlayerbotSecurityLevel::PLAYERBOT_SECURITY_ALLOW_ALL, false);

ai->DoSpecificAction("reset raids", event, true);
ai->DoSpecificAction("update gear", event, true);
Expand Down
16 changes: 8 additions & 8 deletions playerbot/strategy/actions/AcceptQuestAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ bool AcceptQuestAction::Execute(Event& event)

bool AcceptQuestShareAction::Execute(Event& event)
{
Player* master = GetMaster();
Player *bot = ai->GetBot();
Player* requester = event.getOwner() ? event.getOwner() : GetMaster();
Player* bot = ai->GetBot();

if (!ai->IsSafe(master))
master = bot;
if (!ai->IsSafe(requester))
requester = bot;

WorldPacket& p = event.getPacket();
p.rpos(0);
Expand All @@ -103,21 +103,21 @@ bool AcceptQuestShareAction::Execute(Event& event)
{
// can't take quest
bot->SetDividerGuid( ObjectGuid() );
ai->TellError(BOT_TEXT("quest_cant_take"));
ai->TellError(requester, BOT_TEXT("quest_cant_take"));

return false;
}

if( !bot->GetDividerGuid().IsEmpty() )
{
// send msg to quest giving player
master->SendPushToPartyResponse( bot, QUEST_PARTY_MSG_ACCEPT_QUEST );
requester->SendPushToPartyResponse( bot, QUEST_PARTY_MSG_ACCEPT_QUEST );
bot->SetDividerGuid( ObjectGuid() );
}

if( bot->CanAddQuest( qInfo, false ) )
{
bot->AddQuest( qInfo, master );
bot->AddQuest( qInfo, requester);

sPlayerbotAIConfig.logEvent(ai, "AcceptQuestShareAction", qInfo->GetTitle(), to_string(qInfo->GetQuestId()));

Expand All @@ -140,7 +140,7 @@ bool AcceptQuestShareAction::Execute(Event& event)
);
}

ai->TellPlayer(master, BOT_TEXT("quest_accept"), PlayerbotSecurityLevel::PLAYERBOT_SECURITY_ALLOW_ALL, false);
ai->TellPlayer(requester, BOT_TEXT("quest_accept"), PlayerbotSecurityLevel::PLAYERBOT_SECURITY_ALLOW_ALL, false);
return true;
}

Expand Down
21 changes: 11 additions & 10 deletions playerbot/strategy/actions/AddLootAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@ bool AddLootAction::Execute(Event& event)

bool AddAllLootAction::Execute(Event& event)
{
Player* requester = event.getOwner() ? event.getOwner() : GetMaster();
bool added = false;

list<ObjectGuid> gos = context->GetValue<list<ObjectGuid> >("nearest game objects")->Get();
for (list<ObjectGuid>::iterator i = gos.begin(); i != gos.end(); i++)
added |= AddLoot(*i);
added |= AddLoot(requester, *i);

list<ObjectGuid> corpses = context->GetValue<list<ObjectGuid> >("nearest corpses")->Get();
for (list<ObjectGuid>::iterator i = corpses.begin(); i != corpses.end(); i++)
added |= AddLoot(*i);
added |= AddLoot(requester, *i);

return added;
}
Expand All @@ -49,7 +50,7 @@ bool AddAllLootAction::isUseful()
return true;
}

bool AddAllLootAction::AddLoot(ObjectGuid guid)
bool AddAllLootAction::AddLoot(Player* requester, ObjectGuid guid)
{
LootObject loot(bot, guid);

Expand All @@ -63,12 +64,12 @@ bool AddAllLootAction::AddLoot(ObjectGuid guid)
if (ai->HasRealPlayerMaster())
{
bool inDungeon = false;
if (ai->GetMaster()->IsInWorld() &&
ai->GetMaster()->GetMap()->IsDungeon() &&
bot->GetMapId() == ai->GetMaster()->GetMapId())
if (requester->IsInWorld() &&
requester->GetMap()->IsDungeon() &&
bot->GetMapId() == requester->GetMapId())
inDungeon = true;

if (inDungeon && sServerFacade.IsDistanceGreaterThan(sServerFacade.GetDistance2d(ai->GetMaster(), wo), sPlayerbotAIConfig.lootDistance))
if (inDungeon && sServerFacade.IsDistanceGreaterThan(sServerFacade.GetDistance2d(requester, wo), sPlayerbotAIConfig.lootDistance))
return false;

if (Group* group = bot->GetGroup())
Expand All @@ -81,7 +82,7 @@ bool AddAllLootAction::AddLoot(ObjectGuid guid)
return AI_VALUE(LootObjectStack*, "available loot")->Add(guid);
}

bool AddGatheringLootAction::AddLoot(ObjectGuid guid)
bool AddGatheringLootAction::AddLoot(Player* requester, ObjectGuid guid)
{
LootObject loot(bot, guid);

Expand All @@ -108,10 +109,10 @@ bool AddGatheringLootAction::AddLoot(ObjectGuid guid)
{
ostringstream out;
out << "Kill that " << targets.front()->GetName() << " so I can loot freely";
ai->TellError(out.str());
ai->TellError(requester, out.str());
return false;
}
}

return AddAllLootAction::AddLoot(guid);
return AddAllLootAction::AddLoot(requester, guid);
}
4 changes: 2 additions & 2 deletions playerbot/strategy/actions/AddLootAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace ai
virtual bool isUseful();

protected:
virtual bool AddLoot(ObjectGuid guid);
virtual bool AddLoot(Player* requester, ObjectGuid guid);
virtual bool Execute(Event& event) override;
};

Expand All @@ -30,6 +30,6 @@ namespace ai
AddGatheringLootAction(PlayerbotAI* ai) : AddAllLootAction(ai, "add gathering loot") {}

private:
bool AddLoot(ObjectGuid guid) override;
bool AddLoot(Player* requester, ObjectGuid guid) override;
};
}
Loading

0 comments on commit da55660

Please sign in to comment.