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(floor_type *floor_ptr)
349 clear_cave(floor_ptr);
351 /* Fill the arrays of floors and walls in the good proportions */
352 set_floor_and_wall(0);
355 floor_ptr->height = SCREEN_HGT;
356 floor_ptr->width = SCREEN_WID;
358 /* Filled with permanent walls */
359 for (y = 0; y < MAX_HGT; y++)
361 for (x = 0; x < MAX_WID; x++)
363 /* Create "solid" perma-wall */
364 place_solid_perm_bold(y, x);
368 /* Place at center of the floor */
369 p_ptr->y = floor_ptr->height / 2;
370 p_ptr->x = floor_ptr->width / 2;
372 /* Give one square */
373 place_floor_bold(p_ptr->y, p_ptr->x);
375 wipe_generate_random_floor_flags(floor_ptr);
380 #define MAX_PARTY_MON 21 /*!< フロア移動時に先のフロアに連れて行けるペットの最大数 Maximum number of preservable pets */
381 static monster_type party_mon[MAX_PARTY_MON]; /*!< フロア移動に保存するペットモンスターの配列 */
384 * @brief フロア移動時のペット保存処理 / Preserve_pets
387 static void preserve_pet(void)
392 for (num = 0; num < MAX_PARTY_MON; num++)
394 party_mon[num].r_idx = 0;
399 monster_type *m_ptr = ¤t_floor_ptr->m_list[p_ptr->riding];
401 /* Pet of other pet don't follow. */
402 if (m_ptr->parent_m_idx)
405 p_ptr->pet_extra_flags &= ~(PF_RYOUTE);
406 p_ptr->riding_ryoute = p_ptr->old_riding_ryoute = FALSE;
410 /* Preserve the mount */
411 (void)COPY(&party_mon[0], m_ptr, monster_type);
413 /* Delete from this floor */
414 delete_monster_idx(p_ptr->riding);
419 * If player is in wild mode, no pets are preserved
420 * except a monster whom player riding
422 if (!p_ptr->wild_mode && !p_ptr->inside_arena && !p_ptr->phase_out)
424 for (i = current_floor_ptr->m_max - 1, num = 1; (i >= 1 && num < MAX_PARTY_MON); i--)
426 monster_type *m_ptr = ¤t_floor_ptr->m_list[i];
428 if (!monster_is_valid(m_ptr)) continue;
429 if (!is_pet(m_ptr)) continue;
430 if (i == p_ptr->riding) continue;
432 if (reinit_wilderness)
434 /* Don't lose sight of pets when getting a Quest */
438 POSITION dis = distance(p_ptr->y, p_ptr->x, m_ptr->fy, m_ptr->fx);
440 /* Confused (etc.) monsters don't follow. */
441 if (MON_CONFUSED(m_ptr) || MON_STUNNED(m_ptr) || MON_CSLEEP(m_ptr)) continue;
443 /* Pet of other pet don't follow. */
444 if (m_ptr->parent_m_idx) continue;
447 * Pets with nickname will follow even from 3 blocks away
448 * when you or the pet can see the other.
450 if (m_ptr->nickname &&
451 ((player_has_los_bold(m_ptr->fy, m_ptr->fx) && projectable(p_ptr->y, p_ptr->x, m_ptr->fy, m_ptr->fx)) ||
452 (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))))
454 if (dis > 3) continue;
458 if (dis > 1) continue;
462 (void)COPY(&party_mon[num], ¤t_floor_ptr->m_list[i], monster_type);
466 /* Delete from this floor */
467 delete_monster_idx(i);
471 if (record_named_pet)
473 for (i = current_floor_ptr->m_max - 1; i >=1; i--)
475 monster_type *m_ptr = ¤t_floor_ptr->m_list[i];
476 GAME_TEXT m_name[MAX_NLEN];
478 if (!monster_is_valid(m_ptr)) continue;
479 if (!is_pet(m_ptr)) continue;
480 if (!m_ptr->nickname) continue;
481 if (p_ptr->riding == i) continue;
483 monster_desc(m_name, m_ptr, MD_ASSUME_VISIBLE | MD_INDEF_VISIBLE);
484 exe_write_diary(p_ptr, NIKKI_NAMED_PET, RECORD_NAMED_PET_MOVED, m_name);
489 /* Pet of other pet may disappear. */
490 for (i = current_floor_ptr->m_max - 1; i >=1; i--)
492 monster_type *m_ptr = ¤t_floor_ptr->m_list[i];
494 /* Are there its parent? */
495 if (m_ptr->parent_m_idx && !current_floor_ptr->m_list[m_ptr->parent_m_idx].r_idx)
497 /* Its parent have gone, it also goes away. */
501 GAME_TEXT m_name[MAX_NLEN];
502 monster_desc(m_name, m_ptr, 0);
503 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
506 delete_monster_idx(i);
513 * @brief フロア移動時にペットを伴った場合の準備処理 / Pre-calculate the racial counters of preserved pets
516 * To prevent multiple generation of unique monster who is the minion of player
518 void precalc_cur_num_of_pet(void)
522 int max_num = p_ptr->wild_mode ? 1 : MAX_PARTY_MON;
524 for (i = 0; i < max_num; i++)
526 m_ptr = &party_mon[i];
528 /* Skip empty monsters */
529 if (!monster_is_valid(m_ptr)) continue;
531 /* Hack -- Increase the racial counter */
532 real_r_ptr(m_ptr)->cur_num++;
537 * @brief 移動先のフロアに伴ったペットを配置する / Place preserved pet monsters on new floor
540 static void place_pet(player_type *master_ptr)
543 int max_num = master_ptr->wild_mode ? 1 : MAX_PARTY_MON;
545 for (i = 0; i < max_num; i++)
547 POSITION cy = 0, cx = 0;
550 if (!(party_mon[i].r_idx)) continue;
555 master_ptr->riding = m_idx;
567 for (d = 1; d < A_MAX; d++)
569 for (j = 1000; j > 0; j--)
571 scatter(&cy, &cx, master_ptr->y, master_ptr->x, d, 0);
572 if (monster_can_enter(cy, cx, &r_info[party_mon[i].r_idx], 0)) break;
576 m_idx = (d == 6) ? 0 : m_pop();
581 monster_type *m_ptr = ¤t_floor_ptr->m_list[m_idx];
584 current_floor_ptr->grid_array[cy][cx].m_idx = m_idx;
586 m_ptr->r_idx = party_mon[i].r_idx;
588 /* Copy all member of the structure */
589 *m_ptr = party_mon[i];
590 r_ptr = real_r_ptr(m_ptr);
595 m_ptr->mtimed[MTIMED_CSLEEP] = 0;
596 m_ptr->hold_o_idx = 0;
599 if ((r_ptr->flags1 & RF1_FORCE_SLEEP) && !ironman_nightmare)
601 /* Monster is still being nice */
602 m_ptr->mflag |= (MFLAG_NICE);
604 /* Must repair monsters */
605 repair_monsters = TRUE;
607 update_monster(m_idx, TRUE);
610 /* Pre-calculated in precalc_cur_num_of_pet() */
611 /* r_ptr->cur_num++; */
613 /* Hack -- Count the number of "reproducers" */
614 if (r_ptr->flags2 & RF2_MULTIPLY) current_floor_ptr->num_repro++;
619 monster_type *m_ptr = &party_mon[i];
620 monster_race *r_ptr = real_r_ptr(m_ptr);
621 GAME_TEXT m_name[MAX_NLEN];
623 monster_desc(m_name, m_ptr, 0);
624 msg_format(_("%sとはぐれてしまった。", "You have lost sight of %s."), m_name);
625 if (record_named_pet && m_ptr->nickname)
627 monster_desc(m_name, m_ptr, MD_INDEF_VISIBLE);
628 exe_write_diary(master_ptr, NIKKI_NAMED_PET, RECORD_NAMED_PET_LOST_SIGHT, m_name);
631 /* Pre-calculated in precalc_cur_num_of_pet(), but need to decrease */
632 if (r_ptr->cur_num) r_ptr->cur_num--;
636 /* For accuracy of precalc_cur_num_of_pet() */
637 (void)C_WIPE(party_mon, MAX_PARTY_MON, monster_type);
642 * @brief ユニークモンスターやアーティファクトの所在フロアを更新する / Hack -- Update location of unique monsters and artifacts
643 * @param cur_floor_id 現在のフロアID
646 * The r_ptr->floor_id and a_ptr->floor_id are not updated correctly\n
647 * while new floor creation since dungeons may be re-created by\n
648 * auto-scum option.\n
650 static void update_unique_artifact(s16b cur_floor_id)
654 /* Maintain unique monsters */
655 for (i = 1; i < current_floor_ptr->m_max; i++)
658 monster_type *m_ptr = ¤t_floor_ptr->m_list[i];
660 if (!monster_is_valid(m_ptr)) continue;
662 /* Extract real monster race */
663 r_ptr = real_r_ptr(m_ptr);
665 /* Memorize location of the unique monster */
666 if ((r_ptr->flags1 & RF1_UNIQUE) ||
667 (r_ptr->flags7 & RF7_NAZGUL))
669 r_ptr->floor_id = cur_floor_id;
673 /* Maintain artifatcs */
674 for (i = 1; i < current_floor_ptr->o_max; i++)
676 object_type *o_ptr = ¤t_floor_ptr->o_list[i];
678 if (!OBJECT_IS_VALID(o_ptr)) continue;
680 /* Memorize location of the artifact */
681 if (object_is_fixed_artifact(o_ptr))
683 a_info[o_ptr->name1].floor_id = cur_floor_id;
690 * @brief フロア移動時、プレイヤーの移動先モンスターが既にいた場合ランダムな近隣に移動させる / When a monster is at a place where player will return,
693 static void get_out_monster(void)
697 POSITION oy = p_ptr->y;
698 POSITION ox = p_ptr->x;
699 MONSTER_IDX m_idx = current_floor_ptr->grid_array[oy][ox].m_idx;
701 /* Nothing to do if no monster */
704 /* Look until done */
709 /* Pick a (possibly illegal) location */
710 POSITION ny = rand_spread(oy, dis);
711 POSITION nx = rand_spread(ox, dis);
715 /* Stop after 1000 tries */
716 if (tries > 10000) return;
719 * Increase distance after doing enough tries
720 * compared to area of possible space
722 if (tries > 20 * dis * dis) dis++;
724 /* Ignore illegal locations */
725 if (!in_bounds(current_floor_ptr, ny, nx)) continue;
727 /* Require "empty" floor space */
728 if (!cave_empty_bold(ny, nx)) continue;
730 /* Hack -- no teleport onto glyph of warding */
731 if (is_glyph_grid(¤t_floor_ptr->grid_array[ny][nx])) continue;
732 if (is_explosive_rune_grid(¤t_floor_ptr->grid_array[ny][nx])) continue;
734 /* ...nor onto the Pattern */
735 if (pattern_tile(ny, nx)) continue;
737 /*** It's a good place ***/
739 m_ptr = ¤t_floor_ptr->m_list[m_idx];
741 /* Update the old location */
742 current_floor_ptr->grid_array[oy][ox].m_idx = 0;
744 /* Update the new location */
745 current_floor_ptr->grid_array[ny][nx].m_idx = m_idx;
747 /* Move the monster */
751 /* No need to do update_monster() */
759 * @brief 新フロアに移動元フロアに繋がる階段を配置する / Virtually teleport onto the stairs that is connecting between two floors.
760 * @param sf_ptr 移動元の保存フロア構造体参照ポインタ
763 static void locate_connected_stairs(saved_floor_type *sf_ptr, BIT_FLAGS floor_mode)
765 POSITION x, y, sx = 0, sy = 0;
766 POSITION x_table[20];
767 POSITION y_table[20];
771 /* Search usable stairs */
772 for (y = 0; y < current_floor_ptr->height; y++)
774 for (x = 0; x < current_floor_ptr->width; x++)
776 grid_type *g_ptr = ¤t_floor_ptr->grid_array[y][x];
777 feature_type *f_ptr = &f_info[g_ptr->feat];
780 if (floor_mode & CFM_UP)
782 if (have_flag(f_ptr->flags, FF_LESS) && have_flag(f_ptr->flags, FF_STAIRS) &&
783 !have_flag(f_ptr->flags, FF_SPECIAL))
787 /* Found fixed stairs? */
788 if (g_ptr->special &&
789 g_ptr->special == sf_ptr->upper_floor_id)
797 else if (floor_mode & CFM_DOWN)
799 if (have_flag(f_ptr->flags, FF_MORE) && have_flag(f_ptr->flags, FF_STAIRS) &&
800 !have_flag(f_ptr->flags, FF_SPECIAL))
804 /* Found fixed stairs */
805 if (g_ptr->special &&
806 g_ptr->special == sf_ptr->lower_floor_id)
816 if (have_flag(f_ptr->flags, FF_BLDG))
822 if (ok && (num < 20))
839 /* No stairs found! -- No return */
840 prepare_change_floor_mode(CFM_RAND_PLACE | CFM_NO_RETURN);
842 /* Mega Hack -- It's not the stairs you enter. Disable it. */
843 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;
847 /* Choose random one */
850 /* Point stair location */
851 p_ptr->y = y_table[i];
852 p_ptr->x = x_table[i];
857 * @brief 現在のフロアを離れるに伴って行なわれる保存処理
858 * / Maintain quest monsters, mark next floor_id at stairs, save current floor, and prepare to enter next floor.
861 void leave_floor(player_type *creature_ptr, BIT_FLAGS floor_mode)
863 grid_type *g_ptr = NULL;
865 saved_floor_type *sf_ptr;
866 MONRACE_IDX quest_r_idx = 0;
869 /* Preserve pets and prepare to take these to next floor */
872 /* Remove all mirrors without explosion */
873 remove_all_mirrors(FALSE);
875 if (creature_ptr->special_defense & NINJA_S_STEALTH) set_superstealth(creature_ptr, FALSE);
877 /* New floor is not yet prepared */
880 /* Temporary get a floor_id (for Arena) */
881 if (!creature_ptr->floor_id &&
882 (floor_mode & CFM_SAVE_FLOORS) &&
883 !(floor_mode & CFM_NO_RETURN))
885 /* Get temporal floor_id */
886 creature_ptr->floor_id = get_new_floor_id();
889 /* Search the quest monster index */
890 for (i = 0; i < max_q_idx; i++)
892 if ((quest[i].status == QUEST_STATUS_TAKEN) &&
893 ((quest[i].type == QUEST_TYPE_KILL_LEVEL) ||
894 (quest[i].type == QUEST_TYPE_RANDOM)) &&
895 (quest[i].level == current_floor_ptr->dun_level) &&
896 (creature_ptr->dungeon_idx == quest[i].dungeon) &&
897 !(quest[i].flags & QUEST_FLAG_PRESET))
899 quest_r_idx = quest[i].r_idx;
903 /* Maintain quest monsters */
904 for (i = 1; i < current_floor_ptr->m_max; i++)
907 monster_type *m_ptr = ¤t_floor_ptr->m_list[i];
909 if (!monster_is_valid(m_ptr)) continue;
911 /* Only maintain quest monsters */
912 if (quest_r_idx != m_ptr->r_idx) continue;
914 /* Extract real monster race */
915 r_ptr = real_r_ptr(m_ptr);
917 /* Ignore unique monsters */
918 if ((r_ptr->flags1 & RF1_UNIQUE) ||
919 (r_ptr->flags7 & RF7_NAZGUL)) continue;
921 /* Delete non-unique quest monsters */
922 delete_monster_idx(i);
925 /* Check if there is a same item */
926 for (i = 0; i < INVEN_PACK; i++)
928 object_type *o_ptr = &creature_ptr->inventory_list[i];
930 if (!OBJECT_IS_VALID(o_ptr)) continue;
932 /* Delete old memorized location of the artifact */
933 if (object_is_fixed_artifact(o_ptr))
935 a_info[o_ptr->name1].floor_id = 0;
939 /* Extract current floor info or NULL */
940 sf_ptr = get_sf_ptr(creature_ptr->floor_id);
942 /* Choose random stairs */
943 if ((floor_mode & CFM_RAND_CONNECT) && creature_ptr->floor_id)
945 locate_connected_stairs(sf_ptr, floor_mode);
948 /* Extract new dungeon level */
949 if (floor_mode & CFM_SAVE_FLOORS)
951 /* Extract stair position */
952 g_ptr = ¤t_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
953 f_ptr = &f_info[g_ptr->feat];
955 /* Get back to old saved floor? */
956 if (g_ptr->special && !have_flag(f_ptr->flags, FF_SPECIAL) && get_sf_ptr(g_ptr->special))
958 /* Saved floor is exist. Use it. */
959 new_floor_id = g_ptr->special;
962 /* Mark shaft up/down */
963 if (have_flag(f_ptr->flags, FF_STAIRS) && have_flag(f_ptr->flags, FF_SHAFT))
965 prepare_change_floor_mode(CFM_SHAFT);
969 /* Climb up/down some sort of stairs */
970 if (floor_mode & (CFM_DOWN | CFM_UP))
974 /* Extract level movement number */
975 if (floor_mode & CFM_DOWN) move_num = 1;
976 else if (floor_mode & CFM_UP) move_num = -1;
978 /* Shafts are deeper than normal stairs */
979 if (floor_mode & CFM_SHAFT)
980 move_num += SGN(move_num);
982 /* Get out from or Enter the dungeon */
983 if (floor_mode & CFM_DOWN)
985 if (!current_floor_ptr->dun_level)
986 move_num = d_info[creature_ptr->dungeon_idx].mindepth;
988 else if (floor_mode & CFM_UP)
990 if (current_floor_ptr->dun_level + move_num < d_info[creature_ptr->dungeon_idx].mindepth)
991 move_num = -current_floor_ptr->dun_level;
994 current_floor_ptr->dun_level += move_num;
997 /* Leaving the dungeon to town */
998 if (!current_floor_ptr->dun_level && creature_ptr->dungeon_idx)
1000 creature_ptr->leaving_dungeon = TRUE;
1001 if (!vanilla_town && !lite_town)
1003 creature_ptr->wilderness_y = d_info[creature_ptr->dungeon_idx].dy;
1004 creature_ptr->wilderness_x = d_info[creature_ptr->dungeon_idx].dx;
1006 creature_ptr->recall_dungeon = creature_ptr->dungeon_idx;
1007 creature_ptr->dungeon_idx = 0;
1009 /* Reach to the surface -- Clear all saved floors */
1010 floor_mode &= ~CFM_SAVE_FLOORS;
1013 /* Kill some old saved floors */
1014 if (!(floor_mode & CFM_SAVE_FLOORS))
1016 /* Kill all saved floors */
1017 for (i = 0; i < MAX_SAVED_FLOORS; i++)
1018 kill_saved_floor(&saved_floors[i]);
1020 /* Reset visit_mark count */
1021 latest_visit_mark = 1;
1023 else if (floor_mode & CFM_NO_RETURN)
1025 /* Kill current floor */
1026 kill_saved_floor(sf_ptr);
1029 /* No current floor -- Left/Enter dungeon etc... */
1030 if (!creature_ptr->floor_id)
1032 /* No longer need to save current floor */
1037 /* Mark next floor_id on the previous floor */
1041 new_floor_id = get_new_floor_id();
1043 /* Connect from here */
1044 if (g_ptr && !feat_uses_special(g_ptr->feat))
1046 g_ptr->special = new_floor_id;
1050 /* Fix connection -- level teleportation or trap door */
1051 if (floor_mode & CFM_RAND_CONNECT)
1053 if (floor_mode & CFM_UP)
1054 sf_ptr->upper_floor_id = new_floor_id;
1055 else if (floor_mode & CFM_DOWN)
1056 sf_ptr->lower_floor_id = new_floor_id;
1059 /* If you can return, you need to save previous floor */
1060 if ((floor_mode & CFM_SAVE_FLOORS) &&
1061 !(floor_mode & CFM_NO_RETURN))
1063 /* Get out of the my way! */
1066 /* Record the last visit current_world_ptr->game_turn of current floor */
1067 sf_ptr->last_visit = current_world_ptr->game_turn;
1069 forget_lite(current_floor_ptr);
1071 clear_mon_lite(current_floor_ptr);
1073 /* Save current floor */
1074 if (!save_floor(sf_ptr, 0))
1076 /* Save failed -- No return */
1077 prepare_change_floor_mode(CFM_NO_RETURN);
1079 /* Kill current floor */
1080 kill_saved_floor(get_sf_ptr(creature_ptr->floor_id));
1087 * @brief フロアの切り替え処理 / Enter new floor.
1090 * If the floor is an old saved floor, it will be\n
1091 * restored from the temporal file. If the floor is new one, new current_floor_ptr->grid_array\n
1092 * will be generated.\n
1094 void change_floor(BIT_FLAGS floor_mode)
1096 saved_floor_type *sf_ptr;
1097 bool loaded = FALSE;
1099 /* The dungeon is not ready */
1100 current_world_ptr->character_dungeon = FALSE;
1102 /* No longer in the trap detecteded region */
1103 p_ptr->dtrap = FALSE;
1105 /* Mega-Hack -- no panel yet */
1111 /* Mega-Hack -- not ambushed on the wildness? */
1112 p_ptr->ambush_flag = FALSE;
1114 /* No saved floors (On the surface etc.) */
1115 if (!(floor_mode & CFM_SAVE_FLOORS) &&
1116 !(floor_mode & CFM_FIRST_FLOOR))
1118 /* Create current_floor_ptr->grid_array */
1119 generate_random_floor(current_floor_ptr);
1121 /* Paranoia -- No new saved floor */
1125 /* In the dungeon */
1128 /* No floor_id yet */
1132 new_floor_id = get_new_floor_id();
1135 /* Pointer for infomations of new floor */
1136 sf_ptr = get_sf_ptr(new_floor_id);
1138 /* Try to restore old floor */
1139 if (sf_ptr->last_visit)
1141 /* Old saved floor is exist */
1142 if (load_floor(sf_ptr, 0))
1146 /* Forbid return stairs */
1147 if (floor_mode & CFM_NO_RETURN)
1149 grid_type *g_ptr = ¤t_floor_ptr->grid_array[p_ptr->y][p_ptr->x];
1151 if (!feat_uses_special(g_ptr->feat))
1153 if (floor_mode & (CFM_DOWN | CFM_UP))
1155 /* Reset to floor */
1156 g_ptr->feat = feat_ground_type[randint0(100)];
1166 * Set lower/upper_floor_id of new floor when the new
1167 * floor is right-above/right-under the current floor.
1169 * Stair creation/Teleport level/Trap door will take
1170 * you the same floor when you used it later again.
1172 if (p_ptr->floor_id)
1174 saved_floor_type *cur_sf_ptr = get_sf_ptr(p_ptr->floor_id);
1176 if (floor_mode & CFM_UP)
1178 /* New floor is right-above */
1179 if (cur_sf_ptr->upper_floor_id == new_floor_id)
1180 sf_ptr->lower_floor_id = p_ptr->floor_id;
1182 else if (floor_mode & CFM_DOWN)
1184 /* New floor is right-under */
1185 if (cur_sf_ptr->lower_floor_id == new_floor_id)
1186 sf_ptr->upper_floor_id = p_ptr->floor_id;
1190 /* Break connection to killed floor */
1193 if (floor_mode & CFM_UP)
1194 sf_ptr->lower_floor_id = 0;
1195 else if (floor_mode & CFM_DOWN)
1196 sf_ptr->upper_floor_id = 0;
1199 /* Maintain monsters and artifacts */
1203 GAME_TURN tmp_last_visit = sf_ptr->last_visit;
1204 GAME_TURN absence_ticks;
1205 int alloc_chance = d_info[p_ptr->dungeon_idx].max_m_alloc_chance;
1206 GAME_TURN alloc_times;
1208 while (tmp_last_visit > current_world_ptr->game_turn) tmp_last_visit -= TURNS_PER_TICK * TOWN_DAWN;
1209 absence_ticks = (current_world_ptr->game_turn - tmp_last_visit) / TURNS_PER_TICK;
1211 /* Maintain monsters */
1212 for (i = 1; i < current_floor_ptr->m_max; i++)
1214 monster_race *r_ptr;
1215 monster_type *m_ptr = ¤t_floor_ptr->m_list[i];
1217 if (!monster_is_valid(m_ptr)) continue;
1222 m_ptr->hp = m_ptr->maxhp = m_ptr->max_maxhp;
1224 /* Remove timed status (except MTIMED_CSLEEP) */
1225 (void)set_monster_fast(i, 0);
1226 (void)set_monster_slow(i, 0);
1227 (void)set_monster_stunned(i, 0);
1228 (void)set_monster_confused(i, 0);
1229 (void)set_monster_monfear(i, 0);
1230 (void)set_monster_invulner(i, 0, FALSE);
1233 /* Extract real monster race */
1234 r_ptr = real_r_ptr(m_ptr);
1236 /* Ignore non-unique */
1237 if (!(r_ptr->flags1 & RF1_UNIQUE) &&
1238 !(r_ptr->flags7 & RF7_NAZGUL)) continue;
1240 /* Appear at a different floor? */
1241 if (r_ptr->floor_id != new_floor_id)
1243 /* Disapper from here */
1244 delete_monster_idx(i);
1248 /* Maintain artifatcs */
1249 for (i = 1; i < current_floor_ptr->o_max; i++)
1251 object_type *o_ptr = ¤t_floor_ptr->o_list[i];
1253 if (!OBJECT_IS_VALID(o_ptr)) continue;
1255 /* Ignore non-artifact */
1256 if (!object_is_fixed_artifact(o_ptr)) continue;
1258 /* Appear at a different floor? */
1259 if (a_info[o_ptr->name1].floor_id != new_floor_id)
1261 /* Disappear from here */
1262 delete_object_idx(i);
1266 /* Cancel preserve */
1267 a_info[o_ptr->name1].cur_num = 1;
1271 (void)place_quest_monsters();
1273 /* Place some random monsters */
1274 alloc_times = absence_ticks / alloc_chance;
1276 if (randint0(alloc_chance) < (absence_ticks % alloc_chance))
1279 for (i = 0; i < alloc_times; i++)
1281 /* Make a (group of) new monster */
1282 (void)alloc_monster(0, 0);
1287 /* New floor_id or failed to restore */
1288 else /* if (!loaded) */
1290 if (sf_ptr->last_visit)
1292 /* Temporal file is broken? */
1293 msg_print(_("階段は行き止まりだった。", "The staircases come to a dead end..."));
1295 /* Create simple dead end */
1296 build_dead_end(current_floor_ptr);
1298 /* Break connection */
1299 if (floor_mode & CFM_UP)
1301 sf_ptr->upper_floor_id = 0;
1303 else if (floor_mode & CFM_DOWN)
1305 sf_ptr->lower_floor_id = 0;
1310 /* Newly create current_floor_ptr->grid_array */
1311 generate_random_floor(current_floor_ptr);
1314 /* Record last visit current_world_ptr->game_turn */
1315 sf_ptr->last_visit = current_world_ptr->game_turn;
1317 /* Set correct current_floor_ptr->dun_level value */
1318 sf_ptr->dun_level = current_floor_ptr->dun_level;
1320 /* Create connected stairs */
1321 if (!(floor_mode & CFM_NO_RETURN))
1323 /* Extract stair position */
1324 grid_type *g_ptr = ¤t_floor_ptr->grid_array[p_ptr->y][p_ptr->x];
1326 /*** Create connected stairs ***/
1328 /* No stairs down from Quest */
1329 if ((floor_mode & CFM_UP) && !quest_number(current_floor_ptr->dun_level))
1331 g_ptr->feat = (floor_mode & CFM_SHAFT) ? feat_state(feat_down_stair, FF_SHAFT) : feat_down_stair;
1334 /* No stairs up when ironman_downward */
1335 else if ((floor_mode & CFM_DOWN) && !ironman_downward)
1337 g_ptr->feat = (floor_mode & CFM_SHAFT) ? feat_state(feat_up_stair, FF_SHAFT) : feat_up_stair;
1340 /* Paranoia -- Clear mimic */
1343 /* Connect to previous floor */
1344 g_ptr->special = p_ptr->floor_id;
1348 /* Arrive at random grid */
1349 if (floor_mode & (CFM_RAND_PLACE))
1351 (void)new_player_spot();
1354 /* You see stairs blocked */
1355 else if ((floor_mode & CFM_NO_RETURN) && (floor_mode & (CFM_DOWN | CFM_UP)))
1359 msg_print(_("突然階段が塞がれてしまった。", "Suddenly the stairs is blocked!"));
1363 msg_print(_("ゴトゴトと何か音がした。", "You hear some noises."));
1370 * The "current_world_ptr->game_turn" is not always different number because
1371 * the level teleport doesn't take any current_world_ptr->game_turn. Use
1372 * visit mark instead of last visit current_world_ptr->game_turn to find the
1373 * oldest saved floor.
1375 sf_ptr->visit_mark = latest_visit_mark++;
1378 /* Place preserved pet monsters */
1381 /* Reset travel target place */
1382 forget_travel_flow();
1384 /* Hack -- maintain unique and artifacts */
1385 update_unique_artifact(new_floor_id);
1387 /* Now the player is in new floor */
1388 p_ptr->floor_id = new_floor_id;
1390 /* The dungeon is ready */
1391 current_world_ptr->character_dungeon = TRUE;
1393 /* Hack -- Munchkin characters always get whole map */
1394 if (p_ptr->pseikaku == SEIKAKU_MUNCHKIN)
1395 wiz_lite(p_ptr, (bool)(p_ptr->pclass == CLASS_NINJA));
1397 /* Remember when this level was "created" */
1398 current_floor_ptr->generated_turn = current_world_ptr->game_turn;
1400 /* No dungeon feeling yet */
1401 p_ptr->feeling_turn = current_floor_ptr->generated_turn;
1404 /* Clear all flags */
1407 select_floor_music(p_ptr);
1408 p_ptr->change_floor_mode = 0;