// Function to count the total number of tokens in all stacks
#include "cas_casinoinc"


void main() 
{
    object oPC = GetLastUsedBy();
    object oSlotMachine = OBJECT_SELF;
    object oHandle = GetItemPossessedBy(oPC, "I_CAS_HANDLE");
    // Check if the slot machine is currently in use or cooling down
    if (GetLocalInt(oSlotMachine, "SLOT_MACHINE_USED")) 
    {
        SendMessageToPC(oPC, "The slot machine is cooling down. Please wait.");
        return;
    }

    SetLocalInt(oSlotMachine, "SLOT_MACHINE_USED", 1);
    DelayCommand(15.0, SetLocalInt(oSlotMachine, "SLOT_MACHINE_USED", 0));
    DelayCommand(15.0, SendMessageToPC(oPC, "The slot machine is ready for another pull!"));

    EnsureTokenPouch(object oPC);

    int nCurrentTokens = GetLocalInt(oHandle, "nCAS_TOKEN");
    int nBetAmount = GetLocalInt(oHandle, "nBET_AMOUNT"); // Using nBET_AMOUNT

    SendMessageToPC(oPC, "DEBUG: Current Tokens = " + IntToString(nCurrentTokens) + ", Bet Amount = " + IntToString(nBetAmount));

    if (nCurrentTokens < nBetAmount) 
    {
        SendMessageToPC(oPC, "Not enough tokens. You need at least " + IntToString(nBetAmount) + " tokens to play.");
        return;
    }

    // Deduct tokens if possible
    DeductTokens(oPC, nBetAmount);

    SendMessageToPC(oPC, "DEBUG: Calculating outcome...");

    // Calculate the outcome of the slot machine
    int nOutcome = Random(1000) + 1;

    // Determine the outcome and display results
    if (nOutcome <= 30) { // 3%
        DisplaySlotResults(oPC, 1, 1, 1, nBetAmount, VFX_IMP_HEAD_MIND, "Congratulations, you won tickets!");
        
    } else if (nOutcome <= 47) { // 1.67%
        DisplaySlotResults(oPC, 2, 2, 2, nBetAmount, VFX_IMP_FLAME_M, "Congratulations, you won tickets!");
    } else if (nOutcome <= 55) { // 0.83%
        DisplaySlotResults(oPC, 3, 3, 3, nBetAmount, VFX_IMP_FROST_S, "Congratulations, you won tickets!");
    } else if (nOutcome <= 59) { // 0.42%
        DisplaySlotResults(oPC, 4, 4, 4, nBetAmount, VFX_IMP_HOLY_AID, "Congratulations, you won tickets!");
    } else if (nOutcome <= 61) { // 0.25%
        DisplaySlotResults(oPC, 5, 5, 5, nBetAmount, VFX_IMP_DEATH_WARD, "Congratulations, you won tickets!");
    } else if (nOutcome == 62) { // 0.033%
        DisplaySlotResults(oPC, 6, 6, 6, nBetAmount, VFX_FNF_FIREBALL, "Congratulations, you won tickets!");
    } else {
        DisplaySlotResults(oPC, Random(6)+1, Random(6)+1, Random(6)+1, nBetAmount, VFX_NONE, "");
    }
}




