OSDN Git Service

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