OSDN Git Service

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