OSDN Git Service

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