void SpawnFloor(string sResRef, string sSpawnTag, object oArea)
{
   // SendMessageToPC(GetFirstPC(), "Debug: Entering SpawnFloor function.");

    // Additional debugging: Print out the area tag
    //SendMessageToPC(GetFirstPC(), "Debug: Checking in area: " + GetTag(oArea));
    
    // Get the waypoint by tag
    object oWaypoint = GetWaypointByTag(sSpawnTag);

    if (GetIsObjectValid(oWaypoint))
    {
     //   SendMessageToPC(GetFirstPC(), "Debug: Waypoint found. Creating placeable.");
        CreateObject(OBJECT_TYPE_PLACEABLE, sResRef, GetLocation(oWaypoint));
    }
    else
    {
      //  SendMessageToPC(GetFirstPC(), "Debug: No valid waypoint found. Looking for waypoint with tag: " + sSpawnTag);
    }
}
void main()
{
   // SendMessageToPC(GetFirstPC(), "Debug: Script started.");

    // Additional debug message
   // SendMessageToPC(GetFirstPC(), "Debug: About to check gold.");
    // Define the tag of the placeable to check
    string sTagToCheck = "I_CAS_FLOOR";
    string sSpawnTag = "SPAWN_LAVA_HERE";
    string sResRef = "cas_floorlava";

    // Required gold amount
    int nRequiredGold = 5000;

    // Get the area of the speaker (usually the NPC or the player)
    object oArea = GetArea(OBJECT_SELF);
    object oPC = GetPCSpeaker();

  //  SendMessageToPC(oPC, "Debug: Checking gold...");

    // Check if the player has enough gold
    if (GetGold(oPC) >= nRequiredGold)
    {
     //   SendMessageToPC(oPC, "Debug: Sufficient gold. Proceeding with script.");

        // 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)
        {
         //   SendMessageToPC(oPC, "Debug: Existing placeable found. Destroying it.");
            // Delete the existing placeable
            DestroyObject(oPlaceable);

            // Set a delay to spawn a new placeable
            DelayCommand(2.0, SpawnFloor(sResRef, sSpawnTag, oArea));
        }
        else
        {
          //  SendMessageToPC(oPC, "Debug: No existing placeable found. Spawning new one.");
            // 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.");
    }
}



/* void SpawnFloor(string sResRef, string sSpawnTag, object oArea)
{
    // Get the waypoint to spawn at within the specified area
    object oWaypoint = GetNearestObjectByTag(sSpawnTag, oArea);

    // 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_floorlava";

    // Required gold amount
    int nRequiredGold = 5000;

    // Get the area of the speaker (usually the NPC or the player)
    object oArea = GetArea(OBJECT_SELF);
    object oPC = GetLastSpeaker();

    // 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.");
    }
}
*/

