OSDN Git Service

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