OSDN Git Service

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