aboutsummaryrefslogtreecommitdiff
path: root/src/server/game/Globals/ConversationDataStore.cpp
blob: eeba90a20329b52b94655c7c7d657fcd9411dc0a (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
 * 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 "ConversationDataStore.h"
#include "DB2Stores.h"
#include "DatabaseEnv.h"
#include "Log.h"
#include "MapUtils.h"
#include "ObjectMgr.h"
#include "Timer.h"

namespace
{
    std::unordered_map<uint32, ConversationTemplate>       _conversationTemplateStore;
    std::unordered_map<uint32, ConversationLineTemplate>   _conversationLineTemplateStore;
}

void ConversationDataStore::LoadConversationTemplates()
{
    _conversationLineTemplateStore.clear();
    _conversationTemplateStore.clear();

    std::unordered_map<uint32, std::vector<ConversationActorTemplate>> actorsByConversation;

    if (QueryResult lineTemplates = WorldDatabase.Query("SELECT Id, UiCameraID, ActorIdx, Flags, ChatType FROM conversation_line_template"))
    {
        uint32 oldMSTime = getMSTime();

        do
        {
            Field* fields = lineTemplates->Fetch();

            uint32 id = fields[0].GetUInt32();

            if (!sConversationLineStore.LookupEntry(id))
            {
                TC_LOG_ERROR("sql.sql", "Table `conversation_line_template` has template for non existing ConversationLine (ID: {}), skipped", id);
                continue;
            }

            ConversationLineTemplate& conversationLine = _conversationLineTemplateStore[id];
            conversationLine.Id         = id;
            conversationLine.UiCameraID = fields[1].GetUInt32();
            conversationLine.ActorIdx   = fields[2].GetUInt8();
            conversationLine.Flags      = fields[3].GetUInt8();
            conversationLine.ChatType   = fields[4].GetUInt8();
        }
        while (lineTemplates->NextRow());

        TC_LOG_INFO("server.loading", ">> Loaded {} Conversation line templates in {} ms", _conversationLineTemplateStore.size(), GetMSTimeDiffToNow(oldMSTime));
    }
    else
    {
        TC_LOG_INFO("server.loading", ">> Loaded 0 Conversation line templates. DB table `conversation_line_template` is empty.");
    }

    if (QueryResult actors = WorldDatabase.Query("SELECT ConversationId, ConversationActorId, ConversationActorGuid, Idx, CreatureId, CreatureDisplayInfoId, NoActorObject, ActivePlayerObject FROM conversation_actors"))
    {
        uint32 oldMSTime = getMSTime();
        uint32 count = 0;

        struct ConversationActorDbRow
        {
            uint32 ConversationId = 0;
            uint32 ActorIndex = 0;

            ObjectGuid::LowType SpawnId = 0;
            uint32 CreatureId = 0;
            uint32 CreatureDisplayInfoId = 0;

            bool operator()(ConversationActorWorldObjectTemplate& worldObject) const
            {
                if (!sObjectMgr->GetCreatureData(SpawnId))
                {
                    TC_LOG_ERROR("sql.sql", "Table `conversation_actors` references an invalid creature guid (GUID: {}) for Conversation {} and Idx {}, skipped.", SpawnId, ConversationId, ActorIndex);
                    return false;
                }

                if (CreatureId)
                    TC_LOG_ERROR("sql.sql", "Table `conversation_actors` with ConversationActorGuid cannot have CreatureId ({}). Conversation {} and Idx {}.", CreatureId, ConversationId, ActorIndex);

                if (CreatureDisplayInfoId)
                    TC_LOG_ERROR("sql.sql", "Table `conversation_actors` with ConversationActorGuid cannot have CreatureDisplayInfoId ({}). Conversation {} and Idx {}.", CreatureDisplayInfoId, ConversationId, ActorIndex);

                worldObject.SpawnId = SpawnId;
                return true;
            }

            bool operator()(ConversationActorNoObjectTemplate& noObject) const
            {
                if (!sObjectMgr->GetCreatureTemplate(CreatureId))
                {
                    TC_LOG_ERROR("sql.sql", "Table `conversation_actors` references an invalid creature id ({}) for Conversation {} and Idx {}, skipped.", CreatureId, ConversationId, ActorIndex);
                    return false;
                }

                if (CreatureDisplayInfoId && !sCreatureDisplayInfoStore.LookupEntry(CreatureDisplayInfoId))
                {
                    TC_LOG_ERROR("sql.sql", "Table `conversation_actors` references an invalid creature display id ({}) for Conversation {} and Idx {}, skipped.", CreatureDisplayInfoId, ConversationId, ActorIndex);
                    return false;
                }

                if (SpawnId)
                    TC_LOG_ERROR("sql.sql", "Table `conversation_actors` with NoActorObject cannot have ConversationActorGuid ({}). Conversation {} and Idx {}.", SpawnId, ConversationId, ActorIndex);

                noObject.CreatureId = CreatureId;
                noObject.CreatureDisplayInfoId = CreatureDisplayInfoId;
                return true;
            }

            bool operator()([[maybe_unused]] ConversationActorActivePlayerTemplate& activePlayer) const
            {
                if (SpawnId)
                    TC_LOG_ERROR("sql.sql", "Table `conversation_actors` with ActivePlayerObject cannot have ConversationActorGuid ({}). Conversation {} and Idx {}.", SpawnId, ConversationId, ActorIndex);

                if (CreatureId)
                    TC_LOG_ERROR("sql.sql", "Table `conversation_actors` with ActivePlayerObject cannot have CreatureId ({}). Conversation {} and Idx {}.", CreatureId, ConversationId, ActorIndex);

                if (CreatureDisplayInfoId)
                    TC_LOG_ERROR("sql.sql", "Table `conversation_actors` with ActivePlayerObject cannot have CreatureDisplayInfoId ({}). Conversation {} and Idx {}.", CreatureDisplayInfoId, ConversationId, ActorIndex);

                return true;
            }

            bool operator()(ConversationActorTalkingHeadTemplate& talkingHead) const
            {
                if (!sObjectMgr->GetCreatureTemplate(CreatureId))
                {
                    TC_LOG_ERROR("sql.sql", "Table `conversation_actors` references an invalid creature id ({}) for Conversation {} and Idx {}, skipped.", CreatureId, ConversationId, ActorIndex);
                    return false;
                }

                if (CreatureDisplayInfoId && !sCreatureDisplayInfoStore.LookupEntry(CreatureDisplayInfoId))
                {
                    TC_LOG_ERROR("sql.sql", "Table `conversation_actors` references an invalid creature display id ({}) for Conversation {} and Idx {}, skipped.", CreatureDisplayInfoId, ConversationId, ActorIndex);
                    return false;
                }

                if (SpawnId)
                    TC_LOG_ERROR("sql.sql", "Table `conversation_actors` with TalkingHead cannot have ConversationActorGuid ({}). Conversation {} and Idx {}.", SpawnId, ConversationId, ActorIndex);

                talkingHead.CreatureId = CreatureId;
                talkingHead.CreatureDisplayInfoId = CreatureDisplayInfoId;
                return true;
            }
        };

        do
        {
            Field* fields = actors->Fetch();

            ConversationActorDbRow data;
            ConversationActorTemplate actor;
            data.ConversationId           = fields[0].GetUInt32();
            actor.Id                      = fields[1].GetUInt32();
            data.SpawnId                  = fields[2].GetUInt64();
            data.ActorIndex = actor.Index = fields[3].GetUInt16();
            data.CreatureId               = fields[4].GetUInt32();
            data.CreatureDisplayInfoId    = fields[5].GetUInt32();
            bool noActorObject            = fields[6].GetUInt8() == 1;
            bool activePlayerObject       = fields[7].GetUInt8() == 1;

            if (activePlayerObject)
                actor.Data.emplace<ConversationActorActivePlayerTemplate>();
            else if (noActorObject)
                actor.Data.emplace<ConversationActorNoObjectTemplate>();
            else if (data.SpawnId || !data.CreatureId) // @TODO: remove CreatureId check when actor flags are implemented
                actor.Data.emplace<ConversationActorWorldObjectTemplate>();
            else
                actor.Data.emplace<ConversationActorTalkingHeadTemplate>();

            bool valid = std::visit(data, actor.Data);
            if (!valid)
                continue;

            actorsByConversation[data.ConversationId].push_back(actor);
            ++count;
        } while (actors->NextRow());

        TC_LOG_INFO("server.loading", ">> Loaded {} Conversation actors in {} ms", count, GetMSTimeDiffToNow(oldMSTime));
    }
    else
    {
        TC_LOG_INFO("server.loading", ">> Loaded 0 Conversation actors. DB table `conversation_actors` is empty.");
    }

    // TODO: Remove this hack when NextConversationLineID is changed to uint32
    auto getNextConversationLineId = [&](ConversationLineEntry const* conversationLine)
    {
        if (conversationLine && conversationLine->NextConversationLineID)
        {
            static constexpr uint32 FirstLineId = 60000; // Arbitrary id to cover the affected rows

            if (conversationLine->ID > FirstLineId && conversationLine->NextConversationLineID < (sConversationLineStore.GetNumRows() - USHRT_MAX - 1))
                return (uint32)(USHRT_MAX + conversationLine->NextConversationLineID + 1);

            return (uint32)conversationLine->NextConversationLineID;
        }

        return 0u;
    };

    // Validate FirstLineId
    std::unordered_map<uint32, uint32> prevConversationLineIds;
    for (ConversationLineEntry const* conversationLine : sConversationLineStore)
        if (uint32 nextConversationLineId = getNextConversationLineId(conversationLine))
            prevConversationLineIds[nextConversationLineId] = conversationLine->ID;

    auto getFirstLineIdFromAnyLineId = [&](uint32 lineId)
    {
        while (uint32 const* prevLineId = Trinity::Containers::MapGetValuePtr(prevConversationLineIds, lineId))
            lineId = *prevLineId;

        return lineId;
    };

    if (QueryResult templates = WorldDatabase.Query("SELECT Id, FirstLineId, TextureKitId, Flags, ScriptName FROM conversation_template"))
    {
        uint32 oldMSTime = getMSTime();

        do
        {
            Field* fields = templates->Fetch();

            ConversationTemplate conversationTemplate;
            conversationTemplate.Id                 = fields[0].GetUInt32();
            conversationTemplate.FirstLineId        = fields[1].GetUInt32();
            conversationTemplate.TextureKitId       = fields[2].GetUInt32();
            conversationTemplate.Flags              = (ConversationFlags)fields[3].GetUInt8();
            conversationTemplate.ScriptId           = sObjectMgr->GetScriptId(fields[4].GetStringView());

            conversationTemplate.Actors = std::move(actorsByConversation[conversationTemplate.Id]);

            uint32 correctedFirstLineId = getFirstLineIdFromAnyLineId(conversationTemplate.FirstLineId);
            if (conversationTemplate.FirstLineId != correctedFirstLineId)
            {
                TC_LOG_ERROR("sql.sql", "Table `conversation_template` has incorrect FirstLineId {}, it should be {} for Conversation {}, corrected",
                    conversationTemplate.FirstLineId, correctedFirstLineId, conversationTemplate.Id);
                conversationTemplate.FirstLineId = correctedFirstLineId;
            }

            ConversationLineEntry const* currentConversationLine = sConversationLineStore.LookupEntry(conversationTemplate.FirstLineId);
            if (!currentConversationLine)
                TC_LOG_ERROR("sql.sql", "Table `conversation_template` references an invalid line (ID: {}) for Conversation {}, skipped", conversationTemplate.FirstLineId, conversationTemplate.Id);

            while (currentConversationLine != nullptr)
            {
                if (ConversationLineTemplate const* conversationLineTemplate = Trinity::Containers::MapGetValuePtr(_conversationLineTemplateStore, currentConversationLine->ID))
                    conversationTemplate.Lines.push_back(conversationLineTemplate);
                else
                    TC_LOG_ERROR("sql.sql", "Table `conversation_line_template` has missing template for line (ID: {}) in Conversation {}, skipped", currentConversationLine->ID, conversationTemplate.Id);

                uint32 nextConversationLineId = getNextConversationLineId(currentConversationLine);
                if (!nextConversationLineId)
                    break;

                currentConversationLine = sConversationLineStore.AssertEntry(nextConversationLineId);
            }

            _conversationTemplateStore[conversationTemplate.Id] = std::move(conversationTemplate);
        }
        while (templates->NextRow());

        TC_LOG_INFO("server.loading", ">> Loaded {} Conversation templates in {} ms", _conversationTemplateStore.size(), GetMSTimeDiffToNow(oldMSTime));
    }
    else
    {
        TC_LOG_INFO("server.loading", ">> Loaded 0 Conversation templates. DB table `conversation_template` is empty.");
    }
}

ConversationTemplate const* ConversationDataStore::GetConversationTemplate(uint32 conversationId) const
{
    return Trinity::Containers::MapGetValuePtr(_conversationTemplateStore, conversationId);
}

ConversationLineTemplate const* ConversationDataStore::GetConversationLineTemplate(uint32 conversationLineId) const
{
    return Trinity::Containers::MapGetValuePtr(_conversationLineTemplateStore, conversationLineId);
}

ConversationDataStore* ConversationDataStore::Instance()
{
    static ConversationDataStore instance;
    return &instance;
}