// Battle Ground - Tierra Bossnia

-	script	BOSS_Tierra	-1,{
	end;

OnReadyCheck:
	if ($@eBG_start[BGT_BOSS])
		end;
		
	$@eBG_start[BGT_BOSS] = EBG_RUNNING;

	initnpctimer;
	.time_remaining = getvariableofnpc(.bg_time[BGT_BOSS], "BG_Settings");

	.Boss_[GUILLAUME] = 0;
	.Boss_[CROIX] = 0;
	bg_npc_data(0);
	donpcevent("boss_respawn#3::OnBGStart");
	donpcevent("boss_respawn#4::OnBGStart");
	bg_updatescore "ebg_boss",.Boss_[CROIX],.Boss_[GUILLAUME];
	sleep2 3000;
	donpcevent("Balance_Flag::OnBGStart");
	donpcevent("Guillaume_Boss#1::OnSummon");
	donpcevent("Croix_Boss#2::OnSummon");
	mapannounce("ebg_boss","-- Capture Balance Flag to remove inmunity of Enemy Guardians --",1,0x483D8B);
	end;

OnNeutralReset:
	bg_npc_data(0);
	killmonster("ebg_boss", "BOSS_Tierra::OnNeutralBreak");
	if (bg_get_npc_data(0, "Guillaume_Boss#1") > 0)
		bg_immunemob(bg_get_npc_data(0, "Guillaume_Boss#1"), 1);
	if (bg_get_npc_data(0, "Croix_Boss#2") > 0)
		bg_immunemob(bg_get_npc_data(0, "Croix_Boss#2"), 1);
	bg_monster(0, "ebg_boss", 273, 204, "Balance Flag", $bg_tierra_boss[10], "BOSS_Tierra::OnNeutralBreak");
	sleep 2000;
	if( $@eBG_start[BGT_BOSS] != EBG_RUNNING )
		end;
	mapannounce("ebg_boss","-- Balance Flag returned to normality --",1,0x483D8B);
	end;

OnNeutralBreak:
	if ($@eBG_start[BGT_BOSS] != EBG_RUNNING)
		end;
	bg_npc_data(getcharid(4));
	bg_addpoints(BOSS_NEUT_FLAG, 1, 1);

	.@boss1 = bg_get_npc_data(0, "Guillaume_Boss#1");
	.@boss2 = bg_get_npc_data(0, "Croix_Boss#2");

	// If other team got neutral, set immune to 0
	if (.@boss1 > 0) {
		bg_immunemob(.@boss1, (getcharid(4) == $@ebg_boss_BG_id2) ? 0 : 1);
	}
	if (.@boss2 > 0) {
		bg_immunemob(.@boss2, (getcharid(4) == $@ebg_boss_BG_id1) ? 0 : 1);
	}
	if (getcharid(4) == $@ebg_boss_BG_id1) {
		mapannounce("ebg_boss","-- Guillaume captured the Balance Flag. Crox guardian now is vulnerable to attacks!! --",1,0x0000FF);
		bg_monster bg_get_npc_data(),"ebg_boss",273,204,"Guillaume Flag",$bg_tierra_boss[11],"BOSS_Tierra::OnNeutralBreak";
	} else if (getcharid(4) == $@ebg_boss_BG_id2) {
		mapannounce("ebg_boss","-- Croix captured the Balance Flag. Guillaume guardian now is vulnerable to attacks!! --",1,0xFF0000);
		bg_monster bg_get_npc_data(),"ebg_boss",273,204,"Croix Flag",$bg_tierra_boss[12],"BOSS_Tierra::OnNeutralBreak";
	} else {
		bg_npc_data(0);
		bg_monster 0,"ebg_boss",273,204,"Balance Flag",$bg_tierra_boss[10],"BOSS_Tierra::OnNeutralBreak";
	}
	end;

OnPCDieEvent:
	if (strcharinfo(PC_MAP) == "ebg_boss") {
		.@team = callfunc("F_ValidateeBG", BGT_BOSS, 0);
		callfunc("F_warpDie", BGT_BOSS, .@team);
	}
	end;

OnTimer60000:
	.time_remaining--;
	if (.time_remaining != 0) {
		mapannounce("ebg_boss", "The Battle will end in "+ .time_remaining +" Minutes!!", bc_map, 0x483D8B);
		initnpctimer;
		end;
	}
OnMatchStop:
OnMatchEnd:
	stopnpctimer;
	donpcevent("Guillaume_Boss#1::OnTimeStop");
	donpcevent("Croix_Boss#2::OnTimeStop");
	donpcevent("boss_respawn#3::OnBGStop");
	donpcevent("boss_respawn#4::OnBGStop");
	donpcevent("Balance_Flag::OnBGStop");
	killmonster("ebg_boss", "Guillaume_Boss#1::OnBoss");
	killmonster("ebg_boss", "Croix_Boss#2::OnBoss");
	$@eBG_start[BGT_BOSS] =  EBG_ENDING;
	// Team Rewards
	// Note: Points are switched, lowest points win.
	.@win = callfunc("F_eBGReward", BGT_BOSS, $bg_tierra_boss, 2, .Boss_[CROIX], .Boss_[GUILLAUME], 1, 1, 0);
	if (.@win)
		mapannounce("ebg_boss","The "+ callfunc("F_eBGTeamName",.@win) +" army has won the Battle of Tierra Bossnia!",bc_map,callfunc("F_eBGTeamColor",.@win));
	else
		mapannounce("ebg_boss","The battle is over. This is a Tie...!", bc_map, 0x483D8B);
	
	// =======================================================
	bg_npc_data(0);
	.Boss_[GUILLAUME] =  0;
	.Boss_[CROIX] =  0;
	callfunc("F_EndeBG", "ebg_boss",2);
	end;
}

/* Boss Summoner */
-	script	CommonBoss#eBG		FAKE_NPC,{
end;

OnSummon:
	.@self = atoi(strnpcinfo(2));
	.@oppose = ((.@self==CROIX)?GUILLAUME:CROIX);
	.@teamName$ = callfunc("F_eBGTeamName",.@self);
	.@npcName$ = callfunc("F_eBGNPCName", .@self, BGT_BOSS);
	.@color = callfunc("F_eBGTeamColor",.@self);
	setarray .@x[0],176,345;
	if (.@self==CROIX)
		setarray .@x[0],167,50;
	
	if (.uid[.@self])
		callsub(OnTimerStop);
	.@Boss =  $bg_tierra_boss[13 + getvariableofnpc(.Boss_[.@self], "BOSS_Tierra")];
	mapannounce("ebg_boss","-- "+ .@teamName$ +" Guardian [" + strmobinfo(2,.@Boss) + "] entering the battle --", bc_map, .@color);
	.@Boss = bg_monster(getd("$@ebg_boss_BG_id"+.@self), "ebg_boss", .@x[0], .@x[1], .@self$+" Guardian", .@Boss, .@npcName$+"_Boss#"+.@self+"::OnBoss");
	bg_immunemob(.@Boss, 1);
	bg_npc_data(.@Boss, 0);
	bg_npc_data(.@color, 1);
	.uid[.@self] = bg_timer_start(2000,1);
	bg_npc_cond_timer(.uid[.@self],strnpcinfo(0),0,EBG_CONDITION_GREATER,EBG_OP_REVEAL_MOB);
	bg_operations_timer(.uid[.@self],EBG_OP_REVEAL_MOB);
	end;

OnTimeStop:
	.@i = 1;
OnTimerStop:
	.@self = atoi(strnpcinfo(2));
	if (.uid[.@self] == 0) {
		if (.@i)
			end;
		return;
	}
	bg_timer_stop(.uid[.@self]);
	.uid[.@self] = 0;
	if (.@i)
		end;
	return;

OnBoss:
	.@self = atoi(strnpcinfo(2));
	.@npcName$ = callfunc("F_eBGNPCName", .@self, BGT_BOSS);

	callsub(OnTimerStop);
	bg_addpoints(BOSS_KILLED, 1, 1);
	bg_revealmob(bg_get_npc_data(), 2, 0x0000FF); // Hide Spot
	bg_npc_data(0, 0);	// Set BossID as 0

	set(getvariableofnpc(.Boss_[.@self],"BOSS_Tierra"), getvariableofnpc(.Boss_[.@self], "BOSS_Tierra") + 1);
	bg_updatescore("ebg_boss", getvariableofnpc(.Boss_[GUILLAUME], "BOSS_Tierra"), getvariableofnpc(.Boss_[CROIX], "BOSS_Tierra"));

	if (getvariableofnpc(.Boss_[.@self], "BOSS_Tierra") >= $bg_tierra_boss[0]) {
		donpcevent("BOSS_Tierra::OnMatchEnd");
	} else {
		donpcevent("BOSS_Tierra::OnNeutralReset");
		donpcevent(.@npcName$ +"_Boss#"+ .@self +"::OnSummon");
	}
	end;
}

/* Duplicates */
-	duplicate(CommonBoss#eBG)	Guillaume_Boss#1	FAKE_NPC
-	duplicate(CommonBoss#eBG)	Croix_Boss#2	FAKE_NPC


/* Balance Flag */
-	script	Balance_Flag	-1,{
	end;

OnBGStart:
	viewpointmap "ebg_boss", 1, 273, 204, 1, 0xFFFFFF;
	bg_monster 0,"ebg_boss",273,204,"Balance Flag",$bg_tierra_boss[10],"BOSS_Tierra::OnNeutralBreak";
	callsub(OnTimerStart);
	end;

OnBGStop:
	callsub(OnTimerStop);
	viewpointmap "ebg_boss", 2, 273, 204, 1, 0xFFFFFF;
	killmonster "ebg_boss","BOSS_Tierra::OnNeutralBreak";
	end;

OnTimerStart:
	.uid = bg_timer_start(2000,1);
	bg_npc_cond_timer(.uid, "BOSS_Tierra", $@ebg_boss_BG_id1, EBG_CONDITION_EQUAL, EBG_OP_VIEWPOINT, 0);
	bg_npc_cond_timer(.uid, "BOSS_Tierra", $@ebg_boss_BG_id2, EBG_CONDITION_EQUAL, EBG_OP_VIEWPOINT, 1);
	bg_npc_cond_timer(.uid, "BOSS_Tierra", $@ebg_boss_BG_id1, EBG_CONDITION_ELSE, EBG_OP_VIEWPOINT, 2);
	bg_viewpoint_timer(.uid, "ebg_boss", 1, 273, 204, 1, -2);
	return;

OnTimerStop:
	bg_timer_stop(.uid);
	return;

}

// MapFlags

ebg_boss	mapflag	battleground	2
ebg_boss	mapflag	nomemo
ebg_boss	mapflag	nosave	SavePoint
ebg_boss	mapflag	noteleport
ebg_boss	mapflag	monster_noteleport
ebg_boss	mapflag	nowarp
ebg_boss	mapflag	nowarpto
ebg_boss	mapflag	noreturn
ebg_boss	mapflag	nobranch
ebg_boss	mapflag	nopenalty

// Flags

ebg_boss,148,85,1	duplicate(Flag#bat)	Croix camp#tb_1	1_FLAG_EAGLE
ebg_boss,155,85,1	duplicate(Flag#bat)	Croix camp#tb_2	1_FLAG_EAGLE
ebg_boss,357,75,1	duplicate(Flag#bat)	Croix camp#tb_3	1_FLAG_EAGLE
ebg_boss,348,74,1	duplicate(Flag#bat)	Croix camp#tb_4	1_FLAG_EAGLE
ebg_boss,199,49,1	duplicate(Flag#bat)	Croix camp#tb_5	1_FLAG_EAGLE
ebg_boss,168,16,1	duplicate(Flag#bat)	Croix camp#tb_6	1_FLAG_EAGLE
ebg_boss,138,12,1	duplicate(Flag#bat)	Croix camp#tb_7	1_FLAG_EAGLE
ebg_boss,108,35,1	duplicate(Flag#bat)	Croix camp#tb_8	1_FLAG_EAGLE
ebg_boss,164,308,1	duplicate(Flag#bat)	Guillaume camp#tb_1	1_FLAG_LION
ebg_boss,157,308,1	duplicate(Flag#bat)	Guillaume camp#tb_2	1_FLAG_LION
ebg_boss,359,327,1	duplicate(Flag#bat)	Guillaume camp#tb_3	1_FLAG_LION
ebg_boss,350,326,1	duplicate(Flag#bat)	Guillaume camp#tb_4	1_FLAG_LION
ebg_boss,209,344,1	duplicate(Flag#bat)	Guillaume camp#tb_5	1_FLAG_LION
ebg_boss,173,380,1	duplicate(Flag#bat)	Guillaume camp#tb_6	1_FLAG_LION
ebg_boss,150,380,1	duplicate(Flag#bat)	Guillaume camp#tb_7	1_FLAG_LION
ebg_boss,118,357,1	duplicate(Flag#bat)	Guillaume camp#tb_8	1_FLAG_LION
ebg_boss,119,336,1	duplicate(Flag#bat)	Guillaume camp#tb_9	1_FLAG_LION

// Battleground Therapist

ebg_boss,53,377,3	duplicate(Therapist#eBG)	Therapist in battle#tb1	4_F_SISTER
ebg_boss,45,18,3	duplicate(Therapist#eBG)	Therapist in battle#tb2	4_F_SISTER

// Battleground Respawn

ebg_boss,50,374,0	duplicate(respawn_script#bg)	boss_respawn#3	HIDDEN_WARP_NPC	//#gtb_respawn
ebg_boss,42,16,0	duplicate(respawn_script#bg)	boss_respawn#4	HIDDEN_WARP_NPC	//#ctb_respawn
