OSDN Git Service

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