OSDN Git Service

[Refactor] #40274 Unified angband_music_basic_name[] into main/music-definitions...
[hengband/hengband.git] / src / quest.c
1 #include "angband.h"
2 #include "util.h"
3 #include "main/music-definitions-table.h"
4
5 #include "core.h"
6 #include "dungeon.h"
7 #include "floor.h"
8 #include "floor-save.h"
9 #include "floor-events.h"
10 #include "grid.h"
11 #include "quest.h"
12 #include "monsterrace-hook.h"
13 #include "monster.h"
14 #include "player-status.h"
15 #include "player-personality.h"
16 #include "artifact.h"
17 #include "feature.h"
18 #include "world.h"
19 #include "io/write-diary.h"
20 #include "cmd/cmd-dump.h"
21 #include "english.h"
22 #include "view-mainwindow.h"
23
24 quest_type *quest; /*!< Quest info */
25 QUEST_IDX max_q_idx; /*!< Maximum number of quests */
26 char quest_text[10][80]; /*!< Quest text */
27 int quest_text_line; /*!< Current line of the quest text */
28 int leaving_quest = 0;
29
30 /*!
31  * @brief クエスト突入時のメッセージテーブル / Array of places to find an inscription
32  */
33 static concptr find_quest[] =
34 {
35         _("床にメッセージが刻まれている:", "You find the following inscription in the floor"),
36         _("壁にメッセージが刻まれている:", "You see a message inscribed in the wall"),
37         _("メッセージを見つけた:", "There is a sign saying"),
38         _("何かが階段の上に書いてある:", "Something is written on the staircase"),
39         _("巻物を見つけた。メッセージが書いてある:", "You find a scroll with the following message"),
40 };
41
42
43 /*!
44  * @brief ランダムクエストの討伐ユニークを決める / Determine the random quest uniques
45  * @param q_ptr クエスト構造体の参照ポインタ
46  * @return なし
47  */
48 void determine_random_questor(player_type *player_ptr, quest_type *q_ptr)
49 {
50         get_mon_num_prep(player_ptr, mon_hook_quest, NULL);
51
52         MONRACE_IDX r_idx;
53         while (TRUE)
54         {
55                 /*
56                  * Random monster 5 - 10 levels out of depth
57                  * (depending on level)
58                  */
59                 r_idx = get_mon_num(player_ptr, q_ptr->level + 5 + randint1(q_ptr->level / 10), GMN_ARENA);
60                 monster_race *r_ptr;
61                 r_ptr = &r_info[r_idx];
62
63                 if (!(r_ptr->flags1 & RF1_UNIQUE)) continue;
64                 if (r_ptr->flags1 & RF1_QUESTOR) continue;
65                 if (r_ptr->rarity > 100) continue;
66                 if (r_ptr->flags7 & RF7_FRIENDLY) continue;
67                 if (r_ptr->flags7 & RF7_AQUATIC) continue;
68                 if (r_ptr->flags8 & RF8_WILD_ONLY) continue;
69                 if (no_questor_or_bounty_uniques(r_idx)) continue;
70
71                 /*
72                  * Accept monsters that are 2 - 6 levels
73                  * out of depth depending on the quest level
74                  */
75                 if (r_ptr->level > (q_ptr->level + (q_ptr->level / 20))) break;
76         }
77
78         q_ptr->r_idx = r_idx;
79 }
80
81
82 /*!
83  * @brief クエストを達成状態にする /
84  * @param player_ptr プレーヤーへの参照ポインタ
85  * @param quest_num 達成状態にしたいクエストのID
86  * @return なし
87  */
88 void complete_quest(player_type *player_ptr, QUEST_IDX quest_num)
89 {
90         quest_type* const q_ptr = &quest[quest_num];
91
92         switch (q_ptr->type)
93         {
94         case QUEST_TYPE_RANDOM:
95                 if (record_rand_quest) exe_write_diary(player_ptr, DIARY_RAND_QUEST_C, quest_num, NULL);
96                 break;
97         default:
98                 if (record_fix_quest) exe_write_diary(player_ptr, DIARY_FIX_QUEST_C, quest_num, NULL);
99                 break;
100         }
101
102         q_ptr->status = QUEST_STATUS_COMPLETED;
103         q_ptr->complev = player_ptr->lev;
104         update_playtime();
105         q_ptr->comptime = current_world_ptr->play_time;
106
107         if (q_ptr->flags & QUEST_FLAG_SILENT) return;
108
109         play_music(TERM_XTRA_MUSIC_BASIC, MUSIC_BASIC_QUEST_CLEAR);
110         msg_print(_("クエストを達成した!", "You just completed your quest!"));
111         msg_print(NULL);
112 }
113
114
115 /*!
116  * @brief 特定の敵を倒した際にクエスト達成処理 /
117  * Check for "Quest" completion when a quest monster is killed or charmed.
118  * @param player_ptr プレーヤーへの参照ポインタ
119  * @param m_ptr 撃破したモンスターの構造体参照ポインタ
120  * @return なし
121  */
122 void check_quest_completion(player_type *player_ptr, monster_type *m_ptr)
123 {
124         POSITION y = m_ptr->fy;
125         POSITION x = m_ptr->fx;
126
127         floor_type *floor_ptr = player_ptr->current_floor_ptr;
128         QUEST_IDX quest_num = floor_ptr->inside_quest;
129
130         /* Search for an active quest on this dungeon level */
131         if (!quest_num)
132         {
133                 QUEST_IDX i;
134                 for (i = max_q_idx - 1; i > 0; i--)
135                 {
136                         quest_type* const q_ptr = &quest[i];
137
138                         /* Quest is not active */
139                         if (q_ptr->status != QUEST_STATUS_TAKEN)
140                                 continue;
141
142                         /* Quest is not a dungeon quest */
143                         if (q_ptr->flags & QUEST_FLAG_PRESET)
144                                 continue;
145
146                         /* Quest is not on this level */
147                         if ((q_ptr->level != floor_ptr->dun_level) &&
148                                 (q_ptr->type != QUEST_TYPE_KILL_ANY_LEVEL))
149                                 continue;
150
151                         /* Not a "kill monster" quest */
152                         if ((q_ptr->type == QUEST_TYPE_FIND_ARTIFACT) ||
153                                 (q_ptr->type == QUEST_TYPE_FIND_EXIT))
154                                 continue;
155
156                         /* Interesting quest */
157                         if ((q_ptr->type == QUEST_TYPE_KILL_NUMBER) ||
158                                 (q_ptr->type == QUEST_TYPE_TOWER) ||
159                                 (q_ptr->type == QUEST_TYPE_KILL_ALL))
160                                 break;
161
162                         /* Interesting quest */
163                         if (((q_ptr->type == QUEST_TYPE_KILL_LEVEL) ||
164                                 (q_ptr->type == QUEST_TYPE_KILL_ANY_LEVEL) ||
165                                 (q_ptr->type == QUEST_TYPE_RANDOM)) &&
166                                 (q_ptr->r_idx == m_ptr->r_idx))
167                                 break;
168                 }
169
170                 quest_num = i;
171         }
172
173         /* Handle the current quest */
174         bool create_stairs = FALSE;
175         bool reward = FALSE;
176         if (quest_num && (quest[quest_num].status == QUEST_STATUS_TAKEN))
177         {
178                 /* Current quest */
179                 quest_type* const q_ptr = &quest[quest_num];
180
181                 switch (q_ptr->type)
182                 {
183                 case QUEST_TYPE_KILL_NUMBER:
184                 {
185                         q_ptr->cur_num++;
186
187                         if (q_ptr->cur_num >= q_ptr->num_mon)
188                         {
189                                 complete_quest(player_ptr, quest_num);
190                                 q_ptr->cur_num = 0;
191                         }
192
193                         break;
194                 }
195                 case QUEST_TYPE_KILL_ALL:
196                 {
197                         if (!is_hostile(m_ptr)) break;
198
199                         if (count_all_hostile_monsters(floor_ptr) != 1) break;
200
201                         if (q_ptr->flags & QUEST_FLAG_SILENT)
202                         {
203                                 q_ptr->status = QUEST_STATUS_FINISHED;
204                         }
205                         else
206                         {
207                                 complete_quest(player_ptr, quest_num);
208                         }
209
210                         break;
211                 }
212                 case QUEST_TYPE_KILL_LEVEL:
213                 case QUEST_TYPE_RANDOM:
214                 {
215                         /* Only count valid monsters */
216                         if (q_ptr->r_idx != m_ptr->r_idx)
217                                 break;
218
219                         q_ptr->cur_num++;
220
221                         if (q_ptr->cur_num < q_ptr->max_num) break;
222
223                         complete_quest(player_ptr, quest_num);
224
225                         if (!(q_ptr->flags & QUEST_FLAG_PRESET))
226                         {
227                                 create_stairs = TRUE;
228                                 floor_ptr->inside_quest = 0;
229                         }
230
231                         /* Finish the two main quests without rewarding */
232                         if ((quest_num == QUEST_OBERON) || (quest_num == QUEST_SERPENT))
233                         {
234                                 q_ptr->status = QUEST_STATUS_FINISHED;
235                         }
236
237                         if (q_ptr->type == QUEST_TYPE_RANDOM)
238                         {
239                                 reward = TRUE;
240                                 q_ptr->status = QUEST_STATUS_FINISHED;
241                         }
242
243                         break;
244                 }
245                 case QUEST_TYPE_KILL_ANY_LEVEL:
246                 {
247                         q_ptr->cur_num++;
248                         if (q_ptr->cur_num >= q_ptr->max_num)
249                         {
250                                 complete_quest(player_ptr, quest_num);
251                                 q_ptr->cur_num = 0;
252                         }
253
254                         break;
255                 }
256                 case QUEST_TYPE_TOWER:
257                 {
258                         if (!is_hostile(m_ptr)) break;
259
260                         if (count_all_hostile_monsters(floor_ptr) == 1)
261                         {
262                                 q_ptr->status = QUEST_STATUS_STAGE_COMPLETED;
263
264                                 if ((quest[QUEST_TOWER1].status == QUEST_STATUS_STAGE_COMPLETED) &&
265                                         (quest[QUEST_TOWER2].status == QUEST_STATUS_STAGE_COMPLETED) &&
266                                         (quest[QUEST_TOWER3].status == QUEST_STATUS_STAGE_COMPLETED))
267                                 {
268
269                                         complete_quest(player_ptr, QUEST_TOWER1);
270                                 }
271                         }
272
273                         break;
274                 }
275                 }
276         }
277
278         /* Create a magical staircase */
279         if (create_stairs)
280         {
281                 POSITION ny, nx;
282
283                 /* Stagger around */
284                 while (cave_perma_bold(floor_ptr, y, x) || floor_ptr->grid_array[y][x].o_idx || (floor_ptr->grid_array[y][x].info & CAVE_OBJECT))
285                 {
286                         /* Pick a location */
287                         scatter(player_ptr, &ny, &nx, y, x, 1, 0);
288
289                         /* Stagger */
290                         y = ny; x = nx;
291                 }
292
293                 /* Explain the staircase */
294                 msg_print(_("魔法の階段が現れた...", "A magical staircase appears..."));
295
296                 /* Create stairs down */
297                 cave_set_feat(player_ptr, y, x, feat_down_stair);
298
299                 /* Remember to update everything */
300                 player_ptr->update |= (PU_FLOW);
301         }
302
303         if (!reward) return;
304
305         object_type forge;
306         object_type *o_ptr;
307         for (int i = 0; i < (floor_ptr->dun_level / 15) + 1; i++)
308         {
309                 o_ptr = &forge;
310                 object_wipe(o_ptr);
311
312                 /* Make a great object */
313                 make_object(player_ptr, o_ptr, AM_GOOD | AM_GREAT);
314                 (void)drop_near(player_ptr, o_ptr, -1, y, x);
315         }
316 }
317
318
319 /*!
320  * @brief 特定のアーティファクトを入手した際のクエスト達成処理 /
321  * Check for "Quest" completion when a quest monster is killed or charmed.
322  * @param player_ptr プレーヤーへの参照ポインタ
323  * @param o_ptr 入手したオブジェクトの構造体参照ポインタ
324  * @return なし
325  */
326 void check_find_art_quest_completion(player_type *player_ptr, object_type *o_ptr)
327 {
328         /* Check if completed a quest */
329         for (QUEST_IDX i = 0; i < max_q_idx; i++)
330         {
331                 if ((quest[i].type == QUEST_TYPE_FIND_ARTIFACT) &&
332                         (quest[i].status == QUEST_STATUS_TAKEN) &&
333                         (quest[i].k_idx == o_ptr->name1))
334                 {
335                         complete_quest(player_ptr, i);
336                 }
337         }
338 }
339
340
341 /*!
342  * @brief クエストの導入メッセージを表示する / Discover quest
343  * @param q_idx 開始されたクエストのID
344  */
345 void quest_discovery(QUEST_IDX q_idx)
346 {
347         quest_type *q_ptr = &quest[q_idx];
348         monster_race *r_ptr = &r_info[q_ptr->r_idx];
349         MONSTER_NUMBER q_num = q_ptr->max_num;
350
351         if (!q_idx) return;
352
353         GAME_TEXT name[MAX_NLEN];
354         strcpy(name, (r_name + r_ptr->name));
355
356         msg_print(find_quest[rand_range(0, 4)]);
357         msg_print(NULL);
358
359         if (q_num != 1)
360         {
361 #ifdef JP
362 #else
363                 plural_aux(name);
364 #endif
365                 msg_format(_("注意しろ!この階は%d体の%sによって守られている!", "Be warned, this level is guarded by %d %s!"), q_num, name);
366                 return;
367         }
368
369         bool is_random_quest_skipped = (r_ptr->flags1 & RF1_UNIQUE) != 0;
370         is_random_quest_skipped &= r_ptr->max_num == 0;
371         if (!is_random_quest_skipped)
372         {
373                 msg_format(_("注意せよ!この階は%sによって守られている!", "Beware, this level is protected by %s!"), name);
374                 return;
375         }
376
377         msg_print(_("この階は以前は誰かによって守られていたようだ…。", "It seems that this level was protected by someone before..."));
378         quest[q_idx].status = QUEST_STATUS_FINISHED;
379         q_ptr->complev = 0;
380         update_playtime();
381         q_ptr->comptime = current_world_ptr->play_time;
382 }
383
384
385 /*!
386  * @brief 新しく入ったダンジョンの階層に固定されている一般のクエストを探し出しIDを返す。
387  * / Hack -- Check if a level is a "quest" level
388  * @param player_ptr プレーヤーへの参照ポインタ
389  * @param level 検索対象になる階
390  * @return クエストIDを返す。該当がない場合0を返す。
391  */
392 QUEST_IDX quest_number(player_type *player_ptr, DEPTH level)
393 {
394         floor_type *floor_ptr = player_ptr->current_floor_ptr;
395         if (floor_ptr->inside_quest)
396                 return (floor_ptr->inside_quest);
397
398         for (QUEST_IDX i = 0; i < max_q_idx; i++)
399         {
400                 if (quest[i].status != QUEST_STATUS_TAKEN) continue;
401
402                 if ((quest[i].type == QUEST_TYPE_KILL_LEVEL) &&
403                         !(quest[i].flags & QUEST_FLAG_PRESET) &&
404                         (quest[i].level == level) &&
405                         (quest[i].dungeon == player_ptr->dungeon_idx))
406                         return i;
407         }
408
409         return random_quest_number(player_ptr, level);
410 }
411
412
413 /*!
414  * @brief 新しく入ったダンジョンの階層に固定されているランダムクエストを探し出しIDを返す。
415  * @param player_ptr プレーヤーへの参照ポインタ
416  * @param level 検索対象になる階
417  * @return クエストIDを返す。該当がない場合0を返す。
418  */
419 QUEST_IDX random_quest_number(player_type *player_ptr, DEPTH level)
420 {
421         if (player_ptr->dungeon_idx != DUNGEON_ANGBAND) return 0;
422
423         for (QUEST_IDX i = MIN_RANDOM_QUEST; i < MAX_RANDOM_QUEST + 1; i++)
424         {
425                 if ((quest[i].type == QUEST_TYPE_RANDOM) &&
426                         (quest[i].status == QUEST_STATUS_TAKEN) &&
427                         (quest[i].level == level) &&
428                         (quest[i].dungeon == DUNGEON_ANGBAND))
429                 {
430                         return i;
431                 }
432         }
433
434         return 0;
435 }
436
437
438 /*!
439  * @brief クエスト階層から離脱する際の処理
440  * @param player_ptr プレーヤーへの参照ポインタ
441  * @return なし
442  */
443 void leave_quest_check(player_type *player_ptr)
444 {
445         leaving_quest = player_ptr->current_floor_ptr->inside_quest;
446         if (!leaving_quest) return;
447
448         quest_type* const q_ptr = &quest[leaving_quest];
449         bool is_one_time_quest = ((q_ptr->flags & QUEST_FLAG_ONCE) || (q_ptr->type == QUEST_TYPE_RANDOM)) &&
450                 (q_ptr->status == QUEST_STATUS_TAKEN);
451         if (!is_one_time_quest) return;
452
453         q_ptr->status = QUEST_STATUS_FAILED;
454         q_ptr->complev = player_ptr->lev;
455         update_playtime();
456         q_ptr->comptime = current_world_ptr->play_time;
457
458         /* Additional settings */
459         switch (q_ptr->type)
460         {
461         case QUEST_TYPE_TOWER:
462                 quest[QUEST_TOWER1].status = QUEST_STATUS_FAILED;
463                 quest[QUEST_TOWER1].complev = player_ptr->lev;
464                 break;
465         case QUEST_TYPE_FIND_ARTIFACT:
466                 a_info[q_ptr->k_idx].gen_flags &= ~(TRG_QUESTITEM);
467                 break;
468         case QUEST_TYPE_RANDOM:
469                 r_info[q_ptr->r_idx].flags1 &= ~(RF1_QUESTOR);
470
471                 /* Floor of random quest will be blocked */
472                 prepare_change_floor_mode(player_ptr, CFM_NO_RETURN);
473                 break;
474         }
475
476         /* Record finishing a quest */
477         if (q_ptr->type == QUEST_TYPE_RANDOM)
478         {
479                 if (record_rand_quest)
480                         exe_write_diary(player_ptr, DIARY_RAND_QUEST_F, leaving_quest, NULL);
481                 return;
482         }
483
484         if (record_fix_quest)
485                 exe_write_diary(player_ptr, DIARY_FIX_QUEST_F, leaving_quest, NULL);
486 }
487
488
489 /*!
490  * @brief 「塔」クエストの各階層から離脱する際の処理
491  * @return なし
492  */
493 void leave_tower_check(player_type *player_ptr)
494 {
495         leaving_quest = player_ptr->current_floor_ptr->inside_quest;
496         bool is_leaving_from_tower = leaving_quest != 0;
497         is_leaving_from_tower &= quest[leaving_quest].type == QUEST_TYPE_TOWER;
498         is_leaving_from_tower &= quest[QUEST_TOWER1].status != QUEST_STATUS_COMPLETED;
499         if (!is_leaving_from_tower) return;
500         if (quest[leaving_quest].type != QUEST_TYPE_TOWER) return;
501
502         quest[QUEST_TOWER1].status = QUEST_STATUS_FAILED;
503         quest[QUEST_TOWER1].complev = player_ptr->lev;
504         update_playtime();
505         quest[QUEST_TOWER1].comptime = current_world_ptr->play_time;
506 }
507
508
509 /*!
510  * @brief クエスト入り口にプレイヤーが乗った際の処理 / Do building commands
511  * @param player_ptr プレーヤーへの参照ポインタ
512  * @return なし
513  */
514 void do_cmd_quest(player_type *player_ptr)
515 {
516         if (player_ptr->wild_mode) return;
517
518         take_turn(player_ptr, 100);
519
520         if (!cave_have_flag_bold(player_ptr->current_floor_ptr, player_ptr->y, player_ptr->x, FF_QUEST_ENTER))
521         {
522                 msg_print(_("ここにはクエストの入口はない。", "You see no quest level here."));
523                 return;
524         }
525
526         msg_print(_("ここにはクエストへの入口があります。", "There is an entry of a quest."));
527         if (!get_check(_("クエストに入りますか?", "Do you enter? "))) return;
528         if (IS_ECHIZEN(player_ptr))
529                 msg_print(_("『とにかく入ってみようぜぇ。』", ""));
530         else if (player_ptr->pseikaku == SEIKAKU_CHARGEMAN) msg_print("『全滅してやるぞ!』");
531
532         /* Player enters a new quest */
533         player_ptr->oldpy = 0;
534         player_ptr->oldpx = 0;
535
536         leave_quest_check(player_ptr);
537
538         if (quest[player_ptr->current_floor_ptr->inside_quest].type != QUEST_TYPE_RANDOM) player_ptr->current_floor_ptr->dun_level = 1;
539         player_ptr->current_floor_ptr->inside_quest = player_ptr->current_floor_ptr->grid_array[player_ptr->y][player_ptr->x].special;
540
541         player_ptr->leaving = TRUE;
542 }