OSDN Git Service

Merge pull request #1767 from Hourier/Change-Tval-Type-To-Enum-Class
[hengbandforosx/hengbandosx.git] / src / info-reader / artifact-reader.cpp
1 #include "info-reader/artifact-reader.h"
2 #include "artifact/random-art-effects.h"
3 #include "info-reader/info-reader-util.h"
4 #include "info-reader/kind-info-tokens-table.h"
5 #include "info-reader/parse-error-types.h"
6 #include "main/angband-headers.h"
7 #include "object-enchant/tr-types.h"
8 #include "system/artifact-type-definition.h"
9 #include "util/bit-flags-calculator.h"
10 #include "util/enum-converter.h"
11 #include "util/string-processor.h"
12 #include "view/display-messages.h"
13
14 /*!
15  * @brief テキストトークンを走査してフラグを一つ得る(アーティファクト用) /
16  * Grab one activation index flag
17  * @param a_ptr 保管先のアーティファクト構造体参照ポインタ
18  * @param what 参照元の文字列ポインタ
19  * @return 見つかったらtrue
20  */
21 static bool grab_one_artifact_flag(artifact_type *a_ptr, std::string_view what)
22 {
23     if (TrFlags::grab_one_flag(a_ptr->flags, k_info_flags, what))
24         return true;
25
26     if (EnumClassFlagGroup<TRG>::grab_one_flag(a_ptr->gen_flags, k_info_gen_flags, what))
27         return true;
28
29     msg_format(_("未知の伝説のアイテム・フラグ '%s'。", "Unknown artifact flag '%s'."), what.data());
30     return false;
31 }
32
33 /*!
34  * @brief 固定アーティファクト情報(a_info)のパース関数 /
35  * Initialize the "a_info" array, by parsing an ascii "template" file
36  * @param buf テキスト列
37  * @param head ヘッダ構造体
38  * @return エラーコード
39  */
40 errr parse_a_info(std::string_view buf, angband_header *)
41 {
42     static artifact_type *a_ptr = nullptr;
43     const auto &tokens = str_split(buf, ':', false, 10);
44
45     if (tokens[0] == "N") {
46         // N:index:name_ja
47         if (tokens.size() < 3 || tokens[1].size() == 0)
48             return PARSE_ERROR_GENERIC;
49
50         auto i = std::stoi(tokens[1]);
51         if (i < error_idx)
52             return PARSE_ERROR_NON_SEQUENTIAL_RECORDS;
53         if (i >= static_cast<int>(a_info.size())) {
54             a_info.resize(i + 1);
55         }
56
57         error_idx = i;
58         a_ptr = &a_info[i];
59         a_ptr->idx = static_cast<ARTIFACT_IDX>(i);
60         a_ptr->flags.set(TR_IGNORE_ACID);
61         a_ptr->flags.set(TR_IGNORE_ELEC);
62         a_ptr->flags.set(TR_IGNORE_FIRE);
63         a_ptr->flags.set(TR_IGNORE_COLD);
64
65 #ifdef JP
66         a_ptr->name = tokens[2];
67 #endif
68     } else if (!a_ptr)
69         return PARSE_ERROR_MISSING_RECORD_HEADER;
70     else if (tokens[0] == "E") {
71         // E:name_en
72 #ifndef JP
73         if (tokens[1].size() == 0)
74             return PARSE_ERROR_TOO_FEW_ARGUMENTS;
75         a_ptr->name = tokens[1];
76 #endif
77     } else if (tokens[0] == "D") {
78         // D:JapaneseText
79         // D:$EnglishText
80         if (tokens.size() < 2 || tokens[1].size() == 0)
81             return PARSE_ERROR_NON_SEQUENTIAL_RECORDS;
82 #ifdef JP
83         if (tokens[1][0] == '$')
84             return PARSE_ERROR_NONE;
85         a_ptr->text.append(buf.substr(2));
86 #else
87         if (tokens[1][0] != '$')
88             return PARSE_ERROR_NONE;
89         append_english_text(a_ptr->text, buf.substr(3));
90 #endif
91     } else if (tokens[0] == "I") {
92         // I:tval:sval:pval
93         if (tokens.size() < 4)
94             return PARSE_ERROR_TOO_FEW_ARGUMENTS;
95
96         info_set_value(a_ptr->tval, tokens[1]);
97         info_set_value(a_ptr->sval, tokens[2]);
98         info_set_value(a_ptr->pval, tokens[3]);
99     } else if (tokens[0] == "W") {
100         // W:level:ratiry:weight:cost
101         if (tokens.size() < 5)
102             return PARSE_ERROR_TOO_FEW_ARGUMENTS;
103
104         info_set_value(a_ptr->level, tokens[1]);
105         info_set_value(a_ptr->rarity, tokens[2]);
106         info_set_value(a_ptr->weight, tokens[3]);
107         info_set_value(a_ptr->cost, tokens[4]);
108     } else if (tokens[0] == "P") {
109         // P:ac:dd:ds:to_h:to_d:to_a
110         if (tokens.size() < 6)
111             return PARSE_ERROR_TOO_FEW_ARGUMENTS;
112
113         const auto &dice = str_split(tokens[2], 'd', false, 2);
114         if (dice.size() != 2)
115             return PARSE_ERROR_NON_SEQUENTIAL_RECORDS;
116
117         info_set_value(a_ptr->ac, tokens[1]);
118         info_set_value(a_ptr->dd, dice[0]);
119         info_set_value(a_ptr->ds, dice[1]);
120         info_set_value(a_ptr->to_h, tokens[3]);
121         info_set_value(a_ptr->to_d, tokens[4]);
122         info_set_value(a_ptr->to_a, tokens[5]);
123     } else if (tokens[0] == "U") {
124         // U:activation_flag
125         if (tokens.size() < 2 || tokens[1].size() == 0)
126             return PARSE_ERROR_TOO_FEW_ARGUMENTS;
127         auto n = grab_one_activation_flag(tokens[1].c_str());
128         if (n <= RandomArtActType::NONE)
129             return PARSE_ERROR_INVALID_FLAG;
130
131         a_ptr->act_idx = n;
132     } else if (tokens[0] == "F") {
133         // F:flags
134         if (tokens.size() < 2 || tokens[1].size() == 0)
135             return PARSE_ERROR_TOO_FEW_ARGUMENTS;
136
137         const auto &flags = str_split(tokens[1], '|', true, 10);
138         for (const auto &f : flags) {
139             if (f.size() == 0)
140                 continue;
141             if (!grab_one_artifact_flag(a_ptr, f))
142                 return PARSE_ERROR_INVALID_FLAG;
143         }
144     } else
145         return PARSE_ERROR_UNDEFINED_DIRECTIVE;
146
147     return PARSE_ERROR_NONE;
148 }