OSDN Git Service

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