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 "core/asking-player.h"
17 #include "dungeon/dungeon.h"
18 #include "dungeon/quest.h"
19 #include "floor/floor-events.h"
20 #include "floor/floor-generate.h"
21 #include "floor/floor-object.h"
22 #include "floor/floor.h"
23 #include "floor/geometry.h"
24 #include "floor/wild.h"
25 #include "game-option/birth-options.h"
26 #include "game-option/play-record-options.h"
27 #include "grid/feature.h"
28 #include "grid/grid.h"
29 #include "io/files-util.h"
32 #include "io/uid-checker.h"
33 #include "io/write-diary.h"
34 #include "main/sound-of-music.h"
35 #include "mind/mind-mirror-master.h"
36 #include "mind/mind-ninja.h"
37 #include "monster-floor/monster-generator.h"
38 #include "monster-floor/monster-remover.h"
39 #include "monster-floor/monster-summon.h"
40 #include "monster-race/monster-race.h"
41 #include "monster-race/race-flags1.h"
42 #include "monster-race/race-flags2.h"
43 #include "monster-race/race-flags7.h"
44 #include "monster/monster-describer.h"
45 #include "monster/monster-description-types.h"
46 #include "monster/monster-flag-types.h"
47 #include "monster/monster-info.h"
48 #include "monster/monster-list.h"
49 #include "monster/monster-status.h"
50 #include "monster/monster-update.h"
51 #include "monster/smart-learn-types.h"
52 #include "object-enchant/artifact.h"
53 #include "object-hook/hook-checker.h"
54 #include "object-hook/hook-enchant.h"
55 #include "pet/pet-util.h"
56 #include "player/player-class.h"
57 #include "player/player-personalities-types.h"
58 #include "player/special-defense-types.h"
59 #include "spell-kind/spells-floor.h"
60 #include "system/system-variables.h"
61 #include "util/angband-files.h"
62 #include "util/bit-flags-calculator.h"
63 #include "view/display-messages.h"
64 #include "view/main-window-util.h"
65 #include "world/world.h"
69 static FLOOR_IDX new_floor_id; /*!<次のフロアのID / floor_id of the destination */
70 static u32b latest_visit_mark; /*!<フロアを渡った回数?(確認中) / Max number of visit_mark */
71 #define MAX_PARTY_MON 21 /*!< フロア移動時に先のフロアに連れて行けるペットの最大数 Maximum number of preservable pets */
72 static monster_type party_mon[MAX_PARTY_MON]; /*!< フロア移動に保存するペットモンスターの配列 */
75 * Number of floor_id used from birth
77 FLOOR_IDX max_floor_id;
80 * Sign for current process used in temporary files.
81 * Actually it is the start time of current process.
83 u32b saved_floor_file_sign;
86 * @brief 保存フロア配列を初期化する / Initialize saved_floors array.
87 * @param creature_ptr プレーヤーへの参照ポインタ
88 * @param force テンポラリファイルが残っていた場合も警告なしで強制的に削除するフラグ
89 * @details Make sure that old temporary files are not remaining as gurbages.
92 void init_saved_floors(player_type *creature_ptr, bool force)
94 char floor_savefile[1024];
97 BIT_FLAGS mode = 0644;
98 for (i = 0; i < MAX_SAVED_FLOORS; i++)
100 saved_floor_type *sf_ptr = &saved_floors[i];
103 sprintf(floor_savefile, "%s.F%02d", savefile, i);
105 /* Grab permissions */
106 safe_setuid_grab(creature_ptr);
108 /* Try to create the file */
109 fd = fd_make(floor_savefile, mode);
111 /* Drop permissions */
119 msg_print(_("エラー:古いテンポラリ・ファイルが残っています。", "Error: There are old temporary files."));
120 msg_print(_("変愚蛮怒を二重に起動していないか確認してください。", "Make sure you are not running two game processes simultaneously."));
121 msg_print(_("過去に変愚蛮怒がクラッシュした場合は一時ファイルを", "If the temporary files are garbage from an old crashed process, "));
122 msg_print(_("強制的に削除して実行を続けられます。", "you can delete them safely."));
123 if (!get_check(_("強制的に削除してもよろしいですか?", "Do you delete the old temporary files? ")))
124 quit(_("実行中止", "Aborted."));
134 /* Grab permissions */
135 safe_setuid_grab(creature_ptr);
137 /* Simply kill the temporary file */
138 (void)fd_kill(floor_savefile);
140 /* Drop permissions */
143 sf_ptr->floor_id = 0;
146 /* No floor_id used yet (No.0 is reserved to indicate non existance) */
149 /* vist_mark is from 1 */
150 latest_visit_mark = 1;
152 /* A sign to mark temporary files */
153 saved_floor_file_sign = (u32b)time(NULL);
155 /* No next floor yet */
158 /* No change floor mode yet */
159 creature_ptr->change_floor_mode = 0;
163 * @brief 保存フロア用テンポラリファイルを削除する / Kill temporary files
164 * @details Should be called just before the game quit.
165 * @param creature_ptr プレーヤーへの参照ポインタ
168 void clear_saved_floor_files(player_type *creature_ptr)
170 char floor_savefile[1024];
172 for (i = 0; i < MAX_SAVED_FLOORS; i++)
174 saved_floor_type *sf_ptr = &saved_floors[i];
176 /* No temporary file */
177 if (!sf_ptr->floor_id) continue;
178 if (sf_ptr->floor_id == creature_ptr->floor_id) continue;
181 sprintf(floor_savefile, "%s.F%02d", savefile, i);
183 /* Grab permissions */
184 safe_setuid_grab(creature_ptr);
186 /* Simply kill the temporary file */
187 (void)fd_kill(floor_savefile);
189 /* Drop permissions */
195 * @brief 保存フロアIDから参照ポインタを得る / Get a pointer for an item of the saved_floors array.
196 * @param floor_id 保存フロアID
197 * @return IDに対応する保存フロアのポインタ、ない場合はNULLを返す。
199 saved_floor_type *get_sf_ptr(FLOOR_IDX floor_id)
203 /* floor_id No.0 indicates no floor */
204 if (!floor_id) return NULL;
206 for (i = 0; i < MAX_SAVED_FLOORS; i++)
208 saved_floor_type *sf_ptr = &saved_floors[i];
210 if (sf_ptr->floor_id == floor_id) return sf_ptr;
219 * @brief 参照ポインタ先の保存フロアを抹消する / kill a saved floor and get an empty space
220 * @param creature_ptr プレーヤーへの参照ポインタ
221 * @param sf_ptr 保存フロアの参照ポインタ
224 static void kill_saved_floor(player_type *creature_ptr, saved_floor_type *sf_ptr)
226 char floor_savefile[1024];
230 if (!sf_ptr->floor_id) return;
232 if (sf_ptr->floor_id == creature_ptr->floor_id)
234 /* Kill current floor */
235 creature_ptr->floor_id = 0;
237 /* Current floor doesn't have temporary file */
242 sprintf(floor_savefile, "%s.F%02d", savefile, (int)sf_ptr->savefile_id);
244 /* Grab permissions */
245 safe_setuid_grab(creature_ptr);
247 /* Simply kill the temporary file */
248 (void)fd_kill(floor_savefile);
250 /* Drop permissions */
254 /* No longer exists */
255 sf_ptr->floor_id = 0;
260 * @brief 新規に利用可能な保存フロアを返す / Initialize new saved floor and get its floor id.
261 * @param creature_ptr プレーヤーへの参照ポインタ
262 * @return 利用可能な保存フロアID
264 * If number of saved floors are already MAX_SAVED_FLOORS, kill the oldest one.
266 FLOOR_IDX get_new_floor_id(player_type *creature_ptr)
268 saved_floor_type *sf_ptr = NULL;
271 /* Look for empty space */
272 for (i = 0; i < MAX_SAVED_FLOORS; i++)
274 sf_ptr = &saved_floors[i];
276 if (!sf_ptr->floor_id) break;
280 if (i == MAX_SAVED_FLOORS)
283 u32b oldest_visit = 0xffffffffL;
285 /* Search for oldest */
286 for (i = 0; i < MAX_SAVED_FLOORS; i++)
288 sf_ptr = &saved_floors[i];
290 /* Don't kill current floor */
291 if (sf_ptr->floor_id == creature_ptr->floor_id) continue;
293 /* Don't kill newer */
294 if (sf_ptr->visit_mark > oldest_visit) continue;
297 oldest_visit = sf_ptr->visit_mark;
300 /* Kill oldest saved floor */
301 sf_ptr = &saved_floors[oldest];
302 kill_saved_floor(creature_ptr, sf_ptr);
308 /* Prepare new floor data */
309 sf_ptr->savefile_id = i;
310 sf_ptr->floor_id = max_floor_id;
311 sf_ptr->last_visit = 0;
312 sf_ptr->upper_floor_id = 0;
313 sf_ptr->lower_floor_id = 0;
314 sf_ptr->visit_mark = latest_visit_mark++;
316 /* sf_ptr->dun_level may be changed later */
317 sf_ptr->dun_level = creature_ptr->current_floor_ptr->dun_level;
320 /* Increment number of floor_id */
321 if (max_floor_id < MAX_SHORT) max_floor_id++;
323 /* 32767 floor_ids are all used up! Re-use ancient IDs */
324 else max_floor_id = 1;
326 return sf_ptr->floor_id;
331 * @brief フロア切り替え時の処理フラグを追加する / Prepare mode flags of changing floor
332 * @param creature_ptr プレーヤーへの参照ポインタ
333 * @param mode 追加したい所持フラグ
336 void prepare_change_floor_mode(player_type *creature_ptr, BIT_FLAGS mode)
338 creature_ptr->change_floor_mode |= mode;
342 * @brief 階段移動先のフロアが生成できない時に簡単な行き止まりマップを作成する / Builds the dead end
345 static void build_dead_end(player_type *creature_ptr)
349 clear_cave(creature_ptr);
351 /* Mega-Hack -- no player yet */
352 creature_ptr->x = creature_ptr->y = 0;
354 /* Fill the arrays of floors and walls in the good proportions */
355 set_floor_and_wall(0);
358 creature_ptr->current_floor_ptr->height = SCREEN_HGT;
359 creature_ptr->current_floor_ptr->width = SCREEN_WID;
361 /* Filled with permanent walls */
362 for (y = 0; y < MAX_HGT; y++)
364 for (x = 0; x < MAX_WID; x++)
366 /* Create "solid" perma-wall */
367 place_bold(creature_ptr, y, x, GB_SOLID_PERM);
371 /* Place at center of the floor */
372 creature_ptr->y = creature_ptr->current_floor_ptr->height / 2;
373 creature_ptr->x = creature_ptr->current_floor_ptr->width / 2;
375 /* Give one square */
376 place_bold(creature_ptr, creature_ptr->y, creature_ptr->x, GB_FLOOR);
378 wipe_generate_random_floor_flags(creature_ptr->current_floor_ptr);
383 * @brief フロア移動時のペット保存処理 / Preserve_pets
384 * @param master_ptr プレーヤーへの参照ポインタ
387 static void preserve_pet(player_type *master_ptr)
392 for (num = 0; num < MAX_PARTY_MON; num++)
394 party_mon[num].r_idx = 0;
397 if (master_ptr->riding)
399 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[master_ptr->riding];
401 /* Pet of other pet don't follow. */
402 if (m_ptr->parent_m_idx)
404 master_ptr->riding = 0;
405 master_ptr->pet_extra_flags &= ~(PF_TWO_HANDS);
406 master_ptr->riding_ryoute = master_ptr->old_riding_ryoute = FALSE;
410 /* Preserve the mount */
411 (void)COPY(&party_mon[0], m_ptr, monster_type);
413 /* Delete from this floor */
414 delete_monster_idx(master_ptr, master_ptr->riding);
419 * If player is in wild mode, no pets are preserved
420 * except a monster whom player riding
422 if (!master_ptr->wild_mode && !master_ptr->current_floor_ptr->inside_arena && !master_ptr->phase_out)
424 for (i = master_ptr->current_floor_ptr->m_max - 1, num = 1; (i >= 1 && num < MAX_PARTY_MON); i--)
426 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[i];
428 if (!monster_is_valid(m_ptr)) continue;
429 if (!is_pet(m_ptr)) continue;
430 if (i == master_ptr->riding) continue;
432 if (reinit_wilderness)
434 /* Don't lose sight of pets when getting a Quest */
438 POSITION dis = distance(master_ptr->y, master_ptr->x, m_ptr->fy, m_ptr->fx);
440 /* Confused (etc.) monsters don't follow. */
441 if (monster_confused_remaining(m_ptr) || monster_stunned_remaining(m_ptr) || monster_csleep_remaining(m_ptr)) continue;
443 /* Pet of other pet don't follow. */
444 if (m_ptr->parent_m_idx) continue;
447 * Pets with nickname will follow even from 3 blocks away
448 * when you or the pet can see the other.
450 if (m_ptr->nickname &&
451 ((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)) ||
452 (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))))
454 if (dis > 3) continue;
458 if (dis > 1) continue;
462 (void)COPY(&party_mon[num], &master_ptr->current_floor_ptr->m_list[i], monster_type);
466 /* Delete from this floor */
467 delete_monster_idx(master_ptr, i);
471 if (record_named_pet)
473 for (i = master_ptr->current_floor_ptr->m_max - 1; i >=1; i--)
475 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[i];
476 GAME_TEXT m_name[MAX_NLEN];
478 if (!monster_is_valid(m_ptr)) continue;
479 if (!is_pet(m_ptr)) continue;
480 if (!m_ptr->nickname) continue;
481 if (master_ptr->riding == i) continue;
483 monster_desc(master_ptr, m_name, m_ptr, MD_ASSUME_VISIBLE | MD_INDEF_VISIBLE);
484 exe_write_diary(master_ptr, DIARY_NAMED_PET, RECORD_NAMED_PET_MOVED, m_name);
489 /* Pet of other pet may disappear. */
490 for (i = master_ptr->current_floor_ptr->m_max - 1; i >=1; i--)
492 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[i];
494 /* Are there its parent? */
495 if (m_ptr->parent_m_idx && !master_ptr->current_floor_ptr->m_list[m_ptr->parent_m_idx].r_idx)
497 /* Its parent have gone, it also goes away. */
499 if (is_seen(master_ptr, m_ptr))
501 GAME_TEXT m_name[MAX_NLEN];
502 monster_desc(master_ptr, m_name, m_ptr, 0);
503 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
506 delete_monster_idx(master_ptr, i);
513 * @brief フロア移動時にペットを伴った場合の準備処理 / Pre-calculate the racial counters of preserved pets
514 * @param master_ptr プレーヤーへの参照ポインタ
517 * To prevent multiple generation of unique monster who is the minion of player
519 void precalc_cur_num_of_pet(player_type *player_ptr)
523 int max_num = player_ptr->wild_mode ? 1 : MAX_PARTY_MON;
525 for (i = 0; i < max_num; i++)
527 m_ptr = &party_mon[i];
529 /* Skip empty monsters */
530 if (!monster_is_valid(m_ptr)) continue;
532 /* Hack -- Increase the racial counter */
533 real_r_ptr(m_ptr)->cur_num++;
538 * @brief 移動先のフロアに伴ったペットを配置する / Place preserved pet monsters on new floor
539 * @param master_ptr プレーヤーへの参照ポインタ
542 static void place_pet(player_type *master_ptr)
545 int max_num = master_ptr->wild_mode ? 1 : MAX_PARTY_MON;
546 floor_type *floor_ptr = master_ptr->current_floor_ptr;
547 for (i = 0; i < max_num; i++)
549 POSITION cy = 0, cx = 0;
552 if (!(party_mon[i].r_idx)) continue;
556 m_idx = m_pop(floor_ptr);
557 master_ptr->riding = m_idx;
569 for (d = 1; d < A_MAX; d++)
571 for (j = 1000; j > 0; j--)
573 scatter(master_ptr, &cy, &cx, master_ptr->y, master_ptr->x, d, 0);
574 if (monster_can_enter(master_ptr, cy, cx, &r_info[party_mon[i].r_idx], 0)) break;
578 m_idx = (d == 6) ? 0 : m_pop(floor_ptr);
583 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[m_idx];
586 master_ptr->current_floor_ptr->grid_array[cy][cx].m_idx = m_idx;
588 m_ptr->r_idx = party_mon[i].r_idx;
590 /* Copy all member of the structure */
591 *m_ptr = party_mon[i];
592 r_ptr = real_r_ptr(m_ptr);
596 m_ptr->current_floor_ptr = master_ptr->current_floor_ptr;
598 m_ptr->mtimed[MTIMED_CSLEEP] = 0;
599 m_ptr->hold_o_idx = 0;
602 if ((r_ptr->flags1 & RF1_FORCE_SLEEP) && !ironman_nightmare)
604 /* Monster is still being nice */
605 m_ptr->mflag |= (MFLAG_NICE);
607 /* Must repair monsters */
608 repair_monsters = TRUE;
610 update_monster(master_ptr, m_idx, TRUE);
611 lite_spot(master_ptr, cy, cx);
613 /* Pre-calculated in precalc_cur_num_of_pet() */
614 /* r_ptr->cur_num++; */
616 /* Hack -- Count the number of "reproducers" */
617 if (r_ptr->flags2 & RF2_MULTIPLY) master_ptr->current_floor_ptr->num_repro++;
622 monster_type *m_ptr = &party_mon[i];
623 monster_race *r_ptr = real_r_ptr(m_ptr);
624 GAME_TEXT m_name[MAX_NLEN];
626 monster_desc(master_ptr, m_name, m_ptr, 0);
627 msg_format(_("%sとはぐれてしまった。", "You have lost sight of %s."), m_name);
628 if (record_named_pet && m_ptr->nickname)
630 monster_desc(master_ptr, m_name, m_ptr, MD_INDEF_VISIBLE);
631 exe_write_diary(master_ptr, DIARY_NAMED_PET, RECORD_NAMED_PET_LOST_SIGHT, m_name);
634 /* Pre-calculated in precalc_cur_num_of_pet(), but need to decrease */
635 if (r_ptr->cur_num) r_ptr->cur_num--;
639 /* For accuracy of precalc_cur_num_of_pet() */
640 (void)C_WIPE(party_mon, MAX_PARTY_MON, monster_type);
645 * @brief ユニークモンスターやアーティファクトの所在フロアを更新する / Hack -- Update location of unique monsters and artifacts
646 * @param cur_floor_id 現在のフロアID
649 * The r_ptr->floor_id and a_ptr->floor_id are not updated correctly\n
650 * while new floor creation since dungeons may be re-created by\n
651 * auto-scum option.\n
653 static void update_unique_artifact(floor_type *floor_ptr, s16b cur_floor_id)
657 /* Maintain unique monsters */
658 for (i = 1; i < floor_ptr->m_max; i++)
661 monster_type *m_ptr = &floor_ptr->m_list[i];
663 if (!monster_is_valid(m_ptr)) continue;
665 /* Extract real monster race */
666 r_ptr = real_r_ptr(m_ptr);
668 /* Memorize location of the unique monster */
669 if ((r_ptr->flags1 & RF1_UNIQUE) ||
670 (r_ptr->flags7 & RF7_NAZGUL))
672 r_ptr->floor_id = cur_floor_id;
676 /* Maintain artifatcs */
677 for (i = 1; i < floor_ptr->o_max; i++)
679 object_type *o_ptr = &floor_ptr->o_list[i];
681 if (!object_is_valid(o_ptr)) continue;
683 /* Memorize location of the artifact */
684 if (object_is_fixed_artifact(o_ptr))
686 a_info[o_ptr->name1].floor_id = cur_floor_id;
693 * @brief フロア移動時、プレイヤーの移動先モンスターが既にいた場合ランダムな近隣に移動させる / When a monster is at a place where player will return,
696 static void get_out_monster(player_type *protected_ptr)
700 POSITION oy = protected_ptr->y;
701 POSITION ox = protected_ptr->x;
702 floor_type *floor_ptr = protected_ptr->current_floor_ptr;
703 MONSTER_IDX m_idx = floor_ptr->grid_array[oy][ox].m_idx;
705 /* Nothing to do if no monster */
708 /* Look until done */
713 /* Pick a (possibly illegal) location */
714 POSITION ny = rand_spread(oy, dis);
715 POSITION nx = rand_spread(ox, dis);
719 /* Stop after 1000 tries */
720 if (tries > 10000) return;
723 * Increase distance after doing enough tries
724 * compared to area of possible space
726 if (tries > 20 * dis * dis) dis++;
728 /* Ignore illegal locations */
729 if (!in_bounds(floor_ptr, ny, nx)) continue;
731 /* Require "empty" floor space */
732 if (!is_cave_empty_bold(protected_ptr, ny, nx)) continue;
734 /* Hack -- no teleport onto glyph of warding */
735 if (is_glyph_grid(&floor_ptr->grid_array[ny][nx])) continue;
736 if (is_explosive_rune_grid(&floor_ptr->grid_array[ny][nx])) continue;
738 /* ...nor onto the Pattern */
739 if (pattern_tile(floor_ptr, ny, nx)) continue;
741 /*** It's a good place ***/
743 m_ptr = &floor_ptr->m_list[m_idx];
745 /* Update the old location */
746 floor_ptr->grid_array[oy][ox].m_idx = 0;
748 /* Update the new location */
749 floor_ptr->grid_array[ny][nx].m_idx = m_idx;
751 /* Move the monster */
755 /* No need to do update_monster() */
763 * @brief 新フロアに移動元フロアに繋がる階段を配置する / Virtually teleport onto the stairs that is connecting between two floors.
764 * @param sf_ptr 移動元の保存フロア構造体参照ポインタ
767 static void locate_connected_stairs(player_type *creature_ptr, floor_type *floor_ptr, saved_floor_type *sf_ptr, BIT_FLAGS floor_mode)
769 POSITION x, y, sx = 0, sy = 0;
770 POSITION x_table[20];
771 POSITION y_table[20];
775 /* Search usable stairs */
776 for (y = 0; y < floor_ptr->height; y++)
778 for (x = 0; x < floor_ptr->width; x++)
780 grid_type *g_ptr = &floor_ptr->grid_array[y][x];
781 feature_type *f_ptr = &f_info[g_ptr->feat];
784 if (floor_mode & CFM_UP)
786 if (have_flag(f_ptr->flags, FF_LESS) && have_flag(f_ptr->flags, FF_STAIRS) &&
787 !have_flag(f_ptr->flags, FF_SPECIAL))
791 /* Found fixed stairs? */
792 if (g_ptr->special &&
793 g_ptr->special == sf_ptr->upper_floor_id)
801 else if (floor_mode & CFM_DOWN)
803 if (have_flag(f_ptr->flags, FF_MORE) && have_flag(f_ptr->flags, FF_STAIRS) &&
804 !have_flag(f_ptr->flags, FF_SPECIAL))
808 /* Found fixed stairs */
809 if (g_ptr->special &&
810 g_ptr->special == sf_ptr->lower_floor_id)
820 if (have_flag(f_ptr->flags, FF_BLDG))
826 if (ok && (num < 20))
838 creature_ptr->y = sy;
839 creature_ptr->x = sx;
843 /* No stairs found! -- No return */
844 prepare_change_floor_mode(creature_ptr, CFM_RAND_PLACE | CFM_NO_RETURN);
846 /* Mega Hack -- It's not the stairs you enter. Disable it. */
847 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;
851 /* Choose random one */
854 /* Point stair location */
855 creature_ptr->y = y_table[i];
856 creature_ptr->x = x_table[i];
861 * @brief 現在のフロアを離れるに伴って行なわれる保存処理
862 * / Maintain quest monsters, mark next floor_id at stairs, save current floor, and prepare to enter next floor.
863 * @param creature_ptr プレーヤーへの参照ポインタ
866 void leave_floor(player_type *creature_ptr)
868 grid_type *g_ptr = NULL;
870 saved_floor_type *sf_ptr;
871 MONRACE_IDX quest_r_idx = 0;
873 FLOOR_IDX tmp_floor_idx = 0;
875 /* Preserve pets and prepare to take these to next floor */
876 preserve_pet(creature_ptr);
878 /* Remove all mirrors without explosion */
879 remove_all_mirrors(creature_ptr, FALSE);
881 if (creature_ptr->special_defense & NINJA_S_STEALTH) set_superstealth(creature_ptr, FALSE);
883 /* New floor is not yet prepared */
886 /* Temporarily get a floor_id (for Arena) */
887 if (!creature_ptr->floor_id &&
888 (creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) &&
889 !(creature_ptr->change_floor_mode & CFM_NO_RETURN))
891 /* Get temporary floor_id */
892 tmp_floor_idx = get_new_floor_id(creature_ptr);
895 /* Search the quest monster index */
896 for (i = 0; i < max_q_idx; i++)
898 if ((quest[i].status == QUEST_STATUS_TAKEN) &&
899 ((quest[i].type == QUEST_TYPE_KILL_LEVEL) ||
900 (quest[i].type == QUEST_TYPE_RANDOM)) &&
901 (quest[i].level == creature_ptr->current_floor_ptr->dun_level) &&
902 (creature_ptr->dungeon_idx == quest[i].dungeon) &&
903 !(quest[i].flags & QUEST_FLAG_PRESET))
905 quest_r_idx = quest[i].r_idx;
909 /* Maintain quest monsters */
910 for (i = 1; i < creature_ptr->current_floor_ptr->m_max; i++)
913 monster_type *m_ptr = &creature_ptr->current_floor_ptr->m_list[i];
915 if (!monster_is_valid(m_ptr)) continue;
917 /* Only maintain quest monsters */
918 if (quest_r_idx != m_ptr->r_idx) continue;
920 /* Extract real monster race */
921 r_ptr = real_r_ptr(m_ptr);
923 /* Ignore unique monsters */
924 if ((r_ptr->flags1 & RF1_UNIQUE) ||
925 (r_ptr->flags7 & RF7_NAZGUL)) continue;
927 /* Delete non-unique quest monsters */
928 delete_monster_idx(creature_ptr, i);
931 /* Check if there is a same item */
932 for (i = 0; i < INVEN_PACK; i++)
934 object_type *o_ptr = &creature_ptr->inventory_list[i];
936 if (!object_is_valid(o_ptr)) continue;
938 /* Delete old memorized location of the artifact */
939 if (object_is_fixed_artifact(o_ptr))
941 a_info[o_ptr->name1].floor_id = 0;
945 /* Extract current floor info or NULL */
946 sf_ptr = get_sf_ptr(creature_ptr->floor_id);
948 /* Choose random stairs */
949 if ((creature_ptr->change_floor_mode & CFM_RAND_CONNECT) && tmp_floor_idx)
951 locate_connected_stairs(creature_ptr, creature_ptr->current_floor_ptr, sf_ptr, creature_ptr->change_floor_mode);
954 /* Extract new dungeon level */
955 if (creature_ptr->change_floor_mode & CFM_SAVE_FLOORS)
957 /* Extract stair position */
958 g_ptr = &creature_ptr->current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
959 f_ptr = &f_info[g_ptr->feat];
961 /* Get back to old saved floor? */
962 if (g_ptr->special && !have_flag(f_ptr->flags, FF_SPECIAL) && get_sf_ptr(g_ptr->special))
964 /* Saved floor is exist. Use it. */
965 new_floor_id = g_ptr->special;
968 /* Mark shaft up/down */
969 if (have_flag(f_ptr->flags, FF_STAIRS) && have_flag(f_ptr->flags, FF_SHAFT))
971 prepare_change_floor_mode(creature_ptr, CFM_SHAFT);
975 /* Climb up/down some sort of stairs */
976 if (creature_ptr->change_floor_mode & (CFM_DOWN | CFM_UP))
980 /* Extract level movement number */
981 if (creature_ptr->change_floor_mode & CFM_DOWN) move_num = 1;
982 else if (creature_ptr->change_floor_mode & CFM_UP) move_num = -1;
984 /* Shafts are deeper than normal stairs */
985 if (creature_ptr->change_floor_mode & CFM_SHAFT)
986 move_num += SGN(move_num);
988 /* Get out from or Enter the dungeon */
989 if (creature_ptr->change_floor_mode & CFM_DOWN)
991 if (!creature_ptr->current_floor_ptr->dun_level)
992 move_num = d_info[creature_ptr->dungeon_idx].mindepth;
994 else if (creature_ptr->change_floor_mode & CFM_UP)
996 if (creature_ptr->current_floor_ptr->dun_level + move_num < d_info[creature_ptr->dungeon_idx].mindepth)
997 move_num = -creature_ptr->current_floor_ptr->dun_level;
1000 creature_ptr->current_floor_ptr->dun_level += move_num;
1003 /* Leaving the dungeon to town */
1004 if (!creature_ptr->current_floor_ptr->dun_level && creature_ptr->dungeon_idx)
1006 creature_ptr->leaving_dungeon = TRUE;
1007 if (!vanilla_town && !lite_town)
1009 creature_ptr->wilderness_y = d_info[creature_ptr->dungeon_idx].dy;
1010 creature_ptr->wilderness_x = d_info[creature_ptr->dungeon_idx].dx;
1012 creature_ptr->recall_dungeon = creature_ptr->dungeon_idx;
1013 creature_ptr->dungeon_idx = 0;
1015 /* Reach to the surface -- Clear all saved floors */
1016 creature_ptr->change_floor_mode &= ~CFM_SAVE_FLOORS; // TODO
1019 /* Kill some old saved floors */
1020 if (!(creature_ptr->change_floor_mode & CFM_SAVE_FLOORS))
1022 /* Kill all saved floors */
1023 for (i = 0; i < MAX_SAVED_FLOORS; i++)
1024 kill_saved_floor(creature_ptr, &saved_floors[i]);
1026 /* Reset visit_mark count */
1027 latest_visit_mark = 1;
1029 else if (creature_ptr->change_floor_mode & CFM_NO_RETURN)
1031 /* Kill current floor */
1032 kill_saved_floor(creature_ptr, sf_ptr);
1035 /* No current floor -- Left/Enter dungeon etc... */
1036 if (!creature_ptr->floor_id)
1038 /* No longer need to save current floor */
1042 /* Mark next floor_id on the previous floor */
1046 new_floor_id = get_new_floor_id(creature_ptr);
1048 /* Connect from here */
1049 if (g_ptr && !feat_uses_special(g_ptr->feat))
1051 g_ptr->special = new_floor_id;
1055 /* Fix connection -- level teleportation or trap door */
1056 if (creature_ptr->change_floor_mode & CFM_RAND_CONNECT)
1058 if (creature_ptr->change_floor_mode & CFM_UP)
1059 sf_ptr->upper_floor_id = new_floor_id;
1060 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1061 sf_ptr->lower_floor_id = new_floor_id;
1064 /* If you can return, you need to save previous floor */
1065 if ((creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) &&
1066 !(creature_ptr->change_floor_mode & CFM_NO_RETURN))
1068 /* Get out of the my way! */
1069 get_out_monster(creature_ptr);
1071 /* Record the last visit turn of current floor */
1072 sf_ptr->last_visit = current_world_ptr->game_turn;
1074 forget_lite(creature_ptr->current_floor_ptr);
1075 forget_view(creature_ptr->current_floor_ptr);
1076 clear_mon_lite(creature_ptr->current_floor_ptr);
1078 /* Save current floor */
1079 if (!save_floor(creature_ptr, sf_ptr, 0))
1081 /* Save failed -- No return */
1082 prepare_change_floor_mode(creature_ptr, CFM_NO_RETURN);
1084 /* Kill current floor */
1085 kill_saved_floor(creature_ptr, get_sf_ptr(creature_ptr->floor_id));
1092 * @brief フロアの切り替え処理 / Enter new floor.
1093 * @param creature_ptr プレーヤーへの参照ポインタ
1096 * If the floor is an old saved floor, it will be\n
1097 * restored from the temporary file. If the floor is new one, new floor\n
1098 * will be generated.\n
1100 void change_floor(player_type *creature_ptr)
1102 saved_floor_type *sf_ptr;
1103 bool loaded = FALSE;
1105 /* The dungeon is not ready */
1106 current_world_ptr->character_dungeon = FALSE;
1108 /* No longer in the trap detecteded region */
1109 creature_ptr->dtrap = FALSE;
1111 /* Mega-Hack -- no panel yet */
1117 /* Mega-Hack -- not ambushed on the wildness? */
1118 creature_ptr->ambush_flag = FALSE;
1120 /* No saved floors (On the surface etc.) */
1121 if (!(creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) &&
1122 !(creature_ptr->change_floor_mode & CFM_FIRST_FLOOR))
1124 generate_floor(creature_ptr);
1126 /* Paranoia -- No new saved floor */
1130 /* In the dungeon */
1133 /* No floor_id yet */
1137 new_floor_id = get_new_floor_id(creature_ptr);
1140 /* Pointer for infomations of new floor */
1141 sf_ptr = get_sf_ptr(new_floor_id);
1143 /* Try to restore old floor */
1144 if (sf_ptr->last_visit)
1146 /* Old saved floor is exist */
1147 if (load_floor(creature_ptr, sf_ptr, 0))
1151 /* Forbid return stairs */
1152 if (creature_ptr->change_floor_mode & CFM_NO_RETURN)
1154 grid_type *g_ptr = &creature_ptr->current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
1156 if (!feat_uses_special(g_ptr->feat))
1158 if (creature_ptr->change_floor_mode & (CFM_DOWN | CFM_UP))
1160 /* Reset to floor */
1161 g_ptr->feat = feat_ground_type[randint0(100)];
1171 * Set lower/upper_floor_id of new floor when the new
1172 * floor is right-above/right-under the current floor.
1174 * Stair creation/Teleport level/Trap door will take
1175 * you the same floor when you used it later again.
1177 if (creature_ptr->floor_id)
1179 saved_floor_type *cur_sf_ptr = get_sf_ptr(creature_ptr->floor_id);
1181 if (creature_ptr->change_floor_mode & CFM_UP)
1183 /* New floor is right-above */
1184 if (cur_sf_ptr->upper_floor_id == new_floor_id)
1185 sf_ptr->lower_floor_id = creature_ptr->floor_id;
1187 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1189 /* New floor is right-under */
1190 if (cur_sf_ptr->lower_floor_id == new_floor_id)
1191 sf_ptr->upper_floor_id = creature_ptr->floor_id;
1195 /* Break connection to killed floor */
1198 if (creature_ptr->change_floor_mode & CFM_UP)
1199 sf_ptr->lower_floor_id = 0;
1200 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1201 sf_ptr->upper_floor_id = 0;
1204 /* Maintain monsters and artifacts */
1208 GAME_TURN tmp_last_visit = sf_ptr->last_visit;
1209 GAME_TURN absence_ticks;
1210 int alloc_chance = d_info[creature_ptr->dungeon_idx].max_m_alloc_chance;
1211 GAME_TURN alloc_times;
1213 while (tmp_last_visit > current_world_ptr->game_turn) tmp_last_visit -= TURNS_PER_TICK * TOWN_DAWN;
1214 absence_ticks = (current_world_ptr->game_turn - tmp_last_visit) / TURNS_PER_TICK;
1216 /* Maintain monsters */
1217 for (i = 1; i < creature_ptr->current_floor_ptr->m_max; i++)
1219 monster_race *r_ptr;
1220 monster_type *m_ptr = &creature_ptr->current_floor_ptr->m_list[i];
1222 if (!monster_is_valid(m_ptr)) continue;
1227 m_ptr->hp = m_ptr->maxhp = m_ptr->max_maxhp;
1229 /* Remove timed status (except MTIMED_CSLEEP) */
1230 (void)set_monster_fast(creature_ptr, i, 0);
1231 (void)set_monster_slow(creature_ptr, i, 0);
1232 (void)set_monster_stunned(creature_ptr, i, 0);
1233 (void)set_monster_confused(creature_ptr, i, 0);
1234 (void)set_monster_monfear(creature_ptr, i, 0);
1235 (void)set_monster_invulner(creature_ptr, i, 0, FALSE);
1238 /* Extract real monster race */
1239 r_ptr = real_r_ptr(m_ptr);
1241 /* Ignore non-unique */
1242 if (!(r_ptr->flags1 & RF1_UNIQUE) &&
1243 !(r_ptr->flags7 & RF7_NAZGUL)) continue;
1245 /* Appear at a different floor? */
1246 if (r_ptr->floor_id != new_floor_id)
1248 /* Disapper from here */
1249 delete_monster_idx(creature_ptr, i);
1253 /* Maintain artifatcs */
1254 for (i = 1; i < creature_ptr->current_floor_ptr->o_max; i++)
1256 object_type *o_ptr = &creature_ptr->current_floor_ptr->o_list[i];
1258 if (!object_is_valid(o_ptr)) continue;
1260 /* Ignore non-artifact */
1261 if (!object_is_fixed_artifact(o_ptr)) continue;
1263 /* Appear at a different floor? */
1264 if (a_info[o_ptr->name1].floor_id != new_floor_id)
1266 /* Disappear from here */
1267 delete_object_idx(creature_ptr, i);
1271 /* Cancel preserve */
1272 a_info[o_ptr->name1].cur_num = 1;
1276 (void)place_quest_monsters(creature_ptr);
1278 /* Place some random monsters */
1279 alloc_times = absence_ticks / alloc_chance;
1281 if (randint0(alloc_chance) < (absence_ticks % alloc_chance))
1284 for (i = 0; i < alloc_times; i++)
1286 /* Make a (group of) new monster */
1287 (void)alloc_monster(creature_ptr, 0, 0, summon_specific);
1292 /* New floor_id or failed to restore */
1293 else /* if (!loaded) */
1295 if (sf_ptr->last_visit)
1297 /* Temporary file is broken? */
1298 msg_print(_("階段は行き止まりだった。", "The staircases come to a dead end..."));
1300 /* Create simple dead end */
1301 build_dead_end(creature_ptr);
1303 /* Break connection */
1304 if (creature_ptr->change_floor_mode & CFM_UP)
1306 sf_ptr->upper_floor_id = 0;
1308 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1310 sf_ptr->lower_floor_id = 0;
1315 generate_floor(creature_ptr);
1318 /* Record last visit turn */
1319 sf_ptr->last_visit = current_world_ptr->game_turn;
1321 /* Set correct dun_level value */
1322 sf_ptr->dun_level = creature_ptr->current_floor_ptr->dun_level;
1324 /* Create connected stairs */
1325 if (!(creature_ptr->change_floor_mode & CFM_NO_RETURN))
1327 /* Extract stair position */
1328 grid_type *g_ptr = &creature_ptr->current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
1330 /*** Create connected stairs ***/
1332 /* No stairs down from Quest */
1333 if ((creature_ptr->change_floor_mode & CFM_UP) && !quest_number(creature_ptr, creature_ptr->current_floor_ptr->dun_level))
1335 g_ptr->feat = (creature_ptr->change_floor_mode & CFM_SHAFT) ? feat_state(creature_ptr, feat_down_stair, FF_SHAFT) : feat_down_stair;
1338 /* No stairs up when ironman_downward */
1339 else if ((creature_ptr->change_floor_mode & CFM_DOWN) && !ironman_downward)
1341 g_ptr->feat = (creature_ptr->change_floor_mode & CFM_SHAFT) ? feat_state(creature_ptr, feat_up_stair, FF_SHAFT) : feat_up_stair;
1344 /* Paranoia -- Clear mimic */
1347 /* Connect to previous floor */
1348 g_ptr->special = creature_ptr->floor_id;
1352 /* Arrive at random grid */
1353 if (creature_ptr->change_floor_mode & (CFM_RAND_PLACE))
1355 (void)new_player_spot(creature_ptr);
1358 /* You see stairs blocked */
1359 else if ((creature_ptr->change_floor_mode & CFM_NO_RETURN) && (creature_ptr->change_floor_mode & (CFM_DOWN | CFM_UP)))
1361 if (!creature_ptr->blind)
1363 msg_print(_("突然階段が塞がれてしまった。", "Suddenly the stairs is blocked!"));
1367 msg_print(_("ゴトゴトと何か音がした。", "You hear some noises."));
1374 * The "turn" is not always different number because
1375 * the level teleport doesn't take any turn. Use
1376 * visit mark instead of last visit turn to find the
1377 * oldest saved floor.
1379 sf_ptr->visit_mark = latest_visit_mark++;
1382 /* Place preserved pet monsters */
1383 place_pet(creature_ptr);
1385 /* Reset travel target place */
1386 forget_travel_flow(creature_ptr->current_floor_ptr);
1388 /* Hack -- maintain unique and artifacts */
1389 update_unique_artifact(creature_ptr->current_floor_ptr, new_floor_id);
1391 /* Now the player is in new floor */
1392 creature_ptr->floor_id = new_floor_id;
1394 /* The dungeon is ready */
1395 current_world_ptr->character_dungeon = TRUE;
1397 /* Hack -- Munchkin characters always get whole map */
1398 if (creature_ptr->pseikaku == PERSONALITY_MUNCHKIN)
1399 wiz_lite(creature_ptr, (bool)(creature_ptr->pclass == CLASS_NINJA));
1401 /* Remember when this level was "created" */
1402 creature_ptr->current_floor_ptr->generated_turn = current_world_ptr->game_turn;
1404 /* No dungeon feeling yet */
1405 creature_ptr->feeling_turn = creature_ptr->current_floor_ptr->generated_turn;
1406 creature_ptr->feeling = 0;
1408 /* Clear all flags */
1409 creature_ptr->change_floor_mode = 0L;
1411 select_floor_music(creature_ptr);
1412 creature_ptr->change_floor_mode = 0;