// ==============================================================================
// BattleGround System - Tierra Valley 2
// ==============================================================================

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

bat_room,114,227,4	script	Registration::TV2R_Guillaume	418,{
	end;
OnInit:
	waitingroom "Battle Station 10 Players",10,"Tierra_BG2::OnGuillaumeJoin",1;
	end;
OnEnterBG:
	set $@TierraBG2_id1, waitingroom2bg("bat_a02",50,374,"Tierra_BG2::OnGuillaumeQuit","");
	end;
}

bat_room,114,204,0	script	Registration::TV2R_Croix	414,{
	end;
OnInit:
	waitingroom "Battle Station 10 Players",10,"Tierra_BG2::OnCroixJoin",1;
	end;
OnEnterBG:
	set $@TierraBG2_id2, waitingroom2bg("bat_a02",42,16,"Tierra_BG2::OnCroixQuit","");
	end;
}

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

-	script	Tierra_BG2	-1,{
	end;

OnInit:
	disablenpc "Croix Vintenar#tv2";
	disablenpc "Guillaume Vintenar#tv2";
	disablenpc "Therapist in battle#tv21";
	disablenpc "Therapist in battle#tv22";
	end;

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

OnGuillaumeJoin:
OnCroixJoin:
	if( $@TierraBG2 == 0 )
		donpcevent "Tierra_BG2::OnReadyCheck";
	end;

OnReadyCheck:
	if( $@TierraBG2 )
		end;
	set .@Guillaume, getwaitingroomstate(0,"TV2R_Guillaume");
	set .@Croix, getwaitingroomstate(0,"TV2R_Croix");

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

	set $@TierraBG2, 1;
	donpcevent "TV2R_Croix::OnEnterBG";
	donpcevent "TV2R_Guillaume::OnEnterBG";
	donpcevent "Guillaume_TV2B::OnBuild";
	donpcevent "Croix_TV2B::OnBuild";
	bg_monster $@TierraBG2_id1,"bat_a02",176,345,"Food Depot",1909,"Tierra_BG2::OnGuillaumeBreak";
	bg_monster $@TierraBG2_id2,"bat_a02",167,50,"Food Storage",1910,"Tierra_BG2::OnCroixBreak";
	bg_monster 0,"bat_a02",273,204,"Neutrality Flag",1911,"Tierra_BG2::OnNeutralBreak";
	
	set .Neutral_Base, 0;
	set $@TierraBG2_Victory, 0;

	set .Guardian_1, 0;
	set .Guardian_2, 0;
	set .Guardian_3, 0;

	enablenpc "Therapist in battle#tv21";
	enablenpc "Therapist in battle#tv22";
	disablenpc "Croix Vintenar#tv2";
	disablenpc "Guillaume Vintenar#tv2";
	// Respawner
	donpcevent "#gtv2_respawn::OnBGStart";
	donpcevent "#ctv2_respawn::OnBGStart";
	donpcevent "#ntv2_respawn::OnBGStart";
	// Warp Teams
	announce "Battleground -- Tierra Valley [80-99] has started!",0,0xF4A460;
	bg_warp $@TierraBG2_id1,"bat_a02",353,344;
	bg_warp $@TierraBG2_id2,"bat_a02",353,52;
	// Final Messages
	sleep 6000;
	mapannounce "bat_a02","Guillaume Vintenar Axl Rose : Let's attack to burn down Croix's food storage",0,0x0000FF;
	sleep 2000;
	mapannounce "bat_a02","Croix Vintenar Swandery : Master of Valhalla! Let us be gifted with unfailing faith and courage",0,0xFF0000;
	
	initnpctimer;
	end;

OnNeutralBreak:
	if( getcharid(4) == $@TierraBG2_id1 )
	{
		mapannounce "bat_a02","Guillaume obtained a neurality banner, so they have an advantage.",0,0x0000FF;
		bg_team_setxy $@TierraBG2_id2,42,16;
		areawarp "bat_a02",52,208,61,217,"bat_a02",42,16;
	}
	else if( getcharid(4) == $@TierraBG2_id2 )
	{
		mapannounce "bat_a02","Croix obtained a neutrality banner, so they have an advantage.",0,0xFF0000;
		bg_team_setxy $@TierraBG2_id1,50,374;
		areawarp "bat_a02",52,208,61,217,"bat_a02",50,374;
	}
	else end;

	set .Neutral_Base, getcharid(4);
	bg_team_setxy .Neutral_Base,56,212;

	if( .Guardian_1 == 0 )
		set .Guardian_1, bg_monster(.Neutral_Base,"bat_a02",280,233,"Guardian",1949,"Tierra_BG2::OnGuardian1");
	else
		bg_monster_set_team .Guardian_1, .Neutral_Base;
	
	if( .Guardian_2 == 0 )
		set .Guardian_2, bg_monster(.Neutral_Base,"bat_a02",287,203,"Guardian",1950,"Tierra_BG2::OnGuardian2");
	else
		bg_monster_set_team .Guardian_2, .Neutral_Base;

	if( .Guardian_3 == 0 )
		set .Guardian_3, bg_monster(.Neutral_Base,"bat_a02",268,204,"Guardian",1949,"Tierra_BG2::OnGuardian3");
	else
		bg_monster_set_team .Guardian_3, .Neutral_Base;

	sleep 10000;
	if( $@TierraBG2 != 1 ) end;

	if( .Neutral_Base == $@TierraBG2_id1 )
		bg_monster .Neutral_Base,"bat_a02",273,204,"Guillaume Flag",1912,"Tierra_BG2::OnNeutralBreak";
	else if( .Neutral_Base == $@TierraBG2_id2 )
		bg_monster .Neutral_Base,"bat_a02",273,204,"Croix Flag",1913,"Tierra_BG2::OnNeutralBreak";
	end;

OnGuardian1: set .Guardian_1, 0; end;
OnGuardian2: set .Guardian_2, 0; end;
OnGuardian3: set .Guardian_3, 0; end;

OnGuillaumeBreak:
	set $@TierraBG2_Victory, 2;
	mapannounce "bat_a02","Croix Vintenar Swandery: We destroyed Guillaume's food storehouse. We won that! Wow!",0,0xFF0000;
	donpcevent "Tierra_BG2::OnStop";
	end;

OnCroixBreak:
	set $@TierraBG2_Victory, 1;
	mapannounce "bat_a02","Guillaume Vintenar Axl Rose: We destroyed Croix's food storehouse. We won that! Wow!",0,0x0000FF;
	donpcevent "Tierra_BG2::OnStop";
	end;

OnTimer2400000:
	mapannounce "bat_a02","Battle of Tierra Gorge will ends in 5 minutes",0,0xF4A460;
	end;
OnTimer2640000:
	mapannounce "bat_a02","Battle of Tierra Gorge will ends in 1 minute",0,0xF4A460;
	end;

OnTimer2700000:
	set $@TierraBG2_Victory, 3; // Draw Game
	mapannounce "bat_a02","Battle of Tierra Gorge is over. The time is out, this is a Tie",0,0xF4A460;
	donpcevent "Tierra_BG2::OnStop";
	end;

OnStop:
	set $@TierraBG2, 2;
	disablenpc "Therapist in battle#tv21";
	disablenpc "Therapist in battle#tv22";
	donpcevent "#gtv2_respawn::OnBGStop";
	donpcevent "#ctv2_respawn::OnBGStop";
	donpcevent "#ntv2_respawn::OnBGStop";
	enablenpc "Croix Vintenar#tv2";
	enablenpc "Guillaume Vintenar#tv2";
	killmonster "bat_a02","Tierra_BG2::OnGuillaumeBreak";
	killmonster "bat_a02","Tierra_BG2::OnCroixBreak";
	killmonster "bat_a02","Tierra_BG2::OnNeutralBreak";
	killmonster "bat_a02","Tierra_BG2::OnGuardian1";
	killmonster "bat_a02","Tierra_BG2::OnGuardian2";
	killmonster "bat_a02","Tierra_BG2::OnGuardian3";
	stopnpctimer;
	sleep 3000;
	bg_warp $@TierraBG2_id1,"bat_a02",50,374;
	bg_warp $@TierraBG2_id2,"bat_a02",42,16;
	sleep 3000;
	mapannounce "bat_a02","Battle of Tierra Gorge will close in 1 minute!",0,0xF4A460;
	initnpctimer;
	end;

OnTimer30000:
	if( $@TierraBG2 == 2 )
		mapannounce "bat_a02","Battle of Tierra Valley will close in 30 seconds!",0,0xF4A460;
	end;
OnTimer50000:
	if( $@TierraBG2 == 2 )
		mapannounce "bat_a02","Battle of Tierra Valley will close in 10 seconds!",0,0xF4A460;
	end;

OnTimer60000:
	if( $@TierraBG2 != 2 )
		end;
OnReset:
	stopnpctimer;
	set .Neutral_Base, 0;
	disablenpc "Croix Vintenar#tv2";
	disablenpc "Guillaume Vintenar#tv2";
	disablenpc "Therapist in battle#tv21";
	disablenpc "Therapist in battle#tv22";
	donpcevent "#gtv2_respawn::OnBGStop";
	donpcevent "#gtv2_respawn::OnBGStop";
	donpcevent "#ctv2_respawn::OnBGStop";

	killmonster "bat_a02","Tierra_BG2::OnGuillaumeBreak";
	killmonster "bat_a02","Tierra_BG2::OnCroixBreak";
	killmonster "bat_a02","Tierra_BG2::OnNeutralBreak";
	killmonster "bat_a02","Tierra_BG2::OnGuardian1";
	killmonster "bat_a02","Tierra_BG2::OnGuardian2";
	killmonster "bat_a02","Tierra_BG2::OnGuardian3";
	donpcevent "Guillaume_TV2B::OnDestroy";
	donpcevent "Croix_TV2B::OnDestroy";

	set $@TierraBG2_Victory, 0;
	if( $@TierraBG2_id1 ) { bg_destroy $@TierraBG2_id1; set $@TierraBG2_id1, 0; }
	if( $@TierraBG2_id2 ) { bg_destroy $@TierraBG2_id2; set $@TierraBG2_id2, 0; }
	sleep 1000;
	mapwarp "bat_a02","bat_room",155,150;
	sleep 2000;
	maprespawnguildid "bat_a02",0,3; // Just in case someone else
	sleep 2000;
	set $@TierraBG2, 0;
	donpcevent "Tierra_BG2::OnReadyCheck"; // Maybe a game is ready to start
	end;
}

// Other Flags
// *********************************************************************

bat_a02,148,85,1	duplicate(Croix camp#bat)	Croix camp#bat22	974
bat_a02,155,85,1	duplicate(Croix camp#bat)	Croix camp#bat23	974
bat_a02,357,75,1	duplicate(Croix camp#bat)	Croix camp#bat24	974
bat_a02,348,74,1	duplicate(Croix camp#bat)	Croix camp#bat25	974
bat_a02,199,49,1	duplicate(Croix camp#bat)	Croix camp#bat26	974
bat_a02,168,16,1	duplicate(Croix camp#bat)	Croix camp#bat27	974
bat_a02,138,12,1	duplicate(Croix camp#bat)	Croix camp#bat28	974
bat_a02,108,35,1	duplicate(Croix camp#bat)	Croix camp#bat29	974
bat_a02,164,308,1	duplicate(Guillaume camp#bat)	Guillaume camp#bat23	973
bat_a02,157,308,1	duplicate(Guillaume camp#bat)	Guillaume camp#bat24	973
bat_a02,359,327,1	duplicate(Guillaume camp#bat)	Guillaume camp#bat25	973
bat_a02,350,326,1	duplicate(Guillaume camp#bat)	Guillaume camp#bat26	973
bat_a02,209,344,1	duplicate(Guillaume camp#bat)	Guillaume camp#bat27	973
bat_a02,173,380,1	duplicate(Guillaume camp#bat)	Guillaume camp#bat28	973
bat_a02,150,380,1	duplicate(Guillaume camp#bat)	Guillaume camp#bat29	973
bat_a02,118,357,1	duplicate(Guillaume camp#bat)	Guillaume camp#bat30	973
bat_a02,119,336,1	duplicate(Guillaume camp#bat)	Guillaume camp#bat31	973

// Barricades
// *********************************************************************

-	script	Guillaume_TV2B	-1,{
	end;

OnBuild:
	for( set .@i, 0; .@i < 16; set .@i, .@i + 1 )
		bg_monster 0,"bat_a02",170+.@i,130,"Barricade",1906,"Guillaume_TV2B::OnWall";

	setwall "bat_a02",170,130,16,6,1,"bat_a02_g1";
	set .MyMobCount,16;
	end;

OnDestroy:
	killmonster "bat_a02","Guillaume_TV2B::OnWall";
	delwall "bat_a02_g1";
	set .MyMobCount,0;
	end;

OnWall:
	if( set(.MyMobCount,.MyMobCount - 1) < 1 )
	{
		delwall "bat_a02_g1";
		mapannounce "bat_a02","South Gate : The Gate has been Destroy!!",0,0xF4A460;
	}
	end;
}

-	script	Croix_TV2B	-1,{
	end;

OnBuild:
	for( set .@i, 0; .@i < 16; set .@i, .@i + 1 )
		bg_monster 0,"bat_a02",186+.@i,266,"Barricade",1906,"Croix_TV2B::OnWall";
	
	setwall "bat_a02",186,266,16,6,1,"bat_a02_c1";
	set .MyMobCount,16;
	end;

OnDestroy:
	killmonster "bat_a02","Croix_TV2B::OnWall";
	delwall "bat_a02_c1";
	set .MyMobCount,0;
	end;

OnWall:
	if( set(.MyMobCount,.MyMobCount - 1) < 1 )
	{
		delwall "bat_a02_c1";
		mapannounce "bat_a02","North Gate : A Gate has been Destroy!!",0,0xF4A460;
	}
	end;
}

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

bat_a02,45,19,3	script	Croix Vintenar#tv2	415,{
	if( $@TierraBG2_Victory )
	{
		if( $@TierraBG2_Victory == Bat_Team )
		{ // Victory
			set .@Reward, 3;
			mes "[Swandery]";
			mes "Blessed Croax!!";
			mes "Let's enjoy our glorious victory!";
			mes "" + strcharinfo(0) + ", its a sign reflecting victory";
			close2;
		}
		else
		{ // 
			set .@Reward, 1;
			mes "[Swandery]";
			mes "Oh, " + strcharinfo(0) + ". 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;
		}

		setquest 2069;
		getitem 7828, .@Reward;
		bg_leave;
		warp "bat_room",155,150;
		end;
	}
	end;
}

bat_a02,53,377,3	script	Guillaume Vintenar#tv2	419,{
	if( $@TierraBG2_Victory )
	{
		if( $@TierraBG2_Victory == Bat_Team )
		{ // Victory
			set .@Reward, 3;
			mes "[Swandery]";
			mes "Blessed Guillaume!!";
			mes "Let's enjoy our glorious victory!";
			mes "" + strcharinfo(0) + ", its a sign reflecting victory";
			close2;
		}
		else
		{ // 
			set .@Reward, 1;
			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;
		}

		setquest 2069;
		getitem 7828, .@Reward;
		bg_leave;
		warp "bat_room",155,150;
		end;
	}
	end;
}

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

bat_a02,60,216,3	script	Ghost#tv23	950,{
	mes "[Ghost in valley]";
	mes "Boo...Boo...";
	specialeffect2 312;
	close;
}

bat_a02,53,377,3	script	Therapist in battle#tv22	95,{
	mes "[Therapist in battle]";
	mes "Just close your eyes, and take a deep breathe.";
	mes "You can be free from pain.";
	specialeffect2 312;
	close;
}

bat_a02,45,18,3	script	Therapist in battle#tv21	95,{
	mes "[Therapist in battle]";
	mes "Just close your eyes, and take a deep breathe.";
	mes "You can be free from pain.";
	specialeffect2 312;
	close;
}

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

bat_a02,57,213,0	script	#ntv2_respawn	139,{
	end;

OnBGStart:
	initnpctimer;
	end;

OnBGStop:
	stopnpctimer;
	end;

OnTimer19000:
	misceffect 83;
	end;

OnTimer20000:
	areapercentheal "bat_a02",52,208,61,217,100,100;
	areawarp "bat_a02",52,208,61,217,"bat_a02",301,208;
	initnpctimer;
	end;
}

bat_a02,50,374,0	script	#gtv2_respawn	139,{
	end;

OnBGStart:
	initnpctimer;
	end;

OnBGStop:
	stopnpctimer;
	end;

OnTimer24000:
	misceffect 83;
	end;

OnTimer25000:
	areapercentheal "bat_a02",46,370,54,378,100,100;
	areawarp "bat_a02",46,370,54,378,"bat_a02",354,340;
	initnpctimer;
	end;
}

bat_a02,42,16,0	script	#ctv2_respawn	139,{
	end;

OnBGStart:
	initnpctimer;
	end;

OnBGStop:
	stopnpctimer;
	end;

OnTimer24000:
	misceffect 83;
	end;

OnTimer25000:
	areapercentheal "bat_a02",38,12,47,21,100,100;
	areawarp "bat_a02",38,12,47,21,"bat_a02",354,57;
	initnpctimer;
	end;
}
