3 * @brief 保存された階の管理 / management of the saved floor
6 * Copyright (c) 2002 Mogami \n
7 * This software may be copied and distributed for educational, research, and \n
8 * not for profit purposes provided that this copyright and statement are \n
9 * included in all such copies. \n
10 * 2014 Deskull rearranged comment for Doxygen. \n
13 #include "system/angband.h"
14 #include "cmd/cmd-building.h"
15 #include "system/system-variables.h"
17 #include "util/util.h"
19 #include "object/artifact.h"
20 #include "dungeon/dungeon.h"
21 #include "floor/floor.h"
22 #include "floor/floor-save.h"
23 #include "floor/floor-events.h"
24 #include "floor/floor-generate.h"
25 #include "grid/feature.h"
26 #include "floor/geometry.h"
27 #include "grid/grid.h"
28 #include "monster/monster.h"
29 #include "dungeon/quest.h"
30 #include "floor/wild.h"
31 #include "spell/spells-floor.h"
32 #include "monster/monster-status.h"
33 #include "object/object2.h"
34 #include "object/object-hook.h"
35 #include "pet/pet-util.h"
36 #include "cmd/cmd-basic.h"
37 #include "io/uid-checker.h"
38 #include "io/files-util.h"
39 #include "player/player-effects.h"
40 #include "player/player-class.h"
41 #include "player/player-personalities-table.h"
42 #include "world/world.h"
43 #include "spell/spells2.h"
44 #include "io/write-diary.h"
45 #include "cmd/cmd-dump.h"
47 #include "core/player-processor.h"
49 #include "view/display-main-window.h"
51 static FLOOR_IDX new_floor_id; /*!<次のフロアのID / floor_id of the destination */
52 static u32b latest_visit_mark; /*!<フロアを渡った回数?(確認中) / Max number of visit_mark */
53 #define MAX_PARTY_MON 21 /*!< フロア移動時に先のフロアに連れて行けるペットの最大数 Maximum number of preservable pets */
54 static monster_type party_mon[MAX_PARTY_MON]; /*!< フロア移動に保存するペットモンスターの配列 */
57 * Number of floor_id used from birth
59 FLOOR_IDX max_floor_id;
62 * Sign for current process used in temporary files.
63 * Actually it is the start time of current process.
65 u32b saved_floor_file_sign;
68 * @brief 保存フロア配列を初期化する / Initialize saved_floors array.
69 * @param creature_ptr プレーヤーへの参照ポインタ
70 * @param force テンポラリファイルが残っていた場合も警告なしで強制的に削除するフラグ
71 * @details Make sure that old temporary files are not remaining as gurbages.
74 void init_saved_floors(player_type *creature_ptr, bool force)
76 char floor_savefile[1024];
79 BIT_FLAGS mode = 0644;
80 for (i = 0; i < MAX_SAVED_FLOORS; i++)
82 saved_floor_type *sf_ptr = &saved_floors[i];
85 sprintf(floor_savefile, "%s.F%02d", savefile, i);
87 /* Grab permissions */
90 /* Try to create the file */
91 fd = fd_make(floor_savefile, mode);
93 /* Drop permissions */
101 msg_print(_("エラー:古いテンポラリ・ファイルが残っています。", "Error: There are old temporary files."));
102 msg_print(_("変愚蛮怒を二重に起動していないか確認してください。", "Make sure you are not running two game processes simultaneously."));
103 msg_print(_("過去に変愚蛮怒がクラッシュした場合は一時ファイルを", "If the temporary files are garbage from an old crashed process, "));
104 msg_print(_("強制的に削除して実行を続けられます。", "you can delete them safely."));
105 if (!get_check(_("強制的に削除してもよろしいですか?", "Do you delete the old temporary files? ")))
106 quit(_("実行中止", "Aborted."));
116 /* Grab permissions */
119 /* Simply kill the temporary file */
120 (void)fd_kill(floor_savefile);
122 /* Drop permissions */
125 sf_ptr->floor_id = 0;
128 /* No floor_id used yet (No.0 is reserved to indicate non existance) */
131 /* vist_mark is from 1 */
132 latest_visit_mark = 1;
134 /* A sign to mark temporary files */
135 saved_floor_file_sign = (u32b)time(NULL);
137 /* No next floor yet */
140 /* No change floor mode yet */
141 creature_ptr->change_floor_mode = 0;
145 * @brief 保存フロア用テンポラリファイルを削除する / Kill temporary files
146 * @details Should be called just before the game quit.
147 * @param creature_ptr プレーヤーへの参照ポインタ
150 void clear_saved_floor_files(player_type *creature_ptr)
152 char floor_savefile[1024];
154 for (i = 0; i < MAX_SAVED_FLOORS; i++)
156 saved_floor_type *sf_ptr = &saved_floors[i];
158 /* No temporary file */
159 if (!sf_ptr->floor_id) continue;
160 if (sf_ptr->floor_id == creature_ptr->floor_id) continue;
163 sprintf(floor_savefile, "%s.F%02d", savefile, i);
165 /* Grab permissions */
168 /* Simply kill the temporary file */
169 (void)fd_kill(floor_savefile);
171 /* Drop permissions */
177 * @brief 保存フロアIDから参照ポインタを得る / Get a pointer for an item of the saved_floors array.
178 * @param floor_id 保存フロアID
179 * @return IDに対応する保存フロアのポインタ、ない場合はNULLを返す。
181 saved_floor_type *get_sf_ptr(FLOOR_IDX floor_id)
185 /* floor_id No.0 indicates no floor */
186 if (!floor_id) return NULL;
188 for (i = 0; i < MAX_SAVED_FLOORS; i++)
190 saved_floor_type *sf_ptr = &saved_floors[i];
192 if (sf_ptr->floor_id == floor_id) return sf_ptr;
201 * @brief 参照ポインタ先の保存フロアを抹消する / kill a saved floor and get an empty space
202 * @param creature_ptr プレーヤーへの参照ポインタ
203 * @param sf_ptr 保存フロアの参照ポインタ
206 static void kill_saved_floor(player_type *creature_ptr, saved_floor_type *sf_ptr)
208 char floor_savefile[1024];
212 if (!sf_ptr->floor_id) return;
214 if (sf_ptr->floor_id == creature_ptr->floor_id)
216 /* Kill current floor */
217 creature_ptr->floor_id = 0;
219 /* Current floor doesn't have temporary file */
224 sprintf(floor_savefile, "%s.F%02d", savefile, (int)sf_ptr->savefile_id);
226 /* Grab permissions */
229 /* Simply kill the temporary file */
230 (void)fd_kill(floor_savefile);
232 /* Drop permissions */
236 /* No longer exists */
237 sf_ptr->floor_id = 0;
242 * @brief 新規に利用可能な保存フロアを返す / Initialize new saved floor and get its floor id.
243 * @param creature_ptr プレーヤーへの参照ポインタ
244 * @return 利用可能な保存フロアID
246 * If number of saved floors are already MAX_SAVED_FLOORS, kill the oldest one.
248 FLOOR_IDX get_new_floor_id(player_type *creature_ptr)
250 saved_floor_type *sf_ptr = NULL;
253 /* Look for empty space */
254 for (i = 0; i < MAX_SAVED_FLOORS; i++)
256 sf_ptr = &saved_floors[i];
258 if (!sf_ptr->floor_id) break;
262 if (i == MAX_SAVED_FLOORS)
265 u32b oldest_visit = 0xffffffffL;
267 /* Search for oldest */
268 for (i = 0; i < MAX_SAVED_FLOORS; i++)
270 sf_ptr = &saved_floors[i];
272 /* Don't kill current floor */
273 if (sf_ptr->floor_id == creature_ptr->floor_id) continue;
275 /* Don't kill newer */
276 if (sf_ptr->visit_mark > oldest_visit) continue;
279 oldest_visit = sf_ptr->visit_mark;
282 /* Kill oldest saved floor */
283 sf_ptr = &saved_floors[oldest];
284 kill_saved_floor(creature_ptr, sf_ptr);
290 /* Prepare new floor data */
291 sf_ptr->savefile_id = i;
292 sf_ptr->floor_id = max_floor_id;
293 sf_ptr->last_visit = 0;
294 sf_ptr->upper_floor_id = 0;
295 sf_ptr->lower_floor_id = 0;
296 sf_ptr->visit_mark = latest_visit_mark++;
298 /* sf_ptr->dun_level may be changed later */
299 sf_ptr->dun_level = creature_ptr->current_floor_ptr->dun_level;
302 /* Increment number of floor_id */
303 if (max_floor_id < MAX_SHORT) max_floor_id++;
305 /* 32767 floor_ids are all used up! Re-use ancient IDs */
306 else max_floor_id = 1;
308 return sf_ptr->floor_id;
313 * @brief フロア切り替え時の処理フラグを追加する / Prepare mode flags of changing floor
314 * @param creature_ptr プレーヤーへの参照ポインタ
315 * @param mode 追加したい所持フラグ
318 void prepare_change_floor_mode(player_type *creature_ptr, BIT_FLAGS mode)
320 creature_ptr->change_floor_mode |= mode;
324 * @brief 階段移動先のフロアが生成できない時に簡単な行き止まりマップを作成する / Builds the dead end
327 static void build_dead_end(player_type *creature_ptr)
331 clear_cave(creature_ptr);
333 /* Mega-Hack -- no player yet */
334 creature_ptr->x = creature_ptr->y = 0;
336 /* Fill the arrays of floors and walls in the good proportions */
337 set_floor_and_wall(0);
340 creature_ptr->current_floor_ptr->height = SCREEN_HGT;
341 creature_ptr->current_floor_ptr->width = SCREEN_WID;
343 /* Filled with permanent walls */
344 for (y = 0; y < MAX_HGT; y++)
346 for (x = 0; x < MAX_WID; x++)
348 /* Create "solid" perma-wall */
349 place_bold(creature_ptr, y, x, GB_SOLID_PERM);
353 /* Place at center of the floor */
354 creature_ptr->y = creature_ptr->current_floor_ptr->height / 2;
355 creature_ptr->x = creature_ptr->current_floor_ptr->width / 2;
357 /* Give one square */
358 place_bold(creature_ptr, creature_ptr->y, creature_ptr->x, GB_FLOOR);
360 wipe_generate_random_floor_flags(creature_ptr->current_floor_ptr);
365 * @brief フロア移動時のペット保存処理 / Preserve_pets
366 * @param master_ptr プレーヤーへの参照ポインタ
369 static void preserve_pet(player_type *master_ptr)
374 for (num = 0; num < MAX_PARTY_MON; num++)
376 party_mon[num].r_idx = 0;
379 if (master_ptr->riding)
381 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[master_ptr->riding];
383 /* Pet of other pet don't follow. */
384 if (m_ptr->parent_m_idx)
386 master_ptr->riding = 0;
387 master_ptr->pet_extra_flags &= ~(PF_TWO_HANDS);
388 master_ptr->riding_ryoute = master_ptr->old_riding_ryoute = FALSE;
392 /* Preserve the mount */
393 (void)COPY(&party_mon[0], m_ptr, monster_type);
395 /* Delete from this floor */
396 delete_monster_idx(master_ptr, master_ptr->riding);
401 * If player is in wild mode, no pets are preserved
402 * except a monster whom player riding
404 if (!master_ptr->wild_mode && !master_ptr->current_floor_ptr->inside_arena && !master_ptr->phase_out)
406 for (i = master_ptr->current_floor_ptr->m_max - 1, num = 1; (i >= 1 && num < MAX_PARTY_MON); i--)
408 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[i];
410 if (!monster_is_valid(m_ptr)) continue;
411 if (!is_pet(m_ptr)) continue;
412 if (i == master_ptr->riding) continue;
414 if (reinit_wilderness)
416 /* Don't lose sight of pets when getting a Quest */
420 POSITION dis = distance(master_ptr->y, master_ptr->x, m_ptr->fy, m_ptr->fx);
422 /* Confused (etc.) monsters don't follow. */
423 if (MON_CONFUSED(m_ptr) || MON_STUNNED(m_ptr) || MON_CSLEEP(m_ptr)) continue;
425 /* Pet of other pet don't follow. */
426 if (m_ptr->parent_m_idx) continue;
429 * Pets with nickname will follow even from 3 blocks away
430 * when you or the pet can see the other.
432 if (m_ptr->nickname &&
433 ((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)) ||
434 (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))))
436 if (dis > 3) continue;
440 if (dis > 1) continue;
444 (void)COPY(&party_mon[num], &master_ptr->current_floor_ptr->m_list[i], monster_type);
448 /* Delete from this floor */
449 delete_monster_idx(master_ptr, i);
453 if (record_named_pet)
455 for (i = master_ptr->current_floor_ptr->m_max - 1; i >=1; i--)
457 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[i];
458 GAME_TEXT m_name[MAX_NLEN];
460 if (!monster_is_valid(m_ptr)) continue;
461 if (!is_pet(m_ptr)) continue;
462 if (!m_ptr->nickname) continue;
463 if (master_ptr->riding == i) continue;
465 monster_desc(master_ptr, m_name, m_ptr, MD_ASSUME_VISIBLE | MD_INDEF_VISIBLE);
466 exe_write_diary(master_ptr, DIARY_NAMED_PET, RECORD_NAMED_PET_MOVED, m_name);
471 /* Pet of other pet may disappear. */
472 for (i = master_ptr->current_floor_ptr->m_max - 1; i >=1; i--)
474 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[i];
476 /* Are there its parent? */
477 if (m_ptr->parent_m_idx && !master_ptr->current_floor_ptr->m_list[m_ptr->parent_m_idx].r_idx)
479 /* Its parent have gone, it also goes away. */
483 GAME_TEXT m_name[MAX_NLEN];
484 monster_desc(master_ptr, m_name, m_ptr, 0);
485 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
488 delete_monster_idx(master_ptr, i);
495 * @brief フロア移動時にペットを伴った場合の準備処理 / Pre-calculate the racial counters of preserved pets
496 * @param master_ptr プレーヤーへの参照ポインタ
499 * To prevent multiple generation of unique monster who is the minion of player
501 void precalc_cur_num_of_pet(player_type *player_ptr)
505 int max_num = player_ptr->wild_mode ? 1 : MAX_PARTY_MON;
507 for (i = 0; i < max_num; i++)
509 m_ptr = &party_mon[i];
511 /* Skip empty monsters */
512 if (!monster_is_valid(m_ptr)) continue;
514 /* Hack -- Increase the racial counter */
515 real_r_ptr(m_ptr)->cur_num++;
520 * @brief 移動先のフロアに伴ったペットを配置する / Place preserved pet monsters on new floor
521 * @param master_ptr プレーヤーへの参照ポインタ
524 static void place_pet(player_type *master_ptr)
527 int max_num = master_ptr->wild_mode ? 1 : MAX_PARTY_MON;
529 for (i = 0; i < max_num; i++)
531 POSITION cy = 0, cx = 0;
534 if (!(party_mon[i].r_idx)) continue;
538 m_idx = m_pop(master_ptr);
539 master_ptr->riding = m_idx;
551 for (d = 1; d < A_MAX; d++)
553 for (j = 1000; j > 0; j--)
555 scatter(master_ptr, &cy, &cx, master_ptr->y, master_ptr->x, d, 0);
556 if (monster_can_enter(master_ptr, cy, cx, &r_info[party_mon[i].r_idx], 0)) break;
560 m_idx = (d == 6) ? 0 : m_pop(master_ptr);
565 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[m_idx];
568 master_ptr->current_floor_ptr->grid_array[cy][cx].m_idx = m_idx;
570 m_ptr->r_idx = party_mon[i].r_idx;
572 /* Copy all member of the structure */
573 *m_ptr = party_mon[i];
574 r_ptr = real_r_ptr(m_ptr);
578 m_ptr->current_floor_ptr = master_ptr->current_floor_ptr;
580 m_ptr->mtimed[MTIMED_CSLEEP] = 0;
581 m_ptr->hold_o_idx = 0;
584 if ((r_ptr->flags1 & RF1_FORCE_SLEEP) && !ironman_nightmare)
586 /* Monster is still being nice */
587 m_ptr->mflag |= (MFLAG_NICE);
589 /* Must repair monsters */
590 repair_monsters = TRUE;
592 update_monster(master_ptr, m_idx, TRUE);
593 lite_spot(master_ptr, cy, cx);
595 /* Pre-calculated in precalc_cur_num_of_pet() */
596 /* r_ptr->cur_num++; */
598 /* Hack -- Count the number of "reproducers" */
599 if (r_ptr->flags2 & RF2_MULTIPLY) master_ptr->current_floor_ptr->num_repro++;
604 monster_type *m_ptr = &party_mon[i];
605 monster_race *r_ptr = real_r_ptr(m_ptr);
606 GAME_TEXT m_name[MAX_NLEN];
608 monster_desc(master_ptr, m_name, m_ptr, 0);
609 msg_format(_("%sとはぐれてしまった。", "You have lost sight of %s."), m_name);
610 if (record_named_pet && m_ptr->nickname)
612 monster_desc(master_ptr, m_name, m_ptr, MD_INDEF_VISIBLE);
613 exe_write_diary(master_ptr, DIARY_NAMED_PET, RECORD_NAMED_PET_LOST_SIGHT, m_name);
616 /* Pre-calculated in precalc_cur_num_of_pet(), but need to decrease */
617 if (r_ptr->cur_num) r_ptr->cur_num--;
621 /* For accuracy of precalc_cur_num_of_pet() */
622 (void)C_WIPE(party_mon, MAX_PARTY_MON, monster_type);
627 * @brief ユニークモンスターやアーティファクトの所在フロアを更新する / Hack -- Update location of unique monsters and artifacts
628 * @param cur_floor_id 現在のフロアID
631 * The r_ptr->floor_id and a_ptr->floor_id are not updated correctly\n
632 * while new floor creation since dungeons may be re-created by\n
633 * auto-scum option.\n
635 static void update_unique_artifact(floor_type *floor_ptr, s16b cur_floor_id)
639 /* Maintain unique monsters */
640 for (i = 1; i < floor_ptr->m_max; i++)
643 monster_type *m_ptr = &floor_ptr->m_list[i];
645 if (!monster_is_valid(m_ptr)) continue;
647 /* Extract real monster race */
648 r_ptr = real_r_ptr(m_ptr);
650 /* Memorize location of the unique monster */
651 if ((r_ptr->flags1 & RF1_UNIQUE) ||
652 (r_ptr->flags7 & RF7_NAZGUL))
654 r_ptr->floor_id = cur_floor_id;
658 /* Maintain artifatcs */
659 for (i = 1; i < floor_ptr->o_max; i++)
661 object_type *o_ptr = &floor_ptr->o_list[i];
663 if (!OBJECT_IS_VALID(o_ptr)) continue;
665 /* Memorize location of the artifact */
666 if (object_is_fixed_artifact(o_ptr))
668 a_info[o_ptr->name1].floor_id = cur_floor_id;
675 * @brief フロア移動時、プレイヤーの移動先モンスターが既にいた場合ランダムな近隣に移動させる / When a monster is at a place where player will return,
678 static void get_out_monster(player_type *protected_ptr)
682 POSITION oy = protected_ptr->y;
683 POSITION ox = protected_ptr->x;
684 floor_type *floor_ptr = protected_ptr->current_floor_ptr;
685 MONSTER_IDX m_idx = floor_ptr->grid_array[oy][ox].m_idx;
687 /* Nothing to do if no monster */
690 /* Look until done */
695 /* Pick a (possibly illegal) location */
696 POSITION ny = rand_spread(oy, dis);
697 POSITION nx = rand_spread(ox, dis);
701 /* Stop after 1000 tries */
702 if (tries > 10000) return;
705 * Increase distance after doing enough tries
706 * compared to area of possible space
708 if (tries > 20 * dis * dis) dis++;
710 /* Ignore illegal locations */
711 if (!in_bounds(floor_ptr, ny, nx)) continue;
713 /* Require "empty" floor space */
714 if (!is_cave_empty_bold(protected_ptr, ny, nx)) continue;
716 /* Hack -- no teleport onto glyph of warding */
717 if (is_glyph_grid(&floor_ptr->grid_array[ny][nx])) continue;
718 if (is_explosive_rune_grid(&floor_ptr->grid_array[ny][nx])) continue;
720 /* ...nor onto the Pattern */
721 if (pattern_tile(floor_ptr, ny, nx)) continue;
723 /*** It's a good place ***/
725 m_ptr = &floor_ptr->m_list[m_idx];
727 /* Update the old location */
728 floor_ptr->grid_array[oy][ox].m_idx = 0;
730 /* Update the new location */
731 floor_ptr->grid_array[ny][nx].m_idx = m_idx;
733 /* Move the monster */
737 /* No need to do update_monster() */
745 * @brief 新フロアに移動元フロアに繋がる階段を配置する / Virtually teleport onto the stairs that is connecting between two floors.
746 * @param sf_ptr 移動元の保存フロア構造体参照ポインタ
749 static void locate_connected_stairs(player_type *creature_ptr, floor_type *floor_ptr, saved_floor_type *sf_ptr, BIT_FLAGS floor_mode)
751 POSITION x, y, sx = 0, sy = 0;
752 POSITION x_table[20];
753 POSITION y_table[20];
757 /* Search usable stairs */
758 for (y = 0; y < floor_ptr->height; y++)
760 for (x = 0; x < floor_ptr->width; x++)
762 grid_type *g_ptr = &floor_ptr->grid_array[y][x];
763 feature_type *f_ptr = &f_info[g_ptr->feat];
766 if (floor_mode & CFM_UP)
768 if (have_flag(f_ptr->flags, FF_LESS) && have_flag(f_ptr->flags, FF_STAIRS) &&
769 !have_flag(f_ptr->flags, FF_SPECIAL))
773 /* Found fixed stairs? */
774 if (g_ptr->special &&
775 g_ptr->special == sf_ptr->upper_floor_id)
783 else if (floor_mode & CFM_DOWN)
785 if (have_flag(f_ptr->flags, FF_MORE) && have_flag(f_ptr->flags, FF_STAIRS) &&
786 !have_flag(f_ptr->flags, FF_SPECIAL))
790 /* Found fixed stairs */
791 if (g_ptr->special &&
792 g_ptr->special == sf_ptr->lower_floor_id)
802 if (have_flag(f_ptr->flags, FF_BLDG))
808 if (ok && (num < 20))
820 creature_ptr->y = sy;
821 creature_ptr->x = sx;
825 /* No stairs found! -- No return */
826 prepare_change_floor_mode(creature_ptr, CFM_RAND_PLACE | CFM_NO_RETURN);
828 /* Mega Hack -- It's not the stairs you enter. Disable it. */
829 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;
833 /* Choose random one */
836 /* Point stair location */
837 creature_ptr->y = y_table[i];
838 creature_ptr->x = x_table[i];
843 * @brief 現在のフロアを離れるに伴って行なわれる保存処理
844 * / Maintain quest monsters, mark next floor_id at stairs, save current floor, and prepare to enter next floor.
845 * @param creature_ptr プレーヤーへの参照ポインタ
848 void leave_floor(player_type *creature_ptr)
850 grid_type *g_ptr = NULL;
852 saved_floor_type *sf_ptr;
853 MONRACE_IDX quest_r_idx = 0;
855 FLOOR_IDX tmp_floor_idx = 0;
857 /* Preserve pets and prepare to take these to next floor */
858 preserve_pet(creature_ptr);
860 /* Remove all mirrors without explosion */
861 remove_all_mirrors(creature_ptr, FALSE);
863 if (creature_ptr->special_defense & NINJA_S_STEALTH) set_superstealth(creature_ptr, FALSE);
865 /* New floor is not yet prepared */
868 /* Temporarily get a floor_id (for Arena) */
869 if (!creature_ptr->floor_id &&
870 (creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) &&
871 !(creature_ptr->change_floor_mode & CFM_NO_RETURN))
873 /* Get temporary floor_id */
874 tmp_floor_idx = get_new_floor_id(creature_ptr);
877 /* Search the quest monster index */
878 for (i = 0; i < max_q_idx; i++)
880 if ((quest[i].status == QUEST_STATUS_TAKEN) &&
881 ((quest[i].type == QUEST_TYPE_KILL_LEVEL) ||
882 (quest[i].type == QUEST_TYPE_RANDOM)) &&
883 (quest[i].level == creature_ptr->current_floor_ptr->dun_level) &&
884 (creature_ptr->dungeon_idx == quest[i].dungeon) &&
885 !(quest[i].flags & QUEST_FLAG_PRESET))
887 quest_r_idx = quest[i].r_idx;
891 /* Maintain quest monsters */
892 for (i = 1; i < creature_ptr->current_floor_ptr->m_max; i++)
895 monster_type *m_ptr = &creature_ptr->current_floor_ptr->m_list[i];
897 if (!monster_is_valid(m_ptr)) continue;
899 /* Only maintain quest monsters */
900 if (quest_r_idx != m_ptr->r_idx) continue;
902 /* Extract real monster race */
903 r_ptr = real_r_ptr(m_ptr);
905 /* Ignore unique monsters */
906 if ((r_ptr->flags1 & RF1_UNIQUE) ||
907 (r_ptr->flags7 & RF7_NAZGUL)) continue;
909 /* Delete non-unique quest monsters */
910 delete_monster_idx(creature_ptr, i);
913 /* Check if there is a same item */
914 for (i = 0; i < INVEN_PACK; i++)
916 object_type *o_ptr = &creature_ptr->inventory_list[i];
918 if (!OBJECT_IS_VALID(o_ptr)) continue;
920 /* Delete old memorized location of the artifact */
921 if (object_is_fixed_artifact(o_ptr))
923 a_info[o_ptr->name1].floor_id = 0;
927 /* Extract current floor info or NULL */
928 sf_ptr = get_sf_ptr(creature_ptr->floor_id);
930 /* Choose random stairs */
931 if ((creature_ptr->change_floor_mode & CFM_RAND_CONNECT) && tmp_floor_idx)
933 locate_connected_stairs(creature_ptr, creature_ptr->current_floor_ptr, sf_ptr, creature_ptr->change_floor_mode);
936 /* Extract new dungeon level */
937 if (creature_ptr->change_floor_mode & CFM_SAVE_FLOORS)
939 /* Extract stair position */
940 g_ptr = &creature_ptr->current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
941 f_ptr = &f_info[g_ptr->feat];
943 /* Get back to old saved floor? */
944 if (g_ptr->special && !have_flag(f_ptr->flags, FF_SPECIAL) && get_sf_ptr(g_ptr->special))
946 /* Saved floor is exist. Use it. */
947 new_floor_id = g_ptr->special;
950 /* Mark shaft up/down */
951 if (have_flag(f_ptr->flags, FF_STAIRS) && have_flag(f_ptr->flags, FF_SHAFT))
953 prepare_change_floor_mode(creature_ptr, CFM_SHAFT);
957 /* Climb up/down some sort of stairs */
958 if (creature_ptr->change_floor_mode & (CFM_DOWN | CFM_UP))
962 /* Extract level movement number */
963 if (creature_ptr->change_floor_mode & CFM_DOWN) move_num = 1;
964 else if (creature_ptr->change_floor_mode & CFM_UP) move_num = -1;
966 /* Shafts are deeper than normal stairs */
967 if (creature_ptr->change_floor_mode & CFM_SHAFT)
968 move_num += SGN(move_num);
970 /* Get out from or Enter the dungeon */
971 if (creature_ptr->change_floor_mode & CFM_DOWN)
973 if (!creature_ptr->current_floor_ptr->dun_level)
974 move_num = d_info[creature_ptr->dungeon_idx].mindepth;
976 else if (creature_ptr->change_floor_mode & CFM_UP)
978 if (creature_ptr->current_floor_ptr->dun_level + move_num < d_info[creature_ptr->dungeon_idx].mindepth)
979 move_num = -creature_ptr->current_floor_ptr->dun_level;
982 creature_ptr->current_floor_ptr->dun_level += move_num;
985 /* Leaving the dungeon to town */
986 if (!creature_ptr->current_floor_ptr->dun_level && creature_ptr->dungeon_idx)
988 creature_ptr->leaving_dungeon = TRUE;
989 if (!vanilla_town && !lite_town)
991 creature_ptr->wilderness_y = d_info[creature_ptr->dungeon_idx].dy;
992 creature_ptr->wilderness_x = d_info[creature_ptr->dungeon_idx].dx;
994 creature_ptr->recall_dungeon = creature_ptr->dungeon_idx;
995 creature_ptr->dungeon_idx = 0;
997 /* Reach to the surface -- Clear all saved floors */
998 creature_ptr->change_floor_mode &= ~CFM_SAVE_FLOORS; // TODO
1001 /* Kill some old saved floors */
1002 if (!(creature_ptr->change_floor_mode & CFM_SAVE_FLOORS))
1004 /* Kill all saved floors */
1005 for (i = 0; i < MAX_SAVED_FLOORS; i++)
1006 kill_saved_floor(creature_ptr, &saved_floors[i]);
1008 /* Reset visit_mark count */
1009 latest_visit_mark = 1;
1011 else if (creature_ptr->change_floor_mode & CFM_NO_RETURN)
1013 /* Kill current floor */
1014 kill_saved_floor(creature_ptr, sf_ptr);
1017 /* No current floor -- Left/Enter dungeon etc... */
1018 if (!creature_ptr->floor_id)
1020 /* No longer need to save current floor */
1024 /* Mark next floor_id on the previous floor */
1028 new_floor_id = get_new_floor_id(creature_ptr);
1030 /* Connect from here */
1031 if (g_ptr && !feat_uses_special(g_ptr->feat))
1033 g_ptr->special = new_floor_id;
1037 /* Fix connection -- level teleportation or trap door */
1038 if (creature_ptr->change_floor_mode & CFM_RAND_CONNECT)
1040 if (creature_ptr->change_floor_mode & CFM_UP)
1041 sf_ptr->upper_floor_id = new_floor_id;
1042 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1043 sf_ptr->lower_floor_id = new_floor_id;
1046 /* If you can return, you need to save previous floor */
1047 if ((creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) &&
1048 !(creature_ptr->change_floor_mode & CFM_NO_RETURN))
1050 /* Get out of the my way! */
1051 get_out_monster(creature_ptr);
1053 /* Record the last visit turn of current floor */
1054 sf_ptr->last_visit = current_world_ptr->game_turn;
1056 forget_lite(creature_ptr->current_floor_ptr);
1057 forget_view(creature_ptr->current_floor_ptr);
1058 clear_mon_lite(creature_ptr->current_floor_ptr);
1060 /* Save current floor */
1061 if (!save_floor(creature_ptr, sf_ptr, 0))
1063 /* Save failed -- No return */
1064 prepare_change_floor_mode(creature_ptr, CFM_NO_RETURN);
1066 /* Kill current floor */
1067 kill_saved_floor(creature_ptr, get_sf_ptr(creature_ptr->floor_id));
1074 * @brief フロアの切り替え処理 / Enter new floor.
1075 * @param creature_ptr プレーヤーへの参照ポインタ
1078 * If the floor is an old saved floor, it will be\n
1079 * restored from the temporary file. If the floor is new one, new floor\n
1080 * will be generated.\n
1082 void change_floor(player_type *creature_ptr)
1084 saved_floor_type *sf_ptr;
1085 bool loaded = FALSE;
1087 /* The dungeon is not ready */
1088 current_world_ptr->character_dungeon = FALSE;
1090 /* No longer in the trap detecteded region */
1091 creature_ptr->dtrap = FALSE;
1093 /* Mega-Hack -- no panel yet */
1099 /* Mega-Hack -- not ambushed on the wildness? */
1100 creature_ptr->ambush_flag = FALSE;
1102 /* No saved floors (On the surface etc.) */
1103 if (!(creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) &&
1104 !(creature_ptr->change_floor_mode & CFM_FIRST_FLOOR))
1106 generate_floor(creature_ptr);
1108 /* Paranoia -- No new saved floor */
1112 /* In the dungeon */
1115 /* No floor_id yet */
1119 new_floor_id = get_new_floor_id(creature_ptr);
1122 /* Pointer for infomations of new floor */
1123 sf_ptr = get_sf_ptr(new_floor_id);
1125 /* Try to restore old floor */
1126 if (sf_ptr->last_visit)
1128 /* Old saved floor is exist */
1129 if (load_floor(creature_ptr, sf_ptr, 0))
1133 /* Forbid return stairs */
1134 if (creature_ptr->change_floor_mode & CFM_NO_RETURN)
1136 grid_type *g_ptr = &creature_ptr->current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
1138 if (!feat_uses_special(g_ptr->feat))
1140 if (creature_ptr->change_floor_mode & (CFM_DOWN | CFM_UP))
1142 /* Reset to floor */
1143 g_ptr->feat = feat_ground_type[randint0(100)];
1153 * Set lower/upper_floor_id of new floor when the new
1154 * floor is right-above/right-under the current floor.
1156 * Stair creation/Teleport level/Trap door will take
1157 * you the same floor when you used it later again.
1159 if (creature_ptr->floor_id)
1161 saved_floor_type *cur_sf_ptr = get_sf_ptr(creature_ptr->floor_id);
1163 if (creature_ptr->change_floor_mode & CFM_UP)
1165 /* New floor is right-above */
1166 if (cur_sf_ptr->upper_floor_id == new_floor_id)
1167 sf_ptr->lower_floor_id = creature_ptr->floor_id;
1169 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1171 /* New floor is right-under */
1172 if (cur_sf_ptr->lower_floor_id == new_floor_id)
1173 sf_ptr->upper_floor_id = creature_ptr->floor_id;
1177 /* Break connection to killed floor */
1180 if (creature_ptr->change_floor_mode & CFM_UP)
1181 sf_ptr->lower_floor_id = 0;
1182 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1183 sf_ptr->upper_floor_id = 0;
1186 /* Maintain monsters and artifacts */
1190 GAME_TURN tmp_last_visit = sf_ptr->last_visit;
1191 GAME_TURN absence_ticks;
1192 int alloc_chance = d_info[creature_ptr->dungeon_idx].max_m_alloc_chance;
1193 GAME_TURN alloc_times;
1195 while (tmp_last_visit > current_world_ptr->game_turn) tmp_last_visit -= TURNS_PER_TICK * TOWN_DAWN;
1196 absence_ticks = (current_world_ptr->game_turn - tmp_last_visit) / TURNS_PER_TICK;
1198 /* Maintain monsters */
1199 for (i = 1; i < creature_ptr->current_floor_ptr->m_max; i++)
1201 monster_race *r_ptr;
1202 monster_type *m_ptr = &creature_ptr->current_floor_ptr->m_list[i];
1204 if (!monster_is_valid(m_ptr)) continue;
1209 m_ptr->hp = m_ptr->maxhp = m_ptr->max_maxhp;
1211 /* Remove timed status (except MTIMED_CSLEEP) */
1212 (void)set_monster_fast(creature_ptr, i, 0);
1213 (void)set_monster_slow(creature_ptr, i, 0);
1214 (void)set_monster_stunned(creature_ptr, i, 0);
1215 (void)set_monster_confused(creature_ptr, i, 0);
1216 (void)set_monster_monfear(creature_ptr, i, 0);
1217 (void)set_monster_invulner(creature_ptr, i, 0, FALSE);
1220 /* Extract real monster race */
1221 r_ptr = real_r_ptr(m_ptr);
1223 /* Ignore non-unique */
1224 if (!(r_ptr->flags1 & RF1_UNIQUE) &&
1225 !(r_ptr->flags7 & RF7_NAZGUL)) continue;
1227 /* Appear at a different floor? */
1228 if (r_ptr->floor_id != new_floor_id)
1230 /* Disapper from here */
1231 delete_monster_idx(creature_ptr, i);
1235 /* Maintain artifatcs */
1236 for (i = 1; i < creature_ptr->current_floor_ptr->o_max; i++)
1238 object_type *o_ptr = &creature_ptr->current_floor_ptr->o_list[i];
1240 if (!OBJECT_IS_VALID(o_ptr)) continue;
1242 /* Ignore non-artifact */
1243 if (!object_is_fixed_artifact(o_ptr)) continue;
1245 /* Appear at a different floor? */
1246 if (a_info[o_ptr->name1].floor_id != new_floor_id)
1248 /* Disappear from here */
1249 delete_object_idx(creature_ptr, i);
1253 /* Cancel preserve */
1254 a_info[o_ptr->name1].cur_num = 1;
1258 (void)place_quest_monsters(creature_ptr);
1260 /* Place some random monsters */
1261 alloc_times = absence_ticks / alloc_chance;
1263 if (randint0(alloc_chance) < (absence_ticks % alloc_chance))
1266 for (i = 0; i < alloc_times; i++)
1268 /* Make a (group of) new monster */
1269 (void)alloc_monster(creature_ptr, 0, 0);
1274 /* New floor_id or failed to restore */
1275 else /* if (!loaded) */
1277 if (sf_ptr->last_visit)
1279 /* Temporary file is broken? */
1280 msg_print(_("階段は行き止まりだった。", "The staircases come to a dead end..."));
1282 /* Create simple dead end */
1283 build_dead_end(creature_ptr);
1285 /* Break connection */
1286 if (creature_ptr->change_floor_mode & CFM_UP)
1288 sf_ptr->upper_floor_id = 0;
1290 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1292 sf_ptr->lower_floor_id = 0;
1297 generate_floor(creature_ptr);
1300 /* Record last visit turn */
1301 sf_ptr->last_visit = current_world_ptr->game_turn;
1303 /* Set correct dun_level value */
1304 sf_ptr->dun_level = creature_ptr->current_floor_ptr->dun_level;
1306 /* Create connected stairs */
1307 if (!(creature_ptr->change_floor_mode & CFM_NO_RETURN))
1309 /* Extract stair position */
1310 grid_type *g_ptr = &creature_ptr->current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
1312 /*** Create connected stairs ***/
1314 /* No stairs down from Quest */
1315 if ((creature_ptr->change_floor_mode & CFM_UP) && !quest_number(creature_ptr, creature_ptr->current_floor_ptr->dun_level))
1317 g_ptr->feat = (creature_ptr->change_floor_mode & CFM_SHAFT) ? feat_state(creature_ptr, feat_down_stair, FF_SHAFT) : feat_down_stair;
1320 /* No stairs up when ironman_downward */
1321 else if ((creature_ptr->change_floor_mode & CFM_DOWN) && !ironman_downward)
1323 g_ptr->feat = (creature_ptr->change_floor_mode & CFM_SHAFT) ? feat_state(creature_ptr, feat_up_stair, FF_SHAFT) : feat_up_stair;
1326 /* Paranoia -- Clear mimic */
1329 /* Connect to previous floor */
1330 g_ptr->special = creature_ptr->floor_id;
1334 /* Arrive at random grid */
1335 if (creature_ptr->change_floor_mode & (CFM_RAND_PLACE))
1337 (void)new_player_spot(creature_ptr);
1340 /* You see stairs blocked */
1341 else if ((creature_ptr->change_floor_mode & CFM_NO_RETURN) && (creature_ptr->change_floor_mode & (CFM_DOWN | CFM_UP)))
1343 if (!creature_ptr->blind)
1345 msg_print(_("突然階段が塞がれてしまった。", "Suddenly the stairs is blocked!"));
1349 msg_print(_("ゴトゴトと何か音がした。", "You hear some noises."));
1356 * The "turn" is not always different number because
1357 * the level teleport doesn't take any turn. Use
1358 * visit mark instead of last visit turn to find the
1359 * oldest saved floor.
1361 sf_ptr->visit_mark = latest_visit_mark++;
1364 /* Place preserved pet monsters */
1365 place_pet(creature_ptr);
1367 /* Reset travel target place */
1368 forget_travel_flow(creature_ptr->current_floor_ptr);
1370 /* Hack -- maintain unique and artifacts */
1371 update_unique_artifact(creature_ptr->current_floor_ptr, new_floor_id);
1373 /* Now the player is in new floor */
1374 creature_ptr->floor_id = new_floor_id;
1376 /* The dungeon is ready */
1377 current_world_ptr->character_dungeon = TRUE;
1379 /* Hack -- Munchkin characters always get whole map */
1380 if (creature_ptr->pseikaku == PERSONALITY_MUNCHKIN)
1381 wiz_lite(creature_ptr, (bool)(creature_ptr->pclass == CLASS_NINJA));
1383 /* Remember when this level was "created" */
1384 creature_ptr->current_floor_ptr->generated_turn = current_world_ptr->game_turn;
1386 /* No dungeon feeling yet */
1387 creature_ptr->feeling_turn = creature_ptr->current_floor_ptr->generated_turn;
1388 creature_ptr->feeling = 0;
1390 /* Clear all flags */
1391 creature_ptr->change_floor_mode = 0L;
1393 select_floor_music(creature_ptr);
1394 creature_ptr->change_floor_mode = 0;