OSDN Git Service

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