OSDN Git Service

[Refactor] #39963 Changed header inclusion from realm*.h to realm/realm*.h
[hengband/hengband.git] / src / mspells2.c
1 /*!
2  * @file mspells2.c
3  * @brief モンスター魔法の実装(対モンスター処理) / Monster spells (attack monster)
4  * @date 2014/01/17
5  * @author
6  * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke\n
7  * This software may be copied and distributed for educational, research,\n
8  * and not for profit purposes provided that this copyright and statement\n
9  * are included in all such copies.  Other copyrights may also apply.\n
10  * 2014 Deskull rearranged comment for Doxygen.\n
11  * @details
12  */
13
14 #include "angband.h"
15 #include "util.h"
16 #include "main/sound-definitions-table.h"
17
18 #include "cmd-pet.h"
19 #include "floor.h"
20 #include "grid.h"
21 #include "quest.h"
22 #include "realm/realm-hex.h"
23 #include "player-move.h"
24 #include "player-class.h"
25 #include "monster.h"
26 #include "monster-status.h"
27 #include "monster-spell.h"
28 #include "spells.h"
29 #include "dungeon.h"
30 #include "world.h"
31 #include "view/display-main-window.h"
32
33  /*!
34   * @brief モンスターが敵対モンスターにビームを当てること可能かを判定する /
35   * Determine if a beam spell will hit the target.
36   * @param target_ptr プレーヤーへの参照ポインタ
37   * @param y1 始点のY座標
38   * @param x1 始点のX座標
39   * @param y2 目標のY座標
40   * @param x2 目標のX座標
41   * @param m_ptr 使用するモンスターの構造体参照ポインタ
42   * @return ビームが到達可能ならばTRUEを返す
43   */
44 static bool direct_beam(player_type *target_ptr, POSITION y1, POSITION x1, POSITION y2, POSITION x2, monster_type *m_ptr)
45 {
46         /* Check the projection path */
47         floor_type *floor_ptr = target_ptr->current_floor_ptr;
48         u16b grid_g[512];
49         int grid_n = project_path(target_ptr, grid_g, MAX_RANGE, y1, x1, y2, x2, PROJECT_THRU);
50
51         /* No grid is ever projectable from itself */
52         if (!grid_n) return FALSE;
53
54         bool hit2 = FALSE;
55         POSITION y, x;
56         bool is_friend = is_pet(m_ptr);
57         for (int i = 0; i < grid_n; i++)
58         {
59                 y = GRID_Y(grid_g[i]);
60                 x = GRID_X(grid_g[i]);
61
62                 if (y == y2 && x == x2)
63                         hit2 = TRUE;
64                 else if (is_friend && floor_ptr->grid_array[y][x].m_idx > 0 &&
65                         !are_enemies(target_ptr, m_ptr, &floor_ptr->m_list[floor_ptr->grid_array[y][x].m_idx]))
66                 {
67                         /* Friends don't shoot friends */
68                         return FALSE;
69                 }
70
71                 if (is_friend && player_bold(target_ptr, y, x))
72                         return FALSE;
73         }
74
75         if (!hit2) return FALSE;
76         return TRUE;
77 }
78
79
80 /*!
81  * @brief モンスターが敵対モンスターに直接ブレスを当てることが可能かを判定する /
82  * Determine if a breath will hit the target.
83  * @param y1 始点のY座標
84  * @param x1 始点のX座標
85  * @param y2 目標のY座標
86  * @param x2 目標のX座標
87  * @param rad 半径
88  * @param typ 効果属性ID
89  * @param is_friend TRUEならば、プレイヤーを巻き込む時にブレスの判定をFALSEにする。
90  * @return ブレスを直接当てられるならばTRUEを返す
91  */
92 static bool breath_direct(player_type *master_ptr, POSITION y1, POSITION x1, POSITION y2, POSITION x2, POSITION rad, EFFECT_ID typ, bool is_friend)
93 {
94         BIT_FLAGS flg;
95         switch (typ)
96         {
97         case GF_LITE:
98         case GF_LITE_WEAK:
99                 flg = PROJECT_LOS;
100                 break;
101         case GF_DISINTEGRATE:
102                 flg = PROJECT_DISI;
103                 break;
104         default:
105                 flg = 0;
106                 break;
107         }
108
109         /* Check the projection path */
110         u16b grid_g[512];
111         int grid_n = project_path(master_ptr, grid_g, MAX_RANGE, y1, x1, y2, x2, flg);
112
113         /* Project along the path */
114         int i;
115         POSITION y = y1;
116         POSITION x = x1;
117         for (i = 0; i < grid_n; ++i)
118         {
119                 int ny = GRID_Y(grid_g[i]);
120                 int nx = GRID_X(grid_g[i]);
121
122                 if (flg & PROJECT_DISI)
123                 {
124                         /* Hack -- Balls explode before reaching walls */
125                         if (cave_stop_disintegration(master_ptr->current_floor_ptr, ny, nx)) break;
126                 }
127                 else if (flg & PROJECT_LOS)
128                 {
129                         /* Hack -- Balls explode before reaching walls */
130                         if (!cave_los_bold(master_ptr->current_floor_ptr, ny, nx)) break;
131                 }
132                 else
133                 {
134                         /* Hack -- Balls explode before reaching walls */
135                         if (!cave_have_flag_bold(master_ptr->current_floor_ptr, ny, nx, FF_PROJECT)) break;
136                 }
137
138                 /* Save the "blast epicenter" */
139                 y = ny;
140                 x = nx;
141         }
142
143         grid_n = i;
144
145         bool hit2 = FALSE;
146         bool hityou = FALSE;
147         if (!grid_n)
148         {
149                 if (flg & PROJECT_DISI)
150                 {
151                         if (in_disintegration_range(master_ptr->current_floor_ptr, y1, x1, y2, x2) && (distance(y1, x1, y2, x2) <= rad)) hit2 = TRUE;
152                         if (in_disintegration_range(master_ptr->current_floor_ptr, y1, x1, master_ptr->y, master_ptr->x) && (distance(y1, x1, master_ptr->y, master_ptr->x) <= rad)) hityou = TRUE;
153                 }
154                 else if (flg & PROJECT_LOS)
155                 {
156                         if (los(master_ptr, y1, x1, y2, x2) && (distance(y1, x1, y2, x2) <= rad)) hit2 = TRUE;
157                         if (los(master_ptr, y1, x1, master_ptr->y, master_ptr->x) && (distance(y1, x1, master_ptr->y, master_ptr->x) <= rad)) hityou = TRUE;
158                 }
159                 else
160                 {
161                         if (projectable(master_ptr, y1, x1, y2, x2) && (distance(y1, x1, y2, x2) <= rad)) hit2 = TRUE;
162                         if (projectable(master_ptr, y1, x1, master_ptr->y, master_ptr->x) && (distance(y1, x1, master_ptr->y, master_ptr->x) <= rad)) hityou = TRUE;
163                 }
164         }
165         else
166         {
167                 int grids = 0;
168                 POSITION gx[1024], gy[1024];
169                 POSITION gm[32];
170                 POSITION gm_rad = rad;
171                 breath_shape(master_ptr, grid_g, grid_n, &grids, gx, gy, gm, &gm_rad, rad, y1, x1, y, x, typ);
172
173                 for (i = 0; i < grids; i++)
174                 {
175                         y = gy[i];
176                         x = gx[i];
177                         if ((y == y2) && (x == x2)) hit2 = TRUE;
178                         if (player_bold(master_ptr, y, x)) hityou = TRUE;
179                 }
180         }
181
182         if (!hit2) return FALSE;
183         if (is_friend && hityou) return FALSE;
184
185         return TRUE;
186 }
187
188
189 /*!
190  * @brief モンスターが特殊能力の目標地点を決める処理 /
191  * Get the actual center point of ball spells (rad > 1) (originally from TOband)
192  * @param target_ptr プレーヤーへの参照ポインタ
193  * @param sy 始点のY座標
194  * @param sx 始点のX座標
195  * @param ty 目標Y座標を返す参照ポインタ
196  * @param tx 目標X座標を返す参照ポインタ
197  * @param flg 判定のフラグ配列
198  * @return なし
199  */
200 void get_project_point(player_type *target_ptr, POSITION sy, POSITION sx, POSITION *ty, POSITION *tx, BIT_FLAGS flg)
201 {
202         u16b path_g[128];
203         int path_n = project_path(target_ptr, path_g, MAX_RANGE, sy, sx, *ty, *tx, flg);
204
205         *ty = sy;
206         *tx = sx;
207
208         /* Project along the path */
209         for (int i = 0; i < path_n; i++)
210         {
211                 sy = GRID_Y(path_g[i]);
212                 sx = GRID_X(path_g[i]);
213
214                 /* Hack -- Balls explode before reaching walls */
215                 if (!cave_have_flag_bold(target_ptr->current_floor_ptr, sy, sx, FF_PROJECT)) break;
216
217                 *ty = sy;
218                 *tx = sx;
219         }
220 }
221
222
223 /*!
224  * @brief モンスターが敵モンスターに魔力消去を使うかどうかを返す /
225  * Check should monster cast dispel spell at other monster.
226  * @param target_ptr プレーヤーへの参照ポインタ
227  * @param m_idx 術者のモンスターID
228  * @param t_idx 目標のモンスターID
229  * @return 魔力消去を使うべきならばTRUEを変えす。
230  */
231 static bool dispel_check_monster(player_type *target_ptr, MONSTER_IDX m_idx, MONSTER_IDX t_idx)
232 {
233         monster_type *t_ptr = &target_ptr->current_floor_ptr->m_list[t_idx];
234         if (MON_INVULNER(t_ptr)) return TRUE;
235
236         if ((t_ptr->mspeed < 135) && MON_FAST(t_ptr)) return TRUE;
237
238         /* Riding monster */
239         if ((t_idx == target_ptr->riding) && dispel_check(target_ptr, m_idx)) return TRUE;
240
241         /* No need to cast dispel spell */
242         return FALSE;
243 }
244
245
246 /*!
247  * todo モンスターからモンスターへの呪文なのにplayer_typeが引数になり得るのは間違っている……
248  * @brief モンスターが敵モンスターに特殊能力を使う処理のメインルーチン /
249  * Monster tries to 'cast a spell' (or breath, etc) at another monster.
250  * @param target_ptr プレーヤーへの参照ポインタ
251  * @param m_idx 術者のモンスターID
252  * @return 実際に特殊能力を使った場合TRUEを返す
253  * @details
254  * The player is only disturbed if able to be affected by the spell.
255  */
256 bool monst_spell_monst(player_type *target_ptr, MONSTER_IDX m_idx)
257 {
258         POSITION y = 0, x = 0;
259         int k;
260         MONSTER_IDX target_idx = 0;
261         int thrown_spell;
262         HIT_POINT dam = 0;
263         int start;
264         int plus = 1;
265
266         byte spell[96], num = 0;
267
268         GAME_TEXT m_name[160];
269         GAME_TEXT t_name[160];
270
271 #ifdef JP
272 #else
273
274         char m_poss[160];
275 #endif
276
277         floor_type *floor_ptr = target_ptr->current_floor_ptr;
278         monster_type *m_ptr = &floor_ptr->m_list[m_idx];
279         monster_type *t_ptr = NULL;
280
281         monster_race *r_ptr = &r_info[m_ptr->r_idx];
282
283         bool see_m = is_seen(m_ptr);
284         bool maneable = player_has_los_bold(target_ptr, m_ptr->fy, m_ptr->fx);
285         bool pet = is_pet(m_ptr);
286
287         bool in_no_magic_dungeon = (d_info[target_ptr->dungeon_idx].flags1 & DF1_NO_MAGIC) && floor_ptr->dun_level
288                 && (!floor_ptr->inside_quest || is_fixed_quest_idx(floor_ptr->inside_quest));
289
290         bool can_use_lite_area = FALSE;
291         bool can_remember;
292
293         /* Cannot cast spells when confused */
294         if (MON_CONFUSED(m_ptr)) return FALSE;
295
296         /* Extract the racial spell flags */
297         BIT_FLAGS f4 = r_ptr->flags4;
298         BIT_FLAGS f5 = r_ptr->a_ability_flags1;
299         BIT_FLAGS f6 = r_ptr->a_ability_flags2;
300
301         /* Target is given for pet? */
302         if (target_ptr->pet_t_m_idx && pet)
303         {
304                 target_idx = target_ptr->pet_t_m_idx;
305                 t_ptr = &floor_ptr->m_list[target_idx];
306
307                 /* Cancel if not projectable (for now) */
308                 if ((m_idx == target_idx) || !projectable(target_ptr, m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx))
309                 {
310                         target_idx = 0;
311                 }
312         }
313
314         /* Is there counter attack target? */
315         if (!target_idx && m_ptr->target_y)
316         {
317                 target_idx = floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
318
319                 if (target_idx)
320                 {
321                         t_ptr = &floor_ptr->m_list[target_idx];
322
323                         /* Cancel if neither enemy nor a given target */
324                         if ((m_idx == target_idx) ||
325                                 ((target_idx != target_ptr->pet_t_m_idx) && !are_enemies(target_ptr, m_ptr, t_ptr)))
326                         {
327                                 target_idx = 0;
328                         }
329
330                         /* Allow only summoning etc.. if not projectable */
331                         else if (!projectable(target_ptr, m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx))
332                         {
333                                 f4 &= (RF4_INDIRECT_MASK);
334                                 f5 &= (RF5_INDIRECT_MASK);
335                                 f6 &= (RF6_INDIRECT_MASK);
336                         }
337                 }
338         }
339
340         /* Look for enemies normally */
341         if (!target_idx)
342         {
343                 bool success = FALSE;
344
345                 if (target_ptr->phase_out)
346                 {
347                         start = randint1(floor_ptr->m_max - 1) + floor_ptr->m_max;
348                         if (randint0(2)) plus = -1;
349                 }
350                 else start = floor_ptr->m_max + 1;
351
352                 /* Scan thru all monsters */
353                 for (int i = start; ((i < start + floor_ptr->m_max) && (i > start - floor_ptr->m_max)); i += plus)
354                 {
355                         MONSTER_IDX dummy = (i % floor_ptr->m_max);
356                         if (!dummy) continue;
357
358                         target_idx = dummy;
359                         t_ptr = &floor_ptr->m_list[target_idx];
360
361                         if (!monster_is_valid(t_ptr)) continue;
362
363                         /* Monster must be 'an enemy' */
364                         if ((m_idx == target_idx) || !are_enemies(target_ptr, m_ptr, t_ptr)) continue;
365
366                         /* Monster must be projectable */
367                         if (!projectable(target_ptr, m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
368
369                         /* Get it */
370                         success = TRUE;
371                         break;
372                 }
373
374                 /* No enemy found */
375                 if (!success) return FALSE;
376         }
377
378         /* OK -- we've got a target */
379         y = t_ptr->fy;
380         x = t_ptr->fx;
381
382         /* Forget old counter attack target */
383         reset_target(m_ptr);
384
385         /* Remove unimplemented spells */
386         f6 &= ~(RF6_WORLD | RF6_TRAPS | RF6_FORGET);
387
388         if (f4 & RF4_BR_LITE)
389         {
390                 if (!los(target_ptr, m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx))
391                         f4 &= ~(RF4_BR_LITE);
392         }
393
394         /* Remove unimplemented special moves */
395         if (f6 & RF6_SPECIAL)
396         {
397                 if ((m_ptr->r_idx != MON_ROLENTO) && (r_ptr->d_char != 'B'))
398                         f6 &= ~(RF6_SPECIAL);
399         }
400
401         if (f6 & RF6_DARKNESS)
402         {
403                 bool vs_ninja = (target_ptr->pclass == CLASS_NINJA) && !is_hostile(t_ptr);
404
405                 if (vs_ninja &&
406                         !(r_ptr->flags3 & (RF3_UNDEAD | RF3_HURT_LITE)) &&
407                         !(r_ptr->flags7 & RF7_DARK_MASK))
408                         can_use_lite_area = TRUE;
409
410                 if (!(r_ptr->flags2 & RF2_STUPID))
411                 {
412                         if (d_info[target_ptr->dungeon_idx].flags1 & DF1_DARKNESS) f6 &= ~(RF6_DARKNESS);
413                         else if (vs_ninja && !can_use_lite_area) f6 &= ~(RF6_DARKNESS);
414                 }
415         }
416
417         if (in_no_magic_dungeon && !(r_ptr->flags2 & RF2_STUPID))
418         {
419                 f4 &= (RF4_NOMAGIC_MASK);
420                 f5 &= (RF5_NOMAGIC_MASK);
421                 f6 &= (RF6_NOMAGIC_MASK);
422         }
423
424         if (floor_ptr->inside_arena || target_ptr->phase_out)
425         {
426                 f4 &= ~(RF4_SUMMON_MASK);
427                 f5 &= ~(RF5_SUMMON_MASK);
428                 f6 &= ~(RF6_SUMMON_MASK | RF6_TELE_LEVEL);
429
430                 if (m_ptr->r_idx == MON_ROLENTO) f6 &= ~(RF6_SPECIAL);
431         }
432
433         if (target_ptr->phase_out && !one_in_(3))
434         {
435                 f6 &= ~(RF6_HEAL);
436         }
437
438         if (m_idx == target_ptr->riding)
439         {
440                 f4 &= ~(RF4_RIDING_MASK);
441                 f5 &= ~(RF5_RIDING_MASK);
442                 f6 &= ~(RF6_RIDING_MASK);
443         }
444
445         if (pet)
446         {
447                 f4 &= ~(RF4_SHRIEK);
448                 f6 &= ~(RF6_DARKNESS | RF6_TRAPS);
449
450                 if (!(target_ptr->pet_extra_flags & PF_TELEPORT))
451                 {
452                         f6 &= ~(RF6_BLINK | RF6_TPORT | RF6_TELE_TO | RF6_TELE_AWAY | RF6_TELE_LEVEL);
453                 }
454
455                 if (!(target_ptr->pet_extra_flags & PF_ATTACK_SPELL))
456                 {
457                         f4 &= ~(RF4_ATTACK_MASK);
458                         f5 &= ~(RF5_ATTACK_MASK);
459                         f6 &= ~(RF6_ATTACK_MASK);
460                 }
461
462                 if (!(target_ptr->pet_extra_flags & PF_SUMMON_SPELL))
463                 {
464                         f4 &= ~(RF4_SUMMON_MASK);
465                         f5 &= ~(RF5_SUMMON_MASK);
466                         f6 &= ~(RF6_SUMMON_MASK);
467                 }
468
469                 /* Prevent collateral damage */
470                 if (!(target_ptr->pet_extra_flags & PF_BALL_SPELL) && (m_idx != target_ptr->riding))
471                 {
472                         if ((f4 & (RF4_BALL_MASK & ~(RF4_ROCKET))) ||
473                                 (f5 & RF5_BALL_MASK) ||
474                                 (f6 & RF6_BALL_MASK))
475                         {
476                                 POSITION real_y = y;
477                                 POSITION real_x = x;
478
479                                 get_project_point(target_ptr, m_ptr->fy, m_ptr->fx, &real_y, &real_x, 0L);
480
481                                 if (projectable(target_ptr, real_y, real_x, target_ptr->y, target_ptr->x))
482                                 {
483                                         int dist = distance(real_y, real_x, target_ptr->y, target_ptr->x);
484
485                                         if (dist <= 2)
486                                         {
487                                                 f4 &= ~(RF4_BALL_MASK & ~(RF4_ROCKET));
488                                                 f5 &= ~(RF5_BALL_MASK);
489                                                 f6 &= ~(RF6_BALL_MASK);
490                                         }
491                                         else if (dist <= 4)
492                                         {
493                                                 f4 &= ~(RF4_BIG_BALL_MASK);
494                                                 f5 &= ~(RF5_BIG_BALL_MASK);
495                                                 f6 &= ~(RF6_BIG_BALL_MASK);
496                                         }
497                                 }
498                                 else if (f5 & RF5_BA_LITE)
499                                 {
500                                         if ((distance(real_y, real_x, target_ptr->y, target_ptr->x) <= 4) && los(target_ptr, real_y, real_x, target_ptr->y, target_ptr->x))
501                                                 f5 &= ~(RF5_BA_LITE);
502                                 }
503                         }
504
505                         if (f4 & RF4_ROCKET)
506                         {
507                                 POSITION real_y = y;
508                                 POSITION real_x = x;
509
510                                 get_project_point(target_ptr, m_ptr->fy, m_ptr->fx, &real_y, &real_x, PROJECT_STOP);
511                                 if (projectable(target_ptr, real_y, real_x, target_ptr->y, target_ptr->x) && (distance(real_y, real_x, target_ptr->y, target_ptr->x) <= 2))
512                                         f4 &= ~(RF4_ROCKET);
513                         }
514
515                         if (((f4 & RF4_BEAM_MASK) || (f5 & RF5_BEAM_MASK) || (f6 & RF6_BEAM_MASK)) &&
516                                 !direct_beam(target_ptr, m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx, m_ptr))
517                         {
518                                 f4 &= ~(RF4_BEAM_MASK);
519                                 f5 &= ~(RF5_BEAM_MASK);
520                                 f6 &= ~(RF6_BEAM_MASK);
521                         }
522
523                         if ((f4 & RF4_BREATH_MASK) || (f5 & RF5_BREATH_MASK) || (f6 & RF6_BREATH_MASK))
524                         {
525                                 /* Expected breath radius */
526                                 POSITION rad = (r_ptr->flags2 & RF2_POWERFUL) ? 3 : 2;
527
528                                 if (!breath_direct(target_ptr, m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx, rad, 0, TRUE))
529                                 {
530                                         f4 &= ~(RF4_BREATH_MASK);
531                                         f5 &= ~(RF5_BREATH_MASK);
532                                         f6 &= ~(RF6_BREATH_MASK);
533                                 }
534                                 else if ((f4 & RF4_BR_LITE) &&
535                                         !breath_direct(target_ptr, m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx, rad, GF_LITE, TRUE))
536                                 {
537                                         f4 &= ~(RF4_BR_LITE);
538                                 }
539                                 else if ((f4 & RF4_BR_DISI) &&
540                                         !breath_direct(target_ptr, m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx, rad, GF_DISINTEGRATE, TRUE))
541                                 {
542                                         f4 &= ~(RF4_BR_DISI);
543                                 }
544                         }
545                 }
546
547                 /* Special moves restriction */
548                 if (f6 & RF6_SPECIAL)
549                 {
550                         if (m_ptr->r_idx == MON_ROLENTO)
551                         {
552                                 if ((target_ptr->pet_extra_flags & (PF_ATTACK_SPELL | PF_SUMMON_SPELL)) != (PF_ATTACK_SPELL | PF_SUMMON_SPELL))
553                                         f6 &= ~(RF6_SPECIAL);
554                         }
555                         else if (r_ptr->d_char == 'B')
556                         {
557                                 if ((target_ptr->pet_extra_flags & (PF_ATTACK_SPELL | PF_TELEPORT)) != (PF_ATTACK_SPELL | PF_TELEPORT))
558                                         f6 &= ~(RF6_SPECIAL);
559                         }
560                         else f6 &= ~(RF6_SPECIAL);
561                 }
562         }
563
564         /* Remove some spells if necessary */
565
566         if (!(r_ptr->flags2 & RF2_STUPID))
567         {
568                 /* Check for a clean bolt shot */
569                 if (((f4 & RF4_BOLT_MASK) ||
570                         (f5 & RF5_BOLT_MASK) ||
571                         (f6 & RF6_BOLT_MASK)) &&
572                         !clean_shot(target_ptr, m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx, pet))
573                 {
574                         f4 &= ~(RF4_BOLT_MASK);
575                         f5 &= ~(RF5_BOLT_MASK);
576                         f6 &= ~(RF6_BOLT_MASK);
577                 }
578
579                 /* Check for a possible summon */
580                 if (((f4 & RF4_SUMMON_MASK) ||
581                         (f5 & RF5_SUMMON_MASK) ||
582                         (f6 & RF6_SUMMON_MASK)) &&
583                         !(summon_possible(target_ptr, t_ptr->fy, t_ptr->fx)))
584                 {
585                         /* Remove summoning spells */
586                         f4 &= ~(RF4_SUMMON_MASK);
587                         f5 &= ~(RF5_SUMMON_MASK);
588                         f6 &= ~(RF6_SUMMON_MASK);
589                 }
590
591                 /* Dispel magic */
592                 if ((f4 & RF4_DISPEL) && !dispel_check_monster(target_ptr, m_idx, target_idx))
593                 {
594                         /* Remove dispel spell */
595                         f4 &= ~(RF4_DISPEL);
596                 }
597
598                 /* Check for a possible raise dead */
599                 if ((f6 & RF6_RAISE_DEAD) && !raise_possible(target_ptr, m_ptr))
600                 {
601                         /* Remove raise dead spell */
602                         f6 &= ~(RF6_RAISE_DEAD);
603                 }
604
605                 /* Special moves restriction */
606                 if (f6 & RF6_SPECIAL)
607                 {
608                         if ((m_ptr->r_idx == MON_ROLENTO) && !summon_possible(target_ptr, t_ptr->fy, t_ptr->fx))
609                         {
610                                 f6 &= ~(RF6_SPECIAL);
611                         }
612                 }
613         }
614
615         if (r_ptr->flags2 & RF2_SMART)
616         {
617                 /* Hack -- allow "desperate" spells */
618                 if ((m_ptr->hp < m_ptr->maxhp / 10) &&
619                         (randint0(100) < 50))
620                 {
621                         /* Require intelligent spells */
622                         f4 &= (RF4_INT_MASK);
623                         f5 &= (RF5_INT_MASK);
624                         f6 &= (RF6_INT_MASK);
625                 }
626
627                 /* Hack -- decline "teleport level" in some case */
628                 if ((f6 & RF6_TELE_LEVEL) && is_teleport_level_ineffective(target_ptr, (target_idx == target_ptr->riding) ? 0 : target_idx))
629                 {
630                         f6 &= ~(RF6_TELE_LEVEL);
631                 }
632         }
633
634         /* No spells left */
635         if (!f4 && !f5 && !f6) return FALSE;
636
637         /* Extract the "inate" spells */
638         for (k = 0; k < 32; k++)
639         {
640                 if (f4 & (1L << k)) spell[num++] = k + RF4_SPELL_START;
641         }
642
643         /* Extract the "normal" spells */
644         for (k = 0; k < 32; k++)
645         {
646                 if (f5 & (1L << k)) spell[num++] = k + RF5_SPELL_START;
647         }
648
649         /* Extract the "bizarre" spells */
650         for (k = 0; k < 32; k++)
651         {
652                 if (f6 & (1L << k)) spell[num++] = k + RF6_SPELL_START;
653         }
654
655         /* No spells left */
656         if (!num) return FALSE;
657
658         /* Stop if player is dead or gone */
659         if (!target_ptr->playing || target_ptr->is_dead) return FALSE;
660
661         /* Handle "leaving" */
662         if (target_ptr->leaving) return FALSE;
663
664         /* Get the monster name (or "it") */
665         monster_desc(target_ptr, m_name, m_ptr, 0x00);
666
667 #ifdef JP
668 #else
669         /* Get the monster possessive ("his"/"her"/"its") */
670         monster_desc(target_ptr, m_poss, m_ptr, MD_PRON_VISIBLE | MD_POSSESSIVE);
671 #endif
672
673         /* Get the target's name (or "it") */
674         monster_desc(target_ptr, t_name, t_ptr, 0x00);
675
676         /* Choose a spell to cast */
677         thrown_spell = spell[randint0(num)];
678
679         if (target_ptr->riding && (m_idx == target_ptr->riding)) disturb(target_ptr, TRUE, TRUE);
680
681         /* Check for spell failure (inate attacks never fail) */
682         if (!spell_is_inate(thrown_spell) && (in_no_magic_dungeon || (MON_STUNNED(m_ptr) && one_in_(2))))
683         {
684                 disturb(target_ptr, TRUE, TRUE);
685                 if (see_m) msg_format(_("%^sは呪文を唱えようとしたが失敗した。",
686                         "%^s tries to cast a spell, but fails."), m_name);
687
688                 return TRUE;
689         }
690
691         /* Hex: Anti Magic Barrier */
692         if (!spell_is_inate(thrown_spell) && magic_barrier(target_ptr, m_idx))
693         {
694                 if (see_m) msg_format(_("反魔法バリアが%^sの呪文をかき消した。",
695                         "Anti magic barrier cancels the spell which %^s casts."), m_name);
696                 return TRUE;
697         }
698
699         can_remember = is_original_ap_and_seen(target_ptr, m_ptr);
700
701         dam = monspell_to_monster(target_ptr, thrown_spell, y, x, m_idx, target_idx, FALSE);
702         if (dam < 0) return FALSE;
703
704         bool is_special_magic = m_ptr->ml;
705         is_special_magic &= maneable;
706         is_special_magic &= current_world_ptr->timewalk_m_idx == 0;
707         is_special_magic &= !target_ptr->blind;
708         is_special_magic &= target_ptr->pclass == CLASS_IMITATOR;
709         is_special_magic &= thrown_spell != 167; /* Not RF6_SPECIAL */
710         if (is_special_magic)
711         {
712                 if (target_ptr->mane_num == MAX_MANE)
713                 {
714                         target_ptr->mane_num--;
715                         for (int i = 0; i < target_ptr->mane_num - 1; i++)
716                         {
717                                 target_ptr->mane_spell[i] = target_ptr->mane_spell[i + 1];
718                                 target_ptr->mane_dam[i] = target_ptr->mane_dam[i + 1];
719                         }
720                 }
721
722                 target_ptr->mane_spell[target_ptr->mane_num] = thrown_spell - RF4_SPELL_START;
723                 target_ptr->mane_dam[target_ptr->mane_num] = dam;
724                 target_ptr->mane_num++;
725                 target_ptr->new_mane = TRUE;
726
727                 target_ptr->redraw |= PR_IMITATION;
728         }
729
730         /* Remember what the monster did, if we saw it */
731         if (can_remember)
732         {
733                 /* Inate spell */
734                 if (thrown_spell < RF4_SPELL_START + RF4_SPELL_SIZE)
735                 {
736                         r_ptr->r_flags4 |= (1L << (thrown_spell - RF4_SPELL_START));
737                         if (r_ptr->r_cast_spell < MAX_UCHAR) r_ptr->r_cast_spell++;
738                 }
739
740                 /* Bolt or Ball */
741                 else if (thrown_spell < RF5_SPELL_START + RF5_SPELL_SIZE)
742                 {
743                         r_ptr->r_flags5 |= (1L << (thrown_spell - RF5_SPELL_START));
744                         if (r_ptr->r_cast_spell < MAX_UCHAR) r_ptr->r_cast_spell++;
745                 }
746
747                 /* Special spell */
748                 else if (thrown_spell < RF6_SPELL_START + RF6_SPELL_SIZE)
749                 {
750                         r_ptr->r_flags6 |= (1L << (thrown_spell - RF6_SPELL_START));
751                         if (r_ptr->r_cast_spell < MAX_UCHAR) r_ptr->r_cast_spell++;
752                 }
753         }
754
755         /* Always take note of monsters that kill you */
756         if (target_ptr->is_dead && (r_ptr->r_deaths < MAX_SHORT) && !floor_ptr->inside_arena)
757         {
758                 r_ptr->r_deaths++; /* Ignore appearance difference */
759         }
760
761         /* A spell was cast */
762         return TRUE;
763 }