#include "nw_i0_generic"

// Function to spawn a mimic creature at a given location
void SpawnMimic(location lSpawnLocation, string sCreatureTag)
{
    CreateObject(OBJECT_TYPE_CREATURE, sCreatureTag, lSpawnLocation);
}

// Function to apply VFX and play sound at a location
void ApplyEffects(location lSpawnLocation)
{
    // Apply the VFX
    ApplyEffectAtLocation(DURATION_TYPE_INSTANT, EffectVisualEffect(620), lSpawnLocation);
    // Play the sound
    PlaySound("as_cv_woodbreak1");
}

void main()
{
    // Have OBJECT_SELF announce the mimic attack
    ActionSpeakString("Various objects around burst into action, the mimics attack!");

    // Get the object that triggered the disturbance
    object oDisturber = GetLastDisturbed();
    // Get the chest (caller of this script)
    object oChest = OBJECT_SELF;
    // Get the area of the chest
    object oArea = GetArea(oChest);

    // Define tags
    string sMimicTag = "cas_mimic";            // Tag for the mimic creature
    string sMimicBookTag = "cas_mimicbook";    // Tag for the mimic book creature
    string sMimicCoinTag = "cas_mimiccoin";    // Tag for the mimic coin creature
    string sMimicChestTag = "CAS_MIMIC_CHEST"; // Tag for the mimic chests
    string sMimicBookPlaceableTag = "CAS_MIMIC_BOOK"; // Tag for the mimic book placeables
    string sMimicCoinPlaceableTag = "CAS_MIMIC_COIN"; // Tag for the mimic coin placeables
    string sTreasureTag = "CAS_TREASURE";      // Tag for the treasure item

    // Remove the CAS_TREASURE item from the player's inventory
    object oItem = GetFirstItemInInventory(oDisturber);
    while (GetIsObjectValid(oItem))
    {
        if (GetTag(oItem) == sTreasureTag)
        {
            DestroyObject(oItem);
            break; // Exit loop after destroying the first matching item
        }
        oItem = GetNextItemInInventory(oDisturber);
    }

    // Loop through all placeables in the area with a delay for each spawn
    float fDelay = 0.0;
    object oPlaceable = GetFirstObjectInArea(oArea);
    while (GetIsObjectValid(oPlaceable))
    {
        string sTag = GetTag(oPlaceable);

        // Debugging: Speak tag being processed
       // SpeakString("Processing placeable with tag: " + sTag);

        // Handle mimic chest
        if (sTag == sMimicChestTag)
        {
            location lSpawnLocation = GetLocation(oPlaceable);

            // Apply effects and spawn the mimic with delays
            DelayCommand(fDelay, ApplyEffects(lSpawnLocation));
            DelayCommand(fDelay + 0.5, SpawnMimic(lSpawnLocation, sMimicTag));
            DelayCommand(fDelay + 1.0, DestroyObject(oPlaceable)); // Destroy the object after the mimic spawns
            fDelay += 0.5;
        }
        // Handle mimic book
        else if (sTag == sMimicBookPlaceableTag)
        {
            location lSpawnLocation = GetLocation(oPlaceable);

            // Apply effects and spawn the mimic book with delays
            DelayCommand(fDelay, ApplyEffects(lSpawnLocation));
            DelayCommand(fDelay + 0.5, SpawnMimic(lSpawnLocation, sMimicBookTag));
            DelayCommand(fDelay + 1.0, DestroyObject(oPlaceable)); // Destroy the object after the mimic spawns
            fDelay += 0.5;
        }
        // Handle mimic coin
        else if (sTag == sMimicCoinPlaceableTag)
        {
            location lSpawnLocation = GetLocation(oPlaceable);

            // Apply effects and spawn the mimic coin with delays
            DelayCommand(fDelay, ApplyEffects(lSpawnLocation));
            DelayCommand(fDelay + 0.5, SpawnMimic(lSpawnLocation, sMimicCoinTag));
            DelayCommand(fDelay + 1.0, DestroyObject(oPlaceable)); // Destroy the object after the mimic spawns
            fDelay += 0.5;
        }

        // Advance to the next object in the area
        oPlaceable = GetNextObjectInArea(oArea);
    }

    // Debugging: Confirm end of processing
    SpeakString("Mimic spawning complete.");
}
