OSDN Git Service

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