OSDN Git Service

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