OSDN Git Service

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