//-------------------------------------------------------------------
//
//  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 for Civilian
 class C2M5_HoldPositionUnit extends CBaseAITask
 {
   void Init()
    {
     SetOrder_StopNow();
    }

    void OnEnemyTargeted()
    {
    }

    void OnNoEnemy()
    {
    }
 }

  class C2M5_EG_AwayVillageAutoGroup extends CBaseUnitGroup
   {
    void Init()
     {
       SetOrder_Stop();
     }

   event void AwayAuto ()
     {
       SetOrder_Move(["C2M5_NP_AwayAuto","C2M5_NP_AwayAuto_1","C2M5_NP_AwayAuto_2","C2M5_NP_AwayAuto_3","C2M5_NP_AwayAuto_4","C2M5_NP_AwayAuto_5","C2M5_NP_AwayAuto_6","C2M5_NP_AwayAuto_7","C2M5_NP_AwayAuto_8","C2M5_NP_AwayAuto_9","C2M5_NP_AwayAuto_10","C2M5_NP_AwayAuto_11","C2M5_NP_AwayAuto_11"],0,false);
     }

   void OnPathEndReached()
     {
       SetOrder_Stop();
     }
   }

   class C2M5_Kraz extends CBaseAITask
   {
    void OnEnemyTargeted()
     {
     }

    void OnNoEnemy()
     {
     }
   }
   class C2M5_RiverBoatGroup extends CBaseUnitGroup
   {
     event void AwayRiver ()
     {
       SetOrder_Move(["C2M5_NP_RiverAway_1","C2M5_NP_RiverAway_2","C2M5_NP_RiverAway_3","C2M5_NP_RiverAway_4","C2M5_NP_RiverAway_5","C2M5_NP_RiverAway_6","C2M5_NP_RiverAway_7","C2M5_NP_RiverAway_8","C2M5_NP_RiverAway_9"],0,false);
     }
    void OnPathEndReached()
     {
       SetOrder_Stop();
     }
   }
   class C2M5_RiverBoatAtrilleryGroup extends CBaseUnitGroup
   {
     event void AwayRiver ()
     {
       SetEnemyReactionType(ERT_AGGRESSIVE);
     }
   }

   class  C2M5_GroupFire extends CBaseUnitGroup
   {

    void Init()
     {
      SetOrder_Stop();
     }

// Nav Point Explosion
     void Blast( String _DetinationBlast)
     {
        logError("Blast  NP - " + new String(_DetinationBlast));
        Component NP = GetMission().GetObject(_DetinationBlast);
        if ( NP == null )
          logError("Component NP == null");
        else
          NP.ExplodeNavPoint("CNavPointExplosion");
     }
// Nav Point Explosion

     void SetFire (String _Destination)
     {
        logError(" !!! FIRE !!!!");
        if ( _Destination.IsStartsWith("Sing"))
         {
           PostDelayedEvent( "Blast", ["C2M5_NP_Explode_02_" + _Destination], 5000);
           PostDelayedEvent( "Blast", ["C2M5_NP_Explode_04_" + _Destination], 10000);
           PostDelayedEvent( "Blast", ["C2M5_NP_Explode_05_" + _Destination], 15000);
           PostDelayedEvent( "Blast", ["C2M5_NP_Explode_01_" + _Destination], 20000);
           PostDelayedEvent( "Blast", ["C2M5_NP_Explode_03_" + _Destination], 25000);

           Component CCisternB     = GetMission().GetObject("CCisternB");

           if ( CCisternB != null &&  !checkMask( CCisternB, [CLASSIFICATOR_DEAD_OBJECT], []))
                       CCisternB.CommitSuicide();

          GetMission().CompleteObjective(0);

          fireEvent(5.0, [], "SendBroadcastCockpitMessage", [CC2M5Mission_Strings::str_TransportTakeOff, CCockpitColorMap::m_BadNewsColor, "str_TransportTakeOff"]);
         }
        if ( _Destination.IsStartsWith("Sahn"))
         {
           PostDelayedEvent( "Blast", ["C2M5_NP_Explode_01_" + _Destination], 5000);
           PostDelayedEvent( "Blast", ["C2M5_NP_Explode_02_" + _Destination], 10000);
           PostDelayedEvent( "Blast", ["C2M5_NP_Explode_03_" + _Destination], 15000);
           PostDelayedEvent( "Blast", ["C2M5_NP_Explode_04_" + _Destination], 20000);
           PostDelayedEvent( "Blast", ["C2M5_NP_Explode_05_" + _Destination], 25000);

           Component LaboratoryPlants02 = GetMission().GetObject("LaboratoryPlants02");
           Component CisternLarge01     = GetMission().GetObject("CisternLarge01");
           Component CisternSmall01     = GetMission().GetObject("CisternSmall01");
           Component CisternSmall02     = GetMission().GetObject("CisternSmall02");

           if ( LaboratoryPlants02 != null &&  !checkMask( LaboratoryPlants02, [CLASSIFICATOR_DEAD_OBJECT], []))
                       LaboratoryPlants02.CommitSuicide();

           if ( CisternLarge01 != null &&  !checkMask( CisternLarge01, [CLASSIFICATOR_DEAD_OBJECT], []))
                       CisternLarge01.CommitSuicide();

           if ( CisternSmall01 != null &&  !checkMask( CisternSmall01, [CLASSIFICATOR_DEAD_OBJECT], []))
                       CisternSmall01.CommitSuicide();

           if ( CisternSmall02 != null &&  !checkMask( CisternSmall02, [CLASSIFICATOR_DEAD_OBJECT], []))
                       CisternSmall02.CommitSuicide();

           GetMission().CompleteObjective(1);
          fireEvent(5.0, [], "SendBroadcastCockpitMessage", [CC2M5Mission_Strings::str_TransportTakeOff, CCockpitColorMap::m_BadNewsColor, "str_TransportTakeOff"]);
         }
        if ( _Destination.IsStartsWith("Quehn"))
         {
           PostDelayedEvent( "Blast", ["C2M5_NP_Explode_01_" + _Destination], 5000);
           PostDelayedEvent( "Blast", ["C2M5_NP_Explode_02_" + _Destination], 10000);
           PostDelayedEvent( "Blast", ["C2M5_NP_Explode_03_" + _Destination], 15000);
           PostDelayedEvent( "Blast", ["C2M5_NP_Explode_04_" + _Destination], 20000);
           PostDelayedEvent( "Blast", ["C2M5_NP_Explode_05_" + _Destination], 25000);

           Component LaboratoryPlants03 = GetMission().GetObject("LaboratoryPlants03");
           Component CisternLarge02     = GetMission().GetObject("CisternLarge02");
           Component CisternLarge03     = GetMission().GetObject("CisternLarge03");
           Component CisternLarge04     = GetMission().GetObject("CisternLarge04");
           Component Cistern05          = GetMission().GetObject("Cistern05");

           if ( LaboratoryPlants03 != null &&  !checkMask( LaboratoryPlants03, [CLASSIFICATOR_DEAD_OBJECT], []))
                       LaboratoryPlants03.CommitSuicide();

           if ( CisternLarge02 != null &&  !checkMask( CisternLarge02, [CLASSIFICATOR_DEAD_OBJECT], []))
                       CisternLarge02.CommitSuicide();

           if ( CisternLarge03 != null &&  !checkMask( CisternLarge03, [CLASSIFICATOR_DEAD_OBJECT], []))
                       CisternLarge03.CommitSuicide();

           if ( CisternLarge04 != null &&  !checkMask( CisternLarge04, [CLASSIFICATOR_DEAD_OBJECT], []))
                       CisternLarge04.CommitSuicide();

           if ( Cistern05 != null &&  !checkMask( Cistern05, [CLASSIFICATOR_DEAD_OBJECT], []))
                       Cistern05.CommitSuicide();

           GetMission().CompleteObjective(2);
         }

     }

     void SetLoad (String _LoadPoint , String _Transport)
     {
       logError("Group LOAD!!!");
// TMP       SetOrder_Load( _LoadPoint, _Transport);
       SetOrder_Load( "", _Transport);
     }

   String m_CurrentDesant = "";

   event void TransportLanded ( String _TransportID , String _NavID , boolean _Landing)
     {
       GetMission().m_Landed = true;
      if ( _Landing && _NavID.IsStartsWith("C2M5_NP_LandingSing")&& !GetMission().m_LandingSing)
       {
        m_CurrentDesant = "Sing";
        SetOrder_Unload("PlayerTransport","C2M5_NP_Explode_05_Sing", 10,80);
        GetMission().m_LandingSing = true;
        logError("Group UNLOAD Sing!!!");
        PostDelayedEvent("SetLoad", [_NavID,_TransportID],90000);
       }

      if ( _Landing && _NavID.IsStartsWith("C2M5_NP_LandingSahn") && !GetMission().m_LandingSahn)
       {
        m_CurrentDesant = "Sahn";
        SetOrder_Unload("PlayerTransport","C2M5_NP_Explode_05_Sahn", 10,80);
        GetMission().m_LandingSahn = true;
        logError("Group UNLOAD Sahn!!!");
        PostDelayedEvent("SetLoad", [_NavID,_TransportID],90000);
       }

      if ( _Landing && _NavID.IsStartsWith("C2M5_NP_LandingQuehn") && !GetMission().m_LandingQuehn)
       {
        m_CurrentDesant = "Quehn";
        SetOrder_Unload("PlayerTransport","C2M5_NP_Explode_05_Quehn", 10,80);
        GetMission().m_LandingQuehn = true;
        logError("Group UNLOAD Quehn!!!");
        PostDelayedEvent("SetLoad", [_NavID,_TransportID],90000);
       }
     }

   event void OnGroupUnloaded( String _GroupID )
     {
        if (  m_CurrentDesant == "Sing" )
        {
          logError("Group Unloaded - current point Sing");
          RefreshUnitsList();
          InvokeUnitTaskFunction( m_Units[0], "SetOrder_MoveMoveTo", ["C2M5_NP_Destroy01_Sing",5]);
          m_CurrentDesant == "";
        }

        if ( m_CurrentDesant == "Sahn" )
        {
          logError("Group Unloaded - current point Sahn");
          RefreshUnitsList();
          InvokeUnitTaskFunction( m_Units[0], "SetOrder_MoveMoveTo", ["C2M5_NP_Destroy01_Sahn",5]);
          InvokeUnitTaskFunction( m_Units[1], "SetOrder_MoveMoveTo", ["C2M5_NP_Destroy02_Sahn",5]);
          m_CurrentDesant = "";
        }

        if ( m_CurrentDesant == "Quehn" )
        {
          logError("Group Unloaded - current point Quehn");
          RefreshUnitsList();
          InvokeUnitTaskFunction( m_Units[0], "SetOrder_MoveMoveTo", ["C2M5_NP_Destroy01_Quehn",5]);
          InvokeUnitTaskFunction( m_Units[1], "SetOrder_MoveMoveTo", ["C2M5_NP_Destroy02_Quehn",5]);
          m_CurrentDesant = "";
        }
     }
   event void OnGroupLoaded( String _GroupID )
     {
       GetMission().m_Landed = false;
       if ( GetMission().m_LandingSing && !GetMission().m_SingFire)
       {
         sendEvent( 0.0, "PlayerTransport", "Wingman_TakeOff",  [] );
         logError("Transporopt 'takeoff'");
         logError("Group Loaded Sing");
         PostDelayedEvent("SetFire",["Sing"],5000);
         GetMission().m_SingFire = true;
       }

       if ( GetMission().m_LandingSahn && !GetMission().m_SahnFire)
       {
         sendEvent( 0.0, "PlayerTransport", "Wingman_TakeOff",  [] );
         logError("Transporopt 'takeoff'");
         logError("Group Loaded  Sahn");
         PostDelayedEvent("SetFire",["Sahn"],10000);
         GetMission().m_SahnFire = true;
       }

       if ( GetMission().m_LandingQuehn && !GetMission().m_QuehnFire)
       {
         sendEvent( 0.0, "PlayerTransport", "Wingman_TakeOff",  [] );
         logError("Transporopt 'takeoff'");
         logError("Group Loaded  Quehn");
         PostDelayedEvent("SetFire",["Quehn"],10000);
         GetMission().m_QuehnFire = true;
       }
     }
   }

   class SoldierAI extends CBaseAITask
   {
      float _GuardRadius = 5.0;

     void OnEnemyTargeted()
     {
     }

     void OnNoEnemy()
     {
     }

     void SetOrder_MoveMoveTo(String _Dest, float _speed)
     {

       logError("Kva "+ new String(GetIdentificator()) + " to ( " + _Dest + " )");
       SetOrder_Guard(_Dest, _GuardRadius, _speed);
     }
   }

   class WLynxAttack extends CBaseAITask
   {

    void OnEnemyTargeted()
    {
     SetEnemyReactionType(ERT_PASSIVE);
     CBaseAITask::OnEnemyTargeted();
    }
    event void TransportLanded ( String _TransportID , String _NavID , boolean _Landing)
     {

       String NameGroup = new String  (m_Group.m_GroupID);

      if ( _Landing && _NavID.IsStartsWith("C2M5_NP_LandingSing") && NameGroup.IsStartsWith("C2M5_AE_PatrolSing_"))
       {
         SetOrder_Guard("C2M5_NP_LandingSing", 100,50);
         SetEnemyReactionType(ERT_AGGRESSIVE);
       }

      if ( _Landing && _NavID.IsStartsWith("C2M5_NP_LandingSahn") && NameGroup.IsStartsWith("C2M5_AE_PatrolSahn_"))
       {
         SetOrder_Guard("C2M5_NP_LandingSahn", 100,50);
         SetEnemyReactionType(ERT_AGGRESSIVE);
       }

      if ( _Landing && _NavID.IsStartsWith("C2M5_NP_LandingQuehn") && NameGroup.IsStartsWith("C2M5_AE_PatrolQuehn_"))
       {
         SetOrder_Guard("C2M5_NP_LandingQuehn", 100,50);
         SetEnemyReactionType(ERT_AGGRESSIVE);
       }
     }
   }

   class C2M5_PunchAI extends CBaseAITask
   {

  boolean PlayerTargeted = false;
  boolean ChangeRank = false;

    void Init()
     {
       SetOrder_StopNow();
     }


  void OnEnemyTargeted()
  {
    if ( !PlayerTargeted)
    {
      PlayerTargeted = true;
      String NameGroup = new String  (m_Group.m_GroupID);
      InvokeGroupTaskFunction( NameGroup, "PopDelayedOrder", []);

      logError( NameGroup + " see " + new String (GetTargetedEnemy()) );
    }
  }

    event void TransportLanded ( String _TransportID , String _NavID , boolean _Landing)
     {
        if ( _Landing && GetRank()==0 && !PlayerTargeted)
        {
          if ( _NavID== "C2M5_NP_LandingSing" && m_Group.m_GroupID.IsStartsWith("C2M5_Punch01"))
          {
           SetOrder_Guard(_NavID, 100,40);
          }
          if ( _NavID== "C2M5_NP_LandingSahn" && m_Group.m_GroupID.IsStartsWith("C2M5_Punch02"))
          {
           SetOrder_Guard(_NavID, 100,40);
          }
          if ( _NavID== "C2M5_NP_LandingQuehn" && m_Group.m_GroupID.IsStartsWith("C2M5_Punch03"))
          {
           SetOrder_Guard(_NavID, 100,40);
          }
        }
        else
          SetEnemyReactionType(ERT_AGGRESSIVE);
     }

     event void ChangeRank(Array _Target , String _ObjectID)
     {
      ChangeRank = true;
      logError("Getting  _ObjectID="+new String(_ObjectID));
       _Target.append(GetMission().GetObject("C2M5_FG_GroupFire").m_Units);
       if ( _ObjectID.IsStartsWith("C2M5_Punch01") && m_Group.m_GroupID.IsStartsWith("C2M5_Punch01"))
       {
         if ( GetRank()==0)
         {
           SetOrder_Guard("C2M5_NP_LandingSing", 100,40);
           logError("Change Rank! Unit "+ new String(GetIdentificator()) +"- Attack." + "Unit have Rank="+new String(GetRank()));
         }
         else
         {
           setOrder_StopNow();
           logError("Change Rank! Unit "+ new String(GetIdentificator()) +"- StopNow."+"Unit have Rank="+new String(GetRank()));
         }
       }

       if ( _ObjectID.IsStartsWith("C2M5_Punch02") && m_Group.m_GroupID.IsStartsWith("C2M5_Punch02"))
       {
         if ( GetRank()==0)
         {
          SetOrder_Guard("C2M5_NP_LandingSahn", 100,40);
          logError("Change Rank! Unit "+ new String(GetIdentificator()) +"- Attack."+"Unit have Rank="+new String(GetRank()));
         }
         else
          {
           setOrder_StopNow();
           logError("Change Rank! Unit "+ new String(GetIdentificator()) +"- StopNow."+"Unit have Rank="+new String(GetRank()));
          }
       }

       if ( _ObjectID.IsStartsWith("C2M5_Punch03") && m_Group.m_GroupID.IsStartsWith("C2M5_Punch03"))
       {
        if ( GetRank()==0)
        {
         SetOrder_Guard("C2M5_NP_LandingQuehn", 100,40);
         logError("Change Rank! Unit "+ new String(GetIdentificator()) +"- Attack."+"Unit have Rank="+new String(GetRank()));
        }
        else
         {
          setOrder_StopNow();
          logError("Change Rank! Unit "+ new String(GetIdentificator()) +"- StopNow."+"Unit have Rank="+new String(GetRank()));
         }
       }
     }
   }
  class  C2M5TransportTask extends CTransportWingmanTask
  {
   void Wingman_Cargo(String _LeaderID)
   {
    String _LeaderID;
    _LeaderID = InvokeUnitTaskFunction("PlayerTransport", "GetLeaderID",[]);

    if ( GetPossibleLandingTarget() == "C2M5_NP_LandingSing" && GetMission().m_SingFire)
    {
      GetMission().SendPersonalCockpitMessage(_LeaderID, CC2M5Mission_Strings::str_TransportWasLandSing, CCockpitColorMap::m_BadNewsColor, "str_TransportWasLandSing");
    }
    else if ( GetPossibleLandingTarget() == "C2M5_NP_LandingSahn" && GetMission().m_SahnFire)
    {
      GetMission().SendPersonalCockpitMessage(_LeaderID, CC2M5Mission_Strings::str_TransportWasLandSahn, CCockpitColorMap::m_BadNewsColor, "str_TransportWasLandSahn");
    }
    else if ( GetPossibleLandingTarget() == "C2M5_NP_LandingQuehn" && GetMission().m_QuehnFire)
    {
      GetMission().SendPersonalCockpitMessage(_LeaderID, CC2M5Mission_Strings::str_TransportWasLandQuehn, CCockpitColorMap::m_BadNewsColor, "str_TransportWasLandQuehn");
    }
    else
    {
      CTransportWingmanTask::Wingman_Cargo(_LeaderID);
    }
   }
  }

class C2M5_CinPlayer extends CWingmanTask
{
   event void StartCinema()
   {
    logError(" Start Cinema");
      sendEvent(37.0, "C2M5_Cin_PlayerMainUnit", "TakeOff", []);
   }


  event void StopCinema()
   {
    logError(" End Cinema");
      sendEvent(0.0, "C2M5_Cin_PlayerMainUnit", "Landing", ["C2M5Recharge01_Place03"]);
   }
}

class C2M5_CinWingman extends CWingmanTask
{
   event void StartCinema()
   {
    sendEvent(39.0, "C2M5_Cin_PlayerA", "TakeOff", []);
   }

  event void StopCinema()
   {
    logError(" End Cinema");
      sendEvent(0.0, "C2M5_Cin_PlayerA", "Landing", ["C2M5Recharge01_Place02"]);
   }
}

class C2M5_TransportTask extends CWingmanTask
{
   event void StartCinema()
   {
   sendEvent(43.0, "C2M5_Cin_PlayerTransport", "TakeOff", []);
   }

  event void StopCinema()
   {
    logError(" End Cinema");
    sendEvent(0.0, "C2M5_Cin_PlayerTransport", "Landing", ["C2M5Recharge01_Place01"]);
   }
}


class C2M5_Cin_RiverBoatTask extends CBaseAITask
  {
    event void StartCinema()
         {
           PostDelayedEvent("SetOrder_MoveTo_Direct",[GetNavPointPos("C2M5_Cin_RiverBoatNavPoint"), 0.0], 11000);
         }
  }

class C2M5_Cin_WorkerTask extends CBaseAITask
  {
    event void StartCinema()
         {
           PostDelayedEvent("SetOrder_MoveTo",[GetNavPointPos("C2M5_Cin_WarkerNavPoint"), 2.0], 23000);
         }
}
