OSDN Git Service

[Refactor] #3215 QuestType::get_reward() を実装した
[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 QuestType &QuestList::operator[](QuestId id)
65 {
66     return this->quest_data.at(id);
67 }
68
69 const QuestType &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         QuestType 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 QuestType::is_fixed(QuestId quest_idx)
163 {
164     return (enum2i(quest_idx) < MIN_RANDOM_QUEST) || (enum2i(quest_idx) > MAX_RANDOM_QUEST);
165 }
166
167 ArtifactType &QuestType::get_reward() const
168 {
169     const auto &artifacts = ArtifactsInfo::get_instance();
170     return artifacts.get_artifact(this->reward_artifact_idx);
171 }
172
173 /*!
174  * @brief ランダムクエストの討伐ユニークを決める / Determine the random quest uniques
175  * @param q_ptr クエスト構造体の参照ポインタ
176  */
177 void determine_random_questor(PlayerType *player_ptr, QuestType *q_ptr)
178 {
179     get_mon_num_prep(player_ptr, mon_hook_quest, nullptr);
180     MonsterRaceId r_idx;
181     while (true) {
182         /*
183          * Random monster 5 - 10 levels out of depth
184          * (depending on level)
185          */
186         r_idx = get_mon_num(player_ptr, 0, q_ptr->level + 5 + randint1(q_ptr->level / 10), GMN_ARENA);
187         MonsterRaceInfo *r_ptr;
188         r_ptr = &monraces_info[r_idx];
189
190         if (r_ptr->kind_flags.has_not(MonsterKindType::UNIQUE)) {
191             continue;
192         }
193         if (r_ptr->flags8 & RF8_NO_QUEST) {
194             continue;
195         }
196         if (r_ptr->flags1 & RF1_QUESTOR) {
197             continue;
198         }
199         if (r_ptr->rarity > 100) {
200             continue;
201         }
202         if (r_ptr->behavior_flags.has(MonsterBehaviorType::FRIENDLY)) {
203             continue;
204         }
205         if (r_ptr->feature_flags.has(MonsterFeatureType::AQUATIC)) {
206             continue;
207         }
208         if (r_ptr->wilderness_flags.has(MonsterWildernessType::WILD_ONLY)) {
209             continue;
210         }
211         if (no_questor_or_bounty_uniques(r_idx)) {
212             continue;
213         }
214
215         /*
216          * Accept monsters that are 2 - 6 levels
217          * out of depth depending on the quest level
218          */
219         if (r_ptr->level > (q_ptr->level + (q_ptr->level / 20))) {
220             break;
221         }
222     }
223
224     q_ptr->r_idx = r_idx;
225 }
226
227 /*!
228  * @brief クエストの最終状態を記録する(成功or失敗、時間)
229  * @param PlayerType プレイヤー情報への参照ポインタ
230  * @param q_ptr クエスト情報への参照ポインタ
231  * @param stat ステータス(成功or失敗)
232  */
233 void record_quest_final_status(QuestType *q_ptr, PLAYER_LEVEL lev, QuestStatusType stat)
234 {
235     q_ptr->status = stat;
236     q_ptr->complev = lev;
237     update_playtime();
238     q_ptr->comptime = w_ptr->play_time;
239 }
240
241 /*!
242  * @brief クエストを達成状態にする /
243  * @param player_ptr プレイヤーへの参照ポインタ
244  * @param quest_num 達成状態にしたいクエストのID
245  */
246 void complete_quest(PlayerType *player_ptr, QuestId quest_num)
247 {
248     auto &quest_list = QuestList::get_instance();
249     auto *const q_ptr = &quest_list[quest_num];
250
251     switch (q_ptr->type) {
252     case QuestKindType::RANDOM:
253         if (record_rand_quest) {
254             exe_write_diary_quest(player_ptr, DIARY_RAND_QUEST_C, quest_num);
255         }
256         break;
257     default:
258         if (record_fix_quest) {
259             exe_write_diary_quest(player_ptr, DIARY_FIX_QUEST_C, quest_num);
260         }
261         break;
262     }
263
264     record_quest_final_status(q_ptr, player_ptr->lev, QuestStatusType::COMPLETED);
265
266     if (q_ptr->flags & QUEST_FLAG_SILENT) {
267         return;
268     }
269
270     play_music(TERM_XTRA_MUSIC_BASIC, MUSIC_BASIC_QUEST_CLEAR);
271     msg_print(_("クエストを達成した!", "You just completed your quest!"));
272     msg_print(nullptr);
273 }
274
275 /*!
276  * @brief 特定のアーティファクトを入手した際のクエスト達成処理 /
277  * Check for "Quest" completion when a quest monster is killed or charmed.
278  * @param player_ptr プレイヤーへの参照ポインタ
279  * @param o_ptr 入手したオブジェクトの構造体参照ポインタ
280  */
281 void check_find_art_quest_completion(PlayerType *player_ptr, ItemEntity *o_ptr)
282 {
283     const auto &quest_list = QuestList::get_instance();
284     /* Check if completed a quest */
285     for (const auto &[q_idx, q_ref] : quest_list) {
286         auto found_artifact = (q_ref.type == QuestKindType::FIND_ARTIFACT);
287         found_artifact &= (q_ref.status == QuestStatusType::TAKEN);
288         found_artifact &= (o_ptr->is_specific_artifact(q_ref.reward_artifact_idx));
289         if (found_artifact) {
290             complete_quest(player_ptr, q_idx);
291         }
292     }
293 }
294
295 /*!
296  * @brief クエストの導入メッセージを表示する / Discover quest
297  * @param q_idx 開始されたクエストのID
298  */
299 void quest_discovery(QuestId q_idx)
300 {
301     auto &quest_list = QuestList::get_instance();
302     auto *q_ptr = &quest_list[q_idx];
303     auto *r_ptr = &monraces_info[q_ptr->r_idx];
304     MONSTER_NUMBER q_num = q_ptr->max_num;
305
306     if (!inside_quest(q_idx)) {
307         return;
308     }
309
310     GAME_TEXT name[MAX_NLEN];
311     strcpy(name, (r_ptr->name.data()));
312
313     msg_print(find_quest_map[rand_range(0, 4)]);
314     msg_print(nullptr);
315
316     if (q_num != 1) {
317 #ifdef JP
318 #else
319         plural_aux(name);
320 #endif
321         msg_format(_("注意しろ!この階は%d体の%sによって守られている!", "Be warned, this level is guarded by %d %s!"), q_num, name);
322         return;
323     }
324
325     bool is_random_quest_skipped = r_ptr->kind_flags.has(MonsterKindType::UNIQUE);
326     is_random_quest_skipped &= r_ptr->max_num == 0;
327     if (!is_random_quest_skipped) {
328         msg_format(_("注意せよ!この階は%sによって守られている!", "Beware, this level is protected by %s!"), name);
329         return;
330     }
331
332     msg_print(_("この階は以前は誰かによって守られていたようだ…。", "It seems that this level was protected by someone before..."));
333     record_quest_final_status(q_ptr, 0, QuestStatusType::FINISHED);
334 }
335
336 /*!
337  * @brief 新しく入ったダンジョンの階層に固定されている一般のクエストを探し出しIDを返す。
338  * / Hack -- Check if a level is a "quest" level
339  * @param player_ptr プレイヤーへの参照ポインタ
340  * @param level 検索対象になる階
341  * @return クエストIDを返す。該当がない場合0を返す。
342  */
343 QuestId quest_number(PlayerType *player_ptr, DEPTH level)
344 {
345     auto *floor_ptr = player_ptr->current_floor_ptr;
346     const auto &quest_list = QuestList::get_instance();
347     if (inside_quest(floor_ptr->quest_number)) {
348         return floor_ptr->quest_number;
349     }
350
351     for (const auto &[q_idx, q_ref] : quest_list) {
352         if (q_ref.status != QuestStatusType::TAKEN) {
353             continue;
354         }
355         auto depth_quest = (q_ref.type == QuestKindType::KILL_LEVEL);
356         depth_quest &= !(q_ref.flags & QUEST_FLAG_PRESET);
357         depth_quest &= (q_ref.level == level);
358         depth_quest &= (q_ref.dungeon == player_ptr->dungeon_idx);
359         if (depth_quest) {
360             return q_idx;
361         }
362     }
363
364     return random_quest_number(player_ptr, level);
365 }
366
367 /*!
368  * @brief 新しく入ったダンジョンの階層に固定されているランダムクエストを探し出しIDを返す。
369  * @param player_ptr プレイヤーへの参照ポインタ
370  * @param level 検索対象になる階
371  * @return クエストIDを返す。該当がない場合0を返す。
372  */
373 QuestId random_quest_number(PlayerType *player_ptr, DEPTH level)
374 {
375     if (player_ptr->dungeon_idx != DUNGEON_ANGBAND) {
376         return QuestId::NONE;
377     }
378
379     const auto &quest_list = QuestList::get_instance();
380     for (auto q_idx : EnumRange(QuestId::RANDOM_QUEST1, QuestId::RANDOM_QUEST10)) {
381         const auto &q_ref = quest_list[q_idx];
382         auto is_random_quest = (q_ref.type == QuestKindType::RANDOM);
383         is_random_quest &= (q_ref.status == QuestStatusType::TAKEN);
384         is_random_quest &= (q_ref.level == level);
385         is_random_quest &= (q_ref.dungeon == DUNGEON_ANGBAND);
386         if (is_random_quest) {
387             return q_idx;
388         }
389     }
390
391     return QuestId::NONE;
392 }
393
394 /*!
395  * @brief クエスト階層から離脱する際の処理
396  * @param player_ptr プレイヤーへの参照ポインタ
397  */
398 void leave_quest_check(PlayerType *player_ptr)
399 {
400     leaving_quest = player_ptr->current_floor_ptr->quest_number;
401     if (!inside_quest(leaving_quest)) {
402         return;
403     }
404
405     auto &quest_list = QuestList::get_instance();
406     auto *q_ptr = &quest_list[leaving_quest];
407     bool is_one_time_quest = ((q_ptr->flags & QUEST_FLAG_ONCE) || (q_ptr->type == QuestKindType::RANDOM)) && (q_ptr->status == QuestStatusType::TAKEN);
408     if (!is_one_time_quest) {
409         return;
410     }
411
412     record_quest_final_status(q_ptr, player_ptr->lev, QuestStatusType::FAILED);
413
414     /* Additional settings */
415     switch (q_ptr->type) {
416     case QuestKindType::TOWER:
417         quest_list[QuestId::TOWER1].status = QuestStatusType::FAILED;
418         quest_list[QuestId::TOWER1].complev = player_ptr->lev;
419         break;
420     case QuestKindType::FIND_ARTIFACT:
421         ArtifactsInfo::get_instance().get_artifact(q_ptr->reward_artifact_idx).gen_flags.reset(ItemGenerationTraitType::QUESTITEM);
422         break;
423     case QuestKindType::RANDOM:
424         monraces_info[q_ptr->r_idx].flags1 &= ~(RF1_QUESTOR);
425         prepare_change_floor_mode(player_ptr, CFM_NO_RETURN);
426         break;
427     default:
428         break;
429     }
430
431     /* Record finishing a quest */
432     if (q_ptr->type == QuestKindType::RANDOM) {
433         if (record_rand_quest) {
434             exe_write_diary_quest(player_ptr, DIARY_RAND_QUEST_F, leaving_quest);
435         }
436         return;
437     }
438
439     if (record_fix_quest) {
440         exe_write_diary_quest(player_ptr, DIARY_FIX_QUEST_F, leaving_quest);
441     }
442 }
443
444 /*!
445  * @brief 「塔」クエストの各階層から離脱する際の処理
446  */
447 void leave_tower_check(PlayerType *player_ptr)
448 {
449     auto &quest_list = QuestList::get_instance();
450     leaving_quest = player_ptr->current_floor_ptr->quest_number;
451
452     auto &tower1 = quest_list[QuestId::TOWER1];
453     bool is_leaving_from_tower = inside_quest(leaving_quest);
454     is_leaving_from_tower &= quest_list[leaving_quest].type == QuestKindType::TOWER;
455     is_leaving_from_tower &= tower1.status != QuestStatusType::COMPLETED;
456     if (!is_leaving_from_tower) {
457         return;
458     }
459     if (quest_list[leaving_quest].type != QuestKindType::TOWER) {
460         return;
461     }
462     tower1.status = QuestStatusType::FAILED;
463     tower1.complev = player_ptr->lev;
464     update_playtime();
465     tower1.comptime = w_ptr->play_time;
466 }
467
468 /*!
469  * @brief Player enters a new quest
470  */
471 void exe_enter_quest(PlayerType *player_ptr, QuestId quest_idx)
472 {
473     const auto &quest_list = QuestList::get_instance();
474     if (quest_list[quest_idx].type != QuestKindType::RANDOM) {
475         player_ptr->current_floor_ptr->dun_level = 1;
476     }
477     player_ptr->current_floor_ptr->quest_number = quest_idx;
478
479     player_ptr->leaving = true;
480 }
481
482 /*!
483  * @brief クエスト入り口にプレイヤーが乗った際の処理 / Do building commands
484  * @param player_ptr プレイヤーへの参照ポインタ
485  */
486 void do_cmd_quest(PlayerType *player_ptr)
487 {
488     if (player_ptr->wild_mode) {
489         return;
490     }
491
492     PlayerEnergy(player_ptr).set_player_turn_energy(100);
493
494     if (!cave_has_flag_bold(player_ptr->current_floor_ptr, player_ptr->y, player_ptr->x, TerrainCharacteristics::QUEST_ENTER)) {
495         msg_print(_("ここにはクエストの入口はない。", "You see no quest level here."));
496         return;
497     }
498
499     msg_print(_("ここにはクエストへの入口があります。", "There is an entry of a quest."));
500     if (!get_check(_("クエストに入りますか?", "Do you enter? "))) {
501         return;
502     }
503     if (is_echizen(player_ptr)) {
504         msg_print(_("『とにかく入ってみようぜぇ。』", "\"Let's go in anyway.\""));
505     } else if (is_chargeman(player_ptr)) {
506         msg_print(_("『全滅してやるぞ!』", "\"I'll annihilate THEM!\""));
507     }
508
509     player_ptr->oldpy = 0;
510     player_ptr->oldpx = 0;
511     leave_quest_check(player_ptr);
512
513     exe_enter_quest(player_ptr, i2enum<QuestId>(player_ptr->current_floor_ptr->grid_array[player_ptr->y][player_ptr->x].special));
514 }
515
516 bool inside_quest(QuestId id)
517 {
518     return id != QuestId::NONE;
519 }