/* File: floors.c */

/* Purpose: management of the saved floor */

/*
 * Copyright (c) 2002  Mogami
 *
 * This software may be copied and distributed for educational, research, and
 * not for profit purposes provided that this copyright and statement are
 * included in all such copies.
 */

#include "angband.h"
#include "grid.h"


static s16b new_floor_id;       /* floor_id of the destination */
static u32b change_floor_mode;  /* Mode flags for changing floor */
static u32b latest_visit_mark;  /* Max number of visit_mark */


/*
 * Initialize saved_floors array.  Make sure that old temporal files
 * are not remaining as gurbages.
 */
void init_saved_floors(void)
{
	char floor_savefile[1024];
	int i;
	int fd = -1;
	int mode = 0644;
	bool force = FALSE;

#ifdef SET_UID
# ifdef SECURE
	/* Get "games" permissions */
	beGames();
# endif
#endif

	for (i = 0; i < MAX_SAVED_FLOORS; i++)
	{
		saved_floor_type *sf_ptr = &saved_floors[i];

		/* File name */
		sprintf(floor_savefile, "%s.F%02d", savefile, i);

		/* Try to create the file */
		fd = fd_make(floor_savefile, mode);

		/* Failed! */
		if (fd < 0)
		{
			if (!force)
			{
#ifdef JP
				msg_print("エラー：古いテンポラリ・ファイルが残っています。");
				msg_print("TObandを二重に起動していないか確認してください。");
				msg_print("過去にTObandがクラッシュした場合は一時ファイルを");
				msg_print("強制的に削除して実行を続けられます。");
				if (!get_check("強制的に削除してもよろしいですか？")) quit("実行中止");
#else
				msg_print("Error: There are old temporal files.");
				msg_print("Make sure you are not running two game processes simultaneously.");
				msg_print("If the temporal files are garbages of old crashed process, ");
				msg_print("you can delete it safely.");
				if (!get_check("Do you delete old temporal files? ")) quit("Aborted.");
#endif
				force = TRUE;
			}
		}
		else
		{
			/* Close the "fd" */
			(void)fd_close(fd);
		}

		/* Simply kill the temporal file */ 
		(void)fd_kill(floor_savefile);

		sf_ptr->floor_id = 0;
	}

	/* No floor_id used yet (No.0 is reserved to indicate non existance) */
	max_floor_id = 1;

	/* vist_mark is from 1 */
	latest_visit_mark = 1;

	/* A sign to mark temporal files */
	saved_floor_file_sign = time(NULL);

	/* No next floor yet */
	new_floor_id = 0;

	/* No change floor mode yet */
	change_floor_mode = 0;

#ifdef SET_UID
# ifdef SECURE
	/* Drop "games" permissions */
	bePlayer();
# endif
#endif
}


/*
 * Kill temporal files
 * Should be called just before the game quit
 * and before new game discarding saved game.
 */
void clear_saved_floor_files(void)
{
	char floor_savefile[1024];
	int i;

#ifdef SET_UID
# ifdef SECURE
	/* Get "games" permissions */
	beGames();
# endif
#endif

	for (i = 0; i < MAX_SAVED_FLOORS; i++)
	{
		saved_floor_type *sf_ptr = &saved_floors[i];

		/* No temporal file */
		if (!sf_ptr->floor_id) continue;
		if (sf_ptr->floor_id == p_ptr->floor_id) continue;

		/* File name */
		sprintf(floor_savefile, "%s.F%02d", savefile, i);

		/* Simply kill the temporal file */ 
		(void)fd_kill(floor_savefile);
	}

#ifdef SET_UID
# ifdef SECURE
	/* Drop "games" permissions */
	bePlayer();
# endif
#endif
}


/*
 * Get a pointer for an item of the saved_floors array.
 */
saved_floor_type *get_sf_ptr(s16b floor_id)
{
	int i;

	/* floor_id No.0 indicates no floor */
	if (!floor_id) return NULL;

	for (i = 0; i < MAX_SAVED_FLOORS; i++)
	{
		saved_floor_type *sf_ptr = &saved_floors[i];

		if (sf_ptr->floor_id == floor_id) return sf_ptr;
	}

	/* None found */
	return NULL;
}


/*
 * kill a saved floor and get an empty space
 */
static void kill_saved_floor(saved_floor_type *sf_ptr)
{
	char floor_savefile[1024];

	/* Already empty */
	if (!sf_ptr->floor_id) return;

	if (sf_ptr->floor_id == p_ptr->floor_id)
	{
		/* Kill current floor */
		p_ptr->floor_id = 0;

		/* Current floor doesn't have temporal file */
	}
	else 
	{
		/* File name */
		sprintf(floor_savefile, "%s.F%02d", savefile, (int)sf_ptr->savefile_id);

		/* Simply kill the temporal file */ 
		(void)fd_kill(floor_savefile);
	}

	/* No longer exists */
	sf_ptr->floor_id = 0;
}


/*
 * Initialize new saved floor and get its floor id.  If number of
 * saved floors are already MAX_SAVED_FLOORS, kill the oldest one.
 */
s16b get_new_floor_id(void)
{
	saved_floor_type *sf_ptr;
	int i;

	/* Look for empty space */
	for (i = 0; i < MAX_SAVED_FLOORS; i++)
	{
		sf_ptr = &saved_floors[i];

		if (!sf_ptr->floor_id) break;
	}

	/* None found */
	if (i == MAX_SAVED_FLOORS)
	{
		int oldest = 0;
		u32b oldest_visit = 0xffffffffL;

		/* Search for oldest */
		for (i = 0; i < MAX_SAVED_FLOORS; i++)
		{
			sf_ptr = &saved_floors[i];

			/* Don't kill current floor */
			if (sf_ptr->floor_id == p_ptr->floor_id) continue;

			/* Don't kill newer */
			if (sf_ptr->visit_mark > oldest_visit) continue;

			oldest = i;
			oldest_visit = sf_ptr->visit_mark;
		}

		/* Kill oldest saved floor */
		sf_ptr = &saved_floors[oldest];
		kill_saved_floor(sf_ptr);

		/* Use it */
		i = oldest;
	}

	/* Prepare new floor data */
	sf_ptr->savefile_id = i;
	sf_ptr->floor_id = max_floor_id;
	sf_ptr->last_visit = 0;
	sf_ptr->upper_floor_id = 0;
	sf_ptr->lower_floor_id = 0;
	sf_ptr->visit_mark = latest_visit_mark++;

	/* sf_ptr->dun_level is not yet decided */


	/* Increment number of floor_id */
	if (max_floor_id < MAX_SHORT) max_floor_id++;

	/* 32767 floor_ids are all used up!  Re-use ancient IDs */
	else max_floor_id = 1;

	return sf_ptr->floor_id;
}


/*
 * Prepare mode flags of changing floor
 */
void prepare_change_floor_mode(u32b mode)
{
	change_floor_mode |= mode;
}


/*
 * Builds the dead end
 */
static void build_dead_end(void)
{
	int x,y;

	/* Clear and empty the cave */
	clear_cave();

	/* Smallest area */
	cur_hgt = SCREEN_HGT;
	cur_wid = SCREEN_WID;

	/* Filled with permanent walls */
	for (y = 0; y < MAX_HGT; y++)
	{
		for (x = 0; x < MAX_WID; x++)
		{
			/* Create "solid" perma-wall */
			cave[y][x].feat = FEAT_PERM_SOLID;
		}
	}

	/* Place at center of the floor */
	py = cur_hgt / 2;
	px = cur_wid / 2;

	/* Give one square */
	place_floor_bold(py, px);
}


/*
 * Preserve_pets
 */
static void preserve_pet(void)
{
	int num, i;

	for(num = 0; num < 21; num++)
	{
		party_mon[num].r_idx = 0;
	}

	if (p_ptr->riding)
	{
		COPY(&party_mon[0], &m_list[p_ptr->riding], monster_type);

		/* Delete from this floor */
		delete_monster_idx(p_ptr->riding);
	}

	for(i = m_max - 1, num = 1; (i >= 1 && num < 21); i--)
	{
		monster_type *m_ptr = &m_list[i];
			
		if (!m_ptr->r_idx) continue;
		if (!is_pet(m_ptr)) continue;
		if (i == p_ptr->riding) continue;

		if (reinit_wilderness)
		{
			/* Don't lose sight of pets when getting a Quest */
		}
		else
		{
			int dis = distance(py, px, m_ptr->fy, m_ptr->fx);

			/*
			 * Pets with nickname will follow even from 3 blocks away
			 * when you or the pet can see the other.
			 */
			if (m_ptr->nickname && 
			    (player_has_los_bold(m_ptr->fy, m_ptr->fx) ||
			     los(m_ptr->fy, m_ptr->fx, py, px)))
			{
				if (dis > 3) continue;
			}
			else
			{
				if (dis > 1) continue;
			}
			if (m_ptr->confused || m_ptr->stunned || m_ptr->csleep || m_ptr->stoning) continue;
		}

		COPY(&party_mon[num], &m_list[i], monster_type);
		num++;

		/* Delete from this floor */
		delete_monster_idx(i);
	}

	if (record_named_pet)
	{
		for (i = m_max - 1; i >=1; i--)
		{
			monster_type *m_ptr = &m_list[i];
			char m_name[80];

			if (!m_ptr->r_idx) continue;
			if (!is_pet(m_ptr)) continue;
			if (!m_ptr->nickname) continue;
			if (p_ptr->riding == i) continue;

			monster_desc(m_name, m_ptr, 0x88);
			do_cmd_write_nikki(NIKKI_NAMED_PET, 4, m_name);
		}
	}
}


/*
 * Place preserved pet monsters on new floor
 */
static void place_pet(void)
{
	int i, max_num;

	if (p_ptr->wild_mode)
		max_num = 1;
	else
		max_num = 21;

	for (i = 0; i < max_num; i++)
	{
		int cy, cx, m_idx;

		if (!(party_mon[i].r_idx)) continue;


		if (i == 0)
		{
			m_idx = m_pop();
			p_ptr->riding = m_idx;
			if (m_idx)
			{
				cy = py;
				cx = px;
			}
		}
		else
		{
			int j, d;

			for(d = 1; d < 6; d++)
			{
				for(j = 1000; j > 0; j--)
				{
					scatter(&cy, &cx, py, px, d, 0);
					if ((cave_floor_bold(cy, cx) || (cave[cy][cx].feat == FEAT_TREES)) && !cave[cy][cx].m_idx && !((cy == py) && (cx == px))) break;
				}
				if (j) break;
			}
			if (d == 6 || p_ptr->inside_arena)
				m_idx = 0;
			else
				m_idx = m_pop();
		}

		if (m_idx)
		{
			monster_type *m_ptr = &m_list[m_idx];
			monster_race *r_ptr;

			cave[cy][cx].m_idx = m_idx;

			m_ptr->r_idx = party_mon[i].r_idx;

			/* Copy all member of the structure */
			*m_ptr = party_mon[i];
			r_ptr = real_r_ptr(m_ptr);

			m_ptr->fy = cy;
			m_ptr->fx = cx;
			m_ptr->ml = TRUE;
			m_ptr->csleep = 0;
			set_pet(m_ptr);

			if ((r_ptr->flags1 & RF1_FORCE_SLEEP) && !ironman_nightmare)
			{
				/* Monster is still being nice */
				m_ptr->mflag |= (MFLAG_NICE);

				/* Must repair monsters */
				repair_monsters = TRUE;
			}

			/* Update the monster */
			update_mon(m_idx, TRUE);
			lite_spot(cy, cx);

			r_ptr->cur_num++;

			/* Hack -- Count the number of "reproducers" */
			if (r_ptr->flags2 & RF2_MULTIPLY) num_repro++;

			/* Hack -- Count the number of "anti-magic monsters" */
			if (r_ptr->flags3 & RF3_ANTI_MAGIC)
				anti_magic_m_idx[num_anti_magic++] = m_idx;

			/* Hack -- Count the number of "fear field monsters" */
			if (r_ptr->flags3 & RF3_FEAR_FIELD)
				fear_field_m_idx[num_fear_field++] = m_idx;

			/* Hack -- Notice new multi-hued monsters */
			if (r_ptr->flags1 & RF1_ATTR_MULTI) shimmer_monsters = TRUE;
		}
		else
		{
			char m_name[80];

			monster_desc(m_name, &party_mon[i], 0);
#ifdef JP
			msg_format("%sとはぐれてしまった。", m_name);
#else
			msg_format("You have lost sight of %s.", m_name);
#endif
			if (record_named_pet && party_mon[i].nickname)
			{
				monster_desc(m_name, &party_mon[i], 0x08);
				do_cmd_write_nikki(NIKKI_NAMED_PET, 5, m_name);
			}
		}
	}
}


/*
 * Hack -- Update location of unique monsters and artifacts
 *
 * The r_ptr->floor_id and a_ptr->floor_id are not updated correctly
 * while new floor creation since dungeons may be re-created by
 * auto-scum option.
 */
static void update_unique_artifact(s16b cur_floor_id)
{
	int i;

	/* Maintain unique monsters */
	for (i = 1; i < m_max; i++)
	{
		monster_race *r_ptr;
		monster_type *m_ptr = &m_list[i];

		/* Skip dead monsters */
		if (!m_ptr->r_idx) continue;

		/* Extract real monster race */
		r_ptr = real_r_ptr(m_ptr);

		/* Memorize location of the unique monster */
		if ((r_ptr->flags1 & RF1_UNIQUE) ||
		    (r_ptr->flags7 & RF7_NAZGUL))
		{
			r_ptr->floor_id = cur_floor_id;
		}
	}

	/* Maintain artifacts */
	for (i = 1; i < o_max; i++)
	{
		object_type *o_ptr = &o_list[i];

		/* Skip dead objects */
		if (!o_ptr->k_idx) continue;

		/* Memorize location of the artifact */
		if (artifact_p(o_ptr))
		{
			a_info[o_ptr->name1].floor_id = cur_floor_id;
		}
	}
}


/*
 * Maintain quest monsters, mark next floor_id at stairs, save current
 * floor, and prepare to enter next floor.
 */
void leave_floor(void)
{
	cave_type *c_ptr = NULL;
	saved_floor_type *sf_ptr;
	int quest_r_idx = 0;
	int i;
	bool upward_dun = (d_info[dungeon_type].flags1 & DF1_UPWARD) ? TRUE : FALSE;
	int sy = 0, sx = 0;
	int by = py, bx = px;

	/* Preserve pets and prepare to take these to next floor */
	preserve_pet();

	/* New floor is not yet prepared */
	new_floor_id = 0;


	/* From somewhere of the surface to somewhere of the surface */
	if (!dungeon_type)
	{
		/* No need to save current floor */
		return;
	}

	/* Search the quest monster index */
	for (i = 0; i < max_quests; i++)
	{
		if ((quest[i].status == QUEST_STATUS_TAKEN) &&
		    ((quest[i].type == QUEST_TYPE_KILL_LEVEL) ||
		    (quest[i].type == QUEST_TYPE_RANDOM)) &&
		    (quest[i].level == dun_level) &&
		    (dungeon_type == quest[i].dungeon) &&
		    !(quest[i].flags & QUEST_FLAG_PRESET))
		{
			quest_r_idx = quest[i].r_idx;
		}
	}

	/* Maintain quest monsters */
	for (i = 1; i < m_max; i++)
	{
		monster_race *r_ptr;
		monster_type *m_ptr = &m_list[i];

		/* Skip dead monsters */
		if (!m_ptr->r_idx) continue;

		/* Only maintain quest monsters */
		if (quest_r_idx != m_ptr->r_idx) continue;

		/* Extract real monster race */
		r_ptr = real_r_ptr(m_ptr);

		/* Ignore unique monsters */
		if ((r_ptr->flags1 & RF1_UNIQUE) ||
		    (r_ptr->flags7 & RF7_NAZGUL)) continue;

		/* Delete non-unique quest monsters */
		delete_monster_idx(i);
	}

	/* Check if there is a same item */
	for (i = 0; i < INVEN_PACK; i++)
	{
		object_type *o_ptr = &inventory[i];

		/* Skip dead objects */
		if (!o_ptr->k_idx) continue;

		/* Delete old memorized location of the artifact */
		if (artifact_p(o_ptr))
		{
			a_info[o_ptr->name1].floor_id = 0;
		}
	}

	/* Extract current floor info or NULL */
	sf_ptr = get_sf_ptr(p_ptr->floor_id);

	/* Choose random stairs */
	if ((change_floor_mode & CFM_RAND_CONNECT) && p_ptr->floor_id)
	{
		int x, y;
		int x_table[20];
		int y_table[20];
		int num = 0;
		int i;

		/* Search usable stairs */
		for (y = 0; y < cur_hgt; y++)
		{
			for (x = 0; x < cur_wid; x++)
			{
				cave_type *c_ptr = &cave[y][x];
				bool ok = FALSE;

				if (change_floor_mode & CFM_UP)
				{
					/* Found fixed stairs */
					if (c_ptr->special &&
					    c_ptr->special == sf_ptr->upper_floor_id)
					{
						sx = x;
						sy = y;
					}

					if (((c_ptr->feat == FEAT_LESS) && !(change_floor_mode & CFM_FORCE2L)) ||
					    ((c_ptr->feat == FEAT_LESS_LESS) && !(change_floor_mode & CFM_FORCE1L)))
						ok = TRUE;
				}
				else if (change_floor_mode & CFM_DOWN)
				{
					/* Found fixed stairs */
					if (c_ptr->special &&
					    c_ptr->special == sf_ptr->lower_floor_id)
					{
						sx = x;
						sy = y;
					}

					if (((c_ptr->feat == FEAT_MORE) && !(change_floor_mode & CFM_FORCE2L)) ||
					    ((c_ptr->feat == FEAT_MORE_MORE) && !(change_floor_mode & CFM_FORCE1L)))
						ok = TRUE;
				}

				if (ok && num < 20)
				{
					x_table[num] = x;
					y_table[num] = y;
					num++;
				}
			}
		}

		if (sx)
		{
			/* Already fixed */
			py = sy;
			px = sx;
		}
		else if (!num)
		{
			/* No stairs found! -- No return */
			prepare_change_floor_mode(CFM_RAND_PLACE | CFM_NO_RETURN);

			/* Mega Hack -- It's not the stairs you enter.  Disable it.  */
			cave[py][px].special = 0;
		}
		else
		{
			/* Choose random one */
			i = randint0(num);

			/* Point stair location */
			py = y_table[i];
			px = x_table[i];
		}
	}

	/* Extract new dungeon level */
	if (!(change_floor_mode & CFM_CLEAR_ALL))
	{
		int move_num = 0;

		/* Extract stair position */
		c_ptr = &cave[py][px];

		/* Get back to old saved floor? */
		if (dun_level && c_ptr->special && get_sf_ptr(c_ptr->special))
		{
			/* Saved floor is exist.  Use it. */
			new_floor_id = c_ptr->special;
		}

		/* Extract level movement number */
		if (change_floor_mode & CFM_DOWN) move_num = upward_dun ? -1 : 1;
		else if (change_floor_mode & CFM_UP) move_num = upward_dun ? 1 : -1;

		/* Mark shaft up/down */
		if (c_ptr->feat == FEAT_LESS_LESS ||
		    c_ptr->feat == FEAT_MORE_MORE)
		{
			prepare_change_floor_mode(CFM_SHAFT);
			move_num += SGN(move_num);
		}

		/* Get out from or Enter the dungeon */
		if ((move_num > 0) && !dun_level) move_num = d_info[c_ptr->special].mindepth;
		else if (dun_level + move_num < d_info[dungeon_type].mindepth) move_num = -dun_level;

		dun_level += move_num;
	}

	/* Leaving the dungeon to town */
	if (!dun_level && dungeon_type)
	{
		p_ptr->leaving_dungeon = TRUE;
		if (!ironman_forward && !astral_mode)
		{
			p_ptr->wilderness_y = d_info[dungeon_type].dy;
			p_ptr->wilderness_x = d_info[dungeon_type].dx;
		}
		p_ptr->recall_dungeon = dungeon_type;
		dungeon_type = 0;

		/* Reach to the surface -- Clear all saved floors */
		prepare_change_floor_mode(CFM_CLEAR_ALL);
	}

	if (change_floor_mode & CFM_CLEAR_ALL)
	{
		int i;

		/* Kill all saved floors */
		for (i = 0; i < MAX_SAVED_FLOORS; i++)
			kill_saved_floor(&saved_floors[i]);

		/* Reset visit_mark count */
		latest_visit_mark = 1;
	}
	else if (change_floor_mode & CFM_NO_RETURN)
	{
		/* Kill current floor */
		kill_saved_floor(sf_ptr);
	}

	/* No current floor -- Left/Enter dungeon etc... */
	if (!p_ptr->floor_id)
	{
		/* No longer need to save current floor */
		return;
	}


	/* Mark next floor_id on the previous floor */
	if (!new_floor_id)
	{
		/* Get new id */
		new_floor_id = get_new_floor_id();

		/* Connect from here */
		if (c_ptr)
		{
			c_ptr->special = new_floor_id;
		}

		/* Record new dungeon level */
		get_sf_ptr(new_floor_id)->dun_level = dun_level;
	}

	/* Fix connection -- level teleportation or trap door */
	if (change_floor_mode & CFM_RAND_CONNECT)
	{
		if (change_floor_mode & CFM_UP)
			sf_ptr->upper_floor_id = new_floor_id;
		else if (change_floor_mode & CFM_DOWN)
			sf_ptr->lower_floor_id = new_floor_id;
	}

	/* If you can return, you need to save previous floor */
	if (!(change_floor_mode & (CFM_NO_RETURN | CFM_CLEAR_ALL)))
	{
		/* Record the last visit turn of current floor */
		sf_ptr->last_visit = turn;

		/* Before visual update */
		sy = py;
		sx = px;
		py = by;
		px = bx;

		/* Forget the lite */
		forget_lite();

		/* Forget the view */
		forget_view();

		/* Forget the view */
		clear_mon_lite();

		/* After visual update */
		py = sy;
		px = sx;

		/* Hack - Force kill current floor */
		if (ironman_forward || (d_info[dungeon_type].flags1 & DF1_NO_BACK))
		{
			kill_saved_floor(get_sf_ptr(p_ptr->floor_id));
		}

		/* Save current floor */
		else if (!save_floor(sf_ptr, 0))
		{
			/* Save failed -- No return */
			prepare_change_floor_mode(CFM_NO_RETURN);

			/* Kill current floor */
			kill_saved_floor(get_sf_ptr(p_ptr->floor_id));
		}
	}
}


/*
 * Enter new floor.  If the floor is an old saved floor, it will be
 * restored from the temporal file.  If the floor is new one, new cave
 * will be generated.
 */
void change_floor(void)
{
	saved_floor_type *sf_ptr;
	bool loaded = FALSE;
	bool upward_dun = (d_info[dungeon_type].flags1 & DF1_UPWARD) ? TRUE : FALSE;

	/* The dungeon is not ready */
	character_dungeon = FALSE;

	/* No longer in the trap detecteded region */
	p_ptr->dtrap = FALSE;

	/* Mega-Hack -- no panel yet */
	panel_row_min = 0;
	panel_row_max = 0;
	panel_col_min = 0;
	panel_col_max = 0;

	/* Mega-Hack -- not ambushed on the wildness? */
	ambush_flag = FALSE;

	/* On the surface */
	if (!dungeon_type)
	{
		/* Create cave */
		generate_cave();

		/* Paranoia -- Now on the surface */
		new_floor_id = 0;
	}

	/* In the dungeon */
	else
	{
		/* No floor_id yet */
		if (!new_floor_id)
		{
			/* Get new id */
			new_floor_id = get_new_floor_id();
		}

		/* Pointer for infomations of new floor */
		sf_ptr = get_sf_ptr(new_floor_id);

		/* Try to restore old floor */
		if (sf_ptr->last_visit)
		{
			/* Old saved floor is exist */
			if (load_floor(sf_ptr, 0))
			{
				loaded = TRUE;

				/* Forbid return stairs */
				if (change_floor_mode & CFM_NO_RETURN)
				{
					cave_type *c_ptr = &cave[py][px];

					/* Reset to floor */
					place_floor_grid(c_ptr);
					c_ptr->special = 0;
				}
			}
		}

		/*
		 * Set lower/upper_floor_id of new floor when the new
		 * floor is right-above/right-under the current floor.
		 *
		 * Stair creation/Teleport level/Trap door will take
		 * you the same floor when you used it later again.
		 */
		if (p_ptr->floor_id)
		{
			saved_floor_type *cur_sf_ptr = get_sf_ptr(p_ptr->floor_id);

			if (change_floor_mode & CFM_UP)
			{
				/* New floor is right-above */
				if (cur_sf_ptr->upper_floor_id == new_floor_id)
					sf_ptr->lower_floor_id = p_ptr->floor_id;
			}
			else if (change_floor_mode & CFM_DOWN)
			{
				/* New floor is right-under */
				if (cur_sf_ptr->lower_floor_id == new_floor_id)
					sf_ptr->upper_floor_id = p_ptr->floor_id;
			}
		}

		/* Maintain monsters and artifacts */
		if (loaded)
		{
			int i;
			s32b absence_ticks = (turn - sf_ptr->last_visit) / TURNS_PER_TICK;
			int alloc_chance = d_info[dungeon_type].max_m_alloc_chance;
			int alloc_times;

			/* Maintain monsters */
			for (i = 1; i < m_max; i++)
			{
				monster_race *r_ptr;
				monster_type *m_ptr = &m_list[i];

				/* Skip dead monsters */
				if (!m_ptr->r_idx) continue;

				if (!is_pet(m_ptr))
				{
					/* Restore HP */
					m_ptr->hp = m_ptr->maxhp = m_ptr->max_maxhp;

					/* Remove fear */
					m_ptr->monfear = 0;

					/* Remove invulnerability */
					m_ptr->invulner = 0;

					/* Remove fast status */
					m_ptr->fast = 0;

					/* Remove slow status */
					m_ptr->slow = 0;

					/* Remove stun */
					m_ptr->stunned = 0;

					/* Remove confusion */
					m_ptr->confused = 0;

					/* Remove stoning */
					m_ptr->stoning = 0;

					/* Remove Melt Weapon */
					m_ptr->melt_weapon = 0;

					/* Remove Opposite Element */
					m_ptr->opposite_elem = 0;

					/* Remove Silence */
					m_ptr->silent = 0;
					m_ptr->silent_song = FALSE;
				}

				/* Extract real monster race */
				r_ptr = real_r_ptr(m_ptr);

				/* Ignore non-unique */
				if (!(r_ptr->flags1 & RF1_UNIQUE) &&
				    !(r_ptr->flags7 & RF7_NAZGUL)) continue;

				/* Appear at a different floor? */
				if (r_ptr->floor_id != new_floor_id)
				{
					/* Disapper from here */
					delete_monster_idx(i);
				}
			}

			/* Maintain artifacts */
			for (i = 1; i < o_max; i++)
			{
				object_type *o_ptr = &o_list[i];

				/* Skip dead objects */
				if (!o_ptr->k_idx) continue;

				/* Ignore non-artifact */
				if (!artifact_p(o_ptr)) continue;

				/* Appear at a different floor? */
				if (a_info[o_ptr->name1].floor_id != new_floor_id)
				{
					/* Disappear from here */
					delete_object_idx(i);
				}
				else
				{
					/* Cancel preserve */
					a_info[o_ptr->name1].cur_num = 1;
				}
			}

			place_quest_monsters();

			/* Place some random monsters */
			alloc_times = absence_ticks / alloc_chance;

			if (randint0(alloc_chance) < (absence_ticks % alloc_chance))
				alloc_times++;

			for (i = 0; i < alloc_times; i++)
			{
				/* Make a (group of) new monster */
				(void)alloc_monster(0, 0);
			}
		}

		/* New floor_id or failed to restore */
		else /* if (!loaded) */
		{
			if (sf_ptr->last_visit)
			{
				/* Temporal file is broken? */
#ifdef JP
				msg_print("階段は行き止まりだった。");
#else
				msg_print("The staircases come to a dead end...");
#endif

				/* Create simple dead end */
				build_dead_end();

				/* Break connection */
				if (change_floor_mode & CFM_UP)
				{
					sf_ptr->upper_floor_id = 0;
				}
				else if (change_floor_mode & CFM_DOWN)
				{
					sf_ptr->lower_floor_id = 0;
				}
			}
			else
			{
				/* Newly create cave */
				generate_cave();
			}

			/* Record last visit turn */
			sf_ptr->last_visit = turn;

			/* Set correct dun_level value */
			sf_ptr->dun_level = dun_level;

			/* Create connected stairs */
			if (!(change_floor_mode & (CFM_NO_RETURN | CFM_CLEAR_ALL)) && dun_level)
			{
				bool ok = FALSE;

				/* Extract stair position */
				cave_type *c_ptr = &cave[py][px];

				byte prev_feat = c_ptr->mimic ? c_ptr->mimic : f_info[c_ptr->feat].mimic;

				/*** Create connected stairs ***/

				/* No stairs forward from Quest */
				/* No stairs back when ironman_forward */
				if (change_floor_mode & CFM_UP)
				{
					if (upward_dun ^ astral_mode)
					{
						if (!ironman_forward && !(d_info[dungeon_type].flags1 & DF1_NO_BACK))
						{
							c_ptr->feat = (change_floor_mode & CFM_SHAFT) ? FEAT_MORE_MORE : FEAT_MORE;
							apply_grid_effect(py, px, prev_feat, FALSE);
							ok = TRUE;
						}
					}
					else
					{
	 					if (!quest_number(dun_level))
						{
							c_ptr->feat = (change_floor_mode & CFM_SHAFT) ? FEAT_MORE_MORE : FEAT_MORE;
							apply_grid_effect(py, px, prev_feat, FALSE);
							ok = TRUE;
						}
					}
				}

				else if (change_floor_mode & CFM_DOWN)
				{
					if (upward_dun ^ astral_mode)
					{
	 					if (!quest_number(dun_level))
						{
							c_ptr->feat = (change_floor_mode & CFM_SHAFT) ? FEAT_LESS_LESS : FEAT_LESS;
							apply_grid_effect(py, px, prev_feat, FALSE);
							ok = TRUE;
						}
					}
					else
					{
						if (!ironman_forward && !(d_info[dungeon_type].flags1 & DF1_NO_BACK))
						{
							c_ptr->feat = (change_floor_mode & CFM_SHAFT) ? FEAT_LESS_LESS : FEAT_LESS;
							apply_grid_effect(py, px, prev_feat, FALSE);
							ok = TRUE;
						}
					}
				}

				if (ok)
				{
					/* Paranoia -- Clear mimic */
					c_ptr->mimic = 0;

					/* Connect to previous floor */
					c_ptr->special = p_ptr->floor_id;

					/* Mega-Hack - If back from the Heaven Gate, remove other upstairs. */
					if ((change_floor_mode & CFM_DOWN) && (dungeon_type == DUNGEON_HEAVEN_WAY) &&
					    (dun_level == (d_info[dungeon_type].maxdepth - 1)))
					{
						int x, y;

						for (y = 0; y < cur_hgt; y++)
						{
							for (x = 0; x < cur_wid; x++)
							{
								cave_type *cc_ptr = &cave[y][x];

								if (cc_ptr->feat != FEAT_LESS) continue;

								/* Don't remove new upstair */
								if (cc_ptr == c_ptr) continue;

								/* Remove other upstairs */
								cc_ptr->special = 0;
								cc_ptr->feat = floor_type[randint0(100)];
								apply_grid_effect(y, x, prev_feat, FALSE);
							}
						}
					}
				}
			}
		}

		/* Arrive at random grid */
		if (change_floor_mode & (CFM_RAND_PLACE))
		{
			(void)new_player_spot();
		}

		/* You see stairs blocked */
		else if (change_floor_mode & CFM_NO_RETURN)
		{
			if (!p_ptr->blind)
			{
#ifdef JP
				msg_print("突然階段が塞がれてしまった。");
#else
				msg_print("Suddenly the stairs is blocked!");
#endif
			}
			else
			{
#ifdef JP
				msg_print("ゴトゴトと何か音がした。");
#else
				msg_print("You hear some noises.");
#endif
			}
		}

		/*
		 * Update visit mark
		 *
		 * The "turn" is not always different number because
		 * the level teleport doesn't take any turn.  Use
		 * visit mark instead of last visit turn to find the
		 * oldest saved floor.
		 */
		sf_ptr->visit_mark = latest_visit_mark++;
	}

	/* Place preserved pet monsters */
	place_pet();

	/* Hack -- maintain unique and artifacts */
	update_unique_artifact(new_floor_id);

	/* Now the player is in new floor */
	p_ptr->floor_id = new_floor_id;

	/* The dungeon is ready */
	character_dungeon = TRUE;

	/* Town guards attack you if chaos frame is too low! */
	if (!dun_level && p_ptr->town_num)
	{
		if (town[p_ptr->town_num].ethnic != NO_ETHNICITY)
		{
			if (chaos_frame[(int)(town[p_ptr->town_num].ethnic)] <= CFRAME_SUMMON_GUARD)
				summon_named_creature(0, py, px, MON_GUARD, PM_NO_PET | PM_ALLOW_GROUP | PM_IGNORE_AMGRID);
		}
	}

	/* Hack -- Munchkin characters always get whole map */
	if (easy_band) wiz_lite((bool)(p_ptr->pclass == CLASS_NINJA));

	if (p_ptr->inside_quest) sound(SOUND_ENCOUNT);

	/* Remember when this level was "created" */
	old_turn = turn;

	/* Clear all flags */
	change_floor_mode = 0L;
}



/*
 * Create stairs at or move previously created stairs into the player
 * location.
 */
void stair_creation(void)
{
	saved_floor_type *sf_ptr;
	saved_floor_type *dest_sf_ptr;

	int maxdepth = d_info[dungeon_type].maxdepth - ((dungeon_type == DUNGEON_HEAVEN_WAY) ? 1 : 0);
	bool up = TRUE;
	bool down = TRUE;
	s16b dest_floor_id = 0;


	/* Forbid back staircases on Ironman mode */
	if (ironman_forward || (d_info[dungeon_type].flags1 & DF1_NO_BACK) ||
	    ((d_info[dungeon_type].flags1 & DF1_CLOSED) && (dun_level <= d_info[dungeon_type].mindepth)))
	{
		if (d_info[dungeon_type].flags1 & DF1_UPWARD) down = FALSE;
		else up = FALSE;
	}

	/* Forbid forward staircases on quest level */
	if (quest_number(dun_level) || (dun_level >= maxdepth))
	{
		if (d_info[dungeon_type].flags1 & DF1_UPWARD) up = FALSE;
		else down = FALSE;
	}

	/* No effect out of standard dungeon floor */
	if (!dun_level || (!up && !down) || (dungeon_type == DUNGEON_HEAVEN) ||
	    (p_ptr->inside_quest && quest_is_fixed(p_ptr->inside_quest)) ||
	    p_ptr->inside_arena || astral_mode)
	{
		/* arena or quest */
#ifdef JP
		msg_print("効果がありません！");
#else
		msg_print("There is no effect!");
#endif
		return;
	}

	/* Artifacts resists */
	if (!cave_valid_bold(py, px))
	{
#ifdef JP
		msg_print("床上のアイテムが呪文を跳ね返した。");
#else
		msg_print("The object resists the spell.");
#endif

		return;
	}

	/* Destroy all objects in the grid */
	delete_object(py, px);

	/* Extract current floor data */
	sf_ptr = get_sf_ptr(p_ptr->floor_id);

	/* Choose randomly */
	if (up && down)
	{
		if (randint0(100) < 50) up = FALSE;
		else down = FALSE;
	}

	/* Destination is already fixed */
	if (up)
	{
		if (sf_ptr->upper_floor_id) dest_floor_id = sf_ptr->upper_floor_id;
	}
	else
	{
		if (sf_ptr->lower_floor_id) dest_floor_id = sf_ptr->lower_floor_id;
	}


	/* Search old stairs leading to the destination */
	if (dest_floor_id)
	{
		int x, y;

		for (y = 0; y < cur_hgt; y++)
		{
			for (x = 0; x < cur_wid; x++)
			{
				cave_type *c_ptr = &cave[y][x];

				if (!c_ptr->special) continue;
				if (c_ptr->special != dest_floor_id) continue;

				/* Remove old stairs */
				c_ptr->special = 0;
				cave_set_feat(y, x, floor_type[randint0(100)]);
			}
		}
	}

	/* No old destination -- Get new one now */
	else
	{
		dest_floor_id = get_new_floor_id();

		/* Fix it */
		if (up)
			sf_ptr->upper_floor_id = dest_floor_id;
		else
			sf_ptr->lower_floor_id = dest_floor_id;
	}

	/* Extract destination floor data */
	dest_sf_ptr = get_sf_ptr(dest_floor_id);


	/* Create a staircase */
	if (up)
	{
		if (dest_sf_ptr->last_visit && dest_sf_ptr->dun_level <= dun_level - 2)
			cave_set_feat(py, px, FEAT_LESS_LESS);
		else
			cave_set_feat(py, px, FEAT_LESS);
	}
	else
	{
		if (dest_sf_ptr->last_visit && dest_sf_ptr->dun_level >= dun_level + 2)
			cave_set_feat(py, px, FEAT_MORE_MORE);
		else
			cave_set_feat(py, px, FEAT_MORE);
	}


	/* Connect this stairs to the destination */
	cave[py][px].special = dest_floor_id;
}


