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"
19 * @brief モンスターが敵対モンスターにビームを当てること可能かを判定する /
20 * Determine if a beam spell will hit the target.
25 * @param m_ptr 使用するモンスターの構造体参照ポインタ
26 * @return ビームが到達可能ならばTRUEを返す
28 static bool direct_beam(int y1, int x1, int y2, int x2, monster_type *m_ptr)
36 bool is_friend = is_pet(m_ptr);
38 /* Check the projection path */
39 grid_n = project_path(grid_g, MAX_RANGE, y1, x1, y2, x2, PROJECT_THRU);
41 /* No grid is ever projectable from itself */
42 if (!grid_n) return (FALSE);
44 for (i = 0; i < grid_n; i++)
46 y = GRID_Y(grid_g[i]);
47 x = GRID_X(grid_g[i]);
49 if (y == y2 && x == x2)
51 else if (is_friend && cave[y][x].m_idx > 0 &&
52 !are_enemies(m_ptr, &m_list[cave[y][x].m_idx]))
54 /* Friends don't shoot friends */
58 if (is_friend && player_bold(y, x))
67 * @brief モンスターが敵対モンスターに直接ブレスを当てることが可能かを判定する /
68 * Determine if a breath will hit the target.
75 * @param is_friend TRUEならば、プレイヤーを巻き込む時にブレスの判定をFALSEにする。
76 * @return ブレスを直接当てられるならばTRUEを返す
78 static bool breath_direct(POSITION y1, POSITION x1, POSITION y2, POSITION x2, POSITION rad, EFFECT_ID typ, bool is_friend)
80 /* Must be the same as projectable() */
92 POSITION gx[1024], gy[1024];
94 POSITION gm_rad = rad;
107 case GF_DISINTEGRATE:
115 /* Check the projection path */
116 grid_n = project_path(grid_g, MAX_RANGE, y1, x1, y2, x2, flg);
118 /* Project along the path */
119 for (i = 0; i < grid_n; ++i)
121 int ny = GRID_Y(grid_g[i]);
122 int nx = GRID_X(grid_g[i]);
124 if (flg & PROJECT_DISI)
126 /* Hack -- Balls explode before reaching walls */
127 if (cave_stop_disintegration(ny, nx)) break;
129 else if (flg & PROJECT_LOS)
131 /* Hack -- Balls explode before reaching walls */
132 if (!cave_los_bold(ny, nx)) break;
136 /* Hack -- Balls explode before reaching walls */
137 if (!cave_have_flag_bold(ny, nx, FF_PROJECT)) break;
140 /* Save the "blast epicenter" */
149 if (flg & PROJECT_DISI)
151 if (in_disintegration_range(y1, x1, y2, x2) && (distance(y1, x1, y2, x2) <= rad)) hit2 = TRUE;
152 if (in_disintegration_range(y1, x1, p_ptr->y, p_ptr->x) && (distance(y1, x1, p_ptr->y, p_ptr->x) <= rad)) hityou = TRUE;
154 else if (flg & PROJECT_LOS)
156 if (los(y1, x1, y2, x2) && (distance(y1, x1, y2, x2) <= rad)) hit2 = TRUE;
157 if (los(y1, x1, p_ptr->y, p_ptr->x) && (distance(y1, x1, p_ptr->y, p_ptr->x) <= rad)) hityou = TRUE;
161 if (projectable(y1, x1, y2, x2) && (distance(y1, x1, y2, x2) <= rad)) hit2 = TRUE;
162 if (projectable(y1, x1, p_ptr->y, p_ptr->x) && (distance(y1, x1, p_ptr->y, p_ptr->x) <= rad)) hityou = TRUE;
167 breath_shape(grid_g, grid_n, &grids, gx, gy, gm, &gm_rad, rad, y1, x1, y, x, typ);
169 for (i = 0; i < grids; i++)
171 /* Extract the location */
175 if ((y == y2) && (x == x2)) hit2 = TRUE;
176 if (player_bold(y, x)) hityou = TRUE;
180 if (!hit2) return FALSE;
181 if (is_friend && hityou) return FALSE;
187 * @brief モンスターが特殊能力の目標地点を決める処理 /
188 * Get the actual center point of ball spells (rad > 1) (originally from TOband)
191 * @param ty 目標Y座標を返す参照ポインタ
192 * @param tx 目標X座標を返す参照ポインタ
193 * @param flg 判定のフラグ配列
196 void get_project_point(POSITION sy, POSITION sx, POSITION *ty, POSITION *tx, BIT_FLAGS flg)
201 path_n = project_path(path_g, MAX_RANGE, sy, sx, *ty, *tx, flg);
206 /* Project along the path */
207 for (i = 0; i < path_n; i++)
209 sy = GRID_Y(path_g[i]);
210 sx = GRID_X(path_g[i]);
212 /* Hack -- Balls explode before reaching walls */
213 if (!cave_have_flag_bold(sy, sx, FF_PROJECT)) break;
221 * @brief モンスターが敵モンスターに魔力消去を使うかどうかを返す /
222 * Check should monster cast dispel spell at other monster.
223 * @param m_idx 術者のモンスターID
224 * @param t_idx 目標のモンスターID
225 * @return 魔力消去を使うべきならばTRUEを変えす。
227 static bool dispel_check_monster(MONSTER_IDX m_idx, MONSTER_IDX t_idx)
229 monster_type *t_ptr = &m_list[t_idx];
232 if (MON_INVULNER(t_ptr)) return TRUE;
235 if (t_ptr->mspeed < 135)
237 if (MON_FAST(t_ptr)) return TRUE;
241 if (t_idx == p_ptr->riding)
243 if (dispel_check(m_idx)) return TRUE;
246 /* No need to cast dispel spell */
251 * @brief モンスターが敵モンスターに特殊能力を使う処理のメインルーチン /
252 * Monster tries to 'cast a spell' (or breath, etc) at another monster.
253 * @param m_idx 術者のモンスターID
254 * @return 実際に特殊能力を使った場合TRUEを返す
256 * The player is only disturbed if able to be affected by the spell.
258 bool monst_spell_monst(MONSTER_IDX m_idx)
260 POSITION y = 0, x = 0;
262 MONSTER_IDX target_idx = 0;
268 byte spell[96], num = 0;
270 GAME_TEXT m_name[160];
271 GAME_TEXT t_name[160];
277 monster_type *m_ptr = &m_list[m_idx];
278 monster_type *t_ptr = NULL;
280 monster_race *r_ptr = &r_info[m_ptr->r_idx];
284 bool see_m = is_seen(m_ptr);
285 bool maneable = player_has_los_bold(m_ptr->fy, m_ptr->fx);
286 bool pet = is_pet(m_ptr);
288 bool in_no_magic_dungeon = (d_info[dungeon_type].flags1 & DF1_NO_MAGIC) && dun_level
289 && (!p_ptr->inside_quest || is_fixed_quest_idx(p_ptr->inside_quest));
291 bool can_use_lite_area = FALSE;
294 /* Cannot cast spells when confused */
295 if (MON_CONFUSED(m_ptr)) return (FALSE);
297 /* Extract the racial spell flags */
299 f5 = r_ptr->a_ability_flags1;
300 f6 = r_ptr->a_ability_flags2;
302 /* Target is given for pet? */
303 if (pet_t_m_idx && pet)
305 target_idx = pet_t_m_idx;
306 t_ptr = &m_list[target_idx];
308 /* Cancel if not projectable (for now) */
309 if ((m_idx == target_idx) || !projectable(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx))
315 /* Is there counter attack target? */
316 if (!target_idx && m_ptr->target_y)
318 target_idx = cave[m_ptr->target_y][m_ptr->target_x].m_idx;
322 t_ptr = &m_list[target_idx];
324 /* Cancel if neither enemy nor a given target */
325 if ((m_idx == target_idx) ||
326 ((target_idx != pet_t_m_idx) && !are_enemies(m_ptr, t_ptr)))
331 /* Allow only summoning etc.. if not projectable */
332 else if (!projectable(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx))
334 f4 &= (RF4_INDIRECT_MASK);
335 f5 &= (RF5_INDIRECT_MASK);
336 f6 &= (RF6_INDIRECT_MASK);
341 /* Look for enemies normally */
344 bool success = FALSE;
346 if (p_ptr->inside_battle)
348 start = randint1(m_max-1) + m_max;
349 if (randint0(2)) plus = -1;
351 else start = m_max + 1;
353 /* Scan thru all monsters */
354 for (i = start; ((i < start + m_max) && (i > start - m_max)); i += plus)
356 MONSTER_IDX dummy = (i % m_max);
357 if (!dummy) continue;
360 t_ptr = &m_list[target_idx];
362 /* Skip dead monsters */
363 if (!t_ptr->r_idx) continue;
365 /* Monster must be 'an enemy' */
366 if ((m_idx == target_idx) || !are_enemies(m_ptr, t_ptr)) continue;
368 /* Monster must be projectable */
369 if (!projectable(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
377 if (!success) return FALSE;
381 /* OK -- we've got a target */
385 /* Forget old counter attack target */
388 /* Remove unimplemented spells */
389 f6 &= ~(RF6_WORLD | RF6_TRAPS | RF6_FORGET);
391 if (f4 & RF4_BR_LITE)
393 if (!los(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx))
394 f4 &= ~(RF4_BR_LITE);
397 /* Remove unimplemented special moves */
398 if (f6 & RF6_SPECIAL)
400 if ((m_ptr->r_idx != MON_ROLENTO) && (r_ptr->d_char != 'B'))
401 f6 &= ~(RF6_SPECIAL);
404 if (f6 & RF6_DARKNESS)
406 bool vs_ninja = (p_ptr->pclass == CLASS_NINJA) && !is_hostile(t_ptr);
409 !(r_ptr->flags3 & (RF3_UNDEAD | RF3_HURT_LITE)) &&
410 !(r_ptr->flags7 & RF7_DARK_MASK))
411 can_use_lite_area = TRUE;
413 if (!(r_ptr->flags2 & RF2_STUPID))
415 if (d_info[dungeon_type].flags1 & DF1_DARKNESS) f6 &= ~(RF6_DARKNESS);
416 else if (vs_ninja && !can_use_lite_area) f6 &= ~(RF6_DARKNESS);
420 if (in_no_magic_dungeon && !(r_ptr->flags2 & RF2_STUPID))
422 f4 &= (RF4_NOMAGIC_MASK);
423 f5 &= (RF5_NOMAGIC_MASK);
424 f6 &= (RF6_NOMAGIC_MASK);
427 if (p_ptr->inside_arena || p_ptr->inside_battle)
429 f4 &= ~(RF4_SUMMON_MASK);
430 f5 &= ~(RF5_SUMMON_MASK);
431 f6 &= ~(RF6_SUMMON_MASK | RF6_TELE_LEVEL);
433 if (m_ptr->r_idx == MON_ROLENTO) f6 &= ~(RF6_SPECIAL);
436 if (p_ptr->inside_battle && !one_in_(3))
441 if (m_idx == p_ptr->riding)
443 f4 &= ~(RF4_RIDING_MASK);
444 f5 &= ~(RF5_RIDING_MASK);
445 f6 &= ~(RF6_RIDING_MASK);
451 f6 &= ~(RF6_DARKNESS | RF6_TRAPS);
453 if (!(p_ptr->pet_extra_flags & PF_TELEPORT))
455 f6 &= ~(RF6_BLINK | RF6_TPORT | RF6_TELE_TO | RF6_TELE_AWAY | RF6_TELE_LEVEL);
458 if (!(p_ptr->pet_extra_flags & PF_ATTACK_SPELL))
460 f4 &= ~(RF4_ATTACK_MASK);
461 f5 &= ~(RF5_ATTACK_MASK);
462 f6 &= ~(RF6_ATTACK_MASK);
465 if (!(p_ptr->pet_extra_flags & PF_SUMMON_SPELL))
467 f4 &= ~(RF4_SUMMON_MASK);
468 f5 &= ~(RF5_SUMMON_MASK);
469 f6 &= ~(RF6_SUMMON_MASK);
472 /* Prevent collateral damage */
473 if (!(p_ptr->pet_extra_flags & PF_BALL_SPELL) && (m_idx != p_ptr->riding))
475 if ((f4 & (RF4_BALL_MASK & ~(RF4_ROCKET))) ||
476 (f5 & RF5_BALL_MASK) ||
477 (f6 & RF6_BALL_MASK))
482 get_project_point(m_ptr->fy, m_ptr->fx, &real_y, &real_x, 0L);
484 if (projectable(real_y, real_x, p_ptr->y, p_ptr->x))
486 int dist = distance(real_y, real_x, p_ptr->y, p_ptr->x);
490 f4 &= ~(RF4_BALL_MASK & ~(RF4_ROCKET));
491 f5 &= ~(RF5_BALL_MASK);
492 f6 &= ~(RF6_BALL_MASK);
496 f4 &= ~(RF4_BIG_BALL_MASK);
497 f5 &= ~(RF5_BIG_BALL_MASK);
498 f6 &= ~(RF6_BIG_BALL_MASK);
501 else if (f5 & RF5_BA_LITE)
503 if ((distance(real_y, real_x, p_ptr->y, p_ptr->x) <= 4) && los(real_y, real_x, p_ptr->y, p_ptr->x))
504 f5 &= ~(RF5_BA_LITE);
513 get_project_point(m_ptr->fy, m_ptr->fx, &real_y, &real_x, PROJECT_STOP);
514 if (projectable(real_y, real_x, p_ptr->y, p_ptr->x) && (distance(real_y, real_x, p_ptr->y, p_ptr->x) <= 2))
518 if (((f4 & RF4_BEAM_MASK) || (f5 & RF5_BEAM_MASK) || (f6 & RF6_BEAM_MASK)) &&
519 !direct_beam(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx, m_ptr))
521 f4 &= ~(RF4_BEAM_MASK);
522 f5 &= ~(RF5_BEAM_MASK);
523 f6 &= ~(RF6_BEAM_MASK);
526 if ((f4 & RF4_BREATH_MASK) || (f5 & RF5_BREATH_MASK) || (f6 & RF6_BREATH_MASK))
528 /* Expected breath radius */
529 POSITION rad = (r_ptr->flags2 & RF2_POWERFUL) ? 3 : 2;
531 if (!breath_direct(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx, rad, 0, TRUE))
533 f4 &= ~(RF4_BREATH_MASK);
534 f5 &= ~(RF5_BREATH_MASK);
535 f6 &= ~(RF6_BREATH_MASK);
537 else if ((f4 & RF4_BR_LITE) &&
538 !breath_direct(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx, rad, GF_LITE, TRUE))
540 f4 &= ~(RF4_BR_LITE);
542 else if ((f4 & RF4_BR_DISI) &&
543 !breath_direct(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx, rad, GF_DISINTEGRATE, TRUE))
545 f4 &= ~(RF4_BR_DISI);
550 /* Special moves restriction */
551 if (f6 & RF6_SPECIAL)
553 if (m_ptr->r_idx == MON_ROLENTO)
555 if ((p_ptr->pet_extra_flags & (PF_ATTACK_SPELL | PF_SUMMON_SPELL)) != (PF_ATTACK_SPELL | PF_SUMMON_SPELL))
556 f6 &= ~(RF6_SPECIAL);
558 else if (r_ptr->d_char == 'B')
560 if ((p_ptr->pet_extra_flags & (PF_ATTACK_SPELL | PF_TELEPORT)) != (PF_ATTACK_SPELL | PF_TELEPORT))
561 f6 &= ~(RF6_SPECIAL);
563 else f6 &= ~(RF6_SPECIAL);
567 /* Remove some spells if necessary */
569 if (!(r_ptr->flags2 & RF2_STUPID))
571 /* Check for a clean bolt shot */
572 if (((f4 & RF4_BOLT_MASK) ||
573 (f5 & RF5_BOLT_MASK) ||
574 (f6 & RF6_BOLT_MASK)) &&
575 !clean_shot(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx, pet))
577 f4 &= ~(RF4_BOLT_MASK);
578 f5 &= ~(RF5_BOLT_MASK);
579 f6 &= ~(RF6_BOLT_MASK);
582 /* Check for a possible summon */
583 if (((f4 & RF4_SUMMON_MASK) ||
584 (f5 & RF5_SUMMON_MASK) ||
585 (f6 & RF6_SUMMON_MASK)) &&
586 !(summon_possible(t_ptr->fy, t_ptr->fx)))
588 /* Remove summoning spells */
589 f4 &= ~(RF4_SUMMON_MASK);
590 f5 &= ~(RF5_SUMMON_MASK);
591 f6 &= ~(RF6_SUMMON_MASK);
595 if ((f4 & RF4_DISPEL) && !dispel_check_monster(m_idx, target_idx))
597 /* Remove dispel spell */
601 /* Check for a possible raise dead */
602 if ((f6 & RF6_RAISE_DEAD) && !raise_possible(m_ptr))
604 /* Remove raise dead spell */
605 f6 &= ~(RF6_RAISE_DEAD);
608 /* Special moves restriction */
609 if (f6 & RF6_SPECIAL)
611 if ((m_ptr->r_idx == MON_ROLENTO) && !summon_possible(t_ptr->fy, t_ptr->fx))
613 f6 &= ~(RF6_SPECIAL);
618 if (r_ptr->flags2 & RF2_SMART)
620 /* Hack -- allow "desperate" spells */
621 if ((m_ptr->hp < m_ptr->maxhp / 10) &&
622 (randint0(100) < 50))
624 /* Require intelligent spells */
625 f4 &= (RF4_INT_MASK);
626 f5 &= (RF5_INT_MASK);
627 f6 &= (RF6_INT_MASK);
630 /* Hack -- decline "teleport level" in some case */
631 if ((f6 & RF6_TELE_LEVEL) && TELE_LEVEL_IS_INEFF((target_idx == p_ptr->riding) ? 0 : target_idx))
633 f6 &= ~(RF6_TELE_LEVEL);
638 if (!f4 && !f5 && !f6) return FALSE;
640 /* Extract the "inate" spells */
641 for (k = 0; k < 32; k++)
643 if (f4 & (1L << k)) spell[num++] = k + RF4_SPELL_START;
646 /* Extract the "normal" spells */
647 for (k = 0; k < 32; k++)
649 if (f5 & (1L << k)) spell[num++] = k + RF5_SPELL_START;
652 /* Extract the "bizarre" spells */
653 for (k = 0; k < 32; k++)
655 if (f6 & (1L << k)) spell[num++] = k + RF6_SPELL_START;
659 if (!num) return (FALSE);
661 /* Stop if player is dead or gone */
662 if (!p_ptr->playing || p_ptr->is_dead) return (FALSE);
664 /* Handle "leaving" */
665 if (p_ptr->leaving) return (FALSE);
667 /* Get the monster name (or "it") */
668 monster_desc(m_name, m_ptr, 0x00);
671 /* Get the monster possessive ("his"/"her"/"its") */
672 monster_desc(m_poss, m_ptr, MD_PRON_VISIBLE | MD_POSSESSIVE);
675 /* Get the target's name (or "it") */
676 monster_desc(t_name, t_ptr, 0x00);
678 /* Choose a spell to cast */
679 thrown_spell = spell[randint0(num)];
681 if (p_ptr->riding && (m_idx == p_ptr->riding)) disturb(TRUE, TRUE);
683 /* Check for spell failure (inate attacks never fail) */
684 if (!spell_is_inate(thrown_spell) && (in_no_magic_dungeon || (MON_STUNNED(m_ptr) && one_in_(2))))
687 if (see_m) msg_format(_("%^sは呪文を唱えようとしたが失敗した。",
688 "%^s tries to cast a spell, but fails."), m_name);
693 /* Hex: Anti Magic Barrier */
694 if (!spell_is_inate(thrown_spell) && magic_barrier(m_idx))
696 if (see_m) msg_format(_("反魔法バリアが%^sの呪文をかき消した。",
697 "Anti magic barrier cancels the spell which %^s casts."), m_name);
701 can_remember = is_original_ap_and_seen(m_ptr);
703 dam = monspell_to_monster(thrown_spell, y, x, m_idx, target_idx);
704 if (dam < 0) return FALSE;
706 if (m_ptr->ml && maneable && !world_monster && !p_ptr->blind && (p_ptr->pclass == CLASS_IMITATOR))
708 if (thrown_spell != 167) /* Not RF6_SPECIAL */
710 if (p_ptr->mane_num == MAX_MANE)
713 for (i = 0; i < p_ptr->mane_num - 1; i++)
715 p_ptr->mane_spell[i] = p_ptr->mane_spell[i+1];
716 p_ptr->mane_dam[i] = p_ptr->mane_dam[i+1];
719 p_ptr->mane_spell[p_ptr->mane_num] = thrown_spell - RF4_SPELL_START;
720 p_ptr->mane_dam[p_ptr->mane_num] = dam;
724 p_ptr->redraw |= (PR_IMITATION);
728 /* Remember what the monster did, if we saw it */
732 if (thrown_spell < RF4_SPELL_START + RF4_SPELL_SIZE)
734 r_ptr->r_flags4 |= (1L << (thrown_spell - RF4_SPELL_START));
735 if (r_ptr->r_cast_spell < MAX_UCHAR) r_ptr->r_cast_spell++;
739 else if (thrown_spell < RF5_SPELL_START + RF5_SPELL_SIZE)
741 r_ptr->r_flags5 |= (1L << (thrown_spell - RF5_SPELL_START));
742 if (r_ptr->r_cast_spell < MAX_UCHAR) r_ptr->r_cast_spell++;
746 else if (thrown_spell < RF6_SPELL_START + RF6_SPELL_SIZE)
748 r_ptr->r_flags6 |= (1L << (thrown_spell - RF6_SPELL_START));
749 if (r_ptr->r_cast_spell < MAX_UCHAR) r_ptr->r_cast_spell++;
753 /* Always take note of monsters that kill you */
754 if (p_ptr->is_dead && (r_ptr->r_deaths < MAX_SHORT) && !p_ptr->inside_arena)
756 r_ptr->r_deaths++; /* Ignore appearance difference */
759 /* A spell was cast */