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
13 #include "floor/floor-save.h"
14 #include "cmd-building/cmd-building.h"
15 #include "cmd-io/cmd-dump.h"
16 #include "cmd/cmd-basic.h"
17 #include "core/player-processor.h"
18 #include "dungeon/dungeon.h"
19 #include "dungeon/quest.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 "grid/feature.h"
27 #include "grid/grid.h"
28 #include "io/files-util.h"
31 #include "io/uid-checker.h"
32 #include "io/write-diary.h"
33 #include "mind/mind-mirror-master.h"
34 #include "monster-race/race-flags1.h"
35 #include "monster-race/race-flags2.h"
36 #include "monster-race/race-flags7.h"
37 #include "monster/monster-description-types.h"
38 #include "monster/monster-flag-types.h"
39 #include "monster/monster-status.h"
40 #include "monster/monster-info.h"
41 #include "monster/monster2.h"
42 #include "monster/smart-learn-types.h"
43 #include "object-enchant/artifact.h"
44 #include "object/object-hook.h"
45 #include "pet/pet-util.h"
46 #include "player/player-class.h"
47 #include "player/player-effects.h"
48 #include "player/player-personalities-table.h"
49 #include "spell-kind/spells-floor.h"
50 #include "system/system-variables.h"
51 #include "util/util.h"
52 #include "view/display-main-window.h"
53 #include "world/world.h"
55 static FLOOR_IDX new_floor_id; /*!<次のフロアのID / floor_id of the destination */
56 static u32b latest_visit_mark; /*!<フロアを渡った回数?(確認中) / Max number of visit_mark */
57 #define MAX_PARTY_MON 21 /*!< フロア移動時に先のフロアに連れて行けるペットの最大数 Maximum number of preservable pets */
58 static monster_type party_mon[MAX_PARTY_MON]; /*!< フロア移動に保存するペットモンスターの配列 */
61 * Number of floor_id used from birth
63 FLOOR_IDX max_floor_id;
66 * Sign for current process used in temporary files.
67 * Actually it is the start time of current process.
69 u32b saved_floor_file_sign;
72 * @brief 保存フロア配列を初期化する / Initialize saved_floors array.
73 * @param creature_ptr プレーヤーへの参照ポインタ
74 * @param force テンポラリファイルが残っていた場合も警告なしで強制的に削除するフラグ
75 * @details Make sure that old temporary files are not remaining as gurbages.
78 void init_saved_floors(player_type *creature_ptr, bool force)
80 char floor_savefile[1024];
83 BIT_FLAGS mode = 0644;
84 for (i = 0; i < MAX_SAVED_FLOORS; i++)
86 saved_floor_type *sf_ptr = &saved_floors[i];
89 sprintf(floor_savefile, "%s.F%02d", savefile, i);
91 /* Grab permissions */
94 /* Try to create the file */
95 fd = fd_make(floor_savefile, mode);
97 /* Drop permissions */
105 msg_print(_("エラー:古いテンポラリ・ファイルが残っています。", "Error: There are old temporary files."));
106 msg_print(_("変愚蛮怒を二重に起動していないか確認してください。", "Make sure you are not running two game processes simultaneously."));
107 msg_print(_("過去に変愚蛮怒がクラッシュした場合は一時ファイルを", "If the temporary files are garbage from an old crashed process, "));
108 msg_print(_("強制的に削除して実行を続けられます。", "you can delete them safely."));
109 if (!get_check(_("強制的に削除してもよろしいですか?", "Do you delete the old temporary files? ")))
110 quit(_("実行中止", "Aborted."));
120 /* Grab permissions */
123 /* Simply kill the temporary file */
124 (void)fd_kill(floor_savefile);
126 /* Drop permissions */
129 sf_ptr->floor_id = 0;
132 /* No floor_id used yet (No.0 is reserved to indicate non existance) */
135 /* vist_mark is from 1 */
136 latest_visit_mark = 1;
138 /* A sign to mark temporary files */
139 saved_floor_file_sign = (u32b)time(NULL);
141 /* No next floor yet */
144 /* No change floor mode yet */
145 creature_ptr->change_floor_mode = 0;
149 * @brief 保存フロア用テンポラリファイルを削除する / Kill temporary files
150 * @details Should be called just before the game quit.
151 * @param creature_ptr プレーヤーへの参照ポインタ
154 void clear_saved_floor_files(player_type *creature_ptr)
156 char floor_savefile[1024];
158 for (i = 0; i < MAX_SAVED_FLOORS; i++)
160 saved_floor_type *sf_ptr = &saved_floors[i];
162 /* No temporary file */
163 if (!sf_ptr->floor_id) continue;
164 if (sf_ptr->floor_id == creature_ptr->floor_id) continue;
167 sprintf(floor_savefile, "%s.F%02d", savefile, i);
169 /* Grab permissions */
172 /* Simply kill the temporary file */
173 (void)fd_kill(floor_savefile);
175 /* Drop permissions */
181 * @brief 保存フロアIDから参照ポインタを得る / Get a pointer for an item of the saved_floors array.
182 * @param floor_id 保存フロアID
183 * @return IDに対応する保存フロアのポインタ、ない場合はNULLを返す。
185 saved_floor_type *get_sf_ptr(FLOOR_IDX floor_id)
189 /* floor_id No.0 indicates no floor */
190 if (!floor_id) return NULL;
192 for (i = 0; i < MAX_SAVED_FLOORS; i++)
194 saved_floor_type *sf_ptr = &saved_floors[i];
196 if (sf_ptr->floor_id == floor_id) return sf_ptr;
205 * @brief 参照ポインタ先の保存フロアを抹消する / kill a saved floor and get an empty space
206 * @param creature_ptr プレーヤーへの参照ポインタ
207 * @param sf_ptr 保存フロアの参照ポインタ
210 static void kill_saved_floor(player_type *creature_ptr, saved_floor_type *sf_ptr)
212 char floor_savefile[1024];
216 if (!sf_ptr->floor_id) return;
218 if (sf_ptr->floor_id == creature_ptr->floor_id)
220 /* Kill current floor */
221 creature_ptr->floor_id = 0;
223 /* Current floor doesn't have temporary file */
228 sprintf(floor_savefile, "%s.F%02d", savefile, (int)sf_ptr->savefile_id);
230 /* Grab permissions */
233 /* Simply kill the temporary file */
234 (void)fd_kill(floor_savefile);
236 /* Drop permissions */
240 /* No longer exists */
241 sf_ptr->floor_id = 0;
246 * @brief 新規に利用可能な保存フロアを返す / Initialize new saved floor and get its floor id.
247 * @param creature_ptr プレーヤーへの参照ポインタ
248 * @return 利用可能な保存フロアID
250 * If number of saved floors are already MAX_SAVED_FLOORS, kill the oldest one.
252 FLOOR_IDX get_new_floor_id(player_type *creature_ptr)
254 saved_floor_type *sf_ptr = NULL;
257 /* Look for empty space */
258 for (i = 0; i < MAX_SAVED_FLOORS; i++)
260 sf_ptr = &saved_floors[i];
262 if (!sf_ptr->floor_id) break;
266 if (i == MAX_SAVED_FLOORS)
269 u32b oldest_visit = 0xffffffffL;
271 /* Search for oldest */
272 for (i = 0; i < MAX_SAVED_FLOORS; i++)
274 sf_ptr = &saved_floors[i];
276 /* Don't kill current floor */
277 if (sf_ptr->floor_id == creature_ptr->floor_id) continue;
279 /* Don't kill newer */
280 if (sf_ptr->visit_mark > oldest_visit) continue;
283 oldest_visit = sf_ptr->visit_mark;
286 /* Kill oldest saved floor */
287 sf_ptr = &saved_floors[oldest];
288 kill_saved_floor(creature_ptr, sf_ptr);
294 /* Prepare new floor data */
295 sf_ptr->savefile_id = i;
296 sf_ptr->floor_id = max_floor_id;
297 sf_ptr->last_visit = 0;
298 sf_ptr->upper_floor_id = 0;
299 sf_ptr->lower_floor_id = 0;
300 sf_ptr->visit_mark = latest_visit_mark++;
302 /* sf_ptr->dun_level may be changed later */
303 sf_ptr->dun_level = creature_ptr->current_floor_ptr->dun_level;
306 /* Increment number of floor_id */
307 if (max_floor_id < MAX_SHORT) max_floor_id++;
309 /* 32767 floor_ids are all used up! Re-use ancient IDs */
310 else max_floor_id = 1;
312 return sf_ptr->floor_id;
317 * @brief フロア切り替え時の処理フラグを追加する / Prepare mode flags of changing floor
318 * @param creature_ptr プレーヤーへの参照ポインタ
319 * @param mode 追加したい所持フラグ
322 void prepare_change_floor_mode(player_type *creature_ptr, BIT_FLAGS mode)
324 creature_ptr->change_floor_mode |= mode;
328 * @brief 階段移動先のフロアが生成できない時に簡単な行き止まりマップを作成する / Builds the dead end
331 static void build_dead_end(player_type *creature_ptr)
335 clear_cave(creature_ptr);
337 /* Mega-Hack -- no player yet */
338 creature_ptr->x = creature_ptr->y = 0;
340 /* Fill the arrays of floors and walls in the good proportions */
341 set_floor_and_wall(0);
344 creature_ptr->current_floor_ptr->height = SCREEN_HGT;
345 creature_ptr->current_floor_ptr->width = SCREEN_WID;
347 /* Filled with permanent walls */
348 for (y = 0; y < MAX_HGT; y++)
350 for (x = 0; x < MAX_WID; x++)
352 /* Create "solid" perma-wall */
353 place_bold(creature_ptr, y, x, GB_SOLID_PERM);
357 /* Place at center of the floor */
358 creature_ptr->y = creature_ptr->current_floor_ptr->height / 2;
359 creature_ptr->x = creature_ptr->current_floor_ptr->width / 2;
361 /* Give one square */
362 place_bold(creature_ptr, creature_ptr->y, creature_ptr->x, GB_FLOOR);
364 wipe_generate_random_floor_flags(creature_ptr->current_floor_ptr);
369 * @brief フロア移動時のペット保存処理 / Preserve_pets
370 * @param master_ptr プレーヤーへの参照ポインタ
373 static void preserve_pet(player_type *master_ptr)
378 for (num = 0; num < MAX_PARTY_MON; num++)
380 party_mon[num].r_idx = 0;
383 if (master_ptr->riding)
385 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[master_ptr->riding];
387 /* Pet of other pet don't follow. */
388 if (m_ptr->parent_m_idx)
390 master_ptr->riding = 0;
391 master_ptr->pet_extra_flags &= ~(PF_TWO_HANDS);
392 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)
410 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)) continue;
415 if (!is_pet(m_ptr)) continue;
416 if (i == master_ptr->riding) continue;
418 if (reinit_wilderness)
420 /* 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)) continue;
429 /* Pet of other pet don't follow. */
430 if (m_ptr->parent_m_idx) continue;
433 * Pets with nickname will follow even from 3 blocks away
434 * when you or the pet can see the other.
436 if (m_ptr->nickname &&
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) && projectable(master_ptr, m_ptr->fy, m_ptr->fx, master_ptr->y, master_ptr->x))))
440 if (dis > 3) continue;
444 if (dis > 1) continue;
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)
459 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)) continue;
465 if (!is_pet(m_ptr)) continue;
466 if (!m_ptr->nickname) continue;
467 if (master_ptr->riding == i) continue;
469 monster_desc(master_ptr, m_name, m_ptr, MD_ASSUME_VISIBLE | MD_INDEF_VISIBLE);
470 exe_write_diary(master_ptr, DIARY_NAMED_PET, RECORD_NAMED_PET_MOVED, m_name);
475 /* Pet of other pet may disappear. */
476 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)
483 /* Its parent have gone, it also goes away. */
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);
499 * @brief フロア移動時にペットを伴った場合の準備処理 / Pre-calculate the racial counters of preserved pets
500 * @param master_ptr プレーヤーへの参照ポインタ
503 * To prevent multiple generation of unique monster who is the minion of player
505 void precalc_cur_num_of_pet(player_type *player_ptr)
509 int max_num = player_ptr->wild_mode ? 1 : MAX_PARTY_MON;
511 for (i = 0; i < max_num; i++)
513 m_ptr = &party_mon[i];
515 /* Skip empty monsters */
516 if (!monster_is_valid(m_ptr)) continue;
518 /* Hack -- Increase the racial counter */
519 real_r_ptr(m_ptr)->cur_num++;
524 * @brief 移動先のフロアに伴ったペットを配置する / Place preserved pet monsters on new floor
525 * @param master_ptr プレーヤーへの参照ポインタ
528 static void place_pet(player_type *master_ptr)
531 int max_num = master_ptr->wild_mode ? 1 : MAX_PARTY_MON;
533 for (i = 0; i < max_num; i++)
535 POSITION cy = 0, cx = 0;
538 if (!(party_mon[i].r_idx)) continue;
542 m_idx = m_pop(master_ptr);
543 master_ptr->riding = m_idx;
555 for (d = 1; d < A_MAX; d++)
557 for (j = 1000; j > 0; j--)
559 scatter(master_ptr, &cy, &cx, master_ptr->y, master_ptr->x, d, 0);
560 if (monster_can_enter(master_ptr, cy, cx, &r_info[party_mon[i].r_idx], 0)) break;
564 m_idx = (d == 6) ? 0 : m_pop(master_ptr);
569 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[m_idx];
572 master_ptr->current_floor_ptr->grid_array[cy][cx].m_idx = m_idx;
574 m_ptr->r_idx = party_mon[i].r_idx;
576 /* Copy all member of the structure */
577 *m_ptr = party_mon[i];
578 r_ptr = real_r_ptr(m_ptr);
582 m_ptr->current_floor_ptr = master_ptr->current_floor_ptr;
584 m_ptr->mtimed[MTIMED_CSLEEP] = 0;
585 m_ptr->hold_o_idx = 0;
588 if ((r_ptr->flags1 & RF1_FORCE_SLEEP) && !ironman_nightmare)
590 /* Monster is still being nice */
591 m_ptr->mflag |= (MFLAG_NICE);
593 /* Must repair monsters */
594 repair_monsters = TRUE;
596 update_monster(master_ptr, m_idx, TRUE);
597 lite_spot(master_ptr, cy, cx);
599 /* Pre-calculated in precalc_cur_num_of_pet() */
600 /* r_ptr->cur_num++; */
602 /* Hack -- Count the number of "reproducers" */
603 if (r_ptr->flags2 & RF2_MULTIPLY) master_ptr->current_floor_ptr->num_repro++;
608 monster_type *m_ptr = &party_mon[i];
609 monster_race *r_ptr = real_r_ptr(m_ptr);
610 GAME_TEXT m_name[MAX_NLEN];
612 monster_desc(master_ptr, m_name, m_ptr, 0);
613 msg_format(_("%sとはぐれてしまった。", "You have lost sight of %s."), m_name);
614 if (record_named_pet && m_ptr->nickname)
616 monster_desc(master_ptr, m_name, m_ptr, MD_INDEF_VISIBLE);
617 exe_write_diary(master_ptr, DIARY_NAMED_PET, RECORD_NAMED_PET_LOST_SIGHT, m_name);
620 /* Pre-calculated in precalc_cur_num_of_pet(), but need to decrease */
621 if (r_ptr->cur_num) r_ptr->cur_num--;
625 /* For accuracy of precalc_cur_num_of_pet() */
626 (void)C_WIPE(party_mon, MAX_PARTY_MON, monster_type);
631 * @brief ユニークモンスターやアーティファクトの所在フロアを更新する / Hack -- Update location of unique monsters and artifacts
632 * @param cur_floor_id 現在のフロアID
635 * The r_ptr->floor_id and a_ptr->floor_id are not updated correctly\n
636 * while new floor creation since dungeons may be re-created by\n
637 * auto-scum option.\n
639 static void update_unique_artifact(floor_type *floor_ptr, s16b cur_floor_id)
643 /* Maintain unique monsters */
644 for (i = 1; i < floor_ptr->m_max; i++)
647 monster_type *m_ptr = &floor_ptr->m_list[i];
649 if (!monster_is_valid(m_ptr)) continue;
651 /* Extract real monster race */
652 r_ptr = real_r_ptr(m_ptr);
654 /* Memorize location of the unique monster */
655 if ((r_ptr->flags1 & RF1_UNIQUE) ||
656 (r_ptr->flags7 & RF7_NAZGUL))
658 r_ptr->floor_id = cur_floor_id;
662 /* Maintain artifatcs */
663 for (i = 1; i < floor_ptr->o_max; i++)
665 object_type *o_ptr = &floor_ptr->o_list[i];
667 if (!OBJECT_IS_VALID(o_ptr)) continue;
669 /* Memorize location of the artifact */
670 if (object_is_fixed_artifact(o_ptr))
672 a_info[o_ptr->name1].floor_id = cur_floor_id;
679 * @brief フロア移動時、プレイヤーの移動先モンスターが既にいた場合ランダムな近隣に移動させる / When a monster is at a place where player will return,
682 static void get_out_monster(player_type *protected_ptr)
686 POSITION oy = protected_ptr->y;
687 POSITION ox = protected_ptr->x;
688 floor_type *floor_ptr = protected_ptr->current_floor_ptr;
689 MONSTER_IDX m_idx = floor_ptr->grid_array[oy][ox].m_idx;
691 /* Nothing to do if no monster */
694 /* Look until done */
699 /* Pick a (possibly illegal) location */
700 POSITION ny = rand_spread(oy, dis);
701 POSITION nx = rand_spread(ox, dis);
705 /* Stop after 1000 tries */
706 if (tries > 10000) return;
709 * Increase distance after doing enough tries
710 * compared to area of possible space
712 if (tries > 20 * dis * dis) dis++;
714 /* Ignore illegal locations */
715 if (!in_bounds(floor_ptr, ny, nx)) continue;
717 /* Require "empty" floor space */
718 if (!is_cave_empty_bold(protected_ptr, ny, nx)) continue;
720 /* Hack -- no teleport onto glyph of warding */
721 if (is_glyph_grid(&floor_ptr->grid_array[ny][nx])) continue;
722 if (is_explosive_rune_grid(&floor_ptr->grid_array[ny][nx])) continue;
724 /* ...nor onto the Pattern */
725 if (pattern_tile(floor_ptr, ny, nx)) continue;
727 /*** It's a good place ***/
729 m_ptr = &floor_ptr->m_list[m_idx];
731 /* Update the old location */
732 floor_ptr->grid_array[oy][ox].m_idx = 0;
734 /* Update the new location */
735 floor_ptr->grid_array[ny][nx].m_idx = m_idx;
737 /* Move the monster */
741 /* No need to do update_monster() */
749 * @brief 新フロアに移動元フロアに繋がる階段を配置する / Virtually teleport onto the stairs that is connecting between two floors.
750 * @param sf_ptr 移動元の保存フロア構造体参照ポインタ
753 static void locate_connected_stairs(player_type *creature_ptr, floor_type *floor_ptr, saved_floor_type *sf_ptr, BIT_FLAGS floor_mode)
755 POSITION x, y, sx = 0, sy = 0;
756 POSITION x_table[20];
757 POSITION y_table[20];
761 /* Search usable stairs */
762 for (y = 0; y < floor_ptr->height; y++)
764 for (x = 0; x < floor_ptr->width; x++)
766 grid_type *g_ptr = &floor_ptr->grid_array[y][x];
767 feature_type *f_ptr = &f_info[g_ptr->feat];
770 if (floor_mode & CFM_UP)
772 if (have_flag(f_ptr->flags, FF_LESS) && have_flag(f_ptr->flags, FF_STAIRS) &&
773 !have_flag(f_ptr->flags, FF_SPECIAL))
777 /* Found fixed stairs? */
778 if (g_ptr->special &&
779 g_ptr->special == sf_ptr->upper_floor_id)
787 else if (floor_mode & CFM_DOWN)
789 if (have_flag(f_ptr->flags, FF_MORE) && have_flag(f_ptr->flags, FF_STAIRS) &&
790 !have_flag(f_ptr->flags, FF_SPECIAL))
794 /* Found fixed stairs */
795 if (g_ptr->special &&
796 g_ptr->special == sf_ptr->lower_floor_id)
806 if (have_flag(f_ptr->flags, FF_BLDG))
812 if (ok && (num < 20))
824 creature_ptr->y = sy;
825 creature_ptr->x = sx;
829 /* No stairs found! -- No return */
830 prepare_change_floor_mode(creature_ptr, CFM_RAND_PLACE | CFM_NO_RETURN);
832 /* Mega Hack -- It's not the stairs you enter. Disable it. */
833 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;
837 /* Choose random one */
840 /* Point stair location */
841 creature_ptr->y = y_table[i];
842 creature_ptr->x = x_table[i];
847 * @brief 現在のフロアを離れるに伴って行なわれる保存処理
848 * / Maintain quest monsters, mark next floor_id at stairs, save current floor, and prepare to enter next floor.
849 * @param creature_ptr プレーヤーへの参照ポインタ
852 void leave_floor(player_type *creature_ptr)
854 grid_type *g_ptr = NULL;
856 saved_floor_type *sf_ptr;
857 MONRACE_IDX quest_r_idx = 0;
859 FLOOR_IDX tmp_floor_idx = 0;
861 /* Preserve pets and prepare to take these to next floor */
862 preserve_pet(creature_ptr);
864 /* Remove all mirrors without explosion */
865 remove_all_mirrors(creature_ptr, FALSE);
867 if (creature_ptr->special_defense & NINJA_S_STEALTH) set_superstealth(creature_ptr, FALSE);
869 /* New floor is not yet prepared */
872 /* Temporarily get a floor_id (for Arena) */
873 if (!creature_ptr->floor_id &&
874 (creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) &&
875 !(creature_ptr->change_floor_mode & CFM_NO_RETURN))
877 /* Get temporary floor_id */
878 tmp_floor_idx = get_new_floor_id(creature_ptr);
881 /* Search the quest monster index */
882 for (i = 0; i < max_q_idx; i++)
884 if ((quest[i].status == QUEST_STATUS_TAKEN) &&
885 ((quest[i].type == QUEST_TYPE_KILL_LEVEL) ||
886 (quest[i].type == QUEST_TYPE_RANDOM)) &&
887 (quest[i].level == creature_ptr->current_floor_ptr->dun_level) &&
888 (creature_ptr->dungeon_idx == quest[i].dungeon) &&
889 !(quest[i].flags & QUEST_FLAG_PRESET))
891 quest_r_idx = quest[i].r_idx;
895 /* Maintain quest monsters */
896 for (i = 1; i < creature_ptr->current_floor_ptr->m_max; i++)
899 monster_type *m_ptr = &creature_ptr->current_floor_ptr->m_list[i];
901 if (!monster_is_valid(m_ptr)) continue;
903 /* Only maintain quest monsters */
904 if (quest_r_idx != m_ptr->r_idx) continue;
906 /* Extract real monster race */
907 r_ptr = real_r_ptr(m_ptr);
909 /* Ignore unique monsters */
910 if ((r_ptr->flags1 & RF1_UNIQUE) ||
911 (r_ptr->flags7 & RF7_NAZGUL)) continue;
913 /* Delete non-unique quest monsters */
914 delete_monster_idx(creature_ptr, i);
917 /* Check if there is a same item */
918 for (i = 0; i < INVEN_PACK; i++)
920 object_type *o_ptr = &creature_ptr->inventory_list[i];
922 if (!OBJECT_IS_VALID(o_ptr)) continue;
924 /* Delete old memorized location of the artifact */
925 if (object_is_fixed_artifact(o_ptr))
927 a_info[o_ptr->name1].floor_id = 0;
931 /* Extract current floor info or NULL */
932 sf_ptr = get_sf_ptr(creature_ptr->floor_id);
934 /* Choose random stairs */
935 if ((creature_ptr->change_floor_mode & CFM_RAND_CONNECT) && tmp_floor_idx)
937 locate_connected_stairs(creature_ptr, creature_ptr->current_floor_ptr, sf_ptr, creature_ptr->change_floor_mode);
940 /* Extract new dungeon level */
941 if (creature_ptr->change_floor_mode & CFM_SAVE_FLOORS)
943 /* Extract stair position */
944 g_ptr = &creature_ptr->current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
945 f_ptr = &f_info[g_ptr->feat];
947 /* Get back to old saved floor? */
948 if (g_ptr->special && !have_flag(f_ptr->flags, FF_SPECIAL) && get_sf_ptr(g_ptr->special))
950 /* Saved floor is exist. Use it. */
951 new_floor_id = g_ptr->special;
954 /* Mark shaft up/down */
955 if (have_flag(f_ptr->flags, FF_STAIRS) && have_flag(f_ptr->flags, FF_SHAFT))
957 prepare_change_floor_mode(creature_ptr, CFM_SHAFT);
961 /* Climb up/down some sort of stairs */
962 if (creature_ptr->change_floor_mode & (CFM_DOWN | CFM_UP))
966 /* Extract level movement number */
967 if (creature_ptr->change_floor_mode & CFM_DOWN) move_num = 1;
968 else if (creature_ptr->change_floor_mode & CFM_UP) move_num = -1;
970 /* Shafts are deeper than normal stairs */
971 if (creature_ptr->change_floor_mode & CFM_SHAFT)
972 move_num += SGN(move_num);
974 /* Get out from or Enter the dungeon */
975 if (creature_ptr->change_floor_mode & CFM_DOWN)
977 if (!creature_ptr->current_floor_ptr->dun_level)
978 move_num = d_info[creature_ptr->dungeon_idx].mindepth;
980 else if (creature_ptr->change_floor_mode & CFM_UP)
982 if (creature_ptr->current_floor_ptr->dun_level + move_num < d_info[creature_ptr->dungeon_idx].mindepth)
983 move_num = -creature_ptr->current_floor_ptr->dun_level;
986 creature_ptr->current_floor_ptr->dun_level += move_num;
989 /* Leaving the dungeon to town */
990 if (!creature_ptr->current_floor_ptr->dun_level && creature_ptr->dungeon_idx)
992 creature_ptr->leaving_dungeon = TRUE;
993 if (!vanilla_town && !lite_town)
995 creature_ptr->wilderness_y = d_info[creature_ptr->dungeon_idx].dy;
996 creature_ptr->wilderness_x = d_info[creature_ptr->dungeon_idx].dx;
998 creature_ptr->recall_dungeon = creature_ptr->dungeon_idx;
999 creature_ptr->dungeon_idx = 0;
1001 /* Reach to the surface -- Clear all saved floors */
1002 creature_ptr->change_floor_mode &= ~CFM_SAVE_FLOORS; // TODO
1005 /* Kill some old saved floors */
1006 if (!(creature_ptr->change_floor_mode & CFM_SAVE_FLOORS))
1008 /* Kill all saved floors */
1009 for (i = 0; i < MAX_SAVED_FLOORS; i++)
1010 kill_saved_floor(creature_ptr, &saved_floors[i]);
1012 /* Reset visit_mark count */
1013 latest_visit_mark = 1;
1015 else if (creature_ptr->change_floor_mode & CFM_NO_RETURN)
1017 /* Kill current floor */
1018 kill_saved_floor(creature_ptr, sf_ptr);
1021 /* No current floor -- Left/Enter dungeon etc... */
1022 if (!creature_ptr->floor_id)
1024 /* No longer need to save current floor */
1028 /* Mark next floor_id on the previous floor */
1032 new_floor_id = get_new_floor_id(creature_ptr);
1034 /* Connect from here */
1035 if (g_ptr && !feat_uses_special(g_ptr->feat))
1037 g_ptr->special = new_floor_id;
1041 /* Fix connection -- level teleportation or trap door */
1042 if (creature_ptr->change_floor_mode & CFM_RAND_CONNECT)
1044 if (creature_ptr->change_floor_mode & CFM_UP)
1045 sf_ptr->upper_floor_id = new_floor_id;
1046 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1047 sf_ptr->lower_floor_id = new_floor_id;
1050 /* If you can return, you need to save previous floor */
1051 if ((creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) &&
1052 !(creature_ptr->change_floor_mode & CFM_NO_RETURN))
1054 /* Get out of the my way! */
1055 get_out_monster(creature_ptr);
1057 /* Record the last visit turn of current floor */
1058 sf_ptr->last_visit = current_world_ptr->game_turn;
1060 forget_lite(creature_ptr->current_floor_ptr);
1061 forget_view(creature_ptr->current_floor_ptr);
1062 clear_mon_lite(creature_ptr->current_floor_ptr);
1064 /* Save current floor */
1065 if (!save_floor(creature_ptr, sf_ptr, 0))
1067 /* Save failed -- No return */
1068 prepare_change_floor_mode(creature_ptr, CFM_NO_RETURN);
1070 /* Kill current floor */
1071 kill_saved_floor(creature_ptr, get_sf_ptr(creature_ptr->floor_id));
1078 * @brief フロアの切り替え処理 / Enter new floor.
1079 * @param creature_ptr プレーヤーへの参照ポインタ
1082 * If the floor is an old saved floor, it will be\n
1083 * restored from the temporary file. If the floor is new one, new floor\n
1084 * will be generated.\n
1086 void change_floor(player_type *creature_ptr)
1088 saved_floor_type *sf_ptr;
1089 bool loaded = FALSE;
1091 /* The dungeon is not ready */
1092 current_world_ptr->character_dungeon = FALSE;
1094 /* No longer in the trap detecteded region */
1095 creature_ptr->dtrap = FALSE;
1097 /* Mega-Hack -- no panel yet */
1103 /* Mega-Hack -- not ambushed on the wildness? */
1104 creature_ptr->ambush_flag = FALSE;
1106 /* No saved floors (On the surface etc.) */
1107 if (!(creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) &&
1108 !(creature_ptr->change_floor_mode & CFM_FIRST_FLOOR))
1110 generate_floor(creature_ptr);
1112 /* Paranoia -- No new saved floor */
1116 /* In the dungeon */
1119 /* No floor_id yet */
1123 new_floor_id = get_new_floor_id(creature_ptr);
1126 /* Pointer for infomations of new floor */
1127 sf_ptr = get_sf_ptr(new_floor_id);
1129 /* Try to restore old floor */
1130 if (sf_ptr->last_visit)
1132 /* Old saved floor is exist */
1133 if (load_floor(creature_ptr, sf_ptr, 0))
1137 /* Forbid return stairs */
1138 if (creature_ptr->change_floor_mode & CFM_NO_RETURN)
1140 grid_type *g_ptr = &creature_ptr->current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
1142 if (!feat_uses_special(g_ptr->feat))
1144 if (creature_ptr->change_floor_mode & (CFM_DOWN | CFM_UP))
1146 /* Reset to floor */
1147 g_ptr->feat = feat_ground_type[randint0(100)];
1157 * Set lower/upper_floor_id of new floor when the new
1158 * floor is right-above/right-under the current floor.
1160 * Stair creation/Teleport level/Trap door will take
1161 * you the same floor when you used it later again.
1163 if (creature_ptr->floor_id)
1165 saved_floor_type *cur_sf_ptr = get_sf_ptr(creature_ptr->floor_id);
1167 if (creature_ptr->change_floor_mode & CFM_UP)
1169 /* New floor is right-above */
1170 if (cur_sf_ptr->upper_floor_id == new_floor_id)
1171 sf_ptr->lower_floor_id = creature_ptr->floor_id;
1173 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1175 /* New floor is right-under */
1176 if (cur_sf_ptr->lower_floor_id == new_floor_id)
1177 sf_ptr->upper_floor_id = creature_ptr->floor_id;
1181 /* Break connection to killed floor */
1184 if (creature_ptr->change_floor_mode & CFM_UP)
1185 sf_ptr->lower_floor_id = 0;
1186 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1187 sf_ptr->upper_floor_id = 0;
1190 /* Maintain monsters and artifacts */
1194 GAME_TURN tmp_last_visit = sf_ptr->last_visit;
1195 GAME_TURN absence_ticks;
1196 int alloc_chance = d_info[creature_ptr->dungeon_idx].max_m_alloc_chance;
1197 GAME_TURN alloc_times;
1199 while (tmp_last_visit > current_world_ptr->game_turn) tmp_last_visit -= TURNS_PER_TICK * TOWN_DAWN;
1200 absence_ticks = (current_world_ptr->game_turn - tmp_last_visit) / TURNS_PER_TICK;
1202 /* Maintain monsters */
1203 for (i = 1; i < creature_ptr->current_floor_ptr->m_max; i++)
1205 monster_race *r_ptr;
1206 monster_type *m_ptr = &creature_ptr->current_floor_ptr->m_list[i];
1208 if (!monster_is_valid(m_ptr)) continue;
1213 m_ptr->hp = m_ptr->maxhp = m_ptr->max_maxhp;
1215 /* Remove timed status (except MTIMED_CSLEEP) */
1216 (void)set_monster_fast(creature_ptr, i, 0);
1217 (void)set_monster_slow(creature_ptr, i, 0);
1218 (void)set_monster_stunned(creature_ptr, i, 0);
1219 (void)set_monster_confused(creature_ptr, i, 0);
1220 (void)set_monster_monfear(creature_ptr, i, 0);
1221 (void)set_monster_invulner(creature_ptr, i, 0, FALSE);
1224 /* Extract real monster race */
1225 r_ptr = real_r_ptr(m_ptr);
1227 /* Ignore non-unique */
1228 if (!(r_ptr->flags1 & RF1_UNIQUE) &&
1229 !(r_ptr->flags7 & RF7_NAZGUL)) continue;
1231 /* Appear at a different floor? */
1232 if (r_ptr->floor_id != new_floor_id)
1234 /* Disapper from here */
1235 delete_monster_idx(creature_ptr, i);
1239 /* Maintain artifatcs */
1240 for (i = 1; i < creature_ptr->current_floor_ptr->o_max; i++)
1242 object_type *o_ptr = &creature_ptr->current_floor_ptr->o_list[i];
1244 if (!OBJECT_IS_VALID(o_ptr)) continue;
1246 /* Ignore non-artifact */
1247 if (!object_is_fixed_artifact(o_ptr)) continue;
1249 /* Appear at a different floor? */
1250 if (a_info[o_ptr->name1].floor_id != new_floor_id)
1252 /* Disappear from here */
1253 delete_object_idx(creature_ptr, i);
1257 /* Cancel preserve */
1258 a_info[o_ptr->name1].cur_num = 1;
1262 (void)place_quest_monsters(creature_ptr);
1264 /* Place some random monsters */
1265 alloc_times = absence_ticks / alloc_chance;
1267 if (randint0(alloc_chance) < (absence_ticks % alloc_chance))
1270 for (i = 0; i < alloc_times; i++)
1272 /* Make a (group of) new monster */
1273 (void)alloc_monster(creature_ptr, 0, 0);
1278 /* New floor_id or failed to restore */
1279 else /* if (!loaded) */
1281 if (sf_ptr->last_visit)
1283 /* Temporary file is broken? */
1284 msg_print(_("階段は行き止まりだった。", "The staircases come to a dead end..."));
1286 /* Create simple dead end */
1287 build_dead_end(creature_ptr);
1289 /* Break connection */
1290 if (creature_ptr->change_floor_mode & CFM_UP)
1292 sf_ptr->upper_floor_id = 0;
1294 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1296 sf_ptr->lower_floor_id = 0;
1301 generate_floor(creature_ptr);
1304 /* Record last visit turn */
1305 sf_ptr->last_visit = current_world_ptr->game_turn;
1307 /* Set correct dun_level value */
1308 sf_ptr->dun_level = creature_ptr->current_floor_ptr->dun_level;
1310 /* Create connected stairs */
1311 if (!(creature_ptr->change_floor_mode & CFM_NO_RETURN))
1313 /* Extract stair position */
1314 grid_type *g_ptr = &creature_ptr->current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
1316 /*** Create connected stairs ***/
1318 /* No stairs down from Quest */
1319 if ((creature_ptr->change_floor_mode & CFM_UP) && !quest_number(creature_ptr, creature_ptr->current_floor_ptr->dun_level))
1321 g_ptr->feat = (creature_ptr->change_floor_mode & CFM_SHAFT) ? feat_state(creature_ptr, feat_down_stair, FF_SHAFT) : feat_down_stair;
1324 /* No stairs up when ironman_downward */
1325 else if ((creature_ptr->change_floor_mode & CFM_DOWN) && !ironman_downward)
1327 g_ptr->feat = (creature_ptr->change_floor_mode & CFM_SHAFT) ? feat_state(creature_ptr, feat_up_stair, FF_SHAFT) : feat_up_stair;
1330 /* Paranoia -- Clear mimic */
1333 /* Connect to previous floor */
1334 g_ptr->special = creature_ptr->floor_id;
1338 /* Arrive at random grid */
1339 if (creature_ptr->change_floor_mode & (CFM_RAND_PLACE))
1341 (void)new_player_spot(creature_ptr);
1344 /* You see stairs blocked */
1345 else if ((creature_ptr->change_floor_mode & CFM_NO_RETURN) && (creature_ptr->change_floor_mode & (CFM_DOWN | CFM_UP)))
1347 if (!creature_ptr->blind)
1349 msg_print(_("突然階段が塞がれてしまった。", "Suddenly the stairs is blocked!"));
1353 msg_print(_("ゴトゴトと何か音がした。", "You hear some noises."));
1360 * The "turn" is not always different number because
1361 * the level teleport doesn't take any turn. Use
1362 * visit mark instead of last visit turn to find the
1363 * oldest saved floor.
1365 sf_ptr->visit_mark = latest_visit_mark++;
1368 /* Place preserved pet monsters */
1369 place_pet(creature_ptr);
1371 /* Reset travel target place */
1372 forget_travel_flow(creature_ptr->current_floor_ptr);
1374 /* Hack -- maintain unique and artifacts */
1375 update_unique_artifact(creature_ptr->current_floor_ptr, new_floor_id);
1377 /* Now the player is in new floor */
1378 creature_ptr->floor_id = new_floor_id;
1380 /* The dungeon is ready */
1381 current_world_ptr->character_dungeon = TRUE;
1383 /* Hack -- Munchkin characters always get whole map */
1384 if (creature_ptr->pseikaku == PERSONALITY_MUNCHKIN)
1385 wiz_lite(creature_ptr, (bool)(creature_ptr->pclass == CLASS_NINJA));
1387 /* Remember when this level was "created" */
1388 creature_ptr->current_floor_ptr->generated_turn = current_world_ptr->game_turn;
1390 /* No dungeon feeling yet */
1391 creature_ptr->feeling_turn = creature_ptr->current_floor_ptr->generated_turn;
1392 creature_ptr->feeling = 0;
1394 /* Clear all flags */
1395 creature_ptr->change_floor_mode = 0L;
1397 select_floor_music(creature_ptr);
1398 creature_ptr->change_floor_mode = 0;