//-------------------------------------------------------------------
//
//  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.
//
//-------------------------------------------------------------------

///////////////////////////////////////
//
// Mission 12 -- Eliminate Partisans
//
///////////////////////////////////////
class CMission12_AIController extends CBaseAIController
{
  array  ActivatePointCenterList = array(
    vector( 6622, 4148, 0 )     // Soviet Katusha Point of Fire
    );    //


  array  ActivatePointRadiusList = array(
    100.0                       //
    );

  array  ActivatePointNameList = array(
    "A"
    );

  void OnObjectEnterArea( int _AreaIndex, string _NavPointName, string _ObjectID)
  {
    if ( _AreaIndex == 0
      && Core_IsStringStartsWith( _ObjectID, "12_SovietTank"))
    {
      Core_BroadcastEvent("OnSovietOffensiveStop");
    }
  }
}
//====================================================================================
class C12_SovietAmmo extends C12_SovietKatusha
{
  vector   DestinationPoint        = vector( 6940, 4170, 0 );
  string   LeaderName              = "SovietAmmo_Rank";

  void Init()
  {
    DestinationPoint = vector( 6940, 4170, 0 );
    GiveOrder_MoveToTarget();
  }

}
//------------------------------------------------------------------------------------
class C12_SovietKatusha extends CBaseAITask_BaseTask, CNavPointUser
     //CNavPointUser, CBaseAITask_BaseTask
{
  //
  // convoy parameters

  vector   DestinationPoint        = vector( 6644, 4124, 0 );//GetNavPoint("NavPoint_04"); // where to go
  vector   FormationVector         = vector( 25, 0, 0);
  vector   PointOfFire             = vector( 2100, 3485, 130 );
  float    CruiserSpeed            = 29; // autocade with guard should keep this speed
  float    RunSpeed                = 29; // autocade wo guards should keep this speed
  float    OvertakeSpeed           = 32; // maximum allowed speed for overtake
  float    AutocadDistanceMin      = 20; // nearest allowed distance in autocade
  float    AutocadDistanceOptimum  = 25; // try to keep this distance in autocade
  float    AutocadDistanceOvertake = 30; // overtake distance
  float    DisperceRadius          = 60;
  string   CurLeader               = "";
  string   LeaderName              = "SovietOffensive_Rank";
  boolean  IsLeader                = false;
  boolean  OnTanksStops             = false;

  //
  // current state

  boolean  Moving   = false; // true when object is moving to destination
  boolean  Ambushed = false; // true when object is under ambush
  boolean  Retreat  = false; // true when object is retreating
  boolean  Run      = false; // true when object is running without stop to destination

  boolean MovingStatus = false;

  void OnStopped()
    {
    if( OnTanksStops )
      {
      string Leader = GetCommander(LeaderName);
      if ( Leader != "" )
        {
        SetOrder_Formation(
          Leader,
          FormationVector,
          10, //#TMP
          15, //#TMP
          OvertakeSpeed/2,
          OvertakeSpeed/2);
        }
      }
    }

  void OnSovietOffensiveStop()
  {
      OnTanksStops = true;
  }
  void ClearState()
  {
    Moving    = false;
    Ambushed  = false;
    Retreat   = false;
    Run       = false;
    CurLeader = "";
    IsLeader  = false;
  }
  // start up initialization
  void Init()
  {
    SetFireStyle_NoFire();
    DestinationPoint = vector( 6644, 4124, 0 );
    // give start order
    GiveOrder_MoveToTarget();
  }
//  void OnEnemyTargeted()
//  {
//      GiveOrder_Disperse();
//  }
  void GiveOrder_Disperse()
  {
    // set state
    ClearState();
    Ambushed = true;
    SetOrder_StopNow();
        // move to random point
        SetOrder_MoveTo(
            GetNowPosition() + vector( rand(DisperceRadius), rand(DisperceRadius), 0.0),
            OvertakeSpeed);
  }
//  void OnNoEnemy()
//  {
//    if ( !OnTanksStops )
//      {
//      GiveOrder_MoveToTarget();
//      }
//  }
  void GiveOrder_MoveToTarget()
  {
    //Core_LogMessage("" + Core_GetIdentificator() + ": order: move to target");
    string Leader = GetCommander(LeaderName); // find leader that we should followed by
    if ( Moving == true
      && Leader == CurLeader)
    {
      // nothing changed
      return;
    }
    // set state
    ClearState();
    Moving = true;
    CurLeader = Leader;
    if ( Leader == "")
    {
      // we are leader. go directly to destination
      SetOrder_MoveTo(
        DestinationPoint,
        CruiserSpeed);
    }
    else
    {
      // leader found. follow him
      SetOrder_Follow(
        Leader, // follow leader
        AutocadDistanceMin,
        AutocadDistanceOptimum,
        AutocadDistanceOvertake,
        CruiserSpeed,
        OvertakeSpeed);
    };
  }
  void OnLeaderLost( string _LeaderID)
  {
    // reorder
    GiveOrder_MoveToTarget();
  }
}
//===========================================================================================
class C12_SovietDefender extends CBaseAITask_BaseTask, CNavPointUser
     //CNavPointUser, CBaseAITask_BaseTask
{
}
class C12_TraitorAA extends CBaseAITask_BaseTask, CNavPointUser
     //CNavPointUser, CBaseAITask_BaseTask
{
}
class C12_Traitor extends CBaseAITask_BaseTask, CNavPointUser
     //CNavPointUser, CBaseAITask_BaseTask
{
}
//============================================================================================
class C12_GermanAA02 extends C12_GermanAA01
{
  vector   DestinationPoint        = vector( 6187, 6011, 0); // where to go
  string   Ranking                 = "AA02_Rank";
}
//--------------------------------------------------------------------------------------------
class C12_GermanAA01 extends CBaseAITask_BaseTask, CNavPointUser
{
  //
  // convoy parameters

  vector   DestinationPoint        = vector( 6049, 5602, 0); // where to go
  vector   FormationVector         = vector( 5, -90, 0);
  float    CruiserSpeed            = 25; // autocade with guard should keep this speed
  float    RunSpeed                = 25; // autocade wo guards should keep this speed
  float    OvertakeSpeed           = 29; // maximum allowed speed for overtake
  float    AutocadDistanceMin      = 20; // nearest allowed distance in autocade
  float    AutocadDistanceOptimum  = 25; // try to keep this distance in autocade
  float    AutocadDistanceOvertake = 30; // overtake distance
  float    DisperceRadius          = 50;
  string   CurLeader               = "";
  string   Ranking                 = "AA01_Rank";
  boolean  IsLeader                = false;
  boolean  OnTanksStops            = false;

  //
  // current state

  boolean  Moving   = false; // true when object is moving to destination
  boolean  Ambushed = false; // true when object is under ambush
  boolean  Retreat  = false; // true when object is retreating
  boolean  Run      = false; // true when object is running without stop to destination

  boolean MovingStatus = false;

  void OnConvoySPlatoonStop()
  {
    if ( !OnTanksStops )
      {
        OnTanksStops = true;
        SetOrder_Formation(
          Leader,
          FormationVector,
          45, //#TMP
          15, //#TMP
          OvertakeSpeed/2,
          OvertakeSpeed/2);
      }
  }

  void ClearState()
  {
    Moving    = false;
    Ambushed  = false;
    Retreat   = false;
    Run       = false;
    CurLeader = "";
    IsLeader  = false;
  }

  //
  // start up initialization

  void Init()
  {
    SetFireStyle_Nearest();
    // DestinationPoint = vector( 6049, , 0);
    // give start order
    GiveOrder_MoveToTarget();
  }

  void OnEnemyTargeted()
  {
    string EnemyID = GetTargetedEnemy();
    SetFireStyle_Enemy( EnemyID);
    GiveOrder_Disperse();
  }

  void GiveOrder_Disperse()
  {
    // set state
    ClearState();
    Ambushed = true;

    SetOrder_StopNow();

        // move to random point
        SetOrder_MoveTo(
            GetNowPosition() + vector( rand(DisperceRadius), rand(DisperceRadius), 0.0),
            OvertakeSpeed);
  }

  void OnNoEnemy()
  {
      GiveOrder_MoveToTarget();
  }

  void GiveOrder_MoveToTarget()
  {
    //Core_LogMessage("" + Core_GetIdentificator() + ": order: move to target");

    string Leader = GetCommander(Ranking); // find leader that we should followed by
    if ( Moving == true
      && Leader == CurLeader)
    {
      // nothing changed
      return;
    }

    // set state
    ClearState();
    Moving = true;
    CurLeader = Leader;

    if ( Leader == "")
    {
      // we are leader. go directly to destination
      SetOrder_MoveTo(
        DestinationPoint,
        CruiserSpeed);
    }
    else
    {
      // leader found. follow him
      SetOrder_Follow(
        Leader, // follow leader
        AutocadDistanceMin,
        AutocadDistanceOptimum,
        AutocadDistanceOvertake,
        CruiserSpeed,
        OvertakeSpeed);
    };
  }

  void OnLeaderLost( string _LeaderID)
  {
    // reorder
    GiveOrder_MoveToTarget();
  }
};


