OSDN Git Service

vanilla_townでスタート地点の階段が荒野への階段になったり, lite_townで
[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  * Is this feature has special meaning (except floor_id) with c_ptr->special?
733  */
734 static bool feat_uses_special(byte feat)
735 {
736         switch (feat)
737         {
738         case FEAT_QUEST_ENTER:
739         case FEAT_QUEST_EXIT:
740         case FEAT_QUEST_DOWN:
741         case FEAT_QUEST_UP:
742         case FEAT_TOWN:
743         case FEAT_ENTRANCE:
744                 return TRUE;
745         }
746
747         return FALSE;
748 }
749
750
751 /*
752  * Maintain quest monsters, mark next floor_id at stairs, save current
753  * floor, and prepare to enter next floor.
754  */
755 void leave_floor(void)
756 {
757         cave_type *c_ptr = NULL;
758         saved_floor_type *sf_ptr;
759         int quest_r_idx = 0;
760         int i;
761
762         /* Preserve pets and prepare to take these to next floor */
763         preserve_pet();
764
765         /* Remove all mirrors without explosion */
766         remove_all_mirrors(FALSE);
767
768         /* New floor is not yet prepared */
769         new_floor_id = 0;
770
771
772         if (!p_ptr->floor_id)
773         {
774                 if (change_floor_mode & (CFM_NO_RETURN | CFM_CLEAR_ALL))
775                 {
776                         /* No need to save current floor */
777                         return;
778                 }
779                 else
780                 {
781                         /* Get temporal floor_id */
782                         p_ptr->floor_id = get_new_floor_id();
783
784                         /* Record the dungeon level */
785                         get_sf_ptr(p_ptr->floor_id)->dun_level = dun_level;
786                 }
787         }
788
789
790         /* Search the quest monster index */
791         for (i = 0; i < max_quests; i++)
792         {
793                 if ((quest[i].status == QUEST_STATUS_TAKEN) &&
794                     ((quest[i].type == QUEST_TYPE_KILL_LEVEL) ||
795                     (quest[i].type == QUEST_TYPE_RANDOM)) &&
796                     (quest[i].level == dun_level) &&
797                     (dungeon_type == quest[i].dungeon) &&
798                     !(quest[i].flags & QUEST_FLAG_PRESET))
799                 {
800                         quest_r_idx = quest[i].r_idx;
801                 }
802         }
803
804         /* Maintain quest monsters */
805         for (i = 1; i < m_max; i++)
806         {
807                 monster_race *r_ptr;
808                 monster_type *m_ptr = &m_list[i];
809
810                 /* Skip dead monsters */
811                 if (!m_ptr->r_idx) continue;
812
813                 /* Only maintain quest monsters */
814                 if (quest_r_idx != m_ptr->r_idx) continue;
815
816                 /* Extract real monster race */
817                 r_ptr = real_r_ptr(m_ptr);
818
819                 /* Ignore unique monsters */
820                 if ((r_ptr->flags1 & RF1_UNIQUE) ||
821                     (r_ptr->flags7 & RF7_NAZGUL)) continue;
822
823                 /* Delete non-unique quest monsters */
824                 delete_monster_idx(i);
825         }
826
827         /* Check if there is a same item */
828         for (i = 0; i < INVEN_PACK; i++)
829         {
830                 object_type *o_ptr = &inventory[i];
831
832                 /* Skip dead objects */
833                 if (!o_ptr->k_idx) continue;
834
835                 /* Delete old memorized location of the artifact */
836                 if (artifact_p(o_ptr))
837                 {
838                         a_info[o_ptr->name1].floor_id = 0;
839                 }
840         }
841
842         /* Extract current floor info or NULL */
843         sf_ptr = get_sf_ptr(p_ptr->floor_id);
844
845         /* Choose random stairs */
846         if ((change_floor_mode & CFM_RAND_CONNECT) && p_ptr->floor_id)
847         {
848                 int x, y, sx = 0, sy = 0;
849                 int x_table[20];
850                 int y_table[20];
851                 int num = 0;
852                 int i;
853
854                 /* Search usable stairs */
855                 for (y = 0; y < cur_hgt; y++)
856                 {
857                         for (x = 0; x < cur_wid; x++)
858                         {
859                                 cave_type *c_ptr = &cave[y][x];
860                                 bool ok = FALSE;
861
862                                 if (change_floor_mode & CFM_UP)
863                                 {
864                                         /* Found fixed stairs */
865                                         if (c_ptr->special &&
866                                             c_ptr->special == sf_ptr->upper_floor_id)
867                                         {
868                                                 sx = x;
869                                                 sy = y;
870                                         }
871
872                                         if (c_ptr->feat == FEAT_LESS ||
873                                             c_ptr->feat == FEAT_LESS_LESS)
874                                                 ok = TRUE;
875                                 }
876                                 else if (change_floor_mode & CFM_DOWN)
877                                 {
878                                         /* Found fixed stairs */
879                                         if (c_ptr->special &&
880                                             c_ptr->special == sf_ptr->lower_floor_id)
881                                         {
882                                                 sx = x;
883                                                 sy = y;
884                                         }
885
886                                         if (c_ptr->feat == FEAT_MORE ||
887                                             c_ptr->feat == FEAT_MORE_MORE)
888                                                 ok = TRUE;
889                                 }
890
891                                 if (ok && num < 20)
892                                 {
893                                         x_table[num] = x;
894                                         y_table[num] = y;
895                                         num++;
896                                 }
897                         }
898                 }
899
900                 if (sx)
901                 {
902                         /* Already fixed */
903                         py = sy;
904                         px = sx;
905                 }
906                 else if (!num)
907                 {
908                         /* No stairs found! -- No return */
909                         prepare_change_floor_mode(CFM_RAND_PLACE | CFM_NO_RETURN);
910
911                         /* Mega Hack -- It's not the stairs you enter.  Disable it.  */
912                         if (!feat_uses_special(cave[py][px].feat)) cave[py][px].special = 0;
913                 }
914                 else
915                 {
916                         /* Choose random one */
917                         i = randint0(num);
918
919                         /* Point stair location */
920                         py = y_table[i];
921                         px = x_table[i];
922                 }
923         }
924
925         /* Extract new dungeon level */
926         if (!(change_floor_mode & CFM_CLEAR_ALL))
927         {
928                 int move_num = 0;
929
930                 /* Extract stair position */
931                 c_ptr = &cave[py][px];
932
933                 /* Get back to old saved floor? */
934                 if (c_ptr->special && !feat_uses_special(c_ptr->feat) && get_sf_ptr(c_ptr->special))
935                 {
936                         /* Saved floor is exist.  Use it. */
937                         new_floor_id = c_ptr->special;
938                 }
939
940                 /* Extract level movement number */
941                 if (change_floor_mode & CFM_DOWN) move_num = 1;
942                 else if (change_floor_mode & CFM_UP) move_num = -1;
943
944                 /* Mark shaft up/down */
945                 if (c_ptr->feat == FEAT_LESS_LESS ||
946                     c_ptr->feat == FEAT_MORE_MORE)
947                 {
948                         prepare_change_floor_mode(CFM_SHAFT);
949                         move_num += SGN(move_num);
950                 }
951
952                 /* Get out from or Enter the dungeon */
953                 if (change_floor_mode & CFM_DOWN)
954                 {
955                         /* Hack -- Prevent "wild to wild stair" */
956                         if (!dun_level)
957                                 move_num = d_info[(c_ptr->feat == FEAT_ENTRANCE) ? c_ptr->special : DUNGEON_ANGBAND].mindepth;
958                 }
959                 else if (change_floor_mode & CFM_UP)
960                 {
961                         if (dun_level + move_num < d_info[dungeon_type].mindepth)
962                                 move_num = -dun_level;
963                 }
964
965                 dun_level += move_num;
966         }
967
968         /* Leaving the dungeon to town */
969         if (!dun_level && dungeon_type)
970         {
971                 p_ptr->leaving_dungeon = TRUE;
972                 if (!vanilla_town && !lite_town)
973                 {
974                         p_ptr->wilderness_y = d_info[dungeon_type].dy;
975                         p_ptr->wilderness_x = d_info[dungeon_type].dx;
976                 }
977                 p_ptr->recall_dungeon = dungeon_type;
978                 dungeon_type = 0;
979
980                 /* Reach to the surface -- Clear all saved floors */
981                 prepare_change_floor_mode(CFM_CLEAR_ALL);
982         }
983
984         if (change_floor_mode & CFM_CLEAR_ALL)
985         {
986                 int i;
987
988                 /* Kill all saved floors */
989                 for (i = 0; i < MAX_SAVED_FLOORS; i++)
990                         kill_saved_floor(&saved_floors[i]);
991
992                 /* Reset visit_mark count */
993                 latest_visit_mark = 1;
994         }
995         else if (change_floor_mode & CFM_NO_RETURN)
996         {
997                 /* Kill current floor */
998                 kill_saved_floor(sf_ptr);
999         }
1000
1001         /* No current floor -- Left/Enter dungeon etc... */
1002         if (!p_ptr->floor_id)
1003         {
1004                 /* No longer need to save current floor */
1005                 return;
1006         }
1007
1008
1009         /* Mark next floor_id on the previous floor */
1010         if (!new_floor_id)
1011         {
1012                 /* Get new id */
1013                 new_floor_id = get_new_floor_id();
1014
1015                 /* Connect from here */
1016                 if (c_ptr && !feat_uses_special(c_ptr->feat))
1017                 {
1018                         c_ptr->special = new_floor_id;
1019                 }
1020
1021                 /* Record new dungeon level */
1022                 get_sf_ptr(new_floor_id)->dun_level = dun_level;
1023         }
1024
1025         /* Fix connection -- level teleportation or trap door */
1026         if (change_floor_mode & CFM_RAND_CONNECT)
1027         {
1028                 if (change_floor_mode & CFM_UP)
1029                         sf_ptr->upper_floor_id = new_floor_id;
1030                 else if (change_floor_mode & CFM_DOWN)
1031                         sf_ptr->lower_floor_id = new_floor_id;
1032         }
1033
1034         /* If you can return, you need to save previous floor */
1035         if (!(change_floor_mode & (CFM_NO_RETURN | CFM_CLEAR_ALL)))
1036         {
1037                 /* Get out of the my way! */
1038                 get_out_monster();
1039
1040                 /* Record the last visit turn of current floor */
1041                 sf_ptr->last_visit = turn;
1042
1043                 /* Forget the lite */
1044                 forget_lite();
1045
1046                 /* Forget the view */
1047                 forget_view();
1048
1049                 /* Forget the view */
1050                 clear_mon_lite();
1051
1052                 /* Save current floor */
1053                 if (!save_floor(sf_ptr, 0))
1054                 {
1055                         /* Save failed -- No return */
1056                         prepare_change_floor_mode(CFM_NO_RETURN);
1057
1058                         /* Kill current floor */
1059                         kill_saved_floor(get_sf_ptr(p_ptr->floor_id));
1060                 }
1061         }
1062 }
1063
1064
1065 /*
1066  * Enter new floor.  If the floor is an old saved floor, it will be
1067  * restored from the temporal file.  If the floor is new one, new cave
1068  * will be generated.
1069  */
1070 void change_floor(void)
1071 {
1072         saved_floor_type *sf_ptr;
1073         bool loaded = FALSE;
1074
1075         /* The dungeon is not ready */
1076         character_dungeon = FALSE;
1077
1078         /* No longer in the trap detecteded region */
1079         p_ptr->dtrap = FALSE;
1080
1081         /* Mega-Hack -- no panel yet */
1082         panel_row_min = 0;
1083         panel_row_max = 0;
1084         panel_col_min = 0;
1085         panel_col_max = 0;
1086
1087         /* Mega-Hack -- not ambushed on the wildness? */
1088         ambush_flag = FALSE;
1089
1090         /* No saved floors (On the surface etc.) */
1091         if (change_floor_mode & CFM_CLEAR_ALL)
1092         {
1093                 /* Create cave */
1094                 generate_cave();
1095
1096                 /* Paranoia -- No new saved floor */
1097                 new_floor_id = 0;
1098         }
1099
1100         /* In the dungeon */
1101         else
1102         {
1103                 /* No floor_id yet */
1104                 if (!new_floor_id)
1105                 {
1106                         /* Get new id */
1107                         new_floor_id = get_new_floor_id();
1108                 }
1109
1110                 /* Pointer for infomations of new floor */
1111                 sf_ptr = get_sf_ptr(new_floor_id);
1112
1113                 /* Try to restore old floor */
1114                 if (sf_ptr->last_visit)
1115                 {
1116                         /* Old saved floor is exist */
1117                         if (load_floor(sf_ptr, 0))
1118                         {
1119                                 loaded = TRUE;
1120
1121                                 /* Forbid return stairs */
1122                                 if (change_floor_mode & CFM_NO_RETURN)
1123                                 {
1124                                         cave_type *c_ptr = &cave[py][px];
1125
1126                                         if (!feat_uses_special(c_ptr->feat))
1127                                         {
1128                                                 if (change_floor_mode & (CFM_DOWN | CFM_UP))
1129                                                 {
1130                                                         /* Reset to floor */
1131                                                         c_ptr->feat = floor_type[randint0(100)];
1132                                                 }
1133
1134                                                 c_ptr->special = 0;
1135                                         }
1136                                 }
1137                         }
1138                 }
1139
1140                 /*
1141                  * Set lower/upper_floor_id of new floor when the new
1142                  * floor is right-above/right-under the current floor.
1143                  *
1144                  * Stair creation/Teleport level/Trap door will take
1145                  * you the same floor when you used it later again.
1146                  */
1147                 if (p_ptr->floor_id)
1148                 {
1149                         saved_floor_type *cur_sf_ptr = get_sf_ptr(p_ptr->floor_id);
1150
1151                         if (change_floor_mode & CFM_UP)
1152                         {
1153                                 /* New floor is right-above */
1154                                 if (cur_sf_ptr->upper_floor_id == new_floor_id)
1155                                         sf_ptr->lower_floor_id = p_ptr->floor_id;
1156                         }
1157                         else if (change_floor_mode & CFM_DOWN)
1158                         {
1159                                 /* New floor is right-under */
1160                                 if (cur_sf_ptr->lower_floor_id == new_floor_id)
1161                                         sf_ptr->upper_floor_id = p_ptr->floor_id;
1162                         }
1163                 }
1164
1165                 /* Maintain monsters and artifacts */
1166                 if (loaded)
1167                 {
1168                         int i;
1169                         s32b absence_ticks = (turn - sf_ptr->last_visit) / TURNS_PER_TICK;
1170                         int alloc_chance = d_info[dungeon_type].max_m_alloc_chance;
1171                         int alloc_times;
1172
1173                         /* Maintain monsters */
1174                         for (i = 1; i < m_max; i++)
1175                         {
1176                                 monster_race *r_ptr;
1177                                 monster_type *m_ptr = &m_list[i];
1178
1179                                 /* Skip dead monsters */
1180                                 if (!m_ptr->r_idx) continue;
1181
1182                                 if (!is_pet(m_ptr))
1183                                 {
1184                                         /* Restore HP */
1185                                         m_ptr->hp = m_ptr->maxhp = m_ptr->max_maxhp;
1186
1187                                         /* Remove fear */
1188                                         m_ptr->monfear = 0;
1189
1190                                         /* Remove invulnerability */
1191                                         m_ptr->invulner = 0;
1192
1193                                         /* Remove fast status */
1194                                         m_ptr->fast = 0;
1195
1196                                         /* Remove slow status */
1197                                         m_ptr->slow = 0;
1198
1199                                         /* Remove stun */
1200                                         m_ptr->stunned = 0;
1201
1202                                         /* Remove confusion */
1203                                         m_ptr->confused = 0;
1204                                 }
1205
1206                                 /* Extract real monster race */
1207                                 r_ptr = real_r_ptr(m_ptr);
1208
1209                                 /* Ignore non-unique */
1210                                 if (!(r_ptr->flags1 & RF1_UNIQUE) &&
1211                                     !(r_ptr->flags7 & RF7_NAZGUL)) continue;
1212
1213                                 /* Appear at a different floor? */
1214                                 if (r_ptr->floor_id != new_floor_id)
1215                                 {
1216                                         /* Disapper from here */
1217                                         delete_monster_idx(i);
1218                                 }
1219                         }
1220
1221                         /* Maintain artifatcs */
1222                         for (i = 1; i < o_max; i++)
1223                         {
1224                                 object_type *o_ptr = &o_list[i];
1225
1226                                 /* Skip dead objects */
1227                                 if (!o_ptr->k_idx) continue;
1228
1229                                 /* Ignore non-artifact */
1230                                 if (!artifact_p(o_ptr)) continue;
1231
1232                                 /* Appear at a different floor? */
1233                                 if (a_info[o_ptr->name1].floor_id != new_floor_id)
1234                                 {
1235                                         /* Disappear from here */
1236                                         delete_object_idx(i);
1237                                 }
1238                                 else
1239                                 {
1240                                         /* Cancel preserve */
1241                                         a_info[o_ptr->name1].cur_num = 1;
1242                                 }
1243                         }
1244
1245                         place_quest_monsters();
1246
1247                         /* Place some random monsters */
1248                         alloc_times = absence_ticks / alloc_chance;
1249
1250                         if (randint0(alloc_chance) < (absence_ticks % alloc_chance))
1251                                 alloc_times++;
1252
1253                         for (i = 0; i < alloc_times; i++)
1254                         {
1255                                 /* Make a (group of) new monster */
1256                                 (void)alloc_monster(0, 0);
1257                         }
1258                 }
1259
1260                 /* New floor_id or failed to restore */
1261                 else /* if (!loaded) */
1262                 {
1263                         if (sf_ptr->last_visit)
1264                         {
1265                                 /* Temporal file is broken? */
1266 #ifdef JP
1267                                 msg_print("³¬ÃʤϹԤ­»ß¤Þ¤ê¤À¤Ã¤¿¡£");
1268 #else
1269                                 msg_print("The staircases come to a dead end...");
1270 #endif
1271
1272                                 /* Create simple dead end */
1273                                 build_dead_end();
1274
1275                                 /* Break connection */
1276                                 if (change_floor_mode & CFM_UP)
1277                                 {
1278                                         sf_ptr->upper_floor_id = 0;
1279                                 }
1280                                 else if (change_floor_mode & CFM_DOWN)
1281                                 {
1282                                         sf_ptr->lower_floor_id = 0;
1283                                 }
1284                         }
1285                         else
1286                         {
1287                                 /* Newly create cave */
1288                                 generate_cave();
1289                         }
1290
1291                         /* Record last visit turn */
1292                         sf_ptr->last_visit = turn;
1293
1294                         /* Set correct dun_level value */
1295                         sf_ptr->dun_level = dun_level;
1296
1297                         /* Create connected stairs */
1298                         if (!(change_floor_mode & (CFM_NO_RETURN | CFM_CLEAR_ALL)))
1299                         {
1300                                 /* Extract stair position */
1301                                 cave_type *c_ptr = &cave[py][px];
1302
1303                                 /*** Create connected stairs ***/
1304
1305                                 /* No stairs down from Quest */
1306                                 if ((change_floor_mode & CFM_UP) && !quest_number(dun_level))
1307                                 {
1308                                         if (change_floor_mode & CFM_SHAFT)
1309                                                 c_ptr->feat = FEAT_MORE_MORE;
1310                                         else
1311                                                 c_ptr->feat = FEAT_MORE;
1312                                 }
1313
1314                                 /* No stairs up when ironman_downward */
1315                                 else if ((change_floor_mode & CFM_DOWN) && !ironman_downward)
1316                                 {
1317                                         if (change_floor_mode & CFM_SHAFT)
1318                                                 c_ptr->feat = FEAT_LESS_LESS;
1319                                         else
1320                                                 c_ptr->feat = FEAT_LESS;
1321                                 }
1322
1323                                 if (!feat_uses_special(c_ptr->feat))
1324                                 {
1325                                         /* Paranoia -- Clear mimic */
1326                                         c_ptr->mimic = 0;
1327
1328                                         /* Connect to previous floor */
1329                                         c_ptr->special = p_ptr->floor_id;
1330                                 }
1331                         }
1332                 }
1333
1334                 /* Arrive at random grid */
1335                 if (change_floor_mode & (CFM_RAND_PLACE))
1336                 {
1337                         (void)new_player_spot();
1338                 }
1339
1340                 /* You see stairs blocked */
1341                 else if ((change_floor_mode & CFM_NO_RETURN) &&
1342                          (change_floor_mode & (CFM_DOWN | CFM_UP)))
1343                 {
1344                         if (!p_ptr->blind)
1345                         {
1346 #ifdef JP
1347                                 msg_print("ÆÍÁ³³¬Ãʤ¬ºÉ¤¬¤ì¤Æ¤·¤Þ¤Ã¤¿¡£");
1348 #else
1349                                 msg_print("Suddenly the stairs is blocked!");
1350 #endif
1351                         }
1352                         else
1353                         {
1354 #ifdef JP
1355                                 msg_print("¥´¥È¥´¥È¤È²¿¤«²»¤¬¤·¤¿¡£");
1356 #else
1357                                 msg_print("You hear some noises.");
1358 #endif
1359                         }
1360                 }
1361
1362                 /*
1363                  * Update visit mark
1364                  *
1365                  * The "turn" is not always different number because
1366                  * the level teleport doesn't take any turn.  Use
1367                  * visit mark instead of last visit turn to find the
1368                  * oldest saved floor.
1369                  */
1370                 sf_ptr->visit_mark = latest_visit_mark++;
1371         }
1372
1373         /* Place preserved pet monsters */
1374         place_pet();
1375
1376         /* Hack -- maintain unique and artifacts */
1377         update_unique_artifact(new_floor_id);
1378
1379         /* Now the player is in new floor */
1380         p_ptr->floor_id = new_floor_id;
1381
1382         /* The dungeon is ready */
1383         character_dungeon = TRUE;
1384
1385         /* Hack -- Munchkin characters always get whole map */
1386         if (p_ptr->pseikaku == SEIKAKU_MUNCHKIN)
1387                 wiz_lite((bool)(p_ptr->pclass == CLASS_NINJA));
1388
1389         /* Remember when this level was "created" */
1390         old_turn = turn;
1391
1392         /* Clear all flags */
1393         change_floor_mode = 0L;
1394 }
1395
1396
1397
1398 /*
1399  * Create stairs at or move previously created stairs into the player
1400  * location.
1401  */
1402 void stair_creation(void)
1403 {
1404         saved_floor_type *sf_ptr;
1405         saved_floor_type *dest_sf_ptr;
1406
1407         bool up = TRUE;
1408         bool down = TRUE;
1409         s16b dest_floor_id = 0;
1410
1411
1412         /* Forbid up staircases on Ironman mode */
1413         if (ironman_downward) up = FALSE;
1414
1415         /* Forbid down staircases on quest level */
1416         if (quest_number(dun_level) || (dun_level >= d_info[dungeon_type].maxdepth)) down = FALSE;
1417
1418         /* No effect out of standard dungeon floor */
1419         if (!dun_level || (!up && !down) ||
1420             (p_ptr->inside_quest && is_fixed_quest_idx(p_ptr->inside_quest)) ||
1421             p_ptr->inside_arena || p_ptr->inside_battle)
1422         {
1423                 /* arena or quest */
1424 #ifdef JP
1425                 msg_print("¸ú²Ì¤¬¤¢¤ê¤Þ¤»¤ó¡ª");
1426 #else
1427                 msg_print("There is no effect!");
1428 #endif
1429                 return;
1430         }
1431
1432         /* Artifacts resists */
1433         if (!cave_valid_bold(py, px))
1434         {
1435 #ifdef JP
1436                 msg_print("¾²¾å¤Î¥¢¥¤¥Æ¥à¤¬¼öʸ¤òÄ·¤ÍÊÖ¤·¤¿¡£");
1437 #else
1438                 msg_print("The object resists the spell.");
1439 #endif
1440
1441                 return;
1442         }
1443
1444         /* Destroy all objects in the grid */
1445         delete_object(py, px);
1446
1447         /* Extract current floor data */
1448         sf_ptr = get_sf_ptr(p_ptr->floor_id);
1449
1450         /* Choose randomly */
1451         if (up && down)
1452         {
1453                 if (randint0(100) < 50) up = FALSE;
1454                 else down = FALSE;
1455         }
1456
1457         /* Destination is already fixed */
1458         if (up)
1459         {
1460                 if (sf_ptr->upper_floor_id) dest_floor_id = sf_ptr->upper_floor_id;
1461         }
1462         else
1463         {
1464                 if (sf_ptr->lower_floor_id) dest_floor_id = sf_ptr->lower_floor_id;
1465         }
1466
1467
1468         /* Search old stairs leading to the destination */
1469         if (dest_floor_id)
1470         {
1471                 int x, y;
1472
1473                 for (y = 0; y < cur_hgt; y++)
1474                 {
1475                         for (x = 0; x < cur_wid; x++)
1476                         {
1477                                 cave_type *c_ptr = &cave[y][x];
1478
1479                                 if (!c_ptr->special) continue;
1480                                 if (feat_uses_special(c_ptr->feat)) continue;
1481                                 if (c_ptr->special != dest_floor_id) continue;
1482
1483                                 /* Remove old stairs */
1484                                 c_ptr->special = 0;
1485                                 cave_set_feat(y, x, floor_type[randint0(100)]);
1486                         }
1487                 }
1488         }
1489
1490         /* No old destination -- Get new one now */
1491         else
1492         {
1493                 dest_floor_id = get_new_floor_id();
1494
1495                 /* Fix it */
1496                 if (up)
1497                         sf_ptr->upper_floor_id = dest_floor_id;
1498                 else
1499                         sf_ptr->lower_floor_id = dest_floor_id;
1500         }
1501
1502         /* Extract destination floor data */
1503         dest_sf_ptr = get_sf_ptr(dest_floor_id);
1504
1505
1506         /* Create a staircase */
1507         if (up)
1508         {
1509                 if (dest_sf_ptr->last_visit && dest_sf_ptr->dun_level <= dun_level - 2)
1510                         cave_set_feat(py, px, FEAT_LESS_LESS);
1511                 else
1512                         cave_set_feat(py, px, FEAT_LESS);
1513         }
1514         else
1515         {
1516                 if (dest_sf_ptr->last_visit && dest_sf_ptr->dun_level >= dun_level + 2)
1517                         cave_set_feat(py, px, FEAT_MORE_MORE);
1518                 else
1519                         cave_set_feat(py, px, FEAT_MORE);
1520         }
1521
1522
1523         /* Connect this stairs to the destination */
1524         cave[py][px].special = dest_floor_id;
1525 }