OSDN Git Service

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