OSDN Git Service

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