OSDN Git Service

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