OSDN Git Service

[Refactor] #37353 メッセージ整理。/Refactor messages.
[hengbandforosx/hengbandosx.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
469                                 /* Acquire the monster name */
470                                 monster_desc(m_name, m_ptr, 0);
471                                 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
472                         }
473
474
475                         delete_monster_idx(i);
476                 }
477         }
478 }
479
480
481 /*!
482  * @brief フロア移動時にペットを伴った場合の準備処理 / Pre-calculate the racial counters of preserved pets
483  * @return なし
484  * @details
485  * To prevent multiple generation of unique monster who is the minion of player
486  */
487 void precalc_cur_num_of_pet(void)
488 {
489         monster_type *m_ptr;
490         int i;
491         int max_num = p_ptr->wild_mode ? 1 : MAX_PARTY_MON;
492
493         for (i = 0; i < max_num; i++)
494         {
495                 m_ptr = &party_mon[i];
496
497                 /* Skip empty monsters */
498                 if (!m_ptr->r_idx) continue;
499
500                 /* Hack -- Increase the racial counter */
501                 real_r_ptr(m_ptr)->cur_num++;
502         }
503 }
504
505 /*!
506  * @brief 移動先のフロアに伴ったペットを配置する / Place preserved pet monsters on new floor
507  * @return なし
508  */
509 static void place_pet(void)
510 {
511         int i;
512         int max_num = p_ptr->wild_mode ? 1 : MAX_PARTY_MON;
513
514         for (i = 0; i < max_num; i++)
515         {
516                 POSITION cy = 0, cx = 0;
517                 MONSTER_IDX m_idx;
518
519                 if (!(party_mon[i].r_idx)) continue;
520
521                 if (i == 0)
522                 {
523                         m_idx = m_pop();
524                         p_ptr->riding = m_idx;
525                         if (m_idx)
526                         {
527                                 cy = p_ptr->y;
528                                 cx = p_ptr->x;
529                         }
530                 }
531                 else
532                 {
533                         int j;
534                         POSITION d;
535
536                         for (d = 1; d < 6; d++)
537                         {
538                                 for (j = 1000; j > 0; j--)
539                                 {
540                                         scatter(&cy, &cx, p_ptr->y, p_ptr->x, d, 0);
541                                         if (monster_can_enter(cy, cx, &r_info[party_mon[i].r_idx], 0)) break;
542                                 }
543                                 if (j) break;
544                         }
545                         m_idx = (d == 6) ? 0 : m_pop();
546                 }
547
548                 if (m_idx)
549                 {
550                         monster_type *m_ptr = &m_list[m_idx];
551                         monster_race *r_ptr;
552
553                         cave[cy][cx].m_idx = m_idx;
554
555                         m_ptr->r_idx = party_mon[i].r_idx;
556
557                         /* Copy all member of the structure */
558                         *m_ptr = party_mon[i];
559                         r_ptr = real_r_ptr(m_ptr);
560
561                         m_ptr->fy = cy;
562                         m_ptr->fx = cx;
563                         m_ptr->ml = TRUE;
564                         m_ptr->mtimed[MTIMED_CSLEEP] = 0;
565
566                         /* Paranoia */
567                         m_ptr->hold_o_idx = 0;
568                         m_ptr->target_y = 0;
569
570                         if ((r_ptr->flags1 & RF1_FORCE_SLEEP) && !ironman_nightmare)
571                         {
572                                 /* Monster is still being nice */
573                                 m_ptr->mflag |= (MFLAG_NICE);
574
575                                 /* Must repair monsters */
576                                 repair_monsters = TRUE;
577                         }
578
579                         /* Update the monster */
580                         update_mon(m_idx, TRUE);
581                         lite_spot(cy, cx);
582
583                         /* Pre-calculated in precalc_cur_num_of_pet() */
584                         /* r_ptr->cur_num++; */
585
586                         /* Hack -- Count the number of "reproducers" */
587                         if (r_ptr->flags2 & RF2_MULTIPLY) num_repro++;
588
589                         /* Hack -- Notice new multi-hued monsters */
590                         {
591                                 monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
592                                 if (ap_r_ptr->flags1 & (RF1_ATTR_MULTI | RF1_SHAPECHANGER))
593                                         shimmer_monsters = TRUE;
594                         }
595                 }
596                 else
597                 {
598                         monster_type *m_ptr = &party_mon[i];
599                         monster_race *r_ptr = real_r_ptr(m_ptr);
600                         char m_name[80];
601
602                         monster_desc(m_name, m_ptr, 0);
603                         msg_format(_("%sとはぐれてしまった。", "You have lost sight of %s."), m_name);
604                         if (record_named_pet && m_ptr->nickname)
605                         {
606                                 monster_desc(m_name, m_ptr, MD_INDEF_VISIBLE);
607                                 do_cmd_write_nikki(NIKKI_NAMED_PET, RECORD_NAMED_PET_LOST_SIGHT, m_name);
608                         }
609
610                         /* Pre-calculated in precalc_cur_num_of_pet(), but need to decrease */
611                         if (r_ptr->cur_num) r_ptr->cur_num--;
612                 }
613         }
614
615         /* For accuracy of precalc_cur_num_of_pet() */
616         (void)C_WIPE(party_mon, MAX_PARTY_MON, monster_type);
617 }
618
619
620 /*!
621  * @brief ユニークモンスターやアーティファクトの所在フロアを更新する / Hack -- Update location of unique monsters and artifacts
622  * @param cur_floor_id 現在のフロアID
623  * @return なし
624  * @details 
625  * The r_ptr->floor_id and a_ptr->floor_id are not updated correctly\n
626  * while new floor creation since dungeons may be re-created by\n
627  * auto-scum option.\n
628  */
629 static void update_unique_artifact(s16b cur_floor_id)
630 {
631         int i;
632
633         /* Maintain unique monsters */
634         for (i = 1; i < m_max; i++)
635         {
636                 monster_race *r_ptr;
637                 monster_type *m_ptr = &m_list[i];
638
639                 /* Skip dead monsters */
640                 if (!m_ptr->r_idx) continue;
641
642                 /* Extract real monster race */
643                 r_ptr = real_r_ptr(m_ptr);
644
645                 /* Memorize location of the unique monster */
646                 if ((r_ptr->flags1 & RF1_UNIQUE) ||
647                     (r_ptr->flags7 & RF7_NAZGUL))
648                 {
649                         r_ptr->floor_id = cur_floor_id;
650                 }
651         }
652
653         /* Maintain artifatcs */
654         for (i = 1; i < o_max; i++)
655         {
656                 object_type *o_ptr = &o_list[i];
657
658                 /* Skip dead objects */
659                 if (!o_ptr->k_idx) continue;
660
661                 /* Memorize location of the artifact */
662                 if (object_is_fixed_artifact(o_ptr))
663                 {
664                         a_info[o_ptr->name1].floor_id = cur_floor_id;
665                 }
666         }
667 }
668
669
670 /*!
671  * @brief フロア移動時、プレイヤーの移動先モンスターが既にいた場合ランダムな近隣に移動させる / When a monster is at a place where player will return,
672  * @return なし
673  */
674 static void get_out_monster(void)
675 {
676         int tries = 0;
677         POSITION dis = 1;
678         int oy = p_ptr->y;
679         int ox = p_ptr->x;
680         MONSTER_IDX m_idx = cave[oy][ox].m_idx;
681
682         /* Nothing to do if no monster */
683         if (!m_idx) return;
684
685         /* Look until done */
686         while (TRUE)
687         {
688                 monster_type *m_ptr;
689
690                 /* Pick a (possibly illegal) location */
691                 int ny = rand_spread(oy, dis);
692                 int nx = rand_spread(ox, dis);
693
694                 tries++;
695
696                 /* Stop after 1000 tries */
697                 if (tries > 10000) return;
698
699                 /*
700                  * Increase distance after doing enough tries
701                  * compared to area of possible space
702                  */
703                 if (tries > 20 * dis * dis) dis++;
704
705                 /* Ignore illegal locations */
706                 if (!in_bounds(ny, nx)) continue;
707
708                 /* Require "empty" floor space */
709                 if (!cave_empty_bold(ny, nx)) continue;
710
711                 /* Hack -- no teleport onto glyph of warding */
712                 if (is_glyph_grid(&cave[ny][nx])) continue;
713                 if (is_explosive_rune_grid(&cave[ny][nx])) continue;
714
715                 /* ...nor onto the Pattern */
716                 if (pattern_tile(ny, nx)) continue;
717
718                 /*** It's a good place ***/
719
720                 m_ptr = &m_list[m_idx];
721
722                 /* Update the old location */
723                 cave[oy][ox].m_idx = 0;
724
725                 /* Update the new location */
726                 cave[ny][nx].m_idx = m_idx;
727
728                 /* Move the monster */
729                 m_ptr->fy = ny;
730                 m_ptr->fx = nx; 
731
732                 /* No need to do update_mon() */
733
734                 /* Success */
735                 return;
736         }
737 }
738
739 /*!
740  * マス構造体のspecial要素を利用する地形かどうかを判定するマクロ / Is this feature has special meaning (except floor_id) with c_ptr->special?
741  */
742 #define feat_uses_special(F) (have_flag(f_info[(F)].flags, FF_SPECIAL))
743
744
745 /*!
746  * @brief 新フロアに移動元フロアに繋がる階段を配置する / Virtually teleport onto the stairs that is connecting between two floors.
747  * @param sf_ptr 移動元の保存フロア構造体参照ポインタ
748  * @return なし
749  */
750 static void locate_connected_stairs(saved_floor_type *sf_ptr)
751 {
752         POSITION x, y, sx = 0, sy = 0;
753         POSITION x_table[20];
754         POSITION y_table[20];
755         int num = 0;
756         int i;
757
758         /* Search usable stairs */
759         for (y = 0; y < cur_hgt; y++)
760         {
761                 for (x = 0; x < cur_wid; x++)
762                 {
763                         cave_type *c_ptr = &cave[y][x];
764                         feature_type *f_ptr = &f_info[c_ptr->feat];
765                         bool ok = FALSE;
766
767                         if (change_floor_mode & CFM_UP)
768                         {
769                                 if (have_flag(f_ptr->flags, FF_LESS) && have_flag(f_ptr->flags, FF_STAIRS) &&
770                                     !have_flag(f_ptr->flags, FF_SPECIAL))
771                                 {
772                                         ok = TRUE;
773
774                                         /* Found fixed stairs? */
775                                         if (c_ptr->special &&
776                                             c_ptr->special == sf_ptr->upper_floor_id)
777                                         {
778                                                 sx = x;
779                                                 sy = y;
780                                         }
781                                 }
782                         }
783
784                         else if (change_floor_mode & CFM_DOWN)
785                         {
786                                 if (have_flag(f_ptr->flags, FF_MORE) && have_flag(f_ptr->flags, FF_STAIRS) &&
787                                     !have_flag(f_ptr->flags, FF_SPECIAL))
788                                 {
789                                         ok = TRUE;
790
791                                         /* Found fixed stairs */
792                                         if (c_ptr->special &&
793                                             c_ptr->special == sf_ptr->lower_floor_id)
794                                         {
795                                                 sx = x;
796                                                 sy = y;
797                                         }
798                                 }
799                         }
800
801                         else
802                         {
803                                 if (have_flag(f_ptr->flags, FF_BLDG))
804                                 {
805                                         ok = TRUE;
806                                 }
807                         }
808
809                         if (ok && (num < 20))
810                         {
811                                 x_table[num] = x;
812                                 y_table[num] = y;
813                                 num++;
814                         }
815                 }
816         }
817
818         if (sx)
819         {
820                 /* Already fixed */
821                 p_ptr->y = sy;
822                 p_ptr->x = sx;
823         }
824         else if (!num)
825         {
826                 /* No stairs found! -- No return */
827                 prepare_change_floor_mode(CFM_RAND_PLACE | CFM_NO_RETURN);
828
829                 /* Mega Hack -- It's not the stairs you enter.  Disable it.  */
830                 if (!feat_uses_special(cave[p_ptr->y][p_ptr->x].feat)) cave[p_ptr->y][p_ptr->x].special = 0;
831         }
832         else
833         {
834                 /* Choose random one */
835                 i = randint0(num);
836
837                 /* Point stair location */
838                 p_ptr->y = y_table[i];
839                 p_ptr->x = x_table[i];
840         }
841 }
842
843 /*!
844  * @brief 現在のフロアを離れるに伴って行なわれる保存処理
845  * / Maintain quest monsters, mark next floor_id at stairs, save current floor, and prepare to enter next floor.
846  * @return なし
847  */
848 void leave_floor(void)
849 {
850         cave_type *c_ptr = NULL;
851         feature_type *f_ptr;
852         saved_floor_type *sf_ptr;
853         int quest_r_idx = 0;
854         DUNGEON_IDX i;
855
856         /* Preserve pets and prepare to take these to next floor */
857         preserve_pet();
858
859         /* Remove all mirrors without explosion */
860         remove_all_mirrors(FALSE);
861
862         if (p_ptr->special_defense & NINJA_S_STEALTH) set_superstealth(FALSE);
863
864         /* New floor is not yet prepared */
865         new_floor_id = 0;
866
867         /* Temporary get a floor_id (for Arena) */
868         if (!p_ptr->floor_id &&
869             (change_floor_mode & CFM_SAVE_FLOORS) &&
870             !(change_floor_mode & CFM_NO_RETURN))
871         {
872             /* Get temporal floor_id */
873             p_ptr->floor_id = get_new_floor_id();
874         }
875
876
877         /* Search the quest monster index */
878         for (i = 0; i < max_q_idx; i++)
879         {
880                 if ((quest[i].status == QUEST_STATUS_TAKEN) &&
881                     ((quest[i].type == QUEST_TYPE_KILL_LEVEL) ||
882                     (quest[i].type == QUEST_TYPE_RANDOM)) &&
883                     (quest[i].level == dun_level) &&
884                     (dungeon_type == quest[i].dungeon) &&
885                     !(quest[i].flags & QUEST_FLAG_PRESET))
886                 {
887                         quest_r_idx = quest[i].r_idx;
888                 }
889         }
890
891         /* Maintain quest monsters */
892         for (i = 1; i < m_max; i++)
893         {
894                 monster_race *r_ptr;
895                 monster_type *m_ptr = &m_list[i];
896
897                 /* Skip dead monsters */
898                 if (!m_ptr->r_idx) continue;
899
900                 /* Only maintain quest monsters */
901                 if (quest_r_idx != m_ptr->r_idx) continue;
902
903                 /* Extract real monster race */
904                 r_ptr = real_r_ptr(m_ptr);
905
906                 /* Ignore unique monsters */
907                 if ((r_ptr->flags1 & RF1_UNIQUE) ||
908                     (r_ptr->flags7 & RF7_NAZGUL)) continue;
909
910                 /* Delete non-unique quest monsters */
911                 delete_monster_idx(i);
912         }
913
914         /* Check if there is a same item */
915         for (i = 0; i < INVEN_PACK; i++)
916         {
917                 object_type *o_ptr = &inventory[i];
918
919                 /* Skip dead objects */
920                 if (!o_ptr->k_idx) continue;
921
922                 /* Delete old memorized location of the artifact */
923                 if (object_is_fixed_artifact(o_ptr))
924                 {
925                         a_info[o_ptr->name1].floor_id = 0;
926                 }
927         }
928
929         /* Extract current floor info or NULL */
930         sf_ptr = get_sf_ptr(p_ptr->floor_id);
931
932         /* Choose random stairs */
933         if ((change_floor_mode & CFM_RAND_CONNECT) && p_ptr->floor_id)
934         {
935                 locate_connected_stairs(sf_ptr);
936         }
937
938         /* Extract new dungeon level */
939         if (change_floor_mode & CFM_SAVE_FLOORS)
940         {
941                 /* Extract stair position */
942                 c_ptr = &cave[p_ptr->y][p_ptr->x];
943                 f_ptr = &f_info[c_ptr->feat];
944
945                 /* Get back to old saved floor? */
946                 if (c_ptr->special && !have_flag(f_ptr->flags, FF_SPECIAL) && get_sf_ptr(c_ptr->special))
947                 {
948                         /* Saved floor is exist.  Use it. */
949                         new_floor_id = c_ptr->special;
950                 }
951
952                 /* Mark shaft up/down */
953                 if (have_flag(f_ptr->flags, FF_STAIRS) && have_flag(f_ptr->flags, FF_SHAFT))
954                 {
955                         prepare_change_floor_mode(CFM_SHAFT);
956                 }
957         }
958
959         /* Climb up/down some sort of stairs */
960         if (change_floor_mode & (CFM_DOWN | CFM_UP))
961         {
962                 int move_num = 0;
963
964                 /* Extract level movement number */
965                 if (change_floor_mode & CFM_DOWN) move_num = 1;
966                 else if (change_floor_mode & CFM_UP) move_num = -1;
967
968                 /* Shafts are deeper than normal stairs */
969                 if (change_floor_mode & CFM_SHAFT)
970                         move_num += SGN(move_num);
971
972                 /* Get out from or Enter the dungeon */
973                 if (change_floor_mode & CFM_DOWN)
974                 {
975                         if (!dun_level)
976                                 move_num = d_info[dungeon_type].mindepth;
977                 }
978                 else if (change_floor_mode & CFM_UP)
979                 {
980                         if (dun_level + move_num < d_info[dungeon_type].mindepth)
981                                 move_num = -dun_level;
982                 }
983
984                 dun_level += move_num;
985         }
986
987         /* Leaving the dungeon to town */
988         if (!dun_level && dungeon_type)
989         {
990                 p_ptr->leaving_dungeon = TRUE;
991                 if (!vanilla_town && !lite_town)
992                 {
993                         p_ptr->wilderness_y = d_info[dungeon_type].dy;
994                         p_ptr->wilderness_x = d_info[dungeon_type].dx;
995                 }
996                 p_ptr->recall_dungeon = dungeon_type;
997                 dungeon_type = 0;
998
999                 /* Reach to the surface -- Clear all saved floors */
1000                 change_floor_mode &= ~CFM_SAVE_FLOORS;
1001         }
1002
1003         /* Kill some old saved floors */
1004         if (!(change_floor_mode & CFM_SAVE_FLOORS))
1005         {
1006                 /* Kill all saved floors */
1007                 for (i = 0; i < MAX_SAVED_FLOORS; i++)
1008                         kill_saved_floor(&saved_floors[i]);
1009
1010                 /* Reset visit_mark count */
1011                 latest_visit_mark = 1;
1012         }
1013         else if (change_floor_mode & CFM_NO_RETURN)
1014         {
1015                 /* Kill current floor */
1016                 kill_saved_floor(sf_ptr);
1017         }
1018
1019         /* No current floor -- Left/Enter dungeon etc... */
1020         if (!p_ptr->floor_id)
1021         {
1022                 /* No longer need to save current floor */
1023                 return;
1024         }
1025
1026
1027         /* Mark next floor_id on the previous floor */
1028         if (!new_floor_id)
1029         {
1030                 /* Get new id */
1031                 new_floor_id = get_new_floor_id();
1032
1033                 /* Connect from here */
1034                 if (c_ptr && !feat_uses_special(c_ptr->feat))
1035                 {
1036                         c_ptr->special = new_floor_id;
1037                 }
1038         }
1039
1040         /* Fix connection -- level teleportation or trap door */
1041         if (change_floor_mode & CFM_RAND_CONNECT)
1042         {
1043                 if (change_floor_mode & CFM_UP)
1044                         sf_ptr->upper_floor_id = new_floor_id;
1045                 else if (change_floor_mode & CFM_DOWN)
1046                         sf_ptr->lower_floor_id = new_floor_id;
1047         }
1048
1049         /* If you can return, you need to save previous floor */
1050         if ((change_floor_mode & CFM_SAVE_FLOORS) &&
1051             !(change_floor_mode & CFM_NO_RETURN))
1052         {
1053                 /* Get out of the my way! */
1054                 get_out_monster();
1055
1056                 /* Record the last visit turn of current floor */
1057                 sf_ptr->last_visit = turn;
1058
1059                 /* Forget the lite */
1060                 forget_lite();
1061
1062                 /* Forget the view */
1063                 forget_view();
1064
1065                 /* Forget the view */
1066                 clear_mon_lite();
1067
1068                 /* Save current floor */
1069                 if (!save_floor(sf_ptr, 0))
1070                 {
1071                         /* Save failed -- No return */
1072                         prepare_change_floor_mode(CFM_NO_RETURN);
1073
1074                         /* Kill current floor */
1075                         kill_saved_floor(get_sf_ptr(p_ptr->floor_id));
1076                 }
1077         }
1078 }
1079
1080
1081 /*!
1082  * @brief フロアの切り替え処理 / Enter new floor.
1083  * @return なし
1084  * @details
1085  * If the floor is an old saved floor, it will be\n
1086  * restored from the temporal file.  If the floor is new one, new cave\n
1087  * will be generated.\n
1088  */
1089 void change_floor(void)
1090 {
1091         saved_floor_type *sf_ptr;
1092         bool loaded = FALSE;
1093
1094         /* The dungeon is not ready */
1095         character_dungeon = FALSE;
1096
1097         /* No longer in the trap detecteded region */
1098         p_ptr->dtrap = FALSE;
1099
1100         /* Mega-Hack -- no panel yet */
1101         panel_row_min = 0;
1102         panel_row_max = 0;
1103         panel_col_min = 0;
1104         panel_col_max = 0;
1105
1106         /* Mega-Hack -- not ambushed on the wildness? */
1107         ambush_flag = FALSE;
1108
1109         /* No saved floors (On the surface etc.) */
1110         if (!(change_floor_mode & CFM_SAVE_FLOORS) &&
1111             !(change_floor_mode & CFM_FIRST_FLOOR))
1112         {
1113                 /* Create cave */
1114                 generate_cave();
1115
1116                 /* Paranoia -- No new saved floor */
1117                 new_floor_id = 0;
1118         }
1119
1120         /* In the dungeon */
1121         else
1122         {
1123                 /* No floor_id yet */
1124                 if (!new_floor_id)
1125                 {
1126                         /* Get new id */
1127                         new_floor_id = get_new_floor_id();
1128                 }
1129
1130                 /* Pointer for infomations of new floor */
1131                 sf_ptr = get_sf_ptr(new_floor_id);
1132
1133                 /* Try to restore old floor */
1134                 if (sf_ptr->last_visit)
1135                 {
1136                         /* Old saved floor is exist */
1137                         if (load_floor(sf_ptr, 0))
1138                         {
1139                                 loaded = TRUE;
1140
1141                                 /* Forbid return stairs */
1142                                 if (change_floor_mode & CFM_NO_RETURN)
1143                                 {
1144                                         cave_type *c_ptr = &cave[p_ptr->y][p_ptr->x];
1145
1146                                         if (!feat_uses_special(c_ptr->feat))
1147                                         {
1148                                                 if (change_floor_mode & (CFM_DOWN | CFM_UP))
1149                                                 {
1150                                                         /* Reset to floor */
1151                                                         c_ptr->feat = floor_type[randint0(100)];
1152                                                 }
1153
1154                                                 c_ptr->special = 0;
1155                                         }
1156                                 }
1157                         }
1158                 }
1159
1160                 /*
1161                  * Set lower/upper_floor_id of new floor when the new
1162                  * floor is right-above/right-under the current floor.
1163                  *
1164                  * Stair creation/Teleport level/Trap door will take
1165                  * you the same floor when you used it later again.
1166                  */
1167                 if (p_ptr->floor_id)
1168                 {
1169                         saved_floor_type *cur_sf_ptr = get_sf_ptr(p_ptr->floor_id);
1170
1171                         if (change_floor_mode & CFM_UP)
1172                         {
1173                                 /* New floor is right-above */
1174                                 if (cur_sf_ptr->upper_floor_id == new_floor_id)
1175                                         sf_ptr->lower_floor_id = p_ptr->floor_id;
1176                         }
1177                         else if (change_floor_mode & CFM_DOWN)
1178                         {
1179                                 /* New floor is right-under */
1180                                 if (cur_sf_ptr->lower_floor_id == new_floor_id)
1181                                         sf_ptr->upper_floor_id = p_ptr->floor_id;
1182                         }
1183                 }
1184
1185                 /* Break connection to killed floor */
1186                 else
1187                 {
1188                         if (change_floor_mode & CFM_UP)
1189                                 sf_ptr->lower_floor_id = 0;
1190                         else if (change_floor_mode & CFM_DOWN)
1191                                 sf_ptr->upper_floor_id = 0;
1192                 }
1193
1194                 /* Maintain monsters and artifacts */
1195                 if (loaded)
1196                 {
1197                         IDX i;
1198                         s32b tmp_last_visit = sf_ptr->last_visit;
1199                         s32b absence_ticks;
1200                         int alloc_chance = d_info[dungeon_type].max_m_alloc_chance;
1201                         int alloc_times;
1202
1203                         while (tmp_last_visit > turn) tmp_last_visit -= TURNS_PER_TICK * TOWN_DAWN;
1204                         absence_ticks = (turn - tmp_last_visit) / TURNS_PER_TICK;
1205
1206                         /* Maintain monsters */
1207                         for (i = 1; i < m_max; i++)
1208                         {
1209                                 monster_race *r_ptr;
1210                                 monster_type *m_ptr = &m_list[i];
1211
1212                                 /* Skip dead monsters */
1213                                 if (!m_ptr->r_idx) continue;
1214
1215                                 if (!is_pet(m_ptr))
1216                                 {
1217                                         /* Restore HP */
1218                                         m_ptr->hp = m_ptr->maxhp = m_ptr->max_maxhp;
1219
1220                                         /* Remove timed status (except MTIMED_CSLEEP) */
1221                                         (void)set_monster_fast(i, 0);
1222                                         (void)set_monster_slow(i, 0);
1223                                         (void)set_monster_stunned(i, 0);
1224                                         (void)set_monster_confused(i, 0);
1225                                         (void)set_monster_monfear(i, 0);
1226                                         (void)set_monster_invulner(i, 0, FALSE);
1227                                 }
1228
1229                                 /* Extract real monster race */
1230                                 r_ptr = real_r_ptr(m_ptr);
1231
1232                                 /* Ignore non-unique */
1233                                 if (!(r_ptr->flags1 & RF1_UNIQUE) &&
1234                                     !(r_ptr->flags7 & RF7_NAZGUL)) continue;
1235
1236                                 /* Appear at a different floor? */
1237                                 if (r_ptr->floor_id != new_floor_id)
1238                                 {
1239                                         /* Disapper from here */
1240                                         delete_monster_idx(i);
1241                                 }
1242                         }
1243
1244                         /* Maintain artifatcs */
1245                         for (i = 1; i < o_max; i++)
1246                         {
1247                                 object_type *o_ptr = &o_list[i];
1248
1249                                 /* Skip dead objects */
1250                                 if (!o_ptr->k_idx) continue;
1251
1252                                 /* Ignore non-artifact */
1253                                 if (!object_is_fixed_artifact(o_ptr)) continue;
1254
1255                                 /* Appear at a different floor? */
1256                                 if (a_info[o_ptr->name1].floor_id != new_floor_id)
1257                                 {
1258                                         /* Disappear from here */
1259                                         delete_object_idx(i);
1260                                 }
1261                                 else
1262                                 {
1263                                         /* Cancel preserve */
1264                                         a_info[o_ptr->name1].cur_num = 1;
1265                                 }
1266                         }
1267
1268                         (void)place_quest_monsters();
1269
1270                         /* Place some random monsters */
1271                         alloc_times = absence_ticks / alloc_chance;
1272
1273                         if (randint0(alloc_chance) < (absence_ticks % alloc_chance))
1274                                 alloc_times++;
1275
1276                         for (i = 0; i < alloc_times; i++)
1277                         {
1278                                 /* Make a (group of) new monster */
1279                                 (void)alloc_monster(0, 0);
1280                         }
1281
1282                 }
1283
1284                 /* New floor_id or failed to restore */
1285                 else /* if (!loaded) */
1286                 {
1287                         if (sf_ptr->last_visit)
1288                         {
1289                                 /* Temporal file is broken? */
1290                                 msg_print(_("階段は行き止まりだった。", "The staircases come to a dead end..."));
1291
1292                                 /* Create simple dead end */
1293                                 build_dead_end();
1294
1295                                 /* Break connection */
1296                                 if (change_floor_mode & CFM_UP)
1297                                 {
1298                                         sf_ptr->upper_floor_id = 0;
1299                                 }
1300                                 else if (change_floor_mode & CFM_DOWN)
1301                                 {
1302                                         sf_ptr->lower_floor_id = 0;
1303                                 }
1304                         }
1305                         else
1306                         {
1307                                 /* Newly create cave */
1308                                 generate_cave();
1309                         }
1310
1311                         /* Record last visit turn */
1312                         sf_ptr->last_visit = turn;
1313
1314                         /* Set correct dun_level value */
1315                         sf_ptr->dun_level = dun_level;
1316
1317                         /* Create connected stairs */
1318                         if (!(change_floor_mode & CFM_NO_RETURN))
1319                         {
1320                                 /* Extract stair position */
1321                                 cave_type *c_ptr = &cave[p_ptr->y][p_ptr->x];
1322
1323                                 /*** Create connected stairs ***/
1324
1325                                 /* No stairs down from Quest */
1326                                 if ((change_floor_mode & CFM_UP) && !quest_number(dun_level))
1327                                 {
1328                                         c_ptr->feat = (change_floor_mode & CFM_SHAFT) ? feat_state(feat_down_stair, FF_SHAFT) : feat_down_stair;
1329                                 }
1330
1331                                 /* No stairs up when ironman_downward */
1332                                 else if ((change_floor_mode & CFM_DOWN) && !ironman_downward)
1333                                 {
1334                                         c_ptr->feat = (change_floor_mode & CFM_SHAFT) ? feat_state(feat_up_stair, FF_SHAFT) : feat_up_stair;
1335                                 }
1336
1337                                 /* Paranoia -- Clear mimic */
1338                                 c_ptr->mimic = 0;
1339
1340                                 /* Connect to previous floor */
1341                                 c_ptr->special = p_ptr->floor_id;
1342                         }
1343                 }
1344
1345                 /* Arrive at random grid */
1346                 if (change_floor_mode & (CFM_RAND_PLACE))
1347                 {
1348                         (void)new_player_spot();
1349                 }
1350
1351                 /* You see stairs blocked */
1352                 else if ((change_floor_mode & CFM_NO_RETURN) &&
1353                          (change_floor_mode & (CFM_DOWN | CFM_UP)))
1354                 {
1355                         if (!p_ptr->blind)
1356                         {
1357                                 msg_print(_("突然階段が塞がれてしまった。", "Suddenly the stairs is blocked!"));
1358                         }
1359                         else
1360                         {
1361                                 msg_print(_("ゴトゴトと何か音がした。", "You hear some noises."));
1362                         }
1363                 }
1364
1365                 /*
1366                  * Update visit mark
1367                  *
1368                  * The "turn" is not always different number because
1369                  * the level teleport doesn't take any turn.  Use
1370                  * visit mark instead of last visit turn to find the
1371                  * oldest saved floor.
1372                  */
1373                 sf_ptr->visit_mark = latest_visit_mark++;
1374         }
1375
1376         /* Place preserved pet monsters */
1377         place_pet();
1378
1379         /* Reset travel target place */
1380         forget_travel_flow();
1381
1382         /* Hack -- maintain unique and artifacts */
1383         update_unique_artifact(new_floor_id);
1384
1385         /* Now the player is in new floor */
1386         p_ptr->floor_id = new_floor_id;
1387
1388         /* The dungeon is ready */
1389         character_dungeon = TRUE;
1390
1391         /* Hack -- Munchkin characters always get whole map */
1392         if (p_ptr->pseikaku == SEIKAKU_MUNCHKIN)
1393                 wiz_lite((bool)(p_ptr->pclass == CLASS_NINJA));
1394
1395         /* Remember when this level was "created" */
1396         old_turn = turn;
1397
1398         /* No dungeon feeling yet */
1399         p_ptr->feeling_turn = old_turn;
1400         p_ptr->feeling = 0;
1401
1402         /* Clear all flags */
1403         change_floor_mode = 0L;
1404
1405         select_floor_music();
1406 }
1407
1408 /*!
1409  * @brief プレイヤーの手による能動的な階段生成処理 /
1410  * Create stairs at or move previously created stairs into the player location.
1411  * @return なし
1412  */
1413 void stair_creation(void)
1414 {
1415         saved_floor_type *sf_ptr;
1416         saved_floor_type *dest_sf_ptr;
1417
1418         bool up = TRUE;
1419         bool down = TRUE;
1420         s16b dest_floor_id = 0;
1421
1422
1423         /* Forbid up staircases on Ironman mode */
1424         if (ironman_downward) up = FALSE;
1425
1426         /* Forbid down staircases on quest level */
1427         if (quest_number(dun_level) || (dun_level >= d_info[dungeon_type].maxdepth)) down = FALSE;
1428
1429         /* No effect out of standard dungeon floor */
1430         if (!dun_level || (!up && !down) ||
1431             (p_ptr->inside_quest && is_fixed_quest_idx(p_ptr->inside_quest)) ||
1432             p_ptr->inside_arena || p_ptr->inside_battle)
1433         {
1434                 /* arena or quest */
1435                 msg_print(_("効果がありません!", "There is no effect!"));
1436                 return;
1437         }
1438
1439         /* Artifacts resists */
1440         if (!cave_valid_bold(p_ptr->y, p_ptr->x))
1441         {
1442                 msg_print(_("床上のアイテムが呪文を跳ね返した。", "The object resists the spell."));
1443                 return;
1444         }
1445
1446         /* Destroy all objects in the grid */
1447         delete_object(p_ptr->y, p_ptr->x);
1448
1449         /* Extract current floor data */
1450         sf_ptr = get_sf_ptr(p_ptr->floor_id);
1451
1452         /* Paranoia */
1453         if (!sf_ptr)
1454         {
1455                 /* No floor id? -- Create now! */
1456                 p_ptr->floor_id = get_new_floor_id();
1457                 sf_ptr = get_sf_ptr(p_ptr->floor_id);
1458         } 
1459
1460         /* Choose randomly */
1461         if (up && down)
1462         {
1463                 if (randint0(100) < 50) up = FALSE;
1464                 else down = FALSE;
1465         }
1466
1467         /* Destination is already fixed */
1468         if (up)
1469         {
1470                 if (sf_ptr->upper_floor_id) dest_floor_id = sf_ptr->upper_floor_id;
1471         }
1472         else
1473         {
1474                 if (sf_ptr->lower_floor_id) dest_floor_id = sf_ptr->lower_floor_id;
1475         }
1476
1477
1478         /* Search old stairs leading to the destination */
1479         if (dest_floor_id)
1480         {
1481                 POSITION x, y;
1482
1483                 for (y = 0; y < cur_hgt; y++)
1484                 {
1485                         for (x = 0; x < cur_wid; x++)
1486                         {
1487                                 cave_type *c_ptr = &cave[y][x];
1488
1489                                 if (!c_ptr->special) continue;
1490                                 if (feat_uses_special(c_ptr->feat)) continue;
1491                                 if (c_ptr->special != dest_floor_id) continue;
1492
1493                                 /* Remove old stairs */
1494                                 c_ptr->special = 0;
1495                                 cave_set_feat(y, x, floor_type[randint0(100)]);
1496                         }
1497                 }
1498         }
1499
1500         /* No old destination -- Get new one now */
1501         else
1502         {
1503                 dest_floor_id = get_new_floor_id();
1504
1505                 /* Fix it */
1506                 if (up)
1507                         sf_ptr->upper_floor_id = dest_floor_id;
1508                 else
1509                         sf_ptr->lower_floor_id = dest_floor_id;
1510         }
1511
1512         /* Extract destination floor data */
1513         dest_sf_ptr = get_sf_ptr(dest_floor_id);
1514
1515
1516         /* Create a staircase */
1517         if (up)
1518         {
1519                 cave_set_feat(p_ptr->y, p_ptr->x,
1520                         (dest_sf_ptr->last_visit && (dest_sf_ptr->dun_level <= dun_level - 2)) ?
1521                         feat_state(feat_up_stair, FF_SHAFT) : feat_up_stair);
1522         }
1523         else
1524         {
1525                 cave_set_feat(p_ptr->y, p_ptr->x,
1526                         (dest_sf_ptr->last_visit && (dest_sf_ptr->dun_level >= dun_level + 2)) ?
1527                         feat_state(feat_down_stair, FF_SHAFT) : feat_down_stair);
1528         }
1529
1530
1531         /* Connect this stairs to the destination */
1532         cave[p_ptr->y][p_ptr->x].special = dest_floor_id;
1533 }