OSDN Git Service

[Refactor] #37353 ウィンドウ表示変数と関連マクロを view-mainwindow.c/h へ移動.
[hengband/hengband.git] / src / floor-save.c
1 /*!
2  * @file floors.c
3  * @brief 保存された階の管理 / management of the saved floor
4  * @date 2014/01/04
5  * @author
6  * Copyright (c) 2002  Mogami \n
7  * This software may be copied and distributed for educational, research, and \n
8  * not for profit purposes provided that this copyright and statement are \n
9  * included in all such copies. \n
10  * 2014 Deskull rearranged comment for Doxygen. \n
11  */
12
13 #include "angband.h"
14 #include "bldg.h"
15 #include "load.h"
16 #include "util.h"
17
18 #include "artifact.h"
19 #include "dungeon.h"
20 #include "floor.h"
21 #include "floor-events.h"
22 #include "floor-generate.h"
23 #include "feature.h"
24 #include "grid.h"
25 #include "monster.h"
26 #include "quest.h"
27 #include "wild.h"
28 #include "spells-floor.h"
29 #include "monster-status.h"
30 #include "object-hook.h"
31 #include "cmd-pet.h"
32 #include "cmd-basic.h"
33 #include "files.h"
34 #include "player-effects.h"
35 #include "world.h"
36 #include "spells.h"
37 #include "cmd-dump.h"
38 #include "save.h"
39
40 #include "view-mainwindow.h"
41
42
43 static FLOOR_IDX new_floor_id;  /*!<次のフロアのID / floor_id of the destination */
44 static u32b change_floor_mode;  /*!<フロア移行処理に関するフラグ / Mode flags for changing floor */
45 static u32b latest_visit_mark;  /*!<フロアを渡った回数?(確認中) / Max number of visit_mark */
46
47 /*
48  * Number of floor_id used from birth
49  */
50 FLOOR_IDX max_floor_id;
51
52 /*
53  * Sign for current process used in temporal files.
54  * Actually it is the start time of current process.
55  */
56 u32b saved_floor_file_sign;
57
58 /*!
59  * @brief 保存フロア配列を初期化する / Initialize saved_floors array. 
60  * @param force テンポラリファイルが残っていた場合も警告なしで強制的に削除する。
61  * @details Make sure that old temporal files are not remaining as gurbages.
62  * @return なし
63  */
64 void init_saved_floors(bool force)
65 {
66         char floor_savefile[1024];
67         int i;
68         int fd = -1;
69         BIT_FLAGS mode = 0644;
70
71 #ifdef SET_UID
72 # ifdef SECURE
73         /* Get "games" permissions */
74         beGames();
75 # endif
76 #endif
77
78         for (i = 0; i < MAX_SAVED_FLOORS; i++)
79         {
80                 saved_floor_type *sf_ptr = &saved_floors[i];
81
82                 /* File name */
83                 sprintf(floor_savefile, "%s.F%02d", savefile, i);
84
85                 /* Grab permissions */
86                 safe_setuid_grab();
87
88                 /* Try to create the file */
89                 fd = fd_make(floor_savefile, mode);
90
91                 /* Drop permissions */
92                 safe_setuid_drop();
93
94                 /* Failed! */
95                 if (fd < 0)
96                 {
97                         if (!force)
98                         {
99                                 msg_print(_("エラー:古いテンポラリ・ファイルが残っています。", "Error: There are old temporal files."));
100                                 msg_print(_("変愚蛮怒を二重に起動していないか確認してください。", "Make sure you are not running two game processes simultaneously."));
101                                 msg_print(_("過去に変愚蛮怒がクラッシュした場合は一時ファイルを", "If the temporal files are garbages of old crashed process, "));
102                                 msg_print(_("強制的に削除して実行を続けられます。", "you can delete it safely."));
103                                 if (!get_check(_("強制的に削除してもよろしいですか?", "Do you delete old temporal files? ")))
104                                         quit(_("実行中止", "Aborted."));
105                                 force = TRUE;
106                         }
107                 }
108                 else
109                 {
110                         /* Close the "fd" */
111                         (void)fd_close(fd);
112                 }
113
114                 /* Grab permissions */
115                 safe_setuid_grab();
116
117                 /* Simply kill the temporal file */ 
118                 (void)fd_kill(floor_savefile);
119
120                 /* Drop permissions */
121                 safe_setuid_drop();
122
123                 sf_ptr->floor_id = 0;
124         }
125
126         /* No floor_id used yet (No.0 is reserved to indicate non existance) */
127         max_floor_id = 1;
128
129         /* vist_mark is from 1 */
130         latest_visit_mark = 1;
131
132         /* A sign to mark temporal files */
133         saved_floor_file_sign = (u32b)time(NULL);
134
135         /* No next floor yet */
136         new_floor_id = 0;
137
138         /* No change floor mode yet */
139         change_floor_mode = 0;
140
141 #ifdef SET_UID
142 # ifdef SECURE
143         /* Drop "games" permissions */
144         bePlayer();
145 # endif
146 #endif
147 }
148
149 /*!
150  * @brief 保存フロア用テンポラリファイルを削除する / Kill temporal files
151  * @details Should be called just before the game quit.
152  * @return なし
153  */
154 void clear_saved_floor_files(void)
155 {
156         char floor_savefile[1024];
157         int i;
158
159 #ifdef SET_UID
160 # ifdef SECURE
161         /* Get "games" permissions */
162         beGames();
163 # endif
164 #endif
165
166         for (i = 0; i < MAX_SAVED_FLOORS; i++)
167         {
168                 saved_floor_type *sf_ptr = &saved_floors[i];
169
170                 /* No temporal file */
171                 if (!sf_ptr->floor_id) continue;
172                 if (sf_ptr->floor_id == p_ptr->floor_id) continue;
173
174                 /* File name */
175                 sprintf(floor_savefile, "%s.F%02d", savefile, i);
176
177                 /* Grab permissions */
178                 safe_setuid_grab();
179
180                 /* Simply kill the temporal file */ 
181                 (void)fd_kill(floor_savefile);
182
183                 /* Drop permissions */
184                 safe_setuid_drop();
185         }
186
187 #ifdef SET_UID
188 # ifdef SECURE
189         /* Drop "games" permissions */
190         bePlayer();
191 # endif
192 #endif
193 }
194
195 /*!
196  * @brief 保存フロアIDから参照ポインタを得る / Get a pointer for an item of the saved_floors array.
197  * @param floor_id 保存フロアID
198  * @return IDに対応する保存フロアのポインタ、ない場合はNULLを返す。
199  */
200 saved_floor_type *get_sf_ptr(FLOOR_IDX floor_id)
201 {
202         int i;
203
204         /* floor_id No.0 indicates no floor */
205         if (!floor_id) return NULL;
206
207         for (i = 0; i < MAX_SAVED_FLOORS; i++)
208         {
209                 saved_floor_type *sf_ptr = &saved_floors[i];
210
211                 if (sf_ptr->floor_id == floor_id) return sf_ptr;
212         }
213
214         /* None found */
215         return NULL;
216 }
217
218
219 /*!
220  * @brief 参照ポインタ先の保存フロアを抹消する / kill a saved floor and get an empty space
221  * @param sf_ptr 保存フロアの参照ポインタ
222  * @return なし
223  */
224 static void kill_saved_floor(saved_floor_type *sf_ptr)
225 {
226         char floor_savefile[1024];
227         if (!sf_ptr) return;
228
229         /* Already empty */
230         if (!sf_ptr->floor_id) return;
231
232         if (sf_ptr->floor_id == p_ptr->floor_id)
233         {
234                 /* Kill current floor */
235                 p_ptr->floor_id = 0;
236
237                 /* Current floor doesn't have temporal file */
238         }
239         else 
240         {
241                 /* File name */
242                 sprintf(floor_savefile, "%s.F%02d", savefile, (int)sf_ptr->savefile_id);
243
244                 /* Grab permissions */
245                 safe_setuid_grab();
246
247                 /* Simply kill the temporal file */ 
248                 (void)fd_kill(floor_savefile);
249
250                 /* Drop permissions */
251                 safe_setuid_drop();
252         }
253
254         /* No longer exists */
255         sf_ptr->floor_id = 0;
256 }
257
258
259 /*!
260  * @brief 新規に利用可能な保存フロアを返す / Initialize new saved floor and get its floor id.
261  * @return 利用可能な保存フロアID
262  * @details
263  * If number of saved floors are already MAX_SAVED_FLOORS, kill the oldest one.
264  */
265 FLOOR_IDX get_new_floor_id(void)
266 {
267         saved_floor_type *sf_ptr = NULL;
268         FLOOR_IDX i;
269
270         /* Look for empty space */
271         for (i = 0; i < MAX_SAVED_FLOORS; i++)
272         {
273                 sf_ptr = &saved_floors[i];
274
275                 if (!sf_ptr->floor_id) break;
276         }
277
278         /* None found */
279         if (i == MAX_SAVED_FLOORS)
280         {
281                 s16b oldest = 0;
282                 u32b oldest_visit = 0xffffffffL;
283
284                 /* Search for oldest */
285                 for (i = 0; i < MAX_SAVED_FLOORS; i++)
286                 {
287                         sf_ptr = &saved_floors[i];
288
289                         /* Don't kill current floor */
290                         if (sf_ptr->floor_id == p_ptr->floor_id) continue;
291
292                         /* Don't kill newer */
293                         if (sf_ptr->visit_mark > oldest_visit) continue;
294
295                         oldest = i;
296                         oldest_visit = sf_ptr->visit_mark;
297                 }
298
299                 /* Kill oldest saved floor */
300                 sf_ptr = &saved_floors[oldest];
301                 kill_saved_floor(sf_ptr);
302
303                 /* Use it */
304                 i = oldest;
305         }
306
307         /* Prepare new floor data */
308         sf_ptr->savefile_id = i;
309         sf_ptr->floor_id = max_floor_id;
310         sf_ptr->last_visit = 0;
311         sf_ptr->upper_floor_id = 0;
312         sf_ptr->lower_floor_id = 0;
313         sf_ptr->visit_mark = latest_visit_mark++;
314
315         /* sf_ptr->dun_level may be changed later */
316         sf_ptr->dun_level = current_floor_ptr->dun_level;
317
318
319         /* Increment number of floor_id */
320         if (max_floor_id < MAX_SHORT) max_floor_id++;
321
322         /* 32767 floor_ids are all used up!  Re-use ancient IDs */
323         else max_floor_id = 1;
324
325         return sf_ptr->floor_id;
326 }
327
328
329 /*!
330  * @brief フロア切り替え時の処理フラグを追加する / Prepare mode flags of changing floor
331  * @param mode 追加したい所持フラグ
332  * @return なし
333  */
334 void prepare_change_floor_mode(BIT_FLAGS mode)
335 {
336         change_floor_mode |= mode;
337 }
338
339 /*!
340  * @brief 階段移動先のフロアが生成できない時に簡単な行き止まりマップを作成する / Builds the dead end
341  * @return なし
342  */
343 static void build_dead_end(void)
344 {
345         POSITION x, y;
346
347         /* Clear and empty the current_floor_ptr->grid_array */
348         clear_cave();
349
350         /* Fill the arrays of floors and walls in the good proportions */
351         set_floor_and_wall(0);
352
353         /* Smallest area */
354         current_floor_ptr->height = SCREEN_HGT;
355         current_floor_ptr->width = SCREEN_WID;
356
357         /* Filled with permanent walls */
358         for (y = 0; y < MAX_HGT; y++)
359         {
360                 for (x = 0; x < MAX_WID; x++)
361                 {
362                         /* Create "solid" perma-wall */
363                         place_solid_perm_bold(y, x);
364                 }
365         }
366
367         /* Place at center of the floor */
368         p_ptr->y = current_floor_ptr->height / 2;
369         p_ptr->x = current_floor_ptr->width / 2;
370
371         /* Give one square */
372         place_floor_bold(p_ptr->y, p_ptr->x);
373
374         wipe_generate_random_floor_flags();
375 }
376
377
378
379 #define MAX_PARTY_MON 21 /*!< フロア移動時に先のフロアに連れて行けるペットの最大数 Maximum number of preservable pets */
380 static monster_type party_mon[MAX_PARTY_MON]; /*!< フロア移動に保存するペットモンスターの配列 */
381
382 /*!
383  * @brief フロア移動時のペット保存処理 / Preserve_pets
384  * @return なし
385  */
386 static void preserve_pet(void)
387 {
388         int num;
389         MONSTER_IDX i;
390
391         for (num = 0; num < MAX_PARTY_MON; num++)
392         {
393                 party_mon[num].r_idx = 0;
394         }
395
396         if (p_ptr->riding)
397         {
398                 monster_type *m_ptr = &current_floor_ptr->m_list[p_ptr->riding];
399
400                 /* Pet of other pet don't follow. */
401                 if (m_ptr->parent_m_idx)
402                 {
403                         p_ptr->riding = 0;
404                         p_ptr->pet_extra_flags &= ~(PF_RYOUTE);
405                         p_ptr->riding_ryoute = p_ptr->old_riding_ryoute = FALSE;
406                 }
407                 else
408                 {
409                         /* Preserve the mount */
410                         (void)COPY(&party_mon[0], m_ptr, monster_type);
411
412                         /* Delete from this floor */
413                         delete_monster_idx(p_ptr->riding);
414                 }
415         }
416
417         /*
418          * If player is in wild mode, no pets are preserved
419          * except a monster whom player riding
420          */
421         if (!p_ptr->wild_mode && !p_ptr->inside_arena && !p_ptr->inside_battle)
422         {
423                 for (i = current_floor_ptr->m_max - 1, num = 1; (i >= 1 && num < MAX_PARTY_MON); i--)
424                 {
425                         monster_type *m_ptr = &current_floor_ptr->m_list[i];
426
427                         if (!monster_is_valid(m_ptr)) continue;
428                         if (!is_pet(m_ptr)) continue;
429                         if (i == p_ptr->riding) continue;
430
431                         if (reinit_wilderness)
432                         {
433                                 /* Don't lose sight of pets when getting a Quest */
434                         }
435                         else
436                         {
437                                 POSITION dis = distance(p_ptr->y, p_ptr->x, m_ptr->fy, m_ptr->fx);
438
439                                 /* Confused (etc.) monsters don't follow. */
440                                 if (MON_CONFUSED(m_ptr) || MON_STUNNED(m_ptr) || MON_CSLEEP(m_ptr)) continue;
441
442                                 /* Pet of other pet don't follow. */
443                                 if (m_ptr->parent_m_idx) continue;
444
445                                 /*
446                                  * Pets with nickname will follow even from 3 blocks away
447                                  * when you or the pet can see the other.
448                                  */
449                                 if (m_ptr->nickname && 
450                                     ((player_has_los_bold(m_ptr->fy, m_ptr->fx) && projectable(p_ptr->y, p_ptr->x, m_ptr->fy, m_ptr->fx)) ||
451                                      (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))))
452                                 {
453                                         if (dis > 3) continue;
454                                 }
455                                 else
456                                 {
457                                         if (dis > 1) continue;
458                                 }
459                         }
460
461                         (void)COPY(&party_mon[num], &current_floor_ptr->m_list[i], monster_type);
462
463                         num++;
464
465                         /* Delete from this floor */
466                         delete_monster_idx(i);
467                 }
468         }
469
470         if (record_named_pet)
471         {
472                 for (i = current_floor_ptr->m_max - 1; i >=1; i--)
473                 {
474                         monster_type *m_ptr = &current_floor_ptr->m_list[i];
475                         GAME_TEXT m_name[MAX_NLEN];
476
477                         if (!monster_is_valid(m_ptr)) continue;
478                         if (!is_pet(m_ptr)) continue;
479                         if (!m_ptr->nickname) continue;
480                         if (p_ptr->riding == i) continue;
481
482                         monster_desc(m_name, m_ptr, MD_ASSUME_VISIBLE | MD_INDEF_VISIBLE);
483                         do_cmd_write_nikki(NIKKI_NAMED_PET, RECORD_NAMED_PET_MOVED, m_name);
484                 }
485         }
486
487
488         /* Pet of other pet may disappear. */
489         for (i = current_floor_ptr->m_max - 1; i >=1; i--)
490         {
491                 monster_type *m_ptr = &current_floor_ptr->m_list[i];
492
493                 /* Are there its parent? */
494                 if (m_ptr->parent_m_idx && !current_floor_ptr->m_list[m_ptr->parent_m_idx].r_idx)
495                 {
496                         /* Its parent have gone, it also goes away. */
497
498                         if (is_seen(m_ptr))
499                         {
500                                 GAME_TEXT m_name[MAX_NLEN];
501                                 monster_desc(m_name, m_ptr, 0);
502                                 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
503                         }
504
505                         delete_monster_idx(i);
506                 }
507         }
508 }
509
510
511 /*!
512  * @brief フロア移動時にペットを伴った場合の準備処理 / Pre-calculate the racial counters of preserved pets
513  * @return なし
514  * @details
515  * To prevent multiple generation of unique monster who is the minion of player
516  */
517 void precalc_cur_num_of_pet(void)
518 {
519         monster_type *m_ptr;
520         int i;
521         int max_num = p_ptr->wild_mode ? 1 : MAX_PARTY_MON;
522
523         for (i = 0; i < max_num; i++)
524         {
525                 m_ptr = &party_mon[i];
526
527                 /* Skip empty monsters */
528                 if (!monster_is_valid(m_ptr)) continue;
529
530                 /* Hack -- Increase the racial counter */
531                 real_r_ptr(m_ptr)->cur_num++;
532         }
533 }
534
535 /*!
536  * @brief 移動先のフロアに伴ったペットを配置する / Place preserved pet monsters on new floor
537  * @return なし
538  */
539 static void place_pet(void)
540 {
541         int i;
542         int max_num = p_ptr->wild_mode ? 1 : MAX_PARTY_MON;
543
544         for (i = 0; i < max_num; i++)
545         {
546                 POSITION cy = 0, cx = 0;
547                 MONSTER_IDX m_idx;
548
549                 if (!(party_mon[i].r_idx)) continue;
550
551                 if (i == 0)
552                 {
553                         m_idx = m_pop();
554                         p_ptr->riding = m_idx;
555                         if (m_idx)
556                         {
557                                 cy = p_ptr->y;
558                                 cx = p_ptr->x;
559                         }
560                 }
561                 else
562                 {
563                         int j;
564                         POSITION d;
565
566                         for (d = 1; d < A_MAX; d++)
567                         {
568                                 for (j = 1000; j > 0; j--)
569                                 {
570                                         scatter(&cy, &cx, p_ptr->y, p_ptr->x, d, 0);
571                                         if (monster_can_enter(cy, cx, &r_info[party_mon[i].r_idx], 0)) break;
572                                 }
573                                 if (j) break;
574                         }
575                         m_idx = (d == 6) ? 0 : m_pop();
576                 }
577
578                 if (m_idx)
579                 {
580                         monster_type *m_ptr = &current_floor_ptr->m_list[m_idx];
581                         monster_race *r_ptr;
582
583                         current_floor_ptr->grid_array[cy][cx].m_idx = m_idx;
584
585                         m_ptr->r_idx = party_mon[i].r_idx;
586
587                         /* Copy all member of the structure */
588                         *m_ptr = party_mon[i];
589                         r_ptr = real_r_ptr(m_ptr);
590
591                         m_ptr->fy = cy;
592                         m_ptr->fx = cx;
593                         m_ptr->ml = TRUE;
594                         m_ptr->mtimed[MTIMED_CSLEEP] = 0;
595                         m_ptr->hold_o_idx = 0;
596                         m_ptr->target_y = 0;
597
598                         if ((r_ptr->flags1 & RF1_FORCE_SLEEP) && !ironman_nightmare)
599                         {
600                                 /* Monster is still being nice */
601                                 m_ptr->mflag |= (MFLAG_NICE);
602
603                                 /* Must repair monsters */
604                                 repair_monsters = TRUE;
605                         }
606                         update_monster(m_idx, TRUE);
607                         lite_spot(cy, cx);
608
609                         /* Pre-calculated in precalc_cur_num_of_pet() */
610                         /* r_ptr->cur_num++; */
611
612                         /* Hack -- Count the number of "reproducers" */
613                         if (r_ptr->flags2 & RF2_MULTIPLY) current_floor_ptr->num_repro++;
614
615                 }
616                 else
617                 {
618                         monster_type *m_ptr = &party_mon[i];
619                         monster_race *r_ptr = real_r_ptr(m_ptr);
620                         GAME_TEXT m_name[MAX_NLEN];
621
622                         monster_desc(m_name, m_ptr, 0);
623                         msg_format(_("%sとはぐれてしまった。", "You have lost sight of %s."), m_name);
624                         if (record_named_pet && m_ptr->nickname)
625                         {
626                                 monster_desc(m_name, m_ptr, MD_INDEF_VISIBLE);
627                                 do_cmd_write_nikki(NIKKI_NAMED_PET, RECORD_NAMED_PET_LOST_SIGHT, m_name);
628                         }
629
630                         /* Pre-calculated in precalc_cur_num_of_pet(), but need to decrease */
631                         if (r_ptr->cur_num) r_ptr->cur_num--;
632                 }
633         }
634
635         /* For accuracy of precalc_cur_num_of_pet() */
636         (void)C_WIPE(party_mon, MAX_PARTY_MON, monster_type);
637 }
638
639
640 /*!
641  * @brief ユニークモンスターやアーティファクトの所在フロアを更新する / Hack -- Update location of unique monsters and artifacts
642  * @param cur_floor_id 現在のフロアID
643  * @return なし
644  * @details 
645  * The r_ptr->floor_id and a_ptr->floor_id are not updated correctly\n
646  * while new floor creation since dungeons may be re-created by\n
647  * auto-scum option.\n
648  */
649 static void update_unique_artifact(s16b cur_floor_id)
650 {
651         int i;
652
653         /* Maintain unique monsters */
654         for (i = 1; i < current_floor_ptr->m_max; i++)
655         {
656                 monster_race *r_ptr;
657                 monster_type *m_ptr = &current_floor_ptr->m_list[i];
658
659                 if (!monster_is_valid(m_ptr)) continue;
660
661                 /* Extract real monster race */
662                 r_ptr = real_r_ptr(m_ptr);
663
664                 /* Memorize location of the unique monster */
665                 if ((r_ptr->flags1 & RF1_UNIQUE) ||
666                     (r_ptr->flags7 & RF7_NAZGUL))
667                 {
668                         r_ptr->floor_id = cur_floor_id;
669                 }
670         }
671
672         /* Maintain artifatcs */
673         for (i = 1; i < current_floor_ptr->o_max; i++)
674         {
675                 object_type *o_ptr = &current_floor_ptr->o_list[i];
676
677                 /* Skip dead objects */
678                 if (!o_ptr->k_idx) 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)
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 (change_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 (change_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(void)
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 (p_ptr->special_defense & NINJA_S_STEALTH) set_superstealth(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 (!p_ptr->floor_id &&
882             (change_floor_mode & CFM_SAVE_FLOORS) &&
883             !(change_floor_mode & CFM_NO_RETURN))
884         {
885             /* Get temporal floor_id */
886             p_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                     (p_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 = &p_ptr->inventory_list[i];
929
930                 /* Skip dead objects */
931                 if (!o_ptr->k_idx) 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 ((change_floor_mode & CFM_RAND_CONNECT) && p_ptr->floor_id)
945         {
946                 locate_connected_stairs(sf_ptr);
947         }
948
949         /* Extract new dungeon level */
950         if (change_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 (change_floor_mode & (CFM_DOWN | CFM_UP))
972         {
973                 int move_num = 0;
974
975                 /* Extract level movement number */
976                 if (change_floor_mode & CFM_DOWN) move_num = 1;
977                 else if (change_floor_mode & CFM_UP) move_num = -1;
978
979                 /* Shafts are deeper than normal stairs */
980                 if (change_floor_mode & CFM_SHAFT)
981                         move_num += SGN(move_num);
982
983                 /* Get out from or Enter the dungeon */
984                 if (change_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 (change_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                 change_floor_mode &= ~CFM_SAVE_FLOORS;
1012         }
1013
1014         /* Kill some old saved floors */
1015         if (!(change_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 (change_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 (change_floor_mode & CFM_RAND_CONNECT)
1053         {
1054                 if (change_floor_mode & CFM_UP)
1055                         sf_ptr->upper_floor_id = new_floor_id;
1056                 else if (change_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 ((change_floor_mode & CFM_SAVE_FLOORS) &&
1062             !(change_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(void)
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 (!(change_floor_mode & CFM_SAVE_FLOORS) &&
1117             !(change_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 (change_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 (change_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 (change_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 (change_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 (change_floor_mode & CFM_UP)
1195                                 sf_ptr->lower_floor_id = 0;
1196                         else if (change_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                                 /* Skip dead objects */
1255                                 if (!o_ptr->k_idx) continue;
1256
1257                                 /* Ignore non-artifact */
1258                                 if (!object_is_fixed_artifact(o_ptr)) continue;
1259
1260                                 /* Appear at a different floor? */
1261                                 if (a_info[o_ptr->name1].floor_id != new_floor_id)
1262                                 {
1263                                         /* Disappear from here */
1264                                         delete_object_idx(i);
1265                                 }
1266                                 else
1267                                 {
1268                                         /* Cancel preserve */
1269                                         a_info[o_ptr->name1].cur_num = 1;
1270                                 }
1271                         }
1272
1273                         (void)place_quest_monsters();
1274
1275                         /* Place some random monsters */
1276                         alloc_times = absence_ticks / alloc_chance;
1277
1278                         if (randint0(alloc_chance) < (absence_ticks % alloc_chance))
1279                                 alloc_times++;
1280
1281                         for (i = 0; i < alloc_times; i++)
1282                         {
1283                                 /* Make a (group of) new monster */
1284                                 (void)alloc_monster(0, 0);
1285                         }
1286
1287                 }
1288
1289                 /* New floor_id or failed to restore */
1290                 else /* if (!loaded) */
1291                 {
1292                         if (sf_ptr->last_visit)
1293                         {
1294                                 /* Temporal file is broken? */
1295                                 msg_print(_("階段は行き止まりだった。", "The staircases come to a dead end..."));
1296
1297                                 /* Create simple dead end */
1298                                 build_dead_end();
1299
1300                                 /* Break connection */
1301                                 if (change_floor_mode & CFM_UP)
1302                                 {
1303                                         sf_ptr->upper_floor_id = 0;
1304                                 }
1305                                 else if (change_floor_mode & CFM_DOWN)
1306                                 {
1307                                         sf_ptr->lower_floor_id = 0;
1308                                 }
1309                         }
1310                         else
1311                         {
1312                                 /* Newly create current_floor_ptr->grid_array */
1313                                 generate_random_floor();
1314                         }
1315
1316                         /* Record last visit current_world_ptr->game_turn */
1317                         sf_ptr->last_visit = current_world_ptr->game_turn;
1318
1319                         /* Set correct current_floor_ptr->dun_level value */
1320                         sf_ptr->dun_level = current_floor_ptr->dun_level;
1321
1322                         /* Create connected stairs */
1323                         if (!(change_floor_mode & CFM_NO_RETURN))
1324                         {
1325                                 /* Extract stair position */
1326                                 grid_type *g_ptr = &current_floor_ptr->grid_array[p_ptr->y][p_ptr->x];
1327
1328                                 /*** Create connected stairs ***/
1329
1330                                 /* No stairs down from Quest */
1331                                 if ((change_floor_mode & CFM_UP) && !quest_number(current_floor_ptr->dun_level))
1332                                 {
1333                                         g_ptr->feat = (change_floor_mode & CFM_SHAFT) ? feat_state(feat_down_stair, FF_SHAFT) : feat_down_stair;
1334                                 }
1335
1336                                 /* No stairs up when ironman_downward */
1337                                 else if ((change_floor_mode & CFM_DOWN) && !ironman_downward)
1338                                 {
1339                                         g_ptr->feat = (change_floor_mode & CFM_SHAFT) ? feat_state(feat_up_stair, FF_SHAFT) : feat_up_stair;
1340                                 }
1341
1342                                 /* Paranoia -- Clear mimic */
1343                                 g_ptr->mimic = 0;
1344
1345                                 /* Connect to previous floor */
1346                                 g_ptr->special = p_ptr->floor_id;
1347                         }
1348                 }
1349
1350                 /* Arrive at random grid */
1351                 if (change_floor_mode & (CFM_RAND_PLACE))
1352                 {
1353                         (void)new_player_spot();
1354                 }
1355
1356                 /* You see stairs blocked */
1357                 else if ((change_floor_mode & CFM_NO_RETURN) && (change_floor_mode & (CFM_DOWN | CFM_UP)))
1358                 {
1359                         if (!p_ptr->blind)
1360                         {
1361                                 msg_print(_("突然階段が塞がれてしまった。", "Suddenly the stairs is blocked!"));
1362                         }
1363                         else
1364                         {
1365                                 msg_print(_("ゴトゴトと何か音がした。", "You hear some noises."));
1366                         }
1367                 }
1368
1369                 /*
1370                  * Update visit mark
1371                  *
1372                  * The "current_world_ptr->game_turn" is not always different number because
1373                  * the level teleport doesn't take any current_world_ptr->game_turn.  Use
1374                  * visit mark instead of last visit current_world_ptr->game_turn to find the
1375                  * oldest saved floor.
1376                  */
1377                 sf_ptr->visit_mark = latest_visit_mark++;
1378         }
1379
1380         /* Place preserved pet monsters */
1381         place_pet();
1382
1383         /* Reset travel target place */
1384         forget_travel_flow();
1385
1386         /* Hack -- maintain unique and artifacts */
1387         update_unique_artifact(new_floor_id);
1388
1389         /* Now the player is in new floor */
1390         p_ptr->floor_id = new_floor_id;
1391
1392         /* The dungeon is ready */
1393         current_world_ptr->character_dungeon = TRUE;
1394
1395         /* Hack -- Munchkin characters always get whole map */
1396         if (p_ptr->pseikaku == SEIKAKU_MUNCHKIN)
1397                 wiz_lite((bool)(p_ptr->pclass == CLASS_NINJA));
1398
1399         /* Remember when this level was "created" */
1400         current_floor_ptr->generated_turn = current_world_ptr->game_turn;
1401
1402         /* No dungeon feeling yet */
1403         p_ptr->feeling_turn = current_floor_ptr->generated_turn;
1404         p_ptr->feeling = 0;
1405
1406         /* Clear all flags */
1407         change_floor_mode = 0L;
1408
1409         select_floor_music();
1410 }