//-----------------------------------------------
//  DDStdlib.cs
//  Version 1.02
//
//  Author: ACrisp[oAo]
//-----------------------------------------------

//-----------------------------------------------
// This script contains common files needed for Defend and Destroy type missions for Starsiege
// Functions include: Mission briefing, scoreboard setup and tracking, structure. vehicle, and turret tracking functions. 
//
//-----------------------------------------------


function setRules()
{
   // modified from the setRules() function found in CTFStdlib.cs

   %rules = "<F2>GAME TYPE:\n<F0> Defend and Destroy\n\n"       @        
            "<tIDMULT_CTF_MAPNAME>"       		        @ 
            $missionName                  		        @ 
            "\n\n<F2>Briefing:\n<f0>"     		        @
            $briefingTxt		  		        @
            "\n\n<F2>OBJECTIVES AND SCORING:\n<F0>"		@
	    "The following objects are targets.  Defend your own while trying to destroy your enemy's target objects.  Each object is worth <F3>" @
	    $criticalPoints					@
	    " <F0>points.\n\n - ";					

  if ($objective1 != "") %rules = %rules @ $objective1 @ "\n";
  if ($objective2 != "") %rules = %rules @ " - " @ $objective2 @ "\n";
  if ($objective3 != "") %rules = %rules @ " - " @ $objective3 @ "\n";
  if ($objective4 != "") %rules = %rules @ " - " @ $objective4 @ "\n";
  if ($objective5 != "") %rules = %rules @ " - " @ $objective5 @ "\n";
  if ($objective6 != "") %rules = %rules @ " - " @ $objective6 @ "\n";

  if ($capturable1 != "" ) %rules = %rules @ "\nCapture " @ $capturable1 @ " and hold it to retain <F3>" @ $capturablePoints @ "<F0> points.\n";
  if ($capturable2 != "" ) %rules = %rules @ "Capture " @ $capturable2 @ " and hold it to retain <F3>" @ $capturablePoints @ "<F0> points.\n";
  if ($capturable3 != "" ) %rules = %rules @ "Capture " @ $capturable3 @ " and hold it to retain <F3>" @ $capturablePoints @ "<F0> points.\n";

   %rules = %rules @ "\n" @
            "<tIDMULT_TDM_SCORING_2>"  @
            $killPoints                @
            "<tIDMULT_TDM_SCORING_3>"  @
            "<tIDMULT_TDM_SCORING_4>"  @
            $deathPoints               @
            "<tIDMULT_TDM_SCORING_5>";

  if ($noFactions == FALSE)
   {
    %rules = %rules @ "\n" @
             "<F2>FACTIONS PERMITTED:<F0>\n";
    if ($yellowtech != "none") %rules = %rules @ "Yellow Team: " @ $yellowtech @ "\n";
    if ($bluetech != "none") %rules = %rules @ "Blue Team:   " @ $bluetech @ "\n";
    if ($redtech != "none") %rules = %rules @ "Red Team:    " @ $redtech @ "\n";
    if ($purpletech != "none") %rules = %rules @ "Purple Team: " @ $purpletech;
   } 

            
   
   setGameInfo(%rules);
}

function player::onAdd(%who)
{
  say(%who, 1, "Welcome to Starsiege Defend and Destroy!  Read the game info tab for rules and objectives.");
  %who.baseCaptures = 0;
  %who.criticalsOffed = 0;

}

function initScoreBoard()
{
   deleteVariables("$ScoreBoard::PlayerColumn*");
   deleteVariables("$ScoreBoard::TeamColumn*");

   if($server::TeamPlay == "True")	
   {
	   // Player ScoreBoard column headings
	   $ScoreBoard::PlayerColumnHeader1 = *IDMULT_SCORE_TEAM;
	   $ScoreBoard::PlayerColumnHeader2 = *IDMULT_SCORE_SQUAD;
	   $ScoreBoard::PlayerColumnHeader3 = *IDMULT_SCORE_SCORE;
	   $ScoreBoard::PlayerColumnHeader4 = *IDMULT_SCORE_KILLS;
	   $ScoreBoard::PlayerColumnHeader5 = *IDMULT_SCORE_DEATHS;
	   $ScoreBoard::PlayerColumnHeader6 = "Base Captures";
	   $ScoreBoard::PlayerColumnHeader7 = "Critical Items Destroyed";


	   // Player ScoreBoard column functions
	   $ScoreBoard::PlayerColumnFunction1 = "getTeam";
	   $ScoreBoard::PlayerColumnFunction2 = "getSquad";
	   $ScoreBoard::PlayerColumnFunction3 = "getPlayerScore";
	   $ScoreBoard::PlayerColumnFunction4 = "getKills";
	   $ScoreBoard::PlayerColumnFunction5 = "getDeaths";
	   $ScoreBoard::PlayerColumnFunction6 = "getPlayerBaseCaptures";
	   $ScoreBoard::PlayerColumnFunction7 = "getPlayerCriticalsDestroyed";

   }

   // Team ScoreBoard column headings
   $ScoreBoard::TeamColumnHeader1 = *IDMULT_SCORE_SCORE;
   $ScoreBoard::TeamColumnHeader2 = "Critical Items Remaining";
   $ScoreBoard::TeamColumnHeader3 = "Captured Bases";
   $ScoreBoard::TeamColumnHeader4 = *IDMULT_SCORE_PLAYERS;

   // Team ScoreBoard column functions
   $ScoreBoard::TeamColumnFunction1 = "getTeamScore";
   $ScoreBoard::TeamColumnFunction2 = "getCriticalsRemaining";
   $ScoreBoard::TeamColumnFunction3 = "getCapturedBases";
   $ScoreBoard::TeamColumnFunction4 = "getNumberOfPlayersOnTeam";

   // tell server to process all the scoreboard definitions defined above
   serverInitScoreBoard();
}

function getPlayerScore(%a)
{
   return (
         (getPlayerBaseCaptures(%a)) +
         (getPlayerCriticalsDestroyed(%a) * $criticalPoints) +
         (getKills(%a)) -
         (getDeaths(%a))
         );
}

function getPlayerBaseCaptures(%a)
{
   return %a.baseCaptures;
}

function getPlayerCriticalsDestroyed(%a)
{
   return %a.criticalsOffed;
}

function getTeamScore(%team)
{
   %criticalScore = 0;
   if ( getTeamNameFromTeamId(%team) == *IDSTR_TEAM_YELLOW )
    {
      %criticalScore = (($blueCriticalsLost + $redCriticalsLost + $purpleCriticalsLost) * $criticalPoints);
    }
   else if ( getTeamNameFromTeamId(%team) == *IDSTR_TEAM_BLUE )
    {
      %criticalScore = (($yellowCriticalsLost + $redCriticalsLost + $purpleCriticalsLost) * $criticalPoints);
    }
   else if ( getTeamNameFromTeamId(%team) == *IDSTR_TEAM_RED )
    {
      %criticalScore = (($blueCriticalsLost + $yellowCriticalsLost + $purpleCriticalsLost) * $criticalPoints);
    }
   else if ( getTeamNameFromTeamId(%team) == *IDSTR_TEAM_PURPLE )
    {
      %criticalScore = (($blueCriticalsLost + $redCriticalsLost + $yellowCriticalsLost) * $criticalPoints);
    }
   return (
         (%criticalScore) +
         (getCapturedBases(%team) * $capturablePoints)
          );
}

function getCriticalsRemaining(%team)
{
   if( getTeamNameFromTeamId(%team) == *IDSTR_TEAM_YELLOW )
   {
      return ($totalCriticals - $yellowCriticalsLost);
   }
   else if( getTeamNameFromTeamId(%team) == *IDSTR_TEAM_RED )
   {
      return ($totalCriticals - $redCriticalsLost);
   }
   else if( getTeamNameFromTeamId(%team) == *IDSTR_TEAM_BLUE )
   {
      return ($totalCriticals - $blueCriticalsLost);
   }
   else if( getTeamNameFromTeamId(%team) == *IDSTR_TEAM_PURPLE )
   {
      return ($totalCriticals - $purpleCriticalsLost);
   }
   else
   {
      return "0";
   }

}

function getCapturedBases(%team)
{
   %numberofBasesOwned = 0;
   %teamName = getTeamNameFromTeamId(%team);
   if( $base1Owner == %teamName)
   {
      %numberofBasesOwned++;
   }
   if( $base2Owner == %teamName)
   {
      %numberofBasesOwned++;
   }
   if( $base3Owner == %teamName)
   {
      %numberofBasesOwned++;
   }

   return %numberofBasesOwned;
  
}

function placeObject (%object, %x, %y, %rot)
{
  // function by the legendary Dolf Kooz
  if ( %rot == "" ) %rot = 0;

  %z = getTerrainHeight(%x, %y);
  setPosition (%object, %x, %y, %z + 2, %rot);

}

function yellowCritical::turret::onDestroyed(%this, %who)
 {
   %txt = "<f5>" @ getHUDname(%who) @ " has destroyed an enemy turret!";
   say(IDSTR_TEAM_RED, 1, %txt, "ene_struct_dest.wav");
   say(IDSTR_TEAM_BLUE, 1, %txt, "ene_struct_dest.wav");
   say(IDSTR_TEAM_PURPLE, 1, %txt, "ene_struct_dest.wav");

   say(IDSTR_TEAM_YELLOW, 1, "<f5>One of your turrets has been destroyed!", "friend_struct_des.wav");


   $yellowCriticalsLost++;
   if ($yellowCriticalsLost == $totalCriticals)
    {
      winCondition();
    }

 }

function blueCritical::turret::onDestroyed(%this, %who)
 {
   %txt = "<f5>" @ getHUDname(%who) @ " has destroyed an enemy turret!";
   say(IDSTR_TEAM_RED, 1, %txt, "ene_struct_dest.wav");
   say(IDSTR_TEAM_YELLOW, 1, %txt, "ene_struct_dest.wav");
   say(IDSTR_TEAM_PURPLE, 1, %txt, "ene_struct_dest.wav");

   say(IDSTR_TEAM_BLUE, 1, "<f5>One of your turrets has been destroyed!", "friend_struct_des.wav");


   $blueCriticalsLost++;
   if ($blueCriticalsLost == $totalCriticals)
    {
      winCondition();
    }

 }

function redCritical::turret::onDestroyed(%this, %who)
 {
   %txt = "<f5>" @ getHUDname(%who) @ " has destroyed an enemy turret!";
   say(IDSTR_TEAM_YELLOW, 1, %txt, "ene_struct_dest.wav");
   say(IDSTR_TEAM_BLUE, 1, %txt, "ene_struct_dest.wav");
   say(IDSTR_TEAM_PURPLE, 1, %txt, "ene_struct_dest.wav");

   say(IDSTR_TEAM_RED, 1, "<f5>One of your turrets has been destroyed!", "friend_struct_des.wav");


   $redCriticalsLost++;
   if ($redCriticalsLost == $totalCriticals)
    {
      winCondition();
    }

 }

function purpleCritical::turret::onDestroyed(%this, %who)
 {
   %txt = "<f5>" @ getHUDname(%who) @ " has destroyed an enemy turret!";
   say(IDSTR_TEAM_RED, 1, %txt, "ene_struct_dest.wav");
   say(IDSTR_TEAM_BLUE, 1, %txt, "ene_struct_dest.wav");
   say(IDSTR_TEAM_YELLOW, 1, %txt, "ene_struct_dest.wav");

   say(IDSTR_TEAM_PURPLE, 1, "<f5>One of your turrets has been destroyed!", "friend_struct_des.wav");


   $purpleCriticalsLost++;
   if ($purpleCriticalsLost == $totalCriticals)
    {
      winCondition();
    }

 }

function yellowCritical::structure::onDestroyed(%this, %who)
 {
   %txt = "<f5>" @ getHUDname(%who) @ " has destroyed an enemy critical structure!";
   say(IDSTR_TEAM_RED, 1, %txt, "ene_struct_dest.wav");
   say(IDSTR_TEAM_BLUE, 1, %txt, "ene_struct_dest.wav");
   say(IDSTR_TEAM_PURPLE, 1, %txt, "ene_struct_dest.wav");

   say(IDSTR_TEAM_YELLOW, 1, "<f5>One of your critical structures has been destroyed!", "friend_struct_des.wav");


   $yellowCriticalsLost++;
   if ($yellowCriticalsLost == $totalCriticals)
    {
      winCondition();
    }

 }

function blueCritical::structure::onDestroyed(%this, %who)
 {
   %txt = "<f5>" @ getHUDname(%who) @ " has destroyed an enemy critical structure!";
   say(IDSTR_TEAM_RED, 1, %txt, "ene_struct_dest.wav");
   say(IDSTR_TEAM_YELLOW, 1, %txt, "ene_struct_dest.wav");
   say(IDSTR_TEAM_PURPLE, 1, %txt, "ene_struct_dest.wav");

   say(IDSTR_TEAM_BLUE, 1, "<f5>One of your critical structures has been destroyed!", "friend_struct_des.wav");


   $blueCriticalsLost++;
   if ($blueCriticalsLost == $totalCriticals)
    {
      winCondition();
    }

 }

function redCritical::structure::onDestroyed(%this, %who)
 {
   %txt = "<f5>" @ getHUDname(%who) @ " has destroyed an enemy critical structure!";
   say(IDSTR_TEAM_YELLOW, 1, %txt, "ene_struct_dest.wav");
   say(IDSTR_TEAM_BLUE, 1, %txt, "ene_struct_dest.wav");
   say(IDSTR_TEAM_PURPLE, 1, %txt, "ene_struct_dest.wav");

   say(IDSTR_TEAM_RED, 1, "<f5>One of your critical structures has been destroyed!", "friend_struct_des.wav");


   $redCriticalsLost++;
   if ($redCriticalsLost == $totalCriticals)
    {
      winCondition();
    }

 }

function purpleCritical::structure::onDestroyed(%this, %who)
 {
   %txt = "<f5>" @ getHUDname(%who) @ " has destroyed an enemy critical structure!";
   say(IDSTR_TEAM_RED, 1, %txt, "ene_struct_dest.wav");
   say(IDSTR_TEAM_BLUE, 1, %txt, "ene_struct_dest.wav");
   say(IDSTR_TEAM_YELLOW, 1, %txt, "ene_struct_dest.wav");

   say(IDSTR_TEAM_PURPLE, 1, "<f5>One of your critical structures has been destroyed!", "friend_struct_des.wav");


   $purpleCriticalsLost++;
   if ($purpleCriticalsLost == $totalCriticals)
    {
      winCondition();
    }

 }


function winCondition()
 {
   flushConsoleScheduler();
   schedule("missionEndConditionMet();", 2);

 }
