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
45 * @brief モンスターがプレイヤーの弱点をついた選択を取るかどうかの判定 /
46 * Internal probability routine
47 * @param r_ptr モンスター種族の構造体参照ポインタ
49 * @return 適した選択を取るならばTRUEを返す。
51 static bool int_outof(monster_race *r_ptr, int prob)
53 /* Non-Smart monsters are half as "smart" */
54 if (!(r_ptr->flags2 & RF2_SMART)) prob = prob / 2;
57 return (randint0(100) < prob);
62 * @brief モンスターの魔法一覧から戦術的に適さない魔法を除外する /
63 * Remove the "bad" spells from a spell list
64 * @param m_idx モンスターの構造体参照ポインタ
65 * @param f4p モンスター魔法のフラグリスト1
66 * @param f5p モンスター魔法のフラグリスト2
67 * @param f6p モンスター魔法のフラグリスト3
70 static void remove_bad_spells(int m_idx, u32b *f4p, u32b *f5p, u32b *f6p)
72 monster_type *m_ptr = &m_list[m_idx];
73 monster_race *r_ptr = &r_info[m_ptr->r_idx];
82 /* Too stupid to know anything */
83 if (r_ptr->flags2 & RF2_STUPID) return;
86 /* Must be cheating or learning */
87 if (!smart_cheat && !smart_learn) return;
90 /* Update acquired knowledge */
93 /* Hack -- Occasionally forget player status */
94 /* Only save SM_FRIENDLY, SM_PET or SM_CLONED */
95 if (m_ptr->smart && (randint0(100) < 1)) m_ptr->smart &= (SM_FRIENDLY | SM_PET | SM_CLONED);
97 /* Use the memorized flags */
102 /* Cheat if requested */
105 /* Know basic info */
106 if (p_ptr->resist_acid) smart |= (SM_RES_ACID);
107 if (IS_OPPOSE_ACID()) smart |= (SM_OPP_ACID);
108 if (p_ptr->immune_acid) smart |= (SM_IMM_ACID);
109 if (p_ptr->resist_elec) smart |= (SM_RES_ELEC);
110 if (IS_OPPOSE_ELEC()) smart |= (SM_OPP_ELEC);
111 if (p_ptr->immune_elec) smart |= (SM_IMM_ELEC);
112 if (p_ptr->resist_fire) smart |= (SM_RES_FIRE);
113 if (IS_OPPOSE_FIRE()) smart |= (SM_OPP_FIRE);
114 if (p_ptr->immune_fire) smart |= (SM_IMM_FIRE);
115 if (p_ptr->resist_cold) smart |= (SM_RES_COLD);
116 if (IS_OPPOSE_COLD()) smart |= (SM_OPP_COLD);
117 if (p_ptr->immune_cold) smart |= (SM_IMM_COLD);
119 /* Know poison info */
120 if (p_ptr->resist_pois) smart |= (SM_RES_POIS);
121 if (IS_OPPOSE_POIS()) smart |= (SM_OPP_POIS);
123 /* Know special resistances */
124 if (p_ptr->resist_neth) smart |= (SM_RES_NETH);
125 if (p_ptr->resist_lite) smart |= (SM_RES_LITE);
126 if (p_ptr->resist_dark) smart |= (SM_RES_DARK);
127 if (p_ptr->resist_fear) smart |= (SM_RES_FEAR);
128 if (p_ptr->resist_conf) smart |= (SM_RES_CONF);
129 if (p_ptr->resist_chaos) smart |= (SM_RES_CHAOS);
130 if (p_ptr->resist_disen) smart |= (SM_RES_DISEN);
131 if (p_ptr->resist_blind) smart |= (SM_RES_BLIND);
132 if (p_ptr->resist_nexus) smart |= (SM_RES_NEXUS);
133 if (p_ptr->resist_sound) smart |= (SM_RES_SOUND);
134 if (p_ptr->resist_shard) smart |= (SM_RES_SHARD);
135 if (p_ptr->reflect) smart |= (SM_IMM_REFLECT);
137 /* Know bizarre "resistances" */
138 if (p_ptr->free_act) smart |= (SM_IMM_FREE);
139 if (!p_ptr->msp) smart |= (SM_IMM_MANA);
147 if (smart & SM_IMM_ACID)
149 f4 &= ~(RF4_BR_ACID);
150 f5 &= ~(RF5_BA_ACID);
151 f5 &= ~(RF5_BO_ACID);
153 else if ((smart & (SM_OPP_ACID)) && (smart & (SM_RES_ACID)))
155 if (int_outof(r_ptr, 80)) f4 &= ~(RF4_BR_ACID);
156 if (int_outof(r_ptr, 80)) f5 &= ~(RF5_BA_ACID);
157 if (int_outof(r_ptr, 80)) f5 &= ~(RF5_BO_ACID);
159 else if ((smart & (SM_OPP_ACID)) || (smart & (SM_RES_ACID)))
161 if (int_outof(r_ptr, 30)) f4 &= ~(RF4_BR_ACID);
162 if (int_outof(r_ptr, 30)) f5 &= ~(RF5_BA_ACID);
163 if (int_outof(r_ptr, 30)) f5 &= ~(RF5_BO_ACID);
167 if (smart & (SM_IMM_ELEC))
169 f4 &= ~(RF4_BR_ELEC);
170 f5 &= ~(RF5_BA_ELEC);
171 f5 &= ~(RF5_BO_ELEC);
173 else if ((smart & (SM_OPP_ELEC)) && (smart & (SM_RES_ELEC)))
175 if (int_outof(r_ptr, 80)) f4 &= ~(RF4_BR_ELEC);
176 if (int_outof(r_ptr, 80)) f5 &= ~(RF5_BA_ELEC);
177 if (int_outof(r_ptr, 80)) f5 &= ~(RF5_BO_ELEC);
179 else if ((smart & (SM_OPP_ELEC)) || (smart & (SM_RES_ELEC)))
181 if (int_outof(r_ptr, 30)) f4 &= ~(RF4_BR_ELEC);
182 if (int_outof(r_ptr, 30)) f5 &= ~(RF5_BA_ELEC);
183 if (int_outof(r_ptr, 30)) f5 &= ~(RF5_BO_ELEC);
187 if (smart & (SM_IMM_FIRE))
189 f4 &= ~(RF4_BR_FIRE);
190 f5 &= ~(RF5_BA_FIRE);
191 f5 &= ~(RF5_BO_FIRE);
193 else if ((smart & (SM_OPP_FIRE)) && (smart & (SM_RES_FIRE)))
195 if (int_outof(r_ptr, 80)) f4 &= ~(RF4_BR_FIRE);
196 if (int_outof(r_ptr, 80)) f5 &= ~(RF5_BA_FIRE);
197 if (int_outof(r_ptr, 80)) f5 &= ~(RF5_BO_FIRE);
199 else if ((smart & (SM_OPP_FIRE)) || (smart & (SM_RES_FIRE)))
201 if (int_outof(r_ptr, 30)) f4 &= ~(RF4_BR_FIRE);
202 if (int_outof(r_ptr, 30)) f5 &= ~(RF5_BA_FIRE);
203 if (int_outof(r_ptr, 30)) f5 &= ~(RF5_BO_FIRE);
207 if (smart & (SM_IMM_COLD))
209 f4 &= ~(RF4_BR_COLD);
210 f5 &= ~(RF5_BA_COLD);
211 f5 &= ~(RF5_BO_COLD);
212 f5 &= ~(RF5_BO_ICEE);
214 else if ((smart & (SM_OPP_COLD)) && (smart & (SM_RES_COLD)))
216 if (int_outof(r_ptr, 80)) f4 &= ~(RF4_BR_COLD);
217 if (int_outof(r_ptr, 80)) f5 &= ~(RF5_BA_COLD);
218 if (int_outof(r_ptr, 80)) f5 &= ~(RF5_BO_COLD);
219 if (int_outof(r_ptr, 80)) f5 &= ~(RF5_BO_ICEE);
221 else if ((smart & (SM_OPP_COLD)) || (smart & (SM_RES_COLD)))
223 if (int_outof(r_ptr, 30)) f4 &= ~(RF4_BR_COLD);
224 if (int_outof(r_ptr, 30)) f5 &= ~(RF5_BA_COLD);
225 if (int_outof(r_ptr, 30)) f5 &= ~(RF5_BO_COLD);
226 if (int_outof(r_ptr, 20)) f5 &= ~(RF5_BO_ICEE);
230 if ((smart & (SM_OPP_POIS)) && (smart & (SM_RES_POIS)))
232 if (int_outof(r_ptr, 80)) f4 &= ~(RF4_BR_POIS);
233 if (int_outof(r_ptr, 80)) f5 &= ~(RF5_BA_POIS);
234 if (int_outof(r_ptr, 60)) f4 &= ~(RF4_BA_NUKE);
235 if (int_outof(r_ptr, 60)) f4 &= ~(RF4_BR_NUKE);
237 else if ((smart & (SM_OPP_POIS)) || (smart & (SM_RES_POIS)))
239 if (int_outof(r_ptr, 30)) f4 &= ~(RF4_BR_POIS);
240 if (int_outof(r_ptr, 30)) f5 &= ~(RF5_BA_POIS);
244 if (smart & (SM_RES_NETH))
246 if (prace_is_(RACE_SPECTRE))
248 f4 &= ~(RF4_BR_NETH);
249 f5 &= ~(RF5_BA_NETH);
250 f5 &= ~(RF5_BO_NETH);
254 if (int_outof(r_ptr, 20)) f4 &= ~(RF4_BR_NETH);
255 if (int_outof(r_ptr, 50)) f5 &= ~(RF5_BA_NETH);
256 if (int_outof(r_ptr, 50)) f5 &= ~(RF5_BO_NETH);
260 if (smart & (SM_RES_LITE))
262 if (int_outof(r_ptr, 50)) f4 &= ~(RF4_BR_LITE);
263 if (int_outof(r_ptr, 50)) f5 &= ~(RF5_BA_LITE);
266 if (smart & (SM_RES_DARK))
268 if (prace_is_(RACE_VAMPIRE))
270 f4 &= ~(RF4_BR_DARK);
271 f5 &= ~(RF5_BA_DARK);
275 if (int_outof(r_ptr, 50)) f4 &= ~(RF4_BR_DARK);
276 if (int_outof(r_ptr, 50)) f5 &= ~(RF5_BA_DARK);
280 if (smart & (SM_RES_FEAR))
285 if (smart & (SM_RES_CONF))
288 if (int_outof(r_ptr, 50)) f4 &= ~(RF4_BR_CONF);
291 if (smart & (SM_RES_CHAOS))
293 if (int_outof(r_ptr, 20)) f4 &= ~(RF4_BR_CHAO);
294 if (int_outof(r_ptr, 50)) f4 &= ~(RF4_BA_CHAO);
297 if (smart & (SM_RES_DISEN))
299 if (int_outof(r_ptr, 40)) f4 &= ~(RF4_BR_DISE);
302 if (smart & (SM_RES_BLIND))
307 if (smart & (SM_RES_NEXUS))
309 if (int_outof(r_ptr, 50)) f4 &= ~(RF4_BR_NEXU);
310 f6 &= ~(RF6_TELE_LEVEL);
313 if (smart & (SM_RES_SOUND))
315 if (int_outof(r_ptr, 50)) f4 &= ~(RF4_BR_SOUN);
318 if (smart & (SM_RES_SHARD))
320 if (int_outof(r_ptr, 40)) f4 &= ~(RF4_BR_SHAR);
323 if (smart & (SM_IMM_REFLECT))
325 if (int_outof(r_ptr, 150)) f5 &= ~(RF5_BO_COLD);
326 if (int_outof(r_ptr, 150)) f5 &= ~(RF5_BO_FIRE);
327 if (int_outof(r_ptr, 150)) f5 &= ~(RF5_BO_ACID);
328 if (int_outof(r_ptr, 150)) f5 &= ~(RF5_BO_ELEC);
329 if (int_outof(r_ptr, 150)) f5 &= ~(RF5_BO_NETH);
330 if (int_outof(r_ptr, 150)) f5 &= ~(RF5_BO_WATE);
331 if (int_outof(r_ptr, 150)) f5 &= ~(RF5_BO_MANA);
332 if (int_outof(r_ptr, 150)) f5 &= ~(RF5_BO_PLAS);
333 if (int_outof(r_ptr, 150)) f5 &= ~(RF5_BO_ICEE);
334 if (int_outof(r_ptr, 150)) f5 &= ~(RF5_MISSILE);
335 if (int_outof(r_ptr, 150)) f4 &= ~(RF4_SHOOT);
338 if (smart & (SM_IMM_FREE))
344 if (smart & (SM_IMM_MANA))
346 f5 &= ~(RF5_DRAIN_MANA);
349 /* XXX XXX XXX 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(int y1, int 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)
403 s16b 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)
419 object_type *o_ptr = &o_list[this_o_idx];
421 /* Acquire next object */
422 next_o_idx = o_ptr->next_o_idx;
424 /* Known to be worthless? */
425 if (o_ptr->tval == TV_CORPSE)
427 if (!monster_has_hostile_align(m_ptr, 0, 0, &r_info[o_ptr->pval])) return TRUE;
438 * @brief モンスターにとってボルト型魔法が有効な状態かを返す /
439 * Determine if a bolt spell will hit the player.
440 * @param y1 ボルト魔法発射地点のY座標
441 * @param x1 ボルト魔法発射地点のX座標
442 * @param y2 ボルト魔法目標地点のY座標
443 * @param x2 ボルト魔法目標地点のX座標
444 * @param is_friend モンスターがプレイヤーに害意を持たない(ペットか友好的)ならばTRUEをつける
445 * @return ボルト型魔法が有効ならばTRUEを返す。
447 * Originally, it was possible for a friendly to shoot another friendly.\n
448 * Change it so a "clean shot" means no equally friendly monster is\n
449 * between the attacker and target.\n
451 * This is exactly like "projectable", but it will\n
452 * return FALSE if a monster is in the way.\n
453 * no equally friendly monster is\n
454 * between the attacker and target.\n
456 bool clean_shot(int y1, int x1, int y2, int x2, bool is_friend)
458 /* 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(int m_idx, int y, int x, int 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(int m_idx, int y, int x, int 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
573 * @param breath TRUEならばブ・E構萢・.ALSEならばボー・E萢
574 * @param monspell モンスター魔法のID
575 * @param target_type モンスターからモンスターへ撃つならMONSTER_TO_MONSTER、モンスターからプレイヤーならMONSTER_TO_PLAYER
578 void breath(int y, int x, int m_idx, int typ, int dam_hp, int 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);
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));
623 * @brief モンスターのボール型&ブレス型魔法処理 /
625 * @param o_ptr 呪いをかけられる装備オブジェクトの構造体参照ポインタ
628 u32b get_curse(int power, object_type *o_ptr)
634 new_curse = (1 << (randint0(MAX_CURSE)+4));
637 if (!(new_curse & TRC_HEAVY_MASK)) continue;
641 if (new_curse & TRC_SPECIAL_MASK) continue;
645 if (new_curse & TRC_HEAVY_MASK) continue;
647 if (new_curse == TRC_LOW_MELEE && !object_is_weapon(o_ptr)) continue;
648 if (new_curse == TRC_LOW_AC && !object_is_armour(o_ptr)) continue;
655 * @brief 装備への呪い付加判定と付加処理 /
656 * @param chance 呪いの基本確率
657 * @param heavy_chance 重い呪いを選択肢に入れるか否か。
660 void curse_equipment(int chance, int heavy_chance)
662 bool changed = FALSE;
665 u32b oflgs[TR_FLAG_SIZE];
666 object_type *o_ptr = &inventory[INVEN_RARM + randint0(12)];
667 char o_name[MAX_NLEN];
669 if (randint1(100) > chance) return;
671 if (!o_ptr->k_idx) return;
673 object_flags(o_ptr, oflgs);
675 object_desc(o_name, o_ptr, (OD_OMIT_PREFIX | OD_NAME_ONLY));
677 /* Extra, biased saving throw for blessed items */
678 if (have_flag(oflgs, TR_BLESSED) && (randint1(888) > chance))
681 msg_format("%sは呪いを跳ね返した!", o_name,
683 msg_format("Your %s resist%s cursing!", o_name,
686 ((o_ptr->number > 1) ? "" : "s"));
687 /* Hmmm -- can we wear multiple items? If not, this is unnecessary */
691 if ((randint1(100) <= heavy_chance) &&
692 (object_is_artifact(o_ptr) || object_is_ego(o_ptr)))
694 if (!(o_ptr->curse_flags & TRC_HEAVY_CURSE))
696 o_ptr->curse_flags |= TRC_HEAVY_CURSE;
697 o_ptr->curse_flags |= TRC_CURSED;
702 if (!object_is_cursed(o_ptr))
704 o_ptr->curse_flags |= TRC_CURSED;
706 if (heavy_chance >= 50) curse_power++;
708 new_curse = get_curse(curse_power, o_ptr);
709 if (!(o_ptr->curse_flags & new_curse))
712 o_ptr->curse_flags |= new_curse;
718 msg_format("悪意に満ちた黒いオーラが%sをとりまいた...", o_name);
720 msg_format("There is a malignant black aura surrounding %s...", o_name);
723 o_ptr->feeling = FEEL_NONE;
725 p_ptr->update |= (PU_BONUS);
730 * @brief ID値が正しいモンスター魔法IDかどうかを返す /
731 * Return TRUE if a spell is good for hurting the player (directly).
732 * @param spell 判定対象のID
733 * @return 正しいIDならばTRUEを返す。
735 static bool spell_attack(byte spell)
737 /* All RF4 spells hurt (except for shriek and dispel) */
738 if (spell < 128 && spell > 98) return (TRUE);
740 /* Various "ball" spells */
741 if (spell >= 128 && spell <= 128 + 8) return (TRUE);
743 /* "Cause wounds" and "bolt" spells */
744 if (spell >= 128 + 12 && spell < 128 + 27) return (TRUE);
747 if (spell == 160 + 1) return (TRUE);
750 if (spell == 160 + 11) return (TRUE);
758 * @brief ID値が退避目的に適したモンスター魔法IDかどうかを返す /
759 * Return TRUE if a spell is good for escaping.
760 * @param spell 判定対象のID
761 * @return 適した魔法のIDならばTRUEを返す。
763 static bool spell_escape(byte spell)
765 /* Blink or Teleport */
766 if (spell == 160 + 4 || spell == 160 + 5) return (TRUE);
768 /* Teleport the player away */
769 if (spell == 160 + 9 || spell == 160 + 10) return (TRUE);
771 /* Isn't good for escaping */
776 * @brief ID値が妨害目的に適したモンスター魔法IDかどうかを返す /
777 * Return TRUE if a spell is good for annoying the player.
778 * @param spell 判定対象のID
779 * @return 適した魔法のIDならばTRUEを返す。
781 static bool spell_annoy(byte spell)
784 if (spell == 96 + 0) return (TRUE);
786 /* Brain smash, et al (added curses) */
787 if (spell >= 128 + 9 && spell <= 128 + 14) return (TRUE);
789 /* Scare, confuse, blind, slow, paralyze */
790 if (spell >= 128 + 27 && spell <= 128 + 31) return (TRUE);
793 if (spell == 160 + 8) return (TRUE);
796 if (spell == 160 + 10) return (TRUE);
798 /* Darkness, make traps, cause amnesia */
799 if (spell >= 160 + 12 && spell <= 160 + 14) return (TRUE);
806 * @brief ID値が召喚型のモンスター魔法IDかどうかを返す /
807 * Return TRUE if a spell is good for annoying the player.
808 * @param spell 判定対象のID
809 * @return 召喚型魔法のIDならばTRUEを返す。
811 static bool spell_summon(byte spell)
813 /* All summon spells */
814 if (spell >= 160 + 16) return (TRUE);
822 * @brief ID値が死者復活処理かどうかを返す /
823 * Return TRUE if a spell is good for annoying the player.
824 * @param spell 判定対象のID
825 * @return 死者復活の処理ならばTRUEを返す。
827 static bool spell_raise(byte spell)
829 /* All raise-dead spells */
830 if (spell == 160 + 15) return (TRUE);
837 * @brief ID値が戦術的なモンスター魔法IDかどうかを返す /
838 * Return TRUE if a spell is good in a tactical situation.
839 * @param spell 判定対象のID
840 * @return 戦術的な魔法のIDならばTRUEを返す。
842 static bool spell_tactic(byte spell)
845 if (spell == 160 + 4) return (TRUE);
852 * @brief ID値が無敵化するモンスター魔法IDかどうかを返す /
853 * Return TRUE if a spell makes invulnerable.
854 * @param spell 判定対象のID
855 * @return 召喚型魔法のIDならばTRUEを返す。
857 static bool spell_invulner(byte spell)
859 /* Invulnerability */
860 if (spell == 160 + 3) return (TRUE);
862 /* No invulnerability */
867 * @brief ID値が加速するモンスター魔法IDかどうかを返す /
868 * Return TRUE if a spell hastes.
869 * @param spell 判定対象のID
870 * @return 召喚型魔法のIDならばTRUEを返す。
872 static bool spell_haste(byte spell)
875 if (spell == 160 + 0) return (TRUE);
877 /* Not a haste spell */
883 * @brief ID値が時間停止を行うモンスター魔法IDかどうかを返す /
884 * Return TRUE if a spell world.
885 * @param spell 判定対象のID
886 * @return 時間停止魔法のIDならばTRUEを返す。
888 static bool spell_world(byte spell)
890 if (spell == 160 + 6) return (TRUE);
896 * @brief ID値が特別効果のモンスター魔法IDかどうかを返す /
897 * Return TRUE if a spell special.
898 * @param spell 判定対象のID
899 * @return 特別効果魔法のIDならばTRUEを返す。
901 static bool spell_special(byte spell)
903 if (p_ptr->inside_battle) return FALSE;
904 if (spell == 160 + 7) return (TRUE);
910 * @brief ID値が光の剣のモンスター魔法IDかどうかを返す /
911 * Return TRUE if a spell psycho-spear.
912 * @param spell 判定対象のID
913 * @return 光の剣のIDならばTRUEを返す。
915 static bool spell_psy_spe(byte spell)
918 if (spell == 160 + 11) return (TRUE);
920 /* Not a haste spell */
926 * @brief ID値が治癒魔法かどうかを返す /
927 * Return TRUE if a spell is good for healing.
928 * @param spell 判定対象のID
929 * @return 治癒魔法のIDならばTRUEを返す。
931 static bool spell_heal(byte spell)
934 if (spell == 160 + 2) return (TRUE);
942 * @brief ID値が魔力消去かどうかを返す /
943 * Return TRUE if a spell is good for dispel.
944 * @param spell 判定対象のID
945 * @return 魔力消去のIDならばTRUEを返す。
947 static bool spell_dispel(byte spell)
950 if (spell == 96 + 2) return (TRUE);
958 * @brief モンスターがプレイヤーに魔力消去を与えるべきかを判定するルーチン
959 * Check should monster cast dispel spell.
960 * @param m_idx モンスターの構造体配列ID
961 * @return 魔力消去をかけるべきならTRUEを返す。
963 bool dispel_check(int m_idx)
965 monster_type *m_ptr = &m_list[m_idx];
966 monster_race *r_ptr = &r_info[m_ptr->r_idx];
968 /* Invulnabilty (including the song) */
969 if (IS_INVULN()) return (TRUE);
972 if (p_ptr->wraith_form) return (TRUE);
975 if (p_ptr->shield) return (TRUE);
978 if (p_ptr->magicdef) return (TRUE);
981 if (p_ptr->multishadow) return (TRUE);
984 if (p_ptr->dustrobe) return (TRUE);
986 /* Berserk Strength */
987 if (p_ptr->shero && (p_ptr->pclass != CLASS_BERSERKER)) return (TRUE);
990 if (p_ptr->mimic_form == MIMIC_DEMON_LORD) return (TRUE);
992 /* Elemental resistances */
993 if (r_ptr->flags4 & RF4_BR_ACID)
995 if (!p_ptr->immune_acid && (p_ptr->oppose_acid || music_singing(MUSIC_RESIST))) return (TRUE);
996 if (p_ptr->special_defense & DEFENSE_ACID) return (TRUE);
999 if (r_ptr->flags4 & RF4_BR_FIRE)
1001 if (!((p_ptr->prace == RACE_DEMON) && p_ptr->lev > 44))
1003 if (!p_ptr->immune_fire && (p_ptr->oppose_fire || music_singing(MUSIC_RESIST))) return (TRUE);
1004 if (p_ptr->special_defense & DEFENSE_FIRE) return (TRUE);
1008 if (r_ptr->flags4 & RF4_BR_ELEC)
1010 if (!p_ptr->immune_elec && (p_ptr->oppose_elec || music_singing(MUSIC_RESIST))) return (TRUE);
1011 if (p_ptr->special_defense & DEFENSE_ELEC) return (TRUE);
1014 if (r_ptr->flags4 & RF4_BR_COLD)
1016 if (!p_ptr->immune_cold && (p_ptr->oppose_cold || music_singing(MUSIC_RESIST))) return (TRUE);
1017 if (p_ptr->special_defense & DEFENSE_COLD) return (TRUE);
1020 if (r_ptr->flags4 & (RF4_BR_POIS | RF4_BR_NUKE))
1022 if (!((p_ptr->pclass == CLASS_NINJA) && p_ptr->lev > 44))
1024 if (p_ptr->oppose_pois || music_singing(MUSIC_RESIST)) return (TRUE);
1025 if (p_ptr->special_defense & DEFENSE_POIS) return (TRUE);
1029 /* Ultimate resistance */
1030 if (p_ptr->ult_res) return (TRUE);
1032 /* Potion of Neo Tsuyosi special */
1033 if (p_ptr->tsuyoshi) return (TRUE);
1035 /* Elemental Brands */
1036 if ((p_ptr->special_attack & ATTACK_ACID) && !(r_ptr->flagsr & RFR_EFF_IM_ACID_MASK)) return (TRUE);
1037 if ((p_ptr->special_attack & ATTACK_FIRE) && !(r_ptr->flagsr & RFR_EFF_IM_FIRE_MASK)) return (TRUE);
1038 if ((p_ptr->special_attack & ATTACK_ELEC) && !(r_ptr->flagsr & RFR_EFF_IM_ELEC_MASK)) return (TRUE);
1039 if ((p_ptr->special_attack & ATTACK_COLD) && !(r_ptr->flagsr & RFR_EFF_IM_COLD_MASK)) return (TRUE);
1040 if ((p_ptr->special_attack & ATTACK_POIS) && !(r_ptr->flagsr & RFR_EFF_IM_POIS_MASK)) return (TRUE);
1043 if (p_ptr->pspeed < 145)
1045 if (IS_FAST()) return (TRUE);
1049 if (p_ptr->lightspeed && (m_ptr->mspeed < 136)) return (TRUE);
1051 if (p_ptr->riding && (m_list[p_ptr->riding].mspeed < 135))
1053 if (MON_FAST(&m_list[p_ptr->riding])) return (TRUE);
1056 /* No need to cast dispel spell */
1062 * @brief モンスターの魔法選択ルーチン
1063 * Have a monster choose a spell from a list of "useful" spells.
1064 * @param m_idx モンスターの構造体配列ID
1065 * @param spells 候補魔法IDをまとめた配列
1066 * @param num spellsの長さ
1067 * @return 選択したモンスター魔法のID
1069 * Note that this list does NOT include spells that will just hit\n
1070 * other monsters, and the list is restricted when the monster is\n
1071 * "desperate". Should that be the job of this function instead?\n
1073 * Stupid monsters will just pick a spell randomly. Smart monsters\n
1074 * will choose more "intelligently".\n
1076 * Use the helper functions above to put spells into categories.\n
1078 * This function may well be an efficiency bottleneck.\n
1080 static int choose_attack_spell(int m_idx, byte spells[], byte num)
1082 monster_type *m_ptr = &m_list[m_idx];
1083 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1085 byte escape[96], escape_num = 0;
1086 byte attack[96], attack_num = 0;
1087 byte summon[96], summon_num = 0;
1088 byte tactic[96], tactic_num = 0;
1089 byte annoy[96], annoy_num = 0;
1090 byte invul[96], invul_num = 0;
1091 byte haste[96], haste_num = 0;
1092 byte world[96], world_num = 0;
1093 byte special[96], special_num = 0;
1094 byte psy_spe[96], psy_spe_num = 0;
1095 byte raise[96], raise_num = 0;
1096 byte heal[96], heal_num = 0;
1097 byte dispel[96], dispel_num = 0;
1101 /* Stupid monsters choose randomly */
1102 if (r_ptr->flags2 & (RF2_STUPID))
1104 /* Pick at random */
1105 return (spells[randint0(num)]);
1108 /* Categorize spells */
1109 for (i = 0; i < num; i++)
1112 if (spell_escape(spells[i])) escape[escape_num++] = spells[i];
1115 if (spell_attack(spells[i])) attack[attack_num++] = spells[i];
1118 if (spell_summon(spells[i])) summon[summon_num++] = spells[i];
1120 /* Tactical spell? */
1121 if (spell_tactic(spells[i])) tactic[tactic_num++] = spells[i];
1123 /* Annoyance spell? */
1124 if (spell_annoy(spells[i])) annoy[annoy_num++] = spells[i];
1126 /* Invulnerability spell? */
1127 if (spell_invulner(spells[i])) invul[invul_num++] = spells[i];
1130 if (spell_haste(spells[i])) haste[haste_num++] = spells[i];
1133 if (spell_world(spells[i])) world[world_num++] = spells[i];
1135 /* Special spell? */
1136 if (spell_special(spells[i])) special[special_num++] = spells[i];
1138 /* Psycho-spear spell? */
1139 if (spell_psy_spe(spells[i])) psy_spe[psy_spe_num++] = spells[i];
1141 /* Raise-dead spell? */
1142 if (spell_raise(spells[i])) raise[raise_num++] = spells[i];
1145 if (spell_heal(spells[i])) heal[heal_num++] = spells[i];
1148 if (spell_dispel(spells[i])) dispel[dispel_num++] = spells[i];
1151 /*** Try to pick an appropriate spell type ***/
1154 if (world_num && (randint0(100) < 15) && !world_monster)
1156 /* Choose haste spell */
1157 return (world[randint0(world_num)]);
1163 bool success = FALSE;
1164 switch(m_ptr->r_idx)
1168 if ((m_ptr->hp < m_ptr->maxhp / 2) && r_info[MON_BANOR].max_num && r_info[MON_LUPART].max_num) success = TRUE;
1172 if (success) return (special[randint0(special_num)]);
1175 /* Still hurt badly, couldn't flee, attempt to heal */
1176 if (m_ptr->hp < m_ptr->maxhp / 3 && one_in_(2))
1178 /* Choose heal spell if possible */
1179 if (heal_num) return (heal[randint0(heal_num)]);
1182 /* Hurt badly or afraid, attempt to flee */
1183 if (((m_ptr->hp < m_ptr->maxhp / 3) || MON_MONFEAR(m_ptr)) && one_in_(2))
1185 /* Choose escape spell if possible */
1186 if (escape_num) return (escape[randint0(escape_num)]);
1192 bool success = FALSE;
1193 switch (m_ptr->r_idx)
1199 case MON_BANORLUPART:
1200 if (randint0(100) < 70) success = TRUE;
1203 if (randint0(100) < 40) success = TRUE;
1206 if (randint0(100) < 50) success = TRUE;
1209 if (success) return (special[randint0(special_num)]);
1212 /* Player is close and we have attack spells, blink away */
1213 if ((distance(py, px, m_ptr->fy, m_ptr->fx) < 4) && (attack_num || (r_ptr->flags6 & RF6_TRAPS)) && (randint0(100) < 75) && !world_monster)
1215 /* Choose tactical spell */
1216 if (tactic_num) return (tactic[randint0(tactic_num)]);
1219 /* Summon if possible (sometimes) */
1220 if (summon_num && (randint0(100) < 40))
1222 /* Choose summon spell */
1223 return (summon[randint0(summon_num)]);
1227 if (dispel_num && one_in_(2))
1229 /* Choose dispel spell if possible */
1230 if (dispel_check(m_idx))
1232 return (dispel[randint0(dispel_num)]);
1236 /* Raise-dead if possible (sometimes) */
1237 if (raise_num && (randint0(100) < 40))
1239 /* Choose raise-dead spell */
1240 return (raise[randint0(raise_num)]);
1243 /* Attack spell (most of the time) */
1246 if (psy_spe_num && (randint0(100) < 50))
1248 /* Choose attack spell */
1249 return (psy_spe[randint0(psy_spe_num)]);
1251 else if (attack_num && (randint0(100) < 40))
1253 /* Choose attack spell */
1254 return (attack[randint0(attack_num)]);
1257 else if (attack_num && (randint0(100) < 85))
1259 /* Choose attack spell */
1260 return (attack[randint0(attack_num)]);
1263 /* Try another tactical spell (sometimes) */
1264 if (tactic_num && (randint0(100) < 50) && !world_monster)
1266 /* Choose tactic spell */
1267 return (tactic[randint0(tactic_num)]);
1270 /* Cast globe of invulnerability if not already in effect */
1271 if (invul_num && !m_ptr->mtimed[MTIMED_INVULNER] && (randint0(100) < 50))
1273 /* Choose Globe of Invulnerability */
1274 return (invul[randint0(invul_num)]);
1277 /* We're hurt (not badly), try to heal */
1278 if ((m_ptr->hp < m_ptr->maxhp * 3 / 4) && (randint0(100) < 25))
1280 /* Choose heal spell if possible */
1281 if (heal_num) return (heal[randint0(heal_num)]);
1284 /* Haste self if we aren't already somewhat hasted (rarely) */
1285 if (haste_num && (randint0(100) < 20) && !MON_FAST(m_ptr))
1287 /* Choose haste spell */
1288 return (haste[randint0(haste_num)]);
1291 /* Annoy player (most of the time) */
1292 if (annoy_num && (randint0(100) < 80))
1294 /* Choose annoyance spell */
1295 return (annoy[randint0(annoy_num)]);
1298 /* Choose no spell */
1304 * @brief ID値が非魔術的な特殊技能かどうかを返す /
1305 * Return TRUE if a spell is inate spell.
1306 * @param spell 判定対象のID
1307 * @return 非魔術的な特殊技能ならばTRUEを返す。
1309 bool spell_is_inate(u16b spell)
1311 if (spell < 32 * 4) /* Set RF4 */
1313 if ((1L << (spell - 32 * 3)) & RF4_NOMAGIC_MASK) return TRUE;
1315 else if (spell < 32 * 5) /* Set RF5 */
1317 if ((1L << (spell - 32 * 4)) & RF5_NOMAGIC_MASK) return TRUE;
1319 else if (spell < 32 * 6) /* Set RF6 */
1321 if ((1L << (spell - 32 * 5)) & RF6_NOMAGIC_MASK) return TRUE;
1324 /* This spell is not "inate" */
1330 * @brief モンスターがプレイヤーにダメージを与えるための最適な座標を算出する /
1331 * @param m_ptr 技能を使用するモンスター構造体の参照ポインタ
1332 * @param yp 最適な目標地点のY座標を返す参照ポインタ
1333 * @param xp 最適な目標地点のX座標を返す参照ポインタ
1334 * @param f_flag 射線に入れるのを避ける地形の所持フラグ
1335 * @param path_check 射線を判定するための関数ポインタ
1336 * @return 有効な座標があった場合TRUEを返す
1338 static bool adjacent_grid_check(monster_type *m_ptr, int *yp, int *xp,
1339 int f_flag, bool (*path_check)(int, int, int, int))
1343 static int tonari_y[4][8] = {{-1, -1, -1, 0, 0, 1, 1, 1},
1344 {-1, -1, -1, 0, 0, 1, 1, 1},
1345 { 1, 1, 1, 0, 0, -1, -1, -1},
1346 { 1, 1, 1, 0, 0, -1, -1, -1}};
1347 static int tonari_x[4][8] = {{-1, 0, 1, -1, 1, -1, 0, 1},
1348 { 1, 0, -1, 1, -1, 1, 0, -1},
1349 {-1, 0, 1, -1, 1, -1, 0, 1},
1350 { 1, 0, -1, 1, -1, 1, 0, -1}};
1352 if (m_ptr->fy < py && m_ptr->fx < px) tonari = 0;
1353 else if (m_ptr->fy < py) tonari = 1;
1354 else if (m_ptr->fx < px) tonari = 2;
1357 for (i = 0; i < 8; i++)
1359 int next_x = *xp + tonari_x[tonari][i];
1360 int next_y = *yp + tonari_y[tonari][i];
1363 /* Access the next grid */
1364 c_ptr = &cave[next_y][next_x];
1366 /* Skip this feature */
1367 if (!cave_have_flag_grid(c_ptr, f_flag)) continue;
1369 if (path_check(m_ptr->fy, m_ptr->fx, next_y, next_x))
1380 #define DO_SPELL_NONE 0
1381 #define DO_SPELL_BR_LITE 1
1382 #define DO_SPELL_BR_DISI 2
1383 #define DO_SPELL_BA_LITE 3
1386 * @brief モンスターの特殊技能メインルーチン /
1387 * Creatures can cast spells, shoot missiles, and breathe.
1388 * @param m_idx モンスター構造体配列のID
1389 * @return 実際に特殊技能を利用したらTRUEを返す
1391 * Returns "TRUE" if a spell (or whatever) was (successfully) cast.\n
1393 * XXX XXX XXX This function could use some work, but remember to\n
1394 * keep it as optimized as possible, while retaining generic code.\n
1396 * Verify the various "blind-ness" checks in the code.\n
1398 * XXX XXX XXX Note that several effects should really not be "seen"\n
1399 * if the player is blind. See also "effects.c" for other "mistakes".\n
1401 * Perhaps monsters should breathe at locations *near* the player,\n
1402 * since this would allow them to inflict "partial" damage.\n
1404 * Perhaps smart monsters should decline to use "bolt" spells if\n
1405 * there is a monster in the way, unless they wish to kill it.\n
1407 * Note that, to allow the use of the "track_target" option at some\n
1408 * later time, certain non-optimal things are done in the code below,\n
1409 * including explicit checks against the "direct" variable, which is\n
1410 * currently always true by the time it is checked, but which should\n
1411 * really be set according to an explicit "projectable()" test, and\n
1412 * the use of generic "x,y" locations instead of the player location,\n
1413 * with those values being initialized with the player location.\n
1415 * It will not be possible to "correctly" handle the case in which a\n
1416 * monster attempts to attack a location which is thought to contain\n
1417 * the player, but which in fact is nowhere near the player, since this\n
1418 * might induce all sorts of messages about the attack itself, and about\n
1419 * the effects of the attack, which the player might or might not be in\n
1420 * a position to observe. Thus, for simplicity, it is probably best to\n
1421 * only allow "faulty" attacks by a monster if one of the important grids\n
1422 * (probably the initial or final grid) is in fact in view of the player.\n
1423 * It may be necessary to actually prevent spell attacks except when the\n
1424 * monster actually has line of sight to the player. Note that a monster\n
1425 * could be left in a bizarre situation after the player ducked behind a\n
1426 * pillar and then teleported away, for example.\n
1429 * that certain spell attacks do not use the "project()" function\n
1430 * but "simulate" it via the "direct" variable, which is always at least\n
1431 * as restrictive as the "project()" function. This is necessary to\n
1432 * prevent "blindness" attacks and such from bending around walls, etc,\n
1433 * and to allow the use of the "track_target" option in the future.\n
1435 * Note that this function attempts to optimize the use of spells for the\n
1436 * cases in which the monster has no spells, or has spells but cannot use\n
1437 * them, or has spells but they will have no "useful" effect. Note that\n
1438 * this function has been an efficiency bottleneck in the past.\n
1440 * Note the special "MFLAG_NICE" flag, which prevents a monster from using\n
1441 * any spell attacks until the player has had a single chance to move.\n
1443 bool make_attack_spell(int m_idx)
1445 int k, thrown_spell = 0, rlev, failrate;
1446 byte spell[96], num = 0;
1448 monster_type *m_ptr = &m_list[m_idx];
1449 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1454 bool no_inate = FALSE;
1455 bool do_spell = DO_SPELL_NONE;
1457 int rad = 0; //For elemental spells
1459 /* Target location */
1463 /* Target location for lite breath */
1470 /* Extract the "see-able-ness" */
1471 bool seen = (!p_ptr->blind && m_ptr->ml);
1472 bool maneable = player_has_los_bold(m_ptr->fy, m_ptr->fx);
1474 /* Check "projectable" */
1477 bool in_no_magic_dungeon = (d_info[dungeon_type].flags1 & DF1_NO_MAGIC) && dun_level
1478 && (!p_ptr->inside_quest || is_fixed_quest_idx(p_ptr->inside_quest));
1480 bool can_use_lite_area = FALSE;
1484 /* Cannot cast spells when confused */
1485 if (MON_CONFUSED(m_ptr))
1487 reset_target(m_ptr);
1491 /* Cannot cast spells when nice */
1492 if (m_ptr->mflag & MFLAG_NICE) return (FALSE);
1493 if (!is_hostile(m_ptr)) return (FALSE);
1496 /* Sometimes forbid inate attacks (breaths) */
1497 if (randint0(100) >= (r_ptr->freq_spell * 2)) no_inate = TRUE;
1499 /* XXX XXX XXX Handle "track_target" option (?) */
1502 /* Extract the racial spell flags */
1507 /*** require projectable player ***/
1510 if ((m_ptr->cdis > MAX_RANGE) && !m_ptr->target_y) return (FALSE);
1512 /* Check path for lite breath */
1513 if (f4 & RF4_BR_LITE)
1518 if (los(m_ptr->fy, m_ptr->fx, y_br_lite, x_br_lite))
1520 feature_type *f_ptr = &f_info[cave[y_br_lite][x_br_lite].feat];
1522 if (!have_flag(f_ptr->flags, FF_LOS))
1524 if (have_flag(f_ptr->flags, FF_PROJECT) && one_in_(2)) f4 &= ~(RF4_BR_LITE);
1528 /* Check path to next grid */
1529 else if (!adjacent_grid_check(m_ptr, &y_br_lite, &x_br_lite, FF_LOS, los)) f4 &= ~(RF4_BR_LITE);
1531 /* Don't breath lite to the wall if impossible */
1532 if (!(f4 & RF4_BR_LITE))
1540 if (projectable(m_ptr->fy, m_ptr->fx, y, x))
1542 feature_type *f_ptr = &f_info[cave[y][x].feat];
1544 if (!have_flag(f_ptr->flags, FF_PROJECT))
1546 /* Breath disintegration to the wall if possible */
1547 if ((f4 & RF4_BR_DISI) && have_flag(f_ptr->flags, FF_HURT_DISI) && one_in_(2)) do_spell = DO_SPELL_BR_DISI;
1549 /* Breath lite to the transparent wall if possible */
1550 else if ((f4 & RF4_BR_LITE) && have_flag(f_ptr->flags, FF_LOS) && one_in_(2)) do_spell = DO_SPELL_BR_LITE;
1554 /* Check path to next grid */
1557 bool success = FALSE;
1559 if ((f4 & RF4_BR_DISI) && (m_ptr->cdis < MAX_RANGE/2) &&
1560 in_disintegration_range(m_ptr->fy, m_ptr->fx, y, x) &&
1561 (one_in_(10) || (projectable(y, x, m_ptr->fy, m_ptr->fx) && one_in_(2))))
1563 do_spell = DO_SPELL_BR_DISI;
1566 else if ((f4 & RF4_BR_LITE) && (m_ptr->cdis < MAX_RANGE/2) &&
1567 los(m_ptr->fy, m_ptr->fx, y, x) && one_in_(5))
1569 do_spell = DO_SPELL_BR_LITE;
1572 else if ((f5 & RF5_BA_LITE) && (m_ptr->cdis <= MAX_RANGE))
1575 get_project_point(m_ptr->fy, m_ptr->fx, &by, &bx, 0L);
1576 if ((distance(by, bx, y, x) <= 3) && los(by, bx, y, x) && one_in_(5))
1578 do_spell = DO_SPELL_BA_LITE;
1583 if (!success) success = adjacent_grid_check(m_ptr, &y, &x, FF_PROJECT, projectable);
1587 if (m_ptr->target_y && m_ptr->target_x)
1589 y = m_ptr->target_y;
1590 x = m_ptr->target_x;
1591 f4 &= (RF4_INDIRECT_MASK);
1592 f5 &= (RF5_INDIRECT_MASK);
1593 f6 &= (RF6_INDIRECT_MASK);
1597 if (y_br_lite && x_br_lite && (m_ptr->cdis < MAX_RANGE/2) && one_in_(5))
1603 do_spell = DO_SPELL_BR_LITE;
1606 else f4 |= (RF4_BR_LITE);
1611 if (!success) return FALSE;
1614 reset_target(m_ptr);
1616 /* Extract the monster level */
1617 rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
1619 /* Forbid inate attacks sometimes */
1622 f4 &= ~(RF4_NOMAGIC_MASK);
1623 f5 &= ~(RF5_NOMAGIC_MASK);
1624 f6 &= ~(RF6_NOMAGIC_MASK);
1627 if (f6 & RF6_DARKNESS)
1629 if ((p_ptr->pclass == CLASS_NINJA) &&
1630 !(r_ptr->flags3 & (RF3_UNDEAD | RF3_HURT_LITE)) &&
1631 !(r_ptr->flags7 & RF7_DARK_MASK))
1632 can_use_lite_area = TRUE;
1634 if (!(r_ptr->flags2 & RF2_STUPID))
1636 if (d_info[dungeon_type].flags1 & DF1_DARKNESS) f6 &= ~(RF6_DARKNESS);
1637 else if ((p_ptr->pclass == CLASS_NINJA) && !can_use_lite_area) f6 &= ~(RF6_DARKNESS);
1641 if (in_no_magic_dungeon && !(r_ptr->flags2 & RF2_STUPID))
1643 f4 &= (RF4_NOMAGIC_MASK);
1644 f5 &= (RF5_NOMAGIC_MASK);
1645 f6 &= (RF6_NOMAGIC_MASK);
1648 if (r_ptr->flags2 & RF2_SMART)
1650 /* Hack -- allow "desperate" spells */
1651 if ((m_ptr->hp < m_ptr->maxhp / 10) &&
1652 (randint0(100) < 50))
1654 /* Require intelligent spells */
1655 f4 &= (RF4_INT_MASK);
1656 f5 &= (RF5_INT_MASK);
1657 f6 &= (RF6_INT_MASK);
1660 /* Hack -- decline "teleport level" in some case */
1661 if ((f6 & RF6_TELE_LEVEL) && TELE_LEVEL_IS_INEFF(0))
1663 f6 &= ~(RF6_TELE_LEVEL);
1667 /* No spells left */
1668 if (!f4 && !f5 && !f6) return (FALSE);
1670 /* Remove the "ineffective" spells */
1671 remove_bad_spells(m_idx, &f4, &f5, &f6);
1673 if (p_ptr->inside_arena || p_ptr->inside_battle)
1675 f4 &= ~(RF4_SUMMON_MASK);
1676 f5 &= ~(RF5_SUMMON_MASK);
1677 f6 &= ~(RF6_SUMMON_MASK | RF6_TELE_LEVEL);
1679 if (m_ptr->r_idx == MON_ROLENTO) f6 &= ~(RF6_SPECIAL);
1682 /* No spells left */
1683 if (!f4 && !f5 && !f6) return (FALSE);
1685 if (!(r_ptr->flags2 & RF2_STUPID))
1687 if (!p_ptr->csp) f5 &= ~(RF5_DRAIN_MANA);
1689 /* Check for a clean bolt shot */
1690 if (((f4 & RF4_BOLT_MASK) ||
1691 (f5 & RF5_BOLT_MASK) ||
1692 (f6 & RF6_BOLT_MASK)) &&
1693 !clean_shot(m_ptr->fy, m_ptr->fx, py, px, FALSE))
1695 /* Remove spells that will only hurt friends */
1696 f4 &= ~(RF4_BOLT_MASK);
1697 f5 &= ~(RF5_BOLT_MASK);
1698 f6 &= ~(RF6_BOLT_MASK);
1701 /* Check for a possible summon */
1702 if (((f4 & RF4_SUMMON_MASK) ||
1703 (f5 & RF5_SUMMON_MASK) ||
1704 (f6 & RF6_SUMMON_MASK)) &&
1705 !(summon_possible(y, x)))
1707 /* Remove summoning spells */
1708 f4 &= ~(RF4_SUMMON_MASK);
1709 f5 &= ~(RF5_SUMMON_MASK);
1710 f6 &= ~(RF6_SUMMON_MASK);
1713 /* Check for a possible raise dead */
1714 if ((f6 & RF6_RAISE_DEAD) && !raise_possible(m_ptr))
1716 /* Remove raise dead spell */
1717 f6 &= ~(RF6_RAISE_DEAD);
1720 /* Special moves restriction */
1721 if (f6 & RF6_SPECIAL)
1723 if ((m_ptr->r_idx == MON_ROLENTO) && !summon_possible(y, x))
1725 f6 &= ~(RF6_SPECIAL);
1729 /* No spells left */
1730 if (!f4 && !f5 && !f6) return (FALSE);
1733 /* Extract the "inate" spells */
1734 for (k = 0; k < 32; k++)
1736 if (f4 & (1L << k)) spell[num++] = k + RF4_SPELL_START;
1739 /* Extract the "normal" spells */
1740 for (k = 0; k < 32; k++)
1742 if (f5 & (1L << k)) spell[num++] = k + RF5_SPELL_START;
1745 /* Extract the "bizarre" spells */
1746 for (k = 0; k < 32; k++)
1748 if (f6 & (1L << k)) spell[num++] = k + RF6_SPELL_START;
1751 /* No spells left */
1752 if (!num) return (FALSE);
1754 /* Stop if player is dead or gone */
1755 if (!p_ptr->playing || p_ptr->is_dead) return (FALSE);
1757 /* Stop if player is leaving */
1758 if (p_ptr->leaving) return (FALSE);
1760 /* Get the monster name (or "it") */
1761 monster_desc(m_name, m_ptr, 0x00);
1764 /* Get the monster possessive ("his"/"her"/"its") */
1765 monster_desc(m_poss, m_ptr, MD_PRON_VISIBLE | MD_POSSESSIVE);
1775 thrown_spell = choose_attack_spell(m_idx, spell, num);
1776 if (thrown_spell) break;
1781 case DO_SPELL_BR_LITE:
1782 thrown_spell = 96+14; /* RF4_BR_LITE */
1785 case DO_SPELL_BR_DISI:
1786 thrown_spell = 96+31; /* RF4_BR_DISI */
1789 case DO_SPELL_BA_LITE:
1790 thrown_spell = 128+20; /* RF5_BA_LITE */
1794 return FALSE; /* Paranoia */
1797 /* Abort if no spell was chosen */
1798 if (!thrown_spell) return (FALSE);
1800 /* Calculate spell failure rate */
1801 failrate = 25 - (rlev + 3) / 4;
1803 /* Hack -- Stupid monsters will never fail (for jellies and such) */
1804 if (r_ptr->flags2 & RF2_STUPID) failrate = 0;
1806 /* Check for spell failure (inate attacks never fail) */
1807 if (!spell_is_inate(thrown_spell)
1808 && (in_no_magic_dungeon || (MON_STUNNED(m_ptr) && one_in_(2)) || (randint0(100) < failrate)))
1812 msg_format(_("%^sは呪文を唱えようとしたが失敗した。", "%^s tries to cast a spell, but fails."), m_name);
1817 /* Hex: Anti Magic Barrier */
1818 if (!spell_is_inate(thrown_spell) && magic_barrier(m_idx))
1820 msg_format(_("反魔法バリアが%^sの呪文をかき消した。", "Anti magic barrier cancels the spell which %^s casts."), m_name);
1825 direct = player_bold(y, x);
1827 can_remember = is_original_ap_and_seen(m_ptr);
1831 switch (thrown_spell)
1833 case 96 + 2: /* RF4_DISPEL */
1834 case 96 + 4: /* RF4_SHOOT */
1835 case 128 + 9: /* RF5_DRAIN_MANA */
1836 case 128 + 10: /* RF5_MIND_BLAST */
1837 case 128 + 11: /* RF5_BRAIN_SMASH */
1838 case 128 + 12: /* RF5_CAUSE_1 */
1839 case 128 + 13: /* RF5_CAUSE_2 */
1840 case 128 + 14: /* RF5_CAUSE_3 */
1841 case 128 + 15: /* RF5_CAUSE_4 */
1842 case 128 + 16: /* RF5_BO_ACID */
1843 case 128 + 17: /* RF5_BO_ELEC */
1844 case 128 + 18: /* RF5_BO_FIRE */
1845 case 128 + 19: /* RF5_BO_COLD */
1846 case 128 + 21: /* RF5_BO_NETH */
1847 case 128 + 22: /* RF5_BO_WATE */
1848 case 128 + 23: /* RF5_BO_MANA */
1849 case 128 + 24: /* RF5_BO_PLAS */
1850 case 128 + 25: /* RF5_BO_ICEE */
1851 case 128 + 26: /* RF5_MISSILE */
1852 case 128 + 27: /* RF5_SCARE */
1853 case 128 + 28: /* RF5_BLIND */
1854 case 128 + 29: /* RF5_CONF */
1855 case 128 + 30: /* RF5_SLOW */
1856 case 128 + 31: /* RF5_HOLD */
1857 case 160 + 1: /* RF6_HAND_DOOM */
1858 case 160 + 8: /* RF6_TELE_TO */
1859 case 160 + 9: /* RF6_TELE_AWAY */
1860 case 160 + 10: /* RF6_TELE_LEVEL */
1861 case 160 + 11: /* RF6_PSY_SPEAR */
1862 case 160 + 12: /* RF6_DARKNESS */
1863 case 160 + 14: /* RF6_FORGET */
1868 /* Cast the spell. */
1869 dam = monspell_to_player(thrown_spell, y, x, m_idx);
1870 if (dam < 0) return FALSE;
1872 if ((p_ptr->action == ACTION_LEARN) && thrown_spell > 175)
1874 learn_spell(thrown_spell - 96);
1877 if (seen && maneable && !world_monster && (p_ptr->pclass == CLASS_IMITATOR))
1879 if (thrown_spell != 167) /* Not RF6_SPECIAL */
1881 if (p_ptr->mane_num == MAX_MANE)
1885 for (i = 0;i < p_ptr->mane_num;i++)
1887 p_ptr->mane_spell[i] = p_ptr->mane_spell[i+1];
1888 p_ptr->mane_dam[i] = p_ptr->mane_dam[i+1];
1891 p_ptr->mane_spell[p_ptr->mane_num] = thrown_spell - 96;
1892 p_ptr->mane_dam[p_ptr->mane_num] = dam;
1896 p_ptr->redraw |= (PR_IMITATION);
1900 /* Remember what the monster did to us */
1904 if (thrown_spell < 32 * 4)
1906 r_ptr->r_flags4 |= (1L << (thrown_spell - 32 * 3));
1907 if (r_ptr->r_cast_spell < MAX_UCHAR) r_ptr->r_cast_spell++;
1911 else if (thrown_spell < 32 * 5)
1913 r_ptr->r_flags5 |= (1L << (thrown_spell - 32 * 4));
1914 if (r_ptr->r_cast_spell < MAX_UCHAR) r_ptr->r_cast_spell++;
1918 else if (thrown_spell < 32 * 6)
1920 r_ptr->r_flags6 |= (1L << (thrown_spell - 32 * 5));
1921 if (r_ptr->r_cast_spell < MAX_UCHAR) r_ptr->r_cast_spell++;
1926 /* Always take note of monsters that kill you */
1927 if (p_ptr->is_dead && (r_ptr->r_deaths < MAX_SHORT) && !p_ptr->inside_arena)
1929 r_ptr->r_deaths++; /* Ignore appearance difference */
1932 /* A spell was cast */