OSDN Git Service

階段を登り降りして保存されたフロアに戻ると全ての眠っているモンスターが
[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 = real_r_ptr(m_ptr);
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  * Hack -- Update location of unique monsters and artifacts
503  *
504  * The r_ptr->floor_id and a_ptr->floor_id are not updated correctly
505  * while new floor creation since dungeons may be re-created by
506  * auto-scum option.
507  */
508 static void update_unique_artifact(s16b cur_floor_id)
509 {
510         int i;
511
512         /* Maintain unique monsters */
513         for (i = 1; i < m_max; i++)
514         {
515                 monster_race *r_ptr;
516                 monster_type *m_ptr = &m_list[i];
517
518                 /* Skip dead monsters */
519                 if (!m_ptr->r_idx) continue;
520
521                 /* Extract real monster race */
522                 r_ptr = real_r_ptr(m_ptr);
523
524                 /* Memorize location of the unique monster */
525                 if ((r_ptr->flags1 & RF1_UNIQUE) ||
526                     (r_ptr->flags7 & RF7_UNIQUE_7))
527                 {
528                         r_ptr->floor_id = cur_floor_id;
529                 }
530         }
531
532         /* Maintain artifatcs */
533         for (i = 1; i < o_max; i++)
534         {
535                 object_type *o_ptr = &o_list[i];
536
537                 /* Skip dead objects */
538                 if (!o_ptr->k_idx) continue;
539
540                 /* Memorize location of the artifact */
541                 if (artifact_p(o_ptr))
542                 {
543                         a_info[o_ptr->name1].floor_id = cur_floor_id;
544                 }
545         }
546 }
547
548
549 /*
550  * Maintain quest monsters, mark next floor_id at stairs, save current
551  * floor, and prepare to enter next floor.
552  */
553 void leave_floor(void)
554 {
555         cave_type *c_ptr = NULL;
556         saved_floor_type *sf_ptr;
557         int quest_r_idx = 0;
558         int i;
559
560         /* Preserve pets and prepare to take these to next floor */
561         preserve_pet();
562
563         /* New floor is not yet prepared */
564         new_floor_id = 0;
565
566
567         /* From somewhere of the surface to somewhere of the surface */
568         if (!dungeon_type)
569         {
570                 /* No need to save current floor */
571                 return;
572         }
573
574         /* Search the quest monster index */
575         for (i = 0; i < max_quests; i++)
576         {
577                 if ((quest[i].status == QUEST_STATUS_TAKEN) &&
578                     ((quest[i].type == QUEST_TYPE_KILL_LEVEL) ||
579                     (quest[i].type == QUEST_TYPE_RANDOM)) &&
580                     (quest[i].level == dun_level) &&
581                     (dungeon_type == quest[i].dungeon) &&
582                     !(quest[i].flags & QUEST_FLAG_PRESET))
583                 {
584                         quest_r_idx = quest[i].r_idx;
585                 }
586         }
587
588         /* Maintain quest monsters */
589         for (i = 1; i < m_max; i++)
590         {
591                 monster_race *r_ptr;
592                 monster_type *m_ptr = &m_list[i];
593
594                 /* Skip dead monsters */
595                 if (!m_ptr->r_idx) continue;
596
597                 /* Only maintain quest monsters */
598                 if (quest_r_idx != m_ptr->r_idx) continue;
599
600                 /* Extract real monster race */
601                 r_ptr = real_r_ptr(m_ptr);
602
603                 /* Ignore unique monsters */
604                 if ((r_ptr->flags1 & RF1_UNIQUE) ||
605                     (r_ptr->flags7 & RF7_UNIQUE_7)) continue;
606
607                 /* Delete non-unique quest monsters */
608                 delete_monster_idx(i);
609         }
610
611         /* Check if there is a same item */
612         for (i = 0; i < INVEN_PACK; i++)
613         {
614                 object_type *o_ptr = &inventory[i];
615
616                 /* Skip dead objects */
617                 if (!o_ptr->k_idx) continue;
618
619                 /* Delete old memorized location of the artifact */
620                 if (artifact_p(o_ptr))
621                 {
622                         a_info[o_ptr->name1].floor_id = 0;
623                 }
624         }
625
626         /* Extract current floor info or NULL */
627         sf_ptr = get_sf_ptr(p_ptr->floor_id);
628
629         /* Choose random stairs */
630         if ((change_floor_mode & CFM_RAND_CONNECT) && p_ptr->floor_id)
631         {
632                 int x, y, sx = 0, sy = 0;
633                 int x_table[20];
634                 int y_table[20];
635                 int num = 0;
636                 int i;
637
638                 /* Search usable stairs */
639                 for (y = 0; y < cur_hgt; y++)
640                 {
641                         for (x = 0; x < cur_wid; x++)
642                         {
643                                 cave_type *c_ptr = &cave[y][x];
644                                 bool ok = FALSE;
645
646                                 if (change_floor_mode & CFM_UP)
647                                 {
648                                         /* Found fixed stairs */
649                                         if (c_ptr->special &&
650                                             c_ptr->special == sf_ptr->upper_floor_id)
651                                         {
652                                                 sx = x;
653                                                 sy = y;
654                                         }
655
656                                         if (c_ptr->feat == FEAT_LESS ||
657                                             c_ptr->feat == FEAT_LESS_LESS)
658                                                 ok = TRUE;
659                                 }
660                                 else if (change_floor_mode & CFM_DOWN)
661                                 {
662                                         /* Found fixed stairs */
663                                         if (c_ptr->special &&
664                                             c_ptr->special == sf_ptr->lower_floor_id)
665                                         {
666                                                 sx = x;
667                                                 sy = y;
668                                         }
669
670                                         if (c_ptr->feat == FEAT_MORE ||
671                                             c_ptr->feat == FEAT_MORE_MORE)
672                                                 ok = TRUE;
673                                 }
674
675                                 if (ok && num < 20)
676                                 {
677                                         x_table[num] = x;
678                                         y_table[num] = y;
679                                         num++;
680                                 }
681                         }
682                 }
683
684                 if (sx)
685                 {
686                         /* Already fixed */
687                         py = sy;
688                         px = sx;
689                 }
690                 else if (!num)
691                 {
692                         /* No stairs found! -- No return */
693                         prepare_change_floor_mode(CFM_RAND_PLACE | CFM_NO_RETURN);
694
695                         /* Mega Hack -- It's not the stairs you enter.  Disable it.  */
696                         cave[py][px].special = 0;
697                 }
698                 else
699                 {
700                         /* Choose random one */
701                         i = randint0(num);
702                         
703                         /* Point stair location */
704                         py = y_table[i];
705                         px = x_table[i];
706                 }
707         }
708
709         /* Extract new dungeon level */
710         if (!(change_floor_mode & CFM_CLEAR_ALL))
711         {
712                 int move_num = 0;
713
714                 /* Extract stair position */
715                 c_ptr = &cave[py][px];
716
717                 /* Get back to old saved floor? */
718                 if (dun_level && c_ptr->special && get_sf_ptr(c_ptr->special))
719                 {
720                         /* Saved floor is exist.  Use it. */
721                         new_floor_id = c_ptr->special;
722                 }
723
724                 /* Extract level movement number */
725                 if (change_floor_mode & CFM_DOWN) move_num = 1;
726                 else if (change_floor_mode & CFM_UP) move_num = -1;
727                 
728                 /* Mark shaft up/down */
729                 if (c_ptr->feat == FEAT_LESS_LESS ||
730                     c_ptr->feat == FEAT_MORE_MORE)
731                 {
732                         prepare_change_floor_mode(CFM_SHAFT);
733                         move_num += SGN(move_num);
734                 }
735
736                 /* Get out from or Enter the dungeon */
737                 if (change_floor_mode & CFM_DOWN)
738                 {
739                         if (!dun_level)
740                                 move_num = d_info[c_ptr->special].mindepth;
741                 }
742                 else if (change_floor_mode & CFM_UP)
743                 {
744                         if (dun_level + move_num < d_info[dungeon_type].mindepth)
745                                 move_num = -dun_level;
746                 }
747                 
748                 dun_level += move_num;
749         }
750
751         /* Leaving the dungeon to town */
752         if (!dun_level && dungeon_type)
753         {
754                 p_ptr->leaving_dungeon = TRUE;
755                 if (!vanilla_town && !lite_town)
756                 {
757                         p_ptr->wilderness_y = d_info[dungeon_type].dy;
758                         p_ptr->wilderness_x = d_info[dungeon_type].dx;
759                 }
760                 p_ptr->recall_dungeon = dungeon_type;
761                 dungeon_type = 0;
762
763                 /* Reach to the surface -- Clear all saved floors */
764                 prepare_change_floor_mode(CFM_CLEAR_ALL);
765         }
766
767         if (change_floor_mode & CFM_CLEAR_ALL)
768         {
769                 int i;
770
771                 /* Kill all saved floors */
772                 for (i = 0; i < MAX_SAVED_FLOORS; i++)
773                         kill_saved_floor(&saved_floors[i]);
774
775                 /* Reset visit_mark count */
776                 latest_visit_mark = 1;
777         }
778         else if (change_floor_mode & CFM_NO_RETURN)
779         {
780                 /* Kill current floor */
781                 kill_saved_floor(sf_ptr);
782         }
783
784         /* No current floor -- Left/Enter dungeon etc... */
785         if (!p_ptr->floor_id)
786         {
787                 /* No longer need to save current floor */
788                 return;
789         }
790
791
792         /* Mark next floor_id on the previous floor */
793         if (!new_floor_id)
794         {
795                 /* Get new id */
796                 new_floor_id = get_new_floor_id();
797
798                 /* Connect from here */
799                 if (c_ptr)
800                 {
801                         c_ptr->special = new_floor_id;
802                 }
803
804                 /* Record new dungeon level */
805                 get_sf_ptr(new_floor_id)->dun_level = dun_level;
806         }
807
808         /* Fix connection -- level teleportation or trap door */
809         if (change_floor_mode & CFM_RAND_CONNECT)
810         {
811                 if (change_floor_mode & CFM_UP)
812                         sf_ptr->upper_floor_id = new_floor_id;
813                 else if (change_floor_mode & CFM_DOWN)
814                         sf_ptr->lower_floor_id = new_floor_id;
815         }
816
817         /* If you can return, you need to save previous floor */
818         if (!(change_floor_mode & (CFM_NO_RETURN | CFM_CLEAR_ALL)))
819         {
820                 /* Record the last visit turn of current floor */
821                 sf_ptr->last_visit = turn;
822
823                 /* Forget the lite */
824                 forget_lite();
825
826                 /* Forget the view */
827                 forget_view();
828
829                 /* Forget the view */
830                 clear_mon_lite();
831
832                 /* Save current floor */
833                 if (!save_floor(sf_ptr, 0))
834                 {
835                         /* Save failed -- No return */
836                         prepare_change_floor_mode(CFM_NO_RETURN);
837
838                         /* Kill current floor */
839                         kill_saved_floor(get_sf_ptr(p_ptr->floor_id));
840                 }
841         }
842 }
843
844
845 /*
846  * Enter new floor.  If the floor is an old saved floor, it will be
847  * restored from the temporal file.  If the floor is new one, new cave
848  * will be generated.
849  */
850 void change_floor(void)
851 {
852         saved_floor_type *sf_ptr;
853         bool loaded = FALSE;
854
855         /* The dungeon is not ready */
856         character_dungeon = FALSE;
857
858         /* No longer in the trap detecteded region */
859         p_ptr->dtrap = FALSE;
860
861         /* Mega-Hack -- no panel yet */
862         panel_row_min = 0;
863         panel_row_max = 0;
864         panel_col_min = 0;
865         panel_col_max = 0;
866
867         /* Mega-Hack -- not ambushed on the wildness? */
868         ambush_flag = FALSE;
869
870         /* On the surface */
871         if (!dungeon_type)
872         {
873                 /* Create cave */
874                 generate_cave();
875
876                 /* Paranoia -- Now on the surface */
877                 new_floor_id = 0;
878         }
879
880         /* In the dungeon */
881         else
882         {
883                 /* No floor_id yet */
884                 if (!new_floor_id)
885                 {
886                         /* Get new id */
887                         new_floor_id = get_new_floor_id();
888                 }
889
890                 /* Pointer for infomations of new floor */
891                 sf_ptr = get_sf_ptr(new_floor_id);
892
893                 /* Try to restore old floor */
894                 if (sf_ptr->last_visit)
895                 {
896                         /* Old saved floor is exist */
897                         if (load_floor(sf_ptr, 0))
898                         {
899                                 loaded = TRUE;
900
901                                 /* Forbid return stairs */
902                                 if (change_floor_mode & CFM_NO_RETURN)
903                                 {
904                                         cave_type *c_ptr = &cave[py][px];
905                                 
906                                         /* Reset to floor */
907                                         place_floor_grid(c_ptr);
908                                         c_ptr->special = 0;
909                                 }
910                         }
911                 }
912
913                 /*
914                  * Set lower/upper_floor_id of new floor when the new
915                  * floor is right-above/right-under the current floor.
916                  *
917                  * Stair creation/Teleport level/Trap door will take
918                  * you the same floor when you used it later again.
919                  */
920                 if (p_ptr->floor_id)
921                 {
922                         saved_floor_type *cur_sf_ptr = get_sf_ptr(p_ptr->floor_id);
923
924                         if (change_floor_mode & CFM_UP)
925                         {
926                                 /* New floor is right-above */
927                                 if (cur_sf_ptr->upper_floor_id == new_floor_id)
928                                         sf_ptr->lower_floor_id = p_ptr->floor_id;
929                         }
930                         else if (change_floor_mode & CFM_DOWN)
931                         {
932                                 /* New floor is right-under */
933                                 if (cur_sf_ptr->lower_floor_id == new_floor_id)
934                                         sf_ptr->upper_floor_id = p_ptr->floor_id;
935                         }
936                 }
937
938                 /* Maintain monsters and artifacts */
939                 if (loaded)
940                 {
941                         int i;
942                         s32b absence_ticks = (turn - sf_ptr->last_visit) / TURNS_PER_TICK;
943                         int alloc_chance = d_info[dungeon_type].max_m_alloc_chance;
944                         int alloc_times;
945
946                         /* Maintain monsters */
947                         for (i = 1; i < m_max; i++)
948                         {
949                                 monster_race *r_ptr;
950                                 monster_type *m_ptr = &m_list[i];
951
952                                 /* Skip dead monsters */
953                                 if (!m_ptr->r_idx) continue;
954
955                                 if (!is_pet(m_ptr))
956                                 {
957                                         /* Restore HP */
958                                         m_ptr->hp = m_ptr->maxhp = m_ptr->max_maxhp;
959
960                                         /* Remove fear */
961                                         m_ptr->monfear = 0;
962
963                                         /* Remove invulnerability */
964                                         m_ptr->invulner = 0;
965
966                                         /* Remove fast status */
967                                         m_ptr->fast = 0;
968
969                                         /* Remove slow status */
970                                         m_ptr->slow = 0;
971
972                                         /* Remove stun */
973                                         m_ptr->stunned = 0;
974
975                                         /* Remove confusion */
976                                         m_ptr->confused = 0;
977                                 }
978
979                                 /* Extract real monster race */
980                                 r_ptr = real_r_ptr(m_ptr);
981
982                                 /* Ignore non-unique */
983                                 if (!(r_ptr->flags1 & RF1_UNIQUE) &&
984                                     !(r_ptr->flags7 & RF7_UNIQUE_7)) continue;
985
986                                 /* Appear at a different floor? */
987                                 if (r_ptr->floor_id != new_floor_id)
988                                 {
989                                         /* Disapper from here */
990                                         delete_monster_idx(i);
991                                 }
992                         }
993
994                         /* Maintain artifatcs */
995                         for (i = 1; i < o_max; i++)
996                         {
997                                 object_type *o_ptr = &o_list[i];
998
999                                 /* Skip dead objects */
1000                                 if (!o_ptr->k_idx) continue;
1001
1002                                 /* Ignore non-artifact */
1003                                 if (!artifact_p(o_ptr)) continue;
1004
1005                                 /* Appear at a different floor? */
1006                                 if (a_info[o_ptr->name1].floor_id != new_floor_id)
1007                                 {
1008                                         /* Disappear from here */
1009                                         delete_object_idx(i);
1010                                 }
1011                                 else
1012                                 {
1013                                         /* Cancel preserve */
1014                                         a_info[o_ptr->name1].cur_num = 1;
1015                                 }
1016                         }
1017
1018                         place_quest_monsters();
1019
1020                         /* Place some random monsters */
1021                         alloc_times = absence_ticks / alloc_chance;
1022
1023                         if (randint0(alloc_chance) < (absence_ticks % alloc_chance))
1024                                 alloc_times++;
1025
1026                         for (i = 0; i < alloc_times; i++)
1027                         {
1028                                 /* Make a (group of) new monster */
1029                                 (void)alloc_monster(0, 0);
1030                         }
1031                 }
1032
1033                 /* New floor_id or failed to restore */
1034                 else /* if (!loaded) */
1035                 {
1036                         if (sf_ptr->last_visit)
1037                         {
1038                                 /* Temporal file is broken? */
1039 #ifdef JP
1040                                 msg_print("³¬ÃʤϹԤ­»ß¤Þ¤ê¤À¤Ã¤¿¡£");
1041 #else
1042                                 msg_print("The staircases come to a dead end...");
1043 #endif
1044
1045                                 /* Create simple dead end */
1046                                 build_dead_end();
1047
1048                                 /* Break connection */
1049                                 if (change_floor_mode & CFM_UP)
1050                                 {
1051                                         sf_ptr->upper_floor_id = 0;
1052                                 }
1053                                 else if (change_floor_mode & CFM_DOWN)
1054                                 {
1055                                         sf_ptr->lower_floor_id = 0;
1056                                 }
1057                         }
1058                         else
1059                         {
1060                                 /* Newly create cave */
1061                                 generate_cave();
1062                         }
1063
1064                         /* Record last visit turn */
1065                         sf_ptr->last_visit = turn;
1066
1067                         /* Set correct dun_level value */
1068                         sf_ptr->dun_level = dun_level;
1069
1070                         /* Creat connected stairs */
1071                         if (!(change_floor_mode & (CFM_NO_RETURN | CFM_CLEAR_ALL)) && dun_level)
1072                         {
1073                                 bool ok = TRUE;
1074
1075                                 /* Extract stair position */
1076                                 cave_type *c_ptr = &cave[py][px];
1077
1078                                 /*** Create connected stairs ***/
1079
1080                                 /* No stairs down from Quest */
1081                                 if ((change_floor_mode & CFM_UP) && !quest_number(dun_level))
1082                                 {
1083                                         if (change_floor_mode & CFM_SHAFT)
1084                                                 c_ptr->feat = FEAT_MORE_MORE;
1085                                         else
1086                                                 c_ptr->feat = FEAT_MORE;
1087                                 }
1088
1089                                 /* No stairs up when ironman_downward */
1090                                 else if ((change_floor_mode & CFM_DOWN) && !ironman_downward)
1091                                 {
1092                                         if (change_floor_mode & CFM_SHAFT)
1093                                                 c_ptr->feat = FEAT_LESS_LESS;
1094                                         else
1095                                                 c_ptr->feat = FEAT_LESS;
1096                                 }
1097                                 else
1098                                 {
1099                                         /* Hum??? */
1100                                         ok = FALSE;
1101                                 }
1102
1103                                 if (ok)
1104                                 {
1105                                         /* Paranoia -- Clear mimic */
1106                                         c_ptr->mimic = 0;
1107
1108                                         /* Connect to previous floor */
1109                                         c_ptr->special = p_ptr->floor_id;
1110                                 }
1111                         }
1112                 }
1113
1114                 /* Arrive at random grid */
1115                 if (change_floor_mode & (CFM_RAND_PLACE))
1116                 {
1117                         (void)new_player_spot();
1118                 }
1119
1120                 /* You see stairs blocked */
1121                 else if (change_floor_mode & CFM_NO_RETURN)
1122                 {
1123                         if (!p_ptr->blind)
1124                         {
1125 #ifdef JP
1126                                 msg_print("ÆÍÁ³³¬Ãʤ¬ºÉ¤¬¤ì¤Æ¤·¤Þ¤Ã¤¿¡£");
1127 #else
1128                                 msg_print("Suddenly the stairs is blocked!");
1129 #endif
1130                         }
1131                         else
1132                         {
1133 #ifdef JP
1134                                 msg_print("¥´¥È¥´¥È¤È²¿¤«²»¤¬¤·¤¿¡£");
1135 #else
1136                                 msg_print("You hear some noises.");
1137 #endif
1138                         }
1139                 }
1140
1141                 /*
1142                  * Update visit mark
1143                  *
1144                  * The "turn" is not always different number because
1145                  * the level teleport doesn't take any turn.  Use
1146                  * visit mark instead of last visit turn to find the
1147                  * oldest saved floor.
1148                  */
1149                 sf_ptr->visit_mark = latest_visit_mark++;
1150         }
1151
1152         /* Place preserved pet monsters */
1153         place_pet();
1154
1155         /* Hack -- maintain unique and artifacts */
1156         update_unique_artifact(new_floor_id);
1157
1158         /* Now the player is in new floor */
1159         p_ptr->floor_id = new_floor_id;
1160
1161         /* The dungeon is ready */
1162         character_dungeon = TRUE;
1163
1164         /* Hack -- Munchkin characters always get whole map */
1165         if (p_ptr->pseikaku == SEIKAKU_MUNCHKIN)
1166                 wiz_lite(TRUE, (bool)(p_ptr->pclass == CLASS_NINJA));
1167
1168         /* Remember when this level was "created" */
1169         old_turn = turn;
1170
1171         /* Clear all flags */
1172         change_floor_mode = 0L;
1173 }
1174
1175
1176
1177 /*
1178  * Create stairs at or move previously created stairs into the player
1179  * location.
1180  */
1181 void stair_creation(void)
1182 {
1183         saved_floor_type *sf_ptr;
1184         saved_floor_type *dest_sf_ptr;
1185
1186         bool up = TRUE;
1187         bool down = TRUE;
1188         s16b dest_floor_id = 0;
1189
1190
1191         /* Forbid up staircases on Ironman mode */
1192         if (ironman_downward) up = FALSE;
1193
1194         /* Forbid down staircases on quest level */
1195         if (quest_number(dun_level) || (dun_level >= d_info[dungeon_type].maxdepth)) down = FALSE;
1196
1197         /* No effect out of standard dungeon floor */
1198         if (!dun_level || (!up && !down) ||
1199             (p_ptr->inside_quest && (p_ptr->inside_quest < MIN_RANDOM_QUEST)) ||
1200             p_ptr->inside_arena || p_ptr->inside_battle)
1201         {
1202                 /* arena or quest */
1203 #ifdef JP
1204                 msg_print("¸ú²Ì¤¬¤¢¤ê¤Þ¤»¤ó¡ª");
1205 #else
1206                 msg_print("There is no effect!");
1207 #endif
1208                 return;
1209         }
1210
1211         /* Artifacts resists */
1212         if (!cave_valid_bold(py, px))
1213         {
1214 #ifdef JP
1215                 msg_print("¾²¾å¤Î¥¢¥¤¥Æ¥à¤¬¼öʸ¤òÄ·¤ÍÊÖ¤·¤¿¡£");
1216 #else
1217                 msg_print("The object resists the spell.");
1218 #endif
1219
1220                 return;
1221         }
1222
1223         /* Destroy all objects in the grid */
1224         delete_object(py, px);
1225
1226         /* Extract current floor data */
1227         sf_ptr = get_sf_ptr(p_ptr->floor_id);
1228
1229         /* Choose randomly */
1230         if (up && down)
1231         {
1232                 if (randint0(100) < 50) up = FALSE;
1233                 else down = FALSE;
1234         }
1235
1236         /* Destination is already fixed */
1237         if (up)
1238         {
1239                 if (sf_ptr->upper_floor_id) dest_floor_id = sf_ptr->upper_floor_id;
1240         }
1241         else
1242         {
1243                 if (sf_ptr->lower_floor_id) dest_floor_id = sf_ptr->lower_floor_id;
1244         }
1245
1246
1247         /* Search old stairs leading to the destination */
1248         if (dest_floor_id)
1249         {
1250                 int x, y;
1251
1252                 for (y = 0; y < cur_hgt; y++)
1253                 {
1254                         for (x = 0; x < cur_wid; x++)
1255                         {
1256                                 cave_type *c_ptr = &cave[y][x];
1257                                 bool ok = FALSE;
1258
1259                                 if (!c_ptr->special) continue;
1260                                 if (c_ptr->special != dest_floor_id) continue;
1261
1262                                 /* Remove old stairs */
1263                                 c_ptr->special = 0;
1264                                 cave_set_feat(y, x, floor_type[randint0(100)]);
1265                         }
1266                 }
1267         }
1268
1269         /* No old destination -- Get new one now */
1270         else
1271         {
1272                 dest_floor_id = get_new_floor_id();
1273
1274                 /* Fix it */
1275                 if (up)
1276                         sf_ptr->upper_floor_id = dest_floor_id;
1277                 else
1278                         sf_ptr->lower_floor_id = dest_floor_id;
1279         }
1280
1281         /* Extract destination floor data */
1282         dest_sf_ptr = get_sf_ptr(dest_floor_id);
1283
1284
1285         /* Create a staircase */
1286         if (up)
1287         {
1288                 if (dest_sf_ptr->last_visit && dest_sf_ptr->dun_level <= dun_level - 2)
1289                         cave_set_feat(py, px, FEAT_LESS_LESS);
1290                 else
1291                         cave_set_feat(py, px, FEAT_LESS);
1292         }
1293         else
1294         {
1295                 if (dest_sf_ptr->last_visit && dest_sf_ptr->dun_level >= dun_level + 2)
1296                         cave_set_feat(py, px, FEAT_MORE_MORE);
1297                 else
1298                         cave_set_feat(py, px, FEAT_MORE);
1299         }
1300
1301
1302         /* Connect this stairs to the destination */
1303         cave[py][px].special = dest_floor_id;
1304 }
1305
1306