OSDN Git Service

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