2 * Copyright (C) 2005-2010 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
20 #include "CreatureAIImpl.h"
21 #include "CreatureAISelector.h"
22 #include "NullCreatureAI.h"
23 #include "Policies/SingletonImp.h"
24 #include "MovementGenerator.h"
25 #include "ScriptCalls.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
= Script
->GetAI(creature
))
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
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
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
);
73 int val
= p
->Permit(creature
);
82 // select NullCreatureAI if not another cases
83 ainame
= (ai_factory
== NULL
) ? "NullCreatureAI" : ai_factory
->key();
85 DEBUG_LOG("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 assert( creature
->GetCreatureInfo() != NULL
);
93 const MovementGeneratorCreator
*mv_factory
= mv_registry
.GetRegistryItem( creature
->GetDefaultMovementType());
95 /* if( mv_factory == NULL )
98 std::vector<std::string> l;
99 mv_registry.GetRegisteredItems(l);
100 for( std::vector<std::string>::iterator iter = l.begin(); iter != l.end(); ++iter)
102 const MovementGeneratorCreator *factory = mv_registry.GetRegistryItem((*iter).c_str());
103 const SelectableMovement *p = dynamic_cast<const SelectableMovement *>(factory);
105 int val = p->Permit(creature);
114 return ( mv_factory
== NULL
? NULL
: mv_factory
->Create(creature
) );