//@unloadnpc match
//@unloadnpc WALL#0
//@loadnpc npc/amatsu/features/partywar.txt

-	script	match	-1,{
	function wall; function ChkParty; function party_has_duplicate_job; function EjectParty;
		
	OnInit:
		set .GMAccess,1;
		set .match,0;
		set .arena_map$,"guild_vs2";	
		setarray .mtypes$[0],"1 vs 1","Party War";
		setarray .mtypes[0],1,2;			
		set .mtype$,"1 vs 1";	
		set .mtype,1;	
		set .deadlock,1;
		set .spectator,0;
		set .dupejobs,0;
		set $@mscore1,0;
		set $@mscore2,0;
		set .Size,1;
		pvpoff .arena_map$;
		setcell(.arena_map$, 0, 0, 300, 300, cell_nochat, true);
		$@charname1$ = ""; $@charname2$ = "";
		$@party1 = ""; $@party2 = ""; 
		bindatcmd("match","match::OnMatch",0,99);
		
		// Map Flags
		setmapflag .arena_map$, mf_nodrop;
		setmapflag .arena_map$, mf_nosave;
		setmapflag .arena_map$, mf_noicewall;
		setmapflag .arena_map$, mf_nowarp;
		setmapflag .arena_map$, mf_noteleport;
		setmapflag .arena_map$, mf_nobranch;
		setmapflag .arena_map$, mf_notrade;
		setmapflag .arena_map$, mf_nomemo;
		setmapflag .arena_map$, mf_nopenalty;
		setmapflag .arena_map$, mf_partylock;
		setmapflag .arena_map$, mf_noreturn;
		setmapflag .arena_map$, mf_nochat;
		setmapflag .arena_map$, mf_novending;
		setmapflag .arena_map$, mf_gvg_castle;
		
		end;

	OnPCLoginEvent:
		if (spectator) {
			set spectator,0;
			set Hp,shp;
			set Sp,ssp;
			setoption Option_Invisible, 0;
			sc_end(SC_XMAS);
			warp slastmap$,slastx,slasty;	
		}
		end;

	OnMatch:
		mes "[^0000ffMatch Manager^000000]";
		getmapxy(.@map$, @x, @y, 0);
		/*
		if (etatus) {
			mes "Sorry, you can't be a match spectator from this map";
			close;
		}
		*/
		if (strcharinfo(PC_MAP) == .arena_map$) {
		} else {
			if (getmapflag(strcharinfo(PC_MAP), mf_nowarp)) {
				mes "Sorry, you can't be a match spectator from this map";
				close;
			}
		}
		if (.deadlock && !Hp) {
			mes "You may not use @match when you are dead.";
			close;
		}
		if (!getgmlevel() && !.spectator) {
			if (!spectator) {	
				mes "Sorry no ongoing match or spectators are not allowed.";
				close;
			}	
		}
		if (.mtype == 1) { 
			switch(select(
				 ( (getgmlevel() < .GMAccess)?":":" > ^FF0000GM Menu^000000:")+
				 ( ( 
					(strcharinfo(PC_NAME) == $@charname1$ || strcharinfo(PC_NAME) == $@charname2$)
					&& .spectator && !spectator && strcharinfo(PC_MAP) != .arena_map$ 
					)?" > ^0055FFWarp to Arena^000000:":":")+

				 ( ( 
					(strcharinfo(PC_NAME) != $@charname1$ && strcharinfo(PC_NAME) != $@charname2$)
					&& (.spectator && !spectator && strcharinfo(PC_MAP) != .arena_map$)
					)?" > ^0055FFWatch Current Match^000000:":":")+

				 ( (spectator)?" > ^0055FFLeave Arena^000000:":":")+
				 " > ^777777Close^000000"
			)) {

			case 1: 
				while(1) {
					switch(select(
					 ((getgmlevel() && !.match)?" > Match Type [ ^28bf00"+.mtype$+"^000000 ]:":":")+
					 ((getgmlevel() && !.match && $@charname1$ != "" && $@charname2$ != "" && .spectator )?" > Start Round:":":")+
					 ((getgmlevel() && .match)?" > End Round:":":")+
					 ((getgmlevel() && !.match && !.spectator && $@charname1$ != "" && $@charname2$ != "")?" > Invite Spectator:":":")+
					 ((getgmlevel() && !.match )?" > Team 1 [ ^28bf00"+$@charname1$+"^000000 ]:":":")+
					 ((getgmlevel() && !.match )?" > Team 2 [ ^28bf00"+$@charname2$+"^000000 ]:":":")+
					 ((getgmlevel() && !.match && $@charname1$ != "" && $@charname2$ != "" )?" > Recall Players:":":")+
					 ((getgmlevel() && !.match && .mtype == 2)?" > Party Size [ ^28bf00"+.Size+"^000000 ]:":":")+
					 ((getgmlevel() && !.match && .mtype == 2)?" > Allow Dupe Jobs [ "+((.dupejobs)?"^28bf00Yes^000000":"^ff0000No^000000")+" ]:":":")+
					 ((getgmlevel() && .spectator )?" > End Tournament:":":")+
					 " > ^777777Close^000000"
					)) {
					case 1: 
						.@menu$ = "";
						for( .@i = 0; .@i < getarraysize(.mtypes$); .@i++ )
							.@menu$ = .@menu$ + "^FF0000[ "+( .@i+1 )+". ]^0000FF "+.mtypes$[.@i]+":";
						.@select = select( .@menu$ ) - 1;
						.mtype$ = .mtypes$[.@select];
						.mtype = .mtypes[.@select];
						break;
					case 2:
						/* Start Round */
						set .match,1;
						initnpctimer;
						setcell .arena_map$,25,53,25,46,cell_walkable,0;
						setcell .arena_map$,74,46,74,53,cell_walkable,0;
						announce "[Match Manager] The Battle between [ "+$@charname1$+" ] and [ "+$@charname2$+" ] has engaged.  You can watch this match using @match.",bc_all;
						dispbottom "PvP Match at "+.arena_map$+" has started";
						pvpoff .arena_map$;
						setnpctimer 0;
						startnpctimer;		
						break;
					case 3: /* End Round */
						set .match,0;
						stopnpctimer;
						playbgmall("07",.arena_map$);
						pvpoff .arena_map$;	
						wall(1);
						if (isloggedin(getcharid(CHAR_ID_ACCOUNT,$@charname1$))) {
							warpchar("prontera",142,93,getcharid(CHAR_ID_CHAR,$@charname1$));
						}
						if (isloggedin(getcharid(CHAR_ID_ACCOUNT,$@charname2$))) {
							warpchar("prontera",142,93,getcharid(CHAR_ID_CHAR,$@charname2$));
						}
						mes "Select Round Winner:";
						next;
						switch(select(
						 " > [ ^28bf00"+$@charname1$+"^000000 ]:"+
						 " > [ ^28bf00"+$@charname2$+"^000000 ]:"+
						 " > [ ^28bf00Draw^000000 ]:"+
						 " > ^777777Close^000000"
						)) {
						case 1:
							announce "The "+.mtype$+" battle between [ "+$@charname1$+" ] and [ "+$@charname2$+" ] has ended.   The winner of this round is [ "+$@charname1$+" ]. Next round will commence in a few moments. To watch the match use @match.",bc_all;
							$@mscore1++;
							break;
						case 2:
							announce "The "+.mtype$+" battle between [ "+$@charname1$+" ] and [ "+$@charname2$+" ] has ended.   The winner of this round is [ "+$@charname2$+" ]. Next round will commence in a few moments. To watch the match use @match.",bc_all;
							$@mscore2++;
							break;
						case 3:
							announce "The "+.mtype$+" battle between [ "+$@charname1$+" ] and [ "+$@charname2$+" ] has ended.   This round is a draw. To watch the match use @match.",bc_all;
							break;
						default:
							break;
						}
						break;
					case 4:	/* Allow Spectator */
						wall(1);
						if (!.spectator) { 
							set .spectator,1; 
							pvpoff .arena_map$;
							announce "The "+.mtype$+" between [ "+$@charname1$+" ] and [ "+$@charname2$+" ] is about to start.  You can watch this match using @match.",bc_all;
						} else { 
							set .spectator,0;
							pvpoff .arena_map$;
						}
						break;

					case 5:	/* Register Player 1 */
						if (.mtype == 1){
							.@player_name$ = callfunc("F_WhichChar");
							if ( .@player_name$ != "") {
								query_sql( "SELECT `account_id`,`char_id`,`online` FROM `char` WHERE `name` = '"+escape_sql( .@player_name$ )+"' LIMIT 1",.@player_aid,.@player_cid,.@online );
								if (isloggedin(.@player_aid,.@player_cid)) {
									if (getvariableofpc(spectator, @player_aid)) {
										dispbottom "Sorry, [ "+.@player_name$+" is in spectator mode. Ask him to leave spectator mode.";
									} else {
										$@charname1$ = .@player_name$;
										dispbottom "Invitation was sent to "+$@charname1$;
									}
								} else {
									dispbottom "Sorry, I can't find that player online.";
									$@charname1$ = "";
								}
							}
						} else {
							input $@charname1$;
							query_sql( "SELECT `party_id`, `name` FROM `party` WHERE `name` = '"+escape_sql($@charname1$)+"'", $@party1, $@charname1$ );
							if (!$@party1){
								dispbottom("Sorry, I can't find that party");
								set $@charname1$,"";
							}
							break;
						}
						break;
					case 6:	/* Register Player 2 */
						if (.mtype == 1){
							.@player_name$ = callfunc("F_WhichChar");
							if ( .@player_name$ != "") {
								query_sql( "SELECT `account_id`,`char_id`,`online` FROM `char` WHERE `name` = '"+escape_sql( .@player_name$ )+"' LIMIT 1",.@player_aid,.@player_cid,.@online );
								if (isloggedin(.@player_aid,.@player_cid)) {
									if (getvariableofpc(spectator, @player_aid)) {
										dispbottom "Sorry, [ "+.@player_name$+" is in spectator mode. Ask him to leave spectator mode.";
									} else {
										$@charname2$ = .@player_name$;
										dispbottom "Invitation was sent to "+$@charname2$;
									}
								} else {
									dispbottom "Sorry, I can't find that player online.";
									$@charname2$ = "";
								}
							}
						} else {
								input $@charname2$;
							query_sql( "SELECT `party_id`, `name` FROM `party` WHERE `name` = '"+escape_sql($@charname2$)+"'", $@party2, $@charname2$ );
							if (!$@party2){
								dispbottom("Sorry, I can't find that party");
								set $@charname2$,"";
							}
							break;
						}	
						break;
					case 7: /* Recall */
						while(1){
							switch(select(
							 " > Recall [ ^28bf00"+$@charname1$+"^000000 ]:"+
							 " > Recall [ ^28bf00"+$@charname2$+"^000000 ]"
							)) {
							case 1:
								atcommand(sprintf("#warp \"%s\" %s 79 50", $@charname1$, .arena_map$));
								break;
							case 2:
								atcommand(sprintf("#warp \"%s\" %s 22 50", $@charname2$, .arena_map$));
								break;
							}
						}
						break;
					case 8:  /* Party Size */
						input .Size,1; break;
					case 9: /* Allow dupes? */
						if (!.dupejobs) { set .dupejobs,1; } else { set .dupejobs,0;} break;
					case 10: /* End Tournament */
						wall(1);
						if (.mtype == 1 ){
							warpchar("prontera",142,93,getcharid(CHAR_ID_CHAR,$@charname1$));
							warpchar("prontera",142,93,getcharid(CHAR_ID_CHAR,$@charname2$));
						} else if (.mtype == 2) {
							EjectParty($@party1);
							EjectParty($@party2);
						}

						/* Announce Winner */
						if($@mscore1 > $@mscore2){
							announce "The "+.mtype$+" battle between [ "+$@charname1$+" ] and [ "+$@charname2$+" ] has ended.   The winner of this match is [ "+$@charname1$+" ].",bc_all;
						}
						if($@mscore1 < $@mscore2){
							announce "The "+.mtype$+" battle between [ "+$@charname1$+" ] and [ "+$@charname2$+" ] has ended.   The winner of this match is [ "+$@charname2$+" ].",bc_all;
						}
						if($@mscore1 == $@mscore2){
							announce "The "+.mtype$+" battle between [ "+$@charname1$+" ] and [ "+$@charname2$+" ] has ended.   This match is a DRAW.",bc_all;
						}
						mapannounce .arena_map$,"Please use @match to leave the arena.",bc_all;
						set $@charname1$,"";
						set $@charname2$,"";
						set $@party1,0;
						set $@party2,0;
						set $@mscore1,0;
						set $@mscore2,0;
						set .spectator,0; 
						set .match,0;
						break;
					default:
						close;
					}
						
				}
			case 2:
				mes "Please wait until the GM recall's you to the arena.";
				close;
				
				if (.mtype == 1 ) {
					sc_end(SC_ALL);
					if (strcharinfo(PC_NAME) == $@charname1$ ) {
						if(hascashmount()) setcashmount();
						warp .arena_map$,22,50;
					} else if (strcharinfo(PC_NAME) == $@charname2$) {
						if(hascashmount()) setcashmount();	
						warp .arena_map$,79,50;				
					}
				}	
				else if (.mtype == 2) {
					ChkParty(getcharid(CHAR_ID_PARTY));
				}
				close;				
			case 3:
				getmapxy(.@map$, @x, @y, 0);
				set slastmap$,.@map$;
				set slastx,@x;
				set slasty,@y;
				set spectator,1;
				set shp,Hp;
				set ssp,Sp;
				setoption Option_Invisible, 1;
				sc_start SC_XMAS, 1600000, 0;
				warp .arena_map$,49,49;
				close;
			case 4:
				set spectator,0;
				set Hp,shp;
				set Sp,ssp;
				setoption Option_Invisible, 0;
				sc_end(SC_XMAS);
				warp slastmap$,slastx,slasty;
				close;
				default:
				close;
			}
		}

		if (.mtype == 2) { 
			switch(select(
				 ( (getgmlevel() < .GMAccess)?":":" > ^FF0000GM Menu^000000:")+

				 ( ( 
					(getcharid(CHAR_ID_PARTY) == $@party1 || getcharid(CHAR_ID_PARTY) == $@party2)
					&& .spectator && !spectator && strcharinfo(PC_MAP) != .arena_map$ 
					)?" > ^0055FFWarp to Arena^000000:":":")+
				 ( ( 
					(getcharid(CHAR_ID_PARTY) != $@party1 && getcharid(CHAR_ID_PARTY) != $@party2)
					&& .spectator && !spectator && strcharinfo(PC_MAP) != .arena_map$ 
					)?" > ^0055FFWatch Match^000000:":":")+

				 ( (spectator)?" > ^0055FFLeave PvP Match^000000:":":")+
				 " > ^777777Close^000000"
			)) {

			case 1: 
				while(1) {
					switch(select(
					 ((getgmlevel() && !.match)?" > Match Type [ ^28bf00"+.mtype$+"^000000 ]:":":")+
					 ((getgmlevel() && !.match && $@charname1$ != "" && $@charname2$ != "" && .spectator )?" > Start Round:":":")+
					 ((getgmlevel() && .match)?" > End Round:":":")+
					 ((getgmlevel() && !.match && !.spectator && $@charname1$ != "" && $@charname2$ != "")?" > Invite Spectator:":":")+
					 ((getgmlevel() && !.match && .mtype == 1)?" > Player 1 [ ^28bf00"+$@charname1$+"^000000 ]:":":")+
					 ((getgmlevel() && !.match && .mtype == 1)?" > Player 2 [ ^28bf00"+$@charname2$+"^000000 ]:":":")+

					 ((getgmlevel() && !.match && .mtype == 2)?" > Party 1 [ ^28bf00"+$@charname1$+"^000000 ]:":":")+
					 ((getgmlevel() && !.match && .mtype == 2)?" > Party 2 [ ^28bf00"+$@charname2$+"^000000 ]:":":")+
					 ((getgmlevel() && !.match && .mtype == 2)?" > Party Size [ ^28bf00"+.Size+"^000000 ]:":":")+
					 ((getgmlevel() && !.match && .mtype == 2)?" > Allow Dupe Jobs [ "+((.dupejobs)?"^28bf00Yes^000000":"^ff0000No^000000")+" ]:":":")+
					 ((getgmlevel() && .spectator )?" > End Tournament:":":")+
					 " > ^777777Close^000000"
					)) {
					case 1: 
						.@menu$ = "";
						for( .@i = 0; .@i < getarraysize(.mtypes$); .@i++ )
							.@menu$ = .@menu$ + "^FF0000[ "+( .@i+1 )+". ]^0000FF "+.mtypes$[.@i]+":";
						.@select = select( .@menu$ ) - 1;
						.mtype$ = .mtypes$[.@select];
						.mtype = .mtypes[.@select];
						break;
					case 2:
						/* Start Round */
						set .match,1;
						initnpctimer;
						setcell .arena_map$,25,53,25,46,cell_walkable,0;
						setcell .arena_map$,74,46,74,53,cell_walkable,0;
						announce "[Match Manager] The Battle between [ "+$@charname1$+" ] and [ "+$@charname2$+" ] has engaged.  You can watch this match using @match.",bc_all;
						dispbottom "PvP Match at "+.arena_map$+" has started";
						pvpoff .arena_map$;
						setnpctimer 0;
						startnpctimer;		
						break;
					case 3: /* End Round */
						set .match,0;
						stopnpctimer;
						playbgmall("07",.arena_map$);
						pvpoff .arena_map$;	
						wall(1);
						EjectParty($@party1);
						EjectParty($@party2);
						mapannounce .arena_map$,"[Match Manager] Round has ended.   Next round will commence on battle between [ "+$@charname1$+" ] and [ "+$@charname2$+" ] in a few moments.",bc_all;
						break;
					case 4:	/* Allow Spectator */
						wall(1);
						if (!.spectator) { 
							set .spectator,1; 
							pvpoff .arena_map$;
							announce "The "+.mtype$+" between [ "+$@charname1$+" ] and [ "+$@charname2$+" ] is about to start.  You can watch this match using @match.",bc_all;
						} else { 
							set .spectator,0;
							pvpoff .arena_map$;
						}
						break;

					case 5:	/* Register Player 1 */
						input $@charname1$;
						query_sql "SELECT `char_id`, `account_id` FROM `char` WHERE `name` = '" + $@charname1$ + "'",.@char_id,.@account_id;
						if (isloggedin(.@account_id,.@char_id)) {
							if (getvariableofpc(spectator, getcharid(CHAR_ID_ACCOUNT, $@charname1$))) {
								dispbottom "Sorry, [ "+$@charname1$+" is in spectator mode. Ask him to leave spectator mode.";
								set $@charname1$,"";
							} else {
								dispbottom "Invitation was sent to "+$@charname1$;
							}
						}else{
							dispbottom "Sorry, I can't find that player online.";
							set $@charname1$,"";
						}
						break;
					case 6:	/* Register Player 2 */
						input $@charname2$;
						query_sql "SELECT `char_id`, `account_id` FROM `char` WHERE `name` = '" + $@charname2$ + "'",.@char_id,.@account_id;
						if (isloggedin(.@account_id,.@char_id)) { 
							if (getvariableofpc(spectator, getcharid(CHAR_ID_ACCOUNT, $@charname2$))) {
								dispbottom "Sorry, [ "+$@charname2$+" is in spectator mode. Ask him to leave spectator mode.";
								set $@charname2$,"";
							} else {
									dispbottom "Invitation was sent to "+$@charname2$;
							}
						}else{
							dispbottom "Sorry, I can't find that player online.";
							set $@charname2$,"";
						}
						break;
					case 7:  /* Register Party 1 */
						input $@charname1$;
						query_sql( "SELECT `party_id`, `name` FROM `party` WHERE `name` = '"+escape_sql($@charname1$)+"'", $@party1, $@charname1$ );
						if (!$@party1){
							dispbottom("Sorry, I can't find that party");
							set $@charname1$,"";
						}
						break;
					case 8:  /* Register Party 2 */
						input $@charname2$;
						query_sql( "SELECT `party_id`, `name` FROM `party` WHERE `name` = '"+escape_sql($@charname2$)+"'", $@party2, $@charname2$ );
						if (!$@party2){
							dispbottom("Sorry, I can't find that party");
							set $@charname2$,"";
						}
						break;
					case 9:  /* Party Size */
						input .Size,1; break;
					case 10: /* Allow dupes? */
						if (!.dupejobs) { set .dupejobs,1; } else { set .dupejobs,0;} break;
					case 11: /* End Tournament */
						wall(1);
						if (.mtype == 1 ){
							warpchar("prontera",142,93,getcharid(CHAR_ID_CHAR,$@charname1$));
							warpchar("prontera",142,93,getcharid(CHAR_ID_CHAR,$@charname2$));
						} else if (.mtype == 2) {
							EjectParty($@party1);
							EjectParty($@party2);
						}
						announce "The "+.mtype$+" between [ "+$@charname1$+" ] and [ "+$@charname2$+" ] has ended.",bc_all;
						mapannounce .arena_map$,"Please use @match to leave the arena.",bc_all;	
						set $@charname1$,"";
						set $@charname2$,"";
						set $@party1,0;
						set $@party2,0;
						set .spectator,0; 
						set .match,0;
						break;
					default:
						close;
					}
						
				}
			case 2:
				mes "Please wait until the GM recall's your party to the arena.";
				close;
				/*
				if (.mtype == 1 ) {
					sc_end(SC_ALL);
					if (strcharinfo(PC_NAME) == $@charname1$ ) {
						if(hascashmount()) setcashmount();
						warp .arena_map$,22,50;
					} else if (strcharinfo(PC_NAME) == $@charname2$) {
						if(hascashmount()) setcashmount();	
						warp .arena_map$,79,50;				
					}
				}	
				else if (.mtype == 2) {
					ChkParty(getcharid(CHAR_ID_PARTY));
				}
				close;	
				*/			
			case 3:
				getmapxy(.@map$, @x, @y, 0);
				set slastmap$,.@map$;
				set slastx,@x;
				set slasty,@y;
				set spectator,1;
				set shp,Hp;
				set ssp,Sp;
				setoption Option_Invisible, 1;
				sc_start SC_XMAS, 1600000, 0;
				warp .arena_map$,49,49;
				close;
			case 4:
				set spectator,0;
				set Hp,shp;
				set Sp,ssp;
				setoption Option_Invisible, 0;
				sc_end(SC_XMAS);
				warp slastmap$,slastx,slasty;
				close;
				default:
				close;
			}
		}
	end;
	
	OnTimer0001:
		playbgmall("118",.arena_map$);
		end;
	OnTimer5000:
		mapannounce .arena_map$,"The [ "+.mtype$+" ] battle will begin in 5 seconds.",bc_all;
		end;
	OnTimer6000:
		mapannounce .arena_map$,"The [ "+.mtype$+" ] battle will begin in 4 seconds.",bc_all;
		end;
	OnTimer7000:
		mapannounce .arena_map$,"The [ "+.mtype$+" ] battle will begin in 3 seconds.",bc_all;
		end;
	OnTimer8000:
		mapannounce .arena_map$,"The [ "+.mtype$+" ] battle will begin in 2 seconds.",bc_all;
		end;
	OnTimer9000:
		mapannounce .arena_map$,"The [ "+.mtype$+" ] battle will begin in 1 second.",bc_all;
		end;
	OnTimer10000:
		//soundeffectall "6_Startgame.wav", 0,.arena_map$,0,0,100,100;
		pvpon .arena_map$;
		wall(2);
		mapannounce .arena_map$,"The [ "+.mtype$+" ] battle has begun .  Goodluck!!!.",bc_all;
		end;

	function ChkParty {
		// get the charID and accountID of character's party members
		getpartymember(getarg(0), 1);
		getpartymember(getarg(0), 2);

		if ($@partymembercount != .Size) {
			mes "[^0000ffMatch Manager^000000]";	
			mes("Please form a party of "+ .Size +" to continue");
			close();
		}

		// loop through both and use 'isloggedin' to count online party members			
		for (.@i = 0; .@i < $@partymembercount; ++.@i)
			if (isloggedin($@partymemberaid[.@i], $@partymembercid[.@i]))
				.@count_online++;

		// We search accountID & charID because a single party can have
		// multiple characters from the same account. Without searching
		// through the charID, if a player has 2 characters from the same
		// account inside the party but only 1 char online, it would count
		// their online char twice.

		if (.@count_online != .Size) {
			mes "[^0000ffMatch Manager^000000]";
			mes("All your party members must be online to continue");
			close();
		}

		// copy the array to prevent players cheating the system
		copyarray(.@partymembercid, $@partymembercid, .Size);

		getpartymember(getarg(0), 1);
		if ($@partymembercount != .Size) {
			mes "[^0000ffMatch Manager^000000]";
			mes("You've made changes to your party !");
			close();
		}

		for (.@i = 0; .@i < $@partymembercount; ++.@i) {
			if (.@partymembercid[.@i] != $@partymembercid[.@i]) {
				mes "[^0000ffMatch Manager^000000]";
				mes("You've made changes to your party !");
				close();
			}
		}

		// Check for player that is on spectator mode
		copyarray(.@partymemberaid, $@partymemberaid, .Size);
		for (.@i = 0; .@i < $@partymembercount; ++.@i) {
			if (getvariableofpc(spectator, .@partymemberaid[.@i])) {
				mes "[^0000ffMatch Manager^000000]";	
				mes("Sorry, some of your party member is still in spectator mode. Ask him to leave spectator mode.");
				close();
			}
		}
		// Check for dupes
		if(!.dupejobs && party_has_duplicate_job(getarg(0))){
			mes "[^0000ffMatch Manager^000000]";
			mes "It seems that someone in the party has the same class than another member. Please check the requirements...";
			close();
		}

		// Warp Party Members
		getpartymember(getarg(0), 2);
		set .@partymembercount, $@partymembercount;
       		copyarray .@partymemberaid[0], $@partymemberaid[0], .@partymembercount;
      		for (set .@i, 0; .@i < .@partymembercount; .@i ++) {
              		if (attachrid(.@partymemberaid[.@i])) {
				sc_end SC_ALL;
				if (getcharid(CHAR_ID_PARTY) == $@party1 ) {	
					warp .arena_map$,22,50;
				} else if (getcharid(CHAR_ID_PARTY) == $@party2) {
					warp .arena_map$,79,50;	
				}
               		}
       		}
		
	}

	function EjectParty {
		getpartymember(getarg(0), 1);
		set .@partymembercount, $@partymembercount;
       		copyarray .@partymembercid[0], $@partymembercid[0], .@partymembercount;
      		for (set .@i, 0; .@i < .@partymembercount; .@i ++) {
 			warpchar("prontera", 142, 93, .@partymembercid[.@i]);	
       		}
		return;
	}

	function wall {
		if (getarg(0)  == 1) {
			setcell .arena_map$,25,53,25,46,cell_walkable,0;
			setcell .arena_map$,74,46,74,53,cell_walkable,0;
			enablenpc "WALL#1";	
			enablenpc "WALL#2";
			enablenpc "WALL#3";
			enablenpc "WALL#4";
			enablenpc "WALL#5";
			enablenpc "WALL#6";
			enablenpc "WALL#7";
			enablenpc "WALL#8";
			enablenpc "WALL#9";
			enablenpc "WALL#10";
			enablenpc "WALL#11";
			enablenpc "WALL#12";
			enablenpc "WALL#13";
			enablenpc "WALL#14";
			enablenpc "WALL#15";
			enablenpc "WALL#0";
		} else if(getarg(0) == 2) {
			setcell .arena_map$,25,53,25,46,cell_walkable,1;
			setcell .arena_map$,74,46,74,53,cell_walkable,1;
			disablenpc "WALL#1";
			disablenpc "WALL#2";
			disablenpc "WALL#3";
			disablenpc "WALL#4";
			disablenpc "WALL#5";
			disablenpc "WALL#6";
			disablenpc "WALL#7";
			disablenpc "WALL#8";
			disablenpc "WALL#9";
			disablenpc "WALL#10";
			disablenpc "WALL#11";
			disablenpc "WALL#12";
			disablenpc "WALL#13";
			disablenpc "WALL#14";
			disablenpc "WALL#15";
			disablenpc "WALL#0";
		}
		return;	
	}

	function	party_has_duplicate_job	{
    		set .@party_id, getarg( 0 );

    		// Need to have a party.
   		if ( !.@party_id ) {
            		return -1;
    		}

    		// Loading party members variables
    		getpartymember .@party_id, 1;
   		getpartymember .@party_id, 2;

    		// Keep rid attached.
    		set .@rid, playerattached();


    		// Check all members
    		for ( set .@i,0; .@i<$@partymembercount; set .@i, .@i+1 ) {

       			// Online user
        		if ( isloggedin( $@partymemberaid[.@i], $@partymembercid[.@i]) ) {
            			// Attach the member to access "Class"
           			attachrid( $@partymemberaid[.@i] );
				//savepoint $mapSave$, $@mapSaveCordX, $@mapSaveCordY;
            			if ( compare( .@tmp_class$ + "|", "|" + Class + "|" ) ) {
                			if ( .@rid )
                    				attachrid(.@rid);
                			else
                    				detachrid;
                				return 1;
           			 }
            			set .@tmp_class$, .@tmp_class$ + "|" + Class;
        		}

        		// Offline user (use sql)
       			else {
            			set .@sql$, .@sql$ + ( .@sql_i ? "OR " : "" ) + "`char_id`='" + $@partymembercid[.@i] + "' ";
            			set .@sql_i, .@sql_i + 1;
        		}
    		}

    		// SQL for offline users
    		if ( getstrlen(.@sql$) ) {
        		// get class from offline members
        		set .@count, query_sql("SELECT `class` FROM `char` WHERE " + .@sql$, .@class );

       			// Check the class.
        		for ( set .@i, 0; .@i<.@count; set .@i, .@i+1 ) {
            			if ( compare( .@tmp_class$ + "|", "|" + .@class[.@i] + "|" ) ) {
                			if ( .@rid )
                    				attachrid(.@rid);
                			else
                    				detachrid;
               				return 1;
            			}
            			set .@tmp_class$, .@tmp_class$ + "|" + .@class[.@i];
        		}
    		}

    		// Restore RID.
    		if ( .@rid )
        		attachrid(.@rid);
   		else
        		detachrid;
   		 return 0;
	}
}

guild_vs2,25,53,2	script	WALL#0	8W_SOLDIER,{
	end;		
}


function	script	F_WhichChar	{
	__OnInput:
		while( input( .@player_name$,3,24 ) );
		query_sql( "SELECT `name`,`class` FROM `char` WHERE `name` LIKE '%"+escape_sql( .@player_name$ )+"%' ORDER BY `name` LIMIT 50",.@temp_name$,.@class );
		.@temp_name_size = getarraysize( .@temp_name$ );
		
		if ( .@temp_name_size <= 0 ) {
			dispbottom "[ System ] '"+.@player_name$+"' doesnt exist.";
			// next;
			if( select( "Search another Name","Cancel" ) == 1 ) goto __OnInput;
			return "";
		}
		else if ( .@temp_name_size > 1 ) {
			dispbottom "[ System ] Pick a player.";
			for ( .@i = 0; .@i < .@temp_name_size; .@i++ )
				.@menu$ = .@menu$ + "^FF0000["+jobname( .@class[.@i] )+"] ^000000" + .@temp_name$[.@i] +":";
			.@i = select( .@menu$ ) - 1;
		}
	return .@temp_name$[.@i];
}

	// Team A	
guild_vs2,25,52,2	duplicate(WALL#0)	WALL#1	8W_SOLDIER
guild_vs2,25,51,2	duplicate(WALL#0)	WALL#2	8W_SOLDIER
guild_vs2,25,50,2	duplicate(WALL#0)	WALL#3	8W_SOLDIER
guild_vs2,25,49,2	duplicate(WALL#0)	WALL#4	8W_SOLDIER
guild_vs2,25,48,2	duplicate(WALL#0)	WALL#5	8W_SOLDIER
guild_vs2,25,47,2	duplicate(WALL#0)	WALL#6	8W_SOLDIER
guild_vs2,25,46,2	duplicate(WALL#0)	WALL#7	8W_SOLDIER

	// Team B
guild_vs2,75,46,6	duplicate(WALL#0)	WALL#8	8W_SOLDIER
guild_vs2,75,47,6	duplicate(WALL#0)	WALL#9	8W_SOLDIER
guild_vs2,75,48,6	duplicate(WALL#0)	WALL#10	8W_SOLDIER
guild_vs2,75,49,6	duplicate(WALL#0)	WALL#11	8W_SOLDIER
guild_vs2,75,50,6	duplicate(WALL#0)	WALL#12	8W_SOLDIER
guild_vs2,75,51,6	duplicate(WALL#0)	WALL#13	8W_SOLDIER
guild_vs2,75,52,6	duplicate(WALL#0)	WALL#14	8W_SOLDIER
guild_vs2,75,53,6	duplicate(WALL#0)	WALL#15	8W_SOLDIER
