// 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 placeable object itself
    object oPlaceable = OBJECT_SELF;

    // Get the area where the placeable is located
    object oArea = GetArea(oPlaceable);
    
    // Ensure the script operates only in the specified area
    if(GetTag(oArea) != "A_CAS_CASINOCOINS")
    {
        // Exit if the placeable is not in the specified area
        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
            AssignCommand(oCreature, ActionSpeakString("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"));
        }
        
        // Move to the next creature in the area
        oCreature = GetNextObjectInArea(oArea);
    }
}

