From f23d4807a8dd4b2be6c189037611b0606696e5b5 Mon Sep 17 00:00:00 2001 From: Flekz Date: Thu, 1 Dec 2022 12:01:57 +0000 Subject: [PATCH] Add all strategy chat command to add/remove/query strategies for all engines --- playerbot/BotState.h | 10 +++ playerbot/PlayerbotAI.cpp | 90 +++++++++++++------ playerbot/PlayerbotAI.h | 12 +-- .../strategy/actions/ChangeStrategyAction.cpp | 39 ++++++++ .../strategy/actions/ChangeStrategyAction.h | 24 ++++- .../strategy/actions/ChatActionContext.h | 2 + .../generic/ChatCommandHandlerStrategy.cpp | 1 + .../strategy/triggers/ChatTriggerContext.h | 2 + 8 files changed, 140 insertions(+), 40 deletions(-) create mode 100644 playerbot/BotState.h diff --git a/playerbot/BotState.h b/playerbot/BotState.h new file mode 100644 index 000000000..cec054422 --- /dev/null +++ b/playerbot/BotState.h @@ -0,0 +1,10 @@ +#pragma once + +enum class BotState : uint8 +{ + BOT_STATE_COMBAT = 0, + BOT_STATE_NON_COMBAT = 1, + BOT_STATE_DEAD = 2, + BOT_STATE_REACTION = 3, + BOT_STATE_ALL +}; \ No newline at end of file diff --git a/playerbot/PlayerbotAI.cpp b/playerbot/PlayerbotAI.cpp index 9c66408fe..2ef78622c 100644 --- a/playerbot/PlayerbotAI.cpp +++ b/playerbot/PlayerbotAI.cpp @@ -92,7 +92,7 @@ void PacketHandlingHelper::AddPacket(const WorldPacket& packet) PlayerbotAI::PlayerbotAI() : PlayerbotAIBase(), bot(NULL), aiObjectContext(NULL), currentEngine(NULL), chatHelper(this), chatFilter(this), accountId(0), security(NULL), master(NULL), currentState(BotState::BOT_STATE_NON_COMBAT), faceTargetUpdateDelay(0) { - for (uint8 i = 0 ; i < (uint8)BotState::BOT_STATE_MAX; i++) + for (uint8 i = 0 ; i < (uint8)BotState::BOT_STATE_ALL; i++) engines[i] = NULL; for (int i = 0; i < MAX_ACTIVITY_TYPE; i++) @@ -124,7 +124,7 @@ PlayerbotAI::PlayerbotAI(Player* bot) : engines[(uint8)BotState::BOT_STATE_DEAD] = AiFactory::createDeadEngine(bot, this, aiObjectContext); engines[(uint8)BotState::BOT_STATE_REACTION] = reactionEngine = AiFactory::createReactionEngine(bot, this, aiObjectContext); - for (uint8 e = 0; e < (uint8)BotState::BOT_STATE_MAX; e++) + for (uint8 e = 0; e < (uint8)BotState::BOT_STATE_ALL; e++) { engines[e]->initMode = false; engines[e]->Init(); @@ -205,7 +205,7 @@ PlayerbotAI::PlayerbotAI(Player* bot) : PlayerbotAI::~PlayerbotAI() { - for (uint8 i = 0 ; i < (uint8)BotState::BOT_STATE_MAX; i++) + for (uint8 i = 0 ; i < (uint8)BotState::BOT_STATE_ALL; i++) { if (engines[i]) delete engines[i]; @@ -741,7 +741,7 @@ void PlayerbotAI::Reset(bool full) if (full) { - for (uint8 i = 0; i < (uint8)BotState::BOT_STATE_MAX; i++) + for (uint8 i = 0; i < (uint8)BotState::BOT_STATE_ALL; i++) { engines[i]->Init(); } @@ -1610,34 +1610,68 @@ void PlayerbotAI::ReInitCurrentEngine() void PlayerbotAI::ChangeStrategy(string names, BotState type) { - Engine* e = engines[(uint8)type]; - if (!e) - return; - - e->ChangeStrategy(names, BotStateToString(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->ChangeStrategy(names, BotStateToString(BotState(i))); + } + } + } + else + { + Engine* engine = engines[(uint8)type]; + if (engine) + { + engine->ChangeStrategy(names, BotStateToString(type)); + } + } } void PlayerbotAI::ClearStrategies(BotState type) { - Engine* e = engines[(uint8)type]; - if (!e) - return; - - e->removeAllStrategies(); + if (type == BotState::BOT_STATE_ALL) + { + for (uint8 i = 0; i < (uint8)BotState::BOT_STATE_ALL; i++) + { + Engine* engine = engines[i]; + if (engine) + { + engine->removeAllStrategies(); + } + } + } + else + { + Engine* engine = engines[(uint8)type]; + if (engine) + { + engine->removeAllStrategies(); + } + } } list PlayerbotAI::GetStrategies(BotState type) { - Engine* e = engines[(uint8)type]; - if (!e) - return list(); + // Can't get all strategies for all engines + if (type != BotState::BOT_STATE_ALL) + { + Engine* engine = engines[(uint8)type]; + if (engine) + { + return engine->GetStrategies(); + } + } - return e->GetStrategies(); + return list(); } bool PlayerbotAI::CanDoSpecificAction(string name, string qualifier, bool isPossible, bool isUseful) { - for (uint8 i = 0; i < (uint8)BotState::BOT_STATE_MAX; i++) + for (uint8 i = 0; i < (uint8)BotState::BOT_STATE_ALL; i++) { if(engines[i]->CanExecuteAction(name, qualifier, isPossible, isUseful)) { @@ -1650,7 +1684,7 @@ bool PlayerbotAI::CanDoSpecificAction(string name, string qualifier, bool isPoss bool PlayerbotAI::DoSpecificAction(string name, Event event, bool silent, string qualifier) { - for (uint8 i = 0 ; i < (uint8)BotState::BOT_STATE_MAX; i++) + for (uint8 i = 0 ; i < (uint8)BotState::BOT_STATE_ALL; i++) { ostringstream out; ActionResult res = engines[i]->ExecuteAction(name, event, qualifier); @@ -1723,7 +1757,7 @@ bool PlayerbotAI::PlayEmote(uint32 emote) bool PlayerbotAI::ContainsStrategy(StrategyType type) { - for (uint8 i = 0 ; i < (uint8)BotState::BOT_STATE_MAX; i++) + for (uint8 i = 0 ; i < (uint8)BotState::BOT_STATE_ALL; i++) { if(engines[i]) { @@ -1739,10 +1773,14 @@ bool PlayerbotAI::ContainsStrategy(StrategyType type) bool PlayerbotAI::HasStrategy(string name, BotState type) { - const uint8 typeIndex = (uint8)type; - if (engines[typeIndex]) + // Can't check the strategy for all engines at once + if(type != BotState::BOT_STATE_ALL) { - return engines[typeIndex]->HasStrategy(name); + const uint8 typeIndex = (uint8)type; + if (engines[typeIndex]) + { + return engines[typeIndex]->HasStrategy(name); + } } return false; @@ -1750,7 +1788,7 @@ bool PlayerbotAI::HasStrategy(string name, BotState type) void PlayerbotAI::ResetStrategies(bool load) { - for (uint8 i = 0; i < (uint8)BotState::BOT_STATE_MAX; i++) + for (uint8 i = 0; i < (uint8)BotState::BOT_STATE_ALL; i++) { engines[i]->initMode = true; engines[i]->removeAllStrategies(); @@ -1762,7 +1800,7 @@ void PlayerbotAI::ResetStrategies(bool load) AiFactory::AddDefaultReactionStrategies(bot, this, reactionEngine); if (load) sPlayerbotDbStore.Load(this); - for (uint8 i = 0; i < (uint8)BotState::BOT_STATE_MAX; i++) + for (uint8 i = 0; i < (uint8)BotState::BOT_STATE_ALL; i++) { engines[i]->initMode = false; engines[i]->Init(); diff --git a/playerbot/PlayerbotAI.h b/playerbot/PlayerbotAI.h index b981fe324..5bcd6f3c9 100644 --- a/playerbot/PlayerbotAI.h +++ b/playerbot/PlayerbotAI.h @@ -9,6 +9,7 @@ #include "ChatFilter.h" #include "PlayerbotSecurity.h" #include "PlayerbotTextMgr.h" +#include "BotState.h" #include class Player; @@ -82,15 +83,6 @@ enum HealingItemDisplayId MAJOR_DREAMLESS_SLEEP_POTION = 37845, }; -enum class BotState : uint8 -{ - BOT_STATE_COMBAT = 0, - BOT_STATE_NON_COMBAT = 1, - BOT_STATE_DEAD = 2, - BOT_STATE_REACTION = 3, - BOT_STATE_MAX -}; - enum RoguePoisonDisplayId { DEADLY_POISON_DISPLAYID = 13707, @@ -483,7 +475,7 @@ class PlayerbotAI : public PlayerbotAIBase AiObjectContext* aiObjectContext; Engine* currentEngine; ReactionEngine* reactionEngine; - Engine* engines[(uint8)BotState::BOT_STATE_MAX]; + Engine* engines[(uint8)BotState::BOT_STATE_ALL]; BotState currentState; ChatHelper chatHelper; queue chatCommands; diff --git a/playerbot/strategy/actions/ChangeStrategyAction.cpp b/playerbot/strategy/actions/ChangeStrategyAction.cpp index ce92cc7cd..f6698c640 100644 --- a/playerbot/strategy/actions/ChangeStrategyAction.cpp +++ b/playerbot/strategy/actions/ChangeStrategyAction.cpp @@ -78,5 +78,44 @@ bool ChangeReactionStrategyAction::Execute(Event& event) { string text = event.getParam(); ai->ChangeStrategy(text, BotState::BOT_STATE_REACTION); + return true; +} + +bool ChangeAllStrategyAction::Execute(Event& event) +{ + string text = event.getParam(); + string strategyName = text.empty() ? strategy : text; + + uint32 account = sObjectMgr.GetPlayerAccountIdByGUID(bot->GetObjectGuid()); + if (sPlayerbotAIConfig.IsInRandomAccountList(account) && ai->GetMaster() && ai->GetMaster()->GetSession()->GetSecurity() < SEC_GAMEMASTER) + { + if (strategyName.find("loot") != string::npos || strategyName.find("gather") != string::npos) + { + ai->TellError("You can change any strategy except loot and gather"); + return false; + } + } + + ai->ChangeStrategy(text, BotState::BOT_STATE_ALL); + + if (event.getSource() == "nc" || event.getSource() == "co") + { + vector splitted = split(text, ','); + for (vector::iterator i = splitted.begin(); i != splitted.end(); i++) + { + const char* name = i->c_str(); + switch (name[0]) + { + case '+': + case '-': + case '~': + sPlayerbotDbStore.Save(ai); + break; + case '?': + break; + } + } + } + return true; } \ No newline at end of file diff --git a/playerbot/strategy/actions/ChangeStrategyAction.h b/playerbot/strategy/actions/ChangeStrategyAction.h index aafa1d1d0..a91ee35d5 100644 --- a/playerbot/strategy/actions/ChangeStrategyAction.h +++ b/playerbot/strategy/actions/ChangeStrategyAction.h @@ -4,7 +4,8 @@ namespace ai { - class ChangeCombatStrategyAction : public Action { + class ChangeCombatStrategyAction : public Action + { public: ChangeCombatStrategyAction(PlayerbotAI* ai, string name = "co") : Action(ai, name) {} @@ -12,7 +13,8 @@ namespace ai virtual bool Execute(Event& event); }; - class ChangeNonCombatStrategyAction : public Action { + class ChangeNonCombatStrategyAction : public Action + { public: ChangeNonCombatStrategyAction(PlayerbotAI* ai) : Action(ai, "nc") {} @@ -20,7 +22,8 @@ namespace ai virtual bool Execute(Event& event); }; - class ChangeDeadStrategyAction : public Action { + class ChangeDeadStrategyAction : public Action + { public: ChangeDeadStrategyAction(PlayerbotAI* ai) : Action(ai, "de") {} @@ -28,11 +31,24 @@ namespace ai virtual bool Execute(Event& event); }; - class ChangeReactionStrategyAction : public Action { + class ChangeReactionStrategyAction : public Action + { public: ChangeReactionStrategyAction(PlayerbotAI* ai) : Action(ai, "react") {} public: virtual bool Execute(Event& event); }; + + class ChangeAllStrategyAction : public Action + { + public: + ChangeAllStrategyAction(PlayerbotAI* ai, string name = "change strategy from all", string strategy = "") : Action(ai, name), strategy(strategy) {} + + public: + virtual bool Execute(Event& event); + + private: + string strategy; + }; } diff --git a/playerbot/strategy/actions/ChatActionContext.h b/playerbot/strategy/actions/ChatActionContext.h index 78f0da659..8ed9d2466 100644 --- a/playerbot/strategy/actions/ChatActionContext.h +++ b/playerbot/strategy/actions/ChatActionContext.h @@ -112,6 +112,7 @@ namespace ai creators["nc"] = &ChatActionContext::nc; creators["de"] = &ChatActionContext::dead; creators["react"] = &ChatActionContext::react; + creators["all"] = &ChatActionContext::all; creators["trainer"] = &ChatActionContext::trainer; creators["attack my target"] = &ChatActionContext::attack_my_target; creators["chat"] = &ChatActionContext::chat; @@ -220,6 +221,7 @@ namespace ai static Action* nc(PlayerbotAI* ai) { return new ChangeNonCombatStrategyAction(ai); } static Action* dead(PlayerbotAI* ai) { return new ChangeDeadStrategyAction(ai); } static Action* react(PlayerbotAI* ai) { return new ChangeReactionStrategyAction(ai); } + static Action* all(PlayerbotAI* ai) { return new ChangeAllStrategyAction(ai); } static Action* spells(PlayerbotAI* ai) { return new ListSpellsAction(ai); } static Action* talents(PlayerbotAI* ai) { return new ChangeTalentsAction(ai); } diff --git a/playerbot/strategy/generic/ChatCommandHandlerStrategy.cpp b/playerbot/strategy/generic/ChatCommandHandlerStrategy.cpp index 07de41566..3bf2348b3 100644 --- a/playerbot/strategy/generic/ChatCommandHandlerStrategy.cpp +++ b/playerbot/strategy/generic/ChatCommandHandlerStrategy.cpp @@ -185,6 +185,7 @@ ChatCommandHandlerStrategy::ChatCommandHandlerStrategy(PlayerbotAI* ai) : PassTr supported.push_back("nc"); supported.push_back("de"); supported.push_back("react"); + supported.push_back("all"); supported.push_back("trainer"); supported.push_back("chat"); supported.push_back("home"); diff --git a/playerbot/strategy/triggers/ChatTriggerContext.h b/playerbot/strategy/triggers/ChatTriggerContext.h index 65d8d60a0..101ccf479 100644 --- a/playerbot/strategy/triggers/ChatTriggerContext.h +++ b/playerbot/strategy/triggers/ChatTriggerContext.h @@ -47,6 +47,7 @@ namespace ai creators["nc"] = &ChatTriggerContext::nc; creators["de"] = &ChatTriggerContext::dead; creators["react"] = &ChatTriggerContext::react; + creators["all"] = &ChatTriggerContext::all; creators["trainer"] = &ChatTriggerContext::trainer; creators["attack"] = &ChatTriggerContext::attack; creators["pull"] = &ChatTriggerContext::pull; @@ -168,6 +169,7 @@ namespace ai static Trigger* nc(PlayerbotAI* ai) { return new ChatCommandTrigger(ai, "nc"); } static Trigger* dead(PlayerbotAI* ai) { return new ChatCommandTrigger(ai, "de"); } static Trigger* react(PlayerbotAI* ai) { return new ChatCommandTrigger(ai, "react"); } + static Trigger* all(PlayerbotAI* ai) { return new ChatCommandTrigger(ai, "all"); } static Trigger* spells(PlayerbotAI* ai) { return new ChatCommandTrigger(ai, "spells"); } static Trigger* talents(PlayerbotAI* ai) { return new ChatCommandTrigger(ai, "talents"); } static Trigger* equip(PlayerbotAI* ai) { return new ChatCommandTrigger(ai, "e"); }