// Royal Town
int Rule = 5;
int RoundTime = 30;
int PlayerSpawn = 83;
int PlayerSpawnDir = 45;
int PlayerWeapon = 24;
int PlayerWeapon2 = 21;
int PlayerHP = 2500;

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 timer2 = 0;

int hp0 = 0;
int hp1 = 0;
int hp2 = 0;
int hp3 = 0;
int hp4 = 0;

OnStart()
{
	AddNPC("npc05_01");
	AddNPC("npc05_02");

	AddNPC("npc05_05");
	AddNPC("npc05_06");
	AddNPC("npc05_07");
	AddNPC("npc05_08");
	AddNPC("npc05_09");
}

CallFriend(int c, int c2, int p)
{
	if (c>=0 && GetEnemy(c)==p)
	{
		ChangeBehavior(c, "follow", p);
		Perform(c, "guard", 3);
		Perform(c, "say", "An intruder, need a help£¡");
		Perform(c, "say", "Who are you£¡£¡");
		Perform(c, "faceto", p);
			
		if (c2 >= 0)
		{
			ChangeBehavior(c2, "follow", c);
		}
		return 1;
	}			
	return 0;
}

CallFriend2(int c, int c2, int p, int t)
{
	if (c>=0 && GetEnemy(c)==p)
	{
		ChangeBehavior(c, "follow", p);
		if (t == 1)
		{
			Perform(c, "say", "I find you£¡");
		} else
		{
			Perform(c, "say", "Let's play£¡£¡");
		}
		Perform(c, "faceto", p);
			
		if (c2 >= 0)
		{
			ChangeBehavior(c2, "follow", c);
		}
		return 1;
	}			
	return 0;
}

BackGuard(int c, int p, int say)
{
	if (c >= 0)
	{
		Perform(c, "guard", 100);
		Perform(c, "faceto", p);
		ChangeBehavior(c, "dodge", p);
		
		if (say == 1)
		{
			Perform(c, "say", "Take care£¡");
		} 
		if (say == 2)
		{
			Perform(c, "say", "©q©q©q©q©q");
		}
		
		Perform(c, "pause", 4);
		Perform(c, "faceto", p);				
					
		return 1;
	}
	return 0;			
}

PauseAll(int t, int p)
{
	int c;
	c = GetChar("Spear Guard");
	if (c >= 0)
	{
		Perform(c, "guard", t);
		Perform(c, "faceto", p);
	}
	c = GetChar("Blade Guard");
	if (c >= 0)
	{
		Perform(c, "guard", t);
		Perform(c, "faceto", p);
	}
	c = GetChar("Playmonk A");
	if (c >= 0)
	{
		Perform(c, "guard", t);
		Perform(c, "faceto", p);
	}
	c = GetChar("Playmonk B");
	if (c >= 0)
	{
		Perform(c, "guard", t);
		Perform(c, "faceto", p);
	}
	c = GetChar("Unnamed Killer");
	if (c >= 0)
	{
		Perform(c, "guard", t);
		Perform(c, "faceto", p);
	}		
	c = GetChar("Maskedman A");
	if (c >= 0)
	{
		Perform(c, "guard", t);
		Perform(c, "faceto", p);
	}		
	c = GetChar("Maskedman B");
	if (c >= 0)
	{
		Perform(c, "guard", t);
		Perform(c, "faceto", p);
	}								
	
	return 1;
}

OnUpdate()
{
	int player = GetChar("player");
	if (player < 0)
	{
		return 0;
	}
	
	int c;
	int c2;
	int c3;
	int c4;
	int c5;
	
	if (trg0 == 0)
	{
		PlayerPerform("block", 0);
		PlayerPerform("crouch", 0);
		PlayerPerform("say", "£¨Don't care too much, just take the pledge from Smith Fan, and finish my job£¡£©");
		PlayerPerform("pause", 2);
		PlayerPerform("say", "£¨©q©q©qHow come I get confused for these things£©");
		PlayerPerform("pause", 2);
		PlayerPerform("say", "£¨©q©q©qWhat is the matter©q©q©q©q©q£©");
		PlayerPerform("pause", 2);
		PlayerPerform("say", "£¨What does Andy Xiao mean £¿£¿£©");
		PlayerPerform("pause", 2);
		PlayerPerform("say", "£¨©q©qThat mysterious girl©q©q©q©q£©");
		PlayerPerform("pause", 2);
		PlayerPerform("say", "£¨©q©q©q©qWhat happen with that maskedman£¿£¿£©");
		PlayerPerform("pause", 2);
		PlayerPerform("say", "£¨Or Boss Gao cares about me©q©q©q©q£©");
		PlayerPerform("pause", 2);
		PlayerPerform("say", "£¨©q©qWhy I always feel someone is behind me©q©q©q©q£©");
		PlayerPerform("pause", 2);
		PlayerPerform("say", "£¨Maybe I have been found£¿£©");
		PlayerPerform("pause", 2);
		PlayerPerform("say", "£¨©q©q©qFinally, arrive here©q©q©q£©");
		PlayerPerform("crouch", 1);
		PlayerPerform("block", 1);
		trg0 = 1;
	}
	
	if (trg1==0)
	{
		c = GetChar("Spear Guard");
		c2 = GetChar("Blade Guard");
		
		if (CallFriend(c, c2, player))
		{
			trg1 = 1;
		}		
		if (trg1==0 && CallFriend(c2, c, player))
		{
			trg1 = 1;
		}
	}	
	
	if (trg2 == 0)
	{
		c = GetChar("Playmonk A");
		c2 = GetChar("Playmonk B");
		if (CallFriend2(c, c2, player, 1))
		{
			trg2 = 1;
		}		
		if (trg2==0 && CallFriend2(c2, c, player, 0))
		{
			trg2 = 1;
		}
	}
	if (trg2 == 1)
	{
		c = GetChar("Playmonk A");
		c2 = GetChar("Playmonk B");
		if (c>=0 && c2>=0 && GetEnemy(c)==player && GetEnemy(c2)==player)
		{
			Perform(c, "say", "We just play with you£¡£¡");
			Perform(c, "faceto", player);
			Perform(c2, "say", "We just play with you£¡£¡");
			Perform(c2, "faceto", player);
			trg2 = 2;
		}	
	}
	if (trg2 == 2)
	{
		c = GetChar("Playmonk A");
		c2 = GetChar("Playmonk B");
		if (c>=0 && GetHP(c)<GetMaxHP(c)/2)
		{
			Perform(c, "aggress");
			Perform(c, "say", "You'd better practice more£¡");
			Perform(c, "faceto", player);
			trg2 = 3;
		}
		if (c2>=0 && GetHP(c2)<GetMaxHP(c2)/2)
		{
			Perform(c2, "aggress");
			Perform(c2, "say", "Move or I'm gonna kill you£¡");
			Perform(c2, "faceto", player);
			trg2 = 3;
		}			
	}
	
	if (trg3 == 0)
	{
		c = GetChar("Unnamed Killer");
		SetTarget(0, "char", player);
		SetTarget(1, "char", c);
		if (c>=0 && GetEnemy(c)==player && Distance(0, 1)<100)
		{
			ChangeBehavior(c, "kill", player);
			
			c2 = GetChar("Spear Guard");
			if (c2 >= 0)
			{
				ChangeBehavior(c2, "follow", player);
			}
			c2 = GetChar("Blade Guard");
			if (c2 >= 0)
			{
				ChangeBehavior(c2, "follow", player);
			}			
			
			c2 = GetAnyChar("Playmonk A");
			c3 = GetAnyChar("Playmonk B");	
			if (c2>=0 && GetHP(c2)<=0 && c3>=0 && GetHP(c3)<=0)
			{
				Perform(c, "aggress");
				Perform(c, "say", "I heard you are tough, so show me, come on£¡");
				Perform(c, "say", "I have been waiting here for so long£¡");
				Perform(c, "faceto", player);
				
				PlayerPerform("say", "©q©q©q©q©q©q");
				PlayerPerform("pause", 5);
			} else
			{
				Perform(c, "say", "Back-off, I deal with him£¡");
				c2 = GetChar("Playmonk A");
				if (BackGuard(c2, player, 1))
				{
					hp1 = GetHP(c2);
				}
				c2 = GetChar("Playmonk B");
				if (BackGuard(c2, player, 1))
				{
					hp2 = GetHP(c2);
				}
				c2 = GetChar("Maskedman A");
				if (BackGuard(c2, player, 2))
				{
					hp3 = GetHP(c2);
				}
				c2 = GetChar("Maskedman B");
				if (BackGuard(c2, player, 2))
				{
					hp4 = GetHP(c2);
				}
				c2 = GetChar("Spear Guard");
				BackGuard(c2, player, 0);
				c2 = GetChar("Blade Guard");
				BackGuard(c2, player, 0);				
			}
			trg3 = 1;
		}
	}

	if (trg3==1 || trg3==2)
	{
		c5 = -1;
		c = GetChar("Playmonk A");
		c2 = GetChar("Playmonk B");
		c3 = GetChar("Maskedman A");
		c4 = GetChar("Maskedman B");
		
		if (hp1 > 0)
		{
			if (c>=0 && GetHP(c)<hp1)
			{
				Perform(c, "say", "That is your choice£¡£¡");
				Perform(c, "faceto", player);
				c5 = c;
			}
		}
		if (hp2 > 0)
		{
			if (c2>=0 && GetHP(c2)<hp2)
			{
				Perform(c2, "say", "Wanna die?");
				Perform(c2, "faceto", player);
				c5 = c2;
			}
		}
		if (hp3 > 0)
		{
			if (c3>=0 && GetHP(c3)<hp3)
			{
				Perform(c3, "say", "©q©q©q©q©q");
				Perform(c3, "faceto", player);
				c5 = c3;
			}
		}
		if (hp4 > 0)
		{
			if (c4>=0 && GetHP(c4)<hp4)
			{
				Perform(c, "say", "©q©q©q©q©q");
				Perform(c, "faceto", player);
				c5 = c4;
			}
		}
		if (c5 >= 0)
		{
			ChangeBehavior(c, "kill", player);
			ChangeBehavior(c2, "kill", player);
			ChangeBehavior(c3, "kill", player);
			ChangeBehavior(c4, "kill", player);
			trg3 = 3;
		}
	}

	if (trg3 == 1)
	{
		c = GetChar("Unnamed Killer");
		if (c>=0 && GetHP(c)<GetMaxHP(c)*2/3)
		{
			Perform(c, "guard", 4);
			Perform(c, "say", "You are not easy, I am gonna be serious£¡");
			Perform(c, "faceto", player);
								
			trg3 = 2;
		}
	}
	if (trg3 == 2)
	{
		c = GetChar("Unnamed Killer");
		if (c>=0 && GetHP(c)<GetMaxHP(c)/2)
		{
			c2 = GetChar("Playmonk A");
			if (c2 >= 0)
			{
				ChangeBehavior(c2, "follow", c);
				Perform(c2, "say", "Go£¡");
				Perform(c2, "pause", 6);
				Perform(c2, "say", "Are you Okay£¿£¿");
				Perform(c2, "faceto", c);
				Perform(c2, "pause", 8);
			}		
			c2 = GetChar("Playmonk B");
			if (c2 >= 0)
			{
				ChangeBehavior(c2, "follow", c);
				Perform(c2, "say", "Let me help you£¡");
				Perform(c2, "pause", 6);
				Perform(c2, "say", "You OK£¿£¿");
				Perform(c2, "faceto", c);
				Perform(c2, "pause", 8);
			}		
			
			trg3 = 3;			
		}		
	}
	if (trg3 == 3)
	{
		c = GetChar("Unnamed Killer");
		if (c>=0 && GetHP(c)<GetMaxHP(c)/3)
		{
			Perform(c, "say", "Never think that there is such a master in martial arts circles©q©q");
			Perform(c, "faceto", player);
			trg3 = 4;
		}
	}
	if (trg3 == 4)
	{		
		c = GetAnyChar("Unnamed Killer");
		if (c>=0 && GetHP(c)<=0)
		{
			Say(c, "Awesome! Fighting with you is worth my life©q©q©q");
			
			c2 = GetChar("Playmonk A");
			if (c2 >= 0)
			{
				ChangeBehavior(c2, "kill", player);
			}
			c2 = GetChar("Playmonk B");
			if (c2 >= 0)
			{
				ChangeBehavior(c2, "kill", player);
			}
			
			trg3 = 5;
		}
	}
	
	if (trg4==0 && trg6==0)
	{
		c = GetChar("T-Chan");
		SetTarget(0, "char", player);
		SetTarget(1, "char", c);
		if (c>=0 && GetEnemy(c)==player && Distance(0, 1)<80)
		{
			c2 = GetChar("Smith Fan");
			if (c2 >= 0)
			{
				ChangeBehavior(c, "follow", c2);
				SetTarget(0, "char", c2);
				ChangeBehavior(c, "attacktarget", 0);
			}
			Perform(c, "say", "Somebody gets the killer£¡£¡");
			Perform(c, "pause", 5);
			Perform(c, "say", "Stop! I'm here£¡");
			Perform(c, "say", "You dare murder Boss Fan£¡");
			Perform(c, "faceto", player);
			
			PlayerPerform("block", 0);
			PlayerPerform("say", "©q©q©q©q©q©q©q");
			PlayerPerform("pause", 3);
			PlayerPerform("block", 1);
			
			c3 = GetChar("Unnamed Killer");
			if (c3 >= 0)
			{
				ChangeBehavior(c3, "follow", player);
			}
			c3 = GetChar("Maskedman A");
			if (c3 >= 0)
			{
				ChangeBehavior(c3, "follow", player);
			}
			c3 = GetChar("Maskedman B");
			if (c3 >= 0)
			{
				ChangeBehavior(c3, "follow", player);
			}
			c3 = GetChar("Spear Guard");
			if (c3 >= 0)
			{
				ChangeBehavior(c2, "kill", player);
			}
			c3 = GetChar("Blade Guard");
			if (c2 >= 0)
			{
				ChangeBehavior(c2, "kill", player);
			}						
			
			trg4 = 1;
		}
	}
	if (trg4 == 1)
	{
		c = GetChar("T-Chan");
		c2 = GetChar("Smith Fan");
		if (c>=0 && c2>=0)
		{
			SetTarget(0, "char", c);
			SetTarget(1, "char", c2);
			if (Distance(0, 1) < 100)
			{
				ChangeBehavior(c, "patrol", 56, 66, 65, 61, 67);
				Perform(c, "say", "Shit! Kill him£¡");
				Perform(c, "pause", 5);
				Perform(c, "faceto", player);
				Perform(c, "say", "Yes, Boss£¡£¡");
				Perform(c, "pause", 6);
				Perform(c, "say", "Boss, there is a killer, please go away£¡£¡");
				Perform(c, "faceto", c2);
				
				Perform(c2, "say", "Don't kill him, I have something to ask him when I come back£¡");
				Perform(c2, "say", "What! Who dared murder me£¡");
				Perform(c2, "pause", 3);
				Perform(c2, "faceto", c);		
				trg4 = 2;						
				timer0 = GetGameTime() + 10;
			}
		}		
	}
	if (trg4==2 && GetGameTime()>timer0)
	{
		c = GetAnyChar("Smith Fan");
		if (c >= 0)
		{
			RemoveNPC(c);
		}
		trg4 = 3;
	}
	
	if (trg5 == 0)
	{
		c = GetChar("T-Chan");
		if (c>=0 && GetHP(c)<GetMaxHP(c)/2)
		{
			Perform(c, "say", "Come here, surrounding and slashing him into pieces for feeding dogs £¡£¡");
			Perform(c, "pause", 3);
			Perform(c, "say", "You are gonna die today£¡");
			Perform(c, "say", "Good...You can even hurt me£¡");
			Perform(c, "faceto", player);
			
			trg5 = 1;
		}		
	}

	if (trg5 == 1)
	{
		c = GetAnyChar("T-Chan");
		if (c>=0 && GetHP(c)<=0)
		{
			Say(c, "Boss, I'm so sorry©q©q©q©q");
			trg5 = 2;
		}
	}
	
	if (trg6==0 && trg4==0)
	{
		c = GetChar("Smith Fan");
		SetTarget(0, "char", player);
		SetTarget(1, "char", c);
		if (c>=0 && GetEnemy(c)==player && Distance(0, 1)<100)
		{
			c2 = GetChar("T-Chan");
			if (c2 >= 0)
			{
				ChangeBehavior(c, "follow", c2);
				SetTarget(0, "char", c2);
				ChangeBehavior(c, "attacktarget", 0);
			}
			Perform(c, "say", "You wanna murder me? Not that easy, somebody, come here£¡£¡");
			Perform(c, "pause", 6);
			Perform(c, "say", "Who are you£¡£¡");
			Perform(c, "faceto", player);

			PlayerPerform("block", 0);		
			PlayerPerform("say", "Run?£¡£¡");
			PlayerPerform("pause", 6);			
			PlayerPerform("say", "©q©q©qDon't care about my name, care about your life£¡");
			PlayerPerform("pause", 3);	
			PlayerPerform("block", 1);		
			
			PauseAll(10, player);
			
			trg6 = 1;
		}
	}
	
	if (trg6 == 1)
	{
		c = GetChar("Smith Fan");
		c2 = GetChar("T-Chan");
		if (c>=0 && c2>=0)
		{
			SetTarget(0, "char", c);
			SetTarget(1, "char", c2);
			if (Distance(0, 1) < 120)
			{
				Perform(c2, "say", "Kill him£¡£¡");
				Perform(c2, "aggress");
				Perform(c2, "faceto", player);
				Perform(c2, "say", "Yes£¡£¡");
				Perform(c2, "pause", 8);
				Perform(c2, "say", "Boss, please go away, I deal with it£¡");
				Perform(c2, "say", "Killer, go catch the killer£¡£¡");
				Perform(c2, "faceto", c);
				
				SetTarget(0, "waypoint", 73);
				ChangeBehavior(c, "attacktarget", 0);
				Perform(c, "say", "Get to know who is his boss before he dies£¡");
				Perform(c, "say", "All right, I leave here for a while£¡");
				Perform(c, "pause", 4);
				Perform(c, "faceto", c2);
				
				PlayerPerform("say", "©q©q©q©q©q©q©q");
				PlayerPerform("pause", 3);
			
				c3 = GetChar("Unnamed Killer");
				if (c3 >= 0)
				{
					ChangeBehavior(c3, "follow", player);
				}
				c3 = GetChar("Maskedman A");
				if (c3 >= 0)
				{
					ChangeBehavior(c3, "follow", player);
				}
				c3 = GetChar("Maskedman B");
				if (c3 >= 0)
				{
					ChangeBehavior(c3, "follow", player);
				}
							
				trg6 = 2;
				timer0 = GetGameTime() + 6;
			}
		}
	}
	if (trg6==2 && GetGameTime()>timer0)
	{
		c = GetAnyChar("Smith Fan");
		if (c >= 0)
		{
			RemoveNPC(c);
		}
		trg6 = 3;
	}	
	
	if (trg7 == 0)	
	{
		c = GetAnyChar("Spear Guard");
		c2 = GetAnyChar("Blade Guard");		
		c3 = GetAnyChar("Playmonk A");
		c4 = GetAnyChar("Playmonk B");
		if (c>=0 && GetHP(c)<=0 && c2>=0 && GetHP(c2)<=0 && c3>=0 && GetHP(c3)<=0 && c4>=0 && GetHP(c4)<=0)
		{
			RemoveNPC(c);
			RemoveNPC(c2);
			RemoveNPC(c3);
			RemoveNPC(c4);			
			trg7 = 1;
			timer1 = GetGameTime() + 5;
		}
	}
	if (trg7 == 1)
	{
		if (GetGameTime() > timer1)
		{
			AddNPC("npc05_03");
			AddNPC("npc05_04");
			trg7 = 2;
		}
	}
}