OregonCore  revision fb2a440-git
Your Favourite TBC server
CreatureAISelector.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the OregonCore Project. See AUTHORS file for Copyright information
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "Creature.h"
19 #include "CreatureAISelector.h"
20 #include "PassiveAI.h"
21 #include "MovementGenerator.h"
22 #include "TemporarySummon.h"
23 #include "CreatureAIFactory.h"
24 #include "ScriptMgr.h"
25 
29 
30 namespace FactorySelector
31 {
33 {
34  const CreatureAICreator* ai_factory = NULL;
35  CreatureAIRegistry& ai_registry(CreatureAIRepository::Instance());
36 
37  if (creature->IsPet())
38  ai_factory = ai_registry.GetRegistryItem("PetAI");
39 
40  //scriptname in db
41  if (!ai_factory)
42  if (CreatureAI* scriptedAI = sScriptMgr.GetAI(creature))
43  return scriptedAI;
44 
45  // AIname in db
46  std::string ainame = creature->GetAIName();
47  if (!ai_factory && !ainame.empty())
48  ai_factory = ai_registry.GetRegistryItem(ainame.c_str());
49 
50  // select by NPC flags
51  if (!ai_factory)
52  {
53  if (creature->HasUnitTypeMask(UNIT_MASK_CONTROLABLE_GUARDIAN) && ((Guardian*)creature)->GetOwner()->GetTypeId() == TYPEID_PLAYER)
54  ai_factory = ai_registry.GetRegistryItem("PetAI");
55  else if (creature->HasFlag(UNIT_NPC_FLAGS, UNIT_NPC_FLAG_SPELLCLICK))
56  ai_factory = ai_registry.GetRegistryItem("NullCreatureAI");
57  else if (creature->IsGuard())
58  ai_factory = ai_registry.GetRegistryItem("GuardAI");
59  else if (creature->HasUnitTypeMask(UNIT_MASK_CONTROLABLE_GUARDIAN))
60  ai_factory = ai_registry.GetRegistryItem("PetAI");
61  else if (creature->IsTotem())
62  ai_factory = ai_registry.GetRegistryItem("TotemAI");
63  else if (creature->IsTrigger())
64  {
65  if (creature->m_spells[0])
66  ai_factory = ai_registry.GetRegistryItem("TriggerAI");
67  else
68  ai_factory = ai_registry.GetRegistryItem("NullCreatureAI");
69  }
70  else if (creature->GetCreatureType() == CREATURE_TYPE_CRITTER && !creature->HasUnitTypeMask(UNIT_MASK_GUARDIAN))
71  ai_factory = ai_registry.GetRegistryItem("CritterAI");
72  }
73 
74  // select by permit check
75  if (!ai_factory)
76  {
77  int best_val = -1;
78  typedef CreatureAIRegistry::RegistryMapType RMT;
79  RMT const& l = ai_registry.GetRegisteredItems();
80  for (RMT::const_iterator iter = l.begin(); iter != l.end(); ++iter)
81  {
82  const CreatureAICreator* factory = iter->second;
83  const SelectableAI* p = dynamic_cast<const SelectableAI*>(factory);
84  ASSERT(p != NULL);
85  int val = p->Permit(creature);
86  if (val > best_val)
87  {
88  best_val = val;
89  ai_factory = p;
90  }
91  }
92  }
93 
94  // select NullCreatureAI if not another cases
95  ainame = (ai_factory == NULL) ? "NullCreatureAI" : ai_factory->key();
96 
97  DEBUG_LOG("Creature %u used AI is %s.", creature->GetGUIDLow(), ainame.c_str());
98  return (ai_factory == NULL ? new NullCreatureAI(creature) : ai_factory->Create(creature));
99 }
100 
102 {
103  MovementGeneratorRegistry& mv_registry(MovementGeneratorRepository::Instance());
104  ASSERT(creature->GetCreatureTemplate() != NULL);
105  const MovementGeneratorCreator* mv_factory = mv_registry.GetRegistryItem(creature->GetDefaultMovementType());
106 
107  /* if (mv_factory == NULL)
108  {
109  int best_val = -1;
110  std::vector<std::string> l;
111  mv_registry.GetRegisteredItems(l);
112  for (std::vector<std::string>::iterator iter = l.begin(); iter != l.end(); ++iter)
113  {
114  const MovementGeneratorCreator *factory = mv_registry.GetRegistryItem((*iter).c_str());
115  const SelectableMovement *p = dynamic_cast<const SelectableMovement *>(factory);
116  ASSERT(p != NULL);
117  int val = p->Permit(creature);
118  if (val > best_val)
119  {
120  best_val = val;
121  mv_factory = p;
122  }
123  }
124  }*/
125 
126  return (mv_factory == NULL ? NULL : mv_factory->Create(creature));
127 
128 }
129 
131 {
132  const GameObjectAICreator* ai_factory = NULL;
133  GameObjectAIRegistry& ai_registry(GameObjectAIRepository::Instance());
134 
135  // AIname in db
136  std::string GobAiName = go->GetAIName();
137  if (!GobAiName.empty())
138  ai_factory = ai_registry.GetRegistryItem(GobAiName.c_str());
139 
140  //future goAI types go here
141 
142  std::string ainame = (ai_factory == NULL) ? "NullGameObjectAI" : ai_factory->key();
143 
144  DEBUG_LOG("GameObject %u used AI is %s.", go->GetGUIDLow(), ainame.c_str());
145 
146  return (ai_factory == NULL ? new NullGameObjectAI(go) : ai_factory->Create(go));
147 }
148 }
149 
INSTANTIATE_SINGLETON_1(CreatureAIRegistry)
virtual int Permit(const T *) const =0
MovementGeneratorType GetDefaultMovementType() const
Definition: Creature.h:628
uint32 GetGUIDLow() const
Definition: Object.h:166
std::string GetAIName() const
FactoryHolder< CreatureAI >::FactoryHolderRegistry CreatureAIRegistry
CreatureAI * selectAI(Creature *creature)
#define DEBUG_LOG(...)
Definition: Log.h:194
bool IsPet() const
Definition: Unit.h:1048
FactoryHolder< MovementGenerator, MovementGeneratorType >::FactoryHolderRegistry MovementGeneratorRegistry
FactoryHolder< GameObjectAI >::FactoryHolderRegistry GameObjectAIRegistry
CreatureInfo const * GetCreatureTemplate() const
Definition: Creature.h:557
uint32 HasUnitTypeMask(uint32 mask) const
Definition: Unit.h:1044
#define ASSERT
Definition: Errors.h:29
GameObjectAI * SelectGameObjectAI(GameObject *go)
#define sScriptMgr
Definition: Group.h:526
virtual T * Create(void *data=NULL) const =0
std::string GetAIName() const
Definition: Creature.cpp:2383
MovementGenerator * selectMovementGenerator(Creature *creature)
Key key() const
Definition: FactoryHolder.h:35