OSDN Git Service

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