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


--info:top=150,left=40,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=160,left=180,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=190,left=220,allvis=0;
function Sedoy_Warning()
  SedoySpeaker:Message("#L_Sedoy_02", 5);
end;


--info:top=220,left=240,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=220,left=50,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=250,left=180,allvis=0;
function Patrol_Died()
  if (lMessenger==0) then
    -- дубль на случай сверхдальнего забоя
    new_SetGroupRelations("Grey_Group", PLAYER_GROUP, "W");
    new_SetGroupRelations("GreyGroup_1", PLAYER_GROUP, "W");
    YanaSpeaker:Message("#L_Yana_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=300,left=420,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=340,left=120,allvis=0;
function View_Grey_Base()
  lMission = 3;
  SetQuestState("quest_13e_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=380,left=120,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=420,left=120,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=780,left=300,allvis=0;
function Near_MotherShip()
  local myTriggerContext = GetTriggerContext();
  local flight = myTriggerContext:GetFlight();
  flight:Stop();
end;


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


--info:top=1050,left=60,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=460,left=120,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, Yana_Nha, Pirate_Group, mothership);
  end;
end;


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


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


--info:top=550,left=120,allvis=0;
function Yana_Nha()
  if (trig_WAttack) then
    trig_WAttack:Delete();
    trig_WAttack = NIL;
  end;
  YanaSpeaker:Message("#L_Yana_02", 6);
end;


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


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


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


--info:top=1110,left=310,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=530,left=270,allvis=0;
function Lastochka_Dialog()
  lWalrusAttack = NIL;
  lLastochkaDialog = 0;
  SetNormalTimeMode();
  StartDialog("Dialog7");
end;


--info:top=340,left=420,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=340,left=680,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=380,left=680,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=420,left=680,allvis=0;
function Pirates_RunAway_2()
  Pirate1Speaker:Message("#L_pirate_03", 4, "Pirates_RunAway_3");
end;


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


--info:top=560,left=420,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=600,left=420,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=660,left=420,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
    Timer_Seq(5, Kill_Lastochka);
    SedoySpeaker:Message("#L_Sedoy_05", 4);
    countf = 2;
    if (flight_gr5:IsAlive()) then
      NewTrigger("f_died", DiedFCondition, Dialog_3, flight_gr5);
    else
      countf = 1;
    end;
  end;
end;


--info:top=1150,left=590,allvis=0;
function DiedFCondition()
  countf = countf - 1;
  if (countf==0) then
    return TRUE;
  end;
  return FALSE;
end;


--info:top=1120,left=490,allvis=0;
function Kill_Lastochka()
  trig_LDied:Delete();
  NewGreyFlight("Grey_Group", "gr6", ship_pirG3_1:GetPosition()+Vector3(-10, 0, 90), 2, 11, Vector3(0, 0, -1));
  count = flight_gr6:GetPilotCount();
  for i=0,count,1 do
    pilot_tmp = flight_gr6:GetPilotByNumber(i);
    ship_tmp = pilot_tmp:GetShip();
    ship_tmp:PlayFX("Cloak Out.shfx");
  end;
  flight_gr6:RocketAttack(ship_pirG3_1);
  Timer_Seq(5, Lastochka_Died_Now);
  if (lMission==3) then
    NewTrigger("f_died", DiedFCondition, Dialog_3, flight_gr6);
  else
    NewTrigger("f_died", TrueCondition, Dialog_3, flight_gr6);
  end;
end;


--info:top=1170,left=490,allvis=0;
function Lastochka_Died_Now()
  if (flight_pirG3:IsAlive()) then
    ship_pirG3_1:Blow();
  end;
  YanaSpeaker:Message("#L_Yana_04", 2, "Seek_Lastocka_1");
end;


--info:top=1210,left=490,allvis=0;
function Seek_Lastocka_1()
  HeroSpeaker:Message("#L_Hero_07", 8, "Seek_Lastocka_2");
end;


--info:top=1250,left=490,allvis=0;
function Seek_Lastocka_2()
  UelDSpeaker:Message("#L_UelD_01", 4);
end;


--info:top=710,left=240,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=750,left=240,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=800,left=110,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=840,left=110,allvis=0;
function Break_Through()
  trig_Break:Delete();
  trig_Break = NIL;
  if (LineToStation) then
    LineToStation:Delete();
    LineToStation = NIL;
  end;
  SetNormalTimeMode();
  SetQuestState("quest_13e_01", QUEST_DONE);
  StartDialog("Dialog10");
end;


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


--info:top=910,left=170,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_13e_01")==QUEST_PROCESS) then RemoveQuest("quest_13e_01"); end;
  if (GetQuestState("quest_13e_02")==QUEST_PROCESS) then SetQuestState("quest_13e_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("Yana_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=940,left=220,allvis=0;
function Yana_What()
  YanaSpeaker:Message("#L_Yana_03", 4);
end;


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


--info:top=990,left=170,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_13e_03", "#L_Quest_13e_03a", "#L_Quest_13e_03b", QUEST_PROCESS);
  else
    lPirateAttack = 1;
  end;
  NewTrigger("side_inside", TrueCondition, Need_Third_02, PLAYER_TEAM, PIR:GetPosition(), 150);
end;


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


--info:top=710,left=420,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=1150,left=720,allvis=0;
function Dialog_3()
  SetNormalTimeMode();
--------------------------------------------------------------------------------
  --StartDialog("Dialog13");
  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() >= 5) then
      choose_pilot();
    else
      StartDialog("Dialog13");
    end;
  else
    StartDialog("Dialog13");
  end; 
end;

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



--info:top=1190,left=720,allvis=0;
function Wait_Pirates()
  AddPilotToPlayer(KalemSpeaker);
  if (lMission==1) then
    posn = PIR:GetPosition()+Vector3(20, 0, 500)
  else
    posn = VKS:GetPosition()+Vector3(20, 0, 500)
  end;
  NewPirateFlight("Pirate_Group", "pirA1", posn, 1, 18, Vector3(0, 0, -1));
  NewPirateFlight("Pirate_Group", "pirA2", posn+Vector3(10, 0, 20), 1, 18, Vector3(0, 0, -1));
  NewPirateFlight("Pirate_Group", "pirA3", posn+Vector3(-10, 0, 20), 1, 18, Vector3(0, 0, -1));
  flight_pirA2:Escort(ship_pirA1_1);
  flight_pirA3:Escort(ship_pirA1_1);
  if (lMission==3) 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(YanaSpeaker:IsInBase())) and (not(YanaSpeaker:IsBasePilot())) and (YanaSpeaker:IsCanSayReplica()) then
    escortShip = YanaSpeaker:GetShip();
  elseif (not(UelDSpeaker:IsInBase())) and (not(UelDSpeaker:IsBasePilot())) and (UelDSpeaker:IsCanSayReplica()) then
    escortShip = UelDSpeaker:GetShip();
  else
    escortShip = mothership;
  end;
  flight_pirA1:Move(escortShip:GetPosition());
  NewTrigger("f_inside", TrueCondition, Pirates_Damn, flight_pirA1, escortShip:GetPosition(), 200);
  NewTrigger("f_inside", TrueCondition, New_Pirates_Attack, flight_pirA1, escortShip:GetPosition(), 100);
end;


--info:top=1230,left=720,allvis=0;
function Pirates_Damn()
  SetNormalTimeMode();
  SedoySpeaker:Message("#L_Sedoy_08", 4, "Pirates_Damn_1");
  SetAllDied({flight_pirA1,flight_pirA2,flight_pirA3}, Bers_GoOut);
  Timer_Seq(20, Uel_ComeOn);
end;


--info:top=1230,left=830,allvis=0;
function New_Pirates_Attack()
  flight_pirA1:Stop();
end;


--info:top=1270,left=720,allvis=0;
function Pirates_Damn_1()
  KalemSpeaker:Message("#L_Kalem_02", 3, "Pirates_Damn_2");
end;


--info:top=1310,left=720,allvis=0;
function Pirates_Damn_2()
  Pirate1Speaker:Message("#L_pirate_04", 10);
end;


--info:top=1330,left=600,allvis=0;
function Uel_ComeOn()
  if (lMission==1) then
    CreatePortalGlow(PIR:GetPosition()+Vector3(50, 10, -50), Vector3(-1, 0, 0), 3);
    NewFlight("Inoco_Group", "newMS", "Mothership_uel_pl1", "Uel", PIR:GetPosition()+Vector3(50, 10, -50), Vector3(-1, 0, 0));
    NewBerserkFlight("Berserk_Group", "ber1", PIR:GetPosition()+Vector3(50, 10, -35), 1, 17, Vector3(-1, 0, 0));
    NewBerserkFlight("Berserk_Group", "ber2", PIR:GetPosition()+Vector3(50, 10, -65), 1, 17, Vector3(-1, 0, 0));
    NewBerserkFlight("Berserk_Group", "ber3", PIR:GetPosition()+Vector3(50, -5, -50), 2, 17, Vector3(-1, 0, 0));
  else
    CreatePortalGlow(VKS:GetPosition()+Vector3(80, 10, 20), Vector3(-1, 0, 0), 3);
    NewFlight("Inoco_Group", "newMS", "Mothership_uel_pl1", "Uel", VKS:GetPosition()+Vector3(80, 10, 20), Vector3(-1, 0, 0));
    NewBerserkFlight("Berserk_Group", "ber1", VKS:GetPosition()+Vector3(80, 10, 5), 1, 17, Vector3(-1, 0, 0));
    NewBerserkFlight("Berserk_Group", "ber2", VKS:GetPosition()+Vector3(80, 10, 35), 1, 17, Vector3(-1, 0, 0));
    NewBerserkFlight("Berserk_Group", "ber3", VKS:GetPosition()+Vector3(80, -5, 20), 2, 17, Vector3(-1, 0, 0));
  end;
  UelSpeaker:Message("#L_Uel_01", 4);
end;


--info:top=1370,left=600,allvis=0;
function Bers_GoOut()
  posn = NIL;
  if (flight_ber1:IsAlive()) then
    posn = flight_ber1:GetPosition();
  elseif (flight_ber2:IsAlive()) then
    posn = flight_ber2:GetPosition();
  elseif (flight_ber3:IsAlive()) then
    posn = flight_ber3:GetPosition();
  end;
  if (posn) then
    CreatePortalGlow(posn+Vector3(20, 0, 0), Vector3(-1, 0, 0), 5);
    if (flight_ber1:IsAlive()) then
      flight_ber1:Move(posn+Vector3(20, 0, 0));
    end;
    if (flight_ber2:IsAlive()) then
      flight_ber2:Move(posn+Vector3(20, 0, 0));
    end;
    if (flight_ber3:IsAlive()) then
      flight_ber3:Move(posn+Vector3(20, 0, 0));
    end;
    Timer_Seq(4, FlightBers_GoOut);
  end;
  Timer_Seq(5, What_IsIt);
end;


--info:top=1400,left=640,allvis=0;
function FlightBers_GoOut()
  if (flight_ber1:IsAlive()) then flight_ber1:Delete(); end;
  if (flight_ber2:IsAlive()) then flight_ber2:Delete(); end;
  if (flight_ber3:IsAlive()) then flight_ber3:Delete(); end;
end;


--info:top=1420,left=600,allvis=0;
function What_IsIt()
  SetNormalTimeMode();
  StartDialog("Dialog14");
end;


--info:top=1460,left=600,allvis=0;
function Mothership_New()
  Change_Mothership();
  lCrashed = 0;
  Anim_3 = CreateMovie(_TARGETCAM);
  Anim_3:SetLength(7);
  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), 7);
  Anim_3:Play();
  Anim_3:AttachEscapeScript("After_Change");
  Anim_3:AttachAction("After_Change");
end;


--info:top=1490,left=640,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(UelSpeaker);
  mothership:AddShipToInventory("Trident_ino3", 1);
  mothership:AddShipToInventory("EvilEye_ino2", 1);
end;


--info:top=1510,left=600,allvis=0;
function After_Change()
  StartDialog("Dialog15");
end;


--info:top=1550,left=600,allvis=0;
function GoTo_Lidau()
  local pos = mothership:GetPosition();
  local orn = mothership:GetVectorOrientation();
  local glow_orn = mothership: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;
  tmpNull = CreateCarcass("NULL", pos+orn, glow_orn); 
  MothershipFlight:Move(pos+orn);
  tmpNull:AddMeshFX("PARTICLESYSTEM", "portal.pst", glow_orn);
  tmpNull:AddMeshFX("PARTICLESYSTEM", "portal_rotate.pst", glow_orn);
  mothership:PlayFX("Portal Out.shfx");
  Anim_4 = CreateMovie(_TARGETCAM);
  Anim_4:SetLength(5);
  Anim_4:SetInterpolate(_LINEAR);
  Anim_4:SetTarget(mothership);
  Anim_4:InitMovie(1, TRUE, 1);
  Anim_4:AddPosKey(mothership:GetPosition()+Vector3(orn.x*(-1.827)+orn.z*0.8134, 10-orn.y, orn.z*(-1.827)-orn.x*0.8134), 5);
  Anim_4:Play();
  Anim_4:AttachEscapeScript("finMission");
  Anim_4:AttachAction("finMission");
end;


--info:top=1590,left=600,allvis=0;
function finMission()
  CustomTeleportate("xt24", "lidau", "long_jump");
end;


--info:top=110,left=320,allvis=0;
function StartInitMission()
  RecrTeam = CreateTeam("Recruits");
  GreyTeam = CreateTeam("Grey");
  PirateTeam = CreateTeam("Pirates");
  InocoTeam = CreateTeam("Inoco");
  KalemTeam = CreateTeam("Merc");
  BerserkTeam = CreateTeam("Berserk");

  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");
  Inoco_Group = InocoTeam:CreateGroup("Inoco_Group");
  Berserk_Group = BerserkTeam:CreateGroup("Berserk_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("Inoco_Group", PLAYER_GROUP, "F");
  new_SetGroupRelations("Berserk_Group", PLAYER_GROUP, "CF");
  new_SetGroupRelations("Berserk_Group", "Recr_Group", "F");
  new_SetGroupRelations("Berserk_Group", "Inoco_Group", "CF");

  HeroSpeaker = GetPilot("Hero");
  SedoySpeaker = GetPilot("Sedoy");
  YanaSpeaker = GetPilot("Yana");
  UelDSpeaker = GetPilot("Uel2");
  GreySpeaker = CreatePilot("GreyMaster");
  UelSpeaker = CreatePilot("Uel");
  KalemSpeaker = CreatePilot("Kalem");
  LastochkaSpeaker = CreatePilot("Lastochka2");
  Pirate1Speaker = CreatePilot("PirateCaptain");
  Pirate2Speaker = CreatePilot("PirateTough");

-- транспорты с ГК
  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=570,allvis=0;
function My_TeleporterAction()
  local mshname=mothership:GetName();
  if (mshname~="mothership_uel") then
    OutputToScreenLog("#L_sys_01", 7);
    Timer_Seq(7, fin_failed);
  else
    Timer_Seq(1, TeleporterAction);
  end;
end;


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


--info:top=140,left=410,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(YanaSpeaker:IsInBase())) and (not(YanaSpeaker:IsBasePilot())) and (YanaSpeaker:IsCanSayReplica()) then
      escortShip = YanaSpeaker:GetShip();
    elseif (not(UelDSpeaker:IsInBase())) and (not(UelDSpeaker:IsBasePilot())) and (UelDSpeaker:IsCanSayReplica()) then
      escortShip = UelDSpeaker:GetShip();
    else
      escortShip = mothership;
    end;
    flight_rec1:Escort(escortShip);
    Timer_Seq(30, Recruits_Escort);
  end;
end;


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


--info:top=230,left=410,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();
  if (tmpNull) then tmpNull:Delete(); end;
  local mshname=mothership:GetName();
  if (mshname~="mothership_uel") then
    Terminate();
  else
    SetInitTeamRelation("Inoco", PLAYER_TEAM, RELATION_FRIEND);
    SetInitTeamRelation("Berserk", PLAYER_TEAM, RELATION_NEUTRAL);
    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");
    QuestActionDone();
  end;

  return TRUE;
end;


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

  return TRUE;
end;
