#include "inc_debug"
#include "s_mmm_inc"

void main()
{
    object oArea = OBJECT_SELF;
    object oExitingCreature = GetExitingObject();

    // Check if the exiting object is a PC
    if (!GetIsPC(oExitingCreature))
    {
        return; // Only act when a player leaves
    }

    // Loop through all objects in the area to check for remaining players
    object oPlayer = GetFirstObjectInArea(oArea);
    while (GetIsObjectValid(oPlayer))
    {
        if (GetIsPC(oPlayer)) // Found a PC still in the area
        {
            return; // Players are still inside, don't destroy
        }
        oPlayer = GetNextObjectInArea(oArea);
    }

    // No players left inside the area, delay destruction
    float fDelay = 5.0; // Delay time in seconds

    // Notify the exiting player about the delayed destruction
    SendMessageToPC(oExitingCreature, "All players have left. The CTF Tent area will be destroyed in 5 seconds.");

    // Schedule the area to be destroyed after the delay
    DelayCommand(fDelay, DestroyArea(oArea));
}
