//=============================================================================
// sgEquipmentSupplier.
// * Revised by 7DS'Lust
//=============================================================================
class sgEquipmentSupplier extends sgBuilding
    abstract config(SiegeUltimateRC7UB01);

var bool bSuperSupplier;
var() float         SupplyRadius;
var Pawn            CurrentTarget;

var() globalconfig string Weapons[9];
var() bool            ClassesLoaded;
var() class<Weapon> WeaponClasses[9];
var() float         EquipRate;


var bool	bProtected;
var bool AnnounceImmunity;
var int Count;
var globalconfig int SuppProtectTimeSecs;


simulated event PostBeginPlay()
{
 local Pawn p;
 local int nSuppliers;
 local string sLocation;
 
 
 saveConfig();
	
 
 if(Role == ROLE_Authority)
 {
  if ( Pawn(owner).PlayerReplicationInfo.PlayerLocation != None )
  {
   sLocation = PlayerReplicationInfo.PlayerLocation.LocationName;
  }
  else if ( Pawn(owner).PlayerReplicationInfo.PlayerZone != None )
  {
   sLocation = Pawn(owner).PlayerReplicationInfo.PlayerZone.ZoneName;
  }
  
  if ( sLocation != "" && sLocation != " ")
  {
   sLocation = "at"@sLocation;
  }
 }
 
 Super.PostBeginPlay();
 if(Role != ROLE_Authority){return;}
 
 bProtected = true;
 if (SiegeGI(Level.Game).SupplierProtection)
 {
  for ( p = Level.PawnList; p != None; p = p.nextPawn )
  {
   if ( sgEquipmentSupplier(p) != None && string(p.class) ~= string(class) )
   {
    if (sgEquipmentSupplier(p).Team == Team){ nSuppliers++; }
   }
  }
 }
 if (nSuppliers>1)
 {
  bProtected = false;
 }
 else if (AnnounceImmunity)
 {
  AnnounceTeam("Your team has built a "$BuildingName@sLocation, Team);
 }
}


function AnnounceTeam(string sMessage, int iTeam)
{
    local Pawn p;

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



simulated event Timer()
{
	Super.Timer();

	if ( SCount > 0 || Role != ROLE_Authority )
        return;

	if ( CurrentTarget == None || CurrentTarget.bDeleteMe ||
      VSize(Location - CurrentTarget.Location) > SupplyRadius ||
      CurrentTarget != None && CurrentTarget.Health <= 0)
        CurrentTarget = FindTarget();

	if ( CurrentTarget != None )
        Supply(CurrentTarget);

	count++;
	if (count>=(SuppProtectTimeSecs*10) && bProtected)
	{	
		bProtected = false;
		if (AnnounceImmunity) AnnounceTeam("Your team "$BuildingName$" immunity has expired.", Team);
	}
}

function Pawn FindTarget()
{
 local Pawn p;

 foreach RadiusActors(class'Pawn', p, SupplyRadius)
 {
  if ( p.bIsPlayer && p.Health > 0 &&
       p.PlayerReplicationInfo != None &&
       p.PlayerReplicationInfo.Team == Team)
  {
   if( bSuperSupplier )
   {
    CurrentTarget = p;
    Supply(CurrentTarget);
   }
   else
   {
    return p;
   }
  }
 }
 return None;
}

function Supply(Pawn target);

function int GetWeaponCount()
{
    if ( Grade < 2 )
        return 3;
    else if ( Grade < 3 )
        return 5;
    else if ( Grade < 4 )
        return 6;
	else if ( Grade < 5 )
        return 8;
	else if ( Grade >= 5 )
        return 9;
    return 3;
}


static function LoadWeaponClasses()
{
    local int i;
    local class<Weapon> weaponClass;

    default.ClassesLoaded = true;
    for ( i = 0; i < 9; i++ )
        if ( default.Weapons[i] != "" )
            default.WeaponClasses[i] =
              class<Weapon>(DynamicLoadObject(default.Weapons[i],
              class'Class'));
}



simulated event TakeDamage( int damage, Pawn instigatedBy, Vector hitLocation, 
  Vector momentum, name damageType )
{
	if (!bProtected)
		Super.TakeDamage(damage, instigatedBy, hitLocation, momentum, damageType);
}


defaultproperties
{
    SupplyRadius=60.00
    SkinRedTeam=None
    SkinBlueTeam=None
    
    AnnounceImmunity=True
    Weapons(0)="SiegeUltimateRC7UB01.sgEnforcer"
    Weapons(1)="Botpack.ut_biorifle"
    Weapons(2)="Botpack.ShockRifle"
    Weapons(3)="SiegeUltimateRC7UB01.sgPulseGun"
    Weapons(4)="Botpack.ripper"
    Weapons(5)="SiegeUltimateRC7UB01.sgMinigun"
    Weapons(6)="Botpack.UT_FlakCannon"
    Weapons(7)="Botpack.UT_Eightball"
    Weapons(8)="Botpack.SniperRifle"
    SuppProtectTimeSecs=6000000
}
