OSDN Git Service

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