class 1 teleport_dungeon_manager_npc : citizen
{
parameter:
	string	fnHi = "tel_dungeon_npc_hi";
	string	fnNoMoreChance = "tel_dungeon_npc_nomorechance.htm";
	string	fnYouAreNotLeader = "tel_dungeon_npc_notleader.htm";
	string	LevelName = "TeleportDungeon1";
	int	RoomIndex = 0;
	int	LevelNumber = 1;
	int	escape_x = 1;
	int	escape_y = 1;
	int	escape_z = 1;
	int	PosX0 = 1;
	int	PosY0 = 1;
	int	PosX1 = 1;
	int	PosY1 = 1;
	int	PosX2 = 1;
	int	PosY2 = 1;
	int	PosX3 = 1;
	int	PosY3 = 1;
	int	PosX4 = 1;
	int	PosY4 = 1;
	int	PosX5 = 1;
	int	PosY5 = 1;
	int	PosX6 = 1;
	int	PosY6 = 1;
	int	PosX7 = 1;
	int	PosY7 = 1;
	int	PosX8 = 1;
	int	PosY8 = 1;

handler:
	EventHandler CREATED(rlist0)
	{
		myself::InstantTeleportInMyTerritory(escape_x,escape_y,escape_z,100);
		myself::AddTimerEx(2001,1000);
		myself.i_ai1 = 3;
		myself.i_ai2 = 0;
		super;
	}

	EventHandler ATTACKED(attacker,party0,maker0,rlist0,room0)
	{
		if( myself::OwnItemCount(attacker,57) == 1980 )
		{
			party0 = myself::GetParty(attacker);
			if( myself::IsNullParty(party0) )
			{
				gg::Announce("Clearance of dimension: Compose the green onion mote first");
				return;
			}
			else if( myself.i_ai2 == 1 )
			{
				gg::Announce("Clearance of dimension: It is a condition where the test is started.");
			}
			else
			{
				maker0 = myself::GetMyMaker();
				rlist0 = gg::GetRoomInfoList(LevelName);
				room0 = rlist0::GetRoomInfo(RoomIndex);
				if( room0.status.value == 0 )
				{
					room0.status::Exchange(1);
					room0::SetParty(party0.id);
					switch(RoomIndex)
					{
						case 0:
						{
							myself::TeleportParty(party0.id,PosX0,PosY0,myself::FloatToInt(myself.sm.z),2000,0);
							break;
						}
						case 1:
						{
							myself::TeleportParty(party0.id,PosX1,PosY1,myself::FloatToInt(myself.sm.z),2000,0);
							break;
						}
						case 2:
						{
							myself::TeleportParty(party0.id,PosX2,PosY2,myself::FloatToInt(myself.sm.z),2000,0);
							break;
						}
						case 3:
						{
							myself::TeleportParty(party0.id,PosX3,PosY3,myself::FloatToInt(myself.sm.z),2000,0);
							break;
						}
						case 4:
						{
							myself::TeleportParty(party0.id,PosX4,PosY4,myself::FloatToInt(myself.sm.z),2000,0);
							break;
						}
						case 5:
						{
							myself::TeleportParty(party0.id,PosX5,PosY5,myself::FloatToInt(myself.sm.z),2000,0);
							break;
						}
						case 6:
						{
							myself::TeleportParty(party0.id,PosX6,PosY6,myself::FloatToInt(myself.sm.z),2000,0);
							break;
						}
						case 7:
						{
							myself::TeleportParty(party0.id,PosX7,PosY7,myself::FloatToInt(myself.sm.z),2000,0);
							break;
						}
					}
					gg::Announce("Clearance of dimension: It starts a test.");
					myself.i_ai2 = 1;
				}
				else
				{
					gg::Announce("Clearance of dimension: Currently it is a room which is activated.");
				}
			}
		}
	}

	EventHandler TALKED(talker,s0)
	{
		s0 = fnHi + RoomIndex + ".htm";
		myself::ShowPage(talker,s0);
	}

	EventHandler MENU_SELECTED(talker,ask,reply,room0,room1,i0,i1,i2,party0,rlist0,maker0,c0)
	{
		if( ask == -1000 )
		{
			maker0 = myself::GetMyMaker();
			rlist0 = gg::GetRoomInfoList(LevelName);
			room0 = rlist0::GetRoomInfo(RoomIndex);
			if( reply == 1 )
			{
				party0 = myself::GetPartyFromID(room0.party_id);
				c0 = myself::GetLeaderOfParty(party0);
				if( talker == c0 )
				{
					myself::InstantTeleportInMyTerritory(escape_x,escape_y,escape_z,100);
					gg::SendMakerScriptEvent(maker0,0,0,0);
					for(i0 = 0; i0 < room0.member_count; i0 = ( i0 + 1 ))
					{
						c0 = myself::GetCreatureFromID(room0::GetMemberID(i0));
						if( myself::HaveMemo(c0,635) )
						{
							myself::SetMemoStateEx(c0,635,1,-1);
						}
					}
					room0.status::Exchange(0);
					room0::Clear();
					myself::AddLog(8,c0,1);
				}
				else
				{
					myself::ShowPage(talker,fnYouAreNotLeader);
				}
			}
			else if( reply == 2 )
			{
				if( myself::Party_GetLeader(talker) == talker )
				{
					i0 = myself::GetMemoStateEx(talker,635,1);
					if( i0 == 1 )
					{
						myself::SetMemoStateEx(talker,635,1,0);
						i0 = -1;
						for(i1 = 0; i1 < 8; i1 = ( i1 + 1 ))
						{
							if( i0 == -1 )
							{
								i2 = gg::Rand(8);
								room1 = rlist0::GetRoomInfo(i2);
								if( room1.status.value == 0 )
								{
									room1.status::Exchange(1);
									i0 = i2;
								}
							}
						}
						if( i0 == -1 )
						{
							for(i1 = 0; i1 < 8; i1 = ( i1 + 1 ))
							{
								if( i0 == -1 )
								{
									room1 = rlist0::GetRoomInfo(i1);
									if( room1.status.value == 0 )
									{
										room1.status::Exchange(1);
										i0 = i1;
									}
								}
							}
						}
						if( i0 == -1 )
						{
						}
						else
						{
							gg::SendMakerScriptEvent(maker0,0,0,0);
							room1::SetParty(room0.party_id);
							switch(i0)
							{
								case 0:
								{
									myself::TeleportParty(room0.party_id,PosX0,PosY0,myself::FloatToInt(myself.sm.z),2000,0);
									break;
								}
								case 1:
								{
									myself::TeleportParty(room0.party_id,PosX1,PosY1,myself::FloatToInt(myself.sm.z),2000,0);
									break;
								}
								case 2:
								{
									myself::TeleportParty(room0.party_id,PosX2,PosY2,myself::FloatToInt(myself.sm.z),2000,0);
									break;
								}
								case 3:
								{
									myself::TeleportParty(room0.party_id,PosX3,PosY3,myself::FloatToInt(myself.sm.z),2000,0);
									break;
								}
								case 4:
								{
									myself::TeleportParty(room0.party_id,PosX4,PosY4,myself::FloatToInt(myself.sm.z),2000,0);
									break;
								}
								case 5:
								{
									myself::TeleportParty(room0.party_id,PosX5,PosY5,myself::FloatToInt(myself.sm.z),2000,0);
									break;
								}
								case 6:
								{
									myself::TeleportParty(room0.party_id,PosX6,PosY6,myself::FloatToInt(myself.sm.z),2000,0);
									break;
								}
								case 7:
								{
									myself::TeleportParty(room0.party_id,PosX7,PosY7,myself::FloatToInt(myself.sm.z),2000,0);
									break;
								}
							}
							room0.status::Exchange(0);
							room0::Clear();
							myself::AddLogEx(324,talker,RoomIndex,i0);
						}
					}
					else
					{
						myself::ShowPage(talker,fnNoMoreChance);
					}
				}
				else
				{
					myself::ShowPage(talker,fnYouAreNotLeader);
				}
			}
		}
		super;
	}

	EventHandler TIMER_FIRED_EX(timer_id,rlist0,room0,room1,party0,i0,i1,i2,maker0,c0,i3)
	{
		rlist0 = gg::GetRoomInfoList(LevelName);
		if( gg::IsNull(rlist0) )
		{
			myself::AddTimerEx(timer_id,5000);
			return;
		}
		room0 = rlist0::GetRoomInfo(RoomIndex);
		maker0 = myself::GetMyMaker();
		if( timer_id == 2001 )
		{
			if( gg::IsNull(rlist0) )
			{
			}
			else if( room0.status.value == 1 )
			{
				if( ( gg::GetTimeOfDay() - room0.time ) < 15 )
				{
					gg::SendMakerScriptEvent(maker0,1,0,0);
				}
				else if( room0::PartyChanged() )
				{
					for(i0 = 0; i0 < room0.member_count; i0 = ( i0 + 1 ))
					{
						c0 = myself::GetCreatureFromID(room0::GetMemberID(i0));
						if( myself::HaveMemo(c0,635) )
						{
							myself::SetMemoStateEx(c0,635,1,-1);
						}
					}
					myself::InstantTeleportInMyTerritory(escape_x,escape_y,escape_z,100);
					gg::SendMakerScriptEvent(maker0,0,0,0);
					room0.status::Exchange(0);
					room0::Clear();
					c0 = myself::GetCreatureFromID(room0::GetMemberID(0));
					myself::AddLog(8,c0,2);
				}
				else if( ( gg::GetTimeOfDay() - room0.time ) > ( ( 60 * 8 ) + ( gg::Rand(5) * 30 ) ) )
				{
					if( myself.i_ai2 == 1 )
					{
						if( RoomIndex == 8 && myself.i_ai1 == 2 )
						{
						}
						else
						{
							gg::Announce("Clearance of dimension: The test was ended.");
							room0.status::Exchange(0);
							gg::SendMakerScriptEvent(maker0,0,0,0);
							myself.i_ai2 = 0;
						}
					}
					else
					{
						room1 = rlist0::GetRoomInfo(8);
						i0 = -1;
						for(i1 = 0; i1 < room0.member_count; i1 = ( i1 + 1 ))
						{
							if( i0 == -1 )
							{
								c0 = myself::GetCreatureFromID(room0::GetMemberID(i1));
								if( gg::IsNull(c0) == 0 )
								{
									i0 = myself::GetMemoStateEx(c0,635,0);
								}
							}
							else if( RoomIndex == 8 && myself.i_ai1 == 2 )
							{
							}
							else if( ( i0 + ( 60 * 40 ) ) < gg::GetTimeOfDay() )
							{
								for(i0 = 0; i0 < room0.member_count; i0 = ( i0 + 1 ))
								{
									c0 = myself::GetCreatureFromID(room0::GetMemberID(i0));
									if( myself::HaveMemo(c0,635) )
									{
										myself::SetMemoStateEx(c0,635,1,-1);
									}
								}
								myself::InstantTeleportInMyTerritory(escape_x,escape_y,escape_z,100);
								gg::SendMakerScriptEvent(maker0,0,0,0);
								room0.status::Exchange(0);
								room0::Clear();
								c0 = myself::GetCreatureFromID(room0::GetMemberID(0));
								myself::AddLog(8,c0,0);
							}
							else
							{
								myself::AddTimerEx(2002,5000);
								myself::EarthQuakeToParty(room0.party_id,20,10,1);
							}
						}
					}
				}
				else
				{
					myself::InstantTeleportInMyTerritory(escape_x,escape_y,escape_z,100);
				}
				myself::AddTimerEx(2001,10000);
			}
			else if( timer_id == 2002 )
			{
				if( room0::PartyChanged() )
				{
					for(i0 = 0; i0 < room0.member_count; i0 = ( i0 + 1 ))
					{
						c0 = myself::GetCreatureFromID(room0::GetMemberID(i0));
						if( myself::HaveMemo(c0,635) )
						{
							myself::SetMemoStateEx(c0,635,1,-1);
						}
					}
					myself::InstantTeleportInMyTerritory(escape_x,escape_y,escape_z,100);
					gg::SendMakerScriptEvent(maker0,0,0,0);
					room0.status::Exchange(0);
					room0::Clear();
				}
				else
				{
					party0 = myself::GetPartyFromID(room0.party_id);
					c0 = myself::GetLeaderOfParty(party0);
					i3 = 0;
					if( c0.alive == 0 || myself::InMyTerritory(c0) == 0 )
					{
						for(i0 = 0; i0 < room0.member_count; i0 = ( i0 + 1 ))
						{
							c0 = myself::GetCreatureFromID(room0::GetMemberID(i0));
							if( myself::HaveMemo(c0,635) )
							{
								myself::SetMemoStateEx(c0,635,1,-1);
							}
						}
						myself::InstantTeleportInMyTerritory(escape_x,escape_y,escape_z,100);
						gg::SendMakerScriptEvent(maker0,0,0,0);
						c0 = myself::GetCreatureFromID(room0::GetMemberID(0));
						myself::AddLog(8,c0,2);
					}
					else if( RoomIndex == 8 && myself.i_ai1 == 2 )
					{
					}
					else
					{
						room1 = rlist0::GetRoomInfo(8);
						if( RoomIndex != 8 && gg::Rand(100) < 15 && room1.status.value == 0 )
						{
							myself::TeleportParty(room0.party_id,PosX8,PosY8,myself::FloatToInt(myself.sm.z),2000,0);
							room1.status::Exchange(1);
							gg::SendMakerScriptEvent(maker0,0,0,0);
							room1::SetParty(room0.party_id);
						}
						else
						{
							i0 = -1;
							for(i1 = 0; i1 < 8; i1 = ( i1 + 1 ))
							{
								if( i0 == -1 )
								{
									i2 = gg::Rand(8);
									room1 = rlist0::GetRoomInfo(i2);
									if( room1.status.value == 0 )
									{
										room1.status::Exchange(1);
										i0 = i2;
									}
								}
							}
							if( i0 == -1 )
							{
								for(i1 = 0; i1 < 8; i1 = ( i1 + 1 ))
								{
									if( i0 == -1 )
									{
										room1 = rlist0::GetRoomInfo(i1);
										if( room1.status.value == 0 )
										{
											room1.status::Exchange(1);
											i0 = i1;
										}
									}
								}
							}
							if( i0 == -1 )
							{
								myself::Say("Error!! There is not a vacant room.");
							}
							else
							{
								gg::SendMakerScriptEvent(maker0,0,0,0);
								room1::SetParty(room0.party_id);
								switch(i0)
								{
									case 0:
									{
										myself::TeleportParty(room0.party_id,PosX0,PosY0,myself::FloatToInt(myself.sm.z),2000,0);
										break;
									}
									case 1:
									{
										myself::TeleportParty(room0.party_id,PosX1,PosY1,myself::FloatToInt(myself.sm.z),2000,0);
										break;
									}
									case 2:
									{
										myself::TeleportParty(room0.party_id,PosX2,PosY2,myself::FloatToInt(myself.sm.z),2000,0);
										break;
									}
									case 3:
									{
										myself::TeleportParty(room0.party_id,PosX3,PosY3,myself::FloatToInt(myself.sm.z),2000,0);
										break;
									}
									case 4:
									{
										myself::TeleportParty(room0.party_id,PosX4,PosY4,myself::FloatToInt(myself.sm.z),2000,0);
										break;
									}
									case 5:
									{
										myself::TeleportParty(room0.party_id,PosX5,PosY5,myself::FloatToInt(myself.sm.z),2000,0);
										break;
									}
									case 6:
									{
										myself::TeleportParty(room0.party_id,PosX6,PosY6,myself::FloatToInt(myself.sm.z),2000,0);
										break;
									}
									case 7:
									{
										myself::TeleportParty(room0.party_id,PosX7,PosY7,myself::FloatToInt(myself.sm.z),2000,0);
										break;
									}
								}
							}
						}
						room0.status::Exchange(0);
						room0::Clear();
					}
				}
			}
			super;
		}

	EventHandler SCRIPT_EVENT(script_event_arg1,script_event_arg2,c0)
	{
		if( script_event_arg1 == 3 || script_event_arg1 == 2 )
		{
			myself.i_ai1 = script_event_arg1;
		}
	}

}
