------------------------------------------------------------
--     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()
  SetNormalTimeMode();
  StartDialog("Dialog1");
end;


--info:top=160,left=130,allvis=0;
function Aia_GoOut()
  StopHere();
  if (AiaSpeaker:IsInBase()) or (AiaSpeaker:IsBasePilot()) then
    SysSpeaker:Message("#L_sys_02", 10, "Aia_GoOut");
  else
    Timer_Seq(1, Aia_GoTo_Itan);
  end;
end;


--info:top=200,left=130,allvis=0;
function Aia_GoTo_Itan()
  local AiaFlight = AiaSpeaker:GetFlight();
  local AiaShip = AiaSpeaker:GetShip();
  if (AiaFlight:GetPilotCount() > 1) then
    local NewAiaFlight = Aia_Group:CreateFlight("NewAiaFlight");
    NewAiaFlight:AddShip(AiaShip);
    NewAiaFlight:Move(USS_STATION:GetPosition()+Vector3(-20, 30, 30));
  else
    Player_Group:RemoveFlight(AiaFlight);
    Aia_Group:AddFlight(AiaFlight);
    AiaFlight:Move(USS_STATION:GetPosition()+Vector3(-20, 30, 30));
  end;
  AddQuest("quest_17b_01", "#L_Quest_17b_01a", "#L_Quest_17b_01b", QUEST_PROCESS);
  Timer_Seq(180, Aia_ComeOn);
end;


--info:top=240,left=130,allvis=0;
function Aia_ComeOn()
  local AiaFlight = AiaSpeaker:GetFlight();
  AiaFlight:Escort(mothership);
--------------------------------------------------------------------------------
--  NewTrigger("f_object_inside", TrueCondition, Aia_Return, AiaFlight, mothership, 150);
  NewTrigger("f_object_inside", TrueCondition, count_or_go_on, AiaFlight, mothership, 150);
--------------------------------------------------------------------------------
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
      Aia_Return();
    end;
  else
    Aia_Return();
  end; 
end;

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


--info:top=280,left=130,allvis=0;
function Aia_Return()
  SetQuestState("quest_17b_01", QUEST_DONE);
  local AiaFlight = AiaSpeaker:GetFlight();
  Aia_Group:RemoveFlight(AiaFlight);
  Player_Group:AddFlight(AiaFlight);
  SetNormalTimeMode();
  StartDialog("Dialog2");
end;


--info:top=320,left=130,allvis=0;
function GoTo_Station()
  oldFun = ChangeFuncDocking("Com_Trade_Dialog", COM_STATION);
  AddQuest("quest_17b_02", "#L_Quest_17b_02a", "#L_Quest_17b_02b", QUEST_PROCESS);
  nav_current = CreateNavPoint("", mothership:GetPosition(), "nav_void.dds", RGB(255,255,255), mothership);
  LineToStation = CreateLine(nav_current, np_com, RGB(20,80,220), .3, TRUE, .2, 1);
end;


--info:top=340,left=130,allvis=0;
function Com_Trade_Dialog()
  local NShop_USS_sst = {		-- количество каждого предмета в магазине
	{0,0,12,12,0,0,12,9,		-- HC,VC guns
	0,6,6,3,0,6,3,			-- LC,PLC
	0,9,6,6,6,3,			-- PC,RG
	0,0,15,9,0,15,9,6,		-- THC,TVC
	0,9,6,3,9,6,3,			-- TLC,TPLC
	0,9,6,0,6,3,			-- TPC,TRG
	0,9,12,6,0,9,12,6,3,0,0,		-- CG,AC
	0,0,0,9,6,3,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,9,3,		-- Gun,ALS
	0,3,2,2,0,3,3,2,3,1,		-- BS_
	0,3,2,1,0,4,3,2,		-- RBot,Sh
	2,1,2,1,2,2,0,0,2,0},		-- Eng,Gun,ALS
	{0,9,7,4,0,15,10,6,12,12,8,6,	-- SM,SRM,LRM rockets
	0,6,4,10,7,5},			-- T,MIRV
	{0,0,0,0,2,		-- 1
	2,1,2,2,2,		-- 2
	4,3,3,0,0,0,0,		-- 3
	8,6,0,6,6,0,0,0,	-- 4
	0,5,0,0,0,0}};		-- 5 level ships
  CreateShop("comuss");
  AddAllToShops({"comuss"}, NShop_USS_sst, 80, 0.0, 0.0);
  UpdateTradeSystem(TRUE);

  LineToStation:Delete();
  LineToStation = NIL;
  SetQuestState("quest_17b_02", QUEST_DONE);
  MothershipFlight:Stop();
  SetTraderFlag(TRUE);
  TradeStationDlg("comuss");
  Timer_Seq(1, What_Next);
end;


--info:top=380,left=130,allvis=0;
function What_Next()
  ChangeFuncDocking(oldFun, COM_STATION);
  SetNormalTimeMode();
  StartDialog("Dialog3");
end;


--info:top=430,left=210,allvis=0;
function GoTo_X297()
  AddQuest("quest_17b_04", "#L_Quest_17b_04a", "#L_Quest_17b_04b", QUEST_PROCESS);
  LineToPortal = CreateLine(nav_current, NP_PORTAL_U, RGB(20,80,220), .3, TRUE, .2, 1);
end;


--info:top=430,left=40,allvis=0;
function GoTo_X220()
  AddQuest("quest_17b_03", "#L_Quest_17b_03a", "#L_Quest_17b_03b", QUEST_PROCESS);
  LineToStation = CreateLine(nav_current, np_USS, RGB(20,80,220), .3, TRUE, .2, 1);
  NewTrigger("inside", TrueCondition, Jamp_To, mothership, USS_STATION:GetPosition(), 150);
end;


--info:top=470,left=40,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=510,left=40,allvis=0;
function Teleport_To()
  dest_location = "x220";
  CustomTeleportate("el_ariash", dest_location, "long_jump");
end;


--info:top=120,left=410,allvis=0;
function StartInitMission()
  HunterTeam = CreateTeam("Hunter");
  TriadaTeam = CreateTeam("Triada");

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

  Player_Group = MothershipPilot:GetGroup();
  Hunter_Group = HunterTeam:CreateGroup("Hunter_Group");
  Aia_Group = TriadaTeam:CreateGroup("Aia_Group");
  new_SetGroupRelations("Aia_Group", PLAYER_GROUP, "F");

  HeroSpeaker = GetPilot("Hero");
  SedoySpeaker = GetPilot("Sedoy");
  AlisaSpeaker = GetPilot("Alisa");
  KalemSpeaker = GetPilot("Kalem");
  LastochkaSpeaker = GetPilot("Lastochka2");
  AiaSpeaker = GetPilot("Aia");
  SysSpeaker = CreatePilot("Hint");

-- лететь отсюда можно только в пограничные системы
  Teleporter:Delete();
  Teleporter = CreateTrigger();
  Teleporter:AttachEvent(_EVENT_TIMER); 
  Teleporter:SetTime(3);
  Teleporter:AttachCondition(TrueCondition);
  Teleporter:AttachAction(My_TeleporterAction);
-- местное население
  DisableRandom();
-- пиратеги разбежались
  flight_Pir01:Delete();
  flight_Pir02:Delete();
  flight_Pir03:Delete();
  flight_Pir04:Delete();
  flight_Pir05:Delete();
  flight_Pir06:Delete();
-- транспорты эвакуации
  NewUSSFlight("UssGroup_1", "ussE1", FACTORY:GetPosition()+Vector3(-50, 0, 0), 3, 3, Vector3(-0.5, 0, -0.5));
  flight_ussE1:Patrol({STO_Station:GetPosition()+Vector3(0, 60, 0), TRADE_STATION:GetPosition()+Vector3(0, 60, 0), PORTAL_D:GetPosition()+Vector3(0, 0, 30), PORTAL_D:GetPosition()}, TRUE);
  NewTrigger("f_object_inside", trigger_inside2portal_Condition, trigger_inside2portal_Action, flight_ussE1, PORTAL_D, 20);
  NewUSSFlight("UssGroup_1", "ussE2", TRADE_STATION:GetPosition()+Vector3(0, 60, 0), 3, 3, Vector3(1, 0, 0));
  flight_ussE2:Patrol({STO_Station:GetPosition()+Vector3(0, 60, 0), FACTORY:GetPosition()+Vector3(-50, 0, 0), PORTAL_D:GetPosition()+Vector3(0, 0, 30), PORTAL_D:GetPosition()}, TRUE);
  NewTrigger("f_object_inside", trigger_inside2portal_Condition, trigger_inside2portal_Action, flight_ussE2, PORTAL_D, 20);
  NewUSSFlight("UssGroup_1", "ussE3", STO_Station:GetPosition()+Vector3(0, 60, 0), 3, 7, Vector3(0.5, 0, 0.5));
  flight_ussE3:Patrol({STO_Station:GetPosition()+Vector3(0, 60, 0), FACTORY:GetPosition()+Vector3(-50, 0, 0), TRADE_STATION:GetPosition()+Vector3(0, 60, 0)}, TRUE);
  NewUSSFlight("UssGroup_1", "ussE4", USS_STATION:GetPosition()+Vector3(0, 30, 50), 3, 7, Vector3(0.5, 0, 0.5));
  flight_ussE4:Patrol({COM_STATION:GetPosition()+Vector3(-50, 60, 0), USS_STATION:GetPosition()+Vector3(0, 30, 50)}, TRUE);
  NewUSSFlight("UssGroup_1", "ussE5", PORTAL_L:GetPosition()+Vector3(10, 0, 0), 3, 10, Vector3(1, 0, 0));
  flight_ussE5:Patrol({COM_STATION:GetPosition()+Vector3(-50, 60, 0), TRADE_STATION:GetPosition()+Vector3(0, 60, 0), USS_STATION:GetPosition()+Vector3(0, 30, 50), PORTAL_D:GetPosition()+Vector3(0, 0, 30), PORTAL_D:GetPosition()}, TRUE);
  NewTrigger("f_object_inside", trigger_inside2portal_Condition, trigger_inside2portal_Action, flight_ussE5, PORTAL_D, 20);
-- охрана у портала
  NewHunterFlight("Hunter_Group", "hun1", PORTAL_U:GetPosition()+Vector3(200, 0, -100), 1, 19, Vector3(-1, 0, 0));
  flight_hun1:Patrol({PORTAL_U:GetPosition()+Vector3(0, 50, -200), PORTAL_U:GetPosition()+Vector3(-200, 0, -100), PORTAL_U:GetPosition()+Vector3(200, 0, -100)}, TRUE);
  NewHunterFlight("Hunter_Group", "hun2", PORTAL_U:GetPosition()+Vector3(-200, 0, -100), 1, 20, Vector3(-1, 0, 0));
  flight_hun1:Patrol({PORTAL_U:GetPosition()+Vector3(200, 0, -100), PORTAL_U:GetPosition()+Vector3(0, 50, -200), PORTAL_U:GetPosition()+Vector3(-200, 0, -100)}, TRUE);
  NewHunterFlight("Hunter_Group", "hun3", PORTAL_U:GetPosition()+Vector3(100, 0, -50), 1, 20, Vector3(-0.67, 0, 0.33));
  NewHunterFlight("Hunter_Group", "hun4", PORTAL_U:GetPosition()+Vector3(120, 0, -50), 1, 19, Vector3(-0.67, 0, 0.33));
  NewHunterFlight("Hunter_Group", "hun5", PORTAL_U:GetPosition()+Vector3(100, 0, -70), 2, 20, Vector3(-0.67, 0, 0.33));
  NewUSSFlight("UssGroup_1", "uss1", PORTAL_U:GetPosition()+Vector3(-100, 0, -100), 1, 20, Vector3(0.5, 0, -0.5));
  NewUSSFlight("UssGroup_1", "uss2", PORTAL_U:GetPosition()+Vector3(-120, 0, -100), 1, 19, Vector3(0.5, 0, -0.5));
  NewUSSFlight("UssGroup_1", "uss3", PORTAL_U:GetPosition()+Vector3(-100, 0, -120), 1, 19, Vector3(0.5, 0, -0.5));
  NewUSSFlight("UssGroup_1", "uss4", PORTAL_U:GetPosition()+Vector3(-110, -20, -110), 2, 20, Vector3(0.5, 0, -0.5));
-- вылет мазершипа
  CreatePortalGlow(Vector3(50, 200, -100), Vector3(-0.5, 0, 0.5), 7);
  mothership:SetPosition(Vector3(50, 200, -100));
  mothership:SetOrientation(CreateQuaternionFromVector3(Vector3(-0.5, 0, 0.5)));
  mothership:PlayFX("Portal Out.shfx");
  mothership:ApplyImpulse(15, Vector3(-0.5, 0, 0.5));
  FocusOn({mothership}, Vector3(0.6601, 0.4, 0.2534), 60);
end;


--info:top=160,left=410,allvis=0;
function My_TeleporterAction()
  SetInitTeamRelation("Uss", PLAYER_TEAM, GetGroupRelation("UssGroup_1", PLAYER_GROUP));
  if (dest_location=="hanoi") or (dest_location=="dalled") then
    failedMission();
  elseif (GetQuestState("quest_17b_03")~=QUEST_PROCESS) and (GetQuestState("quest_17b_04")~=QUEST_PROCESS) then
    failedMission();
  else
    TeleportToLocation(dest_location);
  end;
end;


--info:top=200,left=410,allvis=0;
function failedMission()
  OutputToScreenLog("#L_sys_01", 5);
  Timer_Seq(5, fin_failed);
end;


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


--info:top=80,left=130,allvis=0;
function ACTION_INIT()
  SetQuestLabel("CurrentQuest","17b");
  StartInitMission();

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


--info:top=80,left=570,allvis=0;
function ACTION_LEAVELOCATION()
  EnableRandom();
  if (tmpNull) then tmpNull:Delete(); end;
  if (LineToStation) then LineToStation:Delete(); end;
  if (LineToPortal) then LineToPortal:Delete(); end;
  if (nav_current) then nav_current:Delete(); end;
  QuestActionDone();
  if (GetQuestState("quest_17b_03")==QUEST_PROCESS) then
    SetQuestState("quest_17b_03", QUEST_DONE);
    ClearQuests();
    QuestStart("mission_17b_1");
  end;

  return TRUE;
end;


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

  return TRUE;
end;
