OSDN Git Service

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