OSDN Git Service

Merge pull request #936 from shimitei/feature/#916_fix_sound_on_off
[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 "grid/grid.h"
13 #include "io/write-diary.h"
14 #include "locale/english.h"
15 #include "main/music-definitions-table.h"
16 #include "main/sound-of-music.h"
17 #include "monster-race/monster-race-hook.h"
18 #include "monster-race/monster-race.h"
19 #include "monster-race/race-flags1.h"
20 #include "monster-race/race-flags7.h"
21 #include "monster-race/race-flags8.h"
22 #include "monster/monster-info.h"
23 #include "monster/monster-list.h"
24 #include "monster/monster-util.h"
25 #include "monster/smart-learn-types.h"
26 #include "object-enchant/item-apply-magic.h"
27 #include "object-enchant/trg-types.h"
28 #include "object/object-generator.h"
29 #include "player/player-personalities-types.h"
30 #include "player/player-status.h"
31 #include "system/artifact-type-definition.h"
32 #include "system/floor-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 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, 0, 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 クエストの最終状態を記録する(成功or失敗、時間)
103  * @param player_type プレイヤー情報への参照ポインタ
104  * @param q_ptr クエスト情報への参照ポインタ
105  * @param stat ステータス(成功or失敗)
106  * @return なし
107  */
108 void record_quest_final_status(quest_type *q_ptr, PLAYER_LEVEL lev, QUEST_STATUS stat)
109 {
110     q_ptr->status = stat;
111     q_ptr->complev = lev;
112     update_playtime();
113     q_ptr->comptime = current_world_ptr->play_time;
114 }
115
116 /*!
117  * @brief クエストを達成状態にする /
118  * @param player_ptr プレーヤーへの参照ポインタ
119  * @param quest_num 達成状態にしたいクエストのID
120  * @return なし
121  */
122 void complete_quest(player_type *player_ptr, QUEST_IDX quest_num)
123 {
124     quest_type *const q_ptr = &quest[quest_num];
125
126     switch (q_ptr->type) {
127     case QUEST_TYPE_RANDOM:
128         if (record_rand_quest)
129             exe_write_diary(player_ptr, DIARY_RAND_QUEST_C, quest_num, NULL);
130         break;
131     default:
132         if (record_fix_quest)
133             exe_write_diary(player_ptr, DIARY_FIX_QUEST_C, quest_num, NULL);
134         break;
135     }
136
137     record_quest_final_status(q_ptr, player_ptr->lev, QUEST_STATUS_COMPLETED);
138
139     if (q_ptr->flags & QUEST_FLAG_SILENT)
140         return;
141
142     play_music(TERM_XTRA_MUSIC_BASIC, MUSIC_BASIC_QUEST_CLEAR);
143     msg_print(_("クエストを達成した!", "You just completed your quest!"));
144     msg_print(NULL);
145 }
146
147 /*!
148  * @brief 特定のアーティファクトを入手した際のクエスト達成処理 /
149  * Check for "Quest" completion when a quest monster is killed or charmed.
150  * @param player_ptr プレーヤーへの参照ポインタ
151  * @param o_ptr 入手したオブジェクトの構造体参照ポインタ
152  * @return なし
153  */
154 void check_find_art_quest_completion(player_type *player_ptr, object_type *o_ptr)
155 {
156     /* Check if completed a quest */
157     for (QUEST_IDX i = 0; i < max_q_idx; i++) {
158         if ((quest[i].type == QUEST_TYPE_FIND_ARTIFACT) && (quest[i].status == QUEST_STATUS_TAKEN) && (quest[i].k_idx == o_ptr->name1)) {
159             complete_quest(player_ptr, i);
160         }
161     }
162 }
163
164 /*!
165  * @brief クエストの導入メッセージを表示する / Discover quest
166  * @param q_idx 開始されたクエストのID
167  */
168 void quest_discovery(QUEST_IDX q_idx)
169 {
170     quest_type *q_ptr = &quest[q_idx];
171     monster_race *r_ptr = &r_info[q_ptr->r_idx];
172     MONSTER_NUMBER q_num = q_ptr->max_num;
173
174     if (!q_idx)
175         return;
176
177     GAME_TEXT name[MAX_NLEN];
178     strcpy(name, (r_ptr->name.c_str()));
179
180     msg_print(find_quest[rand_range(0, 4)]);
181     msg_print(NULL);
182
183     if (q_num != 1) {
184 #ifdef JP
185 #else
186         plural_aux(name);
187 #endif
188         msg_format(_("注意しろ!この階は%d体の%sによって守られている!", "Be warned, this level is guarded by %d %s!"), q_num, name);
189         return;
190     }
191
192     bool is_random_quest_skipped = (r_ptr->flags1 & RF1_UNIQUE) != 0;
193     is_random_quest_skipped &= r_ptr->max_num == 0;
194     if (!is_random_quest_skipped) {
195         msg_format(_("注意せよ!この階は%sによって守られている!", "Beware, this level is protected by %s!"), name);
196         return;
197     }
198
199     msg_print(_("この階は以前は誰かによって守られていたようだ…。", "It seems that this level was protected by someone before..."));
200     record_quest_final_status(q_ptr, 0, QUEST_STATUS_FINISHED);
201 }
202
203 /*!
204  * @brief 新しく入ったダンジョンの階層に固定されている一般のクエストを探し出しIDを返す。
205  * / Hack -- Check if a level is a "quest" level
206  * @param player_ptr プレーヤーへの参照ポインタ
207  * @param level 検索対象になる階
208  * @return クエストIDを返す。該当がない場合0を返す。
209  */
210 QUEST_IDX quest_number(player_type *player_ptr, DEPTH level)
211 {
212     floor_type *floor_ptr = player_ptr->current_floor_ptr;
213     if (floor_ptr->inside_quest)
214         return (floor_ptr->inside_quest);
215
216     for (QUEST_IDX i = 0; i < max_q_idx; i++) {
217         if (quest[i].status != QUEST_STATUS_TAKEN)
218             continue;
219
220         if ((quest[i].type == QUEST_TYPE_KILL_LEVEL) && !(quest[i].flags & QUEST_FLAG_PRESET) && (quest[i].level == level)
221             && (quest[i].dungeon == player_ptr->dungeon_idx))
222             return i;
223     }
224
225     return random_quest_number(player_ptr, level);
226 }
227
228 /*!
229  * @brief 新しく入ったダンジョンの階層に固定されているランダムクエストを探し出しIDを返す。
230  * @param player_ptr プレーヤーへの参照ポインタ
231  * @param level 検索対象になる階
232  * @return クエストIDを返す。該当がない場合0を返す。
233  */
234 QUEST_IDX random_quest_number(player_type *player_ptr, DEPTH level)
235 {
236     if (player_ptr->dungeon_idx != DUNGEON_ANGBAND)
237         return 0;
238
239     for (QUEST_IDX i = MIN_RANDOM_QUEST; i < MAX_RANDOM_QUEST + 1; i++) {
240         if ((quest[i].type == QUEST_TYPE_RANDOM) && (quest[i].status == QUEST_STATUS_TAKEN) && (quest[i].level == level)
241             && (quest[i].dungeon == DUNGEON_ANGBAND)) {
242             return i;
243         }
244     }
245
246     return 0;
247 }
248
249 /*!
250  * @brief クエスト階層から離脱する際の処理
251  * @param player_ptr プレーヤーへの参照ポインタ
252  * @return なし
253  */
254 void leave_quest_check(player_type *player_ptr)
255 {
256     leaving_quest = player_ptr->current_floor_ptr->inside_quest;
257     if (!leaving_quest)
258         return;
259
260     quest_type *const q_ptr = &quest[leaving_quest];
261     bool is_one_time_quest = ((q_ptr->flags & QUEST_FLAG_ONCE) || (q_ptr->type == QUEST_TYPE_RANDOM)) && (q_ptr->status == QUEST_STATUS_TAKEN);
262     if (!is_one_time_quest)
263         return;
264
265     record_quest_final_status(q_ptr, player_ptr->lev, QUEST_STATUS_FAILED);
266
267     /* Additional settings */
268     switch (q_ptr->type) {
269     case QUEST_TYPE_TOWER:
270         quest[QUEST_TOWER1].status = QUEST_STATUS_FAILED;
271         quest[QUEST_TOWER1].complev = player_ptr->lev;
272         break;
273     case QUEST_TYPE_FIND_ARTIFACT:
274         a_info[q_ptr->k_idx].gen_flags.reset(TRG::QUESTITEM);
275         break;
276     case QUEST_TYPE_RANDOM:
277         r_info[q_ptr->r_idx].flags1 &= ~(RF1_QUESTOR);
278
279         /* Floor of random quest will be blocked */
280         prepare_change_floor_mode(player_ptr, CFM_NO_RETURN);
281         break;
282     }
283
284     /* Record finishing a quest */
285     if (q_ptr->type == QUEST_TYPE_RANDOM) {
286         if (record_rand_quest)
287             exe_write_diary(player_ptr, DIARY_RAND_QUEST_F, leaving_quest, NULL);
288         return;
289     }
290
291     if (record_fix_quest)
292         exe_write_diary(player_ptr, DIARY_FIX_QUEST_F, leaving_quest, NULL);
293 }
294
295 /*!
296  * @brief 「塔」クエストの各階層から離脱する際の処理
297  * @return なし
298  */
299 void leave_tower_check(player_type *player_ptr)
300 {
301     leaving_quest = player_ptr->current_floor_ptr->inside_quest;
302     bool is_leaving_from_tower = leaving_quest != 0;
303     is_leaving_from_tower &= quest[leaving_quest].type == QUEST_TYPE_TOWER;
304     is_leaving_from_tower &= quest[QUEST_TOWER1].status != QUEST_STATUS_COMPLETED;
305     if (!is_leaving_from_tower)
306         return;
307     if (quest[leaving_quest].type != QUEST_TYPE_TOWER)
308         return;
309
310     quest[QUEST_TOWER1].status = QUEST_STATUS_FAILED;
311     quest[QUEST_TOWER1].complev = player_ptr->lev;
312     update_playtime();
313     quest[QUEST_TOWER1].comptime = current_world_ptr->play_time;
314 }
315
316 /*!
317  * @brief クエスト入り口にプレイヤーが乗った際の処理 / Do building commands
318  * @param player_ptr プレーヤーへの参照ポインタ
319  * @return なし
320  */
321 void do_cmd_quest(player_type *player_ptr)
322 {
323     if (player_ptr->wild_mode)
324         return;
325
326     take_turn(player_ptr, 100);
327
328     if (!cave_has_flag_bold(player_ptr->current_floor_ptr, player_ptr->y, player_ptr->x, FF_QUEST_ENTER)) {
329         msg_print(_("ここにはクエストの入口はない。", "You see no quest level here."));
330         return;
331     }
332
333     msg_print(_("ここにはクエストへの入口があります。", "There is an entry of a quest."));
334     if (!get_check(_("クエストに入りますか?", "Do you enter? ")))
335         return;
336     if (is_echizen(player_ptr))
337         msg_print(_("『とにかく入ってみようぜぇ。』", "\"Let's go in anyway.\""));
338     else if (player_ptr->pseikaku == PERSONALITY_CHARGEMAN)
339         msg_print(_("『全滅してやるぞ!』", "\"I'll annihilate THEM!\""));
340
341     /* Player enters a new quest */
342     player_ptr->oldpy = 0;
343     player_ptr->oldpx = 0;
344
345     leave_quest_check(player_ptr);
346
347     if (quest[player_ptr->current_floor_ptr->inside_quest].type != QUEST_TYPE_RANDOM)
348         player_ptr->current_floor_ptr->dun_level = 1;
349     player_ptr->current_floor_ptr->inside_quest = player_ptr->current_floor_ptr->grid_array[player_ptr->y][player_ptr->x].special;
350
351     player_ptr->leaving = TRUE;
352 }