Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
CreatureAISelector.cpp
Go to the documentation of this file.
1/*
2* This file is part of Project SkyFire https://www.projectskyfire.org.
3* See LICENSE.md file for Copyright information
4*/
5
6#include "Creature.h"
8#include "PassiveAI.h"
9
10#include "CreatureAIFactory.h"
11#include "MovementGenerator.h"
12#include "Pet.h"
13#include "ScriptMgr.h"
14#include "TemporarySummon.h"
15
17{
19 {
20 const CreatureAICreator* ai_factory = NULL;
22
23 if (creature->IsPet())
24 ai_factory = ai_registry.GetRegistryItem("PetAI");
25
26 //scriptname in db
27 if (!ai_factory)
28 if (CreatureAI* scriptedAI = sScriptMgr->GetCreatureAI(creature))
29 return scriptedAI;
30
31 // AIname in db
32 std::string ainame = creature->GetAIName();
33 if (!ai_factory && !ainame.empty())
34 ai_factory = ai_registry.GetRegistryItem(ainame);
35
36 // select by NPC flags
37 if (!ai_factory)
38 {
39 if (creature->IsVehicle())
40 ai_factory = ai_registry.GetRegistryItem("VehicleAI");
41 else if (creature->HasUnitTypeMask(UNIT_MASK_CONTROLABLE_GUARDIAN) && ((Guardian*)creature)->GetOwner()->GetTypeId() == TypeID::TYPEID_PLAYER)
42 ai_factory = ai_registry.GetRegistryItem("PetAI");
44 ai_factory = ai_registry.GetRegistryItem("NullCreatureAI");
45 else if (creature->IsGuard())
46 ai_factory = ai_registry.GetRegistryItem("GuardAI");
48 ai_factory = ai_registry.GetRegistryItem("PetAI");
49 else if (creature->IsTotem())
50 ai_factory = ai_registry.GetRegistryItem("TotemAI");
51 else if (creature->IsTrigger())
52 {
53 if (creature->m_spells[0])
54 ai_factory = ai_registry.GetRegistryItem("TriggerAI");
55 else
56 ai_factory = ai_registry.GetRegistryItem("NullCreatureAI");
57 }
58 else if (creature->GetCreatureType() == CREATURE_TYPE_CRITTER && !creature->HasUnitTypeMask(UNIT_MASK_GUARDIAN))
59 ai_factory = ai_registry.GetRegistryItem("CritterAI");
60 }
61
62 // select by permit check
63 if (!ai_factory)
64 {
65 int best_val = -1;
67 RMT const& l = ai_registry.GetRegisteredItems();
68 for (RMT::const_iterator iter = l.begin(); iter != l.end(); ++iter)
69 {
70 const CreatureAICreator* factory = iter->second;
71 const SelectableAI* p = dynamic_cast<const SelectableAI*>(factory);
72 ASSERT(p);
73 int val = p->Permit(creature);
74 if (val > best_val)
75 {
76 best_val = val;
77 ai_factory = p;
78 }
79 }
80 }
81
82 // select NullCreatureAI if not another cases
83 ainame = (ai_factory == NULL) ? "NullCreatureAI" : ai_factory->key();
84
85 SF_LOG_DEBUG("scripts", "Creature %s (Entry: %u GUID: %u DB GUID: %u) is using AI type: %s.", creature->GetName().c_str(), creature->GetEntry(), creature->GetGUIDLow(), creature->GetDBTableGUIDLow(), ainame.c_str());
86 return (ai_factory == NULL ? new NullCreatureAI(creature) : ai_factory->Create(creature));
87 }
88
90 {
92 ASSERT(creature->GetCreatureTemplate());
93 const MovementGeneratorCreator* mv_factory = mv_registry.GetRegistryItem(creature->GetDefaultMovementType());
94
95 /* if (mv_factory == NULL)
96 {
97 int best_val = -1;
98 StringVector l;
99 mv_registry.GetRegisteredItems(l);
100 for (StringVector::iterator iter = l.begin(); iter != l.end(); ++iter)
101 {
102 const MovementGeneratorCreator *factory = mv_registry.GetRegistryItem((*iter).c_str());
103 const SelectableMovement *p = dynamic_cast<const SelectableMovement *>(factory);
104 ASSERT(p != NULL);
105 int val = p->Permit(creature);
106 if (val > best_val)
107 {
108 best_val = val;
109 mv_factory = p;
110 }
111 }
112 }*/
113
114 return (mv_factory == NULL ? NULL : mv_factory->Create(creature));
115 }
116
118 {
119 const GameObjectAICreator* ai_factory = NULL;
121
122 // scriptname in db
123 if (GameObjectAI* scriptedAI = sScriptMgr->GetGameObjectAI(go))
124 return scriptedAI;
125
126 ai_factory = ai_registry.GetRegistryItem(go->GetAIName());
127
128 //future goAI types go here
129
130 std::string ainame = (ai_factory == NULL || go->GetScriptId()) ? "NullGameObjectAI" : ai_factory->key();
131
132 SF_LOG_DEBUG("scripts", "GameObject %u used AI is %s.", go->GetGUIDLow(), ainame.c_str());
133
134 return (ai_factory == NULL ? new NullGameObjectAI(go) : ai_factory->Create(go));
135 }
136}
FactoryHolder< CreatureAI > CreatureAICreator
FactoryHolder< GameObjectAI > GameObjectAICreator
FactoryHolder< CreatureAI >::FactoryHolderRegistry CreatureAIRegistry
FactoryHolder< GameObjectAI >::FactoryHolderRegistry GameObjectAIRegistry
#define ASSERT
Definition Errors.h:29
#define SF_LOG_DEBUG(filterType__,...)
Definition Log.h:134
FactoryHolder< MovementGenerator, MovementGeneratorType >::FactoryHolderRegistry MovementGeneratorRegistry
FactoryHolder< MovementGenerator, MovementGeneratorType > MovementGeneratorCreator
@ TYPEID_PLAYER
Definition Object.h:55
#define sScriptMgr
Definition ScriptMgr.h:764
@ CREATURE_TYPE_CRITTER
@ UNIT_NPC_FLAG_SPELLCLICK
Definition Unit.h:711
@ UNIT_MASK_CONTROLABLE_GUARDIAN
Definition Unit.h:813
@ UNIT_MASK_GUARDIAN
Definition Unit.h:807
@ UNIT_FIELD_NPC_FLAGS
bool IsTrigger() const
Definition Creature.h:454
uint32 GetDBTableGUIDLow() const
Definition Creature.h:445
CreatureTemplate const * GetCreatureTemplate() const
Definition Creature.h:524
MovementGeneratorType GetDefaultMovementType() const
Definition Creature.h:589
std::string GetAIName() const
uint32 m_spells[CREATURE_MAX_SPELLS]
Definition Creature.h:565
bool IsGuard() const
Definition Creature.h:455
virtual T * Create(void *data=NULL) const =0
Abstract Factory create method.
Key key() const
virtual uint32 GetScriptId() const
Definition GameObject.h:823
std::string GetAIName() const
uint32 GetGUIDLow() const
Definition Object.h:120
uint32 GetEntry() const
Definition Object.h:125
bool HasFlag(uint16 index, uint32 flag) const
Definition Object.cpp:1309
const T * GetRegistryItem(Key key) const
Returns a registry item.
std::map< Key, T * > RegistryMapType
unsigned int GetRegisteredItems(std::vector< Key > &l) const
Inefficiently return a vector of registered items.
virtual int Permit(const T *) const =0
bool IsVehicle() const
Definition Unit.h:1523
bool IsPet() const
Definition Unit.h:1511
uint32 GetCreatureType() const
Definition Unit.cpp:4910
uint32 HasUnitTypeMask(uint32 mask) const
Definition Unit.h:1495
bool IsTotem() const
Definition Unit.h:1519
std::string const & GetName() const
Definition Object.h:664
CreatureAI * selectAI(Creature *creature)
MovementGenerator * selectMovementGenerator(Creature *creature)
GameObjectAI * SelectGameObjectAI(GameObject *go)