// This script is attached to the OnUsed event of a placeable.
// Function to move the creature to the waypoint after a delay
void MoveCreatureToWaypoint(object oCreature, string sWaypointTag)
{
    object oWaypoint = GetWaypointByTag(sWaypointTag);
    if (oWaypoint == OBJECT_INVALID)
    {
        SendMessageToPC(oCreature, "Security waypoint not found.");
        return;
    }

    // Get the location of the waypoint
    location lDestination = GetLocation(oWaypoint);

    // Move the creature to the waypoint
    AssignCommand(oCreature, ActionJumpToLocation(lDestination));
    SendMessageToPC(oCreature, "You've been sent to the security cells of the Casino. Please wait for the security to come deal with you.");
}


void main()
{
    // Get the object that used the placeable
    object oUser = GetLastUsedBy();
    
    // Check if the user is in the specified area
    object oArea = GetArea(oUser);
    if(GetTag(oArea) != "A_CAS_CASINOCOINS")
    {
        // If the user is not in the specified area, do nothing
        return;
    }

    // Check for creatures in the area with the cas_trapdooriago variable set
    object oCreature = GetFirstObjectInArea(oArea);
    while(oCreature != OBJECT_INVALID)
    {
        if(GetLocalInt(oCreature, "cas_trapdooriago") > 0)
        {
            // Make the creature speak the line
            ActionSpeakString(oCreature, "The floor suddenly drops and you fall into the security room where ropes try and wrap you into a secure state!");

            // Use DelayCommand to wait 3 seconds before moving the creature
            DelayCommand(3.0, MoveCreatureToWaypoint(oCreature, "WP_CAS_SECURITY"));
        }
        
        oCreature = GetNextObjectInArea(oArea);
    }
}

// Function to move the creature to the waypoint after a delay
void MoveCreatureToWaypoint(object oCreature, string sWaypointTag)
{
    object oWaypoint = GetWaypointByTag(sWaypointTag);
    if (oWaypoint == OBJECT_INVALID)
    {
        SendMessageToPC(oCreature, "Security waypoint not found.");
        return;
    }

    // Get the location of the waypoint
    location lDestination = GetLocation(oWaypoint);

    // Move the creature to the waypoint
    AssignCommand(oCreature, ActionJumpToLocation(lDestination));
    SendMessageToPC(oCreature, "You've been sent to the security cells of the Casino. Please wait for the security to come deal with you.");
}
