Updated Copyright year to 2013
[getmangos.git] / src / game / CreatureAISelector.cpp
blob60f7a190ca354c1d7aff05600ca3879d9777751f
1 /*
2 * Copyright (C) 2005-2013 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "CreatureAISelector.h"
20 #include "Creature.h"
21 #include "CreatureAIImpl.h"
22 #include "NullCreatureAI.h"
23 #include "Policies/SingletonImp.h"
24 #include "MovementGenerator.h"
25 #include "ScriptMgr.h"
26 #include "Pet.h"
28 INSTANTIATE_SINGLETON_1(CreatureAIRegistry);
29 INSTANTIATE_SINGLETON_1(MovementGeneratorRegistry);
31 namespace FactorySelector
33 CreatureAI* selectAI(Creature* creature)
35 // Allow scripting AI for normal creatures and not controlled pets (guardians and mini-pets)
36 if ((!creature->IsPet() || !((Pet*)creature)->isControlled()) && !creature->isCharmed())
37 if (CreatureAI* scriptedAI = sScriptMgr.GetCreatureAI(creature))
38 return scriptedAI;
40 CreatureAIRegistry& ai_registry(CreatureAIRepository::Instance());
42 const CreatureAICreator* ai_factory = NULL;
44 std::string ainame = creature->GetAIName();
46 // select by NPC flags _first_ - otherwise EventAI might be choosen for pets/totems
47 // excplicit check for isControlled() and owner type to allow guardian, mini-pets and pets controlled by NPCs to be scripted by EventAI
48 Unit* owner = NULL;
49 if ((creature->IsPet() && ((Pet*)creature)->isControlled() &&
50 ((owner = creature->GetOwner()) && owner->GetTypeId() == TYPEID_PLAYER)) || creature->isCharmed())
51 ai_factory = ai_registry.GetRegistryItem("PetAI");
52 else if (creature->IsTotem())
53 ai_factory = ai_registry.GetRegistryItem("TotemAI");
55 // select by script name
56 if (!ai_factory && !ainame.empty())
57 ai_factory = ai_registry.GetRegistryItem(ainame.c_str());
59 if (!ai_factory && creature->IsGuard())
60 ai_factory = ai_registry.GetRegistryItem("GuardAI");
62 // select by permit check
63 if (!ai_factory)
65 int best_val = PERMIT_BASE_NO;
66 typedef CreatureAIRegistry::RegistryMapType RMT;
67 RMT const& l = ai_registry.GetRegisteredItems();
68 for (RMT::const_iterator iter = l.begin(); iter != l.end(); ++iter)
70 const CreatureAICreator* factory = iter->second;
71 const SelectableAI* p = dynamic_cast<const SelectableAI*>(factory);
72 MANGOS_ASSERT(p != NULL);
73 int val = p->Permit(creature);
74 if (val > best_val)
76 best_val = val;
77 ai_factory = p;
82 // select NullCreatureAI if not another cases
83 ainame = (ai_factory == NULL) ? "NullCreatureAI" : ai_factory->key();
85 DEBUG_FILTER_LOG(LOG_FILTER_AI_AND_MOVEGENSS, "Creature %u used AI is %s.", creature->GetGUIDLow(), ainame.c_str());
86 return (ai_factory == NULL ? new NullCreatureAI(creature) : ai_factory->Create(creature));
89 MovementGenerator* selectMovementGenerator(Creature* creature)
91 MovementGeneratorRegistry& mv_registry(MovementGeneratorRepository::Instance());
92 MANGOS_ASSERT(creature->GetCreatureInfo() != NULL);
93 MovementGeneratorCreator const* mv_factory = mv_registry.GetRegistryItem(
94 creature->GetOwnerGuid().IsPlayer() ? FOLLOW_MOTION_TYPE : creature->GetDefaultMovementType());
96 /* if( mv_factory == NULL )
98 int best_val = -1;
99 std::vector<std::string> l;
100 mv_registry.GetRegisteredItems(l);
101 for( std::vector<std::string>::iterator iter = l.begin(); iter != l.end(); ++iter)
103 const MovementGeneratorCreator *factory = mv_registry.GetRegistryItem((*iter).c_str());
104 const SelectableMovement *p = dynamic_cast<const SelectableMovement *>(factory);
105 ASSERT( p != NULL );
106 int val = p->Permit(creature);
107 if( val > best_val )
109 best_val = val;
110 mv_factory = p;
115 return (mv_factory == NULL ? NULL : mv_factory->Create(creature));