#include "inc_debug"
#include "s_mmm_inc"

void main()
{
    object oPortal = OBJECT_SELF;
    object oUser = GetLastUsedBy();

    // Retrieve tent object and the stored entry location
    object oTent = GetLocalObject(oPortal, "CAS_CTF_TENT_OBJECT");
    location lExitLocation = GetLocalLocation(oTent, "CAS_CTF_EXIT_LOC");

    if (!GetIsObjectValid(oTent))
    {
        SendMessageToPC(oUser, "Error: Tent reference is invalid.");
        return;
    }

    // Check if the exit location is valid
    object oArea = GetAreaFromLocation(lExitLocation);
    vector vPosition = GetPositionFromLocation(lExitLocation);

    if (GetIsObjectValid(oArea) && (vPosition.x != 0.0 || vPosition.y != 0.0 || vPosition.z != 0.0))
    {
        // Teleport the player back to the original location
        AssignCommand(oUser, JumpToLocation(lExitLocation));
        SendMessageToPC(oUser, "You have exited the CTF Tent and returned to where you entered.");
    }
    else
    {
        SendMessageToPC(oUser, "Error: Original entry location is invalid.");
    }
}
