class 1 ai_boss07_valakas_set_db : default_npc
{
parameter:
	int	GM_ID = 3;
	int	NorMalAttackLeft = 1198337;
	int	NorMalAttackRight = 1198593;
	int	RearStrike = 1199361;
	int	RearThrow = 1200129;
	int	Meteor = 1200641;
	int	BreathHigh = 1199105;
	int	BreathLow = 1198849;
	int	Fear = 1200385;

handler:
	EventHandler CREATED(reply,i0)
	{
		if( GM_ID != 0 )
		{
			i0 = myself::GetGlobalMap(GM_ID);
			if( i0 == -1 )
			{
				myself::RegisterGlobalMap(GM_ID,myself::GetIndexFromCreature(myself.sm));
			}
		}
		if( reply == 0 )
		{
			myself::SetDBValue(myself.sm,0);
		}
		else if( myself.sm.db_value == 1 )
		{
			myself::AddTimerEx(1001,( ( 10 * 60 ) * 1000 ));
		}
		super;
	}

	EventHandler SCRIPT_EVENT(script_event_arg1,script_event_arg2)
	{
		if( myself.sm.db_value == 0 )
		{
			myself::AddTimerEx(1001,( ( 20 * 60 ) * 1000 ));
			myself::SetDBValue(myself.sm,1);
		}
		super;
	}

	EventHandler TIMER_FIRED_EX(timer_id,i0,i1,i2,c0,c2)
	{
		if( timer_id == 1001 )
		{
			myself::SetDBValue(myself.sm,2);
			myself::InstantTeleport(myself.sm,212852,-114842,-1632);
			myself.i_quest1 = myself::GetCurrentTick();
			myself::EffectMusic(myself.sm,10000,"B03_A");
			myself::AddTimerEx(1004,2000);
		}
		if( timer_id == 1004 )
		{
			myself::SpecialCamera(myself.sm,1800,180,-1,1500,15000,10000,0,0,1,0);
			myself::AddTimerEx(1102,1500);
		}
		if( timer_id == 1102 )
		{
			myself::SpecialCamera(myself.sm,1300,180,-5,3000,15000,10000,0,-5,1,0);
			myself::AddTimerEx(1103,3300);
		}
		if( timer_id == 1103 )
		{
			myself::SpecialCamera(myself.sm,500,180,-8,600,15000,10000,0,60,1,0);
			myself::AddTimerEx(1104,2900);
		}
		if( timer_id == 1104 )
		{
			myself::SpecialCamera(myself.sm,800,180,-8,2700,15000,10000,0,30,1,0);
			myself::AddTimerEx(1105,2700);
		}
		if( timer_id == 1105 )
		{
			myself::SpecialCamera(myself.sm,200,250,70,0,15000,10000,30,80,1,0);
			myself::AddTimerEx(1106,1);
		}
		if( timer_id == 1106 )
		{
			myself::SpecialCamera(myself.sm,1100,250,70,2500,15000,10000,30,80,1,0);
			myself::AddTimerEx(1107,3200);
		}
		if( timer_id == 1107 )
		{
			myself::SpecialCamera(myself.sm,700,150,30,0,15000,10000,-10,60,1,0);
			myself::AddTimerEx(1108,1400);
		}
		if( timer_id == 1108 )
		{
			myself::SpecialCamera(myself.sm,1200,150,20,2900,15000,10000,-10,30,1,0);
			myself::AddTimerEx(1109,6700);
		}
		if( timer_id == 1109 )
		{
			myself::SpecialCamera(myself.sm,750,170,-10,3400,15000,4000,10,-15,1,0);
			myself::AddTimerEx(1110,5700);
		}
		if( timer_id == 1110 )
		{
			myself::AddUseSkillDesire(myself.sm,1200897,1,1,4000000);
			myself::SetDBValue(myself.sm,3);
			myself::AddTimerEx(1002,( 60 * 1000 ));
			if( myself::IsNullCreature(myself.c_quest2) )
			{
				myself.i_quest2 = 0;
			}
			else if( myself::DistFromMe(myself.c_quest2) > 5000 || myself.c_quest2.alive == 0 )
			{
				myself.i_quest2 = 0;
			}
			if( myself::IsNullCreature(myself.c_quest3) )
			{
				myself.i_quest3 = 0;
			}
			else if( myself::DistFromMe(myself.c_quest3) > 5000 || myself.c_quest3.alive == 0 )
			{
				myself.i_quest3 = 0;
			}
			if( myself::IsNullCreature(myself.c_quest4) )
			{
				myself.i_quest4 = 0;
			}
			if( myself::DistFromMe(myself.c_quest4) > 5000 || myself.c_quest4.alive == 0 )
			{
				myself.i_quest4 = 0;
			}
			if( myself.i_quest2 > myself.i_quest3 )
			{
				i1 = 2;
				i2 = myself.i_quest2;
				c2 = myself.c_quest2;
			}
			else
			{
				i1 = 3;
				i2 = myself.i_quest3;
				c2 = myself.c_quest3;
			}
			if( myself.i_quest4 > i2 )
			{
				i1 = 4;
				i2 = myself.i_quest4;
				c2 = myself.c_quest4;
			}
			if( i2 > 0 )
			{
				if( gg::Rand(100) < 70 )
				{
					switch(i1)
					{
						case 2:
						{
							myself.i_quest2 = 500;
							break;
						}
						case 3:
						{
							myself.i_quest3 = 500;
							break;
						}
						case 4:
						{
							myself.i_quest4 = 500;
							break;
						}
					}
				}
				if( myself.sm.hp < ( ( myself.sm.max_hp * 1.000000 ) / 4.000000 ) )
				{
					i0 = 0;
					i1 = 0;
					if( ( myself::DistFromMe(c2) < 1423 && myself::GetDirection(c2) < ( 180 + 8 ) && myself::GetDirection(c2) > ( 180 - 8 ) ) || ( myself::DistFromMe(c2) < 802 && myself::GetDirection(c2) < ( 180 + 14 ) && myself::GetDirection(c2) > ( 180 - 14 ) ) )
					{
						i0 = 1;
					}
					if( ( myself::DistFromMe(c2) < 1423 && myself::GetDirection(c2) < ( 180 + 8 ) && myself::GetDirection(c2) > ( 180 - 8 ) ) || ( myself::DistFromMe(c2) < 802 && myself::GetDirection(c2) < ( 180 + 14 ) && myself::GetDirection(c2) > ( 180 - 14 ) ) )
					{
						i1 = 1;
					}
					if( c2.z < ( myself.sm.z + 200 ) )
					{
						if( gg::Rand(100) < 20 )
						{
							myself::AddUseSkillDesire(myself.sm,Meteor,0,1,1000000);
						}
						else if( gg::Rand(100) < 15 )
						{
							myself::AddUseSkillDesire(c2,Fear,0,1,1000000);
						}
						else if( gg::Rand(100) < 15 && i0 == 1 && myself.i_quest0 == 1 )
						{
							myself::AddUseSkillDesire(c2,RearStrike,0,1,1000000);
						}
						else if( gg::Rand(100) < 10 && i1 == 1 )
						{
							myself::AddUseSkillDesire(c2,RearThrow,0,1,1000000);
						}
						else if( gg::Rand(100) < 35 )
						{
							myself::AddUseSkillDesire(c2,BreathLow,0,1,1000000);
						}
						else if( myself::GetDirection(c2) > 0 && myself::GetDirection(c2) <= 180 )
						{
							myself::AddUseSkillDesire(c2,NorMalAttackRight,0,1,1000000);
						}
						else
						{
							myself::AddUseSkillDesire(c2,NorMalAttackLeft,0,1,1000000);
						}
					}
					else if( gg::Rand(100) < 20 )
					{
						myself::AddUseSkillDesire(myself.sm,Meteor,0,1,1000000);
					}
					else if( gg::Rand(100) < 15 )
					{
						myself::AddUseSkillDesire(c2,Fear,0,1,1000000);
					}
					else
					{
						myself::AddUseSkillDesire(c2,BreathHigh,0,1,1000000);
					}
				}
				else if( myself.sm.hp < ( ( myself.sm.max_hp * 2.000000 ) / 4.000000 ) )
				{
					i0 = 0;
					i1 = 0;
					if( ( myself::DistFromMe(c2) < 1423 && myself::GetDirection(c2) < ( 180 + 8 ) && myself::GetDirection(c2) > ( 180 - 8 ) ) || ( myself::DistFromMe(c2) < 802 && myself::GetDirection(c2) < ( 180 + 14 ) && myself::GetDirection(c2) > ( 180 - 14 ) ) )
					{
						i0 = 1;
					}
					if( ( myself::DistFromMe(c2) < 1423 && myself::GetDirection(c2) < ( 180 + 8 ) && myself::GetDirection(c2) > ( 180 - 8 ) ) || ( myself::DistFromMe(c2) < 802 && myself::GetDirection(c2) < ( 180 + 14 ) && myself::GetDirection(c2) > ( 180 - 14 ) ) )
					{
						i1 = 1;
					}
					if( c2.z < ( myself.sm.z + 200 ) )
					{
						if( gg::Rand(100) < 5 )
						{
							myself::AddUseSkillDesire(myself.sm,Meteor,0,1,1000000);
						}
						else if( gg::Rand(100) < 10 )
						{
							myself::AddUseSkillDesire(c2,Fear,0,1,1000000);
						}
						else if( gg::Rand(100) < 10 && i0 == 1 && myself.i_quest0 == 1 )
						{
							myself::AddUseSkillDesire(c2,RearStrike,0,1,1000000);
						}
						else if( gg::Rand(100) < 10 && i1 == 1 )
						{
							myself::AddUseSkillDesire(c2,RearThrow,0,1,1000000);
						}
						else if( gg::Rand(100) < 20 )
						{
							myself::AddUseSkillDesire(c2,BreathLow,0,1,1000000);
						}
						else if( myself::GetDirection(c2) > 0 && myself::GetDirection(c2) <= 180 )
						{
							myself::AddUseSkillDesire(c2,NorMalAttackRight,0,1,1000000);
						}
						else
						{
							myself::AddUseSkillDesire(c2,NorMalAttackLeft,0,1,1000000);
						}
					}
					else if( gg::Rand(100) < 5 )
					{
						myself::AddUseSkillDesire(myself.sm,Meteor,0,1,1000000);
					}
					else if( gg::Rand(100) < 10 )
					{
						myself::AddUseSkillDesire(c2,Fear,0,1,1000000);
					}
					else
					{
						myself::AddUseSkillDesire(c2,BreathHigh,0,1,1000000);
					}
				}
				else if( myself.sm.hp < ( ( myself.sm.max_hp * 3.000000 ) / 4.000000 ) )
				{
					i0 = 0;
					i1 = 0;
					if( ( myself::DistFromMe(c2) < 1423 && myself::GetDirection(c2) < ( 180 + 8 ) && myself::GetDirection(c2) > ( 180 - 8 ) ) || ( myself::DistFromMe(c2) < 802 && myself::GetDirection(c2) < ( 180 + 14 ) && myself::GetDirection(c2) > ( 180 - 14 ) ) )
					{
						i0 = 1;
					}
					if( ( myself::DistFromMe(c2) < 1423 && myself::GetDirection(c2) < ( 180 + 8 ) && myself::GetDirection(c2) > ( 180 - 8 ) ) || ( myself::DistFromMe(c2) < 802 && myself::GetDirection(c2) < ( 180 + 14 ) && myself::GetDirection(c2) > ( 180 - 14 ) ) )
					{
						i1 = 1;
					}
					if( c2.z < ( myself.sm.z + 200 ) )
					{
						if( gg::Rand(100) < 0 )
						{
							myself::AddUseSkillDesire(myself.sm,Meteor,0,1,1000000);
						}
						else if( gg::Rand(100) < 5 )
						{
							myself::AddUseSkillDesire(c2,Fear,0,1,1000000);
						}
						else if( gg::Rand(100) < 5 && i0 == 1 && myself.i_quest0 == 1 )
						{
							myself::AddUseSkillDesire(c2,RearStrike,0,1,1000000);
						}
						else if( gg::Rand(100) < 10 && i1 == 1 )
						{
							myself::AddUseSkillDesire(c2,RearThrow,0,1,1000000);
						}
						else if( gg::Rand(100) < 15 )
						{
							myself::AddUseSkillDesire(c2,BreathLow,0,1,1000000);
						}
						else if( myself::GetDirection(c2) > 0 && myself::GetDirection(c2) <= 180 )
						{
							myself::AddUseSkillDesire(c2,NorMalAttackRight,0,1,1000000);
						}
						else
						{
							myself::AddUseSkillDesire(c2,NorMalAttackLeft,0,1,1000000);
						}
					}
					else if( gg::Rand(100) < 0 )
					{
						myself::AddUseSkillDesire(myself.sm,Meteor,0,1,1000000);
					}
					else if( gg::Rand(100) < 5 )
					{
						myself::AddUseSkillDesire(c2,Fear,0,1,1000000);
					}
					else
					{
						myself::AddUseSkillDesire(c2,BreathHigh,0,1,1000000);
					}
				}
				else
				{
					i0 = 0;
					i1 = 0;
					if( ( myself::DistFromMe(c2) < 1423 && myself::GetDirection(c2) < ( 180 + 8 ) && myself::GetDirection(c2) > ( 180 - 8 ) ) || ( myself::DistFromMe(c2) < 802 && myself::GetDirection(c2) < ( 180 + 14 ) && myself::GetDirection(c2) > ( 180 - 14 ) ) )
					{
						i0 = 1;
					}
					if( ( myself::DistFromMe(c2) < 1423 && myself::GetDirection(c2) < ( 180 + 8 ) && myself::GetDirection(c2) > ( 180 - 8 ) ) || ( myself::DistFromMe(c2) < 802 && myself::GetDirection(c2) < ( 180 + 14 ) && myself::GetDirection(c2) > ( 180 - 14 ) ) )
					{
						i1 = 1;
					}
					if( c2.z < ( myself.sm.z + 200 ) )
					{
						if( gg::Rand(100) < 0 )
						{
							myself::AddUseSkillDesire(myself.sm,Meteor,0,1,1000000);
						}
						else if( gg::Rand(100) < 10 )
						{
							myself::AddUseSkillDesire(c2,Fear,0,1,1000000);
						}
						else if( gg::Rand(100) < 5 && i0 == 1 && myself.i_quest0 == 1 )
						{
							myself::AddUseSkillDesire(c2,RearStrike,0,1,1000000);
						}
						else if( gg::Rand(100) < 10 && i1 == 1 )
						{
							myself::AddUseSkillDesire(c2,RearThrow,0,1,1000000);
						}
						else if( gg::Rand(100) < 15 )
						{
							myself::AddUseSkillDesire(c2,BreathLow,0,1,1000000);
						}
						else if( myself::GetDirection(c2) > 0 && myself::GetDirection(c2) <= 180 )
						{
							myself::AddUseSkillDesire(c2,NorMalAttackRight,0,1,1000000);
						}
						else
						{
							myself::AddUseSkillDesire(c2,NorMalAttackLeft,0,1,1000000);
						}
					}
					else if( gg::Rand(100) < 0 )
					{
						myself::AddUseSkillDesire(myself.sm,Meteor,0,1,1000000);
					}
					else if( gg::Rand(100) < 10 )
					{
						myself::AddUseSkillDesire(c2,Fear,0,1,1000000);
					}
					else
					{
						myself::AddUseSkillDesire(c2,BreathHigh,0,1,1000000);
					}
				}
			}
		}
		if( timer_id == 1111 )
		{
			myself::SpecialCamera(myself.sm,1100,210,-5,3000,15000,10000,-13,0,1,1);
			myself::AddTimerEx(1112,3500);
		}
		if( timer_id == 1112 )
		{
			myself::SpecialCamera(myself.sm,1300,200,-8,3000,15000,10000,0,15,1,1);
			myself::AddTimerEx(1113,4500);
		}
		if( timer_id == 1113 )
		{
			myself::SpecialCamera(myself.sm,1000,190,0,500,15000,10000,0,10,1,1);
			myself::AddTimerEx(1114,500);
		}
		if( timer_id == 1114 )
		{
			myself::SpecialCamera(myself.sm,1700,120,0,2500,15000,10000,12,40,1,1);
			myself::AddTimerEx(1115,4600);
		}
		if( timer_id == 1115 )
		{
			myself::SpecialCamera(myself.sm,1700,20,0,700,15000,10000,10,10,1,1);
			myself::AddTimerEx(1116,750);
		}
		if( timer_id == 1116 )
		{
			myself::SpecialCamera(myself.sm,1700,10,0,1000,15000,10000,20,70,1,1);
			myself::AddTimerEx(1117,2500);
		}
		if( timer_id == 1117 )
		{
			myself::SpecialCamera(myself.sm,1700,10,0,300,15000,250,20,-20,1,1);
		}
		super;
	}

	EventHandler MY_DYING(i0,plist0,pos0)
	{
		myself::UnregisterGlobalMap(GM_ID);
		myself::SetDBValue(myself.sm,0);
		myself::EffectMusic(myself.sm,10000,"B03_D");
		myself::SpecialCamera(myself.sm,2000,130,-1,0,15000,10000,0,0,1,1);
		myself::AddTimerEx(1111,500);
	}

}
