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
47 #include "object-curse.h"
49 #include "realm-hex.h"
50 #include "player-move.h"
51 #include "player-status.h"
53 #include "monster-spell.h"
56 #include "realm-song.h"
57 #include "view-mainwindow.h"
58 #include "player-race.h"
59 #include "player-class.h"
63 * @brief モンスターがプレイヤーの弱点をついた選択を取るかどうかの判定 /
64 * Internal probability routine
65 * @param r_ptr モンスター種族の構造体参照ポインタ
67 * @return 適した選択を取るならばTRUEを返す。
69 static bool int_outof(monster_race *r_ptr, PERCENTAGE prob)
71 /* Non-Smart monsters are half as "smart" */
72 if (!(r_ptr->flags2 & RF2_SMART)) prob = prob / 2;
75 return (randint0(100) < prob);
80 * @brief モンスターの魔法一覧から戦術的に適さない魔法を除外する /
81 * Remove the "bad" spells from a spell list
82 * @param m_idx モンスターの構造体参照ポインタ
83 * @param f4p モンスター魔法のフラグリスト1
84 * @param f5p モンスター魔法のフラグリスト2
85 * @param f6p モンスター魔法のフラグリスト3
88 static void remove_bad_spells(MONSTER_IDX m_idx, u32b *f4p, u32b *f5p, u32b *f6p)
90 monster_type *m_ptr = ¤t_floor_ptr->m_list[m_idx];
91 monster_race *r_ptr = &r_info[m_ptr->r_idx];
100 /* Too stupid to know anything */
101 if (r_ptr->flags2 & RF2_STUPID) return;
104 /* Must be cheating or learning */
105 if (!smart_cheat && !smart_learn) return;
108 /* Update acquired knowledge */
111 /* Hack -- Occasionally forget player status */
112 /* Only save SM_FRIENDLY, SM_PET or SM_CLONED */
113 if (m_ptr->smart && (randint0(100) < 1)) m_ptr->smart &= (SM_FRIENDLY | SM_PET | SM_CLONED);
115 /* Use the memorized flags */
116 smart = m_ptr->smart;
120 /* Cheat if requested */
123 /* Know basic info */
124 if (p_ptr->resist_acid) smart |= (SM_RES_ACID);
125 if (IS_OPPOSE_ACID()) smart |= (SM_OPP_ACID);
126 if (p_ptr->immune_acid) smart |= (SM_IMM_ACID);
127 if (p_ptr->resist_elec) smart |= (SM_RES_ELEC);
128 if (IS_OPPOSE_ELEC()) smart |= (SM_OPP_ELEC);
129 if (p_ptr->immune_elec) smart |= (SM_IMM_ELEC);
130 if (p_ptr->resist_fire) smart |= (SM_RES_FIRE);
131 if (IS_OPPOSE_FIRE()) smart |= (SM_OPP_FIRE);
132 if (p_ptr->immune_fire) smart |= (SM_IMM_FIRE);
133 if (p_ptr->resist_cold) smart |= (SM_RES_COLD);
134 if (IS_OPPOSE_COLD()) smart |= (SM_OPP_COLD);
135 if (p_ptr->immune_cold) smart |= (SM_IMM_COLD);
137 /* Know poison info */
138 if (p_ptr->resist_pois) smart |= (SM_RES_POIS);
139 if (IS_OPPOSE_POIS()) smart |= (SM_OPP_POIS);
141 /* Know special resistances */
142 if (p_ptr->resist_neth) smart |= (SM_RES_NETH);
143 if (p_ptr->resist_lite) smart |= (SM_RES_LITE);
144 if (p_ptr->resist_dark) smart |= (SM_RES_DARK);
145 if (p_ptr->resist_fear) smart |= (SM_RES_FEAR);
146 if (p_ptr->resist_conf) smart |= (SM_RES_CONF);
147 if (p_ptr->resist_chaos) smart |= (SM_RES_CHAOS);
148 if (p_ptr->resist_disen) smart |= (SM_RES_DISEN);
149 if (p_ptr->resist_blind) smart |= (SM_RES_BLIND);
150 if (p_ptr->resist_nexus) smart |= (SM_RES_NEXUS);
151 if (p_ptr->resist_sound) smart |= (SM_RES_SOUND);
152 if (p_ptr->resist_shard) smart |= (SM_RES_SHARD);
153 if (p_ptr->reflect) smart |= (SM_IMM_REFLECT);
155 /* Know bizarre "resistances" */
156 if (p_ptr->free_act) smart |= (SM_IMM_FREE);
157 if (!p_ptr->msp) smart |= (SM_IMM_MANA);
165 if (smart & SM_IMM_ACID)
167 f4 &= ~(RF4_BR_ACID);
168 f5 &= ~(RF5_BA_ACID);
169 f5 &= ~(RF5_BO_ACID);
171 else if ((smart & (SM_OPP_ACID)) && (smart & (SM_RES_ACID)))
173 if (int_outof(r_ptr, 80)) f4 &= ~(RF4_BR_ACID);
174 if (int_outof(r_ptr, 80)) f5 &= ~(RF5_BA_ACID);
175 if (int_outof(r_ptr, 80)) f5 &= ~(RF5_BO_ACID);
177 else if ((smart & (SM_OPP_ACID)) || (smart & (SM_RES_ACID)))
179 if (int_outof(r_ptr, 30)) f4 &= ~(RF4_BR_ACID);
180 if (int_outof(r_ptr, 30)) f5 &= ~(RF5_BA_ACID);
181 if (int_outof(r_ptr, 30)) f5 &= ~(RF5_BO_ACID);
185 if (smart & (SM_IMM_ELEC))
187 f4 &= ~(RF4_BR_ELEC);
188 f5 &= ~(RF5_BA_ELEC);
189 f5 &= ~(RF5_BO_ELEC);
191 else if ((smart & (SM_OPP_ELEC)) && (smart & (SM_RES_ELEC)))
193 if (int_outof(r_ptr, 80)) f4 &= ~(RF4_BR_ELEC);
194 if (int_outof(r_ptr, 80)) f5 &= ~(RF5_BA_ELEC);
195 if (int_outof(r_ptr, 80)) f5 &= ~(RF5_BO_ELEC);
197 else if ((smart & (SM_OPP_ELEC)) || (smart & (SM_RES_ELEC)))
199 if (int_outof(r_ptr, 30)) f4 &= ~(RF4_BR_ELEC);
200 if (int_outof(r_ptr, 30)) f5 &= ~(RF5_BA_ELEC);
201 if (int_outof(r_ptr, 30)) f5 &= ~(RF5_BO_ELEC);
205 if (smart & (SM_IMM_FIRE))
207 f4 &= ~(RF4_BR_FIRE);
208 f5 &= ~(RF5_BA_FIRE);
209 f5 &= ~(RF5_BO_FIRE);
211 else if ((smart & (SM_OPP_FIRE)) && (smart & (SM_RES_FIRE)))
213 if (int_outof(r_ptr, 80)) f4 &= ~(RF4_BR_FIRE);
214 if (int_outof(r_ptr, 80)) f5 &= ~(RF5_BA_FIRE);
215 if (int_outof(r_ptr, 80)) f5 &= ~(RF5_BO_FIRE);
217 else if ((smart & (SM_OPP_FIRE)) || (smart & (SM_RES_FIRE)))
219 if (int_outof(r_ptr, 30)) f4 &= ~(RF4_BR_FIRE);
220 if (int_outof(r_ptr, 30)) f5 &= ~(RF5_BA_FIRE);
221 if (int_outof(r_ptr, 30)) f5 &= ~(RF5_BO_FIRE);
225 if (smart & (SM_IMM_COLD))
227 f4 &= ~(RF4_BR_COLD);
228 f5 &= ~(RF5_BA_COLD);
229 f5 &= ~(RF5_BO_COLD);
230 f5 &= ~(RF5_BO_ICEE);
232 else if ((smart & (SM_OPP_COLD)) && (smart & (SM_RES_COLD)))
234 if (int_outof(r_ptr, 80)) f4 &= ~(RF4_BR_COLD);
235 if (int_outof(r_ptr, 80)) f5 &= ~(RF5_BA_COLD);
236 if (int_outof(r_ptr, 80)) f5 &= ~(RF5_BO_COLD);
237 if (int_outof(r_ptr, 80)) f5 &= ~(RF5_BO_ICEE);
239 else if ((smart & (SM_OPP_COLD)) || (smart & (SM_RES_COLD)))
241 if (int_outof(r_ptr, 30)) f4 &= ~(RF4_BR_COLD);
242 if (int_outof(r_ptr, 30)) f5 &= ~(RF5_BA_COLD);
243 if (int_outof(r_ptr, 30)) f5 &= ~(RF5_BO_COLD);
244 if (int_outof(r_ptr, 20)) f5 &= ~(RF5_BO_ICEE);
248 if ((smart & (SM_OPP_POIS)) && (smart & (SM_RES_POIS)))
250 if (int_outof(r_ptr, 80)) f4 &= ~(RF4_BR_POIS);
251 if (int_outof(r_ptr, 80)) f5 &= ~(RF5_BA_POIS);
252 if (int_outof(r_ptr, 60)) f4 &= ~(RF4_BA_NUKE);
253 if (int_outof(r_ptr, 60)) f4 &= ~(RF4_BR_NUKE);
255 else if ((smart & (SM_OPP_POIS)) || (smart & (SM_RES_POIS)))
257 if (int_outof(r_ptr, 30)) f4 &= ~(RF4_BR_POIS);
258 if (int_outof(r_ptr, 30)) f5 &= ~(RF5_BA_POIS);
262 if (smart & (SM_RES_NETH))
264 if (PRACE_IS_(p_ptr, RACE_SPECTRE))
266 f4 &= ~(RF4_BR_NETH);
267 f5 &= ~(RF5_BA_NETH);
268 f5 &= ~(RF5_BO_NETH);
272 if (int_outof(r_ptr, 20)) f4 &= ~(RF4_BR_NETH);
273 if (int_outof(r_ptr, 50)) f5 &= ~(RF5_BA_NETH);
274 if (int_outof(r_ptr, 50)) f5 &= ~(RF5_BO_NETH);
278 if (smart & (SM_RES_LITE))
280 if (int_outof(r_ptr, 50)) f4 &= ~(RF4_BR_LITE);
281 if (int_outof(r_ptr, 50)) f5 &= ~(RF5_BA_LITE);
284 if (smart & (SM_RES_DARK))
286 if (PRACE_IS_(p_ptr, RACE_VAMPIRE))
288 f4 &= ~(RF4_BR_DARK);
289 f5 &= ~(RF5_BA_DARK);
293 if (int_outof(r_ptr, 50)) f4 &= ~(RF4_BR_DARK);
294 if (int_outof(r_ptr, 50)) f5 &= ~(RF5_BA_DARK);
298 if (smart & (SM_RES_FEAR))
303 if (smart & (SM_RES_CONF))
306 if (int_outof(r_ptr, 50)) f4 &= ~(RF4_BR_CONF);
309 if (smart & (SM_RES_CHAOS))
311 if (int_outof(r_ptr, 20)) f4 &= ~(RF4_BR_CHAO);
312 if (int_outof(r_ptr, 50)) f4 &= ~(RF4_BA_CHAO);
315 if (smart & (SM_RES_DISEN))
317 if (int_outof(r_ptr, 40)) f4 &= ~(RF4_BR_DISE);
320 if (smart & (SM_RES_BLIND))
325 if (smart & (SM_RES_NEXUS))
327 if (int_outof(r_ptr, 50)) f4 &= ~(RF4_BR_NEXU);
328 f6 &= ~(RF6_TELE_LEVEL);
331 if (smart & (SM_RES_SOUND))
333 if (int_outof(r_ptr, 50)) f4 &= ~(RF4_BR_SOUN);
336 if (smart & (SM_RES_SHARD))
338 if (int_outof(r_ptr, 40)) f4 &= ~(RF4_BR_SHAR);
341 if (smart & (SM_IMM_REFLECT))
343 if (int_outof(r_ptr, 150)) f5 &= ~(RF5_BO_COLD);
344 if (int_outof(r_ptr, 150)) f5 &= ~(RF5_BO_FIRE);
345 if (int_outof(r_ptr, 150)) f5 &= ~(RF5_BO_ACID);
346 if (int_outof(r_ptr, 150)) f5 &= ~(RF5_BO_ELEC);
347 if (int_outof(r_ptr, 150)) f5 &= ~(RF5_BO_NETH);
348 if (int_outof(r_ptr, 150)) f5 &= ~(RF5_BO_WATE);
349 if (int_outof(r_ptr, 150)) f5 &= ~(RF5_BO_MANA);
350 if (int_outof(r_ptr, 150)) f5 &= ~(RF5_BO_PLAS);
351 if (int_outof(r_ptr, 150)) f5 &= ~(RF5_BO_ICEE);
352 if (int_outof(r_ptr, 150)) f5 &= ~(RF5_MISSILE);
355 if (smart & (SM_IMM_FREE))
361 if (smart & (SM_IMM_MANA))
363 f5 &= ~(RF5_DRAIN_MANA);
366 /* No spells left? */
367 /* if (!f4 && !f5 && !f6) ... */
376 * @brief モンスターにとって所定の地点が召還に相応しい地点かどうかを返す。 /
377 * Determine if there is a space near the player in which a summoned creature can appear
378 * @param y1 判定を行いたいマスのY座標
379 * @param x1 判定を行いたいマスのX座標
380 * @return 召還に相応しいならばTRUEを返す
382 bool summon_possible(POSITION y1, POSITION x1)
386 /* Start at the player's location, and check 2 grids in each dir */
387 for (y = y1 - 2; y <= y1 + 2; y++)
389 for (x = x1 - 2; x <= x1 + 2; x++)
391 /* Ignore illegal locations */
392 if (!in_bounds(y, x)) continue;
394 /* Only check a circular area */
395 if (distance(y1, x1, y, x)>2) continue;
397 /* ...nor on the Pattern */
398 if (pattern_tile(y, x)) continue;
400 /* Require empty floor grid in line of projection */
401 if (cave_empty_bold(y, x) && projectable(y1, x1, y, x) && projectable(y, x, y1, x1)) return (TRUE);
410 * @brief モンスターにとって死者復活を行うべき状態かどうかを返す /
411 * Determine if there is a space near the player in which a summoned creature can appear
412 * @param m_ptr 判定を行いたいモンスターの構造体参照ポインタ
413 * @return 死者復活が有効な状態ならばTRUEを返す。
415 bool raise_possible(monster_type *m_ptr)
418 POSITION y = m_ptr->fy;
419 POSITION x = m_ptr->fx;
420 OBJECT_IDX this_o_idx, next_o_idx = 0;
423 for (xx = x - 5; xx <= x + 5; xx++)
425 for (yy = y - 5; yy <= y + 5; yy++)
427 if (distance(y, x, yy, xx) > 5) continue;
428 if (!los(y, x, yy, xx)) continue;
429 if (!projectable(y, x, yy, xx)) continue;
431 g_ptr = ¤t_floor_ptr->grid_array[yy][xx];
432 /* Scan the pile of objects */
433 for (this_o_idx = g_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
435 object_type *o_ptr = ¤t_floor_ptr->o_list[this_o_idx];
436 next_o_idx = o_ptr->next_o_idx;
438 /* Known to be worthless? */
439 if (o_ptr->tval == TV_CORPSE)
441 if (!monster_has_hostile_align(m_ptr, 0, 0, &r_info[o_ptr->pval])) return TRUE;
452 * @brief モンスターにとってボルト型魔法が有効な状態かを返す /
453 * Determine if a bolt spell will hit the player.
454 * @param y1 ボルト魔法発射地点のY座標
455 * @param x1 ボルト魔法発射地点のX座標
456 * @param y2 ボルト魔法目標地点のY座標
457 * @param x2 ボルト魔法目標地点のX座標
458 * @param is_friend モンスターがプレイヤーに害意を持たない(ペットか友好的)ならばTRUEをつける
459 * @return ボルト型魔法が有効ならばTRUEを返す。
461 * Originally, it was possible for a friendly to shoot another friendly.\n
462 * Change it so a "clean shot" means no equally friendly monster is\n
463 * between the attacker and target.\n
465 * This is exactly like "projectable", but it will\n
466 * return FALSE if a monster is in the way.\n
467 * no equally friendly monster is\n
468 * between the attacker and target.\n
470 bool clean_shot(POSITION y1, POSITION x1, POSITION y2, POSITION x2, bool is_friend)
472 /* Must be the same as projectable() */
480 /* Check the projection path */
481 grid_n = project_path(grid_g, MAX_RANGE, y1, x1, y2, x2, 0);
483 /* No grid is ever projectable from itself */
484 if (!grid_n) return (FALSE);
487 y = GRID_Y(grid_g[grid_n-1]);
488 x = GRID_X(grid_g[grid_n-1]);
490 /* May not end in an unrequested grid */
491 if ((y != y2) || (x != x2)) return (FALSE);
493 for (i = 0; i < grid_n; i++)
495 y = GRID_Y(grid_g[i]);
496 x = GRID_X(grid_g[i]);
498 if ((current_floor_ptr->grid_array[y][x].m_idx > 0) && !((y == y2) && (x == x2)))
500 monster_type *m_ptr = ¤t_floor_ptr->m_list[current_floor_ptr->grid_array[y][x].m_idx];
501 if (is_friend == is_pet(m_ptr))
506 /* Pets may not shoot through the character - TNB */
507 if (player_bold(y, x))
509 if (is_friend) return (FALSE);
517 * @brief モンスターのボルト型魔法処理 /
518 * Cast a bolt at the player Stop if we hit a monster Affect monsters and the player
519 * @param m_idx モンスターのID
524 * @param monspell モンスター魔法のID
525 * @param target_type モンスターからモンスターへ撃つならMONSTER_TO_MONSTER、モンスターからプレイヤーならMONSTER_TO_PLAYER
528 void bolt(MONSTER_IDX m_idx, POSITION y, POSITION x, EFFECT_ID typ, int dam_hp, int monspell, int target_type)
531 bool learnable = spell_learnable(m_idx);
535 case MONSTER_TO_MONSTER:
536 flg = PROJECT_STOP | PROJECT_KILL;
538 case MONSTER_TO_PLAYER:
539 flg = PROJECT_STOP | PROJECT_KILL | PROJECT_PLAYER;
542 if (typ != GF_ARROW) flg |= PROJECT_REFLECTABLE;
544 /* Target the player with a bolt attack */
545 (void)project(m_idx, 0, y, x, dam_hp, typ, flg, (learnable ? monspell : -1));
549 * @brief モンスターのビーム型魔法処理 /
550 * @param m_idx モンスターのID
555 * @param monspell モンスター魔法のID
556 * @param target_type モンスターからモンスターへ撃つならMONSTER_TO_MONSTER、モンスターからプレイヤーならMONSTER_TO_PLAYER
559 void beam(MONSTER_IDX m_idx, POSITION y, POSITION x, EFFECT_ID typ, int dam_hp, int monspell, int target_type)
562 bool learnable = spell_learnable(m_idx);
566 case MONSTER_TO_MONSTER:
567 flg = PROJECT_BEAM | PROJECT_KILL | PROJECT_THRU;
569 case MONSTER_TO_PLAYER:
570 flg = PROJECT_BEAM | PROJECT_KILL | PROJECT_THRU | PROJECT_PLAYER;
574 /* Target the player with a bolt attack */
575 (void)project(m_idx, 0, y, x, dam_hp, typ, flg, (learnable ? monspell : -1));
580 * @brief モンスターのボール型&ブレス型魔法処理 /
581 * Cast a breath (or ball) attack at the player Pass over any monsters that may be in the way Affect grids, objects, monsters, and the player
584 * @param m_idx モンスターのID
589 * @param monspell モンスター魔法のID
590 * @param target_type モンスターからモンスターへ撃つならMONSTER_TO_MONSTER、モンスターからプレイヤーならMONSTER_TO_PLAYER
593 void breath(POSITION y, POSITION x, MONSTER_IDX m_idx, EFFECT_ID typ, int dam_hp, POSITION rad, bool breath, int monspell, int target_type)
595 monster_type *m_ptr = ¤t_floor_ptr->m_list[m_idx];
596 monster_race *r_ptr = &r_info[m_ptr->r_idx];
597 bool learnable = spell_learnable(m_idx);
598 BIT_FLAGS flg = 0x00;
602 case MONSTER_TO_MONSTER:
603 flg = PROJECT_GRID | PROJECT_ITEM | PROJECT_KILL;
605 case MONSTER_TO_PLAYER:
606 flg = PROJECT_GRID | PROJECT_ITEM | PROJECT_KILL | PROJECT_PLAYER;
610 /* Determine the radius of the blast */
611 if ((rad < 1) && breath) rad = (r_ptr->flags2 & (RF2_POWERFUL)) ? 3 : 2;
613 /* Handle breath attacks */
614 if (breath) rad = 0 - rad;
629 flg |= (PROJECT_HIDE | PROJECT_AIMED);
633 /* Target the player with a ball attack */
634 (void)project(m_idx, rad, y, x, dam_hp, typ, flg, (learnable ? monspell : -1));
641 * @brief ID値が正しいモンスター魔法IDかどうかを返す /
642 * Return TRUE if a spell is good for hurting the player (directly).
643 * @param spell 判定対象のID
644 * @return 正しいIDならばTRUEを返す。
646 static bool spell_attack(byte spell)
648 /* All RF4 spells hurt (except for shriek and dispel) */
649 if (spell < 128 && spell > 98) return (TRUE);
651 /* Various "ball" spells */
652 if (spell >= 128 && spell <= 128 + 8) return (TRUE);
654 /* "Cause wounds" and "bolt" spells */
655 if (spell >= 128 + 12 && spell < 128 + 27) return (TRUE);
658 if (spell == 160 + 1) return (TRUE);
661 if (spell == 160 + 11) return (TRUE);
669 * @brief ID値が退避目的に適したモンスター魔法IDかどうかを返す /
670 * Return TRUE if a spell is good for escaping.
671 * @param spell 判定対象のID
672 * @return 適した魔法のIDならばTRUEを返す。
674 static bool spell_escape(byte spell)
676 /* Blink or Teleport */
677 if (spell == 160 + 4 || spell == 160 + 5) return (TRUE);
679 /* Teleport the player away */
680 if (spell == 160 + 9 || spell == 160 + 10) return (TRUE);
682 /* Isn't good for escaping */
687 * @brief ID値が妨害目的に適したモンスター魔法IDかどうかを返す /
688 * Return TRUE if a spell is good for annoying the player.
689 * @param spell 判定対象のID
690 * @return 適した魔法のIDならばTRUEを返す。
692 static bool spell_annoy(byte spell)
695 if (spell == 96 + 0) return (TRUE);
697 /* Brain smash, et al (added curses) */
698 if (spell >= 128 + 9 && spell <= 128 + 14) return (TRUE);
700 /* Scare, confuse, blind, slow, paralyze */
701 if (spell >= 128 + 27 && spell <= 128 + 31) return (TRUE);
704 if (spell == 160 + 8) return (TRUE);
707 if (spell == 160 + 10) return (TRUE);
709 /* Darkness, make traps, cause amnesia */
710 if (spell >= 160 + 12 && spell <= 160 + 14) return (TRUE);
717 * @brief ID値が召喚型のモンスター魔法IDかどうかを返す /
718 * Return TRUE if a spell is good for annoying the player.
719 * @param spell 判定対象のID
720 * @return 召喚型魔法のIDならばTRUEを返す。
722 static bool spell_summon(byte spell)
724 /* All summon spells */
725 if (spell >= 160 + 16) return (TRUE);
733 * @brief ID値が死者復活処理かどうかを返す /
734 * Return TRUE if a spell is good for annoying the player.
735 * @param spell 判定対象のID
736 * @return 死者復活の処理ならばTRUEを返す。
738 static bool spell_raise(byte spell)
740 /* All raise-dead spells */
741 if (spell == 160 + 15) return (TRUE);
748 * @brief ID値が戦術的なモンスター魔法IDかどうかを返す /
749 * Return TRUE if a spell is good in a tactical situation.
750 * @param spell 判定対象のID
751 * @return 戦術的な魔法のIDならばTRUEを返す。
753 static bool spell_tactic(byte spell)
756 if (spell == 160 + 4) return (TRUE);
763 * @brief ID値が無敵化するモンスター魔法IDかどうかを返す /
764 * Return TRUE if a spell makes invulnerable.
765 * @param spell 判定対象のID
766 * @return 召喚型魔法のIDならばTRUEを返す。
768 static bool spell_invulner(byte spell)
770 /* Invulnerability */
771 if (spell == 160 + 3) return (TRUE);
773 /* No invulnerability */
778 * @brief ID値が加速するモンスター魔法IDかどうかを返す /
779 * Return TRUE if a spell hastes.
780 * @param spell 判定対象のID
781 * @return 召喚型魔法のIDならばTRUEを返す。
783 static bool spell_haste(byte spell)
786 if (spell == 160 + 0) return (TRUE);
788 /* Not a haste spell */
794 * @brief ID値が時間停止を行うモンスター魔法IDかどうかを返す /
795 * Return TRUE if a spell world.
796 * @param spell 判定対象のID
797 * @return 時間停止魔法のIDならばTRUEを返す。
799 static bool spell_world(byte spell)
801 if (spell == 160 + 6) return (TRUE);
807 * @brief ID値が特別効果のモンスター魔法IDかどうかを返す /
808 * Return TRUE if a spell special.
809 * @param spell 判定対象のID
810 * @return 特別効果魔法のIDならばTRUEを返す。
812 static bool spell_special(byte spell)
814 if (p_ptr->inside_battle) return FALSE;
815 if (spell == 160 + 7) return (TRUE);
821 * @brief ID値が光の剣のモンスター魔法IDかどうかを返す /
822 * Return TRUE if a spell psycho-spear.
823 * @param spell 判定対象のID
824 * @return 光の剣のIDならばTRUEを返す。
826 static bool spell_psy_spe(byte spell)
829 if (spell == 160 + 11) return (TRUE);
831 /* Not a haste spell */
837 * @brief ID値が治癒魔法かどうかを返す /
838 * Return TRUE if a spell is good for healing.
839 * @param spell 判定対象のID
840 * @return 治癒魔法のIDならばTRUEを返す。
842 static bool spell_heal(byte spell)
845 if (spell == 160 + 2) return (TRUE);
853 * @brief ID値が魔力消去かどうかを返す /
854 * Return TRUE if a spell is good for dispel.
855 * @param spell 判定対象のID
856 * @return 魔力消去のIDならばTRUEを返す。
858 static bool spell_dispel(byte spell)
861 if (spell == 96 + 2) return (TRUE);
869 * @brief モンスターがプレイヤーに魔力消去を与えるべきかを判定するルーチン
870 * Check should monster cast dispel spell.
871 * @param m_idx モンスターの構造体配列ID
872 * @return 魔力消去をかけるべきならTRUEを返す。
874 bool dispel_check(MONSTER_IDX m_idx)
876 monster_type *m_ptr = ¤t_floor_ptr->m_list[m_idx];
877 monster_race *r_ptr = &r_info[m_ptr->r_idx];
879 /* Invulnabilty (including the song) */
880 if (IS_INVULN()) return (TRUE);
883 if (p_ptr->wraith_form) return (TRUE);
886 if (p_ptr->shield) return (TRUE);
889 if (p_ptr->magicdef) return (TRUE);
892 if (p_ptr->multishadow) return (TRUE);
895 if (p_ptr->dustrobe) return (TRUE);
897 /* Berserk Strength */
898 if (p_ptr->shero && (p_ptr->pclass != CLASS_BERSERKER)) return (TRUE);
901 if (p_ptr->mimic_form == MIMIC_DEMON_LORD) return (TRUE);
903 /* Elemental resistances */
904 if (r_ptr->flags4 & RF4_BR_ACID)
906 if (!p_ptr->immune_acid && (p_ptr->oppose_acid || music_singing(MUSIC_RESIST))) return (TRUE);
907 if (p_ptr->special_defense & DEFENSE_ACID) return (TRUE);
910 if (r_ptr->flags4 & RF4_BR_FIRE)
912 if (!((p_ptr->prace == RACE_DEMON) && p_ptr->lev > 44))
914 if (!p_ptr->immune_fire && (p_ptr->oppose_fire || music_singing(MUSIC_RESIST))) return (TRUE);
915 if (p_ptr->special_defense & DEFENSE_FIRE) return (TRUE);
919 if (r_ptr->flags4 & RF4_BR_ELEC)
921 if (!p_ptr->immune_elec && (p_ptr->oppose_elec || music_singing(MUSIC_RESIST))) return (TRUE);
922 if (p_ptr->special_defense & DEFENSE_ELEC) return (TRUE);
925 if (r_ptr->flags4 & RF4_BR_COLD)
927 if (!p_ptr->immune_cold && (p_ptr->oppose_cold || music_singing(MUSIC_RESIST))) return (TRUE);
928 if (p_ptr->special_defense & DEFENSE_COLD) return (TRUE);
931 if (r_ptr->flags4 & (RF4_BR_POIS | RF4_BR_NUKE))
933 if (!((p_ptr->pclass == CLASS_NINJA) && p_ptr->lev > 44))
935 if (p_ptr->oppose_pois || music_singing(MUSIC_RESIST)) return (TRUE);
936 if (p_ptr->special_defense & DEFENSE_POIS) return (TRUE);
940 /* Ultimate resistance */
941 if (p_ptr->ult_res) return (TRUE);
943 /* Potion of Neo Tsuyosi special */
944 if (p_ptr->tsuyoshi) return (TRUE);
946 /* Elemental Brands */
947 if ((p_ptr->special_attack & ATTACK_ACID) && !(r_ptr->flagsr & RFR_EFF_IM_ACID_MASK)) return (TRUE);
948 if ((p_ptr->special_attack & ATTACK_FIRE) && !(r_ptr->flagsr & RFR_EFF_IM_FIRE_MASK)) return (TRUE);
949 if ((p_ptr->special_attack & ATTACK_ELEC) && !(r_ptr->flagsr & RFR_EFF_IM_ELEC_MASK)) return (TRUE);
950 if ((p_ptr->special_attack & ATTACK_COLD) && !(r_ptr->flagsr & RFR_EFF_IM_COLD_MASK)) return (TRUE);
951 if ((p_ptr->special_attack & ATTACK_POIS) && !(r_ptr->flagsr & RFR_EFF_IM_POIS_MASK)) return (TRUE);
953 if (p_ptr->pspeed < 145)
955 if (IS_FAST()) return (TRUE);
959 if (p_ptr->lightspeed && (m_ptr->mspeed < 136)) return (TRUE);
961 if (p_ptr->riding && (current_floor_ptr->m_list[p_ptr->riding].mspeed < 135))
963 if (MON_FAST(¤t_floor_ptr->m_list[p_ptr->riding])) return (TRUE);
966 /* No need to cast dispel spell */
972 * @brief モンスターの魔法選択ルーチン
973 * Have a monster choose a spell from a list of "useful" spells.
974 * @param m_idx モンスターの構造体配列ID
975 * @param spells 候補魔法IDをまとめた配列
976 * @param num spellsの長さ
977 * @return 選択したモンスター魔法のID
979 * Note that this list does NOT include spells that will just hit\n
980 * other monsters, and the list is restricted when the monster is\n
981 * "desperate". Should that be the job of this function instead?\n
983 * Stupid monsters will just pick a spell randomly. Smart monsters\n
984 * will choose more "intelligently".\n
986 * Use the helper functions above to put spells into categories.\n
988 * This function may well be an efficiency bottleneck.\n
990 static int choose_attack_spell(MONSTER_IDX m_idx, byte spells[], byte num)
992 monster_type *m_ptr = ¤t_floor_ptr->m_list[m_idx];
993 monster_race *r_ptr = &r_info[m_ptr->r_idx];
995 byte escape[96], escape_num = 0;
996 byte attack[96], attack_num = 0;
997 byte summon[96], summon_num = 0;
998 byte tactic[96], tactic_num = 0;
999 byte annoy[96], annoy_num = 0;
1000 byte invul[96], invul_num = 0;
1001 byte haste[96], haste_num = 0;
1002 byte world[96], world_num = 0;
1003 byte special[96], special_num = 0;
1004 byte psy_spe[96], psy_spe_num = 0;
1005 byte raise[96], raise_num = 0;
1006 byte heal[96], heal_num = 0;
1007 byte dispel[96], dispel_num = 0;
1011 /* Stupid monsters choose randomly */
1012 if (r_ptr->flags2 & (RF2_STUPID))
1014 /* Pick at random */
1015 return (spells[randint0(num)]);
1018 /* Categorize spells */
1019 for (i = 0; i < num; i++)
1022 if (spell_escape(spells[i])) escape[escape_num++] = spells[i];
1025 if (spell_attack(spells[i])) attack[attack_num++] = spells[i];
1028 if (spell_summon(spells[i])) summon[summon_num++] = spells[i];
1030 /* Tactical spell? */
1031 if (spell_tactic(spells[i])) tactic[tactic_num++] = spells[i];
1033 /* Annoyance spell? */
1034 if (spell_annoy(spells[i])) annoy[annoy_num++] = spells[i];
1036 /* Invulnerability spell? */
1037 if (spell_invulner(spells[i])) invul[invul_num++] = spells[i];
1040 if (spell_haste(spells[i])) haste[haste_num++] = spells[i];
1043 if (spell_world(spells[i])) world[world_num++] = spells[i];
1045 /* Special spell? */
1046 if (spell_special(spells[i])) special[special_num++] = spells[i];
1048 /* Psycho-spear spell? */
1049 if (spell_psy_spe(spells[i])) psy_spe[psy_spe_num++] = spells[i];
1051 /* Raise-dead spell? */
1052 if (spell_raise(spells[i])) raise[raise_num++] = spells[i];
1055 if (spell_heal(spells[i])) heal[heal_num++] = spells[i];
1058 if (spell_dispel(spells[i])) dispel[dispel_num++] = spells[i];
1061 /*** Try to pick an appropriate spell type ***/
1064 if (world_num && (randint0(100) < 15) && !current_world_ptr->timewalk_m_idx)
1066 /* Choose haste spell */
1067 return (world[randint0(world_num)]);
1073 bool success = FALSE;
1074 switch(m_ptr->r_idx)
1078 if ((m_ptr->hp < m_ptr->maxhp / 2) && r_info[MON_BANOR].max_num && r_info[MON_LUPART].max_num) success = TRUE;
1082 if (success) return (special[randint0(special_num)]);
1085 /* Still hurt badly, couldn't flee, attempt to heal */
1086 if (m_ptr->hp < m_ptr->maxhp / 3 && one_in_(2))
1088 /* Choose heal spell if possible */
1089 if (heal_num) return (heal[randint0(heal_num)]);
1092 /* Hurt badly or afraid, attempt to flee */
1093 if (((m_ptr->hp < m_ptr->maxhp / 3) || MON_MONFEAR(m_ptr)) && one_in_(2))
1095 /* Choose escape spell if possible */
1096 if (escape_num) return (escape[randint0(escape_num)]);
1102 bool success = FALSE;
1103 switch (m_ptr->r_idx)
1109 case MON_BANORLUPART:
1110 if (randint0(100) < 70) success = TRUE;
1113 if (randint0(100) < 40) success = TRUE;
1116 if (randint0(100) < 50) success = TRUE;
1119 if (success) return (special[randint0(special_num)]);
1122 /* Player is close and we have attack spells, blink away */
1123 if ((distance(p_ptr->y, p_ptr->x, m_ptr->fy, m_ptr->fx) < 4) && (attack_num || (r_ptr->a_ability_flags2 & RF6_TRAPS)) && (randint0(100) < 75) && !current_world_ptr->timewalk_m_idx)
1125 /* Choose tactical spell */
1126 if (tactic_num) return (tactic[randint0(tactic_num)]);
1129 /* Summon if possible (sometimes) */
1130 if (summon_num && (randint0(100) < 40))
1132 /* Choose summon spell */
1133 return (summon[randint0(summon_num)]);
1137 if (dispel_num && one_in_(2))
1139 /* Choose dispel spell if possible */
1140 if (dispel_check(m_idx))
1142 return (dispel[randint0(dispel_num)]);
1146 /* Raise-dead if possible (sometimes) */
1147 if (raise_num && (randint0(100) < 40))
1149 /* Choose raise-dead spell */
1150 return (raise[randint0(raise_num)]);
1153 /* Attack spell (most of the time) */
1156 if (psy_spe_num && (randint0(100) < 50))
1158 /* Choose attack spell */
1159 return (psy_spe[randint0(psy_spe_num)]);
1161 else if (attack_num && (randint0(100) < 40))
1163 /* Choose attack spell */
1164 return (attack[randint0(attack_num)]);
1167 else if (attack_num && (randint0(100) < 85))
1169 /* Choose attack spell */
1170 return (attack[randint0(attack_num)]);
1173 /* Try another tactical spell (sometimes) */
1174 if (tactic_num && (randint0(100) < 50) && !current_world_ptr->timewalk_m_idx)
1176 /* Choose tactic spell */
1177 return (tactic[randint0(tactic_num)]);
1180 /* Cast globe of invulnerability if not already in effect */
1181 if (invul_num && !m_ptr->mtimed[MTIMED_INVULNER] && (randint0(100) < 50))
1183 /* Choose Globe of Invulnerability */
1184 return (invul[randint0(invul_num)]);
1187 /* We're hurt (not badly), try to heal */
1188 if ((m_ptr->hp < m_ptr->maxhp * 3 / 4) && (randint0(100) < 25))
1190 /* Choose heal spell if possible */
1191 if (heal_num) return (heal[randint0(heal_num)]);
1194 /* Haste self if we aren't already somewhat hasted (rarely) */
1195 if (haste_num && (randint0(100) < 20) && !MON_FAST(m_ptr))
1197 /* Choose haste spell */
1198 return (haste[randint0(haste_num)]);
1201 /* Annoy player (most of the time) */
1202 if (annoy_num && (randint0(100) < 80))
1204 /* Choose annoyance spell */
1205 return (annoy[randint0(annoy_num)]);
1208 /* Choose no spell */
1214 * @brief ID値が非魔術的な特殊技能かどうかを返す /
1215 * Return TRUE if a spell is inate spell.
1216 * @param spell 判定対象のID
1217 * @return 非魔術的な特殊技能ならばTRUEを返す。
1219 bool spell_is_inate(SPELL_IDX spell)
1221 if (spell < 32 * 4) /* Set RF4 */
1223 if ((1L << (spell - 32 * 3)) & RF4_NOMAGIC_MASK) return TRUE;
1225 else if (spell < 32 * 5) /* Set RF5 */
1227 if ((1L << (spell - 32 * 4)) & RF5_NOMAGIC_MASK) return TRUE;
1229 else if (spell < 32 * 6) /* Set RF6 */
1231 if ((1L << (spell - 32 * 5)) & RF6_NOMAGIC_MASK) return TRUE;
1234 /* This spell is not "inate" */
1240 * @brief モンスターがプレイヤーにダメージを与えるための最適な座標を算出する /
1241 * @param m_ptr 技能を使用するモンスター構造体の参照ポインタ
1242 * @param yp 最適な目標地点のY座標を返す参照ポインタ
1243 * @param xp 最適な目標地点のX座標を返す参照ポインタ
1244 * @param f_flag 射線に入れるのを避ける地形の所持フラグ
1245 * @param path_check 射線を判定するための関数ポインタ
1246 * @return 有効な座標があった場合TRUEを返す
1248 static bool adjacent_grid_check(monster_type *m_ptr, POSITION *yp, POSITION *xp,
1249 int f_flag, bool (*path_check)(POSITION, POSITION, POSITION, POSITION))
1253 static int tonari_y[4][8] = {{-1, -1, -1, 0, 0, 1, 1, 1},
1254 {-1, -1, -1, 0, 0, 1, 1, 1},
1255 { 1, 1, 1, 0, 0, -1, -1, -1},
1256 { 1, 1, 1, 0, 0, -1, -1, -1}};
1257 static int tonari_x[4][8] = {{-1, 0, 1, -1, 1, -1, 0, 1},
1258 { 1, 0, -1, 1, -1, 1, 0, -1},
1259 {-1, 0, 1, -1, 1, -1, 0, 1},
1260 { 1, 0, -1, 1, -1, 1, 0, -1}};
1262 if (m_ptr->fy < p_ptr->y && m_ptr->fx < p_ptr->x) tonari = 0;
1263 else if (m_ptr->fy < p_ptr->y) tonari = 1;
1264 else if (m_ptr->fx < p_ptr->x) tonari = 2;
1267 for (i = 0; i < 8; i++)
1269 int next_x = *xp + tonari_x[tonari][i];
1270 int next_y = *yp + tonari_y[tonari][i];
1273 /* Access the next grid */
1274 g_ptr = ¤t_floor_ptr->grid_array[next_y][next_x];
1276 /* Skip this feature */
1277 if (!cave_have_flag_grid(g_ptr, f_flag)) continue;
1279 if (path_check(m_ptr->fy, m_ptr->fx, next_y, next_x))
1290 #define DO_SPELL_NONE 0
1291 #define DO_SPELL_BR_LITE 1
1292 #define DO_SPELL_BR_DISI 2
1293 #define DO_SPELL_BA_LITE 3
1296 * @brief モンスターの特殊技能メインルーチン /
1297 * Creatures can cast spells, shoot missiles, and breathe.
1298 * @param m_idx モンスター構造体配列のID
1299 * @return 実際に特殊技能を利用したらTRUEを返す
1301 * Returns "TRUE" if a spell (or whatever) was (successfully) cast.\n
1303 * This function could use some work, but remember to\n
1304 * keep it as optimized as possible, while retaining generic code.\n
1306 * Verify the various "blind-ness" checks in the code.\n
1308 * Note that several effects should really not be "seen"\n
1309 * if the player is blind. See also "effects.c" for other "mistakes".\n
1311 * Perhaps monsters should breathe at locations *near* the player,\n
1312 * since this would allow them to inflict "partial" damage.\n
1314 * Perhaps smart monsters should decline to use "bolt" spells if\n
1315 * there is a monster in the way, unless they wish to kill it.\n
1317 * Note that, to allow the use of the "track_target" option at some\n
1318 * later time, certain non-optimal things are done in the code below,\n
1319 * including explicit checks against the "direct" variable, which is\n
1320 * currently always true by the time it is checked, but which should\n
1321 * really be set according to an explicit "projectable()" test, and\n
1322 * the use of generic "x,y" locations instead of the player location,\n
1323 * with those values being initialized with the player location.\n
1325 * It will not be possible to "correctly" handle the case in which a\n
1326 * monster attempts to attack a location which is thought to contain\n
1327 * the player, but which in fact is nowhere near the player, since this\n
1328 * might induce all sorts of messages about the attack itself, and about\n
1329 * the effects of the attack, which the player might or might not be in\n
1330 * a position to observe. Thus, for simplicity, it is probably best to\n
1331 * only allow "faulty" attacks by a monster if one of the important grids\n
1332 * (probably the initial or final grid) is in fact in view of the player.\n
1333 * It may be necessary to actually prevent spell attacks except when the\n
1334 * monster actually has line of sight to the player. Note that a monster\n
1335 * could be left in a bizarre situation after the player ducked behind a\n
1336 * pillar and then teleported away, for example.\n
1339 * that certain spell attacks do not use the "project()" function\n
1340 * but "simulate" it via the "direct" variable, which is always at least\n
1341 * as restrictive as the "project()" function. This is necessary to\n
1342 * prevent "blindness" attacks and such from bending around walls, etc,\n
1343 * and to allow the use of the "track_target" option in the future.\n
1345 * Note that this function attempts to optimize the use of spells for the\n
1346 * cases in which the monster has no spells, or has spells but cannot use\n
1347 * them, or has spells but they will have no "useful" effect. Note that\n
1348 * this function has been an efficiency bottleneck in the past.\n
1350 * Note the special "MFLAG_NICE" flag, which prevents a monster from using\n
1351 * any spell attacks until the player has had a single chance to move.\n
1353 bool make_attack_spell(MONSTER_IDX m_idx)
1356 SPELL_IDX thrown_spell = 0;
1358 PERCENTAGE failrate;
1359 byte spell[96], num = 0;
1360 BIT_FLAGS f4, f5, f6;
1361 monster_type *m_ptr = ¤t_floor_ptr->m_list[m_idx];
1362 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1363 GAME_TEXT m_name[MAX_NLEN];
1367 bool no_inate = FALSE;
1368 bool do_spell = DO_SPELL_NONE;
1371 POSITION x = p_ptr->x;
1372 POSITION y = p_ptr->y;
1374 /* Target location for lite breath */
1375 POSITION x_br_lite = 0;
1376 POSITION y_br_lite = 0;
1378 /* Extract the "see-able-ness" */
1379 bool seen = (!p_ptr->blind && m_ptr->ml);
1380 bool maneable = player_has_los_bold(m_ptr->fy, m_ptr->fx);
1382 /* Check "projectable" */
1385 bool in_no_magic_dungeon = (d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_MAGIC) && current_floor_ptr->dun_level
1386 && (!p_ptr->inside_quest || is_fixed_quest_idx(p_ptr->inside_quest));
1388 bool can_use_lite_area = FALSE;
1392 /* Cannot cast spells when confused */
1393 if (MON_CONFUSED(m_ptr))
1395 reset_target(m_ptr);
1399 /* Cannot cast spells when nice */
1400 if (m_ptr->mflag & MFLAG_NICE) return (FALSE);
1401 if (!is_hostile(m_ptr)) return (FALSE);
1404 /* Sometimes forbid inate attacks (breaths) */
1405 if (randint0(100) >= (r_ptr->freq_spell * 2)) no_inate = TRUE;
1407 /* Handle "track_target" option (?) */
1410 /* Extract the racial spell flags */
1412 f5 = r_ptr->a_ability_flags1;
1413 f6 = r_ptr->a_ability_flags2;
1415 /*** require projectable player ***/
1418 if ((m_ptr->cdis > MAX_RANGE) && !m_ptr->target_y) return (FALSE);
1420 /* Check path for lite breath */
1421 if (f4 & RF4_BR_LITE)
1426 if (los(m_ptr->fy, m_ptr->fx, y_br_lite, x_br_lite))
1428 feature_type *f_ptr = &f_info[current_floor_ptr->grid_array[y_br_lite][x_br_lite].feat];
1430 if (!have_flag(f_ptr->flags, FF_LOS))
1432 if (have_flag(f_ptr->flags, FF_PROJECT) && one_in_(2)) f4 &= ~(RF4_BR_LITE);
1436 /* Check path to next grid */
1437 else if (!adjacent_grid_check(m_ptr, &y_br_lite, &x_br_lite, FF_LOS, los)) f4 &= ~(RF4_BR_LITE);
1439 /* Don't breath lite to the wall if impossible */
1440 if (!(f4 & RF4_BR_LITE))
1448 if (projectable(m_ptr->fy, m_ptr->fx, y, x))
1450 feature_type *f_ptr = &f_info[current_floor_ptr->grid_array[y][x].feat];
1452 if (!have_flag(f_ptr->flags, FF_PROJECT))
1454 /* Breath disintegration to the wall if possible */
1455 if ((f4 & RF4_BR_DISI) && have_flag(f_ptr->flags, FF_HURT_DISI) && one_in_(2)) do_spell = DO_SPELL_BR_DISI;
1457 /* Breath lite to the transparent wall if possible */
1458 else if ((f4 & RF4_BR_LITE) && have_flag(f_ptr->flags, FF_LOS) && one_in_(2)) do_spell = DO_SPELL_BR_LITE;
1462 /* Check path to next grid */
1465 bool success = FALSE;
1467 if ((f4 & RF4_BR_DISI) && (m_ptr->cdis < MAX_RANGE/2) &&
1468 in_disintegration_range(m_ptr->fy, m_ptr->fx, y, x) &&
1469 (one_in_(10) || (projectable(y, x, m_ptr->fy, m_ptr->fx) && one_in_(2))))
1471 do_spell = DO_SPELL_BR_DISI;
1474 else if ((f4 & RF4_BR_LITE) && (m_ptr->cdis < MAX_RANGE/2) &&
1475 los(m_ptr->fy, m_ptr->fx, y, x) && one_in_(5))
1477 do_spell = DO_SPELL_BR_LITE;
1480 else if ((f5 & RF5_BA_LITE) && (m_ptr->cdis <= MAX_RANGE))
1482 POSITION by = y, bx = x;
1483 get_project_point(m_ptr->fy, m_ptr->fx, &by, &bx, 0L);
1484 if ((distance(by, bx, y, x) <= 3) && los(by, bx, y, x) && one_in_(5))
1486 do_spell = DO_SPELL_BA_LITE;
1491 if (!success) success = adjacent_grid_check(m_ptr, &y, &x, FF_PROJECT, projectable);
1495 if (m_ptr->target_y && m_ptr->target_x)
1497 y = m_ptr->target_y;
1498 x = m_ptr->target_x;
1499 f4 &= (RF4_INDIRECT_MASK);
1500 f5 &= (RF5_INDIRECT_MASK);
1501 f6 &= (RF6_INDIRECT_MASK);
1505 if (y_br_lite && x_br_lite && (m_ptr->cdis < MAX_RANGE/2) && one_in_(5))
1511 do_spell = DO_SPELL_BR_LITE;
1514 else f4 |= (RF4_BR_LITE);
1519 if (!success) return FALSE;
1522 reset_target(m_ptr);
1524 rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
1526 /* Forbid inate attacks sometimes */
1529 f4 &= ~(RF4_NOMAGIC_MASK);
1530 f5 &= ~(RF5_NOMAGIC_MASK);
1531 f6 &= ~(RF6_NOMAGIC_MASK);
1534 if (f6 & RF6_DARKNESS)
1536 if ((p_ptr->pclass == CLASS_NINJA) &&
1537 !(r_ptr->flags3 & (RF3_UNDEAD | RF3_HURT_LITE)) &&
1538 !(r_ptr->flags7 & RF7_DARK_MASK))
1539 can_use_lite_area = TRUE;
1541 if (!(r_ptr->flags2 & RF2_STUPID))
1543 if (d_info[p_ptr->dungeon_idx].flags1 & DF1_DARKNESS) f6 &= ~(RF6_DARKNESS);
1544 else if ((p_ptr->pclass == CLASS_NINJA) && !can_use_lite_area) f6 &= ~(RF6_DARKNESS);
1548 if (in_no_magic_dungeon && !(r_ptr->flags2 & RF2_STUPID))
1550 f4 &= (RF4_NOMAGIC_MASK);
1551 f5 &= (RF5_NOMAGIC_MASK);
1552 f6 &= (RF6_NOMAGIC_MASK);
1555 if (r_ptr->flags2 & RF2_SMART)
1557 /* Hack -- allow "desperate" spells */
1558 if ((m_ptr->hp < m_ptr->maxhp / 10) &&
1559 (randint0(100) < 50))
1561 /* Require intelligent spells */
1562 f4 &= (RF4_INT_MASK);
1563 f5 &= (RF5_INT_MASK);
1564 f6 &= (RF6_INT_MASK);
1567 /* Hack -- decline "teleport level" in some case */
1568 if ((f6 & RF6_TELE_LEVEL) && TELE_LEVEL_IS_INEFF(0))
1570 f6 &= ~(RF6_TELE_LEVEL);
1574 /* No spells left */
1575 if (!f4 && !f5 && !f6) return (FALSE);
1577 /* Remove the "ineffective" spells */
1578 remove_bad_spells(m_idx, &f4, &f5, &f6);
1580 if (p_ptr->inside_arena || p_ptr->inside_battle)
1582 f4 &= ~(RF4_SUMMON_MASK);
1583 f5 &= ~(RF5_SUMMON_MASK);
1584 f6 &= ~(RF6_SUMMON_MASK | RF6_TELE_LEVEL);
1586 if (m_ptr->r_idx == MON_ROLENTO) f6 &= ~(RF6_SPECIAL);
1589 /* No spells left */
1590 if (!f4 && !f5 && !f6) return (FALSE);
1592 if (!(r_ptr->flags2 & RF2_STUPID))
1594 if (!p_ptr->csp) f5 &= ~(RF5_DRAIN_MANA);
1596 /* Check for a clean bolt shot */
1597 if (((f4 & RF4_BOLT_MASK) ||
1598 (f5 & RF5_BOLT_MASK) ||
1599 (f6 & RF6_BOLT_MASK)) &&
1600 !clean_shot(m_ptr->fy, m_ptr->fx, p_ptr->y, p_ptr->x, FALSE))
1602 /* Remove spells that will only hurt friends */
1603 f4 &= ~(RF4_BOLT_MASK);
1604 f5 &= ~(RF5_BOLT_MASK);
1605 f6 &= ~(RF6_BOLT_MASK);
1608 /* Check for a possible summon */
1609 if (((f4 & RF4_SUMMON_MASK) ||
1610 (f5 & RF5_SUMMON_MASK) ||
1611 (f6 & RF6_SUMMON_MASK)) &&
1612 !(summon_possible(y, x)))
1614 /* Remove summoning spells */
1615 f4 &= ~(RF4_SUMMON_MASK);
1616 f5 &= ~(RF5_SUMMON_MASK);
1617 f6 &= ~(RF6_SUMMON_MASK);
1620 /* Check for a possible raise dead */
1621 if ((f6 & RF6_RAISE_DEAD) && !raise_possible(m_ptr))
1623 /* Remove raise dead spell */
1624 f6 &= ~(RF6_RAISE_DEAD);
1627 /* Special moves restriction */
1628 if (f6 & RF6_SPECIAL)
1630 if ((m_ptr->r_idx == MON_ROLENTO) && !summon_possible(y, x))
1632 f6 &= ~(RF6_SPECIAL);
1636 /* No spells left */
1637 if (!f4 && !f5 && !f6) return (FALSE);
1640 /* Extract the "inate" spells */
1641 for (k = 0; k < 32; k++)
1643 if (f4 & (1L << k)) spell[num++] = k + RF4_SPELL_START;
1646 /* Extract the "normal" spells */
1647 for (k = 0; k < 32; k++)
1649 if (f5 & (1L << k)) spell[num++] = k + RF5_SPELL_START;
1652 /* Extract the "bizarre" spells */
1653 for (k = 0; k < 32; k++)
1655 if (f6 & (1L << k)) spell[num++] = k + RF6_SPELL_START;
1658 /* No spells left */
1659 if (!num) return (FALSE);
1661 /* Stop if player is dead or gone */
1662 if (!p_ptr->playing || p_ptr->is_dead) return (FALSE);
1664 /* Stop if player is leaving */
1665 if (p_ptr->leaving) return (FALSE);
1667 /* Get the monster name (or "it") */
1668 monster_desc(m_name, m_ptr, 0x00);
1671 /* Get the monster possessive ("his"/"her"/"its") */
1672 monster_desc(m_poss, m_ptr, MD_PRON_VISIBLE | MD_POSSESSIVE);
1682 thrown_spell = choose_attack_spell(m_idx, spell, num);
1683 if (thrown_spell) break;
1688 case DO_SPELL_BR_LITE:
1689 thrown_spell = 96+14; /* RF4_BR_LITE */
1692 case DO_SPELL_BR_DISI:
1693 thrown_spell = 96+31; /* RF4_BR_DISI */
1696 case DO_SPELL_BA_LITE:
1697 thrown_spell = 128+20; /* RF5_BA_LITE */
1704 /* Abort if no spell was chosen */
1705 if (!thrown_spell) return (FALSE);
1707 /* Calculate spell failure rate */
1708 failrate = 25 - (rlev + 3) / 4;
1710 /* Hack -- Stupid monsters will never fail (for jellies and such) */
1711 if (r_ptr->flags2 & RF2_STUPID) failrate = 0;
1713 /* Check for spell failure (inate attacks never fail) */
1714 if (!spell_is_inate(thrown_spell)
1715 && (in_no_magic_dungeon || (MON_STUNNED(m_ptr) && one_in_(2)) || (randint0(100) < failrate)))
1717 disturb(TRUE, TRUE);
1718 msg_format(_("%^sは呪文を唱えようとしたが失敗した。", "%^s tries to cast a spell, but fails."), m_name);
1723 /* Hex: Anti Magic Barrier */
1724 if (!spell_is_inate(thrown_spell) && magic_barrier(m_idx))
1726 msg_format(_("反魔法バリアが%^sの呪文をかき消した。", "Anti magic barrier cancels the spell which %^s casts."), m_name);
1731 direct = player_bold(y, x);
1733 can_remember = is_original_ap_and_seen(m_ptr);
1737 switch (thrown_spell)
1739 case 96 + 2: /* RF4_DISPEL */
1740 case 96 + 4: /* RF4_SHOOT */
1741 case 128 + 9: /* RF5_DRAIN_MANA */
1742 case 128 + 10: /* RF5_MIND_BLAST */
1743 case 128 + 11: /* RF5_BRAIN_SMASH */
1744 case 128 + 12: /* RF5_CAUSE_1 */
1745 case 128 + 13: /* RF5_CAUSE_2 */
1746 case 128 + 14: /* RF5_CAUSE_3 */
1747 case 128 + 15: /* RF5_CAUSE_4 */
1748 case 128 + 16: /* RF5_BO_ACID */
1749 case 128 + 17: /* RF5_BO_ELEC */
1750 case 128 + 18: /* RF5_BO_FIRE */
1751 case 128 + 19: /* RF5_BO_COLD */
1752 case 128 + 21: /* RF5_BO_NETH */
1753 case 128 + 22: /* RF5_BO_WATE */
1754 case 128 + 23: /* RF5_BO_MANA */
1755 case 128 + 24: /* RF5_BO_PLAS */
1756 case 128 + 25: /* RF5_BO_ICEE */
1757 case 128 + 26: /* RF5_MISSILE */
1758 case 128 + 27: /* RF5_SCARE */
1759 case 128 + 28: /* RF5_BLIND */
1760 case 128 + 29: /* RF5_CONF */
1761 case 128 + 30: /* RF5_SLOW */
1762 case 128 + 31: /* RF5_HOLD */
1763 case 160 + 1: /* RF6_HAND_DOOM */
1764 case 160 + 8: /* RF6_TELE_TO */
1765 case 160 + 9: /* RF6_TELE_AWAY */
1766 case 160 + 10: /* RF6_TELE_LEVEL */
1767 case 160 + 11: /* RF6_PSY_SPEAR */
1768 case 160 + 12: /* RF6_DARKNESS */
1769 case 160 + 14: /* RF6_FORGET */
1774 /* Cast the spell. */
1775 dam = monspell_to_player(thrown_spell, y, x, m_idx);
1776 if (dam < 0) return FALSE;
1778 if ((p_ptr->action == ACTION_LEARN) && thrown_spell > 175)
1780 learn_spell(thrown_spell - 96);
1783 if (seen && maneable && !current_world_ptr->timewalk_m_idx && (p_ptr->pclass == CLASS_IMITATOR))
1785 if (thrown_spell != 167) /* Not RF6_SPECIAL */
1787 if (p_ptr->mane_num == MAX_MANE)
1791 for (i = 0;i < p_ptr->mane_num;i++)
1793 p_ptr->mane_spell[i] = p_ptr->mane_spell[i+1];
1794 p_ptr->mane_dam[i] = p_ptr->mane_dam[i+1];
1797 p_ptr->mane_spell[p_ptr->mane_num] = thrown_spell - 96;
1798 p_ptr->mane_dam[p_ptr->mane_num] = dam;
1800 p_ptr->new_mane = TRUE;
1802 p_ptr->redraw |= (PR_IMITATION);
1806 /* Remember what the monster did to us */
1810 if (thrown_spell < 32 * 4)
1812 r_ptr->r_flags4 |= (1L << (thrown_spell - 32 * 3));
1813 if (r_ptr->r_cast_spell < MAX_UCHAR) r_ptr->r_cast_spell++;
1817 else if (thrown_spell < 32 * 5)
1819 r_ptr->r_flags5 |= (1L << (thrown_spell - 32 * 4));
1820 if (r_ptr->r_cast_spell < MAX_UCHAR) r_ptr->r_cast_spell++;
1824 else if (thrown_spell < 32 * 6)
1826 r_ptr->r_flags6 |= (1L << (thrown_spell - 32 * 5));
1827 if (r_ptr->r_cast_spell < MAX_UCHAR) r_ptr->r_cast_spell++;
1832 /* Always take note of monsters that kill you */
1833 if (p_ptr->is_dead && (r_ptr->r_deaths < MAX_SHORT) && !p_ptr->inside_arena)
1835 r_ptr->r_deaths++; /* Ignore appearance difference */
1838 /* A spell was cast */