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
23 #include "floor-save.h"
24 #include "floor-events.h"
25 #include "floor-generate.h"
32 #include "spells-floor.h"
33 #include "monster-status.h"
34 #include "object-hook.h"
36 #include "cmd-basic.h"
38 #include "player-effects.h"
39 #include "player-class.h"
40 #include "player-personality.h"
46 #include "view-mainwindow.h"
49 static FLOOR_IDX new_floor_id; /*!<次のフロアのID / floor_id of the destination */
50 static u32b latest_visit_mark; /*!<フロアを渡った回数?(確認中) / Max number of visit_mark */
53 * Number of floor_id used from birth
55 FLOOR_IDX max_floor_id;
58 * Sign for current process used in temporal files.
59 * Actually it is the start time of current process.
61 u32b saved_floor_file_sign;
64 * @brief 保存フロア配列を初期化する / Initialize saved_floors array.
65 * @param force テンポラリファイルが残っていた場合も警告なしで強制的に削除する。
66 * @details Make sure that old temporal files are not remaining as gurbages.
69 void init_saved_floors(bool force)
71 char floor_savefile[1024];
74 BIT_FLAGS mode = 0644;
78 /* Get "games" permissions */
83 for (i = 0; i < MAX_SAVED_FLOORS; i++)
85 saved_floor_type *sf_ptr = &saved_floors[i];
88 sprintf(floor_savefile, "%s.F%02d", savefile, i);
90 /* Grab permissions */
93 /* Try to create the file */
94 fd = fd_make(floor_savefile, mode);
96 /* Drop permissions */
104 msg_print(_("エラー:古いテンポラリ・ファイルが残っています。", "Error: There are old temporal files."));
105 msg_print(_("変愚蛮怒を二重に起動していないか確認してください。", "Make sure you are not running two game processes simultaneously."));
106 msg_print(_("過去に変愚蛮怒がクラッシュした場合は一時ファイルを", "If the temporal files are garbages of old crashed process, "));
107 msg_print(_("強制的に削除して実行を続けられます。", "you can delete it safely."));
108 if (!get_check(_("強制的に削除してもよろしいですか?", "Do you delete old temporal files? ")))
109 quit(_("実行中止", "Aborted."));
119 /* Grab permissions */
122 /* Simply kill the temporal file */
123 (void)fd_kill(floor_savefile);
125 /* Drop permissions */
128 sf_ptr->floor_id = 0;
131 /* No floor_id used yet (No.0 is reserved to indicate non existance) */
134 /* vist_mark is from 1 */
135 latest_visit_mark = 1;
137 /* A sign to mark temporal files */
138 saved_floor_file_sign = (u32b)time(NULL);
140 /* No next floor yet */
143 /* No change floor mode yet */
144 p_ptr->change_floor_mode = 0;
148 /* Drop "games" permissions */
155 * @brief 保存フロア用テンポラリファイルを削除する / Kill temporal files
156 * @details Should be called just before the game quit.
159 void clear_saved_floor_files(void)
161 char floor_savefile[1024];
166 /* Get "games" permissions */
171 for (i = 0; i < MAX_SAVED_FLOORS; i++)
173 saved_floor_type *sf_ptr = &saved_floors[i];
175 /* No temporal file */
176 if (!sf_ptr->floor_id) continue;
177 if (sf_ptr->floor_id == p_ptr->floor_id) continue;
180 sprintf(floor_savefile, "%s.F%02d", savefile, i);
182 /* Grab permissions */
185 /* Simply kill the temporal file */
186 (void)fd_kill(floor_savefile);
188 /* Drop permissions */
194 /* Drop "games" permissions */
201 * @brief 保存フロアIDから参照ポインタを得る / Get a pointer for an item of the saved_floors array.
202 * @param floor_id 保存フロアID
203 * @return IDに対応する保存フロアのポインタ、ない場合はNULLを返す。
205 saved_floor_type *get_sf_ptr(FLOOR_IDX floor_id)
209 /* floor_id No.0 indicates no floor */
210 if (!floor_id) return NULL;
212 for (i = 0; i < MAX_SAVED_FLOORS; i++)
214 saved_floor_type *sf_ptr = &saved_floors[i];
216 if (sf_ptr->floor_id == floor_id) return sf_ptr;
225 * @brief 参照ポインタ先の保存フロアを抹消する / kill a saved floor and get an empty space
226 * @param sf_ptr 保存フロアの参照ポインタ
229 static void kill_saved_floor(saved_floor_type *sf_ptr)
231 char floor_savefile[1024];
235 if (!sf_ptr->floor_id) return;
237 if (sf_ptr->floor_id == p_ptr->floor_id)
239 /* Kill current floor */
242 /* Current floor doesn't have temporal file */
247 sprintf(floor_savefile, "%s.F%02d", savefile, (int)sf_ptr->savefile_id);
249 /* Grab permissions */
252 /* Simply kill the temporal file */
253 (void)fd_kill(floor_savefile);
255 /* Drop permissions */
259 /* No longer exists */
260 sf_ptr->floor_id = 0;
265 * @brief 新規に利用可能な保存フロアを返す / Initialize new saved floor and get its floor id.
266 * @return 利用可能な保存フロアID
268 * If number of saved floors are already MAX_SAVED_FLOORS, kill the oldest one.
270 FLOOR_IDX get_new_floor_id(void)
272 saved_floor_type *sf_ptr = NULL;
275 /* Look for empty space */
276 for (i = 0; i < MAX_SAVED_FLOORS; i++)
278 sf_ptr = &saved_floors[i];
280 if (!sf_ptr->floor_id) break;
284 if (i == MAX_SAVED_FLOORS)
287 u32b oldest_visit = 0xffffffffL;
289 /* Search for oldest */
290 for (i = 0; i < MAX_SAVED_FLOORS; i++)
292 sf_ptr = &saved_floors[i];
294 /* Don't kill current floor */
295 if (sf_ptr->floor_id == p_ptr->floor_id) continue;
297 /* Don't kill newer */
298 if (sf_ptr->visit_mark > oldest_visit) continue;
301 oldest_visit = sf_ptr->visit_mark;
304 /* Kill oldest saved floor */
305 sf_ptr = &saved_floors[oldest];
306 kill_saved_floor(sf_ptr);
312 /* Prepare new floor data */
313 sf_ptr->savefile_id = i;
314 sf_ptr->floor_id = max_floor_id;
315 sf_ptr->last_visit = 0;
316 sf_ptr->upper_floor_id = 0;
317 sf_ptr->lower_floor_id = 0;
318 sf_ptr->visit_mark = latest_visit_mark++;
320 /* sf_ptr->dun_level may be changed later */
321 sf_ptr->dun_level = p_ptr->current_floor_ptr->dun_level;
324 /* Increment number of floor_id */
325 if (max_floor_id < MAX_SHORT) max_floor_id++;
327 /* 32767 floor_ids are all used up! Re-use ancient IDs */
328 else max_floor_id = 1;
330 return sf_ptr->floor_id;
335 * @brief フロア切り替え時の処理フラグを追加する / Prepare mode flags of changing floor
336 * @param mode 追加したい所持フラグ
339 void prepare_change_floor_mode(BIT_FLAGS mode)
341 p_ptr->change_floor_mode |= mode;
345 * @brief 階段移動先のフロアが生成できない時に簡単な行き止まりマップを作成する / Builds the dead end
348 static void build_dead_end(floor_type *floor_ptr)
352 clear_cave(floor_ptr);
354 /* Fill the arrays of floors and walls in the good proportions */
355 set_floor_and_wall(0);
358 floor_ptr->height = SCREEN_HGT;
359 floor_ptr->width = SCREEN_WID;
361 /* Filled with permanent walls */
362 for (y = 0; y < MAX_HGT; y++)
364 for (x = 0; x < MAX_WID; x++)
366 /* Create "solid" perma-wall */
367 place_solid_perm_bold(y, x);
371 /* Place at center of the floor */
372 p_ptr->y = floor_ptr->height / 2;
373 p_ptr->x = floor_ptr->width / 2;
375 /* Give one square */
376 place_floor_bold(p_ptr->y, p_ptr->x);
378 wipe_generate_random_floor_flags(floor_ptr);
383 #define MAX_PARTY_MON 21 /*!< フロア移動時に先のフロアに連れて行けるペットの最大数 Maximum number of preservable pets */
384 static monster_type party_mon[MAX_PARTY_MON]; /*!< フロア移動に保存するペットモンスターの配列 */
387 * @brief フロア移動時のペット保存処理 / Preserve_pets
390 static void preserve_pet(void)
395 for (num = 0; num < MAX_PARTY_MON; num++)
397 party_mon[num].r_idx = 0;
402 monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[p_ptr->riding];
404 /* Pet of other pet don't follow. */
405 if (m_ptr->parent_m_idx)
408 p_ptr->pet_extra_flags &= ~(PF_RYOUTE);
409 p_ptr->riding_ryoute = p_ptr->old_riding_ryoute = FALSE;
413 /* Preserve the mount */
414 (void)COPY(&party_mon[0], m_ptr, monster_type);
416 /* Delete from this floor */
417 delete_monster_idx(p_ptr->riding);
422 * If player is in wild mode, no pets are preserved
423 * except a monster whom player riding
425 if (!p_ptr->wild_mode && !p_ptr->inside_arena && !p_ptr->phase_out)
427 for (i = p_ptr->current_floor_ptr->m_max - 1, num = 1; (i >= 1 && num < MAX_PARTY_MON); i--)
429 monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[i];
431 if (!monster_is_valid(m_ptr)) continue;
432 if (!is_pet(m_ptr)) continue;
433 if (i == p_ptr->riding) continue;
435 if (reinit_wilderness)
437 /* Don't lose sight of pets when getting a Quest */
441 POSITION dis = distance(p_ptr->y, p_ptr->x, m_ptr->fy, m_ptr->fx);
443 /* Confused (etc.) monsters don't follow. */
444 if (MON_CONFUSED(m_ptr) || MON_STUNNED(m_ptr) || MON_CSLEEP(m_ptr)) continue;
446 /* Pet of other pet don't follow. */
447 if (m_ptr->parent_m_idx) continue;
450 * Pets with nickname will follow even from 3 blocks away
451 * when you or the pet can see the other.
453 if (m_ptr->nickname &&
454 ((player_has_los_bold(m_ptr->fy, m_ptr->fx) && projectable(p_ptr->y, p_ptr->x, m_ptr->fy, m_ptr->fx)) ||
455 (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))))
457 if (dis > 3) continue;
461 if (dis > 1) continue;
465 (void)COPY(&party_mon[num], &p_ptr->current_floor_ptr->m_list[i], monster_type);
469 /* Delete from this floor */
470 delete_monster_idx(i);
474 if (record_named_pet)
476 for (i = p_ptr->current_floor_ptr->m_max - 1; i >=1; i--)
478 monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[i];
479 GAME_TEXT m_name[MAX_NLEN];
481 if (!monster_is_valid(m_ptr)) continue;
482 if (!is_pet(m_ptr)) continue;
483 if (!m_ptr->nickname) continue;
484 if (p_ptr->riding == i) continue;
486 monster_desc(m_name, m_ptr, MD_ASSUME_VISIBLE | MD_INDEF_VISIBLE);
487 exe_write_diary(p_ptr, NIKKI_NAMED_PET, RECORD_NAMED_PET_MOVED, m_name);
492 /* Pet of other pet may disappear. */
493 for (i = p_ptr->current_floor_ptr->m_max - 1; i >=1; i--)
495 monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[i];
497 /* Are there its parent? */
498 if (m_ptr->parent_m_idx && !p_ptr->current_floor_ptr->m_list[m_ptr->parent_m_idx].r_idx)
500 /* Its parent have gone, it also goes away. */
504 GAME_TEXT m_name[MAX_NLEN];
505 monster_desc(m_name, m_ptr, 0);
506 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
509 delete_monster_idx(i);
516 * @brief フロア移動時にペットを伴った場合の準備処理 / Pre-calculate the racial counters of preserved pets
519 * To prevent multiple generation of unique monster who is the minion of player
521 void precalc_cur_num_of_pet(void)
525 int max_num = p_ptr->wild_mode ? 1 : MAX_PARTY_MON;
527 for (i = 0; i < max_num; i++)
529 m_ptr = &party_mon[i];
531 /* Skip empty monsters */
532 if (!monster_is_valid(m_ptr)) continue;
534 /* Hack -- Increase the racial counter */
535 real_r_ptr(m_ptr)->cur_num++;
540 * @brief 移動先のフロアに伴ったペットを配置する / Place preserved pet monsters on new floor
543 static void place_pet(player_type *master_ptr)
546 int max_num = master_ptr->wild_mode ? 1 : MAX_PARTY_MON;
548 for (i = 0; i < max_num; i++)
550 POSITION cy = 0, cx = 0;
553 if (!(party_mon[i].r_idx)) continue;
558 master_ptr->riding = m_idx;
570 for (d = 1; d < A_MAX; d++)
572 for (j = 1000; j > 0; j--)
574 scatter(&cy, &cx, master_ptr->y, master_ptr->x, d, 0);
575 if (monster_can_enter(cy, cx, &r_info[party_mon[i].r_idx], 0)) break;
579 m_idx = (d == 6) ? 0 : m_pop();
584 monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[m_idx];
587 p_ptr->current_floor_ptr->grid_array[cy][cx].m_idx = m_idx;
589 m_ptr->r_idx = party_mon[i].r_idx;
591 /* Copy all member of the structure */
592 *m_ptr = party_mon[i];
593 r_ptr = real_r_ptr(m_ptr);
598 m_ptr->mtimed[MTIMED_CSLEEP] = 0;
599 m_ptr->hold_o_idx = 0;
602 if ((r_ptr->flags1 & RF1_FORCE_SLEEP) && !ironman_nightmare)
604 /* Monster is still being nice */
605 m_ptr->mflag |= (MFLAG_NICE);
607 /* Must repair monsters */
608 repair_monsters = TRUE;
610 update_monster(m_idx, TRUE);
613 /* Pre-calculated in precalc_cur_num_of_pet() */
614 /* r_ptr->cur_num++; */
616 /* Hack -- Count the number of "reproducers" */
617 if (r_ptr->flags2 & RF2_MULTIPLY) p_ptr->current_floor_ptr->num_repro++;
622 monster_type *m_ptr = &party_mon[i];
623 monster_race *r_ptr = real_r_ptr(m_ptr);
624 GAME_TEXT m_name[MAX_NLEN];
626 monster_desc(m_name, m_ptr, 0);
627 msg_format(_("%sとはぐれてしまった。", "You have lost sight of %s."), m_name);
628 if (record_named_pet && m_ptr->nickname)
630 monster_desc(m_name, m_ptr, MD_INDEF_VISIBLE);
631 exe_write_diary(master_ptr, NIKKI_NAMED_PET, RECORD_NAMED_PET_LOST_SIGHT, m_name);
634 /* Pre-calculated in precalc_cur_num_of_pet(), but need to decrease */
635 if (r_ptr->cur_num) r_ptr->cur_num--;
639 /* For accuracy of precalc_cur_num_of_pet() */
640 (void)C_WIPE(party_mon, MAX_PARTY_MON, monster_type);
645 * @brief ユニークモンスターやアーティファクトの所在フロアを更新する / Hack -- Update location of unique monsters and artifacts
646 * @param cur_floor_id 現在のフロアID
649 * The r_ptr->floor_id and a_ptr->floor_id are not updated correctly\n
650 * while new floor creation since dungeons may be re-created by\n
651 * auto-scum option.\n
653 static void update_unique_artifact(s16b cur_floor_id)
657 /* Maintain unique monsters */
658 for (i = 1; i < p_ptr->current_floor_ptr->m_max; i++)
661 monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[i];
663 if (!monster_is_valid(m_ptr)) continue;
665 /* Extract real monster race */
666 r_ptr = real_r_ptr(m_ptr);
668 /* Memorize location of the unique monster */
669 if ((r_ptr->flags1 & RF1_UNIQUE) ||
670 (r_ptr->flags7 & RF7_NAZGUL))
672 r_ptr->floor_id = cur_floor_id;
676 /* Maintain artifatcs */
677 for (i = 1; i < p_ptr->current_floor_ptr->o_max; i++)
679 object_type *o_ptr = &p_ptr->current_floor_ptr->o_list[i];
681 if (!OBJECT_IS_VALID(o_ptr)) continue;
683 /* Memorize location of the artifact */
684 if (object_is_fixed_artifact(o_ptr))
686 a_info[o_ptr->name1].floor_id = cur_floor_id;
693 * @brief フロア移動時、プレイヤーの移動先モンスターが既にいた場合ランダムな近隣に移動させる / When a monster is at a place where player will return,
696 static void get_out_monster(floor_type *floor_ptr, player_type *protected_ptr)
700 POSITION oy = protected_ptr->y;
701 POSITION ox = protected_ptr->x;
702 MONSTER_IDX m_idx = floor_ptr->grid_array[oy][ox].m_idx;
704 /* Nothing to do if no monster */
707 /* Look until done */
712 /* Pick a (possibly illegal) location */
713 POSITION ny = rand_spread(oy, dis);
714 POSITION nx = rand_spread(ox, dis);
718 /* Stop after 1000 tries */
719 if (tries > 10000) return;
722 * Increase distance after doing enough tries
723 * compared to area of possible space
725 if (tries > 20 * dis * dis) dis++;
727 /* Ignore illegal locations */
728 if (!in_bounds(floor_ptr, ny, nx)) continue;
730 /* Require "empty" floor space */
731 if (!cave_empty_bold(p_ptr->current_floor_ptr, ny, nx)) continue;
733 /* Hack -- no teleport onto glyph of warding */
734 if (is_glyph_grid(&floor_ptr->grid_array[ny][nx])) continue;
735 if (is_explosive_rune_grid(&floor_ptr->grid_array[ny][nx])) continue;
737 /* ...nor onto the Pattern */
738 if (pattern_tile(ny, nx)) continue;
740 /*** It's a good place ***/
742 m_ptr = &floor_ptr->m_list[m_idx];
744 /* Update the old location */
745 floor_ptr->grid_array[oy][ox].m_idx = 0;
747 /* Update the new location */
748 floor_ptr->grid_array[ny][nx].m_idx = m_idx;
750 /* Move the monster */
754 /* No need to do update_monster() */
762 * @brief 新フロアに移動元フロアに繋がる階段を配置する / Virtually teleport onto the stairs that is connecting between two floors.
763 * @param sf_ptr 移動元の保存フロア構造体参照ポインタ
766 static void locate_connected_stairs(player_type *creature_ptr, floor_type *floor_ptr, saved_floor_type *sf_ptr, BIT_FLAGS floor_mode)
768 POSITION x, y, sx = 0, sy = 0;
769 POSITION x_table[20];
770 POSITION y_table[20];
774 /* Search usable stairs */
775 for (y = 0; y < floor_ptr->height; y++)
777 for (x = 0; x < floor_ptr->width; x++)
779 grid_type *g_ptr = &floor_ptr->grid_array[y][x];
780 feature_type *f_ptr = &f_info[g_ptr->feat];
783 if (floor_mode & CFM_UP)
785 if (have_flag(f_ptr->flags, FF_LESS) && have_flag(f_ptr->flags, FF_STAIRS) &&
786 !have_flag(f_ptr->flags, FF_SPECIAL))
790 /* Found fixed stairs? */
791 if (g_ptr->special &&
792 g_ptr->special == sf_ptr->upper_floor_id)
800 else if (floor_mode & CFM_DOWN)
802 if (have_flag(f_ptr->flags, FF_MORE) && have_flag(f_ptr->flags, FF_STAIRS) &&
803 !have_flag(f_ptr->flags, FF_SPECIAL))
807 /* Found fixed stairs */
808 if (g_ptr->special &&
809 g_ptr->special == sf_ptr->lower_floor_id)
819 if (have_flag(f_ptr->flags, FF_BLDG))
825 if (ok && (num < 20))
837 creature_ptr->y = sy;
838 creature_ptr->x = sx;
842 /* No stairs found! -- No return */
843 prepare_change_floor_mode(CFM_RAND_PLACE | CFM_NO_RETURN);
845 /* Mega Hack -- It's not the stairs you enter. Disable it. */
846 if (!feat_uses_special(floor_ptr->grid_array[creature_ptr->y][creature_ptr->x].feat)) floor_ptr->grid_array[creature_ptr->y][creature_ptr->x].special = 0;
850 /* Choose random one */
853 /* Point stair location */
854 creature_ptr->y = y_table[i];
855 creature_ptr->x = x_table[i];
860 * @brief 現在のフロアを離れるに伴って行なわれる保存処理
861 * / Maintain quest monsters, mark next floor_id at stairs, save current floor, and prepare to enter next floor.
864 void leave_floor(player_type *creature_ptr)
866 grid_type *g_ptr = NULL;
868 saved_floor_type *sf_ptr;
869 MONRACE_IDX quest_r_idx = 0;
871 FLOOR_IDX tmp_floor_idx = 0;
873 /* Preserve pets and prepare to take these to next floor */
876 /* Remove all mirrors without explosion */
877 remove_all_mirrors(FALSE);
879 if (creature_ptr->special_defense & NINJA_S_STEALTH) set_superstealth(creature_ptr, FALSE);
881 /* New floor is not yet prepared */
884 /* Temporary get a floor_id (for Arena) */
885 if (!creature_ptr->floor_id &&
886 (creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) &&
887 !(creature_ptr->change_floor_mode & CFM_NO_RETURN))
889 /* Get temporal floor_id */
890 tmp_floor_idx = get_new_floor_id();
893 /* Search the quest monster index */
894 for (i = 0; i < max_q_idx; i++)
896 if ((quest[i].status == QUEST_STATUS_TAKEN) &&
897 ((quest[i].type == QUEST_TYPE_KILL_LEVEL) ||
898 (quest[i].type == QUEST_TYPE_RANDOM)) &&
899 (quest[i].level == p_ptr->current_floor_ptr->dun_level) &&
900 (creature_ptr->dungeon_idx == quest[i].dungeon) &&
901 !(quest[i].flags & QUEST_FLAG_PRESET))
903 quest_r_idx = quest[i].r_idx;
907 /* Maintain quest monsters */
908 for (i = 1; i < p_ptr->current_floor_ptr->m_max; i++)
911 monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[i];
913 if (!monster_is_valid(m_ptr)) continue;
915 /* Only maintain quest monsters */
916 if (quest_r_idx != m_ptr->r_idx) continue;
918 /* Extract real monster race */
919 r_ptr = real_r_ptr(m_ptr);
921 /* Ignore unique monsters */
922 if ((r_ptr->flags1 & RF1_UNIQUE) ||
923 (r_ptr->flags7 & RF7_NAZGUL)) continue;
925 /* Delete non-unique quest monsters */
926 delete_monster_idx(i);
929 /* Check if there is a same item */
930 for (i = 0; i < INVEN_PACK; i++)
932 object_type *o_ptr = &creature_ptr->inventory_list[i];
934 if (!OBJECT_IS_VALID(o_ptr)) continue;
936 /* Delete old memorized location of the artifact */
937 if (object_is_fixed_artifact(o_ptr))
939 a_info[o_ptr->name1].floor_id = 0;
943 /* Extract current floor info or NULL */
944 sf_ptr = get_sf_ptr(tmp_floor_idx);
946 /* Choose random stairs */
947 if ((creature_ptr->change_floor_mode & CFM_RAND_CONNECT) && tmp_floor_idx)
949 locate_connected_stairs(creature_ptr, creature_ptr->current_floor_ptr, sf_ptr, creature_ptr->change_floor_mode);
952 /* Extract new dungeon level */
953 if (creature_ptr->change_floor_mode & CFM_SAVE_FLOORS)
955 /* Extract stair position */
956 g_ptr = &p_ptr->current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
957 f_ptr = &f_info[g_ptr->feat];
959 /* Get back to old saved floor? */
960 if (g_ptr->special && !have_flag(f_ptr->flags, FF_SPECIAL) && get_sf_ptr(g_ptr->special))
962 /* Saved floor is exist. Use it. */
963 new_floor_id = g_ptr->special;
966 /* Mark shaft up/down */
967 if (have_flag(f_ptr->flags, FF_STAIRS) && have_flag(f_ptr->flags, FF_SHAFT))
969 prepare_change_floor_mode(CFM_SHAFT);
973 /* Climb up/down some sort of stairs */
974 if (creature_ptr->change_floor_mode & (CFM_DOWN | CFM_UP))
978 /* Extract level movement number */
979 if (creature_ptr->change_floor_mode & CFM_DOWN) move_num = 1;
980 else if (creature_ptr->change_floor_mode & CFM_UP) move_num = -1;
982 /* Shafts are deeper than normal stairs */
983 if (creature_ptr->change_floor_mode & CFM_SHAFT)
984 move_num += SGN(move_num);
986 /* Get out from or Enter the dungeon */
987 if (creature_ptr->change_floor_mode & CFM_DOWN)
989 if (!p_ptr->current_floor_ptr->dun_level)
990 move_num = d_info[creature_ptr->dungeon_idx].mindepth;
992 else if (creature_ptr->change_floor_mode & CFM_UP)
994 if (p_ptr->current_floor_ptr->dun_level + move_num < d_info[creature_ptr->dungeon_idx].mindepth)
995 move_num = -p_ptr->current_floor_ptr->dun_level;
998 p_ptr->current_floor_ptr->dun_level += move_num;
1001 /* Leaving the dungeon to town */
1002 if (!p_ptr->current_floor_ptr->dun_level && creature_ptr->dungeon_idx)
1004 creature_ptr->leaving_dungeon = TRUE;
1005 if (!vanilla_town && !lite_town)
1007 creature_ptr->wilderness_y = d_info[creature_ptr->dungeon_idx].dy;
1008 creature_ptr->wilderness_x = d_info[creature_ptr->dungeon_idx].dx;
1010 creature_ptr->recall_dungeon = creature_ptr->dungeon_idx;
1011 creature_ptr->dungeon_idx = 0;
1013 /* Reach to the surface -- Clear all saved floors */
1014 creature_ptr->change_floor_mode &= ~CFM_SAVE_FLOORS; // TODO
1017 /* Kill some old saved floors */
1018 if (!(creature_ptr->change_floor_mode & CFM_SAVE_FLOORS))
1020 /* Kill all saved floors */
1021 for (i = 0; i < MAX_SAVED_FLOORS; i++)
1022 kill_saved_floor(&saved_floors[i]);
1024 /* Reset visit_mark count */
1025 latest_visit_mark = 1;
1027 else if (creature_ptr->change_floor_mode & CFM_NO_RETURN)
1029 /* Kill current floor */
1030 kill_saved_floor(sf_ptr);
1033 /* No current floor -- Left/Enter dungeon etc... */
1036 /* No longer need to save current floor */
1040 /* Mark next floor_id on the previous floor */
1044 new_floor_id = get_new_floor_id();
1046 /* Connect from here */
1047 if (g_ptr && !feat_uses_special(g_ptr->feat))
1049 g_ptr->special = tmp_floor_idx;
1053 /* Fix connection -- level teleportation or trap door */
1054 if (creature_ptr->change_floor_mode & CFM_RAND_CONNECT)
1056 if (creature_ptr->change_floor_mode & CFM_UP)
1057 sf_ptr->upper_floor_id = new_floor_id;
1058 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1059 sf_ptr->lower_floor_id = new_floor_id;
1062 /* If you can return, you need to save previous floor */
1063 if ((creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) &&
1064 !(creature_ptr->change_floor_mode & CFM_NO_RETURN))
1066 /* Get out of the my way! */
1067 get_out_monster(p_ptr->current_floor_ptr, creature_ptr);
1069 /* Record the last visit turn of current floor */
1070 sf_ptr->last_visit = current_world_ptr->game_turn;
1072 forget_lite(p_ptr->current_floor_ptr);
1074 clear_mon_lite(p_ptr->current_floor_ptr);
1076 /* Save current floor */
1077 if (!save_floor(sf_ptr, 0))
1079 /* Save failed -- No return */
1080 prepare_change_floor_mode(CFM_NO_RETURN);
1082 /* Kill current floor */
1083 kill_saved_floor(get_sf_ptr(creature_ptr->floor_id));
1090 * @brief フロアの切り替え処理 / Enter new floor.
1093 * If the floor is an old saved floor, it will be\n
1094 * restored from the temporal file. If the floor is new one, new p_ptr->current_floor_ptr->grid_array\n
1095 * will be generated.\n
1097 void change_floor(player_type *creature_ptr)
1099 saved_floor_type *sf_ptr;
1100 bool loaded = FALSE;
1102 /* The dungeon is not ready */
1103 current_world_ptr->character_dungeon = FALSE;
1105 /* No longer in the trap detecteded region */
1106 creature_ptr->dtrap = FALSE;
1108 /* Mega-Hack -- no panel yet */
1114 /* Mega-Hack -- not ambushed on the wildness? */
1115 creature_ptr->ambush_flag = FALSE;
1117 /* No saved floors (On the surface etc.) */
1118 if (!(creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) &&
1119 !(creature_ptr->change_floor_mode & CFM_FIRST_FLOOR))
1121 generate_floor(creature_ptr->current_floor_ptr);
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 (creature_ptr->change_floor_mode & CFM_NO_RETURN)
1151 grid_type *g_ptr = &creature_ptr->current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
1153 if (!feat_uses_special(g_ptr->feat))
1155 if (creature_ptr->change_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 (creature_ptr->floor_id)
1176 saved_floor_type *cur_sf_ptr = get_sf_ptr(creature_ptr->floor_id);
1178 if (creature_ptr->change_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 = creature_ptr->floor_id;
1184 else if (creature_ptr->change_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 = creature_ptr->floor_id;
1192 /* Break connection to killed floor */
1195 if (creature_ptr->change_floor_mode & CFM_UP)
1196 sf_ptr->lower_floor_id = 0;
1197 else if (creature_ptr->change_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[creature_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 < creature_ptr->current_floor_ptr->m_max; i++)
1216 monster_race *r_ptr;
1217 monster_type *m_ptr = &creature_ptr->current_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 < creature_ptr->current_floor_ptr->o_max; i++)
1253 object_type *o_ptr = &creature_ptr->current_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(creature_ptr->current_floor_ptr, creature_ptr);
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 */
1298 build_dead_end(creature_ptr->current_floor_ptr);
1300 /* Break connection */
1301 if (creature_ptr->change_floor_mode & CFM_UP)
1303 sf_ptr->upper_floor_id = 0;
1305 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1307 sf_ptr->lower_floor_id = 0;
1312 generate_floor(creature_ptr->current_floor_ptr);
1315 /* Record last visit turn */
1316 sf_ptr->last_visit = current_world_ptr->game_turn;
1318 /* Set correct dun_level value */
1319 sf_ptr->dun_level = creature_ptr->current_floor_ptr->dun_level;
1321 /* Create connected stairs */
1322 if (!(creature_ptr->change_floor_mode & CFM_NO_RETURN))
1324 /* Extract stair position */
1325 grid_type *g_ptr = &creature_ptr->current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
1327 /*** Create connected stairs ***/
1329 /* No stairs down from Quest */
1330 if ((creature_ptr->change_floor_mode & CFM_UP) && !quest_number(creature_ptr->current_floor_ptr->dun_level))
1332 g_ptr->feat = (creature_ptr->change_floor_mode & CFM_SHAFT) ? feat_state(feat_down_stair, FF_SHAFT) : feat_down_stair;
1335 /* No stairs up when ironman_downward */
1336 else if ((creature_ptr->change_floor_mode & CFM_DOWN) && !ironman_downward)
1338 g_ptr->feat = (creature_ptr->change_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 = creature_ptr->floor_id;
1349 /* Arrive at random grid */
1350 if (creature_ptr->change_floor_mode & (CFM_RAND_PLACE))
1352 (void)new_player_spot(creature_ptr);
1355 /* You see stairs blocked */
1356 else if ((creature_ptr->change_floor_mode & CFM_NO_RETURN) && (creature_ptr->change_floor_mode & (CFM_DOWN | CFM_UP)))
1358 if (!creature_ptr->blind)
1360 msg_print(_("突然階段が塞がれてしまった。", "Suddenly the stairs is blocked!"));
1364 msg_print(_("ゴトゴトと何か音がした。", "You hear some noises."));
1371 * The "turn" is not always different number because
1372 * the level teleport doesn't take any turn. Use
1373 * visit mark instead of last visit turn to find the
1374 * oldest saved floor.
1376 sf_ptr->visit_mark = latest_visit_mark++;
1379 /* Place preserved pet monsters */
1380 place_pet(creature_ptr);
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 creature_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 (creature_ptr->pseikaku == SEIKAKU_MUNCHKIN)
1396 wiz_lite(creature_ptr, (bool)(creature_ptr->pclass == CLASS_NINJA));
1398 /* Remember when this level was "created" */
1399 creature_ptr->current_floor_ptr->generated_turn = current_world_ptr->game_turn;
1401 /* No dungeon feeling yet */
1402 creature_ptr->feeling_turn = creature_ptr->current_floor_ptr->generated_turn;
1403 creature_ptr->feeling = 0;
1405 /* Clear all flags */
1406 creature_ptr->change_floor_mode = 0L;
1408 select_floor_music(creature_ptr);
1409 creature_ptr->change_floor_mode = 0;