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
22 #include "floor-events.h"
23 #include "floor-generate.h"
29 #include "spells-floor.h"
30 #include "monster-status.h"
31 #include "object-hook.h"
33 #include "cmd-basic.h"
35 #include "player-effects.h"
36 #include "player-class.h"
37 #include "player-personality.h"
43 #include "view-mainwindow.h"
46 static FLOOR_IDX new_floor_id; /*!<次のフロアのID / floor_id of the destination */
47 static u32b latest_visit_mark; /*!<フロアを渡った回数?(確認中) / Max number of visit_mark */
50 * Number of floor_id used from birth
52 FLOOR_IDX max_floor_id;
55 * Sign for current process used in temporal files.
56 * Actually it is the start time of current process.
58 u32b saved_floor_file_sign;
61 * @brief 保存フロア配列を初期化する / Initialize saved_floors array.
62 * @param force テンポラリファイルが残っていた場合も警告なしで強制的に削除する。
63 * @details Make sure that old temporal files are not remaining as gurbages.
66 void init_saved_floors(bool force)
68 char floor_savefile[1024];
71 BIT_FLAGS mode = 0644;
75 /* Get "games" permissions */
80 for (i = 0; i < MAX_SAVED_FLOORS; i++)
82 saved_floor_type *sf_ptr = &saved_floors[i];
85 sprintf(floor_savefile, "%s.F%02d", savefile, i);
87 /* Grab permissions */
90 /* Try to create the file */
91 fd = fd_make(floor_savefile, mode);
93 /* Drop permissions */
101 msg_print(_("エラー:古いテンポラリ・ファイルが残っています。", "Error: There are old temporal files."));
102 msg_print(_("変愚蛮怒を二重に起動していないか確認してください。", "Make sure you are not running two game processes simultaneously."));
103 msg_print(_("過去に変愚蛮怒がクラッシュした場合は一時ファイルを", "If the temporal files are garbages of old crashed process, "));
104 msg_print(_("強制的に削除して実行を続けられます。", "you can delete it safely."));
105 if (!get_check(_("強制的に削除してもよろしいですか?", "Do you delete old temporal files? ")))
106 quit(_("実行中止", "Aborted."));
116 /* Grab permissions */
119 /* Simply kill the temporal file */
120 (void)fd_kill(floor_savefile);
122 /* Drop permissions */
125 sf_ptr->floor_id = 0;
128 /* No floor_id used yet (No.0 is reserved to indicate non existance) */
131 /* vist_mark is from 1 */
132 latest_visit_mark = 1;
134 /* A sign to mark temporal files */
135 saved_floor_file_sign = (u32b)time(NULL);
137 /* No next floor yet */
140 /* No change floor mode yet */
141 p_ptr->change_floor_mode = 0;
145 /* Drop "games" permissions */
152 * @brief 保存フロア用テンポラリファイルを削除する / Kill temporal files
153 * @details Should be called just before the game quit.
156 void clear_saved_floor_files(void)
158 char floor_savefile[1024];
163 /* Get "games" permissions */
168 for (i = 0; i < MAX_SAVED_FLOORS; i++)
170 saved_floor_type *sf_ptr = &saved_floors[i];
172 /* No temporal file */
173 if (!sf_ptr->floor_id) continue;
174 if (sf_ptr->floor_id == p_ptr->floor_id) continue;
177 sprintf(floor_savefile, "%s.F%02d", savefile, i);
179 /* Grab permissions */
182 /* Simply kill the temporal file */
183 (void)fd_kill(floor_savefile);
185 /* Drop permissions */
191 /* Drop "games" permissions */
198 * @brief 保存フロアIDから参照ポインタを得る / Get a pointer for an item of the saved_floors array.
199 * @param floor_id 保存フロアID
200 * @return IDに対応する保存フロアのポインタ、ない場合はNULLを返す。
202 saved_floor_type *get_sf_ptr(FLOOR_IDX floor_id)
206 /* floor_id No.0 indicates no floor */
207 if (!floor_id) return NULL;
209 for (i = 0; i < MAX_SAVED_FLOORS; i++)
211 saved_floor_type *sf_ptr = &saved_floors[i];
213 if (sf_ptr->floor_id == floor_id) return sf_ptr;
222 * @brief 参照ポインタ先の保存フロアを抹消する / kill a saved floor and get an empty space
223 * @param sf_ptr 保存フロアの参照ポインタ
226 static void kill_saved_floor(saved_floor_type *sf_ptr)
228 char floor_savefile[1024];
232 if (!sf_ptr->floor_id) return;
234 if (sf_ptr->floor_id == p_ptr->floor_id)
236 /* Kill current floor */
239 /* Current floor doesn't have temporal file */
244 sprintf(floor_savefile, "%s.F%02d", savefile, (int)sf_ptr->savefile_id);
246 /* Grab permissions */
249 /* Simply kill the temporal file */
250 (void)fd_kill(floor_savefile);
252 /* Drop permissions */
256 /* No longer exists */
257 sf_ptr->floor_id = 0;
262 * @brief 新規に利用可能な保存フロアを返す / Initialize new saved floor and get its floor id.
263 * @return 利用可能な保存フロアID
265 * If number of saved floors are already MAX_SAVED_FLOORS, kill the oldest one.
267 FLOOR_IDX get_new_floor_id(void)
269 saved_floor_type *sf_ptr = NULL;
272 /* Look for empty space */
273 for (i = 0; i < MAX_SAVED_FLOORS; i++)
275 sf_ptr = &saved_floors[i];
277 if (!sf_ptr->floor_id) break;
281 if (i == MAX_SAVED_FLOORS)
284 u32b oldest_visit = 0xffffffffL;
286 /* Search for oldest */
287 for (i = 0; i < MAX_SAVED_FLOORS; i++)
289 sf_ptr = &saved_floors[i];
291 /* Don't kill current floor */
292 if (sf_ptr->floor_id == p_ptr->floor_id) continue;
294 /* Don't kill newer */
295 if (sf_ptr->visit_mark > oldest_visit) continue;
298 oldest_visit = sf_ptr->visit_mark;
301 /* Kill oldest saved floor */
302 sf_ptr = &saved_floors[oldest];
303 kill_saved_floor(sf_ptr);
309 /* Prepare new floor data */
310 sf_ptr->savefile_id = i;
311 sf_ptr->floor_id = max_floor_id;
312 sf_ptr->last_visit = 0;
313 sf_ptr->upper_floor_id = 0;
314 sf_ptr->lower_floor_id = 0;
315 sf_ptr->visit_mark = latest_visit_mark++;
317 /* sf_ptr->dun_level may be changed later */
318 sf_ptr->dun_level = current_floor_ptr->dun_level;
321 /* Increment number of floor_id */
322 if (max_floor_id < MAX_SHORT) max_floor_id++;
324 /* 32767 floor_ids are all used up! Re-use ancient IDs */
325 else max_floor_id = 1;
327 return sf_ptr->floor_id;
332 * @brief フロア切り替え時の処理フラグを追加する / Prepare mode flags of changing floor
333 * @param mode 追加したい所持フラグ
336 void prepare_change_floor_mode(BIT_FLAGS mode)
338 p_ptr->change_floor_mode |= mode;
342 * @brief 階段移動先のフロアが生成できない時に簡単な行き止まりマップを作成する / Builds the dead end
345 static void build_dead_end(void)
349 /* Clear and empty the current_floor_ptr->grid_array */
352 /* Fill the arrays of floors and walls in the good proportions */
353 set_floor_and_wall(0);
356 current_floor_ptr->height = SCREEN_HGT;
357 current_floor_ptr->width = SCREEN_WID;
359 /* Filled with permanent walls */
360 for (y = 0; y < MAX_HGT; y++)
362 for (x = 0; x < MAX_WID; x++)
364 /* Create "solid" perma-wall */
365 place_solid_perm_bold(y, x);
369 /* Place at center of the floor */
370 p_ptr->y = current_floor_ptr->height / 2;
371 p_ptr->x = current_floor_ptr->width / 2;
373 /* Give one square */
374 place_floor_bold(p_ptr->y, p_ptr->x);
376 wipe_generate_random_floor_flags();
381 #define MAX_PARTY_MON 21 /*!< フロア移動時に先のフロアに連れて行けるペットの最大数 Maximum number of preservable pets */
382 static monster_type party_mon[MAX_PARTY_MON]; /*!< フロア移動に保存するペットモンスターの配列 */
385 * @brief フロア移動時のペット保存処理 / Preserve_pets
388 static void preserve_pet(void)
393 for (num = 0; num < MAX_PARTY_MON; num++)
395 party_mon[num].r_idx = 0;
400 monster_type *m_ptr = ¤t_floor_ptr->m_list[p_ptr->riding];
402 /* Pet of other pet don't follow. */
403 if (m_ptr->parent_m_idx)
406 p_ptr->pet_extra_flags &= ~(PF_RYOUTE);
407 p_ptr->riding_ryoute = p_ptr->old_riding_ryoute = FALSE;
411 /* Preserve the mount */
412 (void)COPY(&party_mon[0], m_ptr, monster_type);
414 /* Delete from this floor */
415 delete_monster_idx(p_ptr->riding);
420 * If player is in wild mode, no pets are preserved
421 * except a monster whom player riding
423 if (!p_ptr->wild_mode && !p_ptr->inside_arena && !p_ptr->phase_out)
425 for (i = current_floor_ptr->m_max - 1, num = 1; (i >= 1 && num < MAX_PARTY_MON); i--)
427 monster_type *m_ptr = ¤t_floor_ptr->m_list[i];
429 if (!monster_is_valid(m_ptr)) continue;
430 if (!is_pet(m_ptr)) continue;
431 if (i == p_ptr->riding) continue;
433 if (reinit_wilderness)
435 /* Don't lose sight of pets when getting a Quest */
439 POSITION dis = distance(p_ptr->y, p_ptr->x, m_ptr->fy, m_ptr->fx);
441 /* Confused (etc.) monsters don't follow. */
442 if (MON_CONFUSED(m_ptr) || MON_STUNNED(m_ptr) || MON_CSLEEP(m_ptr)) continue;
444 /* Pet of other pet don't follow. */
445 if (m_ptr->parent_m_idx) continue;
448 * Pets with nickname will follow even from 3 blocks away
449 * when you or the pet can see the other.
451 if (m_ptr->nickname &&
452 ((player_has_los_bold(m_ptr->fy, m_ptr->fx) && projectable(p_ptr->y, p_ptr->x, m_ptr->fy, m_ptr->fx)) ||
453 (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))))
455 if (dis > 3) continue;
459 if (dis > 1) continue;
463 (void)COPY(&party_mon[num], ¤t_floor_ptr->m_list[i], monster_type);
467 /* Delete from this floor */
468 delete_monster_idx(i);
472 if (record_named_pet)
474 for (i = current_floor_ptr->m_max - 1; i >=1; i--)
476 monster_type *m_ptr = ¤t_floor_ptr->m_list[i];
477 GAME_TEXT m_name[MAX_NLEN];
479 if (!monster_is_valid(m_ptr)) continue;
480 if (!is_pet(m_ptr)) continue;
481 if (!m_ptr->nickname) continue;
482 if (p_ptr->riding == i) continue;
484 monster_desc(m_name, m_ptr, MD_ASSUME_VISIBLE | MD_INDEF_VISIBLE);
485 do_cmd_write_nikki(NIKKI_NAMED_PET, RECORD_NAMED_PET_MOVED, m_name);
490 /* Pet of other pet may disappear. */
491 for (i = current_floor_ptr->m_max - 1; i >=1; i--)
493 monster_type *m_ptr = ¤t_floor_ptr->m_list[i];
495 /* Are there its parent? */
496 if (m_ptr->parent_m_idx && !current_floor_ptr->m_list[m_ptr->parent_m_idx].r_idx)
498 /* Its parent have gone, it also goes away. */
502 GAME_TEXT m_name[MAX_NLEN];
503 monster_desc(m_name, m_ptr, 0);
504 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
507 delete_monster_idx(i);
514 * @brief フロア移動時にペットを伴った場合の準備処理 / Pre-calculate the racial counters of preserved pets
517 * To prevent multiple generation of unique monster who is the minion of player
519 void precalc_cur_num_of_pet(void)
523 int max_num = p_ptr->wild_mode ? 1 : MAX_PARTY_MON;
525 for (i = 0; i < max_num; i++)
527 m_ptr = &party_mon[i];
529 /* Skip empty monsters */
530 if (!monster_is_valid(m_ptr)) continue;
532 /* Hack -- Increase the racial counter */
533 real_r_ptr(m_ptr)->cur_num++;
538 * @brief 移動先のフロアに伴ったペットを配置する / Place preserved pet monsters on new floor
541 static void place_pet(void)
544 int max_num = p_ptr->wild_mode ? 1 : MAX_PARTY_MON;
546 for (i = 0; i < max_num; i++)
548 POSITION cy = 0, cx = 0;
551 if (!(party_mon[i].r_idx)) continue;
556 p_ptr->riding = m_idx;
568 for (d = 1; d < A_MAX; d++)
570 for (j = 1000; j > 0; j--)
572 scatter(&cy, &cx, p_ptr->y, p_ptr->x, d, 0);
573 if (monster_can_enter(cy, cx, &r_info[party_mon[i].r_idx], 0)) break;
577 m_idx = (d == 6) ? 0 : m_pop();
582 monster_type *m_ptr = ¤t_floor_ptr->m_list[m_idx];
585 current_floor_ptr->grid_array[cy][cx].m_idx = m_idx;
587 m_ptr->r_idx = party_mon[i].r_idx;
589 /* Copy all member of the structure */
590 *m_ptr = party_mon[i];
591 r_ptr = real_r_ptr(m_ptr);
596 m_ptr->mtimed[MTIMED_CSLEEP] = 0;
597 m_ptr->hold_o_idx = 0;
600 if ((r_ptr->flags1 & RF1_FORCE_SLEEP) && !ironman_nightmare)
602 /* Monster is still being nice */
603 m_ptr->mflag |= (MFLAG_NICE);
605 /* Must repair monsters */
606 repair_monsters = TRUE;
608 update_monster(m_idx, TRUE);
611 /* Pre-calculated in precalc_cur_num_of_pet() */
612 /* r_ptr->cur_num++; */
614 /* Hack -- Count the number of "reproducers" */
615 if (r_ptr->flags2 & RF2_MULTIPLY) current_floor_ptr->num_repro++;
620 monster_type *m_ptr = &party_mon[i];
621 monster_race *r_ptr = real_r_ptr(m_ptr);
622 GAME_TEXT m_name[MAX_NLEN];
624 monster_desc(m_name, m_ptr, 0);
625 msg_format(_("%sとはぐれてしまった。", "You have lost sight of %s."), m_name);
626 if (record_named_pet && m_ptr->nickname)
628 monster_desc(m_name, m_ptr, MD_INDEF_VISIBLE);
629 do_cmd_write_nikki(NIKKI_NAMED_PET, RECORD_NAMED_PET_LOST_SIGHT, m_name);
632 /* Pre-calculated in precalc_cur_num_of_pet(), but need to decrease */
633 if (r_ptr->cur_num) r_ptr->cur_num--;
637 /* For accuracy of precalc_cur_num_of_pet() */
638 (void)C_WIPE(party_mon, MAX_PARTY_MON, monster_type);
643 * @brief ユニークモンスターやアーティファクトの所在フロアを更新する / Hack -- Update location of unique monsters and artifacts
644 * @param cur_floor_id 現在のフロアID
647 * The r_ptr->floor_id and a_ptr->floor_id are not updated correctly\n
648 * while new floor creation since dungeons may be re-created by\n
649 * auto-scum option.\n
651 static void update_unique_artifact(s16b cur_floor_id)
655 /* Maintain unique monsters */
656 for (i = 1; i < current_floor_ptr->m_max; i++)
659 monster_type *m_ptr = ¤t_floor_ptr->m_list[i];
661 if (!monster_is_valid(m_ptr)) continue;
663 /* Extract real monster race */
664 r_ptr = real_r_ptr(m_ptr);
666 /* Memorize location of the unique monster */
667 if ((r_ptr->flags1 & RF1_UNIQUE) ||
668 (r_ptr->flags7 & RF7_NAZGUL))
670 r_ptr->floor_id = cur_floor_id;
674 /* Maintain artifatcs */
675 for (i = 1; i < current_floor_ptr->o_max; i++)
677 object_type *o_ptr = ¤t_floor_ptr->o_list[i];
679 if (!OBJECT_IS_VALID(o_ptr)) continue;
681 /* Memorize location of the artifact */
682 if (object_is_fixed_artifact(o_ptr))
684 a_info[o_ptr->name1].floor_id = cur_floor_id;
691 * @brief フロア移動時、プレイヤーの移動先モンスターが既にいた場合ランダムな近隣に移動させる / When a monster is at a place where player will return,
694 static void get_out_monster(void)
698 POSITION oy = p_ptr->y;
699 POSITION ox = p_ptr->x;
700 MONSTER_IDX m_idx = current_floor_ptr->grid_array[oy][ox].m_idx;
702 /* Nothing to do if no monster */
705 /* Look until done */
710 /* Pick a (possibly illegal) location */
711 POSITION ny = rand_spread(oy, dis);
712 POSITION nx = rand_spread(ox, dis);
716 /* Stop after 1000 tries */
717 if (tries > 10000) return;
720 * Increase distance after doing enough tries
721 * compared to area of possible space
723 if (tries > 20 * dis * dis) dis++;
725 /* Ignore illegal locations */
726 if (!in_bounds(ny, nx)) continue;
728 /* Require "empty" floor space */
729 if (!cave_empty_bold(ny, nx)) continue;
731 /* Hack -- no teleport onto glyph of warding */
732 if (is_glyph_grid(¤t_floor_ptr->grid_array[ny][nx])) continue;
733 if (is_explosive_rune_grid(¤t_floor_ptr->grid_array[ny][nx])) continue;
735 /* ...nor onto the Pattern */
736 if (pattern_tile(ny, nx)) continue;
738 /*** It's a good place ***/
740 m_ptr = ¤t_floor_ptr->m_list[m_idx];
742 /* Update the old location */
743 current_floor_ptr->grid_array[oy][ox].m_idx = 0;
745 /* Update the new location */
746 current_floor_ptr->grid_array[ny][nx].m_idx = m_idx;
748 /* Move the monster */
752 /* No need to do update_monster() */
760 * @brief 新フロアに移動元フロアに繋がる階段を配置する / Virtually teleport onto the stairs that is connecting between two floors.
761 * @param sf_ptr 移動元の保存フロア構造体参照ポインタ
764 static void locate_connected_stairs(saved_floor_type *sf_ptr, BIT_FLAGS floor_mode)
766 POSITION x, y, sx = 0, sy = 0;
767 POSITION x_table[20];
768 POSITION y_table[20];
772 /* Search usable stairs */
773 for (y = 0; y < current_floor_ptr->height; y++)
775 for (x = 0; x < current_floor_ptr->width; x++)
777 grid_type *g_ptr = ¤t_floor_ptr->grid_array[y][x];
778 feature_type *f_ptr = &f_info[g_ptr->feat];
781 if (floor_mode & CFM_UP)
783 if (have_flag(f_ptr->flags, FF_LESS) && have_flag(f_ptr->flags, FF_STAIRS) &&
784 !have_flag(f_ptr->flags, FF_SPECIAL))
788 /* Found fixed stairs? */
789 if (g_ptr->special &&
790 g_ptr->special == sf_ptr->upper_floor_id)
798 else if (floor_mode & CFM_DOWN)
800 if (have_flag(f_ptr->flags, FF_MORE) && have_flag(f_ptr->flags, FF_STAIRS) &&
801 !have_flag(f_ptr->flags, FF_SPECIAL))
805 /* Found fixed stairs */
806 if (g_ptr->special &&
807 g_ptr->special == sf_ptr->lower_floor_id)
817 if (have_flag(f_ptr->flags, FF_BLDG))
823 if (ok && (num < 20))
840 /* No stairs found! -- No return */
841 prepare_change_floor_mode(CFM_RAND_PLACE | CFM_NO_RETURN);
843 /* Mega Hack -- It's not the stairs you enter. Disable it. */
844 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;
848 /* Choose random one */
851 /* Point stair location */
852 p_ptr->y = y_table[i];
853 p_ptr->x = x_table[i];
858 * @brief 現在のフロアを離れるに伴って行なわれる保存処理
859 * / Maintain quest monsters, mark next floor_id at stairs, save current floor, and prepare to enter next floor.
862 void leave_floor(BIT_FLAGS floor_mode)
864 grid_type *g_ptr = NULL;
866 saved_floor_type *sf_ptr;
867 MONRACE_IDX quest_r_idx = 0;
870 /* Preserve pets and prepare to take these to next floor */
873 /* Remove all mirrors without explosion */
874 remove_all_mirrors(FALSE);
876 if (p_ptr->special_defense & NINJA_S_STEALTH) set_superstealth(FALSE);
878 /* New floor is not yet prepared */
881 /* Temporary get a floor_id (for Arena) */
882 if (!p_ptr->floor_id &&
883 (floor_mode & CFM_SAVE_FLOORS) &&
884 !(floor_mode & CFM_NO_RETURN))
886 /* Get temporal floor_id */
887 p_ptr->floor_id = get_new_floor_id();
890 /* Search the quest monster index */
891 for (i = 0; i < max_q_idx; i++)
893 if ((quest[i].status == QUEST_STATUS_TAKEN) &&
894 ((quest[i].type == QUEST_TYPE_KILL_LEVEL) ||
895 (quest[i].type == QUEST_TYPE_RANDOM)) &&
896 (quest[i].level == current_floor_ptr->dun_level) &&
897 (p_ptr->dungeon_idx == quest[i].dungeon) &&
898 !(quest[i].flags & QUEST_FLAG_PRESET))
900 quest_r_idx = quest[i].r_idx;
904 /* Maintain quest monsters */
905 for (i = 1; i < current_floor_ptr->m_max; i++)
908 monster_type *m_ptr = ¤t_floor_ptr->m_list[i];
910 if (!monster_is_valid(m_ptr)) continue;
912 /* Only maintain quest monsters */
913 if (quest_r_idx != m_ptr->r_idx) continue;
915 /* Extract real monster race */
916 r_ptr = real_r_ptr(m_ptr);
918 /* Ignore unique monsters */
919 if ((r_ptr->flags1 & RF1_UNIQUE) ||
920 (r_ptr->flags7 & RF7_NAZGUL)) continue;
922 /* Delete non-unique quest monsters */
923 delete_monster_idx(i);
926 /* Check if there is a same item */
927 for (i = 0; i < INVEN_PACK; i++)
929 object_type *o_ptr = &p_ptr->inventory_list[i];
931 if (!OBJECT_IS_VALID(o_ptr)) continue;
933 /* Delete old memorized location of the artifact */
934 if (object_is_fixed_artifact(o_ptr))
936 a_info[o_ptr->name1].floor_id = 0;
940 /* Extract current floor info or NULL */
941 sf_ptr = get_sf_ptr(p_ptr->floor_id);
943 /* Choose random stairs */
944 if ((floor_mode & CFM_RAND_CONNECT) && p_ptr->floor_id)
946 locate_connected_stairs(sf_ptr, floor_mode);
949 /* Extract new dungeon level */
950 if (floor_mode & CFM_SAVE_FLOORS)
952 /* Extract stair position */
953 g_ptr = ¤t_floor_ptr->grid_array[p_ptr->y][p_ptr->x];
954 f_ptr = &f_info[g_ptr->feat];
956 /* Get back to old saved floor? */
957 if (g_ptr->special && !have_flag(f_ptr->flags, FF_SPECIAL) && get_sf_ptr(g_ptr->special))
959 /* Saved floor is exist. Use it. */
960 new_floor_id = g_ptr->special;
963 /* Mark shaft up/down */
964 if (have_flag(f_ptr->flags, FF_STAIRS) && have_flag(f_ptr->flags, FF_SHAFT))
966 prepare_change_floor_mode(CFM_SHAFT);
970 /* Climb up/down some sort of stairs */
971 if (floor_mode & (CFM_DOWN | CFM_UP))
975 /* Extract level movement number */
976 if (floor_mode & CFM_DOWN) move_num = 1;
977 else if (floor_mode & CFM_UP) move_num = -1;
979 /* Shafts are deeper than normal stairs */
980 if (floor_mode & CFM_SHAFT)
981 move_num += SGN(move_num);
983 /* Get out from or Enter the dungeon */
984 if (floor_mode & CFM_DOWN)
986 if (!current_floor_ptr->dun_level)
987 move_num = d_info[p_ptr->dungeon_idx].mindepth;
989 else if (floor_mode & CFM_UP)
991 if (current_floor_ptr->dun_level + move_num < d_info[p_ptr->dungeon_idx].mindepth)
992 move_num = -current_floor_ptr->dun_level;
995 current_floor_ptr->dun_level += move_num;
998 /* Leaving the dungeon to town */
999 if (!current_floor_ptr->dun_level && p_ptr->dungeon_idx)
1001 p_ptr->leaving_dungeon = TRUE;
1002 if (!vanilla_town && !lite_town)
1004 p_ptr->wilderness_y = d_info[p_ptr->dungeon_idx].dy;
1005 p_ptr->wilderness_x = d_info[p_ptr->dungeon_idx].dx;
1007 p_ptr->recall_dungeon = p_ptr->dungeon_idx;
1008 p_ptr->dungeon_idx = 0;
1010 /* Reach to the surface -- Clear all saved floors */
1011 floor_mode &= ~CFM_SAVE_FLOORS;
1014 /* Kill some old saved floors */
1015 if (!(floor_mode & CFM_SAVE_FLOORS))
1017 /* Kill all saved floors */
1018 for (i = 0; i < MAX_SAVED_FLOORS; i++)
1019 kill_saved_floor(&saved_floors[i]);
1021 /* Reset visit_mark count */
1022 latest_visit_mark = 1;
1024 else if (floor_mode & CFM_NO_RETURN)
1026 /* Kill current floor */
1027 kill_saved_floor(sf_ptr);
1030 /* No current floor -- Left/Enter dungeon etc... */
1031 if (!p_ptr->floor_id)
1033 /* No longer need to save current floor */
1038 /* Mark next floor_id on the previous floor */
1042 new_floor_id = get_new_floor_id();
1044 /* Connect from here */
1045 if (g_ptr && !feat_uses_special(g_ptr->feat))
1047 g_ptr->special = new_floor_id;
1051 /* Fix connection -- level teleportation or trap door */
1052 if (floor_mode & CFM_RAND_CONNECT)
1054 if (floor_mode & CFM_UP)
1055 sf_ptr->upper_floor_id = new_floor_id;
1056 else if (floor_mode & CFM_DOWN)
1057 sf_ptr->lower_floor_id = new_floor_id;
1060 /* If you can return, you need to save previous floor */
1061 if ((floor_mode & CFM_SAVE_FLOORS) &&
1062 !(floor_mode & CFM_NO_RETURN))
1064 /* Get out of the my way! */
1067 /* Record the last visit current_world_ptr->game_turn of current floor */
1068 sf_ptr->last_visit = current_world_ptr->game_turn;
1074 /* Save current floor */
1075 if (!save_floor(sf_ptr, 0))
1077 /* Save failed -- No return */
1078 prepare_change_floor_mode(CFM_NO_RETURN);
1080 /* Kill current floor */
1081 kill_saved_floor(get_sf_ptr(p_ptr->floor_id));
1088 * @brief フロアの切り替え処理 / Enter new floor.
1091 * If the floor is an old saved floor, it will be\n
1092 * restored from the temporal file. If the floor is new one, new current_floor_ptr->grid_array\n
1093 * will be generated.\n
1095 void change_floor(BIT_FLAGS floor_mode)
1097 saved_floor_type *sf_ptr;
1098 bool loaded = FALSE;
1100 /* The dungeon is not ready */
1101 current_world_ptr->character_dungeon = FALSE;
1103 /* No longer in the trap detecteded region */
1104 p_ptr->dtrap = FALSE;
1106 /* Mega-Hack -- no panel yet */
1112 /* Mega-Hack -- not ambushed on the wildness? */
1113 p_ptr->ambush_flag = FALSE;
1115 /* No saved floors (On the surface etc.) */
1116 if (!(floor_mode & CFM_SAVE_FLOORS) &&
1117 !(floor_mode & CFM_FIRST_FLOOR))
1119 /* Create current_floor_ptr->grid_array */
1120 generate_random_floor();
1122 /* Paranoia -- No new saved floor */
1126 /* In the dungeon */
1129 /* No floor_id yet */
1133 new_floor_id = get_new_floor_id();
1136 /* Pointer for infomations of new floor */
1137 sf_ptr = get_sf_ptr(new_floor_id);
1139 /* Try to restore old floor */
1140 if (sf_ptr->last_visit)
1142 /* Old saved floor is exist */
1143 if (load_floor(sf_ptr, 0))
1147 /* Forbid return stairs */
1148 if (floor_mode & CFM_NO_RETURN)
1150 grid_type *g_ptr = ¤t_floor_ptr->grid_array[p_ptr->y][p_ptr->x];
1152 if (!feat_uses_special(g_ptr->feat))
1154 if (floor_mode & (CFM_DOWN | CFM_UP))
1156 /* Reset to floor */
1157 g_ptr->feat = feat_ground_type[randint0(100)];
1167 * Set lower/upper_floor_id of new floor when the new
1168 * floor is right-above/right-under the current floor.
1170 * Stair creation/Teleport level/Trap door will take
1171 * you the same floor when you used it later again.
1173 if (p_ptr->floor_id)
1175 saved_floor_type *cur_sf_ptr = get_sf_ptr(p_ptr->floor_id);
1177 if (floor_mode & CFM_UP)
1179 /* New floor is right-above */
1180 if (cur_sf_ptr->upper_floor_id == new_floor_id)
1181 sf_ptr->lower_floor_id = p_ptr->floor_id;
1183 else if (floor_mode & CFM_DOWN)
1185 /* New floor is right-under */
1186 if (cur_sf_ptr->lower_floor_id == new_floor_id)
1187 sf_ptr->upper_floor_id = p_ptr->floor_id;
1191 /* Break connection to killed floor */
1194 if (floor_mode & CFM_UP)
1195 sf_ptr->lower_floor_id = 0;
1196 else if (floor_mode & CFM_DOWN)
1197 sf_ptr->upper_floor_id = 0;
1200 /* Maintain monsters and artifacts */
1204 GAME_TURN tmp_last_visit = sf_ptr->last_visit;
1205 GAME_TURN absence_ticks;
1206 int alloc_chance = d_info[p_ptr->dungeon_idx].max_m_alloc_chance;
1207 GAME_TURN alloc_times;
1209 while (tmp_last_visit > current_world_ptr->game_turn) tmp_last_visit -= TURNS_PER_TICK * TOWN_DAWN;
1210 absence_ticks = (current_world_ptr->game_turn - tmp_last_visit) / TURNS_PER_TICK;
1212 /* Maintain monsters */
1213 for (i = 1; i < current_floor_ptr->m_max; i++)
1215 monster_race *r_ptr;
1216 monster_type *m_ptr = ¤t_floor_ptr->m_list[i];
1218 if (!monster_is_valid(m_ptr)) continue;
1223 m_ptr->hp = m_ptr->maxhp = m_ptr->max_maxhp;
1225 /* Remove timed status (except MTIMED_CSLEEP) */
1226 (void)set_monster_fast(i, 0);
1227 (void)set_monster_slow(i, 0);
1228 (void)set_monster_stunned(i, 0);
1229 (void)set_monster_confused(i, 0);
1230 (void)set_monster_monfear(i, 0);
1231 (void)set_monster_invulner(i, 0, FALSE);
1234 /* Extract real monster race */
1235 r_ptr = real_r_ptr(m_ptr);
1237 /* Ignore non-unique */
1238 if (!(r_ptr->flags1 & RF1_UNIQUE) &&
1239 !(r_ptr->flags7 & RF7_NAZGUL)) continue;
1241 /* Appear at a different floor? */
1242 if (r_ptr->floor_id != new_floor_id)
1244 /* Disapper from here */
1245 delete_monster_idx(i);
1249 /* Maintain artifatcs */
1250 for (i = 1; i < current_floor_ptr->o_max; i++)
1252 object_type *o_ptr = ¤t_floor_ptr->o_list[i];
1254 if (!OBJECT_IS_VALID(o_ptr)) continue;
1256 /* Ignore non-artifact */
1257 if (!object_is_fixed_artifact(o_ptr)) continue;
1259 /* Appear at a different floor? */
1260 if (a_info[o_ptr->name1].floor_id != new_floor_id)
1262 /* Disappear from here */
1263 delete_object_idx(i);
1267 /* Cancel preserve */
1268 a_info[o_ptr->name1].cur_num = 1;
1272 (void)place_quest_monsters();
1274 /* Place some random monsters */
1275 alloc_times = absence_ticks / alloc_chance;
1277 if (randint0(alloc_chance) < (absence_ticks % alloc_chance))
1280 for (i = 0; i < alloc_times; i++)
1282 /* Make a (group of) new monster */
1283 (void)alloc_monster(0, 0);
1288 /* New floor_id or failed to restore */
1289 else /* if (!loaded) */
1291 if (sf_ptr->last_visit)
1293 /* Temporal file is broken? */
1294 msg_print(_("階段は行き止まりだった。", "The staircases come to a dead end..."));
1296 /* Create simple dead end */
1299 /* Break connection */
1300 if (floor_mode & CFM_UP)
1302 sf_ptr->upper_floor_id = 0;
1304 else if (floor_mode & CFM_DOWN)
1306 sf_ptr->lower_floor_id = 0;
1311 /* Newly create current_floor_ptr->grid_array */
1312 generate_random_floor();
1315 /* Record last visit current_world_ptr->game_turn */
1316 sf_ptr->last_visit = current_world_ptr->game_turn;
1318 /* Set correct current_floor_ptr->dun_level value */
1319 sf_ptr->dun_level = current_floor_ptr->dun_level;
1321 /* Create connected stairs */
1322 if (!(floor_mode & CFM_NO_RETURN))
1324 /* Extract stair position */
1325 grid_type *g_ptr = ¤t_floor_ptr->grid_array[p_ptr->y][p_ptr->x];
1327 /*** Create connected stairs ***/
1329 /* No stairs down from Quest */
1330 if ((floor_mode & CFM_UP) && !quest_number(current_floor_ptr->dun_level))
1332 g_ptr->feat = (floor_mode & CFM_SHAFT) ? feat_state(feat_down_stair, FF_SHAFT) : feat_down_stair;
1335 /* No stairs up when ironman_downward */
1336 else if ((floor_mode & CFM_DOWN) && !ironman_downward)
1338 g_ptr->feat = (floor_mode & CFM_SHAFT) ? feat_state(feat_up_stair, FF_SHAFT) : feat_up_stair;
1341 /* Paranoia -- Clear mimic */
1344 /* Connect to previous floor */
1345 g_ptr->special = p_ptr->floor_id;
1349 /* Arrive at random grid */
1350 if (floor_mode & (CFM_RAND_PLACE))
1352 (void)new_player_spot();
1355 /* You see stairs blocked */
1356 else if ((floor_mode & CFM_NO_RETURN) && (floor_mode & (CFM_DOWN | CFM_UP)))
1360 msg_print(_("突然階段が塞がれてしまった。", "Suddenly the stairs is blocked!"));
1364 msg_print(_("ゴトゴトと何か音がした。", "You hear some noises."));
1371 * The "current_world_ptr->game_turn" is not always different number because
1372 * the level teleport doesn't take any current_world_ptr->game_turn. Use
1373 * visit mark instead of last visit current_world_ptr->game_turn to find the
1374 * oldest saved floor.
1376 sf_ptr->visit_mark = latest_visit_mark++;
1379 /* Place preserved pet monsters */
1382 /* Reset travel target place */
1383 forget_travel_flow();
1385 /* Hack -- maintain unique and artifacts */
1386 update_unique_artifact(new_floor_id);
1388 /* Now the player is in new floor */
1389 p_ptr->floor_id = new_floor_id;
1391 /* The dungeon is ready */
1392 current_world_ptr->character_dungeon = TRUE;
1394 /* Hack -- Munchkin characters always get whole map */
1395 if (p_ptr->pseikaku == SEIKAKU_MUNCHKIN)
1396 wiz_lite((bool)(p_ptr->pclass == CLASS_NINJA));
1398 /* Remember when this level was "created" */
1399 current_floor_ptr->generated_turn = current_world_ptr->game_turn;
1401 /* No dungeon feeling yet */
1402 p_ptr->feeling_turn = current_floor_ptr->generated_turn;
1405 /* Clear all flags */
1408 select_floor_music();