//-------------------------------------------------------------------
//
//  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 CTankBattleMission extends
  CBaseMission, CTankBattleMissionObjectList, CTankBattleMission_Strings, CNavPointUser
{
  int m_NaziForces_A_Num = 0;
  int m_NaziReinforcment_B_Num = 0;
  int m_NaziReinforcment_C_Num = 0;
  int m_NaziReinforcment_D_Num = 0;
  int m_NaziReinforcment_E_Num = 0;
  int m_NaziReinforcment_F_Num = 0;
  int m_NaziReinforcment_G_Num = 0;

  int m_SovietForces_A_Num = 0;
  int m_SovietReinforcment_B_Num = 0;
  int m_SovietReinforcment_C_Num = 0;
  int m_SovietReinforcment_D_Num = 0;
  int m_SovietReinforcment_E_Num = 0;
  int m_SovietReinforcment_F_Num = 0;
  int m_SovietReinforcment_G_Num = 0;

  int GetAutoGeneratedUnitsQty()
  {
    return
      24
    + 8 * 2
    + 19
    + 27
    + 27
    + 2 * 4
    + 4 * 2
    + 16
    + 4 * 6
    + 5
    + 7
    + 3
    + 6
    + 10
    + 4
    + 1
    + 6; // system
  }

  void CTankBattleMission()
  {
    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", "CTankBattleMission_Atmosphere");

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

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

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

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

    BaseMission_CreateObjects();


    // *** create armies:


    // --- A ---

/*
    CreateM9VillageArmy( "A", array(4, 6), array(10, 10));
    m_SovietForces_A_Num = m_SovietForces_A_Num + 10;
    m_NaziForces_A_Num = m_NaziForces_A_Num + 20;
*/
    CreateM9VillageArmy( "A", array(3, 5), array(8, 8));
    m_SovietForces_A_Num = m_SovietForces_A_Num + 8;
    m_NaziForces_A_Num = m_NaziForces_A_Num + 16;

    for ( int a = 1; a <= 8; a = a + 1)
    {
      CreateUnitField(
        "NaziAntiAir_A_" + a,
        "CMountedGermanFaustAntiairSandBags",//"CMountedGermanFlak",
        "",
        2,//3
        GetNavPoint( "NavPoint_A_Nazi_AntiAir_" + a),
        GetNavPointRadius( "NavPoint_A_Nazi_AntiAir_" + a));
    }


    // --- B ---

/*
    CreateM9VillageArmy( "B", array(10), array(10, 10));
    m_SovietReinforcment_B_Num = m_SovietReinforcment_B_Num + 10;
    m_NaziReinforcment_B_Num = m_NaziReinforcment_B_Num + 20;
*/
    CreateM9VillageArmy( "B", array(7), array(6, 6));
    m_SovietReinforcment_B_Num = m_SovietReinforcment_B_Num + 7;
    m_NaziReinforcment_B_Num = m_NaziReinforcment_B_Num + 12;

    // --- C ---

/*
    CreateM9VillageArmy( "C", array(10, 10), array(10, 10, 10));
    m_SovietReinforcment_C_Num = m_SovietReinforcment_C_Num + 20;
    m_NaziReinforcment_C_Num = m_NaziReinforcment_C_Num + 30;
*/
    CreateM9VillageArmy( "C", array(6, 6), array(5, 5, 5));
    m_SovietReinforcment_C_Num = m_SovietReinforcment_C_Num + 12;
    m_NaziReinforcment_C_Num = m_NaziReinforcment_C_Num + 15;

    // --- D ---

/*
    CreateM9VillageArmy( "D", array(6, 7), array(10, 10));
    m_SovietReinforcment_D_Num = m_SovietReinforcment_D_Num + 13;
    m_NaziReinforcment_D_Num = m_NaziReinforcment_D_Num + 20;
*/

    CreateM9VillageArmy( "D", array(5, 6), array(8, 8));
    m_SovietReinforcment_D_Num = m_SovietReinforcment_D_Num + 11;
    m_NaziReinforcment_D_Num = m_NaziReinforcment_D_Num + 16;

    for ( int d = 1; d <= 4; d = d + 1)
    {
      CreateUnitField(
        "NaziAntiAir_D_" + d,
        "CGermanAntiair",
        "",
        2,//3,
        GetNavPoint( "NavPoint_D_Nazi_AntiAir_" + d),
        GetNavPointRadius( "NavPoint_D_Nazi_AntiAir_" + d));
    }

    for ( int d = 1; d <= 2; d = d + 1)
    {
      CreateUnitField(
        "Nazi_Tank_D_Ambush" + d,
        "CGermanTank",
        "",
        2,//3,
        GetNavPoint( "NavPoint_D_Ambush_" + d),
        GetNavPointRadius( "NavPoint_D_Ambush_" + d));

      CreateUnitField(
        "Nazi_AntiAir_D_Ambush" + d,
        "CGermanAntiair",
        "",
        2,//3,
        GetNavPoint( "NavPoint_D_Ambush_" + d),
        GetNavPointRadius( "NavPoint_D_Ambush_" + d));
    }


    // --- E ---

/*
    CreateM9VillageArmy( "E", array(10), array(10));
    m_SovietReinforcment_E_Num = m_SovietReinforcment_E_Num + 10;
    m_NaziReinforcment_E_Num = m_NaziReinforcment_E_Num + 10;
*/

    CreateM9VillageArmy( "E", array(7), array(9));
    m_SovietReinforcment_E_Num = m_SovietReinforcment_E_Num + 7;
    m_NaziReinforcment_E_Num = m_NaziReinforcment_E_Num + 9;

    for ( int e = 1; e <= 6; e = e + 1)
    {
      CreateUnitField(
        "Nazi_Tank_E_Ambush" + e,
        "CGermanTank",
        "",
        2,//3,
        GetNavPoint( "NavPoint_E_Ambush_" + e),
        GetNavPointRadius( "NavPoint_E_Ambush_" + e));

      CreateUnitField(
        "Nazi_AntiAir_E_Ambush" + e,
        "CGermanAntiair",
        "",
        2,//3,
        GetNavPoint( "NavPoint_E_Ambush_" + e),
        GetNavPointRadius( "NavPoint_E_Ambush_" + e));
    }


    // --- F ---

    CreateUnitField(
      "SovietTank_F_",
      "CSovietTank",
      "CM9_Soviet_F",
      5,//6,
      GetNavPoint( "NavPoint_F_Soviet"),
      GetNavPointRadius( "NavPoint_F_Soviet"));

    CreateUnitField(
      "NaziTank_F_",
      "CGermanTank",
      "CM9_Nazi_F",
      7,//10,
      GetNavPoint( "NavPoint_F_Nazi"),
      GetNavPointRadius( "NavPoint_F_Nazi"));

    CreateUnitField(
      "NaziAntiAir_F_",
      "CGermanAntiair",
      "CM9_Nazi_F",
      3,
      GetNavPoint( "NavPoint_F_Nazi"),
      GetNavPointRadius( "NavPoint_F_Nazi"));

    m_SovietReinforcment_F_Num = m_SovietReinforcment_F_Num + 5;//6;
    m_NaziReinforcment_F_Num = m_NaziReinforcment_F_Num + 10;//13;


    // --- G ---

    CreateUnitField(
      "SovietTank_G_",
      "CSovietTank",
      "CM9_Soviet_G",
      6,
      GetNavPoint( "NavPoint_G_Soviet"),
      GetNavPointRadius( "NavPoint_G_Soviet"));

    CreateUnitField(
      "NaziTank_G_",
      "CGermanTank",
      "CM9_Nazi_G",
      10,
      GetNavPoint( "NavPoint_G_Nazi"),
      GetNavPointRadius( "NavPoint_G_Nazi"));

    CreateUnitField(
      "NaziAntiAir_G_",
      "CGermanAntiair",
      "CM9_Nazi_G",
      4,
      GetNavPoint( "NavPoint_G_Nazi"),
      GetNavPointRadius( "NavPoint_G_Nazi"));

    m_SovietReinforcment_G_Num = m_SovietReinforcment_G_Num + 6;
    m_NaziReinforcment_G_Num = m_NaziReinforcment_G_Num + 14;

    for ( int g = 1; g <= 1; g = g + 1)
    {
      CreateUnitField(
        "NaziAntiAir_G_" + g,
        "CGermanAntiair",
        "",
        2,//3,
        GetNavPoint( "NavPoint_G_Nazi_AntiAir_" + g),
        GetNavPointRadius( "NavPoint_G_Nazi_AntiAir_" + g));
    }
  }

  // *** routines ***

  void CreateM9VillageArmy( string _VillageName, array _SovietNum, array _NaziNum)
  {
    // soviet
    for ( int s = 1; s <= _SovietNum.size(); s = s + 1)
    {
      CreateUnitLine(
        "SovietTank_" + _VillageName + "_" + s,
        "CSovietTank",
        "CM9_Soviet_" + _VillageName,
        _SovietNum[s-1],
        GetNavPoint( "NavPoint_" + _VillageName + "_Soviet_" + s + "_start"),
        GetNavPoint( "NavPoint_" + _VillageName + "_Soviet_" + s + "_end"));
    };

    // nazi
    for ( int n = 1; n <= _NaziNum.size(); n = n + 1)
    {
      CreateUnitLine(
        "NaziTank_" + _VillageName + "_" + n,
        "CGermanTank",
        "CM9_Nazi_" + _VillageName,
        _NaziNum[n-1],
        GetNavPoint( "NavPoint_" + _VillageName + "_Nazi_" + n + "_start"),
        GetNavPoint( "NavPoint_" + _VillageName + "_Nazi_" + n + "_end"));
    };
  }

  array m_MissionObjectivesStatuses =
          array(
            str_ObjectiveInProgress
          );

  array m_BonusMissionObjectivesStatuses =
          array(
            str_ObjectiveInProgress,
            str_ObjectiveInProgress,
            str_ObjectiveInProgress,
            str_ObjectiveInProgress
                      );

  //
  //  'virtual' methods
  //

  //
  //  Mission statistics

  string GetMissionStatistics()
  {
    return str_StatisticsTitle;
  }

  //
  //  Mission navpoints

  array GetNavPoints()
  {
    array navpoints =
      array(
        GetNavPoint("NavPoint_Village_A"),
        GetNavPoint("NavPoint_Village_E"),
        GetNavPoint("NavPoint_Village_D"),
        GetNavPoint("NavPoint_Village_C"),
        GetNavPoint("NavPoint_Village_B")
      );
    return navpoints;
  }

  // the 1st navpoint in the array is a default one
  array GetNavPointsDescriptions()
  {
    return m_NavigationPoints;
  }

  //
  //  Mission map skin file

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



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

    if ( Core_IsStringStartsWith(_id, "SovietTank_A"))
      m_SovietForces_A_Num = m_SovietForces_A_Num - 1;
    if ( Core_IsStringStartsWith(_id, "NaziTank_A"))
      m_NaziForces_A_Num = m_NaziForces_A_Num - 1;

    if ( Core_IsStringStartsWith(_id, "SovietTank_B"))
      m_SovietReinforcment_B_Num = m_SovietReinforcment_B_Num - 1;
    if ( Core_IsStringStartsWith(_id, "NaziTank_B"))
    {
      m_NaziReinforcment_B_Num = m_NaziReinforcment_B_Num - 1;
      if ( m_NaziReinforcment_B_Num == 0)
        BaseMission_CompleteBonusObjective(3);
    }

    if ( Core_IsStringStartsWith(_id, "SovietTank_C"))
      m_SovietReinforcment_C_Num = m_SovietReinforcment_C_Num - 1;
    if ( Core_IsStringStartsWith(_id, "NaziTank_C"))
    {
      m_NaziReinforcment_C_Num = m_NaziReinforcment_C_Num - 1;
      if ( m_NaziReinforcment_C_Num == 0)
        BaseMission_CompleteBonusObjective(2);
    }

    if ( Core_IsStringStartsWith(_id, "SovietTank_D"))
      m_SovietReinforcment_D_Num = m_SovietReinforcment_D_Num - 1;
    if ( Core_IsStringStartsWith(_id, "NaziTank_D"))
    {
      m_NaziReinforcment_D_Num = m_NaziReinforcment_D_Num - 1;
      if ( m_NaziReinforcment_D_Num == 0)
        BaseMission_CompleteBonusObjective(1);
    }

    if ( Core_IsStringStartsWith(_id, "SovietTank_E"))
      m_SovietReinforcment_E_Num = m_SovietReinforcment_E_Num - 1;
    if ( Core_IsStringStartsWith(_id, "NaziTank_E"))
    {
      m_NaziReinforcment_E_Num = m_NaziReinforcment_E_Num - 1;
      if ( m_NaziReinforcment_E_Num == 0)
        BaseMission_CompleteBonusObjective(0);
    }

    if ( Core_IsStringStartsWith(_id, "SovietTank_F"))
      m_SovietReinforcment_F_Num = m_SovietReinforcment_F_Num - 1;
    if ( Core_IsStringStartsWith(_id, "NaziTank_F") || Core_IsStringStartsWith(_id, "NaziAntiAir_F_"))
      m_NaziReinforcment_F_Num = m_NaziReinforcment_F_Num - 1;

    if ( Core_IsStringStartsWith(_id, "SovietTank_G"))
      m_SovietReinforcment_G_Num = m_SovietReinforcment_G_Num - 1;
    if ( Core_IsStringStartsWith(_id, "NaziTank_G") || Core_IsStringStartsWith(_id, "NaziAntiAir_G_"))
      m_NaziReinforcment_G_Num = m_NaziReinforcment_G_Num - 1;


    //#TMP:
    /*
    string dbg_msg = "S: A=" + m_SovietForces_A_Num
      + ",B=" + m_SovietReinforcment_B_Num
      + ",C=" + m_SovietReinforcment_C_Num
      + ",D=" + m_SovietReinforcment_D_Num
      + ",E=" + m_SovietReinforcment_E_Num
      + ",F=" + m_SovietReinforcment_F_Num
      + ",G=" + m_SovietReinforcment_G_Num
      + "| N: A=" + m_NaziForces_A_Num
      + ",B=" + m_NaziReinforcment_B_Num
      + ",C=" + m_NaziReinforcment_C_Num
      + ",D=" + m_NaziReinforcment_D_Num
      + ",E=" + m_NaziReinforcment_E_Num
      + ",F=" + m_NaziReinforcment_F_Num
      + ",G=" + m_NaziReinforcment_G_Num;

    Core_BroadcastEvent(
      "OnDisplayMessage",
      dbg_msg,
      color(0.0, 1.0, 1.0));

    Core_LogMessage( dbg_msg);
    */

    // check primary objective completion

    int NaziNum
      = m_NaziForces_A_Num
      + m_NaziReinforcment_B_Num
      + m_NaziReinforcment_C_Num
      + m_NaziReinforcment_D_Num
      + m_NaziReinforcment_E_Num
      + m_NaziReinforcment_F_Num
      + m_NaziReinforcment_G_Num;

    if ( NaziNum == 0)
    {
      BaseMission_CompleteObjective(0);
    };

    // check primary objective failure

    int SovietNum
      = m_SovietForces_A_Num
      + m_SovietReinforcment_B_Num
      + m_SovietReinforcment_C_Num
      + m_SovietReinforcment_D_Num
      + m_SovietReinforcment_E_Num
      + m_SovietReinforcment_F_Num
      + m_SovietReinforcment_G_Num;

    if ( SovietNum == 0)
    {
      BaseMission_DelayedQuit();
    };
  }

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

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