7 #include "spell-kind/spells-teleport.h"
8 #include "core/asking-player.h"
9 #include "core/player-update-types.h"
10 #include "core/speed-table.h"
11 #include "effect/effect-characteristics.h"
12 #include "floor/cave.h"
13 #include "floor/floor.h"
14 #include "grid/grid.h"
15 #include "inventory/inventory-slot-types.h"
16 #include "main/sound-definitions-table.h"
17 #include "main/sound-of-music.h"
18 #include "monster-floor/monster-move.h"
19 #include "monster-race/monster-race.h"
20 #include "monster-race/race-flags-ability2.h"
21 #include "monster-race/race-flags-resistance.h"
22 #include "monster-race/race-flags7.h"
23 #include "monster/monster-info.h"
24 #include "monster/monster-status.h"
25 #include "monster/monster-update.h"
26 #include "mutation/mutation-flag-types.h"
27 #include "object-enchant/tr-types.h"
28 #include "object-hook/hook-checker.h"
29 #include "object/object-flags.h"
30 #include "player/avatar.h"
31 #include "player/player-move.h"
32 #include "spell-kind/spells-launcher.h"
33 #include "spell/spell-types.h"
34 #include "system/floor-type-definition.h"
35 #include "target/grid-selector.h"
36 #include "target/targeting.h"
37 #include "util/bit-flags-calculator.h"
38 #include "view/display-messages.h"
39 #include "world/world.h"
42 * @brief モンスターとの位置交換処理 / Switch position with a monster.
43 * @param caster_ptr プレーヤーへの参照ポインタ
44 * @param dir 方向(5ならばグローバル変数 target_col/target_row の座標を目標にする)
45 * @return 作用が実際にあった場合TRUEを返す
47 bool teleport_swap(player_type *caster_ptr, DIRECTION dir)
50 if ((dir == 5) && target_okay(caster_ptr)) {
54 tx = caster_ptr->x + ddx[dir];
55 ty = caster_ptr->y + ddy[dir];
58 if (caster_ptr->anti_tele) {
59 msg_print(_("不思議な力がテレポートを防いだ!", "A mysterious force prevents you from teleporting!"));
64 g_ptr = &caster_ptr->current_floor_ptr->grid_array[ty][tx];
65 if (!g_ptr->m_idx || (g_ptr->m_idx == caster_ptr->riding)) {
66 msg_print(_("それとは場所を交換できません。", "You can't trade places with that!"));
70 if ((g_ptr->info & CAVE_ICKY) || (distance(ty, tx, caster_ptr->y, caster_ptr->x) > caster_ptr->lev * 3 / 2 + 10)) {
71 msg_print(_("失敗した。", "Failed to swap."));
77 m_ptr = &caster_ptr->current_floor_ptr->m_list[g_ptr->m_idx];
78 r_ptr = &r_info[m_ptr->r_idx];
80 (void)set_monster_csleep(caster_ptr, g_ptr->m_idx, 0);
82 if (r_ptr->flagsr & RFR_RES_TELE) {
83 msg_print(_("テレポートを邪魔された!", "Your teleportation is blocked!"));
84 if (is_original_ap_and_seen(caster_ptr, m_ptr))
85 r_ptr->r_flagsr |= RFR_RES_TELE;
89 sound(SOUND_TELEPORT);
90 (void)move_player_effect(caster_ptr, ty, tx, MPE_FORGET_FLOW | MPE_HANDLE_STUFF | MPE_DONT_PICKUP);
95 * @brief モンスター用テレポート処理
96 * @param caster_ptr プレーヤーへの参照ポインタ
97 * @param dir 方向(5ならばグローバル変数 target_col/target_row の座標を目標にする)
98 * @param distance 移動距離
99 * @return 作用が実際にあった場合TRUEを返す
101 bool teleport_monster(player_type *caster_ptr, DIRECTION dir, int distance)
103 BIT_FLAGS flg = PROJECT_BEAM | PROJECT_KILL;
104 return (project_hook(caster_ptr, GF_AWAY_ALL, dir, distance, flg));
108 * @brief モンスターのテレポートアウェイ処理 /
109 * Teleport a monster, normally up to "dis" grids away.
110 * @param caster_ptr プレーヤーへの参照ポインタ
111 * @param m_idx モンスターID
114 * @return テレポートが実際に行われたらtrue
116 * Attempt to move the monster at least "dis/2" grids away.
117 * But allow variation to prevent infinite loops.
119 bool teleport_away(player_type *caster_ptr, MONSTER_IDX m_idx, POSITION dis, teleport_flags mode)
121 monster_type *m_ptr = &caster_ptr->current_floor_ptr->m_list[m_idx];
122 if (!monster_is_valid(m_ptr))
125 if ((mode & TELEPORT_DEC_VALOUR) && (((caster_ptr->chp * 10) / caster_ptr->mhp) > 5) && (4 + randint1(5) < ((caster_ptr->chp * 10) / caster_ptr->mhp))) {
126 chg_virtue(caster_ptr, V_VALOUR, -1);
129 POSITION oy = m_ptr->fy;
130 POSITION ox = m_ptr->fx;
131 POSITION min = dis / 2;
133 POSITION ny = 0, nx = 0;
140 for (int i = 0; i < 500; i++) {
142 ny = rand_spread(oy, dis);
143 nx = rand_spread(ox, dis);
144 POSITION d = distance(oy, ox, ny, nx);
145 if ((d >= min) && (d <= dis))
149 if (!in_bounds(caster_ptr->current_floor_ptr, ny, nx))
151 if (!cave_monster_teleportable_bold(caster_ptr, m_idx, ny, nx, mode))
153 if (!(caster_ptr->current_floor_ptr->inside_quest || caster_ptr->current_floor_ptr->inside_arena))
154 if (caster_ptr->current_floor_ptr->grid_array[ny][nx].info & CAVE_ICKY)
163 const int MAX_TELEPORT_TRIES = 100;
164 if (tries > MAX_TELEPORT_TRIES)
168 sound(SOUND_TPOTHER);
169 caster_ptr->current_floor_ptr->grid_array[oy][ox].m_idx = 0;
170 caster_ptr->current_floor_ptr->grid_array[ny][nx].m_idx = m_idx;
176 update_monster(caster_ptr, m_idx, TRUE);
177 lite_spot(caster_ptr, oy, ox);
178 lite_spot(caster_ptr, ny, nx);
180 if (r_info[m_ptr->r_idx].flags7 & (RF7_LITE_MASK | RF7_DARK_MASK))
181 caster_ptr->update |= (PU_MON_LITE);
187 * @brief モンスターを指定された座標付近にテレポートする /
188 * Teleport monster next to a grid near the given location
189 * @param caster_ptr プレーヤーへの参照ポインタ
190 * @param m_idx モンスターID
193 * @param power テレポート成功確率
197 void teleport_monster_to(player_type *caster_ptr, MONSTER_IDX m_idx, POSITION ty, POSITION tx, int power, teleport_flags mode)
199 monster_type *m_ptr = &caster_ptr->current_floor_ptr->m_list[m_idx];
202 if (randint1(100) > power)
205 POSITION ny = m_ptr->fy;
206 POSITION nx = m_ptr->fx;
207 POSITION oy = m_ptr->fy;
208 POSITION ox = m_ptr->fx;
214 while (look && --attempts) {
218 for (int i = 0; i < 500; i++) {
220 ny = rand_spread(ty, dis);
221 nx = rand_spread(tx, dis);
222 int d = distance(ty, tx, ny, nx);
223 if ((d >= min) && (d <= dis))
227 if (!in_bounds(caster_ptr->current_floor_ptr, ny, nx))
229 if (!cave_monster_teleportable_bold(caster_ptr, m_idx, ny, nx, mode))
243 sound(SOUND_TPOTHER);
244 caster_ptr->current_floor_ptr->grid_array[oy][ox].m_idx = 0;
245 caster_ptr->current_floor_ptr->grid_array[ny][nx].m_idx = m_idx;
250 update_monster(caster_ptr, m_idx, TRUE);
251 lite_spot(caster_ptr, oy, ox);
252 lite_spot(caster_ptr, ny, nx);
254 if (r_info[m_ptr->r_idx].flags7 & (RF7_LITE_MASK | RF7_DARK_MASK))
255 caster_ptr->update |= (PU_MON_LITE);
259 * @brief プレイヤーのテレポート先選定と移動処理 /
260 * Teleport the player to a location up to "dis" grids away.
261 * @param creature_ptr プレーヤーへの参照ポインタ
263 * @param is_quantum_effect 量子的効果 (反テレポ無効)によるテレポートアウェイならばTRUE
265 * @return 実際にテレポート処理が行われたらtrue
268 * If no such spaces are readily available, the distance may increase.
269 * Try very hard to move the player at least a quarter that distance.
271 * There was a nasty tendency for a long time; which was causing the
272 * player to "bounce" between two or three different spots because
273 * these are the only spots that are "far enough" way to satisfy the
276 * But this tendency is now removed; in the new algorithm, a list of
277 * candidates is selected first, which includes at least 50% of all
278 * floor grids within the distance, and any single grid in this list
279 * of candidates has equal possibility to be choosen as a destination.
282 bool teleport_player_aux(player_type *creature_ptr, POSITION dis, bool is_quantum_effect, teleport_flags mode)
284 if (creature_ptr->wild_mode)
286 if (!is_quantum_effect && creature_ptr->anti_tele && !(mode & TELEPORT_NONMAGICAL)) {
287 msg_print(_("不思議な力がテレポートを防いだ!", "A mysterious force prevents you from teleporting!"));
291 int candidates_at[MAX_TELEPORT_DISTANCE + 1];
292 for (int i = 0; i <= MAX_TELEPORT_DISTANCE; i++)
293 candidates_at[i] = 0;
295 if (dis > MAX_TELEPORT_DISTANCE)
296 dis = MAX_TELEPORT_DISTANCE;
298 int left = MAX(1, creature_ptr->x - dis);
299 int right = MIN(creature_ptr->current_floor_ptr->width - 2, creature_ptr->x + dis);
300 int top = MAX(1, creature_ptr->y - dis);
301 int bottom = MIN(creature_ptr->current_floor_ptr->height - 2, creature_ptr->y + dis);
302 int total_candidates = 0;
303 for (POSITION y = top; y <= bottom; y++) {
304 for (POSITION x = left; x <= right; x++) {
305 if (!cave_player_teleportable_bold(creature_ptr, y, x, mode))
308 int d = distance(creature_ptr->y, creature_ptr->x, y, x);
317 if (0 == total_candidates)
322 for (cur_candidates = 0; min >= 0; min--) {
323 cur_candidates += candidates_at[min];
324 if (cur_candidates && (cur_candidates >= total_candidates / 2))
328 int pick = randint1(cur_candidates);
330 /* Search again the choosen location */
332 for (yy = top; yy <= bottom; yy++) {
333 for (xx = left; xx <= right; xx++) {
334 if (!cave_player_teleportable_bold(creature_ptr, yy, xx, mode))
337 int d = distance(creature_ptr->y, creature_ptr->x, yy, xx);
352 if (player_bold(creature_ptr, yy, xx))
355 sound(SOUND_TELEPORT);
357 if (is_echizen(creature_ptr))
358 msg_format("『こっちだぁ、%s』", creature_ptr->name);
360 (void)move_player_effect(creature_ptr, yy, xx, MPE_FORGET_FLOW | MPE_HANDLE_STUFF | MPE_DONT_PICKUP);
365 * @brief プレイヤーのテレポート処理メインルーチン
366 * @param creature_ptr プレーヤーへの参照ポインタ
371 void teleport_player(player_type *creature_ptr, POSITION dis, BIT_FLAGS mode)
373 if (!teleport_player_aux(creature_ptr, dis, FALSE, mode))
376 /* Monsters with teleport ability may follow the player */
377 POSITION oy = creature_ptr->y;
378 POSITION ox = creature_ptr->x;
379 for (POSITION xx = -1; xx < 2; xx++) {
380 for (POSITION yy = -1; yy < 2; yy++) {
381 MONSTER_IDX tmp_m_idx = creature_ptr->current_floor_ptr->grid_array[oy + yy][ox + xx].m_idx;
382 if (tmp_m_idx && (creature_ptr->riding != tmp_m_idx)) {
386 monster_type *m_ptr = &creature_ptr->current_floor_ptr->m_list[tmp_m_idx];
387 monster_race *r_ptr = &r_info[m_ptr->r_idx];
389 bool is_resistible = (r_ptr->a_ability_flags2 & RF6_TPORT) != 0;
390 is_resistible &= (r_ptr->flagsr & RFR_RES_TELE) == 0;
391 is_resistible &= monster_csleep_remaining(m_ptr) == 0;
393 teleport_monster_to(creature_ptr, tmp_m_idx, creature_ptr->y, creature_ptr->x, r_ptr->level, TELEPORT_SPONTANEOUS);
400 * @brief プレイヤーのテレポートアウェイ処理 /
401 * @param m_idx アウェイを試みたモンスターID
402 * @param target_ptr プレーヤーへの参照ポインタ
404 * @param is_quantum_effect 量子的効果によるテレポートアウェイならばTRUE
407 void teleport_player_away(MONSTER_IDX m_idx, player_type *target_ptr, POSITION dis, bool is_quantum_effect)
409 if (!teleport_player_aux(target_ptr, dis, TELEPORT_PASSIVE, is_quantum_effect))
412 /* Monsters with teleport ability may follow the player */
413 POSITION oy = target_ptr->y;
414 POSITION ox = target_ptr->x;
415 for (POSITION xx = -1; xx < 2; xx++) {
416 for (POSITION yy = -1; yy < 2; yy++) {
417 MONSTER_IDX tmp_m_idx = target_ptr->current_floor_ptr->grid_array[oy + yy][ox + xx].m_idx;
418 bool is_teleportable = tmp_m_idx > 0;
419 is_teleportable &= target_ptr->riding != tmp_m_idx;
420 is_teleportable &= m_idx != tmp_m_idx;
421 if (!is_teleportable) {
425 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[tmp_m_idx];
426 monster_race *r_ptr = &r_info[m_ptr->r_idx];
428 bool is_resistible = (r_ptr->a_ability_flags2 & RF6_TPORT) != 0;
429 is_resistible &= (r_ptr->flagsr & RFR_RES_TELE) == 0;
430 is_resistible &= monster_csleep_remaining(m_ptr) == 0;
432 teleport_monster_to(target_ptr, tmp_m_idx, target_ptr->y, target_ptr->x, r_ptr->level, TELEPORT_SPONTANEOUS);
439 * @brief プレイヤーを指定位置近辺にテレポートさせる
440 * Teleport player to a grid near the given location
441 * @param creature_ptr プレーヤーへの参照ポインタ
444 * @param mode オプションフラグ
448 * This function is slightly obsessive about correctness.
449 * This function allows teleporting into vaults (!)
452 void teleport_player_to(player_type *creature_ptr, POSITION ny, POSITION nx, teleport_flags mode)
454 if (creature_ptr->anti_tele && !(mode & TELEPORT_NONMAGICAL)) {
455 msg_print(_("不思議な力がテレポートを防いだ!", "A mysterious force prevents you from teleporting!"));
459 /* Find a usable location */
461 POSITION dis = 0, ctr = 0;
464 y = (POSITION)rand_spread(ny, dis);
465 x = (POSITION)rand_spread(nx, dis);
466 if (in_bounds(creature_ptr->current_floor_ptr, y, x))
470 bool is_anywhere = current_world_ptr->wizard;
471 is_anywhere &= (mode & TELEPORT_PASSIVE) == 0;
473 &= (creature_ptr->current_floor_ptr->grid_array[y][x].m_idx > 0) || creature_ptr->current_floor_ptr->grid_array[y][x].m_idx == creature_ptr->riding;
477 if (cave_player_teleportable_bold(creature_ptr, y, x, mode))
480 if (++ctr > (4 * dis * dis + 4 * dis + 1)) {
486 sound(SOUND_TELEPORT);
487 (void)move_player_effect(creature_ptr, y, x, MPE_FORGET_FLOW | MPE_HANDLE_STUFF | MPE_DONT_PICKUP);
490 void teleport_away_followable(player_type *tracer_ptr, MONSTER_IDX m_idx)
492 monster_type *m_ptr = &tracer_ptr->current_floor_ptr->m_list[m_idx];
493 POSITION oldfy = m_ptr->fy;
494 POSITION oldfx = m_ptr->fx;
495 bool old_ml = m_ptr->ml;
496 POSITION old_cdis = m_ptr->cdis;
498 teleport_away(tracer_ptr, m_idx, MAX_SIGHT * 2 + 5, TELEPORT_SPONTANEOUS);
500 bool is_followable = old_ml;
501 is_followable &= old_cdis <= MAX_SIGHT;
502 is_followable &= current_world_ptr->timewalk_m_idx == 0;
503 is_followable &= !tracer_ptr->phase_out;
504 is_followable &= los(tracer_ptr, tracer_ptr->y, tracer_ptr->x, oldfy, oldfx);
509 if ((tracer_ptr->muta1 & MUT1_VTELEPORT) || (tracer_ptr->pclass == CLASS_IMITATOR))
512 BIT_FLAGS flgs[TR_FLAG_SIZE];
516 for (i = INVEN_RARM; i < INVEN_TOTAL; i++) {
517 o_ptr = &tracer_ptr->inventory_list[i];
518 if (o_ptr->k_idx && !object_is_cursed(o_ptr)) {
519 object_flags(tracer_ptr, o_ptr, flgs);
520 if (have_flag(flgs, TR_TELEPORT)) {
530 if (!get_check_strict(tracer_ptr, _("ついていきますか?", "Do you follow it? "), CHECK_OKAY_CANCEL))
534 teleport_player(tracer_ptr, 200, TELEPORT_PASSIVE);
535 msg_print(_("失敗!", "Failed!"));
537 teleport_player_to(tracer_ptr, m_ptr->fy, m_ptr->fx, TELEPORT_SPONTANEOUS);
540 tracer_ptr->energy_need += ENERGY_NEED();
546 * @param caster_ptr プレーヤーへの参照ポインタ
547 * @param x テレポート先のX座標
548 * @param y テレポート先のY座標
549 * @return 目標に指定通りテレポートできたならばTRUEを返す
551 bool exe_dimension_door(player_type *caster_ptr, POSITION x, POSITION y)
553 PLAYER_LEVEL plev = caster_ptr->lev;
555 caster_ptr->energy_need += (s16b)((s32b)(60 - plev) * ENERGY_NEED() / 100L);
557 if (!cave_player_teleportable_bold(caster_ptr, y, x, TELEPORT_SPONTANEOUS) || (distance(y, x, caster_ptr->y, caster_ptr->x) > plev / 2 + 10)
558 || (!randint0(plev / 10 + 10))) {
559 caster_ptr->energy_need += (s16b)((s32b)(60 - plev) * ENERGY_NEED() / 100L);
560 teleport_player(caster_ptr, (plev + 2) * 2, TELEPORT_PASSIVE);
564 teleport_player_to(caster_ptr, y, x, TELEPORT_SPONTANEOUS);
569 * @brief 次元の扉処理のメインルーチン /
570 * @param caster_ptr プレーヤーへの参照ポインタ
572 * @return ターンを消費した場合TRUEを返す
574 bool dimension_door(player_type *caster_ptr)
577 if (!tgt_pt(caster_ptr, &x, &y))
580 if (exe_dimension_door(caster_ptr, x, y))
583 msg_print(_("精霊界から物質界に戻る時うまくいかなかった!", "You fail to exit the astral plane correctly!"));