//================= Hercules Script =======================================
//=       _   _                     _
//=      | | | |                   | |
//=      | |_| | ___ _ __ ___ _   _| | ___  ___
//=      |  _  |/ _ \ '__/ __| | | | |/ _ \/ __|
//=      | | | |  __/ | | (__| |_| | |  __/\__ \
//=      \_| |_/\___|_|  \___|\__,_|_|\___||___/
//================= License ===============================================
//= This file is part of Hercules.
//= http://herc.ws - http://github.com/HerculesWS/Hercules
//=
//= Copyright (C) 2012-2016  Hercules Dev Team
//= Copyright (C)  Ai4rei
//= Copyright (C)  L0ne_W0lf
//=
//= Hercules is free software: you can redistribute it and/or modify
//= it under the terms of the GNU General Public License as published by
//= the Free Software Foundation, either version 3 of the License, or
//= (at your option) any later version.
//=
//= This program is distributed in the hope that it will be useful,
//= but WITHOUT ANY WARRANTY; without even the implied warranty of
//= MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//= GNU General Public License for more details.
//=
//= You should have received a copy of the GNU General Public License
//= along with this program.  If not, see <http://www.gnu.org/licenses/>.
//=========================================================================
// BattleGround System - KvM 1-59
//================= Description ===========================================
//= Kreiger Von Midgard Battleground for levels 1 to 59
//= - Winning Team: 1 point
//= - Losing Team: 0 points
//================= Current Version =======================================
//= 1.3
//=========================================================================

//== Waiting Room NPCs =====================================
bat_room,197,226,5	script	KVM Waiting Room#a2::KvM02R_Guillaume	4_M_KY_KNT,{
	end;

OnInit:
	waitingroom(_("Battle Station 5 Players"), 6, "KvM02_BG::OnGuillaumeJoin", 1);
	end;

OnEnterBG:
	$@KvM02BG_id1 = waitingroom2bg("bat_c02",52,129,"KvM02_BG::OnGuillaumeQuit","KvM02_BG::OnGuillaumeDie");
	end;
}

bat_room,197,220,0	warp	#kvm601	2,2,bat_room,154,150

bat_room,197,205,3	script	KVM Waiting Room#b2::KvM02R_Croix	4_M_CRU_KNT,{
	end;

OnInit:
	waitingroom(_("Battle Station 5 Players"), 6, "KvM02_BG::OnCroixJoin", 1);
	end;

OnEnterBG:
	$@KvM02BG_id2 = waitingroom2bg("bat_c02",147,55,"KvM02_BG::OnCroixQuit","KvM02_BG::OnCroixDie");
	end;
}

bat_room,197,211,0	warp	#kvm602	2,2,bat_room,154,150

//== Starting Line =========================================
-	script	KVM02::CellEffect2	FAKE_NPC,{
	end;

OnKvM02One:
	specialeffect EF_CHANGECOLD;
	end;

OnKvM02Two:
	specialeffect EF_CHANGEPOISON;
	end;
}

bat_c02,54,124,3	duplicate(CellEffect2)	#RedcellA1-2	HIDDEN_WARP_NPC
bat_c02,55,124,3	duplicate(CellEffect2)	#RedcellA2-2	HIDDEN_WARP_NPC
bat_c02,56,124,3	duplicate(CellEffect2)	#RedcellA3-2	HIDDEN_WARP_NPC
bat_c02,57,124,3	duplicate(CellEffect2)	#RedcellA4-2	HIDDEN_WARP_NPC
bat_c02,57,125,3	duplicate(CellEffect2)	#RedcellA5-2	HIDDEN_WARP_NPC
bat_c02,57,126,3	duplicate(CellEffect2)	#RedcellA6-2	HIDDEN_WARP_NPC
bat_c02,57,127,3	duplicate(CellEffect2)	#RedcellA7-2	HIDDEN_WARP_NPC

bat_c02,145,59,3	duplicate(CellEffect2)	#RedcellB1-2	HIDDEN_WARP_NPC
bat_c02,144,59,3	duplicate(CellEffect2)	#RedcellB2-2	HIDDEN_WARP_NPC
bat_c02,143,59,3	duplicate(CellEffect2)	#RedcellB3-2	HIDDEN_WARP_NPC
bat_c02,142,59,3	duplicate(CellEffect2)	#RedcellB4-2	HIDDEN_WARP_NPC
bat_c02,142,56,3	duplicate(CellEffect2)	#RedcellB5-2	HIDDEN_WARP_NPC
bat_c02,142,57,3	duplicate(CellEffect2)	#RedcellB6-2	HIDDEN_WARP_NPC
bat_c02,142,58,3	duplicate(CellEffect2)	#RedcellB7-2	HIDDEN_WARP_NPC

bat_c02,54,128,3	script	#A_camp_start02	HIDDEN_WARP_NPC,4,4,{
	end;

OnInit:
	disablenpc "#A_camp_start02";
	end;

OnEnable:
	enablenpc "#A_camp_start02";
	end;

OnDisable:
	disablenpc "#A_camp_start03";
	end;

OnTouch:
	Bat_Team = 1;
	setquest 6025;
	end;
}

bat_c02,146,56,3	script	#B_camp_start02	HIDDEN_WARP_NPC,4,4,{
	end;

OnInit:
	disablenpc "#B_camp_start02";
	end;

OnEnable:
	enablenpc "#B_camp_start02";
	end;

OnDisable:
	disablenpc "#B_camp_start02";
	end;

OnTouch:
	Bat_Team = 2;
	setquest 6025;
	end;
}

//== Battleground Engine ===================================
-	script	KvM02_BG	FAKE_NPC,{
	end;

OnInit:
	setwall "bat_c02",54,122,6,7,0,"batc02wall_a";
	setwall "bat_c02",55,122,5,7,0,"batc02wall_b";
	setwall "bat_c02",140,56,6,7,0,"batc02wall_c";
	setwall "bat_c02",140,57,5,7,0,"batc02wall_d";
	disablenpc "KVM Officer#KVM02A";
	disablenpc "KVM Officer#KVM02B";
	end;

OnGuillaumeJoin:
OnCroixJoin:
	donpcevent "KvM02_BG::OnReadyCheck";
	end;

OnGuillaumeQuit:
	//BG_Delay_Tick = gettimetick(2) + 1200;
OnGuillaumeDie:
	if ($@KvM02BG == 2) {
		--.Guillaume_Count;
		bg_updatescore "bat_c02",.Guillaume_Count,.Croix_Count;
		if( .Guillaume_Count < 1 ) donpcevent "KvM02_BG::OnCroixWin";
		else {
			mapannounce("bat_c02", sprintf(_$("The number of Guillaumes is %d."), .Guillaume_Count), bc_map, "0x00ff00");
			mapannounce("bat_c02", sprintf(_$("The number of Croixes is %d."), .Croix_Count), bc_map, "0x00ff00");
		}
	}
	end;

OnCroixQuit:
	//BG_Delay_Tick = gettimetick(2) + 1200;
OnCroixDie:
	if ($@KvM02BG == 2) {
		--.Croix_Count;
		bg_updatescore "bat_c02",.Guillaume_Count,.Croix_Count;
		if( .Croix_Count < 1 ) donpcevent "KvM02_BG::OnGuillaumeWin";
		else {
			mapannounce("bat_c02", sprintf(_$("The number of Guillaumes is %d."), .Guillaume_Count), bc_map, "0x00ff00");
			mapannounce("bat_c02", sprintf(_$("The number of Croixes is %d."), .Croix_Count), bc_map, "0x00ff00");
		}
	}
	end;

OnReadyCheck:
	if( $@KvM02BG )
		end;
	.@Guillaume = getwaitingroomstate(0,"KvM02R_Guillaume");
	.@Croix = getwaitingroomstate(0,"KvM02R_Croix");

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

	$@KvM02BG = 1; // Starting
	donpcevent "KvM02R_Croix::OnEnterBG";
	donpcevent "KvM02R_Guillaume::OnEnterBG";
	donpcevent "KvM02_BG::OnStart";
	end;

OnStart:
	disablenpc "KVM Officer#KVM02A";
	disablenpc "KVM Officer#KVM02B";
	$@KvM02BG_Victory = 0;
	// Warp Teams
	bg_warp $@KvM02BG_id1,"bat_c02",53,128;
	bg_warp $@KvM02BG_id2,"bat_c02",146,55;
	initnpctimer;
	end;

OnTimer1000:
	mapannounce("bat_c02", _("In 1 minute, KVM will start."), bc_map, "0x00ff00");
	end;

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

OnTimer6000:
	mapannounce("bat_c02", _("Please prepare for the KVM battle."), bc_map, "0x00ff00");
	mapannounce("bat_c02", _("You can buff your people."), bc_map, "0x00ff00");
	donpcevent "#A_camp_start02::OnEnable";
	donpcevent "#B_camp_start02::OnEnable";
	end;

OnTimer13000:
	donpcevent "#A_camp_start02::OnDisable";
	donpcevent "#B_camp_start02::OnDisable";
	end;

OnTimer30000:
	mapannounce("bat_c02", _("30 seconds remaining to start KVM battle."), bc_map, "0x00ff00");
	end;

OnTimer45000:
	mapannounce("bat_c02", _("15 seconds remaining to start KVM battle."), bc_map, "0x00ff00");
	donpcevent "::OnKvM01One";
	end;

OnTimer50000:
	mapannounce("bat_c02", _("10 seconds remaining to start KVM battle."), bc_map, "0x00ff00");
	donpcevent "::OnKvM01Two";
	end;

OnTimer55000:
	mapannounce("bat_c02", _("5 seconds remaining to start KVM battle."), bc_map, "0x00ff00");
	donpcevent "::OnKvM01One";
	end;

OnTimer59000:
	mapannounce("bat_c02", _("KVM is now commencing."), bc_map, "0x00ff00");
	donpcevent "::OnKvM01Two";
	end;

OnTimer61000:
	// Team Members
	.Guillaume_Count = bg_get_data($@KvM02BG_id1, 0);
	.Croix_Count = bg_get_data($@KvM02BG_id2, 0);
	if( .Guillaume_Count < 5 || .Croix_Count < 5 )
	{
		$@KvM02BG_Victory = 3;
		$@KvM02BG = 3;
		mapannounce("bat_c02", _("There are not enough players to start the battle"), bc_map, "0x808080");
		stopnpctimer;
		donpcevent "KvM02_BG::OnStop";
		end;
	}
	$@KvM02BG = 2; // Playing
	bg_warp $@KvM02BG_id1,"bat_c02",62,119;
	bg_warp $@KvM02BG_id2,"bat_c02",137,64;
	end;

OnTimer300000:
	mapannounce("bat_c02", _("1 minute remaining to finish the KVM battle."), bc_map, "0x00ff00");
	end;

OnTimer330000:
	mapannounce("bat_c02", _("30 seconds remaining to finish the KVM battle."), bc_map, "0x00ff00");
	end;

OnTimer345000:
	mapannounce("bat_c02", _("15 seconds remaining to finish the KVM battle."), bc_map, "0x00ff00");
	end;

OnTimer350000:
	mapannounce("bat_c02", _("10 seconds remaining to finish the KVM battle."), bc_map, "0x00ff00");
	end;

OnTimer355000:
	mapannounce("bat_c02", _("5 seconds remaining to finish the KVM battle."), bc_map, "0x00ff00");
	end;

OnTimer360000:
	mapannounce("bat_c02", _("The KVM battle is over."), bc_map, "0x00ff00");
	if( .Croix_Count > .Guillaume_Count )
		donpcevent "KvM02_BG::OnCroixWin";
	else if( .Croix_Count < .Guillaume_Count )
		donpcevent "KvM02_BG::OnGuillaumeWin";
	else
	{ // Draw Game
		$@KvM02BG = 3;
		$@KvM02BG_Victory = 3;
		mapannounce("bat_c02", sprintf(_$("The number of Guillaumes is %d."), .Guillaume_Count), bc_map, "0x00ff00");
		mapannounce("bat_c02", sprintf(_$("The number of Croixes is %d."), .Croix_Count), bc_map, "0x00ff00");
		mapannounce("bat_c02", _("This battle has ended in a draw."), bc_map, "0x00ff00");
		donpcevent "KvM02_BG::OnStop";
	}
	end;

OnGuillaumeWin:
	$@KvM02BG = 3;
	$@KvM02BG_Victory = 1;
	mapannounce("bat_c02", _("Guillaume wins!"), bc_map, "0x00ff00");
	mapannounce("bat_c02", _("Congratulations to Guillaume members."), bc_map, "0x00ff00");
	mapannounce("bat_c02", _("Everyone will be moved to the start point."), bc_map, "0x00ff00");
	donpcevent "KvM02_BG::OnStop";
	end;

OnCroixWin:
	$@KvM02BG = 3;
	$@KvM02BG_Victory = 2;
	mapannounce("bat_c02", _("Croix wins!"), bc_map, "0x00ff00");
	mapannounce("bat_c02", _("Congratulations to Croix members."), bc_map, "0x00ff00");
	mapannounce("bat_c02", _("Everyone will be moved to the start point."), bc_map, "0x00ff00");
	donpcevent "KvM02_BG::OnStop";
	end;

OnStop:
	stopnpctimer;
	enablenpc "KVM Officer#KVM02A";
	enablenpc "KVM Officer#KVM02B";
	// Warp Teams
	bg_warp $@KvM02BG_id1,"bat_c02",53,128;
	bg_warp $@KvM02BG_id2,"bat_c02",146,55;
	donpcevent "KvM02_BG_Out::OnBegin";
	end;

OnReset:
	.Croix_Count = 0;
	.Guillaume_Count = 0;
	$@KvM02BG_Victory = 0;
	if( $@KvM02BG_id1 ) { bg_destroy $@KvM02BG_id1; $@KvM02BG_id1 = 0; }
	if( $@KvM02BG_id2 ) { bg_destroy $@KvM02BG_id2; $@KvM02BG_id2 = 0; }
	disablenpc "KVM Officer#KVM02A";
	disablenpc "KVM Officer#KVM02B";
	mapwarp "bat_c02","bat_room",154,150;
	maprespawnguildid "bat_c02",0,3; // Just in case someone else
	bg_updatescore "bat_c02",5,5;
	$@KvM02BG = 0;
	donpcevent "KvM02_BG::OnReadyCheck"; // Maybe a game is ready to start
	end;
}

-	script	KvM02_BG_Out	FAKE_NPC,{
	end;

OnBegin:
	initnpctimer;
	end;

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

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

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

OnTimer55000:
	mapannounce("bat_c02", _("You will be sent back."), bc_map, "0x00ff00");
	end;

OnTimer60000:
	stopnpctimer;
	donpcevent "KvM02_BG::OnReset";
	end;
}

//== Battleground rewards ==================================
bat_c02,51,130,5	script	KVM Officer#KVM02A	4_M_KY_HEAD,{
	if( $@KvM01BG_Victory )
	{
		if( $@KvM02BG_Victory == Bat_Team )
		{ // Victory
			++kvm_point;
			mes("[KVM Officer]");
			mes("Good Game.");
			mes("May the glory of KVM be with you.");
			mes("You aquire the winning points: 1");
			close2;
		} else {
			mes("[KVM Officer]");
			mes("I am so sorry.");
			mes("I wish you better luck next time.");
			mes("You aquire the losing points: 0");
			close2;
		}
		bg_leave;
		Bat_Team = 0;
		warp "bat_room",154,150;
		end;
	}
	end;
}

bat_c02,148,53,1	script	KVM Officer#KVM02B	4_M_CRU_HEAD,{
	if( $@KvM01BG_Victory )
	{
		if( $@KvM02BG_Victory == Bat_Team )
		{ // Victory
			++kvm_point;
			mes("[KVM Officer]");
			mes("Good Game.");
			mes("May the glory of KVM be with you.");
			mes("You aquire the winning points: 1");
			close2;
		} else {
			mes("[KVM Officer]");
			mes("I am so sorry.");
			mes("I wish you better luck next time.");
			mes("You aquire the losing points: 0");
			close2;
		}
		bg_leave;
		Bat_Team = 0;
		warp "bat_room",154,150;
		end;
	}
	end;
}
