OSDN Git Service

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