OSDN Git Service

[Refactor] #37353 PET_* と PF_* を cmd-petに移動。
[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 #include "monster-status.h"
22 #include "object-hook.h"
23 #include "cmd-pet.h"
24
25 static FLOOR_IDX new_floor_id;  /*!<次のフロアのID / floor_id of the destination */
26 static u32b change_floor_mode;  /*!<フロア移行処理に関するフラグ / Mode flags for changing floor */
27 static u32b latest_visit_mark;  /*!<フロアを渡った回数?(確認中) / Max number of visit_mark */
28
29
30 /*!
31  * @brief 保存フロア配列を初期化する / Initialize saved_floors array. 
32  * @param force テンポラリファイルが残っていた場合も警告なしで強制的に削除する。
33  * @details Make sure that old temporal files are not remaining as gurbages.
34  * @return なし
35  */
36 void init_saved_floors(bool force)
37 {
38         char floor_savefile[1024];
39         int i;
40         int fd = -1;
41         BIT_FLAGS mode = 0644;
42
43 #ifdef SET_UID
44 # ifdef SECURE
45         /* Get "games" permissions */
46         beGames();
47 # endif
48 #endif
49
50         for (i = 0; i < MAX_SAVED_FLOORS; i++)
51         {
52                 saved_floor_type *sf_ptr = &saved_floors[i];
53
54                 /* File name */
55                 sprintf(floor_savefile, "%s.F%02d", savefile, i);
56
57                 /* Grab permissions */
58                 safe_setuid_grab();
59
60                 /* Try to create the file */
61                 fd = fd_make(floor_savefile, mode);
62
63                 /* Drop permissions */
64                 safe_setuid_drop();
65
66                 /* Failed! */
67                 if (fd < 0)
68                 {
69                         if (!force)
70                         {
71                                 msg_print(_("エラー:古いテンポラリ・ファイルが残っています。", "Error: There are old temporal files."));
72                                 msg_print(_("変愚蛮怒を二重に起動していないか確認してください。", "Make sure you are not running two game processes simultaneously."));
73                                 msg_print(_("過去に変愚蛮怒がクラッシュした場合は一時ファイルを", "If the temporal files are garbages of old crashed process, "));
74                                 msg_print(_("強制的に削除して実行を続けられます。", "you can delete it safely."));
75                                 if (!get_check(_("強制的に削除してもよろしいですか?", "Do you delete old temporal files? ")))
76                                         quit(_("実行中止", "Aborted."));
77                                 force = TRUE;
78                         }
79                 }
80                 else
81                 {
82                         /* Close the "fd" */
83                         (void)fd_close(fd);
84                 }
85
86                 /* Grab permissions */
87                 safe_setuid_grab();
88
89                 /* Simply kill the temporal file */ 
90                 (void)fd_kill(floor_savefile);
91
92                 /* Drop permissions */
93                 safe_setuid_drop();
94
95                 sf_ptr->floor_id = 0;
96         }
97
98         /* No floor_id used yet (No.0 is reserved to indicate non existance) */
99         max_floor_id = 1;
100
101         /* vist_mark is from 1 */
102         latest_visit_mark = 1;
103
104         /* A sign to mark temporal files */
105         saved_floor_file_sign = (u32b)time(NULL);
106
107         /* No next floor yet */
108         new_floor_id = 0;
109
110         /* No change floor mode yet */
111         change_floor_mode = 0;
112
113 #ifdef SET_UID
114 # ifdef SECURE
115         /* Drop "games" permissions */
116         bePlayer();
117 # endif
118 #endif
119 }
120
121 /*!
122  * @brief 保存フロア用テンポラリファイルを削除する / Kill temporal files
123  * @details Should be called just before the game quit.
124  * @return なし
125  */
126 void clear_saved_floor_files(void)
127 {
128         char floor_savefile[1024];
129         int i;
130
131 #ifdef SET_UID
132 # ifdef SECURE
133         /* Get "games" permissions */
134         beGames();
135 # endif
136 #endif
137
138         for (i = 0; i < MAX_SAVED_FLOORS; i++)
139         {
140                 saved_floor_type *sf_ptr = &saved_floors[i];
141
142                 /* No temporal file */
143                 if (!sf_ptr->floor_id) continue;
144                 if (sf_ptr->floor_id == p_ptr->floor_id) continue;
145
146                 /* File name */
147                 sprintf(floor_savefile, "%s.F%02d", savefile, i);
148
149                 /* Grab permissions */
150                 safe_setuid_grab();
151
152                 /* Simply kill the temporal file */ 
153                 (void)fd_kill(floor_savefile);
154
155                 /* Drop permissions */
156                 safe_setuid_drop();
157         }
158
159 #ifdef SET_UID
160 # ifdef SECURE
161         /* Drop "games" permissions */
162         bePlayer();
163 # endif
164 #endif
165 }
166
167 /*!
168  * @brief 保存フロアIDから参照ポインタを得る / Get a pointer for an item of the saved_floors array.
169  * @param floor_id 保存フロアID
170  * @return IDに対応する保存フロアのポインタ、ない場合はNULLを返す。
171  */
172 saved_floor_type *get_sf_ptr(FLOOR_IDX floor_id)
173 {
174         int i;
175
176         /* floor_id No.0 indicates no floor */
177         if (!floor_id) return NULL;
178
179         for (i = 0; i < MAX_SAVED_FLOORS; i++)
180         {
181                 saved_floor_type *sf_ptr = &saved_floors[i];
182
183                 if (sf_ptr->floor_id == floor_id) return sf_ptr;
184         }
185
186         /* None found */
187         return NULL;
188 }
189
190
191 /*!
192  * @brief 参照ポインタ先の保存フロアを抹消する / kill a saved floor and get an empty space
193  * @param sf_ptr 保存フロアの参照ポインタ
194  * @return なし
195  */
196 static void kill_saved_floor(saved_floor_type *sf_ptr)
197 {
198         char floor_savefile[1024];
199         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 (!monster_is_valid(m_ptr)) 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 (!monster_is_valid(m_ptr)) 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 (!monster_is_valid(m_ptr)) 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                         m_ptr->hold_o_idx = 0;
568                         m_ptr->target_y = 0;
569
570                         if ((r_ptr->flags1 & RF1_FORCE_SLEEP) && !ironman_nightmare)
571                         {
572                                 /* Monster is still being nice */
573                                 m_ptr->mflag |= (MFLAG_NICE);
574
575                                 /* Must repair monsters */
576                                 repair_monsters = TRUE;
577                         }
578                         update_monster(m_idx, TRUE);
579                         lite_spot(cy, cx);
580
581                         /* Pre-calculated in precalc_cur_num_of_pet() */
582                         /* r_ptr->cur_num++; */
583
584                         /* Hack -- Count the number of "reproducers" */
585                         if (r_ptr->flags2 & RF2_MULTIPLY) current_floor_ptr->num_repro++;
586
587                         /* Hack -- Notice new multi-hued monsters */
588                         {
589                                 monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
590                                 if (ap_r_ptr->flags1 & (RF1_ATTR_MULTI | RF1_SHAPECHANGER))
591                                         shimmer_monsters = TRUE;
592                         }
593                 }
594                 else
595                 {
596                         monster_type *m_ptr = &party_mon[i];
597                         monster_race *r_ptr = real_r_ptr(m_ptr);
598                         GAME_TEXT m_name[MAX_NLEN];
599
600                         monster_desc(m_name, m_ptr, 0);
601                         msg_format(_("%sとはぐれてしまった。", "You have lost sight of %s."), m_name);
602                         if (record_named_pet && m_ptr->nickname)
603                         {
604                                 monster_desc(m_name, m_ptr, MD_INDEF_VISIBLE);
605                                 do_cmd_write_nikki(NIKKI_NAMED_PET, RECORD_NAMED_PET_LOST_SIGHT, m_name);
606                         }
607
608                         /* Pre-calculated in precalc_cur_num_of_pet(), but need to decrease */
609                         if (r_ptr->cur_num) r_ptr->cur_num--;
610                 }
611         }
612
613         /* For accuracy of precalc_cur_num_of_pet() */
614         (void)C_WIPE(party_mon, MAX_PARTY_MON, monster_type);
615 }
616
617
618 /*!
619  * @brief ユニークモンスターやアーティファクトの所在フロアを更新する / Hack -- Update location of unique monsters and artifacts
620  * @param cur_floor_id 現在のフロアID
621  * @return なし
622  * @details 
623  * The r_ptr->floor_id and a_ptr->floor_id are not updated correctly\n
624  * while new floor creation since dungeons may be re-created by\n
625  * auto-scum option.\n
626  */
627 static void update_unique_artifact(s16b cur_floor_id)
628 {
629         int i;
630
631         /* Maintain unique monsters */
632         for (i = 1; i < m_max; i++)
633         {
634                 monster_race *r_ptr;
635                 monster_type *m_ptr = &current_floor_ptr->m_list[i];
636
637                 if (!monster_is_valid(m_ptr)) continue;
638
639                 /* Extract real monster race */
640                 r_ptr = real_r_ptr(m_ptr);
641
642                 /* Memorize location of the unique monster */
643                 if ((r_ptr->flags1 & RF1_UNIQUE) ||
644                     (r_ptr->flags7 & RF7_NAZGUL))
645                 {
646                         r_ptr->floor_id = cur_floor_id;
647                 }
648         }
649
650         /* Maintain artifatcs */
651         for (i = 1; i < o_max; i++)
652         {
653                 object_type *o_ptr = &current_floor_ptr->o_list[i];
654
655                 /* Skip dead objects */
656                 if (!o_ptr->k_idx) continue;
657
658                 /* Memorize location of the artifact */
659                 if (object_is_fixed_artifact(o_ptr))
660                 {
661                         a_info[o_ptr->name1].floor_id = cur_floor_id;
662                 }
663         }
664 }
665
666
667 /*!
668  * @brief フロア移動時、プレイヤーの移動先モンスターが既にいた場合ランダムな近隣に移動させる / When a monster is at a place where player will return,
669  * @return なし
670  */
671 static void get_out_monster(void)
672 {
673         int tries = 0;
674         POSITION dis = 1;
675         POSITION oy = p_ptr->y;
676         POSITION ox = p_ptr->x;
677         MONSTER_IDX m_idx = current_floor_ptr->grid_array[oy][ox].m_idx;
678
679         /* Nothing to do if no monster */
680         if (!m_idx) return;
681
682         /* Look until done */
683         while (TRUE)
684         {
685                 monster_type *m_ptr;
686
687                 /* Pick a (possibly illegal) location */
688                 POSITION ny = rand_spread(oy, dis);
689                 POSITION nx = rand_spread(ox, dis);
690
691                 tries++;
692
693                 /* Stop after 1000 tries */
694                 if (tries > 10000) return;
695
696                 /*
697                  * Increase distance after doing enough tries
698                  * compared to area of possible space
699                  */
700                 if (tries > 20 * dis * dis) dis++;
701
702                 /* Ignore illegal locations */
703                 if (!in_bounds(ny, nx)) continue;
704
705                 /* Require "empty" floor space */
706                 if (!cave_empty_bold(ny, nx)) continue;
707
708                 /* Hack -- no teleport onto glyph of warding */
709                 if (is_glyph_grid(&current_floor_ptr->grid_array[ny][nx])) continue;
710                 if (is_explosive_rune_grid(&current_floor_ptr->grid_array[ny][nx])) continue;
711
712                 /* ...nor onto the Pattern */
713                 if (pattern_tile(ny, nx)) continue;
714
715                 /*** It's a good place ***/
716
717                 m_ptr = &current_floor_ptr->m_list[m_idx];
718
719                 /* Update the old location */
720                 current_floor_ptr->grid_array[oy][ox].m_idx = 0;
721
722                 /* Update the new location */
723                 current_floor_ptr->grid_array[ny][nx].m_idx = m_idx;
724
725                 /* Move the monster */
726                 m_ptr->fy = ny;
727                 m_ptr->fx = nx; 
728
729                 /* No need to do update_monster() */
730
731                 /* Success */
732                 return;
733         }
734 }
735
736 /*!
737  * マス構造体のspecial要素を利用する地形かどうかを判定するマクロ / Is this feature has special meaning (except floor_id) with g_ptr->special?
738  */
739 #define feat_uses_special(F) (have_flag(f_info[(F)].flags, FF_SPECIAL))
740
741
742 /*!
743  * @brief 新フロアに移動元フロアに繋がる階段を配置する / Virtually teleport onto the stairs that is connecting between two floors.
744  * @param sf_ptr 移動元の保存フロア構造体参照ポインタ
745  * @return なし
746  */
747 static void locate_connected_stairs(saved_floor_type *sf_ptr)
748 {
749         POSITION x, y, sx = 0, sy = 0;
750         POSITION x_table[20];
751         POSITION y_table[20];
752         int num = 0;
753         int i;
754
755         /* Search usable stairs */
756         for (y = 0; y < current_floor_ptr->height; y++)
757         {
758                 for (x = 0; x < current_floor_ptr->width; x++)
759                 {
760                         grid_type *g_ptr = &current_floor_ptr->grid_array[y][x];
761                         feature_type *f_ptr = &f_info[g_ptr->feat];
762                         bool ok = FALSE;
763
764                         if (change_floor_mode & CFM_UP)
765                         {
766                                 if (have_flag(f_ptr->flags, FF_LESS) && have_flag(f_ptr->flags, FF_STAIRS) &&
767                                     !have_flag(f_ptr->flags, FF_SPECIAL))
768                                 {
769                                         ok = TRUE;
770
771                                         /* Found fixed stairs? */
772                                         if (g_ptr->special &&
773                                             g_ptr->special == sf_ptr->upper_floor_id)
774                                         {
775                                                 sx = x;
776                                                 sy = y;
777                                         }
778                                 }
779                         }
780
781                         else if (change_floor_mode & CFM_DOWN)
782                         {
783                                 if (have_flag(f_ptr->flags, FF_MORE) && have_flag(f_ptr->flags, FF_STAIRS) &&
784                                     !have_flag(f_ptr->flags, FF_SPECIAL))
785                                 {
786                                         ok = TRUE;
787
788                                         /* Found fixed stairs */
789                                         if (g_ptr->special &&
790                                             g_ptr->special == sf_ptr->lower_floor_id)
791                                         {
792                                                 sx = x;
793                                                 sy = y;
794                                         }
795                                 }
796                         }
797
798                         else
799                         {
800                                 if (have_flag(f_ptr->flags, FF_BLDG))
801                                 {
802                                         ok = TRUE;
803                                 }
804                         }
805
806                         if (ok && (num < 20))
807                         {
808                                 x_table[num] = x;
809                                 y_table[num] = y;
810                                 num++;
811                         }
812                 }
813         }
814
815         if (sx)
816         {
817                 /* Already fixed */
818                 p_ptr->y = sy;
819                 p_ptr->x = sx;
820         }
821         else if (!num)
822         {
823                 /* No stairs found! -- No return */
824                 prepare_change_floor_mode(CFM_RAND_PLACE | CFM_NO_RETURN);
825
826                 /* Mega Hack -- It's not the stairs you enter.  Disable it.  */
827                 if (!feat_uses_special(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;
828         }
829         else
830         {
831                 /* Choose random one */
832                 i = randint0(num);
833
834                 /* Point stair location */
835                 p_ptr->y = y_table[i];
836                 p_ptr->x = x_table[i];
837         }
838 }
839
840 /*!
841  * @brief 現在のフロアを離れるに伴って行なわれる保存処理
842  * / Maintain quest monsters, mark next floor_id at stairs, save current floor, and prepare to enter next floor.
843  * @return なし
844  */
845 void leave_floor(void)
846 {
847         grid_type *g_ptr = NULL;
848         feature_type *f_ptr;
849         saved_floor_type *sf_ptr;
850         MONRACE_IDX quest_r_idx = 0;
851         DUNGEON_IDX i;
852
853         /* Preserve pets and prepare to take these to next floor */
854         preserve_pet();
855
856         /* Remove all mirrors without explosion */
857         remove_all_mirrors(FALSE);
858
859         if (p_ptr->special_defense & NINJA_S_STEALTH) set_superstealth(FALSE);
860
861         /* New floor is not yet prepared */
862         new_floor_id = 0;
863
864         /* Temporary get a floor_id (for Arena) */
865         if (!p_ptr->floor_id &&
866             (change_floor_mode & CFM_SAVE_FLOORS) &&
867             !(change_floor_mode & CFM_NO_RETURN))
868         {
869             /* Get temporal floor_id */
870             p_ptr->floor_id = get_new_floor_id();
871         }
872
873         /* Search the quest monster index */
874         for (i = 0; i < max_q_idx; i++)
875         {
876                 if ((quest[i].status == QUEST_STATUS_TAKEN) &&
877                     ((quest[i].type == QUEST_TYPE_KILL_LEVEL) ||
878                     (quest[i].type == QUEST_TYPE_RANDOM)) &&
879                     (quest[i].level == current_floor_ptr->dun_level) &&
880                     (p_ptr->dungeon_idx == quest[i].dungeon) &&
881                     !(quest[i].flags & QUEST_FLAG_PRESET))
882                 {
883                         quest_r_idx = quest[i].r_idx;
884                 }
885         }
886
887         /* Maintain quest monsters */
888         for (i = 1; i < m_max; i++)
889         {
890                 monster_race *r_ptr;
891                 monster_type *m_ptr = &current_floor_ptr->m_list[i];
892
893                 if (!monster_is_valid(m_ptr)) continue;
894
895                 /* Only maintain quest monsters */
896                 if (quest_r_idx != m_ptr->r_idx) continue;
897
898                 /* Extract real monster race */
899                 r_ptr = real_r_ptr(m_ptr);
900
901                 /* Ignore unique monsters */
902                 if ((r_ptr->flags1 & RF1_UNIQUE) ||
903                     (r_ptr->flags7 & RF7_NAZGUL)) continue;
904
905                 /* Delete non-unique quest monsters */
906                 delete_monster_idx(i);
907         }
908
909         /* Check if there is a same item */
910         for (i = 0; i < INVEN_PACK; i++)
911         {
912                 object_type *o_ptr = &inventory[i];
913
914                 /* Skip dead objects */
915                 if (!o_ptr->k_idx) continue;
916
917                 /* Delete old memorized location of the artifact */
918                 if (object_is_fixed_artifact(o_ptr))
919                 {
920                         a_info[o_ptr->name1].floor_id = 0;
921                 }
922         }
923
924         /* Extract current floor info or NULL */
925         sf_ptr = get_sf_ptr(p_ptr->floor_id);
926
927         /* Choose random stairs */
928         if ((change_floor_mode & CFM_RAND_CONNECT) && p_ptr->floor_id)
929         {
930                 locate_connected_stairs(sf_ptr);
931         }
932
933         /* Extract new dungeon level */
934         if (change_floor_mode & CFM_SAVE_FLOORS)
935         {
936                 /* Extract stair position */
937                 g_ptr = &current_floor_ptr->grid_array[p_ptr->y][p_ptr->x];
938                 f_ptr = &f_info[g_ptr->feat];
939
940                 /* Get back to old saved floor? */
941                 if (g_ptr->special && !have_flag(f_ptr->flags, FF_SPECIAL) && get_sf_ptr(g_ptr->special))
942                 {
943                         /* Saved floor is exist.  Use it. */
944                         new_floor_id = g_ptr->special;
945                 }
946
947                 /* Mark shaft up/down */
948                 if (have_flag(f_ptr->flags, FF_STAIRS) && have_flag(f_ptr->flags, FF_SHAFT))
949                 {
950                         prepare_change_floor_mode(CFM_SHAFT);
951                 }
952         }
953
954         /* Climb up/down some sort of stairs */
955         if (change_floor_mode & (CFM_DOWN | CFM_UP))
956         {
957                 int move_num = 0;
958
959                 /* Extract level movement number */
960                 if (change_floor_mode & CFM_DOWN) move_num = 1;
961                 else if (change_floor_mode & CFM_UP) move_num = -1;
962
963                 /* Shafts are deeper than normal stairs */
964                 if (change_floor_mode & CFM_SHAFT)
965                         move_num += SGN(move_num);
966
967                 /* Get out from or Enter the dungeon */
968                 if (change_floor_mode & CFM_DOWN)
969                 {
970                         if (!current_floor_ptr->dun_level)
971                                 move_num = d_info[p_ptr->dungeon_idx].mindepth;
972                 }
973                 else if (change_floor_mode & CFM_UP)
974                 {
975                         if (current_floor_ptr->dun_level + move_num < d_info[p_ptr->dungeon_idx].mindepth)
976                                 move_num = -current_floor_ptr->dun_level;
977                 }
978
979                 current_floor_ptr->dun_level += move_num;
980         }
981
982         /* Leaving the dungeon to town */
983         if (!current_floor_ptr->dun_level && p_ptr->dungeon_idx)
984         {
985                 p_ptr->leaving_dungeon = TRUE;
986                 if (!vanilla_town && !lite_town)
987                 {
988                         p_ptr->wilderness_y = d_info[p_ptr->dungeon_idx].dy;
989                         p_ptr->wilderness_x = d_info[p_ptr->dungeon_idx].dx;
990                 }
991                 p_ptr->recall_dungeon = p_ptr->dungeon_idx;
992                 p_ptr->dungeon_idx = 0;
993
994                 /* Reach to the surface -- Clear all saved floors */
995                 change_floor_mode &= ~CFM_SAVE_FLOORS;
996         }
997
998         /* Kill some old saved floors */
999         if (!(change_floor_mode & CFM_SAVE_FLOORS))
1000         {
1001                 /* Kill all saved floors */
1002                 for (i = 0; i < MAX_SAVED_FLOORS; i++)
1003                         kill_saved_floor(&saved_floors[i]);
1004
1005                 /* Reset visit_mark count */
1006                 latest_visit_mark = 1;
1007         }
1008         else if (change_floor_mode & CFM_NO_RETURN)
1009         {
1010                 /* Kill current floor */
1011                 kill_saved_floor(sf_ptr);
1012         }
1013
1014         /* No current floor -- Left/Enter dungeon etc... */
1015         if (!p_ptr->floor_id)
1016         {
1017                 /* No longer need to save current floor */
1018                 return;
1019         }
1020
1021
1022         /* Mark next floor_id on the previous floor */
1023         if (!new_floor_id)
1024         {
1025                 /* Get new id */
1026                 new_floor_id = get_new_floor_id();
1027
1028                 /* Connect from here */
1029                 if (g_ptr && !feat_uses_special(g_ptr->feat))
1030                 {
1031                         g_ptr->special = new_floor_id;
1032                 }
1033         }
1034
1035         /* Fix connection -- level teleportation or trap door */
1036         if (change_floor_mode & CFM_RAND_CONNECT)
1037         {
1038                 if (change_floor_mode & CFM_UP)
1039                         sf_ptr->upper_floor_id = new_floor_id;
1040                 else if (change_floor_mode & CFM_DOWN)
1041                         sf_ptr->lower_floor_id = new_floor_id;
1042         }
1043
1044         /* If you can return, you need to save previous floor */
1045         if ((change_floor_mode & CFM_SAVE_FLOORS) &&
1046             !(change_floor_mode & CFM_NO_RETURN))
1047         {
1048                 /* Get out of the my way! */
1049                 get_out_monster();
1050
1051                 /* Record the last visit current_world_ptr->game_turn of current floor */
1052                 sf_ptr->last_visit = current_world_ptr->game_turn;
1053
1054                 forget_lite();
1055                 forget_view();
1056                 clear_mon_lite();
1057
1058                 /* Save current floor */
1059                 if (!save_floor(sf_ptr, 0))
1060                 {
1061                         /* Save failed -- No return */
1062                         prepare_change_floor_mode(CFM_NO_RETURN);
1063
1064                         /* Kill current floor */
1065                         kill_saved_floor(get_sf_ptr(p_ptr->floor_id));
1066                 }
1067         }
1068 }
1069
1070
1071 /*!
1072  * @brief フロアの切り替え処理 / Enter new floor.
1073  * @return なし
1074  * @details
1075  * If the floor is an old saved floor, it will be\n
1076  * restored from the temporal file.  If the floor is new one, new current_floor_ptr->grid_array\n
1077  * will be generated.\n
1078  */
1079 void change_floor(void)
1080 {
1081         saved_floor_type *sf_ptr;
1082         bool loaded = FALSE;
1083
1084         /* The dungeon is not ready */
1085         character_dungeon = FALSE;
1086
1087         /* No longer in the trap detecteded region */
1088         p_ptr->dtrap = FALSE;
1089
1090         /* Mega-Hack -- no panel yet */
1091         panel_row_min = 0;
1092         panel_row_max = 0;
1093         panel_col_min = 0;
1094         panel_col_max = 0;
1095
1096         /* Mega-Hack -- not ambushed on the wildness? */
1097         p_ptr->ambush_flag = FALSE;
1098
1099         /* No saved floors (On the surface etc.) */
1100         if (!(change_floor_mode & CFM_SAVE_FLOORS) &&
1101             !(change_floor_mode & CFM_FIRST_FLOOR))
1102         {
1103                 /* Create current_floor_ptr->grid_array */
1104                 generate_random_floor();
1105
1106                 /* Paranoia -- No new saved floor */
1107                 new_floor_id = 0;
1108         }
1109
1110         /* In the dungeon */
1111         else
1112         {
1113                 /* No floor_id yet */
1114                 if (!new_floor_id)
1115                 {
1116                         /* Get new id */
1117                         new_floor_id = get_new_floor_id();
1118                 }
1119
1120                 /* Pointer for infomations of new floor */
1121                 sf_ptr = get_sf_ptr(new_floor_id);
1122
1123                 /* Try to restore old floor */
1124                 if (sf_ptr->last_visit)
1125                 {
1126                         /* Old saved floor is exist */
1127                         if (load_floor(sf_ptr, 0))
1128                         {
1129                                 loaded = TRUE;
1130
1131                                 /* Forbid return stairs */
1132                                 if (change_floor_mode & CFM_NO_RETURN)
1133                                 {
1134                                         grid_type *g_ptr = &current_floor_ptr->grid_array[p_ptr->y][p_ptr->x];
1135
1136                                         if (!feat_uses_special(g_ptr->feat))
1137                                         {
1138                                                 if (change_floor_mode & (CFM_DOWN | CFM_UP))
1139                                                 {
1140                                                         /* Reset to floor */
1141                                                         g_ptr->feat = feat_ground_type[randint0(100)];
1142                                                 }
1143
1144                                                 g_ptr->special = 0;
1145                                         }
1146                                 }
1147                         }
1148                 }
1149
1150                 /*
1151                  * Set lower/upper_floor_id of new floor when the new
1152                  * floor is right-above/right-under the current floor.
1153                  *
1154                  * Stair creation/Teleport level/Trap door will take
1155                  * you the same floor when you used it later again.
1156                  */
1157                 if (p_ptr->floor_id)
1158                 {
1159                         saved_floor_type *cur_sf_ptr = get_sf_ptr(p_ptr->floor_id);
1160
1161                         if (change_floor_mode & CFM_UP)
1162                         {
1163                                 /* New floor is right-above */
1164                                 if (cur_sf_ptr->upper_floor_id == new_floor_id)
1165                                         sf_ptr->lower_floor_id = p_ptr->floor_id;
1166                         }
1167                         else if (change_floor_mode & CFM_DOWN)
1168                         {
1169                                 /* New floor is right-under */
1170                                 if (cur_sf_ptr->lower_floor_id == new_floor_id)
1171                                         sf_ptr->upper_floor_id = p_ptr->floor_id;
1172                         }
1173                 }
1174
1175                 /* Break connection to killed floor */
1176                 else
1177                 {
1178                         if (change_floor_mode & CFM_UP)
1179                                 sf_ptr->lower_floor_id = 0;
1180                         else if (change_floor_mode & CFM_DOWN)
1181                                 sf_ptr->upper_floor_id = 0;
1182                 }
1183
1184                 /* Maintain monsters and artifacts */
1185                 if (loaded)
1186                 {
1187                         MONSTER_IDX i;
1188                         GAME_TURN tmp_last_visit = sf_ptr->last_visit;
1189                         GAME_TURN absence_ticks;
1190                         int alloc_chance = d_info[p_ptr->dungeon_idx].max_m_alloc_chance;
1191                         GAME_TURN alloc_times;
1192
1193                         while (tmp_last_visit > current_world_ptr->game_turn) tmp_last_visit -= TURNS_PER_TICK * TOWN_DAWN;
1194                         absence_ticks = (current_world_ptr->game_turn - tmp_last_visit) / TURNS_PER_TICK;
1195
1196                         /* Maintain monsters */
1197                         for (i = 1; i < m_max; i++)
1198                         {
1199                                 monster_race *r_ptr;
1200                                 monster_type *m_ptr = &current_floor_ptr->m_list[i];
1201
1202                                 if (!monster_is_valid(m_ptr)) continue;
1203
1204                                 if (!is_pet(m_ptr))
1205                                 {
1206                                         /* Restore HP */
1207                                         m_ptr->hp = m_ptr->maxhp = m_ptr->max_maxhp;
1208
1209                                         /* Remove timed status (except MTIMED_CSLEEP) */
1210                                         (void)set_monster_fast(i, 0);
1211                                         (void)set_monster_slow(i, 0);
1212                                         (void)set_monster_stunned(i, 0);
1213                                         (void)set_monster_confused(i, 0);
1214                                         (void)set_monster_monfear(i, 0);
1215                                         (void)set_monster_invulner(i, 0, FALSE);
1216                                 }
1217
1218                                 /* Extract real monster race */
1219                                 r_ptr = real_r_ptr(m_ptr);
1220
1221                                 /* Ignore non-unique */
1222                                 if (!(r_ptr->flags1 & RF1_UNIQUE) &&
1223                                     !(r_ptr->flags7 & RF7_NAZGUL)) continue;
1224
1225                                 /* Appear at a different floor? */
1226                                 if (r_ptr->floor_id != new_floor_id)
1227                                 {
1228                                         /* Disapper from here */
1229                                         delete_monster_idx(i);
1230                                 }
1231                         }
1232
1233                         /* Maintain artifatcs */
1234                         for (i = 1; i < o_max; i++)
1235                         {
1236                                 object_type *o_ptr = &current_floor_ptr->o_list[i];
1237
1238                                 /* Skip dead objects */
1239                                 if (!o_ptr->k_idx) continue;
1240
1241                                 /* Ignore non-artifact */
1242                                 if (!object_is_fixed_artifact(o_ptr)) continue;
1243
1244                                 /* Appear at a different floor? */
1245                                 if (a_info[o_ptr->name1].floor_id != new_floor_id)
1246                                 {
1247                                         /* Disappear from here */
1248                                         delete_object_idx(i);
1249                                 }
1250                                 else
1251                                 {
1252                                         /* Cancel preserve */
1253                                         a_info[o_ptr->name1].cur_num = 1;
1254                                 }
1255                         }
1256
1257                         (void)place_quest_monsters();
1258
1259                         /* Place some random monsters */
1260                         alloc_times = absence_ticks / alloc_chance;
1261
1262                         if (randint0(alloc_chance) < (absence_ticks % alloc_chance))
1263                                 alloc_times++;
1264
1265                         for (i = 0; i < alloc_times; i++)
1266                         {
1267                                 /* Make a (group of) new monster */
1268                                 (void)alloc_monster(0, 0);
1269                         }
1270
1271                 }
1272
1273                 /* New floor_id or failed to restore */
1274                 else /* if (!loaded) */
1275                 {
1276                         if (sf_ptr->last_visit)
1277                         {
1278                                 /* Temporal file is broken? */
1279                                 msg_print(_("階段は行き止まりだった。", "The staircases come to a dead end..."));
1280
1281                                 /* Create simple dead end */
1282                                 build_dead_end();
1283
1284                                 /* Break connection */
1285                                 if (change_floor_mode & CFM_UP)
1286                                 {
1287                                         sf_ptr->upper_floor_id = 0;
1288                                 }
1289                                 else if (change_floor_mode & CFM_DOWN)
1290                                 {
1291                                         sf_ptr->lower_floor_id = 0;
1292                                 }
1293                         }
1294                         else
1295                         {
1296                                 /* Newly create current_floor_ptr->grid_array */
1297                                 generate_random_floor();
1298                         }
1299
1300                         /* Record last visit current_world_ptr->game_turn */
1301                         sf_ptr->last_visit = current_world_ptr->game_turn;
1302
1303                         /* Set correct current_floor_ptr->dun_level value */
1304                         sf_ptr->dun_level = current_floor_ptr->dun_level;
1305
1306                         /* Create connected stairs */
1307                         if (!(change_floor_mode & CFM_NO_RETURN))
1308                         {
1309                                 /* Extract stair position */
1310                                 grid_type *g_ptr = &current_floor_ptr->grid_array[p_ptr->y][p_ptr->x];
1311
1312                                 /*** Create connected stairs ***/
1313
1314                                 /* No stairs down from Quest */
1315                                 if ((change_floor_mode & CFM_UP) && !quest_number(current_floor_ptr->dun_level))
1316                                 {
1317                                         g_ptr->feat = (change_floor_mode & CFM_SHAFT) ? feat_state(feat_down_stair, FF_SHAFT) : feat_down_stair;
1318                                 }
1319
1320                                 /* No stairs up when ironman_downward */
1321                                 else if ((change_floor_mode & CFM_DOWN) && !ironman_downward)
1322                                 {
1323                                         g_ptr->feat = (change_floor_mode & CFM_SHAFT) ? feat_state(feat_up_stair, FF_SHAFT) : feat_up_stair;
1324                                 }
1325
1326                                 /* Paranoia -- Clear mimic */
1327                                 g_ptr->mimic = 0;
1328
1329                                 /* Connect to previous floor */
1330                                 g_ptr->special = p_ptr->floor_id;
1331                         }
1332                 }
1333
1334                 /* Arrive at random grid */
1335                 if (change_floor_mode & (CFM_RAND_PLACE))
1336                 {
1337                         (void)new_player_spot();
1338                 }
1339
1340                 /* You see stairs blocked */
1341                 else if ((change_floor_mode & CFM_NO_RETURN) && (change_floor_mode & (CFM_DOWN | CFM_UP)))
1342                 {
1343                         if (!p_ptr->blind)
1344                         {
1345                                 msg_print(_("突然階段が塞がれてしまった。", "Suddenly the stairs is blocked!"));
1346                         }
1347                         else
1348                         {
1349                                 msg_print(_("ゴトゴトと何か音がした。", "You hear some noises."));
1350                         }
1351                 }
1352
1353                 /*
1354                  * Update visit mark
1355                  *
1356                  * The "current_world_ptr->game_turn" is not always different number because
1357                  * the level teleport doesn't take any current_world_ptr->game_turn.  Use
1358                  * visit mark instead of last visit current_world_ptr->game_turn to find the
1359                  * oldest saved floor.
1360                  */
1361                 sf_ptr->visit_mark = latest_visit_mark++;
1362         }
1363
1364         /* Place preserved pet monsters */
1365         place_pet();
1366
1367         /* Reset travel target place */
1368         forget_travel_flow();
1369
1370         /* Hack -- maintain unique and artifacts */
1371         update_unique_artifact(new_floor_id);
1372
1373         /* Now the player is in new floor */
1374         p_ptr->floor_id = new_floor_id;
1375
1376         /* The dungeon is ready */
1377         character_dungeon = TRUE;
1378
1379         /* Hack -- Munchkin characters always get whole map */
1380         if (p_ptr->pseikaku == SEIKAKU_MUNCHKIN)
1381                 wiz_lite((bool)(p_ptr->pclass == CLASS_NINJA));
1382
1383         /* Remember when this level was "created" */
1384         current_floor_ptr->generated_turn = current_world_ptr->game_turn;
1385
1386         /* No dungeon feeling yet */
1387         p_ptr->feeling_turn = current_floor_ptr->generated_turn;
1388         p_ptr->feeling = 0;
1389
1390         /* Clear all flags */
1391         change_floor_mode = 0L;
1392
1393         select_floor_music();
1394 }
1395
1396 /*!
1397  * @brief プレイヤーの手による能動的な階段生成処理 /
1398  * Create stairs at or move previously created stairs into the player location.
1399  * @return なし
1400  */
1401 void stair_creation(void)
1402 {
1403         saved_floor_type *sf_ptr;
1404         saved_floor_type *dest_sf_ptr;
1405
1406         bool up = TRUE;
1407         bool down = TRUE;
1408         FLOOR_IDX dest_floor_id = 0;
1409
1410
1411         /* Forbid up staircases on Ironman mode */
1412         if (ironman_downward) up = FALSE;
1413
1414         /* Forbid down staircases on quest level */
1415         if (quest_number(current_floor_ptr->dun_level) || (current_floor_ptr->dun_level >= d_info[p_ptr->dungeon_idx].maxdepth)) down = FALSE;
1416
1417         /* No effect out of standard dungeon floor */
1418         if (!current_floor_ptr->dun_level || (!up && !down) ||
1419             (p_ptr->inside_quest && is_fixed_quest_idx(p_ptr->inside_quest)) ||
1420             p_ptr->inside_arena || p_ptr->inside_battle)
1421         {
1422                 /* arena or quest */
1423                 msg_print(_("効果がありません!", "There is no effect!"));
1424                 return;
1425         }
1426
1427         /* Artifacts resists */
1428         if (!cave_valid_bold(p_ptr->y, p_ptr->x))
1429         {
1430                 msg_print(_("床上のアイテムが呪文を跳ね返した。", "The object resists the spell."));
1431                 return;
1432         }
1433
1434         /* Destroy all objects in the grid */
1435         delete_object(p_ptr->y, p_ptr->x);
1436
1437         /* Extract current floor data */
1438         sf_ptr = get_sf_ptr(p_ptr->floor_id);
1439         if (!sf_ptr)
1440         {
1441                 /* No floor id? -- Create now! */
1442                 p_ptr->floor_id = get_new_floor_id();
1443                 sf_ptr = get_sf_ptr(p_ptr->floor_id);
1444         } 
1445
1446         /* Choose randomly */
1447         if (up && down)
1448         {
1449                 if (randint0(100) < 50) up = FALSE;
1450                 else down = FALSE;
1451         }
1452
1453         /* Destination is already fixed */
1454         if (up)
1455         {
1456                 if (sf_ptr->upper_floor_id) dest_floor_id = sf_ptr->upper_floor_id;
1457         }
1458         else
1459         {
1460                 if (sf_ptr->lower_floor_id) dest_floor_id = sf_ptr->lower_floor_id;
1461         }
1462
1463
1464         /* Search old stairs leading to the destination */
1465         if (dest_floor_id)
1466         {
1467                 POSITION x, y;
1468
1469                 for (y = 0; y < current_floor_ptr->height; y++)
1470                 {
1471                         for (x = 0; x < current_floor_ptr->width; x++)
1472                         {
1473                                 grid_type *g_ptr = &current_floor_ptr->grid_array[y][x];
1474
1475                                 if (!g_ptr->special) continue;
1476                                 if (feat_uses_special(g_ptr->feat)) continue;
1477                                 if (g_ptr->special != dest_floor_id) continue;
1478
1479                                 /* Remove old stairs */
1480                                 g_ptr->special = 0;
1481                                 cave_set_feat(y, x, feat_ground_type[randint0(100)]);
1482                         }
1483                 }
1484         }
1485
1486         /* No old destination -- Get new one now */
1487         else
1488         {
1489                 dest_floor_id = get_new_floor_id();
1490
1491                 /* Fix it */
1492                 if (up)
1493                         sf_ptr->upper_floor_id = dest_floor_id;
1494                 else
1495                         sf_ptr->lower_floor_id = dest_floor_id;
1496         }
1497
1498         /* Extract destination floor data */
1499         dest_sf_ptr = get_sf_ptr(dest_floor_id);
1500
1501
1502         /* Create a staircase */
1503         if (up)
1504         {
1505                 cave_set_feat(p_ptr->y, p_ptr->x,
1506                         (dest_sf_ptr->last_visit && (dest_sf_ptr->dun_level <= current_floor_ptr->dun_level - 2)) ?
1507                         feat_state(feat_up_stair, FF_SHAFT) : feat_up_stair);
1508         }
1509         else
1510         {
1511                 cave_set_feat(p_ptr->y, p_ptr->x,
1512                         (dest_sf_ptr->last_visit && (dest_sf_ptr->dun_level >= current_floor_ptr->dun_level + 2)) ?
1513                         feat_state(feat_down_stair, FF_SHAFT) : feat_down_stair);
1514         }
1515
1516
1517         /* Connect this stairs to the destination */
1518         current_floor_ptr->grid_array[p_ptr->y][p_ptr->x].special = dest_floor_id;
1519 }