3 * @brief 保存された階の管理 / management of the saved floor
6 * Copyright (c) 2002 Mogami \n
7 * This software may be copied and distributed for educational, research, and \n
8 * not for profit purposes provided that this copyright and statement are \n
9 * included in all such copies. \n
10 * 2014 Deskull rearranged comment for Doxygen. \n
15 #include "floor-events.h"
21 #include "spells-floor.h"
22 #include "monster-status.h"
23 #include "object-hook.h"
25 #include "cmd-basic.h"
27 static FLOOR_IDX new_floor_id; /*!<次のフロアのID / floor_id of the destination */
28 static u32b change_floor_mode; /*!<フロア移行処理に関するフラグ / Mode flags for changing floor */
29 static u32b latest_visit_mark; /*!<フロアを渡った回数?(確認中) / Max number of visit_mark */
33 * @brief 保存フロア配列を初期化する / Initialize saved_floors array.
34 * @param force テンポラリファイルが残っていた場合も警告なしで強制的に削除する。
35 * @details Make sure that old temporal files are not remaining as gurbages.
38 void init_saved_floors(bool force)
40 char floor_savefile[1024];
43 BIT_FLAGS mode = 0644;
47 /* Get "games" permissions */
52 for (i = 0; i < MAX_SAVED_FLOORS; i++)
54 saved_floor_type *sf_ptr = &saved_floors[i];
57 sprintf(floor_savefile, "%s.F%02d", savefile, i);
59 /* Grab permissions */
62 /* Try to create the file */
63 fd = fd_make(floor_savefile, mode);
65 /* Drop permissions */
73 msg_print(_("エラー:古いテンポラリ・ファイルが残っています。", "Error: There are old temporal files."));
74 msg_print(_("変愚蛮怒を二重に起動していないか確認してください。", "Make sure you are not running two game processes simultaneously."));
75 msg_print(_("過去に変愚蛮怒がクラッシュした場合は一時ファイルを", "If the temporal files are garbages of old crashed process, "));
76 msg_print(_("強制的に削除して実行を続けられます。", "you can delete it safely."));
77 if (!get_check(_("強制的に削除してもよろしいですか?", "Do you delete old temporal files? ")))
78 quit(_("実行中止", "Aborted."));
88 /* Grab permissions */
91 /* Simply kill the temporal file */
92 (void)fd_kill(floor_savefile);
94 /* Drop permissions */
100 /* No floor_id used yet (No.0 is reserved to indicate non existance) */
103 /* vist_mark is from 1 */
104 latest_visit_mark = 1;
106 /* A sign to mark temporal files */
107 saved_floor_file_sign = (u32b)time(NULL);
109 /* No next floor yet */
112 /* No change floor mode yet */
113 change_floor_mode = 0;
117 /* Drop "games" permissions */
124 * @brief 保存フロア用テンポラリファイルを削除する / Kill temporal files
125 * @details Should be called just before the game quit.
128 void clear_saved_floor_files(void)
130 char floor_savefile[1024];
135 /* Get "games" permissions */
140 for (i = 0; i < MAX_SAVED_FLOORS; i++)
142 saved_floor_type *sf_ptr = &saved_floors[i];
144 /* No temporal file */
145 if (!sf_ptr->floor_id) continue;
146 if (sf_ptr->floor_id == p_ptr->floor_id) continue;
149 sprintf(floor_savefile, "%s.F%02d", savefile, i);
151 /* Grab permissions */
154 /* Simply kill the temporal file */
155 (void)fd_kill(floor_savefile);
157 /* Drop permissions */
163 /* Drop "games" permissions */
170 * @brief 保存フロアIDから参照ポインタを得る / Get a pointer for an item of the saved_floors array.
171 * @param floor_id 保存フロアID
172 * @return IDに対応する保存フロアのポインタ、ない場合はNULLを返す。
174 saved_floor_type *get_sf_ptr(FLOOR_IDX floor_id)
178 /* floor_id No.0 indicates no floor */
179 if (!floor_id) return NULL;
181 for (i = 0; i < MAX_SAVED_FLOORS; i++)
183 saved_floor_type *sf_ptr = &saved_floors[i];
185 if (sf_ptr->floor_id == floor_id) return sf_ptr;
194 * @brief 参照ポインタ先の保存フロアを抹消する / kill a saved floor and get an empty space
195 * @param sf_ptr 保存フロアの参照ポインタ
198 static void kill_saved_floor(saved_floor_type *sf_ptr)
200 char floor_savefile[1024];
204 if (!sf_ptr->floor_id) return;
206 if (sf_ptr->floor_id == p_ptr->floor_id)
208 /* Kill current floor */
211 /* Current floor doesn't have temporal file */
216 sprintf(floor_savefile, "%s.F%02d", savefile, (int)sf_ptr->savefile_id);
218 /* Grab permissions */
221 /* Simply kill the temporal file */
222 (void)fd_kill(floor_savefile);
224 /* Drop permissions */
228 /* No longer exists */
229 sf_ptr->floor_id = 0;
234 * @brief 新規に利用可能な保存フロアを返す / Initialize new saved floor and get its floor id.
235 * @return 利用可能な保存フロアID
237 * If number of saved floors are already MAX_SAVED_FLOORS, kill the oldest one.
239 FLOOR_IDX get_new_floor_id(void)
241 saved_floor_type *sf_ptr = NULL;
244 /* Look for empty space */
245 for (i = 0; i < MAX_SAVED_FLOORS; i++)
247 sf_ptr = &saved_floors[i];
249 if (!sf_ptr->floor_id) break;
253 if (i == MAX_SAVED_FLOORS)
256 u32b oldest_visit = 0xffffffffL;
258 /* Search for oldest */
259 for (i = 0; i < MAX_SAVED_FLOORS; i++)
261 sf_ptr = &saved_floors[i];
263 /* Don't kill current floor */
264 if (sf_ptr->floor_id == p_ptr->floor_id) continue;
266 /* Don't kill newer */
267 if (sf_ptr->visit_mark > oldest_visit) continue;
270 oldest_visit = sf_ptr->visit_mark;
273 /* Kill oldest saved floor */
274 sf_ptr = &saved_floors[oldest];
275 kill_saved_floor(sf_ptr);
281 /* Prepare new floor data */
282 sf_ptr->savefile_id = i;
283 sf_ptr->floor_id = max_floor_id;
284 sf_ptr->last_visit = 0;
285 sf_ptr->upper_floor_id = 0;
286 sf_ptr->lower_floor_id = 0;
287 sf_ptr->visit_mark = latest_visit_mark++;
289 /* sf_ptr->dun_level may be changed later */
290 sf_ptr->dun_level = current_floor_ptr->dun_level;
293 /* Increment number of floor_id */
294 if (max_floor_id < MAX_SHORT) max_floor_id++;
296 /* 32767 floor_ids are all used up! Re-use ancient IDs */
297 else max_floor_id = 1;
299 return sf_ptr->floor_id;
304 * @brief フロア切り替え時の処理フラグを追加する / Prepare mode flags of changing floor
305 * @param mode 追加したい所持フラグ
308 void prepare_change_floor_mode(BIT_FLAGS mode)
310 change_floor_mode |= mode;
314 * @brief 階段移動先のフロアが生成できない時に簡単な行き止まりマップを作成する / Builds the dead end
317 static void build_dead_end(void)
321 /* Clear and empty the current_floor_ptr->grid_array */
324 /* Fill the arrays of floors and walls in the good proportions */
325 set_floor_and_wall(0);
328 current_floor_ptr->height = SCREEN_HGT;
329 current_floor_ptr->width = SCREEN_WID;
331 /* Filled with permanent walls */
332 for (y = 0; y < MAX_HGT; y++)
334 for (x = 0; x < MAX_WID; x++)
336 /* Create "solid" perma-wall */
337 place_solid_perm_bold(y, x);
341 /* Place at center of the floor */
342 p_ptr->y = current_floor_ptr->height / 2;
343 p_ptr->x = current_floor_ptr->width / 2;
345 /* Give one square */
346 place_floor_bold(p_ptr->y, p_ptr->x);
348 wipe_generate_random_floor_flags();
353 #define MAX_PARTY_MON 21 /*!< フロア移動時に先のフロアに連れて行けるペットの最大数 Maximum number of preservable pets */
354 static monster_type party_mon[MAX_PARTY_MON]; /*!< フロア移動に保存するペットモンスターの配列 */
357 * @brief フロア移動時のペット保存処理 / Preserve_pets
360 static void preserve_pet(void)
365 for (num = 0; num < MAX_PARTY_MON; num++)
367 party_mon[num].r_idx = 0;
372 monster_type *m_ptr = ¤t_floor_ptr->m_list[p_ptr->riding];
374 /* Pet of other pet don't follow. */
375 if (m_ptr->parent_m_idx)
378 p_ptr->pet_extra_flags &= ~(PF_RYOUTE);
379 p_ptr->riding_ryoute = p_ptr->old_riding_ryoute = FALSE;
383 /* Preserve the mount */
384 (void)COPY(&party_mon[0], m_ptr, monster_type);
386 /* Delete from this floor */
387 delete_monster_idx(p_ptr->riding);
392 * If player is in wild mode, no pets are preserved
393 * except a monster whom player riding
395 if (!p_ptr->wild_mode && !p_ptr->inside_arena && !p_ptr->inside_battle)
397 for (i = m_max - 1, num = 1; (i >= 1 && num < MAX_PARTY_MON); i--)
399 monster_type *m_ptr = ¤t_floor_ptr->m_list[i];
401 if (!monster_is_valid(m_ptr)) continue;
402 if (!is_pet(m_ptr)) continue;
403 if (i == p_ptr->riding) continue;
405 if (reinit_wilderness)
407 /* Don't lose sight of pets when getting a Quest */
411 POSITION dis = distance(p_ptr->y, p_ptr->x, m_ptr->fy, m_ptr->fx);
413 /* Confused (etc.) monsters don't follow. */
414 if (MON_CONFUSED(m_ptr) || MON_STUNNED(m_ptr) || MON_CSLEEP(m_ptr)) continue;
416 /* Pet of other pet don't follow. */
417 if (m_ptr->parent_m_idx) continue;
420 * Pets with nickname will follow even from 3 blocks away
421 * when you or the pet can see the other.
423 if (m_ptr->nickname &&
424 ((player_has_los_bold(m_ptr->fy, m_ptr->fx) && projectable(p_ptr->y, p_ptr->x, m_ptr->fy, m_ptr->fx)) ||
425 (los(m_ptr->fy, m_ptr->fx, p_ptr->y, p_ptr->x) && projectable(m_ptr->fy, m_ptr->fx, p_ptr->y, p_ptr->x))))
427 if (dis > 3) continue;
431 if (dis > 1) continue;
435 (void)COPY(&party_mon[num], ¤t_floor_ptr->m_list[i], monster_type);
439 /* Delete from this floor */
440 delete_monster_idx(i);
444 if (record_named_pet)
446 for (i = m_max - 1; i >=1; i--)
448 monster_type *m_ptr = ¤t_floor_ptr->m_list[i];
449 GAME_TEXT m_name[MAX_NLEN];
451 if (!monster_is_valid(m_ptr)) continue;
452 if (!is_pet(m_ptr)) continue;
453 if (!m_ptr->nickname) continue;
454 if (p_ptr->riding == i) continue;
456 monster_desc(m_name, m_ptr, MD_ASSUME_VISIBLE | MD_INDEF_VISIBLE);
457 do_cmd_write_nikki(NIKKI_NAMED_PET, RECORD_NAMED_PET_MOVED, m_name);
462 /* Pet of other pet may disappear. */
463 for (i = m_max - 1; i >=1; i--)
465 monster_type *m_ptr = ¤t_floor_ptr->m_list[i];
467 /* Are there its parent? */
468 if (m_ptr->parent_m_idx && !current_floor_ptr->m_list[m_ptr->parent_m_idx].r_idx)
470 /* Its parent have gone, it also goes away. */
474 GAME_TEXT m_name[MAX_NLEN];
475 monster_desc(m_name, m_ptr, 0);
476 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
479 delete_monster_idx(i);
486 * @brief フロア移動時にペットを伴った場合の準備処理 / Pre-calculate the racial counters of preserved pets
489 * To prevent multiple generation of unique monster who is the minion of player
491 void precalc_cur_num_of_pet(void)
495 int max_num = p_ptr->wild_mode ? 1 : MAX_PARTY_MON;
497 for (i = 0; i < max_num; i++)
499 m_ptr = &party_mon[i];
501 /* Skip empty monsters */
502 if (!monster_is_valid(m_ptr)) continue;
504 /* Hack -- Increase the racial counter */
505 real_r_ptr(m_ptr)->cur_num++;
510 * @brief 移動先のフロアに伴ったペットを配置する / Place preserved pet monsters on new floor
513 static void place_pet(void)
516 int max_num = p_ptr->wild_mode ? 1 : MAX_PARTY_MON;
518 for (i = 0; i < max_num; i++)
520 POSITION cy = 0, cx = 0;
523 if (!(party_mon[i].r_idx)) continue;
528 p_ptr->riding = m_idx;
540 for (d = 1; d < A_MAX; d++)
542 for (j = 1000; j > 0; j--)
544 scatter(&cy, &cx, p_ptr->y, p_ptr->x, d, 0);
545 if (monster_can_enter(cy, cx, &r_info[party_mon[i].r_idx], 0)) break;
549 m_idx = (d == 6) ? 0 : m_pop();
554 monster_type *m_ptr = ¤t_floor_ptr->m_list[m_idx];
557 current_floor_ptr->grid_array[cy][cx].m_idx = m_idx;
559 m_ptr->r_idx = party_mon[i].r_idx;
561 /* Copy all member of the structure */
562 *m_ptr = party_mon[i];
563 r_ptr = real_r_ptr(m_ptr);
568 m_ptr->mtimed[MTIMED_CSLEEP] = 0;
569 m_ptr->hold_o_idx = 0;
572 if ((r_ptr->flags1 & RF1_FORCE_SLEEP) && !ironman_nightmare)
574 /* Monster is still being nice */
575 m_ptr->mflag |= (MFLAG_NICE);
577 /* Must repair monsters */
578 repair_monsters = TRUE;
580 update_monster(m_idx, TRUE);
583 /* Pre-calculated in precalc_cur_num_of_pet() */
584 /* r_ptr->cur_num++; */
586 /* Hack -- Count the number of "reproducers" */
587 if (r_ptr->flags2 & RF2_MULTIPLY) current_floor_ptr->num_repro++;
589 /* Hack -- Notice new multi-hued monsters */
591 monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
592 if (ap_r_ptr->flags1 & (RF1_ATTR_MULTI | RF1_SHAPECHANGER))
593 shimmer_monsters = TRUE;
598 monster_type *m_ptr = &party_mon[i];
599 monster_race *r_ptr = real_r_ptr(m_ptr);
600 GAME_TEXT m_name[MAX_NLEN];
602 monster_desc(m_name, m_ptr, 0);
603 msg_format(_("%sとはぐれてしまった。", "You have lost sight of %s."), m_name);
604 if (record_named_pet && m_ptr->nickname)
606 monster_desc(m_name, m_ptr, MD_INDEF_VISIBLE);
607 do_cmd_write_nikki(NIKKI_NAMED_PET, RECORD_NAMED_PET_LOST_SIGHT, m_name);
610 /* Pre-calculated in precalc_cur_num_of_pet(), but need to decrease */
611 if (r_ptr->cur_num) r_ptr->cur_num--;
615 /* For accuracy of precalc_cur_num_of_pet() */
616 (void)C_WIPE(party_mon, MAX_PARTY_MON, monster_type);
621 * @brief ユニークモンスターやアーティファクトの所在フロアを更新する / Hack -- Update location of unique monsters and artifacts
622 * @param cur_floor_id 現在のフロアID
625 * The r_ptr->floor_id and a_ptr->floor_id are not updated correctly\n
626 * while new floor creation since dungeons may be re-created by\n
627 * auto-scum option.\n
629 static void update_unique_artifact(s16b cur_floor_id)
633 /* Maintain unique monsters */
634 for (i = 1; i < m_max; i++)
637 monster_type *m_ptr = ¤t_floor_ptr->m_list[i];
639 if (!monster_is_valid(m_ptr)) continue;
641 /* Extract real monster race */
642 r_ptr = real_r_ptr(m_ptr);
644 /* Memorize location of the unique monster */
645 if ((r_ptr->flags1 & RF1_UNIQUE) ||
646 (r_ptr->flags7 & RF7_NAZGUL))
648 r_ptr->floor_id = cur_floor_id;
652 /* Maintain artifatcs */
653 for (i = 1; i < o_max; i++)
655 object_type *o_ptr = ¤t_floor_ptr->o_list[i];
657 /* Skip dead objects */
658 if (!o_ptr->k_idx) continue;
660 /* Memorize location of the artifact */
661 if (object_is_fixed_artifact(o_ptr))
663 a_info[o_ptr->name1].floor_id = cur_floor_id;
670 * @brief フロア移動時、プレイヤーの移動先モンスターが既にいた場合ランダムな近隣に移動させる / When a monster is at a place where player will return,
673 static void get_out_monster(void)
677 POSITION oy = p_ptr->y;
678 POSITION ox = p_ptr->x;
679 MONSTER_IDX m_idx = current_floor_ptr->grid_array[oy][ox].m_idx;
681 /* Nothing to do if no monster */
684 /* Look until done */
689 /* Pick a (possibly illegal) location */
690 POSITION ny = rand_spread(oy, dis);
691 POSITION nx = rand_spread(ox, dis);
695 /* Stop after 1000 tries */
696 if (tries > 10000) return;
699 * Increase distance after doing enough tries
700 * compared to area of possible space
702 if (tries > 20 * dis * dis) dis++;
704 /* Ignore illegal locations */
705 if (!in_bounds(ny, nx)) continue;
707 /* Require "empty" floor space */
708 if (!cave_empty_bold(ny, nx)) continue;
710 /* Hack -- no teleport onto glyph of warding */
711 if (is_glyph_grid(¤t_floor_ptr->grid_array[ny][nx])) continue;
712 if (is_explosive_rune_grid(¤t_floor_ptr->grid_array[ny][nx])) continue;
714 /* ...nor onto the Pattern */
715 if (pattern_tile(ny, nx)) continue;
717 /*** It's a good place ***/
719 m_ptr = ¤t_floor_ptr->m_list[m_idx];
721 /* Update the old location */
722 current_floor_ptr->grid_array[oy][ox].m_idx = 0;
724 /* Update the new location */
725 current_floor_ptr->grid_array[ny][nx].m_idx = m_idx;
727 /* Move the monster */
731 /* No need to do update_monster() */
739 * @brief 新フロアに移動元フロアに繋がる階段を配置する / Virtually teleport onto the stairs that is connecting between two floors.
740 * @param sf_ptr 移動元の保存フロア構造体参照ポインタ
743 static void locate_connected_stairs(saved_floor_type *sf_ptr)
745 POSITION x, y, sx = 0, sy = 0;
746 POSITION x_table[20];
747 POSITION y_table[20];
751 /* Search usable stairs */
752 for (y = 0; y < current_floor_ptr->height; y++)
754 for (x = 0; x < current_floor_ptr->width; x++)
756 grid_type *g_ptr = ¤t_floor_ptr->grid_array[y][x];
757 feature_type *f_ptr = &f_info[g_ptr->feat];
760 if (change_floor_mode & CFM_UP)
762 if (have_flag(f_ptr->flags, FF_LESS) && have_flag(f_ptr->flags, FF_STAIRS) &&
763 !have_flag(f_ptr->flags, FF_SPECIAL))
767 /* Found fixed stairs? */
768 if (g_ptr->special &&
769 g_ptr->special == sf_ptr->upper_floor_id)
777 else if (change_floor_mode & CFM_DOWN)
779 if (have_flag(f_ptr->flags, FF_MORE) && have_flag(f_ptr->flags, FF_STAIRS) &&
780 !have_flag(f_ptr->flags, FF_SPECIAL))
784 /* Found fixed stairs */
785 if (g_ptr->special &&
786 g_ptr->special == sf_ptr->lower_floor_id)
796 if (have_flag(f_ptr->flags, FF_BLDG))
802 if (ok && (num < 20))
819 /* No stairs found! -- No return */
820 prepare_change_floor_mode(CFM_RAND_PLACE | CFM_NO_RETURN);
822 /* Mega Hack -- It's not the stairs you enter. Disable it. */
823 if (!feat_uses_special(current_floor_ptr->grid_array[p_ptr->y][p_ptr->x].feat)) current_floor_ptr->grid_array[p_ptr->y][p_ptr->x].special = 0;
827 /* Choose random one */
830 /* Point stair location */
831 p_ptr->y = y_table[i];
832 p_ptr->x = x_table[i];
837 * @brief 現在のフロアを離れるに伴って行なわれる保存処理
838 * / Maintain quest monsters, mark next floor_id at stairs, save current floor, and prepare to enter next floor.
841 void leave_floor(void)
843 grid_type *g_ptr = NULL;
845 saved_floor_type *sf_ptr;
846 MONRACE_IDX quest_r_idx = 0;
849 /* Preserve pets and prepare to take these to next floor */
852 /* Remove all mirrors without explosion */
853 remove_all_mirrors(FALSE);
855 if (p_ptr->special_defense & NINJA_S_STEALTH) set_superstealth(FALSE);
857 /* New floor is not yet prepared */
860 /* Temporary get a floor_id (for Arena) */
861 if (!p_ptr->floor_id &&
862 (change_floor_mode & CFM_SAVE_FLOORS) &&
863 !(change_floor_mode & CFM_NO_RETURN))
865 /* Get temporal floor_id */
866 p_ptr->floor_id = get_new_floor_id();
869 /* Search the quest monster index */
870 for (i = 0; i < max_q_idx; i++)
872 if ((quest[i].status == QUEST_STATUS_TAKEN) &&
873 ((quest[i].type == QUEST_TYPE_KILL_LEVEL) ||
874 (quest[i].type == QUEST_TYPE_RANDOM)) &&
875 (quest[i].level == current_floor_ptr->dun_level) &&
876 (p_ptr->dungeon_idx == quest[i].dungeon) &&
877 !(quest[i].flags & QUEST_FLAG_PRESET))
879 quest_r_idx = quest[i].r_idx;
883 /* Maintain quest monsters */
884 for (i = 1; i < m_max; i++)
887 monster_type *m_ptr = ¤t_floor_ptr->m_list[i];
889 if (!monster_is_valid(m_ptr)) continue;
891 /* Only maintain quest monsters */
892 if (quest_r_idx != m_ptr->r_idx) continue;
894 /* Extract real monster race */
895 r_ptr = real_r_ptr(m_ptr);
897 /* Ignore unique monsters */
898 if ((r_ptr->flags1 & RF1_UNIQUE) ||
899 (r_ptr->flags7 & RF7_NAZGUL)) continue;
901 /* Delete non-unique quest monsters */
902 delete_monster_idx(i);
905 /* Check if there is a same item */
906 for (i = 0; i < INVEN_PACK; i++)
908 object_type *o_ptr = &inventory[i];
910 /* Skip dead objects */
911 if (!o_ptr->k_idx) continue;
913 /* Delete old memorized location of the artifact */
914 if (object_is_fixed_artifact(o_ptr))
916 a_info[o_ptr->name1].floor_id = 0;
920 /* Extract current floor info or NULL */
921 sf_ptr = get_sf_ptr(p_ptr->floor_id);
923 /* Choose random stairs */
924 if ((change_floor_mode & CFM_RAND_CONNECT) && p_ptr->floor_id)
926 locate_connected_stairs(sf_ptr);
929 /* Extract new dungeon level */
930 if (change_floor_mode & CFM_SAVE_FLOORS)
932 /* Extract stair position */
933 g_ptr = ¤t_floor_ptr->grid_array[p_ptr->y][p_ptr->x];
934 f_ptr = &f_info[g_ptr->feat];
936 /* Get back to old saved floor? */
937 if (g_ptr->special && !have_flag(f_ptr->flags, FF_SPECIAL) && get_sf_ptr(g_ptr->special))
939 /* Saved floor is exist. Use it. */
940 new_floor_id = g_ptr->special;
943 /* Mark shaft up/down */
944 if (have_flag(f_ptr->flags, FF_STAIRS) && have_flag(f_ptr->flags, FF_SHAFT))
946 prepare_change_floor_mode(CFM_SHAFT);
950 /* Climb up/down some sort of stairs */
951 if (change_floor_mode & (CFM_DOWN | CFM_UP))
955 /* Extract level movement number */
956 if (change_floor_mode & CFM_DOWN) move_num = 1;
957 else if (change_floor_mode & CFM_UP) move_num = -1;
959 /* Shafts are deeper than normal stairs */
960 if (change_floor_mode & CFM_SHAFT)
961 move_num += SGN(move_num);
963 /* Get out from or Enter the dungeon */
964 if (change_floor_mode & CFM_DOWN)
966 if (!current_floor_ptr->dun_level)
967 move_num = d_info[p_ptr->dungeon_idx].mindepth;
969 else if (change_floor_mode & CFM_UP)
971 if (current_floor_ptr->dun_level + move_num < d_info[p_ptr->dungeon_idx].mindepth)
972 move_num = -current_floor_ptr->dun_level;
975 current_floor_ptr->dun_level += move_num;
978 /* Leaving the dungeon to town */
979 if (!current_floor_ptr->dun_level && p_ptr->dungeon_idx)
981 p_ptr->leaving_dungeon = TRUE;
982 if (!vanilla_town && !lite_town)
984 p_ptr->wilderness_y = d_info[p_ptr->dungeon_idx].dy;
985 p_ptr->wilderness_x = d_info[p_ptr->dungeon_idx].dx;
987 p_ptr->recall_dungeon = p_ptr->dungeon_idx;
988 p_ptr->dungeon_idx = 0;
990 /* Reach to the surface -- Clear all saved floors */
991 change_floor_mode &= ~CFM_SAVE_FLOORS;
994 /* Kill some old saved floors */
995 if (!(change_floor_mode & CFM_SAVE_FLOORS))
997 /* Kill all saved floors */
998 for (i = 0; i < MAX_SAVED_FLOORS; i++)
999 kill_saved_floor(&saved_floors[i]);
1001 /* Reset visit_mark count */
1002 latest_visit_mark = 1;
1004 else if (change_floor_mode & CFM_NO_RETURN)
1006 /* Kill current floor */
1007 kill_saved_floor(sf_ptr);
1010 /* No current floor -- Left/Enter dungeon etc... */
1011 if (!p_ptr->floor_id)
1013 /* No longer need to save current floor */
1018 /* Mark next floor_id on the previous floor */
1022 new_floor_id = get_new_floor_id();
1024 /* Connect from here */
1025 if (g_ptr && !feat_uses_special(g_ptr->feat))
1027 g_ptr->special = new_floor_id;
1031 /* Fix connection -- level teleportation or trap door */
1032 if (change_floor_mode & CFM_RAND_CONNECT)
1034 if (change_floor_mode & CFM_UP)
1035 sf_ptr->upper_floor_id = new_floor_id;
1036 else if (change_floor_mode & CFM_DOWN)
1037 sf_ptr->lower_floor_id = new_floor_id;
1040 /* If you can return, you need to save previous floor */
1041 if ((change_floor_mode & CFM_SAVE_FLOORS) &&
1042 !(change_floor_mode & CFM_NO_RETURN))
1044 /* Get out of the my way! */
1047 /* Record the last visit current_world_ptr->game_turn of current floor */
1048 sf_ptr->last_visit = current_world_ptr->game_turn;
1054 /* Save current floor */
1055 if (!save_floor(sf_ptr, 0))
1057 /* Save failed -- No return */
1058 prepare_change_floor_mode(CFM_NO_RETURN);
1060 /* Kill current floor */
1061 kill_saved_floor(get_sf_ptr(p_ptr->floor_id));
1068 * @brief フロアの切り替え処理 / Enter new floor.
1071 * If the floor is an old saved floor, it will be\n
1072 * restored from the temporal file. If the floor is new one, new current_floor_ptr->grid_array\n
1073 * will be generated.\n
1075 void change_floor(void)
1077 saved_floor_type *sf_ptr;
1078 bool loaded = FALSE;
1080 /* The dungeon is not ready */
1081 character_dungeon = FALSE;
1083 /* No longer in the trap detecteded region */
1084 p_ptr->dtrap = FALSE;
1086 /* Mega-Hack -- no panel yet */
1092 /* Mega-Hack -- not ambushed on the wildness? */
1093 p_ptr->ambush_flag = FALSE;
1095 /* No saved floors (On the surface etc.) */
1096 if (!(change_floor_mode & CFM_SAVE_FLOORS) &&
1097 !(change_floor_mode & CFM_FIRST_FLOOR))
1099 /* Create current_floor_ptr->grid_array */
1100 generate_random_floor();
1102 /* Paranoia -- No new saved floor */
1106 /* In the dungeon */
1109 /* No floor_id yet */
1113 new_floor_id = get_new_floor_id();
1116 /* Pointer for infomations of new floor */
1117 sf_ptr = get_sf_ptr(new_floor_id);
1119 /* Try to restore old floor */
1120 if (sf_ptr->last_visit)
1122 /* Old saved floor is exist */
1123 if (load_floor(sf_ptr, 0))
1127 /* Forbid return stairs */
1128 if (change_floor_mode & CFM_NO_RETURN)
1130 grid_type *g_ptr = ¤t_floor_ptr->grid_array[p_ptr->y][p_ptr->x];
1132 if (!feat_uses_special(g_ptr->feat))
1134 if (change_floor_mode & (CFM_DOWN | CFM_UP))
1136 /* Reset to floor */
1137 g_ptr->feat = feat_ground_type[randint0(100)];
1147 * Set lower/upper_floor_id of new floor when the new
1148 * floor is right-above/right-under the current floor.
1150 * Stair creation/Teleport level/Trap door will take
1151 * you the same floor when you used it later again.
1153 if (p_ptr->floor_id)
1155 saved_floor_type *cur_sf_ptr = get_sf_ptr(p_ptr->floor_id);
1157 if (change_floor_mode & CFM_UP)
1159 /* New floor is right-above */
1160 if (cur_sf_ptr->upper_floor_id == new_floor_id)
1161 sf_ptr->lower_floor_id = p_ptr->floor_id;
1163 else if (change_floor_mode & CFM_DOWN)
1165 /* New floor is right-under */
1166 if (cur_sf_ptr->lower_floor_id == new_floor_id)
1167 sf_ptr->upper_floor_id = p_ptr->floor_id;
1171 /* Break connection to killed floor */
1174 if (change_floor_mode & CFM_UP)
1175 sf_ptr->lower_floor_id = 0;
1176 else if (change_floor_mode & CFM_DOWN)
1177 sf_ptr->upper_floor_id = 0;
1180 /* Maintain monsters and artifacts */
1184 GAME_TURN tmp_last_visit = sf_ptr->last_visit;
1185 GAME_TURN absence_ticks;
1186 int alloc_chance = d_info[p_ptr->dungeon_idx].max_m_alloc_chance;
1187 GAME_TURN alloc_times;
1189 while (tmp_last_visit > current_world_ptr->game_turn) tmp_last_visit -= TURNS_PER_TICK * TOWN_DAWN;
1190 absence_ticks = (current_world_ptr->game_turn - tmp_last_visit) / TURNS_PER_TICK;
1192 /* Maintain monsters */
1193 for (i = 1; i < m_max; i++)
1195 monster_race *r_ptr;
1196 monster_type *m_ptr = ¤t_floor_ptr->m_list[i];
1198 if (!monster_is_valid(m_ptr)) continue;
1203 m_ptr->hp = m_ptr->maxhp = m_ptr->max_maxhp;
1205 /* Remove timed status (except MTIMED_CSLEEP) */
1206 (void)set_monster_fast(i, 0);
1207 (void)set_monster_slow(i, 0);
1208 (void)set_monster_stunned(i, 0);
1209 (void)set_monster_confused(i, 0);
1210 (void)set_monster_monfear(i, 0);
1211 (void)set_monster_invulner(i, 0, FALSE);
1214 /* Extract real monster race */
1215 r_ptr = real_r_ptr(m_ptr);
1217 /* Ignore non-unique */
1218 if (!(r_ptr->flags1 & RF1_UNIQUE) &&
1219 !(r_ptr->flags7 & RF7_NAZGUL)) continue;
1221 /* Appear at a different floor? */
1222 if (r_ptr->floor_id != new_floor_id)
1224 /* Disapper from here */
1225 delete_monster_idx(i);
1229 /* Maintain artifatcs */
1230 for (i = 1; i < o_max; i++)
1232 object_type *o_ptr = ¤t_floor_ptr->o_list[i];
1234 /* Skip dead objects */
1235 if (!o_ptr->k_idx) continue;
1237 /* Ignore non-artifact */
1238 if (!object_is_fixed_artifact(o_ptr)) continue;
1240 /* Appear at a different floor? */
1241 if (a_info[o_ptr->name1].floor_id != new_floor_id)
1243 /* Disappear from here */
1244 delete_object_idx(i);
1248 /* Cancel preserve */
1249 a_info[o_ptr->name1].cur_num = 1;
1253 (void)place_quest_monsters();
1255 /* Place some random monsters */
1256 alloc_times = absence_ticks / alloc_chance;
1258 if (randint0(alloc_chance) < (absence_ticks % alloc_chance))
1261 for (i = 0; i < alloc_times; i++)
1263 /* Make a (group of) new monster */
1264 (void)alloc_monster(0, 0);
1269 /* New floor_id or failed to restore */
1270 else /* if (!loaded) */
1272 if (sf_ptr->last_visit)
1274 /* Temporal file is broken? */
1275 msg_print(_("階段は行き止まりだった。", "The staircases come to a dead end..."));
1277 /* Create simple dead end */
1280 /* Break connection */
1281 if (change_floor_mode & CFM_UP)
1283 sf_ptr->upper_floor_id = 0;
1285 else if (change_floor_mode & CFM_DOWN)
1287 sf_ptr->lower_floor_id = 0;
1292 /* Newly create current_floor_ptr->grid_array */
1293 generate_random_floor();
1296 /* Record last visit current_world_ptr->game_turn */
1297 sf_ptr->last_visit = current_world_ptr->game_turn;
1299 /* Set correct current_floor_ptr->dun_level value */
1300 sf_ptr->dun_level = current_floor_ptr->dun_level;
1302 /* Create connected stairs */
1303 if (!(change_floor_mode & CFM_NO_RETURN))
1305 /* Extract stair position */
1306 grid_type *g_ptr = ¤t_floor_ptr->grid_array[p_ptr->y][p_ptr->x];
1308 /*** Create connected stairs ***/
1310 /* No stairs down from Quest */
1311 if ((change_floor_mode & CFM_UP) && !quest_number(current_floor_ptr->dun_level))
1313 g_ptr->feat = (change_floor_mode & CFM_SHAFT) ? feat_state(feat_down_stair, FF_SHAFT) : feat_down_stair;
1316 /* No stairs up when ironman_downward */
1317 else if ((change_floor_mode & CFM_DOWN) && !ironman_downward)
1319 g_ptr->feat = (change_floor_mode & CFM_SHAFT) ? feat_state(feat_up_stair, FF_SHAFT) : feat_up_stair;
1322 /* Paranoia -- Clear mimic */
1325 /* Connect to previous floor */
1326 g_ptr->special = p_ptr->floor_id;
1330 /* Arrive at random grid */
1331 if (change_floor_mode & (CFM_RAND_PLACE))
1333 (void)new_player_spot();
1336 /* You see stairs blocked */
1337 else if ((change_floor_mode & CFM_NO_RETURN) && (change_floor_mode & (CFM_DOWN | CFM_UP)))
1341 msg_print(_("突然階段が塞がれてしまった。", "Suddenly the stairs is blocked!"));
1345 msg_print(_("ゴトゴトと何か音がした。", "You hear some noises."));
1352 * The "current_world_ptr->game_turn" is not always different number because
1353 * the level teleport doesn't take any current_world_ptr->game_turn. Use
1354 * visit mark instead of last visit current_world_ptr->game_turn to find the
1355 * oldest saved floor.
1357 sf_ptr->visit_mark = latest_visit_mark++;
1360 /* Place preserved pet monsters */
1363 /* Reset travel target place */
1364 forget_travel_flow();
1366 /* Hack -- maintain unique and artifacts */
1367 update_unique_artifact(new_floor_id);
1369 /* Now the player is in new floor */
1370 p_ptr->floor_id = new_floor_id;
1372 /* The dungeon is ready */
1373 character_dungeon = TRUE;
1375 /* Hack -- Munchkin characters always get whole map */
1376 if (p_ptr->pseikaku == SEIKAKU_MUNCHKIN)
1377 wiz_lite((bool)(p_ptr->pclass == CLASS_NINJA));
1379 /* Remember when this level was "created" */
1380 current_floor_ptr->generated_turn = current_world_ptr->game_turn;
1382 /* No dungeon feeling yet */
1383 p_ptr->feeling_turn = current_floor_ptr->generated_turn;
1386 /* Clear all flags */
1387 change_floor_mode = 0L;
1389 select_floor_music();