void SpawnFloor(string sResRef, string sSpawnTag, object oArea)
{
    // Get the waypoint to spawn at within the specified area
     object oWaypoint = GetWaypointByTag(sSpawnTag);

    // Check if the waypoint is valid
    if (GetIsObjectValid(oWaypoint))
    {
        // Create the placeable at the waypoint
        CreateObject(OBJECT_TYPE_PLACEABLE, sResRef, GetLocation(oWaypoint));
    }
}

void main()
{
    // Define the tag of the placeable to check
    string sTagToCheck = "I_CAS_FLOOR";
    string sSpawnTag = "SPAWN_FLOOR_HERE";
    string sResRef = "cas_floormud";

    // Required gold amount
    int nRequiredGold = 500;

    // Get the area of the speaker (usually the NPC or the player)
    object oArea = GetArea(OBJECT_SELF);
    object oPC = GetPCSpeaker();

    // Check if the player has enough gold
    if (GetGold(oPC) >= nRequiredGold)
    {
        // Deduct gold from the player
        TakeGoldFromCreature(nRequiredGold, oPC, TRUE);

        // Get the object to be checked
        object oPlaceable = GetNearestObjectByTag(sTagToCheck, OBJECT_SELF);

        // Check if the object is valid and in the same area
        if (GetIsObjectValid(oPlaceable) && GetArea(oPlaceable) == oArea)
        {
            // Delete the existing placeable
            DestroyObject(oPlaceable);

            // Set a delay to spawn a new placeable
            DelayCommand(2.0, SpawnFloor(sResRef, sSpawnTag, oArea));
        }
        else
        {
            // If no placeable is found, spawn a new one immediately
            SpawnFloor(sResRef, sSpawnTag, oArea);
        }
    }
    else
    {
        // Inform the player they do not have enough gold
        SendMessageToPC(oPC, "You do not have enough gold to spawn the floor tile.");
    }
}


