OSDN Git Service

Merge pull request #3569 from sikabane-works/release/3.0.0.88-alpha
[hengbandforosx/hengbandosx.git] / src / store / rumor.cpp
1 #include "store/rumor.h"
2 #include "flavor/flavor-describer.h"
3 #include "flavor/object-flavor-types.h"
4 #include "floor/floor-town.h"
5 #include "io/files-util.h"
6 #include "io/tokenizer.h"
7 #include "monster-race/monster-race.h"
8 #include "object-enchant/special-object-flags.h"
9 #include "object/object-kind-hook.h"
10 #include "system/angband-exceptions.h"
11 #include "system/artifact-type-definition.h"
12 #include "system/baseitem-info.h"
13 #include "system/dungeon-info.h"
14 #include "system/item-entity.h"
15 #include "system/monster-race-info.h"
16 #include "system/player-type-definition.h"
17 #include "view/display-messages.h"
18 #include "world/world.h"
19 #include <algorithm>
20 #include <sstream>
21 #include <stdexcept>
22 #include <string>
23 #include <string_view>
24 #include <utility>
25
26 /*
27  * @brief 固定アーティファクト、モンスター、町 をランダムに1つ選び、ダンジョンを固定的に1つ選ぶ
28  * @param zz 検索文字列
29  * @param max_idx briefに挙げた各リストにおける最大数
30  * @details rumor.txt (rumor_j.txt) の定義により、ダンジョンは鉄獄 (ダンジョンID1)が常に選ばれる
31  * その他は常にランダム ("*")
32  */
33 static short get_rumor_num(std::string_view zz, short max_idx)
34 {
35     if (zz == "*") {
36         return randint1(max_idx);
37     }
38
39     return static_cast<short>(atoi(zz.data()));
40 }
41
42 static std::string bind_rumor_name(std::string_view base, std::string_view item_name)
43 {
44     if (const auto pos = base.find("{Name}");
45         pos != std::string::npos) {
46         const auto head = base.substr(0, pos);
47         const auto tail = base.substr(pos + 6);
48         std::stringstream ss;
49         ss << head << item_name << tail;
50         return ss.str();
51     }
52
53     return std::string(base);
54 }
55
56 /*
57  * @brief 噂の、町やモンスターを表すトークンを得る
58  * @param rumor rumor.txt (rumor_j.txt)の1行
59  * @return トークン読み込み成否 とトークン群の配列
60  * @todo tmp_tokensを使わず単なるsplitにすればもっと簡略化できそう
61  */
62 std::pair<bool, std::vector<std::string>> get_rumor_tokens(std::string rumor)
63 {
64     constexpr auto num_tokens = 3;
65     char *tmp_tokens[num_tokens];
66     if (tokenize(rumor.data() + 2, num_tokens, tmp_tokens, TOKENIZE_CHECKQUOTE) != num_tokens) {
67         msg_print(_("この情報は間違っている。", "This information is wrong."));
68         return { false, {} };
69     }
70
71     std::vector<std::string> tokens(std::begin(tmp_tokens), std::end(tmp_tokens));
72     return { true, tokens };
73 }
74
75 /*!
76  * @brief 固定アーティファクト番号とその定義を、ランダムに抽選する
77  * @param artifact_name rumor.txt (rumor_j.txt)の定義により、常に"*" (ランダム)
78  * @details 固定アーティファクト番号は欠番があるので、もし欠番だったら再抽選する
79  */
80 static std::pair<FixedArtifactId, const ArtifactType *> get_artifact_definition(std::string_view artifact_name)
81 {
82     const auto max_idx = enum2i(artifacts_info.rbegin()->first);
83     while (true) {
84         const auto a_idx = i2enum<FixedArtifactId>(get_rumor_num(artifact_name.data(), max_idx));
85         const auto &artifact = ArtifactsInfo::get_instance().get_artifact(a_idx);
86         if (!artifact.name.empty()) {
87             return { a_idx, &artifact };
88         }
89     }
90 }
91
92 void display_rumor(PlayerType *player_ptr, bool ex)
93 {
94     int section = (ex && (randint0(3) == 0)) ? 1 : 0;
95 #ifdef JP
96     auto opt_rumor = get_random_line_ja_only("rumors_j.txt", section, 10);
97 #else
98     auto opt_rumor = get_random_line("rumors.txt", section);
99 #endif
100     std::string rumor;
101     if (opt_rumor.has_value()) {
102         rumor = std::move(opt_rumor.value());
103     } else {
104         rumor = _("嘘の噂もある。", "Some rumors are wrong.");
105     }
106
107     if (!rumor.starts_with("R:")) {
108         msg_print(rumor);
109         return;
110     }
111
112     const auto &[is_correct, tokens] = get_rumor_tokens(rumor);
113     if (!is_correct) {
114         return;
115     }
116
117     concptr rumor_eff_format = nullptr;
118     std::string fullname;
119     const auto &category = tokens[0];
120     if (category == "ARTIFACT") {
121         const auto &artifact_name = tokens[1];
122         const auto &[a_idx, a_ptr] = get_artifact_definition(artifact_name);
123         const auto bi_id = lookup_baseitem_id(a_ptr->bi_key);
124         ItemEntity item;
125         item.prep(bi_id);
126         item.fixed_artifact_idx = a_idx;
127         item.ident = IDENT_STORE;
128         fullname = describe_flavor(player_ptr, &item, OD_NAME_ONLY);
129     } else if (category == "MONSTER") {
130         const auto &monster_name = tokens[1];
131
132         // @details プレイヤーもダミーで入っているので、1つ引いておかないと数が合わなくなる.
133         const auto monraces_size = static_cast<short>(monraces_info.size() - 1);
134         auto monrace_id = i2enum<MonsterRaceId>(get_rumor_num(monster_name, monraces_size));
135         auto *r_ptr = &monraces_info[monrace_id];
136         fullname = r_ptr->name;
137         if (!r_ptr->r_sights) {
138             r_ptr->r_sights++;
139         }
140     } else if (category == "DUNGEON") {
141         DUNGEON_IDX d_idx;
142         dungeon_type *d_ptr;
143         const auto dungeons_size = static_cast<short>(dungeons_info.size());
144         const auto &d_idx_str = tokens[1];
145         while (true) {
146             d_idx = get_rumor_num(d_idx_str, dungeons_size);
147             d_ptr = &dungeons_info[d_idx];
148             if (!d_ptr->name.empty()) {
149                 break;
150             }
151         }
152
153         fullname = d_ptr->name;
154         if (!max_dlv[d_idx]) {
155             max_dlv[d_idx] = d_ptr->mindepth;
156             rumor_eff_format = _("%sに帰還できるようになった。", "You can recall to %s.");
157         }
158     } else if (category == "TOWN") {
159         IDX t_idx;
160         const auto &town_name = tokens[1];
161         while (true) {
162             t_idx = get_rumor_num(town_name, VALID_TOWNS);
163             if (!towns_info[t_idx].name.empty()) {
164                 break;
165             }
166         }
167
168         fullname = towns_info[t_idx].name;
169         int32_t visit = (1UL << (t_idx - 1));
170         if ((t_idx != SECRET_TOWN) && !(player_ptr->visit & visit)) {
171             player_ptr->visit |= visit;
172             rumor_eff_format = _("%sに行ったことがある気がする。", "You feel you have been to %s.");
173         }
174     } else {
175         THROW_EXCEPTION(std::runtime_error, "Unknown token exists in rumor.txt");
176     }
177
178     const auto rumor_msg = bind_rumor_name(tokens[2], fullname);
179     msg_print(rumor_msg);
180     if (rumor_eff_format) {
181         msg_print(nullptr);
182         msg_format(rumor_eff_format, fullname.data());
183     }
184 }