------------------------------------------------------------
--     2008 Elite Games Team (R) All Rights Reserved      --
--             "STAR WOLVES 2: CIVIL WAR"                 --
------------------------------------------------------------

------------------------------------------------------------
-- Auto-built Quest-Action-file created by "MapBuilder2"  --
------------------------------------------------------------
--      "MapBuilder2" v.2.6 Elite Games Team              --
------------------------------------------------------------


--info:top=120,left=80,allvis=0;
function StartMission()
  ClericSpeaker:Message("#L_Cleric_01", 20);
end;


--info:top=160,left=350,allvis=0;
function PreAnim_NewMothership()
  SetNormalTimeMode();
  MothershipFlight:Stop();
  flight_nm1:Stop();

  if (ClericSpeaker:IsInBase()) or (ClericSpeaker:IsBasePilot()) then
    if (ClericSpeaker:IsBasePilot()) then RemoveBaseDriver(); end;
    Player_Team:RemovePilot(ClericSpeaker);
    ClericSpeaker:Kill();
    TempPilot = CreatePilot("Cleric");
    own = 0;
  else
    TempPilot = CreatePilot("Autopilot");
    ClericShip = ClericSpeaker:GetShip();
    ClericFlight = ClericSpeaker:GetFlight();
    if (ClericFlight:GetPilotCount()==1) then
      Player_Group:RemoveFlight(ClericFlight);
      Cleric_Group:AddFlight(ClericFlight);
      ClericFlight:Escort(mothership);
      own = 1;
    else
      NewClericFlight = Cleric_Group:CreateFlight("NewClericFlight");
      NewClericFlight:AddShip(ClericShip);
      NewClericFlight:Escort(mothership);
      own = 2;
    end;
  end;
  Timer_Seq(1, Anim_NewMothership);
end;


--info:top=200,left=350,allvis=0;
function Anim_NewMothership()
  Change_Mathership();
  Anim_1 = CreateMovie(_ASIMUTHCAM);
  Anim_1:SetLength(8);
  Anim_1:SetInterpolate(_LINEAR);
  Anim_1:SetTarget(ship_nm1_1);
  Anim_1:InitMovie(1, TRUE, 3);
  Anim_1:AddKeys(245.0840, 71.4361, 38.48, 0);
  Anim_1:AddKeys(116.7007, 69.8231, 38.48, 8);
  Anim_1:Play();
  Anim_1:AttachEscapeScript("Cleric_GoOut");
  Anim_1:AttachAction("Cleric_GoOut");
  AlexSpeaker:Message("#L_Alex_01", 4);
end;


--info:top=230,left=380,allvis=0;
function Change_Mathership()
  mothership_Cleric = mothership;
  mothership = SetPlayerMotherShip(ship_nm1_1);
  GetShipName(mothership);

  PortalTrigger_PORTAL_L:SetObject(mothership);
  PortalTrigger_PORTAL_R:SetObject(mothership);
  PortalTrigger_PORTAL_D:SetObject(mothership);
  PortalsActivate_Action();
  CreateTradeStationDocking(TRADE, mothership);
  CreatePatrolStationDocking(patrol_station01, mothership, 80);
  CreateRelationStationDocking(mining_station, mothership, 140, 3);
  CreateNoneStationDocking(space_station, mothership, 120, "0");

  mothership_Cleric:AssignPilot(TempPilot);
  Old_Mothership_Flight = Cleric_Group:CreateFlight("Old_Mothership_Flight");
  Old_Mothership_Flight:AddShip(mothership_Cleric);
end;


--info:top=250,left=350,allvis=0;
function Cleric_GoOut()
  FocusOn({mothership}, Vector3(0.9135, 0.4, -0.4067), 60);
  local pos = mothership_Cleric:GetPosition();
  local orn = mothership_Cleric:GetVectorOrientation();
  local glow_orn = mothership_Cleric:GetVectorOrientation();
  orn.x = orn.x * 25;
  orn.y = orn.y * 25;
  orn.z = orn.z * 25;
  glow_orn.x = -glow_orn.x;
  glow_orn.y = -glow_orn.y;
  glow_orn.z = -glow_orn.z;
  CreatePortalGlow(pos+orn, glow_orn, 5);
  Old_Mothership_Flight:Move(pos+orn);
  mothership_Cleric:PlayFX("Portal Out.shfx");
  Timer_Seq(5, Cleric_Out);
  Timer_Seq(6, finDialog);
end;


--info:top=280,left=390,allvis=0;
function Cleric_Out()
  Old_Mothership_Flight:Delete();
  if (own==2) then
    NewClericFlight:Delete();
  elseif (own==1) then
    ClericFlight:Delete();
  end;
end;


--info:top=300,left=350,allvis=0;
function finDialog()
  SetNormalTimeMode();
--------------------------------------------------------------------------------
  --StartDialog("Dialog2");
  count_or_go_on();
end;

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

function count_or_go_on()
  if (pilot_El) or (pilot_Ace) or (pilot_Astra) or (pilot_Bavard) or (pilot_Joker) then
    if (count_pilots() >= 6) then
      choose_pilot();
    else
      StartDialog("Dialog2");
    end;
  else
    StartDialog("Dialog2");
  end; 
end;

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------


--info:top=340,left=350,allvis=0;
function finMission()
  Grey_Group:RemoveFlight(flight_natali);
  Player_Group:AddFlight(flight_natali);
  if not(HeroSpeaker:IsInBase()) and not(HeroSpeaker:IsBasePilot()) then
    HeroFlight = HeroSpeaker:GetFlight();
    HeroFlight:Dock();
  end;
  if not(SedoySpeaker:IsInBase()) and not(SedoySpeaker:IsBasePilot()) then
    SedoyFlight = SedoySpeaker:GetFlight();
    SedoyFlight:Dock();
  end;
  if not(AlexSpeaker:IsInBase()) and not(AlexSpeaker:IsBasePilot()) then
    AlexFlight = AlexSpeaker:GetFlight();
    AlexFlight:Dock();
  end;
  if not(KalemSpeaker:IsInBase()) and not(KalemSpeaker:IsBasePilot()) then
    KalemFlight = KalemSpeaker:GetFlight();
    KalemFlight:Dock();
  end;
  if not(LastochkaSpeaker:IsInBase()) and not(LastochkaSpeaker:IsBasePilot()) then
    LastochkaFlight = LastochkaSpeaker:GetFlight();
    LastochkaFlight:Dock();
  end;
  flight_natali:Dock();
  Timer_Seq(1, autosaveMission);
end;


--info:top=380,left=350,allvis=0;
function autosaveMission()
  Timer_Seq(1, nextMission);
  AutoSave("#AS_mission_15_4");
end;


--info:top=420,left=350,allvis=0;
function nextMission()
  QuestActionDone();
  QuestStart("mission_19d");
end;


--info:top=120,left=350,allvis=0;
function StartInitMission()
  GreyTeam = CreateTeam("Grey");
  ClericTeam = CreateTeam("Merc");
  Player_Team = CreateTeam(PLAYER_TEAM);

  MothershipPilot = mothership:GetPilot();
  MothershipFlight = MothershipPilot:GetFlight();
  
  Player_Group = MothershipPilot:GetGroup();
  Grey_Group = GreyTeam:CreateGroup("Grey_Group");
  Cleric_Group = ClericTeam:CreateGroup("Cleric_Group");
  
  HeroSpeaker = GetPilot("Hero");
  SedoySpeaker = GetPilot("Sedoy");
  AlexSpeaker = GetPilot("Alex");
  ClericSpeaker = GetPilot("Cleric");
  KalemSpeaker = GetPilot("Kalem");
  LastochkaSpeaker = GetPilot("Lastochka2");
  NataliSpeaker = CreatePilot("Natali");

-- вылет мазершипа
  CreatePortalGlow(Vector3(-200, 50, -100), Vector3(1, 0, 0), 7);
  mothership:SetPosition(Vector3(-200, 50, -100));
  mothership:SetOrientation(CreateQuaternionFromVector3(Vector3(1, 0, 0)));
  mothership:PlayFX("Portal Out.shfx");
  mothership:ApplyImpulse(15, Vector3(1, 0, 0));
  FocusOn({mothership}, Vector3(-0.9135,0.4,-0.4067), 60);
-- серые
  NewFlight("Grey_Group", "nm1", "Mothership_LionG_pl1", "GreyMaster", space_station:GetPosition()+Vector3(-50, 25, 50), Vector3(-1, 0, 0));
  NewFlight("Grey_Group", "grey1", "Lion_gr3", "GreyMaster", space_station:GetPosition()+Vector3(-30, 25, 70), Vector3(-1, 0, 0));
  flight_grey1:Escort(ship_nm1_1);
  NewFlight("Grey_Group", "natali", "Gunslinger_gr3", "Natali", space_station:GetPosition()+Vector3(-40, 35, 60), Vector3(-1, 0, 0));
  flight_natali:Escort(ship_nm1_1);
  NewGreyFlight("Grey_Group", "grey2", space_station:GetPosition()+Vector3(-40, 35, 40), 1, 20, Vector3(-1, 0, 0));
  NewGreyFlight("Grey_Group", "grey3", space_station:GetPosition()+Vector3(-40, 55, 40), 1, 19, Vector3(-1, 0, 0));
  NewGreyFlight("Grey_Group", "grey4", space_station:GetPosition()+Vector3(-40, 55, 60), 1, 19, Vector3(-1, 0, 0));
  NewGreyFlight("Grey_Group", "grey5", space_station:GetPosition()+Vector3(-20, 55, 40), 2, 19, Vector3(-1, 0, 0));
  NewGreyFlight("Grey_Group", "grey6", space_station:GetPosition()+Vector3(-20, 55, 60), 2, 19, Vector3(-1, 0, 0));
  flight_grey2:Escort(ship_nm1_1);
  flight_grey3:Escort(ship_nm1_1);
  flight_grey5:Escort(ship_nm1_1);
  flight_grey4:Escort(ship_grey1_1);
  flight_grey6:Escort(ship_grey1_1);
  flight_nm1:Escort(mothership);
  trig_near = NewTrigger("f_object_inside", TrueCondition, PreAnim_NewMothership, flight_nm1, mothership, 100);

  SetQuestLabel("Transfer_To_NI", "step3")
  AddPost("news015_1");
  AddPost("news015_6");
  AddPost("news015_3");
  AddPost("news015_7");
  AddPost("news015_4");
  AddPost("news015_8");
  AddPost("news015_5");

  Teleporter:Delete();
  Teleporter = CreateTrigger();
  Teleporter:AttachEvent(_EVENT_TIMER); 
  Teleporter:SetTime(3);
  Teleporter:AttachCondition(TrueCondition);
  Teleporter:AttachAction(My_TeleporterAction);
end;


--info:top=160,left=550,allvis=0;
function My_TeleporterAction()
  OutputToScreenLog("#L_sys_01", 7);
  Timer_Seq(7, fin_failed);
end;


--info:top=200,left=550,allvis=0;
function fin_failed()
  EndGame("gamefailed");
end;


--info:top=80,left=80,allvis=0;
function ACTION_INIT()
  StartInitMission();

  Timer_Seq(2, StartMission);
  return TRUE;
end;


--info:top=80,left=570,allvis=0;
function ACTION_LEAVELOCATION()
  Terminate();

  return TRUE;
end;


--info:top=80,left=470,allvis=0;
function ACTION_FINI()

  return TRUE;
end;
