//-------------------------------------------------------------------
//
//  This code is copyright 2001 by G5 Software.
//  Any unauthorized usage, either in part or in whole of this code
//  is strictly prohibited. Violators WILL be prosecuted to the
//  maximum extent allowed by law.
//
//-------------------------------------------------------------------

class CDestroyGuardedTarget extends
  CBaseMission, CDestroyGuardedTargetObjectList, CDestroyGuardedTarget_Strings, CNavPointUser
{
  int GetAutoGeneratedUnitsQty()
  {
    return 6;
  }

  void CDestroyGuardedTarget()
  {
    BaseMission_InitMission();

    BaseMission_UpdateLoadProgress();
    CreateComponent("DebugCamera", "GameObject", "CDebugCamera");
    SetComponentPosition("DebugCamera",
      matrix(
        1.0, 0.0, 0.0, 8000.0,
        0.0, 1.0, 0.0, 8000.0,
        0.0, 0.0, 1.0,  600.0,
        0.0, 0.0, 0.0,    1.0
      ));

    // CreateComponent(IDToRegister, ComponentID, ScriptName | FileName | "")
    BaseMission_UpdateLoadProgress();
    CreateComponent("Atmosphere", "Atmosphere", "CDestroyGuardedTarget_Atmosphere");

    BaseMission_UpdateLoadProgress();
    CreateComponent("Sky", "SkyObject", "CDestroyGuardedTarget_Sky");

    BaseMission_UpdateLoadProgress();
    CreateComponent("Terrain", "ProgressiveTerrainObject", "CDestroyGuardedTarget_Terrain");

    BaseMission_UpdateLoadProgress();
    CreateComponent("Forest", "Forest", "CDestroyGuardedTarget_Forest");

    BaseMission_UpdateLoadProgress();
    CreateComponent("AIController", "AIController", "CDestroyGuardedTarget_AIController");

    BaseMission_CreateObjects();

   // West Cargo direction
   SetBehaviorGroupRank( "CargoWest_01", "CargoWest_Rank", 1);
   SetBehaviorGroupRank( "CargoWest_02", "CargoWest_Rank", 2);
   SetBehaviorGroupRank( "CargoWest_03", "CargoWest_Rank", 3);

   // Sude Cargo direction
   SetBehaviorGroupRank( "CargoSude_01", "CargoSude_Rank", 1);
   SetBehaviorGroupRank( "CargoSude_02", "CargoSude_Rank", 2);
   SetBehaviorGroupRank( "CargoSude_03", "CargoSude_Rank", 3);

   // Nord Cargo direction
   SetBehaviorGroupRank( "CargoNord_01", "CargoNord_Rank", 1);
   SetBehaviorGroupRank( "CargoNord_02", "CargoNord_Rank", 2);
   SetBehaviorGroupRank( "CargoNord_03", "CargoNord_Rank", 3);

    //
    //  Mission specific
    //

    // count how many 1st and 2nd objectives' objects are in the mission
    array m_Objects = GetObjectsIDs();

    m_HousesCount  = 0;
    m_CargoCount   = 0;
    m_CargoToKill  = 9;
    m_EnemiesCount = 0;
    m_EnemiesDestroyed = 0;

    for (int i = 0; i < m_Objects.size(); i = i + 1)
    {
      if (Core_IsStringStartsWith(m_Objects[i], "TargetHouse"))
        m_HousesCount = m_HousesCount + 1;

      if (Core_IsStringStartsWith(m_Objects[i], "Nazi"))
        m_EnemiesCount = m_EnemiesCount + 1;

//      if (Core_IsStringStartsWith(m_Objects[i], "Cargo"))
//        m_CargoToKill = m_CargoToKill + 1;
    }
//    Core_LogMessage("Defender to kill: "+m_EnemiesCount+". Cargo to kill: "+m_CargoToKill);
  }

  int  m_HousesCount;
  int  m_EnemiesCount;
  int  m_EnemiesDestroyed;

  int  m_CargoCount  = 0;
  int  m_CargoToKill = 9;

  int  OBJECTIVE_HQ                 = 0;
  int  OBJECTIVE_CARGO              = 1;
  int  OBJECTIVE_ALL_ENEMIES        = 0;

  array m_MissionObjectivesStatuses =
          array(
            str_ObjectiveInProgress,
            str_ObjectiveInProgress
          );

  array m_BonusMissionObjectivesStatuses =
          array(
            str_ObjectiveInProgress
          );

  //
  //  'virtual' methods
  //

  //
  //  Mission statistics

  string GetMissionStatistics()
  {
    return str_StatisticsTitle + m_EnemiesDestroyed;
  }

  //
  //  Mission navpoints

  array GetNavPoints()
  {
    array navpoints =
      array(
//        vector(11430.0, 4720.0, 600.0)
          GetNavPoint("NavPoint_1")
      );
    return navpoints;
  }

  //
  //  Mission map skin file

  string GetMapSkinFileName()
  {
    return "Missions/Mission_4/Map.skin";
  }

  //
  //  Mission specific event handlers
  //

  //
  //  Object destroyed event handler
  //

  void OnGameObjectDestroyed(string _id)
  {
    BaseMission_OnGameObjectDestroyed(_id);

    if (Core_IsStringStartsWith(_id, "Nazi"))
    {
      m_EnemiesCount = m_EnemiesCount - 1;
      m_EnemiesDestroyed = m_EnemiesDestroyed + 1;
//      Core_LogMessage("Defender has been killed. Defenders left: "+m_EnemiesCount);
    }

    if (Core_IsStringStartsWith(_id, "Field"))
     {
      m_EnemiesDestroyed = m_EnemiesDestroyed + 1;
     }

    if (Core_IsStringStartsWith(_id, "NaziPatrol"))
     {
      Core_BroadcastEvent("StartRunFromBase");
     }

    if (Core_IsStringStartsWith(_id, "TargetHouse"))
    {
      m_HousesCount = m_HousesCount - 1;
      Core_BroadcastEvent("StartRunFromBase");

      if (m_HousesCount > 0)
      {
        string message = str_MessageTitle + m_HousesCount;

        Core_BroadcastEvent(
          "OnDisplayMessage",
          message,
          m_GoodNewsColor
        );
      }
    }

    if (Core_IsStringStartsWith(_id, "Cargo"))
    {
      m_CargoCount = m_CargoCount + 1;
      m_EnemiesDestroyed = m_EnemiesDestroyed + 1;
//      Core_LogMessage(" Cargo has killed, Name: "+_id+". Current Count is: "+m_CargoCount+". Need to kill: "+m_CargoToKill);
    }

    // check for bonus objective completion
    if (m_EnemiesCount == 0)
      BaseMission_CompleteBonusObjective(OBJECTIVE_ALL_ENEMIES);

    // check for main objective
    if (m_HousesCount == 0)
    {
      BaseMission_CompleteObjective(OBJECTIVE_HQ);
    }

    // check for main objective
    if (m_CargoCount >= m_CargoToKill)
    {
//      Core_LogMessage("Objective complete");
      BaseMission_CompleteObjective(OBJECTIVE_CARGO);
    }

  }

  void OnMissionLoaded()
  {
    Core_SendEventTo("Helicopter", "OnInitiallyEnableTargetScreen", false);

    // Start mission music playing
    Core_SendEventTo(SOID_MusicController, "PlayMissionMusic", 4);
  }

  void WestCargoArrive()
    {
      BaseMission_FailObjective(OBJECTIVE_CARGO);
//      BaseMission_DelayedQuit();
    }

  void SudeCargoArrive()
    {
      BaseMission_FailObjective(OBJECTIVE_CARGO);
//      BaseMission_DelayedQuit();
    }

  void NordCargoArrive()
    {
      BaseMission_FailObjective(OBJECTIVE_CARGO);
//      BaseMission_DelayedQuit();
    }

}
