------------------------------------------------------------
--     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=130,allvis=0;
function StartMission()
  ViperSpeaker:Message("#L_Viper_01", 15);
  trig_ViperAttack = NewTrigger("timer", TrueCondition, Viper_Attack, 120);
  Timer_Seq(5, Viper_NeedOut);
end;


--info:top=150,left=130,allvis=0;
function Viper_NeedOut()
  SetNormalTimeMode();
  MothershipFlight:Stop();
  if (ViperSpeaker:IsInBase())or(ViperSpeaker:IsBasePilot()) then
    OutputToScreenLog("#L_sys_02", 9);
    trig_Hint = NewTrigger("timer", TrueCondition, Viper_NeedOut, 10);
  else
    Timer_Seq(1, GoTo_Station);
  end;
end;


--info:top=150,left=240,allvis=0;
function Viper_Attack()
  trig_Hint:Delete();
  OutputToScreenLog("#L_sys_03", 5);
  mothership:Blow();
end;


--info:top=190,left=130,allvis=0;
function GoTo_Station()
  trig_ViperAttack:Delete();
  local ViperFlight = ViperSpeaker:GetFlight();
  local ViperShip = ViperSpeaker:GetShip();
  if (ViperFlight:GetPilotCount()==1) then
    Player_Group:RemoveFlight(ViperFlight);
    Viper_Group:AddFlight(ViperFlight);
    ViperFlight:Patrol({STO_Station:GetPosition()+Vector3(0, 20, -40), STATION_VKS:GetPosition()+Vector3(60, 0, -100), PORTAL_U:GetPosition()+Vector3(50, 0, -100)}, FALSE);
  else
    local NewViperFlight = Viper_Group:CreateFlight("NewViperFlight");
    NewViperFlight:AddShip(ViperShip);
    NewViperFlight:Patrol({STO_Station:GetPosition()+Vector3(0, 20, -40), STATION_VKS:GetPosition()+Vector3(60, 0, -100), PORTAL_U:GetPosition()+Vector3(50, 0, -100)}, FALSE);
  end;
  flight_viper = ViperSpeaker:GetFlight();
  AddQuest("quest_16a_01", "#L_Quest_16a_01a", "#L_Quest_16a_01b", QUEST_PROCESS);
  nav_current = CreateNavPoint("", mothership:GetPosition(), "nav_void.dds", RGB(255,255,255), mothership);
  LineToStation = CreateLine(nav_current, NP_STATION_VKS, RGB(20,80,220), .3, TRUE, .2, 1);
  NewTrigger("outside", TrueCondition, Lastochka_Bad, mothership, mothership:GetPosition(), 100);
  NewVksFlight("VksGroup_1", "sb1", STATION_VKS:GetPosition()+Vector3(60, 20, 30), 1, 18, Vector3(-1, 0, 0));
  flight_sb1:Escort(mothership);
  NewTrigger("f_object_inside", TrueCondition, SB_Dialog, flight_sb1, mothership, 150);
  NewTrigger("inside", TrueCondition, Near_StationVKS, mothership, STATION_VKS:GetPosition(), 180);
end;


--info:top=220,left=170,allvis=0;
function Lastochka_Bad()
  LastochkaSpeaker:Message("#L_Lastochka_01", 3, "Sedoy_Go");
end;


--info:top=260,left=170,allvis=0;
function Sedoy_Go()
  SedoySpeaker:Message("#L_Sedoy_01", 8);
end;


--info:top=280,left=30,allvis=0;
function SB_Dialog()
  SetNormalTimeMode();
  StartDialog("Dialog1");
end;


--info:top=320,left=30,allvis=0;
function SB_GoOut()
  flight_sb1:Move(Vector3(0, 500, -3000));
end;


--info:top=340,left=130,allvis=0;
function Near_StationVKS()
  SetQuestState("quest_16a_01", QUEST_DONE);
  SetNormalTimeMode();
  MothershipFlight:Stop();
  nav_current:Delete();
  LineToStation:Delete();
  DispetcherSpeaker:Message("#L_disp_01", 9);
  AddPost("mail_016a");
  AddQuest("quest_16a_02", "#L_Quest_16a_02a", "#L_Quest_16a_02b", QUEST_PROCESS);
  NewTrigger("outside", TrueCondition, Heder_ComeOn, mothership, STATION_VKS:GetPosition(), 200);
end;


--info:top=380,left=130,allvis=0;
function Heder_ComeOn()
  SetQuestLabel("Heder","alive");
  mRadar = Player_Group:CreateRadar(PORTAL_R:GetPosition(), 300, 0.1);
  Timer_Seq(1, First_Parti);
  Anim_1 = CreateMovie(_TARGETCAM);
  Anim_1:SetLength(15);
  Anim_1:SetInterpolate(_LINEAR);
  Anim_1:SetTarget(PORTAL_R);
  Anim_1:InitMovie(1, TRUE, 1);
  Anim_1:AddPosKey(PORTAL_R:GetPosition()+Vector3(-100, 50, 0), 0);
  Anim_1:Play();
  Anim_1:AttachEscapeScript("Heder_ComeOn_Escape");
  Anim_1:AttachAction("Heder_Dialog");
end;


--info:top=410,left=170,allvis=0;
function First_Parti()
  PORTAL_U:Start(5, 0);
  NewFlight("Heder_Group", "hed1", "Lion_Tramp", "VksSuperAce", PORTAL_R:GetPosition()+Vector3(-10, 0, 0), Vector3(-1, 0, 0));
  NewVksFlight("Heder_Group", "hed2", PORTAL_R:GetPosition()+Vector3(0, 0, 10), 1, 20, Vector3(-1, 0, 0));
  NewVksFlight("Heder_Group", "hed3", PORTAL_R:GetPosition()+Vector3(0, 0, -10), 1, 20, Vector3(-1, 0, 0));
  NewVksFlight("Heder_Group", "hed4", PORTAL_R:GetPosition()+Vector3(0, -10, 0), 2, 20, Vector3(-1, 0, 0));
  flight_hed2:Escort(ship_hed1_1);
  flight_hed3:Escort(ship_hed1_1);
  flight_hed4:Escort(ship_hed1_1);
  ship_hed1_1:ApplyImpulse(15, Vector3(-1, 0, 0));
  flight_hed1:Move(PORTAL_R:GetPosition()+Vector3(-100, 0, 0));
  Timer_Seq(7, Second_Parti);
end;


--info:top=450,left=170,allvis=0;
function Second_Parti()
  PORTAL_U:Start(5, 0);
  NewVksFlight("Heder_Group", "hed5", PORTAL_R:GetPosition()+Vector3(-5, 0, 5), 3, 8, Vector3(-1, 0, 0));
  NewVksFlight("Heder_Group", "hed6", PORTAL_R:GetPosition()+Vector3(-5, 0, -5), 3, 8, Vector3(-1, 0, 0));
  NewVksFlight("Heder_Group", "hed7", PORTAL_R:GetPosition()+Vector3(0, -10, 0), 1, 19, Vector3(-1, 0, 0));
  NewVksFlight("Heder_Group", "hed8", PORTAL_R:GetPosition()+Vector3(0, 10, 0), 1, 19, Vector3(-1, 0, 0));
  flight_hed7:Escort(ship_hed5_1);
  flight_hed8:Escort(ship_hed6_1);
  ship_hed5_1:ApplyImpulse(15, Vector3(-1, 0, 0));
  ship_hed6_1:ApplyImpulse(15, Vector3(-1, 0, 0));
  flight_hed5:Move(PORTAL_R:GetPosition()+Vector3(-80, 0, 30));
  flight_hed6:Move(PORTAL_R:GetPosition()+Vector3(-80, 0, -30));
end;


function Heder_ComeOn_Escape()
  if not(flight_hed1) then
    NewFlight("Heder_Group", "hed1", "Lion_Tramp", "VksSuperAce", PORTAL_R:GetPosition()+Vector3(-110, 0, 0), Vector3(-1, 0, 0));
    NewVksFlight("Heder_Group", "hed2", PORTAL_R:GetPosition()+Vector3(-100, 0, 10), 1, 20, Vector3(-1, 0, 0));
    NewVksFlight("Heder_Group", "hed3", PORTAL_R:GetPosition()+Vector3(-100, 0, -10), 1, 20, Vector3(-1, 0, 0));
    NewVksFlight("Heder_Group", "hed4", PORTAL_R:GetPosition()+Vector3(-100, -10, 0), 2, 20, Vector3(-1, 0, 0));
    flight_hed2:Escort(ship_hed1_1);
    flight_hed3:Escort(ship_hed1_1);
    flight_hed4:Escort(ship_hed1_1);
  end;
  if not(flight_hed5) then
    NewVksFlight("Heder_Group", "hed5", PORTAL_R:GetPosition()+Vector3(-80, 0, 30), 3, 8, Vector3(-1, 0, 0));
    NewVksFlight("Heder_Group", "hed6", PORTAL_R:GetPosition()+Vector3(-80, 0, -30), 3, 8, Vector3(-1, 0, 0));
    NewVksFlight("Heder_Group", "hed7", PORTAL_R:GetPosition()+Vector3(-75, -10, 30), 1, 19, Vector3(-1, 0, 0));
    NewVksFlight("Heder_Group", "hed8", PORTAL_R:GetPosition()+Vector3(-75, 10, 30), 1, 19, Vector3(-1, 0, 0));
    flight_hed7:Escort(ship_hed5_1);
    flight_hed8:Escort(ship_hed6_1);
  end;
  Timer_Seq(1, Heder_Dialog);
end;


--info:top=470,left=130,allvis=0;
function Heder_Dialog()
  Player_Group:DeleteRadar(mRadar);
  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=510,left=130,allvis=0;
function Heder_Operate()
  lAfraid = 0;
  STATION_VKS:SetGodMode(FALSE);
  flight_hed5:Patrol({Vector3(500, 20, 300), Vector3(0, 50, 400), STATION_VKS:GetPosition()+Vector3(20, 0, 100)}, FALSE);
  trig_StationBlow1 = NewTrigger("f_inside", TrueCondition, Station_Blow, flight_hed5, STATION_VKS:GetPosition(), 200);
  flight_hed6:Patrol({Vector3(500, 20, -200), Vector3(0, 50, -200), STATION_VKS:GetPosition()+Vector3(20, 0, -100)}, FALSE);
  trig_StationBlow2 = NewTrigger("f_inside", TrueCondition, Station_Blow, flight_hed6, STATION_VKS:GetPosition(), 200);
  count2 = 2;
  trig_H5Died = NewTrigger("f_died", Died2Condition, First_Nothing, flight_hed5);
  trig_H6Died = NewTrigger("f_died", Died2Condition, First_Nothing, flight_hed6);
  flight_hed1:Move(PORTAL_R:GetPosition()+Vector3(-200, 0, 0));
  local flight = ViperSpeaker:GetFlight();
  flight:SetChaseDistance(500);
  trig_ViperDied = NewTrigger("f_died", TrueCondition, failedMission, flight);
  new_SetAllGroupRelations(rel_HvsV, power_rel_1);
  count = 0;
  if (flight_hed1:IsAlive()) then
    count = count +1;
    trig_HederDied = NewTrigger("f_died", TrueCondition, Only_Heder_Died, flight_hed1);
    trig_Hd1Died = NewTrigger("f_died", DiedCondition, Heder_Died, flight_hed1);
    trig_HederAfraided = NewTrigger("HP", TrueCondition, Heder_Afraided, ship_hed1_1, 5000);
  end;
  if (flight_hed2:IsAlive()) then
    count = count +1;
    trig_Hd2Died = NewTrigger("f_died", DiedCondition, Heder_Died, flight_hed2);
  end;
  if (flight_hed3:IsAlive()) then
    count = count +1;
    trig_Hd3Died = NewTrigger("f_died", DiedCondition, Heder_Died, flight_hed3);
  end;
  if (flight_hed4:IsAlive()) then
    count = count +1;
    trig_Hd4Died = NewTrigger("f_died", DiedCondition, Heder_Died, flight_hed4);
  end;
  if (flight_hed5:IsAlive()) then
    count = count +1;
    trig_Hd5Died = NewTrigger("f_died", DiedCondition, Heder_Died, flight_hed5);
  end;
  if (flight_hed6:IsAlive()) then
    count = count +1;
    trig_Hd6Died = NewTrigger("f_died", DiedCondition, Heder_Died, flight_hed6);
  end;
  if (flight_hed7:IsAlive()) then
    count = count +1;
    trig_Hd7Died = NewTrigger("f_died", DiedCondition, Heder_Died, flight_hed7);
  end;
  if (flight_hed8:IsAlive()) then
    count = count +1;
    trig_Hd8Died = NewTrigger("f_died", DiedCondition, Heder_Died, flight_hed8);
  end;
  flight_vks01:EnableChaseDistanceInfinity();
  flight_vks02:EnableChaseDistanceInfinity();
  flight_vks03:EnableChaseDistanceInfinity();
  flight_vks04:EnableChaseDistanceInfinity();
  flight_vks05:EnableChaseDistanceInfinity();
  flight_vks06:EnableChaseDistanceInfinity();
  flight_vks07:EnableChaseDistanceInfinity();
  flight_vks08:EnableChaseDistanceInfinity();
  flight_vks09:EnableChaseDistanceInfinity();
  flight_vks10:EnableChaseDistanceInfinity();
  flight_vks11:EnableChaseDistanceInfinity();
  flight_vks12:EnableChaseDistanceInfinity();
  flight_vks13:EnableChaseDistanceInfinity();
  flight_vks14:EnableChaseDistanceInfinity();
  flight_vks15:EnableChaseDistanceInfinity();
  flight_vks16:EnableChaseDistanceInfinity();
  RemoveQuest("quest_16a_02");
  RemovePost("mail_016a");
  AddQuest("quest_16a_03", "#L_Quest_16a_03a", "#L_Quest_16a_03b", QUEST_PROCESS);
  ViperSpeaker:Message("#L_Viper_02", 5);
end;


--info:top=790,left=230,allvis=0;
function Died2Condition()
  count2 = count2 - 1;
  if (count2==0) then
    return TRUE;
  end;
  return FALSE;
end;


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


function Only_Heder_Died()
  trig_HederDied:Delete();
  trig_HederDied = NIL;
end;


function Station_Need_Blow()
  STATION_VKS:Blow();
end;


--info:top=850,left=420,allvis=0;
function Station_Blow()
  Delete_Triggers();
  if (STATION_VKS:IsAlive()) then Timer_Seq(1, Station_Need_Blow); end;
  if (trig_HederDied) then
    if (flight_hed5:IsAlive()) then flight_hed5:Move(ship_hed1_1:GetPosition()); end;
    if (flight_hed6:IsAlive()) then flight_hed6:Move(ship_hed1_1:GetPosition()); end;
    if (flight_hem1:IsAlive()) then flight_hem1:Move(ship_hed1_1:GetPosition()); end;
    if (flight_hem2:IsAlive()) then flight_hem2:Move(ship_hed1_1:GetPosition()); end;
    if (flight_hem3:IsAlive()) then flight_hem3:Move(ship_hed1_1:GetPosition()); end;
    if (flight_hem4:IsAlive()) then flight_hem4:Move(ship_hed1_1:GetPosition()); end;
  end;
  SetQuestState("quest_16a_03", QUEST_FAILED);
  if (flight_viper:IsAlive()) then
    ViperSpeaker:Message("#L_Viper_03", 5, "Heder_MayBe");
  elseif (trig_HederDied) then
    HederSpeaker:Message("#L_Heder_07", 6, "Sedoy_GameOver");
  else
    Timer_Seq(1, Sedoy_GameOver);
  end;
end;


--info:top=1090,left=420,allvis=0;
function Delete_Triggers()
  if (trig_StationBlow1) then
    trig_StationBlow1:Delete();
    trig_StationBlow1 = NIL;
  end;
  if (trig_StationBlow2) then
    trig_StationBlow2:Delete();
    trig_StationBlow2 = NIL;
  end;
  if (trig_H5Died) then
    trig_H5Died:Delete();
    trig_H5Died = NIL;
  end;
  if (trig_H6Died) then
    trig_H6Died:Delete();
    trig_H6Died = NIL;
  end;
  if (trig_Hem1Died) then
    trig_Hem1Died:Delete();
    trig_Hem1Died = NIL;
  end;
  if (trig_Hem2Died) then
    trig_Hem2Died:Delete();
    trig_Hem2Died = NIL;
  end;
  if (trig_Hem3Died) then
    trig_Hem3Died:Delete();
    trig_Hem3Died = NIL;
  end;
  if (trig_Hem4Died) then
    trig_Hem4Died:Delete();
    trig_Hem4Died = NIL;
  end;
  if (trig_Hem1Attack) then
    trig_Hem1Attack:Delete();
    trig_Hem1Attack = NIL;
  end;
  if (trig_Hem2Attack) then
    trig_Hem2Attack:Delete();
    trig_Hem2Attack = NIL;
  end;
  if (trig_Hem3Attack) then
    trig_Hem3Attack:Delete();
    trig_Hem3Attack = NIL;
  end;
  if (trig_Hem4Attack) then
    trig_Hem4Attack:Delete();
    trig_Hem4Attack = NIL;
  end;
end;


--info:top=890,left=370,allvis=0;
function Heder_MayBe()
  if (trig_HederDied) then
    HederSpeaker:Message("#L_Heder_01", 7, "Heder_GoOut");
  else
    StopHere();
  end;
end;


--info:top=890,left=480,allvis=0;
function Sedoy_GameOver()
  SedoySpeaker:Message("#L_Sedoy_02", 5, "fin_failed");
end;


--info:top=680,left=20,allvis=0;
function Heder_Afraided()
  if (trig_HederDied) then
    HederSpeaker:Message("#L_Heder_02", 4, "HVks_But");
  end;
end;


--info:top=720,left=20,allvis=0;
function HVks_But()
  VksSpeaker:Message("#L_vks_01", 3, "Heder_Damn");
end;


--info:top=760,left=20,allvis=0;
function Heder_Damn()
  lAfraid = 1;
  Delete_Triggers();
  if (trig_HederDied) then
    if (flight_hed5:IsAlive()) then flight_hed5:Move(ship_hed1_1:GetPosition()); end;
    if (flight_hed6:IsAlive()) then flight_hed6:Move(ship_hed1_1:GetPosition()); end;
    if (flight_hem1 ~= NIL) then if (flight_hem1:IsAlive()) then flight_hem1:Move(ship_hed1_1:GetPosition()); end; end;
    if (flight_hem2 ~= NIL) then if (flight_hem2:IsAlive()) then flight_hem2:Move(ship_hed1_1:GetPosition()); end; end;
    if (flight_hem3 ~= NIL) then if (flight_hem3:IsAlive()) then flight_hem3:Move(ship_hed1_1:GetPosition()); end; end;
    if (flight_hem4 ~= NIL) then if (flight_hem4:IsAlive()) then flight_hem4:Move(ship_hed1_1:GetPosition()); end; end;
    HederSpeaker:Message("#L_Heder_03", 4, "Heder_GoOut");
  end;
end;


--info:top=680,left=160,allvis=0;
function First_Nothing()
  mRadar = Player_Group:CreateRadar(Vector3(350, 0, -200), 200, 0.1);
  CreatePortalGlow(Vector3(350, 0, -200), Vector3(-1, 0, 0), 5);
  NewVksFlight("Heder_Group", "hem1", Vector3(345, 0, -200), 3, 18, Vector3(-1, 0, 0));
  NewVksFlight("Heder_Group", "hem2", Vector3(350, 0, -190), 2, 18, Vector3(-1, 0, 0));
  NewVksFlight("Heder_Group", "hem3", Vector3(350, 0, -210), 2, 18, Vector3(-1, 0, 0));
  NewVksFlight("Heder_Group", "hem4", Vector3(350, -10, -200), 2, 20, Vector3(-1, 0, 0));
  flight_hem2:Escort(ship_hem1_1);
  flight_hem3:Escort(ship_hem1_1);
  flight_hem4:Escort(ship_hem1_1);
  flight_hem1:Move(STATION_VKS:GetPosition());
  Timer_Seq(45, Spy_Go);
  Anim_2 = CreateMovie(_TARGETCAM);
  Anim_2:SetLength(8);
  Anim_2:SetInterpolate(_LINEAR);
  Anim_2:SetTarget(ship_hem1_1);
  Anim_2:InitMovie(1, TRUE, 2);
  Anim_2:AddPosKey(Vector3(250, 40, -230), 0);
  Anim_2:Play();
  Anim_2:AttachEscapeScript("Kalem_Difficult");
  Anim_2:AttachAction("Kalem_Difficult");
  HederSpeaker:Message("#L_Heder_04", 7);
end;


--info:top=720,left=160,allvis=0;
function Kalem_Difficult()
  KalemSpeaker:Message("#L_Kalem_01", 5);
  count2 = 4;
  trig_Hem1Died = NewTrigger("f_died", Died2Condition, doneMission, flight_hem1);
  trig_Hem2Died = NewTrigger("f_died", Died2Condition, doneMission, flight_hem2);
  trig_Hem3Died = NewTrigger("f_died", Died2Condition, doneMission, flight_hem3);
  trig_Hem4Died = NewTrigger("f_died", Died2Condition, doneMission, flight_hem4);
  trig_Hem1Attack = NewTrigger("f_inside", TrueCondition, Kill_Station, flight_hem1, STATION_VKS:GetPosition(), 100);
  trig_Hem2Attack = NewTrigger("f_inside", TrueCondition, Kill_Station, flight_hem2, STATION_VKS:GetPosition(), 100);
  trig_Hem3Attack = NewTrigger("f_inside", TrueCondition, Kill_Station, flight_hem3, STATION_VKS:GetPosition(), 100);
  trig_Hem4Attack = NewTrigger("f_inside", TrueCondition, Kill_Station, flight_hem4, STATION_VKS:GetPosition(), 100);
  NewTrigger("side_object", TrueCondition, Reaction, PLAYER_TEAM, ship_hem1_1, 170);
end;


--info:top=810,left=160,allvis=0;
function Kill_Station()
  Delete_Triggers();
  if (flight_hem1:IsAlive()) then flight_hem1:Attack(STATION_VKS); end;
  if (flight_hem2:IsAlive()) then flight_hem2:Attack(STATION_VKS); end;
  if (flight_hem3:IsAlive()) then flight_hem3:Attack(STATION_VKS); end;
  if (flight_hem4:IsAlive()) then flight_hem4:Attack(STATION_VKS); end;
  Timer_Seq(5, Station_Need_Blow);
  Anim_3 = CreateMovie(_TARGETCAM);
  Anim_3:SetLength(10);
  Anim_3:SetInterpolate(_LINEAR);
  Anim_3:SetTarget(STATION_VKS);
  Anim_3:InitMovie(1, TRUE, 2);
  Anim_3:AddPosKey(STATION_VKS:GetPosition()+Vector3(150, 40, -100), 0);
  Anim_3:Play();
  Anim_3:AttachEscapeScript("Station_Blow");
  Anim_3:AttachAction("Station_Blow");
end;


--info:top=750,left=200,allvis=0;
function Reaction()
  local curPos = ship_hem1_1:GetPosition();
  if (VectorLength(mothership:GetPosition(), curPos)<171) then
    flight_hem1:SetChaseDistance(300);
    flight_hem1:Attack(mothership);
    flight_hem2:Escort(ship_hem4_1);
    flight_hem3:Escort(ship_hem4_1);
    flight_hem4:Move(STATION_VKS:GetPosition());
  else
    flight_hem4:Move(STATION_VKS:GetPosition());
    flight_hem2:SetChaseDistance(300);
    flight_hem2:Move(curPos+Vector3(-30, 20, 30));
    flight_hem3:SetChaseDistance(300);
    flight_hem3:Move(curPos+Vector3(-30, 20, -30));
  end;
end;


--info:top=810,left=100,allvis=0;
function Spy_Go()
  NewFlight("Spy_Group", "spy", "Puma_nav1", "VksAce", STATION_VKS:GetPosition()+Vector3(30, -30, -50), Vector3(0.5, 0, -0.5));
  flight_spy:Patrol({Vector3(-50, 0, -200), Vector3(300, -100,-400), Vector3(600, 150,-300), PORTAL_R:GetPosition()+Vector3(-20, 0, 0), PORTAL_R:GetPosition()}, FALSE);
  NewTrigger("f_object_inside", trigger_inside2portal_Condition, Spy_GoOut, flight_spy, PORTAL_R, 20);
  NewTrigger("side_object", TrueCondition, Spy_Founded, PLAYER_TEAM, ship_spy_1, 125);
  DispetcherSpeaker:Message("#L_disp_02", 7, "Viper_Damn");
end;


--info:top=860,left=100,allvis=0;
function Viper_Damn()
  ViperSpeaker:Message("#L_Viper_04", 9);
  AddQuest("quest_16a_04", "#L_Quest_16a_04a", "#L_Quest_16a_04b", QUEST_PROCESS);
end;


--info:top=900,left=160,allvis=0;
function Spy_GoOut()
  Timer_Seq(3, Spy_Out);
end;


--info:top=940,left=160,allvis=0;
function Spy_Out()
  if (trig_SpyDied) then
    trig_SpyDied:Delete();
    trig_SpyDied = NIL;
  end;
  flight_spy:Delete();
  SetQuestState("quest_16a_04", QUEST_FAILED);
end;


--info:top=900,left=40,allvis=0;
function Spy_Founded()
  trig_SpyDied = NewTrigger("f_died", TrueCondition, Spy_Died, flight_spy);
  LastochkaSpeaker:Message("#L_Lastochka_02", 4, "Viper_KillHim");
end;


--info:top=940,left=40,allvis=0;
function Viper_KillHim()
  ViperSpeaker:Message("#L_Viper_05", 5);
  new_SetGroupRelations("Spy_Group", PLAYER_GROUP, "W");
end;


--info:top=970,left=90,allvis=0;
function Spy_Died()
  SetQuestState("quest_16a_04", QUEST_DONE);
end;


--info:top=880,left=250,allvis=0;
function doneMission()
  Delete_Triggers();
  if (trig_HederDied) then
    HederSpeaker:Message("#L_Heder_05", 9, "Heder_GoOut");
  end;
end;


--info:top=690,left=720,allvis=0;
function failedMission()
  if (trig_HederDied) then
    HederSpeaker:Message("#L_Heder_06", 6, "Anim_StationBlow");
  else
    Timer_Seq(1, Anim_StationBlow);
  end;
end;


--info:top=730,left=720,allvis=0;
function Anim_StationBlow()
  Delete_Triggers();
  Timer_Seq(5, Station_Need_Blow);
  Anim_4 = CreateMovie(_TARGETCAM);
  Anim_4:SetLength(10);
  Anim_4:SetInterpolate(_LINEAR);
  Anim_4:SetTarget(STATION_VKS);
  Anim_4:InitMovie(1, TRUE, 2);
  Anim_4:AddPosKey(STATION_VKS:GetPosition()+Vector3(150, 40, -100), 0);
  Anim_4:Play();
  Anim_4:AttachEscapeScript("Station_Blow");
  Anim_4:AttachAction("Station_Blow");
end;


--info:top=1010,left=150,allvis=0;
function Heder_GoOut()
  StopHere();
  if (trig_HederDied) then
    local pos = ship_hed1_1:GetPosition();
    local orn = ship_hed1_1:GetVectorOrientation();
    local glow_orn = ship_hed1_1: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);
    flight_hed1:Move(pos+orn);
    ship_hed1_1:PlayFX("Portal Out.shfx");
    Timer_Seq(5, Heder_Out);
  end;
end;


--info:top=1050,left=150,allvis=0;
function Heder_Out()
  if (trig_HederDied) then
    trig_HederDied:Delete();
    flight_hed1:Delete();
    if not(STATION_VKS:IsAlive()) then
      if (flight_hed2:IsAlive()) then flight_hed2:Delete(); end;
      if (flight_hed3:IsAlive()) then flight_hed3:Delete(); end;
      if (flight_hed4:IsAlive()) then flight_hed4:Delete(); end;
      if (flight_hed7:IsAlive()) then flight_hed7:Delete(); end;
      if (flight_hed8:IsAlive()) then flight_hed8:Delete(); end;
      if (flight_hem1:IsAlive()) then flight_hem1:Delete(); end;
      if (flight_hem2:IsAlive()) then flight_hem2:Delete(); end;
      if (flight_hem3:IsAlive()) then flight_hem3:Delete(); end;
      if (flight_hem4:IsAlive()) then flight_hem4:Delete(); end;
    elseif (lAfraid==1) then
      if (flight_hed2:IsAlive()) then flight_hed2:Delete(); end;
      if (flight_hed3:IsAlive()) then flight_hed3:Delete(); end;
      if (flight_hed4:IsAlive()) then flight_hed4:Delete(); end;
      if (flight_hed5:IsAlive()) then flight_hed5:Delete(); end;
      if (flight_hed6:IsAlive()) then flight_hed6:Delete(); end;
      if (flight_hed7:IsAlive()) then flight_hed7:Delete(); end;
      if (flight_hed8:IsAlive()) then flight_hed8:Delete(); end;
      if (flight_hem1:IsAlive()) then flight_hem1:Delete(); end;
      if (flight_hem2:IsAlive()) then flight_hem2:Delete(); end;
      if (flight_hem3:IsAlive()) then flight_hem3:Delete(); end;
      if (flight_hem4:IsAlive()) then flight_hem4:Delete(); end;
    end;
  end;
end;


--info:top=850,left=680,allvis=0;
function Heder_Died()
  lMission = 0;
  if not(trig_HederDied) then
    SetQuestLabel("Heder","died");
  end;
  local flight = ViperSpeaker:GetFlight();
  Viper_Group:RemoveFlight(flight);
  Player_Group:AddFlight(flight);
  flight:Escort(mothership);
  if (STATION_VKS:IsAlive()) then
    if (GetQuestState("quest_16a_03")==QUEST_PROCESS) then SetQuestState("quest_16a_03", QUEST_DONE); end;
    StartDialog("Dialog3");
  else
    if (GetQuestState("quest_16a_03")==QUEST_PROCESS) then SetQuestState("quest_16a_03", QUEST_FAILED); end;
    StartDialog("Dialog4");
  end; 
end;


--info:top=890,left=750,allvis=0;
function GoTo_Earth()
  AddQuest("quest_16a_05", "#L_Quest_16a_05a", "#L_Quest_16a_05b", QUEST_PROCESS);
  Timer_Seq(3, Viper_What);
  Timer_Seq(1, autosaveMission);
end;


--info:top=930,left=750,allvis=0;
function Viper_What()
  ViperSpeaker:Message("#L_Viper_06", 8);
end;


--info:top=930,left=640,allvis=0;
function GoTo_Simeiz()
  SetQuestLabel("XT86permit","yes");
  SetQuestLabel("GideonPermit", "yes");
  AddQuest("quest_16aO_01", "#L_Quest_16aO_01a", "#L_Quest_16aO_01b", QUEST_PROCESS);
  Timer_Seq(1, autosaveMission);
end;


--info:top=970,left=690,allvis=0;
function autosaveMission()
  AddPost("book016a_1");
  AutoSave("#AS_mission_16_1");
end;


--info:top=110,left=410,allvis=0;
function StartInitMission()
  VksTeam = CreateTeam("Vks");

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

  Player_Group = MothershipPilot:GetGroup();
  Heder_Group = VksTeam:CreateGroup("Heder_Group");
  Viper_Group = VksTeam:CreateGroup("Viper_Group");
  Spy_Group = VksTeam:CreateGroup("Spy_Group");
  power_rel_1 = {Player_Group, Heder_Group, Spy_Group, Viper_Group, VksGroup_1};
  rel_Neutral = {{"N"},
                 {"N","N"},
                 {"F","N","N"},
                 {"F","N","N", "F"}};
  rel_HvsV = {{"X"},
              {"X","X"},
              {"X","W","X"},
              {"X","W","X", "X"}};
  rel_PlvsAll = {{"W"},
                 {"W","X"},
                 {"W","X","X"},
                 {"W","X","X", "X"}};
  new_SetAllGroupRelations(rel_Neutral, power_rel_1);

  HeroSpeaker = GetPilot("Hero");
  SedoySpeaker = GetPilot("Sedoy");
  ValidolSpeaker = GetPilot("Validol");
  ViperSpeaker = GetPilot("Viper");
  KalemSpeaker = GetPilot("Kalem");
  LastochkaSpeaker = GetPilot("Lastochka2");
  VksSpeaker = CreatePilot("VksAce");
  DispetcherSpeaker = CreatePilot("DispetcherVKS");
  HederSpeaker = CreatePilot("AdmiralVKS_Heder");

  flight_vks02:Delete();
  NewVksFlight("Heder_Group", "vks02", Vector3(404, 0, -304), 2, 16, Vector3(0, 0, 1));
  flight_vks02:Patrol({TRADE_STATION:GetPosition()+Vector3(200,0,0),TRADE_STATION:GetPosition()+Vector3(0,0,200)}, TRUE);
  flight_vks07:Delete();
  NewVksFlight("Heder_Group", "vks07", Vector3(-516, 0, -272), 1, 15, Vector3(0, 0, 1));
  flight_vks07:Patrol({PORTAL_L:GetPosition()+Vector3(100,0,-100),PORTAL_D:GetPosition()+Vector3(-100,0,100)}, TRUE);
  flight_vks09:Delete();
  NewVksFlight("Heder_Group", "vks09", Vector3(-228, 0, -352), 1, 11, Vector3(1, 0, -1));
  flight_vks09:Patrol({Vector3(-136,0,-468),Vector3(-20,0,-468),Vector3(-80,0,-612),Vector3(-172,0,-552)}, TRUE);
  flight_vks11:Delete();
  NewVksFlight("Heder_Group", "vks11", Vector3(312, 0, 760), 1, 12, Vector3(0, 0, -1));
  flight_vks11:Patrol({Vector3(376,0,524),Vector3(156,0,296),Vector3(-20,0,800)}, TRUE);
  flight_vks13:Delete();
  NewVksFlight("Heder_Group", "vks13", Vector3(-648, 0, 528), 1, 13, Vector3(0, 0, -1));
  flight_vks13:Patrol({Vector3(-692,0,276),Vector3(-304,0,480),Vector3(-724,0,696)}, TRUE);
  flight_vks16:Delete();
  NewVksFlight("Heder_Group", "vks16", Vector3(68, 0, -768), 2, 13, Vector3(1, 0, 1));
  flight_vks16:Patrol({Vector3(188,0,-672),Vector3(752,0,-276),Vector3(696,0,-584),Vector3(116,0,-896)}, TRUE);

  lMission = 1;

  local NShop_Vks_sst = {		-- количество каждого предмета в магазине
	{0,0,12,6,0,0,12,9,		-- HC,VC guns
	0,12,9,3,0,12,6,			-- LC,PLC
	0,9,6,12,9,6,			-- PC,RG
	0,0,15,9,0,18,12,6,		-- THC,TVC
	0,9,9,3,12,9,6,			-- TLC,TPLC
	0,9,6,0,9,3,			-- TPC,TRG
	0,9,12,6,0,9,12,6,3,0,0,		-- CG,AC
	9,6,3,0,0,0,0,0,0,0,0,0},	-- GK
	{0,0,9,6,0,0,12,9,0,9,12,9,	-- LRS,ECM,AMS systems
	0,6,3,9,6,0,12,9,6,		-- Stealth,LRRNB
	0,0,18,9,0,6,9,6,		-- RBot
	0,0,12,9,3,0,12,3,9,6,6,6,	-- Sh,Eng,Man
	0,18,12,9,6,0,12,6,		-- Gun,ALS
	0,9,6,6,0,9,9,6,9,3,		-- BS_
	0,9,6,3,0,12,9,6,		-- RBot,Sh
	6,3,6,6,3,6,0,0,6,3},		-- Eng,Gun,ALS
	{0,27,21,12,0,45,30,18,36,36,24,18,	-- SM,SRM,LRM rockets
	0,18,12,30,21,15},			-- T,MIRV
	{0,0,0,0,6,		-- 1
	6,0,6,6,6,		-- 2
	12,9,0,0,0,0,0,		-- 3
	12,9,6,9,0,0,0,0,	-- 4
	6,0,0,0,0,0}};		-- 5 level ships
  CreateShop("is");
  AddAllToShops({"is"}, NShop_Vks_sst, 90, 0.0, 0.0);
  UpdateTradeSystem(TRUE);
  ChangeFuncDocking("IS_Dialog", STATION_VKS);

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

  trig_AttackViper = NewTrigger("reps", TrueCondition, Attack_Somebody, Player_Group, Viper_Group, RELATION_WAR);
  trig_AttackSB = NewTrigger("reps", TrueCondition, Attack_Somebody, Player_Group, VksGroup_1, RELATION_WAR);

-- вылет мазершипа
  CreatePortalGlow(TRADE_STATION:GetPosition()+Vector3(200, 0, -200), Vector3(0, 0, 1), 7);
  mothership:SetPosition(TRADE_STATION:GetPosition()+Vector3(200, 0, -200));
  mothership:SetOrientation(CreateQuaternionFromVector3(Vector3(0, 0, 1)));
  mothership:PlayFX("Portal Out.shfx");
  mothership:ApplyImpulse(15, Vector3(0, 0, 1));
  FocusOn({mothership}, Vector3(0.4067,0.4,-0.9135), 60);

  AddPost("news016_1");
  AddPost("news016_5");
  AddPost("news016_2");
  AddPost("news016_6");
  AddPost("news016_3");
  AddPost("news016_7");
  AddPost("news016_4");
end;


--info:top=130,left=590,allvis=0;
function IS_Dialog()
  MothershipFlight:Stop();
  SetTraderFlag(TRUE);
  TradeStationDlg("is");
end;


--info:top=180,left=410,allvis=0;
function My_TeleporterAction()
  if (lMission==1) then
    OutputToScreenLog("#L_sys_01", 5);
    Timer_Seq(5, fin_failed);
  elseif (STATION_VKS~=NIL) and (STATION_VKS:IsAlive()) then
    SetQuestState("quest_16a_05", QUEST_DONE);
    dest_location = "earth";
    CustomTeleportate("nj2", dest_location, "long_jump");
  else
    TeleportToLocation(dest_location);
  end;
end;


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


--info:top=180,left=580,allvis=0;
function Attack_Somebody()
  if (trig_AttackViper) then
    trig_AttackViper:Delete();
    trig_AttackViper = NIL;
  end;
  if (trig_AttackSB) then
    trig_AttackSB:Delete();
    trig_AttackSB = NIL;
  end;
  ViperSpeaker:Message("#L_Viper_07", 5, "Lastochka_Damn");
  Remove_Pers_FromBase();
  local ornv = mothership:GetVectorOrientation();
-- удаляем базу из группы игрока
  MothershipPilot = mothership:GetPilot(); 
  MothershipFlight = MothershipPilot:GetFlight(); 
  Player_Group:RemoveFlight(MothershipFlight);
  Viper_Group:AddFlight(MothershipFlight);
-- создаем новое звено для персов игрока 
  local SFlight = SedoySpeaker:GetFlight();
  Player_Group:RemoveFlight(SFlight);
  Viper_Group:AddFlight(SFlight);
  SFlight:Escort(mothership);
  local VFlight = ValidolSpeaker:GetFlight();
  Player_Group:RemoveFlight(VFlight);
  Viper_Group:AddFlight(VFlight);
  VFlight:Escort(mothership);
  local KFlight = KalemSpeaker:GetFlight();
  Player_Group:RemoveFlight(KFlight);
  Viper_Group:AddFlight(KFlight);
  KFlight:Escort(mothership);
  local LFlight = LastochkaSpeaker:GetFlight();
  Player_Group:RemoveFlight(LFlight);
  Viper_Group:AddFlight(LFlight);
  LFlight:Escort(mothership);
-- теперь общий стоп и выставляем отношения
  MothershipFlight:Stop();
  Stop_Player_Pilots();
  new_SetAllGroupRelations(rel_PlvsAll, power_rel_1);
  trig_HeroDied = NewTrigger("died", TrueCondition, Hero_Died, HeroSpeaker:GetShip());
end;


--info:top=210,left=620,allvis=0;
function Remove_Pers_FromBase()
  if (HeroSpeaker:IsInBase()) or (HeroSpeaker:IsBasePilot()) then
    local ship = CreateCarcass("EvilEye_pl1", mothership:GetPosition()+Vector3(20, -20, -20), Vector3(1, 0, 0));
    ship:AssignPilot(HeroSpeaker);
    local flight = Player_Group:CreateFlight("tmpFlightH");
    flight:AddShip(ship);
  end;
  if (SedoySpeaker:IsInBase()) or (SedoySpeaker:IsBasePilot()) then
    local ship = CreateCarcass("EvilEye_pl1", mothership:GetPosition()+Vector3(20, -20, 20), Vector3(1, 0, 0));
    ship:AssignPilot(SedoySpeaker);
    local flight = Player_Group:CreateFlight("tmpFlightS");
    flight:AddShip(ship);
  end;
  if (ValidolSpeaker:IsInBase()) or (ValidolSpeaker:IsBasePilot()) then
    local ship = CreateCarcass("EvilEye_pl1", mothership:GetPosition()+Vector3(40, -20, -20), Vector3(1, 0, 0));
    ship:AssignPilot(ValidolSpeaker);
    local flight = Player_Group:CreateFlight("tmpFlightV");
    flight:AddShip(ship);
  end;
  if (KalemSpeaker:IsInBase()) or (KalemSpeaker:IsBasePilot()) then
    local ship = CreateCarcass("EvilEye_pl1", mothership:GetPosition()+Vector3(30, -20, 20), Vector3(1, 0, 0));
    ship:AssignPilot(KalemSpeaker);
    local flight = Player_Group:CreateFlight("tmpFlightK");
    flight:AddShip(ship);
  end;
  if (LastochkaSpeaker:IsInBase()) or (LastochkaSpeaker:IsBasePilot()) then
    local ship = CreateCarcass("EvilEye_pl1", mothership:GetPosition()+Vector3(30, -20, -20), Vector3(1, 0, 0));
    ship:AssignPilot(LastochkaSpeaker);
    local flight = Player_Group:CreateFlight("tmpFlightL");
    flight:AddShip(ship);
  end;
end;


--info:top=240,left=620,allvis=0;
function Stop_Player_Pilots()
  local flight = HeroSpeaker:GetFlight();
  flight:Stop();
  flight = SedoySpeaker:GetFlight();
  flight:Stop();
  flight = ValidolSpeaker:GetFlight();
  flight:Stop();
  flight = KalemSpeaker:GetFlight();
  flight:Stop();
  flight = LastochkaSpeaker:GetFlight();
  flight:Stop();
end;


--info:top=260,left=580,allvis=0;
function Lastochka_Damn()
  LastochkaSpeaker:Message("#L_Lastochka_03", 3, "Sedoy_Damn");
end;


--info:top=300,left=580,allvis=0;
function Sedoy_Damn()
  SedoySpeaker:Message("#L_Sedoy_03", 8, "Kalem_Damn");
end;


--info:top=340,left=580,allvis=0;
function Kalem_Damn()
  KalemSpeaker:Message("#L_Kalem_02", 8);
end;


--info:top=370,left=540,allvis=0;
function Hero_Died()
  ShowMsgBox("#L_sys_04", "#L_sys_05", fin_failed);
end;


--info:top=70,left=130,allvis=0;
function ACTION_INIT()
  SetQuestLabel("CurrentQuest","16a");
  StartInitMission();

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


--info:top=70,left=570,allvis=0;
function ACTION_LEAVELOCATION()
  QuestActionDone();
  if (dest_location=="earth") then
    QuestStart("mission_18a");
  else
    if (GetQuestState("quest_16a_01")~=QUEST_NOINIT) then RemoveQuest("quest_16a_01"); end;
    if (GetQuestState("quest_16a_02")~=QUEST_NOINIT) then RemoveQuest("quest_16a_02"); end;
    if (GetQuestState("quest_16a_03")~=QUEST_NOINIT) then RemoveQuest("quest_16a_03"); end;
    if (GetQuestState("quest_16a_04")~=QUEST_NOINIT) then RemoveQuest("quest_16a_04"); end;
    QuestChoice("mission_16a_1","altrabon");
    QuestChoice("mission_16a_2","xt86");
    QuestChoice("mission_16a_3","gideon");
  end;

  return TRUE;
end;


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

  return TRUE;
end;
