// Battle Ground - Flavius CTF
// Guillaume = Team 1(Blue), Croix = Team2(Red)

ebg_room,57,74,0	duplicate(Register#eBG_Fake00)	Register#ebg_ctf01	4_M_KY_KNT
ebg_room,57,97,4	duplicate(Register#eBG_Fake00)	Register#ebg_ctf02	4_M_CRU_KNT

/*
	BG_Flag#eBG
	Guillaume Flag#1
	Croix Flag#2
*/

-	script	BG_Flag#eBG	FAKE_NPC,{
	end;


OnTimerStartE:
	.@end = true;
	// Fallthrough
OnTimerStart:
	.@id = atoi(strnpcinfo(2));

	if (.@id <= 0)
		end;

	if (.uid[.@id] > 0) // Timer Already Running, Stop it first.
		callsub(OnTimerStop);
	
	.uid[.@id] = bg_timer_start(2000, 1);
	bg_npc_cond_timer(.uid[.@id], strnpcinfo(0), 2, EBG_CONDITION_GREATER, EBG_OP_EFFECT | EBG_OP_VIEWPOINT);
	bg_effect_timer(.uid[.@id], EF_GANBANTEIN);
	bg_viewpoint_timer(.uid[.@id], "ebg_ctf", 1, -1, -1, .@id, callfunc("F_eBGTeamColor_VP", .@id));
	if (.@end == true)
		end;
	return;
	
OnTimeStop:
	.@i = true;
	// Fallthrough
OnTimerStop:
	.@id = atoi(strnpcinfo(2));
	viewpointmap "ebg_ctf",2, 0, 0, .@id, callfunc("F_eBGTeamColor_VP", .@id);
	if (.uid[.@id] == 0) {
		if (.@i == true)
			end;
		return;
	}
	bg_timer_stop(.uid[.@id]);
	.uid[.@id] = 0;

	if (.@i == true)
		end;
	return;
	
OnTouch:
	if ($@eBG_start[BGT_CTF] != EBG_RUNNING || Hp < 1)
		end;
		
	.@self = atoi(strnpcinfo(2));
	.@oppose = ((.@self==CROIX)? GUILLAUME : CROIX);
	.@team_$ = ((.@self==CROIX)? "Croix" : "Guillaume");
	.@team$ = callfunc("F_eBGTeamName", .@self);
	.@oppose$ = callfunc("F_eBGTeamName", .@oppose);
	.@color = callfunc("F_eBGTeamColor", .@self);

	// Opposing Team got the Flag
	if (@BG_TeamID == getd("$@ebg_ctf_BG_id"+ .@oppose) && bg_get_npc_data() < CTF_TAKEN) { // Flag Captured by Opposite Team.
		// Set Flag status as Taken
		bg_npc_data(CTF_TAKEN);
		// Set CharacterID of who got the flag
		.Flag_Carry[.@self] =  getcharid(0);
		//end All Invisibility Status.
		sc_end(SC_HIDING); sc_end(SC_CLOAKING); sc_end(SC_CHASEWALK); sc_end(SC_CLOAKINGEXCEED); sc_end(SC_CAMOUFLAGE); sc_end(SC__INVISIBILITY);
		pc_block_si(PC_BLOCK_ALL, 1); // Block Skills/Items, so can only move.
		mapannounce("ebg_ctf", .@team$ +" Crystal Taken by ["+ strcharinfo(0) +"]", bc_map, callfunc("F_eBGTeamColor", .@oppose));
		// Add Points and disable NPC
		bg_addpoints(CTF_ONHAND, 1, 1);
		disablenpc(.@team_$+" Flag#"+ .@self);
		// Start Character Timer and stop NPC Timer
		bg_hpsp_time($bg_flavius_ctf[1], $bg_flavius_ctf[2], $bg_flavius_ctf[3], 1);
		bg_hpsp_time_extra(EF_BOWLINGBASH, e_hlp, .@self, callfunc("F_eBGTeamColor_VP", .@self));
		callsub(OnTimerStop);
	// Same Team Returning the Flag
	} else if (@BG_TeamID == getd("$@ebg_ctf_BG_id"+ .@self)) {
		// If Team B reached it's base and Team B Flag is present
		if (bg_get_npc_data() == CTF_UNTOUCHED && .Flag_Carry[.@oppose] == getcharid(0)) {
			// Reset the Flag Variables
			.Flag_Carry[.@oppose] = CTF_UNTOUCHED;	
			.Flag_Carry[.@self] =  CTF_UNTOUCHED;
			mapannounce("ebg_ctf", .@oppose$ +" Crystal Captured by ["+ strcharinfo(0) +"]!!", bc_map, .@color); // Announce Team2 Captured Team1 Flag
			// Add Poins
			bg_addpoints(CTF_CAPTURED, 1, 1);
			// Delete Character Timer and start NPC Timer
			bg_hpsp_time_del(EBG_HP_TIME | EBG_SP_TIME);
			// UnBlock Skills/Items
			pc_block_si(PC_BLOCK_ALL, 0);
			callsub(OnTeamScore, .@self); // Update Self Score
			callsub(OnTimerStart);
		// Team B Flag is on ground and teammates reaches near flag.
		} else if (bg_get_npc_data() == CTF_ONGROUND) {
			mapannounce("ebg_ctf", .@team$ + " Crystal Returned by ["+ strcharinfo(0) +"]!!", bc_map, .@color); // Flag Returns
			// Add Fame
			bg_addfame();
			disablenpc(.@team_$ +" Flag#"+ .@self); // Disable Own Flag
			sleep(2000);
			// Move Flag and Start Timer	
			callsub(OnMoveNPC, .@self, .@team_$);
			// Set Flag Status as not captured
			bg_npc_data(CTF_UNTOUCHED);
			callsub(OnTimerStart);
		}
	}
	end;

OnTeamScore:	//Team 1/2 Captured the Flag.
	.@team = getarg(0);
	donpcevent("CTF_Flavius::OnTeam"+ .@team +"Score");
	donpcevent("CTF_Flavius::OnMatchStop");
	return;

OnMoveNPC:
	.@self = getarg(0);
	.@team$ = getarg(1);
	switch(.@self){
		case GUILLAUME:
			movenpc .@team$ +" Flag#"+ .@self, 328, 150;
			break;
		case CROIX:
			movenpc .@team$ +" Flag#"+ .@self, 62, 150;
			break;
	}
	enablenpc .@team$ +" Flag#"+ .@self;
	return;
	
OnBase:
	.@self = atoi(strnpcinfo(2));
	.@team$ = ((.@self==CROIX)? "Croix" : "Guillaume");
	callsub(OnMoveNPC, .@self, .@team$);
	bg_npc_data(CTF_UNTOUCHED);
	.Flag_Carry[.@self] =  0;
	callsub(OnTimerStart);
	end;
}

ebg_ctf,328,150,1	duplicate(BG_Flag#eBG)	Guillaume Flag#1	OBJ_A2,1,1
ebg_ctf,62,150,1	duplicate(BG_Flag#eBG)	Croix Flag#2	OBJ_B2,1,1

// Battleground Core

-	script	CTF_Flavius	FAKE_NPC,{
	end;

OnInit:	// Disable Flags on Load
	disablenpc("Guillaume Flag#1");
	disablenpc("Croix Flag#2");
	end;
	
OnTeam1Score:
	.Team1_Score += 1;
	end;

OnTeam2Score:
	.Team2_Score += 1;
	end;
	
OnGuillaumeJoin:
	@BG_TeamJoin = GUILLAUME;
	doevent("BG_Settings::OnJoinBG2");
	end;
OnCroixJoin:
	@BG_TeamJoin = CROIX;
	doevent("BG_Settings::OnJoinBG2");
	end;
	
OnPCLogoutEvent:
OnPCDieEvent:
	if (strcharinfo(PC_MAP) == "ebg_ctf") {
		.@team = callfunc("F_ValidateeBG", BGT_CTF, 0);
		switch(.@team) {
			case GUILLAUME:
				callsub(OnTeamDie, .@team, CROIX, "Croix");
				break;
			case CROIX:
				callsub(OnTeamDie, .@team, GUILLAUME, "Guillaume");
				break;
		}
	}
	end;
OnTeam1Off:
	callsub(OnTeamDie, GUILLAUME, CROIX, "Croix", 0, @ebg_x, @ebg_y);
	end;
OnTeam2Off:
	callsub(OnTeamDie, CROIX, GUILLAUME, "Guillaume", 0, @ebg_x, @ebg_y);
	end;

OnTeamDie:
	.@self = getarg(0);
	.@oppose = getarg(1);
	.@oppose$ = getarg(2);
	.@warp = getarg(3, 1);
	.@x = getarg(4, -1);
	.@y = getarg(5, -1);
	// Team Member Quit/Die while carrying the flag
	if ($@eBG_start[BGT_CTF] == EBG_RUNNING && getvariableofnpc(.Flag_Carry[.@oppose], "BG_Flag#eBG") == getcharid(0)) {
		// Nobody carry flag now
		set(getvariableofnpc(.Flag_Carry[.@oppose], "BG_Flag#eBG"), 0);
		// Unblock Skills/Item Usage
		pc_block_si(PC_BLOCK_ALL, 0);
		// Drop Flag and announce
		if (.@x == -1 & .@y == -1)
			getmapxy(.@m$, .@x, .@y, 0);
		movenpc(.@oppose$ +" Flag#"+ .@oppose, .@x, .@y);
		mapannounce("ebg_ctf",.@oppose$ +" Flag Dropped by ["+ strcharinfo(0) +"]", bc_map, 0xFF0000);
		// Add Points
		if (on_ebg())
			bg_addpoints(CTF_DROPPED, 1, 1);
		if (@bg_killer_id)
			bg_addfame(@bg_killer_id);
		// Set Status to OnGround and Start Timer
		bg_npc_data(CTF_ONGROUND, 0, .@oppose$ +" Flag#"+ .@oppose);
		donpcevent(.@oppose$ +" Flag#"+ .@oppose +"::OnTimerStartE");
		enablenpc(.@oppose$ +" Flag#"+ .@oppose);
	}
	if (.@warp)
		callfunc("F_warpDie", BGT_CTF, .@self);
	return;
	
OnReadyCheck:
	if ($@eBG_start[BGT_CTF])
		end;
		
	$@eBG_start[BGT_CTF] = EBG_RUNNING;
	initnpctimer();
	.time_remaining = getvariableofnpc(.bg_time[BGT_CTF], "BG_Settings");

	// Some Required BG Variables
	// Set Scores to 0
	.Team1_Score = 0;
	.Team2_Score = 0;
	bg_updatescore("ebg_ctf", .Team1_Score, .Team2_Score);
	bg_announce("Battleground -- Flavius CTF has started!", bc_map | bc_npc, "0xA0522D");
	sleep(3000);
	// NPC Spawn's
	donpcevent("ctf_respawn#1::OnBGStart");
	donpcevent("ctf_respawn#2::OnBGStart");
	// Start Match!!
	donpcevent("CTF_Flavius::OnMatchStart");
	end;

OnMatchStart:
	if ($@eBG_start[BGT_CTF] != EBG_RUNNING)	//If Match not Started, Do Nothing
		end;

	// Flags2Base
	donpcevent("Guillaume Flag#1::OnBase");	//Initialize Team1 Flag
	donpcevent("Croix Flag#2::OnBase");	//Initialize Team2 Flag
	mapannounce("ebg_ctf", "The Flags have been set to their Bases!!", bc_map | bc_blue);
	end;

OnTimer60000:
	.time_remaining--;
	if (.time_remaining != 0) {
		mapannounce("ebg_ctf", "The Battle will end in "+ .time_remaining +" Minutes!!", bc_map, 0xA0522D);
		initnpctimer;
		end;
	}
	.@timer = true;
OnMatchStop:
	// Disable Flags
	disablenpc("Guillaume Flag#1");
	disablenpc("Croix Flag#2");

	.@check_method = 0;	// Assume Timer has end

	if (.@timer == false) {	// Match is still remaining
		// Update Scores
		bg_updatescore("ebg_ctf", .Team1_Score, .Team2_Score);
		.@check_method = 1;
	} else {
		.@end = true;	// End the Match
	}

	// Clear ViewPoints
	viewpointmap("ebg_ctf", 2, 0, 0, GUILLAUME, 0x0000FF);
	viewpointmap("ebg_ctf", 2, 0, 0, CROIX, 0xFF0000);
	
	// Stop Timers
	donpcevent("Guillaume Flag#1::OnTimeStop");
	donpcevent("Croix Flag#2::OnTimeStop");
	
	if (.@timer == false) {
		if (.Team1_Score >= $bg_flavius_ctf[0] || .Team2_Score >= $bg_flavius_ctf[0]) { // Team1 Wins if Score > [0]
			.@end = true;
		} else { // No one reached the Points required.
			sleep(5000);	// Rest for 5 seconds
			donpcevent("CTF_Flavius::OnMatchStart");	// Start the Mew Round
		}
	}
	if (.@end == true)
		callsub(OnMatchEnd, .@check_method);
	end;

OnMatchEnd:
	stopnpctimer;
	// Disable Flags
	disablenpc("Guillaume Flag#1");
	disablenpc("Croix Flag#2");
	// Stop the Timers(Heal/Warp)
	donpcevent("ctf_respawn#1::OnBGStop");
	donpcevent("ctf_respawn#2::OnBGStop");
	$@eBG_start[BGT_CTF] = EBG_ENDING;
	/**
	 * 2 Teams, AddScore = 1, StartingIndex = 4, CheckMethod(0, 1)
	 * CM: 0 = Compare Scores, the higher one's win.
			Triggered when Time is over
	 * CM: 1 = Compare Scores with index 0. score > [0] wins.
			Triggered when score is reached before time
	 */
	.@win = callfunc("F_eBGReward", BGT_CTF, $bg_flavius_ctf, 2, .Team1_Score, .Team2_Score, 1, 4, getarg(0));
	if (.@win > 0) {
		mapannounce("ebg_ctf","The "+ callfunc("F_eBGTeamName", .@win) +" army has won the Battle of Flavius CTF!", bc_map, callfunc("F_eBGTeamColor", .@win));
	} else if (.@win == 0) {
		mapannounce("ebg_ctf", "The battle is over. This is a Tie...!", bc_map, 0xA0522D);
	}
	/* Reset */
	stopnpctimer();
	/* Set Score to 0 */
	.Team1_Score = 0;
	.Team2_Score = 0;
	/* Destroy Existing BG */
	callfunc("F_EndeBG", "ebg_ctf", 2);
	end;
}

// Battleground Respawn Area

ebg_ctf,390,10,1	duplicate(respawn_script#bg)	ctf_respawn#1	HIDDEN_WARP_NPC	//#guictf_respawn
ebg_ctf,10,290,1	duplicate(respawn_script#bg)	ctf_respawn#2	HIDDEN_WARP_NPC	//#croctf_respawn

// Battleground Therapist

ebg_ctf,390,13,3	duplicate(Therapist#eBG)	Therapist in battle#ctf1	4_F_SISTER
ebg_ctf,10,293,3	duplicate(Therapist#eBG)	Therapist in battle#ctf2	4_F_SISTER

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

ebg_ctf,304,231,1	duplicate(Flag#bat)	Guillaume camp#ctf23	1_FLAG_LION
ebg_ctf,319,231,1	duplicate(Flag#bat)	Guillaume camp#ctf24	1_FLAG_LION
ebg_ctf,304,218,1	duplicate(Flag#bat)	Guillaume camp#ctf25	1_FLAG_LION
ebg_ctf,319,218,1	duplicate(Flag#bat)	Guillaume camp#ctf26	1_FLAG_LION
ebg_ctf,304,231,1	duplicate(Flag#bat)	Guillaume camp#ctf27	1_FLAG_LION
ebg_ctf,304,231,1	duplicate(Flag#bat)	Guillaume camp#ctf28	1_FLAG_LION
ebg_ctf,335,142,1	duplicate(Flag#bat)	Guillaume camp#ctf29	1_FLAG_LION
ebg_ctf,335,157,1	duplicate(Flag#bat)	Guillaume camp#ctf30	1_FLAG_LION
ebg_ctf,390,16,1	duplicate(Flag#bat)	Guillaume camp#ctf31	1_FLAG_LION
ebg_ctf,292,163,1	duplicate(Flag#bat)	Guillaume camp#ctf32	1_FLAG_LION
ebg_ctf,292,136,1	duplicate(Flag#bat)	Guillaume camp#ctf33	1_FLAG_LION
ebg_ctf,241,185,1	duplicate(Flag#bat)	Guillaume camp#ctf34	1_FLAG_LION
ebg_ctf,247,179,1	duplicate(Flag#bat)	Guillaume camp#ctf35	1_FLAG_LION

ebg_ctf,96,81,1	duplicate(Flag#bat)	Croix camp#ctf22	1_FLAG_EAGLE
ebg_ctf,96,68,1	duplicate(Flag#bat)	Croix camp#ctf23	1_FLAG_EAGLE
ebg_ctf,79,81,1	duplicate(Flag#bat)	Croix camp#ctf24	1_FLAG_EAGLE
ebg_ctf,79,68,1	duplicate(Flag#bat)	Croix camp#ctf25	1_FLAG_EAGLE
ebg_ctf,96,81,1	duplicate(Flag#bat)	Croix camp#ctf26	1_FLAG_EAGLE
ebg_ctf,96,81,1	duplicate(Flag#bat)	Croix camp#ctf27	1_FLAG_EAGLE
ebg_ctf,59,164,1	duplicate(Flag#bat)	Croix camp#ctf28	1_FLAG_EAGLE
ebg_ctf,59,137,1	duplicate(Flag#bat)	Croix camp#ctf29	1_FLAG_EAGLE
ebg_ctf,10,296,1	duplicate(Flag#bat)	Croix camp#ctf30	1_FLAG_EAGLE
ebg_ctf,110,162,1	duplicate(Flag#bat)	Croix camp#ctf31	1_FLAG_EAGLE
ebg_ctf,110,137,1	duplicate(Flag#bat)	Croix camp#ctf32	1_FLAG_EAGLE
ebg_ctf,152,120,1	duplicate(Flag#bat)	Croix camp#ctf33	1_FLAG_EAGLE
ebg_ctf,158,114,1	duplicate(Flag#bat)	Croix camp#ctf34	1_FLAG_EAGLE

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

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