OSDN Git Service

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