OSDN Git Service

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