------------------------------------------------------------
--     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=130,allvis=0;
function StartMission()
  PORTAL_U:Start(5, 0);
  NewFlight("Imu_Group", "imu1", "StoneArrow_tri2", "TriadaSuperAce", PORTAL_U:GetPosition()+Vector3(0, 0, -20), Vector3(0, 0, -1));
  NewTriadaFlight("Imu_Group", "imuT1", PORTAL_U:GetPosition()+Vector3(10, 0, -10), 1, 20, Vector3(0, 0, -1));
  NewTriadaFlight("Imu_Group", "imuT2", PORTAL_U:GetPosition()+Vector3(-10, 0, -10), 1, 20, Vector3(0, 0, -1));
  NewTriadaFlight("Imu_Group", "imuT3", PORTAL_U:GetPosition()+Vector3(0, -10, -10), 1, 19, Vector3(0, 0, -1));
  NewTriadaFlight("Imu_Group", "imuT4", PORTAL_U:GetPosition()+Vector3(0, 10, -10), 2, 19, Vector3(0, 0, -1));
  NewTriadaFlight("Imu_Group", "imuT5", PORTAL_U:GetPosition()+Vector3(8, 8, -10), 2, 19, Vector3(0, 0, -1));
  flight_imuT1:Escort(ship_imu1_1);
  flight_imuT2:Escort(ship_imu1_1);
  flight_imuT3:Escort(ship_imu1_1);
  flight_imuT4:Escort(ship_imu1_1);
  flight_imuT5:Escort(ship_imu1_1);
  ship_imu1_1:ComeToPortal(PORTAL_D);
  NewTrigger("f_object_inside", trigger_inside2portal_Condition, Imu_GoOut, flight_imu1, PORTAL_D, 20);
  NewTrigger("side_object", TrueCondition, Meet_Imu, PLAYER_TEAM, ship_imu1_1, 150);
end;


--info:top=150,left=190,allvis=0;
function Imu_GoOut()
  flight_imu1:Delete();
  if (GetGroupRelation("Imu_Group", PLAYER_GROUP)==RELATION_WAR) then
    Timer_Seq(1, failedMission);
  end;
end;


--info:top=210,left=130,allvis=0;
function Meet_Imu()
  lMission = 2;
  SetNormalTimeMode();
  if (GetQuestState("quest_16b_05")==QUEST_PROCESS) then SetQuestState("quest_16b_05", QUEST_DONE); end;
  if (Player_Group:HavePerk("F_of_Merchant")) then
    StartDialog("Dialog1");
  else
    StartDialog("Dialog2");
  end;
end;


--info:top=270,left=130,allvis=0;
function Attack_Imu()
  new_SetAllGroupRelations(rel_ImuWar, power_rel_1);
  AddQuest("quest_16b_06", "#L_Quest_16b_06a", "#L_Quest_16b_06b", QUEST_PROCESS);
  NewTrigger("f_died", TrueCondition, Imu_Died, flight_imu1);
  NewFlight("Medish_Group", "med1", "Gunslinger_pir3", "m17b_Medish", Vector3(-300, 300, -300), Vector3(1, 0, 0));
  flight_med1:Escort(mothership);
  trig_MedishNear = NewTrigger("f_object_inside", TrueCondition, Medish_Near, flight_med1, mothership, 150);
  trig_TanakaComeOn = NewTrigger("timer", TrueCondition, Tanaka_ComeOn, 60);
end;


--info:top=340,left=170,allvis=0;
function Imu_Died()
  SetQuestState("quest_16b_06", QUEST_DONE);
  count = 0;
  if (flight_imuT1) and (flight_imuT1:IsAlive()) then
    count = count + 1;
    NewTrigger("f_died", DiedCondition, Caravan_Died, flight_imuT1);
  end;
  if (flight_imuT2) and (flight_imuT2:IsAlive()) then
    count = count + 1;
    NewTrigger("f_died", DiedCondition, Caravan_Died, flight_imuT2);
  end;
  if (flight_imuT3) and (flight_imuT3:IsAlive()) then
    count = count + 1;
    NewTrigger("f_died", DiedCondition, Caravan_Died, flight_imuT3);
  end;
  if (flight_imuT4) and (flight_imuT4:IsAlive()) then
    count = count + 1;
    NewTrigger("f_died", DiedCondition, Caravan_Died, flight_imuT4);
  end;
  if (flight_imuT5) and (flight_imuT5:IsAlive()) then
    count = count + 1;
    NewTrigger("f_died", DiedCondition, Caravan_Died, flight_imuT5);
  end;
  if (flight_med1) and (flight_med1:IsAlive()) then
    count = count + 1;
    NewTrigger("f_died", DiedCondition, Caravan_Died, flight_med1);
  end;
  if (trig_MedishNear~=NIL) then
    if (flight_med2) and (flight_med2:IsAlive()) then
      count = count + 1;
      NewTrigger("f_died", DiedCondition, Caravan_Died, flight_med2);
    end;
    if (flight_med3) and (flight_med3:IsAlive()) then
      count = count + 1;
      NewTrigger("f_died", DiedCondition, Caravan_Died, flight_med3);
    end;
    if (flight_med4) and (flight_med4:IsAlive()) then
      count = count + 1;
      NewTrigger("f_died", DiedCondition, Caravan_Died, flight_med4);
    end;
    if (flight_med5) and (flight_med5:IsAlive()) then
      count = count + 1;
      NewTrigger("f_died", DiedCondition, Caravan_Died, flight_med5);
    end;
  end;
  if (count>0) then
    AddQuest("quest_16b_07", "#L_Quest_16b_07a", "#L_Quest_16b_07b", QUEST_PROCESS);
    if (trig_TanakaComeOn) then
      trig_TanakaComeOn:Delete();
      trig_TanakaComeOn = NIL;
      Timer_Seq(5, Tanaka_ComeOn);
      if (flight_med1) and (flight_med1:IsAlive()) and (trig_MedishNear==NIL) then
        Timer_Seq(1, Medish_Damn);
      end;
    else
      Timer_Seq(1, Tanaka_DidIt);
    end;
  else
    if (trig_TanakaComeOn) then
      trig_TanakaComeOn:Delete();
      trig_TanakaComeOn = NIL;
    end;
    Timer_Seq(1, Caravan_Died);
  end;
end;


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


--info:top=340,left=30,allvis=0;
function Medish_Near()
  trig_MedishNear:Delete();
  trig_MedishNear = NIL;
  SetNormalTimeMode();
  if (flight_imu1:IsAlive()) then
    StartDialog("Dialog3");
  else
    Medish_ComeOn();
    Timer_Seq(1, Medish_Damn);
  end;
end;


--info:top=380,left=30,allvis=0;
function Medish_ComeOn()
  NewPirateFlight("Medish_Group", "med2", mothership:GetPosition()+Vector3(-90, 0, 10), 1, 20, Vector3(1, 0, 0));
  countf = flight_med2:GetPilotCount();
  for i=0,countf,1 do
    pilot_tmp = flight_med2:GetPilotByNumber(i);
    ship_tmp = pilot_tmp:GetShip();
    ship_tmp:PlayFX("Cloak Out.shfx");
  end;
  NewPirateFlight("Medish_Group", "med3", mothership:GetPosition()+Vector3(-90, 0, 30), 1, 19, Vector3(1, 0, 0));
  countf = flight_med3:GetPilotCount();
  for i=0,countf,1 do
    pilot_tmp = flight_med3:GetPilotByNumber(i);
    ship_tmp = pilot_tmp:GetShip();
    ship_tmp:PlayFX("Cloak Out.shfx");
  end;
  NewPirateFlight("Medish_Group", "med4", mothership:GetPosition()+Vector3(-90, 0, -30), 1, 19, Vector3(1, 0, 0));
  countf = flight_med4:GetPilotCount();
  for i=0,countf,1 do
    pilot_tmp = flight_med4:GetPilotByNumber(i);
    ship_tmp = pilot_tmp:GetShip();
    ship_tmp:PlayFX("Cloak Out.shfx");
  end;
  NewPirateFlight("Medish_Group", "med5", mothership:GetPosition()+Vector3(-90, 0, -10), 2, 20, Vector3(1, 0, 0));
  countf = flight_med5:GetPilotCount();
  for i=0,countf,1 do
    pilot_tmp = flight_med5:GetPilotByNumber(i);
    ship_tmp = pilot_tmp:GetShip();
    ship_tmp:PlayFX("Cloak Out.shfx");
  end;
  new_SetAllGroupRelations(rel_MedishWar, power_rel_1);
  if (flight_imu1:IsAlive()) then
    SedoySpeaker:Message("#L_Sedoy_01", 4, "Aia_GoNot");
  else
    count = count + 4;
    NewTrigger("f_died", DiedCondition, Caravan_Died, flight_med2);
    NewTrigger("f_died", DiedCondition, Caravan_Died, flight_med3);
    NewTrigger("f_died", DiedCondition, Caravan_Died, flight_med4);
    NewTrigger("f_died", DiedCondition, Caravan_Died, flight_med5);
  end;
end;


--info:top=420,left=30,allvis=0;
function Aia_GoNot()
  AiaSpeaker:Message("#L_Aia_01", 5);
end;


--info:top=380,left=170,allvis=0;
function Tanaka_DidIt()
  TanakaSpeaker:Message("#L_Tanaka_03", 3, "Medish_Damn");
  if (flight_tan1:IsAlive()) then
    flight_tan1:Escort(mothership);
    escortedShip = ship_tan1_1;
  else
    escortedShip = mothership;
  end;
  if (flight_tan2:IsAlive()) then flight_tan2:Escort(escortedShip); end;
  if (flight_tan3:IsAlive()) then flight_tan3:Escort(escortedShip); end;
  if (flight_tan4:IsAlive()) then flight_tan4:Escort(escortedShip); end;
end;


--info:top=410,left=140,allvis=0;
function Medish_Damn()
  MedishSpeaker:Message("#L_Medish_01", 4, "Aia_Ok");
end;


--info:top=450,left=140,allvis=0;
function Aia_Ok()
  AiaSpeaker:Message("#L_Aia_05", 5);
end;


--info:top=380,left=350,allvis=0;
function Tanaka_ComeOn()
  if (trig_TanakaComeOn) then
    trig_TanakaComeOn:Delete();
    trig_TanakaComeOn = NIL;
  end;
  mshipPos = mothership:GetPosition();
  CreatePortalGlow(mshipPos+Vector3(-40, 50, -50), Vector3(-0.5, 0, 0.5), 5);
  NewFlight("Tanaka_Group", "tan1", "Wyvern_mk2_tri1", "Tanaka", mshipPos+Vector3(-48, 50, -42), Vector3(-0.5, 0, 0.5));
  NewTriadaFlight("Tanaka_Group", "tan2", mshipPos+Vector3(-32, 50, -42), 1, 20, Vector3(-0.5, 0, 0.5));
  NewTriadaFlight("Tanaka_Group", "tan3", mshipPos+Vector3(-48, 50, -58), 1, 19, Vector3(-0.5, 0, 0.5));
  NewTriadaFlight("Tanaka_Group", "tan4", mshipPos+Vector3(-40, 40, -50), 1, 19, Vector3(-0.5, 0, 0.5));
  flight_tan2:Escort(ship_tan1_1);
  flight_tan3:Escort(ship_tan1_1);
  flight_tan4:Escort(ship_tan1_1);
  if (flight_imu1:IsAlive()) then
    Timer_Seq(4, If_AttackImu);
  else
    Timer_Seq(4, If_ImuDied);
  end;
end;


--info:top=420,left=280,allvis=0;
function If_AttackImu()
  AiaSpeaker:Message("#L_Aia_02", 4, "Tanaka_Ok");
end;


--info:top=460,left=280,allvis=0;
function Tanaka_Ok()
  TanakaSpeaker:Message("#L_Tanaka_01", 3, "Aia_Only");
  flight_tan1:EnableChaseDistanceInfinity();
  flight_tan1:Attack(ship_imu1_1);
  flight_tan2:EnableChaseDistanceInfinity();
  flight_tan2:Attack(ship_imu1_1);
  flight_tan3:EnableChaseDistanceInfinity();
  flight_tan3:Attack(ship_imu1_1);
  flight_tan4:EnableChaseDistanceInfinity();
  flight_tan4:Attack(ship_imu1_1);
end;


--info:top=500,left=280,allvis=0;
function Aia_Only()
  AiaSpeaker:Message("#L_Aia_03", 4);
end;


--info:top=420,left=410,allvis=0;
function If_ImuDied()
  TanakaSpeaker:Message("#L_Tanaka_02", 3, "Aia_Suffice");
  flight_tan1:Escort(mothership);
end;


--info:top=460,left=410,allvis=0;
function Aia_Suffice()
  AiaSpeaker:Message("#L_Aia_04", 4);
end;


--info:top=550,left=220,allvis=0;
function Caravan_Died()
  SetQuestState("quest_16b_07", QUEST_DONE);
  new_SetAllGroupRelations(rel_Neutral, power_rel_1);
  new_SetGroupRelations("UssGroup_1", PLAYER_GROUP, "CN");
  SetNormalTimeMode();
  StartDialog("Dialog4");
end;


--info:top=590,left=220,allvis=0;
function Alisa_GoOut()
  if (AlisaSpeaker:IsInBase()) or (AiaSpeaker:IsBasePilot()) then
    if (AlisaSpeaker:IsBasePilot()) then RemoveBaseDriver(); end;
    local Player_Team = CreateTeam(PLAYER_TEAM);
    Player_Team = CreateTeam(PLAYER_TEAM);
    Player_Team:RemovePilot(AlisaSpeaker);
    AlisaSpeaker:Kill();
  elseif (AlisaSpeaker:IsCanSayReplica()) then
    AlisaShip = AlisaSpeaker:GetShip();
    AlisaFlight = AlisaSpeaker:GetFlight();
    if (AlisaFlight:GetPilotCount()==1) then
      Player_Group:RemoveFlight(AlisaFlight);
      Blade_Group:AddFlight(AlisaFlight);
      AlisaFlight:Move(Vector3(300, 500, 3000));
    else
      NewAlisaFlight = Blade_Group:CreateFlight("NewAlisaFlight");
      NewAlisaFlight:AddShip(AlisaShip);
      NewAlisaFlight:Move(Vector3(300, 500, 3000));
    end;
  else
    local Player_Team = CreateTeam(PLAYER_TEAM);
    Player_Team = CreateTeam(PLAYER_TEAM);
    Player_Team:RemovePilot(AlisaSpeaker);
    AlisaSpeaker:Kill();
  end;
  Timer_Seq(5, Minus_One);
end;


--info:top=630,left=220,allvis=0;
function Minus_One()
  SetNormalTimeMode();
--------------------------------------------------------------------------------
--  StartDialog("Dialog5");
  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("Dialog5");
    end;
  else
    StartDialog("Dialog5");
  end; 
end;

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



--info:top=670,left=220,allvis=0;
function Blade_ToCrew()
  lMission = 3;
  if (flight_bl1:IsAlive()) then
    Blade_Group:RemoveFlight(flight_bl1);
    Player_Group:AddFlight(flight_bl1);
    flight_bl1:Escort(mothership);
  else
    BladeSpeaker = CreatePilot("Blade");
    AddPilotToPlayer(BladeSpeaker);
  end;
  Timer_Seq(1, autosaveMission);
end;


--info:top=270,left=580,allvis=0;
function If_Intuition()
  Timer_Seq(3, Blade_By);
end;


--info:top=310,left=580,allvis=0;
function Blade_By()
  SetNormalTimeMode();
  StartDialog("Dialog6");
end;


--info:top=350,left=580,allvis=0;
function Blade_GoOut()
  AddPost("mail_016b");
  flight_bl1:Move(Vector3(3000, 0, -3000));
  Timer_Seq(5, With_Alisa);
end;


--info:top=390,left=580,allvis=0;
function With_Alisa()
  lMission = 0;
  SetNormalTimeMode();
  StartDialog("Dialog7");
end;


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


--info:top=740,left=580,allvis=0;
function autosaveMission()
  Timer_Seq(2, Jamp_To);
  AutoSave("#AS_mission_16_2");
end;


--info:top=780,left=580,allvis=0;
function Jamp_To()
  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(6);
  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("Teleport_To");
  Anim_4:AttachAction("Teleport_To");
end;


--info:top=820,left=580,allvis=0;
function Teleport_To()
  dest_location = "el_ariash";
  CustomTeleportate("deosis", dest_location, "long_jump");
end;


--info:top=120,left=410,allvis=0;
function StartInitMission()
  TriadaTeam = CreateTeam("Triada");
  UssTeam = CreateTeam("Uss");
  PirateTeam = CreateTeam("Pirates");

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

  Player_Group = MothershipPilot:GetGroup();
  Blade_Group = TriadaTeam:CreateGroup("Blade_Group");
  Tanaka_Group = TriadaTeam:CreateGroup("Tanaka_Group");
  Imu_Group = TriadaTeam:CreateGroup("Imu_Group");
  ImuUss_Group = UssTeam:CreateGroup("ImuUss_Group");
  Medish_Group = PirateTeam:CreateGroup("Medish_Group");
  power_rel_1 = {Player_Group, Blade_Group, Tanaka_Group, Imu_Group, ImuUss_Group, UssGroup_1, Medish_Group};
  rel_Neutral = {{"F"},
                 {"F","F"},
                 {"N","N","N"},
                 {"N","N","N","F"},
                 {"N","N","N","F","F"},
                 {"N","N","N","N","N","N"}};
  rel_ImuWar = {{"X"},
                {"X","X"},
                {"W","W","W"},
                {"W","W","W","X"},
                {"W","W","X","X","X"},
                {"X","X","X","X","X","X"}};
  rel_MedishWar = {{"X"},
                   {"X","X"},
                   {"X","X","X"},
                   {"X","X","X","X"},
                   {"X","X","X","X","X"},
                   {"W","W","W","F","F","X"}};
  new_SetAllGroupRelations(rel_Neutral, power_rel_1);

  HeroSpeaker = GetPilot("Hero");
  SedoySpeaker = GetPilot("Sedoy");
  AlisaSpeaker = GetPilot("Alisa");
  KalemSpeaker = GetPilot("Kalem");
  LastochkaSpeaker = GetPilot("Lastochka2");
  AiaSpeaker = GetPilot("Aia");
  MedishSpeaker = CreatePilot("m17b_Medish");
  TanakaSpeaker = CreatePilot("Tanaka");

-- отряд Блэйда
  last_loc = GetLastLocation();
  if (last_loc=="kadenoa") then
    NewFlight("Blade_Group", "bl1", "Wyvern_mk2_tri1", "Blade", PORTAL_L:GetPosition()+Vector3(30, 0, -30), Vector3(1, 0, 0));
    NewTriadaFlight("Blade_Group", "bl2", PORTAL_L:GetPosition()+Vector3(20, 0, -40), 1, 20, Vector3(1, 0, 0));
    flight_bl2:Escort(ship_bl1_1);
    NewTriadaFlight("Blade_Group", "bl3", PORTAL_L:GetPosition()+Vector3(20, 0, -20), 1, 19, Vector3(1, 0, 0));
    flight_bl3:Escort(ship_bl1_1);
    NewTriadaFlight("Blade_Group", "bl4", PORTAL_L:GetPosition()+Vector3(20, -10, -30), 1, 19, Vector3(1, 0, 0));
    flight_bl4:Escort(ship_bl1_1);
    flight_bl1:Escort(mothership);
  elseif (last_loc=="dalled") then
    NewFlight("Blade_Group", "bl1", "Wyvern_mk2_tri1", "Blade", PORTAL_U:GetPosition()+Vector3(-30, 0, -30), Vector3(0, 0, -1));
    NewTriadaFlight("Blade_Group", "bl2", PORTAL_U:GetPosition()+Vector3(-40, 0, -20), 1, 20, Vector3(0, 0, -1));
    flight_bl2:Escort(ship_bl1_1);
    NewTriadaFlight("Blade_Group", "bl3", PORTAL_U:GetPosition()+Vector3(-20, 0, -20), 1, 19, Vector3(0, 0, -1));
    flight_bl3:Escort(ship_bl1_1);
    NewTriadaFlight("Blade_Group", "bl4", PORTAL_U:GetPosition()+Vector3(-30, -10, -20), 1, 19, Vector3(0, 0, -1));
    flight_bl4:Escort(ship_bl1_1);
    flight_bl1:Escort(mothership);
  end;

  lMission = 1;

-- лететь отсюда можно только в Деосис
  Teleporter:Delete();
  Teleporter = CreateTrigger();
  Teleporter:AttachEvent(_EVENT_TIMER); 
  Teleporter:SetTime(3);
  Teleporter:AttachCondition(TrueCondition);
  Teleporter:AttachAction(My_TeleporterAction);
end;


--info:top=150,left=470,allvis=0;
function My_TeleporterAction()
  SetInitTeamRelation("Uss", PLAYER_TEAM, RELATION_NEUTRAL);
  SetInitTeamRelation("Triada", PLAYER_TEAM, RELATION_NEUTRAL);
  if (lMission==1) or (lMission==2) then
    failedMission();
  else
    TeleportToLocation(dest_location);
  end;
end;


--info:top=190,left=470,allvis=0;
function failedMission()
  if (lMission==1) then
    OutputToScreenLog("#L_sys_01", 5);
  else
    OutputToScreenLog("#L_sys_02", 5);
  end;
  Timer_Seq(5, fin_failed);
end;


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


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

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


--info:top=80,left=570,allvis=0;
function ACTION_LEAVELOCATION()
  AddPost("news016_1");
  AddPost("news016_5");
  AddPost("news016_2");
  AddPost("news016_6");
  AddPost("news016_3");
  AddPost("news016_7");
  AddPost("news016_4");
  QuestActionDone();
  if (lMission==0) then
    AddPost("book016b_2");
    QuestStart("mission_17b");
  else
    AddPost("book016b_1");
    QuestStart("mission_19b");
  end;

  return TRUE;
end;


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

  return TRUE;
end;
