summaryrefslogtreecommitdiff
path: root/src/map/quest.cpp
blob: dfe19ff31f85631283e8b8ec810eaf914cb21034 (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
#include "quest.hpp"
//    quest.cpp - Quest Log.
//
//    Copyright © 2015 Ed Pasek <pasekei@gmail.com>
//
//    This file is part of The Mana World (Athena server)
//
//    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 3 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 <algorithm>

#include "../strings/astring.hpp"
#include "../strings/zstring.hpp"
#include "../strings/xstring.hpp"

#include "../generic/db.hpp"

#include "../io/cxxstdio.hpp"
#include "../io/extract.hpp"
#include "../io/line.hpp"

#include "../mmo/config_parse.hpp"
#include "../mmo/extract_enums.hpp"

#include "../ast/quest.hpp"

#include "../poison.hpp"
#include "globals.hpp"
#include "script-parse.hpp"

namespace tmwa
{
namespace map
{
// Function declarations

static
void questdb_searchname_sub(Borrowed<struct quest_data> quest, VarName str, Borrowed<Option<Borrowed<struct quest_data>>> dst)
{
    if (quest->quest_var == str)
        *dst = Some(quest);
}

Option<Borrowed<struct quest_data>> questdb_searchname(XString str_)
{
    VarName str = stringish<VarName>(str_);
    if (XString(str) != str_)
        return None;
    Option<P<struct quest_data>> quest = None;
    for (auto& pair : quest_db)
        questdb_searchname_sub(borrow(pair.second), str, borrow(quest));
    return quest;
}

Borrowed<struct quest_data> questdb_search(QuestId questid)
{
    Option<P<struct quest_data>> id_ = quest_db.search(questid);
    OMATCH_BEGIN_SOME (id, id_)
    {
        return id;
    }
    OMATCH_END ();

    P<struct quest_data> id = quest_db.init(questid);

    id->questid = questid;

    return id;
}

Option<Borrowed<struct quest_data>> questdb_exists(QuestId questid)
{
    return quest_db.search(questid);
}

bool quest_readdb(ZString filename)
{
    io::LineCharReader in(filename);

    if (!in.is_open())
    {
        PRINTF("can't read %s\n"_fmt, filename);
        return false;
    }

    int ln = 0;

    while (true)
    {
        auto res = TRY_UNWRAP(ast::quest::parse_quest(in),
                {
                    PRINTF("read %s done (count=%d)\n"_fmt, filename, ln);
                    return true;
                });
        if (res.get_failure())
            PRINTF("%s\n"_fmt, res.get_failure());
        ast::quest::QuestOrComment ioc = TRY_UNWRAP(std::move(res.get_success()), return false);

        MATCH_BEGIN (ioc)
        {
            MATCH_CASE (const ast::quest::Comment&, c)
            {
                (void)c;
            }
            MATCH_CASE (const ast::quest::Quest&, quest)
            {
                ln++;

                quest_data qdv {};
                qdv.questid = quest.questid.data;
                qdv.quest_var = quest.quest_var.data;
                qdv.quest_vr = quest.quest_vr.data;
                qdv.quest_shift = quest.quest_shift.data;
                qdv.quest_mask = quest.quest_mask.data;

                Borrowed<struct quest_data> id = questdb_search(qdv.questid);
                *id = std::move(qdv);
            }
        }
        MATCH_END ();
    }
}
} // namespace map
} // namespace tmwa