OSDN Git Service

[Refactor] #40624 Separated quest-completion-checker.c/h from floor-events.c/h and...
[hengbandforosx/hengbandosx.git] / src / dungeon / quest.c
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-object.h"
9 #include "floor/floor-save.h"
10 #include "floor/floor.h"
11 #include "game-option/play-record-options.h"
12 #include "grid/feature.h"
13 #include "grid/grid.h"
14 #include "io/write-diary.h"
15 #include "locale/english.h"
16 #include "main/music-definitions-table.h"
17 #include "main/sound-of-music.h"
18 #include "monster-race/monster-race-hook.h"
19 #include "monster-race/monster-race.h"
20 #include "monster-race/race-flags1.h"
21 #include "monster-race/race-flags7.h"
22 #include "monster-race/race-flags8.h"
23 #include "monster/monster-info.h"
24 #include "monster/monster-list.h"
25 #include "monster/monster-util.h"
26 #include "monster/smart-learn-types.h"
27 #include "object-enchant/item-apply-magic.h"
28 #include "object-enchant/trg-types.h"
29 #include "object/object-generator.h"
30 #include "player/player-personalities-types.h"
31 #include "player/player-status.h"
32 #include "system/artifact-type-definition.h"
33 #include "system/floor-type-definition.h"
34 #include "system/system-variables.h"
35 #include "util/bit-flags-calculator.h"
36 #include "view/display-messages.h"
37 #include "world/world.h"
38
39 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 ランダムクエストの討伐ユニークを決める / Determine the random quest uniques
58  * @param q_ptr クエスト構造体の参照ポインタ
59  * @return なし
60  */
61 void determine_random_questor(player_type *player_ptr, quest_type *q_ptr)
62 {
63     get_mon_num_prep(player_ptr, mon_hook_quest, NULL);
64
65     MONRACE_IDX r_idx;
66     while (TRUE) {
67         /*
68          * Random monster 5 - 10 levels out of depth
69          * (depending on level)
70          */
71         r_idx = get_mon_num(player_ptr, q_ptr->level + 5 + randint1(q_ptr->level / 10), GMN_ARENA);
72         monster_race *r_ptr;
73         r_ptr = &r_info[r_idx];
74
75         if (!(r_ptr->flags1 & RF1_UNIQUE))
76             continue;
77         if (r_ptr->flags1 & RF1_QUESTOR)
78             continue;
79         if (r_ptr->rarity > 100)
80             continue;
81         if (r_ptr->flags7 & RF7_FRIENDLY)
82             continue;
83         if (r_ptr->flags7 & RF7_AQUATIC)
84             continue;
85         if (r_ptr->flags8 & RF8_WILD_ONLY)
86             continue;
87         if (no_questor_or_bounty_uniques(r_idx))
88             continue;
89
90         /*
91          * Accept monsters that are 2 - 6 levels
92          * out of depth depending on the quest level
93          */
94         if (r_ptr->level > (q_ptr->level + (q_ptr->level / 20)))
95             break;
96     }
97
98     q_ptr->r_idx = r_idx;
99 }
100
101 /*!
102  * @brief クエストを達成状態にする /
103  * @param player_ptr プレーヤーへの参照ポインタ
104  * @param quest_num 達成状態にしたいクエストのID
105  * @return なし
106  */
107 void complete_quest(player_type *player_ptr, QUEST_IDX quest_num)
108 {
109     quest_type *const q_ptr = &quest[quest_num];
110
111     switch (q_ptr->type) {
112     case QUEST_TYPE_RANDOM:
113         if (record_rand_quest)
114             exe_write_diary(player_ptr, DIARY_RAND_QUEST_C, quest_num, NULL);
115         break;
116     default:
117         if (record_fix_quest)
118             exe_write_diary(player_ptr, DIARY_FIX_QUEST_C, quest_num, NULL);
119         break;
120     }
121
122     q_ptr->status = QUEST_STATUS_COMPLETED;
123     q_ptr->complev = player_ptr->lev;
124     update_playtime();
125     q_ptr->comptime = current_world_ptr->play_time;
126
127     if (q_ptr->flags & QUEST_FLAG_SILENT)
128         return;
129
130     play_music(TERM_XTRA_MUSIC_BASIC, MUSIC_BASIC_QUEST_CLEAR);
131     msg_print(_("クエストを達成した!", "You just completed your quest!"));
132     msg_print(NULL);
133 }
134
135 /*!
136  * @brief 特定のアーティファクトを入手した際のクエスト達成処理 /
137  * Check for "Quest" completion when a quest monster is killed or charmed.
138  * @param player_ptr プレーヤーへの参照ポインタ
139  * @param o_ptr 入手したオブジェクトの構造体参照ポインタ
140  * @return なし
141  */
142 void check_find_art_quest_completion(player_type *player_ptr, object_type *o_ptr)
143 {
144     /* Check if completed a quest */
145     for (QUEST_IDX i = 0; i < max_q_idx; i++) {
146         if ((quest[i].type == QUEST_TYPE_FIND_ARTIFACT) && (quest[i].status == QUEST_STATUS_TAKEN) && (quest[i].k_idx == o_ptr->name1)) {
147             complete_quest(player_ptr, i);
148         }
149     }
150 }
151
152 /*!
153  * @brief クエストの導入メッセージを表示する / Discover quest
154  * @param q_idx 開始されたクエストのID
155  */
156 void quest_discovery(QUEST_IDX q_idx)
157 {
158     quest_type *q_ptr = &quest[q_idx];
159     monster_race *r_ptr = &r_info[q_ptr->r_idx];
160     MONSTER_NUMBER q_num = q_ptr->max_num;
161
162     if (!q_idx)
163         return;
164
165     GAME_TEXT name[MAX_NLEN];
166     strcpy(name, (r_name + r_ptr->name));
167
168     msg_print(find_quest[rand_range(0, 4)]);
169     msg_print(NULL);
170
171     if (q_num != 1) {
172 #ifdef JP
173 #else
174         plural_aux(name);
175 #endif
176         msg_format(_("注意しろ!この階は%d体の%sによって守られている!", "Be warned, this level is guarded by %d %s!"), q_num, name);
177         return;
178     }
179
180     bool is_random_quest_skipped = (r_ptr->flags1 & RF1_UNIQUE) != 0;
181     is_random_quest_skipped &= r_ptr->max_num == 0;
182     if (!is_random_quest_skipped) {
183         msg_format(_("注意せよ!この階は%sによって守られている!", "Beware, this level is protected by %s!"), name);
184         return;
185     }
186
187     msg_print(_("この階は以前は誰かによって守られていたようだ…。", "It seems that this level was protected by someone before..."));
188     quest[q_idx].status = QUEST_STATUS_FINISHED;
189     q_ptr->complev = 0;
190     update_playtime();
191     q_ptr->comptime = current_world_ptr->play_time;
192 }
193
194 /*!
195  * @brief 新しく入ったダンジョンの階層に固定されている一般のクエストを探し出しIDを返す。
196  * / Hack -- Check if a level is a "quest" level
197  * @param player_ptr プレーヤーへの参照ポインタ
198  * @param level 検索対象になる階
199  * @return クエストIDを返す。該当がない場合0を返す。
200  */
201 QUEST_IDX quest_number(player_type *player_ptr, DEPTH level)
202 {
203     floor_type *floor_ptr = player_ptr->current_floor_ptr;
204     if (floor_ptr->inside_quest)
205         return (floor_ptr->inside_quest);
206
207     for (QUEST_IDX i = 0; i < max_q_idx; i++) {
208         if (quest[i].status != QUEST_STATUS_TAKEN)
209             continue;
210
211         if ((quest[i].type == QUEST_TYPE_KILL_LEVEL) && !(quest[i].flags & QUEST_FLAG_PRESET) && (quest[i].level == level)
212             && (quest[i].dungeon == player_ptr->dungeon_idx))
213             return i;
214     }
215
216     return random_quest_number(player_ptr, level);
217 }
218
219 /*!
220  * @brief 新しく入ったダンジョンの階層に固定されているランダムクエストを探し出しIDを返す。
221  * @param player_ptr プレーヤーへの参照ポインタ
222  * @param level 検索対象になる階
223  * @return クエストIDを返す。該当がない場合0を返す。
224  */
225 QUEST_IDX random_quest_number(player_type *player_ptr, DEPTH level)
226 {
227     if (player_ptr->dungeon_idx != DUNGEON_ANGBAND)
228         return 0;
229
230     for (QUEST_IDX i = MIN_RANDOM_QUEST; i < MAX_RANDOM_QUEST + 1; i++) {
231         if ((quest[i].type == QUEST_TYPE_RANDOM) && (quest[i].status == QUEST_STATUS_TAKEN) && (quest[i].level == level)
232             && (quest[i].dungeon == DUNGEON_ANGBAND)) {
233             return i;
234         }
235     }
236
237     return 0;
238 }
239
240 /*!
241  * @brief クエスト階層から離脱する際の処理
242  * @param player_ptr プレーヤーへの参照ポインタ
243  * @return なし
244  */
245 void leave_quest_check(player_type *player_ptr)
246 {
247     leaving_quest = player_ptr->current_floor_ptr->inside_quest;
248     if (!leaving_quest)
249         return;
250
251     quest_type *const q_ptr = &quest[leaving_quest];
252     bool is_one_time_quest = ((q_ptr->flags & QUEST_FLAG_ONCE) || (q_ptr->type == QUEST_TYPE_RANDOM)) && (q_ptr->status == QUEST_STATUS_TAKEN);
253     if (!is_one_time_quest)
254         return;
255
256     q_ptr->status = QUEST_STATUS_FAILED;
257     q_ptr->complev = player_ptr->lev;
258     update_playtime();
259     q_ptr->comptime = current_world_ptr->play_time;
260
261     /* Additional settings */
262     switch (q_ptr->type) {
263     case QUEST_TYPE_TOWER:
264         quest[QUEST_TOWER1].status = QUEST_STATUS_FAILED;
265         quest[QUEST_TOWER1].complev = player_ptr->lev;
266         break;
267     case QUEST_TYPE_FIND_ARTIFACT:
268         a_info[q_ptr->k_idx].gen_flags &= ~(TRG_QUESTITEM);
269         break;
270     case QUEST_TYPE_RANDOM:
271         r_info[q_ptr->r_idx].flags1 &= ~(RF1_QUESTOR);
272
273         /* Floor of random quest will be blocked */
274         prepare_change_floor_mode(player_ptr, CFM_NO_RETURN);
275         break;
276     }
277
278     /* Record finishing a quest */
279     if (q_ptr->type == QUEST_TYPE_RANDOM) {
280         if (record_rand_quest)
281             exe_write_diary(player_ptr, DIARY_RAND_QUEST_F, leaving_quest, NULL);
282         return;
283     }
284
285     if (record_fix_quest)
286         exe_write_diary(player_ptr, DIARY_FIX_QUEST_F, leaving_quest, NULL);
287 }
288
289 /*!
290  * @brief 「塔」クエストの各階層から離脱する際の処理
291  * @return なし
292  */
293 void leave_tower_check(player_type *player_ptr)
294 {
295     leaving_quest = player_ptr->current_floor_ptr->inside_quest;
296     bool is_leaving_from_tower = leaving_quest != 0;
297     is_leaving_from_tower &= quest[leaving_quest].type == QUEST_TYPE_TOWER;
298     is_leaving_from_tower &= quest[QUEST_TOWER1].status != QUEST_STATUS_COMPLETED;
299     if (!is_leaving_from_tower)
300         return;
301     if (quest[leaving_quest].type != QUEST_TYPE_TOWER)
302         return;
303
304     quest[QUEST_TOWER1].status = QUEST_STATUS_FAILED;
305     quest[QUEST_TOWER1].complev = player_ptr->lev;
306     update_playtime();
307     quest[QUEST_TOWER1].comptime = current_world_ptr->play_time;
308 }
309
310 /*!
311  * @brief クエスト入り口にプレイヤーが乗った際の処理 / Do building commands
312  * @param player_ptr プレーヤーへの参照ポインタ
313  * @return なし
314  */
315 void do_cmd_quest(player_type *player_ptr)
316 {
317     if (player_ptr->wild_mode)
318         return;
319
320     take_turn(player_ptr, 100);
321
322     if (!cave_have_flag_bold(player_ptr->current_floor_ptr, player_ptr->y, player_ptr->x, FF_QUEST_ENTER)) {
323         msg_print(_("ここにはクエストの入口はない。", "You see no quest level here."));
324         return;
325     }
326
327     msg_print(_("ここにはクエストへの入口があります。", "There is an entry of a quest."));
328     if (!get_check(_("クエストに入りますか?", "Do you enter? ")))
329         return;
330     if (is_echizen(player_ptr))
331         msg_print(_("『とにかく入ってみようぜぇ。』", ""));
332     else if (player_ptr->pseikaku == PERSONALITY_CHARGEMAN)
333         msg_print("『全滅してやるぞ!』");
334
335     /* Player enters a new quest */
336     player_ptr->oldpy = 0;
337     player_ptr->oldpx = 0;
338
339     leave_quest_check(player_ptr);
340
341     if (quest[player_ptr->current_floor_ptr->inside_quest].type != QUEST_TYPE_RANDOM)
342         player_ptr->current_floor_ptr->dun_level = 1;
343     player_ptr->current_floor_ptr->inside_quest = player_ptr->current_floor_ptr->grid_array[player_ptr->y][player_ptr->x].special;
344
345     player_ptr->leaving = TRUE;
346 }