class UR_HUD extends UTCTFHUD;

var string NewBeginString;
var() editinline vector2D shieldGeneratorCenterPoints[2];
var float LeftTeamGenPulseTime, RightTeamGenPulseTime;
var float OldLeftNbGen, OldRightNbGen;

function DrawGameHud()
{
        local float xl, yl, ypos;
        local float TempResScale;
        local Pawn P;

        // Draw any spectator information
        if (UTOwnerPRI != None)
        {
                if (UTOwnerPRI.bOnlySpectator || UTPlayerOwner.IsInState('Spectating'))
                {
                        P = Pawn(UTPlayerOwner.ViewTarget);
                        if (P != None && P.PlayerReplicationInfo != None && P.PlayerReplicationInfo != UTOwnerPRI)
                        {
                                DisplayHUDMessage(SpectatorMessage @ "-" @ P.PlayerReplicationInfo.GetPlayerAlias(), 0.05, 0.15);
                        }
                        else
                        {
                                DisplayHUDMessage(SpectatorMessage, 0.05, 0.15);
                        }
                }
                else if ( UTOwnerPRI.bIsSpectator )
                {
                        if (UTGRI != None && UTGRI.bMatchHasBegun)
                        {
                                DisplayHUDMessage(NewBeginString);
                        }
                        else
                        {
                                DisplayHUDMessage(WaitingForMatch);
                        }

                }
                else if ( UTPlayerOwner.IsDead() )
                {
                         DisplayHUDMessage(DeadMessage @ (UTPlayerOwner.bFrozen) ? "" : NewBeginString);
                }
        }

        // Draw the Warmup if needed
        if (UTGRI != None && UTGRI.bWarmupRound)
        {
                Canvas.Font = GetFontSizeIndex(2);
                Canvas.DrawColor = WhiteColor;
                Canvas.StrLen(WarmupString, XL, YL);
                Canvas.SetPos((Canvas.ClipX - XL) * 0.5, Canvas.ClipY * 0.175);
                Canvas.DrawText(WarmupString);
        }

        if ( bCrosshairOnFriendly )
        {
                // verify that crosshair trace might hit friendly
                bGreenCrosshair = CheckCrosshairOnFriendly();
                bCrosshairOnFriendly = false;
        }
        else
        {
                bGreenCrosshair = false;
        }

        if ( bShowDebugInfo && PawnOwner != none )
        {
                Canvas.Font = GetFontSizeIndex(0);
                Canvas.DrawColor = ConsoleColor;
                Canvas.StrLen("X", XL, YL);
                YPos = 0;
                PlayerOwner.ViewTarget.DisplayDebug(self, YL, YPos);

                if (ShouldDisplayDebug('AI') && (Pawn(PlayerOwner.ViewTarget) != None))
                {
                        DrawRoute(Pawn(PlayerOwner.ViewTarget));
                }
                return;
        }

        if (bShowAllAI)
        {
                DrawAIOverlays();
        }

        if ( WorldInfo.Pauser != None )
        {
                Canvas.Font = GetFontSizeIndex(2);
                Canvas.Strlen(class'UTGameViewportClient'.default.LevelActionMessages[1],xl,yl);
                Canvas.SetDrawColor(255,255,255,255);
                Canvas.SetPos(0.5*(Canvas.ClipX - XL), 0.44*Canvas.ClipY);
                Canvas.DrawText(class'UTGameViewportClient'.default.LevelActionMessages[1]);
        }

        DisplayLocalMessages();
        DisplayConsoleMessages();

        Canvas.Font = GetFontSizeIndex(1);

        // Draw the character portrait
        if ( CharPRI != none  )
        {
                DisplayPortrait(RenderDelta);
        }

        if ( bShowClock )
        {
                   DisplayClock();
           }

        // if the player isn't dead, draw the living hud
        if ( !UTPlayerOwner.IsDead() )
        {
                DrawLivingHud();
        }

        if ( bHasMap && bShowMap && ( !bIsSplitScreen || bIsFirstPlayer ) )
        {
                TempResScale = ResolutionScale;
                if (bIsSplitScreen)
                {
                        ResolutionScale *=2;
                }
                DisplayMap();
                ResolutionScale = TEmpResScale;
        }

        DisplayDamage();

        if ( bShowQuickPick )
        {
                DisplayQuickPickMenu();
        }
}

function DisplayTeamScore()
{
          local float DestScale, W, POSX;
        local vector2d Logo;
        local byte TeamIndex;
        local LinearColor TeamLC;
        local color TextC;
        local int nbShieldGen;

    super.DisplayTeamScore();

        Canvas.DrawColor = WhiteColor;
    W = 214 * ResolutionScale;

        // Draw the Left Team Indicator
        DestScale = 1.0;
        TeamIndex = UTPlayerOwner.GetTeamNum();
        if ( (TeamIndex == 255) || bIsSplitScreen )
        {
                // spectator
                TeamIndex = 0;
                DestScale = TeamScaleModifier;
        }
        GetTeamColor(TeamIndex, TeamLC, TextC);
        POSX = Canvas.ClipX * 0.49 - W;
        Canvas.SetPos(POSX, 0);

        nbShieldGen = getNbShieldsEnabled(TeamIndex);
        if ( nbShieldGen != OldLeftNbGen )
        {
                LeftTeamGenPulseTime = WorldInfo.TimeSeconds;
        }
        OldLeftNbGen = nbShieldGen;

        if (DestScale < 1.0)
        {
                DrawGlowText(string(nbShieldGen), POSX + 97 * ResolutionScale, 45 * ResolutionScale, 50 * ResolutionScale, LeftTeamGenPulseTime, true);
        }
        else
        {
                DrawGlowText(string(nbShieldGen), POSX + 124 * ResolutionScale, 45 * ResolutionScale, 60 * ResolutionScale, LeftTeamGenPulseTime, true);
        }

        Logo.X = POSX + ((shieldGeneratorCenterPoints[0].X) * DestScale * ResolutionScale) + (30 * ResolutionScale);
        Logo.Y = ((shieldGeneratorCenterPoints[0].Y) * DestScale * ResolutionScale) + (27.5 * ResolutionScale);

    DisplayShieldGenerator(TeamIndex,Logo, 1.5);

        // Draw the Right Team Indicator
        DestScale = TeamScaleModifier;
        TeamIndex = 1 - TeamIndex;
        GetTeamColor(TeamIndex, TeamLC, TextC);
        POSX = Canvas.ClipX * 0.51;

        nbShieldGen = getNbShieldsEnabled(TeamIndex);
        if ( nbShieldGen != OldRightNbGen )
        {
                RightTeamGenPulseTime = WorldInfo.TimeSeconds;
        }
        OldRightNbGen = nbShieldGen;
    Canvas.SetPos(POSX,0);
        Canvas.DrawColor = WhiteColor;
        DrawGlowText(string(nbShieldGen), POSX + 0.66*W, 35 * ResolutionScale, 50 * ResolutionScale, RightTeamGenPulseTime, true);

        Logo.X = (POSX + (shieldGeneratorCenterPoints[1].X) * DestScale * ResolutionScale) + (30 * ResolutionScale);
        Logo.Y = ((shieldGeneratorCenterPoints[1].Y) * DestScale * ResolutionScale) + (27.5 * ResolutionScale);
    DisplayShieldGenerator(TeamIndex,Logo, 1.0);
}

function int getNbShieldsEnabled(byte TeamIndex)
{
        local UR_ShieldGenerator  UR_G;
        local int nbShieldGen;
    foreach AllActors(class'UR_ShieldGenerator', UR_G)
    {
      if(UR_G.DefenderTeamIndex == TeamIndex && UR_G.genEnable)
        nbShieldGen++;
    }

    return nbShieldGen;
}

/*
 * This method is used to retrieved ShieldGenerator
 * if shield is owned by HUDPlayerOwner, display the nearest disable ShieldGenerator
 * if not display the nearest ShieldGenerator to disable
 *
 * Dirty but it works !!! (should be rewritten)
*/
function Actor findNearestShield(byte TeamIndex)
{
        local UR_ShieldGenerator  UR_G, nearestShieldGen;
        local float distanceMin;

    if(PawnOwner == None)
      return none;

    foreach AllActors(class'UR_ShieldGenerator', UR_G)
    {

      if(PlayerOwner.PlayerReplicationInfo.Team.TeamIndex == TeamIndex)
      {
          if(UR_G.DefenderTeamIndex == PlayerOwner.PlayerReplicationInfo.Team.TeamIndex && !UR_G.genEnable )
          {
            if(nearestShieldGen == None)
            {
               nearestShieldGen = UR_G;
               distanceMin = VSize(PawnOwner.Location - UR_G.Location);
               continue;
            }

            if(distanceMin > Vsize(PawnOwner.Location - UR_G.Location))
            {
               nearestShieldGen = UR_G;
               distanceMin = VSize(PawnOwner.Location - UR_G.Location);
            }
          }
      }
      else
      {
          if(UR_G.DefenderTeamIndex != PlayerOwner.PlayerReplicationInfo.Team.TeamIndex && UR_G.genEnable)
          {

            if(nearestShieldGen == None)
            {
              nearestShieldGen = UR_G;
              distanceMin = VSize(PawnOwner.Location - UR_G.Location);
              continue;
            }

            if(distanceMin > Vsize(PawnOwner.Location - UR_G.Location))
            {
              nearestShieldGen = UR_G;
              distanceMin = VSize(PawnOwner.Location - UR_G.Location);
            }
          }
      }
    }

    return nearestShieldGen;
}

function DisplayShieldGenerator(byte TeamIndex, vector2d POS, optional float DestScale=1.0)
{
        if ( bShowDirectional )
        {
                DisplayDirectionIndicator(TeamIndex, POS, findNearestShield(TeamIndex), DestScale );
        }
}

DefaultProperties
{
        NewBeginString="Press [FIRE] to join, Press [ALTFIRE] to choose class."
        shieldGeneratorCenterPoints(0)=(x=75,y=52)
        shieldGeneratorCenterPoints(1)=(x=135,y=43)
}