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
14 #include "market/building.h"
22 #include "floor-save.h"
23 #include "floor-events.h"
24 #include "floor-generate.h"
31 #include "spells-floor.h"
32 #include "monster-status.h"
33 #include "object-hook.h"
35 #include "cmd-basic.h"
36 #include "io/uid-checker.h"
38 #include "player-effects.h"
39 #include "player-class.h"
40 #include "player-personality.h"
43 #include "io/write-diary.h"
44 #include "cmd/cmd-dump.h"
47 #include "view/display-main-window.h"
49 static FLOOR_IDX new_floor_id; /*!<次のフロアのID / floor_id of the destination */
50 static u32b latest_visit_mark; /*!<フロアを渡った回数?(確認中) / Max number of visit_mark */
51 #define MAX_PARTY_MON 21 /*!< フロア移動時に先のフロアに連れて行けるペットの最大数 Maximum number of preservable pets */
52 static monster_type party_mon[MAX_PARTY_MON]; /*!< フロア移動に保存するペットモンスターの配列 */
55 * Number of floor_id used from birth
57 FLOOR_IDX max_floor_id;
60 * Sign for current process used in temporary files.
61 * Actually it is the start time of current process.
63 u32b saved_floor_file_sign;
66 * @brief 保存フロア配列を初期化する / Initialize saved_floors array.
67 * @param creature_ptr プレーヤーへの参照ポインタ
68 * @param force テンポラリファイルが残っていた場合も警告なしで強制的に削除するフラグ
69 * @details Make sure that old temporary files are not remaining as gurbages.
72 void init_saved_floors(player_type *creature_ptr, bool force)
74 char floor_savefile[1024];
77 BIT_FLAGS mode = 0644;
78 for (i = 0; i < MAX_SAVED_FLOORS; i++)
80 saved_floor_type *sf_ptr = &saved_floors[i];
83 sprintf(floor_savefile, "%s.F%02d", savefile, i);
85 /* Grab permissions */
88 /* Try to create the file */
89 fd = fd_make(floor_savefile, mode);
91 /* Drop permissions */
99 msg_print(_("エラー:古いテンポラリ・ファイルが残っています。", "Error: There are old temporary files."));
100 msg_print(_("変愚蛮怒を二重に起動していないか確認してください。", "Make sure you are not running two game processes simultaneously."));
101 msg_print(_("過去に変愚蛮怒がクラッシュした場合は一時ファイルを", "If the temporary files are garbage from an old crashed process, "));
102 msg_print(_("強制的に削除して実行を続けられます。", "you can delete them safely."));
103 if (!get_check(_("強制的に削除してもよろしいですか?", "Do you delete the old temporary files? ")))
104 quit(_("実行中止", "Aborted."));
114 /* Grab permissions */
117 /* Simply kill the temporary file */
118 (void)fd_kill(floor_savefile);
120 /* Drop permissions */
123 sf_ptr->floor_id = 0;
126 /* No floor_id used yet (No.0 is reserved to indicate non existance) */
129 /* vist_mark is from 1 */
130 latest_visit_mark = 1;
132 /* A sign to mark temporary files */
133 saved_floor_file_sign = (u32b)time(NULL);
135 /* No next floor yet */
138 /* No change floor mode yet */
139 creature_ptr->change_floor_mode = 0;
143 * @brief 保存フロア用テンポラリファイルを削除する / Kill temporary files
144 * @details Should be called just before the game quit.
145 * @param creature_ptr プレーヤーへの参照ポインタ
148 void clear_saved_floor_files(player_type *creature_ptr)
150 char floor_savefile[1024];
152 for (i = 0; i < MAX_SAVED_FLOORS; i++)
154 saved_floor_type *sf_ptr = &saved_floors[i];
156 /* No temporary file */
157 if (!sf_ptr->floor_id) continue;
158 if (sf_ptr->floor_id == creature_ptr->floor_id) continue;
161 sprintf(floor_savefile, "%s.F%02d", savefile, i);
163 /* Grab permissions */
166 /* Simply kill the temporary file */
167 (void)fd_kill(floor_savefile);
169 /* Drop permissions */
175 * @brief 保存フロアIDから参照ポインタを得る / Get a pointer for an item of the saved_floors array.
176 * @param floor_id 保存フロアID
177 * @return IDに対応する保存フロアのポインタ、ない場合はNULLを返す。
179 saved_floor_type *get_sf_ptr(FLOOR_IDX floor_id)
183 /* floor_id No.0 indicates no floor */
184 if (!floor_id) return NULL;
186 for (i = 0; i < MAX_SAVED_FLOORS; i++)
188 saved_floor_type *sf_ptr = &saved_floors[i];
190 if (sf_ptr->floor_id == floor_id) return sf_ptr;
199 * @brief 参照ポインタ先の保存フロアを抹消する / kill a saved floor and get an empty space
200 * @param creature_ptr プレーヤーへの参照ポインタ
201 * @param sf_ptr 保存フロアの参照ポインタ
204 static void kill_saved_floor(player_type *creature_ptr, saved_floor_type *sf_ptr)
206 char floor_savefile[1024];
210 if (!sf_ptr->floor_id) return;
212 if (sf_ptr->floor_id == creature_ptr->floor_id)
214 /* Kill current floor */
215 creature_ptr->floor_id = 0;
217 /* Current floor doesn't have temporary file */
222 sprintf(floor_savefile, "%s.F%02d", savefile, (int)sf_ptr->savefile_id);
224 /* Grab permissions */
227 /* Simply kill the temporary file */
228 (void)fd_kill(floor_savefile);
230 /* Drop permissions */
234 /* No longer exists */
235 sf_ptr->floor_id = 0;
240 * @brief 新規に利用可能な保存フロアを返す / Initialize new saved floor and get its floor id.
241 * @param creature_ptr プレーヤーへの参照ポインタ
242 * @return 利用可能な保存フロアID
244 * If number of saved floors are already MAX_SAVED_FLOORS, kill the oldest one.
246 FLOOR_IDX get_new_floor_id(player_type *creature_ptr)
248 saved_floor_type *sf_ptr = NULL;
251 /* Look for empty space */
252 for (i = 0; i < MAX_SAVED_FLOORS; i++)
254 sf_ptr = &saved_floors[i];
256 if (!sf_ptr->floor_id) break;
260 if (i == MAX_SAVED_FLOORS)
263 u32b oldest_visit = 0xffffffffL;
265 /* Search for oldest */
266 for (i = 0; i < MAX_SAVED_FLOORS; i++)
268 sf_ptr = &saved_floors[i];
270 /* Don't kill current floor */
271 if (sf_ptr->floor_id == creature_ptr->floor_id) continue;
273 /* Don't kill newer */
274 if (sf_ptr->visit_mark > oldest_visit) continue;
277 oldest_visit = sf_ptr->visit_mark;
280 /* Kill oldest saved floor */
281 sf_ptr = &saved_floors[oldest];
282 kill_saved_floor(creature_ptr, sf_ptr);
288 /* Prepare new floor data */
289 sf_ptr->savefile_id = i;
290 sf_ptr->floor_id = max_floor_id;
291 sf_ptr->last_visit = 0;
292 sf_ptr->upper_floor_id = 0;
293 sf_ptr->lower_floor_id = 0;
294 sf_ptr->visit_mark = latest_visit_mark++;
296 /* sf_ptr->dun_level may be changed later */
297 sf_ptr->dun_level = creature_ptr->current_floor_ptr->dun_level;
300 /* Increment number of floor_id */
301 if (max_floor_id < MAX_SHORT) max_floor_id++;
303 /* 32767 floor_ids are all used up! Re-use ancient IDs */
304 else max_floor_id = 1;
306 return sf_ptr->floor_id;
311 * @brief フロア切り替え時の処理フラグを追加する / Prepare mode flags of changing floor
312 * @param creature_ptr プレーヤーへの参照ポインタ
313 * @param mode 追加したい所持フラグ
316 void prepare_change_floor_mode(player_type *creature_ptr, BIT_FLAGS mode)
318 creature_ptr->change_floor_mode |= mode;
322 * @brief 階段移動先のフロアが生成できない時に簡単な行き止まりマップを作成する / Builds the dead end
325 static void build_dead_end(player_type *creature_ptr)
329 clear_cave(creature_ptr);
331 /* Mega-Hack -- no player yet */
332 creature_ptr->x = creature_ptr->y = 0;
334 /* Fill the arrays of floors and walls in the good proportions */
335 set_floor_and_wall(0);
338 creature_ptr->current_floor_ptr->height = SCREEN_HGT;
339 creature_ptr->current_floor_ptr->width = SCREEN_WID;
341 /* Filled with permanent walls */
342 for (y = 0; y < MAX_HGT; y++)
344 for (x = 0; x < MAX_WID; x++)
346 /* Create "solid" perma-wall */
347 place_bold(creature_ptr, y, x, GB_SOLID_PERM);
351 /* Place at center of the floor */
352 creature_ptr->y = creature_ptr->current_floor_ptr->height / 2;
353 creature_ptr->x = creature_ptr->current_floor_ptr->width / 2;
355 /* Give one square */
356 place_bold(creature_ptr, creature_ptr->y, creature_ptr->x, GB_FLOOR);
358 wipe_generate_random_floor_flags(creature_ptr->current_floor_ptr);
363 * @brief フロア移動時のペット保存処理 / Preserve_pets
364 * @param master_ptr プレーヤーへの参照ポインタ
367 static void preserve_pet(player_type *master_ptr)
372 for (num = 0; num < MAX_PARTY_MON; num++)
374 party_mon[num].r_idx = 0;
377 if (master_ptr->riding)
379 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[master_ptr->riding];
381 /* Pet of other pet don't follow. */
382 if (m_ptr->parent_m_idx)
384 master_ptr->riding = 0;
385 master_ptr->pet_extra_flags &= ~(PF_TWO_HANDS);
386 master_ptr->riding_ryoute = master_ptr->old_riding_ryoute = FALSE;
390 /* Preserve the mount */
391 (void)COPY(&party_mon[0], m_ptr, monster_type);
393 /* Delete from this floor */
394 delete_monster_idx(master_ptr, master_ptr->riding);
399 * If player is in wild mode, no pets are preserved
400 * except a monster whom player riding
402 if (!master_ptr->wild_mode && !master_ptr->current_floor_ptr->inside_arena && !master_ptr->phase_out)
404 for (i = master_ptr->current_floor_ptr->m_max - 1, num = 1; (i >= 1 && num < MAX_PARTY_MON); i--)
406 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[i];
408 if (!monster_is_valid(m_ptr)) continue;
409 if (!is_pet(m_ptr)) continue;
410 if (i == master_ptr->riding) continue;
412 if (reinit_wilderness)
414 /* Don't lose sight of pets when getting a Quest */
418 POSITION dis = distance(master_ptr->y, master_ptr->x, m_ptr->fy, m_ptr->fx);
420 /* Confused (etc.) monsters don't follow. */
421 if (MON_CONFUSED(m_ptr) || MON_STUNNED(m_ptr) || MON_CSLEEP(m_ptr)) continue;
423 /* Pet of other pet don't follow. */
424 if (m_ptr->parent_m_idx) continue;
427 * Pets with nickname will follow even from 3 blocks away
428 * when you or the pet can see the other.
430 if (m_ptr->nickname &&
431 ((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)) ||
432 (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))))
434 if (dis > 3) continue;
438 if (dis > 1) continue;
442 (void)COPY(&party_mon[num], &master_ptr->current_floor_ptr->m_list[i], monster_type);
446 /* Delete from this floor */
447 delete_monster_idx(master_ptr, i);
451 if (record_named_pet)
453 for (i = master_ptr->current_floor_ptr->m_max - 1; i >=1; i--)
455 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[i];
456 GAME_TEXT m_name[MAX_NLEN];
458 if (!monster_is_valid(m_ptr)) continue;
459 if (!is_pet(m_ptr)) continue;
460 if (!m_ptr->nickname) continue;
461 if (master_ptr->riding == i) continue;
463 monster_desc(master_ptr, m_name, m_ptr, MD_ASSUME_VISIBLE | MD_INDEF_VISIBLE);
464 exe_write_diary(master_ptr, DIARY_NAMED_PET, RECORD_NAMED_PET_MOVED, m_name);
469 /* Pet of other pet may disappear. */
470 for (i = master_ptr->current_floor_ptr->m_max - 1; i >=1; i--)
472 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[i];
474 /* Are there its parent? */
475 if (m_ptr->parent_m_idx && !master_ptr->current_floor_ptr->m_list[m_ptr->parent_m_idx].r_idx)
477 /* Its parent have gone, it also goes away. */
481 GAME_TEXT m_name[MAX_NLEN];
482 monster_desc(master_ptr, m_name, m_ptr, 0);
483 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
486 delete_monster_idx(master_ptr, i);
493 * @brief フロア移動時にペットを伴った場合の準備処理 / Pre-calculate the racial counters of preserved pets
494 * @param master_ptr プレーヤーへの参照ポインタ
497 * To prevent multiple generation of unique monster who is the minion of player
499 void precalc_cur_num_of_pet(player_type *player_ptr)
503 int max_num = player_ptr->wild_mode ? 1 : MAX_PARTY_MON;
505 for (i = 0; i < max_num; i++)
507 m_ptr = &party_mon[i];
509 /* Skip empty monsters */
510 if (!monster_is_valid(m_ptr)) continue;
512 /* Hack -- Increase the racial counter */
513 real_r_ptr(m_ptr)->cur_num++;
518 * @brief 移動先のフロアに伴ったペットを配置する / Place preserved pet monsters on new floor
519 * @param master_ptr プレーヤーへの参照ポインタ
522 static void place_pet(player_type *master_ptr)
525 int max_num = master_ptr->wild_mode ? 1 : MAX_PARTY_MON;
527 for (i = 0; i < max_num; i++)
529 POSITION cy = 0, cx = 0;
532 if (!(party_mon[i].r_idx)) continue;
536 m_idx = m_pop(master_ptr);
537 master_ptr->riding = m_idx;
549 for (d = 1; d < A_MAX; d++)
551 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)) break;
558 m_idx = (d == 6) ? 0 : m_pop(master_ptr);
563 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[m_idx];
566 master_ptr->current_floor_ptr->grid_array[cy][cx].m_idx = m_idx;
568 m_ptr->r_idx = party_mon[i].r_idx;
570 /* Copy all member of the structure */
571 *m_ptr = party_mon[i];
572 r_ptr = real_r_ptr(m_ptr);
576 m_ptr->current_floor_ptr = master_ptr->current_floor_ptr;
578 m_ptr->mtimed[MTIMED_CSLEEP] = 0;
579 m_ptr->hold_o_idx = 0;
582 if ((r_ptr->flags1 & RF1_FORCE_SLEEP) && !ironman_nightmare)
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) master_ptr->current_floor_ptr->num_repro++;
602 monster_type *m_ptr = &party_mon[i];
603 monster_race *r_ptr = real_r_ptr(m_ptr);
604 GAME_TEXT m_name[MAX_NLEN];
606 monster_desc(master_ptr, m_name, m_ptr, 0);
607 msg_format(_("%sとはぐれてしまった。", "You have lost sight of %s."), m_name);
608 if (record_named_pet && m_ptr->nickname)
610 monster_desc(master_ptr, m_name, m_ptr, MD_INDEF_VISIBLE);
611 exe_write_diary(master_ptr, DIARY_NAMED_PET, RECORD_NAMED_PET_LOST_SIGHT, m_name);
614 /* Pre-calculated in precalc_cur_num_of_pet(), but need to decrease */
615 if (r_ptr->cur_num) r_ptr->cur_num--;
619 /* For accuracy of precalc_cur_num_of_pet() */
620 (void)C_WIPE(party_mon, MAX_PARTY_MON, monster_type);
625 * @brief ユニークモンスターやアーティファクトの所在フロアを更新する / Hack -- Update location of unique monsters and artifacts
626 * @param cur_floor_id 現在のフロアID
629 * The r_ptr->floor_id and a_ptr->floor_id are not updated correctly\n
630 * while new floor creation since dungeons may be re-created by\n
631 * auto-scum option.\n
633 static void update_unique_artifact(floor_type *floor_ptr, s16b cur_floor_id)
637 /* Maintain unique monsters */
638 for (i = 1; i < floor_ptr->m_max; i++)
641 monster_type *m_ptr = &floor_ptr->m_list[i];
643 if (!monster_is_valid(m_ptr)) continue;
645 /* Extract real monster race */
646 r_ptr = real_r_ptr(m_ptr);
648 /* Memorize location of the unique monster */
649 if ((r_ptr->flags1 & RF1_UNIQUE) ||
650 (r_ptr->flags7 & RF7_NAZGUL))
652 r_ptr->floor_id = cur_floor_id;
656 /* Maintain artifatcs */
657 for (i = 1; i < floor_ptr->o_max; i++)
659 object_type *o_ptr = &floor_ptr->o_list[i];
661 if (!OBJECT_IS_VALID(o_ptr)) continue;
663 /* Memorize location of the artifact */
664 if (object_is_fixed_artifact(o_ptr))
666 a_info[o_ptr->name1].floor_id = cur_floor_id;
673 * @brief フロア移動時、プレイヤーの移動先モンスターが既にいた場合ランダムな近隣に移動させる / When a monster is at a place where player will return,
676 static void get_out_monster(player_type *protected_ptr)
680 POSITION oy = protected_ptr->y;
681 POSITION ox = protected_ptr->x;
682 floor_type *floor_ptr = protected_ptr->current_floor_ptr;
683 MONSTER_IDX m_idx = floor_ptr->grid_array[oy][ox].m_idx;
685 /* Nothing to do if no monster */
688 /* Look until done */
693 /* Pick a (possibly illegal) location */
694 POSITION ny = rand_spread(oy, dis);
695 POSITION nx = rand_spread(ox, dis);
699 /* Stop after 1000 tries */
700 if (tries > 10000) return;
703 * Increase distance after doing enough tries
704 * compared to area of possible space
706 if (tries > 20 * dis * dis) dis++;
708 /* Ignore illegal locations */
709 if (!in_bounds(floor_ptr, ny, nx)) continue;
711 /* Require "empty" floor space */
712 if (!is_cave_empty_bold(protected_ptr, ny, nx)) continue;
714 /* Hack -- no teleport onto glyph of warding */
715 if (is_glyph_grid(&floor_ptr->grid_array[ny][nx])) continue;
716 if (is_explosive_rune_grid(&floor_ptr->grid_array[ny][nx])) continue;
718 /* ...nor onto the Pattern */
719 if (pattern_tile(floor_ptr, ny, nx)) continue;
721 /*** It's a good place ***/
723 m_ptr = &floor_ptr->m_list[m_idx];
725 /* Update the old location */
726 floor_ptr->grid_array[oy][ox].m_idx = 0;
728 /* Update the new location */
729 floor_ptr->grid_array[ny][nx].m_idx = m_idx;
731 /* Move the monster */
735 /* No need to do update_monster() */
743 * @brief 新フロアに移動元フロアに繋がる階段を配置する / Virtually teleport onto the stairs that is connecting between two floors.
744 * @param sf_ptr 移動元の保存フロア構造体参照ポインタ
747 static void locate_connected_stairs(player_type *creature_ptr, floor_type *floor_ptr, saved_floor_type *sf_ptr, BIT_FLAGS floor_mode)
749 POSITION x, y, sx = 0, sy = 0;
750 POSITION x_table[20];
751 POSITION y_table[20];
755 /* Search usable stairs */
756 for (y = 0; y < floor_ptr->height; y++)
758 for (x = 0; x < floor_ptr->width; x++)
760 grid_type *g_ptr = &floor_ptr->grid_array[y][x];
761 feature_type *f_ptr = &f_info[g_ptr->feat];
764 if (floor_mode & CFM_UP)
766 if (have_flag(f_ptr->flags, FF_LESS) && have_flag(f_ptr->flags, FF_STAIRS) &&
767 !have_flag(f_ptr->flags, FF_SPECIAL))
771 /* Found fixed stairs? */
772 if (g_ptr->special &&
773 g_ptr->special == sf_ptr->upper_floor_id)
781 else if (floor_mode & CFM_DOWN)
783 if (have_flag(f_ptr->flags, FF_MORE) && have_flag(f_ptr->flags, FF_STAIRS) &&
784 !have_flag(f_ptr->flags, FF_SPECIAL))
788 /* Found fixed stairs */
789 if (g_ptr->special &&
790 g_ptr->special == sf_ptr->lower_floor_id)
800 if (have_flag(f_ptr->flags, FF_BLDG))
806 if (ok && (num < 20))
818 creature_ptr->y = sy;
819 creature_ptr->x = sx;
823 /* No stairs found! -- No return */
824 prepare_change_floor_mode(creature_ptr, CFM_RAND_PLACE | CFM_NO_RETURN);
826 /* Mega Hack -- It's not the stairs you enter. Disable it. */
827 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;
831 /* Choose random one */
834 /* Point stair location */
835 creature_ptr->y = y_table[i];
836 creature_ptr->x = x_table[i];
841 * @brief 現在のフロアを離れるに伴って行なわれる保存処理
842 * / Maintain quest monsters, mark next floor_id at stairs, save current floor, and prepare to enter next floor.
843 * @param creature_ptr プレーヤーへの参照ポインタ
846 void leave_floor(player_type *creature_ptr)
848 grid_type *g_ptr = NULL;
850 saved_floor_type *sf_ptr;
851 MONRACE_IDX quest_r_idx = 0;
853 FLOOR_IDX tmp_floor_idx = 0;
855 /* Preserve pets and prepare to take these to next floor */
856 preserve_pet(creature_ptr);
858 /* Remove all mirrors without explosion */
859 remove_all_mirrors(creature_ptr, FALSE);
861 if (creature_ptr->special_defense & NINJA_S_STEALTH) set_superstealth(creature_ptr, FALSE);
863 /* New floor is not yet prepared */
866 /* Temporarily get a floor_id (for Arena) */
867 if (!creature_ptr->floor_id &&
868 (creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) &&
869 !(creature_ptr->change_floor_mode & CFM_NO_RETURN))
871 /* Get temporary floor_id */
872 tmp_floor_idx = get_new_floor_id(creature_ptr);
875 /* Search the quest monster index */
876 for (i = 0; i < max_q_idx; i++)
878 if ((quest[i].status == QUEST_STATUS_TAKEN) &&
879 ((quest[i].type == QUEST_TYPE_KILL_LEVEL) ||
880 (quest[i].type == QUEST_TYPE_RANDOM)) &&
881 (quest[i].level == creature_ptr->current_floor_ptr->dun_level) &&
882 (creature_ptr->dungeon_idx == quest[i].dungeon) &&
883 !(quest[i].flags & QUEST_FLAG_PRESET))
885 quest_r_idx = quest[i].r_idx;
889 /* Maintain quest monsters */
890 for (i = 1; i < creature_ptr->current_floor_ptr->m_max; i++)
893 monster_type *m_ptr = &creature_ptr->current_floor_ptr->m_list[i];
895 if (!monster_is_valid(m_ptr)) continue;
897 /* Only maintain quest monsters */
898 if (quest_r_idx != m_ptr->r_idx) continue;
900 /* Extract real monster race */
901 r_ptr = real_r_ptr(m_ptr);
903 /* Ignore unique monsters */
904 if ((r_ptr->flags1 & RF1_UNIQUE) ||
905 (r_ptr->flags7 & RF7_NAZGUL)) continue;
907 /* Delete non-unique quest monsters */
908 delete_monster_idx(creature_ptr, i);
911 /* Check if there is a same item */
912 for (i = 0; i < INVEN_PACK; i++)
914 object_type *o_ptr = &creature_ptr->inventory_list[i];
916 if (!OBJECT_IS_VALID(o_ptr)) continue;
918 /* Delete old memorized location of the artifact */
919 if (object_is_fixed_artifact(o_ptr))
921 a_info[o_ptr->name1].floor_id = 0;
925 /* Extract current floor info or NULL */
926 sf_ptr = get_sf_ptr(creature_ptr->floor_id);
928 /* Choose random stairs */
929 if ((creature_ptr->change_floor_mode & CFM_RAND_CONNECT) && tmp_floor_idx)
931 locate_connected_stairs(creature_ptr, creature_ptr->current_floor_ptr, sf_ptr, creature_ptr->change_floor_mode);
934 /* Extract new dungeon level */
935 if (creature_ptr->change_floor_mode & CFM_SAVE_FLOORS)
937 /* Extract stair position */
938 g_ptr = &creature_ptr->current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
939 f_ptr = &f_info[g_ptr->feat];
941 /* Get back to old saved floor? */
942 if (g_ptr->special && !have_flag(f_ptr->flags, FF_SPECIAL) && get_sf_ptr(g_ptr->special))
944 /* Saved floor is exist. Use it. */
945 new_floor_id = g_ptr->special;
948 /* Mark shaft up/down */
949 if (have_flag(f_ptr->flags, FF_STAIRS) && have_flag(f_ptr->flags, FF_SHAFT))
951 prepare_change_floor_mode(creature_ptr, CFM_SHAFT);
955 /* Climb up/down some sort of stairs */
956 if (creature_ptr->change_floor_mode & (CFM_DOWN | CFM_UP))
960 /* Extract level movement number */
961 if (creature_ptr->change_floor_mode & CFM_DOWN) move_num = 1;
962 else if (creature_ptr->change_floor_mode & CFM_UP) move_num = -1;
964 /* Shafts are deeper than normal stairs */
965 if (creature_ptr->change_floor_mode & CFM_SHAFT)
966 move_num += SGN(move_num);
968 /* Get out from or Enter the dungeon */
969 if (creature_ptr->change_floor_mode & CFM_DOWN)
971 if (!creature_ptr->current_floor_ptr->dun_level)
972 move_num = d_info[creature_ptr->dungeon_idx].mindepth;
974 else if (creature_ptr->change_floor_mode & CFM_UP)
976 if (creature_ptr->current_floor_ptr->dun_level + move_num < d_info[creature_ptr->dungeon_idx].mindepth)
977 move_num = -creature_ptr->current_floor_ptr->dun_level;
980 creature_ptr->current_floor_ptr->dun_level += move_num;
983 /* Leaving the dungeon to town */
984 if (!creature_ptr->current_floor_ptr->dun_level && creature_ptr->dungeon_idx)
986 creature_ptr->leaving_dungeon = TRUE;
987 if (!vanilla_town && !lite_town)
989 creature_ptr->wilderness_y = d_info[creature_ptr->dungeon_idx].dy;
990 creature_ptr->wilderness_x = d_info[creature_ptr->dungeon_idx].dx;
992 creature_ptr->recall_dungeon = creature_ptr->dungeon_idx;
993 creature_ptr->dungeon_idx = 0;
995 /* Reach to the surface -- Clear all saved floors */
996 creature_ptr->change_floor_mode &= ~CFM_SAVE_FLOORS; // TODO
999 /* Kill some old saved floors */
1000 if (!(creature_ptr->change_floor_mode & CFM_SAVE_FLOORS))
1002 /* Kill all saved floors */
1003 for (i = 0; i < MAX_SAVED_FLOORS; i++)
1004 kill_saved_floor(creature_ptr, &saved_floors[i]);
1006 /* Reset visit_mark count */
1007 latest_visit_mark = 1;
1009 else if (creature_ptr->change_floor_mode & CFM_NO_RETURN)
1011 /* Kill current floor */
1012 kill_saved_floor(creature_ptr, sf_ptr);
1015 /* No current floor -- Left/Enter dungeon etc... */
1016 if (!creature_ptr->floor_id)
1018 /* No longer need to save current floor */
1022 /* Mark next floor_id on the previous floor */
1026 new_floor_id = get_new_floor_id(creature_ptr);
1028 /* Connect from here */
1029 if (g_ptr && !feat_uses_special(g_ptr->feat))
1031 g_ptr->special = tmp_floor_idx;
1035 /* Fix connection -- level teleportation or trap door */
1036 if (creature_ptr->change_floor_mode & CFM_RAND_CONNECT)
1038 if (creature_ptr->change_floor_mode & CFM_UP)
1039 sf_ptr->upper_floor_id = new_floor_id;
1040 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1041 sf_ptr->lower_floor_id = new_floor_id;
1044 /* If you can return, you need to save previous floor */
1045 if ((creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) &&
1046 !(creature_ptr->change_floor_mode & CFM_NO_RETURN))
1048 /* Get out of the my way! */
1049 get_out_monster(creature_ptr);
1051 /* Record the last visit turn of current floor */
1052 sf_ptr->last_visit = current_world_ptr->game_turn;
1054 forget_lite(creature_ptr->current_floor_ptr);
1055 forget_view(creature_ptr->current_floor_ptr);
1056 clear_mon_lite(creature_ptr->current_floor_ptr);
1058 /* Save current floor */
1059 if (!save_floor(creature_ptr, sf_ptr, 0))
1061 /* Save failed -- No return */
1062 prepare_change_floor_mode(creature_ptr, CFM_NO_RETURN);
1064 /* Kill current floor */
1065 kill_saved_floor(creature_ptr, get_sf_ptr(creature_ptr->floor_id));
1072 * @brief フロアの切り替え処理 / Enter new floor.
1073 * @param creature_ptr プレーヤーへの参照ポインタ
1076 * If the floor is an old saved floor, it will be\n
1077 * restored from the temporary file. If the floor is new one, new floor\n
1078 * will be generated.\n
1080 void change_floor(player_type *creature_ptr)
1082 saved_floor_type *sf_ptr;
1083 bool loaded = FALSE;
1085 /* The dungeon is not ready */
1086 current_world_ptr->character_dungeon = FALSE;
1088 /* No longer in the trap detecteded region */
1089 creature_ptr->dtrap = FALSE;
1091 /* Mega-Hack -- no panel yet */
1097 /* Mega-Hack -- not ambushed on the wildness? */
1098 creature_ptr->ambush_flag = FALSE;
1100 /* No saved floors (On the surface etc.) */
1101 if (!(creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) &&
1102 !(creature_ptr->change_floor_mode & CFM_FIRST_FLOOR))
1104 generate_floor(creature_ptr);
1106 /* Paranoia -- No new saved floor */
1110 /* In the dungeon */
1113 /* No floor_id yet */
1117 new_floor_id = get_new_floor_id(creature_ptr);
1120 /* Pointer for infomations of new floor */
1121 sf_ptr = get_sf_ptr(new_floor_id);
1123 /* Try to restore old floor */
1124 if (sf_ptr->last_visit)
1126 /* Old saved floor is exist */
1127 if (load_floor(creature_ptr, sf_ptr, 0))
1131 /* Forbid return stairs */
1132 if (creature_ptr->change_floor_mode & CFM_NO_RETURN)
1134 grid_type *g_ptr = &creature_ptr->current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
1136 if (!feat_uses_special(g_ptr->feat))
1138 if (creature_ptr->change_floor_mode & (CFM_DOWN | CFM_UP))
1140 /* Reset to floor */
1141 g_ptr->feat = feat_ground_type[randint0(100)];
1151 * Set lower/upper_floor_id of new floor when the new
1152 * floor is right-above/right-under the current floor.
1154 * Stair creation/Teleport level/Trap door will take
1155 * you the same floor when you used it later again.
1157 if (creature_ptr->floor_id)
1159 saved_floor_type *cur_sf_ptr = get_sf_ptr(creature_ptr->floor_id);
1161 if (creature_ptr->change_floor_mode & CFM_UP)
1163 /* New floor is right-above */
1164 if (cur_sf_ptr->upper_floor_id == new_floor_id)
1165 sf_ptr->lower_floor_id = creature_ptr->floor_id;
1167 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1169 /* New floor is right-under */
1170 if (cur_sf_ptr->lower_floor_id == new_floor_id)
1171 sf_ptr->upper_floor_id = creature_ptr->floor_id;
1175 /* Break connection to killed floor */
1178 if (creature_ptr->change_floor_mode & CFM_UP)
1179 sf_ptr->lower_floor_id = 0;
1180 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1181 sf_ptr->upper_floor_id = 0;
1184 /* Maintain monsters and artifacts */
1188 GAME_TURN tmp_last_visit = sf_ptr->last_visit;
1189 GAME_TURN absence_ticks;
1190 int alloc_chance = d_info[creature_ptr->dungeon_idx].max_m_alloc_chance;
1191 GAME_TURN alloc_times;
1193 while (tmp_last_visit > current_world_ptr->game_turn) tmp_last_visit -= TURNS_PER_TICK * TOWN_DAWN;
1194 absence_ticks = (current_world_ptr->game_turn - tmp_last_visit) / TURNS_PER_TICK;
1196 /* Maintain monsters */
1197 for (i = 1; i < creature_ptr->current_floor_ptr->m_max; i++)
1199 monster_race *r_ptr;
1200 monster_type *m_ptr = &creature_ptr->current_floor_ptr->m_list[i];
1202 if (!monster_is_valid(m_ptr)) continue;
1207 m_ptr->hp = m_ptr->maxhp = m_ptr->max_maxhp;
1209 /* Remove timed status (except MTIMED_CSLEEP) */
1210 (void)set_monster_fast(creature_ptr, i, 0);
1211 (void)set_monster_slow(creature_ptr, i, 0);
1212 (void)set_monster_stunned(creature_ptr, i, 0);
1213 (void)set_monster_confused(creature_ptr, i, 0);
1214 (void)set_monster_monfear(creature_ptr, i, 0);
1215 (void)set_monster_invulner(creature_ptr, i, 0, FALSE);
1218 /* Extract real monster race */
1219 r_ptr = real_r_ptr(m_ptr);
1221 /* Ignore non-unique */
1222 if (!(r_ptr->flags1 & RF1_UNIQUE) &&
1223 !(r_ptr->flags7 & RF7_NAZGUL)) continue;
1225 /* Appear at a different floor? */
1226 if (r_ptr->floor_id != new_floor_id)
1228 /* Disapper from here */
1229 delete_monster_idx(creature_ptr, i);
1233 /* Maintain artifatcs */
1234 for (i = 1; i < creature_ptr->current_floor_ptr->o_max; i++)
1236 object_type *o_ptr = &creature_ptr->current_floor_ptr->o_list[i];
1238 if (!OBJECT_IS_VALID(o_ptr)) continue;
1240 /* Ignore non-artifact */
1241 if (!object_is_fixed_artifact(o_ptr)) continue;
1243 /* Appear at a different floor? */
1244 if (a_info[o_ptr->name1].floor_id != new_floor_id)
1246 /* Disappear from here */
1247 delete_object_idx(creature_ptr, i);
1251 /* Cancel preserve */
1252 a_info[o_ptr->name1].cur_num = 1;
1256 (void)place_quest_monsters(creature_ptr);
1258 /* Place some random monsters */
1259 alloc_times = absence_ticks / alloc_chance;
1261 if (randint0(alloc_chance) < (absence_ticks % alloc_chance))
1264 for (i = 0; i < alloc_times; i++)
1266 /* Make a (group of) new monster */
1267 (void)alloc_monster(creature_ptr, 0, 0);
1272 /* New floor_id or failed to restore */
1273 else /* if (!loaded) */
1275 if (sf_ptr->last_visit)
1277 /* Temporary file is broken? */
1278 msg_print(_("階段は行き止まりだった。", "The staircases come to a dead end..."));
1280 /* Create simple dead end */
1281 build_dead_end(creature_ptr);
1283 /* Break connection */
1284 if (creature_ptr->change_floor_mode & CFM_UP)
1286 sf_ptr->upper_floor_id = 0;
1288 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1290 sf_ptr->lower_floor_id = 0;
1295 generate_floor(creature_ptr);
1298 /* Record last visit turn */
1299 sf_ptr->last_visit = current_world_ptr->game_turn;
1301 /* Set correct dun_level value */
1302 sf_ptr->dun_level = creature_ptr->current_floor_ptr->dun_level;
1304 /* Create connected stairs */
1305 if (!(creature_ptr->change_floor_mode & CFM_NO_RETURN))
1307 /* Extract stair position */
1308 grid_type *g_ptr = &creature_ptr->current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
1310 /*** Create connected stairs ***/
1312 /* No stairs down from Quest */
1313 if ((creature_ptr->change_floor_mode & CFM_UP) && !quest_number(creature_ptr, creature_ptr->current_floor_ptr->dun_level))
1315 g_ptr->feat = (creature_ptr->change_floor_mode & CFM_SHAFT) ? feat_state(creature_ptr, feat_down_stair, FF_SHAFT) : feat_down_stair;
1318 /* No stairs up when ironman_downward */
1319 else if ((creature_ptr->change_floor_mode & CFM_DOWN) && !ironman_downward)
1321 g_ptr->feat = (creature_ptr->change_floor_mode & CFM_SHAFT) ? feat_state(creature_ptr, feat_up_stair, FF_SHAFT) : feat_up_stair;
1324 /* Paranoia -- Clear mimic */
1327 /* Connect to previous floor */
1328 g_ptr->special = creature_ptr->floor_id;
1332 /* Arrive at random grid */
1333 if (creature_ptr->change_floor_mode & (CFM_RAND_PLACE))
1335 (void)new_player_spot(creature_ptr);
1338 /* You see stairs blocked */
1339 else if ((creature_ptr->change_floor_mode & CFM_NO_RETURN) && (creature_ptr->change_floor_mode & (CFM_DOWN | CFM_UP)))
1341 if (!creature_ptr->blind)
1343 msg_print(_("突然階段が塞がれてしまった。", "Suddenly the stairs is blocked!"));
1347 msg_print(_("ゴトゴトと何か音がした。", "You hear some noises."));
1354 * The "turn" is not always different number because
1355 * the level teleport doesn't take any turn. Use
1356 * visit mark instead of last visit turn to find the
1357 * oldest saved floor.
1359 sf_ptr->visit_mark = latest_visit_mark++;
1362 /* Place preserved pet monsters */
1363 place_pet(creature_ptr);
1365 /* Reset travel target place */
1366 forget_travel_flow(creature_ptr->current_floor_ptr);
1368 /* Hack -- maintain unique and artifacts */
1369 update_unique_artifact(creature_ptr->current_floor_ptr, new_floor_id);
1371 /* Now the player is in new floor */
1372 creature_ptr->floor_id = new_floor_id;
1374 /* The dungeon is ready */
1375 current_world_ptr->character_dungeon = TRUE;
1377 /* Hack -- Munchkin characters always get whole map */
1378 if (creature_ptr->pseikaku == SEIKAKU_MUNCHKIN)
1379 wiz_lite(creature_ptr, (bool)(creature_ptr->pclass == CLASS_NINJA));
1381 /* Remember when this level was "created" */
1382 creature_ptr->current_floor_ptr->generated_turn = current_world_ptr->game_turn;
1384 /* No dungeon feeling yet */
1385 creature_ptr->feeling_turn = creature_ptr->current_floor_ptr->generated_turn;
1386 creature_ptr->feeling = 0;
1388 /* Clear all flags */
1389 creature_ptr->change_floor_mode = 0L;
1391 select_floor_music(creature_ptr);
1392 creature_ptr->change_floor_mode = 0;