OSDN Git Service

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