OSDN Git Service

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