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"
48 static FLOOR_IDX new_floor_id; /*!<次のフロアのID / floor_id of the destination */
49 static u32b latest_visit_mark; /*!<フロアを渡った回数?(確認中) / Max number of visit_mark */
50 #define MAX_PARTY_MON 21 /*!< フロア移動時に先のフロアに連れて行けるペットの最大数 Maximum number of preservable pets */
51 static monster_type party_mon[MAX_PARTY_MON]; /*!< フロア移動に保存するペットモンスターの配列 */
54 * Number of floor_id used from birth
56 FLOOR_IDX max_floor_id;
59 * Sign for current process used in temporary files.
60 * Actually it is the start time of current process.
62 u32b saved_floor_file_sign;
65 * @brief 保存フロア配列を初期化する / Initialize saved_floors array.
66 * @param creature_ptr プレーヤーへの参照ポインタ
67 * @param force テンポラリファイルが残っていた場合も警告なしで強制的に削除するフラグ
68 * @details Make sure that old temporary files are not remaining as gurbages.
71 void init_saved_floors(player_type *creature_ptr, bool force)
73 char floor_savefile[1024];
76 BIT_FLAGS mode = 0644;
80 /* Get "games" permissions */
85 for (i = 0; i < MAX_SAVED_FLOORS; i++)
87 saved_floor_type *sf_ptr = &saved_floors[i];
90 sprintf(floor_savefile, "%s.F%02d", savefile, i);
92 /* Grab permissions */
95 /* Try to create the file */
96 fd = fd_make(floor_savefile, mode);
98 /* Drop permissions */
106 msg_print(_("エラー:古いテンポラリ・ファイルが残っています。", "Error: There are old temporary files."));
107 msg_print(_("変愚蛮怒を二重に起動していないか確認してください。", "Make sure you are not running two game processes simultaneously."));
108 msg_print(_("過去に変愚蛮怒がクラッシュした場合は一時ファイルを", "If the temporary files are garbages of old crashed process, "));
109 msg_print(_("強制的に削除して実行を続けられます。", "you can delete it safely."));
110 if (!get_check(_("強制的に削除してもよろしいですか?", "Do you delete old temporary files? ")))
111 quit(_("実行中止", "Aborted."));
121 /* Grab permissions */
124 /* Simply kill the temporary file */
125 (void)fd_kill(floor_savefile);
127 /* Drop permissions */
130 sf_ptr->floor_id = 0;
133 /* No floor_id used yet (No.0 is reserved to indicate non existance) */
136 /* vist_mark is from 1 */
137 latest_visit_mark = 1;
139 /* A sign to mark temporary files */
140 saved_floor_file_sign = (u32b)time(NULL);
142 /* No next floor yet */
145 /* No change floor mode yet */
146 creature_ptr->change_floor_mode = 0;
150 /* Drop "games" permissions */
157 * @brief 保存フロア用テンポラリファイルを削除する / Kill temporary files
158 * @details Should be called just before the game quit.
159 * @param creature_ptr プレーヤーへの参照ポインタ
162 void clear_saved_floor_files(player_type *creature_ptr)
164 char floor_savefile[1024];
169 /* Get "games" permissions */
174 for (i = 0; i < MAX_SAVED_FLOORS; i++)
176 saved_floor_type *sf_ptr = &saved_floors[i];
178 /* No temporary file */
179 if (!sf_ptr->floor_id) continue;
180 if (sf_ptr->floor_id == creature_ptr->floor_id) continue;
183 sprintf(floor_savefile, "%s.F%02d", savefile, i);
185 /* Grab permissions */
188 /* Simply kill the temporary file */
189 (void)fd_kill(floor_savefile);
191 /* Drop permissions */
197 /* Drop "games" permissions */
204 * @brief 保存フロアIDから参照ポインタを得る / Get a pointer for an item of the saved_floors array.
205 * @param floor_id 保存フロアID
206 * @return IDに対応する保存フロアのポインタ、ない場合はNULLを返す。
208 saved_floor_type *get_sf_ptr(FLOOR_IDX floor_id)
212 /* floor_id No.0 indicates no floor */
213 if (!floor_id) return NULL;
215 for (i = 0; i < MAX_SAVED_FLOORS; i++)
217 saved_floor_type *sf_ptr = &saved_floors[i];
219 if (sf_ptr->floor_id == floor_id) return sf_ptr;
228 * @brief 参照ポインタ先の保存フロアを抹消する / kill a saved floor and get an empty space
229 * @param creature_ptr プレーヤーへの参照ポインタ
230 * @param sf_ptr 保存フロアの参照ポインタ
233 static void kill_saved_floor(player_type *creature_ptr, saved_floor_type *sf_ptr)
235 char floor_savefile[1024];
239 if (!sf_ptr->floor_id) return;
241 if (sf_ptr->floor_id == creature_ptr->floor_id)
243 /* Kill current floor */
244 creature_ptr->floor_id = 0;
246 /* Current floor doesn't have temporary file */
251 sprintf(floor_savefile, "%s.F%02d", savefile, (int)sf_ptr->savefile_id);
253 /* Grab permissions */
256 /* Simply kill the temporary file */
257 (void)fd_kill(floor_savefile);
259 /* Drop permissions */
263 /* No longer exists */
264 sf_ptr->floor_id = 0;
269 * @brief 新規に利用可能な保存フロアを返す / Initialize new saved floor and get its floor id.
270 * @param creature_ptr プレーヤーへの参照ポインタ
271 * @return 利用可能な保存フロアID
273 * If number of saved floors are already MAX_SAVED_FLOORS, kill the oldest one.
275 FLOOR_IDX get_new_floor_id(player_type *creature_ptr)
277 saved_floor_type *sf_ptr = NULL;
280 /* Look for empty space */
281 for (i = 0; i < MAX_SAVED_FLOORS; i++)
283 sf_ptr = &saved_floors[i];
285 if (!sf_ptr->floor_id) break;
289 if (i == MAX_SAVED_FLOORS)
292 u32b oldest_visit = 0xffffffffL;
294 /* Search for oldest */
295 for (i = 0; i < MAX_SAVED_FLOORS; i++)
297 sf_ptr = &saved_floors[i];
299 /* Don't kill current floor */
300 if (sf_ptr->floor_id == creature_ptr->floor_id) continue;
302 /* Don't kill newer */
303 if (sf_ptr->visit_mark > oldest_visit) continue;
306 oldest_visit = sf_ptr->visit_mark;
309 /* Kill oldest saved floor */
310 sf_ptr = &saved_floors[oldest];
311 kill_saved_floor(creature_ptr, sf_ptr);
317 /* Prepare new floor data */
318 sf_ptr->savefile_id = i;
319 sf_ptr->floor_id = max_floor_id;
320 sf_ptr->last_visit = 0;
321 sf_ptr->upper_floor_id = 0;
322 sf_ptr->lower_floor_id = 0;
323 sf_ptr->visit_mark = latest_visit_mark++;
325 /* sf_ptr->dun_level may be changed later */
326 sf_ptr->dun_level = creature_ptr->current_floor_ptr->dun_level;
329 /* Increment number of floor_id */
330 if (max_floor_id < MAX_SHORT) max_floor_id++;
332 /* 32767 floor_ids are all used up! Re-use ancient IDs */
333 else max_floor_id = 1;
335 return sf_ptr->floor_id;
340 * @brief フロア切り替え時の処理フラグを追加する / Prepare mode flags of changing floor
341 * @param creature_ptr プレーヤーへの参照ポインタ
342 * @param mode 追加したい所持フラグ
345 void prepare_change_floor_mode(player_type *creature_ptr, BIT_FLAGS mode)
347 creature_ptr->change_floor_mode |= mode;
351 * @brief 階段移動先のフロアが生成できない時に簡単な行き止まりマップを作成する / Builds the dead end
354 static void build_dead_end(player_type *creature_ptr)
358 clear_cave(creature_ptr);
360 /* Mega-Hack -- no player yet */
361 creature_ptr->x = creature_ptr->y = 0;
363 /* Fill the arrays of floors and walls in the good proportions */
364 set_floor_and_wall(0);
367 creature_ptr->current_floor_ptr->height = SCREEN_HGT;
368 creature_ptr->current_floor_ptr->width = SCREEN_WID;
370 /* Filled with permanent walls */
371 for (y = 0; y < MAX_HGT; y++)
373 for (x = 0; x < MAX_WID; x++)
375 /* Create "solid" perma-wall */
376 place_solid_perm_bold(creature_ptr->current_floor_ptr, y, x);
380 /* Place at center of the floor */
381 creature_ptr->y = creature_ptr->current_floor_ptr->height / 2;
382 creature_ptr->x = creature_ptr->current_floor_ptr->width / 2;
384 /* Give one square */
385 place_floor_bold(creature_ptr->current_floor_ptr, creature_ptr->y, creature_ptr->x);
387 wipe_generate_random_floor_flags(creature_ptr->current_floor_ptr);
392 * @brief フロア移動時のペット保存処理 / Preserve_pets
393 * @param master_ptr プレーヤーへの参照ポインタ
396 static void preserve_pet(player_type *master_ptr)
401 for (num = 0; num < MAX_PARTY_MON; num++)
403 party_mon[num].r_idx = 0;
406 if (master_ptr->riding)
408 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[master_ptr->riding];
410 /* Pet of other pet don't follow. */
411 if (m_ptr->parent_m_idx)
413 master_ptr->riding = 0;
414 master_ptr->pet_extra_flags &= ~(PF_TWO_HANDS);
415 master_ptr->riding_ryoute = master_ptr->old_riding_ryoute = FALSE;
419 /* Preserve the mount */
420 (void)COPY(&party_mon[0], m_ptr, monster_type);
422 /* Delete from this floor */
423 delete_monster_idx(master_ptr->riding);
428 * If player is in wild mode, no pets are preserved
429 * except a monster whom player riding
431 if (!master_ptr->wild_mode && !master_ptr->current_floor_ptr->inside_arena && !master_ptr->phase_out)
433 for (i = master_ptr->current_floor_ptr->m_max - 1, num = 1; (i >= 1 && num < MAX_PARTY_MON); i--)
435 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[i];
437 if (!monster_is_valid(m_ptr)) continue;
438 if (!is_pet(m_ptr)) continue;
439 if (i == master_ptr->riding) continue;
441 if (reinit_wilderness)
443 /* Don't lose sight of pets when getting a Quest */
447 POSITION dis = distance(master_ptr->y, master_ptr->x, m_ptr->fy, m_ptr->fx);
449 /* Confused (etc.) monsters don't follow. */
450 if (MON_CONFUSED(m_ptr) || MON_STUNNED(m_ptr) || MON_CSLEEP(m_ptr)) continue;
452 /* Pet of other pet don't follow. */
453 if (m_ptr->parent_m_idx) continue;
456 * Pets with nickname will follow even from 3 blocks away
457 * when you or the pet can see the other.
459 if (m_ptr->nickname &&
460 ((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)) ||
461 (los(master_ptr, m_ptr->fy, m_ptr->fx, master_ptr->y, master_ptr->x) && projectable(master_ptr, m_ptr->fy, m_ptr->fx, master_ptr->y, master_ptr->x))))
463 if (dis > 3) continue;
467 if (dis > 1) continue;
471 (void)COPY(&party_mon[num], &master_ptr->current_floor_ptr->m_list[i], monster_type);
475 /* Delete from this floor */
476 delete_monster_idx(i);
480 if (record_named_pet)
482 for (i = master_ptr->current_floor_ptr->m_max - 1; i >=1; i--)
484 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[i];
485 GAME_TEXT m_name[MAX_NLEN];
487 if (!monster_is_valid(m_ptr)) continue;
488 if (!is_pet(m_ptr)) continue;
489 if (!m_ptr->nickname) continue;
490 if (master_ptr->riding == i) continue;
492 monster_desc(m_name, m_ptr, MD_ASSUME_VISIBLE | MD_INDEF_VISIBLE);
493 exe_write_diary(master_ptr, DIARY_NAMED_PET, RECORD_NAMED_PET_MOVED, m_name);
498 /* Pet of other pet may disappear. */
499 for (i = master_ptr->current_floor_ptr->m_max - 1; i >=1; i--)
501 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[i];
503 /* Are there its parent? */
504 if (m_ptr->parent_m_idx && !master_ptr->current_floor_ptr->m_list[m_ptr->parent_m_idx].r_idx)
506 /* Its parent have gone, it also goes away. */
510 GAME_TEXT m_name[MAX_NLEN];
511 monster_desc(m_name, m_ptr, 0);
512 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
515 delete_monster_idx(i);
522 * @brief フロア移動時にペットを伴った場合の準備処理 / Pre-calculate the racial counters of preserved pets
523 * @param master_ptr プレーヤーへの参照ポインタ
526 * To prevent multiple generation of unique monster who is the minion of player
528 void precalc_cur_num_of_pet(player_type *player_ptr)
532 int max_num = player_ptr->wild_mode ? 1 : MAX_PARTY_MON;
534 for (i = 0; i < max_num; i++)
536 m_ptr = &party_mon[i];
538 /* Skip empty monsters */
539 if (!monster_is_valid(m_ptr)) continue;
541 /* Hack -- Increase the racial counter */
542 real_r_ptr(m_ptr)->cur_num++;
547 * @brief 移動先のフロアに伴ったペットを配置する / Place preserved pet monsters on new floor
548 * @param master_ptr プレーヤーへの参照ポインタ
551 static void place_pet(player_type *master_ptr)
554 int max_num = master_ptr->wild_mode ? 1 : MAX_PARTY_MON;
556 for (i = 0; i < max_num; i++)
558 POSITION cy = 0, cx = 0;
561 if (!(party_mon[i].r_idx)) continue;
566 master_ptr->riding = m_idx;
578 for (d = 1; d < A_MAX; d++)
580 for (j = 1000; j > 0; j--)
582 scatter(master_ptr, &cy, &cx, master_ptr->y, master_ptr->x, d, 0);
583 if (monster_can_enter(cy, cx, &r_info[party_mon[i].r_idx], 0)) break;
587 m_idx = (d == 6) ? 0 : m_pop();
592 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[m_idx];
595 master_ptr->current_floor_ptr->grid_array[cy][cx].m_idx = m_idx;
597 m_ptr->r_idx = party_mon[i].r_idx;
599 /* Copy all member of the structure */
600 *m_ptr = party_mon[i];
601 r_ptr = real_r_ptr(m_ptr);
605 m_ptr->current_floor_ptr = master_ptr->current_floor_ptr;
607 m_ptr->mtimed[MTIMED_CSLEEP] = 0;
608 m_ptr->hold_o_idx = 0;
611 if ((r_ptr->flags1 & RF1_FORCE_SLEEP) && !ironman_nightmare)
613 /* Monster is still being nice */
614 m_ptr->mflag |= (MFLAG_NICE);
616 /* Must repair monsters */
617 repair_monsters = TRUE;
619 update_monster(master_ptr, m_idx, TRUE);
622 /* Pre-calculated in precalc_cur_num_of_pet() */
623 /* r_ptr->cur_num++; */
625 /* Hack -- Count the number of "reproducers" */
626 if (r_ptr->flags2 & RF2_MULTIPLY) master_ptr->current_floor_ptr->num_repro++;
631 monster_type *m_ptr = &party_mon[i];
632 monster_race *r_ptr = real_r_ptr(m_ptr);
633 GAME_TEXT m_name[MAX_NLEN];
635 monster_desc(m_name, m_ptr, 0);
636 msg_format(_("%sとはぐれてしまった。", "You have lost sight of %s."), m_name);
637 if (record_named_pet && m_ptr->nickname)
639 monster_desc(m_name, m_ptr, MD_INDEF_VISIBLE);
640 exe_write_diary(master_ptr, DIARY_NAMED_PET, RECORD_NAMED_PET_LOST_SIGHT, m_name);
643 /* Pre-calculated in precalc_cur_num_of_pet(), but need to decrease */
644 if (r_ptr->cur_num) r_ptr->cur_num--;
648 /* For accuracy of precalc_cur_num_of_pet() */
649 (void)C_WIPE(party_mon, MAX_PARTY_MON, monster_type);
654 * @brief ユニークモンスターやアーティファクトの所在フロアを更新する / Hack -- Update location of unique monsters and artifacts
655 * @param cur_floor_id 現在のフロアID
658 * The r_ptr->floor_id and a_ptr->floor_id are not updated correctly\n
659 * while new floor creation since dungeons may be re-created by\n
660 * auto-scum option.\n
662 static void update_unique_artifact(floor_type *floor_ptr, s16b cur_floor_id)
666 /* Maintain unique monsters */
667 for (i = 1; i < floor_ptr->m_max; i++)
670 monster_type *m_ptr = &floor_ptr->m_list[i];
672 if (!monster_is_valid(m_ptr)) continue;
674 /* Extract real monster race */
675 r_ptr = real_r_ptr(m_ptr);
677 /* Memorize location of the unique monster */
678 if ((r_ptr->flags1 & RF1_UNIQUE) ||
679 (r_ptr->flags7 & RF7_NAZGUL))
681 r_ptr->floor_id = cur_floor_id;
685 /* Maintain artifatcs */
686 for (i = 1; i < floor_ptr->o_max; i++)
688 object_type *o_ptr = &floor_ptr->o_list[i];
690 if (!OBJECT_IS_VALID(o_ptr)) continue;
692 /* Memorize location of the artifact */
693 if (object_is_fixed_artifact(o_ptr))
695 a_info[o_ptr->name1].floor_id = cur_floor_id;
702 * @brief フロア移動時、プレイヤーの移動先モンスターが既にいた場合ランダムな近隣に移動させる / When a monster is at a place where player will return,
705 static void get_out_monster(floor_type *floor_ptr, player_type *protected_ptr)
709 POSITION oy = protected_ptr->y;
710 POSITION ox = protected_ptr->x;
711 MONSTER_IDX m_idx = floor_ptr->grid_array[oy][ox].m_idx;
713 /* Nothing to do if no monster */
716 /* Look until done */
721 /* Pick a (possibly illegal) location */
722 POSITION ny = rand_spread(oy, dis);
723 POSITION nx = rand_spread(ox, dis);
727 /* Stop after 1000 tries */
728 if (tries > 10000) return;
731 * Increase distance after doing enough tries
732 * compared to area of possible space
734 if (tries > 20 * dis * dis) dis++;
736 /* Ignore illegal locations */
737 if (!in_bounds(floor_ptr, ny, nx)) continue;
739 /* Require "empty" floor space */
740 if (!cave_empty_bold(protected_ptr->current_floor_ptr, ny, nx)) continue;
742 /* Hack -- no teleport onto glyph of warding */
743 if (is_glyph_grid(&floor_ptr->grid_array[ny][nx])) continue;
744 if (is_explosive_rune_grid(&floor_ptr->grid_array[ny][nx])) continue;
746 /* ...nor onto the Pattern */
747 if (pattern_tile(ny, nx)) continue;
749 /*** It's a good place ***/
751 m_ptr = &floor_ptr->m_list[m_idx];
753 /* Update the old location */
754 floor_ptr->grid_array[oy][ox].m_idx = 0;
756 /* Update the new location */
757 floor_ptr->grid_array[ny][nx].m_idx = m_idx;
759 /* Move the monster */
763 /* No need to do update_monster() */
771 * @brief 新フロアに移動元フロアに繋がる階段を配置する / Virtually teleport onto the stairs that is connecting between two floors.
772 * @param sf_ptr 移動元の保存フロア構造体参照ポインタ
775 static void locate_connected_stairs(player_type *creature_ptr, floor_type *floor_ptr, saved_floor_type *sf_ptr, BIT_FLAGS floor_mode)
777 POSITION x, y, sx = 0, sy = 0;
778 POSITION x_table[20];
779 POSITION y_table[20];
783 /* Search usable stairs */
784 for (y = 0; y < floor_ptr->height; y++)
786 for (x = 0; x < floor_ptr->width; x++)
788 grid_type *g_ptr = &floor_ptr->grid_array[y][x];
789 feature_type *f_ptr = &f_info[g_ptr->feat];
792 if (floor_mode & CFM_UP)
794 if (have_flag(f_ptr->flags, FF_LESS) && have_flag(f_ptr->flags, FF_STAIRS) &&
795 !have_flag(f_ptr->flags, FF_SPECIAL))
799 /* Found fixed stairs? */
800 if (g_ptr->special &&
801 g_ptr->special == sf_ptr->upper_floor_id)
809 else if (floor_mode & CFM_DOWN)
811 if (have_flag(f_ptr->flags, FF_MORE) && have_flag(f_ptr->flags, FF_STAIRS) &&
812 !have_flag(f_ptr->flags, FF_SPECIAL))
816 /* Found fixed stairs */
817 if (g_ptr->special &&
818 g_ptr->special == sf_ptr->lower_floor_id)
828 if (have_flag(f_ptr->flags, FF_BLDG))
834 if (ok && (num < 20))
846 creature_ptr->y = sy;
847 creature_ptr->x = sx;
851 /* No stairs found! -- No return */
852 prepare_change_floor_mode(creature_ptr, CFM_RAND_PLACE | CFM_NO_RETURN);
854 /* Mega Hack -- It's not the stairs you enter. Disable it. */
855 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;
859 /* Choose random one */
862 /* Point stair location */
863 creature_ptr->y = y_table[i];
864 creature_ptr->x = x_table[i];
869 * @brief 現在のフロアを離れるに伴って行なわれる保存処理
870 * / Maintain quest monsters, mark next floor_id at stairs, save current floor, and prepare to enter next floor.
871 * @param creature_ptr プレーヤーへの参照ポインタ
874 void leave_floor(player_type *creature_ptr)
876 grid_type *g_ptr = NULL;
878 saved_floor_type *sf_ptr;
879 MONRACE_IDX quest_r_idx = 0;
881 FLOOR_IDX tmp_floor_idx = 0;
883 /* Preserve pets and prepare to take these to next floor */
884 preserve_pet(creature_ptr);
886 /* Remove all mirrors without explosion */
887 remove_all_mirrors(creature_ptr, FALSE);
889 if (creature_ptr->special_defense & NINJA_S_STEALTH) set_superstealth(creature_ptr, FALSE);
891 /* New floor is not yet prepared */
894 /* Temporary get a floor_id (for Arena) */
895 if (!creature_ptr->floor_id &&
896 (creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) &&
897 !(creature_ptr->change_floor_mode & CFM_NO_RETURN))
899 /* Get temporary floor_id */
900 tmp_floor_idx = get_new_floor_id(creature_ptr);
903 /* Search the quest monster index */
904 for (i = 0; i < max_q_idx; i++)
906 if ((quest[i].status == QUEST_STATUS_TAKEN) &&
907 ((quest[i].type == QUEST_TYPE_KILL_LEVEL) ||
908 (quest[i].type == QUEST_TYPE_RANDOM)) &&
909 (quest[i].level == creature_ptr->current_floor_ptr->dun_level) &&
910 (creature_ptr->dungeon_idx == quest[i].dungeon) &&
911 !(quest[i].flags & QUEST_FLAG_PRESET))
913 quest_r_idx = quest[i].r_idx;
917 /* Maintain quest monsters */
918 for (i = 1; i < creature_ptr->current_floor_ptr->m_max; i++)
921 monster_type *m_ptr = &creature_ptr->current_floor_ptr->m_list[i];
923 if (!monster_is_valid(m_ptr)) continue;
925 /* Only maintain quest monsters */
926 if (quest_r_idx != m_ptr->r_idx) continue;
928 /* Extract real monster race */
929 r_ptr = real_r_ptr(m_ptr);
931 /* Ignore unique monsters */
932 if ((r_ptr->flags1 & RF1_UNIQUE) ||
933 (r_ptr->flags7 & RF7_NAZGUL)) continue;
935 /* Delete non-unique quest monsters */
936 delete_monster_idx(i);
939 /* Check if there is a same item */
940 for (i = 0; i < INVEN_PACK; i++)
942 object_type *o_ptr = &creature_ptr->inventory_list[i];
944 if (!OBJECT_IS_VALID(o_ptr)) continue;
946 /* Delete old memorized location of the artifact */
947 if (object_is_fixed_artifact(o_ptr))
949 a_info[o_ptr->name1].floor_id = 0;
953 /* Extract current floor info or NULL */
954 sf_ptr = get_sf_ptr(tmp_floor_idx);
956 /* Choose random stairs */
957 if ((creature_ptr->change_floor_mode & CFM_RAND_CONNECT) && tmp_floor_idx)
959 locate_connected_stairs(creature_ptr, creature_ptr->current_floor_ptr, sf_ptr, creature_ptr->change_floor_mode);
962 /* Extract new dungeon level */
963 if (creature_ptr->change_floor_mode & CFM_SAVE_FLOORS)
965 /* Extract stair position */
966 g_ptr = &creature_ptr->current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
967 f_ptr = &f_info[g_ptr->feat];
969 /* Get back to old saved floor? */
970 if (g_ptr->special && !have_flag(f_ptr->flags, FF_SPECIAL) && get_sf_ptr(g_ptr->special))
972 /* Saved floor is exist. Use it. */
973 new_floor_id = g_ptr->special;
976 /* Mark shaft up/down */
977 if (have_flag(f_ptr->flags, FF_STAIRS) && have_flag(f_ptr->flags, FF_SHAFT))
979 prepare_change_floor_mode(creature_ptr, CFM_SHAFT);
983 /* Climb up/down some sort of stairs */
984 if (creature_ptr->change_floor_mode & (CFM_DOWN | CFM_UP))
988 /* Extract level movement number */
989 if (creature_ptr->change_floor_mode & CFM_DOWN) move_num = 1;
990 else if (creature_ptr->change_floor_mode & CFM_UP) move_num = -1;
992 /* Shafts are deeper than normal stairs */
993 if (creature_ptr->change_floor_mode & CFM_SHAFT)
994 move_num += SGN(move_num);
996 /* Get out from or Enter the dungeon */
997 if (creature_ptr->change_floor_mode & CFM_DOWN)
999 if (!creature_ptr->current_floor_ptr->dun_level)
1000 move_num = d_info[creature_ptr->dungeon_idx].mindepth;
1002 else if (creature_ptr->change_floor_mode & CFM_UP)
1004 if (creature_ptr->current_floor_ptr->dun_level + move_num < d_info[creature_ptr->dungeon_idx].mindepth)
1005 move_num = -creature_ptr->current_floor_ptr->dun_level;
1008 creature_ptr->current_floor_ptr->dun_level += move_num;
1011 /* Leaving the dungeon to town */
1012 if (!creature_ptr->current_floor_ptr->dun_level && creature_ptr->dungeon_idx)
1014 creature_ptr->leaving_dungeon = TRUE;
1015 if (!vanilla_town && !lite_town)
1017 creature_ptr->wilderness_y = d_info[creature_ptr->dungeon_idx].dy;
1018 creature_ptr->wilderness_x = d_info[creature_ptr->dungeon_idx].dx;
1020 creature_ptr->recall_dungeon = creature_ptr->dungeon_idx;
1021 creature_ptr->dungeon_idx = 0;
1023 /* Reach to the surface -- Clear all saved floors */
1024 creature_ptr->change_floor_mode &= ~CFM_SAVE_FLOORS; // TODO
1027 /* Kill some old saved floors */
1028 if (!(creature_ptr->change_floor_mode & CFM_SAVE_FLOORS))
1030 /* Kill all saved floors */
1031 for (i = 0; i < MAX_SAVED_FLOORS; i++)
1032 kill_saved_floor(creature_ptr, &saved_floors[i]);
1034 /* Reset visit_mark count */
1035 latest_visit_mark = 1;
1037 else if (creature_ptr->change_floor_mode & CFM_NO_RETURN)
1039 /* Kill current floor */
1040 kill_saved_floor(creature_ptr, sf_ptr);
1043 /* No current floor -- Left/Enter dungeon etc... */
1046 /* No longer need to save current floor */
1050 /* Mark next floor_id on the previous floor */
1054 new_floor_id = get_new_floor_id(creature_ptr);
1056 /* Connect from here */
1057 if (g_ptr && !feat_uses_special(g_ptr->feat))
1059 g_ptr->special = tmp_floor_idx;
1063 /* Fix connection -- level teleportation or trap door */
1064 if (creature_ptr->change_floor_mode & CFM_RAND_CONNECT)
1066 if (creature_ptr->change_floor_mode & CFM_UP)
1067 sf_ptr->upper_floor_id = new_floor_id;
1068 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1069 sf_ptr->lower_floor_id = new_floor_id;
1072 /* If you can return, you need to save previous floor */
1073 if ((creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) &&
1074 !(creature_ptr->change_floor_mode & CFM_NO_RETURN))
1076 /* Get out of the my way! */
1077 get_out_monster(creature_ptr->current_floor_ptr, creature_ptr);
1079 /* Record the last visit turn of current floor */
1080 sf_ptr->last_visit = current_world_ptr->game_turn;
1082 forget_lite(creature_ptr->current_floor_ptr);
1083 forget_view(creature_ptr->current_floor_ptr);
1084 clear_mon_lite(creature_ptr->current_floor_ptr);
1086 /* Save current floor */
1087 if (!save_floor(creature_ptr, sf_ptr, 0))
1089 /* Save failed -- No return */
1090 prepare_change_floor_mode(creature_ptr, CFM_NO_RETURN);
1092 /* Kill current floor */
1093 kill_saved_floor(creature_ptr, get_sf_ptr(creature_ptr->floor_id));
1100 * @brief フロアの切り替え処理 / Enter new floor.
1101 * @param creature_ptr プレーヤーへの参照ポインタ
1104 * If the floor is an old saved floor, it will be\n
1105 * restored from the temporary file. If the floor is new one, new floor\n
1106 * will be generated.\n
1108 void change_floor(player_type *creature_ptr)
1110 saved_floor_type *sf_ptr;
1111 bool loaded = FALSE;
1113 /* The dungeon is not ready */
1114 current_world_ptr->character_dungeon = FALSE;
1116 /* No longer in the trap detecteded region */
1117 creature_ptr->dtrap = FALSE;
1119 /* Mega-Hack -- no panel yet */
1125 /* Mega-Hack -- not ambushed on the wildness? */
1126 creature_ptr->ambush_flag = FALSE;
1128 /* No saved floors (On the surface etc.) */
1129 if (!(creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) &&
1130 !(creature_ptr->change_floor_mode & CFM_FIRST_FLOOR))
1132 generate_floor(creature_ptr);
1134 /* Paranoia -- No new saved floor */
1138 /* In the dungeon */
1141 /* No floor_id yet */
1145 new_floor_id = get_new_floor_id(creature_ptr);
1148 /* Pointer for infomations of new floor */
1149 sf_ptr = get_sf_ptr(new_floor_id);
1151 /* Try to restore old floor */
1152 if (sf_ptr->last_visit)
1154 /* Old saved floor is exist */
1155 if (load_floor(creature_ptr, sf_ptr, 0))
1159 /* Forbid return stairs */
1160 if (creature_ptr->change_floor_mode & CFM_NO_RETURN)
1162 grid_type *g_ptr = &creature_ptr->current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
1164 if (!feat_uses_special(g_ptr->feat))
1166 if (creature_ptr->change_floor_mode & (CFM_DOWN | CFM_UP))
1168 /* Reset to floor */
1169 g_ptr->feat = feat_ground_type[randint0(100)];
1179 * Set lower/upper_floor_id of new floor when the new
1180 * floor is right-above/right-under the current floor.
1182 * Stair creation/Teleport level/Trap door will take
1183 * you the same floor when you used it later again.
1185 if (creature_ptr->floor_id)
1187 saved_floor_type *cur_sf_ptr = get_sf_ptr(creature_ptr->floor_id);
1189 if (creature_ptr->change_floor_mode & CFM_UP)
1191 /* New floor is right-above */
1192 if (cur_sf_ptr->upper_floor_id == new_floor_id)
1193 sf_ptr->lower_floor_id = creature_ptr->floor_id;
1195 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1197 /* New floor is right-under */
1198 if (cur_sf_ptr->lower_floor_id == new_floor_id)
1199 sf_ptr->upper_floor_id = creature_ptr->floor_id;
1203 /* Break connection to killed floor */
1206 if (creature_ptr->change_floor_mode & CFM_UP)
1207 sf_ptr->lower_floor_id = 0;
1208 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1209 sf_ptr->upper_floor_id = 0;
1212 /* Maintain monsters and artifacts */
1216 GAME_TURN tmp_last_visit = sf_ptr->last_visit;
1217 GAME_TURN absence_ticks;
1218 int alloc_chance = d_info[creature_ptr->dungeon_idx].max_m_alloc_chance;
1219 GAME_TURN alloc_times;
1221 while (tmp_last_visit > current_world_ptr->game_turn) tmp_last_visit -= TURNS_PER_TICK * TOWN_DAWN;
1222 absence_ticks = (current_world_ptr->game_turn - tmp_last_visit) / TURNS_PER_TICK;
1224 /* Maintain monsters */
1225 for (i = 1; i < creature_ptr->current_floor_ptr->m_max; i++)
1227 monster_race *r_ptr;
1228 monster_type *m_ptr = &creature_ptr->current_floor_ptr->m_list[i];
1230 if (!monster_is_valid(m_ptr)) continue;
1235 m_ptr->hp = m_ptr->maxhp = m_ptr->max_maxhp;
1237 /* Remove timed status (except MTIMED_CSLEEP) */
1238 (void)set_monster_fast(creature_ptr, i, 0);
1239 (void)set_monster_slow(creature_ptr, i, 0);
1240 (void)set_monster_stunned(creature_ptr, i, 0);
1241 (void)set_monster_confused(creature_ptr, i, 0);
1242 (void)set_monster_monfear(creature_ptr, i, 0);
1243 (void)set_monster_invulner(creature_ptr, i, 0, FALSE);
1246 /* Extract real monster race */
1247 r_ptr = real_r_ptr(m_ptr);
1249 /* Ignore non-unique */
1250 if (!(r_ptr->flags1 & RF1_UNIQUE) &&
1251 !(r_ptr->flags7 & RF7_NAZGUL)) continue;
1253 /* Appear at a different floor? */
1254 if (r_ptr->floor_id != new_floor_id)
1256 /* Disapper from here */
1257 delete_monster_idx(i);
1261 /* Maintain artifatcs */
1262 for (i = 1; i < creature_ptr->current_floor_ptr->o_max; i++)
1264 object_type *o_ptr = &creature_ptr->current_floor_ptr->o_list[i];
1266 if (!OBJECT_IS_VALID(o_ptr)) continue;
1268 /* Ignore non-artifact */
1269 if (!object_is_fixed_artifact(o_ptr)) continue;
1271 /* Appear at a different floor? */
1272 if (a_info[o_ptr->name1].floor_id != new_floor_id)
1274 /* Disappear from here */
1275 delete_object_idx(creature_ptr->current_floor_ptr, i);
1279 /* Cancel preserve */
1280 a_info[o_ptr->name1].cur_num = 1;
1284 (void)place_quest_monsters(creature_ptr);
1286 /* Place some random monsters */
1287 alloc_times = absence_ticks / alloc_chance;
1289 if (randint0(alloc_chance) < (absence_ticks % alloc_chance))
1292 for (i = 0; i < alloc_times; i++)
1294 /* Make a (group of) new monster */
1295 (void)alloc_monster(0, 0);
1300 /* New floor_id or failed to restore */
1301 else /* if (!loaded) */
1303 if (sf_ptr->last_visit)
1305 /* Temporary file is broken? */
1306 msg_print(_("階段は行き止まりだった。", "The staircases come to a dead end..."));
1308 /* Create simple dead end */
1309 build_dead_end(creature_ptr);
1311 /* Break connection */
1312 if (creature_ptr->change_floor_mode & CFM_UP)
1314 sf_ptr->upper_floor_id = 0;
1316 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1318 sf_ptr->lower_floor_id = 0;
1323 generate_floor(creature_ptr);
1326 /* Record last visit turn */
1327 sf_ptr->last_visit = current_world_ptr->game_turn;
1329 /* Set correct dun_level value */
1330 sf_ptr->dun_level = creature_ptr->current_floor_ptr->dun_level;
1332 /* Create connected stairs */
1333 if (!(creature_ptr->change_floor_mode & CFM_NO_RETURN))
1335 /* Extract stair position */
1336 grid_type *g_ptr = &creature_ptr->current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
1338 /*** Create connected stairs ***/
1340 /* No stairs down from Quest */
1341 if ((creature_ptr->change_floor_mode & CFM_UP) && !quest_number(creature_ptr, creature_ptr->current_floor_ptr->dun_level))
1343 g_ptr->feat = (creature_ptr->change_floor_mode & CFM_SHAFT) ? feat_state(feat_down_stair, FF_SHAFT) : feat_down_stair;
1346 /* No stairs up when ironman_downward */
1347 else if ((creature_ptr->change_floor_mode & CFM_DOWN) && !ironman_downward)
1349 g_ptr->feat = (creature_ptr->change_floor_mode & CFM_SHAFT) ? feat_state(feat_up_stair, FF_SHAFT) : feat_up_stair;
1352 /* Paranoia -- Clear mimic */
1355 /* Connect to previous floor */
1356 g_ptr->special = creature_ptr->floor_id;
1360 /* Arrive at random grid */
1361 if (creature_ptr->change_floor_mode & (CFM_RAND_PLACE))
1363 (void)new_player_spot(creature_ptr);
1366 /* You see stairs blocked */
1367 else if ((creature_ptr->change_floor_mode & CFM_NO_RETURN) && (creature_ptr->change_floor_mode & (CFM_DOWN | CFM_UP)))
1369 if (!creature_ptr->blind)
1371 msg_print(_("突然階段が塞がれてしまった。", "Suddenly the stairs is blocked!"));
1375 msg_print(_("ゴトゴトと何か音がした。", "You hear some noises."));
1382 * The "turn" is not always different number because
1383 * the level teleport doesn't take any turn. Use
1384 * visit mark instead of last visit turn to find the
1385 * oldest saved floor.
1387 sf_ptr->visit_mark = latest_visit_mark++;
1390 /* Place preserved pet monsters */
1391 place_pet(creature_ptr);
1393 /* Reset travel target place */
1394 forget_travel_flow(creature_ptr->current_floor_ptr);
1396 /* Hack -- maintain unique and artifacts */
1397 update_unique_artifact(creature_ptr->current_floor_ptr, new_floor_id);
1399 /* Now the player is in new floor */
1400 creature_ptr->floor_id = new_floor_id;
1402 /* The dungeon is ready */
1403 current_world_ptr->character_dungeon = TRUE;
1405 /* Hack -- Munchkin characters always get whole map */
1406 if (creature_ptr->pseikaku == SEIKAKU_MUNCHKIN)
1407 wiz_lite(creature_ptr, (bool)(creature_ptr->pclass == CLASS_NINJA));
1409 /* Remember when this level was "created" */
1410 creature_ptr->current_floor_ptr->generated_turn = current_world_ptr->game_turn;
1412 /* No dungeon feeling yet */
1413 creature_ptr->feeling_turn = creature_ptr->current_floor_ptr->generated_turn;
1414 creature_ptr->feeling = 0;
1416 /* Clear all flags */
1417 creature_ptr->change_floor_mode = 0L;
1419 select_floor_music(creature_ptr);
1420 creature_ptr->change_floor_mode = 0;