OSDN Git Service

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