OSDN Git Service

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