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

Revision 174, 4.7 kB (checked in by yumileroy, 17 years ago)

[svn] Implemented player on player and player on creature possession:
* Implemented packet and vision forwarding through possessed units
* Added new OnPossess? script call alerting scripts on when possession is applied/removed
* Moved fall damage and fall under map calculations into the Player class
* Added new PossessedAI that is applied only while possession on creature is active
* Implemented summon possessed spell effect
* Fixed Eyes of the Beast

Original author: gvcoman
Date: 2008-11-05 20:51:05-06:00

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