void main()
{
    object oPC = GetLastUsedBy();
    object oPotOfGreed = OBJECT_SELF;

    // Check if the Pot of Greed is currently in use
   // if (GetLocalInt(oPotOfGreed, "I_POT_IN_USE") == 1)
    {
        SendMessageToPC(oPC, "The Pot of Greed is digesting. Please wait.");
        return;
    }

    object oCasinoHandle = GetItemPossessedBy(oPC, "I_CAS_CASINOHANDLE");

    // Check if the player has the casino handler
    if (!GetIsObjectValid(oCasinoHandle))
    {
        SendMessageToPC(oPC, "You do not have a casino handler, please get one.");
        return;
    }

    // Check for enough tickets
   // Check for enough tickets
       int nTickets = GetLocalInt(oCasinoHandle, "nCAS_TICKET");
    int iCostTickets = GetLocalInt(oPotOfGreed, "iCostTickets");

    // Debug messages
    SendMessageToPC(oPC, "You have " + IntToString(nTickets) + " tickets.");
    SendMessageToPC(oPC, "Cost of using the Pot of Greed: " + IntToString(iCostTickets) + " tickets.");

    if (nTickets < iCostTickets)
    {
        SendMessageToPC(oPC, "You do not have enough tickets, go play some games!");
        return;
    }
    
    // Deduct tickets
    SetLocalInt(oCasinoHandle, "nCAS_TICKET", nTickets - iCostTickets);

    // Roll a 1d20 to determine the outcome
    int nRoll = d20();

    // Check for roll other than 20
    if (nRoll != 20)
    {
        SendMessageToPC(oPC, "The Pot of Greed devours your tickets");
      //  SetLocalInt(oPotOfGreed, "I_POT_IN_USE", 1);
       // DelayCommand(10.0, SetLocalInt(oPotOfGreed, "I_POT_IN_USE", 0));
        return;
    }

    // Apply VFX and speak action if roll is 20
    ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_FNF_DEMON_HAND), oPotOfGreed);
    ActionSpeakString("Vomits out something shiny");

    // Roll a 1d20 to determine the item
    int nItemRoll = d20();
    string sItemTag;

    switch (nItemRoll)
    {
        case 1: sItemTag = "mmat_humanskull"; break;
        case 2: sItemTag = "icf_sickle001"; break;
        case 3: sItemTag = "mmat_topaz"; break;
        case 4: sItemTag = "amb_i_gems009"; break;
        case 5: sItemTag = "icf_whip006"; break;
        case 6: sItemTag = "amb_i_jewels478"; break;
        case 7: sItemTag = "amb_i_jewels002"; break;
        case 8: sItemTag = "amb_i_jewels019"; break;
        case 9: sItemTag = "amb_i_jewels047"; break;
        case 10: sItemTag = "amb_i_jewels478"; break;
        case 11: sItemTag = "amb_i_gem001"; break;
        case 12: sItemTag = "amb_i_gem004"; break;
        case 13: sItemTag = "amb_i_gem010"; break;
        case 14: sItemTag = "amb_i_jewels488"; break;
        case 15: sItemTag = "amb_i_jewels487"; break;
        case 16: sItemTag = "amb_i_gem011"; break;
        case 17: sItemTag = "amb_i_gem013"; break;
        case 18: sItemTag = "amb_i_gem009"; break;
        case 19: sItemTag = "amb_i_gem002"; break;
        case 20: sItemTag = "amb_i_jewels"; break;
        default: return; // In case of an unexpected value
    }
    CreateItemOnObject(sItemTag, oPC);
    //SetLocalInt(oPotOfGreed, "I_POT_IN_USE", 1);
    //DelayCommand(10.0, SetLocalInt(oPotOfGreed, "I_POT_IN_USE", 0));
}


