// ==============================================================================
// BattleGround System - Flavius 1
// ==============================================================================

// Registration NPC's
// *********************************************************************

bat_room,86,227,4	script	Registration::Fl1R_Guillaume	4_M_KY_KNT,{
	end;
OnInit:
	waitingroom "Battle Station 10 Players",10,"Flavius_BG1::OnGuillaumeJoin",1;
	end;
OnEnterBG:
	set $@FlaviusBG1_id1, waitingroom2bg("bat_b01",390,10,"Flavius_BG1::OnGuillaumeQuit","");
	end;
}

bat_room,85,204,0	script	Registration::Fl1R_Croix	4_M_CRU_KNT,{
	end;
OnInit:
	waitingroom "Battle Station 10 Players",10,"Flavius_BG1::OnCroixJoin",1;
	end;
OnEnterBG:
	set $@FlaviusBG1_id2, waitingroom2bg("bat_b01",10,290,"Flavius_BG1::OnCroixQuit","");
	end;
}

// Battleground Engine
// *********************************************************************

-	script	Flavius_BG1	FAKE_NPC,{
	end;

OnInit:
	disablenpc "Guillaume Vintenar#fl1";
	disablenpc "Croix Vintenar#fl1";
	disablenpc "Therapist in battle#fl11";
	disablenpc "Therapist in battle#fl12";
	end;

OnGuillaumeQuit:
OnCroixQuit:
	set BG_Delay_Tick, gettimetick(2) + 1200;
	end;

OnGuillaumeJoin:
OnCroixJoin:
	if( $@FlaviusBG1 == 0 )
		donpcevent "Flavius_BG1::OnReadyCheck";
	end;

OnReadyCheck:
	if( $@FlaviusBG1 )
		end;
	set .@Guillaume, getwaitingroomstate(0,"Fl1R_Guillaume");
	set .@Croix, getwaitingroomstate(0,"Fl1R_Croix");

	if( .@Guillaume < 10 || .@Croix < 10 )
	{
		mapannounce "bat_room","Battleground -- Flavius [80-99] G: " + .@Guillaume + "/10, C: " + .@Croix + "/10",1,0x006400;
		end;
	}

	// BG Variables
	set $@FlaviusBG1, 1;
	set $@FlaviusBG1_Victory, 0;
	set .Guillaume_Score, 0;
	set .Guillaume_Loss, 0;
	set .Croix_Score, 0;
	set .Croix_Loss, 0;
	set .Match, 0;
	// Prepare NPC
	donpcevent "#gfl1_respawn::OnBGStart";
	donpcevent "#cfl1_respawn::OnBGStart";
	enablenpc "Therapist in battle#fl11";
	enablenpc "Therapist in battle#fl12";
	disablenpc "Guillaume Vintenar#fl1";
	disablenpc "Croix Vintenar#fl1";
	// Build and Warp Teams
	donpcevent "Fl1R_Guillaume::OnEnterBG";
	donpcevent "Fl1R_Croix::OnEnterBG";
	announce "Battleground -- Flavius [80-99] has started!",0,0x006400;
	initnpctimer;
	// Start Match!!

OnRoundStart:
	sleep 2000;
	if( $@FlaviusBG1 != 1 ) end;

	areapercentheal "bat_b01",382,2,397,17,100,100;
	areapercentheal "bat_b01",2,282,17,297,100,100;
	bg_warp $@FlaviusBG1_id1,"bat_b01",311,224;
	bg_warp $@FlaviusBG1_id2,"bat_b01",87,75;

	sleep 2000;
	if( $@FlaviusBG1 != 1 ) end;

	set .Match, .Match + 1;
	// Crystal Spawn
	set .Guillaume_Crystal, bg_monster($@FlaviusBG1_id1,"bat_b01",328,150,"Crystal Guillaume",1914,"Flavius_BG1::OnGuillaumeBreak");
	setcell "bat_b01",327,151,329,149,cell_basilica,1;
	setcell "bat_b01",327,151,329,149,cell_walkable,1;

	set .Croix_Crystal, bg_monster($@FlaviusBG1_id2,"bat_b01",62,150,"Crystal Croix",1915,"Flavius_BG1::OnCroixBreak");
	setcell "bat_b01",62,149,60,151,cell_basilica,1;
	setcell "bat_b01",62,149,60,151,cell_walkable,1;

	// Guardian Spawns
	bg_monster $@FlaviusBG1_id1,"bat_b01",328,160,"Guillaume Guardian",1949,"Flavius_BG1::OnGuiGuardian";
	bg_monster $@FlaviusBG1_id1,"bat_b01",328,140,"Guillaume Guardian",1950,"Flavius_BG1::OnGuiGuardian";
	set .Guillaume_Guardian, 2;
	bg_monster $@FlaviusBG1_id2,"bat_b01",62,160,"Croix Guardian",1949,"Flavius_BG1::OnCroGuardian";
	bg_monster $@FlaviusBG1_id2,"bat_b01",61,140,"Croix Guardian",1950,"Flavius_BG1::OnCroGuardian";
	set .Croix_Guardian, 2;
	// Announces
	mapannounce "bat_b01","The Battle of Flavius [ " + .Match + " ] Round has begun!!",1,0x006400;
	end;

OnRoundStop:
	// Remove Monsters
	killmonster "bat_b01","Flavius_BG1::OnGuiGuardian";
	killmonster "bat_b01","Flavius_BG1::OnCroGuardian";
	setcell "bat_b01",327,151,329,149,cell_walkable,0;
	setcell "bat_b01",327,151,329,149,cell_basilica,0;
	killmonster "bat_b01","Flavius_BG1::OnGuillaumeBreak";
	setcell "bat_b01",62,149,60,151,cell_walkable,0;
	setcell "bat_b01",62,149,60,151,cell_basilica,0;
	killmonster "bat_b01","Flavius_BG1::OnCroixBreak";
	end;

OnGuiGuardian:
	if( set(.Guillaume_Guardian, .Guillaume_Guardian - 1) <= 0 )
	{
		setcell "bat_b01",327,151,329,149,cell_walkable,0;
		setcell "bat_b01",327,151,329,149,cell_basilica,0;
		mapannounce "bat_b01","The Guillaume Crystal is vulnerable to attack!",1,0x0000FF;
	}
	end;

OnCroGuardian:
	if( set(.Croix_Guardian, .Croix_Guardian - 1) <= 0 )
	{
		setcell "bat_b01",62,149,60,151,cell_walkable,0;
		setcell "bat_b01",62,149,60,151,cell_basilica,0;
		mapannounce "bat_b01","The Croix Crystal is vulnerable to attack!",1,0xFF0000;
	}
	end;

OnGuillaumeBreak:
	donpcevent "Flavius_BG1::OnRoundStop";
	set .Guillaume_Loss, .Guillaume_Loss + 1;
	if( set(.Croix_Score, .Croix_Score + 1) < 2 )
	{
		bg_updatescore "bat_b01",.Guillaume_Score,.Croix_Score;
		mapannounce "bat_b01","The Guillaume Crystal has been destroyed!",1,0x0000FF;
		donpcevent "Flavius_BG1::OnRoundStart";
	}
	else
	{
		bg_updatescore "bat_b01",.Guillaume_Score,.Croix_Score;
		mapannounce "bat_b01","The Croix army has won the Battle of Flavius!",1,0xFF0000;
		set $@FlaviusBG1_Victory, 2;
		donpcevent "Flavius_BG1::OnMatchEnd";
	}
	end;

OnCroixBreak:
	donpcevent "Flavius_BG1::OnRoundStop";
	set .Croix_Loss, .Croix_Loss + 1;
	if( set(.Guillaume_Score, .Guillaume_Score + 1) < 2 )
	{
		bg_updatescore "bat_b01",.Guillaume_Score,.Croix_Score;
		mapannounce "bat_b01","The Croix Crystal has been destroyed!",1,0xFF0000;
		donpcevent "Flavius_BG1::OnRoundStart";
	}
	else
	{
		bg_updatescore "bat_b01",.Guillaume_Score,.Croix_Score;
		mapannounce "bat_b01","The Guillaume army has won the Battle of Flavius!",1,0x0000FF;
		set $@FlaviusBG1_Victory, 1;
		donpcevent "Flavius_BG1::OnMatchEnd";
	}
	end;

OnTimer2400000:
	mapannounce "bat_b01","Battle of Flavius will ends in 5 minutes",1,0x006400;
	end;
OnTimer2640000:
	mapannounce "bat_b01","Battle of Flavius will ends in 1 minute",1,0x006400;
	end;

OnTimer2700000:
	if( .Croix_Score > .Guillaume_Score )
	{
		mapannounce "bat_b01","The Croix army has won the Battle of Flavius!",1,0xFF0000;
		set $@FlaviusBG1_Victory, 2;
	}
	else if( .Croix_Score < .Guillaume_Score )
	{
		mapannounce "bat_b01","The Guillaume army has won the Battle of Flavius!",1,0x0000FF;
		set $@FlaviusBG1_Victory, 1;
	}
	else
	{
		mapannounce "bat_b01","The Battle of Flavius is over. The time is out, this is a Tie",1,0x006400;
		set $@FlaviusBG1_Victory, 3;
	}

OnMatchEnd:
	set $@FlaviusBG1, 2;
	stopnpctimer;
	donpcevent "#gfl1_respawn::OnBGStop";
	donpcevent "#cfl1_respawn::OnBGStop";
	disablenpc "Therapist in battle#fl11";
	disablenpc "Therapist in battle#fl12";
	enablenpc "Guillaume Vintenar#fl1";
	enablenpc "Croix Vintenar#fl1";
	sleep 2000;
	bg_warp $@FlaviusBG1_id1,"bat_b01",390,10;
	bg_warp $@FlaviusBG1_id2,"bat_b01",10,290;
	sleep 3000;
	mapannounce "bat_b01","Battle of Flavius will close in 1 minute!",1,0x006400;
	initnpctimer;
	end;

OnTimer30000:
	if( $@FlaviusBG1 == 2 )
		mapannounce "bat_b01","Battle of Flavius will close in 30 seconds!",1,0x006400;
	end;
OnTimer50000:
	if( $@FlaviusBG1 == 2 )
		mapannounce "bat_b01","Battle of Flavius will close in 10 seconds!",1,0x006400;
	end;

OnTimer60000:
	if( $@FlaviusBG1 != 2 )
		end;
OnReset:
	stopnpctimer;
	donpcevent "Flavius_BG1::OnRoundStop";
	set .Guillaume_Score, 0;
	set .Guillaume_Crystal, 0;
	set .Guillaume_Loss, 0;
	set .Croix_Score, 0;
	set .Croix_Crystal, 0;
	set .Croix_Loss, 0;
	set .Match, 0;
	set $@FlaviusBG1_Victory, 0;
	// NPC's
	disablenpc "Guillaume Vintenar#fl1";
	disablenpc "Croix Vintenar#fl1";
	disablenpc "Therapist in battle#fl11";
	disablenpc "Therapist in battle#fl12";

	if( $@FlaviusBG1_id1 ) { bg_destroy $@FlaviusBG1_id1; set $@FlaviusBG1_id1, 0; }
	if( $@FlaviusBG1_id2 ) { bg_destroy $@FlaviusBG1_id2; set $@FlaviusBG1_id2, 0; }
	sleep 1000;
	mapwarp "bat_b01","bat_room",155,150;
	sleep 2000;
	maprespawnguildid "bat_b01",0,3; // Just in case someone else
	sleep 2000;
	bg_updatescore "bat_b01",0,0;
	set $@FlaviusBG1, 0;
	donpcevent "Flavius_BG1::OnReadyCheck";
	end;
}

// Battleground rewards
// *********************************************************************

bat_b01,390,13,5	script	Guillaume Vintenar#fl1	4_M_KY_HEAD,{
	if( $@FlaviusBG1_Victory )
	{
		if( $@FlaviusBG1_Victory == Bat_Team )
		{ // Victory
			mes "[Swandery]";
			mes "Blessed Guillaume!!";
			mes "Let's enjoy our glorious victory!";
			mes "" + strcharinfo(PC_NAME) + ", its a sign reflecting victory";
			close2;
			set .@reward, 9;
		} else {
			mes "[Swandery]";
			mes "You lost, but you're dedicated to this battle.";
			mes "This is a reward for your great dedication by Guillaume Marollo!";
			mes "Just take this defeat a lesson, and later you would definitely learn.";
			close2;
			set .@reward, 3;
		}

		setquest 2070;
		getitem 7829, .@reward;
		bg_leave;
		warp "bat_room",155,150;
		end;
	}
	end;
}

bat_b01,10,293,5	script	Croix Vintenar#fl1	4_M_CRU_HEAD,{
	if( $@FlaviusBG1_Victory )
	{
		if( $@FlaviusBG1_Victory == Bat_Team )
		{ // Victory
			mes "[Swandery]";
			mes "Blessed Croax!!";
			mes "Let's enjoy our glorious victory!";
			mes "" + strcharinfo(PC_NAME) + ", its a sign reflecting victory";
			close2;
			set .@reward, 9;
		} else {
			mes "[Swandery]";
			mes "Oh, " + strcharinfo(PC_NAME) + ". Don't be sad.";
			mes "Even though we didn't win, we did our best.";
			mes "This is a Royal gift from Croix, and please don't forget this battle. We can win the next.";
			close2;
			set .@reward, 3;
		}

		setquest 2070;
		getitem 7829, .@reward;
		bg_leave;
		warp "bat_room",155,150;
		end;
	}
	end;
}

// Battleground Therapist
// *********************************************************************

bat_b01,390,13,5	script	Therapist in battle#fl12	4_F_SISTER,{
	mes "[Therapist in battle]";
	mes "Just close your eyes, and take a deep breathe.";
	mes "You can be free from pain.";
	specialeffect(EF_HEAL, AREA, playerattached());
	close;
}

bat_b01,10,293,5	script	Therapist in battle#fl11	4_F_SISTER,{
	mes "[Therapist in battle]";
	mes "Just close your eyes, and take a deep breathe.";
	mes "You can be free from pain.";
	specialeffect(EF_HEAL, AREA, playerattached());
	close;
}

// Battleground Respawn
// *********************************************************************

bat_b01,390,10,0	script	#gfl1_respawn	HIDDEN_WARP_NPC,{
	end;

OnBGStart:
	initnpctimer;
	end;

OnBGStop:
	stopnpctimer;
	end;

OnTimer24000:
	specialeffect(EF_SANCTUARY);
	end;

OnTimer25000:
	areapercentheal "bat_b01",382,2,397,17,100,100;
	areawarp "bat_b01",382,2,397,17,"bat_b01",311,224;
	initnpctimer;
	end;
}

bat_b01,10,290,0	script	#cfl1_respawn	HIDDEN_WARP_NPC,{
	end;

OnBGStart:
	initnpctimer;
	end;

OnBGStop:
	stopnpctimer;
	end;

OnTimer24000:
	specialeffect(EF_SANCTUARY);
	end;

OnTimer25000:
	areapercentheal "bat_b01",2,282,17,297,100,100;
	areawarp "bat_b01",2,282,17,297,"bat_b01",87,75;
	initnpctimer;
	end;
}

// Flags
// *********************************************************************

bat_b01,304,231,1	duplicate(Guillaume camp#bat)	Guillaume camp#bat1	1_FLAG_LION
bat_b01,319,231,1	duplicate(Guillaume camp#bat)	Guillaume camp#bat2	1_FLAG_LION
bat_b01,304,218,1	duplicate(Guillaume camp#bat)	Guillaume camp#bat3	1_FLAG_LION
bat_b01,319,218,1	duplicate(Guillaume camp#bat)	Guillaume camp#bat4	1_FLAG_LION
bat_b01,304,231,1	duplicate(Guillaume camp#bat)	Guillaume camp#bat5	1_FLAG_LION
bat_b01,304,231,1	duplicate(Guillaume camp#bat)	Guillaume camp#bat6	1_FLAG_LION
bat_b01,335,142,1	duplicate(Guillaume camp#bat)	Guillaume camp#bat7	1_FLAG_LION
bat_b01,335,157,1	duplicate(Guillaume camp#bat)	Guillaume camp#bat8	1_FLAG_LION
bat_b01,390,16,1	duplicate(Guillaume camp#bat)	Guillaume camp#bat9	1_FLAG_LION
bat_b01,292,163,1	duplicate(Guillaume camp#bat)	Guillaume camp#bat19	1_FLAG_LION
bat_b01,292,136,1	duplicate(Guillaume camp#bat)	Guillaume camp#bat20	1_FLAG_LION
bat_b01,241,185,1	duplicate(Guillaume camp#bat)	Guillaume camp#bat21	1_FLAG_LION
bat_b01,247,179,1	duplicate(Guillaume camp#bat)	Guillaume camp#bat22	1_FLAG_LION

bat_b01,96,81,1	duplicate(Croix camp#bat)	Croix camp#bat1	1_FLAG_EAGLE
bat_b01,96,68,1	duplicate(Croix camp#bat)	Croix camp#bat2	1_FLAG_EAGLE
bat_b01,79,81,1	duplicate(Croix camp#bat)	Croix camp#bat3	1_FLAG_EAGLE
bat_b01,79,68,1	duplicate(Croix camp#bat)	Croix camp#bat4	1_FLAG_EAGLE
bat_b01,96,81,1	duplicate(Croix camp#bat)	Croix camp#bat5	1_FLAG_EAGLE
bat_b01,96,81,1	duplicate(Croix camp#bat)	Croix camp#bat6	1_FLAG_EAGLE
bat_b01,59,164,1	duplicate(Croix camp#bat)	Croix camp#bat7	1_FLAG_EAGLE
bat_b01,59,137,1	duplicate(Croix camp#bat)	Croix camp#bat8	1_FLAG_EAGLE
bat_b01,10,296,1	duplicate(Croix camp#bat)	Croix camp#bat9	1_FLAG_EAGLE
bat_b01,110,162,1	duplicate(Croix camp#bat)	Croix camp#bat18	1_FLAG_EAGLE
bat_b01,110,137,1	duplicate(Croix camp#bat)	Croix camp#bat19	1_FLAG_EAGLE
bat_b01,152,120,1	duplicate(Croix camp#bat)	Croix camp#bat20	1_FLAG_EAGLE
bat_b01,158,114,1	duplicate(Croix camp#bat)	Croix camp#bat21	1_FLAG_EAGLE
