OSDN Git Service

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