OSDN Git Service

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