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

Revision 81, 4.6 kB (checked in by yumileroy, 17 years ago)

[svn] Implement CREATURE_FLAG_EXTRA_TRIGGER. In GM mode, creatures flagged with this will be displayed with displayid_a, in non-gm mode with displayid_h.

Original author: w12x
Date: 2008-10-20 13:41:05-05:00

Line 
1/*
2 * Copyright (C) 2008 Trinity <http://www.trinitycore.org/>
3 *
4 * Thanks to the original authors: MaNGOS <http://www.mangosproject.org/>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include "Creature.h"
22#include "CreatureAIImpl.h"
23#include "CreatureAISelector.h"
24#include "NullCreatureAI.h"
25#include "Policies/SingletonImp.h"
26#include "MovementGenerator.h"
27#include "ScriptCalls.h"
28#include "Pet.h"
29
30INSTANTIATE_SINGLETON_1(CreatureAIRegistry);
31INSTANTIATE_SINGLETON_1(MovementGeneratorRegistry);
32
33namespace FactorySelector
34{
35    CreatureAI* selectAI(Creature *creature)
36    {
37        // Allow scripting AI for normal creatures and not controlled pets (guardians and mini-pets)
38        if((!creature->isPet() || !((Pet*)creature)->isControlled()) && !creature->isCharmed())
39            if(CreatureAI* scriptedAI = Script->GetAI(creature))
40                return scriptedAI;
41
42        CreatureAIRegistry &ai_registry(CreatureAIRepository::Instance());
43        assert( creature->GetCreatureInfo() != NULL );
44        CreatureInfo const *cinfo=creature->GetCreatureInfo();
45
46        const CreatureAICreator *ai_factory = NULL;
47
48        std::string ainame=cinfo->AIName;
49
50        // select by script name
51        if( !ainame.empty())
52            ai_factory = ai_registry.GetRegistryItem( ainame.c_str() );
53
54        // select by NPC flags
55        if(!ai_factory)
56        {
57            if( creature->isGuard() )
58                ai_factory = ai_registry.GetRegistryItem("GuardAI"); 
59            else if(creature->isPet() || creature->isCharmed())
60                ai_factory = ai_registry.GetRegistryItem("PetAI");
61            else if(creature->isTotem())
62                ai_factory = ai_registry.GetRegistryItem("TotemAI");
63            else if(creature->GetCreatureInfo()->flags_extra & CREATURE_FLAG_EXTRA_TRIGGER)
64                ai_factory = ai_registry.GetRegistryItem("NullCreatureAI");
65        }
66
67        // select by permit check
68        if(!ai_factory)
69        {
70            int best_val = -1;
71            typedef CreatureAIRegistry::RegistryMapType RMT;
72            RMT const &l = ai_registry.GetRegisteredItems();
73            for( RMT::const_iterator iter = l.begin(); iter != l.end(); ++iter)
74            {
75                const CreatureAICreator *factory = iter->second;
76                const SelectableAI *p = dynamic_cast<const SelectableAI *>(factory);
77                assert( p != NULL );
78                int val = p->Permit(creature);
79                if( val > best_val )
80                {
81                    best_val = val;
82                    ai_factory = p;
83                }
84            }
85        }
86
87        // select NullCreatureAI if not another cases
88        ainame = (ai_factory == NULL) ? "NullCreatureAI" : ai_factory->key();
89
90        DEBUG_LOG("Creature %u used AI is %s.", creature->GetGUIDLow(), ainame.c_str() );
91        return ( ai_factory == NULL ? new NullCreatureAI : ai_factory->Create(creature) );
92    }
93
94    MovementGenerator* selectMovementGenerator(Creature *creature)
95    {
96        MovementGeneratorRegistry &mv_registry(MovementGeneratorRepository::Instance());
97        assert( creature->GetCreatureInfo() != NULL );
98        const MovementGeneratorCreator *mv_factory = mv_registry.GetRegistryItem( creature->GetDefaultMovementType());
99
100        /* if( mv_factory == NULL  )
101        {
102            int best_val = -1;
103            std::vector<std::string> l;
104            mv_registry.GetRegisteredItems(l);
105            for( std::vector<std::string>::iterator iter = l.begin(); iter != l.end(); ++iter)
106            {
107            const MovementGeneratorCreator *factory = mv_registry.GetRegistryItem((*iter).c_str());
108            const SelectableMovement *p = dynamic_cast<const SelectableMovement *>(factory);
109            assert( p != NULL );
110            int val = p->Permit(creature);
111            if( val > best_val )
112            {
113                best_val = val;
114                mv_factory = p;
115            }
116            }
117        }*/
118
119        return ( mv_factory == NULL ? NULL : mv_factory->Create(creature) );
120
121    }
122}
Note: See TracBrowser for help on using the browser.