3 * @brief モンスター魔法の実装(対モンスター処理) / Monster spells (attack monster)
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
21 #include "realm-hex.h"
22 #include "player-move.h"
23 #include "player-class.h"
25 #include "monster-status.h"
26 #include "monster-spell.h"
30 #include "view-mainwindow.h"
33 * @brief モンスターが敵対モンスターにビームを当てること可能かを判定する /
34 * Determine if a beam spell will hit the target.
39 * @param m_ptr 使用するモンスターの構造体参照ポインタ
40 * @return ビームが到達可能ならばTRUEを返す
42 static bool direct_beam(POSITION y1, POSITION x1, POSITION y2, POSITION x2, monster_type *m_ptr)
51 bool is_friend = is_pet(m_ptr);
53 /* Check the projection path */
54 grid_n = project_path(grid_g, MAX_RANGE, y1, x1, y2, x2, PROJECT_THRU);
56 /* No grid is ever projectable from itself */
57 if (!grid_n) return (FALSE);
59 for (i = 0; i < grid_n; i++)
61 y = GRID_Y(grid_g[i]);
62 x = GRID_X(grid_g[i]);
64 if (y == y2 && x == x2)
66 else if (is_friend && current_floor_ptr->grid_array[y][x].m_idx > 0 &&
67 !are_enemies(m_ptr, ¤t_floor_ptr->m_list[current_floor_ptr->grid_array[y][x].m_idx]))
69 /* Friends don't shoot friends */
73 if (is_friend && player_bold(y, x))
82 * @brief モンスターが敵対モンスターに直接ブレスを当てることが可能かを判定する /
83 * Determine if a breath will hit the target.
90 * @param is_friend TRUEならば、プレイヤーを巻き込む時にブレスの判定をFALSEにする。
91 * @return ブレスを直接当てられるならばTRUEを返す
93 static bool breath_direct(POSITION y1, POSITION x1, POSITION y2, POSITION x2, POSITION rad, EFFECT_ID typ, bool is_friend)
95 /* Must be the same as projectable() */
107 POSITION gx[1024], gy[1024];
109 POSITION gm_rad = rad;
122 case GF_DISINTEGRATE:
130 /* Check the projection path */
131 grid_n = project_path(grid_g, MAX_RANGE, y1, x1, y2, x2, flg);
133 /* Project along the path */
134 for (i = 0; i < grid_n; ++i)
136 int ny = GRID_Y(grid_g[i]);
137 int nx = GRID_X(grid_g[i]);
139 if (flg & PROJECT_DISI)
141 /* Hack -- Balls explode before reaching walls */
142 if (cave_stop_disintegration(ny, nx)) break;
144 else if (flg & PROJECT_LOS)
146 /* Hack -- Balls explode before reaching walls */
147 if (!cave_los_bold(ny, nx)) break;
151 /* Hack -- Balls explode before reaching walls */
152 if (!cave_have_flag_bold(ny, nx, FF_PROJECT)) break;
155 /* Save the "blast epicenter" */
164 if (flg & PROJECT_DISI)
166 if (in_disintegration_range(y1, x1, y2, x2) && (distance(y1, x1, y2, x2) <= rad)) hit2 = TRUE;
167 if (in_disintegration_range(y1, x1, p_ptr->y, p_ptr->x) && (distance(y1, x1, p_ptr->y, p_ptr->x) <= rad)) hityou = TRUE;
169 else if (flg & PROJECT_LOS)
171 if (los(y1, x1, y2, x2) && (distance(y1, x1, y2, x2) <= rad)) hit2 = TRUE;
172 if (los(y1, x1, p_ptr->y, p_ptr->x) && (distance(y1, x1, p_ptr->y, p_ptr->x) <= rad)) hityou = TRUE;
176 if (projectable(y1, x1, y2, x2) && (distance(y1, x1, y2, x2) <= rad)) hit2 = TRUE;
177 if (projectable(y1, x1, p_ptr->y, p_ptr->x) && (distance(y1, x1, p_ptr->y, p_ptr->x) <= rad)) hityou = TRUE;
182 breath_shape(grid_g, grid_n, &grids, gx, gy, gm, &gm_rad, rad, y1, x1, y, x, typ);
184 for (i = 0; i < grids; i++)
188 if ((y == y2) && (x == x2)) hit2 = TRUE;
189 if (player_bold(y, x)) hityou = TRUE;
193 if (!hit2) return FALSE;
194 if (is_friend && hityou) return FALSE;
200 * @brief モンスターが特殊能力の目標地点を決める処理 /
201 * Get the actual center point of ball spells (rad > 1) (originally from TOband)
204 * @param ty 目標Y座標を返す参照ポインタ
205 * @param tx 目標X座標を返す参照ポインタ
206 * @param flg 判定のフラグ配列
209 void get_project_point(POSITION sy, POSITION sx, POSITION *ty, POSITION *tx, BIT_FLAGS flg)
214 path_n = project_path(path_g, MAX_RANGE, sy, sx, *ty, *tx, flg);
219 /* Project along the path */
220 for (i = 0; i < path_n; i++)
222 sy = GRID_Y(path_g[i]);
223 sx = GRID_X(path_g[i]);
225 /* Hack -- Balls explode before reaching walls */
226 if (!cave_have_flag_bold(sy, sx, FF_PROJECT)) break;
234 * @brief モンスターが敵モンスターに魔力消去を使うかどうかを返す /
235 * Check should monster cast dispel spell at other monster.
236 * @param m_idx 術者のモンスターID
237 * @param t_idx 目標のモンスターID
238 * @return 魔力消去を使うべきならばTRUEを変えす。
240 static bool dispel_check_monster(MONSTER_IDX m_idx, MONSTER_IDX t_idx)
242 monster_type *t_ptr = ¤t_floor_ptr->m_list[t_idx];
244 if (MON_INVULNER(t_ptr)) return TRUE;
246 if (t_ptr->mspeed < 135)
248 if (MON_FAST(t_ptr)) return TRUE;
252 if (t_idx == p_ptr->riding)
254 if (dispel_check(m_idx)) return TRUE;
257 /* No need to cast dispel spell */
262 * @brief モンスターが敵モンスターに特殊能力を使う処理のメインルーチン /
263 * Monster tries to 'cast a spell' (or breath, etc) at another monster.
264 * @param m_idx 術者のモンスターID
265 * @return 実際に特殊能力を使った場合TRUEを返す
267 * The player is only disturbed if able to be affected by the spell.
269 bool monst_spell_monst(MONSTER_IDX m_idx)
271 POSITION y = 0, x = 0;
273 MONSTER_IDX target_idx = 0;
279 byte spell[96], num = 0;
281 GAME_TEXT m_name[160];
282 GAME_TEXT t_name[160];
288 monster_type *m_ptr = ¤t_floor_ptr->m_list[m_idx];
289 monster_type *t_ptr = NULL;
291 monster_race *r_ptr = &r_info[m_ptr->r_idx];
295 bool see_m = is_seen(m_ptr);
296 bool maneable = player_has_los_bold(m_ptr->fy, m_ptr->fx);
297 bool pet = is_pet(m_ptr);
299 bool in_no_magic_dungeon = (d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_MAGIC) && current_floor_ptr->dun_level
300 && (!p_ptr->inside_quest || is_fixed_quest_idx(p_ptr->inside_quest));
302 bool can_use_lite_area = FALSE;
305 /* Cannot cast spells when confused */
306 if (MON_CONFUSED(m_ptr)) return (FALSE);
308 /* Extract the racial spell flags */
310 f5 = r_ptr->a_ability_flags1;
311 f6 = r_ptr->a_ability_flags2;
313 /* Target is given for pet? */
314 if (p_ptr->pet_t_m_idx && pet)
316 target_idx = p_ptr->pet_t_m_idx;
317 t_ptr = ¤t_floor_ptr->m_list[target_idx];
319 /* Cancel if not projectable (for now) */
320 if ((m_idx == target_idx) || !projectable(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx))
326 /* Is there counter attack target? */
327 if (!target_idx && m_ptr->target_y)
329 target_idx = current_floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
333 t_ptr = ¤t_floor_ptr->m_list[target_idx];
335 /* Cancel if neither enemy nor a given target */
336 if ((m_idx == target_idx) ||
337 ((target_idx != p_ptr->pet_t_m_idx) && !are_enemies(m_ptr, t_ptr)))
342 /* Allow only summoning etc.. if not projectable */
343 else if (!projectable(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx))
345 f4 &= (RF4_INDIRECT_MASK);
346 f5 &= (RF5_INDIRECT_MASK);
347 f6 &= (RF6_INDIRECT_MASK);
352 /* Look for enemies normally */
355 bool success = FALSE;
357 if (p_ptr->phase_out)
359 start = randint1(current_floor_ptr->m_max-1) + current_floor_ptr->m_max;
360 if (randint0(2)) plus = -1;
362 else start = current_floor_ptr->m_max + 1;
364 /* Scan thru all monsters */
365 for (i = start; ((i < start + current_floor_ptr->m_max) && (i > start - current_floor_ptr->m_max)); i += plus)
367 MONSTER_IDX dummy = (i % current_floor_ptr->m_max);
368 if (!dummy) continue;
371 t_ptr = ¤t_floor_ptr->m_list[target_idx];
373 if (!monster_is_valid(t_ptr)) continue;
375 /* Monster must be 'an enemy' */
376 if ((m_idx == target_idx) || !are_enemies(m_ptr, t_ptr)) continue;
378 /* Monster must be projectable */
379 if (!projectable(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
387 if (!success) return FALSE;
391 /* OK -- we've got a target */
395 /* Forget old counter attack target */
398 /* Remove unimplemented spells */
399 f6 &= ~(RF6_WORLD | RF6_TRAPS | RF6_FORGET);
401 if (f4 & RF4_BR_LITE)
403 if (!los(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx))
404 f4 &= ~(RF4_BR_LITE);
407 /* Remove unimplemented special moves */
408 if (f6 & RF6_SPECIAL)
410 if ((m_ptr->r_idx != MON_ROLENTO) && (r_ptr->d_char != 'B'))
411 f6 &= ~(RF6_SPECIAL);
414 if (f6 & RF6_DARKNESS)
416 bool vs_ninja = (p_ptr->pclass == CLASS_NINJA) && !is_hostile(t_ptr);
419 !(r_ptr->flags3 & (RF3_UNDEAD | RF3_HURT_LITE)) &&
420 !(r_ptr->flags7 & RF7_DARK_MASK))
421 can_use_lite_area = TRUE;
423 if (!(r_ptr->flags2 & RF2_STUPID))
425 if (d_info[p_ptr->dungeon_idx].flags1 & DF1_DARKNESS) f6 &= ~(RF6_DARKNESS);
426 else if (vs_ninja && !can_use_lite_area) f6 &= ~(RF6_DARKNESS);
430 if (in_no_magic_dungeon && !(r_ptr->flags2 & RF2_STUPID))
432 f4 &= (RF4_NOMAGIC_MASK);
433 f5 &= (RF5_NOMAGIC_MASK);
434 f6 &= (RF6_NOMAGIC_MASK);
437 if (p_ptr->inside_arena || p_ptr->phase_out)
439 f4 &= ~(RF4_SUMMON_MASK);
440 f5 &= ~(RF5_SUMMON_MASK);
441 f6 &= ~(RF6_SUMMON_MASK | RF6_TELE_LEVEL);
443 if (m_ptr->r_idx == MON_ROLENTO) f6 &= ~(RF6_SPECIAL);
446 if (p_ptr->phase_out && !one_in_(3))
451 if (m_idx == p_ptr->riding)
453 f4 &= ~(RF4_RIDING_MASK);
454 f5 &= ~(RF5_RIDING_MASK);
455 f6 &= ~(RF6_RIDING_MASK);
461 f6 &= ~(RF6_DARKNESS | RF6_TRAPS);
463 if (!(p_ptr->pet_extra_flags & PF_TELEPORT))
465 f6 &= ~(RF6_BLINK | RF6_TPORT | RF6_TELE_TO | RF6_TELE_AWAY | RF6_TELE_LEVEL);
468 if (!(p_ptr->pet_extra_flags & PF_ATTACK_SPELL))
470 f4 &= ~(RF4_ATTACK_MASK);
471 f5 &= ~(RF5_ATTACK_MASK);
472 f6 &= ~(RF6_ATTACK_MASK);
475 if (!(p_ptr->pet_extra_flags & PF_SUMMON_SPELL))
477 f4 &= ~(RF4_SUMMON_MASK);
478 f5 &= ~(RF5_SUMMON_MASK);
479 f6 &= ~(RF6_SUMMON_MASK);
482 /* Prevent collateral damage */
483 if (!(p_ptr->pet_extra_flags & PF_BALL_SPELL) && (m_idx != p_ptr->riding))
485 if ((f4 & (RF4_BALL_MASK & ~(RF4_ROCKET))) ||
486 (f5 & RF5_BALL_MASK) ||
487 (f6 & RF6_BALL_MASK))
492 get_project_point(m_ptr->fy, m_ptr->fx, &real_y, &real_x, 0L);
494 if (projectable(real_y, real_x, p_ptr->y, p_ptr->x))
496 int dist = distance(real_y, real_x, p_ptr->y, p_ptr->x);
500 f4 &= ~(RF4_BALL_MASK & ~(RF4_ROCKET));
501 f5 &= ~(RF5_BALL_MASK);
502 f6 &= ~(RF6_BALL_MASK);
506 f4 &= ~(RF4_BIG_BALL_MASK);
507 f5 &= ~(RF5_BIG_BALL_MASK);
508 f6 &= ~(RF6_BIG_BALL_MASK);
511 else if (f5 & RF5_BA_LITE)
513 if ((distance(real_y, real_x, p_ptr->y, p_ptr->x) <= 4) && los(real_y, real_x, p_ptr->y, p_ptr->x))
514 f5 &= ~(RF5_BA_LITE);
523 get_project_point(m_ptr->fy, m_ptr->fx, &real_y, &real_x, PROJECT_STOP);
524 if (projectable(real_y, real_x, p_ptr->y, p_ptr->x) && (distance(real_y, real_x, p_ptr->y, p_ptr->x) <= 2))
528 if (((f4 & RF4_BEAM_MASK) || (f5 & RF5_BEAM_MASK) || (f6 & RF6_BEAM_MASK)) &&
529 !direct_beam(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx, m_ptr))
531 f4 &= ~(RF4_BEAM_MASK);
532 f5 &= ~(RF5_BEAM_MASK);
533 f6 &= ~(RF6_BEAM_MASK);
536 if ((f4 & RF4_BREATH_MASK) || (f5 & RF5_BREATH_MASK) || (f6 & RF6_BREATH_MASK))
538 /* Expected breath radius */
539 POSITION rad = (r_ptr->flags2 & RF2_POWERFUL) ? 3 : 2;
541 if (!breath_direct(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx, rad, 0, TRUE))
543 f4 &= ~(RF4_BREATH_MASK);
544 f5 &= ~(RF5_BREATH_MASK);
545 f6 &= ~(RF6_BREATH_MASK);
547 else if ((f4 & RF4_BR_LITE) &&
548 !breath_direct(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx, rad, GF_LITE, TRUE))
550 f4 &= ~(RF4_BR_LITE);
552 else if ((f4 & RF4_BR_DISI) &&
553 !breath_direct(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx, rad, GF_DISINTEGRATE, TRUE))
555 f4 &= ~(RF4_BR_DISI);
560 /* Special moves restriction */
561 if (f6 & RF6_SPECIAL)
563 if (m_ptr->r_idx == MON_ROLENTO)
565 if ((p_ptr->pet_extra_flags & (PF_ATTACK_SPELL | PF_SUMMON_SPELL)) != (PF_ATTACK_SPELL | PF_SUMMON_SPELL))
566 f6 &= ~(RF6_SPECIAL);
568 else if (r_ptr->d_char == 'B')
570 if ((p_ptr->pet_extra_flags & (PF_ATTACK_SPELL | PF_TELEPORT)) != (PF_ATTACK_SPELL | PF_TELEPORT))
571 f6 &= ~(RF6_SPECIAL);
573 else f6 &= ~(RF6_SPECIAL);
577 /* Remove some spells if necessary */
579 if (!(r_ptr->flags2 & RF2_STUPID))
581 /* Check for a clean bolt shot */
582 if (((f4 & RF4_BOLT_MASK) ||
583 (f5 & RF5_BOLT_MASK) ||
584 (f6 & RF6_BOLT_MASK)) &&
585 !clean_shot(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx, pet))
587 f4 &= ~(RF4_BOLT_MASK);
588 f5 &= ~(RF5_BOLT_MASK);
589 f6 &= ~(RF6_BOLT_MASK);
592 /* Check for a possible summon */
593 if (((f4 & RF4_SUMMON_MASK) ||
594 (f5 & RF5_SUMMON_MASK) ||
595 (f6 & RF6_SUMMON_MASK)) &&
596 !(summon_possible(t_ptr->fy, t_ptr->fx)))
598 /* Remove summoning spells */
599 f4 &= ~(RF4_SUMMON_MASK);
600 f5 &= ~(RF5_SUMMON_MASK);
601 f6 &= ~(RF6_SUMMON_MASK);
605 if ((f4 & RF4_DISPEL) && !dispel_check_monster(m_idx, target_idx))
607 /* Remove dispel spell */
611 /* Check for a possible raise dead */
612 if ((f6 & RF6_RAISE_DEAD) && !raise_possible(m_ptr))
614 /* Remove raise dead spell */
615 f6 &= ~(RF6_RAISE_DEAD);
618 /* Special moves restriction */
619 if (f6 & RF6_SPECIAL)
621 if ((m_ptr->r_idx == MON_ROLENTO) && !summon_possible(t_ptr->fy, t_ptr->fx))
623 f6 &= ~(RF6_SPECIAL);
628 if (r_ptr->flags2 & RF2_SMART)
630 /* Hack -- allow "desperate" spells */
631 if ((m_ptr->hp < m_ptr->maxhp / 10) &&
632 (randint0(100) < 50))
634 /* Require intelligent spells */
635 f4 &= (RF4_INT_MASK);
636 f5 &= (RF5_INT_MASK);
637 f6 &= (RF6_INT_MASK);
640 /* Hack -- decline "teleport level" in some case */
641 if ((f6 & RF6_TELE_LEVEL) && TELE_LEVEL_IS_INEFF((target_idx == p_ptr->riding) ? 0 : target_idx))
643 f6 &= ~(RF6_TELE_LEVEL);
648 if (!f4 && !f5 && !f6) return FALSE;
650 /* Extract the "inate" spells */
651 for (k = 0; k < 32; k++)
653 if (f4 & (1L << k)) spell[num++] = k + RF4_SPELL_START;
656 /* Extract the "normal" spells */
657 for (k = 0; k < 32; k++)
659 if (f5 & (1L << k)) spell[num++] = k + RF5_SPELL_START;
662 /* Extract the "bizarre" spells */
663 for (k = 0; k < 32; k++)
665 if (f6 & (1L << k)) spell[num++] = k + RF6_SPELL_START;
669 if (!num) return (FALSE);
671 /* Stop if player is dead or gone */
672 if (!p_ptr->playing || p_ptr->is_dead) return (FALSE);
674 /* Handle "leaving" */
675 if (p_ptr->leaving) return (FALSE);
677 /* Get the monster name (or "it") */
678 monster_desc(m_name, m_ptr, 0x00);
681 /* Get the monster possessive ("his"/"her"/"its") */
682 monster_desc(m_poss, m_ptr, MD_PRON_VISIBLE | MD_POSSESSIVE);
685 /* Get the target's name (or "it") */
686 monster_desc(t_name, t_ptr, 0x00);
688 /* Choose a spell to cast */
689 thrown_spell = spell[randint0(num)];
691 if (p_ptr->riding && (m_idx == p_ptr->riding)) disturb(TRUE, TRUE);
693 /* Check for spell failure (inate attacks never fail) */
694 if (!spell_is_inate(thrown_spell) && (in_no_magic_dungeon || (MON_STUNNED(m_ptr) && one_in_(2))))
697 if (see_m) msg_format(_("%^sは呪文を唱えようとしたが失敗した。",
698 "%^s tries to cast a spell, but fails."), m_name);
703 /* Hex: Anti Magic Barrier */
704 if (!spell_is_inate(thrown_spell) && magic_barrier(m_idx))
706 if (see_m) msg_format(_("反魔法バリアが%^sの呪文をかき消した。",
707 "Anti magic barrier cancels the spell which %^s casts."), m_name);
711 can_remember = is_original_ap_and_seen(m_ptr);
713 dam = monspell_to_monster(thrown_spell, y, x, m_idx, target_idx);
714 if (dam < 0) return FALSE;
716 if (m_ptr->ml && maneable && !current_world_ptr->timewalk_m_idx && !p_ptr->blind && (p_ptr->pclass == CLASS_IMITATOR))
718 if (thrown_spell != 167) /* Not RF6_SPECIAL */
720 if (p_ptr->mane_num == MAX_MANE)
723 for (i = 0; i < p_ptr->mane_num - 1; i++)
725 p_ptr->mane_spell[i] = p_ptr->mane_spell[i+1];
726 p_ptr->mane_dam[i] = p_ptr->mane_dam[i+1];
729 p_ptr->mane_spell[p_ptr->mane_num] = thrown_spell - RF4_SPELL_START;
730 p_ptr->mane_dam[p_ptr->mane_num] = dam;
732 p_ptr->new_mane = TRUE;
734 p_ptr->redraw |= (PR_IMITATION);
738 /* Remember what the monster did, if we saw it */
742 if (thrown_spell < RF4_SPELL_START + RF4_SPELL_SIZE)
744 r_ptr->r_flags4 |= (1L << (thrown_spell - RF4_SPELL_START));
745 if (r_ptr->r_cast_spell < MAX_UCHAR) r_ptr->r_cast_spell++;
749 else if (thrown_spell < RF5_SPELL_START + RF5_SPELL_SIZE)
751 r_ptr->r_flags5 |= (1L << (thrown_spell - RF5_SPELL_START));
752 if (r_ptr->r_cast_spell < MAX_UCHAR) r_ptr->r_cast_spell++;
756 else if (thrown_spell < RF6_SPELL_START + RF6_SPELL_SIZE)
758 r_ptr->r_flags6 |= (1L << (thrown_spell - RF6_SPELL_START));
759 if (r_ptr->r_cast_spell < MAX_UCHAR) r_ptr->r_cast_spell++;
763 /* Always take note of monsters that kill you */
764 if (p_ptr->is_dead && (r_ptr->r_deaths < MAX_SHORT) && !p_ptr->inside_arena)
766 r_ptr->r_deaths++; /* Ignore appearance difference */
769 /* A spell was cast */