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"
20 #include "dungeon/dungeon.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"
41 #include "world/world.h"
42 #include "spell/spells2.h"
43 #include "io/write-diary.h"
44 #include "cmd/cmd-dump.h"
46 #include "core/player-processor.h"
48 #include "view/display-main-window.h"
50 static FLOOR_IDX new_floor_id; /*!<次のフロアのID / floor_id of the destination */
51 static u32b latest_visit_mark; /*!<フロアを渡った回数?(確認中) / Max number of visit_mark */
52 #define MAX_PARTY_MON 21 /*!< フロア移動時に先のフロアに連れて行けるペットの最大数 Maximum number of preservable pets */
53 static monster_type party_mon[MAX_PARTY_MON]; /*!< フロア移動に保存するペットモンスターの配列 */
56 * Number of floor_id used from birth
58 FLOOR_IDX max_floor_id;
61 * Sign for current process used in temporary files.
62 * Actually it is the start time of current process.
64 u32b saved_floor_file_sign;
67 * @brief 保存フロア配列を初期化する / Initialize saved_floors array.
68 * @param creature_ptr プレーヤーへの参照ポインタ
69 * @param force テンポラリファイルが残っていた場合も警告なしで強制的に削除するフラグ
70 * @details Make sure that old temporary files are not remaining as gurbages.
73 void init_saved_floors(player_type *creature_ptr, bool force)
75 char floor_savefile[1024];
78 BIT_FLAGS mode = 0644;
79 for (i = 0; i < MAX_SAVED_FLOORS; i++)
81 saved_floor_type *sf_ptr = &saved_floors[i];
84 sprintf(floor_savefile, "%s.F%02d", savefile, i);
86 /* Grab permissions */
89 /* Try to create the file */
90 fd = fd_make(floor_savefile, mode);
92 /* Drop permissions */
100 msg_print(_("エラー:古いテンポラリ・ファイルが残っています。", "Error: There are old temporary files."));
101 msg_print(_("変愚蛮怒を二重に起動していないか確認してください。", "Make sure you are not running two game processes simultaneously."));
102 msg_print(_("過去に変愚蛮怒がクラッシュした場合は一時ファイルを", "If the temporary files are garbage from an old crashed process, "));
103 msg_print(_("強制的に削除して実行を続けられます。", "you can delete them safely."));
104 if (!get_check(_("強制的に削除してもよろしいですか?", "Do you delete the old temporary files? ")))
105 quit(_("実行中止", "Aborted."));
115 /* Grab permissions */
118 /* Simply kill the temporary file */
119 (void)fd_kill(floor_savefile);
121 /* Drop permissions */
124 sf_ptr->floor_id = 0;
127 /* No floor_id used yet (No.0 is reserved to indicate non existance) */
130 /* vist_mark is from 1 */
131 latest_visit_mark = 1;
133 /* A sign to mark temporary files */
134 saved_floor_file_sign = (u32b)time(NULL);
136 /* No next floor yet */
139 /* No change floor mode yet */
140 creature_ptr->change_floor_mode = 0;
144 * @brief 保存フロア用テンポラリファイルを削除する / Kill temporary files
145 * @details Should be called just before the game quit.
146 * @param creature_ptr プレーヤーへの参照ポインタ
149 void clear_saved_floor_files(player_type *creature_ptr)
151 char floor_savefile[1024];
153 for (i = 0; i < MAX_SAVED_FLOORS; i++)
155 saved_floor_type *sf_ptr = &saved_floors[i];
157 /* No temporary file */
158 if (!sf_ptr->floor_id) continue;
159 if (sf_ptr->floor_id == creature_ptr->floor_id) continue;
162 sprintf(floor_savefile, "%s.F%02d", savefile, i);
164 /* Grab permissions */
167 /* Simply kill the temporary file */
168 (void)fd_kill(floor_savefile);
170 /* Drop permissions */
176 * @brief 保存フロアIDから参照ポインタを得る / Get a pointer for an item of the saved_floors array.
177 * @param floor_id 保存フロアID
178 * @return IDに対応する保存フロアのポインタ、ない場合はNULLを返す。
180 saved_floor_type *get_sf_ptr(FLOOR_IDX floor_id)
184 /* floor_id No.0 indicates no floor */
185 if (!floor_id) return NULL;
187 for (i = 0; i < MAX_SAVED_FLOORS; i++)
189 saved_floor_type *sf_ptr = &saved_floors[i];
191 if (sf_ptr->floor_id == floor_id) return sf_ptr;
200 * @brief 参照ポインタ先の保存フロアを抹消する / kill a saved floor and get an empty space
201 * @param creature_ptr プレーヤーへの参照ポインタ
202 * @param sf_ptr 保存フロアの参照ポインタ
205 static void kill_saved_floor(player_type *creature_ptr, saved_floor_type *sf_ptr)
207 char floor_savefile[1024];
211 if (!sf_ptr->floor_id) return;
213 if (sf_ptr->floor_id == creature_ptr->floor_id)
215 /* Kill current floor */
216 creature_ptr->floor_id = 0;
218 /* Current floor doesn't have temporary file */
223 sprintf(floor_savefile, "%s.F%02d", savefile, (int)sf_ptr->savefile_id);
225 /* Grab permissions */
228 /* Simply kill the temporary file */
229 (void)fd_kill(floor_savefile);
231 /* Drop permissions */
235 /* No longer exists */
236 sf_ptr->floor_id = 0;
241 * @brief 新規に利用可能な保存フロアを返す / Initialize new saved floor and get its floor id.
242 * @param creature_ptr プレーヤーへの参照ポインタ
243 * @return 利用可能な保存フロアID
245 * If number of saved floors are already MAX_SAVED_FLOORS, kill the oldest one.
247 FLOOR_IDX get_new_floor_id(player_type *creature_ptr)
249 saved_floor_type *sf_ptr = NULL;
252 /* Look for empty space */
253 for (i = 0; i < MAX_SAVED_FLOORS; i++)
255 sf_ptr = &saved_floors[i];
257 if (!sf_ptr->floor_id) break;
261 if (i == MAX_SAVED_FLOORS)
264 u32b oldest_visit = 0xffffffffL;
266 /* Search for oldest */
267 for (i = 0; i < MAX_SAVED_FLOORS; i++)
269 sf_ptr = &saved_floors[i];
271 /* Don't kill current floor */
272 if (sf_ptr->floor_id == creature_ptr->floor_id) continue;
274 /* Don't kill newer */
275 if (sf_ptr->visit_mark > oldest_visit) continue;
278 oldest_visit = sf_ptr->visit_mark;
281 /* Kill oldest saved floor */
282 sf_ptr = &saved_floors[oldest];
283 kill_saved_floor(creature_ptr, sf_ptr);
289 /* Prepare new floor data */
290 sf_ptr->savefile_id = i;
291 sf_ptr->floor_id = max_floor_id;
292 sf_ptr->last_visit = 0;
293 sf_ptr->upper_floor_id = 0;
294 sf_ptr->lower_floor_id = 0;
295 sf_ptr->visit_mark = latest_visit_mark++;
297 /* sf_ptr->dun_level may be changed later */
298 sf_ptr->dun_level = creature_ptr->current_floor_ptr->dun_level;
301 /* Increment number of floor_id */
302 if (max_floor_id < MAX_SHORT) max_floor_id++;
304 /* 32767 floor_ids are all used up! Re-use ancient IDs */
305 else max_floor_id = 1;
307 return sf_ptr->floor_id;
312 * @brief フロア切り替え時の処理フラグを追加する / Prepare mode flags of changing floor
313 * @param creature_ptr プレーヤーへの参照ポインタ
314 * @param mode 追加したい所持フラグ
317 void prepare_change_floor_mode(player_type *creature_ptr, BIT_FLAGS mode)
319 creature_ptr->change_floor_mode |= mode;
323 * @brief 階段移動先のフロアが生成できない時に簡単な行き止まりマップを作成する / Builds the dead end
326 static void build_dead_end(player_type *creature_ptr)
330 clear_cave(creature_ptr);
332 /* Mega-Hack -- no player yet */
333 creature_ptr->x = creature_ptr->y = 0;
335 /* Fill the arrays of floors and walls in the good proportions */
336 set_floor_and_wall(0);
339 creature_ptr->current_floor_ptr->height = SCREEN_HGT;
340 creature_ptr->current_floor_ptr->width = SCREEN_WID;
342 /* Filled with permanent walls */
343 for (y = 0; y < MAX_HGT; y++)
345 for (x = 0; x < MAX_WID; x++)
347 /* Create "solid" perma-wall */
348 place_bold(creature_ptr, y, x, GB_SOLID_PERM);
352 /* Place at center of the floor */
353 creature_ptr->y = creature_ptr->current_floor_ptr->height / 2;
354 creature_ptr->x = creature_ptr->current_floor_ptr->width / 2;
356 /* Give one square */
357 place_bold(creature_ptr, creature_ptr->y, creature_ptr->x, GB_FLOOR);
359 wipe_generate_random_floor_flags(creature_ptr->current_floor_ptr);
364 * @brief フロア移動時のペット保存処理 / Preserve_pets
365 * @param master_ptr プレーヤーへの参照ポインタ
368 static void preserve_pet(player_type *master_ptr)
373 for (num = 0; num < MAX_PARTY_MON; num++)
375 party_mon[num].r_idx = 0;
378 if (master_ptr->riding)
380 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[master_ptr->riding];
382 /* Pet of other pet don't follow. */
383 if (m_ptr->parent_m_idx)
385 master_ptr->riding = 0;
386 master_ptr->pet_extra_flags &= ~(PF_TWO_HANDS);
387 master_ptr->riding_ryoute = master_ptr->old_riding_ryoute = FALSE;
391 /* Preserve the mount */
392 (void)COPY(&party_mon[0], m_ptr, monster_type);
394 /* Delete from this floor */
395 delete_monster_idx(master_ptr, master_ptr->riding);
400 * If player is in wild mode, no pets are preserved
401 * except a monster whom player riding
403 if (!master_ptr->wild_mode && !master_ptr->current_floor_ptr->inside_arena && !master_ptr->phase_out)
405 for (i = master_ptr->current_floor_ptr->m_max - 1, num = 1; (i >= 1 && num < MAX_PARTY_MON); i--)
407 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[i];
409 if (!monster_is_valid(m_ptr)) continue;
410 if (!is_pet(m_ptr)) continue;
411 if (i == master_ptr->riding) continue;
413 if (reinit_wilderness)
415 /* Don't lose sight of pets when getting a Quest */
419 POSITION dis = distance(master_ptr->y, master_ptr->x, m_ptr->fy, m_ptr->fx);
421 /* Confused (etc.) monsters don't follow. */
422 if (MON_CONFUSED(m_ptr) || MON_STUNNED(m_ptr) || MON_CSLEEP(m_ptr)) continue;
424 /* Pet of other pet don't follow. */
425 if (m_ptr->parent_m_idx) continue;
428 * Pets with nickname will follow even from 3 blocks away
429 * when you or the pet can see the other.
431 if (m_ptr->nickname &&
432 ((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)) ||
433 (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))))
435 if (dis > 3) continue;
439 if (dis > 1) continue;
443 (void)COPY(&party_mon[num], &master_ptr->current_floor_ptr->m_list[i], monster_type);
447 /* Delete from this floor */
448 delete_monster_idx(master_ptr, i);
452 if (record_named_pet)
454 for (i = master_ptr->current_floor_ptr->m_max - 1; i >=1; i--)
456 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[i];
457 GAME_TEXT m_name[MAX_NLEN];
459 if (!monster_is_valid(m_ptr)) continue;
460 if (!is_pet(m_ptr)) continue;
461 if (!m_ptr->nickname) continue;
462 if (master_ptr->riding == i) continue;
464 monster_desc(master_ptr, m_name, m_ptr, MD_ASSUME_VISIBLE | MD_INDEF_VISIBLE);
465 exe_write_diary(master_ptr, DIARY_NAMED_PET, RECORD_NAMED_PET_MOVED, m_name);
470 /* Pet of other pet may disappear. */
471 for (i = master_ptr->current_floor_ptr->m_max - 1; i >=1; i--)
473 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[i];
475 /* Are there its parent? */
476 if (m_ptr->parent_m_idx && !master_ptr->current_floor_ptr->m_list[m_ptr->parent_m_idx].r_idx)
478 /* Its parent have gone, it also goes away. */
482 GAME_TEXT m_name[MAX_NLEN];
483 monster_desc(master_ptr, m_name, m_ptr, 0);
484 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
487 delete_monster_idx(master_ptr, i);
494 * @brief フロア移動時にペットを伴った場合の準備処理 / Pre-calculate the racial counters of preserved pets
495 * @param master_ptr プレーヤーへの参照ポインタ
498 * To prevent multiple generation of unique monster who is the minion of player
500 void precalc_cur_num_of_pet(player_type *player_ptr)
504 int max_num = player_ptr->wild_mode ? 1 : MAX_PARTY_MON;
506 for (i = 0; i < max_num; i++)
508 m_ptr = &party_mon[i];
510 /* Skip empty monsters */
511 if (!monster_is_valid(m_ptr)) continue;
513 /* Hack -- Increase the racial counter */
514 real_r_ptr(m_ptr)->cur_num++;
519 * @brief 移動先のフロアに伴ったペットを配置する / Place preserved pet monsters on new floor
520 * @param master_ptr プレーヤーへの参照ポインタ
523 static void place_pet(player_type *master_ptr)
526 int max_num = master_ptr->wild_mode ? 1 : MAX_PARTY_MON;
528 for (i = 0; i < max_num; i++)
530 POSITION cy = 0, cx = 0;
533 if (!(party_mon[i].r_idx)) continue;
537 m_idx = m_pop(master_ptr);
538 master_ptr->riding = m_idx;
550 for (d = 1; d < A_MAX; d++)
552 for (j = 1000; j > 0; j--)
554 scatter(master_ptr, &cy, &cx, master_ptr->y, master_ptr->x, d, 0);
555 if (monster_can_enter(master_ptr, cy, cx, &r_info[party_mon[i].r_idx], 0)) break;
559 m_idx = (d == 6) ? 0 : m_pop(master_ptr);
564 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[m_idx];
567 master_ptr->current_floor_ptr->grid_array[cy][cx].m_idx = m_idx;
569 m_ptr->r_idx = party_mon[i].r_idx;
571 /* Copy all member of the structure */
572 *m_ptr = party_mon[i];
573 r_ptr = real_r_ptr(m_ptr);
577 m_ptr->current_floor_ptr = master_ptr->current_floor_ptr;
579 m_ptr->mtimed[MTIMED_CSLEEP] = 0;
580 m_ptr->hold_o_idx = 0;
583 if ((r_ptr->flags1 & RF1_FORCE_SLEEP) && !ironman_nightmare)
585 /* Monster is still being nice */
586 m_ptr->mflag |= (MFLAG_NICE);
588 /* Must repair monsters */
589 repair_monsters = TRUE;
591 update_monster(master_ptr, m_idx, TRUE);
592 lite_spot(master_ptr, cy, cx);
594 /* Pre-calculated in precalc_cur_num_of_pet() */
595 /* r_ptr->cur_num++; */
597 /* Hack -- Count the number of "reproducers" */
598 if (r_ptr->flags2 & RF2_MULTIPLY) master_ptr->current_floor_ptr->num_repro++;
603 monster_type *m_ptr = &party_mon[i];
604 monster_race *r_ptr = real_r_ptr(m_ptr);
605 GAME_TEXT m_name[MAX_NLEN];
607 monster_desc(master_ptr, m_name, m_ptr, 0);
608 msg_format(_("%sとはぐれてしまった。", "You have lost sight of %s."), m_name);
609 if (record_named_pet && m_ptr->nickname)
611 monster_desc(master_ptr, m_name, m_ptr, MD_INDEF_VISIBLE);
612 exe_write_diary(master_ptr, DIARY_NAMED_PET, RECORD_NAMED_PET_LOST_SIGHT, m_name);
615 /* Pre-calculated in precalc_cur_num_of_pet(), but need to decrease */
616 if (r_ptr->cur_num) r_ptr->cur_num--;
620 /* For accuracy of precalc_cur_num_of_pet() */
621 (void)C_WIPE(party_mon, MAX_PARTY_MON, monster_type);
626 * @brief ユニークモンスターやアーティファクトの所在フロアを更新する / Hack -- Update location of unique monsters and artifacts
627 * @param cur_floor_id 現在のフロアID
630 * The r_ptr->floor_id and a_ptr->floor_id are not updated correctly\n
631 * while new floor creation since dungeons may be re-created by\n
632 * auto-scum option.\n
634 static void update_unique_artifact(floor_type *floor_ptr, s16b cur_floor_id)
638 /* Maintain unique monsters */
639 for (i = 1; i < floor_ptr->m_max; i++)
642 monster_type *m_ptr = &floor_ptr->m_list[i];
644 if (!monster_is_valid(m_ptr)) continue;
646 /* Extract real monster race */
647 r_ptr = real_r_ptr(m_ptr);
649 /* Memorize location of the unique monster */
650 if ((r_ptr->flags1 & RF1_UNIQUE) ||
651 (r_ptr->flags7 & RF7_NAZGUL))
653 r_ptr->floor_id = cur_floor_id;
657 /* Maintain artifatcs */
658 for (i = 1; i < floor_ptr->o_max; i++)
660 object_type *o_ptr = &floor_ptr->o_list[i];
662 if (!OBJECT_IS_VALID(o_ptr)) continue;
664 /* Memorize location of the artifact */
665 if (object_is_fixed_artifact(o_ptr))
667 a_info[o_ptr->name1].floor_id = cur_floor_id;
674 * @brief フロア移動時、プレイヤーの移動先モンスターが既にいた場合ランダムな近隣に移動させる / When a monster is at a place where player will return,
677 static void get_out_monster(player_type *protected_ptr)
681 POSITION oy = protected_ptr->y;
682 POSITION ox = protected_ptr->x;
683 floor_type *floor_ptr = protected_ptr->current_floor_ptr;
684 MONSTER_IDX m_idx = floor_ptr->grid_array[oy][ox].m_idx;
686 /* Nothing to do if no monster */
689 /* Look until done */
694 /* Pick a (possibly illegal) location */
695 POSITION ny = rand_spread(oy, dis);
696 POSITION nx = rand_spread(ox, dis);
700 /* Stop after 1000 tries */
701 if (tries > 10000) return;
704 * Increase distance after doing enough tries
705 * compared to area of possible space
707 if (tries > 20 * dis * dis) dis++;
709 /* Ignore illegal locations */
710 if (!in_bounds(floor_ptr, ny, nx)) continue;
712 /* Require "empty" floor space */
713 if (!is_cave_empty_bold(protected_ptr, ny, nx)) continue;
715 /* Hack -- no teleport onto glyph of warding */
716 if (is_glyph_grid(&floor_ptr->grid_array[ny][nx])) continue;
717 if (is_explosive_rune_grid(&floor_ptr->grid_array[ny][nx])) continue;
719 /* ...nor onto the Pattern */
720 if (pattern_tile(floor_ptr, ny, nx)) continue;
722 /*** It's a good place ***/
724 m_ptr = &floor_ptr->m_list[m_idx];
726 /* Update the old location */
727 floor_ptr->grid_array[oy][ox].m_idx = 0;
729 /* Update the new location */
730 floor_ptr->grid_array[ny][nx].m_idx = m_idx;
732 /* Move the monster */
736 /* No need to do update_monster() */
744 * @brief 新フロアに移動元フロアに繋がる階段を配置する / Virtually teleport onto the stairs that is connecting between two floors.
745 * @param sf_ptr 移動元の保存フロア構造体参照ポインタ
748 static void locate_connected_stairs(player_type *creature_ptr, floor_type *floor_ptr, saved_floor_type *sf_ptr, BIT_FLAGS floor_mode)
750 POSITION x, y, sx = 0, sy = 0;
751 POSITION x_table[20];
752 POSITION y_table[20];
756 /* Search usable stairs */
757 for (y = 0; y < floor_ptr->height; y++)
759 for (x = 0; x < floor_ptr->width; x++)
761 grid_type *g_ptr = &floor_ptr->grid_array[y][x];
762 feature_type *f_ptr = &f_info[g_ptr->feat];
765 if (floor_mode & CFM_UP)
767 if (have_flag(f_ptr->flags, FF_LESS) && have_flag(f_ptr->flags, FF_STAIRS) &&
768 !have_flag(f_ptr->flags, FF_SPECIAL))
772 /* Found fixed stairs? */
773 if (g_ptr->special &&
774 g_ptr->special == sf_ptr->upper_floor_id)
782 else if (floor_mode & CFM_DOWN)
784 if (have_flag(f_ptr->flags, FF_MORE) && have_flag(f_ptr->flags, FF_STAIRS) &&
785 !have_flag(f_ptr->flags, FF_SPECIAL))
789 /* Found fixed stairs */
790 if (g_ptr->special &&
791 g_ptr->special == sf_ptr->lower_floor_id)
801 if (have_flag(f_ptr->flags, FF_BLDG))
807 if (ok && (num < 20))
819 creature_ptr->y = sy;
820 creature_ptr->x = sx;
824 /* No stairs found! -- No return */
825 prepare_change_floor_mode(creature_ptr, CFM_RAND_PLACE | CFM_NO_RETURN);
827 /* Mega Hack -- It's not the stairs you enter. Disable it. */
828 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;
832 /* Choose random one */
835 /* Point stair location */
836 creature_ptr->y = y_table[i];
837 creature_ptr->x = x_table[i];
842 * @brief 現在のフロアを離れるに伴って行なわれる保存処理
843 * / Maintain quest monsters, mark next floor_id at stairs, save current floor, and prepare to enter next floor.
844 * @param creature_ptr プレーヤーへの参照ポインタ
847 void leave_floor(player_type *creature_ptr)
849 grid_type *g_ptr = NULL;
851 saved_floor_type *sf_ptr;
852 MONRACE_IDX quest_r_idx = 0;
854 FLOOR_IDX tmp_floor_idx = 0;
856 /* Preserve pets and prepare to take these to next floor */
857 preserve_pet(creature_ptr);
859 /* Remove all mirrors without explosion */
860 remove_all_mirrors(creature_ptr, FALSE);
862 if (creature_ptr->special_defense & NINJA_S_STEALTH) set_superstealth(creature_ptr, FALSE);
864 /* New floor is not yet prepared */
867 /* Temporarily get a floor_id (for Arena) */
868 if (!creature_ptr->floor_id &&
869 (creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) &&
870 !(creature_ptr->change_floor_mode & CFM_NO_RETURN))
872 /* Get temporary floor_id */
873 tmp_floor_idx = get_new_floor_id(creature_ptr);
876 /* Search the quest monster index */
877 for (i = 0; i < max_q_idx; i++)
879 if ((quest[i].status == QUEST_STATUS_TAKEN) &&
880 ((quest[i].type == QUEST_TYPE_KILL_LEVEL) ||
881 (quest[i].type == QUEST_TYPE_RANDOM)) &&
882 (quest[i].level == creature_ptr->current_floor_ptr->dun_level) &&
883 (creature_ptr->dungeon_idx == quest[i].dungeon) &&
884 !(quest[i].flags & QUEST_FLAG_PRESET))
886 quest_r_idx = quest[i].r_idx;
890 /* Maintain quest monsters */
891 for (i = 1; i < creature_ptr->current_floor_ptr->m_max; i++)
894 monster_type *m_ptr = &creature_ptr->current_floor_ptr->m_list[i];
896 if (!monster_is_valid(m_ptr)) continue;
898 /* Only maintain quest monsters */
899 if (quest_r_idx != m_ptr->r_idx) continue;
901 /* Extract real monster race */
902 r_ptr = real_r_ptr(m_ptr);
904 /* Ignore unique monsters */
905 if ((r_ptr->flags1 & RF1_UNIQUE) ||
906 (r_ptr->flags7 & RF7_NAZGUL)) continue;
908 /* Delete non-unique quest monsters */
909 delete_monster_idx(creature_ptr, i);
912 /* Check if there is a same item */
913 for (i = 0; i < INVEN_PACK; i++)
915 object_type *o_ptr = &creature_ptr->inventory_list[i];
917 if (!OBJECT_IS_VALID(o_ptr)) continue;
919 /* Delete old memorized location of the artifact */
920 if (object_is_fixed_artifact(o_ptr))
922 a_info[o_ptr->name1].floor_id = 0;
926 /* Extract current floor info or NULL */
927 sf_ptr = get_sf_ptr(creature_ptr->floor_id);
929 /* Choose random stairs */
930 if ((creature_ptr->change_floor_mode & CFM_RAND_CONNECT) && tmp_floor_idx)
932 locate_connected_stairs(creature_ptr, creature_ptr->current_floor_ptr, sf_ptr, creature_ptr->change_floor_mode);
935 /* Extract new dungeon level */
936 if (creature_ptr->change_floor_mode & CFM_SAVE_FLOORS)
938 /* Extract stair position */
939 g_ptr = &creature_ptr->current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
940 f_ptr = &f_info[g_ptr->feat];
942 /* Get back to old saved floor? */
943 if (g_ptr->special && !have_flag(f_ptr->flags, FF_SPECIAL) && get_sf_ptr(g_ptr->special))
945 /* Saved floor is exist. Use it. */
946 new_floor_id = g_ptr->special;
949 /* Mark shaft up/down */
950 if (have_flag(f_ptr->flags, FF_STAIRS) && have_flag(f_ptr->flags, FF_SHAFT))
952 prepare_change_floor_mode(creature_ptr, CFM_SHAFT);
956 /* Climb up/down some sort of stairs */
957 if (creature_ptr->change_floor_mode & (CFM_DOWN | CFM_UP))
961 /* Extract level movement number */
962 if (creature_ptr->change_floor_mode & CFM_DOWN) move_num = 1;
963 else if (creature_ptr->change_floor_mode & CFM_UP) move_num = -1;
965 /* Shafts are deeper than normal stairs */
966 if (creature_ptr->change_floor_mode & CFM_SHAFT)
967 move_num += SGN(move_num);
969 /* Get out from or Enter the dungeon */
970 if (creature_ptr->change_floor_mode & CFM_DOWN)
972 if (!creature_ptr->current_floor_ptr->dun_level)
973 move_num = d_info[creature_ptr->dungeon_idx].mindepth;
975 else if (creature_ptr->change_floor_mode & CFM_UP)
977 if (creature_ptr->current_floor_ptr->dun_level + move_num < d_info[creature_ptr->dungeon_idx].mindepth)
978 move_num = -creature_ptr->current_floor_ptr->dun_level;
981 creature_ptr->current_floor_ptr->dun_level += move_num;
984 /* Leaving the dungeon to town */
985 if (!creature_ptr->current_floor_ptr->dun_level && creature_ptr->dungeon_idx)
987 creature_ptr->leaving_dungeon = TRUE;
988 if (!vanilla_town && !lite_town)
990 creature_ptr->wilderness_y = d_info[creature_ptr->dungeon_idx].dy;
991 creature_ptr->wilderness_x = d_info[creature_ptr->dungeon_idx].dx;
993 creature_ptr->recall_dungeon = creature_ptr->dungeon_idx;
994 creature_ptr->dungeon_idx = 0;
996 /* Reach to the surface -- Clear all saved floors */
997 creature_ptr->change_floor_mode &= ~CFM_SAVE_FLOORS; // TODO
1000 /* Kill some old saved floors */
1001 if (!(creature_ptr->change_floor_mode & CFM_SAVE_FLOORS))
1003 /* Kill all saved floors */
1004 for (i = 0; i < MAX_SAVED_FLOORS; i++)
1005 kill_saved_floor(creature_ptr, &saved_floors[i]);
1007 /* Reset visit_mark count */
1008 latest_visit_mark = 1;
1010 else if (creature_ptr->change_floor_mode & CFM_NO_RETURN)
1012 /* Kill current floor */
1013 kill_saved_floor(creature_ptr, sf_ptr);
1016 /* No current floor -- Left/Enter dungeon etc... */
1017 if (!creature_ptr->floor_id)
1019 /* No longer need to save current floor */
1023 /* Mark next floor_id on the previous floor */
1027 new_floor_id = get_new_floor_id(creature_ptr);
1029 /* Connect from here */
1030 if (g_ptr && !feat_uses_special(g_ptr->feat))
1032 g_ptr->special = tmp_floor_idx;
1036 /* Fix connection -- level teleportation or trap door */
1037 if (creature_ptr->change_floor_mode & CFM_RAND_CONNECT)
1039 if (creature_ptr->change_floor_mode & CFM_UP)
1040 sf_ptr->upper_floor_id = new_floor_id;
1041 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1042 sf_ptr->lower_floor_id = new_floor_id;
1045 /* If you can return, you need to save previous floor */
1046 if ((creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) &&
1047 !(creature_ptr->change_floor_mode & CFM_NO_RETURN))
1049 /* Get out of the my way! */
1050 get_out_monster(creature_ptr);
1052 /* Record the last visit turn of current floor */
1053 sf_ptr->last_visit = current_world_ptr->game_turn;
1055 forget_lite(creature_ptr->current_floor_ptr);
1056 forget_view(creature_ptr->current_floor_ptr);
1057 clear_mon_lite(creature_ptr->current_floor_ptr);
1059 /* Save current floor */
1060 if (!save_floor(creature_ptr, sf_ptr, 0))
1062 /* Save failed -- No return */
1063 prepare_change_floor_mode(creature_ptr, CFM_NO_RETURN);
1065 /* Kill current floor */
1066 kill_saved_floor(creature_ptr, get_sf_ptr(creature_ptr->floor_id));
1073 * @brief フロアの切り替え処理 / Enter new floor.
1074 * @param creature_ptr プレーヤーへの参照ポインタ
1077 * If the floor is an old saved floor, it will be\n
1078 * restored from the temporary file. If the floor is new one, new floor\n
1079 * will be generated.\n
1081 void change_floor(player_type *creature_ptr)
1083 saved_floor_type *sf_ptr;
1084 bool loaded = FALSE;
1086 /* The dungeon is not ready */
1087 current_world_ptr->character_dungeon = FALSE;
1089 /* No longer in the trap detecteded region */
1090 creature_ptr->dtrap = FALSE;
1092 /* Mega-Hack -- no panel yet */
1098 /* Mega-Hack -- not ambushed on the wildness? */
1099 creature_ptr->ambush_flag = FALSE;
1101 /* No saved floors (On the surface etc.) */
1102 if (!(creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) &&
1103 !(creature_ptr->change_floor_mode & CFM_FIRST_FLOOR))
1105 generate_floor(creature_ptr);
1107 /* Paranoia -- No new saved floor */
1111 /* In the dungeon */
1114 /* No floor_id yet */
1118 new_floor_id = get_new_floor_id(creature_ptr);
1121 /* Pointer for infomations of new floor */
1122 sf_ptr = get_sf_ptr(new_floor_id);
1124 /* Try to restore old floor */
1125 if (sf_ptr->last_visit)
1127 /* Old saved floor is exist */
1128 if (load_floor(creature_ptr, sf_ptr, 0))
1132 /* Forbid return stairs */
1133 if (creature_ptr->change_floor_mode & CFM_NO_RETURN)
1135 grid_type *g_ptr = &creature_ptr->current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
1137 if (!feat_uses_special(g_ptr->feat))
1139 if (creature_ptr->change_floor_mode & (CFM_DOWN | CFM_UP))
1141 /* Reset to floor */
1142 g_ptr->feat = feat_ground_type[randint0(100)];
1152 * Set lower/upper_floor_id of new floor when the new
1153 * floor is right-above/right-under the current floor.
1155 * Stair creation/Teleport level/Trap door will take
1156 * you the same floor when you used it later again.
1158 if (creature_ptr->floor_id)
1160 saved_floor_type *cur_sf_ptr = get_sf_ptr(creature_ptr->floor_id);
1162 if (creature_ptr->change_floor_mode & CFM_UP)
1164 /* New floor is right-above */
1165 if (cur_sf_ptr->upper_floor_id == new_floor_id)
1166 sf_ptr->lower_floor_id = creature_ptr->floor_id;
1168 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1170 /* New floor is right-under */
1171 if (cur_sf_ptr->lower_floor_id == new_floor_id)
1172 sf_ptr->upper_floor_id = creature_ptr->floor_id;
1176 /* Break connection to killed floor */
1179 if (creature_ptr->change_floor_mode & CFM_UP)
1180 sf_ptr->lower_floor_id = 0;
1181 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1182 sf_ptr->upper_floor_id = 0;
1185 /* Maintain monsters and artifacts */
1189 GAME_TURN tmp_last_visit = sf_ptr->last_visit;
1190 GAME_TURN absence_ticks;
1191 int alloc_chance = d_info[creature_ptr->dungeon_idx].max_m_alloc_chance;
1192 GAME_TURN alloc_times;
1194 while (tmp_last_visit > current_world_ptr->game_turn) tmp_last_visit -= TURNS_PER_TICK * TOWN_DAWN;
1195 absence_ticks = (current_world_ptr->game_turn - tmp_last_visit) / TURNS_PER_TICK;
1197 /* Maintain monsters */
1198 for (i = 1; i < creature_ptr->current_floor_ptr->m_max; i++)
1200 monster_race *r_ptr;
1201 monster_type *m_ptr = &creature_ptr->current_floor_ptr->m_list[i];
1203 if (!monster_is_valid(m_ptr)) continue;
1208 m_ptr->hp = m_ptr->maxhp = m_ptr->max_maxhp;
1210 /* Remove timed status (except MTIMED_CSLEEP) */
1211 (void)set_monster_fast(creature_ptr, i, 0);
1212 (void)set_monster_slow(creature_ptr, i, 0);
1213 (void)set_monster_stunned(creature_ptr, i, 0);
1214 (void)set_monster_confused(creature_ptr, i, 0);
1215 (void)set_monster_monfear(creature_ptr, i, 0);
1216 (void)set_monster_invulner(creature_ptr, i, 0, FALSE);
1219 /* Extract real monster race */
1220 r_ptr = real_r_ptr(m_ptr);
1222 /* Ignore non-unique */
1223 if (!(r_ptr->flags1 & RF1_UNIQUE) &&
1224 !(r_ptr->flags7 & RF7_NAZGUL)) continue;
1226 /* Appear at a different floor? */
1227 if (r_ptr->floor_id != new_floor_id)
1229 /* Disapper from here */
1230 delete_monster_idx(creature_ptr, i);
1234 /* Maintain artifatcs */
1235 for (i = 1; i < creature_ptr->current_floor_ptr->o_max; i++)
1237 object_type *o_ptr = &creature_ptr->current_floor_ptr->o_list[i];
1239 if (!OBJECT_IS_VALID(o_ptr)) continue;
1241 /* Ignore non-artifact */
1242 if (!object_is_fixed_artifact(o_ptr)) continue;
1244 /* Appear at a different floor? */
1245 if (a_info[o_ptr->name1].floor_id != new_floor_id)
1247 /* Disappear from here */
1248 delete_object_idx(creature_ptr, i);
1252 /* Cancel preserve */
1253 a_info[o_ptr->name1].cur_num = 1;
1257 (void)place_quest_monsters(creature_ptr);
1259 /* Place some random monsters */
1260 alloc_times = absence_ticks / alloc_chance;
1262 if (randint0(alloc_chance) < (absence_ticks % alloc_chance))
1265 for (i = 0; i < alloc_times; i++)
1267 /* Make a (group of) new monster */
1268 (void)alloc_monster(creature_ptr, 0, 0);
1273 /* New floor_id or failed to restore */
1274 else /* if (!loaded) */
1276 if (sf_ptr->last_visit)
1278 /* Temporary file is broken? */
1279 msg_print(_("階段は行き止まりだった。", "The staircases come to a dead end..."));
1281 /* Create simple dead end */
1282 build_dead_end(creature_ptr);
1284 /* Break connection */
1285 if (creature_ptr->change_floor_mode & CFM_UP)
1287 sf_ptr->upper_floor_id = 0;
1289 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1291 sf_ptr->lower_floor_id = 0;
1296 generate_floor(creature_ptr);
1299 /* Record last visit turn */
1300 sf_ptr->last_visit = current_world_ptr->game_turn;
1302 /* Set correct dun_level value */
1303 sf_ptr->dun_level = creature_ptr->current_floor_ptr->dun_level;
1305 /* Create connected stairs */
1306 if (!(creature_ptr->change_floor_mode & CFM_NO_RETURN))
1308 /* Extract stair position */
1309 grid_type *g_ptr = &creature_ptr->current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
1311 /*** Create connected stairs ***/
1313 /* No stairs down from Quest */
1314 if ((creature_ptr->change_floor_mode & CFM_UP) && !quest_number(creature_ptr, creature_ptr->current_floor_ptr->dun_level))
1316 g_ptr->feat = (creature_ptr->change_floor_mode & CFM_SHAFT) ? feat_state(creature_ptr, feat_down_stair, FF_SHAFT) : feat_down_stair;
1319 /* No stairs up when ironman_downward */
1320 else if ((creature_ptr->change_floor_mode & CFM_DOWN) && !ironman_downward)
1322 g_ptr->feat = (creature_ptr->change_floor_mode & CFM_SHAFT) ? feat_state(creature_ptr, feat_up_stair, FF_SHAFT) : feat_up_stair;
1325 /* Paranoia -- Clear mimic */
1328 /* Connect to previous floor */
1329 g_ptr->special = creature_ptr->floor_id;
1333 /* Arrive at random grid */
1334 if (creature_ptr->change_floor_mode & (CFM_RAND_PLACE))
1336 (void)new_player_spot(creature_ptr);
1339 /* You see stairs blocked */
1340 else if ((creature_ptr->change_floor_mode & CFM_NO_RETURN) && (creature_ptr->change_floor_mode & (CFM_DOWN | CFM_UP)))
1342 if (!creature_ptr->blind)
1344 msg_print(_("突然階段が塞がれてしまった。", "Suddenly the stairs is blocked!"));
1348 msg_print(_("ゴトゴトと何か音がした。", "You hear some noises."));
1355 * The "turn" is not always different number because
1356 * the level teleport doesn't take any turn. Use
1357 * visit mark instead of last visit turn to find the
1358 * oldest saved floor.
1360 sf_ptr->visit_mark = latest_visit_mark++;
1363 /* Place preserved pet monsters */
1364 place_pet(creature_ptr);
1366 /* Reset travel target place */
1367 forget_travel_flow(creature_ptr->current_floor_ptr);
1369 /* Hack -- maintain unique and artifacts */
1370 update_unique_artifact(creature_ptr->current_floor_ptr, new_floor_id);
1372 /* Now the player is in new floor */
1373 creature_ptr->floor_id = new_floor_id;
1375 /* The dungeon is ready */
1376 current_world_ptr->character_dungeon = TRUE;
1378 /* Hack -- Munchkin characters always get whole map */
1379 if (creature_ptr->pseikaku == SEIKAKU_MUNCHKIN)
1380 wiz_lite(creature_ptr, (bool)(creature_ptr->pclass == CLASS_NINJA));
1382 /* Remember when this level was "created" */
1383 creature_ptr->current_floor_ptr->generated_turn = current_world_ptr->game_turn;
1385 /* No dungeon feeling yet */
1386 creature_ptr->feeling_turn = creature_ptr->current_floor_ptr->generated_turn;
1387 creature_ptr->feeling = 0;
1389 /* Clear all flags */
1390 creature_ptr->change_floor_mode = 0L;
1392 select_floor_music(creature_ptr);
1393 creature_ptr->change_floor_mode = 0;