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 BIT_FLAGS change_floor_mode; /*!<フロア移行処理に関するフラグ / Mode flags for changing floor */
48 static u32b latest_visit_mark; /*!<フロアを渡った回数?(確認中) / Max number of visit_mark */
51 * Number of floor_id used from birth
53 FLOOR_IDX max_floor_id;
56 * Sign for current process used in temporal files.
57 * Actually it is the start time of current process.
59 u32b saved_floor_file_sign;
62 * @brief 保存フロア配列を初期化する / Initialize saved_floors array.
63 * @param force テンポラリファイルが残っていた場合も警告なしで強制的に削除する。
64 * @details Make sure that old temporal files are not remaining as gurbages.
67 void init_saved_floors(bool force)
69 char floor_savefile[1024];
72 BIT_FLAGS mode = 0644;
76 /* Get "games" permissions */
81 for (i = 0; i < MAX_SAVED_FLOORS; i++)
83 saved_floor_type *sf_ptr = &saved_floors[i];
86 sprintf(floor_savefile, "%s.F%02d", savefile, i);
88 /* Grab permissions */
91 /* Try to create the file */
92 fd = fd_make(floor_savefile, mode);
94 /* Drop permissions */
102 msg_print(_("エラー:古いテンポラリ・ファイルが残っています。", "Error: There are old temporal files."));
103 msg_print(_("変愚蛮怒を二重に起動していないか確認してください。", "Make sure you are not running two game processes simultaneously."));
104 msg_print(_("過去に変愚蛮怒がクラッシュした場合は一時ファイルを", "If the temporal files are garbages of old crashed process, "));
105 msg_print(_("強制的に削除して実行を続けられます。", "you can delete it safely."));
106 if (!get_check(_("強制的に削除してもよろしいですか?", "Do you delete old temporal files? ")))
107 quit(_("実行中止", "Aborted."));
117 /* Grab permissions */
120 /* Simply kill the temporal file */
121 (void)fd_kill(floor_savefile);
123 /* Drop permissions */
126 sf_ptr->floor_id = 0;
129 /* No floor_id used yet (No.0 is reserved to indicate non existance) */
132 /* vist_mark is from 1 */
133 latest_visit_mark = 1;
135 /* A sign to mark temporal files */
136 saved_floor_file_sign = (u32b)time(NULL);
138 /* No next floor yet */
141 /* No change floor mode yet */
142 change_floor_mode = 0;
146 /* Drop "games" permissions */
153 * @brief 保存フロア用テンポラリファイルを削除する / Kill temporal files
154 * @details Should be called just before the game quit.
157 void clear_saved_floor_files(void)
159 char floor_savefile[1024];
164 /* Get "games" permissions */
169 for (i = 0; i < MAX_SAVED_FLOORS; i++)
171 saved_floor_type *sf_ptr = &saved_floors[i];
173 /* No temporal file */
174 if (!sf_ptr->floor_id) continue;
175 if (sf_ptr->floor_id == p_ptr->floor_id) continue;
178 sprintf(floor_savefile, "%s.F%02d", savefile, i);
180 /* Grab permissions */
183 /* Simply kill the temporal file */
184 (void)fd_kill(floor_savefile);
186 /* Drop permissions */
192 /* Drop "games" permissions */
199 * @brief 保存フロアIDから参照ポインタを得る / Get a pointer for an item of the saved_floors array.
200 * @param floor_id 保存フロアID
201 * @return IDに対応する保存フロアのポインタ、ない場合はNULLを返す。
203 saved_floor_type *get_sf_ptr(FLOOR_IDX floor_id)
207 /* floor_id No.0 indicates no floor */
208 if (!floor_id) return NULL;
210 for (i = 0; i < MAX_SAVED_FLOORS; i++)
212 saved_floor_type *sf_ptr = &saved_floors[i];
214 if (sf_ptr->floor_id == floor_id) return sf_ptr;
223 * @brief 参照ポインタ先の保存フロアを抹消する / kill a saved floor and get an empty space
224 * @param sf_ptr 保存フロアの参照ポインタ
227 static void kill_saved_floor(saved_floor_type *sf_ptr)
229 char floor_savefile[1024];
233 if (!sf_ptr->floor_id) return;
235 if (sf_ptr->floor_id == p_ptr->floor_id)
237 /* Kill current floor */
240 /* Current floor doesn't have temporal file */
245 sprintf(floor_savefile, "%s.F%02d", savefile, (int)sf_ptr->savefile_id);
247 /* Grab permissions */
250 /* Simply kill the temporal file */
251 (void)fd_kill(floor_savefile);
253 /* Drop permissions */
257 /* No longer exists */
258 sf_ptr->floor_id = 0;
263 * @brief 新規に利用可能な保存フロアを返す / Initialize new saved floor and get its floor id.
264 * @return 利用可能な保存フロアID
266 * If number of saved floors are already MAX_SAVED_FLOORS, kill the oldest one.
268 FLOOR_IDX get_new_floor_id(void)
270 saved_floor_type *sf_ptr = NULL;
273 /* Look for empty space */
274 for (i = 0; i < MAX_SAVED_FLOORS; i++)
276 sf_ptr = &saved_floors[i];
278 if (!sf_ptr->floor_id) break;
282 if (i == MAX_SAVED_FLOORS)
285 u32b oldest_visit = 0xffffffffL;
287 /* Search for oldest */
288 for (i = 0; i < MAX_SAVED_FLOORS; i++)
290 sf_ptr = &saved_floors[i];
292 /* Don't kill current floor */
293 if (sf_ptr->floor_id == p_ptr->floor_id) continue;
295 /* Don't kill newer */
296 if (sf_ptr->visit_mark > oldest_visit) continue;
299 oldest_visit = sf_ptr->visit_mark;
302 /* Kill oldest saved floor */
303 sf_ptr = &saved_floors[oldest];
304 kill_saved_floor(sf_ptr);
310 /* Prepare new floor data */
311 sf_ptr->savefile_id = i;
312 sf_ptr->floor_id = max_floor_id;
313 sf_ptr->last_visit = 0;
314 sf_ptr->upper_floor_id = 0;
315 sf_ptr->lower_floor_id = 0;
316 sf_ptr->visit_mark = latest_visit_mark++;
318 /* sf_ptr->dun_level may be changed later */
319 sf_ptr->dun_level = current_floor_ptr->dun_level;
322 /* Increment number of floor_id */
323 if (max_floor_id < MAX_SHORT) max_floor_id++;
325 /* 32767 floor_ids are all used up! Re-use ancient IDs */
326 else max_floor_id = 1;
328 return sf_ptr->floor_id;
333 * @brief フロア切り替え時の処理フラグを追加する / Prepare mode flags of changing floor
334 * @param mode 追加したい所持フラグ
337 void prepare_change_floor_mode(BIT_FLAGS mode)
339 change_floor_mode |= mode;
343 * @brief 階段移動先のフロアが生成できない時に簡単な行き止まりマップを作成する / Builds the dead end
346 static void build_dead_end(void)
350 /* Clear and empty the current_floor_ptr->grid_array */
353 /* Fill the arrays of floors and walls in the good proportions */
354 set_floor_and_wall(0);
357 current_floor_ptr->height = SCREEN_HGT;
358 current_floor_ptr->width = SCREEN_WID;
360 /* Filled with permanent walls */
361 for (y = 0; y < MAX_HGT; y++)
363 for (x = 0; x < MAX_WID; x++)
365 /* Create "solid" perma-wall */
366 place_solid_perm_bold(y, x);
370 /* Place at center of the floor */
371 p_ptr->y = current_floor_ptr->height / 2;
372 p_ptr->x = current_floor_ptr->width / 2;
374 /* Give one square */
375 place_floor_bold(p_ptr->y, p_ptr->x);
377 wipe_generate_random_floor_flags();
382 #define MAX_PARTY_MON 21 /*!< フロア移動時に先のフロアに連れて行けるペットの最大数 Maximum number of preservable pets */
383 static monster_type party_mon[MAX_PARTY_MON]; /*!< フロア移動に保存するペットモンスターの配列 */
386 * @brief フロア移動時のペット保存処理 / Preserve_pets
389 static void preserve_pet(void)
394 for (num = 0; num < MAX_PARTY_MON; num++)
396 party_mon[num].r_idx = 0;
401 monster_type *m_ptr = ¤t_floor_ptr->m_list[p_ptr->riding];
403 /* Pet of other pet don't follow. */
404 if (m_ptr->parent_m_idx)
407 p_ptr->pet_extra_flags &= ~(PF_RYOUTE);
408 p_ptr->riding_ryoute = p_ptr->old_riding_ryoute = FALSE;
412 /* Preserve the mount */
413 (void)COPY(&party_mon[0], m_ptr, monster_type);
415 /* Delete from this floor */
416 delete_monster_idx(p_ptr->riding);
421 * If player is in wild mode, no pets are preserved
422 * except a monster whom player riding
424 if (!p_ptr->wild_mode && !p_ptr->inside_arena && !p_ptr->inside_battle)
426 for (i = current_floor_ptr->m_max - 1, num = 1; (i >= 1 && num < MAX_PARTY_MON); i--)
428 monster_type *m_ptr = ¤t_floor_ptr->m_list[i];
430 if (!monster_is_valid(m_ptr)) continue;
431 if (!is_pet(m_ptr)) continue;
432 if (i == p_ptr->riding) continue;
434 if (reinit_wilderness)
436 /* Don't lose sight of pets when getting a Quest */
440 POSITION dis = distance(p_ptr->y, p_ptr->x, m_ptr->fy, m_ptr->fx);
442 /* Confused (etc.) monsters don't follow. */
443 if (MON_CONFUSED(m_ptr) || MON_STUNNED(m_ptr) || MON_CSLEEP(m_ptr)) continue;
445 /* Pet of other pet don't follow. */
446 if (m_ptr->parent_m_idx) continue;
449 * Pets with nickname will follow even from 3 blocks away
450 * when you or the pet can see the other.
452 if (m_ptr->nickname &&
453 ((player_has_los_bold(m_ptr->fy, m_ptr->fx) && projectable(p_ptr->y, p_ptr->x, m_ptr->fy, m_ptr->fx)) ||
454 (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))))
456 if (dis > 3) continue;
460 if (dis > 1) continue;
464 (void)COPY(&party_mon[num], ¤t_floor_ptr->m_list[i], monster_type);
468 /* Delete from this floor */
469 delete_monster_idx(i);
473 if (record_named_pet)
475 for (i = current_floor_ptr->m_max - 1; i >=1; i--)
477 monster_type *m_ptr = ¤t_floor_ptr->m_list[i];
478 GAME_TEXT m_name[MAX_NLEN];
480 if (!monster_is_valid(m_ptr)) continue;
481 if (!is_pet(m_ptr)) continue;
482 if (!m_ptr->nickname) continue;
483 if (p_ptr->riding == i) continue;
485 monster_desc(m_name, m_ptr, MD_ASSUME_VISIBLE | MD_INDEF_VISIBLE);
486 do_cmd_write_nikki(NIKKI_NAMED_PET, RECORD_NAMED_PET_MOVED, m_name);
491 /* Pet of other pet may disappear. */
492 for (i = current_floor_ptr->m_max - 1; i >=1; i--)
494 monster_type *m_ptr = ¤t_floor_ptr->m_list[i];
496 /* Are there its parent? */
497 if (m_ptr->parent_m_idx && !current_floor_ptr->m_list[m_ptr->parent_m_idx].r_idx)
499 /* Its parent have gone, it also goes away. */
503 GAME_TEXT m_name[MAX_NLEN];
504 monster_desc(m_name, m_ptr, 0);
505 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
508 delete_monster_idx(i);
515 * @brief フロア移動時にペットを伴った場合の準備処理 / Pre-calculate the racial counters of preserved pets
518 * To prevent multiple generation of unique monster who is the minion of player
520 void precalc_cur_num_of_pet(void)
524 int max_num = p_ptr->wild_mode ? 1 : MAX_PARTY_MON;
526 for (i = 0; i < max_num; i++)
528 m_ptr = &party_mon[i];
530 /* Skip empty monsters */
531 if (!monster_is_valid(m_ptr)) continue;
533 /* Hack -- Increase the racial counter */
534 real_r_ptr(m_ptr)->cur_num++;
539 * @brief 移動先のフロアに伴ったペットを配置する / Place preserved pet monsters on new floor
542 static void place_pet(void)
545 int max_num = p_ptr->wild_mode ? 1 : MAX_PARTY_MON;
547 for (i = 0; i < max_num; i++)
549 POSITION cy = 0, cx = 0;
552 if (!(party_mon[i].r_idx)) continue;
557 p_ptr->riding = m_idx;
569 for (d = 1; d < A_MAX; d++)
571 for (j = 1000; j > 0; j--)
573 scatter(&cy, &cx, p_ptr->y, p_ptr->x, d, 0);
574 if (monster_can_enter(cy, cx, &r_info[party_mon[i].r_idx], 0)) break;
578 m_idx = (d == 6) ? 0 : m_pop();
583 monster_type *m_ptr = ¤t_floor_ptr->m_list[m_idx];
586 current_floor_ptr->grid_array[cy][cx].m_idx = m_idx;
588 m_ptr->r_idx = party_mon[i].r_idx;
590 /* Copy all member of the structure */
591 *m_ptr = party_mon[i];
592 r_ptr = real_r_ptr(m_ptr);
597 m_ptr->mtimed[MTIMED_CSLEEP] = 0;
598 m_ptr->hold_o_idx = 0;
601 if ((r_ptr->flags1 & RF1_FORCE_SLEEP) && !ironman_nightmare)
603 /* Monster is still being nice */
604 m_ptr->mflag |= (MFLAG_NICE);
606 /* Must repair monsters */
607 repair_monsters = TRUE;
609 update_monster(m_idx, TRUE);
612 /* Pre-calculated in precalc_cur_num_of_pet() */
613 /* r_ptr->cur_num++; */
615 /* Hack -- Count the number of "reproducers" */
616 if (r_ptr->flags2 & RF2_MULTIPLY) current_floor_ptr->num_repro++;
621 monster_type *m_ptr = &party_mon[i];
622 monster_race *r_ptr = real_r_ptr(m_ptr);
623 GAME_TEXT m_name[MAX_NLEN];
625 monster_desc(m_name, m_ptr, 0);
626 msg_format(_("%sとはぐれてしまった。", "You have lost sight of %s."), m_name);
627 if (record_named_pet && m_ptr->nickname)
629 monster_desc(m_name, m_ptr, MD_INDEF_VISIBLE);
630 do_cmd_write_nikki(NIKKI_NAMED_PET, RECORD_NAMED_PET_LOST_SIGHT, m_name);
633 /* Pre-calculated in precalc_cur_num_of_pet(), but need to decrease */
634 if (r_ptr->cur_num) r_ptr->cur_num--;
638 /* For accuracy of precalc_cur_num_of_pet() */
639 (void)C_WIPE(party_mon, MAX_PARTY_MON, monster_type);
644 * @brief ユニークモンスターやアーティファクトの所在フロアを更新する / Hack -- Update location of unique monsters and artifacts
645 * @param cur_floor_id 現在のフロアID
648 * The r_ptr->floor_id and a_ptr->floor_id are not updated correctly\n
649 * while new floor creation since dungeons may be re-created by\n
650 * auto-scum option.\n
652 static void update_unique_artifact(s16b cur_floor_id)
656 /* Maintain unique monsters */
657 for (i = 1; i < current_floor_ptr->m_max; i++)
660 monster_type *m_ptr = ¤t_floor_ptr->m_list[i];
662 if (!monster_is_valid(m_ptr)) continue;
664 /* Extract real monster race */
665 r_ptr = real_r_ptr(m_ptr);
667 /* Memorize location of the unique monster */
668 if ((r_ptr->flags1 & RF1_UNIQUE) ||
669 (r_ptr->flags7 & RF7_NAZGUL))
671 r_ptr->floor_id = cur_floor_id;
675 /* Maintain artifatcs */
676 for (i = 1; i < current_floor_ptr->o_max; i++)
678 object_type *o_ptr = ¤t_floor_ptr->o_list[i];
680 if (!OBJECT_IS_VALID(o_ptr)) continue;
682 /* Memorize location of the artifact */
683 if (object_is_fixed_artifact(o_ptr))
685 a_info[o_ptr->name1].floor_id = cur_floor_id;
692 * @brief フロア移動時、プレイヤーの移動先モンスターが既にいた場合ランダムな近隣に移動させる / When a monster is at a place where player will return,
695 static void get_out_monster(void)
699 POSITION oy = p_ptr->y;
700 POSITION ox = p_ptr->x;
701 MONSTER_IDX m_idx = current_floor_ptr->grid_array[oy][ox].m_idx;
703 /* Nothing to do if no monster */
706 /* Look until done */
711 /* Pick a (possibly illegal) location */
712 POSITION ny = rand_spread(oy, dis);
713 POSITION nx = rand_spread(ox, dis);
717 /* Stop after 1000 tries */
718 if (tries > 10000) return;
721 * Increase distance after doing enough tries
722 * compared to area of possible space
724 if (tries > 20 * dis * dis) dis++;
726 /* Ignore illegal locations */
727 if (!in_bounds(ny, nx)) continue;
729 /* Require "empty" floor space */
730 if (!cave_empty_bold(ny, nx)) continue;
732 /* Hack -- no teleport onto glyph of warding */
733 if (is_glyph_grid(¤t_floor_ptr->grid_array[ny][nx])) continue;
734 if (is_explosive_rune_grid(¤t_floor_ptr->grid_array[ny][nx])) continue;
736 /* ...nor onto the Pattern */
737 if (pattern_tile(ny, nx)) continue;
739 /*** It's a good place ***/
741 m_ptr = ¤t_floor_ptr->m_list[m_idx];
743 /* Update the old location */
744 current_floor_ptr->grid_array[oy][ox].m_idx = 0;
746 /* Update the new location */
747 current_floor_ptr->grid_array[ny][nx].m_idx = m_idx;
749 /* Move the monster */
753 /* No need to do update_monster() */
761 * @brief 新フロアに移動元フロアに繋がる階段を配置する / Virtually teleport onto the stairs that is connecting between two floors.
762 * @param sf_ptr 移動元の保存フロア構造体参照ポインタ
765 static void locate_connected_stairs(saved_floor_type *sf_ptr, BIT_FLAGS prev_floor_mode)
767 POSITION x, y, sx = 0, sy = 0;
768 POSITION x_table[20];
769 POSITION y_table[20];
773 /* Search usable stairs */
774 for (y = 0; y < current_floor_ptr->height; y++)
776 for (x = 0; x < current_floor_ptr->width; x++)
778 grid_type *g_ptr = ¤t_floor_ptr->grid_array[y][x];
779 feature_type *f_ptr = &f_info[g_ptr->feat];
782 if (prev_floor_mode & CFM_UP)
784 if (have_flag(f_ptr->flags, FF_LESS) && have_flag(f_ptr->flags, FF_STAIRS) &&
785 !have_flag(f_ptr->flags, FF_SPECIAL))
789 /* Found fixed stairs? */
790 if (g_ptr->special &&
791 g_ptr->special == sf_ptr->upper_floor_id)
799 else if (prev_floor_mode & CFM_DOWN)
801 if (have_flag(f_ptr->flags, FF_MORE) && have_flag(f_ptr->flags, FF_STAIRS) &&
802 !have_flag(f_ptr->flags, FF_SPECIAL))
806 /* Found fixed stairs */
807 if (g_ptr->special &&
808 g_ptr->special == sf_ptr->lower_floor_id)
818 if (have_flag(f_ptr->flags, FF_BLDG))
824 if (ok && (num < 20))
841 /* No stairs found! -- No return */
842 prepare_change_floor_mode(CFM_RAND_PLACE | CFM_NO_RETURN);
844 /* Mega Hack -- It's not the stairs you enter. Disable it. */
845 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;
849 /* Choose random one */
852 /* Point stair location */
853 p_ptr->y = y_table[i];
854 p_ptr->x = x_table[i];
859 * @brief 現在のフロアを離れるに伴って行なわれる保存処理
860 * / Maintain quest monsters, mark next floor_id at stairs, save current floor, and prepare to enter next floor.
863 void leave_floor(void)
865 grid_type *g_ptr = NULL;
867 saved_floor_type *sf_ptr;
868 MONRACE_IDX quest_r_idx = 0;
871 /* Preserve pets and prepare to take these to next floor */
874 /* Remove all mirrors without explosion */
875 remove_all_mirrors(FALSE);
877 if (p_ptr->special_defense & NINJA_S_STEALTH) set_superstealth(FALSE);
879 /* New floor is not yet prepared */
882 /* Temporary get a floor_id (for Arena) */
883 if (!p_ptr->floor_id &&
884 (change_floor_mode & CFM_SAVE_FLOORS) &&
885 !(change_floor_mode & CFM_NO_RETURN))
887 /* Get temporal floor_id */
888 p_ptr->floor_id = get_new_floor_id();
891 /* Search the quest monster index */
892 for (i = 0; i < max_q_idx; i++)
894 if ((quest[i].status == QUEST_STATUS_TAKEN) &&
895 ((quest[i].type == QUEST_TYPE_KILL_LEVEL) ||
896 (quest[i].type == QUEST_TYPE_RANDOM)) &&
897 (quest[i].level == current_floor_ptr->dun_level) &&
898 (p_ptr->dungeon_idx == quest[i].dungeon) &&
899 !(quest[i].flags & QUEST_FLAG_PRESET))
901 quest_r_idx = quest[i].r_idx;
905 /* Maintain quest monsters */
906 for (i = 1; i < current_floor_ptr->m_max; i++)
909 monster_type *m_ptr = ¤t_floor_ptr->m_list[i];
911 if (!monster_is_valid(m_ptr)) continue;
913 /* Only maintain quest monsters */
914 if (quest_r_idx != m_ptr->r_idx) continue;
916 /* Extract real monster race */
917 r_ptr = real_r_ptr(m_ptr);
919 /* Ignore unique monsters */
920 if ((r_ptr->flags1 & RF1_UNIQUE) ||
921 (r_ptr->flags7 & RF7_NAZGUL)) continue;
923 /* Delete non-unique quest monsters */
924 delete_monster_idx(i);
927 /* Check if there is a same item */
928 for (i = 0; i < INVEN_PACK; i++)
930 object_type *o_ptr = &p_ptr->inventory_list[i];
932 if (!OBJECT_IS_VALID(o_ptr)) continue;
934 /* Delete old memorized location of the artifact */
935 if (object_is_fixed_artifact(o_ptr))
937 a_info[o_ptr->name1].floor_id = 0;
941 /* Extract current floor info or NULL */
942 sf_ptr = get_sf_ptr(p_ptr->floor_id);
944 /* Choose random stairs */
945 if ((change_floor_mode & CFM_RAND_CONNECT) && p_ptr->floor_id)
947 locate_connected_stairs(sf_ptr, change_floor_mode);
950 /* Extract new dungeon level */
951 if (change_floor_mode & CFM_SAVE_FLOORS)
953 /* Extract stair position */
954 g_ptr = ¤t_floor_ptr->grid_array[p_ptr->y][p_ptr->x];
955 f_ptr = &f_info[g_ptr->feat];
957 /* Get back to old saved floor? */
958 if (g_ptr->special && !have_flag(f_ptr->flags, FF_SPECIAL) && get_sf_ptr(g_ptr->special))
960 /* Saved floor is exist. Use it. */
961 new_floor_id = g_ptr->special;
964 /* Mark shaft up/down */
965 if (have_flag(f_ptr->flags, FF_STAIRS) && have_flag(f_ptr->flags, FF_SHAFT))
967 prepare_change_floor_mode(CFM_SHAFT);
971 /* Climb up/down some sort of stairs */
972 if (change_floor_mode & (CFM_DOWN | CFM_UP))
976 /* Extract level movement number */
977 if (change_floor_mode & CFM_DOWN) move_num = 1;
978 else if (change_floor_mode & CFM_UP) move_num = -1;
980 /* Shafts are deeper than normal stairs */
981 if (change_floor_mode & CFM_SHAFT)
982 move_num += SGN(move_num);
984 /* Get out from or Enter the dungeon */
985 if (change_floor_mode & CFM_DOWN)
987 if (!current_floor_ptr->dun_level)
988 move_num = d_info[p_ptr->dungeon_idx].mindepth;
990 else if (change_floor_mode & CFM_UP)
992 if (current_floor_ptr->dun_level + move_num < d_info[p_ptr->dungeon_idx].mindepth)
993 move_num = -current_floor_ptr->dun_level;
996 current_floor_ptr->dun_level += move_num;
999 /* Leaving the dungeon to town */
1000 if (!current_floor_ptr->dun_level && p_ptr->dungeon_idx)
1002 p_ptr->leaving_dungeon = TRUE;
1003 if (!vanilla_town && !lite_town)
1005 p_ptr->wilderness_y = d_info[p_ptr->dungeon_idx].dy;
1006 p_ptr->wilderness_x = d_info[p_ptr->dungeon_idx].dx;
1008 p_ptr->recall_dungeon = p_ptr->dungeon_idx;
1009 p_ptr->dungeon_idx = 0;
1011 /* Reach to the surface -- Clear all saved floors */
1012 change_floor_mode &= ~CFM_SAVE_FLOORS;
1015 /* Kill some old saved floors */
1016 if (!(change_floor_mode & CFM_SAVE_FLOORS))
1018 /* Kill all saved floors */
1019 for (i = 0; i < MAX_SAVED_FLOORS; i++)
1020 kill_saved_floor(&saved_floors[i]);
1022 /* Reset visit_mark count */
1023 latest_visit_mark = 1;
1025 else if (change_floor_mode & CFM_NO_RETURN)
1027 /* Kill current floor */
1028 kill_saved_floor(sf_ptr);
1031 /* No current floor -- Left/Enter dungeon etc... */
1032 if (!p_ptr->floor_id)
1034 /* No longer need to save current floor */
1039 /* Mark next floor_id on the previous floor */
1043 new_floor_id = get_new_floor_id();
1045 /* Connect from here */
1046 if (g_ptr && !feat_uses_special(g_ptr->feat))
1048 g_ptr->special = new_floor_id;
1052 /* Fix connection -- level teleportation or trap door */
1053 if (change_floor_mode & CFM_RAND_CONNECT)
1055 if (change_floor_mode & CFM_UP)
1056 sf_ptr->upper_floor_id = new_floor_id;
1057 else if (change_floor_mode & CFM_DOWN)
1058 sf_ptr->lower_floor_id = new_floor_id;
1061 /* If you can return, you need to save previous floor */
1062 if ((change_floor_mode & CFM_SAVE_FLOORS) &&
1063 !(change_floor_mode & CFM_NO_RETURN))
1065 /* Get out of the my way! */
1068 /* Record the last visit current_world_ptr->game_turn of current floor */
1069 sf_ptr->last_visit = current_world_ptr->game_turn;
1075 /* Save current floor */
1076 if (!save_floor(sf_ptr, 0))
1078 /* Save failed -- No return */
1079 prepare_change_floor_mode(CFM_NO_RETURN);
1081 /* Kill current floor */
1082 kill_saved_floor(get_sf_ptr(p_ptr->floor_id));
1089 * @brief フロアの切り替え処理 / Enter new floor.
1092 * If the floor is an old saved floor, it will be\n
1093 * restored from the temporal file. If the floor is new one, new current_floor_ptr->grid_array\n
1094 * will be generated.\n
1096 void change_floor(BIT_FLAGS next_floor_mode)
1098 saved_floor_type *sf_ptr;
1099 bool loaded = FALSE;
1101 /* The dungeon is not ready */
1102 current_world_ptr->character_dungeon = FALSE;
1104 /* No longer in the trap detecteded region */
1105 p_ptr->dtrap = FALSE;
1107 /* Mega-Hack -- no panel yet */
1113 /* Mega-Hack -- not ambushed on the wildness? */
1114 p_ptr->ambush_flag = FALSE;
1116 /* No saved floors (On the surface etc.) */
1117 if (!(next_floor_mode & CFM_SAVE_FLOORS) &&
1118 !(next_floor_mode & CFM_FIRST_FLOOR))
1120 /* Create current_floor_ptr->grid_array */
1121 generate_random_floor();
1123 /* Paranoia -- No new saved floor */
1127 /* In the dungeon */
1130 /* No floor_id yet */
1134 new_floor_id = get_new_floor_id();
1137 /* Pointer for infomations of new floor */
1138 sf_ptr = get_sf_ptr(new_floor_id);
1140 /* Try to restore old floor */
1141 if (sf_ptr->last_visit)
1143 /* Old saved floor is exist */
1144 if (load_floor(sf_ptr, 0))
1148 /* Forbid return stairs */
1149 if (next_floor_mode & CFM_NO_RETURN)
1151 grid_type *g_ptr = ¤t_floor_ptr->grid_array[p_ptr->y][p_ptr->x];
1153 if (!feat_uses_special(g_ptr->feat))
1155 if (next_floor_mode & (CFM_DOWN | CFM_UP))
1157 /* Reset to floor */
1158 g_ptr->feat = feat_ground_type[randint0(100)];
1168 * Set lower/upper_floor_id of new floor when the new
1169 * floor is right-above/right-under the current floor.
1171 * Stair creation/Teleport level/Trap door will take
1172 * you the same floor when you used it later again.
1174 if (p_ptr->floor_id)
1176 saved_floor_type *cur_sf_ptr = get_sf_ptr(p_ptr->floor_id);
1178 if (next_floor_mode & CFM_UP)
1180 /* New floor is right-above */
1181 if (cur_sf_ptr->upper_floor_id == new_floor_id)
1182 sf_ptr->lower_floor_id = p_ptr->floor_id;
1184 else if (next_floor_mode & CFM_DOWN)
1186 /* New floor is right-under */
1187 if (cur_sf_ptr->lower_floor_id == new_floor_id)
1188 sf_ptr->upper_floor_id = p_ptr->floor_id;
1192 /* Break connection to killed floor */
1195 if (next_floor_mode & CFM_UP)
1196 sf_ptr->lower_floor_id = 0;
1197 else if (next_floor_mode & CFM_DOWN)
1198 sf_ptr->upper_floor_id = 0;
1201 /* Maintain monsters and artifacts */
1205 GAME_TURN tmp_last_visit = sf_ptr->last_visit;
1206 GAME_TURN absence_ticks;
1207 int alloc_chance = d_info[p_ptr->dungeon_idx].max_m_alloc_chance;
1208 GAME_TURN alloc_times;
1210 while (tmp_last_visit > current_world_ptr->game_turn) tmp_last_visit -= TURNS_PER_TICK * TOWN_DAWN;
1211 absence_ticks = (current_world_ptr->game_turn - tmp_last_visit) / TURNS_PER_TICK;
1213 /* Maintain monsters */
1214 for (i = 1; i < current_floor_ptr->m_max; i++)
1216 monster_race *r_ptr;
1217 monster_type *m_ptr = ¤t_floor_ptr->m_list[i];
1219 if (!monster_is_valid(m_ptr)) continue;
1224 m_ptr->hp = m_ptr->maxhp = m_ptr->max_maxhp;
1226 /* Remove timed status (except MTIMED_CSLEEP) */
1227 (void)set_monster_fast(i, 0);
1228 (void)set_monster_slow(i, 0);
1229 (void)set_monster_stunned(i, 0);
1230 (void)set_monster_confused(i, 0);
1231 (void)set_monster_monfear(i, 0);
1232 (void)set_monster_invulner(i, 0, FALSE);
1235 /* Extract real monster race */
1236 r_ptr = real_r_ptr(m_ptr);
1238 /* Ignore non-unique */
1239 if (!(r_ptr->flags1 & RF1_UNIQUE) &&
1240 !(r_ptr->flags7 & RF7_NAZGUL)) continue;
1242 /* Appear at a different floor? */
1243 if (r_ptr->floor_id != new_floor_id)
1245 /* Disapper from here */
1246 delete_monster_idx(i);
1250 /* Maintain artifatcs */
1251 for (i = 1; i < current_floor_ptr->o_max; i++)
1253 object_type *o_ptr = ¤t_floor_ptr->o_list[i];
1255 if (!OBJECT_IS_VALID(o_ptr)) continue;
1257 /* Ignore non-artifact */
1258 if (!object_is_fixed_artifact(o_ptr)) continue;
1260 /* Appear at a different floor? */
1261 if (a_info[o_ptr->name1].floor_id != new_floor_id)
1263 /* Disappear from here */
1264 delete_object_idx(i);
1268 /* Cancel preserve */
1269 a_info[o_ptr->name1].cur_num = 1;
1273 (void)place_quest_monsters();
1275 /* Place some random monsters */
1276 alloc_times = absence_ticks / alloc_chance;
1278 if (randint0(alloc_chance) < (absence_ticks % alloc_chance))
1281 for (i = 0; i < alloc_times; i++)
1283 /* Make a (group of) new monster */
1284 (void)alloc_monster(0, 0);
1289 /* New floor_id or failed to restore */
1290 else /* if (!loaded) */
1292 if (sf_ptr->last_visit)
1294 /* Temporal file is broken? */
1295 msg_print(_("階段は行き止まりだった。", "The staircases come to a dead end..."));
1297 /* Create simple dead end */
1300 /* Break connection */
1301 if (next_floor_mode & CFM_UP)
1303 sf_ptr->upper_floor_id = 0;
1305 else if (next_floor_mode & CFM_DOWN)
1307 sf_ptr->lower_floor_id = 0;
1312 /* Newly create current_floor_ptr->grid_array */
1313 generate_random_floor();
1316 /* Record last visit current_world_ptr->game_turn */
1317 sf_ptr->last_visit = current_world_ptr->game_turn;
1319 /* Set correct current_floor_ptr->dun_level value */
1320 sf_ptr->dun_level = current_floor_ptr->dun_level;
1322 /* Create connected stairs */
1323 if (!(next_floor_mode & CFM_NO_RETURN))
1325 /* Extract stair position */
1326 grid_type *g_ptr = ¤t_floor_ptr->grid_array[p_ptr->y][p_ptr->x];
1328 /*** Create connected stairs ***/
1330 /* No stairs down from Quest */
1331 if ((next_floor_mode & CFM_UP) && !quest_number(current_floor_ptr->dun_level))
1333 g_ptr->feat = (next_floor_mode & CFM_SHAFT) ? feat_state(feat_down_stair, FF_SHAFT) : feat_down_stair;
1336 /* No stairs up when ironman_downward */
1337 else if ((next_floor_mode & CFM_DOWN) && !ironman_downward)
1339 g_ptr->feat = (next_floor_mode & CFM_SHAFT) ? feat_state(feat_up_stair, FF_SHAFT) : feat_up_stair;
1342 /* Paranoia -- Clear mimic */
1345 /* Connect to previous floor */
1346 g_ptr->special = p_ptr->floor_id;
1350 /* Arrive at random grid */
1351 if (next_floor_mode & (CFM_RAND_PLACE))
1353 (void)new_player_spot();
1356 /* You see stairs blocked */
1357 else if ((next_floor_mode & CFM_NO_RETURN) && (next_floor_mode & (CFM_DOWN | CFM_UP)))
1361 msg_print(_("突然階段が塞がれてしまった。", "Suddenly the stairs is blocked!"));
1365 msg_print(_("ゴトゴトと何か音がした。", "You hear some noises."));
1372 * The "current_world_ptr->game_turn" is not always different number because
1373 * the level teleport doesn't take any current_world_ptr->game_turn. Use
1374 * visit mark instead of last visit current_world_ptr->game_turn to find the
1375 * oldest saved floor.
1377 sf_ptr->visit_mark = latest_visit_mark++;
1380 /* Place preserved pet monsters */
1383 /* Reset travel target place */
1384 forget_travel_flow();
1386 /* Hack -- maintain unique and artifacts */
1387 update_unique_artifact(new_floor_id);
1389 /* Now the player is in new floor */
1390 p_ptr->floor_id = new_floor_id;
1392 /* The dungeon is ready */
1393 current_world_ptr->character_dungeon = TRUE;
1395 /* Hack -- Munchkin characters always get whole map */
1396 if (p_ptr->pseikaku == SEIKAKU_MUNCHKIN)
1397 wiz_lite((bool)(p_ptr->pclass == CLASS_NINJA));
1399 /* Remember when this level was "created" */
1400 current_floor_ptr->generated_turn = current_world_ptr->game_turn;
1402 /* No dungeon feeling yet */
1403 p_ptr->feeling_turn = current_floor_ptr->generated_turn;
1406 /* Clear all flags */
1407 next_floor_mode = 0L;
1409 select_floor_music();