OSDN Git Service

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