OSDN Git Service

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