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
20 #include "floor-events.h"
21 #include "floor-generate.h"
27 #include "spells-floor.h"
28 #include "monster-status.h"
29 #include "object-hook.h"
31 #include "cmd-basic.h"
33 #include "player-effects.h"
38 static FLOOR_IDX new_floor_id; /*!<次のフロアのID / floor_id of the destination */
39 static u32b change_floor_mode; /*!<フロア移行処理に関するフラグ / Mode flags for changing floor */
40 static u32b latest_visit_mark; /*!<フロアを渡った回数?(確認中) / Max number of visit_mark */
43 * Number of floor_id used from birth
45 FLOOR_IDX max_floor_id;
48 * @brief 保存フロア配列を初期化する / Initialize saved_floors array.
49 * @param force テンポラリファイルが残っていた場合も警告なしで強制的に削除する。
50 * @details Make sure that old temporal files are not remaining as gurbages.
53 void init_saved_floors(bool force)
55 char floor_savefile[1024];
58 BIT_FLAGS mode = 0644;
62 /* Get "games" permissions */
67 for (i = 0; i < MAX_SAVED_FLOORS; i++)
69 saved_floor_type *sf_ptr = &saved_floors[i];
72 sprintf(floor_savefile, "%s.F%02d", savefile, i);
74 /* Grab permissions */
77 /* Try to create the file */
78 fd = fd_make(floor_savefile, mode);
80 /* Drop permissions */
88 msg_print(_("エラー:古いテンポラリ・ファイルが残っています。", "Error: There are old temporal files."));
89 msg_print(_("変愚蛮怒を二重に起動していないか確認してください。", "Make sure you are not running two game processes simultaneously."));
90 msg_print(_("過去に変愚蛮怒がクラッシュした場合は一時ファイルを", "If the temporal files are garbages of old crashed process, "));
91 msg_print(_("強制的に削除して実行を続けられます。", "you can delete it safely."));
92 if (!get_check(_("強制的に削除してもよろしいですか?", "Do you delete old temporal files? ")))
93 quit(_("実行中止", "Aborted."));
103 /* Grab permissions */
106 /* Simply kill the temporal file */
107 (void)fd_kill(floor_savefile);
109 /* Drop permissions */
112 sf_ptr->floor_id = 0;
115 /* No floor_id used yet (No.0 is reserved to indicate non existance) */
118 /* vist_mark is from 1 */
119 latest_visit_mark = 1;
121 /* A sign to mark temporal files */
122 saved_floor_file_sign = (u32b)time(NULL);
124 /* No next floor yet */
127 /* No change floor mode yet */
128 change_floor_mode = 0;
132 /* Drop "games" permissions */
139 * @brief 保存フロア用テンポラリファイルを削除する / Kill temporal files
140 * @details Should be called just before the game quit.
143 void clear_saved_floor_files(void)
145 char floor_savefile[1024];
150 /* Get "games" permissions */
155 for (i = 0; i < MAX_SAVED_FLOORS; i++)
157 saved_floor_type *sf_ptr = &saved_floors[i];
159 /* No temporal file */
160 if (!sf_ptr->floor_id) continue;
161 if (sf_ptr->floor_id == p_ptr->floor_id) continue;
164 sprintf(floor_savefile, "%s.F%02d", savefile, i);
166 /* Grab permissions */
169 /* Simply kill the temporal file */
170 (void)fd_kill(floor_savefile);
172 /* Drop permissions */
178 /* Drop "games" permissions */
185 * @brief 保存フロアIDから参照ポインタを得る / Get a pointer for an item of the saved_floors array.
186 * @param floor_id 保存フロアID
187 * @return IDに対応する保存フロアのポインタ、ない場合はNULLを返す。
189 saved_floor_type *get_sf_ptr(FLOOR_IDX floor_id)
193 /* floor_id No.0 indicates no floor */
194 if (!floor_id) return NULL;
196 for (i = 0; i < MAX_SAVED_FLOORS; i++)
198 saved_floor_type *sf_ptr = &saved_floors[i];
200 if (sf_ptr->floor_id == floor_id) return sf_ptr;
209 * @brief 参照ポインタ先の保存フロアを抹消する / kill a saved floor and get an empty space
210 * @param sf_ptr 保存フロアの参照ポインタ
213 static void kill_saved_floor(saved_floor_type *sf_ptr)
215 char floor_savefile[1024];
219 if (!sf_ptr->floor_id) return;
221 if (sf_ptr->floor_id == p_ptr->floor_id)
223 /* Kill current floor */
226 /* Current floor doesn't have temporal file */
231 sprintf(floor_savefile, "%s.F%02d", savefile, (int)sf_ptr->savefile_id);
233 /* Grab permissions */
236 /* Simply kill the temporal file */
237 (void)fd_kill(floor_savefile);
239 /* Drop permissions */
243 /* No longer exists */
244 sf_ptr->floor_id = 0;
249 * @brief 新規に利用可能な保存フロアを返す / Initialize new saved floor and get its floor id.
250 * @return 利用可能な保存フロアID
252 * If number of saved floors are already MAX_SAVED_FLOORS, kill the oldest one.
254 FLOOR_IDX get_new_floor_id(void)
256 saved_floor_type *sf_ptr = NULL;
259 /* Look for empty space */
260 for (i = 0; i < MAX_SAVED_FLOORS; i++)
262 sf_ptr = &saved_floors[i];
264 if (!sf_ptr->floor_id) break;
268 if (i == MAX_SAVED_FLOORS)
271 u32b oldest_visit = 0xffffffffL;
273 /* Search for oldest */
274 for (i = 0; i < MAX_SAVED_FLOORS; i++)
276 sf_ptr = &saved_floors[i];
278 /* Don't kill current floor */
279 if (sf_ptr->floor_id == p_ptr->floor_id) continue;
281 /* Don't kill newer */
282 if (sf_ptr->visit_mark > oldest_visit) continue;
285 oldest_visit = sf_ptr->visit_mark;
288 /* Kill oldest saved floor */
289 sf_ptr = &saved_floors[oldest];
290 kill_saved_floor(sf_ptr);
296 /* Prepare new floor data */
297 sf_ptr->savefile_id = i;
298 sf_ptr->floor_id = max_floor_id;
299 sf_ptr->last_visit = 0;
300 sf_ptr->upper_floor_id = 0;
301 sf_ptr->lower_floor_id = 0;
302 sf_ptr->visit_mark = latest_visit_mark++;
304 /* sf_ptr->dun_level may be changed later */
305 sf_ptr->dun_level = current_floor_ptr->dun_level;
308 /* Increment number of floor_id */
309 if (max_floor_id < MAX_SHORT) max_floor_id++;
311 /* 32767 floor_ids are all used up! Re-use ancient IDs */
312 else max_floor_id = 1;
314 return sf_ptr->floor_id;
319 * @brief フロア切り替え時の処理フラグを追加する / Prepare mode flags of changing floor
320 * @param mode 追加したい所持フラグ
323 void prepare_change_floor_mode(BIT_FLAGS mode)
325 change_floor_mode |= mode;
329 * @brief 階段移動先のフロアが生成できない時に簡単な行き止まりマップを作成する / Builds the dead end
332 static void build_dead_end(void)
336 /* Clear and empty the current_floor_ptr->grid_array */
339 /* Fill the arrays of floors and walls in the good proportions */
340 set_floor_and_wall(0);
343 current_floor_ptr->height = SCREEN_HGT;
344 current_floor_ptr->width = SCREEN_WID;
346 /* Filled with permanent walls */
347 for (y = 0; y < MAX_HGT; y++)
349 for (x = 0; x < MAX_WID; x++)
351 /* Create "solid" perma-wall */
352 place_solid_perm_bold(y, x);
356 /* Place at center of the floor */
357 p_ptr->y = current_floor_ptr->height / 2;
358 p_ptr->x = current_floor_ptr->width / 2;
360 /* Give one square */
361 place_floor_bold(p_ptr->y, p_ptr->x);
363 wipe_generate_random_floor_flags();
368 #define MAX_PARTY_MON 21 /*!< フロア移動時に先のフロアに連れて行けるペットの最大数 Maximum number of preservable pets */
369 static monster_type party_mon[MAX_PARTY_MON]; /*!< フロア移動に保存するペットモンスターの配列 */
372 * @brief フロア移動時のペット保存処理 / Preserve_pets
375 static void preserve_pet(void)
380 for (num = 0; num < MAX_PARTY_MON; num++)
382 party_mon[num].r_idx = 0;
387 monster_type *m_ptr = ¤t_floor_ptr->m_list[p_ptr->riding];
389 /* Pet of other pet don't follow. */
390 if (m_ptr->parent_m_idx)
393 p_ptr->pet_extra_flags &= ~(PF_RYOUTE);
394 p_ptr->riding_ryoute = p_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(p_ptr->riding);
407 * If player is in wild mode, no pets are preserved
408 * except a monster whom player riding
410 if (!p_ptr->wild_mode && !p_ptr->inside_arena && !p_ptr->inside_battle)
412 for (i = current_floor_ptr->m_max - 1, num = 1; (i >= 1 && num < MAX_PARTY_MON); i--)
414 monster_type *m_ptr = ¤t_floor_ptr->m_list[i];
416 if (!monster_is_valid(m_ptr)) continue;
417 if (!is_pet(m_ptr)) continue;
418 if (i == p_ptr->riding) continue;
420 if (reinit_wilderness)
422 /* Don't lose sight of pets when getting a Quest */
426 POSITION dis = distance(p_ptr->y, p_ptr->x, m_ptr->fy, m_ptr->fx);
428 /* Confused (etc.) monsters don't follow. */
429 if (MON_CONFUSED(m_ptr) || MON_STUNNED(m_ptr) || MON_CSLEEP(m_ptr)) continue;
431 /* Pet of other pet don't follow. */
432 if (m_ptr->parent_m_idx) continue;
435 * Pets with nickname will follow even from 3 blocks away
436 * when you or the pet can see the other.
438 if (m_ptr->nickname &&
439 ((player_has_los_bold(m_ptr->fy, m_ptr->fx) && projectable(p_ptr->y, p_ptr->x, m_ptr->fy, m_ptr->fx)) ||
440 (los(m_ptr->fy, m_ptr->fx, p_ptr->y, p_ptr->x) && projectable(m_ptr->fy, m_ptr->fx, p_ptr->y, p_ptr->x))))
442 if (dis > 3) continue;
446 if (dis > 1) continue;
450 (void)COPY(&party_mon[num], ¤t_floor_ptr->m_list[i], monster_type);
454 /* Delete from this floor */
455 delete_monster_idx(i);
459 if (record_named_pet)
461 for (i = current_floor_ptr->m_max - 1; i >=1; i--)
463 monster_type *m_ptr = ¤t_floor_ptr->m_list[i];
464 GAME_TEXT m_name[MAX_NLEN];
466 if (!monster_is_valid(m_ptr)) continue;
467 if (!is_pet(m_ptr)) continue;
468 if (!m_ptr->nickname) continue;
469 if (p_ptr->riding == i) continue;
471 monster_desc(m_name, m_ptr, MD_ASSUME_VISIBLE | MD_INDEF_VISIBLE);
472 do_cmd_write_nikki(NIKKI_NAMED_PET, RECORD_NAMED_PET_MOVED, m_name);
477 /* Pet of other pet may disappear. */
478 for (i = current_floor_ptr->m_max - 1; i >=1; i--)
480 monster_type *m_ptr = ¤t_floor_ptr->m_list[i];
482 /* Are there its parent? */
483 if (m_ptr->parent_m_idx && !current_floor_ptr->m_list[m_ptr->parent_m_idx].r_idx)
485 /* Its parent have gone, it also goes away. */
489 GAME_TEXT m_name[MAX_NLEN];
490 monster_desc(m_name, m_ptr, 0);
491 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
494 delete_monster_idx(i);
501 * @brief フロア移動時にペットを伴った場合の準備処理 / Pre-calculate the racial counters of preserved pets
504 * To prevent multiple generation of unique monster who is the minion of player
506 void precalc_cur_num_of_pet(void)
510 int max_num = p_ptr->wild_mode ? 1 : MAX_PARTY_MON;
512 for (i = 0; i < max_num; i++)
514 m_ptr = &party_mon[i];
516 /* Skip empty monsters */
517 if (!monster_is_valid(m_ptr)) continue;
519 /* Hack -- Increase the racial counter */
520 real_r_ptr(m_ptr)->cur_num++;
525 * @brief 移動先のフロアに伴ったペットを配置する / Place preserved pet monsters on new floor
528 static void place_pet(void)
531 int max_num = p_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;
543 p_ptr->riding = m_idx;
555 for (d = 1; d < A_MAX; d++)
557 for (j = 1000; j > 0; j--)
559 scatter(&cy, &cx, p_ptr->y, p_ptr->x, d, 0);
560 if (monster_can_enter(cy, cx, &r_info[party_mon[i].r_idx], 0)) break;
564 m_idx = (d == 6) ? 0 : m_pop();
569 monster_type *m_ptr = ¤t_floor_ptr->m_list[m_idx];
572 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);
583 m_ptr->mtimed[MTIMED_CSLEEP] = 0;
584 m_ptr->hold_o_idx = 0;
587 if ((r_ptr->flags1 & RF1_FORCE_SLEEP) && !ironman_nightmare)
589 /* Monster is still being nice */
590 m_ptr->mflag |= (MFLAG_NICE);
592 /* Must repair monsters */
593 repair_monsters = TRUE;
595 update_monster(m_idx, TRUE);
598 /* Pre-calculated in precalc_cur_num_of_pet() */
599 /* r_ptr->cur_num++; */
601 /* Hack -- Count the number of "reproducers" */
602 if (r_ptr->flags2 & RF2_MULTIPLY) current_floor_ptr->num_repro++;
607 monster_type *m_ptr = &party_mon[i];
608 monster_race *r_ptr = real_r_ptr(m_ptr);
609 GAME_TEXT m_name[MAX_NLEN];
611 monster_desc(m_name, m_ptr, 0);
612 msg_format(_("%sとはぐれてしまった。", "You have lost sight of %s."), m_name);
613 if (record_named_pet && m_ptr->nickname)
615 monster_desc(m_name, m_ptr, MD_INDEF_VISIBLE);
616 do_cmd_write_nikki(NIKKI_NAMED_PET, RECORD_NAMED_PET_LOST_SIGHT, m_name);
619 /* Pre-calculated in precalc_cur_num_of_pet(), but need to decrease */
620 if (r_ptr->cur_num) r_ptr->cur_num--;
624 /* For accuracy of precalc_cur_num_of_pet() */
625 (void)C_WIPE(party_mon, MAX_PARTY_MON, monster_type);
630 * @brief ユニークモンスターやアーティファクトの所在フロアを更新する / Hack -- Update location of unique monsters and artifacts
631 * @param cur_floor_id 現在のフロアID
634 * The r_ptr->floor_id and a_ptr->floor_id are not updated correctly\n
635 * while new floor creation since dungeons may be re-created by\n
636 * auto-scum option.\n
638 static void update_unique_artifact(s16b cur_floor_id)
642 /* Maintain unique monsters */
643 for (i = 1; i < current_floor_ptr->m_max; i++)
646 monster_type *m_ptr = ¤t_floor_ptr->m_list[i];
648 if (!monster_is_valid(m_ptr)) continue;
650 /* Extract real monster race */
651 r_ptr = real_r_ptr(m_ptr);
653 /* Memorize location of the unique monster */
654 if ((r_ptr->flags1 & RF1_UNIQUE) ||
655 (r_ptr->flags7 & RF7_NAZGUL))
657 r_ptr->floor_id = cur_floor_id;
661 /* Maintain artifatcs */
662 for (i = 1; i < current_floor_ptr->o_max; i++)
664 object_type *o_ptr = ¤t_floor_ptr->o_list[i];
666 /* Skip dead objects */
667 if (!o_ptr->k_idx) 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(void)
686 POSITION oy = p_ptr->y;
687 POSITION ox = p_ptr->x;
688 MONSTER_IDX m_idx = current_floor_ptr->grid_array[oy][ox].m_idx;
690 /* Nothing to do if no monster */
693 /* Look until done */
698 /* Pick a (possibly illegal) location */
699 POSITION ny = rand_spread(oy, dis);
700 POSITION nx = rand_spread(ox, dis);
704 /* Stop after 1000 tries */
705 if (tries > 10000) return;
708 * Increase distance after doing enough tries
709 * compared to area of possible space
711 if (tries > 20 * dis * dis) dis++;
713 /* Ignore illegal locations */
714 if (!in_bounds(ny, nx)) continue;
716 /* Require "empty" floor space */
717 if (!cave_empty_bold(ny, nx)) continue;
719 /* Hack -- no teleport onto glyph of warding */
720 if (is_glyph_grid(¤t_floor_ptr->grid_array[ny][nx])) continue;
721 if (is_explosive_rune_grid(¤t_floor_ptr->grid_array[ny][nx])) continue;
723 /* ...nor onto the Pattern */
724 if (pattern_tile(ny, nx)) continue;
726 /*** It's a good place ***/
728 m_ptr = ¤t_floor_ptr->m_list[m_idx];
730 /* Update the old location */
731 current_floor_ptr->grid_array[oy][ox].m_idx = 0;
733 /* Update the new location */
734 current_floor_ptr->grid_array[ny][nx].m_idx = m_idx;
736 /* Move the monster */
740 /* No need to do update_monster() */
748 * @brief 新フロアに移動元フロアに繋がる階段を配置する / Virtually teleport onto the stairs that is connecting between two floors.
749 * @param sf_ptr 移動元の保存フロア構造体参照ポインタ
752 static void locate_connected_stairs(saved_floor_type *sf_ptr)
754 POSITION x, y, sx = 0, sy = 0;
755 POSITION x_table[20];
756 POSITION y_table[20];
760 /* Search usable stairs */
761 for (y = 0; y < current_floor_ptr->height; y++)
763 for (x = 0; x < current_floor_ptr->width; x++)
765 grid_type *g_ptr = ¤t_floor_ptr->grid_array[y][x];
766 feature_type *f_ptr = &f_info[g_ptr->feat];
769 if (change_floor_mode & CFM_UP)
771 if (have_flag(f_ptr->flags, FF_LESS) && have_flag(f_ptr->flags, FF_STAIRS) &&
772 !have_flag(f_ptr->flags, FF_SPECIAL))
776 /* Found fixed stairs? */
777 if (g_ptr->special &&
778 g_ptr->special == sf_ptr->upper_floor_id)
786 else if (change_floor_mode & CFM_DOWN)
788 if (have_flag(f_ptr->flags, FF_MORE) && have_flag(f_ptr->flags, FF_STAIRS) &&
789 !have_flag(f_ptr->flags, FF_SPECIAL))
793 /* Found fixed stairs */
794 if (g_ptr->special &&
795 g_ptr->special == sf_ptr->lower_floor_id)
805 if (have_flag(f_ptr->flags, FF_BLDG))
811 if (ok && (num < 20))
828 /* No stairs found! -- No return */
829 prepare_change_floor_mode(CFM_RAND_PLACE | CFM_NO_RETURN);
831 /* Mega Hack -- It's not the stairs you enter. Disable it. */
832 if (!feat_uses_special(current_floor_ptr->grid_array[p_ptr->y][p_ptr->x].feat)) current_floor_ptr->grid_array[p_ptr->y][p_ptr->x].special = 0;
836 /* Choose random one */
839 /* Point stair location */
840 p_ptr->y = y_table[i];
841 p_ptr->x = x_table[i];
846 * @brief 現在のフロアを離れるに伴って行なわれる保存処理
847 * / Maintain quest monsters, mark next floor_id at stairs, save current floor, and prepare to enter next floor.
850 void leave_floor(void)
852 grid_type *g_ptr = NULL;
854 saved_floor_type *sf_ptr;
855 MONRACE_IDX quest_r_idx = 0;
858 /* Preserve pets and prepare to take these to next floor */
861 /* Remove all mirrors without explosion */
862 remove_all_mirrors(FALSE);
864 if (p_ptr->special_defense & NINJA_S_STEALTH) set_superstealth(FALSE);
866 /* New floor is not yet prepared */
869 /* Temporary get a floor_id (for Arena) */
870 if (!p_ptr->floor_id &&
871 (change_floor_mode & CFM_SAVE_FLOORS) &&
872 !(change_floor_mode & CFM_NO_RETURN))
874 /* Get temporal floor_id */
875 p_ptr->floor_id = get_new_floor_id();
878 /* Search the quest monster index */
879 for (i = 0; i < max_q_idx; i++)
881 if ((quest[i].status == QUEST_STATUS_TAKEN) &&
882 ((quest[i].type == QUEST_TYPE_KILL_LEVEL) ||
883 (quest[i].type == QUEST_TYPE_RANDOM)) &&
884 (quest[i].level == current_floor_ptr->dun_level) &&
885 (p_ptr->dungeon_idx == quest[i].dungeon) &&
886 !(quest[i].flags & QUEST_FLAG_PRESET))
888 quest_r_idx = quest[i].r_idx;
892 /* Maintain quest monsters */
893 for (i = 1; i < current_floor_ptr->m_max; i++)
896 monster_type *m_ptr = ¤t_floor_ptr->m_list[i];
898 if (!monster_is_valid(m_ptr)) continue;
900 /* Only maintain quest monsters */
901 if (quest_r_idx != m_ptr->r_idx) continue;
903 /* Extract real monster race */
904 r_ptr = real_r_ptr(m_ptr);
906 /* Ignore unique monsters */
907 if ((r_ptr->flags1 & RF1_UNIQUE) ||
908 (r_ptr->flags7 & RF7_NAZGUL)) continue;
910 /* Delete non-unique quest monsters */
911 delete_monster_idx(i);
914 /* Check if there is a same item */
915 for (i = 0; i < INVEN_PACK; i++)
917 object_type *o_ptr = &p_ptr->inventory_list[i];
919 /* Skip dead objects */
920 if (!o_ptr->k_idx) continue;
922 /* Delete old memorized location of the artifact */
923 if (object_is_fixed_artifact(o_ptr))
925 a_info[o_ptr->name1].floor_id = 0;
929 /* Extract current floor info or NULL */
930 sf_ptr = get_sf_ptr(p_ptr->floor_id);
932 /* Choose random stairs */
933 if ((change_floor_mode & CFM_RAND_CONNECT) && p_ptr->floor_id)
935 locate_connected_stairs(sf_ptr);
938 /* Extract new dungeon level */
939 if (change_floor_mode & CFM_SAVE_FLOORS)
941 /* Extract stair position */
942 g_ptr = ¤t_floor_ptr->grid_array[p_ptr->y][p_ptr->x];
943 f_ptr = &f_info[g_ptr->feat];
945 /* Get back to old saved floor? */
946 if (g_ptr->special && !have_flag(f_ptr->flags, FF_SPECIAL) && get_sf_ptr(g_ptr->special))
948 /* Saved floor is exist. Use it. */
949 new_floor_id = g_ptr->special;
952 /* Mark shaft up/down */
953 if (have_flag(f_ptr->flags, FF_STAIRS) && have_flag(f_ptr->flags, FF_SHAFT))
955 prepare_change_floor_mode(CFM_SHAFT);
959 /* Climb up/down some sort of stairs */
960 if (change_floor_mode & (CFM_DOWN | CFM_UP))
964 /* Extract level movement number */
965 if (change_floor_mode & CFM_DOWN) move_num = 1;
966 else if (change_floor_mode & CFM_UP) move_num = -1;
968 /* Shafts are deeper than normal stairs */
969 if (change_floor_mode & CFM_SHAFT)
970 move_num += SGN(move_num);
972 /* Get out from or Enter the dungeon */
973 if (change_floor_mode & CFM_DOWN)
975 if (!current_floor_ptr->dun_level)
976 move_num = d_info[p_ptr->dungeon_idx].mindepth;
978 else if (change_floor_mode & CFM_UP)
980 if (current_floor_ptr->dun_level + move_num < d_info[p_ptr->dungeon_idx].mindepth)
981 move_num = -current_floor_ptr->dun_level;
984 current_floor_ptr->dun_level += move_num;
987 /* Leaving the dungeon to town */
988 if (!current_floor_ptr->dun_level && p_ptr->dungeon_idx)
990 p_ptr->leaving_dungeon = TRUE;
991 if (!vanilla_town && !lite_town)
993 p_ptr->wilderness_y = d_info[p_ptr->dungeon_idx].dy;
994 p_ptr->wilderness_x = d_info[p_ptr->dungeon_idx].dx;
996 p_ptr->recall_dungeon = p_ptr->dungeon_idx;
997 p_ptr->dungeon_idx = 0;
999 /* Reach to the surface -- Clear all saved floors */
1000 change_floor_mode &= ~CFM_SAVE_FLOORS;
1003 /* Kill some old saved floors */
1004 if (!(change_floor_mode & CFM_SAVE_FLOORS))
1006 /* Kill all saved floors */
1007 for (i = 0; i < MAX_SAVED_FLOORS; i++)
1008 kill_saved_floor(&saved_floors[i]);
1010 /* Reset visit_mark count */
1011 latest_visit_mark = 1;
1013 else if (change_floor_mode & CFM_NO_RETURN)
1015 /* Kill current floor */
1016 kill_saved_floor(sf_ptr);
1019 /* No current floor -- Left/Enter dungeon etc... */
1020 if (!p_ptr->floor_id)
1022 /* No longer need to save current floor */
1027 /* Mark next floor_id on the previous floor */
1031 new_floor_id = get_new_floor_id();
1033 /* Connect from here */
1034 if (g_ptr && !feat_uses_special(g_ptr->feat))
1036 g_ptr->special = new_floor_id;
1040 /* Fix connection -- level teleportation or trap door */
1041 if (change_floor_mode & CFM_RAND_CONNECT)
1043 if (change_floor_mode & CFM_UP)
1044 sf_ptr->upper_floor_id = new_floor_id;
1045 else if (change_floor_mode & CFM_DOWN)
1046 sf_ptr->lower_floor_id = new_floor_id;
1049 /* If you can return, you need to save previous floor */
1050 if ((change_floor_mode & CFM_SAVE_FLOORS) &&
1051 !(change_floor_mode & CFM_NO_RETURN))
1053 /* Get out of the my way! */
1056 /* Record the last visit current_world_ptr->game_turn of current floor */
1057 sf_ptr->last_visit = current_world_ptr->game_turn;
1063 /* Save current floor */
1064 if (!save_floor(sf_ptr, 0))
1066 /* Save failed -- No return */
1067 prepare_change_floor_mode(CFM_NO_RETURN);
1069 /* Kill current floor */
1070 kill_saved_floor(get_sf_ptr(p_ptr->floor_id));
1077 * @brief フロアの切り替え処理 / Enter new floor.
1080 * If the floor is an old saved floor, it will be\n
1081 * restored from the temporal file. If the floor is new one, new current_floor_ptr->grid_array\n
1082 * will be generated.\n
1084 void change_floor(void)
1086 saved_floor_type *sf_ptr;
1087 bool loaded = FALSE;
1089 /* The dungeon is not ready */
1090 current_world_ptr->character_dungeon = FALSE;
1092 /* No longer in the trap detecteded region */
1093 p_ptr->dtrap = FALSE;
1095 /* Mega-Hack -- no panel yet */
1101 /* Mega-Hack -- not ambushed on the wildness? */
1102 p_ptr->ambush_flag = FALSE;
1104 /* No saved floors (On the surface etc.) */
1105 if (!(change_floor_mode & CFM_SAVE_FLOORS) &&
1106 !(change_floor_mode & CFM_FIRST_FLOOR))
1108 /* Create current_floor_ptr->grid_array */
1109 generate_random_floor();
1111 /* Paranoia -- No new saved floor */
1115 /* In the dungeon */
1118 /* No floor_id yet */
1122 new_floor_id = get_new_floor_id();
1125 /* Pointer for infomations of new floor */
1126 sf_ptr = get_sf_ptr(new_floor_id);
1128 /* Try to restore old floor */
1129 if (sf_ptr->last_visit)
1131 /* Old saved floor is exist */
1132 if (load_floor(sf_ptr, 0))
1136 /* Forbid return stairs */
1137 if (change_floor_mode & CFM_NO_RETURN)
1139 grid_type *g_ptr = ¤t_floor_ptr->grid_array[p_ptr->y][p_ptr->x];
1141 if (!feat_uses_special(g_ptr->feat))
1143 if (change_floor_mode & (CFM_DOWN | CFM_UP))
1145 /* Reset to floor */
1146 g_ptr->feat = feat_ground_type[randint0(100)];
1156 * Set lower/upper_floor_id of new floor when the new
1157 * floor is right-above/right-under the current floor.
1159 * Stair creation/Teleport level/Trap door will take
1160 * you the same floor when you used it later again.
1162 if (p_ptr->floor_id)
1164 saved_floor_type *cur_sf_ptr = get_sf_ptr(p_ptr->floor_id);
1166 if (change_floor_mode & CFM_UP)
1168 /* New floor is right-above */
1169 if (cur_sf_ptr->upper_floor_id == new_floor_id)
1170 sf_ptr->lower_floor_id = p_ptr->floor_id;
1172 else if (change_floor_mode & CFM_DOWN)
1174 /* New floor is right-under */
1175 if (cur_sf_ptr->lower_floor_id == new_floor_id)
1176 sf_ptr->upper_floor_id = p_ptr->floor_id;
1180 /* Break connection to killed floor */
1183 if (change_floor_mode & CFM_UP)
1184 sf_ptr->lower_floor_id = 0;
1185 else if (change_floor_mode & CFM_DOWN)
1186 sf_ptr->upper_floor_id = 0;
1189 /* Maintain monsters and artifacts */
1193 GAME_TURN tmp_last_visit = sf_ptr->last_visit;
1194 GAME_TURN absence_ticks;
1195 int alloc_chance = d_info[p_ptr->dungeon_idx].max_m_alloc_chance;
1196 GAME_TURN alloc_times;
1198 while (tmp_last_visit > current_world_ptr->game_turn) tmp_last_visit -= TURNS_PER_TICK * TOWN_DAWN;
1199 absence_ticks = (current_world_ptr->game_turn - tmp_last_visit) / TURNS_PER_TICK;
1201 /* Maintain monsters */
1202 for (i = 1; i < current_floor_ptr->m_max; i++)
1204 monster_race *r_ptr;
1205 monster_type *m_ptr = ¤t_floor_ptr->m_list[i];
1207 if (!monster_is_valid(m_ptr)) continue;
1212 m_ptr->hp = m_ptr->maxhp = m_ptr->max_maxhp;
1214 /* Remove timed status (except MTIMED_CSLEEP) */
1215 (void)set_monster_fast(i, 0);
1216 (void)set_monster_slow(i, 0);
1217 (void)set_monster_stunned(i, 0);
1218 (void)set_monster_confused(i, 0);
1219 (void)set_monster_monfear(i, 0);
1220 (void)set_monster_invulner(i, 0, FALSE);
1223 /* Extract real monster race */
1224 r_ptr = real_r_ptr(m_ptr);
1226 /* Ignore non-unique */
1227 if (!(r_ptr->flags1 & RF1_UNIQUE) &&
1228 !(r_ptr->flags7 & RF7_NAZGUL)) continue;
1230 /* Appear at a different floor? */
1231 if (r_ptr->floor_id != new_floor_id)
1233 /* Disapper from here */
1234 delete_monster_idx(i);
1238 /* Maintain artifatcs */
1239 for (i = 1; i < current_floor_ptr->o_max; i++)
1241 object_type *o_ptr = ¤t_floor_ptr->o_list[i];
1243 /* Skip dead objects */
1244 if (!o_ptr->k_idx) 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(i);
1257 /* Cancel preserve */
1258 a_info[o_ptr->name1].cur_num = 1;
1262 (void)place_quest_monsters();
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(0, 0);
1278 /* New floor_id or failed to restore */
1279 else /* if (!loaded) */
1281 if (sf_ptr->last_visit)
1283 /* Temporal file is broken? */
1284 msg_print(_("階段は行き止まりだった。", "The staircases come to a dead end..."));
1286 /* Create simple dead end */
1289 /* Break connection */
1290 if (change_floor_mode & CFM_UP)
1292 sf_ptr->upper_floor_id = 0;
1294 else if (change_floor_mode & CFM_DOWN)
1296 sf_ptr->lower_floor_id = 0;
1301 /* Newly create current_floor_ptr->grid_array */
1302 generate_random_floor();
1305 /* Record last visit current_world_ptr->game_turn */
1306 sf_ptr->last_visit = current_world_ptr->game_turn;
1308 /* Set correct current_floor_ptr->dun_level value */
1309 sf_ptr->dun_level = current_floor_ptr->dun_level;
1311 /* Create connected stairs */
1312 if (!(change_floor_mode & CFM_NO_RETURN))
1314 /* Extract stair position */
1315 grid_type *g_ptr = ¤t_floor_ptr->grid_array[p_ptr->y][p_ptr->x];
1317 /*** Create connected stairs ***/
1319 /* No stairs down from Quest */
1320 if ((change_floor_mode & CFM_UP) && !quest_number(current_floor_ptr->dun_level))
1322 g_ptr->feat = (change_floor_mode & CFM_SHAFT) ? feat_state(feat_down_stair, FF_SHAFT) : feat_down_stair;
1325 /* No stairs up when ironman_downward */
1326 else if ((change_floor_mode & CFM_DOWN) && !ironman_downward)
1328 g_ptr->feat = (change_floor_mode & CFM_SHAFT) ? feat_state(feat_up_stair, FF_SHAFT) : feat_up_stair;
1331 /* Paranoia -- Clear mimic */
1334 /* Connect to previous floor */
1335 g_ptr->special = p_ptr->floor_id;
1339 /* Arrive at random grid */
1340 if (change_floor_mode & (CFM_RAND_PLACE))
1342 (void)new_player_spot();
1345 /* You see stairs blocked */
1346 else if ((change_floor_mode & CFM_NO_RETURN) && (change_floor_mode & (CFM_DOWN | CFM_UP)))
1350 msg_print(_("突然階段が塞がれてしまった。", "Suddenly the stairs is blocked!"));
1354 msg_print(_("ゴトゴトと何か音がした。", "You hear some noises."));
1361 * The "current_world_ptr->game_turn" is not always different number because
1362 * the level teleport doesn't take any current_world_ptr->game_turn. Use
1363 * visit mark instead of last visit current_world_ptr->game_turn to find the
1364 * oldest saved floor.
1366 sf_ptr->visit_mark = latest_visit_mark++;
1369 /* Place preserved pet monsters */
1372 /* Reset travel target place */
1373 forget_travel_flow();
1375 /* Hack -- maintain unique and artifacts */
1376 update_unique_artifact(new_floor_id);
1378 /* Now the player is in new floor */
1379 p_ptr->floor_id = new_floor_id;
1381 /* The dungeon is ready */
1382 current_world_ptr->character_dungeon = TRUE;
1384 /* Hack -- Munchkin characters always get whole map */
1385 if (p_ptr->pseikaku == SEIKAKU_MUNCHKIN)
1386 wiz_lite((bool)(p_ptr->pclass == CLASS_NINJA));
1388 /* Remember when this level was "created" */
1389 current_floor_ptr->generated_turn = current_world_ptr->game_turn;
1391 /* No dungeon feeling yet */
1392 p_ptr->feeling_turn = current_floor_ptr->generated_turn;
1395 /* Clear all flags */
1396 change_floor_mode = 0L;
1398 select_floor_music();