OSDN Git Service

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