OSDN Git Service

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