// Battle Ground - Flavius Team DeathMatch(TDM)

-	script	TDM_Flavius	-1,{
	end;
	
OnPCDieEvent:
	.@die = true;
OnPCLogoutEvent:
	if (strcharinfo(PC_MAP) == "ebg_td" && $@eBG_start[BGT_TD] == EBG_RUNNING) {
		.@team = callfunc("F_ValidateeBG",BGT_TD,0);
		if (.@team != GUILLAUME && .@team != CROIX)
			end;
		callsub(OnTeamOff, .@die, .@team);
		callfunc("F_warpDie", BGT_TD, .@team);
	}
	end;
OnTeam1Off:
	callsub(OnTeamOff, false, GUILLAUME);
	end;
OnTeam2Off:
	callsub(OnTeamOff, false, CROIX);
	end;

OnTeamOff:
	.@die = getarg(0);
	.@team = getarg(1);
	.@account_id = getcharid(3);
	if (.@team == GUILLAUME)
		.Team1_Score--;
	else if (.@team == CROIX)
		.Team2_Score--;
	else
		end;
	if (.@die == true) {
		bg_addpoints BG_TD_DIE, 1, 1;
		if (attachrid(killerrid)) {
			bg_addpoints BG_TD_KILL, 1, 1;
		}
	}
	attachrid(.@account_id);
	callsub(OnValidateScore, 0);
	return;


OnReadyCheck:
	if ($@eBG_start[BGT_TD])
		end;

	$@eBG_start[BGT_TD] = EBG_RUNNING;
	.time_remaining = getvariableofnpc(.bg_time[BGT_TD], "BG_Settings");
	
	initnpctimer;
	.Team1_Score = $bg_flavius_td[0];
	.Team2_Score = $bg_flavius_td[0];
	bg_updatescore "ebg_td", .Team1_Score, .Team2_Score;
	sleep 2000;
	bg_team_reveal $@ebg_td_BG_id1;
	bg_team_reveal $@ebg_td_BG_id2;
	donpcevent("td_respawn#10::OnBGStart");
	donpcevent("td_respawn#11::OnBGStart");
	end;

OnValidateScore:
	if ($@eBG_start[BGT_TD] != EBG_RUNNING)
		return;
	
	.@timer = getarg(0, 0);	// 1 = Time Reached, Tie

	bg_updatescore "ebg_td", .Team1_Score, .Team2_Score;

	/**
	 * 2 Teams, AddScore = 0, StartingIndex = 1, CheckMethod(2)
	 * CM: If Time is reached, it's a Tie, Team with most players win.
	 */
	.@win = callfunc("F_eBGReward", BGT_TD, $bg_flavius_td, 2, .Team1_Score, .Team2_Score, 0, 1, 2, .@timer);
	if (.@win == -1)
		return;
	if (.@win > 0) {
		mapannounce("ebg_td", "The " + callfunc("F_eBGTeamName", .@win) + " army has won the Battle of Flavius TD!", bc_map, callfunc("F_eBGTeamColor", .@win));
	} else {
		mapannounce("ebg_td", "The battle is over. There are no Winners!", bc_map, 0x808000);
	}
	donpcevent("TDM_Flavius::OnMatchEnd");
	return;

OnTimer60000:
	.time_remaining--;
	if (.time_remaining != 0) {
		mapannounce("ebg_td", "The Battle will end in "+ .time_remaining +" Minutes!!", bc_map, 0x808000);
		initnpctimer;
		end;
	}
	callsub(OnValidateScore, 1);
	end;

OnMatchStop:
OnMatchEnd:
	stopnpctimer;
	donpcevent("td_respawn#10::OnBGStop");
	donpcevent("td_respawn#11::OnBGStop");
	
	$@eBG_start[BGT_TD] = EBG_ENDING;
	
	.Team1_Score = $bg_flavius_td[0];
	.Team2_Score = $bg_flavius_td[0];
	
	sleep 5000;
	bg_updatescore "ebg_td", $bg_flavius_td[0], $bg_flavius_td[0];
	
	callfunc("F_EndeBG", "ebg_td", 2);
	end;
}

// Battleground Therapist
ebg_td,390,13,3	duplicate(Therapist#eBG)	Therapist in battle#td1	4_F_SISTER
ebg_td,10,293,3	duplicate(Therapist#eBG)	Therapist in battle#td2	4_F_SISTER

// Battleground Respawn

ebg_td,390,10,1	duplicate(respawn_script#bg)	td_respawn#10	HIDDEN_WARP_NPC	//#guitd_respawn
ebg_td,10,290,1	duplicate(respawn_script#bg)	td_respawn#11	HIDDEN_WARP_NPC	//#crotd_respawn

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

ebg_td,304,231,1	duplicate(Flag#bat)	Alpha Base#td_1	1_FLAG_LION
ebg_td,319,231,1	duplicate(Flag#bat)	Alpha Base#td_2	1_FLAG_LION
ebg_td,304,218,1	duplicate(Flag#bat)	Alpha Base#td_3	1_FLAG_LION
ebg_td,319,218,1	duplicate(Flag#bat)	Alpha Base#td_4	1_FLAG_LION
ebg_td,304,231,1	duplicate(Flag#bat)	Alpha Base#td_5	1_FLAG_LION
ebg_td,304,231,1	duplicate(Flag#bat)	Alpha Base#td_6	1_FLAG_LION
ebg_td,335,142,1	duplicate(Flag#bat)	Alpha Base#td_7	1_FLAG_LION
ebg_td,335,157,1	duplicate(Flag#bat)	Alpha Base#td_8	1_FLAG_LION
ebg_td,390,16,1	duplicate(Flag#bat)	Alpha Base#td_9	1_FLAG_LION
ebg_td,292,163,1	duplicate(Flag#bat)	Alpha Base#td_10	1_FLAG_LION
ebg_td,292,136,1	duplicate(Flag#bat)	Alpha Base#td_11	1_FLAG_LION
ebg_td,241,185,1	duplicate(Flag#bat)	Alpha Base#td_12	1_FLAG_LION
ebg_td,247,179,1	duplicate(Flag#bat)	Alpha Base#td_13	1_FLAG_LION

ebg_td,96,81,1	duplicate(Flag#bat)	Omega Base#td_1	1_FLAG_EAGLE
ebg_td,96,68,1	duplicate(Flag#bat)	Omega Base#td_2	1_FLAG_EAGLE
ebg_td,79,81,1	duplicate(Flag#bat)	Omega Base#td_3	1_FLAG_EAGLE
ebg_td,79,68,1	duplicate(Flag#bat)	Omega Base#td_4	1_FLAG_EAGLE
ebg_td,96,81,1	duplicate(Flag#bat)	Omega Base#td_5	1_FLAG_EAGLE
ebg_td,96,81,1	duplicate(Flag#bat)	Omega Base#td_6	1_FLAG_EAGLE
ebg_td,59,164,1	duplicate(Flag#bat)	Omega Base#td_7	1_FLAG_EAGLE
ebg_td,59,137,1	duplicate(Flag#bat)	Omega Base#td_8	1_FLAG_EAGLE
ebg_td,10,296,1	duplicate(Flag#bat)	Omega Base#td_9	1_FLAG_EAGLE
ebg_td,110,162,1	duplicate(Flag#bat)	Omega Base#td_10	1_FLAG_EAGLE
ebg_td,110,137,1	duplicate(Flag#bat)	Omega Base#td_11	1_FLAG_EAGLE
ebg_td,152,120,1	duplicate(Flag#bat)	Omega Base#td_12	1_FLAG_EAGLE
ebg_td,158,114,1	duplicate(Flag#bat)	Omega Base#td_13	1_FLAG_EAGLE

// MapFlags
// *********************************************************************

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