#include "inc_debug"
#include "s_mmm_inc"

// Function to check if a player is in the same faction (party) as the tent's owner
int IsInParty(object oUser, object oOwner)
{
    object oMember = GetFirstFactionMember(oOwner, TRUE); // Get first PC in the owner's faction
    while (GetIsObjectValid(oMember))
    {
        if (oUser == oMember) // Found the user in the party
            return TRUE;
        
        oMember = GetNextFactionMember(oOwner, TRUE); // Get next party member
    }
    return FALSE;
}

// Function to destroy the CTF Tent area
void DestroyCTFArea(object oArea)
{
    if (GetIsObjectValid(oArea))
    {
        DestroyArea(oArea);
    }
}

void main()
{
    object oPlaceable = OBJECT_SELF;
    object oUser = GetLastUsedBy();
    
    // Check if the placeable already has an owner
    object oOwner = GetLocalObject(oPlaceable, "CAS_CTF_OWNER");

    if (GetIsObjectValid(oOwner))
    {
        if (!IsInParty(oUser, oOwner))
        {
            SendMessageToPC(oUser, "You need to be in the same party as the person who placed this tent.");
            return;
        }
    }
    else
    {
        // First player to use the tent becomes the "owner"
        SetLocalObject(oPlaceable, "CAS_CTF_OWNER", oUser);
        // Store the tent's original location
        SetLocalLocation(oPlaceable, "CAS_CTF_EXIT_LOC", GetLocation(oPlaceable));
    }

    // Check if area already exists
    object oCTFArea = GetLocalObject(oPlaceable, "CAS_CTF_AREA");
    if (!GetIsObjectValid(oCTFArea))
    {
        // Create the Capture the Flag Tent area
        oCTFArea = CreateArea("cas_ctf_home");
        SetLocalObject(oPlaceable, "CAS_CTF_AREA", oCTFArea);

        // Confirm area creation
        if (GetIsObjectValid(oCTFArea))
        {
            SendMessageToPC(oUser, "CTF Tent area created successfully.");
        }
        else
        {
            SendMessageToPC(oUser, "Failed to create CTF Tent area.");
            return;
        }

        // Mark the area as temporary
        SetLocalInt(oCTFArea, "NO_PERSISTENT_LOCATION", TRUE);
        SetLocalInt(oCTFArea, "S_DISABLE_CTF", TRUE);
        SetLocalObject(oCTFArea, "CAS_CTF_OWNER", oUser);

        // Reliable Waypoint Search
        object oObject = GetFirstObjectInArea(oCTFArea);
        object oCTFWaypoint = OBJECT_INVALID;

        while (GetIsObjectValid(oObject))
        {
            if (GetTag(oObject) == "WP_CTF_TENT_ENTER")
            {
                oCTFWaypoint = oObject;
                break;
            }
            oObject = GetNextObjectInArea(oCTFArea);
        }

        // Check if the waypoint was found
        if (!GetIsObjectValid(oCTFWaypoint))
        {
            DestroyCTFArea(oCTFArea);
            SendMessageToPC(oUser, "Error: CTF Tent entrance waypoint not found.");
            return;
        }

        SetLocalObject(oPlaceable, "CAS_CTF_WAYPOINT", oCTFWaypoint);

        // Create the exit portal inside the tent
        object oCTFExitPortal = CreateObject(OBJECT_TYPE_PLACEABLE, "cas_ctf_exit_por", GetLocation(oCTFWaypoint));
        SetLocalLocation(oCTFExitPortal, "CAS_CTF_EXIT_LOC", GetLocalLocation(oPlaceable, "CAS_CTF_EXIT_LOC"));
    }

    // Teleport user to the tent
    object oWaypoint = GetLocalObject(oPlaceable, "CAS_CTF_WAYPOINT");
    AssignCommand(oUser, JumpToObject(oWaypoint));

    // Check if all members leave, then destroy the area
    AssignCommand(oCTFArea, DelayCommand(30.0, DestroyCTFArea(oCTFArea)));
}
