OSDN Git Service

[Refactor] Grid::has_monster()の定義
[hengbandforosx/hengbandosx.git] / src / spell-kind / earthquake.cpp
1 #include "spell-kind/earthquake.h"
2 #include "core/window-redrawer.h"
3 #include "dungeon/dungeon-flag-types.h"
4 #include "dungeon/quest.h"
5 #include "floor/cave.h"
6 #include "floor/floor-object.h"
7 #include "floor/geometry.h"
8 #include "game-option/play-record-options.h"
9 #include "game-option/text-display-options.h"
10 #include "grid/feature-flag-types.h"
11 #include "grid/feature.h"
12 #include "grid/grid.h"
13 #include "grid/stair.h"
14 #include "io/write-diary.h"
15 #include "mind/mind-ninja.h"
16 #include "monster-floor/monster-lite.h"
17 #include "monster-race/monster-race.h"
18 #include "monster/monster-describer.h"
19 #include "monster/monster-description-types.h"
20 #include "monster/monster-info.h"
21 #include "monster/monster-status-setter.h"
22 #include "monster/monster-update.h"
23 #include "monster/smart-learn-types.h"
24 #include "player/player-damage.h"
25 #include "player/player-move.h"
26 #include "player/player-status-flags.h"
27 #include "player/special-defense-types.h"
28 #include "status/bad-status-setter.h"
29 #include "system/dungeon-info.h"
30 #include "system/floor-type-definition.h"
31 #include "system/grid-type-definition.h"
32 #include "system/monster-entity.h"
33 #include "system/monster-race-info.h"
34 #include "system/player-type-definition.h"
35 #include "system/redrawing-flags-updater.h"
36 #include "system/terrain-type-definition.h"
37 #include "util/bit-flags-calculator.h"
38 #include "view/display-messages.h"
39
40 /*!
41  * @brief 地震処理
42  * Induce an "earthquake" of the given radius at the given location.
43  * @param player_ptrプレイヤーへの参照ポインタ
44  * @param cy 中心Y座標
45  * @param cx 中心X座標
46  * @param r 効果半径
47  * @param m_idx 地震を起こしたモンスターID(0ならばプレイヤー)
48  * @return 効力があった場合TRUEを返す
49  */
50 bool earthquake(PlayerType *player_ptr, POSITION cy, POSITION cx, POSITION r, MONSTER_IDX m_idx)
51 {
52     auto *floor_ptr = player_ptr->current_floor_ptr;
53     if ((floor_ptr->is_in_quest() && QuestType::is_fixed(floor_ptr->quest_number)) || !floor_ptr->dun_level) {
54         return false;
55     }
56
57     if (r > 12) {
58         r = 12;
59     }
60
61     bool map[32][32]{};
62     for (auto y = 0; y < 32; y++) {
63         for (auto x = 0; x < 32; x++) {
64             map[y][x] = false;
65         }
66     }
67
68     auto damage = 0;
69     auto hurt = false;
70     for (auto dy = -r; dy <= r; dy++) {
71         for (auto dx = -r; dx <= r; dx++) {
72             const Pos2D pos(cy + dy, cx + dx);
73             if (!in_bounds(floor_ptr, pos.y, pos.x)) {
74                 continue;
75             }
76
77             if (distance(cy, cx, pos.y, pos.x) > r) {
78                 continue;
79             }
80
81             auto &grid = floor_ptr->get_grid(pos);
82             grid.info &= ~(CAVE_ROOM | CAVE_ICKY | CAVE_UNSAFE);
83             grid.info &= ~(CAVE_GLOW | CAVE_MARK | CAVE_KNOWN);
84             if (!dx && !dy) {
85                 continue;
86             }
87
88             if (randint0(100) < 85) {
89                 continue;
90             }
91
92             map[16 + pos.y - cy][16 + pos.x - cx] = true;
93             if (player_ptr->is_located_at(pos)) {
94                 hurt = true;
95             }
96         }
97     }
98
99     auto sn = 0;
100     Pos2D p_pos_new(0, 0); // 落石を避けた後のプレイヤー座標
101     if (hurt && !has_pass_wall(player_ptr) && !has_kill_wall(player_ptr)) {
102         for (auto i = 0; i < 8; i++) {
103             auto y = player_ptr->y + ddy_ddd[i];
104             auto x = player_ptr->x + ddx_ddd[i];
105             if (!is_cave_empty_bold(player_ptr, y, x)) {
106                 continue;
107             }
108
109             if (map[16 + y - cy][16 + x - cx]) {
110                 continue;
111             }
112
113             if (floor_ptr->grid_array[y][x].has_monster()) {
114                 continue;
115             }
116
117             sn++;
118             if (randint0(sn) > 0) {
119                 continue;
120             }
121
122             p_pos_new = { y, x };
123         }
124
125         constexpr static auto msgs = {
126             _("ダンジョンの壁が崩れた!", "The dungeon's ceiling collapses!"),
127             _("ダンジョンの床が不自然にねじ曲がった!", "The dungeon's floor twists in an unnatural way!"),
128             _("ダンジョンが揺れた!崩れた岩が頭に降ってきた!", "The dungeon quakes!  You are pummeled with debris!"),
129         };
130         msg_print(rand_choice(msgs));
131
132         if (!sn) {
133             msg_print(_("あなたはひどい怪我を負った!", "You are severely crushed!"));
134             damage = 200;
135         } else {
136             constexpr std::array<std::pair<bool, std::string_view>, 3> candidates = { {
137                 { false, _("降り注ぐ岩をうまく避けた!", "You nimbly dodge the blast!") },
138                 { true, _("岩石があなたに直撃した!", "You are bashed by rubble!") },
139                 { true, _("あなたは床と壁との間に挟まれてしまった!", "You are crushed between the floor and ceiling!") },
140             } };
141
142             const auto &[is_damaged, msg] = rand_choice(candidates);
143
144             msg_print(msg);
145             if (is_damaged) {
146                 damage = damroll(10, 4);
147                 BadStatusSetter(player_ptr).mod_stun(randint1(50));
148             }
149
150             (void)move_player_effect(player_ptr, p_pos_new.y, p_pos_new.x, MPE_DONT_PICKUP);
151         }
152
153         map[16 + player_ptr->y - cy][16 + player_ptr->x - cx] = false;
154         if (damage) {
155             std::string killer;
156
157             if (m_idx) {
158                 auto *m_ptr = &floor_ptr->m_list[m_idx];
159                 const auto m_name = monster_desc(player_ptr, m_ptr, MD_WRONGDOER_NAME);
160                 killer = format(_("%sの起こした地震", "an earthquake caused by %s"), m_name.data());
161             } else {
162                 killer = _("地震", "an earthquake");
163             }
164
165             take_hit(player_ptr, DAMAGE_ATTACK, damage, killer);
166         }
167     }
168
169     for (auto dy = -r; dy <= r; dy++) {
170         for (auto dx = -r; dx <= r; dx++) {
171             const Pos2D pos(cy + dy, cx + dx);
172             if (!map[16 + pos.y - cy][16 + pos.x - cx]) {
173                 continue;
174             }
175
176             auto &grid = floor_ptr->get_grid(pos);
177             if (grid.m_idx == player_ptr->riding) {
178                 continue;
179             }
180
181             if (!grid.has_monster()) {
182                 continue;
183             }
184
185             auto *m_ptr = &floor_ptr->m_list[grid.m_idx];
186             auto *r_ptr = &m_ptr->get_monrace();
187             if (r_ptr->misc_flags.has(MonsterMiscType::QUESTOR)) {
188                 map[16 + pos.y - cy][16 + pos.x - cx] = false;
189                 continue;
190             }
191
192             if (r_ptr->feature_flags.has(MonsterFeatureType::KILL_WALL) || r_ptr->feature_flags.has(MonsterFeatureType::PASS_WALL)) {
193                 continue;
194             }
195
196             sn = 0;
197             if (r_ptr->behavior_flags.has_not(MonsterBehaviorType::NEVER_MOVE)) {
198                 for (DIRECTION i = 0; i < 8; i++) {
199                     const Pos2D pos_neighbor(pos.y + ddy_ddd[i], pos.x + ddx_ddd[i]);
200                     if (!is_cave_empty_bold(player_ptr, pos_neighbor.y, pos_neighbor.x)) {
201                         continue;
202                     }
203
204                     const auto &grid_neighbor = floor_ptr->get_grid(pos_neighbor);
205                     if (grid_neighbor.is_rune_protection()) {
206                         continue;
207                     }
208
209                     if (grid_neighbor.is_rune_explosion()) {
210                         continue;
211                     }
212
213                     if (pattern_tile(floor_ptr, pos_neighbor.y, pos_neighbor.x)) {
214                         continue;
215                     }
216
217                     if (map[16 + pos_neighbor.y - cy][16 + pos_neighbor.x - cx]) {
218                         continue;
219                     }
220
221                     if (grid_neighbor.has_monster()) {
222                         continue;
223                     }
224
225                     if (player_ptr->is_located_at(pos)) {
226                         continue;
227                     }
228
229                     sn++;
230
231                     if (randint0(sn) > 0) {
232                         continue;
233                     }
234
235                     p_pos_new = pos_neighbor;
236                 }
237             }
238
239             const auto m_name = monster_desc(player_ptr, m_ptr, 0);
240             if (!ignore_unview || is_seen(player_ptr, m_ptr)) {
241                 msg_format(_("%s^は苦痛で泣きわめいた!", "%s^ wails out in pain!"), m_name.data());
242             }
243
244             damage = (sn ? damroll(4, 8) : (m_ptr->hp + 1));
245             (void)set_monster_csleep(player_ptr, grid.m_idx, 0);
246             m_ptr->hp -= damage;
247             if (m_ptr->hp < 0) {
248                 if (!ignore_unview || is_seen(player_ptr, m_ptr)) {
249                     msg_format(_("%s^は岩石に埋もれてしまった!", "%s^ is embedded in the rock!"), m_name.data());
250                 }
251
252                 if (grid.has_monster()) {
253                     const auto &m_ref = floor_ptr->m_list[grid.m_idx];
254                     if (record_named_pet && m_ref.is_named_pet()) {
255                         const auto m2_name = monster_desc(player_ptr, m_ptr, MD_INDEF_VISIBLE);
256                         exe_write_diary(player_ptr, DiaryKind::NAMED_PET, RECORD_NAMED_PET_EARTHQUAKE, m2_name);
257                     }
258                 }
259
260                 delete_monster(player_ptr, pos.y, pos.x);
261                 sn = 0;
262             }
263
264             if (sn == 0) {
265                 continue;
266             }
267
268             const auto m_idx_aux = grid.m_idx;
269             grid.m_idx = 0;
270             floor_ptr->get_grid(p_pos_new).m_idx = m_idx_aux;
271             m_ptr->fy = p_pos_new.y;
272             m_ptr->fx = p_pos_new.x;
273             update_monster(player_ptr, m_idx_aux, true);
274             lite_spot(player_ptr, pos.y, pos.x);
275             lite_spot(player_ptr, p_pos_new.y, p_pos_new.x);
276         }
277     }
278
279     clear_mon_lite(floor_ptr);
280     for (auto dy = -r; dy <= r; dy++) {
281         for (auto dx = -r; dx <= r; dx++) {
282             auto yy = cy + dy;
283             auto xx = cx + dx;
284             if (!map[16 + yy - cy][16 + xx - cx]) {
285                 continue;
286             }
287
288             if (!cave_valid_bold(floor_ptr, yy, xx)) {
289                 continue;
290             }
291
292             delete_all_items_from_floor(player_ptr, yy, xx);
293             int t = cave_has_flag_bold(floor_ptr, yy, xx, TerrainCharacteristics::PROJECT) ? randint0(100) : 200;
294             if (t < 20) {
295                 cave_set_feat(player_ptr, yy, xx, feat_granite);
296                 continue;
297             }
298
299             if (t < 70) {
300                 cave_set_feat(player_ptr, yy, xx, feat_quartz_vein);
301                 continue;
302             }
303
304             if (t < 100) {
305                 cave_set_feat(player_ptr, yy, xx, feat_magma_vein);
306                 continue;
307             }
308
309             cave_set_feat(player_ptr, yy, xx, rand_choice(feat_ground_type));
310         }
311     }
312
313     for (auto dy = -r; dy <= r; dy++) {
314         for (auto dx = -r; dx <= r; dx++) {
315             const Pos2D pos(cy + dy, cx + dx);
316             if (!in_bounds(floor_ptr, pos.y, pos.x)) {
317                 continue;
318             }
319
320             if (distance(cy, cx, pos.y, pos.x) > r) {
321                 continue;
322             }
323
324             auto &grid = floor_ptr->get_grid(pos);
325             if (grid.is_mirror()) {
326                 grid.info |= CAVE_GLOW;
327                 continue;
328             }
329
330             if (floor_ptr->get_dungeon_definition().flags.has(DungeonFeatureType::DARKNESS)) {
331                 continue;
332             }
333
334             for (auto j = 0; j < 9; j++) {
335                 const Pos2D pos_neighbor(pos.y + ddy_ddd[j], pos.x + ddx_ddd[j]);
336                 if (!in_bounds2(floor_ptr, pos_neighbor.y, pos_neighbor.x)) {
337                     continue;
338                 }
339
340                 const auto &grid_neighbor = floor_ptr->get_grid(pos_neighbor);
341                 if (grid_neighbor.get_terrain_mimic().flags.has(TerrainCharacteristics::GLOW)) {
342                     grid.info |= CAVE_GLOW;
343                     break;
344                 }
345             }
346         }
347     }
348
349     auto &rfu = RedrawingFlagsUpdater::get_instance();
350     static constexpr auto flags_srf = {
351         StatusRecalculatingFlag::UN_VIEW,
352         StatusRecalculatingFlag::UN_LITE,
353         StatusRecalculatingFlag::VIEW,
354         StatusRecalculatingFlag::LITE,
355         StatusRecalculatingFlag::FLOW,
356         StatusRecalculatingFlag::MONSTER_LITE,
357         StatusRecalculatingFlag::MONSTER_STATUSES,
358     };
359     rfu.set_flags(flags_srf);
360     static constexpr auto flags_mwrf = {
361         MainWindowRedrawingFlag::HEALTH,
362         MainWindowRedrawingFlag::UHEALTH,
363         MainWindowRedrawingFlag::MAP,
364     };
365     rfu.set_flags(flags_mwrf);
366     static constexpr auto flags_swrf = {
367         SubWindowRedrawingFlag::OVERHEAD,
368         SubWindowRedrawingFlag::DUNGEON,
369     };
370     rfu.set_flags(flags_swrf);
371     if (floor_ptr->grid_array[player_ptr->y][player_ptr->x].info & CAVE_GLOW) {
372         set_superstealth(player_ptr, false);
373     }
374
375     return true;
376 }