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++)
186 /* Extract the location */
190 if ((y == y2) && (x == x2)) hit2 = TRUE;
191 if (player_bold(y, x)) hityou = TRUE;
195 if (!hit2) return FALSE;
196 if (is_friend && hityou) return FALSE;
202 * @brief モンスターが特殊能力の目標地点を決める処理 /
203 * Get the actual center point of ball spells (rad > 1) (originally from TOband)
206 * @param ty 目標Y座標を返す参照ポインタ
207 * @param tx 目標X座標を返す参照ポインタ
208 * @param flg 判定のフラグ配列
211 void get_project_point(POSITION sy, POSITION sx, POSITION *ty, POSITION *tx, BIT_FLAGS flg)
216 path_n = project_path(path_g, MAX_RANGE, sy, sx, *ty, *tx, flg);
221 /* Project along the path */
222 for (i = 0; i < path_n; i++)
224 sy = GRID_Y(path_g[i]);
225 sx = GRID_X(path_g[i]);
227 /* Hack -- Balls explode before reaching walls */
228 if (!cave_have_flag_bold(sy, sx, FF_PROJECT)) break;
236 * @brief モンスターが敵モンスターに魔力消去を使うかどうかを返す /
237 * Check should monster cast dispel spell at other monster.
238 * @param m_idx 術者のモンスターID
239 * @param t_idx 目標のモンスターID
240 * @return 魔力消去を使うべきならばTRUEを変えす。
242 static bool dispel_check_monster(MONSTER_IDX m_idx, MONSTER_IDX t_idx)
244 monster_type *t_ptr = ¤t_floor_ptr->m_list[t_idx];
246 if (MON_INVULNER(t_ptr)) return TRUE;
248 if (t_ptr->mspeed < 135)
250 if (MON_FAST(t_ptr)) return TRUE;
254 if (t_idx == p_ptr->riding)
256 if (dispel_check(m_idx)) return TRUE;
259 /* No need to cast dispel spell */
264 * @brief モンスターが敵モンスターに特殊能力を使う処理のメインルーチン /
265 * Monster tries to 'cast a spell' (or breath, etc) at another monster.
266 * @param m_idx 術者のモンスターID
267 * @return 実際に特殊能力を使った場合TRUEを返す
269 * The player is only disturbed if able to be affected by the spell.
271 bool monst_spell_monst(MONSTER_IDX m_idx)
273 POSITION y = 0, x = 0;
275 MONSTER_IDX target_idx = 0;
281 byte spell[96], num = 0;
283 GAME_TEXT m_name[160];
284 GAME_TEXT t_name[160];
290 monster_type *m_ptr = ¤t_floor_ptr->m_list[m_idx];
291 monster_type *t_ptr = NULL;
293 monster_race *r_ptr = &r_info[m_ptr->r_idx];
297 bool see_m = is_seen(m_ptr);
298 bool maneable = player_has_los_bold(m_ptr->fy, m_ptr->fx);
299 bool pet = is_pet(m_ptr);
301 bool in_no_magic_dungeon = (d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_MAGIC) && current_floor_ptr->dun_level
302 && (!p_ptr->inside_quest || is_fixed_quest_idx(p_ptr->inside_quest));
304 bool can_use_lite_area = FALSE;
307 /* Cannot cast spells when confused */
308 if (MON_CONFUSED(m_ptr)) return (FALSE);
310 /* Extract the racial spell flags */
312 f5 = r_ptr->a_ability_flags1;
313 f6 = r_ptr->a_ability_flags2;
315 /* Target is given for pet? */
316 if (pet_t_m_idx && pet)
318 target_idx = pet_t_m_idx;
319 t_ptr = ¤t_floor_ptr->m_list[target_idx];
321 /* Cancel if not projectable (for now) */
322 if ((m_idx == target_idx) || !projectable(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx))
328 /* Is there counter attack target? */
329 if (!target_idx && m_ptr->target_y)
331 target_idx = current_floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
335 t_ptr = ¤t_floor_ptr->m_list[target_idx];
337 /* Cancel if neither enemy nor a given target */
338 if ((m_idx == target_idx) ||
339 ((target_idx != pet_t_m_idx) && !are_enemies(m_ptr, t_ptr)))
344 /* Allow only summoning etc.. if not projectable */
345 else if (!projectable(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx))
347 f4 &= (RF4_INDIRECT_MASK);
348 f5 &= (RF5_INDIRECT_MASK);
349 f6 &= (RF6_INDIRECT_MASK);
354 /* Look for enemies normally */
357 bool success = FALSE;
359 if (p_ptr->inside_battle)
361 start = randint1(current_floor_ptr->m_max-1) + current_floor_ptr->m_max;
362 if (randint0(2)) plus = -1;
364 else start = current_floor_ptr->m_max + 1;
366 /* Scan thru all monsters */
367 for (i = start; ((i < start + current_floor_ptr->m_max) && (i > start - current_floor_ptr->m_max)); i += plus)
369 MONSTER_IDX dummy = (i % current_floor_ptr->m_max);
370 if (!dummy) continue;
373 t_ptr = ¤t_floor_ptr->m_list[target_idx];
375 if (!monster_is_valid(t_ptr)) continue;
377 /* Monster must be 'an enemy' */
378 if ((m_idx == target_idx) || !are_enemies(m_ptr, t_ptr)) continue;
380 /* Monster must be projectable */
381 if (!projectable(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
389 if (!success) return FALSE;
393 /* OK -- we've got a target */
397 /* Forget old counter attack target */
400 /* Remove unimplemented spells */
401 f6 &= ~(RF6_WORLD | RF6_TRAPS | RF6_FORGET);
403 if (f4 & RF4_BR_LITE)
405 if (!los(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx))
406 f4 &= ~(RF4_BR_LITE);
409 /* Remove unimplemented special moves */
410 if (f6 & RF6_SPECIAL)
412 if ((m_ptr->r_idx != MON_ROLENTO) && (r_ptr->d_char != 'B'))
413 f6 &= ~(RF6_SPECIAL);
416 if (f6 & RF6_DARKNESS)
418 bool vs_ninja = (p_ptr->pclass == CLASS_NINJA) && !is_hostile(t_ptr);
421 !(r_ptr->flags3 & (RF3_UNDEAD | RF3_HURT_LITE)) &&
422 !(r_ptr->flags7 & RF7_DARK_MASK))
423 can_use_lite_area = TRUE;
425 if (!(r_ptr->flags2 & RF2_STUPID))
427 if (d_info[p_ptr->dungeon_idx].flags1 & DF1_DARKNESS) f6 &= ~(RF6_DARKNESS);
428 else if (vs_ninja && !can_use_lite_area) f6 &= ~(RF6_DARKNESS);
432 if (in_no_magic_dungeon && !(r_ptr->flags2 & RF2_STUPID))
434 f4 &= (RF4_NOMAGIC_MASK);
435 f5 &= (RF5_NOMAGIC_MASK);
436 f6 &= (RF6_NOMAGIC_MASK);
439 if (p_ptr->inside_arena || p_ptr->inside_battle)
441 f4 &= ~(RF4_SUMMON_MASK);
442 f5 &= ~(RF5_SUMMON_MASK);
443 f6 &= ~(RF6_SUMMON_MASK | RF6_TELE_LEVEL);
445 if (m_ptr->r_idx == MON_ROLENTO) f6 &= ~(RF6_SPECIAL);
448 if (p_ptr->inside_battle && !one_in_(3))
453 if (m_idx == p_ptr->riding)
455 f4 &= ~(RF4_RIDING_MASK);
456 f5 &= ~(RF5_RIDING_MASK);
457 f6 &= ~(RF6_RIDING_MASK);
463 f6 &= ~(RF6_DARKNESS | RF6_TRAPS);
465 if (!(p_ptr->pet_extra_flags & PF_TELEPORT))
467 f6 &= ~(RF6_BLINK | RF6_TPORT | RF6_TELE_TO | RF6_TELE_AWAY | RF6_TELE_LEVEL);
470 if (!(p_ptr->pet_extra_flags & PF_ATTACK_SPELL))
472 f4 &= ~(RF4_ATTACK_MASK);
473 f5 &= ~(RF5_ATTACK_MASK);
474 f6 &= ~(RF6_ATTACK_MASK);
477 if (!(p_ptr->pet_extra_flags & PF_SUMMON_SPELL))
479 f4 &= ~(RF4_SUMMON_MASK);
480 f5 &= ~(RF5_SUMMON_MASK);
481 f6 &= ~(RF6_SUMMON_MASK);
484 /* Prevent collateral damage */
485 if (!(p_ptr->pet_extra_flags & PF_BALL_SPELL) && (m_idx != p_ptr->riding))
487 if ((f4 & (RF4_BALL_MASK & ~(RF4_ROCKET))) ||
488 (f5 & RF5_BALL_MASK) ||
489 (f6 & RF6_BALL_MASK))
494 get_project_point(m_ptr->fy, m_ptr->fx, &real_y, &real_x, 0L);
496 if (projectable(real_y, real_x, p_ptr->y, p_ptr->x))
498 int dist = distance(real_y, real_x, p_ptr->y, p_ptr->x);
502 f4 &= ~(RF4_BALL_MASK & ~(RF4_ROCKET));
503 f5 &= ~(RF5_BALL_MASK);
504 f6 &= ~(RF6_BALL_MASK);
508 f4 &= ~(RF4_BIG_BALL_MASK);
509 f5 &= ~(RF5_BIG_BALL_MASK);
510 f6 &= ~(RF6_BIG_BALL_MASK);
513 else if (f5 & RF5_BA_LITE)
515 if ((distance(real_y, real_x, p_ptr->y, p_ptr->x) <= 4) && los(real_y, real_x, p_ptr->y, p_ptr->x))
516 f5 &= ~(RF5_BA_LITE);
525 get_project_point(m_ptr->fy, m_ptr->fx, &real_y, &real_x, PROJECT_STOP);
526 if (projectable(real_y, real_x, p_ptr->y, p_ptr->x) && (distance(real_y, real_x, p_ptr->y, p_ptr->x) <= 2))
530 if (((f4 & RF4_BEAM_MASK) || (f5 & RF5_BEAM_MASK) || (f6 & RF6_BEAM_MASK)) &&
531 !direct_beam(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx, m_ptr))
533 f4 &= ~(RF4_BEAM_MASK);
534 f5 &= ~(RF5_BEAM_MASK);
535 f6 &= ~(RF6_BEAM_MASK);
538 if ((f4 & RF4_BREATH_MASK) || (f5 & RF5_BREATH_MASK) || (f6 & RF6_BREATH_MASK))
540 /* Expected breath radius */
541 POSITION rad = (r_ptr->flags2 & RF2_POWERFUL) ? 3 : 2;
543 if (!breath_direct(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx, rad, 0, TRUE))
545 f4 &= ~(RF4_BREATH_MASK);
546 f5 &= ~(RF5_BREATH_MASK);
547 f6 &= ~(RF6_BREATH_MASK);
549 else if ((f4 & RF4_BR_LITE) &&
550 !breath_direct(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx, rad, GF_LITE, TRUE))
552 f4 &= ~(RF4_BR_LITE);
554 else if ((f4 & RF4_BR_DISI) &&
555 !breath_direct(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx, rad, GF_DISINTEGRATE, TRUE))
557 f4 &= ~(RF4_BR_DISI);
562 /* Special moves restriction */
563 if (f6 & RF6_SPECIAL)
565 if (m_ptr->r_idx == MON_ROLENTO)
567 if ((p_ptr->pet_extra_flags & (PF_ATTACK_SPELL | PF_SUMMON_SPELL)) != (PF_ATTACK_SPELL | PF_SUMMON_SPELL))
568 f6 &= ~(RF6_SPECIAL);
570 else if (r_ptr->d_char == 'B')
572 if ((p_ptr->pet_extra_flags & (PF_ATTACK_SPELL | PF_TELEPORT)) != (PF_ATTACK_SPELL | PF_TELEPORT))
573 f6 &= ~(RF6_SPECIAL);
575 else f6 &= ~(RF6_SPECIAL);
579 /* Remove some spells if necessary */
581 if (!(r_ptr->flags2 & RF2_STUPID))
583 /* Check for a clean bolt shot */
584 if (((f4 & RF4_BOLT_MASK) ||
585 (f5 & RF5_BOLT_MASK) ||
586 (f6 & RF6_BOLT_MASK)) &&
587 !clean_shot(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx, pet))
589 f4 &= ~(RF4_BOLT_MASK);
590 f5 &= ~(RF5_BOLT_MASK);
591 f6 &= ~(RF6_BOLT_MASK);
594 /* Check for a possible summon */
595 if (((f4 & RF4_SUMMON_MASK) ||
596 (f5 & RF5_SUMMON_MASK) ||
597 (f6 & RF6_SUMMON_MASK)) &&
598 !(summon_possible(t_ptr->fy, t_ptr->fx)))
600 /* Remove summoning spells */
601 f4 &= ~(RF4_SUMMON_MASK);
602 f5 &= ~(RF5_SUMMON_MASK);
603 f6 &= ~(RF6_SUMMON_MASK);
607 if ((f4 & RF4_DISPEL) && !dispel_check_monster(m_idx, target_idx))
609 /* Remove dispel spell */
613 /* Check for a possible raise dead */
614 if ((f6 & RF6_RAISE_DEAD) && !raise_possible(m_ptr))
616 /* Remove raise dead spell */
617 f6 &= ~(RF6_RAISE_DEAD);
620 /* Special moves restriction */
621 if (f6 & RF6_SPECIAL)
623 if ((m_ptr->r_idx == MON_ROLENTO) && !summon_possible(t_ptr->fy, t_ptr->fx))
625 f6 &= ~(RF6_SPECIAL);
630 if (r_ptr->flags2 & RF2_SMART)
632 /* Hack -- allow "desperate" spells */
633 if ((m_ptr->hp < m_ptr->maxhp / 10) &&
634 (randint0(100) < 50))
636 /* Require intelligent spells */
637 f4 &= (RF4_INT_MASK);
638 f5 &= (RF5_INT_MASK);
639 f6 &= (RF6_INT_MASK);
642 /* Hack -- decline "teleport level" in some case */
643 if ((f6 & RF6_TELE_LEVEL) && TELE_LEVEL_IS_INEFF((target_idx == p_ptr->riding) ? 0 : target_idx))
645 f6 &= ~(RF6_TELE_LEVEL);
650 if (!f4 && !f5 && !f6) return FALSE;
652 /* Extract the "inate" spells */
653 for (k = 0; k < 32; k++)
655 if (f4 & (1L << k)) spell[num++] = k + RF4_SPELL_START;
658 /* Extract the "normal" spells */
659 for (k = 0; k < 32; k++)
661 if (f5 & (1L << k)) spell[num++] = k + RF5_SPELL_START;
664 /* Extract the "bizarre" spells */
665 for (k = 0; k < 32; k++)
667 if (f6 & (1L << k)) spell[num++] = k + RF6_SPELL_START;
671 if (!num) return (FALSE);
673 /* Stop if player is dead or gone */
674 if (!p_ptr->playing || p_ptr->is_dead) return (FALSE);
676 /* Handle "leaving" */
677 if (p_ptr->leaving) return (FALSE);
679 /* Get the monster name (or "it") */
680 monster_desc(m_name, m_ptr, 0x00);
683 /* Get the monster possessive ("his"/"her"/"its") */
684 monster_desc(m_poss, m_ptr, MD_PRON_VISIBLE | MD_POSSESSIVE);
687 /* Get the target's name (or "it") */
688 monster_desc(t_name, t_ptr, 0x00);
690 /* Choose a spell to cast */
691 thrown_spell = spell[randint0(num)];
693 if (p_ptr->riding && (m_idx == p_ptr->riding)) disturb(TRUE, TRUE);
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))))
699 if (see_m) msg_format(_("%^sは呪文を唱えようとしたが失敗した。",
700 "%^s tries to cast a spell, but fails."), m_name);
705 /* Hex: Anti Magic Barrier */
706 if (!spell_is_inate(thrown_spell) && magic_barrier(m_idx))
708 if (see_m) msg_format(_("反魔法バリアが%^sの呪文をかき消した。",
709 "Anti magic barrier cancels the spell which %^s casts."), m_name);
713 can_remember = is_original_ap_and_seen(m_ptr);
715 dam = monspell_to_monster(thrown_spell, y, x, m_idx, target_idx);
716 if (dam < 0) return FALSE;
718 if (m_ptr->ml && maneable && !current_world_ptr->timewalk_m_idx && !p_ptr->blind && (p_ptr->pclass == CLASS_IMITATOR))
720 if (thrown_spell != 167) /* Not RF6_SPECIAL */
722 if (p_ptr->mane_num == MAX_MANE)
725 for (i = 0; i < p_ptr->mane_num - 1; i++)
727 p_ptr->mane_spell[i] = p_ptr->mane_spell[i+1];
728 p_ptr->mane_dam[i] = p_ptr->mane_dam[i+1];
731 p_ptr->mane_spell[p_ptr->mane_num] = thrown_spell - RF4_SPELL_START;
732 p_ptr->mane_dam[p_ptr->mane_num] = dam;
734 p_ptr->new_mane = TRUE;
736 p_ptr->redraw |= (PR_IMITATION);
740 /* Remember what the monster did, if we saw it */
744 if (thrown_spell < RF4_SPELL_START + RF4_SPELL_SIZE)
746 r_ptr->r_flags4 |= (1L << (thrown_spell - RF4_SPELL_START));
747 if (r_ptr->r_cast_spell < MAX_UCHAR) r_ptr->r_cast_spell++;
751 else if (thrown_spell < RF5_SPELL_START + RF5_SPELL_SIZE)
753 r_ptr->r_flags5 |= (1L << (thrown_spell - RF5_SPELL_START));
754 if (r_ptr->r_cast_spell < MAX_UCHAR) r_ptr->r_cast_spell++;
758 else if (thrown_spell < RF6_SPELL_START + RF6_SPELL_SIZE)
760 r_ptr->r_flags6 |= (1L << (thrown_spell - RF6_SPELL_START));
761 if (r_ptr->r_cast_spell < MAX_UCHAR) r_ptr->r_cast_spell++;
765 /* Always take note of monsters that kill you */
766 if (p_ptr->is_dead && (r_ptr->r_deaths < MAX_SHORT) && !p_ptr->inside_arena)
768 r_ptr->r_deaths++; /* Ignore appearance difference */
771 /* A spell was cast */