2 * Copyright (C) 2005-2009 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());
41 assert( creature
->GetCreatureInfo() != NULL
);
42 CreatureInfo
const *cinfo
=creature
->GetCreatureInfo();
44 const CreatureAICreator
*ai_factory
= NULL
;
46 std::string ainame
=cinfo
->AIName
;
48 // select by script name
50 ai_factory
= ai_registry
.GetRegistryItem( ainame
.c_str() );
52 // select by NPC flags
55 if( creature
->isGuard() )
56 ai_factory
= ai_registry
.GetRegistryItem("GuardAI");
57 else if(creature
->isPet() || creature
->isCharmed())
58 ai_factory
= ai_registry
.GetRegistryItem("PetAI");
59 else if(creature
->isTotem())
60 ai_factory
= ai_registry
.GetRegistryItem("TotemAI");
63 // select by permit check
67 typedef CreatureAIRegistry::RegistryMapType RMT
;
68 RMT
const &l
= ai_registry
.GetRegisteredItems();
69 for( RMT::const_iterator iter
= l
.begin(); iter
!= l
.end(); ++iter
)
71 const CreatureAICreator
*factory
= iter
->second
;
72 const SelectableAI
*p
= dynamic_cast<const SelectableAI
*>(factory
);
74 int val
= p
->Permit(creature
);
83 // select NullCreatureAI if not another cases
84 ainame
= (ai_factory
== NULL
) ? "NullCreatureAI" : ai_factory
->key();
86 DEBUG_LOG("Creature %u used AI is %s.", creature
->GetGUIDLow(), ainame
.c_str() );
87 return ( ai_factory
== NULL
? new NullCreatureAI
: ai_factory
->Create(creature
) );
90 MovementGenerator
* selectMovementGenerator(Creature
*creature
)
92 MovementGeneratorRegistry
&mv_registry(MovementGeneratorRepository::Instance());
93 assert( creature
->GetCreatureInfo() != NULL
);
94 const MovementGeneratorCreator
*mv_factory
= mv_registry
.GetRegistryItem( creature
->GetDefaultMovementType());
96 /* if( mv_factory == NULL )
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);
106 int val = p->Permit(creature);
115 return ( mv_factory
== NULL
? NULL
: mv_factory
->Create(creature
) );