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
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 "uid-checker.h"
38 #include "player-effects.h"
39 #include "player-class.h"
40 #include "player-personality.h"
46 #include "view-mainwindow.h"
48 static FLOOR_IDX new_floor_id; /*!<次のフロアのID / floor_id of the destination */
49 static u32b latest_visit_mark; /*!<フロアを渡った回数?(確認中) / Max number of visit_mark */
50 #define MAX_PARTY_MON 21 /*!< フロア移動時に先のフロアに連れて行けるペットの最大数 Maximum number of preservable pets */
51 static monster_type party_mon[MAX_PARTY_MON]; /*!< フロア移動に保存するペットモンスターの配列 */
54 * Number of floor_id used from birth
56 FLOOR_IDX max_floor_id;
59 * Sign for current process used in temporary files.
60 * Actually it is the start time of current process.
62 u32b saved_floor_file_sign;
65 * @brief 保存フロア配列を初期化する / Initialize saved_floors array.
66 * @param creature_ptr プレーヤーへの参照ポインタ
67 * @param force テンポラリファイルが残っていた場合も警告なしで強制的に削除するフラグ
68 * @details Make sure that old temporary files are not remaining as gurbages.
71 void init_saved_floors(player_type *creature_ptr, bool force)
73 char floor_savefile[1024];
76 BIT_FLAGS mode = 0644;
77 for (i = 0; i < MAX_SAVED_FLOORS; i++)
79 saved_floor_type *sf_ptr = &saved_floors[i];
82 sprintf(floor_savefile, "%s.F%02d", savefile, i);
84 /* Grab permissions */
87 /* Try to create the file */
88 fd = fd_make(floor_savefile, mode);
90 /* Drop permissions */
98 msg_print(_("エラー:古いテンポラリ・ファイルが残っています。", "Error: There are old temporary files."));
99 msg_print(_("変愚蛮怒を二重に起動していないか確認してください。", "Make sure you are not running two game processes simultaneously."));
100 msg_print(_("過去に変愚蛮怒がクラッシュした場合は一時ファイルを", "If the temporary files are garbage from an old crashed process, "));
101 msg_print(_("強制的に削除して実行を続けられます。", "you can delete them safely."));
102 if (!get_check(_("強制的に削除してもよろしいですか?", "Do you delete the old temporary files? ")))
103 quit(_("実行中止", "Aborted."));
113 /* Grab permissions */
116 /* Simply kill the temporary file */
117 (void)fd_kill(floor_savefile);
119 /* Drop permissions */
122 sf_ptr->floor_id = 0;
125 /* No floor_id used yet (No.0 is reserved to indicate non existance) */
128 /* vist_mark is from 1 */
129 latest_visit_mark = 1;
131 /* A sign to mark temporary files */
132 saved_floor_file_sign = (u32b)time(NULL);
134 /* No next floor yet */
137 /* No change floor mode yet */
138 creature_ptr->change_floor_mode = 0;
142 * @brief 保存フロア用テンポラリファイルを削除する / Kill temporary files
143 * @details Should be called just before the game quit.
144 * @param creature_ptr プレーヤーへの参照ポインタ
147 void clear_saved_floor_files(player_type *creature_ptr)
149 char floor_savefile[1024];
151 for (i = 0; i < MAX_SAVED_FLOORS; i++)
153 saved_floor_type *sf_ptr = &saved_floors[i];
155 /* No temporary file */
156 if (!sf_ptr->floor_id) continue;
157 if (sf_ptr->floor_id == creature_ptr->floor_id) continue;
160 sprintf(floor_savefile, "%s.F%02d", savefile, i);
162 /* Grab permissions */
165 /* Simply kill the temporary file */
166 (void)fd_kill(floor_savefile);
168 /* Drop permissions */
174 * @brief 保存フロアIDから参照ポインタを得る / Get a pointer for an item of the saved_floors array.
175 * @param floor_id 保存フロアID
176 * @return IDに対応する保存フロアのポインタ、ない場合はNULLを返す。
178 saved_floor_type *get_sf_ptr(FLOOR_IDX floor_id)
182 /* floor_id No.0 indicates no floor */
183 if (!floor_id) return NULL;
185 for (i = 0; i < MAX_SAVED_FLOORS; i++)
187 saved_floor_type *sf_ptr = &saved_floors[i];
189 if (sf_ptr->floor_id == floor_id) return sf_ptr;
198 * @brief 参照ポインタ先の保存フロアを抹消する / kill a saved floor and get an empty space
199 * @param creature_ptr プレーヤーへの参照ポインタ
200 * @param sf_ptr 保存フロアの参照ポインタ
203 static void kill_saved_floor(player_type *creature_ptr, saved_floor_type *sf_ptr)
205 char floor_savefile[1024];
209 if (!sf_ptr->floor_id) return;
211 if (sf_ptr->floor_id == creature_ptr->floor_id)
213 /* Kill current floor */
214 creature_ptr->floor_id = 0;
216 /* Current floor doesn't have temporary file */
221 sprintf(floor_savefile, "%s.F%02d", savefile, (int)sf_ptr->savefile_id);
223 /* Grab permissions */
226 /* Simply kill the temporary file */
227 (void)fd_kill(floor_savefile);
229 /* Drop permissions */
233 /* No longer exists */
234 sf_ptr->floor_id = 0;
239 * @brief 新規に利用可能な保存フロアを返す / Initialize new saved floor and get its floor id.
240 * @param creature_ptr プレーヤーへの参照ポインタ
241 * @return 利用可能な保存フロアID
243 * If number of saved floors are already MAX_SAVED_FLOORS, kill the oldest one.
245 FLOOR_IDX get_new_floor_id(player_type *creature_ptr)
247 saved_floor_type *sf_ptr = NULL;
250 /* Look for empty space */
251 for (i = 0; i < MAX_SAVED_FLOORS; i++)
253 sf_ptr = &saved_floors[i];
255 if (!sf_ptr->floor_id) break;
259 if (i == MAX_SAVED_FLOORS)
262 u32b oldest_visit = 0xffffffffL;
264 /* Search for oldest */
265 for (i = 0; i < MAX_SAVED_FLOORS; i++)
267 sf_ptr = &saved_floors[i];
269 /* Don't kill current floor */
270 if (sf_ptr->floor_id == creature_ptr->floor_id) continue;
272 /* Don't kill newer */
273 if (sf_ptr->visit_mark > oldest_visit) continue;
276 oldest_visit = sf_ptr->visit_mark;
279 /* Kill oldest saved floor */
280 sf_ptr = &saved_floors[oldest];
281 kill_saved_floor(creature_ptr, sf_ptr);
287 /* Prepare new floor data */
288 sf_ptr->savefile_id = i;
289 sf_ptr->floor_id = max_floor_id;
290 sf_ptr->last_visit = 0;
291 sf_ptr->upper_floor_id = 0;
292 sf_ptr->lower_floor_id = 0;
293 sf_ptr->visit_mark = latest_visit_mark++;
295 /* sf_ptr->dun_level may be changed later */
296 sf_ptr->dun_level = creature_ptr->current_floor_ptr->dun_level;
299 /* Increment number of floor_id */
300 if (max_floor_id < MAX_SHORT) max_floor_id++;
302 /* 32767 floor_ids are all used up! Re-use ancient IDs */
303 else max_floor_id = 1;
305 return sf_ptr->floor_id;
310 * @brief フロア切り替え時の処理フラグを追加する / Prepare mode flags of changing floor
311 * @param creature_ptr プレーヤーへの参照ポインタ
312 * @param mode 追加したい所持フラグ
315 void prepare_change_floor_mode(player_type *creature_ptr, BIT_FLAGS mode)
317 creature_ptr->change_floor_mode |= mode;
321 * @brief 階段移動先のフロアが生成できない時に簡単な行き止まりマップを作成する / Builds the dead end
324 static void build_dead_end(player_type *creature_ptr)
328 clear_cave(creature_ptr);
330 /* Mega-Hack -- no player yet */
331 creature_ptr->x = creature_ptr->y = 0;
333 /* Fill the arrays of floors and walls in the good proportions */
334 set_floor_and_wall(0);
337 creature_ptr->current_floor_ptr->height = SCREEN_HGT;
338 creature_ptr->current_floor_ptr->width = SCREEN_WID;
340 /* Filled with permanent walls */
341 for (y = 0; y < MAX_HGT; y++)
343 for (x = 0; x < MAX_WID; x++)
345 /* Create "solid" perma-wall */
346 place_bold(creature_ptr, y, x, gb_solid_perm);
350 /* Place at center of the floor */
351 creature_ptr->y = creature_ptr->current_floor_ptr->height / 2;
352 creature_ptr->x = creature_ptr->current_floor_ptr->width / 2;
354 /* Give one square */
355 place_bold(creature_ptr, creature_ptr->y, creature_ptr->x, gb_floor);
357 wipe_generate_random_floor_flags(creature_ptr->current_floor_ptr);
362 * @brief フロア移動時のペット保存処理 / Preserve_pets
363 * @param master_ptr プレーヤーへの参照ポインタ
366 static void preserve_pet(player_type *master_ptr)
371 for (num = 0; num < MAX_PARTY_MON; num++)
373 party_mon[num].r_idx = 0;
376 if (master_ptr->riding)
378 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[master_ptr->riding];
380 /* Pet of other pet don't follow. */
381 if (m_ptr->parent_m_idx)
383 master_ptr->riding = 0;
384 master_ptr->pet_extra_flags &= ~(PF_TWO_HANDS);
385 master_ptr->riding_ryoute = master_ptr->old_riding_ryoute = FALSE;
389 /* Preserve the mount */
390 (void)COPY(&party_mon[0], m_ptr, monster_type);
392 /* Delete from this floor */
393 delete_monster_idx(master_ptr, master_ptr->riding);
398 * If player is in wild mode, no pets are preserved
399 * except a monster whom player riding
401 if (!master_ptr->wild_mode && !master_ptr->current_floor_ptr->inside_arena && !master_ptr->phase_out)
403 for (i = master_ptr->current_floor_ptr->m_max - 1, num = 1; (i >= 1 && num < MAX_PARTY_MON); i--)
405 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[i];
407 if (!monster_is_valid(m_ptr)) continue;
408 if (!is_pet(m_ptr)) continue;
409 if (i == master_ptr->riding) continue;
411 if (reinit_wilderness)
413 /* Don't lose sight of pets when getting a Quest */
417 POSITION dis = distance(master_ptr->y, master_ptr->x, m_ptr->fy, m_ptr->fx);
419 /* Confused (etc.) monsters don't follow. */
420 if (MON_CONFUSED(m_ptr) || MON_STUNNED(m_ptr) || MON_CSLEEP(m_ptr)) continue;
422 /* Pet of other pet don't follow. */
423 if (m_ptr->parent_m_idx) continue;
426 * Pets with nickname will follow even from 3 blocks away
427 * when you or the pet can see the other.
429 if (m_ptr->nickname &&
430 ((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)) ||
431 (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))))
433 if (dis > 3) continue;
437 if (dis > 1) continue;
441 (void)COPY(&party_mon[num], &master_ptr->current_floor_ptr->m_list[i], monster_type);
445 /* Delete from this floor */
446 delete_monster_idx(master_ptr, i);
450 if (record_named_pet)
452 for (i = master_ptr->current_floor_ptr->m_max - 1; i >=1; i--)
454 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[i];
455 GAME_TEXT m_name[MAX_NLEN];
457 if (!monster_is_valid(m_ptr)) continue;
458 if (!is_pet(m_ptr)) continue;
459 if (!m_ptr->nickname) continue;
460 if (master_ptr->riding == i) continue;
462 monster_desc(master_ptr, m_name, m_ptr, MD_ASSUME_VISIBLE | MD_INDEF_VISIBLE);
463 exe_write_diary(master_ptr, DIARY_NAMED_PET, RECORD_NAMED_PET_MOVED, m_name);
468 /* Pet of other pet may disappear. */
469 for (i = master_ptr->current_floor_ptr->m_max - 1; i >=1; i--)
471 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[i];
473 /* Are there its parent? */
474 if (m_ptr->parent_m_idx && !master_ptr->current_floor_ptr->m_list[m_ptr->parent_m_idx].r_idx)
476 /* Its parent have gone, it also goes away. */
480 GAME_TEXT m_name[MAX_NLEN];
481 monster_desc(master_ptr, m_name, m_ptr, 0);
482 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
485 delete_monster_idx(master_ptr, i);
492 * @brief フロア移動時にペットを伴った場合の準備処理 / Pre-calculate the racial counters of preserved pets
493 * @param master_ptr プレーヤーへの参照ポインタ
496 * To prevent multiple generation of unique monster who is the minion of player
498 void precalc_cur_num_of_pet(player_type *player_ptr)
502 int max_num = player_ptr->wild_mode ? 1 : MAX_PARTY_MON;
504 for (i = 0; i < max_num; i++)
506 m_ptr = &party_mon[i];
508 /* Skip empty monsters */
509 if (!monster_is_valid(m_ptr)) continue;
511 /* Hack -- Increase the racial counter */
512 real_r_ptr(m_ptr)->cur_num++;
517 * @brief 移動先のフロアに伴ったペットを配置する / Place preserved pet monsters on new floor
518 * @param master_ptr プレーヤーへの参照ポインタ
521 static void place_pet(player_type *master_ptr)
524 int max_num = master_ptr->wild_mode ? 1 : MAX_PARTY_MON;
526 for (i = 0; i < max_num; i++)
528 POSITION cy = 0, cx = 0;
531 if (!(party_mon[i].r_idx)) continue;
535 m_idx = m_pop(master_ptr);
536 master_ptr->riding = m_idx;
548 for (d = 1; d < A_MAX; d++)
550 for (j = 1000; j > 0; j--)
552 scatter(master_ptr, &cy, &cx, master_ptr->y, master_ptr->x, d, 0);
553 if (monster_can_enter(master_ptr, cy, cx, &r_info[party_mon[i].r_idx], 0)) break;
557 m_idx = (d == 6) ? 0 : m_pop(master_ptr);
562 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[m_idx];
565 master_ptr->current_floor_ptr->grid_array[cy][cx].m_idx = m_idx;
567 m_ptr->r_idx = party_mon[i].r_idx;
569 /* Copy all member of the structure */
570 *m_ptr = party_mon[i];
571 r_ptr = real_r_ptr(m_ptr);
575 m_ptr->current_floor_ptr = master_ptr->current_floor_ptr;
577 m_ptr->mtimed[MTIMED_CSLEEP] = 0;
578 m_ptr->hold_o_idx = 0;
581 if ((r_ptr->flags1 & RF1_FORCE_SLEEP) && !ironman_nightmare)
583 /* Monster is still being nice */
584 m_ptr->mflag |= (MFLAG_NICE);
586 /* Must repair monsters */
587 repair_monsters = TRUE;
589 update_monster(master_ptr, m_idx, TRUE);
590 lite_spot(master_ptr, cy, cx);
592 /* Pre-calculated in precalc_cur_num_of_pet() */
593 /* r_ptr->cur_num++; */
595 /* Hack -- Count the number of "reproducers" */
596 if (r_ptr->flags2 & RF2_MULTIPLY) master_ptr->current_floor_ptr->num_repro++;
601 monster_type *m_ptr = &party_mon[i];
602 monster_race *r_ptr = real_r_ptr(m_ptr);
603 GAME_TEXT m_name[MAX_NLEN];
605 monster_desc(master_ptr, m_name, m_ptr, 0);
606 msg_format(_("%sとはぐれてしまった。", "You have lost sight of %s."), m_name);
607 if (record_named_pet && m_ptr->nickname)
609 monster_desc(master_ptr, m_name, m_ptr, MD_INDEF_VISIBLE);
610 exe_write_diary(master_ptr, DIARY_NAMED_PET, RECORD_NAMED_PET_LOST_SIGHT, m_name);
613 /* Pre-calculated in precalc_cur_num_of_pet(), but need to decrease */
614 if (r_ptr->cur_num) r_ptr->cur_num--;
618 /* For accuracy of precalc_cur_num_of_pet() */
619 (void)C_WIPE(party_mon, MAX_PARTY_MON, monster_type);
624 * @brief ユニークモンスターやアーティファクトの所在フロアを更新する / Hack -- Update location of unique monsters and artifacts
625 * @param cur_floor_id 現在のフロアID
628 * The r_ptr->floor_id and a_ptr->floor_id are not updated correctly\n
629 * while new floor creation since dungeons may be re-created by\n
630 * auto-scum option.\n
632 static void update_unique_artifact(floor_type *floor_ptr, s16b cur_floor_id)
636 /* Maintain unique monsters */
637 for (i = 1; i < floor_ptr->m_max; i++)
640 monster_type *m_ptr = &floor_ptr->m_list[i];
642 if (!monster_is_valid(m_ptr)) continue;
644 /* Extract real monster race */
645 r_ptr = real_r_ptr(m_ptr);
647 /* Memorize location of the unique monster */
648 if ((r_ptr->flags1 & RF1_UNIQUE) ||
649 (r_ptr->flags7 & RF7_NAZGUL))
651 r_ptr->floor_id = cur_floor_id;
655 /* Maintain artifatcs */
656 for (i = 1; i < floor_ptr->o_max; i++)
658 object_type *o_ptr = &floor_ptr->o_list[i];
660 if (!OBJECT_IS_VALID(o_ptr)) continue;
662 /* Memorize location of the artifact */
663 if (object_is_fixed_artifact(o_ptr))
665 a_info[o_ptr->name1].floor_id = cur_floor_id;
672 * @brief フロア移動時、プレイヤーの移動先モンスターが既にいた場合ランダムな近隣に移動させる / When a monster is at a place where player will return,
675 static void get_out_monster(player_type *protected_ptr)
679 POSITION oy = protected_ptr->y;
680 POSITION ox = protected_ptr->x;
681 floor_type *floor_ptr = protected_ptr->current_floor_ptr;
682 MONSTER_IDX m_idx = floor_ptr->grid_array[oy][ox].m_idx;
684 /* Nothing to do if no monster */
687 /* Look until done */
692 /* Pick a (possibly illegal) location */
693 POSITION ny = rand_spread(oy, dis);
694 POSITION nx = rand_spread(ox, dis);
698 /* Stop after 1000 tries */
699 if (tries > 10000) return;
702 * Increase distance after doing enough tries
703 * compared to area of possible space
705 if (tries > 20 * dis * dis) dis++;
707 /* Ignore illegal locations */
708 if (!in_bounds(floor_ptr, ny, nx)) continue;
710 /* Require "empty" floor space */
711 if (!is_cave_empty_bold(protected_ptr, ny, nx)) continue;
713 /* Hack -- no teleport onto glyph of warding */
714 if (is_glyph_grid(&floor_ptr->grid_array[ny][nx])) continue;
715 if (is_explosive_rune_grid(&floor_ptr->grid_array[ny][nx])) continue;
717 /* ...nor onto the Pattern */
718 if (pattern_tile(floor_ptr, ny, nx)) continue;
720 /*** It's a good place ***/
722 m_ptr = &floor_ptr->m_list[m_idx];
724 /* Update the old location */
725 floor_ptr->grid_array[oy][ox].m_idx = 0;
727 /* Update the new location */
728 floor_ptr->grid_array[ny][nx].m_idx = m_idx;
730 /* Move the monster */
734 /* No need to do update_monster() */
742 * @brief 新フロアに移動元フロアに繋がる階段を配置する / Virtually teleport onto the stairs that is connecting between two floors.
743 * @param sf_ptr 移動元の保存フロア構造体参照ポインタ
746 static void locate_connected_stairs(player_type *creature_ptr, floor_type *floor_ptr, saved_floor_type *sf_ptr, BIT_FLAGS floor_mode)
748 POSITION x, y, sx = 0, sy = 0;
749 POSITION x_table[20];
750 POSITION y_table[20];
754 /* Search usable stairs */
755 for (y = 0; y < floor_ptr->height; y++)
757 for (x = 0; x < floor_ptr->width; x++)
759 grid_type *g_ptr = &floor_ptr->grid_array[y][x];
760 feature_type *f_ptr = &f_info[g_ptr->feat];
763 if (floor_mode & CFM_UP)
765 if (have_flag(f_ptr->flags, FF_LESS) && have_flag(f_ptr->flags, FF_STAIRS) &&
766 !have_flag(f_ptr->flags, FF_SPECIAL))
770 /* Found fixed stairs? */
771 if (g_ptr->special &&
772 g_ptr->special == sf_ptr->upper_floor_id)
780 else if (floor_mode & CFM_DOWN)
782 if (have_flag(f_ptr->flags, FF_MORE) && have_flag(f_ptr->flags, FF_STAIRS) &&
783 !have_flag(f_ptr->flags, FF_SPECIAL))
787 /* Found fixed stairs */
788 if (g_ptr->special &&
789 g_ptr->special == sf_ptr->lower_floor_id)
799 if (have_flag(f_ptr->flags, FF_BLDG))
805 if (ok && (num < 20))
817 creature_ptr->y = sy;
818 creature_ptr->x = sx;
822 /* No stairs found! -- No return */
823 prepare_change_floor_mode(creature_ptr, CFM_RAND_PLACE | CFM_NO_RETURN);
825 /* Mega Hack -- It's not the stairs you enter. Disable it. */
826 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;
830 /* Choose random one */
833 /* Point stair location */
834 creature_ptr->y = y_table[i];
835 creature_ptr->x = x_table[i];
840 * @brief 現在のフロアを離れるに伴って行なわれる保存処理
841 * / Maintain quest monsters, mark next floor_id at stairs, save current floor, and prepare to enter next floor.
842 * @param creature_ptr プレーヤーへの参照ポインタ
845 void leave_floor(player_type *creature_ptr)
847 grid_type *g_ptr = NULL;
849 saved_floor_type *sf_ptr;
850 MONRACE_IDX quest_r_idx = 0;
852 FLOOR_IDX tmp_floor_idx = 0;
854 /* Preserve pets and prepare to take these to next floor */
855 preserve_pet(creature_ptr);
857 /* Remove all mirrors without explosion */
858 remove_all_mirrors(creature_ptr, FALSE);
860 if (creature_ptr->special_defense & NINJA_S_STEALTH) set_superstealth(creature_ptr, FALSE);
862 /* New floor is not yet prepared */
865 /* Temporarily get a floor_id (for Arena) */
866 if (!creature_ptr->floor_id &&
867 (creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) &&
868 !(creature_ptr->change_floor_mode & CFM_NO_RETURN))
870 /* Get temporary floor_id */
871 tmp_floor_idx = get_new_floor_id(creature_ptr);
874 /* Search the quest monster index */
875 for (i = 0; i < max_q_idx; i++)
877 if ((quest[i].status == QUEST_STATUS_TAKEN) &&
878 ((quest[i].type == QUEST_TYPE_KILL_LEVEL) ||
879 (quest[i].type == QUEST_TYPE_RANDOM)) &&
880 (quest[i].level == creature_ptr->current_floor_ptr->dun_level) &&
881 (creature_ptr->dungeon_idx == quest[i].dungeon) &&
882 !(quest[i].flags & QUEST_FLAG_PRESET))
884 quest_r_idx = quest[i].r_idx;
888 /* Maintain quest monsters */
889 for (i = 1; i < creature_ptr->current_floor_ptr->m_max; i++)
892 monster_type *m_ptr = &creature_ptr->current_floor_ptr->m_list[i];
894 if (!monster_is_valid(m_ptr)) continue;
896 /* Only maintain quest monsters */
897 if (quest_r_idx != m_ptr->r_idx) continue;
899 /* Extract real monster race */
900 r_ptr = real_r_ptr(m_ptr);
902 /* Ignore unique monsters */
903 if ((r_ptr->flags1 & RF1_UNIQUE) ||
904 (r_ptr->flags7 & RF7_NAZGUL)) continue;
906 /* Delete non-unique quest monsters */
907 delete_monster_idx(creature_ptr, i);
910 /* Check if there is a same item */
911 for (i = 0; i < INVEN_PACK; i++)
913 object_type *o_ptr = &creature_ptr->inventory_list[i];
915 if (!OBJECT_IS_VALID(o_ptr)) continue;
917 /* Delete old memorized location of the artifact */
918 if (object_is_fixed_artifact(o_ptr))
920 a_info[o_ptr->name1].floor_id = 0;
924 /* Extract current floor info or NULL */
925 sf_ptr = get_sf_ptr(tmp_floor_idx);
927 /* Choose random stairs */
928 if ((creature_ptr->change_floor_mode & CFM_RAND_CONNECT) && tmp_floor_idx)
930 locate_connected_stairs(creature_ptr, creature_ptr->current_floor_ptr, sf_ptr, creature_ptr->change_floor_mode);
933 /* Extract new dungeon level */
934 if (creature_ptr->change_floor_mode & CFM_SAVE_FLOORS)
936 /* Extract stair position */
937 g_ptr = &creature_ptr->current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
938 f_ptr = &f_info[g_ptr->feat];
940 /* Get back to old saved floor? */
941 if (g_ptr->special && !have_flag(f_ptr->flags, FF_SPECIAL) && get_sf_ptr(g_ptr->special))
943 /* Saved floor is exist. Use it. */
944 new_floor_id = g_ptr->special;
947 /* Mark shaft up/down */
948 if (have_flag(f_ptr->flags, FF_STAIRS) && have_flag(f_ptr->flags, FF_SHAFT))
950 prepare_change_floor_mode(creature_ptr, CFM_SHAFT);
954 /* Climb up/down some sort of stairs */
955 if (creature_ptr->change_floor_mode & (CFM_DOWN | CFM_UP))
959 /* Extract level movement number */
960 if (creature_ptr->change_floor_mode & CFM_DOWN) move_num = 1;
961 else if (creature_ptr->change_floor_mode & CFM_UP) move_num = -1;
963 /* Shafts are deeper than normal stairs */
964 if (creature_ptr->change_floor_mode & CFM_SHAFT)
965 move_num += SGN(move_num);
967 /* Get out from or Enter the dungeon */
968 if (creature_ptr->change_floor_mode & CFM_DOWN)
970 if (!creature_ptr->current_floor_ptr->dun_level)
971 move_num = d_info[creature_ptr->dungeon_idx].mindepth;
973 else if (creature_ptr->change_floor_mode & CFM_UP)
975 if (creature_ptr->current_floor_ptr->dun_level + move_num < d_info[creature_ptr->dungeon_idx].mindepth)
976 move_num = -creature_ptr->current_floor_ptr->dun_level;
979 creature_ptr->current_floor_ptr->dun_level += move_num;
982 /* Leaving the dungeon to town */
983 if (!creature_ptr->current_floor_ptr->dun_level && creature_ptr->dungeon_idx)
985 creature_ptr->leaving_dungeon = TRUE;
986 if (!vanilla_town && !lite_town)
988 creature_ptr->wilderness_y = d_info[creature_ptr->dungeon_idx].dy;
989 creature_ptr->wilderness_x = d_info[creature_ptr->dungeon_idx].dx;
991 creature_ptr->recall_dungeon = creature_ptr->dungeon_idx;
992 creature_ptr->dungeon_idx = 0;
994 /* Reach to the surface -- Clear all saved floors */
995 creature_ptr->change_floor_mode &= ~CFM_SAVE_FLOORS; // TODO
998 /* Kill some old saved floors */
999 if (!(creature_ptr->change_floor_mode & CFM_SAVE_FLOORS))
1001 /* Kill all saved floors */
1002 for (i = 0; i < MAX_SAVED_FLOORS; i++)
1003 kill_saved_floor(creature_ptr, &saved_floors[i]);
1005 /* Reset visit_mark count */
1006 latest_visit_mark = 1;
1008 else if (creature_ptr->change_floor_mode & CFM_NO_RETURN)
1010 /* Kill current floor */
1011 kill_saved_floor(creature_ptr, sf_ptr);
1014 /* No current floor -- Left/Enter dungeon etc... */
1017 /* No longer need to save current floor */
1021 /* Mark next floor_id on the previous floor */
1025 new_floor_id = get_new_floor_id(creature_ptr);
1027 /* Connect from here */
1028 if (g_ptr && !feat_uses_special(g_ptr->feat))
1030 g_ptr->special = tmp_floor_idx;
1034 /* Fix connection -- level teleportation or trap door */
1035 if (creature_ptr->change_floor_mode & CFM_RAND_CONNECT)
1037 if (creature_ptr->change_floor_mode & CFM_UP)
1038 sf_ptr->upper_floor_id = new_floor_id;
1039 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1040 sf_ptr->lower_floor_id = new_floor_id;
1043 /* If you can return, you need to save previous floor */
1044 if ((creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) &&
1045 !(creature_ptr->change_floor_mode & CFM_NO_RETURN))
1047 /* Get out of the my way! */
1048 get_out_monster(creature_ptr);
1050 /* Record the last visit turn of current floor */
1051 sf_ptr->last_visit = current_world_ptr->game_turn;
1053 forget_lite(creature_ptr->current_floor_ptr);
1054 forget_view(creature_ptr->current_floor_ptr);
1055 clear_mon_lite(creature_ptr->current_floor_ptr);
1057 /* Save current floor */
1058 if (!save_floor(creature_ptr, sf_ptr, 0))
1060 /* Save failed -- No return */
1061 prepare_change_floor_mode(creature_ptr, CFM_NO_RETURN);
1063 /* Kill current floor */
1064 kill_saved_floor(creature_ptr, get_sf_ptr(creature_ptr->floor_id));
1071 * @brief フロアの切り替え処理 / Enter new floor.
1072 * @param creature_ptr プレーヤーへの参照ポインタ
1075 * If the floor is an old saved floor, it will be\n
1076 * restored from the temporary file. If the floor is new one, new floor\n
1077 * will be generated.\n
1079 void change_floor(player_type *creature_ptr)
1081 saved_floor_type *sf_ptr;
1082 bool loaded = FALSE;
1084 /* The dungeon is not ready */
1085 current_world_ptr->character_dungeon = FALSE;
1087 /* No longer in the trap detecteded region */
1088 creature_ptr->dtrap = FALSE;
1090 /* Mega-Hack -- no panel yet */
1096 /* Mega-Hack -- not ambushed on the wildness? */
1097 creature_ptr->ambush_flag = FALSE;
1099 /* No saved floors (On the surface etc.) */
1100 if (!(creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) &&
1101 !(creature_ptr->change_floor_mode & CFM_FIRST_FLOOR))
1103 generate_floor(creature_ptr);
1105 /* Paranoia -- No new saved floor */
1109 /* In the dungeon */
1112 /* No floor_id yet */
1116 new_floor_id = get_new_floor_id(creature_ptr);
1119 /* Pointer for infomations of new floor */
1120 sf_ptr = get_sf_ptr(new_floor_id);
1122 /* Try to restore old floor */
1123 if (sf_ptr->last_visit)
1125 /* Old saved floor is exist */
1126 if (load_floor(creature_ptr, sf_ptr, 0))
1130 /* Forbid return stairs */
1131 if (creature_ptr->change_floor_mode & CFM_NO_RETURN)
1133 grid_type *g_ptr = &creature_ptr->current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
1135 if (!feat_uses_special(g_ptr->feat))
1137 if (creature_ptr->change_floor_mode & (CFM_DOWN | CFM_UP))
1139 /* Reset to floor */
1140 g_ptr->feat = feat_ground_type[randint0(100)];
1150 * Set lower/upper_floor_id of new floor when the new
1151 * floor is right-above/right-under the current floor.
1153 * Stair creation/Teleport level/Trap door will take
1154 * you the same floor when you used it later again.
1156 if (creature_ptr->floor_id)
1158 saved_floor_type *cur_sf_ptr = get_sf_ptr(creature_ptr->floor_id);
1160 if (creature_ptr->change_floor_mode & CFM_UP)
1162 /* New floor is right-above */
1163 if (cur_sf_ptr->upper_floor_id == new_floor_id)
1164 sf_ptr->lower_floor_id = creature_ptr->floor_id;
1166 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1168 /* New floor is right-under */
1169 if (cur_sf_ptr->lower_floor_id == new_floor_id)
1170 sf_ptr->upper_floor_id = creature_ptr->floor_id;
1174 /* Break connection to killed floor */
1177 if (creature_ptr->change_floor_mode & CFM_UP)
1178 sf_ptr->lower_floor_id = 0;
1179 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1180 sf_ptr->upper_floor_id = 0;
1183 /* Maintain monsters and artifacts */
1187 GAME_TURN tmp_last_visit = sf_ptr->last_visit;
1188 GAME_TURN absence_ticks;
1189 int alloc_chance = d_info[creature_ptr->dungeon_idx].max_m_alloc_chance;
1190 GAME_TURN alloc_times;
1192 while (tmp_last_visit > current_world_ptr->game_turn) tmp_last_visit -= TURNS_PER_TICK * TOWN_DAWN;
1193 absence_ticks = (current_world_ptr->game_turn - tmp_last_visit) / TURNS_PER_TICK;
1195 /* Maintain monsters */
1196 for (i = 1; i < creature_ptr->current_floor_ptr->m_max; i++)
1198 monster_race *r_ptr;
1199 monster_type *m_ptr = &creature_ptr->current_floor_ptr->m_list[i];
1201 if (!monster_is_valid(m_ptr)) continue;
1206 m_ptr->hp = m_ptr->maxhp = m_ptr->max_maxhp;
1208 /* Remove timed status (except MTIMED_CSLEEP) */
1209 (void)set_monster_fast(creature_ptr, i, 0);
1210 (void)set_monster_slow(creature_ptr, i, 0);
1211 (void)set_monster_stunned(creature_ptr, i, 0);
1212 (void)set_monster_confused(creature_ptr, i, 0);
1213 (void)set_monster_monfear(creature_ptr, i, 0);
1214 (void)set_monster_invulner(creature_ptr, i, 0, FALSE);
1217 /* Extract real monster race */
1218 r_ptr = real_r_ptr(m_ptr);
1220 /* Ignore non-unique */
1221 if (!(r_ptr->flags1 & RF1_UNIQUE) &&
1222 !(r_ptr->flags7 & RF7_NAZGUL)) continue;
1224 /* Appear at a different floor? */
1225 if (r_ptr->floor_id != new_floor_id)
1227 /* Disapper from here */
1228 delete_monster_idx(creature_ptr, i);
1232 /* Maintain artifatcs */
1233 for (i = 1; i < creature_ptr->current_floor_ptr->o_max; i++)
1235 object_type *o_ptr = &creature_ptr->current_floor_ptr->o_list[i];
1237 if (!OBJECT_IS_VALID(o_ptr)) continue;
1239 /* Ignore non-artifact */
1240 if (!object_is_fixed_artifact(o_ptr)) continue;
1242 /* Appear at a different floor? */
1243 if (a_info[o_ptr->name1].floor_id != new_floor_id)
1245 /* Disappear from here */
1246 delete_object_idx(creature_ptr, i);
1250 /* Cancel preserve */
1251 a_info[o_ptr->name1].cur_num = 1;
1255 (void)place_quest_monsters(creature_ptr);
1257 /* Place some random monsters */
1258 alloc_times = absence_ticks / alloc_chance;
1260 if (randint0(alloc_chance) < (absence_ticks % alloc_chance))
1263 for (i = 0; i < alloc_times; i++)
1265 /* Make a (group of) new monster */
1266 (void)alloc_monster(creature_ptr, 0, 0);
1271 /* New floor_id or failed to restore */
1272 else /* if (!loaded) */
1274 if (sf_ptr->last_visit)
1276 /* Temporary file is broken? */
1277 msg_print(_("階段は行き止まりだった。", "The staircases come to a dead end..."));
1279 /* Create simple dead end */
1280 build_dead_end(creature_ptr);
1282 /* Break connection */
1283 if (creature_ptr->change_floor_mode & CFM_UP)
1285 sf_ptr->upper_floor_id = 0;
1287 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1289 sf_ptr->lower_floor_id = 0;
1294 generate_floor(creature_ptr);
1297 /* Record last visit turn */
1298 sf_ptr->last_visit = current_world_ptr->game_turn;
1300 /* Set correct dun_level value */
1301 sf_ptr->dun_level = creature_ptr->current_floor_ptr->dun_level;
1303 /* Create connected stairs */
1304 if (!(creature_ptr->change_floor_mode & CFM_NO_RETURN))
1306 /* Extract stair position */
1307 grid_type *g_ptr = &creature_ptr->current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
1309 /*** Create connected stairs ***/
1311 /* No stairs down from Quest */
1312 if ((creature_ptr->change_floor_mode & CFM_UP) && !quest_number(creature_ptr, creature_ptr->current_floor_ptr->dun_level))
1314 g_ptr->feat = (creature_ptr->change_floor_mode & CFM_SHAFT) ? feat_state(creature_ptr, feat_down_stair, FF_SHAFT) : feat_down_stair;
1317 /* No stairs up when ironman_downward */
1318 else if ((creature_ptr->change_floor_mode & CFM_DOWN) && !ironman_downward)
1320 g_ptr->feat = (creature_ptr->change_floor_mode & CFM_SHAFT) ? feat_state(creature_ptr, feat_up_stair, FF_SHAFT) : feat_up_stair;
1323 /* Paranoia -- Clear mimic */
1326 /* Connect to previous floor */
1327 g_ptr->special = creature_ptr->floor_id;
1331 /* Arrive at random grid */
1332 if (creature_ptr->change_floor_mode & (CFM_RAND_PLACE))
1334 (void)new_player_spot(creature_ptr);
1337 /* You see stairs blocked */
1338 else if ((creature_ptr->change_floor_mode & CFM_NO_RETURN) && (creature_ptr->change_floor_mode & (CFM_DOWN | CFM_UP)))
1340 if (!creature_ptr->blind)
1342 msg_print(_("突然階段が塞がれてしまった。", "Suddenly the stairs is blocked!"));
1346 msg_print(_("ゴトゴトと何か音がした。", "You hear some noises."));
1353 * The "turn" is not always different number because
1354 * the level teleport doesn't take any turn. Use
1355 * visit mark instead of last visit turn to find the
1356 * oldest saved floor.
1358 sf_ptr->visit_mark = latest_visit_mark++;
1361 /* Place preserved pet monsters */
1362 place_pet(creature_ptr);
1364 /* Reset travel target place */
1365 forget_travel_flow(creature_ptr->current_floor_ptr);
1367 /* Hack -- maintain unique and artifacts */
1368 update_unique_artifact(creature_ptr->current_floor_ptr, new_floor_id);
1370 /* Now the player is in new floor */
1371 creature_ptr->floor_id = new_floor_id;
1373 /* The dungeon is ready */
1374 current_world_ptr->character_dungeon = TRUE;
1376 /* Hack -- Munchkin characters always get whole map */
1377 if (creature_ptr->pseikaku == SEIKAKU_MUNCHKIN)
1378 wiz_lite(creature_ptr, (bool)(creature_ptr->pclass == CLASS_NINJA));
1380 /* Remember when this level was "created" */
1381 creature_ptr->current_floor_ptr->generated_turn = current_world_ptr->game_turn;
1383 /* No dungeon feeling yet */
1384 creature_ptr->feeling_turn = creature_ptr->current_floor_ptr->generated_turn;
1385 creature_ptr->feeling = 0;
1387 /* Clear all flags */
1388 creature_ptr->change_floor_mode = 0L;
1390 select_floor_music(creature_ptr);
1391 creature_ptr->change_floor_mode = 0;