OSDN Git Service

[Refactor] #1710 Definitions QUEST_STATUS_* to enum class QuestKindType
[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 "dungeon/dungeon.h"
6 #include "floor/cave.h"
7 #include "floor/floor-events.h"
8 #include "floor/floor-mode-changer.h"
9 #include "floor/floor-object.h"
10 #include "game-option/play-record-options.h"
11 #include "grid/feature.h"
12 #include "io/write-diary.h"
13 #include "locale/english.h"
14 #include "main/music-definitions-table.h"
15 #include "main/sound-of-music.h"
16 #include "monster-race/monster-race-hook.h"
17 #include "monster-race/monster-race.h"
18 #include "monster-race/race-flags1.h"
19 #include "monster-race/race-flags7.h"
20 #include "monster-race/race-flags8.h"
21 #include "monster/monster-info.h"
22 #include "monster/monster-list.h"
23 #include "monster/monster-util.h"
24 #include "monster/smart-learn-types.h"
25 #include "object-enchant/item-apply-magic.h"
26 #include "object-enchant/trg-types.h"
27 #include "player-status/player-energy.h"
28 #include "player/player-personality-types.h"
29 #include "player/player-status.h"
30 #include "system/artifact-type-definition.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 "util/bit-flags-calculator.h"
36 #include "view/display-messages.h"
37 #include "world/world.h"
38
39 std::vector<quest_type> quest; /*!< Quest info */
40 QUEST_IDX max_q_idx; /*!< Maximum number of quests */
41 char quest_text[10][80]; /*!< Quest text */
42 int quest_text_line; /*!< Current line of the quest text */
43 int leaving_quest = 0;
44
45 /*!
46  * @brief クエスト突入時のメッセージテーブル / Array of places to find an inscription
47  */
48 static concptr find_quest[] = {
49     _("床にメッセージが刻まれている:", "You find the following inscription in the floor"),
50     _("壁にメッセージが刻まれている:", "You see a message inscribed in the wall"),
51     _("メッセージを見つけた:", "There is a sign saying"),
52     _("何かが階段の上に書いてある:", "Something is written on the staircase"),
53     _("巻物を見つけた。メッセージが書いてある:", "You find a scroll with the following message"),
54 };
55
56 /*!
57  * @brief 該当IDが固定クエストかどうかを判定する.
58  * @param quest_idx クエストID
59  * @return 固定クエストならばTRUEを返す
60  */
61 bool quest_type::is_fixed(short quest_idx)
62 {
63     return ((quest_idx) < MIN_RANDOM_QUEST) || ((quest_idx) > MAX_RANDOM_QUEST);
64 }
65
66 /*!
67  * @brief ランダムクエストの討伐ユニークを決める / Determine the random quest uniques
68  * @param q_ptr クエスト構造体の参照ポインタ
69  */
70 void determine_random_questor(player_type *player_ptr, quest_type *q_ptr)
71 {
72     get_mon_num_prep(player_ptr, mon_hook_quest, nullptr);
73
74     MONRACE_IDX r_idx;
75     while (true) {
76         /*
77          * Random monster 5 - 10 levels out of depth
78          * (depending on level)
79          */
80         r_idx = get_mon_num(player_ptr, 0, q_ptr->level + 5 + randint1(q_ptr->level / 10), GMN_ARENA);
81         monster_race *r_ptr;
82         r_ptr = &r_info[r_idx];
83
84         if (!(r_ptr->flags1 & RF1_UNIQUE))
85             continue;
86         if (r_ptr->flags8 & RF8_NO_QUEST)
87             continue;
88         if (r_ptr->flags1 & RF1_QUESTOR)
89             continue;
90         if (r_ptr->rarity > 100)
91             continue;
92         if (r_ptr->flags7 & RF7_FRIENDLY)
93             continue;
94         if (r_ptr->flags7 & RF7_AQUATIC)
95             continue;
96         if (r_ptr->flags8 & RF8_WILD_ONLY)
97             continue;
98         if (no_questor_or_bounty_uniques(r_idx))
99             continue;
100
101         /*
102          * Accept monsters that are 2 - 6 levels
103          * out of depth depending on the quest level
104          */
105         if (r_ptr->level > (q_ptr->level + (q_ptr->level / 20)))
106             break;
107     }
108
109     q_ptr->r_idx = r_idx;
110 }
111
112 /*!
113  * @brief クエストの最終状態を記録する(成功or失敗、時間)
114  * @param player_type プレイヤー情報への参照ポインタ
115  * @param q_ptr クエスト情報への参照ポインタ
116  * @param stat ステータス(成功or失敗)
117  */
118 void record_quest_final_status(quest_type *q_ptr, PLAYER_LEVEL lev, QuestStatusType stat)
119 {
120     q_ptr->status = stat;
121     q_ptr->complev = lev;
122     update_playtime();
123     q_ptr->comptime = w_ptr->play_time;
124 }
125
126 /*!
127  * @brief クエストを達成状態にする /
128  * @param player_ptr プレイヤーへの参照ポインタ
129  * @param quest_num 達成状態にしたいクエストのID
130  */
131 void complete_quest(player_type *player_ptr, QUEST_IDX quest_num)
132 {
133     quest_type *const q_ptr = &quest[quest_num];
134
135     switch (q_ptr->type) {
136     case QuestKindType::RANDOM:
137         if (record_rand_quest)
138             exe_write_diary(player_ptr, DIARY_RAND_QUEST_C, quest_num, nullptr);
139         break;
140     default:
141         if (record_fix_quest)
142             exe_write_diary(player_ptr, DIARY_FIX_QUEST_C, quest_num, nullptr);
143         break;
144     }
145
146     record_quest_final_status(q_ptr, player_ptr->lev, QuestStatusType::COMPLETED);
147
148     if (q_ptr->flags & QUEST_FLAG_SILENT)
149         return;
150
151     play_music(TERM_XTRA_MUSIC_BASIC, MUSIC_BASIC_QUEST_CLEAR);
152     msg_print(_("クエストを達成した!", "You just completed your quest!"));
153     msg_print(nullptr);
154 }
155
156 /*!
157  * @brief 特定のアーティファクトを入手した際のクエスト達成処理 /
158  * Check for "Quest" completion when a quest monster is killed or charmed.
159  * @param player_ptr プレイヤーへの参照ポインタ
160  * @param o_ptr 入手したオブジェクトの構造体参照ポインタ
161  */
162 void check_find_art_quest_completion(player_type *player_ptr, object_type *o_ptr)
163 {
164     /* Check if completed a quest */
165     for (QUEST_IDX i = 0; i < max_q_idx; i++) {
166         if ((quest[i].type == QuestKindType::FIND_ARTIFACT) && (quest[i].status == QuestStatusType::TAKEN) && (quest[i].k_idx == o_ptr->name1)) {
167             complete_quest(player_ptr, i);
168         }
169     }
170 }
171
172 /*!
173  * @brief クエストの導入メッセージを表示する / Discover quest
174  * @param q_idx 開始されたクエストのID
175  */
176 void quest_discovery(QUEST_IDX q_idx)
177 {
178     quest_type *q_ptr = &quest[q_idx];
179     monster_race *r_ptr = &r_info[q_ptr->r_idx];
180     MONSTER_NUMBER q_num = q_ptr->max_num;
181
182     if (!q_idx)
183         return;
184
185     GAME_TEXT name[MAX_NLEN];
186     strcpy(name, (r_ptr->name.c_str()));
187
188     msg_print(find_quest[rand_range(0, 4)]);
189     msg_print(nullptr);
190
191     if (q_num != 1) {
192 #ifdef JP
193 #else
194         plural_aux(name);
195 #endif
196         msg_format(_("注意しろ!この階は%d体の%sによって守られている!", "Be warned, this level is guarded by %d %s!"), q_num, name);
197         return;
198     }
199
200     bool is_random_quest_skipped = (r_ptr->flags1 & RF1_UNIQUE) != 0;
201     is_random_quest_skipped &= r_ptr->max_num == 0;
202     if (!is_random_quest_skipped) {
203         msg_format(_("注意せよ!この階は%sによって守られている!", "Beware, this level is protected by %s!"), name);
204         return;
205     }
206
207     msg_print(_("この階は以前は誰かによって守られていたようだ…。", "It seems that this level was protected by someone before..."));
208     record_quest_final_status(q_ptr, 0, QuestStatusType::FINISHED);
209 }
210
211 /*!
212  * @brief 新しく入ったダンジョンの階層に固定されている一般のクエストを探し出しIDを返す。
213  * / Hack -- Check if a level is a "quest" level
214  * @param player_ptr プレイヤーへの参照ポインタ
215  * @param level 検索対象になる階
216  * @return クエストIDを返す。該当がない場合0を返す。
217  */
218 QUEST_IDX quest_number(player_type *player_ptr, DEPTH level)
219 {
220     floor_type *floor_ptr = player_ptr->current_floor_ptr;
221     if (floor_ptr->inside_quest)
222         return (floor_ptr->inside_quest);
223
224     for (QUEST_IDX i = 0; i < max_q_idx; i++) {
225         if (quest[i].status != QuestStatusType::TAKEN)
226             continue;
227
228         if ((quest[i].type == QuestKindType::KILL_LEVEL) && !(quest[i].flags & QUEST_FLAG_PRESET) && (quest[i].level == level)
229             && (quest[i].dungeon == player_ptr->dungeon_idx))
230             return i;
231     }
232
233     return random_quest_number(player_ptr, level);
234 }
235
236 /*!
237  * @brief 新しく入ったダンジョンの階層に固定されているランダムクエストを探し出しIDを返す。
238  * @param player_ptr プレイヤーへの参照ポインタ
239  * @param level 検索対象になる階
240  * @return クエストIDを返す。該当がない場合0を返す。
241  */
242 QUEST_IDX random_quest_number(player_type *player_ptr, DEPTH level)
243 {
244     if (player_ptr->dungeon_idx != DUNGEON_ANGBAND)
245         return 0;
246
247     for (QUEST_IDX i = MIN_RANDOM_QUEST; i < MAX_RANDOM_QUEST + 1; i++) {
248         if ((quest[i].type == QuestKindType::RANDOM) && (quest[i].status == QuestStatusType::TAKEN) && (quest[i].level == level)
249             && (quest[i].dungeon == DUNGEON_ANGBAND)) {
250             return i;
251         }
252     }
253
254     return 0;
255 }
256
257 /*!
258  * @brief クエスト階層から離脱する際の処理
259  * @param player_ptr プレイヤーへの参照ポインタ
260  */
261 void leave_quest_check(player_type *player_ptr)
262 {
263     leaving_quest = player_ptr->current_floor_ptr->inside_quest;
264     if (!leaving_quest)
265         return;
266
267     quest_type *const q_ptr = &quest[leaving_quest];
268     bool is_one_time_quest = ((q_ptr->flags & QUEST_FLAG_ONCE) || (q_ptr->type == QuestKindType::RANDOM)) && (q_ptr->status == QuestStatusType::TAKEN);
269     if (!is_one_time_quest)
270         return;
271
272     record_quest_final_status(q_ptr, player_ptr->lev, QuestStatusType::FAILED);
273
274     /* Additional settings */
275     switch (q_ptr->type) {
276     case QuestKindType::TOWER:
277         quest[QUEST_TOWER1].status = QuestStatusType::FAILED;
278         quest[QUEST_TOWER1].complev = player_ptr->lev;
279         break;
280     case QuestKindType::FIND_ARTIFACT:
281         a_info[q_ptr->k_idx].gen_flags.reset(TRG::QUESTITEM);
282         break;
283     case QuestKindType::RANDOM:
284         r_info[q_ptr->r_idx].flags1 &= ~(RF1_QUESTOR);
285         prepare_change_floor_mode(player_ptr, CFM_NO_RETURN);
286         break;
287     default:
288         break;
289     }
290
291     /* Record finishing a quest */
292     if (q_ptr->type == QuestKindType::RANDOM) {
293         if (record_rand_quest)
294             exe_write_diary(player_ptr, DIARY_RAND_QUEST_F, leaving_quest, nullptr);
295         return;
296     }
297
298     if (record_fix_quest)
299         exe_write_diary(player_ptr, DIARY_FIX_QUEST_F, leaving_quest, nullptr);
300 }
301
302 /*!
303  * @brief 「塔」クエストの各階層から離脱する際の処理
304  */
305 void leave_tower_check(player_type *player_ptr)
306 {
307     leaving_quest = player_ptr->current_floor_ptr->inside_quest;
308     bool is_leaving_from_tower = leaving_quest != 0;
309     is_leaving_from_tower &= quest[leaving_quest].type == QuestKindType::TOWER;
310     is_leaving_from_tower &= quest[QUEST_TOWER1].status != QuestStatusType::COMPLETED;
311     if (!is_leaving_from_tower)
312         return;
313     if (quest[leaving_quest].type != QuestKindType::TOWER)
314         return;
315
316     quest[QUEST_TOWER1].status = QuestStatusType::FAILED;
317     quest[QUEST_TOWER1].complev = player_ptr->lev;
318     update_playtime();
319     quest[QUEST_TOWER1].comptime = w_ptr->play_time;
320 }
321
322 /*! 
323  * @brief Player enters a new quest
324  */
325 void exe_enter_quest(player_type *player_ptr, QUEST_IDX quest_idx)
326 {
327     if (quest[quest_idx].type != QuestKindType::RANDOM)
328         player_ptr->current_floor_ptr->dun_level = 1;
329     player_ptr->current_floor_ptr->inside_quest = quest_idx;
330
331     player_ptr->leaving = true;
332 }
333
334 /*!
335  * @brief クエスト入り口にプレイヤーが乗った際の処理 / Do building commands
336  * @param player_ptr プレイヤーへの参照ポインタ
337  */
338 void do_cmd_quest(player_type *player_ptr)
339 {
340     if (player_ptr->wild_mode)
341         return;
342
343     PlayerEnergy(player_ptr).set_player_turn_energy(100);
344
345     if (!cave_has_flag_bold(player_ptr->current_floor_ptr, player_ptr->y, player_ptr->x, FF::QUEST_ENTER)) {
346         msg_print(_("ここにはクエストの入口はない。", "You see no quest level here."));
347         return;
348     }
349
350     msg_print(_("ここにはクエストへの入口があります。", "There is an entry of a quest."));
351     if (!get_check(_("クエストに入りますか?", "Do you enter? ")))
352         return;
353     if (is_echizen(player_ptr))
354         msg_print(_("『とにかく入ってみようぜぇ。』", "\"Let's go in anyway.\""));
355     else if (is_chargeman(player_ptr))
356         msg_print(_("『全滅してやるぞ!』", "\"I'll annihilate THEM!\""));
357
358     player_ptr->oldpy = 0;
359     player_ptr->oldpx = 0;
360     leave_quest_check(player_ptr);
361
362     exe_enter_quest(player_ptr, player_ptr->current_floor_ptr->grid_array[player_ptr->y][player_ptr->x].special);
363 }