OSDN Git Service

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