OSDN Git Service

[Refactor] #39963 Separated spells3.h from spells1.h
[hengband/hengband.git] / src / mspells1.c
1 /*!
2  * @file mspells1.c
3  * @brief モンスター魔法の実装 / Monster spells (attack player)
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  * And now for Intelligent monster attacks (including spells).\n
13  *\n
14  * Original idea and code by "DRS" (David Reeves Sward).\n
15  * Major modifications by "BEN" (Ben Harrison).\n
16  *\n
17  * Give monsters more intelligent attack/spell selection based on\n
18  * observations of previous attacks on the player, and/or by allowing\n
19  * the monster to "cheat" and know the player status.\n
20  *\n
21  * Maintain an idea of the player status, and use that information\n
22  * to occasionally eliminate "ineffective" spell attacks.  We could\n
23  * also eliminate ineffective normal attacks, but there is no reason\n
24  * for the monster to do this, since he gains no benefit.\n
25  * Note that MINDLESS monsters are not allowed to use this code.\n
26  * And non-INTELLIGENT monsters only use it partially effectively.\n
27  *\n
28  * Actually learn what the player resists, and use that information\n
29  * to remove attacks or spells before using them.  This will require\n
30  * much less space, if I am not mistaken.  Thus, each monster gets a\n
31  * set of 32 bit flags, "smart", build from the various "SM_*" flags.\n
32  *\n
33  * This has the added advantage that attacks and spells are related.\n
34  * The "smart_learn" option means that the monster "learns" the flags\n
35  * that should be set, and "smart_cheat" means that he "knows" them.\n
36  * So "smart_cheat" means that the "smart" field is always up to date,\n
37  * while "smart_learn" means that the "smart" field is slowly learned.\n
38  * Both of them have the same effect on the "choose spell" routine.\n
39  */
40
41 #include "angband.h"
42 #include "util.h"
43
44 #include "effect/effect-characteristics.h"
45 #include "dungeon.h"
46 #include "grid.h"
47 #include "object-curse.h"
48 #include "quest.h"
49 #include "realm/realm-hex.h"
50 #include "player-move.h"
51 #include "player-status.h"
52 #include "monster.h"
53 #include "monster-spell.h"
54 #include "spell/spells-type.h"
55 #include "world.h"
56 #include "realm/realm-song.h"
57 #include "view/display-main-window.h"
58 #include "player-race.h"
59 #include "player-class.h"
60 #include "spell/process-effect.h"
61 #include "spell/spells3.h"
62
63 #define DO_SPELL_NONE    0
64 #define DO_SPELL_BR_LITE 1
65 #define DO_SPELL_BR_DISI 2
66 #define DO_SPELL_BA_LITE 3
67
68  /*!
69   * @brief モンスターがプレイヤーの弱点をついた選択を取るかどうかの判定 /
70   * Internal probability routine
71   * @param r_ptr モンスター種族の構造体参照ポインタ
72   * @param prob 基本確率(%)
73   * @return 適した選択を取るならばTRUEを返す。
74   */
75 static bool int_outof(monster_race *r_ptr, PERCENTAGE prob)
76 {
77         /* Non-Smart monsters are half as "smart" */
78         if (!(r_ptr->flags2 & RF2_SMART)) prob = prob / 2;
79
80         /* Roll the dice */
81         return (randint0(100) < prob);
82 }
83
84
85 /*!
86  * @brief モンスターの魔法一覧から戦術的に適さない魔法を除外する /
87  * Remove the "bad" spells from a spell list
88  * @param m_idx モンスターの構造体参照ポインタ
89  * @param f4p モンスター魔法のフラグリスト1
90  * @param f5p モンスター魔法のフラグリスト2
91  * @param f6p モンスター魔法のフラグリスト3
92  * @return なし
93  */
94 static void remove_bad_spells(MONSTER_IDX m_idx, player_type *target_ptr, u32b *f4p, u32b *f5p, u32b *f6p)
95 {
96         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
97         monster_race *r_ptr = &r_info[m_ptr->r_idx];
98
99         u32b f4 = (*f4p);
100         u32b f5 = (*f5p);
101         u32b f6 = (*f6p);
102
103         u32b smart = 0L;
104
105         /* Too stupid to know anything */
106         if (r_ptr->flags2 & RF2_STUPID) return;
107
108
109         /* Must be cheating or learning */
110         if (!smart_cheat && !smart_learn) return;
111
112
113         /* Update acquired knowledge */
114         if (smart_learn)
115         {
116                 /* Hack -- Occasionally forget player status */
117                 /* Only save SM_FRIENDLY, SM_PET or SM_CLONED */
118                 if (m_ptr->smart && (randint0(100) < 1)) m_ptr->smart &= (SM_FRIENDLY | SM_PET | SM_CLONED);
119
120                 /* Use the memorized flags */
121                 smart = m_ptr->smart;
122         }
123
124
125         /* Cheat if requested */
126         if (smart_cheat)
127         {
128                 /* Know element info */
129                 if (target_ptr->resist_acid) smart |= (SM_RES_ACID);
130                 if (is_oppose_acid(target_ptr)) smart |= (SM_OPP_ACID);
131                 if (target_ptr->immune_acid) smart |= (SM_IMM_ACID);
132                 if (target_ptr->resist_elec) smart |= (SM_RES_ELEC);
133                 if (is_oppose_elec(target_ptr)) smart |= (SM_OPP_ELEC);
134                 if (target_ptr->immune_elec) smart |= (SM_IMM_ELEC);
135                 if (target_ptr->resist_fire) smart |= (SM_RES_FIRE);
136                 if (is_oppose_fire(target_ptr)) smart |= (SM_OPP_FIRE);
137                 if (target_ptr->immune_fire) smart |= (SM_IMM_FIRE);
138                 if (target_ptr->resist_cold) smart |= (SM_RES_COLD);
139                 if (is_oppose_cold(target_ptr)) smart |= (SM_OPP_COLD);
140                 if (target_ptr->immune_cold) smart |= (SM_IMM_COLD);
141                 if (target_ptr->resist_pois) smart |= (SM_RES_POIS);
142                 if (is_oppose_pois(target_ptr)) smart |= (SM_OPP_POIS);
143
144                 /* Know special resistances */
145                 if (target_ptr->resist_neth) smart |= (SM_RES_NETH);
146                 if (target_ptr->resist_lite) smart |= (SM_RES_LITE);
147                 if (target_ptr->resist_dark) smart |= (SM_RES_DARK);
148                 if (target_ptr->resist_fear) smart |= (SM_RES_FEAR);
149                 if (target_ptr->resist_conf) smart |= (SM_RES_CONF);
150                 if (target_ptr->resist_chaos) smart |= (SM_RES_CHAOS);
151                 if (target_ptr->resist_disen) smart |= (SM_RES_DISEN);
152                 if (target_ptr->resist_blind) smart |= (SM_RES_BLIND);
153                 if (target_ptr->resist_nexus) smart |= (SM_RES_NEXUS);
154                 if (target_ptr->resist_sound) smart |= (SM_RES_SOUND);
155                 if (target_ptr->resist_shard) smart |= (SM_RES_SHARD);
156                 if (target_ptr->reflect) smart |= (SM_IMM_REFLECT);
157
158                 /* Know bizarre "resistances" */
159                 if (target_ptr->free_act) smart |= (SM_IMM_FREE);
160                 if (!target_ptr->msp) smart |= (SM_IMM_MANA);
161         }
162
163         if (!smart) return;
164
165         if (smart & SM_IMM_ACID)
166         {
167                 f4 &= ~(RF4_BR_ACID);
168                 f5 &= ~(RF5_BA_ACID);
169                 f5 &= ~(RF5_BO_ACID);
170         }
171         else if ((smart & (SM_OPP_ACID)) && (smart & (SM_RES_ACID)))
172         {
173                 if (int_outof(r_ptr, 80)) f4 &= ~(RF4_BR_ACID);
174                 if (int_outof(r_ptr, 80)) f5 &= ~(RF5_BA_ACID);
175                 if (int_outof(r_ptr, 80)) f5 &= ~(RF5_BO_ACID);
176         }
177         else if ((smart & (SM_OPP_ACID)) || (smart & (SM_RES_ACID)))
178         {
179                 if (int_outof(r_ptr, 30)) f4 &= ~(RF4_BR_ACID);
180                 if (int_outof(r_ptr, 30)) f5 &= ~(RF5_BA_ACID);
181                 if (int_outof(r_ptr, 30)) f5 &= ~(RF5_BO_ACID);
182         }
183
184
185         if (smart & (SM_IMM_ELEC))
186         {
187                 f4 &= ~(RF4_BR_ELEC);
188                 f5 &= ~(RF5_BA_ELEC);
189                 f5 &= ~(RF5_BO_ELEC);
190         }
191         else if ((smart & (SM_OPP_ELEC)) && (smart & (SM_RES_ELEC)))
192         {
193                 if (int_outof(r_ptr, 80)) f4 &= ~(RF4_BR_ELEC);
194                 if (int_outof(r_ptr, 80)) f5 &= ~(RF5_BA_ELEC);
195                 if (int_outof(r_ptr, 80)) f5 &= ~(RF5_BO_ELEC);
196         }
197         else if ((smart & (SM_OPP_ELEC)) || (smart & (SM_RES_ELEC)))
198         {
199                 if (int_outof(r_ptr, 30)) f4 &= ~(RF4_BR_ELEC);
200                 if (int_outof(r_ptr, 30)) f5 &= ~(RF5_BA_ELEC);
201                 if (int_outof(r_ptr, 30)) f5 &= ~(RF5_BO_ELEC);
202         }
203
204         if (smart & (SM_IMM_FIRE))
205         {
206                 f4 &= ~(RF4_BR_FIRE);
207                 f5 &= ~(RF5_BA_FIRE);
208                 f5 &= ~(RF5_BO_FIRE);
209         }
210         else if ((smart & (SM_OPP_FIRE)) && (smart & (SM_RES_FIRE)))
211         {
212                 if (int_outof(r_ptr, 80)) f4 &= ~(RF4_BR_FIRE);
213                 if (int_outof(r_ptr, 80)) f5 &= ~(RF5_BA_FIRE);
214                 if (int_outof(r_ptr, 80)) f5 &= ~(RF5_BO_FIRE);
215         }
216         else if ((smart & (SM_OPP_FIRE)) || (smart & (SM_RES_FIRE)))
217         {
218                 if (int_outof(r_ptr, 30)) f4 &= ~(RF4_BR_FIRE);
219                 if (int_outof(r_ptr, 30)) f5 &= ~(RF5_BA_FIRE);
220                 if (int_outof(r_ptr, 30)) f5 &= ~(RF5_BO_FIRE);
221         }
222
223         if (smart & (SM_IMM_COLD))
224         {
225                 f4 &= ~(RF4_BR_COLD);
226                 f5 &= ~(RF5_BA_COLD);
227                 f5 &= ~(RF5_BO_COLD);
228                 f5 &= ~(RF5_BO_ICEE);
229         }
230         else if ((smart & (SM_OPP_COLD)) && (smart & (SM_RES_COLD)))
231         {
232                 if (int_outof(r_ptr, 80)) f4 &= ~(RF4_BR_COLD);
233                 if (int_outof(r_ptr, 80)) f5 &= ~(RF5_BA_COLD);
234                 if (int_outof(r_ptr, 80)) f5 &= ~(RF5_BO_COLD);
235                 if (int_outof(r_ptr, 80)) f5 &= ~(RF5_BO_ICEE);
236         }
237         else if ((smart & (SM_OPP_COLD)) || (smart & (SM_RES_COLD)))
238         {
239                 if (int_outof(r_ptr, 30)) f4 &= ~(RF4_BR_COLD);
240                 if (int_outof(r_ptr, 30)) f5 &= ~(RF5_BA_COLD);
241                 if (int_outof(r_ptr, 30)) f5 &= ~(RF5_BO_COLD);
242                 if (int_outof(r_ptr, 20)) f5 &= ~(RF5_BO_ICEE);
243         }
244
245         if ((smart & (SM_OPP_POIS)) && (smart & (SM_RES_POIS)))
246         {
247                 if (int_outof(r_ptr, 80)) f4 &= ~(RF4_BR_POIS);
248                 if (int_outof(r_ptr, 80)) f5 &= ~(RF5_BA_POIS);
249                 if (int_outof(r_ptr, 60)) f4 &= ~(RF4_BA_NUKE);
250                 if (int_outof(r_ptr, 60)) f4 &= ~(RF4_BR_NUKE);
251         }
252         else if ((smart & (SM_OPP_POIS)) || (smart & (SM_RES_POIS)))
253         {
254                 if (int_outof(r_ptr, 30)) f4 &= ~(RF4_BR_POIS);
255                 if (int_outof(r_ptr, 30)) f5 &= ~(RF5_BA_POIS);
256         }
257
258         if (smart & (SM_RES_NETH))
259         {
260                 if (PRACE_IS_(target_ptr, RACE_SPECTRE))
261                 {
262                         f4 &= ~(RF4_BR_NETH);
263                         f5 &= ~(RF5_BA_NETH);
264                         f5 &= ~(RF5_BO_NETH);
265                 }
266                 else
267                 {
268                         if (int_outof(r_ptr, 20)) f4 &= ~(RF4_BR_NETH);
269                         if (int_outof(r_ptr, 50)) f5 &= ~(RF5_BA_NETH);
270                         if (int_outof(r_ptr, 50)) f5 &= ~(RF5_BO_NETH);
271                 }
272         }
273
274         if (smart & (SM_RES_LITE))
275         {
276                 if (int_outof(r_ptr, 50)) f4 &= ~(RF4_BR_LITE);
277                 if (int_outof(r_ptr, 50)) f5 &= ~(RF5_BA_LITE);
278         }
279
280         if (smart & (SM_RES_DARK))
281         {
282                 if (PRACE_IS_(target_ptr, RACE_VAMPIRE))
283                 {
284                         f4 &= ~(RF4_BR_DARK);
285                         f5 &= ~(RF5_BA_DARK);
286                 }
287                 else
288                 {
289                         if (int_outof(r_ptr, 50)) f4 &= ~(RF4_BR_DARK);
290                         if (int_outof(r_ptr, 50)) f5 &= ~(RF5_BA_DARK);
291                 }
292         }
293
294         if (smart & (SM_RES_FEAR))
295         {
296                 f5 &= ~(RF5_SCARE);
297         }
298
299         if (smart & (SM_RES_CONF))
300         {
301                 f5 &= ~(RF5_CONF);
302                 if (int_outof(r_ptr, 50)) f4 &= ~(RF4_BR_CONF);
303         }
304
305         if (smart & (SM_RES_CHAOS))
306         {
307                 if (int_outof(r_ptr, 20)) f4 &= ~(RF4_BR_CHAO);
308                 if (int_outof(r_ptr, 50)) f4 &= ~(RF4_BA_CHAO);
309         }
310
311         if (smart & (SM_RES_DISEN))
312         {
313                 if (int_outof(r_ptr, 40)) f4 &= ~(RF4_BR_DISE);
314         }
315
316         if (smart & (SM_RES_BLIND))
317         {
318                 f5 &= ~(RF5_BLIND);
319         }
320
321         if (smart & (SM_RES_NEXUS))
322         {
323                 if (int_outof(r_ptr, 50)) f4 &= ~(RF4_BR_NEXU);
324                 f6 &= ~(RF6_TELE_LEVEL);
325         }
326
327         if (smart & (SM_RES_SOUND))
328         {
329                 if (int_outof(r_ptr, 50)) f4 &= ~(RF4_BR_SOUN);
330         }
331
332         if (smart & (SM_RES_SHARD))
333         {
334                 if (int_outof(r_ptr, 40)) f4 &= ~(RF4_BR_SHAR);
335         }
336
337         if (smart & (SM_IMM_REFLECT))
338         {
339                 if (int_outof(r_ptr, 150)) f5 &= ~(RF5_BO_COLD);
340                 if (int_outof(r_ptr, 150)) f5 &= ~(RF5_BO_FIRE);
341                 if (int_outof(r_ptr, 150)) f5 &= ~(RF5_BO_ACID);
342                 if (int_outof(r_ptr, 150)) f5 &= ~(RF5_BO_ELEC);
343                 if (int_outof(r_ptr, 150)) f5 &= ~(RF5_BO_NETH);
344                 if (int_outof(r_ptr, 150)) f5 &= ~(RF5_BO_WATE);
345                 if (int_outof(r_ptr, 150)) f5 &= ~(RF5_BO_MANA);
346                 if (int_outof(r_ptr, 150)) f5 &= ~(RF5_BO_PLAS);
347                 if (int_outof(r_ptr, 150)) f5 &= ~(RF5_BO_ICEE);
348                 if (int_outof(r_ptr, 150)) f5 &= ~(RF5_MISSILE);
349         }
350
351         if (smart & (SM_IMM_FREE))
352         {
353                 f5 &= ~(RF5_HOLD);
354                 f5 &= ~(RF5_SLOW);
355         }
356
357         if (smart & (SM_IMM_MANA))
358         {
359                 f5 &= ~(RF5_DRAIN_MANA);
360         }
361
362         /* No spells left? */
363         /* if (!f4 && !f5 && !f6) ... */
364
365         (*f4p) = f4;
366         (*f5p) = f5;
367         (*f6p) = f6;
368 }
369
370
371 /*!
372  * @brief モンスターにとって所定の地点が召還に相応しい地点かどうかを返す。 /
373  * Determine if there is a space near the player in which a summoned creature can appear
374  * @param target_ptr プレーヤーへの参照ポインタ
375  * @param y1 判定を行いたいマスのY座標
376  * @param x1 判定を行いたいマスのX座標
377  * @return 召還に相応しいならばTRUEを返す
378  */
379 bool summon_possible(player_type *target_ptr, POSITION y1, POSITION x1)
380 {
381         POSITION y, x;
382
383         /* Start at the player's location, and check 2 grids in each dir */
384         floor_type *floor_ptr = target_ptr->current_floor_ptr;
385         for (y = y1 - 2; y <= y1 + 2; y++)
386         {
387                 for (x = x1 - 2; x <= x1 + 2; x++)
388                 {
389                         /* Ignore illegal locations */
390                         if (!in_bounds(floor_ptr, y, x)) continue;
391
392                         /* Only check a circular area */
393                         if (distance(y1, x1, y, x) > 2) continue;
394
395                         /* ...nor on the Pattern */
396                         if (pattern_tile(floor_ptr, y, x)) continue;
397
398                         /* Require empty floor grid in line of projection */
399                         if (is_cave_empty_bold(target_ptr, y, x) && projectable(target_ptr, y1, x1, y, x) && projectable(target_ptr, y, x, y1, x1)) return TRUE;
400                 }
401         }
402
403         return FALSE;
404 }
405
406
407 /*!
408  * @brief モンスターにとって死者復活を行うべき状態かどうかを返す /
409  * Determine if there is a space near the player in which a summoned creature can appear
410  * @param target_ptr プレーヤーへの参照ポインタ
411  * @param m_ptr 判定を行いたいモンスターの構造体参照ポインタ
412  * @return 死者復活が有効な状態ならばTRUEを返す。
413  */
414 bool raise_possible(player_type *target_ptr, monster_type *m_ptr)
415 {
416         POSITION y = m_ptr->fy;
417         POSITION x = m_ptr->fx;
418         floor_type *floor_ptr = target_ptr->current_floor_ptr;
419         for (POSITION xx = x - 5; xx <= x + 5; xx++)
420         {
421                 grid_type *g_ptr;
422                 for (POSITION yy = y - 5; yy <= y + 5; yy++)
423                 {
424                         if (distance(y, x, yy, xx) > 5) continue;
425                         if (!los(target_ptr, y, x, yy, xx)) continue;
426                         if (!projectable(target_ptr, y, x, yy, xx)) continue;
427
428                         g_ptr = &floor_ptr->grid_array[yy][xx];
429                         /* Scan the pile of objects */
430                         OBJECT_IDX this_o_idx, next_o_idx = 0;
431                         for (this_o_idx = g_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
432                         {
433                                 object_type *o_ptr = &floor_ptr->o_list[this_o_idx];
434                                 next_o_idx = o_ptr->next_o_idx;
435
436                                 /* Known to be worthless? */
437                                 if (o_ptr->tval == TV_CORPSE)
438                                 {
439                                         if (!monster_has_hostile_align(target_ptr, m_ptr, 0, 0, &r_info[o_ptr->pval])) return TRUE;
440                                 }
441                         }
442                 }
443         }
444
445         return FALSE;
446 }
447
448
449 /*!
450  * @brief モンスターにとってボルト型魔法が有効な状態かを返す /
451  * Determine if a bolt spell will hit the player.
452  * @param target_ptr プレーヤーへの参照ポインタ
453  * @param y1 ボルト魔法発射地点のY座標
454  * @param x1 ボルト魔法発射地点のX座標
455  * @param y2 ボルト魔法目標地点のY座標
456  * @param x2 ボルト魔法目標地点のX座標
457  * @param is_friend モンスターがプレイヤーに害意を持たない(ペットか友好的)ならばTRUEをつける
458  * @return ボルト型魔法が有効ならばTRUEを返す。
459  * @details
460  * Originally, it was possible for a friendly to shoot another friendly.\n
461  * Change it so a "clean shot" means no equally friendly monster is\n
462  * between the attacker and target.\n
463  *\n
464  * This is exactly like "projectable", but it will\n
465  * return FALSE if a monster is in the way.\n
466  * no equally friendly monster is\n
467  * between the attacker and target.\n
468  */
469 bool clean_shot(player_type *target_ptr, POSITION y1, POSITION x1, POSITION y2, POSITION x2, bool is_friend)
470 {
471         /* Check the projection path */
472         floor_type *floor_ptr = target_ptr->current_floor_ptr;
473         u16b grid_g[512];
474         int grid_n = project_path(target_ptr, grid_g, MAX_RANGE, y1, x1, y2, x2, 0);
475
476         /* No grid is ever projectable from itself */
477         if (!grid_n) return FALSE;
478
479         /* Final grid */
480         POSITION y = GRID_Y(grid_g[grid_n - 1]);
481         POSITION x = GRID_X(grid_g[grid_n - 1]);
482
483         /* May not end in an unrequested grid */
484         if ((y != y2) || (x != x2)) return FALSE;
485
486         for (int i = 0; i < grid_n; i++)
487         {
488                 y = GRID_Y(grid_g[i]);
489                 x = GRID_X(grid_g[i]);
490
491                 if ((floor_ptr->grid_array[y][x].m_idx > 0) && !((y == y2) && (x == x2)))
492                 {
493                         monster_type *m_ptr = &floor_ptr->m_list[floor_ptr->grid_array[y][x].m_idx];
494                         if (is_friend == is_pet(m_ptr))
495                         {
496                                 return FALSE;
497                         }
498                 }
499
500                 /* Pets may not shoot through the character - TNB */
501                 if (player_bold(target_ptr, y, x) && is_friend) return FALSE;
502         }
503
504         return TRUE;
505 }
506
507
508 /*!
509  * @brief モンスターのボルト型魔法処理 /
510  * Cast a bolt at the player Stop if we hit a monster Affect monsters and the player
511  * @param target_ptr プレーヤーへの参照ポインタ
512  * @param m_idx モンスターのID
513  * @param y 目標のY座標
514  * @param x 目標のX座標
515  * @param typ 効果属性ID
516  * @param dam_hp 威力
517  * @param monspell モンスター魔法のID
518  * @param target_type モンスターからモンスターへ撃つならMONSTER_TO_MONSTER、モンスターからプレイヤーならMONSTER_TO_PLAYER
519  * @return なし
520  */
521 void bolt(player_type *target_ptr, MONSTER_IDX m_idx, POSITION y, POSITION x, EFFECT_ID typ, int dam_hp, int monspell, int target_type)
522 {
523         BIT_FLAGS flg = 0;
524         switch (target_type)
525         {
526         case MONSTER_TO_MONSTER:
527                 flg = PROJECT_STOP | PROJECT_KILL;
528                 break;
529         case MONSTER_TO_PLAYER:
530                 flg = PROJECT_STOP | PROJECT_KILL | PROJECT_PLAYER;
531                 break;
532         }
533
534         /* Target the player with a bolt attack */
535         if (typ != GF_ARROW) flg |= PROJECT_REFLECTABLE;
536         bool learnable = spell_learnable(target_ptr, m_idx);
537         (void)project(target_ptr, m_idx, 0, y, x, dam_hp, typ, flg, (learnable ? monspell : -1));
538 }
539
540
541 /*!
542  * @brief モンスターのビーム型魔法処理 /
543  * @param target_ptr プレーヤーへの参照ポインタ
544  * @param m_idx モンスターのID
545  * @param y 目標のY座標
546  * @param x 目標のX座標
547  * @param typ 効果属性ID
548  * @param dam_hp 威力
549  * @param monspell モンスター魔法のID
550  * @param target_type モンスターからモンスターへ撃つならMONSTER_TO_MONSTER、モンスターからプレイヤーならMONSTER_TO_PLAYER
551  * @return なし
552  */
553 void beam(player_type *target_ptr, MONSTER_IDX m_idx, POSITION y, POSITION x, EFFECT_ID typ, int dam_hp, int monspell, int target_type)
554 {
555         BIT_FLAGS flg = 0;
556         switch (target_type)
557         {
558         case MONSTER_TO_MONSTER:
559                 flg = PROJECT_BEAM | PROJECT_KILL | PROJECT_THRU;
560                 break;
561         case MONSTER_TO_PLAYER:
562                 flg = PROJECT_BEAM | PROJECT_KILL | PROJECT_THRU | PROJECT_PLAYER;
563                 break;
564         }
565
566         /* Target the player with a bolt attack */
567         bool learnable = spell_learnable(target_ptr, m_idx);
568         (void)project(target_ptr, m_idx, 0, y, x, dam_hp, typ, flg, (learnable ? monspell : -1));
569 }
570
571
572 /*!
573  * @brief モンスターのボール型&ブレス型魔法処理 /
574  * Cast a breath (or ball) attack at the player Pass over any monsters that may be in the way Affect grids, objects, monsters, and the player
575  * @param target_ptr プレーヤーへの参照ポインタ
576  * @param y 目標地点のY座標
577  * @param x 目標地点のX座標
578  * @param m_idx モンスターのID
579  * @param typ 効果属性ID
580  * @param dam_hp 威力
581  * @param rad 半径
582  * @param breath
583  * @param monspell モンスター魔法のID
584  * @param target_type モンスターからモンスターへ撃つならMONSTER_TO_MONSTER、モンスターからプレイヤーならMONSTER_TO_PLAYER
585  * @return なし
586  */
587 void breath(player_type *target_ptr, POSITION y, POSITION x, MONSTER_IDX m_idx, EFFECT_ID typ, int dam_hp, POSITION rad, bool breath, int monspell, int target_type)
588 {
589         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
590         monster_race *r_ptr = &r_info[m_ptr->r_idx];
591         BIT_FLAGS flg = 0x00;
592         switch (target_type)
593         {
594         case MONSTER_TO_MONSTER:
595                 flg = PROJECT_GRID | PROJECT_ITEM | PROJECT_KILL;
596                 break;
597         case MONSTER_TO_PLAYER:
598                 flg = PROJECT_GRID | PROJECT_ITEM | PROJECT_KILL | PROJECT_PLAYER;
599                 break;
600         }
601
602         /* Determine the radius of the blast */
603         if ((rad < 1) && breath) rad = (r_ptr->flags2 & (RF2_POWERFUL)) ? 3 : 2;
604
605         /* Handle breath attacks */
606         if (breath) rad = 0 - rad;
607
608         switch (typ)
609         {
610         case GF_ROCKET:
611                 flg |= PROJECT_STOP;
612                 break;
613         case GF_DRAIN_MANA:
614         case GF_MIND_BLAST:
615         case GF_BRAIN_SMASH:
616         case GF_CAUSE_1:
617         case GF_CAUSE_2:
618         case GF_CAUSE_3:
619         case GF_CAUSE_4:
620         case GF_HAND_DOOM:
621                 flg |= (PROJECT_HIDE | PROJECT_AIMED);
622                 break;
623         }
624
625         /* Target the player with a ball attack */
626         bool learnable = spell_learnable(target_ptr, m_idx);
627         (void)project(target_ptr, m_idx, rad, y, x, dam_hp, typ, flg, (learnable ? monspell : -1));
628 }
629
630
631 /*!
632  * @brief ID値が正しいモンスター魔法IDかどうかを返す /
633  * Return TRUE if a spell is good for hurting the player (directly).
634  * @param spell 判定対象のID
635  * @return 正しいIDならばTRUEを返す。
636  */
637 static bool spell_attack(byte spell)
638 {
639         /* All RF4 spells hurt (except for shriek and dispel) */
640         if (spell < 128 && spell > 98) return TRUE;
641
642         /* Various "ball" spells */
643         if (spell >= 128 && spell <= 128 + 8) return TRUE;
644
645         /* "Cause wounds" and "bolt" spells */
646         if (spell >= 128 + 12 && spell < 128 + 27) return TRUE;
647
648         /* Hand of Doom */
649         if (spell == 160 + 1) return TRUE;
650
651         /* Psycho-Spear */
652         if (spell == 160 + 11) return TRUE;
653
654         /* Doesn't hurt */
655         return FALSE;
656 }
657
658
659 /*!
660  * @brief ID値が退避目的に適したモンスター魔法IDかどうかを返す /
661  * Return TRUE if a spell is good for escaping.
662  * @param spell 判定対象のID
663  * @return 適した魔法のIDならばTRUEを返す。
664  */
665 static bool spell_escape(byte spell)
666 {
667         /* Blink or Teleport */
668         if (spell == 160 + 4 || spell == 160 + 5) return TRUE;
669
670         /* Teleport the player away */
671         if (spell == 160 + 9 || spell == 160 + 10) return TRUE;
672
673         /* Isn't good for escaping */
674         return FALSE;
675 }
676
677
678 /*!
679  * @brief ID値が妨害目的に適したモンスター魔法IDかどうかを返す /
680  * Return TRUE if a spell is good for annoying the player.
681  * @param spell 判定対象のID
682  * @return 適した魔法のIDならばTRUEを返す。
683  */
684 static bool spell_annoy(byte spell)
685 {
686         /* Shriek */
687         if (spell == 96 + 0) return TRUE;
688
689         /* Brain smash, et al (added curses) */
690         if (spell >= 128 + 9 && spell <= 128 + 14) return TRUE;
691
692         /* Scare, confuse, blind, slow, paralyze */
693         if (spell >= 128 + 27 && spell <= 128 + 31) return TRUE;
694
695         /* Teleport to */
696         if (spell == 160 + 8) return TRUE;
697
698         /* Teleport level */
699         if (spell == 160 + 10) return TRUE;
700
701         /* Darkness, make traps, cause amnesia */
702         if (spell >= 160 + 12 && spell <= 160 + 14) return TRUE;
703
704         /* Doesn't annoy */
705         return FALSE;
706 }
707
708
709 /*!
710  * @brief ID値が召喚型のモンスター魔法IDかどうかを返す /
711  * Return TRUE if a spell is good for annoying the player.
712  * @param spell 判定対象のID
713  * @return 召喚型魔法のIDならばTRUEを返す。
714  */
715 static bool spell_summon(byte spell)
716 {
717         /* All summon spells */
718         if (spell >= 160 + 16) return TRUE;
719
720         /* Doesn't summon */
721         return FALSE;
722 }
723
724
725 /*!
726  * @brief ID値が死者復活処理かどうかを返す /
727  * Return TRUE if a spell is good for annoying the player.
728  * @param spell 判定対象のID
729  * @return 死者復活の処理ならばTRUEを返す。
730  */
731 static bool spell_raise(byte spell)
732 {
733         /* All raise-dead spells */
734         if (spell == 160 + 15) return TRUE;
735
736         /* Doesn't summon */
737         return FALSE;
738 }
739
740
741 /*!
742  * @brief ID値が戦術的なモンスター魔法IDかどうかを返す /
743  * Return TRUE if a spell is good in a tactical situation.
744  * @param spell 判定対象のID
745  * @return 戦術的な魔法のIDならばTRUEを返す。
746  */
747 static bool spell_tactic(byte spell)
748 {
749         /* Blink */
750         if (spell == 160 + 4) return TRUE;
751
752         /* Not good */
753         return FALSE;
754 }
755
756
757 /*!
758  * @brief ID値が無敵化するモンスター魔法IDかどうかを返す /
759  * Return TRUE if a spell makes invulnerable.
760  * @param spell 判定対象のID
761  * @return 召喚型魔法のIDならばTRUEを返す。
762  */
763 static bool spell_invulner(byte spell)
764 {
765         /* Invulnerability */
766         if (spell == 160 + 3) return TRUE;
767
768         /* No invulnerability */
769         return FALSE;
770 }
771
772
773 /*!
774  * @brief ID値が加速するモンスター魔法IDかどうかを返す /
775  * Return TRUE if a spell hastes.
776  * @param spell 判定対象のID
777  * @return 召喚型魔法のIDならばTRUEを返す。
778  */
779 static bool spell_haste(byte spell)
780 {
781         /* Haste self */
782         if (spell == 160 + 0) return TRUE;
783
784         /* Not a haste spell */
785         return FALSE;
786 }
787
788
789 /*!
790  * @brief ID値が時間停止を行うモンスター魔法IDかどうかを返す /
791  * Return TRUE if a spell world.
792  * @param spell 判定対象のID
793  * @return 時間停止魔法のIDならばTRUEを返す。
794  */
795 static bool spell_world(byte spell)
796 {
797         if (spell == 160 + 6) return TRUE;
798         return FALSE;
799 }
800
801
802 /*!
803  * @brief ID値が特別効果のモンスター魔法IDかどうかを返す /
804  * Return TRUE if a spell special.
805  * @param target_ptr プレーヤーへの参照ポインタ
806  * @param spell 判定対象のID
807  * @return 特別効果魔法のIDならばTRUEを返す。
808  */
809 static bool spell_special(player_type *target_ptr, byte spell)
810 {
811         if (target_ptr->phase_out) return FALSE;
812         if (spell == 160 + 7) return TRUE;
813         return FALSE;
814 }
815
816
817 /*!
818  * @brief ID値が光の剣のモンスター魔法IDかどうかを返す /
819  * Return TRUE if a spell psycho-spear.
820  * @param spell 判定対象のID
821  * @return 光の剣のIDならばTRUEを返す。
822  */
823 static bool spell_psy_spe(byte spell)
824 {
825         /* world */
826         if (spell == 160 + 11) return TRUE;
827
828         /* Not a haste spell */
829         return FALSE;
830 }
831
832
833 /*!
834  * @brief ID値が治癒魔法かどうかを返す /
835  * Return TRUE if a spell is good for healing.
836  * @param spell 判定対象のID
837  * @return 治癒魔法のIDならばTRUEを返す。
838  */
839 static bool spell_heal(byte spell)
840 {
841         /* Heal */
842         if (spell == 160 + 2) return TRUE;
843
844         /* No healing */
845         return FALSE;
846 }
847
848
849 /*!
850  * @brief ID値が魔力消去かどうかを返す /
851  * Return TRUE if a spell is good for dispel.
852  * @param spell 判定対象のID
853  * @return 魔力消去のIDならばTRUEを返す。
854  */
855 static bool spell_dispel(byte spell)
856 {
857         /* Dispel */
858         if (spell == 96 + 2) return TRUE;
859
860         /* No dispel */
861         return FALSE;
862 }
863
864
865 /*!
866  * @brief モンスターがプレイヤーに魔力消去を与えるべきかを判定するルーチン
867  * Check should monster cast dispel spell.
868  * @param m_idx モンスターの構造体配列ID
869  * @return 魔力消去をかけるべきならTRUEを返す。
870  */
871 bool dispel_check(player_type *creature_ptr, MONSTER_IDX m_idx)
872 {
873         /* Invulnabilty (including the song) */
874         if (IS_INVULN(creature_ptr)) return TRUE;
875
876         /* Wraith form */
877         if (creature_ptr->wraith_form) return TRUE;
878
879         /* Shield */
880         if (creature_ptr->shield) return TRUE;
881
882         /* Magic defence */
883         if (creature_ptr->magicdef) return TRUE;
884
885         /* Multi Shadow */
886         if (creature_ptr->multishadow) return TRUE;
887
888         /* Robe of dust */
889         if (creature_ptr->dustrobe) return TRUE;
890
891         /* Berserk Strength */
892         if (creature_ptr->shero && (creature_ptr->pclass != CLASS_BERSERKER)) return TRUE;
893
894         /* Demon Lord */
895         if (creature_ptr->mimic_form == MIMIC_DEMON_LORD) return TRUE;
896
897         /* Elemental resistances */
898         monster_type *m_ptr = &creature_ptr->current_floor_ptr->m_list[m_idx];
899         monster_race *r_ptr = &r_info[m_ptr->r_idx];
900         if (r_ptr->flags4 & RF4_BR_ACID)
901         {
902                 if (!creature_ptr->immune_acid && (creature_ptr->oppose_acid || music_singing(creature_ptr, MUSIC_RESIST))) return TRUE;
903                 if (creature_ptr->special_defense & DEFENSE_ACID) return TRUE;
904         }
905
906         if (r_ptr->flags4 & RF4_BR_FIRE)
907         {
908                 if (!((creature_ptr->prace == RACE_DEMON) && creature_ptr->lev > 44))
909                 {
910                         if (!creature_ptr->immune_fire && (creature_ptr->oppose_fire || music_singing(creature_ptr, MUSIC_RESIST))) return TRUE;
911                         if (creature_ptr->special_defense & DEFENSE_FIRE) return TRUE;
912                 }
913         }
914
915         if (r_ptr->flags4 & RF4_BR_ELEC)
916         {
917                 if (!creature_ptr->immune_elec && (creature_ptr->oppose_elec || music_singing(creature_ptr, MUSIC_RESIST))) return TRUE;
918                 if (creature_ptr->special_defense & DEFENSE_ELEC) return TRUE;
919         }
920
921         if (r_ptr->flags4 & RF4_BR_COLD)
922         {
923                 if (!creature_ptr->immune_cold && (creature_ptr->oppose_cold || music_singing(creature_ptr, MUSIC_RESIST))) return TRUE;
924                 if (creature_ptr->special_defense & DEFENSE_COLD) return TRUE;
925         }
926
927         if (r_ptr->flags4 & (RF4_BR_POIS | RF4_BR_NUKE))
928         {
929                 if (!((creature_ptr->pclass == CLASS_NINJA) && creature_ptr->lev > 44))
930                 {
931                         if (creature_ptr->oppose_pois || music_singing(creature_ptr, MUSIC_RESIST)) return TRUE;
932                         if (creature_ptr->special_defense & DEFENSE_POIS) return TRUE;
933                 }
934         }
935
936         /* Ultimate resistance */
937         if (creature_ptr->ult_res) return TRUE;
938
939         /* Potion of Neo Tsuyosi special */
940         if (creature_ptr->tsuyoshi) return TRUE;
941
942         /* Elemental Brands */
943         if ((creature_ptr->special_attack & ATTACK_ACID) && !(r_ptr->flagsr & RFR_EFF_IM_ACID_MASK)) return TRUE;
944         if ((creature_ptr->special_attack & ATTACK_FIRE) && !(r_ptr->flagsr & RFR_EFF_IM_FIRE_MASK)) return TRUE;
945         if ((creature_ptr->special_attack & ATTACK_ELEC) && !(r_ptr->flagsr & RFR_EFF_IM_ELEC_MASK)) return TRUE;
946         if ((creature_ptr->special_attack & ATTACK_COLD) && !(r_ptr->flagsr & RFR_EFF_IM_COLD_MASK)) return TRUE;
947         if ((creature_ptr->special_attack & ATTACK_POIS) && !(r_ptr->flagsr & RFR_EFF_IM_POIS_MASK)) return TRUE;
948
949         if (creature_ptr->pspeed < 145)
950         {
951                 if (IS_FAST(creature_ptr)) return TRUE;
952         }
953
954         /* Light speed */
955         if (creature_ptr->lightspeed && (m_ptr->mspeed < 136)) return TRUE;
956
957         if (creature_ptr->riding && (creature_ptr->current_floor_ptr->m_list[creature_ptr->riding].mspeed < 135))
958         {
959                 if (MON_FAST(&creature_ptr->current_floor_ptr->m_list[creature_ptr->riding])) return TRUE;
960         }
961
962         /* No need to cast dispel spell */
963         return FALSE;
964 }
965
966
967 /*!
968  * todo 長過ぎる。切り分けが必要
969  * @brief モンスターの魔法選択ルーチン
970  * Have a monster choose a spell from a list of "useful" spells.
971  * @param target_ptr プレーヤーへの参照ポインタ
972  * @param m_idx モンスターの構造体配列ID
973  * @param spells 候補魔法IDをまとめた配列
974  * @param num spellsの長さ
975  * @return 選択したモンスター魔法のID
976  * @details
977  * Note that this list does NOT include spells that will just hit\n
978  * other monsters, and the list is restricted when the monster is\n
979  * "desperate".  Should that be the job of this function instead?\n
980  *\n
981  * Stupid monsters will just pick a spell randomly.  Smart monsters\n
982  * will choose more "intelligently".\n
983  *\n
984  * Use the helper functions above to put spells into categories.\n
985  *\n
986  * This function may well be an efficiency bottleneck.\n
987  */
988 static int choose_attack_spell(player_type *target_ptr, MONSTER_IDX m_idx, byte spells[], byte num)
989 {
990         byte escape[96], escape_num = 0;
991         byte attack[96], attack_num = 0;
992         byte summon[96], summon_num = 0;
993         byte tactic[96], tactic_num = 0;
994         byte annoy[96], annoy_num = 0;
995         byte invul[96], invul_num = 0;
996         byte haste[96], haste_num = 0;
997         byte world[96], world_num = 0;
998         byte special[96], special_num = 0;
999         byte psy_spe[96], psy_spe_num = 0;
1000         byte raise[96], raise_num = 0;
1001         byte heal[96], heal_num = 0;
1002         byte dispel[96], dispel_num = 0;
1003
1004         /* Stupid monsters choose randomly */
1005         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1006         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1007         if (r_ptr->flags2 & (RF2_STUPID)) return (spells[randint0(num)]);
1008
1009         /* Categorize spells */
1010         for (int i = 0; i < num; i++)
1011         {
1012                 /* Escape spell? */
1013                 if (spell_escape(spells[i])) escape[escape_num++] = spells[i];
1014
1015                 /* Attack spell? */
1016                 if (spell_attack(spells[i])) attack[attack_num++] = spells[i];
1017
1018                 /* Summon spell? */
1019                 if (spell_summon(spells[i])) summon[summon_num++] = spells[i];
1020
1021                 /* Tactical spell? */
1022                 if (spell_tactic(spells[i])) tactic[tactic_num++] = spells[i];
1023
1024                 /* Annoyance spell? */
1025                 if (spell_annoy(spells[i])) annoy[annoy_num++] = spells[i];
1026
1027                 /* Invulnerability spell? */
1028                 if (spell_invulner(spells[i])) invul[invul_num++] = spells[i];
1029
1030                 /* Haste spell? */
1031                 if (spell_haste(spells[i])) haste[haste_num++] = spells[i];
1032
1033                 /* World spell? */
1034                 if (spell_world(spells[i])) world[world_num++] = spells[i];
1035
1036                 /* Special spell? */
1037                 if (spell_special(target_ptr, spells[i])) special[special_num++] = spells[i];
1038
1039                 /* Psycho-spear spell? */
1040                 if (spell_psy_spe(spells[i])) psy_spe[psy_spe_num++] = spells[i];
1041
1042                 /* Raise-dead spell? */
1043                 if (spell_raise(spells[i])) raise[raise_num++] = spells[i];
1044
1045                 /* Heal spell? */
1046                 if (spell_heal(spells[i])) heal[heal_num++] = spells[i];
1047
1048                 /* Dispel spell? */
1049                 if (spell_dispel(spells[i])) dispel[dispel_num++] = spells[i];
1050         }
1051
1052         /*** Try to pick an appropriate spell type ***/
1053
1054         /* world */
1055         if (world_num && (randint0(100) < 15) && !current_world_ptr->timewalk_m_idx)
1056         {
1057                 /* Choose haste spell */
1058                 return (world[randint0(world_num)]);
1059         }
1060
1061         /* special */
1062         if (special_num)
1063         {
1064                 bool success = FALSE;
1065                 switch (m_ptr->r_idx)
1066                 {
1067                 case MON_BANOR:
1068                 case MON_LUPART:
1069                         if ((m_ptr->hp < m_ptr->maxhp / 2) && r_info[MON_BANOR].max_num && r_info[MON_LUPART].max_num) success = TRUE;
1070                         break;
1071                 default: break;
1072                 }
1073
1074                 if (success) return (special[randint0(special_num)]);
1075         }
1076
1077         /* Still hurt badly, couldn't flee, attempt to heal */
1078         if (m_ptr->hp < m_ptr->maxhp / 3 && one_in_(2))
1079         {
1080                 /* Choose heal spell if possible */
1081                 if (heal_num) return (heal[randint0(heal_num)]);
1082         }
1083
1084         /* Hurt badly or afraid, attempt to flee */
1085         if (((m_ptr->hp < m_ptr->maxhp / 3) || MON_MONFEAR(m_ptr)) && one_in_(2))
1086         {
1087                 /* Choose escape spell if possible */
1088                 if (escape_num) return (escape[randint0(escape_num)]);
1089         }
1090
1091         /* special */
1092         if (special_num)
1093         {
1094                 bool success = FALSE;
1095                 switch (m_ptr->r_idx)
1096                 {
1097                 case MON_OHMU:
1098                 case MON_BANOR:
1099                 case MON_LUPART:
1100                         break;
1101                 case MON_BANORLUPART:
1102                         if (randint0(100) < 70) success = TRUE;
1103                         break;
1104                 case MON_ROLENTO:
1105                         if (randint0(100) < 40) success = TRUE;
1106                         break;
1107                 default:
1108                         if (randint0(100) < 50) success = TRUE;
1109                         break;
1110                 }
1111                 if (success) return (special[randint0(special_num)]);
1112         }
1113
1114         /* Player is close and we have attack spells, blink away */
1115         if ((distance(target_ptr->y, target_ptr->x, m_ptr->fy, m_ptr->fx) < 4) && (attack_num || (r_ptr->a_ability_flags2 & RF6_TRAPS)) && (randint0(100) < 75) && !current_world_ptr->timewalk_m_idx)
1116         {
1117                 /* Choose tactical spell */
1118                 if (tactic_num) return (tactic[randint0(tactic_num)]);
1119         }
1120
1121         /* Summon if possible (sometimes) */
1122         if (summon_num && (randint0(100) < 40))
1123         {
1124                 /* Choose summon spell */
1125                 return (summon[randint0(summon_num)]);
1126         }
1127
1128         /* dispel */
1129         if (dispel_num && one_in_(2))
1130         {
1131                 /* Choose dispel spell if possible */
1132                 if (dispel_check(target_ptr, m_idx))
1133                 {
1134                         return (dispel[randint0(dispel_num)]);
1135                 }
1136         }
1137
1138         /* Raise-dead if possible (sometimes) */
1139         if (raise_num && (randint0(100) < 40))
1140         {
1141                 /* Choose raise-dead spell */
1142                 return (raise[randint0(raise_num)]);
1143         }
1144
1145         /* Attack spell (most of the time) */
1146         if (IS_INVULN(target_ptr))
1147         {
1148                 if (psy_spe_num && (randint0(100) < 50))
1149                 {
1150                         /* Choose attack spell */
1151                         return (psy_spe[randint0(psy_spe_num)]);
1152                 }
1153                 else if (attack_num && (randint0(100) < 40))
1154                 {
1155                         /* Choose attack spell */
1156                         return (attack[randint0(attack_num)]);
1157                 }
1158         }
1159         else if (attack_num && (randint0(100) < 85))
1160         {
1161                 /* Choose attack spell */
1162                 return (attack[randint0(attack_num)]);
1163         }
1164
1165         /* Try another tactical spell (sometimes) */
1166         if (tactic_num && (randint0(100) < 50) && !current_world_ptr->timewalk_m_idx)
1167         {
1168                 /* Choose tactic spell */
1169                 return (tactic[randint0(tactic_num)]);
1170         }
1171
1172         /* Cast globe of invulnerability if not already in effect */
1173         if (invul_num && !m_ptr->mtimed[MTIMED_INVULNER] && (randint0(100) < 50))
1174         {
1175                 /* Choose Globe of Invulnerability */
1176                 return (invul[randint0(invul_num)]);
1177         }
1178
1179         /* We're hurt (not badly), try to heal */
1180         if ((m_ptr->hp < m_ptr->maxhp * 3 / 4) && (randint0(100) < 25))
1181         {
1182                 /* Choose heal spell if possible */
1183                 if (heal_num) return (heal[randint0(heal_num)]);
1184         }
1185
1186         /* Haste self if we aren't already somewhat hasted (rarely) */
1187         if (haste_num && (randint0(100) < 20) && !MON_FAST(m_ptr))
1188         {
1189                 /* Choose haste spell */
1190                 return (haste[randint0(haste_num)]);
1191         }
1192
1193         /* Annoy player (most of the time) */
1194         if (annoy_num && (randint0(100) < 80))
1195         {
1196                 /* Choose annoyance spell */
1197                 return (annoy[randint0(annoy_num)]);
1198         }
1199
1200         /* Choose no spell */
1201         return 0;
1202 }
1203
1204
1205 /*!
1206  * @brief ID値が非魔術的な特殊技能かどうかを返す /
1207  * Return TRUE if a spell is inate spell.
1208  * @param spell 判定対象のID
1209  * @return 非魔術的な特殊技能ならばTRUEを返す。
1210  */
1211 bool spell_is_inate(SPELL_IDX spell)
1212 {
1213         if (spell < 32 * 4) /* Set RF4 */
1214         {
1215                 if ((1L << (spell - 32 * 3)) & RF4_NOMAGIC_MASK) return TRUE;
1216         }
1217
1218         if (spell < 32 * 5) /* Set RF5 */
1219         {
1220                 if ((1L << (spell - 32 * 4)) & RF5_NOMAGIC_MASK) return TRUE;
1221         }
1222
1223         if (spell < 32 * 6) /* Set RF6 */
1224         {
1225                 if ((1L << (spell - 32 * 5)) & RF6_NOMAGIC_MASK) return TRUE;
1226         }
1227
1228         /* This spell is not "inate" */
1229         return FALSE;
1230 }
1231
1232
1233 /*!
1234  * @brief モンスターがプレイヤーにダメージを与えるための最適な座標を算出する /
1235  * @param target_ptr プレーヤーへの参照ポインタ
1236  * @param m_ptr 技能を使用するモンスター構造体の参照ポインタ
1237  * @param yp 最適な目標地点のY座標を返す参照ポインタ
1238  * @param xp 最適な目標地点のX座標を返す参照ポインタ
1239  * @param f_flag 射線に入れるのを避ける地形の所持フラグ
1240  * @param path_check 射線を判定するための関数ポインタ
1241  * @return 有効な座標があった場合TRUEを返す
1242  */
1243 static bool adjacent_grid_check(player_type *target_ptr, monster_type *m_ptr, POSITION *yp, POSITION *xp,
1244         int f_flag, bool(*path_check)(player_type *, POSITION, POSITION, POSITION, POSITION))
1245 {
1246         static int tonari_y[4][8] = { {-1, -1, -1,  0,  0,  1,  1,  1},
1247                                                                  {-1, -1, -1,  0,  0,  1,  1,  1},
1248                                                                  { 1,  1,  1,  0,  0, -1, -1, -1},
1249                                                                  { 1,  1,  1,  0,  0, -1, -1, -1} };
1250         static int tonari_x[4][8] = { {-1,  0,  1, -1,  1, -1,  0,  1},
1251                                                                  { 1,  0, -1,  1, -1,  1,  0, -1},
1252                                                                  {-1,  0,  1, -1,  1, -1,  0,  1},
1253                                                                  { 1,  0, -1,  1, -1,  1,  0, -1} };
1254
1255         int next;
1256         if (m_ptr->fy < target_ptr->y && m_ptr->fx < target_ptr->x) next = 0;
1257         else if (m_ptr->fy < target_ptr->y) next = 1;
1258         else if (m_ptr->fx < target_ptr->x) next = 2;
1259         else next = 3;
1260
1261         floor_type *floor_ptr = target_ptr->current_floor_ptr;
1262         for (int i = 0; i < 8; i++)
1263         {
1264                 int next_x = *xp + tonari_x[next][i];
1265                 int next_y = *yp + tonari_y[next][i];
1266                 grid_type *g_ptr;
1267
1268                 /* Access the next grid */
1269                 g_ptr = &floor_ptr->grid_array[next_y][next_x];
1270
1271                 /* Skip this feature */
1272                 if (!cave_have_flag_grid(g_ptr, f_flag)) continue;
1273
1274                 if (path_check(target_ptr, m_ptr->fy, m_ptr->fx, next_y, next_x))
1275                 {
1276                         *yp = next_y;
1277                         *xp = next_x;
1278                         return TRUE;
1279                 }
1280         }
1281
1282         return FALSE;
1283 }
1284
1285
1286 /*!
1287  * todo メインルーチンの割に長過ぎる。要分割
1288  * @brief モンスターの特殊技能メインルーチン /
1289  * Creatures can cast spells, shoot missiles, and breathe.
1290  * @param target_ptr プレーヤーへの参照ポインタ
1291  * @param m_idx モンスター構造体配列のID
1292  * @return 実際に特殊技能を利用したらTRUEを返す
1293  * @details
1294  * Returns "TRUE" if a spell (or whatever) was (successfully) cast.\n
1295  *\n
1296  * This function could use some work, but remember to\n
1297  * keep it as optimized as possible, while retaining generic code.\n
1298  *\n
1299  * Verify the various "blind-ness" checks in the code.\n
1300  *\n
1301  * Note that several effects should really not be "seen"\n
1302  * if the player is blind.  See also "effects.c" for other "mistakes".\n
1303  *\n
1304  * Perhaps monsters should breathe at locations *near* the player,\n
1305  * since this would allow them to inflict "partial" damage.\n
1306  *\n
1307  * Perhaps smart monsters should decline to use "bolt" spells if\n
1308  * there is a monster in the way, unless they wish to kill it.\n
1309  *\n
1310  * Note that, to allow the use of the "track_target" option at some\n
1311  * later time, certain non-optimal things are done in the code below,\n
1312  * including explicit checks against the "direct" variable, which is\n
1313  * currently always true by the time it is checked, but which should\n
1314  * really be set according to an explicit "projectable()" test, and\n
1315  * the use of generic "x,y" locations instead of the player location,\n
1316  * with those values being initialized with the player location.\n
1317  *\n
1318  * It will not be possible to "correctly" handle the case in which a\n
1319  * monster attempts to attack a location which is thought to contain\n
1320  * the player, but which in fact is nowhere near the player, since this\n
1321  * might induce all sorts of messages about the attack itself, and about\n
1322  * the effects of the attack, which the player might or might not be in\n
1323  * a position to observe.  Thus, for simplicity, it is probably best to\n
1324  * only allow "faulty" attacks by a monster if one of the important grids\n
1325  * (probably the initial or final grid) is in fact in view of the player.\n
1326  * It may be necessary to actually prevent spell attacks except when the\n
1327  * monster actually has line of sight to the player.  Note that a monster\n
1328  * could be left in a bizarre situation after the player ducked behind a\n
1329  * pillar and then teleported away, for example.\n
1330  *\n
1331  * @note
1332  * that certain spell attacks do not use the "project()" function\n
1333  * but "simulate" it via the "direct" variable, which is always at least\n
1334  * as restrictive as the "project()" function.  This is necessary to\n
1335  * prevent "blindness" attacks and such from bending around walls, etc,\n
1336  * and to allow the use of the "track_target" option in the future.\n
1337  *\n
1338  * Note that this function attempts to optimize the use of spells for the\n
1339  * cases in which the monster has no spells, or has spells but cannot use\n
1340  * them, or has spells but they will have no "useful" effect.  Note that\n
1341  * this function has been an efficiency bottleneck in the past.\n
1342  *\n
1343  * Note the special "MFLAG_NICE" flag, which prevents a monster from using\n
1344  * any spell attacks until the player has had a single chance to move.\n
1345  */
1346 bool make_attack_spell(MONSTER_IDX m_idx, player_type *target_ptr)
1347 {
1348 #ifdef JP
1349 #else
1350
1351         char m_poss[80];
1352 #endif
1353         /* Extract the "see-able-ness" */
1354         floor_type *floor_ptr = target_ptr->current_floor_ptr;
1355         monster_type *m_ptr = &floor_ptr->m_list[m_idx];
1356         monster_race *r_ptr = &r_info[m_ptr->r_idx];
1357
1358         /* Cannot cast spells when confused */
1359         if (MON_CONFUSED(m_ptr))
1360         {
1361                 reset_target(m_ptr);
1362                 return FALSE;
1363         }
1364
1365         /* Cannot cast spells when nice */
1366         if (m_ptr->mflag & MFLAG_NICE) return FALSE;
1367         if (!is_hostile(m_ptr)) return FALSE;
1368
1369
1370         /* Sometimes forbid inate attacks (breaths) */
1371         bool no_inate = FALSE;
1372         if (randint0(100) >= (r_ptr->freq_spell * 2)) no_inate = TRUE;
1373
1374         /* Extract the racial spell flags */
1375         BIT_FLAGS f4 = r_ptr->flags4;
1376         BIT_FLAGS f5 = r_ptr->a_ability_flags1;
1377         BIT_FLAGS f6 = r_ptr->a_ability_flags2;
1378
1379         /*** require projectable player ***/
1380
1381         /* Check range */
1382         if ((m_ptr->cdis > MAX_RANGE) && !m_ptr->target_y) return FALSE;
1383
1384         /* Check path for lite breath */
1385         POSITION x = target_ptr->x;
1386         POSITION y = target_ptr->y;
1387         POSITION x_br_lite = 0;
1388         POSITION y_br_lite = 0;
1389         if (f4 & RF4_BR_LITE)
1390         {
1391                 y_br_lite = y;
1392                 x_br_lite = x;
1393
1394                 if (los(target_ptr, m_ptr->fy, m_ptr->fx, y_br_lite, x_br_lite))
1395                 {
1396                         feature_type *f_ptr = &f_info[floor_ptr->grid_array[y_br_lite][x_br_lite].feat];
1397
1398                         if (!have_flag(f_ptr->flags, FF_LOS))
1399                         {
1400                                 if (have_flag(f_ptr->flags, FF_PROJECT) && one_in_(2)) f4 &= ~(RF4_BR_LITE);
1401                         }
1402                 }
1403
1404                 /* Check path to next grid */
1405                 else if (!adjacent_grid_check(target_ptr, m_ptr, &y_br_lite, &x_br_lite, FF_LOS, los)) f4 &= ~(RF4_BR_LITE);
1406
1407                 /* Don't breath lite to the wall if impossible */
1408                 if (!(f4 & RF4_BR_LITE))
1409                 {
1410                         y_br_lite = 0;
1411                         x_br_lite = 0;
1412                 }
1413         }
1414
1415         /* Check path */
1416         bool do_spell = DO_SPELL_NONE;
1417         if (projectable(target_ptr, m_ptr->fy, m_ptr->fx, y, x))
1418         {
1419                 feature_type *f_ptr = &f_info[floor_ptr->grid_array[y][x].feat];
1420
1421                 if (!have_flag(f_ptr->flags, FF_PROJECT))
1422                 {
1423                         /* Breath disintegration to the wall if possible */
1424                         if ((f4 & RF4_BR_DISI) && have_flag(f_ptr->flags, FF_HURT_DISI) && one_in_(2)) do_spell = DO_SPELL_BR_DISI;
1425
1426                         /* Breath lite to the transparent wall if possible */
1427                         else if ((f4 & RF4_BR_LITE) && have_flag(f_ptr->flags, FF_LOS) && one_in_(2)) do_spell = DO_SPELL_BR_LITE;
1428                 }
1429         }
1430
1431         /* Check path to next grid */
1432         else
1433         {
1434                 bool success = FALSE;
1435
1436                 if ((f4 & RF4_BR_DISI) && (m_ptr->cdis < MAX_RANGE / 2) &&
1437                         in_disintegration_range(floor_ptr, m_ptr->fy, m_ptr->fx, y, x) &&
1438                         (one_in_(10) || (projectable(target_ptr, y, x, m_ptr->fy, m_ptr->fx) && one_in_(2))))
1439                 {
1440                         do_spell = DO_SPELL_BR_DISI;
1441                         success = TRUE;
1442                 }
1443                 else if ((f4 & RF4_BR_LITE) && (m_ptr->cdis < MAX_RANGE / 2) &&
1444                         los(target_ptr, m_ptr->fy, m_ptr->fx, y, x) && one_in_(5))
1445                 {
1446                         do_spell = DO_SPELL_BR_LITE;
1447                         success = TRUE;
1448                 }
1449                 else if ((f5 & RF5_BA_LITE) && (m_ptr->cdis <= MAX_RANGE))
1450                 {
1451                         POSITION by = y, bx = x;
1452                         get_project_point(target_ptr, m_ptr->fy, m_ptr->fx, &by, &bx, 0L);
1453                         if ((distance(by, bx, y, x) <= 3) && los(target_ptr, by, bx, y, x) && one_in_(5))
1454                         {
1455                                 do_spell = DO_SPELL_BA_LITE;
1456                                 success = TRUE;
1457                         }
1458                 }
1459
1460                 if (!success) success = adjacent_grid_check(target_ptr, m_ptr, &y, &x, FF_PROJECT, projectable);
1461
1462                 if (!success)
1463                 {
1464                         if (m_ptr->target_y && m_ptr->target_x)
1465                         {
1466                                 y = m_ptr->target_y;
1467                                 x = m_ptr->target_x;
1468                                 f4 &= (RF4_INDIRECT_MASK);
1469                                 f5 &= (RF5_INDIRECT_MASK);
1470                                 f6 &= (RF6_INDIRECT_MASK);
1471                                 success = TRUE;
1472                         }
1473
1474                         if (y_br_lite && x_br_lite && (m_ptr->cdis < MAX_RANGE / 2) && one_in_(5))
1475                         {
1476                                 if (!success)
1477                                 {
1478                                         y = y_br_lite;
1479                                         x = x_br_lite;
1480                                         do_spell = DO_SPELL_BR_LITE;
1481                                         success = TRUE;
1482                                 }
1483                                 else f4 |= (RF4_BR_LITE);
1484                         }
1485                 }
1486
1487                 /* No spells */
1488                 if (!success) return FALSE;
1489         }
1490
1491         reset_target(m_ptr);
1492
1493         DEPTH rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
1494
1495         /* Forbid inate attacks sometimes */
1496         if (no_inate)
1497         {
1498                 f4 &= ~(RF4_NOMAGIC_MASK);
1499                 f5 &= ~(RF5_NOMAGIC_MASK);
1500                 f6 &= ~(RF6_NOMAGIC_MASK);
1501         }
1502
1503         bool can_use_lite_area = FALSE;
1504         if (f6 & RF6_DARKNESS)
1505         {
1506                 if ((target_ptr->pclass == CLASS_NINJA) &&
1507                         !(r_ptr->flags3 & (RF3_UNDEAD | RF3_HURT_LITE)) &&
1508                         !(r_ptr->flags7 & RF7_DARK_MASK))
1509                         can_use_lite_area = TRUE;
1510
1511                 if (!(r_ptr->flags2 & RF2_STUPID))
1512                 {
1513                         if (d_info[target_ptr->dungeon_idx].flags1 & DF1_DARKNESS) f6 &= ~(RF6_DARKNESS);
1514                         else if ((target_ptr->pclass == CLASS_NINJA) && !can_use_lite_area) f6 &= ~(RF6_DARKNESS);
1515                 }
1516         }
1517
1518         bool in_no_magic_dungeon = (d_info[target_ptr->dungeon_idx].flags1 & DF1_NO_MAGIC) && floor_ptr->dun_level
1519                 && (!floor_ptr->inside_quest || is_fixed_quest_idx(floor_ptr->inside_quest));
1520         if (in_no_magic_dungeon && !(r_ptr->flags2 & RF2_STUPID))
1521         {
1522                 f4 &= (RF4_NOMAGIC_MASK);
1523                 f5 &= (RF5_NOMAGIC_MASK);
1524                 f6 &= (RF6_NOMAGIC_MASK);
1525         }
1526
1527         if (r_ptr->flags2 & RF2_SMART)
1528         {
1529                 /* Hack -- allow "desperate" spells */
1530                 if ((m_ptr->hp < m_ptr->maxhp / 10) &&
1531                         (randint0(100) < 50))
1532                 {
1533                         /* Require intelligent spells */
1534                         f4 &= (RF4_INT_MASK);
1535                         f5 &= (RF5_INT_MASK);
1536                         f6 &= (RF6_INT_MASK);
1537                 }
1538
1539                 /* Hack -- decline "teleport level" in some case */
1540                 if ((f6 & RF6_TELE_LEVEL) && is_teleport_level_ineffective(target_ptr, 0))
1541                 {
1542                         f6 &= ~(RF6_TELE_LEVEL);
1543                 }
1544         }
1545
1546         /* No spells left */
1547         if (!f4 && !f5 && !f6) return FALSE;
1548
1549         /* Remove the "ineffective" spells */
1550         remove_bad_spells(m_idx, target_ptr, &f4, &f5, &f6);
1551
1552         if (floor_ptr->inside_arena || target_ptr->phase_out)
1553         {
1554                 f4 &= ~(RF4_SUMMON_MASK);
1555                 f5 &= ~(RF5_SUMMON_MASK);
1556                 f6 &= ~(RF6_SUMMON_MASK | RF6_TELE_LEVEL);
1557
1558                 if (m_ptr->r_idx == MON_ROLENTO) f6 &= ~(RF6_SPECIAL);
1559         }
1560
1561         /* No spells left */
1562         if (!f4 && !f5 && !f6) return FALSE;
1563
1564         if (!(r_ptr->flags2 & RF2_STUPID))
1565         {
1566                 if (!target_ptr->csp) f5 &= ~(RF5_DRAIN_MANA);
1567
1568                 /* Check for a clean bolt shot */
1569                 if (((f4 & RF4_BOLT_MASK) ||
1570                         (f5 & RF5_BOLT_MASK) ||
1571                         (f6 & RF6_BOLT_MASK)) &&
1572                         !clean_shot(target_ptr, m_ptr->fy, m_ptr->fx, target_ptr->y, target_ptr->x, FALSE))
1573                 {
1574                         /* Remove spells that will only hurt friends */
1575                         f4 &= ~(RF4_BOLT_MASK);
1576                         f5 &= ~(RF5_BOLT_MASK);
1577                         f6 &= ~(RF6_BOLT_MASK);
1578                 }
1579
1580                 /* Check for a possible summon */
1581                 if (((f4 & RF4_SUMMON_MASK) ||
1582                         (f5 & RF5_SUMMON_MASK) ||
1583                         (f6 & RF6_SUMMON_MASK)) &&
1584                         !(summon_possible(target_ptr, y, x)))
1585                 {
1586                         /* Remove summoning spells */
1587                         f4 &= ~(RF4_SUMMON_MASK);
1588                         f5 &= ~(RF5_SUMMON_MASK);
1589                         f6 &= ~(RF6_SUMMON_MASK);
1590                 }
1591
1592                 /* Check for a possible raise dead */
1593                 if ((f6 & RF6_RAISE_DEAD) && !raise_possible(target_ptr, m_ptr))
1594                 {
1595                         /* Remove raise dead spell */
1596                         f6 &= ~(RF6_RAISE_DEAD);
1597                 }
1598
1599                 /* Special moves restriction */
1600                 if (f6 & RF6_SPECIAL)
1601                 {
1602                         if ((m_ptr->r_idx == MON_ROLENTO) && !summon_possible(target_ptr, y, x))
1603                         {
1604                                 f6 &= ~(RF6_SPECIAL);
1605                         }
1606                 }
1607
1608                 /* No spells left */
1609                 if (!f4 && !f5 && !f6) return FALSE;
1610         }
1611
1612         /* Extract the "inate" spells */
1613         byte spell[96], num = 0;
1614         for (int k = 0; k < 32; k++)
1615         {
1616                 if (f4 & (1L << k)) spell[num++] = k + RF4_SPELL_START;
1617         }
1618
1619         /* Extract the "normal" spells */
1620         for (int k = 0; k < 32; k++)
1621         {
1622                 if (f5 & (1L << k)) spell[num++] = k + RF5_SPELL_START;
1623         }
1624
1625         /* Extract the "bizarre" spells */
1626         for (int k = 0; k < 32; k++)
1627         {
1628                 if (f6 & (1L << k)) spell[num++] = k + RF6_SPELL_START;
1629         }
1630
1631         /* No spells left */
1632         if (!num) return FALSE;
1633
1634         /* Stop if player is dead or gone */
1635         if (!target_ptr->playing || target_ptr->is_dead) return FALSE;
1636
1637         /* Stop if player is leaving */
1638         if (target_ptr->leaving) return FALSE;
1639
1640         /* Get the monster name (or "it") */
1641         GAME_TEXT m_name[MAX_NLEN];
1642         monster_desc(target_ptr, m_name, m_ptr, 0x00);
1643
1644 #ifdef JP
1645 #else
1646         /* Get the monster possessive ("his"/"her"/"its") */
1647         monster_desc(target_ptr, m_poss, m_ptr, MD_PRON_VISIBLE | MD_POSSESSIVE);
1648 #endif
1649
1650         SPELL_IDX thrown_spell = 0;
1651         switch (do_spell)
1652         {
1653         case DO_SPELL_NONE:
1654         {
1655                 int attempt = 10;
1656                 while (attempt--)
1657                 {
1658                         thrown_spell = choose_attack_spell(target_ptr, m_idx, spell, num);
1659                         if (thrown_spell) break;
1660                 }
1661         }
1662
1663         break;
1664
1665         case DO_SPELL_BR_LITE:
1666                 thrown_spell = 96 + 14; /* RF4_BR_LITE */
1667                 break;
1668
1669         case DO_SPELL_BR_DISI:
1670                 thrown_spell = 96 + 31; /* RF4_BR_DISI */
1671                 break;
1672
1673         case DO_SPELL_BA_LITE:
1674                 thrown_spell = 128 + 20; /* RF5_BA_LITE */
1675                 break;
1676
1677         default:
1678                 return FALSE;
1679         }
1680
1681         /* Abort if no spell was chosen */
1682         if (!thrown_spell) return FALSE;
1683
1684         /* Calculate spell failure rate */
1685         PERCENTAGE failrate = 25 - (rlev + 3) / 4;
1686
1687         /* Hack -- Stupid monsters will never fail (for jellies and such) */
1688         if (r_ptr->flags2 & RF2_STUPID) failrate = 0;
1689
1690         /* Check for spell failure (inate attacks never fail) */
1691         if (!spell_is_inate(thrown_spell)
1692                 && (in_no_magic_dungeon || (MON_STUNNED(m_ptr) && one_in_(2)) || (randint0(100) < failrate)))
1693         {
1694                 disturb(target_ptr, TRUE, TRUE);
1695                 msg_format(_("%^sは呪文を唱えようとしたが失敗した。", "%^s tries to cast a spell, but fails."), m_name);
1696
1697                 return TRUE;
1698         }
1699
1700         /* Hex: Anti Magic Barrier */
1701         if (!spell_is_inate(thrown_spell) && magic_barrier(target_ptr, m_idx))
1702         {
1703                 msg_format(_("反魔法バリアが%^sの呪文をかき消した。", "Anti magic barrier cancels the spell which %^s casts."), m_name);
1704                 return TRUE;
1705         }
1706
1707         /* Projectable? */
1708         bool direct = player_bold(target_ptr, y, x);
1709         bool can_remember = is_original_ap_and_seen(target_ptr, m_ptr);
1710         if (!direct)
1711         {
1712                 switch (thrown_spell)
1713                 {
1714                 case 96 + 2:    /* RF4_DISPEL */
1715                 case 96 + 4:    /* RF4_SHOOT */
1716                 case 128 + 9:   /* RF5_DRAIN_MANA */
1717                 case 128 + 10:  /* RF5_MIND_BLAST */
1718                 case 128 + 11:  /* RF5_BRAIN_SMASH */
1719                 case 128 + 12:  /* RF5_CAUSE_1 */
1720                 case 128 + 13:  /* RF5_CAUSE_2 */
1721                 case 128 + 14:  /* RF5_CAUSE_3 */
1722                 case 128 + 15:  /* RF5_CAUSE_4 */
1723                 case 128 + 16:  /* RF5_BO_ACID */
1724                 case 128 + 17:  /* RF5_BO_ELEC */
1725                 case 128 + 18:  /* RF5_BO_FIRE */
1726                 case 128 + 19:  /* RF5_BO_COLD */
1727                 case 128 + 21:  /* RF5_BO_NETH */
1728                 case 128 + 22:  /* RF5_BO_WATE */
1729                 case 128 + 23:  /* RF5_BO_MANA */
1730                 case 128 + 24:  /* RF5_BO_PLAS */
1731                 case 128 + 25:  /* RF5_BO_ICEE */
1732                 case 128 + 26:  /* RF5_MISSILE */
1733                 case 128 + 27:  /* RF5_SCARE */
1734                 case 128 + 28:  /* RF5_BLIND */
1735                 case 128 + 29:  /* RF5_CONF */
1736                 case 128 + 30:  /* RF5_SLOW */
1737                 case 128 + 31:  /* RF5_HOLD */
1738                 case 160 + 1:   /* RF6_HAND_DOOM */
1739                 case 160 + 8:   /* RF6_TELE_TO */
1740                 case 160 + 9:   /* RF6_TELE_AWAY */
1741                 case 160 + 10:  /* RF6_TELE_LEVEL */
1742                 case 160 + 11:  /* RF6_PSY_SPEAR */
1743                 case 160 + 12:  /* RF6_DARKNESS */
1744                 case 160 + 14:  /* RF6_FORGET */
1745                         return FALSE;
1746                 }
1747         }
1748
1749         /* Cast the spell. */
1750         int dam = monspell_to_player(target_ptr, thrown_spell, y, x, m_idx);
1751         if (dam < 0) return FALSE;
1752
1753         if ((target_ptr->action == ACTION_LEARN) && thrown_spell > 175)
1754         {
1755                 learn_spell(target_ptr, thrown_spell - 96);
1756         }
1757
1758         bool seen = (!target_ptr->blind && m_ptr->ml);
1759         bool maneable = player_has_los_bold(target_ptr, m_ptr->fy, m_ptr->fx);
1760         if (seen && maneable && !current_world_ptr->timewalk_m_idx && (target_ptr->pclass == CLASS_IMITATOR))
1761         {
1762                 if (thrown_spell != 167) /* Not RF6_SPECIAL */
1763                 {
1764                         if (target_ptr->mane_num == MAX_MANE)
1765                         {
1766                                 int i;
1767                                 target_ptr->mane_num--;
1768                                 for (i = 0; i < target_ptr->mane_num; i++)
1769                                 {
1770                                         target_ptr->mane_spell[i] = target_ptr->mane_spell[i + 1];
1771                                         target_ptr->mane_dam[i] = target_ptr->mane_dam[i + 1];
1772                                 }
1773                         }
1774                         target_ptr->mane_spell[target_ptr->mane_num] = thrown_spell - 96;
1775                         target_ptr->mane_dam[target_ptr->mane_num] = dam;
1776                         target_ptr->mane_num++;
1777                         target_ptr->new_mane = TRUE;
1778
1779                         target_ptr->redraw |= (PR_IMITATION);
1780                 }
1781         }
1782
1783         /* Remember what the monster did to us */
1784         if (can_remember)
1785         {
1786                 /* Inate spell */
1787                 if (thrown_spell < 32 * 4)
1788                 {
1789                         r_ptr->r_flags4 |= (1L << (thrown_spell - 32 * 3));
1790                         if (r_ptr->r_cast_spell < MAX_UCHAR) r_ptr->r_cast_spell++;
1791                 }
1792
1793                 /* Bolt or Ball */
1794                 else if (thrown_spell < 32 * 5)
1795                 {
1796                         r_ptr->r_flags5 |= (1L << (thrown_spell - 32 * 4));
1797                         if (r_ptr->r_cast_spell < MAX_UCHAR) r_ptr->r_cast_spell++;
1798                 }
1799
1800                 /* Special spell */
1801                 else if (thrown_spell < 32 * 6)
1802                 {
1803                         r_ptr->r_flags6 |= (1L << (thrown_spell - 32 * 5));
1804                         if (r_ptr->r_cast_spell < MAX_UCHAR) r_ptr->r_cast_spell++;
1805                 }
1806         }
1807
1808         /* Always take note of monsters that kill you */
1809         if (target_ptr->is_dead && (r_ptr->r_deaths < MAX_SHORT) && !floor_ptr->inside_arena)
1810         {
1811                 r_ptr->r_deaths++; /* Ignore appearance difference */
1812         }
1813
1814         /* A spell was cast */
1815         return TRUE;
1816 }