OSDN Git Service

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