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/cave.h"
20 #include "floor/floor-events.h"
21 #include "floor/floor-generate.h"
22 #include "floor/floor-object.h"
23 #include "floor/floor.h"
24 #include "floor/geometry.h"
25 #include "floor/wild.h"
26 #include "game-option/birth-options.h"
27 #include "game-option/play-record-options.h"
28 #include "grid/feature.h"
29 #include "grid/grid.h"
30 #include "inventory/inventory-slot-types.h"
31 #include "io/files-util.h"
32 #include "io/uid-checker.h"
33 #include "io/write-diary.h"
34 #include "load/floor-loader.h"
35 #include "main/sound-of-music.h"
36 #include "mind/mind-mirror-master.h"
37 #include "mind/mind-ninja.h"
38 #include "monster-floor/monster-generator.h"
39 #include "monster-floor/monster-remover.h"
40 #include "monster-floor/monster-summon.h"
41 #include "monster-race/monster-race.h"
42 #include "monster-race/race-flags1.h"
43 #include "monster-race/race-flags2.h"
44 #include "monster-race/race-flags7.h"
45 #include "monster/monster-describer.h"
46 #include "monster/monster-description-types.h"
47 #include "monster/monster-flag-types.h"
48 #include "monster/monster-info.h"
49 #include "monster/monster-list.h"
50 #include "monster/monster-status.h"
51 #include "monster/monster-update.h"
52 #include "monster/smart-learn-types.h"
53 #include "object-hook/hook-checker.h"
54 #include "object-hook/hook-enchant.h"
55 #include "pet/pet-util.h"
56 #include "player/player-class.h"
57 #include "player/player-personalities-types.h"
58 #include "player/special-defense-types.h"
59 #include "save/floor-writer.h"
60 #include "spell-kind/spells-floor.h"
61 #include "system/artifact-type-definition.h"
62 #include "system/floor-type-definition.h"
63 #include "system/system-variables.h"
64 #include "util/angband-files.h"
65 #include "util/bit-flags-calculator.h"
66 #include "view/display-messages.h"
67 #include "window/main-window-util.h"
68 #include "world/world.h"
72 static FLOOR_IDX new_floor_id; /*!<次のフロアのID / floor_id of the destination */
73 static u32b latest_visit_mark; /*!<フロアを渡った回数?(確認中) / Max number of visit_mark */
74 #define MAX_PARTY_MON 21 /*!< フロア移動時に先のフロアに連れて行けるペットの最大数 Maximum number of preservable pets */
75 static monster_type party_mon[MAX_PARTY_MON]; /*!< フロア移動に保存するペットモンスターの配列 */
78 * Number of floor_id used from birth
80 FLOOR_IDX max_floor_id;
83 * Sign for current process used in temporary files.
84 * Actually it is the start time of current process.
86 u32b saved_floor_file_sign;
88 saved_floor_type saved_floors[MAX_SAVED_FLOORS];
91 * @brief 保存フロア配列を初期化する / Initialize saved_floors array.
92 * @param creature_ptr プレーヤーへの参照ポインタ
93 * @param force テンポラリファイルが残っていた場合も警告なしで強制的に削除するフラグ
94 * @details Make sure that old temporary files are not remaining as gurbages.
97 void init_saved_floors(player_type *creature_ptr, bool force)
99 char floor_savefile[1024];
102 BIT_FLAGS mode = 0644;
103 for (i = 0; i < MAX_SAVED_FLOORS; i++) {
104 saved_floor_type *sf_ptr = &saved_floors[i];
107 sprintf(floor_savefile, "%s.F%02d", savefile, i);
109 /* Grab permissions */
110 safe_setuid_grab(creature_ptr);
112 /* Try to create the file */
113 fd = fd_make(floor_savefile, mode);
115 /* Drop permissions */
121 msg_print(_("エラー:古いテンポラリ・ファイルが残っています。", "Error: There are old temporary files."));
122 msg_print(_("変愚蛮怒を二重に起動していないか確認してください。", "Make sure you are not running two game processes simultaneously."));
123 msg_print(_("過去に変愚蛮怒がクラッシュした場合は一時ファイルを", "If the temporary files are garbage from an old crashed process, "));
124 msg_print(_("強制的に削除して実行を続けられます。", "you can delete them safely."));
125 if (!get_check(_("強制的に削除してもよろしいですか?", "Do you delete the old temporary files? ")))
126 quit(_("実行中止", "Aborted."));
134 /* Grab permissions */
135 safe_setuid_grab(creature_ptr);
137 /* Simply kill the temporary file */
138 (void)fd_kill(floor_savefile);
140 /* Drop permissions */
143 sf_ptr->floor_id = 0;
146 /* No floor_id used yet (No.0 is reserved to indicate non existance) */
149 /* vist_mark is from 1 */
150 latest_visit_mark = 1;
152 /* A sign to mark temporary files */
153 saved_floor_file_sign = (u32b)time(NULL);
155 /* No next floor yet */
158 /* No change floor mode yet */
159 creature_ptr->change_floor_mode = 0;
163 * @brief 保存フロア用テンポラリファイルを削除する / Kill temporary files
164 * @details Should be called just before the game quit.
165 * @param creature_ptr プレーヤーへの参照ポインタ
168 void clear_saved_floor_files(player_type *creature_ptr)
170 char floor_savefile[1024];
172 for (i = 0; i < MAX_SAVED_FLOORS; i++) {
173 saved_floor_type *sf_ptr = &saved_floors[i];
175 /* No temporary file */
176 if (!sf_ptr->floor_id)
178 if (sf_ptr->floor_id == creature_ptr->floor_id)
182 sprintf(floor_savefile, "%s.F%02d", savefile, i);
184 /* Grab permissions */
185 safe_setuid_grab(creature_ptr);
187 /* Simply kill the temporary file */
188 (void)fd_kill(floor_savefile);
190 /* Drop permissions */
196 * @brief 保存フロアIDから参照ポインタを得る / Get a pointer for an item of the saved_floors array.
197 * @param floor_id 保存フロアID
198 * @return IDに対応する保存フロアのポインタ、ない場合はNULLを返す。
200 saved_floor_type *get_sf_ptr(FLOOR_IDX floor_id)
204 /* floor_id No.0 indicates no floor */
208 for (i = 0; i < MAX_SAVED_FLOORS; i++) {
209 saved_floor_type *sf_ptr = &saved_floors[i];
211 if (sf_ptr->floor_id == floor_id)
220 * @brief 参照ポインタ先の保存フロアを抹消する / kill a saved floor and get an empty space
221 * @param creature_ptr プレーヤーへの参照ポインタ
222 * @param sf_ptr 保存フロアの参照ポインタ
225 static void kill_saved_floor(player_type *creature_ptr, saved_floor_type *sf_ptr)
227 char floor_savefile[1024];
232 if (!sf_ptr->floor_id)
235 if (sf_ptr->floor_id == creature_ptr->floor_id) {
236 /* Kill current floor */
237 creature_ptr->floor_id = 0;
239 /* Current floor doesn't have temporary file */
242 sprintf(floor_savefile, "%s.F%02d", savefile, (int)sf_ptr->savefile_id);
244 /* Grab permissions */
245 safe_setuid_grab(creature_ptr);
247 /* Simply kill the temporary file */
248 (void)fd_kill(floor_savefile);
250 /* Drop permissions */
254 /* No longer exists */
255 sf_ptr->floor_id = 0;
259 * @brief 新規に利用可能な保存フロアを返す / Initialize new saved floor and get its floor id.
260 * @param creature_ptr プレーヤーへの参照ポインタ
261 * @return 利用可能な保存フロアID
263 * If number of saved floors are already MAX_SAVED_FLOORS, kill the oldest one.
265 FLOOR_IDX get_new_floor_id(player_type *creature_ptr)
267 saved_floor_type *sf_ptr = NULL;
270 /* Look for empty space */
271 for (i = 0; i < MAX_SAVED_FLOORS; i++) {
272 sf_ptr = &saved_floors[i];
274 if (!sf_ptr->floor_id)
279 if (i == MAX_SAVED_FLOORS) {
281 u32b oldest_visit = 0xffffffffL;
283 /* Search for oldest */
284 for (i = 0; i < MAX_SAVED_FLOORS; i++) {
285 sf_ptr = &saved_floors[i];
287 /* Don't kill current floor */
288 if (sf_ptr->floor_id == creature_ptr->floor_id)
291 /* Don't kill newer */
292 if (sf_ptr->visit_mark > oldest_visit)
296 oldest_visit = sf_ptr->visit_mark;
299 /* Kill oldest saved floor */
300 sf_ptr = &saved_floors[oldest];
301 kill_saved_floor(creature_ptr, sf_ptr);
307 /* Prepare new floor data */
308 sf_ptr->savefile_id = i;
309 sf_ptr->floor_id = max_floor_id;
310 sf_ptr->last_visit = 0;
311 sf_ptr->upper_floor_id = 0;
312 sf_ptr->lower_floor_id = 0;
313 sf_ptr->visit_mark = latest_visit_mark++;
315 /* sf_ptr->dun_level may be changed later */
316 sf_ptr->dun_level = creature_ptr->current_floor_ptr->dun_level;
318 /* Increment number of floor_id */
319 if (max_floor_id < MAX_SHORT)
322 /* 32767 floor_ids are all used up! Re-use ancient IDs */
326 return sf_ptr->floor_id;
330 * @brief フロア切り替え時の処理フラグを追加する / Prepare mode flags of changing floor
331 * @param creature_ptr プレーヤーへの参照ポインタ
332 * @param mode 追加したい所持フラグ
335 void prepare_change_floor_mode(player_type *creature_ptr, BIT_FLAGS mode) { creature_ptr->change_floor_mode |= mode; }
338 * @brief 階段移動先のフロアが生成できない時に簡単な行き止まりマップを作成する / Builds the dead end
341 static void build_dead_end(player_type *creature_ptr)
345 clear_cave(creature_ptr);
347 /* Mega-Hack -- no player yet */
348 creature_ptr->x = creature_ptr->y = 0;
350 /* Fill the arrays of floors and walls in the good proportions */
351 set_floor_and_wall(0);
354 creature_ptr->current_floor_ptr->height = SCREEN_HGT;
355 creature_ptr->current_floor_ptr->width = SCREEN_WID;
357 /* Filled with permanent walls */
358 for (y = 0; y < MAX_HGT; y++) {
359 for (x = 0; x < MAX_WID; x++) {
360 /* Create "solid" perma-wall */
361 place_bold(creature_ptr, y, x, GB_SOLID_PERM);
365 /* Place at center of the floor */
366 creature_ptr->y = creature_ptr->current_floor_ptr->height / 2;
367 creature_ptr->x = creature_ptr->current_floor_ptr->width / 2;
369 /* Give one square */
370 place_bold(creature_ptr, creature_ptr->y, creature_ptr->x, GB_FLOOR);
372 wipe_generate_random_floor_flags(creature_ptr->current_floor_ptr);
376 * @brief フロア移動時のペット保存処理 / Preserve_pets
377 * @param master_ptr プレーヤーへの参照ポインタ
380 static void preserve_pet(player_type *master_ptr)
385 for (num = 0; num < MAX_PARTY_MON; num++) {
386 party_mon[num].r_idx = 0;
389 if (master_ptr->riding) {
390 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[master_ptr->riding];
392 /* Pet of other pet don't follow. */
393 if (m_ptr->parent_m_idx) {
394 master_ptr->riding = 0;
395 master_ptr->pet_extra_flags &= ~(PF_TWO_HANDS);
396 master_ptr->riding_ryoute = master_ptr->old_riding_ryoute = FALSE;
398 /* Preserve the mount */
399 (void)COPY(&party_mon[0], m_ptr, monster_type);
401 /* Delete from this floor */
402 delete_monster_idx(master_ptr, master_ptr->riding);
407 * If player is in wild mode, no pets are preserved
408 * except a monster whom player riding
410 if (!master_ptr->wild_mode && !master_ptr->current_floor_ptr->inside_arena && !master_ptr->phase_out) {
411 for (i = master_ptr->current_floor_ptr->m_max - 1, num = 1; (i >= 1 && num < MAX_PARTY_MON); i--) {
412 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[i];
414 if (!monster_is_valid(m_ptr))
418 if (i == master_ptr->riding)
421 if (reinit_wilderness) {
422 /* Don't lose sight of pets when getting a Quest */
424 POSITION dis = distance(master_ptr->y, master_ptr->x, m_ptr->fy, m_ptr->fx);
426 /* Confused (etc.) monsters don't follow. */
427 if (monster_confused_remaining(m_ptr) || monster_stunned_remaining(m_ptr) || monster_csleep_remaining(m_ptr))
430 /* Pet of other pet don't follow. */
431 if (m_ptr->parent_m_idx)
435 * Pets with nickname will follow even from 3 blocks away
436 * when you or the pet can see the other.
439 && ((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))
440 || (los(master_ptr, m_ptr->fy, m_ptr->fx, master_ptr->y, master_ptr->x)
441 && projectable(master_ptr, m_ptr->fy, m_ptr->fx, master_ptr->y, master_ptr->x)))) {
450 (void)COPY(&party_mon[num], &master_ptr->current_floor_ptr->m_list[i], monster_type);
454 /* Delete from this floor */
455 delete_monster_idx(master_ptr, i);
459 if (record_named_pet) {
460 for (i = master_ptr->current_floor_ptr->m_max - 1; i >= 1; i--) {
461 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[i];
462 GAME_TEXT m_name[MAX_NLEN];
464 if (!monster_is_valid(m_ptr))
468 if (!m_ptr->nickname)
470 if (master_ptr->riding == i)
473 monster_desc(master_ptr, m_name, m_ptr, MD_ASSUME_VISIBLE | MD_INDEF_VISIBLE);
474 exe_write_diary(master_ptr, DIARY_NAMED_PET, RECORD_NAMED_PET_MOVED, m_name);
478 /* Pet of other pet may disappear. */
479 for (i = master_ptr->current_floor_ptr->m_max - 1; i >= 1; i--) {
480 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[i];
482 /* Are there its parent? */
483 if (m_ptr->parent_m_idx && !master_ptr->current_floor_ptr->m_list[m_ptr->parent_m_idx].r_idx) {
484 /* Its parent have gone, it also goes away. */
486 if (is_seen(master_ptr, m_ptr)) {
487 GAME_TEXT m_name[MAX_NLEN];
488 monster_desc(master_ptr, m_name, m_ptr, 0);
489 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
492 delete_monster_idx(master_ptr, i);
498 * @brief フロア移動時にペットを伴った場合の準備処理 / Pre-calculate the racial counters of preserved pets
499 * @param master_ptr プレーヤーへの参照ポインタ
502 * To prevent multiple generation of unique monster who is the minion of player
504 void precalc_cur_num_of_pet(player_type *player_ptr)
508 int max_num = player_ptr->wild_mode ? 1 : MAX_PARTY_MON;
510 for (i = 0; i < max_num; i++) {
511 m_ptr = &party_mon[i];
513 /* Skip empty monsters */
514 if (!monster_is_valid(m_ptr))
517 /* Hack -- Increase the racial counter */
518 real_r_ptr(m_ptr)->cur_num++;
523 * @brief 移動先のフロアに伴ったペットを配置する / Place preserved pet monsters on new floor
524 * @param master_ptr プレーヤーへの参照ポインタ
527 static void place_pet(player_type *master_ptr)
530 int max_num = master_ptr->wild_mode ? 1 : MAX_PARTY_MON;
531 floor_type *floor_ptr = master_ptr->current_floor_ptr;
532 for (i = 0; i < max_num; i++) {
533 POSITION cy = 0, cx = 0;
536 if (!(party_mon[i].r_idx))
540 m_idx = m_pop(floor_ptr);
541 master_ptr->riding = m_idx;
550 for (d = 1; d < A_MAX; d++) {
551 for (j = 1000; j > 0; j--) {
552 scatter(master_ptr, &cy, &cx, master_ptr->y, master_ptr->x, d, 0);
553 if (monster_can_enter(master_ptr, cy, cx, &r_info[party_mon[i].r_idx], 0))
559 m_idx = (d == 6) ? 0 : m_pop(floor_ptr);
563 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[m_idx];
566 master_ptr->current_floor_ptr->grid_array[cy][cx].m_idx = m_idx;
568 m_ptr->r_idx = party_mon[i].r_idx;
570 /* Copy all member of the structure */
571 *m_ptr = party_mon[i];
572 r_ptr = real_r_ptr(m_ptr);
576 m_ptr->current_floor_ptr = master_ptr->current_floor_ptr;
578 m_ptr->mtimed[MTIMED_CSLEEP] = 0;
579 m_ptr->hold_o_idx = 0;
582 if ((r_ptr->flags1 & RF1_FORCE_SLEEP) && !ironman_nightmare) {
583 /* Monster is still being nice */
584 m_ptr->mflag |= (MFLAG_NICE);
586 /* Must repair monsters */
587 repair_monsters = TRUE;
589 update_monster(master_ptr, m_idx, TRUE);
590 lite_spot(master_ptr, cy, cx);
592 /* Pre-calculated in precalc_cur_num_of_pet() */
593 /* r_ptr->cur_num++; */
595 /* Hack -- Count the number of "reproducers" */
596 if (r_ptr->flags2 & RF2_MULTIPLY)
597 master_ptr->current_floor_ptr->num_repro++;
600 monster_type *m_ptr = &party_mon[i];
601 monster_race *r_ptr = real_r_ptr(m_ptr);
602 GAME_TEXT m_name[MAX_NLEN];
604 monster_desc(master_ptr, m_name, m_ptr, 0);
605 msg_format(_("%sとはぐれてしまった。", "You have lost sight of %s."), m_name);
606 if (record_named_pet && m_ptr->nickname) {
607 monster_desc(master_ptr, m_name, m_ptr, MD_INDEF_VISIBLE);
608 exe_write_diary(master_ptr, DIARY_NAMED_PET, RECORD_NAMED_PET_LOST_SIGHT, m_name);
611 /* Pre-calculated in precalc_cur_num_of_pet(), but need to decrease */
617 /* For accuracy of precalc_cur_num_of_pet() */
618 (void)C_WIPE(party_mon, MAX_PARTY_MON, monster_type);
622 * @brief ユニークモンスターやアーティファクトの所在フロアを更新する / Hack -- Update location of unique monsters and artifacts
623 * @param cur_floor_id 現在のフロアID
626 * The r_ptr->floor_id and a_ptr->floor_id are not updated correctly\n
627 * while new floor creation since dungeons may be re-created by\n
628 * auto-scum option.\n
630 static void update_unique_artifact(floor_type *floor_ptr, s16b cur_floor_id)
634 /* Maintain unique monsters */
635 for (i = 1; i < floor_ptr->m_max; i++) {
637 monster_type *m_ptr = &floor_ptr->m_list[i];
639 if (!monster_is_valid(m_ptr))
642 /* Extract real monster race */
643 r_ptr = real_r_ptr(m_ptr);
645 /* Memorize location of the unique monster */
646 if ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)) {
647 r_ptr->floor_id = cur_floor_id;
651 /* Maintain artifatcs */
652 for (i = 1; i < floor_ptr->o_max; i++) {
653 object_type *o_ptr = &floor_ptr->o_list[i];
655 if (!object_is_valid(o_ptr))
658 /* Memorize location of the artifact */
659 if (object_is_fixed_artifact(o_ptr)) {
660 a_info[o_ptr->name1].floor_id = cur_floor_id;
666 * @brief フロア移動時、プレイヤーの移動先モンスターが既にいた場合ランダムな近隣に移動させる / When a monster is at a place where player will return,
669 static void get_out_monster(player_type *protected_ptr)
673 POSITION oy = protected_ptr->y;
674 POSITION ox = protected_ptr->x;
675 floor_type *floor_ptr = protected_ptr->current_floor_ptr;
676 MONSTER_IDX m_idx = floor_ptr->grid_array[oy][ox].m_idx;
678 /* Nothing to do if no monster */
682 /* Look until done */
686 /* Pick a (possibly illegal) location */
687 POSITION ny = rand_spread(oy, dis);
688 POSITION nx = rand_spread(ox, dis);
692 /* Stop after 1000 tries */
697 * Increase distance after doing enough tries
698 * compared to area of possible space
700 if (tries > 20 * dis * dis)
703 /* Ignore illegal locations */
704 if (!in_bounds(floor_ptr, ny, nx))
707 /* Require "empty" floor space */
708 if (!is_cave_empty_bold(protected_ptr, ny, nx))
711 /* Hack -- no teleport onto glyph of warding */
712 if (is_glyph_grid(&floor_ptr->grid_array[ny][nx]))
714 if (is_explosive_rune_grid(&floor_ptr->grid_array[ny][nx]))
717 /* ...nor onto the Pattern */
718 if (pattern_tile(floor_ptr, ny, nx))
721 /*** It's a good place ***/
723 m_ptr = &floor_ptr->m_list[m_idx];
725 /* Update the old location */
726 floor_ptr->grid_array[oy][ox].m_idx = 0;
728 /* Update the new location */
729 floor_ptr->grid_array[ny][nx].m_idx = m_idx;
731 /* Move the monster */
735 /* No need to do update_monster() */
743 * @brief 新フロアに移動元フロアに繋がる階段を配置する / Virtually teleport onto the stairs that is connecting between two floors.
744 * @param sf_ptr 移動元の保存フロア構造体参照ポインタ
747 static void locate_connected_stairs(player_type *creature_ptr, floor_type *floor_ptr, saved_floor_type *sf_ptr, BIT_FLAGS floor_mode)
749 POSITION x, y, sx = 0, sy = 0;
750 POSITION x_table[20];
751 POSITION y_table[20];
755 /* Search usable stairs */
756 for (y = 0; y < floor_ptr->height; y++) {
757 for (x = 0; x < floor_ptr->width; x++) {
758 grid_type *g_ptr = &floor_ptr->grid_array[y][x];
759 feature_type *f_ptr = &f_info[g_ptr->feat];
762 if (floor_mode & CFM_UP) {
763 if (have_flag(f_ptr->flags, FF_LESS) && have_flag(f_ptr->flags, FF_STAIRS) && !have_flag(f_ptr->flags, FF_SPECIAL)) {
766 /* Found fixed stairs? */
767 if (g_ptr->special && g_ptr->special == sf_ptr->upper_floor_id) {
774 else if (floor_mode & CFM_DOWN) {
775 if (have_flag(f_ptr->flags, FF_MORE) && have_flag(f_ptr->flags, FF_STAIRS) && !have_flag(f_ptr->flags, FF_SPECIAL)) {
778 /* Found fixed stairs */
779 if (g_ptr->special && g_ptr->special == sf_ptr->lower_floor_id) {
787 if (have_flag(f_ptr->flags, FF_BLDG)) {
792 if (ok && (num < 20)) {
802 creature_ptr->y = sy;
803 creature_ptr->x = sx;
805 /* No stairs found! -- No return */
806 prepare_change_floor_mode(creature_ptr, CFM_RAND_PLACE | CFM_NO_RETURN);
808 /* Mega Hack -- It's not the stairs you enter. Disable it. */
809 if (!feat_uses_special(floor_ptr->grid_array[creature_ptr->y][creature_ptr->x].feat))
810 floor_ptr->grid_array[creature_ptr->y][creature_ptr->x].special = 0;
812 /* Choose random one */
815 /* Point stair location */
816 creature_ptr->y = y_table[i];
817 creature_ptr->x = x_table[i];
822 * @brief 現在のフロアを離れるに伴って行なわれる保存処理
823 * / Maintain quest monsters, mark next floor_id at stairs, save current floor, and prepare to enter next floor.
824 * @param creature_ptr プレーヤーへの参照ポインタ
827 void leave_floor(player_type *creature_ptr)
829 grid_type *g_ptr = NULL;
831 saved_floor_type *sf_ptr;
832 MONRACE_IDX quest_r_idx = 0;
834 FLOOR_IDX tmp_floor_idx = 0;
836 /* Preserve pets and prepare to take these to next floor */
837 preserve_pet(creature_ptr);
839 /* Remove all mirrors without explosion */
840 remove_all_mirrors(creature_ptr, FALSE);
842 if (creature_ptr->special_defense & NINJA_S_STEALTH)
843 set_superstealth(creature_ptr, FALSE);
845 /* New floor is not yet prepared */
848 /* Temporarily get a floor_id (for Arena) */
849 if (!creature_ptr->floor_id && (creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) && !(creature_ptr->change_floor_mode & CFM_NO_RETURN)) {
850 /* Get temporary floor_id */
851 tmp_floor_idx = get_new_floor_id(creature_ptr);
854 /* Search the quest monster index */
855 for (i = 0; i < max_q_idx; i++) {
856 if ((quest[i].status == QUEST_STATUS_TAKEN) && ((quest[i].type == QUEST_TYPE_KILL_LEVEL) || (quest[i].type == QUEST_TYPE_RANDOM))
857 && (quest[i].level == creature_ptr->current_floor_ptr->dun_level) && (creature_ptr->dungeon_idx == quest[i].dungeon)
858 && !(quest[i].flags & QUEST_FLAG_PRESET)) {
859 quest_r_idx = quest[i].r_idx;
863 /* Maintain quest monsters */
864 for (i = 1; i < creature_ptr->current_floor_ptr->m_max; i++) {
866 monster_type *m_ptr = &creature_ptr->current_floor_ptr->m_list[i];
868 if (!monster_is_valid(m_ptr))
871 /* Only maintain quest monsters */
872 if (quest_r_idx != m_ptr->r_idx)
875 /* Extract real monster race */
876 r_ptr = real_r_ptr(m_ptr);
878 /* Ignore unique monsters */
879 if ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL))
882 /* Delete non-unique quest monsters */
883 delete_monster_idx(creature_ptr, i);
886 /* Check if there is a same item */
887 for (i = 0; i < INVEN_PACK; i++) {
888 object_type *o_ptr = &creature_ptr->inventory_list[i];
890 if (!object_is_valid(o_ptr))
893 /* Delete old memorized location of the artifact */
894 if (object_is_fixed_artifact(o_ptr)) {
895 a_info[o_ptr->name1].floor_id = 0;
899 /* Extract current floor info or NULL */
900 sf_ptr = get_sf_ptr(creature_ptr->floor_id);
902 /* Choose random stairs */
903 if ((creature_ptr->change_floor_mode & CFM_RAND_CONNECT) && tmp_floor_idx) {
904 locate_connected_stairs(creature_ptr, creature_ptr->current_floor_ptr, sf_ptr, creature_ptr->change_floor_mode);
907 /* Extract new dungeon level */
908 if (creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) {
909 /* Extract stair position */
910 g_ptr = &creature_ptr->current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
911 f_ptr = &f_info[g_ptr->feat];
913 /* Get back to old saved floor? */
914 if (g_ptr->special && !have_flag(f_ptr->flags, FF_SPECIAL) && get_sf_ptr(g_ptr->special)) {
915 /* Saved floor is exist. Use it. */
916 new_floor_id = g_ptr->special;
919 /* Mark shaft up/down */
920 if (have_flag(f_ptr->flags, FF_STAIRS) && have_flag(f_ptr->flags, FF_SHAFT)) {
921 prepare_change_floor_mode(creature_ptr, CFM_SHAFT);
925 /* Climb up/down some sort of stairs */
926 if (creature_ptr->change_floor_mode & (CFM_DOWN | CFM_UP)) {
929 /* Extract level movement number */
930 if (creature_ptr->change_floor_mode & CFM_DOWN)
932 else if (creature_ptr->change_floor_mode & CFM_UP)
935 /* Shafts are deeper than normal stairs */
936 if (creature_ptr->change_floor_mode & CFM_SHAFT)
937 move_num += SGN(move_num);
939 /* Get out from or Enter the dungeon */
940 if (creature_ptr->change_floor_mode & CFM_DOWN) {
941 if (!creature_ptr->current_floor_ptr->dun_level)
942 move_num = d_info[creature_ptr->dungeon_idx].mindepth;
943 } else if (creature_ptr->change_floor_mode & CFM_UP) {
944 if (creature_ptr->current_floor_ptr->dun_level + move_num < d_info[creature_ptr->dungeon_idx].mindepth)
945 move_num = -creature_ptr->current_floor_ptr->dun_level;
948 creature_ptr->current_floor_ptr->dun_level += move_num;
951 /* Leaving the dungeon to town */
952 if (!creature_ptr->current_floor_ptr->dun_level && creature_ptr->dungeon_idx) {
953 creature_ptr->leaving_dungeon = TRUE;
954 if (!vanilla_town && !lite_town) {
955 creature_ptr->wilderness_y = d_info[creature_ptr->dungeon_idx].dy;
956 creature_ptr->wilderness_x = d_info[creature_ptr->dungeon_idx].dx;
958 creature_ptr->recall_dungeon = creature_ptr->dungeon_idx;
959 creature_ptr->dungeon_idx = 0;
961 /* Reach to the surface -- Clear all saved floors */
962 creature_ptr->change_floor_mode &= ~CFM_SAVE_FLOORS; // TODO
965 /* Kill some old saved floors */
966 if (!(creature_ptr->change_floor_mode & CFM_SAVE_FLOORS)) {
967 /* Kill all saved floors */
968 for (i = 0; i < MAX_SAVED_FLOORS; i++)
969 kill_saved_floor(creature_ptr, &saved_floors[i]);
971 /* Reset visit_mark count */
972 latest_visit_mark = 1;
973 } else if (creature_ptr->change_floor_mode & CFM_NO_RETURN) {
974 /* Kill current floor */
975 kill_saved_floor(creature_ptr, sf_ptr);
978 /* No current floor -- Left/Enter dungeon etc... */
979 if (!creature_ptr->floor_id) {
980 /* No longer need to save current floor */
984 /* Mark next floor_id on the previous floor */
987 new_floor_id = get_new_floor_id(creature_ptr);
989 /* Connect from here */
990 if (g_ptr && !feat_uses_special(g_ptr->feat)) {
991 g_ptr->special = new_floor_id;
995 /* Fix connection -- level teleportation or trap door */
996 if (creature_ptr->change_floor_mode & CFM_RAND_CONNECT) {
997 if (creature_ptr->change_floor_mode & CFM_UP)
998 sf_ptr->upper_floor_id = new_floor_id;
999 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1000 sf_ptr->lower_floor_id = new_floor_id;
1003 /* If you can return, you need to save previous floor */
1004 if ((creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) && !(creature_ptr->change_floor_mode & CFM_NO_RETURN)) {
1005 /* Get out of the my way! */
1006 get_out_monster(creature_ptr);
1008 /* Record the last visit turn of current floor */
1009 sf_ptr->last_visit = current_world_ptr->game_turn;
1011 forget_lite(creature_ptr->current_floor_ptr);
1012 forget_view(creature_ptr->current_floor_ptr);
1013 clear_mon_lite(creature_ptr->current_floor_ptr);
1015 /* Save current floor */
1016 if (!save_floor(creature_ptr, sf_ptr, 0)) {
1017 /* Save failed -- No return */
1018 prepare_change_floor_mode(creature_ptr, CFM_NO_RETURN);
1020 /* Kill current floor */
1021 kill_saved_floor(creature_ptr, get_sf_ptr(creature_ptr->floor_id));
1027 * @brief フロアの切り替え処理 / Enter new floor.
1028 * @param creature_ptr プレーヤーへの参照ポインタ
1031 * If the floor is an old saved floor, it will be\n
1032 * restored from the temporary file. If the floor is new one, new floor\n
1033 * will be generated.\n
1035 void change_floor(player_type *creature_ptr)
1037 saved_floor_type *sf_ptr;
1038 bool loaded = FALSE;
1040 /* The dungeon is not ready */
1041 current_world_ptr->character_dungeon = FALSE;
1043 /* No longer in the trap detecteded region */
1044 creature_ptr->dtrap = FALSE;
1046 /* Mega-Hack -- no panel yet */
1052 /* Mega-Hack -- not ambushed on the wildness? */
1053 creature_ptr->ambush_flag = FALSE;
1055 /* No saved floors (On the surface etc.) */
1056 if (!(creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) && !(creature_ptr->change_floor_mode & CFM_FIRST_FLOOR)) {
1057 generate_floor(creature_ptr);
1059 /* Paranoia -- No new saved floor */
1063 /* In the dungeon */
1065 /* No floor_id yet */
1066 if (!new_floor_id) {
1068 new_floor_id = get_new_floor_id(creature_ptr);
1071 /* Pointer for infomations of new floor */
1072 sf_ptr = get_sf_ptr(new_floor_id);
1074 /* Try to restore old floor */
1075 if (sf_ptr->last_visit) {
1076 /* Old saved floor is exist */
1077 if (load_floor(creature_ptr, sf_ptr, 0)) {
1080 /* Forbid return stairs */
1081 if (creature_ptr->change_floor_mode & CFM_NO_RETURN) {
1082 grid_type *g_ptr = &creature_ptr->current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
1084 if (!feat_uses_special(g_ptr->feat)) {
1085 if (creature_ptr->change_floor_mode & (CFM_DOWN | CFM_UP)) {
1086 /* Reset to floor */
1087 g_ptr->feat = feat_ground_type[randint0(100)];
1097 * Set lower/upper_floor_id of new floor when the new
1098 * floor is right-above/right-under the current floor.
1100 * Stair creation/Teleport level/Trap door will take
1101 * you the same floor when you used it later again.
1103 if (creature_ptr->floor_id) {
1104 saved_floor_type *cur_sf_ptr = get_sf_ptr(creature_ptr->floor_id);
1106 if (creature_ptr->change_floor_mode & CFM_UP) {
1107 /* New floor is right-above */
1108 if (cur_sf_ptr->upper_floor_id == new_floor_id)
1109 sf_ptr->lower_floor_id = creature_ptr->floor_id;
1110 } else if (creature_ptr->change_floor_mode & CFM_DOWN) {
1111 /* New floor is right-under */
1112 if (cur_sf_ptr->lower_floor_id == new_floor_id)
1113 sf_ptr->upper_floor_id = creature_ptr->floor_id;
1117 /* Break connection to killed floor */
1119 if (creature_ptr->change_floor_mode & CFM_UP)
1120 sf_ptr->lower_floor_id = 0;
1121 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1122 sf_ptr->upper_floor_id = 0;
1125 /* Maintain monsters and artifacts */
1128 GAME_TURN tmp_last_visit = sf_ptr->last_visit;
1129 GAME_TURN absence_ticks;
1130 int alloc_chance = d_info[creature_ptr->dungeon_idx].max_m_alloc_chance;
1131 GAME_TURN alloc_times;
1133 while (tmp_last_visit > current_world_ptr->game_turn)
1134 tmp_last_visit -= TURNS_PER_TICK * TOWN_DAWN;
1135 absence_ticks = (current_world_ptr->game_turn - tmp_last_visit) / TURNS_PER_TICK;
1137 /* Maintain monsters */
1138 for (i = 1; i < creature_ptr->current_floor_ptr->m_max; i++) {
1139 monster_race *r_ptr;
1140 monster_type *m_ptr = &creature_ptr->current_floor_ptr->m_list[i];
1142 if (!monster_is_valid(m_ptr))
1145 if (!is_pet(m_ptr)) {
1147 m_ptr->hp = m_ptr->maxhp = m_ptr->max_maxhp;
1149 /* Remove timed status (except MTIMED_CSLEEP) */
1150 (void)set_monster_fast(creature_ptr, i, 0);
1151 (void)set_monster_slow(creature_ptr, i, 0);
1152 (void)set_monster_stunned(creature_ptr, i, 0);
1153 (void)set_monster_confused(creature_ptr, i, 0);
1154 (void)set_monster_monfear(creature_ptr, i, 0);
1155 (void)set_monster_invulner(creature_ptr, i, 0, FALSE);
1158 /* Extract real monster race */
1159 r_ptr = real_r_ptr(m_ptr);
1161 /* Ignore non-unique */
1162 if (!(r_ptr->flags1 & RF1_UNIQUE) && !(r_ptr->flags7 & RF7_NAZGUL))
1165 /* Appear at a different floor? */
1166 if (r_ptr->floor_id != new_floor_id) {
1167 /* Disapper from here */
1168 delete_monster_idx(creature_ptr, i);
1172 /* Maintain artifatcs */
1173 for (i = 1; i < creature_ptr->current_floor_ptr->o_max; i++) {
1174 object_type *o_ptr = &creature_ptr->current_floor_ptr->o_list[i];
1176 if (!object_is_valid(o_ptr))
1179 /* Ignore non-artifact */
1180 if (!object_is_fixed_artifact(o_ptr))
1183 /* Appear at a different floor? */
1184 if (a_info[o_ptr->name1].floor_id != new_floor_id) {
1185 /* Disappear from here */
1186 delete_object_idx(creature_ptr, i);
1188 /* Cancel preserve */
1189 a_info[o_ptr->name1].cur_num = 1;
1193 (void)place_quest_monsters(creature_ptr);
1195 /* Place some random monsters */
1196 alloc_times = absence_ticks / alloc_chance;
1198 if (randint0(alloc_chance) < (absence_ticks % alloc_chance))
1201 for (i = 0; i < alloc_times; i++) {
1202 /* Make a (group of) new monster */
1203 (void)alloc_monster(creature_ptr, 0, 0, summon_specific);
1208 /* New floor_id or failed to restore */
1209 else /* if (!loaded) */
1211 if (sf_ptr->last_visit) {
1212 /* Temporary file is broken? */
1213 msg_print(_("階段は行き止まりだった。", "The staircases come to a dead end..."));
1215 /* Create simple dead end */
1216 build_dead_end(creature_ptr);
1218 /* Break connection */
1219 if (creature_ptr->change_floor_mode & CFM_UP) {
1220 sf_ptr->upper_floor_id = 0;
1221 } else if (creature_ptr->change_floor_mode & CFM_DOWN) {
1222 sf_ptr->lower_floor_id = 0;
1225 generate_floor(creature_ptr);
1228 /* Record last visit turn */
1229 sf_ptr->last_visit = current_world_ptr->game_turn;
1231 /* Set correct dun_level value */
1232 sf_ptr->dun_level = creature_ptr->current_floor_ptr->dun_level;
1234 /* Create connected stairs */
1235 if (!(creature_ptr->change_floor_mode & CFM_NO_RETURN)) {
1236 /* Extract stair position */
1237 grid_type *g_ptr = &creature_ptr->current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
1239 /*** Create connected stairs ***/
1241 /* No stairs down from Quest */
1242 if ((creature_ptr->change_floor_mode & CFM_UP) && !quest_number(creature_ptr, creature_ptr->current_floor_ptr->dun_level)) {
1243 g_ptr->feat = (creature_ptr->change_floor_mode & CFM_SHAFT) ? feat_state(creature_ptr, feat_down_stair, FF_SHAFT) : feat_down_stair;
1246 /* No stairs up when ironman_downward */
1247 else if ((creature_ptr->change_floor_mode & CFM_DOWN) && !ironman_downward) {
1248 g_ptr->feat = (creature_ptr->change_floor_mode & CFM_SHAFT) ? feat_state(creature_ptr, feat_up_stair, FF_SHAFT) : feat_up_stair;
1251 /* Paranoia -- Clear mimic */
1254 /* Connect to previous floor */
1255 g_ptr->special = creature_ptr->floor_id;
1259 /* Arrive at random grid */
1260 if (creature_ptr->change_floor_mode & (CFM_RAND_PLACE)) {
1261 (void)new_player_spot(creature_ptr);
1264 /* You see stairs blocked */
1265 else if ((creature_ptr->change_floor_mode & CFM_NO_RETURN) && (creature_ptr->change_floor_mode & (CFM_DOWN | CFM_UP))) {
1266 if (!creature_ptr->blind) {
1267 msg_print(_("突然階段が塞がれてしまった。", "Suddenly the stairs is blocked!"));
1269 msg_print(_("ゴトゴトと何か音がした。", "You hear some noises."));
1276 * The "turn" is not always different number because
1277 * the level teleport doesn't take any turn. Use
1278 * visit mark instead of last visit turn to find the
1279 * oldest saved floor.
1281 sf_ptr->visit_mark = latest_visit_mark++;
1284 /* Place preserved pet monsters */
1285 place_pet(creature_ptr);
1287 /* Reset travel target place */
1288 forget_travel_flow(creature_ptr->current_floor_ptr);
1290 /* Hack -- maintain unique and artifacts */
1291 update_unique_artifact(creature_ptr->current_floor_ptr, new_floor_id);
1293 /* Now the player is in new floor */
1294 creature_ptr->floor_id = new_floor_id;
1296 /* The dungeon is ready */
1297 current_world_ptr->character_dungeon = TRUE;
1299 /* Hack -- Munchkin characters always get whole map */
1300 if (creature_ptr->pseikaku == PERSONALITY_MUNCHKIN)
1301 wiz_lite(creature_ptr, (bool)(creature_ptr->pclass == CLASS_NINJA));
1303 /* Remember when this level was "created" */
1304 creature_ptr->current_floor_ptr->generated_turn = current_world_ptr->game_turn;
1306 /* No dungeon feeling yet */
1307 creature_ptr->feeling_turn = creature_ptr->current_floor_ptr->generated_turn;
1308 creature_ptr->feeling = 0;
1310 /* Clear all flags */
1311 creature_ptr->change_floor_mode = 0L;
1313 select_floor_music(creature_ptr);
1314 creature_ptr->change_floor_mode = 0;