//===== rAthena Script =======================================
//= BattleGround System - KvM 60-80
//===== Description: =========================================
//= [Official Conversion]
//= Kreiger Von Midgard Battleground for levels 60 to 80
//= - Winning Team: 2 points
//= - Losing Team: 1 point
//===== Changelogs: ==========================================
//= 1.0 First Version. [L0ne_W0lf]
//= 1.1 Updated using official Aegis files. [L0ne_W0lf]
//= 1.2 Upated some announces and dialogs from iRO.
//=     Changed how the scoreboard works slightly.
//=     Removed the areapercentheals, and minor things.
//= 1.3 Fixed wrong names for disablenpc/enablenpc. [Ai4rei]
//= 1.4 Added battle_config checks to allow this script to use the new queue interface or the previous method. [Aleos]
//============================================================

// Waiting Room NPCs
//============================================================
bat_room,225,226,5	script	KVM Waiting Room#a3::KvM03R_Guillaume	418,{
	end;
OnInit:
	waitingroom "Battle Station 5 Players",6,"KvM03_BG_Out::OnGuillaumeJoin",1;
	end;
OnEnterBG:
	set $@KvM03BG_id1, waitingroom2bg("bat_c03",52,129,"KvM03_BG::OnGuillaumeQuit","KvM03_BG::OnGuillaumeDie");
	end;
}

bat_room,225,205,3	script	KVM Waiting Room#b3::KvM03R_Croix	414,{
	end;
OnInit:
	waitingroom "Battle Station 5 Players",6,"KvM03_BG_Out::OnCroixJoin",1;
	end;
OnEnterBG:
	set $@KvM03BG_id2, waitingroom2bg("bat_c03",147,55,"KvM03_BG::OnCroixQuit","KvM03_BG::OnCroixDie");
	end;
}
bat_room,225,220,0	warp2	#kvm701	2,2,bat_room,154,150
bat_room,225,211,0	warp2	#kvm702	2,2,bat_room,154,150

// Starting Line
//============================================================
-	script	KVM03::CellEffect3	-1,{
	end;

OnKvM03One:
	specialeffect EF_CHANGECOLD;
	end;

OnKvM03Two:
	specialeffect EF_CHANGEPOISON;
	end;
}

bat_c03,54,124,3	duplicate(CellEffect3)	#RedcellA1-3	139
bat_c03,55,124,3	duplicate(CellEffect3)	#RedcellA2-3	139
bat_c03,56,124,3	duplicate(CellEffect3)	#RedcellA3-3	139
bat_c03,57,124,3	duplicate(CellEffect3)	#RedcellA4-3	139
bat_c03,57,125,3	duplicate(CellEffect3)	#RedcellA5-3	139
bat_c03,57,126,3	duplicate(CellEffect3)	#RedcellA6-3	139
bat_c03,57,127,3	duplicate(CellEffect3)	#RedcellA7-3	139

bat_c03,145,59,3	duplicate(CellEffect3)	#RedcellB1-3	139
bat_c03,144,59,3	duplicate(CellEffect3)	#RedcellB2-3	139
bat_c03,143,59,3	duplicate(CellEffect3)	#RedcellB3-3	139
bat_c03,142,59,3	duplicate(CellEffect3)	#RedcellB4-3	139
bat_c03,142,56,3	duplicate(CellEffect3)	#RedcellB5-3	139
bat_c03,142,57,3	duplicate(CellEffect3)	#RedcellB6-3	139
bat_c03,142,58,3	duplicate(CellEffect3)	#RedcellB7-3	139

bat_c03,54,128,3	script	#A_camp_start03	139,4,4,{
	end;
OnInit:
	disablenpc "#A_camp_start03";
	end;
OnEnable:
	enablenpc "#A_camp_start03";
	end;
OnDisable:
	disablenpc "#A_camp_start03";
	end;
OnTouch:
	set Bat_Team,1;
	if (!getbattleflag("feature.bgqueue"))
		setquest 6025;
	end;
}

bat_c03,146,56,3	script	#B_camp_start03	139,4,4,{
	end;
OnInit:
	disablenpc "#B_camp_start03";
	end;
OnEnable:
	enablenpc "#B_camp_start03";
	end;
OnDisable:
	disablenpc "#B_camp_start03";
	end;
OnTouch:
	set Bat_Team,2;
	if (!getbattleflag("feature.bgqueue"))
		setquest 6025;
	end;
}

// Battleground Engine
//============================================================
-	script	KvM03_BG	-1,{
	end;

OnInit:
	setwall "bat_c03",54,122,6,7,0,"batc03wall_a";
	setwall "bat_c03",55,122,5,7,0,"batc03wall_b";
	setwall "bat_c03",140,56,6,7,0,"batc03wall_c";
	setwall "bat_c03",140,57,5,7,0,"batc03wall_d";
	disablenpc "KVM Officer#KVM03A";
	disablenpc "KVM Officer#KVM03B";
	end;

OnGuillaumeQuit:
	if (getbattleflag("feature.bgqueue"))
		bg_desert;
//	else
//		set BG_Delay_Tick, gettimetick(2) + 1200;
OnGuillaumeDie:
	if( $@KvM03BG == 2 ) {
		set .Guillaume_Count, .Guillaume_Count - 1;
		bg_updatescore "bat_c03",.Guillaume_Count,.Croix_Count;
		if( .Guillaume_Count < 1 )
			donpcevent "KvM03_BG::OnCroixWin";
		else {
			mapannounce "bat_c03", "The number of Guillaumes is "+.Guillaume_Count+".",bc_map,"0x00ff00";
			mapannounce "bat_c03", "The number of Croixes is "+.Croix_Count+".",bc_map,"0x00ff00";
		}
	}
	end;

OnCroixQuit:
	if (getbattleflag("feature.bgqueue"))
		bg_desert;
//	else
//	set BG_Delay_Tick, gettimetick(2) + 1200;
OnCroixDie:
	if( $@KvM03BG == 2 ) {
		set .Croix_Count, .Croix_Count - 1;
		bg_updatescore "bat_c03",.Guillaume_Count,.Croix_Count;
		if( .Croix_Count < 1 )
			donpcevent "KvM03_BG::OnGuillaumeWin";
		else {
			mapannounce "bat_c03", "The number of Guillaumes is "+.Guillaume_Count+".",bc_map,"0x00ff00";
			mapannounce "bat_c03", "The number of Croixes is "+.Croix_Count+".",bc_map,"0x00ff00";
		}
	}
	end;

OnGuillaumeActive:
	warp "bat_c03",62,119;
	end;

OnCroixActive:
	warp "bat_c03",137,64;
	end;

OnStart:
	disablenpc "KVM Officer#KVM03A";
	disablenpc "KVM Officer#KVM03B";
	set $@KvM03BG_Victory, 0;
	// Warp Teams
	bg_warp $@KvM03BG_id1,"bat_c03",53,128;
	bg_warp $@KvM03BG_id2,"bat_c03",146,55;
	initnpctimer;
	end;

OnTimer1000:
	mapannounce "bat_c03", "In 1 minute, KVM will start.",bc_map,"0x00ff00";
	end;

OnTimer3000:
	mapannounce "bat_c03", "The maximum time for a KVM battle is 5 minutes.",bc_map,"0x00ff00";
	end;

OnTimer6000:
	mapannounce "bat_c03", "Please prepare for the KVM battle.",bc_map,"0x00ff00";
	mapannounce "bat_c03", "You can buff your people.",bc_map,"0x00ff00";
	donpcevent "#A_camp_start03::OnEnable";
	donpcevent "#B_camp_start03::OnEnable";
	end;

OnTimer13000:
	donpcevent "#A_camp_start03::OnDisable";
	donpcevent "#B_camp_start03::OnDisable";
	end;

OnTimer30000:
	mapannounce "bat_c03", "30 seconds remaining to start KVM battle.",bc_map,"0x00ff00";
	end;

OnTimer45000:
	mapannounce "bat_c03", "15 seconds remaining to start KVM battle.",bc_map,"0x00ff00";
	donpcevent "::OnKvM03One";
	end;

OnTimer50000:
	mapannounce "bat_c03", "10 seconds remaining to start KVM battle.",bc_map,"0x00ff00";
	donpcevent "::OnKvM03Two";
	end;

OnTimer55000:
	mapannounce "bat_c03", "5 seconds remaining to start KVM battle.",bc_map,"0x00ff00";
	donpcevent "::OnKvM03One";
	end;

OnTimer59000:
	mapannounce "bat_c03", "KVM is now commencing.",bc_map,"0x00ff00";
	donpcevent "::OnKvM03Two";
	end;

OnTimer61000:
	// Team Members
	set .Guillaume_Count, bg_get_data($@KvM03BG_id1, 0);
	set .Croix_Count, bg_get_data($@KvM03BG_id2, 0);

	if (!getbattleflag("feature.bgqueue")) {
		if (.Guillaume_Count < 5 || .Croix_Count < 5) {
			set $@KvM03BG_Victory, 3;
			set $@KvM03BG, 3;
			mapannounce "bat_c03","There are not enough players to start the battle",1,0xC0C0C0;
			stopnpctimer;
			donpcevent "KvM03_BG::OnStop";
			end;
		}
	}
	bg_updatescore "bat_c03",.Guillaume_Count,.Croix_Count;
	set $@KvM03BG, 2; // Playing
	bg_warp $@KvM03BG_id1,"bat_c03",62,119;
	bg_warp $@KvM03BG_id2,"bat_c03",137,64;
	end;

OnTimer300000:
	mapannounce "bat_c03", "1 minute remaining to finish the KVM battle.",bc_map,"0x00ff00";
	end;

OnTimer330000:
	mapannounce "bat_c03", "30 seconds remaining to finish the KVM battle.",bc_map,"0x00ff00";
	end;

OnTimer345000:
	mapannounce "bat_c03", "15 seconds remaining to finish the KVM battle.",bc_map,"0x00ff00";
	end;

OnTimer350000:
	mapannounce "bat_c03", "10 seconds remaining to finish the KVM battle.",bc_map,"0x00ff00";
	end;

OnTimer355000:
	mapannounce "bat_c03", "5 seconds remaining to finish the KVM battle.",bc_map,"0x00ff00";
	end;

OnTimer360000:
	mapannounce "bat_c03", "KVM has ended.",bc_map,"0x00ff00";
	if( .Croix_Count > .Guillaume_Count )
		donpcevent "KvM03_BG::OnCroixWin";
	else if( .Croix_Count < .Guillaume_Count )
		donpcevent "KvM03_BG::OnGuillaumeWin";
	else
	{ // Draw Game
		set $@KvM03BG, 3;
		set $@KvM03BG_Victory, 3;
		mapannounce "bat_c03", "The number of Guillaumes is "+.Guillaume_Count+".",bc_map,"0x00ff00";
		mapannounce "bat_c03", "The number of Croixes is "+.Croix_Count+".",bc_map,"0x00ff00";
		mapannounce "bat_c03", "This battle has ended in a draw.",bc_map,"0x00ff00";
		donpcevent "KvM03_BG::OnStop";
	}
	end;

OnGuillaumeWin:
	set $@KvM03BG, 3;
	set $@KvM03BG_Victory, 1;
	mapannounce "bat_c03", "Guillaume wins!",bc_map,"0x00ff00";
	mapannounce "bat_c03", "Congratulations to Guillaume members.",bc_map,"0x00ff00";
	mapannounce "bat_c03", "Everyone will be moved to the start point.",bc_map,"0x00ff00";
	donpcevent "KvM03_BG::OnStop";
	end;

OnCroixWin:
	set $@KvM03BG, 3;
	set $@KvM03BG_Victory, 2;
	mapannounce "bat_c03", "Croix wins!",bc_map,"0x00ff00";
	mapannounce "bat_c03", "Congratulations to Croix members.",bc_map,"0x00ff00";
	mapannounce "bat_c03", "Everyone will be moved to the start point.",bc_map,"0x00ff00";
	donpcevent "KvM03_BG::OnStop";
	end;

OnStop:
	stopnpctimer;
	enablenpc "KVM Officer#KVM03A";
	enablenpc "KVM Officer#KVM03B";
	// Warp Teams
	bg_warp $@KvM03BG_id1,"bat_c03",53,128;
	bg_warp $@KvM03BG_id2,"bat_c03",146,55;
	donpcevent "KvM03_BG_Out::OnBegin";
	if (getbattleflag("feature.bgqueue"))
		bg_reserve "bat_c03", true;
	end;
}

-	script	KvM03_BG_Out	-1,{
	end;

OnBegin:
	initnpctimer;
	end;

OnTimer1000:
	mapannounce "bat_c03", "Please apply with the Officer to acquire KVM points.",bc_map,"0x00ff00";
	end;

OnTimer3000:
	mapannounce "bat_c03", "The Officer will grant you the points for 30 seconds.",bc_map,"0x00ff00";
	mapannounce "bat_c03", "In 30 seconds, the Officer will be sent away.",bc_map,"0x00ff00";
	end;

OnTimer5000:
	mapannounce "bat_c03", "Unless you talk to the Officer, you cannot gain the points.",bc_map,"0x00ff00";
	mapannounce "bat_c03", "Please be careful.",bc_map,"0x00ff00";
	end;

OnTimer55000:
	mapannounce "bat_c03", "You will be sent back.",bc_map,"0x00ff00";
	end;

OnTimer60000:
	stopnpctimer;
	if (getbattleflag("feature.bgqueue"))
		bg_reserve "bat_c03", true;
	mapwarp "bat_c03","bat_room",154,150;
	set getvariableofnpc(.Croix_Count,"KvM03_BG"), 0;
	set getvariableofnpc(.Guillaume_Count,"KvM03_BG"), 0;
	set $@KvM03BG_Victory, 0;
	if( $@KvM03BG_id1 ) { bg_destroy $@KvM03BG_id1; set $@KvM03BG_id1, 0; }
	if( $@KvM03BG_id2 ) { bg_destroy $@KvM03BG_id2; set $@KvM03BG_id2, 0; }
	if (getbattleflag("feature.bgqueue"))
		bg_unbook "bat_c03";
	disablenpc "KVM Officer#KVM03A";
	disablenpc "KVM Officer#KVM03B";
	set $@KvM03BG, 0;
	end;

OnGuillaumeJoin:
OnCroixJoin:
	if( $@KvM03BG )
		end;
	set .@Guillaume, getwaitingroomstate(0,"KvM03R_Guillaume");
	set .@Croix, getwaitingroomstate(0,"KvM03R_Croix");

	if( .@Guillaume < 5 || .@Croix < 5 )
		end;

	set $@KvM03BG, 1; // Starting
	donpcevent "KvM03R_Croix::OnEnterBG";
	donpcevent "KvM03R_Guillaume::OnEnterBG";
	donpcevent "KvM03_BG::OnStart";
	end;
}

// Battleground rewards
//============================================================
bat_c03,51,130,5	script	KVM Officer#KVM03A	419,{
	if( $@KvM03BG_Victory ) {
		mes "[KVM Officer]";
		if( $@KvM03BG_Victory == Bat_Team ) {
			mes "Good Game.";
			mes "May the glory of KVM be with you.";
			mes "You aquire the winning points: 2";
			close2;
			set kvm_point,kvm_point + 2;
		}
		else {
			mes "I am so sorry.";
			mes "I wish you better luck next time.";
			mes "You aquire the losing points: 1";
			close2;
			set kvm_point,kvm_point + 1;
		}
		bg_leave;
		set Bat_Team,0;
		if (!getbattleflag("feature.bgqueue"))
			warp "bat_room",154,150;
	}
	end;
}
bat_c03,148,53,1	duplicate(KVM Officer#KVM03A)	KVM Officer#KVM03B	415

// BG Queue makes these scripts useless
-	script	BGQueueInit#kvm03	-1,{
	end;

OnInit:
	if (getbattleflag("feature.bgqueue")) {
		unloadnpc "KvM03R_Guillaume";
		unloadnpc "KvM03R_Croix";
		unloadnpc "#kvm701";
		unloadnpc "#kvm702";
	}
	end;
}
