OSDN Git Service

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