TrinityCore
Loading...
Searching...
No Matches
QuestPools.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "QuestPools.h"
19#include "Containers.h"
20#include "DatabaseEnv.h"
21#include "Log.h"
22#include "ObjectMgr.h"
23#include "QuestDef.h"
24#include "Timer.h"
25#include <unordered_map>
26#include <unordered_set>
27
30
32{
34 return &instance;
35}
36
37static void RegeneratePool(QuestPool& pool)
38{
39 ASSERT(!pool.members.empty(), "Quest pool %u is empty", pool.poolId);
40 uint32 const n = pool.members.size()-1;
41 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()));
42 pool.activeQuests.clear();
43 for (uint32 i = 0; i < pool.numActive; ++i)
44 {
45 uint32 j = urand(i,n);
46 if (i != j)
47 std::swap(pool.members[i], pool.members[j]);
48 for (uint32 quest : pool.members[i])
49 pool.activeQuests.insert(quest);
50 }
51}
52
53static void SaveToDB(QuestPool const& pool, CharacterDatabaseTransaction trans)
54{
56 delStmt->setUInt32(0, pool.poolId);
57 trans->Append(delStmt);
58
59 for (uint32 questId : pool.activeQuests)
60 {
62 insStmt->setUInt32(0, pool.poolId);
63 insStmt->setUInt32(1, questId);
64 trans->Append(insStmt);
65 }
66}
67
69{
70 uint32 oldMSTime = getMSTime();
71 std::unordered_map<uint32, std::pair<std::vector<QuestPool>*, uint32>> lookup; // poolId -> (vector, index)
72
73 _poolLookup.clear();
74 _dailyPools.clear();
75 _weeklyPools.clear();
76 _monthlyPools.clear();
77
78 // load template data from world DB
79 {
80 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");
81 if (!result)
82 {
83 TC_LOG_INFO("server.loading", ">> Loaded 0 quest pools. DB table `quest_pool_members` is empty.");
84 return;
85 }
86
87 do
88 {
89 Field* fields = result->Fetch();
90 if (fields[2].IsNull())
91 {
92 TC_LOG_ERROR("sql.sql", "Table `quest_pool_members` contains reference to non-existing pool {}. Skipped.", fields[1].GetUInt32());
93 continue;
94 }
95
96 uint32 questId = fields[0].GetUInt32();
97 uint32 poolId = fields[1].GetUInt32();
98 uint32 poolIndex = fields[2].GetUInt8();
99 uint32 numActive = fields[3].GetUInt32();
100
101 Quest const* quest = sObjectMgr->GetQuestTemplate(questId);
102 if (!quest)
103 {
104 TC_LOG_ERROR("sql.sql", "Table `quest_pool_members` contains reference to non-existing quest {}. Skipped.", questId);
105 continue;
106 }
107 if (!quest->IsDailyOrWeekly() && !quest->IsMonthly())
108 {
109 TC_LOG_ERROR("sql.sql", "Table `quest_pool_members` contains reference to quest {}, which is neither daily, weekly nor monthly. Skipped.", questId);
110 continue;
111 }
112
113 auto& pair = lookup[poolId];
114 if (!pair.first)
115 {
116 pair.first = (quest->IsDaily() ? &_dailyPools : quest->IsWeekly() ? &_weeklyPools : &_monthlyPools);
117 pair.first->emplace_back();
118 pair.second = (pair.first->size()-1);
119
120 pair.first->back().poolId = poolId;
121 pair.first->back().numActive = numActive;
122 }
123
124 QuestPool::Members& members = (*pair.first)[pair.second].members;
125 if (!(poolIndex < members.size()))
126 members.resize(poolIndex+1);
127 members[poolIndex].push_back(questId);
128 } while (result->NextRow());
129 }
130
131 // load saved spawns from character DB
132 {
133 QueryResult result = CharacterDatabase.Query("SELECT pool_id, quest_id FROM pool_quest_save");
134 if (result)
135 {
136 std::unordered_set<uint32> unknownPoolIds;
137 do
138 {
139 Field* fields = result->Fetch();
140
141 uint32 poolId = fields[0].GetUInt32();
142 uint32 questId = fields[1].GetUInt32();
143
144 auto it = lookup.find(poolId);
145 if (it == lookup.end() || !it->second.first)
146 {
147 TC_LOG_ERROR("sql.sql", "Table `pool_quest_save` contains reference to non-existant quest pool {}. Deleted.", poolId);
148 unknownPoolIds.insert(poolId);
149 continue;
150 }
151
152 (*it->second.first)[it->second.second].activeQuests.insert(questId);
153 } while (result->NextRow());
154
155 CharacterDatabaseTransaction trans = CharacterDatabase.BeginTransaction();
156 for (uint32 poolId : unknownPoolIds)
157 {
159 stmt->setUInt32(0, poolId);
160 trans->Append(stmt);
161 }
162 CharacterDatabase.CommitTransaction(trans);
163 }
164 }
165
166 // post-processing and sanity checks
167 CharacterDatabaseTransaction trans = CharacterDatabase.BeginTransaction();
168 for (auto const& pair : lookup)
169 {
170 if (!pair.second.first)
171 continue;
172 QuestPool& pool = (*pair.second.first)[pair.second.second];
173 if (pool.members.size() < pool.numActive)
174 {
175 TC_LOG_ERROR("sql.sql", "Table `quest_pool_template` contains quest pool {} requesting {} spawns, but only has {} members. Requested spawns reduced.", pool.poolId, pool.numActive, pool.members.size());
176 pool.numActive = pool.members.size();
177 }
178
179 bool doRegenerate = pool.activeQuests.empty();
180 if (!doRegenerate)
181 {
182 std::unordered_set<uint32> accountedFor;
183 uint32 activeCount = 0;
184 for (size_t i = pool.members.size(); (i--);)
185 {
186 QuestPool::Member& member = pool.members[i];
187 if (member.empty())
188 {
189 TC_LOG_ERROR("sql.sql", "Table `quest_pool_members` contains no entries at index {} for quest pool {}. Index removed.", i, pool.poolId);
190 std::swap(pool.members[i], pool.members.back());
191 pool.members.pop_back();
192 continue;
193 }
194
195 // check if the first member is active
196 auto itFirst = pool.activeQuests.find(member[0]);
197 bool status = (itFirst != pool.activeQuests.end());
198 // temporarily remove any spawns that are accounted for
199 if (status)
200 {
201 accountedFor.insert(member[0]);
202 pool.activeQuests.erase(itFirst);
203 }
204
205 // now check if all other members also have the same status, and warn if not
206 for (auto it = member.begin(); (++it) != member.end();)
207 {
208 auto itOther = pool.activeQuests.find(*it);
209 bool otherStatus = (itOther != pool.activeQuests.end());
210 if (status != otherStatus)
211 TC_LOG_WARN("sql.sql", "Table `pool_quest_save` {} quest {} (in pool {}, index {}) saved, but its index is{} active (because quest {} is{} in the table). Set quest {} to {}active.", (status ? "does not have" : "has"), *it, pool.poolId, i, (status ? "" : " not"), member[0], (status ? "" : " not"), *it, (status ? "" : "in"));
212 if (otherStatus)
213 pool.activeQuests.erase(itOther);
214 if (status)
215 accountedFor.insert(*it);
216 }
217
218 if (status)
219 ++activeCount;
220 }
221
222 // warn for any remaining active spawns (not part of the pool)
223 for (uint32 quest : pool.activeQuests)
224 TC_LOG_WARN("sql.sql", "Table `pool_quest_save` has saved quest {} for pool {}, but that quest is not part of the pool. Skipped.", quest, pool.poolId);
225
226 // only the previously-found spawns should actually be active
227 std::swap(pool.activeQuests, accountedFor);
228
229 if (activeCount != pool.numActive)
230 {
231 doRegenerate = true;
232 TC_LOG_ERROR("sql.sql", "Table `pool_quest_save` has {} active members saved for pool {}, which requests {} active members. Pool spawns re-generated.", activeCount, pool.poolId, pool.numActive);
233 }
234 }
235
236 if (doRegenerate)
237 {
238 RegeneratePool(pool);
239 SaveToDB(pool, trans);
240 }
241
242 for (QuestPool::Member const& member : pool.members)
243 {
244 for (uint32 quest : member)
245 {
246 QuestPool*& ref = _poolLookup[quest];
247 if (ref)
248 {
249 TC_LOG_ERROR("sql.sql", "Table `quest_pool_members` lists quest {} as member of pool {}, but it is already a member of pool {}. Skipped.", quest, pool.poolId, ref->poolId);
250 continue;
251 }
252 ref = &pool;
253 }
254 }
255 }
256 CharacterDatabase.CommitTransaction(trans);
257
258 TC_LOG_INFO("server.loading", ">> Loaded {} daily, {} weekly and {} monthly quest pools in {} ms", _dailyPools.size(), _weeklyPools.size(), _monthlyPools.size(), GetMSTimeDiffToNow(oldMSTime));
259}
260
261void QuestPoolMgr::Regenerate(std::vector<QuestPool>& pools)
262{
263 CharacterDatabaseTransaction trans = CharacterDatabase.BeginTransaction();
264 for (QuestPool& pool : pools)
265 {
266 RegeneratePool(pool);
267 SaveToDB(pool, trans);
268 }
269 CharacterDatabase.CommitTransaction(trans);
270}
271
272// the storage structure ends up making this kind of inefficient
273// we don't use it in practice (only in debug commands), so that's fine
275{
276 auto lambda = [poolId](QuestPool const& p) { return (p.poolId == poolId); };
277 auto it = std::find_if(_dailyPools.begin(), _dailyPools.end(), lambda);
278 if (it != _dailyPools.end())
279 return &*it;
280 it = std::find_if(_weeklyPools.begin(), _weeklyPools.end(), lambda);
281 if (it != _weeklyPools.end())
282 return &*it;
283 it = std::find_if(_monthlyPools.begin(), _monthlyPools.end(), lambda);
284 if (it != _monthlyPools.end())
285 return &*it;
286 return nullptr;
287}
288
290{
291 auto it = _poolLookup.find(questId);
292 if (it == _poolLookup.end()) // not pooled
293 return true;
294
295 return (it->second->activeQuests.find(questId) != it->second->activeQuests.end());
296}
@ CHAR_INS_POOL_QUEST_SAVE
@ CHAR_DEL_POOL_QUEST_SAVE
SQLTransaction< CharacterDatabaseConnection > CharacterDatabaseTransaction
std::shared_ptr< ResultSet > QueryResult
DatabaseWorkerPool< CharacterDatabaseConnection > CharacterDatabase
Accessor to the character database.
DatabaseWorkerPool< WorldDatabaseConnection > WorldDatabase
Accessor to the world database.
uint32_t uint32
Definition Define.h:154
#define ASSERT
Definition Errors.h:80
#define TC_LOG_ERROR(filterType__, message__,...)
Definition Log.h:190
#define TC_LOG_INFO(filterType__, message__,...)
Definition Log.h:184
#define TC_LOG_WARN(filterType__, message__,...)
Definition Log.h:187
#define sObjectMgr
Definition ObjectMgr.h:1885
static void RegeneratePool(QuestPool &pool)
static void SaveToDB(QuestPool const &pool, CharacterDatabaseTransaction trans)
uint32 urand(uint32 min, uint32 max)
Definition Random.cpp:42
uint32 GetMSTimeDiffToNow(uint32 oldMSTime)
Definition Timer.h:57
uint32 getMSTime()
Definition Timer.h:33
Class used to access individual fields of database query result.
Definition Field.h:94
uint32 GetUInt32() const noexcept
Definition Field.cpp:57
uint8 GetUInt8() const noexcept
Definition Field.cpp:29
void setUInt32(uint8 index, uint32 value)
std::vector< QuestPool > _dailyPools
Definition QuestPools.h:62
std::vector< QuestPool > _weeklyPools
Definition QuestPools.h:63
bool IsQuestActive(uint32 questId) const
std::unordered_map< uint32, QuestPool * > _poolLookup
Definition QuestPools.h:65
std::vector< QuestPool > _monthlyPools
Definition QuestPools.h:64
static QuestPoolMgr * instance()
void LoadFromDB()
void Regenerate(std::vector< QuestPool > &pools)
QuestPool const * FindQuestPool(uint32 poolId) const
bool IsDaily() const
Definition QuestDef.h:728
bool IsWeekly() const
Definition QuestDef.h:729
bool IsDailyOrWeekly() const
Definition QuestDef.h:732
bool IsMonthly() const
Definition QuestDef.h:730
std::vector< Member > Members
Definition QuestPools.h:29
uint32 numActive
Definition QuestPools.h:32
std::unordered_set< uint32 > activeQuests
Definition QuestPools.h:34
std::vector< uint32 > Member
Definition QuestPools.h:28
uint32 poolId
Definition QuestPools.h:31
Members members
Definition QuestPools.h:33