OSDN Git Service

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