//=========================================
//By : goddameit
//Ver : 3
//=========================================
prontera,157,185,4	script	Warp Battle Ground Vg	100,{
	warp "pvp_room",80,43;
	end;
}
//=======================================================================//
//===============================Register================================//
//=======================================================================//
pvp_room,87,38,4	script	Battleground Vg#1	100,{
end;
OnInitC:
	if(!.k){end;}
	enablenpc strnpcinfo(0);
	waitingroom "Battle Ground",getvariableofnpc(.gameplayeramount,"batleVg"),"Battleground Vg#1::OnJoin",getvariableofnpc(.gameplayeramount,"batleVg");
	end;
OnJoin:
	if(getvariableofnpc(.game,"batleVg")==3){
		mapannounce "pvp_room","[Battle Ground System] Wait for game over completely.",0;
		end;
	}
	set .k,0;
	set $bgvgbid1,waitingroom2bg("bat_b01",87,75,"","batleVg::OnGu1Die");
	warpwaitingpc "bat_b01",87,75;
	delwaitingroom;
	disablenpc strnpcinfo(0);
	set getvariableofnpc(.game,"batleVg"),getvariableofnpc(.game,"batleVg")+1;
	if(getvariableofnpc(.game,"batleVg")==1){
		donpcevent "batleVg::OnGameStart";
	}
	end;
OnPCLoadMapEvent:
	if(getcharid(4)){
		bg_leave;
		dispbottom "=======================================";
		dispbottom "================[Leave]================";
		dispbottom "=======================================";
	}
	end;
}
pvp_room,72,38,4	script	Battleground Vg#2	100,{
end;
OnInitC:
	if(!.k){end;}
	enablenpc strnpcinfo(0);
	waitingroom "Battle Ground",getvariableofnpc(.gameplayeramount,"batleVg"),"Battleground Vg#2::OnJoin",getvariableofnpc(.gameplayeramount,"batleVg");
	end;
OnJoin:
	if(getvariableofnpc(.game,"batleVg")==3){
		mapannounce "pvp_room","[Battle Ground System] Wait for game over completely.",0;
		end;
	}
	set .k,0;
	set $bgvgbid2,waitingroom2bg("bat_b01",311,224,"","batleVg::OnGu2Die");
	warpwaitingpc "bat_b01",311,224;
	delwaitingroom;
	disablenpc strnpcinfo(0);
	set getvariableofnpc(.game,"batleVg"),getvariableofnpc(.game,"batleVg")+1;
	if(getvariableofnpc(.game,"batleVg")==1){
		donpcevent "batleVg::OnGameStart";
	}
	end;
}
//=======================================================================//
//================================Set====================================//
//=======================================================================//
-	script	batleVg	-1,{
ii:	
	setarray .hpto,50,50,10000,90000,10000;	//HP of Ground 1 , HP of Ground 2 , time 1(Register) , time 2(During) , time 3(Respwan)
	if(!getvariableofnpc(.k,"Battleground Vg#2")){
		set getvariableofnpc(.k,"Battleground Vg#2"),1;
		donpcevent "Battleground Vg#2::OnInitC";
	}
	if(!getvariableofnpc(.k,"Battleground Vg#1")){
		set getvariableofnpc(.k,"Battleground Vg#1"),1;
		donpcevent "Battleground Vg#1::OnInitC";
	}
	set .game,0;
	killmonsterall "bat_b01";
	mapwarp "bat_b01","prontera",150,180;
	return;
OnInit:
	set .gameplayeramount,1;
	callsub ii;
//=======================================================================//
//===============================SetWall=================================//
//=======================================================================//
	setwall "bat_b01",327,173,5,6,1,"battleground Vg wall 1";
	setwall "bat_b01",332,172,2,6,1,"battleground Vg wall 2";
	setwall "bat_b01",244,185,3,4,1,"battleground Vg wall 3";
	setwall "bat_b01",244,182,4,6,1,"battleground Vg wall 4";
	setwall "bat_b01",67,128,10,1,1,"battleground Vg wall 5";
	setwall "bat_b01",152,118,4,6,1,"battleground Vg wall 6";
	setwall "bat_b01",156,118,5,4,1,"battleground Vg wall 7";
	disablenpc "battleground Vg warp 1";
	disablenpc "battleground Vg warp 2";
	disablenpc "battleground Vg warp 3";
	disablenpc "battleground Vg warp 4";
	end;
//=======================================================================//
//================================Control================================//
//=======================================================================//
OnGameStart:
	bg_updatescore "bat_b01",.hpto[0],.hpto[1];
	mapwarp "bat_b01","prontera",150,180;
	setarray .@k,gettime(3),gettime(2),gettime(1)+.hpto[2]/1000;
	while(.@k[2]>=60){
		if(.@k[2]>=60){
			setarray .@k[1],.@k[1]+1,.@k[2]-60;
		}
		if(.@k[1]>=60){
			setarray .@k[0],.@k[0]+1,.@k[1]-60;
		}
		if(.@k[0]>=24){
			set .@k[0],.@k[0]-24;
		}
	}
	announce "[Battle Ground System] Will Start at "+.@k[0]+":"+.@k[1]+":"+.@k[2]+" !",0;
	sleep(.hpto[2]);
OnTimerC:
	if(.game!=2){
		sleep(2000);
		if(.game){
			announce "[Battle Ground System] Game Over.",0;
		}
		callsub ii;
		end;
	}
	announce "[Battle Ground System] Go !",0;
	set .ib2,bg_monster($bgvgbid2,"bat_b01",326,149," ",1956,"batleVg::OnKiillT2");
	set .ib1,bg_monster($bgvgbid1,"bat_b01",61,149," ",1916,"batleVg::OnKiillT1");
	enablenpc "battleground Vg warp 1";
	enablenpc "battleground Vg warp 2";
	enablenpc "battleground Vg warp 3";
	enablenpc "battleground Vg warp 4";
	initnpctimer "#bgvg_heal1";
//=======================================================================//
//=================================OnEnd=================================//
//=======================================================================//
OnTimerB:
	sleep(.hpto[3]);
	if(.hpto[0]>.hpto[1]){
		set .@c4,1;
	}else if(.hpto[0]<.hpto[1]){
		set .@c4,2;
	}
	announce "[Battle Ground System] Ground "+(.@c4?.@c4+" Win":"Tie")+" !",0;
	callsub ii;
	end;
//=======================================================================//
//================================OnBreak================================//
//=======================================================================//
OnKiillT2:
	bg_warp $bgvgbid1,"bat_b01",87,75;
	bg_warp $bgvgbid2,"bat_b01",311,224;
	bg_updatescore "bat_b01",set(.hpto[0],.hpto[0]+3),set(.hpto[1],.hpto[1]-5);
	if(.hpto[0]>99){
		bg_updatescore "bat_b01",99,.hpto[1];
	}
	sleep(.hpto[4]);
	set .ib2,bg_monster($bgvgbid2,"bat_b01",326,149," ",1956,"batleVg::OnKiillT2");
	end;
OnKiillT1:
	bg_warp $bgvgbid1,"bat_b01",87,75;
	bg_warp $bgvgbid2,"bat_b01",311,224;
	bg_updatescore "bat_b01",set(.hpto[0],.hpto[0]-5),set(.hpto[1],.hpto[1]+3);
	if(.hpto[1]>99){
		bg_updatescore "bat_b01",.hpto[0],99;
	}
	sleep(.hpto[4]);
	set .ib1,bg_monster($bgvgbid1,"bat_b01",61,149," ",1916,"batleVg::OnKiillT1");
	end;
//=======================================================================//
//=================================OnDie=================================//
//=======================================================================//
OnGu1Die:
OnGu2Die:
	getmapxy(@n$,@n,@b,0);
	if((getcharid(4)!=$bgvgbid2&&getcharid(4)!=$bgvgbid1)||@n$!="bat_b01"){		
		set @n$,"";set @n,0; set @b,0; 
		end;
	}
	if(getcharid(4)==$bgvgbid2){
		set .hpto[1],.hpto[1]-1;
	}else{
		set .hpto[0],.hpto[0]-1;
	}
	if(.hpto[1]>99){
		set .hpto[1],99;
	}
	if(.hpto[1]<0){
		set .winn,1;
		set .hpto[1],0;
	}
	if(.hpto[0]>99){
		set .hpto[0],99;
	}
	if(.hpto[0]<0){
		set .winn,2;
		set .hpto[0],0;
	}
	bg_updatescore "bat_b01",.hpto[0],.hpto[1];
	if(.winn){
		announce "[Battle Ground System] Ground "+.winn+" Win !",0;
		sleep 1000;
		callsub ii;
		mapwarp "bat_b01","prontera",150,180;
		set .game,3;
	}
	end;
}
//=======================================================================//
//==================================Heal=================================//
//=======================================================================//
bat_b01,311,224,4	script	#bgvg_heal1	139,{
OnTimer2000:
	unitwarp .ib2,"bat_b01",326,149;
	unitwarp .ib1,"bat_b01",61,149;
	if(getvariableofnpc(.game,"batleVg")==2){
		unitwarp .ib1,"bat_b01",61,149;
		unitwarp .ib2,"bat_b01",326,149;
		specialeffect 573;
		donpcevent "#bgvg_heal2::OnDo";
		areapercentheal "bat_b01",301,214,321,234,1+rand(2),1+rand(2);
		areapercentheal "bat_b01",77,65,97,85,1+rand(2),1+rand(2);
		sleep(1000);
		areapercentheal "bat_b01",301,214,321,234,1+rand(2),1+rand(2);
		areapercentheal "bat_b01",77,65,97,85,1+rand(2),1+rand(2);
		sleep(1000);
		areapercentheal "bat_b01",301,214,321,234,1+rand(2),1+rand(2);
		areapercentheal "bat_b01",77,65,97,85,1+rand(2),1+rand(2);
		sleep(1000);
		areapercentheal "bat_b01",301,214,321,234,1+rand(2),1+rand(2);
		areapercentheal "bat_b01",77,65,97,85,1+rand(2),1+rand(2);
		sleep(1000);
		areapercentheal "bat_b01",301,214,321,234,1+rand(2),1+rand(2);
		areapercentheal "bat_b01",77,65,97,85,1+rand(2),1+rand(2);
		sleep(1000);
		areapercentheal "bat_b01",301,214,321,234,1+rand(2),1+rand(2);
		areapercentheal "bat_b01",77,65,97,85,1+rand(2),1+rand(2);
		sleep(1000);
		initnpctimer;
	}else{
		stopnpctimer;
	}
	end;
}
bat_b01,87,75,4	script	#bgvg_heal2	139,{
OnDo:
	specialeffect 573;
	end;
}
//=======================================================================//
//==================================Warp=================================//
//=======================================================================//
bat_b01,338,226,0	warp	battleground Vg warp 1	2,2,bat_b01,331,170
bat_b01,286,228,0	warp	battleground Vg warp 2	2,2,bat_b01,242,179
bat_b01,61,73,0	warp	battleground Vg warp 3	2,2,bat_b01,65,133
bat_b01,114,72,0	warp	battleground Vg warp 4	2,2,bat_b01,154,122
bat_b01,199,119,0	script	#battleground Vg warp 5	45,2,2,{
	if(getcharid(4)!=$bgvgbid1){end;}
	warp "bat_b01",87,75;
	end;
}
bat_b01,199,181,0	script	#battleground Vg warp 6	45,2,2,{
	if(getcharid(4)!=$bgvgbid2){end;}
	warp "bat_b01",311,224;
	end;
}
//=======================================================================//
//================================Mapflag================================//
//=======================================================================//
bat_b01	mapflag	battleground	2
bat_b01	mapflag	nomemo
bat_b01	mapflag	nosave	SavePoint
bat_b01	mapflag	noteleport
bat_b01	mapflag	nowarp
bat_b01	mapflag	nowarpto
bat_b01	mapflag	noreturn
bat_b01	mapflag	nobranch
bat_b01	mapflag	nopenalty
bat_b01	mapflag	monster_noteleport
pvp_room	mapflag	loadevent
//=======================================================================//
//===============================Function================================//
//=======================================================================//
function	script	BattleGroundVgFunction	{
	getmapxy(@m$,@x,@y,0);
	if(getcharid(4)!=$bgvgbid1&&getcharid(4)!=$bgvgbid2&&@m$!="bat_b01"){
		dispbottom "[You can't use this item]";
		end;
	}
	bg_warp getcharid(4),@m$,@x,@y;
	end;
}