OSDN Git Service

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