2 * @brief 保存された階の管理 / management of the saved floor
5 * Copyright (c) 2002 Mogami \n
6 * This software may be copied and distributed for educational, research, and \n
7 * not for profit purposes provided that this copyright and statement are \n
8 * included in all such copies. \n
9 * 2014 Deskull rearranged comment for Doxygen. \n
12 #include "floor/floor-save.h"
13 #include "action/travel-execution.h"
14 #include "cmd-building/cmd-building.h"
15 #include "cmd-io/cmd-dump.h"
16 #include "core/asking-player.h"
17 #include "dungeon/dungeon.h"
18 #include "dungeon/quest.h"
19 #include "floor/floor-events.h"
20 #include "floor/floor-generate.h"
21 #include "floor/floor-object.h"
22 #include "floor/geometry.h"
23 #include "floor/wild.h"
24 #include "game-option/birth-options.h"
25 #include "game-option/play-record-options.h"
26 #include "grid/feature.h"
27 #include "grid/grid.h"
28 #include "inventory/inventory-slot-types.h"
29 #include "io/files-util.h"
30 #include "io/uid-checker.h"
31 #include "io/write-diary.h"
32 #include "main/sound-of-music.h"
33 #include "mind/mind-mirror-master.h"
34 #include "mind/mind-ninja.h"
35 #include "monster-floor/monster-generator.h"
36 #include "monster-floor/monster-remover.h"
37 #include "monster-floor/monster-summon.h"
38 #include "monster-race/monster-race.h"
39 #include "monster-race/race-flags1.h"
40 #include "monster-race/race-flags2.h"
41 #include "monster-race/race-flags7.h"
42 #include "monster/monster-describer.h"
43 #include "monster/monster-description-types.h"
44 #include "monster/monster-flag-types.h"
45 #include "monster/monster-info.h"
46 #include "monster/monster-list.h"
47 #include "monster/monster-status.h"
48 #include "monster/monster-update.h"
49 #include "monster/smart-learn-types.h"
50 #include "object-hook/hook-checker.h"
51 #include "object-hook/hook-enchant.h"
52 #include "pet/pet-util.h"
53 #include "player/player-class.h"
54 #include "player/player-personalities-types.h"
55 #include "player/special-defense-types.h"
56 #include "savedata/floor-loader.h"
57 #include "savedata/save.h"
58 #include "spell-kind/spells-floor.h"
59 #include "system/artifact-type-definition.h"
60 #include "system/floor-type-definition.h"
61 #include "system/system-variables.h"
62 #include "util/angband-files.h"
63 #include "util/bit-flags-calculator.h"
64 #include "view/display-messages.h"
65 #include "window/main-window-util.h"
66 #include "world/world.h"
70 static FLOOR_IDX new_floor_id; /*!<次のフロアのID / floor_id of the destination */
71 static u32b latest_visit_mark; /*!<フロアを渡った回数?(確認中) / Max number of visit_mark */
72 #define MAX_PARTY_MON 21 /*!< フロア移動時に先のフロアに連れて行けるペットの最大数 Maximum number of preservable pets */
73 static monster_type party_mon[MAX_PARTY_MON]; /*!< フロア移動に保存するペットモンスターの配列 */
76 * Number of floor_id used from birth
78 FLOOR_IDX max_floor_id;
81 * Sign for current process used in temporary files.
82 * Actually it is the start time of current process.
84 u32b saved_floor_file_sign;
86 saved_floor_type saved_floors[MAX_SAVED_FLOORS];
89 * @brief 保存フロア配列を初期化する / Initialize saved_floors array.
90 * @param creature_ptr プレーヤーへの参照ポインタ
91 * @param force テンポラリファイルが残っていた場合も警告なしで強制的に削除するフラグ
92 * @details Make sure that old temporary files are not remaining as gurbages.
95 void init_saved_floors(player_type *creature_ptr, bool force)
97 char floor_savefile[1024];
100 BIT_FLAGS mode = 0644;
101 for (i = 0; i < MAX_SAVED_FLOORS; i++) {
102 saved_floor_type *sf_ptr = &saved_floors[i];
105 sprintf(floor_savefile, "%s.F%02d", savefile, i);
107 /* Grab permissions */
108 safe_setuid_grab(creature_ptr);
110 /* Try to create the file */
111 fd = fd_make(floor_savefile, mode);
113 /* Drop permissions */
119 msg_print(_("エラー:古いテンポラリ・ファイルが残っています。", "Error: There are old temporary files."));
120 msg_print(_("変愚蛮怒を二重に起動していないか確認してください。", "Make sure you are not running two game processes simultaneously."));
121 msg_print(_("過去に変愚蛮怒がクラッシュした場合は一時ファイルを", "If the temporary files are garbage from an old crashed process, "));
122 msg_print(_("強制的に削除して実行を続けられます。", "you can delete them safely."));
123 if (!get_check(_("強制的に削除してもよろしいですか?", "Do you delete the old temporary files? ")))
124 quit(_("実行中止", "Aborted."));
132 /* Grab permissions */
133 safe_setuid_grab(creature_ptr);
135 /* Simply kill the temporary file */
136 (void)fd_kill(floor_savefile);
138 /* Drop permissions */
141 sf_ptr->floor_id = 0;
144 /* No floor_id used yet (No.0 is reserved to indicate non existance) */
147 /* vist_mark is from 1 */
148 latest_visit_mark = 1;
150 /* A sign to mark temporary files */
151 saved_floor_file_sign = (u32b)time(NULL);
153 /* No next floor yet */
156 /* No change floor mode yet */
157 creature_ptr->change_floor_mode = 0;
161 * @brief 保存フロア用テンポラリファイルを削除する / Kill temporary files
162 * @details Should be called just before the game quit.
163 * @param creature_ptr プレーヤーへの参照ポインタ
166 void clear_saved_floor_files(player_type *creature_ptr)
168 char floor_savefile[1024];
170 for (i = 0; i < MAX_SAVED_FLOORS; i++) {
171 saved_floor_type *sf_ptr = &saved_floors[i];
173 /* No temporary file */
174 if (!sf_ptr->floor_id)
176 if (sf_ptr->floor_id == creature_ptr->floor_id)
180 sprintf(floor_savefile, "%s.F%02d", savefile, i);
182 /* Grab permissions */
183 safe_setuid_grab(creature_ptr);
185 /* Simply kill the temporary file */
186 (void)fd_kill(floor_savefile);
188 /* Drop permissions */
194 * @brief 保存フロアIDから参照ポインタを得る / Get a pointer for an item of the saved_floors array.
195 * @param floor_id 保存フロアID
196 * @return IDに対応する保存フロアのポインタ、ない場合はNULLを返す。
198 saved_floor_type *get_sf_ptr(FLOOR_IDX floor_id)
202 /* floor_id No.0 indicates no floor */
206 for (i = 0; i < MAX_SAVED_FLOORS; i++) {
207 saved_floor_type *sf_ptr = &saved_floors[i];
209 if (sf_ptr->floor_id == floor_id)
218 * @brief 参照ポインタ先の保存フロアを抹消する / kill a saved floor and get an empty space
219 * @param creature_ptr プレーヤーへの参照ポインタ
220 * @param sf_ptr 保存フロアの参照ポインタ
223 static void kill_saved_floor(player_type *creature_ptr, saved_floor_type *sf_ptr)
225 char floor_savefile[1024];
230 if (!sf_ptr->floor_id)
233 if (sf_ptr->floor_id == creature_ptr->floor_id) {
234 /* Kill current floor */
235 creature_ptr->floor_id = 0;
237 /* Current floor doesn't have temporary file */
240 sprintf(floor_savefile, "%s.F%02d", savefile, (int)sf_ptr->savefile_id);
242 /* Grab permissions */
243 safe_setuid_grab(creature_ptr);
245 /* Simply kill the temporary file */
246 (void)fd_kill(floor_savefile);
248 /* Drop permissions */
252 /* No longer exists */
253 sf_ptr->floor_id = 0;
257 * @brief 新規に利用可能な保存フロアを返す / Initialize new saved floor and get its floor id.
258 * @param creature_ptr プレーヤーへの参照ポインタ
259 * @return 利用可能な保存フロアID
261 * If number of saved floors are already MAX_SAVED_FLOORS, kill the oldest one.
263 FLOOR_IDX get_new_floor_id(player_type *creature_ptr)
265 saved_floor_type *sf_ptr = NULL;
268 /* Look for empty space */
269 for (i = 0; i < MAX_SAVED_FLOORS; i++) {
270 sf_ptr = &saved_floors[i];
272 if (!sf_ptr->floor_id)
277 if (i == MAX_SAVED_FLOORS) {
279 u32b oldest_visit = 0xffffffffL;
281 /* Search for oldest */
282 for (i = 0; i < MAX_SAVED_FLOORS; i++) {
283 sf_ptr = &saved_floors[i];
285 /* Don't kill current floor */
286 if (sf_ptr->floor_id == creature_ptr->floor_id)
289 /* Don't kill newer */
290 if (sf_ptr->visit_mark > oldest_visit)
294 oldest_visit = sf_ptr->visit_mark;
297 /* Kill oldest saved floor */
298 sf_ptr = &saved_floors[oldest];
299 kill_saved_floor(creature_ptr, sf_ptr);
305 /* Prepare new floor data */
306 sf_ptr->savefile_id = i;
307 sf_ptr->floor_id = max_floor_id;
308 sf_ptr->last_visit = 0;
309 sf_ptr->upper_floor_id = 0;
310 sf_ptr->lower_floor_id = 0;
311 sf_ptr->visit_mark = latest_visit_mark++;
313 /* sf_ptr->dun_level may be changed later */
314 sf_ptr->dun_level = creature_ptr->current_floor_ptr->dun_level;
316 /* Increment number of floor_id */
317 if (max_floor_id < MAX_SHORT)
320 /* 32767 floor_ids are all used up! Re-use ancient IDs */
324 return sf_ptr->floor_id;
328 * @brief フロア切り替え時の処理フラグを追加する / Prepare mode flags of changing floor
329 * @param creature_ptr プレーヤーへの参照ポインタ
330 * @param mode 追加したい所持フラグ
333 void prepare_change_floor_mode(player_type *creature_ptr, BIT_FLAGS mode) { creature_ptr->change_floor_mode |= mode; }
336 * @brief 階段移動先のフロアが生成できない時に簡単な行き止まりマップを作成する / Builds the dead end
339 static void build_dead_end(player_type *creature_ptr)
343 clear_cave(creature_ptr);
345 /* Mega-Hack -- no player yet */
346 creature_ptr->x = creature_ptr->y = 0;
348 /* Fill the arrays of floors and walls in the good proportions */
349 set_floor_and_wall(0);
352 creature_ptr->current_floor_ptr->height = SCREEN_HGT;
353 creature_ptr->current_floor_ptr->width = SCREEN_WID;
355 /* Filled with permanent walls */
356 for (y = 0; y < MAX_HGT; y++) {
357 for (x = 0; x < MAX_WID; x++) {
358 /* Create "solid" perma-wall */
359 place_bold(creature_ptr, y, x, GB_SOLID_PERM);
363 /* Place at center of the floor */
364 creature_ptr->y = creature_ptr->current_floor_ptr->height / 2;
365 creature_ptr->x = creature_ptr->current_floor_ptr->width / 2;
367 /* Give one square */
368 place_bold(creature_ptr, creature_ptr->y, creature_ptr->x, GB_FLOOR);
370 wipe_generate_random_floor_flags(creature_ptr->current_floor_ptr);
374 * @brief フロア移動時のペット保存処理 / Preserve_pets
375 * @param master_ptr プレーヤーへの参照ポインタ
378 static void preserve_pet(player_type *master_ptr)
383 for (num = 0; num < MAX_PARTY_MON; num++) {
384 party_mon[num].r_idx = 0;
387 if (master_ptr->riding) {
388 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[master_ptr->riding];
390 /* Pet of other pet don't follow. */
391 if (m_ptr->parent_m_idx) {
392 master_ptr->riding = 0;
393 master_ptr->pet_extra_flags &= ~(PF_TWO_HANDS);
394 master_ptr->riding_ryoute = master_ptr->old_riding_ryoute = FALSE;
396 /* Preserve the mount */
397 (void)COPY(&party_mon[0], m_ptr, monster_type);
399 /* Delete from this floor */
400 delete_monster_idx(master_ptr, master_ptr->riding);
405 * If player is in wild mode, no pets are preserved
406 * except a monster whom player riding
408 if (!master_ptr->wild_mode && !master_ptr->current_floor_ptr->inside_arena && !master_ptr->phase_out) {
409 for (i = master_ptr->current_floor_ptr->m_max - 1, num = 1; (i >= 1 && num < MAX_PARTY_MON); i--) {
410 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[i];
412 if (!monster_is_valid(m_ptr))
416 if (i == master_ptr->riding)
419 if (reinit_wilderness) {
420 /* Don't lose sight of pets when getting a Quest */
422 POSITION dis = distance(master_ptr->y, master_ptr->x, m_ptr->fy, m_ptr->fx);
424 /* Confused (etc.) monsters don't follow. */
425 if (monster_confused_remaining(m_ptr) || monster_stunned_remaining(m_ptr) || monster_csleep_remaining(m_ptr))
428 /* Pet of other pet don't follow. */
429 if (m_ptr->parent_m_idx)
433 * Pets with nickname will follow even from 3 blocks away
434 * when you or the pet can see the other.
437 && ((player_has_los_bold(master_ptr, m_ptr->fy, m_ptr->fx) && projectable(master_ptr, master_ptr->y, master_ptr->x, m_ptr->fy, m_ptr->fx))
438 || (los(master_ptr, m_ptr->fy, m_ptr->fx, master_ptr->y, master_ptr->x)
439 && projectable(master_ptr, m_ptr->fy, m_ptr->fx, master_ptr->y, master_ptr->x)))) {
448 (void)COPY(&party_mon[num], &master_ptr->current_floor_ptr->m_list[i], monster_type);
452 /* Delete from this floor */
453 delete_monster_idx(master_ptr, i);
457 if (record_named_pet) {
458 for (i = master_ptr->current_floor_ptr->m_max - 1; i >= 1; i--) {
459 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[i];
460 GAME_TEXT m_name[MAX_NLEN];
462 if (!monster_is_valid(m_ptr))
466 if (!m_ptr->nickname)
468 if (master_ptr->riding == i)
471 monster_desc(master_ptr, m_name, m_ptr, MD_ASSUME_VISIBLE | MD_INDEF_VISIBLE);
472 exe_write_diary(master_ptr, DIARY_NAMED_PET, RECORD_NAMED_PET_MOVED, m_name);
476 /* Pet of other pet may disappear. */
477 for (i = master_ptr->current_floor_ptr->m_max - 1; i >= 1; i--) {
478 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[i];
480 /* Are there its parent? */
481 if (m_ptr->parent_m_idx && !master_ptr->current_floor_ptr->m_list[m_ptr->parent_m_idx].r_idx) {
482 /* Its parent have gone, it also goes away. */
484 if (is_seen(master_ptr, m_ptr)) {
485 GAME_TEXT m_name[MAX_NLEN];
486 monster_desc(master_ptr, m_name, m_ptr, 0);
487 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
490 delete_monster_idx(master_ptr, i);
496 * @brief フロア移動時にペットを伴った場合の準備処理 / Pre-calculate the racial counters of preserved pets
497 * @param master_ptr プレーヤーへの参照ポインタ
500 * To prevent multiple generation of unique monster who is the minion of player
502 void precalc_cur_num_of_pet(player_type *player_ptr)
506 int max_num = player_ptr->wild_mode ? 1 : MAX_PARTY_MON;
508 for (i = 0; i < max_num; i++) {
509 m_ptr = &party_mon[i];
511 /* Skip empty monsters */
512 if (!monster_is_valid(m_ptr))
515 /* Hack -- Increase the racial counter */
516 real_r_ptr(m_ptr)->cur_num++;
521 * @brief 移動先のフロアに伴ったペットを配置する / Place preserved pet monsters on new floor
522 * @param master_ptr プレーヤーへの参照ポインタ
525 static void place_pet(player_type *master_ptr)
528 int max_num = master_ptr->wild_mode ? 1 : MAX_PARTY_MON;
529 floor_type *floor_ptr = master_ptr->current_floor_ptr;
530 for (i = 0; i < max_num; i++) {
531 POSITION cy = 0, cx = 0;
534 if (!(party_mon[i].r_idx))
538 m_idx = m_pop(floor_ptr);
539 master_ptr->riding = m_idx;
548 for (d = 1; d < A_MAX; d++) {
549 for (j = 1000; j > 0; j--) {
550 scatter(master_ptr, &cy, &cx, master_ptr->y, master_ptr->x, d, 0);
551 if (monster_can_enter(master_ptr, cy, cx, &r_info[party_mon[i].r_idx], 0))
557 m_idx = (d == 6) ? 0 : m_pop(floor_ptr);
561 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[m_idx];
564 master_ptr->current_floor_ptr->grid_array[cy][cx].m_idx = m_idx;
566 m_ptr->r_idx = party_mon[i].r_idx;
568 /* Copy all member of the structure */
569 *m_ptr = party_mon[i];
570 r_ptr = real_r_ptr(m_ptr);
574 m_ptr->current_floor_ptr = master_ptr->current_floor_ptr;
576 m_ptr->mtimed[MTIMED_CSLEEP] = 0;
577 m_ptr->hold_o_idx = 0;
580 if ((r_ptr->flags1 & RF1_FORCE_SLEEP) && !ironman_nightmare) {
581 /* Monster is still being nice */
582 m_ptr->mflag |= (MFLAG_NICE);
584 /* Must repair monsters */
585 repair_monsters = TRUE;
587 update_monster(master_ptr, m_idx, TRUE);
588 lite_spot(master_ptr, cy, cx);
590 /* Pre-calculated in precalc_cur_num_of_pet() */
591 /* r_ptr->cur_num++; */
593 /* Hack -- Count the number of "reproducers" */
594 if (r_ptr->flags2 & RF2_MULTIPLY)
595 master_ptr->current_floor_ptr->num_repro++;
598 monster_type *m_ptr = &party_mon[i];
599 monster_race *r_ptr = real_r_ptr(m_ptr);
600 GAME_TEXT m_name[MAX_NLEN];
602 monster_desc(master_ptr, m_name, m_ptr, 0);
603 msg_format(_("%sとはぐれてしまった。", "You have lost sight of %s."), m_name);
604 if (record_named_pet && m_ptr->nickname) {
605 monster_desc(master_ptr, m_name, m_ptr, MD_INDEF_VISIBLE);
606 exe_write_diary(master_ptr, DIARY_NAMED_PET, RECORD_NAMED_PET_LOST_SIGHT, m_name);
609 /* Pre-calculated in precalc_cur_num_of_pet(), but need to decrease */
615 /* For accuracy of precalc_cur_num_of_pet() */
616 (void)C_WIPE(party_mon, MAX_PARTY_MON, monster_type);
620 * @brief ユニークモンスターやアーティファクトの所在フロアを更新する / Hack -- Update location of unique monsters and artifacts
621 * @param cur_floor_id 現在のフロアID
624 * The r_ptr->floor_id and a_ptr->floor_id are not updated correctly\n
625 * while new floor creation since dungeons may be re-created by\n
626 * auto-scum option.\n
628 static void update_unique_artifact(floor_type *floor_ptr, s16b cur_floor_id)
632 /* Maintain unique monsters */
633 for (i = 1; i < floor_ptr->m_max; i++) {
635 monster_type *m_ptr = &floor_ptr->m_list[i];
637 if (!monster_is_valid(m_ptr))
640 /* Extract real monster race */
641 r_ptr = real_r_ptr(m_ptr);
643 /* Memorize location of the unique monster */
644 if ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)) {
645 r_ptr->floor_id = cur_floor_id;
649 /* Maintain artifatcs */
650 for (i = 1; i < floor_ptr->o_max; i++) {
651 object_type *o_ptr = &floor_ptr->o_list[i];
653 if (!object_is_valid(o_ptr))
656 /* Memorize location of the artifact */
657 if (object_is_fixed_artifact(o_ptr)) {
658 a_info[o_ptr->name1].floor_id = cur_floor_id;
664 * @brief フロア移動時、プレイヤーの移動先モンスターが既にいた場合ランダムな近隣に移動させる / When a monster is at a place where player will return,
667 static void get_out_monster(player_type *protected_ptr)
671 POSITION oy = protected_ptr->y;
672 POSITION ox = protected_ptr->x;
673 floor_type *floor_ptr = protected_ptr->current_floor_ptr;
674 MONSTER_IDX m_idx = floor_ptr->grid_array[oy][ox].m_idx;
676 /* Nothing to do if no monster */
680 /* Look until done */
684 /* Pick a (possibly illegal) location */
685 POSITION ny = rand_spread(oy, dis);
686 POSITION nx = rand_spread(ox, dis);
690 /* Stop after 1000 tries */
695 * Increase distance after doing enough tries
696 * compared to area of possible space
698 if (tries > 20 * dis * dis)
701 /* Ignore illegal locations */
702 if (!in_bounds(floor_ptr, ny, nx))
705 /* Require "empty" floor space */
706 if (!is_cave_empty_bold(protected_ptr, ny, nx))
709 /* Hack -- no teleport onto glyph of warding */
710 if (is_glyph_grid(&floor_ptr->grid_array[ny][nx]))
712 if (is_explosive_rune_grid(&floor_ptr->grid_array[ny][nx]))
715 /* ...nor onto the Pattern */
716 if (pattern_tile(floor_ptr, ny, nx))
719 /*** It's a good place ***/
721 m_ptr = &floor_ptr->m_list[m_idx];
723 /* Update the old location */
724 floor_ptr->grid_array[oy][ox].m_idx = 0;
726 /* Update the new location */
727 floor_ptr->grid_array[ny][nx].m_idx = m_idx;
729 /* Move the monster */
733 /* No need to do update_monster() */
741 * @brief 新フロアに移動元フロアに繋がる階段を配置する / Virtually teleport onto the stairs that is connecting between two floors.
742 * @param sf_ptr 移動元の保存フロア構造体参照ポインタ
745 static void locate_connected_stairs(player_type *creature_ptr, floor_type *floor_ptr, saved_floor_type *sf_ptr, BIT_FLAGS floor_mode)
747 POSITION x, y, sx = 0, sy = 0;
748 POSITION x_table[20];
749 POSITION y_table[20];
753 /* Search usable stairs */
754 for (y = 0; y < floor_ptr->height; y++) {
755 for (x = 0; x < floor_ptr->width; x++) {
756 grid_type *g_ptr = &floor_ptr->grid_array[y][x];
757 feature_type *f_ptr = &f_info[g_ptr->feat];
760 if (floor_mode & CFM_UP) {
761 if (have_flag(f_ptr->flags, FF_LESS) && have_flag(f_ptr->flags, FF_STAIRS) && !have_flag(f_ptr->flags, FF_SPECIAL)) {
764 /* Found fixed stairs? */
765 if (g_ptr->special && g_ptr->special == sf_ptr->upper_floor_id) {
772 else if (floor_mode & CFM_DOWN) {
773 if (have_flag(f_ptr->flags, FF_MORE) && have_flag(f_ptr->flags, FF_STAIRS) && !have_flag(f_ptr->flags, FF_SPECIAL)) {
776 /* Found fixed stairs */
777 if (g_ptr->special && g_ptr->special == sf_ptr->lower_floor_id) {
785 if (have_flag(f_ptr->flags, FF_BLDG)) {
790 if (ok && (num < 20)) {
800 creature_ptr->y = sy;
801 creature_ptr->x = sx;
803 /* No stairs found! -- No return */
804 prepare_change_floor_mode(creature_ptr, CFM_RAND_PLACE | CFM_NO_RETURN);
806 /* Mega Hack -- It's not the stairs you enter. Disable it. */
807 if (!feat_uses_special(floor_ptr->grid_array[creature_ptr->y][creature_ptr->x].feat))
808 floor_ptr->grid_array[creature_ptr->y][creature_ptr->x].special = 0;
810 /* Choose random one */
813 /* Point stair location */
814 creature_ptr->y = y_table[i];
815 creature_ptr->x = x_table[i];
820 * @brief 現在のフロアを離れるに伴って行なわれる保存処理
821 * / Maintain quest monsters, mark next floor_id at stairs, save current floor, and prepare to enter next floor.
822 * @param creature_ptr プレーヤーへの参照ポインタ
825 void leave_floor(player_type *creature_ptr)
827 grid_type *g_ptr = NULL;
829 saved_floor_type *sf_ptr;
830 MONRACE_IDX quest_r_idx = 0;
832 FLOOR_IDX tmp_floor_idx = 0;
834 /* Preserve pets and prepare to take these to next floor */
835 preserve_pet(creature_ptr);
837 /* Remove all mirrors without explosion */
838 remove_all_mirrors(creature_ptr, FALSE);
840 if (creature_ptr->special_defense & NINJA_S_STEALTH)
841 set_superstealth(creature_ptr, FALSE);
843 /* New floor is not yet prepared */
846 /* Temporarily get a floor_id (for Arena) */
847 if (!creature_ptr->floor_id && (creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) && !(creature_ptr->change_floor_mode & CFM_NO_RETURN)) {
848 /* Get temporary floor_id */
849 tmp_floor_idx = get_new_floor_id(creature_ptr);
852 /* Search the quest monster index */
853 for (i = 0; i < max_q_idx; i++) {
854 if ((quest[i].status == QUEST_STATUS_TAKEN) && ((quest[i].type == QUEST_TYPE_KILL_LEVEL) || (quest[i].type == QUEST_TYPE_RANDOM))
855 && (quest[i].level == creature_ptr->current_floor_ptr->dun_level) && (creature_ptr->dungeon_idx == quest[i].dungeon)
856 && !(quest[i].flags & QUEST_FLAG_PRESET)) {
857 quest_r_idx = quest[i].r_idx;
861 /* Maintain quest monsters */
862 for (i = 1; i < creature_ptr->current_floor_ptr->m_max; i++) {
864 monster_type *m_ptr = &creature_ptr->current_floor_ptr->m_list[i];
866 if (!monster_is_valid(m_ptr))
869 /* Only maintain quest monsters */
870 if (quest_r_idx != m_ptr->r_idx)
873 /* Extract real monster race */
874 r_ptr = real_r_ptr(m_ptr);
876 /* Ignore unique monsters */
877 if ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL))
880 /* Delete non-unique quest monsters */
881 delete_monster_idx(creature_ptr, i);
884 /* Check if there is a same item */
885 for (i = 0; i < INVEN_PACK; i++) {
886 object_type *o_ptr = &creature_ptr->inventory_list[i];
888 if (!object_is_valid(o_ptr))
891 /* Delete old memorized location of the artifact */
892 if (object_is_fixed_artifact(o_ptr)) {
893 a_info[o_ptr->name1].floor_id = 0;
897 /* Extract current floor info or NULL */
898 sf_ptr = get_sf_ptr(creature_ptr->floor_id);
900 /* Choose random stairs */
901 if ((creature_ptr->change_floor_mode & CFM_RAND_CONNECT) && tmp_floor_idx) {
902 locate_connected_stairs(creature_ptr, creature_ptr->current_floor_ptr, sf_ptr, creature_ptr->change_floor_mode);
905 /* Extract new dungeon level */
906 if (creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) {
907 /* Extract stair position */
908 g_ptr = &creature_ptr->current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
909 f_ptr = &f_info[g_ptr->feat];
911 /* Get back to old saved floor? */
912 if (g_ptr->special && !have_flag(f_ptr->flags, FF_SPECIAL) && get_sf_ptr(g_ptr->special)) {
913 /* Saved floor is exist. Use it. */
914 new_floor_id = g_ptr->special;
917 /* Mark shaft up/down */
918 if (have_flag(f_ptr->flags, FF_STAIRS) && have_flag(f_ptr->flags, FF_SHAFT)) {
919 prepare_change_floor_mode(creature_ptr, CFM_SHAFT);
923 /* Climb up/down some sort of stairs */
924 if (creature_ptr->change_floor_mode & (CFM_DOWN | CFM_UP)) {
927 /* Extract level movement number */
928 if (creature_ptr->change_floor_mode & CFM_DOWN)
930 else if (creature_ptr->change_floor_mode & CFM_UP)
933 /* Shafts are deeper than normal stairs */
934 if (creature_ptr->change_floor_mode & CFM_SHAFT)
935 move_num += SGN(move_num);
937 /* Get out from or Enter the dungeon */
938 if (creature_ptr->change_floor_mode & CFM_DOWN) {
939 if (!creature_ptr->current_floor_ptr->dun_level)
940 move_num = d_info[creature_ptr->dungeon_idx].mindepth;
941 } else if (creature_ptr->change_floor_mode & CFM_UP) {
942 if (creature_ptr->current_floor_ptr->dun_level + move_num < d_info[creature_ptr->dungeon_idx].mindepth)
943 move_num = -creature_ptr->current_floor_ptr->dun_level;
946 creature_ptr->current_floor_ptr->dun_level += move_num;
949 /* Leaving the dungeon to town */
950 if (!creature_ptr->current_floor_ptr->dun_level && creature_ptr->dungeon_idx) {
951 creature_ptr->leaving_dungeon = TRUE;
952 if (!vanilla_town && !lite_town) {
953 creature_ptr->wilderness_y = d_info[creature_ptr->dungeon_idx].dy;
954 creature_ptr->wilderness_x = d_info[creature_ptr->dungeon_idx].dx;
956 creature_ptr->recall_dungeon = creature_ptr->dungeon_idx;
957 creature_ptr->dungeon_idx = 0;
959 /* Reach to the surface -- Clear all saved floors */
960 creature_ptr->change_floor_mode &= ~CFM_SAVE_FLOORS; // TODO
963 /* Kill some old saved floors */
964 if (!(creature_ptr->change_floor_mode & CFM_SAVE_FLOORS)) {
965 /* Kill all saved floors */
966 for (i = 0; i < MAX_SAVED_FLOORS; i++)
967 kill_saved_floor(creature_ptr, &saved_floors[i]);
969 /* Reset visit_mark count */
970 latest_visit_mark = 1;
971 } else if (creature_ptr->change_floor_mode & CFM_NO_RETURN) {
972 /* Kill current floor */
973 kill_saved_floor(creature_ptr, sf_ptr);
976 /* No current floor -- Left/Enter dungeon etc... */
977 if (!creature_ptr->floor_id) {
978 /* No longer need to save current floor */
982 /* Mark next floor_id on the previous floor */
985 new_floor_id = get_new_floor_id(creature_ptr);
987 /* Connect from here */
988 if (g_ptr && !feat_uses_special(g_ptr->feat)) {
989 g_ptr->special = new_floor_id;
993 /* Fix connection -- level teleportation or trap door */
994 if (creature_ptr->change_floor_mode & CFM_RAND_CONNECT) {
995 if (creature_ptr->change_floor_mode & CFM_UP)
996 sf_ptr->upper_floor_id = new_floor_id;
997 else if (creature_ptr->change_floor_mode & CFM_DOWN)
998 sf_ptr->lower_floor_id = new_floor_id;
1001 /* If you can return, you need to save previous floor */
1002 if ((creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) && !(creature_ptr->change_floor_mode & CFM_NO_RETURN)) {
1003 /* Get out of the my way! */
1004 get_out_monster(creature_ptr);
1006 /* Record the last visit turn of current floor */
1007 sf_ptr->last_visit = current_world_ptr->game_turn;
1009 forget_lite(creature_ptr->current_floor_ptr);
1010 forget_view(creature_ptr->current_floor_ptr);
1011 clear_mon_lite(creature_ptr->current_floor_ptr);
1013 /* Save current floor */
1014 if (!save_floor(creature_ptr, sf_ptr, 0)) {
1015 /* Save failed -- No return */
1016 prepare_change_floor_mode(creature_ptr, CFM_NO_RETURN);
1018 /* Kill current floor */
1019 kill_saved_floor(creature_ptr, get_sf_ptr(creature_ptr->floor_id));
1025 * @brief フロアの切り替え処理 / Enter new floor.
1026 * @param creature_ptr プレーヤーへの参照ポインタ
1029 * If the floor is an old saved floor, it will be\n
1030 * restored from the temporary file. If the floor is new one, new floor\n
1031 * will be generated.\n
1033 void change_floor(player_type *creature_ptr)
1035 saved_floor_type *sf_ptr;
1036 bool loaded = FALSE;
1038 /* The dungeon is not ready */
1039 current_world_ptr->character_dungeon = FALSE;
1041 /* No longer in the trap detecteded region */
1042 creature_ptr->dtrap = FALSE;
1044 /* Mega-Hack -- no panel yet */
1050 /* Mega-Hack -- not ambushed on the wildness? */
1051 creature_ptr->ambush_flag = FALSE;
1053 /* No saved floors (On the surface etc.) */
1054 if (!(creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) && !(creature_ptr->change_floor_mode & CFM_FIRST_FLOOR)) {
1055 generate_floor(creature_ptr);
1057 /* Paranoia -- No new saved floor */
1061 /* In the dungeon */
1063 /* No floor_id yet */
1064 if (!new_floor_id) {
1066 new_floor_id = get_new_floor_id(creature_ptr);
1069 /* Pointer for infomations of new floor */
1070 sf_ptr = get_sf_ptr(new_floor_id);
1072 /* Try to restore old floor */
1073 if (sf_ptr->last_visit) {
1074 /* Old saved floor is exist */
1075 if (load_floor(creature_ptr, sf_ptr, 0)) {
1078 /* Forbid return stairs */
1079 if (creature_ptr->change_floor_mode & CFM_NO_RETURN) {
1080 grid_type *g_ptr = &creature_ptr->current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
1082 if (!feat_uses_special(g_ptr->feat)) {
1083 if (creature_ptr->change_floor_mode & (CFM_DOWN | CFM_UP)) {
1084 /* Reset to floor */
1085 g_ptr->feat = feat_ground_type[randint0(100)];
1095 * Set lower/upper_floor_id of new floor when the new
1096 * floor is right-above/right-under the current floor.
1098 * Stair creation/Teleport level/Trap door will take
1099 * you the same floor when you used it later again.
1101 if (creature_ptr->floor_id) {
1102 saved_floor_type *cur_sf_ptr = get_sf_ptr(creature_ptr->floor_id);
1104 if (creature_ptr->change_floor_mode & CFM_UP) {
1105 /* New floor is right-above */
1106 if (cur_sf_ptr->upper_floor_id == new_floor_id)
1107 sf_ptr->lower_floor_id = creature_ptr->floor_id;
1108 } else if (creature_ptr->change_floor_mode & CFM_DOWN) {
1109 /* New floor is right-under */
1110 if (cur_sf_ptr->lower_floor_id == new_floor_id)
1111 sf_ptr->upper_floor_id = creature_ptr->floor_id;
1115 /* Break connection to killed floor */
1117 if (creature_ptr->change_floor_mode & CFM_UP)
1118 sf_ptr->lower_floor_id = 0;
1119 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1120 sf_ptr->upper_floor_id = 0;
1123 /* Maintain monsters and artifacts */
1126 GAME_TURN tmp_last_visit = sf_ptr->last_visit;
1127 GAME_TURN absence_ticks;
1128 int alloc_chance = d_info[creature_ptr->dungeon_idx].max_m_alloc_chance;
1129 GAME_TURN alloc_times;
1131 while (tmp_last_visit > current_world_ptr->game_turn)
1132 tmp_last_visit -= TURNS_PER_TICK * TOWN_DAWN;
1133 absence_ticks = (current_world_ptr->game_turn - tmp_last_visit) / TURNS_PER_TICK;
1135 /* Maintain monsters */
1136 for (i = 1; i < creature_ptr->current_floor_ptr->m_max; i++) {
1137 monster_race *r_ptr;
1138 monster_type *m_ptr = &creature_ptr->current_floor_ptr->m_list[i];
1140 if (!monster_is_valid(m_ptr))
1143 if (!is_pet(m_ptr)) {
1145 m_ptr->hp = m_ptr->maxhp = m_ptr->max_maxhp;
1147 /* Remove timed status (except MTIMED_CSLEEP) */
1148 (void)set_monster_fast(creature_ptr, i, 0);
1149 (void)set_monster_slow(creature_ptr, i, 0);
1150 (void)set_monster_stunned(creature_ptr, i, 0);
1151 (void)set_monster_confused(creature_ptr, i, 0);
1152 (void)set_monster_monfear(creature_ptr, i, 0);
1153 (void)set_monster_invulner(creature_ptr, i, 0, FALSE);
1156 /* Extract real monster race */
1157 r_ptr = real_r_ptr(m_ptr);
1159 /* Ignore non-unique */
1160 if (!(r_ptr->flags1 & RF1_UNIQUE) && !(r_ptr->flags7 & RF7_NAZGUL))
1163 /* Appear at a different floor? */
1164 if (r_ptr->floor_id != new_floor_id) {
1165 /* Disapper from here */
1166 delete_monster_idx(creature_ptr, i);
1170 /* Maintain artifatcs */
1171 for (i = 1; i < creature_ptr->current_floor_ptr->o_max; i++) {
1172 object_type *o_ptr = &creature_ptr->current_floor_ptr->o_list[i];
1174 if (!object_is_valid(o_ptr))
1177 /* Ignore non-artifact */
1178 if (!object_is_fixed_artifact(o_ptr))
1181 /* Appear at a different floor? */
1182 if (a_info[o_ptr->name1].floor_id != new_floor_id) {
1183 /* Disappear from here */
1184 delete_object_idx(creature_ptr, i);
1186 /* Cancel preserve */
1187 a_info[o_ptr->name1].cur_num = 1;
1191 (void)place_quest_monsters(creature_ptr);
1193 /* Place some random monsters */
1194 alloc_times = absence_ticks / alloc_chance;
1196 if (randint0(alloc_chance) < (absence_ticks % alloc_chance))
1199 for (i = 0; i < alloc_times; i++) {
1200 /* Make a (group of) new monster */
1201 (void)alloc_monster(creature_ptr, 0, 0, summon_specific);
1206 /* New floor_id or failed to restore */
1207 else /* if (!loaded) */
1209 if (sf_ptr->last_visit) {
1210 /* Temporary file is broken? */
1211 msg_print(_("階段は行き止まりだった。", "The staircases come to a dead end..."));
1213 /* Create simple dead end */
1214 build_dead_end(creature_ptr);
1216 /* Break connection */
1217 if (creature_ptr->change_floor_mode & CFM_UP) {
1218 sf_ptr->upper_floor_id = 0;
1219 } else if (creature_ptr->change_floor_mode & CFM_DOWN) {
1220 sf_ptr->lower_floor_id = 0;
1223 generate_floor(creature_ptr);
1226 /* Record last visit turn */
1227 sf_ptr->last_visit = current_world_ptr->game_turn;
1229 /* Set correct dun_level value */
1230 sf_ptr->dun_level = creature_ptr->current_floor_ptr->dun_level;
1232 /* Create connected stairs */
1233 if (!(creature_ptr->change_floor_mode & CFM_NO_RETURN)) {
1234 /* Extract stair position */
1235 grid_type *g_ptr = &creature_ptr->current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
1237 /*** Create connected stairs ***/
1239 /* No stairs down from Quest */
1240 if ((creature_ptr->change_floor_mode & CFM_UP) && !quest_number(creature_ptr, creature_ptr->current_floor_ptr->dun_level)) {
1241 g_ptr->feat = (creature_ptr->change_floor_mode & CFM_SHAFT) ? feat_state(creature_ptr, feat_down_stair, FF_SHAFT) : feat_down_stair;
1244 /* No stairs up when ironman_downward */
1245 else if ((creature_ptr->change_floor_mode & CFM_DOWN) && !ironman_downward) {
1246 g_ptr->feat = (creature_ptr->change_floor_mode & CFM_SHAFT) ? feat_state(creature_ptr, feat_up_stair, FF_SHAFT) : feat_up_stair;
1249 /* Paranoia -- Clear mimic */
1252 /* Connect to previous floor */
1253 g_ptr->special = creature_ptr->floor_id;
1257 /* Arrive at random grid */
1258 if (creature_ptr->change_floor_mode & (CFM_RAND_PLACE)) {
1259 (void)new_player_spot(creature_ptr);
1262 /* You see stairs blocked */
1263 else if ((creature_ptr->change_floor_mode & CFM_NO_RETURN) && (creature_ptr->change_floor_mode & (CFM_DOWN | CFM_UP))) {
1264 if (!creature_ptr->blind) {
1265 msg_print(_("突然階段が塞がれてしまった。", "Suddenly the stairs is blocked!"));
1267 msg_print(_("ゴトゴトと何か音がした。", "You hear some noises."));
1274 * The "turn" is not always different number because
1275 * the level teleport doesn't take any turn. Use
1276 * visit mark instead of last visit turn to find the
1277 * oldest saved floor.
1279 sf_ptr->visit_mark = latest_visit_mark++;
1282 /* Place preserved pet monsters */
1283 place_pet(creature_ptr);
1285 /* Reset travel target place */
1286 forget_travel_flow(creature_ptr->current_floor_ptr);
1288 /* Hack -- maintain unique and artifacts */
1289 update_unique_artifact(creature_ptr->current_floor_ptr, new_floor_id);
1291 /* Now the player is in new floor */
1292 creature_ptr->floor_id = new_floor_id;
1294 /* The dungeon is ready */
1295 current_world_ptr->character_dungeon = TRUE;
1297 /* Hack -- Munchkin characters always get whole map */
1298 if (creature_ptr->pseikaku == PERSONALITY_MUNCHKIN)
1299 wiz_lite(creature_ptr, (bool)(creature_ptr->pclass == CLASS_NINJA));
1301 /* Remember when this level was "created" */
1302 creature_ptr->current_floor_ptr->generated_turn = current_world_ptr->game_turn;
1304 /* No dungeon feeling yet */
1305 creature_ptr->feeling_turn = creature_ptr->current_floor_ptr->generated_turn;
1306 creature_ptr->feeling = 0;
1308 /* Clear all flags */
1309 creature_ptr->change_floor_mode = 0L;
1311 select_floor_music(creature_ptr);
1312 creature_ptr->change_floor_mode = 0;