------------------------------------------------------------
--     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=230,allvis=0;
function StartMission()
  SetQuestState("quest_8d_01", QUEST_DONE);
  if (GetQuestState("quest_8d_03")==QUEST_PROCESS) then
    StartDialog("Dialog5");
  else
    SetQuestLabel("QuestPoint", "2");
    StartDialog("Dialog6");
  end;
end;


--info:top=160,left=230,allvis=0;
function GoTo_Heruvim()
  LineToHeruvim = CreateLine(NP_PORTAL_R, np_Pirate, RGB(20,80,220), .3, TRUE, .2, 1);
  trig_Sobor = NewTrigger("side_inside", TrueCondition, Packet_Blow, PLAYER_TEAM, SOBOR:GetPosition(), 150);
  trig_Trade = NewTrigger("side_inside", TrueCondition, Packet_Blow, PLAYER_TEAM, station01:GetPosition(), 100);
  NewTrigger("inside", TrueCondition, Tell_Packet, mothership, Station03:GetPosition(), 140);
  NewPirateFlight("PiratesGroup_1", "smg1", Station03:GetPosition()+Vector3(50, -10, -40), 3, 8, Vector3(1, 0, 0));
end;


--info:top=200,left=230,allvis=0;
function Tell_Packet()
  SetNormalTimeMode();
  MothershipFlight:Stop();
  LineToHeruvim:Delete();
  LineToHeruvim = NIL;
  trig_Blow:Delete();
  trig_Sobor:Delete();
  trig_Trade:Delete();
  StartDialog("Dialog7");
end;


--info:top=240,left=230,allvis=0;
function After_Tell()
  SetQuestLabel("QuestPoint", "2");
  SetQuestState("quest_8d_03", QUEST_DONE);
  RemovePost("mail_008d_3");
  LineToSobor = CreateLine(np_Pirate, np_sobor, RGB(20,80,220), .3, TRUE, .2, 1);
  AddQuest("quest_8d_04", "#L_Quest_8d_04a", "#L_Quest_8d_04b", QUEST_PROCESS);
  NewTrigger("inside", TrueCondition, Alex_Dialog, mothership, SOBOR:GetPosition(), 120);
  Timer_Seq(120, Smuggler_Go);
  LastochkaSpeaker:Message("#L_Lastochka_01", 7, "Hero_Answer");
end;


--info:top=280,left=230,allvis=0;
function Hero_Answer()
  HeroSpeaker:Message("#L_Hero_01", 4);
end;


--info:top=290,left=100,allvis=0;
function Smuggler_Go()
  for i=1,getn(ShopModules),1 do
    if (10 > RAND(100)) then ship_smg1_1:AddModuleToInventory(ShopModules[i],1);  end;
  end;
  NewPirateFlight("PiratesGroup_1", "smg2", Station03:GetPosition()+Vector3(20, 6, 15), 1, 17, Vector3(0.5, 0, 0.5));
  NewPirateFlight("PiratesGroup_1", "smg3", Station03:GetPosition()+Vector3(-20, 6, 5), 1, 15, Vector3(-0.66, 0, 0.33));
  flight_smg2:Escort(ship_smg1_1);
  flight_smg3:Escort(ship_smg1_1);
  flight_smg1:Move(Vector3(3000, 250, -3000));
end;


--info:top=160,left=360,allvis=0;
function GoTo_Station()
  LineToSobor = CreateLine(NP_PORTAL_R, np_sobor, RGB(20,80,220), .3, TRUE, .2, 1);
  AddQuest("quest_8d_04", "#L_Quest_8d_04a", "#L_Quest_8d_04b", QUEST_PROCESS);
  flight_teml1:Escort(mothership);
  flight_teml2:Escort(mothership);
  local pos_ms = mothership:GetPosition();
  local pos_tg = SOBOR:GetPosition();
  local center = Vector3((pos_ms.x+pos_tg.x)/2,(pos_ms.y+pos_tg.y)/2,(pos_ms.z+pos_tg.z)/2);
  local dist = VectorLength(pos_ms, pos_tg)/2;
  trig_Escape = NewTrigger("outside", TrueCondition, Try_Escape_1, mothership, center, dist+25);
  trig_Approach = NewTrigger("inside", TrueCondition, Reinit_Approch_1, mothership, center, dist-25);
--------------------------------------------------------------------------------
--  trig_AlexDialog = NewTrigger("inside", TrueCondition, Alex_Dialog, mothership, SOBOR:GetPosition(), 120);
  trig_AlexDialog = NewTrigger("inside", TrueCondition, count_or_go_on, mothership, SOBOR:GetPosition(), 120);
--------------------------------------------------------------------------------
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
      Alex_Dialog();
    end;
  else
    Alex_Dialog();
  end; 
end;

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


--info:top=230,left=400,allvis=0;
function Try_Escape_1()
  if (trig_Escape) then trig_Escape:Delete(); end;
  if (trig_Approach) then trig_Approach:Delete(); end;
  trig_AlexDialog:Delete();
  new_SetGroupRelations("TamplierGroup_1", PLAYER_GROUP, "W");
  Anim_D = CreateMovie(_TARGETCAM);
  Anim_D:SetLength(6);
  Anim_D:SetInterpolate(_LINEAR);
  Anim_D:SetTarget(mothership);
  Anim_D:InitMovie(1, TRUE, 1);
  Anim_D:AddPosKey(mothership:GetPosition()+Vector3(60, 30, -30), 0);
  Anim_D:AddScriptKey("MShip_Blow", 4);
  Anim_D:Play();
  Anim_D:AttachEscapeScript("MShip_Blow");
  Anim_D:AttachAction("MShip_Blow");
  TamlierSpeaker:Message("#L_templar_01", 4);
end;


--info:top=270,left=400,allvis=0;
function MShip_Blow()
  if (mothership:IsAlive()) then
    mothership:Blow();
  end;
end;


--info:top=190,left=400,allvis=0;
function Reinit_Approch_1()
  local v_target = SOBOR:GetPosition();
  local v_ship = mothership:GetPosition();
  local v_center = Vector3((v_target.x+v_ship.x)/2,(v_target.y+v_ship.y)/2,(v_target.z+v_ship.z)/2);
  local distance = VectorLength(v_target,v_ship);
  trig_Escape:Delete();
  trig_Escape = NIL;
  trig_Approach:Delete();
  trig_Approach = NIL;
  if (distance>120) then
    distance = distance/2;
    trig_Escape = NewTrigger("outside", TrueCondition, Try_Escape_1, mothership, v_center, distance+25);
    trig_Approach = NewTrigger("inside", TrueCondition, Reinit_Approch_1, mothership, v_center, distance-25);
  end;
end;


--info:top=320,left=290,allvis=0;
function Alex_Dialog()
  SetNormalTimeMode();
  MothershipFlight:Stop();
  LineToSobor:Delete();
  LineToSobor = NIL;
  if (trig_Escape) then trig_Escape:Delete(); end;
  if (trig_Approach) then trig_Approach:Delete(); end;
  NewTemplFlight("TamplierGroup_1", "temlA1", PORTAL_R:GetPosition()+Vector3(-60, 10, -60), 3, 13, Vector3(0.5, 0, -0.5));
  NewTemplFlight("TamplierGroup_1", "temlA2", PORTAL_R:GetPosition()+Vector3(-80, 10, -70), 3, 12, Vector3(0.5, 0, -0.5));
  NewTemplFlight("TamplierGroup_1", "temlA3", PORTAL_R:GetPosition()+Vector3(-120, 10, -70), 3, 12, Vector3(0.5, 0, -0.5));
  NewTemplFlight("TamplierGroup_1", "temlA4", PORTAL_R:GetPosition()+Vector3(-60, 20, -60), 1, 19, Vector3(0.5, 0, -0.5));
  NewTemplFlight("TamplierGroup_1", "temlA5", PORTAL_R:GetPosition()+Vector3(-80, 20, -70), 2, 18, Vector3(0.5, 0, -0.5));
  NewTemplFlight("TamplierGroup_1", "temlA6", PORTAL_R:GetPosition()+Vector3(-120, 20, -70), 2, 13, Vector3(0.5, 0, -0.5));
  NewTemplFlight("TamplierGroup_1", "temlA7", PORTAL_R:GetPosition()+Vector3(-90, 20, -70), 2, 19, Vector3(0.5, 0, -0.5));
  flight_temlA4:Escort(ship_temlA1_1);
  flight_temlA5:Escort(ship_temlA1_1);
  flight_temlA6:Escort(ship_temlA2_1);
  flight_temlA7:Escort(ship_temlA3_1);
  flight_teml1:Escort(ship_temlA2_1);
  flight_teml2:Escort(ship_temlA3_1);
  AddPost("news008_2");
  AddPost("news008_4");
  if (GetQuestLabel("Blow")~="Blowed") then
    SetQuestLabel("Blow","Blow");
  end;
  AddPost("news008_5");
  AddPost("news008_7");
  if (GetQuestLabel("Cleric")=="Yes") then
    StartDialog("Dialog8");
  else
    StartDialog("Dialog9");
  end;
end;


--info:top=360,left=290,allvis=0;
function GoOut_Haron()
  mothership:AddModuleToInventory("Escape_PodCleric", 1);
  AddPilotToPlayer(AlexSpeaker);
  LineToPortal = CreateLine(np_sobor, NP_PORTAL_R, RGB(20,80,220), .3, TRUE, .2, 1);
  SetQuestLabel("QuestPoint", "0");
  SetQuestState("quest_8d_04", QUEST_DONE);
  RemovePost("mail_008d_1");
  AddQuest("quest_8d_05", "#L_Quest_8d_05a", "#L_Quest_8d_05b", QUEST_PROCESS);
  NewTrigger("inside", TrueCondition, Alex_Remark, mothership, PORTAL_R:GetPosition(), 150);
  CreateRoute("haron", "kron", "debris2", "debris1", "corrino", "aurora");
end;


--info:top=400,left=290,allvis=0;
function Alex_Remark()
  AlexSpeaker:Message("#L_Alex_01", 6);
end;


--info:top=120,left=90,allvis=0;
function StartInitMis()
  MothershipPilot = mothership:GetPilot();
  MothershipFlight = MothershipPilot:GetFlight();
  
  Player_Group = MothershipPilot:GetGroup();
  myPirateReps = GetTeamRelation("Pirates", PLAYER_TEAM);
  new_SetGroupRelations("TamplierGroup_1", PLAYER_GROUP, "N");
  new_SetGroupRelations("PiratesGroup_1", PLAYER_GROUP, "N");
  
  HeroSpeaker = GetPilot("Hero");
  LastochkaSpeaker = GetPilot("Lastochka");
  SedoySpeaker = GetPilot("Sedoy");
  AlexSpeaker = CreatePilot("Alex");
  TamlierSpeaker = CreatePilot("m8d_Templar");
  
  NewTemplFlight("TamplierGroup_1", "teml1", PORTAL_R:GetPosition()+Vector3(-60, 10, 60), 1, 13, Vector3(0.5, 0, -0.5));
  NewTemplFlight("TamplierGroup_1", "teml2", PORTAL_R:GetPosition()+Vector3(-80, 10, 70), 1, 12, Vector3(0.5, 0, -0.5));
  if (GetQuestState("quest_8d_03")==QUEST_PROCESS) then
    SetQuestLabel("QuestPoint", "1");
    trig_Blow = NewTrigger("timer", TrueCondition, Packet_Blow, 900);
  end;
end;


--info:top=200,left=90,allvis=0;
function Packet_Blow()
  mothership:Blow();
end;


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


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

  lPoint = GetQuestLabel("QuestPoint");
  if (lPoint=="2") then
    Timer_Seq(1, GoTo_Station);
  else
    Timer_Seq(5, StartMission);
  end;
  return TRUE;
end;


--info:top=80,left=570,allvis=0;
function ACTION_LEAVELOCATION()
  if (LineToHeruvim) then LineToHeruvim:Delete(); end;
  if (LineToSobor) then LineToSobor:Delete(); end;
  SetInitTeamRelation("Pirates", PLAYER_TEAM, myPirateReps);
  lPoint = GetQuestLabel("QuestPoint");
  if (lPoint=="1") then
    mothership:Blow();
    Timer_Seq(15, End_Game);
  elseif (lPoint=="0") then
    LineToPortal:Delete();
    QuestActionDone();
  end;

  return TRUE;
end;


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

  return TRUE;
end;
