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

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


--info:top=120,left=80,allvis=0;
function StartMission()
  SedoySpeaker:Message("#L_Sedoy_01", 3, "Uel_Dont_Shoot");
end;


--info:top=160,left=80,allvis=0;
function Uel_Dont_Shoot()
  UelSpeaker:Message("#L_Uel_01", 10);
  AddQuest("quest_14e_03", "#L_Quest_14e_03a", "#L_Quest_14e_03b", QUEST_PROCESS);
  Timer_Seq(1, GoTo_Station);
end;


--info:top=200,left=170,allvis=0;
function GoTo_Station()
  LineToStation = CreateLine(NP_PORTAL_R, np_mine, RGB(20,80,220), .3, TRUE, .2, 1);
  trig_Mine = NewTrigger("inside", TrueCondition, Uel_Explain, mothership, MINE:GetPosition(), 100);
  NewFlight("Uel_Group", "uel1", "EvilEye_ino2", "Uel3", MINE:GetPosition()+Vector3(25, 20, -25), Vector3(1, 0, 0));
end;


--info:top=240,left=170,allvis=0;
function Uel_Explain()
  LineToStation:Delete();
  LineToStation = NIL;
  SetNormalTimeMode();
  SetQuestState("quest_14e_03", QUEST_DONE);
  MothershipFlight:Stop();
--------------------------------------------------------------------------------
--  StartDialog("Dialog1");
  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("Dialog1");
    end;
  else
    StartDialog("Dialog1");
  end; 
end;

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



--info:top=290,left=80,allvis=0;
function Answer_Yes()
  Uel_Group:RemoveFlight(flight_uel1);
  Player_Group:AddFlight(flight_uel1);
  flight_uel1:Escort(mothership);
  AddQuest("quest_14e_04", "#L_Quest_14e_04a", "#L_Quest_14e_04b", QUEST_PROCESS);
  LineToStock = CreateLine(np_mine, np_stock, RGB(20,80,220), .3, TRUE, .2, 1);
  Timer_Seq(1, GoTo_Stock);
end;


--info:top=320,left=20,allvis=0;
function GoTo_Stock_Start()
  LineToStock = CreateLine(NP_PORTAL_R, np_stock, RGB(20,80,220), .3, TRUE, .2, 1);
  Timer_Seq(1, GoTo_Stock);
end;


--info:top=350,left=80,allvis=0;
function GoTo_Stock()
  trig_Stock = NewTrigger("inside", TrueCondition, Near_Stock, mothership, STOCK:GetDockingPosition(), 25);
  SetInitTeamRelation("Berserk", PLAYER_TEAM, RELATION_FRIEND);
  new_SetGroupRelations("Uel_Group", PLAYER_GROUP, "F");
  new_SetGroupRelations("BerserkGroup_1", PLAYER_GROUP, "F");
end;


--info:top=390,left=80,allvis=0;
function Near_Stock()
  LineToStock:Delete();
  LineToStock = NIL;
  SetQuestState("quest_14e_04", QUEST_DONE);
  trig_StockOut = NewTrigger("outside", TrueCondition, Last_Dialog, mothership, STOCK:GetDockingPosition(), 50);
end;


--info:top=430,left=80,allvis=0;
function Last_Dialog()
  SetNormalTimeMode();
  MothershipFlight:Stop();
  StartDialog("Dialog2");
end;


--info:top=530,left=80,allvis=0;
function finMission()
  Timer_Seq(1, autosaveMission);
end;


--info:top=570,left=80,allvis=0;
function autosaveMission()
  Timer_Seq(1, Anim_GoTo);
  AutoSave("#AS_mission_14_2");
end;


--info:top=610,left=80,allvis=0;
function Anim_GoTo()
  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_1 = CreateMovie(_TARGETCAM);
  Anim_1:SetLength(5);
  Anim_1:SetInterpolate(_LINEAR);
  Anim_1:SetTarget(mothership);
  Anim_1:InitMovie(1, TRUE, 1);
  Anim_1: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_1:Play();
  Anim_1:AttachEscapeScript("GoTo_NK");
  Anim_1:AttachAction("GoTo_NK");
end;


--info:top=650,left=80,allvis=0;
function GoTo_NK()
  AddPost("book014e_1");
  CustomTeleportate("zebos", "nk", "long_jump");
end;


--info:top=290,left=260,allvis=0;
function Answer_No()
  trig_BerAttack:Delete();
-- выгружаем пилотов с базы
  Remove_Pers_FromBase();
-- меняем базу на новую
  local posn = mothership:GetPosition();
  local orn = mothership:GetOrientation();
  local ornv = mothership:GetVectorOrientation();
  mothership_uel = mothership;
  mothership = SetPlayerMotherShip(ship_merc4_1); 
  Stop_Player_Pilots();
  PortalTrigger_PORTAL_R:SetObject(mothership);
  PortalsActivate_Action();
-- удаляем новую базу из группы игрока
  MothershipPilot = mothership:GetPilot(); 
  MothershipFlight = MothershipPilot:GetFlight(); 
  Player_Group:RemoveFlight(MothershipFlight);
  Merc_Group:AddFlight(MothershipFlight);
-- создаем новое звено для старого мазершипа и копии Уэла 
--  local tmpPilot = CreatePilot("BasePilot");
--  mothership_uel:AssignPilot(tmpPilot);
--  local flight = Uel_Group:CreateFlight("tmpFlightM");
--  flight:AddShip(mothership_uel);
  FocusOn({HeroSpeaker:GetShip(), SedoySpeaker:GetShip(), YanaSpeaker:GetShip(), KalemSpeaker:GetShip()}, ornv, 60);
  mothership_uel:SetPosition(Vector3(-3000, 0, 3000));
  ship_om_1:SetPosition(posn);
  ship_om_1:SetOrientation(orn);
  local UFlight = UelDSpeaker:GetFlight();
  if (UFlight:GetPilotCount()==1) then
    Player_Group:RemoveFlight(UFlight);
    Uel_Group:AddFlight(UFlight);
  else
    local UelDFlight = Uel_Group:CreateFlight("UelDFlight");
    UelDFlight:AddShip(UelDSpeaker:GetShip());
  end;
-- выставляем отношения
  new_SetGroupRelations("Uel_Group", PLAYER_GROUP, "W");
  new_SetGroupRelations("BerserkGroup_1", PLAYER_GROUP, "W");
  trig_HeroDied = NewTrigger("died", TrueCondition, Hero_Died, HeroSpeaker:GetShip());
  SedoySpeaker:Message("#L_Sedoy_02", 3);
  lStartTest = 0;
  AddQuest("quest_14e_06", "#L_Quest_14e_06a", "#L_Quest_14e_06b", QUEST_PROCESS);
  Timer_Seq(1, Need_Leaving);
end;


--info:top=330,left=290,allvis=0;
function Remove_Pers_FromBase()
  if (HeroSpeaker:IsInBase()) or (HeroSpeaker:IsBasePilot()) then
    if (HeroSpeaker:IsBasePilot()) then RemoveBaseDriver(); end;
    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
    if (SedoySpeaker:IsBasePilot()) then RemoveBaseDriver(); end;
    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 (YanaSpeaker:IsInBase()) or (YanaSpeaker:IsBasePilot()) then
    if (YanaSpeaker:IsBasePilot()) then RemoveBaseDriver(); end;
    local ship = CreateCarcass("EvilEye_pl1", mothership:GetPosition()+Vector3(30, -20, -20), Vector3(1, 0, 0));
    ship:AssignPilot(YanaSpeaker);
    local flight = Player_Group:CreateFlight("tmpFlightY");
    flight:AddShip(ship);
  end;
  if (KalemSpeaker:IsInBase()) or (KalemSpeaker:IsBasePilot()) then
    if (KalemSpeaker:IsBasePilot()) then RemoveBaseDriver(); end;
    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 (UelDSpeaker:IsInBase()) or (UelDSpeaker:IsBasePilot()) then
    if (UelDSpeaker:IsBasePilot()) then RemoveBaseDriver(); end;
    local ship = CreateCarcass("EvilEye_ino2", mothership:GetPosition()+Vector3(40, -20, -20), Vector3(1, 0, 0));
    ship:AssignPilot(UelDSpeaker);
    local flight = Player_Group:CreateFlight("tmpFlightU");
    flight:AddShip(ship);
  end;
end;


--info:top=360,left=290,allvis=0;
function Stop_Player_Pilots()
  local flight = HeroSpeaker:GetFlight();
  flight:Stop();
  local flight = SedoySpeaker:GetFlight();
  flight:Stop();
  local flight = YanaSpeaker:GetFlight();
  flight:Stop();
  local flight = KalemSpeaker:GetFlight();
  flight:Stop();
  local flight = UelDSpeaker:GetFlight();
  flight:Stop();
end;


--info:top=360,left=510,allvis=0;
function Hero_Died()
  ShowMsgBox("#L_sys_01", "#L_sys_02", finFailed);
end;


--info:top=650,left=510,allvis=0;
function finFailed()
  EndGame("gamefailed");
end;


--info:top=380,left=260,allvis=0;
function Need_Leaving()
  if (lStartTest==0) then
    trig_Out = NewTrigger("timer", TrueCondition, Test_Leaving, 30);
    lTest = 2;
    lStartTest = 1;
    Timer_Seq(10, Need_Leaving);
  elseif (lTest==2) then
    Timer_Seq(10, Need_Leaving);
  elseif (lTest==0) then
    lStartTest = 0;
    Timer_Seq(1, Need_Leaving);
  else
    Timer_Seq(1, Already_Leaving);
  end;
end;


--info:top=410,left=350,allvis=0;
function Test_Leaving()
  trig_NearBerserk = NewTrigger("side_object", TrueCondition, False_Leaving, "Berserk", HeroSpeaker:GetShip(), 200);
  trig_LeavingDone = NewTrigger("timer", TrueCondition, True_Leaving, 30);
end;


--info:top=450,left=370,allvis=0;
function False_Leaving()
  trig_NearBerserk:Delete();
  trig_LeavingDone:Delete();
  lTest = 0;
end;


--info:top=470,left=330,allvis=0;
function True_Leaving()
  trig_NearBerserk:Delete();
  trig_LeavingDone:Delete();
  lTest = 1;
end;


--info:top=490,left=260,allvis=0;
function Already_Leaving()
  SetQuestState("quest_14e_06", QUEST_DONE);
  AddQuest("quest_14e_07", "#L_Quest_14e_07a", "#L_Quest_14e_07b", QUEST_PROCESS);
  SetNormalTimeMode();
  HeroSpeaker:Message("#L_Hero_01", 14, "What_Next_1");
end;


--info:top=530,left=260,allvis=0;
function What_Next_1()
  HeroSpeaker:Message("#L_Hero_02", 16, "What_Next_2");
end;


--info:top=570,left=260,allvis=0;
function What_Next_2()
  HeroSpeaker:Message("#L_Hero_03", 9);
  NewTrigger("inside", TrueCondition, Ship_Founded, HeroSpeaker:GetShip(), xyz_trade+Vector3(10, -10, -150), 100);
end;


--info:top=610,left=260,allvis=0;
function Ship_Founded()
  SetNormalTimeMode();
  StartDialog("Dialog3");
end;


--info:top=650,left=260,allvis=0;
function New_Mothership()
  SetQuestState("quest_14e_07", QUEST_DONE);
  Remove_Pers_Flights();
  Merc_Group:RemoveFlight(MothershipFlight);
  Player_Group:AddFlight(MothershipFlight);
  GetShipName(mothership);
  Add_Pers_Flights();
  trig_HeroDied:Delete();
  AddQuest("quest_14e_05", "#L_Quest_14e_05a", "#L_Quest_14e_05b", QUEST_PROCESS);
end;


--info:top=680,left=300,allvis=0;
function Remove_Pers_Flights()
  if (HeroSpeaker:IsCanSayReplica()) then
    HeroFlight = HeroSpeaker:GetFlight();
    Player_Group:RemoveFlight(HeroFlight);
  end;
  if (SedoySpeaker:IsCanSayReplica()) and (SedoySpeaker:GetSide()==PLAYER_TEAM) then
    SedoyFlight = SedoySpeaker:GetFlight();
    Player_Group:RemoveFlight(SedoyFlight);
  end;
  if (YanaSpeaker:IsCanSayReplica()) and (YanaSpeaker:GetSide()==PLAYER_TEAM) then
    YanaFlight = YanaSpeaker:GetFlight();
    Player_Group:RemoveFlight(YanaFlight);
  end;
  if (KalemSpeaker:IsCanSayReplica()) and (KalemSpeaker:GetSide()==PLAYER_TEAM) then
    KalemFlight = KalemSpeaker:GetFlight();
    Player_Group:RemoveFlight(KalemFlight);
  end;
end;


--info:top=710,left=300,allvis=0;
function Add_Pers_Flights()
  if (HeroSpeaker:IsCanSayReplica()) then
    HeroFlight = HeroSpeaker:GetFlight();
    Player_Group:AddFlight(HeroFlight);
  end;
  if (SedoySpeaker:IsCanSayReplica()) and (SedoySpeaker:GetSide()~=PLAYER_TEAM) then
    SedoyFlight = SedoySpeaker:GetFlight();
    Player_Group:AddFlight(SedoyFlight);
  end;
  if (YanaSpeaker:IsCanSayReplica()) and (YanaSpeaker:GetSide()~=PLAYER_TEAM) then
    YanaFlight = YanaSpeaker:GetFlight();
    Player_Group:AddFlight(YanaFlight);
  end;
  if (KalemSpeaker:IsCanSayReplica()) and (KalemSpeaker:GetSide()~=PLAYER_TEAM) then
    KalemFlight = KalemSpeaker:GetFlight();
    Player_Group:AddFlight(KalemFlight);
  end;
end;


--info:top=120,left=350,allvis=0;
function StartInitMission()
  UelTeam = CreateTeam("Berserk");
  MercTeam = CreateTeam("Merc");

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

  Player_Group = MothershipPilot:GetGroup();
  Uel_Group = UelTeam:CreateGroup("Uel_Group");
  Merc_Group = MercTeam:CreateGroup("Merc_Group");
  new_SetGroupRelations("Uel_Group", PLAYER_GROUP, "N");
  new_SetGroupRelations("Merc_Group", PLAYER_GROUP, "N");
  new_SetGroupRelations("Uel_Group", "BerserkGroup_1", "F");

  HeroSpeaker = GetPilot("Hero");
  SedoySpeaker = GetPilot("Sedoy");
  YanaSpeaker = GetPilot("Yana");
  UelDSpeaker = GetPilot("Uel2");
  KalemSpeaker = GetPilot("Kalem");
  UelSpeaker = CreatePilot("Uel");
  UelTSpeaker = CreatePilot("Uel3");

-- брошенные транспорты
  NewFlight("Merc_Group", "merc1", "HMQueen_ino3", "BasePilot", xyz_trade+Vector3(-50, -10, -150), Vector3(0, 0, 1));
  NewFlight("Merc_Group", "merc2", "Arba_ino", "BasePilot", xyz_trade+Vector3(-30, -10, -150), Vector3(0, 0, 1));
  NewFlight("Merc_Group", "merc3", "HMQueen_ino3", "BasePilot", xyz_trade+Vector3(-10, -10, -150), Vector3(0, 0, 1));
  NewFlight("Merc_Group", "merc4", "Mothership_heretic_2", "BasePilot", xyz_trade+Vector3(10, -10, -150), Vector3(0, 0, 1));
  NewFlight("Merc_Group", "merc5", "Arba_ino", "BasePilot", xyz_trade+Vector3(30, -10, -150), Vector3(0, 0, 1));
  NewFlight("Merc_Group", "merc6", "HMQueen_ino3", "BasePilot", xyz_trade+Vector3(50, -10, -150), Vector3(0, 0, 1));

  NewFlight("Uel_Group", "om", "Manticore_ino1_1", "Uel", Vector3(3000, 0, 3000), Vector3(1, 0, 0));
-- триггер на атаку берсерков
  trig_BerAttack = NewTrigger("reps", TrueCondition, Attack_Berserk, Player_Group, BerserkGroup_1, RELATION_WAR);
end;


--info:top=160,left=350,allvis=0;
function Attack_Berserk()
  UelSpeaker:Message("#L_Uel_02", 4);
  Timer_Seq(1, Answer_No);
end;


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

  if (GetQuestState("quest_14e_01")==QUEST_PROCESS) then
    SetQuestState("quest_14e_01", QUEST_DONE);
    Timer_Seq(3, StartMission);
  elseif (GetQuestState("quest_14e_03")==QUEST_PROCESS) then
    Timer_Seq(1, GoTo_Station);
  elseif (GetQuestState("quest_14e_04")==QUEST_PROCESS) then
    Timer_Seq(1, GoTo_Stock_Start);
  end;
  return TRUE;
end;


--info:top=80,left=570,allvis=0;
function ACTION_LEAVELOCATION()
  if (tmpNull) then tmpNull:Delete(); end;
  if (LineToStation) then LineToStation:Delete(); end;
  if (LineToStock) then LineToStock:Delete(); end;
  if (GetQuestState("quest_14e_03")~=QUEST_PROCESS) and (GetQuestState("quest_14e_04")~=QUEST_PROCESS) then
    QuestActionDone();
    if (GetGroupRelation("Uel_Group", PLAYER_GROUP)==RELATION_WAR) then
      SetInitTeamRelation("Berserk", PLAYER_TEAM, RELATION_WAR);
      SetInitTeamRelation("Inoco", PLAYER_TEAM, RELATION_WAR);
      SetQuestLabel("InocoPermit","no");
      SetQuestLabel("ZebosPermit","no");
      QuestStart("mission_14e_3");
    else
      QuestStart("mission_15e");
    end;
  end;

  return TRUE;
end;


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

  return TRUE;
end;
