OSDN Git Service

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