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"
23 #include <string_view>
27 * @brief 固定アーティファクト、モンスター、町 をランダムに1つ選び、ダンジョンを固定的に1つ選ぶ
29 * @param max_idx briefに挙げた各リストにおける最大数
30 * @details rumor.txt (rumor_j.txt) の定義により、ダンジョンは鉄獄 (ダンジョンID1)が常に選ばれる
33 static short get_rumor_num(std::string_view zz, short max_idx)
36 return randint1(max_idx);
39 return static_cast<short>(atoi(zz.data()));
42 static std::string bind_rumor_name(std::string_view base, std::string_view item_name)
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);
49 ss << head << item_name << tail;
53 return std::string(base);
57 * @brief 噂の、町やモンスターを表すトークンを得る
58 * @param rumor rumor.txt (rumor_j.txt)の1行
59 * @return トークン読み込み成否 とトークン群の配列
60 * @todo tmp_tokensを使わず単なるsplitにすればもっと簡略化できそう
62 std::pair<bool, std::vector<std::string>> get_rumor_tokens(std::string rumor)
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."));
71 std::vector<std::string> tokens(std::begin(tmp_tokens), std::end(tmp_tokens));
72 return { true, tokens };
76 * @brief 固定アーティファクト番号とその定義を、ランダムに抽選する
77 * @param artifact_name rumor.txt (rumor_j.txt)の定義により、常に"*" (ランダム)
78 * @details 固定アーティファクト番号は欠番があるので、もし欠番だったら再抽選する
80 static std::pair<FixedArtifactId, const ArtifactType *> get_artifact_definition(std::string_view artifact_name)
82 const auto max_idx = enum2i(artifacts_info.rbegin()->first);
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 };
92 void display_rumor(PlayerType *player_ptr, bool ex)
94 int section = (ex && (randint0(3) == 0)) ? 1 : 0;
96 auto opt_rumor = get_random_line_ja_only("rumors_j.txt", section, 10);
98 auto opt_rumor = get_random_line("rumors.txt", section);
101 if (opt_rumor.has_value()) {
102 rumor = std::move(opt_rumor.value());
104 rumor = _("嘘の噂もある。", "Some rumors are wrong.");
107 if (!rumor.starts_with("R:")) {
112 const auto &[is_correct, tokens] = get_rumor_tokens(rumor);
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);
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];
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) {
140 } else if (category == "DUNGEON") {
143 const auto dungeons_size = static_cast<short>(dungeons_info.size());
144 const auto &d_idx_str = tokens[1];
146 d_idx = get_rumor_num(d_idx_str, dungeons_size);
147 d_ptr = &dungeons_info[d_idx];
148 if (!d_ptr->name.empty()) {
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.");
158 } else if (category == "TOWN") {
160 const auto &town_name = tokens[1];
162 t_idx = get_rumor_num(town_name, VALID_TOWNS);
163 if (!towns_info[t_idx].name.empty()) {
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.");
175 THROW_EXCEPTION(std::runtime_error, "Unknown token exists in rumor.txt");
178 const auto rumor_msg = bind_rumor_name(tokens[2], fullname);
179 msg_print(rumor_msg);
180 if (rumor_eff_format) {
182 msg_format(rumor_eff_format, fullname.data());