class 1 evil_spirit : warrior_hold
{
parameter:
	int	GM_ID = 7;
	int	GM_ID1 = 20;
	int	GM_ID2 = 21;
	int	GM_ID3 = 22;
	int	GM_ID4 = 23;

handler:
	EventHandler CREATED(i0,i1,i2,i3)
	{
		myself.i_ai1 = 0;
		myself.i_ai0 = myself::GetTick();
		i0 = myself::GetGlobalMap(GM_ID1);
		i1 = myself::GetGlobalMap(GM_ID2);
		i2 = myself::GetGlobalMap(GM_ID3);
		i3 = myself::GetGlobalMap(GM_ID4);
		if( i0 == -1 && i1 == -1 && i2 == -1 && i3 == -1 )
		{
			myself::RegisterGlobalMap(GM_ID1,myself::GetIndexFromCreature(myself.sm));
			myself.i_ai2 = GM_ID1;
		}
		if( i0 != -1 && i1 == -1 && i2 == -1 && i3 == -1 )
		{
			myself::RegisterGlobalMap(GM_ID2,myself::GetIndexFromCreature(myself.sm));
			myself.i_ai2 = GM_ID2;
		}
		if( i0 != -1 && i1 != -1 && i2 == -1 && i3 == -1 )
		{
			myself::RegisterGlobalMap(GM_ID3,myself::GetIndexFromCreature(myself.sm));
			myself.i_ai2 = GM_ID3;
		}
		if( i0 != -1 && i1 != -1 && i2 != -1 && i3 == -1 )
		{
			myself::RegisterGlobalMap(GM_ID4,myself::GetIndexFromCreature(myself.sm));
			myself.i_ai2 = GM_ID4;
		}
		myself::AddTimerEx(1001,( ( 1 * 15 ) * 1000 ));
		myself::AddTimerEx(1003,600000);
		super;
	}

	EventHandler SCRIPT_EVENT(script_event_arg1,script_event_arg2)
	{
		if( script_event_arg1 == 1 )
		{
			myself.i_ai1 = 0;
		}
		if( script_event_arg1 == 2 && myself.i_ai1 == 0 )
		{
			myself.i_ai1 = 1;
			myself::AddTimerEx(1004,40000);
			myself::AddTimerEx(1002,( ( 1 * 1 ) * 1000 ));
		}
		if( script_event_arg1 == 3 )
		{
			myself.i_ai0 = myself::GetTick();
		}
	}

	EventHandler ATTACKED(attacker)
	{
		if( attacker.is_pc != 0 || ( myself::IsInCategory(11,attacker.npc_class_id) && myself::DistFromMe(attacker) < 500 ) )
		{
			myself::AddAttackDesire(attacker,0,( 1 * 100 ));
		}
		else
		{
			myself::RemoveAttackDesire(attacker.id);
			myself::AddTimerEx(2001,1000);
		}
		super;
	}

	EventHandler CLAN_ATTACKED(attacker,damage)
	{
		if( myself::GetLifeTime() > 7 && ( ( attacker.is_pc != 0 || myself::IsInCategory(11,attacker.npc_class_id) ) && myself::DistFromMe(attacker) < 500 ) )
		{
			myself::AddAttackDesire(attacker,0,( 1 * 100 ));
		}
		else
		{
			myself::RemoveAttackDesire(attacker.id);
			myself::AddTimerEx(2001,1000);
		}
		super;
	}

	EventHandler TIMER_FIRED_EX(timer_id,i0,i1,i2,i3,i4,c0,c1,c2,c3,c4)
	{
		if( timer_id == 1003 )
		{
			i0 = myself::GetGlobalMap(GM_ID);
			if( i0 != -1 )
			{
				c0 = myself::GetCreatureFromIndex(i0);
				if( c0.db_value == 0 )
				{
					myself::UnregisterGlobalMap(myself.i_ai2);
					myself::Despawn();
				}
			}
			else
			{
				myself::UnregisterGlobalMap(myself.i_ai2);
				myself::Despawn();
			}
			myself::AddTimerEx(1003,10000);
		}
		if( timer_id == 1004 )
		{
			myself.i_ai1 = 0;
			return;
		}
		if( timer_id == 1001 && myself.i_ai1 == 0 )
		{
			if( ( myself::GetTick() - myself.i_ai0 ) < ( ( 3 * 60 ) * 1000 ) )
			{
				myself::CreateOnePrivateEx(1029050,"portrait_spirit",0,0,myself.start_x,myself.start_y,myself.start_z,0,0,0,0);
				myself::CreateOnePrivateEx(1029051,"portrait_spirit_winged",0,0,myself.start_x,myself.start_y,myself.start_z,0,0,0,0);
				myself::AddTimerEx(1001,( ( 5 * 60 ) * 1000 ));
				return;
			}
			else
			{
				myself::AddTimerEx(1001,( ( 1 * 15 ) * 1000 ));
				return;
			}
		}
		if( timer_id == 1002 && myself.i_ai1 == 1 )
		{
			if( ( myself::GetTick() - myself.i_ai0 ) < ( ( 3 * 60 ) * 1000 ) )
			{
				myself::CreateOnePrivateEx(1029050,"portrait_spirit",0,0,myself.start_x,myself.start_y,myself.start_z,0,0,0,0);
				myself::CreateOnePrivateEx(1029051,"portrait_spirit_winged",0,0,myself.start_x,myself.start_y,myself.start_z,0,0,0,0);
				myself::AddTimerEx(1002,( ( 1 * 15 ) * 1000 ));
				return;
			}
			else
			{
				myself::AddTimerEx(1002,( ( 1 * 15 ) * 1000 ));
				return;
			}
		}
		if( timer_id == 2001 )
		{
			if( myself::IsNullCreature(myself.top_desire_target) == 0 )
			{
				if( myself::DistFromMe(myself.top_desire_target) > 500 )
				{
					myself::RemoveAttackDesire(myself.top_desire_target.id);
					myself::AddTimerEx(2001,1000);
					return;
				}
				if( myself.top_desire_target.is_pc == 1 )
				{
					myself::AddAttackDesire(myself.top_desire_target,0,( 1 * 50 ));
				}
			}
		}
		super;
	}

	EventHandler SEE_CREATURE(creature)
	{
		if( creature.is_pc != 0 || ( myself::IsInCategory(11,creature.npc_class_id) && myself::DistFromMe(creature) < 500 ) )
		{
			myself::AddAttackDesire(creature,0,( 1 * 50 ));
		}
		super;
	}

	EventHandler MY_DYING()
	{
		myself::UnregisterGlobalMap(myself.i_ai2);
		super;
	}

}
