OSDN Git Service

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