aboutsummaryrefslogtreecommitdiff
path: root/src/server/game/AI/CreatureAISelector.cpp
blob: ec22e9e4ebae123996245a62bb893b3bcfe9b610 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "AIException.h"
#include "AreaTrigger.h"
#include "Creature.h"
#include "CreatureAISelector.h"
#include "CreatureAIFactory.h"
#include "Log.h"
#include "MovementGenerator.h"

#include "GameObject.h"
#include "GameObjectAIFactory.h"

#include "AreaTriggerAI.h"

#include "Conversation.h"
#include "ConversationAI.h"

#include "ScriptMgr.h"

namespace FactorySelector
{
    template <class T, class Value>
    inline int32 GetPermitFor(T const* obj, Value const& value)
    {
        Permissible<T> const* const p = ASSERT_NOTNULL(dynamic_cast<Permissible<T> const*>(value.second.get()));
        return p->Permit(obj);
    }

    template <class T>
    struct PermissibleOrderPred
    {
    public:
        PermissibleOrderPred(T const* obj) : _obj(obj) { }

        template <class Value>
        bool operator()(Value const& left, Value const& right) const
        {
            return GetPermitFor(_obj, left) < GetPermitFor(_obj, right);
        }

    private:
        T const* const _obj;
    };

    template <class AI, class T>
    inline FactoryHolder<AI, T> const* SelectFactory(T const* obj)
    {
        static_assert(std::is_same<AI, CreatureAI>::value || std::is_same<AI, GameObjectAI>::value, "Invalid template parameter");
        static_assert(std::is_same<AI, CreatureAI>::value == std::is_same<T, Creature>::value, "Incompatible AI for type");
        static_assert(std::is_same<AI, GameObjectAI>::value == std::is_same<T, GameObject>::value, "Incompatible AI for type");

        using AIRegistry = typename FactoryHolder<AI, T>::FactoryHolderRegistry;

        // AIName in db
        std::string const& aiName = obj->GetAIName();
        if (!aiName.empty())
            return AIRegistry::instance()->GetRegistryItem(aiName);

        // select by permit check
        typename AIRegistry::RegistryMapType const& items = AIRegistry::instance()->GetRegisteredItems();
        auto itr = std::max_element(items.begin(), items.end(), PermissibleOrderPred<T>(obj));
        if (itr != items.end() && GetPermitFor(obj, *itr) >= 0)
            return itr->second.get();

        // should _never_ happen, Null AI types defined as PERMIT_BASE_IDLE, it must've been found
        ABORT();
        return nullptr;
    }

    CreatureAI* SelectAI(Creature* creature)
    {
        // special pet case, if a tamed creature uses AIName (example SmartAI) we need to override it
        if (creature->IsPet())
            return ASSERT_NOTNULL(sCreatureAIRegistry->GetRegistryItem("PetAI"))->Create(creature);

        // scriptname in db
        try
        {
            if (CreatureAI* scriptedAI = sScriptMgr->GetCreatureAI(creature))
                return scriptedAI;
        }
        catch (InvalidAIException const& e)
        {
            TC_LOG_ERROR("entities.unit", "Exception trying to assign script '{}' to Creature (Entry: {}), this Creature will have a default AI. Exception message: {}",
                creature->GetScriptName(), creature->GetEntry(), e.what());
        }

        return SelectFactory<CreatureAI>(creature)->Create(creature);
    }

    uint32 GetSelectedAIId(Creature const* creature)
    {
        if (creature->IsPet())
        {
            auto const* registry = ASSERT_NOTNULL(sCreatureAIRegistry->GetRegistryItem("PetAI"));
            auto const* factory = dynamic_cast<SelectableAI<Creature, CreatureAI> const*>(registry);
            ASSERT(factory);

            return factory->GetScriptId();
        }

        if (uint32 id = creature->GetScriptId())
        {
            if (sScriptMgr->CanCreateCreatureAI(id))
            {
                return id;
            }
        }

        auto const* factory = dynamic_cast<SelectableAI<Creature, CreatureAI> const*>(SelectFactory<CreatureAI>(creature));
        ASSERT(factory);

        return factory->GetScriptId();
    }

    MovementGenerator* SelectMovementGenerator(Unit* unit)
    {
        MovementGeneratorType type = unit->GetDefaultMovementType();
        if (Creature* creature = unit->ToCreature())
            if (!creature->GetPlayerMovingMe())
                type = creature->GetDefaultMovementType();

        MovementGeneratorCreator const* mv_factory = sMovementGeneratorRegistry->GetRegistryItem(type);
        return ASSERT_NOTNULL(mv_factory)->Create(unit);
    }

    GameObjectAI* SelectGameObjectAI(GameObject* go)
    {
        // scriptname in db
        if (GameObjectAI* scriptedAI = sScriptMgr->GetGameObjectAI(go))
            return scriptedAI;

        return SelectFactory<GameObjectAI>(go)->Create(go);
    }

    uint32 GetSelectedAIId(GameObject const* go)
    {
        if (uint32 id = go->GetScriptId())
        {
            if (sScriptMgr->CanCreateGameObjectAI(id))
            {
                return id;
            }
        }

        auto const* factory = dynamic_cast<SelectableAI<GameObject, GameObjectAI> const*>(SelectFactory<GameObjectAI>(go));
        ASSERT(factory);

        return factory->GetScriptId();
    }

    static uint32 GetNullAreaTriggerAIScriptId()
    {
        return sObjectMgr->GetScriptId("NullAreaTriggerAI", false);
    }

    AreaTriggerAI* SelectAreaTriggerAI(AreaTrigger* at)
    {
        if (AreaTriggerAI* ai = sScriptMgr->GetAreaTriggerAI(at))
            return ai;
        else
            return new NullAreaTriggerAI(at, GetNullAreaTriggerAIScriptId());
    }

    uint32 GetSelectedAIId(AreaTrigger const* at)
    {
        if (uint32 id = at->GetScriptId())
        {
            if (sScriptMgr->CanCreateAreaTriggerAI(id))
            {
                return id;
            }
        }

        return GetNullAreaTriggerAIScriptId();
    }

    static uint32 GetNullConversationAIScriptId()
    {
        return sObjectMgr->GetScriptId("NullConversationAI", false);
    }

    ConversationAI* SelectConversationAI(Conversation* conversation)
    {
        if (ConversationAI* ai = sScriptMgr->GetConversationAI(conversation))
            return ai;

        return new NullConversationAI(conversation, GetNullConversationAIScriptId());
    }

    uint32 GetSelectedAIId(Conversation const* conversation)
    {
        if (uint32 id = conversation->GetScriptId())
        {
            if (sScriptMgr->CanCreateConversationAI(id))
                return id;
        }

        return GetNullConversationAIScriptId();
    }
}