OSDN Git Service

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