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

Revision 44, 4.5 kB (checked in by yumileroy, 17 years ago)

[svn] * Merge Temp dev SVN with Assembla.
* Changes include:

  • Implementation of w12x's Outdoor PvP and Game Event Systems.
  • Temporary removal of IRC Chat Bot (until infinite loop when disabled is fixed).
  • All mangos -> trinity (to convert your mangos_string table, please run mangos_string_to_trinity_string.sql).
  • Improved Config cleanup.
  • And many more changes.

Original author: Seline
Date: 2008-10-14 11:57:03-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        }
64
65        // select by permit check
66        if(!ai_factory)
67        {
68            int best_val = -1;
69            typedef CreatureAIRegistry::RegistryMapType RMT;
70            RMT const &l = ai_registry.GetRegisteredItems();
71            for( RMT::const_iterator iter = l.begin(); iter != l.end(); ++iter)
72            {
73                const CreatureAICreator *factory = iter->second;
74                const SelectableAI *p = dynamic_cast<const SelectableAI *>(factory);
75                assert( p != NULL );
76                int val = p->Permit(creature);
77                if( val > best_val )
78                {
79                    best_val = val;
80                    ai_factory = p;
81                }
82            }
83        }
84
85        // select NullCreatureAI if not another cases
86        ainame = (ai_factory == NULL) ? "NullCreatureAI" : ai_factory->key();
87
88        DEBUG_LOG("Creature %u used AI is %s.", creature->GetGUIDLow(), ainame.c_str() );
89        return ( ai_factory == NULL ? new NullCreatureAI : ai_factory->Create(creature) );
90    }
91
92    MovementGenerator* selectMovementGenerator(Creature *creature)
93    {
94        MovementGeneratorRegistry &mv_registry(MovementGeneratorRepository::Instance());
95        assert( creature->GetCreatureInfo() != NULL );
96        const MovementGeneratorCreator *mv_factory = mv_registry.GetRegistryItem( creature->GetDefaultMovementType());
97
98        /* if( mv_factory == NULL  )
99        {
100            int best_val = -1;
101            std::vector<std::string> l;
102            mv_registry.GetRegisteredItems(l);
103            for( std::vector<std::string>::iterator iter = l.begin(); iter != l.end(); ++iter)
104            {
105            const MovementGeneratorCreator *factory = mv_registry.GetRegistryItem((*iter).c_str());
106            const SelectableMovement *p = dynamic_cast<const SelectableMovement *>(factory);
107            assert( p != NULL );
108            int val = p->Permit(creature);
109            if( val > best_val )
110            {
111                best_val = val;
112                mv_factory = p;
113            }
114            }
115        }*/
116
117        return ( mv_factory == NULL ? NULL : mv_factory->Create(creature) );
118
119    }
120}
Note: See TracBrowser for help on using the browser.