OSDN Git Service

a9d811f8c71c5a3a6811557360e5286dab4e9d2b
[hengband/hengband.git] / src / floor-save.c
1 /*!
2  * @file floors.c
3  * @brief 保存された階の管理 / management of the saved floor
4  * @date 2014/01/04
5  * @author
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
11  */
12 #pragma once
13
14 #include "angband.h"
15 #include "bldg.h"
16 #include "core.h"
17 #include "load.h"
18 #include "util.h"
19
20 #include "artifact.h"
21 #include "dungeon.h"
22 #include "floor.h"
23 #include "floor-save.h"
24 #include "floor-events.h"
25 #include "floor-generate.h"
26 #include "feature.h"
27 #include "geometry.h"
28 #include "grid.h"
29 #include "monster.h"
30 #include "quest.h"
31 #include "wild.h"
32 #include "spells-floor.h"
33 #include "monster-status.h"
34 #include "object-hook.h"
35 #include "cmd-pet.h"
36 #include "cmd-basic.h"
37 #include "files.h"
38 #include "player-effects.h"
39 #include "player-class.h"
40 #include "player-personality.h"
41 #include "world.h"
42 #include "spells.h"
43 #include "cmd-dump.h"
44 #include "save.h"
45
46 #include "view-mainwindow.h"
47
48
49 static FLOOR_IDX new_floor_id;  /*!<次のフロアのID / floor_id of the destination */
50 static u32b latest_visit_mark;  /*!<フロアを渡った回数?(確認中) / Max number of visit_mark */
51
52 /*
53  * Number of floor_id used from birth
54  */
55 FLOOR_IDX max_floor_id;
56
57 /*
58  * Sign for current process used in temporal files.
59  * Actually it is the start time of current process.
60  */
61 u32b saved_floor_file_sign;
62
63 /*!
64  * @brief 保存フロア配列を初期化する / Initialize saved_floors array. 
65  * @param force テンポラリファイルが残っていた場合も警告なしで強制的に削除する。
66  * @details Make sure that old temporal files are not remaining as gurbages.
67  * @return なし
68  */
69 void init_saved_floors(bool force)
70 {
71         char floor_savefile[1024];
72         int i;
73         int fd = -1;
74         BIT_FLAGS mode = 0644;
75
76 #ifdef SET_UID
77 # ifdef SECURE
78         /* Get "games" permissions */
79         beGames();
80 # endif
81 #endif
82
83         for (i = 0; i < MAX_SAVED_FLOORS; i++)
84         {
85                 saved_floor_type *sf_ptr = &saved_floors[i];
86
87                 /* File name */
88                 sprintf(floor_savefile, "%s.F%02d", savefile, i);
89
90                 /* Grab permissions */
91                 safe_setuid_grab();
92
93                 /* Try to create the file */
94                 fd = fd_make(floor_savefile, mode);
95
96                 /* Drop permissions */
97                 safe_setuid_drop();
98
99                 /* Failed! */
100                 if (fd < 0)
101                 {
102                         if (!force)
103                         {
104                                 msg_print(_("エラー:古いテンポラリ・ファイルが残っています。", "Error: There are old temporal files."));
105                                 msg_print(_("変愚蛮怒を二重に起動していないか確認してください。", "Make sure you are not running two game processes simultaneously."));
106                                 msg_print(_("過去に変愚蛮怒がクラッシュした場合は一時ファイルを", "If the temporal files are garbages of old crashed process, "));
107                                 msg_print(_("強制的に削除して実行を続けられます。", "you can delete it safely."));
108                                 if (!get_check(_("強制的に削除してもよろしいですか?", "Do you delete old temporal files? ")))
109                                         quit(_("実行中止", "Aborted."));
110                                 force = TRUE;
111                         }
112                 }
113                 else
114                 {
115                         /* Close the "fd" */
116                         (void)fd_close(fd);
117                 }
118
119                 /* Grab permissions */
120                 safe_setuid_grab();
121
122                 /* Simply kill the temporal file */ 
123                 (void)fd_kill(floor_savefile);
124
125                 /* Drop permissions */
126                 safe_setuid_drop();
127
128                 sf_ptr->floor_id = 0;
129         }
130
131         /* No floor_id used yet (No.0 is reserved to indicate non existance) */
132         max_floor_id = 1;
133
134         /* vist_mark is from 1 */
135         latest_visit_mark = 1;
136
137         /* A sign to mark temporal files */
138         saved_floor_file_sign = (u32b)time(NULL);
139
140         /* No next floor yet */
141         new_floor_id = 0;
142
143         /* No change floor mode yet */
144         p_ptr->change_floor_mode = 0;
145
146 #ifdef SET_UID
147 # ifdef SECURE
148         /* Drop "games" permissions */
149         bePlayer();
150 # endif
151 #endif
152 }
153
154 /*!
155  * @brief 保存フロア用テンポラリファイルを削除する / Kill temporal files
156  * @details Should be called just before the game quit.
157  * @return なし
158  */
159 void clear_saved_floor_files(void)
160 {
161         char floor_savefile[1024];
162         int i;
163
164 #ifdef SET_UID
165 # ifdef SECURE
166         /* Get "games" permissions */
167         beGames();
168 # endif
169 #endif
170
171         for (i = 0; i < MAX_SAVED_FLOORS; i++)
172         {
173                 saved_floor_type *sf_ptr = &saved_floors[i];
174
175                 /* No temporal file */
176                 if (!sf_ptr->floor_id) continue;
177                 if (sf_ptr->floor_id == p_ptr->floor_id) continue;
178
179                 /* File name */
180                 sprintf(floor_savefile, "%s.F%02d", savefile, i);
181
182                 /* Grab permissions */
183                 safe_setuid_grab();
184
185                 /* Simply kill the temporal file */ 
186                 (void)fd_kill(floor_savefile);
187
188                 /* Drop permissions */
189                 safe_setuid_drop();
190         }
191
192 #ifdef SET_UID
193 # ifdef SECURE
194         /* Drop "games" permissions */
195         bePlayer();
196 # endif
197 #endif
198 }
199
200 /*!
201  * @brief 保存フロアIDから参照ポインタを得る / Get a pointer for an item of the saved_floors array.
202  * @param floor_id 保存フロアID
203  * @return IDに対応する保存フロアのポインタ、ない場合はNULLを返す。
204  */
205 saved_floor_type *get_sf_ptr(FLOOR_IDX floor_id)
206 {
207         int i;
208
209         /* floor_id No.0 indicates no floor */
210         if (!floor_id) return NULL;
211
212         for (i = 0; i < MAX_SAVED_FLOORS; i++)
213         {
214                 saved_floor_type *sf_ptr = &saved_floors[i];
215
216                 if (sf_ptr->floor_id == floor_id) return sf_ptr;
217         }
218
219         /* None found */
220         return NULL;
221 }
222
223
224 /*!
225  * @brief 参照ポインタ先の保存フロアを抹消する / kill a saved floor and get an empty space
226  * @param sf_ptr 保存フロアの参照ポインタ
227  * @return なし
228  */
229 static void kill_saved_floor(saved_floor_type *sf_ptr)
230 {
231         char floor_savefile[1024];
232         if (!sf_ptr) return;
233
234         /* Already empty */
235         if (!sf_ptr->floor_id) return;
236
237         if (sf_ptr->floor_id == p_ptr->floor_id)
238         {
239                 /* Kill current floor */
240                 p_ptr->floor_id = 0;
241
242                 /* Current floor doesn't have temporal file */
243         }
244         else 
245         {
246                 /* File name */
247                 sprintf(floor_savefile, "%s.F%02d", savefile, (int)sf_ptr->savefile_id);
248
249                 /* Grab permissions */
250                 safe_setuid_grab();
251
252                 /* Simply kill the temporal file */ 
253                 (void)fd_kill(floor_savefile);
254
255                 /* Drop permissions */
256                 safe_setuid_drop();
257         }
258
259         /* No longer exists */
260         sf_ptr->floor_id = 0;
261 }
262
263
264 /*!
265  * @brief 新規に利用可能な保存フロアを返す / Initialize new saved floor and get its floor id.
266  * @return 利用可能な保存フロアID
267  * @details
268  * If number of saved floors are already MAX_SAVED_FLOORS, kill the oldest one.
269  */
270 FLOOR_IDX get_new_floor_id(void)
271 {
272         saved_floor_type *sf_ptr = NULL;
273         FLOOR_IDX i;
274
275         /* Look for empty space */
276         for (i = 0; i < MAX_SAVED_FLOORS; i++)
277         {
278                 sf_ptr = &saved_floors[i];
279
280                 if (!sf_ptr->floor_id) break;
281         }
282
283         /* None found */
284         if (i == MAX_SAVED_FLOORS)
285         {
286                 s16b oldest = 0;
287                 u32b oldest_visit = 0xffffffffL;
288
289                 /* Search for oldest */
290                 for (i = 0; i < MAX_SAVED_FLOORS; i++)
291                 {
292                         sf_ptr = &saved_floors[i];
293
294                         /* Don't kill current floor */
295                         if (sf_ptr->floor_id == p_ptr->floor_id) continue;
296
297                         /* Don't kill newer */
298                         if (sf_ptr->visit_mark > oldest_visit) continue;
299
300                         oldest = i;
301                         oldest_visit = sf_ptr->visit_mark;
302                 }
303
304                 /* Kill oldest saved floor */
305                 sf_ptr = &saved_floors[oldest];
306                 kill_saved_floor(sf_ptr);
307
308                 /* Use it */
309                 i = oldest;
310         }
311
312         /* Prepare new floor data */
313         sf_ptr->savefile_id = i;
314         sf_ptr->floor_id = max_floor_id;
315         sf_ptr->last_visit = 0;
316         sf_ptr->upper_floor_id = 0;
317         sf_ptr->lower_floor_id = 0;
318         sf_ptr->visit_mark = latest_visit_mark++;
319
320         /* sf_ptr->dun_level may be changed later */
321         sf_ptr->dun_level = p_ptr->current_floor_ptr->dun_level;
322
323
324         /* Increment number of floor_id */
325         if (max_floor_id < MAX_SHORT) max_floor_id++;
326
327         /* 32767 floor_ids are all used up!  Re-use ancient IDs */
328         else max_floor_id = 1;
329
330         return sf_ptr->floor_id;
331 }
332
333
334 /*!
335  * @brief フロア切り替え時の処理フラグを追加する / Prepare mode flags of changing floor
336  * @param mode 追加したい所持フラグ
337  * @return なし
338  */
339 void prepare_change_floor_mode(BIT_FLAGS mode)
340 {
341         p_ptr->change_floor_mode |= mode;
342 }
343
344 /*!
345  * @brief 階段移動先のフロアが生成できない時に簡単な行き止まりマップを作成する / Builds the dead end
346  * @return なし
347  */
348 static void build_dead_end(floor_type *floor_ptr)
349 {
350         POSITION x, y;
351
352         clear_cave(floor_ptr);
353
354         /* Mega-Hack -- no player yet */
355         p_ptr->x = p_ptr->y = 0;
356
357
358         /* Fill the arrays of floors and walls in the good proportions */
359         set_floor_and_wall(0);
360
361         /* Smallest area */
362         floor_ptr->height = SCREEN_HGT;
363         floor_ptr->width = SCREEN_WID;
364
365         /* Filled with permanent walls */
366         for (y = 0; y < MAX_HGT; y++)
367         {
368                 for (x = 0; x < MAX_WID; x++)
369                 {
370                         /* Create "solid" perma-wall */
371                         place_solid_perm_bold(floor_ptr, y, x);
372                 }
373         }
374
375         /* Place at center of the floor */
376         p_ptr->y = floor_ptr->height / 2;
377         p_ptr->x = floor_ptr->width / 2;
378
379         /* Give one square */
380         place_floor_bold(floor_ptr, p_ptr->y, p_ptr->x);
381
382         wipe_generate_random_floor_flags(floor_ptr);
383 }
384
385
386
387 #define MAX_PARTY_MON 21 /*!< フロア移動時に先のフロアに連れて行けるペットの最大数 Maximum number of preservable pets */
388 static monster_type party_mon[MAX_PARTY_MON]; /*!< フロア移動に保存するペットモンスターの配列 */
389
390 /*!
391  * @brief フロア移動時のペット保存処理 / Preserve_pets
392  * @return なし
393  */
394 static void preserve_pet(player_type *master_ptr)
395 {
396         int num;
397         MONSTER_IDX i;
398
399         for (num = 0; num < MAX_PARTY_MON; num++)
400         {
401                 party_mon[num].r_idx = 0;
402         }
403
404         if (master_ptr->riding)
405         {
406                 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[master_ptr->riding];
407
408                 /* Pet of other pet don't follow. */
409                 if (m_ptr->parent_m_idx)
410                 {
411                         master_ptr->riding = 0;
412                         master_ptr->pet_extra_flags &= ~(PF_RYOUTE);
413                         master_ptr->riding_ryoute = master_ptr->old_riding_ryoute = FALSE;
414                 }
415                 else
416                 {
417                         /* Preserve the mount */
418                         (void)COPY(&party_mon[0], m_ptr, monster_type);
419
420                         /* Delete from this floor */
421                         delete_monster_idx(master_ptr->riding);
422                 }
423         }
424
425         /*
426          * If player is in wild mode, no pets are preserved
427          * except a monster whom player riding
428          */
429         if (!master_ptr->wild_mode && !master_ptr->current_floor_ptr->inside_arena && !master_ptr->phase_out)
430         {
431                 for (i = master_ptr->current_floor_ptr->m_max - 1, num = 1; (i >= 1 && num < MAX_PARTY_MON); i--)
432                 {
433                         monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[i];
434
435                         if (!monster_is_valid(m_ptr)) continue;
436                         if (!is_pet(m_ptr)) continue;
437                         if (i == master_ptr->riding) continue;
438
439                         if (reinit_wilderness)
440                         {
441                                 /* Don't lose sight of pets when getting a Quest */
442                         }
443                         else
444                         {
445                                 POSITION dis = distance(master_ptr->y, master_ptr->x, m_ptr->fy, m_ptr->fx);
446
447                                 /* Confused (etc.) monsters don't follow. */
448                                 if (MON_CONFUSED(m_ptr) || MON_STUNNED(m_ptr) || MON_CSLEEP(m_ptr)) continue;
449
450                                 /* Pet of other pet don't follow. */
451                                 if (m_ptr->parent_m_idx) continue;
452
453                                 /*
454                                  * Pets with nickname will follow even from 3 blocks away
455                                  * when you or the pet can see the other.
456                                  */
457                                 if (m_ptr->nickname && 
458                                     ((player_has_los_bold(master_ptr, m_ptr->fy, m_ptr->fx) && projectable(master_ptr->current_floor_ptr, master_ptr->y, master_ptr->x, m_ptr->fy, m_ptr->fx)) ||
459                                      (los(master_ptr->current_floor_ptr, m_ptr->fy, m_ptr->fx, master_ptr->y, master_ptr->x) && projectable(master_ptr->current_floor_ptr, m_ptr->fy, m_ptr->fx, master_ptr->y, master_ptr->x))))
460                                 {
461                                         if (dis > 3) continue;
462                                 }
463                                 else
464                                 {
465                                         if (dis > 1) continue;
466                                 }
467                         }
468
469                         (void)COPY(&party_mon[num], &master_ptr->current_floor_ptr->m_list[i], monster_type);
470
471                         num++;
472
473                         /* Delete from this floor */
474                         delete_monster_idx(i);
475                 }
476         }
477
478         if (record_named_pet)
479         {
480                 for (i = master_ptr->current_floor_ptr->m_max - 1; i >=1; i--)
481                 {
482                         monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[i];
483                         GAME_TEXT m_name[MAX_NLEN];
484
485                         if (!monster_is_valid(m_ptr)) continue;
486                         if (!is_pet(m_ptr)) continue;
487                         if (!m_ptr->nickname) continue;
488                         if (master_ptr->riding == i) continue;
489
490                         monster_desc(m_name, m_ptr, MD_ASSUME_VISIBLE | MD_INDEF_VISIBLE);
491                         exe_write_diary(master_ptr, NIKKI_NAMED_PET, RECORD_NAMED_PET_MOVED, m_name);
492                 }
493         }
494
495
496         /* Pet of other pet may disappear. */
497         for (i = master_ptr->current_floor_ptr->m_max - 1; i >=1; i--)
498         {
499                 monster_type *m_ptr = &master_ptr->current_floor_ptr->m_list[i];
500
501                 /* Are there its parent? */
502                 if (m_ptr->parent_m_idx && !master_ptr->current_floor_ptr->m_list[m_ptr->parent_m_idx].r_idx)
503                 {
504                         /* Its parent have gone, it also goes away. */
505
506                         if (is_seen(m_ptr))
507                         {
508                                 GAME_TEXT m_name[MAX_NLEN];
509                                 monster_desc(m_name, m_ptr, 0);
510                                 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
511                         }
512
513                         delete_monster_idx(i);
514                 }
515         }
516 }
517
518
519 /*!
520  * @brief フロア移動時にペットを伴った場合の準備処理 / Pre-calculate the racial counters of preserved pets
521  * @return なし
522  * @details
523  * To prevent multiple generation of unique monster who is the minion of player
524  */
525 void precalc_cur_num_of_pet(void)
526 {
527         monster_type *m_ptr;
528         int i;
529         int max_num = p_ptr->wild_mode ? 1 : MAX_PARTY_MON;
530
531         for (i = 0; i < max_num; i++)
532         {
533                 m_ptr = &party_mon[i];
534
535                 /* Skip empty monsters */
536                 if (!monster_is_valid(m_ptr)) continue;
537
538                 /* Hack -- Increase the racial counter */
539                 real_r_ptr(m_ptr)->cur_num++;
540         }
541 }
542
543 /*!
544  * @brief 移動先のフロアに伴ったペットを配置する / Place preserved pet monsters on new floor
545  * @return なし
546  */
547 static void place_pet(player_type *master_ptr)
548 {
549         int i;
550         int max_num = master_ptr->wild_mode ? 1 : MAX_PARTY_MON;
551
552         for (i = 0; i < max_num; i++)
553         {
554                 POSITION cy = 0, cx = 0;
555                 MONSTER_IDX m_idx;
556
557                 if (!(party_mon[i].r_idx)) continue;
558
559                 if (i == 0)
560                 {
561                         m_idx = m_pop();
562                         master_ptr->riding = m_idx;
563                         if (m_idx)
564                         {
565                                 cy = master_ptr->y;
566                                 cx = master_ptr->x;
567                         }
568                 }
569                 else
570                 {
571                         int j;
572                         POSITION d;
573
574                         for (d = 1; d < A_MAX; d++)
575                         {
576                                 for (j = 1000; j > 0; j--)
577                                 {
578                                         scatter(&cy, &cx, master_ptr->y, master_ptr->x, d, 0);
579                                         if (monster_can_enter(cy, cx, &r_info[party_mon[i].r_idx], 0)) break;
580                                 }
581                                 if (j) break;
582                         }
583                         m_idx = (d == 6) ? 0 : m_pop();
584                 }
585
586                 if (m_idx)
587                 {
588                         monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[m_idx];
589                         monster_race *r_ptr;
590
591                         p_ptr->current_floor_ptr->grid_array[cy][cx].m_idx = m_idx;
592
593                         m_ptr->r_idx = party_mon[i].r_idx;
594
595                         /* Copy all member of the structure */
596                         *m_ptr = party_mon[i];
597                         r_ptr = real_r_ptr(m_ptr);
598
599                         m_ptr->fy = cy;
600                         m_ptr->fx = cx;
601                         m_ptr->current_floor_ptr = master_ptr->current_floor_ptr;
602                         m_ptr->ml = TRUE;
603                         m_ptr->mtimed[MTIMED_CSLEEP] = 0;
604                         m_ptr->hold_o_idx = 0;
605                         m_ptr->target_y = 0;
606
607                         if ((r_ptr->flags1 & RF1_FORCE_SLEEP) && !ironman_nightmare)
608                         {
609                                 /* Monster is still being nice */
610                                 m_ptr->mflag |= (MFLAG_NICE);
611
612                                 /* Must repair monsters */
613                                 repair_monsters = TRUE;
614                         }
615                         update_monster(master_ptr, m_idx, TRUE);
616                         lite_spot(cy, cx);
617
618                         /* Pre-calculated in precalc_cur_num_of_pet() */
619                         /* r_ptr->cur_num++; */
620
621                         /* Hack -- Count the number of "reproducers" */
622                         if (r_ptr->flags2 & RF2_MULTIPLY) p_ptr->current_floor_ptr->num_repro++;
623
624                 }
625                 else
626                 {
627                         monster_type *m_ptr = &party_mon[i];
628                         monster_race *r_ptr = real_r_ptr(m_ptr);
629                         GAME_TEXT m_name[MAX_NLEN];
630
631                         monster_desc(m_name, m_ptr, 0);
632                         msg_format(_("%sとはぐれてしまった。", "You have lost sight of %s."), m_name);
633                         if (record_named_pet && m_ptr->nickname)
634                         {
635                                 monster_desc(m_name, m_ptr, MD_INDEF_VISIBLE);
636                                 exe_write_diary(master_ptr, NIKKI_NAMED_PET, RECORD_NAMED_PET_LOST_SIGHT, m_name);
637                         }
638
639                         /* Pre-calculated in precalc_cur_num_of_pet(), but need to decrease */
640                         if (r_ptr->cur_num) r_ptr->cur_num--;
641                 }
642         }
643
644         /* For accuracy of precalc_cur_num_of_pet() */
645         (void)C_WIPE(party_mon, MAX_PARTY_MON, monster_type);
646 }
647
648
649 /*!
650  * @brief ユニークモンスターやアーティファクトの所在フロアを更新する / Hack -- Update location of unique monsters and artifacts
651  * @param cur_floor_id 現在のフロアID
652  * @return なし
653  * @details 
654  * The r_ptr->floor_id and a_ptr->floor_id are not updated correctly\n
655  * while new floor creation since dungeons may be re-created by\n
656  * auto-scum option.\n
657  */
658 static void update_unique_artifact(s16b cur_floor_id)
659 {
660         int i;
661
662         /* Maintain unique monsters */
663         for (i = 1; i < p_ptr->current_floor_ptr->m_max; i++)
664         {
665                 monster_race *r_ptr;
666                 monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[i];
667
668                 if (!monster_is_valid(m_ptr)) continue;
669
670                 /* Extract real monster race */
671                 r_ptr = real_r_ptr(m_ptr);
672
673                 /* Memorize location of the unique monster */
674                 if ((r_ptr->flags1 & RF1_UNIQUE) ||
675                     (r_ptr->flags7 & RF7_NAZGUL))
676                 {
677                         r_ptr->floor_id = cur_floor_id;
678                 }
679         }
680
681         /* Maintain artifatcs */
682         for (i = 1; i < p_ptr->current_floor_ptr->o_max; i++)
683         {
684                 object_type *o_ptr = &p_ptr->current_floor_ptr->o_list[i];
685
686                 if (!OBJECT_IS_VALID(o_ptr)) continue;
687
688                 /* Memorize location of the artifact */
689                 if (object_is_fixed_artifact(o_ptr))
690                 {
691                         a_info[o_ptr->name1].floor_id = cur_floor_id;
692                 }
693         }
694 }
695
696
697 /*!
698  * @brief フロア移動時、プレイヤーの移動先モンスターが既にいた場合ランダムな近隣に移動させる / When a monster is at a place where player will return,
699  * @return なし
700  */
701 static void get_out_monster(floor_type *floor_ptr, player_type *protected_ptr)
702 {
703         int tries = 0;
704         POSITION dis = 1;
705         POSITION oy = protected_ptr->y;
706         POSITION ox = protected_ptr->x;
707         MONSTER_IDX m_idx = floor_ptr->grid_array[oy][ox].m_idx;
708
709         /* Nothing to do if no monster */
710         if (!m_idx) return;
711
712         /* Look until done */
713         while (TRUE)
714         {
715                 monster_type *m_ptr;
716
717                 /* Pick a (possibly illegal) location */
718                 POSITION ny = rand_spread(oy, dis);
719                 POSITION nx = rand_spread(ox, dis);
720
721                 tries++;
722
723                 /* Stop after 1000 tries */
724                 if (tries > 10000) return;
725
726                 /*
727                  * Increase distance after doing enough tries
728                  * compared to area of possible space
729                  */
730                 if (tries > 20 * dis * dis) dis++;
731
732                 /* Ignore illegal locations */
733                 if (!in_bounds(floor_ptr, ny, nx)) continue;
734
735                 /* Require "empty" floor space */
736                 if (!cave_empty_bold(p_ptr->current_floor_ptr, ny, nx)) continue;
737
738                 /* Hack -- no teleport onto glyph of warding */
739                 if (is_glyph_grid(&floor_ptr->grid_array[ny][nx])) continue;
740                 if (is_explosive_rune_grid(&floor_ptr->grid_array[ny][nx])) continue;
741
742                 /* ...nor onto the Pattern */
743                 if (pattern_tile(ny, nx)) continue;
744
745                 /*** It's a good place ***/
746
747                 m_ptr = &floor_ptr->m_list[m_idx];
748
749                 /* Update the old location */
750                 floor_ptr->grid_array[oy][ox].m_idx = 0;
751
752                 /* Update the new location */
753                 floor_ptr->grid_array[ny][nx].m_idx = m_idx;
754
755                 /* Move the monster */
756                 m_ptr->fy = ny;
757                 m_ptr->fx = nx; 
758
759                 /* No need to do update_monster() */
760
761                 /* Success */
762                 return;
763         }
764 }
765
766 /*!
767  * @brief 新フロアに移動元フロアに繋がる階段を配置する / Virtually teleport onto the stairs that is connecting between two floors.
768  * @param sf_ptr 移動元の保存フロア構造体参照ポインタ
769  * @return なし
770  */
771 static void locate_connected_stairs(player_type *creature_ptr, floor_type *floor_ptr, saved_floor_type *sf_ptr, BIT_FLAGS floor_mode)
772 {
773         POSITION x, y, sx = 0, sy = 0;
774         POSITION x_table[20];
775         POSITION y_table[20];
776         int num = 0;
777         int i;
778
779         /* Search usable stairs */
780         for (y = 0; y < floor_ptr->height; y++)
781         {
782                 for (x = 0; x < floor_ptr->width; x++)
783                 {
784                         grid_type *g_ptr = &floor_ptr->grid_array[y][x];
785                         feature_type *f_ptr = &f_info[g_ptr->feat];
786                         bool ok = FALSE;
787
788                         if (floor_mode & CFM_UP)
789                         {
790                                 if (have_flag(f_ptr->flags, FF_LESS) && have_flag(f_ptr->flags, FF_STAIRS) &&
791                                     !have_flag(f_ptr->flags, FF_SPECIAL))
792                                 {
793                                         ok = TRUE;
794
795                                         /* Found fixed stairs? */
796                                         if (g_ptr->special &&
797                                             g_ptr->special == sf_ptr->upper_floor_id)
798                                         {
799                                                 sx = x;
800                                                 sy = y;
801                                         }
802                                 }
803                         }
804
805                         else if (floor_mode & CFM_DOWN)
806                         {
807                                 if (have_flag(f_ptr->flags, FF_MORE) && have_flag(f_ptr->flags, FF_STAIRS) &&
808                                     !have_flag(f_ptr->flags, FF_SPECIAL))
809                                 {
810                                         ok = TRUE;
811
812                                         /* Found fixed stairs */
813                                         if (g_ptr->special &&
814                                             g_ptr->special == sf_ptr->lower_floor_id)
815                                         {
816                                                 sx = x;
817                                                 sy = y;
818                                         }
819                                 }
820                         }
821
822                         else
823                         {
824                                 if (have_flag(f_ptr->flags, FF_BLDG))
825                                 {
826                                         ok = TRUE;
827                                 }
828                         }
829
830                         if (ok && (num < 20))
831                         {
832                                 x_table[num] = x;
833                                 y_table[num] = y;
834                                 num++;
835                         }
836                 }
837         }
838
839         if (sx)
840         {
841                 /* Already fixed */
842                 creature_ptr->y = sy;
843                 creature_ptr->x = sx;
844         }
845         else if (!num)
846         {
847                 /* No stairs found! -- No return */
848                 prepare_change_floor_mode(CFM_RAND_PLACE | CFM_NO_RETURN);
849
850                 /* Mega Hack -- It's not the stairs you enter.  Disable it.  */
851                 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;
852         }
853         else
854         {
855                 /* Choose random one */
856                 i = randint0(num);
857
858                 /* Point stair location */
859                 creature_ptr->y = y_table[i];
860                 creature_ptr->x = x_table[i];
861         }
862 }
863
864 /*!
865  * @brief 現在のフロアを離れるに伴って行なわれる保存処理
866  * / Maintain quest monsters, mark next floor_id at stairs, save current floor, and prepare to enter next floor.
867  * @return なし
868  */
869 void leave_floor(player_type *creature_ptr)
870 {
871         grid_type *g_ptr = NULL;
872         feature_type *f_ptr;
873         saved_floor_type *sf_ptr;
874         MONRACE_IDX quest_r_idx = 0;
875         DUNGEON_IDX i;
876         FLOOR_IDX tmp_floor_idx = 0;
877         
878         /* Preserve pets and prepare to take these to next floor */
879         preserve_pet(creature_ptr);
880
881         /* Remove all mirrors without explosion */
882         remove_all_mirrors(creature_ptr, FALSE);
883
884         if (creature_ptr->special_defense & NINJA_S_STEALTH) set_superstealth(creature_ptr, FALSE);
885
886         /* New floor is not yet prepared */
887         new_floor_id = 0;
888
889         /* Temporary get a floor_id (for Arena) */
890         if (!creature_ptr->floor_id &&
891             (creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) &&
892             !(creature_ptr->change_floor_mode & CFM_NO_RETURN))
893         {
894             /* Get temporal floor_id */
895                 tmp_floor_idx = get_new_floor_id();
896         }
897
898         /* Search the quest monster index */
899         for (i = 0; i < max_q_idx; i++)
900         {
901                 if ((quest[i].status == QUEST_STATUS_TAKEN) &&
902                     ((quest[i].type == QUEST_TYPE_KILL_LEVEL) ||
903                     (quest[i].type == QUEST_TYPE_RANDOM)) &&
904                     (quest[i].level == p_ptr->current_floor_ptr->dun_level) &&
905                     (creature_ptr->dungeon_idx == quest[i].dungeon) &&
906                     !(quest[i].flags & QUEST_FLAG_PRESET))
907                 {
908                         quest_r_idx = quest[i].r_idx;
909                 }
910         }
911
912         /* Maintain quest monsters */
913         for (i = 1; i < p_ptr->current_floor_ptr->m_max; i++)
914         {
915                 monster_race *r_ptr;
916                 monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[i];
917
918                 if (!monster_is_valid(m_ptr)) continue;
919
920                 /* Only maintain quest monsters */
921                 if (quest_r_idx != m_ptr->r_idx) continue;
922
923                 /* Extract real monster race */
924                 r_ptr = real_r_ptr(m_ptr);
925
926                 /* Ignore unique monsters */
927                 if ((r_ptr->flags1 & RF1_UNIQUE) ||
928                     (r_ptr->flags7 & RF7_NAZGUL)) continue;
929
930                 /* Delete non-unique quest monsters */
931                 delete_monster_idx(i);
932         }
933
934         /* Check if there is a same item */
935         for (i = 0; i < INVEN_PACK; i++)
936         {
937                 object_type *o_ptr = &creature_ptr->inventory_list[i];
938
939                 if (!OBJECT_IS_VALID(o_ptr)) continue;
940
941                 /* Delete old memorized location of the artifact */
942                 if (object_is_fixed_artifact(o_ptr))
943                 {
944                         a_info[o_ptr->name1].floor_id = 0;
945                 }
946         }
947
948         /* Extract current floor info or NULL */
949         sf_ptr = get_sf_ptr(tmp_floor_idx);
950
951         /* Choose random stairs */
952         if ((creature_ptr->change_floor_mode & CFM_RAND_CONNECT) && tmp_floor_idx)
953         {
954                 locate_connected_stairs(creature_ptr, creature_ptr->current_floor_ptr, sf_ptr, creature_ptr->change_floor_mode);
955         }
956
957         /* Extract new dungeon level */
958         if (creature_ptr->change_floor_mode & CFM_SAVE_FLOORS)
959         {
960                 /* Extract stair position */
961                 g_ptr = &p_ptr->current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
962                 f_ptr = &f_info[g_ptr->feat];
963
964                 /* Get back to old saved floor? */
965                 if (g_ptr->special && !have_flag(f_ptr->flags, FF_SPECIAL) && get_sf_ptr(g_ptr->special))
966                 {
967                         /* Saved floor is exist.  Use it. */
968                         new_floor_id = g_ptr->special;
969                 }
970
971                 /* Mark shaft up/down */
972                 if (have_flag(f_ptr->flags, FF_STAIRS) && have_flag(f_ptr->flags, FF_SHAFT))
973                 {
974                         prepare_change_floor_mode(CFM_SHAFT);
975                 }
976         }
977
978         /* Climb up/down some sort of stairs */
979         if (creature_ptr->change_floor_mode & (CFM_DOWN | CFM_UP))
980         {
981                 int move_num = 0;
982
983                 /* Extract level movement number */
984                 if (creature_ptr->change_floor_mode & CFM_DOWN) move_num = 1;
985                 else if (creature_ptr->change_floor_mode & CFM_UP) move_num = -1;
986
987                 /* Shafts are deeper than normal stairs */
988                 if (creature_ptr->change_floor_mode & CFM_SHAFT)
989                         move_num += SGN(move_num);
990
991                 /* Get out from or Enter the dungeon */
992                 if (creature_ptr->change_floor_mode & CFM_DOWN)
993                 {
994                         if (!p_ptr->current_floor_ptr->dun_level)
995                                 move_num = d_info[creature_ptr->dungeon_idx].mindepth;
996                 }
997                 else if (creature_ptr->change_floor_mode & CFM_UP)
998                 {
999                         if (p_ptr->current_floor_ptr->dun_level + move_num < d_info[creature_ptr->dungeon_idx].mindepth)
1000                                 move_num = -p_ptr->current_floor_ptr->dun_level;
1001                 }
1002
1003                 p_ptr->current_floor_ptr->dun_level += move_num;
1004         }
1005
1006         /* Leaving the dungeon to town */
1007         if (!p_ptr->current_floor_ptr->dun_level && creature_ptr->dungeon_idx)
1008         {
1009                 creature_ptr->leaving_dungeon = TRUE;
1010                 if (!vanilla_town && !lite_town)
1011                 {
1012                         creature_ptr->wilderness_y = d_info[creature_ptr->dungeon_idx].dy;
1013                         creature_ptr->wilderness_x = d_info[creature_ptr->dungeon_idx].dx;
1014                 }
1015                 creature_ptr->recall_dungeon = creature_ptr->dungeon_idx;
1016                 creature_ptr->dungeon_idx = 0;
1017
1018                 /* Reach to the surface -- Clear all saved floors */
1019                 creature_ptr->change_floor_mode &= ~CFM_SAVE_FLOORS; // TODO
1020         }
1021
1022         /* Kill some old saved floors */
1023         if (!(creature_ptr->change_floor_mode & CFM_SAVE_FLOORS))
1024         {
1025                 /* Kill all saved floors */
1026                 for (i = 0; i < MAX_SAVED_FLOORS; i++)
1027                         kill_saved_floor(&saved_floors[i]);
1028
1029                 /* Reset visit_mark count */
1030                 latest_visit_mark = 1;
1031         }
1032         else if (creature_ptr->change_floor_mode & CFM_NO_RETURN)
1033         {
1034                 /* Kill current floor */
1035                 kill_saved_floor(sf_ptr);
1036         }
1037
1038         /* No current floor -- Left/Enter dungeon etc... */
1039         if (!tmp_floor_idx)
1040         {
1041                 /* No longer need to save current floor */
1042                 return;
1043         }
1044
1045         /* Mark next floor_id on the previous floor */
1046         if (!new_floor_id)
1047         {
1048                 /* Get new id */
1049                 new_floor_id = get_new_floor_id();
1050
1051                 /* Connect from here */
1052                 if (g_ptr && !feat_uses_special(g_ptr->feat))
1053                 {
1054                         g_ptr->special = tmp_floor_idx;
1055                 }
1056         }
1057
1058         /* Fix connection -- level teleportation or trap door */
1059         if (creature_ptr->change_floor_mode & CFM_RAND_CONNECT)
1060         {
1061                 if (creature_ptr->change_floor_mode & CFM_UP)
1062                         sf_ptr->upper_floor_id = new_floor_id;
1063                 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1064                         sf_ptr->lower_floor_id = new_floor_id;
1065         }
1066
1067         /* If you can return, you need to save previous floor */
1068         if ((creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) &&
1069             !(creature_ptr->change_floor_mode & CFM_NO_RETURN))
1070         {
1071                 /* Get out of the my way! */
1072                 get_out_monster(p_ptr->current_floor_ptr, creature_ptr);
1073
1074                 /* Record the last visit turn of current floor */
1075                 sf_ptr->last_visit = current_world_ptr->game_turn;
1076
1077                 forget_lite(p_ptr->current_floor_ptr);
1078                 forget_view(p_ptr->current_floor_ptr);
1079                 clear_mon_lite(p_ptr->current_floor_ptr);
1080
1081                 /* Save current floor */
1082                 if (!save_floor(sf_ptr, 0))
1083                 {
1084                         /* Save failed -- No return */
1085                         prepare_change_floor_mode(CFM_NO_RETURN);
1086
1087                         /* Kill current floor */
1088                         kill_saved_floor(get_sf_ptr(creature_ptr->floor_id));
1089                 }
1090         }
1091 }
1092
1093
1094 /*!
1095  * @brief フロアの切り替え処理 / Enter new floor.
1096  * @return なし
1097  * @details
1098  * If the floor is an old saved floor, it will be\n
1099  * restored from the temporal file.  If the floor is new one, new floor\n
1100  * will be generated.\n
1101  */
1102 void change_floor(player_type *creature_ptr)
1103 {
1104         saved_floor_type *sf_ptr;
1105         bool loaded = FALSE;
1106
1107         /* The dungeon is not ready */
1108         current_world_ptr->character_dungeon = FALSE;
1109
1110         /* No longer in the trap detecteded region */
1111         creature_ptr->dtrap = FALSE;
1112
1113         /* Mega-Hack -- no panel yet */
1114         panel_row_min = 0;
1115         panel_row_max = 0;
1116         panel_col_min = 0;
1117         panel_col_max = 0;
1118
1119         /* Mega-Hack -- not ambushed on the wildness? */
1120         creature_ptr->ambush_flag = FALSE;
1121
1122         /* No saved floors (On the surface etc.) */
1123         if (!(creature_ptr->change_floor_mode & CFM_SAVE_FLOORS) &&
1124             !(creature_ptr->change_floor_mode & CFM_FIRST_FLOOR))
1125         {
1126                 generate_floor(creature_ptr->current_floor_ptr);
1127
1128                 /* Paranoia -- No new saved floor */
1129                 new_floor_id = 0;
1130         }
1131
1132         /* In the dungeon */
1133         else
1134         {
1135                 /* No floor_id yet */
1136                 if (!new_floor_id)
1137                 {
1138                         /* Get new id */
1139                         new_floor_id = get_new_floor_id();
1140                 }
1141
1142                 /* Pointer for infomations of new floor */
1143                 sf_ptr = get_sf_ptr(new_floor_id);
1144
1145                 /* Try to restore old floor */
1146                 if (sf_ptr->last_visit)
1147                 {
1148                         /* Old saved floor is exist */
1149                         if (load_floor(sf_ptr, 0))
1150                         {
1151                                 loaded = TRUE;
1152
1153                                 /* Forbid return stairs */
1154                                 if (creature_ptr->change_floor_mode & CFM_NO_RETURN)
1155                                 {
1156                                         grid_type *g_ptr = &creature_ptr->current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
1157
1158                                         if (!feat_uses_special(g_ptr->feat))
1159                                         {
1160                                                 if (creature_ptr->change_floor_mode & (CFM_DOWN | CFM_UP))
1161                                                 {
1162                                                         /* Reset to floor */
1163                                                         g_ptr->feat = feat_ground_type[randint0(100)];
1164                                                 }
1165
1166                                                 g_ptr->special = 0;
1167                                         }
1168                                 }
1169                         }
1170                 }
1171
1172                 /*
1173                  * Set lower/upper_floor_id of new floor when the new
1174                  * floor is right-above/right-under the current floor.
1175                  *
1176                  * Stair creation/Teleport level/Trap door will take
1177                  * you the same floor when you used it later again.
1178                  */
1179                 if (creature_ptr->floor_id)
1180                 {
1181                         saved_floor_type *cur_sf_ptr = get_sf_ptr(creature_ptr->floor_id);
1182
1183                         if (creature_ptr->change_floor_mode & CFM_UP)
1184                         {
1185                                 /* New floor is right-above */
1186                                 if (cur_sf_ptr->upper_floor_id == new_floor_id)
1187                                         sf_ptr->lower_floor_id = creature_ptr->floor_id;
1188                         }
1189                         else if (creature_ptr->change_floor_mode & CFM_DOWN)
1190                         {
1191                                 /* New floor is right-under */
1192                                 if (cur_sf_ptr->lower_floor_id == new_floor_id)
1193                                         sf_ptr->upper_floor_id = creature_ptr->floor_id;
1194                         }
1195                 }
1196
1197                 /* Break connection to killed floor */
1198                 else
1199                 {
1200                         if (creature_ptr->change_floor_mode & CFM_UP)
1201                                 sf_ptr->lower_floor_id = 0;
1202                         else if (creature_ptr->change_floor_mode & CFM_DOWN)
1203                                 sf_ptr->upper_floor_id = 0;
1204                 }
1205
1206                 /* Maintain monsters and artifacts */
1207                 if (loaded)
1208                 {
1209                         MONSTER_IDX i;
1210                         GAME_TURN tmp_last_visit = sf_ptr->last_visit;
1211                         GAME_TURN absence_ticks;
1212                         int alloc_chance = d_info[creature_ptr->dungeon_idx].max_m_alloc_chance;
1213                         GAME_TURN alloc_times;
1214
1215                         while (tmp_last_visit > current_world_ptr->game_turn) tmp_last_visit -= TURNS_PER_TICK * TOWN_DAWN;
1216                         absence_ticks = (current_world_ptr->game_turn - tmp_last_visit) / TURNS_PER_TICK;
1217
1218                         /* Maintain monsters */
1219                         for (i = 1; i < creature_ptr->current_floor_ptr->m_max; i++)
1220                         {
1221                                 monster_race *r_ptr;
1222                                 monster_type *m_ptr = &creature_ptr->current_floor_ptr->m_list[i];
1223
1224                                 if (!monster_is_valid(m_ptr)) continue;
1225
1226                                 if (!is_pet(m_ptr))
1227                                 {
1228                                         /* Restore HP */
1229                                         m_ptr->hp = m_ptr->maxhp = m_ptr->max_maxhp;
1230
1231                                         /* Remove timed status (except MTIMED_CSLEEP) */
1232                                         (void)set_monster_fast(i, 0);
1233                                         (void)set_monster_slow(i, 0);
1234                                         (void)set_monster_stunned(i, 0);
1235                                         (void)set_monster_confused(i, 0);
1236                                         (void)set_monster_monfear(i, 0);
1237                                         (void)set_monster_invulner(i, 0, FALSE);
1238                                 }
1239
1240                                 /* Extract real monster race */
1241                                 r_ptr = real_r_ptr(m_ptr);
1242
1243                                 /* Ignore non-unique */
1244                                 if (!(r_ptr->flags1 & RF1_UNIQUE) &&
1245                                     !(r_ptr->flags7 & RF7_NAZGUL)) continue;
1246
1247                                 /* Appear at a different floor? */
1248                                 if (r_ptr->floor_id != new_floor_id)
1249                                 {
1250                                         /* Disapper from here */
1251                                         delete_monster_idx(i);
1252                                 }
1253                         }
1254
1255                         /* Maintain artifatcs */
1256                         for (i = 1; i < creature_ptr->current_floor_ptr->o_max; i++)
1257                         {
1258                                 object_type *o_ptr = &creature_ptr->current_floor_ptr->o_list[i];
1259
1260                                 if (!OBJECT_IS_VALID(o_ptr)) continue;
1261
1262                                 /* Ignore non-artifact */
1263                                 if (!object_is_fixed_artifact(o_ptr)) continue;
1264
1265                                 /* Appear at a different floor? */
1266                                 if (a_info[o_ptr->name1].floor_id != new_floor_id)
1267                                 {
1268                                         /* Disappear from here */
1269                                         delete_object_idx(i);
1270                                 }
1271                                 else
1272                                 {
1273                                         /* Cancel preserve */
1274                                         a_info[o_ptr->name1].cur_num = 1;
1275                                 }
1276                         }
1277
1278                         (void)place_quest_monsters(creature_ptr->current_floor_ptr, creature_ptr);
1279
1280                         /* Place some random monsters */
1281                         alloc_times = absence_ticks / alloc_chance;
1282
1283                         if (randint0(alloc_chance) < (absence_ticks % alloc_chance))
1284                                 alloc_times++;
1285
1286                         for (i = 0; i < alloc_times; i++)
1287                         {
1288                                 /* Make a (group of) new monster */
1289                                 (void)alloc_monster(0, 0);
1290                         }
1291
1292                 }
1293
1294                 /* New floor_id or failed to restore */
1295                 else /* if (!loaded) */
1296                 {
1297                         if (sf_ptr->last_visit)
1298                         {
1299                                 /* Temporal file is broken? */
1300                                 msg_print(_("階段は行き止まりだった。", "The staircases come to a dead end..."));
1301
1302                                 /* Create simple dead end */
1303                                 build_dead_end(creature_ptr->current_floor_ptr);
1304
1305                                 /* Break connection */
1306                                 if (creature_ptr->change_floor_mode & CFM_UP)
1307                                 {
1308                                         sf_ptr->upper_floor_id = 0;
1309                                 }
1310                                 else if (creature_ptr->change_floor_mode & CFM_DOWN)
1311                                 {
1312                                         sf_ptr->lower_floor_id = 0;
1313                                 }
1314                         }
1315                         else
1316                         {
1317                                 generate_floor(creature_ptr->current_floor_ptr);
1318                         }
1319
1320                         /* Record last visit turn */
1321                         sf_ptr->last_visit = current_world_ptr->game_turn;
1322
1323                         /* Set correct dun_level value */
1324                         sf_ptr->dun_level = creature_ptr->current_floor_ptr->dun_level;
1325
1326                         /* Create connected stairs */
1327                         if (!(creature_ptr->change_floor_mode & CFM_NO_RETURN))
1328                         {
1329                                 /* Extract stair position */
1330                                 grid_type *g_ptr = &creature_ptr->current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
1331
1332                                 /*** Create connected stairs ***/
1333
1334                                 /* No stairs down from Quest */
1335                                 if ((creature_ptr->change_floor_mode & CFM_UP) && !quest_number(creature_ptr->current_floor_ptr->dun_level))
1336                                 {
1337                                         g_ptr->feat = (creature_ptr->change_floor_mode & CFM_SHAFT) ? feat_state(feat_down_stair, FF_SHAFT) : feat_down_stair;
1338                                 }
1339
1340                                 /* No stairs up when ironman_downward */
1341                                 else if ((creature_ptr->change_floor_mode & CFM_DOWN) && !ironman_downward)
1342                                 {
1343                                         g_ptr->feat = (creature_ptr->change_floor_mode & CFM_SHAFT) ? feat_state(feat_up_stair, FF_SHAFT) : feat_up_stair;
1344                                 }
1345
1346                                 /* Paranoia -- Clear mimic */
1347                                 g_ptr->mimic = 0;
1348
1349                                 /* Connect to previous floor */
1350                                 g_ptr->special = creature_ptr->floor_id;
1351                         }
1352                 }
1353
1354                 /* Arrive at random grid */
1355                 if (creature_ptr->change_floor_mode & (CFM_RAND_PLACE))
1356                 {
1357                         (void)new_player_spot(creature_ptr);
1358                 }
1359
1360                 /* You see stairs blocked */
1361                 else if ((creature_ptr->change_floor_mode & CFM_NO_RETURN) && (creature_ptr->change_floor_mode & (CFM_DOWN | CFM_UP)))
1362                 {
1363                         if (!creature_ptr->blind)
1364                         {
1365                                 msg_print(_("突然階段が塞がれてしまった。", "Suddenly the stairs is blocked!"));
1366                         }
1367                         else
1368                         {
1369                                 msg_print(_("ゴトゴトと何か音がした。", "You hear some noises."));
1370                         }
1371                 }
1372
1373                 /*
1374                  * Update visit mark
1375                  *
1376                  * The "turn" is not always different number because
1377                  * the level teleport doesn't take any turn.  Use
1378                  * visit mark instead of last visit turn to find the
1379                  * oldest saved floor.
1380                  */
1381                 sf_ptr->visit_mark = latest_visit_mark++;
1382         }
1383
1384         /* Place preserved pet monsters */
1385         place_pet(creature_ptr);
1386
1387         /* Reset travel target place */
1388         forget_travel_flow();
1389
1390         /* Hack -- maintain unique and artifacts */
1391         update_unique_artifact(new_floor_id);
1392
1393         /* Now the player is in new floor */
1394         creature_ptr->floor_id = new_floor_id;
1395
1396         /* The dungeon is ready */
1397         current_world_ptr->character_dungeon = TRUE;
1398
1399         /* Hack -- Munchkin characters always get whole map */
1400         if (creature_ptr->pseikaku == SEIKAKU_MUNCHKIN)
1401                 wiz_lite(creature_ptr, (bool)(creature_ptr->pclass == CLASS_NINJA));
1402
1403         /* Remember when this level was "created" */
1404         creature_ptr->current_floor_ptr->generated_turn = current_world_ptr->game_turn;
1405
1406         /* No dungeon feeling yet */
1407         creature_ptr->feeling_turn = creature_ptr->current_floor_ptr->generated_turn;
1408         creature_ptr->feeling = 0;
1409
1410         /* Clear all flags */
1411         creature_ptr->change_floor_mode = 0L;
1412
1413         select_floor_music(creature_ptr);
1414         creature_ptr->change_floor_mode = 0;
1415 }