#include "x4_inc_functions"
#include "_sh_inc_list"
void main()
{
    object oPC = GetPCSpeaker();
    object oArea = GetArea(OBJECT_SELF);

    if (GetFactionLeader(oPC) != oPC) return; //Only the party leader should be able to decide if they want to complete the quest

    //Halt if the PC is in a different area
    if (GetLocalString(oPC, "AreaString") != GetLocalString(oArea, "AreaString")) return;

    //A conditional check
    float fDistance;
    object oMember = GetFirstFactionMember(oPC);
    while (GetIsObjectValid(oMember))
    {
        fDistance = GetDistanceBetween(oMember, oPC); //Get the distance between the PC and a party member
        if (oPC != oMember && (fDistance == 0.0 || fDistance > 10.0)) //If they are in different areas or the distance is longer than 10 meters...
        {
            FloatingTextStringOnCreature("Your party wants to travel, but you are absent!", oMember, FALSE);
            return;
        }
        oMember = GetNextFactionMember(oPC);
    }

    //Get area string
    string sAreaString = GetLocalString(oArea, "AreaString");
    string sRegionString = GetStringLeft(sAreaString, 1);

    //Get the list of free areas
    string sList = "sList"+sRegionString+"q";
    object oList = GetLocalObject(GetModule(), sList);

    //Get the number of free areas
    int nCount = ListGetElementCount(oList);

    //Randomly select a free area
    int nElementIndex = Random(nCount)+1;
    string sRandomAreaString = ListGetString(oList, nElementIndex);

    //Teleport the PC (and their party) to the southern entrance waypoint of the chosen area
    object oWaypoint = GetWaypointByTag("exit_wp_"+sRandomAreaString+"_s");
    int nDistance = GetLocalInt(GetFactionLeader(oPC), "Miles");
    //Let's store the party leader on the area about to be entered
    SetLocalObject(GetArea(oWaypoint), "PartyLeader", GetFactionLeader(oPC));
    //Now let's move the party
    object oParty = GetFirstFactionMember(oPC, FALSE);
    while ( oParty != OBJECT_INVALID )
    {
        SetLocalInt(oParty, "Miles", nDistance); //Set everyone's distance travelled to that of the party leader
        SetLocalInt(oParty, "CanDiscover", GetLocalInt(oParty, "CanDiscover")+1);
        AssignCommand(oParty, ClearAllActions());

        SetLocalInt(oParty, "Miles", GetLocalInt(oParty, "Miles")+1);
        if (GetLocalString(GetModule(), "MODE") == "LOCAL" && GetIsPC(oParty)) SetCampaignInt(CharacterDB(oParty), "MILES", GetLocalInt(oParty, "Miles")); //store miles travelled in LOCAL mode
        AssignCommand(oParty, JumpToObject(oWaypoint));

        oParty = GetNextFactionMember(oPC, FALSE);
    }
}