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/quest-monster-placer.h"
18 #include "dungeon/dungeon.h"
19 #include "dungeon/quest.h"
20 #include "floor/cave.h"
21 #include "floor/floor-events.h"
22 #include "floor/floor-generator.h"
23 #include "floor/floor-object.h"
24 #include "floor/floor.h"
25 #include "floor/geometry.h"
26 #include "floor/wild.h"
27 #include "game-option/birth-options.h"
28 #include "game-option/play-record-options.h"
29 #include "grid/feature.h"
30 #include "grid/grid.h"
31 #include "inventory/inventory-slot-types.h"
32 #include "io/files-util.h"
33 #include "io/uid-checker.h"
34 #include "io/write-diary.h"
35 #include "load/floor-loader.h"
36 #include "main/sound-of-music.h"
37 #include "mind/mind-mirror-master.h"
38 #include "mind/mind-ninja.h"
39 #include "monster-floor/monster-generator.h"
40 #include "monster-floor/monster-remover.h"
41 #include "monster-floor/monster-summon.h"
42 #include "monster-race/monster-race.h"
43 #include "monster-race/race-flags1.h"
44 #include "monster-race/race-flags2.h"
45 #include "monster-race/race-flags7.h"
46 #include "monster/monster-describer.h"
47 #include "monster/monster-description-types.h"
48 #include "monster/monster-flag-types.h"
49 #include "monster/monster-info.h"
50 #include "monster/monster-list.h"
51 #include "monster/monster-status.h"
52 #include "monster/monster-update.h"
53 #include "monster/smart-learn-types.h"
54 #include "object-hook/hook-checker.h"
55 #include "object-hook/hook-enchant.h"
56 #include "pet/pet-util.h"
57 #include "player/player-class.h"
58 #include "player/player-personalities-types.h"
59 #include "player/special-defense-types.h"
60 #include "save/floor-writer.h"
61 #include "spell-kind/spells-floor.h"
62 #include "system/artifact-type-definition.h"
63 #include "system/floor-type-definition.h"
64 #include "system/system-variables.h"
65 #include "util/angband-files.h"
66 #include "util/bit-flags-calculator.h"
67 #include "view/display-messages.h"
68 #include "window/main-window-util.h"
69 #include "world/world.h"
73 static FLOOR_IDX new_floor_id; /*!<次のフロアのID / floor_id of the destination */
74 static u32b latest_visit_mark; /*!<フロアを渡った回数?(確認中) / Max number of visit_mark */
75 #define MAX_PARTY_MON 21 /*!< フロア移動時に先のフロアに連れて行けるペットの最大数 Maximum number of preservable pets */
76 static monster_type party_mon[MAX_PARTY_MON]; /*!< フロア移動に保存するペットモンスターの配列 */
79 * Number of floor_id used from birth
81 FLOOR_IDX max_floor_id;
84 * Sign for current process used in temporary files.
85 * Actually it is the start time of current process.
87 u32b saved_floor_file_sign;
89 saved_floor_type saved_floors[MAX_SAVED_FLOORS];
92 * @brief 保存フロア配列を初期化する / Initialize saved_floors array.
93 * @param creature_ptr プレーヤーへの参照ポインタ
94 * @param force テンポラリファイルが残っていた場合も警告なしで強制的に削除するフラグ
95 * @details Make sure that old temporary files are not remaining as gurbages.
98 void init_saved_floors(player_type *creature_ptr, bool force)
100 char floor_savefile[1024];
103 BIT_FLAGS mode = 0644;
104 for (i = 0; i < MAX_SAVED_FLOORS; i++) {
105 saved_floor_type *sf_ptr = &saved_floors[i];
108 sprintf(floor_savefile, "%s.F%02d", savefile, i);
110 /* Grab permissions */
111 safe_setuid_grab(creature_ptr);
113 /* Try to create the file */
114 fd = fd_make(floor_savefile, mode);
116 /* Drop permissions */
122 msg_print(_("エラー:古いテンポラリ・ファイルが残っています。", "Error: There are old temporary files."));
123 msg_print(_("変愚蛮怒を二重に起動していないか確認してください。", "Make sure you are not running two game processes simultaneously."));
124 msg_print(_("過去に変愚蛮怒がクラッシュした場合は一時ファイルを", "If the temporary files are garbage from an old crashed process, "));
125 msg_print(_("強制的に削除して実行を続けられます。", "you can delete them safely."));
126 if (!get_check(_("強制的に削除してもよろしいですか?", "Do you delete the old temporary files? ")))
127 quit(_("実行中止", "Aborted."));
135 /* Grab permissions */
136 safe_setuid_grab(creature_ptr);
138 /* Simply kill the temporary file */
139 (void)fd_kill(floor_savefile);
141 /* Drop permissions */
144 sf_ptr->floor_id = 0;
147 /* No floor_id used yet (No.0 is reserved to indicate non existance) */
150 /* vist_mark is from 1 */
151 latest_visit_mark = 1;
153 /* A sign to mark temporary files */
154 saved_floor_file_sign = (u32b)time(NULL);
156 /* No next floor yet */
159 /* No change floor mode yet */
160 creature_ptr->change_floor_mode = 0;
164 * @brief 保存フロア用テンポラリファイルを削除する / Kill temporary files
165 * @details Should be called just before the game quit.
166 * @param creature_ptr プレーヤーへの参照ポインタ
169 void clear_saved_floor_files(player_type *creature_ptr)
171 char floor_savefile[1024];
173 for (i = 0; i < MAX_SAVED_FLOORS; i++) {
174 saved_floor_type *sf_ptr = &saved_floors[i];
176 /* No temporary file */
177 if (!sf_ptr->floor_id)
179 if (sf_ptr->floor_id == creature_ptr->floor_id)
183 sprintf(floor_savefile, "%s.F%02d", savefile, i);
185 /* Grab permissions */
186 safe_setuid_grab(creature_ptr);
188 /* Simply kill the temporary file */
189 (void)fd_kill(floor_savefile);
191 /* Drop permissions */
197 * @brief 保存フロアIDから参照ポインタを得る / Get a pointer for an item of the saved_floors array.
198 * @param floor_id 保存フロアID
199 * @return IDに対応する保存フロアのポインタ、ない場合はNULLを返す。
201 saved_floor_type *get_sf_ptr(FLOOR_IDX floor_id)
205 /* floor_id No.0 indicates no floor */
209 for (i = 0; i < MAX_SAVED_FLOORS; i++) {
210 saved_floor_type *sf_ptr = &saved_floors[i];
212 if (sf_ptr->floor_id == floor_id)
221 * @brief 参照ポインタ先の保存フロアを抹消する / kill a saved floor and get an empty space
222 * @param creature_ptr プレーヤーへの参照ポインタ
223 * @param sf_ptr 保存フロアの参照ポインタ
226 static void kill_saved_floor(player_type *creature_ptr, saved_floor_type *sf_ptr)
228 char floor_savefile[1024];
233 if (!sf_ptr->floor_id)
236 if (sf_ptr->floor_id == creature_ptr->floor_id) {
237 /* Kill current floor */
238 creature_ptr->floor_id = 0;
240 /* Current floor doesn't have temporary file */
243 sprintf(floor_savefile, "%s.F%02d", savefile, (int)sf_ptr->savefile_id);
245 /* Grab permissions */
246 safe_setuid_grab(creature_ptr);
248 /* Simply kill the temporary file */
249 (void)fd_kill(floor_savefile);
251 /* Drop permissions */
255 /* No longer exists */
256 sf_ptr->floor_id = 0;
260 * @brief 新規に利用可能な保存フロアを返す / Initialize new saved floor and get its floor id.
261 * @param creature_ptr プレーヤーへの参照ポインタ
262 * @return 利用可能な保存フロアID
264 * If number of saved floors are already MAX_SAVED_FLOORS, kill the oldest one.
266 FLOOR_IDX get_new_floor_id(player_type *creature_ptr)
268 saved_floor_type *sf_ptr = NULL;
271 /* Look for empty space */
272 for (i = 0; i < MAX_SAVED_FLOORS; i++) {
273 sf_ptr = &saved_floors[i];
275 if (!sf_ptr->floor_id)
280 if (i == MAX_SAVED_FLOORS) {
282 u32b oldest_visit = 0xffffffffL;
284 /* Search for oldest */
285 for (i = 0; i < MAX_SAVED_FLOORS; i++) {
286 sf_ptr = &saved_floors[i];
288 /* Don't kill current floor */
289 if (sf_ptr->floor_id == creature_ptr->floor_id)
292 /* Don't kill newer */
293 if (sf_ptr->visit_mark > oldest_visit)
297 oldest_visit = sf_ptr->visit_mark;
300 /* Kill oldest saved floor */
301 sf_ptr = &saved_floors[oldest];
302 kill_saved_floor(creature_ptr, sf_ptr);
308 /* Prepare new floor data */
309 sf_ptr->savefile_id = i;
310 sf_ptr->floor_id = max_floor_id;
311 sf_ptr->last_visit = 0;
312 sf_ptr->upper_floor_id = 0;
313 sf_ptr->lower_floor_id = 0;
314 sf_ptr->visit_mark = latest_visit_mark++;
316 /* sf_ptr->dun_level may be changed later */
317 sf_ptr->dun_level = creature_ptr->current_floor_ptr->dun_level;
319 /* Increment number of floor_id */
320 if (max_floor_id < MAX_SHORT)
323 /* 32767 floor_ids are all used up! Re-use ancient IDs */
327 return sf_ptr->floor_id;
331 * @brief フロア切り替え時の処理フラグを追加する / Prepare mode flags of changing floor
332 * @param creature_ptr プレーヤーへの参照ポインタ
333 * @param mode 追加したい所持フラグ
336 void prepare_change_floor_mode(player_type *creature_ptr, BIT_FLAGS mode) { creature_ptr->change_floor_mode |= mode; }
339 * @brief 階段移動先のフロアが生成できない時に簡単な行き止まりマップを作成する / Builds the dead end
342 static void build_dead_end(player_type *creature_ptr)
346 clear_cave(creature_ptr);
348 /* Mega-Hack -- no player yet */
349 creature_ptr->x = creature_ptr->y = 0;
351 /* Fill the arrays of floors and walls in the good proportions */
352 set_floor_and_wall(0);
355 creature_ptr->current_floor_ptr->height = SCREEN_HGT;
356 creature_ptr->current_floor_ptr->width = SCREEN_WID;
358 /* Filled with permanent walls */
359 for (y = 0; y < MAX_HGT; y++) {
360 for (x = 0; x < MAX_WID; x++) {
361 /* Create "solid" perma-wall */
362 place_bold(creature_ptr, y, x, GB_SOLID_PERM);
366 /* Place at center of the floor */
367 creature_ptr->y = creature_ptr->current_floor_ptr->height / 2;
368 creature_ptr->x = creature_ptr->current_floor_ptr->width / 2;
370 /* Give one square */
371 place_bold(creature_ptr, creature_ptr->y, creature_ptr->x, GB_FLOOR);
373 wipe_generate_random_floor_flags(creature_ptr->current_floor_ptr);
377 * @brief フロア移動時のペット保存処理 / Preserve_pets
378 * @param master_ptr プレーヤーへの参照ポインタ
381 static void preserve_pet(player_type *master_ptr)
386 for (num = 0; num < MAX_PARTY_MON; num++) {
387 party_mon[num].r_idx = 0;
390 if (master_ptr->riding) {
391 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[master_ptr->riding];
393 /* Pet of other pet don't follow. */
394 if (m_ptr->parent_m_idx) {
395 master_ptr->riding = 0;
396 master_ptr->pet_extra_flags &= ~(PF_TWO_HANDS);
397 master_ptr->riding_ryoute = master_ptr->old_riding_ryoute = FALSE;
399 /* Preserve the mount */
400 (void)COPY(&party_mon[0], m_ptr, monster_type);
402 /* Delete from this floor */
403 delete_monster_idx(master_ptr, master_ptr->riding);
408 * If player is in wild mode, no pets are preserved
409 * except a monster whom player riding
411 if (!master_ptr->wild_mode && !master_ptr->current_floor_ptr->inside_arena && !master_ptr->phase_out) {
412 for (i = master_ptr->current_floor_ptr->m_max - 1, num = 1; (i >= 1 && num < MAX_PARTY_MON); i--) {
413 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[i];
415 if (!monster_is_valid(m_ptr))
419 if (i == master_ptr->riding)
422 if (reinit_wilderness) {
423 /* Don't lose sight of pets when getting a Quest */
425 POSITION dis = distance(master_ptr->y, master_ptr->x, m_ptr->fy, m_ptr->fx);
427 /* Confused (etc.) monsters don't follow. */
428 if (monster_confused_remaining(m_ptr) || monster_stunned_remaining(m_ptr) || monster_csleep_remaining(m_ptr))
431 /* Pet of other pet don't follow. */
432 if (m_ptr->parent_m_idx)
436 * Pets with nickname will follow even from 3 blocks away
437 * when you or the pet can see the other.
440 && ((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))
441 || (los(master_ptr, m_ptr->fy, m_ptr->fx, master_ptr->y, master_ptr->x)
442 && projectable(master_ptr, m_ptr->fy, m_ptr->fx, master_ptr->y, master_ptr->x)))) {
451 (void)COPY(&party_mon[num], &master_ptr->current_floor_ptr->m_list[i], monster_type);
455 /* Delete from this floor */
456 delete_monster_idx(master_ptr, i);
460 if (record_named_pet) {
461 for (i = master_ptr->current_floor_ptr->m_max - 1; i >= 1; i--) {
462 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[i];
463 GAME_TEXT m_name[MAX_NLEN];
465 if (!monster_is_valid(m_ptr))
469 if (!m_ptr->nickname)
471 if (master_ptr->riding == i)
474 monster_desc(master_ptr, m_name, m_ptr, MD_ASSUME_VISIBLE | MD_INDEF_VISIBLE);
475 exe_write_diary(master_ptr, DIARY_NAMED_PET, RECORD_NAMED_PET_MOVED, m_name);
479 /* Pet of other pet may disappear. */
480 for (i = master_ptr->current_floor_ptr->m_max - 1; i >= 1; i--) {
481 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[i];
483 /* Are there its parent? */
484 if (m_ptr->parent_m_idx && !master_ptr->current_floor_ptr->m_list[m_ptr->parent_m_idx].r_idx) {
485 /* Its parent have gone, it also goes away. */
487 if (is_seen(master_ptr, m_ptr)) {
488 GAME_TEXT m_name[MAX_NLEN];
489 monster_desc(master_ptr, m_name, m_ptr, 0);
490 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
493 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++) {
512 m_ptr = &party_mon[i];
514 /* Skip empty monsters */
515 if (!monster_is_valid(m_ptr))
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;
532 floor_type *floor_ptr = master_ptr->current_floor_ptr;
533 for (i = 0; i < max_num; i++) {
534 POSITION cy = 0, cx = 0;
537 if (!(party_mon[i].r_idx))
541 m_idx = m_pop(floor_ptr);
542 master_ptr->riding = m_idx;
551 for (d = 1; d < A_MAX; d++) {
552 for (j = 1000; j > 0; j--) {
553 scatter(master_ptr, &cy, &cx, master_ptr->y, master_ptr->x, d, 0);
554 if (monster_can_enter(master_ptr, cy, cx, &r_info[party_mon[i].r_idx], 0))
560 m_idx = (d == 6) ? 0 : m_pop(floor_ptr);
564 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[m_idx];
567 master_ptr->current_floor_ptr->grid_array[cy][cx].m_idx = m_idx;
569 m_ptr->r_idx = party_mon[i].r_idx;
571 /* Copy all member of the structure */
572 *m_ptr = party_mon[i];
573 r_ptr = real_r_ptr(m_ptr);
577 m_ptr->current_floor_ptr = master_ptr->current_floor_ptr;
579 m_ptr->mtimed[MTIMED_CSLEEP] = 0;
580 m_ptr->hold_o_idx = 0;
583 if ((r_ptr->flags1 & RF1_FORCE_SLEEP) && !ironman_nightmare) {
584 /* Monster is still being nice */
585 m_ptr->mflag |= (MFLAG_NICE);
587 /* Must repair monsters */
588 repair_monsters = TRUE;
590 update_monster(master_ptr, m_idx, TRUE);
591 lite_spot(master_ptr, cy, cx);
593 /* Pre-calculated in precalc_cur_num_of_pet() */
594 /* r_ptr->cur_num++; */
596 /* Hack -- Count the number of "reproducers" */
597 if (r_ptr->flags2 & RF2_MULTIPLY)
598 master_ptr->current_floor_ptr->num_repro++;
601 monster_type *m_ptr = &party_mon[i];
602 monster_race *r_ptr = real_r_ptr(m_ptr);
603 GAME_TEXT m_name[MAX_NLEN];
605 monster_desc(master_ptr, m_name, m_ptr, 0);
606 msg_format(_("%sとはぐれてしまった。", "You have lost sight of %s."), m_name);
607 if (record_named_pet && m_ptr->nickname) {
608 monster_desc(master_ptr, m_name, m_ptr, MD_INDEF_VISIBLE);
609 exe_write_diary(master_ptr, DIARY_NAMED_PET, RECORD_NAMED_PET_LOST_SIGHT, m_name);
612 /* Pre-calculated in precalc_cur_num_of_pet(), but need to decrease */
618 /* For accuracy of precalc_cur_num_of_pet() */
619 (void)C_WIPE(party_mon, MAX_PARTY_MON, monster_type);
623 * @brief ユニークモンスターやアーティファクトの所在フロアを更新する / Hack -- Update location of unique monsters and artifacts
624 * @param cur_floor_id 現在のフロアID
627 * The r_ptr->floor_id and a_ptr->floor_id are not updated correctly\n
628 * while new floor creation since dungeons may be re-created by\n
629 * auto-scum option.\n
631 static void update_unique_artifact(floor_type *floor_ptr, s16b cur_floor_id)
635 /* Maintain unique monsters */
636 for (i = 1; i < floor_ptr->m_max; i++) {
638 monster_type *m_ptr = &floor_ptr->m_list[i];
640 if (!monster_is_valid(m_ptr))
643 /* Extract real monster race */
644 r_ptr = real_r_ptr(m_ptr);
646 /* Memorize location of the unique monster */
647 if ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)) {
648 r_ptr->floor_id = cur_floor_id;
652 /* Maintain artifatcs */
653 for (i = 1; i < floor_ptr->o_max; i++) {
654 object_type *o_ptr = &floor_ptr->o_list[i];
656 if (!object_is_valid(o_ptr))
659 /* Memorize location of the artifact */
660 if (object_is_fixed_artifact(o_ptr)) {
661 a_info[o_ptr->name1].floor_id = cur_floor_id;
667 * @brief フロア移動時、プレイヤーの移動先モンスターが既にいた場合ランダムな近隣に移動させる / When a monster is at a place where player will return,
670 static void get_out_monster(player_type *protected_ptr)
674 POSITION oy = protected_ptr->y;
675 POSITION ox = protected_ptr->x;
676 floor_type *floor_ptr = protected_ptr->current_floor_ptr;
677 MONSTER_IDX m_idx = floor_ptr->grid_array[oy][ox].m_idx;
679 /* Nothing to do if no monster */
683 /* Look until done */
687 /* Pick a (possibly illegal) location */
688 POSITION ny = rand_spread(oy, dis);
689 POSITION nx = rand_spread(ox, dis);
693 /* Stop after 1000 tries */
698 * Increase distance after doing enough tries
699 * compared to area of possible space
701 if (tries > 20 * dis * dis)
704 /* Ignore illegal locations */
705 if (!in_bounds(floor_ptr, ny, nx))
708 /* Require "empty" floor space */
709 if (!is_cave_empty_bold(protected_ptr, ny, nx))
712 /* Hack -- no teleport onto glyph of warding */
713 if (is_glyph_grid(&floor_ptr->grid_array[ny][nx]))
715 if (is_explosive_rune_grid(&floor_ptr->grid_array[ny][nx]))
718 /* ...nor onto the Pattern */
719 if (pattern_tile(floor_ptr, ny, nx))
722 /*** It's a good place ***/
724 m_ptr = &floor_ptr->m_list[m_idx];
726 /* Update the old location */
727 floor_ptr->grid_array[oy][ox].m_idx = 0;
729 /* Update the new location */
730 floor_ptr->grid_array[ny][nx].m_idx = m_idx;
732 /* Move the monster */
736 /* No need to do update_monster() */
744 * @brief 新フロアに移動元フロアに繋がる階段を配置する / Virtually teleport onto the stairs that is connecting between two floors.
745 * @param sf_ptr 移動元の保存フロア構造体参照ポインタ
748 static void locate_connected_stairs(player_type *creature_ptr, floor_type *floor_ptr, saved_floor_type *sf_ptr, BIT_FLAGS floor_mode)
750 POSITION x, y, sx = 0, sy = 0;
751 POSITION x_table[20];
752 POSITION y_table[20];
756 /* Search usable stairs */
757 for (y = 0; y < floor_ptr->height; y++) {
758 for (x = 0; x < floor_ptr->width; x++) {
759 grid_type *g_ptr = &floor_ptr->grid_array[y][x];
760 feature_type *f_ptr = &f_info[g_ptr->feat];
763 if (floor_mode & CFM_UP) {
764 if (have_flag(f_ptr->flags, FF_LESS) && have_flag(f_ptr->flags, FF_STAIRS) && !have_flag(f_ptr->flags, FF_SPECIAL)) {
767 /* Found fixed stairs? */
768 if (g_ptr->special && g_ptr->special == sf_ptr->upper_floor_id) {
775 else if (floor_mode & CFM_DOWN) {
776 if (have_flag(f_ptr->flags, FF_MORE) && have_flag(f_ptr->flags, FF_STAIRS) && !have_flag(f_ptr->flags, FF_SPECIAL)) {
779 /* Found fixed stairs */
780 if (g_ptr->special && g_ptr->special == sf_ptr->lower_floor_id) {
788 if (have_flag(f_ptr->flags, FF_BLDG)) {
793 if (ok && (num < 20)) {
803 creature_ptr->y = sy;
804 creature_ptr->x = sx;
806 /* No stairs found! -- No return */
807 prepare_change_floor_mode(creature_ptr, CFM_RAND_PLACE | CFM_NO_RETURN);
809 /* Mega Hack -- It's not the stairs you enter. Disable it. */
810 if (!feat_uses_special(floor_ptr->grid_array[creature_ptr->y][creature_ptr->x].feat))
811 floor_ptr->grid_array[creature_ptr->y][creature_ptr->x].special = 0;
813 /* Choose random one */
816 /* Point stair location */
817 creature_ptr->y = y_table[i];
818 creature_ptr->x = x_table[i];
823 * @brief 現在のフロアを離れるに伴って行なわれる保存処理
824 * / Maintain quest monsters, mark next floor_id at stairs, save current floor, and prepare to enter next floor.
825 * @param creature_ptr プレーヤーへの参照ポインタ
828 void leave_floor(player_type *creature_ptr)
830 grid_type *g_ptr = NULL;
832 saved_floor_type *sf_ptr;
833 MONRACE_IDX quest_r_idx = 0;
835 FLOOR_IDX tmp_floor_idx = 0;
837 /* Preserve pets and prepare to take these to next floor */
838 preserve_pet(creature_ptr);
840 /* Remove all mirrors without explosion */
841 remove_all_mirrors(creature_ptr, FALSE);
843 if (creature_ptr->special_defense & NINJA_S_STEALTH)
844 set_superstealth(creature_ptr, FALSE);
846 /* New floor is not yet prepared */
849 /* Temporarily get a floor_id (for Arena) */
850 if (!creature_ptr->floor_id && (creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) && !(creature_ptr->change_floor_mode & CFM_NO_RETURN)) {
851 /* Get temporary floor_id */
852 tmp_floor_idx = get_new_floor_id(creature_ptr);
855 /* Search the quest monster index */
856 for (i = 0; i < max_q_idx; i++) {
857 if ((quest[i].status == QUEST_STATUS_TAKEN) && ((quest[i].type == QUEST_TYPE_KILL_LEVEL) || (quest[i].type == QUEST_TYPE_RANDOM))
858 && (quest[i].level == creature_ptr->current_floor_ptr->dun_level) && (creature_ptr->dungeon_idx == quest[i].dungeon)
859 && !(quest[i].flags & QUEST_FLAG_PRESET)) {
860 quest_r_idx = quest[i].r_idx;
864 /* Maintain quest monsters */
865 for (i = 1; i < creature_ptr->current_floor_ptr->m_max; i++) {
867 monster_type *m_ptr = &creature_ptr->current_floor_ptr->m_list[i];
869 if (!monster_is_valid(m_ptr))
872 /* Only maintain quest monsters */
873 if (quest_r_idx != m_ptr->r_idx)
876 /* Extract real monster race */
877 r_ptr = real_r_ptr(m_ptr);
879 /* Ignore unique monsters */
880 if ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL))
883 /* Delete non-unique quest monsters */
884 delete_monster_idx(creature_ptr, i);
887 /* Check if there is a same item */
888 for (i = 0; i < INVEN_PACK; i++) {
889 object_type *o_ptr = &creature_ptr->inventory_list[i];
891 if (!object_is_valid(o_ptr))
894 /* Delete old memorized location of the artifact */
895 if (object_is_fixed_artifact(o_ptr)) {
896 a_info[o_ptr->name1].floor_id = 0;
900 /* Extract current floor info or NULL */
901 sf_ptr = get_sf_ptr(creature_ptr->floor_id);
903 /* Choose random stairs */
904 if ((creature_ptr->change_floor_mode & CFM_RAND_CONNECT) && tmp_floor_idx) {
905 locate_connected_stairs(creature_ptr, creature_ptr->current_floor_ptr, sf_ptr, creature_ptr->change_floor_mode);
908 /* Extract new dungeon level */
909 if (creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) {
910 /* Extract stair position */
911 g_ptr = &creature_ptr->current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
912 f_ptr = &f_info[g_ptr->feat];
914 /* Get back to old saved floor? */
915 if (g_ptr->special && !have_flag(f_ptr->flags, FF_SPECIAL) && get_sf_ptr(g_ptr->special)) {
916 /* Saved floor is exist. Use it. */
917 new_floor_id = g_ptr->special;
920 /* Mark shaft up/down */
921 if (have_flag(f_ptr->flags, FF_STAIRS) && have_flag(f_ptr->flags, FF_SHAFT)) {
922 prepare_change_floor_mode(creature_ptr, CFM_SHAFT);
926 /* Climb up/down some sort of stairs */
927 if (creature_ptr->change_floor_mode & (CFM_DOWN | CFM_UP)) {
930 /* Extract level movement number */
931 if (creature_ptr->change_floor_mode & CFM_DOWN)
933 else if (creature_ptr->change_floor_mode & CFM_UP)
936 /* Shafts are deeper than normal stairs */
937 if (creature_ptr->change_floor_mode & CFM_SHAFT)
938 move_num += SGN(move_num);
940 /* Get out from or Enter the dungeon */
941 if (creature_ptr->change_floor_mode & CFM_DOWN) {
942 if (!creature_ptr->current_floor_ptr->dun_level)
943 move_num = d_info[creature_ptr->dungeon_idx].mindepth;
944 } else if (creature_ptr->change_floor_mode & CFM_UP) {
945 if (creature_ptr->current_floor_ptr->dun_level + move_num < d_info[creature_ptr->dungeon_idx].mindepth)
946 move_num = -creature_ptr->current_floor_ptr->dun_level;
949 creature_ptr->current_floor_ptr->dun_level += move_num;
952 /* Leaving the dungeon to town */
953 if (!creature_ptr->current_floor_ptr->dun_level && creature_ptr->dungeon_idx) {
954 creature_ptr->leaving_dungeon = TRUE;
955 if (!vanilla_town && !lite_town) {
956 creature_ptr->wilderness_y = d_info[creature_ptr->dungeon_idx].dy;
957 creature_ptr->wilderness_x = d_info[creature_ptr->dungeon_idx].dx;
959 creature_ptr->recall_dungeon = creature_ptr->dungeon_idx;
960 creature_ptr->dungeon_idx = 0;
962 /* Reach to the surface -- Clear all saved floors */
963 creature_ptr->change_floor_mode &= ~CFM_SAVE_FLOORS; // TODO
966 /* Kill some old saved floors */
967 if (!(creature_ptr->change_floor_mode & CFM_SAVE_FLOORS)) {
968 /* Kill all saved floors */
969 for (i = 0; i < MAX_SAVED_FLOORS; i++)
970 kill_saved_floor(creature_ptr, &saved_floors[i]);
972 /* Reset visit_mark count */
973 latest_visit_mark = 1;
974 } else if (creature_ptr->change_floor_mode & CFM_NO_RETURN) {
975 /* Kill current floor */
976 kill_saved_floor(creature_ptr, sf_ptr);
979 /* No current floor -- Left/Enter dungeon etc... */
980 if (!creature_ptr->floor_id) {
981 /* No longer need to save current floor */
985 /* Mark next floor_id on the previous floor */
988 new_floor_id = get_new_floor_id(creature_ptr);
990 /* Connect from here */
991 if (g_ptr && !feat_uses_special(g_ptr->feat)) {
992 g_ptr->special = new_floor_id;
996 /* Fix connection -- level teleportation or trap door */
997 if (creature_ptr->change_floor_mode & CFM_RAND_CONNECT) {
998 if (creature_ptr->change_floor_mode & CFM_UP)
999 sf_ptr->upper_floor_id = new_floor_id;
1000 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1001 sf_ptr->lower_floor_id = new_floor_id;
1004 /* If you can return, you need to save previous floor */
1005 if ((creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) && !(creature_ptr->change_floor_mode & CFM_NO_RETURN)) {
1006 /* Get out of the my way! */
1007 get_out_monster(creature_ptr);
1009 /* Record the last visit turn of current floor */
1010 sf_ptr->last_visit = current_world_ptr->game_turn;
1012 forget_lite(creature_ptr->current_floor_ptr);
1013 forget_view(creature_ptr->current_floor_ptr);
1014 clear_mon_lite(creature_ptr->current_floor_ptr);
1016 /* Save current floor */
1017 if (!save_floor(creature_ptr, sf_ptr, 0)) {
1018 /* Save failed -- No return */
1019 prepare_change_floor_mode(creature_ptr, CFM_NO_RETURN);
1021 /* Kill current floor */
1022 kill_saved_floor(creature_ptr, get_sf_ptr(creature_ptr->floor_id));
1028 * @brief フロアの切り替え処理 / Enter new floor.
1029 * @param creature_ptr プレーヤーへの参照ポインタ
1032 * If the floor is an old saved floor, it will be\n
1033 * restored from the temporary file. If the floor is new one, new floor\n
1034 * will be generated.\n
1036 void change_floor(player_type *creature_ptr)
1038 saved_floor_type *sf_ptr;
1039 bool loaded = FALSE;
1041 /* The dungeon is not ready */
1042 current_world_ptr->character_dungeon = FALSE;
1044 /* No longer in the trap detecteded region */
1045 creature_ptr->dtrap = FALSE;
1047 /* Mega-Hack -- no panel yet */
1053 /* Mega-Hack -- not ambushed on the wildness? */
1054 creature_ptr->ambush_flag = FALSE;
1056 /* No saved floors (On the surface etc.) */
1057 if (!(creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) && !(creature_ptr->change_floor_mode & CFM_FIRST_FLOOR)) {
1058 generate_floor(creature_ptr);
1060 /* Paranoia -- No new saved floor */
1064 /* In the dungeon */
1066 /* No floor_id yet */
1067 if (!new_floor_id) {
1069 new_floor_id = get_new_floor_id(creature_ptr);
1072 /* Pointer for infomations of new floor */
1073 sf_ptr = get_sf_ptr(new_floor_id);
1075 /* Try to restore old floor */
1076 if (sf_ptr->last_visit) {
1077 /* Old saved floor is exist */
1078 if (load_floor(creature_ptr, sf_ptr, 0)) {
1081 /* Forbid return stairs */
1082 if (creature_ptr->change_floor_mode & CFM_NO_RETURN) {
1083 grid_type *g_ptr = &creature_ptr->current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
1085 if (!feat_uses_special(g_ptr->feat)) {
1086 if (creature_ptr->change_floor_mode & (CFM_DOWN | CFM_UP)) {
1087 /* Reset to floor */
1088 g_ptr->feat = feat_ground_type[randint0(100)];
1098 * Set lower/upper_floor_id of new floor when the new
1099 * floor is right-above/right-under the current floor.
1101 * Stair creation/Teleport level/Trap door will take
1102 * you the same floor when you used it later again.
1104 if (creature_ptr->floor_id) {
1105 saved_floor_type *cur_sf_ptr = get_sf_ptr(creature_ptr->floor_id);
1107 if (creature_ptr->change_floor_mode & CFM_UP) {
1108 /* New floor is right-above */
1109 if (cur_sf_ptr->upper_floor_id == new_floor_id)
1110 sf_ptr->lower_floor_id = creature_ptr->floor_id;
1111 } else if (creature_ptr->change_floor_mode & CFM_DOWN) {
1112 /* New floor is right-under */
1113 if (cur_sf_ptr->lower_floor_id == new_floor_id)
1114 sf_ptr->upper_floor_id = creature_ptr->floor_id;
1118 /* Break connection to killed floor */
1120 if (creature_ptr->change_floor_mode & CFM_UP)
1121 sf_ptr->lower_floor_id = 0;
1122 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1123 sf_ptr->upper_floor_id = 0;
1126 /* Maintain monsters and artifacts */
1129 GAME_TURN tmp_last_visit = sf_ptr->last_visit;
1130 GAME_TURN absence_ticks;
1131 int alloc_chance = d_info[creature_ptr->dungeon_idx].max_m_alloc_chance;
1132 GAME_TURN alloc_times;
1134 while (tmp_last_visit > current_world_ptr->game_turn)
1135 tmp_last_visit -= TURNS_PER_TICK * TOWN_DAWN;
1136 absence_ticks = (current_world_ptr->game_turn - tmp_last_visit) / TURNS_PER_TICK;
1138 /* Maintain monsters */
1139 for (i = 1; i < creature_ptr->current_floor_ptr->m_max; i++) {
1140 monster_race *r_ptr;
1141 monster_type *m_ptr = &creature_ptr->current_floor_ptr->m_list[i];
1143 if (!monster_is_valid(m_ptr))
1146 if (!is_pet(m_ptr)) {
1148 m_ptr->hp = m_ptr->maxhp = m_ptr->max_maxhp;
1150 /* Remove timed status (except MTIMED_CSLEEP) */
1151 (void)set_monster_fast(creature_ptr, i, 0);
1152 (void)set_monster_slow(creature_ptr, i, 0);
1153 (void)set_monster_stunned(creature_ptr, i, 0);
1154 (void)set_monster_confused(creature_ptr, i, 0);
1155 (void)set_monster_monfear(creature_ptr, i, 0);
1156 (void)set_monster_invulner(creature_ptr, i, 0, FALSE);
1159 /* Extract real monster race */
1160 r_ptr = real_r_ptr(m_ptr);
1162 /* Ignore non-unique */
1163 if (!(r_ptr->flags1 & RF1_UNIQUE) && !(r_ptr->flags7 & RF7_NAZGUL))
1166 /* Appear at a different floor? */
1167 if (r_ptr->floor_id != new_floor_id) {
1168 /* Disapper from here */
1169 delete_monster_idx(creature_ptr, i);
1173 /* Maintain artifatcs */
1174 for (i = 1; i < creature_ptr->current_floor_ptr->o_max; i++) {
1175 object_type *o_ptr = &creature_ptr->current_floor_ptr->o_list[i];
1177 if (!object_is_valid(o_ptr))
1180 /* Ignore non-artifact */
1181 if (!object_is_fixed_artifact(o_ptr))
1184 /* Appear at a different floor? */
1185 if (a_info[o_ptr->name1].floor_id != new_floor_id) {
1186 /* Disappear from here */
1187 delete_object_idx(creature_ptr, i);
1189 /* Cancel preserve */
1190 a_info[o_ptr->name1].cur_num = 1;
1194 (void)place_quest_monsters(creature_ptr);
1196 /* Place some random monsters */
1197 alloc_times = absence_ticks / alloc_chance;
1199 if (randint0(alloc_chance) < (absence_ticks % alloc_chance))
1202 for (i = 0; i < alloc_times; i++) {
1203 /* Make a (group of) new monster */
1204 (void)alloc_monster(creature_ptr, 0, 0, summon_specific);
1209 /* New floor_id or failed to restore */
1210 else /* if (!loaded) */
1212 if (sf_ptr->last_visit) {
1213 /* Temporary file is broken? */
1214 msg_print(_("階段は行き止まりだった。", "The staircases come to a dead end..."));
1216 /* Create simple dead end */
1217 build_dead_end(creature_ptr);
1219 /* Break connection */
1220 if (creature_ptr->change_floor_mode & CFM_UP) {
1221 sf_ptr->upper_floor_id = 0;
1222 } else if (creature_ptr->change_floor_mode & CFM_DOWN) {
1223 sf_ptr->lower_floor_id = 0;
1226 generate_floor(creature_ptr);
1229 /* Record last visit turn */
1230 sf_ptr->last_visit = current_world_ptr->game_turn;
1232 /* Set correct dun_level value */
1233 sf_ptr->dun_level = creature_ptr->current_floor_ptr->dun_level;
1235 /* Create connected stairs */
1236 if (!(creature_ptr->change_floor_mode & CFM_NO_RETURN)) {
1237 /* Extract stair position */
1238 grid_type *g_ptr = &creature_ptr->current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
1240 /*** Create connected stairs ***/
1242 /* No stairs down from Quest */
1243 if ((creature_ptr->change_floor_mode & CFM_UP) && !quest_number(creature_ptr, creature_ptr->current_floor_ptr->dun_level)) {
1244 g_ptr->feat = (creature_ptr->change_floor_mode & CFM_SHAFT) ? feat_state(creature_ptr, feat_down_stair, FF_SHAFT) : feat_down_stair;
1247 /* No stairs up when ironman_downward */
1248 else if ((creature_ptr->change_floor_mode & CFM_DOWN) && !ironman_downward) {
1249 g_ptr->feat = (creature_ptr->change_floor_mode & CFM_SHAFT) ? feat_state(creature_ptr, feat_up_stair, FF_SHAFT) : feat_up_stair;
1252 /* Paranoia -- Clear mimic */
1255 /* Connect to previous floor */
1256 g_ptr->special = creature_ptr->floor_id;
1260 /* Arrive at random grid */
1261 if (creature_ptr->change_floor_mode & (CFM_RAND_PLACE)) {
1262 (void)new_player_spot(creature_ptr);
1265 /* You see stairs blocked */
1266 else if ((creature_ptr->change_floor_mode & CFM_NO_RETURN) && (creature_ptr->change_floor_mode & (CFM_DOWN | CFM_UP))) {
1267 if (!creature_ptr->blind) {
1268 msg_print(_("突然階段が塞がれてしまった。", "Suddenly the stairs is blocked!"));
1270 msg_print(_("ゴトゴトと何か音がした。", "You hear some noises."));
1277 * The "turn" is not always different number because
1278 * the level teleport doesn't take any turn. Use
1279 * visit mark instead of last visit turn to find the
1280 * oldest saved floor.
1282 sf_ptr->visit_mark = latest_visit_mark++;
1285 /* Place preserved pet monsters */
1286 place_pet(creature_ptr);
1288 /* Reset travel target place */
1289 forget_travel_flow(creature_ptr->current_floor_ptr);
1291 /* Hack -- maintain unique and artifacts */
1292 update_unique_artifact(creature_ptr->current_floor_ptr, new_floor_id);
1294 /* Now the player is in new floor */
1295 creature_ptr->floor_id = new_floor_id;
1297 /* The dungeon is ready */
1298 current_world_ptr->character_dungeon = TRUE;
1300 /* Hack -- Munchkin characters always get whole map */
1301 if (creature_ptr->pseikaku == PERSONALITY_MUNCHKIN)
1302 wiz_lite(creature_ptr, (bool)(creature_ptr->pclass == CLASS_NINJA));
1304 /* Remember when this level was "created" */
1305 creature_ptr->current_floor_ptr->generated_turn = current_world_ptr->game_turn;
1307 /* No dungeon feeling yet */
1308 creature_ptr->feeling_turn = creature_ptr->current_floor_ptr->generated_turn;
1309 creature_ptr->feeling = 0;
1311 /* Clear all flags */
1312 creature_ptr->change_floor_mode = 0L;
1314 select_floor_music(creature_ptr);
1315 creature_ptr->change_floor_mode = 0;