//=============================================================================
// LastManStanding.
//=============================================================================
class LastManStanding expands DeathMatchPlus;

var config bool bHighDetailGhosts;
var() int Lives;

function PostBeginPlay()
{
	Super.PostBeginPlay();
	FragLimit = 0;
}

event playerpawn Login
(
	string[32] Portal,
	string[120] Options,
	out string[80] Error,
	class<playerpawn> SpawnClass
)
{
	local playerpawn NewPlayer;

	if ( SpawnClass == class'UnrealSpectator' )
		SpawnClass = class'CHSpectator';

	NewPlayer = Super.Login(Portal, Options, Error, SpawnClass);

	if ( NewPlayer != None )
		NewPlayer.PlayerReplicationInfo.Score = Lives;

	return NewPlayer;
}

function Timer()
{
	local Pawn P;

	Super.Timer();

	For ( P=Level.PawnList; P!=None; P=P.NextPawn )
		if ( P.IsInState('Dying') && P.IsA('PlayerPawn') )
			PlayerPawn(P).ServerReStartPlayer();
}
 
function bool RestartPlayer( pawn aPlayer )	
{
	local NavigationPoint startSpot;
	local bool foundStart;

	if( bRestartLevel && Level.NetMode!=NM_DedicatedServer && Level.NetMode!=NM_ListenServer )
		return true;

	if ( aPlayer.IsA('Bot') && (aPlayer.PlayerReplicationInfo.Score < 1) )
		return false; // bots don't respawn when ghosts

	startSpot = FindPlayerStart(0);
	if( startSpot == None )
		return false;
		
	foundStart = aPlayer.SetLocation(startSpot.Location);
	if( foundStart )
	{
		startSpot.PlayTeleportEffect(aPlayer, true);
		aPlayer.SetRotation(startSpot.Rotation);
		aPlayer.ViewRotation = aPlayer.Rotation;
		aPlayer.Acceleration = vect(0,0,0);
		aPlayer.Velocity = vect(0,0,0);
		aPlayer.Health = aPlayer.Default.Health;
		aPlayer.SetCollision( true, true, true );
		aPlayer.ClientSetRotation( startSpot.Rotation );
		aPlayer.bHidden = false;
		aPlayer.SoundDampening = aPlayer.Default.SoundDampening;
		if ( aPlayer.PlayerReplicationInfo.Score < 1 )
		{
			// This guy is a ghost.  Add a visual effect.
			if (bHighDetailGhosts)
			{
				aPlayer.Style = STY_Translucent;
				aPlayer.ScaleGlow = 0.5;
			} else {
				// Need to put a good software effect here.
			}
		} else
			AddDefaultInventory(aPlayer);
	}
	return foundStart;
}

function Killed( pawn killer, pawn Other, name damageType )
{
	local string[32] killed;
	local string[64] message;
	local Pawn PawnLink;
	local int NumGhosts;
	
	Super.Killed(Killer, Other, damageType);	

	// Check to see if everyone is a ghost.
	for ( PawnLink = Level.PawnList; PawnLink != None; PawnLink = PawnLink.nextPawn )
		if ( PawnLink.PlayerReplicationInfo.Score < 1 )
			NumGhosts++;
			
	// End the game if there is only one man standing.
	if ( NumGhosts == (NumPlayers + NumBots) - 1 )
		EndGame();
}

function ScoreKill(pawn Killer, pawn Other)
{
	if( (killer == Other) || (killer == None) )
		Other.PlayerReplicationInfo.Score -= 1;
	else
	{
		killer.killCount++;
		if (Other.PlayerReplicationInfo.Score > 0)
			Other.PlayerReplicationInfo.Score -= 1;
	}
}	

function bool PickupQuery( Pawn Other, Inventory item )
{
	if ( Other.PlayerReplicationInfo.Score < 1 )
		return false;
	
	return Super.PickupQuery( Other, item );
}

/*
AssessBotAttitude returns a value that translates to an attitude
		0 = ATTITUDE_Fear;
		1 = return ATTITUDE_Hate;
		2 = return ATTITUDE_Ignore;
		3 = return ATTITUDE_Friendly;
*/	
function byte AssessBotAttitude(Bot aBot, Pawn Other)
{
	if ( Other.bIsPlayer && (Other.PlayerReplicationInfo.Score < 1) )
		return 2; //bots ignore ghosts
	else if ( Other.IsA('TeamCannon') 
		|| (aBot.RelativeStrength(Other) > aBot.Aggressiveness - 0.35) )
		return 0;
	else
		return 1;
}

function ModifyBehaviour(Bot NewBot)
{
	// Set the Bot's Lives
	NewBot.PlayerReplicationInfo.Score = Lives;

	NewBot.CampingRate += 0.3;
}

defaultproperties
{
     Lives=30
     bTournament=True
     StartUpMessage="Last Man Standing.  How long can you live?"
     BeaconName="LMS"
     GameName="Last Man Standing"
}
