OSDN Git Service

[Refactor] #38997 leave_floor() に player_type * 引数を追加. / Add player_type * argument...
[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(floor_type *floor_ptr)
346 {
347         POSITION x, y;
348
349         clear_cave(floor_ptr);
350
351         /* Fill the arrays of floors and walls in the good proportions */
352         set_floor_and_wall(0);
353
354         /* Smallest area */
355         floor_ptr->height = SCREEN_HGT;
356         floor_ptr->width = SCREEN_WID;
357
358         /* Filled with permanent walls */
359         for (y = 0; y < MAX_HGT; y++)
360         {
361                 for (x = 0; x < MAX_WID; x++)
362                 {
363                         /* Create "solid" perma-wall */
364                         place_solid_perm_bold(y, x);
365                 }
366         }
367
368         /* Place at center of the floor */
369         p_ptr->y = floor_ptr->height / 2;
370         p_ptr->x = floor_ptr->width / 2;
371
372         /* Give one square */
373         place_floor_bold(p_ptr->y, p_ptr->x);
374
375         wipe_generate_random_floor_flags(floor_ptr);
376 }
377
378
379
380 #define MAX_PARTY_MON 21 /*!< フロア移動時に先のフロアに連れて行けるペットの最大数 Maximum number of preservable pets */
381 static monster_type party_mon[MAX_PARTY_MON]; /*!< フロア移動に保存するペットモンスターの配列 */
382
383 /*!
384  * @brief フロア移動時のペット保存処理 / Preserve_pets
385  * @return なし
386  */
387 static void preserve_pet(void)
388 {
389         int num;
390         MONSTER_IDX i;
391
392         for (num = 0; num < MAX_PARTY_MON; num++)
393         {
394                 party_mon[num].r_idx = 0;
395         }
396
397         if (p_ptr->riding)
398         {
399                 monster_type *m_ptr = &current_floor_ptr->m_list[p_ptr->riding];
400
401                 /* Pet of other pet don't follow. */
402                 if (m_ptr->parent_m_idx)
403                 {
404                         p_ptr->riding = 0;
405                         p_ptr->pet_extra_flags &= ~(PF_RYOUTE);
406                         p_ptr->riding_ryoute = p_ptr->old_riding_ryoute = FALSE;
407                 }
408                 else
409                 {
410                         /* Preserve the mount */
411                         (void)COPY(&party_mon[0], m_ptr, monster_type);
412
413                         /* Delete from this floor */
414                         delete_monster_idx(p_ptr->riding);
415                 }
416         }
417
418         /*
419          * If player is in wild mode, no pets are preserved
420          * except a monster whom player riding
421          */
422         if (!p_ptr->wild_mode && !p_ptr->inside_arena && !p_ptr->phase_out)
423         {
424                 for (i = current_floor_ptr->m_max - 1, num = 1; (i >= 1 && num < MAX_PARTY_MON); i--)
425                 {
426                         monster_type *m_ptr = &current_floor_ptr->m_list[i];
427
428                         if (!monster_is_valid(m_ptr)) continue;
429                         if (!is_pet(m_ptr)) continue;
430                         if (i == p_ptr->riding) continue;
431
432                         if (reinit_wilderness)
433                         {
434                                 /* Don't lose sight of pets when getting a Quest */
435                         }
436                         else
437                         {
438                                 POSITION dis = distance(p_ptr->y, p_ptr->x, m_ptr->fy, m_ptr->fx);
439
440                                 /* Confused (etc.) monsters don't follow. */
441                                 if (MON_CONFUSED(m_ptr) || MON_STUNNED(m_ptr) || MON_CSLEEP(m_ptr)) continue;
442
443                                 /* Pet of other pet don't follow. */
444                                 if (m_ptr->parent_m_idx) continue;
445
446                                 /*
447                                  * Pets with nickname will follow even from 3 blocks away
448                                  * when you or the pet can see the other.
449                                  */
450                                 if (m_ptr->nickname && 
451                                     ((player_has_los_bold(m_ptr->fy, m_ptr->fx) && projectable(p_ptr->y, p_ptr->x, m_ptr->fy, m_ptr->fx)) ||
452                                      (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))))
453                                 {
454                                         if (dis > 3) continue;
455                                 }
456                                 else
457                                 {
458                                         if (dis > 1) continue;
459                                 }
460                         }
461
462                         (void)COPY(&party_mon[num], &current_floor_ptr->m_list[i], monster_type);
463
464                         num++;
465
466                         /* Delete from this floor */
467                         delete_monster_idx(i);
468                 }
469         }
470
471         if (record_named_pet)
472         {
473                 for (i = current_floor_ptr->m_max - 1; i >=1; i--)
474                 {
475                         monster_type *m_ptr = &current_floor_ptr->m_list[i];
476                         GAME_TEXT m_name[MAX_NLEN];
477
478                         if (!monster_is_valid(m_ptr)) continue;
479                         if (!is_pet(m_ptr)) continue;
480                         if (!m_ptr->nickname) continue;
481                         if (p_ptr->riding == i) continue;
482
483                         monster_desc(m_name, m_ptr, MD_ASSUME_VISIBLE | MD_INDEF_VISIBLE);
484                         exe_write_diary(p_ptr, NIKKI_NAMED_PET, RECORD_NAMED_PET_MOVED, m_name);
485                 }
486         }
487
488
489         /* Pet of other pet may disappear. */
490         for (i = current_floor_ptr->m_max - 1; i >=1; i--)
491         {
492                 monster_type *m_ptr = &current_floor_ptr->m_list[i];
493
494                 /* Are there its parent? */
495                 if (m_ptr->parent_m_idx && !current_floor_ptr->m_list[m_ptr->parent_m_idx].r_idx)
496                 {
497                         /* Its parent have gone, it also goes away. */
498
499                         if (is_seen(m_ptr))
500                         {
501                                 GAME_TEXT m_name[MAX_NLEN];
502                                 monster_desc(m_name, m_ptr, 0);
503                                 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
504                         }
505
506                         delete_monster_idx(i);
507                 }
508         }
509 }
510
511
512 /*!
513  * @brief フロア移動時にペットを伴った場合の準備処理 / Pre-calculate the racial counters of preserved pets
514  * @return なし
515  * @details
516  * To prevent multiple generation of unique monster who is the minion of player
517  */
518 void precalc_cur_num_of_pet(void)
519 {
520         monster_type *m_ptr;
521         int i;
522         int max_num = p_ptr->wild_mode ? 1 : MAX_PARTY_MON;
523
524         for (i = 0; i < max_num; i++)
525         {
526                 m_ptr = &party_mon[i];
527
528                 /* Skip empty monsters */
529                 if (!monster_is_valid(m_ptr)) continue;
530
531                 /* Hack -- Increase the racial counter */
532                 real_r_ptr(m_ptr)->cur_num++;
533         }
534 }
535
536 /*!
537  * @brief 移動先のフロアに伴ったペットを配置する / Place preserved pet monsters on new floor
538  * @return なし
539  */
540 static void place_pet(player_type *master_ptr)
541 {
542         int i;
543         int max_num = master_ptr->wild_mode ? 1 : MAX_PARTY_MON;
544
545         for (i = 0; i < max_num; i++)
546         {
547                 POSITION cy = 0, cx = 0;
548                 MONSTER_IDX m_idx;
549
550                 if (!(party_mon[i].r_idx)) continue;
551
552                 if (i == 0)
553                 {
554                         m_idx = m_pop();
555                         master_ptr->riding = m_idx;
556                         if (m_idx)
557                         {
558                                 cy = master_ptr->y;
559                                 cx = master_ptr->x;
560                         }
561                 }
562                 else
563                 {
564                         int j;
565                         POSITION d;
566
567                         for (d = 1; d < A_MAX; d++)
568                         {
569                                 for (j = 1000; j > 0; j--)
570                                 {
571                                         scatter(&cy, &cx, master_ptr->y, master_ptr->x, d, 0);
572                                         if (monster_can_enter(cy, cx, &r_info[party_mon[i].r_idx], 0)) break;
573                                 }
574                                 if (j) break;
575                         }
576                         m_idx = (d == 6) ? 0 : m_pop();
577                 }
578
579                 if (m_idx)
580                 {
581                         monster_type *m_ptr = &current_floor_ptr->m_list[m_idx];
582                         monster_race *r_ptr;
583
584                         current_floor_ptr->grid_array[cy][cx].m_idx = m_idx;
585
586                         m_ptr->r_idx = party_mon[i].r_idx;
587
588                         /* Copy all member of the structure */
589                         *m_ptr = party_mon[i];
590                         r_ptr = real_r_ptr(m_ptr);
591
592                         m_ptr->fy = cy;
593                         m_ptr->fx = cx;
594                         m_ptr->ml = TRUE;
595                         m_ptr->mtimed[MTIMED_CSLEEP] = 0;
596                         m_ptr->hold_o_idx = 0;
597                         m_ptr->target_y = 0;
598
599                         if ((r_ptr->flags1 & RF1_FORCE_SLEEP) && !ironman_nightmare)
600                         {
601                                 /* Monster is still being nice */
602                                 m_ptr->mflag |= (MFLAG_NICE);
603
604                                 /* Must repair monsters */
605                                 repair_monsters = TRUE;
606                         }
607                         update_monster(m_idx, TRUE);
608                         lite_spot(cy, cx);
609
610                         /* Pre-calculated in precalc_cur_num_of_pet() */
611                         /* r_ptr->cur_num++; */
612
613                         /* Hack -- Count the number of "reproducers" */
614                         if (r_ptr->flags2 & RF2_MULTIPLY) current_floor_ptr->num_repro++;
615
616                 }
617                 else
618                 {
619                         monster_type *m_ptr = &party_mon[i];
620                         monster_race *r_ptr = real_r_ptr(m_ptr);
621                         GAME_TEXT m_name[MAX_NLEN];
622
623                         monster_desc(m_name, m_ptr, 0);
624                         msg_format(_("%sとはぐれてしまった。", "You have lost sight of %s."), m_name);
625                         if (record_named_pet && m_ptr->nickname)
626                         {
627                                 monster_desc(m_name, m_ptr, MD_INDEF_VISIBLE);
628                                 exe_write_diary(master_ptr, NIKKI_NAMED_PET, RECORD_NAMED_PET_LOST_SIGHT, m_name);
629                         }
630
631                         /* Pre-calculated in precalc_cur_num_of_pet(), but need to decrease */
632                         if (r_ptr->cur_num) r_ptr->cur_num--;
633                 }
634         }
635
636         /* For accuracy of precalc_cur_num_of_pet() */
637         (void)C_WIPE(party_mon, MAX_PARTY_MON, monster_type);
638 }
639
640
641 /*!
642  * @brief ユニークモンスターやアーティファクトの所在フロアを更新する / Hack -- Update location of unique monsters and artifacts
643  * @param cur_floor_id 現在のフロアID
644  * @return なし
645  * @details 
646  * The r_ptr->floor_id and a_ptr->floor_id are not updated correctly\n
647  * while new floor creation since dungeons may be re-created by\n
648  * auto-scum option.\n
649  */
650 static void update_unique_artifact(s16b cur_floor_id)
651 {
652         int i;
653
654         /* Maintain unique monsters */
655         for (i = 1; i < current_floor_ptr->m_max; i++)
656         {
657                 monster_race *r_ptr;
658                 monster_type *m_ptr = &current_floor_ptr->m_list[i];
659
660                 if (!monster_is_valid(m_ptr)) continue;
661
662                 /* Extract real monster race */
663                 r_ptr = real_r_ptr(m_ptr);
664
665                 /* Memorize location of the unique monster */
666                 if ((r_ptr->flags1 & RF1_UNIQUE) ||
667                     (r_ptr->flags7 & RF7_NAZGUL))
668                 {
669                         r_ptr->floor_id = cur_floor_id;
670                 }
671         }
672
673         /* Maintain artifatcs */
674         for (i = 1; i < current_floor_ptr->o_max; i++)
675         {
676                 object_type *o_ptr = &current_floor_ptr->o_list[i];
677
678                 if (!OBJECT_IS_VALID(o_ptr)) continue;
679
680                 /* Memorize location of the artifact */
681                 if (object_is_fixed_artifact(o_ptr))
682                 {
683                         a_info[o_ptr->name1].floor_id = cur_floor_id;
684                 }
685         }
686 }
687
688
689 /*!
690  * @brief フロア移動時、プレイヤーの移動先モンスターが既にいた場合ランダムな近隣に移動させる / When a monster is at a place where player will return,
691  * @return なし
692  */
693 static void get_out_monster(void)
694 {
695         int tries = 0;
696         POSITION dis = 1;
697         POSITION oy = p_ptr->y;
698         POSITION ox = p_ptr->x;
699         MONSTER_IDX m_idx = current_floor_ptr->grid_array[oy][ox].m_idx;
700
701         /* Nothing to do if no monster */
702         if (!m_idx) return;
703
704         /* Look until done */
705         while (TRUE)
706         {
707                 monster_type *m_ptr;
708
709                 /* Pick a (possibly illegal) location */
710                 POSITION ny = rand_spread(oy, dis);
711                 POSITION nx = rand_spread(ox, dis);
712
713                 tries++;
714
715                 /* Stop after 1000 tries */
716                 if (tries > 10000) return;
717
718                 /*
719                  * Increase distance after doing enough tries
720                  * compared to area of possible space
721                  */
722                 if (tries > 20 * dis * dis) dis++;
723
724                 /* Ignore illegal locations */
725                 if (!in_bounds(ny, nx)) continue;
726
727                 /* Require "empty" floor space */
728                 if (!cave_empty_bold(ny, nx)) continue;
729
730                 /* Hack -- no teleport onto glyph of warding */
731                 if (is_glyph_grid(&current_floor_ptr->grid_array[ny][nx])) continue;
732                 if (is_explosive_rune_grid(&current_floor_ptr->grid_array[ny][nx])) continue;
733
734                 /* ...nor onto the Pattern */
735                 if (pattern_tile(ny, nx)) continue;
736
737                 /*** It's a good place ***/
738
739                 m_ptr = &current_floor_ptr->m_list[m_idx];
740
741                 /* Update the old location */
742                 current_floor_ptr->grid_array[oy][ox].m_idx = 0;
743
744                 /* Update the new location */
745                 current_floor_ptr->grid_array[ny][nx].m_idx = m_idx;
746
747                 /* Move the monster */
748                 m_ptr->fy = ny;
749                 m_ptr->fx = nx; 
750
751                 /* No need to do update_monster() */
752
753                 /* Success */
754                 return;
755         }
756 }
757
758 /*!
759  * @brief 新フロアに移動元フロアに繋がる階段を配置する / Virtually teleport onto the stairs that is connecting between two floors.
760  * @param sf_ptr 移動元の保存フロア構造体参照ポインタ
761  * @return なし
762  */
763 static void locate_connected_stairs(saved_floor_type *sf_ptr, BIT_FLAGS floor_mode)
764 {
765         POSITION x, y, sx = 0, sy = 0;
766         POSITION x_table[20];
767         POSITION y_table[20];
768         int num = 0;
769         int i;
770
771         /* Search usable stairs */
772         for (y = 0; y < current_floor_ptr->height; y++)
773         {
774                 for (x = 0; x < current_floor_ptr->width; x++)
775                 {
776                         grid_type *g_ptr = &current_floor_ptr->grid_array[y][x];
777                         feature_type *f_ptr = &f_info[g_ptr->feat];
778                         bool ok = FALSE;
779
780                         if (floor_mode & CFM_UP)
781                         {
782                                 if (have_flag(f_ptr->flags, FF_LESS) && have_flag(f_ptr->flags, FF_STAIRS) &&
783                                     !have_flag(f_ptr->flags, FF_SPECIAL))
784                                 {
785                                         ok = TRUE;
786
787                                         /* Found fixed stairs? */
788                                         if (g_ptr->special &&
789                                             g_ptr->special == sf_ptr->upper_floor_id)
790                                         {
791                                                 sx = x;
792                                                 sy = y;
793                                         }
794                                 }
795                         }
796
797                         else if (floor_mode & CFM_DOWN)
798                         {
799                                 if (have_flag(f_ptr->flags, FF_MORE) && have_flag(f_ptr->flags, FF_STAIRS) &&
800                                     !have_flag(f_ptr->flags, FF_SPECIAL))
801                                 {
802                                         ok = TRUE;
803
804                                         /* Found fixed stairs */
805                                         if (g_ptr->special &&
806                                             g_ptr->special == sf_ptr->lower_floor_id)
807                                         {
808                                                 sx = x;
809                                                 sy = y;
810                                         }
811                                 }
812                         }
813
814                         else
815                         {
816                                 if (have_flag(f_ptr->flags, FF_BLDG))
817                                 {
818                                         ok = TRUE;
819                                 }
820                         }
821
822                         if (ok && (num < 20))
823                         {
824                                 x_table[num] = x;
825                                 y_table[num] = y;
826                                 num++;
827                         }
828                 }
829         }
830
831         if (sx)
832         {
833                 /* Already fixed */
834                 p_ptr->y = sy;
835                 p_ptr->x = sx;
836         }
837         else if (!num)
838         {
839                 /* No stairs found! -- No return */
840                 prepare_change_floor_mode(CFM_RAND_PLACE | CFM_NO_RETURN);
841
842                 /* Mega Hack -- It's not the stairs you enter.  Disable it.  */
843                 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;
844         }
845         else
846         {
847                 /* Choose random one */
848                 i = randint0(num);
849
850                 /* Point stair location */
851                 p_ptr->y = y_table[i];
852                 p_ptr->x = x_table[i];
853         }
854 }
855
856 /*!
857  * @brief 現在のフロアを離れるに伴って行なわれる保存処理
858  * / Maintain quest monsters, mark next floor_id at stairs, save current floor, and prepare to enter next floor.
859  * @return なし
860  */
861 void leave_floor(player_type *creature_ptr, BIT_FLAGS floor_mode)
862 {
863         grid_type *g_ptr = NULL;
864         feature_type *f_ptr;
865         saved_floor_type *sf_ptr;
866         MONRACE_IDX quest_r_idx = 0;
867         DUNGEON_IDX i;
868
869         /* Preserve pets and prepare to take these to next floor */
870         preserve_pet();
871
872         /* Remove all mirrors without explosion */
873         remove_all_mirrors(FALSE);
874
875         if (creature_ptr->special_defense & NINJA_S_STEALTH) set_superstealth(creature_ptr, FALSE);
876
877         /* New floor is not yet prepared */
878         new_floor_id = 0;
879
880         /* Temporary get a floor_id (for Arena) */
881         if (!creature_ptr->floor_id &&
882             (floor_mode & CFM_SAVE_FLOORS) &&
883             !(floor_mode & CFM_NO_RETURN))
884         {
885             /* Get temporal floor_id */
886             creature_ptr->floor_id = get_new_floor_id();
887         }
888
889         /* Search the quest monster index */
890         for (i = 0; i < max_q_idx; i++)
891         {
892                 if ((quest[i].status == QUEST_STATUS_TAKEN) &&
893                     ((quest[i].type == QUEST_TYPE_KILL_LEVEL) ||
894                     (quest[i].type == QUEST_TYPE_RANDOM)) &&
895                     (quest[i].level == current_floor_ptr->dun_level) &&
896                     (creature_ptr->dungeon_idx == quest[i].dungeon) &&
897                     !(quest[i].flags & QUEST_FLAG_PRESET))
898                 {
899                         quest_r_idx = quest[i].r_idx;
900                 }
901         }
902
903         /* Maintain quest monsters */
904         for (i = 1; i < current_floor_ptr->m_max; i++)
905         {
906                 monster_race *r_ptr;
907                 monster_type *m_ptr = &current_floor_ptr->m_list[i];
908
909                 if (!monster_is_valid(m_ptr)) continue;
910
911                 /* Only maintain quest monsters */
912                 if (quest_r_idx != m_ptr->r_idx) continue;
913
914                 /* Extract real monster race */
915                 r_ptr = real_r_ptr(m_ptr);
916
917                 /* Ignore unique monsters */
918                 if ((r_ptr->flags1 & RF1_UNIQUE) ||
919                     (r_ptr->flags7 & RF7_NAZGUL)) continue;
920
921                 /* Delete non-unique quest monsters */
922                 delete_monster_idx(i);
923         }
924
925         /* Check if there is a same item */
926         for (i = 0; i < INVEN_PACK; i++)
927         {
928                 object_type *o_ptr = &creature_ptr->inventory_list[i];
929
930                 if (!OBJECT_IS_VALID(o_ptr)) continue;
931
932                 /* Delete old memorized location of the artifact */
933                 if (object_is_fixed_artifact(o_ptr))
934                 {
935                         a_info[o_ptr->name1].floor_id = 0;
936                 }
937         }
938
939         /* Extract current floor info or NULL */
940         sf_ptr = get_sf_ptr(creature_ptr->floor_id);
941
942         /* Choose random stairs */
943         if ((floor_mode & CFM_RAND_CONNECT) && creature_ptr->floor_id)
944         {
945                 locate_connected_stairs(sf_ptr, floor_mode);
946         }
947
948         /* Extract new dungeon level */
949         if (floor_mode & CFM_SAVE_FLOORS)
950         {
951                 /* Extract stair position */
952                 g_ptr = &current_floor_ptr->grid_array[creature_ptr->y][creature_ptr->x];
953                 f_ptr = &f_info[g_ptr->feat];
954
955                 /* Get back to old saved floor? */
956                 if (g_ptr->special && !have_flag(f_ptr->flags, FF_SPECIAL) && get_sf_ptr(g_ptr->special))
957                 {
958                         /* Saved floor is exist.  Use it. */
959                         new_floor_id = g_ptr->special;
960                 }
961
962                 /* Mark shaft up/down */
963                 if (have_flag(f_ptr->flags, FF_STAIRS) && have_flag(f_ptr->flags, FF_SHAFT))
964                 {
965                         prepare_change_floor_mode(CFM_SHAFT);
966                 }
967         }
968
969         /* Climb up/down some sort of stairs */
970         if (floor_mode & (CFM_DOWN | CFM_UP))
971         {
972                 int move_num = 0;
973
974                 /* Extract level movement number */
975                 if (floor_mode & CFM_DOWN) move_num = 1;
976                 else if (floor_mode & CFM_UP) move_num = -1;
977
978                 /* Shafts are deeper than normal stairs */
979                 if (floor_mode & CFM_SHAFT)
980                         move_num += SGN(move_num);
981
982                 /* Get out from or Enter the dungeon */
983                 if (floor_mode & CFM_DOWN)
984                 {
985                         if (!current_floor_ptr->dun_level)
986                                 move_num = d_info[creature_ptr->dungeon_idx].mindepth;
987                 }
988                 else if (floor_mode & CFM_UP)
989                 {
990                         if (current_floor_ptr->dun_level + move_num < d_info[creature_ptr->dungeon_idx].mindepth)
991                                 move_num = -current_floor_ptr->dun_level;
992                 }
993
994                 current_floor_ptr->dun_level += move_num;
995         }
996
997         /* Leaving the dungeon to town */
998         if (!current_floor_ptr->dun_level && creature_ptr->dungeon_idx)
999         {
1000                 creature_ptr->leaving_dungeon = TRUE;
1001                 if (!vanilla_town && !lite_town)
1002                 {
1003                         creature_ptr->wilderness_y = d_info[creature_ptr->dungeon_idx].dy;
1004                         creature_ptr->wilderness_x = d_info[creature_ptr->dungeon_idx].dx;
1005                 }
1006                 creature_ptr->recall_dungeon = creature_ptr->dungeon_idx;
1007                 creature_ptr->dungeon_idx = 0;
1008
1009                 /* Reach to the surface -- Clear all saved floors */
1010                 floor_mode &= ~CFM_SAVE_FLOORS;
1011         }
1012
1013         /* Kill some old saved floors */
1014         if (!(floor_mode & CFM_SAVE_FLOORS))
1015         {
1016                 /* Kill all saved floors */
1017                 for (i = 0; i < MAX_SAVED_FLOORS; i++)
1018                         kill_saved_floor(&saved_floors[i]);
1019
1020                 /* Reset visit_mark count */
1021                 latest_visit_mark = 1;
1022         }
1023         else if (floor_mode & CFM_NO_RETURN)
1024         {
1025                 /* Kill current floor */
1026                 kill_saved_floor(sf_ptr);
1027         }
1028
1029         /* No current floor -- Left/Enter dungeon etc... */
1030         if (!creature_ptr->floor_id)
1031         {
1032                 /* No longer need to save current floor */
1033                 return;
1034         }
1035
1036
1037         /* Mark next floor_id on the previous floor */
1038         if (!new_floor_id)
1039         {
1040                 /* Get new id */
1041                 new_floor_id = get_new_floor_id();
1042
1043                 /* Connect from here */
1044                 if (g_ptr && !feat_uses_special(g_ptr->feat))
1045                 {
1046                         g_ptr->special = new_floor_id;
1047                 }
1048         }
1049
1050         /* Fix connection -- level teleportation or trap door */
1051         if (floor_mode & CFM_RAND_CONNECT)
1052         {
1053                 if (floor_mode & CFM_UP)
1054                         sf_ptr->upper_floor_id = new_floor_id;
1055                 else if (floor_mode & CFM_DOWN)
1056                         sf_ptr->lower_floor_id = new_floor_id;
1057         }
1058
1059         /* If you can return, you need to save previous floor */
1060         if ((floor_mode & CFM_SAVE_FLOORS) &&
1061             !(floor_mode & CFM_NO_RETURN))
1062         {
1063                 /* Get out of the my way! */
1064                 get_out_monster();
1065
1066                 /* Record the last visit current_world_ptr->game_turn of current floor */
1067                 sf_ptr->last_visit = current_world_ptr->game_turn;
1068
1069                 forget_lite();
1070                 forget_view();
1071                 clear_mon_lite(current_floor_ptr);
1072
1073                 /* Save current floor */
1074                 if (!save_floor(sf_ptr, 0))
1075                 {
1076                         /* Save failed -- No return */
1077                         prepare_change_floor_mode(CFM_NO_RETURN);
1078
1079                         /* Kill current floor */
1080                         kill_saved_floor(get_sf_ptr(creature_ptr->floor_id));
1081                 }
1082         }
1083 }
1084
1085
1086 /*!
1087  * @brief フロアの切り替え処理 / Enter new floor.
1088  * @return なし
1089  * @details
1090  * If the floor is an old saved floor, it will be\n
1091  * restored from the temporal file.  If the floor is new one, new current_floor_ptr->grid_array\n
1092  * will be generated.\n
1093  */
1094 void change_floor(BIT_FLAGS floor_mode)
1095 {
1096         saved_floor_type *sf_ptr;
1097         bool loaded = FALSE;
1098
1099         /* The dungeon is not ready */
1100         current_world_ptr->character_dungeon = FALSE;
1101
1102         /* No longer in the trap detecteded region */
1103         p_ptr->dtrap = FALSE;
1104
1105         /* Mega-Hack -- no panel yet */
1106         panel_row_min = 0;
1107         panel_row_max = 0;
1108         panel_col_min = 0;
1109         panel_col_max = 0;
1110
1111         /* Mega-Hack -- not ambushed on the wildness? */
1112         p_ptr->ambush_flag = FALSE;
1113
1114         /* No saved floors (On the surface etc.) */
1115         if (!(floor_mode & CFM_SAVE_FLOORS) &&
1116             !(floor_mode & CFM_FIRST_FLOOR))
1117         {
1118                 /* Create current_floor_ptr->grid_array */
1119                 generate_random_floor(current_floor_ptr);
1120
1121                 /* Paranoia -- No new saved floor */
1122                 new_floor_id = 0;
1123         }
1124
1125         /* In the dungeon */
1126         else
1127         {
1128                 /* No floor_id yet */
1129                 if (!new_floor_id)
1130                 {
1131                         /* Get new id */
1132                         new_floor_id = get_new_floor_id();
1133                 }
1134
1135                 /* Pointer for infomations of new floor */
1136                 sf_ptr = get_sf_ptr(new_floor_id);
1137
1138                 /* Try to restore old floor */
1139                 if (sf_ptr->last_visit)
1140                 {
1141                         /* Old saved floor is exist */
1142                         if (load_floor(sf_ptr, 0))
1143                         {
1144                                 loaded = TRUE;
1145
1146                                 /* Forbid return stairs */
1147                                 if (floor_mode & CFM_NO_RETURN)
1148                                 {
1149                                         grid_type *g_ptr = &current_floor_ptr->grid_array[p_ptr->y][p_ptr->x];
1150
1151                                         if (!feat_uses_special(g_ptr->feat))
1152                                         {
1153                                                 if (floor_mode & (CFM_DOWN | CFM_UP))
1154                                                 {
1155                                                         /* Reset to floor */
1156                                                         g_ptr->feat = feat_ground_type[randint0(100)];
1157                                                 }
1158
1159                                                 g_ptr->special = 0;
1160                                         }
1161                                 }
1162                         }
1163                 }
1164
1165                 /*
1166                  * Set lower/upper_floor_id of new floor when the new
1167                  * floor is right-above/right-under the current floor.
1168                  *
1169                  * Stair creation/Teleport level/Trap door will take
1170                  * you the same floor when you used it later again.
1171                  */
1172                 if (p_ptr->floor_id)
1173                 {
1174                         saved_floor_type *cur_sf_ptr = get_sf_ptr(p_ptr->floor_id);
1175
1176                         if (floor_mode & CFM_UP)
1177                         {
1178                                 /* New floor is right-above */
1179                                 if (cur_sf_ptr->upper_floor_id == new_floor_id)
1180                                         sf_ptr->lower_floor_id = p_ptr->floor_id;
1181                         }
1182                         else if (floor_mode & CFM_DOWN)
1183                         {
1184                                 /* New floor is right-under */
1185                                 if (cur_sf_ptr->lower_floor_id == new_floor_id)
1186                                         sf_ptr->upper_floor_id = p_ptr->floor_id;
1187                         }
1188                 }
1189
1190                 /* Break connection to killed floor */
1191                 else
1192                 {
1193                         if (floor_mode & CFM_UP)
1194                                 sf_ptr->lower_floor_id = 0;
1195                         else if (floor_mode & CFM_DOWN)
1196                                 sf_ptr->upper_floor_id = 0;
1197                 }
1198
1199                 /* Maintain monsters and artifacts */
1200                 if (loaded)
1201                 {
1202                         MONSTER_IDX i;
1203                         GAME_TURN tmp_last_visit = sf_ptr->last_visit;
1204                         GAME_TURN absence_ticks;
1205                         int alloc_chance = d_info[p_ptr->dungeon_idx].max_m_alloc_chance;
1206                         GAME_TURN alloc_times;
1207
1208                         while (tmp_last_visit > current_world_ptr->game_turn) tmp_last_visit -= TURNS_PER_TICK * TOWN_DAWN;
1209                         absence_ticks = (current_world_ptr->game_turn - tmp_last_visit) / TURNS_PER_TICK;
1210
1211                         /* Maintain monsters */
1212                         for (i = 1; i < current_floor_ptr->m_max; i++)
1213                         {
1214                                 monster_race *r_ptr;
1215                                 monster_type *m_ptr = &current_floor_ptr->m_list[i];
1216
1217                                 if (!monster_is_valid(m_ptr)) continue;
1218
1219                                 if (!is_pet(m_ptr))
1220                                 {
1221                                         /* Restore HP */
1222                                         m_ptr->hp = m_ptr->maxhp = m_ptr->max_maxhp;
1223
1224                                         /* Remove timed status (except MTIMED_CSLEEP) */
1225                                         (void)set_monster_fast(i, 0);
1226                                         (void)set_monster_slow(i, 0);
1227                                         (void)set_monster_stunned(i, 0);
1228                                         (void)set_monster_confused(i, 0);
1229                                         (void)set_monster_monfear(i, 0);
1230                                         (void)set_monster_invulner(i, 0, FALSE);
1231                                 }
1232
1233                                 /* Extract real monster race */
1234                                 r_ptr = real_r_ptr(m_ptr);
1235
1236                                 /* Ignore non-unique */
1237                                 if (!(r_ptr->flags1 & RF1_UNIQUE) &&
1238                                     !(r_ptr->flags7 & RF7_NAZGUL)) continue;
1239
1240                                 /* Appear at a different floor? */
1241                                 if (r_ptr->floor_id != new_floor_id)
1242                                 {
1243                                         /* Disapper from here */
1244                                         delete_monster_idx(i);
1245                                 }
1246                         }
1247
1248                         /* Maintain artifatcs */
1249                         for (i = 1; i < current_floor_ptr->o_max; i++)
1250                         {
1251                                 object_type *o_ptr = &current_floor_ptr->o_list[i];
1252
1253                                 if (!OBJECT_IS_VALID(o_ptr)) continue;
1254
1255                                 /* Ignore non-artifact */
1256                                 if (!object_is_fixed_artifact(o_ptr)) continue;
1257
1258                                 /* Appear at a different floor? */
1259                                 if (a_info[o_ptr->name1].floor_id != new_floor_id)
1260                                 {
1261                                         /* Disappear from here */
1262                                         delete_object_idx(i);
1263                                 }
1264                                 else
1265                                 {
1266                                         /* Cancel preserve */
1267                                         a_info[o_ptr->name1].cur_num = 1;
1268                                 }
1269                         }
1270
1271                         (void)place_quest_monsters();
1272
1273                         /* Place some random monsters */
1274                         alloc_times = absence_ticks / alloc_chance;
1275
1276                         if (randint0(alloc_chance) < (absence_ticks % alloc_chance))
1277                                 alloc_times++;
1278
1279                         for (i = 0; i < alloc_times; i++)
1280                         {
1281                                 /* Make a (group of) new monster */
1282                                 (void)alloc_monster(0, 0);
1283                         }
1284
1285                 }
1286
1287                 /* New floor_id or failed to restore */
1288                 else /* if (!loaded) */
1289                 {
1290                         if (sf_ptr->last_visit)
1291                         {
1292                                 /* Temporal file is broken? */
1293                                 msg_print(_("階段は行き止まりだった。", "The staircases come to a dead end..."));
1294
1295                                 /* Create simple dead end */
1296                                 build_dead_end(current_floor_ptr);
1297
1298                                 /* Break connection */
1299                                 if (floor_mode & CFM_UP)
1300                                 {
1301                                         sf_ptr->upper_floor_id = 0;
1302                                 }
1303                                 else if (floor_mode & CFM_DOWN)
1304                                 {
1305                                         sf_ptr->lower_floor_id = 0;
1306                                 }
1307                         }
1308                         else
1309                         {
1310                                 /* Newly create current_floor_ptr->grid_array */
1311                                 generate_random_floor(current_floor_ptr);
1312                         }
1313
1314                         /* Record last visit current_world_ptr->game_turn */
1315                         sf_ptr->last_visit = current_world_ptr->game_turn;
1316
1317                         /* Set correct current_floor_ptr->dun_level value */
1318                         sf_ptr->dun_level = current_floor_ptr->dun_level;
1319
1320                         /* Create connected stairs */
1321                         if (!(floor_mode & CFM_NO_RETURN))
1322                         {
1323                                 /* Extract stair position */
1324                                 grid_type *g_ptr = &current_floor_ptr->grid_array[p_ptr->y][p_ptr->x];
1325
1326                                 /*** Create connected stairs ***/
1327
1328                                 /* No stairs down from Quest */
1329                                 if ((floor_mode & CFM_UP) && !quest_number(current_floor_ptr->dun_level))
1330                                 {
1331                                         g_ptr->feat = (floor_mode & CFM_SHAFT) ? feat_state(feat_down_stair, FF_SHAFT) : feat_down_stair;
1332                                 }
1333
1334                                 /* No stairs up when ironman_downward */
1335                                 else if ((floor_mode & CFM_DOWN) && !ironman_downward)
1336                                 {
1337                                         g_ptr->feat = (floor_mode & CFM_SHAFT) ? feat_state(feat_up_stair, FF_SHAFT) : feat_up_stair;
1338                                 }
1339
1340                                 /* Paranoia -- Clear mimic */
1341                                 g_ptr->mimic = 0;
1342
1343                                 /* Connect to previous floor */
1344                                 g_ptr->special = p_ptr->floor_id;
1345                         }
1346                 }
1347
1348                 /* Arrive at random grid */
1349                 if (floor_mode & (CFM_RAND_PLACE))
1350                 {
1351                         (void)new_player_spot();
1352                 }
1353
1354                 /* You see stairs blocked */
1355                 else if ((floor_mode & CFM_NO_RETURN) && (floor_mode & (CFM_DOWN | CFM_UP)))
1356                 {
1357                         if (!p_ptr->blind)
1358                         {
1359                                 msg_print(_("突然階段が塞がれてしまった。", "Suddenly the stairs is blocked!"));
1360                         }
1361                         else
1362                         {
1363                                 msg_print(_("ゴトゴトと何か音がした。", "You hear some noises."));
1364                         }
1365                 }
1366
1367                 /*
1368                  * Update visit mark
1369                  *
1370                  * The "current_world_ptr->game_turn" is not always different number because
1371                  * the level teleport doesn't take any current_world_ptr->game_turn.  Use
1372                  * visit mark instead of last visit current_world_ptr->game_turn to find the
1373                  * oldest saved floor.
1374                  */
1375                 sf_ptr->visit_mark = latest_visit_mark++;
1376         }
1377
1378         /* Place preserved pet monsters */
1379         place_pet(p_ptr);
1380
1381         /* Reset travel target place */
1382         forget_travel_flow();
1383
1384         /* Hack -- maintain unique and artifacts */
1385         update_unique_artifact(new_floor_id);
1386
1387         /* Now the player is in new floor */
1388         p_ptr->floor_id = new_floor_id;
1389
1390         /* The dungeon is ready */
1391         current_world_ptr->character_dungeon = TRUE;
1392
1393         /* Hack -- Munchkin characters always get whole map */
1394         if (p_ptr->pseikaku == SEIKAKU_MUNCHKIN)
1395                 wiz_lite(p_ptr, (bool)(p_ptr->pclass == CLASS_NINJA));
1396
1397         /* Remember when this level was "created" */
1398         current_floor_ptr->generated_turn = current_world_ptr->game_turn;
1399
1400         /* No dungeon feeling yet */
1401         p_ptr->feeling_turn = current_floor_ptr->generated_turn;
1402         p_ptr->feeling = 0;
1403
1404         /* Clear all flags */
1405         floor_mode = 0L;
1406
1407         select_floor_music(p_ptr);
1408         p_ptr->change_floor_mode = 0;
1409 }