OSDN Git Service

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