OSDN Git Service

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