diff options
| author | Shauren <shauren.trinity@gmail.com> | 2020-06-15 00:26:08 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-06-15 00:26:08 +0200 |
| commit | c715e635cf3feb50ac61d30659e614aaa2cc0c63 (patch) | |
| tree | 5af30c80f8b1df3f60852adde4680951f6f55442 /src/server/game/AI | |
| parent | abff505a6eaf3e649be506c802b80eed3dd35f3a (diff) | |
| parent | cf88f0a9735f9ba010a4ae46e848c8f1a86e17fa (diff) | |
Merge pull request #24554 from funjoker/cherry-picks
Diffstat (limited to 'src/server/game/AI')
26 files changed, 242 insertions, 203 deletions
diff --git a/src/server/game/AI/CoreAI/CombatAI.cpp b/src/server/game/AI/CoreAI/CombatAI.cpp index 2f2461b5ab0..160b58a9742 100644 --- a/src/server/game/AI/CoreAI/CombatAI.cpp +++ b/src/server/game/AI/CoreAI/CombatAI.cpp @@ -32,11 +32,11 @@ // AggressorAI ///////////////// -int AggressorAI::Permissible(const Creature* creature) +int32 AggressorAI::Permissible(Creature const* creature) { // have some hostile factions, it will be selected by IsHostileTo check at MoveInLineOfSight if (!creature->IsCivilian() && !creature->IsNeutralToAll()) - return PERMIT_BASE_PROACTIVE; + return PERMIT_BASE_REACTIVE; return PERMIT_BASE_NO; } @@ -344,3 +344,11 @@ void VehicleAI::CheckConditions(uint32 diff) else m_ConditionsTimer -= diff; } + +int32 VehicleAI::Permissible(Creature const* creature) +{ + if (creature->IsVehicle()) + return PERMIT_BASE_SPECIAL; + + return PERMIT_BASE_NO; +} diff --git a/src/server/game/AI/CoreAI/CombatAI.h b/src/server/game/AI/CoreAI/CombatAI.h index 9e4e05d9544..9082ad515e8 100644 --- a/src/server/game/AI/CoreAI/CombatAI.h +++ b/src/server/game/AI/CoreAI/CombatAI.h @@ -28,7 +28,7 @@ class TC_GAME_API AggressorAI : public CreatureAI explicit AggressorAI(Creature* c) : CreatureAI(c) { } void UpdateAI(uint32) override; - static int Permissible(const Creature*); + static int32 Permissible(Creature const* creature); }; typedef std::vector<uint32> SpellVct; @@ -45,7 +45,7 @@ class TC_GAME_API CombatAI : public CreatureAI void UpdateAI(uint32 diff) override; void SpellInterrupted(uint32 spellId, uint32 unTimeMs) override; - static int Permissible(Creature const* /*creature*/) { return PERMIT_BASE_NO; } + static int32 Permissible(Creature const* /*creature*/) { return PERMIT_BASE_NO; } protected: EventMap events; @@ -71,7 +71,7 @@ struct TC_GAME_API ArcherAI : public CreatureAI void AttackStart(Unit* who) override; void UpdateAI(uint32 diff) override; - static int Permissible(Creature const* /*creature*/) { return PERMIT_BASE_NO; } + static int32 Permissible(Creature const* /*creature*/) { return PERMIT_BASE_NO; } protected: float m_minRange; @@ -85,7 +85,7 @@ struct TC_GAME_API TurretAI : public CreatureAI void AttackStart(Unit* who) override; void UpdateAI(uint32 diff) override; - static int Permissible(Creature const* /*creature*/) { return PERMIT_BASE_NO; } + static int32 Permissible(Creature const* /*creature*/) { return PERMIT_BASE_NO; } protected: float m_minRange; @@ -104,7 +104,7 @@ struct TC_GAME_API VehicleAI : public CreatureAI void AttackStart(Unit*) override { } void OnCharmed(bool apply) override; - static int Permissible(Creature const* /*creature*/) { return PERMIT_BASE_NO; } + static int32 Permissible(Creature const* creature); private: void LoadConditions(); diff --git a/src/server/game/AI/CoreAI/GameObjectAI.cpp b/src/server/game/AI/CoreAI/GameObjectAI.cpp index 48caa63dbce..94fe71f449d 100644 --- a/src/server/game/AI/CoreAI/GameObjectAI.cpp +++ b/src/server/game/AI/CoreAI/GameObjectAI.cpp @@ -20,11 +20,8 @@ #include "GameObject.h" #include "QuestDef.h" -//GameObjectAI::GameObjectAI(GameObject* g) : go(g) { } -int GameObjectAI::Permissible(const GameObject* go) +int32 GameObjectAI::Permissible(GameObject const* /*go*/) { - if (go->GetAIName() == "GameObjectAI") - return PERMIT_BASE_SPECIAL; return PERMIT_BASE_NO; } @@ -35,7 +32,7 @@ uint32 GameObjectAI::GetDialogStatus(Player* /*player*/) NullGameObjectAI::NullGameObjectAI(GameObject* g) : GameObjectAI(g) { } -int NullGameObjectAI::Permissible(GameObject const* /*go*/) +int32 NullGameObjectAI::Permissible(GameObject const* /*go*/) { return PERMIT_BASE_IDLE; } diff --git a/src/server/game/AI/CoreAI/GameObjectAI.h b/src/server/game/AI/CoreAI/GameObjectAI.h index 655035d505b..4bfc6c7a2f3 100644 --- a/src/server/game/AI/CoreAI/GameObjectAI.h +++ b/src/server/game/AI/CoreAI/GameObjectAI.h @@ -46,7 +46,7 @@ class TC_GAME_API GameObjectAI virtual void SetGUID(uint64 /*guid*/, int32 /*id = 0 */) { } virtual uint64 GetGUID(int32 /*id = 0 */) const { return 0; } - static int Permissible(GameObject const* go); + static int32 Permissible(GameObject const* /*go*/); // Called when a player opens a gossip dialog with the gameobject. virtual bool GossipHello(Player* /*player*/, bool /*reportUse*/) { return false; } @@ -88,6 +88,6 @@ class TC_GAME_API NullGameObjectAI : public GameObjectAI void UpdateAI(uint32 /*diff*/) override { } - static int Permissible(GameObject const* /*go*/); + static int32 Permissible(GameObject const* /*go*/); }; #endif diff --git a/src/server/game/AI/CoreAI/GuardAI.cpp b/src/server/game/AI/CoreAI/GuardAI.cpp index f9d3b96cadb..2b358945bd9 100644 --- a/src/server/game/AI/CoreAI/GuardAI.cpp +++ b/src/server/game/AI/CoreAI/GuardAI.cpp @@ -26,10 +26,10 @@ GuardAI::GuardAI(Creature* creature) : ScriptedAI(creature) { } -int GuardAI::Permissible(Creature const* creature) +int32 GuardAI::Permissible(Creature const* creature) { if (creature->IsGuard()) - return PERMIT_BASE_SPECIAL; + return PERMIT_BASE_PROACTIVE; return PERMIT_BASE_NO; } diff --git a/src/server/game/AI/CoreAI/GuardAI.h b/src/server/game/AI/CoreAI/GuardAI.h index a731e160e90..037094cff4b 100644 --- a/src/server/game/AI/CoreAI/GuardAI.h +++ b/src/server/game/AI/CoreAI/GuardAI.h @@ -27,7 +27,7 @@ class TC_GAME_API GuardAI : public ScriptedAI public: explicit GuardAI(Creature* creature); - static int Permissible(Creature const* creature); + static int32 Permissible(Creature const* creature); void UpdateAI(uint32 diff) override; bool CanSeeAlways(WorldObject const* obj) override; diff --git a/src/server/game/AI/CoreAI/PassiveAI.cpp b/src/server/game/AI/CoreAI/PassiveAI.cpp index 417a3c9bf04..5c3e8026600 100644 --- a/src/server/game/AI/CoreAI/PassiveAI.cpp +++ b/src/server/game/AI/CoreAI/PassiveAI.cpp @@ -22,6 +22,17 @@ PassiveAI::PassiveAI(Creature* c) : CreatureAI(c) { me->SetReactState(REACT_PASS PossessedAI::PossessedAI(Creature* c) : CreatureAI(c) { me->SetReactState(REACT_PASSIVE); } NullCreatureAI::NullCreatureAI(Creature* c) : CreatureAI(c) { me->SetReactState(REACT_PASSIVE); } +int32 NullCreatureAI::Permissible(Creature const* creature) +{ + if (creature->HasNpcFlag(UNIT_NPC_FLAG_SPELLCLICK)) + return PERMIT_BASE_PROACTIVE + 50; + + if (creature->IsTrigger()) + return PERMIT_BASE_REACTIVE; + + return PERMIT_BASE_IDLE; +} + void PassiveAI::UpdateAI(uint32) { if (me->IsInCombat() && me->getAttackers().empty()) @@ -76,8 +87,24 @@ void CritterAI::EnterEvadeMode(EvadeReason why) CreatureAI::EnterEvadeMode(why); } +int32 CritterAI::Permissible(Creature const* creature) +{ + if (creature->IsCritter() && !creature->HasUnitTypeMask(UNIT_MASK_GUARDIAN)) + return PERMIT_BASE_PROACTIVE; + + return PERMIT_BASE_NO; +} + void TriggerAI::IsSummonedBy(Unit* summoner) { if (me->m_spells[0]) me->CastSpell(me, me->m_spells[0], false, nullptr, nullptr, summoner->GetGUID()); } + +int32 TriggerAI::Permissible(Creature const* creature) +{ + if (creature->IsTrigger() && creature->m_spells[0]) + return PERMIT_BASE_PROACTIVE; + + return PERMIT_BASE_NO; +} diff --git a/src/server/game/AI/CoreAI/PassiveAI.h b/src/server/game/AI/CoreAI/PassiveAI.h index 43ad670e0df..41d4c2bfb12 100644 --- a/src/server/game/AI/CoreAI/PassiveAI.h +++ b/src/server/game/AI/CoreAI/PassiveAI.h @@ -29,7 +29,7 @@ class TC_GAME_API PassiveAI : public CreatureAI void AttackStart(Unit*) override { } void UpdateAI(uint32) override; - static int Permissible(const Creature*) { return PERMIT_BASE_IDLE; } + static int32 Permissible(Creature const* /*creature*/) { return PERMIT_BASE_NO; } }; class TC_GAME_API PossessedAI : public CreatureAI @@ -47,7 +47,7 @@ class TC_GAME_API PossessedAI : public CreatureAI void OnCharmed(bool /*apply*/) override; - static int Permissible(const Creature*) { return PERMIT_BASE_IDLE; } + static int32 Permissible(Creature const* /*creature*/) { return PERMIT_BASE_NO; } }; class TC_GAME_API NullCreatureAI : public CreatureAI @@ -61,7 +61,7 @@ class TC_GAME_API NullCreatureAI : public CreatureAI void EnterEvadeMode(EvadeReason /*why*/) override { } void OnCharmed(bool /*apply*/) override { } - static int Permissible(const Creature*) { return PERMIT_BASE_IDLE; } + static int32 Permissible(Creature const* creature); }; class TC_GAME_API CritterAI : public PassiveAI @@ -71,6 +71,8 @@ class TC_GAME_API CritterAI : public PassiveAI void DamageTaken(Unit* done_by, uint32& /*damage*/) override; void EnterEvadeMode(EvadeReason why) override; + + static int32 Permissible(Creature const* creature); }; class TC_GAME_API TriggerAI : public NullCreatureAI @@ -78,6 +80,8 @@ class TC_GAME_API TriggerAI : public NullCreatureAI public: explicit TriggerAI(Creature* c) : NullCreatureAI(c) { } void IsSummonedBy(Unit* summoner) override; + + static int32 Permissible(Creature const* creature); }; #endif diff --git a/src/server/game/AI/CoreAI/PetAI.cpp b/src/server/game/AI/CoreAI/PetAI.cpp index a39e4a8dfc0..c16e1f945eb 100644 --- a/src/server/game/AI/CoreAI/PetAI.cpp +++ b/src/server/game/AI/CoreAI/PetAI.cpp @@ -31,10 +31,14 @@ #include "SpellMgr.h" #include "Util.h" -int PetAI::Permissible(const Creature* creature) +int32 PetAI::Permissible(Creature const* creature) { - if (creature->IsPet()) - return PERMIT_BASE_SPECIAL; + if (creature->HasUnitTypeMask(UNIT_MASK_CONTROLABLE_GUARDIAN)) + { + if (reinterpret_cast<Guardian const*>(creature)->GetOwner()->GetTypeId() == TYPEID_PLAYER) + return PERMIT_BASE_PROACTIVE; + return PERMIT_BASE_REACTIVE; + } return PERMIT_BASE_NO; } diff --git a/src/server/game/AI/CoreAI/PetAI.h b/src/server/game/AI/CoreAI/PetAI.h index c712d42651d..4455c1cd494 100644 --- a/src/server/game/AI/CoreAI/PetAI.h +++ b/src/server/game/AI/CoreAI/PetAI.h @@ -33,7 +33,7 @@ class TC_GAME_API PetAI : public CreatureAI explicit PetAI(Creature* c); void UpdateAI(uint32) override; - static int Permissible(const Creature*); + static int32 Permissible(Creature const* creature); void KilledUnit(Unit* /*victim*/) override; void AttackStart(Unit* target) override; diff --git a/src/server/game/AI/CoreAI/ReactorAI.cpp b/src/server/game/AI/CoreAI/ReactorAI.cpp index 836cc1e0358..8b15bed1908 100644 --- a/src/server/game/AI/CoreAI/ReactorAI.cpp +++ b/src/server/game/AI/CoreAI/ReactorAI.cpp @@ -18,7 +18,7 @@ #include "ReactorAI.h" #include "Creature.h" -int ReactorAI::Permissible(const Creature* creature) +int32 ReactorAI::Permissible(Creature const* creature) { if (creature->IsCivilian() || creature->IsNeutralToAll()) return PERMIT_BASE_REACTIVE; diff --git a/src/server/game/AI/CoreAI/ReactorAI.h b/src/server/game/AI/CoreAI/ReactorAI.h index fa0bd2d8ee1..be76f855781 100644 --- a/src/server/game/AI/CoreAI/ReactorAI.h +++ b/src/server/game/AI/CoreAI/ReactorAI.h @@ -29,6 +29,6 @@ class TC_GAME_API ReactorAI : public CreatureAI void MoveInLineOfSight(Unit*) override { } void UpdateAI(uint32 diff) override; - static int Permissible(const Creature*); + static int32 Permissible(Creature const* creature); }; #endif diff --git a/src/server/game/AI/CoreAI/TotemAI.cpp b/src/server/game/AI/CoreAI/TotemAI.cpp index 4d2bda5faa0..c727ea45003 100644 --- a/src/server/game/AI/CoreAI/TotemAI.cpp +++ b/src/server/game/AI/CoreAI/TotemAI.cpp @@ -24,7 +24,7 @@ #include "GridNotifiersImpl.h" #include "CellImpl.h" -int TotemAI::Permissible(Creature const* creature) +int32 TotemAI::Permissible(Creature const* creature) { if (creature->IsTotem()) return PERMIT_BASE_PROACTIVE; diff --git a/src/server/game/AI/CoreAI/TotemAI.h b/src/server/game/AI/CoreAI/TotemAI.h index 2938190c771..fd25ca86df6 100644 --- a/src/server/game/AI/CoreAI/TotemAI.h +++ b/src/server/game/AI/CoreAI/TotemAI.h @@ -35,7 +35,7 @@ class TC_GAME_API TotemAI : public CreatureAI void EnterEvadeMode(EvadeReason /*why*/) override; void UpdateAI(uint32 diff) override; - static int Permissible(Creature const* creature); + static int32 Permissible(Creature const* creature); private: ObjectGuid i_victimGuid; diff --git a/src/server/game/AI/CreatureAI.h b/src/server/game/AI/CreatureAI.h index b274a4794d1..41e6ab2501d 100644 --- a/src/server/game/AI/CreatureAI.h +++ b/src/server/game/AI/CreatureAI.h @@ -204,7 +204,7 @@ class TC_GAME_API CreatureAI : public UnitAI bool m_MoveInLineOfSight_locked; }; -enum Permitions +enum Permitions : int32 { PERMIT_BASE_NO = -1, PERMIT_BASE_IDLE = 1, diff --git a/src/server/game/AI/CreatureAIFactory.h b/src/server/game/AI/CreatureAIFactory.h index 656435ad632..92e757d835d 100644 --- a/src/server/game/AI/CreatureAIFactory.h +++ b/src/server/game/AI/CreatureAIFactory.h @@ -18,66 +18,34 @@ #ifndef TRINITY_CREATUREAIFACTORY_H #define TRINITY_CREATUREAIFACTORY_H -//#include "Policies/Singleton.h" #include "ObjectRegistry.h" #include "FactoryHolder.h" -#include "GameObjectAI.h" -struct SelectableAI : public FactoryHolder<CreatureAI>, public Permissible<Creature> +typedef FactoryHolder<CreatureAI, Creature> CreatureAICreator; + +struct SelectableAI : public CreatureAICreator, public Permissible<Creature> { - SelectableAI(const char* id) : FactoryHolder<CreatureAI>(id) { } + SelectableAI(std::string const& name) : CreatureAICreator(name), Permissible<Creature>() { } }; template<class REAL_AI> struct CreatureAIFactory : public SelectableAI { - CreatureAIFactory(const char* name) : SelectableAI(name) { } + CreatureAIFactory(std::string const& name) : SelectableAI(name) { } - CreatureAI* Create(void*) const override; + inline CreatureAI* Create(Creature* c) const override + { + return new REAL_AI(c); + } - int Permit(const Creature* c) const override { return REAL_AI::Permissible(c); } + int32 Permit(Creature const* c) const override + { + return REAL_AI::Permissible(c); + } }; -template<class REAL_AI> -inline CreatureAI* -CreatureAIFactory<REAL_AI>::Create(void* data) const -{ - Creature* creature = reinterpret_cast<Creature*>(data); - return (new REAL_AI(creature)); -} - -typedef FactoryHolder<CreatureAI> CreatureAICreator; -typedef FactoryHolder<CreatureAI>::FactoryHolderRegistry CreatureAIRegistry; +typedef CreatureAICreator::FactoryHolderRegistry CreatureAIRegistry; #define sCreatureAIRegistry CreatureAIRegistry::instance() -//GO -struct SelectableGameObjectAI : public FactoryHolder<GameObjectAI>, public Permissible<GameObject> -{ - SelectableGameObjectAI(const char* id) : FactoryHolder<GameObjectAI>(id) { } -}; - -template<class REAL_GO_AI> -struct GameObjectAIFactory : public SelectableGameObjectAI -{ - GameObjectAIFactory(const char* name) : SelectableGameObjectAI(name) { } - - GameObjectAI* Create(void*) const override; - - int Permit(const GameObject* g) const override { return REAL_GO_AI::Permissible(g); } -}; - -template<class REAL_GO_AI> -inline GameObjectAI* -GameObjectAIFactory<REAL_GO_AI>::Create(void* data) const -{ - GameObject* go = reinterpret_cast<GameObject*>(data); - return (new REAL_GO_AI(go)); -} - -typedef FactoryHolder<GameObjectAI> GameObjectAICreator; -typedef FactoryHolder<GameObjectAI>::FactoryHolderRegistry GameObjectAIRegistry; - -#define sGameObjectAIRegistry GameObjectAIRegistry::instance() - #endif diff --git a/src/server/game/AI/CreatureAIRegistry.cpp b/src/server/game/AI/CreatureAIRegistry.cpp index 9aab7376ab2..34cc64af22b 100644 --- a/src/server/game/AI/CreatureAIRegistry.cpp +++ b/src/server/game/AI/CreatureAIRegistry.cpp @@ -22,10 +22,11 @@ #include "PetAI.h" #include "TotemAI.h" #include "RandomMovementGenerator.h" -#include "MovementGeneratorImpl.h" +#include "MovementGenerator.h" #include "CreatureAIRegistry.h" #include "WaypointMovementGenerator.h" #include "CreatureAIFactory.h" +#include "GameObjectAIFactory.h" #include "SmartAI.h" namespace AIRegistry @@ -47,10 +48,12 @@ namespace AIRegistry (new CreatureAIFactory<VehicleAI>("VehicleAI"))->RegisterSelf(); (new CreatureAIFactory<SmartAI>("SmartAI"))->RegisterSelf(); + (new GameObjectAIFactory<NullGameObjectAI>("NullGameObjectAI"))->RegisterSelf(); (new GameObjectAIFactory<GameObjectAI>("GameObjectAI"))->RegisterSelf(); (new GameObjectAIFactory<SmartGameObjectAI>("SmartGameObjectAI"))->RegisterSelf(); - (new MovementGeneratorFactory<RandomMovementGenerator<Creature> >(RANDOM_MOTION_TYPE))->RegisterSelf(); - (new MovementGeneratorFactory<WaypointMovementGenerator<Creature> >(WAYPOINT_MOTION_TYPE))->RegisterSelf(); + (new IdleMovementFactory())->RegisterSelf(); + (new MovementGeneratorFactory<RandomMovementGenerator<Creature>>(RANDOM_MOTION_TYPE))->RegisterSelf(); + (new MovementGeneratorFactory<WaypointMovementGenerator<Creature>>(WAYPOINT_MOTION_TYPE))->RegisterSelf(); } } diff --git a/src/server/game/AI/CreatureAISelector.cpp b/src/server/game/AI/CreatureAISelector.cpp index 487511eff3e..b7181a35f76 100644 --- a/src/server/game/AI/CreatureAISelector.cpp +++ b/src/server/game/AI/CreatureAISelector.cpp @@ -17,130 +17,94 @@ #include "Creature.h" #include "CreatureAISelector.h" -#include "GameObject.h" -#include "PassiveAI.h" +#include "CreatureAIFactory.h" #include "Log.h" #include "MovementGenerator.h" -#include "TemporarySummon.h" -#include "CreatureAIFactory.h" + +#include "GameObject.h" +#include "GameObjectAIFactory.h" + #include "ScriptMgr.h" namespace FactorySelector { - CreatureAI* selectAI(Creature* creature) + template <class T, class Value> + inline int32 GetPermitFor(T const* obj, Value const& value) { - const CreatureAICreator* ai_factory = NULL; - - if (creature->IsPet()) - ai_factory = sCreatureAIRegistry->GetRegistryItem("PetAI"); - - //scriptname in db - if (!ai_factory) - if (CreatureAI* scriptedAI = sScriptMgr->GetCreatureAI(creature)) - return scriptedAI; + Permissible<T> const* const p = ASSERT_NOTNULL(dynamic_cast<Permissible<T> const*>(value.second.get())); + return p->Permit(obj); + } - // AIname in db - std::string ainame=creature->GetAIName(); - if (!ai_factory && !ainame.empty()) - ai_factory = sCreatureAIRegistry->GetRegistryItem(ainame); + template <class T> + struct PermissibleOrderPred + { + public: + PermissibleOrderPred(T const* obj) : _obj(obj) { } - // select by NPC flags - if (!ai_factory) + template <class Value> + bool operator()(Value const& left, Value const& right) const { - if (creature->IsVehicle()) - ai_factory = sCreatureAIRegistry->GetRegistryItem("VehicleAI"); - else if (creature->HasUnitTypeMask(UNIT_MASK_CONTROLABLE_GUARDIAN) && ((Guardian*)creature)->GetOwner()->GetTypeId() == TYPEID_PLAYER) - ai_factory = sCreatureAIRegistry->GetRegistryItem("PetAI"); - else if (creature->HasNpcFlag(UNIT_NPC_FLAG_SPELLCLICK)) - ai_factory = sCreatureAIRegistry->GetRegistryItem("NullCreatureAI"); - else if (creature->IsGuard()) - ai_factory = sCreatureAIRegistry->GetRegistryItem("GuardAI"); - else if (creature->HasUnitTypeMask(UNIT_MASK_CONTROLABLE_GUARDIAN)) - ai_factory = sCreatureAIRegistry->GetRegistryItem("PetAI"); - else if (creature->IsTotem()) - ai_factory = sCreatureAIRegistry->GetRegistryItem("TotemAI"); - else if (creature->IsTrigger()) - { - if (creature->m_spells[0]) - ai_factory = sCreatureAIRegistry->GetRegistryItem("TriggerAI"); - else - ai_factory = sCreatureAIRegistry->GetRegistryItem("NullCreatureAI"); - } - else if (creature->IsCritter() && !creature->HasUnitTypeMask(UNIT_MASK_GUARDIAN)) - ai_factory = sCreatureAIRegistry->GetRegistryItem("CritterAI"); + return GetPermitFor(_obj, left) < GetPermitFor(_obj, right); } - // select by permit check - if (!ai_factory) - { - int best_val = -1; - typedef CreatureAIRegistry::RegistryMapType RMT; - RMT const& l = sCreatureAIRegistry->GetRegisteredItems(); - for (RMT::const_iterator iter = l.begin(); iter != l.end(); ++iter) - { - const CreatureAICreator* factory = iter->second; - const SelectableAI* p = dynamic_cast<const SelectableAI*>(factory); - ASSERT(p); - int val = p->Permit(creature); - if (val > best_val) - { - best_val = val; - ai_factory = p; - } - } - } + private: + T const* const _obj; + }; - // select NullCreatureAI if not another cases - ainame = (ai_factory == NULL) ? "NullCreatureAI" : ai_factory->key(); + template <class AI, class T> + inline FactoryHolder<AI, T> const* SelectFactory(T* obj) + { + static_assert(std::is_same<AI, CreatureAI>::value || std::is_same<AI, GameObjectAI>::value, "Invalid template parameter"); + static_assert(std::is_same<AI, CreatureAI>::value == std::is_same<T, Creature>::value, "Incompatible AI for type"); + static_assert(std::is_same<AI, GameObjectAI>::value == std::is_same<T, GameObject>::value, "Incompatible AI for type"); - TC_LOG_DEBUG("scripts", "Creature %s (%s DB GUID: " UI64FMTD ") is using AI type: %s.", creature->GetName().c_str(), creature->GetGUID().ToString().c_str(), creature->GetSpawnId(), ainame.c_str()); - return (ai_factory == NULL ? new NullCreatureAI(creature) : ai_factory->Create(creature)); - } + using AIRegistry = typename FactoryHolder<AI, T>::FactoryHolderRegistry; - MovementGenerator* selectMovementGenerator(Creature* creature) - { - MovementGeneratorRegistry& mv_registry(*MovementGeneratorRegistry::instance()); - ASSERT(creature->GetCreatureTemplate()); - const MovementGeneratorCreator* mv_factory = mv_registry.GetRegistryItem(creature->GetDefaultMovementType()); + // AIName in db + std::string const& aiName = obj->GetAIName(); + if (!aiName.empty()) + return AIRegistry::instance()->GetRegistryItem(aiName); - /* if (mv_factory == NULL) - { - int best_val = -1; - StringVector l; - mv_registry.GetRegisteredItems(l); - for (StringVector::iterator iter = l.begin(); iter != l.end(); ++iter) - { - const MovementGeneratorCreator *factory = mv_registry.GetRegistryItem((*iter).c_str()); - const SelectableMovement *p = dynamic_cast<const SelectableMovement *>(factory); - ASSERT(p != NULL); - int val = p->Permit(creature); - if (val > best_val) - { - best_val = val; - mv_factory = p; - } - } - }*/ - - return (mv_factory == NULL ? NULL : mv_factory->Create(creature)); + // select by permit check + typename AIRegistry::RegistryMapType const& items = AIRegistry::instance()->GetRegisteredItems(); + auto itr = std::max_element(items.begin(), items.end(), PermissibleOrderPred<T>(obj)); + if (itr != items.end() && GetPermitFor(obj, *itr) >= 0) + return itr->second.get(); + + // should _never_ happen, Null AI types defined as PERMIT_BASE_IDLE, it must've been found + ABORT(); + return nullptr; } - GameObjectAI* SelectGameObjectAI(GameObject* go) + CreatureAI* SelectAI(Creature* creature) { - GameObjectAICreator const* ai_factory = NULL; + // special pet case, if a tamed creature uses AIName (example SmartAI) we need to override it + if (creature->IsPet()) + return ASSERT_NOTNULL(sCreatureAIRegistry->GetRegistryItem("PetAI"))->Create(creature); // scriptname in db - if (GameObjectAI* scriptedAI = sScriptMgr->GetGameObjectAI(go)) + if (CreatureAI* scriptedAI = sScriptMgr->GetCreatureAI(creature)) return scriptedAI; - ai_factory = sGameObjectAIRegistry->GetRegistryItem(go->GetAIName()); + return SelectFactory<CreatureAI>(creature)->Create(creature); + } - //future goAI types go here + MovementGenerator* SelectMovementGenerator(Unit* unit) + { + MovementGeneratorType type = IDLE_MOTION_TYPE; + if (unit->GetTypeId() == TYPEID_UNIT) + type = unit->ToCreature()->GetDefaultMovementType(); - std::string ainame = (ai_factory == NULL || go->GetScriptId()) ? "NullGameObjectAI" : ai_factory->key(); + MovementGeneratorCreator const* mv_factory = sMovementGeneratorRegistry->GetRegistryItem(type); + return ASSERT_NOTNULL(mv_factory)->Create(unit); + } - TC_LOG_DEBUG("scripts", "%s used AI is %s.", go->GetGUID().ToString().c_str(), ainame.c_str()); + GameObjectAI* SelectGameObjectAI(GameObject* go) + { + // scriptname in db + if (GameObjectAI* scriptedAI = sScriptMgr->GetGameObjectAI(go)) + return scriptedAI; - return (ai_factory == NULL ? new NullGameObjectAI(go) : ai_factory->Create(go)); + return SelectFactory<GameObjectAI>(go)->Create(go); } } diff --git a/src/server/game/AI/CreatureAISelector.h b/src/server/game/AI/CreatureAISelector.h index d22bf0bb1e7..b4046a5e982 100644 --- a/src/server/game/AI/CreatureAISelector.h +++ b/src/server/game/AI/CreatureAISelector.h @@ -21,13 +21,14 @@ class CreatureAI; class Creature; class MovementGenerator; +class Unit; class GameObjectAI; class GameObject; namespace FactorySelector { - TC_GAME_API CreatureAI* selectAI(Creature*); - TC_GAME_API MovementGenerator* selectMovementGenerator(Creature*); - TC_GAME_API GameObjectAI* SelectGameObjectAI(GameObject*); + TC_GAME_API CreatureAI* SelectAI(Creature* creature); + TC_GAME_API MovementGenerator* SelectMovementGenerator(Unit* unit); + TC_GAME_API GameObjectAI* SelectGameObjectAI(GameObject* go); } #endif diff --git a/src/server/game/AI/GameObjectAIFactory.h b/src/server/game/AI/GameObjectAIFactory.h new file mode 100644 index 00000000000..88dcb5cbb81 --- /dev/null +++ b/src/server/game/AI/GameObjectAIFactory.h @@ -0,0 +1,54 @@ +/* + * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef TRINITY_GAMEOBJECTAIFACTORY_H +#define TRINITY_GAMEOBJECTAIFACTORY_H + +#include "ObjectRegistry.h" +#include "FactoryHolder.h" + +class GameObject; +class GameObjectAI; + +typedef FactoryHolder<GameObjectAI, GameObject> GameObjectAICreator; + +struct SelectableGameObjectAI : public GameObjectAICreator, public Permissible<GameObject> +{ + SelectableGameObjectAI(std::string const& name) : GameObjectAICreator(name), Permissible<GameObject>() { } +}; + +template<class REAL_GO_AI> +struct GameObjectAIFactory : public SelectableGameObjectAI +{ + GameObjectAIFactory(std::string const& name) : SelectableGameObjectAI(name) { } + + GameObjectAI* Create(GameObject* go) const override + { + return new REAL_GO_AI(go); + } + + int32 Permit(GameObject const* go) const override + { + return REAL_GO_AI::Permissible(go); + } +}; + +typedef GameObjectAICreator::FactoryHolderRegistry GameObjectAIRegistry; + +#define sGameObjectAIRegistry GameObjectAIRegistry::instance() + +#endif diff --git a/src/server/game/AI/ScriptedAI/ScriptedCreature.cpp b/src/server/game/AI/ScriptedAI/ScriptedCreature.cpp index 71125e716a6..bde3a192642 100644 --- a/src/server/game/AI/ScriptedAI/ScriptedCreature.cpp +++ b/src/server/game/AI/ScriptedAI/ScriptedCreature.cpp @@ -537,7 +537,7 @@ bool BossAI::CanAIAttack(Unit const* target) const return CheckBoundary(target); } -void BossAI::_DespawnAtEvade(uint32 delayToRespawn, Creature* who) +void BossAI::_DespawnAtEvade(uint32 delayToRespawn /*= 30*/, Creature* who /*= nullptr*/) { if (delayToRespawn < 2) { diff --git a/src/server/game/AI/ScriptedAI/ScriptedGossip.cpp b/src/server/game/AI/ScriptedAI/ScriptedGossip.cpp index 0e0ad589518..33e69bf850c 100644 --- a/src/server/game/AI/ScriptedAI/ScriptedGossip.cpp +++ b/src/server/game/AI/ScriptedAI/ScriptedGossip.cpp @@ -19,6 +19,7 @@ #include "Creature.h" #include "Player.h" +uint32 GetGossipActionFor(Player* player, uint32 gossipListId) { return player->PlayerTalkClass->GetGossipOptionAction(gossipListId); } void ClearGossipMenuFor(Player* player) { player->PlayerTalkClass->ClearMenus(); } // Using provided text, not from DB void AddGossipItemFor(Player* player, uint32 icon, std::string const& text, uint32 sender, uint32 action) { player->PlayerTalkClass->GetGossipMenu().AddMenuItem(-1, icon, text, sender, action, "", 0); } diff --git a/src/server/game/AI/ScriptedAI/ScriptedGossip.h b/src/server/game/AI/ScriptedAI/ScriptedGossip.h index 3032059d81b..6963155c94a 100644 --- a/src/server/game/AI/ScriptedAI/ScriptedGossip.h +++ b/src/server/game/AI/ScriptedAI/ScriptedGossip.h @@ -86,6 +86,7 @@ enum eTradeskill GOSSIP_SENDER_SEC_STABLEMASTER = 10 }; +uint32 TC_GAME_API GetGossipActionFor(Player* player, uint32 gossipListId); void TC_GAME_API ClearGossipMenuFor(Player* player); // Using provided text, not from DB void TC_GAME_API AddGossipItemFor(Player* player, uint32 icon, std::string const& text, uint32 sender, uint32 action); diff --git a/src/server/game/AI/SmartScripts/SmartAI.cpp b/src/server/game/AI/SmartScripts/SmartAI.cpp index 2dd122de1ee..065ca31bee0 100644 --- a/src/server/game/AI/SmartScripts/SmartAI.cpp +++ b/src/server/game/AI/SmartScripts/SmartAI.cpp @@ -74,6 +74,8 @@ SmartAI::SmartAI(Creature* c) : CreatureAI(c) mJustReset = false; mConditionsTimer = 0; mHasConditions = sConditionMgr->HasConditionsForNotGroupedEntry(CONDITION_SOURCE_TYPE_CREATURE_TEMPLATE_VEHICLE, c->GetEntry()); + + _gossipReturn = false; } bool SmartAI::IsAIControlled() const @@ -594,13 +596,6 @@ void SmartAI::JustRespawned() mFollowCreditType = 0; } -int SmartAI::Permissible(const Creature* creature) -{ - if (creature->GetAIName() == "SmartAI") - return PERMIT_BASE_SPECIAL; - return PERMIT_BASE_NO; -} - void SmartAI::JustReachedHome() { GetScript()->OnReset(); @@ -810,14 +805,16 @@ void SmartAI::SetEvadeDisabled(bool disable) bool SmartAI::GossipHello(Player* player) { + _gossipReturn = false; GetScript()->ProcessEventsFor(SMART_EVENT_GOSSIP_HELLO, player); - return false; + return _gossipReturn; } bool SmartAI::GossipSelect(Player* player, uint32 menuId, uint32 gossipListId) { + _gossipReturn = false; GetScript()->ProcessEventsFor(SMART_EVENT_GOSSIP_SELECT, player, menuId, gossipListId); - return false; + return _gossipReturn; } bool SmartAI::GossipSelectCode(Player* /*player*/, uint32 /*menuId*/, uint32 /*gossipListId*/, const char* /*code*/) @@ -962,13 +959,6 @@ void SmartAI::CheckConditions(uint32 diff) mConditionsTimer -= diff; } -int SmartGameObjectAI::Permissible(const GameObject* g) -{ - if (g->GetAIName() == "SmartGameObjectAI") - return PERMIT_BASE_SPECIAL; - return PERMIT_BASE_NO; -} - void SmartGameObjectAI::UpdateAI(uint32 diff) { GetScript()->OnUpdate(diff); @@ -995,16 +985,17 @@ void SmartGameObjectAI::Reset() // Called when a player opens a gossip dialog with the gameobject. bool SmartGameObjectAI::GossipHello(Player* player, bool reportUse) { - TC_LOG_DEBUG("scripts.ai", "SmartGameObjectAI::GossipHello"); + _gossipReturn = false; GetScript()->ProcessEventsFor(SMART_EVENT_GOSSIP_HELLO, player, uint32(reportUse), 0, false, nullptr, me); - return false; + return _gossipReturn; } // Called when a player selects a gossip item in the gameobject's gossip menu. bool SmartGameObjectAI::GossipSelect(Player* player, uint32 sender, uint32 action) { + _gossipReturn = false; GetScript()->ProcessEventsFor(SMART_EVENT_GOSSIP_SELECT, player, sender, action, false, nullptr, me); - return false; + return _gossipReturn; } // Called when a player selects a gossip with a code in the gameobject's gossip menu. diff --git a/src/server/game/AI/SmartScripts/SmartAI.h b/src/server/game/AI/SmartScripts/SmartAI.h index 1c65c9633b9..070165efa40 100644 --- a/src/server/game/AI/SmartScripts/SmartAI.h +++ b/src/server/game/AI/SmartScripts/SmartAI.h @@ -158,7 +158,7 @@ class TC_GAME_API SmartAI : public CreatureAI ObjectGuid GetGUID(int32 id = 0) const override; //core related - static int Permissible(const Creature*); + static int32 Permissible(Creature const* /*creature*/) { return PERMIT_BASE_NO; } // Called at movepoint reached void MovepointReached(uint32 id); @@ -198,6 +198,8 @@ class TC_GAME_API SmartAI : public CreatureAI void SetWPPauseTimer(uint32 time) { mWPPauseTimer = time; } + void SetGossipReturn(bool val) { _gossipReturn = val; } + private: bool mIsCharmed; uint32 mFollowCreditType; @@ -241,19 +243,22 @@ class TC_GAME_API SmartAI : public CreatureAI void CheckConditions(uint32 diff); bool mHasConditions; uint32 mConditionsTimer; + + // Gossip + bool _gossipReturn; }; class TC_GAME_API SmartGameObjectAI : public GameObjectAI { public: - SmartGameObjectAI(GameObject* g) : GameObjectAI(g) { } + SmartGameObjectAI(GameObject* g) : GameObjectAI(g), _gossipReturn(false) { } ~SmartGameObjectAI() { } void UpdateAI(uint32 diff) override; void InitializeAI() override; void Reset() override; SmartScript* GetScript() { return &mScript; } - static int Permissible(const GameObject* g); + static int32 Permissible(GameObject const* /*go*/) { return PERMIT_BASE_NO; } bool GossipHello(Player* player, bool reportUse) override; bool GossipSelect(Player* player, uint32 menuId, uint32 gossipListId) override; @@ -268,8 +273,13 @@ class TC_GAME_API SmartGameObjectAI : public GameObjectAI void EventInform(uint32 eventId) override; void SpellHit(Unit* unit, const SpellInfo* spellInfo) override; + void SetGossipReturn(bool val) { _gossipReturn = val; } + private: SmartScript mScript; + + // Gossip + bool _gossipReturn; }; /// Registers scripts required by the SAI scripting system diff --git a/src/server/game/AI/SmartScripts/SmartScript.cpp b/src/server/game/AI/SmartScripts/SmartScript.cpp index ebf884133a4..a1c6d3af9b6 100644 --- a/src/server/game/AI/SmartScripts/SmartScript.cpp +++ b/src/server/game/AI/SmartScripts/SmartScript.cpp @@ -2411,7 +2411,7 @@ void SmartScript::ProcessAction(SmartScriptHolder& e, Unit* unit, uint32 var0, u } case SMART_ACTION_SEND_GOSSIP_MENU: { - if (!GetBaseObject()) + if (!GetBaseObject() || !IsSmart()) break; TC_LOG_DEBUG("scripts.ai", "SmartScript::ProcessAction:: SMART_ACTION_SEND_GOSSIP_MENU: gossipMenuId %d, gossipNpcTextId %d", @@ -2421,6 +2421,12 @@ void SmartScript::ProcessAction(SmartScriptHolder& e, Unit* unit, uint32 var0, u if (!targets) break; + // override default gossip + if (me) + ENSURE_AI(SmartAI, me->AI())->SetGossipReturn(true); + else if (go) + ENSURE_AI(SmartGameObjectAI, go->AI())->SetGossipReturn(true); + for (ObjectList::const_iterator itr = targets->begin(); itr != targets->end(); ++itr) { if (Player* player = (*itr)->ToPlayer()) |
