//=============================================================================
// sgBaseCore.
// * Revised by 7DS'Lust
//=============================================================================
class sgBaseCore extends sgBuilding;

event PostBeginPlay()
{
    if ( TeamGamePlus(Level.Game) == None )
    {
        Destroy();
        return;
    }

	MaxEnergy = TeamGamePlus(Level.Game).GoalTeamScore*1000;
	Energy = MaxEnergy;
	SCount = 0;
	SetTimer(0.1, true);

	//UpdateScore();
	Spawn(Class'sgSmokeGenerator');
}

function Destruct()
{
    if ( SiegeGI(Level.Game) != None )
        SiegeGI(Level.Game).CoreDestroyed(Self);
	Spawn(class'sgFlash');
	Spawn(class'shockWave');
	Spawn(class'shockRifleWave');
	Destroy();
}

simulated event Timer()
{
	local sgPRI a;
	
	Super.Timer();

    //if ( SiegeGI(Level.Game) != None && SiegeGI(Level.Game).StartingMaxRU < SiegeGI(Level.Game).MaxRUs[Team] )
    //    SiegeGI(Level.Game).MaxRUs[Team] = SiegeGI(Level.Game).StartingMaxRU;
	
	UpdateScore();
	
	foreach AllActors(class'sgPRI', a)
		if( a.Team == Team && !bDisabledByEMP)
			a.AddRU(0.05 * (Grade * 0.85 + 0.05) * ((1+(a.sgInfoTeamPlayer))/2));

	if (myFX!=None)
    		myFX.RotationRate.Yaw = Energy*0.25;
}

simulated function FinishBuilding()
{
    Super.FinishBuilding();

    if ( Team < 0 || Team > 4 )
        Team = 0;

	if ( Team == 0 )
    	Texture = SpriteRedTeam;
	else
    	Texture = SpriteBlueTeam;	

if (myFX!=None)
    myFX.RotationRate.Yaw = Energy;
}

simulated function MonsterDamage(int Damage, Pawn instigatedBy)
{
	Energy -= Damage;
	
    if ( Energy <= 0 )
		{
			Energy = 0;
			AnnounceAll("Game Over! The monsters have killed the BaseCore!");
				Destruct();
		}
}

simulated event TakeDamage(int Damage, Pawn instigatedBy, Vector hitLocation, 
  Vector momentum, name damageType)
{
	local int actualDamage;
	local float tempScore;
	local Pawn p;

	if ( Role < ROLE_Authority || Level.Game == None )
		return;
	
	actualDamage = Level.Game.ReduceDamage(Damage, DamageType, Self,
		instigatedBy);

	if ( instigatedBy != None && instigatedBy.bIsPlayer )
		{
			if ( TeamGamePlus(Level.Game) != None && instigatedBy.PlayerReplicationInfo.Team == Team )
				{
					actualDamage *= TeamGamePlus(Level.Game).FriendlyFireScale;
					tempScore = -1 * FMin(Energy, actualDamage);
				}
			else
				tempScore = FMin(Energy, actualDamage);

			
			if( sgPRI(instigatedBy.PlayerReplicationInfo) != None )
			{
			 tempScore *= (1 + Grade/10)*(sgPRI(instigatedBy.PlayerReplicationInfo).sgInfoTeamPlayer);
			}

			if (tempScore < 0 || tempScore > 100000)
				return;
			else
				instigatedBy.PlayerReplicationInfo.Score += tempScore/100;
				
			if (instigatedBy.PlayerReplicationInfo.Score < -1000)
				instigatedBy.PlayerReplicationInfo.Score = 0;

			if ( sgPRI(instigatedBy.PlayerReplicationInfo) != None )
				sgPRI(instigatedBy.PlayerReplicationInfo).AddRU((tempScore/5)*RuRewardScale);
		}
    else
		if ( sgBuilding(instigatedBy) != None && TeamGamePlus(Level.Game) != None && sgBuilding(instigatedBy).Team == Team )
        	actualDamage *= TeamGamePlus(Level.Game).FriendlyFireScale;
			if( sgPRI(instigatedBy.PlayerReplicationInfo) != None )
			{
			 actualDamage *= ((1+(sgPRI(instigatedBy.PlayerReplicationInfo).sgInfoTeamPlayer))/2);
			}

    if ( actualDamage > 0 && instigatedBy != None && ((instigatedBy.bIsPlayer && 
	instigatedBy.PlayerReplicationInfo.Team != Team) || (sgBuilding(instigatedBy) != None && 
	sgBuilding(instigatedBy).Team != Team)) )
		for ( p = Level.PawnList; p != None; p = p.nextPawn )
			if ( p.IsA('TournamentPlayer') &&
				p.PlayerReplicationInfo.Team == Team )

	if ((Energy-actualDamage)/1000 > 2)
		TournamentPlayer(P).ReceiveLocalizedMessage(class'sgCoreWarnMsg', actualDamage);
	else
		TournamentPlayer(P).ReceiveLocalizedMessage(class'sgCoreDyingMsg', actualDamage);

	Energy -= actualDamage;
	if( sgPRI(instigatedBy.PlayerReplicationInfo) != None )
	{
	 sgPRI(instigatedBy.PlayerReplicationInfo).sgInfoCoreKiller+=actualDamage;
	}

    if (actualDamage>0) Spawn(class'sgFlash');
		if (myFX!=None)
			myFX.RotationRate.Yaw = Energy;

    if ( Energy <= 0 )
		{
			Energy = 0;
			AnnounceAll("Game Over! "@instigatedBy.PlayerReplicationInfo.PlayerName@"killed the BaseCore!");
				Destruct();
		}
		
		UpdateScore();
}

function AnnounceAll(string sMessage)
{
    local Pawn p;

    for ( p = Level.PawnList; p != None; p = p.nextPawn )
	    if ( (p.bIsPlayer || p.IsA('MessagingSpectator')) &&
          p.PlayerReplicationInfo != None  )
		    p.ClientMessage(sMessage);
}

function UpdateScore()
{
	TeamGamePlus(Level.Game).Teams[Team].Score = (Energy/MaxEnergy)*100;
}

defaultproperties
{
    BuildingName="Base Core"
    UpgradeCost=80
    MaxEnergy=20000.00
    SpriteScale=1.00
    Model=LodMesh'Botpack.Diamond'
    SkinRedTeam=Texture'SiegeUltimateRC7UB01Media.U.BuildingMaps.CoreSkinTeam0'
    SkinBlueTeam=Texture'SiegeUltimateRC7UB01Media.U.BuildingMaps.CoreSkinTeam1'
    SkinGreenTeam=Texture'SiegeUltimateRC7UB01Media.U.BuildingMaps.CoreSkinTeam2'
    SkinYellowTeam=Texture'SiegeUltimateRC7UB01Media.U.BuildingMaps.CoreSkinTeam3'
    SpriteRedTeam=Texture'SiegeUltimateRC7UB01Media.U.KoalasEffects.SKFlare'
    SpriteBlueTeam=Texture'SiegeUltimateRC7UB01Media.U.KoalasEffects.SKFlare'
    SpriteGreenTeam=Texture'SiegeUltimateRC7UB01Media.U.KoalasEffects.SKFlare'
    SpriteYellowTeam=Texture'SiegeUltimateRC7UB01Media.U.KoalasEffects.SKFlare'
    DSofMFX=2.60
    bAlwaysRelevant=True
    MultiSkins(0)=Texture'SiegeUltimateRC7UB01Media.GFX.sgCoreSpriteT0'
    MultiSkins(1)=Texture'SiegeUltimateRC7UB01Media.GFX.sgCoreSpriteT1'
    MultiSkins(2)=Texture'SiegeUltimateRC7UB01Media.GFX.sgCoreSpriteT2'
    MultiSkins(3)=Texture'SiegeUltimateRC7UB01Media.GFX.sgCoreSpriteT3'
    bCollideWorld=True
}
