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"
18 * @brief モンスターが敵対モンスターにビームを当てること可能かを判定する /
19 * Determine if a beam spell will hit the target.
24 * @param m_ptr 使用するモンスターの構造体参照ポインタ
25 * @return ビームが到達可能ならばTRUEを返す
27 static bool direct_beam(int y1, int x1, int y2, int x2, monster_type *m_ptr)
35 bool is_friend = is_pet(m_ptr);
37 /* Check the projection path */
38 grid_n = project_path(grid_g, MAX_RANGE, y1, x1, y2, x2, PROJECT_THRU);
40 /* No grid is ever projectable from itself */
41 if (!grid_n) return (FALSE);
43 for (i = 0; i < grid_n; i++)
45 y = GRID_Y(grid_g[i]);
46 x = GRID_X(grid_g[i]);
48 if (y == y2 && x == x2)
50 else if (is_friend && cave[y][x].m_idx > 0 &&
51 !are_enemies(m_ptr, &m_list[cave[y][x].m_idx]))
53 /* Friends don't shoot friends */
57 if (is_friend && player_bold(y, x))
66 * @brief モンスターが敵対モンスターに直接ブレスを当てることが可能かを判定する /
67 * Determine if a breath will hit the target.
74 * @param is_friend TRUEならば、プレイヤーを巻き込む時にブレスの判定をFALSEにする。
75 * @return ブレスを直接当てられるならばTRUEを返す
77 static bool breath_direct(POSITION y1, POSITION x1, POSITION y2, POSITION x2, POSITION rad, EFFECT_ID typ, bool is_friend)
79 /* Must be the same as projectable() */
91 POSITION gx[1024], gy[1024];
93 POSITION gm_rad = rad;
106 case GF_DISINTEGRATE:
114 /* Check the projection path */
115 grid_n = project_path(grid_g, MAX_RANGE, y1, x1, y2, x2, flg);
117 /* Project along the path */
118 for (i = 0; i < grid_n; ++i)
120 int ny = GRID_Y(grid_g[i]);
121 int nx = GRID_X(grid_g[i]);
123 if (flg & PROJECT_DISI)
125 /* Hack -- Balls explode before reaching walls */
126 if (cave_stop_disintegration(ny, nx)) break;
128 else if (flg & PROJECT_LOS)
130 /* Hack -- Balls explode before reaching walls */
131 if (!cave_los_bold(ny, nx)) break;
135 /* Hack -- Balls explode before reaching walls */
136 if (!cave_have_flag_bold(ny, nx, FF_PROJECT)) break;
139 /* Save the "blast epicenter" */
148 if (flg & PROJECT_DISI)
150 if (in_disintegration_range(y1, x1, y2, x2) && (distance(y1, x1, y2, x2) <= rad)) hit2 = TRUE;
151 if (in_disintegration_range(y1, x1, p_ptr->y, p_ptr->x) && (distance(y1, x1, p_ptr->y, p_ptr->x) <= rad)) hityou = TRUE;
153 else if (flg & PROJECT_LOS)
155 if (los(y1, x1, y2, x2) && (distance(y1, x1, y2, x2) <= rad)) hit2 = TRUE;
156 if (los(y1, x1, p_ptr->y, p_ptr->x) && (distance(y1, x1, p_ptr->y, p_ptr->x) <= rad)) hityou = TRUE;
160 if (projectable(y1, x1, y2, x2) && (distance(y1, x1, y2, x2) <= rad)) hit2 = TRUE;
161 if (projectable(y1, x1, p_ptr->y, p_ptr->x) && (distance(y1, x1, p_ptr->y, p_ptr->x) <= rad)) hityou = TRUE;
166 breath_shape(grid_g, grid_n, &grids, gx, gy, gm, &gm_rad, rad, y1, x1, y, x, typ);
168 for (i = 0; i < grids; i++)
170 /* Extract the location */
174 if ((y == y2) && (x == x2)) hit2 = TRUE;
175 if (player_bold(y, x)) hityou = TRUE;
179 if (!hit2) return FALSE;
180 if (is_friend && hityou) return FALSE;
186 * @brief モンスターが特殊能力の目標地点を決める処理 /
187 * Get the actual center point of ball spells (rad > 1) (originally from TOband)
190 * @param ty 目標Y座標を返す参照ポインタ
191 * @param tx 目標X座標を返す参照ポインタ
192 * @param flg 判定のフラグ配列
195 void get_project_point(POSITION sy, POSITION sx, POSITION *ty, POSITION *tx, BIT_FLAGS flg)
200 path_n = project_path(path_g, MAX_RANGE, sy, sx, *ty, *tx, flg);
205 /* Project along the path */
206 for (i = 0; i < path_n; i++)
208 sy = GRID_Y(path_g[i]);
209 sx = GRID_X(path_g[i]);
211 /* Hack -- Balls explode before reaching walls */
212 if (!cave_have_flag_bold(sy, sx, FF_PROJECT)) break;
220 * @brief モンスターが敵モンスターに魔力消去を使うかどうかを返す /
221 * Check should monster cast dispel spell at other monster.
222 * @param m_idx 術者のモンスターID
223 * @param t_idx 目標のモンスターID
224 * @return 魔力消去を使うべきならばTRUEを変えす。
226 static bool dispel_check_monster(MONSTER_IDX m_idx, MONSTER_IDX t_idx)
228 monster_type *t_ptr = &m_list[t_idx];
231 if (MON_INVULNER(t_ptr)) return TRUE;
234 if (t_ptr->mspeed < 135)
236 if (MON_FAST(t_ptr)) return TRUE;
240 if (t_idx == p_ptr->riding)
242 if (dispel_check(m_idx)) return TRUE;
245 /* No need to cast dispel spell */
250 * @brief モンスターが敵モンスターに特殊能力を使う処理のメインルーチン /
251 * Monster tries to 'cast a spell' (or breath, etc) at another monster.
252 * @param m_idx 術者のモンスターID
253 * @return 実際に特殊能力を使った場合TRUEを返す
255 * The player is only disturbed if able to be affected by the spell.
257 bool monst_spell_monst(MONSTER_IDX m_idx)
259 POSITION y = 0, x = 0;
261 MONSTER_IDX target_idx = 0;
267 byte spell[96], num = 0;
269 GAME_TEXT m_name[160];
270 GAME_TEXT t_name[160];
276 monster_type *m_ptr = &m_list[m_idx];
277 monster_type *t_ptr = NULL;
279 monster_race *r_ptr = &r_info[m_ptr->r_idx];
283 bool see_m = is_seen(m_ptr);
284 bool maneable = player_has_los_bold(m_ptr->fy, m_ptr->fx);
285 bool pet = is_pet(m_ptr);
287 bool in_no_magic_dungeon = (d_info[dungeon_type].flags1 & DF1_NO_MAGIC) && dun_level
288 && (!p_ptr->inside_quest || is_fixed_quest_idx(p_ptr->inside_quest));
290 bool can_use_lite_area = FALSE;
293 /* Cannot cast spells when confused */
294 if (MON_CONFUSED(m_ptr)) return (FALSE);
296 /* Extract the racial spell flags */
298 f5 = r_ptr->a_ability_flags1;
299 f6 = r_ptr->a_ability_flags2;
301 /* Target is given for pet? */
302 if (pet_t_m_idx && pet)
304 target_idx = pet_t_m_idx;
305 t_ptr = &m_list[target_idx];
307 /* Cancel if not projectable (for now) */
308 if ((m_idx == target_idx) || !projectable(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx))
314 /* Is there counter attack target? */
315 if (!target_idx && m_ptr->target_y)
317 target_idx = cave[m_ptr->target_y][m_ptr->target_x].m_idx;
321 t_ptr = &m_list[target_idx];
323 /* Cancel if neither enemy nor a given target */
324 if ((m_idx == target_idx) ||
325 ((target_idx != pet_t_m_idx) && !are_enemies(m_ptr, t_ptr)))
330 /* Allow only summoning etc.. if not projectable */
331 else if (!projectable(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx))
333 f4 &= (RF4_INDIRECT_MASK);
334 f5 &= (RF5_INDIRECT_MASK);
335 f6 &= (RF6_INDIRECT_MASK);
340 /* Look for enemies normally */
343 bool success = FALSE;
345 if (p_ptr->inside_battle)
347 start = randint1(m_max-1) + m_max;
348 if (randint0(2)) plus = -1;
350 else start = m_max + 1;
352 /* Scan thru all monsters */
353 for (i = start; ((i < start + m_max) && (i > start - m_max)); i += plus)
355 MONSTER_IDX dummy = (i % m_max);
356 if (!dummy) continue;
359 t_ptr = &m_list[target_idx];
361 /* Skip dead monsters */
362 if (!t_ptr->r_idx) continue;
364 /* Monster must be 'an enemy' */
365 if ((m_idx == target_idx) || !are_enemies(m_ptr, t_ptr)) continue;
367 /* Monster must be projectable */
368 if (!projectable(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
376 if (!success) return FALSE;
380 /* OK -- we've got a target */
384 /* Forget old counter attack target */
387 /* Remove unimplemented spells */
388 f6 &= ~(RF6_WORLD | RF6_TRAPS | RF6_FORGET);
390 if (f4 & RF4_BR_LITE)
392 if (!los(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx))
393 f4 &= ~(RF4_BR_LITE);
396 /* Remove unimplemented special moves */
397 if (f6 & RF6_SPECIAL)
399 if ((m_ptr->r_idx != MON_ROLENTO) && (r_ptr->d_char != 'B'))
400 f6 &= ~(RF6_SPECIAL);
403 if (f6 & RF6_DARKNESS)
405 bool vs_ninja = (p_ptr->pclass == CLASS_NINJA) && !is_hostile(t_ptr);
408 !(r_ptr->flags3 & (RF3_UNDEAD | RF3_HURT_LITE)) &&
409 !(r_ptr->flags7 & RF7_DARK_MASK))
410 can_use_lite_area = TRUE;
412 if (!(r_ptr->flags2 & RF2_STUPID))
414 if (d_info[dungeon_type].flags1 & DF1_DARKNESS) f6 &= ~(RF6_DARKNESS);
415 else if (vs_ninja && !can_use_lite_area) f6 &= ~(RF6_DARKNESS);
419 if (in_no_magic_dungeon && !(r_ptr->flags2 & RF2_STUPID))
421 f4 &= (RF4_NOMAGIC_MASK);
422 f5 &= (RF5_NOMAGIC_MASK);
423 f6 &= (RF6_NOMAGIC_MASK);
426 if (p_ptr->inside_arena || p_ptr->inside_battle)
428 f4 &= ~(RF4_SUMMON_MASK);
429 f5 &= ~(RF5_SUMMON_MASK);
430 f6 &= ~(RF6_SUMMON_MASK | RF6_TELE_LEVEL);
432 if (m_ptr->r_idx == MON_ROLENTO) f6 &= ~(RF6_SPECIAL);
435 if (p_ptr->inside_battle && !one_in_(3))
440 if (m_idx == p_ptr->riding)
442 f4 &= ~(RF4_RIDING_MASK);
443 f5 &= ~(RF5_RIDING_MASK);
444 f6 &= ~(RF6_RIDING_MASK);
450 f6 &= ~(RF6_DARKNESS | RF6_TRAPS);
452 if (!(p_ptr->pet_extra_flags & PF_TELEPORT))
454 f6 &= ~(RF6_BLINK | RF6_TPORT | RF6_TELE_TO | RF6_TELE_AWAY | RF6_TELE_LEVEL);
457 if (!(p_ptr->pet_extra_flags & PF_ATTACK_SPELL))
459 f4 &= ~(RF4_ATTACK_MASK);
460 f5 &= ~(RF5_ATTACK_MASK);
461 f6 &= ~(RF6_ATTACK_MASK);
464 if (!(p_ptr->pet_extra_flags & PF_SUMMON_SPELL))
466 f4 &= ~(RF4_SUMMON_MASK);
467 f5 &= ~(RF5_SUMMON_MASK);
468 f6 &= ~(RF6_SUMMON_MASK);
471 /* Prevent collateral damage */
472 if (!(p_ptr->pet_extra_flags & PF_BALL_SPELL) && (m_idx != p_ptr->riding))
474 if ((f4 & (RF4_BALL_MASK & ~(RF4_ROCKET))) ||
475 (f5 & RF5_BALL_MASK) ||
476 (f6 & RF6_BALL_MASK))
481 get_project_point(m_ptr->fy, m_ptr->fx, &real_y, &real_x, 0L);
483 if (projectable(real_y, real_x, p_ptr->y, p_ptr->x))
485 int dist = distance(real_y, real_x, p_ptr->y, p_ptr->x);
489 f4 &= ~(RF4_BALL_MASK & ~(RF4_ROCKET));
490 f5 &= ~(RF5_BALL_MASK);
491 f6 &= ~(RF6_BALL_MASK);
495 f4 &= ~(RF4_BIG_BALL_MASK);
496 f5 &= ~(RF5_BIG_BALL_MASK);
497 f6 &= ~(RF6_BIG_BALL_MASK);
500 else if (f5 & RF5_BA_LITE)
502 if ((distance(real_y, real_x, p_ptr->y, p_ptr->x) <= 4) && los(real_y, real_x, p_ptr->y, p_ptr->x))
503 f5 &= ~(RF5_BA_LITE);
512 get_project_point(m_ptr->fy, m_ptr->fx, &real_y, &real_x, PROJECT_STOP);
513 if (projectable(real_y, real_x, p_ptr->y, p_ptr->x) && (distance(real_y, real_x, p_ptr->y, p_ptr->x) <= 2))
517 if (((f4 & RF4_BEAM_MASK) || (f5 & RF5_BEAM_MASK) || (f6 & RF6_BEAM_MASK)) &&
518 !direct_beam(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx, m_ptr))
520 f4 &= ~(RF4_BEAM_MASK);
521 f5 &= ~(RF5_BEAM_MASK);
522 f6 &= ~(RF6_BEAM_MASK);
525 if ((f4 & RF4_BREATH_MASK) || (f5 & RF5_BREATH_MASK) || (f6 & RF6_BREATH_MASK))
527 /* Expected breath radius */
528 POSITION rad = (r_ptr->flags2 & RF2_POWERFUL) ? 3 : 2;
530 if (!breath_direct(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx, rad, 0, TRUE))
532 f4 &= ~(RF4_BREATH_MASK);
533 f5 &= ~(RF5_BREATH_MASK);
534 f6 &= ~(RF6_BREATH_MASK);
536 else if ((f4 & RF4_BR_LITE) &&
537 !breath_direct(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx, rad, GF_LITE, TRUE))
539 f4 &= ~(RF4_BR_LITE);
541 else if ((f4 & RF4_BR_DISI) &&
542 !breath_direct(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx, rad, GF_DISINTEGRATE, TRUE))
544 f4 &= ~(RF4_BR_DISI);
549 /* Special moves restriction */
550 if (f6 & RF6_SPECIAL)
552 if (m_ptr->r_idx == MON_ROLENTO)
554 if ((p_ptr->pet_extra_flags & (PF_ATTACK_SPELL | PF_SUMMON_SPELL)) != (PF_ATTACK_SPELL | PF_SUMMON_SPELL))
555 f6 &= ~(RF6_SPECIAL);
557 else if (r_ptr->d_char == 'B')
559 if ((p_ptr->pet_extra_flags & (PF_ATTACK_SPELL | PF_TELEPORT)) != (PF_ATTACK_SPELL | PF_TELEPORT))
560 f6 &= ~(RF6_SPECIAL);
562 else f6 &= ~(RF6_SPECIAL);
566 /* Remove some spells if necessary */
568 if (!(r_ptr->flags2 & RF2_STUPID))
570 /* Check for a clean bolt shot */
571 if (((f4 & RF4_BOLT_MASK) ||
572 (f5 & RF5_BOLT_MASK) ||
573 (f6 & RF6_BOLT_MASK)) &&
574 !clean_shot(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx, pet))
576 f4 &= ~(RF4_BOLT_MASK);
577 f5 &= ~(RF5_BOLT_MASK);
578 f6 &= ~(RF6_BOLT_MASK);
581 /* Check for a possible summon */
582 if (((f4 & RF4_SUMMON_MASK) ||
583 (f5 & RF5_SUMMON_MASK) ||
584 (f6 & RF6_SUMMON_MASK)) &&
585 !(summon_possible(t_ptr->fy, t_ptr->fx)))
587 /* Remove summoning spells */
588 f4 &= ~(RF4_SUMMON_MASK);
589 f5 &= ~(RF5_SUMMON_MASK);
590 f6 &= ~(RF6_SUMMON_MASK);
594 if ((f4 & RF4_DISPEL) && !dispel_check_monster(m_idx, target_idx))
596 /* Remove dispel spell */
600 /* Check for a possible raise dead */
601 if ((f6 & RF6_RAISE_DEAD) && !raise_possible(m_ptr))
603 /* Remove raise dead spell */
604 f6 &= ~(RF6_RAISE_DEAD);
607 /* Special moves restriction */
608 if (f6 & RF6_SPECIAL)
610 if ((m_ptr->r_idx == MON_ROLENTO) && !summon_possible(t_ptr->fy, t_ptr->fx))
612 f6 &= ~(RF6_SPECIAL);
617 if (r_ptr->flags2 & RF2_SMART)
619 /* Hack -- allow "desperate" spells */
620 if ((m_ptr->hp < m_ptr->maxhp / 10) &&
621 (randint0(100) < 50))
623 /* Require intelligent spells */
624 f4 &= (RF4_INT_MASK);
625 f5 &= (RF5_INT_MASK);
626 f6 &= (RF6_INT_MASK);
629 /* Hack -- decline "teleport level" in some case */
630 if ((f6 & RF6_TELE_LEVEL) && TELE_LEVEL_IS_INEFF((target_idx == p_ptr->riding) ? 0 : target_idx))
632 f6 &= ~(RF6_TELE_LEVEL);
637 if (!f4 && !f5 && !f6) return FALSE;
639 /* Extract the "inate" spells */
640 for (k = 0; k < 32; k++)
642 if (f4 & (1L << k)) spell[num++] = k + RF4_SPELL_START;
645 /* Extract the "normal" spells */
646 for (k = 0; k < 32; k++)
648 if (f5 & (1L << k)) spell[num++] = k + RF5_SPELL_START;
651 /* Extract the "bizarre" spells */
652 for (k = 0; k < 32; k++)
654 if (f6 & (1L << k)) spell[num++] = k + RF6_SPELL_START;
658 if (!num) return (FALSE);
660 /* Stop if player is dead or gone */
661 if (!p_ptr->playing || p_ptr->is_dead) return (FALSE);
663 /* Handle "leaving" */
664 if (p_ptr->leaving) return (FALSE);
666 /* Get the monster name (or "it") */
667 monster_desc(m_name, m_ptr, 0x00);
670 /* Get the monster possessive ("his"/"her"/"its") */
671 monster_desc(m_poss, m_ptr, MD_PRON_VISIBLE | MD_POSSESSIVE);
674 /* Get the target's name (or "it") */
675 monster_desc(t_name, t_ptr, 0x00);
677 /* Choose a spell to cast */
678 thrown_spell = spell[randint0(num)];
680 if (p_ptr->riding && (m_idx == p_ptr->riding)) disturb(TRUE, TRUE);
682 /* Check for spell failure (inate attacks never fail) */
683 if (!spell_is_inate(thrown_spell) && (in_no_magic_dungeon || (MON_STUNNED(m_ptr) && one_in_(2))))
686 if (see_m) msg_format(_("%^sは呪文を唱えようとしたが失敗した。",
687 "%^s tries to cast a spell, but fails."), m_name);
692 /* Hex: Anti Magic Barrier */
693 if (!spell_is_inate(thrown_spell) && magic_barrier(m_idx))
695 if (see_m) msg_format(_("反魔法バリアが%^sの呪文をかき消した。",
696 "Anti magic barrier cancels the spell which %^s casts."), m_name);
700 can_remember = is_original_ap_and_seen(m_ptr);
702 dam = monspell_to_monster(thrown_spell, y, x, m_idx, target_idx);
703 if (dam < 0) return FALSE;
705 if (m_ptr->ml && maneable && !world_monster && !p_ptr->blind && (p_ptr->pclass == CLASS_IMITATOR))
707 if (thrown_spell != 167) /* Not RF6_SPECIAL */
709 if (p_ptr->mane_num == MAX_MANE)
712 for (i = 0; i < p_ptr->mane_num - 1; i++)
714 p_ptr->mane_spell[i] = p_ptr->mane_spell[i+1];
715 p_ptr->mane_dam[i] = p_ptr->mane_dam[i+1];
718 p_ptr->mane_spell[p_ptr->mane_num] = thrown_spell - RF4_SPELL_START;
719 p_ptr->mane_dam[p_ptr->mane_num] = dam;
723 p_ptr->redraw |= (PR_IMITATION);
727 /* Remember what the monster did, if we saw it */
731 if (thrown_spell < RF4_SPELL_START + RF4_SPELL_SIZE)
733 r_ptr->r_flags4 |= (1L << (thrown_spell - RF4_SPELL_START));
734 if (r_ptr->r_cast_spell < MAX_UCHAR) r_ptr->r_cast_spell++;
738 else if (thrown_spell < RF5_SPELL_START + RF5_SPELL_SIZE)
740 r_ptr->r_flags5 |= (1L << (thrown_spell - RF5_SPELL_START));
741 if (r_ptr->r_cast_spell < MAX_UCHAR) r_ptr->r_cast_spell++;
745 else if (thrown_spell < RF6_SPELL_START + RF6_SPELL_SIZE)
747 r_ptr->r_flags6 |= (1L << (thrown_spell - RF6_SPELL_START));
748 if (r_ptr->r_cast_spell < MAX_UCHAR) r_ptr->r_cast_spell++;
752 /* Always take note of monsters that kill you */
753 if (p_ptr->is_dead && (r_ptr->r_deaths < MAX_SHORT) && !p_ptr->inside_arena)
755 r_ptr->r_deaths++; /* Ignore appearance difference */
758 /* A spell was cast */