3 /* Purpose: management of the saved floor */
6 * Copyright (c) 2002 Mogami
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.
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 */
23 * Initialize saved_floors array. Make sure that old temporal files
24 * are not remaining as gurbages.
26 void init_saved_floors(bool force)
28 char floor_savefile[1024];
35 /* Get "games" permissions */
40 for (i = 0; i < MAX_SAVED_FLOORS; i++)
42 saved_floor_type *sf_ptr = &saved_floors[i];
45 sprintf(floor_savefile, "%s.F%02d", savefile, i);
47 /* Grab permissions */
50 /* Try to create the file */
51 fd = fd_make(floor_savefile, mode);
53 /* Drop permissions */
62 msg_print("¥¨¥é¡¼¡§¸Å¤¤¥Æ¥ó¥Ý¥é¥ê¡¦¥Õ¥¡¥¤¥ë¤¬»Ä¤Ã¤Æ¤¤¤Þ¤¹¡£");
63 msg_print("ÊѶòÈÚÅܤòÆó½Å¤Ëµ¯Æ°¤·¤Æ¤¤¤Ê¤¤¤«³Îǧ¤·¤Æ¤¯¤À¤µ¤¤¡£");
64 msg_print("²áµî¤ËÊѶòÈÚÅܤ¬¥¯¥é¥Ã¥·¥å¤·¤¿¾ì¹ç¤Ï°ì»þ¥Õ¥¡¥¤¥ë¤ò");
65 msg_print("¶¯À©Åª¤Ëºï½ü¤·¤Æ¼Â¹Ô¤ò³¤±¤é¤ì¤Þ¤¹¡£");
66 if (!get_check("¶¯À©Åª¤Ëºï½ü¤·¤Æ¤â¤è¤í¤·¤¤¤Ç¤¹¤«¡©")) quit("¼Â¹ÔÃæ»ß");
68 msg_print("Error: There are old temporal files.");
69 msg_print("Make sure you are not running two game processes simultaneously.");
70 msg_print("If the temporal files are garbages of old crashed process, ");
71 msg_print("you can delete it safely.");
72 if (!get_check("Do you delete old temporal files? ")) quit("Aborted.");
83 /* Grab permissions */
86 /* Simply kill the temporal file */
87 (void)fd_kill(floor_savefile);
89 /* Drop permissions */
95 /* No floor_id used yet (No.0 is reserved to indicate non existance) */
98 /* vist_mark is from 1 */
99 latest_visit_mark = 1;
101 /* A sign to mark temporal files */
102 saved_floor_file_sign = time(NULL);
104 /* No next floor yet */
107 /* No change floor mode yet */
108 change_floor_mode = 0;
112 /* Drop "games" permissions */
120 * Kill temporal files
121 * Should be called just before the game quit.
123 void clear_saved_floor_files(void)
125 char floor_savefile[1024];
130 /* Get "games" permissions */
135 for (i = 0; i < MAX_SAVED_FLOORS; i++)
137 saved_floor_type *sf_ptr = &saved_floors[i];
139 /* No temporal file */
140 if (!sf_ptr->floor_id) continue;
141 if (sf_ptr->floor_id == p_ptr->floor_id) continue;
144 sprintf(floor_savefile, "%s.F%02d", savefile, i);
146 /* Grab permissions */
149 /* Simply kill the temporal file */
150 (void)fd_kill(floor_savefile);
152 /* Drop permissions */
158 /* Drop "games" permissions */
166 * Get a pointer for an item of the saved_floors array.
168 saved_floor_type *get_sf_ptr(s16b floor_id)
172 /* floor_id No.0 indicates no floor */
173 if (!floor_id) return NULL;
175 for (i = 0; i < MAX_SAVED_FLOORS; i++)
177 saved_floor_type *sf_ptr = &saved_floors[i];
179 if (sf_ptr->floor_id == floor_id) return sf_ptr;
188 * kill a saved floor and get an empty space
190 static void kill_saved_floor(saved_floor_type *sf_ptr)
192 char floor_savefile[1024];
195 if (!sf_ptr->floor_id) return;
197 if (sf_ptr->floor_id == p_ptr->floor_id)
199 /* Kill current floor */
202 /* Current floor doesn't have temporal file */
207 sprintf(floor_savefile, "%s.F%02d", savefile, (int)sf_ptr->savefile_id);
209 /* Grab permissions */
212 /* Simply kill the temporal file */
213 (void)fd_kill(floor_savefile);
215 /* Drop permissions */
219 /* No longer exists */
220 sf_ptr->floor_id = 0;
225 * Initialize new saved floor and get its floor id. If number of
226 * saved floors are already MAX_SAVED_FLOORS, kill the oldest one.
228 s16b get_new_floor_id(void)
230 saved_floor_type *sf_ptr;
233 /* Look for empty space */
234 for (i = 0; i < MAX_SAVED_FLOORS; i++)
236 sf_ptr = &saved_floors[i];
238 if (!sf_ptr->floor_id) break;
242 if (i == MAX_SAVED_FLOORS)
245 u32b oldest_visit = 0xffffffffL;
247 /* Search for oldest */
248 for (i = 0; i < MAX_SAVED_FLOORS; i++)
250 sf_ptr = &saved_floors[i];
252 /* Don't kill current floor */
253 if (sf_ptr->floor_id == p_ptr->floor_id) continue;
255 /* Don't kill newer */
256 if (sf_ptr->visit_mark > oldest_visit) continue;
259 oldest_visit = sf_ptr->visit_mark;
262 /* Kill oldest saved floor */
263 sf_ptr = &saved_floors[oldest];
264 kill_saved_floor(sf_ptr);
270 /* Prepare new floor data */
271 sf_ptr->savefile_id = i;
272 sf_ptr->floor_id = max_floor_id;
273 sf_ptr->last_visit = 0;
274 sf_ptr->upper_floor_id = 0;
275 sf_ptr->lower_floor_id = 0;
276 sf_ptr->visit_mark = latest_visit_mark++;
278 /* sf_ptr->dun_level is not yet decided */
281 /* Increment number of floor_id */
282 if (max_floor_id < MAX_SHORT) max_floor_id++;
284 /* 32767 floor_ids are all used up! Re-use ancient IDs */
285 else max_floor_id = 1;
287 return sf_ptr->floor_id;
292 * Prepare mode flags of changing floor
294 void prepare_change_floor_mode(u32b mode)
296 change_floor_mode |= mode;
301 * Builds the dead end
303 static void build_dead_end(void)
307 /* Clear and empty the cave */
310 /* Fill the arrays of floors and walls in the good proportions */
311 set_floor_and_wall(0);
314 cur_hgt = SCREEN_HGT;
315 cur_wid = SCREEN_WID;
317 /* Filled with permanent walls */
318 for (y = 0; y < MAX_HGT; y++)
320 for (x = 0; x < MAX_WID; x++)
322 /* Create "solid" perma-wall */
323 place_solid_perm_bold(y, x);
327 /* Place at center of the floor */
331 /* Give one square */
332 place_floor_bold(py, px);
339 static void preserve_pet(void)
343 for (num = 0; num < MAX_PARTY_MON; num++)
345 party_mon[num].r_idx = 0;
350 monster_type *m_ptr = &m_list[p_ptr->riding];
352 /* Pet of other pet don't follow. */
353 if (m_ptr->parent_m_idx)
356 p_ptr->pet_extra_flags &= ~(PF_RYOUTE);
357 p_ptr->riding_ryoute = p_ptr->old_riding_ryoute = FALSE;
361 /* Preserve the mount */
362 COPY(&party_mon[0], m_ptr, monster_type);
364 /* Delete from this floor */
365 delete_monster_idx(p_ptr->riding);
370 * If player is in wild mode, no pets are preserved
371 * except a monster whom player riding
373 if (!p_ptr->wild_mode && !p_ptr->inside_arena && !p_ptr->inside_battle)
375 for (i = m_max - 1, num = 1; (i >= 1 && num < MAX_PARTY_MON); i--)
377 monster_type *m_ptr = &m_list[i];
379 if (!m_ptr->r_idx) continue;
380 if (!is_pet(m_ptr)) continue;
381 if (i == p_ptr->riding) continue;
383 if (reinit_wilderness)
385 /* Don't lose sight of pets when getting a Quest */
389 int dis = distance(py, px, m_ptr->fy, m_ptr->fx);
391 /* Confused (etc.) monsters don't follow. */
392 if (m_ptr->confused || m_ptr->stunned || m_ptr->csleep) continue;
394 /* Pet of other pet don't follow. */
395 if (m_ptr->parent_m_idx) continue;
398 * Pets with nickname will follow even from 3 blocks away
399 * when you or the pet can see the other.
401 if (m_ptr->nickname &&
402 ((player_has_los_bold(m_ptr->fy, m_ptr->fx) && projectable(py, px, m_ptr->fy, m_ptr->fx)) ||
403 (los(m_ptr->fy, m_ptr->fx, py, px) && projectable(m_ptr->fy, m_ptr->fx, py, px))))
405 if (dis > 3) continue;
409 if (dis > 1) continue;
413 COPY(&party_mon[num], &m_list[i], monster_type);
417 /* Delete from this floor */
418 delete_monster_idx(i);
422 if (record_named_pet)
424 for (i = m_max - 1; i >=1; i--)
426 monster_type *m_ptr = &m_list[i];
429 if (!m_ptr->r_idx) continue;
430 if (!is_pet(m_ptr)) continue;
431 if (!m_ptr->nickname) continue;
432 if (p_ptr->riding == i) continue;
434 monster_desc(m_name, m_ptr, MD_ASSUME_VISIBLE | MD_INDEF_VISIBLE);
435 do_cmd_write_nikki(NIKKI_NAMED_PET, 4, m_name);
440 /* Pet of other pet may disappear. */
441 for (i = m_max - 1; i >=1; i--)
443 monster_type *m_ptr = &m_list[i];
445 /* Are there its parent? */
446 if (m_ptr->parent_m_idx && !m_list[m_ptr->parent_m_idx].r_idx)
448 /* Its parent have gone, it also goes away. */
454 /* Acquire the monster name */
455 monster_desc(m_name, m_ptr, 0);
458 msg_format("%s¤Ï¾Ã¤¨µî¤Ã¤¿¡ª", m_name);
460 msg_format("%^s disappears!", m_name);
464 /* Delete the monster */
465 delete_monster_idx(i);
472 * Pre-calculate the racial counters of preserved pets
473 * To prevent multiple generation of unique monster who is the minion of player
475 void precalc_cur_num_of_pet(void)
479 int max_num = p_ptr->wild_mode ? 1 : MAX_PARTY_MON;
481 for (i = 0; i < max_num; i++)
483 m_ptr = &party_mon[i];
485 /* Skip empty monsters */
486 if (!m_ptr->r_idx) continue;
488 /* Hack -- Increase the racial counter */
489 real_r_ptr(m_ptr)->cur_num++;
495 * Place preserved pet monsters on new floor
497 static void place_pet(void)
500 int max_num = p_ptr->wild_mode ? 1 : MAX_PARTY_MON;
502 for (i = 0; i < max_num; i++)
506 if (!(party_mon[i].r_idx)) continue;
511 p_ptr->riding = m_idx;
522 for (d = 1; d < 6; d++)
524 for (j = 1000; j > 0; j--)
526 scatter(&cy, &cx, py, px, d, 0);
527 if (monster_can_enter(cy, cx, &r_info[party_mon[i].r_idx], 0)) break;
531 m_idx = (d == 6) ? 0 : m_pop();
536 monster_type *m_ptr = &m_list[m_idx];
539 cave[cy][cx].m_idx = m_idx;
541 m_ptr->r_idx = party_mon[i].r_idx;
543 /* Copy all member of the structure */
544 *m_ptr = party_mon[i];
545 r_ptr = real_r_ptr(m_ptr);
553 m_ptr->hold_o_idx = 0;
556 if ((r_ptr->flags1 & RF1_FORCE_SLEEP) && !ironman_nightmare)
558 /* Monster is still being nice */
559 m_ptr->mflag |= (MFLAG_NICE);
561 /* Must repair monsters */
562 repair_monsters = TRUE;
565 /* Update the monster */
566 update_mon(m_idx, TRUE);
569 /* Pre-calculated in precalc_cur_num_of_pet() */
570 /* r_ptr->cur_num++; */
572 /* Hack -- Count the number of "reproducers" */
573 if (r_ptr->flags2 & RF2_MULTIPLY) num_repro++;
575 /* Hack -- Notice new multi-hued monsters */
577 monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
578 if (ap_r_ptr->flags1 & (RF1_ATTR_MULTI | RF1_SHAPECHANGER))
579 shimmer_monsters = TRUE;
584 monster_type *m_ptr = &party_mon[i];
585 monster_race *r_ptr = real_r_ptr(m_ptr);
588 monster_desc(m_name, m_ptr, 0);
590 msg_format("%s¤È¤Ï¤°¤ì¤Æ¤·¤Þ¤Ã¤¿¡£", m_name);
592 msg_format("You have lost sight of %s.", m_name);
594 if (record_named_pet && m_ptr->nickname)
596 monster_desc(m_name, m_ptr, MD_INDEF_VISIBLE);
597 do_cmd_write_nikki(NIKKI_NAMED_PET, 5, m_name);
600 /* Pre-calculated in precalc_cur_num_of_pet(), but need to decrease */
601 if (r_ptr->cur_num) r_ptr->cur_num--;
605 /* For accuracy of precalc_cur_num_of_pet() */
606 C_WIPE(party_mon, MAX_PARTY_MON, monster_type);
611 * Hack -- Update location of unique monsters and artifacts
613 * The r_ptr->floor_id and a_ptr->floor_id are not updated correctly
614 * while new floor creation since dungeons may be re-created by
617 static void update_unique_artifact(s16b cur_floor_id)
621 /* Maintain unique monsters */
622 for (i = 1; i < m_max; i++)
625 monster_type *m_ptr = &m_list[i];
627 /* Skip dead monsters */
628 if (!m_ptr->r_idx) continue;
630 /* Extract real monster race */
631 r_ptr = real_r_ptr(m_ptr);
633 /* Memorize location of the unique monster */
634 if ((r_ptr->flags1 & RF1_UNIQUE) ||
635 (r_ptr->flags7 & RF7_NAZGUL))
637 r_ptr->floor_id = cur_floor_id;
641 /* Maintain artifatcs */
642 for (i = 1; i < o_max; i++)
644 object_type *o_ptr = &o_list[i];
646 /* Skip dead objects */
647 if (!o_ptr->k_idx) continue;
649 /* Memorize location of the artifact */
650 if (object_is_fixed_artifact(o_ptr))
652 a_info[o_ptr->name1].floor_id = cur_floor_id;
659 * When a monster is at a place where player will return,
660 * Get out of the my way!
662 static void get_out_monster(void)
668 int m_idx = cave[oy][ox].m_idx;
670 /* Nothing to do if no monster */
673 /* Look until done */
678 /* Pick a (possibly illegal) location */
679 int ny = rand_spread(oy, dis);
680 int nx = rand_spread(ox, dis);
684 /* Stop after 1000 tries */
685 if (tries > 10000) return;
688 * Increase distance after doing enough tries
689 * compared to area of possible space
691 if (tries > 20 * dis * dis) dis++;
693 /* Ignore illegal locations */
694 if (!in_bounds(ny, nx)) continue;
696 /* Require "empty" floor space */
697 if (!cave_empty_bold(ny, nx)) continue;
699 /* Hack -- no teleport onto glyph of warding */
700 if (is_glyph_grid(&cave[ny][nx])) continue;
701 if (is_explosive_rune_grid(&cave[ny][nx])) continue;
703 /* ...nor onto the Pattern */
704 if (pattern_tile(ny, nx)) continue;
706 /*** It's a good place ***/
708 m_ptr = &m_list[m_idx];
710 /* Update the old location */
711 cave[oy][ox].m_idx = 0;
713 /* Update the new location */
714 cave[ny][nx].m_idx = m_idx;
716 /* Move the monster */
720 /* No need to do update_mon() */
729 * Is this feature has special meaning (except floor_id) with c_ptr->special?
731 #define feat_uses_special(F) (have_flag(f_info[(F)].flags, FF_SPECIAL))
735 * Virtually teleport onto the stairs that is connecting between two
738 * Teleport level spell and trap doors will always lead the player to
739 * the one of the floors connected by the one of the stairs in the
742 static void locate_connected_stairs(saved_floor_type *sf_ptr)
744 int x, y, sx = 0, sy = 0;
750 /* Search usable stairs */
751 for (y = 0; y < cur_hgt; y++)
753 for (x = 0; x < cur_wid; x++)
755 cave_type *c_ptr = &cave[y][x];
756 feature_type *f_ptr = &f_info[c_ptr->feat];
759 if (change_floor_mode & CFM_UP)
761 if (have_flag(f_ptr->flags, FF_LESS) && have_flag(f_ptr->flags, FF_STAIRS) &&
762 !have_flag(f_ptr->flags, FF_SPECIAL))
766 /* Found fixed stairs? */
767 if (c_ptr->special &&
768 c_ptr->special == sf_ptr->upper_floor_id)
776 else if (change_floor_mode & CFM_DOWN)
778 if (have_flag(f_ptr->flags, FF_MORE) && have_flag(f_ptr->flags, FF_STAIRS) &&
779 !have_flag(f_ptr->flags, FF_SPECIAL))
783 /* Found fixed stairs */
784 if (c_ptr->special &&
785 c_ptr->special == sf_ptr->lower_floor_id)
795 if (have_flag(f_ptr->flags, FF_BLDG))
801 if (ok && (num < 20))
818 /* No stairs found! -- No return */
819 prepare_change_floor_mode(CFM_RAND_PLACE | CFM_NO_RETURN);
821 /* Mega Hack -- It's not the stairs you enter. Disable it. */
822 if (!feat_uses_special(cave[py][px].feat)) cave[py][px].special = 0;
826 /* Choose random one */
829 /* Point stair location */
836 * Maintain quest monsters, mark next floor_id at stairs, save current
837 * floor, and prepare to enter next floor.
839 void leave_floor(void)
841 cave_type *c_ptr = NULL;
843 saved_floor_type *sf_ptr;
847 /* Preserve pets and prepare to take these to next floor */
850 /* Remove all mirrors without explosion */
851 remove_all_mirrors(FALSE);
853 if (p_ptr->special_defense & NINJA_S_STEALTH) set_superstealth(FALSE);
855 /* New floor is not yet prepared */
858 /* Temporary get a floor_id (for Arena) */
859 if (!p_ptr->floor_id &&
860 (change_floor_mode & CFM_SAVE_FLOORS) &&
861 !(change_floor_mode & CFM_NO_RETURN))
863 /* Get temporal floor_id */
864 p_ptr->floor_id = get_new_floor_id();
866 /* Record the dungeon level */
867 get_sf_ptr(p_ptr->floor_id)->dun_level = dun_level;
871 /* Search the quest monster index */
872 for (i = 0; i < max_quests; i++)
874 if ((quest[i].status == QUEST_STATUS_TAKEN) &&
875 ((quest[i].type == QUEST_TYPE_KILL_LEVEL) ||
876 (quest[i].type == QUEST_TYPE_RANDOM)) &&
877 (quest[i].level == dun_level) &&
878 (dungeon_type == quest[i].dungeon) &&
879 !(quest[i].flags & QUEST_FLAG_PRESET))
881 quest_r_idx = quest[i].r_idx;
885 /* Maintain quest monsters */
886 for (i = 1; i < m_max; i++)
889 monster_type *m_ptr = &m_list[i];
891 /* Skip dead monsters */
892 if (!m_ptr->r_idx) continue;
894 /* Only maintain quest monsters */
895 if (quest_r_idx != m_ptr->r_idx) continue;
897 /* Extract real monster race */
898 r_ptr = real_r_ptr(m_ptr);
900 /* Ignore unique monsters */
901 if ((r_ptr->flags1 & RF1_UNIQUE) ||
902 (r_ptr->flags7 & RF7_NAZGUL)) continue;
904 /* Delete non-unique quest monsters */
905 delete_monster_idx(i);
908 /* Check if there is a same item */
909 for (i = 0; i < INVEN_PACK; i++)
911 object_type *o_ptr = &inventory[i];
913 /* Skip dead objects */
914 if (!o_ptr->k_idx) continue;
916 /* Delete old memorized location of the artifact */
917 if (object_is_fixed_artifact(o_ptr))
919 a_info[o_ptr->name1].floor_id = 0;
923 /* Extract current floor info or NULL */
924 sf_ptr = get_sf_ptr(p_ptr->floor_id);
926 /* Choose random stairs */
927 if ((change_floor_mode & CFM_RAND_CONNECT) && p_ptr->floor_id)
929 locate_connected_stairs(sf_ptr);
932 /* Extract new dungeon level */
933 if (change_floor_mode & CFM_SAVE_FLOORS)
935 /* Extract stair position */
936 c_ptr = &cave[py][px];
937 f_ptr = &f_info[c_ptr->feat];
939 /* Get back to old saved floor? */
940 if (c_ptr->special && !have_flag(f_ptr->flags, FF_SPECIAL) && get_sf_ptr(c_ptr->special))
942 /* Saved floor is exist. Use it. */
943 new_floor_id = c_ptr->special;
946 /* Mark shaft up/down */
947 if (have_flag(f_ptr->flags, FF_STAIRS) && have_flag(f_ptr->flags, FF_SHAFT))
949 prepare_change_floor_mode(CFM_SHAFT);
953 /* Climb up/down some sort of stairs */
954 if (change_floor_mode & (CFM_DOWN | CFM_UP))
958 /* Extract level movement number */
959 if (change_floor_mode & CFM_DOWN) move_num = 1;
960 else if (change_floor_mode & CFM_UP) move_num = -1;
962 /* Shafts are deeper than normal stairs */
963 if (change_floor_mode & CFM_SHAFT)
964 move_num += SGN(move_num);
966 /* Get out from or Enter the dungeon */
967 if (change_floor_mode & CFM_DOWN)
970 move_num = d_info[dungeon_type].mindepth;
972 else if (change_floor_mode & CFM_UP)
974 if (dun_level + move_num < d_info[dungeon_type].mindepth)
975 move_num = -dun_level;
978 dun_level += move_num;
981 /* Leaving the dungeon to town */
982 if (!dun_level && dungeon_type)
984 p_ptr->leaving_dungeon = TRUE;
985 if (!vanilla_town && !lite_town)
987 p_ptr->wilderness_y = d_info[dungeon_type].dy;
988 p_ptr->wilderness_x = d_info[dungeon_type].dx;
990 p_ptr->recall_dungeon = dungeon_type;
993 /* Reach to the surface -- Clear all saved floors */
994 change_floor_mode &= ~CFM_SAVE_FLOORS;
997 /* Kill some old saved floors */
998 if (!(change_floor_mode & CFM_SAVE_FLOORS))
1002 /* Kill all saved floors */
1003 for (i = 0; i < MAX_SAVED_FLOORS; i++)
1004 kill_saved_floor(&saved_floors[i]);
1006 /* Reset visit_mark count */
1007 latest_visit_mark = 1;
1009 else if (change_floor_mode & CFM_NO_RETURN)
1011 /* Kill current floor */
1012 kill_saved_floor(sf_ptr);
1015 /* No current floor -- Left/Enter dungeon etc... */
1016 if (!p_ptr->floor_id)
1018 /* No longer need to save current floor */
1023 /* Mark next floor_id on the previous floor */
1027 new_floor_id = get_new_floor_id();
1029 /* Connect from here */
1030 if (c_ptr && !feat_uses_special(c_ptr->feat))
1032 c_ptr->special = new_floor_id;
1035 /* Record new dungeon level */
1036 get_sf_ptr(new_floor_id)->dun_level = dun_level;
1039 /* Fix connection -- level teleportation or trap door */
1040 if (change_floor_mode & CFM_RAND_CONNECT)
1042 if (change_floor_mode & CFM_UP)
1043 sf_ptr->upper_floor_id = new_floor_id;
1044 else if (change_floor_mode & CFM_DOWN)
1045 sf_ptr->lower_floor_id = new_floor_id;
1048 /* If you can return, you need to save previous floor */
1049 if ((change_floor_mode & CFM_SAVE_FLOORS) &&
1050 !(change_floor_mode & CFM_NO_RETURN))
1052 /* Get out of the my way! */
1055 /* Record the last visit turn of current floor */
1056 sf_ptr->last_visit = turn;
1058 /* Forget the lite */
1061 /* Forget the view */
1064 /* Forget the view */
1067 /* Save current floor */
1068 if (!save_floor(sf_ptr, 0))
1070 /* Save failed -- No return */
1071 prepare_change_floor_mode(CFM_NO_RETURN);
1073 /* Kill current floor */
1074 kill_saved_floor(get_sf_ptr(p_ptr->floor_id));
1081 * Enter new floor. If the floor is an old saved floor, it will be
1082 * restored from the temporal file. If the floor is new one, new cave
1083 * will be generated.
1085 void change_floor(void)
1087 saved_floor_type *sf_ptr;
1088 bool loaded = FALSE;
1090 /* The dungeon is not ready */
1091 character_dungeon = FALSE;
1093 /* No longer in the trap detecteded region */
1094 p_ptr->dtrap = FALSE;
1096 /* Mega-Hack -- no panel yet */
1102 /* Mega-Hack -- not ambushed on the wildness? */
1103 ambush_flag = FALSE;
1105 /* No saved floors (On the surface etc.) */
1106 if (!(change_floor_mode & CFM_SAVE_FLOORS) &&
1107 !(change_floor_mode & CFM_FIRST_FLOOR))
1112 /* Paranoia -- No new saved floor */
1116 /* In the dungeon */
1119 /* No floor_id yet */
1123 new_floor_id = get_new_floor_id();
1126 /* Pointer for infomations of new floor */
1127 sf_ptr = get_sf_ptr(new_floor_id);
1129 /* Try to restore old floor */
1130 if (sf_ptr->last_visit)
1132 /* Old saved floor is exist */
1133 if (load_floor(sf_ptr, 0))
1137 /* Forbid return stairs */
1138 if (change_floor_mode & CFM_NO_RETURN)
1140 cave_type *c_ptr = &cave[py][px];
1142 if (!feat_uses_special(c_ptr->feat))
1144 if (change_floor_mode & (CFM_DOWN | CFM_UP))
1146 /* Reset to floor */
1147 c_ptr->feat = floor_type[randint0(100)];
1157 * Set lower/upper_floor_id of new floor when the new
1158 * floor is right-above/right-under the current floor.
1160 * Stair creation/Teleport level/Trap door will take
1161 * you the same floor when you used it later again.
1163 if (p_ptr->floor_id)
1165 saved_floor_type *cur_sf_ptr = get_sf_ptr(p_ptr->floor_id);
1167 if (change_floor_mode & CFM_UP)
1169 /* New floor is right-above */
1170 if (cur_sf_ptr->upper_floor_id == new_floor_id)
1171 sf_ptr->lower_floor_id = p_ptr->floor_id;
1173 else if (change_floor_mode & CFM_DOWN)
1175 /* New floor is right-under */
1176 if (cur_sf_ptr->lower_floor_id == new_floor_id)
1177 sf_ptr->upper_floor_id = p_ptr->floor_id;
1181 /* Maintain monsters and artifacts */
1185 s32b absence_ticks = (turn - sf_ptr->last_visit) / TURNS_PER_TICK;
1186 int alloc_chance = d_info[dungeon_type].max_m_alloc_chance;
1189 /* Maintain monsters */
1190 for (i = 1; i < m_max; i++)
1192 monster_race *r_ptr;
1193 monster_type *m_ptr = &m_list[i];
1195 /* Skip dead monsters */
1196 if (!m_ptr->r_idx) continue;
1201 m_ptr->hp = m_ptr->maxhp = m_ptr->max_maxhp;
1206 /* Remove invulnerability */
1207 m_ptr->invulner = 0;
1209 /* Remove fast status */
1212 /* Remove slow status */
1218 /* Remove confusion */
1219 m_ptr->confused = 0;
1222 /* Extract real monster race */
1223 r_ptr = real_r_ptr(m_ptr);
1225 /* Ignore non-unique */
1226 if (!(r_ptr->flags1 & RF1_UNIQUE) &&
1227 !(r_ptr->flags7 & RF7_NAZGUL)) continue;
1229 /* Appear at a different floor? */
1230 if (r_ptr->floor_id != new_floor_id)
1232 /* Disapper from here */
1233 delete_monster_idx(i);
1237 /* Maintain artifatcs */
1238 for (i = 1; i < o_max; i++)
1240 object_type *o_ptr = &o_list[i];
1242 /* Skip dead objects */
1243 if (!o_ptr->k_idx) continue;
1245 /* Ignore non-artifact */
1246 if (!object_is_fixed_artifact(o_ptr)) continue;
1248 /* Appear at a different floor? */
1249 if (a_info[o_ptr->name1].floor_id != new_floor_id)
1251 /* Disappear from here */
1252 delete_object_idx(i);
1256 /* Cancel preserve */
1257 a_info[o_ptr->name1].cur_num = 1;
1261 place_quest_monsters();
1263 /* Place some random monsters */
1264 alloc_times = absence_ticks / alloc_chance;
1266 if (randint0(alloc_chance) < (absence_ticks % alloc_chance))
1269 for (i = 0; i < alloc_times; i++)
1271 /* Make a (group of) new monster */
1272 (void)alloc_monster(0, 0);
1276 /* New floor_id or failed to restore */
1277 else /* if (!loaded) */
1279 if (sf_ptr->last_visit)
1281 /* Temporal file is broken? */
1283 msg_print("³¬ÃʤϹԤ»ß¤Þ¤ê¤À¤Ã¤¿¡£");
1285 msg_print("The staircases come to a dead end...");
1288 /* Create simple dead end */
1291 /* Break connection */
1292 if (change_floor_mode & CFM_UP)
1294 sf_ptr->upper_floor_id = 0;
1296 else if (change_floor_mode & CFM_DOWN)
1298 sf_ptr->lower_floor_id = 0;
1303 /* Newly create cave */
1307 /* Record last visit turn */
1308 sf_ptr->last_visit = turn;
1310 /* Set correct dun_level value */
1311 sf_ptr->dun_level = dun_level;
1313 /* Create connected stairs */
1314 if (!(change_floor_mode & CFM_NO_RETURN))
1316 /* Extract stair position */
1317 cave_type *c_ptr = &cave[py][px];
1319 /*** Create connected stairs ***/
1321 /* No stairs down from Quest */
1322 if ((change_floor_mode & CFM_UP) && !quest_number(dun_level))
1324 if (change_floor_mode & CFM_SHAFT)
1325 c_ptr->feat = FEAT_MORE_MORE;
1327 c_ptr->feat = FEAT_MORE;
1330 /* No stairs up when ironman_downward */
1331 else if ((change_floor_mode & CFM_DOWN) && !ironman_downward)
1333 if (change_floor_mode & CFM_SHAFT)
1334 c_ptr->feat = FEAT_LESS_LESS;
1336 c_ptr->feat = FEAT_LESS;
1339 /* Paranoia -- Clear mimic */
1342 /* Connect to previous floor */
1343 c_ptr->special = p_ptr->floor_id;
1347 /* Arrive at random grid */
1348 if (change_floor_mode & (CFM_RAND_PLACE))
1350 (void)new_player_spot();
1353 /* You see stairs blocked */
1354 else if ((change_floor_mode & CFM_NO_RETURN) &&
1355 (change_floor_mode & (CFM_DOWN | CFM_UP)))
1360 msg_print("ÆÍÁ³³¬Ãʤ¬ºÉ¤¬¤ì¤Æ¤·¤Þ¤Ã¤¿¡£");
1362 msg_print("Suddenly the stairs is blocked!");
1368 msg_print("¥´¥È¥´¥È¤È²¿¤«²»¤¬¤·¤¿¡£");
1370 msg_print("You hear some noises.");
1378 * The "turn" is not always different number because
1379 * the level teleport doesn't take any turn. Use
1380 * visit mark instead of last visit turn to find the
1381 * oldest saved floor.
1383 sf_ptr->visit_mark = latest_visit_mark++;
1386 /* Place preserved pet monsters */
1389 /* Hack -- maintain unique and artifacts */
1390 update_unique_artifact(new_floor_id);
1392 /* Now the player is in new floor */
1393 p_ptr->floor_id = new_floor_id;
1395 /* The dungeon is ready */
1396 character_dungeon = TRUE;
1398 /* Hack -- Munchkin characters always get whole map */
1399 if (p_ptr->pseikaku == SEIKAKU_MUNCHKIN)
1400 wiz_lite((bool)(p_ptr->pclass == CLASS_NINJA));
1402 /* Remember when this level was "created" */
1405 /* No dungeon feeling yet */
1406 p_ptr->feeling_turn = old_turn;
1409 /* Clear all flags */
1410 change_floor_mode = 0L;
1416 * Create stairs at or move previously created stairs into the player
1419 void stair_creation(void)
1421 saved_floor_type *sf_ptr;
1422 saved_floor_type *dest_sf_ptr;
1426 s16b dest_floor_id = 0;
1429 /* Forbid up staircases on Ironman mode */
1430 if (ironman_downward) up = FALSE;
1432 /* Forbid down staircases on quest level */
1433 if (quest_number(dun_level) || (dun_level >= d_info[dungeon_type].maxdepth)) down = FALSE;
1435 /* No effect out of standard dungeon floor */
1436 if (!dun_level || (!up && !down) ||
1437 (p_ptr->inside_quest && is_fixed_quest_idx(p_ptr->inside_quest)) ||
1438 p_ptr->inside_arena || p_ptr->inside_battle)
1440 /* arena or quest */
1442 msg_print("¸ú²Ì¤¬¤¢¤ê¤Þ¤»¤ó¡ª");
1444 msg_print("There is no effect!");
1449 /* Artifacts resists */
1450 if (!cave_valid_bold(py, px))
1453 msg_print("¾²¾å¤Î¥¢¥¤¥Æ¥à¤¬¼öʸ¤òÄ·¤ÍÊÖ¤·¤¿¡£");
1455 msg_print("The object resists the spell.");
1461 /* Destroy all objects in the grid */
1462 delete_object(py, px);
1464 /* Extract current floor data */
1465 sf_ptr = get_sf_ptr(p_ptr->floor_id);
1467 /* Choose randomly */
1470 if (randint0(100) < 50) up = FALSE;
1474 /* Destination is already fixed */
1477 if (sf_ptr->upper_floor_id) dest_floor_id = sf_ptr->upper_floor_id;
1481 if (sf_ptr->lower_floor_id) dest_floor_id = sf_ptr->lower_floor_id;
1485 /* Search old stairs leading to the destination */
1490 for (y = 0; y < cur_hgt; y++)
1492 for (x = 0; x < cur_wid; x++)
1494 cave_type *c_ptr = &cave[y][x];
1496 if (!c_ptr->special) continue;
1497 if (feat_uses_special(c_ptr->feat)) continue;
1498 if (c_ptr->special != dest_floor_id) continue;
1500 /* Remove old stairs */
1502 cave_set_feat(y, x, floor_type[randint0(100)]);
1507 /* No old destination -- Get new one now */
1510 dest_floor_id = get_new_floor_id();
1514 sf_ptr->upper_floor_id = dest_floor_id;
1516 sf_ptr->lower_floor_id = dest_floor_id;
1519 /* Extract destination floor data */
1520 dest_sf_ptr = get_sf_ptr(dest_floor_id);
1523 /* Create a staircase */
1526 if (dest_sf_ptr->last_visit && dest_sf_ptr->dun_level <= dun_level - 2)
1527 cave_set_feat(py, px, FEAT_LESS_LESS);
1529 cave_set_feat(py, px, FEAT_LESS);
1533 if (dest_sf_ptr->last_visit && dest_sf_ptr->dun_level >= dun_level + 2)
1534 cave_set_feat(py, px, FEAT_MORE_MORE);
1536 cave_set_feat(py, px, FEAT_MORE);
1540 /* Connect this stairs to the destination */
1541 cave[py][px].special = dest_floor_id;