//////////////////////////////////////////////////////////////////////////////
// Capture the flag rules common to ctf and ctf_death
//////////////////////////////////////////////////////////////////////////////

// define the pads near each of the flag bases (used by AI)

// @1 = color, @2 = Capitalized color (e.g. "Green"),
// @3 = other color, @4 = other color flagbase letter

// If step on home base with flag, get credit
macro bringHomeEnemyFlag 4
{
	if padon @1base testvar hasitem me @3flag1 = 1 and isAlly @1 @3 = 0 then
		trigger @1baser@3

	if @1baser@3 then
		showmessage "@2 scored with @3 flag",
		trigger @1CapturedFlag,
		dropitem me @3flag1 @4flagbase
}

// playsounds if a flag is picked up by an enemy
macro playPickupSound 2
{
	if pickedup @1flag1 by @2 testvar isAlly @1 @2 = 0 then
		trigger @1psound@2

	if @1psound@2 then
		playsound "mp_got@1.wav" (0,0) 3 1
}

// @1 = color, @2 = Capitalized color (e.g. "Green"), @3 = flagbase letter (e.g. 'g')
// @4-6 are other colors, @7-8 are other colors flagbase letters
macro ctfevents 9
{
	// deploy flags for colors in game and set the flags to strategic
	if startup1 testvar iscoloringame @1 > 0 then
		triggerdelay 10 @1xstartup2

	if @1xstartup2 then
		deploy @3flagbase,
		deploy @1flag1 @3flagbase,
		setitemflag @1flag1 strategic 1,
		setitemflag @1flag1 stratsquare 1

	// check for a trooper bringing home an enemy flag
	expand bringHomeEnemyFlag ( @1 "@2" @4 @7 )
	expand bringHomeEnemyFlag ( @1 "@2" @5 @8 )
	expand bringHomeEnemyFlag ( @1 "@2" @6 @9 )

	// update scores and show/play effects

	if @1CapturedFlag then
		createexplosion @1base 137 0 @1,
		playsound "mp_ctftobase.wav" (0,0) 1 2,
		addvar @1score captureFlagBonus,
		trigger check@1wins

	// if flag is dropped and not picked up for 2 minutes then return it to it's base

	variable @1FlagTimeOutVar 0
	variable @1FlagLastTimeOutVar 1

	if dropped @1flag1 then
		setitemflag whoby flashing 0,
		triggerdelay 120000 @1FlagCheckTimeOut

	if pickedup @1flag1 then
		setitemflag whoby flashing 1,
		addvar @1FlagTimeOutVar 1

	if @1FlagCheckTimeOut testvar @1FlagTimeOutVar = @1FlagLastTimeOutVar then
		trigger @1movefl

	if @1movefl then
		showmessage "@2 Flag was dropped and not picked up for 2 minutes - moved to base",
		createexplosion @1flag1 128 0 @1,
		playsound "mp_rebaseflag.wav" (0,0) 2 1,
		moveitem @1flag1 @3flagbase,
		triggerdelay 1000 @1FlagLastTimeOutIncr

	if @1FlagCheckTimeOut testvar @1FlagTimeOutVar <> @1FlagLastTimeOutVar then
		triggerdelay 1000 @1FlagLastTimeOutIncr

	if @1FlagLastTimeOutIncr then
		addvar @1FlagLastTimeOutVar 1

	// playsounds if a flag is picked up by an enemy
	expand playPickupSound ( @1 @4 )
	expand playPickupSound ( @1 @5 )
	expand playPickupSound ( @1 @6 )
}

expand ctfevents ( green "Green" g tan blue grey t b gr )
expand ctfevents ( tan "Tan" t green blue grey g b gr )
expand ctfevents ( blue "Blue" b green tan grey g t gr )
expand ctfevents ( grey "Grey" gr green tan blue g t b )
