//=============================================================================
// Domination.
//=============================================================================
class Domination expands TeamGamePlus
	config;

function PostBeginPlay()
{
	local ControlPoint CP;
	local int TempTotal;

	Super.PostBeginPlay();

	foreach AllActors(class'ControlPoint', CP)
		TempTotal++;
	DominationReplicationInfo(GameReplicationInfo).TotalPoints = TempTotal;
	Log("Total Domination Points Found: "$TempTotal);
}

function Timer()
{
	local NavigationPoint N;
	local int i, TempScores[4];

	if ( !bRequireReady || (CountDown <= 0) )
		for ( N=Level.NavigationPointList; N!=None; N=N.nextNavigationPoint )
			if ( N.IsA('ControlPoint') && (ControlPoint(N).ControllingTeam != None) &&
			     ControlPoint(N).bScoreReady )
			{
				TempScores[ControlPoint(N).ControllingTeam.TeamIndex]++;
				ControlPoint(N).ControllingTeam.Score += 0.2;
			}

	for ( i=0; i<4; i++ )
		DominationReplicationInfo(GameReplicationInfo).TeamScores[i] = TempScores[i];

	Super.Timer();
}

function EndGame()
{
	log("End game");
	Super.EndGame();
}

function ClearControl(Pawn Other)
{
	local NavigationPoint N;

	for ( N=Level.NavigationPointList; N!=None; N=N.nextNavigationPoint )
		if ( N.IsA('ControlPoint') && (ControlPoint(N).ControllingTeam != None) )
		{
			if ( ControlPoint(N).Controller == Other )
			{
				ControlPoint(N).Controller = None;
				ControlPoint(N).UpdateStatus();
			}
		}
}

function Logout( pawn Exiting )
{
	ClearControl(Exiting);
	Super.Logout(Exiting);
}

function Actor SetDefenseFor(Bot aBot)
{
	local Actor Result;
	local int i;
	local NavigationPoint N;

	for ( N=Level.NavigationPointList; N!=None; N=N.nextNavigationPoint )
		if ( N.IsA('ControlPoint') && (ControlPoint(N).ControllingTeam.TeamIndex == aBot.PlayerReplicationInfo.Team) )
		{
			i++;
			if ( (Result == None) || ( FRand() < 1.0/float(i)) )
				Result = N;
		}	
		
	return Result;		
}

function bool FindSpecialAttractionFor(Bot aBot)
{
	local float bestweight, newweight;
	local NavigationPoint N, Best;
	local bool bResult;

	//log("Find Special Attraction for "$aBot$" in state "$aBot.GetStateName());
	if ( !aBot.IsInState('Roaming') || (aBot.Orders == 'Follow')
		|| (aBot.Weapon == None) || (aBot.Weapon.AIRating < 0.5)
		|| (aBot.Health < 80) )
		{
			//log("No Special Attraction for "$aBot);
			return false;
		}

	if ( (ControlPoint(aBot.OrderObject) != None)
		&& ((ControlPoint(aBot.OrderObject).ControllingTeam == None)
			||(ControlPoint(aBot.OrderObject).ControllingTeam.TeamIndex != aBot.PlayerReplicationInfo.Team)))
		Best = ControlPoint(aBot.OrderObject);
	else
	{
		bestweight = 1000000;
		for ( N=Level.NavigationPointList; N!=None; N=N.nextNavigationPoint )
			if ( N.IsA('ControlPoint') &&  ((ControlPoint(N).ControllingTeam == None)
				||(ControlPoint(N).ControllingTeam.TeamIndex != aBot.PlayerReplicationInfo.Team)))
			{
				newweight = VSize(N.location - location) + 3 * Abs(N.Location.Z - Location.Z);
				if ( ControlPoint(N).ControllingTeam == None )
					newweight *= 0.5;
				if ( newweight < bestweight )
				{
					bestweight = newweight;
					Best = N;
				}
			}
	}

	if ( Best != None )
	{
		aBot.OrderObject = Best;
		if ( aBot.ActorReachable(Best) )
			aBot.MoveTarget = Best;
		else
			aBot.MoveTarget = aBot.FindPathToward(Best);
		bResult = (aBot.MoveTarget != None);
	}
	else
		bResult = false;

	//log("Done Find Special Attraction for "$aBot$" Result "$bResult);
	return bResult;
}

defaultproperties
{
	 GameReplicationInfoClass=Class'BotPack.DominationReplicationInfo'
	 bUseTranslocator=true
     bSpawnInTeamArea=True
     StartUpMessage="Unreal Challenge Domination. Work with your team to establish the most control points. "
     GameMenuType=Class'BotPack.ChallengeDominationMenu'
     MapListType=Class'BotPack.DOMMapList'
     MapPrefix="DOM"
     BeaconName="DOM"
     GameName="Domination"
	 ScoreBoardType=Class'UnrealTeamScoreboard'
	 HUDType=Class'BotPack.ChallengeDominationHUD'
}
