// One shot gate
int Rule = 2;
int RoundTime = 15;
int PlayerSpawn = 53;
int PlayerSpawnDir = 0;
int PlayerWeapon = 15;
int PlayerWeapon2 = 0;
int PlayerHP = 1500;

int trg0 = 0;
int trg1 = 0;
int trg2 = 0;
int trg3 = 0;
int trg4 = 0;
int trg5 = 0;
int trg6 = 0;
int trg7 = 0;
int trg8 = 0;
int trg9 = 0;
int timer0 = 0;
int timer1 = 0;
int gameover = 0;
int now = 0;


OnStart()
{
	AddNPC("npc03_01");
	AddNPC("npc03_02");
	AddNPC("npc03_03");
	AddNPC("npc03_04");
	AddNPC("npc03_05");
	AddNPC("npc03_06");
}

FindEnemy(int c, int p)
{
	int c2;
	
	if (c < 0)
	{
		return 0;
	}
		
	if (GetEnemy(c) == p)
	{	
		c2 = GetChar("sentry official");
		if (c2 >= 0)		
		{	
			Perform(c, "say", "Sir, I find a stranger£¡");
			
			ChangeBehavior(c2, "follow", p);
			Perform(c2, "say", "Go catch the stranger£¡£¡Don't let him escape£¡");
			Perform(c2, "pause", 3);
		}			
		return 1;
	}	
	
	return 0;
}

OnUpdate()
{
	int player = GetAnyChar("player");
	if (player < 0)
	{
		return 0;
	}
		
	int c;
	int c2;
	int flag;

	if (trg9 == 0)
	{
		PlayerPerform("say", "£¨What is that noise©q©q©q©q£¿£©");
		trg9 = 1;
	}
	
	if (trg0 == 0)
	{
		c = GetChar("Night owl");
		if (c >= 0)
		{				
			SetTarget(0, "waypoint", 119);		// near flag position
			SetTarget(1, "char", player);
			if (Distance(0, 1) < 400)
			{
				ChangeBehavior(c, "follow", "flag");
				Perform(c, "say", "Haha,you must be dead£¡£¡");
				SetTarget(0, "waypoint", 83);	// stone position
				ChangeBehavior(c, "attacktarget", 0, 3);
				trg0 = 1;	
			}
		}	
	}

	if (trg1 == 0)
	{
		c = GetChar("Bearded bandit");
		if (c >= 0)
		{		
			if (GetChar("flag") == c)
			{
				Perform(c, "say", "Let see what is inside¡«");
 					Perform(c, "pause", 8);
				Perform(c, "say", "Haha, I get the treasure easily£¡£¡");
				trg1 = 1;
			}
		}
	}
	if (trg1 == 1)
	{
		c = GetChar("Bearded bandit");
		if (c>=0 && GetEnemy(c)==player)
		{
			ChangeBehavior(c, "patrol", 59, 92, 83);
			Perform(c, "guard", 5);
			Perform(c, "say", "Who the hell are you? Get out!");
			Perform(c, "faceto", player);
			trg1 = 2;
		}
	}
	if (trg1 == 2)
	{
		c = GetChar("Bearded bandit");
		if (c>=0 && GetHP(c)<GetMaxHP(c)/2 && GetEnemy(c)==player)
		{
			SetTarget(0, "waypoint", 92);
			ChangeBehavior(c, "attacktarget", 0);
			
			Perform(c, "say", "Amazing skill that I never seen, but I am busy now, Bye!");
			Perform(c, "faceto", player);
			trg1 = 3;
			timer0 = GetGameTime() + 60;
		}
	}
	if (trg1 == 3)
	{
		c = GetChar("Bearded bandit");
		if (c>=0 && GetGameTime()>timer0 && GetEnemy(c)==player)
		{
			SetTarget(0, "char", c);
			SetTarget(1, "char", player);
			if (Distanve(0, 1) < 100)
			{
				ChangeBehavior(c, "follow", player);
				Perform(c, "guard", 3);
				Perform(c, "say", "You can run behind me? All right£¡");
				Perform(c, "faceto", player);				
				trg1 = 4;
			}
		}
	}	
	if (trg1>0 && trg1!=5)
	{
		c = GetAnyChar("Bearded bandit");
		if (c>=0 && GetHP(c)<=0)
		{
			Say(c, "My©q©qMy treasure©q©q");
			trg1 = 5;
		}
	}
	
	if (trg2 == 0)
	{
		c = GetChar("Night owl");
		c2 = GetChar("Bearded bandit");
		if (c>=0 && c2>=0 && GetEnemy(c)==c2 && GetEnemy(c2)==c)
		{
			flag = GetChar("flag");
			if (flag == c)
			{
				Perform(c2, "aggress");
				Perform(c2, "say", "I'm gonna slash you in pieces£¡");
				Perform(c2, "faceto", c);
				trg2 = 1;
			}			
			
			if (flag == c2)
			{			
				Perform(c2, "pause", 4);
				Perform(c, "aggress");
				Perform(c, "say", "You'd better give me the treasure£¡");
				Perform(c, "faceto", c2);
				
				Perform(c2, "aggress");
				Perform(c2, "say", "Shit,!I just wanna beat you£¡£¡");
				Perform(c2, "pause", 4);
				Perform(c2, "faceto", c);
				trg2 = 1;
			}			
		}
	}
	
	if (trg3 == 0)
	{
		c = GetChar("Night owl");
		if (c>=0 && GetEnemy(c)==player)
		{			
			Perform(c, "aggress");
			Perform(c, "say", "hahaha£¡");
			Perform(c, "faceto", player);
			
			PlayerPerform("say", "©q©q©q©q©q©q");
			PlayerPerform("pause", 4);
			
			trg3 = 1;
		}		
	}
	if (trg3 == 1)
	{
		c = GetChar("Night owl");
		if (c>=0 && GetEnemy(c)==player)
		{
			if (GetChar("flag") == c)
			{
				ChangeBehavior(c, "run");
				Perform(c, "say", "Treasure is here, come on£¡");		
				Perform(c, "faceto", player);
				
				PlayerPerform("say", "©q©q©q©q©q©q");
				PlayerPerform("pause", 5);
								
				trg3 = 2;
			}
		}		
	}
	
	if (trg4 == 0)
	{
		c = GetChar("flag");
		if (c == player)
		{
			c2 = GetChar("Night owl");			
			if (c2 >= 0)
			{
				ChangeBehavior(c2, "follow", "flag");				
			}						
			
			PlayerPerform("say", "£¨seems like tough, I need to quickly open the gate then get in!£©");
			PlayerPerform("pause", 3);
			PlayerPerform("say", "£¨That should be the pledge for going through gates£©");
			trg4 = 1;
		}
	}
	if (trg4 == 1)
	{
		c = GetChar("Bearded bandit");
		if (c>=0 && GetEnemy(c)==player)
		{
			Perform(c, "attack");
			Perform(c, "say", "Leave what you have or leave your life£¡");
			Perform(c, "faceto", player);
			trg4 = 2;
		}
	}

	if (trg5 == 0)
	{
		c = GetChar("Sentry A");
		if (FindEnemy(c, player))
		{
			trg5 = 1;
		}		
	}	
	
	if (trg6 == 0)
	{
		c = GetChar("Sentry B");
		if (FindEnemy(c, player))
		{
			trg6 = 1;
		}		
	}
	
	if (trg7 == 0)
	{
		c = GetChar("Sentry C");
		if (FindEnemy(c, player))
		{
			trg7 = 1;
		}		
	}		
		
	if (trg5==0 && trg6==0 && trg7==0 && trg8==0)
	{
		c = GetChar("sentry official");
		if (c>=0 && GetEnemy(c)==player)		
		{	
			ChangeBehavior(c, "follow", player);
			Perform(c, "say", "Go catch the stranger£¡£¡Don't let him escape£¡");
			Perform(c, "pause", 3);
			Perform(c, "faceto", player);
			trg8 = 1;
		}			
	}
	
	
	now = GetGameTime();	
	if (gameover==0 && GetHP(player)<=0)
	{
		gameover = -1;
		timer1 = now + 2; 
	}
	if ((gameover==1 || gameover==-1) && now>timer1)
	{
		GameOver(gameover);
		gameover = 2;
	}	
}