Skip to content

Commit

Permalink
If the bot gets pushed away while on the stay strategy, they will mov…
Browse files Browse the repository at this point in the history
…e back to the original position (if possible)
  • Loading branch information
davidonete committed Nov 28, 2022
1 parent 8f4570a commit 066c800
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 12 deletions.
2 changes: 2 additions & 0 deletions playerbot/strategy/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,7 @@ void Engine::addStrategy(string name)

LogAction("S:+%s", strategy->getName().c_str());
strategies[strategy->getName()] = strategy;
strategy->OnStrategyAdded();
}
if(!initMode)
Init();
Expand Down Expand Up @@ -497,6 +498,7 @@ bool Engine::removeStrategy(string name)
return false;

LogAction("S:-%s", name.c_str());
i->second->OnStrategyRemoved();
strategies.erase(i);
Init();
return true;
Expand Down
3 changes: 3 additions & 0 deletions playerbot/strategy/Strategy.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ namespace ai
void Update() {}
void Reset() {}

virtual void OnStrategyAdded() {}
virtual void OnStrategyRemoved() {}

protected:
NamedObjectFactoryList<ActionNode> actionNodeFactories;
};
Expand Down
2 changes: 2 additions & 0 deletions playerbot/strategy/actions/ActionContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ namespace ai
creators["move to loot"] = &ActionContext::move_to_loot;
creators["open loot"] = &ActionContext::open_loot;
creators["guard"] = &ActionContext::guard;
creators["return to stay position"] = &ActionContext::return_to_stay_position;
creators["move out of enemy contact"] = &ActionContext::move_out_of_enemy_contact;
creators["set facing"] = &ActionContext::set_facing;
creators["set behind"] = &ActionContext::set_behind;
Expand Down Expand Up @@ -284,6 +285,7 @@ namespace ai
static Action* select_new_target(PlayerbotAI* ai) { return new SelectNewTargetAction(ai); }
static Action* attack_duel_opponent(PlayerbotAI* ai) { return new AttackDuelOpponentAction(ai); }
static Action* guard(PlayerbotAI* ai) { return new GuardAction(ai); }
static Action* return_to_stay_position(PlayerbotAI* ai) { return new ReturnToStayPositionAction(ai); }
static Action* open_loot(PlayerbotAI* ai) { return new OpenLootAction(ai); }
static Action* move_to_loot(PlayerbotAI* ai) { return new MoveToLootAction(ai); }
static Action* _return(PlayerbotAI* ai) { return new ReturnAction(ai); }
Expand Down
8 changes: 4 additions & 4 deletions playerbot/strategy/actions/ChatShortcutActions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ bool FollowChatShortcutAction::Execute(Event& event)
return false;

ai->Reset();
ai->ChangeStrategy("+follow,-passive", BotState::BOT_STATE_NON_COMBAT);
ai->ChangeStrategy("-follow,-passive", BotState::BOT_STATE_COMBAT);
ai->ChangeStrategy("+follow,-passive,-stay", BotState::BOT_STATE_NON_COMBAT);
ai->ChangeStrategy("+follow,-passive,-stay", BotState::BOT_STATE_COMBAT);

ai::PositionMap& posMap = context->GetValue<ai::PositionMap&>("position")->Get();
ai::PositionEntry pos = posMap["return"];
Expand Down Expand Up @@ -88,8 +88,8 @@ bool StayChatShortcutAction::Execute(Event& event)
return false;

ai->Reset();
ai->ChangeStrategy("+stay,-passive", BotState::BOT_STATE_NON_COMBAT);
ai->ChangeStrategy("-follow,-passive", BotState::BOT_STATE_COMBAT);
ai->ChangeStrategy("+stay,-follow,-passive", BotState::BOT_STATE_NON_COMBAT);
ai->ChangeStrategy("+stay,-follow,-passive", BotState::BOT_STATE_COMBAT);

SetReturnPosition(bot->GetPositionX(), bot->GetPositionY(), bot->GetPositionZ());

Expand Down
22 changes: 22 additions & 0 deletions playerbot/strategy/actions/PositionAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,25 @@ bool ReturnAction::isUseful()
ai::PositionEntry pos = context->GetValue<ai::PositionMap&>("position")->Get()[qualifier];
return pos.isSet() && AI_VALUE2(float, "distance", "position_random") > sPlayerbotAIConfig.followDistance;
}

bool ReturnToStayPositionAction::isPossible()
{
PositionEntry stayPosition = context->GetValue<ai::PositionMap&>("position")->Get()["stay position"];
if (stayPosition.isSet())
{
const float distance = bot->GetDistance(stayPosition.x, stayPosition.y, stayPosition.z);
if (distance > sPlayerbotAIConfig.reactDistance)
{
ai->TellError("The stay position is too far to return. I am going to stay where I am now");

// Set the stay position to current position
stayPosition.Set(bot->GetPositionX(), bot->GetPositionY(), bot->GetPositionZ(), bot->GetMapId());
PositionMap& posMap = AI_VALUE(PositionMap&, "position");
posMap["stay position"] = stayPosition;
}

return true;
}

return false;
}
7 changes: 7 additions & 0 deletions playerbot/strategy/actions/PositionAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,11 @@ namespace ai
virtual bool isUseful();
};

class ReturnToStayPositionAction : public MoveToPositionAction
{
public:
ReturnToStayPositionAction(PlayerbotAI* ai) : MoveToPositionAction(ai, "move to position", "stay position") {}

virtual bool isPossible();
};
}
10 changes: 6 additions & 4 deletions playerbot/strategy/actions/StayActions.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,28 @@

namespace ai
{
class StayActionBase : public MovementAction {
class StayActionBase : public MovementAction
{
public:
StayActionBase(PlayerbotAI* ai, string name) : MovementAction(ai, name) {}

protected:
bool Stay();
};

class StayAction : public StayActionBase {
class StayAction : public StayActionBase
{
public:
StayAction(PlayerbotAI* ai) : StayActionBase(ai, "stay") {}
virtual bool Execute(Event& event);
virtual bool isUseful();
};

class SitAction : public StayActionBase {
class SitAction : public StayActionBase
{
public:
SitAction(PlayerbotAI* ai) : StayActionBase(ai, "sit") {}
virtual bool Execute(Event& event);
virtual bool isUseful();
};

}
33 changes: 33 additions & 0 deletions playerbot/strategy/generic/StayStrategy.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "botpch.h"
#include "../../playerbot.h"
#include "../values/PositionValue.h"
#include "StayStrategy.h"

using namespace ai;
Expand All @@ -9,6 +10,38 @@ NextAction** StayStrategy::getDefaultActions()
return NextAction::array(0, new NextAction("stay", 1.0f), NULL);
}

void StayStrategy::InitTriggers(std::list<TriggerNode*>& triggers)
{
triggers.push_back(new TriggerNode(
"return to stay position",
NextAction::array(0, new NextAction("return to stay position", 1.5f), NULL)));
}

void StayStrategy::OnStrategyAdded()
{
// Set the stay position to current position
AiObjectContext* context = ai->GetAiObjectContext();

Player* bot = ai->GetBot();
PositionEntry stayPosition = AI_VALUE(PositionMap&, "position")["stay position"];
stayPosition.Set(bot->GetPositionX(), bot->GetPositionY(), bot->GetPositionZ(), bot->GetMapId());

PositionMap& posMap = AI_VALUE(PositionMap&, "position");
posMap["stay position"] = stayPosition;
}

void StayStrategy::OnStrategyRemoved()
{
// Remove the saved stay position
AiObjectContext* context = ai->GetAiObjectContext();
PositionEntry stayPosition = AI_VALUE(PositionMap&, "position")["stay position"];
if (stayPosition.isSet())
{
PositionMap& posMap = AI_VALUE(PositionMap&, "position");
posMap.erase("stay position");
}
}

void SitStrategy::InitTriggers(std::list<TriggerNode*> &triggers)
{
triggers.push_back(new TriggerNode(
Expand Down
9 changes: 6 additions & 3 deletions playerbot/strategy/generic/StayStrategy.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@

namespace ai
{
class StayStrategy : public NonCombatStrategy
class StayStrategy : public Strategy
{
public:
StayStrategy(PlayerbotAI* ai) : NonCombatStrategy(ai) {}
StayStrategy(PlayerbotAI* ai) : Strategy(ai) {}
virtual string getName() { return "stay"; }
virtual void InitTriggers(std::list<TriggerNode*>& triggers);
virtual NextAction** getDefaultActions();

void OnStrategyAdded() override;
void OnStrategyRemoved() override;
};

class SitStrategy : public NonCombatStrategy
Expand All @@ -18,5 +22,4 @@ namespace ai
virtual string getName() { return "sit"; }
virtual void InitTriggers(std::list<TriggerNode*> &triggers);
};

}
15 changes: 14 additions & 1 deletion playerbot/strategy/triggers/GenericTriggers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "GenericTriggers.h"
#include "../../LootObjectStack.h"
#include "../../PlayerbotAIConfig.h"
#include "../values/PositionValue.h"

using namespace ai;

Expand Down Expand Up @@ -494,4 +495,16 @@ bool IsFallingFarTrigger::IsActive()
bool HasAreaDebuffTrigger::IsActive()
{
return AI_VALUE2(bool, "has area debuff", "self target");
}
}

bool ReturnToStayPositionTrigger::IsActive()
{
PositionEntry stayPosition = context->GetValue<ai::PositionMap&>("position")->Get()["stay position"];
if (stayPosition.isSet())
{
const float distance = bot->GetDistance(stayPosition.x, stayPosition.y, stayPosition.z);
return distance > sPlayerbotAIConfig.followDistance;
}

return false;
}
8 changes: 8 additions & 0 deletions playerbot/strategy/triggers/GenericTriggers.h
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,14 @@ namespace ai
ReturnTrigger(PlayerbotAI* ai) : StayTimeTrigger(ai, sPlayerbotAIConfig.returnDelay, "return") {}
};

class ReturnToStayPositionTrigger : public Trigger
{
public:
ReturnToStayPositionTrigger(PlayerbotAI* ai) : Trigger(ai, "return to stay position", 2) {}

virtual bool IsActive();
};

class GiveItemTrigger : public Trigger
{
public:
Expand Down
2 changes: 2 additions & 0 deletions playerbot/strategy/triggers/TriggerContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace ai
{
creators["return"] = &TriggerContext::_return;
creators["sit"] = &TriggerContext::sit;
creators["return to stay position"] = &TriggerContext::return_to_stay_position;
creators["collision"] = &TriggerContext::collision;

creators["timer"] = &TriggerContext::Timer;
Expand Down Expand Up @@ -252,6 +253,7 @@ namespace ai
static Trigger* give_water(PlayerbotAI* ai) { return new GiveWaterTrigger(ai); }
static Trigger* no_rti(PlayerbotAI* ai) { return new NoRtiTrigger(ai); }
static Trigger* _return(PlayerbotAI* ai) { return new ReturnTrigger(ai); }
static Trigger* return_to_stay_position(PlayerbotAI* ai) { return new ReturnToStayPositionTrigger(ai); }
static Trigger* sit(PlayerbotAI* ai) { return new SitTrigger(ai); }
static Trigger* far_from_rpg_target(PlayerbotAI* ai) { return new FarFromRpgTargetTrigger(ai); }
static Trigger* near_rpg_target(PlayerbotAI* ai) { return new NearRpgTargetTrigger(ai); }
Expand Down

0 comments on commit 066c800

Please sign in to comment.