------------------------------------------------------------
--     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()
  if (GetQuestState("quest_14e_05")==QUEST_PROCESS) then
    StartDialog("Dialog1");
    lMission = 0;
  else
    StartDialog("Dialog2");
    lMission = 1;
  end;
end;

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

function GoTo_Station()
  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() >= 4) then
      choose_pilot();
    else
      GoTo_Station_1();
    end;
  else
    GoTo_Station_1();
  end; 
end;

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


--info:top=160,left=80,allvis=0;
--------------------------------------------------------------------------------
--function GoTo_Station()
function GoTo_Station_1()
--------------------------------------------------------------------------------
  if (YanaSpeaker:IsInBase()) or (YanaSpeaker:IsBasePilot()) then
    local Player_Team = CreateTeam(PLAYER_TEAM);
    Player_Team:RemovePilot(YanaSpeaker);
    local ship = CreateCarcass("Puma_pl1", mothership:GetPosition()+Vector3(20, -20, 20), Vector3(1, 0, 0));
    ship:AssignPilot(YanaSpeaker);
    YanaFlight = Grey_Group:CreateFlight("YanaFlight");
    YanaFlight:AddShip(ship);
  else
    YanaShip = YanaSpeaker:GetShip();
    YanaFlight = YanaSpeaker:GetFlight();
    if (YanaFlight:GetPilotCount()==1) then
      Player_Group:RemoveFlight(YanaFlight);
      Grey_Group:AddFlight(YanaFlight);
    else
      NewYanaFlight = Grey_Group:CreateFlight("NewYanaFlight");
      NewYanaFlight:AddShip(YanaShip);
    end;
  end;
  flight_Yana = YanaSpeaker:GetFlight();
  flight_Yana:Move(Vector3(-3000, 500, 3000));
  if (flight_alex1) then
    Grey_Group:RemoveFlight(flight_alex1);
    Player_Group:AddFlight(flight_alex1);
    flight_alex1:Escort(mothership);
    Grey_Group:RemoveFlight(flight_alex2);
    Player_Group:AddFlight(flight_alex2);
    flight_alex2:Escort(mothership);
  end;
  Timer_Seq(1, Start_2);
end;


--info:top=190,left=150,allvis=0;
function Start_2()
  if (GetQuestState("quest_14e_05")==QUEST_PROCESS) then SetQuestState("quest_14e_05", QUEST_DONE); end;
  if (GetQuestState("quest_14e_06")==QUEST_PROCESS) then SetQuestState("quest_14e_06", QUEST_DONE); end;
  if (GetQuestState("quest_14e_07")~=QUEST_PROCESS) then
    AddQuest("quest_14e_07", "#L_Quest_14e_07a", "#L_Quest_14e_07b", QUEST_PROCESS);
  end;
  local last_loc = GetLastLocation();
  if (last_loc=="proserpina") then
    LineToStation = CreateLine(NP_PORTAL_D, np_trade, RGB(20,80,220), .3, TRUE, .2, 1);
  elseif (last_loc=="torr") then
    LineToStation = CreateLine(NP_PORTAL_L, np_trade, RGB(20,80,220), .3, TRUE, .2, 1);
  else
    LineToStation = CreateLine(NP_PORTAL_U, np_trade, RGB(20,80,220), .3, TRUE, .2, 1);
  end;
  NewTrigger("inside", TrueCondition, Near_Station, mothership, TRADE:GetPosition(), 150);
end;


--info:top=230,left=150,allvis=0;
function Near_Station()
  LineToStation:Delete();
  LineToStation = NIL;
  SetNormalTimeMode();
  MothershipFlight:Stop();
  SetQuestState("quest_14e_07", QUEST_DONE);
  SetStopESC(TRUE);
  Anim_1 = CreateMovie(_ASIMUTHCAM);
  Anim_1:SetLength(12);
  Anim_1:SetInterpolate(_LINEAR);
  Anim_1:SetTarget(ship_nm1_1);
  Anim_1:InitMovie(1, TRUE, 5);
  Anim_1:AddKeys(224.3069, 95.4849, 48.18, 0);
  Anim_1:AddKeys(67.3815, 59.1708, 48.18, 12);
  Anim_1:Play();
  Anim_1:AttachEscapeScript("finMission");
  Anim_1:AttachAction("finMission");
  Change_Mathership();
end;


--info:top=260,left=190,allvis=0;
function Change_Mathership()
  StopDocking(TRADE);
  StopDocking(STO_Station);
  mothership_old = mothership;
  mothership = SetPlayerMotherShip(ship_nm1_1);

  PortalTrigger_PORTAL_L:SetObject(mothership);
  PortalTrigger_PORTAL_D:SetObject(mothership);
  PortalTrigger_PORTAL_U:SetObject(mothership);
  PortalsActivate_Action();
  GetShipName(mothership);
  CreateTradeStationDocking(TRADE,mothership);
  CreateRepairStationDocking(STO_Station,mothership,110,1);

  ClericSpeaker = CreatePilot("Cleric");
  ClericSpeaker:SetPerk("Gunnery_3");
  ClericSpeaker:SetPerk("Learner");
  ClericSpeaker:SetPerk("Missiles_3");
  ClericSpeaker:SetPerk("F_Weapon_Laser");
  ClericSpeaker:SetPerk("Systems_4");
  ClericSpeaker:SetPerk("Countermeasures_Exp");
  ClericSpeaker:SetPerk("Shields_Exp");
  ClericSpeaker:SetPerk("Piloting_3");
  AddPilotToPlayer(ClericSpeaker);
end;


--info:top=280,left=150,allvis=0;
function finMission()
  FocusOn(mothership, Vector3(-1,0.3,0), 37.32);
  GetShipName(mothership);
  local tmpPilot = CreatePilot("BasePilot");
  mothership_old:AssignPilot(tmpPilot);
  local flight = Grey_Group:CreateFlight("tmpFlightM");
  flight:AddShip(mothership_old);
  flight:Move(STO_Station:GetPosition()+Vector3(60, 10, -60));
  if (lMission==0) then
    AddPost("book014e_2");
  else
    AddPost("book014e_3");
  end;
  AddQuest("quest_15eA_01", "#L_Quest_15eA_01a", "#L_Quest_15eA_01b", QUEST_PROCESS);
  CreateRoute("nk", "torr", "endoria1", "kruger", "shula", "eridan");
  Timer_Seq(1, autosaveMission);
end;


--info:top=320,left=150,allvis=0;
function autosaveMission()
  AutoSave("#AS_mission_14_1");
end;


--info:top=120,left=300,allvis=0;
function StartInitMission()
  GreyTeam = CreateTeam("Grey");

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

  Player_Group = MothershipPilot:GetGroup();
  Grey_Group = GreyTeam:CreateGroup("Grey_Group");
  new_SetGroupRelations("Grey_Group", PLAYER_GROUP, "N");
  new_SetGroupRelations("UftGroup_1", PLAYER_GROUP, "N");
  new_SetGroupRelations("Grey_Group", "UftGroup_1", "F");

  HeroSpeaker = GetPilot("Hero");
  SedoySpeaker = GetPilot("Sedoy");
  YanaSpeaker = GetPilot("Yana");
  KalemSpeaker = GetPilot("Kalem");

  if (GetQuestState("quest_14e_07")~=QUEST_PROCESS) then
    local last_loc = GetLastLocation();
    if (last_loc=="proserpina") then
      NewFlight("Grey_Group", "alex1", "Gunslinger_gr2", "Alex", PORTAL_D:GetPosition()+Vector3(50, 0, 50), Vector3(-0.5, 0, -0.5));
      NewFlight("Grey_Group", "alex2", "Gunslinger_gr2", "Natali", PORTAL_D:GetPosition()+Vector3(60, 10, 60), Vector3(-0.5, 0, -0.5));
    elseif (last_loc=="torr") then
      NewFlight("Grey_Group", "alex1", "Gunslinger_gr2", "Alex", PORTAL_D:GetPosition()+Vector3(50, 0, -50), Vector3(-0.5, 0, 0.5));
      NewFlight("Grey_Group", "alex2", "Gunslinger_gr2", "Natali", PORTAL_D:GetPosition()+Vector3(60, 10, -60), Vector3(-0.5, 0, 0.5));
    else
      NewFlight("Grey_Group", "alex1", "Gunslinger_gr2", "Alex", PORTAL_D:GetPosition()+Vector3(-50, 0, -50), Vector3(-0.5, 0, 0.5));
      NewFlight("Grey_Group", "alex2", "Gunslinger_gr2", "Natali", PORTAL_D:GetPosition()+Vector3(-60, 10, -60), Vector3(-0.5, 0, 0.5));
    end;
  end;
  pilot_alex1_1:SetPerk("Pilot_3");
  pilot_alex1_1:SetPerk("Pilot_4");
  pilot_alex1_1:SetPerk("Piloting_3");
  pilot_alex1_1:SetPerk("Gunnery_2");
  pilot_alex1_1:SetPerk("F_Weapon_AC");
  pilot_alex1_1:SetPerk("Missiles_3");
  pilot_alex1_1:SetPerk("Missiles_4");
  pilot_alex1_1:SetPerk("missile_salvo_2");
  pilot_alex1_1:SetPerk("missile_salvo_3");
  pilot_alex1_1:SetPerk("weak_spot_1");
  pilot_alex1_1:SetPerk("weak_spot_2");
  pilot_alex1_1:SetPerk("polarization_2");
  pilot_alex1_1:SetPerk("polarization_3");
  pilot_alex1_1:SetPerk("depolarization_1");
  pilot_alex1_1:SetPerk("depolarization_2");
  pilot_alex1_1:SetPerk("Systems_2");
  pilot_alex1_1:SetPerk("Systems_3");
  pilot_alex1_1:SetPerk("Countermeasures");
  pilot_alex1_1:SetPerk("Repairman");
  pilot_alex1_1:SetPerk("Shields_Op");
  pilot_alex1_1:SetPerk("Engine_Op");
  NewFlight("Grey_Group", "nm1", "Mothership_Lion_mk2a_grey", "BasePilot", TRADE:GetPosition()+Vector3(60, 10, -60), Vector3(0, 0, -1));

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


--info:top=160,left=300,allvis=0;
function My_TeleporterAction()
  if (GetGroupRelation("Grey_Group", PLAYER_GROUP)==RELATION_WAR) then
    OutputToScreenLog("#L_sys_01", 7);
    Timer_Seq(7, fin_failed);
  else
    Timer_Seq(1, TeleporterAction);
  end;
end;


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


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

  if (GetQuestState("quest_14e_07")~=QUEST_PROCESS) then
    Timer_Seq(5, StartMission);
  else
    Timer_Seq(1, Start_2);
  end;
  return TRUE;
end;


--info:top=80,left=570,allvis=0;
function ACTION_LEAVELOCATION()
  if (LineToStation) then LineToStation:Delete(); end;
  if (GetGroupRelation("Grey_Group", PLAYER_GROUP)==RELATION_WAR) then
    Terminate();
  elseif (GetQuestState("quest_14e_07")~=QUEST_PROCESS) then
    SetQuestLabel("EridanPermit", "yes");
    SetQuestLabel("Transfer_To_NI", "step3")
    QuestActionDone();
    QuestStart("mission_17d");
  end;

  return TRUE;
end;


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

  return TRUE;
end;
