OSDN Git Service

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