OSDN Git Service

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