Files
mod-playerbots/src/strategy/druid/DruidActions.h
Nicolas Lebacq c9cc4324d3 NextAction refactoring to eliminate sentinel arrays and pointers (#1923)
<!--
> [!WARNING]
> **This is a DRAFT PR.**
> The structure is not definitive. The code might not be optimised yet.
It might not even start nor compile yet.
> **Don't panic.  It's going to be ok. 👌 We can make modifications, we
can fix things.** 😁
-->
# Description

This PR aims to refactor the NextAction declaration to achieve two
goals:

## Eliminate C-style sentinel arrays

Currently, a double pointer (`NextAction**`) approach is being used.
This an old pre-C++11 (< 2011) trick before `std::vector<>` became a
thing.
This approach is painful for developers because they constantly need to
declare their `NextAction` arrays as:
```cpp
NextAction::array(0, new NextAction("foo", 1.0f), nullptr)
```
Instead of:
```cpp
{ new NextAction("foo", 1.0f) }
```
The first argument of `NextAction::array` is actually a hack. It is used
to have a named argument so `va_args` can find the remaining arguments.
It is set to 0 everywhere but in fact does nothing. This is very
confusing to people unfamiliar with this antiquated syntax.
The last argument `nullptr` is what we call a sentinel. It's a `nullptr`
because `va_args` is looking for a `nullptr` to stop iterating. It's
also a hack and also leads to confusion.

## Eliminate unnecessary pointers for `NextAction`

Pointers can be used for several reasons, to cite a few:
- Indicate strong, absolute identity.
- Provide strong but transferable ownership (unlike references).
- When a null value is acceptable (`nullptr`).
- When copy is expensive.

`NextAction` meets none of these criteria:
- It has no identity because it is purely behavioural.
- It is never owned by anything as it is passed around and never fetched
from a registry.
- The only situations where it can be `nullptr` are errors that should
in fact throw an `std::invalid_argument` instead.
- They are extremely small objects that embark a single `std::string`
and a single `float`.

Pointers should be avoided when not strictly necessary because they can
quickly lead to undefined behaviour due to unhandled `nullptr`
situations. They also make the syntax heavier due to the necessity to
constantly check for `nullptr`. Finally, they aren't even good for
performance in that situation because shifting a pointer so many times
is likely more expensive than copying such a trivial object.

# End goal

The end goal is to declare `NextAction` arrays this way:
```cpp
{ NextAction("foo", 1.0f) }
```

> [!NOTE]
> Additional note: `NextAction` is nothing but a hacky proxy to an
`Action` constructor. This should eventually be reworked to use handles
instead of strings. This would make copying `NextAction` even cheaper
and remove the need for the extremely heavy stringly typed current
approach. Stringly typed entities are a known anti-pattern so we need to
move on from those.
2026-01-06 12:37:39 +01:00

334 lines
9.1 KiB
C++

/*
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU AGPL v3 license, you may redistribute it
* and/or modify it under version 3 of the License, or (at your option), any later version.
*/
#ifndef _PLAYERBOT_DRUIDACTIONS_H
#define _PLAYERBOT_DRUIDACTIONS_H
#include "GenericSpellActions.h"
#include "SharedDefines.h"
class PlayerbotAI;
class Unit;
class CastFaerieFireAction : public CastDebuffSpellAction
{
public:
CastFaerieFireAction(PlayerbotAI* botAI) : CastDebuffSpellAction(botAI, "faerie fire") {}
};
class CastFaerieFireFeralAction : public CastSpellAction
{
public:
CastFaerieFireFeralAction(PlayerbotAI* botAI) : CastSpellAction(botAI, "faerie fire (feral)") {}
};
class CastRejuvenationAction : public CastHealingSpellAction
{
public:
CastRejuvenationAction(PlayerbotAI* botAI) : CastHealingSpellAction(botAI, "rejuvenation") {}
};
class CastRegrowthAction : public CastHealingSpellAction
{
public:
CastRegrowthAction(PlayerbotAI* botAI) : CastHealingSpellAction(botAI, "regrowth") {}
};
class CastHealingTouchAction : public CastHealingSpellAction
{
public:
CastHealingTouchAction(PlayerbotAI* botAI) : CastHealingSpellAction(botAI, "healing touch") {}
};
class CastRejuvenationOnPartyAction : public HealPartyMemberAction
{
public:
CastRejuvenationOnPartyAction(PlayerbotAI* botAI)
: HealPartyMemberAction(botAI, "rejuvenation", 15.0f, HealingManaEfficiency::VERY_HIGH)
{
}
};
class CastRegrowthOnPartyAction : public HealPartyMemberAction
{
public:
CastRegrowthOnPartyAction(PlayerbotAI* botAI)
: HealPartyMemberAction(botAI, "regrowth", 35.0f, HealingManaEfficiency::HIGH)
{
}
};
class CastHealingTouchOnPartyAction : public HealPartyMemberAction
{
public:
CastHealingTouchOnPartyAction(PlayerbotAI* botAI)
: HealPartyMemberAction(botAI, "healing touch", 50.0f, HealingManaEfficiency::LOW)
{
}
};
class CastReviveAction : public ResurrectPartyMemberAction
{
public:
CastReviveAction(PlayerbotAI* botAI) : ResurrectPartyMemberAction(botAI, "revive") {}
std::vector<NextAction> getPrerequisites() override;
};
class CastRebirthAction : public ResurrectPartyMemberAction
{
public:
CastRebirthAction(PlayerbotAI* botAI) : ResurrectPartyMemberAction(botAI, "rebirth") {}
std::vector<NextAction> getPrerequisites() override;
bool isUseful() override;
};
class CastMarkOfTheWildAction : public CastBuffSpellAction
{
public:
CastMarkOfTheWildAction(PlayerbotAI* botAI) : CastBuffSpellAction(botAI, "mark of the wild") {}
};
class CastMarkOfTheWildOnPartyAction : public BuffOnPartyAction
{
public:
CastMarkOfTheWildOnPartyAction(PlayerbotAI* botAI) : BuffOnPartyAction(botAI, "mark of the wild") {}
};
class CastSurvivalInstinctsAction : public CastBuffSpellAction
{
public:
CastSurvivalInstinctsAction(PlayerbotAI* botAI) : CastBuffSpellAction(botAI, "survival instincts") {}
};
class CastFrenziedRegenerationAction : public CastBuffSpellAction
{
public:
CastFrenziedRegenerationAction(PlayerbotAI* botAI) : CastBuffSpellAction(botAI, "frenzied regeneration") {}
};
class CastThornsAction : public CastBuffSpellAction
{
public:
CastThornsAction(PlayerbotAI* botAI) : CastBuffSpellAction(botAI, "thorns") {}
};
class CastThornsOnPartyAction : public BuffOnPartyAction
{
public:
CastThornsOnPartyAction(PlayerbotAI* botAI) : BuffOnPartyAction(botAI, "thorns") {}
};
class CastThornsOnMainTankAction : public BuffOnMainTankAction
{
public:
CastThornsOnMainTankAction(PlayerbotAI* botAI) : BuffOnMainTankAction(botAI, "thorns", false) {}
};
class CastOmenOfClarityAction : public CastBuffSpellAction
{
public:
CastOmenOfClarityAction(PlayerbotAI* botAI) : CastBuffSpellAction(botAI, "omen of clarity") {}
};
class CastWrathAction : public CastSpellAction
{
public:
CastWrathAction(PlayerbotAI* botAI) : CastSpellAction(botAI, "wrath") {}
};
class CastStarfallAction : public CastSpellAction
{
public:
CastStarfallAction(PlayerbotAI* botAI) : CastSpellAction(botAI, "starfall") {}
bool isUseful() override;
};
class CastHurricaneAction : public CastSpellAction
{
public:
CastHurricaneAction(PlayerbotAI* botAI) : CastSpellAction(botAI, "hurricane") {}
ActionThreatType getThreatType() override { return ActionThreatType::Aoe; }
};
class CastMoonfireAction : public CastDebuffSpellAction
{
public:
CastMoonfireAction(PlayerbotAI* botAI) : CastDebuffSpellAction(botAI, "moonfire", true) {}
};
class CastInsectSwarmAction : public CastDebuffSpellAction
{
public:
CastInsectSwarmAction(PlayerbotAI* botAI) : CastDebuffSpellAction(botAI, "insect swarm", true) {}
};
class CastStarfireAction : public CastSpellAction
{
public:
CastStarfireAction(PlayerbotAI* botAI) : CastSpellAction(botAI, "starfire") {}
};
class CastEntanglingRootsAction : public CastSpellAction
{
public:
CastEntanglingRootsAction(PlayerbotAI* botAI) : CastSpellAction(botAI, "entangling roots") {}
};
class CastEntanglingRootsCcAction : public CastSpellAction
{
public:
CastEntanglingRootsCcAction(PlayerbotAI* botAI) : CastSpellAction(botAI, "entangling roots on cc") {}
Value<Unit*>* GetTargetValue() override;
bool Execute(Event event) override;
};
class CastHibernateAction : public CastSpellAction
{
public:
CastHibernateAction(PlayerbotAI* botAI) : CastSpellAction(botAI, "hibernate") {}
};
class CastHibernateCcAction : public CastSpellAction
{
public:
CastHibernateCcAction(PlayerbotAI* botAI) : CastSpellAction(botAI, "hibernate on cc") {}
Value<Unit*>* GetTargetValue() override;
bool Execute(Event event) override;
};
class CastNaturesGraspAction : public CastBuffSpellAction
{
public:
CastNaturesGraspAction(PlayerbotAI* botAI) : CastBuffSpellAction(botAI, "nature's grasp") {}
};
class CastCurePoisonAction : public CastCureSpellAction
{
public:
CastCurePoisonAction(PlayerbotAI* botAI) : CastCureSpellAction(botAI, "cure poison") {}
};
class CastCurePoisonOnPartyAction : public CurePartyMemberAction
{
public:
CastCurePoisonOnPartyAction(PlayerbotAI* botAI) : CurePartyMemberAction(botAI, "cure poison", DISPEL_POISON) {}
};
class CastAbolishPoisonAction : public CastCureSpellAction
{
public:
CastAbolishPoisonAction(PlayerbotAI* botAI) : CastCureSpellAction(botAI, "abolish poison") {}
std::vector<NextAction> getAlternatives() override;
};
class CastAbolishPoisonOnPartyAction : public CurePartyMemberAction
{
public:
CastAbolishPoisonOnPartyAction(PlayerbotAI* botAI) : CurePartyMemberAction(botAI, "abolish poison", DISPEL_POISON)
{
}
std::vector<NextAction> getAlternatives() override;
};
class CastBarkskinAction : public CastBuffSpellAction
{
public:
CastBarkskinAction(PlayerbotAI* botAI) : CastBuffSpellAction(botAI, "barkskin") {}
};
class CastInnervateAction : public CastSpellAction
{
public:
CastInnervateAction(PlayerbotAI* botAI) : CastSpellAction(botAI, "innervate") {}
std::string const GetTargetName() override { return "self target"; }
};
class CastTranquilityAction : public CastAoeHealSpellAction
{
public:
CastTranquilityAction(PlayerbotAI* botAI)
: CastAoeHealSpellAction(botAI, "tranquility", 15.0f, HealingManaEfficiency::MEDIUM)
{
}
};
class CastNaturesSwiftnessAction : public CastBuffSpellAction
{
public:
CastNaturesSwiftnessAction(PlayerbotAI* botAI) : CastBuffSpellAction(botAI, "nature's swiftness") {}
};
class CastWildGrowthOnPartyAction : public HealPartyMemberAction
{
public:
CastWildGrowthOnPartyAction(PlayerbotAI* ai)
: HealPartyMemberAction(ai, "wild growth", 15.0f, HealingManaEfficiency::VERY_HIGH)
{
}
};
class CastPartySwiftmendAction : public HealPartyMemberAction
{
public:
CastPartySwiftmendAction(PlayerbotAI* ai)
: HealPartyMemberAction(ai, "swiftmend", 15.0f, HealingManaEfficiency::MEDIUM)
{
}
};
class CastPartyNourishAction : public HealPartyMemberAction
{
public:
CastPartyNourishAction(PlayerbotAI* ai) : HealPartyMemberAction(ai, "nourish", 25.0f, HealingManaEfficiency::LOW) {}
};
class CastDruidRemoveCurseOnPartyAction : public CurePartyMemberAction
{
public:
CastDruidRemoveCurseOnPartyAction(PlayerbotAI* ai) : CurePartyMemberAction(ai, "remove curse", DISPEL_CURSE) {}
};
class CastInsectSwarmOnAttackerAction : public CastDebuffSpellOnAttackerAction
{
public:
CastInsectSwarmOnAttackerAction(PlayerbotAI* ai) : CastDebuffSpellOnAttackerAction(ai, "insect swarm") {}
};
class CastMoonfireOnAttackerAction : public CastDebuffSpellOnAttackerAction
{
public:
CastMoonfireOnAttackerAction(PlayerbotAI* ai) : CastDebuffSpellOnAttackerAction(ai, "moonfire") {}
};
class CastEnrageAction : public CastBuffSpellAction
{
public:
CastEnrageAction(PlayerbotAI* ai) : CastBuffSpellAction(ai, "enrage") {}
};
class CastRejuvenationOnNotFullAction : public HealPartyMemberAction
{
public:
CastRejuvenationOnNotFullAction(PlayerbotAI* ai)
: HealPartyMemberAction(ai, "rejuvenation", 5.0f, HealingManaEfficiency::VERY_HIGH)
{
}
bool isUseful() override;
Unit* GetTarget() override;
};
class CastForceOfNatureAction : public CastSpellAction
{
public:
CastForceOfNatureAction(PlayerbotAI* botAI) : CastSpellAction(botAI, "force of nature") {}
};
#endif