OSDN Git Service

フロア保存コードの中心となるファイル。commit 忘れてた。
[hengband/hengband.git] / src / floors.c
1 /* File: floors.c */
2
3 /* Purpose: management of the saved floor */
4
5 /*
6  * Copyright (c) 2002  Mogami
7  *
8  * This software may be copied and distributed for educational, research, and
9  * not for profit purposes provided that this copyright and statement are
10  * included in all such copies.
11  */
12
13 #include "angband.h"
14 #include "grid.h"
15
16
17 static s16b new_floor_id;       /* floor_id of the destination */
18 static u32b change_floor_mode;  /* Mode flags for changing floor */
19 static u32b latest_visit_mark;  /* Max number of visit_mark */
20
21
22 /*
23  * Initialize saved_floors array.  Make sure that old temporal files
24  * are not remaining as gurbages.
25  */
26 void init_saved_floors(void)
27 {
28         char floor_savefile[1024];
29         int i;
30         int fd = -1;
31         int mode = 0644;
32         bool force = FALSE;
33
34 #ifdef SET_UID
35 # ifdef SECURE
36         /* Get "games" permissions */
37         beGames();
38 # endif
39 #endif
40
41         for (i = 0; i < MAX_SAVED_FLOORS; i++)
42         {
43                 saved_floor_type *sf_ptr = &saved_floors[i];
44
45                 /* File name */
46                 sprintf(floor_savefile, "%s.F%02d", savefile, i);
47
48                 /* Try to create the file */
49                 fd = fd_make(floor_savefile, mode);
50
51                 /* Failed! */
52                 if (fd < 0)
53                 {
54                         if (!force)
55                         {
56 #ifdef JP
57                                 msg_print("¥¨¥é¡¼¡§¸Å¤¤¥Æ¥ó¥Ý¥é¥ê¡¦¥Õ¥¡¥¤¥ë¤¬»Ä¤Ã¤Æ¤¤¤Þ¤¹¡£");
58                                 msg_print("ÊѶòÈÚÅܤòÆó½Å¤Ëµ¯Æ°¤·¤Æ¤¤¤Ê¤¤¤«³Îǧ¤·¤Æ¤¯¤À¤µ¤¤¡£");
59                                 msg_print("²áµî¤ËÊѶòÈÚÅܤ¬¥¯¥é¥Ã¥·¥å¤·¤¿¾ì¹ç¤Ï°ì»þ¥Õ¥¡¥¤¥ë¤ò");
60                                 msg_print("¶¯À©Åª¤Ëºï½ü¤·¤Æ¼Â¹Ô¤ò³¤±¤é¤ì¤Þ¤¹¡£");
61                                 if (!get_check("¶¯À©Åª¤Ëºï½ü¤·¤Æ¤â¤è¤í¤·¤¤¤Ç¤¹¤«¡©")) quit("¼Â¹ÔÃæ»ß");
62 #else
63                                 msg_print("Error: There are old temporal files.");
64                                 msg_print("Make sure you are not running two game processes simultaneously.");
65                                 msg_print("If the temporal files are garbages of old crashed process, ");
66                                 msg_print("you can delete it safely.");
67                                 if (!get_check("Do you delete old temporal files? ")) quit("Aborted.");
68 #endif
69                                 force = TRUE;
70                         }
71                 }
72                 else
73                 {
74                         /* Close the "fd" */
75                         (void)fd_close(fd);
76                 }
77
78                 /* Simply kill the temporal file */ 
79                 (void)fd_kill(floor_savefile);
80
81                 sf_ptr->floor_id = 0;
82         }
83
84         /* No floor_id used yet (No.0 is reserved to indicate non existance) */
85         max_floor_id = 1;
86
87         /* vist_mark is from 1 */
88         latest_visit_mark = 1;
89
90         /* A sign to mark temporal files */
91         saved_floor_file_sign = time(NULL);
92
93         /* No next floor yet */
94         new_floor_id = 0;
95
96         /* No change floor mode yet */
97         change_floor_mode = 0;
98
99 #ifdef SET_UID
100 # ifdef SECURE
101         /* Drop "games" permissions */
102         bePlayer();
103 # endif
104 #endif
105 }
106
107
108 /*
109  * Kill temporal files
110  * Should be called just before the game quit
111  * and before new game discarding saved game.
112  */
113 void clear_saved_floor_files(void)
114 {
115         char floor_savefile[1024];
116         int i;
117         int fd = -1;
118         int mode = 0644;
119         bool force = FALSE;
120
121 #ifdef SET_UID
122 # ifdef SECURE
123         /* Get "games" permissions */
124         beGames();
125 # endif
126 #endif
127
128         for (i = 0; i < MAX_SAVED_FLOORS; i++)
129         {
130                 saved_floor_type *sf_ptr = &saved_floors[i];
131
132                 /* No temporal file */
133                 if (!sf_ptr->floor_id) continue;
134                 if (sf_ptr->floor_id == p_ptr->floor_id) continue;
135
136                 /* File name */
137                 sprintf(floor_savefile, "%s.F%02d", savefile, i);
138
139                 /* Simply kill the temporal file */ 
140                 (void)fd_kill(floor_savefile);
141         }
142
143 #ifdef SET_UID
144 # ifdef SECURE
145         /* Drop "games" permissions */
146         bePlayer();
147 # endif
148 #endif
149 }
150
151
152 /*
153  * Get a pointer for an item of the saved_floors array.
154  */
155 saved_floor_type *get_sf_ptr(s16b floor_id)
156 {
157         int i;
158
159         /* floor_id No.0 indicates no floor */
160         if (!floor_id) return NULL;
161
162         for (i = 0; i < MAX_SAVED_FLOORS; i++)
163         {
164                 saved_floor_type *sf_ptr = &saved_floors[i];
165
166                 if (sf_ptr->floor_id == floor_id) return sf_ptr;
167         }
168
169         /* None found */
170         return NULL;
171 }
172
173
174 /*
175  * kill a saved floor and get an empty space
176  */
177 static void kill_saved_floor(saved_floor_type *sf_ptr)
178 {
179         char floor_savefile[1024];
180
181         /* Already empty */
182         if (!sf_ptr->floor_id) return;
183
184         if (sf_ptr->floor_id == p_ptr->floor_id)
185         {
186                 /* Kill current floor */
187                 p_ptr->floor_id = 0;
188
189                 /* Current floor doesn't have temporal file */
190         }
191         else 
192         {
193                 /* File name */
194                 sprintf(floor_savefile, "%s.F%02d", savefile, (int)sf_ptr->savefile_id);
195
196                 /* Simply kill the temporal file */ 
197                 (void)fd_kill(floor_savefile);
198         }
199
200         /* No longer exists */
201         sf_ptr->floor_id = 0;
202 }
203
204
205 /*
206  * Initialize new saved floor and get its floor id.  If number of
207  * saved floors are already MAX_SAVED_FLOORS, kill the oldest one.
208  */
209 s16b get_new_floor_id(void)
210 {
211         saved_floor_type *sf_ptr;
212         int i;
213
214         /* Look for empty space */
215         for (i = 0; i < MAX_SAVED_FLOORS; i++)
216         {
217                 sf_ptr = &saved_floors[i];
218
219                 if (!sf_ptr->floor_id) break;
220         }
221
222         /* None found */
223         if (i == MAX_SAVED_FLOORS)
224         {
225                 int oldest = 0;
226                 u32b oldest_visit = 0xffffffffL;
227
228                 /* Search for oldest */
229                 for (i = 0; i < MAX_SAVED_FLOORS; i++)
230                 {
231                         sf_ptr = &saved_floors[i];
232
233                         /* Don't kill current floor */
234                         if (sf_ptr->floor_id == p_ptr->floor_id) continue;
235
236                         /* Don't kill newer */
237                         if (sf_ptr->visit_mark > oldest_visit) continue;
238
239                         oldest = i;
240                         oldest_visit = sf_ptr->visit_mark;
241                 }
242
243                 /* Kill oldest saved floor */
244                 sf_ptr = &saved_floors[oldest];
245                 kill_saved_floor(sf_ptr);
246
247                 /* Use it */
248                 i = oldest;
249         }
250
251         /* Prepare new floor data */
252         sf_ptr->savefile_id = i;
253         sf_ptr->floor_id = max_floor_id;
254         sf_ptr->last_visit = 0;
255         sf_ptr->upper_floor_id = 0;
256         sf_ptr->lower_floor_id = 0;
257         sf_ptr->visit_mark = latest_visit_mark++;
258
259         /* sf_ptr->dun_level is not yet decided */
260
261
262         /* Increment number of floor_id */
263         if (max_floor_id < MAX_SHORT) max_floor_id++;
264
265         /* 32767 floor_ids are all used up!  Re-use ancient IDs */
266         else max_floor_id = 1;
267
268         return sf_ptr->floor_id;
269 }
270
271
272 /*
273  * Prepare mode flags of changing floor
274  */
275 void prepare_change_floor_mode(u32b mode)
276 {
277         change_floor_mode |= mode;
278 }
279
280
281 /*
282  * Builds the dead end
283  */
284 static void build_dead_end(void)
285 {
286         int x,y;
287
288         /* Clear and empty the cave */
289         clear_cave();
290
291         /* Smallest area */
292         cur_hgt = SCREEN_HGT;
293         cur_wid = SCREEN_WID;
294
295         /* Filled with permanent walls */
296         for (y = 0; y < MAX_HGT; y++)
297         {
298                 for (x = 0; x < MAX_WID; x++)
299                 {
300                         /* Create "solid" perma-wall */
301                         cave[y][x].feat = FEAT_PERM_SOLID;
302                 }
303         }
304
305         /* Place at center of the floor */
306         py = cur_hgt / 2;
307         px = cur_wid / 2;
308
309         /* Give one square */
310         place_floor_bold(py, px);
311 }
312
313
314 /*
315  * Preserve_pets
316  */
317 static void preserve_pet(void)
318 {
319         int num, i;
320
321         for(num = 0; num < 21; num++)
322         {
323                 party_mon[num].r_idx = 0;
324         }
325
326         if (p_ptr->riding)
327         {
328                 COPY(&party_mon[0], &m_list[p_ptr->riding], monster_type);
329
330                 /* Delete from this floor */
331                 delete_monster_idx(p_ptr->riding);
332         }
333
334         for(i = m_max - 1, num = 1; (i >= 1 && num < 21); i--)
335         {
336                 monster_type *m_ptr = &m_list[i];
337                         
338                 if (!m_ptr->r_idx) continue;
339                 if (!is_pet(m_ptr)) continue;
340                 if (i == p_ptr->riding) continue;
341
342                 if (reinit_wilderness)
343                 {
344                         /* Don't lose sight of pets when getting a Quest */
345                 }
346                 else
347                 {
348                         int dis = distance(py, px, m_ptr->fy, m_ptr->fx);
349
350                         /*
351                          * Pets with nickname will follow even from 3 blocks away
352                          * when you or the pet can see the other.
353                          */
354                         if (m_ptr->nickname && 
355                             (player_has_los_bold(m_ptr->fy, m_ptr->fx) ||
356                              los(m_ptr->fy, m_ptr->fx, py, px)))
357                         {
358                                 if (dis > 3) continue;
359                         }
360                         else
361                         {
362                                 if (dis > 1) continue;
363                         }
364                         if (m_ptr->confused || m_ptr->stunned || m_ptr->csleep) continue;
365                 }
366
367                 COPY(&party_mon[num], &m_list[i], monster_type);
368                 num++;
369
370                 /* Delete from this floor */
371                 delete_monster_idx(i);
372         }
373
374         if (record_named_pet)
375         {
376                 for (i = m_max - 1; i >=1; i--)
377                 {
378                         monster_type *m_ptr = &m_list[i];
379                         char m_name[80];
380                                 
381                         if (!m_ptr->r_idx) continue;
382                         if (!is_pet(m_ptr)) continue;
383                         if (!m_ptr->nickname) continue;
384                         if (p_ptr->riding == i) continue;
385                                 
386                         monster_desc(m_name, m_ptr, 0x88);
387                         do_cmd_write_nikki(NIKKI_NAMED_PET, 4, m_name);
388                 }
389         }
390 }
391
392
393 /*
394  * Place preserved pet monsters on new floor
395  */
396 static void place_pet(void)
397 {
398         int i, max_num;
399
400         if (p_ptr->wild_mode)
401                 max_num = 1;
402         else
403                 max_num = 21;
404
405         for (i = 0; i < max_num; i++)
406         {
407                 int cy, cx, m_idx;
408
409                 if (!(party_mon[i].r_idx)) continue;
410
411
412                 if (i == 0)
413                 {
414                         m_idx = m_pop();
415                         p_ptr->riding = m_idx;
416                         if (m_idx)
417                         {
418                                 cy = py;
419                                 cx = px;
420                         }
421                 }
422                 else
423                 {
424                         int j, d;
425
426                         for(d = 1; d < 6; d++)
427                         {
428                                 for(j = 1000; j > 0; j--)
429                                 {
430                                         scatter(&cy, &cx, py, px, d, 0);
431                                         if ((cave_floor_bold(cy, cx) || (cave[cy][cx].feat == FEAT_TREES)) && !cave[cy][cx].m_idx && !((cy == py) && (cx == px))) break;
432                                 }
433                                 if (j) break;
434                         }
435                         if (d == 6 || p_ptr->inside_arena || p_ptr->inside_battle)
436                                 m_idx = 0;
437                         else
438                                 m_idx = m_pop();
439                 }
440                 
441                 if (m_idx)
442                 {
443                         monster_type *m_ptr = &m_list[m_idx];
444                         monster_race *r_ptr;
445                         
446                         cave[cy][cx].m_idx = m_idx;
447
448                         m_ptr->r_idx = party_mon[i].r_idx;
449                         r_ptr = &r_info[m_ptr->r_idx];
450
451                         /* Copy all member of the structure */
452                         *m_ptr = party_mon[i];
453
454                         m_ptr->fy = cy;
455                         m_ptr->fx = cx;
456                         m_ptr->ml = TRUE;
457                         m_ptr->csleep = 0;
458                         set_pet(m_ptr);
459
460                         if ((r_ptr->flags1 & RF1_FORCE_SLEEP) && !ironman_nightmare)
461                         {
462                                 /* Monster is still being nice */
463                                 m_ptr->mflag |= (MFLAG_NICE);
464                                 
465                                 /* Must repair monsters */
466                                 repair_monsters = TRUE;
467                         }
468                         
469                         /* Update the monster */
470                         update_mon(m_idx, TRUE);
471                         lite_spot(cy, cx);
472                         
473                         r_ptr->cur_num++;
474                         
475                         /* Hack -- Count the number of "reproducers" */
476                         if (r_ptr->flags2 & RF2_MULTIPLY) num_repro++;
477                         
478                         /* Hack -- Notice new multi-hued monsters */
479                         if (r_ptr->flags1 & RF1_ATTR_MULTI) shimmer_monsters = TRUE;
480                 }
481                 else
482                 {
483                         char m_name[80];
484                         
485                         monster_desc(m_name, &party_mon[i], 0);
486 #ifdef JP
487                         msg_format("%s¤È¤Ï¤°¤ì¤Æ¤·¤Þ¤Ã¤¿¡£", m_name);
488 #else
489                         msg_format("You have lost sight of %s.", m_name);
490 #endif
491                         if (record_named_pet && party_mon[i].nickname)
492                         {
493                                 monster_desc(m_name, &party_mon[i], 0x08);
494                                 do_cmd_write_nikki(NIKKI_NAMED_PET, 5, m_name);
495                         }
496                 }
497         }
498 }
499
500
501 /*
502  * Maintain unique monsters and artifatcs, mark next floor_id at
503  * stairs, save current floor, and prepare to enter next floor.
504  */
505 void leave_floor(void)
506 {
507         cave_type *c_ptr = NULL;
508         saved_floor_type *sf_ptr;
509         int quest_r_idx = 0;
510         int i;
511
512         /* Preserve pets and prepare to take these to next floor */
513         preserve_pet();
514
515         /* New floor is not yet prepared */
516         new_floor_id = 0;
517
518
519         /* From somewhere of the surface to somewhere of the surface */
520         if (!dungeon_type)
521         {
522                 /* No need to save current floor */
523                 return;
524         }
525
526         /* Search the quest monster index */
527         for (i = 0; i < max_quests; i++)
528         {
529                 if ((quest[i].status == QUEST_STATUS_TAKEN) &&
530                     ((quest[i].type == QUEST_TYPE_KILL_LEVEL) ||
531                     (quest[i].type == QUEST_TYPE_RANDOM)) &&
532                     (quest[i].level == dun_level) &&
533                     (dungeon_type == quest[i].dungeon) &&
534                     !(quest[i].flags & QUEST_FLAG_PRESET))
535                 {
536                         quest_r_idx = quest[i].r_idx;
537                 }
538         }
539
540         /* Maintain unique monsters and quest monsters */
541         for (i = 1; i < m_max; i++)
542         {
543                 monster_race *r_ptr;
544                 monster_type *m_ptr = &m_list[i];
545
546                 /* Skip dead monsters */
547                 if (!m_ptr->r_idx) continue;
548
549                 /* Extract real monster race */
550                 r_ptr = &r_info[m_ptr->r_idx];
551                 if (m_ptr->mflag2 & MFLAG_CHAMELEON)
552                 {
553                         if (r_ptr->flags1 & RF1_UNIQUE)
554                                 r_ptr = &r_info[MON_CHAMELEON_K];
555                         else
556                                 r_ptr = &r_info[MON_CHAMELEON];
557                 }
558
559                 /* Memorize location of unique */
560                 if ((r_ptr->flags1 & RF1_UNIQUE) ||
561                     (r_ptr->flags7 & RF7_UNIQUE_7))
562                 {
563                         r_ptr->floor_id = p_ptr->floor_id;
564                 }
565
566                 else if (quest_r_idx == m_ptr->r_idx)
567                 {
568                         /* Delete non-unique quest monsters */
569                         delete_monster_idx(i);
570                 }
571         }
572
573         /* Maintain artifatcs */
574         for (i = 1; i < o_max; i++)
575         {
576                 object_type *o_ptr = &o_list[i];
577
578                 /* Skip dead objects */
579                 if (!o_ptr->k_idx) continue;
580
581                 /* Memorize location of the artifact */
582                 if (artifact_p(o_ptr))
583                 {
584                         a_info[o_ptr->name1].floor_id = p_ptr->floor_id;
585                 }
586         }
587
588         /* Check if there is a same item */
589         for (i = 0; i < INVEN_PACK; i++)
590         {
591                 object_type *o_ptr = &inventory[i];
592
593                 /* Skip dead objects */
594                 if (!o_ptr->k_idx) continue;
595
596                 /* Delete old memorized location of the artifact */
597                 if (artifact_p(o_ptr))
598                 {
599                         a_info[o_ptr->name1].floor_id = 0;
600                 }
601         }
602
603         /* Extract current floor info or NULL */
604         sf_ptr = get_sf_ptr(p_ptr->floor_id);
605
606         /* Choose random stairs */
607         if ((change_floor_mode & CFM_RAND_CONNECT) && p_ptr->floor_id)
608         {
609                 int x, y, sx = 0, sy = 0;
610                 int x_table[20];
611                 int y_table[20];
612                 int num = 0;
613                 int i;
614
615                 /* Search usable stairs */
616                 for (y = 0; y < cur_hgt; y++)
617                 {
618                         for (x = 0; x < cur_wid; x++)
619                         {
620                                 cave_type *c_ptr = &cave[y][x];
621                                 bool ok = FALSE;
622
623                                 if (change_floor_mode & CFM_UP)
624                                 {
625                                         /* Found fixed stairs */
626                                         if (c_ptr->special &&
627                                             c_ptr->special == sf_ptr->upper_floor_id)
628                                         {
629                                                 sx = x;
630                                                 sy = y;
631                                         }
632
633                                         if (c_ptr->feat == FEAT_LESS ||
634                                             c_ptr->feat == FEAT_LESS_LESS)
635                                                 ok = TRUE;
636                                 }
637                                 else if (change_floor_mode & CFM_DOWN)
638                                 {
639                                         /* Found fixed stairs */
640                                         if (c_ptr->special &&
641                                             c_ptr->special == sf_ptr->lower_floor_id)
642                                         {
643                                                 sx = x;
644                                                 sy = y;
645                                         }
646
647                                         if (c_ptr->feat == FEAT_MORE ||
648                                             c_ptr->feat == FEAT_MORE_MORE)
649                                                 ok = TRUE;
650                                 }
651
652                                 if (ok && num < 20)
653                                 {
654                                         x_table[num] = x;
655                                         y_table[num] = y;
656                                         num++;
657                                 }
658                         }
659                 }
660
661                 if (sx)
662                 {
663                         /* Already fixed */
664                         py = sy;
665                         px = sx;
666                 }
667                 else if (!num)
668                 {
669                         /* No stairs found! -- No return */
670                         prepare_change_floor_mode(CFM_RAND_PLACE | CFM_NO_RETURN);
671
672                         /* Mega Hack -- It's not the stairs you enter.  Disable it.  */
673                         cave[py][px].special = 0;
674                 }
675                 else
676                 {
677                         /* Choose random one */
678                         i = randint0(num);
679                         
680                         /* Point stair location */
681                         py = y_table[i];
682                         px = x_table[i];
683                 }
684         }
685
686         /* Extract new dungeon level */
687         if (!(change_floor_mode & CFM_CLEAR_ALL))
688         {
689                 int move_num = 0;
690
691                 /* Extract stair position */
692                 c_ptr = &cave[py][px];
693
694                 /* Get back to old saved floor? */
695                 if (dun_level && c_ptr->special && get_sf_ptr(c_ptr->special))
696                 {
697                         /* Saved floor is exist.  Use it. */
698                         new_floor_id = c_ptr->special;
699                 }
700
701                 /* Extract level movement number */
702                 if (change_floor_mode & CFM_DOWN)
703                 {
704                         move_num = 1;
705                         if (!dun_level)
706                                 move_num = d_info[c_ptr->special].mindepth;
707                 }
708                 else if (change_floor_mode & CFM_UP)
709                 {
710                         move_num = -1;
711                         if (dun_level + move_num < d_info[dungeon_type].mindepth)
712                                 move_num = -dun_level;
713                 }
714                 
715                 /* Mark shaft up/down */
716                 if (c_ptr->feat == FEAT_LESS_LESS ||
717                     c_ptr->feat == FEAT_MORE_MORE)
718                 {
719                         prepare_change_floor_mode(CFM_SHAFT);
720                         move_num += SGN(move_num);
721                 }
722
723                 dun_level += move_num;
724         }
725
726         /* Leaving the dungeon to town */
727         if (!dun_level && dungeon_type)
728         {
729                 p_ptr->leaving_dungeon = TRUE;
730                 if (!vanilla_town && !lite_town)
731                 {
732                         p_ptr->wilderness_y = d_info[dungeon_type].dy;
733                         p_ptr->wilderness_x = d_info[dungeon_type].dx;
734                 }
735                 p_ptr->recall_dungeon = dungeon_type;
736                 dungeon_type = 0;
737
738                 /* Reach to the surface -- Clear all saved floors */
739                 prepare_change_floor_mode(CFM_CLEAR_ALL);
740         }
741
742         if (change_floor_mode & CFM_CLEAR_ALL)
743         {
744                 int i;
745
746                 /* Kill all saved floors */
747                 for (i = 0; i < MAX_SAVED_FLOORS; i++)
748                         kill_saved_floor(&saved_floors[i]);
749
750                 /* Reset visit_mark count */
751                 latest_visit_mark = 1;
752         }
753         else if (change_floor_mode & CFM_NO_RETURN)
754         {
755                 /* Kill current floor */
756                 kill_saved_floor(sf_ptr);
757         }
758
759         /* No current floor -- Left/Enter dungeon etc... */
760         if (!p_ptr->floor_id)
761         {
762                 /* No longer need to save current floor */
763                 return;
764         }
765
766
767         /* Mark next floor_id on the previous floor */
768         if (!new_floor_id)
769         {
770                 /* Get new id */
771                 new_floor_id = get_new_floor_id();
772
773                 /* Connect from here */
774                 if (c_ptr)
775                 {
776                         c_ptr->special = new_floor_id;
777                 }
778
779                 /* Record new dungeon level */
780                 get_sf_ptr(new_floor_id)->dun_level = dun_level;
781         }
782
783         /* Fix connection -- level teleportation or trap door */
784         if (change_floor_mode & CFM_RAND_CONNECT)
785         {
786                 if (change_floor_mode & CFM_UP)
787                         sf_ptr->upper_floor_id = new_floor_id;
788                 else if (change_floor_mode & CFM_DOWN)
789                         sf_ptr->lower_floor_id = new_floor_id;
790         }
791
792         /* If you can return, you need to save previous floor */
793         if (!(change_floor_mode & (CFM_NO_RETURN | CFM_CLEAR_ALL)))
794         {
795                 /* Record the last visit turn of current floor */
796                 sf_ptr->last_visit = turn;
797
798                 /* Forget the lite */
799                 forget_lite();
800
801                 /* Forget the view */
802                 forget_view();
803
804                 /* Forget the view */
805                 clear_mon_lite();
806
807                 /* Save current floor */
808                 if (!save_floor(sf_ptr, 0))
809                 {
810                         /* Save failed -- No return */
811                         prepare_change_floor_mode(CFM_NO_RETURN);
812
813                         /* Kill current floor */
814                         kill_saved_floor(get_sf_ptr(p_ptr->floor_id));
815                 }
816         }
817 }
818
819
820 /*
821  * Enter new floor.  If the floor is an old saved floor, it will be
822  * restored from the temporal file.  If the floor is new one, new cave
823  * will be generated.
824  */
825 void change_floor(void)
826 {
827         saved_floor_type *sf_ptr;
828         bool loaded = FALSE;
829
830         /* The dungeon is not ready */
831         character_dungeon = FALSE;
832
833         /* No longer in the trap detecteded region */
834         p_ptr->dtrap = FALSE;
835
836         /* Mega-Hack -- no panel yet */
837         panel_row_min = 0;
838         panel_row_max = 0;
839         panel_col_min = 0;
840         panel_col_max = 0;
841
842         /* Mega-Hack -- not ambushed on the wildness? */
843         ambush_flag = FALSE;
844
845         /* On the surface */
846         if (!dungeon_type)
847         {
848                 /* Create cave */
849                 generate_cave();
850
851                 /* Paranoia */
852                 new_floor_id = 0;
853         }
854
855         /* In the dungeon */
856         else
857         {
858                 /* No floor_id yet */
859                 if (!new_floor_id)
860                 {
861                         /* Get new id */
862                         new_floor_id = get_new_floor_id();
863                 }
864
865                 /* Pointer for infomations of new floor */
866                 sf_ptr = get_sf_ptr(new_floor_id);
867
868                 /* Try to restore old floor */
869                 if (sf_ptr->last_visit)
870                 {
871                         /* Old saved floor is exist */
872                         if (load_floor(sf_ptr, 0))
873                         {
874                                 loaded = TRUE;
875
876                                 /* Forbid return stairs */
877                                 if (change_floor_mode & CFM_NO_RETURN)
878                                 {
879                                         cave_type *c_ptr = &cave[py][px];
880                                 
881                                         /* Reset to floor */
882                                         place_floor_grid(c_ptr);
883                                         c_ptr->special = 0;
884                                 }
885                         }
886                 }
887
888                 /*
889                  * Next floor is right-above/right-under the current floor.
890                  *
891                  * Stair creation/Teleport level/Trap door will take
892                  * you the same floor when you used it later again.
893                  */
894                 if (p_ptr->floor_id)
895                 {
896                         saved_floor_type *cur_sf_ptr = get_sf_ptr(p_ptr->floor_id);
897
898                         if (change_floor_mode & CFM_UP)
899                         {
900                                 if (cur_sf_ptr->upper_floor_id == new_floor_id)
901                                         sf_ptr->lower_floor_id = p_ptr->floor_id;
902                         }
903                         else if (change_floor_mode & CFM_DOWN)
904                         {
905                                 if (cur_sf_ptr->lower_floor_id == new_floor_id)
906                                         sf_ptr->upper_floor_id = p_ptr->floor_id;
907                         }
908                 }
909
910                 /* Maintain monsters and artifacts */
911                 if (loaded)
912                 {
913                         int i;
914                         s32b absence_ticks = (turn - sf_ptr->last_visit) / TURNS_PER_TICK;
915                         int alloc_chance = d_info[dungeon_type].max_m_alloc_chance;
916                         int alloc_times;
917
918                         /* Maintain monsters */
919                         for (i = 1; i < m_max; i++)
920                         {
921                                 monster_race *r_ptr;
922                                 monster_type *m_ptr = &m_list[i];
923
924                                 /* Skip dead monsters */
925                                 if (!m_ptr->r_idx) continue;
926
927                                 if (!is_pet(m_ptr))
928                                 {
929                                         /* Restore HP */
930                                         m_ptr->hp = m_ptr->maxhp = m_ptr->max_maxhp;
931
932                                         /* Remove fear */
933                                         m_ptr->monfear = 0;
934
935                                         /* Remove invulnerability */
936                                         m_ptr->invulner = 0;
937
938                                         /* Remove fast status */
939                                         m_ptr->fast = 0;
940
941                                         /* Remove slow status */
942                                         m_ptr->slow = 0;
943
944                                         /* Remove sleep */
945                                         m_ptr->csleep = 0;
946
947                                         /* Remove stun */
948                                         m_ptr->stunned = 0;
949
950                                         /* Remove confusion */
951                                         m_ptr->confused = 0;
952                                 }
953
954                                 /* Extract real monster race */
955                                 r_ptr = &r_info[m_ptr->r_idx];
956                                 if (m_ptr->mflag2 & MFLAG_CHAMELEON)
957                                 {
958                                         if (r_ptr->flags1 & RF1_UNIQUE)
959                                                 r_ptr = &r_info[MON_CHAMELEON_K];
960                                         else
961                                                 r_ptr = &r_info[MON_CHAMELEON];
962                                 }
963
964                                 /* Ignore non-unique */
965                                 if (!(r_ptr->flags1 & RF1_UNIQUE) &&
966                                     !(r_ptr->flags7 & RF7_UNIQUE_7)) continue;
967
968                                 /* Appear at a different floor? */
969                                 if (r_ptr->floor_id != new_floor_id)
970                                 {
971                                         /* Disapper from here */
972                                         delete_monster_idx(i);
973                                 }
974                         }
975
976                         /* Maintain artifatcs */
977                         for (i = 1; i < o_max; i++)
978                         {
979                                 object_type *o_ptr = &o_list[i];
980
981                                 /* Skip dead objects */
982                                 if (!o_ptr->k_idx) continue;
983
984                                 /* Ignore non-artifact */
985                                 if (!artifact_p(o_ptr)) continue;
986
987                                 /* Appear at a different floor? */
988                                 if (a_info[o_ptr->name1].floor_id != new_floor_id)
989                                 {
990                                         /* Disappear from here */
991                                         delete_object_idx(i);
992                                 }
993                                 else
994                                 {
995                                         /* Cancel preserve */
996                                         a_info[o_ptr->name1].cur_num = 1;
997                                 }
998                         }
999
1000                         place_quest_monsters();
1001
1002                         /* Place some random monsters */
1003                         alloc_times = absence_ticks / alloc_chance;
1004
1005                         if (randint0(alloc_chance) < (absence_ticks % alloc_chance))
1006                                 alloc_times++;
1007
1008                         for (i = 0; i < alloc_times; i++)
1009                         {
1010                                 /* Make a (group of) new monster */
1011                                 (void)alloc_monster(0, 0);
1012                         }
1013                 }
1014
1015                 /* New floor_id or failed to restore */
1016                 else /* if (!loaded) */
1017                 {
1018                         if (sf_ptr->last_visit)
1019                         {
1020                                 /* Temporal file is broken? */
1021 #ifdef JP
1022                                 msg_print("³¬ÃʤϹԤ­»ß¤Þ¤ê¤À¤Ã¤¿¡£");
1023 #else
1024                                 msg_print("The staircases come to a dead end...");
1025 #endif
1026
1027                                 /* Create simple dead end */
1028                                 build_dead_end();
1029
1030                                 /* Break connection */
1031                                 if (change_floor_mode & CFM_UP)
1032                                 {
1033                                         sf_ptr->upper_floor_id = 0;
1034                                 }
1035                                 else if (change_floor_mode & CFM_DOWN)
1036                                 {
1037                                         sf_ptr->lower_floor_id = 0;
1038                                 }
1039                         }
1040                         else
1041                         {
1042                                 /* Newly create cave */
1043                                 generate_cave();
1044                         }
1045
1046                         /* Record last visit turn */
1047                         sf_ptr->last_visit = turn;
1048
1049                         /* Set correct dun_level value */
1050                         sf_ptr->dun_level = dun_level;
1051
1052                         /* Creat connected stairs */
1053                         if (!(change_floor_mode & (CFM_NO_RETURN | CFM_CLEAR_ALL)) && dun_level)
1054                         {
1055                                 bool ok = TRUE;
1056
1057                                 /* Extract stair position */
1058                                 cave_type *c_ptr = &cave[py][px];
1059
1060                                 /*** Create connected stairs ***/
1061
1062                                 /* No stairs down from Quest */
1063                                 if ((change_floor_mode & CFM_UP) && !quest_number(dun_level))
1064                                 {
1065                                         if (change_floor_mode & CFM_SHAFT)
1066                                                 c_ptr->feat = FEAT_MORE_MORE;
1067                                         else
1068                                                 c_ptr->feat = FEAT_MORE;
1069                                 }
1070
1071                                 /* No stairs up when ironman_downward */
1072                                 else if ((change_floor_mode & CFM_DOWN) && !ironman_downward)
1073                                 {
1074                                         if (change_floor_mode & CFM_SHAFT)
1075                                                 c_ptr->feat = FEAT_LESS_LESS;
1076                                         else
1077                                                 c_ptr->feat = FEAT_LESS;
1078                                 }
1079                                 else
1080                                 {
1081                                         /* Hum??? */
1082                                         ok = FALSE;
1083                                 }
1084
1085                                 if (ok)
1086                                 {
1087                                         /* Paranoia -- Clear mimic */
1088                                         c_ptr->mimic = 0;
1089
1090                                         /* Connect to previous floor */
1091                                         c_ptr->special = p_ptr->floor_id;
1092                                 }
1093                         }
1094                 }
1095
1096                 /* Arrive at random grid */
1097                 if (change_floor_mode & (CFM_RAND_PLACE))
1098                 {
1099                         (void)new_player_spot();
1100                 }
1101
1102                 /* You see stairs blocked */
1103                 else if (change_floor_mode & CFM_NO_RETURN)
1104                 {
1105                         if (!p_ptr->blind)
1106                         {
1107 #ifdef JP
1108                                 msg_print("ÆÍÁ³³¬Ãʤ¬ºÉ¤¬¤ì¤Æ¤·¤Þ¤Ã¤¿¡£");
1109 #else
1110                                 msg_print("Suddenly the stairs is blocked!");
1111 #endif
1112                         }
1113                         else
1114                         {
1115 #ifdef JP
1116                                 msg_print("¥´¥È¥´¥È¤È²¿¤«²»¤¬¤·¤¿¡£");
1117 #else
1118                                 msg_print("You hear some noises.");
1119 #endif
1120                         }
1121                 }
1122
1123                 /*
1124                  * Update visit mark
1125                  *
1126                  * The "turn" is not always different number because
1127                  * the level teleport doesn't take any turn.  Use
1128                  * visit mark instead of last visit turn to find the
1129                  * oldest saved floor.
1130                  */
1131                 sf_ptr->visit_mark = latest_visit_mark++;
1132         }
1133
1134         /* Place preserved pet monsters */
1135         place_pet();
1136
1137         /* Now the player is in new floor */
1138         p_ptr->floor_id = new_floor_id;
1139
1140         /* The dungeon is ready */
1141         character_dungeon = TRUE;
1142
1143         if (p_ptr->pseikaku == SEIKAKU_MUNCHKIN)
1144                 wiz_lite(TRUE, (bool)(p_ptr->pclass == CLASS_NINJA));
1145
1146         /* Remember when this level was "created" */
1147         old_turn = turn;
1148
1149         /* Clear all flags */
1150         change_floor_mode = 0L;
1151 }
1152
1153
1154
1155 /*
1156  * Create stairs at or move previously created stairs into the player
1157  * location.
1158  */
1159 void stair_creation(void)
1160 {
1161         saved_floor_type *sf_ptr;
1162         saved_floor_type *dest_sf_ptr;
1163
1164         bool up = TRUE;
1165         bool down = TRUE;
1166         s16b dest_floor_id = 0;
1167         int i;
1168
1169
1170         /* Forbid up staircases on Ironman mode */
1171         if (ironman_downward) up = FALSE;
1172
1173         /* Forbid down staircases on quest level */
1174         if (quest_number(dun_level) || (dun_level >= d_info[dungeon_type].maxdepth)) down = FALSE;
1175
1176         /* No effect out of standard dungeon floor */
1177         if (!dun_level || (!up && !down) ||
1178             (p_ptr->inside_quest && (p_ptr->inside_quest < MIN_RANDOM_QUEST)) ||
1179             p_ptr->inside_arena || p_ptr->inside_battle)
1180         {
1181                 /* arena or quest */
1182 #ifdef JP
1183                 msg_print("¸ú²Ì¤¬¤¢¤ê¤Þ¤»¤ó¡ª");
1184 #else
1185                 msg_print("There is no effect!");
1186 #endif
1187                 return;
1188         }
1189
1190         /* Artifacts resists */
1191         if (!cave_valid_bold(py, px))
1192         {
1193 #ifdef JP
1194                 msg_print("¾²¾å¤Î¥¢¥¤¥Æ¥à¤¬¼öʸ¤òÄ·¤ÍÊÖ¤·¤¿¡£");
1195 #else
1196                 msg_print("The object resists the spell.");
1197 #endif
1198
1199                 return;
1200         }
1201
1202         /* Destroy all objects in the grid */
1203         delete_object(py, px);
1204
1205         /* Extract current floor data */
1206         sf_ptr = get_sf_ptr(p_ptr->floor_id);
1207
1208         /* Choose randomly */
1209         if (up && down)
1210         {
1211                 if (randint0(100) < 50) up = FALSE;
1212                 else down = FALSE;
1213         }
1214
1215         /* Destination is already fixed */
1216         if (up)
1217         {
1218                 if (sf_ptr->upper_floor_id) dest_floor_id = sf_ptr->upper_floor_id;
1219         }
1220         else
1221         {
1222                 if (sf_ptr->lower_floor_id) dest_floor_id = sf_ptr->lower_floor_id;
1223         }
1224
1225
1226         /* Search old stairs leading to the destination */
1227         if (dest_floor_id)
1228         {
1229                 int x, y;
1230
1231                 for (y = 0; y < cur_hgt; y++)
1232                 {
1233                         for (x = 0; x < cur_wid; x++)
1234                         {
1235                                 cave_type *c_ptr = &cave[y][x];
1236                                 bool ok = FALSE;
1237
1238                                 if (!c_ptr->special) continue;
1239                                 if (c_ptr->special != dest_floor_id) continue;
1240
1241                                 /* Remove old stairs */
1242                                 c_ptr->special = 0;
1243                                 cave_set_feat(y, x, floor_type[randint0(100)]);
1244                         }
1245                 }
1246         }
1247
1248         /* No old destination -- Get new one now */
1249         else
1250         {
1251                 dest_floor_id = get_new_floor_id();
1252
1253                 /* Fix it */
1254                 if (up)
1255                         sf_ptr->upper_floor_id = dest_floor_id;
1256                 else
1257                         sf_ptr->lower_floor_id = dest_floor_id;
1258         }
1259
1260         /* Extract destination floor data */
1261         dest_sf_ptr = get_sf_ptr(dest_floor_id);
1262
1263
1264         /* Create a staircase */
1265         if (up)
1266         {
1267                 if (dest_sf_ptr->last_visit && dest_sf_ptr->dun_level <= dun_level - 2)
1268                         cave_set_feat(py, px, FEAT_LESS_LESS);
1269                 else
1270                         cave_set_feat(py, px, FEAT_LESS);
1271         }
1272         else
1273         {
1274                 if (dest_sf_ptr->last_visit && dest_sf_ptr->dun_level >= dun_level + 2)
1275                         cave_set_feat(py, px, FEAT_MORE_MORE);
1276                 else
1277                         cave_set_feat(py, px, FEAT_MORE);
1278         }
1279
1280
1281         /* Connect this stairs to the destination */
1282         cave[py][px].special = dest_floor_id;
1283 }
1284
1285