OSDN Git Service

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