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
15 #include "projection.h"
17 #include "realm-hex.h"
20 * @brief モンスターが敵対モンスターにビームを当てること可能かを判定する /
21 * Determine if a beam spell will hit the target.
26 * @param m_ptr 使用するモンスターの構造体参照ポインタ
27 * @return ビームが到達可能ならばTRUEを返す
29 static bool direct_beam(int y1, int x1, int y2, int x2, monster_type *m_ptr)
37 bool is_friend = is_pet(m_ptr);
39 /* Check the projection path */
40 grid_n = project_path(grid_g, MAX_RANGE, y1, x1, y2, x2, PROJECT_THRU);
42 /* No grid is ever projectable from itself */
43 if (!grid_n) return (FALSE);
45 for (i = 0; i < grid_n; i++)
47 y = GRID_Y(grid_g[i]);
48 x = GRID_X(grid_g[i]);
50 if (y == y2 && x == x2)
52 else if (is_friend && cave[y][x].m_idx > 0 &&
53 !are_enemies(m_ptr, &m_list[cave[y][x].m_idx]))
55 /* Friends don't shoot friends */
59 if (is_friend && player_bold(y, x))
68 * @brief モンスターが敵対モンスターに直接ブレスを当てることが可能かを判定する /
69 * Determine if a breath will hit the target.
76 * @param is_friend TRUEならば、プレイヤーを巻き込む時にブレスの判定をFALSEにする。
77 * @return ブレスを直接当てられるならばTRUEを返す
79 static bool breath_direct(POSITION y1, POSITION x1, POSITION y2, POSITION x2, POSITION rad, EFFECT_ID typ, bool is_friend)
81 /* Must be the same as projectable() */
93 POSITION gx[1024], gy[1024];
95 POSITION gm_rad = rad;
108 case GF_DISINTEGRATE:
116 /* Check the projection path */
117 grid_n = project_path(grid_g, MAX_RANGE, y1, x1, y2, x2, flg);
119 /* Project along the path */
120 for (i = 0; i < grid_n; ++i)
122 int ny = GRID_Y(grid_g[i]);
123 int nx = GRID_X(grid_g[i]);
125 if (flg & PROJECT_DISI)
127 /* Hack -- Balls explode before reaching walls */
128 if (cave_stop_disintegration(ny, nx)) break;
130 else if (flg & PROJECT_LOS)
132 /* Hack -- Balls explode before reaching walls */
133 if (!cave_los_bold(ny, nx)) break;
137 /* Hack -- Balls explode before reaching walls */
138 if (!cave_have_flag_bold(ny, nx, FF_PROJECT)) break;
141 /* Save the "blast epicenter" */
150 if (flg & PROJECT_DISI)
152 if (in_disintegration_range(y1, x1, y2, x2) && (distance(y1, x1, y2, x2) <= rad)) hit2 = TRUE;
153 if (in_disintegration_range(y1, x1, p_ptr->y, p_ptr->x) && (distance(y1, x1, p_ptr->y, p_ptr->x) <= rad)) hityou = TRUE;
155 else if (flg & PROJECT_LOS)
157 if (los(y1, x1, y2, x2) && (distance(y1, x1, y2, x2) <= rad)) hit2 = TRUE;
158 if (los(y1, x1, p_ptr->y, p_ptr->x) && (distance(y1, x1, p_ptr->y, p_ptr->x) <= rad)) hityou = TRUE;
162 if (projectable(y1, x1, y2, x2) && (distance(y1, x1, y2, x2) <= rad)) hit2 = TRUE;
163 if (projectable(y1, x1, p_ptr->y, p_ptr->x) && (distance(y1, x1, p_ptr->y, p_ptr->x) <= rad)) hityou = TRUE;
168 breath_shape(grid_g, grid_n, &grids, gx, gy, gm, &gm_rad, rad, y1, x1, y, x, typ);
170 for (i = 0; i < grids; i++)
172 /* Extract the location */
176 if ((y == y2) && (x == x2)) hit2 = TRUE;
177 if (player_bold(y, x)) hityou = TRUE;
181 if (!hit2) return FALSE;
182 if (is_friend && hityou) return FALSE;
188 * @brief モンスターが特殊能力の目標地点を決める処理 /
189 * Get the actual center point of ball spells (rad > 1) (originally from TOband)
192 * @param ty 目標Y座標を返す参照ポインタ
193 * @param tx 目標X座標を返す参照ポインタ
194 * @param flg 判定のフラグ配列
197 void get_project_point(POSITION sy, POSITION sx, POSITION *ty, POSITION *tx, BIT_FLAGS flg)
202 path_n = project_path(path_g, MAX_RANGE, sy, sx, *ty, *tx, flg);
207 /* Project along the path */
208 for (i = 0; i < path_n; i++)
210 sy = GRID_Y(path_g[i]);
211 sx = GRID_X(path_g[i]);
213 /* Hack -- Balls explode before reaching walls */
214 if (!cave_have_flag_bold(sy, sx, FF_PROJECT)) break;
222 * @brief モンスターが敵モンスターに魔力消去を使うかどうかを返す /
223 * Check should monster cast dispel spell at other monster.
224 * @param m_idx 術者のモンスターID
225 * @param t_idx 目標のモンスターID
226 * @return 魔力消去を使うべきならばTRUEを変えす。
228 static bool dispel_check_monster(MONSTER_IDX m_idx, MONSTER_IDX t_idx)
230 monster_type *t_ptr = &m_list[t_idx];
233 if (MON_INVULNER(t_ptr)) return TRUE;
236 if (t_ptr->mspeed < 135)
238 if (MON_FAST(t_ptr)) return TRUE;
242 if (t_idx == p_ptr->riding)
244 if (dispel_check(m_idx)) return TRUE;
247 /* No need to cast dispel spell */
252 * @brief モンスターが敵モンスターに特殊能力を使う処理のメインルーチン /
253 * Monster tries to 'cast a spell' (or breath, etc) at another monster.
254 * @param m_idx 術者のモンスターID
255 * @return 実際に特殊能力を使った場合TRUEを返す
257 * The player is only disturbed if able to be affected by the spell.
259 bool monst_spell_monst(MONSTER_IDX m_idx)
261 POSITION y = 0, x = 0;
263 MONSTER_IDX target_idx = 0;
269 byte spell[96], num = 0;
271 GAME_TEXT m_name[160];
272 GAME_TEXT t_name[160];
278 monster_type *m_ptr = &m_list[m_idx];
279 monster_type *t_ptr = NULL;
281 monster_race *r_ptr = &r_info[m_ptr->r_idx];
285 bool see_m = is_seen(m_ptr);
286 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[p_ptr->dungeon_idx].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;
295 /* Cannot cast spells when confused */
296 if (MON_CONFUSED(m_ptr)) return (FALSE);
298 /* Extract the racial spell flags */
300 f5 = r_ptr->a_ability_flags1;
301 f6 = r_ptr->a_ability_flags2;
303 /* Target is given for pet? */
304 if (pet_t_m_idx && pet)
306 target_idx = pet_t_m_idx;
307 t_ptr = &m_list[target_idx];
309 /* Cancel if not projectable (for now) */
310 if ((m_idx == target_idx) || !projectable(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx))
316 /* Is there counter attack target? */
317 if (!target_idx && m_ptr->target_y)
319 target_idx = cave[m_ptr->target_y][m_ptr->target_x].m_idx;
323 t_ptr = &m_list[target_idx];
325 /* Cancel if neither enemy nor a given target */
326 if ((m_idx == target_idx) ||
327 ((target_idx != pet_t_m_idx) && !are_enemies(m_ptr, t_ptr)))
332 /* Allow only summoning etc.. if not projectable */
333 else if (!projectable(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx))
335 f4 &= (RF4_INDIRECT_MASK);
336 f5 &= (RF5_INDIRECT_MASK);
337 f6 &= (RF6_INDIRECT_MASK);
342 /* Look for enemies normally */
345 bool success = FALSE;
347 if (p_ptr->inside_battle)
349 start = randint1(m_max-1) + m_max;
350 if (randint0(2)) plus = -1;
352 else start = m_max + 1;
354 /* Scan thru all monsters */
355 for (i = start; ((i < start + m_max) && (i > start - m_max)); i += plus)
357 MONSTER_IDX dummy = (i % m_max);
358 if (!dummy) continue;
361 t_ptr = &m_list[target_idx];
363 /* Skip dead monsters */
364 if (!t_ptr->r_idx) continue;
366 /* Monster must be 'an enemy' */
367 if ((m_idx == target_idx) || !are_enemies(m_ptr, t_ptr)) continue;
369 /* Monster must be projectable */
370 if (!projectable(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
378 if (!success) return FALSE;
382 /* OK -- we've got a target */
386 /* Forget old counter attack target */
389 /* Remove unimplemented spells */
390 f6 &= ~(RF6_WORLD | RF6_TRAPS | RF6_FORGET);
392 if (f4 & RF4_BR_LITE)
394 if (!los(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx))
395 f4 &= ~(RF4_BR_LITE);
398 /* Remove unimplemented special moves */
399 if (f6 & RF6_SPECIAL)
401 if ((m_ptr->r_idx != MON_ROLENTO) && (r_ptr->d_char != 'B'))
402 f6 &= ~(RF6_SPECIAL);
405 if (f6 & RF6_DARKNESS)
407 bool vs_ninja = (p_ptr->pclass == CLASS_NINJA) && !is_hostile(t_ptr);
410 !(r_ptr->flags3 & (RF3_UNDEAD | RF3_HURT_LITE)) &&
411 !(r_ptr->flags7 & RF7_DARK_MASK))
412 can_use_lite_area = TRUE;
414 if (!(r_ptr->flags2 & RF2_STUPID))
416 if (d_info[p_ptr->dungeon_idx].flags1 & DF1_DARKNESS) f6 &= ~(RF6_DARKNESS);
417 else if (vs_ninja && !can_use_lite_area) f6 &= ~(RF6_DARKNESS);
421 if (in_no_magic_dungeon && !(r_ptr->flags2 & RF2_STUPID))
423 f4 &= (RF4_NOMAGIC_MASK);
424 f5 &= (RF5_NOMAGIC_MASK);
425 f6 &= (RF6_NOMAGIC_MASK);
428 if (p_ptr->inside_arena || p_ptr->inside_battle)
430 f4 &= ~(RF4_SUMMON_MASK);
431 f5 &= ~(RF5_SUMMON_MASK);
432 f6 &= ~(RF6_SUMMON_MASK | RF6_TELE_LEVEL);
434 if (m_ptr->r_idx == MON_ROLENTO) f6 &= ~(RF6_SPECIAL);
437 if (p_ptr->inside_battle && !one_in_(3))
442 if (m_idx == p_ptr->riding)
444 f4 &= ~(RF4_RIDING_MASK);
445 f5 &= ~(RF5_RIDING_MASK);
446 f6 &= ~(RF6_RIDING_MASK);
452 f6 &= ~(RF6_DARKNESS | RF6_TRAPS);
454 if (!(p_ptr->pet_extra_flags & PF_TELEPORT))
456 f6 &= ~(RF6_BLINK | RF6_TPORT | RF6_TELE_TO | RF6_TELE_AWAY | RF6_TELE_LEVEL);
459 if (!(p_ptr->pet_extra_flags & PF_ATTACK_SPELL))
461 f4 &= ~(RF4_ATTACK_MASK);
462 f5 &= ~(RF5_ATTACK_MASK);
463 f6 &= ~(RF6_ATTACK_MASK);
466 if (!(p_ptr->pet_extra_flags & PF_SUMMON_SPELL))
468 f4 &= ~(RF4_SUMMON_MASK);
469 f5 &= ~(RF5_SUMMON_MASK);
470 f6 &= ~(RF6_SUMMON_MASK);
473 /* Prevent collateral damage */
474 if (!(p_ptr->pet_extra_flags & PF_BALL_SPELL) && (m_idx != p_ptr->riding))
476 if ((f4 & (RF4_BALL_MASK & ~(RF4_ROCKET))) ||
477 (f5 & RF5_BALL_MASK) ||
478 (f6 & RF6_BALL_MASK))
483 get_project_point(m_ptr->fy, m_ptr->fx, &real_y, &real_x, 0L);
485 if (projectable(real_y, real_x, p_ptr->y, p_ptr->x))
487 int dist = distance(real_y, real_x, p_ptr->y, p_ptr->x);
491 f4 &= ~(RF4_BALL_MASK & ~(RF4_ROCKET));
492 f5 &= ~(RF5_BALL_MASK);
493 f6 &= ~(RF6_BALL_MASK);
497 f4 &= ~(RF4_BIG_BALL_MASK);
498 f5 &= ~(RF5_BIG_BALL_MASK);
499 f6 &= ~(RF6_BIG_BALL_MASK);
502 else if (f5 & RF5_BA_LITE)
504 if ((distance(real_y, real_x, p_ptr->y, p_ptr->x) <= 4) && los(real_y, real_x, p_ptr->y, p_ptr->x))
505 f5 &= ~(RF5_BA_LITE);
514 get_project_point(m_ptr->fy, m_ptr->fx, &real_y, &real_x, PROJECT_STOP);
515 if (projectable(real_y, real_x, p_ptr->y, p_ptr->x) && (distance(real_y, real_x, p_ptr->y, p_ptr->x) <= 2))
519 if (((f4 & RF4_BEAM_MASK) || (f5 & RF5_BEAM_MASK) || (f6 & RF6_BEAM_MASK)) &&
520 !direct_beam(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx, m_ptr))
522 f4 &= ~(RF4_BEAM_MASK);
523 f5 &= ~(RF5_BEAM_MASK);
524 f6 &= ~(RF6_BEAM_MASK);
527 if ((f4 & RF4_BREATH_MASK) || (f5 & RF5_BREATH_MASK) || (f6 & RF6_BREATH_MASK))
529 /* Expected breath radius */
530 POSITION rad = (r_ptr->flags2 & RF2_POWERFUL) ? 3 : 2;
532 if (!breath_direct(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx, rad, 0, TRUE))
534 f4 &= ~(RF4_BREATH_MASK);
535 f5 &= ~(RF5_BREATH_MASK);
536 f6 &= ~(RF6_BREATH_MASK);
538 else if ((f4 & RF4_BR_LITE) &&
539 !breath_direct(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx, rad, GF_LITE, TRUE))
541 f4 &= ~(RF4_BR_LITE);
543 else if ((f4 & RF4_BR_DISI) &&
544 !breath_direct(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx, rad, GF_DISINTEGRATE, TRUE))
546 f4 &= ~(RF4_BR_DISI);
551 /* Special moves restriction */
552 if (f6 & RF6_SPECIAL)
554 if (m_ptr->r_idx == MON_ROLENTO)
556 if ((p_ptr->pet_extra_flags & (PF_ATTACK_SPELL | PF_SUMMON_SPELL)) != (PF_ATTACK_SPELL | PF_SUMMON_SPELL))
557 f6 &= ~(RF6_SPECIAL);
559 else if (r_ptr->d_char == 'B')
561 if ((p_ptr->pet_extra_flags & (PF_ATTACK_SPELL | PF_TELEPORT)) != (PF_ATTACK_SPELL | PF_TELEPORT))
562 f6 &= ~(RF6_SPECIAL);
564 else f6 &= ~(RF6_SPECIAL);
568 /* Remove some spells if necessary */
570 if (!(r_ptr->flags2 & RF2_STUPID))
572 /* Check for a clean bolt shot */
573 if (((f4 & RF4_BOLT_MASK) ||
574 (f5 & RF5_BOLT_MASK) ||
575 (f6 & RF6_BOLT_MASK)) &&
576 !clean_shot(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx, pet))
578 f4 &= ~(RF4_BOLT_MASK);
579 f5 &= ~(RF5_BOLT_MASK);
580 f6 &= ~(RF6_BOLT_MASK);
583 /* Check for a possible summon */
584 if (((f4 & RF4_SUMMON_MASK) ||
585 (f5 & RF5_SUMMON_MASK) ||
586 (f6 & RF6_SUMMON_MASK)) &&
587 !(summon_possible(t_ptr->fy, t_ptr->fx)))
589 /* Remove summoning spells */
590 f4 &= ~(RF4_SUMMON_MASK);
591 f5 &= ~(RF5_SUMMON_MASK);
592 f6 &= ~(RF6_SUMMON_MASK);
596 if ((f4 & RF4_DISPEL) && !dispel_check_monster(m_idx, target_idx))
598 /* Remove dispel spell */
602 /* Check for a possible raise dead */
603 if ((f6 & RF6_RAISE_DEAD) && !raise_possible(m_ptr))
605 /* Remove raise dead spell */
606 f6 &= ~(RF6_RAISE_DEAD);
609 /* Special moves restriction */
610 if (f6 & RF6_SPECIAL)
612 if ((m_ptr->r_idx == MON_ROLENTO) && !summon_possible(t_ptr->fy, t_ptr->fx))
614 f6 &= ~(RF6_SPECIAL);
619 if (r_ptr->flags2 & RF2_SMART)
621 /* Hack -- allow "desperate" spells */
622 if ((m_ptr->hp < m_ptr->maxhp / 10) &&
623 (randint0(100) < 50))
625 /* Require intelligent spells */
626 f4 &= (RF4_INT_MASK);
627 f5 &= (RF5_INT_MASK);
628 f6 &= (RF6_INT_MASK);
631 /* Hack -- decline "teleport level" in some case */
632 if ((f6 & RF6_TELE_LEVEL) && TELE_LEVEL_IS_INEFF((target_idx == p_ptr->riding) ? 0 : target_idx))
634 f6 &= ~(RF6_TELE_LEVEL);
639 if (!f4 && !f5 && !f6) return FALSE;
641 /* Extract the "inate" spells */
642 for (k = 0; k < 32; k++)
644 if (f4 & (1L << k)) spell[num++] = k + RF4_SPELL_START;
647 /* Extract the "normal" spells */
648 for (k = 0; k < 32; k++)
650 if (f5 & (1L << k)) spell[num++] = k + RF5_SPELL_START;
653 /* Extract the "bizarre" spells */
654 for (k = 0; k < 32; k++)
656 if (f6 & (1L << k)) spell[num++] = k + RF6_SPELL_START;
660 if (!num) return (FALSE);
662 /* Stop if player is dead or gone */
663 if (!p_ptr->playing || p_ptr->is_dead) return (FALSE);
665 /* Handle "leaving" */
666 if (p_ptr->leaving) return (FALSE);
668 /* Get the monster name (or "it") */
669 monster_desc(m_name, m_ptr, 0x00);
672 /* Get the monster possessive ("his"/"her"/"its") */
673 monster_desc(m_poss, m_ptr, MD_PRON_VISIBLE | MD_POSSESSIVE);
676 /* Get the target's name (or "it") */
677 monster_desc(t_name, t_ptr, 0x00);
679 /* Choose a spell to cast */
680 thrown_spell = spell[randint0(num)];
682 if (p_ptr->riding && (m_idx == p_ptr->riding)) disturb(TRUE, TRUE);
684 /* Check for spell failure (inate attacks never fail) */
685 if (!spell_is_inate(thrown_spell) && (in_no_magic_dungeon || (MON_STUNNED(m_ptr) && one_in_(2))))
688 if (see_m) msg_format(_("%^sは呪文を唱えようとしたが失敗した。",
689 "%^s tries to cast a spell, but fails."), m_name);
694 /* Hex: Anti Magic Barrier */
695 if (!spell_is_inate(thrown_spell) && magic_barrier(m_idx))
697 if (see_m) msg_format(_("反魔法バリアが%^sの呪文をかき消した。",
698 "Anti magic barrier cancels the spell which %^s casts."), m_name);
702 can_remember = is_original_ap_and_seen(m_ptr);
704 dam = monspell_to_monster(thrown_spell, y, x, m_idx, target_idx);
705 if (dam < 0) return FALSE;
707 if (m_ptr->ml && maneable && !world_monster && !p_ptr->blind && (p_ptr->pclass == CLASS_IMITATOR))
709 if (thrown_spell != 167) /* Not RF6_SPECIAL */
711 if (p_ptr->mane_num == MAX_MANE)
714 for (i = 0; i < p_ptr->mane_num - 1; i++)
716 p_ptr->mane_spell[i] = p_ptr->mane_spell[i+1];
717 p_ptr->mane_dam[i] = p_ptr->mane_dam[i+1];
720 p_ptr->mane_spell[p_ptr->mane_num] = thrown_spell - RF4_SPELL_START;
721 p_ptr->mane_dam[p_ptr->mane_num] = dam;
725 p_ptr->redraw |= (PR_IMITATION);
729 /* Remember what the monster did, if we saw it */
733 if (thrown_spell < RF4_SPELL_START + RF4_SPELL_SIZE)
735 r_ptr->r_flags4 |= (1L << (thrown_spell - RF4_SPELL_START));
736 if (r_ptr->r_cast_spell < MAX_UCHAR) r_ptr->r_cast_spell++;
740 else if (thrown_spell < RF5_SPELL_START + RF5_SPELL_SIZE)
742 r_ptr->r_flags5 |= (1L << (thrown_spell - RF5_SPELL_START));
743 if (r_ptr->r_cast_spell < MAX_UCHAR) r_ptr->r_cast_spell++;
747 else if (thrown_spell < RF6_SPELL_START + RF6_SPELL_SIZE)
749 r_ptr->r_flags6 |= (1L << (thrown_spell - RF6_SPELL_START));
750 if (r_ptr->r_cast_spell < MAX_UCHAR) r_ptr->r_cast_spell++;
754 /* Always take note of monsters that kill you */
755 if (p_ptr->is_dead && (r_ptr->r_deaths < MAX_SHORT) && !p_ptr->inside_arena)
757 r_ptr->r_deaths++; /* Ignore appearance difference */
760 /* A spell was cast */