3 * @brief モンスター魔法の実装 / Monster spells (attack player)
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
12 * And now for Intelligent monster attacks (including spells).\n
14 * Original idea and code by "DRS" (David Reeves Sward).\n
15 * Major modifications by "BEN" (Ben Harrison).\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
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
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
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
42 #include "object-curse.h"
46 * @brief モンスターがプレイヤーの弱点をついた選択を取るかどうかの判定 /
47 * Internal probability routine
48 * @param r_ptr モンスター種族の構造体参照ポインタ
50 * @return 適した選択を取るならばTRUEを返す。
52 static bool int_outof(monster_race *r_ptr, PERCENTAGE prob)
54 /* Non-Smart monsters are half as "smart" */
55 if (!(r_ptr->flags2 & RF2_SMART)) prob = prob / 2;
58 return (randint0(100) < prob);
63 * @brief モンスターの魔法一覧から戦術的に適さない魔法を除外する /
64 * Remove the "bad" spells from a spell list
65 * @param m_idx モンスターの構造体参照ポインタ
66 * @param f4p モンスター魔法のフラグリスト1
67 * @param f5p モンスター魔法のフラグリスト2
68 * @param f6p モンスター魔法のフラグリスト3
71 static void remove_bad_spells(MONSTER_IDX m_idx, u32b *f4p, u32b *f5p, u32b *f6p)
73 monster_type *m_ptr = &m_list[m_idx];
74 monster_race *r_ptr = &r_info[m_ptr->r_idx];
83 /* Too stupid to know anything */
84 if (r_ptr->flags2 & RF2_STUPID) return;
87 /* Must be cheating or learning */
88 if (!smart_cheat && !smart_learn) return;
91 /* Update acquired knowledge */
94 /* Hack -- Occasionally forget player status */
95 /* Only save SM_FRIENDLY, SM_PET or SM_CLONED */
96 if (m_ptr->smart && (randint0(100) < 1)) m_ptr->smart &= (SM_FRIENDLY | SM_PET | SM_CLONED);
98 /* Use the memorized flags */
103 /* Cheat if requested */
106 /* Know basic info */
107 if (p_ptr->resist_acid) smart |= (SM_RES_ACID);
108 if (IS_OPPOSE_ACID()) smart |= (SM_OPP_ACID);
109 if (p_ptr->immune_acid) smart |= (SM_IMM_ACID);
110 if (p_ptr->resist_elec) smart |= (SM_RES_ELEC);
111 if (IS_OPPOSE_ELEC()) smart |= (SM_OPP_ELEC);
112 if (p_ptr->immune_elec) smart |= (SM_IMM_ELEC);
113 if (p_ptr->resist_fire) smart |= (SM_RES_FIRE);
114 if (IS_OPPOSE_FIRE()) smart |= (SM_OPP_FIRE);
115 if (p_ptr->immune_fire) smart |= (SM_IMM_FIRE);
116 if (p_ptr->resist_cold) smart |= (SM_RES_COLD);
117 if (IS_OPPOSE_COLD()) smart |= (SM_OPP_COLD);
118 if (p_ptr->immune_cold) smart |= (SM_IMM_COLD);
120 /* Know poison info */
121 if (p_ptr->resist_pois) smart |= (SM_RES_POIS);
122 if (IS_OPPOSE_POIS()) smart |= (SM_OPP_POIS);
124 /* Know special resistances */
125 if (p_ptr->resist_neth) smart |= (SM_RES_NETH);
126 if (p_ptr->resist_lite) smart |= (SM_RES_LITE);
127 if (p_ptr->resist_dark) smart |= (SM_RES_DARK);
128 if (p_ptr->resist_fear) smart |= (SM_RES_FEAR);
129 if (p_ptr->resist_conf) smart |= (SM_RES_CONF);
130 if (p_ptr->resist_chaos) smart |= (SM_RES_CHAOS);
131 if (p_ptr->resist_disen) smart |= (SM_RES_DISEN);
132 if (p_ptr->resist_blind) smart |= (SM_RES_BLIND);
133 if (p_ptr->resist_nexus) smart |= (SM_RES_NEXUS);
134 if (p_ptr->resist_sound) smart |= (SM_RES_SOUND);
135 if (p_ptr->resist_shard) smart |= (SM_RES_SHARD);
136 if (p_ptr->reflect) smart |= (SM_IMM_REFLECT);
138 /* Know bizarre "resistances" */
139 if (p_ptr->free_act) smart |= (SM_IMM_FREE);
140 if (!p_ptr->msp) smart |= (SM_IMM_MANA);
148 if (smart & SM_IMM_ACID)
150 f4 &= ~(RF4_BR_ACID);
151 f5 &= ~(RF5_BA_ACID);
152 f5 &= ~(RF5_BO_ACID);
154 else if ((smart & (SM_OPP_ACID)) && (smart & (SM_RES_ACID)))
156 if (int_outof(r_ptr, 80)) f4 &= ~(RF4_BR_ACID);
157 if (int_outof(r_ptr, 80)) f5 &= ~(RF5_BA_ACID);
158 if (int_outof(r_ptr, 80)) f5 &= ~(RF5_BO_ACID);
160 else if ((smart & (SM_OPP_ACID)) || (smart & (SM_RES_ACID)))
162 if (int_outof(r_ptr, 30)) f4 &= ~(RF4_BR_ACID);
163 if (int_outof(r_ptr, 30)) f5 &= ~(RF5_BA_ACID);
164 if (int_outof(r_ptr, 30)) f5 &= ~(RF5_BO_ACID);
168 if (smart & (SM_IMM_ELEC))
170 f4 &= ~(RF4_BR_ELEC);
171 f5 &= ~(RF5_BA_ELEC);
172 f5 &= ~(RF5_BO_ELEC);
174 else if ((smart & (SM_OPP_ELEC)) && (smart & (SM_RES_ELEC)))
176 if (int_outof(r_ptr, 80)) f4 &= ~(RF4_BR_ELEC);
177 if (int_outof(r_ptr, 80)) f5 &= ~(RF5_BA_ELEC);
178 if (int_outof(r_ptr, 80)) f5 &= ~(RF5_BO_ELEC);
180 else if ((smart & (SM_OPP_ELEC)) || (smart & (SM_RES_ELEC)))
182 if (int_outof(r_ptr, 30)) f4 &= ~(RF4_BR_ELEC);
183 if (int_outof(r_ptr, 30)) f5 &= ~(RF5_BA_ELEC);
184 if (int_outof(r_ptr, 30)) f5 &= ~(RF5_BO_ELEC);
188 if (smart & (SM_IMM_FIRE))
190 f4 &= ~(RF4_BR_FIRE);
191 f5 &= ~(RF5_BA_FIRE);
192 f5 &= ~(RF5_BO_FIRE);
194 else if ((smart & (SM_OPP_FIRE)) && (smart & (SM_RES_FIRE)))
196 if (int_outof(r_ptr, 80)) f4 &= ~(RF4_BR_FIRE);
197 if (int_outof(r_ptr, 80)) f5 &= ~(RF5_BA_FIRE);
198 if (int_outof(r_ptr, 80)) f5 &= ~(RF5_BO_FIRE);
200 else if ((smart & (SM_OPP_FIRE)) || (smart & (SM_RES_FIRE)))
202 if (int_outof(r_ptr, 30)) f4 &= ~(RF4_BR_FIRE);
203 if (int_outof(r_ptr, 30)) f5 &= ~(RF5_BA_FIRE);
204 if (int_outof(r_ptr, 30)) f5 &= ~(RF5_BO_FIRE);
208 if (smart & (SM_IMM_COLD))
210 f4 &= ~(RF4_BR_COLD);
211 f5 &= ~(RF5_BA_COLD);
212 f5 &= ~(RF5_BO_COLD);
213 f5 &= ~(RF5_BO_ICEE);
215 else if ((smart & (SM_OPP_COLD)) && (smart & (SM_RES_COLD)))
217 if (int_outof(r_ptr, 80)) f4 &= ~(RF4_BR_COLD);
218 if (int_outof(r_ptr, 80)) f5 &= ~(RF5_BA_COLD);
219 if (int_outof(r_ptr, 80)) f5 &= ~(RF5_BO_COLD);
220 if (int_outof(r_ptr, 80)) f5 &= ~(RF5_BO_ICEE);
222 else if ((smart & (SM_OPP_COLD)) || (smart & (SM_RES_COLD)))
224 if (int_outof(r_ptr, 30)) f4 &= ~(RF4_BR_COLD);
225 if (int_outof(r_ptr, 30)) f5 &= ~(RF5_BA_COLD);
226 if (int_outof(r_ptr, 30)) f5 &= ~(RF5_BO_COLD);
227 if (int_outof(r_ptr, 20)) f5 &= ~(RF5_BO_ICEE);
231 if ((smart & (SM_OPP_POIS)) && (smart & (SM_RES_POIS)))
233 if (int_outof(r_ptr, 80)) f4 &= ~(RF4_BR_POIS);
234 if (int_outof(r_ptr, 80)) f5 &= ~(RF5_BA_POIS);
235 if (int_outof(r_ptr, 60)) f4 &= ~(RF4_BA_NUKE);
236 if (int_outof(r_ptr, 60)) f4 &= ~(RF4_BR_NUKE);
238 else if ((smart & (SM_OPP_POIS)) || (smart & (SM_RES_POIS)))
240 if (int_outof(r_ptr, 30)) f4 &= ~(RF4_BR_POIS);
241 if (int_outof(r_ptr, 30)) f5 &= ~(RF5_BA_POIS);
245 if (smart & (SM_RES_NETH))
247 if (prace_is_(RACE_SPECTRE))
249 f4 &= ~(RF4_BR_NETH);
250 f5 &= ~(RF5_BA_NETH);
251 f5 &= ~(RF5_BO_NETH);
255 if (int_outof(r_ptr, 20)) f4 &= ~(RF4_BR_NETH);
256 if (int_outof(r_ptr, 50)) f5 &= ~(RF5_BA_NETH);
257 if (int_outof(r_ptr, 50)) f5 &= ~(RF5_BO_NETH);
261 if (smart & (SM_RES_LITE))
263 if (int_outof(r_ptr, 50)) f4 &= ~(RF4_BR_LITE);
264 if (int_outof(r_ptr, 50)) f5 &= ~(RF5_BA_LITE);
267 if (smart & (SM_RES_DARK))
269 if (prace_is_(RACE_VAMPIRE))
271 f4 &= ~(RF4_BR_DARK);
272 f5 &= ~(RF5_BA_DARK);
276 if (int_outof(r_ptr, 50)) f4 &= ~(RF4_BR_DARK);
277 if (int_outof(r_ptr, 50)) f5 &= ~(RF5_BA_DARK);
281 if (smart & (SM_RES_FEAR))
286 if (smart & (SM_RES_CONF))
289 if (int_outof(r_ptr, 50)) f4 &= ~(RF4_BR_CONF);
292 if (smart & (SM_RES_CHAOS))
294 if (int_outof(r_ptr, 20)) f4 &= ~(RF4_BR_CHAO);
295 if (int_outof(r_ptr, 50)) f4 &= ~(RF4_BA_CHAO);
298 if (smart & (SM_RES_DISEN))
300 if (int_outof(r_ptr, 40)) f4 &= ~(RF4_BR_DISE);
303 if (smart & (SM_RES_BLIND))
308 if (smart & (SM_RES_NEXUS))
310 if (int_outof(r_ptr, 50)) f4 &= ~(RF4_BR_NEXU);
311 f6 &= ~(RF6_TELE_LEVEL);
314 if (smart & (SM_RES_SOUND))
316 if (int_outof(r_ptr, 50)) f4 &= ~(RF4_BR_SOUN);
319 if (smart & (SM_RES_SHARD))
321 if (int_outof(r_ptr, 40)) f4 &= ~(RF4_BR_SHAR);
324 if (smart & (SM_IMM_REFLECT))
326 if (int_outof(r_ptr, 150)) f5 &= ~(RF5_BO_COLD);
327 if (int_outof(r_ptr, 150)) f5 &= ~(RF5_BO_FIRE);
328 if (int_outof(r_ptr, 150)) f5 &= ~(RF5_BO_ACID);
329 if (int_outof(r_ptr, 150)) f5 &= ~(RF5_BO_ELEC);
330 if (int_outof(r_ptr, 150)) f5 &= ~(RF5_BO_NETH);
331 if (int_outof(r_ptr, 150)) f5 &= ~(RF5_BO_WATE);
332 if (int_outof(r_ptr, 150)) f5 &= ~(RF5_BO_MANA);
333 if (int_outof(r_ptr, 150)) f5 &= ~(RF5_BO_PLAS);
334 if (int_outof(r_ptr, 150)) f5 &= ~(RF5_BO_ICEE);
335 if (int_outof(r_ptr, 150)) f5 &= ~(RF5_MISSILE);
338 if (smart & (SM_IMM_FREE))
344 if (smart & (SM_IMM_MANA))
346 f5 &= ~(RF5_DRAIN_MANA);
349 /* No spells left? */
350 /* if (!f4 && !f5 && !f6) ... */
359 * @brief モンスターにとって所定の地点が召還に相応しい地点かどうかを返す。 /
360 * Determine if there is a space near the player in which a summoned creature can appear
361 * @param y1 判定を行いたいマスのY座標
362 * @param x1 判定を行いたいマスのX座標
363 * @return 召還に相応しいならばTRUEを返す
365 bool summon_possible(POSITION y1, POSITION x1)
369 /* Start at the player's location, and check 2 grids in each dir */
370 for (y = y1 - 2; y <= y1 + 2; y++)
372 for (x = x1 - 2; x <= x1 + 2; x++)
374 /* Ignore illegal locations */
375 if (!in_bounds(y, x)) continue;
377 /* Only check a circular area */
378 if (distance(y1, x1, y, x)>2) continue;
380 /* ...nor on the Pattern */
381 if (pattern_tile(y, x)) continue;
383 /* Require empty floor grid in line of projection */
384 if (cave_empty_bold(y, x) && projectable(y1, x1, y, x) && projectable(y, x, y1, x1)) return (TRUE);
393 * @brief モンスターにとって死者復活を行うべき状態かどうかを返す /
394 * Determine if there is a space near the player in which a summoned creature can appear
395 * @param m_ptr 判定を行いたいモンスターの構造体参照ポインタ
396 * @return 死者復活が有効な状態ならばTRUEを返す。
398 bool raise_possible(monster_type *m_ptr)
401 POSITION y = m_ptr->fy;
402 POSITION x = m_ptr->fx;
403 OBJECT_IDX this_o_idx, next_o_idx = 0;
406 for (xx = x - 5; xx <= x + 5; xx++)
408 for (yy = y - 5; yy <= y + 5; yy++)
410 if (distance(y, x, yy, xx) > 5) continue;
411 if (!los(y, x, yy, xx)) continue;
412 if (!projectable(y, x, yy, xx)) continue;
414 c_ptr = &cave[yy][xx];
415 /* Scan the pile of objects */
416 for (this_o_idx = c_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
418 object_type *o_ptr = &o_list[this_o_idx];
420 /* Acquire next object */
421 next_o_idx = o_ptr->next_o_idx;
423 /* Known to be worthless? */
424 if (o_ptr->tval == TV_CORPSE)
426 if (!monster_has_hostile_align(m_ptr, 0, 0, &r_info[o_ptr->pval])) return TRUE;
437 * @brief モンスターにとってボルト型魔法が有効な状態かを返す /
438 * Determine if a bolt spell will hit the player.
439 * @param y1 ボルト魔法発射地点のY座標
440 * @param x1 ボルト魔法発射地点のX座標
441 * @param y2 ボルト魔法目標地点のY座標
442 * @param x2 ボルト魔法目標地点のX座標
443 * @param is_friend モンスターがプレイヤーに害意を持たない(ペットか友好的)ならばTRUEをつける
444 * @return ボルト型魔法が有効ならばTRUEを返す。
446 * Originally, it was possible for a friendly to shoot another friendly.\n
447 * Change it so a "clean shot" means no equally friendly monster is\n
448 * between the attacker and target.\n
450 * This is exactly like "projectable", but it will\n
451 * return FALSE if a monster is in the way.\n
452 * no equally friendly monster is\n
453 * between the attacker and target.\n
455 bool clean_shot(POSITION y1, POSITION x1, POSITION y2, POSITION x2, bool is_friend)
457 /* Must be the same as projectable() */
465 /* Check the projection path */
466 grid_n = project_path(grid_g, MAX_RANGE, y1, x1, y2, x2, 0);
468 /* No grid is ever projectable from itself */
469 if (!grid_n) return (FALSE);
472 y = GRID_Y(grid_g[grid_n-1]);
473 x = GRID_X(grid_g[grid_n-1]);
475 /* May not end in an unrequested grid */
476 if ((y != y2) || (x != x2)) return (FALSE);
478 for (i = 0; i < grid_n; i++)
480 y = GRID_Y(grid_g[i]);
481 x = GRID_X(grid_g[i]);
483 if ((cave[y][x].m_idx > 0) && !((y == y2) && (x == x2)))
485 monster_type *m_ptr = &m_list[cave[y][x].m_idx];
486 if (is_friend == is_pet(m_ptr))
491 /* Pets may not shoot through the character - TNB */
492 if (player_bold(y, x))
494 if (is_friend) return (FALSE);
502 * @brief モンスターのボルト型魔法処理 /
503 * Cast a bolt at the player Stop if we hit a monster Affect monsters and the player
504 * @param m_idx モンスターのID
509 * @param monspell モンスター魔法のID
510 * @param target_type モンスターからモンスターへ撃つならMONSTER_TO_MONSTER、モンスターからプレイヤーならMONSTER_TO_PLAYER
513 void bolt(MONSTER_IDX m_idx, POSITION y, POSITION x, EFFECT_ID typ, int dam_hp, int monspell, int target_type)
516 bool learnable = spell_learnable(m_idx);
520 case MONSTER_TO_MONSTER:
521 flg = PROJECT_STOP | PROJECT_KILL;
523 case MONSTER_TO_PLAYER:
524 flg = PROJECT_STOP | PROJECT_KILL | PROJECT_PLAYER;
527 if (typ != GF_ARROW) flg |= PROJECT_REFLECTABLE;
529 /* Target the player with a bolt attack */
530 (void)project(m_idx, 0, y, x, dam_hp, typ, flg, (learnable ? monspell : -1));
534 * @brief モンスターのビーム型魔法処理 /
535 * @param m_idx モンスターのID
540 * @param monspell モンスター魔法のID
541 * @param target_type モンスターからモンスターへ撃つならMONSTER_TO_MONSTER、モンスターからプレイヤーならMONSTER_TO_PLAYER
544 void beam(MONSTER_IDX m_idx, POSITION y, POSITION x, EFFECT_ID typ, int dam_hp, int monspell, int target_type)
547 bool learnable = spell_learnable(m_idx);
551 case MONSTER_TO_MONSTER:
552 flg = PROJECT_BEAM | PROJECT_KILL | PROJECT_THRU;
554 case MONSTER_TO_PLAYER:
555 flg = PROJECT_BEAM | PROJECT_KILL | PROJECT_THRU | PROJECT_PLAYER;
559 /* Target the player with a bolt attack */
560 (void)project(m_idx, 0, y, x, dam_hp, typ, flg, (learnable ? monspell : -1));
565 * @brief モンスターのボール型&ブレス型魔法処理 /
566 * 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
569 * @param m_idx モンスターのID
574 * @param monspell モンスター魔法のID
575 * @param target_type モンスターからモンスターへ撃つならMONSTER_TO_MONSTER、モンスターからプレイヤーならMONSTER_TO_PLAYER
578 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)
580 monster_type *m_ptr = &m_list[m_idx];
581 monster_race *r_ptr = &r_info[m_ptr->r_idx];
582 bool learnable = spell_learnable(m_idx);
583 BIT_FLAGS flg = 0x00;
587 case MONSTER_TO_MONSTER:
588 flg = PROJECT_GRID | PROJECT_ITEM | PROJECT_KILL;
590 case MONSTER_TO_PLAYER:
591 flg = PROJECT_GRID | PROJECT_ITEM | PROJECT_KILL | PROJECT_PLAYER;
595 /* Determine the radius of the blast */
596 if ((rad < 1) && breath) rad = (r_ptr->flags2 & (RF2_POWERFUL)) ? 3 : 2;
598 /* Handle breath attacks */
599 if (breath) rad = 0 - rad;
614 flg |= (PROJECT_HIDE | PROJECT_AIMED);
618 /* Target the player with a ball attack */
619 (void)project(m_idx, rad, y, x, dam_hp, typ, flg, (learnable ? monspell : -1));
626 * @brief ID値が正しいモンスター魔法IDかどうかを返す /
627 * Return TRUE if a spell is good for hurting the player (directly).
628 * @param spell 判定対象のID
629 * @return 正しいIDならばTRUEを返す。
631 static bool spell_attack(byte spell)
633 /* All RF4 spells hurt (except for shriek and dispel) */
634 if (spell < 128 && spell > 98) return (TRUE);
636 /* Various "ball" spells */
637 if (spell >= 128 && spell <= 128 + 8) return (TRUE);
639 /* "Cause wounds" and "bolt" spells */
640 if (spell >= 128 + 12 && spell < 128 + 27) return (TRUE);
643 if (spell == 160 + 1) return (TRUE);
646 if (spell == 160 + 11) return (TRUE);
654 * @brief ID値が退避目的に適したモンスター魔法IDかどうかを返す /
655 * Return TRUE if a spell is good for escaping.
656 * @param spell 判定対象のID
657 * @return 適した魔法のIDならばTRUEを返す。
659 static bool spell_escape(byte spell)
661 /* Blink or Teleport */
662 if (spell == 160 + 4 || spell == 160 + 5) return (TRUE);
664 /* Teleport the player away */
665 if (spell == 160 + 9 || spell == 160 + 10) return (TRUE);
667 /* Isn't good for escaping */
672 * @brief ID値が妨害目的に適したモンスター魔法IDかどうかを返す /
673 * Return TRUE if a spell is good for annoying the player.
674 * @param spell 判定対象のID
675 * @return 適した魔法のIDならばTRUEを返す。
677 static bool spell_annoy(byte spell)
680 if (spell == 96 + 0) return (TRUE);
682 /* Brain smash, et al (added curses) */
683 if (spell >= 128 + 9 && spell <= 128 + 14) return (TRUE);
685 /* Scare, confuse, blind, slow, paralyze */
686 if (spell >= 128 + 27 && spell <= 128 + 31) return (TRUE);
689 if (spell == 160 + 8) return (TRUE);
692 if (spell == 160 + 10) return (TRUE);
694 /* Darkness, make traps, cause amnesia */
695 if (spell >= 160 + 12 && spell <= 160 + 14) return (TRUE);
702 * @brief ID値が召喚型のモンスター魔法IDかどうかを返す /
703 * Return TRUE if a spell is good for annoying the player.
704 * @param spell 判定対象のID
705 * @return 召喚型魔法のIDならばTRUEを返す。
707 static bool spell_summon(byte spell)
709 /* All summon spells */
710 if (spell >= 160 + 16) return (TRUE);
718 * @brief ID値が死者復活処理かどうかを返す /
719 * Return TRUE if a spell is good for annoying the player.
720 * @param spell 判定対象のID
721 * @return 死者復活の処理ならばTRUEを返す。
723 static bool spell_raise(byte spell)
725 /* All raise-dead spells */
726 if (spell == 160 + 15) return (TRUE);
733 * @brief ID値が戦術的なモンスター魔法IDかどうかを返す /
734 * Return TRUE if a spell is good in a tactical situation.
735 * @param spell 判定対象のID
736 * @return 戦術的な魔法のIDならばTRUEを返す。
738 static bool spell_tactic(byte spell)
741 if (spell == 160 + 4) return (TRUE);
748 * @brief ID値が無敵化するモンスター魔法IDかどうかを返す /
749 * Return TRUE if a spell makes invulnerable.
750 * @param spell 判定対象のID
751 * @return 召喚型魔法のIDならばTRUEを返す。
753 static bool spell_invulner(byte spell)
755 /* Invulnerability */
756 if (spell == 160 + 3) return (TRUE);
758 /* No invulnerability */
763 * @brief ID値が加速するモンスター魔法IDかどうかを返す /
764 * Return TRUE if a spell hastes.
765 * @param spell 判定対象のID
766 * @return 召喚型魔法のIDならばTRUEを返す。
768 static bool spell_haste(byte spell)
771 if (spell == 160 + 0) return (TRUE);
773 /* Not a haste spell */
779 * @brief ID値が時間停止を行うモンスター魔法IDかどうかを返す /
780 * Return TRUE if a spell world.
781 * @param spell 判定対象のID
782 * @return 時間停止魔法のIDならばTRUEを返す。
784 static bool spell_world(byte spell)
786 if (spell == 160 + 6) return (TRUE);
792 * @brief ID値が特別効果のモンスター魔法IDかどうかを返す /
793 * Return TRUE if a spell special.
794 * @param spell 判定対象のID
795 * @return 特別効果魔法のIDならばTRUEを返す。
797 static bool spell_special(byte spell)
799 if (p_ptr->inside_battle) return FALSE;
800 if (spell == 160 + 7) return (TRUE);
806 * @brief ID値が光の剣のモンスター魔法IDかどうかを返す /
807 * Return TRUE if a spell psycho-spear.
808 * @param spell 判定対象のID
809 * @return 光の剣のIDならばTRUEを返す。
811 static bool spell_psy_spe(byte spell)
814 if (spell == 160 + 11) return (TRUE);
816 /* Not a haste spell */
822 * @brief ID値が治癒魔法かどうかを返す /
823 * Return TRUE if a spell is good for healing.
824 * @param spell 判定対象のID
825 * @return 治癒魔法のIDならばTRUEを返す。
827 static bool spell_heal(byte spell)
830 if (spell == 160 + 2) return (TRUE);
838 * @brief ID値が魔力消去かどうかを返す /
839 * Return TRUE if a spell is good for dispel.
840 * @param spell 判定対象のID
841 * @return 魔力消去のIDならばTRUEを返す。
843 static bool spell_dispel(byte spell)
846 if (spell == 96 + 2) return (TRUE);
854 * @brief モンスターがプレイヤーに魔力消去を与えるべきかを判定するルーチン
855 * Check should monster cast dispel spell.
856 * @param m_idx モンスターの構造体配列ID
857 * @return 魔力消去をかけるべきならTRUEを返す。
859 bool dispel_check(MONSTER_IDX m_idx)
861 monster_type *m_ptr = &m_list[m_idx];
862 monster_race *r_ptr = &r_info[m_ptr->r_idx];
864 /* Invulnabilty (including the song) */
865 if (IS_INVULN()) return (TRUE);
868 if (p_ptr->wraith_form) return (TRUE);
871 if (p_ptr->shield) return (TRUE);
874 if (p_ptr->magicdef) return (TRUE);
877 if (p_ptr->multishadow) return (TRUE);
880 if (p_ptr->dustrobe) return (TRUE);
882 /* Berserk Strength */
883 if (p_ptr->shero && (p_ptr->pclass != CLASS_BERSERKER)) return (TRUE);
886 if (p_ptr->mimic_form == MIMIC_DEMON_LORD) return (TRUE);
888 /* Elemental resistances */
889 if (r_ptr->flags4 & RF4_BR_ACID)
891 if (!p_ptr->immune_acid && (p_ptr->oppose_acid || music_singing(MUSIC_RESIST))) return (TRUE);
892 if (p_ptr->special_defense & DEFENSE_ACID) return (TRUE);
895 if (r_ptr->flags4 & RF4_BR_FIRE)
897 if (!((p_ptr->prace == RACE_DEMON) && p_ptr->lev > 44))
899 if (!p_ptr->immune_fire && (p_ptr->oppose_fire || music_singing(MUSIC_RESIST))) return (TRUE);
900 if (p_ptr->special_defense & DEFENSE_FIRE) return (TRUE);
904 if (r_ptr->flags4 & RF4_BR_ELEC)
906 if (!p_ptr->immune_elec && (p_ptr->oppose_elec || music_singing(MUSIC_RESIST))) return (TRUE);
907 if (p_ptr->special_defense & DEFENSE_ELEC) return (TRUE);
910 if (r_ptr->flags4 & RF4_BR_COLD)
912 if (!p_ptr->immune_cold && (p_ptr->oppose_cold || music_singing(MUSIC_RESIST))) return (TRUE);
913 if (p_ptr->special_defense & DEFENSE_COLD) return (TRUE);
916 if (r_ptr->flags4 & (RF4_BR_POIS | RF4_BR_NUKE))
918 if (!((p_ptr->pclass == CLASS_NINJA) && p_ptr->lev > 44))
920 if (p_ptr->oppose_pois || music_singing(MUSIC_RESIST)) return (TRUE);
921 if (p_ptr->special_defense & DEFENSE_POIS) return (TRUE);
925 /* Ultimate resistance */
926 if (p_ptr->ult_res) return (TRUE);
928 /* Potion of Neo Tsuyosi special */
929 if (p_ptr->tsuyoshi) return (TRUE);
931 /* Elemental Brands */
932 if ((p_ptr->special_attack & ATTACK_ACID) && !(r_ptr->flagsr & RFR_EFF_IM_ACID_MASK)) return (TRUE);
933 if ((p_ptr->special_attack & ATTACK_FIRE) && !(r_ptr->flagsr & RFR_EFF_IM_FIRE_MASK)) return (TRUE);
934 if ((p_ptr->special_attack & ATTACK_ELEC) && !(r_ptr->flagsr & RFR_EFF_IM_ELEC_MASK)) return (TRUE);
935 if ((p_ptr->special_attack & ATTACK_COLD) && !(r_ptr->flagsr & RFR_EFF_IM_COLD_MASK)) return (TRUE);
936 if ((p_ptr->special_attack & ATTACK_POIS) && !(r_ptr->flagsr & RFR_EFF_IM_POIS_MASK)) return (TRUE);
939 if (p_ptr->pspeed < 145)
941 if (IS_FAST()) return (TRUE);
945 if (p_ptr->lightspeed && (m_ptr->mspeed < 136)) return (TRUE);
947 if (p_ptr->riding && (m_list[p_ptr->riding].mspeed < 135))
949 if (MON_FAST(&m_list[p_ptr->riding])) return (TRUE);
952 /* No need to cast dispel spell */
958 * @brief モンスターの魔法選択ルーチン
959 * Have a monster choose a spell from a list of "useful" spells.
960 * @param m_idx モンスターの構造体配列ID
961 * @param spells 候補魔法IDをまとめた配列
962 * @param num spellsの長さ
963 * @return 選択したモンスター魔法のID
965 * Note that this list does NOT include spells that will just hit\n
966 * other monsters, and the list is restricted when the monster is\n
967 * "desperate". Should that be the job of this function instead?\n
969 * Stupid monsters will just pick a spell randomly. Smart monsters\n
970 * will choose more "intelligently".\n
972 * Use the helper functions above to put spells into categories.\n
974 * This function may well be an efficiency bottleneck.\n
976 static int choose_attack_spell(MONSTER_IDX m_idx, byte spells[], byte num)
978 monster_type *m_ptr = &m_list[m_idx];
979 monster_race *r_ptr = &r_info[m_ptr->r_idx];
981 byte escape[96], escape_num = 0;
982 byte attack[96], attack_num = 0;
983 byte summon[96], summon_num = 0;
984 byte tactic[96], tactic_num = 0;
985 byte annoy[96], annoy_num = 0;
986 byte invul[96], invul_num = 0;
987 byte haste[96], haste_num = 0;
988 byte world[96], world_num = 0;
989 byte special[96], special_num = 0;
990 byte psy_spe[96], psy_spe_num = 0;
991 byte raise[96], raise_num = 0;
992 byte heal[96], heal_num = 0;
993 byte dispel[96], dispel_num = 0;
997 /* Stupid monsters choose randomly */
998 if (r_ptr->flags2 & (RF2_STUPID))
1000 /* Pick at random */
1001 return (spells[randint0(num)]);
1004 /* Categorize spells */
1005 for (i = 0; i < num; i++)
1008 if (spell_escape(spells[i])) escape[escape_num++] = spells[i];
1011 if (spell_attack(spells[i])) attack[attack_num++] = spells[i];
1014 if (spell_summon(spells[i])) summon[summon_num++] = spells[i];
1016 /* Tactical spell? */
1017 if (spell_tactic(spells[i])) tactic[tactic_num++] = spells[i];
1019 /* Annoyance spell? */
1020 if (spell_annoy(spells[i])) annoy[annoy_num++] = spells[i];
1022 /* Invulnerability spell? */
1023 if (spell_invulner(spells[i])) invul[invul_num++] = spells[i];
1026 if (spell_haste(spells[i])) haste[haste_num++] = spells[i];
1029 if (spell_world(spells[i])) world[world_num++] = spells[i];
1031 /* Special spell? */
1032 if (spell_special(spells[i])) special[special_num++] = spells[i];
1034 /* Psycho-spear spell? */
1035 if (spell_psy_spe(spells[i])) psy_spe[psy_spe_num++] = spells[i];
1037 /* Raise-dead spell? */
1038 if (spell_raise(spells[i])) raise[raise_num++] = spells[i];
1041 if (spell_heal(spells[i])) heal[heal_num++] = spells[i];
1044 if (spell_dispel(spells[i])) dispel[dispel_num++] = spells[i];
1047 /*** Try to pick an appropriate spell type ***/
1050 if (world_num && (randint0(100) < 15) && !world_monster)
1052 /* Choose haste spell */
1053 return (world[randint0(world_num)]);
1059 bool success = FALSE;
1060 switch(m_ptr->r_idx)
1064 if ((m_ptr->hp < m_ptr->maxhp / 2) && r_info[MON_BANOR].max_num && r_info[MON_LUPART].max_num) success = TRUE;
1068 if (success) return (special[randint0(special_num)]);
1071 /* Still hurt badly, couldn't flee, attempt to heal */
1072 if (m_ptr->hp < m_ptr->maxhp / 3 && one_in_(2))
1074 /* Choose heal spell if possible */
1075 if (heal_num) return (heal[randint0(heal_num)]);
1078 /* Hurt badly or afraid, attempt to flee */
1079 if (((m_ptr->hp < m_ptr->maxhp / 3) || MON_MONFEAR(m_ptr)) && one_in_(2))
1081 /* Choose escape spell if possible */
1082 if (escape_num) return (escape[randint0(escape_num)]);
1088 bool success = FALSE;
1089 switch (m_ptr->r_idx)
1095 case MON_BANORLUPART:
1096 if (randint0(100) < 70) success = TRUE;
1099 if (randint0(100) < 40) success = TRUE;
1102 if (randint0(100) < 50) success = TRUE;
1105 if (success) return (special[randint0(special_num)]);
1108 /* Player is close and we have attack spells, blink away */
1109 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)
1111 /* Choose tactical spell */
1112 if (tactic_num) return (tactic[randint0(tactic_num)]);
1115 /* Summon if possible (sometimes) */
1116 if (summon_num && (randint0(100) < 40))
1118 /* Choose summon spell */
1119 return (summon[randint0(summon_num)]);
1123 if (dispel_num && one_in_(2))
1125 /* Choose dispel spell if possible */
1126 if (dispel_check(m_idx))
1128 return (dispel[randint0(dispel_num)]);
1132 /* Raise-dead if possible (sometimes) */
1133 if (raise_num && (randint0(100) < 40))
1135 /* Choose raise-dead spell */
1136 return (raise[randint0(raise_num)]);
1139 /* Attack spell (most of the time) */
1142 if (psy_spe_num && (randint0(100) < 50))
1144 /* Choose attack spell */
1145 return (psy_spe[randint0(psy_spe_num)]);
1147 else if (attack_num && (randint0(100) < 40))
1149 /* Choose attack spell */
1150 return (attack[randint0(attack_num)]);
1153 else if (attack_num && (randint0(100) < 85))
1155 /* Choose attack spell */
1156 return (attack[randint0(attack_num)]);
1159 /* Try another tactical spell (sometimes) */
1160 if (tactic_num && (randint0(100) < 50) && !world_monster)
1162 /* Choose tactic spell */
1163 return (tactic[randint0(tactic_num)]);
1166 /* Cast globe of invulnerability if not already in effect */
1167 if (invul_num && !m_ptr->mtimed[MTIMED_INVULNER] && (randint0(100) < 50))
1169 /* Choose Globe of Invulnerability */
1170 return (invul[randint0(invul_num)]);
1173 /* We're hurt (not badly), try to heal */
1174 if ((m_ptr->hp < m_ptr->maxhp * 3 / 4) && (randint0(100) < 25))
1176 /* Choose heal spell if possible */
1177 if (heal_num) return (heal[randint0(heal_num)]);
1180 /* Haste self if we aren't already somewhat hasted (rarely) */
1181 if (haste_num && (randint0(100) < 20) && !MON_FAST(m_ptr))
1183 /* Choose haste spell */
1184 return (haste[randint0(haste_num)]);
1187 /* Annoy player (most of the time) */
1188 if (annoy_num && (randint0(100) < 80))
1190 /* Choose annoyance spell */
1191 return (annoy[randint0(annoy_num)]);
1194 /* Choose no spell */
1200 * @brief ID値が非魔術的な特殊技能かどうかを返す /
1201 * Return TRUE if a spell is inate spell.
1202 * @param spell 判定対象のID
1203 * @return 非魔術的な特殊技能ならばTRUEを返す。
1205 bool spell_is_inate(SPELL_IDX spell)
1207 if (spell < 32 * 4) /* Set RF4 */
1209 if ((1L << (spell - 32 * 3)) & RF4_NOMAGIC_MASK) return TRUE;
1211 else if (spell < 32 * 5) /* Set RF5 */
1213 if ((1L << (spell - 32 * 4)) & RF5_NOMAGIC_MASK) return TRUE;
1215 else if (spell < 32 * 6) /* Set RF6 */
1217 if ((1L << (spell - 32 * 5)) & RF6_NOMAGIC_MASK) return TRUE;
1220 /* This spell is not "inate" */
1226 * @brief モンスターがプレイヤーにダメージを与えるための最適な座標を算出する /
1227 * @param m_ptr 技能を使用するモンスター構造体の参照ポインタ
1228 * @param yp 最適な目標地点のY座標を返す参照ポインタ
1229 * @param xp 最適な目標地点のX座標を返す参照ポインタ
1230 * @param f_flag 射線に入れるのを避ける地形の所持フラグ
1231 * @param path_check 射線を判定するための関数ポインタ
1232 * @return 有効な座標があった場合TRUEを返す
1234 static bool adjacent_grid_check(monster_type *m_ptr, POSITION *yp, POSITION *xp,
1235 int f_flag, bool (*path_check)(POSITION, POSITION, POSITION, POSITION))
1239 static int tonari_y[4][8] = {{-1, -1, -1, 0, 0, 1, 1, 1},
1240 {-1, -1, -1, 0, 0, 1, 1, 1},
1241 { 1, 1, 1, 0, 0, -1, -1, -1},
1242 { 1, 1, 1, 0, 0, -1, -1, -1}};
1243 static int tonari_x[4][8] = {{-1, 0, 1, -1, 1, -1, 0, 1},
1244 { 1, 0, -1, 1, -1, 1, 0, -1},
1245 {-1, 0, 1, -1, 1, -1, 0, 1},
1246 { 1, 0, -1, 1, -1, 1, 0, -1}};
1248 if (m_ptr->fy < p_ptr->y && m_ptr->fx < p_ptr->x) tonari = 0;
1249 else if (m_ptr->fy < p_ptr->y) tonari = 1;
1250 else if (m_ptr->fx < p_ptr->x) tonari = 2;
1253 for (i = 0; i < 8; i++)
1255 int next_x = *xp + tonari_x[tonari][i];
1256 int next_y = *yp + tonari_y[tonari][i];
1259 /* Access the next grid */
1260 c_ptr = &cave[next_y][next_x];
1262 /* Skip this feature */
1263 if (!cave_have_flag_grid(c_ptr, f_flag)) continue;
1265 if (path_check(m_ptr->fy, m_ptr->fx, next_y, next_x))
1276 #define DO_SPELL_NONE 0
1277 #define DO_SPELL_BR_LITE 1
1278 #define DO_SPELL_BR_DISI 2
1279 #define DO_SPELL_BA_LITE 3
1282 * @brief モンスターの特殊技能メインルーチン /
1283 * Creatures can cast spells, shoot missiles, and breathe.
1284 * @param m_idx モンスター構造体配列のID
1285 * @return 実際に特殊技能を利用したらTRUEを返す
1287 * Returns "TRUE" if a spell (or whatever) was (successfully) cast.\n
1289 * This function could use some work, but remember to\n
1290 * keep it as optimized as possible, while retaining generic code.\n
1292 * Verify the various "blind-ness" checks in the code.\n
1294 * Note that several effects should really not be "seen"\n
1295 * if the player is blind. See also "effects.c" for other "mistakes".\n
1297 * Perhaps monsters should breathe at locations *near* the player,\n
1298 * since this would allow them to inflict "partial" damage.\n
1300 * Perhaps smart monsters should decline to use "bolt" spells if\n
1301 * there is a monster in the way, unless they wish to kill it.\n
1303 * Note that, to allow the use of the "track_target" option at some\n
1304 * later time, certain non-optimal things are done in the code below,\n
1305 * including explicit checks against the "direct" variable, which is\n
1306 * currently always true by the time it is checked, but which should\n
1307 * really be set according to an explicit "projectable()" test, and\n
1308 * the use of generic "x,y" locations instead of the player location,\n
1309 * with those values being initialized with the player location.\n
1311 * It will not be possible to "correctly" handle the case in which a\n
1312 * monster attempts to attack a location which is thought to contain\n
1313 * the player, but which in fact is nowhere near the player, since this\n
1314 * might induce all sorts of messages about the attack itself, and about\n
1315 * the effects of the attack, which the player might or might not be in\n
1316 * a position to observe. Thus, for simplicity, it is probably best to\n
1317 * only allow "faulty" attacks by a monster if one of the important grids\n
1318 * (probably the initial or final grid) is in fact in view of the player.\n
1319 * It may be necessary to actually prevent spell attacks except when the\n
1320 * monster actually has line of sight to the player. Note that a monster\n
1321 * could be left in a bizarre situation after the player ducked behind a\n
1322 * pillar and then teleported away, for example.\n
1325 * that certain spell attacks do not use the "project()" function\n
1326 * but "simulate" it via the "direct" variable, which is always at least\n
1327 * as restrictive as the "project()" function. This is necessary to\n
1328 * prevent "blindness" attacks and such from bending around walls, etc,\n
1329 * and to allow the use of the "track_target" option in the future.\n
1331 * Note that this function attempts to optimize the use of spells for the\n
1332 * cases in which the monster has no spells, or has spells but cannot use\n
1333 * them, or has spells but they will have no "useful" effect. Note that\n
1334 * this function has been an efficiency bottleneck in the past.\n
1336 * Note the special "MFLAG_NICE" flag, which prevents a monster from using\n
1337 * any spell attacks until the player has had a single chance to move.\n
1339 bool make_attack_spell(MONSTER_IDX m_idx)
1342 SPELL_IDX thrown_spell = 0;
1344 PERCENTAGE failrate;
1345 byte spell[96], num = 0;
1346 BIT_FLAGS f4, f5, f6;
1347 monster_type *m_ptr = &m_list[m_idx];
1348 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1349 GAME_TEXT m_name[MAX_NLEN];
1353 bool no_inate = FALSE;
1354 bool do_spell = DO_SPELL_NONE;
1357 POSITION x = p_ptr->x;
1358 POSITION y = p_ptr->y;
1360 /* Target location for lite breath */
1361 POSITION x_br_lite = 0;
1362 POSITION y_br_lite = 0;
1364 /* Extract the "see-able-ness" */
1365 bool seen = (!p_ptr->blind && m_ptr->ml);
1366 bool maneable = player_has_los_bold(m_ptr->fy, m_ptr->fx);
1368 /* Check "projectable" */
1371 bool in_no_magic_dungeon = (d_info[dungeon_type].flags1 & DF1_NO_MAGIC) && dun_level
1372 && (!p_ptr->inside_quest || is_fixed_quest_idx(p_ptr->inside_quest));
1374 bool can_use_lite_area = FALSE;
1378 /* Cannot cast spells when confused */
1379 if (MON_CONFUSED(m_ptr))
1381 reset_target(m_ptr);
1385 /* Cannot cast spells when nice */
1386 if (m_ptr->mflag & MFLAG_NICE) return (FALSE);
1387 if (!is_hostile(m_ptr)) return (FALSE);
1390 /* Sometimes forbid inate attacks (breaths) */
1391 if (randint0(100) >= (r_ptr->freq_spell * 2)) no_inate = TRUE;
1393 /* Handle "track_target" option (?) */
1396 /* Extract the racial spell flags */
1398 f5 = r_ptr->a_ability_flags1;
1399 f6 = r_ptr->a_ability_flags2;
1401 /*** require projectable player ***/
1404 if ((m_ptr->cdis > MAX_RANGE) && !m_ptr->target_y) return (FALSE);
1406 /* Check path for lite breath */
1407 if (f4 & RF4_BR_LITE)
1412 if (los(m_ptr->fy, m_ptr->fx, y_br_lite, x_br_lite))
1414 feature_type *f_ptr = &f_info[cave[y_br_lite][x_br_lite].feat];
1416 if (!have_flag(f_ptr->flags, FF_LOS))
1418 if (have_flag(f_ptr->flags, FF_PROJECT) && one_in_(2)) f4 &= ~(RF4_BR_LITE);
1422 /* Check path to next grid */
1423 else if (!adjacent_grid_check(m_ptr, &y_br_lite, &x_br_lite, FF_LOS, los)) f4 &= ~(RF4_BR_LITE);
1425 /* Don't breath lite to the wall if impossible */
1426 if (!(f4 & RF4_BR_LITE))
1434 if (projectable(m_ptr->fy, m_ptr->fx, y, x))
1436 feature_type *f_ptr = &f_info[cave[y][x].feat];
1438 if (!have_flag(f_ptr->flags, FF_PROJECT))
1440 /* Breath disintegration to the wall if possible */
1441 if ((f4 & RF4_BR_DISI) && have_flag(f_ptr->flags, FF_HURT_DISI) && one_in_(2)) do_spell = DO_SPELL_BR_DISI;
1443 /* Breath lite to the transparent wall if possible */
1444 else if ((f4 & RF4_BR_LITE) && have_flag(f_ptr->flags, FF_LOS) && one_in_(2)) do_spell = DO_SPELL_BR_LITE;
1448 /* Check path to next grid */
1451 bool success = FALSE;
1453 if ((f4 & RF4_BR_DISI) && (m_ptr->cdis < MAX_RANGE/2) &&
1454 in_disintegration_range(m_ptr->fy, m_ptr->fx, y, x) &&
1455 (one_in_(10) || (projectable(y, x, m_ptr->fy, m_ptr->fx) && one_in_(2))))
1457 do_spell = DO_SPELL_BR_DISI;
1460 else if ((f4 & RF4_BR_LITE) && (m_ptr->cdis < MAX_RANGE/2) &&
1461 los(m_ptr->fy, m_ptr->fx, y, x) && one_in_(5))
1463 do_spell = DO_SPELL_BR_LITE;
1466 else if ((f5 & RF5_BA_LITE) && (m_ptr->cdis <= MAX_RANGE))
1468 POSITION by = y, bx = x;
1469 get_project_point(m_ptr->fy, m_ptr->fx, &by, &bx, 0L);
1470 if ((distance(by, bx, y, x) <= 3) && los(by, bx, y, x) && one_in_(5))
1472 do_spell = DO_SPELL_BA_LITE;
1477 if (!success) success = adjacent_grid_check(m_ptr, &y, &x, FF_PROJECT, projectable);
1481 if (m_ptr->target_y && m_ptr->target_x)
1483 y = m_ptr->target_y;
1484 x = m_ptr->target_x;
1485 f4 &= (RF4_INDIRECT_MASK);
1486 f5 &= (RF5_INDIRECT_MASK);
1487 f6 &= (RF6_INDIRECT_MASK);
1491 if (y_br_lite && x_br_lite && (m_ptr->cdis < MAX_RANGE/2) && one_in_(5))
1497 do_spell = DO_SPELL_BR_LITE;
1500 else f4 |= (RF4_BR_LITE);
1505 if (!success) return FALSE;
1508 reset_target(m_ptr);
1510 rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
1512 /* Forbid inate attacks sometimes */
1515 f4 &= ~(RF4_NOMAGIC_MASK);
1516 f5 &= ~(RF5_NOMAGIC_MASK);
1517 f6 &= ~(RF6_NOMAGIC_MASK);
1520 if (f6 & RF6_DARKNESS)
1522 if ((p_ptr->pclass == CLASS_NINJA) &&
1523 !(r_ptr->flags3 & (RF3_UNDEAD | RF3_HURT_LITE)) &&
1524 !(r_ptr->flags7 & RF7_DARK_MASK))
1525 can_use_lite_area = TRUE;
1527 if (!(r_ptr->flags2 & RF2_STUPID))
1529 if (d_info[dungeon_type].flags1 & DF1_DARKNESS) f6 &= ~(RF6_DARKNESS);
1530 else if ((p_ptr->pclass == CLASS_NINJA) && !can_use_lite_area) f6 &= ~(RF6_DARKNESS);
1534 if (in_no_magic_dungeon && !(r_ptr->flags2 & RF2_STUPID))
1536 f4 &= (RF4_NOMAGIC_MASK);
1537 f5 &= (RF5_NOMAGIC_MASK);
1538 f6 &= (RF6_NOMAGIC_MASK);
1541 if (r_ptr->flags2 & RF2_SMART)
1543 /* Hack -- allow "desperate" spells */
1544 if ((m_ptr->hp < m_ptr->maxhp / 10) &&
1545 (randint0(100) < 50))
1547 /* Require intelligent spells */
1548 f4 &= (RF4_INT_MASK);
1549 f5 &= (RF5_INT_MASK);
1550 f6 &= (RF6_INT_MASK);
1553 /* Hack -- decline "teleport level" in some case */
1554 if ((f6 & RF6_TELE_LEVEL) && TELE_LEVEL_IS_INEFF(0))
1556 f6 &= ~(RF6_TELE_LEVEL);
1560 /* No spells left */
1561 if (!f4 && !f5 && !f6) return (FALSE);
1563 /* Remove the "ineffective" spells */
1564 remove_bad_spells(m_idx, &f4, &f5, &f6);
1566 if (p_ptr->inside_arena || p_ptr->inside_battle)
1568 f4 &= ~(RF4_SUMMON_MASK);
1569 f5 &= ~(RF5_SUMMON_MASK);
1570 f6 &= ~(RF6_SUMMON_MASK | RF6_TELE_LEVEL);
1572 if (m_ptr->r_idx == MON_ROLENTO) f6 &= ~(RF6_SPECIAL);
1575 /* No spells left */
1576 if (!f4 && !f5 && !f6) return (FALSE);
1578 if (!(r_ptr->flags2 & RF2_STUPID))
1580 if (!p_ptr->csp) f5 &= ~(RF5_DRAIN_MANA);
1582 /* Check for a clean bolt shot */
1583 if (((f4 & RF4_BOLT_MASK) ||
1584 (f5 & RF5_BOLT_MASK) ||
1585 (f6 & RF6_BOLT_MASK)) &&
1586 !clean_shot(m_ptr->fy, m_ptr->fx, p_ptr->y, p_ptr->x, FALSE))
1588 /* Remove spells that will only hurt friends */
1589 f4 &= ~(RF4_BOLT_MASK);
1590 f5 &= ~(RF5_BOLT_MASK);
1591 f6 &= ~(RF6_BOLT_MASK);
1594 /* Check for a possible summon */
1595 if (((f4 & RF4_SUMMON_MASK) ||
1596 (f5 & RF5_SUMMON_MASK) ||
1597 (f6 & RF6_SUMMON_MASK)) &&
1598 !(summon_possible(y, x)))
1600 /* Remove summoning spells */
1601 f4 &= ~(RF4_SUMMON_MASK);
1602 f5 &= ~(RF5_SUMMON_MASK);
1603 f6 &= ~(RF6_SUMMON_MASK);
1606 /* Check for a possible raise dead */
1607 if ((f6 & RF6_RAISE_DEAD) && !raise_possible(m_ptr))
1609 /* Remove raise dead spell */
1610 f6 &= ~(RF6_RAISE_DEAD);
1613 /* Special moves restriction */
1614 if (f6 & RF6_SPECIAL)
1616 if ((m_ptr->r_idx == MON_ROLENTO) && !summon_possible(y, x))
1618 f6 &= ~(RF6_SPECIAL);
1622 /* No spells left */
1623 if (!f4 && !f5 && !f6) return (FALSE);
1626 /* Extract the "inate" spells */
1627 for (k = 0; k < 32; k++)
1629 if (f4 & (1L << k)) spell[num++] = k + RF4_SPELL_START;
1632 /* Extract the "normal" spells */
1633 for (k = 0; k < 32; k++)
1635 if (f5 & (1L << k)) spell[num++] = k + RF5_SPELL_START;
1638 /* Extract the "bizarre" spells */
1639 for (k = 0; k < 32; k++)
1641 if (f6 & (1L << k)) spell[num++] = k + RF6_SPELL_START;
1644 /* No spells left */
1645 if (!num) return (FALSE);
1647 /* Stop if player is dead or gone */
1648 if (!p_ptr->playing || p_ptr->is_dead) return (FALSE);
1650 /* Stop if player is leaving */
1651 if (p_ptr->leaving) return (FALSE);
1653 /* Get the monster name (or "it") */
1654 monster_desc(m_name, m_ptr, 0x00);
1657 /* Get the monster possessive ("his"/"her"/"its") */
1658 monster_desc(m_poss, m_ptr, MD_PRON_VISIBLE | MD_POSSESSIVE);
1668 thrown_spell = choose_attack_spell(m_idx, spell, num);
1669 if (thrown_spell) break;
1674 case DO_SPELL_BR_LITE:
1675 thrown_spell = 96+14; /* RF4_BR_LITE */
1678 case DO_SPELL_BR_DISI:
1679 thrown_spell = 96+31; /* RF4_BR_DISI */
1682 case DO_SPELL_BA_LITE:
1683 thrown_spell = 128+20; /* RF5_BA_LITE */
1687 return FALSE; /* Paranoia */
1690 /* Abort if no spell was chosen */
1691 if (!thrown_spell) return (FALSE);
1693 /* Calculate spell failure rate */
1694 failrate = 25 - (rlev + 3) / 4;
1696 /* Hack -- Stupid monsters will never fail (for jellies and such) */
1697 if (r_ptr->flags2 & RF2_STUPID) failrate = 0;
1699 /* Check for spell failure (inate attacks never fail) */
1700 if (!spell_is_inate(thrown_spell)
1701 && (in_no_magic_dungeon || (MON_STUNNED(m_ptr) && one_in_(2)) || (randint0(100) < failrate)))
1703 disturb(TRUE, TRUE);
1704 msg_format(_("%^sは呪文を唱えようとしたが失敗した。", "%^s tries to cast a spell, but fails."), m_name);
1709 /* Hex: Anti Magic Barrier */
1710 if (!spell_is_inate(thrown_spell) && magic_barrier(m_idx))
1712 msg_format(_("反魔法バリアが%^sの呪文をかき消した。", "Anti magic barrier cancels the spell which %^s casts."), m_name);
1717 direct = player_bold(y, x);
1719 can_remember = is_original_ap_and_seen(m_ptr);
1723 switch (thrown_spell)
1725 case 96 + 2: /* RF4_DISPEL */
1726 case 96 + 4: /* RF4_SHOOT */
1727 case 128 + 9: /* RF5_DRAIN_MANA */
1728 case 128 + 10: /* RF5_MIND_BLAST */
1729 case 128 + 11: /* RF5_BRAIN_SMASH */
1730 case 128 + 12: /* RF5_CAUSE_1 */
1731 case 128 + 13: /* RF5_CAUSE_2 */
1732 case 128 + 14: /* RF5_CAUSE_3 */
1733 case 128 + 15: /* RF5_CAUSE_4 */
1734 case 128 + 16: /* RF5_BO_ACID */
1735 case 128 + 17: /* RF5_BO_ELEC */
1736 case 128 + 18: /* RF5_BO_FIRE */
1737 case 128 + 19: /* RF5_BO_COLD */
1738 case 128 + 21: /* RF5_BO_NETH */
1739 case 128 + 22: /* RF5_BO_WATE */
1740 case 128 + 23: /* RF5_BO_MANA */
1741 case 128 + 24: /* RF5_BO_PLAS */
1742 case 128 + 25: /* RF5_BO_ICEE */
1743 case 128 + 26: /* RF5_MISSILE */
1744 case 128 + 27: /* RF5_SCARE */
1745 case 128 + 28: /* RF5_BLIND */
1746 case 128 + 29: /* RF5_CONF */
1747 case 128 + 30: /* RF5_SLOW */
1748 case 128 + 31: /* RF5_HOLD */
1749 case 160 + 1: /* RF6_HAND_DOOM */
1750 case 160 + 8: /* RF6_TELE_TO */
1751 case 160 + 9: /* RF6_TELE_AWAY */
1752 case 160 + 10: /* RF6_TELE_LEVEL */
1753 case 160 + 11: /* RF6_PSY_SPEAR */
1754 case 160 + 12: /* RF6_DARKNESS */
1755 case 160 + 14: /* RF6_FORGET */
1760 /* Cast the spell. */
1761 dam = monspell_to_player(thrown_spell, y, x, m_idx);
1762 if (dam < 0) return FALSE;
1764 if ((p_ptr->action == ACTION_LEARN) && thrown_spell > 175)
1766 learn_spell(thrown_spell - 96);
1769 if (seen && maneable && !world_monster && (p_ptr->pclass == CLASS_IMITATOR))
1771 if (thrown_spell != 167) /* Not RF6_SPECIAL */
1773 if (p_ptr->mane_num == MAX_MANE)
1777 for (i = 0;i < p_ptr->mane_num;i++)
1779 p_ptr->mane_spell[i] = p_ptr->mane_spell[i+1];
1780 p_ptr->mane_dam[i] = p_ptr->mane_dam[i+1];
1783 p_ptr->mane_spell[p_ptr->mane_num] = thrown_spell - 96;
1784 p_ptr->mane_dam[p_ptr->mane_num] = dam;
1788 p_ptr->redraw |= (PR_IMITATION);
1792 /* Remember what the monster did to us */
1796 if (thrown_spell < 32 * 4)
1798 r_ptr->r_flags4 |= (1L << (thrown_spell - 32 * 3));
1799 if (r_ptr->r_cast_spell < MAX_UCHAR) r_ptr->r_cast_spell++;
1803 else if (thrown_spell < 32 * 5)
1805 r_ptr->r_flags5 |= (1L << (thrown_spell - 32 * 4));
1806 if (r_ptr->r_cast_spell < MAX_UCHAR) r_ptr->r_cast_spell++;
1810 else if (thrown_spell < 32 * 6)
1812 r_ptr->r_flags6 |= (1L << (thrown_spell - 32 * 5));
1813 if (r_ptr->r_cast_spell < MAX_UCHAR) r_ptr->r_cast_spell++;
1818 /* Always take note of monsters that kill you */
1819 if (p_ptr->is_dead && (r_ptr->r_deaths < MAX_SHORT) && !p_ptr->inside_arena)
1821 r_ptr->r_deaths++; /* Ignore appearance difference */
1824 /* A spell was cast */