OSDN Git Service

[Refactor] 明かり・闇フラグの定義に合わせる
[hengbandforosx/hengbandosx.git] / src / monster-floor / monster-remover.cpp
1 #include "monster-floor/monster-remover.h"
2 #include "core/player-update-types.h"
3 #include "core/stuff-handler.h"
4 #include "floor/cave.h"
5 #include "floor/floor-object.h"
6 #include "grid/grid.h"
7 #include "monster-race/monster-race.h"
8 #include "monster-race/race-brightness-mask.h"
9 #include "monster-race/race-flags2.h"
10 #include "monster-race/race-flags7.h"
11 #include "monster-race/race-indice-types.h"
12 #include "monster/monster-info.h"
13 #include "monster/monster-status-setter.h"
14 #include "monster/monster-status.h"
15 #include "system/floor-type-definition.h"
16 #include "system/grid-type-definition.h"
17 #include "system/item-entity.h"
18 #include "system/monster-entity.h"
19 #include "system/monster-race-info.h"
20 #include "system/player-type-definition.h"
21 #include "target/target-checker.h"
22
23 /*!
24  * @brief モンスター配列からモンスターを消去する / Delete a monster by index.
25  * @param i 消去するモンスターのID
26  * @details
27  * モンスターを削除するとそのモンスターが拾っていたアイテムも同時に削除される。 /
28  * When a monster is deleted, all of its objects are deleted.
29  */
30 void delete_monster_idx(PlayerType *player_ptr, MONSTER_IDX i)
31 {
32     auto *floor_ptr = player_ptr->current_floor_ptr;
33     auto *m_ptr = &floor_ptr->m_list[i];
34     auto *r_ptr = &monraces_info[m_ptr->r_idx];
35
36     POSITION y = m_ptr->fy;
37     POSITION x = m_ptr->fx;
38
39     m_ptr->get_real_r_ref().cur_num--;
40     if (r_ptr->flags2 & (RF2_MULTIPLY)) {
41         floor_ptr->num_repro--;
42     }
43
44     if (m_ptr->is_asleep()) {
45         (void)set_monster_csleep(player_ptr, i, 0);
46     }
47     if (m_ptr->is_accelerated()) {
48         (void)set_monster_fast(player_ptr, i, 0);
49     }
50     if (m_ptr->is_decelerated()) {
51         (void)set_monster_slow(player_ptr, i, 0);
52     }
53     if (m_ptr->is_stunned()) {
54         (void)set_monster_stunned(player_ptr, i, 0);
55     }
56     if (m_ptr->is_confused()) {
57         (void)set_monster_confused(player_ptr, i, 0);
58     }
59     if (m_ptr->is_fearful()) {
60         (void)set_monster_monfear(player_ptr, i, 0);
61     }
62     if (m_ptr->is_invulnerable()) {
63         (void)set_monster_invulner(player_ptr, i, 0, false);
64     }
65
66     if (i == target_who) {
67         target_who = 0;
68     }
69
70     if (i == player_ptr->health_who) {
71         health_track(player_ptr, 0);
72     }
73
74     if (player_ptr->pet_t_m_idx == i) {
75         player_ptr->pet_t_m_idx = 0;
76     }
77     if (player_ptr->riding_t_m_idx == i) {
78         player_ptr->riding_t_m_idx = 0;
79     }
80     if (player_ptr->riding == i) {
81         player_ptr->riding = 0;
82     }
83
84     floor_ptr->grid_array[y][x].m_idx = 0;
85     for (auto it = m_ptr->hold_o_idx_list.begin(); it != m_ptr->hold_o_idx_list.end();) {
86         const OBJECT_IDX this_o_idx = *it++;
87         delete_object_idx(player_ptr, this_o_idx);
88     }
89
90     // 召喚元のモンスターが消滅した時は、召喚されたモンスターのparent_m_idxが
91     // 召喚されたモンスター自身のm_idxを指すようにする
92     for (MONSTER_IDX child_m_idx = 1; child_m_idx < floor_ptr->m_max; child_m_idx++) {
93         MonsterEntity *child_m_ptr = &floor_ptr->m_list[child_m_idx];
94         if (MonsterRace(child_m_ptr->r_idx).is_valid() && child_m_ptr->parent_m_idx == i) {
95             child_m_ptr->parent_m_idx = child_m_idx;
96         }
97     }
98
99     *m_ptr = {};
100     floor_ptr->m_cnt--;
101     lite_spot(player_ptr, y, x);
102     if (r_ptr->brightness_flags.has_any_of(ld_mask)) {
103         player_ptr->update |= (PU_MON_LITE);
104     }
105 }
106
107 /*!
108  * @brief プレイヤーのフロア離脱に伴う全モンスター配列の消去 / Delete/Remove all the monsters when the player leaves the level
109  * @param player_ptr プレイヤーへの参照ポインタ
110  * @details
111  * This is an efficient method of simulating multiple calls to the
112  * "delete_monster()" function, with no visual effects.
113  */
114 void wipe_monsters_list(PlayerType *player_ptr)
115 {
116     if (!monraces_info[MonsterRaceId::BANORLUPART].max_num) {
117         if (monraces_info[MonsterRaceId::BANOR].max_num) {
118             monraces_info[MonsterRaceId::BANOR].max_num = 0;
119             monraces_info[MonsterRaceId::BANOR].r_pkills++;
120             monraces_info[MonsterRaceId::BANOR].r_akills++;
121             if (monraces_info[MonsterRaceId::BANOR].r_tkills < MAX_SHORT) {
122                 monraces_info[MonsterRaceId::BANOR].r_tkills++;
123             }
124         }
125
126         if (monraces_info[MonsterRaceId::LUPART].max_num) {
127             monraces_info[MonsterRaceId::LUPART].max_num = 0;
128             monraces_info[MonsterRaceId::LUPART].r_pkills++;
129             monraces_info[MonsterRaceId::LUPART].r_akills++;
130             if (monraces_info[MonsterRaceId::LUPART].r_tkills < MAX_SHORT) {
131                 monraces_info[MonsterRaceId::LUPART].r_tkills++;
132             }
133         }
134     }
135
136     auto *floor_ptr = player_ptr->current_floor_ptr;
137     for (int i = floor_ptr->m_max - 1; i >= 1; i--) {
138         auto *m_ptr = &floor_ptr->m_list[i];
139         if (!m_ptr->is_valid()) {
140             continue;
141         }
142
143         floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].m_idx = 0;
144         *m_ptr = {};
145     }
146
147     /*
148      * Wiping racial counters of all monsters and incrementing of racial
149      * counters of monsters in party_mon[] are required to prevent multiple
150      * generation of unique monster who is the minion of player.
151      */
152     for (auto &[r_idx, r_ref] : monraces_info) {
153         r_ref.cur_num = 0;
154     }
155
156     floor_ptr->m_max = 1;
157     floor_ptr->m_cnt = 0;
158     for (int i = 0; i < MAX_MTIMED; i++) {
159         floor_ptr->mproc_max[i] = 0;
160     }
161
162     floor_ptr->num_repro = 0;
163     target_who = 0;
164     player_ptr->pet_t_m_idx = 0;
165     player_ptr->riding_t_m_idx = 0;
166     health_track(player_ptr, 0);
167 }
168
169 /*!
170  * @brief 指定位置に存在するモンスターを削除する / Delete the monster, if any, at a given location
171  * @param player_ptr プレイヤーへの参照ポインタ
172  * @param x 削除位置x座標
173  * @param y 削除位置y座標
174  */
175 void delete_monster(PlayerType *player_ptr, POSITION y, POSITION x)
176 {
177     grid_type *g_ptr;
178     auto *floor_ptr = player_ptr->current_floor_ptr;
179     if (!in_bounds(floor_ptr, y, x)) {
180         return;
181     }
182
183     g_ptr = &floor_ptr->grid_array[y][x];
184     if (g_ptr->m_idx) {
185         delete_monster_idx(player_ptr, g_ptr->m_idx);
186     }
187 }