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
17 * @brief モンスターが敵対モンスターにビームを当てること可能かを判定する /
18 * Determine if a beam spell will hit the target.
23 * @param m_ptr 使用するモンスターの構造体参照ポインタ
24 * @return ビームが到達可能ならばTRUEを返す
26 static bool direct_beam(int y1, int x1, int y2, int x2, monster_type *m_ptr)
34 bool is_friend = is_pet(m_ptr);
36 /* Check the projection path */
37 grid_n = project_path(grid_g, MAX_RANGE, y1, x1, y2, x2, PROJECT_THRU);
39 /* No grid is ever projectable from itself */
40 if (!grid_n) return (FALSE);
42 for (i = 0; i < grid_n; i++)
44 y = GRID_Y(grid_g[i]);
45 x = GRID_X(grid_g[i]);
47 if (y == y2 && x == x2)
49 else if (is_friend && cave[y][x].m_idx > 0 &&
50 !are_enemies(m_ptr, &m_list[cave[y][x].m_idx]))
52 /* Friends don't shoot friends */
56 if (is_friend && player_bold(y, x))
65 * @brief モンスターが敵対モンスターに直接ブレスを当てることが可能かを判定する /
66 * Determine if a breath will hit the target.
73 * @param is_friend TRUEならば、プレイヤーを巻き込む時にブレスの判定をFALSEにする。
74 * @return ブレスを直接当てられるならばTRUEを返す
76 static bool breath_direct(int y1, int x1, int y2, int x2, int rad, int typ, bool is_friend)
78 /* Must be the same as projectable() */
90 byte gx[1024], gy[1024];
105 case GF_DISINTEGRATE:
113 /* Check the projection path */
114 grid_n = project_path(grid_g, MAX_RANGE, y1, x1, y2, x2, flg);
116 /* Project along the path */
117 for (i = 0; i < grid_n; ++i)
119 int ny = GRID_Y(grid_g[i]);
120 int nx = GRID_X(grid_g[i]);
122 if (flg & PROJECT_DISI)
124 /* Hack -- Balls explode before reaching walls */
125 if (cave_stop_disintegration(ny, nx)) break;
127 else if (flg & PROJECT_LOS)
129 /* Hack -- Balls explode before reaching walls */
130 if (!cave_los_bold(ny, nx)) break;
134 /* Hack -- Balls explode before reaching walls */
135 if (!cave_have_flag_bold(ny, nx, FF_PROJECT)) break;
138 /* Save the "blast epicenter" */
147 if (flg & PROJECT_DISI)
149 if (in_disintegration_range(y1, x1, y2, x2) && (distance(y1, x1, y2, x2) <= rad)) hit2 = TRUE;
150 if (in_disintegration_range(y1, x1, py, px) && (distance(y1, x1, py, px) <= rad)) hityou = TRUE;
152 else if (flg & PROJECT_LOS)
154 if (los(y1, x1, y2, x2) && (distance(y1, x1, y2, x2) <= rad)) hit2 = TRUE;
155 if (los(y1, x1, py, px) && (distance(y1, x1, py, px) <= rad)) hityou = TRUE;
159 if (projectable(y1, x1, y2, x2) && (distance(y1, x1, y2, x2) <= rad)) hit2 = TRUE;
160 if (projectable(y1, x1, py, px) && (distance(y1, x1, py, px) <= rad)) hityou = TRUE;
165 breath_shape(grid_g, grid_n, &grids, gx, gy, gm, &gm_rad, rad, y1, x1, y, x, typ);
167 for (i = 0; i < grids; i++)
169 /* Extract the location */
173 if ((y == y2) && (x == x2)) hit2 = TRUE;
174 if (player_bold(y, x)) hityou = TRUE;
178 if (!hit2) return FALSE;
179 if (is_friend && hityou) return FALSE;
185 * @brief モンスターが特殊能力の目標地点を決める処理 /
186 * Get the actual center point of ball spells (rad > 1) (originally from TOband)
189 * @param ty 目標Y座標を返す参照ポインタ
190 * @param tx 目標X座標を返す参照ポインタ
191 * @param flg 判定のフラグ配列
194 void get_project_point(int sy, int sx, int *ty, int *tx, int flg)
199 path_n = project_path(path_g, MAX_RANGE, sy, sx, *ty, *tx, flg);
204 /* Project along the path */
205 for (i = 0; i < path_n; i++)
207 sy = GRID_Y(path_g[i]);
208 sx = GRID_X(path_g[i]);
210 /* Hack -- Balls explode before reaching walls */
211 if (!cave_have_flag_bold(sy, sx, FF_PROJECT)) break;
219 * @brief モンスターが敵モンスターに魔力消去を使うかどうかを返す /
220 * Check should monster cast dispel spell at other monster.
221 * @param m_idx 術者のモンスターID
222 * @param t_idx 目標のモンスターID
223 * @return 魔力消去を使うべきならばTRUEを変えす。
225 static bool dispel_check_monster(int m_idx, int t_idx)
227 monster_type *t_ptr = &m_list[t_idx];
230 if (MON_INVULNER(t_ptr)) return TRUE;
233 if (t_ptr->mspeed < 135)
235 if (MON_FAST(t_ptr)) return TRUE;
239 if (t_idx == p_ptr->riding)
241 if (dispel_check(m_idx)) return TRUE;
244 /* No need to cast dispel spell */
249 * @brief モンスターが敵モンスターに特殊能力を使う処理のメインルーチン /
250 * Monster tries to 'cast a spell' (or breath, etc) at another monster.
251 * @param m_idx 術者のモンスターID
252 * @return 実際に特殊能力を使った場合TRUEを返す
254 * The player is only disturbed if able to be affected by the spell.
256 bool monst_spell_monst(int m_idx)
260 int thrown_spell, count = 0;
264 int rad = 0; //For elemental balls
266 byte spell[96], num = 0;
275 monster_type *m_ptr = &m_list[m_idx];
276 monster_type *t_ptr = NULL;
278 monster_race *r_ptr = &r_info[m_ptr->r_idx];
279 monster_race *tr_ptr = NULL;
283 bool see_m = is_seen(m_ptr);
284 bool maneable = player_has_los_bold(m_ptr->fy, m_ptr->fx);
287 bool pet = is_pet(m_ptr);
289 bool in_no_magic_dungeon = (d_info[dungeon_type].flags1 & DF1_NO_MAGIC) && dun_level
290 && (!p_ptr->inside_quest || is_fixed_quest_idx(p_ptr->inside_quest));
292 bool can_use_lite_area = FALSE;
296 bool resists_tele = FALSE;
298 /* Cannot cast spells when confused */
299 if (MON_CONFUSED(m_ptr)) return (FALSE);
301 /* Extract the racial spell flags */
306 /* Target is given for pet? */
307 if (pet_t_m_idx && pet)
310 t_ptr = &m_list[t_idx];
312 /* Cancel if not projectable (for now) */
313 if ((m_idx == t_idx) || !projectable(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx))
319 /* Is there counter attack target? */
320 if (!t_idx && m_ptr->target_y)
322 t_idx = cave[m_ptr->target_y][m_ptr->target_x].m_idx;
326 t_ptr = &m_list[t_idx];
328 /* Cancel if neither enemy nor a given target */
329 if ((m_idx == t_idx) ||
330 ((t_idx != pet_t_m_idx) && !are_enemies(m_ptr, t_ptr)))
335 /* Allow only summoning etc.. if not projectable */
336 else if (!projectable(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx))
338 f4 &= (RF4_INDIRECT_MASK);
339 f5 &= (RF5_INDIRECT_MASK);
340 f6 &= (RF6_INDIRECT_MASK);
345 /* Look for enemies normally */
348 bool success = FALSE;
350 if (p_ptr->inside_battle)
352 start = randint1(m_max-1) + m_max;
353 if (randint0(2)) plus = -1;
355 else start = m_max + 1;
357 /* Scan thru all monsters */
358 for (i = start; ((i < start + m_max) && (i > start - m_max)); i += plus)
360 int dummy = (i % m_max);
361 if (!dummy) continue;
364 t_ptr = &m_list[t_idx];
366 /* Skip dead monsters */
367 if (!t_ptr->r_idx) continue;
369 /* Monster must be 'an enemy' */
370 if ((m_idx == t_idx) || !are_enemies(m_ptr, t_ptr)) continue;
372 /* Monster must be projectable */
373 if (!projectable(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
381 if (!success) return FALSE;
385 /* OK -- we've got a target */
388 tr_ptr = &r_info[t_ptr->r_idx];
390 /* Forget old counter attack target */
393 /* Remove unimplemented spells */
394 f6 &= ~(RF6_WORLD | RF6_TRAPS | RF6_FORGET);
396 if (f4 & RF4_BR_LITE)
398 if (!los(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx))
399 f4 &= ~(RF4_BR_LITE);
402 /* Remove unimplemented special moves */
403 if (f6 & RF6_SPECIAL)
405 if ((m_ptr->r_idx != MON_ROLENTO) && (r_ptr->d_char != 'B'))
406 f6 &= ~(RF6_SPECIAL);
409 if (f6 & RF6_DARKNESS)
411 bool vs_ninja = (p_ptr->pclass == CLASS_NINJA) && !is_hostile(t_ptr);
414 !(r_ptr->flags3 & (RF3_UNDEAD | RF3_HURT_LITE)) &&
415 !(r_ptr->flags7 & RF7_DARK_MASK))
416 can_use_lite_area = TRUE;
418 if (!(r_ptr->flags2 & RF2_STUPID))
420 if (d_info[dungeon_type].flags1 & DF1_DARKNESS) f6 &= ~(RF6_DARKNESS);
421 else if (vs_ninja && !can_use_lite_area) f6 &= ~(RF6_DARKNESS);
425 if (in_no_magic_dungeon && !(r_ptr->flags2 & RF2_STUPID))
427 f4 &= (RF4_NOMAGIC_MASK);
428 f5 &= (RF5_NOMAGIC_MASK);
429 f6 &= (RF6_NOMAGIC_MASK);
432 if (p_ptr->inside_arena || p_ptr->inside_battle)
434 f4 &= ~(RF4_SUMMON_MASK);
435 f5 &= ~(RF5_SUMMON_MASK);
436 f6 &= ~(RF6_SUMMON_MASK | RF6_TELE_LEVEL);
438 if (m_ptr->r_idx == MON_ROLENTO) f6 &= ~(RF6_SPECIAL);
441 if (p_ptr->inside_battle && !one_in_(3))
446 if (m_idx == p_ptr->riding)
448 f4 &= ~(RF4_RIDING_MASK);
449 f5 &= ~(RF5_RIDING_MASK);
450 f6 &= ~(RF6_RIDING_MASK);
456 f6 &= ~(RF6_DARKNESS | RF6_TRAPS);
458 if (!(p_ptr->pet_extra_flags & PF_TELEPORT))
460 f6 &= ~(RF6_BLINK | RF6_TPORT | RF6_TELE_TO | RF6_TELE_AWAY | RF6_TELE_LEVEL);
463 if (!(p_ptr->pet_extra_flags & PF_ATTACK_SPELL))
465 f4 &= ~(RF4_ATTACK_MASK);
466 f5 &= ~(RF5_ATTACK_MASK);
467 f6 &= ~(RF6_ATTACK_MASK);
470 if (!(p_ptr->pet_extra_flags & PF_SUMMON_SPELL))
472 f4 &= ~(RF4_SUMMON_MASK);
473 f5 &= ~(RF5_SUMMON_MASK);
474 f6 &= ~(RF6_SUMMON_MASK);
477 /* Prevent collateral damage */
478 if (!(p_ptr->pet_extra_flags & PF_BALL_SPELL) && (m_idx != p_ptr->riding))
480 if ((f4 & (RF4_BALL_MASK & ~(RF4_ROCKET))) ||
481 (f5 & RF5_BALL_MASK) ||
482 (f6 & RF6_BALL_MASK))
487 get_project_point(m_ptr->fy, m_ptr->fx, &real_y, &real_x, 0L);
489 if (projectable(real_y, real_x, py, px))
491 int dist = distance(real_y, real_x, py, px);
495 f4 &= ~(RF4_BALL_MASK & ~(RF4_ROCKET));
496 f5 &= ~(RF5_BALL_MASK);
497 f6 &= ~(RF6_BALL_MASK);
501 f4 &= ~(RF4_BIG_BALL_MASK);
502 f5 &= ~(RF5_BIG_BALL_MASK);
503 f6 &= ~(RF6_BIG_BALL_MASK);
506 else if (f5 & RF5_BA_LITE)
508 if ((distance(real_y, real_x, py, px) <= 4) && los(real_y, real_x, py, px))
509 f5 &= ~(RF5_BA_LITE);
518 get_project_point(m_ptr->fy, m_ptr->fx, &real_y, &real_x, PROJECT_STOP);
519 if (projectable(real_y, real_x, py, px) && (distance(real_y, real_x, py, px) <= 2))
523 if (((f4 & RF4_BEAM_MASK) ||
524 (f5 & RF5_BEAM_MASK) ||
525 (f6 & RF6_BEAM_MASK)) &&
526 !direct_beam(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx, m_ptr))
528 f4 &= ~(RF4_BEAM_MASK);
529 f5 &= ~(RF5_BEAM_MASK);
530 f6 &= ~(RF6_BEAM_MASK);
533 if ((f4 & RF4_BREATH_MASK) ||
534 (f5 & RF5_BREATH_MASK) ||
535 (f6 & RF6_BREATH_MASK))
537 /* Expected breath radius */
538 int rad = (r_ptr->flags2 & RF2_POWERFUL) ? 3 : 2;
540 if (!breath_direct(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx, rad, 0, TRUE))
542 f4 &= ~(RF4_BREATH_MASK);
543 f5 &= ~(RF5_BREATH_MASK);
544 f6 &= ~(RF6_BREATH_MASK);
546 else if ((f4 & RF4_BR_LITE) &&
547 !breath_direct(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx, rad, GF_LITE, TRUE))
549 f4 &= ~(RF4_BR_LITE);
551 else if ((f4 & RF4_BR_DISI) &&
552 !breath_direct(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx, rad, GF_DISINTEGRATE, TRUE))
554 f4 &= ~(RF4_BR_DISI);
559 /* Special moves restriction */
560 if (f6 & RF6_SPECIAL)
562 if (m_ptr->r_idx == MON_ROLENTO)
564 if ((p_ptr->pet_extra_flags & (PF_ATTACK_SPELL | PF_SUMMON_SPELL)) != (PF_ATTACK_SPELL | PF_SUMMON_SPELL))
565 f6 &= ~(RF6_SPECIAL);
567 else if (r_ptr->d_char == 'B')
569 if ((p_ptr->pet_extra_flags & (PF_ATTACK_SPELL | PF_TELEPORT)) != (PF_ATTACK_SPELL | PF_TELEPORT))
570 f6 &= ~(RF6_SPECIAL);
572 else f6 &= ~(RF6_SPECIAL);
576 /* Remove some spells if necessary */
578 if (!(r_ptr->flags2 & RF2_STUPID))
580 /* Check for a clean bolt shot */
581 if (((f4 & RF4_BOLT_MASK) ||
582 (f5 & RF5_BOLT_MASK) ||
583 (f6 & RF6_BOLT_MASK)) &&
584 !clean_shot(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx, pet))
586 f4 &= ~(RF4_BOLT_MASK);
587 f5 &= ~(RF5_BOLT_MASK);
588 f6 &= ~(RF6_BOLT_MASK);
591 /* Check for a possible summon */
592 if (((f4 & RF4_SUMMON_MASK) ||
593 (f5 & RF5_SUMMON_MASK) ||
594 (f6 & RF6_SUMMON_MASK)) &&
595 !(summon_possible(t_ptr->fy, t_ptr->fx)))
597 /* Remove summoning spells */
598 f4 &= ~(RF4_SUMMON_MASK);
599 f5 &= ~(RF5_SUMMON_MASK);
600 f6 &= ~(RF6_SUMMON_MASK);
604 if ((f4 & RF4_DISPEL) && !dispel_check_monster(m_idx, t_idx))
606 /* Remove dispel spell */
610 /* Check for a possible raise dead */
611 if ((f6 & RF6_RAISE_DEAD) && !raise_possible(m_ptr))
613 /* Remove raise dead spell */
614 f6 &= ~(RF6_RAISE_DEAD);
617 /* Special moves restriction */
618 if (f6 & RF6_SPECIAL)
620 if ((m_ptr->r_idx == MON_ROLENTO) && !summon_possible(t_ptr->fy, t_ptr->fx))
622 f6 &= ~(RF6_SPECIAL);
627 if (r_ptr->flags2 & RF2_SMART)
629 /* Hack -- allow "desperate" spells */
630 if ((m_ptr->hp < m_ptr->maxhp / 10) &&
631 (randint0(100) < 50))
633 /* Require intelligent spells */
634 f4 &= (RF4_INT_MASK);
635 f5 &= (RF5_INT_MASK);
636 f6 &= (RF6_INT_MASK);
639 /* Hack -- decline "teleport level" in some case */
640 if ((f6 & RF6_TELE_LEVEL) && TELE_LEVEL_IS_INEFF((t_idx == p_ptr->riding) ? 0 : t_idx))
642 f6 &= ~(RF6_TELE_LEVEL);
647 if (!f4 && !f5 && !f6) return FALSE;
649 /* Extract the "inate" spells */
650 for (k = 0; k < 32; k++)
652 if (f4 & (1L << k)) spell[num++] = k + RF4_SPELL_START;
655 /* Extract the "normal" spells */
656 for (k = 0; k < 32; k++)
658 if (f5 & (1L << k)) spell[num++] = k + RF5_SPELL_START;
661 /* Extract the "bizarre" spells */
662 for (k = 0; k < 32; k++)
664 if (f6 & (1L << k)) spell[num++] = k + RF6_SPELL_START;
668 if (!num) return (FALSE);
670 /* Stop if player is dead or gone */
671 if (!p_ptr->playing || p_ptr->is_dead) return (FALSE);
673 /* Handle "leaving" */
674 if (p_ptr->leaving) return (FALSE);
676 /* Get the monster name (or "it") */
677 monster_desc(m_name, m_ptr, 0x00);
680 /* Get the monster possessive ("his"/"her"/"its") */
681 monster_desc(m_poss, m_ptr, MD_PRON_VISIBLE | MD_POSSESSIVE);
684 /* Get the target's name (or "it") */
685 monster_desc(t_name, t_ptr, 0x00);
687 /* Choose a spell to cast */
688 thrown_spell = spell[randint0(num)];
690 see_t = is_seen(t_ptr);
691 see_either = (see_m || see_t);
693 if (p_ptr->riding && (m_idx == p_ptr->riding)) disturb(1, 1);
695 /* Check for spell failure (inate attacks never fail) */
696 if (!spell_is_inate(thrown_spell) && (in_no_magic_dungeon || (MON_STUNNED(m_ptr) && one_in_(2))))
700 if (see_m) msg_format(_("%^sは呪文を唱えようとしたが失敗した。",
701 "%^s tries to cast a spell, but fails."), m_name);
706 /* Hex: Anti Magic Barrier */
707 if (!spell_is_inate(thrown_spell) && magic_barrier(m_idx))
709 if (see_m) msg_format(_("反魔法バリアが%^sの呪文をかき消した。",
710 "Anti magic barrier cancels the spell which %^s casts."), m_name);
714 can_remember = is_original_ap_and_seen(m_ptr);
716 dam = monspell_to_monster(thrown_spell, y, x, m_idx, t_idx);
717 if (dam < 0)return FALSE;
719 if (m_ptr->ml && maneable && !world_monster && !p_ptr->blind && (p_ptr->pclass == CLASS_IMITATOR))
721 if (thrown_spell != 167) /* Not RF6_SPECIAL */
723 if (p_ptr->mane_num == MAX_MANE)
726 for (i = 0; i < p_ptr->mane_num - 1; i++)
728 p_ptr->mane_spell[i] = p_ptr->mane_spell[i+1];
729 p_ptr->mane_dam[i] = p_ptr->mane_dam[i+1];
732 p_ptr->mane_spell[p_ptr->mane_num] = thrown_spell - RF4_SPELL_START;
733 p_ptr->mane_dam[p_ptr->mane_num] = dam;
737 p_ptr->redraw |= (PR_IMITATION);
741 /* Remember what the monster did, if we saw it */
745 if (thrown_spell < RF4_SPELL_START + RF4_SPELL_SIZE)
747 r_ptr->r_flags4 |= (1L << (thrown_spell - RF4_SPELL_START));
748 if (r_ptr->r_cast_spell < MAX_UCHAR) r_ptr->r_cast_spell++;
752 else if (thrown_spell < RF5_SPELL_START + RF5_SPELL_SIZE)
754 r_ptr->r_flags5 |= (1L << (thrown_spell - RF5_SPELL_START));
755 if (r_ptr->r_cast_spell < MAX_UCHAR) r_ptr->r_cast_spell++;
759 else if (thrown_spell < RF6_SPELL_START + RF6_SPELL_SIZE)
761 r_ptr->r_flags6 |= (1L << (thrown_spell - RF6_SPELL_START));
762 if (r_ptr->r_cast_spell < MAX_UCHAR) r_ptr->r_cast_spell++;
766 /* Always take note of monsters that kill you */
767 if (p_ptr->is_dead && (r_ptr->r_deaths < MAX_SHORT) && !p_ptr->inside_arena)
769 r_ptr->r_deaths++; /* Ignore appearance difference */
772 /* A spell was cast */