OSDN Git Service

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