class UTTeamGame_Zombies extends UTTeamGame;

var bool ZombieGameReset;
var bool bZombieRunBoost;

simulated function PreBeginPlay()
{
Super.PreBeginPlay();
ZombieGameReset=true;
}


simulated function PostBeginPlay()
{
	Super.PostBeginPlay();
	loginternal("Game is started! Or something like that!");
	UTTeamGame(WorldInfo.Game).bPlayersBalanceTeams = true;
	WorldInfo.Game.DefaultPawnClass = class'ZombiesGame.UTPawn_Zombies';
	UTGame(WorldInfo.Game).DefaultInventory[0] = none;
	UTGame(WorldInfo.Game).DefaultInventory[1] = none;
	SetTimer(1,true);
	ZombieGameReset=true;
}

function Timer()
{
	if(Teams[0].Size == 0 || ZombieGameReset == true)
	{
		ZombieGameReset=true;
		loginternal("Teams will be balanced as soon as possible!");
	}
}


function StartMatch()
{
	local bool bTemp;
	local UTPlayerController PC;

    GotoState('MatchInProgress');

    GameReplicationInfo.RemainingMinute = GameReplicationInfo.RemainingTime;
    Super.StartMatch();

	bTemp = bMustJoinBeforeStart;
    bMustJoinBeforeStart = false;

	AddInitialBots();

    bMustJoinBeforeStart = bTemp;

	ZombieGameReset=false;
	`log("START MATCH");
	`log("PS: I hacked your fucking startup script!");


	// Setup team VOIP mute lists
	foreach WorldInfo.AllControllers( class'UTPlayerController', PC )
	{
		SetupPlayerMuteList( PC, false );	// Force spectator channel?
	}


	// we had to wait for the constructioning and the commandline causeevent for the flythrough was lost
	// so we need to fire off another one once we know that we are in the match and meshes have been constructioned!
	if( bDoingAFlyThrough == TRUE )
	{
		foreach WorldInfo.AllControllers(class'UTPlayerController', PC)
		{
			PC.ConsoleCommand( "ce FlyThrough" );
			break;
		}
	}

	foreach WorldInfo.AllControllers(class'UTPlayerController', PC)
	{
		PC.IncrementNumberOfMatchesPlayed();
		break;
	}

	if( bCheckingForFragmentation == TRUE )
	{
		//ConsoleCommand( "killparticles" );
		ConsoleCommand( "MemFragCheck" );
	}
	if( bCheckingForMemLeaks == TRUE )
	{
		//ConsoleCommand( "MemLeakCheck" );
		ConsoleCommand( "MEMTAG_UPDATE" );
	}

	if( BugLocString != "" || BugRotString != "" )
	{
		if( PC.CheatManager != none )
		{
			//`log( "BugLocString:" @ BugLocString );
			//`log( "BugRotString:" @ BugRotString );

			PC.CheatManager.BugItGoString( BugLocString, BugRotString );
		}
	}

	//SetTimer( 10.0f, TRUE, 'TrackMemoryFunctor');
}


function bool TooManyBots(Controller botToRemove)
{
	/// We only manage bots if the game is over. Because it's zombies.
local TeamInfo BotTeam, OtherTeam;
if(ZombieGameReset == true)
{
	loginternal("I ran the rebalance script!");
   	// We only auto-manage bots if we are not in single player mode.
	if ( SinglePlayerMissionID == INDEX_NONE )
	{
		if ( bForceAllRed )
			return false;
		if ( (!bPlayersVsBots || (WorldInfo.NetMode == NM_Standalone)) && (UTBot(botToRemove) != None) &&
			(!bCustomBots || (WorldInfo.NetMode != NM_Standalone)) && botToRemove.PlayerReplicationInfo.Team != None )
		{
			BotTeam = botToRemove.PlayerReplicationInfo.Team;
			OtherTeam = Teams[1-BotTeam.TeamIndex];
			if ( OtherTeam.Size < BotTeam.Size - 1 )
			{
				return true;
			}
			else if ( OtherTeam.Size > BotTeam.Size )
			{
				return false;
			}
		}
		if ( (WorldInfo.NetMode != NM_Standalone) && bPlayersVsBots )
			return ( NumBots > Min(16,BotRatio*NumPlayers) );
		if ( bPlayerBecameActive )
		{
			bPlayerBecameActive = false;
			return true;
		}
		return ( NumBots + NumPlayers > DesiredPlayerCount );
	}
	return false;
}
	return false;
}

function bool CheckScore(PlayerReplicationInfo Scorer)
{
	if (CheckMaxLives(Scorer))
	{
		return false;
	}
	else if (Scorer != None && Scorer.Team == Teams[1] && Teams[0].Size == 0)
	{
		EndGame(Scorer,"lastman");
		return true;
	}
	else if (Scorer != None && Scorer.Team == Teams[0] && bOverTime)
	{
		EndGame(Scorer,"timelimit");
		return true;
	}
	else
	{
		if ( Teams[0].size == 1 && !bPlayedOneKill )
		{
			if ( Teams[0].size == 1 )
			{
				bPlayedOneKill = true;
				BroadcastLocalized(self,class'UTKillsRemainingMessage', 2);
			}
		}
		
		return false;
	}
}

function bool CheckEndGame(PlayerReplicationInfo Winner, string Reason)
{
	local Controller P;
    local bool bLastMan;

	if ( bOverTime )
	{
		if ( Numbots + NumPlayers == 0 )
			return true;
		bLastMan = true;
		foreach WorldInfo.AllControllers(class'Controller', P)
		{
			if ( (P.PlayerReplicationInfo != None) && !P.PlayerReplicationInfo.bOutOfLives )
			{
				bLastMan = false;
				break;
			}
		}
		if ( bLastMan )
			return true;
	}

    bLastMan = ( Reason ~= "LastMan" );
	if ( !bLastMan && CheckModifiedEndGame(Winner, Reason) )
		return false;

	if ( bTeamScoreRounds )
	{
		if ( Winner != None )
		{
			Winner.Team.Score += 1;
			Winner.Team.bForceNetUpdate = TRUE;
		}
	}
	else if ( !bLastMan && (Teams[1].Score == Teams[0].Score) )
	{
		// tie
		if ( !bOverTimeBroadcast )
		{
			StartupStage = 7;
			PlayStartupMessage();
			bOverTimeBroadcast = true;
		}
		return false;
	}
	if ( bLastMan && Teams[0].size == 0 )
		GameReplicationInfo.Winner = Teams[1];
	else if ( Teams[0].size == 0 )
		GameReplicationInfo.Winner = Teams[1];
	else
		GameReplicationInfo.Winner = Teams[0];

	if ( Winner == None )
	{
		foreach WorldInfo.AllControllers(class'Controller', P)
		{
			if ( (P.PlayerReplicationInfo != None) && (P.PlayerReplicationInfo.Team == GameReplicationInfo.Winner)
				&& ((Winner == None) || (P.PlayerReplicationInfo.Score > Winner.Score)) )
			{
				Winner = P.PlayerReplicationInfo;
			}
		}
	}

	EndTime = WorldInfo.TimeSeconds + EndTimeDelay;

	SetEndGameFocus(Winner);
	return true;
}

function PlayRegularEndOfMatchMessage()
{
	local UTPlayerController PC;
	local int Index;

	if ( Teams[0].Size == 0 )
		Index = 5;
	else
		Index = 4;
	foreach WorldInfo.AllControllers(class'UTPlayerController', PC)
	{
		PC.ClientPlayAnnouncement(VictoryMessageClass, Index);
	}
}

function bool DominatingVictory()
{
///This is removed for zombies.
}

static function string GetEndOfMatchRules(int InGoalScore, int InTimeLimit)
{
	local string Work;

	Work = "Attempt to survive the zombie onslaught.";

	return Work;
}

defaultproperties
{

	GameName="Zombies";
   Acronym="ZOM"
   MapPrefixes[0]="DM"
   Description="Can you survive the out break of zombies?"
	HUDType=class'ZombiesGame.UTTeamHUD_Zombies'

}
