Mana
Loading...
Searching...
No Matches
questdb.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2025 The Mana Developers
4 *
5 * This file is part of The Mana Client.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include "resources/questdb.h"
22#include "log.h"
23
24#include <algorithm>
25#include <unordered_map>
26#include <utility>
27
28namespace QuestDB {
29
30// The quests are stored in a map using their variable ID as the key
31static std::unordered_map<int, Quest> quests;
32
33// Helper function to check if a container contains a value
34template<typename Container, typename Value>
35static bool contains(const Container &container, const Value &value)
36{
37 return std::find(container.begin(), container.end(), value) != container.end();
38}
39
40void init()
41{
42 unload();
43}
44
45void readQuestVarNode(XML::Node node, const std::string &filename)
46{
47 int varId = 0;
48 if (!node.attribute("id", varId))
49 return;
50
51 Quest &quest = quests[varId];
52
53 for (auto child : node.children())
54 {
55 if (child.name() == "effect")
56 {
57 QuestEffect &effect = quest.effects.emplace_back();
58 child.attribute("map", effect.map);
59 child.attribute("npc", effect.npcId);
60 child.attribute("effect", effect.statusEffectId);
61 child.attribute("value", effect.values);
62
63 if (effect.map.empty() || effect.npcId == 0 || effect.statusEffectId == 0 || effect.values.empty())
64 {
65 Log::warn("effect node for var %d is missing required attributes", varId);
66 }
67 }
68 else if (child.name() == "quest")
69 {
70 QuestState &state = quest.states.emplace_back();
71 child.attribute("name", state.name);
72 child.attribute("group", state.group);
73 child.attribute("incomplete", state.incomplete);
74 child.attribute("complete", state.complete);
75
76 if (state.incomplete.empty() && state.complete.empty())
77 {
78 Log::warn("quest node for var %d ('%s') has neither 'complete' nor 'incomplete' values",
79 varId, state.name.c_str());
80 continue;
81 }
82
83 for (auto questChild : child.children())
84 {
85 QuestRowType rowType;
86 std::string_view tag = questChild.name();
87 if (tag == "text")
88 rowType = QuestRowType::Text;
89 else if (tag == "name")
90 rowType = QuestRowType::Name;
91 else if (tag == "reward")
92 rowType = QuestRowType::Reward;
93 else if (tag == "questgiver" || tag == "giver")
94 rowType = QuestRowType::Giver;
95 else if (tag == "coordinates")
97 else if (tag == "npc")
98 rowType = QuestRowType::NPC;
99 else
100 {
101 Log::warn("unknown quest row type '%s' for var %d ('%s')",
102 tag.data(), varId, state.name.c_str());
103 continue;
104 }
105
106 QuestRow &row = state.rows.emplace_back(rowType);
107 row.text = questChild.textContent();
108
109 if (rowType == QuestRowType::Coordinates)
110 {
111 questChild.attribute("x", row.x);
112 questChild.attribute("y", row.y);
113 }
114 }
115 }
116 }
117}
118
119void unload()
120{
121 quests.clear();
122}
123
125{
126 return !quests.empty();
127}
128
129// In quests, the map name may include the file extension. This is discouraged
130// but supported for compatibility.
131static std::string_view baseName(const std::string &fileName)
132{
133 auto pos = fileName.find_last_of('.');
134 return pos == std::string::npos ? fileName : std::string_view(fileName.data(), pos);
135}
136
138 const std::string &mapName)
139{
140 QuestEffectMap activeEffects;
141
142 for (auto &[var, quest] : std::as_const(quests))
143 {
144 auto value = questVars.get(var);
145
146 for (auto &effect : quest.effects)
147 {
148 if (baseName(effect.map) != mapName)
149 continue;
150 if (!contains(effect.values, value))
151 continue;
152
153 activeEffects.set(effect.npcId, effect.statusEffectId);
154 }
155 }
156
157 return activeEffects;
158}
159
160std::vector<QuestEntry> getQuestsEntries(const QuestVars &questVars,
161 bool skipCompleted)
162{
163 std::vector<QuestEntry> activeQuests;
164
165 for (auto &[varId, quest] : std::as_const(quests))
166 {
167 auto value = questVars.get(varId);
168
169 for (auto &state : quest.states)
170 {
171 bool matchesIncomplete = contains(state.incomplete, value);
172 bool matchesComplete = contains(state.complete, value);
173
174 if (skipCompleted && matchesComplete)
175 continue;
176
177 if (matchesIncomplete || matchesComplete)
178 {
179 QuestEntry &entry = activeQuests.emplace_back();
180 entry.varId = varId;
181 entry.completed = matchesComplete;
182 entry.state = &state;
183 }
184 }
185 }
186
187 return activeQuests;
188}
189
190static std::pair<int, int> countQuestEntries(const Quest &quest, int value)
191{
192 int totalEntries = 0;
193 int completedEntries = 0;
194
195 for (const auto &state : quest.states)
196 {
197 bool matchesIncomplete = contains(state.incomplete, value);
198 bool matchesComplete = contains(state.complete, value);
199
200 if (matchesIncomplete || matchesComplete)
201 {
202 totalEntries++;
203 if (matchesComplete)
204 completedEntries++;
205 }
206 }
207
208 return { totalEntries, completedEntries };
209}
210
211QuestChange questChange(int varId, int oldValue, int newValue)
212{
213 if (newValue == oldValue)
214 return QuestChange::None;
215
216 auto questIt = quests.find(varId);
217 if (questIt == quests.end())
218 return QuestChange::None;
219
220 const Quest &quest = questIt->second;
221
222 auto [oldQuestEntries, oldCompletedEntries] = countQuestEntries(quest, oldValue);
223 auto [newQuestEntries, newCompletedEntries] = countQuestEntries(quest, newValue);
224
225 if (newCompletedEntries > oldCompletedEntries)
227 if (newQuestEntries > oldQuestEntries)
228 return QuestChange::New;
229 return QuestChange::None;
230}
231
232} // namespace QuestDB
A widget container.
Definition container.h:41
Value get(Key key) const
Definition questdb.h:41
void set(Key key, Value value)
Definition questdb.h:36
Children children() const
Definition xml.h:97
bool attribute(const char *name, T &value) const
Definition xml.h:129
void warn(const char *log_text,...) LOG_PRINTF_ATTR
QuestChange questChange(int varId, int oldValue, int newValue)
Definition questdb.cpp:211
void unload()
Definition questdb.cpp:119
QuestEffectMap getActiveEffects(const QuestVars &questVars, const std::string &mapName)
Definition questdb.cpp:137
bool hasQuests()
Definition questdb.cpp:124
void readQuestVarNode(XML::Node node, const std::string &filename)
Definition questdb.cpp:45
std::vector< QuestEntry > getQuestsEntries(const QuestVars &questVars, bool skipCompleted)
Definition questdb.cpp:160
void init()
Definition questdb.cpp:40
QuestRowType
Definition questdb.h:71
QuestChange
Definition questdb.h:118
int statusEffectId
Definition questdb.h:61
int npcId
Definition questdb.h:60
std::string map
Definition questdb.h:59
std::vector< int > values
Definition questdb.h:58
bool completed
Definition questdb.h:110
int varId
Definition questdb.h:109
const QuestState * state
Definition questdb.h:111
int x
Definition questdb.h:88
std::string text
Definition questdb.h:87
int y
Definition questdb.h:89
std::string name
Definition questdb.h:94
std::string group
Definition questdb.h:95
std::vector< int > incomplete
Definition questdb.h:96
std::vector< QuestRow > rows
Definition questdb.h:98
std::vector< int > complete
Definition questdb.h:97
std::vector< QuestEffect > effects
Definition questdb.h:103
std::vector< QuestState > states
Definition questdb.h:104