OSDN Git Service

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