class 1 ai_scarlet_select_target_skill : use_hate_and_random
{
parameter:
	int	DaemonAttack = 1283585;
	int	DaemonAttack1 = 1283586;
	int	DaemonDrain = 1284865;
	int	DaemonField = 1284609;
	int	DaemonField1 = 1284610;
	int	DaemonCharge = 1283843;
	int	Ver1 = 20;
	int	Ver2 = 10;
	int	Ver3 = 5;

handler:
	EventHandler ATTACKED(attacker,damage,i0,i1,i2,c2,skill_name_id,h0)
	{
		i3 = ( attacker.z - myself.sm.z );
		if( i3 < 0 )
		{
			i3 = -i3;
		}
		if( i3 > 300 )
		{
			return;
		}
		super;
		if( attacker.yongma_type == 1 && myself::GetAbnormalLevel(attacker,myself::Skill_GetAbnormalType(1090049)) <= 0 )
		{
			if( myself::Skill_GetConsumeMP(1090049) < myself.sm.mp && myself::Skill_GetConsumeHP(1090049) < myself.sm.hp && myself::Skill_InReuseDelay(1090049) == 0 )
			{
				myself::AddUseSkillDesire(attacker,1090049,0,1,1000000);
			}
		}
		h0 = myself::GetMaxHateInfo(0);
		if( myself::IsNullHateInfo(h0) == 0 )
		{
			if( myself::IsNullCreature(h0.creature) == 0 )
			{
				c2 = h0.creature;
			}
			else
			{
				c2 = myself.top_desire_target;
			}
		}
		else
		{
			c2 = myself.top_desire_target;
		}
		i0 = 0;
		if( myself.sm.hp < ( ( myself.sm.max_hp * 1.000000 ) / 4.000000 ) )
		{
			i0 = Ver1;
		}
		else if( myself.sm.hp < ( ( myself.sm.max_hp * 2.000000 ) / 4.000000 ) )
		{
			i0 = Ver2;
		}
		else if( myself.sm.hp < ( ( myself.sm.max_hp * 3.000000 ) / 4.000000 ) )
		{
			i0 = Ver3;
		}
		if( gg::Rand(100) < ( 0 + i0 ) )
		{
			if( GM_ID == 7 )
			{
				if( myself::Skill_GetConsumeMP(DaemonField) < myself.sm.mp && myself::Skill_GetConsumeHP(DaemonField) < myself.sm.hp )
				{
					if( myself::Skill_InReuseDelay(DaemonField) == 0 )
					{
						i4 = ( 500000 + gg::Rand(500000) );
						myself::AddUseSkillDesire(myself.sm,DaemonField,0,1,i4);
						return;
					}
				}
			}
			else if( myself::Skill_GetConsumeMP(DaemonField1) < myself.sm.mp && myself::Skill_GetConsumeHP(DaemonField1) < myself.sm.hp )
			{
				if( myself::Skill_InReuseDelay(DaemonField1) == 0 )
				{
					i4 = ( 500000 + gg::Rand(500000) );
					myself::AddUseSkillDesire(myself.sm,DaemonField1,0,1,i4);
					return;
				}
			}
		}
		else if( gg::Rand(100) < ( 5 + i0 ) && myself::FloatToInt(myself::DistFromMe(c2)) < 150 )
		{
			if( GM_ID == 7 )
			{
				if( myself::Skill_GetConsumeMP(DaemonAttack) < myself.sm.mp && myself::Skill_GetConsumeHP(DaemonAttack) < myself.sm.hp )
				{
					if( myself::Skill_InReuseDelay(DaemonAttack) == 0 )
					{
						i4 = ( 500000 + gg::Rand(500000) );
						myself::AddUseSkillDesire(c2,DaemonAttack,0,1,i4);
						return;
					}
				}
			}
			else if( myself::Skill_GetConsumeMP(DaemonAttack1) < myself.sm.mp && myself::Skill_GetConsumeHP(DaemonAttack1) < myself.sm.hp )
			{
				if( myself::Skill_InReuseDelay(DaemonAttack1) == 0 )
				{
					i4 = ( 500000 + gg::Rand(500000) );
					myself::AddUseSkillDesire(c2,DaemonAttack1,0,1,i4);
					return;
				}
			}
		}
		else if( gg::Rand(100) < ( 10 + i0 ) && GM_ID == 10 && myself::FloatToInt(myself::DistFromMe(c2)) < 600 )
		{
			if( myself::Skill_GetConsumeMP(DaemonDrain) < myself.sm.mp && myself::Skill_GetConsumeHP(DaemonDrain) < myself.sm.hp )
			{
				if( myself::Skill_InReuseDelay(DaemonDrain) == 0 )
				{
					i4 = ( 500000 + gg::Rand(500000) );
					myself::AddUseSkillDesire(c2,DaemonDrain,0,1,i4);
					return;
				}
			}
		}
		else if( gg::Rand(100) < ( 15 + i0 ) && GM_ID == 10 && myself::FloatToInt(myself::DistFromMe(c2)) < 1000 && myself::FloatToInt(myself::DistFromMe(c2)) > 500 )
		{
			if( myself::Skill_GetConsumeMP(DaemonCharge) < myself.sm.mp && myself::Skill_GetConsumeHP(DaemonCharge) < myself.sm.hp )
			{
				if( myself::Skill_InReuseDelay(DaemonCharge) == 0 )
				{
					myself.c_ai0 = c2;
					i4 = ( 500000 + gg::Rand(500000) );
					myself::AddUseSkillDesire(c2,DaemonCharge,0,1,i4);
				}
			}
		}
	}

	EventHandler SEE_CREATURE(creature,i0,i1,i2,c2,h0)
	{
		i3 = ( creature.z - myself.sm.z );
		if( i3 < 0 )
		{
			i3 = -i3;
		}
		if( i3 > 300 )
		{
			return;
		}
		if( creature.is_pc == 0 && myself::IsInCategory(11,creature.npc_class_id) == 0 )
		{
			return;
		}
		super;
		h0 = myself::GetMaxHateInfo(0);
		if( myself::IsNullHateInfo(h0) == 0 )
		{
			if( myself::IsNullCreature(h0.creature) == 0 )
			{
				c2 = h0.creature;
			}
			else
			{
				c2 = myself.top_desire_target;
			}
		}
		else
		{
			c2 = myself.top_desire_target;
		}
		i0 = 0;
		if( myself.sm.hp < ( ( myself.sm.max_hp * 1.000000 ) / 4.000000 ) )
		{
			i0 = Ver1;
		}
		else if( myself.sm.hp < ( ( myself.sm.max_hp * 2.000000 ) / 4.000000 ) )
		{
			i0 = Ver2;
		}
		else if( myself.sm.hp < ( ( myself.sm.max_hp * 3.000000 ) / 4.000000 ) )
		{
			i0 = Ver3;
		}
		if( gg::Rand(100) < ( 15 + i0 ) && GM_ID == 10 && myself::FloatToInt(myself::DistFromMe(c2)) < 1000 && myself::FloatToInt(myself::DistFromMe(c2)) > 500 )
		{
			if( myself::Skill_GetConsumeMP(DaemonCharge) < myself.sm.mp && myself::Skill_GetConsumeHP(DaemonCharge) < myself.sm.hp )
			{
				if( myself::Skill_InReuseDelay(DaemonCharge) == 0 )
				{
					myself.c_ai0 = c2;
					i4 = ( 500000 + gg::Rand(500000) );
					myself::AddUseSkillDesire(c2,DaemonCharge,0,1,i4);
				}
			}
		}
	}

	EventHandler SEE_SPELL(speller,skill_name_id,i0,h0)
	{
		i3 = ( speller.z - myself.sm.z );
		if( i3 < 0 )
		{
			i3 = -i3;
		}
		if( i3 > 300 )
		{
			return;
		}
		super;
		h0 = myself::GetMaxHateInfo(0);
		if( myself::IsNullHateInfo(h0) == 0 )
		{
			if( myself::IsNullCreature(h0.creature) == 0 )
			{
				c2 = h0.creature;
			}
			else
			{
				c2 = myself.top_desire_target;
			}
		}
		else
		{
			c2 = myself.top_desire_target;
		}
		i0 = 0;
		if( myself.sm.hp < ( ( myself.sm.max_hp * 1.000000 ) / 4.000000 ) )
		{
			i0 = Ver1;
		}
		else if( myself.sm.hp < ( ( myself.sm.max_hp * 2.000000 ) / 4.000000 ) )
		{
			i0 = Ver2;
		}
		else if( myself.sm.hp < ( ( myself.sm.max_hp * 3.000000 ) / 4.000000 ) )
		{
			i0 = Ver3;
		}
		if( gg::Rand(100) < ( 10 + i0 ) && GM_ID == 10 && myself::FloatToInt(myself::DistFromMe(c2)) < 600 )
		{
			if( myself::Skill_GetConsumeMP(DaemonDrain) < myself.sm.mp && myself::Skill_GetConsumeHP(DaemonDrain) < myself.sm.hp )
			{
				if( myself::Skill_InReuseDelay(DaemonDrain) == 0 )
				{
					i4 = ( 500000 + gg::Rand(500000) );
					myself::AddUseSkillDesire(c2,DaemonDrain,0,1,i4);
					return;
				}
			}
		}
	}

	EventHandler CLAN_ATTACKED(attacker,victim,damage,i0,h0)
	{
		i3 = ( attacker.z - myself.sm.z );
		if( i3 < 0 )
		{
			i3 = -i3;
		}
		if( i3 > 300 )
		{
			return;
		}
		super;
	}

	EventHandler SPELLED(speller,victim,damage,i0,h0)
	{
		i3 = ( speller.z - myself.sm.z );
		if( i3 < 0 )
		{
			i3 = -i3;
		}
		if( i3 > 300 )
		{
			return;
		}
		super;
	}

	EventHandler OUT_OF_TERRITORY(i0,i1,i2)
	{
		if( myself.sm.db_value != 3 )
		{
			return;
		}
		c0 = myself.top_desire_target;
		if( myself::IsNullCreature(c0) == 0 )
		{
			if( c0.is_pc == 0 )
			{
				myself::Shout(myself::IntToStr(c0.npc_class_id));
			}
			else
			{
				myself::Shout(c0.name);
			}
		}
		myself::StopMove();
		myself::RemoveAllAttackDesire();
		myself::InstantTeleport(myself.sm,174226,-88013,-5112);
	}

	EventHandler USE_SKILL_FINISHED(skill_name_id,success,target)
	{
		if( skill_name_id == DaemonCharge )
		{
			if( success == 1 )
			{
				target = myself.c_ai0;
				if( myself::IsNullCreature(target) == 0 && myself::FloatToInt(myself::DistFromMe(target)) < 1500 )
				{
					myself::InstantTeleport(myself.sm,myself::FloatToInt(target.x),myself::FloatToInt(target.y),myself::FloatToInt(target.z));
					i4 = ( 500000 + gg::Rand(500000) );
					myself::AddUseSkillDesire(target,DaemonAttack1,0,1,i4);
					myself::AddAttackDesire(target,1,200);
				}
			}
		}
	}

}
