OSDN Git Service

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