------------------------------------------------------------
--     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=110,left=30,allvis=0;
function StartMission()
  SedoySpeaker:Message("#L_Sedoy_01", 4, "Hero_Answer");
end;


--info:top=150,left=30,allvis=0;
function Hero_Answer()
  HeroSpeaker:Message("#L_Hero_01", 9);
  LineToStation = CreateLine(NP_PORTAL_R, np_vks, RGB(20,80,220), .3, TRUE, .2, 1);
end;


--info:top=200,left=320,allvis=0;
function Meet_Grey_Patrol()
  if (LineToStation) then
    LineToStation:Delete();
    LineToStation = NIL;
  end;
  SetNormalTimeMode();
  new_SetGroupRelations("Grey_Group", PLAYER_GROUP, "W");
  new_SetGroupRelations("GreyGroup_1", PLAYER_GROUP, "W");
  if (ship_gr2_1:IsAlive()) then
    lMessenger = 1;
    local flight = Grey_Group:CreateFlight("flight_gr2");
    flight:AddShip(ship_gr2_1);
    setglobal("flight_gr2", flight);
    trig_Vest = NewTrigger("f_died", TrueCondition, Messenger_Died, flight);
    trig_Doma = NewTrigger("f_inside", TrueCondition, Messenger_InBase, flight, VKS:GetPosition(), 150);
    flight:Move(VKS:GetPosition()+Vector3(40, 0, 20));
    if (flight_gr1:IsAlive()) then
      flight_gr1:Stop();
      GreySpeaker:Message("#L_gray_01", 4, "Sedoy_Warning");
    end;
  else
    Timer_Seq(1, Messenger_Died);
  end;
end;


--info:top=230,left=380,allvis=0;
function Sedoy_Warning()
  SedoySpeaker:Message("#L_Sedoy_02", 5);
end;


--info:top=260,left=380,allvis=0;
function Messenger_Died()
  if (trig_Doma) then
    trig_Doma:Delete();
    trig_Doma = NIL;
  end;
  if (trig_Vest) then
    trig_Vest:Delete();
    trig_Vest = NIL;
  end;
  lMessenger = 0;
end;


--info:top=260,left=160,allvis=0;
function Messenger_InBase()
  if (trig_Vest) then
    trig_Vest:Delete();
    trig_Vest = NIL;
  end;
  trig_Doma:Delete();
  flight_gr2:Delete();
end;


--info:top=280,left=320,allvis=0;
function Patrol_Died()
  if (lMessenger==0) then
    -- дубль на случай сверхдальнего забоя
    new_SetGroupRelations("Grey_Group", PLAYER_GROUP, "W");
    new_SetGroupRelations("GreyGroup_1", PLAYER_GROUP, "W");
    CheSpeaker:Message("#L_Che_01", 5, "Sedoy_OK");
  else
    if (trig_Vest) then
      trig_Vest:Delete();
      trig_Vest = NIL;
    end;
    if (trig_Doma) then
      trig_Doma:Delete();
      trig_Doma = NIL;
      flight_gr2:Delete();
    end;
    Timer_Seq(5, Critical_Attack);
  end;
end;


--info:top=320,left=450,allvis=0;
function Sedoy_OK()
  SedoySpeaker:Message("#L_Sedoy_03", 6);
  trig_Unmissed1 = NewTrigger("group_near_group", TrueCondition, Grey_Unmissed_Base, "GreyGroup_1", PLAYER_GROUP, 200);
  trig_Unmissed2 = NewTrigger("group_near_group", TrueCondition, Grey_Unmissed_Base, "Grey_Group", PLAYER_GROUP, 200);
  trig_NearVKS = NewTrigger("side_inside", TrueCondition, View_Grey_Base, PLAYER_TEAM, VKS:GetPosition(), 300);
  trig_NearPir = NewTrigger("side_inside", TrueCondition, Near_Pirates_Station, PLAYER_TEAM, PIR:GetPosition(), 300);
  NewPirateFlight("Pirate_Group", "pir1", PIR:GetPosition()+Vector3(0, -30, -40), 1, 8, Vector3(0.5, 0, -0.5));
  NewPirateFlight("Pirate_Group", "pir2", PIR:GetPosition()+Vector3(20, -30, -40), 1, 9, Vector3(0.5, 0, -0.5));
  NewPirateFlight("Pirate_Group", "pir3", PIR:GetPosition()+Vector3(-40, -30, 20), 1, 9, Vector3(0.5, 0, -0.5));
end;


--info:top=370,left=140,allvis=0;
function View_Grey_Base()
  lMission = 3;
  SetQuestState("quest_13c_01", QUEST_DONE);
  trig_Unmissed1:Delete();
  trig_Unmissed2:Delete();
  trig_NearVKS:Delete();
  trig_NearPir:Delete();
  if (LineToStation) then
    LineToStation:Delete();
    LineToStation = NIL;
  end;
  SetNormalTimeMode();
  mRadar = Player_Group:CreateRadar(VKS:GetPosition(), 250,0.1);
  Anim_1 = CreateMovie(_TARGETCAM);
  Anim_1:SetLength(5);
  Anim_1:SetInterpolate(_LINEAR);
  Anim_1:SetTarget(VKS);
  Anim_1:InitMovie(1, TRUE, 1);
  Anim_1:AddPosKey(VKS:GetPosition()+Vector3(250, 50, 150), 0);
  Anim_1:AddPosKey(VKS:GetPosition()+Vector3(125, 25, 75), 5);
  Anim_1:Play();
  Anim_1:AttachEscapeScript("Near_VKS_Dialog");
  Anim_1:AttachAction("Near_VKS_Dialog");
end;


--info:top=410,left=140,allvis=0;
function Near_VKS_Dialog()
  Player_Group:DeleteRadar(mRadar);
  if (lQLabel=="merc") then
    if (flight_rec1:IsAlive()) then
      StartDialog("Dialog6");
    else
      StartDialog("Dialog5");
    end;
  else
    StartDialog("Dialog5");
  end;
end;


--info:top=450,left=140,allvis=0;
function Grey_Attack()
  count = 0;
  lLastochkaDialog = 0;
  lWalrusAttack = 0;
  if (flight_gr3:IsAlive()) then
    flight_gr3:Escort(mothership);
    NewTrigger("f_object_inside", TrueCondition, Near_MotherShip, flight_gr3, mothership, 100);
    count = count + 1;
    NewTrigger("f_died", DiedCondition, NullAction, flight_gr3);
  end;
  if (flight_gr4:IsAlive()) then
    flight_gr4:Escort(mothership);
    NewTrigger("f_object_inside", TrueCondition, Near_MotherShip, flight_gr4, mothership, 100);
    count = count + 1;
    NewTrigger("f_died", DiedCondition, NullAction, flight_gr4);
  end;
  if (flight_grey03:IsAlive()) then
    flight_grey03:Escort(mothership);
    NewTrigger("f_object_inside", TrueCondition, Near_MotherShip, flight_grey03, mothership, 100);
    count = count + 1;
    NewTrigger("f_died", DiedCondition, NullAction, flight_grey03);
  end;
  if (flight_grey05:IsAlive()) then
    flight_grey05:Escort(mothership);
    NewTrigger("f_object_inside", TrueCondition, Near_MotherShip, flight_grey05, mothership, 100);
    count = count + 1;
    NewTrigger("f_died", DiedCondition, NullAction, flight_grey05);
  end;
  if (flight_grey07:IsAlive()) then
    flight_grey07:Escort(mothership);
    NewTrigger("f_object_inside", TrueCondition, Near_MotherShip, flight_grey07, mothership, 100);
    count = count + 1;
    NewTrigger("f_died", DiedCondition, NullAction, flight_grey07);
  end;
  if (count==0) then
    lLastochkaDialog = 1;
    Timer_Seq(1, Walrus_Attack);
  else
    Timer_Seq(15, Walrus_Attack);
  end;
end;


--info:top=880,left=20,allvis=0;
function Near_MotherShip()
  local myTriggerContext = GetTriggerContext();
  local flight = myTriggerContext:GetFlight();
  flight:Stop();
end;


--info:top=830,left=920,allvis=0;
function DiedCondition()
  count = count - 1;
  if (count==0) then
    return TRUE;
  end;
  return FALSE;
end;


--info:top=1100,left=70,allvis=0;
function NullAction()
  lLastochkaDialog = 1;
  if (lWalrusAttack) then
    if (lWalrusAttack==1) then Timer_Seq(4, Lastochka_Dialog); end;
  elseif (lPirateAttack) then
    if (lPirateAttack==1) then Timer_Seq(4, Lastochka_Dialog_2); end;
  end;
end;


--info:top=490,left=140,allvis=0;
function Walrus_Attack()
  countW = 0;
  if (flight_pirG1:IsAlive()) then
    flight_pirG1:Escort(mothership);
    NewTrigger("f_object_inside", TrueCondition, Attack_MotherShip, flight_pirG1, mothership, 100);
    countW = countW + 1;
    NewTrigger("f_died", DiedWCondition, Need_Third_01, flight_pirG1);
  end;
  if (flight_pirG2:IsAlive()) then
    flight_pirG2:Escort(mothership);
    NewTrigger("f_object_inside", TrueCondition, Attack_MotherShip, flight_pirG2, mothership, 100);
    countW = countW + 1;
    NewTrigger("f_died", DiedWCondition, Need_Third_01, flight_pirG2);
  end;
  if (countW==0) then
    Timer_Seq(1, Need_Third_01);
  else
    trig_WAttack = NewTrigger("attack", TrueCondition, Che_Nha, Pirate_Group, mothership);
  end;
end;


--info:top=520,left=220,allvis=0;
function DiedWCondition()
  countW = countW - 1;
  if (countW==0) then
    return TRUE;
  end;
  return FALSE;
end;


--info:top=540,left=140,allvis=0;
function Attack_MotherShip()
  local myTriggerContext = GetTriggerContext();
  local flight = myTriggerContext:GetFlight();
  flight:Stop();
end;


--info:top=580,left=140,allvis=0;
function Che_Nha()
  if (trig_WAttack) then
    trig_WAttack:Delete();
    trig_WAttack = NIL;
  end;
  CheSpeaker:Message("#L_Che_02", 6);
end;


--info:top=640,left=280,allvis=0;
function Need_Third_01()
  SedoySpeaker:Message("#L_Sedoy_06", 5, "Need_Third_02");
end;


--info:top=1100,left=450,allvis=0;
function Need_Third_02()
  if (GetQuestState("quest_13c_01")==QUEST_PROCESS) then SetQuestState("quest_13c_01", QUEST_DONE); end;
  LastochkaSpeaker:Message("#L_Lastochka_01", 5, "Need_Third_03");
end;


--info:top=1140,left=450,allvis=0;
function Need_Third_03()
  KalemSpeaker:Message("#L_Kalem_01", 3, "Need_Third_04");
end;


--info:top=1180,left=450,allvis=0;
function Need_Third_04()
  HeroSpeaker:Message("#L_Hero_02", 3);
  if (lWalrusAttack) then
    lWalrusAttack = 1;
    if (lLastochkaDialog==1) then Timer_Seq(4, Lastochka_Dialog); end;
  elseif (lPirateAttack) then
    lPirateAttack = 1;
    if (lLastochkaDialog==1) then Timer_Seq(4, Lastochka_Dialog_2); end;
  end;
end;


--info:top=590,left=280,allvis=0;
function Lastochka_Dialog()
  lWalrusAttack = NIL;
  lLastochkaDialog = 0;
  SetNormalTimeMode();
  StartDialog("Dialog7");
end;


--info:top=370,left=450,allvis=0;
function Grey_Unmissed_Base()
  trig_Unmissed1:Delete();
  trig_Unmissed2:Delete();
  trig_NearVKS:Delete();
  trig_NearPir:Delete();
  SedoySpeaker:Message("#L_Sedoy_04", 2);
  trig_KillPatrol1 = NewTrigger("group_all_outobject", TrueCondition, Critical_Attack, "GreyGroup_1", mothership, 250);
  trig_KillPatrol2 = NewTrigger("group_all_outobject", TrueCondition, Critical_Attack, "Grey_Group", mothership, 250);
  trig_KillTimer = NewTrigger("timer", TrueCondition, Critical_Attack, 30);
end;


--info:top=380,left=720,allvis=0;
function Near_Pirates_Station()
  trig_Unmissed1:Delete();
  trig_Unmissed2:Delete();
  trig_NearVKS:Delete();
  trig_NearPir:Delete();
  lMission = 2;
  if (flight_pirG1:IsAlive()) then
    Pirate_Group:RemoveFlight(flight_pirG1);
    Kalem_Group:AddFlight(flight_pirG1);
  end;
  if (flight_pirG2:IsAlive()) then
    Pirate_Group:RemoveFlight(flight_pirG2);
    Kalem_Group:AddFlight(flight_pirG2);
  end;
  NewTrigger("side_outside", TrueCondition, Critical_Attack, PLAYER_TEAM, PIR:GetPosition(), 350);
  if (flight_pir1:IsAlive()) or (flight_pir2:IsAlive()) or (flight_pir3:IsAlive()) then
    Pirate1Speaker:Message("#L_pirate_01", 4, "Pirates_RunAway_1");
  end;
end;


--info:top=420,left=720,allvis=0;
function Pirates_RunAway_1()
  Pirate2Speaker:Message("#L_pirate_02", 2, "Pirates_RunAway_2");
  if (flight_pir1:IsAlive()) then
    flight_pir1:Move(Vector3(-3000, 0, -200));
  end;
  if (flight_pir2:IsAlive()) then
    flight_pir2:Move(Vector3(-3000, 0, 2000));
  end;
  if (flight_pir3:IsAlive()) then
    flight_pir3:Move(Vector3(-300, 0, 3000));
  end;
end;


--info:top=460,left=720,allvis=0;
function Pirates_RunAway_2()
  Pirate1Speaker:Message("#L_pirate_03", 4, "Pirates_RunAway_3");
end;


--info:top=500,left=720,allvis=0;
function Pirates_RunAway_3()
  SedoySpeaker:Message("#L_Sedoy_07", 4);
end;


--info:top=640,left=450,allvis=0;
function Critical_Attack()
  if (trig_KillPatrol1) then
    trig_KillPatrol1:Delete();
    trig_KillPatrol1 = NIL;
  end;
  if (trig_KillPatrol2) then
    trig_KillPatrol2:Delete();
    trig_KillPatrol2 = NIL;
  end;
  if (trig_KillTimer) then
    trig_KillTimer:Delete();
    trig_KillTimer = NIL;
  end;
  NewGreyFlight("Grey_Group", "gr5", mothership:GetPosition()+Vector3(-10, 0, -90), 2, 11, Vector3(0, 0, 1));
  count = flight_gr5:GetPilotCount();
  for i=0,count,1 do
    pilot_tmp = flight_gr5:GetPilotByNumber(i);
    ship_tmp = pilot_tmp:GetShip();
    ship_tmp:PlayFX("Cloak Out.shfx");
  end;
  flight_gr5:RocketAttack(mothership);
  Timer_Seq(10, Critical_Damages);
end;


--info:top=680,left=450,allvis=0;
function Critical_Damages()
  local autoPilot = CreatePilot("BasePilot");
  flight_gr3:Stop();
  lCrashed = 1;
  mothership:SetMaxSpeed(0);
  mothership:SetSteeringPower(0);
  autoPilot:Message("#L_base_01", 10, "Sedoy_Damn");
end;


--info:top=740,left=450,allvis=0;
function Sedoy_Damn()
  if (lMission==1) then
    SedoySpeaker:Message("#L_Sedoy_05", 4);
    if (flight_gr5:IsAlive()) then
      NewTrigger("f_died", TrueCondition, Dialog_1, flight_gr5);
    else
      Timer_Seq(5, Dialog_1);
    end;
  elseif (lMission==2) then
    SedoySpeaker:Message("#L_Sedoy_05", 4);
    if (flight_gr5:IsAlive()) then
      NewTrigger("f_died", TrueCondition, Dialog_2, flight_gr5);
    else
      Timer_Seq(5, Dialog_2);
    end;
  elseif (lMission==3) then
    SedoySpeaker:Message("#L_Sedoy_05", 4);
    if (flight_gr5:IsAlive()) then
      NewTrigger("f_died", TrueCondition, Dialog_3, flight_gr5);
    else
      Timer_Seq(5, Dialog_3);
    end;
  end;
end;


--info:top=790,left=220,allvis=0;
function Dialog_1()
  if (flight_pirG1) and (flight_pirG1:IsAlive()) then flight_pirG1:Delete(); end;
  if (flight_pirG2) and (flight_pirG2:IsAlive()) then flight_pirG2:Delete(); end;
  if (flight_pirG3) and (flight_pirG3:IsAlive()) then flight_pirG3:Delete(); end;
  NewFlight("Kalem_Group", "pirG1", "Walrus_pir1", "PirateTough", PIR:GetPosition()+Vector3(0, 0, 30), Vector3(1, 0, 0));
  NewFlight("Kalem_Group", "pirG2", "Walrus_pir1", "PirateTough", PIR:GetPosition()+Vector3(0, 0, 50), Vector3(1, 0, 0));
  NewFlight("Kalem_Group", "pirG3", "Walrus_pir1", "PirateTough", PIR:GetPosition()+Vector3(-20, 0, 40), Vector3(1, 0, 0));
  if (flight_pir1) and (flight_pir1:IsAlive()) then flight_pir1:Delete(); end;
  if (flight_pir2) and (flight_pir2:IsAlive()) then flight_pir2:Delete(); end;
  if (flight_pir3) and (flight_pir3:IsAlive()) then flight_pir3:Delete(); end;
  NewPirateFlight("Pirate_Group", "pir1", PIR:GetPosition()+Vector3(0, -30, -40), 1, 13, Vector3(0.5, 0, -0.5));
  NewPirateFlight("Pirate_Group", "pir2", PIR:GetPosition()+Vector3(20, -30, -40), 1, 14, Vector3(0.5, 0, -0.5));
  NewPirateFlight("Pirate_Group", "pir3", PIR:GetPosition()+Vector3(-40, -30, 20), 1, 14, Vector3(0.5, 0, -0.5));
  if (lQLabel=="merc") then
    if (flight_rec1:IsAlive()) then
      StartDialog("Dialog9");
    else
      StartDialog("Dialog8");
    end;
  else
    StartDialog("Dialog8");
  end;
end;


--info:top=830,left=220,allvis=0;
function Grey_Long_Attack()
  count = 0;
  if (flight_gr3:IsAlive()) then
    flight_gr3:Escort(mothership);
    NewTrigger("f_object_inside", TrueCondition, Near_MotherShip, flight_gr3, mothership, 100);
    NewTrigger("f_died", DiedCondition, ToGrey_Station, flight_gr3);
    count = count + 1;
  end;
  if (flight_gr4:IsAlive()) then
    flight_gr4:Escort(mothership);
    NewTrigger("f_object_inside", TrueCondition, Near_MotherShip, flight_gr4, mothership, 100);
    NewTrigger("f_died", DiedCondition, ToGrey_Station, flight_gr4);
    count = count + 1;
  end;
  if (count==0) then
    Timer_Seq(1, ToGrey_Station);
  end;
end;


--info:top=870,left=220,allvis=0;
function ToGrey_Station()
  HeroSpeaker:Message("#L_Hero_03", 5);
  trig_Break = NewTrigger("side_inside", TrueCondition, Break_Through, PLAYER_TEAM, VKS:GetPosition(), 200);
  NewTrigger("side_inside", TrueCondition, Near_Pirate_Station, PLAYER_TEAM, PIR:GetPosition(), 300);
end;


--info:top=910,left=220,allvis=0;
function Break_Through()
  trig_Break:Delete();
  trig_Break = NIL;
  if (LineToStation) then
    LineToStation:Delete();
    LineToStation = NIL;
  end;
  SetNormalTimeMode();
  SetQuestState("quest_13c_01", QUEST_DONE);
  StartDialog("Dialog10");
end;


--info:top=950,left=220,allvis=0;
function ToPirate_Station()
  LineToStation = CreateLine(np_vks, np_pir, RGB(20,80,220), .3, TRUE, .2, 1);
  AddQuest("quest_13c_02", "#L_Quest_13c_02a", "#L_Quest_13c_02b", QUEST_PROCESS);
  NewTrigger("side_inside", TrueCondition, Near_Pirate_Station, PLAYER_TEAM, PIR:GetPosition(), 300);
end;


--info:top=980,left=280,allvis=0;
function Near_Pirate_Station()
  if (trig_Break) then
    trig_Break:Delete();
    trig_Break = NIL;
  end;
  if (LineToStation) then
    LineToStation:Delete();
    LineToStation = NIL;
  end;
  if (GetQuestState("quest_13c_01")==QUEST_PROCESS) then RemoveQuest("quest_13c_01"); end;
  if (GetQuestState("quest_13c_02")==QUEST_PROCESS) then SetQuestState("quest_13c_02", QUEST_DONE); end;
  mRadar = Player_Group:CreateRadar(VKS:GetPosition(), 250,0.1);
  Anim_2 = CreateMovie(_TARGETCAM);
  Anim_2:SetLength(11);
  Anim_2:SetInterpolate(_LINEAR);
  Anim_2:SetTarget(PIR);
  Anim_2:InitMovie(1, TRUE, 3);
  Anim_2:AddPosKey(PIR:GetPosition()+Vector3(150, 50, 250), 0);
  Anim_2:AddPosKey(PIR:GetPosition()+Vector3(125, 25, 75), 10);
  Anim_2:AddScriptKey("Che_What", 1);
  Anim_2:AddScriptKey("Hero_Need_Attack", 5);
  Anim_2:Play();
  Anim_2:AttachEscapeScript("Attack_Pirates");
  Anim_2:AttachAction("Attack_Pirates");
end;


--info:top=1010,left=310,allvis=0;
function Che_What()
  CheSpeaker:Message("#L_Che_03", 4);
end;


--info:top=1040,left=310,allvis=0;
function Hero_Need_Attack()
  HeroSpeaker:Message("#L_Hero_04", 6);
end;


--info:top=1060,left=280,allvis=0;
function Attack_Pirates()
  Player_Group:DeleteRadar(mRadar);
  lPirateAttack = 0;
  count = 0;
  if (flight_pir1:IsAlive()) then
    count = count + 1;
    NewTrigger("f_died", DiedCondition, NullAction, flight_pir1);
  end;
  if (flight_pir2:IsAlive()) then
    count = count + 1;
    NewTrigger("f_died", DiedCondition, NullAction, flight_pir2);
  end;
  if (flight_pir3:IsAlive()) then
    count = count + 1;
    NewTrigger("f_died", DiedCondition, NullAction, flight_pir3);
  end;
  if (count>0) then
    AddQuest("quest_13c_03", "#L_Quest_13c_03a", "#L_Quest_13c_03b", QUEST_PROCESS);
  else
    lPirateAttack = 1;
  end;
  NewTrigger("side_inside", TrueCondition, Need_Third_02, PLAYER_TEAM, PIR:GetPosition(), 150);
end;


--info:top=1210,left=340,allvis=0;
function Lastochka_Dialog_2()
  lPirateAttack = NIL;
  if (GetQuestState("quest_13c_03")==QUEST_PROCESS) then SetQuestState("quest_13c_03", QUEST_DONE); end;
  SetNormalTimeMode();
  StartDialog("Dialog11");
end;


--info:top=790,left=450,allvis=0;
function Dialog_2()
  NewTrigger("side_inside", TrueCondition, Need_Third_02, PLAYER_TEAM, VKS:GetPosition(), 150);
  lPirateAttack = 0;
  count = 0;
  if (flight_gr3:IsAlive()) then
    count = count + 1;
    NewTrigger("f_died", DiedCondition, NullAction, flight_gr3);
  end;
  if (flight_gr4:IsAlive()) then
    count = count + 1;
    NewTrigger("f_died", DiedCondition, NullAction, flight_gr4);
  end;
  if (flight_grey03:IsAlive()) then
    count = count + 1;
    NewTrigger("f_died", DiedCondition, NullAction, flight_grey03);
  end;
  if (flight_grey05:IsAlive()) then
    count = count + 1;
    NewTrigger("f_died", DiedCondition, NullAction, flight_grey05);
  end;
  if (flight_grey07:IsAlive()) then
    count = count + 1;
    NewTrigger("f_died", DiedCondition, NullAction, flight_grey07);
  end;
  if (count==0) then
    lPirateAttack = 1;
  end;
  SetNormalTimeMode();
  StartDialog("Dialog12");
end;


--info:top=790,left=750,allvis=0;
function Dialog_3()
  SetNormalTimeMode();
  StartDialog("Dialog13");
end;


--info:top=1250,left=510,allvis=0;
function Alexandra_ComeOn()
  if (lMission==1) then
    CreatePortalGlow(PIR:GetPosition()+Vector3(50, 10, -50), Vector3(-1, 0, 0), 3);
    NewFlight("Kfni_Group", "newMS", "Mothership_KFNI_pl1", "Alexandra", PIR:GetPosition()+Vector3(50, 10, -50), Vector3(-1, 0, 0));
  else
    CreatePortalGlow(VKS:GetPosition()+Vector3(80, 10, 20), Vector3(-1, 0, 0), 3);
    NewFlight("Kfni_Group", "newMS", "Mothership_KFNI_pl1", "Alexandra", VKS:GetPosition()+Vector3(80, 10, 20), Vector3(-1, 0, 0));
  end;
  Timer_Seq(4, Alexandra_Dialog);
end;


--info:top=1290,left=510,allvis=0;
function Alexandra_Dialog()
  SetNormalTimeMode();
  StartDialog("Dialog14");
end;


--info:top=1330,left=510,allvis=0;
function Mothership_New()
--------------------------------------------------------------------------------
  --Timer_Seq(1, Mothership_New_1);
  Timer_Seq(1, 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() >= 4) then
      choose_pilot();
    else
      Mothership_New_1();
    end;
  else
    Mothership_New_1();
  end; 
end;

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



--info:top=1370,left=510,allvis=0;
function Mothership_New_1()
  Change_Mothership();
  lCrashed = 0;
  Anim_3 = CreateMovie(_TARGETCAM);
  Anim_3:SetLength(18);
  Anim_3:SetInterpolate(_LINEAR);
  Anim_3:SetTarget(ship_newMS_1);
  Anim_3:InitMovie(1, TRUE, 5);
  Anim_3:AddPosKey(ship_newMS_1:GetPosition()+Vector3(-60, 30, 20), 0);
  Anim_3:AddPosKey(ship_newMS_1:GetPosition()+Vector3(50, 30, -20), 18);
  Anim_3:AddScriptKey("In_Change_1", 1);
  Anim_3:AddScriptKey("In_Change_2", 6);
  Anim_3:AddScriptKey("In_Change_3", 9);
  Anim_3:AddScriptKey("In_Change_4", 13);
  Anim_3:Play();
  Anim_3:AttachEscapeScript("After_Change");
  Anim_3:AttachAction("After_Change");
end;


--info:top=1430,left=550,allvis=0;
function In_Change_1()
  HeroSpeaker:Message("#L_Hero_05", 5);
end;


--info:top=1460,left=550,allvis=0;
function In_Change_2()
  AlexandraSpeaker:Message("#L_Alexandra_01", 3);
end;


--info:top=1490,left=550,allvis=0;
function In_Change_3()
  HeroSpeaker:Message("#L_Hero_06", 4);
end;


--info:top=1520,left=550,allvis=0;
function In_Change_4()
  AlexandraSpeaker:Message("#L_Alexandra_02", 5);
end;


--info:top=1400,left=550,allvis=0;
function Change_Mothership()
  mothership_old = mothership;
  mothership = SetPlayerMotherShip(ship_newMS_1);

  PortalTrigger_PORTAL_R:SetObject(mothership);
  PortalsActivate_Action();
  CreateTradeStationDocking(TRADE,mothership);
  GetShipName(mothership);
  AddPilotToPlayer(LastochkaSpeaker);
  AddPilotToPlayer(AlexandraSpeaker);
  AddPilotToPlayer(KalemSpeaker);
  mothership:AddShipToInventory("EvilEye_mk2_kfni1", 1);
  mothership:AddShipToInventory("Cheetah_kfni1", 1);
  mothership:AddShipToInventory("Jaguar_kfni3", 1);
  mothership:AddModuleToInventory("KGK1", 2);
  trig_LDied:Delete();
end;


--info:top=1540,left=510,allvis=0;
function After_Change()
  AddQuest("quest_13c_04", "#L_Quest_13c_04a", "#L_Quest_13c_04b", QUEST_PROCESS);
  if (lMission==3) then
    NewPirateFlight("Pirate_Group", "pirA1", PIR:GetPosition()+Vector3(20, 0, -100), 1, 18, Vector3(0, 0, -1));
    NewPirateFlight("Pirate_Group", "pirA2", PIR:GetPosition()+Vector3(30, 0, -80), 1, 16, Vector3(0, 0, -1));
    NewPirateFlight("Pirate_Group", "pirA3", PIR:GetPosition()+Vector3(10, 0, -80), 1, 15, Vector3(0, 0, -1));
    flight_pirA2:Escort(ship_pirA1_1);
    flight_pirA3:Escort(ship_pirA1_1);
    flight_pirA1:Escort(mothership);
    trig_PDamn = NewTrigger("f_object_inside", TrueCondition, Pirates_Damn, flight_pirA1, mothership, 200);
  end;
  if (lMission==1) then
    trig_GoToPortal = NewTrigger("outside", TrueCondition, GoTo_Portal, mothership, PIR:GetPosition()+Vector3(50, 10, -50), 300);
  else
    trig_GoToPortal = NewTrigger("outside", TrueCondition, GoTo_Portal, mothership, VKS:GetPosition()+Vector3(80, 10, 20), 300);
  end;
  NewTrigger("inside", TrueCondition, finMission, mothership, PORTAL_R:GetPosition(), 50);
end;


--info:top=1590,left=650,allvis=0;
function GoTo_Portal()
  lWait = 0;
  if (trig_PDamn) then
    trig_PDamn:Delete();
    trig_PDamn = NIL;
  end;
  if (trig_PAttack) then
    trig_PAttack:Delete();
    trig_PAttack = NIL;
  end;
  if (flight_pirA1) then flight_pirA1:Delete(); end;
  if (flight_pirA2) then flight_pirA2:Delete(); end;
  if (flight_pirA3) then flight_pirA3:Delete(); end;
  NewTrigger("inside", TrueCondition, New_Gray_Attack, mothership, PORTAL_R:GetPosition(), 300);
  Timer_Seq(1, Dialog_OnPath);
end;


--info:top=1690,left=620,allvis=0;
function Wait_Pirates()
  lWait = 1;
  trig_GoToPortal:Delete();
  trig_GoToPortal = NIL;
end;


--info:top=1580,left=360,allvis=0;
function Pirates_Damn()
  trig_PDamn:Delete();
  trig_PDamn = NIL;
  SetNormalTimeMode();
  if (flight_pirA1:IsAlive()) or (flight_pirA2:IsAlive()) or (flight_pirA3:IsAlive()) then
    Pirate1Speaker:Message("#L_pirate_04", 10);
    trig_PAttack = NewTrigger("timer", TrueCondition, New_Pirates_Attack, 11);
  end;
end;


--info:top=1660,left=510,allvis=0;
function New_Pirates_Attack()
  if (trig_PAttack) then
    trig_PAttack:Delete();
    trig_PAttack = NIL;
    if (flight_pirA1:IsAlive()) or (flight_pirA2:IsAlive()) or (flight_pirA3:IsAlive()) then
      Timer_Seq(1, Wait_Pirates);
      LastochkaSpeaker:Message("#L_Lastochka_02", 8, "Prec_ComeOn");
    end;
  else
    LastochkaSpeaker:Message("#L_Lastochka_02", 8, "Prec_ComeOn");
  end;
end;


--info:top=1700,left=510,allvis=0;
function Prec_ComeOn()
  if (lWait==1) then
    count = 0;
    if (flight_pirA1:IsAlive()) then
      count = count + 1;
      NewTrigger("f_died", DiedCondition, Prec_GoOut, flight_pirA1);
    end;
    if (flight_pirA2:IsAlive()) then
      count = count + 1;
      NewTrigger("f_died", DiedCondition, Prec_GoOut, flight_pirA2);
    end;
    if (flight_pirA3:IsAlive()) then
      count = count + 1;
      NewTrigger("f_died", DiedCondition, Prec_GoOut, flight_pirA3);
    end;
    if (count==0) then
      Timer_Seq(5, Prec_GoOut);
    end;
  end;
  CreatePortalGlow(mothership:GetPosition()+Vector3(50, 10, -50), Vector3(-1, 0, 0), 3);
  NewPrecFlight("Prec_Group", "prec1", mothership:GetPosition()+Vector3(45, 10, -50), 1, 17, Vector3(-1, 0, 0));
  SedoySpeaker:Message("#L_Sedoy_08", 2, "Lastochka_OK");
end;


--info:top=1740,left=510,allvis=0;
function Lastochka_OK()
  LastochkaSpeaker:Message("#L_Lastochka_03", 4);
end;


--info:top=1770,left=510,allvis=0;
function Prec_GoOut()
  if (flight_prec1:IsAlive()) then
    CreatePortalGlow(flight_prec1:GetPosition()+Vector3(20, 0, 0), Vector3(-1, 0, 0), 5);
    flight_prec1:Move(flight_prec1:GetPosition()+Vector3(20, 0, 0));
    Timer_Seq(4, FlightPrec1_GoOut);
  end;
  if (lMission==3) then
    NewTrigger("outside", TrueCondition, Dialog_OnPath, mothership, mothership:GetPosition(), 200);
  end;
  Timer_Seq(5, What_IsIt);
end;


--info:top=1810,left=510,allvis=0;
function FlightPrec1_GoOut()
  flight_prec1:Delete();
end;


--info:top=1830,left=650,allvis=0;
function Dialog_OnPath()
  StartDialog("Dialog15");
end;


--info:top=1830,left=420,allvis=0;
function What_IsIt()
  StartDialog("Dialog16");
end;


--info:top=1580,left=510,allvis=0;
function finMission()
  AutoSave("#AS_mission_13_1");
end;


--info:top=1620,left=630,allvis=0;
function New_Gray_Attack()
  NewGreyFlight("Grey_Group", "greyA1", mothership:GetPosition()+Vector3(60, 5, 0), 1, 17, Vector3(-1, 0, 0));
  countf = flight_greyA1:GetPilotCount();
  for i=0,countf,1 do
    pilot_tmp = flight_greyA1:GetPilotByNumber(i);
    ship_tmp = pilot_tmp:GetShip();
    ship_tmp:PlayFX("Cloak Out.shfx");
  end;
  NewGreyFlight("Grey_Group", "greyA2", mothership:GetPosition()+Vector3(0, 5, -70), 1, 15, Vector3(0, 0, 1));
  countf = flight_greyA2:GetPilotCount();
  for i=0,countf,1 do
    pilot_tmp = flight_greyA2:GetPilotByNumber(i);
    ship_tmp = pilot_tmp:GetShip();
    ship_tmp:PlayFX("Cloak Out.shfx");
  end;
  NewGreyFlight("Grey_Group", "greyA3", mothership:GetPosition()+Vector3(-40, 5, 65), 1, 15, Vector3(0.5, 0, -0.5));
  countf = flight_greyA3:GetPilotCount();
  for i=0,countf,1 do
    pilot_tmp = flight_greyA3:GetPilotByNumber(i);
    ship_tmp = pilot_tmp:GetShip();
    ship_tmp:PlayFX("Cloak Out.shfx");
  end;
  SetAllDied({flight_greyA1,flight_greyA2,flight_greyA3}, Prec_GoOut);
  AlexandraSpeaker:Message("#L_Alexandra_03", 2, "New_Pirates_Attack");
end;


--info:top=110,left=320,allvis=0;
function StartInitMission()
  RecrTeam = CreateTeam("Recruits");
  GreyTeam = CreateTeam("Grey");
  PirateTeam = CreateTeam("Pirates");
  KfniTeam = CreateTeam("Kfni");
  KalemTeam = CreateTeam("Merc");
  PrecTeam = CreateTeam("Prec");

  MothershipPilot = mothership:GetPilot();
  MothershipFlight = MothershipPilot:GetFlight();

  Player_Group = MothershipPilot:GetGroup();
  Recr_Group = RecrTeam:CreateGroup("Recr_Group");
  Grey_Group = GreyTeam:CreateGroup("Grey_Group");
  Pirate_Group = PirateTeam:CreateGroup("Pirate_Group");
  Kalem_Group = KalemTeam:CreateGroup("Kalem_Group");
  Kfni_Group = KfniTeam:CreateGroup("Kfni_Group");
  Prec_Group = PrecTeam:CreateGroup("Prec_Group");
  new_SetGroupRelations("Grey_Group", "Pirate_Group", "F");
  new_SetGroupRelations("Grey_Group", "PiratesGroup_1", "F");
  new_SetGroupRelations("GreyGroup_1", "Pirate_Group", "F");
  new_SetGroupRelations("GreyGroup_1", "Grey_Group", "F");
  new_SetGroupRelations("Kfni_Group", PLAYER_GROUP, "F");
  new_SetGroupRelations("Prec_Group", PLAYER_GROUP, "F");

  HeroSpeaker = GetPilot("Hero");
  SedoySpeaker = GetPilot("Sedoy");
  CheSpeaker = GetPilot("Che");
  GreySpeaker = CreatePilot("GreyMaster");
  AlexandraSpeaker = CreatePilot("Alexandra");
  KalemSpeaker = CreatePilot("Kalem");
  LastochkaSpeaker = CreatePilot("Lastochka2");
  Pirate1Speaker = CreatePilot("PirateCaptain");
  Pirate2Speaker = CreatePilot("PirateTough");
  RestorePerks(AlexandraSpeaker, "AlexandraPerks");
  RestorePerks(LastochkaSpeaker, "LastochkaPerks");

-- транспорты с ГК
  NewFlight("Pirate_Group", "pirG1", "Walrus_pir1", "PirateTough", VKS:GetPosition()+Vector3(0, 0, 30), Vector3(1, 0, 0));
  NewFlight("Pirate_Group", "pirG2", "Walrus_pir1", "PirateTough", VKS:GetPosition()+Vector3(0, 0, 50), Vector3(1, 0, 0));
  NewFlight("Kalem_Group", "pirG3", "Walrus_pir1", "PirateTough", VKS:GetPosition()+Vector3(-20, 0, 40), Vector3(1, 0, 0));
  trig_LDied = NewTrigger("f_died", TrueCondition, Lastochka_Died, flight_pirG3);
-- наемники, если взяли
  lQLabel = GetQuestLabel("Quest13Path");
  if (lQLabel=="merc") then
    NewRecruitFlight("Recr_Group", "rec1", PORTAL_R:GetPosition()+Vector3(-30, 25, 40), 1, 19, Vector3(-0.33, 0, 0.67));
    Timer_Seq(1, Recruits_Escort);
  end;
-- патруль серых
  NewGreyFlight("Grey_Group", "gr1", PORTAL_R:GetPosition()+Vector3(-350, 10, -100), 1, 13, Vector3(1, 0, 0));
  NewShip("gr2_1", "Trident_gr1", "GreyOfficer", PORTAL_R:GetPosition()+Vector3(-360, 10, -110), Vector3(1, 0, 0));
  flight_gr1:AddShip(ship_gr2_1);
  flight_gr1:Escort(mothership);
  NewTrigger("f_object_inside", TrueCondition, Meet_Grey_Patrol, flight_gr1, mothership, 120);
  -- упреждение на досрочную атаку
  NewTrigger("f_died", TrueCondition, Patrol_Died, flight_gr1);
  lMessenger = 0;
-- охрана у станции
  NewGreyFlight("Grey_Group", "gr3", VKS:GetPosition()+Vector3(20, 10, 0), 1, 14, Vector3(1, 0, 0));
  NewGreyFlight("Grey_Group", "gr4", VKS:GetPosition()+Vector3(10, 10, -20), 1, 13, Vector3(1, 0, 0));

  ClearRoute();

  lMission = 1;
  lCrashed = 0;

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


--info:top=140,left=520,allvis=0;
function My_TeleporterAction()
  local mshname=mothership:GetName();
  if (mshname~="mothership_kfni") then
    OutputToScreenLog("#L_sys_01", 7);
    Timer_Seq(7, fin_failed);
  else
    Timer_Seq(1, TeleporterAction);
  end;
end;


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


--info:top=170,left=380,allvis=0;
function Recruits_Escort()
  if (flight_rec1:IsAlive()) then
    if (lCrashed==0) then
      escortShip = mothership;
    elseif (not(HeroSpeaker:IsInBase())) and (not(HeroSpeaker:IsBasePilot())) and (HeroSpeaker:IsCanSayReplica()) then
      escortShip = HeroSpeaker:GetShip();
    elseif (not(SedoySpeaker:IsInBase())) and (not(SedoySpeaker:IsBasePilot())) and (SedoySpeaker:IsCanSayReplica()) then
      escortShip = SedoySpeaker:GetShip();
    elseif (not(CheSpeaker:IsInBase())) and (not(CheSpeaker:IsBasePilot())) and (CheSpeaker:IsCanSayReplica()) then
      escortShip = CheSpeaker:GetShip();
    else
      escortShip = mothership;
    end;
    flight_rec1:Escort(escortShip);
    Timer_Seq(30, Recruits_Escort);
  end;
end;


--info:top=200,left=30,allvis=0;
function Lastochka_Died()
  OutputToScreenLog("#L_sysmsg_01",5);
  Timer_Seq(5, finFailed_fin);
end;


--info:top=240,left=30,allvis=0;
function finFailed_fin()
  EndGame("gamefailed");
end;


--info:top=70,left=130,allvis=0;
function ACTION_INIT()
  _FTrader_exclude();
  StartInitMission();

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


--info:top=70,left=570,allvis=0;
function ACTION_LEAVELOCATION()
  _FTrader_include();
  local mshname=mothership:GetName();
  if (mshname~="mothership_kfni") then
    Terminate();
  else
    if (GetQuestState("quest_13c_04")==QUEST_PROCESS) then SetQuestState("quest_13c_04", QUEST_DONE); end;
    SetInitTeamRelation("Kfni", PLAYER_TEAM, RELATION_FRIEND);
    SetQuestLabel("XT24Permit", "no");
    SetQuestLabel("StartComplete", "yes");
    RemovePost("mail_013");
    AddPost("news013_1");
    AddPost("news013_5");
    AddPost("news013_2");
    AddPost("news013_6");
    AddPost("news013_3");
    AddPost("news013_7");
    AddPost("news013_4");
    if (lQLabel=="merc") then
      if (lMission==1) then
        AddPost("book013c_6");
      elseif (lMission==2) then
        AddPost("book013c_8");
      else
        if (lWait==1) then
          AddPost("book013c_3");
        else
          AddPost("book013c_4");
        end;
      end;
    else
      if (lMission==1) then
        AddPost("book013c_5");
      elseif (lMission==2) then
        AddPost("book013c_7");
      else
        if (lWait==1) then
          AddPost("book013c_1");
        else
          AddPost("book013c_2");
        end;
      end;
    end;
    QuestActionDone();
    QuestStart("mission_14c");
  end;

  return TRUE;
end;


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

  return TRUE;
end;
