aboutsummaryrefslogtreecommitdiff
path: root/src/server/game/Pools/QuestPools.cpp
blob: aae4f5df4d7d0be9cda63f230ad685bd60c29a4e (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
/*
 * 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 "QuestPools.h"
#include "Containers.h"
#include "DatabaseEnv.h"
#include "Log.h"
#include "ObjectMgr.h"
#include "QuestDef.h"
#include "Timer.h"
#include "Transaction.h"
#include <unordered_map>
#include <unordered_set>

/*static*/ QuestPoolMgr* QuestPoolMgr::instance()
{
    static QuestPoolMgr instance;
    return &instance;
}

static void RegeneratePool(QuestPool& pool)
{
    ASSERT(!pool.members.empty(), "Quest pool %u is empty", pool.poolId);
    uint32 const n = pool.members.size()-1;
    ASSERT(pool.numActive <= pool.members.size(), "Quest Pool %u requests %u spawns, but has only %u members.", pool.poolId, pool.numActive, uint32(pool.members.size()));
    pool.activeQuests.clear();
    for (uint32 i = 0; i < pool.numActive; ++i)
    {
        uint32 j = urand(i,n);
        if (i != j)
            std::swap(pool.members[i], pool.members[j]);
        for (uint32 quest : pool.members[i])
            pool.activeQuests.insert(quest);
    }
}

static void SaveToDB(QuestPool const& pool, SQLTransaction trans)
{
    PreparedStatement* delStmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_POOL_QUEST_SAVE);
    delStmt->setUInt32(0, pool.poolId);
    trans->Append(delStmt);

    for (uint32 questId : pool.activeQuests)
    {
        PreparedStatement* insStmt = CharacterDatabase.GetPreparedStatement(CHAR_INS_POOL_QUEST_SAVE);
        insStmt->setUInt32(0, pool.poolId);
        insStmt->setUInt32(1, questId);
        trans->Append(insStmt);
    }
}

void QuestPoolMgr::LoadFromDB()
{
    uint32 oldMSTime = getMSTime();
    std::unordered_map<uint32, std::pair<std::vector<QuestPool>*, uint32>> lookup; // poolId -> (vector, index)

    _poolLookup.clear();
    _dailyPools.clear();
    _weeklyPools.clear();
    _monthlyPools.clear();

    // load template data from world DB
    {
        QueryResult result = WorldDatabase.Query("SELECT qpm.questId, qpm.poolId, qpm.poolIndex, qpt.numActive FROM quest_pool_members qpm LEFT JOIN quest_pool_template qpt ON qpm.poolId = qpt.poolId");
        if (!result)
        {
            TC_LOG_INFO("server.loading", ">> Loaded 0 quest pools. DB table `quest_pool_members` is empty.");
            return;
        }

        do
        {
            Field* fields = result->Fetch();
            if (fields[2].IsNull())
            {
                TC_LOG_ERROR("sql.sql", "Table `quest_pool_members` contains reference to non-existing pool %u. Skipped.", fields[1].GetUInt32());
                continue;
            }

            uint32 questId = fields[0].GetUInt32();
            uint32 poolId = fields[1].GetUInt32();
            uint32 poolIndex = fields[2].GetUInt8();
            uint32 numActive = fields[3].GetUInt32();

            Quest const* quest = sObjectMgr->GetQuestTemplate(questId);
            if (!quest)
            {
                TC_LOG_ERROR("sql.sql", "Table `quest_pool_members` contains reference to non-existing quest %u. Skipped.", questId);
                continue;
            }
            if (!quest->IsDailyOrWeekly() && !quest->IsMonthly())
            {
                TC_LOG_ERROR("sql.sql", "Table `quest_pool_members` contains reference to quest %u, which is neither daily, weekly nor monthly. Skipped.", questId);
                continue;
            }

            auto& pair = lookup[poolId];
            if (!pair.first)
            {
                pair.first = (quest->IsDaily() ? &_dailyPools : quest->IsWeekly() ? &_weeklyPools : &_monthlyPools);
                pair.first->emplace_back();
                pair.second = (pair.first->size()-1);

                pair.first->back().poolId = poolId;
                pair.first->back().numActive = numActive;
            }

            QuestPool::Members& members = (*pair.first)[pair.second].members;
            if (!(poolIndex < members.size()))
                members.resize(poolIndex+1);
            members[poolIndex].push_back(questId);
        } while (result->NextRow());
    }

    // load saved spawns from character DB
    {
        QueryResult result = CharacterDatabase.Query("SELECT pool_id, quest_id FROM pool_quest_save");
        if (result)
        {
            std::unordered_set<uint32> unknownPoolIds;
            do
            {
                Field* fields = result->Fetch();

                uint32 poolId = fields[0].GetUInt32();
                uint32 questId = fields[1].GetUInt32();

                auto it = lookup.find(poolId);
                if (it == lookup.end() || !it->second.first)
                {
                    TC_LOG_ERROR("sql.sql", "Table `pool_quest_save` contains reference to non-existant quest pool %u. Deleted.", poolId);
                    unknownPoolIds.insert(poolId);
                    continue;
                }

                (*it->second.first)[it->second.second].activeQuests.insert(questId);
            } while (result->NextRow());

            SQLTransaction trans = CharacterDatabase.BeginTransaction();
            for (uint32 poolId : unknownPoolIds)
            {
                PreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_POOL_QUEST_SAVE);
                stmt->setUInt32(0, poolId);
                trans->Append(stmt);
            }
            CharacterDatabase.CommitTransaction(trans);
        }
    }

    // post-processing and sanity checks
    SQLTransaction trans = CharacterDatabase.BeginTransaction();
    for (auto pair : lookup)
    {
        if (!pair.second.first)
            continue;
        QuestPool& pool = (*pair.second.first)[pair.second.second];
        if (pool.members.size() < pool.numActive)
        {
            TC_LOG_ERROR("sql.sql", "Table `quest_pool_template` contains quest pool %u requesting %u spawns, but only has %zu members. Requested spawns reduced.", pool.poolId, pool.numActive, pool.members.size());
            pool.numActive = pool.members.size();
        }

        bool doRegenerate = pool.activeQuests.empty();
        if (!doRegenerate)
        {
            std::unordered_set<uint32> accountedFor;
            uint32 activeCount = 0;
            for (size_t i = pool.members.size(); (i--);)
            {
                QuestPool::Member& member = pool.members[i];
                if (member.empty())
                {
                    TC_LOG_ERROR("sql.sql", "Table `quest_pool_members` contains no entries at index %zu for quest pool %u. Index removed.", i, pool.poolId);
                    std::swap(pool.members[i], pool.members.back());
                    pool.members.pop_back();
                    continue;
                }

                // check if the first member is active
                auto itFirst = pool.activeQuests.find(member[0]);
                bool status = (itFirst != pool.activeQuests.end());
                // temporarily remove any spawns that are accounted for
                if (status)
                {
                    accountedFor.insert(member[0]);
                    pool.activeQuests.erase(itFirst);
                }

                // now check if all other members also have the same status, and warn if not
                for (auto it = member.begin(); (++it) != member.end();)
                {
                    auto itOther = pool.activeQuests.find(*it);
                    bool otherStatus = (itOther != pool.activeQuests.end());
                    if (status != otherStatus)
                        TC_LOG_WARN("sql.sql", "Table `pool_quest_save` %s quest %u (in pool %u, index %zu) saved, but its index is%s active (because quest %u is%s in the table). Set quest %u to %sactive.", (status ? "does not have" : "has"), *it, pool.poolId, i, (status ? "" : " not"), member[0], (status ? "" : " not"), *it, (status ? "" : "in"));
                    if (otherStatus)
                        pool.activeQuests.erase(itOther);
                    if (status)
                        accountedFor.insert(*it);
                }

                if (status)
                    ++activeCount;
            }

            // warn for any remaining active spawns (not part of the pool)
            for (uint32 quest : pool.activeQuests)
                TC_LOG_WARN("sql.sql", "Table `pool_quest_save` has saved quest %u for pool %u, but that quest is not part of the pool. Skipped.", quest, pool.poolId);

            // only the previously-found spawns should actually be active
            std::swap(pool.activeQuests, accountedFor);

            if (activeCount != pool.numActive)
            {
                doRegenerate = true;
                TC_LOG_ERROR("sql.sql", "Table `pool_quest_save` has %u active members saved for pool %u, which requests %u active members. Pool spawns re-generated.", activeCount, pool.poolId, pool.numActive);
            }
        }

        if (doRegenerate)
        {
            RegeneratePool(pool);
            SaveToDB(pool, trans);
        }

        for (QuestPool::Member const& member : pool.members)
        {
            for (uint32 quest : member)
            {
                QuestPool*& ref = _poolLookup[quest];
                if (ref)
                {
                    TC_LOG_ERROR("sql.sql", "Table `quest_pool_members` lists quest %u as member of pool %u, but it is already a member of pool %u. Skipped.", quest, pool.poolId, ref->poolId);
                    continue;
                }
                ref = &pool;
            }
        }
    }
    CharacterDatabase.CommitTransaction(trans);

    TC_LOG_INFO("server.loading", ">> Loaded %zu daily, %zu weekly and %zu monthly quest pools in %u ms", _dailyPools.size(), _weeklyPools.size(), _monthlyPools.size(), GetMSTimeDiffToNow(oldMSTime));
}

void QuestPoolMgr::Regenerate(std::vector<QuestPool>& pools)
{
    SQLTransaction trans = CharacterDatabase.BeginTransaction();
    for (QuestPool& pool : pools)
    {
        RegeneratePool(pool);
        SaveToDB(pool, trans);
    }
    CharacterDatabase.CommitTransaction(trans);
}

// the storage structure ends up making this kind of inefficient
// we don't use it in practice (only in debug commands), so that's fine
QuestPool const* QuestPoolMgr::FindQuestPool(uint32 poolId) const
{
    auto lambda = [poolId](QuestPool const& p) { return (p.poolId == poolId); };
    auto it = std::find_if(_dailyPools.begin(), _dailyPools.end(), lambda);
    if (it != _dailyPools.end())
        return &*it;
    it = std::find_if(_weeklyPools.begin(), _weeklyPools.end(), lambda);
    if (it != _weeklyPools.end())
        return &*it;
    it = std::find_if(_monthlyPools.begin(), _monthlyPools.end(), lambda);
    if (it != _monthlyPools.end())
        return &*it;
    return nullptr;
}

bool QuestPoolMgr::IsQuestActive(uint32 questId) const
{
    auto it = _poolLookup.find(questId);
    if (it == _poolLookup.end()) // not pooled
        return true;

    return (it->second->activeQuests.find(questId) != it->second->activeQuests.end());
}