OSDN Git Service

c51a3c149a2cd4810ca551cd904f727f30c4df01
[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 "util.h"
15
16 #include "floor.h"
17 #include "floor-events.h"
18 #include "floor-generate.h"
19 #include "grid.h"
20 #include "monster.h"
21 #include "quest.h"
22 #include "wild.h"
23 #include "spells-floor.h"
24 #include "monster-status.h"
25 #include "object-hook.h"
26 #include "cmd-pet.h"
27 #include "cmd-basic.h"
28 #include "files.h"
29 #include "player-effects.h"
30
31 static FLOOR_IDX new_floor_id;  /*!<次のフロアのID / floor_id of the destination */
32 static u32b change_floor_mode;  /*!<フロア移行処理に関するフラグ / Mode flags for changing floor */
33 static u32b latest_visit_mark;  /*!<フロアを渡った回数?(確認中) / Max number of visit_mark */
34
35
36 /*!
37  * @brief 保存フロア配列を初期化する / Initialize saved_floors array. 
38  * @param force テンポラリファイルが残っていた場合も警告なしで強制的に削除する。
39  * @details Make sure that old temporal files are not remaining as gurbages.
40  * @return なし
41  */
42 void init_saved_floors(bool force)
43 {
44         char floor_savefile[1024];
45         int i;
46         int fd = -1;
47         BIT_FLAGS mode = 0644;
48
49 #ifdef SET_UID
50 # ifdef SECURE
51         /* Get "games" permissions */
52         beGames();
53 # endif
54 #endif
55
56         for (i = 0; i < MAX_SAVED_FLOORS; i++)
57         {
58                 saved_floor_type *sf_ptr = &saved_floors[i];
59
60                 /* File name */
61                 sprintf(floor_savefile, "%s.F%02d", savefile, i);
62
63                 /* Grab permissions */
64                 safe_setuid_grab();
65
66                 /* Try to create the file */
67                 fd = fd_make(floor_savefile, mode);
68
69                 /* Drop permissions */
70                 safe_setuid_drop();
71
72                 /* Failed! */
73                 if (fd < 0)
74                 {
75                         if (!force)
76                         {
77                                 msg_print(_("エラー:古いテンポラリ・ファイルが残っています。", "Error: There are old temporal files."));
78                                 msg_print(_("変愚蛮怒を二重に起動していないか確認してください。", "Make sure you are not running two game processes simultaneously."));
79                                 msg_print(_("過去に変愚蛮怒がクラッシュした場合は一時ファイルを", "If the temporal files are garbages of old crashed process, "));
80                                 msg_print(_("強制的に削除して実行を続けられます。", "you can delete it safely."));
81                                 if (!get_check(_("強制的に削除してもよろしいですか?", "Do you delete old temporal files? ")))
82                                         quit(_("実行中止", "Aborted."));
83                                 force = TRUE;
84                         }
85                 }
86                 else
87                 {
88                         /* Close the "fd" */
89                         (void)fd_close(fd);
90                 }
91
92                 /* Grab permissions */
93                 safe_setuid_grab();
94
95                 /* Simply kill the temporal file */ 
96                 (void)fd_kill(floor_savefile);
97
98                 /* Drop permissions */
99                 safe_setuid_drop();
100
101                 sf_ptr->floor_id = 0;
102         }
103
104         /* No floor_id used yet (No.0 is reserved to indicate non existance) */
105         max_floor_id = 1;
106
107         /* vist_mark is from 1 */
108         latest_visit_mark = 1;
109
110         /* A sign to mark temporal files */
111         saved_floor_file_sign = (u32b)time(NULL);
112
113         /* No next floor yet */
114         new_floor_id = 0;
115
116         /* No change floor mode yet */
117         change_floor_mode = 0;
118
119 #ifdef SET_UID
120 # ifdef SECURE
121         /* Drop "games" permissions */
122         bePlayer();
123 # endif
124 #endif
125 }
126
127 /*!
128  * @brief 保存フロア用テンポラリファイルを削除する / Kill temporal files
129  * @details Should be called just before the game quit.
130  * @return なし
131  */
132 void clear_saved_floor_files(void)
133 {
134         char floor_savefile[1024];
135         int i;
136
137 #ifdef SET_UID
138 # ifdef SECURE
139         /* Get "games" permissions */
140         beGames();
141 # endif
142 #endif
143
144         for (i = 0; i < MAX_SAVED_FLOORS; i++)
145         {
146                 saved_floor_type *sf_ptr = &saved_floors[i];
147
148                 /* No temporal file */
149                 if (!sf_ptr->floor_id) continue;
150                 if (sf_ptr->floor_id == p_ptr->floor_id) continue;
151
152                 /* File name */
153                 sprintf(floor_savefile, "%s.F%02d", savefile, i);
154
155                 /* Grab permissions */
156                 safe_setuid_grab();
157
158                 /* Simply kill the temporal file */ 
159                 (void)fd_kill(floor_savefile);
160
161                 /* Drop permissions */
162                 safe_setuid_drop();
163         }
164
165 #ifdef SET_UID
166 # ifdef SECURE
167         /* Drop "games" permissions */
168         bePlayer();
169 # endif
170 #endif
171 }
172
173 /*!
174  * @brief 保存フロアIDから参照ポインタを得る / Get a pointer for an item of the saved_floors array.
175  * @param floor_id 保存フロアID
176  * @return IDに対応する保存フロアのポインタ、ない場合はNULLを返す。
177  */
178 saved_floor_type *get_sf_ptr(FLOOR_IDX floor_id)
179 {
180         int i;
181
182         /* floor_id No.0 indicates no floor */
183         if (!floor_id) return NULL;
184
185         for (i = 0; i < MAX_SAVED_FLOORS; i++)
186         {
187                 saved_floor_type *sf_ptr = &saved_floors[i];
188
189                 if (sf_ptr->floor_id == floor_id) return sf_ptr;
190         }
191
192         /* None found */
193         return NULL;
194 }
195
196
197 /*!
198  * @brief 参照ポインタ先の保存フロアを抹消する / kill a saved floor and get an empty space
199  * @param sf_ptr 保存フロアの参照ポインタ
200  * @return なし
201  */
202 static void kill_saved_floor(saved_floor_type *sf_ptr)
203 {
204         char floor_savefile[1024];
205         if (!sf_ptr) return;
206
207         /* Already empty */
208         if (!sf_ptr->floor_id) return;
209
210         if (sf_ptr->floor_id == p_ptr->floor_id)
211         {
212                 /* Kill current floor */
213                 p_ptr->floor_id = 0;
214
215                 /* Current floor doesn't have temporal file */
216         }
217         else 
218         {
219                 /* File name */
220                 sprintf(floor_savefile, "%s.F%02d", savefile, (int)sf_ptr->savefile_id);
221
222                 /* Grab permissions */
223                 safe_setuid_grab();
224
225                 /* Simply kill the temporal file */ 
226                 (void)fd_kill(floor_savefile);
227
228                 /* Drop permissions */
229                 safe_setuid_drop();
230         }
231
232         /* No longer exists */
233         sf_ptr->floor_id = 0;
234 }
235
236
237 /*!
238  * @brief 新規に利用可能な保存フロアを返す / Initialize new saved floor and get its floor id.
239  * @return 利用可能な保存フロアID
240  * @details
241  * If number of saved floors are already MAX_SAVED_FLOORS, kill the oldest one.
242  */
243 FLOOR_IDX get_new_floor_id(void)
244 {
245         saved_floor_type *sf_ptr = NULL;
246         FLOOR_IDX i;
247
248         /* Look for empty space */
249         for (i = 0; i < MAX_SAVED_FLOORS; i++)
250         {
251                 sf_ptr = &saved_floors[i];
252
253                 if (!sf_ptr->floor_id) break;
254         }
255
256         /* None found */
257         if (i == MAX_SAVED_FLOORS)
258         {
259                 s16b oldest = 0;
260                 u32b oldest_visit = 0xffffffffL;
261
262                 /* Search for oldest */
263                 for (i = 0; i < MAX_SAVED_FLOORS; i++)
264                 {
265                         sf_ptr = &saved_floors[i];
266
267                         /* Don't kill current floor */
268                         if (sf_ptr->floor_id == p_ptr->floor_id) continue;
269
270                         /* Don't kill newer */
271                         if (sf_ptr->visit_mark > oldest_visit) continue;
272
273                         oldest = i;
274                         oldest_visit = sf_ptr->visit_mark;
275                 }
276
277                 /* Kill oldest saved floor */
278                 sf_ptr = &saved_floors[oldest];
279                 kill_saved_floor(sf_ptr);
280
281                 /* Use it */
282                 i = oldest;
283         }
284
285         /* Prepare new floor data */
286         sf_ptr->savefile_id = i;
287         sf_ptr->floor_id = max_floor_id;
288         sf_ptr->last_visit = 0;
289         sf_ptr->upper_floor_id = 0;
290         sf_ptr->lower_floor_id = 0;
291         sf_ptr->visit_mark = latest_visit_mark++;
292
293         /* sf_ptr->dun_level may be changed later */
294         sf_ptr->dun_level = current_floor_ptr->dun_level;
295
296
297         /* Increment number of floor_id */
298         if (max_floor_id < MAX_SHORT) max_floor_id++;
299
300         /* 32767 floor_ids are all used up!  Re-use ancient IDs */
301         else max_floor_id = 1;
302
303         return sf_ptr->floor_id;
304 }
305
306
307 /*!
308  * @brief フロア切り替え時の処理フラグを追加する / Prepare mode flags of changing floor
309  * @param mode 追加したい所持フラグ
310  * @return なし
311  */
312 void prepare_change_floor_mode(BIT_FLAGS mode)
313 {
314         change_floor_mode |= mode;
315 }
316
317 /*!
318  * @brief 階段移動先のフロアが生成できない時に簡単な行き止まりマップを作成する / Builds the dead end
319  * @return なし
320  */
321 static void build_dead_end(void)
322 {
323         POSITION x, y;
324
325         /* Clear and empty the current_floor_ptr->grid_array */
326         clear_cave();
327
328         /* Fill the arrays of floors and walls in the good proportions */
329         set_floor_and_wall(0);
330
331         /* Smallest area */
332         current_floor_ptr->height = SCREEN_HGT;
333         current_floor_ptr->width = SCREEN_WID;
334
335         /* Filled with permanent walls */
336         for (y = 0; y < MAX_HGT; y++)
337         {
338                 for (x = 0; x < MAX_WID; x++)
339                 {
340                         /* Create "solid" perma-wall */
341                         place_solid_perm_bold(y, x);
342                 }
343         }
344
345         /* Place at center of the floor */
346         p_ptr->y = current_floor_ptr->height / 2;
347         p_ptr->x = current_floor_ptr->width / 2;
348
349         /* Give one square */
350         place_floor_bold(p_ptr->y, p_ptr->x);
351
352         wipe_generate_random_floor_flags();
353 }
354
355
356
357 #define MAX_PARTY_MON 21 /*!< フロア移動時に先のフロアに連れて行けるペットの最大数 Maximum number of preservable pets */
358 static monster_type party_mon[MAX_PARTY_MON]; /*!< フロア移動に保存するペットモンスターの配列 */
359
360 /*!
361  * @brief フロア移動時のペット保存処理 / Preserve_pets
362  * @return なし
363  */
364 static void preserve_pet(void)
365 {
366         int num;
367         MONSTER_IDX i;
368
369         for (num = 0; num < MAX_PARTY_MON; num++)
370         {
371                 party_mon[num].r_idx = 0;
372         }
373
374         if (p_ptr->riding)
375         {
376                 monster_type *m_ptr = &current_floor_ptr->m_list[p_ptr->riding];
377
378                 /* Pet of other pet don't follow. */
379                 if (m_ptr->parent_m_idx)
380                 {
381                         p_ptr->riding = 0;
382                         p_ptr->pet_extra_flags &= ~(PF_RYOUTE);
383                         p_ptr->riding_ryoute = p_ptr->old_riding_ryoute = FALSE;
384                 }
385                 else
386                 {
387                         /* Preserve the mount */
388                         (void)COPY(&party_mon[0], m_ptr, monster_type);
389
390                         /* Delete from this floor */
391                         delete_monster_idx(p_ptr->riding);
392                 }
393         }
394
395         /*
396          * If player is in wild mode, no pets are preserved
397          * except a monster whom player riding
398          */
399         if (!p_ptr->wild_mode && !p_ptr->inside_arena && !p_ptr->inside_battle)
400         {
401                 for (i = m_max - 1, num = 1; (i >= 1 && num < MAX_PARTY_MON); i--)
402                 {
403                         monster_type *m_ptr = &current_floor_ptr->m_list[i];
404
405                         if (!monster_is_valid(m_ptr)) continue;
406                         if (!is_pet(m_ptr)) continue;
407                         if (i == p_ptr->riding) continue;
408
409                         if (reinit_wilderness)
410                         {
411                                 /* Don't lose sight of pets when getting a Quest */
412                         }
413                         else
414                         {
415                                 POSITION dis = distance(p_ptr->y, p_ptr->x, m_ptr->fy, m_ptr->fx);
416
417                                 /* Confused (etc.) monsters don't follow. */
418                                 if (MON_CONFUSED(m_ptr) || MON_STUNNED(m_ptr) || MON_CSLEEP(m_ptr)) continue;
419
420                                 /* Pet of other pet don't follow. */
421                                 if (m_ptr->parent_m_idx) continue;
422
423                                 /*
424                                  * Pets with nickname will follow even from 3 blocks away
425                                  * when you or the pet can see the other.
426                                  */
427                                 if (m_ptr->nickname && 
428                                     ((player_has_los_bold(m_ptr->fy, m_ptr->fx) && projectable(p_ptr->y, p_ptr->x, m_ptr->fy, m_ptr->fx)) ||
429                                      (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))))
430                                 {
431                                         if (dis > 3) continue;
432                                 }
433                                 else
434                                 {
435                                         if (dis > 1) continue;
436                                 }
437                         }
438
439                         (void)COPY(&party_mon[num], &current_floor_ptr->m_list[i], monster_type);
440
441                         num++;
442
443                         /* Delete from this floor */
444                         delete_monster_idx(i);
445                 }
446         }
447
448         if (record_named_pet)
449         {
450                 for (i = m_max - 1; i >=1; i--)
451                 {
452                         monster_type *m_ptr = &current_floor_ptr->m_list[i];
453                         GAME_TEXT m_name[MAX_NLEN];
454
455                         if (!monster_is_valid(m_ptr)) continue;
456                         if (!is_pet(m_ptr)) continue;
457                         if (!m_ptr->nickname) continue;
458                         if (p_ptr->riding == i) continue;
459
460                         monster_desc(m_name, m_ptr, MD_ASSUME_VISIBLE | MD_INDEF_VISIBLE);
461                         do_cmd_write_nikki(NIKKI_NAMED_PET, RECORD_NAMED_PET_MOVED, m_name);
462                 }
463         }
464
465
466         /* Pet of other pet may disappear. */
467         for (i = m_max - 1; i >=1; i--)
468         {
469                 monster_type *m_ptr = &current_floor_ptr->m_list[i];
470
471                 /* Are there its parent? */
472                 if (m_ptr->parent_m_idx && !current_floor_ptr->m_list[m_ptr->parent_m_idx].r_idx)
473                 {
474                         /* Its parent have gone, it also goes away. */
475
476                         if (is_seen(m_ptr))
477                         {
478                                 GAME_TEXT m_name[MAX_NLEN];
479                                 monster_desc(m_name, m_ptr, 0);
480                                 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
481                         }
482
483                         delete_monster_idx(i);
484                 }
485         }
486 }
487
488
489 /*!
490  * @brief フロア移動時にペットを伴った場合の準備処理 / Pre-calculate the racial counters of preserved pets
491  * @return なし
492  * @details
493  * To prevent multiple generation of unique monster who is the minion of player
494  */
495 void precalc_cur_num_of_pet(void)
496 {
497         monster_type *m_ptr;
498         int i;
499         int max_num = p_ptr->wild_mode ? 1 : MAX_PARTY_MON;
500
501         for (i = 0; i < max_num; i++)
502         {
503                 m_ptr = &party_mon[i];
504
505                 /* Skip empty monsters */
506                 if (!monster_is_valid(m_ptr)) continue;
507
508                 /* Hack -- Increase the racial counter */
509                 real_r_ptr(m_ptr)->cur_num++;
510         }
511 }
512
513 /*!
514  * @brief 移動先のフロアに伴ったペットを配置する / Place preserved pet monsters on new floor
515  * @return なし
516  */
517 static void place_pet(void)
518 {
519         int i;
520         int max_num = p_ptr->wild_mode ? 1 : MAX_PARTY_MON;
521
522         for (i = 0; i < max_num; i++)
523         {
524                 POSITION cy = 0, cx = 0;
525                 MONSTER_IDX m_idx;
526
527                 if (!(party_mon[i].r_idx)) continue;
528
529                 if (i == 0)
530                 {
531                         m_idx = m_pop();
532                         p_ptr->riding = m_idx;
533                         if (m_idx)
534                         {
535                                 cy = p_ptr->y;
536                                 cx = p_ptr->x;
537                         }
538                 }
539                 else
540                 {
541                         int j;
542                         POSITION d;
543
544                         for (d = 1; d < A_MAX; d++)
545                         {
546                                 for (j = 1000; j > 0; j--)
547                                 {
548                                         scatter(&cy, &cx, p_ptr->y, p_ptr->x, d, 0);
549                                         if (monster_can_enter(cy, cx, &r_info[party_mon[i].r_idx], 0)) break;
550                                 }
551                                 if (j) break;
552                         }
553                         m_idx = (d == 6) ? 0 : m_pop();
554                 }
555
556                 if (m_idx)
557                 {
558                         monster_type *m_ptr = &current_floor_ptr->m_list[m_idx];
559                         monster_race *r_ptr;
560
561                         current_floor_ptr->grid_array[cy][cx].m_idx = m_idx;
562
563                         m_ptr->r_idx = party_mon[i].r_idx;
564
565                         /* Copy all member of the structure */
566                         *m_ptr = party_mon[i];
567                         r_ptr = real_r_ptr(m_ptr);
568
569                         m_ptr->fy = cy;
570                         m_ptr->fx = cx;
571                         m_ptr->ml = TRUE;
572                         m_ptr->mtimed[MTIMED_CSLEEP] = 0;
573                         m_ptr->hold_o_idx = 0;
574                         m_ptr->target_y = 0;
575
576                         if ((r_ptr->flags1 & RF1_FORCE_SLEEP) && !ironman_nightmare)
577                         {
578                                 /* Monster is still being nice */
579                                 m_ptr->mflag |= (MFLAG_NICE);
580
581                                 /* Must repair monsters */
582                                 repair_monsters = TRUE;
583                         }
584                         update_monster(m_idx, TRUE);
585                         lite_spot(cy, cx);
586
587                         /* Pre-calculated in precalc_cur_num_of_pet() */
588                         /* r_ptr->cur_num++; */
589
590                         /* Hack -- Count the number of "reproducers" */
591                         if (r_ptr->flags2 & RF2_MULTIPLY) current_floor_ptr->num_repro++;
592
593                         /* Hack -- Notice new multi-hued monsters */
594                         {
595                                 monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
596                                 if (ap_r_ptr->flags1 & (RF1_ATTR_MULTI | RF1_SHAPECHANGER))
597                                         shimmer_monsters = TRUE;
598                         }
599                 }
600                 else
601                 {
602                         monster_type *m_ptr = &party_mon[i];
603                         monster_race *r_ptr = real_r_ptr(m_ptr);
604                         GAME_TEXT m_name[MAX_NLEN];
605
606                         monster_desc(m_name, m_ptr, 0);
607                         msg_format(_("%sとはぐれてしまった。", "You have lost sight of %s."), m_name);
608                         if (record_named_pet && m_ptr->nickname)
609                         {
610                                 monster_desc(m_name, m_ptr, MD_INDEF_VISIBLE);
611                                 do_cmd_write_nikki(NIKKI_NAMED_PET, RECORD_NAMED_PET_LOST_SIGHT, m_name);
612                         }
613
614                         /* Pre-calculated in precalc_cur_num_of_pet(), but need to decrease */
615                         if (r_ptr->cur_num) r_ptr->cur_num--;
616                 }
617         }
618
619         /* For accuracy of precalc_cur_num_of_pet() */
620         (void)C_WIPE(party_mon, MAX_PARTY_MON, monster_type);
621 }
622
623
624 /*!
625  * @brief ユニークモンスターやアーティファクトの所在フロアを更新する / Hack -- Update location of unique monsters and artifacts
626  * @param cur_floor_id 現在のフロアID
627  * @return なし
628  * @details 
629  * The r_ptr->floor_id and a_ptr->floor_id are not updated correctly\n
630  * while new floor creation since dungeons may be re-created by\n
631  * auto-scum option.\n
632  */
633 static void update_unique_artifact(s16b cur_floor_id)
634 {
635         int i;
636
637         /* Maintain unique monsters */
638         for (i = 1; i < m_max; i++)
639         {
640                 monster_race *r_ptr;
641                 monster_type *m_ptr = &current_floor_ptr->m_list[i];
642
643                 if (!monster_is_valid(m_ptr)) continue;
644
645                 /* Extract real monster race */
646                 r_ptr = real_r_ptr(m_ptr);
647
648                 /* Memorize location of the unique monster */
649                 if ((r_ptr->flags1 & RF1_UNIQUE) ||
650                     (r_ptr->flags7 & RF7_NAZGUL))
651                 {
652                         r_ptr->floor_id = cur_floor_id;
653                 }
654         }
655
656         /* Maintain artifatcs */
657         for (i = 1; i < o_max; i++)
658         {
659                 object_type *o_ptr = &current_floor_ptr->o_list[i];
660
661                 /* Skip dead objects */
662                 if (!o_ptr->k_idx) continue;
663
664                 /* Memorize location of the artifact */
665                 if (object_is_fixed_artifact(o_ptr))
666                 {
667                         a_info[o_ptr->name1].floor_id = cur_floor_id;
668                 }
669         }
670 }
671
672
673 /*!
674  * @brief フロア移動時、プレイヤーの移動先モンスターが既にいた場合ランダムな近隣に移動させる / When a monster is at a place where player will return,
675  * @return なし
676  */
677 static void get_out_monster(void)
678 {
679         int tries = 0;
680         POSITION dis = 1;
681         POSITION oy = p_ptr->y;
682         POSITION ox = p_ptr->x;
683         MONSTER_IDX m_idx = current_floor_ptr->grid_array[oy][ox].m_idx;
684
685         /* Nothing to do if no monster */
686         if (!m_idx) return;
687
688         /* Look until done */
689         while (TRUE)
690         {
691                 monster_type *m_ptr;
692
693                 /* Pick a (possibly illegal) location */
694                 POSITION ny = rand_spread(oy, dis);
695                 POSITION nx = rand_spread(ox, dis);
696
697                 tries++;
698
699                 /* Stop after 1000 tries */
700                 if (tries > 10000) return;
701
702                 /*
703                  * Increase distance after doing enough tries
704                  * compared to area of possible space
705                  */
706                 if (tries > 20 * dis * dis) dis++;
707
708                 /* Ignore illegal locations */
709                 if (!in_bounds(ny, nx)) continue;
710
711                 /* Require "empty" floor space */
712                 if (!cave_empty_bold(ny, nx)) continue;
713
714                 /* Hack -- no teleport onto glyph of warding */
715                 if (is_glyph_grid(&current_floor_ptr->grid_array[ny][nx])) continue;
716                 if (is_explosive_rune_grid(&current_floor_ptr->grid_array[ny][nx])) continue;
717
718                 /* ...nor onto the Pattern */
719                 if (pattern_tile(ny, nx)) continue;
720
721                 /*** It's a good place ***/
722
723                 m_ptr = &current_floor_ptr->m_list[m_idx];
724
725                 /* Update the old location */
726                 current_floor_ptr->grid_array[oy][ox].m_idx = 0;
727
728                 /* Update the new location */
729                 current_floor_ptr->grid_array[ny][nx].m_idx = m_idx;
730
731                 /* Move the monster */
732                 m_ptr->fy = ny;
733                 m_ptr->fx = nx; 
734
735                 /* No need to do update_monster() */
736
737                 /* Success */
738                 return;
739         }
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 }