//-------------------------------------------------------------------
//
//  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 CConvoyMission extends
  CBaseMission, CConvoyMissionObjectList, CConvoyMission_Strings, CNavPointUser
{
  //
  //  settings
  //

  int GetAutoGeneratedUnitsQty()
  {
    return 6;
  }

  void CConvoyMission()
  {
    BaseMission_InitMission();

    BaseMission_UpdateLoadProgress();
    CreateComponent("DebugCamera", "GameObject", "CDebugCamera");
    SetComponentPosition("DebugCamera",
      matrix(
        1.0, 0.0, 0.0, 5000.0,
        0.0, 1.0, 0.0,  400.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", "CConvoyMission_Atmosphere");

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

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

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

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

    BaseMission_CreateObjects();


    // create AI controller


    // give tasks to AI game objects

    // convoy guards
    SetBehaviorTask( "SovietTank_Guard 1", "CConvoyMission_SovietTank_ConvoyGuard");
    SetBehaviorTask( "SovietTank_Guard 2", "CConvoyMission_SovietTank_ConvoyGuard");
    SetBehaviorTask( "SovietTank_Guard 3", "CConvoyMission_SovietTank_ConvoyGuard");
    SetBehaviorTask( "SovietTank_Guard 4", "CConvoyMission_SovietTank_ConvoyGuard");

    // convoy transports
    SetBehaviorTask( "SovietTruck_Transport 1", "CConvoyMission_SovietTruck_ConvoyTransport");
    SetBehaviorTask( "SovietTruck_Transport 2", "CConvoyMission_SovietTruck_ConvoyTransport");
    SetBehaviorTask( "SovietTruck_Transport 3", "CConvoyMission_SovietTruck_ConvoyTransport");
    SetBehaviorTask( "SovietTruck_Transport 4", "CConvoyMission_SovietTruck_ConvoyTransport");
    SetBehaviorTask( "SovietTruck_Transport 5", "CConvoyMission_SovietTruck_ConvoyTransport");
    SetBehaviorTask( "SovietTruck_Transport 6", "CConvoyMission_SovietTruck_ConvoyTransport");
    SetBehaviorTask( "SovietTruck_Transport 7", "CConvoyMission_SovietTruck_ConvoyTransport");
    SetBehaviorTask( "SovietTruck_Transport 8", "CConvoyMission_SovietTruck_ConvoyTransport");
    SetBehaviorTask( "SovietTruck_Transport 9", "CConvoyMission_SovietTruck_ConvoyTransport");
    SetBehaviorTask( "SovietTruck_Transport 10", "CConvoyMission_SovietTruck_ConvoyTransport");
    SetBehaviorTask( "SovietTruck_Transport 11", "CConvoyMission_SovietTruck_ConvoyTransport");
    SetBehaviorTask( "SovietTruck_Transport 12", "CConvoyMission_SovietTruck_ConvoyTransport");

    // Ambush 1
    SetBehaviorTask( "NaziTank_Ambusher_1 1", "CConvoyMission_NaziTank_BackAttacker_1");
    SetBehaviorTask( "NaziTank_Ambusher_1 2", "CConvoyMission_NaziTank_BackAttacker_1");

    // Ambush 2
    SetBehaviorTask( "NaziTank_Ambusher_2 1", "CConvoyMission_NaziTank_BackAttacker_2");
    SetBehaviorTask( "NaziTank_Ambusher_2 2", "CConvoyMission_NaziTank_BackAttacker_2");


    // all other ambushes
    SetBehaviorTask( "NaziTank_Ambush_1 1", "CConvoyMission_NaziTank_TransportKiller");
    SetBehaviorTask( "NaziTank_Ambush_2 1", "CConvoyMission_NaziTank_TransportKiller");
    SetBehaviorTask( "NaziTank_Ambush_3 1", "CConvoyMission_NaziTank_TransportKiller");
    SetBehaviorTask( "NaziTank_Ambush_4 1", "CConvoyMission_NaziTank_TransportKiller");
    SetBehaviorTask( "NaziTank_Ambush_5 1", "CConvoyMission_NaziTank_TransportKiller");
    SetBehaviorTask( "NaziTank_Ambush_1 2", "CConvoyMission_NaziTank_TransportKiller");
    SetBehaviorTask( "NaziTank_Ambush_2 2", "CConvoyMission_NaziTank_TransportKiller");
    SetBehaviorTask( "NaziTank_Ambush_2 3", "CConvoyMission_NaziTank_TransportKiller");
    SetBehaviorTask( "NaziTank_Ambush_2 4", "CConvoyMission_NaziTank_TransportKiller");
    SetBehaviorTask( "NaziTank_Ambush_3 2", "CConvoyMission_NaziTank_TransportKiller");
    SetBehaviorTask( "NaziTank_Ambush_3 3", "CConvoyMission_NaziTank_TransportKiller");
    SetBehaviorTask( "NaziTank_Ambush_3 4", "CConvoyMission_NaziTank_TransportKiller");
    SetBehaviorTask( "NaziTank_Ambush_4 2", "CConvoyMission_NaziTank_TransportKiller");
    SetBehaviorTask( "NaziTank_Ambush_4 3", "CConvoyMission_NaziTank_TransportKiller");
    SetBehaviorTask( "NaziTank_Ambush_4 4", "CConvoyMission_NaziTank_TransportKiller");
    SetBehaviorTask( "NaziTank_Ambush_5 2", "CConvoyMission_NaziTank_TransportKiller");
    SetBehaviorTask( "NaziTank_Ambush_5 3", "CConvoyMission_NaziTank_TransportKiller");
    SetBehaviorTask( "NaziTank_Ambush_5 4", "CConvoyMission_NaziTank_TransportKiller");
    SetBehaviorTask( "NaziTank_Ambush_5 5", "CConvoyMission_NaziTank_TransportKiller");
    SetBehaviorTask( "NaziTank_Ambush_5 6", "CConvoyMission_NaziTank_TransportKiller");
    SetBehaviorTask( "NaziTank_Ambush_5 7", "CConvoyMission_NaziTank_TransportKiller");
    SetBehaviorTask( "NaziTank_Ambush_5 8", "CConvoyMission_NaziTank_TransportKiller");
    SetBehaviorTask( "NaziTank_Ambush_6 1", "CConvoyMission_NaziTank_TransportKiller");
    SetBehaviorTask( "NaziTank_Ambush_6 2", "CConvoyMission_NaziTank_TransportKiller");
    SetBehaviorTask( "NaziTank_Ambush_6 3", "CConvoyMission_NaziTank_TransportKiller");
    SetBehaviorTask( "NaziTank_Ambush_6 4", "CConvoyMission_NaziTank_TransportKiller");
    SetBehaviorTask( "NaziTank_Ambush_6 5", "CConvoyMission_NaziTank_TransportKiller");
    SetBehaviorTask( "NaziTank_Ambush_6 6", "CConvoyMission_NaziTank_TransportKiller");
    SetBehaviorTask( "NaziTank_Ambush_6 7", "CConvoyMission_NaziTank_TransportKiller");
    SetBehaviorTask( "NaziTank_Ambush_6 8", "CConvoyMission_NaziTank_TransportKiller");
    SetBehaviorTask( "NaziTank_Ambush_6 9", "CConvoyMission_NaziTank_TransportKiller");
    SetBehaviorTask( "NaziTank_Ambush_6 10", "CConvoyMission_NaziTank_TransportKiller");
    SetBehaviorTask( "NaziTank_Ambush_6 11", "CConvoyMission_NaziTank_TransportKiller");
    SetBehaviorTask( "NaziTank_Ambush_6 12", "CConvoyMission_NaziTank_TransportKiller");
    SetBehaviorTask( "NaziTank_Ambush_6 13", "CConvoyMission_NaziTank_TransportKiller");
    SetBehaviorTask( "NaziTank_Ambush_6 14", "CConvoyMission_NaziTank_TransportKiller");
    SetBehaviorTask( "NaziTank_Ambush_6 15", "CConvoyMission_NaziTank_TransportKiller");
    SetBehaviorTask( "NaziTank_Ambush_6 16", "CConvoyMission_NaziTank_TransportKiller");
    SetBehaviorTask( "NaziTank_Ambush_6 17", "CConvoyMission_NaziTank_TransportKiller");
    SetBehaviorTask( "NaziTank_Ambush_6 18", "CConvoyMission_NaziTank_TransportKiller");


    // guardians direction
    SetBehaviorGroupRank( "SovietTank_Guard 1", "Guard_Rank", 1);
    SetBehaviorGroupRank( "SovietTank_Guard 2", "Guard_Rank", 2);
    SetBehaviorGroupRank( "SovietTank_Guard 3", "Guard_Rank", 3);
    SetBehaviorGroupRank( "SovietTank_Guard 4", "Guard_Rank", 4);

    // column direction
    SetBehaviorGroupRank( "SovietTank_Guard 1", "Convoy_Rank", 1);
    SetBehaviorGroupRank( "SovietTank_Guard 2", "Convoy_Rank", 2);
    SetBehaviorGroupRank( "SovietTank_Guard 3", "Convoy_Rank", 3);
    SetBehaviorGroupRank( "SovietTank_Guard 4", "Convoy_Rank", 4);
    SetBehaviorGroupRank( "SovietTruck_Transport 1", "Convoy_Rank", 5);
    SetBehaviorGroupRank( "SovietTruck_Transport 2", "Convoy_Rank", 6);
    SetBehaviorGroupRank( "SovietTruck_Transport 3", "Convoy_Rank", 7);
    SetBehaviorGroupRank( "SovietTruck_Transport 4", "Convoy_Rank", 8);
    SetBehaviorGroupRank( "SovietTruck_Transport 5", "Convoy_Rank", 9);
    SetBehaviorGroupRank( "SovietTruck_Transport 6", "Convoy_Rank", 10);
    SetBehaviorGroupRank( "SovietTruck_Transport 7", "Convoy_Rank", 11);
    SetBehaviorGroupRank( "SovietTruck_Transport 8", "Convoy_Rank", 12);
    SetBehaviorGroupRank( "SovietTruck_Transport 9", "Convoy_Rank", 13);
    SetBehaviorGroupRank( "SovietTruck_Transport 10", "Convoy_Rank", 14);
    SetBehaviorGroupRank( "SovietTruck_Transport 11", "Convoy_Rank", 15);
    SetBehaviorGroupRank( "SovietTruck_Transport 12", "Convoy_Rank", 16);

    // ambush direction
    SetBehaviorGroupRank( "NaziTank_Ambush_1 1", "RangedAmbushRank", 1);
    SetBehaviorGroupRank( "NaziTank_Ambush_2 1", "RangedAmbushRank", 2);
    SetBehaviorGroupRank( "NaziTank_Ambush_3 1", "RangedAmbushRank", 3);
    SetBehaviorGroupRank( "NaziTank_Ambush_4 1", "RangedAmbushRank", 4);
    SetBehaviorGroupRank( "NaziTank_Ambush_5 1", "RangedAmbushRank", 5);
    SetBehaviorGroupRank( "NaziTank_Ambush_1 2", "RangedAmbushRank", 6);
    SetBehaviorGroupRank( "NaziTank_Ambush_2 2", "RangedAmbushRank", 7);
    SetBehaviorGroupRank( "NaziTank_Ambush_2 3", "RangedAmbushRank", 8);
    SetBehaviorGroupRank( "NaziTank_Ambush_2 4", "RangedAmbushRank", 9);
    SetBehaviorGroupRank( "NaziTank_Ambush_3 2", "RangedAmbushRank", 10);
    SetBehaviorGroupRank( "NaziTank_Ambush_3 3", "RangedAmbushRank", 11);
    SetBehaviorGroupRank( "NaziTank_Ambush_3 4", "RangedAmbushRank", 12);
    SetBehaviorGroupRank( "NaziTank_Ambush_4 2", "RangedAmbushRank", 13);
    SetBehaviorGroupRank( "NaziTank_Ambush_4 3", "RangedAmbushRank", 14);
    SetBehaviorGroupRank( "NaziTank_Ambush_4 4", "RangedAmbushRank", 15);
    SetBehaviorGroupRank( "NaziTank_Ambush_5 2", "RangedAmbushRank", 16);
    SetBehaviorGroupRank( "NaziTank_Ambush_5 3", "RangedAmbushRank", 17);
    SetBehaviorGroupRank( "NaziTank_Ambush_5 4", "RangedAmbushRank", 18);
    SetBehaviorGroupRank( "NaziTank_Ambush_5 5", "RangedAmbushRank", 19);
    SetBehaviorGroupRank( "NaziTank_Ambush_5 6", "RangedAmbushRank", 20);
    SetBehaviorGroupRank( "NaziTank_Ambush_5 7", "RangedAmbushRank", 21);
    SetBehaviorGroupRank( "NaziTank_Ambush_5 8", "RangedAmbushRank", 22);
    SetBehaviorGroupRank( "NaziTank_Ambush_6 1", "RangedAmbushRank", 23);
    SetBehaviorGroupRank( "NaziTank_Ambush_6 2", "RangedAmbushRank", 24);
    SetBehaviorGroupRank( "NaziTank_Ambush_6 3", "RangedAmbushRank", 25);
    SetBehaviorGroupRank( "NaziTank_Ambush_6 4", "RangedAmbushRank", 26);
    SetBehaviorGroupRank( "NaziTank_Ambush_6 5", "RangedAmbushRank", 27);
    SetBehaviorGroupRank( "NaziTank_Ambush_6 6", "RangedAmbushRank", 28);
    SetBehaviorGroupRank( "NaziTank_Ambush_6 7", "RangedAmbushRank", 29);
    SetBehaviorGroupRank( "NaziTank_Ambush_6 8", "RangedAmbushRank", 30);
    SetBehaviorGroupRank( "NaziTank_Ambush_6 9", "RangedAmbushRank", 31);
    SetBehaviorGroupRank( "NaziTank_Ambush_6 10", "RangedAmbushRank", 32);
    SetBehaviorGroupRank( "NaziTank_Ambush_6 11", "RangedAmbushRank", 33);
    SetBehaviorGroupRank( "NaziTank_Ambush_6 12", "RangedAmbushRank", 34);
    SetBehaviorGroupRank( "NaziTank_Ambush_6 13", "RangedAmbushRank", 35);
    SetBehaviorGroupRank( "NaziTank_Ambush_6 14", "RangedAmbushRank", 36);
    SetBehaviorGroupRank( "NaziTank_Ambush_6 15", "RangedAmbushRank", 37);
    SetBehaviorGroupRank( "NaziTank_Ambush_6 16", "RangedAmbushRank", 38);
    SetBehaviorGroupRank( "NaziTank_Ambush_6 17", "RangedAmbushRank", 39);
    SetBehaviorGroupRank( "NaziTank_Ambush_6 18", "RangedAmbushRank", 40);

    //
    //  Mission specific
    //

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

    m_ConvoyCount  = 0;
    m_EnemiesCount = 0;

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

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

    m_ConvoyDestroyed = 0;
  }

  int  m_ConvoyCount;
  int  m_ConvoyDestroyed;
  int  m_EnemiesCount;

  array m_MissionObjectivesStatuses =
          array(
            str_ObjectiveInProgress
          );

  array m_BonusMissionObjectivesStatuses =
          array(
          );

  //
  //  'virtual' methods
  //

  //
  //  Objectives

  array GetMissionObjectives()
  {
    return m_MissionObjectives;
  }

  array GetMissionObjectivesStatuses()
  {
    return m_MissionObjectivesStatuses;
  }

  void SetMissionObjectiveStatus(int _obj, string _status)
  {
    m_MissionObjectivesStatuses[_obj] = _status;
  }

  //
  //  Bonus objectives

  array GetBonusMissionObjectives()
  {
    return m_BonusMissionObjectives;
  }

  array GetBonusMissionObjectivesStatuses()
  {
    return m_BonusMissionObjectivesStatuses;
  }

  void SetBonusMissionObjectiveStatus(int _obj, string _status)
  {
    m_BonusMissionObjectivesStatuses[_obj] = _status;
  }

  //
  //  Mission statistics

  string GetMissionStatistics()
  {
    int percent = m_ConvoyDestroyed * 100.0 / m_ConvoyCount;
    return str_StatisticsTitle_1 + percent + str_StatisticsTitle_2;
  }

  //
  //  Mission navpoints

  array GetNavPoints()
  {
    array navpoints =
      array(
          GetNavPoint("NavPoint_Ermoshino"),
          GetNavPoint("NavPoint_Vasilievka"),
          GetNavPoint("NavPoint_Laptevka"),
          GetNavPoint("NavPoint_K140"),
          GetNavPoint("NavPoint_A35")
//        vector(12431.0, 3664.0, 1000.0),
//        vector(10351.0, 7905.0, 1000.0),
//        vector(4593.0, 10088.0, 1000.0),
//        vector(3398.0, 14658.0, 1000.0),
//        vector(6206.0, 1106.0, 1000.0)
      );
    return navpoints;
  }

  //
  //  Mission map skin file

  string GetMapSkinFileName()
  {
    return "Missions/Mission_2/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;
    }

    if (Core_IsStringStartsWith(_id, "SovietTruck_Transport"))
    {
      m_ConvoyDestroyed = m_ConvoyDestroyed + 1;
      int percent = 100.0 - m_ConvoyDestroyed * 100.0 / m_ConvoyCount;

      string message = str_WarningMessage_1 + percent + str_WarningMessage_2;

      Core_BroadcastEvent(
        "OnDisplayMessage",
        message,
        m_BadNewsColor
      );
     Core_SendEventTo(
        "Helicopter",
        "ShowEventObject",
        _id,                       //     - id of object to show
        100.0                         //     - distance from camera to object
        );
    }

    // #UGLY bonus objective removed by vlud - it didn't work and was boring
    // check for bonus objective completion
    //if (m_EnemiesCount == 0)
      //BaseMission_CompleteBonusObjective(0);

    // check for 50% of the convoy destroyed
    if (m_ConvoyDestroyed > m_ConvoyCount/2)
      BaseMission_DelayedQuit();
  }

  void OnTransportsEnteredDestinationZone()
  {
    BaseMission_CompleteObjective(0);
  }

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

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