OSDN Git Service

次の場合に, ペットになっているユニークモンスターと同じモンスターが生
[hengband/hengband.git] / src / floors.c
1 /* File: floors.c */
2
3 /* Purpose: management of the saved floor */
4
5 /*
6  * Copyright (c) 2002  Mogami
7  *
8  * This software may be copied and distributed for educational, research, and
9  * not for profit purposes provided that this copyright and statement are
10  * included in all such copies.
11  */
12
13 #include "angband.h"
14 #include "grid.h"
15
16
17 static s16b new_floor_id;       /* floor_id of the destination */
18 static u32b change_floor_mode;  /* Mode flags for changing floor */
19 static u32b latest_visit_mark;  /* Max number of visit_mark */
20
21
22 /*
23  * Initialize saved_floors array.  Make sure that old temporal files
24  * are not remaining as gurbages.
25  */
26 void init_saved_floors(void)
27 {
28         char floor_savefile[1024];
29         int i;
30         int fd = -1;
31         int mode = 0644;
32         bool force = FALSE;
33
34 #ifdef SET_UID
35 # ifdef SECURE
36         /* Get "games" permissions */
37         beGames();
38 # endif
39 #endif
40
41         for (i = 0; i < MAX_SAVED_FLOORS; i++)
42         {
43                 saved_floor_type *sf_ptr = &saved_floors[i];
44
45                 /* File name */
46                 sprintf(floor_savefile, "%s.F%02d", savefile, i);
47
48                 /* 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                 COPY(&party_mon[0], &m_list[p_ptr->riding], monster_type);
353
354                 /* Delete from this floor */
355                 delete_monster_idx(p_ptr->riding);
356         }
357
358         /*
359          * If player is in wild mode, no pets are preserved
360          * except a monster whom player riding
361          */
362         if (!p_ptr->wild_mode)
363         {
364                 for (i = m_max - 1, num = 1; (i >= 1 && num < MAX_PARTY_MON); i--)
365                 {
366                         monster_type *m_ptr = &m_list[i];
367
368                         if (!m_ptr->r_idx) continue;
369                         if (!is_pet(m_ptr)) continue;
370                         if (i == p_ptr->riding) continue;
371
372                         if (reinit_wilderness)
373                         {
374                                 /* Don't lose sight of pets when getting a Quest */
375                         }
376                         else
377                         {
378                                 int dis = distance(py, px, m_ptr->fy, m_ptr->fx);
379
380                                 /*
381                                  * Pets with nickname will follow even from 3 blocks away
382                                  * when you or the pet can see the other.
383                                  */
384                                 if (m_ptr->nickname && 
385                                     (player_has_los_bold(m_ptr->fy, m_ptr->fx) ||
386                                      los(m_ptr->fy, m_ptr->fx, py, px)))
387                                 {
388                                         if (dis > 3) continue;
389                                 }
390                                 else
391                                 {
392                                         if (dis > 1) continue;
393                                 }
394                                 if (m_ptr->confused || m_ptr->stunned || m_ptr->csleep) continue;
395                         }
396
397                         COPY(&party_mon[num], &m_list[i], monster_type);
398                         num++;
399
400                         /* Delete from this floor */
401                         delete_monster_idx(i);
402                 }
403         }
404
405         if (record_named_pet)
406         {
407                 for (i = m_max - 1; i >=1; i--)
408                 {
409                         monster_type *m_ptr = &m_list[i];
410                         char m_name[80];
411
412                         if (!m_ptr->r_idx) continue;
413                         if (!is_pet(m_ptr)) continue;
414                         if (!m_ptr->nickname) continue;
415                         if (p_ptr->riding == i) continue;
416
417                         monster_desc(m_name, m_ptr, 0x88);
418                         do_cmd_write_nikki(NIKKI_NAMED_PET, 4, m_name);
419                 }
420         }
421 }
422
423
424 /*
425  * Place preserved pet monsters on new floor
426  */
427 static void place_pet(void)
428 {
429         int i, max_num;
430
431         if (p_ptr->wild_mode) max_num = 1;
432         else max_num = MAX_PARTY_MON;
433
434         for (i = 0; i < max_num; i++)
435         {
436                 int cy, cx, m_idx;
437
438                 if (!(party_mon[i].r_idx)) continue;
439
440                 if (i == 0)
441                 {
442                         m_idx = m_pop();
443                         p_ptr->riding = m_idx;
444                         if (m_idx)
445                         {
446                                 cy = py;
447                                 cx = px;
448                         }
449                 }
450                 else
451                 {
452                         int j, d;
453
454                         for (d = 1; d < 6; d++)
455                         {
456                                 for (j = 1000; j > 0; j--)
457                                 {
458                                         scatter(&cy, &cx, py, px, d, 0);
459                                         if ((cave_floor_bold(cy, cx) || (cave[cy][cx].feat == FEAT_TREES)) && !cave[cy][cx].m_idx && !((cy == py) && (cx == px))) break;
460                                 }
461                                 if (j) break;
462                         }
463                         if (d == 6 || p_ptr->inside_arena || p_ptr->inside_battle)
464                                 m_idx = 0;
465                         else
466                                 m_idx = m_pop();
467                 }
468
469                 if (m_idx)
470                 {
471                         monster_type *m_ptr = &m_list[m_idx];
472                         monster_race *r_ptr;
473
474                         cave[cy][cx].m_idx = m_idx;
475
476                         m_ptr->r_idx = party_mon[i].r_idx;
477
478                         /* Copy all member of the structure */
479                         *m_ptr = party_mon[i];
480                         r_ptr = real_r_ptr(m_ptr);
481
482                         m_ptr->fy = cy;
483                         m_ptr->fx = cx;
484                         m_ptr->ml = TRUE;
485                         m_ptr->csleep = 0;
486
487                         /* Paranoia */
488                         m_ptr->hold_o_idx = 0;
489                         m_ptr->target_y = 0;
490
491                         if ((r_ptr->flags1 & RF1_FORCE_SLEEP) && !ironman_nightmare)
492                         {
493                                 /* Monster is still being nice */
494                                 m_ptr->mflag |= (MFLAG_NICE);
495
496                                 /* Must repair monsters */
497                                 repair_monsters = TRUE;
498                         }
499
500                         /* Update the monster */
501                         update_mon(m_idx, TRUE);
502                         lite_spot(cy, cx);
503
504                         /* Pre-calculated in precalc_cur_num_of_pet() */
505                         /* r_ptr->cur_num++; */
506
507                         /* Hack -- Count the number of "reproducers" */
508                         if (r_ptr->flags2 & RF2_MULTIPLY) num_repro++;
509
510                         /* Hack -- Notice new multi-hued monsters */
511                         {
512                                 monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
513                                 if ((ap_r_ptr->flags1 & RF1_ATTR_MULTI) || (ap_r_ptr->flags2 & RF2_SHAPECHANGER))
514                                         shimmer_monsters = TRUE;
515                         }
516                 }
517                 else
518                 {
519                         monster_type *m_ptr = &party_mon[i];
520                         monster_race *r_ptr = real_r_ptr(m_ptr);
521                         char m_name[80];
522
523                         monster_desc(m_name, m_ptr, 0);
524 #ifdef JP
525                         msg_format("%s¤È¤Ï¤°¤ì¤Æ¤·¤Þ¤Ã¤¿¡£", m_name);
526 #else
527                         msg_format("You have lost sight of %s.", m_name);
528 #endif
529                         if (record_named_pet && m_ptr->nickname)
530                         {
531                                 monster_desc(m_name, m_ptr, 0x08);
532                                 do_cmd_write_nikki(NIKKI_NAMED_PET, 5, m_name);
533                         }
534
535                         /* Pre-calculated in precalc_cur_num_of_pet(), but need to decrease */
536                         if (r_ptr->cur_num) r_ptr->cur_num--;
537                 }
538         }
539
540         /* For accuracy of precalc_cur_num_of_pet() */
541         C_WIPE(party_mon, MAX_PARTY_MON, monster_type);
542 }
543
544
545 /*
546  * Hack -- Update location of unique monsters and artifacts
547  *
548  * The r_ptr->floor_id and a_ptr->floor_id are not updated correctly
549  * while new floor creation since dungeons may be re-created by
550  * auto-scum option.
551  */
552 static void update_unique_artifact(s16b cur_floor_id)
553 {
554         int i;
555
556         /* Maintain unique monsters */
557         for (i = 1; i < m_max; i++)
558         {
559                 monster_race *r_ptr;
560                 monster_type *m_ptr = &m_list[i];
561
562                 /* Skip dead monsters */
563                 if (!m_ptr->r_idx) continue;
564
565                 /* Extract real monster race */
566                 r_ptr = real_r_ptr(m_ptr);
567
568                 /* Memorize location of the unique monster */
569                 if ((r_ptr->flags1 & RF1_UNIQUE) ||
570                     (r_ptr->flags7 & RF7_UNIQUE_7))
571                 {
572                         r_ptr->floor_id = cur_floor_id;
573                 }
574         }
575
576         /* Maintain artifatcs */
577         for (i = 1; i < o_max; i++)
578         {
579                 object_type *o_ptr = &o_list[i];
580
581                 /* Skip dead objects */
582                 if (!o_ptr->k_idx) continue;
583
584                 /* Memorize location of the artifact */
585                 if (artifact_p(o_ptr))
586                 {
587                         a_info[o_ptr->name1].floor_id = cur_floor_id;
588                 }
589         }
590 }
591
592
593 /*
594  * When a monster is at a place where player will return,
595  * Get out of the my way!
596  */
597 static void get_out_monster(void)
598 {
599         int tries = 0;
600         int dis = 1;
601         int oy = py;
602         int ox = px;
603         int m_idx = cave[oy][ox].m_idx;
604
605         /* Nothing to do if no monster */
606         if (!m_idx) return;
607
608         /* Look until done */
609         while (TRUE)
610         {
611                 monster_type *m_ptr;
612
613                 /* Pick a (possibly illegal) location */
614                 int ny = rand_spread(oy, dis);
615                 int nx = rand_spread(ox, dis);
616
617                 tries++;
618
619                 /* Stop after 1000 tries */
620                 if (tries > 10000) return;
621
622                 /*
623                  * Increase distance after doing enough tries
624                  * compared to area of possible space
625                  */
626                 if (tries > 20 * dis * dis) dis++;
627
628                 /* Ignore illegal locations */
629                 if (!in_bounds(ny, nx)) continue;
630
631                 /* Require "empty" floor space */
632                 if (!cave_empty_bold(ny, nx)) continue;
633
634                 /* Hack -- no teleport onto glyph of warding */
635                 if (is_glyph_grid(&cave[ny][nx])) continue;
636                 if (is_explosive_rune_grid(&cave[ny][nx])) continue;
637
638                 /* ...nor onto the Pattern */
639                 if ((cave[ny][nx].feat >= FEAT_PATTERN_START) &&
640                     (cave[ny][nx].feat <= FEAT_PATTERN_XTRA2)) continue;
641
642                 /*** It's a good place ***/
643
644                 m_ptr = &m_list[m_idx];
645
646                 /* Update the new location */
647                 cave[ny][nx].m_idx = m_idx;
648
649                 /* Update the old location */
650                 cave[oy][ox].m_idx = 0;
651
652                 /* Move the monster */
653                 m_ptr->fy = ny;
654                 m_ptr->fx = nx; 
655
656                 /* No need to do update_mon() */
657
658                 /* Success */
659                 return;
660         }
661 }
662
663
664 /*
665  * Maintain quest monsters, mark next floor_id at stairs, save current
666  * floor, and prepare to enter next floor.
667  */
668 void leave_floor(void)
669 {
670         cave_type *c_ptr = NULL;
671         saved_floor_type *sf_ptr;
672         int quest_r_idx = 0;
673         int i;
674
675         /* Preserve pets and prepare to take these to next floor */
676         preserve_pet();
677
678         /* New floor is not yet prepared */
679         new_floor_id = 0;
680
681
682         /* From somewhere of the surface to somewhere of the surface */
683         if (!dungeon_type)
684         {
685                 /* No need to save current floor */
686                 return;
687         }
688
689         /* Search the quest monster index */
690         for (i = 0; i < max_quests; i++)
691         {
692                 if ((quest[i].status == QUEST_STATUS_TAKEN) &&
693                     ((quest[i].type == QUEST_TYPE_KILL_LEVEL) ||
694                     (quest[i].type == QUEST_TYPE_RANDOM)) &&
695                     (quest[i].level == dun_level) &&
696                     (dungeon_type == quest[i].dungeon) &&
697                     !(quest[i].flags & QUEST_FLAG_PRESET))
698                 {
699                         quest_r_idx = quest[i].r_idx;
700                 }
701         }
702
703         /* Maintain quest monsters */
704         for (i = 1; i < m_max; i++)
705         {
706                 monster_race *r_ptr;
707                 monster_type *m_ptr = &m_list[i];
708
709                 /* Skip dead monsters */
710                 if (!m_ptr->r_idx) continue;
711
712                 /* Only maintain quest monsters */
713                 if (quest_r_idx != m_ptr->r_idx) continue;
714
715                 /* Extract real monster race */
716                 r_ptr = real_r_ptr(m_ptr);
717
718                 /* Ignore unique monsters */
719                 if ((r_ptr->flags1 & RF1_UNIQUE) ||
720                     (r_ptr->flags7 & RF7_UNIQUE_7)) continue;
721
722                 /* Delete non-unique quest monsters */
723                 delete_monster_idx(i);
724         }
725
726         /* Check if there is a same item */
727         for (i = 0; i < INVEN_PACK; i++)
728         {
729                 object_type *o_ptr = &inventory[i];
730
731                 /* Skip dead objects */
732                 if (!o_ptr->k_idx) continue;
733
734                 /* Delete old memorized location of the artifact */
735                 if (artifact_p(o_ptr))
736                 {
737                         a_info[o_ptr->name1].floor_id = 0;
738                 }
739         }
740
741         /* Extract current floor info or NULL */
742         sf_ptr = get_sf_ptr(p_ptr->floor_id);
743
744         /* Choose random stairs */
745         if ((change_floor_mode & CFM_RAND_CONNECT) && p_ptr->floor_id)
746         {
747                 int x, y, sx = 0, sy = 0;
748                 int x_table[20];
749                 int y_table[20];
750                 int num = 0;
751                 int i;
752
753                 /* Search usable stairs */
754                 for (y = 0; y < cur_hgt; y++)
755                 {
756                         for (x = 0; x < cur_wid; x++)
757                         {
758                                 cave_type *c_ptr = &cave[y][x];
759                                 bool ok = FALSE;
760
761                                 if (change_floor_mode & CFM_UP)
762                                 {
763                                         /* Found fixed stairs */
764                                         if (c_ptr->special &&
765                                             c_ptr->special == sf_ptr->upper_floor_id)
766                                         {
767                                                 sx = x;
768                                                 sy = y;
769                                         }
770
771                                         if (c_ptr->feat == FEAT_LESS ||
772                                             c_ptr->feat == FEAT_LESS_LESS)
773                                                 ok = TRUE;
774                                 }
775                                 else if (change_floor_mode & CFM_DOWN)
776                                 {
777                                         /* Found fixed stairs */
778                                         if (c_ptr->special &&
779                                             c_ptr->special == sf_ptr->lower_floor_id)
780                                         {
781                                                 sx = x;
782                                                 sy = y;
783                                         }
784
785                                         if (c_ptr->feat == FEAT_MORE ||
786                                             c_ptr->feat == FEAT_MORE_MORE)
787                                                 ok = TRUE;
788                                 }
789
790                                 if (ok && num < 20)
791                                 {
792                                         x_table[num] = x;
793                                         y_table[num] = y;
794                                         num++;
795                                 }
796                         }
797                 }
798
799                 if (sx)
800                 {
801                         /* Already fixed */
802                         py = sy;
803                         px = sx;
804                 }
805                 else if (!num)
806                 {
807                         /* No stairs found! -- No return */
808                         prepare_change_floor_mode(CFM_RAND_PLACE | CFM_NO_RETURN);
809
810                         /* Mega Hack -- It's not the stairs you enter.  Disable it.  */
811                         cave[py][px].special = 0;
812                 }
813                 else
814                 {
815                         /* Choose random one */
816                         i = randint0(num);
817
818                         /* Point stair location */
819                         py = y_table[i];
820                         px = x_table[i];
821                 }
822         }
823
824         /* Extract new dungeon level */
825         if (!(change_floor_mode & CFM_CLEAR_ALL))
826         {
827                 int move_num = 0;
828
829                 /* Extract stair position */
830                 c_ptr = &cave[py][px];
831
832                 /* Get back to old saved floor? */
833                 if (dun_level && c_ptr->special && get_sf_ptr(c_ptr->special))
834                 {
835                         /* Saved floor is exist.  Use it. */
836                         new_floor_id = c_ptr->special;
837                 }
838
839                 /* Extract level movement number */
840                 if (change_floor_mode & CFM_DOWN) move_num = 1;
841                 else if (change_floor_mode & CFM_UP) move_num = -1;
842                 
843                 /* Mark shaft up/down */
844                 if (c_ptr->feat == FEAT_LESS_LESS ||
845                     c_ptr->feat == FEAT_MORE_MORE)
846                 {
847                         prepare_change_floor_mode(CFM_SHAFT);
848                         move_num += SGN(move_num);
849                 }
850
851                 /* Get out from or Enter the dungeon */
852                 if (change_floor_mode & CFM_DOWN)
853                 {
854                         if (!dun_level)
855                                 move_num = d_info[c_ptr->special].mindepth;
856                 }
857                 else if (change_floor_mode & CFM_UP)
858                 {
859                         if (dun_level + move_num < d_info[dungeon_type].mindepth)
860                                 move_num = -dun_level;
861                 }
862                 
863                 dun_level += move_num;
864         }
865
866         /* Leaving the dungeon to town */
867         if (!dun_level && dungeon_type)
868         {
869                 p_ptr->leaving_dungeon = TRUE;
870                 if (!vanilla_town && !lite_town)
871                 {
872                         p_ptr->wilderness_y = d_info[dungeon_type].dy;
873                         p_ptr->wilderness_x = d_info[dungeon_type].dx;
874                 }
875                 p_ptr->recall_dungeon = dungeon_type;
876                 dungeon_type = 0;
877
878                 /* Reach to the surface -- Clear all saved floors */
879                 prepare_change_floor_mode(CFM_CLEAR_ALL);
880         }
881
882         if (change_floor_mode & CFM_CLEAR_ALL)
883         {
884                 int i;
885
886                 /* Kill all saved floors */
887                 for (i = 0; i < MAX_SAVED_FLOORS; i++)
888                         kill_saved_floor(&saved_floors[i]);
889
890                 /* Reset visit_mark count */
891                 latest_visit_mark = 1;
892         }
893         else if (change_floor_mode & CFM_NO_RETURN)
894         {
895                 /* Kill current floor */
896                 kill_saved_floor(sf_ptr);
897         }
898
899         /* No current floor -- Left/Enter dungeon etc... */
900         if (!p_ptr->floor_id)
901         {
902                 /* No longer need to save current floor */
903                 return;
904         }
905
906
907         /* Mark next floor_id on the previous floor */
908         if (!new_floor_id)
909         {
910                 /* Get new id */
911                 new_floor_id = get_new_floor_id();
912
913                 /* Connect from here */
914                 if (c_ptr)
915                 {
916                         c_ptr->special = new_floor_id;
917                 }
918
919                 /* Record new dungeon level */
920                 get_sf_ptr(new_floor_id)->dun_level = dun_level;
921         }
922
923         /* Fix connection -- level teleportation or trap door */
924         if (change_floor_mode & CFM_RAND_CONNECT)
925         {
926                 if (change_floor_mode & CFM_UP)
927                         sf_ptr->upper_floor_id = new_floor_id;
928                 else if (change_floor_mode & CFM_DOWN)
929                         sf_ptr->lower_floor_id = new_floor_id;
930         }
931
932         /* If you can return, you need to save previous floor */
933         if (!(change_floor_mode & (CFM_NO_RETURN | CFM_CLEAR_ALL)))
934         {
935                 /* Get out of the my way! */
936                 get_out_monster();
937
938                 /* Record the last visit turn of current floor */
939                 sf_ptr->last_visit = turn;
940
941                 /* Forget the lite */
942                 forget_lite();
943
944                 /* Forget the view */
945                 forget_view();
946
947                 /* Forget the view */
948                 clear_mon_lite();
949
950                 /* Save current floor */
951                 if (!save_floor(sf_ptr, 0))
952                 {
953                         /* Save failed -- No return */
954                         prepare_change_floor_mode(CFM_NO_RETURN);
955
956                         /* Kill current floor */
957                         kill_saved_floor(get_sf_ptr(p_ptr->floor_id));
958                 }
959         }
960 }
961
962
963 /*
964  * Enter new floor.  If the floor is an old saved floor, it will be
965  * restored from the temporal file.  If the floor is new one, new cave
966  * will be generated.
967  */
968 void change_floor(void)
969 {
970         saved_floor_type *sf_ptr;
971         bool loaded = FALSE;
972
973         /* The dungeon is not ready */
974         character_dungeon = FALSE;
975
976         /* No longer in the trap detecteded region */
977         p_ptr->dtrap = FALSE;
978
979         /* Mega-Hack -- no panel yet */
980         panel_row_min = 0;
981         panel_row_max = 0;
982         panel_col_min = 0;
983         panel_col_max = 0;
984
985         /* Mega-Hack -- not ambushed on the wildness? */
986         ambush_flag = FALSE;
987
988         /* On the surface */
989         if (!dungeon_type)
990         {
991                 /* Create cave */
992                 generate_cave();
993
994                 /* Paranoia -- Now on the surface */
995                 new_floor_id = 0;
996         }
997
998         /* In the dungeon */
999         else
1000         {
1001                 /* No floor_id yet */
1002                 if (!new_floor_id)
1003                 {
1004                         /* Get new id */
1005                         new_floor_id = get_new_floor_id();
1006                 }
1007
1008                 /* Pointer for infomations of new floor */
1009                 sf_ptr = get_sf_ptr(new_floor_id);
1010
1011                 /* Try to restore old floor */
1012                 if (sf_ptr->last_visit)
1013                 {
1014                         /* Old saved floor is exist */
1015                         if (load_floor(sf_ptr, 0))
1016                         {
1017                                 loaded = TRUE;
1018
1019                                 /* Forbid return stairs */
1020                                 if (change_floor_mode & CFM_NO_RETURN)
1021                                 {
1022                                         cave_type *c_ptr = &cave[py][px];
1023
1024                                         /* Reset to floor */
1025                                         place_floor_grid(c_ptr);
1026                                         c_ptr->special = 0;
1027                                 }
1028                         }
1029                 }
1030
1031                 /*
1032                  * Set lower/upper_floor_id of new floor when the new
1033                  * floor is right-above/right-under the current floor.
1034                  *
1035                  * Stair creation/Teleport level/Trap door will take
1036                  * you the same floor when you used it later again.
1037                  */
1038                 if (p_ptr->floor_id)
1039                 {
1040                         saved_floor_type *cur_sf_ptr = get_sf_ptr(p_ptr->floor_id);
1041
1042                         if (change_floor_mode & CFM_UP)
1043                         {
1044                                 /* New floor is right-above */
1045                                 if (cur_sf_ptr->upper_floor_id == new_floor_id)
1046                                         sf_ptr->lower_floor_id = p_ptr->floor_id;
1047                         }
1048                         else if (change_floor_mode & CFM_DOWN)
1049                         {
1050                                 /* New floor is right-under */
1051                                 if (cur_sf_ptr->lower_floor_id == new_floor_id)
1052                                         sf_ptr->upper_floor_id = p_ptr->floor_id;
1053                         }
1054                 }
1055
1056                 /* Maintain monsters and artifacts */
1057                 if (loaded)
1058                 {
1059                         int i;
1060                         s32b absence_ticks = (turn - sf_ptr->last_visit) / TURNS_PER_TICK;
1061                         int alloc_chance = d_info[dungeon_type].max_m_alloc_chance;
1062                         int alloc_times;
1063
1064                         /* Maintain monsters */
1065                         for (i = 1; i < m_max; i++)
1066                         {
1067                                 monster_race *r_ptr;
1068                                 monster_type *m_ptr = &m_list[i];
1069
1070                                 /* Skip dead monsters */
1071                                 if (!m_ptr->r_idx) continue;
1072
1073                                 if (!is_pet(m_ptr))
1074                                 {
1075                                         /* Restore HP */
1076                                         m_ptr->hp = m_ptr->maxhp = m_ptr->max_maxhp;
1077
1078                                         /* Remove fear */
1079                                         m_ptr->monfear = 0;
1080
1081                                         /* Remove invulnerability */
1082                                         m_ptr->invulner = 0;
1083
1084                                         /* Remove fast status */
1085                                         m_ptr->fast = 0;
1086
1087                                         /* Remove slow status */
1088                                         m_ptr->slow = 0;
1089
1090                                         /* Remove stun */
1091                                         m_ptr->stunned = 0;
1092
1093                                         /* Remove confusion */
1094                                         m_ptr->confused = 0;
1095                                 }
1096
1097                                 /* Extract real monster race */
1098                                 r_ptr = real_r_ptr(m_ptr);
1099
1100                                 /* Ignore non-unique */
1101                                 if (!(r_ptr->flags1 & RF1_UNIQUE) &&
1102                                     !(r_ptr->flags7 & RF7_UNIQUE_7)) continue;
1103
1104                                 /* Appear at a different floor? */
1105                                 if (r_ptr->floor_id != new_floor_id)
1106                                 {
1107                                         /* Disapper from here */
1108                                         delete_monster_idx(i);
1109                                 }
1110                         }
1111
1112                         /* Maintain artifatcs */
1113                         for (i = 1; i < o_max; i++)
1114                         {
1115                                 object_type *o_ptr = &o_list[i];
1116
1117                                 /* Skip dead objects */
1118                                 if (!o_ptr->k_idx) continue;
1119
1120                                 /* Ignore non-artifact */
1121                                 if (!artifact_p(o_ptr)) continue;
1122
1123                                 /* Appear at a different floor? */
1124                                 if (a_info[o_ptr->name1].floor_id != new_floor_id)
1125                                 {
1126                                         /* Disappear from here */
1127                                         delete_object_idx(i);
1128                                 }
1129                                 else
1130                                 {
1131                                         /* Cancel preserve */
1132                                         a_info[o_ptr->name1].cur_num = 1;
1133                                 }
1134                         }
1135
1136                         place_quest_monsters();
1137
1138                         /* Place some random monsters */
1139                         alloc_times = absence_ticks / alloc_chance;
1140
1141                         if (randint0(alloc_chance) < (absence_ticks % alloc_chance))
1142                                 alloc_times++;
1143
1144                         for (i = 0; i < alloc_times; i++)
1145                         {
1146                                 /* Make a (group of) new monster */
1147                                 (void)alloc_monster(0, 0);
1148                         }
1149                 }
1150
1151                 /* New floor_id or failed to restore */
1152                 else /* if (!loaded) */
1153                 {
1154                         if (sf_ptr->last_visit)
1155                         {
1156                                 /* Temporal file is broken? */
1157 #ifdef JP
1158                                 msg_print("³¬ÃʤϹԤ­»ß¤Þ¤ê¤À¤Ã¤¿¡£");
1159 #else
1160                                 msg_print("The staircases come to a dead end...");
1161 #endif
1162
1163                                 /* Create simple dead end */
1164                                 build_dead_end();
1165
1166                                 /* Break connection */
1167                                 if (change_floor_mode & CFM_UP)
1168                                 {
1169                                         sf_ptr->upper_floor_id = 0;
1170                                 }
1171                                 else if (change_floor_mode & CFM_DOWN)
1172                                 {
1173                                         sf_ptr->lower_floor_id = 0;
1174                                 }
1175                         }
1176                         else
1177                         {
1178                                 /* Newly create cave */
1179                                 generate_cave();
1180                         }
1181
1182                         /* Record last visit turn */
1183                         sf_ptr->last_visit = turn;
1184
1185                         /* Set correct dun_level value */
1186                         sf_ptr->dun_level = dun_level;
1187
1188                         /* Creat connected stairs */
1189                         if (!(change_floor_mode & (CFM_NO_RETURN | CFM_CLEAR_ALL)) && dun_level)
1190                         {
1191                                 bool ok = TRUE;
1192
1193                                 /* Extract stair position */
1194                                 cave_type *c_ptr = &cave[py][px];
1195
1196                                 /*** Create connected stairs ***/
1197
1198                                 /* No stairs down from Quest */
1199                                 if ((change_floor_mode & CFM_UP) && !quest_number(dun_level))
1200                                 {
1201                                         if (change_floor_mode & CFM_SHAFT)
1202                                                 c_ptr->feat = FEAT_MORE_MORE;
1203                                         else
1204                                                 c_ptr->feat = FEAT_MORE;
1205                                 }
1206
1207                                 /* No stairs up when ironman_downward */
1208                                 else if ((change_floor_mode & CFM_DOWN) && !ironman_downward)
1209                                 {
1210                                         if (change_floor_mode & CFM_SHAFT)
1211                                                 c_ptr->feat = FEAT_LESS_LESS;
1212                                         else
1213                                                 c_ptr->feat = FEAT_LESS;
1214                                 }
1215                                 else
1216                                 {
1217                                         /* Hum??? */
1218                                         ok = FALSE;
1219                                 }
1220
1221                                 if (ok)
1222                                 {
1223                                         /* Paranoia -- Clear mimic */
1224                                         c_ptr->mimic = 0;
1225
1226                                         /* Connect to previous floor */
1227                                         c_ptr->special = p_ptr->floor_id;
1228                                 }
1229                         }
1230                 }
1231
1232                 /* Arrive at random grid */
1233                 if (change_floor_mode & (CFM_RAND_PLACE))
1234                 {
1235                         (void)new_player_spot();
1236                 }
1237
1238                 /* You see stairs blocked */
1239                 else if (change_floor_mode & CFM_NO_RETURN)
1240                 {
1241                         if (!p_ptr->blind)
1242                         {
1243 #ifdef JP
1244                                 msg_print("ÆÍÁ³³¬Ãʤ¬ºÉ¤¬¤ì¤Æ¤·¤Þ¤Ã¤¿¡£");
1245 #else
1246                                 msg_print("Suddenly the stairs is blocked!");
1247 #endif
1248                         }
1249                         else
1250                         {
1251 #ifdef JP
1252                                 msg_print("¥´¥È¥´¥È¤È²¿¤«²»¤¬¤·¤¿¡£");
1253 #else
1254                                 msg_print("You hear some noises.");
1255 #endif
1256                         }
1257                 }
1258
1259                 /*
1260                  * Update visit mark
1261                  *
1262                  * The "turn" is not always different number because
1263                  * the level teleport doesn't take any turn.  Use
1264                  * visit mark instead of last visit turn to find the
1265                  * oldest saved floor.
1266                  */
1267                 sf_ptr->visit_mark = latest_visit_mark++;
1268         }
1269
1270         /* Place preserved pet monsters */
1271         place_pet();
1272
1273         /* Hack -- maintain unique and artifacts */
1274         update_unique_artifact(new_floor_id);
1275
1276         /* Now the player is in new floor */
1277         p_ptr->floor_id = new_floor_id;
1278
1279         /* The dungeon is ready */
1280         character_dungeon = TRUE;
1281
1282         /* Hack -- Munchkin characters always get whole map */
1283         if (p_ptr->pseikaku == SEIKAKU_MUNCHKIN)
1284                 wiz_lite((bool)(p_ptr->pclass == CLASS_NINJA));
1285
1286         /* Remember when this level was "created" */
1287         old_turn = turn;
1288
1289         /* Clear all flags */
1290         change_floor_mode = 0L;
1291 }
1292
1293
1294
1295 /*
1296  * Create stairs at or move previously created stairs into the player
1297  * location.
1298  */
1299 void stair_creation(void)
1300 {
1301         saved_floor_type *sf_ptr;
1302         saved_floor_type *dest_sf_ptr;
1303
1304         bool up = TRUE;
1305         bool down = TRUE;
1306         s16b dest_floor_id = 0;
1307
1308
1309         /* Forbid up staircases on Ironman mode */
1310         if (ironman_downward) up = FALSE;
1311
1312         /* Forbid down staircases on quest level */
1313         if (quest_number(dun_level) || (dun_level >= d_info[dungeon_type].maxdepth)) down = FALSE;
1314
1315         /* No effect out of standard dungeon floor */
1316         if (!dun_level || (!up && !down) ||
1317             (p_ptr->inside_quest && (p_ptr->inside_quest < MIN_RANDOM_QUEST)) ||
1318             p_ptr->inside_arena || p_ptr->inside_battle)
1319         {
1320                 /* arena or quest */
1321 #ifdef JP
1322                 msg_print("¸ú²Ì¤¬¤¢¤ê¤Þ¤»¤ó¡ª");
1323 #else
1324                 msg_print("There is no effect!");
1325 #endif
1326                 return;
1327         }
1328
1329         /* Artifacts resists */
1330         if (!cave_valid_bold(py, px))
1331         {
1332 #ifdef JP
1333                 msg_print("¾²¾å¤Î¥¢¥¤¥Æ¥à¤¬¼öʸ¤òÄ·¤ÍÊÖ¤·¤¿¡£");
1334 #else
1335                 msg_print("The object resists the spell.");
1336 #endif
1337
1338                 return;
1339         }
1340
1341         /* Destroy all objects in the grid */
1342         delete_object(py, px);
1343
1344         /* Extract current floor data */
1345         sf_ptr = get_sf_ptr(p_ptr->floor_id);
1346
1347         /* Choose randomly */
1348         if (up && down)
1349         {
1350                 if (randint0(100) < 50) up = FALSE;
1351                 else down = FALSE;
1352         }
1353
1354         /* Destination is already fixed */
1355         if (up)
1356         {
1357                 if (sf_ptr->upper_floor_id) dest_floor_id = sf_ptr->upper_floor_id;
1358         }
1359         else
1360         {
1361                 if (sf_ptr->lower_floor_id) dest_floor_id = sf_ptr->lower_floor_id;
1362         }
1363
1364
1365         /* Search old stairs leading to the destination */
1366         if (dest_floor_id)
1367         {
1368                 int x, y;
1369
1370                 for (y = 0; y < cur_hgt; y++)
1371                 {
1372                         for (x = 0; x < cur_wid; x++)
1373                         {
1374                                 cave_type *c_ptr = &cave[y][x];
1375
1376                                 if (!c_ptr->special) continue;
1377                                 if (c_ptr->special != dest_floor_id) continue;
1378
1379                                 /* Remove old stairs */
1380                                 c_ptr->special = 0;
1381                                 cave_set_feat(y, x, floor_type[randint0(100)]);
1382                         }
1383                 }
1384         }
1385
1386         /* No old destination -- Get new one now */
1387         else
1388         {
1389                 dest_floor_id = get_new_floor_id();
1390
1391                 /* Fix it */
1392                 if (up)
1393                         sf_ptr->upper_floor_id = dest_floor_id;
1394                 else
1395                         sf_ptr->lower_floor_id = dest_floor_id;
1396         }
1397
1398         /* Extract destination floor data */
1399         dest_sf_ptr = get_sf_ptr(dest_floor_id);
1400
1401
1402         /* Create a staircase */
1403         if (up)
1404         {
1405                 if (dest_sf_ptr->last_visit && dest_sf_ptr->dun_level <= dun_level - 2)
1406                         cave_set_feat(py, px, FEAT_LESS_LESS);
1407                 else
1408                         cave_set_feat(py, px, FEAT_LESS);
1409         }
1410         else
1411         {
1412                 if (dest_sf_ptr->last_visit && dest_sf_ptr->dun_level >= dun_level + 2)
1413                         cave_set_feat(py, px, FEAT_MORE_MORE);
1414                 else
1415                         cave_set_feat(py, px, FEAT_MORE);
1416         }
1417
1418
1419         /* Connect this stairs to the destination */
1420         cave[py][px].special = dest_floor_id;
1421 }
1422
1423