summaryrefslogtreecommitdiff
path: root/src/resources/questdb.cpp
blob: 1424c20efdb0cc79929f2b0f413219fc4b080111 (plain) (blame)
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
/*
 *  The Mana Client
 *  Copyright (C) 2025  The Mana Developers
 *
 *  This file is part of The Mana Client.
 *
 *  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
 *  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 "resources/questdb.h"
#include "log.h"

#include <algorithm>
#include <unordered_map>
#include <utility>

namespace QuestDB {

// The quests are stored in a map using their variable ID as the key
static std::unordered_map<int, Quest> quests;

// Helper function to check if a container contains a value
template<typename Container, typename Value>
static bool contains(const Container &container, const Value &value)
{
    return std::find(container.begin(), container.end(), value) != container.end();
}

void init()
{
    unload();
}

void readQuestVarNode(XML::Node node, const std::string &filename)
{
    int varId = 0;
    if (!node.attribute("id", varId))
        return;

    Quest &quest = quests[varId];

    for (auto child : node.children())
    {
        if (child.name() == "effect")
        {
            QuestEffect &effect = quest.effects.emplace_back();
            child.attribute("map", effect.map);
            child.attribute("npc", effect.npcId);
            child.attribute("effect", effect.statusEffectId);
            child.attribute("value", effect.values);

            if (effect.map.empty() || effect.npcId == 0 || effect.statusEffectId == 0 || effect.values.empty())
            {
                Log::warn("effect node for var %d is missing required attributes", varId);
            }
        }
        else if (child.name() == "quest")
        {
            QuestState &state = quest.states.emplace_back();
            child.attribute("name", state.name);
            child.attribute("group", state.group);
            child.attribute("incomplete", state.incomplete);
            child.attribute("complete", state.complete);

            if (state.incomplete.empty() && state.complete.empty())
            {
                Log::warn("quest node for var %d ('%s') has neither 'complete' nor 'incomplete' values",
                          varId, state.name.c_str());
                continue;
            }

            for (auto questChild : child.children())
            {
                QuestRowType rowType;
                std::string_view tag = questChild.name();
                if (tag == "text")
                    rowType = QuestRowType::Text;
                else if (tag == "name")
                    rowType = QuestRowType::Name;
                else if (tag == "reward")
                    rowType = QuestRowType::Reward;
                else if (tag == "questgiver" || tag == "giver")
                    rowType = QuestRowType::Giver;
                else if (tag == "coordinates")
                    rowType = QuestRowType::Coordinates;
                else if (tag == "npc")
                    rowType = QuestRowType::NPC;
                else
                {
                    Log::warn("unknown quest row type '%s' for var %d ('%s')",
                              tag.data(), varId, state.name.c_str());
                    continue;
                }

                QuestRow &row = state.rows.emplace_back(rowType);
                row.text = questChild.textContent();

                if (rowType == QuestRowType::Coordinates)
                {
                    questChild.attribute("x", row.x);
                    questChild.attribute("y", row.y);
                }
            }
        }
    }
}

void unload()
{
    quests.clear();
}

bool hasQuests()
{
    return !quests.empty();
}

// In quests, the map name may include the file extension. This is discouraged
// but supported for compatibility.
static std::string_view baseName(const std::string &fileName)
{
    auto pos = fileName.find_last_of('.');
    return pos == std::string::npos ? fileName : std::string_view(fileName.data(), pos);
}

QuestEffectMap getActiveEffects(const QuestVars &questVars,
                                const std::string &mapName)
{
    QuestEffectMap activeEffects;

    for (auto &[var, quest] : std::as_const(quests))
    {
        auto value = questVars.get(var);

        for (auto &effect : quest.effects)
        {
            if (baseName(effect.map) != mapName)
                continue;
            if (!contains(effect.values, value))
                continue;

            activeEffects.set(effect.npcId, effect.statusEffectId);
        }
    }

    return activeEffects;
}

std::vector<QuestEntry> getQuestsEntries(const QuestVars &questVars,
                                         bool skipCompleted)
{
    std::vector<QuestEntry> activeQuests;

    for (auto &[varId, quest] : std::as_const(quests))
    {
        auto value = questVars.get(varId);

        for (auto &state : quest.states)
        {
            bool matchesIncomplete = contains(state.incomplete, value);
            bool matchesComplete = contains(state.complete, value);

            if (skipCompleted && matchesComplete)
                continue;

            if (matchesIncomplete || matchesComplete)
            {
                QuestEntry &entry = activeQuests.emplace_back();
                entry.varId = varId;
                entry.completed = matchesComplete;
                entry.state = &state;
            }
        }
    }

    return activeQuests;
}

static std::pair<int, int> countQuestEntries(const Quest &quest, int value)
{
    int totalEntries = 0;
    int completedEntries = 0;

    for (const auto &state : quest.states)
    {
        bool matchesIncomplete = contains(state.incomplete, value);
        bool matchesComplete = contains(state.complete, value);

        if (matchesIncomplete || matchesComplete)
        {
            totalEntries++;
            if (matchesComplete)
                completedEntries++;
        }
    }

    return { totalEntries, completedEntries };
}

QuestChange questChange(int varId, int oldValue, int newValue)
{
    if (newValue == oldValue)
        return QuestChange::None;

    auto questIt = quests.find(varId);
    if (questIt == quests.end())
        return QuestChange::None;

    const Quest &quest = questIt->second;

    auto [oldQuestEntries, oldCompletedEntries] = countQuestEntries(quest, oldValue);
    auto [newQuestEntries, newCompletedEntries] = countQuestEntries(quest, newValue);

    if (newCompletedEntries > oldCompletedEntries)
        return QuestChange::Completed;
    if (newQuestEntries > oldQuestEntries)
        return QuestChange::New;
    return QuestChange::None;
}

} // namespace QuestDB