-	script	bg_pvp_round#control	-1,{
OnInit:
	.minplayer2start = 1; // number of players to start this event automatically
	.winningscore = 2; // how many rounds to win
	.eventlasting = 20*60; // event last 20 minutes or the system abort itself
	.registerlasting = 5*60; // 5 minutes to join
	setarray .rewardwin, 501,10, 502,5, 503,3; // reward to winning team
	setarray .rewardlose, 501,5, 502,3, 503,1; // reward to losing team

	.rewardwinsize = getarraysize( .rewardwin );
	.rewardlosesize = getarraysize( .rewardlose );
	.teamname$[1] = "Red";
	.teamname$[2] = "Blue";
	bindatcmd "bgpvp", strnpcinfo(0)+"::OnCommand", 99, 100;
	end;
OnCommand:
	if ( compare( .@atcmd_parameters$, "on" ) ) {
		if ( .start )
			dispbottom "Battle Ground DeathMatch System already started.";
		else
			goto L_Start;
	}
	else if ( compare( .@atcmd_parameters$, "off" ) ) {
		if ( !.start )
			dispbottom "Battle Ground DeathMatch System not yet start.";
		else {
			.gmname$ = strcharinfo(0);
			.start = 0;
			.skip = 1;
			awake strnpcinfo(0);
			end;
		}
	}
	else if ( !.start )
		dispbottom "Battle Ground DeathMatch System is currently not running.";
	dispbottom "Use "+ .@atcmd_command$ +" on to start the event.";
	dispbottom "Use "+ .@atcmd_command$ +" off to abort this event";
	end;
OnStart:
	if ( getwaitingroomstate( 0, .rednpcname$ ) < .minplayer2start || getwaitingroomstate( 0, .bluenpcname$ ) < .minplayer2start )
		announce "[ Battle Ground DeathMatch System ]  Blue Team : "+ getwaitingroomstate( 0, .bluenpcname$ ) + "/" + .minplayer2start + ". Red Team : "+ getwaitingroomstate( 0, .rednpcname$ ) + "/" + .minplayer2start, bc_all, 0x00CED1;
	else
		awake strnpcinfo(0);
	end;
OnClock0000:
OnClock0200:
OnClock0400:
OnClock0600:
OnClock0800:
OnClock1000:
OnClock1200:
OnClock1400:
OnClock1600:
OnClock1800:
OnClock2000:
OnClock2200:
L_Start:
	if ( .start ) end;
	announce "[ Battle Ground DeathMatch System ] Event is now Open ! Come to prontera to join !", bc_all, 0x00CED1;
	enablenpc .rednpcname$;
	enablenpc .bluenpcname$;
	donpcevent .rednpcname$ +"::OnStart";
	donpcevent .bluenpcname$ +"::OnStart";
	.start = 1;
	sleep .registerlasting * 1000;
	if ( !.start ) {
		delwaitingroom .rednpcname$;
		delwaitingroom .bluenpcname$;
		disablenpc .rednpcname$;
		disablenpc .bluenpcname$;
		announce "[ Battle Ground DeathMatch System ] "+ .gmname$ +" has aborted this Event !", bc_all, 0x00CED1;
		.skip = 0;
		end;
	}
	else if ( !getwaitingroomstate( 0, .rednpcname$ ) || !getwaitingroomstate( 0, .bluenpcname$ ) ) {
		delwaitingroom .rednpcname$;
		delwaitingroom .bluenpcname$;
		disablenpc .rednpcname$;
		disablenpc .bluenpcname$;
		announce "[ Battle Ground DeathMatch System ] One side has 0 player. Can not start the Event.", bc_all, 0x00CED1;
		.start = .skip = 0;
		end;
	}
	.start = 2;
	announce "[ Battle Ground DeathMatch System ]  Battleground has started", bc_all, 0x00CED1;
	.red = waitingroom2bg( "bat_b01", 61,150, strnpcinfo(0)+"::OnRedQuit", strnpcinfo(0)+"::OnRedDead", .rednpcname$ );
	copyarray .team1aid, $@arenamembers, $@arenamembersnum;
	.team1count = .life[1] = $@arenamembersnum;
	.blue = waitingroom2bg( "bat_b01", 327,150, strnpcinfo(0)+"::OnBlueQuit", strnpcinfo(0)+"::OnBlueDead", .bluenpcname$ );
	copyarray .team2aid, $@arenamembers, $@arenamembersnum;
	.team2count = .life[2] = $@arenamembersnum;
	delwaitingroom .rednpcname$;
	delwaitingroom .bluenpcname$;
	disablenpc .rednpcname$;
	disablenpc .bluenpcname$;
	callsub L_setwall;
	bg_updatescore "bat_b01", 0, 0;
	bg_warp .red, "bat_b01", 61,150;
	bg_warp .blue, "bat_b01", 327,150;
	sleep 4000 * !.skip;
	mapannounce "bat_b01", "Kill all opponent team to gain 1 point, score "+ .winningscore +" points to win !", 0;
	sleep 2000 * !.skip;
	while (1) {
		for ( .@i = 5; .@i > 0; .@i-- ) {
			mapannounce "bat_b01", "["+ .@i +"]", 0;
			sleep 1000 * !.skip;
		}
		if ( .score[1] == .winningscore -1 && .score[2] == .winningscore -1 )
			mapannounce "bat_b01", "Final Round start!", 0;
		else
			mapannounce "bat_b01", "Round "+ .round++ +" start!", 0;
		callsub L_delwall;
		.life[1] = .team1count;
		.life[2] = .team2count;
		sleep .eventlasting * 1000 * !.skip;
		bg_updatescore "bat_b01", .score[1], .score[2];
		if ( .score[1] == .winningscore || .score[2] == .winningscore || !.winside ) break;
		.winside = 0;
		sleep 5000 * !.skip;
		callsub L_make_player_move;
		bg_warp .red, "bat_b01", 61,150;
		bg_warp .blue, "bat_b01", 327,150;
		callsub L_setwall;
		sleep 1000 * !.skip;
	}
	if ( .winside ) {
		mapannounce "bat_b01", " "+ .teamname$[ .winside ] +" side wins !", bc_all;
		callsub L_reward, .winside, .rewardwin, .rewardwinsize;
		callsub L_reward, ( .winside == 1 )? 2:1, .rewardlose, .rewardlosesize;
	}
	else if ( .start )
		mapannounce "bat_b01", "[ Battle Ground DeathMatch System ] Time Out. Aborting the match.", bc_all;
	else
		mapannounce "bat_b01", "[ Battle Ground DeathMatch System ] "+ .gmname$ +" has aborted this Event !", bc_all, 0x00CED1;
	sleep 5000;
	callsub L_make_player_move;
	bg_warp .red, "prontera", 155,182;
	bg_warp .blue, "prontera", 158,182;
	bg_destroy .red;
	bg_destroy .blue;
	callsub L_delwall;
	deletearray .team1aid;
	deletearray .team2aid;
	.round = .winside = .skip = .score[1] = .score[2] = .team1count = .team2count = .life[1] = .life[2] = .start = 0;
	end;
L_reward:
	.@size = getarraysize( getd(".team"+ getarg(0) +"aid") );
	for ( .@i = 0; .@i < .@size; .@i++ )
		for ( .@j = 0; .@j < getarg(2); .@j += 2 )
			getitem getelementofarray( getarg(1), .@j ), getelementofarray( getarg(1), .@j +1 ), getd(".team"+ getarg(0) +"aid["+ .@i +"]" );
	return;
L_setwall:
	setwall "bat_b01", 58,153, 6, 6, 0, "bg_pvp_round_red_1";
	setwall "bat_b01", 64,153, 6, 4, 0, "bg_pvp_round_red_2";
	setwall "bat_b01", 64,147, 6, 2, 0, "bg_pvp_round_red_3";
	setwall "bat_b01", 58,147, 6, 0, 0, "bg_pvp_round_red_4";
	setwall "bat_b01", 324,153, 6, 6, 0, "bg_pvp_round_blue_1";
	setwall "bat_b01", 330,153, 6, 4, 0, "bg_pvp_round_blue_2";
	setwall "bat_b01", 330,147, 6, 2, 0, "bg_pvp_round_blue_3";
	setwall "bat_b01", 324,147, 6, 0, 0, "bg_pvp_round_blue_4";
	return;
L_delwall:
	delwall "bg_pvp_round_red_1";
	delwall "bg_pvp_round_red_2";
	delwall "bg_pvp_round_red_3";
	delwall "bg_pvp_round_red_4";
	delwall "bg_pvp_round_blue_1";
	delwall "bg_pvp_round_blue_2";
	delwall "bg_pvp_round_blue_3";
	delwall "bg_pvp_round_blue_4";
	return;
L_make_player_move:
	for ( .@j = 1; .@j <= 2; .@j++ ) {
		for ( .@i = 0; .@i < getd(".team"+ .@j +"count"); .@i++ ) {
			attachrid getd(".team"+ .@j +"aid["+ .@i +"]" );
			setoption Option_Xmas | Option_Invisible, 0;
			pcblockmove getcharid(3), 0;
		}
	}
	return;
OnRedQuit: callsub L_quit, 1, 2;
OnBlueQuit: callsub L_quit, 2, 1;
L_quit:
	percentheal 100, 100;
	setoption Option_Xmas | Option_Invisible, 0;
	pcblockmove getcharid(3), 0;
	while ( getd( ".team"+ getarg(0) +"aid["+ .@i +"]" ) != getcharid(3) && .@i < getd(".team"+ getarg(0) +"count") ) .@i++;
	deletearray getd( ".team"+ getarg(0) +"aid["+ .@i +"]" ), 1;
	setd ".team"+ getarg(0) +"count", getd(".team"+ getarg(0) +"count") -1;
	.life[ getarg(0) ]--;
	if ( !getd(".team"+ getarg(0) +"count") ) {
		mapannounce "bat_b01", "All "+ .teamname$[ getarg(0) ] +" team members has Quit !", bc_all, 0xff3333; // purposely use different color
		.score[ getarg(1) ] = .winningscore;
		.winside = getarg(1);
		.skip = 1;
		awake strnpcinfo(0);
	}
	else if ( !.life[ getarg(0) ] ) {
		.winside = getarg(1);
		mapannounce "bat_b01", " "+ .teamname$[ getarg(1) ] +" team has defeated the opponent team, "+ .teamname$[ getarg(1) ] +" team score a point !", bc_all;
		.score[ getarg(1) ]++;
		awake strnpcinfo(0);
	}
	end;
OnRedDead: callsub L_dead, 1, 2;
OnBlueDead: callsub L_dead, 2, 1;
L_dead:
	.life[ getarg(0) ]--;
	if ( !.life[ getarg(0) ] ) {
		.winside = getarg(1);
		mapannounce "bat_b01", " "+ .teamname$[ getarg(1) ] +" team has defeated the opponent team, "+ .teamname$[ getarg(1) ] +" team score a point !", bc_all;
		.score[ getarg(1) ]++;
		awake strnpcinfo(0);
	}
	sleep2 1250;
	percentheal 100,100;
	if ( getcharid(4) ) {
		setoption Option_Xmas | Option_Invisible, 1;
		pcblockmove getcharid(3), 1;
	}
	end;
}

prontera,155,182,5	script	Red side	100,{
	end;
OnInit:
	sleep 1;
	set getvariableofnpc( .rednpcname$, "bg_pvp_round#control" ), strnpcinfo(0);
	disablenpc strnpcinfo(0);
	end;
OnStart:
	waitingroom "Red side", getvariableofnpc( .minplayer2start, "bg_pvp_round#control" ) +1, "bg_pvp_round#control::OnStart", 1;
	end;
}

prontera,158,182,5	script	Blue side	100,{
	end;
OnInit:
	sleep 1;
	set getvariableofnpc( .bluenpcname$, "bg_pvp_round#control" ), strnpcinfo(0);
	disablenpc strnpcinfo(0);
	end;
OnStart:
	waitingroom "Blue side", getvariableofnpc( .minplayer2start, "bg_pvp_round#control" ) +1, "bg_pvp_round#control::OnStart", 1;
	end;
}

bat_b01	mapflag	battleground	2
bat_b01	mapflag	nosave	SavePoint
bat_b01	mapflag	nowarp
bat_b01	mapflag	nowarpto
bat_b01	mapflag	noteleport
bat_b01	mapflag	nomemo
bat_b01	mapflag	nopenalty
bat_b01	mapflag	nobranch
bat_b01	mapflag	noicewall