OSDN Git Service

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