2 * @brief 保存された階の管理 / management of the saved floor
5 * Copyright (c) 2002 Mogami \n
6 * This software may be copied and distributed for educational, research, and \n
7 * not for profit purposes provided that this copyright and statement are \n
8 * included in all such copies. \n
9 * 2014 Deskull rearranged comment for Doxygen. \n
12 #include "floor/floor-save.h"
13 #include "cmd-building/cmd-building.h"
14 #include "cmd-io/cmd-dump.h"
15 #include "cmd/cmd-basic.h"
16 #include "dungeon/dungeon.h"
17 #include "dungeon/quest.h"
18 #include "floor/floor-events.h"
19 #include "floor/floor-generate.h"
20 #include "floor/floor-object.h"
21 #include "floor/floor.h"
22 #include "floor/geometry.h"
23 #include "floor/wild.h"
24 #include "grid/feature.h"
25 #include "grid/grid.h"
26 #include "io/files-util.h"
29 #include "io/uid-checker.h"
30 #include "io/write-diary.h"
31 #include "mind/mind-mirror-master.h"
32 #include "monster-floor/monster-generator.h"
33 #include "monster-floor/monster-remover.h"
34 #include "monster-floor/monster-summon.h"
35 #include "monster-race/race-flags1.h"
36 #include "monster-race/race-flags2.h"
37 #include "monster-race/race-flags7.h"
38 #include "monster/monster-describer.h"
39 #include "monster/monster-description-types.h"
40 #include "monster/monster-flag-types.h"
41 #include "monster/monster-info.h"
42 #include "monster/monster-list.h"
43 #include "monster/monster-status.h"
44 #include "monster/monster-update.h"
45 #include "monster/smart-learn-types.h"
46 #include "object-enchant/artifact.h"
47 #include "object/object-hook.h"
48 #include "pet/pet-util.h"
49 #include "player/player-class.h"
50 #include "player/player-effects.h"
51 #include "player/player-personalities-table.h"
52 #include "spell-kind/spells-floor.h"
53 #include "system/system-variables.h"
54 #include "util/util.h"
55 #include "view/display-main-window.h"
56 #include "world/world.h"
60 static FLOOR_IDX new_floor_id; /*!<次のフロアのID / floor_id of the destination */
61 static u32b latest_visit_mark; /*!<フロアを渡った回数?(確認中) / Max number of visit_mark */
62 #define MAX_PARTY_MON 21 /*!< フロア移動時に先のフロアに連れて行けるペットの最大数 Maximum number of preservable pets */
63 static monster_type party_mon[MAX_PARTY_MON]; /*!< フロア移動に保存するペットモンスターの配列 */
66 * Number of floor_id used from birth
68 FLOOR_IDX max_floor_id;
71 * Sign for current process used in temporary files.
72 * Actually it is the start time of current process.
74 u32b saved_floor_file_sign;
77 * @brief 保存フロア配列を初期化する / Initialize saved_floors array.
78 * @param creature_ptr プレーヤーへの参照ポインタ
79 * @param force テンポラリファイルが残っていた場合も警告なしで強制的に削除するフラグ
80 * @details Make sure that old temporary files are not remaining as gurbages.
83 void init_saved_floors(player_type *creature_ptr, bool force)
85 char floor_savefile[1024];
88 BIT_FLAGS mode = 0644;
89 for (i = 0; i < MAX_SAVED_FLOORS; i++)
91 saved_floor_type *sf_ptr = &saved_floors[i];
94 sprintf(floor_savefile, "%s.F%02d", savefile, i);
96 /* Grab permissions */
99 /* Try to create the file */
100 fd = fd_make(floor_savefile, mode);
102 /* Drop permissions */
110 msg_print(_("エラー:古いテンポラリ・ファイルが残っています。", "Error: There are old temporary files."));
111 msg_print(_("変愚蛮怒を二重に起動していないか確認してください。", "Make sure you are not running two game processes simultaneously."));
112 msg_print(_("過去に変愚蛮怒がクラッシュした場合は一時ファイルを", "If the temporary files are garbage from an old crashed process, "));
113 msg_print(_("強制的に削除して実行を続けられます。", "you can delete them safely."));
114 if (!get_check(_("強制的に削除してもよろしいですか?", "Do you delete the old temporary files? ")))
115 quit(_("実行中止", "Aborted."));
125 /* Grab permissions */
128 /* Simply kill the temporary file */
129 (void)fd_kill(floor_savefile);
131 /* Drop permissions */
134 sf_ptr->floor_id = 0;
137 /* No floor_id used yet (No.0 is reserved to indicate non existance) */
140 /* vist_mark is from 1 */
141 latest_visit_mark = 1;
143 /* A sign to mark temporary files */
144 saved_floor_file_sign = (u32b)time(NULL);
146 /* No next floor yet */
149 /* No change floor mode yet */
150 creature_ptr->change_floor_mode = 0;
154 * @brief 保存フロア用テンポラリファイルを削除する / Kill temporary files
155 * @details Should be called just before the game quit.
156 * @param creature_ptr プレーヤーへの参照ポインタ
159 void clear_saved_floor_files(player_type *creature_ptr)
161 char floor_savefile[1024];
163 for (i = 0; i < MAX_SAVED_FLOORS; i++)
165 saved_floor_type *sf_ptr = &saved_floors[i];
167 /* No temporary file */
168 if (!sf_ptr->floor_id) continue;
169 if (sf_ptr->floor_id == creature_ptr->floor_id) continue;
172 sprintf(floor_savefile, "%s.F%02d", savefile, i);
174 /* Grab permissions */
177 /* Simply kill the temporary file */
178 (void)fd_kill(floor_savefile);
180 /* Drop permissions */
186 * @brief 保存フロアIDから参照ポインタを得る / Get a pointer for an item of the saved_floors array.
187 * @param floor_id 保存フロアID
188 * @return IDに対応する保存フロアのポインタ、ない場合はNULLを返す。
190 saved_floor_type *get_sf_ptr(FLOOR_IDX floor_id)
194 /* floor_id No.0 indicates no floor */
195 if (!floor_id) return NULL;
197 for (i = 0; i < MAX_SAVED_FLOORS; i++)
199 saved_floor_type *sf_ptr = &saved_floors[i];
201 if (sf_ptr->floor_id == floor_id) return sf_ptr;
210 * @brief 参照ポインタ先の保存フロアを抹消する / kill a saved floor and get an empty space
211 * @param creature_ptr プレーヤーへの参照ポインタ
212 * @param sf_ptr 保存フロアの参照ポインタ
215 static void kill_saved_floor(player_type *creature_ptr, saved_floor_type *sf_ptr)
217 char floor_savefile[1024];
221 if (!sf_ptr->floor_id) return;
223 if (sf_ptr->floor_id == creature_ptr->floor_id)
225 /* Kill current floor */
226 creature_ptr->floor_id = 0;
228 /* Current floor doesn't have temporary file */
233 sprintf(floor_savefile, "%s.F%02d", savefile, (int)sf_ptr->savefile_id);
235 /* Grab permissions */
238 /* Simply kill the temporary file */
239 (void)fd_kill(floor_savefile);
241 /* Drop permissions */
245 /* No longer exists */
246 sf_ptr->floor_id = 0;
251 * @brief 新規に利用可能な保存フロアを返す / Initialize new saved floor and get its floor id.
252 * @param creature_ptr プレーヤーへの参照ポインタ
253 * @return 利用可能な保存フロアID
255 * If number of saved floors are already MAX_SAVED_FLOORS, kill the oldest one.
257 FLOOR_IDX get_new_floor_id(player_type *creature_ptr)
259 saved_floor_type *sf_ptr = NULL;
262 /* Look for empty space */
263 for (i = 0; i < MAX_SAVED_FLOORS; i++)
265 sf_ptr = &saved_floors[i];
267 if (!sf_ptr->floor_id) break;
271 if (i == MAX_SAVED_FLOORS)
274 u32b oldest_visit = 0xffffffffL;
276 /* Search for oldest */
277 for (i = 0; i < MAX_SAVED_FLOORS; i++)
279 sf_ptr = &saved_floors[i];
281 /* Don't kill current floor */
282 if (sf_ptr->floor_id == creature_ptr->floor_id) continue;
284 /* Don't kill newer */
285 if (sf_ptr->visit_mark > oldest_visit) continue;
288 oldest_visit = sf_ptr->visit_mark;
291 /* Kill oldest saved floor */
292 sf_ptr = &saved_floors[oldest];
293 kill_saved_floor(creature_ptr, sf_ptr);
299 /* Prepare new floor data */
300 sf_ptr->savefile_id = i;
301 sf_ptr->floor_id = max_floor_id;
302 sf_ptr->last_visit = 0;
303 sf_ptr->upper_floor_id = 0;
304 sf_ptr->lower_floor_id = 0;
305 sf_ptr->visit_mark = latest_visit_mark++;
307 /* sf_ptr->dun_level may be changed later */
308 sf_ptr->dun_level = creature_ptr->current_floor_ptr->dun_level;
311 /* Increment number of floor_id */
312 if (max_floor_id < MAX_SHORT) max_floor_id++;
314 /* 32767 floor_ids are all used up! Re-use ancient IDs */
315 else max_floor_id = 1;
317 return sf_ptr->floor_id;
322 * @brief フロア切り替え時の処理フラグを追加する / Prepare mode flags of changing floor
323 * @param creature_ptr プレーヤーへの参照ポインタ
324 * @param mode 追加したい所持フラグ
327 void prepare_change_floor_mode(player_type *creature_ptr, BIT_FLAGS mode)
329 creature_ptr->change_floor_mode |= mode;
333 * @brief 階段移動先のフロアが生成できない時に簡単な行き止まりマップを作成する / Builds the dead end
336 static void build_dead_end(player_type *creature_ptr)
340 clear_cave(creature_ptr);
342 /* Mega-Hack -- no player yet */
343 creature_ptr->x = creature_ptr->y = 0;
345 /* Fill the arrays of floors and walls in the good proportions */
346 set_floor_and_wall(0);
349 creature_ptr->current_floor_ptr->height = SCREEN_HGT;
350 creature_ptr->current_floor_ptr->width = SCREEN_WID;
352 /* Filled with permanent walls */
353 for (y = 0; y < MAX_HGT; y++)
355 for (x = 0; x < MAX_WID; x++)
357 /* Create "solid" perma-wall */
358 place_bold(creature_ptr, y, x, GB_SOLID_PERM);
362 /* Place at center of the floor */
363 creature_ptr->y = creature_ptr->current_floor_ptr->height / 2;
364 creature_ptr->x = creature_ptr->current_floor_ptr->width / 2;
366 /* Give one square */
367 place_bold(creature_ptr, creature_ptr->y, creature_ptr->x, GB_FLOOR);
369 wipe_generate_random_floor_flags(creature_ptr->current_floor_ptr);
374 * @brief フロア移動時のペット保存処理 / Preserve_pets
375 * @param master_ptr プレーヤーへの参照ポインタ
378 static void preserve_pet(player_type *master_ptr)
383 for (num = 0; num < MAX_PARTY_MON; num++)
385 party_mon[num].r_idx = 0;
388 if (master_ptr->riding)
390 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[master_ptr->riding];
392 /* Pet of other pet don't follow. */
393 if (m_ptr->parent_m_idx)
395 master_ptr->riding = 0;
396 master_ptr->pet_extra_flags &= ~(PF_TWO_HANDS);
397 master_ptr->riding_ryoute = master_ptr->old_riding_ryoute = FALSE;
401 /* Preserve the mount */
402 (void)COPY(&party_mon[0], m_ptr, monster_type);
404 /* Delete from this floor */
405 delete_monster_idx(master_ptr, master_ptr->riding);
410 * If player is in wild mode, no pets are preserved
411 * except a monster whom player riding
413 if (!master_ptr->wild_mode && !master_ptr->current_floor_ptr->inside_arena && !master_ptr->phase_out)
415 for (i = master_ptr->current_floor_ptr->m_max - 1, num = 1; (i >= 1 && num < MAX_PARTY_MON); i--)
417 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[i];
419 if (!monster_is_valid(m_ptr)) continue;
420 if (!is_pet(m_ptr)) continue;
421 if (i == master_ptr->riding) continue;
423 if (reinit_wilderness)
425 /* Don't lose sight of pets when getting a Quest */
429 POSITION dis = distance(master_ptr->y, master_ptr->x, m_ptr->fy, m_ptr->fx);
431 /* Confused (etc.) monsters don't follow. */
432 if (monster_confused_remaining(m_ptr) || monster_stunned_remaining(m_ptr) || monster_csleep_remaining(m_ptr)) continue;
434 /* Pet of other pet don't follow. */
435 if (m_ptr->parent_m_idx) continue;
438 * Pets with nickname will follow even from 3 blocks away
439 * when you or the pet can see the other.
441 if (m_ptr->nickname &&
442 ((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)) ||
443 (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))))
445 if (dis > 3) continue;
449 if (dis > 1) continue;
453 (void)COPY(&party_mon[num], &master_ptr->current_floor_ptr->m_list[i], monster_type);
457 /* Delete from this floor */
458 delete_monster_idx(master_ptr, i);
462 if (record_named_pet)
464 for (i = master_ptr->current_floor_ptr->m_max - 1; i >=1; i--)
466 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[i];
467 GAME_TEXT m_name[MAX_NLEN];
469 if (!monster_is_valid(m_ptr)) continue;
470 if (!is_pet(m_ptr)) continue;
471 if (!m_ptr->nickname) continue;
472 if (master_ptr->riding == i) continue;
474 monster_desc(master_ptr, m_name, m_ptr, MD_ASSUME_VISIBLE | MD_INDEF_VISIBLE);
475 exe_write_diary(master_ptr, DIARY_NAMED_PET, RECORD_NAMED_PET_MOVED, m_name);
480 /* Pet of other pet may disappear. */
481 for (i = master_ptr->current_floor_ptr->m_max - 1; i >=1; i--)
483 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[i];
485 /* Are there its parent? */
486 if (m_ptr->parent_m_idx && !master_ptr->current_floor_ptr->m_list[m_ptr->parent_m_idx].r_idx)
488 /* Its parent have gone, it also goes away. */
492 GAME_TEXT m_name[MAX_NLEN];
493 monster_desc(master_ptr, m_name, m_ptr, 0);
494 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
497 delete_monster_idx(master_ptr, i);
504 * @brief フロア移動時にペットを伴った場合の準備処理 / Pre-calculate the racial counters of preserved pets
505 * @param master_ptr プレーヤーへの参照ポインタ
508 * To prevent multiple generation of unique monster who is the minion of player
510 void precalc_cur_num_of_pet(player_type *player_ptr)
514 int max_num = player_ptr->wild_mode ? 1 : MAX_PARTY_MON;
516 for (i = 0; i < max_num; i++)
518 m_ptr = &party_mon[i];
520 /* Skip empty monsters */
521 if (!monster_is_valid(m_ptr)) continue;
523 /* Hack -- Increase the racial counter */
524 real_r_ptr(m_ptr)->cur_num++;
529 * @brief 移動先のフロアに伴ったペットを配置する / Place preserved pet monsters on new floor
530 * @param master_ptr プレーヤーへの参照ポインタ
533 static void place_pet(player_type *master_ptr)
536 int max_num = master_ptr->wild_mode ? 1 : MAX_PARTY_MON;
537 floor_type *floor_ptr = master_ptr->current_floor_ptr;
538 for (i = 0; i < max_num; i++)
540 POSITION cy = 0, cx = 0;
543 if (!(party_mon[i].r_idx)) continue;
547 m_idx = m_pop(floor_ptr);
548 master_ptr->riding = m_idx;
560 for (d = 1; d < A_MAX; d++)
562 for (j = 1000; j > 0; j--)
564 scatter(master_ptr, &cy, &cx, master_ptr->y, master_ptr->x, d, 0);
565 if (monster_can_enter(master_ptr, cy, cx, &r_info[party_mon[i].r_idx], 0)) break;
569 m_idx = (d == 6) ? 0 : m_pop(floor_ptr);
574 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[m_idx];
577 master_ptr->current_floor_ptr->grid_array[cy][cx].m_idx = m_idx;
579 m_ptr->r_idx = party_mon[i].r_idx;
581 /* Copy all member of the structure */
582 *m_ptr = party_mon[i];
583 r_ptr = real_r_ptr(m_ptr);
587 m_ptr->current_floor_ptr = master_ptr->current_floor_ptr;
589 m_ptr->mtimed[MTIMED_CSLEEP] = 0;
590 m_ptr->hold_o_idx = 0;
593 if ((r_ptr->flags1 & RF1_FORCE_SLEEP) && !ironman_nightmare)
595 /* Monster is still being nice */
596 m_ptr->mflag |= (MFLAG_NICE);
598 /* Must repair monsters */
599 repair_monsters = TRUE;
601 update_monster(master_ptr, m_idx, TRUE);
602 lite_spot(master_ptr, cy, cx);
604 /* Pre-calculated in precalc_cur_num_of_pet() */
605 /* r_ptr->cur_num++; */
607 /* Hack -- Count the number of "reproducers" */
608 if (r_ptr->flags2 & RF2_MULTIPLY) master_ptr->current_floor_ptr->num_repro++;
613 monster_type *m_ptr = &party_mon[i];
614 monster_race *r_ptr = real_r_ptr(m_ptr);
615 GAME_TEXT m_name[MAX_NLEN];
617 monster_desc(master_ptr, m_name, m_ptr, 0);
618 msg_format(_("%sとはぐれてしまった。", "You have lost sight of %s."), m_name);
619 if (record_named_pet && m_ptr->nickname)
621 monster_desc(master_ptr, m_name, m_ptr, MD_INDEF_VISIBLE);
622 exe_write_diary(master_ptr, DIARY_NAMED_PET, RECORD_NAMED_PET_LOST_SIGHT, m_name);
625 /* Pre-calculated in precalc_cur_num_of_pet(), but need to decrease */
626 if (r_ptr->cur_num) r_ptr->cur_num--;
630 /* For accuracy of precalc_cur_num_of_pet() */
631 (void)C_WIPE(party_mon, MAX_PARTY_MON, monster_type);
636 * @brief ユニークモンスターやアーティファクトの所在フロアを更新する / Hack -- Update location of unique monsters and artifacts
637 * @param cur_floor_id 現在のフロアID
640 * The r_ptr->floor_id and a_ptr->floor_id are not updated correctly\n
641 * while new floor creation since dungeons may be re-created by\n
642 * auto-scum option.\n
644 static void update_unique_artifact(floor_type *floor_ptr, s16b cur_floor_id)
648 /* Maintain unique monsters */
649 for (i = 1; i < floor_ptr->m_max; i++)
652 monster_type *m_ptr = &floor_ptr->m_list[i];
654 if (!monster_is_valid(m_ptr)) continue;
656 /* Extract real monster race */
657 r_ptr = real_r_ptr(m_ptr);
659 /* Memorize location of the unique monster */
660 if ((r_ptr->flags1 & RF1_UNIQUE) ||
661 (r_ptr->flags7 & RF7_NAZGUL))
663 r_ptr->floor_id = cur_floor_id;
667 /* Maintain artifatcs */
668 for (i = 1; i < floor_ptr->o_max; i++)
670 object_type *o_ptr = &floor_ptr->o_list[i];
672 if (!OBJECT_IS_VALID(o_ptr)) continue;
674 /* Memorize location of the artifact */
675 if (object_is_fixed_artifact(o_ptr))
677 a_info[o_ptr->name1].floor_id = cur_floor_id;
684 * @brief フロア移動時、プレイヤーの移動先モンスターが既にいた場合ランダムな近隣に移動させる / When a monster is at a place where player will return,
687 static void get_out_monster(player_type *protected_ptr)
691 POSITION oy = protected_ptr->y;
692 POSITION ox = protected_ptr->x;
693 floor_type *floor_ptr = protected_ptr->current_floor_ptr;
694 MONSTER_IDX m_idx = floor_ptr->grid_array[oy][ox].m_idx;
696 /* Nothing to do if no monster */
699 /* Look until done */
704 /* Pick a (possibly illegal) location */
705 POSITION ny = rand_spread(oy, dis);
706 POSITION nx = rand_spread(ox, dis);
710 /* Stop after 1000 tries */
711 if (tries > 10000) return;
714 * Increase distance after doing enough tries
715 * compared to area of possible space
717 if (tries > 20 * dis * dis) dis++;
719 /* Ignore illegal locations */
720 if (!in_bounds(floor_ptr, ny, nx)) continue;
722 /* Require "empty" floor space */
723 if (!is_cave_empty_bold(protected_ptr, ny, nx)) continue;
725 /* Hack -- no teleport onto glyph of warding */
726 if (is_glyph_grid(&floor_ptr->grid_array[ny][nx])) continue;
727 if (is_explosive_rune_grid(&floor_ptr->grid_array[ny][nx])) continue;
729 /* ...nor onto the Pattern */
730 if (pattern_tile(floor_ptr, ny, nx)) continue;
732 /*** It's a good place ***/
734 m_ptr = &floor_ptr->m_list[m_idx];
736 /* Update the old location */
737 floor_ptr->grid_array[oy][ox].m_idx = 0;
739 /* Update the new location */
740 floor_ptr->grid_array[ny][nx].m_idx = m_idx;
742 /* Move the monster */
746 /* No need to do update_monster() */
754 * @brief 新フロアに移動元フロアに繋がる階段を配置する / Virtually teleport onto the stairs that is connecting between two floors.
755 * @param sf_ptr 移動元の保存フロア構造体参照ポインタ
758 static void locate_connected_stairs(player_type *creature_ptr, floor_type *floor_ptr, saved_floor_type *sf_ptr, BIT_FLAGS floor_mode)
760 POSITION x, y, sx = 0, sy = 0;
761 POSITION x_table[20];
762 POSITION y_table[20];
766 /* Search usable stairs */
767 for (y = 0; y < floor_ptr->height; y++)
769 for (x = 0; x < floor_ptr->width; x++)
771 grid_type *g_ptr = &floor_ptr->grid_array[y][x];
772 feature_type *f_ptr = &f_info[g_ptr->feat];
775 if (floor_mode & CFM_UP)
777 if (have_flag(f_ptr->flags, FF_LESS) && have_flag(f_ptr->flags, FF_STAIRS) &&
778 !have_flag(f_ptr->flags, FF_SPECIAL))
782 /* Found fixed stairs? */
783 if (g_ptr->special &&
784 g_ptr->special == sf_ptr->upper_floor_id)
792 else if (floor_mode & CFM_DOWN)
794 if (have_flag(f_ptr->flags, FF_MORE) && have_flag(f_ptr->flags, FF_STAIRS) &&
795 !have_flag(f_ptr->flags, FF_SPECIAL))
799 /* Found fixed stairs */
800 if (g_ptr->special &&
801 g_ptr->special == sf_ptr->lower_floor_id)
811 if (have_flag(f_ptr->flags, FF_BLDG))
817 if (ok && (num < 20))
829 creature_ptr->y = sy;
830 creature_ptr->x = sx;
834 /* No stairs found! -- No return */
835 prepare_change_floor_mode(creature_ptr, CFM_RAND_PLACE | CFM_NO_RETURN);
837 /* Mega Hack -- It's not the stairs you enter. Disable it. */
838 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;
842 /* Choose random one */
845 /* Point stair location */
846 creature_ptr->y = y_table[i];
847 creature_ptr->x = x_table[i];
852 * @brief 現在のフロアを離れるに伴って行なわれる保存処理
853 * / Maintain quest monsters, mark next floor_id at stairs, save current floor, and prepare to enter next floor.
854 * @param creature_ptr プレーヤーへの参照ポインタ
857 void leave_floor(player_type *creature_ptr)
859 grid_type *g_ptr = NULL;
861 saved_floor_type *sf_ptr;
862 MONRACE_IDX quest_r_idx = 0;
864 FLOOR_IDX tmp_floor_idx = 0;
866 /* Preserve pets and prepare to take these to next floor */
867 preserve_pet(creature_ptr);
869 /* Remove all mirrors without explosion */
870 remove_all_mirrors(creature_ptr, FALSE);
872 if (creature_ptr->special_defense & NINJA_S_STEALTH) set_superstealth(creature_ptr, FALSE);
874 /* New floor is not yet prepared */
877 /* Temporarily get a floor_id (for Arena) */
878 if (!creature_ptr->floor_id &&
879 (creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) &&
880 !(creature_ptr->change_floor_mode & CFM_NO_RETURN))
882 /* Get temporary floor_id */
883 tmp_floor_idx = get_new_floor_id(creature_ptr);
886 /* Search the quest monster index */
887 for (i = 0; i < max_q_idx; i++)
889 if ((quest[i].status == QUEST_STATUS_TAKEN) &&
890 ((quest[i].type == QUEST_TYPE_KILL_LEVEL) ||
891 (quest[i].type == QUEST_TYPE_RANDOM)) &&
892 (quest[i].level == creature_ptr->current_floor_ptr->dun_level) &&
893 (creature_ptr->dungeon_idx == quest[i].dungeon) &&
894 !(quest[i].flags & QUEST_FLAG_PRESET))
896 quest_r_idx = quest[i].r_idx;
900 /* Maintain quest monsters */
901 for (i = 1; i < creature_ptr->current_floor_ptr->m_max; i++)
904 monster_type *m_ptr = &creature_ptr->current_floor_ptr->m_list[i];
906 if (!monster_is_valid(m_ptr)) continue;
908 /* Only maintain quest monsters */
909 if (quest_r_idx != m_ptr->r_idx) continue;
911 /* Extract real monster race */
912 r_ptr = real_r_ptr(m_ptr);
914 /* Ignore unique monsters */
915 if ((r_ptr->flags1 & RF1_UNIQUE) ||
916 (r_ptr->flags7 & RF7_NAZGUL)) continue;
918 /* Delete non-unique quest monsters */
919 delete_monster_idx(creature_ptr, i);
922 /* Check if there is a same item */
923 for (i = 0; i < INVEN_PACK; i++)
925 object_type *o_ptr = &creature_ptr->inventory_list[i];
927 if (!OBJECT_IS_VALID(o_ptr)) continue;
929 /* Delete old memorized location of the artifact */
930 if (object_is_fixed_artifact(o_ptr))
932 a_info[o_ptr->name1].floor_id = 0;
936 /* Extract current floor info or NULL */
937 sf_ptr = get_sf_ptr(creature_ptr->floor_id);
939 /* Choose random stairs */
940 if ((creature_ptr->change_floor_mode & CFM_RAND_CONNECT) && tmp_floor_idx)
942 locate_connected_stairs(creature_ptr, creature_ptr->current_floor_ptr, sf_ptr, creature_ptr->change_floor_mode);
945 /* Extract new dungeon level */
946 if (creature_ptr->change_floor_mode & CFM_SAVE_FLOORS)
948 /* Extract stair position */
949 g_ptr = &creature_ptr->current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
950 f_ptr = &f_info[g_ptr->feat];
952 /* Get back to old saved floor? */
953 if (g_ptr->special && !have_flag(f_ptr->flags, FF_SPECIAL) && get_sf_ptr(g_ptr->special))
955 /* Saved floor is exist. Use it. */
956 new_floor_id = g_ptr->special;
959 /* Mark shaft up/down */
960 if (have_flag(f_ptr->flags, FF_STAIRS) && have_flag(f_ptr->flags, FF_SHAFT))
962 prepare_change_floor_mode(creature_ptr, CFM_SHAFT);
966 /* Climb up/down some sort of stairs */
967 if (creature_ptr->change_floor_mode & (CFM_DOWN | CFM_UP))
971 /* Extract level movement number */
972 if (creature_ptr->change_floor_mode & CFM_DOWN) move_num = 1;
973 else if (creature_ptr->change_floor_mode & CFM_UP) move_num = -1;
975 /* Shafts are deeper than normal stairs */
976 if (creature_ptr->change_floor_mode & CFM_SHAFT)
977 move_num += SGN(move_num);
979 /* Get out from or Enter the dungeon */
980 if (creature_ptr->change_floor_mode & CFM_DOWN)
982 if (!creature_ptr->current_floor_ptr->dun_level)
983 move_num = d_info[creature_ptr->dungeon_idx].mindepth;
985 else if (creature_ptr->change_floor_mode & CFM_UP)
987 if (creature_ptr->current_floor_ptr->dun_level + move_num < d_info[creature_ptr->dungeon_idx].mindepth)
988 move_num = -creature_ptr->current_floor_ptr->dun_level;
991 creature_ptr->current_floor_ptr->dun_level += move_num;
994 /* Leaving the dungeon to town */
995 if (!creature_ptr->current_floor_ptr->dun_level && creature_ptr->dungeon_idx)
997 creature_ptr->leaving_dungeon = TRUE;
998 if (!vanilla_town && !lite_town)
1000 creature_ptr->wilderness_y = d_info[creature_ptr->dungeon_idx].dy;
1001 creature_ptr->wilderness_x = d_info[creature_ptr->dungeon_idx].dx;
1003 creature_ptr->recall_dungeon = creature_ptr->dungeon_idx;
1004 creature_ptr->dungeon_idx = 0;
1006 /* Reach to the surface -- Clear all saved floors */
1007 creature_ptr->change_floor_mode &= ~CFM_SAVE_FLOORS; // TODO
1010 /* Kill some old saved floors */
1011 if (!(creature_ptr->change_floor_mode & CFM_SAVE_FLOORS))
1013 /* Kill all saved floors */
1014 for (i = 0; i < MAX_SAVED_FLOORS; i++)
1015 kill_saved_floor(creature_ptr, &saved_floors[i]);
1017 /* Reset visit_mark count */
1018 latest_visit_mark = 1;
1020 else if (creature_ptr->change_floor_mode & CFM_NO_RETURN)
1022 /* Kill current floor */
1023 kill_saved_floor(creature_ptr, sf_ptr);
1026 /* No current floor -- Left/Enter dungeon etc... */
1027 if (!creature_ptr->floor_id)
1029 /* No longer need to save current floor */
1033 /* Mark next floor_id on the previous floor */
1037 new_floor_id = get_new_floor_id(creature_ptr);
1039 /* Connect from here */
1040 if (g_ptr && !feat_uses_special(g_ptr->feat))
1042 g_ptr->special = new_floor_id;
1046 /* Fix connection -- level teleportation or trap door */
1047 if (creature_ptr->change_floor_mode & CFM_RAND_CONNECT)
1049 if (creature_ptr->change_floor_mode & CFM_UP)
1050 sf_ptr->upper_floor_id = new_floor_id;
1051 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1052 sf_ptr->lower_floor_id = new_floor_id;
1055 /* If you can return, you need to save previous floor */
1056 if ((creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) &&
1057 !(creature_ptr->change_floor_mode & CFM_NO_RETURN))
1059 /* Get out of the my way! */
1060 get_out_monster(creature_ptr);
1062 /* Record the last visit turn of current floor */
1063 sf_ptr->last_visit = current_world_ptr->game_turn;
1065 forget_lite(creature_ptr->current_floor_ptr);
1066 forget_view(creature_ptr->current_floor_ptr);
1067 clear_mon_lite(creature_ptr->current_floor_ptr);
1069 /* Save current floor */
1070 if (!save_floor(creature_ptr, sf_ptr, 0))
1072 /* Save failed -- No return */
1073 prepare_change_floor_mode(creature_ptr, CFM_NO_RETURN);
1075 /* Kill current floor */
1076 kill_saved_floor(creature_ptr, get_sf_ptr(creature_ptr->floor_id));
1083 * @brief フロアの切り替え処理 / Enter new floor.
1084 * @param creature_ptr プレーヤーへの参照ポインタ
1087 * If the floor is an old saved floor, it will be\n
1088 * restored from the temporary file. If the floor is new one, new floor\n
1089 * will be generated.\n
1091 void change_floor(player_type *creature_ptr)
1093 saved_floor_type *sf_ptr;
1094 bool loaded = FALSE;
1096 /* The dungeon is not ready */
1097 current_world_ptr->character_dungeon = FALSE;
1099 /* No longer in the trap detecteded region */
1100 creature_ptr->dtrap = FALSE;
1102 /* Mega-Hack -- no panel yet */
1108 /* Mega-Hack -- not ambushed on the wildness? */
1109 creature_ptr->ambush_flag = FALSE;
1111 /* No saved floors (On the surface etc.) */
1112 if (!(creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) &&
1113 !(creature_ptr->change_floor_mode & CFM_FIRST_FLOOR))
1115 generate_floor(creature_ptr);
1117 /* Paranoia -- No new saved floor */
1121 /* In the dungeon */
1124 /* No floor_id yet */
1128 new_floor_id = get_new_floor_id(creature_ptr);
1131 /* Pointer for infomations of new floor */
1132 sf_ptr = get_sf_ptr(new_floor_id);
1134 /* Try to restore old floor */
1135 if (sf_ptr->last_visit)
1137 /* Old saved floor is exist */
1138 if (load_floor(creature_ptr, sf_ptr, 0))
1142 /* Forbid return stairs */
1143 if (creature_ptr->change_floor_mode & CFM_NO_RETURN)
1145 grid_type *g_ptr = &creature_ptr->current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
1147 if (!feat_uses_special(g_ptr->feat))
1149 if (creature_ptr->change_floor_mode & (CFM_DOWN | CFM_UP))
1151 /* Reset to floor */
1152 g_ptr->feat = feat_ground_type[randint0(100)];
1162 * Set lower/upper_floor_id of new floor when the new
1163 * floor is right-above/right-under the current floor.
1165 * Stair creation/Teleport level/Trap door will take
1166 * you the same floor when you used it later again.
1168 if (creature_ptr->floor_id)
1170 saved_floor_type *cur_sf_ptr = get_sf_ptr(creature_ptr->floor_id);
1172 if (creature_ptr->change_floor_mode & CFM_UP)
1174 /* New floor is right-above */
1175 if (cur_sf_ptr->upper_floor_id == new_floor_id)
1176 sf_ptr->lower_floor_id = creature_ptr->floor_id;
1178 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1180 /* New floor is right-under */
1181 if (cur_sf_ptr->lower_floor_id == new_floor_id)
1182 sf_ptr->upper_floor_id = creature_ptr->floor_id;
1186 /* Break connection to killed floor */
1189 if (creature_ptr->change_floor_mode & CFM_UP)
1190 sf_ptr->lower_floor_id = 0;
1191 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1192 sf_ptr->upper_floor_id = 0;
1195 /* Maintain monsters and artifacts */
1199 GAME_TURN tmp_last_visit = sf_ptr->last_visit;
1200 GAME_TURN absence_ticks;
1201 int alloc_chance = d_info[creature_ptr->dungeon_idx].max_m_alloc_chance;
1202 GAME_TURN alloc_times;
1204 while (tmp_last_visit > current_world_ptr->game_turn) tmp_last_visit -= TURNS_PER_TICK * TOWN_DAWN;
1205 absence_ticks = (current_world_ptr->game_turn - tmp_last_visit) / TURNS_PER_TICK;
1207 /* Maintain monsters */
1208 for (i = 1; i < creature_ptr->current_floor_ptr->m_max; i++)
1210 monster_race *r_ptr;
1211 monster_type *m_ptr = &creature_ptr->current_floor_ptr->m_list[i];
1213 if (!monster_is_valid(m_ptr)) continue;
1218 m_ptr->hp = m_ptr->maxhp = m_ptr->max_maxhp;
1220 /* Remove timed status (except MTIMED_CSLEEP) */
1221 (void)set_monster_fast(creature_ptr, i, 0);
1222 (void)set_monster_slow(creature_ptr, i, 0);
1223 (void)set_monster_stunned(creature_ptr, i, 0);
1224 (void)set_monster_confused(creature_ptr, i, 0);
1225 (void)set_monster_monfear(creature_ptr, i, 0);
1226 (void)set_monster_invulner(creature_ptr, i, 0, FALSE);
1229 /* Extract real monster race */
1230 r_ptr = real_r_ptr(m_ptr);
1232 /* Ignore non-unique */
1233 if (!(r_ptr->flags1 & RF1_UNIQUE) &&
1234 !(r_ptr->flags7 & RF7_NAZGUL)) continue;
1236 /* Appear at a different floor? */
1237 if (r_ptr->floor_id != new_floor_id)
1239 /* Disapper from here */
1240 delete_monster_idx(creature_ptr, i);
1244 /* Maintain artifatcs */
1245 for (i = 1; i < creature_ptr->current_floor_ptr->o_max; i++)
1247 object_type *o_ptr = &creature_ptr->current_floor_ptr->o_list[i];
1249 if (!OBJECT_IS_VALID(o_ptr)) continue;
1251 /* Ignore non-artifact */
1252 if (!object_is_fixed_artifact(o_ptr)) continue;
1254 /* Appear at a different floor? */
1255 if (a_info[o_ptr->name1].floor_id != new_floor_id)
1257 /* Disappear from here */
1258 delete_object_idx(creature_ptr, i);
1262 /* Cancel preserve */
1263 a_info[o_ptr->name1].cur_num = 1;
1267 (void)place_quest_monsters(creature_ptr);
1269 /* Place some random monsters */
1270 alloc_times = absence_ticks / alloc_chance;
1272 if (randint0(alloc_chance) < (absence_ticks % alloc_chance))
1275 for (i = 0; i < alloc_times; i++)
1277 /* Make a (group of) new monster */
1278 (void)alloc_monster(creature_ptr, 0, 0, summon_specific);
1283 /* New floor_id or failed to restore */
1284 else /* if (!loaded) */
1286 if (sf_ptr->last_visit)
1288 /* Temporary file is broken? */
1289 msg_print(_("階段は行き止まりだった。", "The staircases come to a dead end..."));
1291 /* Create simple dead end */
1292 build_dead_end(creature_ptr);
1294 /* Break connection */
1295 if (creature_ptr->change_floor_mode & CFM_UP)
1297 sf_ptr->upper_floor_id = 0;
1299 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1301 sf_ptr->lower_floor_id = 0;
1306 generate_floor(creature_ptr);
1309 /* Record last visit turn */
1310 sf_ptr->last_visit = current_world_ptr->game_turn;
1312 /* Set correct dun_level value */
1313 sf_ptr->dun_level = creature_ptr->current_floor_ptr->dun_level;
1315 /* Create connected stairs */
1316 if (!(creature_ptr->change_floor_mode & CFM_NO_RETURN))
1318 /* Extract stair position */
1319 grid_type *g_ptr = &creature_ptr->current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
1321 /*** Create connected stairs ***/
1323 /* No stairs down from Quest */
1324 if ((creature_ptr->change_floor_mode & CFM_UP) && !quest_number(creature_ptr, creature_ptr->current_floor_ptr->dun_level))
1326 g_ptr->feat = (creature_ptr->change_floor_mode & CFM_SHAFT) ? feat_state(creature_ptr, feat_down_stair, FF_SHAFT) : feat_down_stair;
1329 /* No stairs up when ironman_downward */
1330 else if ((creature_ptr->change_floor_mode & CFM_DOWN) && !ironman_downward)
1332 g_ptr->feat = (creature_ptr->change_floor_mode & CFM_SHAFT) ? feat_state(creature_ptr, feat_up_stair, FF_SHAFT) : feat_up_stair;
1335 /* Paranoia -- Clear mimic */
1338 /* Connect to previous floor */
1339 g_ptr->special = creature_ptr->floor_id;
1343 /* Arrive at random grid */
1344 if (creature_ptr->change_floor_mode & (CFM_RAND_PLACE))
1346 (void)new_player_spot(creature_ptr);
1349 /* You see stairs blocked */
1350 else if ((creature_ptr->change_floor_mode & CFM_NO_RETURN) && (creature_ptr->change_floor_mode & (CFM_DOWN | CFM_UP)))
1352 if (!creature_ptr->blind)
1354 msg_print(_("突然階段が塞がれてしまった。", "Suddenly the stairs is blocked!"));
1358 msg_print(_("ゴトゴトと何か音がした。", "You hear some noises."));
1365 * The "turn" is not always different number because
1366 * the level teleport doesn't take any turn. Use
1367 * visit mark instead of last visit turn to find the
1368 * oldest saved floor.
1370 sf_ptr->visit_mark = latest_visit_mark++;
1373 /* Place preserved pet monsters */
1374 place_pet(creature_ptr);
1376 /* Reset travel target place */
1377 forget_travel_flow(creature_ptr->current_floor_ptr);
1379 /* Hack -- maintain unique and artifacts */
1380 update_unique_artifact(creature_ptr->current_floor_ptr, new_floor_id);
1382 /* Now the player is in new floor */
1383 creature_ptr->floor_id = new_floor_id;
1385 /* The dungeon is ready */
1386 current_world_ptr->character_dungeon = TRUE;
1388 /* Hack -- Munchkin characters always get whole map */
1389 if (creature_ptr->pseikaku == PERSONALITY_MUNCHKIN)
1390 wiz_lite(creature_ptr, (bool)(creature_ptr->pclass == CLASS_NINJA));
1392 /* Remember when this level was "created" */
1393 creature_ptr->current_floor_ptr->generated_turn = current_world_ptr->game_turn;
1395 /* No dungeon feeling yet */
1396 creature_ptr->feeling_turn = creature_ptr->current_floor_ptr->generated_turn;
1397 creature_ptr->feeling = 0;
1399 /* Clear all flags */
1400 creature_ptr->change_floor_mode = 0L;
1402 select_floor_music(creature_ptr);
1403 creature_ptr->change_floor_mode = 0;