root/trunk/src/game/CreatureAISelector.cpp @ 37

Revision 37, 4.4 kB (checked in by yumileroy, 17 years ago)

[svn] * svn:eol-style native set on all files that need it

Original author: Neo2003
Date: 2008-10-11 14:16:25-05:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
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.
8 *
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.
13 *
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
17 */
18
19#include "Creature.h"
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"
26#include "Pet.h"
27
28INSTANTIATE_SINGLETON_1(CreatureAIRegistry);
29INSTANTIATE_SINGLETON_1(MovementGeneratorRegistry);
30
31namespace FactorySelector
32{
33    CreatureAI* selectAI(Creature *creature)
34    {
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))
38                return scriptedAI;
39
40        CreatureAIRegistry &ai_registry(CreatureAIRepository::Instance());
41        assert( creature->GetCreatureInfo() != NULL );
42        CreatureInfo const *cinfo=creature->GetCreatureInfo();
43
44        const CreatureAICreator *ai_factory = NULL;
45
46        std::string ainame=cinfo->AIName;
47
48        // select by script name
49        if( !ainame.empty())
50            ai_factory = ai_registry.GetRegistryItem( ainame.c_str() );
51
52        // select by NPC flags
53        if(!ai_factory)
54        {
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");
61        }
62
63        // select by permit check
64        if(!ai_factory)
65        {
66            int best_val = -1;
67            typedef CreatureAIRegistry::RegistryMapType RMT;
68            RMT const &l = ai_registry.GetRegisteredItems();
69            for( RMT::const_iterator iter = l.begin(); iter != l.end(); ++iter)
70            {
71                const CreatureAICreator *factory = iter->second;
72                const SelectableAI *p = dynamic_cast<const SelectableAI *>(factory);
73                assert( p != NULL );
74                int val = p->Permit(creature);
75                if( val > best_val )
76                {
77                    best_val = val;
78                    ai_factory = p;
79                }
80            }
81        }
82
83        // select NullCreatureAI if not another cases
84        ainame = (ai_factory == NULL) ? "NullCreatureAI" : ai_factory->key();
85
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) );
88    }
89
90    MovementGenerator* selectMovementGenerator(Creature *creature)
91    {
92        MovementGeneratorRegistry &mv_registry(MovementGeneratorRepository::Instance());
93        assert( creature->GetCreatureInfo() != NULL );
94        const MovementGeneratorCreator *mv_factory = mv_registry.GetRegistryItem( creature->GetDefaultMovementType());
95
96        /* if( mv_factory == NULL  )
97        {
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)
102            {
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 )
108            {
109                best_val = val;
110                mv_factory = p;
111            }
112            }
113        }*/
114
115        return ( mv_factory == NULL ? NULL : mv_factory->Create(creature) );
116
117    }
118}
Note: See TracBrowser for help on using the browser.