OSDN Git Service

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