OSDN Git Service

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