OSDN Git Service

Merge pull request #1069 from sikabane-works/release/3.0.0Alpha22
[hengbandforosx/hengbandosx.git] / src / dungeon / quest-completion-checker.cpp
1 #include "dungeon/quest-completion-checker.h"
2 #include "core/player-update-types.h"
3 #include "dungeon/quest.h"
4 #include "effect/effect-characteristics.h"
5 #include "floor/cave.h"
6 #include "floor/floor-object.h"
7 #include "floor/floor-util.h"
8 #include "grid/feature-flag-types.h"
9 #include "grid/feature.h"
10 #include "grid/grid.h"
11 #include "monster/monster-info.h"
12 #include "object-enchant/item-apply-magic.h"
13 #include "system/floor-type-definition.h"
14 #include "system/monster-type-definition.h"
15 #include "system/object-type-definition.h"
16 #include "system/player-type-definition.h"
17 #include "view/display-messages.h"
18
19 /*!
20  * @brief 現在フロアに残っている敵モンスターの数を返す /
21  * @return 現在の敵モンスターの数
22  */
23 static MONSTER_NUMBER count_all_hostile_monsters(floor_type *floor_ptr)
24 {
25     MONSTER_NUMBER number_mon = 0;
26     for (POSITION x = 0; x < floor_ptr->width; ++x) {
27         for (POSITION y = 0; y < floor_ptr->height; ++y) {
28             MONSTER_IDX m_idx = floor_ptr->grid_array[y][x].m_idx;
29             if (m_idx > 0 && is_hostile(&floor_ptr->m_list[m_idx]))
30                 ++number_mon;
31         }
32     }
33
34     return number_mon;
35 }
36
37 /*!
38  * @brief 特定の敵を倒した際にクエスト達成処理 /
39  * Check for "Quest" completion when a quest monster is killed or charmed.
40  * @param player_ptr プレーヤーへの参照ポインタ
41  * @param m_ptr 撃破したモンスターの構造体参照ポインタ
42  */
43 void check_quest_completion(player_type *player_ptr, monster_type *m_ptr)
44 {
45     POSITION y = m_ptr->fy;
46     POSITION x = m_ptr->fx;
47     floor_type *floor_ptr = player_ptr->current_floor_ptr;
48     QUEST_IDX quest_num = floor_ptr->inside_quest;
49     if (!quest_num) {
50         QUEST_IDX i;
51         for (i = max_q_idx - 1; i > 0; i--) {
52             quest_type *const q_ptr = &quest[i];
53             if (q_ptr->status != QUEST_STATUS_TAKEN)
54                 continue;
55
56             if (q_ptr->flags & QUEST_FLAG_PRESET)
57                 continue;
58
59             if ((q_ptr->level != floor_ptr->dun_level) && (q_ptr->type != QUEST_TYPE_KILL_ANY_LEVEL))
60                 continue;
61
62             if ((q_ptr->type == QUEST_TYPE_FIND_ARTIFACT) || (q_ptr->type == QUEST_TYPE_FIND_EXIT))
63                 continue;
64
65             if ((q_ptr->type == QUEST_TYPE_KILL_NUMBER) || (q_ptr->type == QUEST_TYPE_TOWER) || (q_ptr->type == QUEST_TYPE_KILL_ALL))
66                 break;
67
68             if (((q_ptr->type == QUEST_TYPE_KILL_LEVEL) || (q_ptr->type == QUEST_TYPE_KILL_ANY_LEVEL) || (q_ptr->type == QUEST_TYPE_RANDOM))
69                 && (q_ptr->r_idx == m_ptr->r_idx))
70                 break;
71         }
72
73         quest_num = i;
74     }
75
76     bool create_stairs = FALSE;
77     bool reward = FALSE;
78     if (quest_num && (quest[quest_num].status == QUEST_STATUS_TAKEN)) {
79         quest_type *const q_ptr = &quest[quest_num];
80         switch (q_ptr->type) {
81         case QUEST_TYPE_KILL_NUMBER: {
82             q_ptr->cur_num++;
83             if (q_ptr->cur_num >= q_ptr->num_mon) {
84                 complete_quest(player_ptr, quest_num);
85                 q_ptr->cur_num = 0;
86             }
87
88             break;
89         }
90         case QUEST_TYPE_KILL_ALL: {
91             if (!is_hostile(m_ptr) || count_all_hostile_monsters(floor_ptr) != 1)
92                 break;
93
94             if (q_ptr->flags & QUEST_FLAG_SILENT) {
95                 q_ptr->status = QUEST_STATUS_FINISHED;
96             } else {
97                 complete_quest(player_ptr, quest_num);
98             }
99
100             break;
101         }
102         case QUEST_TYPE_KILL_LEVEL:
103         case QUEST_TYPE_RANDOM: {
104             if (q_ptr->r_idx != m_ptr->r_idx)
105                 break;
106
107             q_ptr->cur_num++;
108             if (q_ptr->cur_num < q_ptr->max_num)
109                 break;
110
111             complete_quest(player_ptr, quest_num);
112             if (!(q_ptr->flags & QUEST_FLAG_PRESET)) {
113                 create_stairs = TRUE;
114                 floor_ptr->inside_quest = 0;
115             }
116
117             if ((quest_num == QUEST_OBERON) || (quest_num == QUEST_SERPENT))
118                 q_ptr->status = QUEST_STATUS_FINISHED;
119
120             if (q_ptr->type == QUEST_TYPE_RANDOM) {
121                 reward = TRUE;
122                 q_ptr->status = QUEST_STATUS_FINISHED;
123             }
124
125             break;
126         }
127         case QUEST_TYPE_KILL_ANY_LEVEL: {
128             q_ptr->cur_num++;
129             if (q_ptr->cur_num >= q_ptr->max_num) {
130                 complete_quest(player_ptr, quest_num);
131                 q_ptr->cur_num = 0;
132             }
133
134             break;
135         }
136         case QUEST_TYPE_TOWER: {
137             if (!is_hostile(m_ptr))
138                 break;
139
140             if (count_all_hostile_monsters(floor_ptr) == 1) {
141                 q_ptr->status = QUEST_STATUS_STAGE_COMPLETED;
142
143                 if ((quest[QUEST_TOWER1].status == QUEST_STATUS_STAGE_COMPLETED) && (quest[QUEST_TOWER2].status == QUEST_STATUS_STAGE_COMPLETED)
144                     && (quest[QUEST_TOWER3].status == QUEST_STATUS_STAGE_COMPLETED)) {
145
146                     complete_quest(player_ptr, QUEST_TOWER1);
147                 }
148             }
149
150             break;
151         }
152         }
153     }
154
155     if (create_stairs) {
156         POSITION ny, nx;
157         while (cave_has_flag_bold(floor_ptr, y, x, FF_PERMANENT) || !floor_ptr->grid_array[y][x].o_idx_list.empty() || (floor_ptr->grid_array[y][x].info & CAVE_OBJECT)) {
158             scatter(player_ptr, &ny, &nx, y, x, 1, PROJECT_NONE);
159             y = ny;
160             x = nx;
161         }
162
163         msg_print(_("魔法の階段が現れた...", "A magical staircase appears..."));
164         cave_set_feat(player_ptr, y, x, feat_down_stair);
165         player_ptr->update |= PU_FLOW;
166     }
167
168     if (!reward)
169         return;
170
171     object_type forge;
172     object_type *o_ptr;
173     for (int i = 0; i < (floor_ptr->dun_level / 15) + 1; i++) {
174         o_ptr = &forge;
175         o_ptr->wipe();
176         make_object(player_ptr, o_ptr, AM_GOOD | AM_GREAT);
177         (void)drop_near(player_ptr, o_ptr, -1, y, x);
178     }
179 }