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];
198 if (!sf_ptr->floor_id) return;
200 if (sf_ptr->floor_id == p_ptr->floor_id)
202 /* Kill current floor */
205 /* Current floor doesn't have temporal file */
210 sprintf(floor_savefile, "%s.F%02d", savefile, (int)sf_ptr->savefile_id);
212 /* Grab permissions */
215 /* Simply kill the temporal file */
216 (void)fd_kill(floor_savefile);
218 /* Drop permissions */
222 /* No longer exists */
223 sf_ptr->floor_id = 0;
228 * Initialize new saved floor and get its floor id. If number of
229 * saved floors are already MAX_SAVED_FLOORS, kill the oldest one.
231 s16b get_new_floor_id(void)
233 saved_floor_type *sf_ptr;
236 /* Look for empty space */
237 for (i = 0; i < MAX_SAVED_FLOORS; i++)
239 sf_ptr = &saved_floors[i];
241 if (!sf_ptr->floor_id) break;
245 if (i == MAX_SAVED_FLOORS)
248 u32b oldest_visit = 0xffffffffL;
250 /* Search for oldest */
251 for (i = 0; i < MAX_SAVED_FLOORS; i++)
253 sf_ptr = &saved_floors[i];
255 /* Don't kill current floor */
256 if (sf_ptr->floor_id == p_ptr->floor_id) continue;
258 /* Don't kill newer */
259 if (sf_ptr->visit_mark > oldest_visit) continue;
262 oldest_visit = sf_ptr->visit_mark;
265 /* Kill oldest saved floor */
266 sf_ptr = &saved_floors[oldest];
267 kill_saved_floor(sf_ptr);
273 /* Prepare new floor data */
274 sf_ptr->savefile_id = i;
275 sf_ptr->floor_id = max_floor_id;
276 sf_ptr->last_visit = 0;
277 sf_ptr->upper_floor_id = 0;
278 sf_ptr->lower_floor_id = 0;
279 sf_ptr->visit_mark = latest_visit_mark++;
281 /* sf_ptr->dun_level may be changed later */
282 sf_ptr->dun_level = dun_level;
285 /* Increment number of floor_id */
286 if (max_floor_id < MAX_SHORT) max_floor_id++;
288 /* 32767 floor_ids are all used up! Re-use ancient IDs */
289 else max_floor_id = 1;
291 return sf_ptr->floor_id;
296 * Prepare mode flags of changing floor
298 void prepare_change_floor_mode(u32b mode)
300 change_floor_mode |= mode;
305 * Builds the dead end
307 static void build_dead_end(void)
311 /* Clear and empty the cave */
314 /* Fill the arrays of floors and walls in the good proportions */
315 set_floor_and_wall(0);
318 cur_hgt = SCREEN_HGT;
319 cur_wid = SCREEN_WID;
321 /* Filled with permanent walls */
322 for (y = 0; y < MAX_HGT; y++)
324 for (x = 0; x < MAX_WID; x++)
326 /* Create "solid" perma-wall */
327 place_solid_perm_bold(y, x);
331 /* Place at center of the floor */
335 /* Give one square */
336 place_floor_bold(py, px);
338 wipe_generate_cave_flags();
342 /* Maximum number of preservable pets */
343 #define MAX_PARTY_MON 21
345 static monster_type party_mon[MAX_PARTY_MON];
351 static void preserve_pet(void)
355 for (num = 0; num < MAX_PARTY_MON; num++)
357 party_mon[num].r_idx = 0;
362 monster_type *m_ptr = &m_list[p_ptr->riding];
364 /* Pet of other pet don't follow. */
365 if (m_ptr->parent_m_idx)
368 p_ptr->pet_extra_flags &= ~(PF_RYOUTE);
369 p_ptr->riding_ryoute = p_ptr->old_riding_ryoute = FALSE;
373 /* Preserve the mount */
374 COPY(&party_mon[0], m_ptr, monster_type);
376 /* Delete from this floor */
377 delete_monster_idx(p_ptr->riding);
382 * If player is in wild mode, no pets are preserved
383 * except a monster whom player riding
385 if (!p_ptr->wild_mode && !p_ptr->inside_arena && !p_ptr->inside_battle)
387 for (i = m_max - 1, num = 1; (i >= 1 && num < MAX_PARTY_MON); i--)
389 monster_type *m_ptr = &m_list[i];
391 if (!m_ptr->r_idx) continue;
392 if (!is_pet(m_ptr)) continue;
393 if (i == p_ptr->riding) continue;
395 if (reinit_wilderness)
397 /* Don't lose sight of pets when getting a Quest */
401 int dis = distance(py, px, m_ptr->fy, m_ptr->fx);
403 /* Confused (etc.) monsters don't follow. */
404 if (MON_CONFUSED(m_ptr) || MON_STUNNED(m_ptr) || MON_CSLEEP(m_ptr)) continue;
406 /* Pet of other pet don't follow. */
407 if (m_ptr->parent_m_idx) continue;
410 * Pets with nickname will follow even from 3 blocks away
411 * when you or the pet can see the other.
413 if (m_ptr->nickname &&
414 ((player_has_los_bold(m_ptr->fy, m_ptr->fx) && projectable(py, px, m_ptr->fy, m_ptr->fx)) ||
415 (los(m_ptr->fy, m_ptr->fx, py, px) && projectable(m_ptr->fy, m_ptr->fx, py, px))))
417 if (dis > 3) continue;
421 if (dis > 1) continue;
425 COPY(&party_mon[num], &m_list[i], monster_type);
429 /* Delete from this floor */
430 delete_monster_idx(i);
434 if (record_named_pet)
436 for (i = m_max - 1; i >=1; i--)
438 monster_type *m_ptr = &m_list[i];
441 if (!m_ptr->r_idx) continue;
442 if (!is_pet(m_ptr)) continue;
443 if (!m_ptr->nickname) continue;
444 if (p_ptr->riding == i) continue;
446 monster_desc(m_name, m_ptr, MD_ASSUME_VISIBLE | MD_INDEF_VISIBLE);
447 do_cmd_write_nikki(NIKKI_NAMED_PET, RECORD_NAMED_PET_MOVED, m_name);
452 /* Pet of other pet may disappear. */
453 for (i = m_max - 1; i >=1; i--)
455 monster_type *m_ptr = &m_list[i];
457 /* Are there its parent? */
458 if (m_ptr->parent_m_idx && !m_list[m_ptr->parent_m_idx].r_idx)
460 /* Its parent have gone, it also goes away. */
466 /* Acquire the monster name */
467 monster_desc(m_name, m_ptr, 0);
470 msg_format("%s¤Ï¾Ã¤¨µî¤Ã¤¿¡ª", m_name);
472 msg_format("%^s disappears!", m_name);
476 /* Delete the monster */
477 delete_monster_idx(i);
484 * Pre-calculate the racial counters of preserved pets
485 * To prevent multiple generation of unique monster who is the minion of player
487 void precalc_cur_num_of_pet(void)
491 int max_num = p_ptr->wild_mode ? 1 : MAX_PARTY_MON;
493 for (i = 0; i < max_num; i++)
495 m_ptr = &party_mon[i];
497 /* Skip empty monsters */
498 if (!m_ptr->r_idx) continue;
500 /* Hack -- Increase the racial counter */
501 real_r_ptr(m_ptr)->cur_num++;
507 * Place preserved pet monsters on new floor
509 static void place_pet(void)
512 int max_num = p_ptr->wild_mode ? 1 : MAX_PARTY_MON;
514 for (i = 0; i < max_num; i++)
518 if (!(party_mon[i].r_idx)) continue;
523 p_ptr->riding = m_idx;
534 for (d = 1; d < 6; d++)
536 for (j = 1000; j > 0; j--)
538 scatter(&cy, &cx, py, px, d, 0);
539 if (monster_can_enter(cy, cx, &r_info[party_mon[i].r_idx], 0)) break;
543 m_idx = (d == 6) ? 0 : m_pop();
548 monster_type *m_ptr = &m_list[m_idx];
551 cave[cy][cx].m_idx = m_idx;
553 m_ptr->r_idx = party_mon[i].r_idx;
555 /* Copy all member of the structure */
556 *m_ptr = party_mon[i];
557 r_ptr = real_r_ptr(m_ptr);
562 m_ptr->mtimed[MTIMED_CSLEEP] = 0;
565 m_ptr->hold_o_idx = 0;
568 if ((r_ptr->flags1 & RF1_FORCE_SLEEP) && !ironman_nightmare)
570 /* Monster is still being nice */
571 m_ptr->mflag |= (MFLAG_NICE);
573 /* Must repair monsters */
574 repair_monsters = TRUE;
577 /* Update the monster */
578 update_mon(m_idx, TRUE);
581 /* Pre-calculated in precalc_cur_num_of_pet() */
582 /* r_ptr->cur_num++; */
584 /* Hack -- Count the number of "reproducers" */
585 if (r_ptr->flags2 & RF2_MULTIPLY) num_repro++;
587 /* Hack -- Notice new multi-hued monsters */
589 monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
590 if (ap_r_ptr->flags1 & (RF1_ATTR_MULTI | RF1_SHAPECHANGER))
591 shimmer_monsters = TRUE;
596 monster_type *m_ptr = &party_mon[i];
597 monster_race *r_ptr = real_r_ptr(m_ptr);
600 monster_desc(m_name, m_ptr, 0);
602 msg_format("%s¤È¤Ï¤°¤ì¤Æ¤·¤Þ¤Ã¤¿¡£", m_name);
604 msg_format("You have lost sight of %s.", m_name);
606 if (record_named_pet && m_ptr->nickname)
608 monster_desc(m_name, m_ptr, MD_INDEF_VISIBLE);
609 do_cmd_write_nikki(NIKKI_NAMED_PET, RECORD_NAMED_PET_LOST_SIGHT, m_name);
612 /* Pre-calculated in precalc_cur_num_of_pet(), but need to decrease */
613 if (r_ptr->cur_num) r_ptr->cur_num--;
617 /* For accuracy of precalc_cur_num_of_pet() */
618 C_WIPE(party_mon, MAX_PARTY_MON, monster_type);
623 * Hack -- Update location of unique monsters and artifacts
625 * The r_ptr->floor_id and a_ptr->floor_id are not updated correctly
626 * while new floor creation since dungeons may be re-created by
629 static void update_unique_artifact(s16b cur_floor_id)
633 /* Maintain unique monsters */
634 for (i = 1; i < m_max; i++)
637 monster_type *m_ptr = &m_list[i];
639 /* Skip dead monsters */
640 if (!m_ptr->r_idx) continue;
642 /* Extract real monster race */
643 r_ptr = real_r_ptr(m_ptr);
645 /* Memorize location of the unique monster */
646 if ((r_ptr->flags1 & RF1_UNIQUE) ||
647 (r_ptr->flags7 & RF7_NAZGUL))
649 r_ptr->floor_id = cur_floor_id;
653 /* Maintain artifatcs */
654 for (i = 1; i < o_max; i++)
656 object_type *o_ptr = &o_list[i];
658 /* Skip dead objects */
659 if (!o_ptr->k_idx) continue;
661 /* Memorize location of the artifact */
662 if (object_is_fixed_artifact(o_ptr))
664 a_info[o_ptr->name1].floor_id = cur_floor_id;
671 * When a monster is at a place where player will return,
672 * Get out of the my way!
674 static void get_out_monster(void)
680 int m_idx = cave[oy][ox].m_idx;
682 /* Nothing to do if no monster */
685 /* Look until done */
690 /* Pick a (possibly illegal) location */
691 int ny = rand_spread(oy, dis);
692 int nx = rand_spread(ox, dis);
696 /* Stop after 1000 tries */
697 if (tries > 10000) return;
700 * Increase distance after doing enough tries
701 * compared to area of possible space
703 if (tries > 20 * dis * dis) dis++;
705 /* Ignore illegal locations */
706 if (!in_bounds(ny, nx)) continue;
708 /* Require "empty" floor space */
709 if (!cave_empty_bold(ny, nx)) continue;
711 /* Hack -- no teleport onto glyph of warding */
712 if (is_glyph_grid(&cave[ny][nx])) continue;
713 if (is_explosive_rune_grid(&cave[ny][nx])) continue;
715 /* ...nor onto the Pattern */
716 if (pattern_tile(ny, nx)) continue;
718 /*** It's a good place ***/
720 m_ptr = &m_list[m_idx];
722 /* Update the old location */
723 cave[oy][ox].m_idx = 0;
725 /* Update the new location */
726 cave[ny][nx].m_idx = m_idx;
728 /* Move the monster */
732 /* No need to do update_mon() */
741 * Is this feature has special meaning (except floor_id) with c_ptr->special?
743 #define feat_uses_special(F) (have_flag(f_info[(F)].flags, FF_SPECIAL))
747 * Virtually teleport onto the stairs that is connecting between two
750 * Teleport level spell and trap doors will always lead the player to
751 * the one of the floors connected by the one of the stairs in the
754 static void locate_connected_stairs(saved_floor_type *sf_ptr)
756 int x, y, sx = 0, sy = 0;
762 /* Search usable stairs */
763 for (y = 0; y < cur_hgt; y++)
765 for (x = 0; x < cur_wid; x++)
767 cave_type *c_ptr = &cave[y][x];
768 feature_type *f_ptr = &f_info[c_ptr->feat];
771 if (change_floor_mode & CFM_UP)
773 if (have_flag(f_ptr->flags, FF_LESS) && have_flag(f_ptr->flags, FF_STAIRS) &&
774 !have_flag(f_ptr->flags, FF_SPECIAL))
778 /* Found fixed stairs? */
779 if (c_ptr->special &&
780 c_ptr->special == sf_ptr->upper_floor_id)
788 else if (change_floor_mode & CFM_DOWN)
790 if (have_flag(f_ptr->flags, FF_MORE) && have_flag(f_ptr->flags, FF_STAIRS) &&
791 !have_flag(f_ptr->flags, FF_SPECIAL))
795 /* Found fixed stairs */
796 if (c_ptr->special &&
797 c_ptr->special == sf_ptr->lower_floor_id)
807 if (have_flag(f_ptr->flags, FF_BLDG))
813 if (ok && (num < 20))
830 /* No stairs found! -- No return */
831 prepare_change_floor_mode(CFM_RAND_PLACE | CFM_NO_RETURN);
833 /* Mega Hack -- It's not the stairs you enter. Disable it. */
834 if (!feat_uses_special(cave[py][px].feat)) cave[py][px].special = 0;
838 /* Choose random one */
841 /* Point stair location */
848 * Maintain quest monsters, mark next floor_id at stairs, save current
849 * floor, and prepare to enter next floor.
851 void leave_floor(void)
853 cave_type *c_ptr = NULL;
855 saved_floor_type *sf_ptr;
859 /* Preserve pets and prepare to take these to next floor */
862 /* Remove all mirrors without explosion */
863 remove_all_mirrors(FALSE);
865 if (p_ptr->special_defense & NINJA_S_STEALTH) set_superstealth(FALSE);
867 /* New floor is not yet prepared */
870 /* Temporary get a floor_id (for Arena) */
871 if (!p_ptr->floor_id &&
872 (change_floor_mode & CFM_SAVE_FLOORS) &&
873 !(change_floor_mode & CFM_NO_RETURN))
875 /* Get temporal floor_id */
876 p_ptr->floor_id = get_new_floor_id();
880 /* Search the quest monster index */
881 for (i = 0; i < max_quests; i++)
883 if ((quest[i].status == QUEST_STATUS_TAKEN) &&
884 ((quest[i].type == QUEST_TYPE_KILL_LEVEL) ||
885 (quest[i].type == QUEST_TYPE_RANDOM)) &&
886 (quest[i].level == dun_level) &&
887 (dungeon_type == quest[i].dungeon) &&
888 !(quest[i].flags & QUEST_FLAG_PRESET))
890 quest_r_idx = quest[i].r_idx;
894 /* Maintain quest monsters */
895 for (i = 1; i < m_max; i++)
898 monster_type *m_ptr = &m_list[i];
900 /* Skip dead monsters */
901 if (!m_ptr->r_idx) continue;
903 /* Only maintain quest monsters */
904 if (quest_r_idx != m_ptr->r_idx) continue;
906 /* Extract real monster race */
907 r_ptr = real_r_ptr(m_ptr);
909 /* Ignore unique monsters */
910 if ((r_ptr->flags1 & RF1_UNIQUE) ||
911 (r_ptr->flags7 & RF7_NAZGUL)) continue;
913 /* Delete non-unique quest monsters */
914 delete_monster_idx(i);
917 /* Check if there is a same item */
918 for (i = 0; i < INVEN_PACK; i++)
920 object_type *o_ptr = &inventory[i];
922 /* Skip dead objects */
923 if (!o_ptr->k_idx) continue;
925 /* Delete old memorized location of the artifact */
926 if (object_is_fixed_artifact(o_ptr))
928 a_info[o_ptr->name1].floor_id = 0;
932 /* Extract current floor info or NULL */
933 sf_ptr = get_sf_ptr(p_ptr->floor_id);
935 /* Choose random stairs */
936 if ((change_floor_mode & CFM_RAND_CONNECT) && p_ptr->floor_id)
938 locate_connected_stairs(sf_ptr);
941 /* Extract new dungeon level */
942 if (change_floor_mode & CFM_SAVE_FLOORS)
944 /* Extract stair position */
945 c_ptr = &cave[py][px];
946 f_ptr = &f_info[c_ptr->feat];
948 /* Get back to old saved floor? */
949 if (c_ptr->special && !have_flag(f_ptr->flags, FF_SPECIAL) && get_sf_ptr(c_ptr->special))
951 /* Saved floor is exist. Use it. */
952 new_floor_id = c_ptr->special;
955 /* Mark shaft up/down */
956 if (have_flag(f_ptr->flags, FF_STAIRS) && have_flag(f_ptr->flags, FF_SHAFT))
958 prepare_change_floor_mode(CFM_SHAFT);
962 /* Climb up/down some sort of stairs */
963 if (change_floor_mode & (CFM_DOWN | CFM_UP))
967 /* Extract level movement number */
968 if (change_floor_mode & CFM_DOWN) move_num = 1;
969 else if (change_floor_mode & CFM_UP) move_num = -1;
971 /* Shafts are deeper than normal stairs */
972 if (change_floor_mode & CFM_SHAFT)
973 move_num += SGN(move_num);
975 /* Get out from or Enter the dungeon */
976 if (change_floor_mode & CFM_DOWN)
979 move_num = d_info[dungeon_type].mindepth;
981 else if (change_floor_mode & CFM_UP)
983 if (dun_level + move_num < d_info[dungeon_type].mindepth)
984 move_num = -dun_level;
987 dun_level += move_num;
990 /* Leaving the dungeon to town */
991 if (!dun_level && dungeon_type)
993 p_ptr->leaving_dungeon = TRUE;
994 if (!vanilla_town && !lite_town)
996 p_ptr->wilderness_y = d_info[dungeon_type].dy;
997 p_ptr->wilderness_x = d_info[dungeon_type].dx;
999 p_ptr->recall_dungeon = dungeon_type;
1002 /* Reach to the surface -- Clear all saved floors */
1003 change_floor_mode &= ~CFM_SAVE_FLOORS;
1006 /* Kill some old saved floors */
1007 if (!(change_floor_mode & CFM_SAVE_FLOORS))
1011 /* Kill all saved floors */
1012 for (i = 0; i < MAX_SAVED_FLOORS; i++)
1013 kill_saved_floor(&saved_floors[i]);
1015 /* Reset visit_mark count */
1016 latest_visit_mark = 1;
1018 else if (change_floor_mode & CFM_NO_RETURN)
1020 /* Kill current floor */
1021 kill_saved_floor(sf_ptr);
1024 /* No current floor -- Left/Enter dungeon etc... */
1025 if (!p_ptr->floor_id)
1027 /* No longer need to save current floor */
1032 /* Mark next floor_id on the previous floor */
1036 new_floor_id = get_new_floor_id();
1038 /* Connect from here */
1039 if (c_ptr && !feat_uses_special(c_ptr->feat))
1041 c_ptr->special = new_floor_id;
1045 /* Fix connection -- level teleportation or trap door */
1046 if (change_floor_mode & CFM_RAND_CONNECT)
1048 if (change_floor_mode & CFM_UP)
1049 sf_ptr->upper_floor_id = new_floor_id;
1050 else if (change_floor_mode & CFM_DOWN)
1051 sf_ptr->lower_floor_id = new_floor_id;
1054 /* If you can return, you need to save previous floor */
1055 if ((change_floor_mode & CFM_SAVE_FLOORS) &&
1056 !(change_floor_mode & CFM_NO_RETURN))
1058 /* Get out of the my way! */
1061 /* Record the last visit turn of current floor */
1062 sf_ptr->last_visit = turn;
1064 /* Forget the lite */
1067 /* Forget the view */
1070 /* Forget the view */
1073 /* Save current floor */
1074 if (!save_floor(sf_ptr, 0))
1076 /* Save failed -- No return */
1077 prepare_change_floor_mode(CFM_NO_RETURN);
1079 /* Kill current floor */
1080 kill_saved_floor(get_sf_ptr(p_ptr->floor_id));
1087 * Enter new floor. If the floor is an old saved floor, it will be
1088 * restored from the temporal file. If the floor is new one, new cave
1089 * will be generated.
1091 void change_floor(void)
1093 saved_floor_type *sf_ptr;
1094 bool loaded = FALSE;
1096 /* The dungeon is not ready */
1097 character_dungeon = FALSE;
1099 /* No longer in the trap detecteded region */
1100 p_ptr->dtrap = FALSE;
1102 /* Mega-Hack -- no panel yet */
1108 /* Mega-Hack -- not ambushed on the wildness? */
1109 ambush_flag = FALSE;
1111 /* No saved floors (On the surface etc.) */
1112 if (!(change_floor_mode & CFM_SAVE_FLOORS) &&
1113 !(change_floor_mode & CFM_FIRST_FLOOR))
1118 /* Paranoia -- No new saved floor */
1122 /* In the dungeon */
1125 /* No floor_id yet */
1129 new_floor_id = get_new_floor_id();
1132 /* Pointer for infomations of new floor */
1133 sf_ptr = get_sf_ptr(new_floor_id);
1135 /* Try to restore old floor */
1136 if (sf_ptr->last_visit)
1138 /* Old saved floor is exist */
1139 if (load_floor(sf_ptr, 0))
1143 /* Forbid return stairs */
1144 if (change_floor_mode & CFM_NO_RETURN)
1146 cave_type *c_ptr = &cave[py][px];
1148 if (!feat_uses_special(c_ptr->feat))
1150 if (change_floor_mode & (CFM_DOWN | CFM_UP))
1152 /* Reset to floor */
1153 c_ptr->feat = floor_type[randint0(100)];
1163 * Set lower/upper_floor_id of new floor when the new
1164 * floor is right-above/right-under the current floor.
1166 * Stair creation/Teleport level/Trap door will take
1167 * you the same floor when you used it later again.
1169 if (p_ptr->floor_id)
1171 saved_floor_type *cur_sf_ptr = get_sf_ptr(p_ptr->floor_id);
1173 if (change_floor_mode & CFM_UP)
1175 /* New floor is right-above */
1176 if (cur_sf_ptr->upper_floor_id == new_floor_id)
1177 sf_ptr->lower_floor_id = p_ptr->floor_id;
1179 else if (change_floor_mode & CFM_DOWN)
1181 /* New floor is right-under */
1182 if (cur_sf_ptr->lower_floor_id == new_floor_id)
1183 sf_ptr->upper_floor_id = p_ptr->floor_id;
1187 /* Break connection to killed floor */
1190 if (change_floor_mode & CFM_UP)
1191 sf_ptr->lower_floor_id = 0;
1192 else if (change_floor_mode & CFM_DOWN)
1193 sf_ptr->upper_floor_id = 0;
1196 /* Maintain monsters and artifacts */
1200 s32b tmp_last_visit = sf_ptr->last_visit;
1202 int alloc_chance = d_info[dungeon_type].max_m_alloc_chance;
1205 while (tmp_last_visit > turn) tmp_last_visit -= TURNS_PER_TICK * TOWN_DAWN;
1206 absence_ticks = (turn - tmp_last_visit) / TURNS_PER_TICK;
1208 /* Maintain monsters */
1209 for (i = 1; i < m_max; i++)
1211 monster_race *r_ptr;
1212 monster_type *m_ptr = &m_list[i];
1214 /* Skip dead monsters */
1215 if (!m_ptr->r_idx) continue;
1220 m_ptr->hp = m_ptr->maxhp = m_ptr->max_maxhp;
1222 /* Remove timed status (except MTIMED_CSLEEP) */
1223 (void)set_monster_fast(i, 0);
1224 (void)set_monster_slow(i, 0);
1225 (void)set_monster_stunned(i, 0);
1226 (void)set_monster_confused(i, 0);
1227 (void)set_monster_monfear(i, 0);
1228 (void)set_monster_invulner(i, 0, FALSE);
1231 /* Extract real monster race */
1232 r_ptr = real_r_ptr(m_ptr);
1234 /* Ignore non-unique */
1235 if (!(r_ptr->flags1 & RF1_UNIQUE) &&
1236 !(r_ptr->flags7 & RF7_NAZGUL)) continue;
1238 /* Appear at a different floor? */
1239 if (r_ptr->floor_id != new_floor_id)
1241 /* Disapper from here */
1242 delete_monster_idx(i);
1246 /* Maintain artifatcs */
1247 for (i = 1; i < o_max; i++)
1249 object_type *o_ptr = &o_list[i];
1251 /* Skip dead objects */
1252 if (!o_ptr->k_idx) continue;
1254 /* Ignore non-artifact */
1255 if (!object_is_fixed_artifact(o_ptr)) continue;
1257 /* Appear at a different floor? */
1258 if (a_info[o_ptr->name1].floor_id != new_floor_id)
1260 /* Disappear from here */
1261 delete_object_idx(i);
1265 /* Cancel preserve */
1266 a_info[o_ptr->name1].cur_num = 1;
1270 (void)place_quest_monsters();
1272 /* Place some random monsters */
1273 alloc_times = absence_ticks / alloc_chance;
1275 if (randint0(alloc_chance) < (absence_ticks % alloc_chance))
1278 for (i = 0; i < alloc_times; i++)
1280 /* Make a (group of) new monster */
1281 (void)alloc_monster(0, 0);
1285 /* New floor_id or failed to restore */
1286 else /* if (!loaded) */
1288 if (sf_ptr->last_visit)
1290 /* Temporal file is broken? */
1292 msg_print("³¬ÃʤϹԤ»ß¤Þ¤ê¤À¤Ã¤¿¡£");
1294 msg_print("The staircases come to a dead end...");
1297 /* Create simple dead end */
1300 /* Break connection */
1301 if (change_floor_mode & CFM_UP)
1303 sf_ptr->upper_floor_id = 0;
1305 else if (change_floor_mode & CFM_DOWN)
1307 sf_ptr->lower_floor_id = 0;
1312 /* Newly create cave */
1316 /* Record last visit turn */
1317 sf_ptr->last_visit = turn;
1319 /* Set correct dun_level value */
1320 sf_ptr->dun_level = dun_level;
1322 /* Create connected stairs */
1323 if (!(change_floor_mode & CFM_NO_RETURN))
1325 /* Extract stair position */
1326 cave_type *c_ptr = &cave[py][px];
1328 /*** Create connected stairs ***/
1330 /* No stairs down from Quest */
1331 if ((change_floor_mode & CFM_UP) && !quest_number(dun_level))
1333 c_ptr->feat = (change_floor_mode & CFM_SHAFT) ? feat_state(feat_down_stair, FF_SHAFT) : feat_down_stair;
1336 /* No stairs up when ironman_downward */
1337 else if ((change_floor_mode & CFM_DOWN) && !ironman_downward)
1339 c_ptr->feat = (change_floor_mode & CFM_SHAFT) ? feat_state(feat_up_stair, FF_SHAFT) : feat_up_stair;
1342 /* Paranoia -- Clear mimic */
1345 /* Connect to previous floor */
1346 c_ptr->special = p_ptr->floor_id;
1350 /* Arrive at random grid */
1351 if (change_floor_mode & (CFM_RAND_PLACE))
1353 (void)new_player_spot();
1356 /* You see stairs blocked */
1357 else if ((change_floor_mode & CFM_NO_RETURN) &&
1358 (change_floor_mode & (CFM_DOWN | CFM_UP)))
1363 msg_print("ÆÍÁ³³¬Ãʤ¬ºÉ¤¬¤ì¤Æ¤·¤Þ¤Ã¤¿¡£");
1365 msg_print("Suddenly the stairs is blocked!");
1371 msg_print("¥´¥È¥´¥È¤È²¿¤«²»¤¬¤·¤¿¡£");
1373 msg_print("You hear some noises.");
1381 * The "turn" is not always different number because
1382 * the level teleport doesn't take any turn. Use
1383 * visit mark instead of last visit turn to find the
1384 * oldest saved floor.
1386 sf_ptr->visit_mark = latest_visit_mark++;
1389 /* Place preserved pet monsters */
1392 /* Hack -- maintain unique and artifacts */
1393 update_unique_artifact(new_floor_id);
1395 /* Now the player is in new floor */
1396 p_ptr->floor_id = new_floor_id;
1398 /* The dungeon is ready */
1399 character_dungeon = TRUE;
1401 /* Hack -- Munchkin characters always get whole map */
1402 if (p_ptr->pseikaku == SEIKAKU_MUNCHKIN)
1403 wiz_lite((bool)(p_ptr->pclass == CLASS_NINJA));
1405 /* Remember when this level was "created" */
1408 /* No dungeon feeling yet */
1409 p_ptr->feeling_turn = old_turn;
1412 /* Clear all flags */
1413 change_floor_mode = 0L;
1419 * Create stairs at or move previously created stairs into the player
1422 void stair_creation(void)
1424 saved_floor_type *sf_ptr;
1425 saved_floor_type *dest_sf_ptr;
1429 s16b dest_floor_id = 0;
1432 /* Forbid up staircases on Ironman mode */
1433 if (ironman_downward) up = FALSE;
1435 /* Forbid down staircases on quest level */
1436 if (quest_number(dun_level) || (dun_level >= d_info[dungeon_type].maxdepth)) down = FALSE;
1438 /* No effect out of standard dungeon floor */
1439 if (!dun_level || (!up && !down) ||
1440 (p_ptr->inside_quest && is_fixed_quest_idx(p_ptr->inside_quest)) ||
1441 p_ptr->inside_arena || p_ptr->inside_battle)
1443 /* arena or quest */
1445 msg_print("¸ú²Ì¤¬¤¢¤ê¤Þ¤»¤ó¡ª");
1447 msg_print("There is no effect!");
1452 /* Artifacts resists */
1453 if (!cave_valid_bold(py, px))
1456 msg_print("¾²¾å¤Î¥¢¥¤¥Æ¥à¤¬¼öʸ¤òÄ·¤ÍÊÖ¤·¤¿¡£");
1458 msg_print("The object resists the spell.");
1464 /* Destroy all objects in the grid */
1465 delete_object(py, px);
1467 /* Extract current floor data */
1468 sf_ptr = get_sf_ptr(p_ptr->floor_id);
1473 /* No floor id? -- Create now! */
1474 p_ptr->floor_id = get_new_floor_id();
1475 sf_ptr = get_sf_ptr(p_ptr->floor_id);
1478 /* Choose randomly */
1481 if (randint0(100) < 50) up = FALSE;
1485 /* Destination is already fixed */
1488 if (sf_ptr->upper_floor_id) dest_floor_id = sf_ptr->upper_floor_id;
1492 if (sf_ptr->lower_floor_id) dest_floor_id = sf_ptr->lower_floor_id;
1496 /* Search old stairs leading to the destination */
1501 for (y = 0; y < cur_hgt; y++)
1503 for (x = 0; x < cur_wid; x++)
1505 cave_type *c_ptr = &cave[y][x];
1507 if (!c_ptr->special) continue;
1508 if (feat_uses_special(c_ptr->feat)) continue;
1509 if (c_ptr->special != dest_floor_id) continue;
1511 /* Remove old stairs */
1513 cave_set_feat(y, x, floor_type[randint0(100)]);
1518 /* No old destination -- Get new one now */
1521 dest_floor_id = get_new_floor_id();
1525 sf_ptr->upper_floor_id = dest_floor_id;
1527 sf_ptr->lower_floor_id = dest_floor_id;
1530 /* Extract destination floor data */
1531 dest_sf_ptr = get_sf_ptr(dest_floor_id);
1534 /* Create a staircase */
1537 cave_set_feat(py, px,
1538 (dest_sf_ptr->last_visit && (dest_sf_ptr->dun_level <= dun_level - 2)) ?
1539 feat_state(feat_up_stair, FF_SHAFT) : feat_up_stair);
1543 cave_set_feat(py, px,
1544 (dest_sf_ptr->last_visit && (dest_sf_ptr->dun_level >= dun_level + 2)) ?
1545 feat_state(feat_down_stair, FF_SHAFT) : feat_down_stair);
1549 /* Connect this stairs to the destination */
1550 cave[py][px].special = dest_floor_id;