7 #include "spell-kind/spells-teleport.h"
8 #include "avatar/avatar.h"
9 #include "core/asking-player.h"
10 #include "core/speed-table.h"
11 #include "dungeon/quest.h"
12 #include "effect/attribute-types.h"
13 #include "effect/effect-characteristics.h"
14 #include "floor/cave.h"
15 #include "floor/geometry.h"
16 #include "floor/line-of-sight.h"
17 #include "grid/grid.h"
18 #include "inventory/inventory-slot-types.h"
19 #include "main/sound-definitions-table.h"
20 #include "main/sound-of-music.h"
21 #include "monster-floor/monster-move.h"
22 #include "monster-race/monster-race.h"
23 #include "monster-race/race-brightness-mask.h"
24 #include "monster-race/race-flags-resistance.h"
25 #include "monster-race/race-flags7.h"
26 #include "monster/monster-info.h"
27 #include "monster/monster-status-setter.h"
28 #include "monster/monster-status.h"
29 #include "monster/monster-update.h"
30 #include "mutation/mutation-flag-types.h"
31 #include "object-enchant/tr-types.h"
32 #include "player-base/player-class.h"
33 #include "player/player-move.h"
34 #include "player/player-status.h"
35 #include "spell-kind/spells-launcher.h"
36 #include "system/angband-system.h"
37 #include "system/floor-type-definition.h"
38 #include "system/grid-type-definition.h"
39 #include "system/item-entity.h"
40 #include "system/monster-entity.h"
41 #include "system/monster-race-info.h"
42 #include "system/player-type-definition.h"
43 #include "system/redrawing-flags-updater.h"
44 #include "target/grid-selector.h"
45 #include "target/target-checker.h"
46 #include "util/bit-flags-calculator.h"
47 #include "view/display-messages.h"
48 #include "world/world.h"
52 * @brief モンスターとの位置交換処理 / Switch position with a monster.
53 * @param player_ptr プレイヤーへの参照ポインタ
54 * @param dir 方向(5ならばグローバル変数 target_col/target_row の座標を目標にする)
55 * @return 作用が実際にあった場合TRUEを返す
57 bool teleport_swap(PlayerType *player_ptr, DIRECTION dir)
60 if ((dir == 5) && target_okay(player_ptr)) {
64 tx = player_ptr->x + ddx[dir];
65 ty = player_ptr->y + ddy[dir];
68 if (player_ptr->anti_tele) {
69 msg_print(_("不思議な力がテレポートを防いだ!", "A mysterious force prevents you from teleporting!"));
74 g_ptr = &player_ptr->current_floor_ptr->grid_array[ty][tx];
75 if (!g_ptr->m_idx || (g_ptr->m_idx == player_ptr->riding)) {
76 msg_print(_("それとは場所を交換できません。", "You can't trade places with that!"));
80 if ((g_ptr->is_icky()) || (distance(ty, tx, player_ptr->y, player_ptr->x) > player_ptr->lev * 3 / 2 + 10)) {
81 msg_print(_("失敗した。", "Failed to swap."));
86 MonsterRaceInfo *r_ptr;
87 m_ptr = &player_ptr->current_floor_ptr->m_list[g_ptr->m_idx];
88 r_ptr = &m_ptr->get_monrace();
90 (void)set_monster_csleep(player_ptr, g_ptr->m_idx, 0);
92 if (r_ptr->resistance_flags.has(MonsterResistanceType::RESIST_TELEPORT)) {
93 msg_print(_("テレポートを邪魔された!", "Your teleportation is blocked!"));
94 if (is_original_ap_and_seen(player_ptr, m_ptr)) {
95 r_ptr->r_resistance_flags.set(MonsterResistanceType::RESIST_TELEPORT);
100 sound(SOUND_TELEPORT);
101 (void)move_player_effect(player_ptr, ty, tx, MPE_FORGET_FLOW | MPE_HANDLE_STUFF | MPE_DONT_PICKUP);
106 * @brief モンスター用テレポート処理
107 * @param player_ptr プレイヤーへの参照ポインタ
108 * @param dir 方向(5ならばグローバル変数 target_col/target_row の座標を目標にする)
109 * @param distance 移動距離
110 * @return 作用が実際にあった場合TRUEを返す
112 bool teleport_monster(PlayerType *player_ptr, DIRECTION dir, int distance)
114 BIT_FLAGS flg = PROJECT_BEAM | PROJECT_KILL;
115 return project_hook(player_ptr, AttributeType::AWAY_ALL, dir, distance, flg);
119 * @brief モンスターのテレポートアウェイ処理 /
120 * Teleport a monster, normally up to "dis" grids away.
121 * @param player_ptr プレイヤーへの参照ポインタ
122 * @param m_idx モンスターID
125 * @return テレポートが実際に行われたらtrue
127 * Attempt to move the monster at least "dis/2" grids away.
128 * But allow variation to prevent infinite loops.
130 bool teleport_away(PlayerType *player_ptr, MONSTER_IDX m_idx, POSITION dis, teleport_flags mode)
132 auto *m_ptr = &player_ptr->current_floor_ptr->m_list[m_idx];
133 if (!m_ptr->is_valid()) {
137 if ((mode & TELEPORT_DEC_VALOUR) && (((player_ptr->chp * 10) / player_ptr->mhp) > 5) && (4 + randint1(5) < ((player_ptr->chp * 10) / player_ptr->mhp))) {
138 chg_virtue(player_ptr, Virtue::VALOUR, -1);
141 POSITION oy = m_ptr->fy;
142 POSITION ox = m_ptr->fx;
143 POSITION min = dis / 2;
145 POSITION ny = 0, nx = 0;
153 for (int i = 0; i < 500; i++) {
155 ny = rand_spread(oy, dis);
156 nx = rand_spread(ox, dis);
157 POSITION d = distance(oy, ox, ny, nx);
158 if ((d >= min) && (d <= dis)) {
163 if (!in_bounds(player_ptr->current_floor_ptr, ny, nx)) {
166 if (!cave_monster_teleportable_bold(player_ptr, m_idx, ny, nx, mode)) {
169 if (!(player_ptr->current_floor_ptr->is_in_quest() || player_ptr->current_floor_ptr->inside_arena)) {
170 if (player_ptr->current_floor_ptr->grid_array[ny][nx].is_icky()) {
181 const int MAX_TELEPORT_TRIES = 100;
182 if (tries > MAX_TELEPORT_TRIES) {
187 sound(SOUND_TPOTHER);
188 player_ptr->current_floor_ptr->grid_array[oy][ox].m_idx = 0;
189 player_ptr->current_floor_ptr->grid_array[ny][nx].m_idx = m_idx;
195 update_monster(player_ptr, m_idx, true);
196 lite_spot(player_ptr, oy, ox);
197 lite_spot(player_ptr, ny, nx);
199 if (m_ptr->get_monrace().brightness_flags.has_any_of(ld_mask)) {
200 RedrawingFlagsUpdater::get_instance().set_flag(StatusRecalculatingFlag::MONSTER_LITE);
207 * @brief モンスターを指定された座標付近にテレポートする /
208 * Teleport monster next to a grid near the given location
209 * @param player_ptr プレイヤーへの参照ポインタ
210 * @param m_idx モンスターID
213 * @param power テレポート成功確率
216 void teleport_monster_to(PlayerType *player_ptr, MONSTER_IDX m_idx, POSITION ty, POSITION tx, int power, teleport_flags mode)
218 auto *m_ptr = &player_ptr->current_floor_ptr->m_list[m_idx];
219 if (!MonsterRace(m_ptr->r_idx).is_valid()) {
222 if (randint1(100) > power) {
226 POSITION ny = m_ptr->fy;
227 POSITION nx = m_ptr->fx;
228 POSITION oy = m_ptr->fy;
229 POSITION ox = m_ptr->fx;
235 while (look && --attempts) {
240 for (int i = 0; i < 500; i++) {
242 ny = rand_spread(ty, dis);
243 nx = rand_spread(tx, dis);
244 int d = distance(ty, tx, ny, nx);
245 if ((d >= min) && (d <= dis)) {
250 if (!in_bounds(player_ptr->current_floor_ptr, ny, nx)) {
253 if (!cave_monster_teleportable_bold(player_ptr, m_idx, ny, nx, mode)) {
269 sound(SOUND_TPOTHER);
270 player_ptr->current_floor_ptr->grid_array[oy][ox].m_idx = 0;
271 player_ptr->current_floor_ptr->grid_array[ny][nx].m_idx = m_idx;
276 update_monster(player_ptr, m_idx, true);
277 lite_spot(player_ptr, oy, ox);
278 lite_spot(player_ptr, ny, nx);
280 if (m_ptr->get_monrace().brightness_flags.has_any_of(ld_mask)) {
281 RedrawingFlagsUpdater::get_instance().set_flag(StatusRecalculatingFlag::MONSTER_LITE);
286 * @brief プレイヤーのテレポート先選定と移動処理 /
287 * Teleport the player to a location up to "dis" grids away.
288 * @param player_ptr プレイヤーへの参照ポインタ
290 * @param is_quantum_effect 量子的効果 (反テレポ無効)によるテレポートアウェイならばTRUE
292 * @return 実際にテレポート処理が行われたらtrue
295 * If no such spaces are readily available, the distance may increase.
296 * Try very hard to move the player at least a quarter that distance.
298 * There was a nasty tendency for a long time; which was causing the
299 * player to "bounce" between two or three different spots because
300 * these are the only spots that are "far enough" way to satisfy the
303 * But this tendency is now removed; in the new algorithm, a list of
304 * candidates is selected first, which includes at least 50% of all
305 * floor grids within the distance, and any single grid in this list
306 * of candidates has equal possibility to be choosen as a destination.
309 bool teleport_player_aux(PlayerType *player_ptr, POSITION dis, bool is_quantum_effect, teleport_flags mode)
311 if (player_ptr->wild_mode) {
315 if (!is_quantum_effect && player_ptr->anti_tele && !(mode & TELEPORT_NONMAGICAL)) {
316 msg_print(_("不思議な力がテレポートを防いだ!", "A mysterious force prevents you from teleporting!"));
320 constexpr auto max_distance = 200;
321 std::array<int, max_distance + 1> candidates_at{};
322 if (dis > max_distance) {
326 const auto &floor_ref = *player_ptr->current_floor_ptr;
327 const auto left = std::max(1, player_ptr->x - dis);
328 const auto right = std::min(floor_ref.width - 2, player_ptr->x + dis);
329 const auto top = std::max(1, player_ptr->y - dis);
330 const auto bottom = std::min(floor_ref.height - 2, player_ptr->y + dis);
331 auto total_candidates = 0;
332 for (auto y = top; y <= bottom; y++) {
333 for (auto x = left; x <= right; x++) {
334 if (!cave_player_teleportable_bold(player_ptr, y, x, mode)) {
338 int d = distance(player_ptr->y, player_ptr->x, y, x);
348 if (0 == total_candidates) {
354 for (cur_candidates = 0; min >= 0; min--) {
355 cur_candidates += candidates_at[min];
356 if (cur_candidates && (cur_candidates >= total_candidates / 2)) {
361 auto pick = randint1(cur_candidates);
363 /* Search again the choosen location */
366 for (; y <= bottom; y++) {
367 for (x = left; x <= right; x++) {
368 if (!cave_player_teleportable_bold(player_ptr, y, x, mode)) {
372 int d = distance(player_ptr->y, player_ptr->x, y, x);
391 const Pos2D pos(y, x);
392 if (player_ptr->is_located_at(pos)) {
396 sound(SOUND_TELEPORT);
398 if (is_echizen(player_ptr)) {
399 msg_format("『こっちだぁ、%s』", player_ptr->name);
402 (void)move_player_effect(player_ptr, pos.y, pos.x, MPE_FORGET_FLOW | MPE_HANDLE_STUFF | MPE_DONT_PICKUP);
407 * @brief プレイヤーのテレポート処理メインルーチン
408 * @param player_ptr プレイヤーへの参照ポインタ
412 void teleport_player(PlayerType *player_ptr, POSITION dis, BIT_FLAGS mode)
414 const POSITION oy = player_ptr->y;
415 const POSITION ox = player_ptr->x;
417 if (!teleport_player_aux(player_ptr, dis, false, i2enum<teleport_flags>(mode))) {
421 /* Monsters with teleport ability may follow the player */
422 for (POSITION xx = -1; xx < 2; xx++) {
423 for (POSITION yy = -1; yy < 2; yy++) {
424 MONSTER_IDX tmp_m_idx = player_ptr->current_floor_ptr->grid_array[oy + yy][ox + xx].m_idx;
425 if (tmp_m_idx && (player_ptr->riding != tmp_m_idx)) {
426 auto *m_ptr = &player_ptr->current_floor_ptr->m_list[tmp_m_idx];
427 auto *r_ptr = &m_ptr->get_monrace();
429 bool can_follow = r_ptr->ability_flags.has(MonsterAbilityType::TPORT);
430 can_follow &= r_ptr->resistance_flags.has_not(MonsterResistanceType::RESIST_TELEPORT);
431 can_follow &= !m_ptr->is_asleep();
433 teleport_monster_to(player_ptr, tmp_m_idx, player_ptr->y, player_ptr->x, r_ptr->level, TELEPORT_SPONTANEOUS);
441 * @brief プレイヤーのテレポートアウェイ処理 /
442 * @param m_idx アウェイを試みたモンスターID
443 * @param player_ptr プレイヤーへの参照ポインタ
445 * @param is_quantum_effect 量子的効果によるテレポートアウェイならばTRUE
447 void teleport_player_away(MONSTER_IDX m_idx, PlayerType *player_ptr, POSITION dis, bool is_quantum_effect)
449 if (AngbandSystem::get_instance().is_phase_out()) {
453 const POSITION oy = player_ptr->y;
454 const POSITION ox = player_ptr->x;
456 if (!teleport_player_aux(player_ptr, dis, is_quantum_effect, TELEPORT_PASSIVE)) {
460 /* Monsters with teleport ability may follow the player */
461 for (POSITION xx = -1; xx < 2; xx++) {
462 for (POSITION yy = -1; yy < 2; yy++) {
463 MONSTER_IDX tmp_m_idx = player_ptr->current_floor_ptr->grid_array[oy + yy][ox + xx].m_idx;
464 bool is_teleportable = tmp_m_idx > 0;
465 is_teleportable &= player_ptr->riding != tmp_m_idx;
466 is_teleportable &= m_idx != tmp_m_idx;
467 if (!is_teleportable) {
471 auto *m_ptr = &player_ptr->current_floor_ptr->m_list[tmp_m_idx];
472 auto *r_ptr = &m_ptr->get_monrace();
474 bool can_follow = r_ptr->ability_flags.has(MonsterAbilityType::TPORT);
475 can_follow &= r_ptr->resistance_flags.has_not(MonsterResistanceType::RESIST_TELEPORT);
476 can_follow &= !m_ptr->is_asleep();
478 teleport_monster_to(player_ptr, tmp_m_idx, player_ptr->y, player_ptr->x, r_ptr->level, TELEPORT_SPONTANEOUS);
485 * @brief プレイヤーを指定位置近辺にテレポートさせる
486 * Teleport player to a grid near the given location
487 * @param player_ptr プレイヤーへの参照ポインタ
490 * @param mode オプションフラグ
493 * This function is slightly obsessive about correctness.
494 * This function allows teleporting into vaults (!)
497 void teleport_player_to(PlayerType *player_ptr, POSITION ny, POSITION nx, teleport_flags mode)
499 if (player_ptr->anti_tele && !(mode & TELEPORT_NONMAGICAL)) {
500 msg_print(_("不思議な力がテレポートを防いだ!", "A mysterious force prevents you from teleporting!"));
504 /* Find a usable location */
506 POSITION dis = 0, ctr = 0;
509 y = (POSITION)rand_spread(ny, dis);
510 x = (POSITION)rand_spread(nx, dis);
511 if (in_bounds(player_ptr->current_floor_ptr, y, x)) {
516 bool is_anywhere = w_ptr->wizard;
517 is_anywhere &= (mode & TELEPORT_PASSIVE) == 0;
518 is_anywhere &= (player_ptr->current_floor_ptr->grid_array[y][x].m_idx > 0) || player_ptr->current_floor_ptr->grid_array[y][x].m_idx == player_ptr->riding;
523 if (cave_player_teleportable_bold(player_ptr, y, x, mode)) {
527 if (++ctr > (4 * dis * dis + 4 * dis + 1)) {
533 sound(SOUND_TELEPORT);
534 (void)move_player_effect(player_ptr, y, x, MPE_FORGET_FLOW | MPE_HANDLE_STUFF | MPE_DONT_PICKUP);
537 void teleport_away_followable(PlayerType *player_ptr, MONSTER_IDX m_idx)
539 auto *m_ptr = &player_ptr->current_floor_ptr->m_list[m_idx];
540 POSITION oldfy = m_ptr->fy;
541 POSITION oldfx = m_ptr->fx;
542 bool old_ml = m_ptr->ml;
543 POSITION old_cdis = m_ptr->cdis;
545 teleport_away(player_ptr, m_idx, MAX_PLAYER_SIGHT * 2 + 5, TELEPORT_SPONTANEOUS);
547 bool is_followable = old_ml;
548 is_followable &= old_cdis <= MAX_PLAYER_SIGHT;
549 is_followable &= w_ptr->timewalk_m_idx == 0;
550 is_followable &= !AngbandSystem::get_instance().is_phase_out();
551 is_followable &= los(player_ptr, player_ptr->y, player_ptr->x, oldfy, oldfx);
552 if (!is_followable) {
557 if (player_ptr->muta.has(PlayerMutationType::VTELEPORT) || PlayerClass(player_ptr).equals(PlayerClassType::IMITATOR)) {
563 for (i = INVEN_MAIN_HAND; i < INVEN_TOTAL; i++) {
564 o_ptr = &player_ptr->inventory_list[i];
565 if (o_ptr->is_valid() && !o_ptr->is_cursed() && o_ptr->get_flags().has(TR_TELEPORT)) {
575 if (!input_check_strict(player_ptr, _("ついていきますか?", "Do you follow it? "), UserCheck::OKAY_CANCEL)) {
580 teleport_player(player_ptr, 200, TELEPORT_PASSIVE);
581 msg_print(_("失敗!", "Failed!"));
583 teleport_player_to(player_ptr, m_ptr->fy, m_ptr->fx, TELEPORT_SPONTANEOUS);
586 player_ptr->energy_need += ENERGY_NEED();
592 * @param player_ptr プレイヤーへの参照ポインタ
593 * @param x テレポート先のX座標
594 * @param y テレポート先のY座標
595 * @return 目標に指定通りテレポートできたならばTRUEを返す
597 bool exe_dimension_door(PlayerType *player_ptr, POSITION x, POSITION y)
599 PLAYER_LEVEL plev = player_ptr->lev;
601 player_ptr->energy_need += (int16_t)((int32_t)(60 - plev) * ENERGY_NEED() / 100L);
602 auto is_successful = cave_player_teleportable_bold(player_ptr, y, x, TELEPORT_SPONTANEOUS);
603 is_successful &= distance(y, x, player_ptr->y, player_ptr->x) <= plev / 2 + 10;
604 is_successful &= !one_in_(plev / 10 + 10);
605 if (!is_successful) {
606 player_ptr->energy_need += (int16_t)((int32_t)(60 - plev) * ENERGY_NEED() / 100L);
607 teleport_player(player_ptr, (plev + 2) * 2, TELEPORT_PASSIVE);
611 teleport_player_to(player_ptr, y, x, TELEPORT_SPONTANEOUS);
616 * @brief 次元の扉処理のメインルーチン /
617 * @param player_ptr プレイヤーへの参照ポインタ
619 * @return ターンを消費した場合TRUEを返す
621 bool dimension_door(PlayerType *player_ptr)
624 if (!tgt_pt(player_ptr, &x, &y)) {
628 if (exe_dimension_door(player_ptr, x, y)) {
632 msg_print(_("精霊界から物質界に戻る時うまくいかなかった!", "You fail to exit the astral plane correctly!"));