Skip to content

Commit

Permalink
Add all strategy chat command to add/remove/query strategies for all …
Browse files Browse the repository at this point in the history
…engines
  • Loading branch information
davidonete committed Dec 1, 2022
1 parent b8d4852 commit f23d480
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 40 deletions.
10 changes: 10 additions & 0 deletions playerbot/BotState.h
Original file line number Diff line number Diff line change
@@ -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
};
90 changes: 64 additions & 26 deletions playerbot/PlayerbotAI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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<string> PlayerbotAI::GetStrategies(BotState type)
{
Engine* e = engines[(uint8)type];
if (!e)
return list<string>();
// 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<string>();
}

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))
{
Expand All @@ -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);
Expand Down Expand Up @@ -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])
{
Expand All @@ -1739,18 +1773,22 @@ 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;
}

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();
Expand All @@ -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();
Expand Down
12 changes: 2 additions & 10 deletions playerbot/PlayerbotAI.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "ChatFilter.h"
#include "PlayerbotSecurity.h"
#include "PlayerbotTextMgr.h"
#include "BotState.h"
#include <stack>

class Player;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<ChatCommandHolder> chatCommands;
Expand Down
39 changes: 39 additions & 0 deletions playerbot/strategy/actions/ChangeStrategyAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> splitted = split(text, ',');
for (vector<string>::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;
}
24 changes: 20 additions & 4 deletions playerbot/strategy/actions/ChangeStrategyAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,51 @@

namespace ai
{
class ChangeCombatStrategyAction : public Action {
class ChangeCombatStrategyAction : public Action
{
public:
ChangeCombatStrategyAction(PlayerbotAI* ai, string name = "co") : Action(ai, name) {}

public:
virtual bool Execute(Event& event);
};

class ChangeNonCombatStrategyAction : public Action {
class ChangeNonCombatStrategyAction : public Action
{
public:
ChangeNonCombatStrategyAction(PlayerbotAI* ai) : Action(ai, "nc") {}

public:
virtual bool Execute(Event& event);
};

class ChangeDeadStrategyAction : public Action {
class ChangeDeadStrategyAction : public Action
{
public:
ChangeDeadStrategyAction(PlayerbotAI* ai) : Action(ai, "de") {}

public:
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;
};
}
2 changes: 2 additions & 0 deletions playerbot/strategy/actions/ChatActionContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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); }

Expand Down
1 change: 1 addition & 0 deletions playerbot/strategy/generic/ChatCommandHandlerStrategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 2 additions & 0 deletions playerbot/strategy/triggers/ChatTriggerContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"); }
Expand Down

0 comments on commit f23d480

Please sign in to comment.