OSDN Git Service

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