OSDN Git Service

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