#include "inc_loot"

void RemoveSpecificEffects(object oCreature, int nEffectType)
{
    effect eLoop = GetFirstEffect(oCreature);
    while (GetIsEffectValid(eLoop))
    {
        if (GetEffectType(eLoop) == nEffectType)
            RemoveEffect(oCreature, eLoop);
        
        eLoop = GetNextEffect(oCreature);
    }
}

void main()
{
    object oContainer = OBJECT_SELF;
    object oOpener = GetLastOpenedBy();

    // Check if the player has I_CAS_CASINOHANDLE in their inventory
    object oHandle = GetItemPossessedBy(oOpener, "I_CAS_CASINOHANDLE");
    if (!GetIsObjectValid(oHandle))
    {
        SendMessageToPC(oOpener, "You need a casino handle to loot this area.");
        return;
    }

    // Correctly retrieve the LOOT_ID and form the variable name
    int nLootID = GetLocalInt(oContainer, "LOOT_ID");
    string sLootedVar = "I_CHEST_CHRISTENHOLMLOOT" + IntToString(nLootID);

    if (GetLocalInt(oHandle, sLootedVar))
    {
        SendMessageToPC(oOpener, "This chest has already been looted.");
        return; // Prevent re-looting
    }

    // Set the looted variable on the casino handle
    SetLocalInt(oHandle, sLootedVar, 1);

    // Remove Invisibility and Sanctuary effects
    RemoveSpecificEffects(oOpener, EFFECT_TYPE_INVISIBILITY);
    RemoveSpecificEffects(oOpener, EFFECT_TYPE_SANCTUARY);

    // Check if the player is in Stealth Mode
    if (GetStealthMode(oOpener) != STEALTH_MODE_DISABLED)
    {
        // Move Silent skill check and breaking stealth
        int nMoveSilent = GetSkillRank(SKILL_MOVE_SILENTLY, oOpener);
        int nChanceToBreakStealth = 10; // Default chance

        // Determine the chance to break stealth based on Move Silent skill rank
        if (nMoveSilent < 40)
        {
            nChanceToBreakStealth = 100;
        }
        else if (nMoveSilent <= 50)
        {
            nChanceToBreakStealth = 80;
        }
        else if (nMoveSilent <= 60)
        {
            nChanceToBreakStealth = 70;
        }
        else if (nMoveSilent <= 70)
        {
            nChanceToBreakStealth = 60;
        }
        else if (nMoveSilent <= 80)
        {
            nChanceToBreakStealth = 50;
        }
        else if (nMoveSilent <= 90)
        {
            nChanceToBreakStealth = 10;
        }

        if (Random(100) < nChanceToBreakStealth)
        {
            // Breaking stealth
            ActionDoCommand(SetCommandable(TRUE, oOpener));
            ActionDoCommand(SetCommandable(FALSE, oOpener));
        }
    }

    // Generate gold with a 33% chance
    if (Random(100) < 33)
    {
        int nGoldAmount = Random(7001) + 5000; // Gold amount between 5000 and 12000
        LOOT_AddGold(oContainer, nGoldAmount);
    }

    // Generate at least one item from the loot table
    LOOT_GenerateFromTable(521, oContainer);

    // Additional items based on Search skill
int nSearchSkill = GetSkillRank(SKILL_SEARCH, oOpener);
if (nSearchSkill >= 20)
{
    int nExtraItems = (nSearchSkill - 20) / 20;
    int i; // Declare the loop variable outside the loop
    for (i = 0; i <= nExtraItems; i++)
    {
        LOOT_GenerateFromTable(521, oContainer);
    }
}
}
