OSDN Git Service

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