OSDN Git Service

Merge pull request #2725 from Hourier/Move-Feature-to-System
[hengbandforosx/hengbandosx.git] / src / dungeon / quest.cpp
1 #include "dungeon/quest.h"
2 #include "cmd-io/cmd-dump.h"
3 #include "core/asking-player.h"
4 #include "core/player-update-types.h"
5 #include "floor/cave.h"
6 #include "floor/floor-events.h"
7 #include "floor/floor-mode-changer.h"
8 #include "floor/floor-object.h"
9 #include "game-option/play-record-options.h"
10 #include "info-reader/fixed-map-parser.h"
11 #include "io/write-diary.h"
12 #include "locale/english.h"
13 #include "main/music-definitions-table.h"
14 #include "main/sound-of-music.h"
15 #include "monster-race/monster-race-hook.h"
16 #include "monster-race/monster-race.h"
17 #include "monster-race/race-flags1.h"
18 #include "monster-race/race-flags7.h"
19 #include "monster-race/race-flags8.h"
20 #include "monster/monster-info.h"
21 #include "monster/monster-list.h"
22 #include "monster/monster-util.h"
23 #include "monster/smart-learn-types.h"
24 #include "object-enchant/item-apply-magic.h"
25 #include "object-enchant/trg-types.h"
26 #include "player-status/player-energy.h"
27 #include "player/player-personality-types.h"
28 #include "player/player-status.h"
29 #include "system/artifact-type-definition.h"
30 #include "system/dungeon-info.h"
31 #include "system/floor-type-definition.h"
32 #include "system/grid-type-definition.h"
33 #include "system/monster-race-definition.h"
34 #include "system/player-type-definition.h"
35 #include "system/terrain-type-definition.h"
36 #include "util/bit-flags-calculator.h"
37 #include "view/display-messages.h"
38 #include "world/world.h"
39 #include <sstream>
40 #include <stdexcept>
41
42 char quest_text[10][80]; /*!< Quest text */
43 int quest_text_line; /*!< Current line of the quest text */
44 QuestId leaving_quest = QuestId::NONE;
45
46 /*!
47  * @brief クエスト突入時のメッセージテーブル / Array of places to find an inscription
48  */
49 static concptr find_quest_map[] = {
50     _("床にメッセージが刻まれている:", "You find the following inscription in the floor"),
51     _("壁にメッセージが刻まれている:", "You see a message inscribed in the wall"),
52     _("メッセージを見つけた:", "There is a sign saying"),
53     _("何かが階段の上に書いてある:", "Something is written on the staircase"),
54     _("巻物を見つけた。メッセージが書いてある:", "You find a scroll with the following message"),
55 };
56
57 QuestList &QuestList::get_instance()
58 {
59     static QuestList instance{};
60     return instance;
61 }
62
63 quest_type &QuestList::operator[](QuestId id)
64 {
65     return this->quest_data.at(id);
66 }
67
68 const quest_type &QuestList::operator[](QuestId id) const
69 {
70     return this->quest_data.at(id);
71 }
72
73 QuestList::iterator QuestList::begin()
74 {
75     return this->quest_data.begin();
76 }
77
78 QuestList::const_iterator QuestList::begin() const
79 {
80     return this->quest_data.cbegin();
81 }
82
83 QuestList::iterator QuestList::end()
84 {
85     return this->quest_data.end();
86 }
87
88 QuestList::const_iterator QuestList::end() const
89 {
90     return this->quest_data.cend();
91 }
92
93 QuestList::reverse_iterator QuestList::rbegin()
94 {
95     return this->quest_data.rbegin();
96 }
97
98 QuestList::const_reverse_iterator QuestList::rbegin() const
99 {
100     return this->quest_data.crbegin();
101 }
102
103 QuestList::reverse_iterator QuestList::rend()
104 {
105     return this->quest_data.rend();
106 }
107
108 QuestList::const_reverse_iterator QuestList::rend() const
109 {
110     return this->quest_data.crend();
111 }
112
113 QuestList::iterator QuestList::find(QuestId id)
114 {
115     return this->quest_data.find(id);
116 }
117
118 QuestList::const_iterator QuestList::find(QuestId id) const
119 {
120     return this->quest_data.find(id);
121 }
122
123 size_t QuestList::size() const
124 {
125     return this->quest_data.size();
126 }
127
128 /*!
129  * @brief クエスト情報初期化のメインルーチン /
130  * Initialize quest array
131  */
132 void QuestList::initialize()
133 {
134     if (initialized) {
135         return;
136     }
137     try {
138         auto quest_numbers = parse_quest_info(QUEST_DEFINITION_LIST);
139         quest_type init_quest{};
140         init_quest.status = QuestStatusType::UNTAKEN;
141         this->quest_data.insert({ QuestId::NONE, init_quest });
142         for (auto q : quest_numbers) {
143             this->quest_data.insert({ q, init_quest });
144         }
145         initialized = true;
146     } catch (const std::runtime_error &r) {
147         std::stringstream ss;
148         ss << _("ファイル読み込みエラー: ", "File loading error: ") << r.what();
149
150         msg_print(ss.str());
151         msg_print(nullptr);
152         quit(_("クエスト初期化エラー", "Error of quests initializing"));
153     }
154 }
155
156 /*!
157  * @brief 該当IDが固定クエストかどうかを判定する.
158  * @param quest_idx クエストID
159  * @return 固定クエストならばTRUEを返す
160  */
161 bool quest_type::is_fixed(QuestId quest_idx)
162 {
163     return (enum2i(quest_idx) < MIN_RANDOM_QUEST) || (enum2i(quest_idx) > MAX_RANDOM_QUEST);
164 }
165
166 /*!
167  * @brief ランダムクエストの討伐ユニークを決める / Determine the random quest uniques
168  * @param q_ptr クエスト構造体の参照ポインタ
169  */
170 void determine_random_questor(PlayerType *player_ptr, quest_type *q_ptr)
171 {
172     get_mon_num_prep(player_ptr, mon_hook_quest, nullptr);
173     MonsterRaceId r_idx;
174     while (true) {
175         /*
176          * Random monster 5 - 10 levels out of depth
177          * (depending on level)
178          */
179         r_idx = get_mon_num(player_ptr, 0, q_ptr->level + 5 + randint1(q_ptr->level / 10), GMN_ARENA);
180         monster_race *r_ptr;
181         r_ptr = &monraces_info[r_idx];
182
183         if (r_ptr->kind_flags.has_not(MonsterKindType::UNIQUE)) {
184             continue;
185         }
186         if (r_ptr->flags8 & RF8_NO_QUEST) {
187             continue;
188         }
189         if (r_ptr->flags1 & RF1_QUESTOR) {
190             continue;
191         }
192         if (r_ptr->rarity > 100) {
193             continue;
194         }
195         if (r_ptr->behavior_flags.has(MonsterBehaviorType::FRIENDLY)) {
196             continue;
197         }
198         if (r_ptr->feature_flags.has(MonsterFeatureType::AQUATIC)) {
199             continue;
200         }
201         if (r_ptr->wilderness_flags.has(MonsterWildernessType::WILD_ONLY)) {
202             continue;
203         }
204         if (no_questor_or_bounty_uniques(r_idx)) {
205             continue;
206         }
207
208         /*
209          * Accept monsters that are 2 - 6 levels
210          * out of depth depending on the quest level
211          */
212         if (r_ptr->level > (q_ptr->level + (q_ptr->level / 20))) {
213             break;
214         }
215     }
216
217     q_ptr->r_idx = r_idx;
218 }
219
220 /*!
221  * @brief クエストの最終状態を記録する(成功or失敗、時間)
222  * @param PlayerType プレイヤー情報への参照ポインタ
223  * @param q_ptr クエスト情報への参照ポインタ
224  * @param stat ステータス(成功or失敗)
225  */
226 void record_quest_final_status(quest_type *q_ptr, PLAYER_LEVEL lev, QuestStatusType stat)
227 {
228     q_ptr->status = stat;
229     q_ptr->complev = lev;
230     update_playtime();
231     q_ptr->comptime = w_ptr->play_time;
232 }
233
234 /*!
235  * @brief クエストを達成状態にする /
236  * @param player_ptr プレイヤーへの参照ポインタ
237  * @param quest_num 達成状態にしたいクエストのID
238  */
239 void complete_quest(PlayerType *player_ptr, QuestId quest_num)
240 {
241     auto &quest_list = QuestList::get_instance();
242     auto *const q_ptr = &quest_list[quest_num];
243
244     switch (q_ptr->type) {
245     case QuestKindType::RANDOM:
246         if (record_rand_quest) {
247             exe_write_diary_quest(player_ptr, DIARY_RAND_QUEST_C, quest_num);
248         }
249         break;
250     default:
251         if (record_fix_quest) {
252             exe_write_diary_quest(player_ptr, DIARY_FIX_QUEST_C, quest_num);
253         }
254         break;
255     }
256
257     record_quest_final_status(q_ptr, player_ptr->lev, QuestStatusType::COMPLETED);
258
259     if (q_ptr->flags & QUEST_FLAG_SILENT) {
260         return;
261     }
262
263     play_music(TERM_XTRA_MUSIC_BASIC, MUSIC_BASIC_QUEST_CLEAR);
264     msg_print(_("クエストを達成した!", "You just completed your quest!"));
265     msg_print(nullptr);
266 }
267
268 /*!
269  * @brief 特定のアーティファクトを入手した際のクエスト達成処理 /
270  * Check for "Quest" completion when a quest monster is killed or charmed.
271  * @param player_ptr プレイヤーへの参照ポインタ
272  * @param o_ptr 入手したオブジェクトの構造体参照ポインタ
273  */
274 void check_find_art_quest_completion(PlayerType *player_ptr, ObjectType *o_ptr)
275 {
276     const auto &quest_list = QuestList::get_instance();
277     /* Check if completed a quest */
278     for (const auto &[q_idx, q_ref] : quest_list) {
279         auto found_artifact = (q_ref.type == QuestKindType::FIND_ARTIFACT);
280         found_artifact &= (q_ref.status == QuestStatusType::TAKEN);
281         found_artifact &= (q_ref.reward_artifact_idx == o_ptr->fixed_artifact_idx);
282         if (found_artifact) {
283             complete_quest(player_ptr, q_idx);
284         }
285     }
286 }
287
288 /*!
289  * @brief クエストの導入メッセージを表示する / Discover quest
290  * @param q_idx 開始されたクエストのID
291  */
292 void quest_discovery(QuestId q_idx)
293 {
294     auto &quest_list = QuestList::get_instance();
295     auto *q_ptr = &quest_list[q_idx];
296     auto *r_ptr = &monraces_info[q_ptr->r_idx];
297     MONSTER_NUMBER q_num = q_ptr->max_num;
298
299     if (!inside_quest(q_idx)) {
300         return;
301     }
302
303     GAME_TEXT name[MAX_NLEN];
304     strcpy(name, (r_ptr->name.c_str()));
305
306     msg_print(find_quest_map[rand_range(0, 4)]);
307     msg_print(nullptr);
308
309     if (q_num != 1) {
310 #ifdef JP
311 #else
312         plural_aux(name);
313 #endif
314         msg_format(_("注意しろ!この階は%d体の%sによって守られている!", "Be warned, this level is guarded by %d %s!"), q_num, name);
315         return;
316     }
317
318     bool is_random_quest_skipped = r_ptr->kind_flags.has(MonsterKindType::UNIQUE);
319     is_random_quest_skipped &= r_ptr->max_num == 0;
320     if (!is_random_quest_skipped) {
321         msg_format(_("注意せよ!この階は%sによって守られている!", "Beware, this level is protected by %s!"), name);
322         return;
323     }
324
325     msg_print(_("この階は以前は誰かによって守られていたようだ…。", "It seems that this level was protected by someone before..."));
326     record_quest_final_status(q_ptr, 0, QuestStatusType::FINISHED);
327 }
328
329 /*!
330  * @brief 新しく入ったダンジョンの階層に固定されている一般のクエストを探し出しIDを返す。
331  * / Hack -- Check if a level is a "quest" level
332  * @param player_ptr プレイヤーへの参照ポインタ
333  * @param level 検索対象になる階
334  * @return クエストIDを返す。該当がない場合0を返す。
335  */
336 QuestId quest_number(PlayerType *player_ptr, DEPTH level)
337 {
338     auto *floor_ptr = player_ptr->current_floor_ptr;
339     const auto &quest_list = QuestList::get_instance();
340     if (inside_quest(floor_ptr->quest_number)) {
341         return floor_ptr->quest_number;
342     }
343
344     for (const auto &[q_idx, q_ref] : quest_list) {
345         if (q_ref.status != QuestStatusType::TAKEN) {
346             continue;
347         }
348         auto depth_quest = (q_ref.type == QuestKindType::KILL_LEVEL);
349         depth_quest &= !(q_ref.flags & QUEST_FLAG_PRESET);
350         depth_quest &= (q_ref.level == level);
351         depth_quest &= (q_ref.dungeon == player_ptr->dungeon_idx);
352         if (depth_quest) {
353             return q_idx;
354         }
355     }
356
357     return random_quest_number(player_ptr, level);
358 }
359
360 /*!
361  * @brief 新しく入ったダンジョンの階層に固定されているランダムクエストを探し出しIDを返す。
362  * @param player_ptr プレイヤーへの参照ポインタ
363  * @param level 検索対象になる階
364  * @return クエストIDを返す。該当がない場合0を返す。
365  */
366 QuestId random_quest_number(PlayerType *player_ptr, DEPTH level)
367 {
368     if (player_ptr->dungeon_idx != DUNGEON_ANGBAND) {
369         return QuestId::NONE;
370     }
371
372     const auto &quest_list = QuestList::get_instance();
373     for (auto q_idx : EnumRange(QuestId::RANDOM_QUEST1, QuestId::RANDOM_QUEST10)) {
374         const auto &q_ref = quest_list[q_idx];
375         auto is_random_quest = (q_ref.type == QuestKindType::RANDOM);
376         is_random_quest &= (q_ref.status == QuestStatusType::TAKEN);
377         is_random_quest &= (q_ref.level == level);
378         is_random_quest &= (q_ref.dungeon == DUNGEON_ANGBAND);
379         if (is_random_quest) {
380             return q_idx;
381         }
382     }
383
384     return QuestId::NONE;
385 }
386
387 /*!
388  * @brief クエスト階層から離脱する際の処理
389  * @param player_ptr プレイヤーへの参照ポインタ
390  */
391 void leave_quest_check(PlayerType *player_ptr)
392 {
393     leaving_quest = player_ptr->current_floor_ptr->quest_number;
394     if (!inside_quest(leaving_quest)) {
395         return;
396     }
397
398     auto &quest_list = QuestList::get_instance();
399     auto *q_ptr = &quest_list[leaving_quest];
400     bool is_one_time_quest = ((q_ptr->flags & QUEST_FLAG_ONCE) || (q_ptr->type == QuestKindType::RANDOM)) && (q_ptr->status == QuestStatusType::TAKEN);
401     if (!is_one_time_quest) {
402         return;
403     }
404
405     record_quest_final_status(q_ptr, player_ptr->lev, QuestStatusType::FAILED);
406
407     /* Additional settings */
408     switch (q_ptr->type) {
409     case QuestKindType::TOWER:
410         quest_list[QuestId::TOWER1].status = QuestStatusType::FAILED;
411         quest_list[QuestId::TOWER1].complev = player_ptr->lev;
412         break;
413     case QuestKindType::FIND_ARTIFACT:
414         artifacts_info.at(q_ptr->reward_artifact_idx).gen_flags.reset(ItemGenerationTraitType::QUESTITEM);
415         break;
416     case QuestKindType::RANDOM:
417         monraces_info[q_ptr->r_idx].flags1 &= ~(RF1_QUESTOR);
418         prepare_change_floor_mode(player_ptr, CFM_NO_RETURN);
419         break;
420     default:
421         break;
422     }
423
424     /* Record finishing a quest */
425     if (q_ptr->type == QuestKindType::RANDOM) {
426         if (record_rand_quest) {
427             exe_write_diary_quest(player_ptr, DIARY_RAND_QUEST_F, leaving_quest);
428         }
429         return;
430     }
431
432     if (record_fix_quest) {
433         exe_write_diary_quest(player_ptr, DIARY_FIX_QUEST_F, leaving_quest);
434     }
435 }
436
437 /*!
438  * @brief 「塔」クエストの各階層から離脱する際の処理
439  */
440 void leave_tower_check(PlayerType *player_ptr)
441 {
442     auto &quest_list = QuestList::get_instance();
443     leaving_quest = player_ptr->current_floor_ptr->quest_number;
444
445     auto &tower1 = quest_list[QuestId::TOWER1];
446     bool is_leaving_from_tower = inside_quest(leaving_quest);
447     is_leaving_from_tower &= quest_list[leaving_quest].type == QuestKindType::TOWER;
448     is_leaving_from_tower &= tower1.status != QuestStatusType::COMPLETED;
449     if (!is_leaving_from_tower) {
450         return;
451     }
452     if (quest_list[leaving_quest].type != QuestKindType::TOWER) {
453         return;
454     }
455     tower1.status = QuestStatusType::FAILED;
456     tower1.complev = player_ptr->lev;
457     update_playtime();
458     tower1.comptime = w_ptr->play_time;
459 }
460
461 /*!
462  * @brief Player enters a new quest
463  */
464 void exe_enter_quest(PlayerType *player_ptr, QuestId quest_idx)
465 {
466     const auto &quest_list = QuestList::get_instance();
467     if (quest_list[quest_idx].type != QuestKindType::RANDOM) {
468         player_ptr->current_floor_ptr->dun_level = 1;
469     }
470     player_ptr->current_floor_ptr->quest_number = quest_idx;
471
472     player_ptr->leaving = true;
473 }
474
475 /*!
476  * @brief クエスト入り口にプレイヤーが乗った際の処理 / Do building commands
477  * @param player_ptr プレイヤーへの参照ポインタ
478  */
479 void do_cmd_quest(PlayerType *player_ptr)
480 {
481     if (player_ptr->wild_mode) {
482         return;
483     }
484
485     PlayerEnergy(player_ptr).set_player_turn_energy(100);
486
487     if (!cave_has_flag_bold(player_ptr->current_floor_ptr, player_ptr->y, player_ptr->x, TerrainCharacteristics::QUEST_ENTER)) {
488         msg_print(_("ここにはクエストの入口はない。", "You see no quest level here."));
489         return;
490     }
491
492     msg_print(_("ここにはクエストへの入口があります。", "There is an entry of a quest."));
493     if (!get_check(_("クエストに入りますか?", "Do you enter? "))) {
494         return;
495     }
496     if (is_echizen(player_ptr)) {
497         msg_print(_("『とにかく入ってみようぜぇ。』", "\"Let's go in anyway.\""));
498     } else if (is_chargeman(player_ptr)) {
499         msg_print(_("『全滅してやるぞ!』", "\"I'll annihilate THEM!\""));
500     }
501
502     player_ptr->oldpy = 0;
503     player_ptr->oldpx = 0;
504     leave_quest_check(player_ptr);
505
506     exe_enter_quest(player_ptr, i2enum<QuestId>(player_ptr->current_floor_ptr->grid_array[player_ptr->y][player_ptr->x].special));
507 }
508
509 bool inside_quest(QuestId id)
510 {
511     return id != QuestId::NONE;
512 }